amd_bus.c 9.7 KB

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