vmwgfx_kms.c 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225
  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_cmd2
  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. struct drm_format_name_buf format_name;
  444. /* 3D is only supported on HWv8 and newer hosts */
  445. if (dev_priv->active_display_unit == vmw_du_legacy)
  446. return -ENOSYS;
  447. /*
  448. * Sanity checks.
  449. */
  450. /* Surface must be marked as a scanout. */
  451. if (unlikely(!surface->scanout))
  452. return -EINVAL;
  453. if (unlikely(surface->mip_levels[0] != 1 ||
  454. surface->num_sizes != 1 ||
  455. surface->base_size.width < mode_cmd->width ||
  456. surface->base_size.height < mode_cmd->height ||
  457. surface->base_size.depth != 1)) {
  458. DRM_ERROR("Incompatible surface dimensions "
  459. "for requested mode.\n");
  460. return -EINVAL;
  461. }
  462. switch (mode_cmd->pixel_format) {
  463. case DRM_FORMAT_ARGB8888:
  464. format = SVGA3D_A8R8G8B8;
  465. break;
  466. case DRM_FORMAT_XRGB8888:
  467. format = SVGA3D_X8R8G8B8;
  468. break;
  469. case DRM_FORMAT_RGB565:
  470. format = SVGA3D_R5G6B5;
  471. break;
  472. case DRM_FORMAT_XRGB1555:
  473. format = SVGA3D_A1R5G5B5;
  474. break;
  475. default:
  476. DRM_ERROR("Invalid pixel format: %s\n",
  477. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  478. return -EINVAL;
  479. }
  480. /*
  481. * For DX, surface format validation is done when surface->scanout
  482. * is set.
  483. */
  484. if (!dev_priv->has_dx && format != surface->format) {
  485. DRM_ERROR("Invalid surface format for requested mode.\n");
  486. return -EINVAL;
  487. }
  488. vfbs = kzalloc(sizeof(*vfbs), GFP_KERNEL);
  489. if (!vfbs) {
  490. ret = -ENOMEM;
  491. goto out_err1;
  492. }
  493. drm_helper_mode_fill_fb_struct(dev, &vfbs->base.base, mode_cmd);
  494. vfbs->surface = vmw_surface_reference(surface);
  495. vfbs->base.user_handle = mode_cmd->handles[0];
  496. vfbs->is_dmabuf_proxy = is_dmabuf_proxy;
  497. *out = &vfbs->base;
  498. ret = drm_framebuffer_init(dev, &vfbs->base.base,
  499. &vmw_framebuffer_surface_funcs);
  500. if (ret)
  501. goto out_err2;
  502. return 0;
  503. out_err2:
  504. vmw_surface_unreference(&surface);
  505. kfree(vfbs);
  506. out_err1:
  507. return ret;
  508. }
  509. /*
  510. * Dmabuf framebuffer code
  511. */
  512. static void vmw_framebuffer_dmabuf_destroy(struct drm_framebuffer *framebuffer)
  513. {
  514. struct vmw_framebuffer_dmabuf *vfbd =
  515. vmw_framebuffer_to_vfbd(framebuffer);
  516. drm_framebuffer_cleanup(framebuffer);
  517. vmw_dmabuf_unreference(&vfbd->buffer);
  518. if (vfbd->base.user_obj)
  519. ttm_base_object_unref(&vfbd->base.user_obj);
  520. kfree(vfbd);
  521. }
  522. static int vmw_framebuffer_dmabuf_dirty(struct drm_framebuffer *framebuffer,
  523. struct drm_file *file_priv,
  524. unsigned flags, unsigned color,
  525. struct drm_clip_rect *clips,
  526. unsigned num_clips)
  527. {
  528. struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
  529. struct vmw_framebuffer_dmabuf *vfbd =
  530. vmw_framebuffer_to_vfbd(framebuffer);
  531. struct drm_clip_rect norect;
  532. int ret, increment = 1;
  533. drm_modeset_lock_all(dev_priv->dev);
  534. ret = ttm_read_lock(&dev_priv->reservation_sem, true);
  535. if (unlikely(ret != 0)) {
  536. drm_modeset_unlock_all(dev_priv->dev);
  537. return ret;
  538. }
  539. if (!num_clips) {
  540. num_clips = 1;
  541. clips = &norect;
  542. norect.x1 = norect.y1 = 0;
  543. norect.x2 = framebuffer->width;
  544. norect.y2 = framebuffer->height;
  545. } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
  546. num_clips /= 2;
  547. increment = 2;
  548. }
  549. switch (dev_priv->active_display_unit) {
  550. case vmw_du_screen_target:
  551. ret = vmw_kms_stdu_dma(dev_priv, NULL, &vfbd->base, NULL,
  552. clips, NULL, num_clips, increment,
  553. true, true);
  554. break;
  555. case vmw_du_screen_object:
  556. ret = vmw_kms_sou_do_dmabuf_dirty(dev_priv, &vfbd->base,
  557. clips, NULL, num_clips,
  558. increment, true, NULL);
  559. break;
  560. case vmw_du_legacy:
  561. ret = vmw_kms_ldu_do_dmabuf_dirty(dev_priv, &vfbd->base, 0, 0,
  562. clips, num_clips, increment);
  563. break;
  564. default:
  565. ret = -EINVAL;
  566. WARN_ONCE(true, "Dirty called with invalid display system.\n");
  567. break;
  568. }
  569. vmw_fifo_flush(dev_priv, false);
  570. ttm_read_unlock(&dev_priv->reservation_sem);
  571. drm_modeset_unlock_all(dev_priv->dev);
  572. return ret;
  573. }
  574. static const struct drm_framebuffer_funcs vmw_framebuffer_dmabuf_funcs = {
  575. .destroy = vmw_framebuffer_dmabuf_destroy,
  576. .dirty = vmw_framebuffer_dmabuf_dirty,
  577. };
  578. /**
  579. * Pin the dmabuffer to the start of vram.
  580. */
  581. static int vmw_framebuffer_pin(struct vmw_framebuffer *vfb)
  582. {
  583. struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
  584. struct vmw_dma_buffer *buf;
  585. int ret;
  586. buf = vfb->dmabuf ? vmw_framebuffer_to_vfbd(&vfb->base)->buffer :
  587. vmw_framebuffer_to_vfbs(&vfb->base)->surface->res.backup;
  588. if (!buf)
  589. return 0;
  590. switch (dev_priv->active_display_unit) {
  591. case vmw_du_legacy:
  592. vmw_overlay_pause_all(dev_priv);
  593. ret = vmw_dmabuf_pin_in_start_of_vram(dev_priv, buf, false);
  594. vmw_overlay_resume_all(dev_priv);
  595. break;
  596. case vmw_du_screen_object:
  597. case vmw_du_screen_target:
  598. if (vfb->dmabuf)
  599. return vmw_dmabuf_pin_in_vram_or_gmr(dev_priv, buf,
  600. false);
  601. return vmw_dmabuf_pin_in_placement(dev_priv, buf,
  602. &vmw_mob_placement, false);
  603. default:
  604. return -EINVAL;
  605. }
  606. return ret;
  607. }
  608. static int vmw_framebuffer_unpin(struct vmw_framebuffer *vfb)
  609. {
  610. struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
  611. struct vmw_dma_buffer *buf;
  612. buf = vfb->dmabuf ? vmw_framebuffer_to_vfbd(&vfb->base)->buffer :
  613. vmw_framebuffer_to_vfbs(&vfb->base)->surface->res.backup;
  614. if (WARN_ON(!buf))
  615. return 0;
  616. return vmw_dmabuf_unpin(dev_priv, buf, false);
  617. }
  618. /**
  619. * vmw_create_dmabuf_proxy - create a proxy surface for the DMA buf
  620. *
  621. * @dev: DRM device
  622. * @mode_cmd: parameters for the new surface
  623. * @dmabuf_mob: MOB backing the DMA buf
  624. * @srf_out: newly created surface
  625. *
  626. * When the content FB is a DMA buf, we create a surface as a proxy to the
  627. * same buffer. This way we can do a surface copy rather than a surface DMA.
  628. * This is a more efficient approach
  629. *
  630. * RETURNS:
  631. * 0 on success, error code otherwise
  632. */
  633. static int vmw_create_dmabuf_proxy(struct drm_device *dev,
  634. const struct drm_mode_fb_cmd2 *mode_cmd,
  635. struct vmw_dma_buffer *dmabuf_mob,
  636. struct vmw_surface **srf_out)
  637. {
  638. uint32_t format;
  639. struct drm_vmw_size content_base_size;
  640. struct vmw_resource *res;
  641. unsigned int bytes_pp;
  642. struct drm_format_name_buf format_name;
  643. int ret;
  644. switch (mode_cmd->pixel_format) {
  645. case DRM_FORMAT_ARGB8888:
  646. case DRM_FORMAT_XRGB8888:
  647. format = SVGA3D_X8R8G8B8;
  648. bytes_pp = 4;
  649. break;
  650. case DRM_FORMAT_RGB565:
  651. case DRM_FORMAT_XRGB1555:
  652. format = SVGA3D_R5G6B5;
  653. bytes_pp = 2;
  654. break;
  655. case 8:
  656. format = SVGA3D_P8;
  657. bytes_pp = 1;
  658. break;
  659. default:
  660. DRM_ERROR("Invalid framebuffer format %s\n",
  661. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  662. return -EINVAL;
  663. }
  664. content_base_size.width = mode_cmd->pitches[0] / bytes_pp;
  665. content_base_size.height = mode_cmd->height;
  666. content_base_size.depth = 1;
  667. ret = vmw_surface_gb_priv_define(dev,
  668. 0, /* kernel visible only */
  669. 0, /* flags */
  670. format,
  671. true, /* can be a scanout buffer */
  672. 1, /* num of mip levels */
  673. 0,
  674. 0,
  675. content_base_size,
  676. srf_out);
  677. if (ret) {
  678. DRM_ERROR("Failed to allocate proxy content buffer\n");
  679. return ret;
  680. }
  681. res = &(*srf_out)->res;
  682. /* Reserve and switch the backing mob. */
  683. mutex_lock(&res->dev_priv->cmdbuf_mutex);
  684. (void) vmw_resource_reserve(res, false, true);
  685. vmw_dmabuf_unreference(&res->backup);
  686. res->backup = vmw_dmabuf_reference(dmabuf_mob);
  687. res->backup_offset = 0;
  688. vmw_resource_unreserve(res, false, NULL, 0);
  689. mutex_unlock(&res->dev_priv->cmdbuf_mutex);
  690. return 0;
  691. }
  692. static int vmw_kms_new_framebuffer_dmabuf(struct vmw_private *dev_priv,
  693. struct vmw_dma_buffer *dmabuf,
  694. struct vmw_framebuffer **out,
  695. const struct drm_mode_fb_cmd2
  696. *mode_cmd)
  697. {
  698. struct drm_device *dev = dev_priv->dev;
  699. struct vmw_framebuffer_dmabuf *vfbd;
  700. unsigned int requested_size;
  701. struct drm_format_name_buf format_name;
  702. int ret;
  703. requested_size = mode_cmd->height * mode_cmd->pitches[0];
  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->pixel_format) {
  712. case DRM_FORMAT_XRGB8888:
  713. case DRM_FORMAT_ARGB8888:
  714. break;
  715. case DRM_FORMAT_XRGB1555:
  716. case DRM_FORMAT_RGB565:
  717. break;
  718. default:
  719. DRM_ERROR("Invalid pixel format: %s\n",
  720. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  721. return -EINVAL;
  722. }
  723. }
  724. vfbd = kzalloc(sizeof(*vfbd), GFP_KERNEL);
  725. if (!vfbd) {
  726. ret = -ENOMEM;
  727. goto out_err1;
  728. }
  729. drm_helper_mode_fill_fb_struct(dev, &vfbd->base.base, mode_cmd);
  730. vfbd->base.dmabuf = true;
  731. vfbd->buffer = vmw_dmabuf_reference(dmabuf);
  732. vfbd->base.user_handle = mode_cmd->handles[0];
  733. *out = &vfbd->base;
  734. ret = drm_framebuffer_init(dev, &vfbd->base.base,
  735. &vmw_framebuffer_dmabuf_funcs);
  736. if (ret)
  737. goto out_err2;
  738. return 0;
  739. out_err2:
  740. vmw_dmabuf_unreference(&dmabuf);
  741. kfree(vfbd);
  742. out_err1:
  743. return ret;
  744. }
  745. /**
  746. * vmw_kms_new_framebuffer - Create a new framebuffer.
  747. *
  748. * @dev_priv: Pointer to device private struct.
  749. * @dmabuf: Pointer to dma buffer to wrap the kms framebuffer around.
  750. * Either @dmabuf or @surface must be NULL.
  751. * @surface: Pointer to a surface to wrap the kms framebuffer around.
  752. * Either @dmabuf or @surface must be NULL.
  753. * @only_2d: No presents will occur to this dma buffer based framebuffer. This
  754. * Helps the code to do some important optimizations.
  755. * @mode_cmd: Frame-buffer metadata.
  756. */
  757. struct vmw_framebuffer *
  758. vmw_kms_new_framebuffer(struct vmw_private *dev_priv,
  759. struct vmw_dma_buffer *dmabuf,
  760. struct vmw_surface *surface,
  761. bool only_2d,
  762. const struct drm_mode_fb_cmd2 *mode_cmd)
  763. {
  764. struct vmw_framebuffer *vfb = NULL;
  765. bool is_dmabuf_proxy = false;
  766. int ret;
  767. /*
  768. * We cannot use the SurfaceDMA command in an non-accelerated VM,
  769. * therefore, wrap the DMA buf in a surface so we can use the
  770. * SurfaceCopy command.
  771. */
  772. if (dmabuf && only_2d &&
  773. dev_priv->active_display_unit == vmw_du_screen_target) {
  774. ret = vmw_create_dmabuf_proxy(dev_priv->dev, mode_cmd,
  775. dmabuf, &surface);
  776. if (ret)
  777. return ERR_PTR(ret);
  778. is_dmabuf_proxy = true;
  779. }
  780. /* Create the new framebuffer depending one what we have */
  781. if (surface) {
  782. ret = vmw_kms_new_framebuffer_surface(dev_priv, surface, &vfb,
  783. mode_cmd,
  784. is_dmabuf_proxy);
  785. /*
  786. * vmw_create_dmabuf_proxy() adds a reference that is no longer
  787. * needed
  788. */
  789. if (is_dmabuf_proxy)
  790. vmw_surface_unreference(&surface);
  791. } else if (dmabuf) {
  792. ret = vmw_kms_new_framebuffer_dmabuf(dev_priv, dmabuf, &vfb,
  793. mode_cmd);
  794. } else {
  795. BUG();
  796. }
  797. if (ret)
  798. return ERR_PTR(ret);
  799. vfb->pin = vmw_framebuffer_pin;
  800. vfb->unpin = vmw_framebuffer_unpin;
  801. return vfb;
  802. }
  803. /*
  804. * Generic Kernel modesetting functions
  805. */
  806. static struct drm_framebuffer *vmw_kms_fb_create(struct drm_device *dev,
  807. struct drm_file *file_priv,
  808. const struct drm_mode_fb_cmd2 *mode_cmd)
  809. {
  810. struct vmw_private *dev_priv = vmw_priv(dev);
  811. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  812. struct vmw_framebuffer *vfb = NULL;
  813. struct vmw_surface *surface = NULL;
  814. struct vmw_dma_buffer *bo = NULL;
  815. struct ttm_base_object *user_obj;
  816. int ret;
  817. /**
  818. * This code should be conditioned on Screen Objects not being used.
  819. * If screen objects are used, we can allocate a GMR to hold the
  820. * requested framebuffer.
  821. */
  822. if (!vmw_kms_validate_mode_vram(dev_priv,
  823. mode_cmd->pitches[0],
  824. mode_cmd->height)) {
  825. DRM_ERROR("Requested mode exceed bounding box limit.\n");
  826. return ERR_PTR(-ENOMEM);
  827. }
  828. /*
  829. * Take a reference on the user object of the resource
  830. * backing the kms fb. This ensures that user-space handle
  831. * lookups on that resource will always work as long as
  832. * it's registered with a kms framebuffer. This is important,
  833. * since vmw_execbuf_process identifies resources in the
  834. * command stream using user-space handles.
  835. */
  836. user_obj = ttm_base_object_lookup(tfile, mode_cmd->handles[0]);
  837. if (unlikely(user_obj == NULL)) {
  838. DRM_ERROR("Could not locate requested kms frame buffer.\n");
  839. return ERR_PTR(-ENOENT);
  840. }
  841. /**
  842. * End conditioned code.
  843. */
  844. /* returns either a dmabuf or surface */
  845. ret = vmw_user_lookup_handle(dev_priv, tfile,
  846. mode_cmd->handles[0],
  847. &surface, &bo);
  848. if (ret)
  849. goto err_out;
  850. vfb = vmw_kms_new_framebuffer(dev_priv, bo, surface,
  851. !(dev_priv->capabilities & SVGA_CAP_3D),
  852. mode_cmd);
  853. if (IS_ERR(vfb)) {
  854. ret = PTR_ERR(vfb);
  855. goto err_out;
  856. }
  857. err_out:
  858. /* vmw_user_lookup_handle takes one ref so does new_fb */
  859. if (bo)
  860. vmw_dmabuf_unreference(&bo);
  861. if (surface)
  862. vmw_surface_unreference(&surface);
  863. if (ret) {
  864. DRM_ERROR("failed to create vmw_framebuffer: %i\n", ret);
  865. ttm_base_object_unref(&user_obj);
  866. return ERR_PTR(ret);
  867. } else
  868. vfb->user_obj = user_obj;
  869. return &vfb->base;
  870. }
  871. static const struct drm_mode_config_funcs vmw_kms_funcs = {
  872. .fb_create = vmw_kms_fb_create,
  873. };
  874. static int vmw_kms_generic_present(struct vmw_private *dev_priv,
  875. struct drm_file *file_priv,
  876. struct vmw_framebuffer *vfb,
  877. struct vmw_surface *surface,
  878. uint32_t sid,
  879. int32_t destX, int32_t destY,
  880. struct drm_vmw_rect *clips,
  881. uint32_t num_clips)
  882. {
  883. return vmw_kms_sou_do_surface_dirty(dev_priv, vfb, NULL, clips,
  884. &surface->res, destX, destY,
  885. num_clips, 1, NULL);
  886. }
  887. int vmw_kms_present(struct vmw_private *dev_priv,
  888. struct drm_file *file_priv,
  889. struct vmw_framebuffer *vfb,
  890. struct vmw_surface *surface,
  891. uint32_t sid,
  892. int32_t destX, int32_t destY,
  893. struct drm_vmw_rect *clips,
  894. uint32_t num_clips)
  895. {
  896. int ret;
  897. switch (dev_priv->active_display_unit) {
  898. case vmw_du_screen_target:
  899. ret = vmw_kms_stdu_surface_dirty(dev_priv, vfb, NULL, clips,
  900. &surface->res, destX, destY,
  901. num_clips, 1, NULL);
  902. break;
  903. case vmw_du_screen_object:
  904. ret = vmw_kms_generic_present(dev_priv, file_priv, vfb, surface,
  905. sid, destX, destY, clips,
  906. num_clips);
  907. break;
  908. default:
  909. WARN_ONCE(true,
  910. "Present called with invalid display system.\n");
  911. ret = -ENOSYS;
  912. break;
  913. }
  914. if (ret)
  915. return ret;
  916. vmw_fifo_flush(dev_priv, false);
  917. return 0;
  918. }
  919. static void
  920. vmw_kms_create_hotplug_mode_update_property(struct vmw_private *dev_priv)
  921. {
  922. if (dev_priv->hotplug_mode_update_property)
  923. return;
  924. dev_priv->hotplug_mode_update_property =
  925. drm_property_create_range(dev_priv->dev,
  926. DRM_MODE_PROP_IMMUTABLE,
  927. "hotplug_mode_update", 0, 1);
  928. if (!dev_priv->hotplug_mode_update_property)
  929. return;
  930. }
  931. int vmw_kms_init(struct vmw_private *dev_priv)
  932. {
  933. struct drm_device *dev = dev_priv->dev;
  934. int ret;
  935. drm_mode_config_init(dev);
  936. dev->mode_config.funcs = &vmw_kms_funcs;
  937. dev->mode_config.min_width = 1;
  938. dev->mode_config.min_height = 1;
  939. dev->mode_config.max_width = dev_priv->texture_max_width;
  940. dev->mode_config.max_height = dev_priv->texture_max_height;
  941. drm_mode_create_suggested_offset_properties(dev);
  942. vmw_kms_create_hotplug_mode_update_property(dev_priv);
  943. ret = vmw_kms_stdu_init_display(dev_priv);
  944. if (ret) {
  945. ret = vmw_kms_sou_init_display(dev_priv);
  946. if (ret) /* Fallback */
  947. ret = vmw_kms_ldu_init_display(dev_priv);
  948. }
  949. return ret;
  950. }
  951. int vmw_kms_close(struct vmw_private *dev_priv)
  952. {
  953. int ret;
  954. /*
  955. * Docs says we should take the lock before calling this function
  956. * but since it destroys encoders and our destructor calls
  957. * drm_encoder_cleanup which takes the lock we deadlock.
  958. */
  959. drm_mode_config_cleanup(dev_priv->dev);
  960. if (dev_priv->active_display_unit == vmw_du_screen_object)
  961. ret = vmw_kms_sou_close_display(dev_priv);
  962. else if (dev_priv->active_display_unit == vmw_du_screen_target)
  963. ret = vmw_kms_stdu_close_display(dev_priv);
  964. else
  965. ret = vmw_kms_ldu_close_display(dev_priv);
  966. return ret;
  967. }
  968. int vmw_kms_cursor_bypass_ioctl(struct drm_device *dev, void *data,
  969. struct drm_file *file_priv)
  970. {
  971. struct drm_vmw_cursor_bypass_arg *arg = data;
  972. struct vmw_display_unit *du;
  973. struct drm_crtc *crtc;
  974. int ret = 0;
  975. mutex_lock(&dev->mode_config.mutex);
  976. if (arg->flags & DRM_VMW_CURSOR_BYPASS_ALL) {
  977. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  978. du = vmw_crtc_to_du(crtc);
  979. du->hotspot_x = arg->xhot;
  980. du->hotspot_y = arg->yhot;
  981. }
  982. mutex_unlock(&dev->mode_config.mutex);
  983. return 0;
  984. }
  985. crtc = drm_crtc_find(dev, arg->crtc_id);
  986. if (!crtc) {
  987. ret = -ENOENT;
  988. goto out;
  989. }
  990. du = vmw_crtc_to_du(crtc);
  991. du->hotspot_x = arg->xhot;
  992. du->hotspot_y = arg->yhot;
  993. out:
  994. mutex_unlock(&dev->mode_config.mutex);
  995. return ret;
  996. }
  997. int vmw_kms_write_svga(struct vmw_private *vmw_priv,
  998. unsigned width, unsigned height, unsigned pitch,
  999. unsigned bpp, unsigned depth)
  1000. {
  1001. if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
  1002. vmw_write(vmw_priv, SVGA_REG_PITCHLOCK, pitch);
  1003. else if (vmw_fifo_have_pitchlock(vmw_priv))
  1004. vmw_mmio_write(pitch, vmw_priv->mmio_virt +
  1005. SVGA_FIFO_PITCHLOCK);
  1006. vmw_write(vmw_priv, SVGA_REG_WIDTH, width);
  1007. vmw_write(vmw_priv, SVGA_REG_HEIGHT, height);
  1008. vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, bpp);
  1009. if (vmw_read(vmw_priv, SVGA_REG_DEPTH) != depth) {
  1010. DRM_ERROR("Invalid depth %u for %u bpp, host expects %u\n",
  1011. depth, bpp, vmw_read(vmw_priv, SVGA_REG_DEPTH));
  1012. return -EINVAL;
  1013. }
  1014. return 0;
  1015. }
  1016. int vmw_kms_save_vga(struct vmw_private *vmw_priv)
  1017. {
  1018. struct vmw_vga_topology_state *save;
  1019. uint32_t i;
  1020. vmw_priv->vga_width = vmw_read(vmw_priv, SVGA_REG_WIDTH);
  1021. vmw_priv->vga_height = vmw_read(vmw_priv, SVGA_REG_HEIGHT);
  1022. vmw_priv->vga_bpp = vmw_read(vmw_priv, SVGA_REG_BITS_PER_PIXEL);
  1023. if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
  1024. vmw_priv->vga_pitchlock =
  1025. vmw_read(vmw_priv, SVGA_REG_PITCHLOCK);
  1026. else if (vmw_fifo_have_pitchlock(vmw_priv))
  1027. vmw_priv->vga_pitchlock = vmw_mmio_read(vmw_priv->mmio_virt +
  1028. SVGA_FIFO_PITCHLOCK);
  1029. if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
  1030. return 0;
  1031. vmw_priv->num_displays = vmw_read(vmw_priv,
  1032. SVGA_REG_NUM_GUEST_DISPLAYS);
  1033. if (vmw_priv->num_displays == 0)
  1034. vmw_priv->num_displays = 1;
  1035. for (i = 0; i < vmw_priv->num_displays; ++i) {
  1036. save = &vmw_priv->vga_save[i];
  1037. vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
  1038. save->primary = vmw_read(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY);
  1039. save->pos_x = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_X);
  1040. save->pos_y = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y);
  1041. save->width = vmw_read(vmw_priv, SVGA_REG_DISPLAY_WIDTH);
  1042. save->height = vmw_read(vmw_priv, SVGA_REG_DISPLAY_HEIGHT);
  1043. vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
  1044. if (i == 0 && vmw_priv->num_displays == 1 &&
  1045. save->width == 0 && save->height == 0) {
  1046. /*
  1047. * It should be fairly safe to assume that these
  1048. * values are uninitialized.
  1049. */
  1050. save->width = vmw_priv->vga_width - save->pos_x;
  1051. save->height = vmw_priv->vga_height - save->pos_y;
  1052. }
  1053. }
  1054. return 0;
  1055. }
  1056. int vmw_kms_restore_vga(struct vmw_private *vmw_priv)
  1057. {
  1058. struct vmw_vga_topology_state *save;
  1059. uint32_t i;
  1060. vmw_write(vmw_priv, SVGA_REG_WIDTH, vmw_priv->vga_width);
  1061. vmw_write(vmw_priv, SVGA_REG_HEIGHT, vmw_priv->vga_height);
  1062. vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, vmw_priv->vga_bpp);
  1063. if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
  1064. vmw_write(vmw_priv, SVGA_REG_PITCHLOCK,
  1065. vmw_priv->vga_pitchlock);
  1066. else if (vmw_fifo_have_pitchlock(vmw_priv))
  1067. vmw_mmio_write(vmw_priv->vga_pitchlock,
  1068. vmw_priv->mmio_virt + SVGA_FIFO_PITCHLOCK);
  1069. if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
  1070. return 0;
  1071. for (i = 0; i < vmw_priv->num_displays; ++i) {
  1072. save = &vmw_priv->vga_save[i];
  1073. vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
  1074. vmw_write(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY, save->primary);
  1075. vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_X, save->pos_x);
  1076. vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y, save->pos_y);
  1077. vmw_write(vmw_priv, SVGA_REG_DISPLAY_WIDTH, save->width);
  1078. vmw_write(vmw_priv, SVGA_REG_DISPLAY_HEIGHT, save->height);
  1079. vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
  1080. }
  1081. return 0;
  1082. }
  1083. bool vmw_kms_validate_mode_vram(struct vmw_private *dev_priv,
  1084. uint32_t pitch,
  1085. uint32_t height)
  1086. {
  1087. return ((u64) pitch * (u64) height) < (u64)
  1088. ((dev_priv->active_display_unit == vmw_du_screen_target) ?
  1089. dev_priv->prim_bb_mem : dev_priv->vram_size);
  1090. }
  1091. /**
  1092. * Function called by DRM code called with vbl_lock held.
  1093. */
  1094. u32 vmw_get_vblank_counter(struct drm_device *dev, unsigned int pipe)
  1095. {
  1096. return 0;
  1097. }
  1098. /**
  1099. * Function called by DRM code called with vbl_lock held.
  1100. */
  1101. int vmw_enable_vblank(struct drm_device *dev, unsigned int pipe)
  1102. {
  1103. return -ENOSYS;
  1104. }
  1105. /**
  1106. * Function called by DRM code called with vbl_lock held.
  1107. */
  1108. void vmw_disable_vblank(struct drm_device *dev, unsigned int pipe)
  1109. {
  1110. }
  1111. /*
  1112. * Small shared kms functions.
  1113. */
  1114. static int vmw_du_update_layout(struct vmw_private *dev_priv, unsigned num,
  1115. struct drm_vmw_rect *rects)
  1116. {
  1117. struct drm_device *dev = dev_priv->dev;
  1118. struct vmw_display_unit *du;
  1119. struct drm_connector *con;
  1120. mutex_lock(&dev->mode_config.mutex);
  1121. #if 0
  1122. {
  1123. unsigned int i;
  1124. DRM_INFO("%s: new layout ", __func__);
  1125. for (i = 0; i < num; i++)
  1126. DRM_INFO("(%i, %i %ux%u) ", rects[i].x, rects[i].y,
  1127. rects[i].w, rects[i].h);
  1128. DRM_INFO("\n");
  1129. }
  1130. #endif
  1131. list_for_each_entry(con, &dev->mode_config.connector_list, head) {
  1132. du = vmw_connector_to_du(con);
  1133. if (num > du->unit) {
  1134. du->pref_width = rects[du->unit].w;
  1135. du->pref_height = rects[du->unit].h;
  1136. du->pref_active = true;
  1137. du->gui_x = rects[du->unit].x;
  1138. du->gui_y = rects[du->unit].y;
  1139. drm_object_property_set_value
  1140. (&con->base, dev->mode_config.suggested_x_property,
  1141. du->gui_x);
  1142. drm_object_property_set_value
  1143. (&con->base, dev->mode_config.suggested_y_property,
  1144. du->gui_y);
  1145. } else {
  1146. du->pref_width = 800;
  1147. du->pref_height = 600;
  1148. du->pref_active = false;
  1149. drm_object_property_set_value
  1150. (&con->base, dev->mode_config.suggested_x_property,
  1151. 0);
  1152. drm_object_property_set_value
  1153. (&con->base, dev->mode_config.suggested_y_property,
  1154. 0);
  1155. }
  1156. con->status = vmw_du_connector_detect(con, true);
  1157. }
  1158. mutex_unlock(&dev->mode_config.mutex);
  1159. drm_sysfs_hotplug_event(dev);
  1160. return 0;
  1161. }
  1162. int vmw_du_crtc_gamma_set(struct drm_crtc *crtc,
  1163. u16 *r, u16 *g, u16 *b,
  1164. uint32_t size)
  1165. {
  1166. struct vmw_private *dev_priv = vmw_priv(crtc->dev);
  1167. int i;
  1168. for (i = 0; i < size; i++) {
  1169. DRM_DEBUG("%d r/g/b = 0x%04x / 0x%04x / 0x%04x\n", i,
  1170. r[i], g[i], b[i]);
  1171. vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 0, r[i] >> 8);
  1172. vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 1, g[i] >> 8);
  1173. vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 2, b[i] >> 8);
  1174. }
  1175. return 0;
  1176. }
  1177. int vmw_du_connector_dpms(struct drm_connector *connector, int mode)
  1178. {
  1179. return 0;
  1180. }
  1181. enum drm_connector_status
  1182. vmw_du_connector_detect(struct drm_connector *connector, bool force)
  1183. {
  1184. uint32_t num_displays;
  1185. struct drm_device *dev = connector->dev;
  1186. struct vmw_private *dev_priv = vmw_priv(dev);
  1187. struct vmw_display_unit *du = vmw_connector_to_du(connector);
  1188. num_displays = vmw_read(dev_priv, SVGA_REG_NUM_DISPLAYS);
  1189. return ((vmw_connector_to_du(connector)->unit < num_displays &&
  1190. du->pref_active) ?
  1191. connector_status_connected : connector_status_disconnected);
  1192. }
  1193. static struct drm_display_mode vmw_kms_connector_builtin[] = {
  1194. /* 640x480@60Hz */
  1195. { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 25175, 640, 656,
  1196. 752, 800, 0, 480, 489, 492, 525, 0,
  1197. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
  1198. /* 800x600@60Hz */
  1199. { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 40000, 800, 840,
  1200. 968, 1056, 0, 600, 601, 605, 628, 0,
  1201. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1202. /* 1024x768@60Hz */
  1203. { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 65000, 1024, 1048,
  1204. 1184, 1344, 0, 768, 771, 777, 806, 0,
  1205. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
  1206. /* 1152x864@75Hz */
  1207. { DRM_MODE("1152x864", DRM_MODE_TYPE_DRIVER, 108000, 1152, 1216,
  1208. 1344, 1600, 0, 864, 865, 868, 900, 0,
  1209. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1210. /* 1280x768@60Hz */
  1211. { DRM_MODE("1280x768", DRM_MODE_TYPE_DRIVER, 79500, 1280, 1344,
  1212. 1472, 1664, 0, 768, 771, 778, 798, 0,
  1213. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1214. /* 1280x800@60Hz */
  1215. { DRM_MODE("1280x800", DRM_MODE_TYPE_DRIVER, 83500, 1280, 1352,
  1216. 1480, 1680, 0, 800, 803, 809, 831, 0,
  1217. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
  1218. /* 1280x960@60Hz */
  1219. { DRM_MODE("1280x960", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1376,
  1220. 1488, 1800, 0, 960, 961, 964, 1000, 0,
  1221. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1222. /* 1280x1024@60Hz */
  1223. { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1328,
  1224. 1440, 1688, 0, 1024, 1025, 1028, 1066, 0,
  1225. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1226. /* 1360x768@60Hz */
  1227. { DRM_MODE("1360x768", DRM_MODE_TYPE_DRIVER, 85500, 1360, 1424,
  1228. 1536, 1792, 0, 768, 771, 777, 795, 0,
  1229. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1230. /* 1440x1050@60Hz */
  1231. { DRM_MODE("1400x1050", DRM_MODE_TYPE_DRIVER, 121750, 1400, 1488,
  1232. 1632, 1864, 0, 1050, 1053, 1057, 1089, 0,
  1233. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1234. /* 1440x900@60Hz */
  1235. { DRM_MODE("1440x900", DRM_MODE_TYPE_DRIVER, 106500, 1440, 1520,
  1236. 1672, 1904, 0, 900, 903, 909, 934, 0,
  1237. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1238. /* 1600x1200@60Hz */
  1239. { DRM_MODE("1600x1200", DRM_MODE_TYPE_DRIVER, 162000, 1600, 1664,
  1240. 1856, 2160, 0, 1200, 1201, 1204, 1250, 0,
  1241. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1242. /* 1680x1050@60Hz */
  1243. { DRM_MODE("1680x1050", DRM_MODE_TYPE_DRIVER, 146250, 1680, 1784,
  1244. 1960, 2240, 0, 1050, 1053, 1059, 1089, 0,
  1245. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1246. /* 1792x1344@60Hz */
  1247. { DRM_MODE("1792x1344", DRM_MODE_TYPE_DRIVER, 204750, 1792, 1920,
  1248. 2120, 2448, 0, 1344, 1345, 1348, 1394, 0,
  1249. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1250. /* 1853x1392@60Hz */
  1251. { DRM_MODE("1856x1392", DRM_MODE_TYPE_DRIVER, 218250, 1856, 1952,
  1252. 2176, 2528, 0, 1392, 1393, 1396, 1439, 0,
  1253. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1254. /* 1920x1200@60Hz */
  1255. { DRM_MODE("1920x1200", DRM_MODE_TYPE_DRIVER, 193250, 1920, 2056,
  1256. 2256, 2592, 0, 1200, 1203, 1209, 1245, 0,
  1257. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1258. /* 1920x1440@60Hz */
  1259. { DRM_MODE("1920x1440", DRM_MODE_TYPE_DRIVER, 234000, 1920, 2048,
  1260. 2256, 2600, 0, 1440, 1441, 1444, 1500, 0,
  1261. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1262. /* 2560x1600@60Hz */
  1263. { DRM_MODE("2560x1600", DRM_MODE_TYPE_DRIVER, 348500, 2560, 2752,
  1264. 3032, 3504, 0, 1600, 1603, 1609, 1658, 0,
  1265. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1266. /* Terminate */
  1267. { DRM_MODE("", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) },
  1268. };
  1269. /**
  1270. * vmw_guess_mode_timing - Provide fake timings for a
  1271. * 60Hz vrefresh mode.
  1272. *
  1273. * @mode - Pointer to a struct drm_display_mode with hdisplay and vdisplay
  1274. * members filled in.
  1275. */
  1276. void vmw_guess_mode_timing(struct drm_display_mode *mode)
  1277. {
  1278. mode->hsync_start = mode->hdisplay + 50;
  1279. mode->hsync_end = mode->hsync_start + 50;
  1280. mode->htotal = mode->hsync_end + 50;
  1281. mode->vsync_start = mode->vdisplay + 50;
  1282. mode->vsync_end = mode->vsync_start + 50;
  1283. mode->vtotal = mode->vsync_end + 50;
  1284. mode->clock = (u32)mode->htotal * (u32)mode->vtotal / 100 * 6;
  1285. mode->vrefresh = drm_mode_vrefresh(mode);
  1286. }
  1287. int vmw_du_connector_fill_modes(struct drm_connector *connector,
  1288. uint32_t max_width, uint32_t max_height)
  1289. {
  1290. struct vmw_display_unit *du = vmw_connector_to_du(connector);
  1291. struct drm_device *dev = connector->dev;
  1292. struct vmw_private *dev_priv = vmw_priv(dev);
  1293. struct drm_display_mode *mode = NULL;
  1294. struct drm_display_mode *bmode;
  1295. struct drm_display_mode prefmode = { DRM_MODE("preferred",
  1296. DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED,
  1297. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1298. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC)
  1299. };
  1300. int i;
  1301. u32 assumed_bpp = 4;
  1302. if (dev_priv->assume_16bpp)
  1303. assumed_bpp = 2;
  1304. if (dev_priv->active_display_unit == vmw_du_screen_target) {
  1305. max_width = min(max_width, dev_priv->stdu_max_width);
  1306. max_height = min(max_height, dev_priv->stdu_max_height);
  1307. }
  1308. /* Add preferred mode */
  1309. mode = drm_mode_duplicate(dev, &prefmode);
  1310. if (!mode)
  1311. return 0;
  1312. mode->hdisplay = du->pref_width;
  1313. mode->vdisplay = du->pref_height;
  1314. vmw_guess_mode_timing(mode);
  1315. if (vmw_kms_validate_mode_vram(dev_priv,
  1316. mode->hdisplay * assumed_bpp,
  1317. mode->vdisplay)) {
  1318. drm_mode_probed_add(connector, mode);
  1319. } else {
  1320. drm_mode_destroy(dev, mode);
  1321. mode = NULL;
  1322. }
  1323. if (du->pref_mode) {
  1324. list_del_init(&du->pref_mode->head);
  1325. drm_mode_destroy(dev, du->pref_mode);
  1326. }
  1327. /* mode might be null here, this is intended */
  1328. du->pref_mode = mode;
  1329. for (i = 0; vmw_kms_connector_builtin[i].type != 0; i++) {
  1330. bmode = &vmw_kms_connector_builtin[i];
  1331. if (bmode->hdisplay > max_width ||
  1332. bmode->vdisplay > max_height)
  1333. continue;
  1334. if (!vmw_kms_validate_mode_vram(dev_priv,
  1335. bmode->hdisplay * assumed_bpp,
  1336. bmode->vdisplay))
  1337. continue;
  1338. mode = drm_mode_duplicate(dev, bmode);
  1339. if (!mode)
  1340. return 0;
  1341. mode->vrefresh = drm_mode_vrefresh(mode);
  1342. drm_mode_probed_add(connector, mode);
  1343. }
  1344. drm_mode_connector_list_update(connector);
  1345. /* Move the prefered mode first, help apps pick the right mode. */
  1346. drm_mode_sort(&connector->modes);
  1347. return 1;
  1348. }
  1349. int vmw_du_connector_set_property(struct drm_connector *connector,
  1350. struct drm_property *property,
  1351. uint64_t val)
  1352. {
  1353. struct vmw_display_unit *du = vmw_connector_to_du(connector);
  1354. struct vmw_private *dev_priv = vmw_priv(connector->dev);
  1355. if (property == dev_priv->implicit_placement_property)
  1356. du->is_implicit = val;
  1357. return 0;
  1358. }
  1359. int vmw_kms_update_layout_ioctl(struct drm_device *dev, void *data,
  1360. struct drm_file *file_priv)
  1361. {
  1362. struct vmw_private *dev_priv = vmw_priv(dev);
  1363. struct drm_vmw_update_layout_arg *arg =
  1364. (struct drm_vmw_update_layout_arg *)data;
  1365. void __user *user_rects;
  1366. struct drm_vmw_rect *rects;
  1367. unsigned rects_size;
  1368. int ret;
  1369. int i;
  1370. u64 total_pixels = 0;
  1371. struct drm_mode_config *mode_config = &dev->mode_config;
  1372. struct drm_vmw_rect bounding_box = {0};
  1373. if (!arg->num_outputs) {
  1374. struct drm_vmw_rect def_rect = {0, 0, 800, 600};
  1375. vmw_du_update_layout(dev_priv, 1, &def_rect);
  1376. return 0;
  1377. }
  1378. rects_size = arg->num_outputs * sizeof(struct drm_vmw_rect);
  1379. rects = kcalloc(arg->num_outputs, sizeof(struct drm_vmw_rect),
  1380. GFP_KERNEL);
  1381. if (unlikely(!rects))
  1382. return -ENOMEM;
  1383. user_rects = (void __user *)(unsigned long)arg->rects;
  1384. ret = copy_from_user(rects, user_rects, rects_size);
  1385. if (unlikely(ret != 0)) {
  1386. DRM_ERROR("Failed to get rects.\n");
  1387. ret = -EFAULT;
  1388. goto out_free;
  1389. }
  1390. for (i = 0; i < arg->num_outputs; ++i) {
  1391. if (rects[i].x < 0 ||
  1392. rects[i].y < 0 ||
  1393. rects[i].x + rects[i].w > mode_config->max_width ||
  1394. rects[i].y + rects[i].h > mode_config->max_height) {
  1395. DRM_ERROR("Invalid GUI layout.\n");
  1396. ret = -EINVAL;
  1397. goto out_free;
  1398. }
  1399. /*
  1400. * bounding_box.w and bunding_box.h are used as
  1401. * lower-right coordinates
  1402. */
  1403. if (rects[i].x + rects[i].w > bounding_box.w)
  1404. bounding_box.w = rects[i].x + rects[i].w;
  1405. if (rects[i].y + rects[i].h > bounding_box.h)
  1406. bounding_box.h = rects[i].y + rects[i].h;
  1407. total_pixels += (u64) rects[i].w * (u64) rects[i].h;
  1408. }
  1409. if (dev_priv->active_display_unit == vmw_du_screen_target) {
  1410. /*
  1411. * For Screen Targets, the limits for a toplogy are:
  1412. * 1. Bounding box (assuming 32bpp) must be < prim_bb_mem
  1413. * 2. Total pixels (assuming 32bpp) must be < prim_bb_mem
  1414. */
  1415. u64 bb_mem = bounding_box.w * bounding_box.h * 4;
  1416. u64 pixel_mem = total_pixels * 4;
  1417. if (bb_mem > dev_priv->prim_bb_mem) {
  1418. DRM_ERROR("Topology is beyond supported limits.\n");
  1419. ret = -EINVAL;
  1420. goto out_free;
  1421. }
  1422. if (pixel_mem > dev_priv->prim_bb_mem) {
  1423. DRM_ERROR("Combined output size too large\n");
  1424. ret = -EINVAL;
  1425. goto out_free;
  1426. }
  1427. }
  1428. vmw_du_update_layout(dev_priv, arg->num_outputs, rects);
  1429. out_free:
  1430. kfree(rects);
  1431. return ret;
  1432. }
  1433. /**
  1434. * vmw_kms_helper_dirty - Helper to build commands and perform actions based
  1435. * on a set of cliprects and a set of display units.
  1436. *
  1437. * @dev_priv: Pointer to a device private structure.
  1438. * @framebuffer: Pointer to the framebuffer on which to perform the actions.
  1439. * @clips: A set of struct drm_clip_rect. Either this os @vclips must be NULL.
  1440. * Cliprects are given in framebuffer coordinates.
  1441. * @vclips: A set of struct drm_vmw_rect cliprects. Either this or @clips must
  1442. * be NULL. Cliprects are given in source coordinates.
  1443. * @dest_x: X coordinate offset for the crtc / destination clip rects.
  1444. * @dest_y: Y coordinate offset for the crtc / destination clip rects.
  1445. * @num_clips: Number of cliprects in the @clips or @vclips array.
  1446. * @increment: Integer with which to increment the clip counter when looping.
  1447. * Used to skip a predetermined number of clip rects.
  1448. * @dirty: Closure structure. See the description of struct vmw_kms_dirty.
  1449. */
  1450. int vmw_kms_helper_dirty(struct vmw_private *dev_priv,
  1451. struct vmw_framebuffer *framebuffer,
  1452. const struct drm_clip_rect *clips,
  1453. const struct drm_vmw_rect *vclips,
  1454. s32 dest_x, s32 dest_y,
  1455. int num_clips,
  1456. int increment,
  1457. struct vmw_kms_dirty *dirty)
  1458. {
  1459. struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
  1460. struct drm_crtc *crtc;
  1461. u32 num_units = 0;
  1462. u32 i, k;
  1463. dirty->dev_priv = dev_priv;
  1464. list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list, head) {
  1465. if (crtc->primary->fb != &framebuffer->base)
  1466. continue;
  1467. units[num_units++] = vmw_crtc_to_du(crtc);
  1468. }
  1469. for (k = 0; k < num_units; k++) {
  1470. struct vmw_display_unit *unit = units[k];
  1471. s32 crtc_x = unit->crtc.x;
  1472. s32 crtc_y = unit->crtc.y;
  1473. s32 crtc_width = unit->crtc.mode.hdisplay;
  1474. s32 crtc_height = unit->crtc.mode.vdisplay;
  1475. const struct drm_clip_rect *clips_ptr = clips;
  1476. const struct drm_vmw_rect *vclips_ptr = vclips;
  1477. dirty->unit = unit;
  1478. if (dirty->fifo_reserve_size > 0) {
  1479. dirty->cmd = vmw_fifo_reserve(dev_priv,
  1480. dirty->fifo_reserve_size);
  1481. if (!dirty->cmd) {
  1482. DRM_ERROR("Couldn't reserve fifo space "
  1483. "for dirty blits.\n");
  1484. return -ENOMEM;
  1485. }
  1486. memset(dirty->cmd, 0, dirty->fifo_reserve_size);
  1487. }
  1488. dirty->num_hits = 0;
  1489. for (i = 0; i < num_clips; i++, clips_ptr += increment,
  1490. vclips_ptr += increment) {
  1491. s32 clip_left;
  1492. s32 clip_top;
  1493. /*
  1494. * Select clip array type. Note that integer type
  1495. * in @clips is unsigned short, whereas in @vclips
  1496. * it's 32-bit.
  1497. */
  1498. if (clips) {
  1499. dirty->fb_x = (s32) clips_ptr->x1;
  1500. dirty->fb_y = (s32) clips_ptr->y1;
  1501. dirty->unit_x2 = (s32) clips_ptr->x2 + dest_x -
  1502. crtc_x;
  1503. dirty->unit_y2 = (s32) clips_ptr->y2 + dest_y -
  1504. crtc_y;
  1505. } else {
  1506. dirty->fb_x = vclips_ptr->x;
  1507. dirty->fb_y = vclips_ptr->y;
  1508. dirty->unit_x2 = dirty->fb_x + vclips_ptr->w +
  1509. dest_x - crtc_x;
  1510. dirty->unit_y2 = dirty->fb_y + vclips_ptr->h +
  1511. dest_y - crtc_y;
  1512. }
  1513. dirty->unit_x1 = dirty->fb_x + dest_x - crtc_x;
  1514. dirty->unit_y1 = dirty->fb_y + dest_y - crtc_y;
  1515. /* Skip this clip if it's outside the crtc region */
  1516. if (dirty->unit_x1 >= crtc_width ||
  1517. dirty->unit_y1 >= crtc_height ||
  1518. dirty->unit_x2 <= 0 || dirty->unit_y2 <= 0)
  1519. continue;
  1520. /* Clip right and bottom to crtc limits */
  1521. dirty->unit_x2 = min_t(s32, dirty->unit_x2,
  1522. crtc_width);
  1523. dirty->unit_y2 = min_t(s32, dirty->unit_y2,
  1524. crtc_height);
  1525. /* Clip left and top to crtc limits */
  1526. clip_left = min_t(s32, dirty->unit_x1, 0);
  1527. clip_top = min_t(s32, dirty->unit_y1, 0);
  1528. dirty->unit_x1 -= clip_left;
  1529. dirty->unit_y1 -= clip_top;
  1530. dirty->fb_x -= clip_left;
  1531. dirty->fb_y -= clip_top;
  1532. dirty->clip(dirty);
  1533. }
  1534. dirty->fifo_commit(dirty);
  1535. }
  1536. return 0;
  1537. }
  1538. /**
  1539. * vmw_kms_helper_buffer_prepare - Reserve and validate a buffer object before
  1540. * command submission.
  1541. *
  1542. * @dev_priv. Pointer to a device private structure.
  1543. * @buf: The buffer object
  1544. * @interruptible: Whether to perform waits as interruptible.
  1545. * @validate_as_mob: Whether the buffer should be validated as a MOB. If false,
  1546. * The buffer will be validated as a GMR. Already pinned buffers will not be
  1547. * validated.
  1548. *
  1549. * Returns 0 on success, negative error code on failure, -ERESTARTSYS if
  1550. * interrupted by a signal.
  1551. */
  1552. int vmw_kms_helper_buffer_prepare(struct vmw_private *dev_priv,
  1553. struct vmw_dma_buffer *buf,
  1554. bool interruptible,
  1555. bool validate_as_mob)
  1556. {
  1557. struct ttm_buffer_object *bo = &buf->base;
  1558. int ret;
  1559. ttm_bo_reserve(bo, false, false, NULL);
  1560. ret = vmw_validate_single_buffer(dev_priv, bo, interruptible,
  1561. validate_as_mob);
  1562. if (ret)
  1563. ttm_bo_unreserve(bo);
  1564. return ret;
  1565. }
  1566. /**
  1567. * vmw_kms_helper_buffer_revert - Undo the actions of
  1568. * vmw_kms_helper_buffer_prepare.
  1569. *
  1570. * @res: Pointer to the buffer object.
  1571. *
  1572. * Helper to be used if an error forces the caller to undo the actions of
  1573. * vmw_kms_helper_buffer_prepare.
  1574. */
  1575. void vmw_kms_helper_buffer_revert(struct vmw_dma_buffer *buf)
  1576. {
  1577. if (buf)
  1578. ttm_bo_unreserve(&buf->base);
  1579. }
  1580. /**
  1581. * vmw_kms_helper_buffer_finish - Unreserve and fence a buffer object after
  1582. * kms command submission.
  1583. *
  1584. * @dev_priv: Pointer to a device private structure.
  1585. * @file_priv: Pointer to a struct drm_file representing the caller's
  1586. * connection. Must be set to NULL if @user_fence_rep is NULL, and conversely
  1587. * if non-NULL, @user_fence_rep must be non-NULL.
  1588. * @buf: The buffer object.
  1589. * @out_fence: Optional pointer to a fence pointer. If non-NULL, a
  1590. * ref-counted fence pointer is returned here.
  1591. * @user_fence_rep: Optional pointer to a user-space provided struct
  1592. * drm_vmw_fence_rep. If provided, @file_priv must also be provided and the
  1593. * function copies fence data to user-space in a fail-safe manner.
  1594. */
  1595. void vmw_kms_helper_buffer_finish(struct vmw_private *dev_priv,
  1596. struct drm_file *file_priv,
  1597. struct vmw_dma_buffer *buf,
  1598. struct vmw_fence_obj **out_fence,
  1599. struct drm_vmw_fence_rep __user *
  1600. user_fence_rep)
  1601. {
  1602. struct vmw_fence_obj *fence;
  1603. uint32_t handle;
  1604. int ret;
  1605. ret = vmw_execbuf_fence_commands(file_priv, dev_priv, &fence,
  1606. file_priv ? &handle : NULL);
  1607. if (buf)
  1608. vmw_fence_single_bo(&buf->base, fence);
  1609. if (file_priv)
  1610. vmw_execbuf_copy_fence_user(dev_priv, vmw_fpriv(file_priv),
  1611. ret, user_fence_rep, fence,
  1612. handle);
  1613. if (out_fence)
  1614. *out_fence = fence;
  1615. else
  1616. vmw_fence_obj_unreference(&fence);
  1617. vmw_kms_helper_buffer_revert(buf);
  1618. }
  1619. /**
  1620. * vmw_kms_helper_resource_revert - Undo the actions of
  1621. * vmw_kms_helper_resource_prepare.
  1622. *
  1623. * @res: Pointer to the resource. Typically a surface.
  1624. *
  1625. * Helper to be used if an error forces the caller to undo the actions of
  1626. * vmw_kms_helper_resource_prepare.
  1627. */
  1628. void vmw_kms_helper_resource_revert(struct vmw_resource *res)
  1629. {
  1630. vmw_kms_helper_buffer_revert(res->backup);
  1631. vmw_resource_unreserve(res, false, NULL, 0);
  1632. mutex_unlock(&res->dev_priv->cmdbuf_mutex);
  1633. }
  1634. /**
  1635. * vmw_kms_helper_resource_prepare - Reserve and validate a resource before
  1636. * command submission.
  1637. *
  1638. * @res: Pointer to the resource. Typically a surface.
  1639. * @interruptible: Whether to perform waits as interruptible.
  1640. *
  1641. * Reserves and validates also the backup buffer if a guest-backed resource.
  1642. * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
  1643. * interrupted by a signal.
  1644. */
  1645. int vmw_kms_helper_resource_prepare(struct vmw_resource *res,
  1646. bool interruptible)
  1647. {
  1648. int ret = 0;
  1649. if (interruptible)
  1650. ret = mutex_lock_interruptible(&res->dev_priv->cmdbuf_mutex);
  1651. else
  1652. mutex_lock(&res->dev_priv->cmdbuf_mutex);
  1653. if (unlikely(ret != 0))
  1654. return -ERESTARTSYS;
  1655. ret = vmw_resource_reserve(res, interruptible, false);
  1656. if (ret)
  1657. goto out_unlock;
  1658. if (res->backup) {
  1659. ret = vmw_kms_helper_buffer_prepare(res->dev_priv, res->backup,
  1660. interruptible,
  1661. res->dev_priv->has_mob);
  1662. if (ret)
  1663. goto out_unreserve;
  1664. }
  1665. ret = vmw_resource_validate(res);
  1666. if (ret)
  1667. goto out_revert;
  1668. return 0;
  1669. out_revert:
  1670. vmw_kms_helper_buffer_revert(res->backup);
  1671. out_unreserve:
  1672. vmw_resource_unreserve(res, false, NULL, 0);
  1673. out_unlock:
  1674. mutex_unlock(&res->dev_priv->cmdbuf_mutex);
  1675. return ret;
  1676. }
  1677. /**
  1678. * vmw_kms_helper_resource_finish - Unreserve and fence a resource after
  1679. * kms command submission.
  1680. *
  1681. * @res: Pointer to the resource. Typically a surface.
  1682. * @out_fence: Optional pointer to a fence pointer. If non-NULL, a
  1683. * ref-counted fence pointer is returned here.
  1684. */
  1685. void vmw_kms_helper_resource_finish(struct vmw_resource *res,
  1686. struct vmw_fence_obj **out_fence)
  1687. {
  1688. if (res->backup || out_fence)
  1689. vmw_kms_helper_buffer_finish(res->dev_priv, NULL, res->backup,
  1690. out_fence, NULL);
  1691. vmw_resource_unreserve(res, false, NULL, 0);
  1692. mutex_unlock(&res->dev_priv->cmdbuf_mutex);
  1693. }
  1694. /**
  1695. * vmw_kms_update_proxy - Helper function to update a proxy surface from
  1696. * its backing MOB.
  1697. *
  1698. * @res: Pointer to the surface resource
  1699. * @clips: Clip rects in framebuffer (surface) space.
  1700. * @num_clips: Number of clips in @clips.
  1701. * @increment: Integer with which to increment the clip counter when looping.
  1702. * Used to skip a predetermined number of clip rects.
  1703. *
  1704. * This function makes sure the proxy surface is updated from its backing MOB
  1705. * using the region given by @clips. The surface resource @res and its backing
  1706. * MOB needs to be reserved and validated on call.
  1707. */
  1708. int vmw_kms_update_proxy(struct vmw_resource *res,
  1709. const struct drm_clip_rect *clips,
  1710. unsigned num_clips,
  1711. int increment)
  1712. {
  1713. struct vmw_private *dev_priv = res->dev_priv;
  1714. struct drm_vmw_size *size = &vmw_res_to_srf(res)->base_size;
  1715. struct {
  1716. SVGA3dCmdHeader header;
  1717. SVGA3dCmdUpdateGBImage body;
  1718. } *cmd;
  1719. SVGA3dBox *box;
  1720. size_t copy_size = 0;
  1721. int i;
  1722. if (!clips)
  1723. return 0;
  1724. cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd) * num_clips);
  1725. if (!cmd) {
  1726. DRM_ERROR("Couldn't reserve fifo space for proxy surface "
  1727. "update.\n");
  1728. return -ENOMEM;
  1729. }
  1730. for (i = 0; i < num_clips; ++i, clips += increment, ++cmd) {
  1731. box = &cmd->body.box;
  1732. cmd->header.id = SVGA_3D_CMD_UPDATE_GB_IMAGE;
  1733. cmd->header.size = sizeof(cmd->body);
  1734. cmd->body.image.sid = res->id;
  1735. cmd->body.image.face = 0;
  1736. cmd->body.image.mipmap = 0;
  1737. if (clips->x1 > size->width || clips->x2 > size->width ||
  1738. clips->y1 > size->height || clips->y2 > size->height) {
  1739. DRM_ERROR("Invalid clips outsize of framebuffer.\n");
  1740. return -EINVAL;
  1741. }
  1742. box->x = clips->x1;
  1743. box->y = clips->y1;
  1744. box->z = 0;
  1745. box->w = clips->x2 - clips->x1;
  1746. box->h = clips->y2 - clips->y1;
  1747. box->d = 1;
  1748. copy_size += sizeof(*cmd);
  1749. }
  1750. vmw_fifo_commit(dev_priv, copy_size);
  1751. return 0;
  1752. }
  1753. int vmw_kms_fbdev_init_data(struct vmw_private *dev_priv,
  1754. unsigned unit,
  1755. u32 max_width,
  1756. u32 max_height,
  1757. struct drm_connector **p_con,
  1758. struct drm_crtc **p_crtc,
  1759. struct drm_display_mode **p_mode)
  1760. {
  1761. struct drm_connector *con;
  1762. struct vmw_display_unit *du;
  1763. struct drm_display_mode *mode;
  1764. int i = 0;
  1765. list_for_each_entry(con, &dev_priv->dev->mode_config.connector_list,
  1766. head) {
  1767. if (i == unit)
  1768. break;
  1769. ++i;
  1770. }
  1771. if (i != unit) {
  1772. DRM_ERROR("Could not find initial display unit.\n");
  1773. return -EINVAL;
  1774. }
  1775. if (list_empty(&con->modes))
  1776. (void) vmw_du_connector_fill_modes(con, max_width, max_height);
  1777. if (list_empty(&con->modes)) {
  1778. DRM_ERROR("Could not find initial display mode.\n");
  1779. return -EINVAL;
  1780. }
  1781. du = vmw_connector_to_du(con);
  1782. *p_con = con;
  1783. *p_crtc = &du->crtc;
  1784. list_for_each_entry(mode, &con->modes, head) {
  1785. if (mode->type & DRM_MODE_TYPE_PREFERRED)
  1786. break;
  1787. }
  1788. if (mode->type & DRM_MODE_TYPE_PREFERRED)
  1789. *p_mode = mode;
  1790. else {
  1791. WARN_ONCE(true, "Could not find initial preferred mode.\n");
  1792. *p_mode = list_first_entry(&con->modes,
  1793. struct drm_display_mode,
  1794. head);
  1795. }
  1796. return 0;
  1797. }
  1798. /**
  1799. * vmw_kms_del_active - unregister a crtc binding to the implicit framebuffer
  1800. *
  1801. * @dev_priv: Pointer to a device private struct.
  1802. * @du: The display unit of the crtc.
  1803. */
  1804. void vmw_kms_del_active(struct vmw_private *dev_priv,
  1805. struct vmw_display_unit *du)
  1806. {
  1807. mutex_lock(&dev_priv->global_kms_state_mutex);
  1808. if (du->active_implicit) {
  1809. if (--(dev_priv->num_implicit) == 0)
  1810. dev_priv->implicit_fb = NULL;
  1811. du->active_implicit = false;
  1812. }
  1813. mutex_unlock(&dev_priv->global_kms_state_mutex);
  1814. }
  1815. /**
  1816. * vmw_kms_add_active - register a crtc binding to an implicit framebuffer
  1817. *
  1818. * @vmw_priv: Pointer to a device private struct.
  1819. * @du: The display unit of the crtc.
  1820. * @vfb: The implicit framebuffer
  1821. *
  1822. * Registers a binding to an implicit framebuffer.
  1823. */
  1824. void vmw_kms_add_active(struct vmw_private *dev_priv,
  1825. struct vmw_display_unit *du,
  1826. struct vmw_framebuffer *vfb)
  1827. {
  1828. mutex_lock(&dev_priv->global_kms_state_mutex);
  1829. WARN_ON_ONCE(!dev_priv->num_implicit && dev_priv->implicit_fb);
  1830. if (!du->active_implicit && du->is_implicit) {
  1831. dev_priv->implicit_fb = vfb;
  1832. du->active_implicit = true;
  1833. dev_priv->num_implicit++;
  1834. }
  1835. mutex_unlock(&dev_priv->global_kms_state_mutex);
  1836. }
  1837. /**
  1838. * vmw_kms_screen_object_flippable - Check whether we can page-flip a crtc.
  1839. *
  1840. * @dev_priv: Pointer to device-private struct.
  1841. * @crtc: The crtc we want to flip.
  1842. *
  1843. * Returns true or false depending whether it's OK to flip this crtc
  1844. * based on the criterion that we must not have more than one implicit
  1845. * frame-buffer at any one time.
  1846. */
  1847. bool vmw_kms_crtc_flippable(struct vmw_private *dev_priv,
  1848. struct drm_crtc *crtc)
  1849. {
  1850. struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
  1851. bool ret;
  1852. mutex_lock(&dev_priv->global_kms_state_mutex);
  1853. ret = !du->is_implicit || dev_priv->num_implicit == 1;
  1854. mutex_unlock(&dev_priv->global_kms_state_mutex);
  1855. return ret;
  1856. }
  1857. /**
  1858. * vmw_kms_update_implicit_fb - Update the implicit fb.
  1859. *
  1860. * @dev_priv: Pointer to device-private struct.
  1861. * @crtc: The crtc the new implicit frame-buffer is bound to.
  1862. */
  1863. void vmw_kms_update_implicit_fb(struct vmw_private *dev_priv,
  1864. struct drm_crtc *crtc)
  1865. {
  1866. struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
  1867. struct vmw_framebuffer *vfb;
  1868. mutex_lock(&dev_priv->global_kms_state_mutex);
  1869. if (!du->is_implicit)
  1870. goto out_unlock;
  1871. vfb = vmw_framebuffer_to_vfb(crtc->primary->fb);
  1872. WARN_ON_ONCE(dev_priv->num_implicit != 1 &&
  1873. dev_priv->implicit_fb != vfb);
  1874. dev_priv->implicit_fb = vfb;
  1875. out_unlock:
  1876. mutex_unlock(&dev_priv->global_kms_state_mutex);
  1877. }
  1878. /**
  1879. * vmw_kms_create_implicit_placement_proparty - Set up the implicit placement
  1880. * property.
  1881. *
  1882. * @dev_priv: Pointer to a device private struct.
  1883. * @immutable: Whether the property is immutable.
  1884. *
  1885. * Sets up the implicit placement property unless it's already set up.
  1886. */
  1887. void
  1888. vmw_kms_create_implicit_placement_property(struct vmw_private *dev_priv,
  1889. bool immutable)
  1890. {
  1891. if (dev_priv->implicit_placement_property)
  1892. return;
  1893. dev_priv->implicit_placement_property =
  1894. drm_property_create_range(dev_priv->dev,
  1895. immutable ?
  1896. DRM_MODE_PROP_IMMUTABLE : 0,
  1897. "implicit_placement", 0, 1);
  1898. }