amd_bus.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. #include <linux/init.h>
  2. #include <linux/pci.h>
  3. #include <linux/topology.h>
  4. #include <linux/cpu.h>
  5. #include <linux/range.h>
  6. #include <asm/amd_nb.h>
  7. #include <asm/pci_x86.h>
  8. #include <asm/pci-direct.h>
  9. #include "bus_numa.h"
  10. /*
  11. * This discovers the pcibus <-> node mapping on AMD K8.
  12. * also get peer root bus resource for io,mmio
  13. */
  14. struct pci_hostbridge_probe {
  15. u32 bus;
  16. u32 slot;
  17. u32 vendor;
  18. u32 device;
  19. };
  20. static struct pci_hostbridge_probe pci_probes[] __initdata = {
  21. { 0, 0x18, PCI_VENDOR_ID_AMD, 0x1100 },
  22. { 0, 0x18, PCI_VENDOR_ID_AMD, 0x1200 },
  23. { 0xff, 0, PCI_VENDOR_ID_AMD, 0x1200 },
  24. { 0, 0x18, PCI_VENDOR_ID_AMD, 0x1300 },
  25. };
  26. #define RANGE_NUM 16
  27. static struct pci_root_info __init *find_pci_root_info(int node, int link)
  28. {
  29. struct pci_root_info *info;
  30. /* find the position */
  31. list_for_each_entry(info, &pci_root_infos, list)
  32. if (info->node == node && info->link == link)
  33. return info;
  34. return NULL;
  35. }
  36. static void __init set_mp_bus_range_to_node(int min_bus, int max_bus, int node)
  37. {
  38. #ifdef CONFIG_NUMA
  39. int j;
  40. for (j = min_bus; j <= max_bus; j++)
  41. set_mp_bus_to_node(j, node);
  42. #endif
  43. }
  44. /**
  45. * early_fill_mp_bus_to_node()
  46. * called before pcibios_scan_root and pci_scan_bus
  47. * fills the mp_bus_to_cpumask array based according to the LDT Bus Number
  48. * Registers found in the K8 northbridge
  49. */
  50. static int __init early_fill_mp_bus_info(void)
  51. {
  52. int i;
  53. unsigned bus;
  54. unsigned slot;
  55. int node;
  56. int link;
  57. int def_node;
  58. int def_link;
  59. struct pci_root_info *info;
  60. u32 reg;
  61. u64 start;
  62. u64 end;
  63. struct range range[RANGE_NUM];
  64. u64 val;
  65. u32 address;
  66. bool found;
  67. struct resource fam10h_mmconf_res, *fam10h_mmconf;
  68. u64 fam10h_mmconf_start;
  69. u64 fam10h_mmconf_end;
  70. if (!early_pci_allowed())
  71. return -1;
  72. found = false;
  73. for (i = 0; i < ARRAY_SIZE(pci_probes); i++) {
  74. u32 id;
  75. u16 device;
  76. u16 vendor;
  77. bus = pci_probes[i].bus;
  78. slot = pci_probes[i].slot;
  79. id = read_pci_config(bus, slot, 0, PCI_VENDOR_ID);
  80. vendor = id & 0xffff;
  81. device = (id>>16) & 0xffff;
  82. if (pci_probes[i].vendor == vendor &&
  83. pci_probes[i].device == device) {
  84. found = true;
  85. break;
  86. }
  87. }
  88. if (!found)
  89. return 0;
  90. for (i = 0; i < 4; i++) {
  91. int min_bus;
  92. int max_bus;
  93. reg = read_pci_config(bus, slot, 1, 0xe0 + (i << 2));
  94. /* Check if that register is enabled for bus range */
  95. if ((reg & 7) != 3)
  96. continue;
  97. min_bus = (reg >> 16) & 0xff;
  98. max_bus = (reg >> 24) & 0xff;
  99. node = (reg >> 4) & 0x07;
  100. set_mp_bus_range_to_node(min_bus, max_bus, node);
  101. link = (reg >> 8) & 0x03;
  102. info = alloc_pci_root_info(min_bus, max_bus, node, link);
  103. sprintf(info->name, "PCI Bus #%02x", min_bus);
  104. }
  105. /* get the default node and link for left over res */
  106. reg = read_pci_config(bus, slot, 0, 0x60);
  107. def_node = (reg >> 8) & 0x07;
  108. reg = read_pci_config(bus, slot, 0, 0x64);
  109. def_link = (reg >> 8) & 0x03;
  110. memset(range, 0, sizeof(range));
  111. add_range(range, RANGE_NUM, 0, 0, 0xffff + 1);
  112. /* io port resource */
  113. for (i = 0; i < 4; i++) {
  114. reg = read_pci_config(bus, slot, 1, 0xc0 + (i << 3));
  115. if (!(reg & 3))
  116. continue;
  117. start = reg & 0xfff000;
  118. reg = read_pci_config(bus, slot, 1, 0xc4 + (i << 3));
  119. node = reg & 0x07;
  120. link = (reg >> 4) & 0x03;
  121. end = (reg & 0xfff000) | 0xfff;
  122. info = find_pci_root_info(node, link);
  123. if (!info)
  124. continue; /* not found */
  125. printk(KERN_DEBUG "node %d link %d: io port [%llx, %llx]\n",
  126. node, link, start, end);
  127. /* kernel only handle 16 bit only */
  128. if (end > 0xffff)
  129. end = 0xffff;
  130. update_res(info, start, end, IORESOURCE_IO, 1);
  131. subtract_range(range, RANGE_NUM, start, end + 1);
  132. }
  133. /* add left over io port range to def node/link, [0, 0xffff] */
  134. /* find the position */
  135. info = find_pci_root_info(def_node, def_link);
  136. if (info) {
  137. for (i = 0; i < RANGE_NUM; i++) {
  138. if (!range[i].end)
  139. continue;
  140. update_res(info, range[i].start, range[i].end - 1,
  141. IORESOURCE_IO, 1);
  142. }
  143. }
  144. memset(range, 0, sizeof(range));
  145. /* 0xfd00000000-0xffffffffff for HT */
  146. end = cap_resource((0xfdULL<<32) - 1);
  147. end++;
  148. add_range(range, RANGE_NUM, 0, 0, end);
  149. /* need to take out [0, TOM) for RAM*/
  150. address = MSR_K8_TOP_MEM1;
  151. rdmsrl(address, val);
  152. end = (val & 0xffffff800000ULL);
  153. printk(KERN_INFO "TOM: %016llx aka %lldM\n", end, end>>20);
  154. if (end < (1ULL<<32))
  155. subtract_range(range, RANGE_NUM, 0, end);
  156. /* get mmconfig */
  157. fam10h_mmconf = amd_get_mmconfig_range(&fam10h_mmconf_res);
  158. /* need to take out mmconf range */
  159. if (fam10h_mmconf) {
  160. printk(KERN_DEBUG "Fam 10h mmconf %pR\n", fam10h_mmconf);
  161. fam10h_mmconf_start = fam10h_mmconf->start;
  162. fam10h_mmconf_end = fam10h_mmconf->end;
  163. subtract_range(range, RANGE_NUM, fam10h_mmconf_start,
  164. fam10h_mmconf_end + 1);
  165. } else {
  166. fam10h_mmconf_start = 0;
  167. fam10h_mmconf_end = 0;
  168. }
  169. /* mmio resource */
  170. for (i = 0; i < 8; i++) {
  171. reg = read_pci_config(bus, slot, 1, 0x80 + (i << 3));
  172. if (!(reg & 3))
  173. continue;
  174. start = reg & 0xffffff00; /* 39:16 on 31:8*/
  175. start <<= 8;
  176. reg = read_pci_config(bus, slot, 1, 0x84 + (i << 3));
  177. node = reg & 0x07;
  178. link = (reg >> 4) & 0x03;
  179. end = (reg & 0xffffff00);
  180. end <<= 8;
  181. end |= 0xffff;
  182. info = find_pci_root_info(node, link);
  183. if (!info)
  184. continue;
  185. printk(KERN_DEBUG "node %d link %d: mmio [%llx, %llx]",
  186. node, link, start, end);
  187. /*
  188. * some sick allocation would have range overlap with fam10h
  189. * mmconf range, so need to update start and end.
  190. */
  191. if (fam10h_mmconf_end) {
  192. int changed = 0;
  193. u64 endx = 0;
  194. if (start >= fam10h_mmconf_start &&
  195. start <= fam10h_mmconf_end) {
  196. start = fam10h_mmconf_end + 1;
  197. changed = 1;
  198. }
  199. if (end >= fam10h_mmconf_start &&
  200. end <= fam10h_mmconf_end) {
  201. end = fam10h_mmconf_start - 1;
  202. changed = 1;
  203. }
  204. if (start < fam10h_mmconf_start &&
  205. end > fam10h_mmconf_end) {
  206. /* we got a hole */
  207. endx = fam10h_mmconf_start - 1;
  208. update_res(info, start, endx, IORESOURCE_MEM, 0);
  209. subtract_range(range, RANGE_NUM, start,
  210. endx + 1);
  211. printk(KERN_CONT " ==> [%llx, %llx]", start, endx);
  212. start = fam10h_mmconf_end + 1;
  213. changed = 1;
  214. }
  215. if (changed) {
  216. if (start <= end) {
  217. printk(KERN_CONT " %s [%llx, %llx]", endx ? "and" : "==>", start, end);
  218. } else {
  219. printk(KERN_CONT "%s\n", endx?"":" ==> none");
  220. continue;
  221. }
  222. }
  223. }
  224. update_res(info, cap_resource(start), cap_resource(end),
  225. IORESOURCE_MEM, 1);
  226. subtract_range(range, RANGE_NUM, start, end + 1);
  227. printk(KERN_CONT "\n");
  228. }
  229. /* need to take out [4G, TOM2) for RAM*/
  230. /* SYS_CFG */
  231. address = MSR_K8_SYSCFG;
  232. rdmsrl(address, val);
  233. /* TOP_MEM2 is enabled? */
  234. if (val & (1<<21)) {
  235. /* TOP_MEM2 */
  236. address = MSR_K8_TOP_MEM2;
  237. rdmsrl(address, val);
  238. end = (val & 0xffffff800000ULL);
  239. printk(KERN_INFO "TOM2: %016llx aka %lldM\n", end, end>>20);
  240. subtract_range(range, RANGE_NUM, 1ULL<<32, end);
  241. }
  242. /*
  243. * add left over mmio range to def node/link ?
  244. * that is tricky, just record range in from start_min to 4G
  245. */
  246. info = find_pci_root_info(def_node, def_link);
  247. if (info) {
  248. for (i = 0; i < RANGE_NUM; i++) {
  249. if (!range[i].end)
  250. continue;
  251. update_res(info, cap_resource(range[i].start),
  252. cap_resource(range[i].end - 1),
  253. IORESOURCE_MEM, 1);
  254. }
  255. }
  256. list_for_each_entry(info, &pci_root_infos, list) {
  257. int busnum;
  258. struct pci_root_res *root_res;
  259. busnum = info->bus_min;
  260. printk(KERN_DEBUG "bus: [%02x, %02x] on node %x link %x\n",
  261. info->bus_min, info->bus_max, info->node, info->link);
  262. list_for_each_entry(root_res, &info->resources, list)
  263. printk(KERN_DEBUG "bus: %02x %pR\n",
  264. busnum, &root_res->res);
  265. }
  266. return 0;
  267. }
  268. #define ENABLE_CF8_EXT_CFG (1ULL << 46)
  269. static void __cpuinit enable_pci_io_ecs(void *unused)
  270. {
  271. u64 reg;
  272. rdmsrl(MSR_AMD64_NB_CFG, reg);
  273. if (!(reg & ENABLE_CF8_EXT_CFG)) {
  274. reg |= ENABLE_CF8_EXT_CFG;
  275. wrmsrl(MSR_AMD64_NB_CFG, reg);
  276. }
  277. }
  278. static int __cpuinit amd_cpu_notify(struct notifier_block *self,
  279. unsigned long action, void *hcpu)
  280. {
  281. int cpu = (long)hcpu;
  282. switch (action) {
  283. case CPU_ONLINE:
  284. case CPU_ONLINE_FROZEN:
  285. smp_call_function_single(cpu, enable_pci_io_ecs, NULL, 0);
  286. break;
  287. default:
  288. break;
  289. }
  290. return NOTIFY_OK;
  291. }
  292. static struct notifier_block __cpuinitdata amd_cpu_notifier = {
  293. .notifier_call = amd_cpu_notify,
  294. };
  295. static void __init pci_enable_pci_io_ecs(void)
  296. {
  297. #ifdef CONFIG_AMD_NB
  298. unsigned int i, n;
  299. for (n = i = 0; !n && amd_nb_bus_dev_ranges[i].dev_limit; ++i) {
  300. u8 bus = amd_nb_bus_dev_ranges[i].bus;
  301. u8 slot = amd_nb_bus_dev_ranges[i].dev_base;
  302. u8 limit = amd_nb_bus_dev_ranges[i].dev_limit;
  303. for (; slot < limit; ++slot) {
  304. u32 val = read_pci_config(bus, slot, 3, 0);
  305. if (!early_is_amd_nb(val))
  306. continue;
  307. val = read_pci_config(bus, slot, 3, 0x8c);
  308. if (!(val & (ENABLE_CF8_EXT_CFG >> 32))) {
  309. val |= ENABLE_CF8_EXT_CFG >> 32;
  310. write_pci_config(bus, slot, 3, 0x8c, val);
  311. }
  312. ++n;
  313. }
  314. }
  315. #endif
  316. }
  317. static int __init pci_io_ecs_init(void)
  318. {
  319. int cpu;
  320. /* assume all cpus from fam10h have IO ECS */
  321. if (boot_cpu_data.x86 < 0x10)
  322. return 0;
  323. /* Try the PCI method first. */
  324. if (early_pci_allowed())
  325. pci_enable_pci_io_ecs();
  326. register_cpu_notifier(&amd_cpu_notifier);
  327. for_each_online_cpu(cpu)
  328. amd_cpu_notify(&amd_cpu_notifier, (unsigned long)CPU_ONLINE,
  329. (void *)(long)cpu);
  330. pci_probe |= PCI_HAS_IO_ECS;
  331. return 0;
  332. }
  333. static int __init amd_postcore_init(void)
  334. {
  335. if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD)
  336. return 0;
  337. early_fill_mp_bus_info();
  338. pci_io_ecs_init();
  339. return 0;
  340. }
  341. postcore_initcall(amd_postcore_init);