2 * Written by Kanoj Sarcar (kanoj@sgi.com) Aug 99
3 * Adapted for K8/x86-64 Jul 2002 by Andi Kleen.
8 #include <linux/config.h>
10 typedef struct plat_pglist_data {
12 unsigned long start_pfn, end_pfn;
15 struct bootmem_data_t;
18 * Following are macros that are specific to this numa platform.
20 * XXX check what the compiler generates for all this
23 extern plat_pg_data_t *plat_node_data[];
26 #define MAX_NUMNODES MAXNODE
27 #define NODEMAPSIZE 0xff
29 /* Simple perfect hash to map physical addresses to node numbers */
30 extern int memnode_shift;
31 extern u8 memnodemap[NODEMAPSIZE];
35 #define VIRTUAL_BUG_ON(x) do { if (x) out_of_line_bug(); } while(0)
37 #define VIRTUAL_BUG_ON(x) do {} while (0)
40 /* VALID_PAGE below hardcodes the same algorithm*/
41 static inline int phys_to_nid(unsigned long addr)
44 VIRTUAL_BUG_ON((addr >> memnode_shift) >= NODEMAPSIZE);
45 nid = memnodemap[addr >> memnode_shift];
46 VIRTUAL_BUG_ON(nid > maxnode);
50 #define PLAT_NODE_DATA(n) (plat_node_data[(n)])
51 #define PLAT_NODE_DATA_STARTNR(n) \
52 (PLAT_NODE_DATA(n)->gendata.node_start_mapnr)
53 #define PLAT_NODE_DATA_SIZE(n) (PLAT_NODE_DATA(n)->gendata.node_size)
55 #define PLAT_NODE_DATA_LOCALNR(p, n) \
56 (((p) - PLAT_NODE_DATA(n)->gendata.node_start_paddr) >> PAGE_SHIFT)
58 #ifdef CONFIG_DISCONTIGMEM
61 * Following are macros that each numa implmentation must define.
65 * Given a kernel address, find the home node of the underlying memory.
67 #define KVADDR_TO_NID(kaddr) phys_to_nid(__pa(kaddr))
70 * Return a pointer to the node data for node n.
72 #define NODE_DATA(n) (&((PLAT_NODE_DATA(n))->gendata))
75 * NODE_MEM_MAP gives the kaddr for the mem_map of the node.
77 #define NODE_MEM_MAP(nid) (NODE_DATA(nid)->node_mem_map)
80 * Given a kaddr, ADDR_TO_MAPBASE finds the owning node of the memory
81 * and returns the the mem_map of that node.
83 #define ADDR_TO_MAPBASE(kaddr) \
84 NODE_MEM_MAP(KVADDR_TO_NID((unsigned long)(kaddr)))
87 * Given a kaddr, LOCAL_BASE_ADDR finds the owning node of the memory
88 * and returns the kaddr corresponding to first physical page in the
91 #define LOCAL_BASE_ADDR(kaddr) ((unsigned long)__va(NODE_DATA(KVADDR_TO_NID(kaddr))->node_start_paddr))
93 #define LOCAL_MAP_NR(kvaddr) \
94 (((unsigned long)(kvaddr)-LOCAL_BASE_ADDR(kvaddr)) >> PAGE_SHIFT)
96 #define BAD_PAGE 0xffffffffffff
98 /* this really should be optimized a bit */
99 static inline unsigned long
100 paddr_to_local_pfn(unsigned long phys_addr, struct page **mem_map, int check)
103 if (check) { /* we rely on gcc optimizing this way for most cases */
104 unsigned long index = phys_addr >> memnode_shift;
105 if (index >= NODEMAPSIZE || memnodemap[index] == 0xff) {
109 nid = memnodemap[index];
111 nid = phys_to_nid(phys_addr);
113 plat_pg_data_t *plat_pgdat = plat_node_data[nid];
114 unsigned long pfn = phys_addr >> PAGE_SHIFT;
115 VIRTUAL_BUG_ON(pfn >= plat_pgdat->end_pfn);
116 VIRTUAL_BUG_ON(pfn < plat_pgdat->start_pfn);
117 *mem_map = plat_pgdat->gendata.node_mem_map;
118 return pfn - plat_pgdat->start_pfn;
120 #define virt_to_page(kaddr) \
121 ({ struct page *lmemmap; \
122 unsigned long lpfn = paddr_to_local_pfn(__pa(kaddr),&lmemmap,0); \
125 /* needs to handle bad addresses too */
126 #define pte_page(pte) \
127 ({ struct page *lmemmap; \
128 unsigned long addr = pte_val(pte) & PHYSICAL_PAGE_MASK; \
129 unsigned long lpfn = paddr_to_local_pfn(addr,&lmemmap,1); \
132 #define pfn_to_page(pfn) virt_to_page(__va((unsigned long)(pfn) << PAGE_SHIFT))
133 #define page_to_pfn(page) ({ \
134 int nodeid = phys_to_nid(__pa(page)); \
135 plat_pg_data_t *nd = PLAT_NODE_DATA(nodeid); \
136 (page - nd->gendata.node_mem_map) + nd->start_pfn; \
139 #define VALID_PAGE(page_ptr) ({ \
141 unsigned long phys = __pa(page_ptr); \
142 unsigned long index = phys >> memnode_shift; \
143 if (index <= NODEMAPSIZE) { \
144 unsigned nodeid = memnodemap[index]; \
145 pg_data_t *nd = NODE_DATA(nodeid); \
146 struct page *lmemmap = nd->node_mem_map; \
147 ok = (nodeid != 0xff) && \
148 (page_ptr >= lmemmap && page_ptr < lmemmap + nd->node_size); \
153 #define page_to_phys(page) (page_to_pfn(page) << PAGE_SHIFT)
157 extern void setup_node_bootmem(int nodeid, unsigned long start_, unsigned long end_);
161 extern int fake_node;
162 #define cputonode(cpu) (fake_node ? 0 : (cpu))
163 #define numa_node_id() cputonode(smp_processor_id())
164 #endif /* CONFIG_NUMA */
166 #define MAX_NR_NODES 8
168 #endif /* CONFIG_DISCONTIGMEM */
170 #endif /* _ASM_MMZONE_H_ */