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
Line 
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 libc
30 * @{
31 */
32/** @file
33 */
34
35#include <assert.h>
36#include <stdio.h>
37#include <errno.h>
38#include <abi/ddi/arg.h>
39#include <ddi.h>
40#include <libarch/ddi.h>
41#include <device/hw_res.h>
42#include <device/hw_res_parsed.h>
43#include <device/pio_window.h>
44#include <libc.h>
45#include <task.h>
46#include <as.h>
47#include <align.h>
48#include <libarch/config.h>
49#include "private/libc.h"
50
51
52/** Map a piece of physical memory to task.
53 *
54 * Caller of this function must have the PERM_MEM_MANAGER permission.
55 *
56 * @param phys Physical address of the starting frame.
57 * @param pages Number of pages to map.
58 * @param flags Flags for the new address space area.
59 * @param virt Virtual address of the starting page.
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.
63 *
64 * @return EOK on success.
65 * @return EPERM if the caller lacks the PERM_MEM_MANAGER permission.
66 * @return ENOMEM if there was some problem in creating
67 * the address space area.
68 *
69 */
70errno_t physmem_map(uintptr_t phys, size_t pages, unsigned int flags, void **virt)
71{
72 return (errno_t) __SYSCALL5(SYS_PHYSMEM_MAP, (sysarg_t) phys,
73 pages, flags, (sysarg_t) virt, (sysarg_t) __progsymbols.end);
74}
75
76/** Unmap a piece of physical memory to task.
77 *
78 * Caller of this function must have the PERM_MEM_MANAGER permission.
79 *
80 * @param virt Virtual address from the phys-mapped region.
81 *
82 * @return EOK on success.
83 * @return EPERM if the caller lacks the PERM_MEM_MANAGER permission.
84 *
85 */
86errno_t physmem_unmap(void *virt)
87{
88 return (errno_t) __SYSCALL1(SYS_PHYSMEM_UNMAP, (sysarg_t) virt);
89}
90
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 *
97 * Caller of this function must have the PERM_MEM_MANAGER permission.
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.
106 * @return EPERM if the caller lacks the PERM_MEM_MANAGER permission.
107 * @return ENOMEM if there was some problem in creating
108 * the address space area.
109 *
110 */
111errno_t dmamem_map(void *virt, size_t size, unsigned int map_flags,
112 unsigned int flags, uintptr_t *phys)
113{
114 return (errno_t) __SYSCALL6(SYS_DMAMEM_MAP, (sysarg_t) size,
115 (sysarg_t) map_flags, (sysarg_t) flags & ~DMAMEM_FLAGS_ANONYMOUS,
116 (sysarg_t) phys, (sysarg_t) virt, 0);
117}
118
119/** Map a piece of physical memory suitable for DMA transfers.
120 *
121 * Caller of this function must have the PERM_MEM_MANAGER permission.
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.
134 * @return EPERM if the caller lacks the PERM_MEM_MANAGER permission.
135 * @return ENOMEM if there was some problem in creating
136 * the address space area.
137 *
138 */
139errno_t dmamem_map_anonymous(size_t size, uintptr_t constraint,
140 unsigned int map_flags, unsigned int flags, uintptr_t *phys, void **virt)
141{
142 *phys = constraint;
143
144 return (errno_t) __SYSCALL6(SYS_DMAMEM_MAP, (sysarg_t) size,
145 (sysarg_t) map_flags, (sysarg_t) flags | DMAMEM_FLAGS_ANONYMOUS,
146 (sysarg_t) phys, (sysarg_t) virt, (sysarg_t) __progsymbols.end);
147}
148
149errno_t dmamem_unmap(void *virt, size_t size)
150{
151 return (errno_t) __SYSCALL3(SYS_DMAMEM_UNMAP, (sysarg_t) virt, (sysarg_t) size, 0);
152}
153
154errno_t dmamem_unmap_anonymous(void *virt)
155{
156 return (errno_t) __SYSCALL3(SYS_DMAMEM_UNMAP, (sysarg_t) virt, 0,
157 DMAMEM_FLAGS_ANONYMOUS);
158}
159
160/** Enable I/O space range to task.
161 *
162 * Caller of this function must have the PERM_IO_MANAGER permission.
163 *
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
169 * @return EPERM if the caller lacks the PERM_IO_MANAGER permission
170 * @return ENOENT if there is no task with specified ID
171 * @return ENOMEM if there was some problem in allocating memory.
172 *
173 */
174static errno_t iospace_enable(task_id_t id, void *ioaddr, size_t size)
175{
176 const ddi_ioarg_t arg = {
177 .task_id = id,
178 .ioaddr = ioaddr,
179 .size = size
180 };
181
182 return (errno_t) __SYSCALL1(SYS_IOSPACE_ENABLE, (sysarg_t) &arg);
183}
184
185/** Disable I/O space range to task.
186 *
187 * Caller of this function must have the PERM_IO_MANAGER permission.
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
194 * @return EPERM if the caller lacks the PERM_IO_MANAGER permission
195 * @return ENOENT if there is no task with specified ID
196 *
197 */
198static errno_t iospace_disable(task_id_t id, void *ioaddr, size_t size)
199{
200 const ddi_ioarg_t arg = {
201 .task_id = id,
202 .ioaddr = ioaddr,
203 .size = size
204 };
205
206 return (errno_t) __SYSCALL1(SYS_IOSPACE_DISABLE, (sysarg_t) &arg);
207}
208
209/** Enable PIO for specified address range.
210 *
211 * @param range I/O range to be enable.
212 * @param virt Virtual address for application's PIO operations.
213 */
214errno_t pio_enable_range(addr_range_t *range, void **virt)
215{
216 return pio_enable(RNGABSPTR(*range), RNGSZ(*range), virt);
217}
218
219/** Enable PIO for specified HW resource wrt. to the PIO window.
220 *
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.
225 * @param[out] phys If non-NULL, physical address of the resource
226 * @param[out] size If non-NULL, size of the enabled resource.
227 *
228 * @return EOK on success.
229 * @return An error code on failure.
230 *
231 */
232errno_t pio_enable_resource(pio_window_t *win, hw_resource_t *res, void **virt,
233 uintptr_t *phys, size_t *size)
234{
235 uintptr_t addr;
236 size_t sz;
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 }
246 sz = res->res.io_range.size;
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 }
255 sz = res->res.mem_range.size;
256 break;
257 default:
258 return EINVAL;
259 }
260
261 if (phys)
262 *phys = addr;
263 if (size)
264 *size = sz;
265
266 return pio_enable((void *) addr, sz, virt);
267}
268
269/** Enable PIO for specified I/O range.
270 *
271 * @param pio_addr I/O start address.
272 * @param size Size of the I/O region.
273 * @param virt Virtual address for application's
274 * PIO operations. Can be NULL for PMIO.
275 *
276 * @return EOK on success.
277 * @return An error code on failure.
278 *
279 */
280errno_t pio_enable(void *pio_addr, size_t size, void **virt)
281{
282#ifdef IO_SPACE_BOUNDARY
283 if (pio_addr < IO_SPACE_BOUNDARY) {
284 if (virt)
285 *virt = pio_addr;
286 return iospace_enable(task_get_id(), pio_addr, size);
287 }
288#else
289 (void) iospace_enable;
290#endif
291 if (!virt)
292 return EINVAL;
293
294 uintptr_t phys_frame =
295 ALIGN_DOWN((uintptr_t) pio_addr, PAGE_SIZE);
296 size_t offset = (uintptr_t) pio_addr - phys_frame;
297 size_t pages = SIZE2PAGES(offset + size);
298
299 void *virt_page = AS_AREA_ANY;
300 errno_t rc = physmem_map(phys_frame, pages,
301 AS_AREA_READ | AS_AREA_WRITE, &virt_page);
302 if (rc != EOK)
303 return rc;
304
305 *virt = virt_page + offset;
306 return EOK;
307}
308
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.
315 * @return An error code on failure.
316 *
317 */
318errno_t pio_disable(void *virt, size_t size)
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
329void pio_write_8(ioport8_t *reg, uint8_t val)
330{
331 pio_trace_log(reg, val, true);
332 arch_pio_write_8(reg, val);
333}
334
335void pio_write_16(ioport16_t *reg, uint16_t val)
336{
337 pio_trace_log(reg, val, true);
338 arch_pio_write_16(reg, val);
339}
340
341void pio_write_32(ioport32_t *reg, uint32_t val)
342{
343 pio_trace_log(reg, val, true);
344 arch_pio_write_32(reg, val);
345}
346
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
353uint8_t pio_read_8(const ioport8_t *reg)
354{
355 const uint8_t val = arch_pio_read_8(reg);
356 pio_trace_log(reg, val, false);
357 return val;
358}
359
360uint16_t pio_read_16(const ioport16_t *reg)
361{
362 const uint16_t val = arch_pio_read_16(reg);
363 pio_trace_log(reg, val, false);
364 return val;
365}
366
367uint32_t pio_read_32(const ioport32_t *reg)
368{
369 const uint32_t val = arch_pio_read_32(reg);
370 pio_trace_log(reg, val, false);
371 return val;
372}
373
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
381/** @}
382 */
Note: See TracBrowser for help on using the repository browser.