[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 <stdio.h>
|
---|
[c6ae4c2] | 37 | #include <errno.h>
|
---|
[c0699467] | 38 | #include <abi/ddi/arg.h>
|
---|
[a1e17fc] | 39 | #include <ddi.h>
|
---|
[1ae8f2b] | 40 | #include <libarch/ddi.h>
|
---|
[8049b79] | 41 | #include <device/hw_res.h>
|
---|
| 42 | #include <device/hw_res_parsed.h>
|
---|
| 43 | #include <device/pio_window.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 |
|
---|
[28f8f170] | 51 | /** Map a piece of physical memory to task.
|
---|
[a1e17fc] | 52 | *
|
---|
[9f9b6f0e] | 53 | * Caller of this function must have the PERM_MEM_MANAGER permission.
|
---|
[a1e17fc] | 54 | *
|
---|
[c6ae4c2] | 55 | * @param phys Physical address of the starting frame.
|
---|
[28f8f170] | 56 | * @param pages Number of pages to map.
|
---|
| 57 | * @param flags Flags for the new address space area.
|
---|
[fbcdeb8] | 58 | * @param virt Virtual address of the starting page.
|
---|
[bf9cb2f] | 59 | * If set to AS_AREA_ANY ((void *) -1), a suitable value
|
---|
| 60 | * is found by the kernel, otherwise the kernel tries to
|
---|
| 61 | * obey the desired value.
|
---|
[28f8f170] | 62 | *
|
---|
[bf9cb2f] | 63 | * @return EOK on success.
|
---|
[9f9b6f0e] | 64 | * @return EPERM if the caller lacks the PERM_MEM_MANAGER permission.
|
---|
[28f8f170] | 65 | * @return ENOMEM if there was some problem in creating
|
---|
| 66 | * the address space area.
|
---|
[a1e17fc] | 67 | *
|
---|
| 68 | */
|
---|
[b7fd2a0] | 69 | errno_t physmem_map(uintptr_t phys, size_t pages, unsigned int flags, void **virt)
|
---|
[a1e17fc] | 70 | {
|
---|
[b7fd2a0] | 71 | return (errno_t) __SYSCALL5(SYS_PHYSMEM_MAP, (sysarg_t) phys,
|
---|
[2eadda9] | 72 | pages, flags, (sysarg_t) virt, (sysarg_t) __progsymbols.end);
|
---|
[a1e17fc] | 73 | }
|
---|
[9426c1a3] | 74 |
|
---|
[8cd680c] | 75 | /** Unmap a piece of physical memory to task.
|
---|
| 76 | *
|
---|
[9f9b6f0e] | 77 | * Caller of this function must have the PERM_MEM_MANAGER permission.
|
---|
[8cd680c] | 78 | *
|
---|
| 79 | * @param virt Virtual address from the phys-mapped region.
|
---|
| 80 | *
|
---|
| 81 | * @return EOK on success.
|
---|
[9f9b6f0e] | 82 | * @return EPERM if the caller lacks the PERM_MEM_MANAGER permission.
|
---|
[8cd680c] | 83 | *
|
---|
| 84 | */
|
---|
[b7fd2a0] | 85 | errno_t physmem_unmap(void *virt)
|
---|
[8cd680c] | 86 | {
|
---|
[b7fd2a0] | 87 | return (errno_t) __SYSCALL1(SYS_PHYSMEM_UNMAP, (sysarg_t) virt);
|
---|
[8cd680c] | 88 | }
|
---|
| 89 |
|
---|
[bf9cb2f] | 90 | /** Lock a piece physical memory for DMA transfers.
|
---|
| 91 | *
|
---|
| 92 | * The mapping of the specified virtual memory address
|
---|
| 93 | * to physical memory address is locked in order to
|
---|
| 94 | * make it safe for DMA transferts.
|
---|
| 95 | *
|
---|
[9f9b6f0e] | 96 | * Caller of this function must have the PERM_MEM_MANAGER permission.
|
---|
[bf9cb2f] | 97 | *
|
---|
| 98 | * @param virt Virtual address of the memory to be locked.
|
---|
| 99 | * @param size Number of bytes to lock.
|
---|
| 100 | * @param map_flags Desired virtual memory area flags.
|
---|
| 101 | * @param flags Flags for the physical memory address.
|
---|
| 102 | * @param phys Locked physical memory address.
|
---|
| 103 | *
|
---|
| 104 | * @return EOK on success.
|
---|
[9f9b6f0e] | 105 | * @return EPERM if the caller lacks the PERM_MEM_MANAGER permission.
|
---|
[bf9cb2f] | 106 | * @return ENOMEM if there was some problem in creating
|
---|
| 107 | * the address space area.
|
---|
| 108 | *
|
---|
| 109 | */
|
---|
[b7fd2a0] | 110 | errno_t dmamem_map(void *virt, size_t size, unsigned int map_flags,
|
---|
[8442d10] | 111 | unsigned int flags, uintptr_t *phys)
|
---|
[fd6bd6d] | 112 | {
|
---|
[b7fd2a0] | 113 | return (errno_t) __SYSCALL6(SYS_DMAMEM_MAP, (sysarg_t) size,
|
---|
[fbcdeb8] | 114 | (sysarg_t) map_flags, (sysarg_t) flags & ~DMAMEM_FLAGS_ANONYMOUS,
|
---|
| 115 | (sysarg_t) phys, (sysarg_t) virt, 0);
|
---|
[fd6bd6d] | 116 | }
|
---|
| 117 |
|
---|
[bf9cb2f] | 118 | /** Map a piece of physical memory suitable for DMA transfers.
|
---|
| 119 | *
|
---|
[9f9b6f0e] | 120 | * Caller of this function must have the PERM_MEM_MANAGER permission.
|
---|
[bf9cb2f] | 121 | *
|
---|
| 122 | * @param size Number of bytes to map.
|
---|
| 123 | * @param constraint Bit mask defining the contraint on the physical
|
---|
| 124 | * address to be mapped.
|
---|
| 125 | * @param map_flags Desired virtual memory area flags.
|
---|
| 126 | * @param flags Flags for the physical memory address.
|
---|
| 127 | * @param virt Virtual address of the starting page.
|
---|
| 128 | * If set to AS_AREA_ANY ((void *) -1), a suitable value
|
---|
| 129 | * is found by the kernel, otherwise the kernel tries to
|
---|
| 130 | * obey the desired value.
|
---|
| 131 | *
|
---|
| 132 | * @return EOK on success.
|
---|
[9f9b6f0e] | 133 | * @return EPERM if the caller lacks the PERM_MEM_MANAGER permission.
|
---|
[bf9cb2f] | 134 | * @return ENOMEM if there was some problem in creating
|
---|
| 135 | * the address space area.
|
---|
| 136 | *
|
---|
| 137 | */
|
---|
[b7fd2a0] | 138 | errno_t dmamem_map_anonymous(size_t size, uintptr_t constraint,
|
---|
[8442d10] | 139 | unsigned int map_flags, unsigned int flags, uintptr_t *phys, void **virt)
|
---|
[fd6bd6d] | 140 | {
|
---|
[8442d10] | 141 | *phys = constraint;
|
---|
[a35b458] | 142 |
|
---|
[b7fd2a0] | 143 | return (errno_t) __SYSCALL6(SYS_DMAMEM_MAP, (sysarg_t) size,
|
---|
[fbcdeb8] | 144 | (sysarg_t) map_flags, (sysarg_t) flags | DMAMEM_FLAGS_ANONYMOUS,
|
---|
[2eadda9] | 145 | (sysarg_t) phys, (sysarg_t) virt, (sysarg_t) __progsymbols.end);
|
---|
[fd6bd6d] | 146 | }
|
---|
| 147 |
|
---|
[b7fd2a0] | 148 | errno_t dmamem_unmap(void *virt, size_t size)
|
---|
[fd6bd6d] | 149 | {
|
---|
[b7fd2a0] | 150 | return (errno_t) __SYSCALL3(SYS_DMAMEM_UNMAP, (sysarg_t) virt, (sysarg_t) size, 0);
|
---|
[fd6bd6d] | 151 | }
|
---|
| 152 |
|
---|
[b7fd2a0] | 153 | errno_t dmamem_unmap_anonymous(void *virt)
|
---|
[fd6bd6d] | 154 | {
|
---|
[b7fd2a0] | 155 | return (errno_t) __SYSCALL3(SYS_DMAMEM_UNMAP, (sysarg_t) virt, 0,
|
---|
[c6ae4c2] | 156 | DMAMEM_FLAGS_ANONYMOUS);
|
---|
[fd6bd6d] | 157 | }
|
---|
| 158 |
|
---|
[9426c1a3] | 159 | /** Enable I/O space range to task.
|
---|
| 160 | *
|
---|
[9f9b6f0e] | 161 | * Caller of this function must have the PERM_IO_MANAGER permission.
|
---|
[9426c1a3] | 162 | *
|
---|
[fd6bd6d] | 163 | * @param id Task ID.
|
---|
| 164 | * @param ioaddr Starting address of the I/O range.
|
---|
| 165 | * @param size Size of the range.
|
---|
| 166 | *
|
---|
| 167 | * @return EOK on success
|
---|
[9f9b6f0e] | 168 | * @return EPERM if the caller lacks the PERM_IO_MANAGER permission
|
---|
[fd6bd6d] | 169 | * @return ENOENT if there is no task with specified ID
|
---|
| 170 | * @return ENOMEM if there was some problem in allocating memory.
|
---|
[9426c1a3] | 171 | *
|
---|
| 172 | */
|
---|
[b7fd2a0] | 173 | static errno_t iospace_enable(task_id_t id, void *ioaddr, size_t size)
|
---|
[9426c1a3] | 174 | {
|
---|
[edb0a33] | 175 | const ddi_ioarg_t arg = {
|
---|
| 176 | .task_id = id,
|
---|
| 177 | .ioaddr = ioaddr,
|
---|
| 178 | .size = size
|
---|
| 179 | };
|
---|
[a35b458] | 180 |
|
---|
[b7fd2a0] | 181 | return (errno_t) __SYSCALL1(SYS_IOSPACE_ENABLE, (sysarg_t) &arg);
|
---|
[9426c1a3] | 182 | }
|
---|
[3d77747] | 183 |
|
---|
[8cd680c] | 184 | /** Disable I/O space range to task.
|
---|
| 185 | *
|
---|
[9f9b6f0e] | 186 | * Caller of this function must have the PERM_IO_MANAGER permission.
|
---|
[8cd680c] | 187 | *
|
---|
| 188 | * @param id Task ID.
|
---|
| 189 | * @param ioaddr Starting address of the I/O range.
|
---|
| 190 | * @param size Size of the range.
|
---|
| 191 | *
|
---|
| 192 | * @return EOK on success
|
---|
[9f9b6f0e] | 193 | * @return EPERM if the caller lacks the PERM_IO_MANAGER permission
|
---|
[8cd680c] | 194 | * @return ENOENT if there is no task with specified ID
|
---|
| 195 | *
|
---|
| 196 | */
|
---|
[b7fd2a0] | 197 | static errno_t iospace_disable(task_id_t id, void *ioaddr, size_t size)
|
---|
[8cd680c] | 198 | {
|
---|
| 199 | const ddi_ioarg_t arg = {
|
---|
| 200 | .task_id = id,
|
---|
| 201 | .ioaddr = ioaddr,
|
---|
| 202 | .size = size
|
---|
| 203 | };
|
---|
[a35b458] | 204 |
|
---|
[b7fd2a0] | 205 | return (errno_t) __SYSCALL1(SYS_IOSPACE_DISABLE, (sysarg_t) &arg);
|
---|
[8cd680c] | 206 | }
|
---|
| 207 |
|
---|
[8049b79] | 208 | /** Enable PIO for specified address range.
|
---|
| 209 | *
|
---|
| 210 | * @param range I/O range to be enable.
|
---|
[1b20da0] | 211 | * @param virt Virtual address for application's PIO operations.
|
---|
[8049b79] | 212 | */
|
---|
[b7fd2a0] | 213 | errno_t pio_enable_range(addr_range_t *range, void **virt)
|
---|
[8049b79] | 214 | {
|
---|
| 215 | return pio_enable(RNGABSPTR(*range), RNGSZ(*range), virt);
|
---|
| 216 | }
|
---|
| 217 |
|
---|
| 218 | /** Enable PIO for specified HW resource wrt. to the PIO window.
|
---|
[eeb5cc2] | 219 | *
|
---|
[9e9ced0] | 220 | * @param win PIO window. May be NULL if the resources are known to be
|
---|
| 221 | * absolute.
|
---|
| 222 | * @param res Resources specifying the I/O range wrt. to the PIO window.
|
---|
| 223 | * @param[out] virt Virtual address for application's PIO operations.
|
---|
[848e880f] | 224 | * @param[out] phys If non-NULL, physical address of the resource
|
---|
[9e9ced0] | 225 | * @param[out] size If non-NULL, size of the enabled resource.
|
---|
[eeb5cc2] | 226 | *
|
---|
| 227 | * @return EOK on success.
|
---|
[cde999a] | 228 | * @return An error code on failure.
|
---|
[eeb5cc2] | 229 | *
|
---|
| 230 | */
|
---|
[9e9ced0] | 231 | errno_t pio_enable_resource(pio_window_t *win, hw_resource_t *res, void **virt,
|
---|
[848e880f] | 232 | uintptr_t *phys, size_t *size)
|
---|
[eeb5cc2] | 233 | {
|
---|
| 234 | uintptr_t addr;
|
---|
[9e9ced0] | 235 | size_t sz;
|
---|
[eeb5cc2] | 236 |
|
---|
| 237 | switch (res->type) {
|
---|
| 238 | case IO_RANGE:
|
---|
| 239 | addr = res->res.io_range.address;
|
---|
| 240 | if (res->res.io_range.relative) {
|
---|
| 241 | if (!win)
|
---|
| 242 | return EINVAL;
|
---|
| 243 | addr += win->io.base;
|
---|
| 244 | }
|
---|
[9e9ced0] | 245 | sz = res->res.io_range.size;
|
---|
[eeb5cc2] | 246 | break;
|
---|
| 247 | case MEM_RANGE:
|
---|
| 248 | addr = res->res.mem_range.address;
|
---|
| 249 | if (res->res.mem_range.relative) {
|
---|
| 250 | if (!win)
|
---|
| 251 | return EINVAL;
|
---|
| 252 | addr += win->mem.base;
|
---|
| 253 | }
|
---|
[9e9ced0] | 254 | sz = res->res.mem_range.size;
|
---|
[eeb5cc2] | 255 | break;
|
---|
| 256 | default:
|
---|
| 257 | return EINVAL;
|
---|
| 258 | }
|
---|
| 259 |
|
---|
[848e880f] | 260 | if (phys)
|
---|
| 261 | *phys = addr;
|
---|
[9e9ced0] | 262 | if (size)
|
---|
| 263 | *size = sz;
|
---|
| 264 |
|
---|
| 265 | return pio_enable((void *) addr, sz, virt);
|
---|
[eeb5cc2] | 266 | }
|
---|
| 267 |
|
---|
[0d5a50c] | 268 | /** Enable PIO for specified I/O range.
|
---|
| 269 | *
|
---|
[fd6bd6d] | 270 | * @param pio_addr I/O start address.
|
---|
| 271 | * @param size Size of the I/O region.
|
---|
[fbcdeb8] | 272 | * @param virt Virtual address for application's
|
---|
[6659037] | 273 | * PIO operations. Can be NULL for PMIO.
|
---|
[fd6bd6d] | 274 | *
|
---|
[fbcdeb8] | 275 | * @return EOK on success.
|
---|
[cde999a] | 276 | * @return An error code on failure.
|
---|
[0d5a50c] | 277 | *
|
---|
| 278 | */
|
---|
[b7fd2a0] | 279 | errno_t pio_enable(void *pio_addr, size_t size, void **virt)
|
---|
[0d5a50c] | 280 | {
|
---|
| 281 | #ifdef IO_SPACE_BOUNDARY
|
---|
| 282 | if (pio_addr < IO_SPACE_BOUNDARY) {
|
---|
[6659037] | 283 | if (virt)
|
---|
| 284 | *virt = pio_addr;
|
---|
[0d5a50c] | 285 | return iospace_enable(task_get_id(), pio_addr, size);
|
---|
| 286 | }
|
---|
[aac1c417] | 287 | #else
|
---|
| 288 | (void) iospace_enable;
|
---|
[0d5a50c] | 289 | #endif
|
---|
[6659037] | 290 | if (!virt)
|
---|
| 291 | return EINVAL;
|
---|
[a35b458] | 292 |
|
---|
[8442d10] | 293 | uintptr_t phys_frame =
|
---|
| 294 | ALIGN_DOWN((uintptr_t) pio_addr, PAGE_SIZE);
|
---|
| 295 | size_t offset = (uintptr_t) pio_addr - phys_frame;
|
---|
[fbcdeb8] | 296 | size_t pages = SIZE2PAGES(offset + size);
|
---|
[a35b458] | 297 |
|
---|
[bf9cb2f] | 298 | void *virt_page = AS_AREA_ANY;
|
---|
[b7fd2a0] | 299 | errno_t rc = physmem_map(phys_frame, pages,
|
---|
[fbcdeb8] | 300 | AS_AREA_READ | AS_AREA_WRITE, &virt_page);
|
---|
| 301 | if (rc != EOK)
|
---|
| 302 | return rc;
|
---|
[a35b458] | 303 |
|
---|
[fbcdeb8] | 304 | *virt = virt_page + offset;
|
---|
| 305 | return EOK;
|
---|
[0d5a50c] | 306 | }
|
---|
| 307 |
|
---|
[8cd680c] | 308 | /** Disable PIO for specified I/O range.
|
---|
| 309 | *
|
---|
| 310 | * @param virt I/O start address.
|
---|
| 311 | * @param size Size of the I/O region.
|
---|
| 312 | *
|
---|
| 313 | * @return EOK on success.
|
---|
[cde999a] | 314 | * @return An error code on failure.
|
---|
[8cd680c] | 315 | *
|
---|
| 316 | */
|
---|
[b7fd2a0] | 317 | errno_t pio_disable(void *virt, size_t size)
|
---|
[8cd680c] | 318 | {
|
---|
| 319 | #ifdef IO_SPACE_BOUNDARY
|
---|
| 320 | if (virt < IO_SPACE_BOUNDARY)
|
---|
| 321 | return iospace_disable(task_get_id(), virt, size);
|
---|
| 322 | #else
|
---|
| 323 | (void) iospace_disable;
|
---|
| 324 | #endif
|
---|
| 325 | return physmem_unmap(virt);
|
---|
| 326 | }
|
---|
| 327 |
|
---|
[3218648] | 328 | void pio_write_8(ioport8_t *reg, uint8_t val)
|
---|
| 329 | {
|
---|
[acc0efb] | 330 | pio_trace_log(reg, val, true);
|
---|
[3218648] | 331 | arch_pio_write_8(reg, val);
|
---|
| 332 | }
|
---|
| 333 |
|
---|
| 334 | void pio_write_16(ioport16_t *reg, uint16_t val)
|
---|
| 335 | {
|
---|
[acc0efb] | 336 | pio_trace_log(reg, val, true);
|
---|
[3218648] | 337 | arch_pio_write_16(reg, val);
|
---|
| 338 | }
|
---|
| 339 |
|
---|
| 340 | void pio_write_32(ioport32_t *reg, uint32_t val)
|
---|
| 341 | {
|
---|
[acc0efb] | 342 | pio_trace_log(reg, val, true);
|
---|
[3218648] | 343 | arch_pio_write_32(reg, val);
|
---|
| 344 | }
|
---|
| 345 |
|
---|
[aa537a5a] | 346 | void pio_write_64(ioport64_t *reg, uint64_t val)
|
---|
| 347 | {
|
---|
| 348 | pio_trace_log(reg, val, true);
|
---|
| 349 | arch_pio_write_64(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 |
|
---|
[aa537a5a] | 373 | uint64_t pio_read_64(const ioport64_t *reg)
|
---|
| 374 | {
|
---|
| 375 | const uint64_t val = arch_pio_read_64(reg);
|
---|
| 376 | pio_trace_log(reg, val, false);
|
---|
| 377 | return val;
|
---|
| 378 | }
|
---|
| 379 |
|
---|
[a46da63] | 380 | /** @}
|
---|
[b2951e2] | 381 | */
|
---|