The io_mapping functions

API

The io_mapping functions in linux/io-mapping.h provide an abstraction forefficiently mapping small regions of an I/O device to the CPU. The initialusage is to support the large graphics aperture on 32-bit processors whereioremap_wc cannot be used to statically map the entire aperture to the CPUas it would consume too much of the kernel address space.

A mapping object is created during driver initialization using:

struct io_mapping *io_mapping_create_wc(unsigned long base,                                        unsigned long size)

‘base’ is the bus address of the region to be mademappable, while ‘size’ indicates how large a mapping region toenable. Both are in bytes.

This _wc variant provides a mapping which may only be used withio_mapping_map_atomic_wc(),io_mapping_map_local_wc() orio_mapping_map_wc().

With this mapping object, individual pages can be mapped either temporarilyor long term, depending on the requirements. Of course, temporary maps aremore efficient. They come in two flavours:

void *io_mapping_map_local_wc(struct io_mapping *mapping,                              unsigned long offset)void *io_mapping_map_atomic_wc(struct io_mapping *mapping,                               unsigned long offset)

‘offset’ is the offset within the defined mapping region. Accessingaddresses beyond the region specified in the creation function yieldsundefined results. Using an offset which is not page aligned yields anundefined result. The return value points to a single page in CPU addressspace.

This _wc variant returns a write-combining map to the page and may only beused with mappings created byio_mapping_create_wc()

Temporary mappings are only valid in the context of the caller. The mappingis not guaranteed to be globally visible.

io_mapping_map_local_wc() has a side effect on X86 32bit as it disablesmigration to make the mapping code work. No caller can rely on this sideeffect.

io_mapping_map_atomic_wc() has the side effect of disabling preemption andpagefaults. Don’t use in new code. Useio_mapping_map_local_wc() instead.

Nested mappings need to be undone in reverse order because the mappingcode uses a stack for keeping track of them:

addr1 = io_mapping_map_local_wc(map1, offset1);addr2 = io_mapping_map_local_wc(map2, offset2);...io_mapping_unmap_local(addr2);io_mapping_unmap_local(addr1);

The mappings are released with:

void io_mapping_unmap_local(void *vaddr)void io_mapping_unmap_atomic(void *vaddr)

‘vaddr’ must be the value returned by the lastio_mapping_map_local_wc() orio_mapping_map_atomic_wc() call. This unmaps the specified mapping andundoes the side effects of the mapping functions.

If you need to sleep while holding a mapping, you can use the regularvariant, although this may be significantly slower:

void *io_mapping_map_wc(struct io_mapping *mapping,                        unsigned long offset)

This works like io_mapping_map_atomic/local_wc() except it has no sideeffects and the pointer is globally visible.

The mappings are released with:

void io_mapping_unmap(void *vaddr)

Use for pages mapped withio_mapping_map_wc().

At driver close time, the io_mapping object must be freed:

void io_mapping_free(struct io_mapping *mapping)