• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  * This file contains NUMA specific variables and functions which can
7  * be split away from DISCONTIGMEM and are used on NUMA machines with
8  * contiguous memory.
9  *
10  *                         2002/08/07 Erich Focht <efocht@ess.nec.de>
11  */
12 
13 #include <linux/cpu.h>
14 #include <linux/kernel.h>
15 #include <linux/mm.h>
16 #include <linux/node.h>
17 #include <linux/init.h>
18 #include <linux/bootmem.h>
19 #include <linux/module.h>
20 #include <asm/mmzone.h>
21 #include <asm/numa.h>
22 
23 
24 /*
25  * The following structures are usually initialized by ACPI or
26  * similar mechanisms and describe the NUMA characteristics of the machine.
27  */
28 int num_node_memblks;
29 struct node_memblk_s node_memblk[NR_NODE_MEMBLKS];
30 struct node_cpuid_s node_cpuid[NR_CPUS] =
31 	{ [0 ... NR_CPUS-1] = { .phys_id = 0, .nid = NUMA_NO_NODE } };
32 
33 /*
34  * This is a matrix with "distances" between nodes, they should be
35  * proportional to the memory access latency ratios.
36  */
37 u8 numa_slit[MAX_NUMNODES * MAX_NUMNODES];
38 
39 /* Identify which cnode a physical address resides on */
40 int
paddr_to_nid(unsigned long paddr)41 paddr_to_nid(unsigned long paddr)
42 {
43 	int	i;
44 
45 	for (i = 0; i < num_node_memblks; i++)
46 		if (paddr >= node_memblk[i].start_paddr &&
47 		    paddr < node_memblk[i].start_paddr + node_memblk[i].size)
48 			break;
49 
50 	return (i < num_node_memblks) ? node_memblk[i].nid : (num_node_memblks ? -1 : 0);
51 }
52 EXPORT_SYMBOL(paddr_to_nid);
53 
54 #if defined(CONFIG_SPARSEMEM) && defined(CONFIG_NUMA)
55 /*
56  * Because of holes evaluate on section limits.
57  * If the section of memory exists, then return the node where the section
58  * resides.  Otherwise return node 0 as the default.  This is used by
59  * SPARSEMEM to allocate the SPARSEMEM sectionmap on the NUMA node where
60  * the section resides.
61  */
__early_pfn_to_nid(unsigned long pfn,struct mminit_pfnnid_cache * state)62 int __meminit __early_pfn_to_nid(unsigned long pfn,
63 					struct mminit_pfnnid_cache *state)
64 {
65 	int i, section = pfn >> PFN_SECTION_SHIFT, ssec, esec;
66 
67 	if (section >= state->last_start && section < state->last_end)
68 		return state->last_nid;
69 
70 	for (i = 0; i < num_node_memblks; i++) {
71 		ssec = node_memblk[i].start_paddr >> PA_SECTION_SHIFT;
72 		esec = (node_memblk[i].start_paddr + node_memblk[i].size +
73 			((1L << PA_SECTION_SHIFT) - 1)) >> PA_SECTION_SHIFT;
74 		if (section >= ssec && section < esec) {
75 			state->last_start = ssec;
76 			state->last_end = esec;
77 			state->last_nid = node_memblk[i].nid;
78 			return node_memblk[i].nid;
79 		}
80 	}
81 
82 	return -1;
83 }
84 
numa_clear_node(int cpu)85 void numa_clear_node(int cpu)
86 {
87 	unmap_cpu_from_node(cpu, NUMA_NO_NODE);
88 }
89 
90 #ifdef CONFIG_MEMORY_HOTPLUG
91 /*
92  *  SRAT information is stored in node_memblk[], then we can use SRAT
93  *  information at memory-hot-add if necessary.
94  */
95 
memory_add_physaddr_to_nid(u64 addr)96 int memory_add_physaddr_to_nid(u64 addr)
97 {
98 	int nid = paddr_to_nid(addr);
99 	if (nid < 0)
100 		return 0;
101 	return nid;
102 }
103 
104 EXPORT_SYMBOL_GPL(memory_add_physaddr_to_nid);
105 #endif
106 #endif
107