vmwgfx_overlay.c 14 KB

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