source: mainline/uspace/lib/c/generic/ddi.c@ b1834a01

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b1834a01 was 498ced1, checked in by Jiří Zárevúcky <jiri.zarevucky@…>, 7 years ago

Unify reference counting and remove some unnecessary instances of <atomic.h>

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