2 * (c) 2008-2010 Adam Lackorzynski <adam@os.inf.tu-dresden.de>,
3 * Alexander Warg <warg@os.inf.tu-dresden.de>,
4 * Torsten Frenzel <frenzel@os.inf.tu-dresden.de>,
5 * Henning Schild <hschild@os.inf.tu-dresden.de>
6 * economic rights: Technische Universität Dresden (Germany)
7 * This file is part of TUD:OS and distributed under the terms of the
8 * GNU Lesser General Public License 2.1.
9 * Please see the COPYING-LGPL-2.1 file for details.
11 #include <l4/cxx/exceptions>
12 #include <l4/cxx/iostream>
14 #include <l4/vbus/vbus.h>
15 #include <l4/io/types.h>
16 #include <l4/re/namespace>
19 #include <l4/re/error_helper>
20 #include <l4/re/util/cap_alloc>
21 #include <l4/sys/factory>
24 #include <l4/sys/task>
25 #include <l4/sys/kdebug.h>
26 #include <l4/util/splitlog2.h>
27 #include <l4/crtn/initpriorities.h>
34 using L4Re::Util::Auto_cap;
38 /***********************************************************************
40 ***********************************************************************/
41 static Cap<void> &vbus()
43 static Cap<void> _vbus;
47 static Cap<L4::Icu> &icu()
49 static Cap<L4::Icu> _icu;
55 extern "C" void __attribute__((__used__))
56 __internal_l4io_init()
60 L4::Cap<L4Re::Rm> rm = L4Re::Env::env()->rm();
62 vbus() = L4Re::Env::env()->get_cap<void>("vbus");
63 if (!vbus().is_valid())
65 printf("libio: Warning: Query of 'vbus' failed!\n");
69 l4vbus_device_handle_t handle = 0;
70 int ret = l4vbus_get_device_by_hid(vbus().cap(), 0, &handle, "L40009",
74 //printf("libio: Warning: Finding 'icu' in system bus failed with '%s'\n",
75 // l4sys_errtostr(ret));
79 icu() = L4Re::chkcap(L4Re::Util::cap_alloc.alloc<L4::Icu>(),
80 "allocating ICU cap");
82 ret = l4vbus_vicu_get_cap(vbus().cap(), handle, icu().cap());
85 printf("libio: Warning: Getting 'icu' device failed.\n");
86 L4Re::Util::cap_alloc.free(icu());
90 catch (L4::Runtime_error const &e)
93 printf("Libio caught exception\n");
98 L4_DECLARE_CONSTRUCTOR(__internal_l4io_init, INIT_PRIO_LIBIO_INIT);
101 /***********************************************************************
103 ***********************************************************************/
106 l4io_request_irq(int irqnum, l4_cap_idx_t irq_cap)
108 L4::Cap<L4::Irq> irq(irq_cap);
109 long ret = l4_error(L4Re::Env::env()->factory()->create_irq(irq));
111 printf("Create irq failed with %ld\n", ret);
115 ret = l4_error(icu()->bind(irqnum, irq));
117 printf("Bind irq to icu failed with %ld\n", ret);
126 { return icu().cap(); }
129 l4io_release_irq(int irqnum, l4_cap_idx_t irq_cap)
131 long ret = l4_error(icu()->unbind(irqnum, L4::Cap<L4::Irq>(irq_cap)));
133 printf("Unbind irq %d from icu failed with %ld\n", irqnum, ret);
137 l4_task_unmap(L4_BASE_TASK_CAP,
138 l4_obj_fpage(irq_cap, 0, L4_FPAGE_RWX),
144 /***********************************************************************
146 ***********************************************************************/
149 __map_iomem(l4_addr_t phys, l4_addr_t* virt, unsigned long size, int flags)
151 Cap<L4Re::Dataspace> iomem = L4::cap_cast<L4Re::Dataspace>(vbus());
152 unsigned char align = L4_PAGESHIFT;
154 if (size >= L4_SUPERPAGESIZE)
155 align = L4_SUPERPAGESHIFT;
157 unsigned long rmflags = 0;
159 if (flags & L4IO_MEM_EAGER_MAP)
160 rmflags |= L4Re::Rm::Eager_map;
162 if (*virt && (flags & L4IO_MEM_USE_RESERVED_AREA))
163 rmflags |= L4Re::Rm::In_area;
166 rmflags |= L4Re::Rm::Search_addr;
168 int res = L4Re::Env::env()->rm()->attach(virt, size, rmflags, iomem, phys, align);
171 printf("Cannot attach iomem to virtual address %lx with size %lx: %s(%d).\n",
172 *virt, size, l4sys_errtostr(res), res);
180 l4io_request_iomem(l4_addr_t phys, unsigned long size,
181 int flags, l4_addr_t *virt)
184 return __map_iomem(phys, virt, size, flags);
188 l4io_request_iomem_region(l4_addr_t phys, l4_addr_t virt, unsigned long size,
192 return -L4_EADDRNOTAVAIL;
193 return __map_iomem(phys, &virt, size, flags);
197 l4io_release_iomem(l4_addr_t virt, unsigned long size)
200 return L4Re::Env::env()->rm()->detach(virt, 0);
204 l4io_search_iomem_region(l4_addr_t phys, l4_addr_t size,
205 l4_addr_t *rstart, l4_addr_t *rsize)
207 *rstart = l4_trunc_page(phys);
208 *rsize = l4_round_page(size + phys - *rstart);
212 /***********************************************************************
214 ***********************************************************************/
217 l4io_request_ioport(unsigned portnum, unsigned len)
219 l4vbus_resource_t res;
220 res.type = L4IO_RESOURCE_PORT;
222 res.end = portnum + len - 1;
223 return l4vbus_request_resource(vbus().cap(), &res, 0);
227 l4io_release_ioport(unsigned portnum, unsigned len)
229 l4vbus_resource_t res;
230 res.type = L4IO_RESOURCE_PORT;
232 res.end = portnum + len - 1;
233 return l4vbus_release_resource(vbus().cap(), &res);
236 /***********************************************************************
238 ***********************************************************************/
241 l4io_iterate_devices(l4io_device_handle_t *devhandle,
242 l4io_device_t *dev, l4io_resource_handle_t *reshandle)
247 return l4vbus_get_next_device(vbus().cap(), L4VBUS_NULL,
248 devhandle, L4VBUS_MAX_DEPTH, dev);
252 l4io_lookup_device(const char *devname,
253 l4io_device_handle_t *dev_handle, l4io_device_t *dev,
254 l4io_resource_handle_t *res_handle)
257 l4io_device_handle_t dh = 0;
259 if (!vbus().is_valid())
262 if ((r = l4vbus_get_device_by_hid(vbus().cap(), 0,
263 &dh, devname, L4VBUS_MAX_DEPTH, dev)))
276 l4io_lookup_resource(l4io_device_handle_t devhandle,
277 enum l4io_resource_types_t type,
278 l4io_resource_handle_t *res_handle,
279 l4io_resource_t *desc)
281 l4vbus_resource_t resource;
282 while (!l4vbus_get_resource(vbus().cap(), devhandle, *res_handle, &resource))
285 // copy device description
286 if (resource.type == type || type == L4IO_RESOURCE_ANY)
297 l4io_request_resource_iomem(l4io_device_handle_t devhandle,
298 l4io_resource_handle_t *reshandle)
303 if (l4io_lookup_resource(devhandle, L4IO_RESOURCE_MEM,
308 if (l4io_request_iomem(res.start, res.end - res.start + 1,
309 L4IO_MEM_NONCACHED, &v))
316 void __l4io_get_all_ports(l4vbus_device_handle_t parent)
318 l4vbus_device_handle_t next_dev = 0;
319 l4vbus_device_t info;
321 while (!l4vbus_get_next_device(vbus().cap(), parent, &next_dev,
322 L4VBUS_MAX_DEPTH, &info))
324 l4vbus_resource_t resource;
325 for (unsigned r = 0; r < info.num_resources; ++r)
327 l4vbus_get_resource(vbus().cap(), next_dev, r, &resource);
328 if (resource.type == L4IO_RESOURCE_PORT)
329 l4vbus_request_resource(vbus().cap(), &resource, 0);
335 l4io_request_all_ioports(void)
337 __l4io_get_all_ports(l4io_get_root_device());
341 l4io_has_resource(enum l4io_resource_types_t type,
342 l4vbus_paddr_t start, l4vbus_paddr_t end)
344 l4io_device_handle_t dh = l4io_get_root_device();
346 l4io_resource_handle_t reshandle;
352 if (l4io_iterate_devices(&dh, &dev, &reshandle))
355 if (dev.num_resources)
356 while (!l4io_lookup_resource(dh, type, &reshandle, &res))
357 if (start >= res.start && end <= res.end)