map.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * AMD Address Translation Library
  4. *
  5. * map.c : Functions to read and decode DRAM address maps
  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. static int df2_get_intlv_mode(struct addr_ctx *ctx)
  14. {
  15. ctx->map.intlv_mode = FIELD_GET(DF2_INTLV_NUM_CHAN, ctx->map.base);
  16. if (ctx->map.intlv_mode == 8)
  17. ctx->map.intlv_mode = DF2_2CHAN_HASH;
  18. if (ctx->map.intlv_mode != NONE &&
  19. ctx->map.intlv_mode != NOHASH_2CHAN &&
  20. ctx->map.intlv_mode != DF2_2CHAN_HASH)
  21. return -EINVAL;
  22. return 0;
  23. }
  24. static int df3_get_intlv_mode(struct addr_ctx *ctx)
  25. {
  26. ctx->map.intlv_mode = FIELD_GET(DF3_INTLV_NUM_CHAN, ctx->map.base);
  27. return 0;
  28. }
  29. static int df3p5_get_intlv_mode(struct addr_ctx *ctx)
  30. {
  31. ctx->map.intlv_mode = FIELD_GET(DF3p5_INTLV_NUM_CHAN, ctx->map.base);
  32. if (ctx->map.intlv_mode == DF3_6CHAN)
  33. return -EINVAL;
  34. return 0;
  35. }
  36. static int df4_get_intlv_mode(struct addr_ctx *ctx)
  37. {
  38. ctx->map.intlv_mode = FIELD_GET(DF4_INTLV_NUM_CHAN, ctx->map.intlv);
  39. if (ctx->map.intlv_mode == DF3_COD4_2CHAN_HASH ||
  40. ctx->map.intlv_mode == DF3_COD2_4CHAN_HASH ||
  41. ctx->map.intlv_mode == DF3_COD1_8CHAN_HASH ||
  42. ctx->map.intlv_mode == DF3_6CHAN)
  43. return -EINVAL;
  44. return 0;
  45. }
  46. static int df4p5_get_intlv_mode(struct addr_ctx *ctx)
  47. {
  48. ctx->map.intlv_mode = FIELD_GET(DF4p5_INTLV_NUM_CHAN, ctx->map.intlv);
  49. if (ctx->map.intlv_mode <= NOHASH_32CHAN)
  50. return 0;
  51. if (ctx->map.intlv_mode >= MI3_HASH_8CHAN &&
  52. ctx->map.intlv_mode <= MI3_HASH_32CHAN)
  53. return 0;
  54. /*
  55. * Modes matching the ranges above are returned as-is.
  56. *
  57. * All other modes are "fixed up" by adding 20h to make a unique value.
  58. */
  59. ctx->map.intlv_mode += 0x20;
  60. return 0;
  61. }
  62. static int get_intlv_mode(struct addr_ctx *ctx)
  63. {
  64. int ret;
  65. switch (df_cfg.rev) {
  66. case DF2:
  67. ret = df2_get_intlv_mode(ctx);
  68. break;
  69. case DF3:
  70. ret = df3_get_intlv_mode(ctx);
  71. break;
  72. case DF3p5:
  73. ret = df3p5_get_intlv_mode(ctx);
  74. break;
  75. case DF4:
  76. ret = df4_get_intlv_mode(ctx);
  77. break;
  78. case DF4p5:
  79. ret = df4p5_get_intlv_mode(ctx);
  80. break;
  81. default:
  82. ret = -EINVAL;
  83. }
  84. if (ret)
  85. atl_debug_on_bad_df_rev();
  86. return ret;
  87. }
  88. static u64 get_hi_addr_offset(u32 reg_dram_offset)
  89. {
  90. u8 shift = DF_DRAM_BASE_LIMIT_LSB;
  91. u64 hi_addr_offset;
  92. switch (df_cfg.rev) {
  93. case DF2:
  94. hi_addr_offset = FIELD_GET(DF2_HI_ADDR_OFFSET, reg_dram_offset);
  95. break;
  96. case DF3:
  97. case DF3p5:
  98. hi_addr_offset = FIELD_GET(DF3_HI_ADDR_OFFSET, reg_dram_offset);
  99. break;
  100. case DF4:
  101. case DF4p5:
  102. hi_addr_offset = FIELD_GET(DF4_HI_ADDR_OFFSET, reg_dram_offset);
  103. break;
  104. default:
  105. hi_addr_offset = 0;
  106. atl_debug_on_bad_df_rev();
  107. }
  108. if (df_cfg.rev == DF4p5 && df_cfg.flags.heterogeneous)
  109. shift = MI300_DRAM_LIMIT_LSB;
  110. return hi_addr_offset << shift;
  111. }
  112. /*
  113. * Returns: 0 if offset is disabled.
  114. * 1 if offset is enabled.
  115. * -EINVAL on error.
  116. */
  117. static int get_dram_offset(struct addr_ctx *ctx, u64 *norm_offset)
  118. {
  119. u32 reg_dram_offset;
  120. u8 map_num;
  121. /* Should not be called for map 0. */
  122. if (!ctx->map.num) {
  123. atl_debug(ctx, "Trying to find DRAM offset for map 0");
  124. return -EINVAL;
  125. }
  126. /*
  127. * DramOffset registers don't exist for map 0, so the base register
  128. * actually refers to map 1.
  129. * Adjust the map_num for the register offsets.
  130. */
  131. map_num = ctx->map.num - 1;
  132. if (df_cfg.rev >= DF4) {
  133. /* Read D18F7x140 (DramOffset) */
  134. if (df_indirect_read_instance(ctx->node_id, 7, 0x140 + (4 * map_num),
  135. ctx->inst_id, &reg_dram_offset))
  136. return -EINVAL;
  137. } else {
  138. /* Read D18F0x1B4 (DramOffset) */
  139. if (df_indirect_read_instance(ctx->node_id, 0, 0x1B4 + (4 * map_num),
  140. ctx->inst_id, &reg_dram_offset))
  141. return -EINVAL;
  142. }
  143. if (!FIELD_GET(DF_HI_ADDR_OFFSET_EN, reg_dram_offset))
  144. return 0;
  145. *norm_offset = get_hi_addr_offset(reg_dram_offset);
  146. return 1;
  147. }
  148. static int df3_6ch_get_dram_addr_map(struct addr_ctx *ctx)
  149. {
  150. u16 dst_fabric_id = FIELD_GET(DF3_DST_FABRIC_ID, ctx->map.limit);
  151. u8 i, j, shift = 4, mask = 0xF;
  152. u32 reg, offset = 0x60;
  153. u16 dst_node_id;
  154. /* Get Socket 1 register. */
  155. if (dst_fabric_id & df_cfg.socket_id_mask)
  156. offset = 0x68;
  157. /* Read D18F0x06{0,8} (DF::Skt0CsTargetRemap0)/(DF::Skt0CsTargetRemap1) */
  158. if (df_indirect_read_broadcast(ctx->node_id, 0, offset, &reg))
  159. return -EINVAL;
  160. /* Save 8 remap entries. */
  161. for (i = 0, j = 0; i < 8; i++, j++)
  162. ctx->map.remap_array[i] = (reg >> (j * shift)) & mask;
  163. dst_node_id = dst_fabric_id & df_cfg.node_id_mask;
  164. dst_node_id >>= df_cfg.node_id_shift;
  165. /* Read D18F2x090 (DF::Np2ChannelConfig) */
  166. if (df_indirect_read_broadcast(dst_node_id, 2, 0x90, &reg))
  167. return -EINVAL;
  168. ctx->map.np2_bits = FIELD_GET(DF_LOG2_ADDR_64K_SPACE0, reg);
  169. return 0;
  170. }
  171. static int df2_get_dram_addr_map(struct addr_ctx *ctx)
  172. {
  173. /* Read D18F0x110 (DramBaseAddress). */
  174. if (df_indirect_read_instance(ctx->node_id, 0, 0x110 + (8 * ctx->map.num),
  175. ctx->inst_id, &ctx->map.base))
  176. return -EINVAL;
  177. /* Read D18F0x114 (DramLimitAddress). */
  178. if (df_indirect_read_instance(ctx->node_id, 0, 0x114 + (8 * ctx->map.num),
  179. ctx->inst_id, &ctx->map.limit))
  180. return -EINVAL;
  181. return 0;
  182. }
  183. static int df3_get_dram_addr_map(struct addr_ctx *ctx)
  184. {
  185. if (df2_get_dram_addr_map(ctx))
  186. return -EINVAL;
  187. /* Read D18F0x3F8 (DfGlobalCtl). */
  188. if (df_indirect_read_instance(ctx->node_id, 0, 0x3F8,
  189. ctx->inst_id, &ctx->map.ctl))
  190. return -EINVAL;
  191. return 0;
  192. }
  193. static int df4_get_dram_addr_map(struct addr_ctx *ctx)
  194. {
  195. u8 remap_sel, i, j, shift = 4, mask = 0xF;
  196. u32 remap_reg;
  197. /* Read D18F7xE00 (DramBaseAddress). */
  198. if (df_indirect_read_instance(ctx->node_id, 7, 0xE00 + (16 * ctx->map.num),
  199. ctx->inst_id, &ctx->map.base))
  200. return -EINVAL;
  201. /* Read D18F7xE04 (DramLimitAddress). */
  202. if (df_indirect_read_instance(ctx->node_id, 7, 0xE04 + (16 * ctx->map.num),
  203. ctx->inst_id, &ctx->map.limit))
  204. return -EINVAL;
  205. /* Read D18F7xE08 (DramAddressCtl). */
  206. if (df_indirect_read_instance(ctx->node_id, 7, 0xE08 + (16 * ctx->map.num),
  207. ctx->inst_id, &ctx->map.ctl))
  208. return -EINVAL;
  209. /* Read D18F7xE0C (DramAddressIntlv). */
  210. if (df_indirect_read_instance(ctx->node_id, 7, 0xE0C + (16 * ctx->map.num),
  211. ctx->inst_id, &ctx->map.intlv))
  212. return -EINVAL;
  213. /* Check if Remap Enable bit is valid. */
  214. if (!FIELD_GET(DF4_REMAP_EN, ctx->map.ctl))
  215. return 0;
  216. /* Fill with bogus values, because '0' is a valid value. */
  217. memset(&ctx->map.remap_array, 0xFF, sizeof(ctx->map.remap_array));
  218. /* Get Remap registers. */
  219. remap_sel = FIELD_GET(DF4_REMAP_SEL, ctx->map.ctl);
  220. /* Read D18F7x180 (CsTargetRemap0A). */
  221. if (df_indirect_read_instance(ctx->node_id, 7, 0x180 + (8 * remap_sel),
  222. ctx->inst_id, &remap_reg))
  223. return -EINVAL;
  224. /* Save first 8 remap entries. */
  225. for (i = 0, j = 0; i < 8; i++, j++)
  226. ctx->map.remap_array[i] = (remap_reg >> (j * shift)) & mask;
  227. /* Read D18F7x184 (CsTargetRemap0B). */
  228. if (df_indirect_read_instance(ctx->node_id, 7, 0x184 + (8 * remap_sel),
  229. ctx->inst_id, &remap_reg))
  230. return -EINVAL;
  231. /* Save next 8 remap entries. */
  232. for (i = 8, j = 0; i < 16; i++, j++)
  233. ctx->map.remap_array[i] = (remap_reg >> (j * shift)) & mask;
  234. return 0;
  235. }
  236. static int df4p5_get_dram_addr_map(struct addr_ctx *ctx)
  237. {
  238. u8 remap_sel, i, j, shift = 5, mask = 0x1F;
  239. u32 remap_reg;
  240. /* Read D18F7x200 (DramBaseAddress). */
  241. if (df_indirect_read_instance(ctx->node_id, 7, 0x200 + (16 * ctx->map.num),
  242. ctx->inst_id, &ctx->map.base))
  243. return -EINVAL;
  244. /* Read D18F7x204 (DramLimitAddress). */
  245. if (df_indirect_read_instance(ctx->node_id, 7, 0x204 + (16 * ctx->map.num),
  246. ctx->inst_id, &ctx->map.limit))
  247. return -EINVAL;
  248. /* Read D18F7x208 (DramAddressCtl). */
  249. if (df_indirect_read_instance(ctx->node_id, 7, 0x208 + (16 * ctx->map.num),
  250. ctx->inst_id, &ctx->map.ctl))
  251. return -EINVAL;
  252. /* Read D18F7x20C (DramAddressIntlv). */
  253. if (df_indirect_read_instance(ctx->node_id, 7, 0x20C + (16 * ctx->map.num),
  254. ctx->inst_id, &ctx->map.intlv))
  255. return -EINVAL;
  256. /* Check if Remap Enable bit is valid. */
  257. if (!FIELD_GET(DF4_REMAP_EN, ctx->map.ctl))
  258. return 0;
  259. /* Fill with bogus values, because '0' is a valid value. */
  260. memset(&ctx->map.remap_array, 0xFF, sizeof(ctx->map.remap_array));
  261. /* Get Remap registers. */
  262. remap_sel = FIELD_GET(DF4p5_REMAP_SEL, ctx->map.ctl);
  263. /* Read D18F7x180 (CsTargetRemap0A). */
  264. if (df_indirect_read_instance(ctx->node_id, 7, 0x180 + (24 * remap_sel),
  265. ctx->inst_id, &remap_reg))
  266. return -EINVAL;
  267. /* Save first 6 remap entries. */
  268. for (i = 0, j = 0; i < 6; i++, j++)
  269. ctx->map.remap_array[i] = (remap_reg >> (j * shift)) & mask;
  270. /* Read D18F7x184 (CsTargetRemap0B). */
  271. if (df_indirect_read_instance(ctx->node_id, 7, 0x184 + (24 * remap_sel),
  272. ctx->inst_id, &remap_reg))
  273. return -EINVAL;
  274. /* Save next 6 remap entries. */
  275. for (i = 6, j = 0; i < 12; i++, j++)
  276. ctx->map.remap_array[i] = (remap_reg >> (j * shift)) & mask;
  277. /* Read D18F7x188 (CsTargetRemap0C). */
  278. if (df_indirect_read_instance(ctx->node_id, 7, 0x188 + (24 * remap_sel),
  279. ctx->inst_id, &remap_reg))
  280. return -EINVAL;
  281. /* Save next 6 remap entries. */
  282. for (i = 12, j = 0; i < 18; i++, j++)
  283. ctx->map.remap_array[i] = (remap_reg >> (j * shift)) & mask;
  284. return 0;
  285. }
  286. static int get_dram_addr_map(struct addr_ctx *ctx)
  287. {
  288. switch (df_cfg.rev) {
  289. case DF2: return df2_get_dram_addr_map(ctx);
  290. case DF3:
  291. case DF3p5: return df3_get_dram_addr_map(ctx);
  292. case DF4: return df4_get_dram_addr_map(ctx);
  293. case DF4p5: return df4p5_get_dram_addr_map(ctx);
  294. default:
  295. atl_debug_on_bad_df_rev();
  296. return -EINVAL;
  297. }
  298. }
  299. static int get_coh_st_fabric_id(struct addr_ctx *ctx)
  300. {
  301. u32 reg;
  302. /*
  303. * On MI300 systems, the Coherent Station Fabric ID is derived
  304. * later. And it does not depend on the register value.
  305. */
  306. if (df_cfg.rev == DF4p5 && df_cfg.flags.heterogeneous)
  307. return 0;
  308. /* Read D18F0x50 (FabricBlockInstanceInformation3). */
  309. if (df_indirect_read_instance(ctx->node_id, 0, 0x50, ctx->inst_id, &reg))
  310. return -EINVAL;
  311. if (df_cfg.rev < DF4p5)
  312. ctx->coh_st_fabric_id = FIELD_GET(DF2_COH_ST_FABRIC_ID, reg);
  313. else
  314. ctx->coh_st_fabric_id = FIELD_GET(DF4p5_COH_ST_FABRIC_ID, reg);
  315. return 0;
  316. }
  317. static int find_normalized_offset(struct addr_ctx *ctx, u64 *norm_offset)
  318. {
  319. u64 last_offset = 0;
  320. int ret;
  321. for (ctx->map.num = 1; ctx->map.num < df_cfg.num_coh_st_maps; ctx->map.num++) {
  322. ret = get_dram_offset(ctx, norm_offset);
  323. if (ret < 0)
  324. return ret;
  325. /* Continue search if this map's offset is not enabled. */
  326. if (!ret)
  327. continue;
  328. /* Enabled offsets should never be 0. */
  329. if (*norm_offset == 0) {
  330. atl_debug(ctx, "Enabled map %u offset is 0", ctx->map.num);
  331. return -EINVAL;
  332. }
  333. /* Offsets should always increase from one map to the next. */
  334. if (*norm_offset <= last_offset) {
  335. atl_debug(ctx, "Map %u offset (0x%016llx) <= previous (0x%016llx)",
  336. ctx->map.num, *norm_offset, last_offset);
  337. return -EINVAL;
  338. }
  339. /* Match if this map's offset is less than the current calculated address. */
  340. if (ctx->ret_addr >= *norm_offset)
  341. break;
  342. last_offset = *norm_offset;
  343. }
  344. /*
  345. * Finished search without finding a match.
  346. * Reset to map 0 and no offset.
  347. */
  348. if (ctx->map.num >= df_cfg.num_coh_st_maps) {
  349. ctx->map.num = 0;
  350. *norm_offset = 0;
  351. }
  352. return 0;
  353. }
  354. static bool valid_map(struct addr_ctx *ctx)
  355. {
  356. if (df_cfg.rev >= DF4)
  357. return FIELD_GET(DF_ADDR_RANGE_VAL, ctx->map.ctl);
  358. else
  359. return FIELD_GET(DF_ADDR_RANGE_VAL, ctx->map.base);
  360. }
  361. static int get_address_map_common(struct addr_ctx *ctx)
  362. {
  363. u64 norm_offset = 0;
  364. if (get_coh_st_fabric_id(ctx))
  365. return -EINVAL;
  366. if (find_normalized_offset(ctx, &norm_offset))
  367. return -EINVAL;
  368. if (get_dram_addr_map(ctx))
  369. return -EINVAL;
  370. if (!valid_map(ctx))
  371. return -EINVAL;
  372. ctx->ret_addr -= norm_offset;
  373. return 0;
  374. }
  375. static u8 get_num_intlv_chan(struct addr_ctx *ctx)
  376. {
  377. switch (ctx->map.intlv_mode) {
  378. case NONE:
  379. return 1;
  380. case NOHASH_2CHAN:
  381. case DF2_2CHAN_HASH:
  382. case DF3_COD4_2CHAN_HASH:
  383. case DF4_NPS4_2CHAN_HASH:
  384. case DF4p5_NPS4_2CHAN_1K_HASH:
  385. case DF4p5_NPS4_2CHAN_2K_HASH:
  386. return 2;
  387. case DF4_NPS4_3CHAN_HASH:
  388. case DF4p5_NPS4_3CHAN_1K_HASH:
  389. case DF4p5_NPS4_3CHAN_2K_HASH:
  390. return 3;
  391. case NOHASH_4CHAN:
  392. case DF3_COD2_4CHAN_HASH:
  393. case DF4_NPS2_4CHAN_HASH:
  394. case DF4p5_NPS2_4CHAN_1K_HASH:
  395. case DF4p5_NPS2_4CHAN_2K_HASH:
  396. return 4;
  397. case DF4_NPS2_5CHAN_HASH:
  398. case DF4p5_NPS2_5CHAN_1K_HASH:
  399. case DF4p5_NPS2_5CHAN_2K_HASH:
  400. return 5;
  401. case DF3_6CHAN:
  402. case DF4_NPS2_6CHAN_HASH:
  403. case DF4p5_NPS2_6CHAN_1K_HASH:
  404. case DF4p5_NPS2_6CHAN_2K_HASH:
  405. return 6;
  406. case NOHASH_8CHAN:
  407. case DF3_COD1_8CHAN_HASH:
  408. case DF4_NPS1_8CHAN_HASH:
  409. case MI3_HASH_8CHAN:
  410. case DF4p5_NPS1_8CHAN_1K_HASH:
  411. case DF4p5_NPS1_8CHAN_2K_HASH:
  412. return 8;
  413. case DF4_NPS1_10CHAN_HASH:
  414. case DF4p5_NPS1_10CHAN_1K_HASH:
  415. case DF4p5_NPS1_10CHAN_2K_HASH:
  416. return 10;
  417. case DF4_NPS1_12CHAN_HASH:
  418. case DF4p5_NPS1_12CHAN_1K_HASH:
  419. case DF4p5_NPS1_12CHAN_2K_HASH:
  420. return 12;
  421. case NOHASH_16CHAN:
  422. case MI3_HASH_16CHAN:
  423. case DF4p5_NPS1_16CHAN_1K_HASH:
  424. case DF4p5_NPS1_16CHAN_2K_HASH:
  425. return 16;
  426. case DF4p5_NPS0_24CHAN_1K_HASH:
  427. case DF4p5_NPS0_24CHAN_2K_HASH:
  428. return 24;
  429. case NOHASH_32CHAN:
  430. case MI3_HASH_32CHAN:
  431. return 32;
  432. default:
  433. atl_debug_on_bad_intlv_mode(ctx);
  434. return 0;
  435. }
  436. }
  437. static void calculate_intlv_bits(struct addr_ctx *ctx)
  438. {
  439. ctx->map.num_intlv_chan = get_num_intlv_chan(ctx);
  440. ctx->map.total_intlv_chan = ctx->map.num_intlv_chan;
  441. ctx->map.total_intlv_chan *= ctx->map.num_intlv_dies;
  442. ctx->map.total_intlv_chan *= ctx->map.num_intlv_sockets;
  443. /*
  444. * Get the number of bits needed to cover this many channels.
  445. * order_base_2() rounds up automatically.
  446. */
  447. ctx->map.total_intlv_bits = order_base_2(ctx->map.total_intlv_chan);
  448. }
  449. static u8 get_intlv_bit_pos(struct addr_ctx *ctx)
  450. {
  451. u8 addr_sel = 0;
  452. switch (df_cfg.rev) {
  453. case DF2:
  454. addr_sel = FIELD_GET(DF2_INTLV_ADDR_SEL, ctx->map.base);
  455. break;
  456. case DF3:
  457. case DF3p5:
  458. addr_sel = FIELD_GET(DF3_INTLV_ADDR_SEL, ctx->map.base);
  459. break;
  460. case DF4:
  461. case DF4p5:
  462. addr_sel = FIELD_GET(DF4_INTLV_ADDR_SEL, ctx->map.intlv);
  463. break;
  464. default:
  465. atl_debug_on_bad_df_rev();
  466. break;
  467. }
  468. /* Add '8' to get the 'interleave bit position'. */
  469. return addr_sel + 8;
  470. }
  471. static u8 get_num_intlv_dies(struct addr_ctx *ctx)
  472. {
  473. u8 dies = 0;
  474. switch (df_cfg.rev) {
  475. case DF2:
  476. dies = FIELD_GET(DF2_INTLV_NUM_DIES, ctx->map.limit);
  477. break;
  478. case DF3:
  479. dies = FIELD_GET(DF3_INTLV_NUM_DIES, ctx->map.base);
  480. break;
  481. case DF3p5:
  482. dies = FIELD_GET(DF3p5_INTLV_NUM_DIES, ctx->map.base);
  483. break;
  484. case DF4:
  485. case DF4p5:
  486. dies = FIELD_GET(DF4_INTLV_NUM_DIES, ctx->map.intlv);
  487. break;
  488. default:
  489. atl_debug_on_bad_df_rev();
  490. break;
  491. }
  492. /* Register value is log2, e.g. 0 -> 1 die, 1 -> 2 dies, etc. */
  493. return 1 << dies;
  494. }
  495. static u8 get_num_intlv_sockets(struct addr_ctx *ctx)
  496. {
  497. u8 sockets = 0;
  498. switch (df_cfg.rev) {
  499. case DF2:
  500. sockets = FIELD_GET(DF2_INTLV_NUM_SOCKETS, ctx->map.limit);
  501. break;
  502. case DF3:
  503. case DF3p5:
  504. sockets = FIELD_GET(DF2_INTLV_NUM_SOCKETS, ctx->map.base);
  505. break;
  506. case DF4:
  507. case DF4p5:
  508. sockets = FIELD_GET(DF4_INTLV_NUM_SOCKETS, ctx->map.intlv);
  509. break;
  510. default:
  511. atl_debug_on_bad_df_rev();
  512. break;
  513. }
  514. /* Register value is log2, e.g. 0 -> 1 sockets, 1 -> 2 sockets, etc. */
  515. return 1 << sockets;
  516. }
  517. static int get_global_map_data(struct addr_ctx *ctx)
  518. {
  519. if (get_intlv_mode(ctx))
  520. return -EINVAL;
  521. if (ctx->map.intlv_mode == DF3_6CHAN &&
  522. df3_6ch_get_dram_addr_map(ctx))
  523. return -EINVAL;
  524. ctx->map.intlv_bit_pos = get_intlv_bit_pos(ctx);
  525. ctx->map.num_intlv_dies = get_num_intlv_dies(ctx);
  526. ctx->map.num_intlv_sockets = get_num_intlv_sockets(ctx);
  527. calculate_intlv_bits(ctx);
  528. return 0;
  529. }
  530. /*
  531. * Verify the interleave bits are correct in the different interleaving
  532. * settings.
  533. *
  534. * If @num_intlv_dies and/or @num_intlv_sockets are 1, it means the
  535. * respective interleaving is disabled.
  536. */
  537. static inline bool map_bits_valid(struct addr_ctx *ctx, u8 bit1, u8 bit2,
  538. u8 num_intlv_dies, u8 num_intlv_sockets)
  539. {
  540. if (!(ctx->map.intlv_bit_pos == bit1 || ctx->map.intlv_bit_pos == bit2)) {
  541. pr_debug("Invalid interleave bit: %u", ctx->map.intlv_bit_pos);
  542. return false;
  543. }
  544. if (ctx->map.num_intlv_dies > num_intlv_dies) {
  545. pr_debug("Invalid number of interleave dies: %u", ctx->map.num_intlv_dies);
  546. return false;
  547. }
  548. if (ctx->map.num_intlv_sockets > num_intlv_sockets) {
  549. pr_debug("Invalid number of interleave sockets: %u", ctx->map.num_intlv_sockets);
  550. return false;
  551. }
  552. return true;
  553. }
  554. static int validate_address_map(struct addr_ctx *ctx)
  555. {
  556. switch (ctx->map.intlv_mode) {
  557. case DF2_2CHAN_HASH:
  558. case DF3_COD4_2CHAN_HASH:
  559. case DF3_COD2_4CHAN_HASH:
  560. case DF3_COD1_8CHAN_HASH:
  561. if (!map_bits_valid(ctx, 8, 9, 1, 1))
  562. goto err;
  563. break;
  564. case DF4_NPS4_2CHAN_HASH:
  565. case DF4_NPS2_4CHAN_HASH:
  566. case DF4_NPS1_8CHAN_HASH:
  567. case DF4p5_NPS4_2CHAN_1K_HASH:
  568. case DF4p5_NPS4_2CHAN_2K_HASH:
  569. case DF4p5_NPS2_4CHAN_1K_HASH:
  570. case DF4p5_NPS2_4CHAN_2K_HASH:
  571. case DF4p5_NPS1_8CHAN_1K_HASH:
  572. case DF4p5_NPS1_8CHAN_2K_HASH:
  573. case DF4p5_NPS1_16CHAN_1K_HASH:
  574. case DF4p5_NPS1_16CHAN_2K_HASH:
  575. if (!map_bits_valid(ctx, 8, 8, 1, 2))
  576. goto err;
  577. break;
  578. case DF4p5_NPS4_3CHAN_1K_HASH:
  579. case DF4p5_NPS4_3CHAN_2K_HASH:
  580. case DF4p5_NPS2_5CHAN_1K_HASH:
  581. case DF4p5_NPS2_5CHAN_2K_HASH:
  582. case DF4p5_NPS2_6CHAN_1K_HASH:
  583. case DF4p5_NPS2_6CHAN_2K_HASH:
  584. case DF4p5_NPS1_10CHAN_1K_HASH:
  585. case DF4p5_NPS1_10CHAN_2K_HASH:
  586. case DF4p5_NPS1_12CHAN_1K_HASH:
  587. case DF4p5_NPS1_12CHAN_2K_HASH:
  588. if (ctx->map.num_intlv_sockets != 1 || !map_bits_valid(ctx, 8, 0, 1, 1))
  589. goto err;
  590. break;
  591. case DF4p5_NPS0_24CHAN_1K_HASH:
  592. case DF4p5_NPS0_24CHAN_2K_HASH:
  593. if (ctx->map.num_intlv_sockets < 2 || !map_bits_valid(ctx, 8, 0, 1, 2))
  594. goto err;
  595. break;
  596. case MI3_HASH_8CHAN:
  597. case MI3_HASH_16CHAN:
  598. case MI3_HASH_32CHAN:
  599. if (!map_bits_valid(ctx, 8, 8, 4, 1))
  600. goto err;
  601. break;
  602. /* Nothing to do for modes that don't need special validation checks. */
  603. default:
  604. break;
  605. }
  606. return 0;
  607. err:
  608. atl_debug(ctx, "Inconsistent address map");
  609. return -EINVAL;
  610. }
  611. static void dump_address_map(struct dram_addr_map *map)
  612. {
  613. u8 i;
  614. pr_debug("intlv_mode=0x%x", map->intlv_mode);
  615. pr_debug("num=0x%x", map->num);
  616. pr_debug("base=0x%x", map->base);
  617. pr_debug("limit=0x%x", map->limit);
  618. pr_debug("ctl=0x%x", map->ctl);
  619. pr_debug("intlv=0x%x", map->intlv);
  620. for (i = 0; i < MAX_COH_ST_CHANNELS; i++)
  621. pr_debug("remap_array[%u]=0x%x", i, map->remap_array[i]);
  622. pr_debug("intlv_bit_pos=%u", map->intlv_bit_pos);
  623. pr_debug("num_intlv_chan=%u", map->num_intlv_chan);
  624. pr_debug("num_intlv_dies=%u", map->num_intlv_dies);
  625. pr_debug("num_intlv_sockets=%u", map->num_intlv_sockets);
  626. pr_debug("total_intlv_chan=%u", map->total_intlv_chan);
  627. pr_debug("total_intlv_bits=%u", map->total_intlv_bits);
  628. }
  629. int get_address_map(struct addr_ctx *ctx)
  630. {
  631. int ret;
  632. ret = get_address_map_common(ctx);
  633. if (ret)
  634. return ret;
  635. ret = get_global_map_data(ctx);
  636. if (ret)
  637. return ret;
  638. dump_address_map(&ctx->map);
  639. ret = validate_address_map(ctx);
  640. if (ret)
  641. return ret;
  642. return ret;
  643. }