numa.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Author: Xiang Gao <gaoxiang@loongson.cn>
  4. * Huacai Chen <chenhuacai@loongson.cn>
  5. *
  6. * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
  7. */
  8. #include <linux/init.h>
  9. #include <linux/kernel.h>
  10. #include <linux/mm.h>
  11. #include <linux/mmzone.h>
  12. #include <linux/export.h>
  13. #include <linux/nodemask.h>
  14. #include <linux/swap.h>
  15. #include <linux/memblock.h>
  16. #include <linux/pfn.h>
  17. #include <linux/acpi.h>
  18. #include <linux/efi.h>
  19. #include <linux/irq.h>
  20. #include <linux/pci.h>
  21. #include <asm/bootinfo.h>
  22. #include <asm/loongson.h>
  23. #include <asm/numa.h>
  24. #include <asm/page.h>
  25. #include <asm/pgalloc.h>
  26. #include <asm/sections.h>
  27. #include <asm/time.h>
  28. int numa_off;
  29. unsigned char node_distances[MAX_NUMNODES][MAX_NUMNODES];
  30. EXPORT_SYMBOL(node_distances);
  31. static struct numa_meminfo numa_meminfo;
  32. cpumask_t cpus_on_node[MAX_NUMNODES];
  33. cpumask_t phys_cpus_on_node[MAX_NUMNODES];
  34. EXPORT_SYMBOL(cpus_on_node);
  35. /*
  36. * apicid, cpu, node mappings
  37. */
  38. s16 __cpuid_to_node[CONFIG_NR_CPUS] = {
  39. [0 ... CONFIG_NR_CPUS - 1] = NUMA_NO_NODE
  40. };
  41. EXPORT_SYMBOL(__cpuid_to_node);
  42. nodemask_t numa_nodes_parsed __initdata;
  43. #ifdef CONFIG_HAVE_SETUP_PER_CPU_AREA
  44. unsigned long __per_cpu_offset[NR_CPUS] __read_mostly;
  45. EXPORT_SYMBOL(__per_cpu_offset);
  46. static int __init pcpu_cpu_to_node(int cpu)
  47. {
  48. return early_cpu_to_node(cpu);
  49. }
  50. static int __init pcpu_cpu_distance(unsigned int from, unsigned int to)
  51. {
  52. if (early_cpu_to_node(from) == early_cpu_to_node(to))
  53. return LOCAL_DISTANCE;
  54. else
  55. return REMOTE_DISTANCE;
  56. }
  57. void __init pcpu_populate_pte(unsigned long addr)
  58. {
  59. populate_kernel_pte(addr);
  60. }
  61. void __init setup_per_cpu_areas(void)
  62. {
  63. unsigned long delta;
  64. unsigned int cpu;
  65. int rc = -EINVAL;
  66. if (pcpu_chosen_fc == PCPU_FC_AUTO) {
  67. if (nr_node_ids >= 8)
  68. pcpu_chosen_fc = PCPU_FC_PAGE;
  69. else
  70. pcpu_chosen_fc = PCPU_FC_EMBED;
  71. }
  72. /*
  73. * Always reserve area for module percpu variables. That's
  74. * what the legacy allocator did.
  75. */
  76. if (pcpu_chosen_fc != PCPU_FC_PAGE) {
  77. rc = pcpu_embed_first_chunk(PERCPU_MODULE_RESERVE,
  78. PERCPU_DYNAMIC_RESERVE, PMD_SIZE,
  79. pcpu_cpu_distance, pcpu_cpu_to_node);
  80. if (rc < 0)
  81. pr_warn("%s allocator failed (%d), falling back to page size\n",
  82. pcpu_fc_names[pcpu_chosen_fc], rc);
  83. }
  84. if (rc < 0)
  85. rc = pcpu_page_first_chunk(PERCPU_MODULE_RESERVE, pcpu_cpu_to_node);
  86. if (rc < 0)
  87. panic("cannot initialize percpu area (err=%d)", rc);
  88. delta = (unsigned long)pcpu_base_addr - (unsigned long)__per_cpu_start;
  89. for_each_possible_cpu(cpu)
  90. __per_cpu_offset[cpu] = delta + pcpu_unit_offsets[cpu];
  91. }
  92. #endif
  93. /*
  94. * Get nodeid by logical cpu number.
  95. * __cpuid_to_node maps phyical cpu id to node, so we
  96. * should use cpu_logical_map(cpu) to index it.
  97. *
  98. * This routine is only used in early phase during
  99. * booting, after setup_per_cpu_areas calling and numa_node
  100. * initialization, cpu_to_node will be used instead.
  101. */
  102. int early_cpu_to_node(int cpu)
  103. {
  104. int physid = cpu_logical_map(cpu);
  105. if (physid < 0)
  106. return NUMA_NO_NODE;
  107. return __cpuid_to_node[physid];
  108. }
  109. void __init early_numa_add_cpu(int cpuid, s16 node)
  110. {
  111. int cpu = __cpu_number_map[cpuid];
  112. if (cpu < 0)
  113. return;
  114. cpumask_set_cpu(cpu, &cpus_on_node[node]);
  115. cpumask_set_cpu(cpuid, &phys_cpus_on_node[node]);
  116. }
  117. void numa_add_cpu(unsigned int cpu)
  118. {
  119. int nid = cpu_to_node(cpu);
  120. cpumask_set_cpu(cpu, &cpus_on_node[nid]);
  121. }
  122. void numa_remove_cpu(unsigned int cpu)
  123. {
  124. int nid = cpu_to_node(cpu);
  125. cpumask_clear_cpu(cpu, &cpus_on_node[nid]);
  126. }
  127. static int __init numa_add_memblk_to(int nid, u64 start, u64 end,
  128. struct numa_meminfo *mi)
  129. {
  130. /* ignore zero length blks */
  131. if (start == end)
  132. return 0;
  133. /* whine about and ignore invalid blks */
  134. if (start > end || nid < 0 || nid >= MAX_NUMNODES) {
  135. pr_warn("NUMA: Warning: invalid memblk node %d [mem %#010Lx-%#010Lx]\n",
  136. nid, start, end - 1);
  137. return 0;
  138. }
  139. if (mi->nr_blks >= NR_NODE_MEMBLKS) {
  140. pr_err("NUMA: too many memblk ranges\n");
  141. return -EINVAL;
  142. }
  143. mi->blk[mi->nr_blks].start = PFN_ALIGN(start);
  144. mi->blk[mi->nr_blks].end = PFN_ALIGN(end - PAGE_SIZE + 1);
  145. mi->blk[mi->nr_blks].nid = nid;
  146. mi->nr_blks++;
  147. return 0;
  148. }
  149. /**
  150. * numa_add_memblk - Add one numa_memblk to numa_meminfo
  151. * @nid: NUMA node ID of the new memblk
  152. * @start: Start address of the new memblk
  153. * @end: End address of the new memblk
  154. *
  155. * Add a new memblk to the default numa_meminfo.
  156. *
  157. * RETURNS:
  158. * 0 on success, -errno on failure.
  159. */
  160. int __init numa_add_memblk(int nid, u64 start, u64 end)
  161. {
  162. return numa_add_memblk_to(nid, start, end, &numa_meminfo);
  163. }
  164. static void __init node_mem_init(unsigned int node)
  165. {
  166. unsigned long start_pfn, end_pfn;
  167. unsigned long node_addrspace_offset;
  168. node_addrspace_offset = nid_to_addrbase(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. pr_info("Node%d: start_pfn=0x%lx, end_pfn=0x%lx\n",
  173. node, start_pfn, end_pfn);
  174. alloc_node_data(node);
  175. }
  176. #ifdef CONFIG_ACPI_NUMA
  177. static void __init add_node_intersection(u32 node, u64 start, u64 size, u32 type)
  178. {
  179. static unsigned long num_physpages;
  180. num_physpages += (size >> PAGE_SHIFT);
  181. pr_info("Node%d: mem_type:%d, mem_start:0x%llx, mem_size:0x%llx Bytes\n",
  182. node, type, start, size);
  183. pr_info(" start_pfn:0x%llx, end_pfn:0x%llx, num_physpages:0x%lx\n",
  184. start >> PAGE_SHIFT, (start + size) >> PAGE_SHIFT, num_physpages);
  185. memblock_set_node(start, size, &memblock.memory, node);
  186. }
  187. /*
  188. * add_numamem_region
  189. *
  190. * Add a uasable memory region described by BIOS. The
  191. * routine gets each intersection between BIOS's region
  192. * and node's region, and adds them into node's memblock
  193. * pool.
  194. *
  195. */
  196. static void __init add_numamem_region(u64 start, u64 end, u32 type)
  197. {
  198. u32 i;
  199. u64 ofs = start;
  200. if (start >= end) {
  201. pr_debug("Invalid region: %016llx-%016llx\n", start, end);
  202. return;
  203. }
  204. for (i = 0; i < numa_meminfo.nr_blks; i++) {
  205. struct numa_memblk *mb = &numa_meminfo.blk[i];
  206. if (ofs > mb->end)
  207. continue;
  208. if (end > mb->end) {
  209. add_node_intersection(mb->nid, ofs, mb->end - ofs, type);
  210. ofs = mb->end;
  211. } else {
  212. add_node_intersection(mb->nid, ofs, end - ofs, type);
  213. break;
  214. }
  215. }
  216. }
  217. static void __init init_node_memblock(void)
  218. {
  219. u32 mem_type;
  220. u64 mem_end, mem_start, mem_size;
  221. efi_memory_desc_t *md;
  222. /* Parse memory information and activate */
  223. for_each_efi_memory_desc(md) {
  224. mem_type = md->type;
  225. mem_start = md->phys_addr;
  226. mem_size = md->num_pages << EFI_PAGE_SHIFT;
  227. mem_end = mem_start + mem_size;
  228. switch (mem_type) {
  229. case EFI_LOADER_CODE:
  230. case EFI_LOADER_DATA:
  231. case EFI_BOOT_SERVICES_CODE:
  232. case EFI_BOOT_SERVICES_DATA:
  233. case EFI_PERSISTENT_MEMORY:
  234. case EFI_CONVENTIONAL_MEMORY:
  235. add_numamem_region(mem_start, mem_end, mem_type);
  236. break;
  237. case EFI_PAL_CODE:
  238. case EFI_UNUSABLE_MEMORY:
  239. case EFI_ACPI_RECLAIM_MEMORY:
  240. add_numamem_region(mem_start, mem_end, mem_type);
  241. fallthrough;
  242. case EFI_RESERVED_TYPE:
  243. case EFI_RUNTIME_SERVICES_CODE:
  244. case EFI_RUNTIME_SERVICES_DATA:
  245. case EFI_MEMORY_MAPPED_IO:
  246. case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
  247. pr_info("Resvd: mem_type:%d, mem_start:0x%llx, mem_size:0x%llx Bytes\n",
  248. mem_type, mem_start, mem_size);
  249. break;
  250. }
  251. }
  252. }
  253. static void __init numa_default_distance(void)
  254. {
  255. int row, col;
  256. for (row = 0; row < MAX_NUMNODES; row++)
  257. for (col = 0; col < MAX_NUMNODES; col++) {
  258. if (col == row)
  259. node_distances[row][col] = LOCAL_DISTANCE;
  260. else
  261. /* We assume that one node per package here!
  262. *
  263. * A SLIT should be used for multiple nodes
  264. * per package to override default setting.
  265. */
  266. node_distances[row][col] = REMOTE_DISTANCE;
  267. }
  268. }
  269. /*
  270. * fake_numa_init() - For Non-ACPI systems
  271. * Return: 0 on success, -errno on failure.
  272. */
  273. static int __init fake_numa_init(void)
  274. {
  275. phys_addr_t start = memblock_start_of_DRAM();
  276. phys_addr_t end = memblock_end_of_DRAM() - 1;
  277. node_set(0, numa_nodes_parsed);
  278. pr_info("Faking a node at [mem %pap-%pap]\n", &start, &end);
  279. return numa_add_memblk(0, start, end + 1);
  280. }
  281. int __init init_numa_memory(void)
  282. {
  283. int i;
  284. int ret;
  285. int node;
  286. for (i = 0; i < NR_CPUS; i++)
  287. set_cpuid_to_node(i, NUMA_NO_NODE);
  288. numa_default_distance();
  289. nodes_clear(numa_nodes_parsed);
  290. nodes_clear(node_possible_map);
  291. nodes_clear(node_online_map);
  292. memset(&numa_meminfo, 0, sizeof(numa_meminfo));
  293. /* Parse SRAT and SLIT if provided by firmware. */
  294. ret = acpi_disabled ? fake_numa_init() : acpi_numa_init();
  295. if (ret < 0)
  296. return ret;
  297. node_possible_map = numa_nodes_parsed;
  298. if (WARN_ON(nodes_empty(node_possible_map)))
  299. return -EINVAL;
  300. init_node_memblock();
  301. if (!memblock_validate_numa_coverage(SZ_1M))
  302. return -EINVAL;
  303. for_each_node_mask(node, node_possible_map) {
  304. node_mem_init(node);
  305. node_set_online(node);
  306. }
  307. max_low_pfn = PHYS_PFN(memblock_end_of_DRAM());
  308. setup_nr_node_ids();
  309. loongson_sysconf.nr_nodes = nr_node_ids;
  310. loongson_sysconf.cores_per_node = cpumask_weight(&phys_cpus_on_node[0]);
  311. return 0;
  312. }
  313. #endif
  314. void __init paging_init(void)
  315. {
  316. unsigned int node;
  317. unsigned long zones_size[MAX_NR_ZONES] = {0, };
  318. for_each_online_node(node) {
  319. unsigned long start_pfn, end_pfn;
  320. get_pfn_range_for_nid(node, &start_pfn, &end_pfn);
  321. if (end_pfn > max_low_pfn)
  322. max_low_pfn = end_pfn;
  323. }
  324. #ifdef CONFIG_ZONE_DMA32
  325. zones_size[ZONE_DMA32] = MAX_DMA32_PFN;
  326. #endif
  327. zones_size[ZONE_NORMAL] = max_low_pfn;
  328. free_area_init(zones_size);
  329. }
  330. void __init mem_init(void)
  331. {
  332. high_memory = (void *) __va(max_low_pfn << PAGE_SHIFT);
  333. memblock_free_all();
  334. }
  335. int pcibus_to_node(struct pci_bus *bus)
  336. {
  337. return dev_to_node(&bus->dev);
  338. }
  339. EXPORT_SYMBOL(pcibus_to_node);