vmwgfx_cursor_plane.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  1. // SPDX-License-Identifier: GPL-2.0 OR MIT
  2. /**************************************************************************
  3. *
  4. * Copyright (c) 2024-2025 Broadcom. All Rights Reserved. The term
  5. * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
  6. *
  7. **************************************************************************/
  8. #include "vmwgfx_cursor_plane.h"
  9. #include "vmwgfx_bo.h"
  10. #include "vmwgfx_drv.h"
  11. #include "vmwgfx_kms.h"
  12. #include "vmwgfx_resource_priv.h"
  13. #include "vmw_surface_cache.h"
  14. #include "drm/drm_atomic.h"
  15. #include "drm/drm_atomic_helper.h"
  16. #include "drm/drm_plane.h"
  17. #include <asm/page.h>
  18. #define VMW_CURSOR_SNOOP_FORMAT SVGA3D_A8R8G8B8
  19. #define VMW_CURSOR_SNOOP_WIDTH 64
  20. #define VMW_CURSOR_SNOOP_HEIGHT 64
  21. struct vmw_svga_fifo_cmd_define_cursor {
  22. u32 cmd;
  23. SVGAFifoCmdDefineAlphaCursor cursor;
  24. };
  25. /**
  26. * vmw_send_define_cursor_cmd - queue a define cursor command
  27. * @dev_priv: the private driver struct
  28. * @image: buffer which holds the cursor image
  29. * @width: width of the mouse cursor image
  30. * @height: height of the mouse cursor image
  31. * @hotspotX: the horizontal position of mouse hotspot
  32. * @hotspotY: the vertical position of mouse hotspot
  33. */
  34. static void vmw_send_define_cursor_cmd(struct vmw_private *dev_priv,
  35. u32 *image, u32 width, u32 height,
  36. u32 hotspotX, u32 hotspotY)
  37. {
  38. struct vmw_svga_fifo_cmd_define_cursor *cmd;
  39. const u32 image_size = width * height * sizeof(*image);
  40. const u32 cmd_size = sizeof(*cmd) + image_size;
  41. /*
  42. * Try to reserve fifocmd space and swallow any failures;
  43. * such reservations cannot be left unconsumed for long
  44. * under the risk of clogging other fifocmd users, so
  45. * we treat reservations separtely from the way we treat
  46. * other fallible KMS-atomic resources at prepare_fb
  47. */
  48. cmd = VMW_CMD_RESERVE(dev_priv, cmd_size);
  49. if (unlikely(!cmd))
  50. return;
  51. memset(cmd, 0, sizeof(*cmd));
  52. memcpy(&cmd[1], image, image_size);
  53. cmd->cmd = SVGA_CMD_DEFINE_ALPHA_CURSOR;
  54. cmd->cursor.id = 0;
  55. cmd->cursor.width = width;
  56. cmd->cursor.height = height;
  57. cmd->cursor.hotspotX = hotspotX;
  58. cmd->cursor.hotspotY = hotspotY;
  59. vmw_cmd_commit_flush(dev_priv, cmd_size);
  60. }
  61. static void
  62. vmw_cursor_plane_update_legacy(struct vmw_private *vmw,
  63. struct vmw_plane_state *vps)
  64. {
  65. struct vmw_surface *surface = vmw_user_object_surface(&vps->uo);
  66. s32 hotspot_x = vps->cursor.legacy.hotspot_x + vps->base.hotspot_x;
  67. s32 hotspot_y = vps->cursor.legacy.hotspot_y + vps->base.hotspot_y;
  68. if (WARN_ON(!surface || !surface->snooper.image))
  69. return;
  70. if (vps->cursor.legacy.id != surface->snooper.id) {
  71. vmw_send_define_cursor_cmd(vmw, surface->snooper.image,
  72. vps->base.crtc_w, vps->base.crtc_h,
  73. hotspot_x, hotspot_y);
  74. vps->cursor.legacy.id = surface->snooper.id;
  75. }
  76. }
  77. static enum vmw_cursor_update_type
  78. vmw_cursor_update_type(struct vmw_private *vmw, struct vmw_plane_state *vps)
  79. {
  80. struct vmw_surface *surface = vmw_user_object_surface(&vps->uo);
  81. if (surface && surface->snooper.image)
  82. return VMW_CURSOR_UPDATE_LEGACY;
  83. if (vmw->has_mob) {
  84. if ((vmw->capabilities2 & SVGA_CAP2_CURSOR_MOB) != 0)
  85. return VMW_CURSOR_UPDATE_MOB;
  86. else
  87. return VMW_CURSOR_UPDATE_GB_ONLY;
  88. }
  89. drm_warn_once(&vmw->drm, "Unknown Cursor Type!\n");
  90. return VMW_CURSOR_UPDATE_NONE;
  91. }
  92. static void vmw_cursor_update_mob(struct vmw_private *vmw,
  93. struct vmw_plane_state *vps)
  94. {
  95. SVGAGBCursorHeader *header;
  96. SVGAGBAlphaCursorHeader *alpha_header;
  97. struct vmw_bo *bo = vmw_user_object_buffer(&vps->uo);
  98. u32 *image = vmw_bo_map_and_cache(bo);
  99. const u32 image_size = vps->base.crtc_w * vps->base.crtc_h * sizeof(*image);
  100. header = vmw_bo_map_and_cache(vps->cursor.mob);
  101. alpha_header = &header->header.alphaHeader;
  102. memset(header, 0, sizeof(*header));
  103. header->type = SVGA_ALPHA_CURSOR;
  104. header->sizeInBytes = image_size;
  105. alpha_header->hotspotX = vps->cursor.legacy.hotspot_x + vps->base.hotspot_x;
  106. alpha_header->hotspotY = vps->cursor.legacy.hotspot_y + vps->base.hotspot_y;
  107. alpha_header->width = vps->base.crtc_w;
  108. alpha_header->height = vps->base.crtc_h;
  109. memcpy(header + 1, image, image_size);
  110. vmw_write(vmw, SVGA_REG_CURSOR_MOBID, vmw_bo_mobid(vps->cursor.mob));
  111. vmw_bo_unmap(bo);
  112. vmw_bo_unmap(vps->cursor.mob);
  113. }
  114. static u32 vmw_cursor_mob_size(enum vmw_cursor_update_type update_type,
  115. u32 w, u32 h)
  116. {
  117. switch (update_type) {
  118. case VMW_CURSOR_UPDATE_LEGACY:
  119. case VMW_CURSOR_UPDATE_GB_ONLY:
  120. case VMW_CURSOR_UPDATE_NONE:
  121. return 0;
  122. case VMW_CURSOR_UPDATE_MOB:
  123. return w * h * sizeof(u32) + sizeof(SVGAGBCursorHeader);
  124. }
  125. return 0;
  126. }
  127. static void vmw_cursor_mob_destroy(struct vmw_bo **vbo)
  128. {
  129. if (!(*vbo))
  130. return;
  131. ttm_bo_unpin(&(*vbo)->tbo);
  132. vmw_bo_unreference(vbo);
  133. }
  134. /**
  135. * vmw_cursor_mob_unmap - Unmaps the cursor mobs.
  136. *
  137. * @vps: state of the cursor plane
  138. *
  139. * Returns 0 on success
  140. */
  141. static int
  142. vmw_cursor_mob_unmap(struct vmw_plane_state *vps)
  143. {
  144. int ret = 0;
  145. struct vmw_bo *vbo = vps->cursor.mob;
  146. if (!vbo || !vbo->map.virtual)
  147. return 0;
  148. ret = ttm_bo_reserve(&vbo->tbo, true, false, NULL);
  149. if (likely(ret == 0)) {
  150. vmw_bo_unmap(vbo);
  151. ttm_bo_unreserve(&vbo->tbo);
  152. }
  153. return ret;
  154. }
  155. static void vmw_cursor_mob_put(struct vmw_cursor_plane *vcp,
  156. struct vmw_plane_state *vps)
  157. {
  158. u32 i;
  159. if (!vps->cursor.mob)
  160. return;
  161. vmw_cursor_mob_unmap(vps);
  162. /* Look for a free slot to return this mob to the cache. */
  163. for (i = 0; i < ARRAY_SIZE(vcp->cursor_mobs); i++) {
  164. if (!vcp->cursor_mobs[i]) {
  165. vcp->cursor_mobs[i] = vps->cursor.mob;
  166. vps->cursor.mob = NULL;
  167. return;
  168. }
  169. }
  170. /* Cache is full: See if this mob is bigger than an existing mob. */
  171. for (i = 0; i < ARRAY_SIZE(vcp->cursor_mobs); i++) {
  172. if (vcp->cursor_mobs[i]->tbo.base.size <
  173. vps->cursor.mob->tbo.base.size) {
  174. vmw_cursor_mob_destroy(&vcp->cursor_mobs[i]);
  175. vcp->cursor_mobs[i] = vps->cursor.mob;
  176. vps->cursor.mob = NULL;
  177. return;
  178. }
  179. }
  180. /* Destroy it if it's not worth caching. */
  181. vmw_cursor_mob_destroy(&vps->cursor.mob);
  182. }
  183. static int vmw_cursor_mob_get(struct vmw_cursor_plane *vcp,
  184. struct vmw_plane_state *vps)
  185. {
  186. struct vmw_private *dev_priv = vmw_priv(vcp->base.dev);
  187. u32 size = vmw_cursor_mob_size(vps->cursor.update_type,
  188. vps->base.crtc_w, vps->base.crtc_h);
  189. u32 i;
  190. u32 cursor_max_dim, mob_max_size;
  191. struct vmw_fence_obj *fence = NULL;
  192. int ret;
  193. if (!dev_priv->has_mob ||
  194. (dev_priv->capabilities2 & SVGA_CAP2_CURSOR_MOB) == 0)
  195. return -EINVAL;
  196. mob_max_size = vmw_read(dev_priv, SVGA_REG_MOB_MAX_SIZE);
  197. cursor_max_dim = vmw_read(dev_priv, SVGA_REG_CURSOR_MAX_DIMENSION);
  198. if (size > mob_max_size || vps->base.crtc_w > cursor_max_dim ||
  199. vps->base.crtc_h > cursor_max_dim)
  200. return -EINVAL;
  201. if (vps->cursor.mob) {
  202. if (vps->cursor.mob->tbo.base.size >= size)
  203. return 0;
  204. vmw_cursor_mob_put(vcp, vps);
  205. }
  206. /* Look for an unused mob in the cache. */
  207. for (i = 0; i < ARRAY_SIZE(vcp->cursor_mobs); i++) {
  208. if (vcp->cursor_mobs[i] &&
  209. vcp->cursor_mobs[i]->tbo.base.size >= size) {
  210. vps->cursor.mob = vcp->cursor_mobs[i];
  211. vcp->cursor_mobs[i] = NULL;
  212. return 0;
  213. }
  214. }
  215. /* Create a new mob if we can't find an existing one. */
  216. ret = vmw_bo_create_and_populate(dev_priv, size, VMW_BO_DOMAIN_MOB,
  217. &vps->cursor.mob);
  218. if (ret != 0)
  219. return ret;
  220. /* Fence the mob creation so we are guarateed to have the mob */
  221. ret = ttm_bo_reserve(&vps->cursor.mob->tbo, false, false, NULL);
  222. if (ret != 0)
  223. goto teardown;
  224. ret = vmw_execbuf_fence_commands(NULL, dev_priv, &fence, NULL);
  225. if (ret != 0) {
  226. ttm_bo_unreserve(&vps->cursor.mob->tbo);
  227. goto teardown;
  228. }
  229. dma_fence_wait(&fence->base, false);
  230. dma_fence_put(&fence->base);
  231. ttm_bo_unreserve(&vps->cursor.mob->tbo);
  232. return 0;
  233. teardown:
  234. vmw_cursor_mob_destroy(&vps->cursor.mob);
  235. return ret;
  236. }
  237. static void vmw_cursor_update_position(struct vmw_private *dev_priv,
  238. bool show, int x, int y)
  239. {
  240. const u32 svga_cursor_on = show ? SVGA_CURSOR_ON_SHOW
  241. : SVGA_CURSOR_ON_HIDE;
  242. u32 count;
  243. spin_lock(&dev_priv->cursor_lock);
  244. if (dev_priv->capabilities2 & SVGA_CAP2_EXTRA_REGS) {
  245. vmw_write(dev_priv, SVGA_REG_CURSOR4_X, x);
  246. vmw_write(dev_priv, SVGA_REG_CURSOR4_Y, y);
  247. vmw_write(dev_priv, SVGA_REG_CURSOR4_SCREEN_ID, SVGA3D_INVALID_ID);
  248. vmw_write(dev_priv, SVGA_REG_CURSOR4_ON, svga_cursor_on);
  249. vmw_write(dev_priv, SVGA_REG_CURSOR4_SUBMIT, 1);
  250. } else if (vmw_is_cursor_bypass3_enabled(dev_priv)) {
  251. vmw_fifo_mem_write(dev_priv, SVGA_FIFO_CURSOR_ON, svga_cursor_on);
  252. vmw_fifo_mem_write(dev_priv, SVGA_FIFO_CURSOR_X, x);
  253. vmw_fifo_mem_write(dev_priv, SVGA_FIFO_CURSOR_Y, y);
  254. count = vmw_fifo_mem_read(dev_priv, SVGA_FIFO_CURSOR_COUNT);
  255. vmw_fifo_mem_write(dev_priv, SVGA_FIFO_CURSOR_COUNT, ++count);
  256. } else {
  257. vmw_write(dev_priv, SVGA_REG_CURSOR_X, x);
  258. vmw_write(dev_priv, SVGA_REG_CURSOR_Y, y);
  259. vmw_write(dev_priv, SVGA_REG_CURSOR_ON, svga_cursor_on);
  260. }
  261. spin_unlock(&dev_priv->cursor_lock);
  262. }
  263. void vmw_kms_cursor_snoop(struct vmw_surface *srf,
  264. struct ttm_object_file *tfile,
  265. struct ttm_buffer_object *bo,
  266. SVGA3dCmdHeader *header)
  267. {
  268. struct ttm_bo_kmap_obj map;
  269. unsigned long kmap_offset;
  270. unsigned long kmap_num;
  271. SVGA3dCopyBox *box;
  272. u32 box_count;
  273. void *virtual;
  274. bool is_iomem;
  275. struct vmw_dma_cmd {
  276. SVGA3dCmdHeader header;
  277. SVGA3dCmdSurfaceDMA dma;
  278. } *cmd;
  279. int i, ret;
  280. const struct SVGA3dSurfaceDesc *desc =
  281. vmw_surface_get_desc(VMW_CURSOR_SNOOP_FORMAT);
  282. const u32 image_pitch = VMW_CURSOR_SNOOP_WIDTH * desc->pitchBytesPerBlock;
  283. cmd = container_of(header, struct vmw_dma_cmd, header);
  284. /* No snooper installed, nothing to copy */
  285. if (!srf->snooper.image)
  286. return;
  287. if (cmd->dma.host.face != 0 || cmd->dma.host.mipmap != 0) {
  288. DRM_ERROR("face and mipmap for cursors should never != 0\n");
  289. return;
  290. }
  291. if (cmd->header.size < 64) {
  292. DRM_ERROR("at least one full copy box must be given\n");
  293. return;
  294. }
  295. box = (SVGA3dCopyBox *)&cmd[1];
  296. box_count = (cmd->header.size - sizeof(SVGA3dCmdSurfaceDMA)) /
  297. sizeof(SVGA3dCopyBox);
  298. if (cmd->dma.guest.ptr.offset % PAGE_SIZE ||
  299. box->x != 0 || box->y != 0 || box->z != 0 ||
  300. box->srcx != 0 || box->srcy != 0 || box->srcz != 0 ||
  301. box->d != 1 || box_count != 1 ||
  302. box->w > VMW_CURSOR_SNOOP_WIDTH || box->h > VMW_CURSOR_SNOOP_HEIGHT) {
  303. /* TODO handle none page aligned offsets */
  304. /* TODO handle more dst & src != 0 */
  305. /* TODO handle more then one copy */
  306. DRM_ERROR("Can't snoop dma request for cursor!\n");
  307. DRM_ERROR("(%u, %u, %u) (%u, %u, %u) (%ux%ux%u) %u %u\n",
  308. box->srcx, box->srcy, box->srcz,
  309. box->x, box->y, box->z,
  310. box->w, box->h, box->d, box_count,
  311. cmd->dma.guest.ptr.offset);
  312. return;
  313. }
  314. kmap_offset = cmd->dma.guest.ptr.offset >> PAGE_SHIFT;
  315. kmap_num = (VMW_CURSOR_SNOOP_HEIGHT * image_pitch) >> PAGE_SHIFT;
  316. ret = ttm_bo_reserve(bo, true, false, NULL);
  317. if (unlikely(ret != 0)) {
  318. DRM_ERROR("reserve failed\n");
  319. return;
  320. }
  321. ret = ttm_bo_kmap(bo, kmap_offset, kmap_num, &map);
  322. if (unlikely(ret != 0))
  323. goto err_unreserve;
  324. virtual = ttm_kmap_obj_virtual(&map, &is_iomem);
  325. if (box->w == VMW_CURSOR_SNOOP_WIDTH && cmd->dma.guest.pitch == image_pitch) {
  326. memcpy(srf->snooper.image, virtual,
  327. VMW_CURSOR_SNOOP_HEIGHT * image_pitch);
  328. } else {
  329. /* Image is unsigned pointer. */
  330. for (i = 0; i < box->h; i++)
  331. memcpy(srf->snooper.image + i * image_pitch,
  332. virtual + i * cmd->dma.guest.pitch,
  333. box->w * desc->pitchBytesPerBlock);
  334. }
  335. srf->snooper.id++;
  336. ttm_bo_kunmap(&map);
  337. err_unreserve:
  338. ttm_bo_unreserve(bo);
  339. }
  340. void vmw_cursor_plane_destroy(struct drm_plane *plane)
  341. {
  342. struct vmw_cursor_plane *vcp = vmw_plane_to_vcp(plane);
  343. u32 i;
  344. vmw_cursor_update_position(vmw_priv(plane->dev), false, 0, 0);
  345. for (i = 0; i < ARRAY_SIZE(vcp->cursor_mobs); i++)
  346. vmw_cursor_mob_destroy(&vcp->cursor_mobs[i]);
  347. drm_plane_cleanup(plane);
  348. }
  349. /**
  350. * vmw_cursor_mob_map - Maps the cursor mobs.
  351. *
  352. * @vps: plane_state
  353. *
  354. * Returns 0 on success
  355. */
  356. static int
  357. vmw_cursor_mob_map(struct vmw_plane_state *vps)
  358. {
  359. int ret;
  360. u32 size = vmw_cursor_mob_size(vps->cursor.update_type,
  361. vps->base.crtc_w, vps->base.crtc_h);
  362. struct vmw_bo *vbo = vps->cursor.mob;
  363. if (!vbo)
  364. return -EINVAL;
  365. if (vbo->tbo.base.size < size)
  366. return -EINVAL;
  367. if (vbo->map.virtual)
  368. return 0;
  369. ret = ttm_bo_reserve(&vbo->tbo, false, false, NULL);
  370. if (unlikely(ret != 0))
  371. return -ENOMEM;
  372. vmw_bo_map_and_cache(vbo);
  373. ttm_bo_unreserve(&vbo->tbo);
  374. return 0;
  375. }
  376. /**
  377. * vmw_cursor_plane_cleanup_fb - Unpins the plane surface
  378. *
  379. * @plane: cursor plane
  380. * @old_state: contains the state to clean up
  381. *
  382. * Unmaps all cursor bo mappings and unpins the cursor surface
  383. *
  384. * Returns 0 on success
  385. */
  386. void
  387. vmw_cursor_plane_cleanup_fb(struct drm_plane *plane,
  388. struct drm_plane_state *old_state)
  389. {
  390. struct vmw_cursor_plane *vcp = vmw_plane_to_vcp(plane);
  391. struct vmw_plane_state *vps = vmw_plane_state_to_vps(old_state);
  392. if (!vmw_user_object_is_null(&vps->uo))
  393. vmw_user_object_unmap(&vps->uo);
  394. vmw_cursor_mob_unmap(vps);
  395. vmw_cursor_mob_put(vcp, vps);
  396. vmw_du_plane_unpin_surf(vps);
  397. vmw_user_object_unref(&vps->uo);
  398. }
  399. static bool
  400. vmw_cursor_buffer_changed(struct vmw_plane_state *new_vps,
  401. struct vmw_plane_state *old_vps)
  402. {
  403. struct vmw_bo *new_bo = vmw_user_object_buffer(&new_vps->uo);
  404. struct vmw_bo *old_bo = vmw_user_object_buffer(&old_vps->uo);
  405. struct vmw_surface *surf;
  406. bool dirty = false;
  407. int ret;
  408. if (new_bo != old_bo)
  409. return true;
  410. if (new_bo) {
  411. if (!old_bo) {
  412. return true;
  413. } else if (new_bo->dirty) {
  414. vmw_bo_dirty_scan(new_bo);
  415. dirty = vmw_bo_is_dirty(new_bo);
  416. if (dirty) {
  417. surf = vmw_user_object_surface(&new_vps->uo);
  418. if (surf)
  419. vmw_bo_dirty_transfer_to_res(&surf->res);
  420. else
  421. vmw_bo_dirty_clear(new_bo);
  422. }
  423. return dirty;
  424. } else if (new_bo != old_bo) {
  425. /*
  426. * Currently unused because the top exits right away.
  427. * In most cases buffer being different will mean
  428. * that the contents is different. For the few percent
  429. * of cases where that's not true the cost of doing
  430. * the memcmp on all other seems to outweight the
  431. * benefits. Leave the conditional to be able to
  432. * trivially validate it by removing the initial
  433. * if (new_bo != old_bo) at the start.
  434. */
  435. void *old_image;
  436. void *new_image;
  437. bool changed = false;
  438. struct ww_acquire_ctx ctx;
  439. const u32 size = new_vps->base.crtc_w *
  440. new_vps->base.crtc_h * sizeof(u32);
  441. ww_acquire_init(&ctx, &reservation_ww_class);
  442. ret = ttm_bo_reserve(&old_bo->tbo, false, false, &ctx);
  443. if (ret != 0) {
  444. ww_acquire_fini(&ctx);
  445. return true;
  446. }
  447. ret = ttm_bo_reserve(&new_bo->tbo, false, false, &ctx);
  448. if (ret != 0) {
  449. ttm_bo_unreserve(&old_bo->tbo);
  450. ww_acquire_fini(&ctx);
  451. return true;
  452. }
  453. old_image = vmw_bo_map_and_cache(old_bo);
  454. new_image = vmw_bo_map_and_cache(new_bo);
  455. if (old_image && new_image && old_image != new_image)
  456. changed = memcmp(old_image, new_image, size) !=
  457. 0;
  458. ttm_bo_unreserve(&new_bo->tbo);
  459. ttm_bo_unreserve(&old_bo->tbo);
  460. ww_acquire_fini(&ctx);
  461. return changed;
  462. }
  463. return false;
  464. }
  465. return false;
  466. }
  467. static bool
  468. vmw_cursor_plane_changed(struct vmw_plane_state *new_vps,
  469. struct vmw_plane_state *old_vps)
  470. {
  471. if (old_vps->base.crtc_w != new_vps->base.crtc_w ||
  472. old_vps->base.crtc_h != new_vps->base.crtc_h)
  473. return true;
  474. if (old_vps->base.hotspot_x != new_vps->base.hotspot_x ||
  475. old_vps->base.hotspot_y != new_vps->base.hotspot_y)
  476. return true;
  477. if (old_vps->cursor.legacy.hotspot_x !=
  478. new_vps->cursor.legacy.hotspot_x ||
  479. old_vps->cursor.legacy.hotspot_y !=
  480. new_vps->cursor.legacy.hotspot_y)
  481. return true;
  482. if (old_vps->base.fb != new_vps->base.fb)
  483. return true;
  484. return false;
  485. }
  486. /**
  487. * vmw_cursor_plane_prepare_fb - Readies the cursor by referencing it
  488. *
  489. * @plane: display plane
  490. * @new_state: info on the new plane state, including the FB
  491. *
  492. * Returns 0 on success
  493. */
  494. int vmw_cursor_plane_prepare_fb(struct drm_plane *plane,
  495. struct drm_plane_state *new_state)
  496. {
  497. struct drm_framebuffer *fb = new_state->fb;
  498. struct vmw_cursor_plane *vcp = vmw_plane_to_vcp(plane);
  499. struct vmw_plane_state *vps = vmw_plane_state_to_vps(new_state);
  500. struct vmw_plane_state *old_vps = vmw_plane_state_to_vps(plane->state);
  501. struct vmw_private *vmw = vmw_priv(plane->dev);
  502. struct vmw_bo *bo = NULL;
  503. struct vmw_surface *surface;
  504. int ret = 0;
  505. if (!vmw_user_object_is_null(&vps->uo)) {
  506. vmw_user_object_unmap(&vps->uo);
  507. vmw_user_object_unref(&vps->uo);
  508. }
  509. if (fb) {
  510. if (vmw_framebuffer_to_vfb(fb)->bo) {
  511. vps->uo.buffer = vmw_framebuffer_to_vfbd(fb)->buffer;
  512. vps->uo.surface = NULL;
  513. } else {
  514. memcpy(&vps->uo, &vmw_framebuffer_to_vfbs(fb)->uo, sizeof(vps->uo));
  515. }
  516. vmw_user_object_ref(&vps->uo);
  517. }
  518. vps->cursor.update_type = vmw_cursor_update_type(vmw, vps);
  519. switch (vps->cursor.update_type) {
  520. case VMW_CURSOR_UPDATE_LEGACY:
  521. surface = vmw_user_object_surface(&vps->uo);
  522. if (!surface || vps->cursor.legacy.id == surface->snooper.id)
  523. vps->cursor.update_type = VMW_CURSOR_UPDATE_NONE;
  524. break;
  525. case VMW_CURSOR_UPDATE_GB_ONLY:
  526. case VMW_CURSOR_UPDATE_MOB: {
  527. bo = vmw_user_object_buffer(&vps->uo);
  528. if (bo) {
  529. struct ttm_operation_ctx ctx = { false, false };
  530. ret = ttm_bo_reserve(&bo->tbo, true, false, NULL);
  531. if (ret != 0)
  532. return -ENOMEM;
  533. ret = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
  534. if (ret != 0)
  535. return -ENOMEM;
  536. /*
  537. * vmw_bo_pin_reserved also validates, so to skip
  538. * the extra validation use ttm_bo_pin directly
  539. */
  540. if (!bo->tbo.pin_count)
  541. ttm_bo_pin(&bo->tbo);
  542. if (vmw_framebuffer_to_vfb(fb)->bo) {
  543. const u32 size = new_state->crtc_w *
  544. new_state->crtc_h *
  545. sizeof(u32);
  546. (void)vmw_bo_map_and_cache_size(bo, size);
  547. } else {
  548. vmw_bo_map_and_cache(bo);
  549. }
  550. ttm_bo_unreserve(&bo->tbo);
  551. }
  552. if (!vmw_user_object_is_null(&vps->uo)) {
  553. if (!vmw_cursor_plane_changed(vps, old_vps) &&
  554. !vmw_cursor_buffer_changed(vps, old_vps)) {
  555. vps->cursor.update_type =
  556. VMW_CURSOR_UPDATE_NONE;
  557. } else {
  558. vmw_cursor_mob_get(vcp, vps);
  559. vmw_cursor_mob_map(vps);
  560. }
  561. }
  562. }
  563. break;
  564. case VMW_CURSOR_UPDATE_NONE:
  565. /* do nothing */
  566. break;
  567. }
  568. return 0;
  569. }
  570. /**
  571. * vmw_cursor_plane_atomic_check - check if the new state is okay
  572. *
  573. * @plane: cursor plane
  574. * @state: info on the new plane state
  575. *
  576. * This is a chance to fail if the new cursor state does not fit
  577. * our requirements.
  578. *
  579. * Returns 0 on success
  580. */
  581. int vmw_cursor_plane_atomic_check(struct drm_plane *plane,
  582. struct drm_atomic_state *state)
  583. {
  584. struct drm_plane_state *new_state =
  585. drm_atomic_get_new_plane_state(state, plane);
  586. struct vmw_private *vmw = vmw_priv(plane->dev);
  587. int ret = 0;
  588. struct drm_crtc_state *crtc_state = NULL;
  589. struct vmw_surface *surface = NULL;
  590. struct vmw_plane_state *vps = vmw_plane_state_to_vps(new_state);
  591. enum vmw_cursor_update_type update_type;
  592. struct drm_framebuffer *fb = new_state->fb;
  593. if (new_state->crtc)
  594. crtc_state = drm_atomic_get_new_crtc_state(new_state->state,
  595. new_state->crtc);
  596. ret = drm_atomic_helper_check_plane_state(new_state, crtc_state,
  597. DRM_PLANE_NO_SCALING,
  598. DRM_PLANE_NO_SCALING, true,
  599. true);
  600. if (ret)
  601. return ret;
  602. /* Turning off */
  603. if (!fb)
  604. return 0;
  605. update_type = vmw_cursor_update_type(vmw, vps);
  606. if (update_type == VMW_CURSOR_UPDATE_LEGACY) {
  607. if (new_state->crtc_w != VMW_CURSOR_SNOOP_WIDTH ||
  608. new_state->crtc_h != VMW_CURSOR_SNOOP_HEIGHT) {
  609. drm_warn(&vmw->drm,
  610. "Invalid cursor dimensions (%d, %d)\n",
  611. new_state->crtc_w, new_state->crtc_h);
  612. return -EINVAL;
  613. }
  614. surface = vmw_user_object_surface(&vps->uo);
  615. if (!surface || !surface->snooper.image) {
  616. drm_warn(&vmw->drm,
  617. "surface not suitable for cursor\n");
  618. return -EINVAL;
  619. }
  620. }
  621. return 0;
  622. }
  623. void
  624. vmw_cursor_plane_atomic_update(struct drm_plane *plane,
  625. struct drm_atomic_state *state)
  626. {
  627. struct vmw_bo *bo;
  628. struct drm_plane_state *new_state =
  629. drm_atomic_get_new_plane_state(state, plane);
  630. struct drm_plane_state *old_state =
  631. drm_atomic_get_old_plane_state(state, plane);
  632. struct drm_crtc *crtc = new_state->crtc ?: old_state->crtc;
  633. struct vmw_private *dev_priv = vmw_priv(plane->dev);
  634. struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
  635. struct vmw_plane_state *vps = vmw_plane_state_to_vps(new_state);
  636. s32 hotspot_x, hotspot_y, cursor_x, cursor_y;
  637. /*
  638. * Hide the cursor if the new bo is null
  639. */
  640. if (vmw_user_object_is_null(&vps->uo)) {
  641. vmw_cursor_update_position(dev_priv, false, 0, 0);
  642. return;
  643. }
  644. switch (vps->cursor.update_type) {
  645. case VMW_CURSOR_UPDATE_LEGACY:
  646. vmw_cursor_plane_update_legacy(dev_priv, vps);
  647. break;
  648. case VMW_CURSOR_UPDATE_MOB:
  649. vmw_cursor_update_mob(dev_priv, vps);
  650. break;
  651. case VMW_CURSOR_UPDATE_GB_ONLY:
  652. bo = vmw_user_object_buffer(&vps->uo);
  653. if (bo)
  654. vmw_send_define_cursor_cmd(dev_priv, bo->map.virtual,
  655. vps->base.crtc_w,
  656. vps->base.crtc_h,
  657. vps->base.hotspot_x,
  658. vps->base.hotspot_y);
  659. break;
  660. case VMW_CURSOR_UPDATE_NONE:
  661. /* do nothing */
  662. break;
  663. }
  664. /*
  665. * For all update types update the cursor position
  666. */
  667. cursor_x = new_state->crtc_x + du->set_gui_x;
  668. cursor_y = new_state->crtc_y + du->set_gui_y;
  669. hotspot_x = vps->cursor.legacy.hotspot_x + new_state->hotspot_x;
  670. hotspot_y = vps->cursor.legacy.hotspot_y + new_state->hotspot_y;
  671. vmw_cursor_update_position(dev_priv, true, cursor_x + hotspot_x,
  672. cursor_y + hotspot_y);
  673. }
  674. int vmw_kms_cursor_bypass_ioctl(struct drm_device *dev, void *data,
  675. struct drm_file *file_priv)
  676. {
  677. struct drm_vmw_cursor_bypass_arg *arg = data;
  678. struct vmw_display_unit *du;
  679. struct vmw_plane_state *vps;
  680. struct drm_crtc *crtc;
  681. int ret = 0;
  682. mutex_lock(&dev->mode_config.mutex);
  683. if (arg->flags & DRM_VMW_CURSOR_BYPASS_ALL) {
  684. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  685. du = vmw_crtc_to_du(crtc);
  686. vps = vmw_plane_state_to_vps(du->cursor.base.state);
  687. vps->cursor.legacy.hotspot_x = arg->xhot;
  688. vps->cursor.legacy.hotspot_y = arg->yhot;
  689. }
  690. mutex_unlock(&dev->mode_config.mutex);
  691. return 0;
  692. }
  693. crtc = drm_crtc_find(dev, file_priv, arg->crtc_id);
  694. if (!crtc) {
  695. ret = -ENOENT;
  696. goto out;
  697. }
  698. du = vmw_crtc_to_du(crtc);
  699. vps = vmw_plane_state_to_vps(du->cursor.base.state);
  700. vps->cursor.legacy.hotspot_x = arg->xhot;
  701. vps->cursor.legacy.hotspot_y = arg->yhot;
  702. out:
  703. mutex_unlock(&dev->mode_config.mutex);
  704. return ret;
  705. }
  706. void *vmw_cursor_snooper_create(struct drm_file *file_priv,
  707. struct vmw_surface_metadata *metadata)
  708. {
  709. if (!file_priv->atomic && metadata->scanout &&
  710. metadata->num_sizes == 1 &&
  711. metadata->sizes[0].width == VMW_CURSOR_SNOOP_WIDTH &&
  712. metadata->sizes[0].height == VMW_CURSOR_SNOOP_HEIGHT &&
  713. metadata->format == VMW_CURSOR_SNOOP_FORMAT) {
  714. const struct SVGA3dSurfaceDesc *desc =
  715. vmw_surface_get_desc(VMW_CURSOR_SNOOP_FORMAT);
  716. const u32 cursor_size_bytes = VMW_CURSOR_SNOOP_WIDTH *
  717. VMW_CURSOR_SNOOP_HEIGHT *
  718. desc->pitchBytesPerBlock;
  719. void *image = kzalloc(cursor_size_bytes, GFP_KERNEL);
  720. if (!image) {
  721. DRM_ERROR("Failed to allocate cursor_image\n");
  722. return ERR_PTR(-ENOMEM);
  723. }
  724. return image;
  725. }
  726. return NULL;
  727. }