vmwgfx_vkms.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. // SPDX-License-Identifier: GPL-2.0 OR MIT
  2. /**************************************************************************
  3. *
  4. * Copyright (c) 2024 Broadcom. All Rights Reserved. The term
  5. * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a
  8. * copy of this software and associated documentation files (the
  9. * "Software"), to deal in the Software without restriction, including
  10. * without limitation the rights to use, copy, modify, merge, publish,
  11. * distribute, sub license, and/or sell copies of the Software, and to
  12. * permit persons to whom the Software is furnished to do so, subject to
  13. * the following conditions:
  14. *
  15. * The above copyright notice and this permission notice (including the
  16. * next paragraph) shall be included in all copies or substantial portions
  17. * of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  22. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  23. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  24. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  25. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. *
  27. **************************************************************************/
  28. #include "vmwgfx_vkms.h"
  29. #include "vmwgfx_bo.h"
  30. #include "vmwgfx_drv.h"
  31. #include "vmwgfx_kms.h"
  32. #include "vmw_surface_cache.h"
  33. #include <drm/drm_crtc.h>
  34. #include <drm/drm_debugfs_crc.h>
  35. #include <drm/drm_print.h>
  36. #include <drm/drm_vblank.h>
  37. #include <linux/crc32.h>
  38. #include <linux/delay.h>
  39. #define GUESTINFO_VBLANK "guestinfo.vmwgfx.vkms_enable"
  40. static int
  41. vmw_surface_sync(struct vmw_private *vmw,
  42. struct vmw_surface *surf)
  43. {
  44. int ret;
  45. struct vmw_fence_obj *fence = NULL;
  46. struct vmw_bo *bo = surf->res.guest_memory_bo;
  47. vmw_resource_clean(&surf->res);
  48. ret = ttm_bo_reserve(&bo->tbo, false, false, NULL);
  49. if (ret != 0) {
  50. drm_warn(&vmw->drm, "%s: failed reserve\n", __func__);
  51. goto done;
  52. }
  53. ret = vmw_execbuf_fence_commands(NULL, vmw, &fence, NULL);
  54. if (ret != 0) {
  55. drm_warn(&vmw->drm, "%s: failed execbuf\n", __func__);
  56. ttm_bo_unreserve(&bo->tbo);
  57. goto done;
  58. }
  59. dma_fence_wait(&fence->base, false);
  60. dma_fence_put(&fence->base);
  61. ttm_bo_unreserve(&bo->tbo);
  62. done:
  63. return ret;
  64. }
  65. static void
  66. compute_crc(struct drm_crtc *crtc,
  67. struct vmw_surface *surf,
  68. u32 *crc)
  69. {
  70. u8 *mapped_surface;
  71. struct vmw_bo *bo = surf->res.guest_memory_bo;
  72. const struct SVGA3dSurfaceDesc *desc =
  73. vmw_surface_get_desc(surf->metadata.format);
  74. u32 row_pitch_bytes;
  75. SVGA3dSize blocks;
  76. u32 y;
  77. *crc = 0;
  78. vmw_surface_get_size_in_blocks(desc, &surf->metadata.base_size, &blocks);
  79. row_pitch_bytes = blocks.width * desc->pitchBytesPerBlock;
  80. WARN_ON(!bo);
  81. mapped_surface = vmw_bo_map_and_cache(bo);
  82. for (y = 0; y < blocks.height; y++) {
  83. *crc = crc32_le(*crc, mapped_surface, row_pitch_bytes);
  84. mapped_surface += row_pitch_bytes;
  85. }
  86. vmw_bo_unmap(bo);
  87. }
  88. static void
  89. crc_generate_worker(struct work_struct *work)
  90. {
  91. struct vmw_display_unit *du =
  92. container_of(work, struct vmw_display_unit, vkms.crc_generator_work);
  93. struct drm_crtc *crtc = &du->crtc;
  94. struct vmw_private *vmw = vmw_priv(crtc->dev);
  95. bool crc_pending;
  96. u64 frame_start, frame_end;
  97. u32 crc32 = 0;
  98. struct vmw_surface *surf = 0;
  99. spin_lock_irq(&du->vkms.crc_state_lock);
  100. crc_pending = du->vkms.crc_pending;
  101. spin_unlock_irq(&du->vkms.crc_state_lock);
  102. /*
  103. * We raced with the vblank hrtimer and previous work already computed
  104. * the crc, nothing to do.
  105. */
  106. if (!crc_pending)
  107. return;
  108. spin_lock_irq(&du->vkms.crc_state_lock);
  109. surf = vmw_surface_reference(du->vkms.surface);
  110. spin_unlock_irq(&du->vkms.crc_state_lock);
  111. if (surf) {
  112. if (vmw_surface_sync(vmw, surf)) {
  113. drm_warn(
  114. crtc->dev,
  115. "CRC worker wasn't able to sync the crc surface!\n");
  116. return;
  117. }
  118. compute_crc(crtc, surf, &crc32);
  119. vmw_surface_unreference(&surf);
  120. }
  121. spin_lock_irq(&du->vkms.crc_state_lock);
  122. frame_start = du->vkms.frame_start;
  123. frame_end = du->vkms.frame_end;
  124. du->vkms.frame_start = 0;
  125. du->vkms.frame_end = 0;
  126. du->vkms.crc_pending = false;
  127. spin_unlock_irq(&du->vkms.crc_state_lock);
  128. /*
  129. * The worker can fall behind the vblank hrtimer, make sure we catch up.
  130. */
  131. while (frame_start <= frame_end)
  132. drm_crtc_add_crc_entry(crtc, true, frame_start++, &crc32);
  133. }
  134. static enum hrtimer_restart
  135. vmw_vkms_vblank_simulate(struct hrtimer *timer)
  136. {
  137. struct vmw_display_unit *du = container_of(timer, struct vmw_display_unit, vkms.timer);
  138. struct drm_crtc *crtc = &du->crtc;
  139. struct vmw_private *vmw = vmw_priv(crtc->dev);
  140. bool has_surface = false;
  141. u64 ret_overrun;
  142. bool locked, ret;
  143. ret_overrun = hrtimer_forward_now(&du->vkms.timer,
  144. du->vkms.period_ns);
  145. if (ret_overrun != 1)
  146. drm_dbg_driver(crtc->dev, "vblank timer missed %lld frames.\n",
  147. ret_overrun - 1);
  148. locked = vmw_vkms_vblank_trylock(crtc);
  149. ret = drm_crtc_handle_vblank(crtc);
  150. WARN_ON(!ret);
  151. if (!locked)
  152. return HRTIMER_RESTART;
  153. has_surface = du->vkms.surface != NULL;
  154. vmw_vkms_unlock(crtc);
  155. if (du->vkms.crc_enabled && has_surface) {
  156. u64 frame = drm_crtc_accurate_vblank_count(crtc);
  157. spin_lock(&du->vkms.crc_state_lock);
  158. if (!du->vkms.crc_pending)
  159. du->vkms.frame_start = frame;
  160. else
  161. drm_dbg_driver(crtc->dev,
  162. "crc worker falling behind, frame_start: %llu, frame_end: %llu\n",
  163. du->vkms.frame_start, frame);
  164. du->vkms.frame_end = frame;
  165. du->vkms.crc_pending = true;
  166. spin_unlock(&du->vkms.crc_state_lock);
  167. ret = queue_work(vmw->crc_workq, &du->vkms.crc_generator_work);
  168. if (!ret)
  169. drm_dbg_driver(crtc->dev, "Composer worker already queued\n");
  170. }
  171. return HRTIMER_RESTART;
  172. }
  173. void
  174. vmw_vkms_init(struct vmw_private *vmw)
  175. {
  176. char buffer[64];
  177. const size_t max_buf_len = sizeof(buffer) - 1;
  178. size_t buf_len = max_buf_len;
  179. int ret;
  180. vmw->vkms_enabled = false;
  181. ret = vmw_host_get_guestinfo(GUESTINFO_VBLANK, buffer, &buf_len);
  182. if (ret || buf_len > max_buf_len)
  183. return;
  184. buffer[buf_len] = '\0';
  185. ret = kstrtobool(buffer, &vmw->vkms_enabled);
  186. if (!ret && vmw->vkms_enabled) {
  187. ret = drm_vblank_init(&vmw->drm, VMWGFX_NUM_DISPLAY_UNITS);
  188. vmw->vkms_enabled = (ret == 0);
  189. }
  190. vmw->crc_workq = alloc_ordered_workqueue("vmwgfx_crc_generator", 0);
  191. if (!vmw->crc_workq) {
  192. drm_warn(&vmw->drm, "crc workqueue allocation failed. Disabling vkms.");
  193. vmw->vkms_enabled = false;
  194. }
  195. if (vmw->vkms_enabled)
  196. drm_info(&vmw->drm, "VKMS enabled\n");
  197. }
  198. void
  199. vmw_vkms_cleanup(struct vmw_private *vmw)
  200. {
  201. destroy_workqueue(vmw->crc_workq);
  202. }
  203. bool
  204. vmw_vkms_get_vblank_timestamp(struct drm_crtc *crtc,
  205. int *max_error,
  206. ktime_t *vblank_time,
  207. bool in_vblank_irq)
  208. {
  209. struct drm_device *dev = crtc->dev;
  210. struct vmw_private *vmw = vmw_priv(dev);
  211. unsigned int pipe = crtc->index;
  212. struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
  213. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  214. if (!vmw->vkms_enabled)
  215. return false;
  216. if (!READ_ONCE(vblank->enabled)) {
  217. *vblank_time = ktime_get();
  218. return true;
  219. }
  220. *vblank_time = READ_ONCE(du->vkms.timer.node.expires);
  221. if (WARN_ON(*vblank_time == vblank->time))
  222. return true;
  223. /*
  224. * To prevent races we roll the hrtimer forward before we do any
  225. * interrupt processing - this is how real hw works (the interrupt is
  226. * only generated after all the vblank registers are updated) and what
  227. * the vblank core expects. Therefore we need to always correct the
  228. * timestampe by one frame.
  229. */
  230. *vblank_time -= du->vkms.period_ns;
  231. return true;
  232. }
  233. int
  234. vmw_vkms_enable_vblank(struct drm_crtc *crtc)
  235. {
  236. struct drm_device *dev = crtc->dev;
  237. struct vmw_private *vmw = vmw_priv(dev);
  238. unsigned int pipe = drm_crtc_index(crtc);
  239. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  240. struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
  241. if (!vmw->vkms_enabled)
  242. return -EINVAL;
  243. drm_calc_timestamping_constants(crtc, &crtc->mode);
  244. hrtimer_init(&du->vkms.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  245. du->vkms.timer.function = &vmw_vkms_vblank_simulate;
  246. du->vkms.period_ns = ktime_set(0, vblank->framedur_ns);
  247. hrtimer_start(&du->vkms.timer, du->vkms.period_ns, HRTIMER_MODE_REL);
  248. return 0;
  249. }
  250. void
  251. vmw_vkms_disable_vblank(struct drm_crtc *crtc)
  252. {
  253. struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
  254. struct vmw_private *vmw = vmw_priv(crtc->dev);
  255. if (!vmw->vkms_enabled)
  256. return;
  257. hrtimer_cancel(&du->vkms.timer);
  258. du->vkms.surface = NULL;
  259. du->vkms.period_ns = ktime_set(0, 0);
  260. }
  261. enum vmw_vkms_lock_state {
  262. VMW_VKMS_LOCK_UNLOCKED = 0,
  263. VMW_VKMS_LOCK_MODESET = 1,
  264. VMW_VKMS_LOCK_VBLANK = 2
  265. };
  266. void
  267. vmw_vkms_crtc_init(struct drm_crtc *crtc)
  268. {
  269. struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
  270. atomic_set(&du->vkms.atomic_lock, VMW_VKMS_LOCK_UNLOCKED);
  271. spin_lock_init(&du->vkms.crc_state_lock);
  272. INIT_WORK(&du->vkms.crc_generator_work, crc_generate_worker);
  273. du->vkms.surface = NULL;
  274. }
  275. void
  276. vmw_vkms_crtc_cleanup(struct drm_crtc *crtc)
  277. {
  278. struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
  279. if (du->vkms.surface)
  280. vmw_surface_unreference(&du->vkms.surface);
  281. WARN_ON(work_pending(&du->vkms.crc_generator_work));
  282. hrtimer_cancel(&du->vkms.timer);
  283. }
  284. void
  285. vmw_vkms_crtc_atomic_begin(struct drm_crtc *crtc,
  286. struct drm_atomic_state *state)
  287. {
  288. struct vmw_private *vmw = vmw_priv(crtc->dev);
  289. if (vmw->vkms_enabled)
  290. vmw_vkms_modeset_lock(crtc);
  291. }
  292. void
  293. vmw_vkms_crtc_atomic_flush(struct drm_crtc *crtc,
  294. struct drm_atomic_state *state)
  295. {
  296. unsigned long flags;
  297. struct vmw_private *vmw = vmw_priv(crtc->dev);
  298. if (!vmw->vkms_enabled)
  299. return;
  300. if (crtc->state->event) {
  301. spin_lock_irqsave(&crtc->dev->event_lock, flags);
  302. if (drm_crtc_vblank_get(crtc) != 0)
  303. drm_crtc_send_vblank_event(crtc, crtc->state->event);
  304. else
  305. drm_crtc_arm_vblank_event(crtc, crtc->state->event);
  306. spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
  307. crtc->state->event = NULL;
  308. }
  309. vmw_vkms_unlock(crtc);
  310. }
  311. void
  312. vmw_vkms_crtc_atomic_enable(struct drm_crtc *crtc,
  313. struct drm_atomic_state *state)
  314. {
  315. struct vmw_private *vmw = vmw_priv(crtc->dev);
  316. if (vmw->vkms_enabled)
  317. drm_crtc_vblank_on(crtc);
  318. }
  319. void
  320. vmw_vkms_crtc_atomic_disable(struct drm_crtc *crtc,
  321. struct drm_atomic_state *state)
  322. {
  323. struct vmw_private *vmw = vmw_priv(crtc->dev);
  324. if (vmw->vkms_enabled)
  325. drm_crtc_vblank_off(crtc);
  326. }
  327. static bool
  328. is_crc_supported(struct drm_crtc *crtc)
  329. {
  330. struct vmw_private *vmw = vmw_priv(crtc->dev);
  331. if (!vmw->vkms_enabled)
  332. return false;
  333. if (vmw->active_display_unit != vmw_du_screen_target)
  334. return false;
  335. return true;
  336. }
  337. static const char * const pipe_crc_sources[] = {"auto"};
  338. static int
  339. crc_parse_source(const char *src_name,
  340. bool *enabled)
  341. {
  342. int ret = 0;
  343. if (!src_name) {
  344. *enabled = false;
  345. } else if (strcmp(src_name, "auto") == 0) {
  346. *enabled = true;
  347. } else {
  348. *enabled = false;
  349. ret = -EINVAL;
  350. }
  351. return ret;
  352. }
  353. const char *const *
  354. vmw_vkms_get_crc_sources(struct drm_crtc *crtc,
  355. size_t *count)
  356. {
  357. *count = 0;
  358. if (!is_crc_supported(crtc))
  359. return NULL;
  360. *count = ARRAY_SIZE(pipe_crc_sources);
  361. return pipe_crc_sources;
  362. }
  363. int
  364. vmw_vkms_verify_crc_source(struct drm_crtc *crtc,
  365. const char *src_name,
  366. size_t *values_cnt)
  367. {
  368. bool enabled;
  369. if (!is_crc_supported(crtc))
  370. return -EINVAL;
  371. if (crc_parse_source(src_name, &enabled) < 0) {
  372. drm_dbg_driver(crtc->dev, "unknown source '%s'\n", src_name);
  373. return -EINVAL;
  374. }
  375. *values_cnt = 1;
  376. return 0;
  377. }
  378. int
  379. vmw_vkms_set_crc_source(struct drm_crtc *crtc,
  380. const char *src_name)
  381. {
  382. struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
  383. bool enabled, prev_enabled, locked;
  384. int ret;
  385. if (!is_crc_supported(crtc))
  386. return -EINVAL;
  387. ret = crc_parse_source(src_name, &enabled);
  388. if (enabled)
  389. drm_crtc_vblank_get(crtc);
  390. locked = vmw_vkms_modeset_lock_relaxed(crtc);
  391. prev_enabled = du->vkms.crc_enabled;
  392. du->vkms.crc_enabled = enabled;
  393. if (locked)
  394. vmw_vkms_unlock(crtc);
  395. if (prev_enabled)
  396. drm_crtc_vblank_put(crtc);
  397. return ret;
  398. }
  399. void
  400. vmw_vkms_set_crc_surface(struct drm_crtc *crtc,
  401. struct vmw_surface *surf)
  402. {
  403. struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
  404. struct vmw_private *vmw = vmw_priv(crtc->dev);
  405. if (vmw->vkms_enabled && du->vkms.surface != surf) {
  406. WARN_ON(atomic_read(&du->vkms.atomic_lock) != VMW_VKMS_LOCK_MODESET);
  407. if (du->vkms.surface)
  408. vmw_surface_unreference(&du->vkms.surface);
  409. if (surf)
  410. du->vkms.surface = vmw_surface_reference(surf);
  411. }
  412. }
  413. /**
  414. * vmw_vkms_lock_max_wait_ns - Return the max wait for the vkms lock
  415. * @du: The vmw_display_unit from which to grab the vblank timings
  416. *
  417. * Returns the maximum wait time used to acquire the vkms lock. By
  418. * default uses a time of a single frame and in case where vblank
  419. * was not initialized for the display unit 1/60th of a second.
  420. */
  421. static inline u64
  422. vmw_vkms_lock_max_wait_ns(struct vmw_display_unit *du)
  423. {
  424. s64 nsecs = ktime_to_ns(du->vkms.period_ns);
  425. return (nsecs > 0) ? nsecs : 16666666;
  426. }
  427. /**
  428. * vmw_vkms_modeset_lock - Protects access to crtc during modeset
  429. * @crtc: The crtc to lock for vkms
  430. *
  431. * This function prevents the VKMS timers/callbacks from being called
  432. * while a modeset operation is in process. We don't want the callbacks
  433. * e.g. the vblank simulator to be trying to access incomplete state
  434. * so we need to make sure they execute only when the modeset has
  435. * finished.
  436. *
  437. * Normally this would have been done with a spinlock but locking the
  438. * entire atomic modeset with vmwgfx is impossible because kms prepare
  439. * executes non-atomic ops (e.g. vmw_validation_prepare holds a mutex to
  440. * guard various bits of state). Which means that we need to synchronize
  441. * atomic context (the vblank handler) with the non-atomic entirity
  442. * of kms - so use an atomic_t to track which part of vkms has access
  443. * to the basic vkms state.
  444. */
  445. void
  446. vmw_vkms_modeset_lock(struct drm_crtc *crtc)
  447. {
  448. struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
  449. const u64 nsecs_delay = 10;
  450. const u64 MAX_NSECS_DELAY = vmw_vkms_lock_max_wait_ns(du);
  451. u64 total_delay = 0;
  452. int ret;
  453. do {
  454. ret = atomic_cmpxchg(&du->vkms.atomic_lock,
  455. VMW_VKMS_LOCK_UNLOCKED,
  456. VMW_VKMS_LOCK_MODESET);
  457. if (ret == VMW_VKMS_LOCK_UNLOCKED || total_delay >= MAX_NSECS_DELAY)
  458. break;
  459. ndelay(nsecs_delay);
  460. total_delay += nsecs_delay;
  461. } while (1);
  462. if (total_delay >= MAX_NSECS_DELAY) {
  463. drm_warn(crtc->dev, "VKMS lock expired! total_delay = %lld, ret = %d, cur = %d\n",
  464. total_delay, ret, atomic_read(&du->vkms.atomic_lock));
  465. }
  466. }
  467. /**
  468. * vmw_vkms_modeset_lock_relaxed - Protects access to crtc during modeset
  469. * @crtc: The crtc to lock for vkms
  470. *
  471. * Much like vmw_vkms_modeset_lock except that when the crtc is currently
  472. * in a modeset it will return immediately.
  473. *
  474. * Returns true if actually locked vkms to modeset or false otherwise.
  475. */
  476. bool
  477. vmw_vkms_modeset_lock_relaxed(struct drm_crtc *crtc)
  478. {
  479. struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
  480. const u64 nsecs_delay = 10;
  481. const u64 MAX_NSECS_DELAY = vmw_vkms_lock_max_wait_ns(du);
  482. u64 total_delay = 0;
  483. int ret;
  484. do {
  485. ret = atomic_cmpxchg(&du->vkms.atomic_lock,
  486. VMW_VKMS_LOCK_UNLOCKED,
  487. VMW_VKMS_LOCK_MODESET);
  488. if (ret == VMW_VKMS_LOCK_UNLOCKED ||
  489. ret == VMW_VKMS_LOCK_MODESET ||
  490. total_delay >= MAX_NSECS_DELAY)
  491. break;
  492. ndelay(nsecs_delay);
  493. total_delay += nsecs_delay;
  494. } while (1);
  495. if (total_delay >= MAX_NSECS_DELAY) {
  496. drm_warn(crtc->dev, "VKMS relaxed lock expired!\n");
  497. return false;
  498. }
  499. return ret == VMW_VKMS_LOCK_UNLOCKED;
  500. }
  501. /**
  502. * vmw_vkms_vblank_trylock - Protects access to crtc during vblank
  503. * @crtc: The crtc to lock for vkms
  504. *
  505. * Tries to lock vkms for vblank, returns immediately.
  506. *
  507. * Returns true if locked vkms to vblank or false otherwise.
  508. */
  509. bool
  510. vmw_vkms_vblank_trylock(struct drm_crtc *crtc)
  511. {
  512. struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
  513. u32 ret;
  514. ret = atomic_cmpxchg(&du->vkms.atomic_lock,
  515. VMW_VKMS_LOCK_UNLOCKED,
  516. VMW_VKMS_LOCK_VBLANK);
  517. return ret == VMW_VKMS_LOCK_UNLOCKED;
  518. }
  519. void
  520. vmw_vkms_unlock(struct drm_crtc *crtc)
  521. {
  522. struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
  523. /* Release flag; mark it as unlocked. */
  524. atomic_set(&du->vkms.atomic_lock, VMW_VKMS_LOCK_UNLOCKED);
  525. }