summaryrefslogtreecommitdiff
path: root/device/intr.c
blob: 5033d93f882b3d8e29fdb5318ff2957e1e8cfa77 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/*
 * Copyright (c) 2010-2016 Free Software Foundation.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <device/intr.h>
#include <device/ds_routines.h>
#include <ipc/ipc_space.h>
#include <kern/debug.h>
#include <kern/queue.h>
#include <kern/printf.h>
#include <mach/notify.h>
#include <machine/cpu.h>

#include "interrupt.h"

#ifndef MACH_XEN

/* The cache which holds our proxy memory objects.  */
static struct kmem_cache intr_entry_cache;

struct intr_entry
{
  ipc_port_t port;	/* We receive notifications on this port.  */
  queue_chain_t chain;
  ipc_port_t dest;
  int new_style;	/* version2 notifications?.  */
  int line;
  /* The number of interrupts occur since last run of intr_thread. */
  int interrupts;
};
typedef struct intr_entry *intr_entry_t;

static queue_head_t intr_queue;

/* This function can only be used in the interrupt handler. */
boolean_t
queue_intr (struct intr_entry *e)
{
  unsigned long flags;

  cpu_intr_save (&flags);
  e->interrupts++;
  cpu_intr_restore (flags);

  thread_wakeup ((event_t) &intr_thread);
  return TRUE;
}

/* insert an interrupt entry in the queue.
 * This entry exists in the queue until
 * the corresponding interrupt port is removed.*/
kern_return_t
insert_intr_entry (int line,
		   ipc_port_t dest,
		   int new_style,	/* version2 notifications? */
		   struct intr_entry **entry)
{
  kern_return_t err = 0;
  unsigned long flags;
  struct intr_entry *e, *new;
  int free = 0;
  ipc_port_t dnnotify;
  ipc_port_request_index_t dnindex;

  new = (struct intr_entry *) kmem_cache_alloc (&intr_entry_cache);
  if (new == NULL)
    return D_NO_MEMORY;

  /* Allocate port, keeping a reference for it.  */
  new->port = ipc_port_alloc_kernel ();
  if (new->port == IP_NULL)
    {
      kmem_cache_free (&intr_entry_cache, (vm_offset_t) new);
      return KERN_RESOURCE_SHORTAGE;
    }

  /* Associate the port with the object.  */
  ipc_kobject_set (new->port, (ipc_kobject_t) new, IKOT_INTR_ENTRY);

  new->line = line;
  new->dest = dest;
  new->new_style = new_style;
  new->interrupts = 0;

  /* Register a dead-name notification so that we are notified if the
     userspace handler dies.  */
  dnnotify = ipc_port_make_sonce (new->port);
  ip_lock (dest);
  /* We use a bogus port name.  We don't need it to distinguish the
     notifications because we register just one per object.  */
 retry:
  err = ipc_port_dnrequest (dest, (mach_port_t) 1, dnnotify, &dnindex);
  if (err)
    {
      err = ipc_port_dngrow (dest);
      /* dest is unlocked */
      if (err != KERN_SUCCESS)
	{
	  ipc_port_release_sonce (dnnotify);
	  kmem_cache_free (&intr_entry_cache, (vm_offset_t) new);
	  return err;
	}
      ip_lock (dest);
      goto retry;
    }
  /* dest is locked.  dnindex is only valid until we unlock it and we
     might decide to cancel.  */

  /* check whether the intr entry has been in the queue. */
  cpu_intr_save (&flags);
  queue_iterate (&intr_queue, e, struct intr_entry *, chain)
    if (e->dest == dest && e->line == line)
      {
	printf ("the interrupt entry for line %d and port %p "
		"has already been inserted before.\n",
		line, dest);
	free = 1;
	err = D_ALREADY_OPEN;
	goto out;
      }
  queue_enter (&intr_queue, new, struct intr_entry *, chain);
 out:
  cpu_intr_restore (flags);
  if (free)
    {
      ipc_port_dncancel (new->dest, (mach_port_t) 1, dnindex);
      ip_unlock (new->dest);
      ipc_port_release_sonce (dnnotify);
      ipc_port_dealloc_kernel (new->port);
      ipc_port_release_send (new->dest);
      kmem_cache_free (&intr_entry_cache, (vm_offset_t) new);
    }
  else
    ip_unlock (new->dest);
  *entry = new;
  return err;
}

/* Lookup a intr_entry object by its port.  */
static intr_entry_t
intr_entry_port_lookup (ipc_port_t port)
{
  struct intr_entry *entry;

  if (!IP_VALID(port))
    return 0;

  ip_lock (port);
  if (ip_active (port) && (ip_kotype (port) == IKOT_INTR_ENTRY))
    entry = (struct intr_entry *) port->ip_kobject;
  else
    entry = 0;
  ip_unlock (port);
  return entry;
}

/* Process a dead-name notification for a userspace interrupt handler
   notification port.  */
boolean_t
intr_entry_notify (mach_msg_header_t *msg)
{
  struct intr_entry *entry;

  if (msg->msgh_id == MACH_NOTIFY_NO_SENDERS)
    {
      extern void enable_irq (unsigned int irq_nr);
      mach_no_senders_notification_t *ns;

      ns = (mach_no_senders_notification_t *) msg;
      entry = intr_entry_port_lookup
	((ipc_port_t) ns->not_header.msgh_remote_port);
      assert (entry);

      enable_irq (entry->line);	/* entry is not locked here */
      return TRUE;
    }
  else if (msg->msgh_id == MACH_NOTIFY_DEAD_NAME)
    {
      int line;
      mach_dead_name_notification_t *dn;
      unsigned long flags;

      dn = (mach_dead_name_notification_t *) msg;
      entry = intr_entry_port_lookup
	((ipc_port_t) dn->not_header.msgh_remote_port);
      assert (entry);

      cpu_intr_save (&flags);
      line = entry->line;
      assert (!queue_empty (&intr_queue));
      queue_remove (&intr_queue, entry, struct intr_entry *, chain);
      remove_user_intr_handler (entry->line, entry);
      cpu_intr_restore (flags);

      ipc_port_dealloc_kernel (entry->port);
      ipc_port_release_send (entry->dest);
      kmem_cache_free (&intr_entry_cache, (vm_offset_t) entry);

      printf ("irq handler %d: userspace handler died\n", line);
      return TRUE;
    }

  printf ("intr_entry_notify: strange notification %d\n",
	  msg->msgh_id);
  return FALSE;
}


mach_intr_notification_t mach_intr_notification_template;

static void
init_mach_intr_notification (mach_intr_notification_t *n)
{
  mach_msg_header_t *m = &n->intr_header;
  mach_msg_type_t *t = &n->intr_type;

  m->msgh_bits = MACH_MSGH_BITS (MACH_MSG_TYPE_PORT_SEND,
				 MACH_MSG_TYPE_PORT_SEND);
  m->msgh_size = sizeof *n;
  m->msgh_seqno = INTR_NOTIFY_MSGH_SEQNO;
  m->msgh_local_port = MACH_PORT_NULL;
  m->msgh_remote_port = MACH_PORT_NULL;
  m->msgh_id = MACH_INTR_NOTIFY;

  t->msgt_name = MACH_MSG_TYPE_INTEGER_32;
  t->msgt_size = 32;
  t->msgt_number = 1;
  t->msgt_inline = TRUE;
  t->msgt_longform = FALSE;
  t->msgt_deallocate = FALSE;
  t->msgt_unused = 0;
}

static boolean_t
deliver_intr (int line, ipc_port_t dest_port, ipc_port_t interrupt_port)
{
  ipc_kmsg_t kmsg;
  mach_intr_notification_t *n;
  ipc_port_t sright, sonce, old;
  mach_port_t dest = (mach_port_t) dest_port;

  if (dest == MACH_PORT_NULL)
    return FALSE;

  kmsg = ikm_alloc(sizeof *n);
  if (kmsg == IKM_NULL)
    return FALSE;

  ikm_init(kmsg, sizeof *n);
  n = (mach_intr_notification_t *) &kmsg->ikm_header;
  *n = mach_intr_notification_template;

  /* Arrange no-senders notification.  */
  sright = ipc_port_make_send (interrupt_port);
  sonce = ipc_port_make_sonce (interrupt_port);
  ip_lock (interrupt_port);
  ipc_port_nsrequest (interrupt_port, interrupt_port->ip_mscount,
                      sonce, &old);
  if (old != IP_NULL)
    ipc_port_release_sonce (old);

  n->intr_header.msgh_remote_port = dest;
  n->intr_header.msgh_local_port = (mach_port_t) sright;
  n->line = line;

  ipc_port_copy_send (dest_port);
  ipc_mqueue_send_always(kmsg);

  return TRUE;
}

mach_intr_notification2_t mach_intr_notification2_template;

static void
init_mach_intr_notification2 (mach_intr_notification2_t *n)
{
  mach_msg_header_t *m = &n->intr_header;
  mach_msg_type_t *t = &n->line_type;

  m->msgh_bits =
    MACH_MSGH_BITS_COMPLEX | MACH_MSGH_BITS(MACH_MSG_TYPE_PORT_SEND, 0);
  m->msgh_size = sizeof *n;
  m->msgh_seqno = INTR_NOTIFY_MSGH_SEQNO;
  m->msgh_local_port = MACH_PORT_NULL;
  m->msgh_remote_port = MACH_PORT_NULL;
  m->msgh_id = MACH_INTR_NOTIFY2;

  t->msgt_name = MACH_MSG_TYPE_INTEGER_32;
  t->msgt_size = 32;
  t->msgt_number = 1;
  t->msgt_inline = TRUE;
  t->msgt_longform = FALSE;
  t->msgt_deallocate = FALSE;
  t->msgt_unused = 0;

  t = &n->count_type;
  t->msgt_name = MACH_MSG_TYPE_INTEGER_32;
  t->msgt_size = 32;
  t->msgt_number = 1;
  t->msgt_inline = TRUE;
  t->msgt_longform = FALSE;
  t->msgt_deallocate = FALSE;
  t->msgt_unused = 0;

  t = &n->port_type;
  t->msgt_name = MACH_MSG_TYPE_MOVE_SEND;
  t->msgt_size = 32;
  t->msgt_number = 1;
  t->msgt_inline = TRUE;
  t->msgt_longform = FALSE;
  t->msgt_deallocate = FALSE;
  t->msgt_unused = 0;
}

static boolean_t
deliver_intr_notification2 (ipc_port_t notification_port,
			    int line, int count,
			    ipc_port_t interrupt_port)
{
  ipc_kmsg_t kmsg;
  ipc_port_t sright, sonce, old;
  mach_intr_notification2_t *n;

  assert (notification_port);
  assert (interrupt_port);

  kmsg = ikm_alloc(sizeof *n);
  if (kmsg == IKM_NULL)
    return FALSE;

  ikm_init(kmsg, sizeof *n);
  n = (mach_intr_notification2_t *) &kmsg->ikm_header;
  *n = mach_intr_notification2_template;

  /* Arrange no-senders notification.  */
  sright = ipc_port_make_send (interrupt_port);
  sonce = ipc_port_make_sonce (interrupt_port);
  ip_lock (interrupt_port);
  ipc_port_nsrequest (interrupt_port, interrupt_port->ip_mscount,
                      sonce, &old);
  if (old != IP_NULL)
    ipc_port_release_sonce (old);

  n->intr_header.msgh_remote_port = (mach_port_t) notification_port;
  n->line = line;
  n->count = count;
  n->interrupt_port = (mach_port_t) sright;

  ipc_port_copy_send (notification_port);
  ipc_mqueue_send_always(kmsg);

  return TRUE;
}

void
intr_thread ()
{
  struct intr_entry *e;
  int line;
  ipc_port_t dest;
  queue_init (&intr_queue);
  init_mach_intr_notification (&mach_intr_notification_template);
  init_mach_intr_notification2 (&mach_intr_notification2_template);

  kmem_cache_init (&intr_entry_cache, "intr_entry",
		   sizeof (struct intr_entry), 0, NULL, 0);

  for (;;)
    {
      unsigned long flags;
      assert_wait ((event_t) &intr_thread, FALSE);
      cpu_intr_save (&flags);

    restart:
      queue_iterate (&intr_queue, e, struct intr_entry *, chain)
	if (e->interrupts)
	  {
            int ok;
            ipc_port_t interrupt_port = e->port;
            int count = e->interrupts;
	    line = e->line;
	    dest = e->dest;
	    if (e->new_style)
	      {
		e->interrupts = 0;

		cpu_intr_restore (flags);
		ok = deliver_intr_notification2 (dest, line, count, interrupt_port);
                printf ("new: count? %d ok? %d\n", count, ok);
		cpu_intr_save (&flags);
	      }
	    else
	      {
		e->interrupts--;

		cpu_intr_restore (flags);
		deliver_intr (line, dest, interrupt_port);
		cpu_intr_save (&flags);

                assert (e->interrupts == 0);
	      }

	    /* We cannot assume that e still exists at this point
	       because we released the lock.  Hence we restart the
	       iteration.  */
	    goto restart;
	  }

      cpu_intr_restore (flags);
      thread_block (thread_no_continuation);
    }
}

#else	/* MACH_XEN */

boolean_t
intr_entry_notify (mach_msg_header_t *msg)
{
  panic ("not reached");
}

#endif	/* MACH_XEN */