vmwgfx_kms.c 76 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926
  1. /**************************************************************************
  2. *
  3. * Copyright © 2009-2015 VMware, Inc., Palo Alto, CA., USA
  4. * All Rights Reserved.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sub license, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice (including the
  15. * next paragraph) shall be included in all copies or substantial portions
  16. * of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  21. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  22. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  23. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  24. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. *
  26. **************************************************************************/
  27. #include "vmwgfx_kms.h"
  28. #include <drm/drm_plane_helper.h>
  29. #include <drm/drm_atomic.h>
  30. #include <drm/drm_atomic_helper.h>
  31. #include <drm/drm_rect.h>
  32. /* Might need a hrtimer here? */
  33. #define VMWGFX_PRESENT_RATE ((HZ / 60 > 0) ? HZ / 60 : 1)
  34. void vmw_du_cleanup(struct vmw_display_unit *du)
  35. {
  36. drm_plane_cleanup(&du->primary);
  37. drm_plane_cleanup(&du->cursor);
  38. drm_connector_unregister(&du->connector);
  39. drm_crtc_cleanup(&du->crtc);
  40. drm_encoder_cleanup(&du->encoder);
  41. drm_connector_cleanup(&du->connector);
  42. }
  43. /*
  44. * Display Unit Cursor functions
  45. */
  46. static int vmw_cursor_update_image(struct vmw_private *dev_priv,
  47. u32 *image, u32 width, u32 height,
  48. u32 hotspotX, u32 hotspotY)
  49. {
  50. struct {
  51. u32 cmd;
  52. SVGAFifoCmdDefineAlphaCursor cursor;
  53. } *cmd;
  54. u32 image_size = width * height * 4;
  55. u32 cmd_size = sizeof(*cmd) + image_size;
  56. if (!image)
  57. return -EINVAL;
  58. cmd = vmw_fifo_reserve(dev_priv, cmd_size);
  59. if (unlikely(cmd == NULL)) {
  60. DRM_ERROR("Fifo reserve failed.\n");
  61. return -ENOMEM;
  62. }
  63. memset(cmd, 0, sizeof(*cmd));
  64. memcpy(&cmd[1], image, image_size);
  65. cmd->cmd = SVGA_CMD_DEFINE_ALPHA_CURSOR;
  66. cmd->cursor.id = 0;
  67. cmd->cursor.width = width;
  68. cmd->cursor.height = height;
  69. cmd->cursor.hotspotX = hotspotX;
  70. cmd->cursor.hotspotY = hotspotY;
  71. vmw_fifo_commit_flush(dev_priv, cmd_size);
  72. return 0;
  73. }
  74. static int vmw_cursor_update_dmabuf(struct vmw_private *dev_priv,
  75. struct vmw_dma_buffer *dmabuf,
  76. u32 width, u32 height,
  77. u32 hotspotX, u32 hotspotY)
  78. {
  79. struct ttm_bo_kmap_obj map;
  80. unsigned long kmap_offset;
  81. unsigned long kmap_num;
  82. void *virtual;
  83. bool dummy;
  84. int ret;
  85. kmap_offset = 0;
  86. kmap_num = (width*height*4 + PAGE_SIZE - 1) >> PAGE_SHIFT;
  87. ret = ttm_bo_reserve(&dmabuf->base, true, false, NULL);
  88. if (unlikely(ret != 0)) {
  89. DRM_ERROR("reserve failed\n");
  90. return -EINVAL;
  91. }
  92. ret = ttm_bo_kmap(&dmabuf->base, kmap_offset, kmap_num, &map);
  93. if (unlikely(ret != 0))
  94. goto err_unreserve;
  95. virtual = ttm_kmap_obj_virtual(&map, &dummy);
  96. ret = vmw_cursor_update_image(dev_priv, virtual, width, height,
  97. hotspotX, hotspotY);
  98. ttm_bo_kunmap(&map);
  99. err_unreserve:
  100. ttm_bo_unreserve(&dmabuf->base);
  101. return ret;
  102. }
  103. static void vmw_cursor_update_position(struct vmw_private *dev_priv,
  104. bool show, int x, int y)
  105. {
  106. u32 *fifo_mem = dev_priv->mmio_virt;
  107. uint32_t count;
  108. spin_lock(&dev_priv->cursor_lock);
  109. vmw_mmio_write(show ? 1 : 0, fifo_mem + SVGA_FIFO_CURSOR_ON);
  110. vmw_mmio_write(x, fifo_mem + SVGA_FIFO_CURSOR_X);
  111. vmw_mmio_write(y, fifo_mem + SVGA_FIFO_CURSOR_Y);
  112. count = vmw_mmio_read(fifo_mem + SVGA_FIFO_CURSOR_COUNT);
  113. vmw_mmio_write(++count, fifo_mem + SVGA_FIFO_CURSOR_COUNT);
  114. spin_unlock(&dev_priv->cursor_lock);
  115. }
  116. void vmw_kms_cursor_snoop(struct vmw_surface *srf,
  117. struct ttm_object_file *tfile,
  118. struct ttm_buffer_object *bo,
  119. SVGA3dCmdHeader *header)
  120. {
  121. struct ttm_bo_kmap_obj map;
  122. unsigned long kmap_offset;
  123. unsigned long kmap_num;
  124. SVGA3dCopyBox *box;
  125. unsigned box_count;
  126. void *virtual;
  127. bool dummy;
  128. struct vmw_dma_cmd {
  129. SVGA3dCmdHeader header;
  130. SVGA3dCmdSurfaceDMA dma;
  131. } *cmd;
  132. int i, ret;
  133. cmd = container_of(header, struct vmw_dma_cmd, header);
  134. /* No snooper installed */
  135. if (!srf->snooper.image)
  136. return;
  137. if (cmd->dma.host.face != 0 || cmd->dma.host.mipmap != 0) {
  138. DRM_ERROR("face and mipmap for cursors should never != 0\n");
  139. return;
  140. }
  141. if (cmd->header.size < 64) {
  142. DRM_ERROR("at least one full copy box must be given\n");
  143. return;
  144. }
  145. box = (SVGA3dCopyBox *)&cmd[1];
  146. box_count = (cmd->header.size - sizeof(SVGA3dCmdSurfaceDMA)) /
  147. sizeof(SVGA3dCopyBox);
  148. if (cmd->dma.guest.ptr.offset % PAGE_SIZE ||
  149. box->x != 0 || box->y != 0 || box->z != 0 ||
  150. box->srcx != 0 || box->srcy != 0 || box->srcz != 0 ||
  151. box->d != 1 || box_count != 1) {
  152. /* TODO handle none page aligned offsets */
  153. /* TODO handle more dst & src != 0 */
  154. /* TODO handle more then one copy */
  155. DRM_ERROR("Cant snoop dma request for cursor!\n");
  156. DRM_ERROR("(%u, %u, %u) (%u, %u, %u) (%ux%ux%u) %u %u\n",
  157. box->srcx, box->srcy, box->srcz,
  158. box->x, box->y, box->z,
  159. box->w, box->h, box->d, box_count,
  160. cmd->dma.guest.ptr.offset);
  161. return;
  162. }
  163. kmap_offset = cmd->dma.guest.ptr.offset >> PAGE_SHIFT;
  164. kmap_num = (64*64*4) >> PAGE_SHIFT;
  165. ret = ttm_bo_reserve(bo, true, false, NULL);
  166. if (unlikely(ret != 0)) {
  167. DRM_ERROR("reserve failed\n");
  168. return;
  169. }
  170. ret = ttm_bo_kmap(bo, kmap_offset, kmap_num, &map);
  171. if (unlikely(ret != 0))
  172. goto err_unreserve;
  173. virtual = ttm_kmap_obj_virtual(&map, &dummy);
  174. if (box->w == 64 && cmd->dma.guest.pitch == 64*4) {
  175. memcpy(srf->snooper.image, virtual, 64*64*4);
  176. } else {
  177. /* Image is unsigned pointer. */
  178. for (i = 0; i < box->h; i++)
  179. memcpy(srf->snooper.image + i * 64,
  180. virtual + i * cmd->dma.guest.pitch,
  181. box->w * 4);
  182. }
  183. srf->snooper.age++;
  184. ttm_bo_kunmap(&map);
  185. err_unreserve:
  186. ttm_bo_unreserve(bo);
  187. }
  188. /**
  189. * vmw_kms_legacy_hotspot_clear - Clear legacy hotspots
  190. *
  191. * @dev_priv: Pointer to the device private struct.
  192. *
  193. * Clears all legacy hotspots.
  194. */
  195. void vmw_kms_legacy_hotspot_clear(struct vmw_private *dev_priv)
  196. {
  197. struct drm_device *dev = dev_priv->dev;
  198. struct vmw_display_unit *du;
  199. struct drm_crtc *crtc;
  200. drm_modeset_lock_all(dev);
  201. drm_for_each_crtc(crtc, dev) {
  202. du = vmw_crtc_to_du(crtc);
  203. du->hotspot_x = 0;
  204. du->hotspot_y = 0;
  205. }
  206. drm_modeset_unlock_all(dev);
  207. }
  208. void vmw_kms_cursor_post_execbuf(struct vmw_private *dev_priv)
  209. {
  210. struct drm_device *dev = dev_priv->dev;
  211. struct vmw_display_unit *du;
  212. struct drm_crtc *crtc;
  213. mutex_lock(&dev->mode_config.mutex);
  214. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  215. du = vmw_crtc_to_du(crtc);
  216. if (!du->cursor_surface ||
  217. du->cursor_age == du->cursor_surface->snooper.age)
  218. continue;
  219. du->cursor_age = du->cursor_surface->snooper.age;
  220. vmw_cursor_update_image(dev_priv,
  221. du->cursor_surface->snooper.image,
  222. 64, 64,
  223. du->hotspot_x + du->core_hotspot_x,
  224. du->hotspot_y + du->core_hotspot_y);
  225. }
  226. mutex_unlock(&dev->mode_config.mutex);
  227. }
  228. void vmw_du_cursor_plane_destroy(struct drm_plane *plane)
  229. {
  230. vmw_cursor_update_position(plane->dev->dev_private, false, 0, 0);
  231. drm_plane_cleanup(plane);
  232. }
  233. void vmw_du_primary_plane_destroy(struct drm_plane *plane)
  234. {
  235. drm_plane_cleanup(plane);
  236. /* Planes are static in our case so we don't free it */
  237. }
  238. /**
  239. * vmw_du_vps_unpin_surf - unpins resource associated with a framebuffer surface
  240. *
  241. * @vps: plane state associated with the display surface
  242. * @unreference: true if we also want to unreference the display.
  243. */
  244. void vmw_du_plane_unpin_surf(struct vmw_plane_state *vps,
  245. bool unreference)
  246. {
  247. if (vps->surf) {
  248. if (vps->pinned) {
  249. vmw_resource_unpin(&vps->surf->res);
  250. vps->pinned--;
  251. }
  252. if (unreference) {
  253. if (vps->pinned)
  254. DRM_ERROR("Surface still pinned\n");
  255. vmw_surface_unreference(&vps->surf);
  256. }
  257. }
  258. }
  259. /**
  260. * vmw_du_plane_cleanup_fb - Unpins the cursor
  261. *
  262. * @plane: display plane
  263. * @old_state: Contains the FB to clean up
  264. *
  265. * Unpins the framebuffer surface
  266. *
  267. * Returns 0 on success
  268. */
  269. void
  270. vmw_du_plane_cleanup_fb(struct drm_plane *plane,
  271. struct drm_plane_state *old_state)
  272. {
  273. struct vmw_plane_state *vps = vmw_plane_state_to_vps(old_state);
  274. vmw_du_plane_unpin_surf(vps, false);
  275. }
  276. /**
  277. * vmw_du_cursor_plane_prepare_fb - Readies the cursor by referencing it
  278. *
  279. * @plane: display plane
  280. * @new_state: info on the new plane state, including the FB
  281. *
  282. * Returns 0 on success
  283. */
  284. int
  285. vmw_du_cursor_plane_prepare_fb(struct drm_plane *plane,
  286. struct drm_plane_state *new_state)
  287. {
  288. struct drm_framebuffer *fb = new_state->fb;
  289. struct vmw_plane_state *vps = vmw_plane_state_to_vps(new_state);
  290. if (vps->surf)
  291. vmw_surface_unreference(&vps->surf);
  292. if (vps->dmabuf)
  293. vmw_dmabuf_unreference(&vps->dmabuf);
  294. if (fb) {
  295. if (vmw_framebuffer_to_vfb(fb)->dmabuf) {
  296. vps->dmabuf = vmw_framebuffer_to_vfbd(fb)->buffer;
  297. vmw_dmabuf_reference(vps->dmabuf);
  298. } else {
  299. vps->surf = vmw_framebuffer_to_vfbs(fb)->surface;
  300. vmw_surface_reference(vps->surf);
  301. }
  302. }
  303. return 0;
  304. }
  305. void
  306. vmw_du_cursor_plane_atomic_update(struct drm_plane *plane,
  307. struct drm_plane_state *old_state)
  308. {
  309. struct drm_crtc *crtc = plane->state->crtc ?: old_state->crtc;
  310. struct vmw_private *dev_priv = vmw_priv(crtc->dev);
  311. struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
  312. struct vmw_plane_state *vps = vmw_plane_state_to_vps(plane->state);
  313. s32 hotspot_x, hotspot_y;
  314. int ret = 0;
  315. hotspot_x = du->hotspot_x;
  316. hotspot_y = du->hotspot_y;
  317. 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. if (crtc->primary->fb) {
  1268. int cpp = crtc->primary->fb->pitches[0] /
  1269. crtc->primary->fb->width;
  1270. requested_bb_mem += crtc->mode.hdisplay * cpp *
  1271. crtc->mode.vdisplay;
  1272. }
  1273. if (requested_bb_mem > dev_priv->prim_bb_mem)
  1274. return -EINVAL;
  1275. }
  1276. }
  1277. return drm_atomic_helper_check(dev, state);
  1278. }
  1279. static const struct drm_mode_config_funcs vmw_kms_funcs = {
  1280. .fb_create = vmw_kms_fb_create,
  1281. .atomic_check = vmw_kms_atomic_check_modeset,
  1282. .atomic_commit = drm_atomic_helper_commit,
  1283. };
  1284. static int vmw_kms_generic_present(struct vmw_private *dev_priv,
  1285. struct drm_file *file_priv,
  1286. struct vmw_framebuffer *vfb,
  1287. struct vmw_surface *surface,
  1288. uint32_t sid,
  1289. int32_t destX, int32_t destY,
  1290. struct drm_vmw_rect *clips,
  1291. uint32_t num_clips)
  1292. {
  1293. return vmw_kms_sou_do_surface_dirty(dev_priv, vfb, NULL, clips,
  1294. &surface->res, destX, destY,
  1295. num_clips, 1, NULL, NULL);
  1296. }
  1297. int vmw_kms_present(struct vmw_private *dev_priv,
  1298. struct drm_file *file_priv,
  1299. struct vmw_framebuffer *vfb,
  1300. struct vmw_surface *surface,
  1301. uint32_t sid,
  1302. int32_t destX, int32_t destY,
  1303. struct drm_vmw_rect *clips,
  1304. uint32_t num_clips)
  1305. {
  1306. int ret;
  1307. switch (dev_priv->active_display_unit) {
  1308. case vmw_du_screen_target:
  1309. ret = vmw_kms_stdu_surface_dirty(dev_priv, vfb, NULL, clips,
  1310. &surface->res, destX, destY,
  1311. num_clips, 1, NULL, NULL);
  1312. break;
  1313. case vmw_du_screen_object:
  1314. ret = vmw_kms_generic_present(dev_priv, file_priv, vfb, surface,
  1315. sid, destX, destY, clips,
  1316. num_clips);
  1317. break;
  1318. default:
  1319. WARN_ONCE(true,
  1320. "Present called with invalid display system.\n");
  1321. ret = -ENOSYS;
  1322. break;
  1323. }
  1324. if (ret)
  1325. return ret;
  1326. vmw_fifo_flush(dev_priv, false);
  1327. return 0;
  1328. }
  1329. static void
  1330. vmw_kms_create_hotplug_mode_update_property(struct vmw_private *dev_priv)
  1331. {
  1332. if (dev_priv->hotplug_mode_update_property)
  1333. return;
  1334. dev_priv->hotplug_mode_update_property =
  1335. drm_property_create_range(dev_priv->dev,
  1336. DRM_MODE_PROP_IMMUTABLE,
  1337. "hotplug_mode_update", 0, 1);
  1338. if (!dev_priv->hotplug_mode_update_property)
  1339. return;
  1340. }
  1341. int vmw_kms_init(struct vmw_private *dev_priv)
  1342. {
  1343. struct drm_device *dev = dev_priv->dev;
  1344. int ret;
  1345. drm_mode_config_init(dev);
  1346. dev->mode_config.funcs = &vmw_kms_funcs;
  1347. dev->mode_config.min_width = 1;
  1348. dev->mode_config.min_height = 1;
  1349. dev->mode_config.max_width = dev_priv->texture_max_width;
  1350. dev->mode_config.max_height = dev_priv->texture_max_height;
  1351. drm_mode_create_suggested_offset_properties(dev);
  1352. vmw_kms_create_hotplug_mode_update_property(dev_priv);
  1353. ret = vmw_kms_stdu_init_display(dev_priv);
  1354. if (ret) {
  1355. ret = vmw_kms_sou_init_display(dev_priv);
  1356. if (ret) /* Fallback */
  1357. ret = vmw_kms_ldu_init_display(dev_priv);
  1358. }
  1359. return ret;
  1360. }
  1361. int vmw_kms_close(struct vmw_private *dev_priv)
  1362. {
  1363. int ret = 0;
  1364. /*
  1365. * Docs says we should take the lock before calling this function
  1366. * but since it destroys encoders and our destructor calls
  1367. * drm_encoder_cleanup which takes the lock we deadlock.
  1368. */
  1369. drm_mode_config_cleanup(dev_priv->dev);
  1370. if (dev_priv->active_display_unit == vmw_du_legacy)
  1371. ret = vmw_kms_ldu_close_display(dev_priv);
  1372. return ret;
  1373. }
  1374. int vmw_kms_cursor_bypass_ioctl(struct drm_device *dev, void *data,
  1375. struct drm_file *file_priv)
  1376. {
  1377. struct drm_vmw_cursor_bypass_arg *arg = data;
  1378. struct vmw_display_unit *du;
  1379. struct drm_crtc *crtc;
  1380. int ret = 0;
  1381. mutex_lock(&dev->mode_config.mutex);
  1382. if (arg->flags & DRM_VMW_CURSOR_BYPASS_ALL) {
  1383. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  1384. du = vmw_crtc_to_du(crtc);
  1385. du->hotspot_x = arg->xhot;
  1386. du->hotspot_y = arg->yhot;
  1387. }
  1388. mutex_unlock(&dev->mode_config.mutex);
  1389. return 0;
  1390. }
  1391. crtc = drm_crtc_find(dev, file_priv, arg->crtc_id);
  1392. if (!crtc) {
  1393. ret = -ENOENT;
  1394. goto out;
  1395. }
  1396. du = vmw_crtc_to_du(crtc);
  1397. du->hotspot_x = arg->xhot;
  1398. du->hotspot_y = arg->yhot;
  1399. out:
  1400. mutex_unlock(&dev->mode_config.mutex);
  1401. return ret;
  1402. }
  1403. int vmw_kms_write_svga(struct vmw_private *vmw_priv,
  1404. unsigned width, unsigned height, unsigned pitch,
  1405. unsigned bpp, unsigned depth)
  1406. {
  1407. if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
  1408. vmw_write(vmw_priv, SVGA_REG_PITCHLOCK, pitch);
  1409. else if (vmw_fifo_have_pitchlock(vmw_priv))
  1410. vmw_mmio_write(pitch, vmw_priv->mmio_virt +
  1411. SVGA_FIFO_PITCHLOCK);
  1412. vmw_write(vmw_priv, SVGA_REG_WIDTH, width);
  1413. vmw_write(vmw_priv, SVGA_REG_HEIGHT, height);
  1414. vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, bpp);
  1415. if (vmw_read(vmw_priv, SVGA_REG_DEPTH) != depth) {
  1416. DRM_ERROR("Invalid depth %u for %u bpp, host expects %u\n",
  1417. depth, bpp, vmw_read(vmw_priv, SVGA_REG_DEPTH));
  1418. return -EINVAL;
  1419. }
  1420. return 0;
  1421. }
  1422. int vmw_kms_save_vga(struct vmw_private *vmw_priv)
  1423. {
  1424. struct vmw_vga_topology_state *save;
  1425. uint32_t i;
  1426. vmw_priv->vga_width = vmw_read(vmw_priv, SVGA_REG_WIDTH);
  1427. vmw_priv->vga_height = vmw_read(vmw_priv, SVGA_REG_HEIGHT);
  1428. vmw_priv->vga_bpp = vmw_read(vmw_priv, SVGA_REG_BITS_PER_PIXEL);
  1429. if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
  1430. vmw_priv->vga_pitchlock =
  1431. vmw_read(vmw_priv, SVGA_REG_PITCHLOCK);
  1432. else if (vmw_fifo_have_pitchlock(vmw_priv))
  1433. vmw_priv->vga_pitchlock = vmw_mmio_read(vmw_priv->mmio_virt +
  1434. SVGA_FIFO_PITCHLOCK);
  1435. if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
  1436. return 0;
  1437. vmw_priv->num_displays = vmw_read(vmw_priv,
  1438. SVGA_REG_NUM_GUEST_DISPLAYS);
  1439. if (vmw_priv->num_displays == 0)
  1440. vmw_priv->num_displays = 1;
  1441. for (i = 0; i < vmw_priv->num_displays; ++i) {
  1442. save = &vmw_priv->vga_save[i];
  1443. vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
  1444. save->primary = vmw_read(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY);
  1445. save->pos_x = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_X);
  1446. save->pos_y = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y);
  1447. save->width = vmw_read(vmw_priv, SVGA_REG_DISPLAY_WIDTH);
  1448. save->height = vmw_read(vmw_priv, SVGA_REG_DISPLAY_HEIGHT);
  1449. vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
  1450. if (i == 0 && vmw_priv->num_displays == 1 &&
  1451. save->width == 0 && save->height == 0) {
  1452. /*
  1453. * It should be fairly safe to assume that these
  1454. * values are uninitialized.
  1455. */
  1456. save->width = vmw_priv->vga_width - save->pos_x;
  1457. save->height = vmw_priv->vga_height - save->pos_y;
  1458. }
  1459. }
  1460. return 0;
  1461. }
  1462. int vmw_kms_restore_vga(struct vmw_private *vmw_priv)
  1463. {
  1464. struct vmw_vga_topology_state *save;
  1465. uint32_t i;
  1466. vmw_write(vmw_priv, SVGA_REG_WIDTH, vmw_priv->vga_width);
  1467. vmw_write(vmw_priv, SVGA_REG_HEIGHT, vmw_priv->vga_height);
  1468. vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, vmw_priv->vga_bpp);
  1469. if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
  1470. vmw_write(vmw_priv, SVGA_REG_PITCHLOCK,
  1471. vmw_priv->vga_pitchlock);
  1472. else if (vmw_fifo_have_pitchlock(vmw_priv))
  1473. vmw_mmio_write(vmw_priv->vga_pitchlock,
  1474. vmw_priv->mmio_virt + SVGA_FIFO_PITCHLOCK);
  1475. if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
  1476. return 0;
  1477. for (i = 0; i < vmw_priv->num_displays; ++i) {
  1478. save = &vmw_priv->vga_save[i];
  1479. vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
  1480. vmw_write(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY, save->primary);
  1481. vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_X, save->pos_x);
  1482. vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y, save->pos_y);
  1483. vmw_write(vmw_priv, SVGA_REG_DISPLAY_WIDTH, save->width);
  1484. vmw_write(vmw_priv, SVGA_REG_DISPLAY_HEIGHT, save->height);
  1485. vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
  1486. }
  1487. return 0;
  1488. }
  1489. bool vmw_kms_validate_mode_vram(struct vmw_private *dev_priv,
  1490. uint32_t pitch,
  1491. uint32_t height)
  1492. {
  1493. return ((u64) pitch * (u64) height) < (u64)
  1494. ((dev_priv->active_display_unit == vmw_du_screen_target) ?
  1495. dev_priv->prim_bb_mem : dev_priv->vram_size);
  1496. }
  1497. /**
  1498. * Function called by DRM code called with vbl_lock held.
  1499. */
  1500. u32 vmw_get_vblank_counter(struct drm_device *dev, unsigned int pipe)
  1501. {
  1502. return 0;
  1503. }
  1504. /**
  1505. * Function called by DRM code called with vbl_lock held.
  1506. */
  1507. int vmw_enable_vblank(struct drm_device *dev, unsigned int pipe)
  1508. {
  1509. return -EINVAL;
  1510. }
  1511. /**
  1512. * Function called by DRM code called with vbl_lock held.
  1513. */
  1514. void vmw_disable_vblank(struct drm_device *dev, unsigned int pipe)
  1515. {
  1516. }
  1517. /*
  1518. * Small shared kms functions.
  1519. */
  1520. static int vmw_du_update_layout(struct vmw_private *dev_priv, unsigned num,
  1521. struct drm_vmw_rect *rects)
  1522. {
  1523. struct drm_device *dev = dev_priv->dev;
  1524. struct vmw_display_unit *du;
  1525. struct drm_connector *con;
  1526. mutex_lock(&dev->mode_config.mutex);
  1527. #if 0
  1528. {
  1529. unsigned int i;
  1530. DRM_INFO("%s: new layout ", __func__);
  1531. for (i = 0; i < num; i++)
  1532. DRM_INFO("(%i, %i %ux%u) ", rects[i].x, rects[i].y,
  1533. rects[i].w, rects[i].h);
  1534. DRM_INFO("\n");
  1535. }
  1536. #endif
  1537. list_for_each_entry(con, &dev->mode_config.connector_list, head) {
  1538. du = vmw_connector_to_du(con);
  1539. if (num > du->unit) {
  1540. du->pref_width = rects[du->unit].w;
  1541. du->pref_height = rects[du->unit].h;
  1542. du->pref_active = true;
  1543. du->gui_x = rects[du->unit].x;
  1544. du->gui_y = rects[du->unit].y;
  1545. drm_object_property_set_value
  1546. (&con->base, dev->mode_config.suggested_x_property,
  1547. du->gui_x);
  1548. drm_object_property_set_value
  1549. (&con->base, dev->mode_config.suggested_y_property,
  1550. du->gui_y);
  1551. } else {
  1552. du->pref_width = 800;
  1553. du->pref_height = 600;
  1554. du->pref_active = false;
  1555. drm_object_property_set_value
  1556. (&con->base, dev->mode_config.suggested_x_property,
  1557. 0);
  1558. drm_object_property_set_value
  1559. (&con->base, dev->mode_config.suggested_y_property,
  1560. 0);
  1561. }
  1562. con->status = vmw_du_connector_detect(con, true);
  1563. }
  1564. mutex_unlock(&dev->mode_config.mutex);
  1565. drm_sysfs_hotplug_event(dev);
  1566. return 0;
  1567. }
  1568. int vmw_du_crtc_gamma_set(struct drm_crtc *crtc,
  1569. u16 *r, u16 *g, u16 *b,
  1570. uint32_t size,
  1571. struct drm_modeset_acquire_ctx *ctx)
  1572. {
  1573. struct vmw_private *dev_priv = vmw_priv(crtc->dev);
  1574. int i;
  1575. for (i = 0; i < size; i++) {
  1576. DRM_DEBUG("%d r/g/b = 0x%04x / 0x%04x / 0x%04x\n", i,
  1577. r[i], g[i], b[i]);
  1578. vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 0, r[i] >> 8);
  1579. vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 1, g[i] >> 8);
  1580. vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 2, b[i] >> 8);
  1581. }
  1582. return 0;
  1583. }
  1584. int vmw_du_connector_dpms(struct drm_connector *connector, int mode)
  1585. {
  1586. return 0;
  1587. }
  1588. enum drm_connector_status
  1589. vmw_du_connector_detect(struct drm_connector *connector, bool force)
  1590. {
  1591. uint32_t num_displays;
  1592. struct drm_device *dev = connector->dev;
  1593. struct vmw_private *dev_priv = vmw_priv(dev);
  1594. struct vmw_display_unit *du = vmw_connector_to_du(connector);
  1595. num_displays = vmw_read(dev_priv, SVGA_REG_NUM_DISPLAYS);
  1596. return ((vmw_connector_to_du(connector)->unit < num_displays &&
  1597. du->pref_active) ?
  1598. connector_status_connected : connector_status_disconnected);
  1599. }
  1600. static struct drm_display_mode vmw_kms_connector_builtin[] = {
  1601. /* 640x480@60Hz */
  1602. { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 25175, 640, 656,
  1603. 752, 800, 0, 480, 489, 492, 525, 0,
  1604. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
  1605. /* 800x600@60Hz */
  1606. { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 40000, 800, 840,
  1607. 968, 1056, 0, 600, 601, 605, 628, 0,
  1608. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1609. /* 1024x768@60Hz */
  1610. { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 65000, 1024, 1048,
  1611. 1184, 1344, 0, 768, 771, 777, 806, 0,
  1612. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
  1613. /* 1152x864@75Hz */
  1614. { DRM_MODE("1152x864", DRM_MODE_TYPE_DRIVER, 108000, 1152, 1216,
  1615. 1344, 1600, 0, 864, 865, 868, 900, 0,
  1616. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1617. /* 1280x768@60Hz */
  1618. { DRM_MODE("1280x768", DRM_MODE_TYPE_DRIVER, 79500, 1280, 1344,
  1619. 1472, 1664, 0, 768, 771, 778, 798, 0,
  1620. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1621. /* 1280x800@60Hz */
  1622. { DRM_MODE("1280x800", DRM_MODE_TYPE_DRIVER, 83500, 1280, 1352,
  1623. 1480, 1680, 0, 800, 803, 809, 831, 0,
  1624. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
  1625. /* 1280x960@60Hz */
  1626. { DRM_MODE("1280x960", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1376,
  1627. 1488, 1800, 0, 960, 961, 964, 1000, 0,
  1628. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1629. /* 1280x1024@60Hz */
  1630. { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1328,
  1631. 1440, 1688, 0, 1024, 1025, 1028, 1066, 0,
  1632. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1633. /* 1360x768@60Hz */
  1634. { DRM_MODE("1360x768", DRM_MODE_TYPE_DRIVER, 85500, 1360, 1424,
  1635. 1536, 1792, 0, 768, 771, 777, 795, 0,
  1636. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1637. /* 1440x1050@60Hz */
  1638. { DRM_MODE("1400x1050", DRM_MODE_TYPE_DRIVER, 121750, 1400, 1488,
  1639. 1632, 1864, 0, 1050, 1053, 1057, 1089, 0,
  1640. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1641. /* 1440x900@60Hz */
  1642. { DRM_MODE("1440x900", DRM_MODE_TYPE_DRIVER, 106500, 1440, 1520,
  1643. 1672, 1904, 0, 900, 903, 909, 934, 0,
  1644. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1645. /* 1600x1200@60Hz */
  1646. { DRM_MODE("1600x1200", DRM_MODE_TYPE_DRIVER, 162000, 1600, 1664,
  1647. 1856, 2160, 0, 1200, 1201, 1204, 1250, 0,
  1648. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1649. /* 1680x1050@60Hz */
  1650. { DRM_MODE("1680x1050", DRM_MODE_TYPE_DRIVER, 146250, 1680, 1784,
  1651. 1960, 2240, 0, 1050, 1053, 1059, 1089, 0,
  1652. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1653. /* 1792x1344@60Hz */
  1654. { DRM_MODE("1792x1344", DRM_MODE_TYPE_DRIVER, 204750, 1792, 1920,
  1655. 2120, 2448, 0, 1344, 1345, 1348, 1394, 0,
  1656. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1657. /* 1853x1392@60Hz */
  1658. { DRM_MODE("1856x1392", DRM_MODE_TYPE_DRIVER, 218250, 1856, 1952,
  1659. 2176, 2528, 0, 1392, 1393, 1396, 1439, 0,
  1660. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1661. /* 1920x1200@60Hz */
  1662. { DRM_MODE("1920x1200", DRM_MODE_TYPE_DRIVER, 193250, 1920, 2056,
  1663. 2256, 2592, 0, 1200, 1203, 1209, 1245, 0,
  1664. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1665. /* 1920x1440@60Hz */
  1666. { DRM_MODE("1920x1440", DRM_MODE_TYPE_DRIVER, 234000, 1920, 2048,
  1667. 2256, 2600, 0, 1440, 1441, 1444, 1500, 0,
  1668. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1669. /* 2560x1600@60Hz */
  1670. { DRM_MODE("2560x1600", DRM_MODE_TYPE_DRIVER, 348500, 2560, 2752,
  1671. 3032, 3504, 0, 1600, 1603, 1609, 1658, 0,
  1672. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1673. /* Terminate */
  1674. { DRM_MODE("", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) },
  1675. };
  1676. /**
  1677. * vmw_guess_mode_timing - Provide fake timings for a
  1678. * 60Hz vrefresh mode.
  1679. *
  1680. * @mode - Pointer to a struct drm_display_mode with hdisplay and vdisplay
  1681. * members filled in.
  1682. */
  1683. void vmw_guess_mode_timing(struct drm_display_mode *mode)
  1684. {
  1685. mode->hsync_start = mode->hdisplay + 50;
  1686. mode->hsync_end = mode->hsync_start + 50;
  1687. mode->htotal = mode->hsync_end + 50;
  1688. mode->vsync_start = mode->vdisplay + 50;
  1689. mode->vsync_end = mode->vsync_start + 50;
  1690. mode->vtotal = mode->vsync_end + 50;
  1691. mode->clock = (u32)mode->htotal * (u32)mode->vtotal / 100 * 6;
  1692. mode->vrefresh = drm_mode_vrefresh(mode);
  1693. }
  1694. int vmw_du_connector_fill_modes(struct drm_connector *connector,
  1695. uint32_t max_width, uint32_t max_height)
  1696. {
  1697. struct vmw_display_unit *du = vmw_connector_to_du(connector);
  1698. struct drm_device *dev = connector->dev;
  1699. struct vmw_private *dev_priv = vmw_priv(dev);
  1700. struct drm_display_mode *mode = NULL;
  1701. struct drm_display_mode *bmode;
  1702. struct drm_display_mode prefmode = { DRM_MODE("preferred",
  1703. DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED,
  1704. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1705. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC)
  1706. };
  1707. int i;
  1708. u32 assumed_bpp = 4;
  1709. if (dev_priv->assume_16bpp)
  1710. assumed_bpp = 2;
  1711. if (dev_priv->active_display_unit == vmw_du_screen_target) {
  1712. max_width = min(max_width, dev_priv->stdu_max_width);
  1713. max_width = min(max_width, dev_priv->texture_max_width);
  1714. max_height = min(max_height, dev_priv->stdu_max_height);
  1715. max_height = min(max_height, dev_priv->texture_max_height);
  1716. }
  1717. /* Add preferred mode */
  1718. mode = drm_mode_duplicate(dev, &prefmode);
  1719. if (!mode)
  1720. return 0;
  1721. mode->hdisplay = du->pref_width;
  1722. mode->vdisplay = du->pref_height;
  1723. vmw_guess_mode_timing(mode);
  1724. if (vmw_kms_validate_mode_vram(dev_priv,
  1725. mode->hdisplay * assumed_bpp,
  1726. mode->vdisplay)) {
  1727. drm_mode_probed_add(connector, mode);
  1728. } else {
  1729. drm_mode_destroy(dev, mode);
  1730. mode = NULL;
  1731. }
  1732. if (du->pref_mode) {
  1733. list_del_init(&du->pref_mode->head);
  1734. drm_mode_destroy(dev, du->pref_mode);
  1735. }
  1736. /* mode might be null here, this is intended */
  1737. du->pref_mode = mode;
  1738. for (i = 0; vmw_kms_connector_builtin[i].type != 0; i++) {
  1739. bmode = &vmw_kms_connector_builtin[i];
  1740. if (bmode->hdisplay > max_width ||
  1741. bmode->vdisplay > max_height)
  1742. continue;
  1743. if (!vmw_kms_validate_mode_vram(dev_priv,
  1744. bmode->hdisplay * assumed_bpp,
  1745. bmode->vdisplay))
  1746. continue;
  1747. mode = drm_mode_duplicate(dev, bmode);
  1748. if (!mode)
  1749. return 0;
  1750. mode->vrefresh = drm_mode_vrefresh(mode);
  1751. drm_mode_probed_add(connector, mode);
  1752. }
  1753. drm_mode_connector_list_update(connector);
  1754. /* Move the prefered mode first, help apps pick the right mode. */
  1755. drm_mode_sort(&connector->modes);
  1756. return 1;
  1757. }
  1758. int vmw_du_connector_set_property(struct drm_connector *connector,
  1759. struct drm_property *property,
  1760. uint64_t val)
  1761. {
  1762. struct vmw_display_unit *du = vmw_connector_to_du(connector);
  1763. struct vmw_private *dev_priv = vmw_priv(connector->dev);
  1764. if (property == dev_priv->implicit_placement_property)
  1765. du->is_implicit = val;
  1766. return 0;
  1767. }
  1768. /**
  1769. * vmw_du_connector_atomic_set_property - Atomic version of get property
  1770. *
  1771. * @crtc - crtc the property is associated with
  1772. *
  1773. * Returns:
  1774. * Zero on success, negative errno on failure.
  1775. */
  1776. int
  1777. vmw_du_connector_atomic_set_property(struct drm_connector *connector,
  1778. struct drm_connector_state *state,
  1779. struct drm_property *property,
  1780. uint64_t val)
  1781. {
  1782. struct vmw_private *dev_priv = vmw_priv(connector->dev);
  1783. struct vmw_connector_state *vcs = vmw_connector_state_to_vcs(state);
  1784. struct vmw_display_unit *du = vmw_connector_to_du(connector);
  1785. if (property == dev_priv->implicit_placement_property) {
  1786. vcs->is_implicit = val;
  1787. /*
  1788. * We should really be doing a drm_atomic_commit() to
  1789. * commit the new state, but since this doesn't cause
  1790. * an immedate state change, this is probably ok
  1791. */
  1792. du->is_implicit = vcs->is_implicit;
  1793. } else {
  1794. return -EINVAL;
  1795. }
  1796. return 0;
  1797. }
  1798. /**
  1799. * vmw_du_connector_atomic_get_property - Atomic version of get property
  1800. *
  1801. * @connector - connector the property is associated with
  1802. *
  1803. * Returns:
  1804. * Zero on success, negative errno on failure.
  1805. */
  1806. int
  1807. vmw_du_connector_atomic_get_property(struct drm_connector *connector,
  1808. const struct drm_connector_state *state,
  1809. struct drm_property *property,
  1810. uint64_t *val)
  1811. {
  1812. struct vmw_private *dev_priv = vmw_priv(connector->dev);
  1813. struct vmw_connector_state *vcs = vmw_connector_state_to_vcs(state);
  1814. if (property == dev_priv->implicit_placement_property)
  1815. *val = vcs->is_implicit;
  1816. else {
  1817. DRM_ERROR("Invalid Property %s\n", property->name);
  1818. return -EINVAL;
  1819. }
  1820. return 0;
  1821. }
  1822. int vmw_kms_update_layout_ioctl(struct drm_device *dev, void *data,
  1823. struct drm_file *file_priv)
  1824. {
  1825. struct vmw_private *dev_priv = vmw_priv(dev);
  1826. struct drm_vmw_update_layout_arg *arg =
  1827. (struct drm_vmw_update_layout_arg *)data;
  1828. void __user *user_rects;
  1829. struct drm_vmw_rect *rects;
  1830. unsigned rects_size;
  1831. int ret;
  1832. int i;
  1833. u64 total_pixels = 0;
  1834. struct drm_mode_config *mode_config = &dev->mode_config;
  1835. struct drm_vmw_rect bounding_box = {0};
  1836. if (!arg->num_outputs) {
  1837. struct drm_vmw_rect def_rect = {0, 0, 800, 600};
  1838. vmw_du_update_layout(dev_priv, 1, &def_rect);
  1839. return 0;
  1840. }
  1841. rects_size = arg->num_outputs * sizeof(struct drm_vmw_rect);
  1842. rects = kcalloc(arg->num_outputs, sizeof(struct drm_vmw_rect),
  1843. GFP_KERNEL);
  1844. if (unlikely(!rects))
  1845. return -ENOMEM;
  1846. user_rects = (void __user *)(unsigned long)arg->rects;
  1847. ret = copy_from_user(rects, user_rects, rects_size);
  1848. if (unlikely(ret != 0)) {
  1849. DRM_ERROR("Failed to get rects.\n");
  1850. ret = -EFAULT;
  1851. goto out_free;
  1852. }
  1853. for (i = 0; i < arg->num_outputs; ++i) {
  1854. if (rects[i].x < 0 ||
  1855. rects[i].y < 0 ||
  1856. rects[i].x + rects[i].w > mode_config->max_width ||
  1857. rects[i].y + rects[i].h > mode_config->max_height) {
  1858. DRM_ERROR("Invalid GUI layout.\n");
  1859. ret = -EINVAL;
  1860. goto out_free;
  1861. }
  1862. /*
  1863. * bounding_box.w and bunding_box.h are used as
  1864. * lower-right coordinates
  1865. */
  1866. if (rects[i].x + rects[i].w > bounding_box.w)
  1867. bounding_box.w = rects[i].x + rects[i].w;
  1868. if (rects[i].y + rects[i].h > bounding_box.h)
  1869. bounding_box.h = rects[i].y + rects[i].h;
  1870. total_pixels += (u64) rects[i].w * (u64) rects[i].h;
  1871. }
  1872. if (dev_priv->active_display_unit == vmw_du_screen_target) {
  1873. /*
  1874. * For Screen Targets, the limits for a toplogy are:
  1875. * 1. Bounding box (assuming 32bpp) must be < prim_bb_mem
  1876. * 2. Total pixels (assuming 32bpp) must be < prim_bb_mem
  1877. */
  1878. u64 bb_mem = (u64) bounding_box.w * bounding_box.h * 4;
  1879. u64 pixel_mem = total_pixels * 4;
  1880. if (bb_mem > dev_priv->prim_bb_mem) {
  1881. DRM_ERROR("Topology is beyond supported limits.\n");
  1882. ret = -EINVAL;
  1883. goto out_free;
  1884. }
  1885. if (pixel_mem > dev_priv->prim_bb_mem) {
  1886. DRM_ERROR("Combined output size too large\n");
  1887. ret = -EINVAL;
  1888. goto out_free;
  1889. }
  1890. }
  1891. vmw_du_update_layout(dev_priv, arg->num_outputs, rects);
  1892. out_free:
  1893. kfree(rects);
  1894. return ret;
  1895. }
  1896. /**
  1897. * vmw_kms_helper_dirty - Helper to build commands and perform actions based
  1898. * on a set of cliprects and a set of display units.
  1899. *
  1900. * @dev_priv: Pointer to a device private structure.
  1901. * @framebuffer: Pointer to the framebuffer on which to perform the actions.
  1902. * @clips: A set of struct drm_clip_rect. Either this os @vclips must be NULL.
  1903. * Cliprects are given in framebuffer coordinates.
  1904. * @vclips: A set of struct drm_vmw_rect cliprects. Either this or @clips must
  1905. * be NULL. Cliprects are given in source coordinates.
  1906. * @dest_x: X coordinate offset for the crtc / destination clip rects.
  1907. * @dest_y: Y coordinate offset for the crtc / destination clip rects.
  1908. * @num_clips: Number of cliprects in the @clips or @vclips array.
  1909. * @increment: Integer with which to increment the clip counter when looping.
  1910. * Used to skip a predetermined number of clip rects.
  1911. * @dirty: Closure structure. See the description of struct vmw_kms_dirty.
  1912. */
  1913. int vmw_kms_helper_dirty(struct vmw_private *dev_priv,
  1914. struct vmw_framebuffer *framebuffer,
  1915. const struct drm_clip_rect *clips,
  1916. const struct drm_vmw_rect *vclips,
  1917. s32 dest_x, s32 dest_y,
  1918. int num_clips,
  1919. int increment,
  1920. struct vmw_kms_dirty *dirty)
  1921. {
  1922. struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
  1923. struct drm_crtc *crtc;
  1924. u32 num_units = 0;
  1925. u32 i, k;
  1926. dirty->dev_priv = dev_priv;
  1927. /* If crtc is passed, no need to iterate over other display units */
  1928. if (dirty->crtc) {
  1929. units[num_units++] = vmw_crtc_to_du(dirty->crtc);
  1930. } else {
  1931. list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list,
  1932. head) {
  1933. if (crtc->primary->fb != &framebuffer->base)
  1934. continue;
  1935. units[num_units++] = vmw_crtc_to_du(crtc);
  1936. }
  1937. }
  1938. for (k = 0; k < num_units; k++) {
  1939. struct vmw_display_unit *unit = units[k];
  1940. s32 crtc_x = unit->crtc.x;
  1941. s32 crtc_y = unit->crtc.y;
  1942. s32 crtc_width = unit->crtc.mode.hdisplay;
  1943. s32 crtc_height = unit->crtc.mode.vdisplay;
  1944. const struct drm_clip_rect *clips_ptr = clips;
  1945. const struct drm_vmw_rect *vclips_ptr = vclips;
  1946. dirty->unit = unit;
  1947. if (dirty->fifo_reserve_size > 0) {
  1948. dirty->cmd = vmw_fifo_reserve(dev_priv,
  1949. dirty->fifo_reserve_size);
  1950. if (!dirty->cmd) {
  1951. DRM_ERROR("Couldn't reserve fifo space "
  1952. "for dirty blits.\n");
  1953. return -ENOMEM;
  1954. }
  1955. memset(dirty->cmd, 0, dirty->fifo_reserve_size);
  1956. }
  1957. dirty->num_hits = 0;
  1958. for (i = 0; i < num_clips; i++, clips_ptr += increment,
  1959. vclips_ptr += increment) {
  1960. s32 clip_left;
  1961. s32 clip_top;
  1962. /*
  1963. * Select clip array type. Note that integer type
  1964. * in @clips is unsigned short, whereas in @vclips
  1965. * it's 32-bit.
  1966. */
  1967. if (clips) {
  1968. dirty->fb_x = (s32) clips_ptr->x1;
  1969. dirty->fb_y = (s32) clips_ptr->y1;
  1970. dirty->unit_x2 = (s32) clips_ptr->x2 + dest_x -
  1971. crtc_x;
  1972. dirty->unit_y2 = (s32) clips_ptr->y2 + dest_y -
  1973. crtc_y;
  1974. } else {
  1975. dirty->fb_x = vclips_ptr->x;
  1976. dirty->fb_y = vclips_ptr->y;
  1977. dirty->unit_x2 = dirty->fb_x + vclips_ptr->w +
  1978. dest_x - crtc_x;
  1979. dirty->unit_y2 = dirty->fb_y + vclips_ptr->h +
  1980. dest_y - crtc_y;
  1981. }
  1982. dirty->unit_x1 = dirty->fb_x + dest_x - crtc_x;
  1983. dirty->unit_y1 = dirty->fb_y + dest_y - crtc_y;
  1984. /* Skip this clip if it's outside the crtc region */
  1985. if (dirty->unit_x1 >= crtc_width ||
  1986. dirty->unit_y1 >= crtc_height ||
  1987. dirty->unit_x2 <= 0 || dirty->unit_y2 <= 0)
  1988. continue;
  1989. /* Clip right and bottom to crtc limits */
  1990. dirty->unit_x2 = min_t(s32, dirty->unit_x2,
  1991. crtc_width);
  1992. dirty->unit_y2 = min_t(s32, dirty->unit_y2,
  1993. crtc_height);
  1994. /* Clip left and top to crtc limits */
  1995. clip_left = min_t(s32, dirty->unit_x1, 0);
  1996. clip_top = min_t(s32, dirty->unit_y1, 0);
  1997. dirty->unit_x1 -= clip_left;
  1998. dirty->unit_y1 -= clip_top;
  1999. dirty->fb_x -= clip_left;
  2000. dirty->fb_y -= clip_top;
  2001. dirty->clip(dirty);
  2002. }
  2003. dirty->fifo_commit(dirty);
  2004. }
  2005. return 0;
  2006. }
  2007. /**
  2008. * vmw_kms_helper_buffer_prepare - Reserve and validate a buffer object before
  2009. * command submission.
  2010. *
  2011. * @dev_priv. Pointer to a device private structure.
  2012. * @buf: The buffer object
  2013. * @interruptible: Whether to perform waits as interruptible.
  2014. * @validate_as_mob: Whether the buffer should be validated as a MOB. If false,
  2015. * The buffer will be validated as a GMR. Already pinned buffers will not be
  2016. * validated.
  2017. *
  2018. * Returns 0 on success, negative error code on failure, -ERESTARTSYS if
  2019. * interrupted by a signal.
  2020. */
  2021. int vmw_kms_helper_buffer_prepare(struct vmw_private *dev_priv,
  2022. struct vmw_dma_buffer *buf,
  2023. bool interruptible,
  2024. bool validate_as_mob,
  2025. bool for_cpu_blit)
  2026. {
  2027. struct ttm_operation_ctx ctx = {
  2028. .interruptible = interruptible,
  2029. .no_wait_gpu = false};
  2030. struct ttm_buffer_object *bo = &buf->base;
  2031. int ret;
  2032. ttm_bo_reserve(bo, false, false, NULL);
  2033. if (for_cpu_blit)
  2034. ret = ttm_bo_validate(bo, &vmw_nonfixed_placement, &ctx);
  2035. else
  2036. ret = vmw_validate_single_buffer(dev_priv, bo, interruptible,
  2037. validate_as_mob);
  2038. if (ret)
  2039. ttm_bo_unreserve(bo);
  2040. return ret;
  2041. }
  2042. /**
  2043. * vmw_kms_helper_buffer_revert - Undo the actions of
  2044. * vmw_kms_helper_buffer_prepare.
  2045. *
  2046. * @res: Pointer to the buffer object.
  2047. *
  2048. * Helper to be used if an error forces the caller to undo the actions of
  2049. * vmw_kms_helper_buffer_prepare.
  2050. */
  2051. void vmw_kms_helper_buffer_revert(struct vmw_dma_buffer *buf)
  2052. {
  2053. if (buf)
  2054. ttm_bo_unreserve(&buf->base);
  2055. }
  2056. /**
  2057. * vmw_kms_helper_buffer_finish - Unreserve and fence a buffer object after
  2058. * kms command submission.
  2059. *
  2060. * @dev_priv: Pointer to a device private structure.
  2061. * @file_priv: Pointer to a struct drm_file representing the caller's
  2062. * connection. Must be set to NULL if @user_fence_rep is NULL, and conversely
  2063. * if non-NULL, @user_fence_rep must be non-NULL.
  2064. * @buf: The buffer object.
  2065. * @out_fence: Optional pointer to a fence pointer. If non-NULL, a
  2066. * ref-counted fence pointer is returned here.
  2067. * @user_fence_rep: Optional pointer to a user-space provided struct
  2068. * drm_vmw_fence_rep. If provided, @file_priv must also be provided and the
  2069. * function copies fence data to user-space in a fail-safe manner.
  2070. */
  2071. void vmw_kms_helper_buffer_finish(struct vmw_private *dev_priv,
  2072. struct drm_file *file_priv,
  2073. struct vmw_dma_buffer *buf,
  2074. struct vmw_fence_obj **out_fence,
  2075. struct drm_vmw_fence_rep __user *
  2076. user_fence_rep)
  2077. {
  2078. struct vmw_fence_obj *fence;
  2079. uint32_t handle;
  2080. int ret;
  2081. ret = vmw_execbuf_fence_commands(file_priv, dev_priv, &fence,
  2082. file_priv ? &handle : NULL);
  2083. if (buf)
  2084. vmw_fence_single_bo(&buf->base, fence);
  2085. if (file_priv)
  2086. vmw_execbuf_copy_fence_user(dev_priv, vmw_fpriv(file_priv),
  2087. ret, user_fence_rep, fence,
  2088. handle, -1, NULL);
  2089. if (out_fence)
  2090. *out_fence = fence;
  2091. else
  2092. vmw_fence_obj_unreference(&fence);
  2093. vmw_kms_helper_buffer_revert(buf);
  2094. }
  2095. /**
  2096. * vmw_kms_helper_resource_revert - Undo the actions of
  2097. * vmw_kms_helper_resource_prepare.
  2098. *
  2099. * @res: Pointer to the resource. Typically a surface.
  2100. *
  2101. * Helper to be used if an error forces the caller to undo the actions of
  2102. * vmw_kms_helper_resource_prepare.
  2103. */
  2104. void vmw_kms_helper_resource_revert(struct vmw_validation_ctx *ctx)
  2105. {
  2106. struct vmw_resource *res = ctx->res;
  2107. vmw_kms_helper_buffer_revert(ctx->buf);
  2108. vmw_dmabuf_unreference(&ctx->buf);
  2109. vmw_resource_unreserve(res, false, NULL, 0);
  2110. mutex_unlock(&res->dev_priv->cmdbuf_mutex);
  2111. }
  2112. /**
  2113. * vmw_kms_helper_resource_prepare - Reserve and validate a resource before
  2114. * command submission.
  2115. *
  2116. * @res: Pointer to the resource. Typically a surface.
  2117. * @interruptible: Whether to perform waits as interruptible.
  2118. *
  2119. * Reserves and validates also the backup buffer if a guest-backed resource.
  2120. * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
  2121. * interrupted by a signal.
  2122. */
  2123. int vmw_kms_helper_resource_prepare(struct vmw_resource *res,
  2124. bool interruptible,
  2125. struct vmw_validation_ctx *ctx)
  2126. {
  2127. int ret = 0;
  2128. ctx->buf = NULL;
  2129. ctx->res = res;
  2130. if (interruptible)
  2131. ret = mutex_lock_interruptible(&res->dev_priv->cmdbuf_mutex);
  2132. else
  2133. mutex_lock(&res->dev_priv->cmdbuf_mutex);
  2134. if (unlikely(ret != 0))
  2135. return -ERESTARTSYS;
  2136. ret = vmw_resource_reserve(res, interruptible, false);
  2137. if (ret)
  2138. goto out_unlock;
  2139. if (res->backup) {
  2140. ret = vmw_kms_helper_buffer_prepare(res->dev_priv, res->backup,
  2141. interruptible,
  2142. res->dev_priv->has_mob,
  2143. false);
  2144. if (ret)
  2145. goto out_unreserve;
  2146. ctx->buf = vmw_dmabuf_reference(res->backup);
  2147. }
  2148. ret = vmw_resource_validate(res);
  2149. if (ret)
  2150. goto out_revert;
  2151. return 0;
  2152. out_revert:
  2153. vmw_kms_helper_buffer_revert(ctx->buf);
  2154. out_unreserve:
  2155. vmw_resource_unreserve(res, false, NULL, 0);
  2156. out_unlock:
  2157. mutex_unlock(&res->dev_priv->cmdbuf_mutex);
  2158. return ret;
  2159. }
  2160. /**
  2161. * vmw_kms_helper_resource_finish - Unreserve and fence a resource after
  2162. * kms command submission.
  2163. *
  2164. * @res: Pointer to the resource. Typically a surface.
  2165. * @out_fence: Optional pointer to a fence pointer. If non-NULL, a
  2166. * ref-counted fence pointer is returned here.
  2167. */
  2168. void vmw_kms_helper_resource_finish(struct vmw_validation_ctx *ctx,
  2169. struct vmw_fence_obj **out_fence)
  2170. {
  2171. struct vmw_resource *res = ctx->res;
  2172. if (ctx->buf || out_fence)
  2173. vmw_kms_helper_buffer_finish(res->dev_priv, NULL, ctx->buf,
  2174. out_fence, NULL);
  2175. vmw_dmabuf_unreference(&ctx->buf);
  2176. vmw_resource_unreserve(res, false, NULL, 0);
  2177. mutex_unlock(&res->dev_priv->cmdbuf_mutex);
  2178. }
  2179. /**
  2180. * vmw_kms_update_proxy - Helper function to update a proxy surface from
  2181. * its backing MOB.
  2182. *
  2183. * @res: Pointer to the surface resource
  2184. * @clips: Clip rects in framebuffer (surface) space.
  2185. * @num_clips: Number of clips in @clips.
  2186. * @increment: Integer with which to increment the clip counter when looping.
  2187. * Used to skip a predetermined number of clip rects.
  2188. *
  2189. * This function makes sure the proxy surface is updated from its backing MOB
  2190. * using the region given by @clips. The surface resource @res and its backing
  2191. * MOB needs to be reserved and validated on call.
  2192. */
  2193. int vmw_kms_update_proxy(struct vmw_resource *res,
  2194. const struct drm_clip_rect *clips,
  2195. unsigned num_clips,
  2196. int increment)
  2197. {
  2198. struct vmw_private *dev_priv = res->dev_priv;
  2199. struct drm_vmw_size *size = &vmw_res_to_srf(res)->base_size;
  2200. struct {
  2201. SVGA3dCmdHeader header;
  2202. SVGA3dCmdUpdateGBImage body;
  2203. } *cmd;
  2204. SVGA3dBox *box;
  2205. size_t copy_size = 0;
  2206. int i;
  2207. if (!clips)
  2208. return 0;
  2209. cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd) * num_clips);
  2210. if (!cmd) {
  2211. DRM_ERROR("Couldn't reserve fifo space for proxy surface "
  2212. "update.\n");
  2213. return -ENOMEM;
  2214. }
  2215. for (i = 0; i < num_clips; ++i, clips += increment, ++cmd) {
  2216. box = &cmd->body.box;
  2217. cmd->header.id = SVGA_3D_CMD_UPDATE_GB_IMAGE;
  2218. cmd->header.size = sizeof(cmd->body);
  2219. cmd->body.image.sid = res->id;
  2220. cmd->body.image.face = 0;
  2221. cmd->body.image.mipmap = 0;
  2222. if (clips->x1 > size->width || clips->x2 > size->width ||
  2223. clips->y1 > size->height || clips->y2 > size->height) {
  2224. DRM_ERROR("Invalid clips outsize of framebuffer.\n");
  2225. return -EINVAL;
  2226. }
  2227. box->x = clips->x1;
  2228. box->y = clips->y1;
  2229. box->z = 0;
  2230. box->w = clips->x2 - clips->x1;
  2231. box->h = clips->y2 - clips->y1;
  2232. box->d = 1;
  2233. copy_size += sizeof(*cmd);
  2234. }
  2235. vmw_fifo_commit(dev_priv, copy_size);
  2236. return 0;
  2237. }
  2238. int vmw_kms_fbdev_init_data(struct vmw_private *dev_priv,
  2239. unsigned unit,
  2240. u32 max_width,
  2241. u32 max_height,
  2242. struct drm_connector **p_con,
  2243. struct drm_crtc **p_crtc,
  2244. struct drm_display_mode **p_mode)
  2245. {
  2246. struct drm_connector *con;
  2247. struct vmw_display_unit *du;
  2248. struct drm_display_mode *mode;
  2249. int i = 0;
  2250. int ret = 0;
  2251. mutex_lock(&dev_priv->dev->mode_config.mutex);
  2252. list_for_each_entry(con, &dev_priv->dev->mode_config.connector_list,
  2253. head) {
  2254. if (i == unit)
  2255. break;
  2256. ++i;
  2257. }
  2258. if (i != unit) {
  2259. DRM_ERROR("Could not find initial display unit.\n");
  2260. ret = -EINVAL;
  2261. goto out_unlock;
  2262. }
  2263. if (list_empty(&con->modes))
  2264. (void) vmw_du_connector_fill_modes(con, max_width, max_height);
  2265. if (list_empty(&con->modes)) {
  2266. DRM_ERROR("Could not find initial display mode.\n");
  2267. ret = -EINVAL;
  2268. goto out_unlock;
  2269. }
  2270. du = vmw_connector_to_du(con);
  2271. *p_con = con;
  2272. *p_crtc = &du->crtc;
  2273. list_for_each_entry(mode, &con->modes, head) {
  2274. if (mode->type & DRM_MODE_TYPE_PREFERRED)
  2275. break;
  2276. }
  2277. if (mode->type & DRM_MODE_TYPE_PREFERRED)
  2278. *p_mode = mode;
  2279. else {
  2280. WARN_ONCE(true, "Could not find initial preferred mode.\n");
  2281. *p_mode = list_first_entry(&con->modes,
  2282. struct drm_display_mode,
  2283. head);
  2284. }
  2285. out_unlock:
  2286. mutex_unlock(&dev_priv->dev->mode_config.mutex);
  2287. return ret;
  2288. }
  2289. /**
  2290. * vmw_kms_del_active - unregister a crtc binding to the implicit framebuffer
  2291. *
  2292. * @dev_priv: Pointer to a device private struct.
  2293. * @du: The display unit of the crtc.
  2294. */
  2295. void vmw_kms_del_active(struct vmw_private *dev_priv,
  2296. struct vmw_display_unit *du)
  2297. {
  2298. mutex_lock(&dev_priv->global_kms_state_mutex);
  2299. if (du->active_implicit) {
  2300. if (--(dev_priv->num_implicit) == 0)
  2301. dev_priv->implicit_fb = NULL;
  2302. du->active_implicit = false;
  2303. }
  2304. mutex_unlock(&dev_priv->global_kms_state_mutex);
  2305. }
  2306. /**
  2307. * vmw_kms_add_active - register a crtc binding to an implicit framebuffer
  2308. *
  2309. * @vmw_priv: Pointer to a device private struct.
  2310. * @du: The display unit of the crtc.
  2311. * @vfb: The implicit framebuffer
  2312. *
  2313. * Registers a binding to an implicit framebuffer.
  2314. */
  2315. void vmw_kms_add_active(struct vmw_private *dev_priv,
  2316. struct vmw_display_unit *du,
  2317. struct vmw_framebuffer *vfb)
  2318. {
  2319. mutex_lock(&dev_priv->global_kms_state_mutex);
  2320. WARN_ON_ONCE(!dev_priv->num_implicit && dev_priv->implicit_fb);
  2321. if (!du->active_implicit && du->is_implicit) {
  2322. dev_priv->implicit_fb = vfb;
  2323. du->active_implicit = true;
  2324. dev_priv->num_implicit++;
  2325. }
  2326. mutex_unlock(&dev_priv->global_kms_state_mutex);
  2327. }
  2328. /**
  2329. * vmw_kms_screen_object_flippable - Check whether we can page-flip a crtc.
  2330. *
  2331. * @dev_priv: Pointer to device-private struct.
  2332. * @crtc: The crtc we want to flip.
  2333. *
  2334. * Returns true or false depending whether it's OK to flip this crtc
  2335. * based on the criterion that we must not have more than one implicit
  2336. * frame-buffer at any one time.
  2337. */
  2338. bool vmw_kms_crtc_flippable(struct vmw_private *dev_priv,
  2339. struct drm_crtc *crtc)
  2340. {
  2341. struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
  2342. bool ret;
  2343. mutex_lock(&dev_priv->global_kms_state_mutex);
  2344. ret = !du->is_implicit || dev_priv->num_implicit == 1;
  2345. mutex_unlock(&dev_priv->global_kms_state_mutex);
  2346. return ret;
  2347. }
  2348. /**
  2349. * vmw_kms_update_implicit_fb - Update the implicit fb.
  2350. *
  2351. * @dev_priv: Pointer to device-private struct.
  2352. * @crtc: The crtc the new implicit frame-buffer is bound to.
  2353. */
  2354. void vmw_kms_update_implicit_fb(struct vmw_private *dev_priv,
  2355. struct drm_crtc *crtc)
  2356. {
  2357. struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
  2358. struct vmw_framebuffer *vfb;
  2359. mutex_lock(&dev_priv->global_kms_state_mutex);
  2360. if (!du->is_implicit)
  2361. goto out_unlock;
  2362. vfb = vmw_framebuffer_to_vfb(crtc->primary->fb);
  2363. WARN_ON_ONCE(dev_priv->num_implicit != 1 &&
  2364. dev_priv->implicit_fb != vfb);
  2365. dev_priv->implicit_fb = vfb;
  2366. out_unlock:
  2367. mutex_unlock(&dev_priv->global_kms_state_mutex);
  2368. }
  2369. /**
  2370. * vmw_kms_create_implicit_placement_proparty - Set up the implicit placement
  2371. * property.
  2372. *
  2373. * @dev_priv: Pointer to a device private struct.
  2374. * @immutable: Whether the property is immutable.
  2375. *
  2376. * Sets up the implicit placement property unless it's already set up.
  2377. */
  2378. void
  2379. vmw_kms_create_implicit_placement_property(struct vmw_private *dev_priv,
  2380. bool immutable)
  2381. {
  2382. if (dev_priv->implicit_placement_property)
  2383. return;
  2384. dev_priv->implicit_placement_property =
  2385. drm_property_create_range(dev_priv->dev,
  2386. immutable ?
  2387. DRM_MODE_PROP_IMMUTABLE : 0,
  2388. "implicit_placement", 0, 1);
  2389. }
  2390. /**
  2391. * vmw_kms_set_config - Wrapper around drm_atomic_helper_set_config
  2392. *
  2393. * @set: The configuration to set.
  2394. *
  2395. * The vmwgfx Xorg driver doesn't assign the mode::type member, which
  2396. * when drm_mode_set_crtcinfo is called as part of the configuration setting
  2397. * causes it to return incorrect crtc dimensions causing severe problems in
  2398. * the vmwgfx modesetting. So explicitly clear that member before calling
  2399. * into drm_atomic_helper_set_config.
  2400. */
  2401. int vmw_kms_set_config(struct drm_mode_set *set,
  2402. struct drm_modeset_acquire_ctx *ctx)
  2403. {
  2404. if (set && set->mode)
  2405. set->mode->type = 0;
  2406. return drm_atomic_helper_set_config(set, ctx);
  2407. }
  2408. /**
  2409. * vmw_kms_suspend - Save modesetting state and turn modesetting off.
  2410. *
  2411. * @dev: Pointer to the drm device
  2412. * Return: 0 on success. Negative error code on failure.
  2413. */
  2414. int vmw_kms_suspend(struct drm_device *dev)
  2415. {
  2416. struct vmw_private *dev_priv = vmw_priv(dev);
  2417. dev_priv->suspend_state = drm_atomic_helper_suspend(dev);
  2418. if (IS_ERR(dev_priv->suspend_state)) {
  2419. int ret = PTR_ERR(dev_priv->suspend_state);
  2420. DRM_ERROR("Failed kms suspend: %d\n", ret);
  2421. dev_priv->suspend_state = NULL;
  2422. return ret;
  2423. }
  2424. return 0;
  2425. }
  2426. /**
  2427. * vmw_kms_resume - Re-enable modesetting and restore state
  2428. *
  2429. * @dev: Pointer to the drm device
  2430. * Return: 0 on success. Negative error code on failure.
  2431. *
  2432. * State is resumed from a previous vmw_kms_suspend(). It's illegal
  2433. * to call this function without a previous vmw_kms_suspend().
  2434. */
  2435. int vmw_kms_resume(struct drm_device *dev)
  2436. {
  2437. struct vmw_private *dev_priv = vmw_priv(dev);
  2438. int ret;
  2439. if (WARN_ON(!dev_priv->suspend_state))
  2440. return 0;
  2441. ret = drm_atomic_helper_resume(dev, dev_priv->suspend_state);
  2442. dev_priv->suspend_state = NULL;
  2443. return ret;
  2444. }
  2445. /**
  2446. * vmw_kms_lost_device - Notify kms that modesetting capabilities will be lost
  2447. *
  2448. * @dev: Pointer to the drm device
  2449. */
  2450. void vmw_kms_lost_device(struct drm_device *dev)
  2451. {
  2452. drm_atomic_helper_shutdown(dev);
  2453. }