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