amd_bus.c 14 KB

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