vmwgfx_kms.c 81 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097
  1. // SPDX-License-Identifier: GPL-2.0 OR MIT
  2. /**************************************************************************
  3. *
  4. * Copyright 2009-2015 VMware, Inc., Palo Alto, CA., USA
  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_bo(struct vmw_private *dev_priv,
  75. struct vmw_buffer_object *bo,
  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(&bo->base, true, false, NULL);
  88. if (unlikely(ret != 0)) {
  89. DRM_ERROR("reserve failed\n");
  90. return -EINVAL;
  91. }
  92. ret = ttm_bo_kmap(&bo->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(&bo->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->bo)
  293. vmw_bo_unreference(&vps->bo);
  294. if (fb) {
  295. if (vmw_framebuffer_to_vfb(fb)->bo) {
  296. vps->bo = vmw_framebuffer_to_vfbd(fb)->buffer;
  297. vmw_bo_reference(vps->bo);
  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. if (plane->state->fb) {
  318. hotspot_x += plane->state->fb->hot_x;
  319. hotspot_y += plane->state->fb->hot_y;
  320. }
  321. du->cursor_surface = vps->surf;
  322. du->cursor_bo = vps->bo;
  323. if (vps->surf) {
  324. du->cursor_age = du->cursor_surface->snooper.age;
  325. ret = vmw_cursor_update_image(dev_priv,
  326. vps->surf->snooper.image,
  327. 64, 64, hotspot_x,
  328. hotspot_y);
  329. } else if (vps->bo) {
  330. ret = vmw_cursor_update_bo(dev_priv, vps->bo,
  331. plane->state->crtc_w,
  332. plane->state->crtc_h,
  333. hotspot_x, hotspot_y);
  334. } else {
  335. vmw_cursor_update_position(dev_priv, false, 0, 0);
  336. return;
  337. }
  338. if (!ret) {
  339. du->cursor_x = plane->state->crtc_x + du->set_gui_x;
  340. du->cursor_y = plane->state->crtc_y + du->set_gui_y;
  341. vmw_cursor_update_position(dev_priv, true,
  342. du->cursor_x + hotspot_x,
  343. du->cursor_y + hotspot_y);
  344. du->core_hotspot_x = hotspot_x - du->hotspot_x;
  345. du->core_hotspot_y = hotspot_y - du->hotspot_y;
  346. } else {
  347. DRM_ERROR("Failed to update cursor image\n");
  348. }
  349. }
  350. /**
  351. * vmw_du_primary_plane_atomic_check - check if the new state is okay
  352. *
  353. * @plane: display plane
  354. * @state: info on the new plane state, including the FB
  355. *
  356. * Check if the new state is settable given the current state. Other
  357. * than what the atomic helper checks, we care about crtc fitting
  358. * the FB and maintaining one active framebuffer.
  359. *
  360. * Returns 0 on success
  361. */
  362. int vmw_du_primary_plane_atomic_check(struct drm_plane *plane,
  363. struct drm_plane_state *state)
  364. {
  365. struct drm_crtc_state *crtc_state = NULL;
  366. struct drm_framebuffer *new_fb = state->fb;
  367. int ret;
  368. if (state->crtc)
  369. crtc_state = drm_atomic_get_new_crtc_state(state->state, state->crtc);
  370. ret = drm_atomic_helper_check_plane_state(state, crtc_state,
  371. DRM_PLANE_HELPER_NO_SCALING,
  372. DRM_PLANE_HELPER_NO_SCALING,
  373. false, true);
  374. if (!ret && new_fb) {
  375. struct drm_crtc *crtc = state->crtc;
  376. struct vmw_connector_state *vcs;
  377. struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
  378. struct vmw_private *dev_priv = vmw_priv(crtc->dev);
  379. struct vmw_framebuffer *vfb = vmw_framebuffer_to_vfb(new_fb);
  380. vcs = vmw_connector_state_to_vcs(du->connector.state);
  381. /* Only one active implicit framebuffer at a time. */
  382. mutex_lock(&dev_priv->global_kms_state_mutex);
  383. if (vcs->is_implicit && dev_priv->implicit_fb &&
  384. !(dev_priv->num_implicit == 1 && du->active_implicit)
  385. && dev_priv->implicit_fb != vfb) {
  386. DRM_ERROR("Multiple implicit framebuffers "
  387. "not supported.\n");
  388. ret = -EINVAL;
  389. }
  390. mutex_unlock(&dev_priv->global_kms_state_mutex);
  391. }
  392. return ret;
  393. }
  394. /**
  395. * vmw_du_cursor_plane_atomic_check - check if the new state is okay
  396. *
  397. * @plane: cursor plane
  398. * @state: info on the new plane state
  399. *
  400. * This is a chance to fail if the new cursor state does not fit
  401. * our requirements.
  402. *
  403. * Returns 0 on success
  404. */
  405. int vmw_du_cursor_plane_atomic_check(struct drm_plane *plane,
  406. struct drm_plane_state *new_state)
  407. {
  408. int ret = 0;
  409. struct vmw_surface *surface = NULL;
  410. struct drm_framebuffer *fb = new_state->fb;
  411. struct drm_rect src = drm_plane_state_src(new_state);
  412. struct drm_rect dest = drm_plane_state_dest(new_state);
  413. /* Turning off */
  414. if (!fb)
  415. return ret;
  416. ret = drm_plane_helper_check_update(plane, new_state->crtc, fb,
  417. &src, &dest,
  418. DRM_MODE_ROTATE_0,
  419. DRM_PLANE_HELPER_NO_SCALING,
  420. DRM_PLANE_HELPER_NO_SCALING,
  421. true, true, &new_state->visible);
  422. if (!ret)
  423. return ret;
  424. /* A lot of the code assumes this */
  425. if (new_state->crtc_w != 64 || new_state->crtc_h != 64) {
  426. DRM_ERROR("Invalid cursor dimensions (%d, %d)\n",
  427. new_state->crtc_w, new_state->crtc_h);
  428. ret = -EINVAL;
  429. }
  430. if (!vmw_framebuffer_to_vfb(fb)->bo)
  431. surface = vmw_framebuffer_to_vfbs(fb)->surface;
  432. if (surface && !surface->snooper.image) {
  433. DRM_ERROR("surface not suitable for cursor\n");
  434. ret = -EINVAL;
  435. }
  436. return ret;
  437. }
  438. int vmw_du_crtc_atomic_check(struct drm_crtc *crtc,
  439. struct drm_crtc_state *new_state)
  440. {
  441. struct vmw_display_unit *du = vmw_crtc_to_du(new_state->crtc);
  442. int connector_mask = drm_connector_mask(&du->connector);
  443. bool has_primary = new_state->plane_mask &
  444. drm_plane_mask(crtc->primary);
  445. /* We always want to have an active plane with an active CRTC */
  446. if (has_primary != new_state->enable)
  447. return -EINVAL;
  448. if (new_state->connector_mask != connector_mask &&
  449. new_state->connector_mask != 0) {
  450. DRM_ERROR("Invalid connectors configuration\n");
  451. return -EINVAL;
  452. }
  453. /*
  454. * Our virtual device does not have a dot clock, so use the logical
  455. * clock value as the dot clock.
  456. */
  457. if (new_state->mode.crtc_clock == 0)
  458. new_state->adjusted_mode.crtc_clock = new_state->mode.clock;
  459. return 0;
  460. }
  461. void vmw_du_crtc_atomic_begin(struct drm_crtc *crtc,
  462. struct drm_crtc_state *old_crtc_state)
  463. {
  464. }
  465. void vmw_du_crtc_atomic_flush(struct drm_crtc *crtc,
  466. struct drm_crtc_state *old_crtc_state)
  467. {
  468. struct drm_pending_vblank_event *event = crtc->state->event;
  469. if (event) {
  470. crtc->state->event = NULL;
  471. spin_lock_irq(&crtc->dev->event_lock);
  472. drm_crtc_send_vblank_event(crtc, event);
  473. spin_unlock_irq(&crtc->dev->event_lock);
  474. }
  475. }
  476. /**
  477. * vmw_du_crtc_duplicate_state - duplicate crtc state
  478. * @crtc: DRM crtc
  479. *
  480. * Allocates and returns a copy of the crtc state (both common and
  481. * vmw-specific) for the specified crtc.
  482. *
  483. * Returns: The newly allocated crtc state, or NULL on failure.
  484. */
  485. struct drm_crtc_state *
  486. vmw_du_crtc_duplicate_state(struct drm_crtc *crtc)
  487. {
  488. struct drm_crtc_state *state;
  489. struct vmw_crtc_state *vcs;
  490. if (WARN_ON(!crtc->state))
  491. return NULL;
  492. vcs = kmemdup(crtc->state, sizeof(*vcs), GFP_KERNEL);
  493. if (!vcs)
  494. return NULL;
  495. state = &vcs->base;
  496. __drm_atomic_helper_crtc_duplicate_state(crtc, state);
  497. return state;
  498. }
  499. /**
  500. * vmw_du_crtc_reset - creates a blank vmw crtc state
  501. * @crtc: DRM crtc
  502. *
  503. * Resets the atomic state for @crtc by freeing the state pointer (which
  504. * might be NULL, e.g. at driver load time) and allocating a new empty state
  505. * object.
  506. */
  507. void vmw_du_crtc_reset(struct drm_crtc *crtc)
  508. {
  509. struct vmw_crtc_state *vcs;
  510. if (crtc->state) {
  511. __drm_atomic_helper_crtc_destroy_state(crtc->state);
  512. kfree(vmw_crtc_state_to_vcs(crtc->state));
  513. }
  514. vcs = kzalloc(sizeof(*vcs), GFP_KERNEL);
  515. if (!vcs) {
  516. DRM_ERROR("Cannot allocate vmw_crtc_state\n");
  517. return;
  518. }
  519. crtc->state = &vcs->base;
  520. crtc->state->crtc = crtc;
  521. }
  522. /**
  523. * vmw_du_crtc_destroy_state - destroy crtc state
  524. * @crtc: DRM crtc
  525. * @state: state object to destroy
  526. *
  527. * Destroys the crtc state (both common and vmw-specific) for the
  528. * specified plane.
  529. */
  530. void
  531. vmw_du_crtc_destroy_state(struct drm_crtc *crtc,
  532. struct drm_crtc_state *state)
  533. {
  534. drm_atomic_helper_crtc_destroy_state(crtc, state);
  535. }
  536. /**
  537. * vmw_du_plane_duplicate_state - duplicate plane state
  538. * @plane: drm plane
  539. *
  540. * Allocates and returns a copy of the plane state (both common and
  541. * vmw-specific) for the specified plane.
  542. *
  543. * Returns: The newly allocated plane state, or NULL on failure.
  544. */
  545. struct drm_plane_state *
  546. vmw_du_plane_duplicate_state(struct drm_plane *plane)
  547. {
  548. struct drm_plane_state *state;
  549. struct vmw_plane_state *vps;
  550. vps = kmemdup(plane->state, sizeof(*vps), GFP_KERNEL);
  551. if (!vps)
  552. return NULL;
  553. vps->pinned = 0;
  554. vps->cpp = 0;
  555. /* Each ref counted resource needs to be acquired again */
  556. if (vps->surf)
  557. (void) vmw_surface_reference(vps->surf);
  558. if (vps->bo)
  559. (void) vmw_bo_reference(vps->bo);
  560. state = &vps->base;
  561. __drm_atomic_helper_plane_duplicate_state(plane, state);
  562. return state;
  563. }
  564. /**
  565. * vmw_du_plane_reset - creates a blank vmw plane state
  566. * @plane: drm plane
  567. *
  568. * Resets the atomic state for @plane by freeing the state pointer (which might
  569. * be NULL, e.g. at driver load time) and allocating a new empty state object.
  570. */
  571. void vmw_du_plane_reset(struct drm_plane *plane)
  572. {
  573. struct vmw_plane_state *vps;
  574. if (plane->state)
  575. vmw_du_plane_destroy_state(plane, plane->state);
  576. vps = kzalloc(sizeof(*vps), GFP_KERNEL);
  577. if (!vps) {
  578. DRM_ERROR("Cannot allocate vmw_plane_state\n");
  579. return;
  580. }
  581. plane->state = &vps->base;
  582. plane->state->plane = plane;
  583. plane->state->rotation = DRM_MODE_ROTATE_0;
  584. }
  585. /**
  586. * vmw_du_plane_destroy_state - destroy plane state
  587. * @plane: DRM plane
  588. * @state: state object to destroy
  589. *
  590. * Destroys the plane state (both common and vmw-specific) for the
  591. * specified plane.
  592. */
  593. void
  594. vmw_du_plane_destroy_state(struct drm_plane *plane,
  595. struct drm_plane_state *state)
  596. {
  597. struct vmw_plane_state *vps = vmw_plane_state_to_vps(state);
  598. /* Should have been freed by cleanup_fb */
  599. if (vps->surf)
  600. vmw_surface_unreference(&vps->surf);
  601. if (vps->bo)
  602. vmw_bo_unreference(&vps->bo);
  603. drm_atomic_helper_plane_destroy_state(plane, state);
  604. }
  605. /**
  606. * vmw_du_connector_duplicate_state - duplicate connector state
  607. * @connector: DRM connector
  608. *
  609. * Allocates and returns a copy of the connector state (both common and
  610. * vmw-specific) for the specified connector.
  611. *
  612. * Returns: The newly allocated connector state, or NULL on failure.
  613. */
  614. struct drm_connector_state *
  615. vmw_du_connector_duplicate_state(struct drm_connector *connector)
  616. {
  617. struct drm_connector_state *state;
  618. struct vmw_connector_state *vcs;
  619. if (WARN_ON(!connector->state))
  620. return NULL;
  621. vcs = kmemdup(connector->state, sizeof(*vcs), GFP_KERNEL);
  622. if (!vcs)
  623. return NULL;
  624. state = &vcs->base;
  625. __drm_atomic_helper_connector_duplicate_state(connector, state);
  626. return state;
  627. }
  628. /**
  629. * vmw_du_connector_reset - creates a blank vmw connector state
  630. * @connector: DRM connector
  631. *
  632. * Resets the atomic state for @connector by freeing the state pointer (which
  633. * might be NULL, e.g. at driver load time) and allocating a new empty state
  634. * object.
  635. */
  636. void vmw_du_connector_reset(struct drm_connector *connector)
  637. {
  638. struct vmw_connector_state *vcs;
  639. if (connector->state) {
  640. __drm_atomic_helper_connector_destroy_state(connector->state);
  641. kfree(vmw_connector_state_to_vcs(connector->state));
  642. }
  643. vcs = kzalloc(sizeof(*vcs), GFP_KERNEL);
  644. if (!vcs) {
  645. DRM_ERROR("Cannot allocate vmw_connector_state\n");
  646. return;
  647. }
  648. __drm_atomic_helper_connector_reset(connector, &vcs->base);
  649. }
  650. /**
  651. * vmw_du_connector_destroy_state - destroy connector state
  652. * @connector: DRM connector
  653. * @state: state object to destroy
  654. *
  655. * Destroys the connector state (both common and vmw-specific) for the
  656. * specified plane.
  657. */
  658. void
  659. vmw_du_connector_destroy_state(struct drm_connector *connector,
  660. struct drm_connector_state *state)
  661. {
  662. drm_atomic_helper_connector_destroy_state(connector, state);
  663. }
  664. /*
  665. * Generic framebuffer code
  666. */
  667. /*
  668. * Surface framebuffer code
  669. */
  670. static void vmw_framebuffer_surface_destroy(struct drm_framebuffer *framebuffer)
  671. {
  672. struct vmw_framebuffer_surface *vfbs =
  673. vmw_framebuffer_to_vfbs(framebuffer);
  674. drm_framebuffer_cleanup(framebuffer);
  675. vmw_surface_unreference(&vfbs->surface);
  676. if (vfbs->base.user_obj)
  677. ttm_base_object_unref(&vfbs->base.user_obj);
  678. kfree(vfbs);
  679. }
  680. static int vmw_framebuffer_surface_dirty(struct drm_framebuffer *framebuffer,
  681. struct drm_file *file_priv,
  682. unsigned flags, unsigned color,
  683. struct drm_clip_rect *clips,
  684. unsigned num_clips)
  685. {
  686. struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
  687. struct vmw_framebuffer_surface *vfbs =
  688. vmw_framebuffer_to_vfbs(framebuffer);
  689. struct drm_clip_rect norect;
  690. int ret, inc = 1;
  691. /* Legacy Display Unit does not support 3D */
  692. if (dev_priv->active_display_unit == vmw_du_legacy)
  693. return -EINVAL;
  694. drm_modeset_lock_all(dev_priv->dev);
  695. ret = ttm_read_lock(&dev_priv->reservation_sem, true);
  696. if (unlikely(ret != 0)) {
  697. drm_modeset_unlock_all(dev_priv->dev);
  698. return ret;
  699. }
  700. if (!num_clips) {
  701. num_clips = 1;
  702. clips = &norect;
  703. norect.x1 = norect.y1 = 0;
  704. norect.x2 = framebuffer->width;
  705. norect.y2 = framebuffer->height;
  706. } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
  707. num_clips /= 2;
  708. inc = 2; /* skip source rects */
  709. }
  710. if (dev_priv->active_display_unit == vmw_du_screen_object)
  711. ret = vmw_kms_sou_do_surface_dirty(dev_priv, &vfbs->base,
  712. clips, NULL, NULL, 0, 0,
  713. num_clips, inc, NULL, NULL);
  714. else
  715. ret = vmw_kms_stdu_surface_dirty(dev_priv, &vfbs->base,
  716. clips, NULL, NULL, 0, 0,
  717. num_clips, inc, NULL, NULL);
  718. vmw_fifo_flush(dev_priv, false);
  719. ttm_read_unlock(&dev_priv->reservation_sem);
  720. drm_modeset_unlock_all(dev_priv->dev);
  721. return 0;
  722. }
  723. /**
  724. * vmw_kms_readback - Perform a readback from the screen system to
  725. * a buffer-object backed framebuffer.
  726. *
  727. * @dev_priv: Pointer to the device private structure.
  728. * @file_priv: Pointer to a struct drm_file identifying the caller.
  729. * Must be set to NULL if @user_fence_rep is NULL.
  730. * @vfb: Pointer to the buffer-object backed framebuffer.
  731. * @user_fence_rep: User-space provided structure for fence information.
  732. * Must be set to non-NULL if @file_priv is non-NULL.
  733. * @vclips: Array of clip rects.
  734. * @num_clips: Number of clip rects in @vclips.
  735. *
  736. * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
  737. * interrupted.
  738. */
  739. int vmw_kms_readback(struct vmw_private *dev_priv,
  740. struct drm_file *file_priv,
  741. struct vmw_framebuffer *vfb,
  742. struct drm_vmw_fence_rep __user *user_fence_rep,
  743. struct drm_vmw_rect *vclips,
  744. uint32_t num_clips)
  745. {
  746. switch (dev_priv->active_display_unit) {
  747. case vmw_du_screen_object:
  748. return vmw_kms_sou_readback(dev_priv, file_priv, vfb,
  749. user_fence_rep, vclips, num_clips,
  750. NULL);
  751. case vmw_du_screen_target:
  752. return vmw_kms_stdu_dma(dev_priv, file_priv, vfb,
  753. user_fence_rep, NULL, vclips, num_clips,
  754. 1, false, true, NULL);
  755. default:
  756. WARN_ONCE(true,
  757. "Readback called with invalid display system.\n");
  758. }
  759. return -ENOSYS;
  760. }
  761. static const struct drm_framebuffer_funcs vmw_framebuffer_surface_funcs = {
  762. .destroy = vmw_framebuffer_surface_destroy,
  763. .dirty = vmw_framebuffer_surface_dirty,
  764. };
  765. static int vmw_kms_new_framebuffer_surface(struct vmw_private *dev_priv,
  766. struct vmw_surface *surface,
  767. struct vmw_framebuffer **out,
  768. const struct drm_mode_fb_cmd2
  769. *mode_cmd,
  770. bool is_bo_proxy)
  771. {
  772. struct drm_device *dev = dev_priv->dev;
  773. struct vmw_framebuffer_surface *vfbs;
  774. enum SVGA3dSurfaceFormat format;
  775. int ret;
  776. struct drm_format_name_buf format_name;
  777. /* 3D is only supported on HWv8 and newer hosts */
  778. if (dev_priv->active_display_unit == vmw_du_legacy)
  779. return -ENOSYS;
  780. /*
  781. * Sanity checks.
  782. */
  783. /* Surface must be marked as a scanout. */
  784. if (unlikely(!surface->scanout))
  785. return -EINVAL;
  786. if (unlikely(surface->mip_levels[0] != 1 ||
  787. surface->num_sizes != 1 ||
  788. surface->base_size.width < mode_cmd->width ||
  789. surface->base_size.height < mode_cmd->height ||
  790. surface->base_size.depth != 1)) {
  791. DRM_ERROR("Incompatible surface dimensions "
  792. "for requested mode.\n");
  793. return -EINVAL;
  794. }
  795. switch (mode_cmd->pixel_format) {
  796. case DRM_FORMAT_ARGB8888:
  797. format = SVGA3D_A8R8G8B8;
  798. break;
  799. case DRM_FORMAT_XRGB8888:
  800. format = SVGA3D_X8R8G8B8;
  801. break;
  802. case DRM_FORMAT_RGB565:
  803. format = SVGA3D_R5G6B5;
  804. break;
  805. case DRM_FORMAT_XRGB1555:
  806. format = SVGA3D_A1R5G5B5;
  807. break;
  808. default:
  809. DRM_ERROR("Invalid pixel format: %s\n",
  810. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  811. return -EINVAL;
  812. }
  813. /*
  814. * For DX, surface format validation is done when surface->scanout
  815. * is set.
  816. */
  817. if (!dev_priv->has_dx && format != surface->format) {
  818. DRM_ERROR("Invalid surface format for requested mode.\n");
  819. return -EINVAL;
  820. }
  821. vfbs = kzalloc(sizeof(*vfbs), GFP_KERNEL);
  822. if (!vfbs) {
  823. ret = -ENOMEM;
  824. goto out_err1;
  825. }
  826. drm_helper_mode_fill_fb_struct(dev, &vfbs->base.base, mode_cmd);
  827. vfbs->surface = vmw_surface_reference(surface);
  828. vfbs->base.user_handle = mode_cmd->handles[0];
  829. vfbs->is_bo_proxy = is_bo_proxy;
  830. *out = &vfbs->base;
  831. ret = drm_framebuffer_init(dev, &vfbs->base.base,
  832. &vmw_framebuffer_surface_funcs);
  833. if (ret)
  834. goto out_err2;
  835. return 0;
  836. out_err2:
  837. vmw_surface_unreference(&surface);
  838. kfree(vfbs);
  839. out_err1:
  840. return ret;
  841. }
  842. /*
  843. * Buffer-object framebuffer code
  844. */
  845. static void vmw_framebuffer_bo_destroy(struct drm_framebuffer *framebuffer)
  846. {
  847. struct vmw_framebuffer_bo *vfbd =
  848. vmw_framebuffer_to_vfbd(framebuffer);
  849. drm_framebuffer_cleanup(framebuffer);
  850. vmw_bo_unreference(&vfbd->buffer);
  851. if (vfbd->base.user_obj)
  852. ttm_base_object_unref(&vfbd->base.user_obj);
  853. kfree(vfbd);
  854. }
  855. static int vmw_framebuffer_bo_dirty(struct drm_framebuffer *framebuffer,
  856. struct drm_file *file_priv,
  857. unsigned int flags, unsigned int color,
  858. struct drm_clip_rect *clips,
  859. unsigned int num_clips)
  860. {
  861. struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
  862. struct vmw_framebuffer_bo *vfbd =
  863. vmw_framebuffer_to_vfbd(framebuffer);
  864. struct drm_clip_rect norect;
  865. int ret, increment = 1;
  866. drm_modeset_lock_all(dev_priv->dev);
  867. ret = ttm_read_lock(&dev_priv->reservation_sem, true);
  868. if (unlikely(ret != 0)) {
  869. drm_modeset_unlock_all(dev_priv->dev);
  870. return ret;
  871. }
  872. if (!num_clips) {
  873. num_clips = 1;
  874. clips = &norect;
  875. norect.x1 = norect.y1 = 0;
  876. norect.x2 = framebuffer->width;
  877. norect.y2 = framebuffer->height;
  878. } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
  879. num_clips /= 2;
  880. increment = 2;
  881. }
  882. switch (dev_priv->active_display_unit) {
  883. case vmw_du_screen_target:
  884. ret = vmw_kms_stdu_dma(dev_priv, NULL, &vfbd->base, NULL,
  885. clips, NULL, num_clips, increment,
  886. true, true, NULL);
  887. break;
  888. case vmw_du_screen_object:
  889. ret = vmw_kms_sou_do_bo_dirty(dev_priv, &vfbd->base,
  890. clips, NULL, num_clips,
  891. increment, true, NULL, NULL);
  892. break;
  893. case vmw_du_legacy:
  894. ret = vmw_kms_ldu_do_bo_dirty(dev_priv, &vfbd->base, 0, 0,
  895. clips, num_clips, increment);
  896. break;
  897. default:
  898. ret = -EINVAL;
  899. WARN_ONCE(true, "Dirty called with invalid display system.\n");
  900. break;
  901. }
  902. vmw_fifo_flush(dev_priv, false);
  903. ttm_read_unlock(&dev_priv->reservation_sem);
  904. drm_modeset_unlock_all(dev_priv->dev);
  905. return ret;
  906. }
  907. static const struct drm_framebuffer_funcs vmw_framebuffer_bo_funcs = {
  908. .destroy = vmw_framebuffer_bo_destroy,
  909. .dirty = vmw_framebuffer_bo_dirty,
  910. };
  911. /**
  912. * Pin the bofer in a location suitable for access by the
  913. * display system.
  914. */
  915. static int vmw_framebuffer_pin(struct vmw_framebuffer *vfb)
  916. {
  917. struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
  918. struct vmw_buffer_object *buf;
  919. struct ttm_placement *placement;
  920. int ret;
  921. buf = vfb->bo ? vmw_framebuffer_to_vfbd(&vfb->base)->buffer :
  922. vmw_framebuffer_to_vfbs(&vfb->base)->surface->res.backup;
  923. if (!buf)
  924. return 0;
  925. switch (dev_priv->active_display_unit) {
  926. case vmw_du_legacy:
  927. vmw_overlay_pause_all(dev_priv);
  928. ret = vmw_bo_pin_in_start_of_vram(dev_priv, buf, false);
  929. vmw_overlay_resume_all(dev_priv);
  930. break;
  931. case vmw_du_screen_object:
  932. case vmw_du_screen_target:
  933. if (vfb->bo) {
  934. if (dev_priv->capabilities & SVGA_CAP_3D) {
  935. /*
  936. * Use surface DMA to get content to
  937. * sreen target surface.
  938. */
  939. placement = &vmw_vram_gmr_placement;
  940. } else {
  941. /* Use CPU blit. */
  942. placement = &vmw_sys_placement;
  943. }
  944. } else {
  945. /* Use surface / image update */
  946. placement = &vmw_mob_placement;
  947. }
  948. return vmw_bo_pin_in_placement(dev_priv, buf, placement, false);
  949. default:
  950. return -EINVAL;
  951. }
  952. return ret;
  953. }
  954. static int vmw_framebuffer_unpin(struct vmw_framebuffer *vfb)
  955. {
  956. struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
  957. struct vmw_buffer_object *buf;
  958. buf = vfb->bo ? vmw_framebuffer_to_vfbd(&vfb->base)->buffer :
  959. vmw_framebuffer_to_vfbs(&vfb->base)->surface->res.backup;
  960. if (WARN_ON(!buf))
  961. return 0;
  962. return vmw_bo_unpin(dev_priv, buf, false);
  963. }
  964. /**
  965. * vmw_create_bo_proxy - create a proxy surface for the buffer object
  966. *
  967. * @dev: DRM device
  968. * @mode_cmd: parameters for the new surface
  969. * @bo_mob: MOB backing the buffer object
  970. * @srf_out: newly created surface
  971. *
  972. * When the content FB is a buffer object, we create a surface as a proxy to the
  973. * same buffer. This way we can do a surface copy rather than a surface DMA.
  974. * This is a more efficient approach
  975. *
  976. * RETURNS:
  977. * 0 on success, error code otherwise
  978. */
  979. static int vmw_create_bo_proxy(struct drm_device *dev,
  980. const struct drm_mode_fb_cmd2 *mode_cmd,
  981. struct vmw_buffer_object *bo_mob,
  982. struct vmw_surface **srf_out)
  983. {
  984. uint32_t format;
  985. struct drm_vmw_size content_base_size = {0};
  986. struct vmw_resource *res;
  987. unsigned int bytes_pp;
  988. struct drm_format_name_buf format_name;
  989. int ret;
  990. switch (mode_cmd->pixel_format) {
  991. case DRM_FORMAT_ARGB8888:
  992. case DRM_FORMAT_XRGB8888:
  993. format = SVGA3D_X8R8G8B8;
  994. bytes_pp = 4;
  995. break;
  996. case DRM_FORMAT_RGB565:
  997. case DRM_FORMAT_XRGB1555:
  998. format = SVGA3D_R5G6B5;
  999. bytes_pp = 2;
  1000. break;
  1001. case 8:
  1002. format = SVGA3D_P8;
  1003. bytes_pp = 1;
  1004. break;
  1005. default:
  1006. DRM_ERROR("Invalid framebuffer format %s\n",
  1007. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  1008. return -EINVAL;
  1009. }
  1010. content_base_size.width = mode_cmd->pitches[0] / bytes_pp;
  1011. content_base_size.height = mode_cmd->height;
  1012. content_base_size.depth = 1;
  1013. ret = vmw_surface_gb_priv_define(dev,
  1014. 0, /* kernel visible only */
  1015. 0, /* flags */
  1016. format,
  1017. true, /* can be a scanout buffer */
  1018. 1, /* num of mip levels */
  1019. 0,
  1020. 0,
  1021. content_base_size,
  1022. SVGA3D_MS_PATTERN_NONE,
  1023. SVGA3D_MS_QUALITY_NONE,
  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_bo_unreference(&res->backup);
  1034. res->backup = vmw_bo_reference(bo_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_bo(struct vmw_private *dev_priv,
  1041. struct vmw_buffer_object *bo,
  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_bo *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 > bo->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.bo = true;
  1079. vfbd->buffer = vmw_bo_reference(bo);
  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_bo_funcs);
  1084. if (ret)
  1085. goto out_err2;
  1086. return 0;
  1087. out_err2:
  1088. vmw_bo_unreference(&bo);
  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. * @bo: Pointer to buffer object to wrap the kms framebuffer around.
  1114. * Either @bo or @surface must be NULL.
  1115. * @surface: Pointer to a surface to wrap the kms framebuffer around.
  1116. * Either @bo or @surface must be NULL.
  1117. * @only_2d: No presents will occur to this buffer object based framebuffer.
  1118. * This 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_buffer_object *bo,
  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_bo_proxy = false;
  1130. int ret;
  1131. /*
  1132. * We cannot use the SurfaceDMA command in an non-accelerated VM,
  1133. * therefore, wrap the buffer object 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. bo && 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_bo_proxy(dev_priv->dev, mode_cmd,
  1141. bo, &surface);
  1142. if (ret)
  1143. return ERR_PTR(ret);
  1144. is_bo_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_bo_proxy);
  1151. /*
  1152. * vmw_create_bo_proxy() adds a reference that is no longer
  1153. * needed
  1154. */
  1155. if (is_bo_proxy)
  1156. vmw_surface_unreference(&surface);
  1157. } else if (bo) {
  1158. ret = vmw_kms_new_framebuffer_bo(dev_priv, bo, &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_buffer_object *bo = NULL;
  1181. struct ttm_base_object *user_obj;
  1182. int ret;
  1183. /*
  1184. * Take a reference on the user object of the resource
  1185. * backing the kms fb. This ensures that user-space handle
  1186. * lookups on that resource will always work as long as
  1187. * it's registered with a kms framebuffer. This is important,
  1188. * since vmw_execbuf_process identifies resources in the
  1189. * command stream using user-space handles.
  1190. */
  1191. user_obj = ttm_base_object_lookup(tfile, mode_cmd->handles[0]);
  1192. if (unlikely(user_obj == NULL)) {
  1193. DRM_ERROR("Could not locate requested kms frame buffer.\n");
  1194. return ERR_PTR(-ENOENT);
  1195. }
  1196. /**
  1197. * End conditioned code.
  1198. */
  1199. /* returns either a bo or surface */
  1200. ret = vmw_user_lookup_handle(dev_priv, tfile,
  1201. mode_cmd->handles[0],
  1202. &surface, &bo);
  1203. if (ret)
  1204. goto err_out;
  1205. if (!bo &&
  1206. !vmw_kms_srf_ok(dev_priv, mode_cmd->width, mode_cmd->height)) {
  1207. DRM_ERROR("Surface size cannot exceed %dx%d",
  1208. dev_priv->texture_max_width,
  1209. dev_priv->texture_max_height);
  1210. goto err_out;
  1211. }
  1212. vfb = vmw_kms_new_framebuffer(dev_priv, bo, surface,
  1213. !(dev_priv->capabilities & SVGA_CAP_3D),
  1214. mode_cmd);
  1215. if (IS_ERR(vfb)) {
  1216. ret = PTR_ERR(vfb);
  1217. goto err_out;
  1218. }
  1219. err_out:
  1220. /* vmw_user_lookup_handle takes one ref so does new_fb */
  1221. if (bo)
  1222. vmw_bo_unreference(&bo);
  1223. if (surface)
  1224. vmw_surface_unreference(&surface);
  1225. if (ret) {
  1226. DRM_ERROR("failed to create vmw_framebuffer: %i\n", ret);
  1227. ttm_base_object_unref(&user_obj);
  1228. return ERR_PTR(ret);
  1229. } else
  1230. vfb->user_obj = user_obj;
  1231. return &vfb->base;
  1232. }
  1233. /**
  1234. * vmw_kms_check_display_memory - Validates display memory required for a
  1235. * topology
  1236. * @dev: DRM device
  1237. * @num_rects: number of drm_rect in rects
  1238. * @rects: array of drm_rect representing the topology to validate indexed by
  1239. * crtc index.
  1240. *
  1241. * Returns:
  1242. * 0 on success otherwise negative error code
  1243. */
  1244. static int vmw_kms_check_display_memory(struct drm_device *dev,
  1245. uint32_t num_rects,
  1246. struct drm_rect *rects)
  1247. {
  1248. struct vmw_private *dev_priv = vmw_priv(dev);
  1249. struct drm_rect bounding_box = {0};
  1250. u64 total_pixels = 0, pixel_mem, bb_mem;
  1251. int i;
  1252. for (i = 0; i < num_rects; i++) {
  1253. /*
  1254. * For STDU only individual screen (screen target) is limited by
  1255. * SCREENTARGET_MAX_WIDTH/HEIGHT registers.
  1256. */
  1257. if (dev_priv->active_display_unit == vmw_du_screen_target &&
  1258. (drm_rect_width(&rects[i]) > dev_priv->stdu_max_width ||
  1259. drm_rect_height(&rects[i]) > dev_priv->stdu_max_height)) {
  1260. DRM_ERROR("Screen size not supported.\n");
  1261. return -EINVAL;
  1262. }
  1263. /* Bounding box upper left is at (0,0). */
  1264. if (rects[i].x2 > bounding_box.x2)
  1265. bounding_box.x2 = rects[i].x2;
  1266. if (rects[i].y2 > bounding_box.y2)
  1267. bounding_box.y2 = rects[i].y2;
  1268. total_pixels += (u64) drm_rect_width(&rects[i]) *
  1269. (u64) drm_rect_height(&rects[i]);
  1270. }
  1271. /* Virtual svga device primary limits are always in 32-bpp. */
  1272. pixel_mem = total_pixels * 4;
  1273. /*
  1274. * For HV10 and below prim_bb_mem is vram size. When
  1275. * SVGA_REG_MAX_PRIMARY_BOUNDING_BOX_MEM is not present vram size is
  1276. * limit on primary bounding box
  1277. */
  1278. if (pixel_mem > dev_priv->prim_bb_mem) {
  1279. DRM_ERROR("Combined output size too large.\n");
  1280. return -EINVAL;
  1281. }
  1282. /* SVGA_CAP_NO_BB_RESTRICTION is available for STDU only. */
  1283. if (dev_priv->active_display_unit != vmw_du_screen_target ||
  1284. !(dev_priv->capabilities & SVGA_CAP_NO_BB_RESTRICTION)) {
  1285. bb_mem = (u64) bounding_box.x2 * bounding_box.y2 * 4;
  1286. if (bb_mem > dev_priv->prim_bb_mem) {
  1287. DRM_ERROR("Topology is beyond supported limits.\n");
  1288. return -EINVAL;
  1289. }
  1290. }
  1291. return 0;
  1292. }
  1293. /**
  1294. * vmw_kms_check_topology - Validates topology in drm_atomic_state
  1295. * @dev: DRM device
  1296. * @state: the driver state object
  1297. *
  1298. * Returns:
  1299. * 0 on success otherwise negative error code
  1300. */
  1301. static int vmw_kms_check_topology(struct drm_device *dev,
  1302. struct drm_atomic_state *state)
  1303. {
  1304. struct vmw_private *dev_priv = vmw_priv(dev);
  1305. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  1306. struct drm_rect *rects;
  1307. struct drm_crtc *crtc;
  1308. uint32_t i;
  1309. int ret = 0;
  1310. rects = kcalloc(dev->mode_config.num_crtc, sizeof(struct drm_rect),
  1311. GFP_KERNEL);
  1312. if (!rects)
  1313. return -ENOMEM;
  1314. mutex_lock(&dev_priv->requested_layout_mutex);
  1315. drm_for_each_crtc(crtc, dev) {
  1316. struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
  1317. struct drm_crtc_state *crtc_state = crtc->state;
  1318. i = drm_crtc_index(crtc);
  1319. if (crtc_state && crtc_state->enable) {
  1320. rects[i].x1 = du->gui_x;
  1321. rects[i].y1 = du->gui_y;
  1322. rects[i].x2 = du->gui_x + crtc_state->mode.hdisplay;
  1323. rects[i].y2 = du->gui_y + crtc_state->mode.vdisplay;
  1324. }
  1325. }
  1326. /* Determine change to topology due to new atomic state */
  1327. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state,
  1328. new_crtc_state, i) {
  1329. struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
  1330. struct drm_connector *connector;
  1331. struct drm_connector_state *conn_state;
  1332. struct vmw_connector_state *vmw_conn_state;
  1333. if (!new_crtc_state->enable) {
  1334. rects[i].x1 = 0;
  1335. rects[i].y1 = 0;
  1336. rects[i].x2 = 0;
  1337. rects[i].y2 = 0;
  1338. continue;
  1339. }
  1340. if (!du->pref_active) {
  1341. ret = -EINVAL;
  1342. goto clean;
  1343. }
  1344. /*
  1345. * For vmwgfx each crtc has only one connector attached and it
  1346. * is not changed so don't really need to check the
  1347. * crtc->connector_mask and iterate over it.
  1348. */
  1349. connector = &du->connector;
  1350. conn_state = drm_atomic_get_connector_state(state, connector);
  1351. if (IS_ERR(conn_state)) {
  1352. ret = PTR_ERR(conn_state);
  1353. goto clean;
  1354. }
  1355. vmw_conn_state = vmw_connector_state_to_vcs(conn_state);
  1356. vmw_conn_state->gui_x = du->gui_x;
  1357. vmw_conn_state->gui_y = du->gui_y;
  1358. rects[i].x1 = du->gui_x;
  1359. rects[i].y1 = du->gui_y;
  1360. rects[i].x2 = du->gui_x + new_crtc_state->mode.hdisplay;
  1361. rects[i].y2 = du->gui_y + new_crtc_state->mode.vdisplay;
  1362. }
  1363. ret = vmw_kms_check_display_memory(dev, dev->mode_config.num_crtc,
  1364. rects);
  1365. clean:
  1366. mutex_unlock(&dev_priv->requested_layout_mutex);
  1367. kfree(rects);
  1368. return ret;
  1369. }
  1370. /**
  1371. * vmw_kms_atomic_check_modeset- validate state object for modeset changes
  1372. *
  1373. * @dev: DRM device
  1374. * @state: the driver state object
  1375. *
  1376. * This is a simple wrapper around drm_atomic_helper_check_modeset() for
  1377. * us to assign a value to mode->crtc_clock so that
  1378. * drm_calc_timestamping_constants() won't throw an error message
  1379. *
  1380. * Returns:
  1381. * Zero for success or -errno
  1382. */
  1383. static int
  1384. vmw_kms_atomic_check_modeset(struct drm_device *dev,
  1385. struct drm_atomic_state *state)
  1386. {
  1387. struct drm_crtc *crtc;
  1388. struct drm_crtc_state *crtc_state;
  1389. bool need_modeset = false;
  1390. int i, ret;
  1391. ret = drm_atomic_helper_check(dev, state);
  1392. if (ret)
  1393. return ret;
  1394. if (!state->allow_modeset)
  1395. return ret;
  1396. /*
  1397. * Legacy path do not set allow_modeset properly like
  1398. * @drm_atomic_helper_update_plane, This will result in unnecessary call
  1399. * to vmw_kms_check_topology. So extra set of check.
  1400. */
  1401. for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
  1402. if (drm_atomic_crtc_needs_modeset(crtc_state))
  1403. need_modeset = true;
  1404. }
  1405. if (need_modeset)
  1406. return vmw_kms_check_topology(dev, state);
  1407. return ret;
  1408. }
  1409. static const struct drm_mode_config_funcs vmw_kms_funcs = {
  1410. .fb_create = vmw_kms_fb_create,
  1411. .atomic_check = vmw_kms_atomic_check_modeset,
  1412. .atomic_commit = drm_atomic_helper_commit,
  1413. };
  1414. static int vmw_kms_generic_present(struct vmw_private *dev_priv,
  1415. struct drm_file *file_priv,
  1416. struct vmw_framebuffer *vfb,
  1417. struct vmw_surface *surface,
  1418. uint32_t sid,
  1419. int32_t destX, int32_t destY,
  1420. struct drm_vmw_rect *clips,
  1421. uint32_t num_clips)
  1422. {
  1423. return vmw_kms_sou_do_surface_dirty(dev_priv, vfb, NULL, clips,
  1424. &surface->res, destX, destY,
  1425. num_clips, 1, NULL, NULL);
  1426. }
  1427. int vmw_kms_present(struct vmw_private *dev_priv,
  1428. struct drm_file *file_priv,
  1429. struct vmw_framebuffer *vfb,
  1430. struct vmw_surface *surface,
  1431. uint32_t sid,
  1432. int32_t destX, int32_t destY,
  1433. struct drm_vmw_rect *clips,
  1434. uint32_t num_clips)
  1435. {
  1436. int ret;
  1437. switch (dev_priv->active_display_unit) {
  1438. case vmw_du_screen_target:
  1439. ret = vmw_kms_stdu_surface_dirty(dev_priv, vfb, NULL, clips,
  1440. &surface->res, destX, destY,
  1441. num_clips, 1, NULL, NULL);
  1442. break;
  1443. case vmw_du_screen_object:
  1444. ret = vmw_kms_generic_present(dev_priv, file_priv, vfb, surface,
  1445. sid, destX, destY, clips,
  1446. num_clips);
  1447. break;
  1448. default:
  1449. WARN_ONCE(true,
  1450. "Present called with invalid display system.\n");
  1451. ret = -ENOSYS;
  1452. break;
  1453. }
  1454. if (ret)
  1455. return ret;
  1456. vmw_fifo_flush(dev_priv, false);
  1457. return 0;
  1458. }
  1459. static void
  1460. vmw_kms_create_hotplug_mode_update_property(struct vmw_private *dev_priv)
  1461. {
  1462. if (dev_priv->hotplug_mode_update_property)
  1463. return;
  1464. dev_priv->hotplug_mode_update_property =
  1465. drm_property_create_range(dev_priv->dev,
  1466. DRM_MODE_PROP_IMMUTABLE,
  1467. "hotplug_mode_update", 0, 1);
  1468. if (!dev_priv->hotplug_mode_update_property)
  1469. return;
  1470. }
  1471. int vmw_kms_init(struct vmw_private *dev_priv)
  1472. {
  1473. struct drm_device *dev = dev_priv->dev;
  1474. int ret;
  1475. drm_mode_config_init(dev);
  1476. dev->mode_config.funcs = &vmw_kms_funcs;
  1477. dev->mode_config.min_width = 1;
  1478. dev->mode_config.min_height = 1;
  1479. dev->mode_config.max_width = dev_priv->texture_max_width;
  1480. dev->mode_config.max_height = dev_priv->texture_max_height;
  1481. drm_mode_create_suggested_offset_properties(dev);
  1482. vmw_kms_create_hotplug_mode_update_property(dev_priv);
  1483. ret = vmw_kms_stdu_init_display(dev_priv);
  1484. if (ret) {
  1485. ret = vmw_kms_sou_init_display(dev_priv);
  1486. if (ret) /* Fallback */
  1487. ret = vmw_kms_ldu_init_display(dev_priv);
  1488. }
  1489. return ret;
  1490. }
  1491. int vmw_kms_close(struct vmw_private *dev_priv)
  1492. {
  1493. int ret = 0;
  1494. /*
  1495. * Docs says we should take the lock before calling this function
  1496. * but since it destroys encoders and our destructor calls
  1497. * drm_encoder_cleanup which takes the lock we deadlock.
  1498. */
  1499. drm_mode_config_cleanup(dev_priv->dev);
  1500. if (dev_priv->active_display_unit == vmw_du_legacy)
  1501. ret = vmw_kms_ldu_close_display(dev_priv);
  1502. return ret;
  1503. }
  1504. int vmw_kms_cursor_bypass_ioctl(struct drm_device *dev, void *data,
  1505. struct drm_file *file_priv)
  1506. {
  1507. struct drm_vmw_cursor_bypass_arg *arg = data;
  1508. struct vmw_display_unit *du;
  1509. struct drm_crtc *crtc;
  1510. int ret = 0;
  1511. mutex_lock(&dev->mode_config.mutex);
  1512. if (arg->flags & DRM_VMW_CURSOR_BYPASS_ALL) {
  1513. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  1514. du = vmw_crtc_to_du(crtc);
  1515. du->hotspot_x = arg->xhot;
  1516. du->hotspot_y = arg->yhot;
  1517. }
  1518. mutex_unlock(&dev->mode_config.mutex);
  1519. return 0;
  1520. }
  1521. crtc = drm_crtc_find(dev, file_priv, arg->crtc_id);
  1522. if (!crtc) {
  1523. ret = -ENOENT;
  1524. goto out;
  1525. }
  1526. du = vmw_crtc_to_du(crtc);
  1527. du->hotspot_x = arg->xhot;
  1528. du->hotspot_y = arg->yhot;
  1529. out:
  1530. mutex_unlock(&dev->mode_config.mutex);
  1531. return ret;
  1532. }
  1533. int vmw_kms_write_svga(struct vmw_private *vmw_priv,
  1534. unsigned width, unsigned height, unsigned pitch,
  1535. unsigned bpp, unsigned depth)
  1536. {
  1537. if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
  1538. vmw_write(vmw_priv, SVGA_REG_PITCHLOCK, pitch);
  1539. else if (vmw_fifo_have_pitchlock(vmw_priv))
  1540. vmw_mmio_write(pitch, vmw_priv->mmio_virt +
  1541. SVGA_FIFO_PITCHLOCK);
  1542. vmw_write(vmw_priv, SVGA_REG_WIDTH, width);
  1543. vmw_write(vmw_priv, SVGA_REG_HEIGHT, height);
  1544. vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, bpp);
  1545. if (vmw_read(vmw_priv, SVGA_REG_DEPTH) != depth) {
  1546. DRM_ERROR("Invalid depth %u for %u bpp, host expects %u\n",
  1547. depth, bpp, vmw_read(vmw_priv, SVGA_REG_DEPTH));
  1548. return -EINVAL;
  1549. }
  1550. return 0;
  1551. }
  1552. int vmw_kms_save_vga(struct vmw_private *vmw_priv)
  1553. {
  1554. struct vmw_vga_topology_state *save;
  1555. uint32_t i;
  1556. vmw_priv->vga_width = vmw_read(vmw_priv, SVGA_REG_WIDTH);
  1557. vmw_priv->vga_height = vmw_read(vmw_priv, SVGA_REG_HEIGHT);
  1558. vmw_priv->vga_bpp = vmw_read(vmw_priv, SVGA_REG_BITS_PER_PIXEL);
  1559. if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
  1560. vmw_priv->vga_pitchlock =
  1561. vmw_read(vmw_priv, SVGA_REG_PITCHLOCK);
  1562. else if (vmw_fifo_have_pitchlock(vmw_priv))
  1563. vmw_priv->vga_pitchlock = vmw_mmio_read(vmw_priv->mmio_virt +
  1564. SVGA_FIFO_PITCHLOCK);
  1565. if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
  1566. return 0;
  1567. vmw_priv->num_displays = vmw_read(vmw_priv,
  1568. SVGA_REG_NUM_GUEST_DISPLAYS);
  1569. if (vmw_priv->num_displays == 0)
  1570. vmw_priv->num_displays = 1;
  1571. for (i = 0; i < vmw_priv->num_displays; ++i) {
  1572. save = &vmw_priv->vga_save[i];
  1573. vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
  1574. save->primary = vmw_read(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY);
  1575. save->pos_x = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_X);
  1576. save->pos_y = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y);
  1577. save->width = vmw_read(vmw_priv, SVGA_REG_DISPLAY_WIDTH);
  1578. save->height = vmw_read(vmw_priv, SVGA_REG_DISPLAY_HEIGHT);
  1579. vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
  1580. if (i == 0 && vmw_priv->num_displays == 1 &&
  1581. save->width == 0 && save->height == 0) {
  1582. /*
  1583. * It should be fairly safe to assume that these
  1584. * values are uninitialized.
  1585. */
  1586. save->width = vmw_priv->vga_width - save->pos_x;
  1587. save->height = vmw_priv->vga_height - save->pos_y;
  1588. }
  1589. }
  1590. return 0;
  1591. }
  1592. int vmw_kms_restore_vga(struct vmw_private *vmw_priv)
  1593. {
  1594. struct vmw_vga_topology_state *save;
  1595. uint32_t i;
  1596. vmw_write(vmw_priv, SVGA_REG_WIDTH, vmw_priv->vga_width);
  1597. vmw_write(vmw_priv, SVGA_REG_HEIGHT, vmw_priv->vga_height);
  1598. vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, vmw_priv->vga_bpp);
  1599. if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
  1600. vmw_write(vmw_priv, SVGA_REG_PITCHLOCK,
  1601. vmw_priv->vga_pitchlock);
  1602. else if (vmw_fifo_have_pitchlock(vmw_priv))
  1603. vmw_mmio_write(vmw_priv->vga_pitchlock,
  1604. vmw_priv->mmio_virt + SVGA_FIFO_PITCHLOCK);
  1605. if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
  1606. return 0;
  1607. for (i = 0; i < vmw_priv->num_displays; ++i) {
  1608. save = &vmw_priv->vga_save[i];
  1609. vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
  1610. vmw_write(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY, save->primary);
  1611. vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_X, save->pos_x);
  1612. vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y, save->pos_y);
  1613. vmw_write(vmw_priv, SVGA_REG_DISPLAY_WIDTH, save->width);
  1614. vmw_write(vmw_priv, SVGA_REG_DISPLAY_HEIGHT, save->height);
  1615. vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
  1616. }
  1617. return 0;
  1618. }
  1619. bool vmw_kms_validate_mode_vram(struct vmw_private *dev_priv,
  1620. uint32_t pitch,
  1621. uint32_t height)
  1622. {
  1623. return ((u64) pitch * (u64) height) < (u64)
  1624. ((dev_priv->active_display_unit == vmw_du_screen_target) ?
  1625. dev_priv->prim_bb_mem : dev_priv->vram_size);
  1626. }
  1627. /**
  1628. * Function called by DRM code called with vbl_lock held.
  1629. */
  1630. u32 vmw_get_vblank_counter(struct drm_device *dev, unsigned int pipe)
  1631. {
  1632. return 0;
  1633. }
  1634. /**
  1635. * Function called by DRM code called with vbl_lock held.
  1636. */
  1637. int vmw_enable_vblank(struct drm_device *dev, unsigned int pipe)
  1638. {
  1639. return -EINVAL;
  1640. }
  1641. /**
  1642. * Function called by DRM code called with vbl_lock held.
  1643. */
  1644. void vmw_disable_vblank(struct drm_device *dev, unsigned int pipe)
  1645. {
  1646. }
  1647. /**
  1648. * vmw_du_update_layout - Update the display unit with topology from resolution
  1649. * plugin and generate DRM uevent
  1650. * @dev_priv: device private
  1651. * @num_rects: number of drm_rect in rects
  1652. * @rects: toplogy to update
  1653. */
  1654. static int vmw_du_update_layout(struct vmw_private *dev_priv,
  1655. unsigned int num_rects, struct drm_rect *rects)
  1656. {
  1657. struct drm_device *dev = dev_priv->dev;
  1658. struct vmw_display_unit *du;
  1659. struct drm_connector *con;
  1660. struct drm_connector_list_iter conn_iter;
  1661. /*
  1662. * Currently only gui_x/y is protected with requested_layout_mutex.
  1663. */
  1664. mutex_lock(&dev_priv->requested_layout_mutex);
  1665. drm_connector_list_iter_begin(dev, &conn_iter);
  1666. drm_for_each_connector_iter(con, &conn_iter) {
  1667. du = vmw_connector_to_du(con);
  1668. if (num_rects > du->unit) {
  1669. du->pref_width = drm_rect_width(&rects[du->unit]);
  1670. du->pref_height = drm_rect_height(&rects[du->unit]);
  1671. du->pref_active = true;
  1672. du->gui_x = rects[du->unit].x1;
  1673. du->gui_y = rects[du->unit].y1;
  1674. } else {
  1675. du->pref_width = 800;
  1676. du->pref_height = 600;
  1677. du->pref_active = false;
  1678. du->gui_x = 0;
  1679. du->gui_y = 0;
  1680. }
  1681. }
  1682. drm_connector_list_iter_end(&conn_iter);
  1683. mutex_unlock(&dev_priv->requested_layout_mutex);
  1684. mutex_lock(&dev->mode_config.mutex);
  1685. list_for_each_entry(con, &dev->mode_config.connector_list, head) {
  1686. du = vmw_connector_to_du(con);
  1687. if (num_rects > du->unit) {
  1688. drm_object_property_set_value
  1689. (&con->base, dev->mode_config.suggested_x_property,
  1690. du->gui_x);
  1691. drm_object_property_set_value
  1692. (&con->base, dev->mode_config.suggested_y_property,
  1693. du->gui_y);
  1694. } else {
  1695. drm_object_property_set_value
  1696. (&con->base, dev->mode_config.suggested_x_property,
  1697. 0);
  1698. drm_object_property_set_value
  1699. (&con->base, dev->mode_config.suggested_y_property,
  1700. 0);
  1701. }
  1702. con->status = vmw_du_connector_detect(con, true);
  1703. }
  1704. mutex_unlock(&dev->mode_config.mutex);
  1705. drm_sysfs_hotplug_event(dev);
  1706. return 0;
  1707. }
  1708. int vmw_du_crtc_gamma_set(struct drm_crtc *crtc,
  1709. u16 *r, u16 *g, u16 *b,
  1710. uint32_t size,
  1711. struct drm_modeset_acquire_ctx *ctx)
  1712. {
  1713. struct vmw_private *dev_priv = vmw_priv(crtc->dev);
  1714. int i;
  1715. for (i = 0; i < size; i++) {
  1716. DRM_DEBUG("%d r/g/b = 0x%04x / 0x%04x / 0x%04x\n", i,
  1717. r[i], g[i], b[i]);
  1718. vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 0, r[i] >> 8);
  1719. vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 1, g[i] >> 8);
  1720. vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 2, b[i] >> 8);
  1721. }
  1722. return 0;
  1723. }
  1724. int vmw_du_connector_dpms(struct drm_connector *connector, int mode)
  1725. {
  1726. return 0;
  1727. }
  1728. enum drm_connector_status
  1729. vmw_du_connector_detect(struct drm_connector *connector, bool force)
  1730. {
  1731. uint32_t num_displays;
  1732. struct drm_device *dev = connector->dev;
  1733. struct vmw_private *dev_priv = vmw_priv(dev);
  1734. struct vmw_display_unit *du = vmw_connector_to_du(connector);
  1735. num_displays = vmw_read(dev_priv, SVGA_REG_NUM_DISPLAYS);
  1736. return ((vmw_connector_to_du(connector)->unit < num_displays &&
  1737. du->pref_active) ?
  1738. connector_status_connected : connector_status_disconnected);
  1739. }
  1740. static struct drm_display_mode vmw_kms_connector_builtin[] = {
  1741. /* 640x480@60Hz */
  1742. { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 25175, 640, 656,
  1743. 752, 800, 0, 480, 489, 492, 525, 0,
  1744. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
  1745. /* 800x600@60Hz */
  1746. { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 40000, 800, 840,
  1747. 968, 1056, 0, 600, 601, 605, 628, 0,
  1748. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1749. /* 1024x768@60Hz */
  1750. { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 65000, 1024, 1048,
  1751. 1184, 1344, 0, 768, 771, 777, 806, 0,
  1752. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
  1753. /* 1152x864@75Hz */
  1754. { DRM_MODE("1152x864", DRM_MODE_TYPE_DRIVER, 108000, 1152, 1216,
  1755. 1344, 1600, 0, 864, 865, 868, 900, 0,
  1756. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1757. /* 1280x768@60Hz */
  1758. { DRM_MODE("1280x768", DRM_MODE_TYPE_DRIVER, 79500, 1280, 1344,
  1759. 1472, 1664, 0, 768, 771, 778, 798, 0,
  1760. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1761. /* 1280x800@60Hz */
  1762. { DRM_MODE("1280x800", DRM_MODE_TYPE_DRIVER, 83500, 1280, 1352,
  1763. 1480, 1680, 0, 800, 803, 809, 831, 0,
  1764. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
  1765. /* 1280x960@60Hz */
  1766. { DRM_MODE("1280x960", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1376,
  1767. 1488, 1800, 0, 960, 961, 964, 1000, 0,
  1768. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1769. /* 1280x1024@60Hz */
  1770. { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1328,
  1771. 1440, 1688, 0, 1024, 1025, 1028, 1066, 0,
  1772. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1773. /* 1360x768@60Hz */
  1774. { DRM_MODE("1360x768", DRM_MODE_TYPE_DRIVER, 85500, 1360, 1424,
  1775. 1536, 1792, 0, 768, 771, 777, 795, 0,
  1776. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1777. /* 1440x1050@60Hz */
  1778. { DRM_MODE("1400x1050", DRM_MODE_TYPE_DRIVER, 121750, 1400, 1488,
  1779. 1632, 1864, 0, 1050, 1053, 1057, 1089, 0,
  1780. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1781. /* 1440x900@60Hz */
  1782. { DRM_MODE("1440x900", DRM_MODE_TYPE_DRIVER, 106500, 1440, 1520,
  1783. 1672, 1904, 0, 900, 903, 909, 934, 0,
  1784. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1785. /* 1600x1200@60Hz */
  1786. { DRM_MODE("1600x1200", DRM_MODE_TYPE_DRIVER, 162000, 1600, 1664,
  1787. 1856, 2160, 0, 1200, 1201, 1204, 1250, 0,
  1788. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1789. /* 1680x1050@60Hz */
  1790. { DRM_MODE("1680x1050", DRM_MODE_TYPE_DRIVER, 146250, 1680, 1784,
  1791. 1960, 2240, 0, 1050, 1053, 1059, 1089, 0,
  1792. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1793. /* 1792x1344@60Hz */
  1794. { DRM_MODE("1792x1344", DRM_MODE_TYPE_DRIVER, 204750, 1792, 1920,
  1795. 2120, 2448, 0, 1344, 1345, 1348, 1394, 0,
  1796. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1797. /* 1853x1392@60Hz */
  1798. { DRM_MODE("1856x1392", DRM_MODE_TYPE_DRIVER, 218250, 1856, 1952,
  1799. 2176, 2528, 0, 1392, 1393, 1396, 1439, 0,
  1800. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1801. /* 1920x1200@60Hz */
  1802. { DRM_MODE("1920x1200", DRM_MODE_TYPE_DRIVER, 193250, 1920, 2056,
  1803. 2256, 2592, 0, 1200, 1203, 1209, 1245, 0,
  1804. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1805. /* 1920x1440@60Hz */
  1806. { DRM_MODE("1920x1440", DRM_MODE_TYPE_DRIVER, 234000, 1920, 2048,
  1807. 2256, 2600, 0, 1440, 1441, 1444, 1500, 0,
  1808. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1809. /* 2560x1600@60Hz */
  1810. { DRM_MODE("2560x1600", DRM_MODE_TYPE_DRIVER, 348500, 2560, 2752,
  1811. 3032, 3504, 0, 1600, 1603, 1609, 1658, 0,
  1812. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1813. /* Terminate */
  1814. { DRM_MODE("", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) },
  1815. };
  1816. /**
  1817. * vmw_guess_mode_timing - Provide fake timings for a
  1818. * 60Hz vrefresh mode.
  1819. *
  1820. * @mode - Pointer to a struct drm_display_mode with hdisplay and vdisplay
  1821. * members filled in.
  1822. */
  1823. void vmw_guess_mode_timing(struct drm_display_mode *mode)
  1824. {
  1825. mode->hsync_start = mode->hdisplay + 50;
  1826. mode->hsync_end = mode->hsync_start + 50;
  1827. mode->htotal = mode->hsync_end + 50;
  1828. mode->vsync_start = mode->vdisplay + 50;
  1829. mode->vsync_end = mode->vsync_start + 50;
  1830. mode->vtotal = mode->vsync_end + 50;
  1831. mode->clock = (u32)mode->htotal * (u32)mode->vtotal / 100 * 6;
  1832. mode->vrefresh = drm_mode_vrefresh(mode);
  1833. }
  1834. int vmw_du_connector_fill_modes(struct drm_connector *connector,
  1835. uint32_t max_width, uint32_t max_height)
  1836. {
  1837. struct vmw_display_unit *du = vmw_connector_to_du(connector);
  1838. struct drm_device *dev = connector->dev;
  1839. struct vmw_private *dev_priv = vmw_priv(dev);
  1840. struct drm_display_mode *mode = NULL;
  1841. struct drm_display_mode *bmode;
  1842. struct drm_display_mode prefmode = { DRM_MODE("preferred",
  1843. DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED,
  1844. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1845. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC)
  1846. };
  1847. int i;
  1848. u32 assumed_bpp = 4;
  1849. if (dev_priv->assume_16bpp)
  1850. assumed_bpp = 2;
  1851. max_width = min(max_width, dev_priv->texture_max_width);
  1852. max_height = min(max_height, dev_priv->texture_max_height);
  1853. /*
  1854. * For STDU extra limit for a mode on SVGA_REG_SCREENTARGET_MAX_WIDTH/
  1855. * HEIGHT registers.
  1856. */
  1857. if (dev_priv->active_display_unit == vmw_du_screen_target) {
  1858. max_width = min(max_width, dev_priv->stdu_max_width);
  1859. max_height = min(max_height, dev_priv->stdu_max_height);
  1860. }
  1861. /* Add preferred mode */
  1862. mode = drm_mode_duplicate(dev, &prefmode);
  1863. if (!mode)
  1864. return 0;
  1865. mode->hdisplay = du->pref_width;
  1866. mode->vdisplay = du->pref_height;
  1867. vmw_guess_mode_timing(mode);
  1868. if (vmw_kms_validate_mode_vram(dev_priv,
  1869. mode->hdisplay * assumed_bpp,
  1870. mode->vdisplay)) {
  1871. drm_mode_probed_add(connector, mode);
  1872. } else {
  1873. drm_mode_destroy(dev, mode);
  1874. mode = NULL;
  1875. }
  1876. if (du->pref_mode) {
  1877. list_del_init(&du->pref_mode->head);
  1878. drm_mode_destroy(dev, du->pref_mode);
  1879. }
  1880. /* mode might be null here, this is intended */
  1881. du->pref_mode = mode;
  1882. for (i = 0; vmw_kms_connector_builtin[i].type != 0; i++) {
  1883. bmode = &vmw_kms_connector_builtin[i];
  1884. if (bmode->hdisplay > max_width ||
  1885. bmode->vdisplay > max_height)
  1886. continue;
  1887. if (!vmw_kms_validate_mode_vram(dev_priv,
  1888. bmode->hdisplay * assumed_bpp,
  1889. bmode->vdisplay))
  1890. continue;
  1891. mode = drm_mode_duplicate(dev, bmode);
  1892. if (!mode)
  1893. return 0;
  1894. mode->vrefresh = drm_mode_vrefresh(mode);
  1895. drm_mode_probed_add(connector, mode);
  1896. }
  1897. drm_connector_list_update(connector);
  1898. /* Move the prefered mode first, help apps pick the right mode. */
  1899. drm_mode_sort(&connector->modes);
  1900. return 1;
  1901. }
  1902. int vmw_du_connector_set_property(struct drm_connector *connector,
  1903. struct drm_property *property,
  1904. uint64_t val)
  1905. {
  1906. struct vmw_display_unit *du = vmw_connector_to_du(connector);
  1907. struct vmw_private *dev_priv = vmw_priv(connector->dev);
  1908. if (property == dev_priv->implicit_placement_property)
  1909. du->is_implicit = val;
  1910. return 0;
  1911. }
  1912. /**
  1913. * vmw_du_connector_atomic_set_property - Atomic version of get property
  1914. *
  1915. * @crtc - crtc the property is associated with
  1916. *
  1917. * Returns:
  1918. * Zero on success, negative errno on failure.
  1919. */
  1920. int
  1921. vmw_du_connector_atomic_set_property(struct drm_connector *connector,
  1922. struct drm_connector_state *state,
  1923. struct drm_property *property,
  1924. uint64_t val)
  1925. {
  1926. struct vmw_private *dev_priv = vmw_priv(connector->dev);
  1927. struct vmw_connector_state *vcs = vmw_connector_state_to_vcs(state);
  1928. struct vmw_display_unit *du = vmw_connector_to_du(connector);
  1929. if (property == dev_priv->implicit_placement_property) {
  1930. vcs->is_implicit = val;
  1931. /*
  1932. * We should really be doing a drm_atomic_commit() to
  1933. * commit the new state, but since this doesn't cause
  1934. * an immedate state change, this is probably ok
  1935. */
  1936. du->is_implicit = vcs->is_implicit;
  1937. } else {
  1938. return -EINVAL;
  1939. }
  1940. return 0;
  1941. }
  1942. /**
  1943. * vmw_du_connector_atomic_get_property - Atomic version of get property
  1944. *
  1945. * @connector - connector the property is associated with
  1946. *
  1947. * Returns:
  1948. * Zero on success, negative errno on failure.
  1949. */
  1950. int
  1951. vmw_du_connector_atomic_get_property(struct drm_connector *connector,
  1952. const struct drm_connector_state *state,
  1953. struct drm_property *property,
  1954. uint64_t *val)
  1955. {
  1956. struct vmw_private *dev_priv = vmw_priv(connector->dev);
  1957. struct vmw_connector_state *vcs = vmw_connector_state_to_vcs(state);
  1958. if (property == dev_priv->implicit_placement_property)
  1959. *val = vcs->is_implicit;
  1960. else {
  1961. DRM_ERROR("Invalid Property %s\n", property->name);
  1962. return -EINVAL;
  1963. }
  1964. return 0;
  1965. }
  1966. /**
  1967. * vmw_kms_update_layout_ioctl - Handler for DRM_VMW_UPDATE_LAYOUT ioctl
  1968. * @dev: drm device for the ioctl
  1969. * @data: data pointer for the ioctl
  1970. * @file_priv: drm file for the ioctl call
  1971. *
  1972. * Update preferred topology of display unit as per ioctl request. The topology
  1973. * is expressed as array of drm_vmw_rect.
  1974. * e.g.
  1975. * [0 0 640 480] [640 0 800 600] [0 480 640 480]
  1976. *
  1977. * NOTE:
  1978. * The x and y offset (upper left) in drm_vmw_rect cannot be less than 0. Beside
  1979. * device limit on topology, x + w and y + h (lower right) cannot be greater
  1980. * than INT_MAX. So topology beyond these limits will return with error.
  1981. *
  1982. * Returns:
  1983. * Zero on success, negative errno on failure.
  1984. */
  1985. int vmw_kms_update_layout_ioctl(struct drm_device *dev, void *data,
  1986. struct drm_file *file_priv)
  1987. {
  1988. struct vmw_private *dev_priv = vmw_priv(dev);
  1989. struct drm_mode_config *mode_config = &dev->mode_config;
  1990. struct drm_vmw_update_layout_arg *arg =
  1991. (struct drm_vmw_update_layout_arg *)data;
  1992. void __user *user_rects;
  1993. struct drm_vmw_rect *rects;
  1994. struct drm_rect *drm_rects;
  1995. unsigned rects_size;
  1996. int ret, i;
  1997. if (!arg->num_outputs) {
  1998. struct drm_rect def_rect = {0, 0, 800, 600};
  1999. vmw_du_update_layout(dev_priv, 1, &def_rect);
  2000. return 0;
  2001. }
  2002. rects_size = arg->num_outputs * sizeof(struct drm_vmw_rect);
  2003. rects = kcalloc(arg->num_outputs, sizeof(struct drm_vmw_rect),
  2004. GFP_KERNEL);
  2005. if (unlikely(!rects))
  2006. return -ENOMEM;
  2007. user_rects = (void __user *)(unsigned long)arg->rects;
  2008. ret = copy_from_user(rects, user_rects, rects_size);
  2009. if (unlikely(ret != 0)) {
  2010. DRM_ERROR("Failed to get rects.\n");
  2011. ret = -EFAULT;
  2012. goto out_free;
  2013. }
  2014. drm_rects = (struct drm_rect *)rects;
  2015. for (i = 0; i < arg->num_outputs; i++) {
  2016. struct drm_vmw_rect curr_rect;
  2017. /* Verify user-space for overflow as kernel use drm_rect */
  2018. if ((rects[i].x + rects[i].w > INT_MAX) ||
  2019. (rects[i].y + rects[i].h > INT_MAX)) {
  2020. ret = -ERANGE;
  2021. goto out_free;
  2022. }
  2023. curr_rect = rects[i];
  2024. drm_rects[i].x1 = curr_rect.x;
  2025. drm_rects[i].y1 = curr_rect.y;
  2026. drm_rects[i].x2 = curr_rect.x + curr_rect.w;
  2027. drm_rects[i].y2 = curr_rect.y + curr_rect.h;
  2028. /*
  2029. * Currently this check is limiting the topology within
  2030. * mode_config->max (which actually is max texture size
  2031. * supported by virtual device). This limit is here to address
  2032. * window managers that create a big framebuffer for whole
  2033. * topology.
  2034. */
  2035. if (drm_rects[i].x1 < 0 || drm_rects[i].y1 < 0 ||
  2036. drm_rects[i].x2 > mode_config->max_width ||
  2037. drm_rects[i].y2 > mode_config->max_height) {
  2038. DRM_ERROR("Invalid GUI layout.\n");
  2039. ret = -EINVAL;
  2040. goto out_free;
  2041. }
  2042. }
  2043. ret = vmw_kms_check_display_memory(dev, arg->num_outputs, drm_rects);
  2044. if (ret == 0)
  2045. vmw_du_update_layout(dev_priv, arg->num_outputs, drm_rects);
  2046. out_free:
  2047. kfree(rects);
  2048. return ret;
  2049. }
  2050. /**
  2051. * vmw_kms_helper_dirty - Helper to build commands and perform actions based
  2052. * on a set of cliprects and a set of display units.
  2053. *
  2054. * @dev_priv: Pointer to a device private structure.
  2055. * @framebuffer: Pointer to the framebuffer on which to perform the actions.
  2056. * @clips: A set of struct drm_clip_rect. Either this os @vclips must be NULL.
  2057. * Cliprects are given in framebuffer coordinates.
  2058. * @vclips: A set of struct drm_vmw_rect cliprects. Either this or @clips must
  2059. * be NULL. Cliprects are given in source coordinates.
  2060. * @dest_x: X coordinate offset for the crtc / destination clip rects.
  2061. * @dest_y: Y coordinate offset for the crtc / destination clip rects.
  2062. * @num_clips: Number of cliprects in the @clips or @vclips array.
  2063. * @increment: Integer with which to increment the clip counter when looping.
  2064. * Used to skip a predetermined number of clip rects.
  2065. * @dirty: Closure structure. See the description of struct vmw_kms_dirty.
  2066. */
  2067. int vmw_kms_helper_dirty(struct vmw_private *dev_priv,
  2068. struct vmw_framebuffer *framebuffer,
  2069. const struct drm_clip_rect *clips,
  2070. const struct drm_vmw_rect *vclips,
  2071. s32 dest_x, s32 dest_y,
  2072. int num_clips,
  2073. int increment,
  2074. struct vmw_kms_dirty *dirty)
  2075. {
  2076. struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
  2077. struct drm_crtc *crtc;
  2078. u32 num_units = 0;
  2079. u32 i, k;
  2080. dirty->dev_priv = dev_priv;
  2081. /* If crtc is passed, no need to iterate over other display units */
  2082. if (dirty->crtc) {
  2083. units[num_units++] = vmw_crtc_to_du(dirty->crtc);
  2084. } else {
  2085. list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list,
  2086. head) {
  2087. struct drm_plane *plane = crtc->primary;
  2088. if (plane->state->fb == &framebuffer->base)
  2089. units[num_units++] = vmw_crtc_to_du(crtc);
  2090. }
  2091. }
  2092. for (k = 0; k < num_units; k++) {
  2093. struct vmw_display_unit *unit = units[k];
  2094. s32 crtc_x = unit->crtc.x;
  2095. s32 crtc_y = unit->crtc.y;
  2096. s32 crtc_width = unit->crtc.mode.hdisplay;
  2097. s32 crtc_height = unit->crtc.mode.vdisplay;
  2098. const struct drm_clip_rect *clips_ptr = clips;
  2099. const struct drm_vmw_rect *vclips_ptr = vclips;
  2100. dirty->unit = unit;
  2101. if (dirty->fifo_reserve_size > 0) {
  2102. dirty->cmd = vmw_fifo_reserve(dev_priv,
  2103. dirty->fifo_reserve_size);
  2104. if (!dirty->cmd) {
  2105. DRM_ERROR("Couldn't reserve fifo space "
  2106. "for dirty blits.\n");
  2107. return -ENOMEM;
  2108. }
  2109. memset(dirty->cmd, 0, dirty->fifo_reserve_size);
  2110. }
  2111. dirty->num_hits = 0;
  2112. for (i = 0; i < num_clips; i++, clips_ptr += increment,
  2113. vclips_ptr += increment) {
  2114. s32 clip_left;
  2115. s32 clip_top;
  2116. /*
  2117. * Select clip array type. Note that integer type
  2118. * in @clips is unsigned short, whereas in @vclips
  2119. * it's 32-bit.
  2120. */
  2121. if (clips) {
  2122. dirty->fb_x = (s32) clips_ptr->x1;
  2123. dirty->fb_y = (s32) clips_ptr->y1;
  2124. dirty->unit_x2 = (s32) clips_ptr->x2 + dest_x -
  2125. crtc_x;
  2126. dirty->unit_y2 = (s32) clips_ptr->y2 + dest_y -
  2127. crtc_y;
  2128. } else {
  2129. dirty->fb_x = vclips_ptr->x;
  2130. dirty->fb_y = vclips_ptr->y;
  2131. dirty->unit_x2 = dirty->fb_x + vclips_ptr->w +
  2132. dest_x - crtc_x;
  2133. dirty->unit_y2 = dirty->fb_y + vclips_ptr->h +
  2134. dest_y - crtc_y;
  2135. }
  2136. dirty->unit_x1 = dirty->fb_x + dest_x - crtc_x;
  2137. dirty->unit_y1 = dirty->fb_y + dest_y - crtc_y;
  2138. /* Skip this clip if it's outside the crtc region */
  2139. if (dirty->unit_x1 >= crtc_width ||
  2140. dirty->unit_y1 >= crtc_height ||
  2141. dirty->unit_x2 <= 0 || dirty->unit_y2 <= 0)
  2142. continue;
  2143. /* Clip right and bottom to crtc limits */
  2144. dirty->unit_x2 = min_t(s32, dirty->unit_x2,
  2145. crtc_width);
  2146. dirty->unit_y2 = min_t(s32, dirty->unit_y2,
  2147. crtc_height);
  2148. /* Clip left and top to crtc limits */
  2149. clip_left = min_t(s32, dirty->unit_x1, 0);
  2150. clip_top = min_t(s32, dirty->unit_y1, 0);
  2151. dirty->unit_x1 -= clip_left;
  2152. dirty->unit_y1 -= clip_top;
  2153. dirty->fb_x -= clip_left;
  2154. dirty->fb_y -= clip_top;
  2155. dirty->clip(dirty);
  2156. }
  2157. dirty->fifo_commit(dirty);
  2158. }
  2159. return 0;
  2160. }
  2161. /**
  2162. * vmw_kms_helper_buffer_prepare - Reserve and validate a buffer object before
  2163. * command submission.
  2164. *
  2165. * @dev_priv. Pointer to a device private structure.
  2166. * @buf: The buffer object
  2167. * @interruptible: Whether to perform waits as interruptible.
  2168. * @validate_as_mob: Whether the buffer should be validated as a MOB. If false,
  2169. * The buffer will be validated as a GMR. Already pinned buffers will not be
  2170. * validated.
  2171. *
  2172. * Returns 0 on success, negative error code on failure, -ERESTARTSYS if
  2173. * interrupted by a signal.
  2174. */
  2175. int vmw_kms_helper_buffer_prepare(struct vmw_private *dev_priv,
  2176. struct vmw_buffer_object *buf,
  2177. bool interruptible,
  2178. bool validate_as_mob,
  2179. bool for_cpu_blit)
  2180. {
  2181. struct ttm_operation_ctx ctx = {
  2182. .interruptible = interruptible,
  2183. .no_wait_gpu = false};
  2184. struct ttm_buffer_object *bo = &buf->base;
  2185. int ret;
  2186. ttm_bo_reserve(bo, false, false, NULL);
  2187. if (for_cpu_blit)
  2188. ret = ttm_bo_validate(bo, &vmw_nonfixed_placement, &ctx);
  2189. else
  2190. ret = vmw_validate_single_buffer(dev_priv, bo, interruptible,
  2191. validate_as_mob);
  2192. if (ret)
  2193. ttm_bo_unreserve(bo);
  2194. return ret;
  2195. }
  2196. /**
  2197. * vmw_kms_helper_buffer_revert - Undo the actions of
  2198. * vmw_kms_helper_buffer_prepare.
  2199. *
  2200. * @res: Pointer to the buffer object.
  2201. *
  2202. * Helper to be used if an error forces the caller to undo the actions of
  2203. * vmw_kms_helper_buffer_prepare.
  2204. */
  2205. void vmw_kms_helper_buffer_revert(struct vmw_buffer_object *buf)
  2206. {
  2207. if (buf)
  2208. ttm_bo_unreserve(&buf->base);
  2209. }
  2210. /**
  2211. * vmw_kms_helper_buffer_finish - Unreserve and fence a buffer object after
  2212. * kms command submission.
  2213. *
  2214. * @dev_priv: Pointer to a device private structure.
  2215. * @file_priv: Pointer to a struct drm_file representing the caller's
  2216. * connection. Must be set to NULL if @user_fence_rep is NULL, and conversely
  2217. * if non-NULL, @user_fence_rep must be non-NULL.
  2218. * @buf: The buffer object.
  2219. * @out_fence: Optional pointer to a fence pointer. If non-NULL, a
  2220. * ref-counted fence pointer is returned here.
  2221. * @user_fence_rep: Optional pointer to a user-space provided struct
  2222. * drm_vmw_fence_rep. If provided, @file_priv must also be provided and the
  2223. * function copies fence data to user-space in a fail-safe manner.
  2224. */
  2225. void vmw_kms_helper_buffer_finish(struct vmw_private *dev_priv,
  2226. struct drm_file *file_priv,
  2227. struct vmw_buffer_object *buf,
  2228. struct vmw_fence_obj **out_fence,
  2229. struct drm_vmw_fence_rep __user *
  2230. user_fence_rep)
  2231. {
  2232. struct vmw_fence_obj *fence;
  2233. uint32_t handle;
  2234. int ret;
  2235. ret = vmw_execbuf_fence_commands(file_priv, dev_priv, &fence,
  2236. file_priv ? &handle : NULL);
  2237. if (buf)
  2238. vmw_bo_fence_single(&buf->base, fence);
  2239. if (file_priv)
  2240. vmw_execbuf_copy_fence_user(dev_priv, vmw_fpriv(file_priv),
  2241. ret, user_fence_rep, fence,
  2242. handle, -1, NULL);
  2243. if (out_fence)
  2244. *out_fence = fence;
  2245. else
  2246. vmw_fence_obj_unreference(&fence);
  2247. vmw_kms_helper_buffer_revert(buf);
  2248. }
  2249. /**
  2250. * vmw_kms_helper_resource_revert - Undo the actions of
  2251. * vmw_kms_helper_resource_prepare.
  2252. *
  2253. * @res: Pointer to the resource. Typically a surface.
  2254. *
  2255. * Helper to be used if an error forces the caller to undo the actions of
  2256. * vmw_kms_helper_resource_prepare.
  2257. */
  2258. void vmw_kms_helper_resource_revert(struct vmw_validation_ctx *ctx)
  2259. {
  2260. struct vmw_resource *res = ctx->res;
  2261. vmw_kms_helper_buffer_revert(ctx->buf);
  2262. vmw_bo_unreference(&ctx->buf);
  2263. vmw_resource_unreserve(res, false, NULL, 0);
  2264. mutex_unlock(&res->dev_priv->cmdbuf_mutex);
  2265. }
  2266. /**
  2267. * vmw_kms_helper_resource_prepare - Reserve and validate a resource before
  2268. * command submission.
  2269. *
  2270. * @res: Pointer to the resource. Typically a surface.
  2271. * @interruptible: Whether to perform waits as interruptible.
  2272. *
  2273. * Reserves and validates also the backup buffer if a guest-backed resource.
  2274. * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
  2275. * interrupted by a signal.
  2276. */
  2277. int vmw_kms_helper_resource_prepare(struct vmw_resource *res,
  2278. bool interruptible,
  2279. struct vmw_validation_ctx *ctx)
  2280. {
  2281. int ret = 0;
  2282. ctx->buf = NULL;
  2283. ctx->res = res;
  2284. if (interruptible)
  2285. ret = mutex_lock_interruptible(&res->dev_priv->cmdbuf_mutex);
  2286. else
  2287. mutex_lock(&res->dev_priv->cmdbuf_mutex);
  2288. if (unlikely(ret != 0))
  2289. return -ERESTARTSYS;
  2290. ret = vmw_resource_reserve(res, interruptible, false);
  2291. if (ret)
  2292. goto out_unlock;
  2293. if (res->backup) {
  2294. ret = vmw_kms_helper_buffer_prepare(res->dev_priv, res->backup,
  2295. interruptible,
  2296. res->dev_priv->has_mob,
  2297. false);
  2298. if (ret)
  2299. goto out_unreserve;
  2300. ctx->buf = vmw_bo_reference(res->backup);
  2301. }
  2302. ret = vmw_resource_validate(res);
  2303. if (ret)
  2304. goto out_revert;
  2305. return 0;
  2306. out_revert:
  2307. vmw_kms_helper_buffer_revert(ctx->buf);
  2308. out_unreserve:
  2309. vmw_resource_unreserve(res, false, NULL, 0);
  2310. out_unlock:
  2311. mutex_unlock(&res->dev_priv->cmdbuf_mutex);
  2312. return ret;
  2313. }
  2314. /**
  2315. * vmw_kms_helper_resource_finish - Unreserve and fence a resource after
  2316. * kms command submission.
  2317. *
  2318. * @res: Pointer to the resource. Typically a surface.
  2319. * @out_fence: Optional pointer to a fence pointer. If non-NULL, a
  2320. * ref-counted fence pointer is returned here.
  2321. */
  2322. void vmw_kms_helper_resource_finish(struct vmw_validation_ctx *ctx,
  2323. struct vmw_fence_obj **out_fence)
  2324. {
  2325. struct vmw_resource *res = ctx->res;
  2326. if (ctx->buf || out_fence)
  2327. vmw_kms_helper_buffer_finish(res->dev_priv, NULL, ctx->buf,
  2328. out_fence, NULL);
  2329. vmw_bo_unreference(&ctx->buf);
  2330. vmw_resource_unreserve(res, false, NULL, 0);
  2331. mutex_unlock(&res->dev_priv->cmdbuf_mutex);
  2332. }
  2333. /**
  2334. * vmw_kms_update_proxy - Helper function to update a proxy surface from
  2335. * its backing MOB.
  2336. *
  2337. * @res: Pointer to the surface resource
  2338. * @clips: Clip rects in framebuffer (surface) space.
  2339. * @num_clips: Number of clips in @clips.
  2340. * @increment: Integer with which to increment the clip counter when looping.
  2341. * Used to skip a predetermined number of clip rects.
  2342. *
  2343. * This function makes sure the proxy surface is updated from its backing MOB
  2344. * using the region given by @clips. The surface resource @res and its backing
  2345. * MOB needs to be reserved and validated on call.
  2346. */
  2347. int vmw_kms_update_proxy(struct vmw_resource *res,
  2348. const struct drm_clip_rect *clips,
  2349. unsigned num_clips,
  2350. int increment)
  2351. {
  2352. struct vmw_private *dev_priv = res->dev_priv;
  2353. struct drm_vmw_size *size = &vmw_res_to_srf(res)->base_size;
  2354. struct {
  2355. SVGA3dCmdHeader header;
  2356. SVGA3dCmdUpdateGBImage body;
  2357. } *cmd;
  2358. SVGA3dBox *box;
  2359. size_t copy_size = 0;
  2360. int i;
  2361. if (!clips)
  2362. return 0;
  2363. cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd) * num_clips);
  2364. if (!cmd) {
  2365. DRM_ERROR("Couldn't reserve fifo space for proxy surface "
  2366. "update.\n");
  2367. return -ENOMEM;
  2368. }
  2369. for (i = 0; i < num_clips; ++i, clips += increment, ++cmd) {
  2370. box = &cmd->body.box;
  2371. cmd->header.id = SVGA_3D_CMD_UPDATE_GB_IMAGE;
  2372. cmd->header.size = sizeof(cmd->body);
  2373. cmd->body.image.sid = res->id;
  2374. cmd->body.image.face = 0;
  2375. cmd->body.image.mipmap = 0;
  2376. if (clips->x1 > size->width || clips->x2 > size->width ||
  2377. clips->y1 > size->height || clips->y2 > size->height) {
  2378. DRM_ERROR("Invalid clips outsize of framebuffer.\n");
  2379. return -EINVAL;
  2380. }
  2381. box->x = clips->x1;
  2382. box->y = clips->y1;
  2383. box->z = 0;
  2384. box->w = clips->x2 - clips->x1;
  2385. box->h = clips->y2 - clips->y1;
  2386. box->d = 1;
  2387. copy_size += sizeof(*cmd);
  2388. }
  2389. vmw_fifo_commit(dev_priv, copy_size);
  2390. return 0;
  2391. }
  2392. int vmw_kms_fbdev_init_data(struct vmw_private *dev_priv,
  2393. unsigned unit,
  2394. u32 max_width,
  2395. u32 max_height,
  2396. struct drm_connector **p_con,
  2397. struct drm_crtc **p_crtc,
  2398. struct drm_display_mode **p_mode)
  2399. {
  2400. struct drm_connector *con;
  2401. struct vmw_display_unit *du;
  2402. struct drm_display_mode *mode;
  2403. int i = 0;
  2404. int ret = 0;
  2405. mutex_lock(&dev_priv->dev->mode_config.mutex);
  2406. list_for_each_entry(con, &dev_priv->dev->mode_config.connector_list,
  2407. head) {
  2408. if (i == unit)
  2409. break;
  2410. ++i;
  2411. }
  2412. if (i != unit) {
  2413. DRM_ERROR("Could not find initial display unit.\n");
  2414. ret = -EINVAL;
  2415. goto out_unlock;
  2416. }
  2417. if (list_empty(&con->modes))
  2418. (void) vmw_du_connector_fill_modes(con, max_width, max_height);
  2419. if (list_empty(&con->modes)) {
  2420. DRM_ERROR("Could not find initial display mode.\n");
  2421. ret = -EINVAL;
  2422. goto out_unlock;
  2423. }
  2424. du = vmw_connector_to_du(con);
  2425. *p_con = con;
  2426. *p_crtc = &du->crtc;
  2427. list_for_each_entry(mode, &con->modes, head) {
  2428. if (mode->type & DRM_MODE_TYPE_PREFERRED)
  2429. break;
  2430. }
  2431. if (mode->type & DRM_MODE_TYPE_PREFERRED)
  2432. *p_mode = mode;
  2433. else {
  2434. WARN_ONCE(true, "Could not find initial preferred mode.\n");
  2435. *p_mode = list_first_entry(&con->modes,
  2436. struct drm_display_mode,
  2437. head);
  2438. }
  2439. out_unlock:
  2440. mutex_unlock(&dev_priv->dev->mode_config.mutex);
  2441. return ret;
  2442. }
  2443. /**
  2444. * vmw_kms_del_active - unregister a crtc binding to the implicit framebuffer
  2445. *
  2446. * @dev_priv: Pointer to a device private struct.
  2447. * @du: The display unit of the crtc.
  2448. */
  2449. void vmw_kms_del_active(struct vmw_private *dev_priv,
  2450. struct vmw_display_unit *du)
  2451. {
  2452. mutex_lock(&dev_priv->global_kms_state_mutex);
  2453. if (du->active_implicit) {
  2454. if (--(dev_priv->num_implicit) == 0)
  2455. dev_priv->implicit_fb = NULL;
  2456. du->active_implicit = false;
  2457. }
  2458. mutex_unlock(&dev_priv->global_kms_state_mutex);
  2459. }
  2460. /**
  2461. * vmw_kms_add_active - register a crtc binding to an implicit framebuffer
  2462. *
  2463. * @vmw_priv: Pointer to a device private struct.
  2464. * @du: The display unit of the crtc.
  2465. * @vfb: The implicit framebuffer
  2466. *
  2467. * Registers a binding to an implicit framebuffer.
  2468. */
  2469. void vmw_kms_add_active(struct vmw_private *dev_priv,
  2470. struct vmw_display_unit *du,
  2471. struct vmw_framebuffer *vfb)
  2472. {
  2473. mutex_lock(&dev_priv->global_kms_state_mutex);
  2474. WARN_ON_ONCE(!dev_priv->num_implicit && dev_priv->implicit_fb);
  2475. if (!du->active_implicit && du->is_implicit) {
  2476. dev_priv->implicit_fb = vfb;
  2477. du->active_implicit = true;
  2478. dev_priv->num_implicit++;
  2479. }
  2480. mutex_unlock(&dev_priv->global_kms_state_mutex);
  2481. }
  2482. /**
  2483. * vmw_kms_screen_object_flippable - Check whether we can page-flip a crtc.
  2484. *
  2485. * @dev_priv: Pointer to device-private struct.
  2486. * @crtc: The crtc we want to flip.
  2487. *
  2488. * Returns true or false depending whether it's OK to flip this crtc
  2489. * based on the criterion that we must not have more than one implicit
  2490. * frame-buffer at any one time.
  2491. */
  2492. bool vmw_kms_crtc_flippable(struct vmw_private *dev_priv,
  2493. struct drm_crtc *crtc)
  2494. {
  2495. struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
  2496. bool ret;
  2497. mutex_lock(&dev_priv->global_kms_state_mutex);
  2498. ret = !du->is_implicit || dev_priv->num_implicit == 1;
  2499. mutex_unlock(&dev_priv->global_kms_state_mutex);
  2500. return ret;
  2501. }
  2502. /**
  2503. * vmw_kms_update_implicit_fb - Update the implicit fb.
  2504. *
  2505. * @dev_priv: Pointer to device-private struct.
  2506. * @crtc: The crtc the new implicit frame-buffer is bound to.
  2507. */
  2508. void vmw_kms_update_implicit_fb(struct vmw_private *dev_priv,
  2509. struct drm_crtc *crtc)
  2510. {
  2511. struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
  2512. struct drm_plane *plane = crtc->primary;
  2513. struct vmw_framebuffer *vfb;
  2514. mutex_lock(&dev_priv->global_kms_state_mutex);
  2515. if (!du->is_implicit)
  2516. goto out_unlock;
  2517. vfb = vmw_framebuffer_to_vfb(plane->state->fb);
  2518. WARN_ON_ONCE(dev_priv->num_implicit != 1 &&
  2519. dev_priv->implicit_fb != vfb);
  2520. dev_priv->implicit_fb = vfb;
  2521. out_unlock:
  2522. mutex_unlock(&dev_priv->global_kms_state_mutex);
  2523. }
  2524. /**
  2525. * vmw_kms_create_implicit_placement_proparty - Set up the implicit placement
  2526. * property.
  2527. *
  2528. * @dev_priv: Pointer to a device private struct.
  2529. * @immutable: Whether the property is immutable.
  2530. *
  2531. * Sets up the implicit placement property unless it's already set up.
  2532. */
  2533. void
  2534. vmw_kms_create_implicit_placement_property(struct vmw_private *dev_priv,
  2535. bool immutable)
  2536. {
  2537. if (dev_priv->implicit_placement_property)
  2538. return;
  2539. dev_priv->implicit_placement_property =
  2540. drm_property_create_range(dev_priv->dev,
  2541. immutable ?
  2542. DRM_MODE_PROP_IMMUTABLE : 0,
  2543. "implicit_placement", 0, 1);
  2544. }
  2545. /**
  2546. * vmw_kms_set_config - Wrapper around drm_atomic_helper_set_config
  2547. *
  2548. * @set: The configuration to set.
  2549. *
  2550. * The vmwgfx Xorg driver doesn't assign the mode::type member, which
  2551. * when drm_mode_set_crtcinfo is called as part of the configuration setting
  2552. * causes it to return incorrect crtc dimensions causing severe problems in
  2553. * the vmwgfx modesetting. So explicitly clear that member before calling
  2554. * into drm_atomic_helper_set_config.
  2555. */
  2556. int vmw_kms_set_config(struct drm_mode_set *set,
  2557. struct drm_modeset_acquire_ctx *ctx)
  2558. {
  2559. if (set && set->mode)
  2560. set->mode->type = 0;
  2561. return drm_atomic_helper_set_config(set, ctx);
  2562. }
  2563. /**
  2564. * vmw_kms_suspend - Save modesetting state and turn modesetting off.
  2565. *
  2566. * @dev: Pointer to the drm device
  2567. * Return: 0 on success. Negative error code on failure.
  2568. */
  2569. int vmw_kms_suspend(struct drm_device *dev)
  2570. {
  2571. struct vmw_private *dev_priv = vmw_priv(dev);
  2572. dev_priv->suspend_state = drm_atomic_helper_suspend(dev);
  2573. if (IS_ERR(dev_priv->suspend_state)) {
  2574. int ret = PTR_ERR(dev_priv->suspend_state);
  2575. DRM_ERROR("Failed kms suspend: %d\n", ret);
  2576. dev_priv->suspend_state = NULL;
  2577. return ret;
  2578. }
  2579. return 0;
  2580. }
  2581. /**
  2582. * vmw_kms_resume - Re-enable modesetting and restore state
  2583. *
  2584. * @dev: Pointer to the drm device
  2585. * Return: 0 on success. Negative error code on failure.
  2586. *
  2587. * State is resumed from a previous vmw_kms_suspend(). It's illegal
  2588. * to call this function without a previous vmw_kms_suspend().
  2589. */
  2590. int vmw_kms_resume(struct drm_device *dev)
  2591. {
  2592. struct vmw_private *dev_priv = vmw_priv(dev);
  2593. int ret;
  2594. if (WARN_ON(!dev_priv->suspend_state))
  2595. return 0;
  2596. ret = drm_atomic_helper_resume(dev, dev_priv->suspend_state);
  2597. dev_priv->suspend_state = NULL;
  2598. return ret;
  2599. }
  2600. /**
  2601. * vmw_kms_lost_device - Notify kms that modesetting capabilities will be lost
  2602. *
  2603. * @dev: Pointer to the drm device
  2604. */
  2605. void vmw_kms_lost_device(struct drm_device *dev)
  2606. {
  2607. drm_atomic_helper_shutdown(dev);
  2608. }