Skip to content

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.


#include <linux/io.h>

Prototype

void __iomem *ioremap(resource_size_t offset,
                      unsigned long size);

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.
  • NULL if 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

void iounmap(void __iomem *addr);

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:

ioremap()
io_mapping_create()
iounmap()
io_mapping_destroy()

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.