PCI Express - Clarification about BAR, Memory addressing and physical memory

5

3

I'm an absolute beginner starting to understand the PCI Express protocol and I need some clarifications about its mechanics.

I understood that a PCI Express endpoint device may have some memory BAR mapped in the system memory (is it always RAM the system memory we are talking about?). I need to understand what happens when I send from the CPU to the device (A) a memory read request, addressing a certain memory address (first memory BAR, offset 0).

Let's assume that my device doesn't have any in-device memory.

The root complex on behalf of CPU creates the TLP and forwards it to device (A) because the memory address of destination is assigned to (A).

A receives the TLP, unpacks it, and creates a completion TLP containing data coming from its internal application logic.

Now the TLP travels backwards to the Root Complex that unpacks it, gathers the interesting data and gives it back to the the CPU.

What is the system memory role during this communication? None?

Does the root complex communicate (in this case) with the physical memory of the system, other than sending and receiving packets to/from the endpoint?

To some people it may seem a silly question but for me this is crucial to understand the connection between the physical memory and the memory address BARs assigned to each PCI Express device.

Jacopo Reggiani

Posted 2013-06-13T10:32:37.777

Reputation: 75

What does BAR stand for? – None – 2013-06-13T10:57:46.490

1Base Address Register. – LawrenceC – 2013-06-13T11:38:02.687

Thank you very much Hennes, I really need to understand if the physical memory addressed by the memory BARs of a PCIe device resides in the device itself, is it part of the RAM, both, none? When we refer to memory as a whole, is it the sum of RAM + devices memories? the answer ultrasawblade gave seems to go in that direction, but I'm not sure – Jacopo Reggiani – 2013-06-13T16:11:26.957

"Memory as whole" in the way you are saying is simply a range of addresses. A better term is "CPU address space." When the CPU issues a MOV instruction to read or write from an address, it can be set up where things other than RAM respond. This was quite common on older systems, and I believe most ARM-based CPU's still mostly rely entirely on I/O devices appearing like and being addressable in the same way as RAM. – LawrenceC – 2013-06-14T16:33:09.420

Answers

1

When a CPU talks to a memory address, it isn't RAM that has to respond. It can be an I/O device. You can actually think of RAM as a "specialized memory-mapped I/O device" whose job is just to save and give back data, although with today's modern CPUs that have caching and such, it's not physically straightforward.

I am not too keen in the low level details of PCIe, but you seem to be wondering how the PCIe bus itself communicates with the CPU. It does so like anything else that communicates with the CPU:

  • Memory mapping - i.e. a device is "mapped" where reads and writes to a range of addresses don't go to RAM, but a device or controller.
  • DMA - an external device or controller reads/writes a section of RAM, without the CPU being involved at all.
  • I/O ports - this is just another address space (a feature of the Intel x86 family of CPUs) that has historically been dedicated to I/O devices. You'll never find RAM here, but it works like memory mapping. The main difference between I/O ports and memory mapping is that I/O port instructions always work serially, no "out of order" or "reordering" of operations will happen here as opposed to the CPU attempting to do that with accesses to main memory.
  • IRQs - an external device sends an interrupt to the CPU

The BARs and such are "mapped" into memory, and take the place of any RAM that may be "under" it. With the MMU I believe it's possible to "remap" the RAM beneath it to a different physical address.

LawrenceC

Posted 2013-06-13T10:32:37.777

Reputation: 63 487

In the case of Memory mapping yo wrote: – Jacopo Reggiani – 2013-06-13T14:21:51.787

In the case of Memory mapping yo wrote:

"Memory mapping - i.e. a device is "mapped" where reads and writes to a range of addresses don't go to RAM, but a device or controller."

If my device hasn't got an inboard memory, is it possible that its let's say 1 MB of Memory is mapped into RAM? If so, when I ask my software driver to read from an address linked to my device memory map, am I asking both data from the device and contextually writing it into the related address in RAM? – Jacopo Reggiani – 2013-06-13T14:27:23.360

Let me explain better and in simple words: Is RAM involved in every PCI Express Memory Read/Write Transaction? Does the physical memory addressed by a Memory BAR resides in the device or in the RAM? – Jacopo Reggiani – 2013-06-13T15:19:15.977

1It resides in the device. – LawrenceC – 2013-06-14T16:29:56.093

@ultrasawblade Please tell me, in your answer MMIO (memory mapped I / O) refers to the I/O Ports or to the Memory Mapping, or is it something else? MMIO: http://en.wikipedia.org/wiki/MMIO

– Alex – 2013-11-09T21:27:34.923

1Memory mapped I/O does not refer to I/O ports. Memory mapped I/O is accessed by the CPU using the same instructions it does for reading/writing RAM. I/O ports are accessed by the CPU using I/O-port specific instructions. – LawrenceC – 2013-11-10T03:27:13.810

@ultrasawblade Thanks. But can I use I/O-port specific instructions (asm: in, out) directly with PCI-Express? – Alex – 2013-11-10T11:45:51.660

1The PCI-E controller itself appears in the x86 I/O space on x86 and compatible architectures at well-known addresses. Now ... there are I/O BARs (which look like they are deprecated according to Wikipedia) so a PCI-E device can say it needs addresses in the I/O range as well as the memory range. If a PCI-E device has an I/O BAR then you will be using I/O ports after the device is configured. – LawrenceC – 2013-11-10T12:30:02.180

@ultrasawblade It's very confusing terminology. 1. Are I/O BARs the same that I/O-port specific instructions (asm: in, out)? 2. And I/O BARs (I/O-port specific instructions (asm: in, out)) are now depricated? 3. But if my device still using an I/O BARs then I can use I/O-port specific instructions (asm: in, out), that's right? – Alex – 2013-11-10T15:11:58.397