amd_bus.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/init.h>
  3. #include <linux/pci.h>
  4. #include <linux/topology.h>
  5. #include <linux/cpu.h>
  6. #include <linux/range.h>
  7. #include <asm/amd_nb.h>
  8. #include <asm/pci_x86.h>
  9. #include <asm/pci-direct.h>
  10. #include "bus_numa.h"
  11. #define AMD_NB_F0_NODE_ID 0x60
  12. #define AMD_NB_F0_UNIT_ID 0x64
  13. #define AMD_NB_F1_CONFIG_MAP_REG 0xe0
  14. #define RANGE_NUM 16
  15. #define AMD_NB_F1_CONFIG_MAP_RANGES 4
  16. struct amd_hostbridge {
  17. u32 bus;
  18. u32 slot;
  19. u32 device;
  20. };
  21. /*
  22. * IMPORTANT NOTE:
  23. * hb_probes[] and early_root_info_init() is in maintenance mode.
  24. * It only supports K8, Fam10h, Fam11h, and Fam15h_00h-0fh .
  25. * Future processor will rely on information in ACPI.
  26. */
  27. static struct amd_hostbridge hb_probes[] __initdata = {
  28. { 0, 0x18, 0x1100 }, /* K8 */
  29. { 0, 0x18, 0x1200 }, /* Family10h */
  30. { 0xff, 0, 0x1200 }, /* Family10h */
  31. { 0, 0x18, 0x1300 }, /* Family11h */
  32. { 0, 0x18, 0x1600 }, /* Family15h */
  33. };
  34. static struct pci_root_info __init *find_pci_root_info(int node, int link)
  35. {
  36. struct pci_root_info *info;
  37. /* find the position */
  38. list_for_each_entry(info, &pci_root_infos, list)
  39. if (info->node == node && info->link == link)
  40. return info;
  41. return NULL;
  42. }
  43. static inline resource_size_t cap_resource(u64 val)
  44. {
  45. if (val > RESOURCE_SIZE_MAX)
  46. return RESOURCE_SIZE_MAX;
  47. return val;
  48. }
  49. /**
  50. * early_root_info_init()
  51. * called before pcibios_scan_root and pci_scan_bus
  52. * fills the mp_bus_to_cpumask array based according
  53. * to the LDT Bus Number Registers found in the northbridge.
  54. */
  55. static int __init early_root_info_init(void)
  56. {
  57. int i;
  58. unsigned bus;
  59. unsigned slot;
  60. int node;
  61. int link;
  62. int def_node;
  63. int def_link;
  64. struct pci_root_info *info;
  65. u32 reg;
  66. u64 start;
  67. u64 end;
  68. struct range range[RANGE_NUM];
  69. u64 val;
  70. u32 address;
  71. bool found;
  72. struct resource fam10h_mmconf_res, *fam10h_mmconf;
  73. u64 fam10h_mmconf_start;
  74. u64 fam10h_mmconf_end;
  75. if (!early_pci_allowed())
  76. return -1;
  77. found = false;
  78. for (i = 0; i < ARRAY_SIZE(hb_probes); i++) {
  79. u32 id;
  80. u16 device;
  81. u16 vendor;
  82. bus = hb_probes[i].bus;
  83. slot = hb_probes[i].slot;
  84. id = read_pci_config(bus, slot, 0, PCI_VENDOR_ID);
  85. vendor = id & 0xffff;
  86. device = (id>>16) & 0xffff;
  87. if (vendor != PCI_VENDOR_ID_AMD &&
  88. vendor != PCI_VENDOR_ID_HYGON)
  89. continue;
  90. if (hb_probes[i].device == device) {
  91. found = true;
  92. break;
  93. }
  94. }
  95. if (!found)
  96. return 0;
  97. /*
  98. * We should learn topology and routing information from _PXM and
  99. * _CRS methods in the ACPI namespace. We extract node numbers
  100. * here to work around BIOSes that don't supply _PXM.
  101. */
  102. for (i = 0; i < AMD_NB_F1_CONFIG_MAP_RANGES; i++) {
  103. int min_bus;
  104. int max_bus;
  105. reg = read_pci_config(bus, slot, 1,
  106. AMD_NB_F1_CONFIG_MAP_REG + (i << 2));
  107. /* Check if that register is enabled for bus range */
  108. if ((reg & 7) != 3)
  109. continue;
  110. min_bus = (reg >> 16) & 0xff;
  111. max_bus = (reg >> 24) & 0xff;
  112. node = (reg >> 4) & 0x07;
  113. link = (reg >> 8) & 0x03;
  114. alloc_pci_root_info(min_bus, max_bus, node, link);
  115. }
  116. /*
  117. * The following code extracts routing information for use on old
  118. * systems where Linux doesn't automatically use host bridge _CRS
  119. * methods (or when the user specifies "pci=nocrs").
  120. *
  121. * We only do this through Fam11h, because _CRS should be enough on
  122. * newer systems.
  123. */
  124. if (boot_cpu_data.x86 > 0x11)
  125. return 0;
  126. /* get the default node and link for left over res */
  127. reg = read_pci_config(bus, slot, 0, AMD_NB_F0_NODE_ID);
  128. def_node = (reg >> 8) & 0x07;
  129. reg = read_pci_config(bus, slot, 0, AMD_NB_F0_UNIT_ID);
  130. def_link = (reg >> 8) & 0x03;
  131. memset(range, 0, sizeof(range));
  132. add_range(range, RANGE_NUM, 0, 0, 0xffff + 1);
  133. /* io port resource */
  134. for (i = 0; i < 4; i++) {
  135. reg = read_pci_config(bus, slot, 1, 0xc0 + (i << 3));
  136. if (!(reg & 3))
  137. continue;
  138. start = reg & 0xfff000;
  139. reg = read_pci_config(bus, slot, 1, 0xc4 + (i << 3));
  140. node = reg & 0x07;
  141. link = (reg >> 4) & 0x03;
  142. end = (reg & 0xfff000) | 0xfff;
  143. info = find_pci_root_info(node, link);
  144. if (!info)
  145. continue; /* not found */
  146. printk(KERN_DEBUG "node %d link %d: io port [%llx, %llx]\n",
  147. node, link, start, end);
  148. /* kernel only handle 16 bit only */
  149. if (end > 0xffff)
  150. end = 0xffff;
  151. update_res(info, start, end, IORESOURCE_IO, 1);
  152. subtract_range(range, RANGE_NUM, start, end + 1);
  153. }
  154. /* add left over io port range to def node/link, [0, 0xffff] */
  155. /* find the position */
  156. info = find_pci_root_info(def_node, def_link);
  157. if (info) {
  158. for (i = 0; i < RANGE_NUM; i++) {
  159. if (!range[i].end)
  160. continue;
  161. update_res(info, range[i].start, range[i].end - 1,
  162. IORESOURCE_IO, 1);
  163. }
  164. }
  165. memset(range, 0, sizeof(range));
  166. /* 0xfd00000000-0xffffffffff for HT */
  167. end = cap_resource((0xfdULL<<32) - 1);
  168. end++;
  169. add_range(range, RANGE_NUM, 0, 0, end);
  170. /* need to take out [0, TOM) for RAM*/
  171. address = MSR_K8_TOP_MEM1;
  172. rdmsrl(address, val);
  173. end = (val & 0xffffff800000ULL);
  174. printk(KERN_INFO "TOM: %016llx aka %lldM\n", end, end>>20);
  175. if (end < (1ULL<<32))
  176. subtract_range(range, RANGE_NUM, 0, end);
  177. /* get mmconfig */
  178. fam10h_mmconf = amd_get_mmconfig_range(&fam10h_mmconf_res);
  179. /* need to take out mmconf range */
  180. if (fam10h_mmconf) {
  181. printk(KERN_DEBUG "Fam 10h mmconf %pR\n", fam10h_mmconf);
  182. fam10h_mmconf_start = fam10h_mmconf->start;
  183. fam10h_mmconf_end = fam10h_mmconf->end;
  184. subtract_range(range, RANGE_NUM, fam10h_mmconf_start,
  185. fam10h_mmconf_end + 1);
  186. } else {
  187. fam10h_mmconf_start = 0;
  188. fam10h_mmconf_end = 0;
  189. }
  190. /* mmio resource */
  191. for (i = 0; i < 8; i++) {
  192. reg = read_pci_config(bus, slot, 1, 0x80 + (i << 3));
  193. if (!(reg & 3))
  194. continue;
  195. start = reg & 0xffffff00; /* 39:16 on 31:8*/
  196. start <<= 8;
  197. reg = read_pci_config(bus, slot, 1, 0x84 + (i << 3));
  198. node = reg & 0x07;
  199. link = (reg >> 4) & 0x03;
  200. end = (reg & 0xffffff00);
  201. end <<= 8;
  202. end |= 0xffff;
  203. info = find_pci_root_info(node, link);
  204. if (!info)
  205. continue;
  206. printk(KERN_DEBUG "node %d link %d: mmio [%llx, %llx]",
  207. node, link, start, end);
  208. /*
  209. * some sick allocation would have range overlap with fam10h
  210. * mmconf range, so need to update start and end.
  211. */
  212. if (fam10h_mmconf_end) {
  213. int changed = 0;
  214. u64 endx = 0;
  215. if (start >= fam10h_mmconf_start &&
  216. start <= fam10h_mmconf_end) {
  217. start = fam10h_mmconf_end + 1;
  218. changed = 1;
  219. }
  220. if (end >= fam10h_mmconf_start &&
  221. end <= fam10h_mmconf_end) {
  222. end = fam10h_mmconf_start - 1;
  223. changed = 1;
  224. }
  225. if (start < fam10h_mmconf_start &&
  226. end > fam10h_mmconf_end) {
  227. /* we got a hole */
  228. endx = fam10h_mmconf_start - 1;
  229. update_res(info, start, endx, IORESOURCE_MEM, 0);
  230. subtract_range(range, RANGE_NUM, start,
  231. endx + 1);
  232. printk(KERN_CONT " ==> [%llx, %llx]", start, endx);
  233. start = fam10h_mmconf_end + 1;
  234. changed = 1;
  235. }
  236. if (changed) {
  237. if (start <= end) {
  238. printk(KERN_CONT " %s [%llx, %llx]", endx ? "and" : "==>", start, end);
  239. } else {
  240. printk(KERN_CONT "%s\n", endx?"":" ==> none");
  241. continue;
  242. }
  243. }
  244. }
  245. update_res(info, cap_resource(start), cap_resource(end),
  246. IORESOURCE_MEM, 1);
  247. subtract_range(range, RANGE_NUM, start, end + 1);
  248. printk(KERN_CONT "\n");
  249. }
  250. /* need to take out [4G, TOM2) for RAM*/
  251. /* SYS_CFG */
  252. address = MSR_AMD64_SYSCFG;
  253. rdmsrl(address, val);
  254. /* TOP_MEM2 is enabled? */
  255. if (val & (1<<21)) {
  256. /* TOP_MEM2 */
  257. address = MSR_K8_TOP_MEM2;
  258. rdmsrl(address, val);
  259. end = (val & 0xffffff800000ULL);
  260. printk(KERN_INFO "TOM2: %016llx aka %lldM\n", end, end>>20);
  261. subtract_range(range, RANGE_NUM, 1ULL<<32, end);
  262. }
  263. /*
  264. * add left over mmio range to def node/link ?
  265. * that is tricky, just record range in from start_min to 4G
  266. */
  267. info = find_pci_root_info(def_node, def_link);
  268. if (info) {
  269. for (i = 0; i < RANGE_NUM; i++) {
  270. if (!range[i].end)
  271. continue;
  272. update_res(info, cap_resource(range[i].start),
  273. cap_resource(range[i].end - 1),
  274. IORESOURCE_MEM, 1);
  275. }
  276. }
  277. list_for_each_entry(info, &pci_root_infos, list) {
  278. int busnum;
  279. struct pci_root_res *root_res;
  280. busnum = info->busn.start;
  281. printk(KERN_DEBUG "bus: %pR on node %x link %x\n",
  282. &info->busn, info->node, info->link);
  283. list_for_each_entry(root_res, &info->resources, list)
  284. printk(KERN_DEBUG "bus: %02x %pR\n",
  285. busnum, &root_res->res);
  286. }
  287. return 0;
  288. }
  289. #define ENABLE_CF8_EXT_CFG (1ULL << 46)
  290. static int amd_bus_cpu_online(unsigned int cpu)
  291. {
  292. u64 reg;
  293. rdmsrl(MSR_AMD64_NB_CFG, reg);
  294. if (!(reg & ENABLE_CF8_EXT_CFG)) {
  295. reg |= ENABLE_CF8_EXT_CFG;
  296. wrmsrl(MSR_AMD64_NB_CFG, reg);
  297. }
  298. return 0;
  299. }
  300. static void __init pci_enable_pci_io_ecs(void)
  301. {
  302. #ifdef CONFIG_AMD_NB
  303. unsigned int i, n;
  304. for (n = i = 0; !n && amd_nb_bus_dev_ranges[i].dev_limit; ++i) {
  305. u8 bus = amd_nb_bus_dev_ranges[i].bus;
  306. u8 slot = amd_nb_bus_dev_ranges[i].dev_base;
  307. u8 limit = amd_nb_bus_dev_ranges[i].dev_limit;
  308. for (; slot < limit; ++slot) {
  309. u32 val = read_pci_config(bus, slot, 3, 0);
  310. if (!early_is_amd_nb(val))
  311. continue;
  312. val = read_pci_config(bus, slot, 3, 0x8c);
  313. if (!(val & (ENABLE_CF8_EXT_CFG >> 32))) {
  314. val |= ENABLE_CF8_EXT_CFG >> 32;
  315. write_pci_config(bus, slot, 3, 0x8c, val);
  316. }
  317. ++n;
  318. }
  319. }
  320. #endif
  321. }
  322. static int __init pci_io_ecs_init(void)
  323. {
  324. int ret;
  325. /* assume all cpus from fam10h have IO ECS */
  326. if (boot_cpu_data.x86 < 0x10)
  327. return 0;
  328. /* Try the PCI method first. */
  329. if (early_pci_allowed())
  330. pci_enable_pci_io_ecs();
  331. ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "pci/amd_bus:online",
  332. amd_bus_cpu_online, NULL);
  333. WARN_ON(ret < 0);
  334. pci_probe |= PCI_HAS_IO_ECS;
  335. return 0;
  336. }
  337. static int __init amd_postcore_init(void)
  338. {
  339. if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD &&
  340. boot_cpu_data.x86_vendor != X86_VENDOR_HYGON)
  341. return 0;
  342. early_root_info_init();
  343. pci_io_ecs_init();
  344. return 0;
  345. }
  346. postcore_initcall(amd_postcore_init);