Page 1 of 1

Do I understand the PCI page correctly?

Posted: Sun Dec 22, 2013 3:23 pm
by Tarkin
Hello,
I've been trying to learn about PCI, so I turned to the PCI page of the OSDEV wiki:
http://wiki.osdev.org/PCI

Looking at the CONFIG_ADDRESS table, it shows the bottom two bits zeroed out,
and bits 7 - 2 are the Register Number.

The page provides example C code for 'Configuration Mechanism #1'.

For quick & easy reference I cut & pasted the code below:

Code: Select all

unsigned short pciConfigReadWord (unsigned short bus, unsigned short slot,
                                  unsigned short func, unsigned short offset)
 {
    unsigned long address;
    unsigned long lbus = (unsigned long)bus;
    unsigned long lslot = (unsigned long)slot;
    unsigned long lfunc = (unsigned long)func;
    unsigned short tmp = 0;
 
    /* create configuration address as per Figure 1 */
    address = (unsigned long)((lbus << 16) | (lslot << 11) |
              (lfunc << 8) | (offset & 0xfc) | ((UINT32)0x80000000));
 
    /* write out the address */
    sysOutLong (0xCF8, address);
    /* read in the data */
    /* (offset & 2) * 8) = 0 will choose the fisrt word of the 32 bits register */
    tmp = (unsigned short)((sysInLong (0xCFC) >> ((offset & 2) * 8)) & 0xffff);
    return (tmp);
 }
My questions are:
- Does the 'offset' parameter correspond to the Register Number?
- If it does, I understand that '& 0xFC' will zero out the bottom two bits,
but shouldn't it be shifted left two bits first?

Thanks,
Tarkin

Re: Do I understand the PCI page correctly?

Posted: Mon Dec 23, 2013 8:53 am
by dansmahajan
Tarkin wrote: My questions are:
- Does the 'offset' parameter correspond to the Register Number?
yes it does
- If it does, I understand that '& 0xFC' will zero out the bottom two bits,
but shouldn't it be shifted left two bits first?
register num are multiples of 4(100 in binary) ie 0,4,8....making bottom 2 bits always zero so no need to shift anything

Re: Do I understand the PCI page correctly?

Posted: Mon Dec 23, 2013 12:04 pm
by Tarkin
dansmahajan wrote: register num are multiples of 4(100 in binary) ie 0,4,8....making bottom 2 bits always zero so no need to shift anything
Ok, thanks for clearing that up!

Re: Do I understand the PCI page correctly?

Posted: Tue Dec 24, 2013 4:36 am
by Combuster
Updated the PCI page because this gets asked too often and nobody mentioned the real reasoning behind all of this so far.

Also,
register num are multiples of 4
This is not true actually. For the snippet given they are actually multiples of two.