vmwgfx_kms.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546
  1. /**************************************************************************
  2. *
  3. * Copyright © 2009 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_display_unit_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_crtc_cleanup(&du->crtc);
  37. drm_encoder_cleanup(&du->encoder);
  38. drm_connector_cleanup(&du->connector);
  39. }
  40. /*
  41. * Display Unit Cursor functions
  42. */
  43. int vmw_cursor_update_image(struct vmw_private *dev_priv,
  44. u32 *image, u32 width, u32 height,
  45. u32 hotspotX, u32 hotspotY)
  46. {
  47. struct {
  48. u32 cmd;
  49. SVGAFifoCmdDefineAlphaCursor cursor;
  50. } *cmd;
  51. u32 image_size = width * height * 4;
  52. u32 cmd_size = sizeof(*cmd) + image_size;
  53. if (!image)
  54. return -EINVAL;
  55. cmd = vmw_fifo_reserve(dev_priv, cmd_size);
  56. if (unlikely(cmd == NULL)) {
  57. DRM_ERROR("Fifo reserve failed.\n");
  58. return -ENOMEM;
  59. }
  60. memset(cmd, 0, sizeof(*cmd));
  61. memcpy(&cmd[1], image, image_size);
  62. cmd->cmd = cpu_to_le32(SVGA_CMD_DEFINE_ALPHA_CURSOR);
  63. cmd->cursor.id = cpu_to_le32(0);
  64. cmd->cursor.width = cpu_to_le32(width);
  65. cmd->cursor.height = cpu_to_le32(height);
  66. cmd->cursor.hotspotX = cpu_to_le32(hotspotX);
  67. cmd->cursor.hotspotY = cpu_to_le32(hotspotY);
  68. vmw_fifo_commit(dev_priv, cmd_size);
  69. return 0;
  70. }
  71. void vmw_cursor_update_position(struct vmw_private *dev_priv,
  72. bool show, int x, int y)
  73. {
  74. __le32 __iomem *fifo_mem = dev_priv->mmio_virt;
  75. uint32_t count;
  76. iowrite32(show ? 1 : 0, fifo_mem + SVGA_FIFO_CURSOR_ON);
  77. iowrite32(x, fifo_mem + SVGA_FIFO_CURSOR_X);
  78. iowrite32(y, fifo_mem + SVGA_FIFO_CURSOR_Y);
  79. count = ioread32(fifo_mem + SVGA_FIFO_CURSOR_COUNT);
  80. iowrite32(++count, fifo_mem + SVGA_FIFO_CURSOR_COUNT);
  81. }
  82. int vmw_du_crtc_cursor_set(struct drm_crtc *crtc, struct drm_file *file_priv,
  83. uint32_t handle, uint32_t width, uint32_t height)
  84. {
  85. struct vmw_private *dev_priv = vmw_priv(crtc->dev);
  86. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  87. struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
  88. struct vmw_surface *surface = NULL;
  89. struct vmw_dma_buffer *dmabuf = NULL;
  90. int ret;
  91. if (handle) {
  92. ret = vmw_user_surface_lookup_handle(dev_priv, tfile,
  93. handle, &surface);
  94. if (!ret) {
  95. if (!surface->snooper.image) {
  96. DRM_ERROR("surface not suitable for cursor\n");
  97. return -EINVAL;
  98. }
  99. } else {
  100. ret = vmw_user_dmabuf_lookup(tfile,
  101. handle, &dmabuf);
  102. if (ret) {
  103. DRM_ERROR("failed to find surface or dmabuf: %i\n", ret);
  104. return -EINVAL;
  105. }
  106. }
  107. }
  108. /* takedown old cursor */
  109. if (du->cursor_surface) {
  110. du->cursor_surface->snooper.crtc = NULL;
  111. vmw_surface_unreference(&du->cursor_surface);
  112. }
  113. if (du->cursor_dmabuf)
  114. vmw_dmabuf_unreference(&du->cursor_dmabuf);
  115. /* setup new image */
  116. if (surface) {
  117. /* vmw_user_surface_lookup takes one reference */
  118. du->cursor_surface = surface;
  119. du->cursor_surface->snooper.crtc = crtc;
  120. du->cursor_age = du->cursor_surface->snooper.age;
  121. vmw_cursor_update_image(dev_priv, surface->snooper.image,
  122. 64, 64, du->hotspot_x, du->hotspot_y);
  123. } else if (dmabuf) {
  124. struct ttm_bo_kmap_obj map;
  125. unsigned long kmap_offset;
  126. unsigned long kmap_num;
  127. void *virtual;
  128. bool dummy;
  129. /* vmw_user_surface_lookup takes one reference */
  130. du->cursor_dmabuf = dmabuf;
  131. kmap_offset = 0;
  132. kmap_num = (64*64*4) >> PAGE_SHIFT;
  133. ret = ttm_bo_reserve(&dmabuf->base, true, false, false, 0);
  134. if (unlikely(ret != 0)) {
  135. DRM_ERROR("reserve failed\n");
  136. return -EINVAL;
  137. }
  138. ret = ttm_bo_kmap(&dmabuf->base, kmap_offset, kmap_num, &map);
  139. if (unlikely(ret != 0))
  140. goto err_unreserve;
  141. virtual = ttm_kmap_obj_virtual(&map, &dummy);
  142. vmw_cursor_update_image(dev_priv, virtual, 64, 64,
  143. du->hotspot_x, du->hotspot_y);
  144. ttm_bo_kunmap(&map);
  145. err_unreserve:
  146. ttm_bo_unreserve(&dmabuf->base);
  147. } else {
  148. vmw_cursor_update_position(dev_priv, false, 0, 0);
  149. return 0;
  150. }
  151. vmw_cursor_update_position(dev_priv, true, du->cursor_x, du->cursor_y);
  152. return 0;
  153. }
  154. int vmw_du_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
  155. {
  156. struct vmw_private *dev_priv = vmw_priv(crtc->dev);
  157. struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
  158. bool shown = du->cursor_surface || du->cursor_dmabuf ? true : false;
  159. du->cursor_x = x + crtc->x;
  160. du->cursor_y = y + crtc->y;
  161. vmw_cursor_update_position(dev_priv, shown,
  162. du->cursor_x, du->cursor_y);
  163. return 0;
  164. }
  165. void vmw_kms_cursor_snoop(struct vmw_surface *srf,
  166. struct ttm_object_file *tfile,
  167. struct ttm_buffer_object *bo,
  168. SVGA3dCmdHeader *header)
  169. {
  170. struct ttm_bo_kmap_obj map;
  171. unsigned long kmap_offset;
  172. unsigned long kmap_num;
  173. SVGA3dCopyBox *box;
  174. unsigned box_count;
  175. void *virtual;
  176. bool dummy;
  177. struct vmw_dma_cmd {
  178. SVGA3dCmdHeader header;
  179. SVGA3dCmdSurfaceDMA dma;
  180. } *cmd;
  181. int ret;
  182. cmd = container_of(header, struct vmw_dma_cmd, header);
  183. /* No snooper installed */
  184. if (!srf->snooper.image)
  185. return;
  186. if (cmd->dma.host.face != 0 || cmd->dma.host.mipmap != 0) {
  187. DRM_ERROR("face and mipmap for cursors should never != 0\n");
  188. return;
  189. }
  190. if (cmd->header.size < 64) {
  191. DRM_ERROR("at least one full copy box must be given\n");
  192. return;
  193. }
  194. box = (SVGA3dCopyBox *)&cmd[1];
  195. box_count = (cmd->header.size - sizeof(SVGA3dCmdSurfaceDMA)) /
  196. sizeof(SVGA3dCopyBox);
  197. if (cmd->dma.guest.pitch != (64 * 4) ||
  198. cmd->dma.guest.ptr.offset % PAGE_SIZE ||
  199. box->x != 0 || box->y != 0 || box->z != 0 ||
  200. box->srcx != 0 || box->srcy != 0 || box->srcz != 0 ||
  201. box->w != 64 || box->h != 64 || box->d != 1 ||
  202. box_count != 1) {
  203. /* TODO handle none page aligned offsets */
  204. /* TODO handle partial uploads and pitch != 256 */
  205. /* TODO handle more then one copy (size != 64) */
  206. DRM_ERROR("lazy programmer, can't handle weird stuff\n");
  207. return;
  208. }
  209. kmap_offset = cmd->dma.guest.ptr.offset >> PAGE_SHIFT;
  210. kmap_num = (64*64*4) >> PAGE_SHIFT;
  211. ret = ttm_bo_reserve(bo, true, false, false, 0);
  212. if (unlikely(ret != 0)) {
  213. DRM_ERROR("reserve failed\n");
  214. return;
  215. }
  216. ret = ttm_bo_kmap(bo, kmap_offset, kmap_num, &map);
  217. if (unlikely(ret != 0))
  218. goto err_unreserve;
  219. virtual = ttm_kmap_obj_virtual(&map, &dummy);
  220. memcpy(srf->snooper.image, virtual, 64*64*4);
  221. srf->snooper.age++;
  222. /* we can't call this function from this function since execbuf has
  223. * reserved fifo space.
  224. *
  225. * if (srf->snooper.crtc)
  226. * vmw_ldu_crtc_cursor_update_image(dev_priv,
  227. * srf->snooper.image, 64, 64,
  228. * du->hotspot_x, du->hotspot_y);
  229. */
  230. ttm_bo_kunmap(&map);
  231. err_unreserve:
  232. ttm_bo_unreserve(bo);
  233. }
  234. void vmw_kms_cursor_post_execbuf(struct vmw_private *dev_priv)
  235. {
  236. struct drm_device *dev = dev_priv->dev;
  237. struct vmw_display_unit *du;
  238. struct drm_crtc *crtc;
  239. mutex_lock(&dev->mode_config.mutex);
  240. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  241. du = vmw_crtc_to_du(crtc);
  242. if (!du->cursor_surface ||
  243. du->cursor_age == du->cursor_surface->snooper.age)
  244. continue;
  245. du->cursor_age = du->cursor_surface->snooper.age;
  246. vmw_cursor_update_image(dev_priv,
  247. du->cursor_surface->snooper.image,
  248. 64, 64, du->hotspot_x, du->hotspot_y);
  249. }
  250. mutex_unlock(&dev->mode_config.mutex);
  251. }
  252. /*
  253. * Generic framebuffer code
  254. */
  255. int vmw_framebuffer_create_handle(struct drm_framebuffer *fb,
  256. struct drm_file *file_priv,
  257. unsigned int *handle)
  258. {
  259. if (handle)
  260. handle = 0;
  261. return 0;
  262. }
  263. /*
  264. * Surface framebuffer code
  265. */
  266. #define vmw_framebuffer_to_vfbs(x) \
  267. container_of(x, struct vmw_framebuffer_surface, base.base)
  268. struct vmw_framebuffer_surface {
  269. struct vmw_framebuffer base;
  270. struct vmw_surface *surface;
  271. struct vmw_dma_buffer *buffer;
  272. struct list_head head;
  273. struct drm_master *master;
  274. };
  275. void vmw_framebuffer_surface_destroy(struct drm_framebuffer *framebuffer)
  276. {
  277. struct vmw_framebuffer_surface *vfbs =
  278. vmw_framebuffer_to_vfbs(framebuffer);
  279. struct vmw_master *vmaster = vmw_master(vfbs->master);
  280. mutex_lock(&vmaster->fb_surf_mutex);
  281. list_del(&vfbs->head);
  282. mutex_unlock(&vmaster->fb_surf_mutex);
  283. drm_master_put(&vfbs->master);
  284. drm_framebuffer_cleanup(framebuffer);
  285. vmw_surface_unreference(&vfbs->surface);
  286. ttm_base_object_unref(&vfbs->base.user_obj);
  287. kfree(vfbs);
  288. }
  289. static int do_surface_dirty_sou(struct vmw_private *dev_priv,
  290. struct drm_file *file_priv,
  291. struct vmw_framebuffer *framebuffer,
  292. struct vmw_surface *surf,
  293. unsigned flags, unsigned color,
  294. struct drm_clip_rect *clips,
  295. unsigned num_clips, int inc)
  296. {
  297. int left = clips->x2, right = clips->x1;
  298. int top = clips->y2, bottom = clips->y1;
  299. size_t fifo_size;
  300. int i, ret;
  301. struct {
  302. SVGA3dCmdHeader header;
  303. SVGA3dCmdBlitSurfaceToScreen body;
  304. } *cmd;
  305. fifo_size = sizeof(*cmd);
  306. cmd = kzalloc(fifo_size, GFP_KERNEL);
  307. if (unlikely(cmd == NULL)) {
  308. DRM_ERROR("Temporary fifo memory alloc failed.\n");
  309. return -ENOMEM;
  310. }
  311. cmd->header.id = cpu_to_le32(SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN);
  312. cmd->header.size = cpu_to_le32(sizeof(cmd->body));
  313. cmd->body.srcImage.sid = cpu_to_le32(framebuffer->user_handle);
  314. cmd->body.destScreenId = SVGA_ID_INVALID; /* virtual coords */
  315. for (i = 0; i < num_clips; i++, clips += inc) {
  316. left = min_t(int, left, (int)clips->x1);
  317. right = max_t(int, right, (int)clips->x2);
  318. top = min_t(int, top, (int)clips->y1);
  319. bottom = max_t(int, bottom, (int)clips->y2);
  320. }
  321. cmd->body.srcRect.left = left;
  322. cmd->body.srcRect.right = right;
  323. cmd->body.srcRect.top = top;
  324. cmd->body.srcRect.bottom = bottom;
  325. cmd->body.destRect.left = left;
  326. cmd->body.destRect.right = right;
  327. cmd->body.destRect.top = top;
  328. cmd->body.destRect.bottom = bottom;
  329. ret = vmw_execbuf_process(file_priv, dev_priv, NULL, cmd, fifo_size,
  330. 0, NULL);
  331. kfree(cmd);
  332. return ret;
  333. }
  334. int vmw_framebuffer_surface_dirty(struct drm_framebuffer *framebuffer,
  335. struct drm_file *file_priv,
  336. unsigned flags, unsigned color,
  337. struct drm_clip_rect *clips,
  338. unsigned num_clips)
  339. {
  340. struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
  341. struct vmw_master *vmaster = vmw_master(file_priv->master);
  342. struct vmw_framebuffer_surface *vfbs =
  343. vmw_framebuffer_to_vfbs(framebuffer);
  344. struct vmw_surface *surf = vfbs->surface;
  345. struct drm_clip_rect norect;
  346. int ret, inc = 1;
  347. if (unlikely(vfbs->master != file_priv->master))
  348. return -EINVAL;
  349. /* Require ScreenObject support for 3D */
  350. if (!dev_priv->sou_priv)
  351. return -EINVAL;
  352. ret = ttm_read_lock(&vmaster->lock, true);
  353. if (unlikely(ret != 0))
  354. return ret;
  355. if (!num_clips) {
  356. num_clips = 1;
  357. clips = &norect;
  358. norect.x1 = norect.y1 = 0;
  359. norect.x2 = framebuffer->width;
  360. norect.y2 = framebuffer->height;
  361. } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
  362. num_clips /= 2;
  363. inc = 2; /* skip source rects */
  364. }
  365. ret = do_surface_dirty_sou(dev_priv, file_priv, &vfbs->base, surf,
  366. flags, color,
  367. clips, num_clips, inc);
  368. ttm_read_unlock(&vmaster->lock);
  369. return 0;
  370. }
  371. static struct drm_framebuffer_funcs vmw_framebuffer_surface_funcs = {
  372. .destroy = vmw_framebuffer_surface_destroy,
  373. .dirty = vmw_framebuffer_surface_dirty,
  374. .create_handle = vmw_framebuffer_create_handle,
  375. };
  376. static int vmw_kms_new_framebuffer_surface(struct vmw_private *dev_priv,
  377. struct drm_file *file_priv,
  378. struct vmw_surface *surface,
  379. struct vmw_framebuffer **out,
  380. const struct drm_mode_fb_cmd
  381. *mode_cmd)
  382. {
  383. struct drm_device *dev = dev_priv->dev;
  384. struct vmw_framebuffer_surface *vfbs;
  385. enum SVGA3dSurfaceFormat format;
  386. struct vmw_master *vmaster = vmw_master(file_priv->master);
  387. int ret;
  388. /* 3D is only supported on HWv8 hosts which supports screen objects */
  389. if (!dev_priv->sou_priv)
  390. return -ENOSYS;
  391. /*
  392. * Sanity checks.
  393. */
  394. if (unlikely(surface->mip_levels[0] != 1 ||
  395. surface->num_sizes != 1 ||
  396. surface->sizes[0].width < mode_cmd->width ||
  397. surface->sizes[0].height < mode_cmd->height ||
  398. surface->sizes[0].depth != 1)) {
  399. DRM_ERROR("Incompatible surface dimensions "
  400. "for requested mode.\n");
  401. return -EINVAL;
  402. }
  403. switch (mode_cmd->depth) {
  404. case 32:
  405. format = SVGA3D_A8R8G8B8;
  406. break;
  407. case 24:
  408. format = SVGA3D_X8R8G8B8;
  409. break;
  410. case 16:
  411. format = SVGA3D_R5G6B5;
  412. break;
  413. case 15:
  414. format = SVGA3D_A1R5G5B5;
  415. break;
  416. case 8:
  417. format = SVGA3D_LUMINANCE8;
  418. break;
  419. default:
  420. DRM_ERROR("Invalid color depth: %d\n", mode_cmd->depth);
  421. return -EINVAL;
  422. }
  423. if (unlikely(format != surface->format)) {
  424. DRM_ERROR("Invalid surface format for requested mode.\n");
  425. return -EINVAL;
  426. }
  427. vfbs = kzalloc(sizeof(*vfbs), GFP_KERNEL);
  428. if (!vfbs) {
  429. ret = -ENOMEM;
  430. goto out_err1;
  431. }
  432. ret = drm_framebuffer_init(dev, &vfbs->base.base,
  433. &vmw_framebuffer_surface_funcs);
  434. if (ret)
  435. goto out_err2;
  436. if (!vmw_surface_reference(surface)) {
  437. DRM_ERROR("failed to reference surface %p\n", surface);
  438. goto out_err3;
  439. }
  440. /* XXX get the first 3 from the surface info */
  441. vfbs->base.base.bits_per_pixel = mode_cmd->bpp;
  442. vfbs->base.base.pitch = mode_cmd->pitch;
  443. vfbs->base.base.depth = mode_cmd->depth;
  444. vfbs->base.base.width = mode_cmd->width;
  445. vfbs->base.base.height = mode_cmd->height;
  446. vfbs->surface = surface;
  447. vfbs->base.user_handle = mode_cmd->handle;
  448. vfbs->master = drm_master_get(file_priv->master);
  449. mutex_lock(&vmaster->fb_surf_mutex);
  450. list_add_tail(&vfbs->head, &vmaster->fb_surf);
  451. mutex_unlock(&vmaster->fb_surf_mutex);
  452. *out = &vfbs->base;
  453. return 0;
  454. out_err3:
  455. drm_framebuffer_cleanup(&vfbs->base.base);
  456. out_err2:
  457. kfree(vfbs);
  458. out_err1:
  459. return ret;
  460. }
  461. /*
  462. * Dmabuf framebuffer code
  463. */
  464. #define vmw_framebuffer_to_vfbd(x) \
  465. container_of(x, struct vmw_framebuffer_dmabuf, base.base)
  466. struct vmw_framebuffer_dmabuf {
  467. struct vmw_framebuffer base;
  468. struct vmw_dma_buffer *buffer;
  469. };
  470. void vmw_framebuffer_dmabuf_destroy(struct drm_framebuffer *framebuffer)
  471. {
  472. struct vmw_framebuffer_dmabuf *vfbd =
  473. vmw_framebuffer_to_vfbd(framebuffer);
  474. drm_framebuffer_cleanup(framebuffer);
  475. vmw_dmabuf_unreference(&vfbd->buffer);
  476. ttm_base_object_unref(&vfbd->base.user_obj);
  477. kfree(vfbd);
  478. }
  479. static int do_dmabuf_dirty_ldu(struct vmw_private *dev_priv,
  480. struct vmw_framebuffer *framebuffer,
  481. struct vmw_dma_buffer *buffer,
  482. unsigned flags, unsigned color,
  483. struct drm_clip_rect *clips,
  484. unsigned num_clips, int increment)
  485. {
  486. size_t fifo_size;
  487. int i;
  488. struct {
  489. uint32_t header;
  490. SVGAFifoCmdUpdate body;
  491. } *cmd;
  492. fifo_size = sizeof(*cmd) * num_clips;
  493. cmd = vmw_fifo_reserve(dev_priv, fifo_size);
  494. if (unlikely(cmd == NULL)) {
  495. DRM_ERROR("Fifo reserve failed.\n");
  496. return -ENOMEM;
  497. }
  498. memset(cmd, 0, fifo_size);
  499. for (i = 0; i < num_clips; i++, clips += increment) {
  500. cmd[i].header = cpu_to_le32(SVGA_CMD_UPDATE);
  501. cmd[i].body.x = cpu_to_le32(clips->x1);
  502. cmd[i].body.y = cpu_to_le32(clips->y1);
  503. cmd[i].body.width = cpu_to_le32(clips->x2 - clips->x1);
  504. cmd[i].body.height = cpu_to_le32(clips->y2 - clips->y1);
  505. }
  506. vmw_fifo_commit(dev_priv, fifo_size);
  507. return 0;
  508. }
  509. static int do_dmabuf_dirty_sou(struct drm_file *file_priv,
  510. struct vmw_private *dev_priv,
  511. struct vmw_framebuffer *framebuffer,
  512. struct vmw_dma_buffer *buffer,
  513. unsigned flags, unsigned color,
  514. struct drm_clip_rect *clips,
  515. unsigned num_clips, int increment)
  516. {
  517. size_t fifo_size;
  518. int i, ret;
  519. struct {
  520. uint32_t header;
  521. SVGAFifoCmdDefineGMRFB body;
  522. } *cmd;
  523. struct {
  524. uint32_t header;
  525. SVGAFifoCmdBlitGMRFBToScreen body;
  526. } *blits;
  527. fifo_size = sizeof(*cmd) + sizeof(*blits) * num_clips;
  528. cmd = kmalloc(fifo_size, GFP_KERNEL);
  529. if (unlikely(cmd == NULL)) {
  530. DRM_ERROR("Failed to allocate temporary cmd buffer.\n");
  531. return -ENOMEM;
  532. }
  533. memset(cmd, 0, fifo_size);
  534. cmd->header = SVGA_CMD_DEFINE_GMRFB;
  535. cmd->body.format.bitsPerPixel = framebuffer->base.bits_per_pixel;
  536. cmd->body.format.colorDepth = framebuffer->base.depth;
  537. cmd->body.format.reserved = 0;
  538. cmd->body.bytesPerLine = framebuffer->base.pitch;
  539. cmd->body.ptr.gmrId = framebuffer->user_handle;
  540. cmd->body.ptr.offset = 0;
  541. blits = (void *)&cmd[1];
  542. for (i = 0; i < num_clips; i++, clips += increment) {
  543. blits[i].header = SVGA_CMD_BLIT_GMRFB_TO_SCREEN;
  544. blits[i].body.srcOrigin.x = clips->x1;
  545. blits[i].body.srcOrigin.y = clips->y1;
  546. blits[i].body.destRect.left = clips->x1;
  547. blits[i].body.destRect.top = clips->y1;
  548. blits[i].body.destRect.right = clips->x2;
  549. blits[i].body.destRect.bottom = clips->y2;
  550. }
  551. ret = vmw_execbuf_process(file_priv, dev_priv, NULL, cmd,
  552. fifo_size, 0, NULL);
  553. kfree(cmd);
  554. return ret;
  555. }
  556. int vmw_framebuffer_dmabuf_dirty(struct drm_framebuffer *framebuffer,
  557. struct drm_file *file_priv,
  558. unsigned flags, unsigned color,
  559. struct drm_clip_rect *clips,
  560. unsigned num_clips)
  561. {
  562. struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
  563. struct vmw_master *vmaster = vmw_master(file_priv->master);
  564. struct vmw_framebuffer_dmabuf *vfbd =
  565. vmw_framebuffer_to_vfbd(framebuffer);
  566. struct vmw_dma_buffer *dmabuf = vfbd->buffer;
  567. struct drm_clip_rect norect;
  568. int ret, increment = 1;
  569. ret = ttm_read_lock(&vmaster->lock, true);
  570. if (unlikely(ret != 0))
  571. return ret;
  572. if (!num_clips) {
  573. num_clips = 1;
  574. clips = &norect;
  575. norect.x1 = norect.y1 = 0;
  576. norect.x2 = framebuffer->width;
  577. norect.y2 = framebuffer->height;
  578. } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
  579. num_clips /= 2;
  580. increment = 2;
  581. }
  582. if (dev_priv->ldu_priv) {
  583. ret = do_dmabuf_dirty_ldu(dev_priv, &vfbd->base, dmabuf,
  584. flags, color,
  585. clips, num_clips, increment);
  586. } else {
  587. ret = do_dmabuf_dirty_sou(file_priv, dev_priv, &vfbd->base,
  588. dmabuf, flags, color,
  589. clips, num_clips, increment);
  590. }
  591. ttm_read_unlock(&vmaster->lock);
  592. return ret;
  593. }
  594. static struct drm_framebuffer_funcs vmw_framebuffer_dmabuf_funcs = {
  595. .destroy = vmw_framebuffer_dmabuf_destroy,
  596. .dirty = vmw_framebuffer_dmabuf_dirty,
  597. .create_handle = vmw_framebuffer_create_handle,
  598. };
  599. /**
  600. * Pin the dmabuffer to the start of vram.
  601. */
  602. static int vmw_framebuffer_dmabuf_pin(struct vmw_framebuffer *vfb)
  603. {
  604. struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
  605. struct vmw_framebuffer_dmabuf *vfbd =
  606. vmw_framebuffer_to_vfbd(&vfb->base);
  607. int ret;
  608. /* This code should not be used with screen objects */
  609. BUG_ON(dev_priv->sou_priv);
  610. vmw_overlay_pause_all(dev_priv);
  611. ret = vmw_dmabuf_to_start_of_vram(dev_priv, vfbd->buffer, true, false);
  612. vmw_overlay_resume_all(dev_priv);
  613. WARN_ON(ret != 0);
  614. return 0;
  615. }
  616. static int vmw_framebuffer_dmabuf_unpin(struct vmw_framebuffer *vfb)
  617. {
  618. struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
  619. struct vmw_framebuffer_dmabuf *vfbd =
  620. vmw_framebuffer_to_vfbd(&vfb->base);
  621. if (!vfbd->buffer) {
  622. WARN_ON(!vfbd->buffer);
  623. return 0;
  624. }
  625. return vmw_dmabuf_unpin(dev_priv, vfbd->buffer, false);
  626. }
  627. static int vmw_kms_new_framebuffer_dmabuf(struct vmw_private *dev_priv,
  628. struct vmw_dma_buffer *dmabuf,
  629. struct vmw_framebuffer **out,
  630. const struct drm_mode_fb_cmd
  631. *mode_cmd)
  632. {
  633. struct drm_device *dev = dev_priv->dev;
  634. struct vmw_framebuffer_dmabuf *vfbd;
  635. unsigned int requested_size;
  636. int ret;
  637. requested_size = mode_cmd->height * mode_cmd->pitch;
  638. if (unlikely(requested_size > dmabuf->base.num_pages * PAGE_SIZE)) {
  639. DRM_ERROR("Screen buffer object size is too small "
  640. "for requested mode.\n");
  641. return -EINVAL;
  642. }
  643. /* Limited framebuffer color depth support for screen objects */
  644. if (dev_priv->sou_priv) {
  645. switch (mode_cmd->depth) {
  646. case 32:
  647. case 24:
  648. /* Only support 32 bpp for 32 and 24 depth fbs */
  649. if (mode_cmd->bpp == 32)
  650. break;
  651. DRM_ERROR("Invalid color depth/bbp: %d %d\n",
  652. mode_cmd->depth, mode_cmd->bpp);
  653. return -EINVAL;
  654. case 16:
  655. case 15:
  656. /* Only support 16 bpp for 16 and 15 depth fbs */
  657. if (mode_cmd->bpp == 16)
  658. break;
  659. DRM_ERROR("Invalid color depth/bbp: %d %d\n",
  660. mode_cmd->depth, mode_cmd->bpp);
  661. return -EINVAL;
  662. default:
  663. DRM_ERROR("Invalid color depth: %d\n", mode_cmd->depth);
  664. return -EINVAL;
  665. }
  666. }
  667. vfbd = kzalloc(sizeof(*vfbd), GFP_KERNEL);
  668. if (!vfbd) {
  669. ret = -ENOMEM;
  670. goto out_err1;
  671. }
  672. ret = drm_framebuffer_init(dev, &vfbd->base.base,
  673. &vmw_framebuffer_dmabuf_funcs);
  674. if (ret)
  675. goto out_err2;
  676. if (!vmw_dmabuf_reference(dmabuf)) {
  677. DRM_ERROR("failed to reference dmabuf %p\n", dmabuf);
  678. goto out_err3;
  679. }
  680. vfbd->base.base.bits_per_pixel = mode_cmd->bpp;
  681. vfbd->base.base.pitch = mode_cmd->pitch;
  682. vfbd->base.base.depth = mode_cmd->depth;
  683. vfbd->base.base.width = mode_cmd->width;
  684. vfbd->base.base.height = mode_cmd->height;
  685. if (!dev_priv->sou_priv) {
  686. vfbd->base.pin = vmw_framebuffer_dmabuf_pin;
  687. vfbd->base.unpin = vmw_framebuffer_dmabuf_unpin;
  688. }
  689. vfbd->base.dmabuf = true;
  690. vfbd->buffer = dmabuf;
  691. vfbd->base.user_handle = mode_cmd->handle;
  692. *out = &vfbd->base;
  693. return 0;
  694. out_err3:
  695. drm_framebuffer_cleanup(&vfbd->base.base);
  696. out_err2:
  697. kfree(vfbd);
  698. out_err1:
  699. return ret;
  700. }
  701. /*
  702. * Generic Kernel modesetting functions
  703. */
  704. static struct drm_framebuffer *vmw_kms_fb_create(struct drm_device *dev,
  705. struct drm_file *file_priv,
  706. struct drm_mode_fb_cmd *mode_cmd)
  707. {
  708. struct vmw_private *dev_priv = vmw_priv(dev);
  709. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  710. struct vmw_framebuffer *vfb = NULL;
  711. struct vmw_surface *surface = NULL;
  712. struct vmw_dma_buffer *bo = NULL;
  713. struct ttm_base_object *user_obj;
  714. u64 required_size;
  715. int ret;
  716. /**
  717. * This code should be conditioned on Screen Objects not being used.
  718. * If screen objects are used, we can allocate a GMR to hold the
  719. * requested framebuffer.
  720. */
  721. required_size = mode_cmd->pitch * mode_cmd->height;
  722. if (unlikely(required_size > (u64) dev_priv->vram_size)) {
  723. DRM_ERROR("VRAM size is too small for requested mode.\n");
  724. return NULL;
  725. }
  726. /*
  727. * Take a reference on the user object of the resource
  728. * backing the kms fb. This ensures that user-space handle
  729. * lookups on that resource will always work as long as
  730. * it's registered with a kms framebuffer. This is important,
  731. * since vmw_execbuf_process identifies resources in the
  732. * command stream using user-space handles.
  733. */
  734. user_obj = ttm_base_object_lookup(tfile, mode_cmd->handle);
  735. if (unlikely(user_obj == NULL)) {
  736. DRM_ERROR("Could not locate requested kms frame buffer.\n");
  737. return ERR_PTR(-ENOENT);
  738. }
  739. /**
  740. * End conditioned code.
  741. */
  742. ret = vmw_user_surface_lookup_handle(dev_priv, tfile,
  743. mode_cmd->handle, &surface);
  744. if (ret)
  745. goto try_dmabuf;
  746. if (!surface->scanout)
  747. goto err_not_scanout;
  748. ret = vmw_kms_new_framebuffer_surface(dev_priv, file_priv, surface,
  749. &vfb, mode_cmd);
  750. /* vmw_user_surface_lookup takes one ref so does new_fb */
  751. vmw_surface_unreference(&surface);
  752. if (ret) {
  753. DRM_ERROR("failed to create vmw_framebuffer: %i\n", ret);
  754. ttm_base_object_unref(&user_obj);
  755. return ERR_PTR(ret);
  756. } else
  757. vfb->user_obj = user_obj;
  758. return &vfb->base;
  759. try_dmabuf:
  760. DRM_INFO("%s: trying buffer\n", __func__);
  761. ret = vmw_user_dmabuf_lookup(tfile, mode_cmd->handle, &bo);
  762. if (ret) {
  763. DRM_ERROR("failed to find buffer: %i\n", ret);
  764. return ERR_PTR(-ENOENT);
  765. }
  766. ret = vmw_kms_new_framebuffer_dmabuf(dev_priv, bo, &vfb,
  767. mode_cmd);
  768. /* vmw_user_dmabuf_lookup takes one ref so does new_fb */
  769. vmw_dmabuf_unreference(&bo);
  770. if (ret) {
  771. DRM_ERROR("failed to create vmw_framebuffer: %i\n", ret);
  772. ttm_base_object_unref(&user_obj);
  773. return ERR_PTR(ret);
  774. } else
  775. vfb->user_obj = user_obj;
  776. return &vfb->base;
  777. err_not_scanout:
  778. DRM_ERROR("surface not marked as scanout\n");
  779. /* vmw_user_surface_lookup takes one ref */
  780. vmw_surface_unreference(&surface);
  781. ttm_base_object_unref(&user_obj);
  782. return ERR_PTR(-EINVAL);
  783. }
  784. static struct drm_mode_config_funcs vmw_kms_funcs = {
  785. .fb_create = vmw_kms_fb_create,
  786. };
  787. int vmw_kms_present(struct vmw_private *dev_priv,
  788. struct drm_file *file_priv,
  789. struct vmw_framebuffer *vfb,
  790. struct vmw_surface *surface,
  791. uint32_t sid,
  792. int32_t destX, int32_t destY,
  793. struct drm_vmw_rect *clips,
  794. uint32_t num_clips)
  795. {
  796. size_t fifo_size;
  797. int i, ret;
  798. struct {
  799. SVGA3dCmdHeader header;
  800. SVGA3dCmdBlitSurfaceToScreen body;
  801. } *cmd;
  802. SVGASignedRect *blits;
  803. BUG_ON(surface == NULL);
  804. BUG_ON(!clips || !num_clips);
  805. fifo_size = sizeof(*cmd) + sizeof(SVGASignedRect) * num_clips;
  806. cmd = kmalloc(fifo_size, GFP_KERNEL);
  807. if (unlikely(cmd == NULL)) {
  808. DRM_ERROR("Failed to allocate temporary fifo memory.\n");
  809. return -ENOMEM;
  810. }
  811. memset(cmd, 0, fifo_size);
  812. cmd->header.id = cpu_to_le32(SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN);
  813. cmd->header.size = cpu_to_le32(fifo_size - sizeof(cmd->header));
  814. cmd->body.srcImage.sid = sid;
  815. cmd->body.destScreenId = SVGA_ID_INVALID; /* virtual coords */
  816. cmd->body.srcRect.left = 0;
  817. cmd->body.srcRect.right = surface->sizes[0].width;
  818. cmd->body.srcRect.top = 0;
  819. cmd->body.srcRect.bottom = surface->sizes[0].height;
  820. cmd->body.destRect.left = destX;
  821. cmd->body.destRect.right = destX + surface->sizes[0].width;
  822. cmd->body.destRect.top = destY;
  823. cmd->body.destRect.bottom = destY + surface->sizes[0].height;
  824. blits = (SVGASignedRect *)&cmd[1];
  825. for (i = 0; i < num_clips; i++) {
  826. blits[i].left = clips[i].x;
  827. blits[i].right = clips[i].x + clips[i].w;
  828. blits[i].top = clips[i].y;
  829. blits[i].bottom = clips[i].y + clips[i].h;
  830. }
  831. ret = vmw_execbuf_process(file_priv, dev_priv, NULL, cmd,
  832. fifo_size, 0, NULL);
  833. kfree(cmd);
  834. return ret;
  835. }
  836. int vmw_kms_readback(struct vmw_private *dev_priv,
  837. struct drm_file *file_priv,
  838. struct vmw_framebuffer *vfb,
  839. struct drm_vmw_fence_rep __user *user_fence_rep,
  840. struct drm_vmw_rect *clips,
  841. uint32_t num_clips)
  842. {
  843. struct vmw_framebuffer_dmabuf *vfbd =
  844. vmw_framebuffer_to_vfbd(&vfb->base);
  845. struct vmw_dma_buffer *dmabuf = vfbd->buffer;
  846. struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
  847. struct drm_crtc *crtc;
  848. size_t fifo_size;
  849. int i, k, ret, num_units, blits_pos;
  850. struct {
  851. uint32_t header;
  852. SVGAFifoCmdDefineGMRFB body;
  853. } *cmd;
  854. struct {
  855. uint32_t header;
  856. SVGAFifoCmdBlitScreenToGMRFB body;
  857. } *blits;
  858. num_units = 0;
  859. list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list, head) {
  860. if (crtc->fb != &vfb->base)
  861. continue;
  862. units[num_units++] = vmw_crtc_to_du(crtc);
  863. }
  864. BUG_ON(dmabuf == NULL);
  865. BUG_ON(!clips || !num_clips);
  866. /* take a safe guess at fifo size */
  867. fifo_size = sizeof(*cmd) + sizeof(*blits) * num_clips * num_units;
  868. cmd = kmalloc(fifo_size, GFP_KERNEL);
  869. if (unlikely(cmd == NULL)) {
  870. DRM_ERROR("Failed to allocate temporary fifo memory.\n");
  871. return -ENOMEM;
  872. }
  873. memset(cmd, 0, fifo_size);
  874. cmd->header = SVGA_CMD_DEFINE_GMRFB;
  875. cmd->body.format.bitsPerPixel = vfb->base.bits_per_pixel;
  876. cmd->body.format.colorDepth = vfb->base.depth;
  877. cmd->body.format.reserved = 0;
  878. cmd->body.bytesPerLine = vfb->base.pitch;
  879. cmd->body.ptr.gmrId = vfb->user_handle;
  880. cmd->body.ptr.offset = 0;
  881. blits = (void *)&cmd[1];
  882. blits_pos = 0;
  883. for (i = 0; i < num_units; i++) {
  884. struct drm_vmw_rect *c = clips;
  885. for (k = 0; k < num_clips; k++, c++) {
  886. /* transform clip coords to crtc origin based coords */
  887. int clip_x1 = c->x - units[i]->crtc.x;
  888. int clip_x2 = c->x - units[i]->crtc.x + c->w;
  889. int clip_y1 = c->y - units[i]->crtc.y;
  890. int clip_y2 = c->y - units[i]->crtc.y + c->h;
  891. int dest_x = c->x;
  892. int dest_y = c->y;
  893. /* compensate for clipping, we negate
  894. * a negative number and add that.
  895. */
  896. if (clip_x1 < 0)
  897. dest_x += -clip_x1;
  898. if (clip_y1 < 0)
  899. dest_y += -clip_y1;
  900. /* clip */
  901. clip_x1 = max(clip_x1, 0);
  902. clip_y1 = max(clip_y1, 0);
  903. clip_x2 = min(clip_x2, units[i]->crtc.mode.hdisplay);
  904. clip_y2 = min(clip_y2, units[i]->crtc.mode.vdisplay);
  905. /* and cull any rects that misses the crtc */
  906. if (clip_x1 >= units[i]->crtc.mode.hdisplay ||
  907. clip_y1 >= units[i]->crtc.mode.vdisplay ||
  908. clip_x2 <= 0 || clip_y2 <= 0)
  909. continue;
  910. blits[blits_pos].header = SVGA_CMD_BLIT_SCREEN_TO_GMRFB;
  911. blits[blits_pos].body.srcScreenId = units[i]->unit;
  912. blits[blits_pos].body.destOrigin.x = dest_x;
  913. blits[blits_pos].body.destOrigin.y = dest_y;
  914. blits[blits_pos].body.srcRect.left = clip_x1;
  915. blits[blits_pos].body.srcRect.top = clip_y1;
  916. blits[blits_pos].body.srcRect.right = clip_x2;
  917. blits[blits_pos].body.srcRect.bottom = clip_y2;
  918. blits_pos++;
  919. }
  920. }
  921. /* reset size here and use calculated exact size from loops */
  922. fifo_size = sizeof(*cmd) + sizeof(*blits) * blits_pos;
  923. ret = vmw_execbuf_process(file_priv, dev_priv, NULL, cmd, fifo_size,
  924. 0, user_fence_rep);
  925. kfree(cmd);
  926. return ret;
  927. }
  928. int vmw_kms_init(struct vmw_private *dev_priv)
  929. {
  930. struct drm_device *dev = dev_priv->dev;
  931. int ret;
  932. drm_mode_config_init(dev);
  933. dev->mode_config.funcs = &vmw_kms_funcs;
  934. dev->mode_config.min_width = 1;
  935. dev->mode_config.min_height = 1;
  936. /* assumed largest fb size */
  937. dev->mode_config.max_width = 8192;
  938. dev->mode_config.max_height = 8192;
  939. ret = vmw_kms_init_screen_object_display(dev_priv);
  940. if (ret) /* Fallback */
  941. (void)vmw_kms_init_legacy_display_system(dev_priv);
  942. return 0;
  943. }
  944. int vmw_kms_close(struct vmw_private *dev_priv)
  945. {
  946. /*
  947. * Docs says we should take the lock before calling this function
  948. * but since it destroys encoders and our destructor calls
  949. * drm_encoder_cleanup which takes the lock we deadlock.
  950. */
  951. drm_mode_config_cleanup(dev_priv->dev);
  952. vmw_kms_close_legacy_display_system(dev_priv);
  953. return 0;
  954. }
  955. int vmw_kms_cursor_bypass_ioctl(struct drm_device *dev, void *data,
  956. struct drm_file *file_priv)
  957. {
  958. struct drm_vmw_cursor_bypass_arg *arg = data;
  959. struct vmw_display_unit *du;
  960. struct drm_mode_object *obj;
  961. struct drm_crtc *crtc;
  962. int ret = 0;
  963. mutex_lock(&dev->mode_config.mutex);
  964. if (arg->flags & DRM_VMW_CURSOR_BYPASS_ALL) {
  965. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  966. du = vmw_crtc_to_du(crtc);
  967. du->hotspot_x = arg->xhot;
  968. du->hotspot_y = arg->yhot;
  969. }
  970. mutex_unlock(&dev->mode_config.mutex);
  971. return 0;
  972. }
  973. obj = drm_mode_object_find(dev, arg->crtc_id, DRM_MODE_OBJECT_CRTC);
  974. if (!obj) {
  975. ret = -EINVAL;
  976. goto out;
  977. }
  978. crtc = obj_to_crtc(obj);
  979. du = vmw_crtc_to_du(crtc);
  980. du->hotspot_x = arg->xhot;
  981. du->hotspot_y = arg->yhot;
  982. out:
  983. mutex_unlock(&dev->mode_config.mutex);
  984. return ret;
  985. }
  986. int vmw_kms_write_svga(struct vmw_private *vmw_priv,
  987. unsigned width, unsigned height, unsigned pitch,
  988. unsigned bpp, unsigned depth)
  989. {
  990. if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
  991. vmw_write(vmw_priv, SVGA_REG_PITCHLOCK, pitch);
  992. else if (vmw_fifo_have_pitchlock(vmw_priv))
  993. iowrite32(pitch, vmw_priv->mmio_virt + SVGA_FIFO_PITCHLOCK);
  994. vmw_write(vmw_priv, SVGA_REG_WIDTH, width);
  995. vmw_write(vmw_priv, SVGA_REG_HEIGHT, height);
  996. vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, bpp);
  997. if (vmw_read(vmw_priv, SVGA_REG_DEPTH) != depth) {
  998. DRM_ERROR("Invalid depth %u for %u bpp, host expects %u\n",
  999. depth, bpp, vmw_read(vmw_priv, SVGA_REG_DEPTH));
  1000. return -EINVAL;
  1001. }
  1002. return 0;
  1003. }
  1004. int vmw_kms_save_vga(struct vmw_private *vmw_priv)
  1005. {
  1006. struct vmw_vga_topology_state *save;
  1007. uint32_t i;
  1008. vmw_priv->vga_width = vmw_read(vmw_priv, SVGA_REG_WIDTH);
  1009. vmw_priv->vga_height = vmw_read(vmw_priv, SVGA_REG_HEIGHT);
  1010. vmw_priv->vga_bpp = vmw_read(vmw_priv, SVGA_REG_BITS_PER_PIXEL);
  1011. if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
  1012. vmw_priv->vga_pitchlock =
  1013. vmw_read(vmw_priv, SVGA_REG_PITCHLOCK);
  1014. else if (vmw_fifo_have_pitchlock(vmw_priv))
  1015. vmw_priv->vga_pitchlock = ioread32(vmw_priv->mmio_virt +
  1016. SVGA_FIFO_PITCHLOCK);
  1017. if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
  1018. return 0;
  1019. vmw_priv->num_displays = vmw_read(vmw_priv,
  1020. SVGA_REG_NUM_GUEST_DISPLAYS);
  1021. if (vmw_priv->num_displays == 0)
  1022. vmw_priv->num_displays = 1;
  1023. for (i = 0; i < vmw_priv->num_displays; ++i) {
  1024. save = &vmw_priv->vga_save[i];
  1025. vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
  1026. save->primary = vmw_read(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY);
  1027. save->pos_x = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_X);
  1028. save->pos_y = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y);
  1029. save->width = vmw_read(vmw_priv, SVGA_REG_DISPLAY_WIDTH);
  1030. save->height = vmw_read(vmw_priv, SVGA_REG_DISPLAY_HEIGHT);
  1031. vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
  1032. if (i == 0 && vmw_priv->num_displays == 1 &&
  1033. save->width == 0 && save->height == 0) {
  1034. /*
  1035. * It should be fairly safe to assume that these
  1036. * values are uninitialized.
  1037. */
  1038. save->width = vmw_priv->vga_width - save->pos_x;
  1039. save->height = vmw_priv->vga_height - save->pos_y;
  1040. }
  1041. }
  1042. return 0;
  1043. }
  1044. int vmw_kms_restore_vga(struct vmw_private *vmw_priv)
  1045. {
  1046. struct vmw_vga_topology_state *save;
  1047. uint32_t i;
  1048. vmw_write(vmw_priv, SVGA_REG_WIDTH, vmw_priv->vga_width);
  1049. vmw_write(vmw_priv, SVGA_REG_HEIGHT, vmw_priv->vga_height);
  1050. vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, vmw_priv->vga_bpp);
  1051. if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
  1052. vmw_write(vmw_priv, SVGA_REG_PITCHLOCK,
  1053. vmw_priv->vga_pitchlock);
  1054. else if (vmw_fifo_have_pitchlock(vmw_priv))
  1055. iowrite32(vmw_priv->vga_pitchlock,
  1056. vmw_priv->mmio_virt + SVGA_FIFO_PITCHLOCK);
  1057. if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
  1058. return 0;
  1059. for (i = 0; i < vmw_priv->num_displays; ++i) {
  1060. save = &vmw_priv->vga_save[i];
  1061. vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
  1062. vmw_write(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY, save->primary);
  1063. vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_X, save->pos_x);
  1064. vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y, save->pos_y);
  1065. vmw_write(vmw_priv, SVGA_REG_DISPLAY_WIDTH, save->width);
  1066. vmw_write(vmw_priv, SVGA_REG_DISPLAY_HEIGHT, save->height);
  1067. vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
  1068. }
  1069. return 0;
  1070. }
  1071. bool vmw_kms_validate_mode_vram(struct vmw_private *dev_priv,
  1072. uint32_t pitch,
  1073. uint32_t height)
  1074. {
  1075. return ((u64) pitch * (u64) height) < (u64) dev_priv->vram_size;
  1076. }
  1077. u32 vmw_get_vblank_counter(struct drm_device *dev, int crtc)
  1078. {
  1079. return 0;
  1080. }
  1081. /*
  1082. * Small shared kms functions.
  1083. */
  1084. int vmw_du_update_layout(struct vmw_private *dev_priv, unsigned num,
  1085. struct drm_vmw_rect *rects)
  1086. {
  1087. struct drm_device *dev = dev_priv->dev;
  1088. struct vmw_display_unit *du;
  1089. struct drm_connector *con;
  1090. mutex_lock(&dev->mode_config.mutex);
  1091. #if 0
  1092. {
  1093. unsigned int i;
  1094. DRM_INFO("%s: new layout ", __func__);
  1095. for (i = 0; i < num; i++)
  1096. DRM_INFO("(%i, %i %ux%u) ", rects[i].x, rects[i].y,
  1097. rects[i].w, rects[i].h);
  1098. DRM_INFO("\n");
  1099. }
  1100. #endif
  1101. list_for_each_entry(con, &dev->mode_config.connector_list, head) {
  1102. du = vmw_connector_to_du(con);
  1103. if (num > du->unit) {
  1104. du->pref_width = rects[du->unit].w;
  1105. du->pref_height = rects[du->unit].h;
  1106. du->pref_active = true;
  1107. } else {
  1108. du->pref_width = 800;
  1109. du->pref_height = 600;
  1110. du->pref_active = false;
  1111. }
  1112. con->status = vmw_du_connector_detect(con, true);
  1113. }
  1114. mutex_unlock(&dev->mode_config.mutex);
  1115. return 0;
  1116. }
  1117. void vmw_du_crtc_save(struct drm_crtc *crtc)
  1118. {
  1119. }
  1120. void vmw_du_crtc_restore(struct drm_crtc *crtc)
  1121. {
  1122. }
  1123. void vmw_du_crtc_gamma_set(struct drm_crtc *crtc,
  1124. u16 *r, u16 *g, u16 *b,
  1125. uint32_t start, uint32_t size)
  1126. {
  1127. struct vmw_private *dev_priv = vmw_priv(crtc->dev);
  1128. int i;
  1129. for (i = 0; i < size; i++) {
  1130. DRM_DEBUG("%d r/g/b = 0x%04x / 0x%04x / 0x%04x\n", i,
  1131. r[i], g[i], b[i]);
  1132. vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 0, r[i] >> 8);
  1133. vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 1, g[i] >> 8);
  1134. vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 2, b[i] >> 8);
  1135. }
  1136. }
  1137. void vmw_du_connector_dpms(struct drm_connector *connector, int mode)
  1138. {
  1139. }
  1140. void vmw_du_connector_save(struct drm_connector *connector)
  1141. {
  1142. }
  1143. void vmw_du_connector_restore(struct drm_connector *connector)
  1144. {
  1145. }
  1146. enum drm_connector_status
  1147. vmw_du_connector_detect(struct drm_connector *connector, bool force)
  1148. {
  1149. uint32_t num_displays;
  1150. struct drm_device *dev = connector->dev;
  1151. struct vmw_private *dev_priv = vmw_priv(dev);
  1152. mutex_lock(&dev_priv->hw_mutex);
  1153. num_displays = vmw_read(dev_priv, SVGA_REG_NUM_DISPLAYS);
  1154. mutex_unlock(&dev_priv->hw_mutex);
  1155. return ((vmw_connector_to_du(connector)->unit < num_displays) ?
  1156. connector_status_connected : connector_status_disconnected);
  1157. }
  1158. static struct drm_display_mode vmw_kms_connector_builtin[] = {
  1159. /* 640x480@60Hz */
  1160. { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 25175, 640, 656,
  1161. 752, 800, 0, 480, 489, 492, 525, 0,
  1162. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
  1163. /* 800x600@60Hz */
  1164. { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 40000, 800, 840,
  1165. 968, 1056, 0, 600, 601, 605, 628, 0,
  1166. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1167. /* 1024x768@60Hz */
  1168. { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 65000, 1024, 1048,
  1169. 1184, 1344, 0, 768, 771, 777, 806, 0,
  1170. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
  1171. /* 1152x864@75Hz */
  1172. { DRM_MODE("1152x864", DRM_MODE_TYPE_DRIVER, 108000, 1152, 1216,
  1173. 1344, 1600, 0, 864, 865, 868, 900, 0,
  1174. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1175. /* 1280x768@60Hz */
  1176. { DRM_MODE("1280x768", DRM_MODE_TYPE_DRIVER, 79500, 1280, 1344,
  1177. 1472, 1664, 0, 768, 771, 778, 798, 0,
  1178. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1179. /* 1280x800@60Hz */
  1180. { DRM_MODE("1280x800", DRM_MODE_TYPE_DRIVER, 83500, 1280, 1352,
  1181. 1480, 1680, 0, 800, 803, 809, 831, 0,
  1182. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
  1183. /* 1280x960@60Hz */
  1184. { DRM_MODE("1280x960", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1376,
  1185. 1488, 1800, 0, 960, 961, 964, 1000, 0,
  1186. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1187. /* 1280x1024@60Hz */
  1188. { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1328,
  1189. 1440, 1688, 0, 1024, 1025, 1028, 1066, 0,
  1190. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1191. /* 1360x768@60Hz */
  1192. { DRM_MODE("1360x768", DRM_MODE_TYPE_DRIVER, 85500, 1360, 1424,
  1193. 1536, 1792, 0, 768, 771, 777, 795, 0,
  1194. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1195. /* 1440x1050@60Hz */
  1196. { DRM_MODE("1400x1050", DRM_MODE_TYPE_DRIVER, 121750, 1400, 1488,
  1197. 1632, 1864, 0, 1050, 1053, 1057, 1089, 0,
  1198. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1199. /* 1440x900@60Hz */
  1200. { DRM_MODE("1440x900", DRM_MODE_TYPE_DRIVER, 106500, 1440, 1520,
  1201. 1672, 1904, 0, 900, 903, 909, 934, 0,
  1202. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1203. /* 1600x1200@60Hz */
  1204. { DRM_MODE("1600x1200", DRM_MODE_TYPE_DRIVER, 162000, 1600, 1664,
  1205. 1856, 2160, 0, 1200, 1201, 1204, 1250, 0,
  1206. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1207. /* 1680x1050@60Hz */
  1208. { DRM_MODE("1680x1050", DRM_MODE_TYPE_DRIVER, 146250, 1680, 1784,
  1209. 1960, 2240, 0, 1050, 1053, 1059, 1089, 0,
  1210. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1211. /* 1792x1344@60Hz */
  1212. { DRM_MODE("1792x1344", DRM_MODE_TYPE_DRIVER, 204750, 1792, 1920,
  1213. 2120, 2448, 0, 1344, 1345, 1348, 1394, 0,
  1214. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1215. /* 1853x1392@60Hz */
  1216. { DRM_MODE("1856x1392", DRM_MODE_TYPE_DRIVER, 218250, 1856, 1952,
  1217. 2176, 2528, 0, 1392, 1393, 1396, 1439, 0,
  1218. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1219. /* 1920x1200@60Hz */
  1220. { DRM_MODE("1920x1200", DRM_MODE_TYPE_DRIVER, 193250, 1920, 2056,
  1221. 2256, 2592, 0, 1200, 1203, 1209, 1245, 0,
  1222. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1223. /* 1920x1440@60Hz */
  1224. { DRM_MODE("1920x1440", DRM_MODE_TYPE_DRIVER, 234000, 1920, 2048,
  1225. 2256, 2600, 0, 1440, 1441, 1444, 1500, 0,
  1226. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1227. /* 2560x1600@60Hz */
  1228. { DRM_MODE("2560x1600", DRM_MODE_TYPE_DRIVER, 348500, 2560, 2752,
  1229. 3032, 3504, 0, 1600, 1603, 1609, 1658, 0,
  1230. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1231. /* Terminate */
  1232. { DRM_MODE("", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) },
  1233. };
  1234. int vmw_du_connector_fill_modes(struct drm_connector *connector,
  1235. uint32_t max_width, uint32_t max_height)
  1236. {
  1237. struct vmw_display_unit *du = vmw_connector_to_du(connector);
  1238. struct drm_device *dev = connector->dev;
  1239. struct vmw_private *dev_priv = vmw_priv(dev);
  1240. struct drm_display_mode *mode = NULL;
  1241. struct drm_display_mode *bmode;
  1242. struct drm_display_mode prefmode = { DRM_MODE("preferred",
  1243. DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED,
  1244. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1245. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC)
  1246. };
  1247. int i;
  1248. /* Add preferred mode */
  1249. {
  1250. mode = drm_mode_duplicate(dev, &prefmode);
  1251. if (!mode)
  1252. return 0;
  1253. mode->hdisplay = du->pref_width;
  1254. mode->vdisplay = du->pref_height;
  1255. mode->vrefresh = drm_mode_vrefresh(mode);
  1256. if (vmw_kms_validate_mode_vram(dev_priv, mode->hdisplay * 2,
  1257. mode->vdisplay)) {
  1258. drm_mode_probed_add(connector, mode);
  1259. if (du->pref_mode) {
  1260. list_del_init(&du->pref_mode->head);
  1261. drm_mode_destroy(dev, du->pref_mode);
  1262. }
  1263. du->pref_mode = mode;
  1264. }
  1265. }
  1266. for (i = 0; vmw_kms_connector_builtin[i].type != 0; i++) {
  1267. bmode = &vmw_kms_connector_builtin[i];
  1268. if (bmode->hdisplay > max_width ||
  1269. bmode->vdisplay > max_height)
  1270. continue;
  1271. if (!vmw_kms_validate_mode_vram(dev_priv, bmode->hdisplay * 2,
  1272. bmode->vdisplay))
  1273. continue;
  1274. mode = drm_mode_duplicate(dev, bmode);
  1275. if (!mode)
  1276. return 0;
  1277. mode->vrefresh = drm_mode_vrefresh(mode);
  1278. drm_mode_probed_add(connector, mode);
  1279. }
  1280. drm_mode_connector_list_update(connector);
  1281. return 1;
  1282. }
  1283. int vmw_du_connector_set_property(struct drm_connector *connector,
  1284. struct drm_property *property,
  1285. uint64_t val)
  1286. {
  1287. return 0;
  1288. }