2 * (c) 2010 Alexander Warg <warg@os.inf.tu-dresden.de>
3 * economic rights: Technische Universität Dresden (Germany)
5 * This file is part of TUD:OS and distributed under the terms of the
6 * GNU General Public License 2.
7 * Please see the COPYING-GPL-2 file for details.
10 #include <l4/vbus/vdevice-ops.h>
13 #include "vbus_factory.h"
19 #include <l4/re/util/cap_alloc>
20 #include <l4/re/namespace>
22 #include <l4/re/error_helper>
25 #include <l4/sys/debugger.h>
26 #include <l4/sys/kdebug.h>
33 using L4Re::Util::Auto_cap;
40 registry->register_obj(this);
45 registry->unregister_obj(this);
49 Sw_icu::bind_irq(l4_msgtag_t tag, unsigned irqn, L4::Ipc::Snd_fpage const &/*irqc*/)
54 d_printf(DBG_ALL, "%s[%p]: bind_irq(%d, ...)\n", name(), this, irqn);
56 Irq_set::Iterator i = _irqs.find(irqn);
60 int err = i->bind(rcv_cap);
62 d_printf(DBG_ERR, "ERROR: binding irq %d, result is %d (%s)\n", irqn, err, l4sys_errtostr(err));
68 Sw_icu::unbind_irq(l4_msgtag_t tag, unsigned irqn, L4::Ipc::Snd_fpage const &/*irqc*/)
73 d_printf(DBG_ALL, "%s[%p]: unbind_irq(%d, ...)\n", name(), this, irqn);
75 Irq_set::Iterator i = _irqs.find(irqn);
79 // could check the validity of the cap too, however we just don't care
84 Sw_icu::set_mode(l4_msgtag_t /*tag*/, unsigned irqn, l4_umword_t mode)
86 Irq_set::Iterator i = _irqs.find(irqn);
90 return i->set_mode(mode);
94 Sw_icu::unmask_irq(l4_msgtag_t /*tag*/, unsigned irqn)
96 Irq_set::Iterator i = _irqs.find(irqn);
100 if (!i->unmask_via_icu())
108 Sw_icu::irqs_allocated(Resource const *r)
110 for (unsigned n = r->start(); n <= r->end(); ++n)
112 if (_irqs.find(n) == _irqs.end())
120 Sw_icu::add_irqs(Resource const *r)
122 for (unsigned n = r->start(); n <= r->end(); ++n)
124 if (_irqs.find(n) != _irqs.end())
127 Io_irq_pin *ri = Hw::Irqs::real_irq(n);
130 d_printf(DBG_ERR, "ERROR: No IRQ%d available.\n", n);
133 Sw_irq_pin *irq = new Sw_irq_pin(ri, n, r->flags());
140 Sw_icu::add_irq(unsigned n, unsigned flags, Io_irq_pin *be)
142 if (_irqs.find(n) == _irqs.end())
145 Sw_irq_pin *irq = new Sw_irq_pin(be, n, flags);
151 Sw_icu::alloc_irq(unsigned flags, Io_irq_pin *be)
154 for (i = 1; i < 1000; ++i)
155 if (_irqs.find(i) == _irqs.end())
161 Sw_irq_pin *irq = new Sw_irq_pin(be, i, flags);
168 Sw_icu::dispatch(l4_umword_t /*obj*/, L4::Ipc::Iostream &ios)
170 l4_umword_t op, irqn;
171 L4::Ipc::Snd_fpage irqc;
175 if (tag.label() != L4_PROTO_IRQ)
176 return -L4_EBADPROTO;
182 return bind_irq(tag, irqn, irqc);
184 case L4_ICU_OP_UNBIND:
186 return unbind_irq(tag, irqn, irqc);
188 case L4_ICU_OP_UNMASK:
190 unmask_irq(tag, irqn);
193 case L4_ICU_OP_SET_MODE:
197 return set_mode(tag, irqn, mode);
203 ii.features = ii.nr_msis = ii.nr_irqs = 0;
204 if (_irqs.rbegin() != _irqs.rend())
205 ii.nr_irqs = (*_irqs.rbegin()).irqn() + 1;
210 default: return -L4_ENOSYS;
215 Sw_icu::dispatch(l4_umword_t, l4_uint32_t func, L4::Ipc::Iostream &ios)
217 if (func != L4vbus_vicu_get_cap)
224 //static VBus_factory<Sw_icu> __vicu_factory("Vicu");
227 Sw_icu::Sw_irq_pin::trigger() const
229 return l4_error(_irq->trigger());
234 Sw_icu::Sw_irq_pin::l4_type() const
237 unsigned r = (m & S_irq_type_mask) / Resource::Irq_type_base;
242 Sw_icu::Sw_irq_pin::set_mode(l4_umword_t mode)
244 if (!(_state & S_allow_set_mode))
246 unsigned t = l4_type();
248 mode = (mode & L4_IRQ_F_MASK) | 1;
250 if (t != L4_IRQ_F_NONE
252 d_printf(DBG_WARN, "WARNING: Changing type of IRQ %d from %x to %lx prohibited\n",
257 return _master->set_mode(mode);
261 Sw_icu::Sw_irq_pin::allocate_master_irq()
263 assert (_master->shared());
264 Auto_cap<L4::Irq>::Cap lirq = chkcap(L4Re::Util::cap_alloc.alloc<L4::Irq>(),
265 "allocating IRQ capability");
266 // printf("IRQ mode = %x -> %x\n", type(), l4_type());
267 chksys(L4Re::Env::env()->factory()->create(lirq.get(), L4_PROTO_IRQ) << l4_umword_t(1), "allocating IRQ");
268 chksys(_master->bind(lirq.get(), l4_type()), "binding IRQ");
269 _master->irq(lirq.release());
270 _master->set_chained(true);
275 Sw_icu::Sw_irq_pin::bind(L4::Cap<void> rc)
279 if (_irq.get().validate(L4Re::This_task).label() > 0)
288 Auto_cap<L4::Irq>::Cap irq =
289 chkcap(L4Re::Util::cap_alloc.alloc<L4::Irq>(), "allocating IRQ capability");
291 irq.get().move(L4::cap_cast<L4::Irq>(rc));
293 if (_master->shared() && !_master->chained() && _master->sw_irqs() == 0)
295 allocate_master_irq();
296 assert (_master->chained());
299 if (!_master->chained())
301 // the first irq shall be attached to a hw irq
302 d_printf(DBG_DEBUG2, "IRQ %d -> client\nIRQ mode = %x -> %x\n",
303 irqn(), type(), l4_type());
304 int err = _master->bind(irq.get(), l4_type());
309 _master->irq(_irq.get());
310 _master->inc_sw_irqs();
313 _state |= S_unmask_via_icu;
315 d_printf(DBG_DEBUG2, " bound irq %u -> err=%d\n", irqn(), err);
319 d_printf(DBG_DEBUG2, "IRQ %d -> proxy -> %d clients\n", irqn(), _master->sw_irqs() + 1);
320 L4Re::chksys(_master->irq()->chain(l4_umword_t(_master), irq.get()), "attach");
322 _master->inc_sw_irqs();
328 Sw_icu::Sw_irq_pin::_unbind()
331 _master->dec_sw_irqs();
332 if (_master->sw_irqs() == 0)
334 if (_master->chained())
335 L4Re::Util::cap_alloc.free(_master->irq());
337 _master->irq(L4::Cap<L4::Irq>::Invalid);
338 _master->set_chained(false);
341 _irq = L4::Cap<L4::Irq>::Invalid;
348 Sw_icu::Sw_irq_pin::unbind()
353 if (!_master->sw_irqs())