amd_bus.c 9.9 KB

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