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>
15 #include <l4/re/namespace>
16 #include <l4/re/dataspace-sys.h>
18 #include <l4/re/error_helper>
20 #include <l4/vbus/vbus_types.h>
21 #include <l4/vbus/vdevice-ops.h>
33 #include "vbus_factory.h"
37 class Root_irq_rs : public Resource_space
44 Root_irq_rs(Vi::System_bus *bus)
45 : Resource_space(), _bus(bus), _icu(new Vi::Sw_icu())
47 _bus->add_child(_icu);
51 bool request(Resource *parent, Device *, Resource *child, Device *)
53 // printf("VBUS: IRQ resource request: "); child->dump();
59 _icu = new Vi::Sw_icu();
60 _bus->add_child(_icu);
64 d_printf(DBG_DEBUG2, "Add IRQ resources to vbus: ");
65 if (dlevel(DBG_DEBUG2))
68 _icu->add_irqs(child);
69 _bus->resource_set()->insert(child);
74 bool alloc(Resource *parent, Device *, Resource *child, Device *, bool)
76 d_printf(DBG_DEBUG2, "Allocate virtual IRQ resource ...\n");
77 if (dlevel(DBG_DEBUG2))
80 Vi::Msi_resource *msi = dynamic_cast<Vi::Msi_resource*>(child);
84 d_printf(DBG_DEBUG2, " Allocate Virtual MSI...\n");
88 _icu = new Vi::Sw_icu();
89 _bus->add_child(_icu);
92 int nr = _icu->alloc_irq(msi->flags(), msi->hw_msi());
95 d_printf(DBG_ERR, "ERROR: cannot allocate MSI resource\n");
99 msi->start_end(nr, nr);
100 msi->del_flags(Resource::F_disabled);
102 if (dlevel(DBG_DEBUG2))
105 msi->hw_msi()->dump(4);
108 _bus->resource_set()->insert(msi);
115 class Root_x_rs : public Resource_space
118 Vi::System_bus *_bus;
121 Root_x_rs(Vi::System_bus *bus) : Resource_space(), _bus(bus)
124 bool request(Resource *parent, Device *, Resource *child, Device *)
126 //printf("VBUS: X resource request: "); child->dump();
131 _bus->resource_set()->insert(child);
135 bool alloc(Resource *, Device *, Resource *, Device *, bool)
147 System_bus::resource_allocated(Resource const *r) const
149 Resource_set::const_iterator i = _resources.find(const_cast<Resource*>(r));
150 if (i == _resources.end())
153 if ((*i)->start() <= r->start()
154 && (*i)->end() >= r->end())
161 System_bus::System_bus() : _sw_icu(0)
164 add_resource(new Root_resource(Resource::Irq_res, new Root_irq_rs(this)));
165 Resource_space *x = new Root_x_rs(this);
166 add_resource(new Root_resource(Resource::Mmio_res, x));
167 add_resource(new Root_resource(Resource::Mmio_res | Resource::F_prefetchable, x));
168 add_resource(new Root_resource(Resource::Io_res, x));
171 System_bus::~System_bus()
173 registry->unregister_obj(this);
174 // FIXME: must delete all devices
179 System_bus::dump_resources() const
181 for (Resource_set::const_iterator i = _resources.begin(); i != _resources.end(); ++i)
186 System_bus::request_resource(L4::Ipc::Iostream &ios)
188 l4vbus_resource_t res;
191 Resource ires(res.type, res.start, res.end);
192 if (dlevel(DBG_DEBUG2))
194 printf("request resource: ");
199 Resource_set::const_iterator i = _resources.find(&ires);
201 for (Resource_set::Const_iterator m = _resources.begin(); m != _resources.end(); ++m)
208 if (i == _resources.end() || !(*i)->contains(ires))
212 if (Io_config::cfg->verbose() > 1)
214 printf(" found resource: ");
220 if (res.type == L4VBUS_RESOURCE_PORT)
222 l4_uint64_t sz = res.end + 1 - res.start;
225 while ((1UL << szl2) < sz)
228 if ((1UL << szl2) > sz)
231 ios << L4::Ipc::Snd_fpage::io(res.start, szl2, L4_FPAGE_RWX);
240 System_bus::request_iomem(L4::Ipc::Iostream &ios)
246 case L4Re::Dataspace_::Map:
248 l4_addr_t offset, spot;
250 ios >> offset >> spot >> flags;
252 // printf("map iomem: %lx...\n", offset);
253 Resource pivot(L4VBUS_RESOURCE_MEM, offset, offset);
254 Resource_set::iterator r = _resources.find(&pivot);
256 if (r == _resources.end())
259 offset = l4_trunc_page(offset);
261 l4_addr_t st = l4_trunc_page((*r)->start());
262 l4_addr_t adr = (*r)->map_iomem();
267 adr = l4_trunc_page(adr);
269 l4_addr_t addr = offset - st + adr;
271 = l4_fpage_max_order(L4_PAGESHIFT,
272 addr, addr, addr + (*r)->size(), spot);
274 // we also might want to do WB instead of UNCACHED...
275 ios << L4::Ipc::Snd_fpage::mem(l4_trunc_size(addr, order), order,
276 L4_FPAGE_RWX, l4_trunc_page(spot),
277 L4::Ipc::Snd_fpage::Map,
278 L4::Ipc::Snd_fpage::Uncached);
286 System_bus::dispatch(l4_umword_t obj, L4::Ipc::Iostream &ios)
291 if (tag.label() == 0)
293 l4vbus_device_handle_t devid;
295 ios >> devid >> func;
296 Device *dev = get_dev_by_id(devid);
299 return dev->vdevice_dispatch(obj, func, ios);
302 if (tag.label() == L4Re::Protocol::Dataspace)
303 return request_iomem(ios);
305 return -L4_EBADPROTO;
310 System_bus::dispatch(l4_umword_t, l4_uint32_t func, L4::Ipc::Iostream &ios)
314 case L4vbus_vbus_request_resource:
315 return request_resource(ios);
322 static Dev_factory_t<System_bus> __sb_root_factory("System_bus");