k8-bus_64.c 12 KB

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