Do I understand the PCI page correctly?

All about the OSDev Wiki. Discussions about the organization and general structure of articles and how to use the wiki. Request changes here if you don't know how to use the wiki.
Post Reply
Tarkin
Posts: 3
Joined: Sat Jun 27, 2009 8:15 am

Do I understand the PCI page correctly?

Post 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
dansmahajan
Member
Member
Posts: 62
Joined: Mon Jan 07, 2013 10:38 am

Re: Do I understand the PCI page correctly?

Post 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
Tarkin
Posts: 3
Joined: Sat Jun 27, 2009 8:15 am

Re: Do I understand the PCI page correctly?

Post 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!
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Do I understand the PCI page correctly?

Post 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.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Post Reply