internal.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * AMD Address Translation Library
  4. *
  5. * internal.h : Helper functions and common defines
  6. *
  7. * Copyright (c) 2023, Advanced Micro Devices, Inc.
  8. * All Rights Reserved.
  9. *
  10. * Author: Yazen Ghannam <Yazen.Ghannam@amd.com>
  11. */
  12. #ifndef __AMD_ATL_INTERNAL_H__
  13. #define __AMD_ATL_INTERNAL_H__
  14. #include <linux/bitfield.h>
  15. #include <linux/bitops.h>
  16. #include <linux/ras.h>
  17. #include <asm/amd/nb.h>
  18. #include <asm/amd/node.h>
  19. #include "reg_fields.h"
  20. #undef pr_fmt
  21. #define pr_fmt(fmt) "amd_atl: " fmt
  22. /* Maximum possible number of Coherent Stations within a single Data Fabric. */
  23. #define MAX_COH_ST_CHANNELS 32
  24. /* PCI ID for Zen4 Server DF Function 0. */
  25. #define DF_FUNC0_ID_ZEN4_SERVER 0x14AD1022
  26. /* PCI IDs for MI300 DF Function 0. */
  27. #define DF_FUNC0_ID_MI300 0x15281022
  28. /* Shift needed for adjusting register values to true values. */
  29. #define DF_DRAM_BASE_LIMIT_LSB 28
  30. #define MI300_DRAM_LIMIT_LSB 20
  31. #define INVALID_SPA ~0ULL
  32. enum df_revisions {
  33. UNKNOWN,
  34. DF2,
  35. DF3,
  36. DF3p5,
  37. DF4,
  38. DF4p5,
  39. };
  40. /* These are mapped 1:1 to the hardware values. Special cases are set at > 0x20. */
  41. enum intlv_modes {
  42. NONE = 0x00,
  43. NOHASH_2CHAN = 0x01,
  44. NOHASH_4CHAN = 0x03,
  45. NOHASH_8CHAN = 0x05,
  46. DF3_6CHAN = 0x06,
  47. NOHASH_16CHAN = 0x07,
  48. NOHASH_32CHAN = 0x08,
  49. DF3_COD4_2CHAN_HASH = 0x0C,
  50. DF3_COD2_4CHAN_HASH = 0x0D,
  51. DF3_COD1_8CHAN_HASH = 0x0E,
  52. DF4_NPS4_2CHAN_HASH = 0x10,
  53. DF4_NPS2_4CHAN_HASH = 0x11,
  54. DF4_NPS1_8CHAN_HASH = 0x12,
  55. DF4_NPS4_3CHAN_HASH = 0x13,
  56. DF4_NPS2_6CHAN_HASH = 0x14,
  57. DF4_NPS1_12CHAN_HASH = 0x15,
  58. DF4_NPS2_5CHAN_HASH = 0x16,
  59. DF4_NPS1_10CHAN_HASH = 0x17,
  60. MI3_HASH_8CHAN = 0x18,
  61. MI3_HASH_16CHAN = 0x19,
  62. MI3_HASH_32CHAN = 0x1A,
  63. DF2_2CHAN_HASH = 0x21,
  64. /* DF4.5 modes are all IntLvNumChan + 0x20 */
  65. DF4p5_NPS1_16CHAN_1K_HASH = 0x2C,
  66. DF4p5_NPS0_24CHAN_1K_HASH = 0x2E,
  67. DF4p5_NPS4_2CHAN_1K_HASH = 0x30,
  68. DF4p5_NPS2_4CHAN_1K_HASH = 0x31,
  69. DF4p5_NPS1_8CHAN_1K_HASH = 0x32,
  70. DF4p5_NPS4_3CHAN_1K_HASH = 0x33,
  71. DF4p5_NPS2_6CHAN_1K_HASH = 0x34,
  72. DF4p5_NPS1_12CHAN_1K_HASH = 0x35,
  73. DF4p5_NPS2_5CHAN_1K_HASH = 0x36,
  74. DF4p5_NPS1_10CHAN_1K_HASH = 0x37,
  75. DF4p5_NPS4_2CHAN_2K_HASH = 0x40,
  76. DF4p5_NPS2_4CHAN_2K_HASH = 0x41,
  77. DF4p5_NPS1_8CHAN_2K_HASH = 0x42,
  78. DF4p5_NPS1_16CHAN_2K_HASH = 0x43,
  79. DF4p5_NPS4_3CHAN_2K_HASH = 0x44,
  80. DF4p5_NPS2_6CHAN_2K_HASH = 0x45,
  81. DF4p5_NPS1_12CHAN_2K_HASH = 0x46,
  82. DF4p5_NPS0_24CHAN_2K_HASH = 0x47,
  83. DF4p5_NPS2_5CHAN_2K_HASH = 0x48,
  84. DF4p5_NPS1_10CHAN_2K_HASH = 0x49,
  85. };
  86. struct df4p5_denorm_ctx {
  87. /* Indicates the number of "lost" bits. This will be 1, 2, or 3. */
  88. u8 perm_shift;
  89. /* A mask indicating the bits that need to be rehashed. */
  90. u16 rehash_vector;
  91. /*
  92. * Represents the value that the high bits of the normalized address
  93. * are divided by during normalization. This value will be 3 for
  94. * interleave modes with a number of channels divisible by 3 or the
  95. * value will be 5 for interleave modes with a number of channels
  96. * divisible by 5. Power-of-two interleave modes are handled
  97. * separately.
  98. */
  99. u8 mod_value;
  100. /*
  101. * Represents the bits that can be directly pulled from the normalized
  102. * address. In each case, pass through bits [7:0] of the normalized
  103. * address. The other bits depend on the interleave bit position which
  104. * will be bit 10 for 1K interleave stripe cases and bit 11 for 2K
  105. * interleave stripe cases.
  106. */
  107. u64 base_denorm_addr;
  108. /*
  109. * Represents the high bits of the physical address that have been
  110. * divided by the mod_value.
  111. */
  112. u64 div_addr;
  113. u64 current_spa;
  114. u64 resolved_spa;
  115. u16 coh_st_fabric_id;
  116. };
  117. struct df_flags {
  118. __u8 legacy_ficaa : 1,
  119. socket_id_shift_quirk : 1,
  120. heterogeneous : 1,
  121. prm_only : 1,
  122. __reserved_0 : 4;
  123. };
  124. struct df_config {
  125. enum df_revisions rev;
  126. /*
  127. * These masks operate on the 16-bit Coherent Station IDs,
  128. * e.g. Instance, Fabric, Destination, etc.
  129. */
  130. u16 component_id_mask;
  131. u16 die_id_mask;
  132. u16 node_id_mask;
  133. u16 socket_id_mask;
  134. /*
  135. * Least-significant bit of Node ID portion of the
  136. * system-wide Coherent Station Fabric ID.
  137. */
  138. u8 node_id_shift;
  139. /*
  140. * Least-significant bit of Die portion of the Node ID.
  141. * Adjusted to include the Node ID shift in order to apply
  142. * to the Coherent Station Fabric ID.
  143. */
  144. u8 die_id_shift;
  145. /*
  146. * Least-significant bit of Socket portion of the Node ID.
  147. * Adjusted to include the Node ID shift in order to apply
  148. * to the Coherent Station Fabric ID.
  149. */
  150. u8 socket_id_shift;
  151. /* Number of DRAM Address maps visible in a Coherent Station. */
  152. u8 num_coh_st_maps;
  153. u32 dram_hole_base;
  154. /* Global flags to handle special cases. */
  155. struct df_flags flags;
  156. };
  157. extern struct df_config df_cfg;
  158. struct dram_addr_map {
  159. /*
  160. * Each DRAM Address Map can operate independently
  161. * in different interleaving modes.
  162. */
  163. enum intlv_modes intlv_mode;
  164. /* System-wide number for this address map. */
  165. u8 num;
  166. /* Raw register values */
  167. u32 base;
  168. u32 limit;
  169. u32 ctl;
  170. u32 intlv;
  171. /*
  172. * Logical to Physical Coherent Station Remapping array
  173. *
  174. * Index: Logical Coherent Station Instance ID
  175. * Value: Physical Coherent Station Instance ID
  176. *
  177. * phys_coh_st_inst_id = remap_array[log_coh_st_inst_id]
  178. */
  179. u8 remap_array[MAX_COH_ST_CHANNELS];
  180. /*
  181. * Number of bits covering DRAM Address map 0
  182. * when interleaving is non-power-of-2.
  183. *
  184. * Used only for DF3_6CHAN.
  185. */
  186. u8 np2_bits;
  187. /* Position of the 'interleave bit'. */
  188. u8 intlv_bit_pos;
  189. /* Number of channels interleaved in this map. */
  190. u8 num_intlv_chan;
  191. /* Number of dies interleaved in this map. */
  192. u8 num_intlv_dies;
  193. /* Number of sockets interleaved in this map. */
  194. u8 num_intlv_sockets;
  195. /*
  196. * Total number of channels interleaved accounting
  197. * for die and socket interleaving.
  198. */
  199. u8 total_intlv_chan;
  200. /* Total bits needed to cover 'total_intlv_chan'. */
  201. u8 total_intlv_bits;
  202. };
  203. /* Original input values cached for debug printing. */
  204. struct addr_ctx_inputs {
  205. u64 norm_addr;
  206. u8 socket_id;
  207. u8 die_id;
  208. u8 coh_st_inst_id;
  209. };
  210. struct addr_ctx {
  211. u64 ret_addr;
  212. struct addr_ctx_inputs inputs;
  213. struct dram_addr_map map;
  214. /* AMD Node ID calculated from Socket and Die IDs. */
  215. u8 node_id;
  216. /*
  217. * Coherent Station Instance ID
  218. * Local ID used within a 'node'.
  219. */
  220. u16 inst_id;
  221. /*
  222. * Coherent Station Fabric ID
  223. * System-wide ID that includes 'node' bits.
  224. */
  225. u16 coh_st_fabric_id;
  226. };
  227. int df_indirect_read_instance(u16 node, u8 func, u16 reg, u8 instance_id, u32 *lo);
  228. int df_indirect_read_broadcast(u16 node, u8 func, u16 reg, u32 *lo);
  229. int get_df_system_info(void);
  230. int determine_node_id(struct addr_ctx *ctx, u8 socket_num, u8 die_num);
  231. int get_umc_info_mi300(void);
  232. int get_address_map(struct addr_ctx *ctx);
  233. int denormalize_address(struct addr_ctx *ctx);
  234. int dehash_address(struct addr_ctx *ctx);
  235. unsigned long norm_to_sys_addr(u8 socket_id, u8 die_id, u8 coh_st_inst_id, unsigned long addr);
  236. unsigned long convert_umc_mca_addr_to_sys_addr(struct atl_err *err);
  237. u64 add_base_and_hole(struct addr_ctx *ctx, u64 addr);
  238. u64 remove_base_and_hole(struct addr_ctx *ctx, u64 addr);
  239. /* GUIDs for PRM handlers */
  240. extern const guid_t norm_to_sys_guid;
  241. #ifdef CONFIG_AMD_ATL_PRM
  242. unsigned long prm_umc_norm_to_sys_addr(u8 socket_id, u64 umc_bank_inst_id, unsigned long addr);
  243. #else
  244. static inline unsigned long prm_umc_norm_to_sys_addr(u8 socket_id, u64 umc_bank_inst_id,
  245. unsigned long addr)
  246. {
  247. return -ENODEV;
  248. }
  249. #endif
  250. /*
  251. * Make a gap in @data that is @num_bits long starting at @bit_num.
  252. * e.g. data = 11111111'b
  253. * bit_num = 3
  254. * num_bits = 2
  255. * result = 1111100111'b
  256. */
  257. static inline u64 expand_bits(u8 bit_num, u8 num_bits, u64 data)
  258. {
  259. u64 temp1, temp2;
  260. if (!num_bits)
  261. return data;
  262. if (!bit_num) {
  263. WARN_ON_ONCE(num_bits >= BITS_PER_LONG);
  264. return data << num_bits;
  265. }
  266. WARN_ON_ONCE(bit_num >= BITS_PER_LONG);
  267. temp1 = data & GENMASK_ULL(bit_num - 1, 0);
  268. temp2 = data & GENMASK_ULL(63, bit_num);
  269. temp2 <<= num_bits;
  270. return temp1 | temp2;
  271. }
  272. /*
  273. * Remove bits in @data between @low_bit and @high_bit inclusive.
  274. * e.g. data = XXXYYZZZ'b
  275. * low_bit = 3
  276. * high_bit = 4
  277. * result = XXXZZZ'b
  278. */
  279. static inline u64 remove_bits(u8 low_bit, u8 high_bit, u64 data)
  280. {
  281. u64 temp1, temp2;
  282. WARN_ON_ONCE(high_bit >= BITS_PER_LONG);
  283. WARN_ON_ONCE(low_bit >= BITS_PER_LONG);
  284. WARN_ON_ONCE(low_bit > high_bit);
  285. if (!low_bit)
  286. return data >> (high_bit++);
  287. temp1 = GENMASK_ULL(low_bit - 1, 0) & data;
  288. temp2 = GENMASK_ULL(63, high_bit + 1) & data;
  289. temp2 >>= high_bit - low_bit + 1;
  290. return temp1 | temp2;
  291. }
  292. #define atl_debug(ctx, fmt, arg...) \
  293. pr_debug("socket_id=%u die_id=%u coh_st_inst_id=%u norm_addr=0x%016llx: " fmt,\
  294. (ctx)->inputs.socket_id, (ctx)->inputs.die_id,\
  295. (ctx)->inputs.coh_st_inst_id, (ctx)->inputs.norm_addr, ##arg)
  296. static inline void atl_debug_on_bad_df_rev(void)
  297. {
  298. pr_debug("Unrecognized DF rev: %u", df_cfg.rev);
  299. }
  300. static inline void atl_debug_on_bad_intlv_mode(struct addr_ctx *ctx)
  301. {
  302. atl_debug(ctx, "Unrecognized interleave mode: %u", ctx->map.intlv_mode);
  303. }
  304. #define MI300_UMC_MCA_COL GENMASK(5, 1)
  305. #define MI300_UMC_MCA_ROW13 BIT(23)
  306. #endif /* __AMD_ATL_INTERNAL_H__ */