Page 1 of 1

[FIXED] PCI config space

Posted: Thu Nov 03, 2011 5:36 am
by Bietje
Hi,

You got some sort of PCI device and you have to write to the address specified in one of the BARs. This address might not exist (i.e. the address is located at 0xde000000 but you have less memory then that). Is it possible then to map that address to virtual memory so you can still configure the PCI device, or isn't that accepted by the PCI device?

Greets,
Bietje

Re: PCI config space

Posted: Thu Nov 03, 2011 6:07 am
by Solar
Bietje wrote:...located at 0xde000000 but you have less memory then that...
The term "memory-mapped IO" means that memory of the device is mapped into the address space. I.e., it has nothing to do with the amount of RAM you have installed.

Re: PCI config space

Posted: Thu Nov 03, 2011 6:24 am
by Bietje
Solar wrote:
Bietje wrote:...located at 0xde000000 but you have less memory then that...
The term "memory-mapped IO" means that memory of the device is mapped into the address space. I.e., it has nothing to do with the amount of RAM you have installed.
Meaning that mapping the memory address to a virtual address to write/read from it doesn't matter?

Re: PCI config space

Posted: Thu Nov 03, 2011 6:52 am
by davidv1992
It means that read/writes to the physical address specified in the BAR are send to the device. Whatever paging does on top of that is of no interest to the device.

Re: PCI config space

Posted: Thu Nov 03, 2011 7:02 am
by Bietje
Hmm oke

There is some read/write register at some address specified by the BAR (MSI-X address register), writing a value (say 0xff) to that address and reading it back returns 0 when the address is mapped 1:1. So how can I read back the correct value from memory mapped I/O?

Greets,
Bietje

ps:
what I did:

Code: Select all

uint32_t *bar = get_bar_value(0x20);
*bar = 0xff
printf("%x\n", *bar);
So bar is the address specified by the BAR and the output is 0.

Re: PCI config space

Posted: Thu Nov 03, 2011 7:11 am
by Solar
Bietje wrote:So how can I read back the correct value from memory mapped I/O?
What would be the "correct" value you expected?

Edit: I get the impression that you are feeling your way in the dark, unaware of what is actually going on or what the BAR is meant to be. Please tell me if I'm wrong.

Re: PCI config space

Posted: Thu Nov 03, 2011 8:55 am
by Bietje
Solar wrote:
Bietje wrote:So how can I read back the correct value from memory mapped I/O?
What would be the "correct" value you expected?

Edit: I get the impression that you are feeling your way in the dark, unaware of what is actually going on or what the BAR is meant to be. Please tell me if I'm wrong.
With correct I mean that when I do a memory write towards the memory mapped address that the value is stored/saved there, and when I read back the location that the value is returned. Although, I thought that was correct since the space is read/write according to the PCI (3.0) specification.

Re: PCI config space

Posted: Thu Nov 03, 2011 9:16 am
by Solar
Bietje wrote:
Solar wrote:What would be the "correct" value you expected?
With correct I mean that when I do a memory write towards the memory mapped address that the value is stored/saved there, and when I read back the location that the value is returned...
...which is true for RAM, but might be a completely different thing if what you are writing to is a device register. I'm out on a limb here since I never wrote PCI drivers myself, but as far as I understood it, the BAR contents merely tell you where the device's memory is. What it is, and what writing 0x000000ff to offset 0x20 (as you did) means to the device, is up to the device, and should be looked up in its technical documentation. Perhaps your 0x000000ff@0x20 asked a SATA controller to tell you how many SATA devices are connected to it, and the 0x00000000 you got in reply is the answer (zero). Perhaps you just told your GPU to go into suspend mode, and it told you that it could not. I don't know.

Perhaps I am pointing out the obvious to you and am making a fool of myself. I don't know that either. It's hard to tell from the information you gave so far.

Re: PCI config space

Posted: Thu Nov 03, 2011 9:30 am
by Bietje
My stupidity...
PCI Specification wrote:When you want to retrieve the actual base address of a BAR, be sure to mask the lower bits. For 16-Bit Memory Space BARs, you calculate (BAR[x] & 0xFFF0). For 32-Bit Memory Space BARs, you calculate (BAR[x] & 0xFFFFFFF0). For 64-Bit Memory Space BARs, you calculate ((BAR[x] & 0xFFFFFFF0) + ((BAR[x+1] & 0xFFFFFFFF) << 32)) For I/O Space BARs, you calculate (BAR[x] & 0xFFFFFFFC).
That fixed allot... Thread wasn't completely useless it clarified some other things as well, thanks!

Greets,
Bietje