0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025 #ifndef __I915_UTILS_H
0026 #define __I915_UTILS_H
0027
0028 #include <linux/list.h>
0029 #include <linux/overflow.h>
0030 #include <linux/sched.h>
0031 #include <linux/string_helpers.h>
0032 #include <linux/types.h>
0033 #include <linux/workqueue.h>
0034 #include <linux/sched/clock.h>
0035
0036 #ifdef CONFIG_X86
0037 #include <asm/hypervisor.h>
0038 #endif
0039
0040 struct drm_i915_private;
0041 struct timer_list;
0042
0043 #define FDO_BUG_URL "https://gitlab.freedesktop.org/drm/intel/-/wikis/How-to-file-i915-bugs"
0044
0045 #define MISSING_CASE(x) WARN(1, "Missing case (%s == %ld)\n", \
0046 __stringify(x), (long)(x))
0047
0048 void __printf(3, 4)
0049 __i915_printk(struct drm_i915_private *dev_priv, const char *level,
0050 const char *fmt, ...);
0051
0052 #define i915_report_error(dev_priv, fmt, ...) \
0053 __i915_printk(dev_priv, KERN_ERR, fmt, ##__VA_ARGS__)
0054
0055 #if IS_ENABLED(CONFIG_DRM_I915_DEBUG)
0056
0057 int __i915_inject_probe_error(struct drm_i915_private *i915, int err,
0058 const char *func, int line);
0059 #define i915_inject_probe_error(_i915, _err) \
0060 __i915_inject_probe_error((_i915), (_err), __func__, __LINE__)
0061 bool i915_error_injected(void);
0062
0063 #else
0064
0065 #define i915_inject_probe_error(i915, e) ({ BUILD_BUG_ON_INVALID(i915); 0; })
0066 #define i915_error_injected() false
0067
0068 #endif
0069
0070 #define i915_inject_probe_failure(i915) i915_inject_probe_error((i915), -ENODEV)
0071
0072 #define i915_probe_error(i915, fmt, ...) \
0073 __i915_printk(i915, i915_error_injected() ? KERN_DEBUG : KERN_ERR, \
0074 fmt, ##__VA_ARGS__)
0075
0076 #if defined(GCC_VERSION) && GCC_VERSION >= 70000
0077 #define add_overflows_t(T, A, B) \
0078 __builtin_add_overflow_p((A), (B), (T)0)
0079 #else
0080 #define add_overflows_t(T, A, B) ({ \
0081 typeof(A) a = (A); \
0082 typeof(B) b = (B); \
0083 (T)(a + b) < a; \
0084 })
0085 #endif
0086
0087 #define add_overflows(A, B) \
0088 add_overflows_t(typeof((A) + (B)), (A), (B))
0089
0090 #define range_overflows(start, size, max) ({ \
0091 typeof(start) start__ = (start); \
0092 typeof(size) size__ = (size); \
0093 typeof(max) max__ = (max); \
0094 (void)(&start__ == &size__); \
0095 (void)(&start__ == &max__); \
0096 start__ >= max__ || size__ > max__ - start__; \
0097 })
0098
0099 #define range_overflows_t(type, start, size, max) \
0100 range_overflows((type)(start), (type)(size), (type)(max))
0101
0102 #define range_overflows_end(start, size, max) ({ \
0103 typeof(start) start__ = (start); \
0104 typeof(size) size__ = (size); \
0105 typeof(max) max__ = (max); \
0106 (void)(&start__ == &size__); \
0107 (void)(&start__ == &max__); \
0108 start__ > max__ || size__ > max__ - start__; \
0109 })
0110
0111 #define range_overflows_end_t(type, start, size, max) \
0112 range_overflows_end((type)(start), (type)(size), (type)(max))
0113
0114
0115 #define overflows_type(x, T) \
0116 (sizeof(x) > sizeof(T) && (x) >> BITS_PER_TYPE(T))
0117
0118 #define ptr_mask_bits(ptr, n) ({ \
0119 unsigned long __v = (unsigned long)(ptr); \
0120 (typeof(ptr))(__v & -BIT(n)); \
0121 })
0122
0123 #define ptr_unmask_bits(ptr, n) ((unsigned long)(ptr) & (BIT(n) - 1))
0124
0125 #define ptr_unpack_bits(ptr, bits, n) ({ \
0126 unsigned long __v = (unsigned long)(ptr); \
0127 *(bits) = __v & (BIT(n) - 1); \
0128 (typeof(ptr))(__v & -BIT(n)); \
0129 })
0130
0131 #define ptr_pack_bits(ptr, bits, n) ({ \
0132 unsigned long __bits = (bits); \
0133 GEM_BUG_ON(__bits & -BIT(n)); \
0134 ((typeof(ptr))((unsigned long)(ptr) | __bits)); \
0135 })
0136
0137 #define ptr_dec(ptr) ({ \
0138 unsigned long __v = (unsigned long)(ptr); \
0139 (typeof(ptr))(__v - 1); \
0140 })
0141
0142 #define ptr_inc(ptr) ({ \
0143 unsigned long __v = (unsigned long)(ptr); \
0144 (typeof(ptr))(__v + 1); \
0145 })
0146
0147 #define page_mask_bits(ptr) ptr_mask_bits(ptr, PAGE_SHIFT)
0148 #define page_unmask_bits(ptr) ptr_unmask_bits(ptr, PAGE_SHIFT)
0149 #define page_pack_bits(ptr, bits) ptr_pack_bits(ptr, bits, PAGE_SHIFT)
0150 #define page_unpack_bits(ptr, bits) ptr_unpack_bits(ptr, bits, PAGE_SHIFT)
0151
0152 #define struct_member(T, member) (((T *)0)->member)
0153
0154 #define fetch_and_zero(ptr) ({ \
0155 typeof(*ptr) __T = *(ptr); \
0156 *(ptr) = (typeof(*ptr))0; \
0157 __T; \
0158 })
0159
0160 static __always_inline ptrdiff_t ptrdiff(const void *a, const void *b)
0161 {
0162 return a - b;
0163 }
0164
0165
0166
0167
0168
0169
0170
0171 #define container_of_user(ptr, type, member) ({ \
0172 void __user *__mptr = (void __user *)(ptr); \
0173 BUILD_BUG_ON_MSG(!__same_type(*(ptr), struct_member(type, member)) && \
0174 !__same_type(*(ptr), void), \
0175 "pointer type mismatch in container_of()"); \
0176 ((type __user *)(__mptr - offsetof(type, member))); })
0177
0178
0179
0180
0181
0182
0183
0184
0185
0186
0187
0188
0189
0190
0191 #define check_user_mbz(U) ({ \
0192 typeof(*(U)) mbz__; \
0193 get_user(mbz__, (U)) ? -EFAULT : mbz__ ? -EINVAL : 0; \
0194 })
0195
0196 #define u64_to_ptr(T, x) ({ \
0197 typecheck(u64, x); \
0198 (T *)(uintptr_t)(x); \
0199 })
0200
0201 #define __mask_next_bit(mask) ({ \
0202 int __idx = ffs(mask) - 1; \
0203 mask &= ~BIT(__idx); \
0204 __idx; \
0205 })
0206
0207 static inline bool is_power_of_2_u64(u64 n)
0208 {
0209 return (n != 0 && ((n & (n - 1)) == 0));
0210 }
0211
0212 static inline void __list_del_many(struct list_head *head,
0213 struct list_head *first)
0214 {
0215 first->prev = head;
0216 WRITE_ONCE(head->next, first);
0217 }
0218
0219 static inline int list_is_last_rcu(const struct list_head *list,
0220 const struct list_head *head)
0221 {
0222 return READ_ONCE(list->next) == head;
0223 }
0224
0225 static inline unsigned long msecs_to_jiffies_timeout(const unsigned int m)
0226 {
0227 unsigned long j = msecs_to_jiffies(m);
0228
0229 return min_t(unsigned long, MAX_JIFFY_OFFSET, j + 1);
0230 }
0231
0232
0233
0234
0235
0236
0237
0238 static inline void
0239 wait_remaining_ms_from_jiffies(unsigned long timestamp_jiffies, int to_wait_ms)
0240 {
0241 unsigned long target_jiffies, tmp_jiffies, remaining_jiffies;
0242
0243
0244
0245
0246
0247 tmp_jiffies = jiffies;
0248 target_jiffies = timestamp_jiffies +
0249 msecs_to_jiffies_timeout(to_wait_ms);
0250
0251 if (time_after(target_jiffies, tmp_jiffies)) {
0252 remaining_jiffies = target_jiffies - tmp_jiffies;
0253 while (remaining_jiffies)
0254 remaining_jiffies =
0255 schedule_timeout_uninterruptible(remaining_jiffies);
0256 }
0257 }
0258
0259
0260
0261
0262
0263
0264
0265
0266
0267 #define __wait_for(OP, COND, US, Wmin, Wmax) ({ \
0268 const ktime_t end__ = ktime_add_ns(ktime_get_raw(), 1000ll * (US)); \
0269 long wait__ = (Wmin); \
0270 int ret__; \
0271 might_sleep(); \
0272 for (;;) { \
0273 const bool expired__ = ktime_after(ktime_get_raw(), end__); \
0274 OP; \
0275 \
0276 barrier(); \
0277 if (COND) { \
0278 ret__ = 0; \
0279 break; \
0280 } \
0281 if (expired__) { \
0282 ret__ = -ETIMEDOUT; \
0283 break; \
0284 } \
0285 usleep_range(wait__, wait__ * 2); \
0286 if (wait__ < (Wmax)) \
0287 wait__ <<= 1; \
0288 } \
0289 ret__; \
0290 })
0291
0292 #define _wait_for(COND, US, Wmin, Wmax) __wait_for(, (COND), (US), (Wmin), \
0293 (Wmax))
0294 #define wait_for(COND, MS) _wait_for((COND), (MS) * 1000, 10, 1000)
0295
0296
0297 #if defined(CONFIG_DRM_I915_DEBUG) && defined(CONFIG_PREEMPT_COUNT)
0298 # define _WAIT_FOR_ATOMIC_CHECK(ATOMIC) WARN_ON_ONCE((ATOMIC) && !in_atomic())
0299 #else
0300 # define _WAIT_FOR_ATOMIC_CHECK(ATOMIC) do { } while (0)
0301 #endif
0302
0303 #define _wait_for_atomic(COND, US, ATOMIC) \
0304 ({ \
0305 int cpu, ret, timeout = (US) * 1000; \
0306 u64 base; \
0307 _WAIT_FOR_ATOMIC_CHECK(ATOMIC); \
0308 if (!(ATOMIC)) { \
0309 preempt_disable(); \
0310 cpu = smp_processor_id(); \
0311 } \
0312 base = local_clock(); \
0313 for (;;) { \
0314 u64 now = local_clock(); \
0315 if (!(ATOMIC)) \
0316 preempt_enable(); \
0317 \
0318 barrier(); \
0319 if (COND) { \
0320 ret = 0; \
0321 break; \
0322 } \
0323 if (now - base >= timeout) { \
0324 ret = -ETIMEDOUT; \
0325 break; \
0326 } \
0327 cpu_relax(); \
0328 if (!(ATOMIC)) { \
0329 preempt_disable(); \
0330 if (unlikely(cpu != smp_processor_id())) { \
0331 timeout -= now - base; \
0332 cpu = smp_processor_id(); \
0333 base = local_clock(); \
0334 } \
0335 } \
0336 } \
0337 ret; \
0338 })
0339
0340 #define wait_for_us(COND, US) \
0341 ({ \
0342 int ret__; \
0343 BUILD_BUG_ON(!__builtin_constant_p(US)); \
0344 if ((US) > 10) \
0345 ret__ = _wait_for((COND), (US), 10, 10); \
0346 else \
0347 ret__ = _wait_for_atomic((COND), (US), 0); \
0348 ret__; \
0349 })
0350
0351 #define wait_for_atomic_us(COND, US) \
0352 ({ \
0353 BUILD_BUG_ON(!__builtin_constant_p(US)); \
0354 BUILD_BUG_ON((US) > 50000); \
0355 _wait_for_atomic((COND), (US), 1); \
0356 })
0357
0358 #define wait_for_atomic(COND, MS) wait_for_atomic_us((COND), (MS) * 1000)
0359
0360 #define KHz(x) (1000 * (x))
0361 #define MHz(x) KHz(1000 * (x))
0362
0363 #define KBps(x) (1000 * (x))
0364 #define MBps(x) KBps(1000 * (x))
0365 #define GBps(x) ((u64)1000 * MBps((x)))
0366
0367 void add_taint_for_CI(struct drm_i915_private *i915, unsigned int taint);
0368 static inline void __add_taint_for_CI(unsigned int taint)
0369 {
0370
0371
0372
0373
0374
0375
0376 add_taint(taint, LOCKDEP_STILL_OK);
0377 }
0378
0379 void cancel_timer(struct timer_list *t);
0380 void set_timer_ms(struct timer_list *t, unsigned long timeout);
0381
0382 static inline bool timer_active(const struct timer_list *t)
0383 {
0384 return READ_ONCE(t->expires);
0385 }
0386
0387 static inline bool timer_expired(const struct timer_list *t)
0388 {
0389 return timer_active(t) && !timer_pending(t);
0390 }
0391
0392 static inline bool i915_run_as_guest(void)
0393 {
0394 #if IS_ENABLED(CONFIG_X86)
0395 return !hypervisor_is_type(X86_HYPER_NATIVE);
0396 #else
0397
0398 return false;
0399 #endif
0400 }
0401
0402 bool i915_vtd_active(struct drm_i915_private *i915);
0403
0404 #endif