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 | */
|
---|
60 | int 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 | *
|
---|
74 | * @return EOK on success
|
---|
75 | * @return EPERM if the caller lacks the CAP_MEM_MANAGER capability
|
---|
76 | * @return ENOENT if there is no task with specified ID
|
---|
77 | * @return ENOMEM if there was some problem in creating
|
---|
78 | * the address space area.
|
---|
79 | *
|
---|
80 | */
|
---|
81 | int 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 | int dmamem_map(void *virt, size_t size, unsigned int map_flags,
|
---|
88 | unsigned int flags, uintptr_t *phys)
|
---|
89 | {
|
---|
90 | return (int) __SYSCALL6(SYS_DMAMEM_MAP, (sysarg_t) size,
|
---|
91 | (sysarg_t) map_flags, (sysarg_t) flags & ~DMAMEM_FLAGS_ANONYMOUS,
|
---|
92 | (sysarg_t) phys, (sysarg_t) virt, 0);
|
---|
93 | }
|
---|
94 |
|
---|
95 | int dmamem_map_anonymous(size_t size, uintptr_t constraint,
|
---|
96 | unsigned int map_flags, unsigned int flags, uintptr_t *phys, void **virt)
|
---|
97 | {
|
---|
98 | *phys = constraint;
|
---|
99 |
|
---|
100 | return (int) __SYSCALL6(SYS_DMAMEM_MAP, (sysarg_t) size,
|
---|
101 | (sysarg_t) map_flags, (sysarg_t) flags | DMAMEM_FLAGS_ANONYMOUS,
|
---|
102 | (sysarg_t) phys, (sysarg_t) virt, (sysarg_t) __entry);
|
---|
103 | }
|
---|
104 |
|
---|
105 | int dmamem_unmap(void *virt, size_t size)
|
---|
106 | {
|
---|
107 | return __SYSCALL3(SYS_DMAMEM_UNMAP, (sysarg_t) virt, (sysarg_t) size, 0);
|
---|
108 | }
|
---|
109 |
|
---|
110 | int dmamem_unmap_anonymous(void *virt)
|
---|
111 | {
|
---|
112 | return __SYSCALL3(SYS_DMAMEM_UNMAP, (sysarg_t) virt, 0,
|
---|
113 | DMAMEM_FLAGS_ANONYMOUS);
|
---|
114 | }
|
---|
115 |
|
---|
116 | /** Enable I/O space range to task.
|
---|
117 | *
|
---|
118 | * Caller of this function must have the IO_MEM_MANAGER capability.
|
---|
119 | *
|
---|
120 | * @param id Task ID.
|
---|
121 | * @param ioaddr Starting address of the I/O range.
|
---|
122 | * @param size Size of the range.
|
---|
123 | *
|
---|
124 | * @return EOK on success
|
---|
125 | * @return EPERM if the caller lacks the CAP_IO_MANAGER capability
|
---|
126 | * @return ENOENT if there is no task with specified ID
|
---|
127 | * @return ENOMEM if there was some problem in allocating memory.
|
---|
128 | *
|
---|
129 | */
|
---|
130 | static int iospace_enable(task_id_t id, void *ioaddr, size_t size)
|
---|
131 | {
|
---|
132 | const ddi_ioarg_t arg = {
|
---|
133 | .task_id = id,
|
---|
134 | .ioaddr = ioaddr,
|
---|
135 | .size = size
|
---|
136 | };
|
---|
137 |
|
---|
138 | return __SYSCALL1(SYS_IOSPACE_ENABLE, (sysarg_t) &arg);
|
---|
139 | }
|
---|
140 |
|
---|
141 | /** Enable PIO for specified address range.
|
---|
142 | *
|
---|
143 | * @param range I/O range to be enable.
|
---|
144 | * @param virt Virtual address for application's PIO operations.
|
---|
145 | */
|
---|
146 | int pio_enable_range(addr_range_t *range, void **virt)
|
---|
147 | {
|
---|
148 | return pio_enable(RNGABSPTR(*range), RNGSZ(*range), virt);
|
---|
149 | }
|
---|
150 |
|
---|
151 | /** Enable PIO for specified HW resource wrt. to the PIO window.
|
---|
152 | *
|
---|
153 | * @param win PIO window. May be NULL if the resources are known to be
|
---|
154 | * absolute.
|
---|
155 | * @param res Resources specifying the I/O range wrt. to the PIO window.
|
---|
156 | * @param virt Virtual address for application's PIO operations.
|
---|
157 | *
|
---|
158 | * @return EOK on success.
|
---|
159 | * @return Negative error code on failure.
|
---|
160 | *
|
---|
161 | */
|
---|
162 | int pio_enable_resource(pio_window_t *win, hw_resource_t *res, void **virt)
|
---|
163 | {
|
---|
164 | uintptr_t addr;
|
---|
165 | size_t size;
|
---|
166 |
|
---|
167 | switch (res->type) {
|
---|
168 | case IO_RANGE:
|
---|
169 | addr = res->res.io_range.address;
|
---|
170 | if (res->res.io_range.relative) {
|
---|
171 | if (!win)
|
---|
172 | return EINVAL;
|
---|
173 | addr += win->io.base;
|
---|
174 | }
|
---|
175 | size = res->res.io_range.size;
|
---|
176 | break;
|
---|
177 | case MEM_RANGE:
|
---|
178 | addr = res->res.mem_range.address;
|
---|
179 | if (res->res.mem_range.relative) {
|
---|
180 | if (!win)
|
---|
181 | return EINVAL;
|
---|
182 | addr += win->mem.base;
|
---|
183 | }
|
---|
184 | size = res->res.mem_range.size;
|
---|
185 | break;
|
---|
186 | default:
|
---|
187 | return EINVAL;
|
---|
188 | }
|
---|
189 |
|
---|
190 | return pio_enable((void *) addr, size, virt);
|
---|
191 | }
|
---|
192 |
|
---|
193 | /** Enable PIO for specified I/O range.
|
---|
194 | *
|
---|
195 | * @param pio_addr I/O start address.
|
---|
196 | * @param size Size of the I/O region.
|
---|
197 | * @param virt Virtual address for application's
|
---|
198 | * PIO operations. Can be NULL for PMIO.
|
---|
199 | *
|
---|
200 | * @return EOK on success.
|
---|
201 | * @return Negative error code on failure.
|
---|
202 | *
|
---|
203 | */
|
---|
204 | int pio_enable(void *pio_addr, size_t size, void **virt)
|
---|
205 | {
|
---|
206 | #ifdef IO_SPACE_BOUNDARY
|
---|
207 | if (pio_addr < IO_SPACE_BOUNDARY) {
|
---|
208 | if (virt)
|
---|
209 | *virt = pio_addr;
|
---|
210 | return iospace_enable(task_get_id(), pio_addr, size);
|
---|
211 | }
|
---|
212 | #else
|
---|
213 | (void) iospace_enable;
|
---|
214 | #endif
|
---|
215 | if (!virt)
|
---|
216 | return EINVAL;
|
---|
217 |
|
---|
218 | uintptr_t phys_frame =
|
---|
219 | ALIGN_DOWN((uintptr_t) pio_addr, PAGE_SIZE);
|
---|
220 | size_t offset = (uintptr_t) pio_addr - phys_frame;
|
---|
221 | size_t pages = SIZE2PAGES(offset + size);
|
---|
222 |
|
---|
223 | void *virt_page;
|
---|
224 | int rc = physmem_map(phys_frame, pages,
|
---|
225 | AS_AREA_READ | AS_AREA_WRITE, &virt_page);
|
---|
226 | if (rc != EOK)
|
---|
227 | return rc;
|
---|
228 |
|
---|
229 | *virt = virt_page + offset;
|
---|
230 | return EOK;
|
---|
231 | }
|
---|
232 |
|
---|
233 | void pio_write_8(ioport8_t *reg, uint8_t val)
|
---|
234 | {
|
---|
235 | pio_trace_log(reg, val, true);
|
---|
236 | arch_pio_write_8(reg, val);
|
---|
237 | }
|
---|
238 |
|
---|
239 | void pio_write_16(ioport16_t *reg, uint16_t val)
|
---|
240 | {
|
---|
241 | pio_trace_log(reg, val, true);
|
---|
242 | arch_pio_write_16(reg, val);
|
---|
243 | }
|
---|
244 |
|
---|
245 | void pio_write_32(ioport32_t *reg, uint32_t val)
|
---|
246 | {
|
---|
247 | pio_trace_log(reg, val, true);
|
---|
248 | arch_pio_write_32(reg, val);
|
---|
249 | }
|
---|
250 |
|
---|
251 | uint8_t pio_read_8(const ioport8_t *reg)
|
---|
252 | {
|
---|
253 | const uint8_t val = arch_pio_read_8(reg);
|
---|
254 | pio_trace_log(reg, val, false);
|
---|
255 | return val;
|
---|
256 | }
|
---|
257 |
|
---|
258 | uint16_t pio_read_16(const ioport16_t *reg)
|
---|
259 | {
|
---|
260 | const uint16_t val = arch_pio_read_16(reg);
|
---|
261 | pio_trace_log(reg, val, false);
|
---|
262 | return val;
|
---|
263 | }
|
---|
264 |
|
---|
265 | uint32_t pio_read_32(const ioport32_t *reg)
|
---|
266 | {
|
---|
267 | const uint32_t val = arch_pio_read_32(reg);
|
---|
268 | pio_trace_log(reg, val, false);
|
---|
269 | return val;
|
---|
270 | }
|
---|
271 |
|
---|
272 | /** Register IRQ notification.
|
---|
273 | *
|
---|
274 | * @param inr IRQ number.
|
---|
275 | * @param devno Device number of the device generating inr.
|
---|
276 | * @param method Use this method for notifying me.
|
---|
277 | * @param ucode Top-half pseudocode handler.
|
---|
278 | *
|
---|
279 | * @return Value returned by the kernel.
|
---|
280 | *
|
---|
281 | */
|
---|
282 | int irq_register(int inr, int devno, int method, const irq_code_t *ucode)
|
---|
283 | {
|
---|
284 | return __SYSCALL4(SYS_IRQ_REGISTER, inr, devno, method,
|
---|
285 | (sysarg_t) ucode);
|
---|
286 | }
|
---|
287 |
|
---|
288 | /** Unregister IRQ notification.
|
---|
289 | *
|
---|
290 | * @param inr IRQ number.
|
---|
291 | * @param devno Device number of the device generating inr.
|
---|
292 | *
|
---|
293 | * @return Value returned by the kernel.
|
---|
294 | *
|
---|
295 | */
|
---|
296 | int irq_unregister(int inr, int devno)
|
---|
297 | {
|
---|
298 | return __SYSCALL2(SYS_IRQ_UNREGISTER, inr, devno);
|
---|
299 | }
|
---|
300 |
|
---|
301 | /** @}
|
---|
302 | */
|
---|