Skip to contentSkip to conversation

x86_64: EFI64 support

PATCH In review Chandramouli Narayanan 5 participants 16 messages May 2, 2007 – Jun 1, 2007

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover x86_64: EFI64 support

--
Following sets of patches add EFI support to x86_64 architecture. The patches have been tested against 2.6.21 kernel on Intel platforms with EFI1.10 and UEFI2.0 firmware. The patch files document implementation and note issues from testing.

Although the tools below are _not_ needed for building the kernel, the needed bootloader support and associated tools for x86_64 platforms with EFI firmware and specifications are listed below.

1. UEFI specification: http://www.uefi.org

2. Booting EFI64 enabled kernel requires boot loader support. Patches to elilo and gnu-efi library with x86_64 support and documentation have been submitted to respective project maintainers.
elilo: http://sourceforge.net/projects/elilo
gnu-efi library: Nigel Croxon at HP is the current maintainer

3. The tool to convert ELF to PE-COFF image:

	binutils-2.17.50.0.14 supports Intel64 EFI.
	see http://www.kernel.org/pub/linux/devel/binutils/

4. x86_64 platform with EFI/UEFI firmware.

Greatly appreciate your comments on the patches and feedback from testing,

thanks,
Chandramouli Narayanan <[email protected]>

#1 lore raw

PATCH 1/3 x86_64: EFI64 support

General note on EFI x86_64 support
----------------------------------

The following set of patches implements EFI x86_64 Linux kernel support. References to EFI and UEFI (Unified Extensible Firmware Interface) are used interchangeably in the text below.

UEFI specification can be found here: http://www.uefi.org

UEFI x86_64 support is implemented in the following 3 kernel patches. For booting the UEFI x86_64 enabled kernel, bootloader support is required. ELILO with x86_64 support has been submitted to the ELILO project. Please visit the ELILO source link for boot loader source and instructions.

Testing
-------
The x86_64 UEFI kernel patches below have been applied against 2.6.21 and tested with x86_64 ELILO bootloader on Intel platforms with EFI1.10 and UEFI 2.0 firmware. The EFI firmware that I have tested also had CSM supporting compatibility for legacy operating systems.

With this said, the patches require wider testing on systems with EFI64 firmware support.

Mechanics:
---------

- Create a VFAT partition on the disk
- Copy the following to the VFAT partition:
elilo bootloader with x86_64 support and elilo configuration file efi64 kernel image, initrd
- Boot to EFI shell and invoke elilo choosing efi64 kernel image
- On UEFI2.0 firmware systems, pass vga=fbcon for boot messages to appear on console.

Issues noted:
-------------
1. With the patches applied to 2.6.21rc5-git3 kernel, KDE desktop did not get repainted when the system was woken up from sleep. This happened when the screen lock was in effect due to inactivity and the system went to sleep. Following the key press, the system was woken up but KMenu did not show up. The mouse click around the desktop did not have the desired effect, although windows that were open before sleep showed up and keyboard input was taken and the system was operational. Taking the system down to 'init 3' and back to 'init 5' restored the desktop and mouse control.

This behavior did not manifest with the patches applied against 2.6.21rc7-git2 and the final 2.6.21 release.

2. With CALGARY_IOMMU=y in the kernel configuration, the Calgary detection fails with the message "Calgary: Unable to locate Rio Grande Table in EBDA - bailing". However, the legacy kernel has no such error.

3. On some x86_64 systems with EFI1.10 firmware, early boot messages did not appear on console. However, I didn't encounter this behavior on x86_64 systems with UEFI2.0 firmware. This does not appear to be kernel issue but rather firmware/bootloader issue. I will post an update on the status of this issue as I learn more.

EFI x86_64 support Patch 1 of 3
-------------------------------
The main file of this patch is the addition of efi.c for x86_64. This file is modelled after the EFI IA32 avatar. Some x86_64 specifics are worth noting here. EFI initialization and EFI service mapping are implemented in efi.c. NX bit is turned off for EFI runtime service area so that EFI runtime code is executable. On x86_64, parameters passed to UEFI firmware services need to follow the UEFI calling convention. For this purpose, a uefi_call_wrapper() is implemented. EFI calls are wrapped before calling the firmware service. An efi_stub.S to make EFI calls in physical mode with interrupts turned off is added for x86_64.

Boot parameter setup file is updated for x86_64 EFI support. x86_64 EFI boot loader must conform to the EFI boot parameter offsets defined in the file include/asm-x86_64/bootsetup.h (and x86_64 patches submitted to ELILO bootloader conforms to this).

EFI x86_64 build option is added to the kernel configuration.

arch/x86_64/Kconfig +14 -0
diff -uprN -X linux-2.6.21rc7-git2-orig/Documentation/dontdiff linux-2.6.21rc7-git2-orig/arch/x86_64/Kconfig linux-2.6.21rc7-git2-uefi-finaltest/arch/x86_64/Kconfig
@@ -254,6 +254,20 @@ config X86_HT
 	depends on SMP && !MK8
 	default y
 
+config EFI
+	bool "Boot from EFI support (EXPERIMENTAL)"
+	default n
+	---help---
+
+	This enables the the kernel to boot on EFI platforms using
+	system configuration information passed to it from the firmware.
+	This also enables the kernel to use any EFI runtime services that are
+	available (such as the EFI variable services).
+	This option is only useful on systems that have EFI firmware
+	and will result in a kernel image that is ~8k larger. However,
+	even with this option, the resultant kernel should continue to
+	boot on existing non-EFI platforms.
+
 config MATH_EMULATION
 	bool
 
drivers/char/Kconfig +1 -1
diff -uprN -X linux-2.6.21rc7-git2-orig/Documentation/dontdiff linux-2.6.21rc7-git2-orig/drivers/char/Kconfig linux-2.6.21rc7-git2-uefi-finaltest/drivers/char/Kconfig
@@ -837,7 +837,7 @@ config GEN_RTC_X
 
 config EFI_RTC
 	bool "EFI Real Time Clock Services"
-	depends on IA64
+	depends on IA64 || X86_64
 
 config DS1302
 	tristate "DS1302 RTC support"
include/asm-x86_64/bootsetup.h +6 -0
diff -uprN -X linux-2.6.21rc7-git2-orig/Documentation/dontdiff linux-2.6.21rc7-git2-orig/include/asm-x86_64/bootsetup.h linux-2.6.21rc7-git2-uefi-finaltest/include/asm-x86_64/bootsetup.h
@@ -17,6 +17,12 @@ extern char x86_boot_params[BOOT_PARAM_S
 #define APM_BIOS_INFO (*(struct apm_bios_info *) (PARAM+0x40))
 #define DRIVE_INFO (*(struct drive_info_struct *) (PARAM+0x80))
 #define SYS_DESC_TABLE (*(struct sys_desc_table_struct*)(PARAM+0xa0))
+#define EFI_SYSTAB (*((unsigned long *)(PARAM+0x1b8)))
+#define EFI_LOADER_SIG ((unsigned char *)(PARAM+0x1c0))
+#define EFI_MEMDESC_SIZE (*((unsigned int *) (PARAM+0x1c4)))
+#define EFI_MEMDESC_VERSION (*((unsigned int *) (PARAM+0x1c8)))
+#define EFI_MEMMAP_SIZE (*((unsigned int *) (PARAM+0x1cc)))
+#define EFI_MEMMAP (*((unsigned long *)(PARAM+0x1d0)))
 #define MOUNT_ROOT_RDONLY (*(unsigned short *) (PARAM+0x1F2))
 #define RAMDISK_FLAGS (*(unsigned short *) (PARAM+0x1F8))
 #define SAVED_VIDEO_MODE (*(unsigned short *) (PARAM+0x1FA))
arch/x86_64/kernel/efi.c +824 -0

824 lines not on this page · raw message

arch/x86_64/kernel/efi_stub.S +101 -0

101 lines not on this page · raw message

arch/x86_64/kernel/head.S +28 -1
diff -uprN -X linux-2.6.21rc7-git2-orig/Documentation/dontdiff linux-2.6.21rc7-git2-orig/arch/x86_64/kernel/head.S linux-2.6.21rc7-git2-uefi-finaltest/arch/x86_64/kernel/head.S
@@ -94,12 +94,29 @@ startup_32:
 	 * EFER.LMA = 1). Now we want to jump in 64bit mode, to do that we use
 	 * the new gdt/idt that has __KERNEL_CS with CS.L = 1.
 	 */
-	ljmp	$__KERNEL_CS, $(startup_64 - __START_KERNEL_map)
+	ljmp	$__KERNEL_CS, $(long64 - __START_KERNEL_map)
 
 	.code64
 	.org 0x100	
 	.globl startup_64
 startup_64:
+	/*
+	 * At this point the CPU runs in long64 bit with
+	 * paging disabled. First of all, need to load new DS, GDT, and CS.
+	 * There is no stack until we set one up.
+	 */
+
+	/* Initialize the %ds segment register */
+	mov $__KERNEL_DS,%eax
+	mov %eax,%ds
+
+	/* Load new GDT and CS. At this point, BP is in physical mode. */
+	lgdt    cpu_gdt_descr_phys-__START_KERNEL_map
+
+	mov     $(ljumpvector64 - __START_KERNEL_map), %rax
+	ljmp    *(%rax)
+long64:
+
 	/* We come here either from startup_32
 	 * or directly from a 64bit bootloader.
 	 * Since we may have come directly from a bootloader we
@@ -357,6 +374,16 @@ gdt:
 	.endr
 #endif
 
+	.align 16
+	.globl cpu_gdt_descr_phys
+cpu_gdt_descr_phys:
+	.word   gdt_end-cpu_gdt_table-1
+gdt_phys:
+	.quad   cpu_gdt_table-__START_KERNEL_map
+ljumpvector64:
+	.long   long64-__START_KERNEL_map
+	.word   __KERNEL_CS
+
 /* We need valid kernel segments for data and code in long mode too
  * IRET will check the segment types  kkeil 2000/10/28
  * Also sysret mandates a special GDT layout 
arch/x86_64/kernel/Makefile +1 -0
diff -uprN -X linux-2.6.21rc7-git2-orig/Documentation/dontdiff linux-2.6.21rc7-git2-orig/arch/x86_64/kernel/Makefile linux-2.6.21rc7-git2-uefi-finaltest/arch/x86_64/kernel/Makefile
@@ -37,6 +37,7 @@ obj-$(CONFIG_X86_PM_TIMER)	+= pmtimer.o
 obj-$(CONFIG_X86_VSMP)		+= vsmp.o
 obj-$(CONFIG_K8_NB)		+= k8.o
 obj-$(CONFIG_AUDIT)		+= audit.o
+obj-$(CONFIG_EFI)              += efi.o efi_stub.o
 
 obj-$(CONFIG_MODULES)		+= module.o
 obj-$(CONFIG_PCI)		+= early-quirks.o

--

#2 lore raw
Quoted 1 line · On Tue, 01 May 2007 11:59:46 -0700 Chandramouli Narayanan wrote:

EFI x86_64 build option is added to the kernel configuration.

Hi Mouli,

Can you share EFI code as much as possible among ia64, i386, and x86_64 instead of duplicating it?

A diffstat patch summary would be Good.
(see Documentation/SubmittingPatches)

arch/x86_64/Kconfig config X86_HT · line 261

Quoted 12 lines

diff -uprN -X linux-2.6.21rc7-git2-orig/Documentation/dontdiff linux-2.6.21rc7-git2-orig/arch/x86_64/Kconfig linux-2.6.21rc7-git2-uefi-finaltest/arch/x86_64/Kconfig

arch/x86_64/Kconfig +5 -0
@@ -254,6 +254,20 @@ config X86_HT
 	depends on SMP && !MK8
 	default y
 
+config EFI
+	bool "Boot from EFI support (EXPERIMENTAL)"
+	default n
+	---help---
+

No blank line above.
Indent following lines by 2 spaces: i.e., <tab><space><space> as in Documentation/CodingStyle.

Quoted 33 lines
arch/x86_64/Kconfig +9 -0
+	This enables the the kernel to boot on EFI platforms using
+	system configuration information passed to it from the firmware.
+	This also enables the kernel to use any EFI runtime services that are
+	available (such as the EFI variable services).
+	This option is only useful on systems that have EFI firmware
+	and will result in a kernel image that is ~8k larger. However,
+	even with this option, the resultant kernel should continue to
+	boot on existing non-EFI platforms.
+
 config MATH_EMULATION
 	bool
 

diff -uprN -X linux-2.6.21rc7-git2-orig/Documentation/dontdiff linux-2.6.21rc7-git2-orig/include/asm-x86_64/bootsetup.h linux-2.6.21rc7-git2-uefi-finaltest/include/asm-x86_64/bootsetup.h

include/asm-x86_64/bootsetup.h +6 -0
@@ -17,6 +17,12 @@ extern char x86_boot_params[BOOT_PARAM_S
 #define APM_BIOS_INFO (*(struct apm_bios_info *) (PARAM+0x40))
 #define DRIVE_INFO (*(struct drive_info_struct *) (PARAM+0x80))
 #define SYS_DESC_TABLE (*(struct sys_desc_table_struct*)(PARAM+0xa0))
+#define EFI_SYSTAB (*((unsigned long *)(PARAM+0x1b8)))
+#define EFI_LOADER_SIG ((unsigned char *)(PARAM+0x1c0))
+#define EFI_MEMDESC_SIZE (*((unsigned int *) (PARAM+0x1c4)))
+#define EFI_MEMDESC_VERSION (*((unsigned int *) (PARAM+0x1c8)))
+#define EFI_MEMMAP_SIZE (*((unsigned int *) (PARAM+0x1cc)))
+#define EFI_MEMMAP (*((unsigned long *)(PARAM+0x1d0)))
 #define MOUNT_ROOT_RDONLY (*(unsigned short *) (PARAM+0x1F2))
 #define RAMDISK_FLAGS (*(unsigned short *) (PARAM+0x1F8))
 #define SAVED_VIDEO_MODE (*(unsigned short *) (PARAM+0x1FA))

diff -uprN -X linux-2.6.21rc7-git2-orig/Documentation/dontdiff linux-2.6.21rc7-git2-orig/arch/x86_64/kernel/efi.c linux-2.6.21rc7-git2-uefi-finaltest/arch/x86_64/kernel/efi.c

arch/x86_64/kernel/efi.c line 66

Quoted 6 lines
arch/x86_64/kernel/efi.c +5 -0
@@ -0,0 +1,824 @@
 
+extern unsigned long efi_call_phys(void *fp, u64 arg_num, ...);
+struct efi efi;
+EXPORT_SYMBOL(efi);
+struct efi efi_phys __initdata;
+struct efi_memory_map memmap ;

no space before ;

Quoted 4 lines
arch/x86_64/kernel/efi.c +4 -0
+static efi_system_table_t efi_systab __initdata;
+
+static unsigned long efi_rt_eflags;
+static spinlock_t efi_rt_lock = SPIN_LOCK_UNLOCKED;

arch/x86_64/kernel/efi.c line 80

Quoted 10 lines
arch/x86_64/kernel/efi.c +10 -0
+static pgd_t save_pgd;
+
+/* Convert SysV calling convention to EFI x86_64 calling convention */
+
+static efi_status_t uefi_call_wrapper(void *fp, unsigned long va_num, ...)
+{
+	va_list ap;
+	int i;
+	unsigned long args[EFI_ARG_NUM_MAX];
+	unsigned int arg_size,stack_adjust_size;

space after comma.

arch/x86_64/kernel/efi.c line 83

Quoted 3 lines
arch/x86_64/kernel/efi.c +3 -0
+	efi_status_t status;
+
+	if (va_num > EFI_ARG_NUM_MAX || va_num<0)	{
	                                va_num < 0) {

arch/x86_64/kernel/efi.c line 86

Quoted 3 lines
arch/x86_64/kernel/efi.c +3 -0
+		return EFI_LOAD_ERROR;
+	}
+	if (va_num==0)
	if (va_num == 0)

arch/x86_64/kernel/efi.c line 91

Quoted 5 lines
arch/x86_64/kernel/efi.c +5 -0
+		/* There is no need to convert arguments for void argument. */
+		__asm__ __volatile__("call *%0;ret;"::"r"(fp));
+
+	/* The EFI arguments is stored in an array. Then later on it will be 
+	 * pushed into stack or passed to registers according to MS ABI.

passed _to_ registers? passed via or thru registers?

arch/x86_64/kernel/efi.c line 98

Quoted 7 lines
arch/x86_64/kernel/efi.c +7 -0
+	 */
+	va_start(ap, va_num);
+	for (i = 0; i < va_num; i++) {
+		args[i] = va_arg(ap, unsigned long);
+	}
+	va_end(ap);
+	arg_size = va_num*8;
	arg_size = va_num * 8;

arch/x86_64/kernel/efi.c line 99

Quoted 1 line
arch/x86_64/kernel/efi.c +1 -0
+	stack_adjust_size = (va_num > EFI_REG_ARG_NUM? EFI_REG_ARG_NUM : va_num)*8;

Please re-read Documentation/CodingStyle.

Quoted 65 lines
arch/x86_64/kernel/efi.c +65 -0
+
+	/* Starting from here, assembly code makes sure all registers used are
+	 * under controlled by our code itself instead of by gcc.
+	 */
+	/* Start converting SysV calling convention to MS calling convention. */
+	__asm__ __volatile__(
+		/* 0. Save preserved registers. EFI call may clobbered them. */
+		"		pushq %%rbp;pushq %%rbx;pushq %%r12;"
+		"		pushq %%r13;pushq %%r14;pushq %%r15;"
+		/* 1. Push arguments passed by stack into stack. */
+		"		mov %1, %%r12;"
+		"		mov %3, %%r13;"
+		"		mov %1, %%rax;"
+		"		dec %%rax;"
+		"		mov $8, %%bl;"
+		"		mul %%bl;"
+		"		add %%rax, %%r13;"
+		"lstack:"
+		"		cmp $4, %%r12;"
+		"		jle lregister;" 
+		"		pushq (%%r13);"
+		"		sub $8, %%r13;"
+		"		dec %%r12;" 
+		"		jmp lstack;"
+		/* 2. Move arguments passed by registers into registers.
+		 *    rdi->rcx, rsi->rdx, rdx->r8, rcx->r9.
+		 */
+		"lregister:"	
+		"		mov %3, %%r14;"
+		"		mov $0, %%r12;"
+		"lloadregister:"
+		"		cmp %1, %%r12;"
+		"		jge lcall;"
+		"		mov (%%r14), %%rcx;"
+		"		inc %%r12;"
+		"		cmp %1, %%r12;"
+		"		jge lcall;"
+		"		mov 8(%%r14), %%rdx;"
+		"		inc %%r12;"
+		"		cmp %1, %%r12;"
+		"		jge lcall;"
+		"		mov 0x10(%%r14), %%r8;"
+		"		inc %%r12;"
+		"		cmp %1, %%r12;"
+		"		jge lcall;"
+		"		mov 0x18(%%r14), %%r9;"
+		/* 3. Save stack space for those register arguments. */
+		"lcall:				"
+		"		sub %2, %%rsp;"
+		/* 4. Save arg_size to r12 which is preserved in EFI call. */
+		"		mov %4, %%r12;"
+		/* 5. Call EFI function. */
+		"		call *%5;"
+		"		mov %%rax, %0;"
+		/* 6. Restore stack space reserved for those register
+		 * arguments.
+		 */
+		"		add %%r12, %%rsp;"
+		/* 7. Restore preserved registers. */
+		"		popq %%r15;popq %%r14;popq %%r13;"
+		"		popq %%r12;popq %%rbx;popq %%rbp;"
+		: "=r"(status)
+		:"r"((unsigned long)va_num),
+		 "r"((unsigned long)stack_adjust_size),
+		 "r"(args),

arch/x86_64/kernel/efi.c line 189

Quoted 10 lines
arch/x86_64/kernel/efi.c +10 -0
+		 "r"((unsigned long)arg_size),
+		 "r"(fp)
+		:"rsp","rbx","rax","r11","r12","r13","r14","rcx","rdx","r8","r9" 
+		);
+	return status;
+}
+
+static efi_status_t
+_efi_get_wakeup_time(efi_bool_t *enabled, efi_bool_t *pending,
+                                            efi_time_t *tm)

Move 'static efi_status_t' to same line as function name. Move formal parameters down as needed.

arch/x86_64/kernel/efi.c line 208

Quoted 11 lines
arch/x86_64/kernel/efi.c +11 -0
+{
+	return uefi_call_wrapper((void*)efi.systab->runtime->get_wakeup_time,
+					EFI_ARG_NUM_GET_WAKEUP_TIME, 
+					(u64)enabled,
+					(u64)pending,
+					(u64)tm); 
+}
+
+static efi_status_t
+_efi_get_variable(efi_char16_t *name, efi_guid_t *vendor, u32 *attr,
+                                         unsigned long *data_size, void *data)

ditto

arch/x86_64/kernel/efi.c line 221

Quoted 13 lines
arch/x86_64/kernel/efi.c +13 -0
+{
+	return uefi_call_wrapper((void*)efi.systab->runtime->get_variable,
+					EFI_ARG_NUM_GET_VARIABLE, 
+					(u64)name,
+					(u64)vendor,
+					(u64)attr,
+					(u64)data_size,
+					(u64)data); 
+}
+
+static efi_status_t
+_efi_get_next_variable(unsigned long *name_size, efi_char16_t *name,
+                                              efi_guid_t *vendor)

ditto

arch/x86_64/kernel/efi.c line 264

Quoted 13 lines
arch/x86_64/kernel/efi.c +13 -0
+{
+	return uefi_call_wrapper((void*)efi.systab->runtime->get_next_variable,
+					EFI_ARG_NUM_GET_NEXT_VARIABLE, 
+					(u64)name_size,
+					(u64)name,
+					(u64)vendor); 
+}
+
+static efi_status_t
+phys_efi_set_virtual_address_map(unsigned long memory_map_size,
+				 unsigned long descriptor_size,
+				 u32 descriptor_version,
+				 efi_memory_desc_t *virtual_map)

ditto

Quoted 6 lines
arch/x86_64/kernel/efi.c +6 -0
+{
+	efi_status_t status;
+
+	efi_call_phys_prelog();
+	status = efi_call_phys(efi_phys.set_virtual_address_map,
+					EFI_ARG_NUM_SET_VIRTUAL_ADDRESS_MAP,

arch/x86_64/kernel/efi.c line 319

Quoted 10 lines
arch/x86_64/kernel/efi.c +10 -0
+					(unsigned long)memory_map_size,
+					(unsigned long)descriptor_size,
+					(unsigned long)descriptor_version, 
+					(unsigned long)virtual_map);
+	efi_call_phys_epilog();
+	return status;
+}
+
+efi_status_t
+phys_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)

ditto

Quoted 15 lines
arch/x86_64/kernel/efi.c +15 -0
+{
+
+	efi_status_t status;
+
+	efi_call_phys_prelog();
+	status = efi_call_phys(efi_phys.get_time,
+					EFI_ARG_NUM_GET_TIME,
+					(unsigned long)tm,
+					(unsigned long)tc);
+	efi_call_phys_epilog();
+	return status;
+}
+
+inline int efi_set_rtc_mmss(unsigned long nowtime)
+{

arch/x86_64/kernel/efi.c line 344

Quoted 10 lines
arch/x86_64/kernel/efi.c +10 -0
+	int real_seconds, real_minutes;
+	efi_status_t 	status;
+	efi_time_t 	eft;
+	efi_time_cap_t 	cap;
+
+	spin_lock(&efi_rt_lock);
+	status = efi.get_time(&eft, &cap);
+	spin_unlock(&efi_rt_lock);
+	if (status != EFI_SUCCESS) {
+		printk("Ooops: efitime: can't read time!\n");

Creative spelling of Ooops. :)
(also below)

arch/x86_64/kernel/efi.c line 369

Quoted 9 lines
arch/x86_64/kernel/efi.c +9 -0
+		return -1;
+	}
+
+}
+/*
+ * This should only be used during kernel init and before runtime
+ * services have been remapped, therefore, we'll need to call in physical
+ * mode.  Note, this call isn't used later, so mark it __init.
+ */

Confusing comments being adjacent as they are...

Quoted 19 lines
arch/x86_64/kernel/efi.c +19 -0
+/*
+ * This is used during kernel init before runtime
+ * services have been remapped and also during suspend, therefore,
+ * we'll need to call both in physical and virtual modes.
+ */
+inline unsigned long efi_get_time(void)
+{
+	efi_status_t status;
+	efi_time_t eft;
+	efi_time_cap_t cap;
+
+	if (efi.get_time) {
+		/* if we are in virtual mode use remapped function */
+ 		status = efi.get_time(&eft, &cap);
+	} else {
+		/* we are in physical mode */
+		status = phys_efi_get_time(&eft, &cap);
+	}
+	if (status != EFI_SUCCESS)

arch/x86_64/kernel/efi.c line 413

Quoted 10 lines
arch/x86_64/kernel/efi.c +10 -0
+		printk("Oops: efitime: can't read time status: 0x%lx\n",status);
+
+	return mktime(eft.year, eft.month, eft.day, eft.hour,
+			eft.minute, eft.second);
+}
+
+
+/* Make EFI runtime code executable */
+static void
+phys_pmd_init(pmd_t *pmd_page, unsigned long address, unsigned long end)

Fix 'static void'.

Quoted 5 lines
arch/x86_64/kernel/efi.c +5 -0
+{
+	int i = pmd_index(address);
+
+	for (; i < PTRS_PER_PMD && address < end; i++, address += PMD_SIZE) {
+		unsigned long entry;

arch/x86_64/kernel/efi.c line 428

Quoted 10 lines
arch/x86_64/kernel/efi.c +10 -0
+		pmd_t *pmd = pmd_page + pmd_index(address);
+
+		entry = pmd_val(*pmd);
+			entry &= ~_PAGE_NX;
+		set_pmd(pmd, __pmd(entry));
+	}
+}
+
+static void
+phys_pud_init(pud_t *pud_page, unsigned long addr, unsigned long end)

ditto

Quoted 30 lines
arch/x86_64/kernel/efi.c +30 -0
+{ 
+	int i = pud_index(addr);
+
+	for (; i < PTRS_PER_PUD && addr < end; i++, addr += PUD_SIZE ) {
+		pud_t *pud = pud_page + pud_index(addr);
+		pmd_t *pmd;
+
+		if (pud_val(*pud)) {
+			pmd = pmd_offset(pud,0);
+			phys_pmd_init(pmd, addr, end);
+		}
+	}
+}
+
+static void change_rt_pmd(unsigned long start, unsigned long end)
+{ 
+	unsigned long next; 
+
+	start = (unsigned long)__va(start);
+	end = (unsigned long)__va(end);
+
+	for (; start < end; start = next) {
+		pgd_t *pgd = pgd_offset_k(start);
+		pud_t *pud;
+
+		pud = pud_offset(pgd, start & PGDIR_MASK);
+		next = start + PGDIR_SIZE;
+		if (next > end) 
+			next = end; 
+		phys_pud_init(pud, __pa(start), __pa(next));

arch/x86_64/kernel/efi.c line 468

Quoted 10 lines
arch/x86_64/kernel/efi.c +10 -0
+	} 
+	__flush_tlb_all();
+}
+/*
+ * We need to map the EFI memory map again after paging_init().
+ */
+void __init efi_map_memmap(void)
+{
+        efi_memory_desc_t *md;
+        void *p;

Use tab(s) instead of spaces to indent.

arch/x86_64/kernel/efi.c line 476

Quoted 8 lines
arch/x86_64/kernel/efi.c +8 -0
+
+	memmap.map = __va((unsigned long) memmap.phys_map);
+	memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size);
+
+	/* Make EFI runtime code executable */
+        for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
+                md = p;
+               if (md->type == EFI_RUNTIME_SERVICES_CODE && 

Use tabs instead of spaces to indent.

Quoted 50 lines
arch/x86_64/kernel/efi.c +49 -0
+			(__supported_pte_mask & _PAGE_NX))
+			change_rt_pmd(md->phys_addr, md->phys_addr + 
+					(md->num_pages << EFI_PAGE_SHIFT));
+	}
+}
 
+/*
+ * This function will switch the EFI runtime services to virtual mode.
+ * Essentially, look through the EFI memmap and map every region that
+ * has the runtime attribute bit set in its memory descriptor and update
+ * that memory descriptor with the virtual address obtained from ioremap().
+ * This enables the runtime services to be called without having to
+ * thunk back into physical mode for every invocation.
+ */
+void __init efi_enter_virtual_mode(void)
+{
+	efi_memory_desc_t *md;
+	efi_status_t status;
+	unsigned long end;
+	void *p;
+
+	efi.systab = NULL;
+	for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
+		md = p;
+		if (!(md->attribute & EFI_MEMORY_RUNTIME))
+			continue;
+		if (md->attribute & EFI_MEMORY_WB)
+			md->virt_addr = (unsigned long)__va(md->phys_addr);
+		else if (md->attribute & (EFI_MEMORY_UC | EFI_MEMORY_WC))
+			md->virt_addr = (unsigned long)ioremap(md->phys_addr,
+					 md->num_pages << EFI_PAGE_SHIFT);
+		end = md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT);
+		if ((md->phys_addr <= (unsigned long)efi_phys.systab) &&
+				((unsigned long)efi_phys.systab < end))
+			efi.systab = (efi_system_table_t *) 
+					(md->virt_addr - md->phys_addr +
+					(unsigned long)efi_phys.systab);
+	}
+
+	if (!efi.systab)
+		BUG();
+
+	status = phys_efi_set_virtual_address_map(
+			memmap.desc_size * memmap.nr_map,
+			memmap.desc_size,
+			memmap.desc_version,
+		       	memmap.phys_map);
+
+	if (status != EFI_SUCCESS) {
+		printk (KERN_ALERT "You are screwed! "

arch/x86_64/kernel/efi.c line 683

Quoted 10 lines
arch/x86_64/kernel/efi.c +10 -0
+			"Unable to switch EFI into virtual mode "
+			"(status=%lx)\n", status);
+		panic("EFI call to SetVirtualAddressMap() failed!");
+	}
+
+	/*
+	 * Now that EFI is in virtual mode, update the function
+	 * pointers in the runtime service table to the new virtual addresses.
+	 *
+	 * Since x86_64 EFI follows MS calling convention, we can not call

cannot

arch/x86_64/kernel/efi.c line 702

Quoted 7 lines
arch/x86_64/kernel/efi.c +7 -0
+	 * the services directly. We put a wrapper around the real service
+	 * calls and call the wrapper directly.
+	 */
+
+}
+
+void __init

Wrong line.

Quoted 9 lines
arch/x86_64/kernel/efi.c +4 -0
+efi_initialize_iomem_resources(struct resource *code_resource,
+			       struct resource *data_resource)
+{
+}

diff -uprN -X linux-2.6.21rc7-git2-orig/Documentation/dontdiff linux-2.6.21rc7-git2-orig/arch/x86_64/kernel/efi_stub.S linux-2.6.21rc7-git2-uefi-finaltest/arch/x86_64/kernel/efi_stub.S

arch/x86_64/kernel/efi_stub.S line 39

Quoted 5 lines
arch/x86_64/kernel/efi_stub.S +4 -0
@@ -0,0 +1,101 @@
 
+/*
+ * In gcc calling convention, EBX, ESP, EBP, ESI and EDI are all callee save.
+ * So we'd better save all of them at the beginning of this function and restore
+ * at the end no matter how many we use, because we can not assure EFI runtime

cannot

Quoted 2 lines

+ * service functions will comply with gcc calling convention, too.
+ */

---
~Randy
*** Remember to use Documentation/SubmitChecklist when testing your code ***

#3 lore raw
Quoted 2 lines · Randy Dunlap

Can you share EFI code as much as possible among ia64, i386, and x86_64 instead of duplicating it?

Hi Randy,

Based on the feedback from Andi and you, these are the areas:

1. conversion of EFI memory map to e820 map 2. Consolidation/sharing of efi code among the architectures.
3. Coding style violations/typos/clarity in comments.

First I apologize for the coding style violations where I should have done better. I will fix these in the next patch.

I would like to address the above in stages with patch updates. This will take me some time with the fixes and testing.

A while back, Edgar Hucek submitted a patch (probably 2.6.18 mm tree?) for efi to e820 memory mapping for i386.I don't see this code in the 2.6.21 release which makes me wonder whether it was accepted and the history behind it. Any pointers in this regard?

thanks
- mouli

#4 lore raw
Quoted 14 lines · On Thu, 03 May 2007 12:20:30 -0700 chandramouli narayanan wrote:
Quoted 2 lines · Randy Dunlap

Can you share EFI code as much as possible among ia64, i386, and x86_64 instead of duplicating it?

Hi Randy,

Based on the feedback from Andi and you, these are the areas:

1. conversion of EFI memory map to e820 map
2. Consolidation/sharing of efi code among the architectures.
3. Coding style violations/typos/clarity in comments.

First I apologize for the coding style violations where I should have done better. I will fix these in the next patch.

Quoted 7 lines

I would like to address the above in stages with patch updates. This will take me some time with the fixes and testing.

A while back, Edgar Hucek submitted a patch (probably 2.6.18 mm tree?) for efi to e820 memory mapping for i386.I don't see this code in the 2.6.21 release which makes me wonder whether it was accepted and the history behind it. Any pointers in this regard?

I guess that Edgar stopped pushing it. Sometimes persistence is required.

Linus said that the patch "looks fine per se",
http://marc.info/?l=linux-kernel&m=115380564326171&w=2
but needs more testers.

Presumably you have done more testing. :)

---
~Randy
*** Remember to use Documentation/SubmitChecklist when testing your code ***

#5 lore raw
In reply to chandramouli narayanan
Quoted 8 lines · chandramouli narayanan
Quoted 2 lines · Randy Dunlap

Can you share EFI code as much as possible among ia64, i386, and x86_64 instead of duplicating it?

Hi Randy,

Based on the feedback from Andi and you, these are the areas:

1. conversion of EFI memory map to e820 map

for the e820 map, is supposed to bootloader to converting, and pass that via real_mode_data. right?

YH

#6 lore raw
In reply to Chandramouli Narayanan
Quoted 2 lines · On Tuesday 01 May 2007 20:59:46 Chandramouli Narayanan wrote:

General note on EFI x86_64 support
----------------------------------

More review. This code unfortunately has some problems.

First this seems to be quite different from what the 32bit EFI support does (which i suppose is pre UEFI) Are there plans to sync this up eventually?

Also your howto above should be somewhere in Documentation/

Quoted 4 lines

- Create a VFAT partition on the disk
- Copy the following to the VFAT partition:
elilo bootloader with x86_64 support and elilo configuration file efi64 kernel image, initrd

What format is the kernel image?

Quoted 3 lines

- Boot to EFI shell and invoke elilo choosing efi64 kernel image
- On UEFI2.0 firmware systems, pass vga=fbcon for boot messages to appear on console.

They don't have a compat mode for vga anymore?

Quoted 4 lines

2. With CALGARY_IOMMU=y in the kernel configuration, the Calgary detection fails with the message "Calgary: Unable to locate Rio Grande Table in EBDA - bailing". However, the legacy kernel has no such error.

Getting that message when you don't have a IBM Summit system with Calgary is expected. It would be more worrying why the old kernel didn't give it.

arch/x86_64/Kconfig config X86_HT · line 259

Quoted 3 lines
arch/x86_64/Kconfig +3 -0
+config EFI
+	bool "Boot from EFI support (EXPERIMENTAL)"
+	default n

The config should be only added after the feature works -- later in the series.

Drop the default n

arch/x86_64/Kconfig config X86_HT · line 269

Quoted 10 lines
arch/x86_64/Kconfig +10 -0
+	---help---
+
+	This enables the the kernel to boot on EFI platforms using
+	system configuration information passed to it from the firmware.
+	This also enables the kernel to use any EFI runtime services that are
+	available (such as the EFI variable services).
+	This option is only useful on systems that have EFI firmware
+	and will result in a kernel image that is ~8k larger. However,
+	even with this option, the resultant kernel should continue to
+	boot on existing non-EFI platforms.

The description should probably have a reference to the Documentation describing how to set this up.

Mention UEFI?

include/asm-x86_64/bootsetup.h extern char x86_boot_params[BOOT_PARAM_S · line 25

Quoted 6 lines
include/asm-x86_64/bootsetup.h +6 -0
+#define EFI_SYSTAB (*((unsigned long *)(PARAM+0x1b8)))
+#define EFI_LOADER_SIG ((unsigned char *)(PARAM+0x1c0))
+#define EFI_MEMDESC_SIZE (*((unsigned int *) (PARAM+0x1c4)))
+#define EFI_MEMDESC_VERSION (*((unsigned int *) (PARAM+0x1c8)))
+#define EFI_MEMMAP_SIZE (*((unsigned int *) (PARAM+0x1cc)))
+#define EFI_MEMMAP (*((unsigned long *)(PARAM+0x1d0)))

This needs to be documented in Documentation/i386/zero-page.txt

But it might be already obsolete with the early conversion to e820 change?

Quoted 13 lines
arch/x86_64/kernel/efi.c +13 -0
+#define EFI_ARG_NUM_GET_TIME			2
+#define EFI_ARG_NUM_SET_TIME			1
+#define EFI_ARG_NUM_GET_WAKEUP_TIME		3
+#define EFI_ARG_NUM_SET_WAKEUP_TIME		2
+#define EFI_ARG_NUM_GET_VARIABLE		5
+#define EFI_ARG_NUM_GET_NEXT_VARIABLE		3
+#define EFI_ARG_NUM_SET_VARIABLE		5
+#define EFI_ARG_NUM_GET_NEXT_HIGH_MONO_COUNT	1
+#define EFI_ARG_NUM_RESET_SYSTEM		4
+#define EFI_ARG_NUM_SET_VIRTUAL_ADDRESS_MAP	4
+
+#define EFI_ARG_NUM_MAX 10
+#define EFI_REG_ARG_NUM 4

arch/x86_64/kernel/efi.c line 70

Quoted 10 lines
arch/x86_64/kernel/efi.c +10 -0
+
+extern unsigned long efi_call_phys(void *fp, u64 arg_num, ...);
+struct efi efi;
+EXPORT_SYMBOL(efi);
+struct efi efi_phys __initdata;
+struct efi_memory_map memmap ;
+static efi_system_table_t efi_systab __initdata;
+
+static unsigned long efi_rt_eflags;
+static spinlock_t efi_rt_lock = SPIN_LOCK_UNLOCKED;

Each lock needs a comment what it protects and if there is a locking order.

Quoted 1 line

+static pgd_t save_pgd;

That looks dubious, more comments later.

arch/x86_64/kernel/efi.c line 76

Quoted 5 lines
arch/x86_64/kernel/efi.c +5 -0
+
+/* Convert SysV calling convention to EFI x86_64 calling convention */
+
+static efi_status_t uefi_call_wrapper(void *fp, unsigned long va_num, ...)
+{

Any reason you can't do something simple like (untested)

/* rdi, rsi, rdx, rcx, r8, r9 -> rcx,rdx,r8,r9,stack,stack
arg1 function to call */

call_ms:

        mov %rsi,%rcx
        mov %rdx,%rdx
        mov %rcx,%r8
        mov %r8,%r9
        push %r9
        call *%rdi
        pop %r9
        ret

I assume none of the calls has more than 6 arguments. ndiswrapper probably has already tested variants if you're lazy. Then you also wouldn't need the defines for the number of arguments.

Also such code is better written in pure assembly; some versions off gcc don't like clobbering of too many registers.

arch/x86_64/kernel/efi.c

Quoted 6 lines
arch/x86_64/kernel/efi.c +6 -0
+static efi_status_t _efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
+{
+	return uefi_call_wrapper((void*)efi.systab->runtime->get_time,
+					EFI_ARG_NUM_GET_TIME,
+					(u64)tm,
+					(u64)tc

Are the casts really needed?

arch/x86_64/kernel/efi.c line 283

Quoted 10 lines
arch/x86_64/kernel/efi.c +10 -0
+static void efi_call_phys_prelog(void)
+{
+	unsigned long vaddress;
+
+	spin_lock(&efi_rt_lock);
+	local_irq_save(efi_rt_eflags);
+
+	vaddress = (unsigned long)__va(0x0UL);	
+	pgd_val(save_pgd) = pgd_val(*pgd_offset_k(0x0UL));
+	set_pgd(pgd_offset_k(0x0UL), *pgd_offset_k(vaddress));

Who tells you the other CPUs don't use the current pgd? If it's only used in early boot it should be __init. If not it's broken.

Most likely you need to create an own thread with special page tables.

Review

Quoted 1 line
(quoted diff) +1 -0
+	local_flush_tlb();

That won't flush the global mappings.

arch/x86_64/kernel/efi.c line 294

Quoted 9 lines
arch/x86_64/kernel/efi.c +9 -0
+}
+
+static void efi_call_phys_epilog(void)
+{
+	/*
+	 * After the lock is released, the original page table is restored.
+	 */
+	set_pgd(pgd_offset_k(0x0UL), save_pgd);
+	local_flush_tlb();

Same

Quoted 16 lines
arch/x86_64/kernel/efi.c +16 -0
+	local_irq_restore(efi_rt_eflags);
+	spin_unlock(&efi_rt_lock);
+}
+
+static efi_status_t
+phys_efi_set_virtual_address_map(unsigned long memory_map_size,
+				 unsigned long descriptor_size,
+				 u32 descriptor_version,
+				 efi_memory_desc_t *virtual_map)
+{
+	efi_status_t status;
+
+	efi_call_phys_prelog();
+	status = efi_call_phys(efi_phys.set_virtual_address_map,
+					EFI_ARG_NUM_SET_VIRTUAL_ADDRESS_MAP,
+					(unsigned long)memory_map_size,

arch/x86_64/kernel/efi.c line 320

Quoted 10 lines
arch/x86_64/kernel/efi.c +10 -0
+					(unsigned long)descriptor_size,
+					(unsigned long)descriptor_version, 
+					(unsigned long)virtual_map);
+	efi_call_phys_epilog();
+	return status;
+}
+
+efi_status_t
+phys_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
+{

Looks broken -- i think that is called later, so the pgds can be messed up.

Ok there is suspend/resume -- if you're careful you might be able to call this before the other CPUs are put online again. But that is also current being changed to use frozen CPUs. You probably need to coordinate with Rafael.

[i suspect your resume hang is somehow related to this]

Quoted 1 line

+inline int efi_set_rtc_mmss(unsigned long nowtime)

I think that can be called any time so definitely broken regarding page tables.

Quoted 1 line

+inline unsigned long efi_get_time(void)

inline without static usually leads to wasted code because the compiler has to generate an out of line copy.

I don't see why all these functions need to be inline anyways. Best just drop that everywhere and let the compiler decide.

That would probably eliminate some of your 8k.

arch/x86_64/kernel/efi.c line 389

Quoted 2 lines
arch/x86_64/kernel/efi.c +2 -0
+	if (status != EFI_SUCCESS)
+		printk("Oops: efitime: can't read time status: 0x%lx\n",status);

This should have suitable KERN_* prefixes (missing in most printks)

Quoted 1 line

+/* Make EFI runtime code executable */

It would be better to integrate this into the standard page table setup instead of cut'n'pasting so much code here.

arch/x86_64/kernel/efi.c line 471

Quoted 9 lines

+ * We need to map the EFI memory map again after paging_init().
+ */

arch/x86_64/kernel/efi.c +7 -0
+void __init efi_map_memmap(void)
+{
+        efi_memory_desc_t *md;
+        void *p;
+
+	memmap.map = __va((unsigned long) memmap.phys_map);
+	memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size);

White space broken.

arch/x86_64/kernel/efi.c line 484

Quoted 2 lines
arch/x86_64/kernel/efi.c +2 -0
+#if EFI_DEBUG
+void __init print_efi_memmap(void)

The memory map should be probably always printed; it's very useful for debugging. But if you integrate it with e820 that code can do it.

arch/x86_64/kernel/efi.c line 548

Quoted 7 lines
arch/x86_64/kernel/efi.c +7 -0
+	/*
+	 * Show what we know for posterity
+	 */
+	c16 = (efi_char16_t *) early_ioremap(efi.systab->fw_vendor, 2);
+	if (c16) {
+		for (i = 0; i < sizeof(vendor) && *c16; ++i) 
+			vendor[i] = *c16++;

Probably safer to use probe_kernel_address() here and bail out if it's broken.

arch/x86_64/kernel/efi.c line 551

Quoted 3 lines
arch/x86_64/kernel/efi.c +3 -0
+		vendor[i] = '\0';
+	} else
+		printk(KERN_ERR PFX "Could not map the firmware vendor!\n");

EFI should be in the error string

arch/x86_64/kernel/efi.c line 676

Quoted 5 lines
arch/x86_64/kernel/efi.c +5 -0
+	if (status != EFI_SUCCESS) {
+		printk (KERN_ALERT "You are screwed! "
+			"Unable to switch EFI into virtual mode "
+			"(status=%lx)\n", status);
+		panic("EFI call to SetVirtualAddressMap() failed!");

Is the panic really needed?

arch/x86_64/kernel/head.S startup_32: · line 97

Quoted 9 lines

diff -uprN -X linux-2.6.21rc7-git2-orig/Documentation/dontdiff linux-2.6.21rc7-git2-orig/arch/x86_64/kernel/head.S linux-2.6.21rc7-git2-uefi-finaltest/arch/x86_64/kernel/head.S

arch/x86_64/kernel/head.S +1 -1
@@ -94,12 +94,29 @@ startup_32:
 	 * EFER.LMA = 1). Now we want to jump in 64bit mode, to do that we use
 	 * the new gdt/idt that has __KERNEL_CS with CS.L = 1.
 	 */
-	ljmp	$__KERNEL_CS, $(startup_64 - __START_KERNEL_map)
+	ljmp	$__KERNEL_CS, $(long64 - __START_KERNEL_map)

What is the head.S change good for?

-Andi

#7 lore raw
Quoted 10 lines · Andi Kleen
Quoted 3 lines · On Tuesday 01 May 2007 20:59:46 Chandramouli Narayanan wrote:

General note on EFI x86_64 support
----------------------------------

More review. This code unfortunately has some problems.

First this seems to be quite different from what the 32bit EFI support does (which i suppose is pre UEFI) Are there plans to sync this up eventually?

Consolidation of the efi support across architectures needs to be done at some point and this will be a bigger task. But right now my focus is x86_64.

Quoted 2 lines

Also your howto above should be somewhere in Documentation/

Will do.

Quoted 9 lines
Quoted 6 lines

- Create a VFAT partition on the disk
- Copy the following to the VFAT partition:
elilo bootloader with x86_64 support and elilo configuration file efi64 kernel image, initrd

What format is the kernel image?

bzImage

Quoted 8 lines
Quoted 5 lines

- Boot to EFI shell and invoke elilo choosing efi64 kernel image
- On UEFI2.0 firmware systems, pass vga=fbcon for boot messages to appear on console.

They don't have a compat mode for vga anymore?

I'm not sure what you mean by VGA compatibility mode. There is no requirement in [U]EFI for VGA.

Quoted 9 lines
Quoted 5 lines

2. With CALGARY_IOMMU=y in the kernel configuration, the Calgary detection fails with the message "Calgary: Unable to locate Rio Grande Table in EBDA - bailing". However, the legacy kernel has no such error.

Getting that message when you don't have a IBM Summit system with Calgary is expected. It would be more worrying why the old kernel didn't give it.

All right. I will double-check into the older kernel.

Quoted 10 lines

arch/x86_64/Kconfig config X86_HT · line 259

Quoted 5 lines
arch/x86_64/Kconfig +3 -0
+config EFI
+	bool "Boot from EFI support (EXPERIMENTAL)"
+	default n

The config should be only added after the feature works -- later in the series.

Drop the default n

To the extent I have tested, the feature works. Should this still be 'n'?

Quoted 16 lines

arch/x86_64/Kconfig config X86_HT · line 269

Quoted 12 lines
arch/x86_64/Kconfig +10 -0
+	---help---
+
+	This enables the the kernel to boot on EFI platforms using
+	system configuration information passed to it from the firmware.
+	This also enables the kernel to use any EFI runtime services that are
+	available (such as the EFI variable services).
+	This option is only useful on systems that have EFI firmware
+	and will result in a kernel image that is ~8k larger. However,
+	even with this option, the resultant kernel should continue to
+	boot on existing non-EFI platforms.

The description should probably have a reference to the Documentation describing how to set this up.

Quoted 2 lines

Mention UEFI?

Will add to the doc.

Quoted 11 lines

include/asm-x86_64/bootsetup.h extern char x86_boot_params[BOOT_PARAM_S · line 25

Quoted 8 lines
include/asm-x86_64/bootsetup.h +6 -0
+#define EFI_SYSTAB (*((unsigned long *)(PARAM+0x1b8)))
+#define EFI_LOADER_SIG ((unsigned char *)(PARAM+0x1c0))
+#define EFI_MEMDESC_SIZE (*((unsigned int *) (PARAM+0x1c4)))
+#define EFI_MEMDESC_VERSION (*((unsigned int *) (PARAM+0x1c8)))
+#define EFI_MEMMAP_SIZE (*((unsigned int *) (PARAM+0x1cc)))
+#define EFI_MEMMAP (*((unsigned long *)(PARAM+0x1d0)))

This needs to be documented in Documentation/i386/zero-page.txt

Quoted 2 lines

But it might be already obsolete with the early conversion to e820 change?

Not sure if this becomes obsolete with e820 change. I will look into it and add to the doc.

Quoted 19 lines
Quoted 19 lines
arch/x86_64/kernel/efi.c +18 -0
+#define EFI_ARG_NUM_GET_TIME			2
+#define EFI_ARG_NUM_SET_TIME			1
+#define EFI_ARG_NUM_GET_WAKEUP_TIME		3
+#define EFI_ARG_NUM_SET_WAKEUP_TIME		2
+#define EFI_ARG_NUM_GET_VARIABLE		5
+#define EFI_ARG_NUM_GET_NEXT_VARIABLE		3
+#define EFI_ARG_NUM_SET_VARIABLE		5
+#define EFI_ARG_NUM_GET_NEXT_HIGH_MONO_COUNT	1
+#define EFI_ARG_NUM_RESET_SYSTEM		4
+#define EFI_ARG_NUM_SET_VIRTUAL_ADDRESS_MAP	4
+
+#define EFI_ARG_NUM_MAX 10
+#define EFI_REG_ARG_NUM 4
+
+extern unsigned long efi_call_phys(void *fp, u64 arg_num, ...);
+struct efi efi;
+EXPORT_SYMBOL(efi);
+struct efi efi_phys __initdata;
Quoted 10 lines

arch/x86_64/kernel/efi.c line 70

Quoted 6 lines
arch/x86_64/kernel/efi.c +5 -0
+struct efi_memory_map memmap ;
+static efi_system_table_t efi_systab __initdata;
+
+static unsigned long efi_rt_eflags;
+static spinlock_t efi_rt_lock = SPIN_LOCK_UNLOCKED;

Each lock needs a comment what it protects and if there is a locking order.

I will add the comments. Ditto for i386.

Quoted 28 lines
Quoted 2 lines

+static pgd_t save_pgd;

That looks dubious, more comments later.

arch/x86_64/kernel/efi.c line 76

Quoted 6 lines
arch/x86_64/kernel/efi.c +5 -0
+
+/* Convert SysV calling convention to EFI x86_64 calling convention */
+
+static efi_status_t uefi_call_wrapper(void *fp, unsigned long va_num, ...)
+{

Any reason you can't do something simple like (untested)

/* rdi, rsi, rdx, rcx, r8, r9 -> rcx,rdx,r8,r9,stack,stack
arg1 function to call */

call_ms:

        mov %rsi,%rcx
        mov %rdx,%rdx
        mov %rcx,%r8
        mov %r8,%r9
        push %r9
        call *%rdi
        pop %r9
        ret
Quoted 6 lines

I assume none of the calls has more than 6 arguments. ndiswrapper probably has already tested variants if you're lazy. Then you also wouldn't need the defines for the number of arguments.

Also such code is better written in pure assembly; some versions off gcc don't like clobbering of too many registers.

This wrapper code was tested and added to elilo with x86_64 support. There are some efi calls with > 6 args as used in elilo. So, the wrapper code is more elaborate to deal with those cases. Although the efi calls used here in the kernel do not exceed 6 args, for the sake of generality, the same code is being used here. So, is there a better way to handle more number of arguments? I tested with the LIN2WINxx macros from ndiswrapper code ( http://ndiswrapper.sourceforge.net/joomla/) and that works. The LIN2WINxx defines macros to call with a specified set of arguments. For instance, an efi runtime call with one argument should be called with LIN2WIN1() and so on. If this approach is acceptable for inclusion, that is a possible candidate for replacement. I had an issue with the code snippet you provided for an efi call with 5 args to get efi variables. I didn't investigate this further.

Quoted 11 lines

arch/x86_64/kernel/efi.c

Quoted 8 lines
arch/x86_64/kernel/efi.c +6 -0
+static efi_status_t _efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
+{
+	return uefi_call_wrapper((void*)efi.systab->runtime->get_time,
+					EFI_ARG_NUM_GET_TIME,
+					(u64)tm,
+					(u64)tc

Are the casts really needed?

Not needed. I fixed it.

Quoted 16 lines

arch/x86_64/kernel/efi.c line 283

Quoted 12 lines
arch/x86_64/kernel/efi.c +10 -0
+static void efi_call_phys_prelog(void)
+{
+	unsigned long vaddress;
+
+	spin_lock(&efi_rt_lock);
+	local_irq_save(efi_rt_eflags);
+
+	vaddress = (unsigned long)__va(0x0UL);	
+	pgd_val(save_pgd) = pgd_val(*pgd_offset_k(0x0UL));
+	set_pgd(pgd_offset_k(0x0UL), *pgd_offset_k(vaddress));

Who tells you the other CPUs don't use the current pgd? If it's only used in early boot it should be __init. If not it's broken.

This code is called during early boot. This should be __init. Ditto for i386 version. It too does not have __init.

Quoted 45 lines

Most likely you need to create an own thread with special page tables.

Review

Quoted 2 lines
(quoted diff) +1 -0
+	local_flush_tlb();

That won't flush the global mappings.

arch/x86_64/kernel/efi.c line 294

Quoted 10 lines
arch/x86_64/kernel/efi.c +9 -0
+}
+
+static void efi_call_phys_epilog(void)
+{
+	/*
+	 * After the lock is released, the original page table is restored.
+	 */
+	set_pgd(pgd_offset_k(0x0UL), save_pgd);
+	local_flush_tlb();

Same

Quoted 22 lines
arch/x86_64/kernel/efi.c +22 -0
+	local_irq_restore(efi_rt_eflags);
+	spin_unlock(&efi_rt_lock);
+}
+
+static efi_status_t
+phys_efi_set_virtual_address_map(unsigned long memory_map_size,
+				 unsigned long descriptor_size,
+				 u32 descriptor_version,
+				 efi_memory_desc_t *virtual_map)
+{
+	efi_status_t status;
+
+	efi_call_phys_prelog();
+	status = efi_call_phys(efi_phys.set_virtual_address_map,
+					EFI_ARG_NUM_SET_VIRTUAL_ADDRESS_MAP,
+					(unsigned long)memory_map_size,
+					(unsigned long)descriptor_size,
+					(unsigned long)descriptor_version, 
+					(unsigned long)virtual_map);
+	efi_call_phys_epilog();
+	return status;
+}
Quoted 9 lines

arch/x86_64/kernel/efi.c line 320

Quoted 5 lines
arch/x86_64/kernel/efi.c +4 -0
+
+efi_status_t
+phys_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
+{

Looks broken -- i think that is called later, so the pgds can be messed up.

I don't think so. For instance, when efi_get_time is called later, the virtual mode is set up and it does not cause the physical mode call.

Quoted 8 lines

Ok there is suspend/resume -- if you're careful you might be able to call this before the other CPUs are put online again. But that is also current being changed to use frozen CPUs. You probably need to coordinate with Rafael.

[i suspect your resume hang is somehow related to this]

I don't have a suspend/resume hang. The issue I documented was with regard to the behavior of the desktop with just one of the git kernels prior to 2.6.21 release. I tested the suspend/resume with the patch applied to 2.6.21. I tested by directly writing the state to /sys/power/state. However, powersave -U did not seem to do anything. This seems to me a user-mode utility issue rather than the kernel support.

Quoted 6 lines
Quoted 2 lines

+inline int efi_set_rtc_mmss(unsigned long nowtime)

I think that can be called any time so definitely broken regarding page tables.

efi init code sets up efi.get_time to a virtual mode function that can be called later. So, I don't think there is an issue here. Correct me if I'm wrong.

Quoted 4 lines
Quoted 3 lines

+inline unsigned long efi_get_time(void)

Quoted 8 lines

inline without static usually leads to wasted code because the compiler has to generate an out of line copy.

I don't see why all these functions need to be inline anyways. Best just drop that everywhere and let the compiler decide.

That would probably eliminate some of your 8k.

Agreed. However, the function is declared extern in include/linux/efi.h. So, it can not be both static and inline. Ditto for the i386 code too.

Quoted 8 lines

arch/x86_64/kernel/efi.c line 389

Quoted 4 lines
arch/x86_64/kernel/efi.c +2 -0
+	if (status != EFI_SUCCESS)
+		printk("Oops: efitime: can't read time status: 0x%lx\n",status);

This should have suitable KERN_* prefixes (missing in most printks)

Ok, I will fix this. Ditto for i386/efi code.

Quoted 7 lines
Quoted 2 lines

+/* Make EFI runtime code executable */

It would be better to integrate this into the standard page table setup instead of cut'n'pasting so much code here.

I've finally a version that is working with standard page table setup code. By the way, I got rid of the duplicate code. Also, I have integrated EFI memory map into e820 space.

Quoted 14 lines

arch/x86_64/kernel/efi.c line 471

Quoted 11 lines

+ * We need to map the EFI memory map again after paging_init().
+ */

arch/x86_64/kernel/efi.c +7 -0
+void __init efi_map_memmap(void)
+{
+        efi_memory_desc_t *md;
+        void *p;
+
+	memmap.map = __va((unsigned long) memmap.phys_map);
+	memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size);

White space broken.

Will fix.

Quoted 9 lines

arch/x86_64/kernel/efi.c line 484

Quoted 4 lines
arch/x86_64/kernel/efi.c +2 -0
+#if EFI_DEBUG
+void __init print_efi_memmap(void)

The memory map should be probably always printed; it's very useful for debugging. But if you integrate it with e820 that code can do it.

I integrated the EFI memory map into e820 and it takes care of printing that. So, print_efi_memmap() can be gotten rid of.

Quoted 12 lines

arch/x86_64/kernel/efi.c line 548

Quoted 8 lines
arch/x86_64/kernel/efi.c +7 -0
+	/*
+	 * Show what we know for posterity
+	 */
+	c16 = (efi_char16_t *) early_ioremap(efi.systab->fw_vendor, 2);
+	if (c16) {
+		for (i = 0; i < sizeof(vendor) && *c16; ++i) 
+			vendor[i] = *c16++;

Probably safer to use probe_kernel_address() here and bail out if it's broken.

I will make the needed changes.

Quoted 8 lines

arch/x86_64/kernel/efi.c line 551

Quoted 5 lines
arch/x86_64/kernel/efi.c +3 -0
+		vendor[i] = '\0';
+	} else
+		printk(KERN_ERR PFX "Could not map the firmware vendor!\n");

EFI should be in the error string

PFX is set to EFI and that should take care.

Quoted 11 lines

arch/x86_64/kernel/efi.c line 676

Quoted 8 lines
arch/x86_64/kernel/efi.c +5 -0
+	if (status != EFI_SUCCESS) {
+		printk (KERN_ALERT "You are screwed! "
+			"Unable to switch EFI into virtual mode "
+			"(status=%lx)\n", status);
+		panic("EFI call to SetVirtualAddressMap() failed!");

Is the panic really needed?

Probably not needed. This is lifted from i386 efi init code.

Quoted 15 lines

arch/x86_64/kernel/head.S startup_32: · line 98

Quoted 11 lines

diff -uprN -X linux-2.6.21rc7-git2-orig/Documentation/dontdiff linux-2.6.21rc7-git2-orig/arch/x86_64/kernel/head.S linux-2.6.21rc7-git2-uefi-finaltest/arch/x86_64/kernel/head.S

arch/x86_64/kernel/head.S +1 -1
@@ -94,12 +94,29 @@ startup_32:
 	 * EFER.LMA = 1). Now we want to jump in 64bit mode, to do that we use
 	 * the new gdt/idt that has __KERNEL_CS with CS.L = 1.
 	 */
-	ljmp	$__KERNEL_CS, $(startup_64 - __START_KERNEL_map)
+	ljmp	$__KERNEL_CS, $(long64 - __START_KERNEL_map)
 

What is the head.S change good for?

Sorry, this is remnant code that should _not_ have been part of the patch.

Quoted 4 lines

-Andi

I'm testing next set of patches with fixes and post them for review. thanks for feedback,
- mouli

#8 lore raw
In reply to Chandramouli Narayanan

Sorry for the late replay I missed this patch series going by the first time.

Quoted 8 lines
arch/x86_64/kernel/efi.c +8 -0
+static efi_status_t
+phys_efi_set_virtual_address_map(unsigned long memory_map_size,
+				 unsigned long descriptor_size,
+				 u32 descriptor_version,
+				 efi_memory_desc_t *virtual_map)
+{
+	efi_status_t status;
+

arch/x86_64/kernel/efi.c line 316

Quoted 10 lines
arch/x86_64/kernel/efi.c +10 -0
+	efi_call_phys_prelog();
+	status = efi_call_phys(efi_phys.set_virtual_address_map,
+					EFI_ARG_NUM_SET_VIRTUAL_ADDRESS_MAP,
+					(unsigned long)memory_map_size,
+					(unsigned long)descriptor_size,
+					(unsigned long)descriptor_version, 
+					(unsigned long)virtual_map);
+	efi_call_phys_epilog();
+	return status;
+}

Please, Please kill this.

As far as I can tell virtual mode is incompatible with kexec. It is unnecessary because none of the EFI calls are fast path.

Further I believe that using virtual addresses is likely to make things more brittle.

So please drop the EFI virtual mode nonsense.

Thank you,
Eric

#9 lore raw
In reply to Chandramouli Narayanan
Quoted 9 lines · Chandramouli Narayanan

diff -uprN -X linux-2.6.21rc7-git2-orig/Documentation/dontdiff linux-2.6.21rc7-git2-orig/drivers/char/Kconfig linux-2.6.21rc7-git2-uefi-finaltest/drivers/char/Kconfig
--- linux-2.6.21rc7-git2-orig/drivers/char/Kconfig 2007-04-19 12:39:39.000000000 -0700
+++ linux-2.6.21rc7-git2-uefi-finaltest/drivers/char/Kconfig 2007-04-19 13:01:02.000000000 -0700

drivers/char/Kconfig config GEN_RTC_X · line 840

Quoted 5 lines
drivers/char/Kconfig +1 -1
@@ -837,7 +837,7 @@ config GEN_RTC_X
 
 config EFI_RTC
 	bool "EFI Real Time Clock Services"
-	depends on IA64
+	depends on IA64 || X86_64

Please remove this.

We have an architecturally defined hardware realtime clock on x86_64. We don't need EFI to abstract it for us.

My condolences to hardware manufacturers that can't follow a 20 year old standard.

Eric

#10 lore raw

PATCH 2/3 x86_64: EFI64 support

In reply to Chandramouli Narayanan

EFI x86_64 support Patch 2 of 3
-------------------------------
The patch depends on EFI x86_64 patch 1/3.

EFI initialization and memory map set up are done. It should be noted that support for user-defined memory is not yet implemented. Time and reboot services go through the EFI service call for x86_64.

arch/x86_64/kernel/e820.c +102 -6
diff -uprN -X linux-2.6.21rc7-git2-orig/Documentation/dontdiff linux-2.6.21rc7-git2-orig/arch/x86_64/kernel/e820.c linux-2.6.21rc7-git2-uefi-finaltest/arch/x86_64/kernel/e820.c
@@ -17,6 +17,7 @@
 #include <linux/kexec.h>
 #include <linux/module.h>
 #include <linux/mm.h>
+#include <linux/efi.h>
 
 #include <asm/pgtable.h>
 #include <asm/page.h>
@@ -97,11 +98,40 @@ static inline int bad_addr(unsigned long
 /*
  * This function checks if any part of the range <start,end> is mapped
  * with type.
+ * FIXME: here only handle E820_RAM and 0 memory type condition
  */
 int __meminit
 e820_any_mapped(unsigned long start, unsigned long end, unsigned type)
 { 
 	int i;
+	if (efi_enabled) {
+		efi_memory_desc_t *md;
+		void *p;
+		unsigned long map_start, map_end;
+
+		for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
+			md = p;
+			map_start = md->phys_addr;
+			map_end = map_start + (md->num_pages << EFI_PAGE_SHIFT);
+			if (type == E820_RAM) {
+				switch (md->type) {
+				case EFI_CONVENTIONAL_MEMORY:
+				case EFI_BOOT_SERVICES_DATA:
+				case EFI_BOOT_SERVICES_CODE:
+				case EFI_LOADER_DATA:
+				case EFI_LOADER_CODE:
+					if (map_start >= end || map_end <= start)
+						continue;
+					return 1;
+					break;
+				default:
+					continue;
+				}
+			}
+		}
+		return 0;
+	}
+
 	for (i = 0; i < e820.nr_map; i++) { 
 		struct e820entry *ei = &e820.map[i]; 
 		if (type && ei->type != type) 
@@ -148,6 +178,29 @@ int __init e820_all_mapped(unsigned long
 unsigned long __init find_e820_area(unsigned long start, unsigned long end, unsigned size) 
 { 
 	int i; 
+	if (efi_enabled) {
+		efi_memory_desc_t *md;
+		void *p;
+		unsigned long map_start, map_end, last;
+		for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
+			md = p;
+			if (md->type != EFI_CONVENTIONAL_MEMORY)
+				continue;
+			map_start = md->phys_addr;
+			map_end = map_start + (md->num_pages << EFI_PAGE_SHIFT);
+			if (map_start < start)
+				map_start = start;
+			if (map_start > map_end)
+				continue;
+			while (bad_addr(&map_start, size) && map_start+size <= map_end)
+				;
+			last = PAGE_ALIGN(map_start) + size;
+			if (last > map_end || last > end)
+				continue;
+			return map_start;
+		}
+		return -1UL;		
+	}
 	for (i = 0; i < e820.nr_map; i++) { 
 		struct e820entry *ei = &e820.map[i]; 
 		unsigned long addr = ei->addr, last; 
@@ -186,7 +239,7 @@ unsigned long __init e820_end_of_ram(voi
 	if (end_pfn > end_pfn_map) 
 		end_pfn = end_pfn_map; 
 
-	printk("end_pfn_map = %lu\n", end_pfn_map);
+	printk("e820_end_of_ram: end_pfn_map = %lu\n", end_pfn_map);
 	return end_pfn;	
 }
 
@@ -309,6 +362,28 @@ e820_register_active_regions(int nid, un
 {
 	int i;
 	unsigned long ei_startpfn, ei_endpfn;
+	if (efi_enabled) {
+		efi_memory_desc_t *md;
+		void *p;
+		for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
+			md = p;
+			/* add runtime date & runtime service to memory space */
+			if (md->type >= EFI_MEMORY_MAPPED_IO)
+				continue;
+			ei_startpfn = md->phys_addr >> PAGE_SHIFT;
+			ei_endpfn = ei_startpfn + md->num_pages;
+			if (ei_endpfn > end_pfn_map)
+				end_pfn_map = ei_endpfn;
+			/* Obey end_user_pfn to save on memmap */
+			if (ei_startpfn >= end_user_pfn)
+				continue;
+			if (ei_endpfn > end_user_pfn)
+				ei_endpfn = end_user_pfn;
+			add_active_range(nid, ei_startpfn, ei_endpfn);
+		}
+		return;
+	}
+
 	for (i = 0; i < e820.nr_map; i++) {
 		struct e820entry *ei = &e820.map[i];
 		ei_startpfn = round_up(ei->addr, PAGE_SIZE) >> PAGE_SHIFT;
@@ -595,6 +670,11 @@ void early_panic(char *msg)
 
 void __init setup_memory_region(void)
 {
+	if (efi_enabled) {
+		efi_init();
+		return;
+	}
+	
 	/*
 	 * Try to copy the BIOS-supplied E820-map.
 	 *
@@ -664,7 +744,7 @@ early_param("memmap", parse_memmap_opt);
 
 void __init finish_e820_parsing(void)
 {
-	if (userdef) {
+	if (userdef && !efi_enabled) {
 		printk(KERN_INFO "user-defined physical RAM map:\n");
 		e820_print_map("user");
 	}
@@ -685,15 +765,31 @@ __init void e820_setup_gap(void)
 	unsigned long last;
 	int i;
 	int found = 0;
+	efi_memory_desc_t *md;
+	void *p;
 
 	last = 0x100000000ull;
 	gapstart = 0x10000000;
 	gapsize = 0x400000;
-	i = e820.nr_map;
+	if (efi_enabled) {
+		i = memmap.nr_map;
+		p = (void *)memmap.map + i * memmap.desc_size;
+	}
+	else
+		i = e820.nr_map;
 	while (--i >= 0) {
-		unsigned long long start = e820.map[i].addr;
-		unsigned long long end = start + e820.map[i].size;
-
+		unsigned long start;
+		unsigned long end;
+		if (efi_enabled) {
+			p -= memmap.desc_size;
+			md = p;
+			start = md->phys_addr;
+			end = start + (md->num_pages << EFI_PAGE_SHIFT);
+		} else {
+			start = e820.map[i].addr;
+			end = start + e820.map[i].size;
+		}
+		
 		/*
 		 * Since "last" is at most 4GB, we know we'll
 		 * fit in 32 bits if this condition is true
arch/x86_64/kernel/reboot.c +8 -1
diff -uprN -X linux-2.6.21rc7-git2-orig/Documentation/dontdiff linux-2.6.21rc7-git2-orig/arch/x86_64/kernel/reboot.c linux-2.6.21rc7-git2-uefi-finaltest/arch/x86_64/kernel/reboot.c
@@ -7,6 +7,7 @@
 #include <linux/ctype.h>
 #include <linux/string.h>
 #include <linux/pm.h>
+#include <linux/efi.h>
 #include <asm/io.h>
 #include <asm/kdebug.h>
 #include <asm/delay.h>
@@ -116,9 +117,15 @@ void machine_emergency_restart(void)
 {
 	int i;
 
+	/* If EFI enabled, reset system through EFI protocol. */
+	if (efi_enabled) {
+		efi.reset_system(EFI_RESET_WARM, EFI_SUCCESS, 0, NULL);
+		return;
+	}
+
 	/* Tell the BIOS if we want cold or warm reboot */
 	*((unsigned short *)__va(0x472)) = reboot_mode;
-       
+
 	for (;;) {
 		/* Could also try the reset bit in the Hammer NB */
 		switch (reboot_type) { 
arch/x86_64/kernel/setup.c +21 -3
diff -uprN -X linux-2.6.21rc7-git2-orig/Documentation/dontdiff linux-2.6.21rc7-git2-orig/arch/x86_64/kernel/setup.c linux-2.6.21rc7-git2-uefi-finaltest/arch/x86_64/kernel/setup.c
@@ -44,6 +44,7 @@
 #include <linux/dmi.h>
 #include <linux/dma-mapping.h>
 #include <linux/ctype.h>
+#include <linux/efi.h>
 
 #include <asm/mtrr.h>
 #include <asm/uaccess.h>
@@ -69,6 +70,10 @@
  * Machine setup..
  */
 
+#ifdef CONFIG_EFI
+int efi_enabled = 0;
+EXPORT_SYMBOL(efi_enabled);
+#endif
 struct cpuinfo_x86 boot_cpu_data __read_mostly;
 EXPORT_SYMBOL(boot_cpu_data);
 
@@ -234,6 +239,10 @@ void __init setup_arch(char **cmdline_p)
 	rd_prompt = ((RAMDISK_FLAGS & RAMDISK_PROMPT_FLAG) != 0);
 	rd_doload = ((RAMDISK_FLAGS & RAMDISK_LOAD_FLAG) != 0);
 #endif
+#ifdef CONFIG_EFI
+	if (!strncmp(EFI_LOADER_SIG, "EFIL", 4))
+		efi_enabled = 1;
+#endif
 	setup_memory_region();
 	copy_edd();
 
@@ -271,6 +280,8 @@ void __init setup_arch(char **cmdline_p)
 	discover_ebda();
 
 	init_memory_mapping(0, (end_pfn_map << PAGE_SHIFT));
+	if (efi_enabled)
+		efi_map_memmap();
 
 	dmi_scan_machine();
 
@@ -373,6 +384,8 @@ void __init setup_arch(char **cmdline_p)
 	}
 #endif
 
+	if (efi_enabled)
+		reserve_efi_runtime();
 	paging_init();
 
 #ifdef CONFIG_PCI
@@ -404,8 +417,12 @@ void __init setup_arch(char **cmdline_p)
 	/*
 	 * We trust e820 completely. No explicit ROM probing in memory.
  	 */
-	e820_reserve_resources(); 
-	e820_mark_nosave_regions();
+	if (efi_enabled)
+		efi_initialize_iomem_resources(&code_resource, &data_resource);
+	else {
+		e820_reserve_resources(); 
+		e820_mark_nosave_regions();
+	}
 
 	{
 	unsigned i;
@@ -418,7 +435,8 @@ void __init setup_arch(char **cmdline_p)
 
 #ifdef CONFIG_VT
 #if defined(CONFIG_VGA_CONSOLE)
-	conswitchp = &vga_con;
+	if (!efi_enabled || (efi_mem_type(0xa0000) != EFI_CONVENTIONAL_MEMORY))
+		conswitchp = &vga_con;
 #elif defined(CONFIG_DUMMY_CONSOLE)
 	conswitchp = &dummy_con;
 #endif
arch/x86_64/kernel/time.c +12 -1
diff -uprN -X linux-2.6.21rc7-git2-orig/Documentation/dontdiff linux-2.6.21rc7-git2-orig/arch/x86_64/kernel/time.c linux-2.6.21rc7-git2-uefi-finaltest/arch/x86_64/kernel/time.c
@@ -27,6 +27,7 @@
 #include <linux/notifier.h>
 #include <linux/cpu.h>
 #include <linux/kallsyms.h>
+#include <linux/efi.h>
 #include <linux/acpi.h>
 #ifdef CONFIG_ACPI
 #include <acpi/achware.h>	/* for PM timer frequency */
@@ -92,6 +93,11 @@ static void set_rtc_mmss(unsigned long n
  */
 
 	spin_lock(&rtc_lock);
+	if (efi_enabled) {
+		efi_set_rtc_mmss(nowtime);
+		spin_unlock(&rtc_lock);
+		return;
+	}
 
 /*
  * Tell the clock it's being set and stop it.
@@ -204,10 +210,15 @@ static irqreturn_t timer_interrupt(int i
 static unsigned long get_cmos_time(void)
 {
 	unsigned int year, mon, day, hour, min, sec;
-	unsigned long flags;
+	unsigned long flags, retval;
 	unsigned century = 0;
 
 	spin_lock_irqsave(&rtc_lock, flags);
+	if (efi_enabled) {
+		retval = efi_get_time();
+		spin_unlock_irqrestore(&rtc_lock, flags);
+		return retval;
+	}
 
 	do {
 		sec = CMOS_READ(RTC_SECONDS);
arch/x86_64/mm/init.c +5 -2
diff -uprN -X linux-2.6.21rc7-git2-orig/Documentation/dontdiff linux-2.6.21rc7-git2-orig/arch/x86_64/mm/init.c linux-2.6.21rc7-git2-uefi-finaltest/arch/x86_64/mm/init.c
@@ -26,6 +26,7 @@
 #include <linux/dma-mapping.h>
 #include <linux/module.h>
 #include <linux/memory_hotplug.h>
+#include <linux/efi.h>
 
 #include <asm/processor.h>
 #include <asm/system.h>
@@ -296,8 +297,8 @@ static void __meminit phys_pud_init(pud_
 
 		if (addr >= end)
 			break;
-
-		if (!after_bootmem && !e820_any_mapped(addr,addr+PUD_SIZE,0)) {
+		if (!after_bootmem && !efi_enabled && 
+			!e820_any_mapped(addr,addr+PUD_SIZE,0)) {
 			set_pud(pud, __pud(0)); 
 			continue;
 		} 
@@ -659,6 +660,8 @@ void __init reserve_bootmem_generic(unsi
 			return;
 		printk(KERN_ERR "reserve_bootmem: illegal reserve %lx %u\n",
 				phys, len);
+		printk(KERN_ERR "reserve_bootmem: pfn (%lx) end_pfn_map (%u)\n",
+			pfn,end_pfn_map);
 		return;
 	}
 

--

#11 lore raw

arch/x86_64/kernel/e820.c int __init e820_all_mapped(unsigned long · line 182

Quoted 8 lines
arch/x86_64/kernel/e820.c +2 -0
 		if (type && ei->type != type) 
@@ -148,6 +178,29 @@ int __init e820_all_mapped(unsigned long
 unsigned long __init find_e820_area(unsigned long start, unsigned long end, unsigned size) 
 { 
 	int i; 
+	if (efi_enabled) {
+		efi_memory_desc_t *md;

Please convert the memory map from EFI into the e820 map in one place. This would avoid all these weird special cases. Also custom memory map would just work then.

I know i386 doesn't do it this way, but it's wrong and we'll eventually fix it there too.

Also you have a few arbitary and white space changes in there that should be removed.

-Andi

#12 lore raw
Quoted 4 lines · Andi Kleen
Quoted 4 lines
arch/x86_64/kernel/e820.c +0 -0
 		if (type && ei->type != type) 
@@ -148,6 +178,29 @@ int __init e820_all_mapped(unsigned long
 unsigned long __init find_e820_area(unsigned long start, unsigned long end, unsigned size) 
 { 
Quoted 9 lines

arch/x86_64/kernel/e820.c static inline int bad_addr(unsigned long

Quoted 4 lines
arch/x86_64/kernel/e820.c +2 -0
 	int i; 
+	if (efi_enabled) {
+		efi_memory_desc_t *md;
 

Please convert the memory map from EFI into the e820 map in one place. This would avoid all these weird special cases. Also custom memory map would just work then.

I will work on this and provide an update patch.

Quoted 6 lines

I know i386 doesn't do it this way, but it's wrong and we'll eventually fix it there too.

Also you have a few arbitary and white space changes in there that should be removed.

oops! I will fix this too.

Quoted 2 lines

-Andi

- mouli

#13 lore raw
In reply to Chandramouli Narayanan
Quoted 19 lines · Chandramouli Narayanan

diff -uprN -X linux-2.6.21rc7-git2-orig/Documentation/dontdiff linux-2.6.21rc7-git2-orig/arch/x86_64/kernel/reboot.c linux-2.6.21rc7-git2-uefi-finaltest/arch/x86_64/kernel/reboot.c --- linux-2.6.21rc7-git2-orig/arch/x86_64/kernel/reboot.c 2007-02-04 10:44:54.000000000 -0800
+++ linux-2.6.21rc7-git2-uefi-finaltest/arch/x86_64/kernel/reboot.c 2007-04-19 13:01:02.000000000 -0700

arch/x86_64/kernel/reboot.c +1 -0
@@ -7,6 +7,7 @@
 #include <linux/ctype.h>
 #include <linux/string.h>
 #include <linux/pm.h>
+#include <linux/efi.h>
 #include <asm/io.h>
 #include <asm/kdebug.h>
 #include <asm/delay.h>
@@ -116,9 +117,15 @@ void machine_emergency_restart(void)
 {
 	int i;
 

arch/x86_64/kernel/reboot.c void machine_emergency_restart(void) · line 128

Quoted 10 lines
arch/x86_64/kernel/reboot.c +7 -1
+	/* If EFI enabled, reset system through EFI protocol. */
+	if (efi_enabled) {
+		efi.reset_system(EFI_RESET_WARM, EFI_SUCCESS, 0, NULL);
+		return;
+	}
+
 	/* Tell the BIOS if we want cold or warm reboot */
 	*((unsigned short *)__va(0x472)) = reboot_mode;
-       
+

Don't do this.

Instead add an architecture specific pointer that the EFI initialization code can override. That should be a lot less intrusive and more maintainable.

Quoted 19 lines
arch/x86_64/kernel/reboot.c +0 -0
 	for (;;) {
 		/* Could also try the reset bit in the Hammer NB */
 		switch (reboot_type) { 

diff -uprN -X linux-2.6.21rc7-git2-orig/Documentation/dontdiff linux-2.6.21rc7-git2-orig/arch/x86_64/kernel/setup.c linux-2.6.21rc7-git2-uefi-finaltest/arch/x86_64/kernel/setup.c --- linux-2.6.21rc7-git2-orig/arch/x86_64/kernel/setup.c 2007-04-19 12:39:39.000000000 -0700
+++ linux-2.6.21rc7-git2-uefi-finaltest/arch/x86_64/kernel/setup.c 2007-04-19 13:01:02.000000000 -0700

arch/x86_64/kernel/setup.c +1 -0
@@ -44,6 +44,7 @@
 #include <linux/dmi.h>
 #include <linux/dma-mapping.h>
 #include <linux/ctype.h>
+#include <linux/efi.h>
 
 #include <asm/mtrr.h>
 #include <asm/uaccess.h>

arch/x86_64/kernel/setup.c line 76

Quoted 7 lines
arch/x86_64/kernel/setup.c +4 -0
@@ -69,6 +70,10 @@
  * Machine setup..
  */
 
+#ifdef CONFIG_EFI
+int efi_enabled = 0;
+EXPORT_SYMBOL(efi_enabled);
+#endif

Please remove this variable.

I didn't see a single use where it appears to be actually needed.

Eric

#14 lore raw

PATCH 3/3 x86_64: EFI64 support

In reply to Chandramouli Narayanan

EFI x86_64 support Patch 3 of 3
-------------------------------
This patch depends on the EFI x86_64 patches 1/3 and 2/3.

This patch adds Graphics Output Protocol support to the kernel. x86_64 systems with UEFI2.0 firmware conform to UEFI 2.0 specification. UEFI2.0 spec deprecates Universal Graphics Adapter (UGA) protocol and only Graphics Output Protocol (GOP) is produced. Therefore, the boot loader needs to query the UEFI firmware with appropriate Output Protocol and pass the video information to the kernel. As a result of GOP protocol, an EFI framebuffer driver is needed for displaying console messages. Patch 3 of 3 adds a EFI framebuffer driver. The EFI frame buffer driver in this patch is based on the Intel Mac framebuffer driver.

Without the patch 3 of 3, the early console messages will not appear. Nor will switch to the text mode console work.

The x86_64 ELILO bootloader takes care of passing the video information as appropriate for EFI firmware.

drivers/video/efifb.c +252 -0
diff -uprN -X linux-2.6.21rc7-git2-orig/Documentation/dontdiff linux-2.6.21rc7-git2-orig/drivers/video/efifb.c linux-2.6.21rc7-git2-uefi-finaltest/drivers/video/efifb.c
@@ -0,0 +1,252 @@
+/*
+ * framebuffer driver for Intel Based Mac's
+ *
+ * (c) 2006 Edgar Hucek <[email protected]>
+ * Original efi driver written by Gerd Knorr <[email protected]>
+ *
+ */
+
+#include <linux/delay.h>
+#include <linux/errno.h>
+#include <linux/fb.h>
+#include <linux/kernel.h>
+#include <linux/init.h>
+#include <linux/ioport.h>
+#include <linux/mm.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/screen_info.h>
+#include <linux/slab.h>
+#include <linux/string.h>
+#include <linux/dmi.h>
+#include <linux/efi.h>
+
+#include <asm/io.h>
+
+#include <video/vga.h>
+
+static struct fb_var_screeninfo efifb_defined __initdata = {
+	.activate		= FB_ACTIVATE_NOW,
+	.height			= -1,
+	.width			= -1,
+	.right_margin		= 32,
+	.upper_margin		= 16,
+	.lower_margin		= 4,
+	.vsync_len		= 4,
+	.vmode			= FB_VMODE_NONINTERLACED,
+};
+
+static struct fb_fix_screeninfo efifb_fix __initdata = {
+	.id			= "EFI VGA",
+	.type			= FB_TYPE_PACKED_PIXELS,
+	.accel			= FB_ACCEL_NONE,
+	.visual			= FB_VISUAL_TRUECOLOR,
+};
+
+static int efifb_setcolreg(unsigned regno, unsigned red, unsigned green,
+			    unsigned blue, unsigned transp,
+			    struct fb_info *info)
+{
+	/*
+	 *  Set a single color register. The values supplied are
+	 *  already rounded down to the hardware's capabilities
+	 *  (according to the entries in the `var' structure). Return
+	 *  != 0 for invalid regno.
+	 */
+
+	if (regno >= info->cmap.len)
+		return 1;
+
+	if (regno < 16) {
+		red   >>= 8;
+		green >>= 8;
+		blue  >>= 8;
+		((u32 *)(info->pseudo_palette))[regno] =
+			(red   << info->var.red.offset)   |
+			(green << info->var.green.offset) |
+			(blue  << info->var.blue.offset);
+	}
+	return 0;
+}
+
+static struct fb_ops efifb_ops = {
+	.owner		= THIS_MODULE,
+	.fb_setcolreg	= efifb_setcolreg,
+	.fb_fillrect	= cfb_fillrect,
+	.fb_copyarea	= cfb_copyarea,
+	.fb_imageblit	= cfb_imageblit,
+};
+
+static int __init efifb_probe(struct platform_device *dev)
+{
+	struct fb_info *info;
+	int err;
+	unsigned int size_vmode;
+	unsigned int size_remap;
+	unsigned int size_total;
+
+	efifb_fix.smem_start = screen_info.lfb_base;
+	efifb_defined.bits_per_pixel = screen_info.lfb_depth;
+	efifb_defined.xres = screen_info.lfb_width;
+	efifb_defined.yres = screen_info.lfb_height;
+	efifb_fix.line_length = screen_info.lfb_linelength;
+
+	/*   size_vmode -- that is the amount of memory needed for the
+	 *                 used video mode, i.e. the minimum amount of
+	 *                 memory we need. */
+	size_vmode = efifb_defined.yres * efifb_fix.line_length;
+
+	/*   size_total -- all video memory we have. Used for
+	 *                 entries, ressource allocation and bounds
+	 *                 checking. */
+	//size_total = screen_info.lfb_size * 65536;
+	size_total = screen_info.lfb_size;
+	if (size_total < size_vmode)
+		size_total = size_vmode;
+
+	/*   size_remap -- the amount of video memory we are going to
+	 *                 use for efifb.  With modern cards it is no
+	 *                 option to simply use size_total as that
+	 *                 wastes plenty of kernel address space. */
+	size_remap  = size_vmode * 2;
+	if (size_remap < size_vmode)
+		size_remap = size_vmode;
+	if (size_remap > size_total)
+		size_remap = size_total;
+	efifb_fix.smem_len = size_remap;
+
+#ifndef __i386__
+	//screen_info.imacpm_seg = 0;
+#endif
+
+	if (!request_mem_region(efifb_fix.smem_start, size_total, "efifb")) {
+		printk(KERN_WARNING
+		       "efifb: cannot reserve video memory at 0x%lx\n",
+			efifb_fix.smem_start);
+		/* We cannot make this fatal. Sometimes this comes from magic
+		   spaces our resource handlers simply don't know about */
+	}
+
+	info = framebuffer_alloc(sizeof(u32) * 16, &dev->dev);
+	if (!info) {
+		err = -ENOMEM;
+		goto err_release_mem;
+	}
+	info->pseudo_palette = info->par;
+	info->par = NULL;
+
+	info->screen_base = ioremap(efifb_fix.smem_start, efifb_fix.smem_len);
+	if (!info->screen_base) {
+		printk(KERN_ERR "efifb: abort, cannot ioremap video memory "
+				"0x%x @ 0x%lx\n",
+			efifb_fix.smem_len, efifb_fix.smem_start);
+		err = -EIO;
+		goto err_unmap;
+	}
+
+	printk(KERN_INFO "efifb: framebuffer at 0x%lx, mapped to 0x%p, "
+	       "using %dk, total %dk\n",
+	       efifb_fix.smem_start, info->screen_base,
+	       size_remap/1024, size_total/1024);
+	printk(KERN_INFO "efifb: mode is %dx%dx%d, linelength=%d, pages=%d\n",
+	       efifb_defined.xres, efifb_defined.yres,
+	       efifb_defined.bits_per_pixel, efifb_fix.line_length,
+	       screen_info.pages);
+
+	efifb_defined.xres_virtual = efifb_defined.xres;
+	efifb_defined.yres_virtual = efifb_fix.smem_len /
+					efifb_fix.line_length;
+	printk(KERN_INFO "efifb: scrolling: redraw\n");
+	efifb_defined.yres_virtual = efifb_defined.yres;
+
+	/* some dummy values for timing to make fbset happy */
+	efifb_defined.pixclock     = 10000000 / efifb_defined.xres *
+					1000 / efifb_defined.yres;
+	efifb_defined.left_margin  = (efifb_defined.xres / 8) & 0xf8;
+	efifb_defined.hsync_len    = (efifb_defined.xres / 8) & 0xf8;
+
+	efifb_defined.red.offset    = screen_info.red_pos;
+	efifb_defined.red.length    = screen_info.red_size;
+	efifb_defined.green.offset  = screen_info.green_pos;
+	efifb_defined.green.length  = screen_info.green_size;
+	efifb_defined.blue.offset   = screen_info.blue_pos;
+	efifb_defined.blue.length   = screen_info.blue_size;
+	efifb_defined.transp.offset = screen_info.rsvd_pos;
+	efifb_defined.transp.length = screen_info.rsvd_size;
+
+	printk(KERN_INFO "efifb: %s: "
+	       "size=%d:%d:%d:%d, shift=%d:%d:%d:%d\n",
+	       "Truecolor",
+	       screen_info.rsvd_size,
+	       screen_info.red_size,
+	       screen_info.green_size,
+	       screen_info.blue_size,
+	       screen_info.rsvd_pos,
+	       screen_info.red_pos,
+	       screen_info.green_pos,
+	       screen_info.blue_pos);
+
+	efifb_fix.ypanstep  = 0;
+	efifb_fix.ywrapstep = 0;
+
+	info->fbops = &efifb_ops;
+	info->var = efifb_defined;
+	info->fix = efifb_fix;
+	info->flags = FBINFO_FLAG_DEFAULT;
+
+	if (fb_alloc_cmap(&info->cmap, 256, 0) < 0) {
+		err = -ENOMEM;
+		goto err_unmap;
+	}
+	if (register_framebuffer(info)<0) {
+		err = -EINVAL;
+		goto err_fb_dealoc;
+	}
+	printk(KERN_INFO "fb%d: %s frame buffer device\n",
+	       info->node, info->fix.id);
+	return 0;
+
+err_fb_dealoc:
+	fb_dealloc_cmap(&info->cmap);
+err_unmap:
+	iounmap(info->screen_base);
+	framebuffer_release(info);
+err_release_mem:
+	release_mem_region(efifb_fix.smem_start, size_total);
+	return err;
+}
+
+static struct platform_driver efifb_driver = {
+	.probe	= efifb_probe,
+	.driver	= {
+		.name	= "efifb",
+	},
+};
+
+static struct platform_device efifb_device = {
+	.name	= "efifb",
+};
+
+static int __init efifb_init(void)
+{
+	int ret;
+
+	if (!efi_enabled)
+		return -ENODEV;
+
+	if (screen_info.orig_video_isVGA != VIDEO_TYPE_EFI)
+		return -ENODEV;
+
+
+	ret = platform_driver_register(&efifb_driver);
+
+	if (!ret) {
+		ret = platform_device_register(&efifb_device);
+		if (ret)
+			platform_driver_unregister(&efifb_driver);
+	}
+	return ret;
+}
+module_init(efifb_init);
+
+MODULE_LICENSE("GPL");
drivers/video/Kconfig +11 -0
diff -uprN -X linux-2.6.21rc7-git2-orig/Documentation/dontdiff linux-2.6.21rc7-git2-orig/drivers/video/Kconfig linux-2.6.21rc7-git2-uefi-finaltest/drivers/video/Kconfig
@@ -539,6 +539,17 @@ config FB_VESA
 	  You will get a boot time penguin logo at no additional cost. Please
 	  read <file:Documentation/fb/vesafb.txt>. If unsure, say Y.
 
+config FB_EFI
+	bool "EFI-based Framebuffer Support"
+	depends on (FB = y) && X86 && EFI
+	select FB_CFB_FILLRECT
+	select FB_CFB_COPYAREA
+	select FB_CFB_IMAGEBLIT
+	help
+	  This is the EFI frame buffer device driver. If the firmware on
+	  your platform is UEFI2.0, select Y to add support for
+	  Graphics Output Protocol for early console messages to appear.
+
 config FB_IMAC
 	bool "Intel-based Macintosh Framebuffer Support"
 	depends on (FB = y) && X86 && EFI
drivers/video/Makefile +1 -0
diff -uprN -X linux-2.6.21rc7-git2-orig/Documentation/dontdiff linux-2.6.21rc7-git2-orig/drivers/video/Makefile linux-2.6.21rc7-git2-uefi-finaltest/drivers/video/Makefile
@@ -103,6 +103,7 @@ obj-$(CONFIG_FB_SM501)            += sm5
 # Platform or fallback drivers go here
 obj-$(CONFIG_FB_VESA)             += vesafb.o
 obj-$(CONFIG_FB_IMAC)             += imacfb.o
+obj-$(CONFIG_FB_EFI)              += efifb.o
 obj-$(CONFIG_FB_VGA16)            += vga16fb.o vgastate.o
 obj-$(CONFIG_FB_OF)               += offb.o
 
include/linux/screen_info.h +1 -0
diff -uprN -X linux-2.6.21rc7-git2-orig/Documentation/dontdiff linux-2.6.21rc7-git2-orig/include/linux/screen_info.h linux-2.6.21rc7-git2-uefi-finaltest/include/linux/screen_info.h
@@ -63,6 +63,7 @@ extern struct screen_info screen_info;
 #define VIDEO_TYPE_EGAC		0x21	/* EGA in Color Mode		*/
 #define VIDEO_TYPE_VGAC		0x22	/* VGA+ in Color Mode		*/
 #define VIDEO_TYPE_VLFB		0x23	/* VESA VGA in graphic mode	*/
+#define VIDEO_TYPE_EFI		0x24	/* EFI graphic mode		*/
 
 #define VIDEO_TYPE_PICA_S3	0x30	/* ACER PICA-61 local S3 video	*/
 #define VIDEO_TYPE_MIPS_G364	0x31    /* MIPS Magnum 4000 G364 video  */

--

#15 lore raw

drivers/video/efifb.c line 236

Quoted 8 lines · Chandramouli Narayanan
drivers/video/efifb.c +8 -0
+
+static int __init efifb_init(void)
+{
+	int ret;
+
+	if (!efi_enabled)
+		return -ENODEV;
+

Please remove this efi_enabled check it appears redundant with the check immediately below.

Quoted 4 lines
drivers/video/efifb.c +4 -0
+	if (screen_info.orig_video_isVGA != VIDEO_TYPE_EFI)
+		return -ENODEV;
+
+

drivers/video/efifb.c line 250

Quoted 10 lines
drivers/video/efifb.c +10 -0
+	ret = platform_driver_register(&efifb_driver);
+
+	if (!ret) {
+		ret = platform_device_register(&efifb_device);
+		if (ret)
+			platform_driver_unregister(&efifb_driver);
+	}
+	return ret;
+}
+module_init(efifb_init);

Eric