vmwgfx_kms.c 76 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932
  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_dmabuf(struct vmw_private *dev_priv,
  75. struct vmw_dma_buffer *dmabuf,
  76. u32 width, u32 height,
  77. u32 hotspotX, u32 hotspotY)
  78. {
  79. struct ttm_bo_kmap_obj map;
  80. unsigned long kmap_offset;
  81. unsigned long kmap_num;
  82. void *virtual;
  83. bool dummy;
  84. int ret;
  85. kmap_offset = 0;
  86. kmap_num = (width*height*4 + PAGE_SIZE - 1) >> PAGE_SHIFT;
  87. ret = ttm_bo_reserve(&dmabuf->base, true, false, NULL);
  88. if (unlikely(ret != 0)) {
  89. DRM_ERROR("reserve failed\n");
  90. return -EINVAL;
  91. }
  92. ret = ttm_bo_kmap(&dmabuf->base, kmap_offset, kmap_num, &map);
  93. if (unlikely(ret != 0))
  94. goto err_unreserve;
  95. virtual = ttm_kmap_obj_virtual(&map, &dummy);
  96. ret = vmw_cursor_update_image(dev_priv, virtual, width, height,
  97. hotspotX, hotspotY);
  98. ttm_bo_kunmap(&map);
  99. err_unreserve:
  100. ttm_bo_unreserve(&dmabuf->base);
  101. return ret;
  102. }
  103. static void vmw_cursor_update_position(struct vmw_private *dev_priv,
  104. bool show, int x, int y)
  105. {
  106. u32 *fifo_mem = dev_priv->mmio_virt;
  107. uint32_t count;
  108. spin_lock(&dev_priv->cursor_lock);
  109. vmw_mmio_write(show ? 1 : 0, fifo_mem + SVGA_FIFO_CURSOR_ON);
  110. vmw_mmio_write(x, fifo_mem + SVGA_FIFO_CURSOR_X);
  111. vmw_mmio_write(y, fifo_mem + SVGA_FIFO_CURSOR_Y);
  112. count = vmw_mmio_read(fifo_mem + SVGA_FIFO_CURSOR_COUNT);
  113. vmw_mmio_write(++count, fifo_mem + SVGA_FIFO_CURSOR_COUNT);
  114. spin_unlock(&dev_priv->cursor_lock);
  115. }
  116. void vmw_kms_cursor_snoop(struct vmw_surface *srf,
  117. struct ttm_object_file *tfile,
  118. struct ttm_buffer_object *bo,
  119. SVGA3dCmdHeader *header)
  120. {
  121. struct ttm_bo_kmap_obj map;
  122. unsigned long kmap_offset;
  123. unsigned long kmap_num;
  124. SVGA3dCopyBox *box;
  125. unsigned box_count;
  126. void *virtual;
  127. bool dummy;
  128. struct vmw_dma_cmd {
  129. SVGA3dCmdHeader header;
  130. SVGA3dCmdSurfaceDMA dma;
  131. } *cmd;
  132. int i, ret;
  133. cmd = container_of(header, struct vmw_dma_cmd, header);
  134. /* No snooper installed */
  135. if (!srf->snooper.image)
  136. return;
  137. if (cmd->dma.host.face != 0 || cmd->dma.host.mipmap != 0) {
  138. DRM_ERROR("face and mipmap for cursors should never != 0\n");
  139. return;
  140. }
  141. if (cmd->header.size < 64) {
  142. DRM_ERROR("at least one full copy box must be given\n");
  143. return;
  144. }
  145. box = (SVGA3dCopyBox *)&cmd[1];
  146. box_count = (cmd->header.size - sizeof(SVGA3dCmdSurfaceDMA)) /
  147. sizeof(SVGA3dCopyBox);
  148. if (cmd->dma.guest.ptr.offset % PAGE_SIZE ||
  149. box->x != 0 || box->y != 0 || box->z != 0 ||
  150. box->srcx != 0 || box->srcy != 0 || box->srcz != 0 ||
  151. box->d != 1 || box_count != 1) {
  152. /* TODO handle none page aligned offsets */
  153. /* TODO handle more dst & src != 0 */
  154. /* TODO handle more then one copy */
  155. DRM_ERROR("Cant snoop dma request for cursor!\n");
  156. DRM_ERROR("(%u, %u, %u) (%u, %u, %u) (%ux%ux%u) %u %u\n",
  157. box->srcx, box->srcy, box->srcz,
  158. box->x, box->y, box->z,
  159. box->w, box->h, box->d, box_count,
  160. cmd->dma.guest.ptr.offset);
  161. return;
  162. }
  163. kmap_offset = cmd->dma.guest.ptr.offset >> PAGE_SHIFT;
  164. kmap_num = (64*64*4) >> PAGE_SHIFT;
  165. ret = ttm_bo_reserve(bo, true, false, NULL);
  166. if (unlikely(ret != 0)) {
  167. DRM_ERROR("reserve failed\n");
  168. return;
  169. }
  170. ret = ttm_bo_kmap(bo, kmap_offset, kmap_num, &map);
  171. if (unlikely(ret != 0))
  172. goto err_unreserve;
  173. virtual = ttm_kmap_obj_virtual(&map, &dummy);
  174. if (box->w == 64 && cmd->dma.guest.pitch == 64*4) {
  175. memcpy(srf->snooper.image, virtual, 64*64*4);
  176. } else {
  177. /* Image is unsigned pointer. */
  178. for (i = 0; i < box->h; i++)
  179. memcpy(srf->snooper.image + i * 64,
  180. virtual + i * cmd->dma.guest.pitch,
  181. box->w * 4);
  182. }
  183. srf->snooper.age++;
  184. ttm_bo_kunmap(&map);
  185. err_unreserve:
  186. ttm_bo_unreserve(bo);
  187. }
  188. /**
  189. * vmw_kms_legacy_hotspot_clear - Clear legacy hotspots
  190. *
  191. * @dev_priv: Pointer to the device private struct.
  192. *
  193. * Clears all legacy hotspots.
  194. */
  195. void vmw_kms_legacy_hotspot_clear(struct vmw_private *dev_priv)
  196. {
  197. struct drm_device *dev = dev_priv->dev;
  198. struct vmw_display_unit *du;
  199. struct drm_crtc *crtc;
  200. drm_modeset_lock_all(dev);
  201. drm_for_each_crtc(crtc, dev) {
  202. du = vmw_crtc_to_du(crtc);
  203. du->hotspot_x = 0;
  204. du->hotspot_y = 0;
  205. }
  206. drm_modeset_unlock_all(dev);
  207. }
  208. void vmw_kms_cursor_post_execbuf(struct vmw_private *dev_priv)
  209. {
  210. struct drm_device *dev = dev_priv->dev;
  211. struct vmw_display_unit *du;
  212. struct drm_crtc *crtc;
  213. mutex_lock(&dev->mode_config.mutex);
  214. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  215. du = vmw_crtc_to_du(crtc);
  216. if (!du->cursor_surface ||
  217. du->cursor_age == du->cursor_surface->snooper.age)
  218. continue;
  219. du->cursor_age = du->cursor_surface->snooper.age;
  220. vmw_cursor_update_image(dev_priv,
  221. du->cursor_surface->snooper.image,
  222. 64, 64,
  223. du->hotspot_x + du->core_hotspot_x,
  224. du->hotspot_y + du->core_hotspot_y);
  225. }
  226. mutex_unlock(&dev->mode_config.mutex);
  227. }
  228. void vmw_du_cursor_plane_destroy(struct drm_plane *plane)
  229. {
  230. vmw_cursor_update_position(plane->dev->dev_private, false, 0, 0);
  231. drm_plane_cleanup(plane);
  232. }
  233. void vmw_du_primary_plane_destroy(struct drm_plane *plane)
  234. {
  235. drm_plane_cleanup(plane);
  236. /* Planes are static in our case so we don't free it */
  237. }
  238. /**
  239. * vmw_du_vps_unpin_surf - unpins resource associated with a framebuffer surface
  240. *
  241. * @vps: plane state associated with the display surface
  242. * @unreference: true if we also want to unreference the display.
  243. */
  244. void vmw_du_plane_unpin_surf(struct vmw_plane_state *vps,
  245. bool unreference)
  246. {
  247. if (vps->surf) {
  248. if (vps->pinned) {
  249. vmw_resource_unpin(&vps->surf->res);
  250. vps->pinned--;
  251. }
  252. if (unreference) {
  253. if (vps->pinned)
  254. DRM_ERROR("Surface still pinned\n");
  255. vmw_surface_unreference(&vps->surf);
  256. }
  257. }
  258. }
  259. /**
  260. * vmw_du_plane_cleanup_fb - Unpins the cursor
  261. *
  262. * @plane: display plane
  263. * @old_state: Contains the FB to clean up
  264. *
  265. * Unpins the framebuffer surface
  266. *
  267. * Returns 0 on success
  268. */
  269. void
  270. vmw_du_plane_cleanup_fb(struct drm_plane *plane,
  271. struct drm_plane_state *old_state)
  272. {
  273. struct vmw_plane_state *vps = vmw_plane_state_to_vps(old_state);
  274. vmw_du_plane_unpin_surf(vps, false);
  275. }
  276. /**
  277. * vmw_du_cursor_plane_prepare_fb - Readies the cursor by referencing it
  278. *
  279. * @plane: display plane
  280. * @new_state: info on the new plane state, including the FB
  281. *
  282. * Returns 0 on success
  283. */
  284. int
  285. vmw_du_cursor_plane_prepare_fb(struct drm_plane *plane,
  286. struct drm_plane_state *new_state)
  287. {
  288. struct drm_framebuffer *fb = new_state->fb;
  289. struct vmw_plane_state *vps = vmw_plane_state_to_vps(new_state);
  290. if (vps->surf)
  291. vmw_surface_unreference(&vps->surf);
  292. if (vps->dmabuf)
  293. vmw_dmabuf_unreference(&vps->dmabuf);
  294. if (fb) {
  295. if (vmw_framebuffer_to_vfb(fb)->dmabuf) {
  296. vps->dmabuf = vmw_framebuffer_to_vfbd(fb)->buffer;
  297. vmw_dmabuf_reference(vps->dmabuf);
  298. } else {
  299. vps->surf = vmw_framebuffer_to_vfbs(fb)->surface;
  300. vmw_surface_reference(vps->surf);
  301. }
  302. }
  303. return 0;
  304. }
  305. void
  306. vmw_du_cursor_plane_atomic_update(struct drm_plane *plane,
  307. struct drm_plane_state *old_state)
  308. {
  309. struct drm_crtc *crtc = plane->state->crtc ?: old_state->crtc;
  310. struct vmw_private *dev_priv = vmw_priv(crtc->dev);
  311. struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
  312. struct vmw_plane_state *vps = vmw_plane_state_to_vps(plane->state);
  313. s32 hotspot_x, hotspot_y;
  314. int ret = 0;
  315. hotspot_x = du->hotspot_x;
  316. hotspot_y = du->hotspot_y;
  317. 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_dmabuf = vps->dmabuf;
  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->dmabuf) {
  330. ret = vmw_cursor_update_dmabuf(dev_priv, vps->dmabuf,
  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)->dmabuf)
  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 = 1 << drm_connector_index(&du->connector);
  443. bool has_primary = new_state->plane_mask &
  444. BIT(drm_plane_index(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->dmabuf)
  559. (void) vmw_dmabuf_reference(vps->dmabuf);
  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->dmabuf)
  602. vmw_dmabuf_unreference(&vps->dmabuf);
  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 dma-buffer 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 dma-buffer 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_dmabuf_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_dmabuf_proxy = is_dmabuf_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. * Dmabuf framebuffer code
  844. */
  845. static void vmw_framebuffer_dmabuf_destroy(struct drm_framebuffer *framebuffer)
  846. {
  847. struct vmw_framebuffer_dmabuf *vfbd =
  848. vmw_framebuffer_to_vfbd(framebuffer);
  849. drm_framebuffer_cleanup(framebuffer);
  850. vmw_dmabuf_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_dmabuf_dirty(struct drm_framebuffer *framebuffer,
  856. struct drm_file *file_priv,
  857. unsigned flags, unsigned color,
  858. struct drm_clip_rect *clips,
  859. unsigned num_clips)
  860. {
  861. struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
  862. struct vmw_framebuffer_dmabuf *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_dmabuf_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_dmabuf_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_dmabuf_funcs = {
  908. .destroy = vmw_framebuffer_dmabuf_destroy,
  909. .dirty = vmw_framebuffer_dmabuf_dirty,
  910. };
  911. /**
  912. * Pin the dmabuffer 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_dma_buffer *buf;
  919. struct ttm_placement *placement;
  920. int ret;
  921. buf = vfb->dmabuf ? 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_dmabuf_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->dmabuf) {
  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_dmabuf_pin_in_placement(dev_priv, buf, placement,
  949. false);
  950. default:
  951. return -EINVAL;
  952. }
  953. return ret;
  954. }
  955. static int vmw_framebuffer_unpin(struct vmw_framebuffer *vfb)
  956. {
  957. struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
  958. struct vmw_dma_buffer *buf;
  959. buf = vfb->dmabuf ? vmw_framebuffer_to_vfbd(&vfb->base)->buffer :
  960. vmw_framebuffer_to_vfbs(&vfb->base)->surface->res.backup;
  961. if (WARN_ON(!buf))
  962. return 0;
  963. return vmw_dmabuf_unpin(dev_priv, buf, false);
  964. }
  965. /**
  966. * vmw_create_dmabuf_proxy - create a proxy surface for the DMA buf
  967. *
  968. * @dev: DRM device
  969. * @mode_cmd: parameters for the new surface
  970. * @dmabuf_mob: MOB backing the DMA buf
  971. * @srf_out: newly created surface
  972. *
  973. * When the content FB is a DMA buf, we create a surface as a proxy to the
  974. * same buffer. This way we can do a surface copy rather than a surface DMA.
  975. * This is a more efficient approach
  976. *
  977. * RETURNS:
  978. * 0 on success, error code otherwise
  979. */
  980. static int vmw_create_dmabuf_proxy(struct drm_device *dev,
  981. const struct drm_mode_fb_cmd2 *mode_cmd,
  982. struct vmw_dma_buffer *dmabuf_mob,
  983. struct vmw_surface **srf_out)
  984. {
  985. uint32_t format;
  986. struct drm_vmw_size content_base_size = {0};
  987. struct vmw_resource *res;
  988. unsigned int bytes_pp;
  989. struct drm_format_name_buf format_name;
  990. int ret;
  991. switch (mode_cmd->pixel_format) {
  992. case DRM_FORMAT_ARGB8888:
  993. case DRM_FORMAT_XRGB8888:
  994. format = SVGA3D_X8R8G8B8;
  995. bytes_pp = 4;
  996. break;
  997. case DRM_FORMAT_RGB565:
  998. case DRM_FORMAT_XRGB1555:
  999. format = SVGA3D_R5G6B5;
  1000. bytes_pp = 2;
  1001. break;
  1002. case 8:
  1003. format = SVGA3D_P8;
  1004. bytes_pp = 1;
  1005. break;
  1006. default:
  1007. DRM_ERROR("Invalid framebuffer format %s\n",
  1008. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  1009. return -EINVAL;
  1010. }
  1011. content_base_size.width = mode_cmd->pitches[0] / bytes_pp;
  1012. content_base_size.height = mode_cmd->height;
  1013. content_base_size.depth = 1;
  1014. ret = vmw_surface_gb_priv_define(dev,
  1015. 0, /* kernel visible only */
  1016. 0, /* flags */
  1017. format,
  1018. true, /* can be a scanout buffer */
  1019. 1, /* num of mip levels */
  1020. 0,
  1021. 0,
  1022. content_base_size,
  1023. srf_out);
  1024. if (ret) {
  1025. DRM_ERROR("Failed to allocate proxy content buffer\n");
  1026. return ret;
  1027. }
  1028. res = &(*srf_out)->res;
  1029. /* Reserve and switch the backing mob. */
  1030. mutex_lock(&res->dev_priv->cmdbuf_mutex);
  1031. (void) vmw_resource_reserve(res, false, true);
  1032. vmw_dmabuf_unreference(&res->backup);
  1033. res->backup = vmw_dmabuf_reference(dmabuf_mob);
  1034. res->backup_offset = 0;
  1035. vmw_resource_unreserve(res, false, NULL, 0);
  1036. mutex_unlock(&res->dev_priv->cmdbuf_mutex);
  1037. return 0;
  1038. }
  1039. static int vmw_kms_new_framebuffer_dmabuf(struct vmw_private *dev_priv,
  1040. struct vmw_dma_buffer *dmabuf,
  1041. struct vmw_framebuffer **out,
  1042. const struct drm_mode_fb_cmd2
  1043. *mode_cmd)
  1044. {
  1045. struct drm_device *dev = dev_priv->dev;
  1046. struct vmw_framebuffer_dmabuf *vfbd;
  1047. unsigned int requested_size;
  1048. struct drm_format_name_buf format_name;
  1049. int ret;
  1050. requested_size = mode_cmd->height * mode_cmd->pitches[0];
  1051. if (unlikely(requested_size > dmabuf->base.num_pages * PAGE_SIZE)) {
  1052. DRM_ERROR("Screen buffer object size is too small "
  1053. "for requested mode.\n");
  1054. return -EINVAL;
  1055. }
  1056. /* Limited framebuffer color depth support for screen objects */
  1057. if (dev_priv->active_display_unit == vmw_du_screen_object) {
  1058. switch (mode_cmd->pixel_format) {
  1059. case DRM_FORMAT_XRGB8888:
  1060. case DRM_FORMAT_ARGB8888:
  1061. break;
  1062. case DRM_FORMAT_XRGB1555:
  1063. case DRM_FORMAT_RGB565:
  1064. break;
  1065. default:
  1066. DRM_ERROR("Invalid pixel format: %s\n",
  1067. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  1068. return -EINVAL;
  1069. }
  1070. }
  1071. vfbd = kzalloc(sizeof(*vfbd), GFP_KERNEL);
  1072. if (!vfbd) {
  1073. ret = -ENOMEM;
  1074. goto out_err1;
  1075. }
  1076. drm_helper_mode_fill_fb_struct(dev, &vfbd->base.base, mode_cmd);
  1077. vfbd->base.dmabuf = true;
  1078. vfbd->buffer = vmw_dmabuf_reference(dmabuf);
  1079. vfbd->base.user_handle = mode_cmd->handles[0];
  1080. *out = &vfbd->base;
  1081. ret = drm_framebuffer_init(dev, &vfbd->base.base,
  1082. &vmw_framebuffer_dmabuf_funcs);
  1083. if (ret)
  1084. goto out_err2;
  1085. return 0;
  1086. out_err2:
  1087. vmw_dmabuf_unreference(&dmabuf);
  1088. kfree(vfbd);
  1089. out_err1:
  1090. return ret;
  1091. }
  1092. /**
  1093. * vmw_kms_srf_ok - check if a surface can be created
  1094. *
  1095. * @width: requested width
  1096. * @height: requested height
  1097. *
  1098. * Surfaces need to be less than texture size
  1099. */
  1100. static bool
  1101. vmw_kms_srf_ok(struct vmw_private *dev_priv, uint32_t width, uint32_t height)
  1102. {
  1103. if (width > dev_priv->texture_max_width ||
  1104. height > dev_priv->texture_max_height)
  1105. return false;
  1106. return true;
  1107. }
  1108. /**
  1109. * vmw_kms_new_framebuffer - Create a new framebuffer.
  1110. *
  1111. * @dev_priv: Pointer to device private struct.
  1112. * @dmabuf: Pointer to dma buffer to wrap the kms framebuffer around.
  1113. * Either @dmabuf or @surface must be NULL.
  1114. * @surface: Pointer to a surface to wrap the kms framebuffer around.
  1115. * Either @dmabuf or @surface must be NULL.
  1116. * @only_2d: No presents will occur to this dma buffer based framebuffer. This
  1117. * Helps the code to do some important optimizations.
  1118. * @mode_cmd: Frame-buffer metadata.
  1119. */
  1120. struct vmw_framebuffer *
  1121. vmw_kms_new_framebuffer(struct vmw_private *dev_priv,
  1122. struct vmw_dma_buffer *dmabuf,
  1123. struct vmw_surface *surface,
  1124. bool only_2d,
  1125. const struct drm_mode_fb_cmd2 *mode_cmd)
  1126. {
  1127. struct vmw_framebuffer *vfb = NULL;
  1128. bool is_dmabuf_proxy = false;
  1129. int ret;
  1130. /*
  1131. * We cannot use the SurfaceDMA command in an non-accelerated VM,
  1132. * therefore, wrap the DMA buf in a surface so we can use the
  1133. * SurfaceCopy command.
  1134. */
  1135. if (vmw_kms_srf_ok(dev_priv, mode_cmd->width, mode_cmd->height) &&
  1136. dmabuf && only_2d &&
  1137. mode_cmd->width > 64 && /* Don't create a proxy for cursor */
  1138. dev_priv->active_display_unit == vmw_du_screen_target) {
  1139. ret = vmw_create_dmabuf_proxy(dev_priv->dev, mode_cmd,
  1140. dmabuf, &surface);
  1141. if (ret)
  1142. return ERR_PTR(ret);
  1143. is_dmabuf_proxy = true;
  1144. }
  1145. /* Create the new framebuffer depending one what we have */
  1146. if (surface) {
  1147. ret = vmw_kms_new_framebuffer_surface(dev_priv, surface, &vfb,
  1148. mode_cmd,
  1149. is_dmabuf_proxy);
  1150. /*
  1151. * vmw_create_dmabuf_proxy() adds a reference that is no longer
  1152. * needed
  1153. */
  1154. if (is_dmabuf_proxy)
  1155. vmw_surface_unreference(&surface);
  1156. } else if (dmabuf) {
  1157. ret = vmw_kms_new_framebuffer_dmabuf(dev_priv, dmabuf, &vfb,
  1158. mode_cmd);
  1159. } else {
  1160. BUG();
  1161. }
  1162. if (ret)
  1163. return ERR_PTR(ret);
  1164. vfb->pin = vmw_framebuffer_pin;
  1165. vfb->unpin = vmw_framebuffer_unpin;
  1166. return vfb;
  1167. }
  1168. /*
  1169. * Generic Kernel modesetting functions
  1170. */
  1171. static struct drm_framebuffer *vmw_kms_fb_create(struct drm_device *dev,
  1172. struct drm_file *file_priv,
  1173. const struct drm_mode_fb_cmd2 *mode_cmd)
  1174. {
  1175. struct vmw_private *dev_priv = vmw_priv(dev);
  1176. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  1177. struct vmw_framebuffer *vfb = NULL;
  1178. struct vmw_surface *surface = NULL;
  1179. struct vmw_dma_buffer *bo = NULL;
  1180. struct ttm_base_object *user_obj;
  1181. int ret;
  1182. /**
  1183. * This code should be conditioned on Screen Objects not being used.
  1184. * If screen objects are used, we can allocate a GMR to hold the
  1185. * requested framebuffer.
  1186. */
  1187. if (!vmw_kms_validate_mode_vram(dev_priv,
  1188. mode_cmd->pitches[0],
  1189. mode_cmd->height)) {
  1190. DRM_ERROR("Requested mode exceed bounding box limit.\n");
  1191. return ERR_PTR(-ENOMEM);
  1192. }
  1193. /*
  1194. * Take a reference on the user object of the resource
  1195. * backing the kms fb. This ensures that user-space handle
  1196. * lookups on that resource will always work as long as
  1197. * it's registered with a kms framebuffer. This is important,
  1198. * since vmw_execbuf_process identifies resources in the
  1199. * command stream using user-space handles.
  1200. */
  1201. user_obj = ttm_base_object_lookup(tfile, mode_cmd->handles[0]);
  1202. if (unlikely(user_obj == NULL)) {
  1203. DRM_ERROR("Could not locate requested kms frame buffer.\n");
  1204. return ERR_PTR(-ENOENT);
  1205. }
  1206. /**
  1207. * End conditioned code.
  1208. */
  1209. /* returns either a dmabuf or surface */
  1210. ret = vmw_user_lookup_handle(dev_priv, tfile,
  1211. mode_cmd->handles[0],
  1212. &surface, &bo);
  1213. if (ret)
  1214. goto err_out;
  1215. if (!bo &&
  1216. !vmw_kms_srf_ok(dev_priv, mode_cmd->width, mode_cmd->height)) {
  1217. DRM_ERROR("Surface size cannot exceed %dx%d",
  1218. dev_priv->texture_max_width,
  1219. dev_priv->texture_max_height);
  1220. goto err_out;
  1221. }
  1222. vfb = vmw_kms_new_framebuffer(dev_priv, bo, surface,
  1223. !(dev_priv->capabilities & SVGA_CAP_3D),
  1224. mode_cmd);
  1225. if (IS_ERR(vfb)) {
  1226. ret = PTR_ERR(vfb);
  1227. goto err_out;
  1228. }
  1229. err_out:
  1230. /* vmw_user_lookup_handle takes one ref so does new_fb */
  1231. if (bo)
  1232. vmw_dmabuf_unreference(&bo);
  1233. if (surface)
  1234. vmw_surface_unreference(&surface);
  1235. if (ret) {
  1236. DRM_ERROR("failed to create vmw_framebuffer: %i\n", ret);
  1237. ttm_base_object_unref(&user_obj);
  1238. return ERR_PTR(ret);
  1239. } else
  1240. vfb->user_obj = user_obj;
  1241. return &vfb->base;
  1242. }
  1243. /**
  1244. * vmw_kms_atomic_check_modeset- validate state object for modeset changes
  1245. *
  1246. * @dev: DRM device
  1247. * @state: the driver state object
  1248. *
  1249. * This is a simple wrapper around drm_atomic_helper_check_modeset() for
  1250. * us to assign a value to mode->crtc_clock so that
  1251. * drm_calc_timestamping_constants() won't throw an error message
  1252. *
  1253. * RETURNS
  1254. * Zero for success or -errno
  1255. */
  1256. static int
  1257. vmw_kms_atomic_check_modeset(struct drm_device *dev,
  1258. struct drm_atomic_state *state)
  1259. {
  1260. struct drm_crtc_state *crtc_state;
  1261. struct drm_crtc *crtc;
  1262. struct vmw_private *dev_priv = vmw_priv(dev);
  1263. int i;
  1264. for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
  1265. unsigned long requested_bb_mem = 0;
  1266. if (dev_priv->active_display_unit == vmw_du_screen_target) {
  1267. struct drm_plane *plane = crtc->primary;
  1268. struct drm_plane_state *plane_state;
  1269. plane_state = drm_atomic_get_new_plane_state(state, plane);
  1270. if (plane_state && plane_state->fb) {
  1271. int cpp = plane_state->fb->format->cpp[0];
  1272. requested_bb_mem += crtc->mode.hdisplay * cpp *
  1273. crtc->mode.vdisplay;
  1274. }
  1275. if (requested_bb_mem > dev_priv->prim_bb_mem)
  1276. return -EINVAL;
  1277. }
  1278. }
  1279. return drm_atomic_helper_check(dev, state);
  1280. }
  1281. static const struct drm_mode_config_funcs vmw_kms_funcs = {
  1282. .fb_create = vmw_kms_fb_create,
  1283. .atomic_check = vmw_kms_atomic_check_modeset,
  1284. .atomic_commit = drm_atomic_helper_commit,
  1285. };
  1286. static int vmw_kms_generic_present(struct vmw_private *dev_priv,
  1287. struct drm_file *file_priv,
  1288. struct vmw_framebuffer *vfb,
  1289. struct vmw_surface *surface,
  1290. uint32_t sid,
  1291. int32_t destX, int32_t destY,
  1292. struct drm_vmw_rect *clips,
  1293. uint32_t num_clips)
  1294. {
  1295. return vmw_kms_sou_do_surface_dirty(dev_priv, vfb, NULL, clips,
  1296. &surface->res, destX, destY,
  1297. num_clips, 1, NULL, NULL);
  1298. }
  1299. int vmw_kms_present(struct vmw_private *dev_priv,
  1300. struct drm_file *file_priv,
  1301. struct vmw_framebuffer *vfb,
  1302. struct vmw_surface *surface,
  1303. uint32_t sid,
  1304. int32_t destX, int32_t destY,
  1305. struct drm_vmw_rect *clips,
  1306. uint32_t num_clips)
  1307. {
  1308. int ret;
  1309. switch (dev_priv->active_display_unit) {
  1310. case vmw_du_screen_target:
  1311. ret = vmw_kms_stdu_surface_dirty(dev_priv, vfb, NULL, clips,
  1312. &surface->res, destX, destY,
  1313. num_clips, 1, NULL, NULL);
  1314. break;
  1315. case vmw_du_screen_object:
  1316. ret = vmw_kms_generic_present(dev_priv, file_priv, vfb, surface,
  1317. sid, destX, destY, clips,
  1318. num_clips);
  1319. break;
  1320. default:
  1321. WARN_ONCE(true,
  1322. "Present called with invalid display system.\n");
  1323. ret = -ENOSYS;
  1324. break;
  1325. }
  1326. if (ret)
  1327. return ret;
  1328. vmw_fifo_flush(dev_priv, false);
  1329. return 0;
  1330. }
  1331. static void
  1332. vmw_kms_create_hotplug_mode_update_property(struct vmw_private *dev_priv)
  1333. {
  1334. if (dev_priv->hotplug_mode_update_property)
  1335. return;
  1336. dev_priv->hotplug_mode_update_property =
  1337. drm_property_create_range(dev_priv->dev,
  1338. DRM_MODE_PROP_IMMUTABLE,
  1339. "hotplug_mode_update", 0, 1);
  1340. if (!dev_priv->hotplug_mode_update_property)
  1341. return;
  1342. }
  1343. int vmw_kms_init(struct vmw_private *dev_priv)
  1344. {
  1345. struct drm_device *dev = dev_priv->dev;
  1346. int ret;
  1347. drm_mode_config_init(dev);
  1348. dev->mode_config.funcs = &vmw_kms_funcs;
  1349. dev->mode_config.min_width = 1;
  1350. dev->mode_config.min_height = 1;
  1351. dev->mode_config.max_width = dev_priv->texture_max_width;
  1352. dev->mode_config.max_height = dev_priv->texture_max_height;
  1353. drm_mode_create_suggested_offset_properties(dev);
  1354. vmw_kms_create_hotplug_mode_update_property(dev_priv);
  1355. ret = vmw_kms_stdu_init_display(dev_priv);
  1356. if (ret) {
  1357. ret = vmw_kms_sou_init_display(dev_priv);
  1358. if (ret) /* Fallback */
  1359. ret = vmw_kms_ldu_init_display(dev_priv);
  1360. }
  1361. return ret;
  1362. }
  1363. int vmw_kms_close(struct vmw_private *dev_priv)
  1364. {
  1365. int ret = 0;
  1366. /*
  1367. * Docs says we should take the lock before calling this function
  1368. * but since it destroys encoders and our destructor calls
  1369. * drm_encoder_cleanup which takes the lock we deadlock.
  1370. */
  1371. drm_mode_config_cleanup(dev_priv->dev);
  1372. if (dev_priv->active_display_unit == vmw_du_legacy)
  1373. ret = vmw_kms_ldu_close_display(dev_priv);
  1374. return ret;
  1375. }
  1376. int vmw_kms_cursor_bypass_ioctl(struct drm_device *dev, void *data,
  1377. struct drm_file *file_priv)
  1378. {
  1379. struct drm_vmw_cursor_bypass_arg *arg = data;
  1380. struct vmw_display_unit *du;
  1381. struct drm_crtc *crtc;
  1382. int ret = 0;
  1383. mutex_lock(&dev->mode_config.mutex);
  1384. if (arg->flags & DRM_VMW_CURSOR_BYPASS_ALL) {
  1385. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  1386. du = vmw_crtc_to_du(crtc);
  1387. du->hotspot_x = arg->xhot;
  1388. du->hotspot_y = arg->yhot;
  1389. }
  1390. mutex_unlock(&dev->mode_config.mutex);
  1391. return 0;
  1392. }
  1393. crtc = drm_crtc_find(dev, file_priv, arg->crtc_id);
  1394. if (!crtc) {
  1395. ret = -ENOENT;
  1396. goto out;
  1397. }
  1398. du = vmw_crtc_to_du(crtc);
  1399. du->hotspot_x = arg->xhot;
  1400. du->hotspot_y = arg->yhot;
  1401. out:
  1402. mutex_unlock(&dev->mode_config.mutex);
  1403. return ret;
  1404. }
  1405. int vmw_kms_write_svga(struct vmw_private *vmw_priv,
  1406. unsigned width, unsigned height, unsigned pitch,
  1407. unsigned bpp, unsigned depth)
  1408. {
  1409. if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
  1410. vmw_write(vmw_priv, SVGA_REG_PITCHLOCK, pitch);
  1411. else if (vmw_fifo_have_pitchlock(vmw_priv))
  1412. vmw_mmio_write(pitch, vmw_priv->mmio_virt +
  1413. SVGA_FIFO_PITCHLOCK);
  1414. vmw_write(vmw_priv, SVGA_REG_WIDTH, width);
  1415. vmw_write(vmw_priv, SVGA_REG_HEIGHT, height);
  1416. vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, bpp);
  1417. if (vmw_read(vmw_priv, SVGA_REG_DEPTH) != depth) {
  1418. DRM_ERROR("Invalid depth %u for %u bpp, host expects %u\n",
  1419. depth, bpp, vmw_read(vmw_priv, SVGA_REG_DEPTH));
  1420. return -EINVAL;
  1421. }
  1422. return 0;
  1423. }
  1424. int vmw_kms_save_vga(struct vmw_private *vmw_priv)
  1425. {
  1426. struct vmw_vga_topology_state *save;
  1427. uint32_t i;
  1428. vmw_priv->vga_width = vmw_read(vmw_priv, SVGA_REG_WIDTH);
  1429. vmw_priv->vga_height = vmw_read(vmw_priv, SVGA_REG_HEIGHT);
  1430. vmw_priv->vga_bpp = vmw_read(vmw_priv, SVGA_REG_BITS_PER_PIXEL);
  1431. if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
  1432. vmw_priv->vga_pitchlock =
  1433. vmw_read(vmw_priv, SVGA_REG_PITCHLOCK);
  1434. else if (vmw_fifo_have_pitchlock(vmw_priv))
  1435. vmw_priv->vga_pitchlock = vmw_mmio_read(vmw_priv->mmio_virt +
  1436. SVGA_FIFO_PITCHLOCK);
  1437. if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
  1438. return 0;
  1439. vmw_priv->num_displays = vmw_read(vmw_priv,
  1440. SVGA_REG_NUM_GUEST_DISPLAYS);
  1441. if (vmw_priv->num_displays == 0)
  1442. vmw_priv->num_displays = 1;
  1443. for (i = 0; i < vmw_priv->num_displays; ++i) {
  1444. save = &vmw_priv->vga_save[i];
  1445. vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
  1446. save->primary = vmw_read(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY);
  1447. save->pos_x = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_X);
  1448. save->pos_y = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y);
  1449. save->width = vmw_read(vmw_priv, SVGA_REG_DISPLAY_WIDTH);
  1450. save->height = vmw_read(vmw_priv, SVGA_REG_DISPLAY_HEIGHT);
  1451. vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
  1452. if (i == 0 && vmw_priv->num_displays == 1 &&
  1453. save->width == 0 && save->height == 0) {
  1454. /*
  1455. * It should be fairly safe to assume that these
  1456. * values are uninitialized.
  1457. */
  1458. save->width = vmw_priv->vga_width - save->pos_x;
  1459. save->height = vmw_priv->vga_height - save->pos_y;
  1460. }
  1461. }
  1462. return 0;
  1463. }
  1464. int vmw_kms_restore_vga(struct vmw_private *vmw_priv)
  1465. {
  1466. struct vmw_vga_topology_state *save;
  1467. uint32_t i;
  1468. vmw_write(vmw_priv, SVGA_REG_WIDTH, vmw_priv->vga_width);
  1469. vmw_write(vmw_priv, SVGA_REG_HEIGHT, vmw_priv->vga_height);
  1470. vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, vmw_priv->vga_bpp);
  1471. if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
  1472. vmw_write(vmw_priv, SVGA_REG_PITCHLOCK,
  1473. vmw_priv->vga_pitchlock);
  1474. else if (vmw_fifo_have_pitchlock(vmw_priv))
  1475. vmw_mmio_write(vmw_priv->vga_pitchlock,
  1476. vmw_priv->mmio_virt + SVGA_FIFO_PITCHLOCK);
  1477. if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
  1478. return 0;
  1479. for (i = 0; i < vmw_priv->num_displays; ++i) {
  1480. save = &vmw_priv->vga_save[i];
  1481. vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
  1482. vmw_write(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY, save->primary);
  1483. vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_X, save->pos_x);
  1484. vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y, save->pos_y);
  1485. vmw_write(vmw_priv, SVGA_REG_DISPLAY_WIDTH, save->width);
  1486. vmw_write(vmw_priv, SVGA_REG_DISPLAY_HEIGHT, save->height);
  1487. vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
  1488. }
  1489. return 0;
  1490. }
  1491. bool vmw_kms_validate_mode_vram(struct vmw_private *dev_priv,
  1492. uint32_t pitch,
  1493. uint32_t height)
  1494. {
  1495. return ((u64) pitch * (u64) height) < (u64)
  1496. ((dev_priv->active_display_unit == vmw_du_screen_target) ?
  1497. dev_priv->prim_bb_mem : dev_priv->vram_size);
  1498. }
  1499. /**
  1500. * Function called by DRM code called with vbl_lock held.
  1501. */
  1502. u32 vmw_get_vblank_counter(struct drm_device *dev, unsigned int pipe)
  1503. {
  1504. return 0;
  1505. }
  1506. /**
  1507. * Function called by DRM code called with vbl_lock held.
  1508. */
  1509. int vmw_enable_vblank(struct drm_device *dev, unsigned int pipe)
  1510. {
  1511. return -EINVAL;
  1512. }
  1513. /**
  1514. * Function called by DRM code called with vbl_lock held.
  1515. */
  1516. void vmw_disable_vblank(struct drm_device *dev, unsigned int pipe)
  1517. {
  1518. }
  1519. /*
  1520. * Small shared kms functions.
  1521. */
  1522. static int vmw_du_update_layout(struct vmw_private *dev_priv, unsigned num,
  1523. struct drm_vmw_rect *rects)
  1524. {
  1525. struct drm_device *dev = dev_priv->dev;
  1526. struct vmw_display_unit *du;
  1527. struct drm_connector *con;
  1528. mutex_lock(&dev->mode_config.mutex);
  1529. #if 0
  1530. {
  1531. unsigned int i;
  1532. DRM_INFO("%s: new layout ", __func__);
  1533. for (i = 0; i < num; i++)
  1534. DRM_INFO("(%i, %i %ux%u) ", rects[i].x, rects[i].y,
  1535. rects[i].w, rects[i].h);
  1536. DRM_INFO("\n");
  1537. }
  1538. #endif
  1539. list_for_each_entry(con, &dev->mode_config.connector_list, head) {
  1540. du = vmw_connector_to_du(con);
  1541. if (num > du->unit) {
  1542. du->pref_width = rects[du->unit].w;
  1543. du->pref_height = rects[du->unit].h;
  1544. du->pref_active = true;
  1545. du->gui_x = rects[du->unit].x;
  1546. du->gui_y = rects[du->unit].y;
  1547. drm_object_property_set_value
  1548. (&con->base, dev->mode_config.suggested_x_property,
  1549. du->gui_x);
  1550. drm_object_property_set_value
  1551. (&con->base, dev->mode_config.suggested_y_property,
  1552. du->gui_y);
  1553. } else {
  1554. du->pref_width = 800;
  1555. du->pref_height = 600;
  1556. du->pref_active = false;
  1557. drm_object_property_set_value
  1558. (&con->base, dev->mode_config.suggested_x_property,
  1559. 0);
  1560. drm_object_property_set_value
  1561. (&con->base, dev->mode_config.suggested_y_property,
  1562. 0);
  1563. }
  1564. con->status = vmw_du_connector_detect(con, true);
  1565. }
  1566. mutex_unlock(&dev->mode_config.mutex);
  1567. drm_sysfs_hotplug_event(dev);
  1568. return 0;
  1569. }
  1570. int vmw_du_crtc_gamma_set(struct drm_crtc *crtc,
  1571. u16 *r, u16 *g, u16 *b,
  1572. uint32_t size,
  1573. struct drm_modeset_acquire_ctx *ctx)
  1574. {
  1575. struct vmw_private *dev_priv = vmw_priv(crtc->dev);
  1576. int i;
  1577. for (i = 0; i < size; i++) {
  1578. DRM_DEBUG("%d r/g/b = 0x%04x / 0x%04x / 0x%04x\n", i,
  1579. r[i], g[i], b[i]);
  1580. vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 0, r[i] >> 8);
  1581. vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 1, g[i] >> 8);
  1582. vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 2, b[i] >> 8);
  1583. }
  1584. return 0;
  1585. }
  1586. int vmw_du_connector_dpms(struct drm_connector *connector, int mode)
  1587. {
  1588. return 0;
  1589. }
  1590. enum drm_connector_status
  1591. vmw_du_connector_detect(struct drm_connector *connector, bool force)
  1592. {
  1593. uint32_t num_displays;
  1594. struct drm_device *dev = connector->dev;
  1595. struct vmw_private *dev_priv = vmw_priv(dev);
  1596. struct vmw_display_unit *du = vmw_connector_to_du(connector);
  1597. num_displays = vmw_read(dev_priv, SVGA_REG_NUM_DISPLAYS);
  1598. return ((vmw_connector_to_du(connector)->unit < num_displays &&
  1599. du->pref_active) ?
  1600. connector_status_connected : connector_status_disconnected);
  1601. }
  1602. static struct drm_display_mode vmw_kms_connector_builtin[] = {
  1603. /* 640x480@60Hz */
  1604. { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 25175, 640, 656,
  1605. 752, 800, 0, 480, 489, 492, 525, 0,
  1606. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
  1607. /* 800x600@60Hz */
  1608. { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 40000, 800, 840,
  1609. 968, 1056, 0, 600, 601, 605, 628, 0,
  1610. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1611. /* 1024x768@60Hz */
  1612. { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 65000, 1024, 1048,
  1613. 1184, 1344, 0, 768, 771, 777, 806, 0,
  1614. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
  1615. /* 1152x864@75Hz */
  1616. { DRM_MODE("1152x864", DRM_MODE_TYPE_DRIVER, 108000, 1152, 1216,
  1617. 1344, 1600, 0, 864, 865, 868, 900, 0,
  1618. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1619. /* 1280x768@60Hz */
  1620. { DRM_MODE("1280x768", DRM_MODE_TYPE_DRIVER, 79500, 1280, 1344,
  1621. 1472, 1664, 0, 768, 771, 778, 798, 0,
  1622. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1623. /* 1280x800@60Hz */
  1624. { DRM_MODE("1280x800", DRM_MODE_TYPE_DRIVER, 83500, 1280, 1352,
  1625. 1480, 1680, 0, 800, 803, 809, 831, 0,
  1626. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
  1627. /* 1280x960@60Hz */
  1628. { DRM_MODE("1280x960", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1376,
  1629. 1488, 1800, 0, 960, 961, 964, 1000, 0,
  1630. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1631. /* 1280x1024@60Hz */
  1632. { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1328,
  1633. 1440, 1688, 0, 1024, 1025, 1028, 1066, 0,
  1634. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1635. /* 1360x768@60Hz */
  1636. { DRM_MODE("1360x768", DRM_MODE_TYPE_DRIVER, 85500, 1360, 1424,
  1637. 1536, 1792, 0, 768, 771, 777, 795, 0,
  1638. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1639. /* 1440x1050@60Hz */
  1640. { DRM_MODE("1400x1050", DRM_MODE_TYPE_DRIVER, 121750, 1400, 1488,
  1641. 1632, 1864, 0, 1050, 1053, 1057, 1089, 0,
  1642. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1643. /* 1440x900@60Hz */
  1644. { DRM_MODE("1440x900", DRM_MODE_TYPE_DRIVER, 106500, 1440, 1520,
  1645. 1672, 1904, 0, 900, 903, 909, 934, 0,
  1646. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1647. /* 1600x1200@60Hz */
  1648. { DRM_MODE("1600x1200", DRM_MODE_TYPE_DRIVER, 162000, 1600, 1664,
  1649. 1856, 2160, 0, 1200, 1201, 1204, 1250, 0,
  1650. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1651. /* 1680x1050@60Hz */
  1652. { DRM_MODE("1680x1050", DRM_MODE_TYPE_DRIVER, 146250, 1680, 1784,
  1653. 1960, 2240, 0, 1050, 1053, 1059, 1089, 0,
  1654. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1655. /* 1792x1344@60Hz */
  1656. { DRM_MODE("1792x1344", DRM_MODE_TYPE_DRIVER, 204750, 1792, 1920,
  1657. 2120, 2448, 0, 1344, 1345, 1348, 1394, 0,
  1658. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1659. /* 1853x1392@60Hz */
  1660. { DRM_MODE("1856x1392", DRM_MODE_TYPE_DRIVER, 218250, 1856, 1952,
  1661. 2176, 2528, 0, 1392, 1393, 1396, 1439, 0,
  1662. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1663. /* 1920x1200@60Hz */
  1664. { DRM_MODE("1920x1200", DRM_MODE_TYPE_DRIVER, 193250, 1920, 2056,
  1665. 2256, 2592, 0, 1200, 1203, 1209, 1245, 0,
  1666. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1667. /* 1920x1440@60Hz */
  1668. { DRM_MODE("1920x1440", DRM_MODE_TYPE_DRIVER, 234000, 1920, 2048,
  1669. 2256, 2600, 0, 1440, 1441, 1444, 1500, 0,
  1670. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1671. /* 2560x1600@60Hz */
  1672. { DRM_MODE("2560x1600", DRM_MODE_TYPE_DRIVER, 348500, 2560, 2752,
  1673. 3032, 3504, 0, 1600, 1603, 1609, 1658, 0,
  1674. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1675. /* Terminate */
  1676. { DRM_MODE("", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) },
  1677. };
  1678. /**
  1679. * vmw_guess_mode_timing - Provide fake timings for a
  1680. * 60Hz vrefresh mode.
  1681. *
  1682. * @mode - Pointer to a struct drm_display_mode with hdisplay and vdisplay
  1683. * members filled in.
  1684. */
  1685. void vmw_guess_mode_timing(struct drm_display_mode *mode)
  1686. {
  1687. mode->hsync_start = mode->hdisplay + 50;
  1688. mode->hsync_end = mode->hsync_start + 50;
  1689. mode->htotal = mode->hsync_end + 50;
  1690. mode->vsync_start = mode->vdisplay + 50;
  1691. mode->vsync_end = mode->vsync_start + 50;
  1692. mode->vtotal = mode->vsync_end + 50;
  1693. mode->clock = (u32)mode->htotal * (u32)mode->vtotal / 100 * 6;
  1694. mode->vrefresh = drm_mode_vrefresh(mode);
  1695. }
  1696. int vmw_du_connector_fill_modes(struct drm_connector *connector,
  1697. uint32_t max_width, uint32_t max_height)
  1698. {
  1699. struct vmw_display_unit *du = vmw_connector_to_du(connector);
  1700. struct drm_device *dev = connector->dev;
  1701. struct vmw_private *dev_priv = vmw_priv(dev);
  1702. struct drm_display_mode *mode = NULL;
  1703. struct drm_display_mode *bmode;
  1704. struct drm_display_mode prefmode = { DRM_MODE("preferred",
  1705. DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED,
  1706. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1707. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC)
  1708. };
  1709. int i;
  1710. u32 assumed_bpp = 4;
  1711. if (dev_priv->assume_16bpp)
  1712. assumed_bpp = 2;
  1713. if (dev_priv->active_display_unit == vmw_du_screen_target) {
  1714. max_width = min(max_width, dev_priv->stdu_max_width);
  1715. max_width = min(max_width, dev_priv->texture_max_width);
  1716. max_height = min(max_height, dev_priv->stdu_max_height);
  1717. max_height = min(max_height, dev_priv->texture_max_height);
  1718. }
  1719. /* Add preferred mode */
  1720. mode = drm_mode_duplicate(dev, &prefmode);
  1721. if (!mode)
  1722. return 0;
  1723. mode->hdisplay = du->pref_width;
  1724. mode->vdisplay = du->pref_height;
  1725. vmw_guess_mode_timing(mode);
  1726. if (vmw_kms_validate_mode_vram(dev_priv,
  1727. mode->hdisplay * assumed_bpp,
  1728. mode->vdisplay)) {
  1729. drm_mode_probed_add(connector, mode);
  1730. } else {
  1731. drm_mode_destroy(dev, mode);
  1732. mode = NULL;
  1733. }
  1734. if (du->pref_mode) {
  1735. list_del_init(&du->pref_mode->head);
  1736. drm_mode_destroy(dev, du->pref_mode);
  1737. }
  1738. /* mode might be null here, this is intended */
  1739. du->pref_mode = mode;
  1740. for (i = 0; vmw_kms_connector_builtin[i].type != 0; i++) {
  1741. bmode = &vmw_kms_connector_builtin[i];
  1742. if (bmode->hdisplay > max_width ||
  1743. bmode->vdisplay > max_height)
  1744. continue;
  1745. if (!vmw_kms_validate_mode_vram(dev_priv,
  1746. bmode->hdisplay * assumed_bpp,
  1747. bmode->vdisplay))
  1748. continue;
  1749. mode = drm_mode_duplicate(dev, bmode);
  1750. if (!mode)
  1751. return 0;
  1752. mode->vrefresh = drm_mode_vrefresh(mode);
  1753. drm_mode_probed_add(connector, mode);
  1754. }
  1755. drm_mode_connector_list_update(connector);
  1756. /* Move the prefered mode first, help apps pick the right mode. */
  1757. drm_mode_sort(&connector->modes);
  1758. return 1;
  1759. }
  1760. int vmw_du_connector_set_property(struct drm_connector *connector,
  1761. struct drm_property *property,
  1762. uint64_t val)
  1763. {
  1764. struct vmw_display_unit *du = vmw_connector_to_du(connector);
  1765. struct vmw_private *dev_priv = vmw_priv(connector->dev);
  1766. if (property == dev_priv->implicit_placement_property)
  1767. du->is_implicit = val;
  1768. return 0;
  1769. }
  1770. /**
  1771. * vmw_du_connector_atomic_set_property - Atomic version of get property
  1772. *
  1773. * @crtc - crtc the property is associated with
  1774. *
  1775. * Returns:
  1776. * Zero on success, negative errno on failure.
  1777. */
  1778. int
  1779. vmw_du_connector_atomic_set_property(struct drm_connector *connector,
  1780. struct drm_connector_state *state,
  1781. struct drm_property *property,
  1782. uint64_t val)
  1783. {
  1784. struct vmw_private *dev_priv = vmw_priv(connector->dev);
  1785. struct vmw_connector_state *vcs = vmw_connector_state_to_vcs(state);
  1786. struct vmw_display_unit *du = vmw_connector_to_du(connector);
  1787. if (property == dev_priv->implicit_placement_property) {
  1788. vcs->is_implicit = val;
  1789. /*
  1790. * We should really be doing a drm_atomic_commit() to
  1791. * commit the new state, but since this doesn't cause
  1792. * an immedate state change, this is probably ok
  1793. */
  1794. du->is_implicit = vcs->is_implicit;
  1795. } else {
  1796. return -EINVAL;
  1797. }
  1798. return 0;
  1799. }
  1800. /**
  1801. * vmw_du_connector_atomic_get_property - Atomic version of get property
  1802. *
  1803. * @connector - connector the property is associated with
  1804. *
  1805. * Returns:
  1806. * Zero on success, negative errno on failure.
  1807. */
  1808. int
  1809. vmw_du_connector_atomic_get_property(struct drm_connector *connector,
  1810. const struct drm_connector_state *state,
  1811. struct drm_property *property,
  1812. uint64_t *val)
  1813. {
  1814. struct vmw_private *dev_priv = vmw_priv(connector->dev);
  1815. struct vmw_connector_state *vcs = vmw_connector_state_to_vcs(state);
  1816. if (property == dev_priv->implicit_placement_property)
  1817. *val = vcs->is_implicit;
  1818. else {
  1819. DRM_ERROR("Invalid Property %s\n", property->name);
  1820. return -EINVAL;
  1821. }
  1822. return 0;
  1823. }
  1824. int vmw_kms_update_layout_ioctl(struct drm_device *dev, void *data,
  1825. struct drm_file *file_priv)
  1826. {
  1827. struct vmw_private *dev_priv = vmw_priv(dev);
  1828. struct drm_vmw_update_layout_arg *arg =
  1829. (struct drm_vmw_update_layout_arg *)data;
  1830. void __user *user_rects;
  1831. struct drm_vmw_rect *rects;
  1832. unsigned rects_size;
  1833. int ret;
  1834. int i;
  1835. u64 total_pixels = 0;
  1836. struct drm_mode_config *mode_config = &dev->mode_config;
  1837. struct drm_vmw_rect bounding_box = {0};
  1838. if (!arg->num_outputs) {
  1839. struct drm_vmw_rect def_rect = {0, 0, 800, 600};
  1840. vmw_du_update_layout(dev_priv, 1, &def_rect);
  1841. return 0;
  1842. }
  1843. rects_size = arg->num_outputs * sizeof(struct drm_vmw_rect);
  1844. rects = kcalloc(arg->num_outputs, sizeof(struct drm_vmw_rect),
  1845. GFP_KERNEL);
  1846. if (unlikely(!rects))
  1847. return -ENOMEM;
  1848. user_rects = (void __user *)(unsigned long)arg->rects;
  1849. ret = copy_from_user(rects, user_rects, rects_size);
  1850. if (unlikely(ret != 0)) {
  1851. DRM_ERROR("Failed to get rects.\n");
  1852. ret = -EFAULT;
  1853. goto out_free;
  1854. }
  1855. for (i = 0; i < arg->num_outputs; ++i) {
  1856. if (rects[i].x < 0 ||
  1857. rects[i].y < 0 ||
  1858. rects[i].x + rects[i].w > mode_config->max_width ||
  1859. rects[i].y + rects[i].h > mode_config->max_height) {
  1860. DRM_ERROR("Invalid GUI layout.\n");
  1861. ret = -EINVAL;
  1862. goto out_free;
  1863. }
  1864. /*
  1865. * bounding_box.w and bunding_box.h are used as
  1866. * lower-right coordinates
  1867. */
  1868. if (rects[i].x + rects[i].w > bounding_box.w)
  1869. bounding_box.w = rects[i].x + rects[i].w;
  1870. if (rects[i].y + rects[i].h > bounding_box.h)
  1871. bounding_box.h = rects[i].y + rects[i].h;
  1872. total_pixels += (u64) rects[i].w * (u64) rects[i].h;
  1873. }
  1874. if (dev_priv->active_display_unit == vmw_du_screen_target) {
  1875. /*
  1876. * For Screen Targets, the limits for a toplogy are:
  1877. * 1. Bounding box (assuming 32bpp) must be < prim_bb_mem
  1878. * 2. Total pixels (assuming 32bpp) must be < prim_bb_mem
  1879. */
  1880. u64 bb_mem = (u64) bounding_box.w * bounding_box.h * 4;
  1881. u64 pixel_mem = total_pixels * 4;
  1882. if (bb_mem > dev_priv->prim_bb_mem) {
  1883. DRM_ERROR("Topology is beyond supported limits.\n");
  1884. ret = -EINVAL;
  1885. goto out_free;
  1886. }
  1887. if (pixel_mem > dev_priv->prim_bb_mem) {
  1888. DRM_ERROR("Combined output size too large\n");
  1889. ret = -EINVAL;
  1890. goto out_free;
  1891. }
  1892. }
  1893. vmw_du_update_layout(dev_priv, arg->num_outputs, rects);
  1894. out_free:
  1895. kfree(rects);
  1896. return ret;
  1897. }
  1898. /**
  1899. * vmw_kms_helper_dirty - Helper to build commands and perform actions based
  1900. * on a set of cliprects and a set of display units.
  1901. *
  1902. * @dev_priv: Pointer to a device private structure.
  1903. * @framebuffer: Pointer to the framebuffer on which to perform the actions.
  1904. * @clips: A set of struct drm_clip_rect. Either this os @vclips must be NULL.
  1905. * Cliprects are given in framebuffer coordinates.
  1906. * @vclips: A set of struct drm_vmw_rect cliprects. Either this or @clips must
  1907. * be NULL. Cliprects are given in source coordinates.
  1908. * @dest_x: X coordinate offset for the crtc / destination clip rects.
  1909. * @dest_y: Y coordinate offset for the crtc / destination clip rects.
  1910. * @num_clips: Number of cliprects in the @clips or @vclips array.
  1911. * @increment: Integer with which to increment the clip counter when looping.
  1912. * Used to skip a predetermined number of clip rects.
  1913. * @dirty: Closure structure. See the description of struct vmw_kms_dirty.
  1914. */
  1915. int vmw_kms_helper_dirty(struct vmw_private *dev_priv,
  1916. struct vmw_framebuffer *framebuffer,
  1917. const struct drm_clip_rect *clips,
  1918. const struct drm_vmw_rect *vclips,
  1919. s32 dest_x, s32 dest_y,
  1920. int num_clips,
  1921. int increment,
  1922. struct vmw_kms_dirty *dirty)
  1923. {
  1924. struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
  1925. struct drm_crtc *crtc;
  1926. u32 num_units = 0;
  1927. u32 i, k;
  1928. dirty->dev_priv = dev_priv;
  1929. /* If crtc is passed, no need to iterate over other display units */
  1930. if (dirty->crtc) {
  1931. units[num_units++] = vmw_crtc_to_du(dirty->crtc);
  1932. } else {
  1933. list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list,
  1934. head) {
  1935. struct drm_plane *plane = crtc->primary;
  1936. if (plane->state->fb == &framebuffer->base)
  1937. units[num_units++] = vmw_crtc_to_du(crtc);
  1938. }
  1939. }
  1940. for (k = 0; k < num_units; k++) {
  1941. struct vmw_display_unit *unit = units[k];
  1942. s32 crtc_x = unit->crtc.x;
  1943. s32 crtc_y = unit->crtc.y;
  1944. s32 crtc_width = unit->crtc.mode.hdisplay;
  1945. s32 crtc_height = unit->crtc.mode.vdisplay;
  1946. const struct drm_clip_rect *clips_ptr = clips;
  1947. const struct drm_vmw_rect *vclips_ptr = vclips;
  1948. dirty->unit = unit;
  1949. if (dirty->fifo_reserve_size > 0) {
  1950. dirty->cmd = vmw_fifo_reserve(dev_priv,
  1951. dirty->fifo_reserve_size);
  1952. if (!dirty->cmd) {
  1953. DRM_ERROR("Couldn't reserve fifo space "
  1954. "for dirty blits.\n");
  1955. return -ENOMEM;
  1956. }
  1957. memset(dirty->cmd, 0, dirty->fifo_reserve_size);
  1958. }
  1959. dirty->num_hits = 0;
  1960. for (i = 0; i < num_clips; i++, clips_ptr += increment,
  1961. vclips_ptr += increment) {
  1962. s32 clip_left;
  1963. s32 clip_top;
  1964. /*
  1965. * Select clip array type. Note that integer type
  1966. * in @clips is unsigned short, whereas in @vclips
  1967. * it's 32-bit.
  1968. */
  1969. if (clips) {
  1970. dirty->fb_x = (s32) clips_ptr->x1;
  1971. dirty->fb_y = (s32) clips_ptr->y1;
  1972. dirty->unit_x2 = (s32) clips_ptr->x2 + dest_x -
  1973. crtc_x;
  1974. dirty->unit_y2 = (s32) clips_ptr->y2 + dest_y -
  1975. crtc_y;
  1976. } else {
  1977. dirty->fb_x = vclips_ptr->x;
  1978. dirty->fb_y = vclips_ptr->y;
  1979. dirty->unit_x2 = dirty->fb_x + vclips_ptr->w +
  1980. dest_x - crtc_x;
  1981. dirty->unit_y2 = dirty->fb_y + vclips_ptr->h +
  1982. dest_y - crtc_y;
  1983. }
  1984. dirty->unit_x1 = dirty->fb_x + dest_x - crtc_x;
  1985. dirty->unit_y1 = dirty->fb_y + dest_y - crtc_y;
  1986. /* Skip this clip if it's outside the crtc region */
  1987. if (dirty->unit_x1 >= crtc_width ||
  1988. dirty->unit_y1 >= crtc_height ||
  1989. dirty->unit_x2 <= 0 || dirty->unit_y2 <= 0)
  1990. continue;
  1991. /* Clip right and bottom to crtc limits */
  1992. dirty->unit_x2 = min_t(s32, dirty->unit_x2,
  1993. crtc_width);
  1994. dirty->unit_y2 = min_t(s32, dirty->unit_y2,
  1995. crtc_height);
  1996. /* Clip left and top to crtc limits */
  1997. clip_left = min_t(s32, dirty->unit_x1, 0);
  1998. clip_top = min_t(s32, dirty->unit_y1, 0);
  1999. dirty->unit_x1 -= clip_left;
  2000. dirty->unit_y1 -= clip_top;
  2001. dirty->fb_x -= clip_left;
  2002. dirty->fb_y -= clip_top;
  2003. dirty->clip(dirty);
  2004. }
  2005. dirty->fifo_commit(dirty);
  2006. }
  2007. return 0;
  2008. }
  2009. /**
  2010. * vmw_kms_helper_buffer_prepare - Reserve and validate a buffer object before
  2011. * command submission.
  2012. *
  2013. * @dev_priv. Pointer to a device private structure.
  2014. * @buf: The buffer object
  2015. * @interruptible: Whether to perform waits as interruptible.
  2016. * @validate_as_mob: Whether the buffer should be validated as a MOB. If false,
  2017. * The buffer will be validated as a GMR. Already pinned buffers will not be
  2018. * validated.
  2019. *
  2020. * Returns 0 on success, negative error code on failure, -ERESTARTSYS if
  2021. * interrupted by a signal.
  2022. */
  2023. int vmw_kms_helper_buffer_prepare(struct vmw_private *dev_priv,
  2024. struct vmw_dma_buffer *buf,
  2025. bool interruptible,
  2026. bool validate_as_mob,
  2027. bool for_cpu_blit)
  2028. {
  2029. struct ttm_operation_ctx ctx = {
  2030. .interruptible = interruptible,
  2031. .no_wait_gpu = false};
  2032. struct ttm_buffer_object *bo = &buf->base;
  2033. int ret;
  2034. ttm_bo_reserve(bo, false, false, NULL);
  2035. if (for_cpu_blit)
  2036. ret = ttm_bo_validate(bo, &vmw_nonfixed_placement, &ctx);
  2037. else
  2038. ret = vmw_validate_single_buffer(dev_priv, bo, interruptible,
  2039. validate_as_mob);
  2040. if (ret)
  2041. ttm_bo_unreserve(bo);
  2042. return ret;
  2043. }
  2044. /**
  2045. * vmw_kms_helper_buffer_revert - Undo the actions of
  2046. * vmw_kms_helper_buffer_prepare.
  2047. *
  2048. * @res: Pointer to the buffer object.
  2049. *
  2050. * Helper to be used if an error forces the caller to undo the actions of
  2051. * vmw_kms_helper_buffer_prepare.
  2052. */
  2053. void vmw_kms_helper_buffer_revert(struct vmw_dma_buffer *buf)
  2054. {
  2055. if (buf)
  2056. ttm_bo_unreserve(&buf->base);
  2057. }
  2058. /**
  2059. * vmw_kms_helper_buffer_finish - Unreserve and fence a buffer object after
  2060. * kms command submission.
  2061. *
  2062. * @dev_priv: Pointer to a device private structure.
  2063. * @file_priv: Pointer to a struct drm_file representing the caller's
  2064. * connection. Must be set to NULL if @user_fence_rep is NULL, and conversely
  2065. * if non-NULL, @user_fence_rep must be non-NULL.
  2066. * @buf: The buffer object.
  2067. * @out_fence: Optional pointer to a fence pointer. If non-NULL, a
  2068. * ref-counted fence pointer is returned here.
  2069. * @user_fence_rep: Optional pointer to a user-space provided struct
  2070. * drm_vmw_fence_rep. If provided, @file_priv must also be provided and the
  2071. * function copies fence data to user-space in a fail-safe manner.
  2072. */
  2073. void vmw_kms_helper_buffer_finish(struct vmw_private *dev_priv,
  2074. struct drm_file *file_priv,
  2075. struct vmw_dma_buffer *buf,
  2076. struct vmw_fence_obj **out_fence,
  2077. struct drm_vmw_fence_rep __user *
  2078. user_fence_rep)
  2079. {
  2080. struct vmw_fence_obj *fence;
  2081. uint32_t handle;
  2082. int ret;
  2083. ret = vmw_execbuf_fence_commands(file_priv, dev_priv, &fence,
  2084. file_priv ? &handle : NULL);
  2085. if (buf)
  2086. vmw_fence_single_bo(&buf->base, fence);
  2087. if (file_priv)
  2088. vmw_execbuf_copy_fence_user(dev_priv, vmw_fpriv(file_priv),
  2089. ret, user_fence_rep, fence,
  2090. handle, -1, NULL);
  2091. if (out_fence)
  2092. *out_fence = fence;
  2093. else
  2094. vmw_fence_obj_unreference(&fence);
  2095. vmw_kms_helper_buffer_revert(buf);
  2096. }
  2097. /**
  2098. * vmw_kms_helper_resource_revert - Undo the actions of
  2099. * vmw_kms_helper_resource_prepare.
  2100. *
  2101. * @res: Pointer to the resource. Typically a surface.
  2102. *
  2103. * Helper to be used if an error forces the caller to undo the actions of
  2104. * vmw_kms_helper_resource_prepare.
  2105. */
  2106. void vmw_kms_helper_resource_revert(struct vmw_validation_ctx *ctx)
  2107. {
  2108. struct vmw_resource *res = ctx->res;
  2109. vmw_kms_helper_buffer_revert(ctx->buf);
  2110. vmw_dmabuf_unreference(&ctx->buf);
  2111. vmw_resource_unreserve(res, false, NULL, 0);
  2112. mutex_unlock(&res->dev_priv->cmdbuf_mutex);
  2113. }
  2114. /**
  2115. * vmw_kms_helper_resource_prepare - Reserve and validate a resource before
  2116. * command submission.
  2117. *
  2118. * @res: Pointer to the resource. Typically a surface.
  2119. * @interruptible: Whether to perform waits as interruptible.
  2120. *
  2121. * Reserves and validates also the backup buffer if a guest-backed resource.
  2122. * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
  2123. * interrupted by a signal.
  2124. */
  2125. int vmw_kms_helper_resource_prepare(struct vmw_resource *res,
  2126. bool interruptible,
  2127. struct vmw_validation_ctx *ctx)
  2128. {
  2129. int ret = 0;
  2130. ctx->buf = NULL;
  2131. ctx->res = res;
  2132. if (interruptible)
  2133. ret = mutex_lock_interruptible(&res->dev_priv->cmdbuf_mutex);
  2134. else
  2135. mutex_lock(&res->dev_priv->cmdbuf_mutex);
  2136. if (unlikely(ret != 0))
  2137. return -ERESTARTSYS;
  2138. ret = vmw_resource_reserve(res, interruptible, false);
  2139. if (ret)
  2140. goto out_unlock;
  2141. if (res->backup) {
  2142. ret = vmw_kms_helper_buffer_prepare(res->dev_priv, res->backup,
  2143. interruptible,
  2144. res->dev_priv->has_mob,
  2145. false);
  2146. if (ret)
  2147. goto out_unreserve;
  2148. ctx->buf = vmw_dmabuf_reference(res->backup);
  2149. }
  2150. ret = vmw_resource_validate(res);
  2151. if (ret)
  2152. goto out_revert;
  2153. return 0;
  2154. out_revert:
  2155. vmw_kms_helper_buffer_revert(ctx->buf);
  2156. out_unreserve:
  2157. vmw_resource_unreserve(res, false, NULL, 0);
  2158. out_unlock:
  2159. mutex_unlock(&res->dev_priv->cmdbuf_mutex);
  2160. return ret;
  2161. }
  2162. /**
  2163. * vmw_kms_helper_resource_finish - Unreserve and fence a resource after
  2164. * kms command submission.
  2165. *
  2166. * @res: Pointer to the resource. Typically a surface.
  2167. * @out_fence: Optional pointer to a fence pointer. If non-NULL, a
  2168. * ref-counted fence pointer is returned here.
  2169. */
  2170. void vmw_kms_helper_resource_finish(struct vmw_validation_ctx *ctx,
  2171. struct vmw_fence_obj **out_fence)
  2172. {
  2173. struct vmw_resource *res = ctx->res;
  2174. if (ctx->buf || out_fence)
  2175. vmw_kms_helper_buffer_finish(res->dev_priv, NULL, ctx->buf,
  2176. out_fence, NULL);
  2177. vmw_dmabuf_unreference(&ctx->buf);
  2178. vmw_resource_unreserve(res, false, NULL, 0);
  2179. mutex_unlock(&res->dev_priv->cmdbuf_mutex);
  2180. }
  2181. /**
  2182. * vmw_kms_update_proxy - Helper function to update a proxy surface from
  2183. * its backing MOB.
  2184. *
  2185. * @res: Pointer to the surface resource
  2186. * @clips: Clip rects in framebuffer (surface) space.
  2187. * @num_clips: Number of clips in @clips.
  2188. * @increment: Integer with which to increment the clip counter when looping.
  2189. * Used to skip a predetermined number of clip rects.
  2190. *
  2191. * This function makes sure the proxy surface is updated from its backing MOB
  2192. * using the region given by @clips. The surface resource @res and its backing
  2193. * MOB needs to be reserved and validated on call.
  2194. */
  2195. int vmw_kms_update_proxy(struct vmw_resource *res,
  2196. const struct drm_clip_rect *clips,
  2197. unsigned num_clips,
  2198. int increment)
  2199. {
  2200. struct vmw_private *dev_priv = res->dev_priv;
  2201. struct drm_vmw_size *size = &vmw_res_to_srf(res)->base_size;
  2202. struct {
  2203. SVGA3dCmdHeader header;
  2204. SVGA3dCmdUpdateGBImage body;
  2205. } *cmd;
  2206. SVGA3dBox *box;
  2207. size_t copy_size = 0;
  2208. int i;
  2209. if (!clips)
  2210. return 0;
  2211. cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd) * num_clips);
  2212. if (!cmd) {
  2213. DRM_ERROR("Couldn't reserve fifo space for proxy surface "
  2214. "update.\n");
  2215. return -ENOMEM;
  2216. }
  2217. for (i = 0; i < num_clips; ++i, clips += increment, ++cmd) {
  2218. box = &cmd->body.box;
  2219. cmd->header.id = SVGA_3D_CMD_UPDATE_GB_IMAGE;
  2220. cmd->header.size = sizeof(cmd->body);
  2221. cmd->body.image.sid = res->id;
  2222. cmd->body.image.face = 0;
  2223. cmd->body.image.mipmap = 0;
  2224. if (clips->x1 > size->width || clips->x2 > size->width ||
  2225. clips->y1 > size->height || clips->y2 > size->height) {
  2226. DRM_ERROR("Invalid clips outsize of framebuffer.\n");
  2227. return -EINVAL;
  2228. }
  2229. box->x = clips->x1;
  2230. box->y = clips->y1;
  2231. box->z = 0;
  2232. box->w = clips->x2 - clips->x1;
  2233. box->h = clips->y2 - clips->y1;
  2234. box->d = 1;
  2235. copy_size += sizeof(*cmd);
  2236. }
  2237. vmw_fifo_commit(dev_priv, copy_size);
  2238. return 0;
  2239. }
  2240. int vmw_kms_fbdev_init_data(struct vmw_private *dev_priv,
  2241. unsigned unit,
  2242. u32 max_width,
  2243. u32 max_height,
  2244. struct drm_connector **p_con,
  2245. struct drm_crtc **p_crtc,
  2246. struct drm_display_mode **p_mode)
  2247. {
  2248. struct drm_connector *con;
  2249. struct vmw_display_unit *du;
  2250. struct drm_display_mode *mode;
  2251. int i = 0;
  2252. int ret = 0;
  2253. mutex_lock(&dev_priv->dev->mode_config.mutex);
  2254. list_for_each_entry(con, &dev_priv->dev->mode_config.connector_list,
  2255. head) {
  2256. if (i == unit)
  2257. break;
  2258. ++i;
  2259. }
  2260. if (i != unit) {
  2261. DRM_ERROR("Could not find initial display unit.\n");
  2262. ret = -EINVAL;
  2263. goto out_unlock;
  2264. }
  2265. if (list_empty(&con->modes))
  2266. (void) vmw_du_connector_fill_modes(con, max_width, max_height);
  2267. if (list_empty(&con->modes)) {
  2268. DRM_ERROR("Could not find initial display mode.\n");
  2269. ret = -EINVAL;
  2270. goto out_unlock;
  2271. }
  2272. du = vmw_connector_to_du(con);
  2273. *p_con = con;
  2274. *p_crtc = &du->crtc;
  2275. list_for_each_entry(mode, &con->modes, head) {
  2276. if (mode->type & DRM_MODE_TYPE_PREFERRED)
  2277. break;
  2278. }
  2279. if (mode->type & DRM_MODE_TYPE_PREFERRED)
  2280. *p_mode = mode;
  2281. else {
  2282. WARN_ONCE(true, "Could not find initial preferred mode.\n");
  2283. *p_mode = list_first_entry(&con->modes,
  2284. struct drm_display_mode,
  2285. head);
  2286. }
  2287. out_unlock:
  2288. mutex_unlock(&dev_priv->dev->mode_config.mutex);
  2289. return ret;
  2290. }
  2291. /**
  2292. * vmw_kms_del_active - unregister a crtc binding to the implicit framebuffer
  2293. *
  2294. * @dev_priv: Pointer to a device private struct.
  2295. * @du: The display unit of the crtc.
  2296. */
  2297. void vmw_kms_del_active(struct vmw_private *dev_priv,
  2298. struct vmw_display_unit *du)
  2299. {
  2300. mutex_lock(&dev_priv->global_kms_state_mutex);
  2301. if (du->active_implicit) {
  2302. if (--(dev_priv->num_implicit) == 0)
  2303. dev_priv->implicit_fb = NULL;
  2304. du->active_implicit = false;
  2305. }
  2306. mutex_unlock(&dev_priv->global_kms_state_mutex);
  2307. }
  2308. /**
  2309. * vmw_kms_add_active - register a crtc binding to an implicit framebuffer
  2310. *
  2311. * @vmw_priv: Pointer to a device private struct.
  2312. * @du: The display unit of the crtc.
  2313. * @vfb: The implicit framebuffer
  2314. *
  2315. * Registers a binding to an implicit framebuffer.
  2316. */
  2317. void vmw_kms_add_active(struct vmw_private *dev_priv,
  2318. struct vmw_display_unit *du,
  2319. struct vmw_framebuffer *vfb)
  2320. {
  2321. mutex_lock(&dev_priv->global_kms_state_mutex);
  2322. WARN_ON_ONCE(!dev_priv->num_implicit && dev_priv->implicit_fb);
  2323. if (!du->active_implicit && du->is_implicit) {
  2324. dev_priv->implicit_fb = vfb;
  2325. du->active_implicit = true;
  2326. dev_priv->num_implicit++;
  2327. }
  2328. mutex_unlock(&dev_priv->global_kms_state_mutex);
  2329. }
  2330. /**
  2331. * vmw_kms_screen_object_flippable - Check whether we can page-flip a crtc.
  2332. *
  2333. * @dev_priv: Pointer to device-private struct.
  2334. * @crtc: The crtc we want to flip.
  2335. *
  2336. * Returns true or false depending whether it's OK to flip this crtc
  2337. * based on the criterion that we must not have more than one implicit
  2338. * frame-buffer at any one time.
  2339. */
  2340. bool vmw_kms_crtc_flippable(struct vmw_private *dev_priv,
  2341. struct drm_crtc *crtc)
  2342. {
  2343. struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
  2344. bool ret;
  2345. mutex_lock(&dev_priv->global_kms_state_mutex);
  2346. ret = !du->is_implicit || dev_priv->num_implicit == 1;
  2347. mutex_unlock(&dev_priv->global_kms_state_mutex);
  2348. return ret;
  2349. }
  2350. /**
  2351. * vmw_kms_update_implicit_fb - Update the implicit fb.
  2352. *
  2353. * @dev_priv: Pointer to device-private struct.
  2354. * @crtc: The crtc the new implicit frame-buffer is bound to.
  2355. */
  2356. void vmw_kms_update_implicit_fb(struct vmw_private *dev_priv,
  2357. struct drm_crtc *crtc)
  2358. {
  2359. struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
  2360. struct drm_plane *plane = crtc->primary;
  2361. struct vmw_framebuffer *vfb;
  2362. mutex_lock(&dev_priv->global_kms_state_mutex);
  2363. if (!du->is_implicit)
  2364. goto out_unlock;
  2365. vfb = vmw_framebuffer_to_vfb(plane->state->fb);
  2366. WARN_ON_ONCE(dev_priv->num_implicit != 1 &&
  2367. dev_priv->implicit_fb != vfb);
  2368. dev_priv->implicit_fb = vfb;
  2369. out_unlock:
  2370. mutex_unlock(&dev_priv->global_kms_state_mutex);
  2371. }
  2372. /**
  2373. * vmw_kms_create_implicit_placement_proparty - Set up the implicit placement
  2374. * property.
  2375. *
  2376. * @dev_priv: Pointer to a device private struct.
  2377. * @immutable: Whether the property is immutable.
  2378. *
  2379. * Sets up the implicit placement property unless it's already set up.
  2380. */
  2381. void
  2382. vmw_kms_create_implicit_placement_property(struct vmw_private *dev_priv,
  2383. bool immutable)
  2384. {
  2385. if (dev_priv->implicit_placement_property)
  2386. return;
  2387. dev_priv->implicit_placement_property =
  2388. drm_property_create_range(dev_priv->dev,
  2389. immutable ?
  2390. DRM_MODE_PROP_IMMUTABLE : 0,
  2391. "implicit_placement", 0, 1);
  2392. }
  2393. /**
  2394. * vmw_kms_set_config - Wrapper around drm_atomic_helper_set_config
  2395. *
  2396. * @set: The configuration to set.
  2397. *
  2398. * The vmwgfx Xorg driver doesn't assign the mode::type member, which
  2399. * when drm_mode_set_crtcinfo is called as part of the configuration setting
  2400. * causes it to return incorrect crtc dimensions causing severe problems in
  2401. * the vmwgfx modesetting. So explicitly clear that member before calling
  2402. * into drm_atomic_helper_set_config.
  2403. */
  2404. int vmw_kms_set_config(struct drm_mode_set *set,
  2405. struct drm_modeset_acquire_ctx *ctx)
  2406. {
  2407. if (set && set->mode)
  2408. set->mode->type = 0;
  2409. return drm_atomic_helper_set_config(set, ctx);
  2410. }
  2411. /**
  2412. * vmw_kms_suspend - Save modesetting state and turn modesetting off.
  2413. *
  2414. * @dev: Pointer to the drm device
  2415. * Return: 0 on success. Negative error code on failure.
  2416. */
  2417. int vmw_kms_suspend(struct drm_device *dev)
  2418. {
  2419. struct vmw_private *dev_priv = vmw_priv(dev);
  2420. dev_priv->suspend_state = drm_atomic_helper_suspend(dev);
  2421. if (IS_ERR(dev_priv->suspend_state)) {
  2422. int ret = PTR_ERR(dev_priv->suspend_state);
  2423. DRM_ERROR("Failed kms suspend: %d\n", ret);
  2424. dev_priv->suspend_state = NULL;
  2425. return ret;
  2426. }
  2427. return 0;
  2428. }
  2429. /**
  2430. * vmw_kms_resume - Re-enable modesetting and restore state
  2431. *
  2432. * @dev: Pointer to the drm device
  2433. * Return: 0 on success. Negative error code on failure.
  2434. *
  2435. * State is resumed from a previous vmw_kms_suspend(). It's illegal
  2436. * to call this function without a previous vmw_kms_suspend().
  2437. */
  2438. int vmw_kms_resume(struct drm_device *dev)
  2439. {
  2440. struct vmw_private *dev_priv = vmw_priv(dev);
  2441. int ret;
  2442. if (WARN_ON(!dev_priv->suspend_state))
  2443. return 0;
  2444. ret = drm_atomic_helper_resume(dev, dev_priv->suspend_state);
  2445. dev_priv->suspend_state = NULL;
  2446. return ret;
  2447. }
  2448. /**
  2449. * vmw_kms_lost_device - Notify kms that modesetting capabilities will be lost
  2450. *
  2451. * @dev: Pointer to the drm device
  2452. */
  2453. void vmw_kms_lost_device(struct drm_device *dev)
  2454. {
  2455. drm_atomic_helper_shutdown(dev);
  2456. }