140b326eeSChris Wilson /* 240b326eeSChris Wilson * Copyright © 2016 Intel Corporation 340b326eeSChris Wilson * 440b326eeSChris Wilson * Permission is hereby granted, free of charge, to any person obtaining a 540b326eeSChris Wilson * copy of this software and associated documentation files (the "Software"), 640b326eeSChris Wilson * to deal in the Software without restriction, including without limitation 740b326eeSChris Wilson * the rights to use, copy, modify, merge, publish, distribute, sublicense, 840b326eeSChris Wilson * and/or sell copies of the Software, and to permit persons to whom the 940b326eeSChris Wilson * Software is furnished to do so, subject to the following conditions: 1040b326eeSChris Wilson * 1140b326eeSChris Wilson * The above copyright notice and this permission notice (including the next 1240b326eeSChris Wilson * paragraph) shall be included in all copies or substantial portions of the 1340b326eeSChris Wilson * Software. 1440b326eeSChris Wilson * 1540b326eeSChris Wilson * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 1640b326eeSChris Wilson * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 1740b326eeSChris Wilson * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 1840b326eeSChris Wilson * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 1940b326eeSChris Wilson * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 2040b326eeSChris Wilson * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 2140b326eeSChris Wilson * IN THE SOFTWARE. 2240b326eeSChris Wilson * 2340b326eeSChris Wilson */ 2440b326eeSChris Wilson 2540b326eeSChris Wilson #ifndef __I915_UTILS_H 2640b326eeSChris Wilson #define __I915_UTILS_H 2740b326eeSChris Wilson 28cb363304SJani Nikula #include <linux/list.h> 29976b55f0SChris Wilson #include <linux/overflow.h> 30b30ed4ccSJani Nikula #include <linux/sched.h> 31cb363304SJani Nikula #include <linux/types.h> 32cb363304SJani Nikula #include <linux/workqueue.h> 33cb363304SJani Nikula 34358c855cSJani Nikula struct drm_i915_private; 353a7a92abSChris Wilson struct timer_list; 36358c855cSJani Nikula 3717f5d579SLyude Paul #define FDO_BUG_URL "https://gitlab.freedesktop.org/drm/intel/-/wikis/How-to-file-i915-bugs" 3817f5d579SLyude Paul 39f0d66153SMichal Wajdeczko #undef WARN_ON 40f0d66153SMichal Wajdeczko /* Many gcc seem to no see through this and fall over :( */ 41f0d66153SMichal Wajdeczko #if 0 42f0d66153SMichal Wajdeczko #define WARN_ON(x) ({ \ 43f0d66153SMichal Wajdeczko bool __i915_warn_cond = (x); \ 44f0d66153SMichal Wajdeczko if (__builtin_constant_p(__i915_warn_cond)) \ 45f0d66153SMichal Wajdeczko BUILD_BUG_ON(__i915_warn_cond); \ 46f0d66153SMichal Wajdeczko WARN(__i915_warn_cond, "WARN_ON(" #x ")"); }) 47f0d66153SMichal Wajdeczko #else 48f0d66153SMichal Wajdeczko #define WARN_ON(x) WARN((x), "%s", "WARN_ON(" __stringify(x) ")") 49f0d66153SMichal Wajdeczko #endif 50f0d66153SMichal Wajdeczko 51f0d66153SMichal Wajdeczko #undef WARN_ON_ONCE 52f0d66153SMichal Wajdeczko #define WARN_ON_ONCE(x) WARN_ONCE((x), "%s", "WARN_ON_ONCE(" __stringify(x) ")") 53f0d66153SMichal Wajdeczko 5457bdff48SLucas De Marchi #define MISSING_CASE(x) WARN(1, "Missing case (%s == %ld)\n", \ 5557bdff48SLucas De Marchi __stringify(x), (long)(x)) 56f0d66153SMichal Wajdeczko 57358c855cSJani Nikula void __printf(3, 4) 58358c855cSJani Nikula __i915_printk(struct drm_i915_private *dev_priv, const char *level, 59358c855cSJani Nikula const char *fmt, ...); 60358c855cSJani Nikula 61358c855cSJani Nikula #define i915_report_error(dev_priv, fmt, ...) \ 62358c855cSJani Nikula __i915_printk(dev_priv, KERN_ERR, fmt, ##__VA_ARGS__) 63358c855cSJani Nikula 64358c855cSJani Nikula #if IS_ENABLED(CONFIG_DRM_I915_DEBUG) 65358c855cSJani Nikula 66dd6e38dfSJanusz Krzysztofik int __i915_inject_probe_error(struct drm_i915_private *i915, int err, 67358c855cSJani Nikula const char *func, int line); 68dd6e38dfSJanusz Krzysztofik #define i915_inject_probe_error(_i915, _err) \ 69dd6e38dfSJanusz Krzysztofik __i915_inject_probe_error((_i915), (_err), __func__, __LINE__) 70358c855cSJani Nikula bool i915_error_injected(void); 71358c855cSJani Nikula 72358c855cSJani Nikula #else 73358c855cSJani Nikula 74b58a8813SMichal Wajdeczko #define i915_inject_probe_error(i915, e) ({ BUILD_BUG_ON_INVALID(i915); 0; }) 75358c855cSJani Nikula #define i915_error_injected() false 76358c855cSJani Nikula 77358c855cSJani Nikula #endif 78358c855cSJani Nikula 79dd6e38dfSJanusz Krzysztofik #define i915_inject_probe_failure(i915) i915_inject_probe_error((i915), -ENODEV) 80358c855cSJani Nikula 81358c855cSJani Nikula #define i915_probe_error(i915, fmt, ...) \ 82358c855cSJani Nikula __i915_printk(i915, i915_error_injected() ? KERN_DEBUG : KERN_ERR, \ 83358c855cSJani Nikula fmt, ##__VA_ARGS__) 84358c855cSJani Nikula 85815f0ddbSNick Desaulniers #if defined(GCC_VERSION) && GCC_VERSION >= 70000 8670bbe53cSVille Syrjälä #define add_overflows_t(T, A, B) \ 8770bbe53cSVille Syrjälä __builtin_add_overflow_p((A), (B), (T)0) 881692cd60SChris Wilson #else 8970bbe53cSVille Syrjälä #define add_overflows_t(T, A, B) ({ \ 901692cd60SChris Wilson typeof(A) a = (A); \ 911692cd60SChris Wilson typeof(B) b = (B); \ 9270bbe53cSVille Syrjälä (T)(a + b) < a; \ 931692cd60SChris Wilson }) 941692cd60SChris Wilson #endif 951692cd60SChris Wilson 9670bbe53cSVille Syrjälä #define add_overflows(A, B) \ 9770bbe53cSVille Syrjälä add_overflows_t(typeof((A) + (B)), (A), (B)) 9870bbe53cSVille Syrjälä 9940b326eeSChris Wilson #define range_overflows(start, size, max) ({ \ 10040b326eeSChris Wilson typeof(start) start__ = (start); \ 10140b326eeSChris Wilson typeof(size) size__ = (size); \ 10240b326eeSChris Wilson typeof(max) max__ = (max); \ 10340b326eeSChris Wilson (void)(&start__ == &size__); \ 10440b326eeSChris Wilson (void)(&start__ == &max__); \ 105520f8350SMatthew Auld start__ >= max__ || size__ > max__ - start__; \ 10640b326eeSChris Wilson }) 10740b326eeSChris Wilson 10840b326eeSChris Wilson #define range_overflows_t(type, start, size, max) \ 10940b326eeSChris Wilson range_overflows((type)(start), (type)(size), (type)(max)) 11040b326eeSChris Wilson 111520f8350SMatthew Auld #define range_overflows_end(start, size, max) ({ \ 112520f8350SMatthew Auld typeof(start) start__ = (start); \ 113520f8350SMatthew Auld typeof(size) size__ = (size); \ 114520f8350SMatthew Auld typeof(max) max__ = (max); \ 115520f8350SMatthew Auld (void)(&start__ == &size__); \ 116520f8350SMatthew Auld (void)(&start__ == &max__); \ 117520f8350SMatthew Auld start__ > max__ || size__ > max__ - start__; \ 118520f8350SMatthew Auld }) 119520f8350SMatthew Auld 120520f8350SMatthew Auld #define range_overflows_end_t(type, start, size, max) \ 121520f8350SMatthew Auld range_overflows_end((type)(start), (type)(size), (type)(max)) 122520f8350SMatthew Auld 12340b326eeSChris Wilson /* Note we don't consider signbits :| */ 12440b326eeSChris Wilson #define overflows_type(x, T) \ 12574f6e183SChris Wilson (sizeof(x) > sizeof(T) && (x) >> BITS_PER_TYPE(T)) 12640b326eeSChris Wilson 127976b55f0SChris Wilson static inline bool 128976b55f0SChris Wilson __check_struct_size(size_t base, size_t arr, size_t count, size_t *size) 129976b55f0SChris Wilson { 130976b55f0SChris Wilson size_t sz; 131976b55f0SChris Wilson 132976b55f0SChris Wilson if (check_mul_overflow(count, arr, &sz)) 133976b55f0SChris Wilson return false; 134976b55f0SChris Wilson 135976b55f0SChris Wilson if (check_add_overflow(sz, base, &sz)) 136976b55f0SChris Wilson return false; 137976b55f0SChris Wilson 138976b55f0SChris Wilson *size = sz; 139976b55f0SChris Wilson return true; 140976b55f0SChris Wilson } 141976b55f0SChris Wilson 142976b55f0SChris Wilson /** 143976b55f0SChris Wilson * check_struct_size() - Calculate size of structure with trailing array. 144976b55f0SChris Wilson * @p: Pointer to the structure. 145976b55f0SChris Wilson * @member: Name of the array member. 146976b55f0SChris Wilson * @n: Number of elements in the array. 147976b55f0SChris Wilson * @sz: Total size of structure and array 148976b55f0SChris Wilson * 149976b55f0SChris Wilson * Calculates size of memory needed for structure @p followed by an 150976b55f0SChris Wilson * array of @n @member elements, like struct_size() but reports 151976b55f0SChris Wilson * whether it overflowed, and the resultant size in @sz 152976b55f0SChris Wilson * 153976b55f0SChris Wilson * Return: false if the calculation overflowed. 154976b55f0SChris Wilson */ 155976b55f0SChris Wilson #define check_struct_size(p, member, n, sz) \ 156976b55f0SChris Wilson likely(__check_struct_size(sizeof(*(p)), \ 157976b55f0SChris Wilson sizeof(*(p)->member) + __must_be_array((p)->member), \ 158976b55f0SChris Wilson n, sz)) 159976b55f0SChris Wilson 1600ce81788SChris Wilson #define ptr_mask_bits(ptr, n) ({ \ 161b7163936SChris Wilson unsigned long __v = (unsigned long)(ptr); \ 1620ce81788SChris Wilson (typeof(ptr))(__v & -BIT(n)); \ 163b7163936SChris Wilson }) 164b7163936SChris Wilson 1650ce81788SChris Wilson #define ptr_unmask_bits(ptr, n) ((unsigned long)(ptr) & (BIT(n) - 1)) 1660ce81788SChris Wilson 1670ce81788SChris Wilson #define ptr_unpack_bits(ptr, bits, n) ({ \ 168b7163936SChris Wilson unsigned long __v = (unsigned long)(ptr); \ 1690ce81788SChris Wilson *(bits) = __v & (BIT(n) - 1); \ 1700ce81788SChris Wilson (typeof(ptr))(__v & -BIT(n)); \ 171b7163936SChris Wilson }) 172b7163936SChris Wilson 1732d751415STvrtko Ursulin #define ptr_pack_bits(ptr, bits, n) ({ \ 1742d751415STvrtko Ursulin unsigned long __bits = (bits); \ 1752d751415STvrtko Ursulin GEM_BUG_ON(__bits & -BIT(n)); \ 1762d751415STvrtko Ursulin ((typeof(ptr))((unsigned long)(ptr) | __bits)); \ 1772d751415STvrtko Ursulin }) 178b7163936SChris Wilson 179df403069SChris Wilson #define ptr_dec(ptr) ({ \ 180df403069SChris Wilson unsigned long __v = (unsigned long)(ptr); \ 181df403069SChris Wilson (typeof(ptr))(__v - 1); \ 182df403069SChris Wilson }) 18322b7a426SChris Wilson 184df403069SChris Wilson #define ptr_inc(ptr) ({ \ 185df403069SChris Wilson unsigned long __v = (unsigned long)(ptr); \ 186df403069SChris Wilson (typeof(ptr))(__v + 1); \ 187df403069SChris Wilson }) 18822b7a426SChris Wilson 1890ce81788SChris Wilson #define page_mask_bits(ptr) ptr_mask_bits(ptr, PAGE_SHIFT) 1900ce81788SChris Wilson #define page_unmask_bits(ptr) ptr_unmask_bits(ptr, PAGE_SHIFT) 1910ce81788SChris Wilson #define page_pack_bits(ptr, bits) ptr_pack_bits(ptr, bits, PAGE_SHIFT) 1920ce81788SChris Wilson #define page_unpack_bits(ptr, bits) ptr_unpack_bits(ptr, bits, PAGE_SHIFT) 1930ce81788SChris Wilson 1944547c255SImre Deak #define struct_member(T, member) (((T *)0)->member) 1954547c255SImre Deak 19616f11f46SMichal Wajdeczko #define ptr_offset(ptr, member) offsetof(typeof(*(ptr)), member) 19716f11f46SMichal Wajdeczko 198b7163936SChris Wilson #define fetch_and_zero(ptr) ({ \ 199b7163936SChris Wilson typeof(*ptr) __T = *(ptr); \ 200b7163936SChris Wilson *(ptr) = (typeof(*ptr))0; \ 201b7163936SChris Wilson __T; \ 202b7163936SChris Wilson }) 203b7163936SChris Wilson 204*d6e9c965SMichal Wajdeczko static __always_inline ptrdiff_t ptrdiff(const void *a, const void *b) 205*d6e9c965SMichal Wajdeczko { 206*d6e9c965SMichal Wajdeczko return a - b; 207*d6e9c965SMichal Wajdeczko } 208*d6e9c965SMichal Wajdeczko 2099d1305efSChris Wilson /* 2109d1305efSChris Wilson * container_of_user: Extract the superclass from a pointer to a member. 2119d1305efSChris Wilson * 2129d1305efSChris Wilson * Exactly like container_of() with the exception that it plays nicely 2139d1305efSChris Wilson * with sparse for __user @ptr. 2149d1305efSChris Wilson */ 2159d1305efSChris Wilson #define container_of_user(ptr, type, member) ({ \ 2169d1305efSChris Wilson void __user *__mptr = (void __user *)(ptr); \ 2174547c255SImre Deak BUILD_BUG_ON_MSG(!__same_type(*(ptr), struct_member(type, member)) && \ 2189d1305efSChris Wilson !__same_type(*(ptr), void), \ 2199d1305efSChris Wilson "pointer type mismatch in container_of()"); \ 2209d1305efSChris Wilson ((type __user *)(__mptr - offsetof(type, member))); }) 2219d1305efSChris Wilson 2229d1305efSChris Wilson /* 2239d1305efSChris Wilson * check_user_mbz: Check that a user value exists and is zero 2249d1305efSChris Wilson * 2259d1305efSChris Wilson * Frequently in our uABI we reserve space for future extensions, and 2269d1305efSChris Wilson * two ensure that userspace is prepared we enforce that space must 2279d1305efSChris Wilson * be zero. (Then any future extension can safely assume a default value 2289d1305efSChris Wilson * of 0.) 2299d1305efSChris Wilson * 2309d1305efSChris Wilson * check_user_mbz() combines checking that the user pointer is accessible 2319d1305efSChris Wilson * and that the contained value is zero. 2329d1305efSChris Wilson * 2339d1305efSChris Wilson * Returns: -EFAULT if not accessible, -EINVAL if !zero, or 0 on success. 2349d1305efSChris Wilson */ 2359d1305efSChris Wilson #define check_user_mbz(U) ({ \ 2369d1305efSChris Wilson typeof(*(U)) mbz__; \ 2379d1305efSChris Wilson get_user(mbz__, (U)) ? -EFAULT : mbz__ ? -EINVAL : 0; \ 2389d1305efSChris Wilson }) 2399d1305efSChris Wilson 240bb8920f5SMichal Wajdeczko static inline u64 ptr_to_u64(const void *ptr) 241bb8920f5SMichal Wajdeczko { 242bb8920f5SMichal Wajdeczko return (uintptr_t)ptr; 243bb8920f5SMichal Wajdeczko } 244bb8920f5SMichal Wajdeczko 2454ff4b44cSChris Wilson #define u64_to_ptr(T, x) ({ \ 2464ff4b44cSChris Wilson typecheck(u64, x); \ 2474ff4b44cSChris Wilson (T *)(uintptr_t)(x); \ 2484ff4b44cSChris Wilson }) 2494ff4b44cSChris Wilson 25016586fcdSMichal Wajdeczko #define __mask_next_bit(mask) ({ \ 25116586fcdSMichal Wajdeczko int __idx = ffs(mask) - 1; \ 25216586fcdSMichal Wajdeczko mask &= ~BIT(__idx); \ 25316586fcdSMichal Wajdeczko __idx; \ 25416586fcdSMichal Wajdeczko }) 25516586fcdSMichal Wajdeczko 2562920516bSMatthew Auld static inline bool is_power_of_2_u64(u64 n) 2572920516bSMatthew Auld { 2582920516bSMatthew Auld return (n != 0 && ((n & (n - 1)) == 0)); 2592920516bSMatthew Auld } 2602920516bSMatthew Auld 2616c067579SChris Wilson static inline void __list_del_many(struct list_head *head, 2626c067579SChris Wilson struct list_head *first) 2636c067579SChris Wilson { 2646c067579SChris Wilson first->prev = head; 2656c067579SChris Wilson WRITE_ONCE(head->next, first); 2666c067579SChris Wilson } 2676c067579SChris Wilson 268875c3b4bSChris Wilson static inline int list_is_last_rcu(const struct list_head *list, 269875c3b4bSChris Wilson const struct list_head *head) 270875c3b4bSChris Wilson { 271875c3b4bSChris Wilson return READ_ONCE(list->next) == head; 272875c3b4bSChris Wilson } 273875c3b4bSChris Wilson 274b30ed4ccSJani Nikula static inline unsigned long msecs_to_jiffies_timeout(const unsigned int m) 275b30ed4ccSJani Nikula { 276b30ed4ccSJani Nikula unsigned long j = msecs_to_jiffies(m); 277b30ed4ccSJani Nikula 278b30ed4ccSJani Nikula return min_t(unsigned long, MAX_JIFFY_OFFSET, j + 1); 279b30ed4ccSJani Nikula } 280b30ed4ccSJani Nikula 281b30ed4ccSJani Nikula /* 282b30ed4ccSJani Nikula * If you need to wait X milliseconds between events A and B, but event B 283b30ed4ccSJani Nikula * doesn't happen exactly after event A, you record the timestamp (jiffies) of 284b30ed4ccSJani Nikula * when event A happened, then just before event B you call this function and 285b30ed4ccSJani Nikula * pass the timestamp as the first argument, and X as the second argument. 286b30ed4ccSJani Nikula */ 287b30ed4ccSJani Nikula static inline void 288b30ed4ccSJani Nikula wait_remaining_ms_from_jiffies(unsigned long timestamp_jiffies, int to_wait_ms) 289b30ed4ccSJani Nikula { 290b30ed4ccSJani Nikula unsigned long target_jiffies, tmp_jiffies, remaining_jiffies; 291b30ed4ccSJani Nikula 292b30ed4ccSJani Nikula /* 293b30ed4ccSJani Nikula * Don't re-read the value of "jiffies" every time since it may change 294b30ed4ccSJani Nikula * behind our back and break the math. 295b30ed4ccSJani Nikula */ 296b30ed4ccSJani Nikula tmp_jiffies = jiffies; 297b30ed4ccSJani Nikula target_jiffies = timestamp_jiffies + 298b30ed4ccSJani Nikula msecs_to_jiffies_timeout(to_wait_ms); 299b30ed4ccSJani Nikula 300b30ed4ccSJani Nikula if (time_after(target_jiffies, tmp_jiffies)) { 301b30ed4ccSJani Nikula remaining_jiffies = target_jiffies - tmp_jiffies; 302b30ed4ccSJani Nikula while (remaining_jiffies) 303b30ed4ccSJani Nikula remaining_jiffies = 304b30ed4ccSJani Nikula schedule_timeout_uninterruptible(remaining_jiffies); 305b30ed4ccSJani Nikula } 306b30ed4ccSJani Nikula } 307b30ed4ccSJani Nikula 308b30ed4ccSJani Nikula /** 309b30ed4ccSJani Nikula * __wait_for - magic wait macro 310b30ed4ccSJani Nikula * 311b30ed4ccSJani Nikula * Macro to help avoid open coding check/wait/timeout patterns. Note that it's 312b30ed4ccSJani Nikula * important that we check the condition again after having timed out, since the 313b30ed4ccSJani Nikula * timeout could be due to preemption or similar and we've never had a chance to 314b30ed4ccSJani Nikula * check the condition before the timeout. 315b30ed4ccSJani Nikula */ 316b30ed4ccSJani Nikula #define __wait_for(OP, COND, US, Wmin, Wmax) ({ \ 317b30ed4ccSJani Nikula const ktime_t end__ = ktime_add_ns(ktime_get_raw(), 1000ll * (US)); \ 318b30ed4ccSJani Nikula long wait__ = (Wmin); /* recommended min for usleep is 10 us */ \ 319b30ed4ccSJani Nikula int ret__; \ 320b30ed4ccSJani Nikula might_sleep(); \ 321b30ed4ccSJani Nikula for (;;) { \ 322b30ed4ccSJani Nikula const bool expired__ = ktime_after(ktime_get_raw(), end__); \ 323b30ed4ccSJani Nikula OP; \ 324b30ed4ccSJani Nikula /* Guarantee COND check prior to timeout */ \ 325b30ed4ccSJani Nikula barrier(); \ 326b30ed4ccSJani Nikula if (COND) { \ 327b30ed4ccSJani Nikula ret__ = 0; \ 328b30ed4ccSJani Nikula break; \ 329b30ed4ccSJani Nikula } \ 330b30ed4ccSJani Nikula if (expired__) { \ 331b30ed4ccSJani Nikula ret__ = -ETIMEDOUT; \ 332b30ed4ccSJani Nikula break; \ 333b30ed4ccSJani Nikula } \ 334b30ed4ccSJani Nikula usleep_range(wait__, wait__ * 2); \ 335b30ed4ccSJani Nikula if (wait__ < (Wmax)) \ 336b30ed4ccSJani Nikula wait__ <<= 1; \ 337b30ed4ccSJani Nikula } \ 338b30ed4ccSJani Nikula ret__; \ 339b30ed4ccSJani Nikula }) 340b30ed4ccSJani Nikula 341b30ed4ccSJani Nikula #define _wait_for(COND, US, Wmin, Wmax) __wait_for(, (COND), (US), (Wmin), \ 342b30ed4ccSJani Nikula (Wmax)) 343b30ed4ccSJani Nikula #define wait_for(COND, MS) _wait_for((COND), (MS) * 1000, 10, 1000) 344b30ed4ccSJani Nikula 345b30ed4ccSJani Nikula /* If CONFIG_PREEMPT_COUNT is disabled, in_atomic() always reports false. */ 346b30ed4ccSJani Nikula #if defined(CONFIG_DRM_I915_DEBUG) && defined(CONFIG_PREEMPT_COUNT) 347b30ed4ccSJani Nikula # define _WAIT_FOR_ATOMIC_CHECK(ATOMIC) WARN_ON_ONCE((ATOMIC) && !in_atomic()) 348b30ed4ccSJani Nikula #else 349b30ed4ccSJani Nikula # define _WAIT_FOR_ATOMIC_CHECK(ATOMIC) do { } while (0) 350b30ed4ccSJani Nikula #endif 351b30ed4ccSJani Nikula 352b30ed4ccSJani Nikula #define _wait_for_atomic(COND, US, ATOMIC) \ 353b30ed4ccSJani Nikula ({ \ 354b30ed4ccSJani Nikula int cpu, ret, timeout = (US) * 1000; \ 355b30ed4ccSJani Nikula u64 base; \ 356b30ed4ccSJani Nikula _WAIT_FOR_ATOMIC_CHECK(ATOMIC); \ 357b30ed4ccSJani Nikula if (!(ATOMIC)) { \ 358b30ed4ccSJani Nikula preempt_disable(); \ 359b30ed4ccSJani Nikula cpu = smp_processor_id(); \ 360b30ed4ccSJani Nikula } \ 361b30ed4ccSJani Nikula base = local_clock(); \ 362b30ed4ccSJani Nikula for (;;) { \ 363b30ed4ccSJani Nikula u64 now = local_clock(); \ 364b30ed4ccSJani Nikula if (!(ATOMIC)) \ 365b30ed4ccSJani Nikula preempt_enable(); \ 366b30ed4ccSJani Nikula /* Guarantee COND check prior to timeout */ \ 367b30ed4ccSJani Nikula barrier(); \ 368b30ed4ccSJani Nikula if (COND) { \ 369b30ed4ccSJani Nikula ret = 0; \ 370b30ed4ccSJani Nikula break; \ 371b30ed4ccSJani Nikula } \ 372b30ed4ccSJani Nikula if (now - base >= timeout) { \ 373b30ed4ccSJani Nikula ret = -ETIMEDOUT; \ 374b30ed4ccSJani Nikula break; \ 375b30ed4ccSJani Nikula } \ 376b30ed4ccSJani Nikula cpu_relax(); \ 377b30ed4ccSJani Nikula if (!(ATOMIC)) { \ 378b30ed4ccSJani Nikula preempt_disable(); \ 379b30ed4ccSJani Nikula if (unlikely(cpu != smp_processor_id())) { \ 380b30ed4ccSJani Nikula timeout -= now - base; \ 381b30ed4ccSJani Nikula cpu = smp_processor_id(); \ 382b30ed4ccSJani Nikula base = local_clock(); \ 383b30ed4ccSJani Nikula } \ 384b30ed4ccSJani Nikula } \ 385b30ed4ccSJani Nikula } \ 386b30ed4ccSJani Nikula ret; \ 387b30ed4ccSJani Nikula }) 388b30ed4ccSJani Nikula 389b30ed4ccSJani Nikula #define wait_for_us(COND, US) \ 390b30ed4ccSJani Nikula ({ \ 391b30ed4ccSJani Nikula int ret__; \ 392b30ed4ccSJani Nikula BUILD_BUG_ON(!__builtin_constant_p(US)); \ 393b30ed4ccSJani Nikula if ((US) > 10) \ 394b30ed4ccSJani Nikula ret__ = _wait_for((COND), (US), 10, 10); \ 395b30ed4ccSJani Nikula else \ 396b30ed4ccSJani Nikula ret__ = _wait_for_atomic((COND), (US), 0); \ 397b30ed4ccSJani Nikula ret__; \ 398b30ed4ccSJani Nikula }) 399b30ed4ccSJani Nikula 400b30ed4ccSJani Nikula #define wait_for_atomic_us(COND, US) \ 401b30ed4ccSJani Nikula ({ \ 402b30ed4ccSJani Nikula BUILD_BUG_ON(!__builtin_constant_p(US)); \ 403b30ed4ccSJani Nikula BUILD_BUG_ON((US) > 50000); \ 404b30ed4ccSJani Nikula _wait_for_atomic((COND), (US), 1); \ 405b30ed4ccSJani Nikula }) 406b30ed4ccSJani Nikula 407b30ed4ccSJani Nikula #define wait_for_atomic(COND, MS) wait_for_atomic_us((COND), (MS) * 1000) 408b30ed4ccSJani Nikula 409b30ed4ccSJani Nikula #define KHz(x) (1000 * (x)) 410b30ed4ccSJani Nikula #define MHz(x) KHz(1000 * (x)) 411b30ed4ccSJani Nikula 412b30ed4ccSJani Nikula #define KBps(x) (1000 * (x)) 413b30ed4ccSJani Nikula #define MBps(x) KBps(1000 * (x)) 414b30ed4ccSJani Nikula #define GBps(x) ((u64)1000 * MBps((x))) 415b30ed4ccSJani Nikula 416b74eeeb6SMichal Wajdeczko static inline const char *yesno(bool v) 417b74eeeb6SMichal Wajdeczko { 418b74eeeb6SMichal Wajdeczko return v ? "yes" : "no"; 419b74eeeb6SMichal Wajdeczko } 420b74eeeb6SMichal Wajdeczko 421b74eeeb6SMichal Wajdeczko static inline const char *onoff(bool v) 422b74eeeb6SMichal Wajdeczko { 423b74eeeb6SMichal Wajdeczko return v ? "on" : "off"; 424b74eeeb6SMichal Wajdeczko } 425b74eeeb6SMichal Wajdeczko 4260868b1ceSVille Syrjälä static inline const char *enabledisable(bool v) 4270868b1ceSVille Syrjälä { 4280868b1ceSVille Syrjälä return v ? "enable" : "disable"; 4290868b1ceSVille Syrjälä } 4300868b1ceSVille Syrjälä 431b74eeeb6SMichal Wajdeczko static inline const char *enableddisabled(bool v) 432b74eeeb6SMichal Wajdeczko { 433b74eeeb6SMichal Wajdeczko return v ? "enabled" : "disabled"; 434b74eeeb6SMichal Wajdeczko } 435b74eeeb6SMichal Wajdeczko 43665706203SMichał Winiarski void add_taint_for_CI(struct drm_i915_private *i915, unsigned int taint); 43765706203SMichał Winiarski static inline void __add_taint_for_CI(unsigned int taint) 438e3adffe8SJani Nikula { 439e3adffe8SJani Nikula /* 440e3adffe8SJani Nikula * The system is "ok", just about surviving for the user, but 441e3adffe8SJani Nikula * CI results are now unreliable as the HW is very suspect. 442e3adffe8SJani Nikula * CI checks the taint state after every test and will reboot 443e3adffe8SJani Nikula * the machine if the kernel is tainted. 444e3adffe8SJani Nikula */ 445e3adffe8SJani Nikula add_taint(taint, LOCKDEP_STILL_OK); 446e3adffe8SJani Nikula } 447e3adffe8SJani Nikula 4483a7a92abSChris Wilson void cancel_timer(struct timer_list *t); 4493a7a92abSChris Wilson void set_timer_ms(struct timer_list *t, unsigned long timeout); 4503a7a92abSChris Wilson 451c185a16eSChris Wilson static inline bool timer_active(const struct timer_list *t) 452c185a16eSChris Wilson { 453c185a16eSChris Wilson return READ_ONCE(t->expires); 454c185a16eSChris Wilson } 455c185a16eSChris Wilson 4563a7a92abSChris Wilson static inline bool timer_expired(const struct timer_list *t) 4573a7a92abSChris Wilson { 458c185a16eSChris Wilson return timer_active(t) && !timer_pending(t); 4593a7a92abSChris Wilson } 4603a7a92abSChris Wilson 461babaab2fSChris Wilson /* 462babaab2fSChris Wilson * This is a lookalike for IS_ENABLED() that takes a kconfig value, 463babaab2fSChris Wilson * e.g. CONFIG_DRM_I915_SPIN_REQUEST, and evaluates whether it is non-zero 464babaab2fSChris Wilson * i.e. whether the configuration is active. Wrapping up the config inside 465babaab2fSChris Wilson * a boolean context prevents clang and smatch from complaining about potential 466babaab2fSChris Wilson * issues in confusing logical-&& with bitwise-& for constants. 467babaab2fSChris Wilson * 468babaab2fSChris Wilson * Sadly IS_ENABLED() itself does not work with kconfig values. 469babaab2fSChris Wilson * 470babaab2fSChris Wilson * Returns 0 if @config is 0, 1 if set to any value. 471babaab2fSChris Wilson */ 472babaab2fSChris Wilson #define IS_ACTIVE(config) ((config) != 0) 473babaab2fSChris Wilson 47440b326eeSChris Wilson #endif /* !__I915_UTILS_H */ 475