vmwgfx_kms.c 57 KB

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