The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
mmio.h
Go to the documentation of this file.
1#pragma once
2
3/**
4 * mapping device memory (MMIO) into the kernel.
5 *
6 * device memory such as a linear framebuffer sits outside RAM, so the direct
7 * map of RAM does not cover it. ioremap maps it into a kernel-only window
8 * (MMIO_REGION_BASE..MMIO_REGION_END) shared by every address space. mappings
9 * are permanent: devices are never unplugged.
10 */
11
12#include "common/stdint.h"
13#include "kernel/memory/phys.h"
14
15/**
16 * reserve the MMIO window. must run after paging_init() and before the first
17 * address space is created.
18 */
19void mmio_init(void);
20
21/**
22 * map a range of device memory into the kernel.
23 * @param phys physical address of the device memory (need not be aligned).
24 * @param size number of bytes.
25 * @return kernel pointer to phys, or NULL if the window is exhausted.
26 */
27void *ioremap(phys_addr_t phys, size_t size);
28
29/**
30 * map a range of device registers into the kernel, not cached (each access
31 * goes to the device). use this for controller registers; ioremap is for
32 * memory such as a framebuffer.
33 * @param phys physical address of the registers (need not be aligned).
34 * @param size number of bytes.
35 * @return kernel pointer to phys, or NULL if the window is exhausted.
36 */
37void *ioremap_nocache(phys_addr_t phys, size_t size);
38
39/**
40 * map memory such as a framebuffer write-combining (uncached if the CPU
41 * has no PAT): writes are combined into bursts, reads are slow.
42 * @param phys physical address (need not be aligned).
43 * @param size number of bytes.
44 * @return kernel pointer to phys, or NULL if the window is exhausted.
45 */
46void *ioremap_wc(phys_addr_t phys, size_t size);
47
48/**
49 * read a 32-bit device register with one 32-bit access. a plain volatile
50 * read can become a narrower access (gcc can turn "*reg & 1" into a byte
51 * read), and many devices (the local APIC, xHCI) ignore narrow accesses.
52 */
53static inline uint32_t mmio_read32(const volatile void *reg)
54{
55 uint32_t value;
56 __asm__ volatile("movl (%1), %0" : "=r"(value) : "r"(reg) : "memory");
57 return value;
58}
59
60/**
61 * write a 32-bit device register with one 32-bit access.
62 */
63static inline void mmio_write32(volatile void *reg, uint32_t value)
64{
65 __asm__ volatile("movl %0, (%1)" : : "r"(value), "r"(reg) : "memory");
66}
67
68/**
69 * 16-bit and 8-bit device registers (Intel HDA has both), each with one
70 * access of that width.
71 */
72static inline uint16_t mmio_read16(const volatile void *reg)
73{
74 uint16_t value;
75 __asm__ volatile("movw (%1), %0" : "=r"(value) : "r"(reg) : "memory");
76 return value;
77}
78
79static inline void mmio_write16(volatile void *reg, uint16_t value)
80{
81 __asm__ volatile("movw %0, (%1)" : : "r"(value), "r"(reg) : "memory");
82}
83
84static inline uint8_t mmio_read8(const volatile void *reg)
85{
86 uint8_t value;
87 __asm__ volatile("movb (%1), %0" : "=q"(value) : "r"(reg) : "memory");
88 return value;
89}
90
91static inline void mmio_write8(volatile void *reg, uint8_t value)
92{
93 __asm__ volatile("movb %0, (%1)" : : "q"(value), "r"(reg) : "memory");
94}
unsigned int uint32_t
Definition fb.h:17
unsigned char uint8_t
Definition fb.h:18
unsigned short uint16_t
Definition stdint.h:13
void * ioremap_nocache(phys_addr_t phys, size_t size)
map a range of device registers into the kernel, not cached (each access goes to the device).
Definition mmio.c:70
void mmio_init(void)
mapping device memory (MMIO) into the kernel.
Definition mmio.c:12
void * ioremap_wc(phys_addr_t phys, size_t size)
map memory such as a framebuffer write-combining (uncached if the CPU has no PAT): writes are combine...
Definition mmio.c:75
void * ioremap(phys_addr_t phys, size_t size)
map a range of device memory into the kernel.
Definition mmio.c:65
uintptr_t phys_addr_t
physical addresses and the conversion between physical and virtual.
Definition phys.h:18