amd_bus.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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. }
  104. /* get the default node and link for left over res */
  105. reg = read_pci_config(bus, slot, 0, 0x60);
  106. def_node = (reg >> 8) & 0x07;
  107. reg = read_pci_config(bus, slot, 0, 0x64);
  108. def_link = (reg >> 8) & 0x03;
  109. memset(range, 0, sizeof(range));
  110. add_range(range, RANGE_NUM, 0, 0, 0xffff + 1);
  111. /* io port resource */
  112. for (i = 0; i < 4; i++) {
  113. reg = read_pci_config(bus, slot, 1, 0xc0 + (i << 3));
  114. if (!(reg & 3))
  115. continue;
  116. start = reg & 0xfff000;
  117. reg = read_pci_config(bus, slot, 1, 0xc4 + (i << 3));
  118. node = reg & 0x07;
  119. link = (reg >> 4) & 0x03;
  120. end = (reg & 0xfff000) | 0xfff;
  121. info = find_pci_root_info(node, link);
  122. if (!info)
  123. continue; /* not found */
  124. printk(KERN_DEBUG "node %d link %d: io port [%llx, %llx]\n",
  125. node, link, start, end);
  126. /* kernel only handle 16 bit only */
  127. if (end > 0xffff)
  128. end = 0xffff;
  129. update_res(info, start, end, IORESOURCE_IO, 1);
  130. subtract_range(range, RANGE_NUM, start, end + 1);
  131. }
  132. /* add left over io port range to def node/link, [0, 0xffff] */
  133. /* find the position */
  134. info = find_pci_root_info(def_node, def_link);
  135. if (info) {
  136. for (i = 0; i < RANGE_NUM; i++) {
  137. if (!range[i].end)
  138. continue;
  139. update_res(info, range[i].start, range[i].end - 1,
  140. IORESOURCE_IO, 1);
  141. }
  142. }
  143. memset(range, 0, sizeof(range));
  144. /* 0xfd00000000-0xffffffffff for HT */
  145. end = cap_resource((0xfdULL<<32) - 1);
  146. end++;
  147. add_range(range, RANGE_NUM, 0, 0, end);
  148. /* need to take out [0, TOM) for RAM*/
  149. address = MSR_K8_TOP_MEM1;
  150. rdmsrl(address, val);
  151. end = (val & 0xffffff800000ULL);
  152. printk(KERN_INFO "TOM: %016llx aka %lldM\n", end, end>>20);
  153. if (end < (1ULL<<32))
  154. subtract_range(range, RANGE_NUM, 0, end);
  155. /* get mmconfig */
  156. fam10h_mmconf = amd_get_mmconfig_range(&fam10h_mmconf_res);
  157. /* need to take out mmconf range */
  158. if (fam10h_mmconf) {
  159. printk(KERN_DEBUG "Fam 10h mmconf %pR\n", fam10h_mmconf);
  160. fam10h_mmconf_start = fam10h_mmconf->start;
  161. fam10h_mmconf_end = fam10h_mmconf->end;
  162. subtract_range(range, RANGE_NUM, fam10h_mmconf_start,
  163. fam10h_mmconf_end + 1);
  164. } else {
  165. fam10h_mmconf_start = 0;
  166. fam10h_mmconf_end = 0;
  167. }
  168. /* mmio resource */
  169. for (i = 0; i < 8; i++) {
  170. reg = read_pci_config(bus, slot, 1, 0x80 + (i << 3));
  171. if (!(reg & 3))
  172. continue;
  173. start = reg & 0xffffff00; /* 39:16 on 31:8*/
  174. start <<= 8;
  175. reg = read_pci_config(bus, slot, 1, 0x84 + (i << 3));
  176. node = reg & 0x07;
  177. link = (reg >> 4) & 0x03;
  178. end = (reg & 0xffffff00);
  179. end <<= 8;
  180. end |= 0xffff;
  181. info = find_pci_root_info(node, link);
  182. if (!info)
  183. continue;
  184. printk(KERN_DEBUG "node %d link %d: mmio [%llx, %llx]",
  185. node, link, start, end);
  186. /*
  187. * some sick allocation would have range overlap with fam10h
  188. * mmconf range, so need to update start and end.
  189. */
  190. if (fam10h_mmconf_end) {
  191. int changed = 0;
  192. u64 endx = 0;
  193. if (start >= fam10h_mmconf_start &&
  194. start <= fam10h_mmconf_end) {
  195. start = fam10h_mmconf_end + 1;
  196. changed = 1;
  197. }
  198. if (end >= fam10h_mmconf_start &&
  199. end <= fam10h_mmconf_end) {
  200. end = fam10h_mmconf_start - 1;
  201. changed = 1;
  202. }
  203. if (start < fam10h_mmconf_start &&
  204. end > fam10h_mmconf_end) {
  205. /* we got a hole */
  206. endx = fam10h_mmconf_start - 1;
  207. update_res(info, start, endx, IORESOURCE_MEM, 0);
  208. subtract_range(range, RANGE_NUM, start,
  209. endx + 1);
  210. printk(KERN_CONT " ==> [%llx, %llx]", start, endx);
  211. start = fam10h_mmconf_end + 1;
  212. changed = 1;
  213. }
  214. if (changed) {
  215. if (start <= end) {
  216. printk(KERN_CONT " %s [%llx, %llx]", endx ? "and" : "==>", start, end);
  217. } else {
  218. printk(KERN_CONT "%s\n", endx?"":" ==> none");
  219. continue;
  220. }
  221. }
  222. }
  223. update_res(info, cap_resource(start), cap_resource(end),
  224. IORESOURCE_MEM, 1);
  225. subtract_range(range, RANGE_NUM, start, end + 1);
  226. printk(KERN_CONT "\n");
  227. }
  228. /* need to take out [4G, TOM2) for RAM*/
  229. /* SYS_CFG */
  230. address = MSR_K8_SYSCFG;
  231. rdmsrl(address, val);
  232. /* TOP_MEM2 is enabled? */
  233. if (val & (1<<21)) {
  234. /* TOP_MEM2 */
  235. address = MSR_K8_TOP_MEM2;
  236. rdmsrl(address, val);
  237. end = (val & 0xffffff800000ULL);
  238. printk(KERN_INFO "TOM2: %016llx aka %lldM\n", end, end>>20);
  239. subtract_range(range, RANGE_NUM, 1ULL<<32, end);
  240. }
  241. /*
  242. * add left over mmio range to def node/link ?
  243. * that is tricky, just record range in from start_min to 4G
  244. */
  245. info = find_pci_root_info(def_node, def_link);
  246. if (info) {
  247. for (i = 0; i < RANGE_NUM; i++) {
  248. if (!range[i].end)
  249. continue;
  250. update_res(info, cap_resource(range[i].start),
  251. cap_resource(range[i].end - 1),
  252. IORESOURCE_MEM, 1);
  253. }
  254. }
  255. list_for_each_entry(info, &pci_root_infos, list) {
  256. int busnum;
  257. struct pci_root_res *root_res;
  258. busnum = info->busn.start;
  259. printk(KERN_DEBUG "bus: %pR on node %x link %x\n",
  260. &info->busn, info->node, info->link);
  261. list_for_each_entry(root_res, &info->resources, list)
  262. printk(KERN_DEBUG "bus: %02x %pR\n",
  263. busnum, &root_res->res);
  264. }
  265. return 0;
  266. }
  267. #define ENABLE_CF8_EXT_CFG (1ULL << 46)
  268. static void __cpuinit enable_pci_io_ecs(void *unused)
  269. {
  270. u64 reg;
  271. rdmsrl(MSR_AMD64_NB_CFG, reg);
  272. if (!(reg & ENABLE_CF8_EXT_CFG)) {
  273. reg |= ENABLE_CF8_EXT_CFG;
  274. wrmsrl(MSR_AMD64_NB_CFG, reg);
  275. }
  276. }
  277. static int __cpuinit amd_cpu_notify(struct notifier_block *self,
  278. unsigned long action, void *hcpu)
  279. {
  280. int cpu = (long)hcpu;
  281. switch (action) {
  282. case CPU_ONLINE:
  283. case CPU_ONLINE_FROZEN:
  284. smp_call_function_single(cpu, enable_pci_io_ecs, NULL, 0);
  285. break;
  286. default:
  287. break;
  288. }
  289. return NOTIFY_OK;
  290. }
  291. static struct notifier_block __cpuinitdata amd_cpu_notifier = {
  292. .notifier_call = amd_cpu_notify,
  293. };
  294. static void __init pci_enable_pci_io_ecs(void)
  295. {
  296. #ifdef CONFIG_AMD_NB
  297. unsigned int i, n;
  298. for (n = i = 0; !n && amd_nb_bus_dev_ranges[i].dev_limit; ++i) {
  299. u8 bus = amd_nb_bus_dev_ranges[i].bus;
  300. u8 slot = amd_nb_bus_dev_ranges[i].dev_base;
  301. u8 limit = amd_nb_bus_dev_ranges[i].dev_limit;
  302. for (; slot < limit; ++slot) {
  303. u32 val = read_pci_config(bus, slot, 3, 0);
  304. if (!early_is_amd_nb(val))
  305. continue;
  306. val = read_pci_config(bus, slot, 3, 0x8c);
  307. if (!(val & (ENABLE_CF8_EXT_CFG >> 32))) {
  308. val |= ENABLE_CF8_EXT_CFG >> 32;
  309. write_pci_config(bus, slot, 3, 0x8c, val);
  310. }
  311. ++n;
  312. }
  313. }
  314. #endif
  315. }
  316. static int __init pci_io_ecs_init(void)
  317. {
  318. int cpu;
  319. /* assume all cpus from fam10h have IO ECS */
  320. if (boot_cpu_data.x86 < 0x10)
  321. return 0;
  322. /* Try the PCI method first. */
  323. if (early_pci_allowed())
  324. pci_enable_pci_io_ecs();
  325. register_cpu_notifier(&amd_cpu_notifier);
  326. for_each_online_cpu(cpu)
  327. amd_cpu_notify(&amd_cpu_notifier, (unsigned long)CPU_ONLINE,
  328. (void *)(long)cpu);
  329. pci_probe |= PCI_HAS_IO_ECS;
  330. return 0;
  331. }
  332. static int __init amd_postcore_init(void)
  333. {
  334. if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD)
  335. return 0;
  336. early_fill_mp_bus_info();
  337. pci_io_ecs_init();
  338. return 0;
  339. }
  340. postcore_initcall(amd_postcore_init);