numa.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Author: Xiang Gao <[email protected]>
  4. * Huacai Chen <[email protected]>
  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. struct pglist_data *node_data[MAX_NUMNODES];
  30. unsigned char node_distances[MAX_NUMNODES][MAX_NUMNODES];
  31. EXPORT_SYMBOL(node_data);
  32. EXPORT_SYMBOL(node_distances);
  33. static struct numa_meminfo numa_meminfo;
  34. cpumask_t cpus_on_node[MAX_NUMNODES];
  35. cpumask_t phys_cpus_on_node[MAX_NUMNODES];
  36. EXPORT_SYMBOL(cpus_on_node);
  37. /*
  38. * apicid, cpu, node mappings
  39. */
  40. s16 __cpuid_to_node[CONFIG_NR_CPUS] = {
  41. [0 ... CONFIG_NR_CPUS - 1] = NUMA_NO_NODE
  42. };
  43. EXPORT_SYMBOL(__cpuid_to_node);
  44. nodemask_t numa_nodes_parsed __initdata;
  45. #ifdef CONFIG_HAVE_SETUP_PER_CPU_AREA
  46. unsigned long __per_cpu_offset[NR_CPUS] __read_mostly;
  47. EXPORT_SYMBOL(__per_cpu_offset);
  48. static int __init pcpu_cpu_to_node(int cpu)
  49. {
  50. return early_cpu_to_node(cpu);
  51. }
  52. static int __init pcpu_cpu_distance(unsigned int from, unsigned int to)
  53. {
  54. if (early_cpu_to_node(from) == early_cpu_to_node(to))
  55. return LOCAL_DISTANCE;
  56. else
  57. return REMOTE_DISTANCE;
  58. }
  59. void __init pcpu_populate_pte(unsigned long addr)
  60. {
  61. pgd_t *pgd = pgd_offset_k(addr);
  62. p4d_t *p4d = p4d_offset(pgd, addr);
  63. pud_t *pud;
  64. pmd_t *pmd;
  65. if (p4d_none(*p4d)) {
  66. pud_t *new;
  67. new = memblock_alloc(PAGE_SIZE, PAGE_SIZE);
  68. pgd_populate(&init_mm, pgd, new);
  69. #ifndef __PAGETABLE_PUD_FOLDED
  70. pud_init((unsigned long)new, (unsigned long)invalid_pmd_table);
  71. #endif
  72. }
  73. pud = pud_offset(p4d, addr);
  74. if (pud_none(*pud)) {
  75. pmd_t *new;
  76. new = memblock_alloc(PAGE_SIZE, PAGE_SIZE);
  77. pud_populate(&init_mm, pud, new);
  78. #ifndef __PAGETABLE_PMD_FOLDED
  79. pmd_init((unsigned long)new, (unsigned long)invalid_pte_table);
  80. #endif
  81. }
  82. pmd = pmd_offset(pud, addr);
  83. if (!pmd_present(*pmd)) {
  84. pte_t *new;
  85. new = memblock_alloc(PAGE_SIZE, PAGE_SIZE);
  86. pmd_populate_kernel(&init_mm, pmd, new);
  87. }
  88. }
  89. void __init setup_per_cpu_areas(void)
  90. {
  91. unsigned long delta;
  92. unsigned int cpu;
  93. int rc = -EINVAL;
  94. if (pcpu_chosen_fc == PCPU_FC_AUTO) {
  95. if (nr_node_ids >= 8)
  96. pcpu_chosen_fc = PCPU_FC_PAGE;
  97. else
  98. pcpu_chosen_fc = PCPU_FC_EMBED;
  99. }
  100. /*
  101. * Always reserve area for module percpu variables. That's
  102. * what the legacy allocator did.
  103. */
  104. if (pcpu_chosen_fc != PCPU_FC_PAGE) {
  105. rc = pcpu_embed_first_chunk(PERCPU_MODULE_RESERVE,
  106. PERCPU_DYNAMIC_RESERVE, PMD_SIZE,
  107. pcpu_cpu_distance, pcpu_cpu_to_node);
  108. if (rc < 0)
  109. pr_warn("%s allocator failed (%d), falling back to page size\n",
  110. pcpu_fc_names[pcpu_chosen_fc], rc);
  111. }
  112. if (rc < 0)
  113. rc = pcpu_page_first_chunk(PERCPU_MODULE_RESERVE, pcpu_cpu_to_node);
  114. if (rc < 0)
  115. panic("cannot initialize percpu area (err=%d)", rc);
  116. delta = (unsigned long)pcpu_base_addr - (unsigned long)__per_cpu_start;
  117. for_each_possible_cpu(cpu)
  118. __per_cpu_offset[cpu] = delta + pcpu_unit_offsets[cpu];
  119. }
  120. #endif
  121. /*
  122. * Get nodeid by logical cpu number.
  123. * __cpuid_to_node maps phyical cpu id to node, so we
  124. * should use cpu_logical_map(cpu) to index it.
  125. *
  126. * This routine is only used in early phase during
  127. * booting, after setup_per_cpu_areas calling and numa_node
  128. * initialization, cpu_to_node will be used instead.
  129. */
  130. int early_cpu_to_node(int cpu)
  131. {
  132. int physid = cpu_logical_map(cpu);
  133. if (physid < 0)
  134. return NUMA_NO_NODE;
  135. return __cpuid_to_node[physid];
  136. }
  137. void __init early_numa_add_cpu(int cpuid, s16 node)
  138. {
  139. int cpu = __cpu_number_map[cpuid];
  140. if (cpu < 0)
  141. return;
  142. cpumask_set_cpu(cpu, &cpus_on_node[node]);
  143. cpumask_set_cpu(cpuid, &phys_cpus_on_node[node]);
  144. }
  145. void numa_add_cpu(unsigned int cpu)
  146. {
  147. int nid = cpu_to_node(cpu);
  148. cpumask_set_cpu(cpu, &cpus_on_node[nid]);
  149. }
  150. void numa_remove_cpu(unsigned int cpu)
  151. {
  152. int nid = cpu_to_node(cpu);
  153. cpumask_clear_cpu(cpu, &cpus_on_node[nid]);
  154. }
  155. static int __init numa_add_memblk_to(int nid, u64 start, u64 end,
  156. struct numa_meminfo *mi)
  157. {
  158. /* ignore zero length blks */
  159. if (start == end)
  160. return 0;
  161. /* whine about and ignore invalid blks */
  162. if (start > end || nid < 0 || nid >= MAX_NUMNODES) {
  163. pr_warn("NUMA: Warning: invalid memblk node %d [mem %#010Lx-%#010Lx]\n",
  164. nid, start, end - 1);
  165. return 0;
  166. }
  167. if (mi->nr_blks >= NR_NODE_MEMBLKS) {
  168. pr_err("NUMA: too many memblk ranges\n");
  169. return -EINVAL;
  170. }
  171. mi->blk[mi->nr_blks].start = PFN_ALIGN(start);
  172. mi->blk[mi->nr_blks].end = PFN_ALIGN(end - PAGE_SIZE + 1);
  173. mi->blk[mi->nr_blks].nid = nid;
  174. mi->nr_blks++;
  175. return 0;
  176. }
  177. /**
  178. * numa_add_memblk - Add one numa_memblk to numa_meminfo
  179. * @nid: NUMA node ID of the new memblk
  180. * @start: Start address of the new memblk
  181. * @end: End address of the new memblk
  182. *
  183. * Add a new memblk to the default numa_meminfo.
  184. *
  185. * RETURNS:
  186. * 0 on success, -errno on failure.
  187. */
  188. int __init numa_add_memblk(int nid, u64 start, u64 end)
  189. {
  190. return numa_add_memblk_to(nid, start, end, &numa_meminfo);
  191. }
  192. static void __init alloc_node_data(int nid)
  193. {
  194. void *nd;
  195. unsigned long nd_pa;
  196. size_t nd_sz = roundup(sizeof(pg_data_t), PAGE_SIZE);
  197. nd_pa = memblock_phys_alloc_try_nid(nd_sz, SMP_CACHE_BYTES, nid);
  198. if (!nd_pa) {
  199. pr_err("Cannot find %zu Byte for node_data (initial node: %d)\n", nd_sz, nid);
  200. return;
  201. }
  202. nd = __va(nd_pa);
  203. node_data[nid] = nd;
  204. memset(nd, 0, sizeof(pg_data_t));
  205. }
  206. static void __init node_mem_init(unsigned int node)
  207. {
  208. unsigned long start_pfn, end_pfn;
  209. unsigned long node_addrspace_offset;
  210. node_addrspace_offset = nid_to_addrbase(node);
  211. pr_info("Node%d's addrspace_offset is 0x%lx\n",
  212. node, node_addrspace_offset);
  213. get_pfn_range_for_nid(node, &start_pfn, &end_pfn);
  214. pr_info("Node%d: start_pfn=0x%lx, end_pfn=0x%lx\n",
  215. node, start_pfn, end_pfn);
  216. alloc_node_data(node);
  217. }
  218. #ifdef CONFIG_ACPI_NUMA
  219. /*
  220. * Sanity check to catch more bad NUMA configurations (they are amazingly
  221. * common). Make sure the nodes cover all memory.
  222. */
  223. static bool __init numa_meminfo_cover_memory(const struct numa_meminfo *mi)
  224. {
  225. int i;
  226. u64 numaram, biosram;
  227. numaram = 0;
  228. for (i = 0; i < mi->nr_blks; i++) {
  229. u64 s = mi->blk[i].start >> PAGE_SHIFT;
  230. u64 e = mi->blk[i].end >> PAGE_SHIFT;
  231. numaram += e - s;
  232. numaram -= __absent_pages_in_range(mi->blk[i].nid, s, e);
  233. if ((s64)numaram < 0)
  234. numaram = 0;
  235. }
  236. max_pfn = max_low_pfn;
  237. biosram = max_pfn - absent_pages_in_range(0, max_pfn);
  238. BUG_ON((s64)(biosram - numaram) >= (1 << (20 - PAGE_SHIFT)));
  239. return true;
  240. }
  241. static void __init add_node_intersection(u32 node, u64 start, u64 size, u32 type)
  242. {
  243. static unsigned long num_physpages;
  244. num_physpages += (size >> PAGE_SHIFT);
  245. pr_info("Node%d: mem_type:%d, mem_start:0x%llx, mem_size:0x%llx Bytes\n",
  246. node, type, start, size);
  247. pr_info(" start_pfn:0x%llx, end_pfn:0x%llx, num_physpages:0x%lx\n",
  248. start >> PAGE_SHIFT, (start + size) >> PAGE_SHIFT, num_physpages);
  249. memblock_set_node(start, size, &memblock.memory, node);
  250. }
  251. /*
  252. * add_numamem_region
  253. *
  254. * Add a uasable memory region described by BIOS. The
  255. * routine gets each intersection between BIOS's region
  256. * and node's region, and adds them into node's memblock
  257. * pool.
  258. *
  259. */
  260. static void __init add_numamem_region(u64 start, u64 end, u32 type)
  261. {
  262. u32 i;
  263. u64 ofs = start;
  264. if (start >= end) {
  265. pr_debug("Invalid region: %016llx-%016llx\n", start, end);
  266. return;
  267. }
  268. for (i = 0; i < numa_meminfo.nr_blks; i++) {
  269. struct numa_memblk *mb = &numa_meminfo.blk[i];
  270. if (ofs > mb->end)
  271. continue;
  272. if (end > mb->end) {
  273. add_node_intersection(mb->nid, ofs, mb->end - ofs, type);
  274. ofs = mb->end;
  275. } else {
  276. add_node_intersection(mb->nid, ofs, end - ofs, type);
  277. break;
  278. }
  279. }
  280. }
  281. static void __init init_node_memblock(void)
  282. {
  283. u32 mem_type;
  284. u64 mem_end, mem_start, mem_size;
  285. efi_memory_desc_t *md;
  286. /* Parse memory information and activate */
  287. for_each_efi_memory_desc(md) {
  288. mem_type = md->type;
  289. mem_start = md->phys_addr;
  290. mem_size = md->num_pages << EFI_PAGE_SHIFT;
  291. mem_end = mem_start + mem_size;
  292. switch (mem_type) {
  293. case EFI_LOADER_CODE:
  294. case EFI_LOADER_DATA:
  295. case EFI_BOOT_SERVICES_CODE:
  296. case EFI_BOOT_SERVICES_DATA:
  297. case EFI_PERSISTENT_MEMORY:
  298. case EFI_CONVENTIONAL_MEMORY:
  299. add_numamem_region(mem_start, mem_end, mem_type);
  300. break;
  301. case EFI_PAL_CODE:
  302. case EFI_UNUSABLE_MEMORY:
  303. case EFI_ACPI_RECLAIM_MEMORY:
  304. add_numamem_region(mem_start, mem_end, mem_type);
  305. fallthrough;
  306. case EFI_RESERVED_TYPE:
  307. case EFI_RUNTIME_SERVICES_CODE:
  308. case EFI_RUNTIME_SERVICES_DATA:
  309. case EFI_MEMORY_MAPPED_IO:
  310. case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
  311. pr_info("Resvd: mem_type:%d, mem_start:0x%llx, mem_size:0x%llx Bytes\n",
  312. mem_type, mem_start, mem_size);
  313. break;
  314. }
  315. }
  316. }
  317. static void __init numa_default_distance(void)
  318. {
  319. int row, col;
  320. for (row = 0; row < MAX_NUMNODES; row++)
  321. for (col = 0; col < MAX_NUMNODES; col++) {
  322. if (col == row)
  323. node_distances[row][col] = LOCAL_DISTANCE;
  324. else
  325. /* We assume that one node per package here!
  326. *
  327. * A SLIT should be used for multiple nodes
  328. * per package to override default setting.
  329. */
  330. node_distances[row][col] = REMOTE_DISTANCE;
  331. }
  332. }
  333. int __init init_numa_memory(void)
  334. {
  335. int i;
  336. int ret;
  337. int node;
  338. for (i = 0; i < NR_CPUS; i++)
  339. set_cpuid_to_node(i, NUMA_NO_NODE);
  340. numa_default_distance();
  341. nodes_clear(numa_nodes_parsed);
  342. nodes_clear(node_possible_map);
  343. nodes_clear(node_online_map);
  344. memset(&numa_meminfo, 0, sizeof(numa_meminfo));
  345. /* Parse SRAT and SLIT if provided by firmware. */
  346. ret = acpi_numa_init();
  347. if (ret < 0)
  348. return ret;
  349. node_possible_map = numa_nodes_parsed;
  350. if (WARN_ON(nodes_empty(node_possible_map)))
  351. return -EINVAL;
  352. init_node_memblock();
  353. if (numa_meminfo_cover_memory(&numa_meminfo) == false)
  354. return -EINVAL;
  355. for_each_node_mask(node, node_possible_map) {
  356. node_mem_init(node);
  357. node_set_online(node);
  358. }
  359. max_low_pfn = PHYS_PFN(memblock_end_of_DRAM());
  360. setup_nr_node_ids();
  361. loongson_sysconf.nr_nodes = nr_node_ids;
  362. loongson_sysconf.cores_per_node = cpumask_weight(&phys_cpus_on_node[0]);
  363. return 0;
  364. }
  365. #endif
  366. void __init paging_init(void)
  367. {
  368. unsigned int node;
  369. unsigned long zones_size[MAX_NR_ZONES] = {0, };
  370. for_each_online_node(node) {
  371. unsigned long start_pfn, end_pfn;
  372. get_pfn_range_for_nid(node, &start_pfn, &end_pfn);
  373. if (end_pfn > max_low_pfn)
  374. max_low_pfn = end_pfn;
  375. }
  376. #ifdef CONFIG_ZONE_DMA32
  377. zones_size[ZONE_DMA32] = MAX_DMA32_PFN;
  378. #endif
  379. zones_size[ZONE_NORMAL] = max_low_pfn;
  380. free_area_init(zones_size);
  381. }
  382. void __init mem_init(void)
  383. {
  384. high_memory = (void *) __va(max_low_pfn << PAGE_SHIFT);
  385. memblock_free_all();
  386. setup_zero_pages(); /* This comes from node 0 */
  387. }
  388. int pcibus_to_node(struct pci_bus *bus)
  389. {
  390. return dev_to_node(&bus->dev);
  391. }
  392. EXPORT_SYMBOL(pcibus_to_node);