amd_bus.c 13 KB

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