source: mainline/uspace/lib/c/generic/ddi.c@ 7ee7e6a

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

Further reduce the number of inclusions of sys/types.h

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