Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright © 2016 Intel Corporation
0003  *
0004  * Permission is hereby granted, free of charge, to any person obtaining a
0005  * copy of this software and associated documentation files (the "Software"),
0006  * to deal in the Software without restriction, including without limitation
0007  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
0008  * and/or sell copies of the Software, and to permit persons to whom the
0009  * Software is furnished to do so, subject to the following conditions:
0010  *
0011  * The above copyright notice and this permission notice (including the next
0012  * paragraph) shall be included in all copies or substantial portions of the
0013  * Software.
0014  *
0015  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0016  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0017  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
0018  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
0019  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
0020  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
0021  * IN THE SOFTWARE.
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 /* Note we don't consider signbits :| */
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  * container_of_user: Extract the superclass from a pointer to a member.
0167  *
0168  * Exactly like container_of() with the exception that it plays nicely
0169  * with sparse for __user @ptr.
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  * check_user_mbz: Check that a user value exists and is zero
0180  *
0181  * Frequently in our uABI we reserve space for future extensions, and
0182  * two ensure that userspace is prepared we enforce that space must
0183  * be zero. (Then any future extension can safely assume a default value
0184  * of 0.)
0185  *
0186  * check_user_mbz() combines checking that the user pointer is accessible
0187  * and that the contained value is zero.
0188  *
0189  * Returns: -EFAULT if not accessible, -EINVAL if !zero, or 0 on success.
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  * If you need to wait X milliseconds between events A and B, but event B
0234  * doesn't happen exactly after event A, you record the timestamp (jiffies) of
0235  * when event A happened, then just before event B you call this function and
0236  * pass the timestamp as the first argument, and X as the second argument.
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      * Don't re-read the value of "jiffies" every time since it may change
0245      * behind our back and break the math.
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  * __wait_for - magic wait macro
0261  *
0262  * Macro to help avoid open coding check/wait/timeout patterns. Note that it's
0263  * important that we check the condition again after having timed out, since the
0264  * timeout could be due to preemption or similar and we've never had a chance to
0265  * check the condition before the timeout.
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); /* recommended min for usleep is 10 us */ \
0270     int ret__;                          \
0271     might_sleep();                          \
0272     for (;;) {                          \
0273         const bool expired__ = ktime_after(ktime_get_raw(), end__); \
0274         OP;                         \
0275         /* Guarantee COND check prior to timeout */     \
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 /* If CONFIG_PREEMPT_COUNT is disabled, in_atomic() always reports false. */
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         /* Guarantee COND check prior to timeout */ \
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      * The system is "ok", just about surviving for the user, but
0372      * CI results are now unreliable as the HW is very suspect.
0373      * CI checks the taint state after every test and will reboot
0374      * the machine if the kernel is tainted.
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     /* Not supported yet */
0398     return false;
0399 #endif
0400 }
0401 
0402 bool i915_vtd_active(struct drm_i915_private *i915);
0403 
0404 #endif /* !__I915_UTILS_H */