pt_common.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (c) 2024-2025, NVIDIA CORPORATION & AFFILIATES
  4. *
  5. * This header is included after the format. It contains definitions
  6. * that build on the format definitions to create the basic format API.
  7. *
  8. * The format API is listed here, with kdocs. The functions without bodies are
  9. * implemented in the format using the pattern:
  10. * static inline FMTpt_XXX(..) {..}
  11. * #define pt_XXX FMTpt_XXX
  12. *
  13. * If the format doesn't implement a function then pt_fmt_defaults.h can provide
  14. * a generic version.
  15. *
  16. * The routines marked "@pts: Entry to query" operate on the entire contiguous
  17. * entry and can be called with a pts->index pointing to any sub item that makes
  18. * up that entry.
  19. *
  20. * The header order is:
  21. * pt_defs.h
  22. * FMT.h
  23. * pt_common.h
  24. */
  25. #ifndef __GENERIC_PT_PT_COMMON_H
  26. #define __GENERIC_PT_PT_COMMON_H
  27. #include "pt_defs.h"
  28. #include "pt_fmt_defaults.h"
  29. /**
  30. * pt_attr_from_entry() - Convert the permission bits back to attrs
  31. * @pts: Entry to convert from
  32. * @attrs: Resulting attrs
  33. *
  34. * Fill in the attrs with the permission bits encoded in the current leaf entry.
  35. * The attrs should be usable with pt_install_leaf_entry() to reconstruct the
  36. * same entry.
  37. */
  38. static inline void pt_attr_from_entry(const struct pt_state *pts,
  39. struct pt_write_attrs *attrs);
  40. /**
  41. * pt_can_have_leaf() - True if the current level can have an OA entry
  42. * @pts: The current level
  43. *
  44. * True if the current level can support pt_install_leaf_entry(). A leaf
  45. * entry produce an OA.
  46. */
  47. static inline bool pt_can_have_leaf(const struct pt_state *pts);
  48. /**
  49. * pt_can_have_table() - True if the current level can have a lower table
  50. * @pts: The current level
  51. *
  52. * Every level except 0 is allowed to have a lower table.
  53. */
  54. static inline bool pt_can_have_table(const struct pt_state *pts)
  55. {
  56. /* No further tables at level 0 */
  57. return pts->level > 0;
  58. }
  59. /**
  60. * pt_clear_entries() - Make entries empty (non-present)
  61. * @pts: Starting table index
  62. * @num_contig_lg2: Number of contiguous items to clear
  63. *
  64. * Clear a run of entries. A cleared entry will load back as PT_ENTRY_EMPTY
  65. * and does not have any effect on table walking. The starting index must be
  66. * aligned to num_contig_lg2.
  67. */
  68. static inline void pt_clear_entries(struct pt_state *pts,
  69. unsigned int num_contig_lg2);
  70. /**
  71. * pt_entry_make_write_dirty() - Make an entry dirty
  72. * @pts: Table entry to change
  73. *
  74. * Make pt_entry_is_write_dirty() return true for this entry. This can be called
  75. * asynchronously with any other table manipulation under a RCU lock and must
  76. * not corrupt the table.
  77. */
  78. static inline bool pt_entry_make_write_dirty(struct pt_state *pts);
  79. /**
  80. * pt_entry_make_write_clean() - Make the entry write clean
  81. * @pts: Table entry to change
  82. *
  83. * Modify the entry so that pt_entry_is_write_dirty() == false. The HW will
  84. * eventually be notified of this change via a TLB flush, which is the point
  85. * that the HW must become synchronized. Any "write dirty" prior to the TLB
  86. * flush can be lost, but once the TLB flush completes all writes must make
  87. * their entries write dirty.
  88. *
  89. * The format should alter the entry in a way that is compatible with any
  90. * concurrent update from HW. The entire contiguous entry is changed.
  91. */
  92. static inline void pt_entry_make_write_clean(struct pt_state *pts);
  93. /**
  94. * pt_entry_is_write_dirty() - True if the entry has been written to
  95. * @pts: Entry to query
  96. *
  97. * "write dirty" means that the HW has written to the OA translated
  98. * by this entry. If the entry is contiguous then the consolidated
  99. * "write dirty" for all the items must be returned.
  100. */
  101. static inline bool pt_entry_is_write_dirty(const struct pt_state *pts);
  102. /**
  103. * pt_dirty_supported() - True if the page table supports dirty tracking
  104. * @common: Page table to query
  105. */
  106. static inline bool pt_dirty_supported(struct pt_common *common);
  107. /**
  108. * pt_entry_num_contig_lg2() - Number of contiguous items for this leaf entry
  109. * @pts: Entry to query
  110. *
  111. * Return the number of contiguous items this leaf entry spans. If the entry
  112. * is single item it returns ilog2(1).
  113. */
  114. static inline unsigned int pt_entry_num_contig_lg2(const struct pt_state *pts);
  115. /**
  116. * pt_entry_oa() - Output Address for this leaf entry
  117. * @pts: Entry to query
  118. *
  119. * Return the output address for the start of the entry. If the entry
  120. * is contiguous this returns the same value for each sub-item. I.e.::
  121. *
  122. * log2_mod(pt_entry_oa(), pt_entry_oa_lg2sz()) == 0
  123. *
  124. * See pt_item_oa(). The format should implement one of these two functions
  125. * depending on how it stores the OAs in the table.
  126. */
  127. static inline pt_oaddr_t pt_entry_oa(const struct pt_state *pts);
  128. /**
  129. * pt_entry_oa_lg2sz() - Return the size of an OA entry
  130. * @pts: Entry to query
  131. *
  132. * If the entry is not contiguous this returns pt_table_item_lg2sz(), otherwise
  133. * it returns the total VA/OA size of the entire contiguous entry.
  134. */
  135. static inline unsigned int pt_entry_oa_lg2sz(const struct pt_state *pts)
  136. {
  137. return pt_entry_num_contig_lg2(pts) + pt_table_item_lg2sz(pts);
  138. }
  139. /**
  140. * pt_entry_oa_exact() - Return the complete OA for an entry
  141. * @pts: Entry to query
  142. *
  143. * During iteration the first entry could have a VA with an offset from the
  144. * natural start of the entry. Return the exact OA including the pts's VA
  145. * offset.
  146. */
  147. static inline pt_oaddr_t pt_entry_oa_exact(const struct pt_state *pts)
  148. {
  149. return _pt_entry_oa_fast(pts) |
  150. log2_mod(pts->range->va, pt_entry_oa_lg2sz(pts));
  151. }
  152. /**
  153. * pt_full_va_prefix() - The top bits of the VA
  154. * @common: Page table to query
  155. *
  156. * This is usually 0, but some formats have their VA space going downward from
  157. * PT_VADDR_MAX, and will return that instead. This value must always be
  158. * adjusted by struct pt_common max_vasz_lg2.
  159. */
  160. static inline pt_vaddr_t pt_full_va_prefix(const struct pt_common *common);
  161. /**
  162. * pt_has_system_page_size() - True if level 0 can install a PAGE_SHIFT entry
  163. * @common: Page table to query
  164. *
  165. * If true the caller can use, at level 0, pt_install_leaf_entry(PAGE_SHIFT).
  166. * This is useful to create optimized paths for common cases of PAGE_SIZE
  167. * mappings.
  168. */
  169. static inline bool pt_has_system_page_size(const struct pt_common *common);
  170. /**
  171. * pt_install_leaf_entry() - Write a leaf entry to the table
  172. * @pts: Table index to change
  173. * @oa: Output Address for this leaf
  174. * @oasz_lg2: Size in VA/OA for this leaf
  175. * @attrs: Attributes to modify the entry
  176. *
  177. * A leaf OA entry will return PT_ENTRY_OA from pt_load_entry(). It translates
  178. * the VA indicated by pts to the given OA.
  179. *
  180. * For a single item non-contiguous entry oasz_lg2 is pt_table_item_lg2sz().
  181. * For contiguous it is pt_table_item_lg2sz() + num_contig_lg2.
  182. *
  183. * This must not be called if pt_can_have_leaf() == false. Contiguous sizes
  184. * not indicated by pt_possible_sizes() must not be specified.
  185. */
  186. static inline void pt_install_leaf_entry(struct pt_state *pts, pt_oaddr_t oa,
  187. unsigned int oasz_lg2,
  188. const struct pt_write_attrs *attrs);
  189. /**
  190. * pt_install_table() - Write a table entry to the table
  191. * @pts: Table index to change
  192. * @table_pa: CPU physical address of the lower table's memory
  193. * @attrs: Attributes to modify the table index
  194. *
  195. * A table entry will return PT_ENTRY_TABLE from pt_load_entry(). The table_pa
  196. * is the table at pts->level - 1. This is done by cmpxchg so pts must have the
  197. * current entry loaded. The pts is updated with the installed entry.
  198. *
  199. * This must not be called if pt_can_have_table() == false.
  200. *
  201. * Returns: true if the table was installed successfully.
  202. */
  203. static inline bool pt_install_table(struct pt_state *pts, pt_oaddr_t table_pa,
  204. const struct pt_write_attrs *attrs);
  205. /**
  206. * pt_item_oa() - Output Address for this leaf item
  207. * @pts: Item to query
  208. *
  209. * Return the output address for this item. If the item is part of a contiguous
  210. * entry it returns the value of the OA for this individual sub item.
  211. *
  212. * See pt_entry_oa(). The format should implement one of these two functions
  213. * depending on how it stores the OA's in the table.
  214. */
  215. static inline pt_oaddr_t pt_item_oa(const struct pt_state *pts);
  216. /**
  217. * pt_load_entry_raw() - Read from the location pts points at into the pts
  218. * @pts: Table index to load
  219. *
  220. * Return the type of entry that was loaded. pts->entry will be filled in with
  221. * the entry's content. See pt_load_entry()
  222. */
  223. static inline enum pt_entry_type pt_load_entry_raw(struct pt_state *pts);
  224. /**
  225. * pt_max_oa_lg2() - Return the maximum OA the table format can hold
  226. * @common: Page table to query
  227. *
  228. * The value oalog2_to_max_int(pt_max_oa_lg2()) is the MAX for the
  229. * OA. This is the absolute maximum address the table can hold. struct pt_common
  230. * max_oasz_lg2 sets a lower dynamic maximum based on HW capability.
  231. */
  232. static inline unsigned int
  233. pt_max_oa_lg2(const struct pt_common *common);
  234. /**
  235. * pt_num_items_lg2() - Return the number of items in this table level
  236. * @pts: The current level
  237. *
  238. * The number of items in a table level defines the number of bits this level
  239. * decodes from the VA. This function is not called for the top level,
  240. * so it does not need to compute a special value for the top case. The
  241. * result for the top is based on pt_common max_vasz_lg2.
  242. *
  243. * The value is used as part of determining the table indexes via the
  244. * equation::
  245. *
  246. * log2_mod(log2_div(VA, pt_table_item_lg2sz()), pt_num_items_lg2())
  247. */
  248. static inline unsigned int pt_num_items_lg2(const struct pt_state *pts);
  249. /**
  250. * pt_pgsz_lg2_to_level - Return the level that maps the page size
  251. * @common: Page table to query
  252. * @pgsize_lg2: Log2 page size
  253. *
  254. * Returns the table level that will map the given page size. The page
  255. * size must be part of the pt_possible_sizes() for some level.
  256. */
  257. static inline unsigned int pt_pgsz_lg2_to_level(struct pt_common *common,
  258. unsigned int pgsize_lg2);
  259. /**
  260. * pt_possible_sizes() - Return a bitmap of possible output sizes at this level
  261. * @pts: The current level
  262. *
  263. * Each level has a list of possible output sizes that can be installed as
  264. * leaf entries. If pt_can_have_leaf() is false returns zero.
  265. *
  266. * Otherwise the bit in position pt_table_item_lg2sz() should be set indicating
  267. * that a non-contiguous single item leaf entry is supported. The following
  268. * pt_num_items_lg2() number of bits can be set indicating contiguous entries
  269. * are supported. Bit pt_table_item_lg2sz() + pt_num_items_lg2() must not be
  270. * set, contiguous entries cannot span the entire table.
  271. *
  272. * The OR of pt_possible_sizes() of all levels is the typical bitmask of all
  273. * supported sizes in the entire table.
  274. */
  275. static inline pt_vaddr_t pt_possible_sizes(const struct pt_state *pts);
  276. /**
  277. * pt_table_item_lg2sz() - Size of a single item entry in this table level
  278. * @pts: The current level
  279. *
  280. * The size of the item specifies how much VA and OA a single item occupies.
  281. *
  282. * See pt_entry_oa_lg2sz() for the same value including the effect of contiguous
  283. * entries.
  284. */
  285. static inline unsigned int pt_table_item_lg2sz(const struct pt_state *pts);
  286. /**
  287. * pt_table_oa_lg2sz() - Return the VA/OA size of the entire table
  288. * @pts: The current level
  289. *
  290. * Return the size of VA decoded by the entire table level.
  291. */
  292. static inline unsigned int pt_table_oa_lg2sz(const struct pt_state *pts)
  293. {
  294. if (pts->range->top_level == pts->level)
  295. return pts->range->max_vasz_lg2;
  296. return min_t(unsigned int, pts->range->common->max_vasz_lg2,
  297. pt_num_items_lg2(pts) + pt_table_item_lg2sz(pts));
  298. }
  299. /**
  300. * pt_table_pa() - Return the CPU physical address of the table entry
  301. * @pts: Entry to query
  302. *
  303. * This is only ever called on PT_ENTRY_TABLE entries. Must return the same
  304. * value passed to pt_install_table().
  305. */
  306. static inline pt_oaddr_t pt_table_pa(const struct pt_state *pts);
  307. /**
  308. * pt_table_ptr() - Return a CPU pointer for a table item
  309. * @pts: Entry to query
  310. *
  311. * Same as pt_table_pa() but returns a CPU pointer.
  312. */
  313. static inline struct pt_table_p *pt_table_ptr(const struct pt_state *pts)
  314. {
  315. return __va(pt_table_pa(pts));
  316. }
  317. /**
  318. * pt_max_sw_bit() - Return the maximum software bit usable for any level and
  319. * entry
  320. * @common: Page table
  321. *
  322. * The swbit can be passed as bitnr to the other sw_bit functions.
  323. */
  324. static inline unsigned int pt_max_sw_bit(struct pt_common *common);
  325. /**
  326. * pt_test_sw_bit_acquire() - Read a software bit in an item
  327. * @pts: Entry to read
  328. * @bitnr: Bit to read
  329. *
  330. * Software bits are ignored by HW and can be used for any purpose by the
  331. * software. This does a test bit and acquire operation.
  332. */
  333. static inline bool pt_test_sw_bit_acquire(struct pt_state *pts,
  334. unsigned int bitnr);
  335. /**
  336. * pt_set_sw_bit_release() - Set a software bit in an item
  337. * @pts: Entry to set
  338. * @bitnr: Bit to set
  339. *
  340. * Software bits are ignored by HW and can be used for any purpose by the
  341. * software. This does a set bit and release operation.
  342. */
  343. static inline void pt_set_sw_bit_release(struct pt_state *pts,
  344. unsigned int bitnr);
  345. /**
  346. * pt_load_entry() - Read from the location pts points at into the pts
  347. * @pts: Table index to load
  348. *
  349. * Set the type of entry that was loaded. pts->entry and pts->table_lower
  350. * will be filled in with the entry's content.
  351. */
  352. static inline void pt_load_entry(struct pt_state *pts)
  353. {
  354. pts->type = pt_load_entry_raw(pts);
  355. if (pts->type == PT_ENTRY_TABLE)
  356. pts->table_lower = pt_table_ptr(pts);
  357. }
  358. #endif