PatchworkOS  19e446b
A non-POSIX operating system.
Loading...
Searching...
No Matches
port.c
Go to the documentation of this file.
1#include <kernel/cpu/port.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, PORT_MAX + 1);
14
15uint64_t port_reserve(port_t* out, port_t minBase, port_t maxBase, uint64_t alignment, uint64_t length,
16 const char* owner)
17{
18 UNUSED(owner);
19
20 if (out == NULL || length == 0 || minBase > maxBase)
21 {
22 errno = EINVAL;
23 return ERR;
24 }
26
27 if (maxBase + length < maxBase || maxBase + length > ports.length)
28 {
30 return ERR;
31 }
32
33 uint64_t base = bitmap_find_clear_region_and_set(&ports, minBase, maxBase + length, length, alignment);
34 if (base == ports.length)
35 {
36 errno = ENOSPC;
37 return ERR;
38 }
39
40 *out = (port_t)base;
41 return 0;
42}
43
44void port_release(port_t base, uint64_t length)
45{
46 if (length == 0 || base + length < base || base + length > ports.length)
47 {
48 return;
49 }
51
52 bitmap_clear_range(&ports, base, base + length);
53}
void port_release(port_t base, uint64_t length)
Release a previously reserved range of I/O ports.
Definition port.c:44
#define PORT_MAX
Maximum I/O port number.
Definition port.h:33
uint64_t port_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 port.c:15
uint16_t port_t
I/O port type.
Definition port.h:28
#define LOCK_CREATE()
Create a lock initializer.
Definition lock.h:69
#define LOCK_SCOPE(lock)
Acquires a lock for the reminder of the current scope.
Definition lock.h:58
#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
static 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.
Definition bitmap.h:392
#define BITMAP_CREATE(name, bits)
Define and create a bitmap and its buffer.
Definition bitmap.h:73
static void bitmap_clear_range(bitmap_t *map, uint64_t low, uint64_t high)
Clear a range of bits in the bitmap.
Definition bitmap.h:264
#define UNUSED(x)
Mark a variable as unused.
Definition defs.h:96
#define NULL
Pointer error value.
Definition NULL.h:25
#define ERR
Integer error value.
Definition ERR.h:17
static lock_t lock
Definition port.c:13
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
A simple ticket lock implementation.
Definition lock.h:44