amd_bus.c 13 KB

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