source: mainline/uspace/lib/c/generic/ddi.c@ 1be7bee

Last change on this file since 1be7bee was 09ab0a9a, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix vertical spacing with new Ccheck revision.

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