"Summary" from the CPU connected to a line: Data bus, address bus, control bus, this line hangs N interface, have the same, have different, name is called Memory interface, interrupt control interface, DMA interface, parallel interface, serial interface, AD interface ... For a device to be plugged in, use its own interface and a matching interface on the bus to connect ... So there are various devices on the bus: memory, hard disk, mouse, keyboard, monitor ...
For the CPU, if it is to send data to a device, is actually sent to the corresponding interface, the interface circuit has a plurality of registers (also known as ports), the access device is actually access to the relevant port, all the information will be transferred from the interface to its device. So the CPU will prepare the data to the bus, but a lot of interfaces, who should be sent to? At this point you need to assign an address to each interface, and then put the address on the address bus, the control information needed to put on the control bus, you can communicate with the device.
For a system, there will usually be multiple peripherals, each peripheral interface circuit, there will be more than one port, each port needs an address, for them to identify a specific address value, is the system must solve the matter, at the same time, you also have a memory bar, may be 512M or 1G or greater Kingston, Modern DDR2 and the like, each of their addresses also need to assign an identity value, in addition, many peripherals have their own memory, buffer, like your memory bar, you also need to allocate memory for them ... Your CPU may need to deal with every byte of it, so don't expect to be lazy, and every inch of their land must be planned! This may sound annoying, and doing so could directly lead to brain cells being killed. But there is always someone to do, arm may do this: he designed the CPU is 32-bit, at most can address the 2^32=4g space, so the 4GB space to the memory and port, let them partition. But Intel may have a better way of distributing ...
1, the concept of the address
1) Physical Address : The address of the CPU address bus, by the hardware circuit control its specific meaning. A large portion of the physical address is left to memory in the memory bar, but it is often mapped to other storage (such as video memory, BIOS, and so on). After the virtual address in the program instruction passes through the segment map and the page map, the physical address is generated, and the physical address is placed on the address line of the CPU.
Physical address space, part of the physical RAM (memory) used, partly for the bus, which is determined by the hardware design, so in the x86 processor of the three bits address line, the physical address space is 2 of 32 square, that is 4GB, but the physical RAM is generally not up to 4GB, Because there is still a part to be used for the bus (there are many other devices hanging on the bus). In the PC, the low-end physical address is generally used for RAM, high-end physical address to the bus.
2) Bus address: The address line of the bus or the signal generated on the address cycle. The peripheral uses the bus address and the CPU uses the physical address.
The relationship between the physical address and the bus address is determined by the design of the system. On the x86 platform, the physical address is the bus address because they share the same address space-a bit difficult to understand, as described in the "stand-alone addressing" below. On other platforms, conversion/mapping may be required. For example: The CPU needs to access the physical address is 0xfa000 unit, then on the x86 platform, a PCI bus will be generated on the 0xfa000 address access. Because the physical address and the bus address are the same, it is not possible to see with the eyes where the address is being used, either in memory or in a storage unit on a card, or even there is no corresponding memory on the address.
3) virtual address : The modern operating system generally adopts virtual memory management (Management) mechanism, which requires the support of MMU (Memory Management Unit). The MMU is usually a part of the CPU, if the processor does not have the MMU, or the MMU is not enabled, the memory address emitted by the CPU execution unit is transmitted directly to the chip pin and is received by the memory chip (physical memory), which is called the Physical Address (physical). If the processor enables the memory address of the MMU,CPU execution unit to be intercepted by the MMU, the address from the CPU to the MMU is called the virtual address, and the MMU translates the address to another address on the external address pin of the CPU chip, That is, the virtual address is mapped to a physical address.
In **linux, the 4GB (virtual) memory of the process is divided into user space and kernel space. The user space is distributed as 0~3GB (that is, Page_offset, which equals 0xc0000000 in 0x86)
, the remaining 1G is the kernel space. Programmers can only use virtual addresses. Each process in the system has its own private user space (0~3G), which is not visible to other processes in the system. **
The address of the CPU when the request is made is the virtual address of the current context, and the MMU finds the physical address of the virtual address from the page table and completes the reference. Also read the data is a virtual address, such as Mov ax, var. compile-time Var is a virtual address, but also through the MMU from the table to find the physical address, and then generate the bus timing, complete fetching data.
2. Address method
1) Peripherals are carried out through the registers on the read-write device, the peripheral register is also called the "I/O port", and the IO port has two ways of addressing: independent addressing and unified programming.
Unified Addressing: the IO register (i.e. IO port) in the peripheral interface is treated as the main storage unit, and each end share population uses the address of a memory cell, which is used as an IO address space by a portion of the primary memory, for example, in PDP-11, the highest 4K main memory as the IO device register address. The port occupies the memory address space, which reduces the storage capacity.
Unified addressing is also referred to as "I/O memory", where the peripheral registers are in "memory space" (many peripherals have their own memory, buffers, peripheral registers and memory referred to as "I/O space").
For example, Samsung's s3c2440 is a 32-bit ARM processor, and its 4GB address space is partitioned by peripherals, RAM, and so on:
0x8000 the address of the LED 8*8 dot Matrix
0x4800 0000 ~ 0x6000 0000 SFR (Special Register) address space
0x3800 1002 Keyboard Address
0x3000 0000 ~ 0x3400 0000 SDRAM Space
0x2000 0020 ~ 0x2000 002e IDE
0x1900 0300 CS8900
stand-alone Addressing (separate address): The IO address is separate from the storage address, the i/0 port address does not occupy the address range of the storage space, so that in the system there is another storage address independent of the IO address, the CPU must have dedicated and input and output operation of the IO instructions (in, Out, etc.) and control logic. Stand-alone address, the address bus come up with a location, the device is not known to the IO port, or to the memory, so the processor through the MEMR/MEMW and Ior/iow two sets of control signals to achieve the I/O port and memory of different addressing. For example, the Intel 80x86 uses a separate address, CPU memory and I/O are addressed together, that is, the memory portion of the addresses and I/O addresses overlap.
Stand-alone addressing is also known as an I/O port, and the peripheral registers are located in the I/O (address) space.
For the x86 architecture, it is accessed through the In/out directive. PC architecture A total of 65,536 8bit I/O ports, composed of 64K I/O address space, numbered from 0~0XFFFF, there are 16 bits, 80x86 with a low 16-bit address line a0-a15 addressing. Two consecutive 8bit ports can be composed of a 16bit port, 4 consecutive ports composed of a 32bit. The physical address space of the I/O address space and the CPU is two different concepts, such as the I/O address space is 64K, and a 32bit CPU physical address space is 4G. For example, under Intel 8086+redhat9.0, use "More/proc/ioports" to see:
0000-001f:dma1
0020-003f:pic1
0040-005f:timer
0060-006f:keyboard
0070-007f:rtc
0080-008F:DMA Page Reg
00a0-00bf:pic2
00c0-00df:dma2
00f0-00ff:fpu
0170-0177:ide1
......
However, the Intel x86 platform generally uses a technology called Memory Mapping (MMIO), which is part of the PCI specification, the IO device port is mapped to memory space, and after mapping, the CPU accesses the IO port as if it were accessing memory. See the X86/X64 system typical memory address allocation table given by the Intel TA 719 Documentation:
System resource Consumption
BIOS 1M
Local APIC 4K
Chipset retention 2M
IO APIC 4K
PCI Device 256M
PCI Express Device 256M
PCI device (optional) 256M
Display Frame Cache 16M
Tseg 1M
For a given system, it is either a stand-alone address or a unified address, depending on the CPU's architecture. For example, PowerPC, m68k, such as the use of unified addressing, and X86, such as the use of independent addressing, there is the concept of IO space. Currently, most embedded microcontrollers such as ARM, PowerPC, etc. do not provide I/O space, only memory space, can be directly accessed with the address, pointer. However, for the Linux kernel, it may be used for different CPUs, so it must be considered in both ways, so it takes a new approach, the I/O map-based or memory-mapped I/O port is known as an I/O region (I/O regions), regardless of which way you use, Must first apply IO area: Request_resource (), release it at the end: Release_resource ().
2) Access to peripherals
1. The process of accessing I/O memory is:
Request_mem_region (), Ioremap (), Ioread8 ()/iowrite8 (), Iounmap (), Release_mem_region ().
As mentioned earlier, IO memory is a unified address concept, for unified addressing, IO address space is a part of physical memory, for programming, we can only operate virtual memory, so, the first step of access is to map the physical address of the device to the virtual address, Linux2.6 under Ioremap ():
void *ioremap (unsigned long offset, unsigned long size);
We can then access these addresses directly through pointers, but we can also use a set of functions from the Linux kernel to read and write:
Ioread8 (), Iowrite16 (), Ioread8_rep (), Iowrite8_rep () ...
2. Access the I/O port
There are 2 ways to access the IO port: I/O mapping mode (i/o-mapped), Memory Mapping (memory-mapped). The former path does not map to memory space, directly uses functions such as INTB ()/outb () to read and write IO ports, the latter Mmio is to first map IO ports to IO memory ("Memory Space"), and then access IO ports using functions that access IO memory.
void Ioport_map (unsigned long port, unsigned int count);
With this function, you can map the count of successive IO ports starting with Port to a "memory space", and then you can access these IO ports at the address they return as if they were accessing IO memory.
The advantages of i/0 stand-alone address are: No memory space, the use of I/O instructions, the program is clear, it is easy to see whether I/O operation or memory operation, decoding circuit is relatively simple (because the i/0 port address space is generally small, the address line is less). The disadvantage is: only with specialized i/0 instructions, access to the port method is not as much as the method of accessing the memory.
Which one to use depends on the overall design of the system. You can also use both methods in one system, provided that you first support I/O independent addressing. : Intel's x86 microprocessors support I/O stand-alone addresses because they have I/O directives in their instruction systems, and control signal pins that can differentiate between I/O access and memory access are set. And some microprocessor or microcontroller, in order to reduce the PIN, thereby reducing the chip occupied area, does not support I/O stand-alone address, can only use the memory unified addressing.
Physical addresses and virtual addresses, unified addressing and stand-alone addressing, and I/O ports and I/O memory