amd_bus.c 12 KB

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