1 1.6 riastrad /* $NetBSD: i915_utils.h,v 1.6 2022/05/27 21:02:27 riastradh Exp $ */ 2 1.1 riastrad 3 1.1 riastrad /* 4 1.1 riastrad * Copyright 2016 Intel Corporation 5 1.1 riastrad * 6 1.1 riastrad * Permission is hereby granted, free of charge, to any person obtaining a 7 1.1 riastrad * copy of this software and associated documentation files (the "Software"), 8 1.1 riastrad * to deal in the Software without restriction, including without limitation 9 1.1 riastrad * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 1.1 riastrad * and/or sell copies of the Software, and to permit persons to whom the 11 1.1 riastrad * Software is furnished to do so, subject to the following conditions: 12 1.1 riastrad * 13 1.1 riastrad * The above copyright notice and this permission notice (including the next 14 1.1 riastrad * paragraph) shall be included in all copies or substantial portions of the 15 1.1 riastrad * Software. 16 1.1 riastrad * 17 1.1 riastrad * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 1.1 riastrad * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 1.1 riastrad * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 20 1.1 riastrad * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 1.1 riastrad * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 22 1.1 riastrad * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 23 1.1 riastrad * IN THE SOFTWARE. 24 1.1 riastrad * 25 1.1 riastrad */ 26 1.1 riastrad 27 1.1 riastrad #ifndef __I915_UTILS_H 28 1.1 riastrad #define __I915_UTILS_H 29 1.1 riastrad 30 1.1 riastrad #include <linux/list.h> 31 1.1 riastrad #include <linux/overflow.h> 32 1.1 riastrad #include <linux/sched.h> 33 1.5 riastrad #include <linux/sched/clock.h> 34 1.1 riastrad #include <linux/types.h> 35 1.1 riastrad #include <linux/workqueue.h> 36 1.1 riastrad 37 1.1 riastrad struct drm_i915_private; 38 1.1 riastrad struct timer_list; 39 1.1 riastrad 40 1.1 riastrad #undef WARN_ON 41 1.1 riastrad /* Many gcc seem to no see through this and fall over :( */ 42 1.1 riastrad #if 0 43 1.1 riastrad #define WARN_ON(x) ({ \ 44 1.1 riastrad bool __i915_warn_cond = (x); \ 45 1.1 riastrad if (__builtin_constant_p(__i915_warn_cond)) \ 46 1.1 riastrad BUILD_BUG_ON(__i915_warn_cond); \ 47 1.6 riastrad WARN(__i915_warn_cond, "WARN_ON(" #x ")\n"); }) 48 1.1 riastrad #else 49 1.6 riastrad #define WARN_ON(x) WARN((x), "%s\n", "WARN_ON(" __stringify(x) ")") 50 1.1 riastrad #endif 51 1.1 riastrad 52 1.1 riastrad #undef WARN_ON_ONCE 53 1.6 riastrad #define WARN_ON_ONCE(x) WARN_ONCE((x), "%s", "WARN_ON_ONCE(" __stringify(x) ")\n") 54 1.1 riastrad 55 1.1 riastrad #define MISSING_CASE(x) WARN(1, "Missing case (%s == %ld)\n", \ 56 1.1 riastrad __stringify(x), (long)(x)) 57 1.1 riastrad 58 1.1 riastrad void __printf(3, 4) 59 1.1 riastrad __i915_printk(struct drm_i915_private *dev_priv, const char *level, 60 1.1 riastrad const char *fmt, ...); 61 1.1 riastrad 62 1.1 riastrad #define i915_report_error(dev_priv, fmt, ...) \ 63 1.1 riastrad __i915_printk(dev_priv, KERN_ERR, fmt, ##__VA_ARGS__) 64 1.1 riastrad 65 1.1 riastrad #if IS_ENABLED(CONFIG_DRM_I915_DEBUG) 66 1.1 riastrad 67 1.1 riastrad int __i915_inject_probe_error(struct drm_i915_private *i915, int err, 68 1.1 riastrad const char *func, int line); 69 1.1 riastrad #define i915_inject_probe_error(_i915, _err) \ 70 1.1 riastrad __i915_inject_probe_error((_i915), (_err), __func__, __LINE__) 71 1.1 riastrad bool i915_error_injected(void); 72 1.1 riastrad 73 1.1 riastrad #else 74 1.1 riastrad 75 1.1 riastrad #define i915_inject_probe_error(i915, e) ({ BUILD_BUG_ON_INVALID(i915); 0; }) 76 1.1 riastrad #define i915_error_injected() false 77 1.1 riastrad 78 1.1 riastrad #endif 79 1.1 riastrad 80 1.1 riastrad #define i915_inject_probe_failure(i915) i915_inject_probe_error((i915), -ENODEV) 81 1.1 riastrad 82 1.1 riastrad #define i915_probe_error(i915, fmt, ...) \ 83 1.1 riastrad __i915_printk(i915, i915_error_injected() ? KERN_DEBUG : KERN_ERR, \ 84 1.1 riastrad fmt, ##__VA_ARGS__) 85 1.1 riastrad 86 1.1 riastrad #if defined(GCC_VERSION) && GCC_VERSION >= 70000 87 1.1 riastrad #define add_overflows_t(T, A, B) \ 88 1.1 riastrad __builtin_add_overflow_p((A), (B), (T)0) 89 1.1 riastrad #else 90 1.1 riastrad #define add_overflows_t(T, A, B) ({ \ 91 1.1 riastrad typeof(A) a = (A); \ 92 1.1 riastrad typeof(B) b = (B); \ 93 1.1 riastrad (T)(a + b) < a; \ 94 1.1 riastrad }) 95 1.1 riastrad #endif 96 1.1 riastrad 97 1.1 riastrad #define add_overflows(A, B) \ 98 1.1 riastrad add_overflows_t(typeof((A) + (B)), (A), (B)) 99 1.1 riastrad 100 1.1 riastrad #define range_overflows(start, size, max) ({ \ 101 1.1 riastrad typeof(start) start__ = (start); \ 102 1.1 riastrad typeof(size) size__ = (size); \ 103 1.1 riastrad typeof(max) max__ = (max); \ 104 1.1 riastrad (void)(&start__ == &size__); \ 105 1.1 riastrad (void)(&start__ == &max__); \ 106 1.1 riastrad start__ > max__ || size__ > max__ - start__; \ 107 1.1 riastrad }) 108 1.1 riastrad 109 1.1 riastrad #define range_overflows_t(type, start, size, max) \ 110 1.1 riastrad range_overflows((type)(start), (type)(size), (type)(max)) 111 1.1 riastrad 112 1.1 riastrad /* Note we don't consider signbits :| */ 113 1.1 riastrad #define overflows_type(x, T) \ 114 1.1 riastrad (sizeof(x) > sizeof(T) && (x) >> BITS_PER_TYPE(T)) 115 1.1 riastrad 116 1.1 riastrad static inline bool 117 1.1 riastrad __check_struct_size(size_t base, size_t arr, size_t count, size_t *size) 118 1.1 riastrad { 119 1.1 riastrad size_t sz; 120 1.1 riastrad 121 1.1 riastrad if (check_mul_overflow(count, arr, &sz)) 122 1.1 riastrad return false; 123 1.1 riastrad 124 1.1 riastrad if (check_add_overflow(sz, base, &sz)) 125 1.1 riastrad return false; 126 1.1 riastrad 127 1.1 riastrad *size = sz; 128 1.1 riastrad return true; 129 1.1 riastrad } 130 1.1 riastrad 131 1.1 riastrad /** 132 1.1 riastrad * check_struct_size() - Calculate size of structure with trailing array. 133 1.1 riastrad * @p: Pointer to the structure. 134 1.1 riastrad * @member: Name of the array member. 135 1.1 riastrad * @n: Number of elements in the array. 136 1.1 riastrad * @sz: Total size of structure and array 137 1.1 riastrad * 138 1.1 riastrad * Calculates size of memory needed for structure @p followed by an 139 1.1 riastrad * array of @n @member elements, like struct_size() but reports 140 1.1 riastrad * whether it overflowed, and the resultant size in @sz 141 1.1 riastrad * 142 1.1 riastrad * Return: false if the calculation overflowed. 143 1.1 riastrad */ 144 1.1 riastrad #define check_struct_size(p, member, n, sz) \ 145 1.1 riastrad likely(__check_struct_size(sizeof(*(p)), \ 146 1.1 riastrad sizeof(*(p)->member) + __must_be_array((p)->member), \ 147 1.1 riastrad n, sz)) 148 1.1 riastrad 149 1.1 riastrad #define ptr_mask_bits(ptr, n) ({ \ 150 1.1 riastrad unsigned long __v = (unsigned long)(ptr); \ 151 1.1 riastrad (typeof(ptr))(__v & -BIT(n)); \ 152 1.1 riastrad }) 153 1.1 riastrad 154 1.1 riastrad #define ptr_unmask_bits(ptr, n) ((unsigned long)(ptr) & (BIT(n) - 1)) 155 1.1 riastrad 156 1.1 riastrad #define ptr_unpack_bits(ptr, bits, n) ({ \ 157 1.1 riastrad unsigned long __v = (unsigned long)(ptr); \ 158 1.1 riastrad *(bits) = __v & (BIT(n) - 1); \ 159 1.1 riastrad (typeof(ptr))(__v & -BIT(n)); \ 160 1.1 riastrad }) 161 1.1 riastrad 162 1.1 riastrad #define ptr_pack_bits(ptr, bits, n) ({ \ 163 1.1 riastrad unsigned long __bits = (bits); \ 164 1.1 riastrad GEM_BUG_ON(__bits & -BIT(n)); \ 165 1.1 riastrad ((typeof(ptr))((unsigned long)(ptr) | __bits)); \ 166 1.1 riastrad }) 167 1.1 riastrad 168 1.1 riastrad #define ptr_dec(ptr) ({ \ 169 1.1 riastrad unsigned long __v = (unsigned long)(ptr); \ 170 1.1 riastrad (typeof(ptr))(__v - 1); \ 171 1.1 riastrad }) 172 1.1 riastrad 173 1.1 riastrad #define ptr_inc(ptr) ({ \ 174 1.1 riastrad unsigned long __v = (unsigned long)(ptr); \ 175 1.1 riastrad (typeof(ptr))(__v + 1); \ 176 1.1 riastrad }) 177 1.1 riastrad 178 1.1 riastrad #define page_mask_bits(ptr) ptr_mask_bits(ptr, PAGE_SHIFT) 179 1.1 riastrad #define page_unmask_bits(ptr) ptr_unmask_bits(ptr, PAGE_SHIFT) 180 1.1 riastrad #define page_pack_bits(ptr, bits) ptr_pack_bits(ptr, bits, PAGE_SHIFT) 181 1.1 riastrad #define page_unpack_bits(ptr, bits) ptr_unpack_bits(ptr, bits, PAGE_SHIFT) 182 1.1 riastrad 183 1.1 riastrad #define struct_member(T, member) (((T *)0)->member) 184 1.1 riastrad 185 1.1 riastrad #define ptr_offset(ptr, member) offsetof(typeof(*(ptr)), member) 186 1.1 riastrad 187 1.1 riastrad #define fetch_and_zero(ptr) ({ \ 188 1.1 riastrad typeof(*ptr) __T = *(ptr); \ 189 1.1 riastrad *(ptr) = (typeof(*ptr))0; \ 190 1.1 riastrad __T; \ 191 1.1 riastrad }) 192 1.1 riastrad 193 1.1 riastrad /* 194 1.1 riastrad * container_of_user: Extract the superclass from a pointer to a member. 195 1.1 riastrad * 196 1.1 riastrad * Exactly like container_of() with the exception that it plays nicely 197 1.1 riastrad * with sparse for __user @ptr. 198 1.1 riastrad */ 199 1.1 riastrad #define container_of_user(ptr, type, member) ({ \ 200 1.1 riastrad void __user *__mptr = (void __user *)(ptr); \ 201 1.1 riastrad BUILD_BUG_ON_MSG(!__same_type(*(ptr), struct_member(type, member)) && \ 202 1.1 riastrad !__same_type(*(ptr), void), \ 203 1.1 riastrad "pointer type mismatch in container_of()"); \ 204 1.1 riastrad ((type __user *)(__mptr - offsetof(type, member))); }) 205 1.1 riastrad 206 1.1 riastrad /* 207 1.1 riastrad * check_user_mbz: Check that a user value exists and is zero 208 1.1 riastrad * 209 1.1 riastrad * Frequently in our uABI we reserve space for future extensions, and 210 1.1 riastrad * two ensure that userspace is prepared we enforce that space must 211 1.1 riastrad * be zero. (Then any future extension can safely assume a default value 212 1.1 riastrad * of 0.) 213 1.1 riastrad * 214 1.1 riastrad * check_user_mbz() combines checking that the user pointer is accessible 215 1.1 riastrad * and that the contained value is zero. 216 1.1 riastrad * 217 1.1 riastrad * Returns: -EFAULT if not accessible, -EINVAL if !zero, or 0 on success. 218 1.1 riastrad */ 219 1.1 riastrad #define check_user_mbz(U) ({ \ 220 1.1 riastrad typeof(*(U)) mbz__; \ 221 1.1 riastrad get_user(mbz__, (U)) ? -EFAULT : mbz__ ? -EINVAL : 0; \ 222 1.1 riastrad }) 223 1.1 riastrad 224 1.1 riastrad static inline u64 ptr_to_u64(const void *ptr) 225 1.1 riastrad { 226 1.1 riastrad return (uintptr_t)ptr; 227 1.1 riastrad } 228 1.1 riastrad 229 1.1 riastrad #define u64_to_ptr(T, x) ({ \ 230 1.1 riastrad typecheck(u64, x); \ 231 1.1 riastrad (T *)(uintptr_t)(x); \ 232 1.1 riastrad }) 233 1.1 riastrad 234 1.1 riastrad #define __mask_next_bit(mask) ({ \ 235 1.1 riastrad int __idx = ffs(mask) - 1; \ 236 1.1 riastrad mask &= ~BIT(__idx); \ 237 1.1 riastrad __idx; \ 238 1.1 riastrad }) 239 1.1 riastrad 240 1.1 riastrad static inline void __list_del_many(struct list_head *head, 241 1.1 riastrad struct list_head *first) 242 1.1 riastrad { 243 1.1 riastrad first->prev = head; 244 1.1 riastrad WRITE_ONCE(head->next, first); 245 1.1 riastrad } 246 1.1 riastrad 247 1.1 riastrad /* 248 1.1 riastrad * Wait until the work is finally complete, even if it tries to postpone 249 1.1 riastrad * by requeueing itself. Note, that if the worker never cancels itself, 250 1.1 riastrad * we will spin forever. 251 1.1 riastrad */ 252 1.1 riastrad static inline void drain_delayed_work(struct delayed_work *dw) 253 1.1 riastrad { 254 1.1 riastrad do { 255 1.1 riastrad while (flush_delayed_work(dw)) 256 1.1 riastrad ; 257 1.1 riastrad } while (delayed_work_pending(dw)); 258 1.1 riastrad } 259 1.1 riastrad 260 1.1 riastrad static inline unsigned long msecs_to_jiffies_timeout(const unsigned int m) 261 1.1 riastrad { 262 1.1 riastrad unsigned long j = msecs_to_jiffies(m); 263 1.1 riastrad 264 1.1 riastrad return min_t(unsigned long, MAX_JIFFY_OFFSET, j + 1); 265 1.1 riastrad } 266 1.1 riastrad 267 1.1 riastrad /* 268 1.1 riastrad * If you need to wait X milliseconds between events A and B, but event B 269 1.1 riastrad * doesn't happen exactly after event A, you record the timestamp (jiffies) of 270 1.1 riastrad * when event A happened, then just before event B you call this function and 271 1.1 riastrad * pass the timestamp as the first argument, and X as the second argument. 272 1.1 riastrad */ 273 1.1 riastrad static inline void 274 1.1 riastrad wait_remaining_ms_from_jiffies(unsigned long timestamp_jiffies, int to_wait_ms) 275 1.1 riastrad { 276 1.1 riastrad unsigned long target_jiffies, tmp_jiffies, remaining_jiffies; 277 1.1 riastrad 278 1.1 riastrad /* 279 1.1 riastrad * Don't re-read the value of "jiffies" every time since it may change 280 1.1 riastrad * behind our back and break the math. 281 1.1 riastrad */ 282 1.1 riastrad tmp_jiffies = jiffies; 283 1.1 riastrad target_jiffies = timestamp_jiffies + 284 1.1 riastrad msecs_to_jiffies_timeout(to_wait_ms); 285 1.1 riastrad 286 1.1 riastrad if (time_after(target_jiffies, tmp_jiffies)) { 287 1.1 riastrad remaining_jiffies = target_jiffies - tmp_jiffies; 288 1.1 riastrad while (remaining_jiffies) 289 1.1 riastrad remaining_jiffies = 290 1.1 riastrad schedule_timeout_uninterruptible(remaining_jiffies); 291 1.1 riastrad } 292 1.1 riastrad } 293 1.1 riastrad 294 1.1 riastrad /** 295 1.1 riastrad * __wait_for - magic wait macro 296 1.1 riastrad * 297 1.1 riastrad * Macro to help avoid open coding check/wait/timeout patterns. Note that it's 298 1.1 riastrad * important that we check the condition again after having timed out, since the 299 1.1 riastrad * timeout could be due to preemption or similar and we've never had a chance to 300 1.1 riastrad * check the condition before the timeout. 301 1.1 riastrad */ 302 1.2 riastrad #ifdef __NetBSD__ 303 1.5 riastrad #define __wait_for(OP, COND, US, Wmin, Wmax) ({ \ 304 1.2 riastrad int ret__ = 0; \ 305 1.2 riastrad if (cold) { \ 306 1.5 riastrad int ms__ = ((US) + 999)/1000; \ 307 1.5 riastrad for (;;) { \ 308 1.5 riastrad const bool expired__ = ms__-- == 0; \ 309 1.4 riastrad OP; \ 310 1.4 riastrad barrier(); \ 311 1.5 riastrad if (COND) { \ 312 1.5 riastrad ret__ = 0; \ 313 1.5 riastrad break; \ 314 1.5 riastrad } \ 315 1.5 riastrad if (expired__) { \ 316 1.5 riastrad ret__ = -ETIMEDOUT; \ 317 1.2 riastrad break; \ 318 1.2 riastrad } \ 319 1.2 riastrad DELAY(1000); \ 320 1.2 riastrad } \ 321 1.2 riastrad } else { \ 322 1.5 riastrad const ktime_t end__ = \ 323 1.5 riastrad ktime_add_ns(ktime_get_raw(), 1000ll * (US)); \ 324 1.5 riastrad long wait__ = (Wmin); \ 325 1.5 riastrad might_sleep(); \ 326 1.5 riastrad for (;;) { \ 327 1.5 riastrad const bool expired__ = \ 328 1.5 riastrad ktime_after(ktime_get_raw(), end__); \ 329 1.4 riastrad OP; \ 330 1.4 riastrad /* Guarantee COND check prior to timeout */ \ 331 1.4 riastrad barrier(); \ 332 1.5 riastrad if (COND) { \ 333 1.5 riastrad ret__ = 0; \ 334 1.2 riastrad break; \ 335 1.2 riastrad } \ 336 1.5 riastrad if (expired__) { \ 337 1.5 riastrad ret__ = -ETIMEDOUT; \ 338 1.5 riastrad break; \ 339 1.2 riastrad } \ 340 1.5 riastrad usleep_range(wait__, wait__ * 2); \ 341 1.5 riastrad if (wait__ < (Wmax)) \ 342 1.5 riastrad wait__ <<= 1; \ 343 1.2 riastrad } \ 344 1.2 riastrad } \ 345 1.2 riastrad ret__; \ 346 1.2 riastrad }) 347 1.2 riastrad #else /* !NetBSD */ 348 1.1 riastrad #define __wait_for(OP, COND, US, Wmin, Wmax) ({ \ 349 1.1 riastrad const ktime_t end__ = ktime_add_ns(ktime_get_raw(), 1000ll * (US)); \ 350 1.1 riastrad long wait__ = (Wmin); /* recommended min for usleep is 10 us */ \ 351 1.1 riastrad int ret__; \ 352 1.1 riastrad might_sleep(); \ 353 1.1 riastrad for (;;) { \ 354 1.1 riastrad const bool expired__ = ktime_after(ktime_get_raw(), end__); \ 355 1.1 riastrad OP; \ 356 1.1 riastrad /* Guarantee COND check prior to timeout */ \ 357 1.1 riastrad barrier(); \ 358 1.1 riastrad if (COND) { \ 359 1.1 riastrad ret__ = 0; \ 360 1.1 riastrad break; \ 361 1.1 riastrad } \ 362 1.1 riastrad if (expired__) { \ 363 1.1 riastrad ret__ = -ETIMEDOUT; \ 364 1.1 riastrad break; \ 365 1.1 riastrad } \ 366 1.1 riastrad usleep_range(wait__, wait__ * 2); \ 367 1.1 riastrad if (wait__ < (Wmax)) \ 368 1.1 riastrad wait__ <<= 1; \ 369 1.1 riastrad } \ 370 1.1 riastrad ret__; \ 371 1.1 riastrad }) 372 1.2 riastrad #endif 373 1.1 riastrad 374 1.1 riastrad #define _wait_for(COND, US, Wmin, Wmax) __wait_for(, (COND), (US), (Wmin), \ 375 1.1 riastrad (Wmax)) 376 1.1 riastrad #define wait_for(COND, MS) _wait_for((COND), (MS) * 1000, 10, 1000) 377 1.1 riastrad 378 1.1 riastrad /* If CONFIG_PREEMPT_COUNT is disabled, in_atomic() always reports false. */ 379 1.1 riastrad #if defined(CONFIG_DRM_I915_DEBUG) && defined(CONFIG_PREEMPT_COUNT) 380 1.1 riastrad # define _WAIT_FOR_ATOMIC_CHECK(ATOMIC) WARN_ON_ONCE((ATOMIC) && !in_atomic()) 381 1.1 riastrad #else 382 1.1 riastrad # define _WAIT_FOR_ATOMIC_CHECK(ATOMIC) do { } while (0) 383 1.1 riastrad #endif 384 1.1 riastrad 385 1.1 riastrad #define _wait_for_atomic(COND, US, ATOMIC) \ 386 1.1 riastrad ({ \ 387 1.1 riastrad int cpu, ret, timeout = (US) * 1000; \ 388 1.1 riastrad u64 base; \ 389 1.1 riastrad _WAIT_FOR_ATOMIC_CHECK(ATOMIC); \ 390 1.1 riastrad if (!(ATOMIC)) { \ 391 1.1 riastrad preempt_disable(); \ 392 1.1 riastrad cpu = smp_processor_id(); \ 393 1.1 riastrad } \ 394 1.1 riastrad base = local_clock(); \ 395 1.1 riastrad for (;;) { \ 396 1.1 riastrad u64 now = local_clock(); \ 397 1.1 riastrad if (!(ATOMIC)) \ 398 1.1 riastrad preempt_enable(); \ 399 1.1 riastrad /* Guarantee COND check prior to timeout */ \ 400 1.1 riastrad barrier(); \ 401 1.1 riastrad if (COND) { \ 402 1.1 riastrad ret = 0; \ 403 1.1 riastrad break; \ 404 1.1 riastrad } \ 405 1.1 riastrad if (now - base >= timeout) { \ 406 1.1 riastrad ret = -ETIMEDOUT; \ 407 1.1 riastrad break; \ 408 1.1 riastrad } \ 409 1.1 riastrad cpu_relax(); \ 410 1.1 riastrad if (!(ATOMIC)) { \ 411 1.1 riastrad preempt_disable(); \ 412 1.1 riastrad if (unlikely(cpu != smp_processor_id())) { \ 413 1.1 riastrad timeout -= now - base; \ 414 1.1 riastrad cpu = smp_processor_id(); \ 415 1.1 riastrad base = local_clock(); \ 416 1.1 riastrad } \ 417 1.1 riastrad } \ 418 1.1 riastrad } \ 419 1.1 riastrad ret; \ 420 1.1 riastrad }) 421 1.1 riastrad 422 1.1 riastrad #define wait_for_us(COND, US) \ 423 1.1 riastrad ({ \ 424 1.1 riastrad int ret__; \ 425 1.1 riastrad BUILD_BUG_ON(!__builtin_constant_p(US)); \ 426 1.1 riastrad if ((US) > 10) \ 427 1.1 riastrad ret__ = _wait_for((COND), (US), 10, 10); \ 428 1.1 riastrad else \ 429 1.1 riastrad ret__ = _wait_for_atomic((COND), (US), 0); \ 430 1.1 riastrad ret__; \ 431 1.1 riastrad }) 432 1.1 riastrad 433 1.1 riastrad #define wait_for_atomic_us(COND, US) \ 434 1.1 riastrad ({ \ 435 1.1 riastrad BUILD_BUG_ON(!__builtin_constant_p(US)); \ 436 1.1 riastrad BUILD_BUG_ON((US) > 50000); \ 437 1.1 riastrad _wait_for_atomic((COND), (US), 1); \ 438 1.1 riastrad }) 439 1.1 riastrad 440 1.1 riastrad #define wait_for_atomic(COND, MS) wait_for_atomic_us((COND), (MS) * 1000) 441 1.1 riastrad 442 1.1 riastrad #define KHz(x) (1000 * (x)) 443 1.1 riastrad #define MHz(x) KHz(1000 * (x)) 444 1.1 riastrad 445 1.1 riastrad #define KBps(x) (1000 * (x)) 446 1.1 riastrad #define MBps(x) KBps(1000 * (x)) 447 1.1 riastrad #define GBps(x) ((u64)1000 * MBps((x))) 448 1.1 riastrad 449 1.1 riastrad static inline const char *yesno(bool v) 450 1.1 riastrad { 451 1.1 riastrad return v ? "yes" : "no"; 452 1.1 riastrad } 453 1.1 riastrad 454 1.1 riastrad static inline const char *onoff(bool v) 455 1.1 riastrad { 456 1.1 riastrad return v ? "on" : "off"; 457 1.1 riastrad } 458 1.1 riastrad 459 1.1 riastrad static inline const char *enableddisabled(bool v) 460 1.1 riastrad { 461 1.1 riastrad return v ? "enabled" : "disabled"; 462 1.1 riastrad } 463 1.1 riastrad 464 1.1 riastrad static inline void add_taint_for_CI(unsigned int taint) 465 1.1 riastrad { 466 1.1 riastrad /* 467 1.1 riastrad * The system is "ok", just about surviving for the user, but 468 1.1 riastrad * CI results are now unreliable as the HW is very suspect. 469 1.1 riastrad * CI checks the taint state after every test and will reboot 470 1.1 riastrad * the machine if the kernel is tainted. 471 1.1 riastrad */ 472 1.1 riastrad add_taint(taint, LOCKDEP_STILL_OK); 473 1.1 riastrad } 474 1.1 riastrad 475 1.1 riastrad void cancel_timer(struct timer_list *t); 476 1.1 riastrad void set_timer_ms(struct timer_list *t, unsigned long timeout); 477 1.1 riastrad 478 1.5 riastrad #ifdef __NetBSD__ 479 1.5 riastrad static inline bool 480 1.5 riastrad timer_expired(const struct timer_list *t) 481 1.1 riastrad { 482 1.5 riastrad return callout_expired(__UNCONST(&t->tl_callout)); 483 1.1 riastrad } 484 1.3 riastrad #else 485 1.5 riastrad static inline bool timer_expired(const struct timer_list *t) 486 1.3 riastrad { 487 1.5 riastrad return READ_ONCE(t->expires) && !timer_pending(t); 488 1.3 riastrad } 489 1.3 riastrad #endif 490 1.1 riastrad 491 1.1 riastrad /* 492 1.1 riastrad * This is a lookalike for IS_ENABLED() that takes a kconfig value, 493 1.1 riastrad * e.g. CONFIG_DRM_I915_SPIN_REQUEST, and evaluates whether it is non-zero 494 1.1 riastrad * i.e. whether the configuration is active. Wrapping up the config inside 495 1.1 riastrad * a boolean context prevents clang and smatch from complaining about potential 496 1.1 riastrad * issues in confusing logical-&& with bitwise-& for constants. 497 1.1 riastrad * 498 1.1 riastrad * Sadly IS_ENABLED() itself does not work with kconfig values. 499 1.1 riastrad * 500 1.1 riastrad * Returns 0 if @config is 0, 1 if set to any value. 501 1.1 riastrad */ 502 1.1 riastrad #define IS_ACTIVE(config) ((config) != 0) 503 1.1 riastrad 504 1.1 riastrad #endif /* !__I915_UTILS_H */ 505