amd_bus.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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. u64 start;
  70. u64 end;
  71. struct range range[RANGE_NUM];
  72. u64 val;
  73. u32 address;
  74. bool found;
  75. if (!early_pci_allowed())
  76. return -1;
  77. found = false;
  78. for (i = 0; i < ARRAY_SIZE(pci_probes); i++) {
  79. u32 id;
  80. u16 device;
  81. u16 vendor;
  82. bus = pci_probes[i].bus;
  83. slot = pci_probes[i].slot;
  84. id = read_pci_config(bus, slot, 0, PCI_VENDOR_ID);
  85. vendor = id & 0xffff;
  86. device = (id>>16) & 0xffff;
  87. if (pci_probes[i].vendor == vendor &&
  88. pci_probes[i].device == device) {
  89. found = true;
  90. break;
  91. }
  92. }
  93. if (!found)
  94. return 0;
  95. pci_root_num = 0;
  96. for (i = 0; i < 4; i++) {
  97. int min_bus;
  98. int max_bus;
  99. reg = read_pci_config(bus, slot, 1, 0xe0 + (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. #ifdef CONFIG_NUMA
  107. for (j = min_bus; j <= max_bus; j++)
  108. set_mp_bus_to_node(j, node);
  109. #endif
  110. link = (reg >> 8) & 0x03;
  111. info = &pci_root_info[pci_root_num];
  112. info->bus_min = min_bus;
  113. info->bus_max = max_bus;
  114. info->node = node;
  115. info->link = link;
  116. sprintf(info->name, "PCI Bus #%02x", min_bus);
  117. pci_root_num++;
  118. }
  119. /* get the default node and link for left over res */
  120. reg = read_pci_config(bus, slot, 0, 0x60);
  121. def_node = (reg >> 8) & 0x07;
  122. reg = read_pci_config(bus, slot, 0, 0x64);
  123. def_link = (reg >> 8) & 0x03;
  124. memset(range, 0, sizeof(range));
  125. range[0].end = 0xffff;
  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. /* find the position */
  137. for (j = 0; j < pci_root_num; j++) {
  138. info = &pci_root_info[j];
  139. if (info->node == node && info->link == link)
  140. break;
  141. }
  142. if (j == pci_root_num)
  143. continue; /* not found */
  144. info = &pci_root_info[j];
  145. printk(KERN_DEBUG "node %d link %d: io port [%llx, %llx]\n",
  146. node, link, start, end);
  147. /* kernel only handle 16 bit only */
  148. if (end > 0xffff)
  149. end = 0xffff;
  150. update_res(info, start, end, IORESOURCE_IO, 1);
  151. subtract_range(range, RANGE_NUM, start, end);
  152. }
  153. /* add left over io port range to def node/link, [0, 0xffff] */
  154. /* find the position */
  155. for (j = 0; j < pci_root_num; j++) {
  156. info = &pci_root_info[j];
  157. if (info->node == def_node && info->link == def_link)
  158. break;
  159. }
  160. if (j < pci_root_num) {
  161. info = &pci_root_info[j];
  162. for (i = 0; i < RANGE_NUM; i++) {
  163. if (!range[i].end)
  164. continue;
  165. update_res(info, range[i].start, range[i].end,
  166. IORESOURCE_IO, 1);
  167. }
  168. }
  169. memset(range, 0, sizeof(range));
  170. /* 0xfd00000000-0xffffffffff for HT */
  171. range[0].end = cap_resource((0xfdULL<<32) - 1);
  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 - 1);
  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, fam10h_mmconf_end);
  185. }
  186. /* mmio resource */
  187. for (i = 0; i < 8; i++) {
  188. reg = read_pci_config(bus, slot, 1, 0x80 + (i << 3));
  189. if (!(reg & 3))
  190. continue;
  191. start = reg & 0xffffff00; /* 39:16 on 31:8*/
  192. start <<= 8;
  193. reg = read_pci_config(bus, slot, 1, 0x84 + (i << 3));
  194. node = reg & 0x07;
  195. link = (reg >> 4) & 0x03;
  196. end = (reg & 0xffffff00);
  197. end <<= 8;
  198. end |= 0xffff;
  199. /* find the position */
  200. for (j = 0; j < pci_root_num; j++) {
  201. info = &pci_root_info[j];
  202. if (info->node == node && info->link == link)
  203. break;
  204. }
  205. if (j == pci_root_num)
  206. continue; /* not found */
  207. info = &pci_root_info[j];
  208. printk(KERN_DEBUG "node %d link %d: mmio [%llx, %llx]",
  209. node, link, start, end);
  210. /*
  211. * some sick allocation would have range overlap with fam10h
  212. * mmconf range, so need to update start and end.
  213. */
  214. if (fam10h_mmconf_end) {
  215. int changed = 0;
  216. u64 endx = 0;
  217. if (start >= fam10h_mmconf_start &&
  218. start <= fam10h_mmconf_end) {
  219. start = fam10h_mmconf_end + 1;
  220. changed = 1;
  221. }
  222. if (end >= fam10h_mmconf_start &&
  223. end <= fam10h_mmconf_end) {
  224. end = fam10h_mmconf_start - 1;
  225. changed = 1;
  226. }
  227. if (start < fam10h_mmconf_start &&
  228. end > fam10h_mmconf_end) {
  229. /* we got a hole */
  230. endx = fam10h_mmconf_start - 1;
  231. update_res(info, start, endx, IORESOURCE_MEM, 0);
  232. subtract_range(range, RANGE_NUM, start, endx);
  233. printk(KERN_CONT " ==> [%llx, %llx]", start, endx);
  234. start = fam10h_mmconf_end + 1;
  235. changed = 1;
  236. }
  237. if (changed) {
  238. if (start <= end) {
  239. printk(KERN_CONT " %s [%llx, %llx]", endx ? "and" : "==>", start, end);
  240. } else {
  241. printk(KERN_CONT "%s\n", endx?"":" ==> none");
  242. continue;
  243. }
  244. }
  245. }
  246. update_res(info, cap_resource(start), cap_resource(end),
  247. IORESOURCE_MEM, 1);
  248. subtract_range(range, RANGE_NUM, start, end);
  249. printk(KERN_CONT "\n");
  250. }
  251. /* need to take out [4G, TOM2) for RAM*/
  252. /* SYS_CFG */
  253. address = MSR_K8_SYSCFG;
  254. rdmsrl(address, val);
  255. /* TOP_MEM2 is enabled? */
  256. if (val & (1<<21)) {
  257. /* TOP_MEM2 */
  258. address = MSR_K8_TOP_MEM2;
  259. rdmsrl(address, val);
  260. end = (val & 0xffffff800000ULL);
  261. printk(KERN_INFO "TOM2: %016llx aka %lldM\n", end, end>>20);
  262. subtract_range(range, RANGE_NUM, 1ULL<<32, end - 1);
  263. }
  264. /*
  265. * add left over mmio range to def node/link ?
  266. * that is tricky, just record range in from start_min to 4G
  267. */
  268. for (j = 0; j < pci_root_num; j++) {
  269. info = &pci_root_info[j];
  270. if (info->node == def_node && info->link == def_link)
  271. break;
  272. }
  273. if (j < pci_root_num) {
  274. info = &pci_root_info[j];
  275. for (i = 0; i < RANGE_NUM; i++) {
  276. if (!range[i].end)
  277. continue;
  278. update_res(info, cap_resource(range[i].start),
  279. cap_resource(range[i].end),
  280. IORESOURCE_MEM, 1);
  281. }
  282. }
  283. for (i = 0; i < pci_root_num; i++) {
  284. int res_num;
  285. int busnum;
  286. info = &pci_root_info[i];
  287. res_num = info->res_num;
  288. busnum = info->bus_min;
  289. printk(KERN_DEBUG "bus: [%02x, %02x] on node %x link %x\n",
  290. info->bus_min, info->bus_max, info->node, info->link);
  291. for (j = 0; j < res_num; j++) {
  292. res = &info->res[j];
  293. printk(KERN_DEBUG "bus: %02x index %x %s: [%llx, %llx]\n",
  294. busnum, j,
  295. (res->flags & IORESOURCE_IO)?"io port":"mmio",
  296. res->start, res->end);
  297. }
  298. }
  299. return 0;
  300. }
  301. #else /* !CONFIG_X86_64 */
  302. static int __init early_fill_mp_bus_info(void) { return 0; }
  303. #endif /* !CONFIG_X86_64 */
  304. /* common 32/64 bit code */
  305. #define ENABLE_CF8_EXT_CFG (1ULL << 46)
  306. static void enable_pci_io_ecs(void *unused)
  307. {
  308. u64 reg;
  309. rdmsrl(MSR_AMD64_NB_CFG, reg);
  310. if (!(reg & ENABLE_CF8_EXT_CFG)) {
  311. reg |= ENABLE_CF8_EXT_CFG;
  312. wrmsrl(MSR_AMD64_NB_CFG, reg);
  313. }
  314. }
  315. static int __cpuinit amd_cpu_notify(struct notifier_block *self,
  316. unsigned long action, void *hcpu)
  317. {
  318. int cpu = (long)hcpu;
  319. switch (action) {
  320. case CPU_ONLINE:
  321. case CPU_ONLINE_FROZEN:
  322. smp_call_function_single(cpu, enable_pci_io_ecs, NULL, 0);
  323. break;
  324. default:
  325. break;
  326. }
  327. return NOTIFY_OK;
  328. }
  329. static struct notifier_block __cpuinitdata amd_cpu_notifier = {
  330. .notifier_call = amd_cpu_notify,
  331. };
  332. static int __init pci_io_ecs_init(void)
  333. {
  334. int cpu;
  335. /* assume all cpus from fam10h have IO ECS */
  336. if (boot_cpu_data.x86 < 0x10)
  337. return 0;
  338. register_cpu_notifier(&amd_cpu_notifier);
  339. for_each_online_cpu(cpu)
  340. amd_cpu_notify(&amd_cpu_notifier, (unsigned long)CPU_ONLINE,
  341. (void *)(long)cpu);
  342. pci_probe |= PCI_HAS_IO_ECS;
  343. return 0;
  344. }
  345. static int __init amd_postcore_init(void)
  346. {
  347. if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD)
  348. return 0;
  349. early_fill_mp_bus_info();
  350. pci_io_ecs_init();
  351. return 0;
  352. }
  353. postcore_initcall(amd_postcore_init);