xref: /freebsd/usr.sbin/bhyve/pci_emul.c (revision ab0b9f6b3073e6c4d1dfbf07444d7db67a189a96)
1 /*-
2  * Copyright (c) 2011 NetApp, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/linker_set.h>
34 #include <sys/errno.h>
35 
36 #include <ctype.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <strings.h>
41 #include <assert.h>
42 #include <stdbool.h>
43 
44 #include <machine/vmm.h>
45 #include <vmmapi.h>
46 
47 #include "bhyverun.h"
48 #include "inout.h"
49 #include "legacy_irq.h"
50 #include "mem.h"
51 #include "pci_emul.h"
52 
53 #define CONF1_ADDR_PORT    0x0cf8
54 #define CONF1_DATA_PORT    0x0cfc
55 
56 #define CONF1_ENABLE	   0x80000000ul
57 
58 #define	CFGWRITE(pi,off,val,b)						\
59 do {									\
60 	if ((b) == 1) {							\
61 		pci_set_cfgdata8((pi),(off),(val));			\
62 	} else if ((b) == 2) {						\
63 		pci_set_cfgdata16((pi),(off),(val));			\
64 	} else {							\
65 		pci_set_cfgdata32((pi),(off),(val));			\
66 	}								\
67 } while (0)
68 
69 #define MAXSLOTS	(PCI_SLOTMAX + 1)
70 #define	MAXFUNCS	(PCI_FUNCMAX + 1)
71 
72 static struct slotinfo {
73 	char	*si_name;
74 	char	*si_param;
75 	struct pci_devinst *si_devi;
76 	int	si_legacy;
77 } pci_slotinfo[MAXSLOTS][MAXFUNCS];
78 
79 SET_DECLARE(pci_devemu_set, struct pci_devemu);
80 
81 static uint64_t pci_emul_iobase;
82 static uint64_t pci_emul_membase32;
83 static uint64_t pci_emul_membase64;
84 
85 #define	PCI_EMUL_IOBASE		0x2000
86 #define	PCI_EMUL_IOLIMIT	0x10000
87 
88 #define	PCI_EMUL_MEMLIMIT32	0xE0000000		/* 3.5GB */
89 
90 #define	PCI_EMUL_MEMBASE64	0xD000000000UL
91 #define	PCI_EMUL_MEMLIMIT64	0xFD00000000UL
92 
93 static struct pci_devemu *pci_emul_finddev(char *name);
94 
95 static int pci_emul_devices;
96 
97 /*
98  * I/O access
99  */
100 
101 /*
102  * Slot options are in the form:
103  *
104  *  <slot>[:<func>],<emul>[,<config>]
105  *
106  *  slot is 0..31
107  *  func is 0..7
108  *  emul is a string describing the type of PCI device e.g. virtio-net
109  *  config is an optional string, depending on the device, that can be
110  *  used for configuration.
111  *   Examples are:
112  *     1,virtio-net,tap0
113  *     3:0,dummy
114  */
115 static void
116 pci_parse_slot_usage(char *aopt)
117 {
118 
119 	fprintf(stderr, "Invalid PCI slot info field \"%s\"\n", aopt);
120 }
121 
122 int
123 pci_parse_slot(char *opt, int legacy)
124 {
125 	char *slot, *func, *emul, *config;
126 	char *str, *cpy;
127 	int error, snum, fnum;
128 
129 	error = -1;
130 	str = cpy = strdup(opt);
131 
132         slot = strsep(&str, ",");
133         func = NULL;
134         if (strchr(slot, ':') != NULL) {
135 		func = cpy;
136 		(void) strsep(&func, ":");
137         }
138 
139 	emul = strsep(&str, ",");
140 	config = str;
141 
142 	if (emul == NULL) {
143 		pci_parse_slot_usage(opt);
144 		goto done;
145 	}
146 
147 	snum = atoi(slot);
148 	fnum = func ? atoi(func) : 0;
149 
150 	if (snum < 0 || snum >= MAXSLOTS || fnum < 0 || fnum >= MAXFUNCS) {
151 		pci_parse_slot_usage(opt);
152 		goto done;
153 	}
154 
155 	if (pci_slotinfo[snum][fnum].si_name != NULL) {
156 		fprintf(stderr, "pci slot %d:%d already occupied!\n",
157 			snum, fnum);
158 		goto done;
159 	}
160 
161 	if (pci_emul_finddev(emul) == NULL) {
162 		fprintf(stderr, "pci slot %d:%d: unknown device \"%s\"\n",
163 			snum, fnum, emul);
164 		goto done;
165 	}
166 
167 	error = 0;
168 	pci_slotinfo[snum][fnum].si_name = emul;
169 	pci_slotinfo[snum][fnum].si_param = config;
170 	pci_slotinfo[snum][fnum].si_legacy = legacy;
171 
172 done:
173 	if (error)
174 		free(cpy);
175 
176 	return (error);
177 }
178 
179 static int
180 pci_valid_pba_offset(struct pci_devinst *pi, uint64_t offset)
181 {
182 
183 	if (offset < pi->pi_msix.pba_offset)
184 		return (0);
185 
186 	if (offset >= pi->pi_msix.pba_offset + pi->pi_msix.pba_size) {
187 		return (0);
188 	}
189 
190 	return (1);
191 }
192 
193 int
194 pci_emul_msix_twrite(struct pci_devinst *pi, uint64_t offset, int size,
195 		     uint64_t value)
196 {
197 	int msix_entry_offset;
198 	int tab_index;
199 	char *dest;
200 
201 	/* support only 4 or 8 byte writes */
202 	if (size != 4 && size != 8)
203 		return (-1);
204 
205 	/*
206 	 * Return if table index is beyond what device supports
207 	 */
208 	tab_index = offset / MSIX_TABLE_ENTRY_SIZE;
209 	if (tab_index >= pi->pi_msix.table_count)
210 		return (-1);
211 
212 	msix_entry_offset = offset % MSIX_TABLE_ENTRY_SIZE;
213 
214 	/* support only aligned writes */
215 	if ((msix_entry_offset % size) != 0)
216 		return (-1);
217 
218 	dest = (char *)(pi->pi_msix.table + tab_index);
219 	dest += msix_entry_offset;
220 
221 	if (size == 4)
222 		*((uint32_t *)dest) = value;
223 	else
224 		*((uint64_t *)dest) = value;
225 
226 	return (0);
227 }
228 
229 uint64_t
230 pci_emul_msix_tread(struct pci_devinst *pi, uint64_t offset, int size)
231 {
232 	char *dest;
233 	int msix_entry_offset;
234 	int tab_index;
235 	uint64_t retval = ~0;
236 
237 	/*
238 	 * The PCI standard only allows 4 and 8 byte accesses to the MSI-X
239 	 * table but we also allow 1 byte access to accomodate reads from
240 	 * ddb.
241 	 */
242 	if (size != 1 && size != 4 && size != 8)
243 		return (retval);
244 
245 	msix_entry_offset = offset % MSIX_TABLE_ENTRY_SIZE;
246 
247 	/* support only aligned reads */
248 	if ((msix_entry_offset % size) != 0) {
249 		return (retval);
250 	}
251 
252 	tab_index = offset / MSIX_TABLE_ENTRY_SIZE;
253 
254 	if (tab_index < pi->pi_msix.table_count) {
255 		/* valid MSI-X Table access */
256 		dest = (char *)(pi->pi_msix.table + tab_index);
257 		dest += msix_entry_offset;
258 
259 		if (size == 1)
260 			retval = *((uint8_t *)dest);
261 		else if (size == 4)
262 			retval = *((uint32_t *)dest);
263 		else
264 			retval = *((uint64_t *)dest);
265 	} else if (pci_valid_pba_offset(pi, offset)) {
266 		/* return 0 for PBA access */
267 		retval = 0;
268 	}
269 
270 	return (retval);
271 }
272 
273 int
274 pci_msix_table_bar(struct pci_devinst *pi)
275 {
276 
277 	if (pi->pi_msix.table != NULL)
278 		return (pi->pi_msix.table_bar);
279 	else
280 		return (-1);
281 }
282 
283 int
284 pci_msix_pba_bar(struct pci_devinst *pi)
285 {
286 
287 	if (pi->pi_msix.table != NULL)
288 		return (pi->pi_msix.pba_bar);
289 	else
290 		return (-1);
291 }
292 
293 static int
294 pci_emul_io_handler(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
295 		    uint32_t *eax, void *arg)
296 {
297 	struct pci_devinst *pdi = arg;
298 	struct pci_devemu *pe = pdi->pi_d;
299 	uint64_t offset;
300 	int i;
301 
302 	for (i = 0; i <= PCI_BARMAX; i++) {
303 		if (pdi->pi_bar[i].type == PCIBAR_IO &&
304 		    port >= pdi->pi_bar[i].addr &&
305 		    port + bytes <= pdi->pi_bar[i].addr + pdi->pi_bar[i].size) {
306 			offset = port - pdi->pi_bar[i].addr;
307 			if (in)
308 				*eax = (*pe->pe_barread)(ctx, vcpu, pdi, i,
309 							 offset, bytes);
310 			else
311 				(*pe->pe_barwrite)(ctx, vcpu, pdi, i, offset,
312 						   bytes, *eax);
313 			return (0);
314 		}
315 	}
316 	return (-1);
317 }
318 
319 static int
320 pci_emul_mem_handler(struct vmctx *ctx, int vcpu, int dir, uint64_t addr,
321 		     int size, uint64_t *val, void *arg1, long arg2)
322 {
323 	struct pci_devinst *pdi = arg1;
324 	struct pci_devemu *pe = pdi->pi_d;
325 	uint64_t offset;
326 	int bidx = (int) arg2;
327 
328 	assert(bidx <= PCI_BARMAX);
329 	assert(pdi->pi_bar[bidx].type == PCIBAR_MEM32 ||
330 	       pdi->pi_bar[bidx].type == PCIBAR_MEM64);
331 	assert(addr >= pdi->pi_bar[bidx].addr &&
332 	       addr + size <= pdi->pi_bar[bidx].addr + pdi->pi_bar[bidx].size);
333 
334 	offset = addr - pdi->pi_bar[bidx].addr;
335 
336 	if (dir == MEM_F_WRITE)
337 		(*pe->pe_barwrite)(ctx, vcpu, pdi, bidx, offset, size, *val);
338 	else
339 		*val = (*pe->pe_barread)(ctx, vcpu, pdi, bidx, offset, size);
340 
341 	return (0);
342 }
343 
344 
345 static int
346 pci_emul_alloc_resource(uint64_t *baseptr, uint64_t limit, uint64_t size,
347 			uint64_t *addr)
348 {
349 	uint64_t base;
350 
351 	assert((size & (size - 1)) == 0);	/* must be a power of 2 */
352 
353 	base = roundup2(*baseptr, size);
354 
355 	if (base + size <= limit) {
356 		*addr = base;
357 		*baseptr = base + size;
358 		return (0);
359 	} else
360 		return (-1);
361 }
362 
363 int
364 pci_emul_alloc_bar(struct pci_devinst *pdi, int idx, enum pcibar_type type,
365 		   uint64_t size)
366 {
367 
368 	return (pci_emul_alloc_pbar(pdi, idx, 0, type, size));
369 }
370 
371 /*
372  * Register (or unregister) the MMIO or I/O region associated with the BAR
373  * register 'idx' of an emulated pci device.
374  */
375 static void
376 modify_bar_registration(struct pci_devinst *pi, int idx, int registration)
377 {
378 	int error;
379 	struct inout_port iop;
380 	struct mem_range mr;
381 
382 	switch (pi->pi_bar[idx].type) {
383 	case PCIBAR_IO:
384 		bzero(&iop, sizeof(struct inout_port));
385 		iop.name = pi->pi_name;
386 		iop.port = pi->pi_bar[idx].addr;
387 		iop.size = pi->pi_bar[idx].size;
388 		if (registration) {
389 			iop.flags = IOPORT_F_INOUT;
390 			iop.handler = pci_emul_io_handler;
391 			iop.arg = pi;
392 			error = register_inout(&iop);
393 		} else
394 			error = unregister_inout(&iop);
395 		break;
396 	case PCIBAR_MEM32:
397 	case PCIBAR_MEM64:
398 		bzero(&mr, sizeof(struct mem_range));
399 		mr.name = pi->pi_name;
400 		mr.base = pi->pi_bar[idx].addr;
401 		mr.size = pi->pi_bar[idx].size;
402 		if (registration) {
403 			mr.flags = MEM_F_RW;
404 			mr.handler = pci_emul_mem_handler;
405 			mr.arg1 = pi;
406 			mr.arg2 = idx;
407 			error = register_mem(&mr);
408 		} else
409 			error = unregister_mem(&mr);
410 		break;
411 	default:
412 		error = EINVAL;
413 		break;
414 	}
415 	assert(error == 0);
416 }
417 
418 static void
419 unregister_bar(struct pci_devinst *pi, int idx)
420 {
421 
422 	modify_bar_registration(pi, idx, 0);
423 }
424 
425 static void
426 register_bar(struct pci_devinst *pi, int idx)
427 {
428 
429 	modify_bar_registration(pi, idx, 1);
430 }
431 
432 /* Are we decoding i/o port accesses for the emulated pci device? */
433 static int
434 porten(struct pci_devinst *pi)
435 {
436 	uint16_t cmd;
437 
438 	cmd = pci_get_cfgdata16(pi, PCIR_COMMAND);
439 
440 	return (cmd & PCIM_CMD_PORTEN);
441 }
442 
443 /* Are we decoding memory accesses for the emulated pci device? */
444 static int
445 memen(struct pci_devinst *pi)
446 {
447 	uint16_t cmd;
448 
449 	cmd = pci_get_cfgdata16(pi, PCIR_COMMAND);
450 
451 	return (cmd & PCIM_CMD_MEMEN);
452 }
453 
454 /*
455  * Update the MMIO or I/O address that is decoded by the BAR register.
456  *
457  * If the pci device has enabled the address space decoding then intercept
458  * the address range decoded by the BAR register.
459  */
460 static void
461 update_bar_address(struct  pci_devinst *pi, uint64_t addr, int idx, int type)
462 {
463 	int decode;
464 
465 	if (pi->pi_bar[idx].type == PCIBAR_IO)
466 		decode = porten(pi);
467 	else
468 		decode = memen(pi);
469 
470 	if (decode)
471 		unregister_bar(pi, idx);
472 
473 	switch (type) {
474 	case PCIBAR_IO:
475 	case PCIBAR_MEM32:
476 		pi->pi_bar[idx].addr = addr;
477 		break;
478 	case PCIBAR_MEM64:
479 		pi->pi_bar[idx].addr &= ~0xffffffffUL;
480 		pi->pi_bar[idx].addr |= addr;
481 		break;
482 	case PCIBAR_MEMHI64:
483 		pi->pi_bar[idx].addr &= 0xffffffff;
484 		pi->pi_bar[idx].addr |= addr;
485 		break;
486 	default:
487 		assert(0);
488 	}
489 
490 	if (decode)
491 		register_bar(pi, idx);
492 }
493 
494 int
495 pci_emul_alloc_pbar(struct pci_devinst *pdi, int idx, uint64_t hostbase,
496 		    enum pcibar_type type, uint64_t size)
497 {
498 	int error;
499 	uint64_t *baseptr, limit, addr, mask, lobits, bar;
500 
501 	assert(idx >= 0 && idx <= PCI_BARMAX);
502 
503 	if ((size & (size - 1)) != 0)
504 		size = 1UL << flsl(size);	/* round up to a power of 2 */
505 
506 	/* Enforce minimum BAR sizes required by the PCI standard */
507 	if (type == PCIBAR_IO) {
508 		if (size < 4)
509 			size = 4;
510 	} else {
511 		if (size < 16)
512 			size = 16;
513 	}
514 
515 	switch (type) {
516 	case PCIBAR_NONE:
517 		baseptr = NULL;
518 		addr = mask = lobits = 0;
519 		break;
520 	case PCIBAR_IO:
521 		if (hostbase &&
522 		    pci_slotinfo[pdi->pi_slot][pdi->pi_func].si_legacy) {
523 			assert(hostbase < PCI_EMUL_IOBASE);
524 			baseptr = &hostbase;
525 		} else {
526 			baseptr = &pci_emul_iobase;
527 		}
528 		limit = PCI_EMUL_IOLIMIT;
529 		mask = PCIM_BAR_IO_BASE;
530 		lobits = PCIM_BAR_IO_SPACE;
531 		break;
532 	case PCIBAR_MEM64:
533 		/*
534 		 * XXX
535 		 * Some drivers do not work well if the 64-bit BAR is allocated
536 		 * above 4GB. Allow for this by allocating small requests under
537 		 * 4GB unless then allocation size is larger than some arbitrary
538 		 * number (32MB currently).
539 		 */
540 		if (size > 32 * 1024 * 1024) {
541 			/*
542 			 * XXX special case for device requiring peer-peer DMA
543 			 */
544 			if (size == 0x100000000UL)
545 				baseptr = &hostbase;
546 			else
547 				baseptr = &pci_emul_membase64;
548 			limit = PCI_EMUL_MEMLIMIT64;
549 			mask = PCIM_BAR_MEM_BASE;
550 			lobits = PCIM_BAR_MEM_SPACE | PCIM_BAR_MEM_64 |
551 				 PCIM_BAR_MEM_PREFETCH;
552 			break;
553 		} else {
554 			baseptr = &pci_emul_membase32;
555 			limit = PCI_EMUL_MEMLIMIT32;
556 			mask = PCIM_BAR_MEM_BASE;
557 			lobits = PCIM_BAR_MEM_SPACE | PCIM_BAR_MEM_64;
558 		}
559 		break;
560 	case PCIBAR_MEM32:
561 		baseptr = &pci_emul_membase32;
562 		limit = PCI_EMUL_MEMLIMIT32;
563 		mask = PCIM_BAR_MEM_BASE;
564 		lobits = PCIM_BAR_MEM_SPACE | PCIM_BAR_MEM_32;
565 		break;
566 	default:
567 		printf("pci_emul_alloc_base: invalid bar type %d\n", type);
568 		assert(0);
569 	}
570 
571 	if (baseptr != NULL) {
572 		error = pci_emul_alloc_resource(baseptr, limit, size, &addr);
573 		if (error != 0)
574 			return (error);
575 	}
576 
577 	pdi->pi_bar[idx].type = type;
578 	pdi->pi_bar[idx].addr = addr;
579 	pdi->pi_bar[idx].size = size;
580 
581 	/* Initialize the BAR register in config space */
582 	bar = (addr & mask) | lobits;
583 	pci_set_cfgdata32(pdi, PCIR_BAR(idx), bar);
584 
585 	if (type == PCIBAR_MEM64) {
586 		assert(idx + 1 <= PCI_BARMAX);
587 		pdi->pi_bar[idx + 1].type = PCIBAR_MEMHI64;
588 		pci_set_cfgdata32(pdi, PCIR_BAR(idx + 1), bar >> 32);
589 	}
590 
591 	register_bar(pdi, idx);
592 
593 	return (0);
594 }
595 
596 #define	CAP_START_OFFSET	0x40
597 static int
598 pci_emul_add_capability(struct pci_devinst *pi, u_char *capdata, int caplen)
599 {
600 	int i, capoff, capid, reallen;
601 	uint16_t sts;
602 
603 	static u_char endofcap[4] = {
604 		PCIY_RESERVED, 0, 0, 0
605 	};
606 
607 	assert(caplen > 0 && capdata[0] != PCIY_RESERVED);
608 
609 	reallen = roundup2(caplen, 4);		/* dword aligned */
610 
611 	sts = pci_get_cfgdata16(pi, PCIR_STATUS);
612 	if ((sts & PCIM_STATUS_CAPPRESENT) == 0) {
613 		capoff = CAP_START_OFFSET;
614 		pci_set_cfgdata8(pi, PCIR_CAP_PTR, capoff);
615 		pci_set_cfgdata16(pi, PCIR_STATUS, sts|PCIM_STATUS_CAPPRESENT);
616 	} else {
617 		capoff = pci_get_cfgdata8(pi, PCIR_CAP_PTR);
618 		while (1) {
619 			assert((capoff & 0x3) == 0);
620 			capid = pci_get_cfgdata8(pi, capoff);
621 			if (capid == PCIY_RESERVED)
622 				break;
623 			capoff = pci_get_cfgdata8(pi, capoff + 1);
624 		}
625 	}
626 
627 	/* Check if we have enough space */
628 	if (capoff + reallen + sizeof(endofcap) > PCI_REGMAX + 1)
629 		return (-1);
630 
631 	/* Copy the capability */
632 	for (i = 0; i < caplen; i++)
633 		pci_set_cfgdata8(pi, capoff + i, capdata[i]);
634 
635 	/* Set the next capability pointer */
636 	pci_set_cfgdata8(pi, capoff + 1, capoff + reallen);
637 
638 	/* Copy of the reserved capability which serves as the end marker */
639 	for (i = 0; i < sizeof(endofcap); i++)
640 		pci_set_cfgdata8(pi, capoff + reallen + i, endofcap[i]);
641 
642 	return (0);
643 }
644 
645 static struct pci_devemu *
646 pci_emul_finddev(char *name)
647 {
648 	struct pci_devemu **pdpp, *pdp;
649 
650 	SET_FOREACH(pdpp, pci_devemu_set) {
651 		pdp = *pdpp;
652 		if (!strcmp(pdp->pe_emu, name)) {
653 			return (pdp);
654 		}
655 	}
656 
657 	return (NULL);
658 }
659 
660 static int
661 pci_emul_init(struct vmctx *ctx, struct pci_devemu *pde, int slot, int func,
662 	      char *params)
663 {
664 	struct pci_devinst *pdi;
665 	int err;
666 
667 	pdi = malloc(sizeof(struct pci_devinst));
668 	bzero(pdi, sizeof(*pdi));
669 
670 	pdi->pi_vmctx = ctx;
671 	pdi->pi_bus = 0;
672 	pdi->pi_slot = slot;
673 	pdi->pi_func = func;
674 	pdi->pi_lintr_pin = -1;
675 	pdi->pi_d = pde;
676 	snprintf(pdi->pi_name, PI_NAMESZ, "%s-pci-%d", pde->pe_emu, slot);
677 
678 	/* Disable legacy interrupts */
679 	pci_set_cfgdata8(pdi, PCIR_INTLINE, 255);
680 	pci_set_cfgdata8(pdi, PCIR_INTPIN, 0);
681 
682 	pci_set_cfgdata8(pdi, PCIR_COMMAND,
683 		    PCIM_CMD_PORTEN | PCIM_CMD_MEMEN | PCIM_CMD_BUSMASTEREN);
684 
685 	err = (*pde->pe_init)(ctx, pdi, params);
686 	if (err != 0) {
687 		free(pdi);
688 	} else {
689 		pci_emul_devices++;
690 		pci_slotinfo[slot][func].si_devi = pdi;
691 	}
692 
693 	return (err);
694 }
695 
696 void
697 pci_populate_msicap(struct msicap *msicap, int msgnum, int nextptr)
698 {
699 	int mmc;
700 
701 	CTASSERT(sizeof(struct msicap) == 14);
702 
703 	/* Number of msi messages must be a power of 2 between 1 and 32 */
704 	assert((msgnum & (msgnum - 1)) == 0 && msgnum >= 1 && msgnum <= 32);
705 	mmc = ffs(msgnum) - 1;
706 
707 	bzero(msicap, sizeof(struct msicap));
708 	msicap->capid = PCIY_MSI;
709 	msicap->nextptr = nextptr;
710 	msicap->msgctrl = PCIM_MSICTRL_64BIT | (mmc << 1);
711 }
712 
713 int
714 pci_emul_add_msicap(struct pci_devinst *pi, int msgnum)
715 {
716 	struct msicap msicap;
717 
718 	pci_populate_msicap(&msicap, msgnum, 0);
719 
720 	return (pci_emul_add_capability(pi, (u_char *)&msicap, sizeof(msicap)));
721 }
722 
723 static void
724 pci_populate_msixcap(struct msixcap *msixcap, int msgnum, int barnum,
725 		     uint32_t msix_tab_size, int nextptr)
726 {
727 	CTASSERT(sizeof(struct msixcap) == 12);
728 
729 	assert(msix_tab_size % 4096 == 0);
730 
731 	bzero(msixcap, sizeof(struct msixcap));
732 	msixcap->capid = PCIY_MSIX;
733 	msixcap->nextptr = nextptr;
734 
735 	/*
736 	 * Message Control Register, all fields set to
737 	 * zero except for the Table Size.
738 	 * Note: Table size N is encoded as N-1
739 	 */
740 	msixcap->msgctrl = msgnum - 1;
741 
742 	/*
743 	 * MSI-X BAR setup:
744 	 * - MSI-X table start at offset 0
745 	 * - PBA table starts at a 4K aligned offset after the MSI-X table
746 	 */
747 	msixcap->table_info = barnum & PCIM_MSIX_BIR_MASK;
748 	msixcap->pba_info = msix_tab_size | (barnum & PCIM_MSIX_BIR_MASK);
749 }
750 
751 static void
752 pci_msix_table_init(struct pci_devinst *pi, int table_entries)
753 {
754 	int i, table_size;
755 
756 	assert(table_entries > 0);
757 	assert(table_entries <= MAX_MSIX_TABLE_ENTRIES);
758 
759 	table_size = table_entries * MSIX_TABLE_ENTRY_SIZE;
760 	pi->pi_msix.table = malloc(table_size);
761 	bzero(pi->pi_msix.table, table_size);
762 
763 	/* set mask bit of vector control register */
764 	for (i = 0; i < table_entries; i++)
765 		pi->pi_msix.table[i].vector_control |= PCIM_MSIX_VCTRL_MASK;
766 }
767 
768 int
769 pci_emul_add_msixcap(struct pci_devinst *pi, int msgnum, int barnum)
770 {
771 	uint16_t pba_index;
772 	uint32_t tab_size;
773 	struct msixcap msixcap;
774 
775 	assert(msgnum >= 1 && msgnum <= MAX_MSIX_TABLE_ENTRIES);
776 	assert(barnum >= 0 && barnum <= PCIR_MAX_BAR_0);
777 
778 	tab_size = msgnum * MSIX_TABLE_ENTRY_SIZE;
779 
780 	/* Align table size to nearest 4K */
781 	tab_size = roundup2(tab_size, 4096);
782 
783 	pi->pi_msix.table_bar = barnum;
784 	pi->pi_msix.pba_bar   = barnum;
785 	pi->pi_msix.table_offset = 0;
786 	pi->pi_msix.table_count = msgnum;
787 	pi->pi_msix.pba_offset = tab_size;
788 
789 	/* calculate the MMIO size required for MSI-X PBA */
790 	pba_index = (msgnum - 1) / (PBA_TABLE_ENTRY_SIZE * 8);
791 	pi->pi_msix.pba_size = (pba_index + 1) * PBA_TABLE_ENTRY_SIZE;
792 
793 	pci_msix_table_init(pi, msgnum);
794 
795 	pci_populate_msixcap(&msixcap, msgnum, barnum, tab_size, 0);
796 
797 	/* allocate memory for MSI-X Table and PBA */
798 	pci_emul_alloc_bar(pi, barnum, PCIBAR_MEM32,
799 				tab_size + pi->pi_msix.pba_size);
800 
801 	return (pci_emul_add_capability(pi, (u_char *)&msixcap,
802 					sizeof(msixcap)));
803 }
804 
805 void
806 msixcap_cfgwrite(struct pci_devinst *pi, int capoff, int offset,
807 		 int bytes, uint32_t val)
808 {
809 	uint16_t msgctrl, rwmask;
810 	int off, table_bar;
811 
812 	off = offset - capoff;
813 	table_bar = pi->pi_msix.table_bar;
814 	/* Message Control Register */
815 	if (off == 2 && bytes == 2) {
816 		rwmask = PCIM_MSIXCTRL_MSIX_ENABLE | PCIM_MSIXCTRL_FUNCTION_MASK;
817 		msgctrl = pci_get_cfgdata16(pi, offset);
818 		msgctrl &= ~rwmask;
819 		msgctrl |= val & rwmask;
820 		val = msgctrl;
821 
822 		pi->pi_msix.enabled = val & PCIM_MSIXCTRL_MSIX_ENABLE;
823 		pi->pi_msix.function_mask = val & PCIM_MSIXCTRL_FUNCTION_MASK;
824 	}
825 
826 	CFGWRITE(pi, offset, val, bytes);
827 }
828 
829 void
830 msicap_cfgwrite(struct pci_devinst *pi, int capoff, int offset,
831 		int bytes, uint32_t val)
832 {
833 	uint16_t msgctrl, rwmask, msgdata, mme;
834 	uint32_t addrlo;
835 
836 	/*
837 	 * If guest is writing to the message control register make sure
838 	 * we do not overwrite read-only fields.
839 	 */
840 	if ((offset - capoff) == 2 && bytes == 2) {
841 		rwmask = PCIM_MSICTRL_MME_MASK | PCIM_MSICTRL_MSI_ENABLE;
842 		msgctrl = pci_get_cfgdata16(pi, offset);
843 		msgctrl &= ~rwmask;
844 		msgctrl |= val & rwmask;
845 		val = msgctrl;
846 
847 		addrlo = pci_get_cfgdata32(pi, capoff + 4);
848 		if (msgctrl & PCIM_MSICTRL_64BIT)
849 			msgdata = pci_get_cfgdata16(pi, capoff + 12);
850 		else
851 			msgdata = pci_get_cfgdata16(pi, capoff + 8);
852 
853 		mme = msgctrl & PCIM_MSICTRL_MME_MASK;
854 		pi->pi_msi.enabled = msgctrl & PCIM_MSICTRL_MSI_ENABLE ? 1 : 0;
855 		if (pi->pi_msi.enabled) {
856 			pi->pi_msi.addr = addrlo;
857 			pi->pi_msi.msg_data = msgdata;
858 			pi->pi_msi.maxmsgnum = 1 << (mme >> 4);
859 		} else {
860 			pi->pi_msi.maxmsgnum = 0;
861 		}
862 	}
863 
864 	CFGWRITE(pi, offset, val, bytes);
865 }
866 
867 void
868 pciecap_cfgwrite(struct pci_devinst *pi, int capoff, int offset,
869 		 int bytes, uint32_t val)
870 {
871 
872 	/* XXX don't write to the readonly parts */
873 	CFGWRITE(pi, offset, val, bytes);
874 }
875 
876 #define	PCIECAP_VERSION	0x2
877 int
878 pci_emul_add_pciecap(struct pci_devinst *pi, int type)
879 {
880 	int err;
881 	struct pciecap pciecap;
882 
883 	CTASSERT(sizeof(struct pciecap) == 60);
884 
885 	if (type != PCIEM_TYPE_ROOT_PORT)
886 		return (-1);
887 
888 	bzero(&pciecap, sizeof(pciecap));
889 
890 	pciecap.capid = PCIY_EXPRESS;
891 	pciecap.pcie_capabilities = PCIECAP_VERSION | PCIEM_TYPE_ROOT_PORT;
892 	pciecap.link_capabilities = 0x411;	/* gen1, x1 */
893 	pciecap.link_status = 0x11;		/* gen1, x1 */
894 
895 	err = pci_emul_add_capability(pi, (u_char *)&pciecap, sizeof(pciecap));
896 	return (err);
897 }
898 
899 /*
900  * This function assumes that 'coff' is in the capabilities region of the
901  * config space.
902  */
903 static void
904 pci_emul_capwrite(struct pci_devinst *pi, int offset, int bytes, uint32_t val)
905 {
906 	int capid;
907 	uint8_t capoff, nextoff;
908 
909 	/* Do not allow un-aligned writes */
910 	if ((offset & (bytes - 1)) != 0)
911 		return;
912 
913 	/* Find the capability that we want to update */
914 	capoff = CAP_START_OFFSET;
915 	while (1) {
916 		capid = pci_get_cfgdata8(pi, capoff);
917 		if (capid == PCIY_RESERVED)
918 			break;
919 
920 		nextoff = pci_get_cfgdata8(pi, capoff + 1);
921 		if (offset >= capoff && offset < nextoff)
922 			break;
923 
924 		capoff = nextoff;
925 	}
926 	assert(offset >= capoff);
927 
928 	/*
929 	 * Capability ID and Next Capability Pointer are readonly.
930 	 * However, some o/s's do 4-byte writes that include these.
931 	 * For this case, trim the write back to 2 bytes and adjust
932 	 * the data.
933 	 */
934 	if (offset == capoff || offset == capoff + 1) {
935 		if (offset == capoff && bytes == 4) {
936 			bytes = 2;
937 			offset += 2;
938 			val >>= 16;
939 		} else
940 			return;
941 	}
942 
943 	switch (capid) {
944 	case PCIY_MSI:
945 		msicap_cfgwrite(pi, capoff, offset, bytes, val);
946 		break;
947 	case PCIY_MSIX:
948 		msixcap_cfgwrite(pi, capoff, offset, bytes, val);
949 		break;
950 	case PCIY_EXPRESS:
951 		pciecap_cfgwrite(pi, capoff, offset, bytes, val);
952 		break;
953 	default:
954 		break;
955 	}
956 }
957 
958 static int
959 pci_emul_iscap(struct pci_devinst *pi, int offset)
960 {
961 	int found;
962 	uint16_t sts;
963 	uint8_t capid, lastoff;
964 
965 	found = 0;
966 	sts = pci_get_cfgdata16(pi, PCIR_STATUS);
967 	if ((sts & PCIM_STATUS_CAPPRESENT) != 0) {
968 		lastoff = pci_get_cfgdata8(pi, PCIR_CAP_PTR);
969 		while (1) {
970 			assert((lastoff & 0x3) == 0);
971 			capid = pci_get_cfgdata8(pi, lastoff);
972 			if (capid == PCIY_RESERVED)
973 				break;
974 			lastoff = pci_get_cfgdata8(pi, lastoff + 1);
975 		}
976 		if (offset >= CAP_START_OFFSET && offset <= lastoff)
977 			found = 1;
978 	}
979 	return (found);
980 }
981 
982 static int
983 pci_emul_fallback_handler(struct vmctx *ctx, int vcpu, int dir, uint64_t addr,
984 			  int size, uint64_t *val, void *arg1, long arg2)
985 {
986 	/*
987 	 * Ignore writes; return 0xff's for reads. The mem read code
988 	 * will take care of truncating to the correct size.
989 	 */
990 	if (dir == MEM_F_READ) {
991 		*val = 0xffffffffffffffff;
992 	}
993 
994 	return (0);
995 }
996 
997 int
998 init_pci(struct vmctx *ctx)
999 {
1000 	struct mem_range memp;
1001 	struct pci_devemu *pde;
1002 	struct slotinfo *si;
1003 	size_t lowmem;
1004 	int slot, func;
1005 	int error;
1006 
1007 	pci_emul_iobase = PCI_EMUL_IOBASE;
1008 	pci_emul_membase32 = vm_get_lowmem_limit(ctx);
1009 	pci_emul_membase64 = PCI_EMUL_MEMBASE64;
1010 
1011 	for (slot = 0; slot < MAXSLOTS; slot++) {
1012 		for (func = 0; func < MAXFUNCS; func++) {
1013 			si = &pci_slotinfo[slot][func];
1014 			if (si->si_name != NULL) {
1015 				pde = pci_emul_finddev(si->si_name);
1016 				assert(pde != NULL);
1017 				error = pci_emul_init(ctx, pde, slot, func,
1018 					    si->si_param);
1019 				if (error)
1020 					return (error);
1021 			}
1022 		}
1023 	}
1024 
1025 	/*
1026 	 * The guest physical memory map looks like the following:
1027 	 * [0,		    lowmem)		guest system memory
1028 	 * [lowmem,	    lowmem_limit)	memory hole (may be absent)
1029 	 * [lowmem_limit,   4GB)		PCI hole (32-bit BAR allocation)
1030 	 * [4GB,	    4GB + highmem)
1031 	 *
1032 	 * Accesses to memory addresses that are not allocated to system
1033 	 * memory or PCI devices return 0xff's.
1034 	 */
1035 	error = vm_get_memory_seg(ctx, 0, &lowmem, NULL);
1036 	assert(error == 0);
1037 
1038 	memset(&memp, 0, sizeof(struct mem_range));
1039 	memp.name = "PCI hole";
1040 	memp.flags = MEM_F_RW;
1041 	memp.base = lowmem;
1042 	memp.size = (4ULL * 1024 * 1024 * 1024) - lowmem;
1043 	memp.handler = pci_emul_fallback_handler;
1044 
1045 	error = register_mem_fallback(&memp);
1046 	assert(error == 0);
1047 
1048 	return (0);
1049 }
1050 
1051 int
1052 pci_msi_enabled(struct pci_devinst *pi)
1053 {
1054 	return (pi->pi_msi.enabled);
1055 }
1056 
1057 int
1058 pci_msi_maxmsgnum(struct pci_devinst *pi)
1059 {
1060 	if (pi->pi_msi.enabled)
1061 		return (pi->pi_msi.maxmsgnum);
1062 	else
1063 		return (0);
1064 }
1065 
1066 int
1067 pci_msix_enabled(struct pci_devinst *pi)
1068 {
1069 
1070 	return (pi->pi_msix.enabled && !pi->pi_msi.enabled);
1071 }
1072 
1073 void
1074 pci_generate_msix(struct pci_devinst *pi, int index)
1075 {
1076 	struct msix_table_entry *mte;
1077 
1078 	if (!pci_msix_enabled(pi))
1079 		return;
1080 
1081 	if (pi->pi_msix.function_mask)
1082 		return;
1083 
1084 	if (index >= pi->pi_msix.table_count)
1085 		return;
1086 
1087 	mte = &pi->pi_msix.table[index];
1088 	if ((mte->vector_control & PCIM_MSIX_VCTRL_MASK) == 0) {
1089 		/* XXX Set PBA bit if interrupt is disabled */
1090 		vm_lapic_msi(pi->pi_vmctx, mte->addr, mte->msg_data);
1091 	}
1092 }
1093 
1094 void
1095 pci_generate_msi(struct pci_devinst *pi, int index)
1096 {
1097 
1098 	if (pci_msi_enabled(pi) && index < pci_msi_maxmsgnum(pi)) {
1099 		vm_lapic_msi(pi->pi_vmctx, pi->pi_msi.addr,
1100 			     pi->pi_msi.msg_data + index);
1101 	}
1102 }
1103 
1104 int
1105 pci_is_legacy(struct pci_devinst *pi)
1106 {
1107 
1108 	return (pci_slotinfo[pi->pi_slot][pi->pi_func].si_legacy);
1109 }
1110 
1111 int
1112 pci_lintr_request(struct pci_devinst *pi, int req)
1113 {
1114 	int irq;
1115 
1116 	irq = legacy_irq_alloc(req);
1117 	if (irq < 0)
1118 		return (-1);
1119 
1120 	pi->pi_lintr_pin = irq;
1121 	pci_set_cfgdata8(pi, PCIR_INTLINE, irq);
1122 	pci_set_cfgdata8(pi, PCIR_INTPIN, 1);
1123 	return (0);
1124 }
1125 
1126 void
1127 pci_lintr_assert(struct pci_devinst *pi)
1128 {
1129 
1130 	assert(pi->pi_lintr_pin >= 0);
1131 
1132 	if (pi->pi_lintr_state == 0) {
1133 		pi->pi_lintr_state = 1;
1134 		vm_ioapic_assert_irq(pi->pi_vmctx, pi->pi_lintr_pin);
1135 	}
1136 }
1137 
1138 void
1139 pci_lintr_deassert(struct pci_devinst *pi)
1140 {
1141 
1142 	assert(pi->pi_lintr_pin >= 0);
1143 
1144 	if (pi->pi_lintr_state == 1) {
1145 		pi->pi_lintr_state = 0;
1146 		vm_ioapic_deassert_irq(pi->pi_vmctx, pi->pi_lintr_pin);
1147 	}
1148 }
1149 
1150 /*
1151  * Return 1 if the emulated device in 'slot' is a multi-function device.
1152  * Return 0 otherwise.
1153  */
1154 static int
1155 pci_emul_is_mfdev(int slot)
1156 {
1157 	int f, numfuncs;
1158 
1159 	numfuncs = 0;
1160 	for (f = 0; f < MAXFUNCS; f++) {
1161 		if (pci_slotinfo[slot][f].si_devi != NULL) {
1162 			numfuncs++;
1163 		}
1164 	}
1165 	return (numfuncs > 1);
1166 }
1167 
1168 /*
1169  * Ensure that the PCIM_MFDEV bit is properly set (or unset) depending on
1170  * whether or not is a multi-function being emulated in the pci 'slot'.
1171  */
1172 static void
1173 pci_emul_hdrtype_fixup(int slot, int off, int bytes, uint32_t *rv)
1174 {
1175 	int mfdev;
1176 
1177 	if (off <= PCIR_HDRTYPE && off + bytes > PCIR_HDRTYPE) {
1178 		mfdev = pci_emul_is_mfdev(slot);
1179 		switch (bytes) {
1180 		case 1:
1181 		case 2:
1182 			*rv &= ~PCIM_MFDEV;
1183 			if (mfdev) {
1184 				*rv |= PCIM_MFDEV;
1185 			}
1186 			break;
1187 		case 4:
1188 			*rv &= ~(PCIM_MFDEV << 16);
1189 			if (mfdev) {
1190 				*rv |= (PCIM_MFDEV << 16);
1191 			}
1192 			break;
1193 		}
1194 	}
1195 }
1196 
1197 static int cfgbus, cfgslot, cfgfunc, cfgoff;
1198 
1199 static int
1200 pci_emul_cfgaddr(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
1201 		 uint32_t *eax, void *arg)
1202 {
1203 	uint32_t x;
1204 
1205 	if (bytes != 4) {
1206 		if (in)
1207 			*eax = (bytes == 2) ? 0xffff : 0xff;
1208 		return (0);
1209 	}
1210 
1211 	if (in) {
1212 		x = (cfgbus << 16) |
1213 		    (cfgslot << 11) |
1214 		    (cfgfunc << 8) |
1215 		    cfgoff;
1216 		*eax = x | CONF1_ENABLE;
1217 	} else {
1218 		x = *eax;
1219 		cfgoff = x & PCI_REGMAX;
1220 		cfgfunc = (x >> 8) & PCI_FUNCMAX;
1221 		cfgslot = (x >> 11) & PCI_SLOTMAX;
1222 		cfgbus = (x >> 16) & PCI_BUSMAX;
1223 	}
1224 
1225 	return (0);
1226 }
1227 INOUT_PORT(pci_cfgaddr, CONF1_ADDR_PORT, IOPORT_F_INOUT, pci_emul_cfgaddr);
1228 
1229 static uint32_t
1230 bits_changed(uint32_t old, uint32_t new, uint32_t mask)
1231 {
1232 
1233 	return ((old ^ new) & mask);
1234 }
1235 
1236 static void
1237 pci_emul_cmdwrite(struct pci_devinst *pi, uint32_t new, int bytes)
1238 {
1239 	int i;
1240 	uint16_t old;
1241 
1242 	/*
1243 	 * The command register is at an offset of 4 bytes and thus the
1244 	 * guest could write 1, 2 or 4 bytes starting at this offset.
1245 	 */
1246 
1247 	old = pci_get_cfgdata16(pi, PCIR_COMMAND);	/* stash old value */
1248 	CFGWRITE(pi, PCIR_COMMAND, new, bytes);		/* update config */
1249 	new = pci_get_cfgdata16(pi, PCIR_COMMAND);	/* get updated value */
1250 
1251 	/*
1252 	 * If the MMIO or I/O address space decoding has changed then
1253 	 * register/unregister all BARs that decode that address space.
1254 	 */
1255 	for (i = 0; i <= PCI_BARMAX; i++) {
1256 		switch (pi->pi_bar[i].type) {
1257 			case PCIBAR_NONE:
1258 			case PCIBAR_MEMHI64:
1259 				break;
1260 			case PCIBAR_IO:
1261 				/* I/O address space decoding changed? */
1262 				if (bits_changed(old, new, PCIM_CMD_PORTEN)) {
1263 					if (porten(pi))
1264 						register_bar(pi, i);
1265 					else
1266 						unregister_bar(pi, i);
1267 				}
1268 				break;
1269 			case PCIBAR_MEM32:
1270 			case PCIBAR_MEM64:
1271 				/* MMIO address space decoding changed? */
1272 				if (bits_changed(old, new, PCIM_CMD_MEMEN)) {
1273 					if (memen(pi))
1274 						register_bar(pi, i);
1275 					else
1276 						unregister_bar(pi, i);
1277 				}
1278 				break;
1279 			default:
1280 				assert(0);
1281 		}
1282 	}
1283 }
1284 
1285 static int
1286 pci_emul_cfgdata(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
1287 		 uint32_t *eax, void *arg)
1288 {
1289 	struct pci_devinst *pi;
1290 	struct pci_devemu *pe;
1291 	int coff, idx, needcfg;
1292 	uint64_t addr, bar, mask;
1293 
1294 	assert(bytes == 1 || bytes == 2 || bytes == 4);
1295 
1296 	if (cfgbus == 0)
1297 		pi = pci_slotinfo[cfgslot][cfgfunc].si_devi;
1298 	else
1299 		pi = NULL;
1300 
1301 	coff = cfgoff + (port - CONF1_DATA_PORT);
1302 
1303 #if 0
1304 	printf("pcicfg-%s from 0x%0x of %d bytes (%d/%d/%d)\n\r",
1305 		in ? "read" : "write", coff, bytes, cfgbus, cfgslot, cfgfunc);
1306 #endif
1307 
1308 	/*
1309 	 * Just return if there is no device at this cfgslot:cfgfunc or
1310 	 * if the guest is doing an un-aligned access
1311 	 */
1312 	if (pi == NULL || (coff & (bytes - 1)) != 0) {
1313 		if (in)
1314 			*eax = 0xffffffff;
1315 		return (0);
1316 	}
1317 
1318 	pe = pi->pi_d;
1319 
1320 	/*
1321 	 * Config read
1322 	 */
1323 	if (in) {
1324 		/* Let the device emulation override the default handler */
1325 		if (pe->pe_cfgread != NULL) {
1326 			needcfg = pe->pe_cfgread(ctx, vcpu, pi,
1327 						    coff, bytes, eax);
1328 		} else {
1329 			needcfg = 1;
1330 		}
1331 
1332 		if (needcfg) {
1333 			if (bytes == 1)
1334 				*eax = pci_get_cfgdata8(pi, coff);
1335 			else if (bytes == 2)
1336 				*eax = pci_get_cfgdata16(pi, coff);
1337 			else
1338 				*eax = pci_get_cfgdata32(pi, coff);
1339 		}
1340 
1341 		pci_emul_hdrtype_fixup(cfgslot, coff, bytes, eax);
1342 	} else {
1343 		/* Let the device emulation override the default handler */
1344 		if (pe->pe_cfgwrite != NULL &&
1345 		    (*pe->pe_cfgwrite)(ctx, vcpu, pi, coff, bytes, *eax) == 0)
1346 			return (0);
1347 
1348 		/*
1349 		 * Special handling for write to BAR registers
1350 		 */
1351 		if (coff >= PCIR_BAR(0) && coff < PCIR_BAR(PCI_BARMAX + 1)) {
1352 			/*
1353 			 * Ignore writes to BAR registers that are not
1354 			 * 4-byte aligned.
1355 			 */
1356 			if (bytes != 4 || (coff & 0x3) != 0)
1357 				return (0);
1358 			idx = (coff - PCIR_BAR(0)) / 4;
1359 			mask = ~(pi->pi_bar[idx].size - 1);
1360 			switch (pi->pi_bar[idx].type) {
1361 			case PCIBAR_NONE:
1362 				pi->pi_bar[idx].addr = bar = 0;
1363 				break;
1364 			case PCIBAR_IO:
1365 				addr = *eax & mask;
1366 				addr &= 0xffff;
1367 				bar = addr | PCIM_BAR_IO_SPACE;
1368 				/*
1369 				 * Register the new BAR value for interception
1370 				 */
1371 				if (addr != pi->pi_bar[idx].addr) {
1372 					update_bar_address(pi, addr, idx,
1373 							   PCIBAR_IO);
1374 				}
1375 				break;
1376 			case PCIBAR_MEM32:
1377 				addr = bar = *eax & mask;
1378 				bar |= PCIM_BAR_MEM_SPACE | PCIM_BAR_MEM_32;
1379 				if (addr != pi->pi_bar[idx].addr) {
1380 					update_bar_address(pi, addr, idx,
1381 							   PCIBAR_MEM32);
1382 				}
1383 				break;
1384 			case PCIBAR_MEM64:
1385 				addr = bar = *eax & mask;
1386 				bar |= PCIM_BAR_MEM_SPACE | PCIM_BAR_MEM_64 |
1387 				       PCIM_BAR_MEM_PREFETCH;
1388 				if (addr != (uint32_t)pi->pi_bar[idx].addr) {
1389 					update_bar_address(pi, addr, idx,
1390 							   PCIBAR_MEM64);
1391 				}
1392 				break;
1393 			case PCIBAR_MEMHI64:
1394 				mask = ~(pi->pi_bar[idx - 1].size - 1);
1395 				addr = ((uint64_t)*eax << 32) & mask;
1396 				bar = addr >> 32;
1397 				if (bar != pi->pi_bar[idx - 1].addr >> 32) {
1398 					update_bar_address(pi, addr, idx - 1,
1399 							   PCIBAR_MEMHI64);
1400 				}
1401 				break;
1402 			default:
1403 				assert(0);
1404 			}
1405 			pci_set_cfgdata32(pi, coff, bar);
1406 
1407 		} else if (pci_emul_iscap(pi, coff)) {
1408 			pci_emul_capwrite(pi, coff, bytes, *eax);
1409 		} else if (coff == PCIR_COMMAND) {
1410 			pci_emul_cmdwrite(pi, *eax, bytes);
1411 		} else {
1412 			CFGWRITE(pi, coff, *eax, bytes);
1413 		}
1414 	}
1415 
1416 	return (0);
1417 }
1418 
1419 INOUT_PORT(pci_cfgdata, CONF1_DATA_PORT+0, IOPORT_F_INOUT, pci_emul_cfgdata);
1420 INOUT_PORT(pci_cfgdata, CONF1_DATA_PORT+1, IOPORT_F_INOUT, pci_emul_cfgdata);
1421 INOUT_PORT(pci_cfgdata, CONF1_DATA_PORT+2, IOPORT_F_INOUT, pci_emul_cfgdata);
1422 INOUT_PORT(pci_cfgdata, CONF1_DATA_PORT+3, IOPORT_F_INOUT, pci_emul_cfgdata);
1423 
1424 /*
1425  * I/O ports to configure PCI IRQ routing. We ignore all writes to it.
1426  */
1427 static int
1428 pci_irq_port_handler(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
1429 		     uint32_t *eax, void *arg)
1430 {
1431 	assert(in == 0);
1432 	return (0);
1433 }
1434 INOUT_PORT(pci_irq, 0xC00, IOPORT_F_OUT, pci_irq_port_handler);
1435 INOUT_PORT(pci_irq, 0xC01, IOPORT_F_OUT, pci_irq_port_handler);
1436 
1437 #define PCI_EMUL_TEST
1438 #ifdef PCI_EMUL_TEST
1439 /*
1440  * Define a dummy test device
1441  */
1442 #define DIOSZ	20
1443 #define DMEMSZ	4096
1444 struct pci_emul_dsoftc {
1445 	uint8_t   ioregs[DIOSZ];
1446 	uint8_t	  memregs[DMEMSZ];
1447 };
1448 
1449 #define	PCI_EMUL_MSI_MSGS	 4
1450 #define	PCI_EMUL_MSIX_MSGS	16
1451 
1452 static int
1453 pci_emul_dinit(struct vmctx *ctx, struct pci_devinst *pi, char *opts)
1454 {
1455 	int error;
1456 	struct pci_emul_dsoftc *sc;
1457 
1458 	sc = malloc(sizeof(struct pci_emul_dsoftc));
1459 	memset(sc, 0, sizeof(struct pci_emul_dsoftc));
1460 
1461 	pi->pi_arg = sc;
1462 
1463 	pci_set_cfgdata16(pi, PCIR_DEVICE, 0x0001);
1464 	pci_set_cfgdata16(pi, PCIR_VENDOR, 0x10DD);
1465 	pci_set_cfgdata8(pi, PCIR_CLASS, 0x02);
1466 
1467 	error = pci_emul_add_msicap(pi, PCI_EMUL_MSI_MSGS);
1468 	assert(error == 0);
1469 
1470 	error = pci_emul_alloc_bar(pi, 0, PCIBAR_IO, DIOSZ);
1471 	assert(error == 0);
1472 
1473 	error = pci_emul_alloc_bar(pi, 1, PCIBAR_MEM32, DMEMSZ);
1474 	assert(error == 0);
1475 
1476 	return (0);
1477 }
1478 
1479 static void
1480 pci_emul_diow(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, int baridx,
1481 	      uint64_t offset, int size, uint64_t value)
1482 {
1483 	int i;
1484 	struct pci_emul_dsoftc *sc = pi->pi_arg;
1485 
1486 	if (baridx == 0) {
1487 		if (offset + size > DIOSZ) {
1488 			printf("diow: iow too large, offset %ld size %d\n",
1489 			       offset, size);
1490 			return;
1491 		}
1492 
1493 		if (size == 1) {
1494 			sc->ioregs[offset] = value & 0xff;
1495 		} else if (size == 2) {
1496 			*(uint16_t *)&sc->ioregs[offset] = value & 0xffff;
1497 		} else if (size == 4) {
1498 			*(uint32_t *)&sc->ioregs[offset] = value;
1499 		} else {
1500 			printf("diow: iow unknown size %d\n", size);
1501 		}
1502 
1503 		/*
1504 		 * Special magic value to generate an interrupt
1505 		 */
1506 		if (offset == 4 && size == 4 && pci_msi_enabled(pi))
1507 			pci_generate_msi(pi, value % pci_msi_maxmsgnum(pi));
1508 
1509 		if (value == 0xabcdef) {
1510 			for (i = 0; i < pci_msi_maxmsgnum(pi); i++)
1511 				pci_generate_msi(pi, i);
1512 		}
1513 	}
1514 
1515 	if (baridx == 1) {
1516 		if (offset + size > DMEMSZ) {
1517 			printf("diow: memw too large, offset %ld size %d\n",
1518 			       offset, size);
1519 			return;
1520 		}
1521 
1522 		if (size == 1) {
1523 			sc->memregs[offset] = value;
1524 		} else if (size == 2) {
1525 			*(uint16_t *)&sc->memregs[offset] = value;
1526 		} else if (size == 4) {
1527 			*(uint32_t *)&sc->memregs[offset] = value;
1528 		} else if (size == 8) {
1529 			*(uint64_t *)&sc->memregs[offset] = value;
1530 		} else {
1531 			printf("diow: memw unknown size %d\n", size);
1532 		}
1533 
1534 		/*
1535 		 * magic interrupt ??
1536 		 */
1537 	}
1538 
1539 	if (baridx > 1) {
1540 		printf("diow: unknown bar idx %d\n", baridx);
1541 	}
1542 }
1543 
1544 static uint64_t
1545 pci_emul_dior(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, int baridx,
1546 	      uint64_t offset, int size)
1547 {
1548 	struct pci_emul_dsoftc *sc = pi->pi_arg;
1549 	uint32_t value;
1550 
1551 	if (baridx == 0) {
1552 		if (offset + size > DIOSZ) {
1553 			printf("dior: ior too large, offset %ld size %d\n",
1554 			       offset, size);
1555 			return (0);
1556 		}
1557 
1558 		if (size == 1) {
1559 			value = sc->ioregs[offset];
1560 		} else if (size == 2) {
1561 			value = *(uint16_t *) &sc->ioregs[offset];
1562 		} else if (size == 4) {
1563 			value = *(uint32_t *) &sc->ioregs[offset];
1564 		} else {
1565 			printf("dior: ior unknown size %d\n", size);
1566 		}
1567 	}
1568 
1569 	if (baridx == 1) {
1570 		if (offset + size > DMEMSZ) {
1571 			printf("dior: memr too large, offset %ld size %d\n",
1572 			       offset, size);
1573 			return (0);
1574 		}
1575 
1576 		if (size == 1) {
1577 			value = sc->memregs[offset];
1578 		} else if (size == 2) {
1579 			value = *(uint16_t *) &sc->memregs[offset];
1580 		} else if (size == 4) {
1581 			value = *(uint32_t *) &sc->memregs[offset];
1582 		} else if (size == 8) {
1583 			value = *(uint64_t *) &sc->memregs[offset];
1584 		} else {
1585 			printf("dior: ior unknown size %d\n", size);
1586 		}
1587 	}
1588 
1589 
1590 	if (baridx > 1) {
1591 		printf("dior: unknown bar idx %d\n", baridx);
1592 		return (0);
1593 	}
1594 
1595 	return (value);
1596 }
1597 
1598 struct pci_devemu pci_dummy = {
1599 	.pe_emu = "dummy",
1600 	.pe_init = pci_emul_dinit,
1601 	.pe_barwrite = pci_emul_diow,
1602 	.pe_barread = pci_emul_dior
1603 };
1604 PCI_EMUL_SET(pci_dummy);
1605 
1606 #endif /* PCI_EMUL_TEST */
1607