numa.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2010 Loongson Inc. & Lemote Inc. &
  4. * Institute of Computing Technology
  5. * Author: Xiang Gao, [email protected]
  6. * Huacai Chen, [email protected]
  7. * Xiaofu Meng, Shuangshuang Zhang
  8. */
  9. #include <linux/init.h>
  10. #include <linux/kernel.h>
  11. #include <linux/mm.h>
  12. #include <linux/mmzone.h>
  13. #include <linux/export.h>
  14. #include <linux/nodemask.h>
  15. #include <linux/swap.h>
  16. #include <linux/memblock.h>
  17. #include <linux/pfn.h>
  18. #include <linux/highmem.h>
  19. #include <asm/page.h>
  20. #include <asm/pgalloc.h>
  21. #include <asm/sections.h>
  22. #include <linux/irq.h>
  23. #include <asm/bootinfo.h>
  24. #include <asm/mc146818-time.h>
  25. #include <asm/time.h>
  26. #include <asm/wbflush.h>
  27. #include <boot_param.h>
  28. #include <loongson.h>
  29. unsigned char __node_distances[MAX_NUMNODES][MAX_NUMNODES];
  30. EXPORT_SYMBOL(__node_distances);
  31. struct pglist_data *__node_data[MAX_NUMNODES];
  32. EXPORT_SYMBOL(__node_data);
  33. cpumask_t __node_cpumask[MAX_NUMNODES];
  34. EXPORT_SYMBOL(__node_cpumask);
  35. static void cpu_node_probe(void)
  36. {
  37. int i;
  38. nodes_clear(node_possible_map);
  39. nodes_clear(node_online_map);
  40. for (i = 0; i < loongson_sysconf.nr_nodes; i++) {
  41. node_set_state(num_online_nodes(), N_POSSIBLE);
  42. node_set_online(num_online_nodes());
  43. }
  44. pr_info("NUMA: Discovered %d cpus on %d nodes\n",
  45. loongson_sysconf.nr_cpus, num_online_nodes());
  46. }
  47. static int __init compute_node_distance(int row, int col)
  48. {
  49. int package_row = row * loongson_sysconf.cores_per_node /
  50. loongson_sysconf.cores_per_package;
  51. int package_col = col * loongson_sysconf.cores_per_node /
  52. loongson_sysconf.cores_per_package;
  53. if (col == row)
  54. return LOCAL_DISTANCE;
  55. else if (package_row == package_col)
  56. return 40;
  57. else
  58. return 100;
  59. }
  60. static void __init init_topology_matrix(void)
  61. {
  62. int row, col;
  63. for (row = 0; row < MAX_NUMNODES; row++)
  64. for (col = 0; col < MAX_NUMNODES; col++)
  65. __node_distances[row][col] = -1;
  66. for_each_online_node(row) {
  67. for_each_online_node(col) {
  68. __node_distances[row][col] =
  69. compute_node_distance(row, col);
  70. }
  71. }
  72. }
  73. static void __init node_mem_init(unsigned int node)
  74. {
  75. struct pglist_data *nd;
  76. unsigned long node_addrspace_offset;
  77. unsigned long start_pfn, end_pfn;
  78. unsigned long nd_pa;
  79. int tnid;
  80. const size_t nd_size = roundup(sizeof(pg_data_t), SMP_CACHE_BYTES);
  81. node_addrspace_offset = nid_to_addrbase(node);
  82. pr_info("Node%d's addrspace_offset is 0x%lx\n",
  83. node, node_addrspace_offset);
  84. get_pfn_range_for_nid(node, &start_pfn, &end_pfn);
  85. pr_info("Node%d: start_pfn=0x%lx, end_pfn=0x%lx\n",
  86. node, start_pfn, end_pfn);
  87. nd_pa = memblock_phys_alloc_try_nid(nd_size, SMP_CACHE_BYTES, node);
  88. if (!nd_pa)
  89. panic("Cannot allocate %zu bytes for node %d data\n",
  90. nd_size, node);
  91. nd = __va(nd_pa);
  92. memset(nd, 0, sizeof(struct pglist_data));
  93. tnid = early_pfn_to_nid(nd_pa >> PAGE_SHIFT);
  94. if (tnid != node)
  95. pr_info("NODE_DATA(%d) on node %d\n", node, tnid);
  96. __node_data[node] = nd;
  97. NODE_DATA(node)->node_start_pfn = start_pfn;
  98. NODE_DATA(node)->node_spanned_pages = end_pfn - start_pfn;
  99. if (node == 0) {
  100. /* kernel start address */
  101. unsigned long kernel_start_pfn = PFN_DOWN(__pa_symbol(&_text));
  102. /* kernel end address */
  103. unsigned long kernel_end_pfn = PFN_UP(__pa_symbol(&_end));
  104. /* used by finalize_initrd() */
  105. max_low_pfn = end_pfn;
  106. /* Reserve the kernel text/data/bss */
  107. memblock_reserve(kernel_start_pfn << PAGE_SHIFT,
  108. ((kernel_end_pfn - kernel_start_pfn) << PAGE_SHIFT));
  109. /* Reserve 0xfe000000~0xffffffff for RS780E integrated GPU */
  110. if (node_end_pfn(0) >= (0xffffffff >> PAGE_SHIFT))
  111. memblock_reserve((node_addrspace_offset | 0xfe000000),
  112. 32 << 20);
  113. /* Reserve pfn range 0~node[0]->node_start_pfn */
  114. memblock_reserve(0, PAGE_SIZE * start_pfn);
  115. }
  116. }
  117. static __init void prom_meminit(void)
  118. {
  119. unsigned int node, cpu, active_cpu = 0;
  120. cpu_node_probe();
  121. init_topology_matrix();
  122. for (node = 0; node < loongson_sysconf.nr_nodes; node++) {
  123. if (node_online(node)) {
  124. szmem(node);
  125. node_mem_init(node);
  126. cpumask_clear(&__node_cpumask[node]);
  127. }
  128. }
  129. max_low_pfn = PHYS_PFN(memblock_end_of_DRAM());
  130. for (cpu = 0; cpu < loongson_sysconf.nr_cpus; cpu++) {
  131. node = cpu / loongson_sysconf.cores_per_node;
  132. if (node >= num_online_nodes())
  133. node = 0;
  134. if (loongson_sysconf.reserved_cpus_mask & (1<<cpu))
  135. continue;
  136. cpumask_set_cpu(active_cpu, &__node_cpumask[node]);
  137. pr_info("NUMA: set cpumask cpu %d on node %d\n", active_cpu, node);
  138. active_cpu++;
  139. }
  140. }
  141. void __init paging_init(void)
  142. {
  143. unsigned long zones_size[MAX_NR_ZONES] = {0, };
  144. pagetable_init();
  145. zones_size[ZONE_DMA32] = MAX_DMA32_PFN;
  146. zones_size[ZONE_NORMAL] = max_low_pfn;
  147. free_area_init(zones_size);
  148. }
  149. void __init mem_init(void)
  150. {
  151. high_memory = (void *) __va(get_num_physpages() << PAGE_SHIFT);
  152. memblock_free_all();
  153. setup_zero_pages(); /* This comes from node 0 */
  154. }
  155. /* All PCI device belongs to logical Node-0 */
  156. int pcibus_to_node(struct pci_bus *bus)
  157. {
  158. return 0;
  159. }
  160. EXPORT_SYMBOL(pcibus_to_node);
  161. void __init prom_init_numa_memory(void)
  162. {
  163. pr_info("CP0_Config3: CP0 16.3 (0x%x)\n", read_c0_config3());
  164. pr_info("CP0_PageGrain: CP0 5.1 (0x%x)\n", read_c0_pagegrain());
  165. prom_meminit();
  166. }
  167. pg_data_t * __init arch_alloc_nodedata(int nid)
  168. {
  169. return memblock_alloc(sizeof(pg_data_t), SMP_CACHE_BYTES);
  170. }
  171. void arch_refresh_nodedata(int nid, pg_data_t *pgdat)
  172. {
  173. __node_data[nid] = pgdat;
  174. }