PatchworkOS  2ca1c69
A non-POSIX operating system.
Loading...
Searching...
No Matches
io.c
Go to the documentation of this file.
1#include <kernel/cpu/io.h>
2#include <kernel/sync/lock.h>
3
4#include <kernel/log/log.h>
5
6#include <errno.h>
7#include <stdbool.h>
8#include <stddef.h>
9#include <sys/bitmap.h>
10#include <sys/list.h>
11
12static BITMAP_CREATE(ports, IO_PORT_MAX + 1);
14
15uint64_t io_reserve(port_t* out, port_t minBase, port_t maxBase, uint64_t alignment, uint64_t length, const char* owner)
16{
17 (void)owner;
18
19 if (out == NULL || length == 0 || minBase > maxBase)
20 {
21 errno = EINVAL;
22 return ERR;
23 }
25
26 if (maxBase + length < maxBase || maxBase + length > ports.length)
27 {
29 return ERR;
30 }
31
32 uint64_t base = bitmap_find_clear_region_and_set(&ports, minBase, maxBase + length, length, alignment);
33 if (base == ports.length)
34 {
35 errno = ENOSPC;
36 return ERR;
37 }
38
39 *out = (port_t)base;
40 return 0;
41}
42
43void io_release(port_t base, uint64_t length)
44{
45 if (length == 0 || base + length < base || base + length > ports.length)
46 {
47 return;
48 }
50
51 bitmap_clear_range(&ports, base, base + length);
52}
#define IO_PORT_MAX
Maximum I/O port number.
Definition io.h:32
uint64_t io_reserve(port_t *out, port_t minBase, port_t maxBase, uint64_t alignment, uint64_t length, const char *owner)
Find and reserve a range of I/O ports if available.
Definition io.c:15
void io_release(port_t base, uint64_t length)
Release a previously reserved range of I/O ports.
Definition io.c:43
uint16_t port_t
I/O port type.
Definition io.h:27
#define LOCK_CREATE()
Create a lock initializer.
Definition lock.h:68
#define LOCK_SCOPE(lock)
Acquires a lock for the reminder of the current scope.
Definition lock.h:57
#define ENOSPC
No space left on device.
Definition errno.h:172
#define EINVAL
Invalid argument.
Definition errno.h:142
#define EOVERFLOW
Value too large for defined data type.
Definition errno.h:402
#define errno
Error number variable.
Definition errno.h:27
void bitmap_clear_range(bitmap_t *map, uint64_t low, uint64_t high)
Clear a range of bits in the bitmap.
uint64_t bitmap_find_clear_region_and_set(bitmap_t *map, uint64_t minIdx, uintptr_t maxIdx, uint64_t length, uint64_t alignment)
Find a clear region of specified length and alignment, and set it.
#define BITMAP_CREATE(name, bits)
Define and create a bitmap and its buffer.
Definition bitmap.h:73
#define NULL
Pointer error value.
Definition NULL.h:23
#define ERR
Integer error value.
Definition ERR.h:17
static lock_t lock
Definition io.c:13
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
A simple ticket lock implementation.
Definition lock.h:43