Chris Wilson | d501b1d | 2016-04-13 17:35:02 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2016 Intel Corporation |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | * copy of this software and associated documentation files (the "Software"), |
| 6 | * to deal in the Software without restriction, including without limitation |
| 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | * and/or sell copies of the Software, and to permit persons to whom the |
| 9 | * Software is furnished to do so, subject to the following conditions: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice (including the next |
| 12 | * paragraph) shall be included in all copies or substantial portions of the |
| 13 | * Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 20 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 21 | * IN THE SOFTWARE. |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | #ifndef __I915_GEM_H__ |
| 26 | #define __I915_GEM_H__ |
| 27 | |
Chris Wilson | 4797948 | 2017-05-03 10:39:21 +0100 | [diff] [blame] | 28 | #include <linux/bug.h> |
Chris Wilson | f351d08 | 2018-05-16 19:33:49 +0100 | [diff] [blame] | 29 | #include <linux/interrupt.h> |
Chris Wilson | 4797948 | 2017-05-03 10:39:21 +0100 | [diff] [blame] | 30 | |
Jani Nikula | a09d9a8 | 2019-08-06 13:07:28 +0300 | [diff] [blame] | 31 | #include <drm/drm_drv.h> |
| 32 | |
Chris Wilson | 8ad305f | 2019-11-19 10:09:13 +0000 | [diff] [blame] | 33 | #include "i915_utils.h" |
| 34 | |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 35 | struct drm_i915_private; |
| 36 | |
Chris Wilson | d501b1d | 2016-04-13 17:35:02 +0100 | [diff] [blame] | 37 | #ifdef CONFIG_DRM_I915_DEBUG_GEM |
Chris Wilson | 7f961d7 | 2018-04-26 11:32:19 +0100 | [diff] [blame] | 38 | |
Jani Nikula | bdbf43d | 2019-10-28 12:38:15 +0200 | [diff] [blame] | 39 | #define GEM_SHOW_DEBUG() drm_debug_enabled(DRM_UT_DRIVER) |
Chris Wilson | 7f961d7 | 2018-04-26 11:32:19 +0100 | [diff] [blame] | 40 | |
Chris Wilson | 03c62d8 | 2021-01-14 11:34:34 +0000 | [diff] [blame] | 41 | #ifdef CONFIG_DRM_I915_DEBUG_GEM_ONCE |
| 42 | #define __GEM_BUG(cond) BUG() |
| 43 | #else |
| 44 | #define __GEM_BUG(cond) \ |
| 45 | WARN(1, "%s:%d GEM_BUG_ON(%s)\n", __func__, __LINE__, __stringify(cond)) |
| 46 | #endif |
| 47 | |
Mika Kuoppala | d5653ec | 2017-11-16 10:39:54 +0200 | [diff] [blame] | 48 | #define GEM_BUG_ON(condition) do { if (unlikely((condition))) { \ |
Chris Wilson | c97fb52 | 2019-10-10 08:14:26 +0100 | [diff] [blame] | 49 | GEM_TRACE_ERR("%s:%d GEM_BUG_ON(%s)\n", \ |
| 50 | __func__, __LINE__, __stringify(condition)); \ |
Chris Wilson | 8ad305f | 2019-11-19 10:09:13 +0000 | [diff] [blame] | 51 | GEM_TRACE_DUMP(); \ |
Chris Wilson | 03c62d8 | 2021-01-14 11:34:34 +0000 | [diff] [blame] | 52 | __GEM_BUG(condition); \ |
Mika Kuoppala | d5653ec | 2017-11-16 10:39:54 +0200 | [diff] [blame] | 53 | } \ |
| 54 | } while(0) |
Matthew Auld | 17cfde6 | 2016-12-13 20:32:19 +0000 | [diff] [blame] | 55 | #define GEM_WARN_ON(expr) WARN_ON(expr) |
Chris Wilson | eca56a3 | 2017-02-06 17:05:01 +0000 | [diff] [blame] | 56 | |
Chris Wilson | ae9a043 | 2017-02-07 10:23:19 +0000 | [diff] [blame] | 57 | #define GEM_DEBUG_DECL(var) var |
| 58 | #define GEM_DEBUG_EXEC(expr) expr |
| 59 | #define GEM_DEBUG_BUG_ON(expr) GEM_BUG_ON(expr) |
Tvrtko Ursulin | bbb8a9d | 2018-10-12 07:31:42 +0100 | [diff] [blame] | 60 | #define GEM_DEBUG_WARN_ON(expr) GEM_WARN_ON(expr) |
Chris Wilson | eca56a3 | 2017-02-06 17:05:01 +0000 | [diff] [blame] | 61 | |
Chris Wilson | d501b1d | 2016-04-13 17:35:02 +0100 | [diff] [blame] | 62 | #else |
Chris Wilson | 7f961d7 | 2018-04-26 11:32:19 +0100 | [diff] [blame] | 63 | |
| 64 | #define GEM_SHOW_DEBUG() (0) |
| 65 | |
Matthew Auld | 4cfa20c | 2016-12-02 18:47:50 +0000 | [diff] [blame] | 66 | #define GEM_BUG_ON(expr) BUILD_BUG_ON_INVALID(expr) |
Tvrtko Ursulin | bbb8a9d | 2018-10-12 07:31:42 +0100 | [diff] [blame] | 67 | #define GEM_WARN_ON(expr) ({ unlikely(!!(expr)); }) |
Chris Wilson | eca56a3 | 2017-02-06 17:05:01 +0000 | [diff] [blame] | 68 | |
Chris Wilson | ae9a043 | 2017-02-07 10:23:19 +0000 | [diff] [blame] | 69 | #define GEM_DEBUG_DECL(var) |
| 70 | #define GEM_DEBUG_EXEC(expr) do { } while (0) |
| 71 | #define GEM_DEBUG_BUG_ON(expr) |
Tvrtko Ursulin | bbb8a9d | 2018-10-12 07:31:42 +0100 | [diff] [blame] | 72 | #define GEM_DEBUG_WARN_ON(expr) ({ BUILD_BUG_ON_INVALID(expr); 0; }) |
Chris Wilson | d501b1d | 2016-04-13 17:35:02 +0100 | [diff] [blame] | 73 | #endif |
| 74 | |
Chris Wilson | bccd3b8 | 2017-11-09 14:30:19 +0000 | [diff] [blame] | 75 | #if IS_ENABLED(CONFIG_DRM_I915_TRACE_GEM) |
| 76 | #define GEM_TRACE(...) trace_printk(__VA_ARGS__) |
Chris Wilson | c97fb52 | 2019-10-10 08:14:26 +0100 | [diff] [blame] | 77 | #define GEM_TRACE_ERR(...) do { \ |
| 78 | pr_err(__VA_ARGS__); \ |
| 79 | trace_printk(__VA_ARGS__); \ |
| 80 | } while (0) |
Chris Wilson | 7a73e67 | 2019-11-10 18:57:54 +0000 | [diff] [blame] | 81 | #define GEM_TRACE_DUMP() \ |
Michał Winiarski | 6570620 | 2020-07-06 16:41:06 +0200 | [diff] [blame] | 82 | do { ftrace_dump(DUMP_ALL); __add_taint_for_CI(TAINT_WARN); } while (0) |
Chris Wilson | 09a4c02 | 2018-05-24 09:11:35 +0100 | [diff] [blame] | 83 | #define GEM_TRACE_DUMP_ON(expr) \ |
Chris Wilson | 7a73e67 | 2019-11-10 18:57:54 +0000 | [diff] [blame] | 84 | do { if (expr) GEM_TRACE_DUMP(); } while (0) |
Chris Wilson | bccd3b8 | 2017-11-09 14:30:19 +0000 | [diff] [blame] | 85 | #else |
| 86 | #define GEM_TRACE(...) do { } while (0) |
Chris Wilson | c97fb52 | 2019-10-10 08:14:26 +0100 | [diff] [blame] | 87 | #define GEM_TRACE_ERR(...) do { } while (0) |
Chris Wilson | 629820f | 2018-03-09 10:11:14 +0000 | [diff] [blame] | 88 | #define GEM_TRACE_DUMP() do { } while (0) |
Chris Wilson | 09a4c02 | 2018-05-24 09:11:35 +0100 | [diff] [blame] | 89 | #define GEM_TRACE_DUMP_ON(expr) BUILD_BUG_ON_INVALID(expr) |
Chris Wilson | bccd3b8 | 2017-11-09 14:30:19 +0000 | [diff] [blame] | 90 | #endif |
| 91 | |
Chris Wilson | 3d60624 | 2019-03-07 10:45:29 +0000 | [diff] [blame] | 92 | #define I915_GEM_IDLE_TIMEOUT (HZ / 5) |
| 93 | |
Chris Wilson | c36eebd | 2019-10-09 17:09:06 +0100 | [diff] [blame] | 94 | static inline void tasklet_lock(struct tasklet_struct *t) |
| 95 | { |
| 96 | while (!tasklet_trylock(t)) |
| 97 | cpu_relax(); |
| 98 | } |
| 99 | |
Chris Wilson | 3c00660 | 2019-10-14 13:13:36 +0100 | [diff] [blame] | 100 | static inline bool tasklet_is_locked(const struct tasklet_struct *t) |
| 101 | { |
| 102 | return test_bit(TASKLET_STATE_RUN, &t->state); |
| 103 | } |
| 104 | |
Chris Wilson | f351d08 | 2018-05-16 19:33:49 +0100 | [diff] [blame] | 105 | static inline void __tasklet_disable_sync_once(struct tasklet_struct *t) |
| 106 | { |
Chris Wilson | 41a1bde | 2019-03-13 16:28:35 +0000 | [diff] [blame] | 107 | if (!atomic_fetch_inc(&t->count)) |
Chris Wilson | f351d08 | 2018-05-16 19:33:49 +0100 | [diff] [blame] | 108 | tasklet_unlock_wait(t); |
| 109 | } |
| 110 | |
Chris Wilson | 9512f98 | 2018-06-28 21:12:11 +0100 | [diff] [blame] | 111 | static inline bool __tasklet_is_enabled(const struct tasklet_struct *t) |
| 112 | { |
| 113 | return !atomic_read(&t->count); |
| 114 | } |
| 115 | |
Chris Wilson | 41a1bde | 2019-03-13 16:28:35 +0000 | [diff] [blame] | 116 | static inline bool __tasklet_enable(struct tasklet_struct *t) |
| 117 | { |
| 118 | return atomic_dec_and_test(&t->count); |
| 119 | } |
| 120 | |
Chris Wilson | 6d06779 | 2019-05-21 22:11:30 +0100 | [diff] [blame] | 121 | static inline bool __tasklet_is_scheduled(struct tasklet_struct *t) |
| 122 | { |
| 123 | return test_bit(TASKLET_STATE_SCHED, &t->state); |
| 124 | } |
| 125 | |
Maarten Lankhorst | 80f0b67 | 2020-08-19 16:08:45 +0200 | [diff] [blame] | 126 | struct i915_gem_ww_ctx { |
| 127 | struct ww_acquire_ctx ctx; |
| 128 | struct list_head obj_list; |
| 129 | bool intr; |
| 130 | struct drm_i915_gem_object *contended; |
| 131 | }; |
| 132 | |
| 133 | void i915_gem_ww_ctx_init(struct i915_gem_ww_ctx *ctx, bool intr); |
| 134 | void i915_gem_ww_ctx_fini(struct i915_gem_ww_ctx *ctx); |
| 135 | int __must_check i915_gem_ww_ctx_backoff(struct i915_gem_ww_ctx *ctx); |
Maarten Lankhorst | c43ce12 | 2020-08-19 16:08:48 +0200 | [diff] [blame] | 136 | void i915_gem_ww_unlock_single(struct drm_i915_gem_object *obj); |
Maarten Lankhorst | 80f0b67 | 2020-08-19 16:08:45 +0200 | [diff] [blame] | 137 | |
Chris Wilson | d501b1d | 2016-04-13 17:35:02 +0100 | [diff] [blame] | 138 | #endif /* __I915_GEM_H__ */ |