k8-bus_64.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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 (!pci_root_num)
  58. return;
  59. for (i = 0; i < pci_root_num; i++) {
  60. if (pci_root_info[i].bus_min == b->number)
  61. break;
  62. }
  63. if (i == pci_root_num)
  64. return;
  65. info = &pci_root_info[i];
  66. for (j = 0; j < info->res_num; j++) {
  67. struct resource *res;
  68. struct resource *root;
  69. res = &info->res[j];
  70. b->resource[j] = res;
  71. if (res->flags & IORESOURCE_IO)
  72. root = &ioport_resource;
  73. else
  74. root = &iomem_resource;
  75. insert_resource(root, res);
  76. }
  77. }
  78. #define RANGE_NUM 16
  79. struct res_range {
  80. size_t start;
  81. size_t end;
  82. };
  83. static void __init update_range(struct res_range *range, size_t start,
  84. size_t end)
  85. {
  86. int i;
  87. int j;
  88. for (j = 0; j < RANGE_NUM; j++) {
  89. if (!range[j].end)
  90. continue;
  91. if (start == range[j].start && end < range[j].end) {
  92. range[j].start = end + 1;
  93. break;
  94. } else if (start == range[j].start && end == range[j].end) {
  95. range[j].start = 0;
  96. range[j].end = 0;
  97. break;
  98. } else if (start > range[j].start && end == range[j].end) {
  99. range[j].end = start - 1;
  100. break;
  101. } else if (start > range[j].start && end < range[j].end) {
  102. /* find the new spare */
  103. for (i = 0; i < RANGE_NUM; i++) {
  104. if (range[i].end == 0)
  105. break;
  106. }
  107. if (i < RANGE_NUM) {
  108. range[i].end = range[j].end;
  109. range[i].start = end + 1;
  110. } else {
  111. printk(KERN_ERR "run of slot in ranges\n");
  112. }
  113. range[j].end = start - 1;
  114. break;
  115. }
  116. }
  117. }
  118. static void __init update_res(struct pci_root_info *info, size_t start,
  119. size_t end, unsigned long flags, int merge)
  120. {
  121. int i;
  122. struct resource *res;
  123. if (!merge)
  124. goto addit;
  125. /* try to merge it with old one */
  126. for (i = 0; i < info->res_num; i++) {
  127. res = &info->res[i];
  128. if (res->flags != flags)
  129. continue;
  130. if (res->end + 1 == start) {
  131. res->end = end;
  132. return;
  133. } else if (end + 1 == res->start) {
  134. res->start = start;
  135. return;
  136. }
  137. }
  138. addit:
  139. /* need to add that */
  140. if (info->res_num >= RES_NUM)
  141. return;
  142. res = &info->res[info->res_num];
  143. res->name = info->name;
  144. res->flags = flags;
  145. res->start = start;
  146. res->end = end;
  147. res->child = NULL;
  148. info->res_num++;
  149. }
  150. struct pci_hostbridge_probe {
  151. u32 bus;
  152. u32 slot;
  153. u32 vendor;
  154. u32 device;
  155. };
  156. static struct pci_hostbridge_probe pci_probes[] __initdata = {
  157. { 0, 0x18, PCI_VENDOR_ID_AMD, 0x1100 },
  158. { 0, 0x18, PCI_VENDOR_ID_AMD, 0x1200 },
  159. { 0xff, 0, PCI_VENDOR_ID_AMD, 0x1200 },
  160. { 0, 0x18, PCI_VENDOR_ID_AMD, 0x1300 },
  161. };
  162. /**
  163. * early_fill_mp_bus_to_node()
  164. * called before pcibios_scan_root and pci_scan_bus
  165. * fills the mp_bus_to_cpumask array based according to the LDT Bus Number
  166. * Registers found in the K8 northbridge
  167. */
  168. static int __init early_fill_mp_bus_info(void)
  169. {
  170. int i;
  171. int j;
  172. unsigned bus;
  173. unsigned slot;
  174. int found;
  175. int node;
  176. int link;
  177. int def_node;
  178. int def_link;
  179. struct pci_root_info *info;
  180. u32 reg;
  181. struct resource *res;
  182. size_t start;
  183. size_t end;
  184. struct res_range range[RANGE_NUM];
  185. u64 val;
  186. u32 address;
  187. #ifdef CONFIG_NUMA
  188. for (i = 0; i < BUS_NR; i++)
  189. mp_bus_to_node[i] = -1;
  190. #endif
  191. if (!early_pci_allowed())
  192. return -1;
  193. found = 0;
  194. for (i = 0; i < ARRAY_SIZE(pci_probes); i++) {
  195. u32 id;
  196. u16 device;
  197. u16 vendor;
  198. bus = pci_probes[i].bus;
  199. slot = pci_probes[i].slot;
  200. id = read_pci_config(bus, slot, 0, PCI_VENDOR_ID);
  201. vendor = id & 0xffff;
  202. device = (id>>16) & 0xffff;
  203. if (pci_probes[i].vendor == vendor &&
  204. pci_probes[i].device == device) {
  205. found = 1;
  206. break;
  207. }
  208. }
  209. if (!found)
  210. return 0;
  211. pci_root_num = 0;
  212. for (i = 0; i < 4; i++) {
  213. int min_bus;
  214. int max_bus;
  215. reg = read_pci_config(bus, slot, 1, 0xe0 + (i << 2));
  216. /* Check if that register is enabled for bus range */
  217. if ((reg & 7) != 3)
  218. continue;
  219. min_bus = (reg >> 16) & 0xff;
  220. max_bus = (reg >> 24) & 0xff;
  221. node = (reg >> 4) & 0x07;
  222. #ifdef CONFIG_NUMA
  223. for (j = min_bus; j <= max_bus; j++)
  224. mp_bus_to_node[j] = (unsigned char) node;
  225. #endif
  226. link = (reg >> 8) & 0x03;
  227. info = &pci_root_info[pci_root_num];
  228. info->bus_min = min_bus;
  229. info->bus_max = max_bus;
  230. info->node = node;
  231. info->link = link;
  232. sprintf(info->name, "PCI Bus #%02x", min_bus);
  233. pci_root_num++;
  234. }
  235. /* get the default node and link for left over res */
  236. reg = read_pci_config(bus, slot, 0, 0x60);
  237. def_node = (reg >> 8) & 0x07;
  238. reg = read_pci_config(bus, slot, 0, 0x64);
  239. def_link = (reg >> 8) & 0x03;
  240. memset(range, 0, sizeof(range));
  241. range[0].end = 0xffff;
  242. /* io port resource */
  243. for (i = 0; i < 4; i++) {
  244. reg = read_pci_config(bus, slot, 1, 0xc0 + (i << 3));
  245. if (!(reg & 3))
  246. continue;
  247. start = reg & 0xfff000;
  248. reg = read_pci_config(bus, slot, 1, 0xc4 + (i << 3));
  249. node = reg & 0x07;
  250. link = (reg >> 4) & 0x03;
  251. end = (reg & 0xfff000) | 0xfff;
  252. /* find the position */
  253. for (j = 0; j < pci_root_num; j++) {
  254. info = &pci_root_info[j];
  255. if (info->node == node && info->link == link)
  256. break;
  257. }
  258. if (j == pci_root_num)
  259. continue; /* not found */
  260. info = &pci_root_info[j];
  261. update_res(info, start, end, IORESOURCE_IO, 0);
  262. update_range(range, start, end);
  263. }
  264. /* add left over io port range to def node/link, [0, 0xffff] */
  265. /* find the position */
  266. for (j = 0; j < pci_root_num; j++) {
  267. info = &pci_root_info[j];
  268. if (info->node == def_node && info->link == def_link)
  269. break;
  270. }
  271. if (j < pci_root_num) {
  272. info = &pci_root_info[j];
  273. for (i = 0; i < RANGE_NUM; i++) {
  274. if (!range[i].end)
  275. continue;
  276. update_res(info, range[i].start, range[i].end,
  277. IORESOURCE_IO, 1);
  278. }
  279. }
  280. memset(range, 0, sizeof(range));
  281. /* 0xfd00000000-0xffffffffff for HT */
  282. /* 0xfc00000000-0xfcffffffff for Family 10h mmconfig*/
  283. range[0].end = 0xfbffffffffULL;
  284. /* need to take out [0, TOM) for RAM*/
  285. address = MSR_K8_TOP_MEM1;
  286. rdmsrl(address, val);
  287. end = (val & 0xffffff8000000ULL);
  288. printk(KERN_INFO "TOM: %016lx aka %ldM\n", end, end>>20);
  289. if (end < (1ULL<<32))
  290. update_range(range, 0, end - 1);
  291. /* mmio resource */
  292. for (i = 0; i < 8; i++) {
  293. reg = read_pci_config(bus, slot, 1, 0x80 + (i << 3));
  294. if (!(reg & 3))
  295. continue;
  296. start = reg & 0xffffff00; /* 39:16 on 31:8*/
  297. start <<= 8;
  298. reg = read_pci_config(bus, slot, 1, 0x84 + (i << 3));
  299. node = reg & 0x07;
  300. link = (reg >> 4) & 0x03;
  301. end = (reg & 0xffffff00);
  302. end <<= 8;
  303. end |= 0xffff;
  304. /* find the position */
  305. for (j = 0; j < pci_root_num; j++) {
  306. info = &pci_root_info[j];
  307. if (info->node == node && info->link == link)
  308. break;
  309. }
  310. if (j == pci_root_num)
  311. continue; /* not found */
  312. info = &pci_root_info[j];
  313. update_res(info, start, end, IORESOURCE_MEM, 0);
  314. update_range(range, start, end);
  315. }
  316. /* need to take out [4G, TOM2) for RAM*/
  317. /* SYS_CFG */
  318. address = MSR_K8_SYSCFG;
  319. rdmsrl(address, val);
  320. /* TOP_MEM2 is enabled? */
  321. if (val & (1<<21)) {
  322. /* TOP_MEM2 */
  323. address = MSR_K8_TOP_MEM2;
  324. rdmsrl(address, val);
  325. end = (val & 0xffffff8000000ULL);
  326. printk(KERN_INFO "TOM2: %016lx aka %ldM\n", end, end>>20);
  327. update_range(range, 1ULL<<32, end - 1);
  328. }
  329. /*
  330. * add left over mmio range to def node/link ?
  331. * that is tricky, just record range in from start_min to 4G
  332. */
  333. for (j = 0; j < pci_root_num; j++) {
  334. info = &pci_root_info[j];
  335. if (info->node == def_node && info->link == def_link)
  336. break;
  337. }
  338. if (j < pci_root_num) {
  339. info = &pci_root_info[j];
  340. for (i = 0; i < RANGE_NUM; i++) {
  341. if (!range[i].end)
  342. continue;
  343. update_res(info, range[i].start, range[i].end,
  344. IORESOURCE_MEM, 1);
  345. }
  346. }
  347. #ifdef CONFIG_NUMA
  348. for (i = 0; i < BUS_NR; i++) {
  349. node = mp_bus_to_node[i];
  350. if (node >= 0)
  351. printk(KERN_DEBUG "bus: %02x to node: %02x\n", i, node);
  352. }
  353. #endif
  354. for (i = 0; i < pci_root_num; i++) {
  355. int res_num;
  356. int busnum;
  357. info = &pci_root_info[i];
  358. res_num = info->res_num;
  359. busnum = info->bus_min;
  360. printk(KERN_DEBUG "bus: [%02x,%02x] on node %x link %x\n",
  361. info->bus_min, info->bus_max, info->node, info->link);
  362. for (j = 0; j < res_num; j++) {
  363. res = &info->res[j];
  364. printk(KERN_DEBUG "bus: %02x index %x %s: [%llx, %llx]\n",
  365. busnum, j,
  366. (res->flags & IORESOURCE_IO)?"io port":"mmio",
  367. res->start, res->end);
  368. }
  369. }
  370. return 0;
  371. }
  372. postcore_initcall(early_fill_mp_bus_info);