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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since bf9cb2f was bf9cb2f, checked in by Martin Decky <martin@…>, 11 years ago

complete the desired API semantics of physmem_map() and dmamem_map_anonymous() to be compatible with as_area_create()
(the user is allowed to request a specific virtual memory base address, the kernel uses an available base address if AS_AREA_ANY is used)

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