amd_bus.c 9.5 KB

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