[a1e17fc] | 1 | /*
|
---|
[df4ed85] | 2 | * Copyright (c) 2006 Jakub Jermar
|
---|
[a1e17fc] | 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
[b2951e2] | 27 | */
|
---|
| 28 |
|
---|
[a46da63] | 29 | /** @addtogroup libc
|
---|
[b2951e2] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
[c0699467] | 33 | */
|
---|
[a1e17fc] | 34 |
|
---|
[c6ae4c2] | 35 | #include <assert.h>
|
---|
[acc0efb] | 36 | #include <atomic.h>
|
---|
[c6ae4c2] | 37 | #include <unistd.h>
|
---|
[acc0efb] | 38 | #include <stdio.h>
|
---|
[c6ae4c2] | 39 | #include <errno.h>
|
---|
[c0699467] | 40 | #include <sys/types.h>
|
---|
| 41 | #include <abi/ddi/arg.h>
|
---|
[a1e17fc] | 42 | #include <ddi.h>
|
---|
[1ae8f2b] | 43 | #include <libarch/ddi.h>
|
---|
[8049b79] | 44 | #include <device/hw_res.h>
|
---|
| 45 | #include <device/hw_res_parsed.h>
|
---|
| 46 | #include <device/pio_window.h>
|
---|
[a1e17fc] | 47 | #include <libc.h>
|
---|
| 48 | #include <task.h>
|
---|
[0d5a50c] | 49 | #include <as.h>
|
---|
| 50 | #include <align.h>
|
---|
| 51 | #include <libarch/config.h>
|
---|
[fbcdeb8] | 52 | #include "private/libc.h"
|
---|
[a1e17fc] | 53 |
|
---|
[acc0efb] | 54 |
|
---|
[84afc7b] | 55 | /** Return unique device number.
|
---|
| 56 | *
|
---|
| 57 | * @return New unique device number.
|
---|
| 58 | *
|
---|
| 59 | */
|
---|
| 60 | int device_assign_devno(void)
|
---|
| 61 | {
|
---|
| 62 | return __SYSCALL0(SYS_DEVICE_ASSIGN_DEVNO);
|
---|
| 63 | }
|
---|
| 64 |
|
---|
[28f8f170] | 65 | /** Map a piece of physical memory to task.
|
---|
[a1e17fc] | 66 | *
|
---|
| 67 | * Caller of this function must have the CAP_MEM_MANAGER capability.
|
---|
| 68 | *
|
---|
[c6ae4c2] | 69 | * @param phys Physical address of the starting frame.
|
---|
[28f8f170] | 70 | * @param pages Number of pages to map.
|
---|
| 71 | * @param flags Flags for the new address space area.
|
---|
[fbcdeb8] | 72 | * @param virt Virtual address of the starting page.
|
---|
[bf9cb2f] | 73 | * If set to AS_AREA_ANY ((void *) -1), a suitable value
|
---|
| 74 | * is found by the kernel, otherwise the kernel tries to
|
---|
| 75 | * obey the desired value.
|
---|
[28f8f170] | 76 | *
|
---|
[bf9cb2f] | 77 | * @return EOK on success.
|
---|
| 78 | * @return EPERM if the caller lacks the CAP_MEM_MANAGER capability.
|
---|
[28f8f170] | 79 | * @return ENOMEM if there was some problem in creating
|
---|
| 80 | * the address space area.
|
---|
[a1e17fc] | 81 | *
|
---|
| 82 | */
|
---|
[8442d10] | 83 | int physmem_map(uintptr_t phys, size_t pages, unsigned int flags, void **virt)
|
---|
[a1e17fc] | 84 | {
|
---|
[fbcdeb8] | 85 | return __SYSCALL5(SYS_PHYSMEM_MAP, (sysarg_t) phys,
|
---|
| 86 | pages, flags, (sysarg_t) virt, (sysarg_t) __entry);
|
---|
[a1e17fc] | 87 | }
|
---|
[9426c1a3] | 88 |
|
---|
[bf9cb2f] | 89 | /** Lock a piece physical memory for DMA transfers.
|
---|
| 90 | *
|
---|
| 91 | * The mapping of the specified virtual memory address
|
---|
| 92 | * to physical memory address is locked in order to
|
---|
| 93 | * make it safe for DMA transferts.
|
---|
| 94 | *
|
---|
| 95 | * Caller of this function must have the CAP_MEM_MANAGER capability.
|
---|
| 96 | *
|
---|
| 97 | * @param virt Virtual address of the memory to be locked.
|
---|
| 98 | * @param size Number of bytes to lock.
|
---|
| 99 | * @param map_flags Desired virtual memory area flags.
|
---|
| 100 | * @param flags Flags for the physical memory address.
|
---|
| 101 | * @param phys Locked physical memory address.
|
---|
| 102 | *
|
---|
| 103 | * @return EOK on success.
|
---|
| 104 | * @return EPERM if the caller lacks the CAP_MEM_MANAGER capability.
|
---|
| 105 | * @return ENOMEM if there was some problem in creating
|
---|
| 106 | * the address space area.
|
---|
| 107 | *
|
---|
| 108 | */
|
---|
[c6ae4c2] | 109 | int dmamem_map(void *virt, size_t size, unsigned int map_flags,
|
---|
[8442d10] | 110 | unsigned int flags, uintptr_t *phys)
|
---|
[fd6bd6d] | 111 | {
|
---|
[fbcdeb8] | 112 | return (int) __SYSCALL6(SYS_DMAMEM_MAP, (sysarg_t) size,
|
---|
| 113 | (sysarg_t) map_flags, (sysarg_t) flags & ~DMAMEM_FLAGS_ANONYMOUS,
|
---|
| 114 | (sysarg_t) phys, (sysarg_t) virt, 0);
|
---|
[fd6bd6d] | 115 | }
|
---|
| 116 |
|
---|
[bf9cb2f] | 117 | /** Map a piece of physical memory suitable for DMA transfers.
|
---|
| 118 | *
|
---|
| 119 | * Caller of this function must have the CAP_MEM_MANAGER capability.
|
---|
| 120 | *
|
---|
| 121 | * @param size Number of bytes to map.
|
---|
| 122 | * @param constraint Bit mask defining the contraint on the physical
|
---|
| 123 | * address to be mapped.
|
---|
| 124 | * @param map_flags Desired virtual memory area flags.
|
---|
| 125 | * @param flags Flags for the physical memory address.
|
---|
| 126 | * @param virt Virtual address of the starting page.
|
---|
| 127 | * If set to AS_AREA_ANY ((void *) -1), a suitable value
|
---|
| 128 | * is found by the kernel, otherwise the kernel tries to
|
---|
| 129 | * obey the desired value.
|
---|
| 130 | *
|
---|
| 131 | * @return EOK on success.
|
---|
| 132 | * @return EPERM if the caller lacks the CAP_MEM_MANAGER capability.
|
---|
| 133 | * @return ENOMEM if there was some problem in creating
|
---|
| 134 | * the address space area.
|
---|
| 135 | *
|
---|
| 136 | */
|
---|
[8442d10] | 137 | int dmamem_map_anonymous(size_t size, uintptr_t constraint,
|
---|
| 138 | unsigned int map_flags, unsigned int flags, uintptr_t *phys, void **virt)
|
---|
[fd6bd6d] | 139 | {
|
---|
[8442d10] | 140 | *phys = constraint;
|
---|
| 141 |
|
---|
[fbcdeb8] | 142 | return (int) __SYSCALL6(SYS_DMAMEM_MAP, (sysarg_t) size,
|
---|
| 143 | (sysarg_t) map_flags, (sysarg_t) flags | DMAMEM_FLAGS_ANONYMOUS,
|
---|
| 144 | (sysarg_t) phys, (sysarg_t) virt, (sysarg_t) __entry);
|
---|
[fd6bd6d] | 145 | }
|
---|
| 146 |
|
---|
[fbcdeb8] | 147 | int dmamem_unmap(void *virt, size_t size)
|
---|
[fd6bd6d] | 148 | {
|
---|
[fbcdeb8] | 149 | return __SYSCALL3(SYS_DMAMEM_UNMAP, (sysarg_t) virt, (sysarg_t) size, 0);
|
---|
[fd6bd6d] | 150 | }
|
---|
| 151 |
|
---|
[c6ae4c2] | 152 | int dmamem_unmap_anonymous(void *virt)
|
---|
[fd6bd6d] | 153 | {
|
---|
[c6ae4c2] | 154 | return __SYSCALL3(SYS_DMAMEM_UNMAP, (sysarg_t) virt, 0,
|
---|
| 155 | DMAMEM_FLAGS_ANONYMOUS);
|
---|
[fd6bd6d] | 156 | }
|
---|
| 157 |
|
---|
[9426c1a3] | 158 | /** Enable I/O space range to task.
|
---|
| 159 | *
|
---|
| 160 | * Caller of this function must have the IO_MEM_MANAGER capability.
|
---|
| 161 | *
|
---|
[fd6bd6d] | 162 | * @param id Task ID.
|
---|
| 163 | * @param ioaddr Starting address of the I/O range.
|
---|
| 164 | * @param size Size of the range.
|
---|
| 165 | *
|
---|
| 166 | * @return EOK on success
|
---|
| 167 | * @return EPERM if the caller lacks the CAP_IO_MANAGER capability
|
---|
| 168 | * @return ENOENT if there is no task with specified ID
|
---|
| 169 | * @return ENOMEM if there was some problem in allocating memory.
|
---|
[9426c1a3] | 170 | *
|
---|
| 171 | */
|
---|
[edb0a33] | 172 | static int iospace_enable(task_id_t id, void *ioaddr, size_t size)
|
---|
[9426c1a3] | 173 | {
|
---|
[edb0a33] | 174 | const ddi_ioarg_t arg = {
|
---|
| 175 | .task_id = id,
|
---|
| 176 | .ioaddr = ioaddr,
|
---|
| 177 | .size = size
|
---|
| 178 | };
|
---|
[fd6bd6d] | 179 |
|
---|
[5140e3e] | 180 | return __SYSCALL1(SYS_IOSPACE_ENABLE, (sysarg_t) &arg);
|
---|
[9426c1a3] | 181 | }
|
---|
[3d77747] | 182 |
|
---|
[8049b79] | 183 | /** Enable PIO for specified address range.
|
---|
| 184 | *
|
---|
| 185 | * @param range I/O range to be enable.
|
---|
| 186 | * @param virt Virtual address for application's PIO operations.
|
---|
| 187 | */
|
---|
| 188 | int pio_enable_range(addr_range_t *range, void **virt)
|
---|
| 189 | {
|
---|
| 190 | return pio_enable(RNGABSPTR(*range), RNGSZ(*range), virt);
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 | /** Enable PIO for specified HW resource wrt. to the PIO window.
|
---|
[eeb5cc2] | 194 | *
|
---|
| 195 | * @param win PIO window. May be NULL if the resources are known to be
|
---|
| 196 | * absolute.
|
---|
| 197 | * @param res Resources specifying the I/O range wrt. to the PIO window.
|
---|
| 198 | * @param virt Virtual address for application's PIO operations.
|
---|
| 199 | *
|
---|
| 200 | * @return EOK on success.
|
---|
| 201 | * @return Negative error code on failure.
|
---|
| 202 | *
|
---|
| 203 | */
|
---|
| 204 | int pio_enable_resource(pio_window_t *win, hw_resource_t *res, void **virt)
|
---|
| 205 | {
|
---|
| 206 | uintptr_t addr;
|
---|
| 207 | size_t size;
|
---|
| 208 |
|
---|
| 209 | switch (res->type) {
|
---|
| 210 | case IO_RANGE:
|
---|
| 211 | addr = res->res.io_range.address;
|
---|
| 212 | if (res->res.io_range.relative) {
|
---|
| 213 | if (!win)
|
---|
| 214 | return EINVAL;
|
---|
| 215 | addr += win->io.base;
|
---|
| 216 | }
|
---|
| 217 | size = res->res.io_range.size;
|
---|
| 218 | break;
|
---|
| 219 | case MEM_RANGE:
|
---|
| 220 | addr = res->res.mem_range.address;
|
---|
| 221 | if (res->res.mem_range.relative) {
|
---|
| 222 | if (!win)
|
---|
| 223 | return EINVAL;
|
---|
| 224 | addr += win->mem.base;
|
---|
| 225 | }
|
---|
| 226 | size = res->res.mem_range.size;
|
---|
| 227 | break;
|
---|
| 228 | default:
|
---|
| 229 | return EINVAL;
|
---|
| 230 | }
|
---|
| 231 |
|
---|
| 232 | return pio_enable((void *) addr, size, virt);
|
---|
| 233 | }
|
---|
| 234 |
|
---|
[0d5a50c] | 235 | /** Enable PIO for specified I/O range.
|
---|
| 236 | *
|
---|
[fd6bd6d] | 237 | * @param pio_addr I/O start address.
|
---|
| 238 | * @param size Size of the I/O region.
|
---|
[fbcdeb8] | 239 | * @param virt Virtual address for application's
|
---|
[6659037] | 240 | * PIO operations. Can be NULL for PMIO.
|
---|
[fd6bd6d] | 241 | *
|
---|
[fbcdeb8] | 242 | * @return EOK on success.
|
---|
| 243 | * @return Negative error code on failure.
|
---|
[0d5a50c] | 244 | *
|
---|
| 245 | */
|
---|
[fbcdeb8] | 246 | int pio_enable(void *pio_addr, size_t size, void **virt)
|
---|
[0d5a50c] | 247 | {
|
---|
| 248 | #ifdef IO_SPACE_BOUNDARY
|
---|
| 249 | if (pio_addr < IO_SPACE_BOUNDARY) {
|
---|
[6659037] | 250 | if (virt)
|
---|
| 251 | *virt = pio_addr;
|
---|
[0d5a50c] | 252 | return iospace_enable(task_get_id(), pio_addr, size);
|
---|
| 253 | }
|
---|
[aac1c417] | 254 | #else
|
---|
| 255 | (void) iospace_enable;
|
---|
[0d5a50c] | 256 | #endif
|
---|
[6659037] | 257 | if (!virt)
|
---|
| 258 | return EINVAL;
|
---|
[8442d10] | 259 |
|
---|
| 260 | uintptr_t phys_frame =
|
---|
| 261 | ALIGN_DOWN((uintptr_t) pio_addr, PAGE_SIZE);
|
---|
| 262 | size_t offset = (uintptr_t) pio_addr - phys_frame;
|
---|
[fbcdeb8] | 263 | size_t pages = SIZE2PAGES(offset + size);
|
---|
| 264 |
|
---|
[bf9cb2f] | 265 | void *virt_page = AS_AREA_ANY;
|
---|
[fbcdeb8] | 266 | int rc = physmem_map(phys_frame, pages,
|
---|
| 267 | AS_AREA_READ | AS_AREA_WRITE, &virt_page);
|
---|
| 268 | if (rc != EOK)
|
---|
| 269 | return rc;
|
---|
| 270 |
|
---|
| 271 | *virt = virt_page + offset;
|
---|
| 272 | return EOK;
|
---|
[0d5a50c] | 273 | }
|
---|
| 274 |
|
---|
[3218648] | 275 | void pio_write_8(ioport8_t *reg, uint8_t val)
|
---|
| 276 | {
|
---|
[acc0efb] | 277 | pio_trace_log(reg, val, true);
|
---|
[3218648] | 278 | arch_pio_write_8(reg, val);
|
---|
| 279 | }
|
---|
| 280 |
|
---|
| 281 | void pio_write_16(ioport16_t *reg, uint16_t val)
|
---|
| 282 | {
|
---|
[acc0efb] | 283 | pio_trace_log(reg, val, true);
|
---|
[3218648] | 284 | arch_pio_write_16(reg, val);
|
---|
| 285 | }
|
---|
| 286 |
|
---|
| 287 | void pio_write_32(ioport32_t *reg, uint32_t val)
|
---|
| 288 | {
|
---|
[acc0efb] | 289 | pio_trace_log(reg, val, true);
|
---|
[3218648] | 290 | arch_pio_write_32(reg, val);
|
---|
| 291 | }
|
---|
| 292 |
|
---|
[b5c2f56] | 293 | uint8_t pio_read_8(const ioport8_t *reg)
|
---|
[3218648] | 294 | {
|
---|
[acc0efb] | 295 | const uint8_t val = arch_pio_read_8(reg);
|
---|
| 296 | pio_trace_log(reg, val, false);
|
---|
| 297 | return val;
|
---|
[3218648] | 298 | }
|
---|
| 299 |
|
---|
[b5c2f56] | 300 | uint16_t pio_read_16(const ioport16_t *reg)
|
---|
[3218648] | 301 | {
|
---|
[acc0efb] | 302 | const uint16_t val = arch_pio_read_16(reg);
|
---|
| 303 | pio_trace_log(reg, val, false);
|
---|
| 304 | return val;
|
---|
[3218648] | 305 | }
|
---|
| 306 |
|
---|
[b5c2f56] | 307 | uint32_t pio_read_32(const ioport32_t *reg)
|
---|
[3218648] | 308 | {
|
---|
[acc0efb] | 309 | const uint32_t val = arch_pio_read_32(reg);
|
---|
| 310 | pio_trace_log(reg, val, false);
|
---|
| 311 | return val;
|
---|
[3218648] | 312 | }
|
---|
| 313 |
|
---|
[64d2b10] | 314 | /** Register IRQ notification.
|
---|
| 315 | *
|
---|
| 316 | * @param inr IRQ number.
|
---|
| 317 | * @param devno Device number of the device generating inr.
|
---|
| 318 | * @param method Use this method for notifying me.
|
---|
| 319 | * @param ucode Top-half pseudocode handler.
|
---|
| 320 | *
|
---|
| 321 | * @return Value returned by the kernel.
|
---|
| 322 | *
|
---|
| 323 | */
|
---|
[9359aae] | 324 | int irq_register(int inr, int devno, int method, const irq_code_t *ucode)
|
---|
[64d2b10] | 325 | {
|
---|
[f044e96] | 326 | return __SYSCALL4(SYS_IRQ_REGISTER, inr, devno, method,
|
---|
[64d2b10] | 327 | (sysarg_t) ucode);
|
---|
| 328 | }
|
---|
| 329 |
|
---|
| 330 | /** Unregister IRQ notification.
|
---|
| 331 | *
|
---|
| 332 | * @param inr IRQ number.
|
---|
| 333 | * @param devno Device number of the device generating inr.
|
---|
| 334 | *
|
---|
| 335 | * @return Value returned by the kernel.
|
---|
| 336 | *
|
---|
| 337 | */
|
---|
[f044e96] | 338 | int irq_unregister(int inr, int devno)
|
---|
[64d2b10] | 339 | {
|
---|
[f044e96] | 340 | return __SYSCALL2(SYS_IRQ_UNREGISTER, inr, devno);
|
---|
[64d2b10] | 341 | }
|
---|
| 342 |
|
---|
[a46da63] | 343 | /** @}
|
---|
[b2951e2] | 344 | */
|
---|