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

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

Add pio_disable().

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