source: mainline/uspace/lib/c/generic/ddi.c@ 8442d10

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 8442d10 was 8442d10, checked in by Martin Decky <martin@…>, 12 years ago

improve the API of physmem_map(), dmamem_map() and dmamem_map_anonymous()
the "constraint" argument of dmamem_map_anonymous() should be used to specify bits disallowed in the physical frame address

  • Property mode set to 100644
File size: 6.6 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 <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
52/** Return unique device number.
53 *
54 * @return New unique device number.
55 *
56 */
57int device_assign_devno(void)
58{
59 return __SYSCALL0(SYS_DEVICE_ASSIGN_DEVNO);
60}
61
62/** Map a piece of physical memory to task.
63 *
64 * Caller of this function must have the CAP_MEM_MANAGER capability.
65 *
66 * @param phys Physical address of the starting frame.
67 * @param pages Number of pages to map.
68 * @param flags Flags for the new address space area.
69 * @param virt Virtual address of the starting page.
70 *
71 * @return EOK on success
72 * @return EPERM if the caller lacks the CAP_MEM_MANAGER capability
73 * @return ENOENT if there is no task with specified ID
74 * @return ENOMEM if there was some problem in creating
75 * the address space area.
76 *
77 */
78int physmem_map(uintptr_t phys, size_t pages, unsigned int flags, void **virt)
79{
80 return __SYSCALL5(SYS_PHYSMEM_MAP, (sysarg_t) phys,
81 pages, flags, (sysarg_t) virt, (sysarg_t) __entry);
82}
83
84int dmamem_map(void *virt, size_t size, unsigned int map_flags,
85 unsigned int flags, uintptr_t *phys)
86{
87 return (int) __SYSCALL6(SYS_DMAMEM_MAP, (sysarg_t) size,
88 (sysarg_t) map_flags, (sysarg_t) flags & ~DMAMEM_FLAGS_ANONYMOUS,
89 (sysarg_t) phys, (sysarg_t) virt, 0);
90}
91
92int dmamem_map_anonymous(size_t size, uintptr_t constraint,
93 unsigned int map_flags, unsigned int flags, uintptr_t *phys, void **virt)
94{
95 *phys = constraint;
96
97 return (int) __SYSCALL6(SYS_DMAMEM_MAP, (sysarg_t) size,
98 (sysarg_t) map_flags, (sysarg_t) flags | DMAMEM_FLAGS_ANONYMOUS,
99 (sysarg_t) phys, (sysarg_t) virt, (sysarg_t) __entry);
100}
101
102int dmamem_unmap(void *virt, size_t size)
103{
104 return __SYSCALL3(SYS_DMAMEM_UNMAP, (sysarg_t) virt, (sysarg_t) size, 0);
105}
106
107int dmamem_unmap_anonymous(void *virt)
108{
109 return __SYSCALL3(SYS_DMAMEM_UNMAP, (sysarg_t) virt, 0,
110 DMAMEM_FLAGS_ANONYMOUS);
111}
112
113/** Enable I/O space range to task.
114 *
115 * Caller of this function must have the IO_MEM_MANAGER capability.
116 *
117 * @param id Task ID.
118 * @param ioaddr Starting address of the I/O range.
119 * @param size Size of the range.
120 *
121 * @return EOK on success
122 * @return EPERM if the caller lacks the CAP_IO_MANAGER capability
123 * @return ENOENT if there is no task with specified ID
124 * @return ENOMEM if there was some problem in allocating memory.
125 *
126 */
127static int iospace_enable(task_id_t id, void *ioaddr, size_t size)
128{
129 const ddi_ioarg_t arg = {
130 .task_id = id,
131 .ioaddr = ioaddr,
132 .size = size
133 };
134
135 return __SYSCALL1(SYS_IOSPACE_ENABLE, (sysarg_t) &arg);
136}
137
138/** Enable PIO for specified I/O range.
139 *
140 * @param pio_addr I/O start address.
141 * @param size Size of the I/O region.
142 * @param virt Virtual address for application's
143 * PIO operations. Can be NULL for PMIO.
144 *
145 * @return EOK on success.
146 * @return Negative error code on failure.
147 *
148 */
149int pio_enable(void *pio_addr, size_t size, void **virt)
150{
151#ifdef IO_SPACE_BOUNDARY
152 if (pio_addr < IO_SPACE_BOUNDARY) {
153 if (virt)
154 *virt = pio_addr;
155 return iospace_enable(task_get_id(), pio_addr, size);
156 }
157#else
158 (void) iospace_enable;
159#endif
160 if (!virt)
161 return EINVAL;
162
163 uintptr_t phys_frame =
164 ALIGN_DOWN((uintptr_t) pio_addr, PAGE_SIZE);
165 size_t offset = (uintptr_t) pio_addr - phys_frame;
166 size_t pages = SIZE2PAGES(offset + size);
167
168 void *virt_page;
169 int rc = physmem_map(phys_frame, pages,
170 AS_AREA_READ | AS_AREA_WRITE, &virt_page);
171 if (rc != EOK)
172 return rc;
173
174 *virt = virt_page + offset;
175 return EOK;
176}
177
178void pio_write_8(ioport8_t *reg, uint8_t val)
179{
180 pio_trace_log(reg, val, true);
181 arch_pio_write_8(reg, val);
182}
183
184void pio_write_16(ioport16_t *reg, uint16_t val)
185{
186 pio_trace_log(reg, val, true);
187 arch_pio_write_16(reg, val);
188}
189
190void pio_write_32(ioport32_t *reg, uint32_t val)
191{
192 pio_trace_log(reg, val, true);
193 arch_pio_write_32(reg, val);
194}
195
196uint8_t pio_read_8(const ioport8_t *reg)
197{
198 const uint8_t val = arch_pio_read_8(reg);
199 pio_trace_log(reg, val, false);
200 return val;
201}
202
203uint16_t pio_read_16(const ioport16_t *reg)
204{
205 const uint16_t val = arch_pio_read_16(reg);
206 pio_trace_log(reg, val, false);
207 return val;
208}
209
210uint32_t pio_read_32(const ioport32_t *reg)
211{
212 const uint32_t val = arch_pio_read_32(reg);
213 pio_trace_log(reg, val, false);
214 return val;
215}
216
217/** Register IRQ notification.
218 *
219 * @param inr IRQ number.
220 * @param devno Device number of the device generating inr.
221 * @param method Use this method for notifying me.
222 * @param ucode Top-half pseudocode handler.
223 *
224 * @return Value returned by the kernel.
225 *
226 */
227int irq_register(int inr, int devno, int method, irq_code_t *ucode)
228{
229 return __SYSCALL4(SYS_IRQ_REGISTER, inr, devno, method,
230 (sysarg_t) ucode);
231}
232
233/** Unregister IRQ notification.
234 *
235 * @param inr IRQ number.
236 * @param devno Device number of the device generating inr.
237 *
238 * @return Value returned by the kernel.
239 *
240 */
241int irq_unregister(int inr, int devno)
242{
243 return __SYSCALL2(SYS_IRQ_UNREGISTER, inr, devno);
244}
245
246/** @}
247 */
Note: See TracBrowser for help on using the repository browser.