vmwgfx_kms.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071
  1. /**************************************************************************
  2. *
  3. * Copyright © 2009-2015 VMware, Inc., Palo Alto, CA., USA
  4. * All Rights Reserved.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sub license, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice (including the
  15. * next paragraph) shall be included in all copies or substantial portions
  16. * of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  21. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  22. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  23. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  24. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. *
  26. **************************************************************************/
  27. #include "vmwgfx_kms.h"
  28. /* Might need a hrtimer here? */
  29. #define VMWGFX_PRESENT_RATE ((HZ / 60 > 0) ? HZ / 60 : 1)
  30. void vmw_du_cleanup(struct vmw_display_unit *du)
  31. {
  32. if (du->cursor_surface)
  33. vmw_surface_unreference(&du->cursor_surface);
  34. if (du->cursor_dmabuf)
  35. vmw_dmabuf_unreference(&du->cursor_dmabuf);
  36. drm_connector_unregister(&du->connector);
  37. drm_crtc_cleanup(&du->crtc);
  38. drm_encoder_cleanup(&du->encoder);
  39. drm_connector_cleanup(&du->connector);
  40. }
  41. /*
  42. * Display Unit Cursor functions
  43. */
  44. int vmw_cursor_update_image(struct vmw_private *dev_priv,
  45. u32 *image, u32 width, u32 height,
  46. u32 hotspotX, u32 hotspotY)
  47. {
  48. struct {
  49. u32 cmd;
  50. SVGAFifoCmdDefineAlphaCursor cursor;
  51. } *cmd;
  52. u32 image_size = width * height * 4;
  53. u32 cmd_size = sizeof(*cmd) + image_size;
  54. if (!image)
  55. return -EINVAL;
  56. cmd = vmw_fifo_reserve(dev_priv, cmd_size);
  57. if (unlikely(cmd == NULL)) {
  58. DRM_ERROR("Fifo reserve failed.\n");
  59. return -ENOMEM;
  60. }
  61. memset(cmd, 0, sizeof(*cmd));
  62. memcpy(&cmd[1], image, image_size);
  63. cmd->cmd = SVGA_CMD_DEFINE_ALPHA_CURSOR;
  64. cmd->cursor.id = 0;
  65. cmd->cursor.width = width;
  66. cmd->cursor.height = height;
  67. cmd->cursor.hotspotX = hotspotX;
  68. cmd->cursor.hotspotY = hotspotY;
  69. vmw_fifo_commit(dev_priv, cmd_size);
  70. return 0;
  71. }
  72. int vmw_cursor_update_dmabuf(struct vmw_private *dev_priv,
  73. struct vmw_dma_buffer *dmabuf,
  74. u32 width, u32 height,
  75. u32 hotspotX, u32 hotspotY)
  76. {
  77. struct ttm_bo_kmap_obj map;
  78. unsigned long kmap_offset;
  79. unsigned long kmap_num;
  80. void *virtual;
  81. bool dummy;
  82. int ret;
  83. kmap_offset = 0;
  84. kmap_num = (width*height*4 + PAGE_SIZE - 1) >> PAGE_SHIFT;
  85. ret = ttm_bo_reserve(&dmabuf->base, true, false, false, NULL);
  86. if (unlikely(ret != 0)) {
  87. DRM_ERROR("reserve failed\n");
  88. return -EINVAL;
  89. }
  90. ret = ttm_bo_kmap(&dmabuf->base, kmap_offset, kmap_num, &map);
  91. if (unlikely(ret != 0))
  92. goto err_unreserve;
  93. virtual = ttm_kmap_obj_virtual(&map, &dummy);
  94. ret = vmw_cursor_update_image(dev_priv, virtual, width, height,
  95. hotspotX, hotspotY);
  96. ttm_bo_kunmap(&map);
  97. err_unreserve:
  98. ttm_bo_unreserve(&dmabuf->base);
  99. return ret;
  100. }
  101. void vmw_cursor_update_position(struct vmw_private *dev_priv,
  102. bool show, int x, int y)
  103. {
  104. u32 __iomem *fifo_mem = dev_priv->mmio_virt;
  105. uint32_t count;
  106. iowrite32(show ? 1 : 0, fifo_mem + SVGA_FIFO_CURSOR_ON);
  107. iowrite32(x, fifo_mem + SVGA_FIFO_CURSOR_X);
  108. iowrite32(y, fifo_mem + SVGA_FIFO_CURSOR_Y);
  109. count = ioread32(fifo_mem + SVGA_FIFO_CURSOR_COUNT);
  110. iowrite32(++count, fifo_mem + SVGA_FIFO_CURSOR_COUNT);
  111. }
  112. int vmw_du_crtc_cursor_set(struct drm_crtc *crtc, struct drm_file *file_priv,
  113. uint32_t handle, uint32_t width, uint32_t height)
  114. {
  115. struct vmw_private *dev_priv = vmw_priv(crtc->dev);
  116. struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
  117. struct vmw_surface *surface = NULL;
  118. struct vmw_dma_buffer *dmabuf = NULL;
  119. int ret;
  120. /*
  121. * FIXME: Unclear whether there's any global state touched by the
  122. * cursor_set function, especially vmw_cursor_update_position looks
  123. * suspicious. For now take the easy route and reacquire all locks. We
  124. * can do this since the caller in the drm core doesn't check anything
  125. * which is protected by any looks.
  126. */
  127. drm_modeset_unlock_crtc(crtc);
  128. drm_modeset_lock_all(dev_priv->dev);
  129. /* A lot of the code assumes this */
  130. if (handle && (width != 64 || height != 64)) {
  131. ret = -EINVAL;
  132. goto out;
  133. }
  134. if (handle) {
  135. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  136. ret = vmw_user_lookup_handle(dev_priv, tfile,
  137. handle, &surface, &dmabuf);
  138. if (ret) {
  139. DRM_ERROR("failed to find surface or dmabuf: %i\n", ret);
  140. ret = -EINVAL;
  141. goto out;
  142. }
  143. }
  144. /* need to do this before taking down old image */
  145. if (surface && !surface->snooper.image) {
  146. DRM_ERROR("surface not suitable for cursor\n");
  147. vmw_surface_unreference(&surface);
  148. ret = -EINVAL;
  149. goto out;
  150. }
  151. /* takedown old cursor */
  152. if (du->cursor_surface) {
  153. du->cursor_surface->snooper.crtc = NULL;
  154. vmw_surface_unreference(&du->cursor_surface);
  155. }
  156. if (du->cursor_dmabuf)
  157. vmw_dmabuf_unreference(&du->cursor_dmabuf);
  158. /* setup new image */
  159. if (surface) {
  160. /* vmw_user_surface_lookup takes one reference */
  161. du->cursor_surface = surface;
  162. du->cursor_surface->snooper.crtc = crtc;
  163. du->cursor_age = du->cursor_surface->snooper.age;
  164. vmw_cursor_update_image(dev_priv, surface->snooper.image,
  165. 64, 64, du->hotspot_x, du->hotspot_y);
  166. } else if (dmabuf) {
  167. /* vmw_user_surface_lookup takes one reference */
  168. du->cursor_dmabuf = dmabuf;
  169. ret = vmw_cursor_update_dmabuf(dev_priv, dmabuf, width, height,
  170. du->hotspot_x, du->hotspot_y);
  171. } else {
  172. vmw_cursor_update_position(dev_priv, false, 0, 0);
  173. ret = 0;
  174. goto out;
  175. }
  176. vmw_cursor_update_position(dev_priv, true,
  177. du->cursor_x + du->hotspot_x,
  178. du->cursor_y + du->hotspot_y);
  179. ret = 0;
  180. out:
  181. drm_modeset_unlock_all(dev_priv->dev);
  182. drm_modeset_lock_crtc(crtc, crtc->cursor);
  183. return ret;
  184. }
  185. int vmw_du_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
  186. {
  187. struct vmw_private *dev_priv = vmw_priv(crtc->dev);
  188. struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
  189. bool shown = du->cursor_surface || du->cursor_dmabuf ? true : false;
  190. du->cursor_x = x + crtc->x;
  191. du->cursor_y = y + crtc->y;
  192. /*
  193. * FIXME: Unclear whether there's any global state touched by the
  194. * cursor_set function, especially vmw_cursor_update_position looks
  195. * suspicious. For now take the easy route and reacquire all locks. We
  196. * can do this since the caller in the drm core doesn't check anything
  197. * which is protected by any looks.
  198. */
  199. drm_modeset_unlock_crtc(crtc);
  200. drm_modeset_lock_all(dev_priv->dev);
  201. vmw_cursor_update_position(dev_priv, shown,
  202. du->cursor_x + du->hotspot_x,
  203. du->cursor_y + du->hotspot_y);
  204. drm_modeset_unlock_all(dev_priv->dev);
  205. drm_modeset_lock_crtc(crtc, crtc->cursor);
  206. return 0;
  207. }
  208. void vmw_kms_cursor_snoop(struct vmw_surface *srf,
  209. struct ttm_object_file *tfile,
  210. struct ttm_buffer_object *bo,
  211. SVGA3dCmdHeader *header)
  212. {
  213. struct ttm_bo_kmap_obj map;
  214. unsigned long kmap_offset;
  215. unsigned long kmap_num;
  216. SVGA3dCopyBox *box;
  217. unsigned box_count;
  218. void *virtual;
  219. bool dummy;
  220. struct vmw_dma_cmd {
  221. SVGA3dCmdHeader header;
  222. SVGA3dCmdSurfaceDMA dma;
  223. } *cmd;
  224. int i, ret;
  225. cmd = container_of(header, struct vmw_dma_cmd, header);
  226. /* No snooper installed */
  227. if (!srf->snooper.image)
  228. return;
  229. if (cmd->dma.host.face != 0 || cmd->dma.host.mipmap != 0) {
  230. DRM_ERROR("face and mipmap for cursors should never != 0\n");
  231. return;
  232. }
  233. if (cmd->header.size < 64) {
  234. DRM_ERROR("at least one full copy box must be given\n");
  235. return;
  236. }
  237. box = (SVGA3dCopyBox *)&cmd[1];
  238. box_count = (cmd->header.size - sizeof(SVGA3dCmdSurfaceDMA)) /
  239. sizeof(SVGA3dCopyBox);
  240. if (cmd->dma.guest.ptr.offset % PAGE_SIZE ||
  241. box->x != 0 || box->y != 0 || box->z != 0 ||
  242. box->srcx != 0 || box->srcy != 0 || box->srcz != 0 ||
  243. box->d != 1 || box_count != 1) {
  244. /* TODO handle none page aligned offsets */
  245. /* TODO handle more dst & src != 0 */
  246. /* TODO handle more then one copy */
  247. DRM_ERROR("Cant snoop dma request for cursor!\n");
  248. DRM_ERROR("(%u, %u, %u) (%u, %u, %u) (%ux%ux%u) %u %u\n",
  249. box->srcx, box->srcy, box->srcz,
  250. box->x, box->y, box->z,
  251. box->w, box->h, box->d, box_count,
  252. cmd->dma.guest.ptr.offset);
  253. return;
  254. }
  255. kmap_offset = cmd->dma.guest.ptr.offset >> PAGE_SHIFT;
  256. kmap_num = (64*64*4) >> PAGE_SHIFT;
  257. ret = ttm_bo_reserve(bo, true, false, false, NULL);
  258. if (unlikely(ret != 0)) {
  259. DRM_ERROR("reserve failed\n");
  260. return;
  261. }
  262. ret = ttm_bo_kmap(bo, kmap_offset, kmap_num, &map);
  263. if (unlikely(ret != 0))
  264. goto err_unreserve;
  265. virtual = ttm_kmap_obj_virtual(&map, &dummy);
  266. if (box->w == 64 && cmd->dma.guest.pitch == 64*4) {
  267. memcpy(srf->snooper.image, virtual, 64*64*4);
  268. } else {
  269. /* Image is unsigned pointer. */
  270. for (i = 0; i < box->h; i++)
  271. memcpy(srf->snooper.image + i * 64,
  272. virtual + i * cmd->dma.guest.pitch,
  273. box->w * 4);
  274. }
  275. srf->snooper.age++;
  276. ttm_bo_kunmap(&map);
  277. err_unreserve:
  278. ttm_bo_unreserve(bo);
  279. }
  280. void vmw_kms_cursor_post_execbuf(struct vmw_private *dev_priv)
  281. {
  282. struct drm_device *dev = dev_priv->dev;
  283. struct vmw_display_unit *du;
  284. struct drm_crtc *crtc;
  285. mutex_lock(&dev->mode_config.mutex);
  286. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  287. du = vmw_crtc_to_du(crtc);
  288. if (!du->cursor_surface ||
  289. du->cursor_age == du->cursor_surface->snooper.age)
  290. continue;
  291. du->cursor_age = du->cursor_surface->snooper.age;
  292. vmw_cursor_update_image(dev_priv,
  293. du->cursor_surface->snooper.image,
  294. 64, 64, du->hotspot_x, du->hotspot_y);
  295. }
  296. mutex_unlock(&dev->mode_config.mutex);
  297. }
  298. /*
  299. * Generic framebuffer code
  300. */
  301. /*
  302. * Surface framebuffer code
  303. */
  304. static void vmw_framebuffer_surface_destroy(struct drm_framebuffer *framebuffer)
  305. {
  306. struct vmw_framebuffer_surface *vfbs =
  307. vmw_framebuffer_to_vfbs(framebuffer);
  308. drm_framebuffer_cleanup(framebuffer);
  309. vmw_surface_unreference(&vfbs->surface);
  310. if (vfbs->base.user_obj)
  311. ttm_base_object_unref(&vfbs->base.user_obj);
  312. kfree(vfbs);
  313. }
  314. static int vmw_framebuffer_surface_dirty(struct drm_framebuffer *framebuffer,
  315. struct drm_file *file_priv,
  316. unsigned flags, unsigned color,
  317. struct drm_clip_rect *clips,
  318. unsigned num_clips)
  319. {
  320. struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
  321. struct vmw_framebuffer_surface *vfbs =
  322. vmw_framebuffer_to_vfbs(framebuffer);
  323. struct drm_clip_rect norect;
  324. int ret, inc = 1;
  325. /* Legacy Display Unit does not support 3D */
  326. if (dev_priv->active_display_unit == vmw_du_legacy)
  327. return -EINVAL;
  328. drm_modeset_lock_all(dev_priv->dev);
  329. ret = ttm_read_lock(&dev_priv->reservation_sem, true);
  330. if (unlikely(ret != 0)) {
  331. drm_modeset_unlock_all(dev_priv->dev);
  332. return ret;
  333. }
  334. if (!num_clips) {
  335. num_clips = 1;
  336. clips = &norect;
  337. norect.x1 = norect.y1 = 0;
  338. norect.x2 = framebuffer->width;
  339. norect.y2 = framebuffer->height;
  340. } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
  341. num_clips /= 2;
  342. inc = 2; /* skip source rects */
  343. }
  344. if (dev_priv->active_display_unit == vmw_du_screen_object)
  345. ret = vmw_kms_sou_do_surface_dirty(dev_priv, &vfbs->base,
  346. clips, NULL, NULL, 0, 0,
  347. num_clips, inc, NULL);
  348. else
  349. ret = vmw_kms_stdu_surface_dirty(dev_priv, &vfbs->base,
  350. clips, NULL, NULL, 0, 0,
  351. num_clips, inc, NULL);
  352. vmw_fifo_flush(dev_priv, false);
  353. ttm_read_unlock(&dev_priv->reservation_sem);
  354. drm_modeset_unlock_all(dev_priv->dev);
  355. return 0;
  356. }
  357. /**
  358. * vmw_kms_readback - Perform a readback from the screen system to
  359. * a dma-buffer backed framebuffer.
  360. *
  361. * @dev_priv: Pointer to the device private structure.
  362. * @file_priv: Pointer to a struct drm_file identifying the caller.
  363. * Must be set to NULL if @user_fence_rep is NULL.
  364. * @vfb: Pointer to the dma-buffer backed framebuffer.
  365. * @user_fence_rep: User-space provided structure for fence information.
  366. * Must be set to non-NULL if @file_priv is non-NULL.
  367. * @vclips: Array of clip rects.
  368. * @num_clips: Number of clip rects in @vclips.
  369. *
  370. * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
  371. * interrupted.
  372. */
  373. int vmw_kms_readback(struct vmw_private *dev_priv,
  374. struct drm_file *file_priv,
  375. struct vmw_framebuffer *vfb,
  376. struct drm_vmw_fence_rep __user *user_fence_rep,
  377. struct drm_vmw_rect *vclips,
  378. uint32_t num_clips)
  379. {
  380. switch (dev_priv->active_display_unit) {
  381. case vmw_du_screen_object:
  382. return vmw_kms_sou_readback(dev_priv, file_priv, vfb,
  383. user_fence_rep, vclips, num_clips);
  384. case vmw_du_screen_target:
  385. return vmw_kms_stdu_dma(dev_priv, file_priv, vfb,
  386. user_fence_rep, NULL, vclips, num_clips,
  387. 1, false, true);
  388. default:
  389. WARN_ONCE(true,
  390. "Readback called with invalid display system.\n");
  391. }
  392. return -ENOSYS;
  393. }
  394. static struct drm_framebuffer_funcs vmw_framebuffer_surface_funcs = {
  395. .destroy = vmw_framebuffer_surface_destroy,
  396. .dirty = vmw_framebuffer_surface_dirty,
  397. };
  398. static int vmw_kms_new_framebuffer_surface(struct vmw_private *dev_priv,
  399. struct vmw_surface *surface,
  400. struct vmw_framebuffer **out,
  401. const struct drm_mode_fb_cmd
  402. *mode_cmd,
  403. bool is_dmabuf_proxy)
  404. {
  405. struct drm_device *dev = dev_priv->dev;
  406. struct vmw_framebuffer_surface *vfbs;
  407. enum SVGA3dSurfaceFormat format;
  408. int ret;
  409. /* 3D is only supported on HWv8 and newer hosts */
  410. if (dev_priv->active_display_unit == vmw_du_legacy)
  411. return -ENOSYS;
  412. /*
  413. * Sanity checks.
  414. */
  415. /* Surface must be marked as a scanout. */
  416. if (unlikely(!surface->scanout))
  417. return -EINVAL;
  418. if (unlikely(surface->mip_levels[0] != 1 ||
  419. surface->num_sizes != 1 ||
  420. surface->base_size.width < mode_cmd->width ||
  421. surface->base_size.height < mode_cmd->height ||
  422. surface->base_size.depth != 1)) {
  423. DRM_ERROR("Incompatible surface dimensions "
  424. "for requested mode.\n");
  425. return -EINVAL;
  426. }
  427. switch (mode_cmd->depth) {
  428. case 32:
  429. format = SVGA3D_A8R8G8B8;
  430. break;
  431. case 24:
  432. format = SVGA3D_X8R8G8B8;
  433. break;
  434. case 16:
  435. format = SVGA3D_R5G6B5;
  436. break;
  437. case 15:
  438. format = SVGA3D_A1R5G5B5;
  439. break;
  440. default:
  441. DRM_ERROR("Invalid color depth: %d\n", mode_cmd->depth);
  442. return -EINVAL;
  443. }
  444. /*
  445. * For DX, surface format validation is done when surface->scanout
  446. * is set.
  447. */
  448. if (!dev_priv->has_dx && format != surface->format) {
  449. DRM_ERROR("Invalid surface format for requested mode.\n");
  450. return -EINVAL;
  451. }
  452. vfbs = kzalloc(sizeof(*vfbs), GFP_KERNEL);
  453. if (!vfbs) {
  454. ret = -ENOMEM;
  455. goto out_err1;
  456. }
  457. /* XXX get the first 3 from the surface info */
  458. vfbs->base.base.bits_per_pixel = mode_cmd->bpp;
  459. vfbs->base.base.pitches[0] = mode_cmd->pitch;
  460. vfbs->base.base.depth = mode_cmd->depth;
  461. vfbs->base.base.width = mode_cmd->width;
  462. vfbs->base.base.height = mode_cmd->height;
  463. vfbs->surface = vmw_surface_reference(surface);
  464. vfbs->base.user_handle = mode_cmd->handle;
  465. vfbs->is_dmabuf_proxy = is_dmabuf_proxy;
  466. *out = &vfbs->base;
  467. ret = drm_framebuffer_init(dev, &vfbs->base.base,
  468. &vmw_framebuffer_surface_funcs);
  469. if (ret)
  470. goto out_err2;
  471. return 0;
  472. out_err2:
  473. vmw_surface_unreference(&surface);
  474. kfree(vfbs);
  475. out_err1:
  476. return ret;
  477. }
  478. /*
  479. * Dmabuf framebuffer code
  480. */
  481. static void vmw_framebuffer_dmabuf_destroy(struct drm_framebuffer *framebuffer)
  482. {
  483. struct vmw_framebuffer_dmabuf *vfbd =
  484. vmw_framebuffer_to_vfbd(framebuffer);
  485. drm_framebuffer_cleanup(framebuffer);
  486. vmw_dmabuf_unreference(&vfbd->buffer);
  487. if (vfbd->base.user_obj)
  488. ttm_base_object_unref(&vfbd->base.user_obj);
  489. kfree(vfbd);
  490. }
  491. static int vmw_framebuffer_dmabuf_dirty(struct drm_framebuffer *framebuffer,
  492. struct drm_file *file_priv,
  493. unsigned flags, unsigned color,
  494. struct drm_clip_rect *clips,
  495. unsigned num_clips)
  496. {
  497. struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
  498. struct vmw_framebuffer_dmabuf *vfbd =
  499. vmw_framebuffer_to_vfbd(framebuffer);
  500. struct drm_clip_rect norect;
  501. int ret, increment = 1;
  502. drm_modeset_lock_all(dev_priv->dev);
  503. ret = ttm_read_lock(&dev_priv->reservation_sem, true);
  504. if (unlikely(ret != 0)) {
  505. drm_modeset_unlock_all(dev_priv->dev);
  506. return ret;
  507. }
  508. if (!num_clips) {
  509. num_clips = 1;
  510. clips = &norect;
  511. norect.x1 = norect.y1 = 0;
  512. norect.x2 = framebuffer->width;
  513. norect.y2 = framebuffer->height;
  514. } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
  515. num_clips /= 2;
  516. increment = 2;
  517. }
  518. switch (dev_priv->active_display_unit) {
  519. case vmw_du_screen_target:
  520. ret = vmw_kms_stdu_dma(dev_priv, NULL, &vfbd->base, NULL,
  521. clips, NULL, num_clips, increment,
  522. true, true);
  523. break;
  524. case vmw_du_screen_object:
  525. ret = vmw_kms_sou_do_dmabuf_dirty(dev_priv, &vfbd->base,
  526. clips, num_clips, increment,
  527. true,
  528. NULL);
  529. break;
  530. case vmw_du_legacy:
  531. ret = vmw_kms_ldu_do_dmabuf_dirty(dev_priv, &vfbd->base, 0, 0,
  532. clips, num_clips, increment);
  533. break;
  534. default:
  535. ret = -EINVAL;
  536. WARN_ONCE(true, "Dirty called with invalid display system.\n");
  537. break;
  538. }
  539. vmw_fifo_flush(dev_priv, false);
  540. ttm_read_unlock(&dev_priv->reservation_sem);
  541. drm_modeset_unlock_all(dev_priv->dev);
  542. return ret;
  543. }
  544. static struct drm_framebuffer_funcs vmw_framebuffer_dmabuf_funcs = {
  545. .destroy = vmw_framebuffer_dmabuf_destroy,
  546. .dirty = vmw_framebuffer_dmabuf_dirty,
  547. };
  548. /**
  549. * Pin the dmabuffer to the start of vram.
  550. */
  551. static int vmw_framebuffer_pin(struct vmw_framebuffer *vfb)
  552. {
  553. struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
  554. struct vmw_dma_buffer *buf;
  555. int ret;
  556. buf = vfb->dmabuf ? vmw_framebuffer_to_vfbd(&vfb->base)->buffer :
  557. vmw_framebuffer_to_vfbs(&vfb->base)->surface->res.backup;
  558. if (!buf)
  559. return 0;
  560. switch (dev_priv->active_display_unit) {
  561. case vmw_du_legacy:
  562. vmw_overlay_pause_all(dev_priv);
  563. ret = vmw_dmabuf_pin_in_start_of_vram(dev_priv, buf, false);
  564. vmw_overlay_resume_all(dev_priv);
  565. break;
  566. case vmw_du_screen_object:
  567. case vmw_du_screen_target:
  568. if (vfb->dmabuf)
  569. return vmw_dmabuf_pin_in_vram_or_gmr(dev_priv, buf,
  570. false);
  571. return vmw_dmabuf_pin_in_placement(dev_priv, buf,
  572. &vmw_mob_placement, false);
  573. default:
  574. return -EINVAL;
  575. }
  576. return ret;
  577. }
  578. static int vmw_framebuffer_unpin(struct vmw_framebuffer *vfb)
  579. {
  580. struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
  581. struct vmw_dma_buffer *buf;
  582. buf = vfb->dmabuf ? vmw_framebuffer_to_vfbd(&vfb->base)->buffer :
  583. vmw_framebuffer_to_vfbs(&vfb->base)->surface->res.backup;
  584. if (WARN_ON(!buf))
  585. return 0;
  586. return vmw_dmabuf_unpin(dev_priv, buf, false);
  587. }
  588. /**
  589. * vmw_create_dmabuf_proxy - create a proxy surface for the DMA buf
  590. *
  591. * @dev: DRM device
  592. * @mode_cmd: parameters for the new surface
  593. * @dmabuf_mob: MOB backing the DMA buf
  594. * @srf_out: newly created surface
  595. *
  596. * When the content FB is a DMA buf, we create a surface as a proxy to the
  597. * same buffer. This way we can do a surface copy rather than a surface DMA.
  598. * This is a more efficient approach
  599. *
  600. * RETURNS:
  601. * 0 on success, error code otherwise
  602. */
  603. static int vmw_create_dmabuf_proxy(struct drm_device *dev,
  604. const struct drm_mode_fb_cmd *mode_cmd,
  605. struct vmw_dma_buffer *dmabuf_mob,
  606. struct vmw_surface **srf_out)
  607. {
  608. uint32_t format;
  609. struct drm_vmw_size content_base_size;
  610. struct vmw_resource *res;
  611. int ret;
  612. switch (mode_cmd->depth) {
  613. case 32:
  614. case 24:
  615. format = SVGA3D_X8R8G8B8;
  616. break;
  617. case 16:
  618. case 15:
  619. format = SVGA3D_R5G6B5;
  620. break;
  621. case 8:
  622. format = SVGA3D_P8;
  623. break;
  624. default:
  625. DRM_ERROR("Invalid framebuffer format %d\n", mode_cmd->depth);
  626. return -EINVAL;
  627. }
  628. content_base_size.width = mode_cmd->width;
  629. content_base_size.height = mode_cmd->height;
  630. content_base_size.depth = 1;
  631. ret = vmw_surface_gb_priv_define(dev,
  632. 0, /* kernel visible only */
  633. 0, /* flags */
  634. format,
  635. true, /* can be a scanout buffer */
  636. 1, /* num of mip levels */
  637. 0,
  638. 0,
  639. content_base_size,
  640. srf_out);
  641. if (ret) {
  642. DRM_ERROR("Failed to allocate proxy content buffer\n");
  643. return ret;
  644. }
  645. res = &(*srf_out)->res;
  646. /* Reserve and switch the backing mob. */
  647. mutex_lock(&res->dev_priv->cmdbuf_mutex);
  648. (void) vmw_resource_reserve(res, false, true);
  649. vmw_dmabuf_unreference(&res->backup);
  650. res->backup = vmw_dmabuf_reference(dmabuf_mob);
  651. res->backup_offset = 0;
  652. vmw_resource_unreserve(res, false, NULL, 0);
  653. mutex_unlock(&res->dev_priv->cmdbuf_mutex);
  654. return 0;
  655. }
  656. static int vmw_kms_new_framebuffer_dmabuf(struct vmw_private *dev_priv,
  657. struct vmw_dma_buffer *dmabuf,
  658. struct vmw_framebuffer **out,
  659. const struct drm_mode_fb_cmd
  660. *mode_cmd)
  661. {
  662. struct drm_device *dev = dev_priv->dev;
  663. struct vmw_framebuffer_dmabuf *vfbd;
  664. unsigned int requested_size;
  665. int ret;
  666. requested_size = mode_cmd->height * mode_cmd->pitch;
  667. if (unlikely(requested_size > dmabuf->base.num_pages * PAGE_SIZE)) {
  668. DRM_ERROR("Screen buffer object size is too small "
  669. "for requested mode.\n");
  670. return -EINVAL;
  671. }
  672. /* Limited framebuffer color depth support for screen objects */
  673. if (dev_priv->active_display_unit == vmw_du_screen_object) {
  674. switch (mode_cmd->depth) {
  675. case 32:
  676. case 24:
  677. /* Only support 32 bpp for 32 and 24 depth fbs */
  678. if (mode_cmd->bpp == 32)
  679. break;
  680. DRM_ERROR("Invalid color depth/bbp: %d %d\n",
  681. mode_cmd->depth, mode_cmd->bpp);
  682. return -EINVAL;
  683. case 16:
  684. case 15:
  685. /* Only support 16 bpp for 16 and 15 depth fbs */
  686. if (mode_cmd->bpp == 16)
  687. break;
  688. DRM_ERROR("Invalid color depth/bbp: %d %d\n",
  689. mode_cmd->depth, mode_cmd->bpp);
  690. return -EINVAL;
  691. default:
  692. DRM_ERROR("Invalid color depth: %d\n", mode_cmd->depth);
  693. return -EINVAL;
  694. }
  695. }
  696. vfbd = kzalloc(sizeof(*vfbd), GFP_KERNEL);
  697. if (!vfbd) {
  698. ret = -ENOMEM;
  699. goto out_err1;
  700. }
  701. vfbd->base.base.bits_per_pixel = mode_cmd->bpp;
  702. vfbd->base.base.pitches[0] = mode_cmd->pitch;
  703. vfbd->base.base.depth = mode_cmd->depth;
  704. vfbd->base.base.width = mode_cmd->width;
  705. vfbd->base.base.height = mode_cmd->height;
  706. vfbd->base.dmabuf = true;
  707. vfbd->buffer = vmw_dmabuf_reference(dmabuf);
  708. vfbd->base.user_handle = mode_cmd->handle;
  709. *out = &vfbd->base;
  710. ret = drm_framebuffer_init(dev, &vfbd->base.base,
  711. &vmw_framebuffer_dmabuf_funcs);
  712. if (ret)
  713. goto out_err2;
  714. return 0;
  715. out_err2:
  716. vmw_dmabuf_unreference(&dmabuf);
  717. kfree(vfbd);
  718. out_err1:
  719. return ret;
  720. }
  721. /**
  722. * vmw_kms_new_framebuffer - Create a new framebuffer.
  723. *
  724. * @dev_priv: Pointer to device private struct.
  725. * @dmabuf: Pointer to dma buffer to wrap the kms framebuffer around.
  726. * Either @dmabuf or @surface must be NULL.
  727. * @surface: Pointer to a surface to wrap the kms framebuffer around.
  728. * Either @dmabuf or @surface must be NULL.
  729. * @only_2d: No presents will occur to this dma buffer based framebuffer. This
  730. * Helps the code to do some important optimizations.
  731. * @mode_cmd: Frame-buffer metadata.
  732. */
  733. struct vmw_framebuffer *
  734. vmw_kms_new_framebuffer(struct vmw_private *dev_priv,
  735. struct vmw_dma_buffer *dmabuf,
  736. struct vmw_surface *surface,
  737. bool only_2d,
  738. const struct drm_mode_fb_cmd *mode_cmd)
  739. {
  740. struct vmw_framebuffer *vfb = NULL;
  741. bool is_dmabuf_proxy = false;
  742. int ret;
  743. /*
  744. * We cannot use the SurfaceDMA command in an non-accelerated VM,
  745. * therefore, wrap the DMA buf in a surface so we can use the
  746. * SurfaceCopy command.
  747. */
  748. if (dmabuf && only_2d &&
  749. dev_priv->active_display_unit == vmw_du_screen_target) {
  750. ret = vmw_create_dmabuf_proxy(dev_priv->dev, mode_cmd,
  751. dmabuf, &surface);
  752. if (ret)
  753. return ERR_PTR(ret);
  754. is_dmabuf_proxy = true;
  755. }
  756. /* Create the new framebuffer depending one what we have */
  757. if (surface) {
  758. ret = vmw_kms_new_framebuffer_surface(dev_priv, surface, &vfb,
  759. mode_cmd,
  760. is_dmabuf_proxy);
  761. /*
  762. * vmw_create_dmabuf_proxy() adds a reference that is no longer
  763. * needed
  764. */
  765. if (is_dmabuf_proxy)
  766. vmw_surface_unreference(&surface);
  767. } else if (dmabuf) {
  768. ret = vmw_kms_new_framebuffer_dmabuf(dev_priv, dmabuf, &vfb,
  769. mode_cmd);
  770. } else {
  771. BUG();
  772. }
  773. if (ret)
  774. return ERR_PTR(ret);
  775. vfb->pin = vmw_framebuffer_pin;
  776. vfb->unpin = vmw_framebuffer_unpin;
  777. return vfb;
  778. }
  779. /*
  780. * Generic Kernel modesetting functions
  781. */
  782. static struct drm_framebuffer *vmw_kms_fb_create(struct drm_device *dev,
  783. struct drm_file *file_priv,
  784. struct drm_mode_fb_cmd2 *mode_cmd2)
  785. {
  786. struct vmw_private *dev_priv = vmw_priv(dev);
  787. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  788. struct vmw_framebuffer *vfb = NULL;
  789. struct vmw_surface *surface = NULL;
  790. struct vmw_dma_buffer *bo = NULL;
  791. struct ttm_base_object *user_obj;
  792. struct drm_mode_fb_cmd mode_cmd;
  793. int ret;
  794. mode_cmd.width = mode_cmd2->width;
  795. mode_cmd.height = mode_cmd2->height;
  796. mode_cmd.pitch = mode_cmd2->pitches[0];
  797. mode_cmd.handle = mode_cmd2->handles[0];
  798. drm_fb_get_bpp_depth(mode_cmd2->pixel_format, &mode_cmd.depth,
  799. &mode_cmd.bpp);
  800. /**
  801. * This code should be conditioned on Screen Objects not being used.
  802. * If screen objects are used, we can allocate a GMR to hold the
  803. * requested framebuffer.
  804. */
  805. if (!vmw_kms_validate_mode_vram(dev_priv,
  806. mode_cmd.pitch,
  807. mode_cmd.height)) {
  808. DRM_ERROR("Requested mode exceed bounding box limit.\n");
  809. return ERR_PTR(-ENOMEM);
  810. }
  811. /*
  812. * Take a reference on the user object of the resource
  813. * backing the kms fb. This ensures that user-space handle
  814. * lookups on that resource will always work as long as
  815. * it's registered with a kms framebuffer. This is important,
  816. * since vmw_execbuf_process identifies resources in the
  817. * command stream using user-space handles.
  818. */
  819. user_obj = ttm_base_object_lookup(tfile, mode_cmd.handle);
  820. if (unlikely(user_obj == NULL)) {
  821. DRM_ERROR("Could not locate requested kms frame buffer.\n");
  822. return ERR_PTR(-ENOENT);
  823. }
  824. /**
  825. * End conditioned code.
  826. */
  827. /* returns either a dmabuf or surface */
  828. ret = vmw_user_lookup_handle(dev_priv, tfile,
  829. mode_cmd.handle,
  830. &surface, &bo);
  831. if (ret)
  832. goto err_out;
  833. vfb = vmw_kms_new_framebuffer(dev_priv, bo, surface,
  834. !(dev_priv->capabilities & SVGA_CAP_3D),
  835. &mode_cmd);
  836. if (IS_ERR(vfb)) {
  837. ret = PTR_ERR(vfb);
  838. goto err_out;
  839. }
  840. err_out:
  841. /* vmw_user_lookup_handle takes one ref so does new_fb */
  842. if (bo)
  843. vmw_dmabuf_unreference(&bo);
  844. if (surface)
  845. vmw_surface_unreference(&surface);
  846. if (ret) {
  847. DRM_ERROR("failed to create vmw_framebuffer: %i\n", ret);
  848. ttm_base_object_unref(&user_obj);
  849. return ERR_PTR(ret);
  850. } else
  851. vfb->user_obj = user_obj;
  852. return &vfb->base;
  853. }
  854. static const struct drm_mode_config_funcs vmw_kms_funcs = {
  855. .fb_create = vmw_kms_fb_create,
  856. };
  857. static int vmw_kms_generic_present(struct vmw_private *dev_priv,
  858. struct drm_file *file_priv,
  859. struct vmw_framebuffer *vfb,
  860. struct vmw_surface *surface,
  861. uint32_t sid,
  862. int32_t destX, int32_t destY,
  863. struct drm_vmw_rect *clips,
  864. uint32_t num_clips)
  865. {
  866. return vmw_kms_sou_do_surface_dirty(dev_priv, vfb, NULL, clips,
  867. &surface->res, destX, destY,
  868. num_clips, 1, NULL);
  869. }
  870. int vmw_kms_present(struct vmw_private *dev_priv,
  871. struct drm_file *file_priv,
  872. struct vmw_framebuffer *vfb,
  873. struct vmw_surface *surface,
  874. uint32_t sid,
  875. int32_t destX, int32_t destY,
  876. struct drm_vmw_rect *clips,
  877. uint32_t num_clips)
  878. {
  879. int ret;
  880. switch (dev_priv->active_display_unit) {
  881. case vmw_du_screen_target:
  882. ret = vmw_kms_stdu_surface_dirty(dev_priv, vfb, NULL, clips,
  883. &surface->res, destX, destY,
  884. num_clips, 1, NULL);
  885. break;
  886. case vmw_du_screen_object:
  887. ret = vmw_kms_generic_present(dev_priv, file_priv, vfb, surface,
  888. sid, destX, destY, clips,
  889. num_clips);
  890. break;
  891. default:
  892. WARN_ONCE(true,
  893. "Present called with invalid display system.\n");
  894. ret = -ENOSYS;
  895. break;
  896. }
  897. if (ret)
  898. return ret;
  899. vmw_fifo_flush(dev_priv, false);
  900. return 0;
  901. }
  902. int vmw_kms_init(struct vmw_private *dev_priv)
  903. {
  904. struct drm_device *dev = dev_priv->dev;
  905. int ret;
  906. drm_mode_config_init(dev);
  907. dev->mode_config.funcs = &vmw_kms_funcs;
  908. dev->mode_config.min_width = 1;
  909. dev->mode_config.min_height = 1;
  910. dev->mode_config.max_width = dev_priv->texture_max_width;
  911. dev->mode_config.max_height = dev_priv->texture_max_height;
  912. ret = vmw_kms_stdu_init_display(dev_priv);
  913. if (ret) {
  914. ret = vmw_kms_sou_init_display(dev_priv);
  915. if (ret) /* Fallback */
  916. ret = vmw_kms_ldu_init_display(dev_priv);
  917. }
  918. return ret;
  919. }
  920. int vmw_kms_close(struct vmw_private *dev_priv)
  921. {
  922. int ret;
  923. /*
  924. * Docs says we should take the lock before calling this function
  925. * but since it destroys encoders and our destructor calls
  926. * drm_encoder_cleanup which takes the lock we deadlock.
  927. */
  928. drm_mode_config_cleanup(dev_priv->dev);
  929. if (dev_priv->active_display_unit == vmw_du_screen_object)
  930. ret = vmw_kms_sou_close_display(dev_priv);
  931. else if (dev_priv->active_display_unit == vmw_du_screen_target)
  932. ret = vmw_kms_stdu_close_display(dev_priv);
  933. else
  934. ret = vmw_kms_ldu_close_display(dev_priv);
  935. return ret;
  936. }
  937. int vmw_kms_cursor_bypass_ioctl(struct drm_device *dev, void *data,
  938. struct drm_file *file_priv)
  939. {
  940. struct drm_vmw_cursor_bypass_arg *arg = data;
  941. struct vmw_display_unit *du;
  942. struct drm_crtc *crtc;
  943. int ret = 0;
  944. mutex_lock(&dev->mode_config.mutex);
  945. if (arg->flags & DRM_VMW_CURSOR_BYPASS_ALL) {
  946. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  947. du = vmw_crtc_to_du(crtc);
  948. du->hotspot_x = arg->xhot;
  949. du->hotspot_y = arg->yhot;
  950. }
  951. mutex_unlock(&dev->mode_config.mutex);
  952. return 0;
  953. }
  954. crtc = drm_crtc_find(dev, arg->crtc_id);
  955. if (!crtc) {
  956. ret = -ENOENT;
  957. goto out;
  958. }
  959. du = vmw_crtc_to_du(crtc);
  960. du->hotspot_x = arg->xhot;
  961. du->hotspot_y = arg->yhot;
  962. out:
  963. mutex_unlock(&dev->mode_config.mutex);
  964. return ret;
  965. }
  966. int vmw_kms_write_svga(struct vmw_private *vmw_priv,
  967. unsigned width, unsigned height, unsigned pitch,
  968. unsigned bpp, unsigned depth)
  969. {
  970. if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
  971. vmw_write(vmw_priv, SVGA_REG_PITCHLOCK, pitch);
  972. else if (vmw_fifo_have_pitchlock(vmw_priv))
  973. iowrite32(pitch, vmw_priv->mmio_virt + SVGA_FIFO_PITCHLOCK);
  974. vmw_write(vmw_priv, SVGA_REG_WIDTH, width);
  975. vmw_write(vmw_priv, SVGA_REG_HEIGHT, height);
  976. vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, bpp);
  977. if (vmw_read(vmw_priv, SVGA_REG_DEPTH) != depth) {
  978. DRM_ERROR("Invalid depth %u for %u bpp, host expects %u\n",
  979. depth, bpp, vmw_read(vmw_priv, SVGA_REG_DEPTH));
  980. return -EINVAL;
  981. }
  982. return 0;
  983. }
  984. int vmw_kms_save_vga(struct vmw_private *vmw_priv)
  985. {
  986. struct vmw_vga_topology_state *save;
  987. uint32_t i;
  988. vmw_priv->vga_width = vmw_read(vmw_priv, SVGA_REG_WIDTH);
  989. vmw_priv->vga_height = vmw_read(vmw_priv, SVGA_REG_HEIGHT);
  990. vmw_priv->vga_bpp = vmw_read(vmw_priv, SVGA_REG_BITS_PER_PIXEL);
  991. if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
  992. vmw_priv->vga_pitchlock =
  993. vmw_read(vmw_priv, SVGA_REG_PITCHLOCK);
  994. else if (vmw_fifo_have_pitchlock(vmw_priv))
  995. vmw_priv->vga_pitchlock = ioread32(vmw_priv->mmio_virt +
  996. SVGA_FIFO_PITCHLOCK);
  997. if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
  998. return 0;
  999. vmw_priv->num_displays = vmw_read(vmw_priv,
  1000. SVGA_REG_NUM_GUEST_DISPLAYS);
  1001. if (vmw_priv->num_displays == 0)
  1002. vmw_priv->num_displays = 1;
  1003. for (i = 0; i < vmw_priv->num_displays; ++i) {
  1004. save = &vmw_priv->vga_save[i];
  1005. vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
  1006. save->primary = vmw_read(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY);
  1007. save->pos_x = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_X);
  1008. save->pos_y = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y);
  1009. save->width = vmw_read(vmw_priv, SVGA_REG_DISPLAY_WIDTH);
  1010. save->height = vmw_read(vmw_priv, SVGA_REG_DISPLAY_HEIGHT);
  1011. vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
  1012. if (i == 0 && vmw_priv->num_displays == 1 &&
  1013. save->width == 0 && save->height == 0) {
  1014. /*
  1015. * It should be fairly safe to assume that these
  1016. * values are uninitialized.
  1017. */
  1018. save->width = vmw_priv->vga_width - save->pos_x;
  1019. save->height = vmw_priv->vga_height - save->pos_y;
  1020. }
  1021. }
  1022. return 0;
  1023. }
  1024. int vmw_kms_restore_vga(struct vmw_private *vmw_priv)
  1025. {
  1026. struct vmw_vga_topology_state *save;
  1027. uint32_t i;
  1028. vmw_write(vmw_priv, SVGA_REG_WIDTH, vmw_priv->vga_width);
  1029. vmw_write(vmw_priv, SVGA_REG_HEIGHT, vmw_priv->vga_height);
  1030. vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, vmw_priv->vga_bpp);
  1031. if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
  1032. vmw_write(vmw_priv, SVGA_REG_PITCHLOCK,
  1033. vmw_priv->vga_pitchlock);
  1034. else if (vmw_fifo_have_pitchlock(vmw_priv))
  1035. iowrite32(vmw_priv->vga_pitchlock,
  1036. vmw_priv->mmio_virt + SVGA_FIFO_PITCHLOCK);
  1037. if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
  1038. return 0;
  1039. for (i = 0; i < vmw_priv->num_displays; ++i) {
  1040. save = &vmw_priv->vga_save[i];
  1041. vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
  1042. vmw_write(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY, save->primary);
  1043. vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_X, save->pos_x);
  1044. vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y, save->pos_y);
  1045. vmw_write(vmw_priv, SVGA_REG_DISPLAY_WIDTH, save->width);
  1046. vmw_write(vmw_priv, SVGA_REG_DISPLAY_HEIGHT, save->height);
  1047. vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
  1048. }
  1049. return 0;
  1050. }
  1051. bool vmw_kms_validate_mode_vram(struct vmw_private *dev_priv,
  1052. uint32_t pitch,
  1053. uint32_t height)
  1054. {
  1055. return ((u64) pitch * (u64) height) < (u64)
  1056. ((dev_priv->active_display_unit == vmw_du_screen_target) ?
  1057. dev_priv->prim_bb_mem : dev_priv->vram_size);
  1058. }
  1059. /**
  1060. * Function called by DRM code called with vbl_lock held.
  1061. */
  1062. u32 vmw_get_vblank_counter(struct drm_device *dev, int crtc)
  1063. {
  1064. return 0;
  1065. }
  1066. /**
  1067. * Function called by DRM code called with vbl_lock held.
  1068. */
  1069. int vmw_enable_vblank(struct drm_device *dev, int crtc)
  1070. {
  1071. return -ENOSYS;
  1072. }
  1073. /**
  1074. * Function called by DRM code called with vbl_lock held.
  1075. */
  1076. void vmw_disable_vblank(struct drm_device *dev, int crtc)
  1077. {
  1078. }
  1079. /*
  1080. * Small shared kms functions.
  1081. */
  1082. static int vmw_du_update_layout(struct vmw_private *dev_priv, unsigned num,
  1083. struct drm_vmw_rect *rects)
  1084. {
  1085. struct drm_device *dev = dev_priv->dev;
  1086. struct vmw_display_unit *du;
  1087. struct drm_connector *con;
  1088. mutex_lock(&dev->mode_config.mutex);
  1089. #if 0
  1090. {
  1091. unsigned int i;
  1092. DRM_INFO("%s: new layout ", __func__);
  1093. for (i = 0; i < num; i++)
  1094. DRM_INFO("(%i, %i %ux%u) ", rects[i].x, rects[i].y,
  1095. rects[i].w, rects[i].h);
  1096. DRM_INFO("\n");
  1097. }
  1098. #endif
  1099. list_for_each_entry(con, &dev->mode_config.connector_list, head) {
  1100. du = vmw_connector_to_du(con);
  1101. if (num > du->unit) {
  1102. du->pref_width = rects[du->unit].w;
  1103. du->pref_height = rects[du->unit].h;
  1104. du->pref_active = true;
  1105. du->gui_x = rects[du->unit].x;
  1106. du->gui_y = rects[du->unit].y;
  1107. } else {
  1108. du->pref_width = 800;
  1109. du->pref_height = 600;
  1110. du->pref_active = false;
  1111. }
  1112. con->status = vmw_du_connector_detect(con, true);
  1113. }
  1114. mutex_unlock(&dev->mode_config.mutex);
  1115. return 0;
  1116. }
  1117. void vmw_du_crtc_save(struct drm_crtc *crtc)
  1118. {
  1119. }
  1120. void vmw_du_crtc_restore(struct drm_crtc *crtc)
  1121. {
  1122. }
  1123. void vmw_du_crtc_gamma_set(struct drm_crtc *crtc,
  1124. u16 *r, u16 *g, u16 *b,
  1125. uint32_t start, uint32_t size)
  1126. {
  1127. struct vmw_private *dev_priv = vmw_priv(crtc->dev);
  1128. int i;
  1129. for (i = 0; i < size; i++) {
  1130. DRM_DEBUG("%d r/g/b = 0x%04x / 0x%04x / 0x%04x\n", i,
  1131. r[i], g[i], b[i]);
  1132. vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 0, r[i] >> 8);
  1133. vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 1, g[i] >> 8);
  1134. vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 2, b[i] >> 8);
  1135. }
  1136. }
  1137. int vmw_du_connector_dpms(struct drm_connector *connector, int mode)
  1138. {
  1139. return 0;
  1140. }
  1141. void vmw_du_connector_save(struct drm_connector *connector)
  1142. {
  1143. }
  1144. void vmw_du_connector_restore(struct drm_connector *connector)
  1145. {
  1146. }
  1147. enum drm_connector_status
  1148. vmw_du_connector_detect(struct drm_connector *connector, bool force)
  1149. {
  1150. uint32_t num_displays;
  1151. struct drm_device *dev = connector->dev;
  1152. struct vmw_private *dev_priv = vmw_priv(dev);
  1153. struct vmw_display_unit *du = vmw_connector_to_du(connector);
  1154. num_displays = vmw_read(dev_priv, SVGA_REG_NUM_DISPLAYS);
  1155. return ((vmw_connector_to_du(connector)->unit < num_displays &&
  1156. du->pref_active) ?
  1157. connector_status_connected : connector_status_disconnected);
  1158. }
  1159. static struct drm_display_mode vmw_kms_connector_builtin[] = {
  1160. /* 640x480@60Hz */
  1161. { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 25175, 640, 656,
  1162. 752, 800, 0, 480, 489, 492, 525, 0,
  1163. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
  1164. /* 800x600@60Hz */
  1165. { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 40000, 800, 840,
  1166. 968, 1056, 0, 600, 601, 605, 628, 0,
  1167. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1168. /* 1024x768@60Hz */
  1169. { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 65000, 1024, 1048,
  1170. 1184, 1344, 0, 768, 771, 777, 806, 0,
  1171. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
  1172. /* 1152x864@75Hz */
  1173. { DRM_MODE("1152x864", DRM_MODE_TYPE_DRIVER, 108000, 1152, 1216,
  1174. 1344, 1600, 0, 864, 865, 868, 900, 0,
  1175. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1176. /* 1280x768@60Hz */
  1177. { DRM_MODE("1280x768", DRM_MODE_TYPE_DRIVER, 79500, 1280, 1344,
  1178. 1472, 1664, 0, 768, 771, 778, 798, 0,
  1179. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1180. /* 1280x800@60Hz */
  1181. { DRM_MODE("1280x800", DRM_MODE_TYPE_DRIVER, 83500, 1280, 1352,
  1182. 1480, 1680, 0, 800, 803, 809, 831, 0,
  1183. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
  1184. /* 1280x960@60Hz */
  1185. { DRM_MODE("1280x960", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1376,
  1186. 1488, 1800, 0, 960, 961, 964, 1000, 0,
  1187. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1188. /* 1280x1024@60Hz */
  1189. { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1328,
  1190. 1440, 1688, 0, 1024, 1025, 1028, 1066, 0,
  1191. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1192. /* 1360x768@60Hz */
  1193. { DRM_MODE("1360x768", DRM_MODE_TYPE_DRIVER, 85500, 1360, 1424,
  1194. 1536, 1792, 0, 768, 771, 777, 795, 0,
  1195. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1196. /* 1440x1050@60Hz */
  1197. { DRM_MODE("1400x1050", DRM_MODE_TYPE_DRIVER, 121750, 1400, 1488,
  1198. 1632, 1864, 0, 1050, 1053, 1057, 1089, 0,
  1199. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1200. /* 1440x900@60Hz */
  1201. { DRM_MODE("1440x900", DRM_MODE_TYPE_DRIVER, 106500, 1440, 1520,
  1202. 1672, 1904, 0, 900, 903, 909, 934, 0,
  1203. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1204. /* 1600x1200@60Hz */
  1205. { DRM_MODE("1600x1200", DRM_MODE_TYPE_DRIVER, 162000, 1600, 1664,
  1206. 1856, 2160, 0, 1200, 1201, 1204, 1250, 0,
  1207. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1208. /* 1680x1050@60Hz */
  1209. { DRM_MODE("1680x1050", DRM_MODE_TYPE_DRIVER, 146250, 1680, 1784,
  1210. 1960, 2240, 0, 1050, 1053, 1059, 1089, 0,
  1211. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1212. /* 1792x1344@60Hz */
  1213. { DRM_MODE("1792x1344", DRM_MODE_TYPE_DRIVER, 204750, 1792, 1920,
  1214. 2120, 2448, 0, 1344, 1345, 1348, 1394, 0,
  1215. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1216. /* 1853x1392@60Hz */
  1217. { DRM_MODE("1856x1392", DRM_MODE_TYPE_DRIVER, 218250, 1856, 1952,
  1218. 2176, 2528, 0, 1392, 1393, 1396, 1439, 0,
  1219. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1220. /* 1920x1200@60Hz */
  1221. { DRM_MODE("1920x1200", DRM_MODE_TYPE_DRIVER, 193250, 1920, 2056,
  1222. 2256, 2592, 0, 1200, 1203, 1209, 1245, 0,
  1223. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1224. /* 1920x1440@60Hz */
  1225. { DRM_MODE("1920x1440", DRM_MODE_TYPE_DRIVER, 234000, 1920, 2048,
  1226. 2256, 2600, 0, 1440, 1441, 1444, 1500, 0,
  1227. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1228. /* 2560x1600@60Hz */
  1229. { DRM_MODE("2560x1600", DRM_MODE_TYPE_DRIVER, 348500, 2560, 2752,
  1230. 3032, 3504, 0, 1600, 1603, 1609, 1658, 0,
  1231. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1232. /* Terminate */
  1233. { DRM_MODE("", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) },
  1234. };
  1235. /**
  1236. * vmw_guess_mode_timing - Provide fake timings for a
  1237. * 60Hz vrefresh mode.
  1238. *
  1239. * @mode - Pointer to a struct drm_display_mode with hdisplay and vdisplay
  1240. * members filled in.
  1241. */
  1242. void vmw_guess_mode_timing(struct drm_display_mode *mode)
  1243. {
  1244. mode->hsync_start = mode->hdisplay + 50;
  1245. mode->hsync_end = mode->hsync_start + 50;
  1246. mode->htotal = mode->hsync_end + 50;
  1247. mode->vsync_start = mode->vdisplay + 50;
  1248. mode->vsync_end = mode->vsync_start + 50;
  1249. mode->vtotal = mode->vsync_end + 50;
  1250. mode->clock = (u32)mode->htotal * (u32)mode->vtotal / 100 * 6;
  1251. mode->vrefresh = drm_mode_vrefresh(mode);
  1252. }
  1253. int vmw_du_connector_fill_modes(struct drm_connector *connector,
  1254. uint32_t max_width, uint32_t max_height)
  1255. {
  1256. struct vmw_display_unit *du = vmw_connector_to_du(connector);
  1257. struct drm_device *dev = connector->dev;
  1258. struct vmw_private *dev_priv = vmw_priv(dev);
  1259. struct drm_display_mode *mode = NULL;
  1260. struct drm_display_mode *bmode;
  1261. struct drm_display_mode prefmode = { DRM_MODE("preferred",
  1262. DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED,
  1263. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1264. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC)
  1265. };
  1266. int i;
  1267. u32 assumed_bpp = 2;
  1268. /*
  1269. * If using screen objects, then assume 32-bpp because that's what the
  1270. * SVGA device is assuming
  1271. */
  1272. if (dev_priv->active_display_unit == vmw_du_screen_object)
  1273. assumed_bpp = 4;
  1274. if (dev_priv->active_display_unit == vmw_du_screen_target) {
  1275. max_width = min(max_width, dev_priv->stdu_max_width);
  1276. max_height = min(max_height, dev_priv->stdu_max_height);
  1277. }
  1278. /* Add preferred mode */
  1279. mode = drm_mode_duplicate(dev, &prefmode);
  1280. if (!mode)
  1281. return 0;
  1282. mode->hdisplay = du->pref_width;
  1283. mode->vdisplay = du->pref_height;
  1284. vmw_guess_mode_timing(mode);
  1285. if (vmw_kms_validate_mode_vram(dev_priv,
  1286. mode->hdisplay * assumed_bpp,
  1287. mode->vdisplay)) {
  1288. drm_mode_probed_add(connector, mode);
  1289. } else {
  1290. drm_mode_destroy(dev, mode);
  1291. mode = NULL;
  1292. }
  1293. if (du->pref_mode) {
  1294. list_del_init(&du->pref_mode->head);
  1295. drm_mode_destroy(dev, du->pref_mode);
  1296. }
  1297. /* mode might be null here, this is intended */
  1298. du->pref_mode = mode;
  1299. for (i = 0; vmw_kms_connector_builtin[i].type != 0; i++) {
  1300. bmode = &vmw_kms_connector_builtin[i];
  1301. if (bmode->hdisplay > max_width ||
  1302. bmode->vdisplay > max_height)
  1303. continue;
  1304. if (!vmw_kms_validate_mode_vram(dev_priv,
  1305. bmode->hdisplay * assumed_bpp,
  1306. bmode->vdisplay))
  1307. continue;
  1308. mode = drm_mode_duplicate(dev, bmode);
  1309. if (!mode)
  1310. return 0;
  1311. mode->vrefresh = drm_mode_vrefresh(mode);
  1312. drm_mode_probed_add(connector, mode);
  1313. }
  1314. drm_mode_connector_list_update(connector, true);
  1315. /* Move the prefered mode first, help apps pick the right mode. */
  1316. drm_mode_sort(&connector->modes);
  1317. return 1;
  1318. }
  1319. int vmw_du_connector_set_property(struct drm_connector *connector,
  1320. struct drm_property *property,
  1321. uint64_t val)
  1322. {
  1323. return 0;
  1324. }
  1325. int vmw_kms_update_layout_ioctl(struct drm_device *dev, void *data,
  1326. struct drm_file *file_priv)
  1327. {
  1328. struct vmw_private *dev_priv = vmw_priv(dev);
  1329. struct drm_vmw_update_layout_arg *arg =
  1330. (struct drm_vmw_update_layout_arg *)data;
  1331. void __user *user_rects;
  1332. struct drm_vmw_rect *rects;
  1333. unsigned rects_size;
  1334. int ret;
  1335. int i;
  1336. u64 total_pixels = 0;
  1337. struct drm_mode_config *mode_config = &dev->mode_config;
  1338. struct drm_vmw_rect bounding_box = {0};
  1339. if (!arg->num_outputs) {
  1340. struct drm_vmw_rect def_rect = {0, 0, 800, 600};
  1341. vmw_du_update_layout(dev_priv, 1, &def_rect);
  1342. return 0;
  1343. }
  1344. rects_size = arg->num_outputs * sizeof(struct drm_vmw_rect);
  1345. rects = kcalloc(arg->num_outputs, sizeof(struct drm_vmw_rect),
  1346. GFP_KERNEL);
  1347. if (unlikely(!rects))
  1348. return -ENOMEM;
  1349. user_rects = (void __user *)(unsigned long)arg->rects;
  1350. ret = copy_from_user(rects, user_rects, rects_size);
  1351. if (unlikely(ret != 0)) {
  1352. DRM_ERROR("Failed to get rects.\n");
  1353. ret = -EFAULT;
  1354. goto out_free;
  1355. }
  1356. for (i = 0; i < arg->num_outputs; ++i) {
  1357. if (rects[i].x < 0 ||
  1358. rects[i].y < 0 ||
  1359. rects[i].x + rects[i].w > mode_config->max_width ||
  1360. rects[i].y + rects[i].h > mode_config->max_height) {
  1361. DRM_ERROR("Invalid GUI layout.\n");
  1362. ret = -EINVAL;
  1363. goto out_free;
  1364. }
  1365. /*
  1366. * bounding_box.w and bunding_box.h are used as
  1367. * lower-right coordinates
  1368. */
  1369. if (rects[i].x + rects[i].w > bounding_box.w)
  1370. bounding_box.w = rects[i].x + rects[i].w;
  1371. if (rects[i].y + rects[i].h > bounding_box.h)
  1372. bounding_box.h = rects[i].y + rects[i].h;
  1373. total_pixels += (u64) rects[i].w * (u64) rects[i].h;
  1374. }
  1375. if (dev_priv->active_display_unit == vmw_du_screen_target) {
  1376. /*
  1377. * For Screen Targets, the limits for a toplogy are:
  1378. * 1. Bounding box (assuming 32bpp) must be < prim_bb_mem
  1379. * 2. Total pixels (assuming 32bpp) must be < prim_bb_mem
  1380. */
  1381. u64 bb_mem = bounding_box.w * bounding_box.h * 4;
  1382. u64 pixel_mem = total_pixels * 4;
  1383. if (bb_mem > dev_priv->prim_bb_mem) {
  1384. DRM_ERROR("Topology is beyond supported limits.\n");
  1385. ret = -EINVAL;
  1386. goto out_free;
  1387. }
  1388. if (pixel_mem > dev_priv->prim_bb_mem) {
  1389. DRM_ERROR("Combined output size too large\n");
  1390. ret = -EINVAL;
  1391. goto out_free;
  1392. }
  1393. }
  1394. vmw_du_update_layout(dev_priv, arg->num_outputs, rects);
  1395. out_free:
  1396. kfree(rects);
  1397. return ret;
  1398. }
  1399. /**
  1400. * vmw_kms_helper_dirty - Helper to build commands and perform actions based
  1401. * on a set of cliprects and a set of display units.
  1402. *
  1403. * @dev_priv: Pointer to a device private structure.
  1404. * @framebuffer: Pointer to the framebuffer on which to perform the actions.
  1405. * @clips: A set of struct drm_clip_rect. Either this os @vclips must be NULL.
  1406. * Cliprects are given in framebuffer coordinates.
  1407. * @vclips: A set of struct drm_vmw_rect cliprects. Either this or @clips must
  1408. * be NULL. Cliprects are given in source coordinates.
  1409. * @dest_x: X coordinate offset for the crtc / destination clip rects.
  1410. * @dest_y: Y coordinate offset for the crtc / destination clip rects.
  1411. * @num_clips: Number of cliprects in the @clips or @vclips array.
  1412. * @increment: Integer with which to increment the clip counter when looping.
  1413. * Used to skip a predetermined number of clip rects.
  1414. * @dirty: Closure structure. See the description of struct vmw_kms_dirty.
  1415. */
  1416. int vmw_kms_helper_dirty(struct vmw_private *dev_priv,
  1417. struct vmw_framebuffer *framebuffer,
  1418. const struct drm_clip_rect *clips,
  1419. const struct drm_vmw_rect *vclips,
  1420. s32 dest_x, s32 dest_y,
  1421. int num_clips,
  1422. int increment,
  1423. struct vmw_kms_dirty *dirty)
  1424. {
  1425. struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
  1426. struct drm_crtc *crtc;
  1427. u32 num_units = 0;
  1428. u32 i, k;
  1429. dirty->dev_priv = dev_priv;
  1430. list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list, head) {
  1431. if (crtc->primary->fb != &framebuffer->base)
  1432. continue;
  1433. units[num_units++] = vmw_crtc_to_du(crtc);
  1434. }
  1435. for (k = 0; k < num_units; k++) {
  1436. struct vmw_display_unit *unit = units[k];
  1437. s32 crtc_x = unit->crtc.x;
  1438. s32 crtc_y = unit->crtc.y;
  1439. s32 crtc_width = unit->crtc.mode.hdisplay;
  1440. s32 crtc_height = unit->crtc.mode.vdisplay;
  1441. const struct drm_clip_rect *clips_ptr = clips;
  1442. const struct drm_vmw_rect *vclips_ptr = vclips;
  1443. dirty->unit = unit;
  1444. if (dirty->fifo_reserve_size > 0) {
  1445. dirty->cmd = vmw_fifo_reserve(dev_priv,
  1446. dirty->fifo_reserve_size);
  1447. if (!dirty->cmd) {
  1448. DRM_ERROR("Couldn't reserve fifo space "
  1449. "for dirty blits.\n");
  1450. return -ENOMEM;
  1451. }
  1452. memset(dirty->cmd, 0, dirty->fifo_reserve_size);
  1453. }
  1454. dirty->num_hits = 0;
  1455. for (i = 0; i < num_clips; i++, clips_ptr += increment,
  1456. vclips_ptr += increment) {
  1457. s32 clip_left;
  1458. s32 clip_top;
  1459. /*
  1460. * Select clip array type. Note that integer type
  1461. * in @clips is unsigned short, whereas in @vclips
  1462. * it's 32-bit.
  1463. */
  1464. if (clips) {
  1465. dirty->fb_x = (s32) clips_ptr->x1;
  1466. dirty->fb_y = (s32) clips_ptr->y1;
  1467. dirty->unit_x2 = (s32) clips_ptr->x2 + dest_x -
  1468. crtc_x;
  1469. dirty->unit_y2 = (s32) clips_ptr->y2 + dest_y -
  1470. crtc_y;
  1471. } else {
  1472. dirty->fb_x = vclips_ptr->x;
  1473. dirty->fb_y = vclips_ptr->y;
  1474. dirty->unit_x2 = dirty->fb_x + vclips_ptr->w +
  1475. dest_x - crtc_x;
  1476. dirty->unit_y2 = dirty->fb_y + vclips_ptr->h +
  1477. dest_y - crtc_y;
  1478. }
  1479. dirty->unit_x1 = dirty->fb_x + dest_x - crtc_x;
  1480. dirty->unit_y1 = dirty->fb_y + dest_y - crtc_y;
  1481. /* Skip this clip if it's outside the crtc region */
  1482. if (dirty->unit_x1 >= crtc_width ||
  1483. dirty->unit_y1 >= crtc_height ||
  1484. dirty->unit_x2 <= 0 || dirty->unit_y2 <= 0)
  1485. continue;
  1486. /* Clip right and bottom to crtc limits */
  1487. dirty->unit_x2 = min_t(s32, dirty->unit_x2,
  1488. crtc_width);
  1489. dirty->unit_y2 = min_t(s32, dirty->unit_y2,
  1490. crtc_height);
  1491. /* Clip left and top to crtc limits */
  1492. clip_left = min_t(s32, dirty->unit_x1, 0);
  1493. clip_top = min_t(s32, dirty->unit_y1, 0);
  1494. dirty->unit_x1 -= clip_left;
  1495. dirty->unit_y1 -= clip_top;
  1496. dirty->fb_x -= clip_left;
  1497. dirty->fb_y -= clip_top;
  1498. dirty->clip(dirty);
  1499. }
  1500. dirty->fifo_commit(dirty);
  1501. }
  1502. return 0;
  1503. }
  1504. /**
  1505. * vmw_kms_helper_buffer_prepare - Reserve and validate a buffer object before
  1506. * command submission.
  1507. *
  1508. * @dev_priv. Pointer to a device private structure.
  1509. * @buf: The buffer object
  1510. * @interruptible: Whether to perform waits as interruptible.
  1511. * @validate_as_mob: Whether the buffer should be validated as a MOB. If false,
  1512. * The buffer will be validated as a GMR. Already pinned buffers will not be
  1513. * validated.
  1514. *
  1515. * Returns 0 on success, negative error code on failure, -ERESTARTSYS if
  1516. * interrupted by a signal.
  1517. */
  1518. int vmw_kms_helper_buffer_prepare(struct vmw_private *dev_priv,
  1519. struct vmw_dma_buffer *buf,
  1520. bool interruptible,
  1521. bool validate_as_mob)
  1522. {
  1523. struct ttm_buffer_object *bo = &buf->base;
  1524. int ret;
  1525. ttm_bo_reserve(bo, false, false, interruptible, NULL);
  1526. ret = vmw_validate_single_buffer(dev_priv, bo, interruptible,
  1527. validate_as_mob);
  1528. if (ret)
  1529. ttm_bo_unreserve(bo);
  1530. return ret;
  1531. }
  1532. /**
  1533. * vmw_kms_helper_buffer_revert - Undo the actions of
  1534. * vmw_kms_helper_buffer_prepare.
  1535. *
  1536. * @res: Pointer to the buffer object.
  1537. *
  1538. * Helper to be used if an error forces the caller to undo the actions of
  1539. * vmw_kms_helper_buffer_prepare.
  1540. */
  1541. void vmw_kms_helper_buffer_revert(struct vmw_dma_buffer *buf)
  1542. {
  1543. if (buf)
  1544. ttm_bo_unreserve(&buf->base);
  1545. }
  1546. /**
  1547. * vmw_kms_helper_buffer_finish - Unreserve and fence a buffer object after
  1548. * kms command submission.
  1549. *
  1550. * @dev_priv: Pointer to a device private structure.
  1551. * @file_priv: Pointer to a struct drm_file representing the caller's
  1552. * connection. Must be set to NULL if @user_fence_rep is NULL, and conversely
  1553. * if non-NULL, @user_fence_rep must be non-NULL.
  1554. * @buf: The buffer object.
  1555. * @out_fence: Optional pointer to a fence pointer. If non-NULL, a
  1556. * ref-counted fence pointer is returned here.
  1557. * @user_fence_rep: Optional pointer to a user-space provided struct
  1558. * drm_vmw_fence_rep. If provided, @file_priv must also be provided and the
  1559. * function copies fence data to user-space in a fail-safe manner.
  1560. */
  1561. void vmw_kms_helper_buffer_finish(struct vmw_private *dev_priv,
  1562. struct drm_file *file_priv,
  1563. struct vmw_dma_buffer *buf,
  1564. struct vmw_fence_obj **out_fence,
  1565. struct drm_vmw_fence_rep __user *
  1566. user_fence_rep)
  1567. {
  1568. struct vmw_fence_obj *fence;
  1569. uint32_t handle;
  1570. int ret;
  1571. ret = vmw_execbuf_fence_commands(file_priv, dev_priv, &fence,
  1572. file_priv ? &handle : NULL);
  1573. if (buf)
  1574. vmw_fence_single_bo(&buf->base, fence);
  1575. if (file_priv)
  1576. vmw_execbuf_copy_fence_user(dev_priv, vmw_fpriv(file_priv),
  1577. ret, user_fence_rep, fence,
  1578. handle);
  1579. if (out_fence)
  1580. *out_fence = fence;
  1581. else
  1582. vmw_fence_obj_unreference(&fence);
  1583. vmw_kms_helper_buffer_revert(buf);
  1584. }
  1585. /**
  1586. * vmw_kms_helper_resource_revert - Undo the actions of
  1587. * vmw_kms_helper_resource_prepare.
  1588. *
  1589. * @res: Pointer to the resource. Typically a surface.
  1590. *
  1591. * Helper to be used if an error forces the caller to undo the actions of
  1592. * vmw_kms_helper_resource_prepare.
  1593. */
  1594. void vmw_kms_helper_resource_revert(struct vmw_resource *res)
  1595. {
  1596. vmw_kms_helper_buffer_revert(res->backup);
  1597. vmw_resource_unreserve(res, false, NULL, 0);
  1598. mutex_unlock(&res->dev_priv->cmdbuf_mutex);
  1599. }
  1600. /**
  1601. * vmw_kms_helper_resource_prepare - Reserve and validate a resource before
  1602. * command submission.
  1603. *
  1604. * @res: Pointer to the resource. Typically a surface.
  1605. * @interruptible: Whether to perform waits as interruptible.
  1606. *
  1607. * Reserves and validates also the backup buffer if a guest-backed resource.
  1608. * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
  1609. * interrupted by a signal.
  1610. */
  1611. int vmw_kms_helper_resource_prepare(struct vmw_resource *res,
  1612. bool interruptible)
  1613. {
  1614. int ret = 0;
  1615. if (interruptible)
  1616. ret = mutex_lock_interruptible(&res->dev_priv->cmdbuf_mutex);
  1617. else
  1618. mutex_lock(&res->dev_priv->cmdbuf_mutex);
  1619. if (unlikely(ret != 0))
  1620. return -ERESTARTSYS;
  1621. ret = vmw_resource_reserve(res, interruptible, false);
  1622. if (ret)
  1623. goto out_unlock;
  1624. if (res->backup) {
  1625. ret = vmw_kms_helper_buffer_prepare(res->dev_priv, res->backup,
  1626. interruptible,
  1627. res->dev_priv->has_mob);
  1628. if (ret)
  1629. goto out_unreserve;
  1630. }
  1631. ret = vmw_resource_validate(res);
  1632. if (ret)
  1633. goto out_revert;
  1634. return 0;
  1635. out_revert:
  1636. vmw_kms_helper_buffer_revert(res->backup);
  1637. out_unreserve:
  1638. vmw_resource_unreserve(res, false, NULL, 0);
  1639. out_unlock:
  1640. mutex_unlock(&res->dev_priv->cmdbuf_mutex);
  1641. return ret;
  1642. }
  1643. /**
  1644. * vmw_kms_helper_resource_finish - Unreserve and fence a resource after
  1645. * kms command submission.
  1646. *
  1647. * @res: Pointer to the resource. Typically a surface.
  1648. * @out_fence: Optional pointer to a fence pointer. If non-NULL, a
  1649. * ref-counted fence pointer is returned here.
  1650. */
  1651. void vmw_kms_helper_resource_finish(struct vmw_resource *res,
  1652. struct vmw_fence_obj **out_fence)
  1653. {
  1654. if (res->backup || out_fence)
  1655. vmw_kms_helper_buffer_finish(res->dev_priv, NULL, res->backup,
  1656. out_fence, NULL);
  1657. vmw_resource_unreserve(res, false, NULL, 0);
  1658. mutex_unlock(&res->dev_priv->cmdbuf_mutex);
  1659. }
  1660. /**
  1661. * vmw_kms_update_proxy - Helper function to update a proxy surface from
  1662. * its backing MOB.
  1663. *
  1664. * @res: Pointer to the surface resource
  1665. * @clips: Clip rects in framebuffer (surface) space.
  1666. * @num_clips: Number of clips in @clips.
  1667. * @increment: Integer with which to increment the clip counter when looping.
  1668. * Used to skip a predetermined number of clip rects.
  1669. *
  1670. * This function makes sure the proxy surface is updated from its backing MOB
  1671. * using the region given by @clips. The surface resource @res and its backing
  1672. * MOB needs to be reserved and validated on call.
  1673. */
  1674. int vmw_kms_update_proxy(struct vmw_resource *res,
  1675. const struct drm_clip_rect *clips,
  1676. unsigned num_clips,
  1677. int increment)
  1678. {
  1679. struct vmw_private *dev_priv = res->dev_priv;
  1680. struct drm_vmw_size *size = &vmw_res_to_srf(res)->base_size;
  1681. struct {
  1682. SVGA3dCmdHeader header;
  1683. SVGA3dCmdUpdateGBImage body;
  1684. } *cmd;
  1685. SVGA3dBox *box;
  1686. size_t copy_size = 0;
  1687. int i;
  1688. if (!clips)
  1689. return 0;
  1690. cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd) * num_clips);
  1691. if (!cmd) {
  1692. DRM_ERROR("Couldn't reserve fifo space for proxy surface "
  1693. "update.\n");
  1694. return -ENOMEM;
  1695. }
  1696. for (i = 0; i < num_clips; ++i, clips += increment, ++cmd) {
  1697. box = &cmd->body.box;
  1698. cmd->header.id = SVGA_3D_CMD_UPDATE_GB_IMAGE;
  1699. cmd->header.size = sizeof(cmd->body);
  1700. cmd->body.image.sid = res->id;
  1701. cmd->body.image.face = 0;
  1702. cmd->body.image.mipmap = 0;
  1703. if (clips->x1 > size->width || clips->x2 > size->width ||
  1704. clips->y1 > size->height || clips->y2 > size->height) {
  1705. DRM_ERROR("Invalid clips outsize of framebuffer.\n");
  1706. return -EINVAL;
  1707. }
  1708. box->x = clips->x1;
  1709. box->y = clips->y1;
  1710. box->z = 0;
  1711. box->w = clips->x2 - clips->x1;
  1712. box->h = clips->y2 - clips->y1;
  1713. box->d = 1;
  1714. copy_size += sizeof(*cmd);
  1715. }
  1716. vmw_fifo_commit(dev_priv, copy_size);
  1717. return 0;
  1718. }
  1719. int vmw_kms_fbdev_init_data(struct vmw_private *dev_priv,
  1720. unsigned unit,
  1721. u32 max_width,
  1722. u32 max_height,
  1723. struct drm_connector **p_con,
  1724. struct drm_crtc **p_crtc,
  1725. struct drm_display_mode **p_mode)
  1726. {
  1727. struct drm_connector *con;
  1728. struct vmw_display_unit *du;
  1729. struct drm_display_mode *mode;
  1730. int i = 0;
  1731. list_for_each_entry(con, &dev_priv->dev->mode_config.connector_list,
  1732. head) {
  1733. if (i == unit)
  1734. break;
  1735. ++i;
  1736. }
  1737. if (i != unit) {
  1738. DRM_ERROR("Could not find initial display unit.\n");
  1739. return -EINVAL;
  1740. }
  1741. if (list_empty(&con->modes))
  1742. (void) vmw_du_connector_fill_modes(con, max_width, max_height);
  1743. if (list_empty(&con->modes)) {
  1744. DRM_ERROR("Could not find initial display mode.\n");
  1745. return -EINVAL;
  1746. }
  1747. du = vmw_connector_to_du(con);
  1748. *p_con = con;
  1749. *p_crtc = &du->crtc;
  1750. list_for_each_entry(mode, &con->modes, head) {
  1751. if (mode->type & DRM_MODE_TYPE_PREFERRED)
  1752. break;
  1753. }
  1754. if (mode->type & DRM_MODE_TYPE_PREFERRED)
  1755. *p_mode = mode;
  1756. else {
  1757. WARN_ONCE(true, "Could not find initial preferred mode.\n");
  1758. *p_mode = list_first_entry(&con->modes,
  1759. struct drm_display_mode,
  1760. head);
  1761. }
  1762. return 0;
  1763. }