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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since ef93167 was aac1c417, checked in by Jan Vesely <jano.vesely@…>, 13 years ago

Fix build on archs without io ports.

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