summaryrefslogtreecommitdiffstats
path: root/usr.sbin/bhyve/ioapic.c
blob: b996214686320ce71c2125b7926a423f6b973c5a (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
/*-
 * Copyright (c) 2012 NetApp, Inc.
 * All rights reserved.
 *
 * 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 NETAPP, INC ``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 NETAPP, INC 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.
 *
 * $FreeBSD$
 */

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

#include <sys/types.h>

#include <x86/apicreg.h>
#include <machine/vmm.h>

#include <string.h>
#include <assert.h>
#include <stdbool.h>
#include <pthread.h>

#include <vmmapi.h>

#include "inout.h"
#include "mem.h"
#include "bhyverun.h"

#include <stdio.h>

static uint64_t ioapic_clearpend, ioapic_togglepend, ioapic_setpend;

#define	IOAPIC_PADDR	0xFEC00000

#define	IOREGSEL	0x00
#define	IOWIN		0x10

#define	REDIR_ENTRIES	16
#define	INTR_ASSERTED(ioapic, pin)	\
	((ioapic)->rtbl[(pin)].pinstate == true)

struct ioapic {
	int		inited;
	uint32_t	id;
	struct {
		uint64_t reg;
		bool     pinstate;
		bool     pending;
	} rtbl[REDIR_ENTRIES];

	uintptr_t	paddr;		/* gpa where the ioapic is mapped */
	uint32_t	ioregsel;
	struct memory_region *region;
	pthread_mutex_t	mtx;
};

static struct ioapic ioapics[1];	/* only a single ioapic for now */

static int ioapic_region_read(struct vmctx *vm, struct ioapic *ioapic,
    uintptr_t paddr, int size, uint64_t *data);
static int ioapic_region_write(struct vmctx *vm, struct ioapic *ioapic,
    uintptr_t paddr, int size, uint64_t data);
static int ioapic_region_handler(struct vmctx *vm, int vcpu, int dir,
    uintptr_t paddr, int size, uint64_t *val, void *arg1, long arg2);

static void
ioapic_set_pinstate(struct vmctx *ctx, int pin, bool newstate)
{
	int vector, apicid, vcpu;
	uint32_t low, high;
	struct ioapic *ioapic;
	
	ioapic = &ioapics[0];		/* assume a single ioapic */

	/* Nothing to do if interrupt pin has not changed state */
	if (ioapic->rtbl[pin].pinstate == newstate)
		return;

	ioapic->rtbl[pin].pinstate = newstate;	/* record it */

	/* Nothing to do if interrupt pin is deasserted */
	if (!INTR_ASSERTED(ioapic, pin))
		return;

	/*
	 * XXX
	 * We only deal with:
	 * - edge triggered interrupts
	 * - fixed delivery mode
	 *  Level-triggered sources will work so long as there is
	 * no sharing.
	 */
	low = ioapic->rtbl[pin].reg;
	high = ioapic->rtbl[pin].reg >> 32;
	if ((low & IOART_INTMASK) == IOART_INTMCLR &&
	    (low & IOART_DESTMOD) == IOART_DESTPHY &&
	    (low & IOART_DELMOD) == IOART_DELFIXED) {
		vector = low & IOART_INTVEC;
		apicid = high >> APIC_ID_SHIFT;
		if (apicid != 0xff) {
			/* unicast */
			vcpu = vm_apicid2vcpu(ctx, apicid);
			vm_lapic_irq(ctx, vcpu, vector);
		} else {
			/* broadcast */
			vcpu = 0;
			while (vcpu < guest_ncpus) {
				vm_lapic_irq(ctx, vcpu, vector);
				vcpu++;
			}
		}
	} else if ((low & IOART_INTMASK) != IOART_INTMCLR &&
		   low & IOART_TRGRLVL) {
		/*
		 * For level-triggered interrupts that have been
		 * masked, set the pending bit so that an interrupt
		 * will be generated on unmask and if the level is
		 * still asserted
		 */
		ioapic_setpend++;
		ioapic->rtbl[pin].pending = true;
	}
}

static void
ioapic_set_pinstate_locked(struct vmctx *ctx, int pin, bool newstate)
{
	struct ioapic *ioapic;

	if (pin < 0 || pin >= REDIR_ENTRIES)
		return;

	ioapic = &ioapics[0];

	pthread_mutex_lock(&ioapic->mtx);
	ioapic_set_pinstate(ctx, pin, newstate);
	pthread_mutex_unlock(&ioapic->mtx);
}

/*
 * External entry points require locking
 */
void
ioapic_deassert_pin(struct vmctx *ctx, int pin)
{
	ioapic_set_pinstate_locked(ctx, pin, false);
}

void
ioapic_assert_pin(struct vmctx *ctx, int pin)
{
	ioapic_set_pinstate_locked(ctx, pin, true);
}

void
ioapic_init(int which)
{
	struct mem_range memp;
	struct ioapic *ioapic;
	int error;
	int i;

	assert(which == 0);

	ioapic = &ioapics[which];
	assert(ioapic->inited == 0);

	bzero(ioapic, sizeof(struct ioapic));

	pthread_mutex_init(&ioapic->mtx, NULL);

	/* Initialize all redirection entries to mask all interrupts */
	for (i = 0; i < REDIR_ENTRIES; i++)
		ioapic->rtbl[i].reg = 0x0001000000010000UL;

	ioapic->paddr = IOAPIC_PADDR;

	/* Register emulated memory region */
	memp.name = "ioapic";
	memp.flags = MEM_F_RW;
	memp.handler = ioapic_region_handler;
	memp.arg1 = ioapic;
	memp.arg2 = which;
	memp.base = ioapic->paddr;
	memp.size = sizeof(struct IOAPIC);
	error = register_mem(&memp);

	assert (error == 0);

	ioapic->inited = 1;
}

static uint32_t
ioapic_read(struct ioapic *ioapic, uint32_t addr)
{
	int regnum, pin, rshift;

	assert(ioapic->inited);

	regnum = addr & 0xff;
	switch (regnum) {
	case IOAPIC_ID:
		return (ioapic->id);
		break;
	case IOAPIC_VER:
		return ((REDIR_ENTRIES << MAXREDIRSHIFT) | 0x11);
		break;
	case IOAPIC_ARB:
		return (ioapic->id);
		break;
	default:
		break;
	}

	/* redirection table entries */
	if (regnum >= IOAPIC_REDTBL &&
	    regnum < IOAPIC_REDTBL + REDIR_ENTRIES * 2) {
		pin = (regnum - IOAPIC_REDTBL) / 2;
		if ((regnum - IOAPIC_REDTBL) % 2)
			rshift = 32;
		else
			rshift = 0;

		return (ioapic->rtbl[pin].reg >> rshift);
	}

	return (0);
}

static void
ioapic_write(struct vmctx *vm, struct ioapic *ioapic, uint32_t addr,
    uint32_t data)
{
	int regnum, pin, lshift;

	assert(ioapic->inited);

	regnum = addr & 0xff;
	switch (regnum) {
	case IOAPIC_ID:
		ioapic->id = data & APIC_ID_MASK;
		break;
	case IOAPIC_VER:
	case IOAPIC_ARB:
		/* readonly */
		break;
	default:
		break;
	}

	/* redirection table entries */
	if (regnum >= IOAPIC_REDTBL &&
	    regnum < IOAPIC_REDTBL + REDIR_ENTRIES * 2) {
		pin = (regnum - IOAPIC_REDTBL) / 2;
		if ((regnum - IOAPIC_REDTBL) % 2)
			lshift = 32;
		else
			lshift = 0;

		ioapic->rtbl[pin].reg &= ~((uint64_t)0xffffffff << lshift);
		ioapic->rtbl[pin].reg |= ((uint64_t)data << lshift);
	
		if (ioapic->rtbl[pin].pending &&
		    ((ioapic->rtbl[pin].reg & IOART_INTMASK) ==
		         IOART_INTMCLR)) {
			ioapic->rtbl[pin].pending = false;
			ioapic_clearpend++;
			/*
			 * Inject the deferred level-triggered int if it is
			 * still asserted. Simulate by toggling the pin
			 * off and then on.
			 */
			if (ioapic->rtbl[pin].pinstate == true) {
				ioapic_togglepend++;
				ioapic_set_pinstate(vm, pin, false);
				ioapic_set_pinstate(vm, pin, true);
			}
		}
	}
}

static int
ioapic_region_read(struct vmctx *vm, struct ioapic *ioapic, uintptr_t paddr,
    int size, uint64_t *data)
{
	int offset;

	offset = paddr - ioapic->paddr;

	/*
	 * The IOAPIC specification allows 32-bit wide accesses to the
	 * IOREGSEL (offset 0) and IOWIN (offset 16) registers.
	 */
	if (size != 4 || (offset != IOREGSEL && offset != IOWIN)) {
#if 1
		printf("invalid access to ioapic%d: size %d, offset %d\n",
		       (int)(ioapic - ioapics), size, offset);
#endif
		*data = 0;
		return (0);
	}

	if (offset == IOREGSEL)
		*data = ioapic->ioregsel;
	else
		*data = ioapic_read(ioapic, ioapic->ioregsel);

	return (0);
}

static int
ioapic_region_write(struct vmctx *vm, struct ioapic *ioapic, uintptr_t paddr,
    int size, uint64_t data)
{
	int offset;

	offset = paddr - ioapic->paddr;

	/*
	 * The ioapic specification allows 32-bit wide accesses to the
	 * IOREGSEL (offset 0) and IOWIN (offset 16) registers.
	 */
	if (size != 4 || (offset != IOREGSEL && offset != IOWIN)) {
#if 1
		printf("invalid access to ioapic%d: size %d, offset %d\n",
		       (int)(ioapic - ioapics), size, offset);
#endif
		return (0);
	}

	if (offset == IOREGSEL)
		ioapic->ioregsel = data;
	else
		ioapic_write(vm, ioapic, ioapic->ioregsel, data);

	return (0);
}

static int
ioapic_region_handler(struct vmctx *vm, int vcpu, int dir, uintptr_t paddr,
    int size, uint64_t *val, void *arg1, long arg2)
{
	struct ioapic *ioapic;
	int which;

	ioapic = arg1;
	which = arg2;

	assert(ioapic == &ioapics[which]);

	pthread_mutex_lock(&ioapic->mtx);
	if (dir == MEM_F_READ)
		ioapic_region_read(vm, ioapic, paddr, size, val);
	else
		ioapic_region_write(vm, ioapic, paddr, size, *val);
	pthread_mutex_unlock(&ioapic->mtx);

	return (0);
}
OpenPOWER on IntegriCloud