amd_bus.c 9.6 KB

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