2 * (c) 2010 Adam Lackorzynski <adam@os.inf.tu-dresden.de>,
3 * Alexander Warg <warg@os.inf.tu-dresden.de>
4 * economic rights: Technische Universität Dresden (Germany)
6 * This file is part of TUD:OS and distributed under the terms of the
7 * GNU General Public License 2.
8 * Please see the COPYING-GPL-2 file for details.
10 #include <l4/re/protocols>
12 #include <l4/cxx/ipc_server>
13 #include <l4/cxx/iostream>
14 #include <l4/cxx/l4iostream>
17 #include <l4/re/namespace>
18 #include <l4/re/dataspace-sys.h>
20 #include <l4/re/error_helper>
22 #include <l4/vbus/vbus_types.h>
23 #include <l4/vbus/vdevice-ops.h>
35 #include "vbus_factory.h"
39 class Root_irq_rs : public Resource_space
46 Root_irq_rs(Vi::System_bus *bus) : Resource_space(), _bus(bus), _icu(0)
49 bool request(Resource *parent, Device *, Resource *child, Device *)
51 // printf("VBUS: IRQ resource request: "); child->dump();
52 Adr_resource *r = dynamic_cast<Adr_resource*>(child);
58 _icu = new Vi::Sw_icu();
59 _bus->add_child(_icu);
62 d_printf(DBG_DEBUG2, "Add IRQ resources to vbus: ");
63 if (dlevel(DBG_DEBUG2))
67 _bus->resource_set()->insert(r);
72 bool alloc(Resource *parent, Device *, Resource *child, Device *, bool)
74 d_printf(DBG_DEBUG2, "Allocate virtual IRQ resource ...\n");
75 if (dlevel(DBG_DEBUG2))
78 Vi::Msi_resource *msi = dynamic_cast<Vi::Msi_resource*>(child);
82 d_printf(DBG_DEBUG2, " Allocate Virtual MSI...\n");
86 _icu = new Vi::Sw_icu();
87 _bus->add_child(_icu);
90 int nr = _icu->alloc_irq(msi->flags(), msi->hw_msi());
93 d_printf(DBG_ERR, "ERROR: cannot allocate MSI resource\n");
97 msi->start_end(nr, nr);
98 msi->del_flags(Resource::F_disabled);
100 if (dlevel(DBG_DEBUG2))
103 msi->hw_msi()->dump(4);
106 _bus->resource_set()->insert(msi);
113 class Root_x_rs : public Resource_space
116 Vi::System_bus *_bus;
119 Root_x_rs(Vi::System_bus *bus) : Resource_space(), _bus(bus)
122 bool request(Resource *parent, Device *, Resource *child, Device *)
124 //printf("VBUS: X resource request: "); child->dump();
125 Adr_resource *r = dynamic_cast<Adr_resource*>(child);
130 _bus->resource_set()->insert(r);
134 bool alloc(Resource *, Device *, Resource *, Device *, bool)
146 System_bus::resource_allocated(Resource const *_r) const
148 Adr_resource const *r = dynamic_cast<Adr_resource const *>(_r);
152 Resource_set::const_iterator i = _resources.find(const_cast<Adr_resource*>(r));
153 if (i == _resources.end())
156 if ((*i)->data().start() <= r->data().start()
157 && (*i)->data().end() >= r->data().end())
164 System_bus::System_bus()
167 add_resource(new Root_resource(Resource::Irq_res, new Root_irq_rs(this)));
168 Resource_space *x = new Root_x_rs(this);
169 add_resource(new Root_resource(Resource::Mmio_res, x));
170 add_resource(new Root_resource(Resource::Mmio_res | Resource::F_prefetchable, x));
171 add_resource(new Root_resource(Resource::Io_res, x));
174 System_bus::~System_bus()
176 registry->unregister_obj(this);
177 // FIXME: must delete all devices
182 System_bus::dump_resources() const
184 for (Resource_set::const_iterator i = _resources.begin(); i != _resources.end(); ++i)
189 System_bus::request_resource(L4::Ipc_iostream &ios)
191 l4vbus_resource_t res;
194 ::Adr_resource ires(res.type, res.start, res.end);
195 if (dlevel(DBG_DEBUG2))
197 printf("request resource: ");
198 Adr_resource(ires).dump();
202 Resource_set::const_iterator i = _resources.find(&ires);
204 for (Resource_set::Const_iterator m = _resources.begin(); m != _resources.end(); ++m)
211 if (i == _resources.end() || !(*i)->contains(ires))
215 if (Io_config::cfg->verbose() > 1)
217 printf(" found resource: ");
223 if (res.type == L4VBUS_RESOURCE_PORT)
225 l4_uint64_t sz = res.end + 1 - res.start;
228 while ((1UL << szl2) < sz)
231 if ((1UL << szl2) > sz)
234 ios << L4::Snd_fpage::io(res.start, szl2, L4_FPAGE_RWX);
243 System_bus::request_iomem(L4::Ipc_iostream &ios)
249 case L4Re::Dataspace_::Map:
251 l4_addr_t offset, spot;
253 ios >> offset >> spot >> flags;
255 // printf("map iomem: %lx...\n", offset);
256 Adr_resource pivot(L4VBUS_RESOURCE_MEM, offset, offset);
257 Resource_set::iterator r = _resources.find(&pivot);
259 if (r == _resources.end())
262 offset = l4_trunc_page(offset);
264 l4_addr_t st = l4_trunc_page((*r)->start());
265 l4_addr_t adr = (*r)->map_iomem();
270 adr = l4_trunc_page(adr);
272 l4_addr_t addr = offset - st + adr;
274 = l4_fpage_max_order(L4_PAGESHIFT,
275 addr, addr, addr + (*r)->size(), spot);
277 // we also might want to do WB instead of UNCACHED...
278 ios << L4::Snd_fpage::mem(l4_trunc_size(addr, order), order,
279 L4_FPAGE_RWX, l4_trunc_page(spot),
281 L4::Snd_fpage::Uncached);
289 System_bus::dispatch(l4_umword_t obj, L4::Ipc_iostream &ios)
294 if (tag.label() == 0)
296 l4vbus_device_handle_t devid;
298 ios >> devid >> func;
299 Device *dev = get_dev_by_id(devid);
302 return dev->vdevice_dispatch(obj, func, ios);
305 if (tag.label() == L4Re::Protocol::Dataspace)
306 return request_iomem(ios);
308 return -L4_EBADPROTO;
313 System_bus::dispatch(l4_umword_t, l4_uint32_t func, L4::Ipc_iostream &ios)
317 case L4vbus_vbus_request_resource:
318 return request_resource(ios);
325 static Dev_factory_t<System_bus> __sb_root_factory("System_bus");