[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 |
|
---|
[8cd680c] | 89 | /** Unmap a piece of physical memory to task.
|
---|
| 90 | *
|
---|
| 91 | * Caller of this function must have the CAP_MEM_MANAGER capability.
|
---|
| 92 | *
|
---|
| 93 | * @param virt Virtual address from the phys-mapped region.
|
---|
| 94 | *
|
---|
| 95 | * @return EOK on success.
|
---|
| 96 | * @return EPERM if the caller lacks the CAP_MEM_MANAGER capability.
|
---|
| 97 | *
|
---|
| 98 | */
|
---|
| 99 | int physmem_unmap(void *virt)
|
---|
| 100 | {
|
---|
| 101 | return __SYSCALL1(SYS_PHYSMEM_UNMAP, (sysarg_t) virt);
|
---|
| 102 | }
|
---|
| 103 |
|
---|
[bf9cb2f] | 104 | /** Lock a piece physical memory for DMA transfers.
|
---|
| 105 | *
|
---|
| 106 | * The mapping of the specified virtual memory address
|
---|
| 107 | * to physical memory address is locked in order to
|
---|
| 108 | * make it safe for DMA transferts.
|
---|
| 109 | *
|
---|
| 110 | * Caller of this function must have the CAP_MEM_MANAGER capability.
|
---|
| 111 | *
|
---|
| 112 | * @param virt Virtual address of the memory to be locked.
|
---|
| 113 | * @param size Number of bytes to lock.
|
---|
| 114 | * @param map_flags Desired virtual memory area flags.
|
---|
| 115 | * @param flags Flags for the physical memory address.
|
---|
| 116 | * @param phys Locked physical memory address.
|
---|
| 117 | *
|
---|
| 118 | * @return EOK on success.
|
---|
| 119 | * @return EPERM if the caller lacks the CAP_MEM_MANAGER capability.
|
---|
| 120 | * @return ENOMEM if there was some problem in creating
|
---|
| 121 | * the address space area.
|
---|
| 122 | *
|
---|
| 123 | */
|
---|
[c6ae4c2] | 124 | int dmamem_map(void *virt, size_t size, unsigned int map_flags,
|
---|
[8442d10] | 125 | unsigned int flags, uintptr_t *phys)
|
---|
[fd6bd6d] | 126 | {
|
---|
[fbcdeb8] | 127 | return (int) __SYSCALL6(SYS_DMAMEM_MAP, (sysarg_t) size,
|
---|
| 128 | (sysarg_t) map_flags, (sysarg_t) flags & ~DMAMEM_FLAGS_ANONYMOUS,
|
---|
| 129 | (sysarg_t) phys, (sysarg_t) virt, 0);
|
---|
[fd6bd6d] | 130 | }
|
---|
| 131 |
|
---|
[bf9cb2f] | 132 | /** Map a piece of physical memory suitable for DMA transfers.
|
---|
| 133 | *
|
---|
| 134 | * Caller of this function must have the CAP_MEM_MANAGER capability.
|
---|
| 135 | *
|
---|
| 136 | * @param size Number of bytes to map.
|
---|
| 137 | * @param constraint Bit mask defining the contraint on the physical
|
---|
| 138 | * address to be mapped.
|
---|
| 139 | * @param map_flags Desired virtual memory area flags.
|
---|
| 140 | * @param flags Flags for the physical memory address.
|
---|
| 141 | * @param virt Virtual address of the starting page.
|
---|
| 142 | * If set to AS_AREA_ANY ((void *) -1), a suitable value
|
---|
| 143 | * is found by the kernel, otherwise the kernel tries to
|
---|
| 144 | * obey the desired value.
|
---|
| 145 | *
|
---|
| 146 | * @return EOK on success.
|
---|
| 147 | * @return EPERM if the caller lacks the CAP_MEM_MANAGER capability.
|
---|
| 148 | * @return ENOMEM if there was some problem in creating
|
---|
| 149 | * the address space area.
|
---|
| 150 | *
|
---|
| 151 | */
|
---|
[8442d10] | 152 | int dmamem_map_anonymous(size_t size, uintptr_t constraint,
|
---|
| 153 | unsigned int map_flags, unsigned int flags, uintptr_t *phys, void **virt)
|
---|
[fd6bd6d] | 154 | {
|
---|
[8442d10] | 155 | *phys = constraint;
|
---|
| 156 |
|
---|
[fbcdeb8] | 157 | return (int) __SYSCALL6(SYS_DMAMEM_MAP, (sysarg_t) size,
|
---|
| 158 | (sysarg_t) map_flags, (sysarg_t) flags | DMAMEM_FLAGS_ANONYMOUS,
|
---|
| 159 | (sysarg_t) phys, (sysarg_t) virt, (sysarg_t) __entry);
|
---|
[fd6bd6d] | 160 | }
|
---|
| 161 |
|
---|
[fbcdeb8] | 162 | int dmamem_unmap(void *virt, size_t size)
|
---|
[fd6bd6d] | 163 | {
|
---|
[fbcdeb8] | 164 | return __SYSCALL3(SYS_DMAMEM_UNMAP, (sysarg_t) virt, (sysarg_t) size, 0);
|
---|
[fd6bd6d] | 165 | }
|
---|
| 166 |
|
---|
[c6ae4c2] | 167 | int dmamem_unmap_anonymous(void *virt)
|
---|
[fd6bd6d] | 168 | {
|
---|
[c6ae4c2] | 169 | return __SYSCALL3(SYS_DMAMEM_UNMAP, (sysarg_t) virt, 0,
|
---|
| 170 | DMAMEM_FLAGS_ANONYMOUS);
|
---|
[fd6bd6d] | 171 | }
|
---|
| 172 |
|
---|
[9426c1a3] | 173 | /** Enable I/O space range to task.
|
---|
| 174 | *
|
---|
| 175 | * Caller of this function must have the IO_MEM_MANAGER capability.
|
---|
| 176 | *
|
---|
[fd6bd6d] | 177 | * @param id Task ID.
|
---|
| 178 | * @param ioaddr Starting address of the I/O range.
|
---|
| 179 | * @param size Size of the range.
|
---|
| 180 | *
|
---|
| 181 | * @return EOK on success
|
---|
| 182 | * @return EPERM if the caller lacks the CAP_IO_MANAGER capability
|
---|
| 183 | * @return ENOENT if there is no task with specified ID
|
---|
| 184 | * @return ENOMEM if there was some problem in allocating memory.
|
---|
[9426c1a3] | 185 | *
|
---|
| 186 | */
|
---|
[edb0a33] | 187 | static int iospace_enable(task_id_t id, void *ioaddr, size_t size)
|
---|
[9426c1a3] | 188 | {
|
---|
[edb0a33] | 189 | const ddi_ioarg_t arg = {
|
---|
| 190 | .task_id = id,
|
---|
| 191 | .ioaddr = ioaddr,
|
---|
| 192 | .size = size
|
---|
| 193 | };
|
---|
[fd6bd6d] | 194 |
|
---|
[5140e3e] | 195 | return __SYSCALL1(SYS_IOSPACE_ENABLE, (sysarg_t) &arg);
|
---|
[9426c1a3] | 196 | }
|
---|
[3d77747] | 197 |
|
---|
[8cd680c] | 198 | /** Disable I/O space range to task.
|
---|
| 199 | *
|
---|
| 200 | * Caller of this function must have the IO_MEM_MANAGER capability.
|
---|
| 201 | *
|
---|
| 202 | * @param id Task ID.
|
---|
| 203 | * @param ioaddr Starting address of the I/O range.
|
---|
| 204 | * @param size Size of the range.
|
---|
| 205 | *
|
---|
| 206 | * @return EOK on success
|
---|
| 207 | * @return EPERM if the caller lacks the CAP_IO_MANAGER capability
|
---|
| 208 | * @return ENOENT if there is no task with specified ID
|
---|
| 209 | *
|
---|
| 210 | */
|
---|
| 211 | static int iospace_disable(task_id_t id, void *ioaddr, size_t size)
|
---|
| 212 | {
|
---|
| 213 | const ddi_ioarg_t arg = {
|
---|
| 214 | .task_id = id,
|
---|
| 215 | .ioaddr = ioaddr,
|
---|
| 216 | .size = size
|
---|
| 217 | };
|
---|
| 218 |
|
---|
| 219 | return __SYSCALL1(SYS_IOSPACE_DISABLE, (sysarg_t) &arg);
|
---|
| 220 | }
|
---|
| 221 |
|
---|
[8049b79] | 222 | /** Enable PIO for specified address range.
|
---|
| 223 | *
|
---|
| 224 | * @param range I/O range to be enable.
|
---|
| 225 | * @param virt Virtual address for application's PIO operations.
|
---|
| 226 | */
|
---|
| 227 | int pio_enable_range(addr_range_t *range, void **virt)
|
---|
| 228 | {
|
---|
| 229 | return pio_enable(RNGABSPTR(*range), RNGSZ(*range), virt);
|
---|
| 230 | }
|
---|
| 231 |
|
---|
| 232 | /** Enable PIO for specified HW resource wrt. to the PIO window.
|
---|
[eeb5cc2] | 233 | *
|
---|
| 234 | * @param win PIO window. May be NULL if the resources are known to be
|
---|
| 235 | * absolute.
|
---|
| 236 | * @param res Resources specifying the I/O range wrt. to the PIO window.
|
---|
| 237 | * @param virt Virtual address for application's PIO operations.
|
---|
| 238 | *
|
---|
| 239 | * @return EOK on success.
|
---|
| 240 | * @return Negative error code on failure.
|
---|
| 241 | *
|
---|
| 242 | */
|
---|
| 243 | int pio_enable_resource(pio_window_t *win, hw_resource_t *res, void **virt)
|
---|
| 244 | {
|
---|
| 245 | uintptr_t addr;
|
---|
| 246 | size_t size;
|
---|
| 247 |
|
---|
| 248 | switch (res->type) {
|
---|
| 249 | case IO_RANGE:
|
---|
| 250 | addr = res->res.io_range.address;
|
---|
| 251 | if (res->res.io_range.relative) {
|
---|
| 252 | if (!win)
|
---|
| 253 | return EINVAL;
|
---|
| 254 | addr += win->io.base;
|
---|
| 255 | }
|
---|
| 256 | size = res->res.io_range.size;
|
---|
| 257 | break;
|
---|
| 258 | case MEM_RANGE:
|
---|
| 259 | addr = res->res.mem_range.address;
|
---|
| 260 | if (res->res.mem_range.relative) {
|
---|
| 261 | if (!win)
|
---|
| 262 | return EINVAL;
|
---|
| 263 | addr += win->mem.base;
|
---|
| 264 | }
|
---|
| 265 | size = res->res.mem_range.size;
|
---|
| 266 | break;
|
---|
| 267 | default:
|
---|
| 268 | return EINVAL;
|
---|
| 269 | }
|
---|
| 270 |
|
---|
| 271 | return pio_enable((void *) addr, size, virt);
|
---|
| 272 | }
|
---|
| 273 |
|
---|
[0d5a50c] | 274 | /** Enable PIO for specified I/O range.
|
---|
| 275 | *
|
---|
[fd6bd6d] | 276 | * @param pio_addr I/O start address.
|
---|
| 277 | * @param size Size of the I/O region.
|
---|
[fbcdeb8] | 278 | * @param virt Virtual address for application's
|
---|
[6659037] | 279 | * PIO operations. Can be NULL for PMIO.
|
---|
[fd6bd6d] | 280 | *
|
---|
[fbcdeb8] | 281 | * @return EOK on success.
|
---|
| 282 | * @return Negative error code on failure.
|
---|
[0d5a50c] | 283 | *
|
---|
| 284 | */
|
---|
[fbcdeb8] | 285 | int pio_enable(void *pio_addr, size_t size, void **virt)
|
---|
[0d5a50c] | 286 | {
|
---|
| 287 | #ifdef IO_SPACE_BOUNDARY
|
---|
| 288 | if (pio_addr < IO_SPACE_BOUNDARY) {
|
---|
[6659037] | 289 | if (virt)
|
---|
| 290 | *virt = pio_addr;
|
---|
[0d5a50c] | 291 | return iospace_enable(task_get_id(), pio_addr, size);
|
---|
| 292 | }
|
---|
[aac1c417] | 293 | #else
|
---|
| 294 | (void) iospace_enable;
|
---|
[0d5a50c] | 295 | #endif
|
---|
[6659037] | 296 | if (!virt)
|
---|
| 297 | return EINVAL;
|
---|
[8442d10] | 298 |
|
---|
| 299 | uintptr_t phys_frame =
|
---|
| 300 | ALIGN_DOWN((uintptr_t) pio_addr, PAGE_SIZE);
|
---|
| 301 | size_t offset = (uintptr_t) pio_addr - phys_frame;
|
---|
[fbcdeb8] | 302 | size_t pages = SIZE2PAGES(offset + size);
|
---|
| 303 |
|
---|
[bf9cb2f] | 304 | void *virt_page = AS_AREA_ANY;
|
---|
[fbcdeb8] | 305 | int rc = physmem_map(phys_frame, pages,
|
---|
| 306 | AS_AREA_READ | AS_AREA_WRITE, &virt_page);
|
---|
| 307 | if (rc != EOK)
|
---|
| 308 | return rc;
|
---|
| 309 |
|
---|
| 310 | *virt = virt_page + offset;
|
---|
| 311 | return EOK;
|
---|
[0d5a50c] | 312 | }
|
---|
| 313 |
|
---|
[8cd680c] | 314 | /** Disable PIO for specified I/O range.
|
---|
| 315 | *
|
---|
| 316 | * @param virt I/O start address.
|
---|
| 317 | * @param size Size of the I/O region.
|
---|
| 318 | *
|
---|
| 319 | * @return EOK on success.
|
---|
| 320 | * @return Negative error code on failure.
|
---|
| 321 | *
|
---|
| 322 | */
|
---|
| 323 | int pio_disable(void *virt, size_t size)
|
---|
| 324 | {
|
---|
| 325 | #ifdef IO_SPACE_BOUNDARY
|
---|
| 326 | if (virt < IO_SPACE_BOUNDARY)
|
---|
| 327 | return iospace_disable(task_get_id(), virt, size);
|
---|
| 328 | #else
|
---|
| 329 | (void) iospace_disable;
|
---|
| 330 | #endif
|
---|
| 331 | return physmem_unmap(virt);
|
---|
| 332 | }
|
---|
| 333 |
|
---|
[3218648] | 334 | void pio_write_8(ioport8_t *reg, uint8_t val)
|
---|
| 335 | {
|
---|
[acc0efb] | 336 | pio_trace_log(reg, val, true);
|
---|
[3218648] | 337 | arch_pio_write_8(reg, val);
|
---|
| 338 | }
|
---|
| 339 |
|
---|
| 340 | void pio_write_16(ioport16_t *reg, uint16_t val)
|
---|
| 341 | {
|
---|
[acc0efb] | 342 | pio_trace_log(reg, val, true);
|
---|
[3218648] | 343 | arch_pio_write_16(reg, val);
|
---|
| 344 | }
|
---|
| 345 |
|
---|
| 346 | void pio_write_32(ioport32_t *reg, uint32_t val)
|
---|
| 347 | {
|
---|
[acc0efb] | 348 | pio_trace_log(reg, val, true);
|
---|
[3218648] | 349 | arch_pio_write_32(reg, val);
|
---|
| 350 | }
|
---|
| 351 |
|
---|
[b5c2f56] | 352 | uint8_t pio_read_8(const ioport8_t *reg)
|
---|
[3218648] | 353 | {
|
---|
[acc0efb] | 354 | const uint8_t val = arch_pio_read_8(reg);
|
---|
| 355 | pio_trace_log(reg, val, false);
|
---|
| 356 | return val;
|
---|
[3218648] | 357 | }
|
---|
| 358 |
|
---|
[b5c2f56] | 359 | uint16_t pio_read_16(const ioport16_t *reg)
|
---|
[3218648] | 360 | {
|
---|
[acc0efb] | 361 | const uint16_t val = arch_pio_read_16(reg);
|
---|
| 362 | pio_trace_log(reg, val, false);
|
---|
| 363 | return val;
|
---|
[3218648] | 364 | }
|
---|
| 365 |
|
---|
[b5c2f56] | 366 | uint32_t pio_read_32(const ioport32_t *reg)
|
---|
[3218648] | 367 | {
|
---|
[acc0efb] | 368 | const uint32_t val = arch_pio_read_32(reg);
|
---|
| 369 | pio_trace_log(reg, val, false);
|
---|
| 370 | return val;
|
---|
[3218648] | 371 | }
|
---|
| 372 |
|
---|
[a46da63] | 373 | /** @}
|
---|
[b2951e2] | 374 | */
|
---|