| 1 | #include <acpi/print.h> |
| 2 | #include <acpi/uacpi_interface.h> |
| 3 | #include <asm.h> |
| 4 | #include <compiler.h> |
| 5 | #include <console/printf.h> |
| 6 | #include <drivers/mmio.h> |
| 7 | #include <irq/idt.h> |
| 8 | #include <mem/alloc.h> |
| 9 | #include <mem/vmm.h> |
| 10 | #include <pit.h> |
| 11 | #include <sch/sched.h> |
| 12 | #include <smp/core.h> |
| 13 | #include <stdbool.h> |
| 14 | #include <stdint.h> |
| 15 | #include <sync/mutex.h> |
| 16 | #include <sync/spinlock.h> |
| 17 | #include <thread/thread.h> |
| 18 | #include <uacpi/event.h> |
| 19 | #include <uacpi/platform/arch_helpers.h> |
| 20 | #include <uacpi/status.h> |
| 21 | #include <uacpi/types.h> |
| 22 | #include <uacpi/uacpi.h> |
| 23 | |
| 24 | #include "uacpi/kernel_api.h" |
| 25 | #include "uacpi/log.h" |
| 26 | #include "uacpi/namespace.h" |
| 27 | |
| 28 | uint64_t tsc_freq = 0; |
| 29 | |
| 30 | #define panic_if_error(x) \ |
| 31 | if (uacpi_unlikely_error(x)) \ |
| 32 | panic("uACPI initialization failed!"); |
| 33 | |
| 34 | static uint64_t our_rsdp = 0; |
| 35 | void uacpi_init(uint64_t rsdp) { |
| 36 | our_rsdp = rsdp; |
| 37 | tsc_freq = measure_tsc_freq_pit(); |
| 38 | |
| 39 | panic_if_error(uacpi_initialize(0)); |
| 40 | panic_if_error(uacpi_namespace_load()); |
| 41 | panic_if_error(uacpi_namespace_initialize()); |
| 42 | panic_if_error(uacpi_finalize_gpe_initialization()); |
| 43 | } |
| 44 | |
| 45 | uacpi_status uacpi_kernel_get_rsdp(uacpi_phys_addr *out_rsdp_address) { |
| 46 | |
| 47 | if (our_rsdp == 0) { |
| 48 | printf(format: "no rsdp set\n" ); |
| 49 | return UACPI_STATUS_INTERNAL_ERROR; |
| 50 | } |
| 51 | *out_rsdp_address = our_rsdp; |
| 52 | return UACPI_STATUS_OK; |
| 53 | } |
| 54 | |
| 55 | void *uacpi_kernel_map(uacpi_phys_addr addr, uacpi_size len) { |
| 56 | void *ret = mmio_map(phys: addr, size: len); |
| 57 | return ret; |
| 58 | } |
| 59 | |
| 60 | void uacpi_kernel_unmap(void *addr, uacpi_size len) { |
| 61 | return; |
| 62 | vmm_unmap_virt(addr, len, vflags: VMM_FLAG_NONE); |
| 63 | } |
| 64 | |
| 65 | void uacpi_kernel_log(uacpi_log_level level, const uacpi_char *data) { |
| 66 | switch (level) { |
| 67 | case UACPI_LOG_ERROR: printf(format: ">> UACPI ERROR: %s" , data); break; |
| 68 | case UACPI_LOG_TRACE: printf(format: ">> UACPI TRACE: %s" , data); break; |
| 69 | case UACPI_LOG_INFO: printf(format: ">> UACPI INFO: %s" , data); break; |
| 70 | case UACPI_LOG_WARN: printf(format: ">> UACPI WARN: %s" , data); break; |
| 71 | default: break; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | void *uacpi_kernel_alloc(uacpi_size size) { |
| 76 | void *ret = kmalloc(size); |
| 77 | return ret; |
| 78 | } |
| 79 | |
| 80 | void uacpi_kernel_free(void *mem) { |
| 81 | kfree(mem); |
| 82 | } |
| 83 | |
| 84 | uacpi_status uacpi_kernel_io_map(uacpi_io_addr base, uacpi_size len, |
| 85 | uacpi_handle *out_handle) { |
| 86 | if (!out_handle || len == 0) { |
| 87 | return UACPI_STATUS_INVALID_ARGUMENT; |
| 88 | } |
| 89 | |
| 90 | uacpi_io_handle *handle = |
| 91 | (uacpi_io_handle *) kmalloc(sizeof(uacpi_io_handle)); |
| 92 | if (!handle) { |
| 93 | return UACPI_STATUS_INTERNAL_ERROR; |
| 94 | } |
| 95 | |
| 96 | handle->base = base; |
| 97 | handle->len = len; |
| 98 | handle->valid = true; |
| 99 | |
| 100 | *out_handle = (uacpi_handle) handle; |
| 101 | return UACPI_STATUS_OK; |
| 102 | } |
| 103 | |
| 104 | void uacpi_kernel_io_unmap(uacpi_handle h) { |
| 105 | if (!h) |
| 106 | return; |
| 107 | |
| 108 | uacpi_io_handle *handle = (uacpi_io_handle *) h; |
| 109 | handle->valid = false; |
| 110 | kfree(handle); |
| 111 | } |
| 112 | |
| 113 | uacpi_u64 uacpi_kernel_get_nanoseconds_since_boot(void) { |
| 114 | |
| 115 | return (rdtsc() * 1000000000ull) / tsc_freq; |
| 116 | } |
| 117 | |
| 118 | void uacpi_kernel_stall(uacpi_u8 usec) { |
| 119 | |
| 120 | uint64_t start = rdtsc(); |
| 121 | uint64_t target = start + ((tsc_freq / 1000000ull) * usec); |
| 122 | |
| 123 | while (rdtsc() < target) |
| 124 | cpu_relax(); |
| 125 | } |
| 126 | |
| 127 | void uacpi_kernel_sleep(uacpi_u64 msec) { |
| 128 | |
| 129 | for (uacpi_u64 i = 0; i < msec * 10; i++) |
| 130 | uacpi_kernel_stall(usec: 100); |
| 131 | } |
| 132 | |
| 133 | uacpi_status uacpi_kernel_install_interrupt_handler( |
| 134 | uacpi_u32 irq, uacpi_interrupt_handler handler, uacpi_handle ctx, |
| 135 | uacpi_handle *out_irq_handle) { |
| 136 | irq += 32; |
| 137 | |
| 138 | if (irq >= 256) |
| 139 | return UACPI_STATUS_INVALID_ARGUMENT; |
| 140 | |
| 141 | irq_register(name: "uacpi" , vector: irq, handler: (void *) handler, ctx, flags: IRQ_FLAG_NONE); |
| 142 | |
| 143 | if (out_irq_handle) |
| 144 | *out_irq_handle = (uacpi_handle) (uintptr_t) irq; |
| 145 | |
| 146 | return UACPI_STATUS_OK; |
| 147 | } |
| 148 | |
| 149 | uacpi_status |
| 150 | uacpi_kernel_uninstall_interrupt_handler(uacpi_interrupt_handler handler, |
| 151 | uacpi_handle irq_handle) { |
| 152 | uint32_t irq = (uint32_t) (uintptr_t) irq_handle; |
| 153 | irq += 32; |
| 154 | |
| 155 | (void) handler; |
| 156 | |
| 157 | if (irq >= 256) |
| 158 | return UACPI_STATUS_INVALID_ARGUMENT; |
| 159 | |
| 160 | irq_free_entry(entry: irq); |
| 161 | return UACPI_STATUS_OK; |
| 162 | } |
| 163 | |
| 164 | uacpi_handle uacpi_kernel_create_spinlock(void) { |
| 165 | struct spinlock *lock = kmalloc(sizeof(struct spinlock), ALLOC_FLAGS_ZERO); |
| 166 | spinlock_init(lock); |
| 167 | return lock; |
| 168 | } |
| 169 | |
| 170 | void uacpi_kernel_free_spinlock(uacpi_handle a) { |
| 171 | kfree(a); |
| 172 | } |
| 173 | |
| 174 | uacpi_handle uacpi_kernel_create_mutex(void) { |
| 175 | struct mutex_simple *m = |
| 176 | kmalloc(sizeof(struct mutex_simple), ALLOC_FLAGS_ZERO); |
| 177 | mutex_simple_init(m); |
| 178 | return m; |
| 179 | } |
| 180 | |
| 181 | void uacpi_kernel_free_mutex(uacpi_handle a) { |
| 182 | kfree(a); |
| 183 | } |
| 184 | |
| 185 | uacpi_status uacpi_kernel_acquire_mutex(uacpi_handle m, uacpi_u16 b) { |
| 186 | (void) b; |
| 187 | mutex_simple_lock(m); |
| 188 | return UACPI_STATUS_OK; |
| 189 | } |
| 190 | |
| 191 | void uacpi_kernel_release_mutex(uacpi_handle m) { |
| 192 | mutex_simple_unlock(m); |
| 193 | } |
| 194 | |
| 195 | uacpi_cpu_flags uacpi_kernel_lock_spinlock(uacpi_handle a) { |
| 196 | return spin_lock(lock: (struct spinlock *) a); |
| 197 | } |
| 198 | |
| 199 | void uacpi_kernel_unlock_spinlock(uacpi_handle a, uacpi_cpu_flags b) { |
| 200 | spin_unlock(lock: (struct spinlock *) a, old: b); |
| 201 | } |
| 202 | |
| 203 | uacpi_thread_id uacpi_kernel_get_thread_id(void) { |
| 204 | return thread_get_current(); |
| 205 | } |
| 206 | |
| 207 | // |
| 208 | // |
| 209 | // |
| 210 | // stuff down here is unfinished/not complete |
| 211 | // vvvvvvvvvvv |
| 212 | // |
| 213 | // |
| 214 | // |
| 215 | // |
| 216 | // |
| 217 | |
| 218 | uacpi_status uacpi_kernel_schedule_work(uacpi_work_type, uacpi_work_handler, |
| 219 | uacpi_handle) { |
| 220 | return UACPI_STATUS_OK; |
| 221 | } |
| 222 | |
| 223 | uacpi_status uacpi_kernel_wait_for_work_completion(void) { |
| 224 | |
| 225 | return UACPI_STATUS_OK; |
| 226 | } |
| 227 | |
| 228 | uacpi_handle uacpi_kernel_create_event(void) { |
| 229 | |
| 230 | return kmalloc(8, ALLOC_FLAGS_ZERO); |
| 231 | } |
| 232 | void uacpi_kernel_free_event(uacpi_handle a) { |
| 233 | |
| 234 | kfree(a); |
| 235 | } |
| 236 | |
| 237 | uacpi_bool uacpi_kernel_wait_for_event(uacpi_handle, uacpi_u16) { |
| 238 | return false; |
| 239 | } |
| 240 | void uacpi_kernel_signal_event(uacpi_handle) {} |
| 241 | |
| 242 | void uacpi_kernel_reset_event(uacpi_handle) {} |
| 243 | |
| 244 | uacpi_status uacpi_kernel_handle_firmware_request(uacpi_firmware_request *) { |
| 245 | |
| 246 | return UACPI_STATUS_UNIMPLEMENTED; |
| 247 | } |
| 248 | |