vmwgfx_kms.c 80 KB

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