[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>
|
---|
[a1e17fc] | 44 | #include <libc.h>
|
---|
| 45 | #include <task.h>
|
---|
[0d5a50c] | 46 | #include <as.h>
|
---|
| 47 | #include <align.h>
|
---|
| 48 | #include <libarch/config.h>
|
---|
[fbcdeb8] | 49 | #include "private/libc.h"
|
---|
[a1e17fc] | 50 |
|
---|
[acc0efb] | 51 |
|
---|
[84afc7b] | 52 | /** Return unique device number.
|
---|
| 53 | *
|
---|
| 54 | * @return New unique device number.
|
---|
| 55 | *
|
---|
| 56 | */
|
---|
| 57 | int device_assign_devno(void)
|
---|
| 58 | {
|
---|
| 59 | return __SYSCALL0(SYS_DEVICE_ASSIGN_DEVNO);
|
---|
| 60 | }
|
---|
| 61 |
|
---|
[28f8f170] | 62 | /** Map a piece of physical memory to task.
|
---|
[a1e17fc] | 63 | *
|
---|
| 64 | * Caller of this function must have the CAP_MEM_MANAGER capability.
|
---|
| 65 | *
|
---|
[c6ae4c2] | 66 | * @param phys Physical address of the starting frame.
|
---|
[28f8f170] | 67 | * @param pages Number of pages to map.
|
---|
| 68 | * @param flags Flags for the new address space area.
|
---|
[fbcdeb8] | 69 | * @param virt Virtual address of the starting page.
|
---|
[28f8f170] | 70 | *
|
---|
| 71 | * @return EOK on success
|
---|
| 72 | * @return EPERM if the caller lacks the CAP_MEM_MANAGER capability
|
---|
| 73 | * @return ENOENT if there is no task with specified ID
|
---|
| 74 | * @return ENOMEM if there was some problem in creating
|
---|
| 75 | * the address space area.
|
---|
[a1e17fc] | 76 | *
|
---|
| 77 | */
|
---|
[8442d10] | 78 | int physmem_map(uintptr_t phys, size_t pages, unsigned int flags, void **virt)
|
---|
[a1e17fc] | 79 | {
|
---|
[fbcdeb8] | 80 | return __SYSCALL5(SYS_PHYSMEM_MAP, (sysarg_t) phys,
|
---|
| 81 | pages, flags, (sysarg_t) virt, (sysarg_t) __entry);
|
---|
[a1e17fc] | 82 | }
|
---|
[9426c1a3] | 83 |
|
---|
[c6ae4c2] | 84 | int dmamem_map(void *virt, size_t size, unsigned int map_flags,
|
---|
[8442d10] | 85 | unsigned int flags, uintptr_t *phys)
|
---|
[fd6bd6d] | 86 | {
|
---|
[fbcdeb8] | 87 | return (int) __SYSCALL6(SYS_DMAMEM_MAP, (sysarg_t) size,
|
---|
| 88 | (sysarg_t) map_flags, (sysarg_t) flags & ~DMAMEM_FLAGS_ANONYMOUS,
|
---|
| 89 | (sysarg_t) phys, (sysarg_t) virt, 0);
|
---|
[fd6bd6d] | 90 | }
|
---|
| 91 |
|
---|
[8442d10] | 92 | int dmamem_map_anonymous(size_t size, uintptr_t constraint,
|
---|
| 93 | unsigned int map_flags, unsigned int flags, uintptr_t *phys, void **virt)
|
---|
[fd6bd6d] | 94 | {
|
---|
[8442d10] | 95 | *phys = constraint;
|
---|
| 96 |
|
---|
[fbcdeb8] | 97 | return (int) __SYSCALL6(SYS_DMAMEM_MAP, (sysarg_t) size,
|
---|
| 98 | (sysarg_t) map_flags, (sysarg_t) flags | DMAMEM_FLAGS_ANONYMOUS,
|
---|
| 99 | (sysarg_t) phys, (sysarg_t) virt, (sysarg_t) __entry);
|
---|
[fd6bd6d] | 100 | }
|
---|
| 101 |
|
---|
[fbcdeb8] | 102 | int dmamem_unmap(void *virt, size_t size)
|
---|
[fd6bd6d] | 103 | {
|
---|
[fbcdeb8] | 104 | return __SYSCALL3(SYS_DMAMEM_UNMAP, (sysarg_t) virt, (sysarg_t) size, 0);
|
---|
[fd6bd6d] | 105 | }
|
---|
| 106 |
|
---|
[c6ae4c2] | 107 | int dmamem_unmap_anonymous(void *virt)
|
---|
[fd6bd6d] | 108 | {
|
---|
[c6ae4c2] | 109 | return __SYSCALL3(SYS_DMAMEM_UNMAP, (sysarg_t) virt, 0,
|
---|
| 110 | DMAMEM_FLAGS_ANONYMOUS);
|
---|
[fd6bd6d] | 111 | }
|
---|
| 112 |
|
---|
[9426c1a3] | 113 | /** Enable I/O space range to task.
|
---|
| 114 | *
|
---|
| 115 | * Caller of this function must have the IO_MEM_MANAGER capability.
|
---|
| 116 | *
|
---|
[fd6bd6d] | 117 | * @param id Task ID.
|
---|
| 118 | * @param ioaddr Starting address of the I/O range.
|
---|
| 119 | * @param size Size of the range.
|
---|
| 120 | *
|
---|
| 121 | * @return EOK on success
|
---|
| 122 | * @return EPERM if the caller lacks the CAP_IO_MANAGER capability
|
---|
| 123 | * @return ENOENT if there is no task with specified ID
|
---|
| 124 | * @return ENOMEM if there was some problem in allocating memory.
|
---|
[9426c1a3] | 125 | *
|
---|
| 126 | */
|
---|
[edb0a33] | 127 | static int iospace_enable(task_id_t id, void *ioaddr, size_t size)
|
---|
[9426c1a3] | 128 | {
|
---|
[edb0a33] | 129 | const ddi_ioarg_t arg = {
|
---|
| 130 | .task_id = id,
|
---|
| 131 | .ioaddr = ioaddr,
|
---|
| 132 | .size = size
|
---|
| 133 | };
|
---|
[fd6bd6d] | 134 |
|
---|
[5140e3e] | 135 | return __SYSCALL1(SYS_IOSPACE_ENABLE, (sysarg_t) &arg);
|
---|
[9426c1a3] | 136 | }
|
---|
[3d77747] | 137 |
|
---|
[eeb5cc2] | 138 | /** Enable PIO for specified HW resource.
|
---|
| 139 | *
|
---|
| 140 | * @param win PIO window. May be NULL if the resources are known to be
|
---|
| 141 | * absolute.
|
---|
| 142 | * @param res Resources specifying the I/O range wrt. to the PIO window.
|
---|
| 143 | * @param virt Virtual address for application's PIO operations.
|
---|
| 144 | *
|
---|
| 145 | * @return EOK on success.
|
---|
| 146 | * @return Negative error code on failure.
|
---|
| 147 | *
|
---|
| 148 | */
|
---|
| 149 | int pio_enable_resource(pio_window_t *win, hw_resource_t *res, void **virt)
|
---|
| 150 | {
|
---|
| 151 | uintptr_t addr;
|
---|
| 152 | size_t size;
|
---|
| 153 |
|
---|
| 154 | switch (res->type) {
|
---|
| 155 | case IO_RANGE:
|
---|
| 156 | addr = res->res.io_range.address;
|
---|
| 157 | if (res->res.io_range.relative) {
|
---|
| 158 | if (!win)
|
---|
| 159 | return EINVAL;
|
---|
| 160 | addr += win->io.base;
|
---|
| 161 | }
|
---|
| 162 | size = res->res.io_range.size;
|
---|
| 163 | break;
|
---|
| 164 | case MEM_RANGE:
|
---|
| 165 | addr = res->res.mem_range.address;
|
---|
| 166 | if (res->res.mem_range.relative) {
|
---|
| 167 | if (!win)
|
---|
| 168 | return EINVAL;
|
---|
| 169 | addr += win->mem.base;
|
---|
| 170 | }
|
---|
| 171 | size = res->res.mem_range.size;
|
---|
| 172 | break;
|
---|
| 173 | default:
|
---|
| 174 | return EINVAL;
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | return pio_enable((void *) addr, size, virt);
|
---|
| 178 | }
|
---|
| 179 |
|
---|
[0d5a50c] | 180 | /** Enable PIO for specified I/O range.
|
---|
| 181 | *
|
---|
[fd6bd6d] | 182 | * @param pio_addr I/O start address.
|
---|
| 183 | * @param size Size of the I/O region.
|
---|
[fbcdeb8] | 184 | * @param virt Virtual address for application's
|
---|
[6659037] | 185 | * PIO operations. Can be NULL for PMIO.
|
---|
[fd6bd6d] | 186 | *
|
---|
[fbcdeb8] | 187 | * @return EOK on success.
|
---|
| 188 | * @return Negative error code on failure.
|
---|
[0d5a50c] | 189 | *
|
---|
| 190 | */
|
---|
[fbcdeb8] | 191 | int pio_enable(void *pio_addr, size_t size, void **virt)
|
---|
[0d5a50c] | 192 | {
|
---|
| 193 | #ifdef IO_SPACE_BOUNDARY
|
---|
| 194 | if (pio_addr < IO_SPACE_BOUNDARY) {
|
---|
[6659037] | 195 | if (virt)
|
---|
| 196 | *virt = pio_addr;
|
---|
[0d5a50c] | 197 | return iospace_enable(task_get_id(), pio_addr, size);
|
---|
| 198 | }
|
---|
[aac1c417] | 199 | #else
|
---|
| 200 | (void) iospace_enable;
|
---|
[0d5a50c] | 201 | #endif
|
---|
[6659037] | 202 | if (!virt)
|
---|
| 203 | return EINVAL;
|
---|
[8442d10] | 204 |
|
---|
| 205 | uintptr_t phys_frame =
|
---|
| 206 | ALIGN_DOWN((uintptr_t) pio_addr, PAGE_SIZE);
|
---|
| 207 | size_t offset = (uintptr_t) pio_addr - phys_frame;
|
---|
[fbcdeb8] | 208 | size_t pages = SIZE2PAGES(offset + size);
|
---|
| 209 |
|
---|
| 210 | void *virt_page;
|
---|
| 211 | int rc = physmem_map(phys_frame, pages,
|
---|
| 212 | AS_AREA_READ | AS_AREA_WRITE, &virt_page);
|
---|
| 213 | if (rc != EOK)
|
---|
| 214 | return rc;
|
---|
| 215 |
|
---|
| 216 | *virt = virt_page + offset;
|
---|
| 217 | return EOK;
|
---|
[0d5a50c] | 218 | }
|
---|
| 219 |
|
---|
[3218648] | 220 | void pio_write_8(ioport8_t *reg, uint8_t val)
|
---|
| 221 | {
|
---|
[acc0efb] | 222 | pio_trace_log(reg, val, true);
|
---|
[3218648] | 223 | arch_pio_write_8(reg, val);
|
---|
| 224 | }
|
---|
| 225 |
|
---|
| 226 | void pio_write_16(ioport16_t *reg, uint16_t val)
|
---|
| 227 | {
|
---|
[acc0efb] | 228 | pio_trace_log(reg, val, true);
|
---|
[3218648] | 229 | arch_pio_write_16(reg, val);
|
---|
| 230 | }
|
---|
| 231 |
|
---|
| 232 | void pio_write_32(ioport32_t *reg, uint32_t val)
|
---|
| 233 | {
|
---|
[acc0efb] | 234 | pio_trace_log(reg, val, true);
|
---|
[3218648] | 235 | arch_pio_write_32(reg, val);
|
---|
| 236 | }
|
---|
| 237 |
|
---|
[b5c2f56] | 238 | uint8_t pio_read_8(const ioport8_t *reg)
|
---|
[3218648] | 239 | {
|
---|
[acc0efb] | 240 | const uint8_t val = arch_pio_read_8(reg);
|
---|
| 241 | pio_trace_log(reg, val, false);
|
---|
| 242 | return val;
|
---|
[3218648] | 243 | }
|
---|
| 244 |
|
---|
[b5c2f56] | 245 | uint16_t pio_read_16(const ioport16_t *reg)
|
---|
[3218648] | 246 | {
|
---|
[acc0efb] | 247 | const uint16_t val = arch_pio_read_16(reg);
|
---|
| 248 | pio_trace_log(reg, val, false);
|
---|
| 249 | return val;
|
---|
[3218648] | 250 | }
|
---|
| 251 |
|
---|
[b5c2f56] | 252 | uint32_t pio_read_32(const ioport32_t *reg)
|
---|
[3218648] | 253 | {
|
---|
[acc0efb] | 254 | const uint32_t val = arch_pio_read_32(reg);
|
---|
| 255 | pio_trace_log(reg, val, false);
|
---|
| 256 | return val;
|
---|
[3218648] | 257 | }
|
---|
| 258 |
|
---|
[64d2b10] | 259 | /** Register IRQ notification.
|
---|
| 260 | *
|
---|
| 261 | * @param inr IRQ number.
|
---|
| 262 | * @param devno Device number of the device generating inr.
|
---|
| 263 | * @param method Use this method for notifying me.
|
---|
| 264 | * @param ucode Top-half pseudocode handler.
|
---|
| 265 | *
|
---|
| 266 | * @return Value returned by the kernel.
|
---|
| 267 | *
|
---|
| 268 | */
|
---|
[f044e96] | 269 | int irq_register(int inr, int devno, int method, irq_code_t *ucode)
|
---|
[64d2b10] | 270 | {
|
---|
[f044e96] | 271 | return __SYSCALL4(SYS_IRQ_REGISTER, inr, devno, method,
|
---|
[64d2b10] | 272 | (sysarg_t) ucode);
|
---|
| 273 | }
|
---|
| 274 |
|
---|
| 275 | /** Unregister IRQ notification.
|
---|
| 276 | *
|
---|
| 277 | * @param inr IRQ number.
|
---|
| 278 | * @param devno Device number of the device generating inr.
|
---|
| 279 | *
|
---|
| 280 | * @return Value returned by the kernel.
|
---|
| 281 | *
|
---|
| 282 | */
|
---|
[f044e96] | 283 | int irq_unregister(int inr, int devno)
|
---|
[64d2b10] | 284 | {
|
---|
[f044e96] | 285 | return __SYSCALL2(SYS_IRQ_UNREGISTER, inr, devno);
|
---|
[64d2b10] | 286 | }
|
---|
| 287 |
|
---|
[a46da63] | 288 | /** @}
|
---|
[b2951e2] | 289 | */
|
---|