vmwgfx_kms.c 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252
  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. /* Might need a hrtimer here? */
  29. #define VMWGFX_PRESENT_RATE ((HZ / 60 > 0) ? HZ / 60 : 1)
  30. void vmw_du_cleanup(struct vmw_display_unit *du)
  31. {
  32. if (du->cursor_surface)
  33. vmw_surface_unreference(&du->cursor_surface);
  34. if (du->cursor_dmabuf)
  35. vmw_dmabuf_unreference(&du->cursor_dmabuf);
  36. drm_connector_unregister(&du->connector);
  37. drm_crtc_cleanup(&du->crtc);
  38. drm_encoder_cleanup(&du->encoder);
  39. drm_connector_cleanup(&du->connector);
  40. }
  41. /*
  42. * Display Unit Cursor functions
  43. */
  44. int vmw_cursor_update_image(struct vmw_private *dev_priv,
  45. u32 *image, u32 width, u32 height,
  46. u32 hotspotX, u32 hotspotY)
  47. {
  48. struct {
  49. u32 cmd;
  50. SVGAFifoCmdDefineAlphaCursor cursor;
  51. } *cmd;
  52. u32 image_size = width * height * 4;
  53. u32 cmd_size = sizeof(*cmd) + image_size;
  54. if (!image)
  55. return -EINVAL;
  56. cmd = vmw_fifo_reserve(dev_priv, cmd_size);
  57. if (unlikely(cmd == NULL)) {
  58. DRM_ERROR("Fifo reserve failed.\n");
  59. return -ENOMEM;
  60. }
  61. memset(cmd, 0, sizeof(*cmd));
  62. memcpy(&cmd[1], image, image_size);
  63. cmd->cmd = SVGA_CMD_DEFINE_ALPHA_CURSOR;
  64. cmd->cursor.id = 0;
  65. cmd->cursor.width = width;
  66. cmd->cursor.height = height;
  67. cmd->cursor.hotspotX = hotspotX;
  68. cmd->cursor.hotspotY = hotspotY;
  69. vmw_fifo_commit_flush(dev_priv, cmd_size);
  70. return 0;
  71. }
  72. int vmw_cursor_update_dmabuf(struct vmw_private *dev_priv,
  73. struct vmw_dma_buffer *dmabuf,
  74. u32 width, u32 height,
  75. u32 hotspotX, u32 hotspotY)
  76. {
  77. struct ttm_bo_kmap_obj map;
  78. unsigned long kmap_offset;
  79. unsigned long kmap_num;
  80. void *virtual;
  81. bool dummy;
  82. int ret;
  83. kmap_offset = 0;
  84. kmap_num = (width*height*4 + PAGE_SIZE - 1) >> PAGE_SHIFT;
  85. ret = ttm_bo_reserve(&dmabuf->base, true, false, NULL);
  86. if (unlikely(ret != 0)) {
  87. DRM_ERROR("reserve failed\n");
  88. return -EINVAL;
  89. }
  90. ret = ttm_bo_kmap(&dmabuf->base, kmap_offset, kmap_num, &map);
  91. if (unlikely(ret != 0))
  92. goto err_unreserve;
  93. virtual = ttm_kmap_obj_virtual(&map, &dummy);
  94. ret = vmw_cursor_update_image(dev_priv, virtual, width, height,
  95. hotspotX, hotspotY);
  96. ttm_bo_kunmap(&map);
  97. err_unreserve:
  98. ttm_bo_unreserve(&dmabuf->base);
  99. return ret;
  100. }
  101. void vmw_cursor_update_position(struct vmw_private *dev_priv,
  102. bool show, int x, int y)
  103. {
  104. u32 *fifo_mem = dev_priv->mmio_virt;
  105. uint32_t count;
  106. vmw_mmio_write(show ? 1 : 0, fifo_mem + SVGA_FIFO_CURSOR_ON);
  107. vmw_mmio_write(x, fifo_mem + SVGA_FIFO_CURSOR_X);
  108. vmw_mmio_write(y, fifo_mem + SVGA_FIFO_CURSOR_Y);
  109. count = vmw_mmio_read(fifo_mem + SVGA_FIFO_CURSOR_COUNT);
  110. vmw_mmio_write(++count, fifo_mem + SVGA_FIFO_CURSOR_COUNT);
  111. }
  112. /*
  113. * vmw_du_crtc_cursor_set2 - Driver cursor_set2 callback.
  114. */
  115. int vmw_du_crtc_cursor_set2(struct drm_crtc *crtc, struct drm_file *file_priv,
  116. uint32_t handle, uint32_t width, uint32_t height,
  117. int32_t hot_x, int32_t hot_y)
  118. {
  119. struct vmw_private *dev_priv = vmw_priv(crtc->dev);
  120. struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
  121. struct vmw_surface *surface = NULL;
  122. struct vmw_dma_buffer *dmabuf = NULL;
  123. s32 hotspot_x, hotspot_y;
  124. int ret;
  125. /*
  126. * FIXME: Unclear whether there's any global state touched by the
  127. * cursor_set function, especially vmw_cursor_update_position looks
  128. * suspicious. For now take the easy route and reacquire all locks. We
  129. * can do this since the caller in the drm core doesn't check anything
  130. * which is protected by any looks.
  131. */
  132. drm_modeset_unlock_crtc(crtc);
  133. drm_modeset_lock_all(dev_priv->dev);
  134. hotspot_x = hot_x + du->hotspot_x;
  135. hotspot_y = hot_y + du->hotspot_y;
  136. /* A lot of the code assumes this */
  137. if (handle && (width != 64 || height != 64)) {
  138. ret = -EINVAL;
  139. goto out;
  140. }
  141. if (handle) {
  142. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  143. ret = vmw_user_lookup_handle(dev_priv, tfile,
  144. handle, &surface, &dmabuf);
  145. if (ret) {
  146. DRM_ERROR("failed to find surface or dmabuf: %i\n", ret);
  147. ret = -EINVAL;
  148. goto out;
  149. }
  150. }
  151. /* need to do this before taking down old image */
  152. if (surface && !surface->snooper.image) {
  153. DRM_ERROR("surface not suitable for cursor\n");
  154. vmw_surface_unreference(&surface);
  155. ret = -EINVAL;
  156. goto out;
  157. }
  158. /* takedown old cursor */
  159. if (du->cursor_surface) {
  160. du->cursor_surface->snooper.crtc = NULL;
  161. vmw_surface_unreference(&du->cursor_surface);
  162. }
  163. if (du->cursor_dmabuf)
  164. vmw_dmabuf_unreference(&du->cursor_dmabuf);
  165. /* setup new image */
  166. ret = 0;
  167. if (surface) {
  168. /* vmw_user_surface_lookup takes one reference */
  169. du->cursor_surface = surface;
  170. du->cursor_surface->snooper.crtc = crtc;
  171. du->cursor_age = du->cursor_surface->snooper.age;
  172. ret = vmw_cursor_update_image(dev_priv, surface->snooper.image,
  173. 64, 64, hotspot_x, hotspot_y);
  174. } else if (dmabuf) {
  175. /* vmw_user_surface_lookup takes one reference */
  176. du->cursor_dmabuf = dmabuf;
  177. ret = vmw_cursor_update_dmabuf(dev_priv, dmabuf, width, height,
  178. hotspot_x, hotspot_y);
  179. } else {
  180. vmw_cursor_update_position(dev_priv, false, 0, 0);
  181. goto out;
  182. }
  183. if (!ret) {
  184. vmw_cursor_update_position(dev_priv, true,
  185. du->cursor_x + hotspot_x,
  186. du->cursor_y + hotspot_y);
  187. du->core_hotspot_x = hot_x;
  188. du->core_hotspot_y = hot_y;
  189. }
  190. out:
  191. drm_modeset_unlock_all(dev_priv->dev);
  192. drm_modeset_lock_crtc(crtc, crtc->cursor);
  193. return ret;
  194. }
  195. int vmw_du_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
  196. {
  197. struct vmw_private *dev_priv = vmw_priv(crtc->dev);
  198. struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
  199. bool shown = du->cursor_surface || du->cursor_dmabuf ? true : false;
  200. du->cursor_x = x + du->set_gui_x;
  201. du->cursor_y = y + du->set_gui_y;
  202. /*
  203. * FIXME: Unclear whether there's any global state touched by the
  204. * cursor_set function, especially vmw_cursor_update_position looks
  205. * suspicious. For now take the easy route and reacquire all locks. We
  206. * can do this since the caller in the drm core doesn't check anything
  207. * which is protected by any looks.
  208. */
  209. drm_modeset_unlock_crtc(crtc);
  210. drm_modeset_lock_all(dev_priv->dev);
  211. vmw_cursor_update_position(dev_priv, shown,
  212. du->cursor_x + du->hotspot_x +
  213. du->core_hotspot_x,
  214. du->cursor_y + du->hotspot_y +
  215. du->core_hotspot_y);
  216. drm_modeset_unlock_all(dev_priv->dev);
  217. drm_modeset_lock_crtc(crtc, crtc->cursor);
  218. return 0;
  219. }
  220. void vmw_kms_cursor_snoop(struct vmw_surface *srf,
  221. struct ttm_object_file *tfile,
  222. struct ttm_buffer_object *bo,
  223. SVGA3dCmdHeader *header)
  224. {
  225. struct ttm_bo_kmap_obj map;
  226. unsigned long kmap_offset;
  227. unsigned long kmap_num;
  228. SVGA3dCopyBox *box;
  229. unsigned box_count;
  230. void *virtual;
  231. bool dummy;
  232. struct vmw_dma_cmd {
  233. SVGA3dCmdHeader header;
  234. SVGA3dCmdSurfaceDMA dma;
  235. } *cmd;
  236. int i, ret;
  237. cmd = container_of(header, struct vmw_dma_cmd, header);
  238. /* No snooper installed */
  239. if (!srf->snooper.image)
  240. return;
  241. if (cmd->dma.host.face != 0 || cmd->dma.host.mipmap != 0) {
  242. DRM_ERROR("face and mipmap for cursors should never != 0\n");
  243. return;
  244. }
  245. if (cmd->header.size < 64) {
  246. DRM_ERROR("at least one full copy box must be given\n");
  247. return;
  248. }
  249. box = (SVGA3dCopyBox *)&cmd[1];
  250. box_count = (cmd->header.size - sizeof(SVGA3dCmdSurfaceDMA)) /
  251. sizeof(SVGA3dCopyBox);
  252. if (cmd->dma.guest.ptr.offset % PAGE_SIZE ||
  253. box->x != 0 || box->y != 0 || box->z != 0 ||
  254. box->srcx != 0 || box->srcy != 0 || box->srcz != 0 ||
  255. box->d != 1 || box_count != 1) {
  256. /* TODO handle none page aligned offsets */
  257. /* TODO handle more dst & src != 0 */
  258. /* TODO handle more then one copy */
  259. DRM_ERROR("Cant snoop dma request for cursor!\n");
  260. DRM_ERROR("(%u, %u, %u) (%u, %u, %u) (%ux%ux%u) %u %u\n",
  261. box->srcx, box->srcy, box->srcz,
  262. box->x, box->y, box->z,
  263. box->w, box->h, box->d, box_count,
  264. cmd->dma.guest.ptr.offset);
  265. return;
  266. }
  267. kmap_offset = cmd->dma.guest.ptr.offset >> PAGE_SHIFT;
  268. kmap_num = (64*64*4) >> PAGE_SHIFT;
  269. ret = ttm_bo_reserve(bo, true, false, NULL);
  270. if (unlikely(ret != 0)) {
  271. DRM_ERROR("reserve failed\n");
  272. return;
  273. }
  274. ret = ttm_bo_kmap(bo, kmap_offset, kmap_num, &map);
  275. if (unlikely(ret != 0))
  276. goto err_unreserve;
  277. virtual = ttm_kmap_obj_virtual(&map, &dummy);
  278. if (box->w == 64 && cmd->dma.guest.pitch == 64*4) {
  279. memcpy(srf->snooper.image, virtual, 64*64*4);
  280. } else {
  281. /* Image is unsigned pointer. */
  282. for (i = 0; i < box->h; i++)
  283. memcpy(srf->snooper.image + i * 64,
  284. virtual + i * cmd->dma.guest.pitch,
  285. box->w * 4);
  286. }
  287. srf->snooper.age++;
  288. ttm_bo_kunmap(&map);
  289. err_unreserve:
  290. ttm_bo_unreserve(bo);
  291. }
  292. /**
  293. * vmw_kms_legacy_hotspot_clear - Clear legacy hotspots
  294. *
  295. * @dev_priv: Pointer to the device private struct.
  296. *
  297. * Clears all legacy hotspots.
  298. */
  299. void vmw_kms_legacy_hotspot_clear(struct vmw_private *dev_priv)
  300. {
  301. struct drm_device *dev = dev_priv->dev;
  302. struct vmw_display_unit *du;
  303. struct drm_crtc *crtc;
  304. drm_modeset_lock_all(dev);
  305. drm_for_each_crtc(crtc, dev) {
  306. du = vmw_crtc_to_du(crtc);
  307. du->hotspot_x = 0;
  308. du->hotspot_y = 0;
  309. }
  310. drm_modeset_unlock_all(dev);
  311. }
  312. void vmw_kms_cursor_post_execbuf(struct vmw_private *dev_priv)
  313. {
  314. struct drm_device *dev = dev_priv->dev;
  315. struct vmw_display_unit *du;
  316. struct drm_crtc *crtc;
  317. mutex_lock(&dev->mode_config.mutex);
  318. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  319. du = vmw_crtc_to_du(crtc);
  320. if (!du->cursor_surface ||
  321. du->cursor_age == du->cursor_surface->snooper.age)
  322. continue;
  323. du->cursor_age = du->cursor_surface->snooper.age;
  324. vmw_cursor_update_image(dev_priv,
  325. du->cursor_surface->snooper.image,
  326. 64, 64,
  327. du->hotspot_x + du->core_hotspot_x,
  328. du->hotspot_y + du->core_hotspot_y);
  329. }
  330. mutex_unlock(&dev->mode_config.mutex);
  331. }
  332. /*
  333. * Generic framebuffer code
  334. */
  335. /*
  336. * Surface framebuffer code
  337. */
  338. static void vmw_framebuffer_surface_destroy(struct drm_framebuffer *framebuffer)
  339. {
  340. struct vmw_framebuffer_surface *vfbs =
  341. vmw_framebuffer_to_vfbs(framebuffer);
  342. drm_framebuffer_cleanup(framebuffer);
  343. vmw_surface_unreference(&vfbs->surface);
  344. if (vfbs->base.user_obj)
  345. ttm_base_object_unref(&vfbs->base.user_obj);
  346. kfree(vfbs);
  347. }
  348. static int vmw_framebuffer_surface_dirty(struct drm_framebuffer *framebuffer,
  349. struct drm_file *file_priv,
  350. unsigned flags, unsigned color,
  351. struct drm_clip_rect *clips,
  352. unsigned num_clips)
  353. {
  354. struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
  355. struct vmw_framebuffer_surface *vfbs =
  356. vmw_framebuffer_to_vfbs(framebuffer);
  357. struct drm_clip_rect norect;
  358. int ret, inc = 1;
  359. /* Legacy Display Unit does not support 3D */
  360. if (dev_priv->active_display_unit == vmw_du_legacy)
  361. return -EINVAL;
  362. drm_modeset_lock_all(dev_priv->dev);
  363. ret = ttm_read_lock(&dev_priv->reservation_sem, true);
  364. if (unlikely(ret != 0)) {
  365. drm_modeset_unlock_all(dev_priv->dev);
  366. return ret;
  367. }
  368. if (!num_clips) {
  369. num_clips = 1;
  370. clips = &norect;
  371. norect.x1 = norect.y1 = 0;
  372. norect.x2 = framebuffer->width;
  373. norect.y2 = framebuffer->height;
  374. } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
  375. num_clips /= 2;
  376. inc = 2; /* skip source rects */
  377. }
  378. if (dev_priv->active_display_unit == vmw_du_screen_object)
  379. ret = vmw_kms_sou_do_surface_dirty(dev_priv, &vfbs->base,
  380. clips, NULL, NULL, 0, 0,
  381. num_clips, inc, NULL);
  382. else
  383. ret = vmw_kms_stdu_surface_dirty(dev_priv, &vfbs->base,
  384. clips, NULL, NULL, 0, 0,
  385. num_clips, inc, NULL);
  386. vmw_fifo_flush(dev_priv, false);
  387. ttm_read_unlock(&dev_priv->reservation_sem);
  388. drm_modeset_unlock_all(dev_priv->dev);
  389. return 0;
  390. }
  391. /**
  392. * vmw_kms_readback - Perform a readback from the screen system to
  393. * a dma-buffer backed framebuffer.
  394. *
  395. * @dev_priv: Pointer to the device private structure.
  396. * @file_priv: Pointer to a struct drm_file identifying the caller.
  397. * Must be set to NULL if @user_fence_rep is NULL.
  398. * @vfb: Pointer to the dma-buffer backed framebuffer.
  399. * @user_fence_rep: User-space provided structure for fence information.
  400. * Must be set to non-NULL if @file_priv is non-NULL.
  401. * @vclips: Array of clip rects.
  402. * @num_clips: Number of clip rects in @vclips.
  403. *
  404. * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
  405. * interrupted.
  406. */
  407. int vmw_kms_readback(struct vmw_private *dev_priv,
  408. struct drm_file *file_priv,
  409. struct vmw_framebuffer *vfb,
  410. struct drm_vmw_fence_rep __user *user_fence_rep,
  411. struct drm_vmw_rect *vclips,
  412. uint32_t num_clips)
  413. {
  414. switch (dev_priv->active_display_unit) {
  415. case vmw_du_screen_object:
  416. return vmw_kms_sou_readback(dev_priv, file_priv, vfb,
  417. user_fence_rep, vclips, num_clips);
  418. case vmw_du_screen_target:
  419. return vmw_kms_stdu_dma(dev_priv, file_priv, vfb,
  420. user_fence_rep, NULL, vclips, num_clips,
  421. 1, false, true);
  422. default:
  423. WARN_ONCE(true,
  424. "Readback called with invalid display system.\n");
  425. }
  426. return -ENOSYS;
  427. }
  428. static const struct drm_framebuffer_funcs vmw_framebuffer_surface_funcs = {
  429. .destroy = vmw_framebuffer_surface_destroy,
  430. .dirty = vmw_framebuffer_surface_dirty,
  431. };
  432. static int vmw_kms_new_framebuffer_surface(struct vmw_private *dev_priv,
  433. struct vmw_surface *surface,
  434. struct vmw_framebuffer **out,
  435. const struct drm_mode_fb_cmd
  436. *mode_cmd,
  437. bool is_dmabuf_proxy)
  438. {
  439. struct drm_device *dev = dev_priv->dev;
  440. struct vmw_framebuffer_surface *vfbs;
  441. enum SVGA3dSurfaceFormat format;
  442. int ret;
  443. /* 3D is only supported on HWv8 and newer hosts */
  444. if (dev_priv->active_display_unit == vmw_du_legacy)
  445. return -ENOSYS;
  446. /*
  447. * Sanity checks.
  448. */
  449. /* Surface must be marked as a scanout. */
  450. if (unlikely(!surface->scanout))
  451. return -EINVAL;
  452. if (unlikely(surface->mip_levels[0] != 1 ||
  453. surface->num_sizes != 1 ||
  454. surface->base_size.width < mode_cmd->width ||
  455. surface->base_size.height < mode_cmd->height ||
  456. surface->base_size.depth != 1)) {
  457. DRM_ERROR("Incompatible surface dimensions "
  458. "for requested mode.\n");
  459. return -EINVAL;
  460. }
  461. switch (mode_cmd->depth) {
  462. case 32:
  463. format = SVGA3D_A8R8G8B8;
  464. break;
  465. case 24:
  466. format = SVGA3D_X8R8G8B8;
  467. break;
  468. case 16:
  469. format = SVGA3D_R5G6B5;
  470. break;
  471. case 15:
  472. format = SVGA3D_A1R5G5B5;
  473. break;
  474. default:
  475. DRM_ERROR("Invalid color depth: %d\n", mode_cmd->depth);
  476. return -EINVAL;
  477. }
  478. /*
  479. * For DX, surface format validation is done when surface->scanout
  480. * is set.
  481. */
  482. if (!dev_priv->has_dx && format != surface->format) {
  483. DRM_ERROR("Invalid surface format for requested mode.\n");
  484. return -EINVAL;
  485. }
  486. vfbs = kzalloc(sizeof(*vfbs), GFP_KERNEL);
  487. if (!vfbs) {
  488. ret = -ENOMEM;
  489. goto out_err1;
  490. }
  491. /* XXX get the first 3 from the surface info */
  492. vfbs->base.base.bits_per_pixel = mode_cmd->bpp;
  493. vfbs->base.base.pitches[0] = mode_cmd->pitch;
  494. vfbs->base.base.depth = mode_cmd->depth;
  495. vfbs->base.base.width = mode_cmd->width;
  496. vfbs->base.base.height = mode_cmd->height;
  497. vfbs->surface = vmw_surface_reference(surface);
  498. vfbs->base.user_handle = mode_cmd->handle;
  499. vfbs->is_dmabuf_proxy = is_dmabuf_proxy;
  500. *out = &vfbs->base;
  501. ret = drm_framebuffer_init(dev, &vfbs->base.base,
  502. &vmw_framebuffer_surface_funcs);
  503. if (ret)
  504. goto out_err2;
  505. return 0;
  506. out_err2:
  507. vmw_surface_unreference(&surface);
  508. kfree(vfbs);
  509. out_err1:
  510. return ret;
  511. }
  512. /*
  513. * Dmabuf framebuffer code
  514. */
  515. static void vmw_framebuffer_dmabuf_destroy(struct drm_framebuffer *framebuffer)
  516. {
  517. struct vmw_framebuffer_dmabuf *vfbd =
  518. vmw_framebuffer_to_vfbd(framebuffer);
  519. drm_framebuffer_cleanup(framebuffer);
  520. vmw_dmabuf_unreference(&vfbd->buffer);
  521. if (vfbd->base.user_obj)
  522. ttm_base_object_unref(&vfbd->base.user_obj);
  523. kfree(vfbd);
  524. }
  525. static int vmw_framebuffer_dmabuf_dirty(struct drm_framebuffer *framebuffer,
  526. struct drm_file *file_priv,
  527. unsigned flags, unsigned color,
  528. struct drm_clip_rect *clips,
  529. unsigned num_clips)
  530. {
  531. struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
  532. struct vmw_framebuffer_dmabuf *vfbd =
  533. vmw_framebuffer_to_vfbd(framebuffer);
  534. struct drm_clip_rect norect;
  535. int ret, increment = 1;
  536. drm_modeset_lock_all(dev_priv->dev);
  537. ret = ttm_read_lock(&dev_priv->reservation_sem, true);
  538. if (unlikely(ret != 0)) {
  539. drm_modeset_unlock_all(dev_priv->dev);
  540. return ret;
  541. }
  542. if (!num_clips) {
  543. num_clips = 1;
  544. clips = &norect;
  545. norect.x1 = norect.y1 = 0;
  546. norect.x2 = framebuffer->width;
  547. norect.y2 = framebuffer->height;
  548. } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
  549. num_clips /= 2;
  550. increment = 2;
  551. }
  552. switch (dev_priv->active_display_unit) {
  553. case vmw_du_screen_target:
  554. ret = vmw_kms_stdu_dma(dev_priv, NULL, &vfbd->base, NULL,
  555. clips, NULL, num_clips, increment,
  556. true, true);
  557. break;
  558. case vmw_du_screen_object:
  559. ret = vmw_kms_sou_do_dmabuf_dirty(dev_priv, &vfbd->base,
  560. clips, NULL, num_clips,
  561. increment, true, NULL);
  562. break;
  563. case vmw_du_legacy:
  564. ret = vmw_kms_ldu_do_dmabuf_dirty(dev_priv, &vfbd->base, 0, 0,
  565. clips, num_clips, increment);
  566. break;
  567. default:
  568. ret = -EINVAL;
  569. WARN_ONCE(true, "Dirty called with invalid display system.\n");
  570. break;
  571. }
  572. vmw_fifo_flush(dev_priv, false);
  573. ttm_read_unlock(&dev_priv->reservation_sem);
  574. drm_modeset_unlock_all(dev_priv->dev);
  575. return ret;
  576. }
  577. static const struct drm_framebuffer_funcs vmw_framebuffer_dmabuf_funcs = {
  578. .destroy = vmw_framebuffer_dmabuf_destroy,
  579. .dirty = vmw_framebuffer_dmabuf_dirty,
  580. };
  581. /**
  582. * Pin the dmabuffer to the start of vram.
  583. */
  584. static int vmw_framebuffer_pin(struct vmw_framebuffer *vfb)
  585. {
  586. struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
  587. struct vmw_dma_buffer *buf;
  588. int ret;
  589. buf = vfb->dmabuf ? vmw_framebuffer_to_vfbd(&vfb->base)->buffer :
  590. vmw_framebuffer_to_vfbs(&vfb->base)->surface->res.backup;
  591. if (!buf)
  592. return 0;
  593. switch (dev_priv->active_display_unit) {
  594. case vmw_du_legacy:
  595. vmw_overlay_pause_all(dev_priv);
  596. ret = vmw_dmabuf_pin_in_start_of_vram(dev_priv, buf, false);
  597. vmw_overlay_resume_all(dev_priv);
  598. break;
  599. case vmw_du_screen_object:
  600. case vmw_du_screen_target:
  601. if (vfb->dmabuf)
  602. return vmw_dmabuf_pin_in_vram_or_gmr(dev_priv, buf,
  603. false);
  604. return vmw_dmabuf_pin_in_placement(dev_priv, buf,
  605. &vmw_mob_placement, false);
  606. default:
  607. return -EINVAL;
  608. }
  609. return ret;
  610. }
  611. static int vmw_framebuffer_unpin(struct vmw_framebuffer *vfb)
  612. {
  613. struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
  614. struct vmw_dma_buffer *buf;
  615. buf = vfb->dmabuf ? vmw_framebuffer_to_vfbd(&vfb->base)->buffer :
  616. vmw_framebuffer_to_vfbs(&vfb->base)->surface->res.backup;
  617. if (WARN_ON(!buf))
  618. return 0;
  619. return vmw_dmabuf_unpin(dev_priv, buf, false);
  620. }
  621. /**
  622. * vmw_create_dmabuf_proxy - create a proxy surface for the DMA buf
  623. *
  624. * @dev: DRM device
  625. * @mode_cmd: parameters for the new surface
  626. * @dmabuf_mob: MOB backing the DMA buf
  627. * @srf_out: newly created surface
  628. *
  629. * When the content FB is a DMA buf, we create a surface as a proxy to the
  630. * same buffer. This way we can do a surface copy rather than a surface DMA.
  631. * This is a more efficient approach
  632. *
  633. * RETURNS:
  634. * 0 on success, error code otherwise
  635. */
  636. static int vmw_create_dmabuf_proxy(struct drm_device *dev,
  637. const struct drm_mode_fb_cmd *mode_cmd,
  638. struct vmw_dma_buffer *dmabuf_mob,
  639. struct vmw_surface **srf_out)
  640. {
  641. uint32_t format;
  642. struct drm_vmw_size content_base_size;
  643. struct vmw_resource *res;
  644. unsigned int bytes_pp;
  645. int ret;
  646. switch (mode_cmd->depth) {
  647. case 32:
  648. case 24:
  649. format = SVGA3D_X8R8G8B8;
  650. bytes_pp = 4;
  651. break;
  652. case 16:
  653. case 15:
  654. format = SVGA3D_R5G6B5;
  655. bytes_pp = 2;
  656. break;
  657. case 8:
  658. format = SVGA3D_P8;
  659. bytes_pp = 1;
  660. break;
  661. default:
  662. DRM_ERROR("Invalid framebuffer format %d\n", mode_cmd->depth);
  663. return -EINVAL;
  664. }
  665. content_base_size.width = mode_cmd->pitch / bytes_pp;
  666. content_base_size.height = mode_cmd->height;
  667. content_base_size.depth = 1;
  668. ret = vmw_surface_gb_priv_define(dev,
  669. 0, /* kernel visible only */
  670. 0, /* flags */
  671. format,
  672. true, /* can be a scanout buffer */
  673. 1, /* num of mip levels */
  674. 0,
  675. 0,
  676. content_base_size,
  677. srf_out);
  678. if (ret) {
  679. DRM_ERROR("Failed to allocate proxy content buffer\n");
  680. return ret;
  681. }
  682. res = &(*srf_out)->res;
  683. /* Reserve and switch the backing mob. */
  684. mutex_lock(&res->dev_priv->cmdbuf_mutex);
  685. (void) vmw_resource_reserve(res, false, true);
  686. vmw_dmabuf_unreference(&res->backup);
  687. res->backup = vmw_dmabuf_reference(dmabuf_mob);
  688. res->backup_offset = 0;
  689. vmw_resource_unreserve(res, false, NULL, 0);
  690. mutex_unlock(&res->dev_priv->cmdbuf_mutex);
  691. return 0;
  692. }
  693. static int vmw_kms_new_framebuffer_dmabuf(struct vmw_private *dev_priv,
  694. struct vmw_dma_buffer *dmabuf,
  695. struct vmw_framebuffer **out,
  696. const struct drm_mode_fb_cmd
  697. *mode_cmd)
  698. {
  699. struct drm_device *dev = dev_priv->dev;
  700. struct vmw_framebuffer_dmabuf *vfbd;
  701. unsigned int requested_size;
  702. int ret;
  703. requested_size = mode_cmd->height * mode_cmd->pitch;
  704. if (unlikely(requested_size > dmabuf->base.num_pages * PAGE_SIZE)) {
  705. DRM_ERROR("Screen buffer object size is too small "
  706. "for requested mode.\n");
  707. return -EINVAL;
  708. }
  709. /* Limited framebuffer color depth support for screen objects */
  710. if (dev_priv->active_display_unit == vmw_du_screen_object) {
  711. switch (mode_cmd->depth) {
  712. case 32:
  713. case 24:
  714. /* Only support 32 bpp for 32 and 24 depth fbs */
  715. if (mode_cmd->bpp == 32)
  716. break;
  717. DRM_ERROR("Invalid color depth/bbp: %d %d\n",
  718. mode_cmd->depth, mode_cmd->bpp);
  719. return -EINVAL;
  720. case 16:
  721. case 15:
  722. /* Only support 16 bpp for 16 and 15 depth fbs */
  723. if (mode_cmd->bpp == 16)
  724. break;
  725. DRM_ERROR("Invalid color depth/bbp: %d %d\n",
  726. mode_cmd->depth, mode_cmd->bpp);
  727. return -EINVAL;
  728. default:
  729. DRM_ERROR("Invalid color depth: %d\n", mode_cmd->depth);
  730. return -EINVAL;
  731. }
  732. }
  733. vfbd = kzalloc(sizeof(*vfbd), GFP_KERNEL);
  734. if (!vfbd) {
  735. ret = -ENOMEM;
  736. goto out_err1;
  737. }
  738. vfbd->base.base.bits_per_pixel = mode_cmd->bpp;
  739. vfbd->base.base.pitches[0] = mode_cmd->pitch;
  740. vfbd->base.base.depth = mode_cmd->depth;
  741. vfbd->base.base.width = mode_cmd->width;
  742. vfbd->base.base.height = mode_cmd->height;
  743. vfbd->base.dmabuf = true;
  744. vfbd->buffer = vmw_dmabuf_reference(dmabuf);
  745. vfbd->base.user_handle = mode_cmd->handle;
  746. *out = &vfbd->base;
  747. ret = drm_framebuffer_init(dev, &vfbd->base.base,
  748. &vmw_framebuffer_dmabuf_funcs);
  749. if (ret)
  750. goto out_err2;
  751. return 0;
  752. out_err2:
  753. vmw_dmabuf_unreference(&dmabuf);
  754. kfree(vfbd);
  755. out_err1:
  756. return ret;
  757. }
  758. /**
  759. * vmw_kms_new_framebuffer - Create a new framebuffer.
  760. *
  761. * @dev_priv: Pointer to device private struct.
  762. * @dmabuf: Pointer to dma buffer to wrap the kms framebuffer around.
  763. * Either @dmabuf or @surface must be NULL.
  764. * @surface: Pointer to a surface to wrap the kms framebuffer around.
  765. * Either @dmabuf or @surface must be NULL.
  766. * @only_2d: No presents will occur to this dma buffer based framebuffer. This
  767. * Helps the code to do some important optimizations.
  768. * @mode_cmd: Frame-buffer metadata.
  769. */
  770. struct vmw_framebuffer *
  771. vmw_kms_new_framebuffer(struct vmw_private *dev_priv,
  772. struct vmw_dma_buffer *dmabuf,
  773. struct vmw_surface *surface,
  774. bool only_2d,
  775. const struct drm_mode_fb_cmd *mode_cmd)
  776. {
  777. struct vmw_framebuffer *vfb = NULL;
  778. bool is_dmabuf_proxy = false;
  779. int ret;
  780. /*
  781. * We cannot use the SurfaceDMA command in an non-accelerated VM,
  782. * therefore, wrap the DMA buf in a surface so we can use the
  783. * SurfaceCopy command.
  784. */
  785. if (dmabuf && only_2d &&
  786. dev_priv->active_display_unit == vmw_du_screen_target) {
  787. ret = vmw_create_dmabuf_proxy(dev_priv->dev, mode_cmd,
  788. dmabuf, &surface);
  789. if (ret)
  790. return ERR_PTR(ret);
  791. is_dmabuf_proxy = true;
  792. }
  793. /* Create the new framebuffer depending one what we have */
  794. if (surface) {
  795. ret = vmw_kms_new_framebuffer_surface(dev_priv, surface, &vfb,
  796. mode_cmd,
  797. is_dmabuf_proxy);
  798. /*
  799. * vmw_create_dmabuf_proxy() adds a reference that is no longer
  800. * needed
  801. */
  802. if (is_dmabuf_proxy)
  803. vmw_surface_unreference(&surface);
  804. } else if (dmabuf) {
  805. ret = vmw_kms_new_framebuffer_dmabuf(dev_priv, dmabuf, &vfb,
  806. mode_cmd);
  807. } else {
  808. BUG();
  809. }
  810. if (ret)
  811. return ERR_PTR(ret);
  812. vfb->pin = vmw_framebuffer_pin;
  813. vfb->unpin = vmw_framebuffer_unpin;
  814. return vfb;
  815. }
  816. /*
  817. * Generic Kernel modesetting functions
  818. */
  819. static struct drm_framebuffer *vmw_kms_fb_create(struct drm_device *dev,
  820. struct drm_file *file_priv,
  821. const struct drm_mode_fb_cmd2 *mode_cmd2)
  822. {
  823. struct vmw_private *dev_priv = vmw_priv(dev);
  824. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  825. struct vmw_framebuffer *vfb = NULL;
  826. struct vmw_surface *surface = NULL;
  827. struct vmw_dma_buffer *bo = NULL;
  828. struct ttm_base_object *user_obj;
  829. struct drm_mode_fb_cmd mode_cmd;
  830. int ret;
  831. mode_cmd.width = mode_cmd2->width;
  832. mode_cmd.height = mode_cmd2->height;
  833. mode_cmd.pitch = mode_cmd2->pitches[0];
  834. mode_cmd.handle = mode_cmd2->handles[0];
  835. drm_fb_get_bpp_depth(mode_cmd2->pixel_format, &mode_cmd.depth,
  836. &mode_cmd.bpp);
  837. /**
  838. * This code should be conditioned on Screen Objects not being used.
  839. * If screen objects are used, we can allocate a GMR to hold the
  840. * requested framebuffer.
  841. */
  842. if (!vmw_kms_validate_mode_vram(dev_priv,
  843. mode_cmd.pitch,
  844. mode_cmd.height)) {
  845. DRM_ERROR("Requested mode exceed bounding box limit.\n");
  846. return ERR_PTR(-ENOMEM);
  847. }
  848. /*
  849. * Take a reference on the user object of the resource
  850. * backing the kms fb. This ensures that user-space handle
  851. * lookups on that resource will always work as long as
  852. * it's registered with a kms framebuffer. This is important,
  853. * since vmw_execbuf_process identifies resources in the
  854. * command stream using user-space handles.
  855. */
  856. user_obj = ttm_base_object_lookup(tfile, mode_cmd.handle);
  857. if (unlikely(user_obj == NULL)) {
  858. DRM_ERROR("Could not locate requested kms frame buffer.\n");
  859. return ERR_PTR(-ENOENT);
  860. }
  861. /**
  862. * End conditioned code.
  863. */
  864. /* returns either a dmabuf or surface */
  865. ret = vmw_user_lookup_handle(dev_priv, tfile,
  866. mode_cmd.handle,
  867. &surface, &bo);
  868. if (ret)
  869. goto err_out;
  870. vfb = vmw_kms_new_framebuffer(dev_priv, bo, surface,
  871. !(dev_priv->capabilities & SVGA_CAP_3D),
  872. &mode_cmd);
  873. if (IS_ERR(vfb)) {
  874. ret = PTR_ERR(vfb);
  875. goto err_out;
  876. }
  877. err_out:
  878. /* vmw_user_lookup_handle takes one ref so does new_fb */
  879. if (bo)
  880. vmw_dmabuf_unreference(&bo);
  881. if (surface)
  882. vmw_surface_unreference(&surface);
  883. if (ret) {
  884. DRM_ERROR("failed to create vmw_framebuffer: %i\n", ret);
  885. ttm_base_object_unref(&user_obj);
  886. return ERR_PTR(ret);
  887. } else
  888. vfb->user_obj = user_obj;
  889. return &vfb->base;
  890. }
  891. static const struct drm_mode_config_funcs vmw_kms_funcs = {
  892. .fb_create = vmw_kms_fb_create,
  893. };
  894. static int vmw_kms_generic_present(struct vmw_private *dev_priv,
  895. struct drm_file *file_priv,
  896. struct vmw_framebuffer *vfb,
  897. struct vmw_surface *surface,
  898. uint32_t sid,
  899. int32_t destX, int32_t destY,
  900. struct drm_vmw_rect *clips,
  901. uint32_t num_clips)
  902. {
  903. return vmw_kms_sou_do_surface_dirty(dev_priv, vfb, NULL, clips,
  904. &surface->res, destX, destY,
  905. num_clips, 1, NULL);
  906. }
  907. int vmw_kms_present(struct vmw_private *dev_priv,
  908. struct drm_file *file_priv,
  909. struct vmw_framebuffer *vfb,
  910. struct vmw_surface *surface,
  911. uint32_t sid,
  912. int32_t destX, int32_t destY,
  913. struct drm_vmw_rect *clips,
  914. uint32_t num_clips)
  915. {
  916. int ret;
  917. switch (dev_priv->active_display_unit) {
  918. case vmw_du_screen_target:
  919. ret = vmw_kms_stdu_surface_dirty(dev_priv, vfb, NULL, clips,
  920. &surface->res, destX, destY,
  921. num_clips, 1, NULL);
  922. break;
  923. case vmw_du_screen_object:
  924. ret = vmw_kms_generic_present(dev_priv, file_priv, vfb, surface,
  925. sid, destX, destY, clips,
  926. num_clips);
  927. break;
  928. default:
  929. WARN_ONCE(true,
  930. "Present called with invalid display system.\n");
  931. ret = -ENOSYS;
  932. break;
  933. }
  934. if (ret)
  935. return ret;
  936. vmw_fifo_flush(dev_priv, false);
  937. return 0;
  938. }
  939. static void
  940. vmw_kms_create_hotplug_mode_update_property(struct vmw_private *dev_priv)
  941. {
  942. if (dev_priv->hotplug_mode_update_property)
  943. return;
  944. dev_priv->hotplug_mode_update_property =
  945. drm_property_create_range(dev_priv->dev,
  946. DRM_MODE_PROP_IMMUTABLE,
  947. "hotplug_mode_update", 0, 1);
  948. if (!dev_priv->hotplug_mode_update_property)
  949. return;
  950. }
  951. int vmw_kms_init(struct vmw_private *dev_priv)
  952. {
  953. struct drm_device *dev = dev_priv->dev;
  954. int ret;
  955. drm_mode_config_init(dev);
  956. dev->mode_config.funcs = &vmw_kms_funcs;
  957. dev->mode_config.min_width = 1;
  958. dev->mode_config.min_height = 1;
  959. dev->mode_config.max_width = dev_priv->texture_max_width;
  960. dev->mode_config.max_height = dev_priv->texture_max_height;
  961. drm_mode_create_suggested_offset_properties(dev);
  962. vmw_kms_create_hotplug_mode_update_property(dev_priv);
  963. ret = vmw_kms_stdu_init_display(dev_priv);
  964. if (ret) {
  965. ret = vmw_kms_sou_init_display(dev_priv);
  966. if (ret) /* Fallback */
  967. ret = vmw_kms_ldu_init_display(dev_priv);
  968. }
  969. return ret;
  970. }
  971. int vmw_kms_close(struct vmw_private *dev_priv)
  972. {
  973. int ret;
  974. /*
  975. * Docs says we should take the lock before calling this function
  976. * but since it destroys encoders and our destructor calls
  977. * drm_encoder_cleanup which takes the lock we deadlock.
  978. */
  979. drm_mode_config_cleanup(dev_priv->dev);
  980. if (dev_priv->active_display_unit == vmw_du_screen_object)
  981. ret = vmw_kms_sou_close_display(dev_priv);
  982. else if (dev_priv->active_display_unit == vmw_du_screen_target)
  983. ret = vmw_kms_stdu_close_display(dev_priv);
  984. else
  985. ret = vmw_kms_ldu_close_display(dev_priv);
  986. return ret;
  987. }
  988. int vmw_kms_cursor_bypass_ioctl(struct drm_device *dev, void *data,
  989. struct drm_file *file_priv)
  990. {
  991. struct drm_vmw_cursor_bypass_arg *arg = data;
  992. struct vmw_display_unit *du;
  993. struct drm_crtc *crtc;
  994. int ret = 0;
  995. mutex_lock(&dev->mode_config.mutex);
  996. if (arg->flags & DRM_VMW_CURSOR_BYPASS_ALL) {
  997. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  998. du = vmw_crtc_to_du(crtc);
  999. du->hotspot_x = arg->xhot;
  1000. du->hotspot_y = arg->yhot;
  1001. }
  1002. mutex_unlock(&dev->mode_config.mutex);
  1003. return 0;
  1004. }
  1005. crtc = drm_crtc_find(dev, arg->crtc_id);
  1006. if (!crtc) {
  1007. ret = -ENOENT;
  1008. goto out;
  1009. }
  1010. du = vmw_crtc_to_du(crtc);
  1011. du->hotspot_x = arg->xhot;
  1012. du->hotspot_y = arg->yhot;
  1013. out:
  1014. mutex_unlock(&dev->mode_config.mutex);
  1015. return ret;
  1016. }
  1017. int vmw_kms_write_svga(struct vmw_private *vmw_priv,
  1018. unsigned width, unsigned height, unsigned pitch,
  1019. unsigned bpp, unsigned depth)
  1020. {
  1021. if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
  1022. vmw_write(vmw_priv, SVGA_REG_PITCHLOCK, pitch);
  1023. else if (vmw_fifo_have_pitchlock(vmw_priv))
  1024. vmw_mmio_write(pitch, vmw_priv->mmio_virt +
  1025. SVGA_FIFO_PITCHLOCK);
  1026. vmw_write(vmw_priv, SVGA_REG_WIDTH, width);
  1027. vmw_write(vmw_priv, SVGA_REG_HEIGHT, height);
  1028. vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, bpp);
  1029. if (vmw_read(vmw_priv, SVGA_REG_DEPTH) != depth) {
  1030. DRM_ERROR("Invalid depth %u for %u bpp, host expects %u\n",
  1031. depth, bpp, vmw_read(vmw_priv, SVGA_REG_DEPTH));
  1032. return -EINVAL;
  1033. }
  1034. return 0;
  1035. }
  1036. int vmw_kms_save_vga(struct vmw_private *vmw_priv)
  1037. {
  1038. struct vmw_vga_topology_state *save;
  1039. uint32_t i;
  1040. vmw_priv->vga_width = vmw_read(vmw_priv, SVGA_REG_WIDTH);
  1041. vmw_priv->vga_height = vmw_read(vmw_priv, SVGA_REG_HEIGHT);
  1042. vmw_priv->vga_bpp = vmw_read(vmw_priv, SVGA_REG_BITS_PER_PIXEL);
  1043. if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
  1044. vmw_priv->vga_pitchlock =
  1045. vmw_read(vmw_priv, SVGA_REG_PITCHLOCK);
  1046. else if (vmw_fifo_have_pitchlock(vmw_priv))
  1047. vmw_priv->vga_pitchlock = vmw_mmio_read(vmw_priv->mmio_virt +
  1048. SVGA_FIFO_PITCHLOCK);
  1049. if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
  1050. return 0;
  1051. vmw_priv->num_displays = vmw_read(vmw_priv,
  1052. SVGA_REG_NUM_GUEST_DISPLAYS);
  1053. if (vmw_priv->num_displays == 0)
  1054. vmw_priv->num_displays = 1;
  1055. for (i = 0; i < vmw_priv->num_displays; ++i) {
  1056. save = &vmw_priv->vga_save[i];
  1057. vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
  1058. save->primary = vmw_read(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY);
  1059. save->pos_x = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_X);
  1060. save->pos_y = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y);
  1061. save->width = vmw_read(vmw_priv, SVGA_REG_DISPLAY_WIDTH);
  1062. save->height = vmw_read(vmw_priv, SVGA_REG_DISPLAY_HEIGHT);
  1063. vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
  1064. if (i == 0 && vmw_priv->num_displays == 1 &&
  1065. save->width == 0 && save->height == 0) {
  1066. /*
  1067. * It should be fairly safe to assume that these
  1068. * values are uninitialized.
  1069. */
  1070. save->width = vmw_priv->vga_width - save->pos_x;
  1071. save->height = vmw_priv->vga_height - save->pos_y;
  1072. }
  1073. }
  1074. return 0;
  1075. }
  1076. int vmw_kms_restore_vga(struct vmw_private *vmw_priv)
  1077. {
  1078. struct vmw_vga_topology_state *save;
  1079. uint32_t i;
  1080. vmw_write(vmw_priv, SVGA_REG_WIDTH, vmw_priv->vga_width);
  1081. vmw_write(vmw_priv, SVGA_REG_HEIGHT, vmw_priv->vga_height);
  1082. vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, vmw_priv->vga_bpp);
  1083. if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
  1084. vmw_write(vmw_priv, SVGA_REG_PITCHLOCK,
  1085. vmw_priv->vga_pitchlock);
  1086. else if (vmw_fifo_have_pitchlock(vmw_priv))
  1087. vmw_mmio_write(vmw_priv->vga_pitchlock,
  1088. vmw_priv->mmio_virt + SVGA_FIFO_PITCHLOCK);
  1089. if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
  1090. return 0;
  1091. for (i = 0; i < vmw_priv->num_displays; ++i) {
  1092. save = &vmw_priv->vga_save[i];
  1093. vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
  1094. vmw_write(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY, save->primary);
  1095. vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_X, save->pos_x);
  1096. vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y, save->pos_y);
  1097. vmw_write(vmw_priv, SVGA_REG_DISPLAY_WIDTH, save->width);
  1098. vmw_write(vmw_priv, SVGA_REG_DISPLAY_HEIGHT, save->height);
  1099. vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
  1100. }
  1101. return 0;
  1102. }
  1103. bool vmw_kms_validate_mode_vram(struct vmw_private *dev_priv,
  1104. uint32_t pitch,
  1105. uint32_t height)
  1106. {
  1107. return ((u64) pitch * (u64) height) < (u64)
  1108. ((dev_priv->active_display_unit == vmw_du_screen_target) ?
  1109. dev_priv->prim_bb_mem : dev_priv->vram_size);
  1110. }
  1111. /**
  1112. * Function called by DRM code called with vbl_lock held.
  1113. */
  1114. u32 vmw_get_vblank_counter(struct drm_device *dev, unsigned int pipe)
  1115. {
  1116. return 0;
  1117. }
  1118. /**
  1119. * Function called by DRM code called with vbl_lock held.
  1120. */
  1121. int vmw_enable_vblank(struct drm_device *dev, unsigned int pipe)
  1122. {
  1123. return -ENOSYS;
  1124. }
  1125. /**
  1126. * Function called by DRM code called with vbl_lock held.
  1127. */
  1128. void vmw_disable_vblank(struct drm_device *dev, unsigned int pipe)
  1129. {
  1130. }
  1131. /*
  1132. * Small shared kms functions.
  1133. */
  1134. static int vmw_du_update_layout(struct vmw_private *dev_priv, unsigned num,
  1135. struct drm_vmw_rect *rects)
  1136. {
  1137. struct drm_device *dev = dev_priv->dev;
  1138. struct vmw_display_unit *du;
  1139. struct drm_connector *con;
  1140. mutex_lock(&dev->mode_config.mutex);
  1141. #if 0
  1142. {
  1143. unsigned int i;
  1144. DRM_INFO("%s: new layout ", __func__);
  1145. for (i = 0; i < num; i++)
  1146. DRM_INFO("(%i, %i %ux%u) ", rects[i].x, rects[i].y,
  1147. rects[i].w, rects[i].h);
  1148. DRM_INFO("\n");
  1149. }
  1150. #endif
  1151. list_for_each_entry(con, &dev->mode_config.connector_list, head) {
  1152. du = vmw_connector_to_du(con);
  1153. if (num > du->unit) {
  1154. du->pref_width = rects[du->unit].w;
  1155. du->pref_height = rects[du->unit].h;
  1156. du->pref_active = true;
  1157. du->gui_x = rects[du->unit].x;
  1158. du->gui_y = rects[du->unit].y;
  1159. drm_object_property_set_value
  1160. (&con->base, dev->mode_config.suggested_x_property,
  1161. du->gui_x);
  1162. drm_object_property_set_value
  1163. (&con->base, dev->mode_config.suggested_y_property,
  1164. du->gui_y);
  1165. } else {
  1166. du->pref_width = 800;
  1167. du->pref_height = 600;
  1168. du->pref_active = false;
  1169. drm_object_property_set_value
  1170. (&con->base, dev->mode_config.suggested_x_property,
  1171. 0);
  1172. drm_object_property_set_value
  1173. (&con->base, dev->mode_config.suggested_y_property,
  1174. 0);
  1175. }
  1176. con->status = vmw_du_connector_detect(con, true);
  1177. }
  1178. mutex_unlock(&dev->mode_config.mutex);
  1179. drm_sysfs_hotplug_event(dev);
  1180. return 0;
  1181. }
  1182. int vmw_du_crtc_gamma_set(struct drm_crtc *crtc,
  1183. u16 *r, u16 *g, u16 *b,
  1184. uint32_t size)
  1185. {
  1186. struct vmw_private *dev_priv = vmw_priv(crtc->dev);
  1187. int i;
  1188. for (i = 0; i < size; i++) {
  1189. DRM_DEBUG("%d r/g/b = 0x%04x / 0x%04x / 0x%04x\n", i,
  1190. r[i], g[i], b[i]);
  1191. vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 0, r[i] >> 8);
  1192. vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 1, g[i] >> 8);
  1193. vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 2, b[i] >> 8);
  1194. }
  1195. return 0;
  1196. }
  1197. int vmw_du_connector_dpms(struct drm_connector *connector, int mode)
  1198. {
  1199. return 0;
  1200. }
  1201. enum drm_connector_status
  1202. vmw_du_connector_detect(struct drm_connector *connector, bool force)
  1203. {
  1204. uint32_t num_displays;
  1205. struct drm_device *dev = connector->dev;
  1206. struct vmw_private *dev_priv = vmw_priv(dev);
  1207. struct vmw_display_unit *du = vmw_connector_to_du(connector);
  1208. num_displays = vmw_read(dev_priv, SVGA_REG_NUM_DISPLAYS);
  1209. return ((vmw_connector_to_du(connector)->unit < num_displays &&
  1210. du->pref_active) ?
  1211. connector_status_connected : connector_status_disconnected);
  1212. }
  1213. static struct drm_display_mode vmw_kms_connector_builtin[] = {
  1214. /* 640x480@60Hz */
  1215. { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 25175, 640, 656,
  1216. 752, 800, 0, 480, 489, 492, 525, 0,
  1217. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
  1218. /* 800x600@60Hz */
  1219. { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 40000, 800, 840,
  1220. 968, 1056, 0, 600, 601, 605, 628, 0,
  1221. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1222. /* 1024x768@60Hz */
  1223. { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 65000, 1024, 1048,
  1224. 1184, 1344, 0, 768, 771, 777, 806, 0,
  1225. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
  1226. /* 1152x864@75Hz */
  1227. { DRM_MODE("1152x864", DRM_MODE_TYPE_DRIVER, 108000, 1152, 1216,
  1228. 1344, 1600, 0, 864, 865, 868, 900, 0,
  1229. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1230. /* 1280x768@60Hz */
  1231. { DRM_MODE("1280x768", DRM_MODE_TYPE_DRIVER, 79500, 1280, 1344,
  1232. 1472, 1664, 0, 768, 771, 778, 798, 0,
  1233. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1234. /* 1280x800@60Hz */
  1235. { DRM_MODE("1280x800", DRM_MODE_TYPE_DRIVER, 83500, 1280, 1352,
  1236. 1480, 1680, 0, 800, 803, 809, 831, 0,
  1237. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
  1238. /* 1280x960@60Hz */
  1239. { DRM_MODE("1280x960", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1376,
  1240. 1488, 1800, 0, 960, 961, 964, 1000, 0,
  1241. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1242. /* 1280x1024@60Hz */
  1243. { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1328,
  1244. 1440, 1688, 0, 1024, 1025, 1028, 1066, 0,
  1245. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1246. /* 1360x768@60Hz */
  1247. { DRM_MODE("1360x768", DRM_MODE_TYPE_DRIVER, 85500, 1360, 1424,
  1248. 1536, 1792, 0, 768, 771, 777, 795, 0,
  1249. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1250. /* 1440x1050@60Hz */
  1251. { DRM_MODE("1400x1050", DRM_MODE_TYPE_DRIVER, 121750, 1400, 1488,
  1252. 1632, 1864, 0, 1050, 1053, 1057, 1089, 0,
  1253. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1254. /* 1440x900@60Hz */
  1255. { DRM_MODE("1440x900", DRM_MODE_TYPE_DRIVER, 106500, 1440, 1520,
  1256. 1672, 1904, 0, 900, 903, 909, 934, 0,
  1257. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1258. /* 1600x1200@60Hz */
  1259. { DRM_MODE("1600x1200", DRM_MODE_TYPE_DRIVER, 162000, 1600, 1664,
  1260. 1856, 2160, 0, 1200, 1201, 1204, 1250, 0,
  1261. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1262. /* 1680x1050@60Hz */
  1263. { DRM_MODE("1680x1050", DRM_MODE_TYPE_DRIVER, 146250, 1680, 1784,
  1264. 1960, 2240, 0, 1050, 1053, 1059, 1089, 0,
  1265. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1266. /* 1792x1344@60Hz */
  1267. { DRM_MODE("1792x1344", DRM_MODE_TYPE_DRIVER, 204750, 1792, 1920,
  1268. 2120, 2448, 0, 1344, 1345, 1348, 1394, 0,
  1269. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1270. /* 1853x1392@60Hz */
  1271. { DRM_MODE("1856x1392", DRM_MODE_TYPE_DRIVER, 218250, 1856, 1952,
  1272. 2176, 2528, 0, 1392, 1393, 1396, 1439, 0,
  1273. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1274. /* 1920x1200@60Hz */
  1275. { DRM_MODE("1920x1200", DRM_MODE_TYPE_DRIVER, 193250, 1920, 2056,
  1276. 2256, 2592, 0, 1200, 1203, 1209, 1245, 0,
  1277. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1278. /* 1920x1440@60Hz */
  1279. { DRM_MODE("1920x1440", DRM_MODE_TYPE_DRIVER, 234000, 1920, 2048,
  1280. 2256, 2600, 0, 1440, 1441, 1444, 1500, 0,
  1281. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1282. /* 2560x1600@60Hz */
  1283. { DRM_MODE("2560x1600", DRM_MODE_TYPE_DRIVER, 348500, 2560, 2752,
  1284. 3032, 3504, 0, 1600, 1603, 1609, 1658, 0,
  1285. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1286. /* Terminate */
  1287. { DRM_MODE("", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) },
  1288. };
  1289. /**
  1290. * vmw_guess_mode_timing - Provide fake timings for a
  1291. * 60Hz vrefresh mode.
  1292. *
  1293. * @mode - Pointer to a struct drm_display_mode with hdisplay and vdisplay
  1294. * members filled in.
  1295. */
  1296. void vmw_guess_mode_timing(struct drm_display_mode *mode)
  1297. {
  1298. mode->hsync_start = mode->hdisplay + 50;
  1299. mode->hsync_end = mode->hsync_start + 50;
  1300. mode->htotal = mode->hsync_end + 50;
  1301. mode->vsync_start = mode->vdisplay + 50;
  1302. mode->vsync_end = mode->vsync_start + 50;
  1303. mode->vtotal = mode->vsync_end + 50;
  1304. mode->clock = (u32)mode->htotal * (u32)mode->vtotal / 100 * 6;
  1305. mode->vrefresh = drm_mode_vrefresh(mode);
  1306. }
  1307. int vmw_du_connector_fill_modes(struct drm_connector *connector,
  1308. uint32_t max_width, uint32_t max_height)
  1309. {
  1310. struct vmw_display_unit *du = vmw_connector_to_du(connector);
  1311. struct drm_device *dev = connector->dev;
  1312. struct vmw_private *dev_priv = vmw_priv(dev);
  1313. struct drm_display_mode *mode = NULL;
  1314. struct drm_display_mode *bmode;
  1315. struct drm_display_mode prefmode = { DRM_MODE("preferred",
  1316. DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED,
  1317. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1318. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC)
  1319. };
  1320. int i;
  1321. u32 assumed_bpp = 2;
  1322. /*
  1323. * If using screen objects, then assume 32-bpp because that's what the
  1324. * SVGA device is assuming
  1325. */
  1326. if (dev_priv->active_display_unit == vmw_du_screen_object)
  1327. assumed_bpp = 4;
  1328. if (dev_priv->active_display_unit == vmw_du_screen_target) {
  1329. max_width = min(max_width, dev_priv->stdu_max_width);
  1330. max_height = min(max_height, dev_priv->stdu_max_height);
  1331. }
  1332. /* Add preferred mode */
  1333. mode = drm_mode_duplicate(dev, &prefmode);
  1334. if (!mode)
  1335. return 0;
  1336. mode->hdisplay = du->pref_width;
  1337. mode->vdisplay = du->pref_height;
  1338. vmw_guess_mode_timing(mode);
  1339. if (vmw_kms_validate_mode_vram(dev_priv,
  1340. mode->hdisplay * assumed_bpp,
  1341. mode->vdisplay)) {
  1342. drm_mode_probed_add(connector, mode);
  1343. } else {
  1344. drm_mode_destroy(dev, mode);
  1345. mode = NULL;
  1346. }
  1347. if (du->pref_mode) {
  1348. list_del_init(&du->pref_mode->head);
  1349. drm_mode_destroy(dev, du->pref_mode);
  1350. }
  1351. /* mode might be null here, this is intended */
  1352. du->pref_mode = mode;
  1353. for (i = 0; vmw_kms_connector_builtin[i].type != 0; i++) {
  1354. bmode = &vmw_kms_connector_builtin[i];
  1355. if (bmode->hdisplay > max_width ||
  1356. bmode->vdisplay > max_height)
  1357. continue;
  1358. if (!vmw_kms_validate_mode_vram(dev_priv,
  1359. bmode->hdisplay * assumed_bpp,
  1360. bmode->vdisplay))
  1361. continue;
  1362. mode = drm_mode_duplicate(dev, bmode);
  1363. if (!mode)
  1364. return 0;
  1365. mode->vrefresh = drm_mode_vrefresh(mode);
  1366. drm_mode_probed_add(connector, mode);
  1367. }
  1368. drm_mode_connector_list_update(connector);
  1369. /* Move the prefered mode first, help apps pick the right mode. */
  1370. drm_mode_sort(&connector->modes);
  1371. return 1;
  1372. }
  1373. int vmw_du_connector_set_property(struct drm_connector *connector,
  1374. struct drm_property *property,
  1375. uint64_t val)
  1376. {
  1377. struct vmw_display_unit *du = vmw_connector_to_du(connector);
  1378. struct vmw_private *dev_priv = vmw_priv(connector->dev);
  1379. if (property == dev_priv->implicit_placement_property)
  1380. du->is_implicit = val;
  1381. return 0;
  1382. }
  1383. int vmw_kms_update_layout_ioctl(struct drm_device *dev, void *data,
  1384. struct drm_file *file_priv)
  1385. {
  1386. struct vmw_private *dev_priv = vmw_priv(dev);
  1387. struct drm_vmw_update_layout_arg *arg =
  1388. (struct drm_vmw_update_layout_arg *)data;
  1389. void __user *user_rects;
  1390. struct drm_vmw_rect *rects;
  1391. unsigned rects_size;
  1392. int ret;
  1393. int i;
  1394. u64 total_pixels = 0;
  1395. struct drm_mode_config *mode_config = &dev->mode_config;
  1396. struct drm_vmw_rect bounding_box = {0};
  1397. if (!arg->num_outputs) {
  1398. struct drm_vmw_rect def_rect = {0, 0, 800, 600};
  1399. vmw_du_update_layout(dev_priv, 1, &def_rect);
  1400. return 0;
  1401. }
  1402. rects_size = arg->num_outputs * sizeof(struct drm_vmw_rect);
  1403. rects = kcalloc(arg->num_outputs, sizeof(struct drm_vmw_rect),
  1404. GFP_KERNEL);
  1405. if (unlikely(!rects))
  1406. return -ENOMEM;
  1407. user_rects = (void __user *)(unsigned long)arg->rects;
  1408. ret = copy_from_user(rects, user_rects, rects_size);
  1409. if (unlikely(ret != 0)) {
  1410. DRM_ERROR("Failed to get rects.\n");
  1411. ret = -EFAULT;
  1412. goto out_free;
  1413. }
  1414. for (i = 0; i < arg->num_outputs; ++i) {
  1415. if (rects[i].x < 0 ||
  1416. rects[i].y < 0 ||
  1417. rects[i].x + rects[i].w > mode_config->max_width ||
  1418. rects[i].y + rects[i].h > mode_config->max_height) {
  1419. DRM_ERROR("Invalid GUI layout.\n");
  1420. ret = -EINVAL;
  1421. goto out_free;
  1422. }
  1423. /*
  1424. * bounding_box.w and bunding_box.h are used as
  1425. * lower-right coordinates
  1426. */
  1427. if (rects[i].x + rects[i].w > bounding_box.w)
  1428. bounding_box.w = rects[i].x + rects[i].w;
  1429. if (rects[i].y + rects[i].h > bounding_box.h)
  1430. bounding_box.h = rects[i].y + rects[i].h;
  1431. total_pixels += (u64) rects[i].w * (u64) rects[i].h;
  1432. }
  1433. if (dev_priv->active_display_unit == vmw_du_screen_target) {
  1434. /*
  1435. * For Screen Targets, the limits for a toplogy are:
  1436. * 1. Bounding box (assuming 32bpp) must be < prim_bb_mem
  1437. * 2. Total pixels (assuming 32bpp) must be < prim_bb_mem
  1438. */
  1439. u64 bb_mem = bounding_box.w * bounding_box.h * 4;
  1440. u64 pixel_mem = total_pixels * 4;
  1441. if (bb_mem > dev_priv->prim_bb_mem) {
  1442. DRM_ERROR("Topology is beyond supported limits.\n");
  1443. ret = -EINVAL;
  1444. goto out_free;
  1445. }
  1446. if (pixel_mem > dev_priv->prim_bb_mem) {
  1447. DRM_ERROR("Combined output size too large\n");
  1448. ret = -EINVAL;
  1449. goto out_free;
  1450. }
  1451. }
  1452. vmw_du_update_layout(dev_priv, arg->num_outputs, rects);
  1453. out_free:
  1454. kfree(rects);
  1455. return ret;
  1456. }
  1457. /**
  1458. * vmw_kms_helper_dirty - Helper to build commands and perform actions based
  1459. * on a set of cliprects and a set of display units.
  1460. *
  1461. * @dev_priv: Pointer to a device private structure.
  1462. * @framebuffer: Pointer to the framebuffer on which to perform the actions.
  1463. * @clips: A set of struct drm_clip_rect. Either this os @vclips must be NULL.
  1464. * Cliprects are given in framebuffer coordinates.
  1465. * @vclips: A set of struct drm_vmw_rect cliprects. Either this or @clips must
  1466. * be NULL. Cliprects are given in source coordinates.
  1467. * @dest_x: X coordinate offset for the crtc / destination clip rects.
  1468. * @dest_y: Y coordinate offset for the crtc / destination clip rects.
  1469. * @num_clips: Number of cliprects in the @clips or @vclips array.
  1470. * @increment: Integer with which to increment the clip counter when looping.
  1471. * Used to skip a predetermined number of clip rects.
  1472. * @dirty: Closure structure. See the description of struct vmw_kms_dirty.
  1473. */
  1474. int vmw_kms_helper_dirty(struct vmw_private *dev_priv,
  1475. struct vmw_framebuffer *framebuffer,
  1476. const struct drm_clip_rect *clips,
  1477. const struct drm_vmw_rect *vclips,
  1478. s32 dest_x, s32 dest_y,
  1479. int num_clips,
  1480. int increment,
  1481. struct vmw_kms_dirty *dirty)
  1482. {
  1483. struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
  1484. struct drm_crtc *crtc;
  1485. u32 num_units = 0;
  1486. u32 i, k;
  1487. dirty->dev_priv = dev_priv;
  1488. list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list, head) {
  1489. if (crtc->primary->fb != &framebuffer->base)
  1490. continue;
  1491. units[num_units++] = vmw_crtc_to_du(crtc);
  1492. }
  1493. for (k = 0; k < num_units; k++) {
  1494. struct vmw_display_unit *unit = units[k];
  1495. s32 crtc_x = unit->crtc.x;
  1496. s32 crtc_y = unit->crtc.y;
  1497. s32 crtc_width = unit->crtc.mode.hdisplay;
  1498. s32 crtc_height = unit->crtc.mode.vdisplay;
  1499. const struct drm_clip_rect *clips_ptr = clips;
  1500. const struct drm_vmw_rect *vclips_ptr = vclips;
  1501. dirty->unit = unit;
  1502. if (dirty->fifo_reserve_size > 0) {
  1503. dirty->cmd = vmw_fifo_reserve(dev_priv,
  1504. dirty->fifo_reserve_size);
  1505. if (!dirty->cmd) {
  1506. DRM_ERROR("Couldn't reserve fifo space "
  1507. "for dirty blits.\n");
  1508. return -ENOMEM;
  1509. }
  1510. memset(dirty->cmd, 0, dirty->fifo_reserve_size);
  1511. }
  1512. dirty->num_hits = 0;
  1513. for (i = 0; i < num_clips; i++, clips_ptr += increment,
  1514. vclips_ptr += increment) {
  1515. s32 clip_left;
  1516. s32 clip_top;
  1517. /*
  1518. * Select clip array type. Note that integer type
  1519. * in @clips is unsigned short, whereas in @vclips
  1520. * it's 32-bit.
  1521. */
  1522. if (clips) {
  1523. dirty->fb_x = (s32) clips_ptr->x1;
  1524. dirty->fb_y = (s32) clips_ptr->y1;
  1525. dirty->unit_x2 = (s32) clips_ptr->x2 + dest_x -
  1526. crtc_x;
  1527. dirty->unit_y2 = (s32) clips_ptr->y2 + dest_y -
  1528. crtc_y;
  1529. } else {
  1530. dirty->fb_x = vclips_ptr->x;
  1531. dirty->fb_y = vclips_ptr->y;
  1532. dirty->unit_x2 = dirty->fb_x + vclips_ptr->w +
  1533. dest_x - crtc_x;
  1534. dirty->unit_y2 = dirty->fb_y + vclips_ptr->h +
  1535. dest_y - crtc_y;
  1536. }
  1537. dirty->unit_x1 = dirty->fb_x + dest_x - crtc_x;
  1538. dirty->unit_y1 = dirty->fb_y + dest_y - crtc_y;
  1539. /* Skip this clip if it's outside the crtc region */
  1540. if (dirty->unit_x1 >= crtc_width ||
  1541. dirty->unit_y1 >= crtc_height ||
  1542. dirty->unit_x2 <= 0 || dirty->unit_y2 <= 0)
  1543. continue;
  1544. /* Clip right and bottom to crtc limits */
  1545. dirty->unit_x2 = min_t(s32, dirty->unit_x2,
  1546. crtc_width);
  1547. dirty->unit_y2 = min_t(s32, dirty->unit_y2,
  1548. crtc_height);
  1549. /* Clip left and top to crtc limits */
  1550. clip_left = min_t(s32, dirty->unit_x1, 0);
  1551. clip_top = min_t(s32, dirty->unit_y1, 0);
  1552. dirty->unit_x1 -= clip_left;
  1553. dirty->unit_y1 -= clip_top;
  1554. dirty->fb_x -= clip_left;
  1555. dirty->fb_y -= clip_top;
  1556. dirty->clip(dirty);
  1557. }
  1558. dirty->fifo_commit(dirty);
  1559. }
  1560. return 0;
  1561. }
  1562. /**
  1563. * vmw_kms_helper_buffer_prepare - Reserve and validate a buffer object before
  1564. * command submission.
  1565. *
  1566. * @dev_priv. Pointer to a device private structure.
  1567. * @buf: The buffer object
  1568. * @interruptible: Whether to perform waits as interruptible.
  1569. * @validate_as_mob: Whether the buffer should be validated as a MOB. If false,
  1570. * The buffer will be validated as a GMR. Already pinned buffers will not be
  1571. * validated.
  1572. *
  1573. * Returns 0 on success, negative error code on failure, -ERESTARTSYS if
  1574. * interrupted by a signal.
  1575. */
  1576. int vmw_kms_helper_buffer_prepare(struct vmw_private *dev_priv,
  1577. struct vmw_dma_buffer *buf,
  1578. bool interruptible,
  1579. bool validate_as_mob)
  1580. {
  1581. struct ttm_buffer_object *bo = &buf->base;
  1582. int ret;
  1583. ttm_bo_reserve(bo, false, false, NULL);
  1584. ret = vmw_validate_single_buffer(dev_priv, bo, interruptible,
  1585. validate_as_mob);
  1586. if (ret)
  1587. ttm_bo_unreserve(bo);
  1588. return ret;
  1589. }
  1590. /**
  1591. * vmw_kms_helper_buffer_revert - Undo the actions of
  1592. * vmw_kms_helper_buffer_prepare.
  1593. *
  1594. * @res: Pointer to the buffer object.
  1595. *
  1596. * Helper to be used if an error forces the caller to undo the actions of
  1597. * vmw_kms_helper_buffer_prepare.
  1598. */
  1599. void vmw_kms_helper_buffer_revert(struct vmw_dma_buffer *buf)
  1600. {
  1601. if (buf)
  1602. ttm_bo_unreserve(&buf->base);
  1603. }
  1604. /**
  1605. * vmw_kms_helper_buffer_finish - Unreserve and fence a buffer object after
  1606. * kms command submission.
  1607. *
  1608. * @dev_priv: Pointer to a device private structure.
  1609. * @file_priv: Pointer to a struct drm_file representing the caller's
  1610. * connection. Must be set to NULL if @user_fence_rep is NULL, and conversely
  1611. * if non-NULL, @user_fence_rep must be non-NULL.
  1612. * @buf: The buffer object.
  1613. * @out_fence: Optional pointer to a fence pointer. If non-NULL, a
  1614. * ref-counted fence pointer is returned here.
  1615. * @user_fence_rep: Optional pointer to a user-space provided struct
  1616. * drm_vmw_fence_rep. If provided, @file_priv must also be provided and the
  1617. * function copies fence data to user-space in a fail-safe manner.
  1618. */
  1619. void vmw_kms_helper_buffer_finish(struct vmw_private *dev_priv,
  1620. struct drm_file *file_priv,
  1621. struct vmw_dma_buffer *buf,
  1622. struct vmw_fence_obj **out_fence,
  1623. struct drm_vmw_fence_rep __user *
  1624. user_fence_rep)
  1625. {
  1626. struct vmw_fence_obj *fence;
  1627. uint32_t handle;
  1628. int ret;
  1629. ret = vmw_execbuf_fence_commands(file_priv, dev_priv, &fence,
  1630. file_priv ? &handle : NULL);
  1631. if (buf)
  1632. vmw_fence_single_bo(&buf->base, fence);
  1633. if (file_priv)
  1634. vmw_execbuf_copy_fence_user(dev_priv, vmw_fpriv(file_priv),
  1635. ret, user_fence_rep, fence,
  1636. handle);
  1637. if (out_fence)
  1638. *out_fence = fence;
  1639. else
  1640. vmw_fence_obj_unreference(&fence);
  1641. vmw_kms_helper_buffer_revert(buf);
  1642. }
  1643. /**
  1644. * vmw_kms_helper_resource_revert - Undo the actions of
  1645. * vmw_kms_helper_resource_prepare.
  1646. *
  1647. * @res: Pointer to the resource. Typically a surface.
  1648. *
  1649. * Helper to be used if an error forces the caller to undo the actions of
  1650. * vmw_kms_helper_resource_prepare.
  1651. */
  1652. void vmw_kms_helper_resource_revert(struct vmw_resource *res)
  1653. {
  1654. vmw_kms_helper_buffer_revert(res->backup);
  1655. vmw_resource_unreserve(res, false, NULL, 0);
  1656. mutex_unlock(&res->dev_priv->cmdbuf_mutex);
  1657. }
  1658. /**
  1659. * vmw_kms_helper_resource_prepare - Reserve and validate a resource before
  1660. * command submission.
  1661. *
  1662. * @res: Pointer to the resource. Typically a surface.
  1663. * @interruptible: Whether to perform waits as interruptible.
  1664. *
  1665. * Reserves and validates also the backup buffer if a guest-backed resource.
  1666. * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
  1667. * interrupted by a signal.
  1668. */
  1669. int vmw_kms_helper_resource_prepare(struct vmw_resource *res,
  1670. bool interruptible)
  1671. {
  1672. int ret = 0;
  1673. if (interruptible)
  1674. ret = mutex_lock_interruptible(&res->dev_priv->cmdbuf_mutex);
  1675. else
  1676. mutex_lock(&res->dev_priv->cmdbuf_mutex);
  1677. if (unlikely(ret != 0))
  1678. return -ERESTARTSYS;
  1679. ret = vmw_resource_reserve(res, interruptible, false);
  1680. if (ret)
  1681. goto out_unlock;
  1682. if (res->backup) {
  1683. ret = vmw_kms_helper_buffer_prepare(res->dev_priv, res->backup,
  1684. interruptible,
  1685. res->dev_priv->has_mob);
  1686. if (ret)
  1687. goto out_unreserve;
  1688. }
  1689. ret = vmw_resource_validate(res);
  1690. if (ret)
  1691. goto out_revert;
  1692. return 0;
  1693. out_revert:
  1694. vmw_kms_helper_buffer_revert(res->backup);
  1695. out_unreserve:
  1696. vmw_resource_unreserve(res, false, NULL, 0);
  1697. out_unlock:
  1698. mutex_unlock(&res->dev_priv->cmdbuf_mutex);
  1699. return ret;
  1700. }
  1701. /**
  1702. * vmw_kms_helper_resource_finish - Unreserve and fence a resource after
  1703. * kms command submission.
  1704. *
  1705. * @res: Pointer to the resource. Typically a surface.
  1706. * @out_fence: Optional pointer to a fence pointer. If non-NULL, a
  1707. * ref-counted fence pointer is returned here.
  1708. */
  1709. void vmw_kms_helper_resource_finish(struct vmw_resource *res,
  1710. struct vmw_fence_obj **out_fence)
  1711. {
  1712. if (res->backup || out_fence)
  1713. vmw_kms_helper_buffer_finish(res->dev_priv, NULL, res->backup,
  1714. out_fence, NULL);
  1715. vmw_resource_unreserve(res, false, NULL, 0);
  1716. mutex_unlock(&res->dev_priv->cmdbuf_mutex);
  1717. }
  1718. /**
  1719. * vmw_kms_update_proxy - Helper function to update a proxy surface from
  1720. * its backing MOB.
  1721. *
  1722. * @res: Pointer to the surface resource
  1723. * @clips: Clip rects in framebuffer (surface) space.
  1724. * @num_clips: Number of clips in @clips.
  1725. * @increment: Integer with which to increment the clip counter when looping.
  1726. * Used to skip a predetermined number of clip rects.
  1727. *
  1728. * This function makes sure the proxy surface is updated from its backing MOB
  1729. * using the region given by @clips. The surface resource @res and its backing
  1730. * MOB needs to be reserved and validated on call.
  1731. */
  1732. int vmw_kms_update_proxy(struct vmw_resource *res,
  1733. const struct drm_clip_rect *clips,
  1734. unsigned num_clips,
  1735. int increment)
  1736. {
  1737. struct vmw_private *dev_priv = res->dev_priv;
  1738. struct drm_vmw_size *size = &vmw_res_to_srf(res)->base_size;
  1739. struct {
  1740. SVGA3dCmdHeader header;
  1741. SVGA3dCmdUpdateGBImage body;
  1742. } *cmd;
  1743. SVGA3dBox *box;
  1744. size_t copy_size = 0;
  1745. int i;
  1746. if (!clips)
  1747. return 0;
  1748. cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd) * num_clips);
  1749. if (!cmd) {
  1750. DRM_ERROR("Couldn't reserve fifo space for proxy surface "
  1751. "update.\n");
  1752. return -ENOMEM;
  1753. }
  1754. for (i = 0; i < num_clips; ++i, clips += increment, ++cmd) {
  1755. box = &cmd->body.box;
  1756. cmd->header.id = SVGA_3D_CMD_UPDATE_GB_IMAGE;
  1757. cmd->header.size = sizeof(cmd->body);
  1758. cmd->body.image.sid = res->id;
  1759. cmd->body.image.face = 0;
  1760. cmd->body.image.mipmap = 0;
  1761. if (clips->x1 > size->width || clips->x2 > size->width ||
  1762. clips->y1 > size->height || clips->y2 > size->height) {
  1763. DRM_ERROR("Invalid clips outsize of framebuffer.\n");
  1764. return -EINVAL;
  1765. }
  1766. box->x = clips->x1;
  1767. box->y = clips->y1;
  1768. box->z = 0;
  1769. box->w = clips->x2 - clips->x1;
  1770. box->h = clips->y2 - clips->y1;
  1771. box->d = 1;
  1772. copy_size += sizeof(*cmd);
  1773. }
  1774. vmw_fifo_commit(dev_priv, copy_size);
  1775. return 0;
  1776. }
  1777. int vmw_kms_fbdev_init_data(struct vmw_private *dev_priv,
  1778. unsigned unit,
  1779. u32 max_width,
  1780. u32 max_height,
  1781. struct drm_connector **p_con,
  1782. struct drm_crtc **p_crtc,
  1783. struct drm_display_mode **p_mode)
  1784. {
  1785. struct drm_connector *con;
  1786. struct vmw_display_unit *du;
  1787. struct drm_display_mode *mode;
  1788. int i = 0;
  1789. list_for_each_entry(con, &dev_priv->dev->mode_config.connector_list,
  1790. head) {
  1791. if (i == unit)
  1792. break;
  1793. ++i;
  1794. }
  1795. if (i != unit) {
  1796. DRM_ERROR("Could not find initial display unit.\n");
  1797. return -EINVAL;
  1798. }
  1799. if (list_empty(&con->modes))
  1800. (void) vmw_du_connector_fill_modes(con, max_width, max_height);
  1801. if (list_empty(&con->modes)) {
  1802. DRM_ERROR("Could not find initial display mode.\n");
  1803. return -EINVAL;
  1804. }
  1805. du = vmw_connector_to_du(con);
  1806. *p_con = con;
  1807. *p_crtc = &du->crtc;
  1808. list_for_each_entry(mode, &con->modes, head) {
  1809. if (mode->type & DRM_MODE_TYPE_PREFERRED)
  1810. break;
  1811. }
  1812. if (mode->type & DRM_MODE_TYPE_PREFERRED)
  1813. *p_mode = mode;
  1814. else {
  1815. WARN_ONCE(true, "Could not find initial preferred mode.\n");
  1816. *p_mode = list_first_entry(&con->modes,
  1817. struct drm_display_mode,
  1818. head);
  1819. }
  1820. return 0;
  1821. }
  1822. /**
  1823. * vmw_kms_del_active - unregister a crtc binding to the implicit framebuffer
  1824. *
  1825. * @dev_priv: Pointer to a device private struct.
  1826. * @du: The display unit of the crtc.
  1827. */
  1828. void vmw_kms_del_active(struct vmw_private *dev_priv,
  1829. struct vmw_display_unit *du)
  1830. {
  1831. mutex_lock(&dev_priv->global_kms_state_mutex);
  1832. if (du->active_implicit) {
  1833. if (--(dev_priv->num_implicit) == 0)
  1834. dev_priv->implicit_fb = NULL;
  1835. du->active_implicit = false;
  1836. }
  1837. mutex_unlock(&dev_priv->global_kms_state_mutex);
  1838. }
  1839. /**
  1840. * vmw_kms_add_active - register a crtc binding to an implicit framebuffer
  1841. *
  1842. * @vmw_priv: Pointer to a device private struct.
  1843. * @du: The display unit of the crtc.
  1844. * @vfb: The implicit framebuffer
  1845. *
  1846. * Registers a binding to an implicit framebuffer.
  1847. */
  1848. void vmw_kms_add_active(struct vmw_private *dev_priv,
  1849. struct vmw_display_unit *du,
  1850. struct vmw_framebuffer *vfb)
  1851. {
  1852. mutex_lock(&dev_priv->global_kms_state_mutex);
  1853. WARN_ON_ONCE(!dev_priv->num_implicit && dev_priv->implicit_fb);
  1854. if (!du->active_implicit && du->is_implicit) {
  1855. dev_priv->implicit_fb = vfb;
  1856. du->active_implicit = true;
  1857. dev_priv->num_implicit++;
  1858. }
  1859. mutex_unlock(&dev_priv->global_kms_state_mutex);
  1860. }
  1861. /**
  1862. * vmw_kms_screen_object_flippable - Check whether we can page-flip a crtc.
  1863. *
  1864. * @dev_priv: Pointer to device-private struct.
  1865. * @crtc: The crtc we want to flip.
  1866. *
  1867. * Returns true or false depending whether it's OK to flip this crtc
  1868. * based on the criterion that we must not have more than one implicit
  1869. * frame-buffer at any one time.
  1870. */
  1871. bool vmw_kms_crtc_flippable(struct vmw_private *dev_priv,
  1872. struct drm_crtc *crtc)
  1873. {
  1874. struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
  1875. bool ret;
  1876. mutex_lock(&dev_priv->global_kms_state_mutex);
  1877. ret = !du->is_implicit || dev_priv->num_implicit == 1;
  1878. mutex_unlock(&dev_priv->global_kms_state_mutex);
  1879. return ret;
  1880. }
  1881. /**
  1882. * vmw_kms_update_implicit_fb - Update the implicit fb.
  1883. *
  1884. * @dev_priv: Pointer to device-private struct.
  1885. * @crtc: The crtc the new implicit frame-buffer is bound to.
  1886. */
  1887. void vmw_kms_update_implicit_fb(struct vmw_private *dev_priv,
  1888. struct drm_crtc *crtc)
  1889. {
  1890. struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
  1891. struct vmw_framebuffer *vfb;
  1892. mutex_lock(&dev_priv->global_kms_state_mutex);
  1893. if (!du->is_implicit)
  1894. goto out_unlock;
  1895. vfb = vmw_framebuffer_to_vfb(crtc->primary->fb);
  1896. WARN_ON_ONCE(dev_priv->num_implicit != 1 &&
  1897. dev_priv->implicit_fb != vfb);
  1898. dev_priv->implicit_fb = vfb;
  1899. out_unlock:
  1900. mutex_unlock(&dev_priv->global_kms_state_mutex);
  1901. }
  1902. /**
  1903. * vmw_kms_create_implicit_placement_proparty - Set up the implicit placement
  1904. * property.
  1905. *
  1906. * @dev_priv: Pointer to a device private struct.
  1907. * @immutable: Whether the property is immutable.
  1908. *
  1909. * Sets up the implicit placement property unless it's already set up.
  1910. */
  1911. void
  1912. vmw_kms_create_implicit_placement_property(struct vmw_private *dev_priv,
  1913. bool immutable)
  1914. {
  1915. if (dev_priv->implicit_placement_property)
  1916. return;
  1917. dev_priv->implicit_placement_property =
  1918. drm_property_create_range(dev_priv->dev,
  1919. immutable ?
  1920. DRM_MODE_PROP_IMMUTABLE : 0,
  1921. "implicit_placement", 0, 1);
  1922. }