vmwgfx_kms.c 75 KB

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