source: mainline/uspace/lib/c/generic/ddi.c@ 38db6288

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

Apply PIO window when parsing HW resources.

  • Each parsed IO and memory range will have both absolute and PIO window relative addresses computed.
  • Provide pio_enable_range() variant of pio_enable() working with addr_range_t.
  • Property mode set to 100644
File size: 7.9 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>
[c6ae4c2]37#include <unistd.h>
[acc0efb]38#include <stdio.h>
[c6ae4c2]39#include <errno.h>
[c0699467]40#include <sys/types.h>
41#include <abi/ddi/arg.h>
[a1e17fc]42#include <ddi.h>
[1ae8f2b]43#include <libarch/ddi.h>
[8049b79]44#include <device/hw_res.h>
45#include <device/hw_res_parsed.h>
46#include <device/pio_window.h>
[a1e17fc]47#include <libc.h>
48#include <task.h>
[0d5a50c]49#include <as.h>
50#include <align.h>
51#include <libarch/config.h>
[fbcdeb8]52#include "private/libc.h"
[a1e17fc]53
[acc0efb]54
[84afc7b]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
[28f8f170]65/** Map a piece of physical memory to task.
[a1e17fc]66 *
67 * Caller of this function must have the CAP_MEM_MANAGER capability.
68 *
[c6ae4c2]69 * @param phys Physical address of the starting frame.
[28f8f170]70 * @param pages Number of pages to map.
71 * @param flags Flags for the new address space area.
[fbcdeb8]72 * @param virt Virtual address of the starting page.
[28f8f170]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.
[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
[c6ae4c2]87int dmamem_map(void *virt, size_t size, unsigned int map_flags,
[8442d10]88 unsigned int flags, uintptr_t *phys)
[fd6bd6d]89{
[fbcdeb8]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);
[fd6bd6d]93}
94
[8442d10]95int dmamem_map_anonymous(size_t size, uintptr_t constraint,
96 unsigned int map_flags, unsigned int flags, uintptr_t *phys, void **virt)
[fd6bd6d]97{
[8442d10]98 *phys = constraint;
99
[fbcdeb8]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);
[fd6bd6d]103}
104
[fbcdeb8]105int dmamem_unmap(void *virt, size_t size)
[fd6bd6d]106{
[fbcdeb8]107 return __SYSCALL3(SYS_DMAMEM_UNMAP, (sysarg_t) virt, (sysarg_t) size, 0);
[fd6bd6d]108}
109
[c6ae4c2]110int dmamem_unmap_anonymous(void *virt)
[fd6bd6d]111{
[c6ae4c2]112 return __SYSCALL3(SYS_DMAMEM_UNMAP, (sysarg_t) virt, 0,
113 DMAMEM_FLAGS_ANONYMOUS);
[fd6bd6d]114}
115
[9426c1a3]116/** Enable I/O space range to task.
117 *
118 * Caller of this function must have the IO_MEM_MANAGER capability.
119 *
[fd6bd6d]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.
[9426c1a3]128 *
129 */
[edb0a33]130static int iospace_enable(task_id_t id, void *ioaddr, size_t size)
[9426c1a3]131{
[edb0a33]132 const ddi_ioarg_t arg = {
133 .task_id = id,
134 .ioaddr = ioaddr,
135 .size = size
136 };
[fd6bd6d]137
[5140e3e]138 return __SYSCALL1(SYS_IOSPACE_ENABLE, (sysarg_t) &arg);
[9426c1a3]139}
[3d77747]140
[8049b79]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 */
146int 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.
[eeb5cc2]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 */
162int 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
[0d5a50c]193/** Enable PIO for specified I/O range.
194 *
[fd6bd6d]195 * @param pio_addr I/O start address.
196 * @param size Size of the I/O region.
[fbcdeb8]197 * @param virt Virtual address for application's
[6659037]198 * PIO operations. Can be NULL for PMIO.
[fd6bd6d]199 *
[fbcdeb8]200 * @return EOK on success.
201 * @return Negative error code on failure.
[0d5a50c]202 *
203 */
[fbcdeb8]204int pio_enable(void *pio_addr, size_t size, void **virt)
[0d5a50c]205{
206#ifdef IO_SPACE_BOUNDARY
207 if (pio_addr < IO_SPACE_BOUNDARY) {
[6659037]208 if (virt)
209 *virt = pio_addr;
[0d5a50c]210 return iospace_enable(task_get_id(), pio_addr, size);
211 }
[aac1c417]212#else
213 (void) iospace_enable;
[0d5a50c]214#endif
[6659037]215 if (!virt)
216 return EINVAL;
[8442d10]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;
[fbcdeb8]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;
[0d5a50c]231}
232
[3218648]233void pio_write_8(ioport8_t *reg, uint8_t val)
234{
[acc0efb]235 pio_trace_log(reg, val, true);
[3218648]236 arch_pio_write_8(reg, val);
237}
238
239void pio_write_16(ioport16_t *reg, uint16_t val)
240{
[acc0efb]241 pio_trace_log(reg, val, true);
[3218648]242 arch_pio_write_16(reg, val);
243}
244
245void pio_write_32(ioport32_t *reg, uint32_t val)
246{
[acc0efb]247 pio_trace_log(reg, val, true);
[3218648]248 arch_pio_write_32(reg, val);
249}
250
[b5c2f56]251uint8_t pio_read_8(const ioport8_t *reg)
[3218648]252{
[acc0efb]253 const uint8_t val = arch_pio_read_8(reg);
254 pio_trace_log(reg, val, false);
255 return val;
[3218648]256}
257
[b5c2f56]258uint16_t pio_read_16(const ioport16_t *reg)
[3218648]259{
[acc0efb]260 const uint16_t val = arch_pio_read_16(reg);
261 pio_trace_log(reg, val, false);
262 return val;
[3218648]263}
264
[b5c2f56]265uint32_t pio_read_32(const ioport32_t *reg)
[3218648]266{
[acc0efb]267 const uint32_t val = arch_pio_read_32(reg);
268 pio_trace_log(reg, val, false);
269 return val;
[3218648]270}
271
[64d2b10]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 */
[f044e96]282int irq_register(int inr, int devno, int method, irq_code_t *ucode)
[64d2b10]283{
[f044e96]284 return __SYSCALL4(SYS_IRQ_REGISTER, inr, devno, method,
[64d2b10]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 */
[f044e96]296int irq_unregister(int inr, int devno)
[64d2b10]297{
[f044e96]298 return __SYSCALL2(SYS_IRQ_UNREGISTER, inr, devno);
[64d2b10]299}
300
[a46da63]301/** @}
[b2951e2]302 */
Note: See TracBrowser for help on using the repository browser.