amd_bus.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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. /*
  11. * This discovers the pcibus <-> node mapping on AMD K8.
  12. * also get peer root bus resource for io,mmio
  13. */
  14. struct pci_hostbridge_probe {
  15. u32 bus;
  16. u32 slot;
  17. u32 vendor;
  18. u32 device;
  19. };
  20. static struct pci_hostbridge_probe pci_probes[] __initdata = {
  21. { 0, 0x18, PCI_VENDOR_ID_AMD, 0x1100 },
  22. { 0, 0x18, PCI_VENDOR_ID_AMD, 0x1200 },
  23. { 0xff, 0, PCI_VENDOR_ID_AMD, 0x1200 },
  24. { 0, 0x18, PCI_VENDOR_ID_AMD, 0x1300 },
  25. };
  26. static u64 __initdata fam10h_mmconf_start;
  27. static u64 __initdata fam10h_mmconf_end;
  28. static void __init get_pci_mmcfg_amd_fam10h_range(void)
  29. {
  30. u32 address;
  31. u64 base, msr;
  32. unsigned segn_busn_bits;
  33. /* assume all cpus from fam10h have mmconf */
  34. if (boot_cpu_data.x86 < 0x10)
  35. return;
  36. address = MSR_FAM10H_MMIO_CONF_BASE;
  37. rdmsrl(address, msr);
  38. /* mmconfig is not enable */
  39. if (!(msr & FAM10H_MMIO_CONF_ENABLE))
  40. return;
  41. base = msr & (FAM10H_MMIO_CONF_BASE_MASK<<FAM10H_MMIO_CONF_BASE_SHIFT);
  42. segn_busn_bits = (msr >> FAM10H_MMIO_CONF_BUSRANGE_SHIFT) &
  43. FAM10H_MMIO_CONF_BUSRANGE_MASK;
  44. fam10h_mmconf_start = base;
  45. fam10h_mmconf_end = base + (1ULL<<(segn_busn_bits + 20)) - 1;
  46. }
  47. #define RANGE_NUM 16
  48. /**
  49. * early_fill_mp_bus_to_node()
  50. * called before pcibios_scan_root and pci_scan_bus
  51. * fills the mp_bus_to_cpumask array based according to the LDT Bus Number
  52. * Registers found in the K8 northbridge
  53. */
  54. static int __init early_fill_mp_bus_info(void)
  55. {
  56. int i;
  57. int j;
  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. struct resource *res;
  67. u64 start;
  68. u64 end;
  69. struct range range[RANGE_NUM];
  70. u64 val;
  71. u32 address;
  72. bool found;
  73. if (!early_pci_allowed())
  74. return -1;
  75. found = false;
  76. for (i = 0; i < ARRAY_SIZE(pci_probes); i++) {
  77. u32 id;
  78. u16 device;
  79. u16 vendor;
  80. bus = pci_probes[i].bus;
  81. slot = pci_probes[i].slot;
  82. id = read_pci_config(bus, slot, 0, PCI_VENDOR_ID);
  83. vendor = id & 0xffff;
  84. device = (id>>16) & 0xffff;
  85. if (pci_probes[i].vendor == vendor &&
  86. pci_probes[i].device == device) {
  87. found = true;
  88. break;
  89. }
  90. }
  91. if (!found)
  92. return 0;
  93. pci_root_num = 0;
  94. for (i = 0; i < 4; i++) {
  95. int min_bus;
  96. int max_bus;
  97. reg = read_pci_config(bus, slot, 1, 0xe0 + (i << 2));
  98. /* Check if that register is enabled for bus range */
  99. if ((reg & 7) != 3)
  100. continue;
  101. min_bus = (reg >> 16) & 0xff;
  102. max_bus = (reg >> 24) & 0xff;
  103. node = (reg >> 4) & 0x07;
  104. #ifdef CONFIG_NUMA
  105. for (j = min_bus; j <= max_bus; j++)
  106. set_mp_bus_to_node(j, node);
  107. #endif
  108. link = (reg >> 8) & 0x03;
  109. info = &pci_root_info[pci_root_num];
  110. info->bus_min = min_bus;
  111. info->bus_max = max_bus;
  112. info->node = node;
  113. info->link = link;
  114. sprintf(info->name, "PCI Bus #%02x", min_bus);
  115. pci_root_num++;
  116. }
  117. /* get the default node and link for left over res */
  118. reg = read_pci_config(bus, slot, 0, 0x60);
  119. def_node = (reg >> 8) & 0x07;
  120. reg = read_pci_config(bus, slot, 0, 0x64);
  121. def_link = (reg >> 8) & 0x03;
  122. memset(range, 0, sizeof(range));
  123. add_range(range, RANGE_NUM, 0, 0, 0xffff + 1);
  124. /* io port resource */
  125. for (i = 0; i < 4; i++) {
  126. reg = read_pci_config(bus, slot, 1, 0xc0 + (i << 3));
  127. if (!(reg & 3))
  128. continue;
  129. start = reg & 0xfff000;
  130. reg = read_pci_config(bus, slot, 1, 0xc4 + (i << 3));
  131. node = reg & 0x07;
  132. link = (reg >> 4) & 0x03;
  133. end = (reg & 0xfff000) | 0xfff;
  134. /* find the position */
  135. for (j = 0; j < pci_root_num; j++) {
  136. info = &pci_root_info[j];
  137. if (info->node == node && info->link == link)
  138. break;
  139. }
  140. if (j == pci_root_num)
  141. continue; /* not found */
  142. info = &pci_root_info[j];
  143. printk(KERN_DEBUG "node %d link %d: io port [%llx, %llx]\n",
  144. node, link, start, end);
  145. /* kernel only handle 16 bit only */
  146. if (end > 0xffff)
  147. end = 0xffff;
  148. update_res(info, start, end, IORESOURCE_IO, 1);
  149. subtract_range(range, RANGE_NUM, start, end + 1);
  150. }
  151. /* add left over io port range to def node/link, [0, 0xffff] */
  152. /* find the position */
  153. for (j = 0; j < pci_root_num; j++) {
  154. info = &pci_root_info[j];
  155. if (info->node == def_node && info->link == def_link)
  156. break;
  157. }
  158. if (j < pci_root_num) {
  159. info = &pci_root_info[j];
  160. for (i = 0; i < RANGE_NUM; i++) {
  161. if (!range[i].end)
  162. continue;
  163. update_res(info, range[i].start, range[i].end - 1,
  164. IORESOURCE_IO, 1);
  165. }
  166. }
  167. memset(range, 0, sizeof(range));
  168. /* 0xfd00000000-0xffffffffff for HT */
  169. end = cap_resource((0xfdULL<<32) - 1);
  170. end++;
  171. add_range(range, RANGE_NUM, 0, 0, end);
  172. /* need to take out [0, TOM) for RAM*/
  173. address = MSR_K8_TOP_MEM1;
  174. rdmsrl(address, val);
  175. end = (val & 0xffffff800000ULL);
  176. printk(KERN_INFO "TOM: %016llx aka %lldM\n", end, end>>20);
  177. if (end < (1ULL<<32))
  178. subtract_range(range, RANGE_NUM, 0, end);
  179. /* get mmconfig */
  180. get_pci_mmcfg_amd_fam10h_range();
  181. /* need to take out mmconf range */
  182. if (fam10h_mmconf_end) {
  183. printk(KERN_DEBUG "Fam 10h mmconf [%llx, %llx]\n", fam10h_mmconf_start, fam10h_mmconf_end);
  184. subtract_range(range, RANGE_NUM, fam10h_mmconf_start,
  185. fam10h_mmconf_end + 1);
  186. }
  187. /* mmio resource */
  188. for (i = 0; i < 8; i++) {
  189. reg = read_pci_config(bus, slot, 1, 0x80 + (i << 3));
  190. if (!(reg & 3))
  191. continue;
  192. start = reg & 0xffffff00; /* 39:16 on 31:8*/
  193. start <<= 8;
  194. reg = read_pci_config(bus, slot, 1, 0x84 + (i << 3));
  195. node = reg & 0x07;
  196. link = (reg >> 4) & 0x03;
  197. end = (reg & 0xffffff00);
  198. end <<= 8;
  199. end |= 0xffff;
  200. /* find the position */
  201. for (j = 0; j < pci_root_num; j++) {
  202. info = &pci_root_info[j];
  203. if (info->node == node && info->link == link)
  204. break;
  205. }
  206. if (j == pci_root_num)
  207. continue; /* not found */
  208. info = &pci_root_info[j];
  209. printk(KERN_DEBUG "node %d link %d: mmio [%llx, %llx]",
  210. node, link, start, end);
  211. /*
  212. * some sick allocation would have range overlap with fam10h
  213. * mmconf range, so need to update start and end.
  214. */
  215. if (fam10h_mmconf_end) {
  216. int changed = 0;
  217. u64 endx = 0;
  218. if (start >= fam10h_mmconf_start &&
  219. start <= fam10h_mmconf_end) {
  220. start = fam10h_mmconf_end + 1;
  221. changed = 1;
  222. }
  223. if (end >= fam10h_mmconf_start &&
  224. end <= fam10h_mmconf_end) {
  225. end = fam10h_mmconf_start - 1;
  226. changed = 1;
  227. }
  228. if (start < fam10h_mmconf_start &&
  229. end > fam10h_mmconf_end) {
  230. /* we got a hole */
  231. endx = fam10h_mmconf_start - 1;
  232. update_res(info, start, endx, IORESOURCE_MEM, 0);
  233. subtract_range(range, RANGE_NUM, start,
  234. endx + 1);
  235. printk(KERN_CONT " ==> [%llx, %llx]", start, endx);
  236. start = fam10h_mmconf_end + 1;
  237. changed = 1;
  238. }
  239. if (changed) {
  240. if (start <= end) {
  241. printk(KERN_CONT " %s [%llx, %llx]", endx ? "and" : "==>", start, end);
  242. } else {
  243. printk(KERN_CONT "%s\n", endx?"":" ==> none");
  244. continue;
  245. }
  246. }
  247. }
  248. update_res(info, cap_resource(start), cap_resource(end),
  249. IORESOURCE_MEM, 1);
  250. subtract_range(range, RANGE_NUM, start, end + 1);
  251. printk(KERN_CONT "\n");
  252. }
  253. /* need to take out [4G, TOM2) for RAM*/
  254. /* SYS_CFG */
  255. address = MSR_K8_SYSCFG;
  256. rdmsrl(address, val);
  257. /* TOP_MEM2 is enabled? */
  258. if (val & (1<<21)) {
  259. /* TOP_MEM2 */
  260. address = MSR_K8_TOP_MEM2;
  261. rdmsrl(address, val);
  262. end = (val & 0xffffff800000ULL);
  263. printk(KERN_INFO "TOM2: %016llx aka %lldM\n", end, end>>20);
  264. subtract_range(range, RANGE_NUM, 1ULL<<32, end);
  265. }
  266. /*
  267. * add left over mmio range to def node/link ?
  268. * that is tricky, just record range in from start_min to 4G
  269. */
  270. for (j = 0; j < pci_root_num; j++) {
  271. info = &pci_root_info[j];
  272. if (info->node == def_node && info->link == def_link)
  273. break;
  274. }
  275. if (j < pci_root_num) {
  276. info = &pci_root_info[j];
  277. for (i = 0; i < RANGE_NUM; i++) {
  278. if (!range[i].end)
  279. continue;
  280. update_res(info, cap_resource(range[i].start),
  281. cap_resource(range[i].end - 1),
  282. IORESOURCE_MEM, 1);
  283. }
  284. }
  285. for (i = 0; i < pci_root_num; i++) {
  286. int res_num;
  287. int busnum;
  288. info = &pci_root_info[i];
  289. res_num = info->res_num;
  290. busnum = info->bus_min;
  291. printk(KERN_DEBUG "bus: [%02x, %02x] on node %x link %x\n",
  292. info->bus_min, info->bus_max, info->node, info->link);
  293. for (j = 0; j < res_num; j++) {
  294. res = &info->res[j];
  295. printk(KERN_DEBUG "bus: %02x index %x %pR\n",
  296. busnum, j, res);
  297. }
  298. }
  299. return 0;
  300. }
  301. #define ENABLE_CF8_EXT_CFG (1ULL << 46)
  302. static void __cpuinit enable_pci_io_ecs(void *unused)
  303. {
  304. u64 reg;
  305. rdmsrl(MSR_AMD64_NB_CFG, reg);
  306. if (!(reg & ENABLE_CF8_EXT_CFG)) {
  307. reg |= ENABLE_CF8_EXT_CFG;
  308. wrmsrl(MSR_AMD64_NB_CFG, reg);
  309. }
  310. }
  311. static int __cpuinit amd_cpu_notify(struct notifier_block *self,
  312. unsigned long action, void *hcpu)
  313. {
  314. int cpu = (long)hcpu;
  315. switch (action) {
  316. case CPU_ONLINE:
  317. case CPU_ONLINE_FROZEN:
  318. smp_call_function_single(cpu, enable_pci_io_ecs, NULL, 0);
  319. break;
  320. default:
  321. break;
  322. }
  323. return NOTIFY_OK;
  324. }
  325. static struct notifier_block __cpuinitdata amd_cpu_notifier = {
  326. .notifier_call = amd_cpu_notify,
  327. };
  328. static void __init pci_enable_pci_io_ecs(void)
  329. {
  330. #ifdef CONFIG_AMD_NB
  331. unsigned int i, n;
  332. for (n = i = 0; !n && amd_nb_bus_dev_ranges[i].dev_limit; ++i) {
  333. u8 bus = amd_nb_bus_dev_ranges[i].bus;
  334. u8 slot = amd_nb_bus_dev_ranges[i].dev_base;
  335. u8 limit = amd_nb_bus_dev_ranges[i].dev_limit;
  336. for (; slot < limit; ++slot) {
  337. u32 val = read_pci_config(bus, slot, 3, 0);
  338. if (!early_is_amd_nb(val))
  339. continue;
  340. val = read_pci_config(bus, slot, 3, 0x8c);
  341. if (!(val & (ENABLE_CF8_EXT_CFG >> 32))) {
  342. val |= ENABLE_CF8_EXT_CFG >> 32;
  343. write_pci_config(bus, slot, 3, 0x8c, val);
  344. }
  345. ++n;
  346. }
  347. }
  348. pr_info("Extended Config Space enabled on %u nodes\n", n);
  349. #endif
  350. }
  351. static int __init pci_io_ecs_init(void)
  352. {
  353. int cpu;
  354. /* assume all cpus from fam10h have IO ECS */
  355. if (boot_cpu_data.x86 < 0x10)
  356. return 0;
  357. /* Try the PCI method first. */
  358. if (early_pci_allowed())
  359. pci_enable_pci_io_ecs();
  360. register_cpu_notifier(&amd_cpu_notifier);
  361. for_each_online_cpu(cpu)
  362. amd_cpu_notify(&amd_cpu_notifier, (unsigned long)CPU_ONLINE,
  363. (void *)(long)cpu);
  364. pci_probe |= PCI_HAS_IO_ECS;
  365. return 0;
  366. }
  367. static int __init amd_postcore_init(void)
  368. {
  369. if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD)
  370. return 0;
  371. early_fill_mp_bus_info();
  372. pci_io_ecs_init();
  373. return 0;
  374. }
  375. postcore_initcall(amd_postcore_init);