vmwgfx_overlay.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. // SPDX-License-Identifier: GPL-2.0 OR MIT
  2. /**************************************************************************
  3. *
  4. * Copyright 2009-2014 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 <drm/ttm/ttm_placement.h>
  28. #include "device_include/svga_overlay.h"
  29. #include "device_include/svga_escape.h"
  30. #include "vmwgfx_drv.h"
  31. #define VMW_MAX_NUM_STREAMS 1
  32. #define VMW_OVERLAY_CAP_MASK (SVGA_FIFO_CAP_VIDEO | SVGA_FIFO_CAP_ESCAPE)
  33. struct vmw_stream {
  34. struct vmw_buffer_object *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_buffer_object *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->active_display_unit == vmw_du_screen_object);
  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_buffer_object *buf,
  195. bool pin, bool inter)
  196. {
  197. if (!pin)
  198. return vmw_bo_unpin(dev_priv, buf, inter);
  199. if (dev_priv->active_display_unit == vmw_du_legacy)
  200. return vmw_bo_pin_in_vram(dev_priv, buf, inter);
  201. return vmw_bo_pin_in_vram_or_gmr(dev_priv, buf, 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_bo_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_buffer_object *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_bo_reference(buf);
  301. stream->saved = *arg;
  302. /* stream is no longer stopped/paused */
  303. stream->paused = false;
  304. return 0;
  305. }
  306. /**
  307. * Try to resume all paused streams.
  308. *
  309. * Used by the kms code after moving a new scanout buffer to vram.
  310. *
  311. * Takes the overlay lock.
  312. */
  313. int vmw_overlay_resume_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->paused)
  323. continue;
  324. ret = vmw_overlay_update_stream(dev_priv, stream->buf,
  325. &stream->saved, false);
  326. if (ret != 0)
  327. DRM_INFO("%s: *warning* failed to resume stream %i\n",
  328. __func__, i);
  329. }
  330. mutex_unlock(&overlay->mutex);
  331. return 0;
  332. }
  333. /**
  334. * Pauses all active streams.
  335. *
  336. * Used by the kms code when moving a new scanout buffer to vram.
  337. *
  338. * Takes the overlay lock.
  339. */
  340. int vmw_overlay_pause_all(struct vmw_private *dev_priv)
  341. {
  342. struct vmw_overlay *overlay = dev_priv->overlay_priv;
  343. int i, ret;
  344. if (!overlay)
  345. return 0;
  346. mutex_lock(&overlay->mutex);
  347. for (i = 0; i < VMW_MAX_NUM_STREAMS; i++) {
  348. if (overlay->stream[i].paused)
  349. DRM_INFO("%s: *warning* stream %i already paused\n",
  350. __func__, i);
  351. ret = vmw_overlay_stop(dev_priv, i, true, false);
  352. WARN_ON(ret != 0);
  353. }
  354. mutex_unlock(&overlay->mutex);
  355. return 0;
  356. }
  357. static bool vmw_overlay_available(const struct vmw_private *dev_priv)
  358. {
  359. return (dev_priv->overlay_priv != NULL &&
  360. ((dev_priv->fifo.capabilities & VMW_OVERLAY_CAP_MASK) ==
  361. VMW_OVERLAY_CAP_MASK));
  362. }
  363. int vmw_overlay_ioctl(struct drm_device *dev, void *data,
  364. struct drm_file *file_priv)
  365. {
  366. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  367. struct vmw_private *dev_priv = vmw_priv(dev);
  368. struct vmw_overlay *overlay = dev_priv->overlay_priv;
  369. struct drm_vmw_control_stream_arg *arg =
  370. (struct drm_vmw_control_stream_arg *)data;
  371. struct vmw_buffer_object *buf;
  372. struct vmw_resource *res;
  373. int ret;
  374. if (!vmw_overlay_available(dev_priv))
  375. return -ENOSYS;
  376. ret = vmw_user_stream_lookup(dev_priv, tfile, &arg->stream_id, &res);
  377. if (ret)
  378. return ret;
  379. mutex_lock(&overlay->mutex);
  380. if (!arg->enabled) {
  381. ret = vmw_overlay_stop(dev_priv, arg->stream_id, false, true);
  382. goto out_unlock;
  383. }
  384. ret = vmw_user_bo_lookup(tfile, arg->handle, &buf, NULL);
  385. if (ret)
  386. goto out_unlock;
  387. ret = vmw_overlay_update_stream(dev_priv, buf, arg, true);
  388. vmw_bo_unreference(&buf);
  389. out_unlock:
  390. mutex_unlock(&overlay->mutex);
  391. vmw_resource_unreference(&res);
  392. return ret;
  393. }
  394. int vmw_overlay_num_overlays(struct vmw_private *dev_priv)
  395. {
  396. if (!vmw_overlay_available(dev_priv))
  397. return 0;
  398. return VMW_MAX_NUM_STREAMS;
  399. }
  400. int vmw_overlay_num_free_overlays(struct vmw_private *dev_priv)
  401. {
  402. struct vmw_overlay *overlay = dev_priv->overlay_priv;
  403. int i, k;
  404. if (!vmw_overlay_available(dev_priv))
  405. return 0;
  406. mutex_lock(&overlay->mutex);
  407. for (i = 0, k = 0; i < VMW_MAX_NUM_STREAMS; i++)
  408. if (!overlay->stream[i].claimed)
  409. k++;
  410. mutex_unlock(&overlay->mutex);
  411. return k;
  412. }
  413. int vmw_overlay_claim(struct vmw_private *dev_priv, uint32_t *out)
  414. {
  415. struct vmw_overlay *overlay = dev_priv->overlay_priv;
  416. int i;
  417. if (!overlay)
  418. return -ENOSYS;
  419. mutex_lock(&overlay->mutex);
  420. for (i = 0; i < VMW_MAX_NUM_STREAMS; i++) {
  421. if (overlay->stream[i].claimed)
  422. continue;
  423. overlay->stream[i].claimed = true;
  424. *out = i;
  425. mutex_unlock(&overlay->mutex);
  426. return 0;
  427. }
  428. mutex_unlock(&overlay->mutex);
  429. return -ESRCH;
  430. }
  431. int vmw_overlay_unref(struct vmw_private *dev_priv, uint32_t stream_id)
  432. {
  433. struct vmw_overlay *overlay = dev_priv->overlay_priv;
  434. BUG_ON(stream_id >= VMW_MAX_NUM_STREAMS);
  435. if (!overlay)
  436. return -ENOSYS;
  437. mutex_lock(&overlay->mutex);
  438. WARN_ON(!overlay->stream[stream_id].claimed);
  439. vmw_overlay_stop(dev_priv, stream_id, false, false);
  440. overlay->stream[stream_id].claimed = false;
  441. mutex_unlock(&overlay->mutex);
  442. return 0;
  443. }
  444. int vmw_overlay_init(struct vmw_private *dev_priv)
  445. {
  446. struct vmw_overlay *overlay;
  447. int i;
  448. if (dev_priv->overlay_priv)
  449. return -EINVAL;
  450. overlay = kzalloc(sizeof(*overlay), GFP_KERNEL);
  451. if (!overlay)
  452. return -ENOMEM;
  453. mutex_init(&overlay->mutex);
  454. for (i = 0; i < VMW_MAX_NUM_STREAMS; i++) {
  455. overlay->stream[i].buf = NULL;
  456. overlay->stream[i].paused = false;
  457. overlay->stream[i].claimed = false;
  458. }
  459. dev_priv->overlay_priv = overlay;
  460. return 0;
  461. }
  462. int vmw_overlay_close(struct vmw_private *dev_priv)
  463. {
  464. struct vmw_overlay *overlay = dev_priv->overlay_priv;
  465. bool forgotten_buffer = false;
  466. int i;
  467. if (!overlay)
  468. return -ENOSYS;
  469. for (i = 0; i < VMW_MAX_NUM_STREAMS; i++) {
  470. if (overlay->stream[i].buf) {
  471. forgotten_buffer = true;
  472. vmw_overlay_stop(dev_priv, i, false, false);
  473. }
  474. }
  475. WARN_ON(forgotten_buffer);
  476. dev_priv->overlay_priv = NULL;
  477. kfree(overlay);
  478. return 0;
  479. }