summaryrefslogtreecommitdiffstats
path: root/sys/arch/powerpc64/dev/xive.c
blob: 5953b66b3aad1d4a3029654aef31bff5e59f9b54 (plain) (blame)
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
/*	$OpenBSD: xive.c,v 1.15 2020/10/10 17:05:55 kettenis Exp $	*/
/*
 * Copyright (c) 2020 Mark Kettenis <kettenis@openbsd.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/evcount.h>
#include <sys/malloc.h>
#include <sys/queue.h>

#include <machine/bus.h>
#include <machine/fdt.h>
#include <machine/opal.h>

#include <dev/ofw/openfirm.h>
#include <dev/ofw/fdt.h>

#define XIVE_NUM_PRIORITIES	8
#define XIVE_NUM_IRQS		1024

#define XIVE_EQ_SIZE		PAGE_SHIFT
#define XIVE_EQ_IDX_MASK	((1 << (PAGE_SHIFT - 2)) - 1)
#define XIVE_EQ_GEN_MASK	0x80000000

#define XIVE_TM_CPPR_HV		0x031

#define XIVE_TM_SPC_ACK_HV	0x830
#define  XIVE_TM_SPC_ACK_HE_MASK	0xc000
#define  XIVE_TM_SPC_ACK_HE_NONE	0x0000
#define  XIVE_TM_SPC_ACK_HE_PHYS	0x8000

#define XIVE_ESB_STORE_TRIGGER	0x000
#define XIVE_ESB_LOAD_EOI	0x000
#define XIVE_ESB_STORE_EOI	0x400
#define XIVE_ESB_SET_PQ_00	0xc00
#define XIVE_ESB_SET_PQ_01	0xd00
#define XIVE_ESB_SET_PQ_10	0xe00
#define XIVE_ESB_SET_PQ_11	0xf00

#define XIVE_ESB_VAL_P	0x2
#define XIVE_ESB_VAL_Q	0x1

static inline uint8_t
xive_prio(int ipl)
{
	return ((IPL_IPI - ipl) > 7 ? 0xff : IPL_IPI - ipl);
}

static inline int
xive_ipl(uint8_t prio)
{
	return (IPL_IPI - prio);
}

struct intrhand {
	TAILQ_ENTRY(intrhand)	ih_list;
	int			(*ih_func)(void *);
	void			*ih_arg;
	int			ih_ipl;
	int			ih_flags;
	uint32_t		ih_girq;
	struct evcount		ih_count;
	const char		*ih_name;

	bus_space_handle_t	ih_esb_eoi;
	bus_space_handle_t	ih_esb_trig;
	uint64_t		ih_xive_flags;
};

struct xive_eq {
	struct xive_dmamem	*eq_queue;
	uint32_t		eq_idx;
	uint32_t		eq_gen;
};

struct xive_softc {
	struct device		sc_dev;
	bus_space_tag_t		sc_iot;
	bus_space_handle_t	sc_ioh;
	bus_dma_tag_t		sc_dmat;

	struct intrhand		*sc_handler[XIVE_NUM_IRQS];
	struct xive_eq		sc_eq[MAXCPUS][XIVE_NUM_PRIORITIES];

	uint32_t		sc_page_size;
	uint32_t		sc_lirq;
};

struct xive_softc *xive_sc;

struct xive_dmamem {
	bus_dmamap_t		xdm_map;
	bus_dma_segment_t	xdm_seg;
	size_t			xdm_size;
	caddr_t			xdm_kva;
};

#define XIVE_DMA_MAP(_xdm)	((_xdm)->xdm_map)
#define XIVE_DMA_LEN(_xdm)	((_xdm)->xdm_size)
#define XIVE_DMA_DVA(_xdm)	((_xdm)->xdm_map->dm_segs[0].ds_addr)
#define XIVE_DMA_KVA(_xdm)	((void *)(_xdm)->xdm_kva)

struct xive_dmamem *xive_dmamem_alloc(bus_dma_tag_t, bus_size_t,
	    bus_size_t);
void	xive_dmamem_free(bus_dma_tag_t, struct xive_dmamem *);

static inline void
xive_write_1(struct xive_softc *sc, bus_size_t off, uint8_t val)
{
	bus_space_write_1(sc->sc_iot, sc->sc_ioh, off, val);
}

static inline uint16_t
xive_read_2(struct xive_softc *sc, bus_size_t off)
{
	return bus_space_read_2(sc->sc_iot, sc->sc_ioh, off);
}

static inline void
xive_unmask(struct xive_softc *sc, struct intrhand *ih)
{
	bus_space_read_8(sc->sc_iot, ih->ih_esb_eoi, XIVE_ESB_SET_PQ_00);
}

int	xive_match(struct device *, void *, void *);
void	xive_attach(struct device *, struct device *, void *);
int	xive_activate(struct device *, int);

struct cfattach	xive_ca = {
	sizeof (struct xive_softc), xive_match, xive_attach, NULL,
	xive_activate
};

struct cfdriver xive_cd = {
	NULL, "xive", DV_DULL
};

void	xive_hvi(struct trapframe *);
void 	*xive_intr_establish(uint32_t, int, int, struct cpu_info *,
	    int (*)(void *), void *, const char *);
void	xive_intr_send_ipi(void *);
void	xive_setipl(int);

int
xive_match(struct device *parent, void *match, void *aux)
{
	struct fdt_attach_args *faa = aux;

	return OF_is_compatible(faa->fa_node, "ibm,opal-xive-pe");
}

void
xive_attach(struct device *parent, struct device *self, void *aux)
{
	struct xive_softc *sc = (struct xive_softc *)self;
	struct fdt_attach_args *faa = aux;
	struct cpu_info *ci;
	CPU_INFO_ITERATOR cii;
	int64_t error;
	int i;

	if (faa->fa_nreg < 2) {
		printf(": no registers\n");
		return;
	}

	sc->sc_iot = faa->fa_iot;
	if (bus_space_map(sc->sc_iot, faa->fa_reg[1].addr,
	    faa->fa_reg[1].size, 0, &sc->sc_ioh)) {
		printf(": can't map registers\n");
		return;
	}

	sc->sc_dmat = faa->fa_dmat;

	sc->sc_page_size = OF_getpropint(faa->fa_node,
	    "ibm,xive-provision-page-size", 0);

	error = opal_xive_reset(OPAL_XIVE_MODE_EXPL);
	if (error != OPAL_SUCCESS)
		printf(": can't enable exploitation mode\n");

	printf("\n");

	CPU_INFO_FOREACH(cii, ci) {
		for (i = 0; i < XIVE_NUM_PRIORITIES; i++) {
			sc->sc_eq[ci->ci_cpuid][i].eq_queue =
			    xive_dmamem_alloc(sc->sc_dmat,
			    1 << XIVE_EQ_SIZE, 1 << XIVE_EQ_SIZE);
			if (sc->sc_eq[ci->ci_cpuid][i].eq_queue == NULL) {
				printf("%s: can't allocate event queue\n",
				    sc->sc_dev.dv_xname);
				return;
			}

			error = opal_xive_set_queue_info(ci->ci_pir, i,
			    XIVE_DMA_DVA(sc->sc_eq[ci->ci_cpuid][i].eq_queue),
			    XIVE_EQ_SIZE, OPAL_XIVE_EQ_ENABLED |
			    OPAL_XIVE_EQ_ALWAYS_NOTIFY);
			if (error != OPAL_SUCCESS) {
				printf("%s: can't enable event queue\n",
				    sc->sc_dev.dv_xname);
				return;
			}

			sc->sc_eq[ci->ci_cpuid][i].eq_gen = XIVE_EQ_GEN_MASK;
		}
	}

	/* There can be only one. */
	KASSERT(xive_sc == NULL);
	xive_sc = sc;

	_hvi = xive_hvi;
	_intr_establish = xive_intr_establish;
	_intr_send_ipi = xive_intr_send_ipi;
	_setipl = xive_setipl;

	/* Synchronize hardware state to software state. */
	xive_write_1(sc, XIVE_TM_CPPR_HV, xive_prio(curcpu()->ci_cpl));
	eieio();
}

int
xive_activate(struct device *self, int act)
{
	switch (act) {
	case DVACT_POWERDOWN:
		opal_xive_reset(OPAL_XIVE_MODE_EMU);
		break;
	}

	return 0;
}

void *
xive_intr_establish(uint32_t girq, int type, int level, struct cpu_info *ci,
    int (*func)(void *), void *arg, const char *name)
{
	struct xive_softc *sc = xive_sc;
	struct intrhand *ih;
	bus_space_handle_t eoi, trig;
	bus_size_t page_size;
	uint64_t flags, eoi_page, trig_page;
	uint32_t esb_shift, lirq;
	int64_t error;

	if (ci == NULL)
		ci = cpu_info_primary;

	/* Allocate a logical IRQ. */
	if (sc->sc_lirq >= XIVE_NUM_IRQS)
		return NULL;
	lirq = sc->sc_lirq++;

	error = opal_xive_get_irq_info(girq, opal_phys(&flags),
	    opal_phys(&eoi_page), opal_phys(&trig_page),
	    opal_phys(&esb_shift), NULL);
	if (error != OPAL_SUCCESS)
		return NULL;
	page_size = 1 << esb_shift;

	/* Map EOI page. */
	if (bus_space_map(sc->sc_iot, eoi_page, page_size, 0, &eoi))
		return NULL;

	/* Map trigger page. */
	if (trig_page == eoi_page)
		trig = eoi;
	else if (trig_page == 0)
		trig = 0;
	else if (bus_space_map(sc->sc_iot, trig_page, page_size, 0, &trig)) {
		bus_space_unmap(sc->sc_iot, trig, page_size);
		return NULL;
	}

	error = opal_xive_set_irq_config(girq, ci->ci_pir,
	    xive_prio(level & IPL_IRQMASK), lirq);
	if (error != OPAL_SUCCESS) {
		if (trig != eoi && trig != 0)
			bus_space_unmap(sc->sc_iot, trig, page_size);
		bus_space_unmap(sc->sc_iot, eoi, page_size);
		return NULL;
	}

	ih = malloc(sizeof(*ih), M_DEVBUF, M_WAITOK);
	ih->ih_func = func;
	ih->ih_arg = arg;
	ih->ih_ipl = level & IPL_IRQMASK;
	ih->ih_flags = level & IPL_FLAGMASK;
	ih->ih_girq = girq;
	ih->ih_name = name;
	ih->ih_esb_eoi = eoi;
	ih->ih_esb_trig = trig;
	ih->ih_xive_flags = flags;
	sc->sc_handler[lirq] = ih;

	if (name != NULL)
		evcount_attach(&ih->ih_count, name, &ih->ih_girq);

	xive_unmask(sc, ih);

	return ih;
}

void
xive_intr_send_ipi(void *cookie)
{
	struct xive_softc *sc = xive_sc;
	struct intrhand *ih = cookie;

	if (ih && ih->ih_esb_trig)
		bus_space_write_8(sc->sc_iot, ih->ih_esb_trig,
		    XIVE_ESB_STORE_TRIGGER, 0);
}

void
xive_eoi(struct xive_softc *sc, struct intrhand *ih)
{
	uint64_t eoi;

	if (ih->ih_xive_flags & OPAL_XIVE_IRQ_STORE_EOI) {
		bus_space_write_8(sc->sc_iot, ih->ih_esb_eoi,
		    XIVE_ESB_STORE_EOI, 0);
	} else if (ih->ih_xive_flags & OPAL_XIVE_IRQ_LSI) {
		eoi = bus_space_read_8(sc->sc_iot, ih->ih_esb_eoi,
		    XIVE_ESB_LOAD_EOI);
	} else {
		eoi = bus_space_read_8(sc->sc_iot, ih->ih_esb_eoi,
		    XIVE_ESB_SET_PQ_00);
		if ((eoi & XIVE_ESB_VAL_Q) && ih->ih_esb_trig != 0)
			bus_space_write_8(sc->sc_iot, ih->ih_esb_trig,
			    XIVE_ESB_STORE_TRIGGER, 0);
	}
}

void
xive_setipl(int new)
{
	struct xive_softc *sc = xive_sc;
	struct cpu_info *ci = curcpu();
	uint8_t oldprio = xive_prio(ci->ci_cpl);
	uint8_t newprio = xive_prio(new);
	u_long msr;

	msr = intr_disable();
	ci->ci_cpl = new;
	if (newprio != oldprio) {
		xive_write_1(sc, XIVE_TM_CPPR_HV, newprio);
		eieio();
	}
	intr_restore(msr);
}

void
xive_hvi(struct trapframe *frame)
{
	struct xive_softc *sc = xive_sc;
	struct cpu_info *ci = curcpu();
	struct intrhand *ih;
	struct xive_eq *eq;
	uint32_t *event;
	uint32_t lirq;
	int old, new;
	int handled;
	uint16_t ack, he;
	uint8_t cppr;

	old = ci->ci_cpl;

	while (1) {
		ack = xive_read_2(sc, XIVE_TM_SPC_ACK_HV);

		he = (ack & XIVE_TM_SPC_ACK_HE_MASK);
		if (he == XIVE_TM_SPC_ACK_HE_NONE)
			break;
		KASSERT(he == XIVE_TM_SPC_ACK_HE_PHYS);

		eieio();

		/* Synchronize software state to hardware state. */
		cppr = ack;
		new = xive_ipl(cppr);
		if (new <= old) {
			/*
			 * QEMU generates spurious interrupts.  It is
			 * unclear whether this can happen on real
			 * hardware as well.  We just ignore the
			 * interrupt, but we need to reset the CPPR
			 * register since we did accept the interrupt.
			 */
			goto spurious;
		}
		ci->ci_cpl = new;

		KASSERT(cppr < XIVE_NUM_PRIORITIES);
		eq = &sc->sc_eq[ci->ci_cpuid][cppr];
		event = XIVE_DMA_KVA(eq->eq_queue);
		while ((event[eq->eq_idx] & XIVE_EQ_GEN_MASK) == eq->eq_gen) {
			lirq = event[eq->eq_idx] & ~XIVE_EQ_GEN_MASK;
			KASSERT(lirq < XIVE_NUM_IRQS);
			ih = sc->sc_handler[lirq];
			if (ih != NULL) {
#ifdef MULTIPROCESSOR
				int need_lock;

				if (ih->ih_flags & IPL_MPSAFE)
					need_lock = 0;
				else
					need_lock = (ih->ih_ipl < IPL_SCHED);

				if (need_lock)
					KERNEL_LOCK();
#endif
				intr_enable();
				handled = ih->ih_func(ih->ih_arg);
				intr_disable();
				if (handled)
					ih->ih_count.ec_count++;
#ifdef MULTIPROCESSOR
				if (need_lock)
					KERNEL_UNLOCK();
#endif
				xive_eoi(sc, ih);
			}
			eq->eq_idx = (eq->eq_idx + 1) & XIVE_EQ_IDX_MASK;

			/* Toggle generation on wrap around. */
			if (eq->eq_idx == 0)
				eq->eq_gen ^= XIVE_EQ_GEN_MASK;
		}

		ci->ci_cpl = old;
	spurious:
		xive_write_1(sc, XIVE_TM_CPPR_HV, xive_prio(old));
		eieio();
	}
}

struct xive_dmamem *
xive_dmamem_alloc(bus_dma_tag_t dmat, bus_size_t size, bus_size_t align)
{
	struct xive_dmamem *xdm;
	int nsegs;

	xdm = malloc(sizeof(*xdm), M_DEVBUF, M_WAITOK | M_ZERO);
	xdm->xdm_size = size;

	if (bus_dmamap_create(dmat, size, 1, size, 0,
	    BUS_DMA_WAITOK | BUS_DMA_ALLOCNOW, &xdm->xdm_map) != 0)
		goto xdmfree;

	if (bus_dmamem_alloc(dmat, size, align, 0, &xdm->xdm_seg, 1,
	    &nsegs, BUS_DMA_WAITOK | BUS_DMA_ZERO) != 0)
		goto destroy;

	if (bus_dmamem_map(dmat, &xdm->xdm_seg, nsegs, size,
	    &xdm->xdm_kva, BUS_DMA_WAITOK | BUS_DMA_NOCACHE) != 0)
		goto free;

	if (bus_dmamap_load_raw(dmat, xdm->xdm_map, &xdm->xdm_seg,
	    nsegs, size, BUS_DMA_WAITOK) != 0)
		goto unmap;

	return xdm;

unmap:
	bus_dmamem_unmap(dmat, xdm->xdm_kva, size);
free:
	bus_dmamem_free(dmat, &xdm->xdm_seg, 1);
destroy:
	bus_dmamap_destroy(dmat, xdm->xdm_map);
xdmfree:
	free(xdm, M_DEVBUF, sizeof(*xdm));

	return NULL;
}

void
xive_dmamem_free(bus_dma_tag_t dmat, struct xive_dmamem *xdm)
{
	bus_dmamem_unmap(dmat, xdm->xdm_kva, xdm->xdm_size);
	bus_dmamem_free(dmat, &xdm->xdm_seg, 1);
	bus_dmamap_destroy(dmat, xdm->xdm_map);
	free(xdm, M_DEVBUF, sizeof(*xdm));
}