vmwgfx_kms.c 75 KB

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