| 1 | /*
|
|---|
| 2 | * Copyright (c) 2006 Jakub Jermar
|
|---|
| 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.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | /** @addtogroup kernel_generic_ddi
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 |
|
|---|
| 33 | /**
|
|---|
| 34 | * @file
|
|---|
| 35 | * @brief Device Driver Interface functions.
|
|---|
| 36 | *
|
|---|
| 37 | * This file contains functions that comprise the Device Driver Interface.
|
|---|
| 38 | * These are the functions for mapping physical memory and enabling I/O
|
|---|
| 39 | * space to tasks.
|
|---|
| 40 | */
|
|---|
| 41 |
|
|---|
| 42 | #include <assert.h>
|
|---|
| 43 | #include <ddi/ddi.h>
|
|---|
| 44 | #include <proc/task.h>
|
|---|
| 45 | #include <security/perm.h>
|
|---|
| 46 | #include <mm/frame.h>
|
|---|
| 47 | #include <mm/as.h>
|
|---|
| 48 | #include <mm/km.h>
|
|---|
| 49 | #include <mm/page.h>
|
|---|
| 50 | #include <synch/mutex.h>
|
|---|
| 51 | #include <syscall/copy.h>
|
|---|
| 52 | #include <adt/odict.h>
|
|---|
| 53 | #include <arch.h>
|
|---|
| 54 | #include <align.h>
|
|---|
| 55 | #include <errno.h>
|
|---|
| 56 | #include <mem.h>
|
|---|
| 57 | #include <trace.h>
|
|---|
| 58 | #include <bitops.h>
|
|---|
| 59 | #include <arch/asm.h>
|
|---|
| 60 |
|
|---|
| 61 | /** This lock protects the @c pareas ordered dictionary. */
|
|---|
| 62 | static mutex_t pareas_lock;
|
|---|
| 63 |
|
|---|
| 64 | /** Ordered dictionary of enabled physical memory areas by base address. */
|
|---|
| 65 | static odict_t pareas;
|
|---|
| 66 |
|
|---|
| 67 | static void *pareas_getkey(odlink_t *);
|
|---|
| 68 | static int pareas_cmp(void *, void *);
|
|---|
| 69 |
|
|---|
| 70 | /** Initialize DDI.
|
|---|
| 71 | *
|
|---|
| 72 | */
|
|---|
| 73 | void ddi_init(void)
|
|---|
| 74 | {
|
|---|
| 75 | odict_initialize(&pareas, pareas_getkey, pareas_cmp);
|
|---|
| 76 | mutex_initialize(&pareas_lock, MUTEX_PASSIVE);
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | /** Initialize physical area structure.
|
|---|
| 80 | *
|
|---|
| 81 | * This should always be called first on the parea structure before
|
|---|
| 82 | * filling in fields and calling ddi_parea_register.
|
|---|
| 83 | *
|
|---|
| 84 | * @param parea Pointer to physical area structure.
|
|---|
| 85 | *
|
|---|
| 86 | */
|
|---|
| 87 | void ddi_parea_init(parea_t *parea)
|
|---|
| 88 | {
|
|---|
| 89 | memset(parea, 0, sizeof(parea_t));
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | /** Enable piece of physical memory for mapping by physmem_map().
|
|---|
| 93 | *
|
|---|
| 94 | * @param parea Pointer to physical area structure.
|
|---|
| 95 | *
|
|---|
| 96 | */
|
|---|
| 97 | void ddi_parea_register(parea_t *parea)
|
|---|
| 98 | {
|
|---|
| 99 | mutex_lock(&pareas_lock);
|
|---|
| 100 |
|
|---|
| 101 | /*
|
|---|
| 102 | * We don't check for overlaps here as the kernel is pretty sane.
|
|---|
| 103 | */
|
|---|
| 104 | odict_insert(&parea->lpareas, &pareas, NULL);
|
|---|
| 105 |
|
|---|
| 106 | mutex_unlock(&pareas_lock);
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | /** Map piece of physical memory into virtual address space of current task.
|
|---|
| 110 | *
|
|---|
| 111 | * @param phys Physical address of the starting frame.
|
|---|
| 112 | * @param pages Number of pages to map.
|
|---|
| 113 | * @param flags Address space area flags for the mapping.
|
|---|
| 114 | * @param virt Virtual address of the starting page.
|
|---|
| 115 | * @param bound Lowest virtual address bound.
|
|---|
| 116 | *
|
|---|
| 117 | * @return EOK on success.
|
|---|
| 118 | * @return EPERM if the caller lacks permissions to use this syscall.
|
|---|
| 119 | * @return EBADMEM if phys is not page aligned.
|
|---|
| 120 | * @return ENOENT if there is no task matching the specified ID or
|
|---|
| 121 | * the physical address space is not enabled for mapping.
|
|---|
| 122 | * @return ENOMEM if there was a problem in creating address space area.
|
|---|
| 123 | *
|
|---|
| 124 | */
|
|---|
| 125 | _NO_TRACE static errno_t physmem_map(uintptr_t phys, size_t pages,
|
|---|
| 126 | unsigned int flags, uintptr_t *virt, uintptr_t bound)
|
|---|
| 127 | {
|
|---|
| 128 | assert(TASK);
|
|---|
| 129 |
|
|---|
| 130 | if ((phys % FRAME_SIZE) != 0)
|
|---|
| 131 | return EBADMEM;
|
|---|
| 132 |
|
|---|
| 133 | /*
|
|---|
| 134 | * Unprivileged tasks are only allowed to map pareas
|
|---|
| 135 | * which are explicitly marked as such.
|
|---|
| 136 | */
|
|---|
| 137 | bool priv =
|
|---|
| 138 | ((perm_get(TASK) & PERM_MEM_MANAGER) == PERM_MEM_MANAGER);
|
|---|
| 139 |
|
|---|
| 140 | mem_backend_data_t backend_data;
|
|---|
| 141 | backend_data.base = phys;
|
|---|
| 142 | backend_data.frames = pages;
|
|---|
| 143 | backend_data.anonymous = false;
|
|---|
| 144 |
|
|---|
| 145 | /*
|
|---|
| 146 | * Check if the memory region is explicitly enabled
|
|---|
| 147 | * for mapping by any parea structure.
|
|---|
| 148 | */
|
|---|
| 149 |
|
|---|
| 150 | mutex_lock(&pareas_lock);
|
|---|
| 151 | odlink_t *odlink = odict_find_eq(&pareas, &phys, NULL);
|
|---|
| 152 | parea_t *parea = odlink != NULL ?
|
|---|
| 153 | odict_get_instance(odlink, parea_t, lpareas) : NULL;
|
|---|
| 154 |
|
|---|
| 155 | if ((parea != NULL) && (parea->frames >= pages)) {
|
|---|
| 156 | if ((!priv) && (!parea->unpriv)) {
|
|---|
| 157 | mutex_unlock(&pareas_lock);
|
|---|
| 158 | return EPERM;
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | goto map;
|
|---|
| 162 | }
|
|---|
| 163 |
|
|---|
| 164 | parea = NULL;
|
|---|
| 165 | mutex_unlock(&pareas_lock);
|
|---|
| 166 |
|
|---|
| 167 | /*
|
|---|
| 168 | * Check if the memory region is part of physical
|
|---|
| 169 | * memory generally enabled for mapping.
|
|---|
| 170 | */
|
|---|
| 171 |
|
|---|
| 172 | irq_spinlock_lock(&zones.lock, true);
|
|---|
| 173 | size_t znum = find_zone(ADDR2PFN(phys), pages, 0);
|
|---|
| 174 |
|
|---|
| 175 | if (znum == (size_t) -1) {
|
|---|
| 176 | /*
|
|---|
| 177 | * Frames not found in any zone
|
|---|
| 178 | * -> assume it is a hardware device and allow mapping
|
|---|
| 179 | * for privileged tasks.
|
|---|
| 180 | */
|
|---|
| 181 | irq_spinlock_unlock(&zones.lock, true);
|
|---|
| 182 |
|
|---|
| 183 | if (!priv)
|
|---|
| 184 | return EPERM;
|
|---|
| 185 |
|
|---|
| 186 | goto map;
|
|---|
| 187 | }
|
|---|
| 188 |
|
|---|
| 189 | if (zones.info[znum].flags & (ZONE_FIRMWARE | ZONE_RESERVED)) {
|
|---|
| 190 | /*
|
|---|
| 191 | * Frames are part of firmware or reserved zone
|
|---|
| 192 | * -> allow mapping for privileged tasks.
|
|---|
| 193 | */
|
|---|
| 194 | irq_spinlock_unlock(&zones.lock, true);
|
|---|
| 195 |
|
|---|
| 196 | if (!priv)
|
|---|
| 197 | return EPERM;
|
|---|
| 198 |
|
|---|
| 199 | goto map;
|
|---|
| 200 | }
|
|---|
| 201 |
|
|---|
| 202 | irq_spinlock_unlock(&zones.lock, true);
|
|---|
| 203 | return ENOENT;
|
|---|
| 204 |
|
|---|
| 205 | map:
|
|---|
| 206 | if (!as_area_create(TASK->as, flags, FRAMES2SIZE(pages),
|
|---|
| 207 | AS_AREA_ATTR_NONE, &phys_backend, &backend_data, virt, bound)) {
|
|---|
| 208 | /*
|
|---|
| 209 | * The address space area was not created.
|
|---|
| 210 | * We report it using ENOMEM.
|
|---|
| 211 | */
|
|---|
| 212 |
|
|---|
| 213 | if (parea != NULL)
|
|---|
| 214 | mutex_unlock(&pareas_lock);
|
|---|
| 215 |
|
|---|
| 216 | return ENOMEM;
|
|---|
| 217 | }
|
|---|
| 218 |
|
|---|
| 219 | /*
|
|---|
| 220 | * Mapping is created on-demand during page fault.
|
|---|
| 221 | */
|
|---|
| 222 |
|
|---|
| 223 | if (parea != NULL) {
|
|---|
| 224 | parea->mapped = true;
|
|---|
| 225 | mutex_unlock(&pareas_lock);
|
|---|
| 226 | }
|
|---|
| 227 |
|
|---|
| 228 | return EOK;
|
|---|
| 229 | }
|
|---|
| 230 |
|
|---|
| 231 | _NO_TRACE static errno_t physmem_unmap(uintptr_t virt)
|
|---|
| 232 | {
|
|---|
| 233 | assert(TASK);
|
|---|
| 234 |
|
|---|
| 235 | return as_area_destroy(TASK->as, virt);
|
|---|
| 236 | }
|
|---|
| 237 |
|
|---|
| 238 | /** Wrapper for SYS_PHYSMEM_MAP syscall.
|
|---|
| 239 | *
|
|---|
| 240 | * @param phys Physical base address to map
|
|---|
| 241 | * @param pages Number of pages
|
|---|
| 242 | * @param flags Flags of newly mapped pages
|
|---|
| 243 | * @param virt_ptr Destination virtual address
|
|---|
| 244 | * @param bound Lowest virtual address bound.
|
|---|
| 245 | *
|
|---|
| 246 | * @return 0 on success, otherwise it returns error code found in errno.h
|
|---|
| 247 | *
|
|---|
| 248 | */
|
|---|
| 249 | sys_errno_t sys_physmem_map(uintptr_t phys, size_t pages, unsigned int flags,
|
|---|
| 250 | uspace_ptr_uintptr_t virt_ptr, uintptr_t bound)
|
|---|
| 251 | {
|
|---|
| 252 | uintptr_t virt;
|
|---|
| 253 | errno_t rc = copy_from_uspace(&virt, virt_ptr, sizeof(virt));
|
|---|
| 254 | if (rc != EOK)
|
|---|
| 255 | return rc;
|
|---|
| 256 |
|
|---|
| 257 | rc = physmem_map(ALIGN_DOWN(phys, FRAME_SIZE), pages, flags, &virt,
|
|---|
| 258 | bound);
|
|---|
| 259 | if (rc != EOK)
|
|---|
| 260 | return rc;
|
|---|
| 261 |
|
|---|
| 262 | rc = copy_to_uspace(virt_ptr, &virt, sizeof(virt));
|
|---|
| 263 | if (rc != EOK) {
|
|---|
| 264 | physmem_unmap(virt);
|
|---|
| 265 | return rc;
|
|---|
| 266 | }
|
|---|
| 267 |
|
|---|
| 268 | return EOK;
|
|---|
| 269 | }
|
|---|
| 270 |
|
|---|
| 271 | sys_errno_t sys_physmem_unmap(uintptr_t virt)
|
|---|
| 272 | {
|
|---|
| 273 | return physmem_unmap(virt);
|
|---|
| 274 | }
|
|---|
| 275 |
|
|---|
| 276 | /** Get key function for the @c pareas ordered dictionary.
|
|---|
| 277 | *
|
|---|
| 278 | * @param odlink Link
|
|---|
| 279 | * @return Pointer to base address cast as 'void *'
|
|---|
| 280 | */
|
|---|
| 281 | static void *pareas_getkey(odlink_t *odlink)
|
|---|
| 282 | {
|
|---|
| 283 | parea_t *parea = odict_get_instance(odlink, parea_t, lpareas);
|
|---|
| 284 | return (void *) &parea->pbase;
|
|---|
| 285 | }
|
|---|
| 286 |
|
|---|
| 287 | /** Key comparison function for the @c pareas ordered dictionary.
|
|---|
| 288 | *
|
|---|
| 289 | * @param a Pointer to parea A base
|
|---|
| 290 | * @param b Pointer to parea B base
|
|---|
| 291 | * @return -1, 0, 1 iff base of A is less than, equal to, greater than B
|
|---|
| 292 | */
|
|---|
| 293 | static int pareas_cmp(void *a, void *b)
|
|---|
| 294 | {
|
|---|
| 295 | uintptr_t pa = *(uintptr_t *)a;
|
|---|
| 296 | uintptr_t pb = *(uintptr_t *)b;
|
|---|
| 297 |
|
|---|
| 298 | if (pa < pb)
|
|---|
| 299 | return -1;
|
|---|
| 300 | else if (pa == pb)
|
|---|
| 301 | return 0;
|
|---|
| 302 | else
|
|---|
| 303 | return +1;
|
|---|
| 304 | }
|
|---|
| 305 |
|
|---|
| 306 | /** Enable range of I/O space for task.
|
|---|
| 307 | *
|
|---|
| 308 | * @param id Task ID of the destination task.
|
|---|
| 309 | * @param ioaddr Starting I/O address.
|
|---|
| 310 | * @param size Size of the enabled I/O space.
|
|---|
| 311 | *
|
|---|
| 312 | * @return 0 on success, EPERM if the caller lacks permissions to use this
|
|---|
| 313 | * syscall, ENOENT if there is no task matching the specified ID.
|
|---|
| 314 | *
|
|---|
| 315 | */
|
|---|
| 316 | _NO_TRACE static errno_t iospace_enable(task_id_t id, uintptr_t ioaddr, size_t size)
|
|---|
| 317 | {
|
|---|
| 318 | /*
|
|---|
| 319 | * Make sure the caller is authorised to make this syscall.
|
|---|
| 320 | */
|
|---|
| 321 | perm_t perms = perm_get(TASK);
|
|---|
| 322 | if (!(perms & PERM_IO_MANAGER))
|
|---|
| 323 | return EPERM;
|
|---|
| 324 |
|
|---|
| 325 | irq_spinlock_lock(&tasks_lock, true);
|
|---|
| 326 |
|
|---|
| 327 | task_t *task = task_find_by_id(id);
|
|---|
| 328 |
|
|---|
| 329 | if ((!task) || (!container_check(CONTAINER, task->container))) {
|
|---|
| 330 | /*
|
|---|
| 331 | * There is no task with the specified ID
|
|---|
| 332 | * or the task belongs to a different security
|
|---|
| 333 | * context.
|
|---|
| 334 | */
|
|---|
| 335 | irq_spinlock_unlock(&tasks_lock, true);
|
|---|
| 336 | return ENOENT;
|
|---|
| 337 | }
|
|---|
| 338 |
|
|---|
| 339 | /* Lock the task and release the lock protecting tasks dictionary. */
|
|---|
| 340 | irq_spinlock_exchange(&tasks_lock, &task->lock);
|
|---|
| 341 | errno_t rc = ddi_iospace_enable_arch(task, ioaddr, size);
|
|---|
| 342 | irq_spinlock_unlock(&task->lock, true);
|
|---|
| 343 |
|
|---|
| 344 | return rc;
|
|---|
| 345 | }
|
|---|
| 346 |
|
|---|
| 347 | /** Disable range of I/O space for task.
|
|---|
| 348 | *
|
|---|
| 349 | * @param id Task ID of the destination task.
|
|---|
| 350 | * @param ioaddr Starting I/O address.
|
|---|
| 351 | * @param size Size of the enabled I/O space.
|
|---|
| 352 | *
|
|---|
| 353 | * @return 0 on success, EPERM if the caller lacks permissions to use this
|
|---|
| 354 | * syscall, ENOENT if there is no task matching the specified ID.
|
|---|
| 355 | *
|
|---|
| 356 | */
|
|---|
| 357 | _NO_TRACE static errno_t iospace_disable(task_id_t id, uintptr_t ioaddr, size_t size)
|
|---|
| 358 | {
|
|---|
| 359 | /*
|
|---|
| 360 | * Make sure the caller is authorised to make this syscall.
|
|---|
| 361 | */
|
|---|
| 362 | perm_t perms = perm_get(TASK);
|
|---|
| 363 | if (!(perms & PERM_IO_MANAGER))
|
|---|
| 364 | return EPERM;
|
|---|
| 365 |
|
|---|
| 366 | irq_spinlock_lock(&tasks_lock, true);
|
|---|
| 367 |
|
|---|
| 368 | task_t *task = task_find_by_id(id);
|
|---|
| 369 |
|
|---|
| 370 | if ((!task) || (!container_check(CONTAINER, task->container))) {
|
|---|
| 371 | /*
|
|---|
| 372 | * There is no task with the specified ID
|
|---|
| 373 | * or the task belongs to a different security
|
|---|
| 374 | * context.
|
|---|
| 375 | */
|
|---|
| 376 | irq_spinlock_unlock(&tasks_lock, true);
|
|---|
| 377 | return ENOENT;
|
|---|
| 378 | }
|
|---|
| 379 |
|
|---|
| 380 | /* Lock the task and release the lock protecting tasks dictionary. */
|
|---|
| 381 | irq_spinlock_exchange(&tasks_lock, &task->lock);
|
|---|
| 382 | errno_t rc = ddi_iospace_disable_arch(task, ioaddr, size);
|
|---|
| 383 | irq_spinlock_unlock(&task->lock, true);
|
|---|
| 384 |
|
|---|
| 385 | return rc;
|
|---|
| 386 | }
|
|---|
| 387 |
|
|---|
| 388 | /** Wrapper for SYS_ENABLE_IOSPACE syscall.
|
|---|
| 389 | *
|
|---|
| 390 | * @param uspace_io_arg User space address of DDI argument structure.
|
|---|
| 391 | *
|
|---|
| 392 | * @return 0 on success, otherwise it returns error code found in errno.h
|
|---|
| 393 | *
|
|---|
| 394 | */
|
|---|
| 395 | sys_errno_t sys_iospace_enable(uspace_ptr_ddi_ioarg_t uspace_io_arg)
|
|---|
| 396 | {
|
|---|
| 397 | ddi_ioarg_t arg;
|
|---|
| 398 | errno_t rc = copy_from_uspace(&arg, uspace_io_arg, sizeof(ddi_ioarg_t));
|
|---|
| 399 | if (rc != EOK)
|
|---|
| 400 | return (sys_errno_t) rc;
|
|---|
| 401 |
|
|---|
| 402 | return (sys_errno_t) iospace_enable((task_id_t) arg.task_id,
|
|---|
| 403 | (uintptr_t) arg.ioaddr, (size_t) arg.size);
|
|---|
| 404 | }
|
|---|
| 405 |
|
|---|
| 406 | sys_errno_t sys_iospace_disable(uspace_ptr_ddi_ioarg_t uspace_io_arg)
|
|---|
| 407 | {
|
|---|
| 408 | ddi_ioarg_t arg;
|
|---|
| 409 | errno_t rc = copy_from_uspace(&arg, uspace_io_arg, sizeof(ddi_ioarg_t));
|
|---|
| 410 | if (rc != EOK)
|
|---|
| 411 | return (sys_errno_t) rc;
|
|---|
| 412 |
|
|---|
| 413 | return (sys_errno_t) iospace_disable((task_id_t) arg.task_id,
|
|---|
| 414 | (uintptr_t) arg.ioaddr, (size_t) arg.size);
|
|---|
| 415 | }
|
|---|
| 416 |
|
|---|
| 417 | _NO_TRACE static errno_t dmamem_map(uintptr_t virt, size_t size, unsigned int map_flags,
|
|---|
| 418 | unsigned int flags, uintptr_t *phys)
|
|---|
| 419 | {
|
|---|
| 420 | assert(TASK);
|
|---|
| 421 |
|
|---|
| 422 | // TODO: implement locking of non-anonymous mapping
|
|---|
| 423 | return page_find_mapping(virt, phys);
|
|---|
| 424 | }
|
|---|
| 425 |
|
|---|
| 426 | _NO_TRACE static errno_t dmamem_map_anonymous(size_t size, uintptr_t constraint,
|
|---|
| 427 | unsigned int map_flags, unsigned int flags, uintptr_t *phys,
|
|---|
| 428 | uintptr_t *virt, uintptr_t bound)
|
|---|
| 429 | {
|
|---|
| 430 | assert(TASK);
|
|---|
| 431 |
|
|---|
| 432 | size_t frames = SIZE2FRAMES(size);
|
|---|
| 433 | if (frames == 0)
|
|---|
| 434 | return EINVAL;
|
|---|
| 435 |
|
|---|
| 436 | // FIXME: probably need to ensure that the memory is suitable for DMA
|
|---|
| 437 | *phys = frame_alloc(frames, FRAME_ATOMIC, constraint);
|
|---|
| 438 | if (*phys == 0)
|
|---|
| 439 | return ENOMEM;
|
|---|
| 440 |
|
|---|
| 441 | mem_backend_data_t backend_data;
|
|---|
| 442 | backend_data.base = *phys;
|
|---|
| 443 | backend_data.frames = frames;
|
|---|
| 444 | backend_data.anonymous = true;
|
|---|
| 445 |
|
|---|
| 446 | if (!as_area_create(TASK->as, map_flags, size,
|
|---|
| 447 | AS_AREA_ATTR_NONE, &phys_backend, &backend_data, virt, bound)) {
|
|---|
| 448 | frame_free(*phys, frames);
|
|---|
| 449 | return ENOMEM;
|
|---|
| 450 | }
|
|---|
| 451 |
|
|---|
| 452 | return EOK;
|
|---|
| 453 | }
|
|---|
| 454 |
|
|---|
| 455 | _NO_TRACE static errno_t dmamem_unmap(uintptr_t virt, size_t size)
|
|---|
| 456 | {
|
|---|
| 457 | // TODO: implement unlocking & unmap
|
|---|
| 458 | return EOK;
|
|---|
| 459 | }
|
|---|
| 460 |
|
|---|
| 461 | _NO_TRACE static errno_t dmamem_unmap_anonymous(uintptr_t virt)
|
|---|
| 462 | {
|
|---|
| 463 | return as_area_destroy(TASK->as, virt);
|
|---|
| 464 | }
|
|---|
| 465 |
|
|---|
| 466 | sys_errno_t sys_dmamem_map(size_t size, unsigned int map_flags, unsigned int flags,
|
|---|
| 467 | uspace_ptr_uintptr_t phys_ptr, uspace_ptr_uintptr_t virt_ptr, uintptr_t bound)
|
|---|
| 468 | {
|
|---|
| 469 | if ((flags & DMAMEM_FLAGS_ANONYMOUS) == 0) {
|
|---|
| 470 | /*
|
|---|
| 471 | * Non-anonymous DMA mapping
|
|---|
| 472 | */
|
|---|
| 473 |
|
|---|
| 474 | uintptr_t phys;
|
|---|
| 475 | errno_t rc = dmamem_map(virt_ptr, size, map_flags,
|
|---|
| 476 | flags, &phys);
|
|---|
| 477 |
|
|---|
| 478 | if (rc != EOK)
|
|---|
| 479 | return rc;
|
|---|
| 480 |
|
|---|
| 481 | rc = copy_to_uspace(phys_ptr, &phys, sizeof(phys));
|
|---|
| 482 | if (rc != EOK) {
|
|---|
| 483 | dmamem_unmap(virt_ptr, size);
|
|---|
| 484 | return rc;
|
|---|
| 485 | }
|
|---|
| 486 | } else {
|
|---|
| 487 | /*
|
|---|
| 488 | * Anonymous DMA mapping
|
|---|
| 489 | */
|
|---|
| 490 |
|
|---|
| 491 | uintptr_t constraint;
|
|---|
| 492 | errno_t rc = copy_from_uspace(&constraint, phys_ptr,
|
|---|
| 493 | sizeof(constraint));
|
|---|
| 494 | if (rc != EOK)
|
|---|
| 495 | return rc;
|
|---|
| 496 |
|
|---|
| 497 | uintptr_t virt;
|
|---|
| 498 | rc = copy_from_uspace(&virt, virt_ptr, sizeof(virt));
|
|---|
| 499 | if (rc != EOK)
|
|---|
| 500 | return rc;
|
|---|
| 501 |
|
|---|
| 502 | uintptr_t phys;
|
|---|
| 503 | rc = dmamem_map_anonymous(size, constraint, map_flags, flags,
|
|---|
| 504 | &phys, &virt, bound);
|
|---|
| 505 | if (rc != EOK)
|
|---|
| 506 | return rc;
|
|---|
| 507 |
|
|---|
| 508 | rc = copy_to_uspace(phys_ptr, &phys, sizeof(phys));
|
|---|
| 509 | if (rc != EOK) {
|
|---|
| 510 | dmamem_unmap_anonymous(virt);
|
|---|
| 511 | return rc;
|
|---|
| 512 | }
|
|---|
| 513 |
|
|---|
| 514 | rc = copy_to_uspace(virt_ptr, &virt, sizeof(virt));
|
|---|
| 515 | if (rc != EOK) {
|
|---|
| 516 | dmamem_unmap_anonymous(virt);
|
|---|
| 517 | return rc;
|
|---|
| 518 | }
|
|---|
| 519 | }
|
|---|
| 520 |
|
|---|
| 521 | return EOK;
|
|---|
| 522 | }
|
|---|
| 523 |
|
|---|
| 524 | sys_errno_t sys_dmamem_unmap(uintptr_t virt, size_t size, unsigned int flags)
|
|---|
| 525 | {
|
|---|
| 526 | if ((flags & DMAMEM_FLAGS_ANONYMOUS) == 0)
|
|---|
| 527 | return dmamem_unmap(virt, size);
|
|---|
| 528 | else
|
|---|
| 529 | return dmamem_unmap_anonymous(virt);
|
|---|
| 530 | }
|
|---|
| 531 | void *pio_map(void *phys, size_t size)
|
|---|
| 532 | {
|
|---|
| 533 | #ifdef IO_SPACE_BOUNDARY
|
|---|
| 534 | if (phys < IO_SPACE_BOUNDARY)
|
|---|
| 535 | return phys;
|
|---|
| 536 | #endif
|
|---|
| 537 | return (void *) km_map((uintptr_t) phys, size, KM_NATURAL_ALIGNMENT,
|
|---|
| 538 | PAGE_READ | PAGE_WRITE | PAGE_NOT_CACHEABLE);
|
|---|
| 539 | }
|
|---|
| 540 |
|
|---|
| 541 | void pio_unmap(void *phys, void *virt, size_t size)
|
|---|
| 542 | {
|
|---|
| 543 | #ifdef IO_SPACE_BOUNDARY
|
|---|
| 544 | if (phys < IO_SPACE_BOUNDARY)
|
|---|
| 545 | return;
|
|---|
| 546 | #endif
|
|---|
| 547 | km_unmap((uintptr_t) virt, size);
|
|---|
| 548 | }
|
|---|
| 549 |
|
|---|
| 550 | /** @}
|
|---|
| 551 | */
|
|---|