i915_utils.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /*
  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. #ifndef __I915_UTILS_H
  25. #define __I915_UTILS_H
  26. #include <linux/list.h>
  27. #include <linux/overflow.h>
  28. #include <linux/sched.h>
  29. #include <linux/string_helpers.h>
  30. #include <linux/types.h>
  31. #include <linux/workqueue.h>
  32. #include <linux/sched/clock.h>
  33. #ifdef CONFIG_X86
  34. #include <asm/hypervisor.h>
  35. #endif
  36. struct drm_i915_private;
  37. struct timer_list;
  38. #define FDO_BUG_URL "https://drm.pages.freedesktop.org/intel-docs/how-to-file-i915-bugs.html"
  39. #define MISSING_CASE(x) WARN(1, "Missing case (%s == %ld)\n", \
  40. __stringify(x), (long)(x))
  41. #if IS_ENABLED(CONFIG_DRM_I915_DEBUG)
  42. int __i915_inject_probe_error(struct drm_i915_private *i915, int err,
  43. const char *func, int line);
  44. #define i915_inject_probe_error(_i915, _err) \
  45. __i915_inject_probe_error((_i915), (_err), __func__, __LINE__)
  46. bool i915_error_injected(void);
  47. #else
  48. #define i915_inject_probe_error(i915, e) ({ BUILD_BUG_ON_INVALID(i915); 0; })
  49. #define i915_error_injected() false
  50. #endif
  51. #define i915_inject_probe_failure(i915) i915_inject_probe_error((i915), -ENODEV)
  52. #define i915_probe_error(i915, fmt, ...) ({ \
  53. if (i915_error_injected()) \
  54. drm_dbg(&(i915)->drm, fmt, ##__VA_ARGS__); \
  55. else \
  56. drm_err(&(i915)->drm, fmt, ##__VA_ARGS__); \
  57. })
  58. #define range_overflows(start, size, max) ({ \
  59. typeof(start) start__ = (start); \
  60. typeof(size) size__ = (size); \
  61. typeof(max) max__ = (max); \
  62. (void)(&start__ == &size__); \
  63. (void)(&start__ == &max__); \
  64. start__ >= max__ || size__ > max__ - start__; \
  65. })
  66. #define range_overflows_t(type, start, size, max) \
  67. range_overflows((type)(start), (type)(size), (type)(max))
  68. #define range_overflows_end(start, size, max) ({ \
  69. typeof(start) start__ = (start); \
  70. typeof(size) size__ = (size); \
  71. typeof(max) max__ = (max); \
  72. (void)(&start__ == &size__); \
  73. (void)(&start__ == &max__); \
  74. start__ > max__ || size__ > max__ - start__; \
  75. })
  76. #define range_overflows_end_t(type, start, size, max) \
  77. range_overflows_end((type)(start), (type)(size), (type)(max))
  78. #define ptr_mask_bits(ptr, n) ({ \
  79. unsigned long __v = (unsigned long)(ptr); \
  80. (typeof(ptr))(__v & -BIT(n)); \
  81. })
  82. #define ptr_unmask_bits(ptr, n) ((unsigned long)(ptr) & (BIT(n) - 1))
  83. #define ptr_unpack_bits(ptr, bits, n) ({ \
  84. unsigned long __v = (unsigned long)(ptr); \
  85. *(bits) = __v & (BIT(n) - 1); \
  86. (typeof(ptr))(__v & -BIT(n)); \
  87. })
  88. #define ptr_pack_bits(ptr, bits, n) ({ \
  89. unsigned long __bits = (bits); \
  90. GEM_BUG_ON(__bits & -BIT(n)); \
  91. ((typeof(ptr))((unsigned long)(ptr) | __bits)); \
  92. })
  93. #define ptr_dec(ptr) ({ \
  94. unsigned long __v = (unsigned long)(ptr); \
  95. (typeof(ptr))(__v - 1); \
  96. })
  97. #define ptr_inc(ptr) ({ \
  98. unsigned long __v = (unsigned long)(ptr); \
  99. (typeof(ptr))(__v + 1); \
  100. })
  101. #define page_mask_bits(ptr) ptr_mask_bits(ptr, PAGE_SHIFT)
  102. #define page_unmask_bits(ptr) ptr_unmask_bits(ptr, PAGE_SHIFT)
  103. #define page_pack_bits(ptr, bits) ptr_pack_bits(ptr, bits, PAGE_SHIFT)
  104. #define page_unpack_bits(ptr, bits) ptr_unpack_bits(ptr, bits, PAGE_SHIFT)
  105. #define fetch_and_zero(ptr) ({ \
  106. typeof(*ptr) __T = *(ptr); \
  107. *(ptr) = (typeof(*ptr))0; \
  108. __T; \
  109. })
  110. static __always_inline ptrdiff_t ptrdiff(const void *a, const void *b)
  111. {
  112. return a - b;
  113. }
  114. /*
  115. * container_of_user: Extract the superclass from a pointer to a member.
  116. *
  117. * Exactly like container_of() with the exception that it plays nicely
  118. * with sparse for __user @ptr.
  119. */
  120. #define container_of_user(ptr, type, member) ({ \
  121. void __user *__mptr = (void __user *)(ptr); \
  122. BUILD_BUG_ON_MSG(!__same_type(*(ptr), typeof_member(type, member)) && \
  123. !__same_type(*(ptr), void), \
  124. "pointer type mismatch in container_of()"); \
  125. ((type __user *)(__mptr - offsetof(type, member))); })
  126. /*
  127. * check_user_mbz: Check that a user value exists and is zero
  128. *
  129. * Frequently in our uABI we reserve space for future extensions, and
  130. * two ensure that userspace is prepared we enforce that space must
  131. * be zero. (Then any future extension can safely assume a default value
  132. * of 0.)
  133. *
  134. * check_user_mbz() combines checking that the user pointer is accessible
  135. * and that the contained value is zero.
  136. *
  137. * Returns: -EFAULT if not accessible, -EINVAL if !zero, or 0 on success.
  138. */
  139. #define check_user_mbz(U) ({ \
  140. typeof(*(U)) mbz__; \
  141. get_user(mbz__, (U)) ? -EFAULT : mbz__ ? -EINVAL : 0; \
  142. })
  143. #define u64_to_ptr(T, x) ({ \
  144. typecheck(u64, x); \
  145. (T *)(uintptr_t)(x); \
  146. })
  147. #define __mask_next_bit(mask) ({ \
  148. int __idx = ffs(mask) - 1; \
  149. mask &= ~BIT(__idx); \
  150. __idx; \
  151. })
  152. static inline bool is_power_of_2_u64(u64 n)
  153. {
  154. return (n != 0 && ((n & (n - 1)) == 0));
  155. }
  156. static inline void __list_del_many(struct list_head *head,
  157. struct list_head *first)
  158. {
  159. first->prev = head;
  160. WRITE_ONCE(head->next, first);
  161. }
  162. static inline int list_is_last_rcu(const struct list_head *list,
  163. const struct list_head *head)
  164. {
  165. return READ_ONCE(list->next) == head;
  166. }
  167. static inline unsigned long msecs_to_jiffies_timeout(const unsigned int m)
  168. {
  169. unsigned long j = msecs_to_jiffies(m);
  170. return min_t(unsigned long, MAX_JIFFY_OFFSET, j + 1);
  171. }
  172. /*
  173. * If you need to wait X milliseconds between events A and B, but event B
  174. * doesn't happen exactly after event A, you record the timestamp (jiffies) of
  175. * when event A happened, then just before event B you call this function and
  176. * pass the timestamp as the first argument, and X as the second argument.
  177. */
  178. static inline void
  179. wait_remaining_ms_from_jiffies(unsigned long timestamp_jiffies, int to_wait_ms)
  180. {
  181. unsigned long target_jiffies, tmp_jiffies, remaining_jiffies;
  182. /*
  183. * Don't re-read the value of "jiffies" every time since it may change
  184. * behind our back and break the math.
  185. */
  186. tmp_jiffies = jiffies;
  187. target_jiffies = timestamp_jiffies +
  188. msecs_to_jiffies_timeout(to_wait_ms);
  189. if (time_after(target_jiffies, tmp_jiffies)) {
  190. remaining_jiffies = target_jiffies - tmp_jiffies;
  191. while (remaining_jiffies)
  192. remaining_jiffies =
  193. schedule_timeout_uninterruptible(remaining_jiffies);
  194. }
  195. }
  196. /*
  197. * __wait_for - magic wait macro
  198. *
  199. * Macro to help avoid open coding check/wait/timeout patterns. Note that it's
  200. * important that we check the condition again after having timed out, since the
  201. * timeout could be due to preemption or similar and we've never had a chance to
  202. * check the condition before the timeout.
  203. */
  204. #define __wait_for(OP, COND, US, Wmin, Wmax) ({ \
  205. const ktime_t end__ = ktime_add_ns(ktime_get_raw(), 1000ll * (US)); \
  206. long wait__ = (Wmin); /* recommended min for usleep is 10 us */ \
  207. int ret__; \
  208. might_sleep(); \
  209. for (;;) { \
  210. const bool expired__ = ktime_after(ktime_get_raw(), end__); \
  211. OP; \
  212. /* Guarantee COND check prior to timeout */ \
  213. barrier(); \
  214. if (COND) { \
  215. ret__ = 0; \
  216. break; \
  217. } \
  218. if (expired__) { \
  219. ret__ = -ETIMEDOUT; \
  220. break; \
  221. } \
  222. usleep_range(wait__, wait__ * 2); \
  223. if (wait__ < (Wmax)) \
  224. wait__ <<= 1; \
  225. } \
  226. ret__; \
  227. })
  228. #define _wait_for(COND, US, Wmin, Wmax) __wait_for(, (COND), (US), (Wmin), \
  229. (Wmax))
  230. #define wait_for(COND, MS) _wait_for((COND), (MS) * 1000, 10, 1000)
  231. /* If CONFIG_PREEMPT_COUNT is disabled, in_atomic() always reports false. */
  232. #if defined(CONFIG_DRM_I915_DEBUG) && defined(CONFIG_PREEMPT_COUNT)
  233. # define _WAIT_FOR_ATOMIC_CHECK(ATOMIC) WARN_ON_ONCE((ATOMIC) && !in_atomic())
  234. #else
  235. # define _WAIT_FOR_ATOMIC_CHECK(ATOMIC) do { } while (0)
  236. #endif
  237. #define _wait_for_atomic(COND, US, ATOMIC) \
  238. ({ \
  239. int cpu, ret, timeout = (US) * 1000; \
  240. u64 base; \
  241. _WAIT_FOR_ATOMIC_CHECK(ATOMIC); \
  242. if (!(ATOMIC)) { \
  243. preempt_disable(); \
  244. cpu = smp_processor_id(); \
  245. } \
  246. base = local_clock(); \
  247. for (;;) { \
  248. u64 now = local_clock(); \
  249. if (!(ATOMIC)) \
  250. preempt_enable(); \
  251. /* Guarantee COND check prior to timeout */ \
  252. barrier(); \
  253. if (COND) { \
  254. ret = 0; \
  255. break; \
  256. } \
  257. if (now - base >= timeout) { \
  258. ret = -ETIMEDOUT; \
  259. break; \
  260. } \
  261. cpu_relax(); \
  262. if (!(ATOMIC)) { \
  263. preempt_disable(); \
  264. if (unlikely(cpu != smp_processor_id())) { \
  265. timeout -= now - base; \
  266. cpu = smp_processor_id(); \
  267. base = local_clock(); \
  268. } \
  269. } \
  270. } \
  271. ret; \
  272. })
  273. #define wait_for_us(COND, US) \
  274. ({ \
  275. int ret__; \
  276. BUILD_BUG_ON(!__builtin_constant_p(US)); \
  277. if ((US) > 10) \
  278. ret__ = _wait_for((COND), (US), 10, 10); \
  279. else \
  280. ret__ = _wait_for_atomic((COND), (US), 0); \
  281. ret__; \
  282. })
  283. #define wait_for_atomic_us(COND, US) \
  284. ({ \
  285. BUILD_BUG_ON(!__builtin_constant_p(US)); \
  286. BUILD_BUG_ON((US) > 50000); \
  287. _wait_for_atomic((COND), (US), 1); \
  288. })
  289. #define wait_for_atomic(COND, MS) wait_for_atomic_us((COND), (MS) * 1000)
  290. #define KHz(x) (1000 * (x))
  291. #define MHz(x) KHz(1000 * (x))
  292. void add_taint_for_CI(struct drm_i915_private *i915, unsigned int taint);
  293. static inline void __add_taint_for_CI(unsigned int taint)
  294. {
  295. /*
  296. * The system is "ok", just about surviving for the user, but
  297. * CI results are now unreliable as the HW is very suspect.
  298. * CI checks the taint state after every test and will reboot
  299. * the machine if the kernel is tainted.
  300. */
  301. add_taint(taint, LOCKDEP_STILL_OK);
  302. }
  303. void cancel_timer(struct timer_list *t);
  304. void set_timer_ms(struct timer_list *t, unsigned long timeout);
  305. static inline bool timer_active(const struct timer_list *t)
  306. {
  307. return READ_ONCE(t->expires);
  308. }
  309. static inline bool timer_expired(const struct timer_list *t)
  310. {
  311. return timer_active(t) && !timer_pending(t);
  312. }
  313. static inline bool i915_run_as_guest(void)
  314. {
  315. #if IS_ENABLED(CONFIG_X86)
  316. return !hypervisor_is_type(X86_HYPER_NATIVE);
  317. #else
  318. /* Not supported yet */
  319. return false;
  320. #endif
  321. }
  322. bool i915_vtd_active(struct drm_i915_private *i915);
  323. bool i915_direct_stolen_access(struct drm_i915_private *i915);
  324. #endif /* !__I915_UTILS_H */