summaryrefslogtreecommitdiffstats
path: root/sys/mips/beri/beri_pic.c
blob: f13f144bc9915031f88e78ffb154a40e96c42b1b (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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
/*-
 * Copyright (c) 2013 SRI International
 * All rights reserved.
 *
 * This software was developed by SRI International and the University of
 * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
 * ("CTSRD"), as part of the DARPA CRASH research programme.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/lock.h>
#include <sys/malloc.h>
#include <sys/module.h>
#include <sys/mutex.h>
#include <sys/systm.h>
#include <sys/bus.h>

#include <machine/bus.h>
#include <machine/intr_machdep.h>

#include <dev/ofw/ofw_bus.h>
#include <dev/ofw/ofw_bus_subr.h>

#include <dev/fdt/fdt_common.h>

#include "fdt_ic_if.h"

struct beripic_softc;

static uint64_t	bp_read_cfg(struct beripic_softc *, int);
static void	bp_write_cfg(struct beripic_softc *, int, uint64_t);
static void	bp_detach_resources(device_t);
static char	*bp_strconfig(uint64_t, char *, size_t);
static void	bp_config_source(device_t, int, int, u_long, u_long);
#ifdef __mips__
static void	bp_set_counter_name(device_t, device_t, int);
#endif

static int	beripic_fdt_probe(device_t);
static int	beripic_fdt_attach(device_t);

static int	beripic_activate_intr(device_t, struct resource *);
static struct resource *
		beripic_alloc_intr(device_t, device_t, int *, u_long, u_int);
static int	beripic_config_intr(device_t, int,  enum intr_trigger,
		    enum intr_polarity);
static int	beripic_release_intr(device_t, struct resource *);
static int	beripic_setup_intr(device_t, device_t, struct resource *,
		    int, driver_filter_t *, driver_intr_t *, void *, void **);
static int	beripic_teardown_intr(device_t, device_t, struct resource *,
		    void *);

static int	beripic_filter(void *);
static void	beripic_intr(void *);

#define	BP_MAX_HARD_IRQS	6
#define	BP_FIRST_SOFT		64

struct beripic_softc {
	device_t		bp_dev;
	struct resource		*bp_cfg_res;
	struct resource		*bp_read_res;
	struct resource		*bp_set_res;
	struct resource		*bp_clear_res;
	int			bp_cfg_rid;
	int			bp_read_rid;
	int			bp_set_rid;
	int			bp_clear_rid;
	bus_space_tag_t		bp_cfg_bst;
	bus_space_tag_t		bp_read_bst;
	bus_space_tag_t		bp_set_bst;
	bus_space_tag_t		bp_clear_bst;
	bus_space_handle_t	bp_cfg_bsh;
	bus_space_handle_t	bp_read_bsh;
	bus_space_handle_t	bp_set_bsh;
	bus_space_handle_t	bp_clear_bsh;

	struct resource		*bp_irqs[BP_MAX_HARD_IRQS];
	int			bp_irq_rids[BP_MAX_HARD_IRQS];
	int			bp_nirqs;
	int			bp_next_irq;
	int			bp_next_tid;

	int			bp_nthreads;

	int			bp_nhard;
	int			bp_nsoft;
	int			bp_nsrcs;
	struct rman		bp_src_rman;

#ifdef __mips__
	mips_intrcnt_t		*bp_counters;
#endif

	struct mtx		bp_cfgmtx;
};

struct beripic_intr_arg {
	driver_filter_t		*filter;
	driver_intr_t		*intr;
	void			*arg;
	struct resource		*irq;
#ifdef __mips__
	mips_intrcnt_t		counter;
#endif
};

struct beripic_cookie {
	struct beripic_intr_arg	*bpia;
	struct resource		*hirq;
	void			*cookie;
};

#define	BP_CFG_MASK_E		0x80000000ull
#define	BP_CFG_SHIFT_E		31
#define	BP_CFG_MASK_TID		0x7FFFFF00ull	/* Depends on CPU */
#define	BP_CFG_SHIFT_TID	8
#define	BP_CFG_MASK_IRQ		0x0000000Full
#define BP_CFG_SHIFT_IRQ	0
#define	BP_CFG_VALID		(BP_CFG_MASK_E|BP_CFG_MASK_TID|BP_CFG_MASK_IRQ)
#define	BP_CFG_RESERVED		~BP_CFG_VALID

#define	BP_CFG_ENABLED(cfg)	(((cfg) & BP_CFG_MASK_E) >> BP_CFG_SHIFT_E)
#define	BP_CFG_TID(cfg)		(((cfg) & BP_CFG_MASK_TID) >> BP_CFG_SHIFT_TID)
#define	BP_CFG_IRQ(cfg)		(((cfg) & BP_CFG_MASK_IRQ) >> BP_CFG_SHIFT_IRQ)

MALLOC_DEFINE(M_BERIPIC, "beripic", "beripic memory");

static uint64_t
bp_read_cfg(struct beripic_softc *sc, int irq)
{
	
	KASSERT((irq >= 0 && irq < sc->bp_nsrcs),
	    ("IRQ of of range %d (0-%d)", irq, sc->bp_nsrcs - 1));
	return (bus_space_read_8(sc->bp_cfg_bst, sc->bp_cfg_bsh, irq * 8));
}

static void
bp_write_cfg(struct beripic_softc *sc, int irq, uint64_t config)
{
	
	KASSERT((irq >= 0 && irq < sc->bp_nsrcs),
	    ("IRQ of of range %d (0-%d)", irq, sc->bp_nsrcs - 1));
	bus_space_write_8(sc->bp_cfg_bst, sc->bp_cfg_bsh, irq * 8, config);
}

static void
bp_detach_resources(device_t dev)
{
	struct beripic_softc *sc;
	int i;

	sc = device_get_softc(dev);

	if (sc->bp_cfg_res != NULL) {
		bus_release_resource(dev, SYS_RES_MEMORY, sc->bp_cfg_rid,
		    sc->bp_cfg_res);
		sc->bp_cfg_res = NULL;
	}
	if (sc->bp_read_res != NULL) {
		bus_release_resource(dev, SYS_RES_MEMORY, sc->bp_read_rid,
		    sc->bp_read_res);
		sc->bp_read_res = NULL;
	}
	if (sc->bp_set_res != NULL) {
		bus_release_resource(dev, SYS_RES_MEMORY, sc->bp_set_rid,
		    sc->bp_set_res);
		sc->bp_set_res = NULL;
	}
	if (sc->bp_clear_res != NULL) {
		bus_release_resource(dev, SYS_RES_MEMORY, sc->bp_clear_rid,
		    sc->bp_clear_res);
		sc->bp_clear_res = NULL;
	}
	for (i = sc->bp_nirqs - 1; i >= 0; i--) {
		bus_release_resource(dev, SYS_RES_IRQ, sc->bp_irq_rids[i],
		    sc->bp_irqs[i]);
	}
	sc->bp_nirqs = 0;
}

static char *
bp_strconfig(uint64_t config, char *configstr, size_t len)
{
	
	if (snprintf(configstr, len, "%s tid: %llu hardintr %llu",
	    BP_CFG_ENABLED(config) ? "enabled" : "disabled",
	    BP_CFG_TID(config), BP_CFG_IRQ(config)) > len - 1)
		return (NULL);
	return (configstr);
}

static void
bp_config_source(device_t ic, int src, int enable, u_long tid, u_long irq)
{
	struct beripic_softc *sc;
	uint64_t config;

	sc = device_get_softc(ic);

	config = 0;
	config |= enable << BP_CFG_SHIFT_E;
	config |= tid << BP_CFG_SHIFT_TID;
	config |= irq << BP_CFG_SHIFT_IRQ;

	bp_write_cfg(sc, src, config);
}

#ifdef __mips__
static void
bp_set_counter_name(device_t ic, device_t child, int src)
{
	struct beripic_softc *sc;
	char name[MAXCOMLEN + 1];

	sc = device_get_softc(ic);

	if (snprintf(name, sizeof(name), "bp%dsrc%d%s%s%s",
	    device_get_unit(ic), src, src < sc->bp_nhard ? "" : "s",
	    child == NULL ? "" : " ",
	    child == NULL ? " " : device_get_nameunit(child)) >= sizeof(name))
		name[sizeof(name) - 2] = '+';
	
	mips_intrcnt_setname(sc->bp_counters[src], name);
}
#endif

static int
beripic_fdt_probe(device_t dev)
{

	if (!ofw_bus_is_compatible(dev, "sri-cambridge,beri-pic"))
		return (ENXIO);
		
	device_set_desc(dev, "BERI Programmable Interrupt Controller");
	return (BUS_PROBE_DEFAULT);
}

static int
beripic_fdt_attach(device_t dev)
{
	char configstr[64];
	struct beripic_softc *sc;
	struct fdt_ic *fic;
	pcell_t nhard, nsoft;
	phandle_t ph;
	int error, i, src;
	uint64_t config;

	sc = device_get_softc(dev);
	sc->bp_dev = dev;

	mtx_init(&sc->bp_cfgmtx, "beripic config lock", NULL, MTX_DEF);

	/*
	 * FDT lists CONFIG, IP_READ, IP_SET, and IP_CLEAR registers as
	 * seperate memory regions in that order.
	 */
	sc->bp_cfg_rid = 0;
	sc->bp_cfg_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
	    &sc->bp_cfg_rid, RF_ACTIVE);
	if (sc->bp_cfg_res == NULL) {
		device_printf(dev, "failed to map config memory");
		error = ENXIO;
		goto err;
	}
	if (bootverbose)
		device_printf(sc->bp_dev, "config region at mem %p-%p\n",
		    (void *)rman_get_start(sc->bp_cfg_res),
		    (void *)(rman_get_start(sc->bp_cfg_res) +
		    rman_get_size(sc->bp_cfg_res)));

	sc->bp_read_rid = 1;
	sc->bp_read_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
	    &sc->bp_read_rid, RF_ACTIVE);
	if (sc->bp_read_res == NULL) {
		device_printf(dev, "failed to map IP read memory");
		error = ENXIO;
		goto err;
	}
	if (bootverbose)
		device_printf(sc->bp_dev, "IP read region at mem %p-%p\n",
		    (void *)rman_get_start(sc->bp_read_res),
		    (void *)(rman_get_start(sc->bp_read_res) +
		    rman_get_size(sc->bp_read_res)));

	sc->bp_set_rid = 2;
	sc->bp_set_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
	    &sc->bp_set_rid, RF_ACTIVE);
	if (sc->bp_set_res == NULL) {
		device_printf(dev, "failed to map IP read memory");
		error = ENXIO;
		goto err;
	}
	if (bootverbose)
		device_printf(sc->bp_dev, "IP set region at mem %p-%p\n",
		    (void *)rman_get_start(sc->bp_set_res),
		    (void *)(rman_get_start(sc->bp_set_res) +
		    rman_get_size(sc->bp_set_res)));

	sc->bp_clear_rid = 3;
	sc->bp_clear_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
	    &sc->bp_clear_rid, RF_ACTIVE);
	if (sc->bp_clear_res == NULL) {
		device_printf(dev, "failed to map IP read memory");
		error = ENXIO;
		goto err;
	}
	if (bootverbose)
		device_printf(sc->bp_dev, "IP clear region at mem %p-%p\n",
		    (void *)rman_get_start(sc->bp_clear_res),
		    (void *)(rman_get_start(sc->bp_clear_res) +
		    rman_get_size(sc->bp_clear_res)));

	i = 0;
	for (i = 0; i < BP_MAX_HARD_IRQS; i++) {
		sc->bp_irq_rids[i] = i;
		sc->bp_irqs[i] = bus_alloc_resource_any(dev, SYS_RES_IRQ,
		    &sc->bp_irq_rids[i], RF_ACTIVE | RF_SHAREABLE);
		if (sc->bp_irqs[i] == NULL)
			break;
	}
	if (i == 0) {
		device_printf(dev, "failed to allocate any parent IRQs!");
		error = ENXIO;
		goto err;
	}
	sc->bp_nirqs = i;

	ph = ofw_bus_gen_get_node(device_get_parent(dev), dev);

#ifndef SMP
	sc->bp_nthreads = 1;
#else
	sc->bp_nthreads = 1;
	/* XXX: get nthreads from cpu(s) somehow */
#endif

	if (OF_getprop(ph, "hard-interrupt-sources", &nhard, sizeof(nhard))
	    <= 0) {
		device_printf(dev, "failed to get number of hard sources");
		error = ENXIO;
		goto err;
	}
	if (OF_getprop(ph, "soft-interrupt-sources", &nsoft, sizeof(nsoft))
	    <= 0) {
		device_printf(dev, "failed to get number of soft sources");
		error = ENXIO;
		goto err;
	}

	sc->bp_nhard = nhard;
	sc->bp_nsoft = nsoft;
	sc->bp_nsrcs = sc->bp_nhard + sc->bp_nsoft;
	/* XXX: should deal with gap between hard and soft */
	KASSERT(sc->bp_nhard <= BP_FIRST_SOFT,
	    ("too many hard sources"));
	KASSERT(rman_get_size(sc->bp_cfg_res) / 8 == sc->bp_nsrcs,
	    ("config space size does not match sources"));
	KASSERT(sc->bp_nhard % 64 == 0,
	    ("Non-multiple of 64 intr counts not supported"));
	KASSERT(sc->bp_nsoft % 64 == 0,
	    ("Non-multiple of 64 intr counts not supported"));
	if (bootverbose)
		device_printf(dev, "%d hard and %d soft sources\n",
		    sc->bp_nhard, sc->bp_nsoft);

#ifdef __mips__
	sc->bp_counters = malloc(sizeof(*sc->bp_counters) * sc->bp_nsrcs,
	    M_BERIPIC, M_WAITOK|M_ZERO);
	for (i = 0; i < sc->bp_nsrcs; i++) {
		sc->bp_counters[i] = mips_intrcnt_create("");
		bp_set_counter_name(dev, NULL, i);
	}
#endif
	
	sc->bp_src_rman.rm_start = 0;
	sc->bp_src_rman.rm_end = sc->bp_nsrcs - 1;
	sc->bp_src_rman.rm_type = RMAN_ARRAY;
	sc->bp_src_rman.rm_descr = "Interrupt source";
	if (rman_init(&(sc->bp_src_rman)) != 0 ||
	    rman_manage_region(&(sc->bp_src_rman), 0, sc->bp_nsrcs - 1) != 0) {
		device_printf(dev, "Failed to set up sources rman");
		error = ENXIO;
		goto err;
	}

	sc->bp_cfg_bst = rman_get_bustag(sc->bp_cfg_res);
	sc->bp_cfg_bsh = rman_get_bushandle(sc->bp_cfg_res);
	sc->bp_read_bst = rman_get_bustag(sc->bp_read_res);
	sc->bp_read_bsh = rman_get_bushandle(sc->bp_read_res);
	sc->bp_set_bst = rman_get_bustag(sc->bp_set_res);
	sc->bp_set_bsh = rman_get_bushandle(sc->bp_set_res);
	sc->bp_clear_bst = rman_get_bustag(sc->bp_clear_res);
	sc->bp_clear_bsh = rman_get_bushandle(sc->bp_clear_res);

	for (src = 0; src < sc->bp_nsrcs; src++) {
		config = bp_read_cfg(sc, src);
		if (config == 0)
			continue;

		if (bootverbose) {
			device_printf(dev, "initial config: src %d: %s\n", src,
			    bp_strconfig(config, configstr, sizeof(configstr)));
			if (config & BP_CFG_RESERVED)
				device_printf(dev,
				    "reserved bits not 0: 0x%016jx\n",
				    (uintmax_t) config);
		}

		bp_config_source(dev, src, 0, 0, 0);
	}

	fic = malloc(sizeof(*fic), M_BERIPIC, M_WAITOK|M_ZERO);
	fic->iph = ph;
	fic->dev = dev;
	SLIST_INSERT_HEAD(&fdt_ic_list_head, fic, fdt_ics);

	return (0);
err:
	bp_detach_resources(dev);

	return (error);
}

static struct resource *
beripic_alloc_intr(device_t ic, device_t child, int *rid, u_long irq,
    u_int flags)
{
	struct beripic_softc *sc;
	struct resource *rv;

	sc = device_get_softc(ic);

	rv = rman_reserve_resource(&(sc->bp_src_rman), irq, irq, 1, flags,
	    child);
	if (rv == NULL)
		 printf("%s: could not reserve source interrupt for %s\n",
		     __func__, device_get_nameunit(child));
	rman_set_rid(rv, *rid);

	if ((flags & RF_ACTIVE) &&
	    beripic_activate_intr(ic, rv) != 0) {
		printf("%s: could not activate interrupt\n", __func__);
		rman_release_resource(rv);
		return (NULL);
	}

	return (rv);
}

static int
beripic_release_intr(device_t ic, struct resource *r)
{
	
	return (rman_release_resource(r));
}

static int
beripic_activate_intr(device_t ic, struct resource *r)
{
	
	return (rman_activate_resource(r));
}

static int
beripic_deactivate_intr(device_t ic, struct resource *r)
{
	
	return (rman_deactivate_resource(r));
}

static int
beripic_config_intr(device_t dev, int irq, enum intr_trigger trig,
   enum intr_polarity pol)
{

	if (trig != INTR_TRIGGER_CONFORM || pol != INTR_POLARITY_CONFORM)
		return (EINVAL);

	return (0);
}

static int
beripic_setup_intr(device_t ic, device_t child, struct resource *irq,
    int flags, driver_filter_t *filter, driver_intr_t *intr, void *arg, 
    void **cookiep)
{
	struct beripic_softc *sc;
	struct beripic_intr_arg *bpia;
	struct beripic_cookie *bpc;
	int error;
	u_long hirq, src, tid;

	sc = device_get_softc(ic);

	src = rman_get_start(irq);

	KASSERT(src < sc->bp_nsrcs, ("source (%lu) out of range 0-%d",
	     src, sc->bp_nsrcs - 1));

	bpia = malloc(sizeof(*bpia), M_BERIPIC, M_WAITOK|M_ZERO);
	bpia->filter = filter;
	bpia->intr = intr;
	bpia->arg = arg;
	bpia->irq = irq;
#ifdef __mips__
	bpia->counter = sc->bp_counters[src];
	bp_set_counter_name(ic, child, src);
#endif

	bpc = malloc(sizeof(*bpc), M_BERIPIC, M_WAITOK|M_ZERO);
	bpc->bpia = bpia;

	mtx_lock(&(sc->bp_cfgmtx));
	bpc->hirq = sc->bp_irqs[sc->bp_next_irq];
	hirq = rman_get_start(bpc->hirq);
	tid = sc->bp_next_tid;

	error = BUS_SETUP_INTR(device_get_parent(ic), ic, bpc->hirq, flags,
	    beripic_filter, intr == NULL ? NULL : beripic_intr, bpia,
	    &(bpc->cookie));
	if (error != 0)
		goto err;

#ifdef NOTYET
#ifdef SMP
	/* XXX: bind ithread to cpu */
	sc->bp_next_tid++;
	if (sc->bp_next_tid >= sc->bp_nthreads)
		sc->bp_next_tid = 0;
#endif
#endif
	if (sc->bp_next_tid == 0) {
		sc->bp_next_irq++;
		if (sc->bp_next_irq >= sc->bp_nirqs)
			sc->bp_next_irq = 0;
	}
	mtx_unlock(&(sc->bp_cfgmtx));

	*cookiep = bpc;

	bp_config_source(ic, rman_get_start(irq), 1, tid, hirq);

	return (0);
err:
	free(bpc, M_BERIPIC);
	free(bpia, M_BERIPIC);

	return (error);
}

static int
beripic_teardown_intr(device_t dev, device_t child, struct resource *irq,
    void *cookie)
{
	struct beripic_cookie *bpc;
	int error;

	bpc = cookie;

	bp_config_source(dev, rman_get_start(irq), 0, 0, 0);

	free(bpc->bpia, M_BERIPIC);

	error = BUS_TEARDOWN_INTR(device_get_parent(dev), dev, bpc->hirq,
	    bpc->cookie);

	free(bpc, M_BERIPIC);

	return (error);
}

static int
beripic_filter(void *arg)
{
	struct beripic_intr_arg *bpic;

	bpic = arg;

#ifdef __mips__
	mips_intrcnt_inc(bpic->counter);
#endif

	/* XXX: Add a check that our source is high */

	if (bpic->filter == NULL)
		return (FILTER_SCHEDULE_THREAD);

	return (bpic->filter(bpic->arg));
}

static void
beripic_intr(void *arg)
{
	struct beripic_intr_arg *bpic;

	bpic = arg;

	KASSERT(bpic->intr != NULL,
	    ("%s installed, but no child intr", __func__));

	bpic->intr(bpic->arg);
}

#ifdef SMP
static void
beripic_setup_ipi(device_t ic, u_int tid, u_int ipi_irq)
{
	
	bp_config_source(ic, BP_FIRST_SOFT + tid, 1, tid, ipi_irq);
}

static void
beripic_send_ipi(device_t ic, u_int tid)
{
	struct beripic_softc *sc;
	uint64_t bit;

	sc = device_get_softc(ic);

	KASSERT(tid < sc->bp_nsoft, ("tid (%d) too large\n", tid));

	bit = 1ULL << (tid % 64);
	bus_space_write_8(sc->bp_set_bst, sc->bp_set_bsh, 
	    (BP_FIRST_SOFT / 8) + (tid / 64), bit);
}

static void
beripic_clear_ipi(device_t ic, u_int tid)
{
	struct beripic_softc *sc;
	uint64_t bit;

	sc = device_get_softc(ic);

	KASSERT(tid < sc->bp_nsoft, ("tid (%d) to large\n", tid));

	bit = 1ULL << (tid % 64);
	bus_space_write_8(sc->bp_clear_bst, sc->bp_clear_bsh, 
	    (BP_FIRST_SOFT / 8) + (tid / 64), bit);
}
#endif

devclass_t	beripic_devclass;

static device_method_t beripic_fdt_methods[] = {
	/* Device interface */
	DEVMETHOD(device_probe,		beripic_fdt_probe),
	DEVMETHOD(device_attach,	beripic_fdt_attach),

	DEVMETHOD(fdt_ic_activate_intr,	beripic_activate_intr),
	DEVMETHOD(fdt_ic_alloc_intr,	beripic_alloc_intr),
	DEVMETHOD(fdt_ic_config_intr,	beripic_config_intr),
	DEVMETHOD(fdt_ic_deactivate_intr, beripic_deactivate_intr),
	DEVMETHOD(fdt_ic_release_intr,	beripic_release_intr),
	DEVMETHOD(fdt_ic_setup_intr,	beripic_setup_intr),
	DEVMETHOD(fdt_ic_teardown_intr,	beripic_teardown_intr),

#ifdef SMP
	DEVMETHOD(fdt_ic_setup_ipi,	beripic_setup_ipi),
	DEVMETHOD(fdt_ic_clear_ipi,	beripic_clear_ipi),
	DEVMETHOD(fdt_ic_send_ipi,	beripic_send_ipi),
#endif

	{ 0, 0 },
};

static driver_t beripic_fdt_driver = {
	"beripic",
	beripic_fdt_methods,
	sizeof(struct beripic_softc)
};

DRIVER_MODULE(beripic, simplebus, beripic_fdt_driver, beripic_devclass, 0, 0);
OpenPOWER on IntegriCloud