pt_defs.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (c) 2024-2025, NVIDIA CORPORATION & AFFILIATES
  4. *
  5. * This header is included before the format. It contains definitions
  6. * that are required to compile the format. The header order is:
  7. * pt_defs.h
  8. * fmt_XX.h
  9. * pt_common.h
  10. */
  11. #ifndef __GENERIC_PT_DEFS_H
  12. #define __GENERIC_PT_DEFS_H
  13. #include <linux/generic_pt/common.h>
  14. #include <linux/types.h>
  15. #include <linux/atomic.h>
  16. #include <linux/bits.h>
  17. #include <linux/limits.h>
  18. #include <linux/bug.h>
  19. #include <linux/kconfig.h>
  20. #include "pt_log2.h"
  21. /* Header self-compile default defines */
  22. #ifndef pt_write_attrs
  23. typedef u64 pt_vaddr_t;
  24. typedef u64 pt_oaddr_t;
  25. #endif
  26. struct pt_table_p;
  27. enum {
  28. PT_VADDR_MAX = sizeof(pt_vaddr_t) == 8 ? U64_MAX : U32_MAX,
  29. PT_VADDR_MAX_LG2 = sizeof(pt_vaddr_t) == 8 ? 64 : 32,
  30. PT_OADDR_MAX = sizeof(pt_oaddr_t) == 8 ? U64_MAX : U32_MAX,
  31. PT_OADDR_MAX_LG2 = sizeof(pt_oaddr_t) == 8 ? 64 : 32,
  32. };
  33. /*
  34. * The format instantiation can have features wired off or on to optimize the
  35. * code gen. Supported features are just a reflection of what the current set of
  36. * kernel users want to use.
  37. */
  38. #ifndef PT_SUPPORTED_FEATURES
  39. #define PT_SUPPORTED_FEATURES 0
  40. #endif
  41. /*
  42. * When in debug mode we compile all formats with all features. This allows the
  43. * kunit to test the full matrix. SIGN_EXTEND can't co-exist with DYNAMIC_TOP or
  44. * FULL_VA. DMA_INCOHERENT requires a SW bit that not all formats have
  45. */
  46. #if IS_ENABLED(CONFIG_DEBUG_GENERIC_PT)
  47. enum {
  48. PT_ORIG_SUPPORTED_FEATURES = PT_SUPPORTED_FEATURES,
  49. PT_DEBUG_SUPPORTED_FEATURES =
  50. UINT_MAX &
  51. ~((PT_ORIG_SUPPORTED_FEATURES & BIT(PT_FEAT_DMA_INCOHERENT) ?
  52. 0 :
  53. BIT(PT_FEAT_DMA_INCOHERENT))) &
  54. ~((PT_ORIG_SUPPORTED_FEATURES & BIT(PT_FEAT_SIGN_EXTEND)) ?
  55. BIT(PT_FEAT_DYNAMIC_TOP) | BIT(PT_FEAT_FULL_VA) :
  56. BIT(PT_FEAT_SIGN_EXTEND)),
  57. };
  58. #undef PT_SUPPORTED_FEATURES
  59. #define PT_SUPPORTED_FEATURES PT_DEBUG_SUPPORTED_FEATURES
  60. #endif
  61. #ifndef PT_FORCE_ENABLED_FEATURES
  62. #define PT_FORCE_ENABLED_FEATURES 0
  63. #endif
  64. /**
  65. * DOC: Generic Page Table Language
  66. *
  67. * Language used in Generic Page Table
  68. * VA
  69. * The input address to the page table, often the virtual address.
  70. * OA
  71. * The output address from the page table, often the physical address.
  72. * leaf
  73. * An entry that results in an output address.
  74. * start/end
  75. * An half-open range, e.g. [0,0) refers to no VA.
  76. * start/last
  77. * An inclusive closed range, e.g. [0,0] refers to the VA 0
  78. * common
  79. * The generic page table container struct pt_common
  80. * level
  81. * Level 0 is always a table of only leaves with no futher table pointers.
  82. * Increasing levels increase the size of the table items. The least
  83. * significant VA bits used to index page tables are used to index the Level
  84. * 0 table. The various labels for table levels used by HW descriptions are
  85. * not used.
  86. * top_level
  87. * The inclusive highest level of the table. A two-level table
  88. * has a top level of 1.
  89. * table
  90. * A linear array of translation items for that level.
  91. * index
  92. * The position in a table of an element: item = table[index]
  93. * item
  94. * A single index in a table
  95. * entry
  96. * A single logical element in a table. If contiguous pages are not
  97. * supported then item and entry are the same thing, otherwise entry refers
  98. * to all the items that comprise a single contiguous translation.
  99. * item/entry_size
  100. * The number of bytes of VA the table index translates for.
  101. * If the item is a table entry then the next table covers
  102. * this size. If the entry translates to an output address then the
  103. * full OA is: OA | (VA % entry_size)
  104. * contig_count
  105. * The number of consecutive items fused into a single entry.
  106. * item_size * contig_count is the size of that entry's translation.
  107. * lg2
  108. * Indicates the value is encoded as log2, i.e. 1<<x is the actual value.
  109. * Normally the compiler is fine to optimize divide and mod with log2 values
  110. * automatically when inlining, however if the values are not constant
  111. * expressions it can't. So we do it by hand; we want to avoid 64-bit
  112. * divmod.
  113. */
  114. /* Returned by pt_load_entry() and for_each_pt_level_entry() */
  115. enum pt_entry_type {
  116. PT_ENTRY_EMPTY,
  117. /* Entry is valid and points to a lower table level */
  118. PT_ENTRY_TABLE,
  119. /* Entry is valid and returns an output address */
  120. PT_ENTRY_OA,
  121. };
  122. struct pt_range {
  123. struct pt_common *common;
  124. struct pt_table_p *top_table;
  125. pt_vaddr_t va;
  126. pt_vaddr_t last_va;
  127. u8 top_level;
  128. u8 max_vasz_lg2;
  129. };
  130. /*
  131. * Similar to xa_state, this records information about an in-progress parse at a
  132. * single level.
  133. */
  134. struct pt_state {
  135. struct pt_range *range;
  136. struct pt_table_p *table;
  137. struct pt_table_p *table_lower;
  138. u64 entry;
  139. enum pt_entry_type type;
  140. unsigned short index;
  141. unsigned short end_index;
  142. u8 level;
  143. };
  144. #define pt_cur_table(pts, type) ((type *)((pts)->table))
  145. /*
  146. * Try to install a new table pointer. The locking methodology requires this to
  147. * be atomic (multiple threads can race to install a pointer). The losing
  148. * threads will fail the atomic and return false. They should free any memory
  149. * and reparse the table level again.
  150. */
  151. #if !IS_ENABLED(CONFIG_GENERIC_ATOMIC64)
  152. static inline bool pt_table_install64(struct pt_state *pts, u64 table_entry)
  153. {
  154. u64 *entryp = pt_cur_table(pts, u64) + pts->index;
  155. u64 old_entry = pts->entry;
  156. bool ret;
  157. /*
  158. * Ensure the zero'd table content itself is visible before its PTE can
  159. * be. release is a NOP on !SMP, but the HW is still doing an acquire.
  160. */
  161. if (!IS_ENABLED(CONFIG_SMP))
  162. dma_wmb();
  163. ret = try_cmpxchg64_release(entryp, &old_entry, table_entry);
  164. if (ret)
  165. pts->entry = table_entry;
  166. return ret;
  167. }
  168. #endif
  169. static inline bool pt_table_install32(struct pt_state *pts, u32 table_entry)
  170. {
  171. u32 *entryp = pt_cur_table(pts, u32) + pts->index;
  172. u32 old_entry = pts->entry;
  173. bool ret;
  174. /*
  175. * Ensure the zero'd table content itself is visible before its PTE can
  176. * be. release is a NOP on !SMP, but the HW is still doing an acquire.
  177. */
  178. if (!IS_ENABLED(CONFIG_SMP))
  179. dma_wmb();
  180. ret = try_cmpxchg_release(entryp, &old_entry, table_entry);
  181. if (ret)
  182. pts->entry = table_entry;
  183. return ret;
  184. }
  185. #define PT_SUPPORTED_FEATURE(feature_nr) (PT_SUPPORTED_FEATURES & BIT(feature_nr))
  186. static __always_inline bool pt_feature(const struct pt_common *common,
  187. unsigned int feature_nr)
  188. {
  189. if (PT_FORCE_ENABLED_FEATURES & BIT(feature_nr))
  190. return true;
  191. if (!PT_SUPPORTED_FEATURE(feature_nr))
  192. return false;
  193. return common->features & BIT(feature_nr);
  194. }
  195. static __always_inline bool pts_feature(const struct pt_state *pts,
  196. unsigned int feature_nr)
  197. {
  198. return pt_feature(pts->range->common, feature_nr);
  199. }
  200. /*
  201. * PT_WARN_ON is used for invariants that the kunit should be checking can't
  202. * happen.
  203. */
  204. #if IS_ENABLED(CONFIG_DEBUG_GENERIC_PT)
  205. #define PT_WARN_ON WARN_ON
  206. #else
  207. static inline bool PT_WARN_ON(bool condition)
  208. {
  209. return false;
  210. }
  211. #endif
  212. /* These all work on the VA type */
  213. #define log2_to_int(a_lg2) log2_to_int_t(pt_vaddr_t, a_lg2)
  214. #define log2_to_max_int(a_lg2) log2_to_max_int_t(pt_vaddr_t, a_lg2)
  215. #define log2_div(a, b_lg2) log2_div_t(pt_vaddr_t, a, b_lg2)
  216. #define log2_div_eq(a, b, c_lg2) log2_div_eq_t(pt_vaddr_t, a, b, c_lg2)
  217. #define log2_mod(a, b_lg2) log2_mod_t(pt_vaddr_t, a, b_lg2)
  218. #define log2_mod_eq_max(a, b_lg2) log2_mod_eq_max_t(pt_vaddr_t, a, b_lg2)
  219. #define log2_set_mod(a, val, b_lg2) log2_set_mod_t(pt_vaddr_t, a, val, b_lg2)
  220. #define log2_set_mod_max(a, b_lg2) log2_set_mod_max_t(pt_vaddr_t, a, b_lg2)
  221. #define log2_mul(a, b_lg2) log2_mul_t(pt_vaddr_t, a, b_lg2)
  222. #define vaffs(a) ffs_t(pt_vaddr_t, a)
  223. #define vafls(a) fls_t(pt_vaddr_t, a)
  224. #define vaffz(a) ffz_t(pt_vaddr_t, a)
  225. /*
  226. * The full VA (fva) versions permit the lg2 value to be == PT_VADDR_MAX_LG2 and
  227. * generate a useful defined result. The non-fva versions will malfunction at
  228. * this extreme.
  229. */
  230. static inline pt_vaddr_t fvalog2_div(pt_vaddr_t a, unsigned int b_lg2)
  231. {
  232. if (PT_SUPPORTED_FEATURE(PT_FEAT_FULL_VA) && b_lg2 == PT_VADDR_MAX_LG2)
  233. return 0;
  234. return log2_div_t(pt_vaddr_t, a, b_lg2);
  235. }
  236. static inline pt_vaddr_t fvalog2_mod(pt_vaddr_t a, unsigned int b_lg2)
  237. {
  238. if (PT_SUPPORTED_FEATURE(PT_FEAT_FULL_VA) && b_lg2 == PT_VADDR_MAX_LG2)
  239. return a;
  240. return log2_mod_t(pt_vaddr_t, a, b_lg2);
  241. }
  242. static inline bool fvalog2_div_eq(pt_vaddr_t a, pt_vaddr_t b,
  243. unsigned int c_lg2)
  244. {
  245. if (PT_SUPPORTED_FEATURE(PT_FEAT_FULL_VA) && c_lg2 == PT_VADDR_MAX_LG2)
  246. return true;
  247. return log2_div_eq_t(pt_vaddr_t, a, b, c_lg2);
  248. }
  249. static inline pt_vaddr_t fvalog2_set_mod(pt_vaddr_t a, pt_vaddr_t val,
  250. unsigned int b_lg2)
  251. {
  252. if (PT_SUPPORTED_FEATURE(PT_FEAT_FULL_VA) && b_lg2 == PT_VADDR_MAX_LG2)
  253. return val;
  254. return log2_set_mod_t(pt_vaddr_t, a, val, b_lg2);
  255. }
  256. static inline pt_vaddr_t fvalog2_set_mod_max(pt_vaddr_t a, unsigned int b_lg2)
  257. {
  258. if (PT_SUPPORTED_FEATURE(PT_FEAT_FULL_VA) && b_lg2 == PT_VADDR_MAX_LG2)
  259. return PT_VADDR_MAX;
  260. return log2_set_mod_max_t(pt_vaddr_t, a, b_lg2);
  261. }
  262. /* These all work on the OA type */
  263. #define oalog2_to_int(a_lg2) log2_to_int_t(pt_oaddr_t, a_lg2)
  264. #define oalog2_to_max_int(a_lg2) log2_to_max_int_t(pt_oaddr_t, a_lg2)
  265. #define oalog2_div(a, b_lg2) log2_div_t(pt_oaddr_t, a, b_lg2)
  266. #define oalog2_div_eq(a, b, c_lg2) log2_div_eq_t(pt_oaddr_t, a, b, c_lg2)
  267. #define oalog2_mod(a, b_lg2) log2_mod_t(pt_oaddr_t, a, b_lg2)
  268. #define oalog2_mod_eq_max(a, b_lg2) log2_mod_eq_max_t(pt_oaddr_t, a, b_lg2)
  269. #define oalog2_set_mod(a, val, b_lg2) log2_set_mod_t(pt_oaddr_t, a, val, b_lg2)
  270. #define oalog2_set_mod_max(a, b_lg2) log2_set_mod_max_t(pt_oaddr_t, a, b_lg2)
  271. #define oalog2_mul(a, b_lg2) log2_mul_t(pt_oaddr_t, a, b_lg2)
  272. #define oaffs(a) ffs_t(pt_oaddr_t, a)
  273. #define oafls(a) fls_t(pt_oaddr_t, a)
  274. #define oaffz(a) ffz_t(pt_oaddr_t, a)
  275. static inline uintptr_t _pt_top_set(struct pt_table_p *table_mem,
  276. unsigned int top_level)
  277. {
  278. return top_level | (uintptr_t)table_mem;
  279. }
  280. static inline void pt_top_set(struct pt_common *common,
  281. struct pt_table_p *table_mem,
  282. unsigned int top_level)
  283. {
  284. WRITE_ONCE(common->top_of_table, _pt_top_set(table_mem, top_level));
  285. }
  286. static inline void pt_top_set_level(struct pt_common *common,
  287. unsigned int top_level)
  288. {
  289. pt_top_set(common, NULL, top_level);
  290. }
  291. static inline unsigned int pt_top_get_level(const struct pt_common *common)
  292. {
  293. return READ_ONCE(common->top_of_table) % (1 << PT_TOP_LEVEL_BITS);
  294. }
  295. static inline bool pt_check_install_leaf_args(struct pt_state *pts,
  296. pt_oaddr_t oa,
  297. unsigned int oasz_lg2);
  298. #endif