vmwgfx_kms.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115
  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. static int vmw_surface_dmabuf_pin(struct vmw_framebuffer *vfb);
  31. static int vmw_surface_dmabuf_unpin(struct vmw_framebuffer *vfb);
  32. void vmw_display_unit_cleanup(struct vmw_display_unit *du)
  33. {
  34. if (du->cursor_surface)
  35. vmw_surface_unreference(&du->cursor_surface);
  36. if (du->cursor_dmabuf)
  37. vmw_dmabuf_unreference(&du->cursor_dmabuf);
  38. drm_crtc_cleanup(&du->crtc);
  39. drm_encoder_cleanup(&du->encoder);
  40. drm_connector_cleanup(&du->connector);
  41. }
  42. /*
  43. * Display Unit Cursor functions
  44. */
  45. int vmw_cursor_update_image(struct vmw_private *dev_priv,
  46. u32 *image, u32 width, u32 height,
  47. u32 hotspotX, u32 hotspotY)
  48. {
  49. struct {
  50. u32 cmd;
  51. SVGAFifoCmdDefineAlphaCursor cursor;
  52. } *cmd;
  53. u32 image_size = width * height * 4;
  54. u32 cmd_size = sizeof(*cmd) + image_size;
  55. if (!image)
  56. return -EINVAL;
  57. cmd = vmw_fifo_reserve(dev_priv, cmd_size);
  58. if (unlikely(cmd == NULL)) {
  59. DRM_ERROR("Fifo reserve failed.\n");
  60. return -ENOMEM;
  61. }
  62. memset(cmd, 0, sizeof(*cmd));
  63. memcpy(&cmd[1], image, image_size);
  64. cmd->cmd = cpu_to_le32(SVGA_CMD_DEFINE_ALPHA_CURSOR);
  65. cmd->cursor.id = cpu_to_le32(0);
  66. cmd->cursor.width = cpu_to_le32(width);
  67. cmd->cursor.height = cpu_to_le32(height);
  68. cmd->cursor.hotspotX = cpu_to_le32(hotspotX);
  69. cmd->cursor.hotspotY = cpu_to_le32(hotspotY);
  70. vmw_fifo_commit(dev_priv, cmd_size);
  71. return 0;
  72. }
  73. void vmw_cursor_update_position(struct vmw_private *dev_priv,
  74. bool show, int x, int y)
  75. {
  76. __le32 __iomem *fifo_mem = dev_priv->mmio_virt;
  77. uint32_t count;
  78. iowrite32(show ? 1 : 0, fifo_mem + SVGA_FIFO_CURSOR_ON);
  79. iowrite32(x, fifo_mem + SVGA_FIFO_CURSOR_X);
  80. iowrite32(y, fifo_mem + SVGA_FIFO_CURSOR_Y);
  81. count = ioread32(fifo_mem + SVGA_FIFO_CURSOR_COUNT);
  82. iowrite32(++count, fifo_mem + SVGA_FIFO_CURSOR_COUNT);
  83. }
  84. int vmw_du_crtc_cursor_set(struct drm_crtc *crtc, struct drm_file *file_priv,
  85. uint32_t handle, uint32_t width, uint32_t height)
  86. {
  87. struct vmw_private *dev_priv = vmw_priv(crtc->dev);
  88. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  89. struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
  90. struct vmw_surface *surface = NULL;
  91. struct vmw_dma_buffer *dmabuf = NULL;
  92. int ret;
  93. if (handle) {
  94. ret = vmw_user_surface_lookup_handle(dev_priv, tfile,
  95. handle, &surface);
  96. if (!ret) {
  97. if (!surface->snooper.image) {
  98. DRM_ERROR("surface not suitable for cursor\n");
  99. return -EINVAL;
  100. }
  101. } else {
  102. ret = vmw_user_dmabuf_lookup(tfile,
  103. handle, &dmabuf);
  104. if (ret) {
  105. DRM_ERROR("failed to find surface or dmabuf: %i\n", ret);
  106. return -EINVAL;
  107. }
  108. }
  109. }
  110. /* takedown old cursor */
  111. if (du->cursor_surface) {
  112. du->cursor_surface->snooper.crtc = NULL;
  113. vmw_surface_unreference(&du->cursor_surface);
  114. }
  115. if (du->cursor_dmabuf)
  116. vmw_dmabuf_unreference(&du->cursor_dmabuf);
  117. /* setup new image */
  118. if (surface) {
  119. /* vmw_user_surface_lookup takes one reference */
  120. du->cursor_surface = surface;
  121. du->cursor_surface->snooper.crtc = crtc;
  122. du->cursor_age = du->cursor_surface->snooper.age;
  123. vmw_cursor_update_image(dev_priv, surface->snooper.image,
  124. 64, 64, du->hotspot_x, du->hotspot_y);
  125. } else if (dmabuf) {
  126. struct ttm_bo_kmap_obj map;
  127. unsigned long kmap_offset;
  128. unsigned long kmap_num;
  129. void *virtual;
  130. bool dummy;
  131. /* vmw_user_surface_lookup takes one reference */
  132. du->cursor_dmabuf = dmabuf;
  133. kmap_offset = 0;
  134. kmap_num = (64*64*4) >> PAGE_SHIFT;
  135. ret = ttm_bo_reserve(&dmabuf->base, true, false, false, 0);
  136. if (unlikely(ret != 0)) {
  137. DRM_ERROR("reserve failed\n");
  138. return -EINVAL;
  139. }
  140. ret = ttm_bo_kmap(&dmabuf->base, kmap_offset, kmap_num, &map);
  141. if (unlikely(ret != 0))
  142. goto err_unreserve;
  143. virtual = ttm_kmap_obj_virtual(&map, &dummy);
  144. vmw_cursor_update_image(dev_priv, virtual, 64, 64,
  145. du->hotspot_x, du->hotspot_y);
  146. ttm_bo_kunmap(&map);
  147. err_unreserve:
  148. ttm_bo_unreserve(&dmabuf->base);
  149. } else {
  150. vmw_cursor_update_position(dev_priv, false, 0, 0);
  151. return 0;
  152. }
  153. vmw_cursor_update_position(dev_priv, true, du->cursor_x, du->cursor_y);
  154. return 0;
  155. }
  156. int vmw_du_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
  157. {
  158. struct vmw_private *dev_priv = vmw_priv(crtc->dev);
  159. struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
  160. bool shown = du->cursor_surface || du->cursor_dmabuf ? true : false;
  161. du->cursor_x = x + crtc->x;
  162. du->cursor_y = y + crtc->y;
  163. vmw_cursor_update_position(dev_priv, shown,
  164. du->cursor_x, du->cursor_y);
  165. return 0;
  166. }
  167. void vmw_kms_cursor_snoop(struct vmw_surface *srf,
  168. struct ttm_object_file *tfile,
  169. struct ttm_buffer_object *bo,
  170. SVGA3dCmdHeader *header)
  171. {
  172. struct ttm_bo_kmap_obj map;
  173. unsigned long kmap_offset;
  174. unsigned long kmap_num;
  175. SVGA3dCopyBox *box;
  176. unsigned box_count;
  177. void *virtual;
  178. bool dummy;
  179. struct vmw_dma_cmd {
  180. SVGA3dCmdHeader header;
  181. SVGA3dCmdSurfaceDMA dma;
  182. } *cmd;
  183. int ret;
  184. cmd = container_of(header, struct vmw_dma_cmd, header);
  185. /* No snooper installed */
  186. if (!srf->snooper.image)
  187. return;
  188. if (cmd->dma.host.face != 0 || cmd->dma.host.mipmap != 0) {
  189. DRM_ERROR("face and mipmap for cursors should never != 0\n");
  190. return;
  191. }
  192. if (cmd->header.size < 64) {
  193. DRM_ERROR("at least one full copy box must be given\n");
  194. return;
  195. }
  196. box = (SVGA3dCopyBox *)&cmd[1];
  197. box_count = (cmd->header.size - sizeof(SVGA3dCmdSurfaceDMA)) /
  198. sizeof(SVGA3dCopyBox);
  199. if (cmd->dma.guest.pitch != (64 * 4) ||
  200. cmd->dma.guest.ptr.offset % PAGE_SIZE ||
  201. box->x != 0 || box->y != 0 || box->z != 0 ||
  202. box->srcx != 0 || box->srcy != 0 || box->srcz != 0 ||
  203. box->w != 64 || box->h != 64 || box->d != 1 ||
  204. box_count != 1) {
  205. /* TODO handle none page aligned offsets */
  206. /* TODO handle partial uploads and pitch != 256 */
  207. /* TODO handle more then one copy (size != 64) */
  208. DRM_ERROR("lazy programmer, can't handle weird stuff\n");
  209. return;
  210. }
  211. kmap_offset = cmd->dma.guest.ptr.offset >> PAGE_SHIFT;
  212. kmap_num = (64*64*4) >> PAGE_SHIFT;
  213. ret = ttm_bo_reserve(bo, true, false, false, 0);
  214. if (unlikely(ret != 0)) {
  215. DRM_ERROR("reserve failed\n");
  216. return;
  217. }
  218. ret = ttm_bo_kmap(bo, kmap_offset, kmap_num, &map);
  219. if (unlikely(ret != 0))
  220. goto err_unreserve;
  221. virtual = ttm_kmap_obj_virtual(&map, &dummy);
  222. memcpy(srf->snooper.image, virtual, 64*64*4);
  223. srf->snooper.age++;
  224. /* we can't call this function from this function since execbuf has
  225. * reserved fifo space.
  226. *
  227. * if (srf->snooper.crtc)
  228. * vmw_ldu_crtc_cursor_update_image(dev_priv,
  229. * srf->snooper.image, 64, 64,
  230. * du->hotspot_x, du->hotspot_y);
  231. */
  232. ttm_bo_kunmap(&map);
  233. err_unreserve:
  234. ttm_bo_unreserve(bo);
  235. }
  236. void vmw_kms_cursor_post_execbuf(struct vmw_private *dev_priv)
  237. {
  238. struct drm_device *dev = dev_priv->dev;
  239. struct vmw_display_unit *du;
  240. struct drm_crtc *crtc;
  241. mutex_lock(&dev->mode_config.mutex);
  242. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  243. du = vmw_crtc_to_du(crtc);
  244. if (!du->cursor_surface ||
  245. du->cursor_age == du->cursor_surface->snooper.age)
  246. continue;
  247. du->cursor_age = du->cursor_surface->snooper.age;
  248. vmw_cursor_update_image(dev_priv,
  249. du->cursor_surface->snooper.image,
  250. 64, 64, du->hotspot_x, du->hotspot_y);
  251. }
  252. mutex_unlock(&dev->mode_config.mutex);
  253. }
  254. /*
  255. * Generic framebuffer code
  256. */
  257. int vmw_framebuffer_create_handle(struct drm_framebuffer *fb,
  258. struct drm_file *file_priv,
  259. unsigned int *handle)
  260. {
  261. if (handle)
  262. handle = 0;
  263. return 0;
  264. }
  265. /*
  266. * Surface framebuffer code
  267. */
  268. #define vmw_framebuffer_to_vfbs(x) \
  269. container_of(x, struct vmw_framebuffer_surface, base.base)
  270. struct vmw_framebuffer_surface {
  271. struct vmw_framebuffer base;
  272. struct vmw_surface *surface;
  273. struct vmw_dma_buffer *buffer;
  274. struct delayed_work d_work;
  275. struct mutex work_lock;
  276. bool present_fs;
  277. struct list_head head;
  278. struct drm_master *master;
  279. };
  280. /**
  281. * vmw_kms_idle_workqueues - Flush workqueues on this master
  282. *
  283. * @vmaster - Pointer identifying the master, for the surfaces of which
  284. * we idle the dirty work queues.
  285. *
  286. * This function should be called with the ttm lock held in exclusive mode
  287. * to idle all dirty work queues before the fifo is taken down.
  288. *
  289. * The work task may actually requeue itself, but after the flush returns we're
  290. * sure that there's nothing to present, since the ttm lock is held in
  291. * exclusive mode, so the fifo will never get used.
  292. */
  293. void vmw_kms_idle_workqueues(struct vmw_master *vmaster)
  294. {
  295. struct vmw_framebuffer_surface *entry;
  296. mutex_lock(&vmaster->fb_surf_mutex);
  297. list_for_each_entry(entry, &vmaster->fb_surf, head) {
  298. if (cancel_delayed_work_sync(&entry->d_work))
  299. (void) entry->d_work.work.func(&entry->d_work.work);
  300. (void) cancel_delayed_work_sync(&entry->d_work);
  301. }
  302. mutex_unlock(&vmaster->fb_surf_mutex);
  303. }
  304. void vmw_framebuffer_surface_destroy(struct drm_framebuffer *framebuffer)
  305. {
  306. struct vmw_framebuffer_surface *vfbs =
  307. vmw_framebuffer_to_vfbs(framebuffer);
  308. struct vmw_master *vmaster = vmw_master(vfbs->master);
  309. mutex_lock(&vmaster->fb_surf_mutex);
  310. list_del(&vfbs->head);
  311. mutex_unlock(&vmaster->fb_surf_mutex);
  312. cancel_delayed_work_sync(&vfbs->d_work);
  313. drm_master_put(&vfbs->master);
  314. drm_framebuffer_cleanup(framebuffer);
  315. vmw_surface_unreference(&vfbs->surface);
  316. kfree(vfbs);
  317. }
  318. static void vmw_framebuffer_present_fs_callback(struct work_struct *work)
  319. {
  320. struct delayed_work *d_work =
  321. container_of(work, struct delayed_work, work);
  322. struct vmw_framebuffer_surface *vfbs =
  323. container_of(d_work, struct vmw_framebuffer_surface, d_work);
  324. struct vmw_surface *surf = vfbs->surface;
  325. struct drm_framebuffer *framebuffer = &vfbs->base.base;
  326. struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
  327. struct {
  328. SVGA3dCmdHeader header;
  329. SVGA3dCmdPresent body;
  330. SVGA3dCopyRect cr;
  331. } *cmd;
  332. /**
  333. * Strictly we should take the ttm_lock in read mode before accessing
  334. * the fifo, to make sure the fifo is present and up. However,
  335. * instead we flush all workqueues under the ttm lock in exclusive mode
  336. * before taking down the fifo.
  337. */
  338. mutex_lock(&vfbs->work_lock);
  339. if (!vfbs->present_fs)
  340. goto out_unlock;
  341. cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
  342. if (unlikely(cmd == NULL))
  343. goto out_resched;
  344. cmd->header.id = cpu_to_le32(SVGA_3D_CMD_PRESENT);
  345. cmd->header.size = cpu_to_le32(sizeof(cmd->body) + sizeof(cmd->cr));
  346. cmd->body.sid = cpu_to_le32(surf->res.id);
  347. cmd->cr.x = cpu_to_le32(0);
  348. cmd->cr.y = cpu_to_le32(0);
  349. cmd->cr.srcx = cmd->cr.x;
  350. cmd->cr.srcy = cmd->cr.y;
  351. cmd->cr.w = cpu_to_le32(framebuffer->width);
  352. cmd->cr.h = cpu_to_le32(framebuffer->height);
  353. vfbs->present_fs = false;
  354. vmw_fifo_commit(dev_priv, sizeof(*cmd));
  355. out_resched:
  356. /**
  357. * Will not re-add if already pending.
  358. */
  359. schedule_delayed_work(&vfbs->d_work, VMWGFX_PRESENT_RATE);
  360. out_unlock:
  361. mutex_unlock(&vfbs->work_lock);
  362. }
  363. int vmw_framebuffer_surface_dirty(struct drm_framebuffer *framebuffer,
  364. struct drm_file *file_priv,
  365. unsigned flags, unsigned color,
  366. struct drm_clip_rect *clips,
  367. unsigned num_clips)
  368. {
  369. struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
  370. struct vmw_master *vmaster = vmw_master(file_priv->master);
  371. struct vmw_framebuffer_surface *vfbs =
  372. vmw_framebuffer_to_vfbs(framebuffer);
  373. struct vmw_surface *surf = vfbs->surface;
  374. struct drm_clip_rect norect;
  375. SVGA3dCopyRect *cr;
  376. int i, inc = 1;
  377. int ret;
  378. struct {
  379. SVGA3dCmdHeader header;
  380. SVGA3dCmdPresent body;
  381. SVGA3dCopyRect cr;
  382. } *cmd;
  383. if (unlikely(vfbs->master != file_priv->master))
  384. return -EINVAL;
  385. ret = ttm_read_lock(&vmaster->lock, true);
  386. if (unlikely(ret != 0))
  387. return ret;
  388. if (!num_clips ||
  389. !(dev_priv->fifo.capabilities &
  390. SVGA_FIFO_CAP_SCREEN_OBJECT)) {
  391. int ret;
  392. mutex_lock(&vfbs->work_lock);
  393. vfbs->present_fs = true;
  394. ret = schedule_delayed_work(&vfbs->d_work, VMWGFX_PRESENT_RATE);
  395. mutex_unlock(&vfbs->work_lock);
  396. if (ret) {
  397. /**
  398. * No work pending, Force immediate present.
  399. */
  400. vmw_framebuffer_present_fs_callback(&vfbs->d_work.work);
  401. }
  402. ttm_read_unlock(&vmaster->lock);
  403. return 0;
  404. }
  405. if (!num_clips) {
  406. num_clips = 1;
  407. clips = &norect;
  408. norect.x1 = norect.y1 = 0;
  409. norect.x2 = framebuffer->width;
  410. norect.y2 = framebuffer->height;
  411. } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
  412. num_clips /= 2;
  413. inc = 2; /* skip source rects */
  414. }
  415. cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd) + (num_clips - 1) * sizeof(cmd->cr));
  416. if (unlikely(cmd == NULL)) {
  417. DRM_ERROR("Fifo reserve failed.\n");
  418. ttm_read_unlock(&vmaster->lock);
  419. return -ENOMEM;
  420. }
  421. memset(cmd, 0, sizeof(*cmd));
  422. cmd->header.id = cpu_to_le32(SVGA_3D_CMD_PRESENT);
  423. cmd->header.size = cpu_to_le32(sizeof(cmd->body) + num_clips * sizeof(cmd->cr));
  424. cmd->body.sid = cpu_to_le32(surf->res.id);
  425. for (i = 0, cr = &cmd->cr; i < num_clips; i++, cr++, clips += inc) {
  426. cr->x = cpu_to_le16(clips->x1);
  427. cr->y = cpu_to_le16(clips->y1);
  428. cr->srcx = cr->x;
  429. cr->srcy = cr->y;
  430. cr->w = cpu_to_le16(clips->x2 - clips->x1);
  431. cr->h = cpu_to_le16(clips->y2 - clips->y1);
  432. }
  433. vmw_fifo_commit(dev_priv, sizeof(*cmd) + (num_clips - 1) * sizeof(cmd->cr));
  434. ttm_read_unlock(&vmaster->lock);
  435. return 0;
  436. }
  437. static struct drm_framebuffer_funcs vmw_framebuffer_surface_funcs = {
  438. .destroy = vmw_framebuffer_surface_destroy,
  439. .dirty = vmw_framebuffer_surface_dirty,
  440. .create_handle = vmw_framebuffer_create_handle,
  441. };
  442. static int vmw_kms_new_framebuffer_surface(struct vmw_private *dev_priv,
  443. struct drm_file *file_priv,
  444. struct vmw_surface *surface,
  445. struct vmw_framebuffer **out,
  446. const struct drm_mode_fb_cmd
  447. *mode_cmd)
  448. {
  449. struct drm_device *dev = dev_priv->dev;
  450. struct vmw_framebuffer_surface *vfbs;
  451. enum SVGA3dSurfaceFormat format;
  452. struct vmw_master *vmaster = vmw_master(file_priv->master);
  453. int ret;
  454. /*
  455. * Sanity checks.
  456. */
  457. if (unlikely(surface->mip_levels[0] != 1 ||
  458. surface->num_sizes != 1 ||
  459. surface->sizes[0].width < mode_cmd->width ||
  460. surface->sizes[0].height < mode_cmd->height ||
  461. surface->sizes[0].depth != 1)) {
  462. DRM_ERROR("Incompatible surface dimensions "
  463. "for requested mode.\n");
  464. return -EINVAL;
  465. }
  466. switch (mode_cmd->depth) {
  467. case 32:
  468. format = SVGA3D_A8R8G8B8;
  469. break;
  470. case 24:
  471. format = SVGA3D_X8R8G8B8;
  472. break;
  473. case 16:
  474. format = SVGA3D_R5G6B5;
  475. break;
  476. case 15:
  477. format = SVGA3D_A1R5G5B5;
  478. break;
  479. case 8:
  480. format = SVGA3D_LUMINANCE8;
  481. break;
  482. default:
  483. DRM_ERROR("Invalid color depth: %d\n", mode_cmd->depth);
  484. return -EINVAL;
  485. }
  486. if (unlikely(format != surface->format)) {
  487. DRM_ERROR("Invalid surface format for requested mode.\n");
  488. return -EINVAL;
  489. }
  490. vfbs = kzalloc(sizeof(*vfbs), GFP_KERNEL);
  491. if (!vfbs) {
  492. ret = -ENOMEM;
  493. goto out_err1;
  494. }
  495. ret = drm_framebuffer_init(dev, &vfbs->base.base,
  496. &vmw_framebuffer_surface_funcs);
  497. if (ret)
  498. goto out_err2;
  499. if (!vmw_surface_reference(surface)) {
  500. DRM_ERROR("failed to reference surface %p\n", surface);
  501. goto out_err3;
  502. }
  503. /* XXX get the first 3 from the surface info */
  504. vfbs->base.base.bits_per_pixel = mode_cmd->bpp;
  505. vfbs->base.base.pitch = mode_cmd->pitch;
  506. vfbs->base.base.depth = mode_cmd->depth;
  507. vfbs->base.base.width = mode_cmd->width;
  508. vfbs->base.base.height = mode_cmd->height;
  509. vfbs->base.pin = &vmw_surface_dmabuf_pin;
  510. vfbs->base.unpin = &vmw_surface_dmabuf_unpin;
  511. vfbs->surface = surface;
  512. vfbs->master = drm_master_get(file_priv->master);
  513. mutex_init(&vfbs->work_lock);
  514. mutex_lock(&vmaster->fb_surf_mutex);
  515. INIT_DELAYED_WORK(&vfbs->d_work, &vmw_framebuffer_present_fs_callback);
  516. list_add_tail(&vfbs->head, &vmaster->fb_surf);
  517. mutex_unlock(&vmaster->fb_surf_mutex);
  518. *out = &vfbs->base;
  519. return 0;
  520. out_err3:
  521. drm_framebuffer_cleanup(&vfbs->base.base);
  522. out_err2:
  523. kfree(vfbs);
  524. out_err1:
  525. return ret;
  526. }
  527. /*
  528. * Dmabuf framebuffer code
  529. */
  530. #define vmw_framebuffer_to_vfbd(x) \
  531. container_of(x, struct vmw_framebuffer_dmabuf, base.base)
  532. struct vmw_framebuffer_dmabuf {
  533. struct vmw_framebuffer base;
  534. struct vmw_dma_buffer *buffer;
  535. };
  536. void vmw_framebuffer_dmabuf_destroy(struct drm_framebuffer *framebuffer)
  537. {
  538. struct vmw_framebuffer_dmabuf *vfbd =
  539. vmw_framebuffer_to_vfbd(framebuffer);
  540. drm_framebuffer_cleanup(framebuffer);
  541. vmw_dmabuf_unreference(&vfbd->buffer);
  542. kfree(vfbd);
  543. }
  544. int vmw_framebuffer_dmabuf_dirty(struct drm_framebuffer *framebuffer,
  545. struct drm_file *file_priv,
  546. unsigned flags, unsigned color,
  547. struct drm_clip_rect *clips,
  548. unsigned num_clips)
  549. {
  550. struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
  551. struct vmw_master *vmaster = vmw_master(file_priv->master);
  552. struct drm_clip_rect norect;
  553. int ret;
  554. struct {
  555. uint32_t header;
  556. SVGAFifoCmdUpdate body;
  557. } *cmd;
  558. int i, increment = 1;
  559. ret = ttm_read_lock(&vmaster->lock, true);
  560. if (unlikely(ret != 0))
  561. return ret;
  562. if (!num_clips) {
  563. num_clips = 1;
  564. clips = &norect;
  565. norect.x1 = norect.y1 = 0;
  566. norect.x2 = framebuffer->width;
  567. norect.y2 = framebuffer->height;
  568. } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
  569. num_clips /= 2;
  570. increment = 2;
  571. }
  572. cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd) * num_clips);
  573. if (unlikely(cmd == NULL)) {
  574. DRM_ERROR("Fifo reserve failed.\n");
  575. ttm_read_unlock(&vmaster->lock);
  576. return -ENOMEM;
  577. }
  578. for (i = 0; i < num_clips; i++, clips += increment) {
  579. cmd[i].header = cpu_to_le32(SVGA_CMD_UPDATE);
  580. cmd[i].body.x = cpu_to_le32(clips->x1);
  581. cmd[i].body.y = cpu_to_le32(clips->y1);
  582. cmd[i].body.width = cpu_to_le32(clips->x2 - clips->x1);
  583. cmd[i].body.height = cpu_to_le32(clips->y2 - clips->y1);
  584. }
  585. vmw_fifo_commit(dev_priv, sizeof(*cmd) * num_clips);
  586. ttm_read_unlock(&vmaster->lock);
  587. return 0;
  588. }
  589. static struct drm_framebuffer_funcs vmw_framebuffer_dmabuf_funcs = {
  590. .destroy = vmw_framebuffer_dmabuf_destroy,
  591. .dirty = vmw_framebuffer_dmabuf_dirty,
  592. .create_handle = vmw_framebuffer_create_handle,
  593. };
  594. /**
  595. * We need to reserve the start of vram because the host might
  596. * scribble to it at mode changes, so we need to reserve it.
  597. */
  598. static int vmw_surface_dmabuf_pin(struct vmw_framebuffer *vfb)
  599. {
  600. struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
  601. struct vmw_framebuffer_surface *vfbs =
  602. vmw_framebuffer_to_vfbs(&vfb->base);
  603. unsigned long size = vfbs->base.base.pitch * vfbs->base.base.height;
  604. int ret;
  605. vfbs->buffer = kzalloc(sizeof(*vfbs->buffer), GFP_KERNEL);
  606. if (unlikely(vfbs->buffer == NULL))
  607. return -ENOMEM;
  608. vmw_overlay_pause_all(dev_priv);
  609. ret = vmw_dmabuf_init(dev_priv, vfbs->buffer, size,
  610. &vmw_vram_ne_placement,
  611. false, &vmw_dmabuf_bo_free);
  612. vmw_overlay_resume_all(dev_priv);
  613. if (unlikely(ret != 0))
  614. vfbs->buffer = NULL;
  615. return ret;
  616. }
  617. /**
  618. * See vmw_surface_dmabuf_pin.
  619. */
  620. static int vmw_surface_dmabuf_unpin(struct vmw_framebuffer *vfb)
  621. {
  622. struct ttm_buffer_object *bo;
  623. struct vmw_framebuffer_surface *vfbs =
  624. vmw_framebuffer_to_vfbs(&vfb->base);
  625. if (unlikely(vfbs->buffer == NULL))
  626. return 0;
  627. bo = &vfbs->buffer->base;
  628. ttm_bo_unref(&bo);
  629. vfbs->buffer = NULL;
  630. return 0;
  631. }
  632. /**
  633. * Pin the dmabuffer to the start of vram.
  634. */
  635. static int vmw_framebuffer_dmabuf_pin(struct vmw_framebuffer *vfb)
  636. {
  637. struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
  638. struct vmw_framebuffer_dmabuf *vfbd =
  639. vmw_framebuffer_to_vfbd(&vfb->base);
  640. int ret;
  641. vmw_overlay_pause_all(dev_priv);
  642. ret = vmw_dmabuf_to_start_of_vram(dev_priv, vfbd->buffer);
  643. vmw_overlay_resume_all(dev_priv);
  644. WARN_ON(ret != 0);
  645. return 0;
  646. }
  647. static int vmw_framebuffer_dmabuf_unpin(struct vmw_framebuffer *vfb)
  648. {
  649. struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
  650. struct vmw_framebuffer_dmabuf *vfbd =
  651. vmw_framebuffer_to_vfbd(&vfb->base);
  652. if (!vfbd->buffer) {
  653. WARN_ON(!vfbd->buffer);
  654. return 0;
  655. }
  656. return vmw_dmabuf_from_vram(dev_priv, vfbd->buffer);
  657. }
  658. static int vmw_kms_new_framebuffer_dmabuf(struct vmw_private *dev_priv,
  659. struct vmw_dma_buffer *dmabuf,
  660. struct vmw_framebuffer **out,
  661. const struct drm_mode_fb_cmd
  662. *mode_cmd)
  663. {
  664. struct drm_device *dev = dev_priv->dev;
  665. struct vmw_framebuffer_dmabuf *vfbd;
  666. unsigned int requested_size;
  667. int ret;
  668. requested_size = mode_cmd->height * mode_cmd->pitch;
  669. if (unlikely(requested_size > dmabuf->base.num_pages * PAGE_SIZE)) {
  670. DRM_ERROR("Screen buffer object size is too small "
  671. "for requested mode.\n");
  672. return -EINVAL;
  673. }
  674. vfbd = kzalloc(sizeof(*vfbd), GFP_KERNEL);
  675. if (!vfbd) {
  676. ret = -ENOMEM;
  677. goto out_err1;
  678. }
  679. ret = drm_framebuffer_init(dev, &vfbd->base.base,
  680. &vmw_framebuffer_dmabuf_funcs);
  681. if (ret)
  682. goto out_err2;
  683. if (!vmw_dmabuf_reference(dmabuf)) {
  684. DRM_ERROR("failed to reference dmabuf %p\n", dmabuf);
  685. goto out_err3;
  686. }
  687. vfbd->base.base.bits_per_pixel = mode_cmd->bpp;
  688. vfbd->base.base.pitch = mode_cmd->pitch;
  689. vfbd->base.base.depth = mode_cmd->depth;
  690. vfbd->base.base.width = mode_cmd->width;
  691. vfbd->base.base.height = mode_cmd->height;
  692. vfbd->base.pin = vmw_framebuffer_dmabuf_pin;
  693. vfbd->base.unpin = vmw_framebuffer_dmabuf_unpin;
  694. vfbd->buffer = dmabuf;
  695. *out = &vfbd->base;
  696. return 0;
  697. out_err3:
  698. drm_framebuffer_cleanup(&vfbd->base.base);
  699. out_err2:
  700. kfree(vfbd);
  701. out_err1:
  702. return ret;
  703. }
  704. /*
  705. * Generic Kernel modesetting functions
  706. */
  707. static struct drm_framebuffer *vmw_kms_fb_create(struct drm_device *dev,
  708. struct drm_file *file_priv,
  709. struct drm_mode_fb_cmd *mode_cmd)
  710. {
  711. struct vmw_private *dev_priv = vmw_priv(dev);
  712. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  713. struct vmw_framebuffer *vfb = NULL;
  714. struct vmw_surface *surface = NULL;
  715. struct vmw_dma_buffer *bo = NULL;
  716. u64 required_size;
  717. int ret;
  718. /**
  719. * This code should be conditioned on Screen Objects not being used.
  720. * If screen objects are used, we can allocate a GMR to hold the
  721. * requested framebuffer.
  722. */
  723. required_size = mode_cmd->pitch * mode_cmd->height;
  724. if (unlikely(required_size > (u64) dev_priv->vram_size)) {
  725. DRM_ERROR("VRAM size is too small for requested mode.\n");
  726. return NULL;
  727. }
  728. /**
  729. * End conditioned code.
  730. */
  731. ret = vmw_user_surface_lookup_handle(dev_priv, tfile,
  732. mode_cmd->handle, &surface);
  733. if (ret)
  734. goto try_dmabuf;
  735. if (!surface->scanout)
  736. goto err_not_scanout;
  737. ret = vmw_kms_new_framebuffer_surface(dev_priv, file_priv, surface,
  738. &vfb, mode_cmd);
  739. /* vmw_user_surface_lookup takes one ref so does new_fb */
  740. vmw_surface_unreference(&surface);
  741. if (ret) {
  742. DRM_ERROR("failed to create vmw_framebuffer: %i\n", ret);
  743. return ERR_PTR(ret);
  744. }
  745. return &vfb->base;
  746. try_dmabuf:
  747. DRM_INFO("%s: trying buffer\n", __func__);
  748. ret = vmw_user_dmabuf_lookup(tfile, mode_cmd->handle, &bo);
  749. if (ret) {
  750. DRM_ERROR("failed to find buffer: %i\n", ret);
  751. return ERR_PTR(-ENOENT);
  752. }
  753. ret = vmw_kms_new_framebuffer_dmabuf(dev_priv, bo, &vfb,
  754. mode_cmd);
  755. /* vmw_user_dmabuf_lookup takes one ref so does new_fb */
  756. vmw_dmabuf_unreference(&bo);
  757. if (ret) {
  758. DRM_ERROR("failed to create vmw_framebuffer: %i\n", ret);
  759. return ERR_PTR(ret);
  760. }
  761. return &vfb->base;
  762. err_not_scanout:
  763. DRM_ERROR("surface not marked as scanout\n");
  764. /* vmw_user_surface_lookup takes one ref */
  765. vmw_surface_unreference(&surface);
  766. return ERR_PTR(-EINVAL);
  767. }
  768. static struct drm_mode_config_funcs vmw_kms_funcs = {
  769. .fb_create = vmw_kms_fb_create,
  770. };
  771. int vmw_kms_init(struct vmw_private *dev_priv)
  772. {
  773. struct drm_device *dev = dev_priv->dev;
  774. int ret;
  775. drm_mode_config_init(dev);
  776. dev->mode_config.funcs = &vmw_kms_funcs;
  777. dev->mode_config.min_width = 1;
  778. dev->mode_config.min_height = 1;
  779. /* assumed largest fb size */
  780. dev->mode_config.max_width = 8192;
  781. dev->mode_config.max_height = 8192;
  782. ret = vmw_kms_init_legacy_display_system(dev_priv);
  783. return 0;
  784. }
  785. int vmw_kms_close(struct vmw_private *dev_priv)
  786. {
  787. /*
  788. * Docs says we should take the lock before calling this function
  789. * but since it destroys encoders and our destructor calls
  790. * drm_encoder_cleanup which takes the lock we deadlock.
  791. */
  792. drm_mode_config_cleanup(dev_priv->dev);
  793. vmw_kms_close_legacy_display_system(dev_priv);
  794. return 0;
  795. }
  796. int vmw_kms_cursor_bypass_ioctl(struct drm_device *dev, void *data,
  797. struct drm_file *file_priv)
  798. {
  799. struct drm_vmw_cursor_bypass_arg *arg = data;
  800. struct vmw_display_unit *du;
  801. struct drm_mode_object *obj;
  802. struct drm_crtc *crtc;
  803. int ret = 0;
  804. mutex_lock(&dev->mode_config.mutex);
  805. if (arg->flags & DRM_VMW_CURSOR_BYPASS_ALL) {
  806. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  807. du = vmw_crtc_to_du(crtc);
  808. du->hotspot_x = arg->xhot;
  809. du->hotspot_y = arg->yhot;
  810. }
  811. mutex_unlock(&dev->mode_config.mutex);
  812. return 0;
  813. }
  814. obj = drm_mode_object_find(dev, arg->crtc_id, DRM_MODE_OBJECT_CRTC);
  815. if (!obj) {
  816. ret = -EINVAL;
  817. goto out;
  818. }
  819. crtc = obj_to_crtc(obj);
  820. du = vmw_crtc_to_du(crtc);
  821. du->hotspot_x = arg->xhot;
  822. du->hotspot_y = arg->yhot;
  823. out:
  824. mutex_unlock(&dev->mode_config.mutex);
  825. return ret;
  826. }
  827. int vmw_kms_write_svga(struct vmw_private *vmw_priv,
  828. unsigned width, unsigned height, unsigned pitch,
  829. unsigned bpp, unsigned depth)
  830. {
  831. if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
  832. vmw_write(vmw_priv, SVGA_REG_PITCHLOCK, pitch);
  833. else if (vmw_fifo_have_pitchlock(vmw_priv))
  834. iowrite32(pitch, vmw_priv->mmio_virt + SVGA_FIFO_PITCHLOCK);
  835. vmw_write(vmw_priv, SVGA_REG_WIDTH, width);
  836. vmw_write(vmw_priv, SVGA_REG_HEIGHT, height);
  837. vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, bpp);
  838. if (vmw_read(vmw_priv, SVGA_REG_DEPTH) != depth) {
  839. DRM_ERROR("Invalid depth %u for %u bpp, host expects %u\n",
  840. depth, bpp, vmw_read(vmw_priv, SVGA_REG_DEPTH));
  841. return -EINVAL;
  842. }
  843. return 0;
  844. }
  845. int vmw_kms_save_vga(struct vmw_private *vmw_priv)
  846. {
  847. struct vmw_vga_topology_state *save;
  848. uint32_t i;
  849. vmw_priv->vga_width = vmw_read(vmw_priv, SVGA_REG_WIDTH);
  850. vmw_priv->vga_height = vmw_read(vmw_priv, SVGA_REG_HEIGHT);
  851. vmw_priv->vga_bpp = vmw_read(vmw_priv, SVGA_REG_BITS_PER_PIXEL);
  852. if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
  853. vmw_priv->vga_pitchlock =
  854. vmw_read(vmw_priv, SVGA_REG_PITCHLOCK);
  855. else if (vmw_fifo_have_pitchlock(vmw_priv))
  856. vmw_priv->vga_pitchlock = ioread32(vmw_priv->mmio_virt +
  857. SVGA_FIFO_PITCHLOCK);
  858. if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
  859. return 0;
  860. vmw_priv->num_displays = vmw_read(vmw_priv,
  861. SVGA_REG_NUM_GUEST_DISPLAYS);
  862. if (vmw_priv->num_displays == 0)
  863. vmw_priv->num_displays = 1;
  864. for (i = 0; i < vmw_priv->num_displays; ++i) {
  865. save = &vmw_priv->vga_save[i];
  866. vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
  867. save->primary = vmw_read(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY);
  868. save->pos_x = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_X);
  869. save->pos_y = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y);
  870. save->width = vmw_read(vmw_priv, SVGA_REG_DISPLAY_WIDTH);
  871. save->height = vmw_read(vmw_priv, SVGA_REG_DISPLAY_HEIGHT);
  872. vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
  873. if (i == 0 && vmw_priv->num_displays == 1 &&
  874. save->width == 0 && save->height == 0) {
  875. /*
  876. * It should be fairly safe to assume that these
  877. * values are uninitialized.
  878. */
  879. save->width = vmw_priv->vga_width - save->pos_x;
  880. save->height = vmw_priv->vga_height - save->pos_y;
  881. }
  882. }
  883. return 0;
  884. }
  885. int vmw_kms_restore_vga(struct vmw_private *vmw_priv)
  886. {
  887. struct vmw_vga_topology_state *save;
  888. uint32_t i;
  889. vmw_write(vmw_priv, SVGA_REG_WIDTH, vmw_priv->vga_width);
  890. vmw_write(vmw_priv, SVGA_REG_HEIGHT, vmw_priv->vga_height);
  891. vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, vmw_priv->vga_bpp);
  892. if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
  893. vmw_write(vmw_priv, SVGA_REG_PITCHLOCK,
  894. vmw_priv->vga_pitchlock);
  895. else if (vmw_fifo_have_pitchlock(vmw_priv))
  896. iowrite32(vmw_priv->vga_pitchlock,
  897. vmw_priv->mmio_virt + SVGA_FIFO_PITCHLOCK);
  898. if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
  899. return 0;
  900. for (i = 0; i < vmw_priv->num_displays; ++i) {
  901. save = &vmw_priv->vga_save[i];
  902. vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
  903. vmw_write(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY, save->primary);
  904. vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_X, save->pos_x);
  905. vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y, save->pos_y);
  906. vmw_write(vmw_priv, SVGA_REG_DISPLAY_WIDTH, save->width);
  907. vmw_write(vmw_priv, SVGA_REG_DISPLAY_HEIGHT, save->height);
  908. vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
  909. }
  910. return 0;
  911. }
  912. bool vmw_kms_validate_mode_vram(struct vmw_private *dev_priv,
  913. uint32_t pitch,
  914. uint32_t height)
  915. {
  916. return ((u64) pitch * (u64) height) < (u64) dev_priv->vram_size;
  917. }
  918. u32 vmw_get_vblank_counter(struct drm_device *dev, int crtc)
  919. {
  920. return 0;
  921. }