amd_bus.c 10 KB

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