source: mainline/uspace/lib/c/generic/ddi.c@ 582a0b8

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 582a0b8 was 582a0b8, checked in by Jakub Jermar <jakub@…>, 8 years ago

Remove unistd.h

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