ip27-memory.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 2000, 05 by Ralf Baechle (ralf@linux-mips.org)
  7. * Copyright (C) 2000 by Silicon Graphics, Inc.
  8. * Copyright (C) 2004 by Christoph Hellwig
  9. *
  10. * On SGI IP27 the ARC memory configuration data is completely bogus but
  11. * alternate easier to use mechanisms are available.
  12. */
  13. #include <linux/init.h>
  14. #include <linux/kernel.h>
  15. #include <linux/memblock.h>
  16. #include <linux/mm.h>
  17. #include <linux/mmzone.h>
  18. #include <linux/module.h>
  19. #include <linux/nodemask.h>
  20. #include <linux/swap.h>
  21. #include <linux/bootmem.h>
  22. #include <linux/pfn.h>
  23. #include <linux/highmem.h>
  24. #include <asm/page.h>
  25. #include <asm/pgalloc.h>
  26. #include <asm/sections.h>
  27. #include <asm/sn/arch.h>
  28. #include <asm/sn/hub.h>
  29. #include <asm/sn/klconfig.h>
  30. #include <asm/sn/sn_private.h>
  31. #define SLOT_PFNSHIFT (SLOT_SHIFT - PAGE_SHIFT)
  32. #define PFN_NASIDSHFT (NASID_SHFT - PAGE_SHIFT)
  33. struct node_data *__node_data[MAX_COMPACT_NODES];
  34. EXPORT_SYMBOL(__node_data);
  35. static int fine_mode;
  36. static int is_fine_dirmode(void)
  37. {
  38. return ((LOCAL_HUB_L(NI_STATUS_REV_ID) & NSRI_REGIONSIZE_MASK) >> NSRI_REGIONSIZE_SHFT) & REGIONSIZE_FINE;
  39. }
  40. static hubreg_t get_region(cnodeid_t cnode)
  41. {
  42. if (fine_mode)
  43. return COMPACT_TO_NASID_NODEID(cnode) >> NASID_TO_FINEREG_SHFT;
  44. else
  45. return COMPACT_TO_NASID_NODEID(cnode) >> NASID_TO_COARSEREG_SHFT;
  46. }
  47. static hubreg_t region_mask;
  48. static void gen_region_mask(hubreg_t *region_mask)
  49. {
  50. cnodeid_t cnode;
  51. (*region_mask) = 0;
  52. for_each_online_node(cnode) {
  53. (*region_mask) |= 1ULL << get_region(cnode);
  54. }
  55. }
  56. #define rou_rflag rou_flags
  57. static int router_distance;
  58. static void router_recurse(klrou_t *router_a, klrou_t *router_b, int depth)
  59. {
  60. klrou_t *router;
  61. lboard_t *brd;
  62. int port;
  63. if (router_a->rou_rflag == 1)
  64. return;
  65. if (depth >= router_distance)
  66. return;
  67. router_a->rou_rflag = 1;
  68. for (port = 1; port <= MAX_ROUTER_PORTS; port++) {
  69. if (router_a->rou_port[port].port_nasid == INVALID_NASID)
  70. continue;
  71. brd = (lboard_t *)NODE_OFFSET_TO_K0(
  72. router_a->rou_port[port].port_nasid,
  73. router_a->rou_port[port].port_offset);
  74. if (brd->brd_type == KLTYPE_ROUTER) {
  75. router = (klrou_t *)NODE_OFFSET_TO_K0(NASID_GET(brd), brd->brd_compts[0]);
  76. if (router == router_b) {
  77. if (depth < router_distance)
  78. router_distance = depth;
  79. }
  80. else
  81. router_recurse(router, router_b, depth + 1);
  82. }
  83. }
  84. router_a->rou_rflag = 0;
  85. }
  86. unsigned char __node_distances[MAX_COMPACT_NODES][MAX_COMPACT_NODES];
  87. EXPORT_SYMBOL(__node_distances);
  88. static int __init compute_node_distance(nasid_t nasid_a, nasid_t nasid_b)
  89. {
  90. klrou_t *router, *router_a = NULL, *router_b = NULL;
  91. lboard_t *brd, *dest_brd;
  92. cnodeid_t cnode;
  93. nasid_t nasid;
  94. int port;
  95. /* Figure out which routers nodes in question are connected to */
  96. for_each_online_node(cnode) {
  97. nasid = COMPACT_TO_NASID_NODEID(cnode);
  98. if (nasid == -1) continue;
  99. brd = find_lboard_class((lboard_t *)KL_CONFIG_INFO(nasid),
  100. KLTYPE_ROUTER);
  101. if (!brd)
  102. continue;
  103. do {
  104. if (brd->brd_flags & DUPLICATE_BOARD)
  105. continue;
  106. router = (klrou_t *)NODE_OFFSET_TO_K0(NASID_GET(brd), brd->brd_compts[0]);
  107. router->rou_rflag = 0;
  108. for (port = 1; port <= MAX_ROUTER_PORTS; port++) {
  109. if (router->rou_port[port].port_nasid == INVALID_NASID)
  110. continue;
  111. dest_brd = (lboard_t *)NODE_OFFSET_TO_K0(
  112. router->rou_port[port].port_nasid,
  113. router->rou_port[port].port_offset);
  114. if (dest_brd->brd_type == KLTYPE_IP27) {
  115. if (dest_brd->brd_nasid == nasid_a)
  116. router_a = router;
  117. if (dest_brd->brd_nasid == nasid_b)
  118. router_b = router;
  119. }
  120. }
  121. } while ((brd = find_lboard_class(KLCF_NEXT(brd), KLTYPE_ROUTER)));
  122. }
  123. if (router_a == NULL) {
  124. printk("node_distance: router_a NULL\n");
  125. return -1;
  126. }
  127. if (router_b == NULL) {
  128. printk("node_distance: router_b NULL\n");
  129. return -1;
  130. }
  131. if (nasid_a == nasid_b)
  132. return 0;
  133. if (router_a == router_b)
  134. return 1;
  135. router_distance = 100;
  136. router_recurse(router_a, router_b, 2);
  137. return router_distance;
  138. }
  139. static void __init init_topology_matrix(void)
  140. {
  141. nasid_t nasid, nasid2;
  142. cnodeid_t row, col;
  143. for (row = 0; row < MAX_COMPACT_NODES; row++)
  144. for (col = 0; col < MAX_COMPACT_NODES; col++)
  145. __node_distances[row][col] = -1;
  146. for_each_online_node(row) {
  147. nasid = COMPACT_TO_NASID_NODEID(row);
  148. for_each_online_node(col) {
  149. nasid2 = COMPACT_TO_NASID_NODEID(col);
  150. __node_distances[row][col] =
  151. compute_node_distance(nasid, nasid2);
  152. }
  153. }
  154. }
  155. static void __init dump_topology(void)
  156. {
  157. nasid_t nasid;
  158. cnodeid_t cnode;
  159. lboard_t *brd, *dest_brd;
  160. int port;
  161. int router_num = 0;
  162. klrou_t *router;
  163. cnodeid_t row, col;
  164. printk("************** Topology ********************\n");
  165. printk(" ");
  166. for_each_online_node(col)
  167. printk("%02d ", col);
  168. printk("\n");
  169. for_each_online_node(row) {
  170. printk("%02d ", row);
  171. for_each_online_node(col)
  172. printk("%2d ", node_distance(row, col));
  173. printk("\n");
  174. }
  175. for_each_online_node(cnode) {
  176. nasid = COMPACT_TO_NASID_NODEID(cnode);
  177. if (nasid == -1) continue;
  178. brd = find_lboard_class((lboard_t *)KL_CONFIG_INFO(nasid),
  179. KLTYPE_ROUTER);
  180. if (!brd)
  181. continue;
  182. do {
  183. if (brd->brd_flags & DUPLICATE_BOARD)
  184. continue;
  185. printk("Router %d:", router_num);
  186. router_num++;
  187. router = (klrou_t *)NODE_OFFSET_TO_K0(NASID_GET(brd), brd->brd_compts[0]);
  188. for (port = 1; port <= MAX_ROUTER_PORTS; port++) {
  189. if (router->rou_port[port].port_nasid == INVALID_NASID)
  190. continue;
  191. dest_brd = (lboard_t *)NODE_OFFSET_TO_K0(
  192. router->rou_port[port].port_nasid,
  193. router->rou_port[port].port_offset);
  194. if (dest_brd->brd_type == KLTYPE_IP27)
  195. printk(" %d", dest_brd->brd_nasid);
  196. if (dest_brd->brd_type == KLTYPE_ROUTER)
  197. printk(" r");
  198. }
  199. printk("\n");
  200. } while ( (brd = find_lboard_class(KLCF_NEXT(brd), KLTYPE_ROUTER)) );
  201. }
  202. }
  203. static unsigned long __init slot_getbasepfn(cnodeid_t cnode, int slot)
  204. {
  205. nasid_t nasid = COMPACT_TO_NASID_NODEID(cnode);
  206. return ((unsigned long)nasid << PFN_NASIDSHFT) | (slot << SLOT_PFNSHIFT);
  207. }
  208. static unsigned long __init slot_psize_compute(cnodeid_t node, int slot)
  209. {
  210. nasid_t nasid;
  211. lboard_t *brd;
  212. klmembnk_t *banks;
  213. unsigned long size;
  214. nasid = COMPACT_TO_NASID_NODEID(node);
  215. /* Find the node board */
  216. brd = find_lboard((lboard_t *)KL_CONFIG_INFO(nasid), KLTYPE_IP27);
  217. if (!brd)
  218. return 0;
  219. /* Get the memory bank structure */
  220. banks = (klmembnk_t *) find_first_component(brd, KLSTRUCT_MEMBNK);
  221. if (!banks)
  222. return 0;
  223. /* Size in _Megabytes_ */
  224. size = (unsigned long)banks->membnk_bnksz[slot/4];
  225. /* hack for 128 dimm banks */
  226. if (size <= 128) {
  227. if (slot % 4 == 0) {
  228. size <<= 20; /* size in bytes */
  229. return size >> PAGE_SHIFT;
  230. } else
  231. return 0;
  232. } else {
  233. size /= 4;
  234. size <<= 20;
  235. return size >> PAGE_SHIFT;
  236. }
  237. }
  238. static void __init mlreset(void)
  239. {
  240. int i;
  241. master_nasid = get_nasid();
  242. fine_mode = is_fine_dirmode();
  243. /*
  244. * Probe for all CPUs - this creates the cpumask and sets up the
  245. * mapping tables. We need to do this as early as possible.
  246. */
  247. #ifdef CONFIG_SMP
  248. cpu_node_probe();
  249. #endif
  250. init_topology_matrix();
  251. dump_topology();
  252. gen_region_mask(&region_mask);
  253. setup_replication_mask();
  254. /*
  255. * Set all nodes' calias sizes to 8k
  256. */
  257. for_each_online_node(i) {
  258. nasid_t nasid;
  259. nasid = COMPACT_TO_NASID_NODEID(i);
  260. /*
  261. * Always have node 0 in the region mask, otherwise
  262. * CALIAS accesses get exceptions since the hub
  263. * thinks it is a node 0 address.
  264. */
  265. REMOTE_HUB_S(nasid, PI_REGION_PRESENT, (region_mask | 1));
  266. #ifdef CONFIG_REPLICATE_EXHANDLERS
  267. REMOTE_HUB_S(nasid, PI_CALIAS_SIZE, PI_CALIAS_SIZE_8K);
  268. #else
  269. REMOTE_HUB_S(nasid, PI_CALIAS_SIZE, PI_CALIAS_SIZE_0);
  270. #endif
  271. #ifdef LATER
  272. /*
  273. * Set up all hubs to have a big window pointing at
  274. * widget 0. Memory mode, widget 0, offset 0
  275. */
  276. REMOTE_HUB_S(nasid, IIO_ITTE(SWIN0_BIGWIN),
  277. ((HUB_PIO_MAP_TO_MEM << IIO_ITTE_IOSP_SHIFT) |
  278. (0 << IIO_ITTE_WIDGET_SHIFT)));
  279. #endif
  280. }
  281. }
  282. static void __init szmem(void)
  283. {
  284. unsigned long slot_psize, slot0sz = 0, nodebytes; /* Hack to detect problem configs */
  285. int slot;
  286. cnodeid_t node;
  287. for_each_online_node(node) {
  288. nodebytes = 0;
  289. for (slot = 0; slot < MAX_MEM_SLOTS; slot++) {
  290. slot_psize = slot_psize_compute(node, slot);
  291. if (slot == 0)
  292. slot0sz = slot_psize;
  293. /*
  294. * We need to refine the hack when we have replicated
  295. * kernel text.
  296. */
  297. nodebytes += (1LL << SLOT_SHIFT);
  298. if (!slot_psize)
  299. continue;
  300. if ((nodebytes >> PAGE_SHIFT) * (sizeof(struct page)) >
  301. (slot0sz << PAGE_SHIFT)) {
  302. printk("Ignoring slot %d onwards on node %d\n",
  303. slot, node);
  304. slot = MAX_MEM_SLOTS;
  305. continue;
  306. }
  307. memblock_add_node(PFN_PHYS(slot_getbasepfn(node, slot)),
  308. PFN_PHYS(slot_psize), node);
  309. }
  310. }
  311. }
  312. static void __init node_mem_init(cnodeid_t node)
  313. {
  314. unsigned long slot_firstpfn = slot_getbasepfn(node, 0);
  315. unsigned long slot_freepfn = node_getfirstfree(node);
  316. unsigned long bootmap_size;
  317. unsigned long start_pfn, end_pfn;
  318. get_pfn_range_for_nid(node, &start_pfn, &end_pfn);
  319. /*
  320. * Allocate the node data structures on the node first.
  321. */
  322. __node_data[node] = __va(slot_freepfn << PAGE_SHIFT);
  323. memset(__node_data[node], 0, PAGE_SIZE);
  324. NODE_DATA(node)->bdata = &bootmem_node_data[node];
  325. NODE_DATA(node)->node_start_pfn = start_pfn;
  326. NODE_DATA(node)->node_spanned_pages = end_pfn - start_pfn;
  327. cpumask_clear(&hub_data(node)->h_cpus);
  328. slot_freepfn += PFN_UP(sizeof(struct pglist_data) +
  329. sizeof(struct hub_data));
  330. bootmap_size = init_bootmem_node(NODE_DATA(node), slot_freepfn,
  331. start_pfn, end_pfn);
  332. free_bootmem_with_active_regions(node, end_pfn);
  333. reserve_bootmem_node(NODE_DATA(node), slot_firstpfn << PAGE_SHIFT,
  334. ((slot_freepfn - slot_firstpfn) << PAGE_SHIFT) + bootmap_size,
  335. BOOTMEM_DEFAULT);
  336. sparse_memory_present_with_active_regions(node);
  337. }
  338. /*
  339. * A node with nothing. We use it to avoid any special casing in
  340. * cpumask_of_node
  341. */
  342. static struct node_data null_node = {
  343. .hub = {
  344. .h_cpus = CPU_MASK_NONE
  345. }
  346. };
  347. /*
  348. * Currently, the intranode memory hole support assumes that each slot
  349. * contains at least 32 MBytes of memory. We assume all bootmem data
  350. * fits on the first slot.
  351. */
  352. void __init prom_meminit(void)
  353. {
  354. cnodeid_t node;
  355. mlreset();
  356. szmem();
  357. for (node = 0; node < MAX_COMPACT_NODES; node++) {
  358. if (node_online(node)) {
  359. node_mem_init(node);
  360. continue;
  361. }
  362. __node_data[node] = &null_node;
  363. }
  364. }
  365. void __init prom_free_prom_memory(void)
  366. {
  367. /* We got nothing to free here ... */
  368. }
  369. extern void setup_zero_pages(void);
  370. void __init paging_init(void)
  371. {
  372. unsigned long zones_size[MAX_NR_ZONES] = {0, };
  373. unsigned node;
  374. pagetable_init();
  375. for_each_online_node(node) {
  376. unsigned long start_pfn, end_pfn;
  377. get_pfn_range_for_nid(node, &start_pfn, &end_pfn);
  378. if (end_pfn > max_low_pfn)
  379. max_low_pfn = end_pfn;
  380. }
  381. zones_size[ZONE_NORMAL] = max_low_pfn;
  382. free_area_init_nodes(zones_size);
  383. }
  384. void __init mem_init(void)
  385. {
  386. high_memory = (void *) __va(get_num_physpages() << PAGE_SHIFT);
  387. free_all_bootmem();
  388. setup_zero_pages(); /* This comes from node 0 */
  389. mem_init_print_info(NULL);
  390. }