diff -uNr /home/yhlu/xx1/linux-2.6.13-rc2.orig/arch/x86_64/kernel/genapic.c /home/yhlu/xx1/linux-2.6.13-rc2/arch/x86_64/kernel/genapic.c
--- /home/yhlu/xx1/linux-2.6.13-rc2.orig/arch/x86_64/kernel/genapic.c	2005-07-05 20:46:33.000000000 -0700
+++ /home/yhlu/xx1/linux-2.6.13-rc2/arch/x86_64/kernel/genapic.c	2005-07-06 15:37:52.932607728 -0700
@@ -31,25 +31,20 @@
 
 extern struct genapic apic_cluster;
 extern struct genapic apic_flat;
+extern struct genapic apic_physflat;
 
 struct genapic *genapic = &apic_flat;
 
-
 /*
  * Check the APIC IDs in bios_cpu_apicid and choose the APIC mode.
  */
-void __init clustered_apic_check(void)
+void __init extended_apic_check(void)
 {
 	long i;
 	u8 clusters, max_cluster;
 	u8 id;
 	u8 cluster_cnt[NUM_APIC_CLUSTERS];
-
-	if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD) {
-		/* AMD always uses flat mode right now */
-		genapic = &apic_flat;
-		goto print;
-	}
+	int num_cpus = 0;
 
 #if defined(CONFIG_ACPI_BUS)
 	/*
@@ -65,10 +60,19 @@
 
 	memset(cluster_cnt, 0, sizeof(cluster_cnt));
 
+        /* Count how many CPUs the BIOS told us about, but not
+          more than what the user specified */
 	for (i = 0; i < NR_CPUS; i++) {
 		id = bios_cpu_apicid[i];
-		if (id != BAD_APICID)
-			cluster_cnt[APIC_CLUSTERID(id)]++;
+		if (id == BAD_APICID)
+			continue;
+		cluster_cnt[APIC_CLUSTERID(id)]++;
+		num_cpus++;
+	}
+
+	if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD) {
+		genapic = num_cpus > 8 ? &apic_physflat : &apic_flat;
+		goto print;
 	}
 
 	clusters = 0;
diff -uNr /home/yhlu/xx1/linux-2.6.13-rc2.orig/arch/x86_64/kernel/genapic_flat.c /home/yhlu/xx1/linux-2.6.13-rc2/arch/x86_64/kernel/genapic_flat.c
--- /home/yhlu/xx1/linux-2.6.13-rc2.orig/arch/x86_64/kernel/genapic_flat.c	2005-07-05 20:46:33.000000000 -0700
+++ /home/yhlu/xx1/linux-2.6.13-rc2/arch/x86_64/kernel/genapic_flat.c	2005-07-06 15:41:04.414498056 -0700
@@ -2,7 +2,7 @@
  * Copyright 2004 James Cleverdon, IBM.
  * Subject to the GNU Public License, v.2
  *
- * Flat APIC subarch code.  Maximum 8 CPUs, logical delivery.
+ * Flat APIC subarch code.  Logical delivery.
  *
  * Hacked for x86-64 by James Cleverdon from i386 architecture code by
  * Martin Bligh, Andi Kleen, James Bottomley, John Stultz, and
@@ -195,6 +195,53 @@
 	.phys_pkg_id = phys_pkg_id,
 };
 
+
+/* 
+ * Physflat mode is used when there are more than 8 CPUs on a AMD system.
+ * We cannot use logical delivery in this case because the mask
+ * overflows, so use physical mode.
+ */
+
+static cpumask_t physflat_target_cpus(void)
+{
+       return cpumask_of_cpu(0);
+}
+
+static void physflat_send_IPI_mask(cpumask_t cpumask, int vector)
+{
+       send_IPI_mask_sequence(cpumask, vector);
+}
+
+static unsigned int physflat_cpu_mask_to_apicid(cpumask_t cpumask)
+{
+       int cpu;
+
+       /*
+        * We're using fixed IRQ delivery, can only return one phys APIC ID.
+        * May as well be the first.
+        */
+       cpu = first_cpu(cpumask);
+       if ((unsigned)cpu < NR_CPUS)
+               return x86_cpu_to_apicid[cpu];
+       else
+               return BAD_APICID;
+}
+
+struct genapic apic_physflat =  {
+       .name = "physical flat",
+       .int_delivery_mode = dest_LowestPrio,
+       .int_dest_mode = (APIC_DEST_PHYSICAL != 0),
+       .int_delivery_dest = APIC_DEST_PHYSICAL | APIC_DM_LOWEST,
+       .target_cpus = physflat_target_cpus,
+       .apic_id_registered = flat_apic_id_registered,
+       .init_apic_ldr = flat_init_apic_ldr,/*not needed, but shouldn't hurt*/
+       .send_IPI_all = flat_send_IPI_all,
+       .send_IPI_allbutself = flat_send_IPI_allbutself,
+       .send_IPI_mask = physflat_send_IPI_mask,
+       .cpu_mask_to_apicid = physflat_cpu_mask_to_apicid,
+       .phys_pkg_id = phys_pkg_id,
+};
+
 static int __init print_ipi_mode(void)
 {
 	printk ("Using IPI %s mode\n", no_broadcast ? "No-Shortcut" :
diff -uNr /home/yhlu/xx1/linux-2.6.13-rc2.orig/arch/x86_64/kernel/mpparse.c /home/yhlu/xx1/linux-2.6.13-rc2/arch/x86_64/kernel/mpparse.c
--- /home/yhlu/xx1/linux-2.6.13-rc2.orig/arch/x86_64/kernel/mpparse.c	2005-07-05 20:46:33.000000000 -0700
+++ /home/yhlu/xx1/linux-2.6.13-rc2/arch/x86_64/kernel/mpparse.c	2005-07-06 15:32:47.211084464 -0700
@@ -341,7 +341,7 @@
 			}
 		}
 	}
-	clustered_apic_check();
+	extended_apic_check();
 	if (!num_processors)
 		printk(KERN_ERR "SMP mptable: no processors registered!\n");
 	return num_processors;
diff -uNr /home/yhlu/xx1/linux-2.6.13-rc2.orig/arch/x86_64/kernel/smpboot.c /home/yhlu/xx1/linux-2.6.13-rc2/arch/x86_64/kernel/smpboot.c
--- /home/yhlu/xx1/linux-2.6.13-rc2.orig/arch/x86_64/kernel/smpboot.c	2005-07-05 20:46:33.000000000 -0700
+++ /home/yhlu/xx1/linux-2.6.13-rc2/arch/x86_64/kernel/smpboot.c	2005-07-06 18:45:11.923021480 -0700
@@ -442,7 +442,7 @@
 	/*
 	 * Allow the master to continue.
 	 */
-	cpu_set(cpuid, cpu_callin_map);
+//	cpu_set(cpuid, cpu_callin_map); // moved to start_secondary by yhlu
 }
 
 static inline void set_cpu_sibling_map(int cpu)
@@ -529,8 +529,11 @@
 	/* Wait for TSC sync to not schedule things before.
 	   We still process interrupts, which could see an inconsistent
 	   time in that window unfortunately. */
+
 	tsc_sync_wait();
 
+	cpu_set(smp_processor_id(), cpu_callin_map); // moved from smp_callin by yhlu
+
 	cpu_idle();
 }
 
