internal.h 8.8 KB

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