vmwgfx_kms.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872
  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 programer, cant handle wierd 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 delayed_work d_work;
  272. struct mutex work_lock;
  273. bool present_fs;
  274. };
  275. void vmw_framebuffer_surface_destroy(struct drm_framebuffer *framebuffer)
  276. {
  277. struct vmw_framebuffer_surface *vfb =
  278. vmw_framebuffer_to_vfbs(framebuffer);
  279. cancel_delayed_work_sync(&vfb->d_work);
  280. drm_framebuffer_cleanup(framebuffer);
  281. vmw_surface_unreference(&vfb->surface);
  282. kfree(framebuffer);
  283. }
  284. static void vmw_framebuffer_present_fs_callback(struct work_struct *work)
  285. {
  286. struct delayed_work *d_work =
  287. container_of(work, struct delayed_work, work);
  288. struct vmw_framebuffer_surface *vfbs =
  289. container_of(d_work, struct vmw_framebuffer_surface, d_work);
  290. struct vmw_surface *surf = vfbs->surface;
  291. struct drm_framebuffer *framebuffer = &vfbs->base.base;
  292. struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
  293. struct {
  294. SVGA3dCmdHeader header;
  295. SVGA3dCmdPresent body;
  296. SVGA3dCopyRect cr;
  297. } *cmd;
  298. mutex_lock(&vfbs->work_lock);
  299. if (!vfbs->present_fs)
  300. goto out_unlock;
  301. cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
  302. if (unlikely(cmd == NULL))
  303. goto out_resched;
  304. cmd->header.id = cpu_to_le32(SVGA_3D_CMD_PRESENT);
  305. cmd->header.size = cpu_to_le32(sizeof(cmd->body) + sizeof(cmd->cr));
  306. cmd->body.sid = cpu_to_le32(surf->res.id);
  307. cmd->cr.x = cpu_to_le32(0);
  308. cmd->cr.y = cpu_to_le32(0);
  309. cmd->cr.srcx = cmd->cr.x;
  310. cmd->cr.srcy = cmd->cr.y;
  311. cmd->cr.w = cpu_to_le32(framebuffer->width);
  312. cmd->cr.h = cpu_to_le32(framebuffer->height);
  313. vfbs->present_fs = false;
  314. vmw_fifo_commit(dev_priv, sizeof(*cmd));
  315. out_resched:
  316. /**
  317. * Will not re-add if already pending.
  318. */
  319. schedule_delayed_work(&vfbs->d_work, VMWGFX_PRESENT_RATE);
  320. out_unlock:
  321. mutex_unlock(&vfbs->work_lock);
  322. }
  323. int vmw_framebuffer_surface_dirty(struct drm_framebuffer *framebuffer,
  324. unsigned flags, unsigned color,
  325. struct drm_clip_rect *clips,
  326. unsigned num_clips)
  327. {
  328. struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
  329. struct vmw_framebuffer_surface *vfbs =
  330. vmw_framebuffer_to_vfbs(framebuffer);
  331. struct vmw_surface *surf = vfbs->surface;
  332. struct drm_clip_rect norect;
  333. SVGA3dCopyRect *cr;
  334. int i, inc = 1;
  335. struct {
  336. SVGA3dCmdHeader header;
  337. SVGA3dCmdPresent body;
  338. SVGA3dCopyRect cr;
  339. } *cmd;
  340. if (!num_clips ||
  341. !(dev_priv->fifo.capabilities &
  342. SVGA_FIFO_CAP_SCREEN_OBJECT)) {
  343. int ret;
  344. mutex_lock(&vfbs->work_lock);
  345. vfbs->present_fs = true;
  346. ret = schedule_delayed_work(&vfbs->d_work, VMWGFX_PRESENT_RATE);
  347. mutex_unlock(&vfbs->work_lock);
  348. if (ret) {
  349. /**
  350. * No work pending, Force immediate present.
  351. */
  352. vmw_framebuffer_present_fs_callback(&vfbs->d_work.work);
  353. }
  354. return 0;
  355. }
  356. if (!num_clips) {
  357. num_clips = 1;
  358. clips = &norect;
  359. norect.x1 = norect.y1 = 0;
  360. norect.x2 = framebuffer->width;
  361. norect.y2 = framebuffer->height;
  362. } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
  363. num_clips /= 2;
  364. inc = 2; /* skip source rects */
  365. }
  366. cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd) + (num_clips - 1) * sizeof(cmd->cr));
  367. if (unlikely(cmd == NULL)) {
  368. DRM_ERROR("Fifo reserve failed.\n");
  369. return -ENOMEM;
  370. }
  371. memset(cmd, 0, sizeof(*cmd));
  372. cmd->header.id = cpu_to_le32(SVGA_3D_CMD_PRESENT);
  373. cmd->header.size = cpu_to_le32(sizeof(cmd->body) + num_clips * sizeof(cmd->cr));
  374. cmd->body.sid = cpu_to_le32(surf->res.id);
  375. for (i = 0, cr = &cmd->cr; i < num_clips; i++, cr++, clips += inc) {
  376. cr->x = cpu_to_le16(clips->x1);
  377. cr->y = cpu_to_le16(clips->y1);
  378. cr->srcx = cr->x;
  379. cr->srcy = cr->y;
  380. cr->w = cpu_to_le16(clips->x2 - clips->x1);
  381. cr->h = cpu_to_le16(clips->y2 - clips->y1);
  382. }
  383. vmw_fifo_commit(dev_priv, sizeof(*cmd) + (num_clips - 1) * sizeof(cmd->cr));
  384. return 0;
  385. }
  386. static struct drm_framebuffer_funcs vmw_framebuffer_surface_funcs = {
  387. .destroy = vmw_framebuffer_surface_destroy,
  388. .dirty = vmw_framebuffer_surface_dirty,
  389. .create_handle = vmw_framebuffer_create_handle,
  390. };
  391. int vmw_kms_new_framebuffer_surface(struct vmw_private *dev_priv,
  392. struct vmw_surface *surface,
  393. struct vmw_framebuffer **out,
  394. unsigned width, unsigned height)
  395. {
  396. struct drm_device *dev = dev_priv->dev;
  397. struct vmw_framebuffer_surface *vfbs;
  398. int ret;
  399. vfbs = kzalloc(sizeof(*vfbs), GFP_KERNEL);
  400. if (!vfbs) {
  401. ret = -ENOMEM;
  402. goto out_err1;
  403. }
  404. ret = drm_framebuffer_init(dev, &vfbs->base.base,
  405. &vmw_framebuffer_surface_funcs);
  406. if (ret)
  407. goto out_err2;
  408. if (!vmw_surface_reference(surface)) {
  409. DRM_ERROR("failed to reference surface %p\n", surface);
  410. goto out_err3;
  411. }
  412. /* XXX get the first 3 from the surface info */
  413. vfbs->base.base.bits_per_pixel = 32;
  414. vfbs->base.base.pitch = width * 32 / 4;
  415. vfbs->base.base.depth = 24;
  416. vfbs->base.base.width = width;
  417. vfbs->base.base.height = height;
  418. vfbs->base.pin = NULL;
  419. vfbs->base.unpin = NULL;
  420. vfbs->surface = surface;
  421. mutex_init(&vfbs->work_lock);
  422. INIT_DELAYED_WORK(&vfbs->d_work, &vmw_framebuffer_present_fs_callback);
  423. *out = &vfbs->base;
  424. return 0;
  425. out_err3:
  426. drm_framebuffer_cleanup(&vfbs->base.base);
  427. out_err2:
  428. kfree(vfbs);
  429. out_err1:
  430. return ret;
  431. }
  432. /*
  433. * Dmabuf framebuffer code
  434. */
  435. #define vmw_framebuffer_to_vfbd(x) \
  436. container_of(x, struct vmw_framebuffer_dmabuf, base.base)
  437. struct vmw_framebuffer_dmabuf {
  438. struct vmw_framebuffer base;
  439. struct vmw_dma_buffer *buffer;
  440. };
  441. void vmw_framebuffer_dmabuf_destroy(struct drm_framebuffer *framebuffer)
  442. {
  443. struct vmw_framebuffer_dmabuf *vfbd =
  444. vmw_framebuffer_to_vfbd(framebuffer);
  445. drm_framebuffer_cleanup(framebuffer);
  446. vmw_dmabuf_unreference(&vfbd->buffer);
  447. kfree(vfbd);
  448. }
  449. int vmw_framebuffer_dmabuf_dirty(struct drm_framebuffer *framebuffer,
  450. unsigned flags, unsigned color,
  451. struct drm_clip_rect *clips,
  452. unsigned num_clips)
  453. {
  454. struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
  455. struct drm_clip_rect norect;
  456. struct {
  457. uint32_t header;
  458. SVGAFifoCmdUpdate body;
  459. } *cmd;
  460. int i, increment = 1;
  461. if (!num_clips ||
  462. !(dev_priv->fifo.capabilities &
  463. SVGA_FIFO_CAP_SCREEN_OBJECT)) {
  464. num_clips = 1;
  465. clips = &norect;
  466. norect.x1 = norect.y1 = 0;
  467. norect.x2 = framebuffer->width;
  468. norect.y2 = framebuffer->height;
  469. } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
  470. num_clips /= 2;
  471. increment = 2;
  472. }
  473. cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd) * num_clips);
  474. if (unlikely(cmd == NULL)) {
  475. DRM_ERROR("Fifo reserve failed.\n");
  476. return -ENOMEM;
  477. }
  478. for (i = 0; i < num_clips; i++, clips += increment) {
  479. cmd[i].header = cpu_to_le32(SVGA_CMD_UPDATE);
  480. cmd[i].body.x = cpu_to_le32(clips[i].x1);
  481. cmd[i].body.y = cpu_to_le32(clips[i].y1);
  482. cmd[i].body.width = cpu_to_le32(clips[i].x2 - clips[i].x1);
  483. cmd[i].body.height = cpu_to_le32(clips[i].y2 - clips[i].y1);
  484. }
  485. vmw_fifo_commit(dev_priv, sizeof(*cmd) * num_clips);
  486. return 0;
  487. }
  488. static struct drm_framebuffer_funcs vmw_framebuffer_dmabuf_funcs = {
  489. .destroy = vmw_framebuffer_dmabuf_destroy,
  490. .dirty = vmw_framebuffer_dmabuf_dirty,
  491. .create_handle = vmw_framebuffer_create_handle,
  492. };
  493. static int vmw_framebuffer_dmabuf_pin(struct vmw_framebuffer *vfb)
  494. {
  495. struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
  496. struct vmw_framebuffer_dmabuf *vfbd =
  497. vmw_framebuffer_to_vfbd(&vfb->base);
  498. int ret;
  499. vmw_overlay_pause_all(dev_priv);
  500. ret = vmw_dmabuf_to_start_of_vram(dev_priv, vfbd->buffer);
  501. if (dev_priv->capabilities & SVGA_CAP_MULTIMON) {
  502. vmw_write(dev_priv, SVGA_REG_NUM_GUEST_DISPLAYS, 1);
  503. vmw_write(dev_priv, SVGA_REG_DISPLAY_ID, 0);
  504. vmw_write(dev_priv, SVGA_REG_DISPLAY_IS_PRIMARY, true);
  505. vmw_write(dev_priv, SVGA_REG_DISPLAY_POSITION_X, 0);
  506. vmw_write(dev_priv, SVGA_REG_DISPLAY_POSITION_Y, 0);
  507. vmw_write(dev_priv, SVGA_REG_DISPLAY_WIDTH, 0);
  508. vmw_write(dev_priv, SVGA_REG_DISPLAY_HEIGHT, 0);
  509. vmw_write(dev_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
  510. vmw_write(dev_priv, SVGA_REG_ENABLE, 1);
  511. vmw_write(dev_priv, SVGA_REG_WIDTH, vfb->base.width);
  512. vmw_write(dev_priv, SVGA_REG_HEIGHT, vfb->base.height);
  513. vmw_write(dev_priv, SVGA_REG_BITS_PER_PIXEL, vfb->base.bits_per_pixel);
  514. vmw_write(dev_priv, SVGA_REG_DEPTH, vfb->base.depth);
  515. vmw_write(dev_priv, SVGA_REG_RED_MASK, 0x00ff0000);
  516. vmw_write(dev_priv, SVGA_REG_GREEN_MASK, 0x0000ff00);
  517. vmw_write(dev_priv, SVGA_REG_BLUE_MASK, 0x000000ff);
  518. } else
  519. WARN_ON(true);
  520. vmw_overlay_resume_all(dev_priv);
  521. return 0;
  522. }
  523. static int vmw_framebuffer_dmabuf_unpin(struct vmw_framebuffer *vfb)
  524. {
  525. struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
  526. struct vmw_framebuffer_dmabuf *vfbd =
  527. vmw_framebuffer_to_vfbd(&vfb->base);
  528. if (!vfbd->buffer) {
  529. WARN_ON(!vfbd->buffer);
  530. return 0;
  531. }
  532. return vmw_dmabuf_from_vram(dev_priv, vfbd->buffer);
  533. }
  534. int vmw_kms_new_framebuffer_dmabuf(struct vmw_private *dev_priv,
  535. struct vmw_dma_buffer *dmabuf,
  536. struct vmw_framebuffer **out,
  537. unsigned width, unsigned height)
  538. {
  539. struct drm_device *dev = dev_priv->dev;
  540. struct vmw_framebuffer_dmabuf *vfbd;
  541. int ret;
  542. vfbd = kzalloc(sizeof(*vfbd), GFP_KERNEL);
  543. if (!vfbd) {
  544. ret = -ENOMEM;
  545. goto out_err1;
  546. }
  547. ret = drm_framebuffer_init(dev, &vfbd->base.base,
  548. &vmw_framebuffer_dmabuf_funcs);
  549. if (ret)
  550. goto out_err2;
  551. if (!vmw_dmabuf_reference(dmabuf)) {
  552. DRM_ERROR("failed to reference dmabuf %p\n", dmabuf);
  553. goto out_err3;
  554. }
  555. /* XXX get the first 3 from the surface info */
  556. vfbd->base.base.bits_per_pixel = 32;
  557. vfbd->base.base.pitch = width * 32 / 4;
  558. vfbd->base.base.depth = 24;
  559. vfbd->base.base.width = width;
  560. vfbd->base.base.height = height;
  561. vfbd->base.pin = vmw_framebuffer_dmabuf_pin;
  562. vfbd->base.unpin = vmw_framebuffer_dmabuf_unpin;
  563. vfbd->buffer = dmabuf;
  564. *out = &vfbd->base;
  565. return 0;
  566. out_err3:
  567. drm_framebuffer_cleanup(&vfbd->base.base);
  568. out_err2:
  569. kfree(vfbd);
  570. out_err1:
  571. return ret;
  572. }
  573. /*
  574. * Generic Kernel modesetting functions
  575. */
  576. static struct drm_framebuffer *vmw_kms_fb_create(struct drm_device *dev,
  577. struct drm_file *file_priv,
  578. struct drm_mode_fb_cmd *mode_cmd)
  579. {
  580. struct vmw_private *dev_priv = vmw_priv(dev);
  581. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  582. struct vmw_framebuffer *vfb = NULL;
  583. struct vmw_surface *surface = NULL;
  584. struct vmw_dma_buffer *bo = NULL;
  585. int ret;
  586. ret = vmw_user_surface_lookup_handle(dev_priv, tfile,
  587. mode_cmd->handle, &surface);
  588. if (ret)
  589. goto try_dmabuf;
  590. ret = vmw_kms_new_framebuffer_surface(dev_priv, surface, &vfb,
  591. mode_cmd->width, mode_cmd->height);
  592. /* vmw_user_surface_lookup takes one ref so does new_fb */
  593. vmw_surface_unreference(&surface);
  594. if (ret) {
  595. DRM_ERROR("failed to create vmw_framebuffer: %i\n", ret);
  596. return NULL;
  597. }
  598. return &vfb->base;
  599. try_dmabuf:
  600. DRM_INFO("%s: trying buffer\n", __func__);
  601. ret = vmw_user_dmabuf_lookup(tfile, mode_cmd->handle, &bo);
  602. if (ret) {
  603. DRM_ERROR("failed to find buffer: %i\n", ret);
  604. return NULL;
  605. }
  606. ret = vmw_kms_new_framebuffer_dmabuf(dev_priv, bo, &vfb,
  607. mode_cmd->width, mode_cmd->height);
  608. /* vmw_user_dmabuf_lookup takes one ref so does new_fb */
  609. vmw_dmabuf_unreference(&bo);
  610. if (ret) {
  611. DRM_ERROR("failed to create vmw_framebuffer: %i\n", ret);
  612. return NULL;
  613. }
  614. return &vfb->base;
  615. }
  616. static int vmw_kms_fb_changed(struct drm_device *dev)
  617. {
  618. return 0;
  619. }
  620. static struct drm_mode_config_funcs vmw_kms_funcs = {
  621. .fb_create = vmw_kms_fb_create,
  622. .fb_changed = vmw_kms_fb_changed,
  623. };
  624. int vmw_kms_init(struct vmw_private *dev_priv)
  625. {
  626. struct drm_device *dev = dev_priv->dev;
  627. int ret;
  628. drm_mode_config_init(dev);
  629. dev->mode_config.funcs = &vmw_kms_funcs;
  630. dev->mode_config.min_width = 640;
  631. dev->mode_config.min_height = 480;
  632. dev->mode_config.max_width = 2048;
  633. dev->mode_config.max_height = 2048;
  634. ret = vmw_kms_init_legacy_display_system(dev_priv);
  635. return 0;
  636. }
  637. int vmw_kms_close(struct vmw_private *dev_priv)
  638. {
  639. /*
  640. * Docs says we should take the lock before calling this function
  641. * but since it destroys encoders and our destructor calls
  642. * drm_encoder_cleanup which takes the lock we deadlock.
  643. */
  644. drm_mode_config_cleanup(dev_priv->dev);
  645. vmw_kms_close_legacy_display_system(dev_priv);
  646. return 0;
  647. }
  648. int vmw_kms_cursor_bypass_ioctl(struct drm_device *dev, void *data,
  649. struct drm_file *file_priv)
  650. {
  651. struct drm_vmw_cursor_bypass_arg *arg = data;
  652. struct vmw_display_unit *du;
  653. struct drm_mode_object *obj;
  654. struct drm_crtc *crtc;
  655. int ret = 0;
  656. mutex_lock(&dev->mode_config.mutex);
  657. if (arg->flags & DRM_VMW_CURSOR_BYPASS_ALL) {
  658. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  659. du = vmw_crtc_to_du(crtc);
  660. du->hotspot_x = arg->xhot;
  661. du->hotspot_y = arg->yhot;
  662. }
  663. mutex_unlock(&dev->mode_config.mutex);
  664. return 0;
  665. }
  666. obj = drm_mode_object_find(dev, arg->crtc_id, DRM_MODE_OBJECT_CRTC);
  667. if (!obj) {
  668. ret = -EINVAL;
  669. goto out;
  670. }
  671. crtc = obj_to_crtc(obj);
  672. du = vmw_crtc_to_du(crtc);
  673. du->hotspot_x = arg->xhot;
  674. du->hotspot_y = arg->yhot;
  675. out:
  676. mutex_unlock(&dev->mode_config.mutex);
  677. return ret;
  678. }
  679. int vmw_kms_save_vga(struct vmw_private *vmw_priv)
  680. {
  681. /*
  682. * setup a single multimon monitor with the size
  683. * of 0x0, this stops the UI from resizing when we
  684. * change the framebuffer size
  685. */
  686. if (vmw_priv->capabilities & SVGA_CAP_MULTIMON) {
  687. vmw_write(vmw_priv, SVGA_REG_NUM_GUEST_DISPLAYS, 1);
  688. vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, 0);
  689. vmw_write(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY, true);
  690. vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_X, 0);
  691. vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y, 0);
  692. vmw_write(vmw_priv, SVGA_REG_DISPLAY_WIDTH, 0);
  693. vmw_write(vmw_priv, SVGA_REG_DISPLAY_HEIGHT, 0);
  694. vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
  695. }
  696. vmw_priv->vga_width = vmw_read(vmw_priv, SVGA_REG_WIDTH);
  697. vmw_priv->vga_height = vmw_read(vmw_priv, SVGA_REG_HEIGHT);
  698. vmw_priv->vga_bpp = vmw_read(vmw_priv, SVGA_REG_BITS_PER_PIXEL);
  699. vmw_priv->vga_depth = vmw_read(vmw_priv, SVGA_REG_DEPTH);
  700. vmw_priv->vga_pseudo = vmw_read(vmw_priv, SVGA_REG_PSEUDOCOLOR);
  701. vmw_priv->vga_red_mask = vmw_read(vmw_priv, SVGA_REG_RED_MASK);
  702. vmw_priv->vga_green_mask = vmw_read(vmw_priv, SVGA_REG_GREEN_MASK);
  703. vmw_priv->vga_blue_mask = vmw_read(vmw_priv, SVGA_REG_BLUE_MASK);
  704. return 0;
  705. }
  706. int vmw_kms_restore_vga(struct vmw_private *vmw_priv)
  707. {
  708. vmw_write(vmw_priv, SVGA_REG_WIDTH, vmw_priv->vga_width);
  709. vmw_write(vmw_priv, SVGA_REG_HEIGHT, vmw_priv->vga_height);
  710. vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, vmw_priv->vga_bpp);
  711. vmw_write(vmw_priv, SVGA_REG_DEPTH, vmw_priv->vga_depth);
  712. vmw_write(vmw_priv, SVGA_REG_PSEUDOCOLOR, vmw_priv->vga_pseudo);
  713. vmw_write(vmw_priv, SVGA_REG_RED_MASK, vmw_priv->vga_red_mask);
  714. vmw_write(vmw_priv, SVGA_REG_GREEN_MASK, vmw_priv->vga_green_mask);
  715. vmw_write(vmw_priv, SVGA_REG_BLUE_MASK, vmw_priv->vga_blue_mask);
  716. /* TODO check for multimon */
  717. vmw_write(vmw_priv, SVGA_REG_NUM_GUEST_DISPLAYS, 0);
  718. return 0;
  719. }