vmwgfx_kms.c 61 KB

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