vmwgfx_kms.c 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845
  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. #include <drm/drm_plane_helper.h>
  29. #include <drm/drm_atomic.h>
  30. #include <drm/drm_atomic_helper.h>
  31. #include <drm/drm_rect.h>
  32. /* Might need a hrtimer here? */
  33. #define VMWGFX_PRESENT_RATE ((HZ / 60 > 0) ? HZ / 60 : 1)
  34. void vmw_du_cleanup(struct vmw_display_unit *du)
  35. {
  36. drm_plane_cleanup(&du->primary);
  37. drm_plane_cleanup(&du->cursor);
  38. drm_connector_unregister(&du->connector);
  39. drm_crtc_cleanup(&du->crtc);
  40. drm_encoder_cleanup(&du->encoder);
  41. drm_connector_cleanup(&du->connector);
  42. }
  43. /*
  44. * Display Unit Cursor functions
  45. */
  46. static int vmw_cursor_update_image(struct vmw_private *dev_priv,
  47. u32 *image, u32 width, u32 height,
  48. u32 hotspotX, u32 hotspotY)
  49. {
  50. struct {
  51. u32 cmd;
  52. SVGAFifoCmdDefineAlphaCursor cursor;
  53. } *cmd;
  54. u32 image_size = width * height * 4;
  55. u32 cmd_size = sizeof(*cmd) + image_size;
  56. if (!image)
  57. return -EINVAL;
  58. cmd = vmw_fifo_reserve(dev_priv, cmd_size);
  59. if (unlikely(cmd == NULL)) {
  60. DRM_ERROR("Fifo reserve failed.\n");
  61. return -ENOMEM;
  62. }
  63. memset(cmd, 0, sizeof(*cmd));
  64. memcpy(&cmd[1], image, image_size);
  65. cmd->cmd = SVGA_CMD_DEFINE_ALPHA_CURSOR;
  66. cmd->cursor.id = 0;
  67. cmd->cursor.width = width;
  68. cmd->cursor.height = height;
  69. cmd->cursor.hotspotX = hotspotX;
  70. cmd->cursor.hotspotY = hotspotY;
  71. vmw_fifo_commit_flush(dev_priv, cmd_size);
  72. return 0;
  73. }
  74. static int vmw_cursor_update_dmabuf(struct vmw_private *dev_priv,
  75. struct vmw_dma_buffer *dmabuf,
  76. u32 width, u32 height,
  77. u32 hotspotX, u32 hotspotY)
  78. {
  79. struct ttm_bo_kmap_obj map;
  80. unsigned long kmap_offset;
  81. unsigned long kmap_num;
  82. void *virtual;
  83. bool dummy;
  84. int ret;
  85. kmap_offset = 0;
  86. kmap_num = (width*height*4 + PAGE_SIZE - 1) >> PAGE_SHIFT;
  87. ret = ttm_bo_reserve(&dmabuf->base, true, false, NULL);
  88. if (unlikely(ret != 0)) {
  89. DRM_ERROR("reserve failed\n");
  90. return -EINVAL;
  91. }
  92. ret = ttm_bo_kmap(&dmabuf->base, kmap_offset, kmap_num, &map);
  93. if (unlikely(ret != 0))
  94. goto err_unreserve;
  95. virtual = ttm_kmap_obj_virtual(&map, &dummy);
  96. ret = vmw_cursor_update_image(dev_priv, virtual, width, height,
  97. hotspotX, hotspotY);
  98. ttm_bo_kunmap(&map);
  99. err_unreserve:
  100. ttm_bo_unreserve(&dmabuf->base);
  101. return ret;
  102. }
  103. static void vmw_cursor_update_position(struct vmw_private *dev_priv,
  104. bool show, int x, int y)
  105. {
  106. u32 *fifo_mem = dev_priv->mmio_virt;
  107. uint32_t count;
  108. spin_lock(&dev_priv->cursor_lock);
  109. vmw_mmio_write(show ? 1 : 0, fifo_mem + SVGA_FIFO_CURSOR_ON);
  110. vmw_mmio_write(x, fifo_mem + SVGA_FIFO_CURSOR_X);
  111. vmw_mmio_write(y, fifo_mem + SVGA_FIFO_CURSOR_Y);
  112. count = vmw_mmio_read(fifo_mem + SVGA_FIFO_CURSOR_COUNT);
  113. vmw_mmio_write(++count, fifo_mem + SVGA_FIFO_CURSOR_COUNT);
  114. spin_unlock(&dev_priv->cursor_lock);
  115. }
  116. void vmw_kms_cursor_snoop(struct vmw_surface *srf,
  117. struct ttm_object_file *tfile,
  118. struct ttm_buffer_object *bo,
  119. SVGA3dCmdHeader *header)
  120. {
  121. struct ttm_bo_kmap_obj map;
  122. unsigned long kmap_offset;
  123. unsigned long kmap_num;
  124. SVGA3dCopyBox *box;
  125. unsigned box_count;
  126. void *virtual;
  127. bool dummy;
  128. struct vmw_dma_cmd {
  129. SVGA3dCmdHeader header;
  130. SVGA3dCmdSurfaceDMA dma;
  131. } *cmd;
  132. int i, ret;
  133. cmd = container_of(header, struct vmw_dma_cmd, header);
  134. /* No snooper installed */
  135. if (!srf->snooper.image)
  136. return;
  137. if (cmd->dma.host.face != 0 || cmd->dma.host.mipmap != 0) {
  138. DRM_ERROR("face and mipmap for cursors should never != 0\n");
  139. return;
  140. }
  141. if (cmd->header.size < 64) {
  142. DRM_ERROR("at least one full copy box must be given\n");
  143. return;
  144. }
  145. box = (SVGA3dCopyBox *)&cmd[1];
  146. box_count = (cmd->header.size - sizeof(SVGA3dCmdSurfaceDMA)) /
  147. sizeof(SVGA3dCopyBox);
  148. if (cmd->dma.guest.ptr.offset % PAGE_SIZE ||
  149. box->x != 0 || box->y != 0 || box->z != 0 ||
  150. box->srcx != 0 || box->srcy != 0 || box->srcz != 0 ||
  151. box->d != 1 || box_count != 1) {
  152. /* TODO handle none page aligned offsets */
  153. /* TODO handle more dst & src != 0 */
  154. /* TODO handle more then one copy */
  155. DRM_ERROR("Cant snoop dma request for cursor!\n");
  156. DRM_ERROR("(%u, %u, %u) (%u, %u, %u) (%ux%ux%u) %u %u\n",
  157. box->srcx, box->srcy, box->srcz,
  158. box->x, box->y, box->z,
  159. box->w, box->h, box->d, box_count,
  160. cmd->dma.guest.ptr.offset);
  161. return;
  162. }
  163. kmap_offset = cmd->dma.guest.ptr.offset >> PAGE_SHIFT;
  164. kmap_num = (64*64*4) >> PAGE_SHIFT;
  165. ret = ttm_bo_reserve(bo, true, false, NULL);
  166. if (unlikely(ret != 0)) {
  167. DRM_ERROR("reserve failed\n");
  168. return;
  169. }
  170. ret = ttm_bo_kmap(bo, kmap_offset, kmap_num, &map);
  171. if (unlikely(ret != 0))
  172. goto err_unreserve;
  173. virtual = ttm_kmap_obj_virtual(&map, &dummy);
  174. if (box->w == 64 && cmd->dma.guest.pitch == 64*4) {
  175. memcpy(srf->snooper.image, virtual, 64*64*4);
  176. } else {
  177. /* Image is unsigned pointer. */
  178. for (i = 0; i < box->h; i++)
  179. memcpy(srf->snooper.image + i * 64,
  180. virtual + i * cmd->dma.guest.pitch,
  181. box->w * 4);
  182. }
  183. srf->snooper.age++;
  184. ttm_bo_kunmap(&map);
  185. err_unreserve:
  186. ttm_bo_unreserve(bo);
  187. }
  188. /**
  189. * vmw_kms_legacy_hotspot_clear - Clear legacy hotspots
  190. *
  191. * @dev_priv: Pointer to the device private struct.
  192. *
  193. * Clears all legacy hotspots.
  194. */
  195. void vmw_kms_legacy_hotspot_clear(struct vmw_private *dev_priv)
  196. {
  197. struct drm_device *dev = dev_priv->dev;
  198. struct vmw_display_unit *du;
  199. struct drm_crtc *crtc;
  200. drm_modeset_lock_all(dev);
  201. drm_for_each_crtc(crtc, dev) {
  202. du = vmw_crtc_to_du(crtc);
  203. du->hotspot_x = 0;
  204. du->hotspot_y = 0;
  205. }
  206. drm_modeset_unlock_all(dev);
  207. }
  208. void vmw_kms_cursor_post_execbuf(struct vmw_private *dev_priv)
  209. {
  210. struct drm_device *dev = dev_priv->dev;
  211. struct vmw_display_unit *du;
  212. struct drm_crtc *crtc;
  213. mutex_lock(&dev->mode_config.mutex);
  214. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  215. du = vmw_crtc_to_du(crtc);
  216. if (!du->cursor_surface ||
  217. du->cursor_age == du->cursor_surface->snooper.age)
  218. continue;
  219. du->cursor_age = du->cursor_surface->snooper.age;
  220. vmw_cursor_update_image(dev_priv,
  221. du->cursor_surface->snooper.image,
  222. 64, 64,
  223. du->hotspot_x + du->core_hotspot_x,
  224. du->hotspot_y + du->core_hotspot_y);
  225. }
  226. mutex_unlock(&dev->mode_config.mutex);
  227. }
  228. void vmw_du_cursor_plane_destroy(struct drm_plane *plane)
  229. {
  230. vmw_cursor_update_position(plane->dev->dev_private, false, 0, 0);
  231. drm_plane_cleanup(plane);
  232. }
  233. void vmw_du_primary_plane_destroy(struct drm_plane *plane)
  234. {
  235. drm_plane_cleanup(plane);
  236. /* Planes are static in our case so we don't free it */
  237. }
  238. /**
  239. * vmw_du_vps_unpin_surf - unpins resource associated with a framebuffer surface
  240. *
  241. * @vps: plane state associated with the display surface
  242. * @unreference: true if we also want to unreference the display.
  243. */
  244. void vmw_du_plane_unpin_surf(struct vmw_plane_state *vps,
  245. bool unreference)
  246. {
  247. if (vps->surf) {
  248. if (vps->pinned) {
  249. vmw_resource_unpin(&vps->surf->res);
  250. vps->pinned--;
  251. }
  252. if (unreference) {
  253. if (vps->pinned)
  254. DRM_ERROR("Surface still pinned\n");
  255. vmw_surface_unreference(&vps->surf);
  256. }
  257. }
  258. }
  259. /**
  260. * vmw_du_plane_cleanup_fb - Unpins the cursor
  261. *
  262. * @plane: display plane
  263. * @old_state: Contains the FB to clean up
  264. *
  265. * Unpins the framebuffer surface
  266. *
  267. * Returns 0 on success
  268. */
  269. void
  270. vmw_du_plane_cleanup_fb(struct drm_plane *plane,
  271. struct drm_plane_state *old_state)
  272. {
  273. struct vmw_plane_state *vps = vmw_plane_state_to_vps(old_state);
  274. vmw_du_plane_unpin_surf(vps, false);
  275. }
  276. /**
  277. * vmw_du_cursor_plane_prepare_fb - Readies the cursor by referencing it
  278. *
  279. * @plane: display plane
  280. * @new_state: info on the new plane state, including the FB
  281. *
  282. * Returns 0 on success
  283. */
  284. int
  285. vmw_du_cursor_plane_prepare_fb(struct drm_plane *plane,
  286. struct drm_plane_state *new_state)
  287. {
  288. struct drm_framebuffer *fb = new_state->fb;
  289. struct vmw_plane_state *vps = vmw_plane_state_to_vps(new_state);
  290. if (vps->surf)
  291. vmw_surface_unreference(&vps->surf);
  292. if (vps->dmabuf)
  293. vmw_dmabuf_unreference(&vps->dmabuf);
  294. if (fb) {
  295. if (vmw_framebuffer_to_vfb(fb)->dmabuf) {
  296. vps->dmabuf = vmw_framebuffer_to_vfbd(fb)->buffer;
  297. vmw_dmabuf_reference(vps->dmabuf);
  298. } else {
  299. vps->surf = vmw_framebuffer_to_vfbs(fb)->surface;
  300. vmw_surface_reference(vps->surf);
  301. }
  302. }
  303. return 0;
  304. }
  305. void
  306. vmw_du_cursor_plane_atomic_update(struct drm_plane *plane,
  307. struct drm_plane_state *old_state)
  308. {
  309. struct drm_crtc *crtc = plane->state->crtc ?: old_state->crtc;
  310. struct vmw_private *dev_priv = vmw_priv(crtc->dev);
  311. struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
  312. struct vmw_plane_state *vps = vmw_plane_state_to_vps(plane->state);
  313. s32 hotspot_x, hotspot_y;
  314. int ret = 0;
  315. hotspot_x = du->hotspot_x;
  316. hotspot_y = du->hotspot_y;
  317. du->cursor_surface = vps->surf;
  318. du->cursor_dmabuf = vps->dmabuf;
  319. /* setup new image */
  320. if (vps->surf) {
  321. du->cursor_age = du->cursor_surface->snooper.age;
  322. ret = vmw_cursor_update_image(dev_priv,
  323. vps->surf->snooper.image,
  324. 64, 64, hotspot_x, hotspot_y);
  325. } else if (vps->dmabuf) {
  326. ret = vmw_cursor_update_dmabuf(dev_priv, vps->dmabuf,
  327. plane->state->crtc_w,
  328. plane->state->crtc_h,
  329. hotspot_x, hotspot_y);
  330. } else {
  331. vmw_cursor_update_position(dev_priv, false, 0, 0);
  332. return;
  333. }
  334. if (!ret) {
  335. du->cursor_x = plane->state->crtc_x + du->set_gui_x;
  336. du->cursor_y = plane->state->crtc_y + du->set_gui_y;
  337. vmw_cursor_update_position(dev_priv, true,
  338. du->cursor_x + hotspot_x,
  339. du->cursor_y + hotspot_y);
  340. } else {
  341. DRM_ERROR("Failed to update cursor image\n");
  342. }
  343. }
  344. /**
  345. * vmw_du_primary_plane_atomic_check - check if the new state is okay
  346. *
  347. * @plane: display plane
  348. * @state: info on the new plane state, including the FB
  349. *
  350. * Check if the new state is settable given the current state. Other
  351. * than what the atomic helper checks, we care about crtc fitting
  352. * the FB and maintaining one active framebuffer.
  353. *
  354. * Returns 0 on success
  355. */
  356. int vmw_du_primary_plane_atomic_check(struct drm_plane *plane,
  357. struct drm_plane_state *state)
  358. {
  359. struct drm_framebuffer *new_fb = state->fb;
  360. bool visible;
  361. struct drm_rect src = {
  362. .x1 = state->src_x,
  363. .y1 = state->src_y,
  364. .x2 = state->src_x + state->src_w,
  365. .y2 = state->src_y + state->src_h,
  366. };
  367. struct drm_rect dest = {
  368. .x1 = state->crtc_x,
  369. .y1 = state->crtc_y,
  370. .x2 = state->crtc_x + state->crtc_w,
  371. .y2 = state->crtc_y + state->crtc_h,
  372. };
  373. struct drm_rect clip = dest;
  374. int ret;
  375. ret = drm_plane_helper_check_update(plane, state->crtc, new_fb,
  376. &src, &dest, &clip,
  377. DRM_ROTATE_0,
  378. DRM_PLANE_HELPER_NO_SCALING,
  379. DRM_PLANE_HELPER_NO_SCALING,
  380. false, true, &visible);
  381. if (!ret && new_fb) {
  382. struct drm_crtc *crtc = state->crtc;
  383. struct vmw_connector_state *vcs;
  384. struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
  385. struct vmw_private *dev_priv = vmw_priv(crtc->dev);
  386. struct vmw_framebuffer *vfb = vmw_framebuffer_to_vfb(new_fb);
  387. vcs = vmw_connector_state_to_vcs(du->connector.state);
  388. if ((dest.x2 > new_fb->width ||
  389. dest.y2 > new_fb->height)) {
  390. DRM_ERROR("CRTC area outside of framebuffer\n");
  391. return -EINVAL;
  392. }
  393. /* Only one active implicit framebuffer at a time. */
  394. mutex_lock(&dev_priv->global_kms_state_mutex);
  395. if (vcs->is_implicit && dev_priv->implicit_fb &&
  396. !(dev_priv->num_implicit == 1 && du->active_implicit)
  397. && dev_priv->implicit_fb != vfb) {
  398. DRM_ERROR("Multiple implicit framebuffers "
  399. "not supported.\n");
  400. ret = -EINVAL;
  401. }
  402. mutex_unlock(&dev_priv->global_kms_state_mutex);
  403. }
  404. return ret;
  405. }
  406. /**
  407. * vmw_du_cursor_plane_atomic_check - check if the new state is okay
  408. *
  409. * @plane: cursor plane
  410. * @state: info on the new plane state
  411. *
  412. * This is a chance to fail if the new cursor state does not fit
  413. * our requirements.
  414. *
  415. * Returns 0 on success
  416. */
  417. int vmw_du_cursor_plane_atomic_check(struct drm_plane *plane,
  418. struct drm_plane_state *new_state)
  419. {
  420. int ret = 0;
  421. struct vmw_surface *surface = NULL;
  422. struct drm_framebuffer *fb = new_state->fb;
  423. /* Turning off */
  424. if (!fb)
  425. return ret;
  426. /* A lot of the code assumes this */
  427. if (new_state->crtc_w != 64 || new_state->crtc_h != 64) {
  428. DRM_ERROR("Invalid cursor dimensions (%d, %d)\n",
  429. new_state->crtc_w, new_state->crtc_h);
  430. ret = -EINVAL;
  431. }
  432. if (!vmw_framebuffer_to_vfb(fb)->dmabuf)
  433. surface = vmw_framebuffer_to_vfbs(fb)->surface;
  434. if (surface && !surface->snooper.image) {
  435. DRM_ERROR("surface not suitable for cursor\n");
  436. ret = -EINVAL;
  437. }
  438. return ret;
  439. }
  440. int vmw_du_crtc_atomic_check(struct drm_crtc *crtc,
  441. struct drm_crtc_state *new_state)
  442. {
  443. struct vmw_display_unit *du = vmw_crtc_to_du(new_state->crtc);
  444. int connector_mask = 1 << drm_connector_index(&du->connector);
  445. bool has_primary = new_state->plane_mask &
  446. BIT(drm_plane_index(crtc->primary));
  447. /* We always want to have an active plane with an active CRTC */
  448. if (has_primary != new_state->enable)
  449. return -EINVAL;
  450. if (new_state->connector_mask != connector_mask &&
  451. new_state->connector_mask != 0) {
  452. DRM_ERROR("Invalid connectors configuration\n");
  453. return -EINVAL;
  454. }
  455. /*
  456. * Our virtual device does not have a dot clock, so use the logical
  457. * clock value as the dot clock.
  458. */
  459. if (new_state->mode.crtc_clock == 0)
  460. new_state->adjusted_mode.crtc_clock = new_state->mode.clock;
  461. return 0;
  462. }
  463. void vmw_du_crtc_atomic_begin(struct drm_crtc *crtc,
  464. struct drm_crtc_state *old_crtc_state)
  465. {
  466. }
  467. void vmw_du_crtc_atomic_flush(struct drm_crtc *crtc,
  468. struct drm_crtc_state *old_crtc_state)
  469. {
  470. struct drm_pending_vblank_event *event = crtc->state->event;
  471. if (event) {
  472. crtc->state->event = NULL;
  473. spin_lock_irq(&crtc->dev->event_lock);
  474. if (drm_crtc_vblank_get(crtc) == 0)
  475. drm_crtc_arm_vblank_event(crtc, event);
  476. else
  477. drm_crtc_send_vblank_event(crtc, event);
  478. spin_unlock_irq(&crtc->dev->event_lock);
  479. }
  480. }
  481. /**
  482. * vmw_du_crtc_duplicate_state - duplicate crtc state
  483. * @crtc: DRM crtc
  484. *
  485. * Allocates and returns a copy of the crtc state (both common and
  486. * vmw-specific) for the specified crtc.
  487. *
  488. * Returns: The newly allocated crtc state, or NULL on failure.
  489. */
  490. struct drm_crtc_state *
  491. vmw_du_crtc_duplicate_state(struct drm_crtc *crtc)
  492. {
  493. struct drm_crtc_state *state;
  494. struct vmw_crtc_state *vcs;
  495. if (WARN_ON(!crtc->state))
  496. return NULL;
  497. vcs = kmemdup(crtc->state, sizeof(*vcs), GFP_KERNEL);
  498. if (!vcs)
  499. return NULL;
  500. state = &vcs->base;
  501. __drm_atomic_helper_crtc_duplicate_state(crtc, state);
  502. return state;
  503. }
  504. /**
  505. * vmw_du_crtc_reset - creates a blank vmw crtc state
  506. * @crtc: DRM crtc
  507. *
  508. * Resets the atomic state for @crtc by freeing the state pointer (which
  509. * might be NULL, e.g. at driver load time) and allocating a new empty state
  510. * object.
  511. */
  512. void vmw_du_crtc_reset(struct drm_crtc *crtc)
  513. {
  514. struct vmw_crtc_state *vcs;
  515. if (crtc->state) {
  516. __drm_atomic_helper_crtc_destroy_state(crtc->state);
  517. kfree(vmw_crtc_state_to_vcs(crtc->state));
  518. }
  519. vcs = kzalloc(sizeof(*vcs), GFP_KERNEL);
  520. if (!vcs) {
  521. DRM_ERROR("Cannot allocate vmw_crtc_state\n");
  522. return;
  523. }
  524. crtc->state = &vcs->base;
  525. crtc->state->crtc = crtc;
  526. }
  527. /**
  528. * vmw_du_crtc_destroy_state - destroy crtc state
  529. * @crtc: DRM crtc
  530. * @state: state object to destroy
  531. *
  532. * Destroys the crtc state (both common and vmw-specific) for the
  533. * specified plane.
  534. */
  535. void
  536. vmw_du_crtc_destroy_state(struct drm_crtc *crtc,
  537. struct drm_crtc_state *state)
  538. {
  539. drm_atomic_helper_crtc_destroy_state(crtc, state);
  540. }
  541. /**
  542. * vmw_du_plane_duplicate_state - duplicate plane state
  543. * @plane: drm plane
  544. *
  545. * Allocates and returns a copy of the plane state (both common and
  546. * vmw-specific) for the specified plane.
  547. *
  548. * Returns: The newly allocated plane state, or NULL on failure.
  549. */
  550. struct drm_plane_state *
  551. vmw_du_plane_duplicate_state(struct drm_plane *plane)
  552. {
  553. struct drm_plane_state *state;
  554. struct vmw_plane_state *vps;
  555. vps = kmemdup(plane->state, sizeof(*vps), GFP_KERNEL);
  556. if (!vps)
  557. return NULL;
  558. vps->pinned = 0;
  559. /* Mapping is managed by prepare_fb/cleanup_fb */
  560. memset(&vps->guest_map, 0, sizeof(vps->guest_map));
  561. memset(&vps->host_map, 0, sizeof(vps->host_map));
  562. vps->cpp = 0;
  563. /* Each ref counted resource needs to be acquired again */
  564. if (vps->surf)
  565. (void) vmw_surface_reference(vps->surf);
  566. if (vps->dmabuf)
  567. (void) vmw_dmabuf_reference(vps->dmabuf);
  568. state = &vps->base;
  569. __drm_atomic_helper_plane_duplicate_state(plane, state);
  570. return state;
  571. }
  572. /**
  573. * vmw_du_plane_reset - creates a blank vmw plane state
  574. * @plane: drm plane
  575. *
  576. * Resets the atomic state for @plane by freeing the state pointer (which might
  577. * be NULL, e.g. at driver load time) and allocating a new empty state object.
  578. */
  579. void vmw_du_plane_reset(struct drm_plane *plane)
  580. {
  581. struct vmw_plane_state *vps;
  582. if (plane->state)
  583. vmw_du_plane_destroy_state(plane, plane->state);
  584. vps = kzalloc(sizeof(*vps), GFP_KERNEL);
  585. if (!vps) {
  586. DRM_ERROR("Cannot allocate vmw_plane_state\n");
  587. return;
  588. }
  589. plane->state = &vps->base;
  590. plane->state->plane = plane;
  591. plane->state->rotation = DRM_ROTATE_0;
  592. }
  593. /**
  594. * vmw_du_plane_destroy_state - destroy plane state
  595. * @plane: DRM plane
  596. * @state: state object to destroy
  597. *
  598. * Destroys the plane state (both common and vmw-specific) for the
  599. * specified plane.
  600. */
  601. void
  602. vmw_du_plane_destroy_state(struct drm_plane *plane,
  603. struct drm_plane_state *state)
  604. {
  605. struct vmw_plane_state *vps = vmw_plane_state_to_vps(state);
  606. /* Should have been freed by cleanup_fb */
  607. if (vps->guest_map.virtual) {
  608. DRM_ERROR("Guest mapping not freed\n");
  609. ttm_bo_kunmap(&vps->guest_map);
  610. }
  611. if (vps->host_map.virtual) {
  612. DRM_ERROR("Host mapping not freed\n");
  613. ttm_bo_kunmap(&vps->host_map);
  614. }
  615. if (vps->surf)
  616. vmw_surface_unreference(&vps->surf);
  617. if (vps->dmabuf)
  618. vmw_dmabuf_unreference(&vps->dmabuf);
  619. drm_atomic_helper_plane_destroy_state(plane, state);
  620. }
  621. /**
  622. * vmw_du_connector_duplicate_state - duplicate connector state
  623. * @connector: DRM connector
  624. *
  625. * Allocates and returns a copy of the connector state (both common and
  626. * vmw-specific) for the specified connector.
  627. *
  628. * Returns: The newly allocated connector state, or NULL on failure.
  629. */
  630. struct drm_connector_state *
  631. vmw_du_connector_duplicate_state(struct drm_connector *connector)
  632. {
  633. struct drm_connector_state *state;
  634. struct vmw_connector_state *vcs;
  635. if (WARN_ON(!connector->state))
  636. return NULL;
  637. vcs = kmemdup(connector->state, sizeof(*vcs), GFP_KERNEL);
  638. if (!vcs)
  639. return NULL;
  640. state = &vcs->base;
  641. __drm_atomic_helper_connector_duplicate_state(connector, state);
  642. return state;
  643. }
  644. /**
  645. * vmw_du_connector_reset - creates a blank vmw connector state
  646. * @connector: DRM connector
  647. *
  648. * Resets the atomic state for @connector by freeing the state pointer (which
  649. * might be NULL, e.g. at driver load time) and allocating a new empty state
  650. * object.
  651. */
  652. void vmw_du_connector_reset(struct drm_connector *connector)
  653. {
  654. struct vmw_connector_state *vcs;
  655. if (connector->state) {
  656. __drm_atomic_helper_connector_destroy_state(connector->state);
  657. kfree(vmw_connector_state_to_vcs(connector->state));
  658. }
  659. vcs = kzalloc(sizeof(*vcs), GFP_KERNEL);
  660. if (!vcs) {
  661. DRM_ERROR("Cannot allocate vmw_connector_state\n");
  662. return;
  663. }
  664. __drm_atomic_helper_connector_reset(connector, &vcs->base);
  665. }
  666. /**
  667. * vmw_du_connector_destroy_state - destroy connector state
  668. * @connector: DRM connector
  669. * @state: state object to destroy
  670. *
  671. * Destroys the connector state (both common and vmw-specific) for the
  672. * specified plane.
  673. */
  674. void
  675. vmw_du_connector_destroy_state(struct drm_connector *connector,
  676. struct drm_connector_state *state)
  677. {
  678. drm_atomic_helper_connector_destroy_state(connector, state);
  679. }
  680. /*
  681. * Generic framebuffer code
  682. */
  683. /*
  684. * Surface framebuffer code
  685. */
  686. static void vmw_framebuffer_surface_destroy(struct drm_framebuffer *framebuffer)
  687. {
  688. struct vmw_framebuffer_surface *vfbs =
  689. vmw_framebuffer_to_vfbs(framebuffer);
  690. drm_framebuffer_cleanup(framebuffer);
  691. vmw_surface_unreference(&vfbs->surface);
  692. if (vfbs->base.user_obj)
  693. ttm_base_object_unref(&vfbs->base.user_obj);
  694. kfree(vfbs);
  695. }
  696. static int vmw_framebuffer_surface_dirty(struct drm_framebuffer *framebuffer,
  697. struct drm_file *file_priv,
  698. unsigned flags, unsigned color,
  699. struct drm_clip_rect *clips,
  700. unsigned num_clips)
  701. {
  702. struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
  703. struct vmw_framebuffer_surface *vfbs =
  704. vmw_framebuffer_to_vfbs(framebuffer);
  705. struct drm_clip_rect norect;
  706. int ret, inc = 1;
  707. /* Legacy Display Unit does not support 3D */
  708. if (dev_priv->active_display_unit == vmw_du_legacy)
  709. return -EINVAL;
  710. drm_modeset_lock_all(dev_priv->dev);
  711. ret = ttm_read_lock(&dev_priv->reservation_sem, true);
  712. if (unlikely(ret != 0)) {
  713. drm_modeset_unlock_all(dev_priv->dev);
  714. return ret;
  715. }
  716. if (!num_clips) {
  717. num_clips = 1;
  718. clips = &norect;
  719. norect.x1 = norect.y1 = 0;
  720. norect.x2 = framebuffer->width;
  721. norect.y2 = framebuffer->height;
  722. } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
  723. num_clips /= 2;
  724. inc = 2; /* skip source rects */
  725. }
  726. if (dev_priv->active_display_unit == vmw_du_screen_object)
  727. ret = vmw_kms_sou_do_surface_dirty(dev_priv, &vfbs->base,
  728. clips, NULL, NULL, 0, 0,
  729. num_clips, inc, NULL);
  730. else
  731. ret = vmw_kms_stdu_surface_dirty(dev_priv, &vfbs->base,
  732. clips, NULL, NULL, 0, 0,
  733. num_clips, inc, NULL);
  734. vmw_fifo_flush(dev_priv, false);
  735. ttm_read_unlock(&dev_priv->reservation_sem);
  736. drm_modeset_unlock_all(dev_priv->dev);
  737. return 0;
  738. }
  739. /**
  740. * vmw_kms_readback - Perform a readback from the screen system to
  741. * a dma-buffer backed framebuffer.
  742. *
  743. * @dev_priv: Pointer to the device private structure.
  744. * @file_priv: Pointer to a struct drm_file identifying the caller.
  745. * Must be set to NULL if @user_fence_rep is NULL.
  746. * @vfb: Pointer to the dma-buffer backed framebuffer.
  747. * @user_fence_rep: User-space provided structure for fence information.
  748. * Must be set to non-NULL if @file_priv is non-NULL.
  749. * @vclips: Array of clip rects.
  750. * @num_clips: Number of clip rects in @vclips.
  751. *
  752. * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
  753. * interrupted.
  754. */
  755. int vmw_kms_readback(struct vmw_private *dev_priv,
  756. struct drm_file *file_priv,
  757. struct vmw_framebuffer *vfb,
  758. struct drm_vmw_fence_rep __user *user_fence_rep,
  759. struct drm_vmw_rect *vclips,
  760. uint32_t num_clips)
  761. {
  762. switch (dev_priv->active_display_unit) {
  763. case vmw_du_screen_object:
  764. return vmw_kms_sou_readback(dev_priv, file_priv, vfb,
  765. user_fence_rep, vclips, num_clips);
  766. case vmw_du_screen_target:
  767. return vmw_kms_stdu_dma(dev_priv, file_priv, vfb,
  768. user_fence_rep, NULL, vclips, num_clips,
  769. 1, false, true);
  770. default:
  771. WARN_ONCE(true,
  772. "Readback called with invalid display system.\n");
  773. }
  774. return -ENOSYS;
  775. }
  776. static const struct drm_framebuffer_funcs vmw_framebuffer_surface_funcs = {
  777. .destroy = vmw_framebuffer_surface_destroy,
  778. .dirty = vmw_framebuffer_surface_dirty,
  779. };
  780. static int vmw_kms_new_framebuffer_surface(struct vmw_private *dev_priv,
  781. struct vmw_surface *surface,
  782. struct vmw_framebuffer **out,
  783. const struct drm_mode_fb_cmd2
  784. *mode_cmd,
  785. bool is_dmabuf_proxy)
  786. {
  787. struct drm_device *dev = dev_priv->dev;
  788. struct vmw_framebuffer_surface *vfbs;
  789. enum SVGA3dSurfaceFormat format;
  790. int ret;
  791. struct drm_format_name_buf format_name;
  792. /* 3D is only supported on HWv8 and newer hosts */
  793. if (dev_priv->active_display_unit == vmw_du_legacy)
  794. return -ENOSYS;
  795. /*
  796. * Sanity checks.
  797. */
  798. /* Surface must be marked as a scanout. */
  799. if (unlikely(!surface->scanout))
  800. return -EINVAL;
  801. if (unlikely(surface->mip_levels[0] != 1 ||
  802. surface->num_sizes != 1 ||
  803. surface->base_size.width < mode_cmd->width ||
  804. surface->base_size.height < mode_cmd->height ||
  805. surface->base_size.depth != 1)) {
  806. DRM_ERROR("Incompatible surface dimensions "
  807. "for requested mode.\n");
  808. return -EINVAL;
  809. }
  810. switch (mode_cmd->pixel_format) {
  811. case DRM_FORMAT_ARGB8888:
  812. format = SVGA3D_A8R8G8B8;
  813. break;
  814. case DRM_FORMAT_XRGB8888:
  815. format = SVGA3D_X8R8G8B8;
  816. break;
  817. case DRM_FORMAT_RGB565:
  818. format = SVGA3D_R5G6B5;
  819. break;
  820. case DRM_FORMAT_XRGB1555:
  821. format = SVGA3D_A1R5G5B5;
  822. break;
  823. default:
  824. DRM_ERROR("Invalid pixel format: %s\n",
  825. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  826. return -EINVAL;
  827. }
  828. /*
  829. * For DX, surface format validation is done when surface->scanout
  830. * is set.
  831. */
  832. if (!dev_priv->has_dx && format != surface->format) {
  833. DRM_ERROR("Invalid surface format for requested mode.\n");
  834. return -EINVAL;
  835. }
  836. vfbs = kzalloc(sizeof(*vfbs), GFP_KERNEL);
  837. if (!vfbs) {
  838. ret = -ENOMEM;
  839. goto out_err1;
  840. }
  841. drm_helper_mode_fill_fb_struct(dev, &vfbs->base.base, mode_cmd);
  842. vfbs->surface = vmw_surface_reference(surface);
  843. vfbs->base.user_handle = mode_cmd->handles[0];
  844. vfbs->is_dmabuf_proxy = is_dmabuf_proxy;
  845. *out = &vfbs->base;
  846. ret = drm_framebuffer_init(dev, &vfbs->base.base,
  847. &vmw_framebuffer_surface_funcs);
  848. if (ret)
  849. goto out_err2;
  850. return 0;
  851. out_err2:
  852. vmw_surface_unreference(&surface);
  853. kfree(vfbs);
  854. out_err1:
  855. return ret;
  856. }
  857. /*
  858. * Dmabuf framebuffer code
  859. */
  860. static void vmw_framebuffer_dmabuf_destroy(struct drm_framebuffer *framebuffer)
  861. {
  862. struct vmw_framebuffer_dmabuf *vfbd =
  863. vmw_framebuffer_to_vfbd(framebuffer);
  864. drm_framebuffer_cleanup(framebuffer);
  865. vmw_dmabuf_unreference(&vfbd->buffer);
  866. if (vfbd->base.user_obj)
  867. ttm_base_object_unref(&vfbd->base.user_obj);
  868. kfree(vfbd);
  869. }
  870. static int vmw_framebuffer_dmabuf_dirty(struct drm_framebuffer *framebuffer,
  871. struct drm_file *file_priv,
  872. unsigned flags, unsigned color,
  873. struct drm_clip_rect *clips,
  874. unsigned num_clips)
  875. {
  876. struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
  877. struct vmw_framebuffer_dmabuf *vfbd =
  878. vmw_framebuffer_to_vfbd(framebuffer);
  879. struct drm_clip_rect norect;
  880. int ret, increment = 1;
  881. drm_modeset_lock_all(dev_priv->dev);
  882. ret = ttm_read_lock(&dev_priv->reservation_sem, true);
  883. if (unlikely(ret != 0)) {
  884. drm_modeset_unlock_all(dev_priv->dev);
  885. return ret;
  886. }
  887. if (!num_clips) {
  888. num_clips = 1;
  889. clips = &norect;
  890. norect.x1 = norect.y1 = 0;
  891. norect.x2 = framebuffer->width;
  892. norect.y2 = framebuffer->height;
  893. } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
  894. num_clips /= 2;
  895. increment = 2;
  896. }
  897. switch (dev_priv->active_display_unit) {
  898. case vmw_du_screen_target:
  899. ret = vmw_kms_stdu_dma(dev_priv, NULL, &vfbd->base, NULL,
  900. clips, NULL, num_clips, increment,
  901. true, true);
  902. break;
  903. case vmw_du_screen_object:
  904. ret = vmw_kms_sou_do_dmabuf_dirty(dev_priv, &vfbd->base,
  905. clips, NULL, num_clips,
  906. increment, true, NULL);
  907. break;
  908. case vmw_du_legacy:
  909. ret = vmw_kms_ldu_do_dmabuf_dirty(dev_priv, &vfbd->base, 0, 0,
  910. clips, num_clips, increment);
  911. break;
  912. default:
  913. ret = -EINVAL;
  914. WARN_ONCE(true, "Dirty called with invalid display system.\n");
  915. break;
  916. }
  917. vmw_fifo_flush(dev_priv, false);
  918. ttm_read_unlock(&dev_priv->reservation_sem);
  919. drm_modeset_unlock_all(dev_priv->dev);
  920. return ret;
  921. }
  922. static const struct drm_framebuffer_funcs vmw_framebuffer_dmabuf_funcs = {
  923. .destroy = vmw_framebuffer_dmabuf_destroy,
  924. .dirty = vmw_framebuffer_dmabuf_dirty,
  925. };
  926. /**
  927. * Pin the dmabuffer to the start of vram.
  928. */
  929. static int vmw_framebuffer_pin(struct vmw_framebuffer *vfb)
  930. {
  931. struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
  932. struct vmw_dma_buffer *buf;
  933. int ret;
  934. buf = vfb->dmabuf ? vmw_framebuffer_to_vfbd(&vfb->base)->buffer :
  935. vmw_framebuffer_to_vfbs(&vfb->base)->surface->res.backup;
  936. if (!buf)
  937. return 0;
  938. switch (dev_priv->active_display_unit) {
  939. case vmw_du_legacy:
  940. vmw_overlay_pause_all(dev_priv);
  941. ret = vmw_dmabuf_pin_in_start_of_vram(dev_priv, buf, false);
  942. vmw_overlay_resume_all(dev_priv);
  943. break;
  944. case vmw_du_screen_object:
  945. case vmw_du_screen_target:
  946. if (vfb->dmabuf)
  947. return vmw_dmabuf_pin_in_vram_or_gmr(dev_priv, buf,
  948. false);
  949. return vmw_dmabuf_pin_in_placement(dev_priv, buf,
  950. &vmw_mob_placement, false);
  951. default:
  952. return -EINVAL;
  953. }
  954. return ret;
  955. }
  956. static int vmw_framebuffer_unpin(struct vmw_framebuffer *vfb)
  957. {
  958. struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
  959. struct vmw_dma_buffer *buf;
  960. buf = vfb->dmabuf ? vmw_framebuffer_to_vfbd(&vfb->base)->buffer :
  961. vmw_framebuffer_to_vfbs(&vfb->base)->surface->res.backup;
  962. if (WARN_ON(!buf))
  963. return 0;
  964. return vmw_dmabuf_unpin(dev_priv, buf, false);
  965. }
  966. /**
  967. * vmw_create_dmabuf_proxy - create a proxy surface for the DMA buf
  968. *
  969. * @dev: DRM device
  970. * @mode_cmd: parameters for the new surface
  971. * @dmabuf_mob: MOB backing the DMA buf
  972. * @srf_out: newly created surface
  973. *
  974. * When the content FB is a DMA buf, we create a surface as a proxy to the
  975. * same buffer. This way we can do a surface copy rather than a surface DMA.
  976. * This is a more efficient approach
  977. *
  978. * RETURNS:
  979. * 0 on success, error code otherwise
  980. */
  981. static int vmw_create_dmabuf_proxy(struct drm_device *dev,
  982. const struct drm_mode_fb_cmd2 *mode_cmd,
  983. struct vmw_dma_buffer *dmabuf_mob,
  984. struct vmw_surface **srf_out)
  985. {
  986. uint32_t format;
  987. struct drm_vmw_size content_base_size = {0};
  988. struct vmw_resource *res;
  989. unsigned int bytes_pp;
  990. struct drm_format_name_buf format_name;
  991. int ret;
  992. switch (mode_cmd->pixel_format) {
  993. case DRM_FORMAT_ARGB8888:
  994. case DRM_FORMAT_XRGB8888:
  995. format = SVGA3D_X8R8G8B8;
  996. bytes_pp = 4;
  997. break;
  998. case DRM_FORMAT_RGB565:
  999. case DRM_FORMAT_XRGB1555:
  1000. format = SVGA3D_R5G6B5;
  1001. bytes_pp = 2;
  1002. break;
  1003. case 8:
  1004. format = SVGA3D_P8;
  1005. bytes_pp = 1;
  1006. break;
  1007. default:
  1008. DRM_ERROR("Invalid framebuffer format %s\n",
  1009. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  1010. return -EINVAL;
  1011. }
  1012. content_base_size.width = mode_cmd->pitches[0] / bytes_pp;
  1013. content_base_size.height = mode_cmd->height;
  1014. content_base_size.depth = 1;
  1015. ret = vmw_surface_gb_priv_define(dev,
  1016. 0, /* kernel visible only */
  1017. 0, /* flags */
  1018. format,
  1019. true, /* can be a scanout buffer */
  1020. 1, /* num of mip levels */
  1021. 0,
  1022. 0,
  1023. content_base_size,
  1024. srf_out);
  1025. if (ret) {
  1026. DRM_ERROR("Failed to allocate proxy content buffer\n");
  1027. return ret;
  1028. }
  1029. res = &(*srf_out)->res;
  1030. /* Reserve and switch the backing mob. */
  1031. mutex_lock(&res->dev_priv->cmdbuf_mutex);
  1032. (void) vmw_resource_reserve(res, false, true);
  1033. vmw_dmabuf_unreference(&res->backup);
  1034. res->backup = vmw_dmabuf_reference(dmabuf_mob);
  1035. res->backup_offset = 0;
  1036. vmw_resource_unreserve(res, false, NULL, 0);
  1037. mutex_unlock(&res->dev_priv->cmdbuf_mutex);
  1038. return 0;
  1039. }
  1040. static int vmw_kms_new_framebuffer_dmabuf(struct vmw_private *dev_priv,
  1041. struct vmw_dma_buffer *dmabuf,
  1042. struct vmw_framebuffer **out,
  1043. const struct drm_mode_fb_cmd2
  1044. *mode_cmd)
  1045. {
  1046. struct drm_device *dev = dev_priv->dev;
  1047. struct vmw_framebuffer_dmabuf *vfbd;
  1048. unsigned int requested_size;
  1049. struct drm_format_name_buf format_name;
  1050. int ret;
  1051. requested_size = mode_cmd->height * mode_cmd->pitches[0];
  1052. if (unlikely(requested_size > dmabuf->base.num_pages * PAGE_SIZE)) {
  1053. DRM_ERROR("Screen buffer object size is too small "
  1054. "for requested mode.\n");
  1055. return -EINVAL;
  1056. }
  1057. /* Limited framebuffer color depth support for screen objects */
  1058. if (dev_priv->active_display_unit == vmw_du_screen_object) {
  1059. switch (mode_cmd->pixel_format) {
  1060. case DRM_FORMAT_XRGB8888:
  1061. case DRM_FORMAT_ARGB8888:
  1062. break;
  1063. case DRM_FORMAT_XRGB1555:
  1064. case DRM_FORMAT_RGB565:
  1065. break;
  1066. default:
  1067. DRM_ERROR("Invalid pixel format: %s\n",
  1068. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  1069. return -EINVAL;
  1070. }
  1071. }
  1072. vfbd = kzalloc(sizeof(*vfbd), GFP_KERNEL);
  1073. if (!vfbd) {
  1074. ret = -ENOMEM;
  1075. goto out_err1;
  1076. }
  1077. drm_helper_mode_fill_fb_struct(dev, &vfbd->base.base, mode_cmd);
  1078. vfbd->base.dmabuf = true;
  1079. vfbd->buffer = vmw_dmabuf_reference(dmabuf);
  1080. vfbd->base.user_handle = mode_cmd->handles[0];
  1081. *out = &vfbd->base;
  1082. ret = drm_framebuffer_init(dev, &vfbd->base.base,
  1083. &vmw_framebuffer_dmabuf_funcs);
  1084. if (ret)
  1085. goto out_err2;
  1086. return 0;
  1087. out_err2:
  1088. vmw_dmabuf_unreference(&dmabuf);
  1089. kfree(vfbd);
  1090. out_err1:
  1091. return ret;
  1092. }
  1093. /**
  1094. * vmw_kms_srf_ok - check if a surface can be created
  1095. *
  1096. * @width: requested width
  1097. * @height: requested height
  1098. *
  1099. * Surfaces need to be less than texture size
  1100. */
  1101. static bool
  1102. vmw_kms_srf_ok(struct vmw_private *dev_priv, uint32_t width, uint32_t height)
  1103. {
  1104. if (width > dev_priv->texture_max_width ||
  1105. height > dev_priv->texture_max_height)
  1106. return false;
  1107. return true;
  1108. }
  1109. /**
  1110. * vmw_kms_new_framebuffer - Create a new framebuffer.
  1111. *
  1112. * @dev_priv: Pointer to device private struct.
  1113. * @dmabuf: Pointer to dma buffer to wrap the kms framebuffer around.
  1114. * Either @dmabuf or @surface must be NULL.
  1115. * @surface: Pointer to a surface to wrap the kms framebuffer around.
  1116. * Either @dmabuf or @surface must be NULL.
  1117. * @only_2d: No presents will occur to this dma buffer based framebuffer. This
  1118. * Helps the code to do some important optimizations.
  1119. * @mode_cmd: Frame-buffer metadata.
  1120. */
  1121. struct vmw_framebuffer *
  1122. vmw_kms_new_framebuffer(struct vmw_private *dev_priv,
  1123. struct vmw_dma_buffer *dmabuf,
  1124. struct vmw_surface *surface,
  1125. bool only_2d,
  1126. const struct drm_mode_fb_cmd2 *mode_cmd)
  1127. {
  1128. struct vmw_framebuffer *vfb = NULL;
  1129. bool is_dmabuf_proxy = false;
  1130. int ret;
  1131. /*
  1132. * We cannot use the SurfaceDMA command in an non-accelerated VM,
  1133. * therefore, wrap the DMA buf in a surface so we can use the
  1134. * SurfaceCopy command.
  1135. */
  1136. if (vmw_kms_srf_ok(dev_priv, mode_cmd->width, mode_cmd->height) &&
  1137. dmabuf && only_2d &&
  1138. mode_cmd->width > 64 && /* Don't create a proxy for cursor */
  1139. dev_priv->active_display_unit == vmw_du_screen_target) {
  1140. ret = vmw_create_dmabuf_proxy(dev_priv->dev, mode_cmd,
  1141. dmabuf, &surface);
  1142. if (ret)
  1143. return ERR_PTR(ret);
  1144. is_dmabuf_proxy = true;
  1145. }
  1146. /* Create the new framebuffer depending one what we have */
  1147. if (surface) {
  1148. ret = vmw_kms_new_framebuffer_surface(dev_priv, surface, &vfb,
  1149. mode_cmd,
  1150. is_dmabuf_proxy);
  1151. /*
  1152. * vmw_create_dmabuf_proxy() adds a reference that is no longer
  1153. * needed
  1154. */
  1155. if (is_dmabuf_proxy)
  1156. vmw_surface_unreference(&surface);
  1157. } else if (dmabuf) {
  1158. ret = vmw_kms_new_framebuffer_dmabuf(dev_priv, dmabuf, &vfb,
  1159. mode_cmd);
  1160. } else {
  1161. BUG();
  1162. }
  1163. if (ret)
  1164. return ERR_PTR(ret);
  1165. vfb->pin = vmw_framebuffer_pin;
  1166. vfb->unpin = vmw_framebuffer_unpin;
  1167. return vfb;
  1168. }
  1169. /*
  1170. * Generic Kernel modesetting functions
  1171. */
  1172. static struct drm_framebuffer *vmw_kms_fb_create(struct drm_device *dev,
  1173. struct drm_file *file_priv,
  1174. const struct drm_mode_fb_cmd2 *mode_cmd)
  1175. {
  1176. struct vmw_private *dev_priv = vmw_priv(dev);
  1177. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  1178. struct vmw_framebuffer *vfb = NULL;
  1179. struct vmw_surface *surface = NULL;
  1180. struct vmw_dma_buffer *bo = NULL;
  1181. struct ttm_base_object *user_obj;
  1182. int ret;
  1183. /**
  1184. * This code should be conditioned on Screen Objects not being used.
  1185. * If screen objects are used, we can allocate a GMR to hold the
  1186. * requested framebuffer.
  1187. */
  1188. if (!vmw_kms_validate_mode_vram(dev_priv,
  1189. mode_cmd->pitches[0],
  1190. mode_cmd->height)) {
  1191. DRM_ERROR("Requested mode exceed bounding box limit.\n");
  1192. return ERR_PTR(-ENOMEM);
  1193. }
  1194. /*
  1195. * Take a reference on the user object of the resource
  1196. * backing the kms fb. This ensures that user-space handle
  1197. * lookups on that resource will always work as long as
  1198. * it's registered with a kms framebuffer. This is important,
  1199. * since vmw_execbuf_process identifies resources in the
  1200. * command stream using user-space handles.
  1201. */
  1202. user_obj = ttm_base_object_lookup(tfile, mode_cmd->handles[0]);
  1203. if (unlikely(user_obj == NULL)) {
  1204. DRM_ERROR("Could not locate requested kms frame buffer.\n");
  1205. return ERR_PTR(-ENOENT);
  1206. }
  1207. /**
  1208. * End conditioned code.
  1209. */
  1210. /* returns either a dmabuf or surface */
  1211. ret = vmw_user_lookup_handle(dev_priv, tfile,
  1212. mode_cmd->handles[0],
  1213. &surface, &bo);
  1214. if (ret)
  1215. goto err_out;
  1216. if (!bo &&
  1217. !vmw_kms_srf_ok(dev_priv, mode_cmd->width, mode_cmd->height)) {
  1218. DRM_ERROR("Surface size cannot exceed %dx%d",
  1219. dev_priv->texture_max_width,
  1220. dev_priv->texture_max_height);
  1221. goto err_out;
  1222. }
  1223. vfb = vmw_kms_new_framebuffer(dev_priv, bo, surface,
  1224. !(dev_priv->capabilities & SVGA_CAP_3D),
  1225. mode_cmd);
  1226. if (IS_ERR(vfb)) {
  1227. ret = PTR_ERR(vfb);
  1228. goto err_out;
  1229. }
  1230. err_out:
  1231. /* vmw_user_lookup_handle takes one ref so does new_fb */
  1232. if (bo)
  1233. vmw_dmabuf_unreference(&bo);
  1234. if (surface)
  1235. vmw_surface_unreference(&surface);
  1236. if (ret) {
  1237. DRM_ERROR("failed to create vmw_framebuffer: %i\n", ret);
  1238. ttm_base_object_unref(&user_obj);
  1239. return ERR_PTR(ret);
  1240. } else
  1241. vfb->user_obj = user_obj;
  1242. return &vfb->base;
  1243. }
  1244. /**
  1245. * vmw_kms_atomic_check_modeset- validate state object for modeset changes
  1246. *
  1247. * @dev: DRM device
  1248. * @state: the driver state object
  1249. *
  1250. * This is a simple wrapper around drm_atomic_helper_check_modeset() for
  1251. * us to assign a value to mode->crtc_clock so that
  1252. * drm_calc_timestamping_constants() won't throw an error message
  1253. *
  1254. * RETURNS
  1255. * Zero for success or -errno
  1256. */
  1257. int
  1258. vmw_kms_atomic_check_modeset(struct drm_device *dev,
  1259. struct drm_atomic_state *state)
  1260. {
  1261. struct drm_crtc_state *crtc_state;
  1262. struct drm_crtc *crtc;
  1263. struct vmw_private *dev_priv = vmw_priv(dev);
  1264. int i;
  1265. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  1266. unsigned long requested_bb_mem = 0;
  1267. if (dev_priv->active_display_unit == vmw_du_screen_target) {
  1268. if (crtc->primary->fb) {
  1269. int cpp = crtc->primary->fb->pitches[0] /
  1270. crtc->primary->fb->width;
  1271. requested_bb_mem += crtc->mode.hdisplay * cpp *
  1272. crtc->mode.vdisplay;
  1273. }
  1274. if (requested_bb_mem > dev_priv->prim_bb_mem)
  1275. return -EINVAL;
  1276. }
  1277. }
  1278. return drm_atomic_helper_check(dev, state);
  1279. }
  1280. static const struct drm_mode_config_funcs vmw_kms_funcs = {
  1281. .fb_create = vmw_kms_fb_create,
  1282. .atomic_check = vmw_kms_atomic_check_modeset,
  1283. .atomic_commit = drm_atomic_helper_commit,
  1284. };
  1285. static int vmw_kms_generic_present(struct vmw_private *dev_priv,
  1286. struct drm_file *file_priv,
  1287. struct vmw_framebuffer *vfb,
  1288. struct vmw_surface *surface,
  1289. uint32_t sid,
  1290. int32_t destX, int32_t destY,
  1291. struct drm_vmw_rect *clips,
  1292. uint32_t num_clips)
  1293. {
  1294. return vmw_kms_sou_do_surface_dirty(dev_priv, vfb, NULL, clips,
  1295. &surface->res, destX, destY,
  1296. num_clips, 1, NULL);
  1297. }
  1298. int vmw_kms_present(struct vmw_private *dev_priv,
  1299. struct drm_file *file_priv,
  1300. struct vmw_framebuffer *vfb,
  1301. struct vmw_surface *surface,
  1302. uint32_t sid,
  1303. int32_t destX, int32_t destY,
  1304. struct drm_vmw_rect *clips,
  1305. uint32_t num_clips)
  1306. {
  1307. int ret;
  1308. switch (dev_priv->active_display_unit) {
  1309. case vmw_du_screen_target:
  1310. ret = vmw_kms_stdu_surface_dirty(dev_priv, vfb, NULL, clips,
  1311. &surface->res, destX, destY,
  1312. num_clips, 1, NULL);
  1313. break;
  1314. case vmw_du_screen_object:
  1315. ret = vmw_kms_generic_present(dev_priv, file_priv, vfb, surface,
  1316. sid, destX, destY, clips,
  1317. num_clips);
  1318. break;
  1319. default:
  1320. WARN_ONCE(true,
  1321. "Present called with invalid display system.\n");
  1322. ret = -ENOSYS;
  1323. break;
  1324. }
  1325. if (ret)
  1326. return ret;
  1327. vmw_fifo_flush(dev_priv, false);
  1328. return 0;
  1329. }
  1330. static void
  1331. vmw_kms_create_hotplug_mode_update_property(struct vmw_private *dev_priv)
  1332. {
  1333. if (dev_priv->hotplug_mode_update_property)
  1334. return;
  1335. dev_priv->hotplug_mode_update_property =
  1336. drm_property_create_range(dev_priv->dev,
  1337. DRM_MODE_PROP_IMMUTABLE,
  1338. "hotplug_mode_update", 0, 1);
  1339. if (!dev_priv->hotplug_mode_update_property)
  1340. return;
  1341. }
  1342. int vmw_kms_init(struct vmw_private *dev_priv)
  1343. {
  1344. struct drm_device *dev = dev_priv->dev;
  1345. int ret;
  1346. drm_mode_config_init(dev);
  1347. dev->mode_config.funcs = &vmw_kms_funcs;
  1348. dev->mode_config.min_width = 1;
  1349. dev->mode_config.min_height = 1;
  1350. dev->mode_config.max_width = dev_priv->texture_max_width;
  1351. dev->mode_config.max_height = dev_priv->texture_max_height;
  1352. drm_mode_create_suggested_offset_properties(dev);
  1353. vmw_kms_create_hotplug_mode_update_property(dev_priv);
  1354. ret = vmw_kms_stdu_init_display(dev_priv);
  1355. if (ret) {
  1356. ret = vmw_kms_sou_init_display(dev_priv);
  1357. if (ret) /* Fallback */
  1358. ret = vmw_kms_ldu_init_display(dev_priv);
  1359. }
  1360. return ret;
  1361. }
  1362. int vmw_kms_close(struct vmw_private *dev_priv)
  1363. {
  1364. int ret;
  1365. /*
  1366. * Docs says we should take the lock before calling this function
  1367. * but since it destroys encoders and our destructor calls
  1368. * drm_encoder_cleanup which takes the lock we deadlock.
  1369. */
  1370. drm_mode_config_cleanup(dev_priv->dev);
  1371. if (dev_priv->active_display_unit == vmw_du_screen_object)
  1372. ret = vmw_kms_sou_close_display(dev_priv);
  1373. else if (dev_priv->active_display_unit == vmw_du_screen_target)
  1374. ret = vmw_kms_stdu_close_display(dev_priv);
  1375. else
  1376. ret = vmw_kms_ldu_close_display(dev_priv);
  1377. return ret;
  1378. }
  1379. int vmw_kms_cursor_bypass_ioctl(struct drm_device *dev, void *data,
  1380. struct drm_file *file_priv)
  1381. {
  1382. struct drm_vmw_cursor_bypass_arg *arg = data;
  1383. struct vmw_display_unit *du;
  1384. struct drm_crtc *crtc;
  1385. int ret = 0;
  1386. mutex_lock(&dev->mode_config.mutex);
  1387. if (arg->flags & DRM_VMW_CURSOR_BYPASS_ALL) {
  1388. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  1389. du = vmw_crtc_to_du(crtc);
  1390. du->hotspot_x = arg->xhot;
  1391. du->hotspot_y = arg->yhot;
  1392. }
  1393. mutex_unlock(&dev->mode_config.mutex);
  1394. return 0;
  1395. }
  1396. crtc = drm_crtc_find(dev, arg->crtc_id);
  1397. if (!crtc) {
  1398. ret = -ENOENT;
  1399. goto out;
  1400. }
  1401. du = vmw_crtc_to_du(crtc);
  1402. du->hotspot_x = arg->xhot;
  1403. du->hotspot_y = arg->yhot;
  1404. out:
  1405. mutex_unlock(&dev->mode_config.mutex);
  1406. return ret;
  1407. }
  1408. int vmw_kms_write_svga(struct vmw_private *vmw_priv,
  1409. unsigned width, unsigned height, unsigned pitch,
  1410. unsigned bpp, unsigned depth)
  1411. {
  1412. if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
  1413. vmw_write(vmw_priv, SVGA_REG_PITCHLOCK, pitch);
  1414. else if (vmw_fifo_have_pitchlock(vmw_priv))
  1415. vmw_mmio_write(pitch, vmw_priv->mmio_virt +
  1416. SVGA_FIFO_PITCHLOCK);
  1417. vmw_write(vmw_priv, SVGA_REG_WIDTH, width);
  1418. vmw_write(vmw_priv, SVGA_REG_HEIGHT, height);
  1419. vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, bpp);
  1420. if (vmw_read(vmw_priv, SVGA_REG_DEPTH) != depth) {
  1421. DRM_ERROR("Invalid depth %u for %u bpp, host expects %u\n",
  1422. depth, bpp, vmw_read(vmw_priv, SVGA_REG_DEPTH));
  1423. return -EINVAL;
  1424. }
  1425. return 0;
  1426. }
  1427. int vmw_kms_save_vga(struct vmw_private *vmw_priv)
  1428. {
  1429. struct vmw_vga_topology_state *save;
  1430. uint32_t i;
  1431. vmw_priv->vga_width = vmw_read(vmw_priv, SVGA_REG_WIDTH);
  1432. vmw_priv->vga_height = vmw_read(vmw_priv, SVGA_REG_HEIGHT);
  1433. vmw_priv->vga_bpp = vmw_read(vmw_priv, SVGA_REG_BITS_PER_PIXEL);
  1434. if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
  1435. vmw_priv->vga_pitchlock =
  1436. vmw_read(vmw_priv, SVGA_REG_PITCHLOCK);
  1437. else if (vmw_fifo_have_pitchlock(vmw_priv))
  1438. vmw_priv->vga_pitchlock = vmw_mmio_read(vmw_priv->mmio_virt +
  1439. SVGA_FIFO_PITCHLOCK);
  1440. if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
  1441. return 0;
  1442. vmw_priv->num_displays = vmw_read(vmw_priv,
  1443. SVGA_REG_NUM_GUEST_DISPLAYS);
  1444. if (vmw_priv->num_displays == 0)
  1445. vmw_priv->num_displays = 1;
  1446. for (i = 0; i < vmw_priv->num_displays; ++i) {
  1447. save = &vmw_priv->vga_save[i];
  1448. vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
  1449. save->primary = vmw_read(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY);
  1450. save->pos_x = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_X);
  1451. save->pos_y = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y);
  1452. save->width = vmw_read(vmw_priv, SVGA_REG_DISPLAY_WIDTH);
  1453. save->height = vmw_read(vmw_priv, SVGA_REG_DISPLAY_HEIGHT);
  1454. vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
  1455. if (i == 0 && vmw_priv->num_displays == 1 &&
  1456. save->width == 0 && save->height == 0) {
  1457. /*
  1458. * It should be fairly safe to assume that these
  1459. * values are uninitialized.
  1460. */
  1461. save->width = vmw_priv->vga_width - save->pos_x;
  1462. save->height = vmw_priv->vga_height - save->pos_y;
  1463. }
  1464. }
  1465. return 0;
  1466. }
  1467. int vmw_kms_restore_vga(struct vmw_private *vmw_priv)
  1468. {
  1469. struct vmw_vga_topology_state *save;
  1470. uint32_t i;
  1471. vmw_write(vmw_priv, SVGA_REG_WIDTH, vmw_priv->vga_width);
  1472. vmw_write(vmw_priv, SVGA_REG_HEIGHT, vmw_priv->vga_height);
  1473. vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, vmw_priv->vga_bpp);
  1474. if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
  1475. vmw_write(vmw_priv, SVGA_REG_PITCHLOCK,
  1476. vmw_priv->vga_pitchlock);
  1477. else if (vmw_fifo_have_pitchlock(vmw_priv))
  1478. vmw_mmio_write(vmw_priv->vga_pitchlock,
  1479. vmw_priv->mmio_virt + SVGA_FIFO_PITCHLOCK);
  1480. if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
  1481. return 0;
  1482. for (i = 0; i < vmw_priv->num_displays; ++i) {
  1483. save = &vmw_priv->vga_save[i];
  1484. vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
  1485. vmw_write(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY, save->primary);
  1486. vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_X, save->pos_x);
  1487. vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y, save->pos_y);
  1488. vmw_write(vmw_priv, SVGA_REG_DISPLAY_WIDTH, save->width);
  1489. vmw_write(vmw_priv, SVGA_REG_DISPLAY_HEIGHT, save->height);
  1490. vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
  1491. }
  1492. return 0;
  1493. }
  1494. bool vmw_kms_validate_mode_vram(struct vmw_private *dev_priv,
  1495. uint32_t pitch,
  1496. uint32_t height)
  1497. {
  1498. return ((u64) pitch * (u64) height) < (u64)
  1499. ((dev_priv->active_display_unit == vmw_du_screen_target) ?
  1500. dev_priv->prim_bb_mem : dev_priv->vram_size);
  1501. }
  1502. /**
  1503. * Function called by DRM code called with vbl_lock held.
  1504. */
  1505. u32 vmw_get_vblank_counter(struct drm_device *dev, unsigned int pipe)
  1506. {
  1507. return 0;
  1508. }
  1509. /**
  1510. * Function called by DRM code called with vbl_lock held.
  1511. */
  1512. int vmw_enable_vblank(struct drm_device *dev, unsigned int pipe)
  1513. {
  1514. return -ENOSYS;
  1515. }
  1516. /**
  1517. * Function called by DRM code called with vbl_lock held.
  1518. */
  1519. void vmw_disable_vblank(struct drm_device *dev, unsigned int pipe)
  1520. {
  1521. }
  1522. /*
  1523. * Small shared kms functions.
  1524. */
  1525. static int vmw_du_update_layout(struct vmw_private *dev_priv, unsigned num,
  1526. struct drm_vmw_rect *rects)
  1527. {
  1528. struct drm_device *dev = dev_priv->dev;
  1529. struct vmw_display_unit *du;
  1530. struct drm_connector *con;
  1531. mutex_lock(&dev->mode_config.mutex);
  1532. #if 0
  1533. {
  1534. unsigned int i;
  1535. DRM_INFO("%s: new layout ", __func__);
  1536. for (i = 0; i < num; i++)
  1537. DRM_INFO("(%i, %i %ux%u) ", rects[i].x, rects[i].y,
  1538. rects[i].w, rects[i].h);
  1539. DRM_INFO("\n");
  1540. }
  1541. #endif
  1542. list_for_each_entry(con, &dev->mode_config.connector_list, head) {
  1543. du = vmw_connector_to_du(con);
  1544. if (num > du->unit) {
  1545. du->pref_width = rects[du->unit].w;
  1546. du->pref_height = rects[du->unit].h;
  1547. du->pref_active = true;
  1548. du->gui_x = rects[du->unit].x;
  1549. du->gui_y = rects[du->unit].y;
  1550. drm_object_property_set_value
  1551. (&con->base, dev->mode_config.suggested_x_property,
  1552. du->gui_x);
  1553. drm_object_property_set_value
  1554. (&con->base, dev->mode_config.suggested_y_property,
  1555. du->gui_y);
  1556. } else {
  1557. du->pref_width = 800;
  1558. du->pref_height = 600;
  1559. du->pref_active = false;
  1560. drm_object_property_set_value
  1561. (&con->base, dev->mode_config.suggested_x_property,
  1562. 0);
  1563. drm_object_property_set_value
  1564. (&con->base, dev->mode_config.suggested_y_property,
  1565. 0);
  1566. }
  1567. con->status = vmw_du_connector_detect(con, true);
  1568. }
  1569. mutex_unlock(&dev->mode_config.mutex);
  1570. drm_sysfs_hotplug_event(dev);
  1571. return 0;
  1572. }
  1573. int vmw_du_crtc_gamma_set(struct drm_crtc *crtc,
  1574. u16 *r, u16 *g, u16 *b,
  1575. uint32_t size,
  1576. struct drm_modeset_acquire_ctx *ctx)
  1577. {
  1578. struct vmw_private *dev_priv = vmw_priv(crtc->dev);
  1579. int i;
  1580. for (i = 0; i < size; i++) {
  1581. DRM_DEBUG("%d r/g/b = 0x%04x / 0x%04x / 0x%04x\n", i,
  1582. r[i], g[i], b[i]);
  1583. vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 0, r[i] >> 8);
  1584. vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 1, g[i] >> 8);
  1585. vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 2, b[i] >> 8);
  1586. }
  1587. return 0;
  1588. }
  1589. int vmw_du_connector_dpms(struct drm_connector *connector, int mode)
  1590. {
  1591. return 0;
  1592. }
  1593. enum drm_connector_status
  1594. vmw_du_connector_detect(struct drm_connector *connector, bool force)
  1595. {
  1596. uint32_t num_displays;
  1597. struct drm_device *dev = connector->dev;
  1598. struct vmw_private *dev_priv = vmw_priv(dev);
  1599. struct vmw_display_unit *du = vmw_connector_to_du(connector);
  1600. num_displays = vmw_read(dev_priv, SVGA_REG_NUM_DISPLAYS);
  1601. return ((vmw_connector_to_du(connector)->unit < num_displays &&
  1602. du->pref_active) ?
  1603. connector_status_connected : connector_status_disconnected);
  1604. }
  1605. static struct drm_display_mode vmw_kms_connector_builtin[] = {
  1606. /* 640x480@60Hz */
  1607. { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 25175, 640, 656,
  1608. 752, 800, 0, 480, 489, 492, 525, 0,
  1609. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
  1610. /* 800x600@60Hz */
  1611. { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 40000, 800, 840,
  1612. 968, 1056, 0, 600, 601, 605, 628, 0,
  1613. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1614. /* 1024x768@60Hz */
  1615. { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 65000, 1024, 1048,
  1616. 1184, 1344, 0, 768, 771, 777, 806, 0,
  1617. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
  1618. /* 1152x864@75Hz */
  1619. { DRM_MODE("1152x864", DRM_MODE_TYPE_DRIVER, 108000, 1152, 1216,
  1620. 1344, 1600, 0, 864, 865, 868, 900, 0,
  1621. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1622. /* 1280x768@60Hz */
  1623. { DRM_MODE("1280x768", DRM_MODE_TYPE_DRIVER, 79500, 1280, 1344,
  1624. 1472, 1664, 0, 768, 771, 778, 798, 0,
  1625. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1626. /* 1280x800@60Hz */
  1627. { DRM_MODE("1280x800", DRM_MODE_TYPE_DRIVER, 83500, 1280, 1352,
  1628. 1480, 1680, 0, 800, 803, 809, 831, 0,
  1629. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
  1630. /* 1280x960@60Hz */
  1631. { DRM_MODE("1280x960", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1376,
  1632. 1488, 1800, 0, 960, 961, 964, 1000, 0,
  1633. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1634. /* 1280x1024@60Hz */
  1635. { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1328,
  1636. 1440, 1688, 0, 1024, 1025, 1028, 1066, 0,
  1637. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1638. /* 1360x768@60Hz */
  1639. { DRM_MODE("1360x768", DRM_MODE_TYPE_DRIVER, 85500, 1360, 1424,
  1640. 1536, 1792, 0, 768, 771, 777, 795, 0,
  1641. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1642. /* 1440x1050@60Hz */
  1643. { DRM_MODE("1400x1050", DRM_MODE_TYPE_DRIVER, 121750, 1400, 1488,
  1644. 1632, 1864, 0, 1050, 1053, 1057, 1089, 0,
  1645. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1646. /* 1440x900@60Hz */
  1647. { DRM_MODE("1440x900", DRM_MODE_TYPE_DRIVER, 106500, 1440, 1520,
  1648. 1672, 1904, 0, 900, 903, 909, 934, 0,
  1649. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1650. /* 1600x1200@60Hz */
  1651. { DRM_MODE("1600x1200", DRM_MODE_TYPE_DRIVER, 162000, 1600, 1664,
  1652. 1856, 2160, 0, 1200, 1201, 1204, 1250, 0,
  1653. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1654. /* 1680x1050@60Hz */
  1655. { DRM_MODE("1680x1050", DRM_MODE_TYPE_DRIVER, 146250, 1680, 1784,
  1656. 1960, 2240, 0, 1050, 1053, 1059, 1089, 0,
  1657. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1658. /* 1792x1344@60Hz */
  1659. { DRM_MODE("1792x1344", DRM_MODE_TYPE_DRIVER, 204750, 1792, 1920,
  1660. 2120, 2448, 0, 1344, 1345, 1348, 1394, 0,
  1661. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1662. /* 1853x1392@60Hz */
  1663. { DRM_MODE("1856x1392", DRM_MODE_TYPE_DRIVER, 218250, 1856, 1952,
  1664. 2176, 2528, 0, 1392, 1393, 1396, 1439, 0,
  1665. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1666. /* 1920x1200@60Hz */
  1667. { DRM_MODE("1920x1200", DRM_MODE_TYPE_DRIVER, 193250, 1920, 2056,
  1668. 2256, 2592, 0, 1200, 1203, 1209, 1245, 0,
  1669. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1670. /* 1920x1440@60Hz */
  1671. { DRM_MODE("1920x1440", DRM_MODE_TYPE_DRIVER, 234000, 1920, 2048,
  1672. 2256, 2600, 0, 1440, 1441, 1444, 1500, 0,
  1673. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1674. /* 2560x1600@60Hz */
  1675. { DRM_MODE("2560x1600", DRM_MODE_TYPE_DRIVER, 348500, 2560, 2752,
  1676. 3032, 3504, 0, 1600, 1603, 1609, 1658, 0,
  1677. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1678. /* Terminate */
  1679. { DRM_MODE("", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) },
  1680. };
  1681. /**
  1682. * vmw_guess_mode_timing - Provide fake timings for a
  1683. * 60Hz vrefresh mode.
  1684. *
  1685. * @mode - Pointer to a struct drm_display_mode with hdisplay and vdisplay
  1686. * members filled in.
  1687. */
  1688. void vmw_guess_mode_timing(struct drm_display_mode *mode)
  1689. {
  1690. mode->hsync_start = mode->hdisplay + 50;
  1691. mode->hsync_end = mode->hsync_start + 50;
  1692. mode->htotal = mode->hsync_end + 50;
  1693. mode->vsync_start = mode->vdisplay + 50;
  1694. mode->vsync_end = mode->vsync_start + 50;
  1695. mode->vtotal = mode->vsync_end + 50;
  1696. mode->clock = (u32)mode->htotal * (u32)mode->vtotal / 100 * 6;
  1697. mode->vrefresh = drm_mode_vrefresh(mode);
  1698. }
  1699. int vmw_du_connector_fill_modes(struct drm_connector *connector,
  1700. uint32_t max_width, uint32_t max_height)
  1701. {
  1702. struct vmw_display_unit *du = vmw_connector_to_du(connector);
  1703. struct drm_device *dev = connector->dev;
  1704. struct vmw_private *dev_priv = vmw_priv(dev);
  1705. struct drm_display_mode *mode = NULL;
  1706. struct drm_display_mode *bmode;
  1707. struct drm_display_mode prefmode = { DRM_MODE("preferred",
  1708. DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED,
  1709. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1710. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC)
  1711. };
  1712. int i;
  1713. u32 assumed_bpp = 4;
  1714. if (dev_priv->assume_16bpp)
  1715. assumed_bpp = 2;
  1716. if (dev_priv->active_display_unit == vmw_du_screen_target) {
  1717. max_width = min(max_width, dev_priv->stdu_max_width);
  1718. max_width = min(max_width, dev_priv->texture_max_width);
  1719. max_height = min(max_height, dev_priv->stdu_max_height);
  1720. max_height = min(max_height, dev_priv->texture_max_height);
  1721. }
  1722. /* Add preferred mode */
  1723. mode = drm_mode_duplicate(dev, &prefmode);
  1724. if (!mode)
  1725. return 0;
  1726. mode->hdisplay = du->pref_width;
  1727. mode->vdisplay = du->pref_height;
  1728. vmw_guess_mode_timing(mode);
  1729. if (vmw_kms_validate_mode_vram(dev_priv,
  1730. mode->hdisplay * assumed_bpp,
  1731. mode->vdisplay)) {
  1732. drm_mode_probed_add(connector, mode);
  1733. } else {
  1734. drm_mode_destroy(dev, mode);
  1735. mode = NULL;
  1736. }
  1737. if (du->pref_mode) {
  1738. list_del_init(&du->pref_mode->head);
  1739. drm_mode_destroy(dev, du->pref_mode);
  1740. }
  1741. /* mode might be null here, this is intended */
  1742. du->pref_mode = mode;
  1743. for (i = 0; vmw_kms_connector_builtin[i].type != 0; i++) {
  1744. bmode = &vmw_kms_connector_builtin[i];
  1745. if (bmode->hdisplay > max_width ||
  1746. bmode->vdisplay > max_height)
  1747. continue;
  1748. if (!vmw_kms_validate_mode_vram(dev_priv,
  1749. bmode->hdisplay * assumed_bpp,
  1750. bmode->vdisplay))
  1751. continue;
  1752. mode = drm_mode_duplicate(dev, bmode);
  1753. if (!mode)
  1754. return 0;
  1755. mode->vrefresh = drm_mode_vrefresh(mode);
  1756. drm_mode_probed_add(connector, mode);
  1757. }
  1758. drm_mode_connector_list_update(connector);
  1759. /* Move the prefered mode first, help apps pick the right mode. */
  1760. drm_mode_sort(&connector->modes);
  1761. return 1;
  1762. }
  1763. int vmw_du_connector_set_property(struct drm_connector *connector,
  1764. struct drm_property *property,
  1765. uint64_t val)
  1766. {
  1767. struct vmw_display_unit *du = vmw_connector_to_du(connector);
  1768. struct vmw_private *dev_priv = vmw_priv(connector->dev);
  1769. if (property == dev_priv->implicit_placement_property)
  1770. du->is_implicit = val;
  1771. return 0;
  1772. }
  1773. /**
  1774. * vmw_du_connector_atomic_set_property - Atomic version of get property
  1775. *
  1776. * @crtc - crtc the property is associated with
  1777. *
  1778. * Returns:
  1779. * Zero on success, negative errno on failure.
  1780. */
  1781. int
  1782. vmw_du_connector_atomic_set_property(struct drm_connector *connector,
  1783. struct drm_connector_state *state,
  1784. struct drm_property *property,
  1785. uint64_t val)
  1786. {
  1787. struct vmw_private *dev_priv = vmw_priv(connector->dev);
  1788. struct vmw_connector_state *vcs = vmw_connector_state_to_vcs(state);
  1789. struct vmw_display_unit *du = vmw_connector_to_du(connector);
  1790. if (property == dev_priv->implicit_placement_property) {
  1791. vcs->is_implicit = val;
  1792. /*
  1793. * We should really be doing a drm_atomic_commit() to
  1794. * commit the new state, but since this doesn't cause
  1795. * an immedate state change, this is probably ok
  1796. */
  1797. du->is_implicit = vcs->is_implicit;
  1798. } else {
  1799. return -EINVAL;
  1800. }
  1801. return 0;
  1802. }
  1803. /**
  1804. * vmw_du_connector_atomic_get_property - Atomic version of get property
  1805. *
  1806. * @connector - connector the property is associated with
  1807. *
  1808. * Returns:
  1809. * Zero on success, negative errno on failure.
  1810. */
  1811. int
  1812. vmw_du_connector_atomic_get_property(struct drm_connector *connector,
  1813. const struct drm_connector_state *state,
  1814. struct drm_property *property,
  1815. uint64_t *val)
  1816. {
  1817. struct vmw_private *dev_priv = vmw_priv(connector->dev);
  1818. struct vmw_connector_state *vcs = vmw_connector_state_to_vcs(state);
  1819. if (property == dev_priv->implicit_placement_property)
  1820. *val = vcs->is_implicit;
  1821. else {
  1822. DRM_ERROR("Invalid Property %s\n", property->name);
  1823. return -EINVAL;
  1824. }
  1825. return 0;
  1826. }
  1827. int vmw_kms_update_layout_ioctl(struct drm_device *dev, void *data,
  1828. struct drm_file *file_priv)
  1829. {
  1830. struct vmw_private *dev_priv = vmw_priv(dev);
  1831. struct drm_vmw_update_layout_arg *arg =
  1832. (struct drm_vmw_update_layout_arg *)data;
  1833. void __user *user_rects;
  1834. struct drm_vmw_rect *rects;
  1835. unsigned rects_size;
  1836. int ret;
  1837. int i;
  1838. u64 total_pixels = 0;
  1839. struct drm_mode_config *mode_config = &dev->mode_config;
  1840. struct drm_vmw_rect bounding_box = {0};
  1841. if (!arg->num_outputs) {
  1842. struct drm_vmw_rect def_rect = {0, 0, 800, 600};
  1843. vmw_du_update_layout(dev_priv, 1, &def_rect);
  1844. return 0;
  1845. }
  1846. rects_size = arg->num_outputs * sizeof(struct drm_vmw_rect);
  1847. rects = kcalloc(arg->num_outputs, sizeof(struct drm_vmw_rect),
  1848. GFP_KERNEL);
  1849. if (unlikely(!rects))
  1850. return -ENOMEM;
  1851. user_rects = (void __user *)(unsigned long)arg->rects;
  1852. ret = copy_from_user(rects, user_rects, rects_size);
  1853. if (unlikely(ret != 0)) {
  1854. DRM_ERROR("Failed to get rects.\n");
  1855. ret = -EFAULT;
  1856. goto out_free;
  1857. }
  1858. for (i = 0; i < arg->num_outputs; ++i) {
  1859. if (rects[i].x < 0 ||
  1860. rects[i].y < 0 ||
  1861. rects[i].x + rects[i].w > mode_config->max_width ||
  1862. rects[i].y + rects[i].h > mode_config->max_height) {
  1863. DRM_ERROR("Invalid GUI layout.\n");
  1864. ret = -EINVAL;
  1865. goto out_free;
  1866. }
  1867. /*
  1868. * bounding_box.w and bunding_box.h are used as
  1869. * lower-right coordinates
  1870. */
  1871. if (rects[i].x + rects[i].w > bounding_box.w)
  1872. bounding_box.w = rects[i].x + rects[i].w;
  1873. if (rects[i].y + rects[i].h > bounding_box.h)
  1874. bounding_box.h = rects[i].y + rects[i].h;
  1875. total_pixels += (u64) rects[i].w * (u64) rects[i].h;
  1876. }
  1877. if (dev_priv->active_display_unit == vmw_du_screen_target) {
  1878. /*
  1879. * For Screen Targets, the limits for a toplogy are:
  1880. * 1. Bounding box (assuming 32bpp) must be < prim_bb_mem
  1881. * 2. Total pixels (assuming 32bpp) must be < prim_bb_mem
  1882. */
  1883. u64 bb_mem = (u64) bounding_box.w * bounding_box.h * 4;
  1884. u64 pixel_mem = total_pixels * 4;
  1885. if (bb_mem > dev_priv->prim_bb_mem) {
  1886. DRM_ERROR("Topology is beyond supported limits.\n");
  1887. ret = -EINVAL;
  1888. goto out_free;
  1889. }
  1890. if (pixel_mem > dev_priv->prim_bb_mem) {
  1891. DRM_ERROR("Combined output size too large\n");
  1892. ret = -EINVAL;
  1893. goto out_free;
  1894. }
  1895. }
  1896. vmw_du_update_layout(dev_priv, arg->num_outputs, rects);
  1897. out_free:
  1898. kfree(rects);
  1899. return ret;
  1900. }
  1901. /**
  1902. * vmw_kms_helper_dirty - Helper to build commands and perform actions based
  1903. * on a set of cliprects and a set of display units.
  1904. *
  1905. * @dev_priv: Pointer to a device private structure.
  1906. * @framebuffer: Pointer to the framebuffer on which to perform the actions.
  1907. * @clips: A set of struct drm_clip_rect. Either this os @vclips must be NULL.
  1908. * Cliprects are given in framebuffer coordinates.
  1909. * @vclips: A set of struct drm_vmw_rect cliprects. Either this or @clips must
  1910. * be NULL. Cliprects are given in source coordinates.
  1911. * @dest_x: X coordinate offset for the crtc / destination clip rects.
  1912. * @dest_y: Y coordinate offset for the crtc / destination clip rects.
  1913. * @num_clips: Number of cliprects in the @clips or @vclips array.
  1914. * @increment: Integer with which to increment the clip counter when looping.
  1915. * Used to skip a predetermined number of clip rects.
  1916. * @dirty: Closure structure. See the description of struct vmw_kms_dirty.
  1917. */
  1918. int vmw_kms_helper_dirty(struct vmw_private *dev_priv,
  1919. struct vmw_framebuffer *framebuffer,
  1920. const struct drm_clip_rect *clips,
  1921. const struct drm_vmw_rect *vclips,
  1922. s32 dest_x, s32 dest_y,
  1923. int num_clips,
  1924. int increment,
  1925. struct vmw_kms_dirty *dirty)
  1926. {
  1927. struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
  1928. struct drm_crtc *crtc;
  1929. u32 num_units = 0;
  1930. u32 i, k;
  1931. dirty->dev_priv = dev_priv;
  1932. list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list, head) {
  1933. if (crtc->primary->fb != &framebuffer->base)
  1934. continue;
  1935. units[num_units++] = vmw_crtc_to_du(crtc);
  1936. }
  1937. for (k = 0; k < num_units; k++) {
  1938. struct vmw_display_unit *unit = units[k];
  1939. s32 crtc_x = unit->crtc.x;
  1940. s32 crtc_y = unit->crtc.y;
  1941. s32 crtc_width = unit->crtc.mode.hdisplay;
  1942. s32 crtc_height = unit->crtc.mode.vdisplay;
  1943. const struct drm_clip_rect *clips_ptr = clips;
  1944. const struct drm_vmw_rect *vclips_ptr = vclips;
  1945. dirty->unit = unit;
  1946. if (dirty->fifo_reserve_size > 0) {
  1947. dirty->cmd = vmw_fifo_reserve(dev_priv,
  1948. dirty->fifo_reserve_size);
  1949. if (!dirty->cmd) {
  1950. DRM_ERROR("Couldn't reserve fifo space "
  1951. "for dirty blits.\n");
  1952. return -ENOMEM;
  1953. }
  1954. memset(dirty->cmd, 0, dirty->fifo_reserve_size);
  1955. }
  1956. dirty->num_hits = 0;
  1957. for (i = 0; i < num_clips; i++, clips_ptr += increment,
  1958. vclips_ptr += increment) {
  1959. s32 clip_left;
  1960. s32 clip_top;
  1961. /*
  1962. * Select clip array type. Note that integer type
  1963. * in @clips is unsigned short, whereas in @vclips
  1964. * it's 32-bit.
  1965. */
  1966. if (clips) {
  1967. dirty->fb_x = (s32) clips_ptr->x1;
  1968. dirty->fb_y = (s32) clips_ptr->y1;
  1969. dirty->unit_x2 = (s32) clips_ptr->x2 + dest_x -
  1970. crtc_x;
  1971. dirty->unit_y2 = (s32) clips_ptr->y2 + dest_y -
  1972. crtc_y;
  1973. } else {
  1974. dirty->fb_x = vclips_ptr->x;
  1975. dirty->fb_y = vclips_ptr->y;
  1976. dirty->unit_x2 = dirty->fb_x + vclips_ptr->w +
  1977. dest_x - crtc_x;
  1978. dirty->unit_y2 = dirty->fb_y + vclips_ptr->h +
  1979. dest_y - crtc_y;
  1980. }
  1981. dirty->unit_x1 = dirty->fb_x + dest_x - crtc_x;
  1982. dirty->unit_y1 = dirty->fb_y + dest_y - crtc_y;
  1983. /* Skip this clip if it's outside the crtc region */
  1984. if (dirty->unit_x1 >= crtc_width ||
  1985. dirty->unit_y1 >= crtc_height ||
  1986. dirty->unit_x2 <= 0 || dirty->unit_y2 <= 0)
  1987. continue;
  1988. /* Clip right and bottom to crtc limits */
  1989. dirty->unit_x2 = min_t(s32, dirty->unit_x2,
  1990. crtc_width);
  1991. dirty->unit_y2 = min_t(s32, dirty->unit_y2,
  1992. crtc_height);
  1993. /* Clip left and top to crtc limits */
  1994. clip_left = min_t(s32, dirty->unit_x1, 0);
  1995. clip_top = min_t(s32, dirty->unit_y1, 0);
  1996. dirty->unit_x1 -= clip_left;
  1997. dirty->unit_y1 -= clip_top;
  1998. dirty->fb_x -= clip_left;
  1999. dirty->fb_y -= clip_top;
  2000. dirty->clip(dirty);
  2001. }
  2002. dirty->fifo_commit(dirty);
  2003. }
  2004. return 0;
  2005. }
  2006. /**
  2007. * vmw_kms_helper_buffer_prepare - Reserve and validate a buffer object before
  2008. * command submission.
  2009. *
  2010. * @dev_priv. Pointer to a device private structure.
  2011. * @buf: The buffer object
  2012. * @interruptible: Whether to perform waits as interruptible.
  2013. * @validate_as_mob: Whether the buffer should be validated as a MOB. If false,
  2014. * The buffer will be validated as a GMR. Already pinned buffers will not be
  2015. * validated.
  2016. *
  2017. * Returns 0 on success, negative error code on failure, -ERESTARTSYS if
  2018. * interrupted by a signal.
  2019. */
  2020. int vmw_kms_helper_buffer_prepare(struct vmw_private *dev_priv,
  2021. struct vmw_dma_buffer *buf,
  2022. bool interruptible,
  2023. bool validate_as_mob)
  2024. {
  2025. struct ttm_buffer_object *bo = &buf->base;
  2026. int ret;
  2027. ttm_bo_reserve(bo, false, false, NULL);
  2028. ret = vmw_validate_single_buffer(dev_priv, bo, interruptible,
  2029. validate_as_mob);
  2030. if (ret)
  2031. ttm_bo_unreserve(bo);
  2032. return ret;
  2033. }
  2034. /**
  2035. * vmw_kms_helper_buffer_revert - Undo the actions of
  2036. * vmw_kms_helper_buffer_prepare.
  2037. *
  2038. * @res: Pointer to the buffer object.
  2039. *
  2040. * Helper to be used if an error forces the caller to undo the actions of
  2041. * vmw_kms_helper_buffer_prepare.
  2042. */
  2043. void vmw_kms_helper_buffer_revert(struct vmw_dma_buffer *buf)
  2044. {
  2045. if (buf)
  2046. ttm_bo_unreserve(&buf->base);
  2047. }
  2048. /**
  2049. * vmw_kms_helper_buffer_finish - Unreserve and fence a buffer object after
  2050. * kms command submission.
  2051. *
  2052. * @dev_priv: Pointer to a device private structure.
  2053. * @file_priv: Pointer to a struct drm_file representing the caller's
  2054. * connection. Must be set to NULL if @user_fence_rep is NULL, and conversely
  2055. * if non-NULL, @user_fence_rep must be non-NULL.
  2056. * @buf: The buffer object.
  2057. * @out_fence: Optional pointer to a fence pointer. If non-NULL, a
  2058. * ref-counted fence pointer is returned here.
  2059. * @user_fence_rep: Optional pointer to a user-space provided struct
  2060. * drm_vmw_fence_rep. If provided, @file_priv must also be provided and the
  2061. * function copies fence data to user-space in a fail-safe manner.
  2062. */
  2063. void vmw_kms_helper_buffer_finish(struct vmw_private *dev_priv,
  2064. struct drm_file *file_priv,
  2065. struct vmw_dma_buffer *buf,
  2066. struct vmw_fence_obj **out_fence,
  2067. struct drm_vmw_fence_rep __user *
  2068. user_fence_rep)
  2069. {
  2070. struct vmw_fence_obj *fence;
  2071. uint32_t handle;
  2072. int ret;
  2073. ret = vmw_execbuf_fence_commands(file_priv, dev_priv, &fence,
  2074. file_priv ? &handle : NULL);
  2075. if (buf)
  2076. vmw_fence_single_bo(&buf->base, fence);
  2077. if (file_priv)
  2078. vmw_execbuf_copy_fence_user(dev_priv, vmw_fpriv(file_priv),
  2079. ret, user_fence_rep, fence,
  2080. handle);
  2081. if (out_fence)
  2082. *out_fence = fence;
  2083. else
  2084. vmw_fence_obj_unreference(&fence);
  2085. vmw_kms_helper_buffer_revert(buf);
  2086. }
  2087. /**
  2088. * vmw_kms_helper_resource_revert - Undo the actions of
  2089. * vmw_kms_helper_resource_prepare.
  2090. *
  2091. * @res: Pointer to the resource. Typically a surface.
  2092. *
  2093. * Helper to be used if an error forces the caller to undo the actions of
  2094. * vmw_kms_helper_resource_prepare.
  2095. */
  2096. void vmw_kms_helper_resource_revert(struct vmw_resource *res)
  2097. {
  2098. vmw_kms_helper_buffer_revert(res->backup);
  2099. vmw_resource_unreserve(res, false, NULL, 0);
  2100. mutex_unlock(&res->dev_priv->cmdbuf_mutex);
  2101. }
  2102. /**
  2103. * vmw_kms_helper_resource_prepare - Reserve and validate a resource before
  2104. * command submission.
  2105. *
  2106. * @res: Pointer to the resource. Typically a surface.
  2107. * @interruptible: Whether to perform waits as interruptible.
  2108. *
  2109. * Reserves and validates also the backup buffer if a guest-backed resource.
  2110. * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
  2111. * interrupted by a signal.
  2112. */
  2113. int vmw_kms_helper_resource_prepare(struct vmw_resource *res,
  2114. bool interruptible)
  2115. {
  2116. int ret = 0;
  2117. if (interruptible)
  2118. ret = mutex_lock_interruptible(&res->dev_priv->cmdbuf_mutex);
  2119. else
  2120. mutex_lock(&res->dev_priv->cmdbuf_mutex);
  2121. if (unlikely(ret != 0))
  2122. return -ERESTARTSYS;
  2123. ret = vmw_resource_reserve(res, interruptible, false);
  2124. if (ret)
  2125. goto out_unlock;
  2126. if (res->backup) {
  2127. ret = vmw_kms_helper_buffer_prepare(res->dev_priv, res->backup,
  2128. interruptible,
  2129. res->dev_priv->has_mob);
  2130. if (ret)
  2131. goto out_unreserve;
  2132. }
  2133. ret = vmw_resource_validate(res);
  2134. if (ret)
  2135. goto out_revert;
  2136. return 0;
  2137. out_revert:
  2138. vmw_kms_helper_buffer_revert(res->backup);
  2139. out_unreserve:
  2140. vmw_resource_unreserve(res, false, NULL, 0);
  2141. out_unlock:
  2142. mutex_unlock(&res->dev_priv->cmdbuf_mutex);
  2143. return ret;
  2144. }
  2145. /**
  2146. * vmw_kms_helper_resource_finish - Unreserve and fence a resource after
  2147. * kms command submission.
  2148. *
  2149. * @res: Pointer to the resource. Typically a surface.
  2150. * @out_fence: Optional pointer to a fence pointer. If non-NULL, a
  2151. * ref-counted fence pointer is returned here.
  2152. */
  2153. void vmw_kms_helper_resource_finish(struct vmw_resource *res,
  2154. struct vmw_fence_obj **out_fence)
  2155. {
  2156. if (res->backup || out_fence)
  2157. vmw_kms_helper_buffer_finish(res->dev_priv, NULL, res->backup,
  2158. out_fence, NULL);
  2159. vmw_resource_unreserve(res, false, NULL, 0);
  2160. mutex_unlock(&res->dev_priv->cmdbuf_mutex);
  2161. }
  2162. /**
  2163. * vmw_kms_update_proxy - Helper function to update a proxy surface from
  2164. * its backing MOB.
  2165. *
  2166. * @res: Pointer to the surface resource
  2167. * @clips: Clip rects in framebuffer (surface) space.
  2168. * @num_clips: Number of clips in @clips.
  2169. * @increment: Integer with which to increment the clip counter when looping.
  2170. * Used to skip a predetermined number of clip rects.
  2171. *
  2172. * This function makes sure the proxy surface is updated from its backing MOB
  2173. * using the region given by @clips. The surface resource @res and its backing
  2174. * MOB needs to be reserved and validated on call.
  2175. */
  2176. int vmw_kms_update_proxy(struct vmw_resource *res,
  2177. const struct drm_clip_rect *clips,
  2178. unsigned num_clips,
  2179. int increment)
  2180. {
  2181. struct vmw_private *dev_priv = res->dev_priv;
  2182. struct drm_vmw_size *size = &vmw_res_to_srf(res)->base_size;
  2183. struct {
  2184. SVGA3dCmdHeader header;
  2185. SVGA3dCmdUpdateGBImage body;
  2186. } *cmd;
  2187. SVGA3dBox *box;
  2188. size_t copy_size = 0;
  2189. int i;
  2190. if (!clips)
  2191. return 0;
  2192. cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd) * num_clips);
  2193. if (!cmd) {
  2194. DRM_ERROR("Couldn't reserve fifo space for proxy surface "
  2195. "update.\n");
  2196. return -ENOMEM;
  2197. }
  2198. for (i = 0; i < num_clips; ++i, clips += increment, ++cmd) {
  2199. box = &cmd->body.box;
  2200. cmd->header.id = SVGA_3D_CMD_UPDATE_GB_IMAGE;
  2201. cmd->header.size = sizeof(cmd->body);
  2202. cmd->body.image.sid = res->id;
  2203. cmd->body.image.face = 0;
  2204. cmd->body.image.mipmap = 0;
  2205. if (clips->x1 > size->width || clips->x2 > size->width ||
  2206. clips->y1 > size->height || clips->y2 > size->height) {
  2207. DRM_ERROR("Invalid clips outsize of framebuffer.\n");
  2208. return -EINVAL;
  2209. }
  2210. box->x = clips->x1;
  2211. box->y = clips->y1;
  2212. box->z = 0;
  2213. box->w = clips->x2 - clips->x1;
  2214. box->h = clips->y2 - clips->y1;
  2215. box->d = 1;
  2216. copy_size += sizeof(*cmd);
  2217. }
  2218. vmw_fifo_commit(dev_priv, copy_size);
  2219. return 0;
  2220. }
  2221. int vmw_kms_fbdev_init_data(struct vmw_private *dev_priv,
  2222. unsigned unit,
  2223. u32 max_width,
  2224. u32 max_height,
  2225. struct drm_connector **p_con,
  2226. struct drm_crtc **p_crtc,
  2227. struct drm_display_mode **p_mode)
  2228. {
  2229. struct drm_connector *con;
  2230. struct vmw_display_unit *du;
  2231. struct drm_display_mode *mode;
  2232. int i = 0;
  2233. list_for_each_entry(con, &dev_priv->dev->mode_config.connector_list,
  2234. head) {
  2235. if (i == unit)
  2236. break;
  2237. ++i;
  2238. }
  2239. if (i != unit) {
  2240. DRM_ERROR("Could not find initial display unit.\n");
  2241. return -EINVAL;
  2242. }
  2243. if (list_empty(&con->modes))
  2244. (void) vmw_du_connector_fill_modes(con, max_width, max_height);
  2245. if (list_empty(&con->modes)) {
  2246. DRM_ERROR("Could not find initial display mode.\n");
  2247. return -EINVAL;
  2248. }
  2249. du = vmw_connector_to_du(con);
  2250. *p_con = con;
  2251. *p_crtc = &du->crtc;
  2252. list_for_each_entry(mode, &con->modes, head) {
  2253. if (mode->type & DRM_MODE_TYPE_PREFERRED)
  2254. break;
  2255. }
  2256. if (mode->type & DRM_MODE_TYPE_PREFERRED)
  2257. *p_mode = mode;
  2258. else {
  2259. WARN_ONCE(true, "Could not find initial preferred mode.\n");
  2260. *p_mode = list_first_entry(&con->modes,
  2261. struct drm_display_mode,
  2262. head);
  2263. }
  2264. return 0;
  2265. }
  2266. /**
  2267. * vmw_kms_del_active - unregister a crtc binding to the implicit framebuffer
  2268. *
  2269. * @dev_priv: Pointer to a device private struct.
  2270. * @du: The display unit of the crtc.
  2271. */
  2272. void vmw_kms_del_active(struct vmw_private *dev_priv,
  2273. struct vmw_display_unit *du)
  2274. {
  2275. mutex_lock(&dev_priv->global_kms_state_mutex);
  2276. if (du->active_implicit) {
  2277. if (--(dev_priv->num_implicit) == 0)
  2278. dev_priv->implicit_fb = NULL;
  2279. du->active_implicit = false;
  2280. }
  2281. mutex_unlock(&dev_priv->global_kms_state_mutex);
  2282. }
  2283. /**
  2284. * vmw_kms_add_active - register a crtc binding to an implicit framebuffer
  2285. *
  2286. * @vmw_priv: Pointer to a device private struct.
  2287. * @du: The display unit of the crtc.
  2288. * @vfb: The implicit framebuffer
  2289. *
  2290. * Registers a binding to an implicit framebuffer.
  2291. */
  2292. void vmw_kms_add_active(struct vmw_private *dev_priv,
  2293. struct vmw_display_unit *du,
  2294. struct vmw_framebuffer *vfb)
  2295. {
  2296. mutex_lock(&dev_priv->global_kms_state_mutex);
  2297. WARN_ON_ONCE(!dev_priv->num_implicit && dev_priv->implicit_fb);
  2298. if (!du->active_implicit && du->is_implicit) {
  2299. dev_priv->implicit_fb = vfb;
  2300. du->active_implicit = true;
  2301. dev_priv->num_implicit++;
  2302. }
  2303. mutex_unlock(&dev_priv->global_kms_state_mutex);
  2304. }
  2305. /**
  2306. * vmw_kms_screen_object_flippable - Check whether we can page-flip a crtc.
  2307. *
  2308. * @dev_priv: Pointer to device-private struct.
  2309. * @crtc: The crtc we want to flip.
  2310. *
  2311. * Returns true or false depending whether it's OK to flip this crtc
  2312. * based on the criterion that we must not have more than one implicit
  2313. * frame-buffer at any one time.
  2314. */
  2315. bool vmw_kms_crtc_flippable(struct vmw_private *dev_priv,
  2316. struct drm_crtc *crtc)
  2317. {
  2318. struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
  2319. bool ret;
  2320. mutex_lock(&dev_priv->global_kms_state_mutex);
  2321. ret = !du->is_implicit || dev_priv->num_implicit == 1;
  2322. mutex_unlock(&dev_priv->global_kms_state_mutex);
  2323. return ret;
  2324. }
  2325. /**
  2326. * vmw_kms_update_implicit_fb - Update the implicit fb.
  2327. *
  2328. * @dev_priv: Pointer to device-private struct.
  2329. * @crtc: The crtc the new implicit frame-buffer is bound to.
  2330. */
  2331. void vmw_kms_update_implicit_fb(struct vmw_private *dev_priv,
  2332. struct drm_crtc *crtc)
  2333. {
  2334. struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
  2335. struct vmw_framebuffer *vfb;
  2336. mutex_lock(&dev_priv->global_kms_state_mutex);
  2337. if (!du->is_implicit)
  2338. goto out_unlock;
  2339. vfb = vmw_framebuffer_to_vfb(crtc->primary->fb);
  2340. WARN_ON_ONCE(dev_priv->num_implicit != 1 &&
  2341. dev_priv->implicit_fb != vfb);
  2342. dev_priv->implicit_fb = vfb;
  2343. out_unlock:
  2344. mutex_unlock(&dev_priv->global_kms_state_mutex);
  2345. }
  2346. /**
  2347. * vmw_kms_create_implicit_placement_proparty - Set up the implicit placement
  2348. * property.
  2349. *
  2350. * @dev_priv: Pointer to a device private struct.
  2351. * @immutable: Whether the property is immutable.
  2352. *
  2353. * Sets up the implicit placement property unless it's already set up.
  2354. */
  2355. void
  2356. vmw_kms_create_implicit_placement_property(struct vmw_private *dev_priv,
  2357. bool immutable)
  2358. {
  2359. if (dev_priv->implicit_placement_property)
  2360. return;
  2361. dev_priv->implicit_placement_property =
  2362. drm_property_create_range(dev_priv->dev,
  2363. immutable ?
  2364. DRM_MODE_PROP_IMMUTABLE : 0,
  2365. "implicit_placement", 0, 1);
  2366. }
  2367. /**
  2368. * vmw_kms_set_config - Wrapper around drm_atomic_helper_set_config
  2369. *
  2370. * @set: The configuration to set.
  2371. *
  2372. * The vmwgfx Xorg driver doesn't assign the mode::type member, which
  2373. * when drm_mode_set_crtcinfo is called as part of the configuration setting
  2374. * causes it to return incorrect crtc dimensions causing severe problems in
  2375. * the vmwgfx modesetting. So explicitly clear that member before calling
  2376. * into drm_atomic_helper_set_config.
  2377. */
  2378. int vmw_kms_set_config(struct drm_mode_set *set,
  2379. struct drm_modeset_acquire_ctx *ctx)
  2380. {
  2381. if (set && set->mode)
  2382. set->mode->type = 0;
  2383. return drm_atomic_helper_set_config(set, ctx);
  2384. }