vmwgfx_overlay.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  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 "drmP.h"
  28. #include "vmwgfx_drv.h"
  29. #include "ttm/ttm_placement.h"
  30. #include "svga_overlay.h"
  31. #include "svga_escape.h"
  32. #define VMW_MAX_NUM_STREAMS 1
  33. struct vmw_stream {
  34. struct vmw_dma_buffer *buf;
  35. bool claimed;
  36. bool paused;
  37. struct drm_vmw_control_stream_arg saved;
  38. };
  39. /**
  40. * Overlay control
  41. */
  42. struct vmw_overlay {
  43. /*
  44. * Each stream is a single overlay. In Xv these are called ports.
  45. */
  46. struct mutex mutex;
  47. struct vmw_stream stream[VMW_MAX_NUM_STREAMS];
  48. };
  49. static inline struct vmw_overlay *vmw_overlay(struct drm_device *dev)
  50. {
  51. struct vmw_private *dev_priv = vmw_priv(dev);
  52. return dev_priv ? dev_priv->overlay_priv : NULL;
  53. }
  54. struct vmw_escape_header {
  55. uint32_t cmd;
  56. SVGAFifoCmdEscape body;
  57. };
  58. struct vmw_escape_video_flush {
  59. struct vmw_escape_header escape;
  60. SVGAEscapeVideoFlush flush;
  61. };
  62. static inline void fill_escape(struct vmw_escape_header *header,
  63. uint32_t size)
  64. {
  65. header->cmd = SVGA_CMD_ESCAPE;
  66. header->body.nsid = SVGA_ESCAPE_NSID_VMWARE;
  67. header->body.size = size;
  68. }
  69. static inline void fill_flush(struct vmw_escape_video_flush *cmd,
  70. uint32_t stream_id)
  71. {
  72. fill_escape(&cmd->escape, sizeof(cmd->flush));
  73. cmd->flush.cmdType = SVGA_ESCAPE_VMWARE_VIDEO_FLUSH;
  74. cmd->flush.streamId = stream_id;
  75. }
  76. /**
  77. * Send put command to hw.
  78. *
  79. * Returns
  80. * -ERESTARTSYS if interrupted by a signal.
  81. */
  82. static int vmw_overlay_send_put(struct vmw_private *dev_priv,
  83. struct vmw_dma_buffer *buf,
  84. struct drm_vmw_control_stream_arg *arg,
  85. bool interruptible)
  86. {
  87. struct vmw_escape_video_flush *flush;
  88. size_t fifo_size;
  89. bool have_so = dev_priv->sou_priv ? true : false;
  90. int i, num_items;
  91. SVGAGuestPtr ptr;
  92. struct {
  93. struct vmw_escape_header escape;
  94. struct {
  95. uint32_t cmdType;
  96. uint32_t streamId;
  97. } header;
  98. } *cmds;
  99. struct {
  100. uint32_t registerId;
  101. uint32_t value;
  102. } *items;
  103. /* defines are a index needs + 1 */
  104. if (have_so)
  105. num_items = SVGA_VIDEO_DST_SCREEN_ID + 1;
  106. else
  107. num_items = SVGA_VIDEO_PITCH_3 + 1;
  108. fifo_size = sizeof(*cmds) + sizeof(*flush) + sizeof(*items) * num_items;
  109. cmds = vmw_fifo_reserve(dev_priv, fifo_size);
  110. /* hardware has hung, can't do anything here */
  111. if (!cmds)
  112. return -ENOMEM;
  113. items = (typeof(items))&cmds[1];
  114. flush = (struct vmw_escape_video_flush *)&items[num_items];
  115. /* the size is header + number of items */
  116. fill_escape(&cmds->escape, sizeof(*items) * (num_items + 1));
  117. cmds->header.cmdType = SVGA_ESCAPE_VMWARE_VIDEO_SET_REGS;
  118. cmds->header.streamId = arg->stream_id;
  119. /* the IDs are neatly numbered */
  120. for (i = 0; i < num_items; i++)
  121. items[i].registerId = i;
  122. vmw_bo_get_guest_ptr(&buf->base, &ptr);
  123. ptr.offset += arg->offset;
  124. items[SVGA_VIDEO_ENABLED].value = true;
  125. items[SVGA_VIDEO_FLAGS].value = arg->flags;
  126. items[SVGA_VIDEO_DATA_OFFSET].value = ptr.offset;
  127. items[SVGA_VIDEO_FORMAT].value = arg->format;
  128. items[SVGA_VIDEO_COLORKEY].value = arg->color_key;
  129. items[SVGA_VIDEO_SIZE].value = arg->size;
  130. items[SVGA_VIDEO_WIDTH].value = arg->width;
  131. items[SVGA_VIDEO_HEIGHT].value = arg->height;
  132. items[SVGA_VIDEO_SRC_X].value = arg->src.x;
  133. items[SVGA_VIDEO_SRC_Y].value = arg->src.y;
  134. items[SVGA_VIDEO_SRC_WIDTH].value = arg->src.w;
  135. items[SVGA_VIDEO_SRC_HEIGHT].value = arg->src.h;
  136. items[SVGA_VIDEO_DST_X].value = arg->dst.x;
  137. items[SVGA_VIDEO_DST_Y].value = arg->dst.y;
  138. items[SVGA_VIDEO_DST_WIDTH].value = arg->dst.w;
  139. items[SVGA_VIDEO_DST_HEIGHT].value = arg->dst.h;
  140. items[SVGA_VIDEO_PITCH_1].value = arg->pitch[0];
  141. items[SVGA_VIDEO_PITCH_2].value = arg->pitch[1];
  142. items[SVGA_VIDEO_PITCH_3].value = arg->pitch[2];
  143. if (have_so) {
  144. items[SVGA_VIDEO_DATA_GMRID].value = ptr.gmrId;
  145. items[SVGA_VIDEO_DST_SCREEN_ID].value = SVGA_ID_INVALID;
  146. }
  147. fill_flush(flush, arg->stream_id);
  148. vmw_fifo_commit(dev_priv, fifo_size);
  149. return 0;
  150. }
  151. /**
  152. * Send stop command to hw.
  153. *
  154. * Returns
  155. * -ERESTARTSYS if interrupted by a signal.
  156. */
  157. static int vmw_overlay_send_stop(struct vmw_private *dev_priv,
  158. uint32_t stream_id,
  159. bool interruptible)
  160. {
  161. struct {
  162. struct vmw_escape_header escape;
  163. SVGAEscapeVideoSetRegs body;
  164. struct vmw_escape_video_flush flush;
  165. } *cmds;
  166. int ret;
  167. for (;;) {
  168. cmds = vmw_fifo_reserve(dev_priv, sizeof(*cmds));
  169. if (cmds)
  170. break;
  171. ret = vmw_fallback_wait(dev_priv, false, true, 0,
  172. interruptible, 3*HZ);
  173. if (interruptible && ret == -ERESTARTSYS)
  174. return ret;
  175. else
  176. BUG_ON(ret != 0);
  177. }
  178. fill_escape(&cmds->escape, sizeof(cmds->body));
  179. cmds->body.header.cmdType = SVGA_ESCAPE_VMWARE_VIDEO_SET_REGS;
  180. cmds->body.header.streamId = stream_id;
  181. cmds->body.items[0].registerId = SVGA_VIDEO_ENABLED;
  182. cmds->body.items[0].value = false;
  183. fill_flush(&cmds->flush, stream_id);
  184. vmw_fifo_commit(dev_priv, sizeof(*cmds));
  185. return 0;
  186. }
  187. /**
  188. * Move a buffer to vram or gmr if @pin is set, else unpin the buffer.
  189. *
  190. * With the introduction of screen objects buffers could now be
  191. * used with GMRs instead of being locked to vram.
  192. */
  193. static int vmw_overlay_move_buffer(struct vmw_private *dev_priv,
  194. struct vmw_dma_buffer *buf,
  195. bool pin, bool inter)
  196. {
  197. if (!pin)
  198. return vmw_dmabuf_unpin(dev_priv, buf, inter);
  199. if (!dev_priv->sou_priv)
  200. return vmw_dmabuf_to_vram(dev_priv, buf, true, inter);
  201. return vmw_dmabuf_to_vram_or_gmr(dev_priv, buf, true, inter);
  202. }
  203. /**
  204. * Stop or pause a stream.
  205. *
  206. * If the stream is paused the no evict flag is removed from the buffer
  207. * but left in vram. This allows for instance mode_set to evict it
  208. * should it need to.
  209. *
  210. * The caller must hold the overlay lock.
  211. *
  212. * @stream_id which stream to stop/pause.
  213. * @pause true to pause, false to stop completely.
  214. */
  215. static int vmw_overlay_stop(struct vmw_private *dev_priv,
  216. uint32_t stream_id, bool pause,
  217. bool interruptible)
  218. {
  219. struct vmw_overlay *overlay = dev_priv->overlay_priv;
  220. struct vmw_stream *stream = &overlay->stream[stream_id];
  221. int ret;
  222. /* no buffer attached the stream is completely stopped */
  223. if (!stream->buf)
  224. return 0;
  225. /* If the stream is paused this is already done */
  226. if (!stream->paused) {
  227. ret = vmw_overlay_send_stop(dev_priv, stream_id,
  228. interruptible);
  229. if (ret)
  230. return ret;
  231. /* We just remove the NO_EVICT flag so no -ENOMEM */
  232. ret = vmw_overlay_move_buffer(dev_priv, stream->buf, false,
  233. interruptible);
  234. if (interruptible && ret == -ERESTARTSYS)
  235. return ret;
  236. else
  237. BUG_ON(ret != 0);
  238. }
  239. if (!pause) {
  240. vmw_dmabuf_unreference(&stream->buf);
  241. stream->paused = false;
  242. } else {
  243. stream->paused = true;
  244. }
  245. return 0;
  246. }
  247. /**
  248. * Update a stream and send any put or stop fifo commands needed.
  249. *
  250. * The caller must hold the overlay lock.
  251. *
  252. * Returns
  253. * -ENOMEM if buffer doesn't fit in vram.
  254. * -ERESTARTSYS if interrupted.
  255. */
  256. static int vmw_overlay_update_stream(struct vmw_private *dev_priv,
  257. struct vmw_dma_buffer *buf,
  258. struct drm_vmw_control_stream_arg *arg,
  259. bool interruptible)
  260. {
  261. struct vmw_overlay *overlay = dev_priv->overlay_priv;
  262. struct vmw_stream *stream = &overlay->stream[arg->stream_id];
  263. int ret = 0;
  264. if (!buf)
  265. return -EINVAL;
  266. DRM_DEBUG(" %s: old %p, new %p, %spaused\n", __func__,
  267. stream->buf, buf, stream->paused ? "" : "not ");
  268. if (stream->buf != buf) {
  269. ret = vmw_overlay_stop(dev_priv, arg->stream_id,
  270. false, interruptible);
  271. if (ret)
  272. return ret;
  273. } else if (!stream->paused) {
  274. /* If the buffers match and not paused then just send
  275. * the put command, no need to do anything else.
  276. */
  277. ret = vmw_overlay_send_put(dev_priv, buf, arg, interruptible);
  278. if (ret == 0)
  279. stream->saved = *arg;
  280. else
  281. BUG_ON(!interruptible);
  282. return ret;
  283. }
  284. /* We don't start the old stream if we are interrupted.
  285. * Might return -ENOMEM if it can't fit the buffer in vram.
  286. */
  287. ret = vmw_overlay_move_buffer(dev_priv, buf, true, interruptible);
  288. if (ret)
  289. return ret;
  290. ret = vmw_overlay_send_put(dev_priv, buf, arg, interruptible);
  291. if (ret) {
  292. /* This one needs to happen no matter what. We only remove
  293. * the NO_EVICT flag so this is safe from -ENOMEM.
  294. */
  295. BUG_ON(vmw_overlay_move_buffer(dev_priv, buf, false, false)
  296. != 0);
  297. return ret;
  298. }
  299. if (stream->buf != buf)
  300. stream->buf = vmw_dmabuf_reference(buf);
  301. stream->saved = *arg;
  302. /* stream is no longer stopped/paused */
  303. stream->paused = false;
  304. return 0;
  305. }
  306. /**
  307. * Stop all streams.
  308. *
  309. * Used by the fb code when starting.
  310. *
  311. * Takes the overlay lock.
  312. */
  313. int vmw_overlay_stop_all(struct vmw_private *dev_priv)
  314. {
  315. struct vmw_overlay *overlay = dev_priv->overlay_priv;
  316. int i, ret;
  317. if (!overlay)
  318. return 0;
  319. mutex_lock(&overlay->mutex);
  320. for (i = 0; i < VMW_MAX_NUM_STREAMS; i++) {
  321. struct vmw_stream *stream = &overlay->stream[i];
  322. if (!stream->buf)
  323. continue;
  324. ret = vmw_overlay_stop(dev_priv, i, false, false);
  325. WARN_ON(ret != 0);
  326. }
  327. mutex_unlock(&overlay->mutex);
  328. return 0;
  329. }
  330. /**
  331. * Try to resume all paused streams.
  332. *
  333. * Used by the kms code after moving a new scanout buffer to vram.
  334. *
  335. * Takes the overlay lock.
  336. */
  337. int vmw_overlay_resume_all(struct vmw_private *dev_priv)
  338. {
  339. struct vmw_overlay *overlay = dev_priv->overlay_priv;
  340. int i, ret;
  341. if (!overlay)
  342. return 0;
  343. mutex_lock(&overlay->mutex);
  344. for (i = 0; i < VMW_MAX_NUM_STREAMS; i++) {
  345. struct vmw_stream *stream = &overlay->stream[i];
  346. if (!stream->paused)
  347. continue;
  348. ret = vmw_overlay_update_stream(dev_priv, stream->buf,
  349. &stream->saved, false);
  350. if (ret != 0)
  351. DRM_INFO("%s: *warning* failed to resume stream %i\n",
  352. __func__, i);
  353. }
  354. mutex_unlock(&overlay->mutex);
  355. return 0;
  356. }
  357. /**
  358. * Pauses all active streams.
  359. *
  360. * Used by the kms code when moving a new scanout buffer to vram.
  361. *
  362. * Takes the overlay lock.
  363. */
  364. int vmw_overlay_pause_all(struct vmw_private *dev_priv)
  365. {
  366. struct vmw_overlay *overlay = dev_priv->overlay_priv;
  367. int i, ret;
  368. if (!overlay)
  369. return 0;
  370. mutex_lock(&overlay->mutex);
  371. for (i = 0; i < VMW_MAX_NUM_STREAMS; i++) {
  372. if (overlay->stream[i].paused)
  373. DRM_INFO("%s: *warning* stream %i already paused\n",
  374. __func__, i);
  375. ret = vmw_overlay_stop(dev_priv, i, true, false);
  376. WARN_ON(ret != 0);
  377. }
  378. mutex_unlock(&overlay->mutex);
  379. return 0;
  380. }
  381. int vmw_overlay_ioctl(struct drm_device *dev, void *data,
  382. struct drm_file *file_priv)
  383. {
  384. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  385. struct vmw_private *dev_priv = vmw_priv(dev);
  386. struct vmw_overlay *overlay = dev_priv->overlay_priv;
  387. struct drm_vmw_control_stream_arg *arg =
  388. (struct drm_vmw_control_stream_arg *)data;
  389. struct vmw_dma_buffer *buf;
  390. struct vmw_resource *res;
  391. int ret;
  392. if (!overlay)
  393. return -ENOSYS;
  394. ret = vmw_user_stream_lookup(dev_priv, tfile, &arg->stream_id, &res);
  395. if (ret)
  396. return ret;
  397. mutex_lock(&overlay->mutex);
  398. if (!arg->enabled) {
  399. ret = vmw_overlay_stop(dev_priv, arg->stream_id, false, true);
  400. goto out_unlock;
  401. }
  402. ret = vmw_user_dmabuf_lookup(tfile, arg->handle, &buf);
  403. if (ret)
  404. goto out_unlock;
  405. ret = vmw_overlay_update_stream(dev_priv, buf, arg, true);
  406. vmw_dmabuf_unreference(&buf);
  407. out_unlock:
  408. mutex_unlock(&overlay->mutex);
  409. vmw_resource_unreference(&res);
  410. return ret;
  411. }
  412. int vmw_overlay_num_overlays(struct vmw_private *dev_priv)
  413. {
  414. if (!dev_priv->overlay_priv)
  415. return 0;
  416. return VMW_MAX_NUM_STREAMS;
  417. }
  418. int vmw_overlay_num_free_overlays(struct vmw_private *dev_priv)
  419. {
  420. struct vmw_overlay *overlay = dev_priv->overlay_priv;
  421. int i, k;
  422. if (!overlay)
  423. return 0;
  424. mutex_lock(&overlay->mutex);
  425. for (i = 0, k = 0; i < VMW_MAX_NUM_STREAMS; i++)
  426. if (!overlay->stream[i].claimed)
  427. k++;
  428. mutex_unlock(&overlay->mutex);
  429. return k;
  430. }
  431. int vmw_overlay_claim(struct vmw_private *dev_priv, uint32_t *out)
  432. {
  433. struct vmw_overlay *overlay = dev_priv->overlay_priv;
  434. int i;
  435. if (!overlay)
  436. return -ENOSYS;
  437. mutex_lock(&overlay->mutex);
  438. for (i = 0; i < VMW_MAX_NUM_STREAMS; i++) {
  439. if (overlay->stream[i].claimed)
  440. continue;
  441. overlay->stream[i].claimed = true;
  442. *out = i;
  443. mutex_unlock(&overlay->mutex);
  444. return 0;
  445. }
  446. mutex_unlock(&overlay->mutex);
  447. return -ESRCH;
  448. }
  449. int vmw_overlay_unref(struct vmw_private *dev_priv, uint32_t stream_id)
  450. {
  451. struct vmw_overlay *overlay = dev_priv->overlay_priv;
  452. BUG_ON(stream_id >= VMW_MAX_NUM_STREAMS);
  453. if (!overlay)
  454. return -ENOSYS;
  455. mutex_lock(&overlay->mutex);
  456. WARN_ON(!overlay->stream[stream_id].claimed);
  457. vmw_overlay_stop(dev_priv, stream_id, false, false);
  458. overlay->stream[stream_id].claimed = false;
  459. mutex_unlock(&overlay->mutex);
  460. return 0;
  461. }
  462. int vmw_overlay_init(struct vmw_private *dev_priv)
  463. {
  464. struct vmw_overlay *overlay;
  465. int i;
  466. if (dev_priv->overlay_priv)
  467. return -EINVAL;
  468. if (!(dev_priv->fifo.capabilities & SVGA_FIFO_CAP_VIDEO) &&
  469. (dev_priv->fifo.capabilities & SVGA_FIFO_CAP_ESCAPE)) {
  470. DRM_INFO("hardware doesn't support overlays\n");
  471. return -ENOSYS;
  472. }
  473. overlay = kzalloc(sizeof(*overlay), GFP_KERNEL);
  474. if (!overlay)
  475. return -ENOMEM;
  476. mutex_init(&overlay->mutex);
  477. for (i = 0; i < VMW_MAX_NUM_STREAMS; i++) {
  478. overlay->stream[i].buf = NULL;
  479. overlay->stream[i].paused = false;
  480. overlay->stream[i].claimed = false;
  481. }
  482. dev_priv->overlay_priv = overlay;
  483. return 0;
  484. }
  485. int vmw_overlay_close(struct vmw_private *dev_priv)
  486. {
  487. struct vmw_overlay *overlay = dev_priv->overlay_priv;
  488. bool forgotten_buffer = false;
  489. int i;
  490. if (!overlay)
  491. return -ENOSYS;
  492. for (i = 0; i < VMW_MAX_NUM_STREAMS; i++) {
  493. if (overlay->stream[i].buf) {
  494. forgotten_buffer = true;
  495. vmw_overlay_stop(dev_priv, i, false, false);
  496. }
  497. }
  498. WARN_ON(forgotten_buffer);
  499. dev_priv->overlay_priv = NULL;
  500. kfree(overlay);
  501. return 0;
  502. }