srat_32.c 10.6 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/*
 * Some of the code in this file has been gleaned from the 64 bit 
 * discontigmem support code base.
 *
 * Copyright (C) 2002, IBM Corp.
 *
 * All rights reserved.          
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
 * NON INFRINGEMENT.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * Send feedback to Pat Gaughen <gone@us.ibm.com>
 */
#include <linux/mm.h>
#include <linux/bootmem.h>
#include <linux/mmzone.h>
#include <linux/acpi.h>
#include <linux/nodemask.h>
#include <asm/srat.h>
#include <asm/topology.h>
33
#include <asm/smp.h>
L
Linus Torvalds 已提交
34 35 36 37 38 39 40 41 42 43 44 45

/*
 * proximity macros and definitions
 */
#define NODE_ARRAY_INDEX(x)	((x) / 8)	/* 8 bits/char */
#define NODE_ARRAY_OFFSET(x)	((x) % 8)	/* 8 bits/char */
#define BMAP_SET(bmap, bit)	((bmap)[NODE_ARRAY_INDEX(bit)] |= 1 << NODE_ARRAY_OFFSET(bit))
#define BMAP_TEST(bmap, bit)	((bmap)[NODE_ARRAY_INDEX(bit)] & (1 << NODE_ARRAY_OFFSET(bit)))
/* bitmap length; _PXM is at most 255 */
#define PXM_BITMAP_LEN (MAX_PXM_DOMAINS / 8) 
static u8 pxm_bitmap[PXM_BITMAP_LEN];	/* bitmap of proximity domains */

46
#define MAX_CHUNKS_PER_NODE	3
L
Linus Torvalds 已提交
47 48 49 50 51 52 53 54 55 56 57
#define MAXCHUNKS		(MAX_CHUNKS_PER_NODE * MAX_NUMNODES)
struct node_memory_chunk_s {
	unsigned long	start_pfn;
	unsigned long	end_pfn;
	u8	pxm;		// proximity domain of node
	u8	nid;		// which cnode contains this chunk?
	u8	bank;		// which mem bank on this node
};
static struct node_memory_chunk_s node_memory_chunk[MAXCHUNKS];

static int num_memory_chunks;		/* total number of memory chunks */
58
static u8 __initdata apicid_to_pxm[MAX_APICID];
L
Linus Torvalds 已提交
59 60 61 62 63 64

extern void * boot_ioremap(unsigned long, unsigned long);

/* Identify CPU proximity domains */
static void __init parse_cpu_affinity_structure(char *p)
{
65 66
	struct acpi_srat_cpu_affinity *cpu_affinity =
				(struct acpi_srat_cpu_affinity *) p;
L
Linus Torvalds 已提交
67

68
	if ((cpu_affinity->flags & ACPI_SRAT_CPU_ENABLED) == 0)
L
Linus Torvalds 已提交
69 70 71
		return;		/* empty entry */

	/* mark this node as "seen" in node bitmap */
72
	BMAP_SET(pxm_bitmap, cpu_affinity->proximity_domain_lo);
L
Linus Torvalds 已提交
73

74
	apicid_to_pxm[cpu_affinity->apic_id] = cpu_affinity->proximity_domain_lo;
75

L
Linus Torvalds 已提交
76
	printk("CPU 0x%02X in proximity domain 0x%02X\n",
77
		cpu_affinity->apic_id, cpu_affinity->proximity_domain_lo);
L
Linus Torvalds 已提交
78 79 80 81 82 83 84 85 86
}

/*
 * Identify memory proximity domains and hot-remove capabilities.
 * Fill node memory chunk list structure.
 */
static void __init parse_memory_affinity_structure (char *sratp)
{
	unsigned long long paddr, size;
87
	unsigned long start_pfn, end_pfn;
L
Linus Torvalds 已提交
88 89
	u8 pxm;
	struct node_memory_chunk_s *p, *q, *pend;
90 91
	struct acpi_srat_mem_affinity *memory_affinity =
			(struct acpi_srat_mem_affinity *) sratp;
L
Linus Torvalds 已提交
92

93
	if ((memory_affinity->flags & ACPI_SRAT_MEM_ENABLED) == 0)
L
Linus Torvalds 已提交
94 95
		return;		/* empty entry */

96 97
	pxm = memory_affinity->proximity_domain & 0xff;

L
Linus Torvalds 已提交
98
	/* mark this node as "seen" in node bitmap */
99
	BMAP_SET(pxm_bitmap, pxm);
L
Linus Torvalds 已提交
100 101

	/* calculate info for memory chunk structure */
102 103 104
	paddr = memory_affinity->base_address;
	size = memory_affinity->length;

L
Linus Torvalds 已提交
105 106
	start_pfn = paddr >> PAGE_SHIFT;
	end_pfn = (paddr + size) >> PAGE_SHIFT;
107

L
Linus Torvalds 已提交
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133

	if (num_memory_chunks >= MAXCHUNKS) {
		printk("Too many mem chunks in SRAT. Ignoring %lld MBytes at %llx\n",
			size/(1024*1024), paddr);
		return;
	}

	/* Insertion sort based on base address */
	pend = &node_memory_chunk[num_memory_chunks];
	for (p = &node_memory_chunk[0]; p < pend; p++) {
		if (start_pfn < p->start_pfn)
			break;
	}
	if (p < pend) {
		for (q = pend; q >= p; q--)
			*(q + 1) = *q;
	}
	p->start_pfn = start_pfn;
	p->end_pfn = end_pfn;
	p->pxm = pxm;

	num_memory_chunks++;

	printk("Memory range 0x%lX to 0x%lX (type 0x%X) in proximity domain 0x%02X %s\n",
		start_pfn, end_pfn,
		memory_affinity->memory_type,
134 135
		pxm,
		((memory_affinity->flags & ACPI_SRAT_MEM_HOT_PLUGGABLE) ?
L
Linus Torvalds 已提交
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
		 "enabled and removable" : "enabled" ) );
}

/*
 * The SRAT table always lists ascending addresses, so can always
 * assume that the first "start" address that you see is the real
 * start of the node, and that the current "end" address is after
 * the previous one.
 */
static __init void node_read_chunk(int nid, struct node_memory_chunk_s *memory_chunk)
{
	/*
	 * Only add present memory as told by the e820.
	 * There is no guarantee from the SRAT that the memory it
	 * enumerates is present at boot time because it represents
	 * *possible* memory hotplug areas the same as normal RAM.
	 */
	if (memory_chunk->start_pfn >= max_pfn) {
		printk (KERN_INFO "Ignoring SRAT pfns: 0x%08lx -> %08lx\n",
			memory_chunk->start_pfn, memory_chunk->end_pfn);
		return;
	}
	if (memory_chunk->nid != nid)
		return;

	if (!node_has_online_mem(nid))
		node_start_pfn[nid] = memory_chunk->start_pfn;

	if (node_start_pfn[nid] > memory_chunk->start_pfn)
		node_start_pfn[nid] = memory_chunk->start_pfn;

	if (node_end_pfn[nid] < memory_chunk->end_pfn)
		node_end_pfn[nid] = memory_chunk->end_pfn;
}

/* Parse the ACPI Static Resource Affinity Table */
static int __init acpi20_parse_srat(struct acpi_table_srat *sratp)
{
	u8 *start, *end, *p;
	int i, j, nid;

	start = (u8 *)(&(sratp->reserved) + 1);	/* skip header */
	p = start;
	end = (u8 *)sratp + sratp->header.length;

	memset(pxm_bitmap, 0, sizeof(pxm_bitmap));	/* init proximity domain bitmap */
	memset(node_memory_chunk, 0, sizeof(node_memory_chunk));

	num_memory_chunks = 0;
	while (p < end) {
		switch (*p) {
187
		case ACPI_SRAT_TYPE_CPU_AFFINITY:
L
Linus Torvalds 已提交
188 189
			parse_cpu_affinity_structure(p);
			break;
190
		case ACPI_SRAT_TYPE_MEMORY_AFFINITY:
L
Linus Torvalds 已提交
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
			parse_memory_affinity_structure(p);
			break;
		default:
			printk("ACPI 2.0 SRAT: unknown entry skipped: type=0x%02X, len=%d\n", p[0], p[1]);
			break;
		}
		p += p[1];
		if (p[1] == 0) {
			printk("acpi20_parse_srat: Entry length value is zero;"
				" can't parse any further!\n");
			break;
		}
	}

	if (num_memory_chunks == 0) {
		printk("could not finy any ACPI SRAT memory areas.\n");
		goto out_fail;
	}

	/* Calculate total number of nodes in system from PXM bitmap and create
	 * a set of sequential node IDs starting at zero.  (ACPI doesn't seem
	 * to specify the range of _PXM values.)
	 */
	/*
	 * MCD - we no longer HAVE to number nodes sequentially.  PXM domain
	 * numbers could go as high as 256, and MAX_NUMNODES for i386 is typically
	 * 32, so we will continue numbering them in this manner until MAX_NUMNODES
	 * approaches MAX_PXM_DOMAINS for i386.
	 */
	nodes_clear(node_online_map);
	for (i = 0; i < MAX_PXM_DOMAINS; i++) {
		if (BMAP_TEST(pxm_bitmap, i)) {
223
			int nid = acpi_map_pxm_to_node(i);
L
Linus Torvalds 已提交
224 225 226 227 228 229 230
			node_set_online(nid);
		}
	}
	BUG_ON(num_online_nodes() == 0);

	/* set cnode id in memory chunk structure */
	for (i = 0; i < num_memory_chunks; i++)
231
		node_memory_chunk[i].nid = pxm_to_node(node_memory_chunk[i].pxm);
L
Linus Torvalds 已提交
232 233 234 235 236 237 238 239 240

	printk("pxm bitmap: ");
	for (i = 0; i < sizeof(pxm_bitmap); i++) {
		printk("%02X ", pxm_bitmap[i]);
	}
	printk("\n");
	printk("Number of logical nodes in system = %d\n", num_online_nodes());
	printk("Number of memory chunks in system = %d\n", num_memory_chunks);

241 242 243
	for (i = 0; i < MAX_APICID; i++)
		apicid_2_node[i] = pxm_to_node(apicid_to_pxm[i]);

L
Linus Torvalds 已提交
244 245 246 247 248
	for (j = 0; j < num_memory_chunks; j++){
		struct node_memory_chunk_s * chunk = &node_memory_chunk[j];
		printk("chunk %d nid %d start_pfn %08lx end_pfn %08lx\n",
		       j, chunk->nid, chunk->start_pfn, chunk->end_pfn);
		node_read_chunk(chunk->nid, chunk);
249
		add_active_range(chunk->nid, chunk->start_pfn, chunk->end_pfn);
L
Linus Torvalds 已提交
250 251 252 253 254 255 256 257 258 259 260 261 262 263
	}
 
	for_each_online_node(nid) {
		unsigned long start = node_start_pfn[nid];
		unsigned long end = node_end_pfn[nid];

		memory_present(nid, start, end);
		node_remap_size[nid] = node_memmap_size_bytes(nid, start, end);
	}
	return 1;
out_fail:
	return 0;
}

264 265 266 267 268
struct acpi_static_rsdt {
	struct acpi_table_rsdt table;
	u32 padding[7]; /* Allow for 7 more table entries */
};

L
Linus Torvalds 已提交
269 270 271 272 273
int __init get_memcfg_from_srat(void)
{
	struct acpi_table_header *header = NULL;
	struct acpi_table_rsdp *rsdp = NULL;
	struct acpi_table_rsdt *rsdt = NULL;
274 275
	acpi_native_uint rsdp_address = 0;
	struct acpi_static_rsdt saved_rsdt;
L
Linus Torvalds 已提交
276 277 278
	int tables = 0;
	int i = 0;

279
	rsdp_address = acpi_os_get_root_pointer();
280
	if (!rsdp_address) {
281 282 283 284
		printk("%s: System description tables not found\n",
		       __FUNCTION__);
		goto out_err;
	}
L
Linus Torvalds 已提交
285

286 287
	printk("%s: assigning address to rsdp\n", __FUNCTION__);
	rsdp = (struct acpi_table_rsdp *)(u32)rsdp_address;
L
Linus Torvalds 已提交
288 289 290 291 292 293 294 295
	if (!rsdp) {
		printk("%s: Didn't find ACPI root!\n", __FUNCTION__);
		goto out_err;
	}

	printk(KERN_INFO "%.8s v%d [%.6s]\n", rsdp->signature, rsdp->revision,
		rsdp->oem_id);

296
	if (strncmp(rsdp->signature, ACPI_SIG_RSDP,strlen(ACPI_SIG_RSDP))) {
L
Linus Torvalds 已提交
297 298 299 300 301
		printk(KERN_WARNING "%s: RSDP table signature incorrect\n", __FUNCTION__);
		goto out_err;
	}

	rsdt = (struct acpi_table_rsdt *)
302
	    boot_ioremap(rsdp->rsdt_physical_address, sizeof(struct acpi_table_rsdt));
L
Linus Torvalds 已提交
303 304 305 306 307 308 309 310

	if (!rsdt) {
		printk(KERN_WARNING
		       "%s: ACPI: Invalid root system description tables (RSDT)\n",
		       __FUNCTION__);
		goto out_err;
	}

311
	header = &rsdt->header;
L
Linus Torvalds 已提交
312

313
	if (strncmp(header->signature, ACPI_SIG_RSDT, strlen(ACPI_SIG_RSDT))) {
L
Linus Torvalds 已提交
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
		printk(KERN_WARNING "ACPI: RSDT signature incorrect\n");
		goto out_err;
	}

	/* 
	 * The number of tables is computed by taking the 
	 * size of all entries (header size minus total 
	 * size of RSDT) divided by the size of each entry
	 * (4-byte table pointers).
	 */
	tables = (header->length - sizeof(struct acpi_table_header)) / 4;

	if (!tables)
		goto out_err;

	memcpy(&saved_rsdt, rsdt, sizeof(saved_rsdt));

331
	if (saved_rsdt.table.header.length > sizeof(saved_rsdt)) {
L
Linus Torvalds 已提交
332
		printk(KERN_WARNING "ACPI: Too big length in RSDT: %d\n",
333
		       saved_rsdt.table.header.length);
L
Linus Torvalds 已提交
334 335 336 337 338 339 340 341
		goto out_err;
	}

	printk("Begin SRAT table scan....\n");

	for (i = 0; i < tables; i++) {
		/* Map in header, then map in full table length. */
		header = (struct acpi_table_header *)
342
			boot_ioremap(saved_rsdt.table.table_offset_entry[i], sizeof(struct acpi_table_header));
L
Linus Torvalds 已提交
343 344 345
		if (!header)
			break;
		header = (struct acpi_table_header *)
346
			boot_ioremap(saved_rsdt.table.table_offset_entry[i], header->length);
L
Linus Torvalds 已提交
347 348 349
		if (!header)
			break;

350
		if (strncmp((char *) &header->signature, ACPI_SIG_SRAT, 4))
L
Linus Torvalds 已提交
351 352 353 354 355 356
			continue;

		/* we've found the srat table. don't need to look at any more tables */
		return acpi20_parse_srat((struct acpi_table_srat *)header);
	}
out_err:
357
	remove_all_active_ranges();
L
Linus Torvalds 已提交
358 359 360
	printk("failed to get NUMA memory information from SRAT table\n");
	return 0;
}