i915_ptr_util.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /* SPDX-License-Identifier: MIT */
  2. /* Copyright © 2025 Intel Corporation */
  3. #ifndef __I915_PTR_UTIL_H__
  4. #define __I915_PTR_UTIL_H__
  5. #include <linux/types.h>
  6. #define ptr_mask_bits(ptr, n) ({ \
  7. unsigned long __v = (unsigned long)(ptr); \
  8. (typeof(ptr))(__v & -BIT(n)); \
  9. })
  10. #define ptr_unmask_bits(ptr, n) ((unsigned long)(ptr) & (BIT(n) - 1))
  11. #define ptr_unpack_bits(ptr, bits, n) ({ \
  12. unsigned long __v = (unsigned long)(ptr); \
  13. *(bits) = __v & (BIT(n) - 1); \
  14. (typeof(ptr))(__v & -BIT(n)); \
  15. })
  16. #define ptr_pack_bits(ptr, bits, n) ({ \
  17. unsigned long __bits = (bits); \
  18. GEM_BUG_ON(__bits & -BIT(n)); \
  19. ((typeof(ptr))((unsigned long)(ptr) | __bits)); \
  20. })
  21. #define ptr_dec(ptr) ({ \
  22. unsigned long __v = (unsigned long)(ptr); \
  23. (typeof(ptr))(__v - 1); \
  24. })
  25. #define ptr_inc(ptr) ({ \
  26. unsigned long __v = (unsigned long)(ptr); \
  27. (typeof(ptr))(__v + 1); \
  28. })
  29. #define page_mask_bits(ptr) ptr_mask_bits(ptr, PAGE_SHIFT)
  30. #define page_unmask_bits(ptr) ptr_unmask_bits(ptr, PAGE_SHIFT)
  31. #define page_pack_bits(ptr, bits) ptr_pack_bits(ptr, bits, PAGE_SHIFT)
  32. #define page_unpack_bits(ptr, bits) ptr_unpack_bits(ptr, bits, PAGE_SHIFT)
  33. static __always_inline ptrdiff_t ptrdiff(const void *a, const void *b)
  34. {
  35. return a - b;
  36. }
  37. #define u64_to_ptr(T, x) ({ \
  38. typecheck(u64, x); \
  39. (T *)(uintptr_t)(x); \
  40. })
  41. /*
  42. * container_of_user: Extract the superclass from a pointer to a member.
  43. *
  44. * Exactly like container_of() with the exception that it plays nicely
  45. * with sparse for __user @ptr.
  46. */
  47. #define container_of_user(ptr, type, member) ({ \
  48. void __user *__mptr = (void __user *)(ptr); \
  49. BUILD_BUG_ON_MSG(!__same_type(*(ptr), typeof_member(type, member)) && \
  50. !__same_type(*(ptr), void), \
  51. "pointer type mismatch in container_of()"); \
  52. ((type __user *)(__mptr - offsetof(type, member))); })
  53. #endif /* __I915_PTR_UTIL_H__ */