Linux memory Operations--ioremap and Mmap Learning notes

Source: Internet
Author: User

Recently in doing video output related things, for reserved for framebuffer memory use is not very clear, now find some information to tidy up, in case of use.

For a system, there will be a lot of peripherals, then the management of these peripherals are done through the CPU. So how does the CPU find the peripherals in this process?

Although there are many peripherals in a system, there will be multiple ports in the interface circuit of each peripheral. However, if the system can be given a specific address value for each port, then the system can easily find any one of the peripherals. When the system is managed, it needs to allocate a memory address for both memory and peripherals. For a 32bit system, the addressable range is the address space of 2^32=4g.

When it comes to address space, it is necessary to specify the type of address space: Physical address, bus address, virtual address.

(1) Physical Address

The address of the CPU address bus from the hardware circuit to 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.

(2) Bus address

The address line of the bus or the signal generated on the address cycle. The peripheral uses address bus, and the CPU uses the physical address.

The relationship between the physical address and the bus address is determined by the system design. On the X86 platform, the physical address is the bus address because they share the same address space. On other platforms, conversion/mapping may be required.

(3) Virtual address

The Virtual memory Management (management) mechanism is widely used in modern operating systems, which requires the support of MMU. 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 becomes the physical address, if the processor has the MMU enabled, The memory address emitted by the CPU execution unit will be intercepted by the MMU, the address from the CPU to the MMU is called the virtual address, and the MMU translates this address to another address on the external address pin of the CPU chip, that is, the virtual address mapped to a physical address.

In Linux, 4GB of process memory is divided into user space and kernel space. The user space is distributed as 1~3GB the remaining 1GB is the kernel space. Programmers can only use virtual addresses. Each process in the system has its own private user control (0~3GB), which is not visible to other processes in the system.

Addressing method

Peripherals are performed by means of registers on the device, the peripheral registers are also called "IO Ports", and the IO ports are addressed in two ways, independent addressing and unified addressing.

Unified addressing: The IO register (i.e. IO port) in the peripheral interface is treated as a primary storage unit, with each end share population using the address of a single memory cell, which is used as an IO address space by a portion of main memory. The principle of unified addressing is that the address space of the IO port address memory is addressed, and this method becomes the memory image addressing. The CPU accesses a port with the same operation as the memory access, and also uses instructions to access the memory. Stand-alone addressing is a separate part of the address space for the port address, and its access directives require a separate instruction (unlike memory access directives).

Depending on the CPU architecture, there are two ways that the CPU addresses the IO port:

(1) I/O mapping mode (i/o-mapped)
Typically, such as the X86 processor specifically implements a separate address space for peripherals, called "I/O address space" or "I/O port space," the CPU accesses address units in this space through specialized I/O directives such as X86 's in and out directives.
(2) Memory mapping mode (memory-mapped)
the CPU of a RISC instruction system (such as ARM, PowerPC, etc.) typically implements only one physical address space, and the peripheral I/O port becomes part of the memory. At this point, the CPU can access the peripheral I/O port as if it were accessing a memory unit without having to set up specialized peripheral I/O instructions.
However, the difference in hardware implementation is completely transparent to the software, and driver developers can treat the I/O port and peripheral memory as a "I/O Memory" resource in the memory-mapped way.
in general, when the system is running, the physical address of the peripheral's I/O memory resources is known and is determined by the hardware design. However, the CPU usually does not have a predefined virtual address range for the physical address of these known peripheral I/O memory resources, and the driver cannot access the I/O memory resources directly through the physical address, but must map them into the core virtual address space (via the page table) before the core virtual address ranges obtained from the mappings are Access these I/O memory resources through an inbound instruction.
entrance: phys_addr: The IO address to start the mapping;
Size: The amount of space to map;
Flags: Flags to map the IO space and permissions;
function: Mapping an IO address space to the virtual address space of the kernel for easy access;
implementation: To determine the IO address space to be mapped, the low Pci/isa address does not need to be remapped, does not allow the user to map the IO address space to the RAM in use, and finally applies a vm_area_struct structure, calling Remap_area_ Pages fill in the page table, if the completion process is unsuccessful, release the application of the vm_area_struct space;
Meaning:
For example, ISA device and PCI device, or FB, the hardware of the jumper or physical connection determines the hardware memory mapped to the CPU physical address.
access to these addresses in the kernel must be assigned to the virtual address of this memory, which is exactly what __ioremap means, and it is important to note that the physical memory is already "there" and no need to alloc the page to this address.

in order for the software to access I/O memory, the device must be assigned a virtual address. This is Ioremap's job. This function is specifically designed to allocate virtual addresses (spaces) for the I/O memory area. I don't do anything for the I/O address directly mapped ioremap. with Ioremap (and Iounmap), the device can access any I/O memory space, regardless of whether it is mapped directly to the virtual address space. However, these addresses can never be used directly (referring to physical addresses) and are used with readb functions.

void * __IOREMAP (unsigned long phys_addr, unsigned long size, unsigned long flags)  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.