vmwgfx_overlay.c 14 KB

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