hpet: Enable hidden HPET on NVidia motherboards
PATCH v2 1/1 hpet: Enable hidden HPET on NVidia motherboards
Enables HPET for NVidia motherboards with broken BIOS. The patch reads the HPET address from the pci config space. The patch should also work if ACPI is disabled.
The HPET search is done in early-quirks because even DECLARE_PCI_FIXUP_EARLY was too late. If the new quirk causes problems it can be disabled with the nohpet boot option.
The patch assumes that the BIOS has done the basic setup of HPET, but has not published the result in ACPI tables. This is at least true for some Asus and Gigabyte motherboards.
Patch is against 2.6.21-rc6-git7 but should apply cleanly to most kernels.
---
It looks probable that most NVidia chipsets have the HPET address at 0x44. It might be possible to enable the HPET even if BIOS did not initialize it properly by writing the wanted address there. Some other pci config space bits might need to be fiddled around too, most likely candidates are 0x74 bit 2 and 0xA3 bit ?. One or both of them have been identified to change in some motherboards when HPET is enabled/disabled in BIOS.
x86_64/kernel/early-quirks.c renamed from x86_64/kernel/early-quirks.c.orig +105 -0
@@ -15,6 +15,7 @@#include <asm/pci-direct.h> #include <asm/proto.h> #include <asm/dma.h> +#include <linux/bootmem.h> static void __init via_bugs (void ) {
@@ -36,6 +37,104 @@ static int __init nvidia_hpet_check(stru }#endif +#ifdef CONFIG_HPET + +static u32 nvidia_hpet_chips[]__initdata = { +/* ISA Bridge */ +PCI_VENDOR_ID_NVIDIA | (0x0050 <<16 ), +PCI_VENDOR_ID_NVIDIA | (0x0051 <<16 ), +/* LPC Bridge */ +PCI_VENDOR_ID_NVIDIA | (0x0360 <<16 ), +PCI_VENDOR_ID_NVIDIA | (0x0361 <<16 ), +PCI_VENDOR_ID_NVIDIA | (0x0362 <<16 ), +PCI_VENDOR_ID_NVIDIA | (0x0363 <<16 ), +PCI_VENDOR_ID_NVIDIA | (0x0364 <<16 ), +PCI_VENDOR_ID_NVIDIA | (0x0365 <<16 ), +PCI_VENDOR_ID_NVIDIA | (0x0366 <<16 ), +PCI_VENDOR_ID_NVIDIA | (0x0367 <<16 ), +0 , +}; + +static int __init force_enable_nvidia_hpet (void ) +{ +int num, slot, func; +u32 addr; +struct resource *hpet_res; + +if (hpet_address || nohpet) +return 0 ; + +/* Poor man's PCI discovery */ +for (num =0 ; num <32 ; num++) { +for (slot =0 ; slot <32 ; slot++) { +for (func =0 ; func <8 ; func++) { +u32 class; +u32 vendor; +u8 type; +int i; + class =read_pci_config (num,slot,func, +PCI_CLASS_REVISION ); +if (class ==0xffffffff ) +break ; + vendor =read_pci_config (num, slot, func, +PCI_VENDOR_ID ); + +for (i =0 ; nvidia_hpet_chips[i]; i++) +if (nvidia_hpet_chips[i] == vendor) { + goto found; + } + + type =read_pci_config_byte (num, slot, func, +PCI_HEADER_TYPE ); +if (!(type &0x80 )) +break ; + } + } + } +return 0 ; + + found: + addr =read_pci_config (num, slot, func,0x44 ); +if (!addr) { +return 0 ; + } + +#define HPET_RESOURCE_NAME_SIZE 12 + hpet_res =alloc_bootmem (sizeof (*hpet_res) +HPET_RESOURCE_NAME_SIZE ); +if (!hpet_res) { +/* We could try writing to the 0x44 and see if that is enough +* to enable HPET even if BIOS did not initialize it. +*/ +printk (KERN_INFO "HPET not set up by BIOS.\n" ); +return 0 ; + } + +memset (hpet_res,0 ,sizeof (*hpet_res)); + hpet_res->name = (void *)&hpet_res[1 ]; + hpet_res->flags =IORESOURCE_MEM |IORESOURCE_BUSY ; +strncpy ((char *)hpet_res->name,"NVidia HPET" , +HPET_RESOURCE_NAME_SIZE ); + hpet_res->start = addr; + hpet_res->end = hpet_res->start +HPET_MMAP_SIZE -1 ; + +if (request_resource (&iomem_resource, hpet_res)) { +printk (KERN_INFO "NVidia quirk failed. Could not " +"reserve resources for HPET at base: %#x\n" , addr); +return 0 ; + } + + hpet_address = addr; +printk (KERN_INFO "NVidia quirk. Enabled hidden HPET that BIOS had " +"configured at base: %#x\n" , addr); +return 1 ; +} +#else +static int __init force_enable_nvidia_hpet (void ) +{ +return 0 ; +} +#endif +static void __init nvidia_bugs (void ) {#ifdef CONFIG_ACPI
@@ -50,6 +149,10 @@ static void __init nvidia_bugs(void)return ;if (acpi_table_parse (ACPI_SIG_HPET , nvidia_hpet_check)) { +if (force_enable_nvidia_hpet ()) { +return ; + } + acpi_skip_timer_override =1 ;printk (KERN_INFO "Nvidia board " "detected. Ignoring ACPI "
@@ -57,6 +160,8 @@ static void __init nvidia_bugs(void)printk (KERN_INFO "If you got timer trouble " "try acpi_use_timer_override\n"); } +#else +force_enable_nvidia_hpet ();#endif /* RED-PEN skip them on mptables too? */
Quoted 11 lines · Mikko Tiihonen
Enables HPET for NVidia motherboards with broken BIOS. The patch reads the HPET address from the pci config space. The patch should also work if ACPI is disabled.
The HPET search is done in early-quirks because even
DECLARE_PCI_FIXUP_EARLYwas too late. If the new quirk causes problems it can be disabled with the nohpet boot option.The patch assumes that the BIOS has done the basic setup of HPET, but has not published the result in ACPI tables. This is at least true for some Asus and Gigabyte motherboards.
I think the code should only be enabled by its CONFIG_*.
Signature · 1 line
Krzysztof Halasa
Quoted 10 lines · Krzysztof Halasa
Quoted 8 lines · Mikko Tiihonen
Enables HPET for NVidia motherboards with broken BIOS. The patch reads the HPET address from the pci config space. The patch should also work if ACPI is disabled.
The HPET search is done in early-quirks because even
DECLARE_PCI_FIXUP_EARLYwas too late. If the new quirk causes problems it can be disabled with the nohpet boot option.
Quoted 5 lines
Quoted 3 lines
The patch assumes that the BIOS has done the basic setup of HPET, but has not published the result in ACPI tables. This is at least true for some Asus and Gigabyte motherboards.
I think the code should only be enabled by its CONFIG_*.
Currently the all the code is protected by CONFIG_HPET. Are you proposing to use for example CONFIG_HPET_NVIDIA instead?
I think that the code should be safe even without it's own config option. It won't override values from ACPI tables and can still be disabled with nohpet boot option. Also all the code and data are __init.
-Mikko
Quoted 2 lines · Mikko Tiihonen
Currently the all the code is protected by
CONFIG_HPET. Are you proposing to use for exampleCONFIG_HPET_NVIDIAinstead?
Something like that. CONFIG_HPET_NFORCE_DETECT maybe?
Quoted 1 line
I think that the code should be safe even without it's own config option.
Well, who knows. The hardware side (config bits) are probably safe and easy to verify, but depending on BIOS assigning address to inactive device seems risky.
Signature · 1 line
Krzysztof Halasa
Quoted 2 lines · Mikko Tiihonen
It looks probable that most NVidia chipsets have the HPET address at
0x44. It might be possible to enable the HPET even if BIOS did not
That seems like a dangerous assumption. If anything this needs to be keyed on specific PCI IDs.
And the way you coded a recursive PCI scan is just .... <looking for words, censoring some that come to mind first> ... not nice.
Quoted 5 lines
initialize it properly by writing the wanted address there. Some other pci config space bits might need to be fiddled around too, most likely candidates are
0x74bit 2 and0xA3bit ?. One or both of them have been identified to change in some motherboards when HPET is enabled/disabled in BIOS.
Or just add a random generator and poke random bits? Should be roughly equivalent.
Also there should be done anything here without confirmation from Nvidia that HPET is actually supposed to work. Sometimes hardware is disabled by BIOS because it is seriously broken (there was at least one other chipset that could corrupt your flash if you force enabled HPET in some steppings)
-Andi
Quoted 7 lines · Andi Kleen
Quoted 2 lines · Mikko Tiihonen
It looks probable that most NVidia chipsets have the HPET address at
0x44. It might be possible to enable the HPET even if BIOS did notThat seems like a dangerous assumption. If anything this needs to be keyed on specific PCI IDs.
The patch contains a list of PCI IDs. Currently the CK804 and MCP55 have been verified to work. Other PCI IDs can be added if needed.
Quoted 2 lines
And the way you coded a recursive PCI scan is just .... <looking for words, censoring some that come to mind first> ... not nice.
I actually was more worried that someone might complain that the pci scanning is copy & paste code from end of the same file. I did try to use the generic pci functions first but because they insist on enabling interrupts they cannot be used this early. And this code needs to be run before the timer initialization.
If you want I can submit a separate patch to move the ... not nice pci scanning code to pci directory under some early_pci_scan(u32 *pci_ids, hook) function. The same code was already cut&pasted in i386/kernel/acpi/earlyquik.c, in x86_64/kernel/aperture.c and in x86_64/kernel/early-quirks.c. Moving the uglyness to a central place would at least hide it from the casual browser. Or would a global flag that the pci scanning code checks to see if locks should be used or not be better?
Quoted 8 lines
Quoted 5 lines
initialize it properly by writing the wanted address there. Some other pci config space bits might need to be fiddled around too, most likely candidates are
0x74bit 2 and0xA3bit ?. One or both of them have been identified to change in some motherboards when HPET is enabled/disabled in BIOS.Or just add a random generator and poke random bits? Should be roughly equivalent.
Fair enough, that was just my written hope that some day someone might reverse engineer how HPET is enabled on NVidia chipsets. The patch does not try write to any registers so it should be safe? The code also properly checks that the memory area does not collide with any existing resource. I could of course add
a check that there is a HPET at that address, but the hpet driver already checks it itself later and disabled itself it cannot see valid data.
Quoted 5 lines
Also there should be done anything here without confirmation from Nvidia that HPET is actually supposed to work. Sometimes hardware is disabled by BIOS because it is seriously broken (there was at least one other chipset that could corrupt your flash if you force enabled HPET in some steppings)
I hope someone has some secret contacts at NVidia because they have not been very open with their chipsets. I looked at LinuxBios and their NForce4 chipset code had just had commented out code that wrote to 0x44 register. So obviously something more is needed.
Even Intel just posted code a while ago that allows enabling HPET from a quirk even if BIOS did not set it up properly.
Does anyone know how to _really_ test if HPET works properly (from user space for example). I've just tested with busylooping gettimeofday while changing the clocksource and measured the speed difference. We could then change the quirk to point to instructions on how to test the HPET manually and then request to submit the PCI ID.
-Mikko
Quoted 5 lines · Mikko Tiihonen
I actually was more worried that someone might complain that the pci scanning is copy & paste code from end of the same file. I did try to use the generic pci functions first but because they insist on enabling interrupts they cannot be used this early. And this code needs to be run before the timer initialization.
Yes that's the issue. You're adding another PCI scanner copy'n'pasted from the caller of the function you're adding it to. See the problem?
Quoted 3 lines
If you want I can submit a separate patch to move the ... not nice pci scanning code to pci directory under some
early_pci_scan(u32 *pci_ids, hook)function. The same code was already cut&pasted in
That is what early-quirks is anyways. But the way to scan for multple things is not to add anther recursive scan, but to just extend or change the main loop.
Quoted 10 lines
Quoted 5 lines
Also there should be done anything here without confirmation from Nvidia that HPET is actually supposed to work. Sometimes hardware is disabled by BIOS because it is seriously broken (there was at least one other chipset that could corrupt your flash if you force enabled HPET in some steppings)
I hope someone has some secret contacts at NVidia because they have not been very open with their chipsets. I looked at LinuxBios and their NForce4 chipset code had just had commented out code that wrote to
0x44register. So obviously something more is needed.
Andy, can you help please? There is interest in force enabling HPET on boards where the BIOS didn't chose too. We would need a list of PCI-IDs where this is safe to do and what bits to poke. Thanks.
-Andi
Quoted 9 lines · Andi Kleen
Quoted 5 lines · Mikko Tiihonen
I actually was more worried that someone might complain that the pci scanning is copy & paste code from end of the same file. I did try to use the generic pci functions first but because they insist on enabling interrupts they cannot be used this early. And this code needs to be run before the timer initialization.
Yes that's the issue. You're adding another PCI scanner copy'n'pasted from the caller of the function you're adding it to. See the problem?
I'll remove the duplicated scanning code in next patch version by creating a more general scanning code.
Quoted 7 lines
Quoted 3 lines
If you want I can submit a separate patch to move the ... not nice pci scanning code to pci directory under some
early_pci_scan(u32 *pci_ids, hook)function. The same code was already cut&pasted inThat is what early-quirks is anyways. But the way to scan for multple things is not to add anther recursive scan, but to just extend or change the main loop.
I don't think the quirk can avoid a recursive scan easily. It is supposed to be only run if the first scan finds nvidia chipset, does not detect acpi hpet and, as a last resort, tries to scan for the hpet manually to avoid enabling the original quirk. In more pictoresque way:
scan-for-chipsets
-> nvidia chipset found
-> does acpi hpet exist
-> did manual_hpet_quirk fail (new)
-> enable acpi_skip_timer_override quirkIt could be done in one scanning loop provided that it is quaranteed that the nvidia chipset is always before the the hpet quirk chip in pci hierarchy. Then the nvidia quirk needs to be split to two parts, of which the last part is run after all other quirks and to check if hpet quirk was ever activated.
scan-for-pci-ids
-> nvidia chipset id found
-> does acpi hpet exist
-> possibly_enable_acpi_skip_timer_override = 1
-> nvidia pci-id matching hpet chip found
-> if possibly_enable_acpi_skip_timer_override
-> did manual_hpet_quirk work
-> possibly_enable_acpi_skip_timer_override = 0if possibly_enable_acpi_skip_timer_override
-> enable acpi_skip_timer_override quirk
But I really do think that overall the first solution is better, even if it does a recursive scan. Assuming of course that stupid code duplication is removed.
-Mikko
Quoted 3 lines
It could be done in one scanning loop provided that it is quaranteed that the nvidia chipset is always before the the hpet quirk chip in pci hierarchy. Then the nvidia quirk needs to be split to two parts, of which
You just only set flags for both in the scanning loop and then handle it after every device has been visited.
It's a simple state machine.
-Andi
PATCH v3 1/1 hpet: Detect hidden HPET on NVidia motherboards
In reply to Andi KleenEnables HPET for NVidia motherboards with broken BIOS. The patch reads the HPET address from the pci config space. The patch should also work if ACPI is disabled.
The new quirk activates use of HPET only run if
- CONFIG_HPET_NFORCE_DETECT is enabled
- nohpet boot option is not set
- main chipset is from NVidia
- ACPI tables do not list HPET
- matching PCI ID for device with HPET is found
- BIOS has set up the HPET to some address
- there is no other resource allocated at the HPET address
This is true at least for some Asus, Gigabyte and DFI motherboards.
Patch is against 2.6.21-rc6-git7 but should apply cleanly to most kernels.
---
Changes since v2:
- removed duplicated pci scanning code and moved it to pci/early.c
- added CONFIG_HPET_NFORCE_DETECT
arch/i386/pci/early.c +47 -0
@@ -57,3 +57,50 @@ int early_pci_allowed(void)return (pci_probe & (PCI_PROBE_CONF1 |PCI_PROBE_NOEARLY )) ==PCI_PROBE_CONF1 ; } + +void __init early_pci_scan (struct pci_entry matching[]) +{ +int num, slot, func; + +/* Poor man's PCI discovery */ +for (num =0 ; num <32 ; num++) { +for (slot =0 ; slot <32 ; slot++) { +for (func =0 ; func <8 ; func++) { +u32 class; +u32 vendor; +u8 type; +int i; + class =read_pci_config (num,slot,func, +PCI_CLASS_REVISION ); +if (class ==0xffffffff ) +break ; + + class >>=16 ; + + vendor =read_pci_config (num, slot, func, +PCI_VENDOR_ID ); + +for (i =0 ; matching[i].f; i++) { +if (class != matching[i].class) +continue ; + +if ((vendor &0xffff ) != matching[i].vendor) +continue ; + +if (matching[i].device !=0xffff && + (vendor >>16 ) != matching[i].device) +continue ; + + matching[i].f (num, slot, func); +return ; + } + +/* No multi-function device? */ + type =read_pci_config_byte (num,slot,func, +PCI_HEADER_TYPE ); +if (!(type &0x80 )) +break ; + } + } + } +}
arch/x86_64/Kconfig +9 -0
@@ -453,6 +453,15 @@ config HPET_EMULATE_RTC bool "Provide RTC interrupt" depends on HPET_TIMER && RTC=y +config HPET_NFORCE_DETECT + bool "Detect HPET on NForce motherboards with broken BIOS" + depends on HPET_TIMER + default y + help + Some buggy BIOS implementations do not list HPET in ACPI + tables but still have the HPET enabled. This option allows + using HPET on such motherboards with NForce chipsets. + # Mark as embedded because too many people got it wrong. # The code disables itself when not needed. config IOMMU
arch/x86_64/kernel/early-quirks.c +89 -49
186 lines not on this page · raw message
include/asm-x86_64/pci-direct.h +15 -0
@@ -6,6 +6,20 @@/* Direct PCI access. This is used for PCI accesses in early boot before the PCI subsystem works. */ +/** +* struct pci_entry - simple pci device matching struct with callback +* @class: class to match - mandatory +* @vendor: vendor to match - mandatory +* @device: device to match - use 0xffff to ignore +* @f: callback function - called on matching entry +*/ +struct pci_entry { +u16 class; +u16 vendor; +u16 device; +void (*f)(int num,int slot,int func); +}; +extern u32 read_pci_config (u8 bus,u8 slot,u8 func,u8 offset);extern u8 read_pci_config_byte (u8 bus,u8 slot,u8 func,u8 offset);extern u16 read_pci_config_16 (u8 bus,u8 slot,u8 func,u8 offset);
@@ -13,5 +27,6 @@ extern void write_pci_config(u8 bus, u8extern void write_pci_config_byte (u8 bus,u8 slot,u8 func,u8 offset,u8 val);extern int early_pci_allowed (void ); +extern void early_pci_scan (struct pci_entry matching[]);#endif
Re: [PATCH v3] hpet: Detect hidden HPET on NVidia motherboards
Quoted 13 lines · On Wed, 18 Apr 2007 00:57:48 +0300 Mikko Tiihonen <[email protected]> wrote:
Enables HPET for NVidia motherboards with broken BIOS. The patch reads the HPET address from the pci config space. The patch should also work if ACPI is disabled.
The new quirk activates use of HPET only run if
-CONFIG_HPET_NFORCE_DETECTis enabled
- nohpet boot option is not set
- main chipset is from NVidia
- ACPI tables do not list HPET
- matching PCI ID for device with HPET is found
- BIOS has set up the HPET to some address
- there is no other resource allocated at the HPET address
Quoted 4 lines
This is true at least for some Asus, Gigabyte and DFI motherboards.
Patch is against 2.6.21-rc6-git7 but should apply cleanly to most kernels.
I looked at applying this but
a) there have been rather a lot of underlying changes in Andi's devel tree and
b) we still haven't heard from Andy?
Re: [PATCH v3] hpet: Detect hidden HPET on NVidia motherboards
Quoted 3 lines
a) there have been rather a lot of underlying changes in Andi's devel tree and
b) we still haven't heard from Andy?
c) my original review comments haven't been completely addressed.
-Andi