ioremap()¶
Purpose¶
ioremap() creates a kernel virtual mapping for a physical MMIO (Memory-Mapped I/O) region.
Drivers use the returned virtual address to access hardware registers through MMIO access APIs such as readb(), readw(), readl(), writeb(), writew(), and writel().
Unlike normal memory allocation, ioremap() does not allocate RAM. It creates a virtual mapping to an existing physical device.
Header¶
Prototype¶
Parameters¶
| Parameter | Description |
|---|---|
offset |
Physical base address of the MMIO region. |
size |
Size of the mapping in bytes. |
Return Value¶
Returns:
- A kernel virtual address on success.
NULLif the mapping cannot be created.
Example¶
#define GPIO_BASE 0xFE200000
void __iomem *base;
base = ioremap(GPIO_BASE, SZ_4K);
if (!base)
return -ENOMEM;
status = readl(base + GPIO_STATUS);
/* ... */
iounmap(base);
iounmap()¶
Purpose¶
iounmap() removes a virtual mapping previously created by ioremap().
The underlying physical device remains unchanged.
Prototype¶
Parameters¶
| Parameter | Description |
|---|---|
addr |
Virtual address returned by ioremap(). |
Example¶
void __iomem *base;
base = ioremap(GPIO_BASE, SZ_4K);
if (!base)
return -ENOMEM;
/* Access MMIO registers */
iounmap(base);
Simulator Implementation¶
In this project:
Unlike Linux, the simulator does not modify page tables.
Instead, it creates simulated virtual mappings and performs software address translation through io_mapping_translate().
Notes¶
ioremap()maps existing MMIO regions; it does not allocate memory.- The returned address should only be accessed using MMIO access APIs.
- Multiple virtual mappings may reference the same physical MMIO region.
- Always release mappings with
iounmap()when they are no longer needed.