amd_bus.c 14 KB

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