blob: e622aee6e4be90a599f29ae3496108714580f67a [file] [log] [blame]
Chris Wilsond501b1d2016-04-13 17:35:02 +01001/*
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 Wilson47979482017-05-03 10:39:21 +010028#include <linux/bug.h>
Chris Wilsonf351d082018-05-16 19:33:49 +010029#include <linux/interrupt.h>
Chris Wilson47979482017-05-03 10:39:21 +010030
Jani Nikulaa09d9a82019-08-06 13:07:28 +030031#include <drm/drm_drv.h>
32
Chris Wilson8ad305f2019-11-19 10:09:13 +000033#include "i915_utils.h"
34
Chris Wilsone4d20062018-04-06 16:51:44 +010035struct drm_i915_private;
36
Chris Wilsond501b1d2016-04-13 17:35:02 +010037#ifdef CONFIG_DRM_I915_DEBUG_GEM
Chris Wilson7f961d72018-04-26 11:32:19 +010038
Jani Nikulabdbf43d2019-10-28 12:38:15 +020039#define GEM_SHOW_DEBUG() drm_debug_enabled(DRM_UT_DRIVER)
Chris Wilson7f961d72018-04-26 11:32:19 +010040
Chris Wilson03c62d82021-01-14 11:34:34 +000041#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 Kuoppalad5653ec2017-11-16 10:39:54 +020048#define GEM_BUG_ON(condition) do { if (unlikely((condition))) { \
Chris Wilsonc97fb522019-10-10 08:14:26 +010049 GEM_TRACE_ERR("%s:%d GEM_BUG_ON(%s)\n", \
50 __func__, __LINE__, __stringify(condition)); \
Chris Wilson8ad305f2019-11-19 10:09:13 +000051 GEM_TRACE_DUMP(); \
Chris Wilson03c62d82021-01-14 11:34:34 +000052 __GEM_BUG(condition); \
Mika Kuoppalad5653ec2017-11-16 10:39:54 +020053 } \
54 } while(0)
Matthew Auld17cfde62016-12-13 20:32:19 +000055#define GEM_WARN_ON(expr) WARN_ON(expr)
Chris Wilsoneca56a32017-02-06 17:05:01 +000056
Chris Wilsonae9a0432017-02-07 10:23:19 +000057#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 Ursulinbbb8a9d2018-10-12 07:31:42 +010060#define GEM_DEBUG_WARN_ON(expr) GEM_WARN_ON(expr)
Chris Wilsoneca56a32017-02-06 17:05:01 +000061
Chris Wilsond501b1d2016-04-13 17:35:02 +010062#else
Chris Wilson7f961d72018-04-26 11:32:19 +010063
64#define GEM_SHOW_DEBUG() (0)
65
Matthew Auld4cfa20c2016-12-02 18:47:50 +000066#define GEM_BUG_ON(expr) BUILD_BUG_ON_INVALID(expr)
Tvrtko Ursulinbbb8a9d2018-10-12 07:31:42 +010067#define GEM_WARN_ON(expr) ({ unlikely(!!(expr)); })
Chris Wilsoneca56a32017-02-06 17:05:01 +000068
Chris Wilsonae9a0432017-02-07 10:23:19 +000069#define GEM_DEBUG_DECL(var)
70#define GEM_DEBUG_EXEC(expr) do { } while (0)
71#define GEM_DEBUG_BUG_ON(expr)
Tvrtko Ursulinbbb8a9d2018-10-12 07:31:42 +010072#define GEM_DEBUG_WARN_ON(expr) ({ BUILD_BUG_ON_INVALID(expr); 0; })
Chris Wilsond501b1d2016-04-13 17:35:02 +010073#endif
74
Chris Wilsonbccd3b82017-11-09 14:30:19 +000075#if IS_ENABLED(CONFIG_DRM_I915_TRACE_GEM)
76#define GEM_TRACE(...) trace_printk(__VA_ARGS__)
Chris Wilsonc97fb522019-10-10 08:14:26 +010077#define GEM_TRACE_ERR(...) do { \
78 pr_err(__VA_ARGS__); \
79 trace_printk(__VA_ARGS__); \
80} while (0)
Chris Wilson7a73e672019-11-10 18:57:54 +000081#define GEM_TRACE_DUMP() \
Michał Winiarski65706202020-07-06 16:41:06 +020082 do { ftrace_dump(DUMP_ALL); __add_taint_for_CI(TAINT_WARN); } while (0)
Chris Wilson09a4c022018-05-24 09:11:35 +010083#define GEM_TRACE_DUMP_ON(expr) \
Chris Wilson7a73e672019-11-10 18:57:54 +000084 do { if (expr) GEM_TRACE_DUMP(); } while (0)
Chris Wilsonbccd3b82017-11-09 14:30:19 +000085#else
86#define GEM_TRACE(...) do { } while (0)
Chris Wilsonc97fb522019-10-10 08:14:26 +010087#define GEM_TRACE_ERR(...) do { } while (0)
Chris Wilson629820f2018-03-09 10:11:14 +000088#define GEM_TRACE_DUMP() do { } while (0)
Chris Wilson09a4c022018-05-24 09:11:35 +010089#define GEM_TRACE_DUMP_ON(expr) BUILD_BUG_ON_INVALID(expr)
Chris Wilsonbccd3b82017-11-09 14:30:19 +000090#endif
91
Chris Wilson3d606242019-03-07 10:45:29 +000092#define I915_GEM_IDLE_TIMEOUT (HZ / 5)
93
Chris Wilsonc36eebd2019-10-09 17:09:06 +010094static inline void tasklet_lock(struct tasklet_struct *t)
95{
96 while (!tasklet_trylock(t))
97 cpu_relax();
98}
99
Chris Wilson3c006602019-10-14 13:13:36 +0100100static inline bool tasklet_is_locked(const struct tasklet_struct *t)
101{
102 return test_bit(TASKLET_STATE_RUN, &t->state);
103}
104
Chris Wilsonf351d082018-05-16 19:33:49 +0100105static inline void __tasklet_disable_sync_once(struct tasklet_struct *t)
106{
Chris Wilson41a1bde2019-03-13 16:28:35 +0000107 if (!atomic_fetch_inc(&t->count))
Chris Wilsonf351d082018-05-16 19:33:49 +0100108 tasklet_unlock_wait(t);
109}
110
Chris Wilson9512f982018-06-28 21:12:11 +0100111static inline bool __tasklet_is_enabled(const struct tasklet_struct *t)
112{
113 return !atomic_read(&t->count);
114}
115
Chris Wilson41a1bde2019-03-13 16:28:35 +0000116static inline bool __tasklet_enable(struct tasklet_struct *t)
117{
118 return atomic_dec_and_test(&t->count);
119}
120
Chris Wilson6d067792019-05-21 22:11:30 +0100121static inline bool __tasklet_is_scheduled(struct tasklet_struct *t)
122{
123 return test_bit(TASKLET_STATE_SCHED, &t->state);
124}
125
Maarten Lankhorst80f0b672020-08-19 16:08:45 +0200126struct 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
133void i915_gem_ww_ctx_init(struct i915_gem_ww_ctx *ctx, bool intr);
134void i915_gem_ww_ctx_fini(struct i915_gem_ww_ctx *ctx);
135int __must_check i915_gem_ww_ctx_backoff(struct i915_gem_ww_ctx *ctx);
Maarten Lankhorstc43ce122020-08-19 16:08:48 +0200136void i915_gem_ww_unlock_single(struct drm_i915_gem_object *obj);
Maarten Lankhorst80f0b672020-08-19 16:08:45 +0200137
Chris Wilsond501b1d2016-04-13 17:35:02 +0100138#endif /* __I915_GEM_H__ */