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"
18 #include <l4/re/util/cap_alloc>
19 #include <l4/re/namespace>
21 #include <l4/re/error_helper>
24 #include <l4/sys/debugger.h>
25 #include <l4/sys/kdebug.h>
31 using L4Re::Util::Auto_cap;
35 enum { MAX_HW_IRQS = 256 };
37 Kernel_irq_pin *Sw_icu::_real_irqs[MAX_HW_IRQS];
40 Sw_icu::real_irq(unsigned n)
42 if (n >= sizeof(_real_irqs)/sizeof(_real_irqs[0]))
46 _real_irqs[n] = new Kernel_irq_pin(n);
55 registry->register_obj(this);
60 registry->unregister_obj(this);
64 Sw_icu::bind_irq(l4_msgtag_t tag, unsigned irqn, L4::Snd_fpage const &/*irqc*/)
69 d_printf(DBG_ALL, "%s[%p]: bind_irq(%d, ...)\n", name(), this, irqn);
71 Irq_set::Iterator i = _irqs.find(irqn);
75 int err = i->bind(rcv_cap);
77 d_printf(DBG_ERR, "ERROR: binding irq %d, result is %d (%s)\n", irqn, err, l4sys_errtostr(err));
83 Sw_icu::unbind_irq(l4_msgtag_t tag, unsigned irqn, L4::Snd_fpage const &/*irqc*/)
88 d_printf(DBG_ALL, "%s[%p]: unbind_irq(%d, ...)\n", name(), this, irqn);
90 Irq_set::Iterator i = _irqs.find(irqn);
94 // could check the validity of the cap too, however we just don't care
99 Sw_icu::set_mode(l4_msgtag_t /*tag*/, unsigned irqn, l4_umword_t mode)
101 Irq_set::Iterator i = _irqs.find(irqn);
102 if (i == _irqs.end())
105 if (i->l4_type() != (mode & 0x6))
107 d_printf(DBG_WARN, "WARNING: Changing type of IRQ %d from %x to %lx prohibited\n",
108 irqn, i->l4_type(), mode);
115 Sw_icu::unmask_irq(l4_msgtag_t /*tag*/, unsigned irqn)
117 Irq_set::Iterator i = _irqs.find(irqn);
118 if (i == _irqs.end())
121 if (!i->unmask_via_icu())
129 Sw_icu::irqs_allocated(Adr_resource const *r)
131 for (unsigned n = r->_data().start(); n <= r->_data().end(); ++n)
133 if (_irqs.find(n) == _irqs.end())
141 Sw_icu::add_irqs(Adr_resource const *r)
143 for (unsigned n = r->_data().start(); n <= r->_data().end(); ++n)
145 if (_irqs.find(n) != _irqs.end())
148 Sw_irq_pin *irq = new Sw_irq_pin(real_irq(n), n, r->flags());
155 Sw_icu::add_irq(unsigned n, unsigned flags, Io_irq_pin *be)
157 if (_irqs.find(n) == _irqs.end())
160 Sw_irq_pin *irq = new Sw_irq_pin(be, n, flags);
166 Sw_icu::alloc_irq(unsigned flags, Io_irq_pin *be)
169 for (i = 1; i < 1000; ++i)
170 if (_irqs.find(i) == _irqs.end())
176 Sw_irq_pin *irq = new Sw_irq_pin(be, i, flags);
183 Sw_icu::dispatch(l4_umword_t /*obj*/, L4::Ipc_iostream &ios)
185 l4_umword_t op, irqn;
188 ios >> tag >> op >> irqn;
190 if (tag.label() != L4_PROTO_IRQ)
191 return -L4_EBADPROTO;
197 return bind_irq(tag, irqn, irqc);
199 case L4_ICU_OP_UNBIND:
201 return unbind_irq(tag, irqn, irqc);
203 case L4_ICU_OP_UNMASK:
204 unmask_irq(tag, irqn);
207 case L4_ICU_OP_SET_MODE:
211 return set_mode(tag, irqn, mode);
214 default: return -L4_ENOSYS;
219 Sw_icu::dispatch(l4_umword_t, l4_uint32_t func, L4::Ipc_iostream &ios)
221 if (func != L4vbus_vicu_get_cap)
228 //static VBus_factory<Sw_icu> __vicu_factory("Vicu");
231 Sw_icu::Sw_irq_pin::trigger() const
233 return l4_error(_irq->trigger());
238 Sw_icu::Sw_irq_pin::l4_type() const
241 unsigned r = (m & S_irq_type_mask) / Adr_resource::Irq_info_factor;
246 Sw_icu::Sw_irq_pin::allocate_master_irq()
248 assert (_master->shared());
249 Auto_cap<L4::Irq>::Cap lirq = chkcap(L4Re::Util::cap_alloc.alloc<L4::Irq>(),
250 "allocating IRQ capability");
251 // printf("IRQ mode = %x -> %x\n", type(), l4_type());
252 chksys(L4Re::Env::env()->factory()->create(lirq.get(), L4_PROTO_IRQ) << l4_umword_t(1), "allocating IRQ");
253 chksys(_master->bind(lirq.get(), l4_type()), "binding IRQ");
254 _master->irq(lirq.release());
255 _master->set_chained(true);
260 Sw_icu::Sw_irq_pin::bind(L4::Cap<void> rc)
264 if (_irq.get().validate(L4Re::This_task).label() > 0)
273 Auto_cap<L4::Irq>::Cap irq =
274 chkcap(L4Re::Util::cap_alloc.alloc<L4::Irq>(), "allocating IRQ capability");
276 irq.get().move(L4::cap_cast<L4::Irq>(rc));
278 if (_master->shared() && !_master->chained() && _master->sw_irqs() == 0)
280 allocate_master_irq();
281 assert (_master->chained());
284 if (!_master->chained())
286 // the first irq shall be attached to a hw irq
287 d_printf(DBG_DEBUG2, "IRQ %d -> client\nIRQ mode = %x -> %x\n",
288 irqn(), type(), l4_type());
289 int err = _master->bind(irq.get(), l4_type());
294 _master->irq(_irq.get());
295 _master->inc_sw_irqs();
298 _state |= S_unmask_via_icu;
300 d_printf(DBG_DEBUG2, " bound irq %u -> err=%d\n", irqn(), err);
304 d_printf(DBG_DEBUG2, "IRQ %d -> proxy -> %d clients\n", irqn(), _master->sw_irqs() + 1);
305 L4Re::chksys(_master->irq()->chain(l4_umword_t(_master), irq.get()), "attach");
307 _master->inc_sw_irqs();
313 Sw_icu::Sw_irq_pin::_unbind()
316 _master->dec_sw_irqs();
317 if (_master->sw_irqs() == 0)
319 if (_master->chained())
320 L4Re::Util::cap_alloc.free(_master->irq());
322 _master->irq(L4::Cap<L4::Irq>::Invalid);
323 _master->set_chained(false);
326 _irq = L4::Cap<L4::Irq>::Invalid;
333 Sw_icu::Sw_irq_pin::unbind()
338 if (!_master->sw_irqs())