amd_bus.c 9.5 KB

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