source: mainline/uspace/lib/c/generic/ddi.c@ 7ee7e6a

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

Further reduce the number of inclusions of sys/types.h

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