amd_bus.c 9.4 KB

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