Skip to contentSkip to conversation

x86_64: move apic init in init_IRQs

PATCH In review Eric W. Biederman 2 participants 4 messages Oct 4, 2005 – Oct 5, 2005

lore
Share & tools
lore
#0 lore raw

PATCH 2/2 x86_64: move apic init in init_IRQs

All kinds of ugliness exists because we don't initialize the apics during init_IRQs.
- We calibrate jiffies in non apic mode even when we are using apics.
- We have to have special code to initialize the apics when non-smp.
- The legacy i8259 must exist and be setup correctly, even we won't use it past initialization.
- The kexec on panic code must restore the state of the io_apics.
- init/main.c needs a special case for !smp smp_init on x86

In addition to code movement I needed a couple of non-obvious changes:
- Move setup_boot_APIC_clock into APIC_late_time_init for simplicity.
- Use cpu_khz to generate a better approximation of loops_per_jiffies so I can verify the timer interrupt is working.
- Call setup_apic_nmi_watchdog again after cpu_khz is initialized on the boot cpu.

This code is tested and works on both Opterons and Xeons.

---

 arch/x86_64/kernel/apic.c    |   65 +++++++++++++++++++++++++++++++++---------
 arch/x86_64/kernel/i8259.c   |    4 +++
 arch/x86_64/kernel/io_apic.c |    6 +++-
 arch/x86_64/kernel/smpboot.c |   61 +--------------------------------------
 arch/x86_64/kernel/time.c    |    6 ++++
 include/asm-x86_64/apic.h    |    3 +-
 include/asm-x86_64/hw_irq.h  |    1 +
 7 files changed, 71 insertions(+), 75 deletions(-)

14b537ae0d84dcbc726183297bdaf2befc48ba13

arch/x86_64/kernel/apic.c +51 -14
@@ -601,6 +601,7 @@ static int __init detect_init_APIC (void
 
 void __init init_apic_mappings(void)
 {
+	unsigned int orig_boot_cpu_id;
 	unsigned long apic_phys;
 
 	/*
@@ -621,7 +622,11 @@ void __init init_apic_mappings(void)
 	 * Fetch the APIC ID of the BSP in case we have a
 	 * default configuration (or the MP table is broken).
 	 */
+	orig_boot_cpu_id = boot_cpu_id;
 	boot_cpu_id = GET_APIC_ID(apic_read(APIC_ID));
+	if ((orig_boot_cpu_id != -1U) && (orig_boot_cpu_id != boot_cpu_id))
+		printk(KERN_WARNING "Boot APIC ID in local APIC unexpected (%d vs %d)",
+			orig_boot_cpu_id, boot_cpu_id);
 
 #ifdef CONFIG_X86_IO_APIC
 	{
@@ -759,6 +764,7 @@ static unsigned int calibration_result;
 
 void __init setup_boot_APIC_clock (void)
 {
+	unsigned long flags;
 	if (disable_apic_timer) { 
 		printk(KERN_INFO "Disabling APIC timer\n"); 
 		return; 
@@ -767,6 +773,7 @@ void __init setup_boot_APIC_clock (void)
 	printk(KERN_INFO "Using local APIC timer interrupts.\n");
 	using_apic_timer = 1;
 
+	local_save_flags(flags);
 	local_irq_disable();
 
 	calibration_result = calibrate_APIC_clock();
@@ -775,7 +782,7 @@ void __init setup_boot_APIC_clock (void)
 	 */
 	setup_APIC_timer(calibration_result);
 
-	local_irq_enable();
+	local_irq_restore(flags);
 }
 
 void __cpuinit setup_secondary_APIC_clock(void)
@@ -1029,17 +1036,17 @@ asmlinkage void smp_error_interrupt(void
 
 int disable_apic; 
 
+
 /*
- * This initializes the IO-APIC and APIC hardware if this is
- * a UP kernel.
+ * This initializes the IO-APIC and APIC hardware.
  */
-int __init APIC_init_uniprocessor (void)
+int __init APIC_init(void)
 {
-	if (disable_apic) { 
+	if (disable_apic) {
 		printk(KERN_INFO "Apic disabled\n");
-		return -1; 
+		return -1;
 	}
-	if (!cpu_has_apic) { 
+	if (!cpu_has_apic) {
 		disable_apic = 1;
 		printk(KERN_INFO "Apic disabled by BIOS\n");
 		return -1;
@@ -1047,21 +1054,51 @@ int __init APIC_init_uniprocessor (void)
 
 	verify_local_APIC();
 
+	/*
+	 * Should not be necessary because the MP table should list the boot
+	 * CPU too, but we do it for the sake of robustness anyway.
+	 */
+	if (!physid_isset(boot_cpu_id, phys_cpu_present_map)) {
+		printk(KERN_NOTICE "weird, boot CPU (#%d) not listed by the BIOS.\n",
+			boot_cpu_id);
+		physid_set(boot_cpu_id, phys_cpu_present_map);
+	}
+
+	/*
+	 * Switch from PIC to APIC mode.
+	 */
 	connect_bsp_APIC();
+	setup_local_APIC();
 
-	phys_cpu_present_map = physid_mask_of_physid(boot_cpu_id);
-	apic_write_around(APIC_ID, boot_cpu_id);
+#ifdef CONFIG_X86_IO_APIC
+	/*
+	 * Now start the IO-APICs
+	 */
+	if (smp_found_config && !skip_ioapic_setup && nr_ioapics)
+		setup_IO_APIC();
+#endif
+	return 0;
+}
 
-	setup_local_APIC();
+void __init APIC_late_time_init(void)
+{
+	/* Improve our loops per jiffy estimate */
+	loops_per_jiffy = ((1000 + HZ - 1)/HZ)*cpu_khz;
+	boot_cpu_data.loops_per_jiffy = loops_per_jiffy;
+	cpu_data[0].loops_per_jiffy = loops_per_jiffy;
+
+	/* setup_apic_nmi_watchdog doesn't work properly before cpu_khz is
+	 * initialized.  So redo it here to ensure the boot cpu is setup
+	 * properly.  
+	 */
+	if (nmi_watchdog == NMI_LOCAL_APIC)
+		setup_apic_nmi_watchdog();
 
 #ifdef CONFIG_X86_IO_APIC
 	if (smp_found_config && !skip_ioapic_setup && nr_ioapics)
-			setup_IO_APIC();
-	else
-		nr_ioapics = 0;
+		IO_APIC_late_time_init();
 #endif
 	setup_boot_APIC_clock();
-	return 0;
 }
 
 static __init int setup_disableapic(char *str) 
arch/x86_64/kernel/i8259.c +4 -0
@@ -598,4 +598,8 @@ void __init init_IRQ(void)
 
 	if (!acpi_ioapic)
 		setup_irq(2, &irq2);
+
+#ifdef CONFIG_X86_LOCAL_APIC
+	APIC_init();
+#endif
 }
arch/x86_64/kernel/io_apic.c +5 -1
@@ -1760,11 +1760,15 @@ void __init setup_IO_APIC(void)
 	sync_Arb_IDs();
 	setup_IO_APIC_irqs();
 	init_IO_APIC_traps();
-	check_timer();
 	if (!acpi_ioapic)
 		print_IO_APIC();
 }
 
+void __init IO_APIC_late_time_init(void)
+{
+	check_timer();
+}
+
 struct sysfs_ioapic_data {
 	struct sys_device dev;
 	struct IO_APIC_route_entry entry[0];
arch/x86_64/kernel/smpboot.c +2 -59

85 lines not on this page · raw message

arch/x86_64/kernel/time.c +6 -0
@@ -884,6 +884,7 @@ static struct irqaction irq0 = {
 
 extern void __init config_acpi_tables(void);
 
+extern void (*late_time_init)(void);
 void __init time_init(void)
 {
 	char *timename;
@@ -941,6 +942,11 @@ void __init time_init(void)
 
 	set_cyc2ns_scale(cpu_khz / 1000);
 
+#if CONFIG_X86_LOCAL_APIC
+	if (!disable_apic) {
+		late_time_init = APIC_late_time_init;
+	}
+#endif
 #ifndef CONFIG_SMP
 	time_init_gtod();
 #endif
include/asm-x86_64/apic.h +2 -1
@@ -94,7 +94,8 @@ extern void release_lapic_nmi(void);
 extern void disable_timer_nmi_watchdog(void);
 extern void enable_timer_nmi_watchdog(void);
 extern void nmi_watchdog_tick (struct pt_regs * regs, unsigned reason);
-extern int APIC_init_uniprocessor (void);
+extern int APIC_init(void);
+extern void APIC_late_time_init(void);
 extern void disable_APIC_timer(void);
 extern void enable_APIC_timer(void);
 extern void clustered_apic_check(void);
include/asm-x86_64/hw_irq.h +1 -0
@@ -97,6 +97,7 @@ extern void init_8259A(int aeoi);
 extern void FASTCALL(send_IPI_self(int vector));
 extern void init_VISWS_APIC_irqs(void);
 extern void setup_IO_APIC(void);
+extern void IO_APIC_late_time_init(void);
 extern void disable_IO_APIC(void);
 extern void print_IO_APIC(void);
 extern int IO_APIC_get_PCI_irq_vector(int bus, int slot, int fn);
#1 lore raw
Quoted 6 lines · Eric W. Biederman

All kinds of ugliness exists because we don't initialize the apics during init_IRQs.
- We calibrate jiffies in non apic mode even when we are using apics.
- We have to have special code to initialize the apics when non-smp.
- The legacy i8259 must exist and be setup correctly, even we won't use it past initialization.

Actually some setups use its timer even in ACPI mode. Do you take this into account?

In theory it looks reasonable, but I guess it won't work without the bogus watchdog change. That would need to be resolved first.

-Andi

#2 lore raw
Quoted 10 lines · Andi Kleen
Quoted 6 lines · Eric W. Biederman

All kinds of ugliness exists because we don't initialize the apics during init_IRQs.
- We calibrate jiffies in non apic mode even when we are using apics.
- We have to have special code to initialize the apics when non-smp.
- The legacy i8259 must exist and be setup correctly, even we won't use it past initialization.

Actually some setups use its timer even in ACPI mode. Do you take this into account?

The patch is basically code motion and duplication removal with just a few tweaks thrown in to account for the change in location in the kernel boot process when all of occurs.

So everything that was working should continue to work.

Quoted 2 lines

In theory it looks reasonable, but I guess it won't work without the bogus watchdog change. That would need to be resolved first.

Sure. That should be straight forward. The only part that breaks is the watchdog initialization/testing. As I recall there was no obvious place to put it. The call has to happen after everything is running so it is a bit tricky to find a good place to put it.

Eric

#3 lore raw

PATCH 1/1 x86_64: move apic init in init_IRQs (take 2)

In reply to Andi Kleen

Ok here is the x86_64 version again without moving check_nmi_watchdog.

There is a lot that could be done to move check_nmi_watchdog earlier or to make it per cpu. Unfortunately when I tried a per cpu version of check_nmi_watchdog the code failed miserably, on my dual processor hyperthreaded Xeon. Despite everything being setup properly. And it isn't reasonable to call check_nmi_watchdog twice during boot because the second time will take 10 seconds.

All kinds of ugliness exists because we don't initialize the apics during init_IRQs.
- We calibrate jiffies in non apic mode even when we are using apics.
- We have to have special code to initialize the apics when non-smp.
- The legacy i8259 must exist and be setup correctly, even we won't use it past initialization.
- The kexec on panic code must restore the state of the io_apics.
- init/main.c needs a special case for !smp smp_init on x86

In addition to code movement I needed a couple of non-obvious changes:
- Move setup_boot_APIC_clock into APIC_late_time_init for simplicity.
- Use cpu_khz to generate a better approximation of loops_per_jiffies so I can verify the timer interrupt is working.
- Call setup_apic_nmi_watchdog again after cpu_khz is initialized on the boot cpu.

Plus APIC is now capitalized properly in kernel informational messages.

---

 arch/x86_64/kernel/apic.c    |   72 +++++++++++++++++++++++++++++++++---------
 arch/x86_64/kernel/i8259.c   |    4 ++
 arch/x86_64/kernel/io_apic.c |    6 +++-
 arch/x86_64/kernel/smpboot.c |   61 +-----------------------------------
 arch/x86_64/kernel/time.c    |    6 ++++
 include/asm-x86_64/apic.h    |    3 +-
 include/asm-x86_64/hw_irq.h  |    1 +
 7 files changed, 76 insertions(+), 77 deletions(-)

635bab0ceddb7f630750dfb48533d0298f02e8bc

arch/x86_64/kernel/apic.c +56 -16
@@ -601,6 +601,7 @@ static int __init detect_init_APIC (void
 
 void __init init_apic_mappings(void)
 {
+	unsigned int orig_boot_cpu_id;
 	unsigned long apic_phys;
 
 	/*
@@ -621,7 +622,11 @@ void __init init_apic_mappings(void)
 	 * Fetch the APIC ID of the BSP in case we have a
 	 * default configuration (or the MP table is broken).
 	 */
+	orig_boot_cpu_id = boot_cpu_id;
 	boot_cpu_id = GET_APIC_ID(apic_read(APIC_ID));
+	if ((orig_boot_cpu_id != -1U) && (orig_boot_cpu_id != boot_cpu_id))
+		printk(KERN_WARNING "Boot APIC ID in local APIC unexpected (%d vs %d)",
+			orig_boot_cpu_id, boot_cpu_id);
 
 #ifdef CONFIG_X86_IO_APIC
 	{
@@ -759,6 +764,7 @@ static unsigned int calibration_result;
 
 void __init setup_boot_APIC_clock (void)
 {
+	unsigned long flags;
 	if (disable_apic_timer) { 
 		printk(KERN_INFO "Disabling APIC timer\n"); 
 		return; 
@@ -767,6 +773,7 @@ void __init setup_boot_APIC_clock (void)
 	printk(KERN_INFO "Using local APIC timer interrupts.\n");
 	using_apic_timer = 1;
 
+	local_save_flags(flags);
 	local_irq_disable();
 
 	calibration_result = calibrate_APIC_clock();
@@ -775,7 +782,7 @@ void __init setup_boot_APIC_clock (void)
 	 */
 	setup_APIC_timer(calibration_result);
 
-	local_irq_enable();
+	local_irq_restore(flags);
 }
 
 void __cpuinit setup_secondary_APIC_clock(void)
@@ -1029,40 +1036,73 @@ asmlinkage void smp_error_interrupt(void
 
 int disable_apic; 
 
+
 /*
- * This initializes the IO-APIC and APIC hardware if this is
- * a UP kernel.
+ * This initializes the IO-APIC and APIC hardware.
  */
-int __init APIC_init_uniprocessor (void)
+int __init APIC_init(void)
 {
-	if (disable_apic) { 
-		printk(KERN_INFO "Apic disabled\n");
-		return -1; 
+	if (disable_apic) {
+		printk(KERN_INFO "APIC disabled\n");
+		return -1;
 	}
-	if (!cpu_has_apic) { 
+	if (!cpu_has_apic) {
 		disable_apic = 1;
-		printk(KERN_INFO "Apic disabled by BIOS\n");
+		printk(KERN_INFO "APIC disabled by BIOS\n");
 		return -1;
 	}
 
 	verify_local_APIC();
 
+	/*
+	 * Should not be necessary because the MP table should list the boot
+	 * CPU too, but we do it for the sake of robustness anyway.
+	 */
+	if (!physid_isset(boot_cpu_id, phys_cpu_present_map)) {
+		printk(KERN_NOTICE "weird, boot CPU (#%d) not listed by the BIOS.\n",
+			boot_cpu_id);
+		physid_set(boot_cpu_id, phys_cpu_present_map);
+	}
+
+	/*
+	 * Switch from PIC to APIC mode.
+	 */
 	connect_bsp_APIC();
+	setup_local_APIC();
 
-	phys_cpu_present_map = physid_mask_of_physid(boot_cpu_id);
-	apic_write_around(APIC_ID, boot_cpu_id);
+#ifdef CONFIG_X86_IO_APIC
+	/*
+	 * Now start the IO-APICs
+	 */
+	if (smp_found_config && !skip_ioapic_setup && nr_ioapics)
+		setup_IO_APIC();
+#endif
+	return 0;
+}
 
-	setup_local_APIC();
+void __init APIC_late_time_init(void)
+{
+	/* Improve our loops per jiffy estimate */
+	loops_per_jiffy = ((1000 + HZ - 1)/HZ)*cpu_khz;
+	boot_cpu_data.loops_per_jiffy = loops_per_jiffy;
+	cpu_data[0].loops_per_jiffy = loops_per_jiffy;
+
+	/* setup_apic_nmi_watchdog doesn't work properly before cpu_khz is
+	 * initialized.  So redo it here to ensure the boot cpu is setup
+	 * properly.  
+	 */
+	if (nmi_watchdog == NMI_LOCAL_APIC)
+		setup_apic_nmi_watchdog();
 
 #ifdef CONFIG_X86_IO_APIC
 	if (smp_found_config && !skip_ioapic_setup && nr_ioapics)
-			setup_IO_APIC();
-	else
-		nr_ioapics = 0;
+		IO_APIC_late_time_init();
 #endif
 	setup_boot_APIC_clock();
+#ifndef CONFIG_SMP
+	/* SMP kernels perform this check after cpu startup */
 	check_nmi_watchdog();
-	return 0;
+#endif
 }
 
 static __init int setup_disableapic(char *str) 
arch/x86_64/kernel/i8259.c +4 -0
@@ -598,4 +598,8 @@ void __init init_IRQ(void)
 
 	if (!acpi_ioapic)
 		setup_irq(2, &irq2);
+
+#ifdef CONFIG_X86_LOCAL_APIC
+	APIC_init();
+#endif
 }
arch/x86_64/kernel/io_apic.c +5 -1
@@ -1760,11 +1760,15 @@ void __init setup_IO_APIC(void)
 	sync_Arb_IDs();
 	setup_IO_APIC_irqs();
 	init_IO_APIC_traps();
-	check_timer();
 	if (!acpi_ioapic)
 		print_IO_APIC();
 }
 
+void __init IO_APIC_late_time_init(void)
+{
+	check_timer();
+}
+
 struct sysfs_ioapic_data {
 	struct sys_device dev;
 	struct IO_APIC_route_entry entry[0];
arch/x86_64/kernel/smpboot.c +2 -59

85 lines not on this page · raw message

arch/x86_64/kernel/time.c +6 -0
@@ -884,6 +884,7 @@ static struct irqaction irq0 = {
 
 extern void __init config_acpi_tables(void);
 
+extern void (*late_time_init)(void);
 void __init time_init(void)
 {
 	char *timename;
@@ -941,6 +942,11 @@ void __init time_init(void)
 
 	set_cyc2ns_scale(cpu_khz / 1000);
 
+#if CONFIG_X86_LOCAL_APIC
+	if (!disable_apic) {
+		late_time_init = APIC_late_time_init;
+	}
+#endif
 #ifndef CONFIG_SMP
 	time_init_gtod();
 #endif
include/asm-x86_64/apic.h +2 -1
@@ -94,7 +94,8 @@ extern void release_lapic_nmi(void);
 extern void disable_timer_nmi_watchdog(void);
 extern void enable_timer_nmi_watchdog(void);
 extern void nmi_watchdog_tick (struct pt_regs * regs, unsigned reason);
-extern int APIC_init_uniprocessor (void);
+extern int APIC_init(void);
+extern void APIC_late_time_init(void);
 extern void disable_APIC_timer(void);
 extern void enable_APIC_timer(void);
 extern void clustered_apic_check(void);
include/asm-x86_64/hw_irq.h +1 -0
@@ -97,6 +97,7 @@ extern void init_8259A(int aeoi);
 extern void FASTCALL(send_IPI_self(int vector));
 extern void init_VISWS_APIC_irqs(void);
 extern void setup_IO_APIC(void);
+extern void IO_APIC_late_time_init(void);
 extern void disable_IO_APIC(void);
 extern void print_IO_APIC(void);
 extern int IO_APIC_get_PCI_irq_vector(int bus, int slot, int fn);