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>
29 #include <pthread-l4.h>
33 using L4Re::Util::Auto_cap;
37 enum { MAX_HW_IRQS = 256 };
39 Kernel_irq_pin *Sw_icu::_real_irqs[MAX_HW_IRQS];
42 Sw_icu::real_irq(unsigned n)
44 if (n >= sizeof(_real_irqs)/sizeof(_real_irqs[0]))
48 _real_irqs[n] = new Kernel_irq_pin(n);
57 registry->register_obj(this);
62 registry->unregister_obj(this);
66 Sw_icu::bind_irq(l4_msgtag_t tag, unsigned irqn, L4::Snd_fpage const &/*irqc*/)
71 d_printf(DBG_ALL, "%s[%p]: bind_irq(%d, ...)\n", name(), this, irqn);
73 Irq_set::Iterator i = _irqs.find(irqn);
77 int err = i->bind(rcv_cap);
79 d_printf(DBG_ERR, "ERROR: binding irq %d, result is %d (%s)\n", irqn, err, l4sys_errtostr(err));
85 Sw_icu::unbind_irq(l4_msgtag_t tag, unsigned irqn, L4::Snd_fpage const &/*irqc*/)
90 d_printf(DBG_ALL, "%s[%p]: unbind_irq(%d, ...)\n", name(), this, irqn);
92 Irq_set::Iterator i = _irqs.find(irqn);
96 // could check the validity of the cap too, however we just don't care
101 Sw_icu::set_mode(l4_msgtag_t /*tag*/, unsigned irqn, l4_umword_t mode)
103 Irq_set::Iterator i = _irqs.find(irqn);
104 if (i == _irqs.end())
107 if (i->l4_type() != (mode & 0x6))
109 d_printf(DBG_WARN, "WARNING: Changing type of IRQ %d from %x to %lx prohibited\n",
110 irqn, i->l4_type(), mode);
117 Sw_icu::unmask_irq(l4_msgtag_t /*tag*/, unsigned irqn)
119 Irq_set::Iterator i = _irqs.find(irqn);
120 if (i == _irqs.end())
123 if (!i->unmask_via_icu())
131 Sw_icu::irqs_allocated(Adr_resource const *r)
133 for (unsigned n = r->_data().start(); n <= r->_data().end(); ++n)
135 if (_irqs.find(n) == _irqs.end())
143 Sw_icu::add_irqs(Adr_resource const *r)
145 for (unsigned n = r->_data().start(); n <= r->_data().end(); ++n)
147 if (_irqs.find(n) != _irqs.end())
150 Sw_irq_pin *irq = new Sw_irq_pin(real_irq(n), n, r->flags());
157 Sw_icu::add_irq(unsigned n, unsigned flags, Io_irq_pin *be)
159 if (_irqs.find(n) == _irqs.end())
162 Sw_irq_pin *irq = new Sw_irq_pin(be, n, flags);
168 Sw_icu::alloc_irq(unsigned flags, Io_irq_pin *be)
171 for (i = 1; i < 1000; ++i)
172 if (_irqs.find(i) == _irqs.end())
178 Sw_irq_pin *irq = new Sw_irq_pin(be, i, flags);
185 Sw_icu::dispatch(l4_umword_t /*obj*/, L4::Ipc_iostream &ios)
187 l4_umword_t op, irqn;
190 ios >> tag >> op >> irqn;
192 if (tag.label() != L4_PROTO_IRQ)
193 return -L4_EBADPROTO;
199 return bind_irq(tag, irqn, irqc);
201 case L4_ICU_OP_UNBIND:
203 return unbind_irq(tag, irqn, irqc);
205 case L4_ICU_OP_UNMASK:
206 unmask_irq(tag, irqn);
209 case L4_ICU_OP_SET_MODE:
213 return set_mode(tag, irqn, mode);
216 default: return -L4_ENOSYS;
221 Sw_icu::dispatch(l4_umword_t, l4_uint32_t func, L4::Ipc_iostream &ios)
223 if (func != L4vbus_vicu_get_cap)
230 //static VBus_factory<Sw_icu> __vicu_factory("Vicu");
233 Sw_icu::Sw_irq_pin::trigger() const
235 return l4_error(_irq->trigger());
240 Sw_icu::Sw_irq_pin::l4_type() const
243 unsigned r = (m & S_irq_type_mask) / Adr_resource::Irq_info_factor;
248 Sw_icu::Sw_irq_pin::allocate_master_irq()
250 assert (_master->shared());
251 Auto_cap<L4::Irq>::Cap lirq = chkcap(L4Re::Util::cap_alloc.alloc<L4::Irq>(),
252 "allocating IRQ capability");
253 // printf("IRQ mode = %x -> %x\n", type(), l4_type());
254 chksys(L4Re::Env::env()->factory()->create(lirq.get(), L4_PROTO_IRQ) << l4_umword_t(1), "allocating IRQ");
255 chksys(_master->bind(lirq.get(), l4_type()), "binding IRQ");
256 _master->irq(lirq.release());
257 _master->set_chained(true);
262 Sw_icu::Sw_irq_pin::bind(L4::Cap<void> rc)
266 if (_irq.get().validate(L4Re::This_task).label() > 0)
275 Auto_cap<L4::Irq>::Cap irq =
276 chkcap(L4Re::Util::cap_alloc.alloc<L4::Irq>(), "allocating IRQ capability");
278 irq.get().move(L4::cap_cast<L4::Irq>(rc));
280 if (_master->shared() && !_master->chained() && _master->sw_irqs() == 0)
282 allocate_master_irq();
283 assert (_master->chained());
286 if (!_master->chained())
288 // the first irq shall be attached to a hw irq
289 d_printf(DBG_DEBUG2, "IRQ %d -> client\nIRQ mode = %x -> %x\n",
290 irqn(), type(), l4_type());
291 int err = _master->bind(irq.get(), l4_type());
296 _master->irq(_irq.get());
297 _master->inc_sw_irqs();
300 _state |= S_unmask_via_icu;
302 d_printf(DBG_DEBUG2, " bound irq %u -> err=%d\n", irqn(), err);
306 d_printf(DBG_DEBUG2, "IRQ %d -> proxy -> %d clients\n", irqn(), _master->sw_irqs() + 1);
307 L4Re::chksys(_master->irq()->chain(l4_umword_t(_master), irq.get()), "attach");
309 _master->inc_sw_irqs();
315 Sw_icu::Sw_irq_pin::_unbind()
318 _master->dec_sw_irqs();
319 if (_master->sw_irqs() == 0)
321 if (_master->chained())
322 L4Re::Util::cap_alloc.free(_master->irq());
324 _master->irq(L4::Cap<L4::Irq>::Invalid);
325 _master->set_chained(false);
328 _irq = L4::Cap<L4::Irq>::Invalid;
335 Sw_icu::Sw_irq_pin::unbind()
340 if (!_master->sw_irqs())