Detecting PCI bus

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
iocoder
Member
Member
Posts: 208
Joined: Sun Oct 18, 2009 5:47 pm
Libera.chat IRC: iocoder
Location: Alexandria, Egypt | Ottawa, Canada
Contact:

Detecting PCI bus

Post 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
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: Detecting PCI bus

Post 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;
        }
    }
User avatar
iocoder
Member
Member
Posts: 208
Joined: Sun Oct 18, 2009 5:47 pm
Libera.chat IRC: iocoder
Location: Alexandria, Egypt | Ottawa, Canada
Contact:

Re: Detecting PCI bus

Post 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??
User avatar
Jezze
Member
Member
Posts: 395
Joined: Thu Jul 26, 2007 1:53 am
Libera.chat IRC: jfu
Contact:

Re: Detecting PCI bus

Post 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);

Fudge - Simplicity, clarity and speed.
http://github.com/Jezze/fudge/
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: Detecting PCI bus

Post 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.
Cognition
Member
Member
Posts: 191
Joined: Tue Apr 15, 2008 6:37 pm
Location: Gotham, Batmanistan

Re: Detecting PCI bus

Post 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.
Reserved for OEM use.
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: Detecting PCI bus

Post 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
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
User avatar
iocoder
Member
Member
Posts: 208
Joined: Sun Oct 18, 2009 5:47 pm
Libera.chat IRC: iocoder
Location: Alexandria, Egypt | Ottawa, Canada
Contact:

Re: Detecting PCI bus

Post 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?
aod
Member
Member
Posts: 26
Joined: Fri Sep 30, 2011 2:36 am

Re: Detecting PCI bus

Post 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. ;)
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: Detecting PCI bus

Post 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.
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: Detecting PCI bus

Post 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
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
User avatar
xenos
Member
Member
Posts: 1144
Joined: Thu Aug 11, 2005 11:00 pm
Libera.chat IRC: xenos1984
Location: Tartu, Estonia
GitHub: https://github.com/xenos1984
Contact:

Re: Detecting PCI bus

Post 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.
Programmers' Hardware Database // GitHub user: xenos1984; OS project: NOS
linguofreak
Member
Member
Posts: 510
Joined: Wed Mar 09, 2011 3:55 am

Re: Detecting PCI bus

Post 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.
Post Reply