summaryrefslogtreecommitdiff
path: root/libirqhelp/irqhelp.c
blob: 985885b906a8e24c804b881b3756c57dc8186cf9 (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
/* Library providing helper functions for userspace irq handling.
   Copyright (C) 2022 Free Software Foundation, Inc.

   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, 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, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */

#include "irqhelp.h"

#include <sys/types.h>
#include <sys/queue.h>

#include <fcntl.h>
#include <inttypes.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <hurd.h>
#include <hurd/paths.h>
#include <device/notify.h>
#include <device/device.h>
#include "acpi_U.h"
#include <mach.h>
#include <stdbool.h>

#define IRQ_THREAD_PRIORITY	2
#define log_error(fmt...)	fprintf(stderr, ## fmt)

struct irq {
  void (*handler)(void *);
  void *context;
  int gsi;
  mach_port_t port;
  bool enabled;
  bool shutdown;
  pthread_mutex_t irqlock;
  pthread_cond_t irqcond;
};

static mach_port_t irqdev = MACH_PORT_NULL;
static mach_port_t acpidev = MACH_PORT_NULL;

static error_t
get_acpi(void)
{
  error_t err = 0;
  mach_port_t tryacpi, device_master;

  err = get_privileged_ports (0, &device_master);
  if (!err)
    {
      err = device_open (device_master, D_READ, "acpi", &tryacpi);
      mach_port_deallocate (mach_task_self (), device_master);
      if (!err)
	{
	  acpidev = tryacpi;
	  return 0;
	}
    }

  tryacpi = file_name_lookup (_SERVERS_ACPI, O_RDONLY, 0);
  if (tryacpi == MACH_PORT_NULL)
    return ENODEV;

  acpidev = tryacpi;
  return 0;
}

static error_t
get_irq(void)
{
  error_t err = 0;
  mach_port_t tryirq, device_master;

  err = get_privileged_ports (0, &device_master);
  if (err)
    return err;

  err = device_open (device_master, D_READ|D_WRITE, "irq", &tryirq);
  mach_port_deallocate (mach_task_self (), device_master);
  if (err)
    return err;

  irqdev = tryirq;
  return err;
}

static void
toggle_irq(struct irq *irq, bool on)
{
  pthread_mutex_lock (&irq->irqlock);
  irq->enabled = on;
  pthread_mutex_unlock (&irq->irqlock);

  if (on)
    pthread_cond_signal (&irq->irqcond);
}

error_t
irqhelp_init(void)
{
  static bool inited = false;
  error_t err;

  if (inited)
    {
      log_error("already inited\n");
      return 0;
    }

  err = get_irq();
  if (err)
    {
      log_error("cannot grab irq device\n");
      return err;
    }

  err = get_acpi();
  if (err)
    {
      log_error("cannot grab acpi device\n");
      return err;
    }

  inited = true;
  return 0;
}

void
irqhelp_disable_irq(struct irq *irq)
{
  if (!irq)
    { 
      log_error("cannot disable this irq\n");
      return;
    }

  toggle_irq(irq, false);
}

void
irqhelp_enable_irq(struct irq *irq)
{
  if (!irq)
    {
      log_error("cannot enable this irq\n");
      return;
    }

  toggle_irq(irq, true);
}

void *
irqhelp_server_loop(void *arg)
{
  struct irq *irq = (struct irq *)arg;
  mach_port_t master_host;
  mach_port_t pset, psetcntl;
  error_t err;

  if (!irq)
    {
      log_error("cannot start this irq thread\n");
      return NULL;
    }

  err = get_privileged_ports (&master_host, 0);
  if (err)
    {
      log_error("cannot get master_host port\n");
      return NULL;
    }

  err = thread_get_assignment (mach_thread_self (), &pset);
  if (err)
    goto fail;

  err = host_processor_set_priv (master_host, pset, &psetcntl);
  if (err)
    goto fail;

  thread_max_priority (mach_thread_self (), psetcntl, 0);
  err = thread_priority (mach_thread_self (), IRQ_THREAD_PRIORITY, 0);
  if (err)
    goto fail;

  mach_port_deallocate (mach_task_self (), master_host);

  int interrupt_demuxer (mach_msg_header_t *inp,
			 mach_msg_header_t *outp)
  {
    device_intr_notification_t *n = (device_intr_notification_t *) inp;

    ((mig_reply_header_t *) outp)->RetCode = MIG_NO_REPLY;
    if (n->intr_header.msgh_id != DEVICE_INTR_NOTIFY)
      {
	static bool printed0 = false;
	if (!printed0)
	  {
	    log_error("msg received is not an interrupt\n");
	    printed0 = true;
	  }
	return 0;
      }

    /* FIXME: id <-> gsi now has an indirection, assuming 1:1 */
    if (n->id != irq->gsi)
      {
	static bool printed1 = false;
	if (!printed1)
	  {
	    log_error("interrupt expected on irq %d arrived on irq %d\n", irq->gsi, n->id);
	    printed1 = true;
	  }
	return 0;
      }

    /* wait if irq disabled */
    pthread_mutex_lock (&irq->irqlock);
    while (!irq->enabled)
      pthread_cond_wait (&irq->irqcond, &irq->irqlock);
    pthread_mutex_unlock (&irq->irqlock);

    /* handle interrupt when not shutting down */
    if (!irq->shutdown)
      {
        irq->handler(irq->context);
        device_intr_ack (irqdev, irq->port, MACH_MSG_TYPE_MAKE_SEND);
      }
    return 1;
  }

  /* Server loop */
  mach_msg_server (interrupt_demuxer, 0, irq->port);
  goto done;

fail:
  log_error("failed to register irq %d\n", irq->gsi);

done:
  pthread_cond_destroy(&irq->irqcond);
  pthread_mutex_destroy(&irq->irqlock);
  free(irq);
  return NULL;
}

static struct irq *
interrupt_register(int gsi,
		   void (*handler)(void *),
		   void *context)
{
  error_t err;
  struct irq *irq = NULL;
  mach_port_t delivery_port;

  irq = malloc(sizeof(struct irq));
  if (!irq)
    {
      log_error("cannot malloc irq\n");
      return NULL;
    }

  irq->handler = handler;
  irq->context = context;
  irq->gsi = gsi;
  irq->enabled = true;  /* avoid deadlock by not requiring initial explicit enable */
  irq->shutdown = false;
  pthread_mutex_init (&irq->irqlock, NULL);
  pthread_cond_init (&irq->irqcond, NULL);
  
  err = mach_port_allocate (mach_task_self (), MACH_PORT_RIGHT_RECEIVE,
			    &delivery_port);
  if (err)
    {
      log_error("cannot allocate mach port\n");
      return NULL;
    }

  irq->port = delivery_port;

  err = device_intr_register(irqdev, irq->gsi,
			     0, irq->port,
			     MACH_MSG_TYPE_MAKE_SEND);
  if (err)
    {
      log_error("device_intr_register failed with %d\n", err);
      return NULL;
    }

  return irq;
}

struct irq *
irqhelp_install_interrupt_handler(int gsi,
				  int bus,
				  int dev,
				  int fun,
				  void (*handler)(void *),
				  void *context)
{
  error_t err;

  if (!handler)
    {
      log_error("no handler\n");
      return NULL;
    }

  if (gsi < 0)
    {
      if ((bus < 0) || (dev < 0) || (fun < 0))
	{
	  log_error("invalid b/d/f\n");
	  return NULL;
	}

      /* We need to call acpi translator to get gsi */
      err = acpi_get_pci_irq (acpidev, bus, dev, fun, &gsi);
      if (err)
	{
	  log_error("tried acpi to get pci gsi and failed for %02x:%02x.%d\n", bus, dev, fun);
	  return NULL;
	}
    }

  return interrupt_register(gsi, handler, context);
}

error_t
irqhelp_remove_interrupt_handler(struct irq *irq)
{
  if (!irq)
    {
      log_error("cannot deregister this irq\n");
      return ENODEV;
    }

  irq->shutdown = true;

  /* Turn port into dead name */
  mach_port_insert_right (mach_task_self (), irq->port, irq->port,
                          MACH_MSG_TYPE_MAKE_SEND);
  mach_port_mod_refs (mach_task_self (), irq->port,
                      MACH_PORT_RIGHT_RECEIVE, -1);
  return 0;
}