[PATCH] x86-64: Support dualcore and 8 socket systems in k8 fallback node parsing

In particular on systems where the local APIC space and node space
is very different from the Linux CPU number space.

Previously the older NUMA setup code directly parsing the K8
northbridge registers had some issues on 8 socket or dual core
systems. This patch fixes them.

This is mainly done by fixing some confusion between Linux
CPU numbers and local APIC ids. We now pass the local APIC IDs
to later code, which avoids mismatches.

Also add some heuristics to detect cases where the Hypertransport
nodeids and the local APIC IDs don't match, but are shifted
by a constant offset.

This is still all quite hackish, hopefully BIOS writers fill
in correct SRATs instead.

Signed-off-by: Andi Kleen <ak@suse.de>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
diff --git a/arch/x86_64/mm/k8topology.c b/arch/x86_64/mm/k8topology.c
index ec35747..65417b0 100644
--- a/arch/x86_64/mm/k8topology.c
+++ b/arch/x86_64/mm/k8topology.c
@@ -45,10 +45,12 @@
 	unsigned long prevbase;
 	struct node nodes[8];
 	int nodeid, i, nb; 
+	unsigned char nodeids[8];
 	int found = 0;
 	u32 reg;
 	unsigned numnodes;
 	nodemask_t nodes_parsed;
+	unsigned dualcore = 0;
 
 	nodes_clear(nodes_parsed);
 
@@ -67,11 +69,15 @@
 	prevbase = 0;
 	for (i = 0; i < 8; i++) { 
 		unsigned long base,limit; 
-
+		u32 nodeid;
+		
+		/* Undefined before E stepping, but hopefully 0 */
+		dualcore |= ((read_pci_config(0, nb, 3, 0xe8) >> 12) & 3) == 1;
 		base = read_pci_config(0, nb, 1, 0x40 + i*8);
 		limit = read_pci_config(0, nb, 1, 0x44 + i*8);
 
 		nodeid = limit & 7; 
+		nodeids[i] = nodeid;
 		if ((base & 3) == 0) { 
 			if (i < numnodes)
 				printk("Skipping disabled node %d\n", i); 
@@ -157,8 +163,9 @@
 
 	for (i = 0; i < 8; i++) {
 		if (nodes[i].start != nodes[i].end) { 
-			/* assume 1:1 NODE:CPU */
-			cpu_to_node[i] = i; 
+			nodeid = nodeids[i];
+			apicid_to_node[nodeid << dualcore] = i;
+			apicid_to_node[(nodeid << dualcore) + dualcore] = i;
 			setup_node_bootmem(i, nodes[i].start, nodes[i].end); 
 		} 
 	}