Page 1 of 1

Detecting PCI bus

Posted: Mon Apr 02, 2012 3:22 pm
by iocoder
Hello!
is there a way to detect whether there is a PCI or not?? without going into multiboot specification.... i like to know how an OS can determine whether this platform enables a PCI bus or just an ISA bus..

thanks in advance :)

EDIT: multiprocessor* specification

Re: Detecting PCI bus

Posted: Mon Apr 02, 2012 3:58 pm
by bluemoon
Check the wiki: PCI

To detect PCI device you can scan thru' all nodes.
In my detection I do this:

Code: Select all

  // PCI driver previously loaded from initrd
    pci = (DRIVER_BUS*) g_driver_mgr.find (kDriverType_Bus, 'pci ');

    for ( unsigned int bus=0; bus<256; bus++ ) {
        for ( unsigned int slot=0; slot<32; slot++ ) {
            for ( unsigned int func=0; func<7; func++ ) {
                if ( pci->get_info(pci, bus, slot, func, &info ) < 0 ) break;
                kprintf ("PCI   : bus:%d slot:%d func:%d  device:%X vendor:%X class:%d:%d\n",
                         bus, slot, func, info.device_id, info.vendor_id, info.class_code, info.subclass_code );

                // Do stuff here
            }
            if ( func==0 ) break;
        }
    }

Re: Detecting PCI bus

Posted: Mon Apr 02, 2012 4:06 pm
by iocoder
Thanks bluemoon but you got me wrong :( i meant to detect the PCI system itself ;) to detect whether there is a PCI system or not :)) is that possible??

Re: Detecting PCI bus

Posted: Mon Apr 02, 2012 4:11 pm
by Jezze
I've heard that you can run into trouble detecting like that because you are looking in memory ranges that might contain random data. It is possible to just check exactly the areas occupied by real devices. It is not as straight forward but it looks something like this.

Code: Select all

static unsigned int scan(unsigned int num)
{

    unsigned int slot;

    for (slot = 0; slot < 32; slot++)
    {

        unsigned int address = get_address(num, slot, 0x00);

        if (pci_inw(address, 0x00) == 0xFFFF)
            continue;

        unsigned short header = pci_inb(address, 0x0E);

        if ((header & 0x01))
            scan(pci_inb(address, 0x19));

        if ((header & 0x02))
            scan(pci_inb(address, 0x18));

        if ((header & 0x80))
        {

            unsigned int function;

            for (function = 0; function < 8; function++)
            {

                unsigned int address = get_address(num, slot, function);

                if (pci_inw(address, 0x00) == 0xFFFF)
                    continue;

                add_device(num, slot, function);

            }

            continue;

        }

        add_device(num, slot, 0);

    }

    return 0;

}

scan(0);


Re: Detecting PCI bus

Posted: Mon Apr 02, 2012 5:32 pm
by bluemoon
To detect if PCI exist at all, you may:
1. call BIOS 1A
2. ACPI
3. just assume it's there and if not, blame the user for non-supported computer.

Re: Detecting PCI bus

Posted: Mon Apr 02, 2012 5:39 pm
by Cognition
Initially you detect the presence of PCI through either the MCFG table through ACPI's table interface or the system firmware via BIOS32/Int 0x1A.

Re: Detecting PCI bus

Posted: Mon Apr 02, 2012 5:53 pm
by Brendan
Hi,
iocoder wrote:is there a way to detect whether there is a PCI or not??
Yes (sort of).

Normally you'd want to know if PCI exists, and if it does, which mechanism you have to use to access PCI configuration space. There are 4 different cases:
  • No PCI at all
  • mechanism #1
  • mechanism #2
  • PCI Express memory mapped configuration space (with mechanism #1 too, for backward compatibility)
Start by checking for the existence of an MCFG ACPI table. If this table exists then you've got PCI Express memory mapped configuration space (with mechanism #1 too).

Otherwise, if the system is UEFI you could see if "EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL" exists and either assume mechanism #1 (given that you know PCI Express isn't supported) or attempt to actually use "EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL" functions to get better information.

For "PC BIOS" try the PCI BIOS v2.0c+ - INSTALLATION CHECK BIOS function. If this is supported then it'll give you the information you need. If it's not supported it doesn't mean that there is no PCI though (only that there's no PCI BIOS functions).

After you've tried everything else, the next step is manual probing. Here's some code from a version of my OS:

Code: Select all

;Detect if mechanism #1 is supported

	mov dx,0x0CF8
	in eax,dx
	mov ebx,eax
	mov eax,0x80000000
	out dx,eax
	in eax,dx
	cmp eax,0x80000000
	jne .noMech1
	mov eax,0x01
	jmp .gotPCI
.noMech1:
	mov eax,ebx
	out dx, eax

;Detect if mechanism #2 is supported

	mov dx,0x0CF8
	in al,dx
	mov bl,al
	clr al
	out dx,al
	in al,dx
	test al,al
	jne .noPCI

	mov dx,0x0CFA
	in al,dx
	mov bh,al
	clr al
	out dx,al
	in al,dx
	test al,al
	jne .noPCI
	mov eax,0x02

.gotPCI:       ; Value in EAX will be 1 if mechanism #1 is supported, or 2 if mechanism #2 is supported

Cheers,

Brendan

Re: Detecting PCI bus

Posted: Tue Apr 03, 2012 8:55 am
by iocoder
bluemoon wrote:To detect if PCI exist at all, you may:
1. call BIOS 1A
2. ACPI
3. just assume it's there and if not, blame the user for non-supported computer.
Cognition wrote:Initially you detect the presence of PCI through either the MCFG table through ACPI's table interface or the system firmware via BIOS32/Int 0x1A.
Brendan wrote:Start by checking for the existence of an MCFG ACPI table. If this table exists then you've got PCI Express memory mapped configuration space (with mechanism #1 too).
Well, checking the ACPI tables sounds good, but what about if the personal computer doesn't support ACPI, nor PC BIOS, nor EFI?? In this case, the operating system will think that there is not a PCI while there might be, won't it?

Re: Detecting PCI bus

Posted: Tue Apr 03, 2012 9:45 am
by aod
iocoder wrote: Well, checking the ACPI tables sounds good, but what about if the personal computer doesn't support ACPI, nor PC BIOS, nor EFI?? In this case, the operating system will think that there is not a PCI while there might be, won't it?
Then it is certainly non-x86 machine and your code wouldn't run on it neither. ;)

Re: Detecting PCI bus

Posted: Tue Apr 03, 2012 2:36 pm
by bluemoon
You should have a list of hardware requirement for your OS, you can't support running your OS in an ant's brain, natively.

Re: Detecting PCI bus

Posted: Tue Apr 03, 2012 4:17 pm
by Brendan
Hi,
iocoder wrote:Well, checking the ACPI tables sounds good, but what about if the personal computer doesn't support ACPI, nor PC BIOS, nor EFI?? In this case, the operating system will think that there is not a PCI while there might be, won't it?
If you're running on 80x86 and the environment you booted from is something other than PC BIOS or UEFI (e.g. coreboot, OpenFirmware, GRUB2, etc) then you'd need to see if it is has some way of finding out the PCI bus access mechanism, and then probably be disappointed that it doesn't, and then fall back to the manual probing code. :)

If you're not running on 80x86 at all then you'd need to say what you are running on, so that I can tell you I don't know much about whatever it is. ;)


Cheers,

Brendan

Re: Detecting PCI bus

Posted: Wed Apr 04, 2012 1:32 am
by xenos
Dealing with PCI may also be necessary (and quite a bit fun) on non-x86 architectures as well. For example, consider the BCM7405 STB SoC, which has an integrated PCI controller. If your OS is running on such a device, you simply know that it has a PCI bus, and you know how it can be accessed.

Just my two cents.

Re: Detecting PCI bus

Posted: Thu Apr 05, 2012 1:44 am
by linguofreak
Brendan wrote:Hi,
iocoder wrote:Well, checking the ACPI tables sounds good, but what about if the personal computer doesn't support ACPI, nor PC BIOS, nor EFI?? In this case, the operating system will think that there is not a PCI while there might be, won't it?
If you're running on 80x86 and the environment you booted from is something other than PC BIOS or UEFI (e.g. coreboot, OpenFirmware, GRUB2, etc) then you'd need to see if it is has some way of finding out the PCI bus access mechanism, and then probably be disappointed that it doesn't, and then fall back to the manual probing code.
I think by "Doesn't support PC BIOS" he meant "Doesn't support INT 1A AX=B101", as that method of detecting PCI had been suggested.

My thinking, though, is that any computers that have PCI but don't support any of the above-mentioned detection methods are probably old enough that the OP can just decide not to support them.