numa.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. * Copyright (C) 2010 Loongson Inc. & Lemote Inc. &
  3. * Institute of Computing Technology
  4. * Author: Xiang Gao, gaoxiang@ict.ac.cn
  5. * Huacai Chen, chenhc@lemote.com
  6. * Xiaofu Meng, Shuangshuang Zhang
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. */
  13. #include <linux/init.h>
  14. #include <linux/kernel.h>
  15. #include <linux/mm.h>
  16. #include <linux/mmzone.h>
  17. #include <linux/module.h>
  18. #include <linux/nodemask.h>
  19. #include <linux/swap.h>
  20. #include <linux/memblock.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 <linux/irq.h>
  28. #include <asm/bootinfo.h>
  29. #include <asm/mc146818-time.h>
  30. #include <asm/time.h>
  31. #include <asm/wbflush.h>
  32. #include <boot_param.h>
  33. static struct node_data prealloc__node_data[MAX_NUMNODES];
  34. unsigned char __node_distances[MAX_NUMNODES][MAX_NUMNODES];
  35. EXPORT_SYMBOL(__node_distances);
  36. struct node_data *__node_data[MAX_NUMNODES];
  37. EXPORT_SYMBOL(__node_data);
  38. static void enable_lpa(void)
  39. {
  40. unsigned long value;
  41. value = __read_32bit_c0_register($16, 3);
  42. value |= 0x00000080;
  43. __write_32bit_c0_register($16, 3, value);
  44. value = __read_32bit_c0_register($16, 3);
  45. pr_info("CP0_Config3: CP0 16.3 (0x%lx)\n", value);
  46. value = __read_32bit_c0_register($5, 1);
  47. value |= 0x20000000;
  48. __write_32bit_c0_register($5, 1, value);
  49. value = __read_32bit_c0_register($5, 1);
  50. pr_info("CP0_PageGrain: CP0 5.1 (0x%lx)\n", value);
  51. }
  52. static void cpu_node_probe(void)
  53. {
  54. int i;
  55. nodes_clear(node_possible_map);
  56. nodes_clear(node_online_map);
  57. for (i = 0; i < loongson_sysconf.nr_nodes; i++) {
  58. node_set_state(num_online_nodes(), N_POSSIBLE);
  59. node_set_online(num_online_nodes());
  60. }
  61. pr_info("NUMA: Discovered %d cpus on %d nodes\n",
  62. loongson_sysconf.nr_cpus, num_online_nodes());
  63. }
  64. static int __init compute_node_distance(int row, int col)
  65. {
  66. int package_row = row * loongson_sysconf.cores_per_node /
  67. loongson_sysconf.cores_per_package;
  68. int package_col = col * loongson_sysconf.cores_per_node /
  69. loongson_sysconf.cores_per_package;
  70. if (col == row)
  71. return 0;
  72. else if (package_row == package_col)
  73. return 40;
  74. else
  75. return 100;
  76. }
  77. static void __init init_topology_matrix(void)
  78. {
  79. int row, col;
  80. for (row = 0; row < MAX_NUMNODES; row++)
  81. for (col = 0; col < MAX_NUMNODES; col++)
  82. __node_distances[row][col] = -1;
  83. for_each_online_node(row) {
  84. for_each_online_node(col) {
  85. __node_distances[row][col] =
  86. compute_node_distance(row, col);
  87. }
  88. }
  89. }
  90. static unsigned long nid_to_addroffset(unsigned int nid)
  91. {
  92. unsigned long result;
  93. switch (nid) {
  94. case 0:
  95. default:
  96. result = NODE0_ADDRSPACE_OFFSET;
  97. break;
  98. case 1:
  99. result = NODE1_ADDRSPACE_OFFSET;
  100. break;
  101. case 2:
  102. result = NODE2_ADDRSPACE_OFFSET;
  103. break;
  104. case 3:
  105. result = NODE3_ADDRSPACE_OFFSET;
  106. break;
  107. }
  108. return result;
  109. }
  110. static void __init szmem(unsigned int node)
  111. {
  112. u32 i, mem_type;
  113. static unsigned long num_physpages = 0;
  114. u64 node_id, node_psize, start_pfn, end_pfn, mem_start, mem_size;
  115. /* Parse memory information and activate */
  116. for (i = 0; i < loongson_memmap->nr_map; i++) {
  117. node_id = loongson_memmap->map[i].node_id;
  118. if (node_id != node)
  119. continue;
  120. mem_type = loongson_memmap->map[i].mem_type;
  121. mem_size = loongson_memmap->map[i].mem_size;
  122. mem_start = loongson_memmap->map[i].mem_start;
  123. switch (mem_type) {
  124. case SYSTEM_RAM_LOW:
  125. start_pfn = ((node_id << 44) + mem_start) >> PAGE_SHIFT;
  126. node_psize = (mem_size << 20) >> PAGE_SHIFT;
  127. end_pfn = start_pfn + node_psize;
  128. num_physpages += node_psize;
  129. pr_info("Node%d: mem_type:%d, mem_start:0x%llx, mem_size:0x%llx MB\n",
  130. (u32)node_id, mem_type, mem_start, mem_size);
  131. pr_info(" start_pfn:0x%llx, end_pfn:0x%llx, num_physpages:0x%lx\n",
  132. start_pfn, end_pfn, num_physpages);
  133. add_memory_region((node_id << 44) + mem_start,
  134. (u64)mem_size << 20, BOOT_MEM_RAM);
  135. memblock_add_node(PFN_PHYS(start_pfn),
  136. PFN_PHYS(end_pfn - start_pfn), node);
  137. break;
  138. case SYSTEM_RAM_HIGH:
  139. start_pfn = ((node_id << 44) + mem_start) >> PAGE_SHIFT;
  140. node_psize = (mem_size << 20) >> PAGE_SHIFT;
  141. end_pfn = start_pfn + node_psize;
  142. num_physpages += node_psize;
  143. pr_info("Node%d: mem_type:%d, mem_start:0x%llx, mem_size:0x%llx MB\n",
  144. (u32)node_id, mem_type, mem_start, mem_size);
  145. pr_info(" start_pfn:0x%llx, end_pfn:0x%llx, num_physpages:0x%lx\n",
  146. start_pfn, end_pfn, num_physpages);
  147. add_memory_region((node_id << 44) + mem_start,
  148. (u64)mem_size << 20, BOOT_MEM_RAM);
  149. memblock_add_node(PFN_PHYS(start_pfn),
  150. PFN_PHYS(end_pfn - start_pfn), node);
  151. break;
  152. case MEM_RESERVED:
  153. pr_info("Node%d: mem_type:%d, mem_start:0x%llx, mem_size:0x%llx MB\n",
  154. (u32)node_id, mem_type, mem_start, mem_size);
  155. add_memory_region((node_id << 44) + mem_start,
  156. (u64)mem_size << 20, BOOT_MEM_RESERVED);
  157. memblock_reserve(((node_id << 44) + mem_start),
  158. mem_size << 20);
  159. break;
  160. }
  161. }
  162. }
  163. static void __init node_mem_init(unsigned int node)
  164. {
  165. unsigned long bootmap_size;
  166. unsigned long node_addrspace_offset;
  167. unsigned long start_pfn, end_pfn, freepfn;
  168. node_addrspace_offset = nid_to_addroffset(node);
  169. pr_info("Node%d's addrspace_offset is 0x%lx\n",
  170. node, node_addrspace_offset);
  171. get_pfn_range_for_nid(node, &start_pfn, &end_pfn);
  172. freepfn = start_pfn;
  173. if (node == 0)
  174. freepfn = PFN_UP(__pa_symbol(&_end)); /* kernel end address */
  175. pr_info("Node%d: start_pfn=0x%lx, end_pfn=0x%lx, freepfn=0x%lx\n",
  176. node, start_pfn, end_pfn, freepfn);
  177. __node_data[node] = prealloc__node_data + node;
  178. NODE_DATA(node)->bdata = &bootmem_node_data[node];
  179. NODE_DATA(node)->node_start_pfn = start_pfn;
  180. NODE_DATA(node)->node_spanned_pages = end_pfn - start_pfn;
  181. bootmap_size = init_bootmem_node(NODE_DATA(node), freepfn,
  182. start_pfn, end_pfn);
  183. free_bootmem_with_active_regions(node, end_pfn);
  184. if (node == 0) /* used by finalize_initrd() */
  185. max_low_pfn = end_pfn;
  186. /* This is reserved for the kernel and bdata->node_bootmem_map */
  187. reserve_bootmem_node(NODE_DATA(node), start_pfn << PAGE_SHIFT,
  188. ((freepfn - start_pfn) << PAGE_SHIFT) + bootmap_size,
  189. BOOTMEM_DEFAULT);
  190. if (node == 0 && node_end_pfn(0) >= (0xffffffff >> PAGE_SHIFT)) {
  191. /* Reserve 0xfe000000~0xffffffff for RS780E integrated GPU */
  192. reserve_bootmem_node(NODE_DATA(node),
  193. (node_addrspace_offset | 0xfe000000),
  194. 32 << 20, BOOTMEM_DEFAULT);
  195. }
  196. sparse_memory_present_with_active_regions(node);
  197. }
  198. static __init void prom_meminit(void)
  199. {
  200. unsigned int node, cpu, active_cpu = 0;
  201. cpu_node_probe();
  202. init_topology_matrix();
  203. for (node = 0; node < loongson_sysconf.nr_nodes; node++) {
  204. if (node_online(node)) {
  205. szmem(node);
  206. node_mem_init(node);
  207. cpumask_clear(&__node_data[(node)]->cpumask);
  208. }
  209. }
  210. for (cpu = 0; cpu < loongson_sysconf.nr_cpus; cpu++) {
  211. node = cpu / loongson_sysconf.cores_per_node;
  212. if (node >= num_online_nodes())
  213. node = 0;
  214. if (loongson_sysconf.reserved_cpus_mask & (1<<cpu))
  215. continue;
  216. cpumask_set_cpu(active_cpu, &__node_data[(node)]->cpumask);
  217. pr_info("NUMA: set cpumask cpu %d on node %d\n", active_cpu, node);
  218. active_cpu++;
  219. }
  220. }
  221. void __init paging_init(void)
  222. {
  223. unsigned node;
  224. unsigned long zones_size[MAX_NR_ZONES] = {0, };
  225. pagetable_init();
  226. for_each_online_node(node) {
  227. unsigned long start_pfn, end_pfn;
  228. get_pfn_range_for_nid(node, &start_pfn, &end_pfn);
  229. if (end_pfn > max_low_pfn)
  230. max_low_pfn = end_pfn;
  231. }
  232. #ifdef CONFIG_ZONE_DMA32
  233. zones_size[ZONE_DMA32] = MAX_DMA32_PFN;
  234. #endif
  235. zones_size[ZONE_NORMAL] = max_low_pfn;
  236. free_area_init_nodes(zones_size);
  237. }
  238. void __init mem_init(void)
  239. {
  240. high_memory = (void *) __va(get_num_physpages() << PAGE_SHIFT);
  241. free_all_bootmem();
  242. setup_zero_pages(); /* This comes from node 0 */
  243. mem_init_print_info(NULL);
  244. }
  245. /* All PCI device belongs to logical Node-0 */
  246. int pcibus_to_node(struct pci_bus *bus)
  247. {
  248. return 0;
  249. }
  250. EXPORT_SYMBOL(pcibus_to_node);
  251. void __init prom_init_numa_memory(void)
  252. {
  253. enable_lpa();
  254. prom_meminit();
  255. }
  256. EXPORT_SYMBOL(prom_init_numa_memory);