0015-haswell-NRI-Program-memory-map.patch 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. From 89ff35083af68d1b24c1633886202ecc153af67d Mon Sep 17 00:00:00 2001
  2. From: Angel Pons <th3fanbus@gmail.com>
  3. Date: Sat, 7 May 2022 21:24:50 +0200
  4. Subject: [PATCH 15/26] haswell NRI: Program memory map
  5. This is very similar to Sandy/Ivy Bridge, except that there's several
  6. registers to program in GDXCBAR. One of these GDXCBAR registers has a
  7. lock bit that must be set in order for the memory controller to allow
  8. normal access to DRAM. And it took me four months to realize this one
  9. bit was the only reason why native raminit did not work.
  10. Change-Id: I3af73a018a7ba948701a542e661e7fefd57591fe
  11. Signed-off-by: Angel Pons <th3fanbus@gmail.com>
  12. ---
  13. .../intel/haswell/native_raminit/Makefile.inc | 1 +
  14. .../intel/haswell/native_raminit/memory_map.c | 183 ++++++++++++++++++
  15. .../haswell/native_raminit/raminit_main.c | 1 +
  16. .../haswell/native_raminit/raminit_native.h | 1 +
  17. .../intel/haswell/registers/host_bridge.h | 2 +
  18. 5 files changed, 188 insertions(+)
  19. create mode 100644 src/northbridge/intel/haswell/native_raminit/memory_map.c
  20. diff --git a/src/northbridge/intel/haswell/native_raminit/Makefile.inc b/src/northbridge/intel/haswell/native_raminit/Makefile.inc
  21. index fc55277a65..37d527e972 100644
  22. --- a/src/northbridge/intel/haswell/native_raminit/Makefile.inc
  23. +++ b/src/northbridge/intel/haswell/native_raminit/Makefile.inc
  24. @@ -4,6 +4,7 @@ romstage-y += configure_mc.c
  25. romstage-y += lookup_timings.c
  26. romstage-y += init_mpll.c
  27. romstage-y += io_comp_control.c
  28. +romstage-y += memory_map.c
  29. romstage-y += raminit_main.c
  30. romstage-y += raminit_native.c
  31. romstage-y += spd_bitmunching.c
  32. diff --git a/src/northbridge/intel/haswell/native_raminit/memory_map.c b/src/northbridge/intel/haswell/native_raminit/memory_map.c
  33. new file mode 100644
  34. index 0000000000..e3aded2b37
  35. --- /dev/null
  36. +++ b/src/northbridge/intel/haswell/native_raminit/memory_map.c
  37. @@ -0,0 +1,183 @@
  38. +/* SPDX-License-Identifier: GPL-2.0-or-later */
  39. +
  40. +#include <device/pci_ops.h>
  41. +#include <northbridge/intel/haswell/haswell.h>
  42. +#include <southbridge/intel/lynxpoint/me.h>
  43. +#include <types.h>
  44. +
  45. +#include "raminit_native.h"
  46. +
  47. +/* GDXCBAR */
  48. +#define MPCOHTRK_GDXC_MOT_ADDRESS_LO 0x10
  49. +#define MPCOHTRK_GDXC_MOT_ADDRESS_HI 0x14
  50. +#define MPCOHTRK_GDXC_MOT_REGION 0x18
  51. +
  52. +#define MPCOHTRK_GDXC_OCLA_ADDRESS_LO 0x20
  53. +#define MPCOHTRK_GDXC_OCLA_ADDRESS_HI 0x24
  54. +#define MPCOHTRK_GDXC_OCLA_REGION 0x28
  55. +
  56. +/* This lock bit made me lose what little sanity I had left. - Angel Pons */
  57. +#define MPCOHTRK_GDXC_OCLA_ADDRESS_HI_LOCK BIT(2)
  58. +
  59. +static inline uint32_t gdxcbar_read32(const uintptr_t offset)
  60. +{
  61. + return read32p((mchbar_read32(GDXCBAR) & ~1) + offset);
  62. +}
  63. +
  64. +static inline void gdxcbar_write32(const uintptr_t offset, const uint32_t value)
  65. +{
  66. + write32p((mchbar_read32(GDXCBAR) & ~1) + offset, value);
  67. +}
  68. +
  69. +static inline void gdxcbar_clrsetbits32(const uintptr_t offset, uint32_t clear, uint32_t set)
  70. +{
  71. + const uintptr_t address = (mchbar_read32(GDXCBAR) & ~1) + offset;
  72. + clrsetbits32((void *)address, clear, set);
  73. +}
  74. +
  75. +#define gdxcbar_setbits32(offset, set) gdxcbar_clrsetbits32(offset, 0, set)
  76. +#define gdxcbar_clrbits32(offset, clear) gdxcbar_clrsetbits32(offset, clear, 0)
  77. +
  78. +/* All values stored in here (except the bool) are specified in MiB */
  79. +struct memory_map_data {
  80. + uint32_t dpr_size;
  81. + uint32_t tseg_size;
  82. + uint32_t gtt_size;
  83. + uint32_t gms_size;
  84. + uint32_t me_stolen_size;
  85. + uint32_t mmio_size;
  86. + uint32_t touud;
  87. + uint32_t remaplimit;
  88. + uint32_t remapbase;
  89. + uint32_t tom;
  90. + uint32_t tom_minus_me;
  91. + uint32_t tolud;
  92. + uint32_t bdsm_base;
  93. + uint32_t gtt_base;
  94. + uint32_t tseg_base;
  95. + bool reclaim_possible;
  96. +};
  97. +
  98. +static void compute_memory_map(struct memory_map_data *map)
  99. +{
  100. + map->tom_minus_me = map->tom - map->me_stolen_size;
  101. +
  102. + /*
  103. + * MMIO size will actually be slightly smaller than computed,
  104. + * but matches what MRC does and is more MTRR-friendly given
  105. + * that TSEG is treated as WB, but SMRR makes TSEG UC anyway.
  106. + */
  107. + const uint32_t mmio_size = MIN(map->tom_minus_me, 4096) / 2;
  108. + map->gtt_base = ALIGN_DOWN(mmio_size, map->tseg_size);
  109. + map->tseg_base = map->gtt_base - map->tseg_size;
  110. + map->bdsm_base = map->gtt_base + map->gtt_size;
  111. + map->tolud = map->bdsm_base + map->gms_size;
  112. + map->reclaim_possible = map->tom_minus_me > map->tolud;
  113. +
  114. + if (map->reclaim_possible) {
  115. + map->remapbase = MAX(4096, map->tom_minus_me);
  116. + map->touud = MIN(4096, map->tom_minus_me) + map->remapbase - map->tolud;
  117. + map->remaplimit = map->touud - 1;
  118. + } else {
  119. + map->remapbase = 0;
  120. + map->remaplimit = 0;
  121. + map->touud = map->tom_minus_me;
  122. + }
  123. +}
  124. +
  125. +static void display_memory_map(const struct memory_map_data *map)
  126. +{
  127. + if (!CONFIG(DEBUG_RAM_SETUP))
  128. + return;
  129. +
  130. + printk(BIOS_DEBUG, "============ MEMORY MAP ============\n");
  131. + printk(BIOS_DEBUG, "\n");
  132. + printk(BIOS_DEBUG, "dpr_size = %u MiB\n", map->dpr_size);
  133. + printk(BIOS_DEBUG, "tseg_size = %u MiB\n", map->tseg_size);
  134. + printk(BIOS_DEBUG, "gtt_size = %u MiB\n", map->gtt_size);
  135. + printk(BIOS_DEBUG, "gms_size = %u MiB\n", map->gms_size);
  136. + printk(BIOS_DEBUG, "me_stolen_size = %u MiB\n", map->me_stolen_size);
  137. + printk(BIOS_DEBUG, "\n");
  138. + printk(BIOS_DEBUG, "touud = %u MiB\n", map->touud);
  139. + printk(BIOS_DEBUG, "remaplimit = %u MiB\n", map->remaplimit);
  140. + printk(BIOS_DEBUG, "remapbase = %u MiB\n", map->remapbase);
  141. + printk(BIOS_DEBUG, "tom = %u MiB\n", map->tom);
  142. + printk(BIOS_DEBUG, "tom_minus_me = %u MiB\n", map->tom_minus_me);
  143. + printk(BIOS_DEBUG, "tolud = %u MiB\n", map->tolud);
  144. + printk(BIOS_DEBUG, "bdsm_base = %u MiB\n", map->bdsm_base);
  145. + printk(BIOS_DEBUG, "gtt_base = %u MiB\n", map->gtt_base);
  146. + printk(BIOS_DEBUG, "tseg_base = %u MiB\n", map->tseg_base);
  147. + printk(BIOS_DEBUG, "\n");
  148. + printk(BIOS_DEBUG, "reclaim_possible = %s\n", map->reclaim_possible ? "Yes" : "No");
  149. +}
  150. +
  151. +static void map_write_reg64(const uint16_t reg, const uint64_t size)
  152. +{
  153. + const uint64_t value = size << 20;
  154. + pci_write_config32(HOST_BRIDGE, reg + 4, value >> 32);
  155. + pci_write_config32(HOST_BRIDGE, reg + 0, value >> 0);
  156. +}
  157. +
  158. +static void map_write_reg32(const uint16_t reg, const uint32_t size)
  159. +{
  160. + const uint32_t value = size << 20;
  161. + pci_write_config32(HOST_BRIDGE, reg, value);
  162. +}
  163. +
  164. +static void program_memory_map(const struct memory_map_data *map)
  165. +{
  166. + map_write_reg64(TOUUD, map->touud);
  167. + map_write_reg64(TOM, map->tom);
  168. + if (map->reclaim_possible) {
  169. + map_write_reg64(REMAPBASE, map->remapbase);
  170. + map_write_reg64(REMAPLIMIT, map->remaplimit);
  171. + }
  172. + if (map->me_stolen_size) {
  173. + map_write_reg64(MESEG_LIMIT, 0x80000 - map->me_stolen_size);
  174. + map_write_reg64(MESEG_BASE, map->tom_minus_me);
  175. + pci_or_config32(HOST_BRIDGE, MESEG_LIMIT, ME_STLEN_EN);
  176. + }
  177. + map_write_reg32(TOLUD, map->tolud);
  178. + map_write_reg32(BDSM, map->bdsm_base);
  179. + map_write_reg32(BGSM, map->gtt_base);
  180. + map_write_reg32(TSEG, map->tseg_base);
  181. +
  182. + const uint32_t dpr_reg = map->tseg_base << 20 | map->dpr_size << 4;
  183. + pci_write_config32(HOST_BRIDGE, DPR, dpr_reg);
  184. +
  185. + const uint16_t gfx_stolen_size = GGC_IGD_MEM_IN_32MB_UNITS(map->gms_size / 32);
  186. + const uint16_t ggc = map->gtt_size << 8 | gfx_stolen_size;
  187. + pci_write_config16(HOST_BRIDGE, GGC, ggc);
  188. +
  189. + /** TODO: Do not hardcode these? GDXC has weird alignment requirements, though. **/
  190. + gdxcbar_write32(MPCOHTRK_GDXC_MOT_ADDRESS_LO, 0);
  191. + gdxcbar_write32(MPCOHTRK_GDXC_MOT_ADDRESS_HI, 0);
  192. + gdxcbar_write32(MPCOHTRK_GDXC_MOT_REGION, 0);
  193. +
  194. + gdxcbar_write32(MPCOHTRK_GDXC_OCLA_ADDRESS_LO, 0);
  195. + gdxcbar_write32(MPCOHTRK_GDXC_OCLA_ADDRESS_HI, 0);
  196. + gdxcbar_write32(MPCOHTRK_GDXC_OCLA_REGION, 0);
  197. +
  198. + gdxcbar_setbits32(MPCOHTRK_GDXC_OCLA_ADDRESS_HI, MPCOHTRK_GDXC_OCLA_ADDRESS_HI_LOCK);
  199. +}
  200. +
  201. +enum raminit_status configure_memory_map(struct sysinfo *ctrl)
  202. +{
  203. + struct memory_map_data memory_map = {
  204. + .tom = ctrl->channel_size_mb[0] + ctrl->channel_size_mb[1],
  205. + .dpr_size = CONFIG_INTEL_TXT_DPR_SIZE,
  206. + .tseg_size = CONFIG_SMM_TSEG_SIZE >> 20,
  207. + .me_stolen_size = intel_early_me_uma_size(),
  208. + };
  209. + /** FIXME: MRC hardcodes iGPU parameters, but we should not **/
  210. + const bool igpu_on = pci_read_config32(HOST_BRIDGE, DEVEN) & DEVEN_D2EN;
  211. + if (CONFIG(ONBOARD_VGA_IS_PRIMARY) || igpu_on) {
  212. + memory_map.gtt_size = 2;
  213. + memory_map.gms_size = 64;
  214. + pci_or_config32(HOST_BRIDGE, DEVEN, DEVEN_D2EN);
  215. + }
  216. + compute_memory_map(&memory_map);
  217. + display_memory_map(&memory_map);
  218. + program_memory_map(&memory_map);
  219. + return 0;
  220. +}
  221. diff --git a/src/northbridge/intel/haswell/native_raminit/raminit_main.c b/src/northbridge/intel/haswell/native_raminit/raminit_main.c
  222. index 3a773cfa19..136a8ba989 100644
  223. --- a/src/northbridge/intel/haswell/native_raminit/raminit_main.c
  224. +++ b/src/northbridge/intel/haswell/native_raminit/raminit_main.c
  225. @@ -24,6 +24,7 @@ static const struct task_entry cold_boot[] = {
  226. { initialise_mpll, true, "INITMPLL", },
  227. { convert_timings, true, "CONVTIM", },
  228. { configure_mc, true, "CONFMC", },
  229. + { configure_memory_map, true, "MEMMAP", },
  230. };
  231. /* Return a generic stepping value to make stepping checks simpler */
  232. diff --git a/src/northbridge/intel/haswell/native_raminit/raminit_native.h b/src/northbridge/intel/haswell/native_raminit/raminit_native.h
  233. index cd1f2eb2a5..4763b25e8d 100644
  234. --- a/src/northbridge/intel/haswell/native_raminit/raminit_native.h
  235. +++ b/src/northbridge/intel/haswell/native_raminit/raminit_native.h
  236. @@ -202,6 +202,7 @@ enum raminit_status collect_spd_info(struct sysinfo *ctrl);
  237. enum raminit_status initialise_mpll(struct sysinfo *ctrl);
  238. enum raminit_status convert_timings(struct sysinfo *ctrl);
  239. enum raminit_status configure_mc(struct sysinfo *ctrl);
  240. +enum raminit_status configure_memory_map(struct sysinfo *ctrl);
  241. void configure_timings(struct sysinfo *ctrl);
  242. void configure_refresh(struct sysinfo *ctrl);
  243. diff --git a/src/northbridge/intel/haswell/registers/host_bridge.h b/src/northbridge/intel/haswell/registers/host_bridge.h
  244. index 1ee0ab2890..0228cf6bb9 100644
  245. --- a/src/northbridge/intel/haswell/registers/host_bridge.h
  246. +++ b/src/northbridge/intel/haswell/registers/host_bridge.h
  247. @@ -34,6 +34,8 @@
  248. #define MESEG_BASE 0x70 /* Management Engine Base */
  249. #define MESEG_LIMIT 0x78 /* Management Engine Limit */
  250. +#define MELCK (1 << 10) /* ME Range Lock */
  251. +#define ME_STLEN_EN (1 << 11) /* ME Stolen Memory Enable */
  252. #define PAM0 0x80
  253. #define PAM1 0x81
  254. --
  255. 2.39.2