denormalize.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * AMD Address Translation Library
  4. *
  5. * denormalize.c : Functions to account for interleaving bits
  6. *
  7. * Copyright (c) 2023, Advanced Micro Devices, Inc.
  8. * All Rights Reserved.
  9. *
  10. * Author: Yazen Ghannam <Yazen.Ghannam@amd.com>
  11. */
  12. #include "internal.h"
  13. /*
  14. * Returns the Destination Fabric ID. This is the first (lowest)
  15. * COH_ST Fabric ID used within a DRAM Address map.
  16. */
  17. static u16 get_dst_fabric_id(struct addr_ctx *ctx)
  18. {
  19. switch (df_cfg.rev) {
  20. case DF2: return FIELD_GET(DF2_DST_FABRIC_ID, ctx->map.limit);
  21. case DF3: return FIELD_GET(DF3_DST_FABRIC_ID, ctx->map.limit);
  22. case DF3p5: return FIELD_GET(DF3p5_DST_FABRIC_ID, ctx->map.limit);
  23. case DF4: return FIELD_GET(DF4_DST_FABRIC_ID, ctx->map.ctl);
  24. case DF4p5: return FIELD_GET(DF4p5_DST_FABRIC_ID, ctx->map.ctl);
  25. default:
  26. atl_debug_on_bad_df_rev();
  27. return 0;
  28. }
  29. }
  30. /*
  31. * Make a contiguous gap in address for N bits starting at bit P.
  32. *
  33. * Example:
  34. * address bits: [20:0]
  35. * # of interleave bits (n): 3
  36. * starting interleave bit (p): 8
  37. *
  38. * expanded address bits: [20+n : n+p][n+p-1 : p][p-1 : 0]
  39. * [23 : 11][10 : 8][7 : 0]
  40. */
  41. static u64 make_space_for_coh_st_id_at_intlv_bit(struct addr_ctx *ctx)
  42. {
  43. return expand_bits(ctx->map.intlv_bit_pos,
  44. ctx->map.total_intlv_bits,
  45. ctx->ret_addr);
  46. }
  47. /*
  48. * Make two gaps in address for N bits.
  49. * First gap is a single bit at bit P.
  50. * Second gap is the remaining N-1 bits at bit 12.
  51. *
  52. * Example:
  53. * address bits: [20:0]
  54. * # of interleave bits (n): 3
  55. * starting interleave bit (p): 8
  56. *
  57. * First gap
  58. * expanded address bits: [20+1 : p+1][p][p-1 : 0]
  59. * [21 : 9][8][7 : 0]
  60. *
  61. * Second gap uses result from first.
  62. * r = n - 1; remaining interleave bits
  63. * expanded address bits: [21+r : 12+r][12+r-1: 12][11 : 0]
  64. * [23 : 14][13 : 12][11 : 0]
  65. */
  66. static u64 make_space_for_coh_st_id_split_2_1(struct addr_ctx *ctx)
  67. {
  68. /* Make a single space at the interleave bit. */
  69. u64 denorm_addr = expand_bits(ctx->map.intlv_bit_pos, 1, ctx->ret_addr);
  70. /* Done if there's only a single interleave bit. */
  71. if (ctx->map.total_intlv_bits <= 1)
  72. return denorm_addr;
  73. /* Make spaces for the remaining interleave bits starting at bit 12. */
  74. return expand_bits(12, ctx->map.total_intlv_bits - 1, denorm_addr);
  75. }
  76. /*
  77. * Make space for CS ID at bits [14:8] as follows:
  78. *
  79. * 8 channels -> bits [10:8]
  80. * 16 channels -> bits [11:8]
  81. * 32 channels -> bits [14,11:8]
  82. *
  83. * 1 die -> N/A
  84. * 2 dies -> bit [12]
  85. * 4 dies -> bits [13:12]
  86. */
  87. static u64 make_space_for_coh_st_id_mi300(struct addr_ctx *ctx)
  88. {
  89. u8 num_intlv_bits = ilog2(ctx->map.num_intlv_chan);
  90. u64 denorm_addr;
  91. if (ctx->map.intlv_bit_pos != 8) {
  92. pr_debug("Invalid interleave bit: %u", ctx->map.intlv_bit_pos);
  93. return ~0ULL;
  94. }
  95. /* Channel bits. Covers up to 4 bits at [11:8]. */
  96. denorm_addr = expand_bits(8, min(num_intlv_bits, 4), ctx->ret_addr);
  97. /* Die bits. Always starts at [12]. */
  98. denorm_addr = expand_bits(12, ilog2(ctx->map.num_intlv_dies), denorm_addr);
  99. /* Additional channel bit at [14]. */
  100. if (num_intlv_bits > 4)
  101. denorm_addr = expand_bits(14, 1, denorm_addr);
  102. return denorm_addr;
  103. }
  104. /*
  105. * Take the current calculated address and shift enough bits in the middle
  106. * to make a gap where the interleave bits will be inserted.
  107. */
  108. static u64 make_space_for_coh_st_id(struct addr_ctx *ctx)
  109. {
  110. switch (ctx->map.intlv_mode) {
  111. case NOHASH_2CHAN:
  112. case NOHASH_4CHAN:
  113. case NOHASH_8CHAN:
  114. case NOHASH_16CHAN:
  115. case NOHASH_32CHAN:
  116. case DF2_2CHAN_HASH:
  117. return make_space_for_coh_st_id_at_intlv_bit(ctx);
  118. case DF3_COD4_2CHAN_HASH:
  119. case DF3_COD2_4CHAN_HASH:
  120. case DF3_COD1_8CHAN_HASH:
  121. case DF4_NPS4_2CHAN_HASH:
  122. case DF4_NPS2_4CHAN_HASH:
  123. case DF4_NPS1_8CHAN_HASH:
  124. case DF4p5_NPS4_2CHAN_1K_HASH:
  125. case DF4p5_NPS4_2CHAN_2K_HASH:
  126. case DF4p5_NPS2_4CHAN_2K_HASH:
  127. case DF4p5_NPS1_8CHAN_2K_HASH:
  128. case DF4p5_NPS1_16CHAN_2K_HASH:
  129. return make_space_for_coh_st_id_split_2_1(ctx);
  130. case MI3_HASH_8CHAN:
  131. case MI3_HASH_16CHAN:
  132. case MI3_HASH_32CHAN:
  133. return make_space_for_coh_st_id_mi300(ctx);
  134. default:
  135. atl_debug_on_bad_intlv_mode(ctx);
  136. return ~0ULL;
  137. }
  138. }
  139. static u16 get_coh_st_id_df2(struct addr_ctx *ctx)
  140. {
  141. u8 num_socket_intlv_bits = ilog2(ctx->map.num_intlv_sockets);
  142. u8 num_die_intlv_bits = ilog2(ctx->map.num_intlv_dies);
  143. u8 num_intlv_bits;
  144. u16 coh_st_id, mask;
  145. coh_st_id = ctx->coh_st_fabric_id - get_dst_fabric_id(ctx);
  146. /* Channel interleave bits */
  147. num_intlv_bits = order_base_2(ctx->map.num_intlv_chan);
  148. mask = GENMASK(num_intlv_bits - 1, 0);
  149. coh_st_id &= mask;
  150. /* Die interleave bits */
  151. if (num_die_intlv_bits) {
  152. u16 die_bits;
  153. mask = GENMASK(num_die_intlv_bits - 1, 0);
  154. die_bits = ctx->coh_st_fabric_id & df_cfg.die_id_mask;
  155. die_bits >>= df_cfg.die_id_shift;
  156. coh_st_id |= (die_bits & mask) << num_intlv_bits;
  157. num_intlv_bits += num_die_intlv_bits;
  158. }
  159. /* Socket interleave bits */
  160. if (num_socket_intlv_bits) {
  161. u16 socket_bits;
  162. mask = GENMASK(num_socket_intlv_bits - 1, 0);
  163. socket_bits = ctx->coh_st_fabric_id & df_cfg.socket_id_mask;
  164. socket_bits >>= df_cfg.socket_id_shift;
  165. coh_st_id |= (socket_bits & mask) << num_intlv_bits;
  166. }
  167. return coh_st_id;
  168. }
  169. static u16 get_coh_st_id_df4(struct addr_ctx *ctx)
  170. {
  171. /*
  172. * Start with the original component mask and the number of interleave
  173. * bits for the channels in this map.
  174. */
  175. u8 num_intlv_bits = ilog2(ctx->map.num_intlv_chan);
  176. u16 mask = df_cfg.component_id_mask;
  177. u16 socket_bits;
  178. /* Set the derived Coherent Station ID to the input Coherent Station Fabric ID. */
  179. u16 coh_st_id = ctx->coh_st_fabric_id & mask;
  180. /*
  181. * Subtract the "base" Destination Fabric ID.
  182. * This accounts for systems with disabled Coherent Stations.
  183. */
  184. coh_st_id -= get_dst_fabric_id(ctx) & mask;
  185. /*
  186. * Generate and use a new mask based on the number of bits
  187. * needed for channel interleaving in this map.
  188. */
  189. mask = GENMASK(num_intlv_bits - 1, 0);
  190. coh_st_id &= mask;
  191. /* Done if socket interleaving is not enabled. */
  192. if (ctx->map.num_intlv_sockets <= 1)
  193. return coh_st_id;
  194. /*
  195. * Figure out how many bits are needed for the number of
  196. * interleaved sockets. And shift the derived Coherent Station ID to account
  197. * for these.
  198. */
  199. num_intlv_bits = ilog2(ctx->map.num_intlv_sockets);
  200. coh_st_id <<= num_intlv_bits;
  201. /* Generate a new mask for the socket interleaving bits. */
  202. mask = GENMASK(num_intlv_bits - 1, 0);
  203. /* Get the socket interleave bits from the original Coherent Station Fabric ID. */
  204. socket_bits = (ctx->coh_st_fabric_id & df_cfg.socket_id_mask) >> df_cfg.socket_id_shift;
  205. /* Apply the appropriate socket bits to the derived Coherent Station ID. */
  206. coh_st_id |= socket_bits & mask;
  207. return coh_st_id;
  208. }
  209. /*
  210. * MI300 hash has:
  211. * (C)hannel[3:0] = coh_st_id[3:0]
  212. * (S)tack[0] = coh_st_id[4]
  213. * (D)ie[1:0] = coh_st_id[6:5]
  214. *
  215. * Hashed coh_st_id is swizzled so that Stack bit is at the end.
  216. * coh_st_id = SDDCCCC
  217. */
  218. static u16 get_coh_st_id_mi300(struct addr_ctx *ctx)
  219. {
  220. u8 channel_bits, die_bits, stack_bit;
  221. u16 die_id;
  222. /* Subtract the "base" Destination Fabric ID. */
  223. ctx->coh_st_fabric_id -= get_dst_fabric_id(ctx);
  224. die_id = (ctx->coh_st_fabric_id & df_cfg.die_id_mask) >> df_cfg.die_id_shift;
  225. channel_bits = FIELD_GET(GENMASK(3, 0), ctx->coh_st_fabric_id);
  226. stack_bit = FIELD_GET(BIT(4), ctx->coh_st_fabric_id) << 6;
  227. die_bits = die_id << 4;
  228. return stack_bit | die_bits | channel_bits;
  229. }
  230. /*
  231. * Derive the correct Coherent Station ID that represents the interleave bits
  232. * used within the system physical address. This accounts for the
  233. * interleave mode, number of interleaved channels/dies/sockets, and
  234. * other system/mode-specific bit swizzling.
  235. *
  236. * Returns: Coherent Station ID on success.
  237. * All bits set on error.
  238. */
  239. static u16 calculate_coh_st_id(struct addr_ctx *ctx)
  240. {
  241. switch (ctx->map.intlv_mode) {
  242. case NOHASH_2CHAN:
  243. case NOHASH_4CHAN:
  244. case NOHASH_8CHAN:
  245. case NOHASH_16CHAN:
  246. case NOHASH_32CHAN:
  247. case DF3_COD4_2CHAN_HASH:
  248. case DF3_COD2_4CHAN_HASH:
  249. case DF3_COD1_8CHAN_HASH:
  250. case DF2_2CHAN_HASH:
  251. return get_coh_st_id_df2(ctx);
  252. case DF4_NPS4_2CHAN_HASH:
  253. case DF4_NPS2_4CHAN_HASH:
  254. case DF4_NPS1_8CHAN_HASH:
  255. case DF4p5_NPS4_2CHAN_1K_HASH:
  256. case DF4p5_NPS4_2CHAN_2K_HASH:
  257. case DF4p5_NPS2_4CHAN_2K_HASH:
  258. case DF4p5_NPS1_8CHAN_2K_HASH:
  259. case DF4p5_NPS1_16CHAN_2K_HASH:
  260. return get_coh_st_id_df4(ctx);
  261. case MI3_HASH_8CHAN:
  262. case MI3_HASH_16CHAN:
  263. case MI3_HASH_32CHAN:
  264. return get_coh_st_id_mi300(ctx);
  265. /* COH_ST ID is simply the COH_ST Fabric ID adjusted by the Destination Fabric ID. */
  266. case DF4p5_NPS2_4CHAN_1K_HASH:
  267. case DF4p5_NPS1_8CHAN_1K_HASH:
  268. case DF4p5_NPS1_16CHAN_1K_HASH:
  269. return ctx->coh_st_fabric_id - get_dst_fabric_id(ctx);
  270. default:
  271. atl_debug_on_bad_intlv_mode(ctx);
  272. return ~0;
  273. }
  274. }
  275. static u64 insert_coh_st_id_at_intlv_bit(struct addr_ctx *ctx, u64 denorm_addr, u16 coh_st_id)
  276. {
  277. return denorm_addr | (coh_st_id << ctx->map.intlv_bit_pos);
  278. }
  279. static u64 insert_coh_st_id_split_2_1(struct addr_ctx *ctx, u64 denorm_addr, u16 coh_st_id)
  280. {
  281. /* Insert coh_st_id[0] at the interleave bit. */
  282. denorm_addr |= (coh_st_id & BIT(0)) << ctx->map.intlv_bit_pos;
  283. /* Insert coh_st_id[2:1] at bit 12. */
  284. denorm_addr |= (coh_st_id & GENMASK(2, 1)) << 11;
  285. return denorm_addr;
  286. }
  287. static u64 insert_coh_st_id_split_2_2(struct addr_ctx *ctx, u64 denorm_addr, u16 coh_st_id)
  288. {
  289. /* Insert coh_st_id[1:0] at bit 8. */
  290. denorm_addr |= (coh_st_id & GENMASK(1, 0)) << 8;
  291. /*
  292. * Insert coh_st_id[n:2] at bit 12. 'n' could be 2 or 3.
  293. * Grab both because bit 3 will be clear if unused.
  294. */
  295. denorm_addr |= (coh_st_id & GENMASK(3, 2)) << 10;
  296. return denorm_addr;
  297. }
  298. static u64 insert_coh_st_id(struct addr_ctx *ctx, u64 denorm_addr, u16 coh_st_id)
  299. {
  300. switch (ctx->map.intlv_mode) {
  301. case NOHASH_2CHAN:
  302. case NOHASH_4CHAN:
  303. case NOHASH_8CHAN:
  304. case NOHASH_16CHAN:
  305. case NOHASH_32CHAN:
  306. case MI3_HASH_8CHAN:
  307. case MI3_HASH_16CHAN:
  308. case MI3_HASH_32CHAN:
  309. case DF2_2CHAN_HASH:
  310. return insert_coh_st_id_at_intlv_bit(ctx, denorm_addr, coh_st_id);
  311. case DF3_COD4_2CHAN_HASH:
  312. case DF3_COD2_4CHAN_HASH:
  313. case DF3_COD1_8CHAN_HASH:
  314. case DF4_NPS4_2CHAN_HASH:
  315. case DF4_NPS2_4CHAN_HASH:
  316. case DF4_NPS1_8CHAN_HASH:
  317. case DF4p5_NPS4_2CHAN_1K_HASH:
  318. case DF4p5_NPS4_2CHAN_2K_HASH:
  319. case DF4p5_NPS2_4CHAN_2K_HASH:
  320. case DF4p5_NPS1_8CHAN_2K_HASH:
  321. case DF4p5_NPS1_16CHAN_2K_HASH:
  322. return insert_coh_st_id_split_2_1(ctx, denorm_addr, coh_st_id);
  323. case DF4p5_NPS2_4CHAN_1K_HASH:
  324. case DF4p5_NPS1_8CHAN_1K_HASH:
  325. case DF4p5_NPS1_16CHAN_1K_HASH:
  326. return insert_coh_st_id_split_2_2(ctx, denorm_addr, coh_st_id);
  327. default:
  328. atl_debug_on_bad_intlv_mode(ctx);
  329. return ~0ULL;
  330. }
  331. }
  332. /*
  333. * MI300 systems have a fixed, hardware-defined physical-to-logical
  334. * Coherent Station mapping. The Remap registers are not used.
  335. */
  336. static const u16 phy_to_log_coh_st_map_mi300[] = {
  337. 12, 13, 14, 15,
  338. 8, 9, 10, 11,
  339. 4, 5, 6, 7,
  340. 0, 1, 2, 3,
  341. 28, 29, 30, 31,
  342. 24, 25, 26, 27,
  343. 20, 21, 22, 23,
  344. 16, 17, 18, 19,
  345. };
  346. static u16 get_logical_coh_st_fabric_id_mi300(struct addr_ctx *ctx)
  347. {
  348. if (ctx->inst_id >= ARRAY_SIZE(phy_to_log_coh_st_map_mi300)) {
  349. atl_debug(ctx, "Instance ID out of range");
  350. return ~0;
  351. }
  352. return phy_to_log_coh_st_map_mi300[ctx->inst_id] | (ctx->node_id << df_cfg.node_id_shift);
  353. }
  354. static u16 get_logical_coh_st_fabric_id(struct addr_ctx *ctx)
  355. {
  356. u16 component_id, log_fabric_id;
  357. /* Start with the physical COH_ST Fabric ID. */
  358. u16 phys_fabric_id = ctx->coh_st_fabric_id;
  359. if (df_cfg.rev == DF4p5 && df_cfg.flags.heterogeneous)
  360. return get_logical_coh_st_fabric_id_mi300(ctx);
  361. /* Skip logical ID lookup if remapping is disabled. */
  362. if (!FIELD_GET(DF4_REMAP_EN, ctx->map.ctl) &&
  363. ctx->map.intlv_mode != DF3_6CHAN)
  364. return phys_fabric_id;
  365. /* Mask off the Node ID bits to get the "local" Component ID. */
  366. component_id = phys_fabric_id & df_cfg.component_id_mask;
  367. /*
  368. * Search the list of logical Component IDs for the one that
  369. * matches this physical Component ID.
  370. */
  371. for (log_fabric_id = 0; log_fabric_id < MAX_COH_ST_CHANNELS; log_fabric_id++) {
  372. if (ctx->map.remap_array[log_fabric_id] == component_id)
  373. break;
  374. }
  375. if (log_fabric_id == MAX_COH_ST_CHANNELS)
  376. atl_debug(ctx, "COH_ST remap entry not found for 0x%x",
  377. log_fabric_id);
  378. /* Get the Node ID bits from the physical and apply to the logical. */
  379. return (phys_fabric_id & df_cfg.node_id_mask) | log_fabric_id;
  380. }
  381. static u16 get_logical_coh_st_fabric_id_for_current_spa(struct addr_ctx *ctx,
  382. struct df4p5_denorm_ctx *denorm_ctx)
  383. {
  384. bool hash_ctl_64k, hash_ctl_2M, hash_ctl_1G, hash_ctl_1T;
  385. bool hash_pa8, hash_pa9, hash_pa12, hash_pa13;
  386. u64 cs_id = 0;
  387. hash_ctl_64k = FIELD_GET(DF4_HASH_CTL_64K, ctx->map.ctl);
  388. hash_ctl_2M = FIELD_GET(DF4_HASH_CTL_2M, ctx->map.ctl);
  389. hash_ctl_1G = FIELD_GET(DF4_HASH_CTL_1G, ctx->map.ctl);
  390. hash_ctl_1T = FIELD_GET(DF4p5_HASH_CTL_1T, ctx->map.ctl);
  391. hash_pa8 = FIELD_GET(BIT_ULL(8), denorm_ctx->current_spa);
  392. hash_pa8 ^= FIELD_GET(BIT_ULL(14), denorm_ctx->current_spa);
  393. hash_pa8 ^= FIELD_GET(BIT_ULL(16), denorm_ctx->current_spa) & hash_ctl_64k;
  394. hash_pa8 ^= FIELD_GET(BIT_ULL(21), denorm_ctx->current_spa) & hash_ctl_2M;
  395. hash_pa8 ^= FIELD_GET(BIT_ULL(30), denorm_ctx->current_spa) & hash_ctl_1G;
  396. hash_pa8 ^= FIELD_GET(BIT_ULL(40), denorm_ctx->current_spa) & hash_ctl_1T;
  397. hash_pa9 = FIELD_GET(BIT_ULL(9), denorm_ctx->current_spa);
  398. hash_pa9 ^= FIELD_GET(BIT_ULL(17), denorm_ctx->current_spa) & hash_ctl_64k;
  399. hash_pa9 ^= FIELD_GET(BIT_ULL(22), denorm_ctx->current_spa) & hash_ctl_2M;
  400. hash_pa9 ^= FIELD_GET(BIT_ULL(31), denorm_ctx->current_spa) & hash_ctl_1G;
  401. hash_pa9 ^= FIELD_GET(BIT_ULL(41), denorm_ctx->current_spa) & hash_ctl_1T;
  402. hash_pa12 = FIELD_GET(BIT_ULL(12), denorm_ctx->current_spa);
  403. hash_pa12 ^= FIELD_GET(BIT_ULL(18), denorm_ctx->current_spa) & hash_ctl_64k;
  404. hash_pa12 ^= FIELD_GET(BIT_ULL(23), denorm_ctx->current_spa) & hash_ctl_2M;
  405. hash_pa12 ^= FIELD_GET(BIT_ULL(32), denorm_ctx->current_spa) & hash_ctl_1G;
  406. hash_pa12 ^= FIELD_GET(BIT_ULL(42), denorm_ctx->current_spa) & hash_ctl_1T;
  407. hash_pa13 = FIELD_GET(BIT_ULL(13), denorm_ctx->current_spa);
  408. hash_pa13 ^= FIELD_GET(BIT_ULL(19), denorm_ctx->current_spa) & hash_ctl_64k;
  409. hash_pa13 ^= FIELD_GET(BIT_ULL(24), denorm_ctx->current_spa) & hash_ctl_2M;
  410. hash_pa13 ^= FIELD_GET(BIT_ULL(33), denorm_ctx->current_spa) & hash_ctl_1G;
  411. hash_pa13 ^= FIELD_GET(BIT_ULL(43), denorm_ctx->current_spa) & hash_ctl_1T;
  412. switch (ctx->map.intlv_mode) {
  413. case DF4p5_NPS0_24CHAN_1K_HASH:
  414. cs_id = FIELD_GET(GENMASK_ULL(63, 13), denorm_ctx->current_spa) << 3;
  415. cs_id %= denorm_ctx->mod_value;
  416. cs_id <<= 2;
  417. cs_id |= (hash_pa9 | (hash_pa12 << 1));
  418. cs_id |= hash_pa8 << df_cfg.socket_id_shift;
  419. break;
  420. case DF4p5_NPS0_24CHAN_2K_HASH:
  421. cs_id = FIELD_GET(GENMASK_ULL(63, 14), denorm_ctx->current_spa) << 4;
  422. cs_id %= denorm_ctx->mod_value;
  423. cs_id <<= 2;
  424. cs_id |= (hash_pa12 | (hash_pa13 << 1));
  425. cs_id |= hash_pa8 << df_cfg.socket_id_shift;
  426. break;
  427. case DF4p5_NPS1_12CHAN_1K_HASH:
  428. cs_id = FIELD_GET(GENMASK_ULL(63, 12), denorm_ctx->current_spa) << 2;
  429. cs_id %= denorm_ctx->mod_value;
  430. cs_id <<= 2;
  431. cs_id |= (hash_pa8 | (hash_pa9 << 1));
  432. break;
  433. case DF4p5_NPS1_12CHAN_2K_HASH:
  434. cs_id = FIELD_GET(GENMASK_ULL(63, 13), denorm_ctx->current_spa) << 3;
  435. cs_id %= denorm_ctx->mod_value;
  436. cs_id <<= 2;
  437. cs_id |= (hash_pa8 | (hash_pa12 << 1));
  438. break;
  439. case DF4p5_NPS2_6CHAN_1K_HASH:
  440. case DF4p5_NPS1_10CHAN_1K_HASH:
  441. cs_id = FIELD_GET(GENMASK_ULL(63, 12), denorm_ctx->current_spa) << 2;
  442. cs_id |= (FIELD_GET(BIT_ULL(9), denorm_ctx->current_spa) << 1);
  443. cs_id %= denorm_ctx->mod_value;
  444. cs_id <<= 1;
  445. cs_id |= hash_pa8;
  446. break;
  447. case DF4p5_NPS2_6CHAN_2K_HASH:
  448. case DF4p5_NPS1_10CHAN_2K_HASH:
  449. cs_id = FIELD_GET(GENMASK_ULL(63, 12), denorm_ctx->current_spa) << 2;
  450. cs_id %= denorm_ctx->mod_value;
  451. cs_id <<= 1;
  452. cs_id |= hash_pa8;
  453. break;
  454. case DF4p5_NPS4_3CHAN_1K_HASH:
  455. case DF4p5_NPS2_5CHAN_1K_HASH:
  456. cs_id = FIELD_GET(GENMASK_ULL(63, 12), denorm_ctx->current_spa) << 2;
  457. cs_id |= FIELD_GET(GENMASK_ULL(9, 8), denorm_ctx->current_spa);
  458. cs_id %= denorm_ctx->mod_value;
  459. break;
  460. case DF4p5_NPS4_3CHAN_2K_HASH:
  461. case DF4p5_NPS2_5CHAN_2K_HASH:
  462. cs_id = FIELD_GET(GENMASK_ULL(63, 12), denorm_ctx->current_spa) << 2;
  463. cs_id |= FIELD_GET(BIT_ULL(8), denorm_ctx->current_spa) << 1;
  464. cs_id %= denorm_ctx->mod_value;
  465. break;
  466. default:
  467. atl_debug_on_bad_intlv_mode(ctx);
  468. return 0;
  469. }
  470. if (cs_id > 0xffff) {
  471. atl_debug(ctx, "Translation error: Resulting cs_id larger than u16\n");
  472. return 0;
  473. }
  474. return cs_id;
  475. }
  476. static int denorm_addr_common(struct addr_ctx *ctx)
  477. {
  478. u64 denorm_addr;
  479. u16 coh_st_id;
  480. /*
  481. * Convert the original physical COH_ST Fabric ID to a logical value.
  482. * This is required for non-power-of-two and other interleaving modes.
  483. */
  484. ctx->coh_st_fabric_id = get_logical_coh_st_fabric_id(ctx);
  485. denorm_addr = make_space_for_coh_st_id(ctx);
  486. coh_st_id = calculate_coh_st_id(ctx);
  487. ctx->ret_addr = insert_coh_st_id(ctx, denorm_addr, coh_st_id);
  488. return 0;
  489. }
  490. static int denorm_addr_df3_6chan(struct addr_ctx *ctx)
  491. {
  492. u16 coh_st_id = ctx->coh_st_fabric_id & df_cfg.component_id_mask;
  493. u8 total_intlv_bits = ctx->map.total_intlv_bits;
  494. u8 low_bit, intlv_bit = ctx->map.intlv_bit_pos;
  495. u64 msb_intlv_bits, temp_addr_a, temp_addr_b;
  496. u8 np2_bits = ctx->map.np2_bits;
  497. if (ctx->map.intlv_mode != DF3_6CHAN)
  498. return -EINVAL;
  499. /*
  500. * 'np2_bits' holds the number of bits needed to cover the
  501. * amount of memory (rounded up) in this map using 64K chunks.
  502. *
  503. * Example:
  504. * Total memory in map: 6GB
  505. * Rounded up to next power-of-2: 8GB
  506. * Number of 64K chunks: 0x20000
  507. * np2_bits = log2(# of chunks): 17
  508. *
  509. * Get the two most-significant interleave bits from the
  510. * input address based on the following:
  511. *
  512. * [15 + np2_bits - total_intlv_bits : 14 + np2_bits - total_intlv_bits]
  513. */
  514. low_bit = 14 + np2_bits - total_intlv_bits;
  515. msb_intlv_bits = ctx->ret_addr >> low_bit;
  516. msb_intlv_bits &= 0x3;
  517. /*
  518. * If MSB are 11b, then logical COH_ST ID is 6 or 7.
  519. * Need to adjust based on the mod3 result.
  520. */
  521. if (msb_intlv_bits == 3) {
  522. u8 addr_mod, phys_addr_msb, msb_coh_st_id;
  523. /* Get the remaining interleave bits from the input address. */
  524. temp_addr_b = GENMASK_ULL(low_bit - 1, intlv_bit) & ctx->ret_addr;
  525. temp_addr_b >>= intlv_bit;
  526. /* Calculate the logical COH_ST offset based on mod3. */
  527. addr_mod = temp_addr_b % 3;
  528. /* Get COH_ST ID bits [2:1]. */
  529. msb_coh_st_id = (coh_st_id >> 1) & 0x3;
  530. /* Get the bit that starts the physical address bits. */
  531. phys_addr_msb = (intlv_bit + np2_bits + 1);
  532. phys_addr_msb &= BIT(0);
  533. phys_addr_msb++;
  534. phys_addr_msb *= 3 - addr_mod + msb_coh_st_id;
  535. phys_addr_msb %= 3;
  536. /* Move the physical address MSB to the correct place. */
  537. temp_addr_b |= phys_addr_msb << (low_bit - total_intlv_bits - intlv_bit);
  538. /* Generate a new COH_ST ID as follows: coh_st_id = [1, 1, coh_st_id[0]] */
  539. coh_st_id &= BIT(0);
  540. coh_st_id |= GENMASK(2, 1);
  541. } else {
  542. temp_addr_b = GENMASK_ULL(63, intlv_bit) & ctx->ret_addr;
  543. temp_addr_b >>= intlv_bit;
  544. }
  545. temp_addr_a = GENMASK_ULL(intlv_bit - 1, 0) & ctx->ret_addr;
  546. temp_addr_b <<= intlv_bit + total_intlv_bits;
  547. ctx->ret_addr = temp_addr_a | temp_addr_b;
  548. ctx->ret_addr |= coh_st_id << intlv_bit;
  549. return 0;
  550. }
  551. static int denorm_addr_df4_np2(struct addr_ctx *ctx)
  552. {
  553. bool hash_ctl_64k, hash_ctl_2M, hash_ctl_1G;
  554. u16 group, group_offset, log_coh_st_offset;
  555. unsigned int mod_value, shift_value;
  556. u16 mask = df_cfg.component_id_mask;
  557. u64 temp_addr_a, temp_addr_b;
  558. bool hash_pa8, hashed_bit;
  559. switch (ctx->map.intlv_mode) {
  560. case DF4_NPS4_3CHAN_HASH:
  561. mod_value = 3;
  562. shift_value = 13;
  563. break;
  564. case DF4_NPS2_6CHAN_HASH:
  565. mod_value = 3;
  566. shift_value = 12;
  567. break;
  568. case DF4_NPS1_12CHAN_HASH:
  569. mod_value = 3;
  570. shift_value = 11;
  571. break;
  572. case DF4_NPS2_5CHAN_HASH:
  573. mod_value = 5;
  574. shift_value = 13;
  575. break;
  576. case DF4_NPS1_10CHAN_HASH:
  577. mod_value = 5;
  578. shift_value = 12;
  579. break;
  580. default:
  581. atl_debug_on_bad_intlv_mode(ctx);
  582. return -EINVAL;
  583. }
  584. if (ctx->map.num_intlv_sockets == 1) {
  585. hash_pa8 = BIT_ULL(shift_value) & ctx->ret_addr;
  586. temp_addr_a = remove_bits(shift_value, shift_value, ctx->ret_addr);
  587. } else {
  588. hash_pa8 = ctx->coh_st_fabric_id & df_cfg.socket_id_mask;
  589. temp_addr_a = ctx->ret_addr;
  590. }
  591. /* Make a gap for the real bit [8]. */
  592. temp_addr_a = expand_bits(8, 1, temp_addr_a);
  593. /* Make an additional gap for bits [13:12], as appropriate.*/
  594. if (ctx->map.intlv_mode == DF4_NPS2_6CHAN_HASH ||
  595. ctx->map.intlv_mode == DF4_NPS1_10CHAN_HASH) {
  596. temp_addr_a = expand_bits(13, 1, temp_addr_a);
  597. } else if (ctx->map.intlv_mode == DF4_NPS1_12CHAN_HASH) {
  598. temp_addr_a = expand_bits(12, 2, temp_addr_a);
  599. }
  600. /* Keep bits [13:0]. */
  601. temp_addr_a &= GENMASK_ULL(13, 0);
  602. /* Get the appropriate high bits. */
  603. shift_value += 1 - ilog2(ctx->map.num_intlv_sockets);
  604. temp_addr_b = GENMASK_ULL(63, shift_value) & ctx->ret_addr;
  605. temp_addr_b >>= shift_value;
  606. temp_addr_b *= mod_value;
  607. /*
  608. * Coherent Stations are divided into groups.
  609. *
  610. * Multiples of 3 (mod3) are divided into quadrants.
  611. * e.g. NP4_3CHAN -> [0, 1, 2] [6, 7, 8]
  612. * [3, 4, 5] [9, 10, 11]
  613. *
  614. * Multiples of 5 (mod5) are divided into sides.
  615. * e.g. NP2_5CHAN -> [0, 1, 2, 3, 4] [5, 6, 7, 8, 9]
  616. */
  617. /*
  618. * Calculate the logical offset for the COH_ST within its DRAM Address map.
  619. * e.g. if map includes [5, 6, 7, 8, 9] and target instance is '8', then
  620. * log_coh_st_offset = 8 - 5 = 3
  621. */
  622. log_coh_st_offset = (ctx->coh_st_fabric_id & mask) - (get_dst_fabric_id(ctx) & mask);
  623. /*
  624. * Figure out the group number.
  625. *
  626. * Following above example,
  627. * log_coh_st_offset = 3
  628. * mod_value = 5
  629. * group = 3 / 5 = 0
  630. */
  631. group = log_coh_st_offset / mod_value;
  632. /*
  633. * Figure out the offset within the group.
  634. *
  635. * Following above example,
  636. * log_coh_st_offset = 3
  637. * mod_value = 5
  638. * group_offset = 3 % 5 = 3
  639. */
  640. group_offset = log_coh_st_offset % mod_value;
  641. /* Adjust group_offset if the hashed bit [8] is set. */
  642. if (hash_pa8) {
  643. if (!group_offset)
  644. group_offset = mod_value - 1;
  645. else
  646. group_offset--;
  647. }
  648. /* Add in the group offset to the high bits. */
  649. temp_addr_b += group_offset;
  650. /* Shift the high bits to the proper starting position. */
  651. temp_addr_b <<= 14;
  652. /* Combine the high and low bits together. */
  653. ctx->ret_addr = temp_addr_a | temp_addr_b;
  654. /* Account for hashing here instead of in dehash_address(). */
  655. hash_ctl_64k = FIELD_GET(DF4_HASH_CTL_64K, ctx->map.ctl);
  656. hash_ctl_2M = FIELD_GET(DF4_HASH_CTL_2M, ctx->map.ctl);
  657. hash_ctl_1G = FIELD_GET(DF4_HASH_CTL_1G, ctx->map.ctl);
  658. hashed_bit = !!hash_pa8;
  659. hashed_bit ^= FIELD_GET(BIT_ULL(14), ctx->ret_addr);
  660. hashed_bit ^= FIELD_GET(BIT_ULL(16), ctx->ret_addr) & hash_ctl_64k;
  661. hashed_bit ^= FIELD_GET(BIT_ULL(21), ctx->ret_addr) & hash_ctl_2M;
  662. hashed_bit ^= FIELD_GET(BIT_ULL(30), ctx->ret_addr) & hash_ctl_1G;
  663. ctx->ret_addr |= hashed_bit << 8;
  664. /* Done for 3 and 5 channel. */
  665. if (ctx->map.intlv_mode == DF4_NPS4_3CHAN_HASH ||
  666. ctx->map.intlv_mode == DF4_NPS2_5CHAN_HASH)
  667. return 0;
  668. /* Select the proper 'group' bit to use for Bit 13. */
  669. if (ctx->map.intlv_mode == DF4_NPS1_12CHAN_HASH)
  670. hashed_bit = !!(group & BIT(1));
  671. else
  672. hashed_bit = group & BIT(0);
  673. hashed_bit ^= FIELD_GET(BIT_ULL(18), ctx->ret_addr) & hash_ctl_64k;
  674. hashed_bit ^= FIELD_GET(BIT_ULL(23), ctx->ret_addr) & hash_ctl_2M;
  675. hashed_bit ^= FIELD_GET(BIT_ULL(32), ctx->ret_addr) & hash_ctl_1G;
  676. ctx->ret_addr |= hashed_bit << 13;
  677. /* Done for 6 and 10 channel. */
  678. if (ctx->map.intlv_mode != DF4_NPS1_12CHAN_HASH)
  679. return 0;
  680. hashed_bit = group & BIT(0);
  681. hashed_bit ^= FIELD_GET(BIT_ULL(17), ctx->ret_addr) & hash_ctl_64k;
  682. hashed_bit ^= FIELD_GET(BIT_ULL(22), ctx->ret_addr) & hash_ctl_2M;
  683. hashed_bit ^= FIELD_GET(BIT_ULL(31), ctx->ret_addr) & hash_ctl_1G;
  684. ctx->ret_addr |= hashed_bit << 12;
  685. return 0;
  686. }
  687. static u64 normalize_addr_df4p5_np2(struct addr_ctx *ctx, struct df4p5_denorm_ctx *denorm_ctx,
  688. u64 addr)
  689. {
  690. u64 temp_addr_a = 0, temp_addr_b = 0;
  691. switch (ctx->map.intlv_mode) {
  692. case DF4p5_NPS0_24CHAN_1K_HASH:
  693. case DF4p5_NPS1_12CHAN_1K_HASH:
  694. case DF4p5_NPS2_6CHAN_1K_HASH:
  695. case DF4p5_NPS4_3CHAN_1K_HASH:
  696. case DF4p5_NPS1_10CHAN_1K_HASH:
  697. case DF4p5_NPS2_5CHAN_1K_HASH:
  698. temp_addr_a = FIELD_GET(GENMASK_ULL(11, 10), addr) << 8;
  699. break;
  700. case DF4p5_NPS0_24CHAN_2K_HASH:
  701. case DF4p5_NPS1_12CHAN_2K_HASH:
  702. case DF4p5_NPS2_6CHAN_2K_HASH:
  703. case DF4p5_NPS4_3CHAN_2K_HASH:
  704. case DF4p5_NPS1_10CHAN_2K_HASH:
  705. case DF4p5_NPS2_5CHAN_2K_HASH:
  706. temp_addr_a = FIELD_GET(GENMASK_ULL(11, 9), addr) << 8;
  707. break;
  708. default:
  709. atl_debug_on_bad_intlv_mode(ctx);
  710. return 0;
  711. }
  712. switch (ctx->map.intlv_mode) {
  713. case DF4p5_NPS0_24CHAN_1K_HASH:
  714. temp_addr_b = FIELD_GET(GENMASK_ULL(63, 13), addr) / denorm_ctx->mod_value;
  715. temp_addr_b <<= 10;
  716. break;
  717. case DF4p5_NPS0_24CHAN_2K_HASH:
  718. temp_addr_b = FIELD_GET(GENMASK_ULL(63, 14), addr) / denorm_ctx->mod_value;
  719. temp_addr_b <<= 11;
  720. break;
  721. case DF4p5_NPS1_12CHAN_1K_HASH:
  722. temp_addr_b = FIELD_GET(GENMASK_ULL(63, 12), addr) / denorm_ctx->mod_value;
  723. temp_addr_b <<= 10;
  724. break;
  725. case DF4p5_NPS1_12CHAN_2K_HASH:
  726. temp_addr_b = FIELD_GET(GENMASK_ULL(63, 13), addr) / denorm_ctx->mod_value;
  727. temp_addr_b <<= 11;
  728. break;
  729. case DF4p5_NPS2_6CHAN_1K_HASH:
  730. case DF4p5_NPS1_10CHAN_1K_HASH:
  731. temp_addr_b = FIELD_GET(GENMASK_ULL(63, 12), addr) << 1;
  732. temp_addr_b |= FIELD_GET(BIT_ULL(9), addr);
  733. temp_addr_b /= denorm_ctx->mod_value;
  734. temp_addr_b <<= 10;
  735. break;
  736. case DF4p5_NPS2_6CHAN_2K_HASH:
  737. case DF4p5_NPS1_10CHAN_2K_HASH:
  738. temp_addr_b = FIELD_GET(GENMASK_ULL(63, 12), addr) / denorm_ctx->mod_value;
  739. temp_addr_b <<= 11;
  740. break;
  741. case DF4p5_NPS4_3CHAN_1K_HASH:
  742. case DF4p5_NPS2_5CHAN_1K_HASH:
  743. temp_addr_b = FIELD_GET(GENMASK_ULL(63, 12), addr) << 2;
  744. temp_addr_b |= FIELD_GET(GENMASK_ULL(9, 8), addr);
  745. temp_addr_b /= denorm_ctx->mod_value;
  746. temp_addr_b <<= 10;
  747. break;
  748. case DF4p5_NPS4_3CHAN_2K_HASH:
  749. case DF4p5_NPS2_5CHAN_2K_HASH:
  750. temp_addr_b = FIELD_GET(GENMASK_ULL(63, 12), addr) << 1;
  751. temp_addr_b |= FIELD_GET(BIT_ULL(8), addr);
  752. temp_addr_b /= denorm_ctx->mod_value;
  753. temp_addr_b <<= 11;
  754. break;
  755. default:
  756. atl_debug_on_bad_intlv_mode(ctx);
  757. return 0;
  758. }
  759. return denorm_ctx->base_denorm_addr | temp_addr_a | temp_addr_b;
  760. }
  761. static void recalculate_hashed_bits_df4p5_np2(struct addr_ctx *ctx,
  762. struct df4p5_denorm_ctx *denorm_ctx)
  763. {
  764. bool hash_ctl_64k, hash_ctl_2M, hash_ctl_1G, hash_ctl_1T, hashed_bit;
  765. if (!denorm_ctx->rehash_vector)
  766. return;
  767. hash_ctl_64k = FIELD_GET(DF4_HASH_CTL_64K, ctx->map.ctl);
  768. hash_ctl_2M = FIELD_GET(DF4_HASH_CTL_2M, ctx->map.ctl);
  769. hash_ctl_1G = FIELD_GET(DF4_HASH_CTL_1G, ctx->map.ctl);
  770. hash_ctl_1T = FIELD_GET(DF4p5_HASH_CTL_1T, ctx->map.ctl);
  771. if (denorm_ctx->rehash_vector & BIT_ULL(8)) {
  772. hashed_bit = FIELD_GET(BIT_ULL(8), denorm_ctx->current_spa);
  773. hashed_bit ^= FIELD_GET(BIT_ULL(14), denorm_ctx->current_spa);
  774. hashed_bit ^= FIELD_GET(BIT_ULL(16), denorm_ctx->current_spa) & hash_ctl_64k;
  775. hashed_bit ^= FIELD_GET(BIT_ULL(21), denorm_ctx->current_spa) & hash_ctl_2M;
  776. hashed_bit ^= FIELD_GET(BIT_ULL(30), denorm_ctx->current_spa) & hash_ctl_1G;
  777. hashed_bit ^= FIELD_GET(BIT_ULL(40), denorm_ctx->current_spa) & hash_ctl_1T;
  778. if (FIELD_GET(BIT_ULL(8), denorm_ctx->current_spa) != hashed_bit)
  779. denorm_ctx->current_spa ^= BIT_ULL(8);
  780. }
  781. if (denorm_ctx->rehash_vector & BIT_ULL(9)) {
  782. hashed_bit = FIELD_GET(BIT_ULL(9), denorm_ctx->current_spa);
  783. hashed_bit ^= FIELD_GET(BIT_ULL(17), denorm_ctx->current_spa) & hash_ctl_64k;
  784. hashed_bit ^= FIELD_GET(BIT_ULL(22), denorm_ctx->current_spa) & hash_ctl_2M;
  785. hashed_bit ^= FIELD_GET(BIT_ULL(31), denorm_ctx->current_spa) & hash_ctl_1G;
  786. hashed_bit ^= FIELD_GET(BIT_ULL(41), denorm_ctx->current_spa) & hash_ctl_1T;
  787. if (FIELD_GET(BIT_ULL(9), denorm_ctx->current_spa) != hashed_bit)
  788. denorm_ctx->current_spa ^= BIT_ULL(9);
  789. }
  790. if (denorm_ctx->rehash_vector & BIT_ULL(12)) {
  791. hashed_bit = FIELD_GET(BIT_ULL(12), denorm_ctx->current_spa);
  792. hashed_bit ^= FIELD_GET(BIT_ULL(18), denorm_ctx->current_spa) & hash_ctl_64k;
  793. hashed_bit ^= FIELD_GET(BIT_ULL(23), denorm_ctx->current_spa) & hash_ctl_2M;
  794. hashed_bit ^= FIELD_GET(BIT_ULL(32), denorm_ctx->current_spa) & hash_ctl_1G;
  795. hashed_bit ^= FIELD_GET(BIT_ULL(42), denorm_ctx->current_spa) & hash_ctl_1T;
  796. if (FIELD_GET(BIT_ULL(12), denorm_ctx->current_spa) != hashed_bit)
  797. denorm_ctx->current_spa ^= BIT_ULL(12);
  798. }
  799. if (denorm_ctx->rehash_vector & BIT_ULL(13)) {
  800. hashed_bit = FIELD_GET(BIT_ULL(13), denorm_ctx->current_spa);
  801. hashed_bit ^= FIELD_GET(BIT_ULL(19), denorm_ctx->current_spa) & hash_ctl_64k;
  802. hashed_bit ^= FIELD_GET(BIT_ULL(24), denorm_ctx->current_spa) & hash_ctl_2M;
  803. hashed_bit ^= FIELD_GET(BIT_ULL(33), denorm_ctx->current_spa) & hash_ctl_1G;
  804. hashed_bit ^= FIELD_GET(BIT_ULL(43), denorm_ctx->current_spa) & hash_ctl_1T;
  805. if (FIELD_GET(BIT_ULL(13), denorm_ctx->current_spa) != hashed_bit)
  806. denorm_ctx->current_spa ^= BIT_ULL(13);
  807. }
  808. }
  809. static bool match_logical_coh_st_fabric_id(struct addr_ctx *ctx,
  810. struct df4p5_denorm_ctx *denorm_ctx)
  811. {
  812. /*
  813. * The logical CS fabric ID of the permutation must be calculated from the
  814. * current SPA with the base and with the MMIO hole.
  815. */
  816. u16 id = get_logical_coh_st_fabric_id_for_current_spa(ctx, denorm_ctx);
  817. atl_debug(ctx, "Checking calculated logical coherent station fabric id:\n");
  818. atl_debug(ctx, " calculated fabric id = 0x%x\n", id);
  819. atl_debug(ctx, " expected fabric id = 0x%x\n", denorm_ctx->coh_st_fabric_id);
  820. return denorm_ctx->coh_st_fabric_id == id;
  821. }
  822. static bool match_norm_addr(struct addr_ctx *ctx, struct df4p5_denorm_ctx *denorm_ctx)
  823. {
  824. u64 addr = remove_base_and_hole(ctx, denorm_ctx->current_spa);
  825. /*
  826. * The normalized address must be calculated with the current SPA without
  827. * the base and without the MMIO hole.
  828. */
  829. addr = normalize_addr_df4p5_np2(ctx, denorm_ctx, addr);
  830. atl_debug(ctx, "Checking calculated normalized address:\n");
  831. atl_debug(ctx, " calculated normalized addr = 0x%016llx\n", addr);
  832. atl_debug(ctx, " expected normalized addr = 0x%016llx\n", ctx->ret_addr);
  833. return addr == ctx->ret_addr;
  834. }
  835. static int check_permutations(struct addr_ctx *ctx, struct df4p5_denorm_ctx *denorm_ctx)
  836. {
  837. u64 test_perm, temp_addr, denorm_addr, num_perms;
  838. unsigned int dropped_remainder;
  839. denorm_ctx->div_addr *= denorm_ctx->mod_value;
  840. /*
  841. * The high order bits of num_permutations represent the permutations
  842. * of the dropped remainder. This will be either 0-3 or 0-5 depending
  843. * on the interleave mode. The low order bits represent the
  844. * permutations of other "lost" bits which will be any combination of
  845. * 1, 2, or 3 bits depending on the interleave mode.
  846. */
  847. num_perms = denorm_ctx->mod_value << denorm_ctx->perm_shift;
  848. for (test_perm = 0; test_perm < num_perms; test_perm++) {
  849. denorm_addr = denorm_ctx->base_denorm_addr;
  850. dropped_remainder = test_perm >> denorm_ctx->perm_shift;
  851. temp_addr = denorm_ctx->div_addr + dropped_remainder;
  852. switch (ctx->map.intlv_mode) {
  853. case DF4p5_NPS0_24CHAN_2K_HASH:
  854. denorm_addr |= temp_addr << 14;
  855. break;
  856. case DF4p5_NPS0_24CHAN_1K_HASH:
  857. case DF4p5_NPS1_12CHAN_2K_HASH:
  858. denorm_addr |= temp_addr << 13;
  859. break;
  860. case DF4p5_NPS1_12CHAN_1K_HASH:
  861. case DF4p5_NPS2_6CHAN_2K_HASH:
  862. case DF4p5_NPS1_10CHAN_2K_HASH:
  863. denorm_addr |= temp_addr << 12;
  864. break;
  865. case DF4p5_NPS2_6CHAN_1K_HASH:
  866. case DF4p5_NPS1_10CHAN_1K_HASH:
  867. denorm_addr |= FIELD_GET(BIT_ULL(0), temp_addr) << 9;
  868. denorm_addr |= FIELD_GET(GENMASK_ULL(63, 1), temp_addr) << 12;
  869. break;
  870. case DF4p5_NPS4_3CHAN_1K_HASH:
  871. case DF4p5_NPS2_5CHAN_1K_HASH:
  872. denorm_addr |= FIELD_GET(GENMASK_ULL(1, 0), temp_addr) << 8;
  873. denorm_addr |= FIELD_GET(GENMASK_ULL(63, 2), (temp_addr)) << 12;
  874. break;
  875. case DF4p5_NPS4_3CHAN_2K_HASH:
  876. case DF4p5_NPS2_5CHAN_2K_HASH:
  877. denorm_addr |= FIELD_GET(BIT_ULL(0), temp_addr) << 8;
  878. denorm_addr |= FIELD_GET(GENMASK_ULL(63, 1), temp_addr) << 12;
  879. break;
  880. default:
  881. atl_debug_on_bad_intlv_mode(ctx);
  882. return -EINVAL;
  883. }
  884. switch (ctx->map.intlv_mode) {
  885. case DF4p5_NPS0_24CHAN_1K_HASH:
  886. denorm_addr |= FIELD_GET(BIT_ULL(0), test_perm) << 8;
  887. denorm_addr |= FIELD_GET(BIT_ULL(1), test_perm) << 9;
  888. denorm_addr |= FIELD_GET(BIT_ULL(2), test_perm) << 12;
  889. break;
  890. case DF4p5_NPS0_24CHAN_2K_HASH:
  891. denorm_addr |= FIELD_GET(BIT_ULL(0), test_perm) << 8;
  892. denorm_addr |= FIELD_GET(BIT_ULL(1), test_perm) << 12;
  893. denorm_addr |= FIELD_GET(BIT_ULL(2), test_perm) << 13;
  894. break;
  895. case DF4p5_NPS1_12CHAN_2K_HASH:
  896. denorm_addr |= FIELD_GET(BIT_ULL(0), test_perm) << 8;
  897. denorm_addr |= FIELD_GET(BIT_ULL(1), test_perm) << 12;
  898. break;
  899. case DF4p5_NPS1_12CHAN_1K_HASH:
  900. case DF4p5_NPS4_3CHAN_1K_HASH:
  901. case DF4p5_NPS2_5CHAN_1K_HASH:
  902. denorm_addr |= FIELD_GET(BIT_ULL(0), test_perm) << 8;
  903. denorm_addr |= FIELD_GET(BIT_ULL(1), test_perm) << 9;
  904. break;
  905. case DF4p5_NPS2_6CHAN_1K_HASH:
  906. case DF4p5_NPS2_6CHAN_2K_HASH:
  907. case DF4p5_NPS4_3CHAN_2K_HASH:
  908. case DF4p5_NPS1_10CHAN_1K_HASH:
  909. case DF4p5_NPS1_10CHAN_2K_HASH:
  910. case DF4p5_NPS2_5CHAN_2K_HASH:
  911. denorm_addr |= FIELD_GET(BIT_ULL(0), test_perm) << 8;
  912. break;
  913. default:
  914. atl_debug_on_bad_intlv_mode(ctx);
  915. return -EINVAL;
  916. }
  917. denorm_ctx->current_spa = add_base_and_hole(ctx, denorm_addr);
  918. recalculate_hashed_bits_df4p5_np2(ctx, denorm_ctx);
  919. atl_debug(ctx, "Checking potential system physical address 0x%016llx\n",
  920. denorm_ctx->current_spa);
  921. if (!match_logical_coh_st_fabric_id(ctx, denorm_ctx))
  922. continue;
  923. if (!match_norm_addr(ctx, denorm_ctx))
  924. continue;
  925. if (denorm_ctx->resolved_spa == INVALID_SPA ||
  926. denorm_ctx->current_spa > denorm_ctx->resolved_spa)
  927. denorm_ctx->resolved_spa = denorm_ctx->current_spa;
  928. }
  929. if (denorm_ctx->resolved_spa == INVALID_SPA) {
  930. atl_debug(ctx, "Failed to find valid SPA for normalized address 0x%016llx\n",
  931. ctx->ret_addr);
  932. return -EINVAL;
  933. }
  934. /* Return the resolved SPA without the base, without the MMIO hole */
  935. ctx->ret_addr = remove_base_and_hole(ctx, denorm_ctx->resolved_spa);
  936. return 0;
  937. }
  938. static int init_df4p5_denorm_ctx(struct addr_ctx *ctx, struct df4p5_denorm_ctx *denorm_ctx)
  939. {
  940. denorm_ctx->current_spa = INVALID_SPA;
  941. denorm_ctx->resolved_spa = INVALID_SPA;
  942. switch (ctx->map.intlv_mode) {
  943. case DF4p5_NPS0_24CHAN_1K_HASH:
  944. denorm_ctx->perm_shift = 3;
  945. denorm_ctx->rehash_vector = BIT(8) | BIT(9) | BIT(12);
  946. break;
  947. case DF4p5_NPS0_24CHAN_2K_HASH:
  948. denorm_ctx->perm_shift = 3;
  949. denorm_ctx->rehash_vector = BIT(8) | BIT(12) | BIT(13);
  950. break;
  951. case DF4p5_NPS1_12CHAN_1K_HASH:
  952. denorm_ctx->perm_shift = 2;
  953. denorm_ctx->rehash_vector = BIT(8);
  954. break;
  955. case DF4p5_NPS1_12CHAN_2K_HASH:
  956. denorm_ctx->perm_shift = 2;
  957. denorm_ctx->rehash_vector = BIT(8) | BIT(12);
  958. break;
  959. case DF4p5_NPS2_6CHAN_1K_HASH:
  960. case DF4p5_NPS2_6CHAN_2K_HASH:
  961. case DF4p5_NPS1_10CHAN_1K_HASH:
  962. case DF4p5_NPS1_10CHAN_2K_HASH:
  963. denorm_ctx->perm_shift = 1;
  964. denorm_ctx->rehash_vector = BIT(8);
  965. break;
  966. case DF4p5_NPS4_3CHAN_1K_HASH:
  967. case DF4p5_NPS2_5CHAN_1K_HASH:
  968. denorm_ctx->perm_shift = 2;
  969. denorm_ctx->rehash_vector = 0;
  970. break;
  971. case DF4p5_NPS4_3CHAN_2K_HASH:
  972. case DF4p5_NPS2_5CHAN_2K_HASH:
  973. denorm_ctx->perm_shift = 1;
  974. denorm_ctx->rehash_vector = 0;
  975. break;
  976. default:
  977. atl_debug_on_bad_intlv_mode(ctx);
  978. return -EINVAL;
  979. }
  980. denorm_ctx->base_denorm_addr = FIELD_GET(GENMASK_ULL(7, 0), ctx->ret_addr);
  981. switch (ctx->map.intlv_mode) {
  982. case DF4p5_NPS0_24CHAN_1K_HASH:
  983. case DF4p5_NPS1_12CHAN_1K_HASH:
  984. case DF4p5_NPS2_6CHAN_1K_HASH:
  985. case DF4p5_NPS4_3CHAN_1K_HASH:
  986. case DF4p5_NPS1_10CHAN_1K_HASH:
  987. case DF4p5_NPS2_5CHAN_1K_HASH:
  988. denorm_ctx->base_denorm_addr |= FIELD_GET(GENMASK_ULL(9, 8), ctx->ret_addr) << 10;
  989. denorm_ctx->div_addr = FIELD_GET(GENMASK_ULL(63, 10), ctx->ret_addr);
  990. break;
  991. case DF4p5_NPS0_24CHAN_2K_HASH:
  992. case DF4p5_NPS1_12CHAN_2K_HASH:
  993. case DF4p5_NPS2_6CHAN_2K_HASH:
  994. case DF4p5_NPS4_3CHAN_2K_HASH:
  995. case DF4p5_NPS1_10CHAN_2K_HASH:
  996. case DF4p5_NPS2_5CHAN_2K_HASH:
  997. denorm_ctx->base_denorm_addr |= FIELD_GET(GENMASK_ULL(10, 8), ctx->ret_addr) << 9;
  998. denorm_ctx->div_addr = FIELD_GET(GENMASK_ULL(63, 11), ctx->ret_addr);
  999. break;
  1000. default:
  1001. atl_debug_on_bad_intlv_mode(ctx);
  1002. return -EINVAL;
  1003. }
  1004. if (ctx->map.num_intlv_chan % 3 == 0)
  1005. denorm_ctx->mod_value = 3;
  1006. else
  1007. denorm_ctx->mod_value = 5;
  1008. denorm_ctx->coh_st_fabric_id = get_logical_coh_st_fabric_id(ctx) - get_dst_fabric_id(ctx);
  1009. atl_debug(ctx, "Initialized df4p5_denorm_ctx:");
  1010. atl_debug(ctx, " mod_value = %d", denorm_ctx->mod_value);
  1011. atl_debug(ctx, " perm_shift = %d", denorm_ctx->perm_shift);
  1012. atl_debug(ctx, " rehash_vector = 0x%x", denorm_ctx->rehash_vector);
  1013. atl_debug(ctx, " base_denorm_addr = 0x%016llx", denorm_ctx->base_denorm_addr);
  1014. atl_debug(ctx, " div_addr = 0x%016llx", denorm_ctx->div_addr);
  1015. atl_debug(ctx, " coh_st_fabric_id = 0x%x", denorm_ctx->coh_st_fabric_id);
  1016. return 0;
  1017. }
  1018. /*
  1019. * For DF 4.5, parts of the physical address can be directly pulled from the
  1020. * normalized address. The exact bits will differ between interleave modes, but
  1021. * using NPS0_24CHAN_1K_HASH as an example, the normalized address consists of
  1022. * bits [63:13] (divided by 3), bits [11:10], and bits [7:0] of the system
  1023. * physical address.
  1024. *
  1025. * In this case, there is no way to reconstruct the missing bits (bits 8, 9,
  1026. * and 12) from the normalized address. Additionally, when bits [63:13] are
  1027. * divided by 3, the remainder is dropped. Determine the proper combination of
  1028. * "lost" bits and dropped remainder by iterating through each possible
  1029. * permutation of these bits and then normalizing the generated system physical
  1030. * addresses. If the normalized address matches the address we are trying to
  1031. * translate, then we have found the correct permutation of bits.
  1032. */
  1033. static int denorm_addr_df4p5_np2(struct addr_ctx *ctx)
  1034. {
  1035. struct df4p5_denorm_ctx denorm_ctx;
  1036. int ret = 0;
  1037. memset(&denorm_ctx, 0, sizeof(denorm_ctx));
  1038. atl_debug(ctx, "Denormalizing DF 4.5 normalized address 0x%016llx", ctx->ret_addr);
  1039. ret = init_df4p5_denorm_ctx(ctx, &denorm_ctx);
  1040. if (ret)
  1041. return ret;
  1042. return check_permutations(ctx, &denorm_ctx);
  1043. }
  1044. int denormalize_address(struct addr_ctx *ctx)
  1045. {
  1046. switch (ctx->map.intlv_mode) {
  1047. case NONE:
  1048. return 0;
  1049. case DF4_NPS4_3CHAN_HASH:
  1050. case DF4_NPS2_6CHAN_HASH:
  1051. case DF4_NPS1_12CHAN_HASH:
  1052. case DF4_NPS2_5CHAN_HASH:
  1053. case DF4_NPS1_10CHAN_HASH:
  1054. return denorm_addr_df4_np2(ctx);
  1055. case DF4p5_NPS0_24CHAN_1K_HASH:
  1056. case DF4p5_NPS4_3CHAN_1K_HASH:
  1057. case DF4p5_NPS2_6CHAN_1K_HASH:
  1058. case DF4p5_NPS1_12CHAN_1K_HASH:
  1059. case DF4p5_NPS2_5CHAN_1K_HASH:
  1060. case DF4p5_NPS1_10CHAN_1K_HASH:
  1061. case DF4p5_NPS4_3CHAN_2K_HASH:
  1062. case DF4p5_NPS2_6CHAN_2K_HASH:
  1063. case DF4p5_NPS1_12CHAN_2K_HASH:
  1064. case DF4p5_NPS0_24CHAN_2K_HASH:
  1065. case DF4p5_NPS2_5CHAN_2K_HASH:
  1066. case DF4p5_NPS1_10CHAN_2K_HASH:
  1067. return denorm_addr_df4p5_np2(ctx);
  1068. case DF3_6CHAN:
  1069. return denorm_addr_df3_6chan(ctx);
  1070. default:
  1071. return denorm_addr_common(ctx);
  1072. }
  1073. }