1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2011 NetApp, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 * $FreeBSD: stable/12/usr.sbin/bhyve/pci_emul.c 368051 2020-11-26 07:31:30Z kib $
29 */
30
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD: stable/12/usr.sbin/bhyve/pci_emul.c 368051 2020-11-26 07:31:30Z kib $");
33
34 #include <sys/param.h>
35 #include <sys/linker_set.h>
36 #include <vm/vm.h>
37 #include <vm/vm_param.h>
38 #include <vm/pmap.h>
39
40 #include <ctype.h>
41 #include <errno.h>
42 #include <pthread.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <strings.h>
47 #include <assert.h>
48 #include <stdbool.h>
49
50 #include <machine/vmm.h>
51 #include <machine/cpufunc.h>
52 #include <machine/specialreg.h>
53 #include <vmmapi.h>
54
55 #include "acpi.h"
56 #include "bhyverun.h"
57 #include "debug.h"
58 #include "inout.h"
59 #include "ioapic.h"
60 #include "mem.h"
61 #include "pci_emul.h"
62 #include "pci_irq.h"
63 #include "pci_lpc.h"
64
65 #define CONF1_ADDR_PORT 0x0cf8
66 #define CONF1_DATA_PORT 0x0cfc
67
68 #define CONF1_ENABLE 0x80000000ul
69
70 #define MAXBUSES (PCI_BUSMAX + 1)
71 #define MAXSLOTS (PCI_SLOTMAX + 1)
72 #define MAXFUNCS (PCI_FUNCMAX + 1)
73
74 struct funcinfo {
75 char *fi_name;
76 char *fi_param;
77 struct pci_devinst *fi_devi;
78 };
79
80 struct intxinfo {
81 int ii_count;
82 int ii_pirq_pin;
83 int ii_ioapic_irq;
84 };
85
86 struct slotinfo {
87 struct intxinfo si_intpins[4];
88 struct funcinfo si_funcs[MAXFUNCS];
89 };
90
91 struct businfo {
92 uint16_t iobase, iolimit; /* I/O window */
93 uint32_t membase32, memlimit32; /* mmio window below 4GB */
94 uint64_t membase64, memlimit64; /* mmio window above 4GB */
95 struct slotinfo slotinfo[MAXSLOTS];
96 };
97
98 static struct businfo *pci_businfo[MAXBUSES];
99
100 SET_DECLARE(pci_devemu_set, struct pci_devemu);
101
102 static uint64_t pci_emul_iobase;
103 static uint64_t pci_emul_membase32;
104 static uint64_t pci_emul_membase64;
105 static uint64_t pci_emul_memlim64;
106
107 #define PCI_EMUL_IOBASE 0x2000
108 #define PCI_EMUL_IOLIMIT 0x10000
109
110 #define PCI_EMUL_ECFG_BASE 0xE0000000 /* 3.5GB */
111 #define PCI_EMUL_ECFG_SIZE (MAXBUSES * 1024 * 1024) /* 1MB per bus */
112 SYSRES_MEM(PCI_EMUL_ECFG_BASE, PCI_EMUL_ECFG_SIZE);
113
114 #define PCI_EMUL_MEMLIMIT32 PCI_EMUL_ECFG_BASE
115
116 static struct pci_devemu *pci_emul_finddev(char *name);
117 static void pci_lintr_route(struct pci_devinst *pi);
118 static void pci_lintr_update(struct pci_devinst *pi);
119 static void pci_cfgrw(struct vmctx *ctx, int vcpu, int in, int bus, int slot,
120 int func, int coff, int bytes, uint32_t *val);
121
122 static __inline void
CFGWRITE(struct pci_devinst * pi,int coff,uint32_t val,int bytes)123 CFGWRITE(struct pci_devinst *pi, int coff, uint32_t val, int bytes)
124 {
125
126 if (bytes == 1)
127 pci_set_cfgdata8(pi, coff, val);
128 else if (bytes == 2)
129 pci_set_cfgdata16(pi, coff, val);
130 else
131 pci_set_cfgdata32(pi, coff, val);
132 }
133
134 static __inline uint32_t
CFGREAD(struct pci_devinst * pi,int coff,int bytes)135 CFGREAD(struct pci_devinst *pi, int coff, int bytes)
136 {
137
138 if (bytes == 1)
139 return (pci_get_cfgdata8(pi, coff));
140 else if (bytes == 2)
141 return (pci_get_cfgdata16(pi, coff));
142 else
143 return (pci_get_cfgdata32(pi, coff));
144 }
145
146 /*
147 * I/O access
148 */
149
150 /*
151 * Slot options are in the form:
152 *
153 * <bus>:<slot>:<func>,<emul>[,<config>]
154 * <slot>[:<func>],<emul>[,<config>]
155 *
156 * slot is 0..31
157 * func is 0..7
158 * emul is a string describing the type of PCI device e.g. virtio-net
159 * config is an optional string, depending on the device, that can be
160 * used for configuration.
161 * Examples are:
162 * 1,virtio-net,tap0
163 * 3:0,dummy
164 */
165 static void
pci_parse_slot_usage(char * aopt)166 pci_parse_slot_usage(char *aopt)
167 {
168
169 EPRINTLN("Invalid PCI slot info field \"%s\"", aopt);
170 }
171
172 int
pci_parse_slot(char * opt)173 pci_parse_slot(char *opt)
174 {
175 struct businfo *bi;
176 struct slotinfo *si;
177 char *emul, *config, *str, *cp;
178 int error, bnum, snum, fnum;
179
180 error = -1;
181 str = strdup(opt);
182
183 emul = config = NULL;
184 if ((cp = strchr(str, ',')) != NULL) {
185 *cp = '\0';
186 emul = cp + 1;
187 if ((cp = strchr(emul, ',')) != NULL) {
188 *cp = '\0';
189 config = cp + 1;
190 }
191 } else {
192 pci_parse_slot_usage(opt);
193 goto done;
194 }
195
196 /* <bus>:<slot>:<func> */
197 if (sscanf(str, "%d:%d:%d", &bnum, &snum, &fnum) != 3) {
198 bnum = 0;
199 /* <slot>:<func> */
200 if (sscanf(str, "%d:%d", &snum, &fnum) != 2) {
201 fnum = 0;
202 /* <slot> */
203 if (sscanf(str, "%d", &snum) != 1) {
204 snum = -1;
205 }
206 }
207 }
208
209 if (bnum < 0 || bnum >= MAXBUSES || snum < 0 || snum >= MAXSLOTS ||
210 fnum < 0 || fnum >= MAXFUNCS) {
211 pci_parse_slot_usage(opt);
212 goto done;
213 }
214
215 if (pci_businfo[bnum] == NULL)
216 pci_businfo[bnum] = calloc(1, sizeof(struct businfo));
217
218 bi = pci_businfo[bnum];
219 si = &bi->slotinfo[snum];
220
221 if (si->si_funcs[fnum].fi_name != NULL) {
222 EPRINTLN("pci slot %d:%d already occupied!",
223 snum, fnum);
224 goto done;
225 }
226
227 if (pci_emul_finddev(emul) == NULL) {
228 EPRINTLN("pci slot %d:%d: unknown device \"%s\"",
229 snum, fnum, emul);
230 goto done;
231 }
232
233 error = 0;
234 si->si_funcs[fnum].fi_name = emul;
235 si->si_funcs[fnum].fi_param = config;
236
237 done:
238 if (error)
239 free(str);
240
241 return (error);
242 }
243
244 void
pci_print_supported_devices()245 pci_print_supported_devices()
246 {
247 struct pci_devemu **pdpp, *pdp;
248
249 SET_FOREACH(pdpp, pci_devemu_set) {
250 pdp = *pdpp;
251 printf("%s\n", pdp->pe_emu);
252 }
253 }
254
255 static int
pci_valid_pba_offset(struct pci_devinst * pi,uint64_t offset)256 pci_valid_pba_offset(struct pci_devinst *pi, uint64_t offset)
257 {
258
259 if (offset < pi->pi_msix.pba_offset)
260 return (0);
261
262 if (offset >= pi->pi_msix.pba_offset + pi->pi_msix.pba_size) {
263 return (0);
264 }
265
266 return (1);
267 }
268
269 int
pci_emul_msix_twrite(struct pci_devinst * pi,uint64_t offset,int size,uint64_t value)270 pci_emul_msix_twrite(struct pci_devinst *pi, uint64_t offset, int size,
271 uint64_t value)
272 {
273 int msix_entry_offset;
274 int tab_index;
275 char *dest;
276
277 /* support only 4 or 8 byte writes */
278 if (size != 4 && size != 8)
279 return (-1);
280
281 /*
282 * Return if table index is beyond what device supports
283 */
284 tab_index = offset / MSIX_TABLE_ENTRY_SIZE;
285 if (tab_index >= pi->pi_msix.table_count)
286 return (-1);
287
288 msix_entry_offset = offset % MSIX_TABLE_ENTRY_SIZE;
289
290 /* support only aligned writes */
291 if ((msix_entry_offset % size) != 0)
292 return (-1);
293
294 dest = (char *)(pi->pi_msix.table + tab_index);
295 dest += msix_entry_offset;
296
297 if (size == 4)
298 *((uint32_t *)dest) = value;
299 else
300 *((uint64_t *)dest) = value;
301
302 return (0);
303 }
304
305 uint64_t
pci_emul_msix_tread(struct pci_devinst * pi,uint64_t offset,int size)306 pci_emul_msix_tread(struct pci_devinst *pi, uint64_t offset, int size)
307 {
308 char *dest;
309 int msix_entry_offset;
310 int tab_index;
311 uint64_t retval = ~0;
312
313 /*
314 * The PCI standard only allows 4 and 8 byte accesses to the MSI-X
315 * table but we also allow 1 byte access to accommodate reads from
316 * ddb.
317 */
318 if (size != 1 && size != 4 && size != 8)
319 return (retval);
320
321 msix_entry_offset = offset % MSIX_TABLE_ENTRY_SIZE;
322
323 /* support only aligned reads */
324 if ((msix_entry_offset % size) != 0) {
325 return (retval);
326 }
327
328 tab_index = offset / MSIX_TABLE_ENTRY_SIZE;
329
330 if (tab_index < pi->pi_msix.table_count) {
331 /* valid MSI-X Table access */
332 dest = (char *)(pi->pi_msix.table + tab_index);
333 dest += msix_entry_offset;
334
335 if (size == 1)
336 retval = *((uint8_t *)dest);
337 else if (size == 4)
338 retval = *((uint32_t *)dest);
339 else
340 retval = *((uint64_t *)dest);
341 } else if (pci_valid_pba_offset(pi, offset)) {
342 /* return 0 for PBA access */
343 retval = 0;
344 }
345
346 return (retval);
347 }
348
349 int
pci_msix_table_bar(struct pci_devinst * pi)350 pci_msix_table_bar(struct pci_devinst *pi)
351 {
352
353 if (pi->pi_msix.table != NULL)
354 return (pi->pi_msix.table_bar);
355 else
356 return (-1);
357 }
358
359 int
pci_msix_pba_bar(struct pci_devinst * pi)360 pci_msix_pba_bar(struct pci_devinst *pi)
361 {
362
363 if (pi->pi_msix.table != NULL)
364 return (pi->pi_msix.pba_bar);
365 else
366 return (-1);
367 }
368
369 static int
pci_emul_io_handler(struct vmctx * ctx,int vcpu,int in,int port,int bytes,uint32_t * eax,void * arg)370 pci_emul_io_handler(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
371 uint32_t *eax, void *arg)
372 {
373 struct pci_devinst *pdi = arg;
374 struct pci_devemu *pe = pdi->pi_d;
375 uint64_t offset;
376 int i;
377
378 for (i = 0; i <= PCI_BARMAX; i++) {
379 if (pdi->pi_bar[i].type == PCIBAR_IO &&
380 port >= pdi->pi_bar[i].addr &&
381 port + bytes <= pdi->pi_bar[i].addr + pdi->pi_bar[i].size) {
382 offset = port - pdi->pi_bar[i].addr;
383 if (in)
384 *eax = (*pe->pe_barread)(ctx, vcpu, pdi, i,
385 offset, bytes);
386 else
387 (*pe->pe_barwrite)(ctx, vcpu, pdi, i, offset,
388 bytes, *eax);
389 return (0);
390 }
391 }
392 return (-1);
393 }
394
395 static int
pci_emul_mem_handler(struct vmctx * ctx,int vcpu,int dir,uint64_t addr,int size,uint64_t * val,void * arg1,long arg2)396 pci_emul_mem_handler(struct vmctx *ctx, int vcpu, int dir, uint64_t addr,
397 int size, uint64_t *val, void *arg1, long arg2)
398 {
399 struct pci_devinst *pdi = arg1;
400 struct pci_devemu *pe = pdi->pi_d;
401 uint64_t offset;
402 int bidx = (int) arg2;
403
404 assert(bidx <= PCI_BARMAX);
405 assert(pdi->pi_bar[bidx].type == PCIBAR_MEM32 ||
406 pdi->pi_bar[bidx].type == PCIBAR_MEM64);
407 assert(addr >= pdi->pi_bar[bidx].addr &&
408 addr + size <= pdi->pi_bar[bidx].addr + pdi->pi_bar[bidx].size);
409
410 offset = addr - pdi->pi_bar[bidx].addr;
411
412 if (dir == MEM_F_WRITE) {
413 if (size == 8) {
414 (*pe->pe_barwrite)(ctx, vcpu, pdi, bidx, offset,
415 4, *val & 0xffffffff);
416 (*pe->pe_barwrite)(ctx, vcpu, pdi, bidx, offset + 4,
417 4, *val >> 32);
418 } else {
419 (*pe->pe_barwrite)(ctx, vcpu, pdi, bidx, offset,
420 size, *val);
421 }
422 } else {
423 if (size == 8) {
424 *val = (*pe->pe_barread)(ctx, vcpu, pdi, bidx,
425 offset, 4);
426 *val |= (*pe->pe_barread)(ctx, vcpu, pdi, bidx,
427 offset + 4, 4) << 32;
428 } else {
429 *val = (*pe->pe_barread)(ctx, vcpu, pdi, bidx,
430 offset, size);
431 }
432 }
433
434 return (0);
435 }
436
437
438 static int
pci_emul_alloc_resource(uint64_t * baseptr,uint64_t limit,uint64_t size,uint64_t * addr)439 pci_emul_alloc_resource(uint64_t *baseptr, uint64_t limit, uint64_t size,
440 uint64_t *addr)
441 {
442 uint64_t base;
443
444 assert((size & (size - 1)) == 0); /* must be a power of 2 */
445
446 base = roundup2(*baseptr, size);
447
448 if (base + size <= limit) {
449 *addr = base;
450 *baseptr = base + size;
451 return (0);
452 } else
453 return (-1);
454 }
455
456 /*
457 * Register (or unregister) the MMIO or I/O region associated with the BAR
458 * register 'idx' of an emulated pci device.
459 */
460 static void
modify_bar_registration(struct pci_devinst * pi,int idx,int registration)461 modify_bar_registration(struct pci_devinst *pi, int idx, int registration)
462 {
463 int error;
464 struct inout_port iop;
465 struct mem_range mr;
466
467 switch (pi->pi_bar[idx].type) {
468 case PCIBAR_IO:
469 bzero(&iop, sizeof(struct inout_port));
470 iop.name = pi->pi_name;
471 iop.port = pi->pi_bar[idx].addr;
472 iop.size = pi->pi_bar[idx].size;
473 if (registration) {
474 iop.flags = IOPORT_F_INOUT;
475 iop.handler = pci_emul_io_handler;
476 iop.arg = pi;
477 error = register_inout(&iop);
478 } else
479 error = unregister_inout(&iop);
480 break;
481 case PCIBAR_MEM32:
482 case PCIBAR_MEM64:
483 bzero(&mr, sizeof(struct mem_range));
484 mr.name = pi->pi_name;
485 mr.base = pi->pi_bar[idx].addr;
486 mr.size = pi->pi_bar[idx].size;
487 if (registration) {
488 mr.flags = MEM_F_RW;
489 mr.handler = pci_emul_mem_handler;
490 mr.arg1 = pi;
491 mr.arg2 = idx;
492 error = register_mem(&mr);
493 } else
494 error = unregister_mem(&mr);
495 break;
496 default:
497 error = EINVAL;
498 break;
499 }
500 assert(error == 0);
501 }
502
503 static void
unregister_bar(struct pci_devinst * pi,int idx)504 unregister_bar(struct pci_devinst *pi, int idx)
505 {
506
507 modify_bar_registration(pi, idx, 0);
508 }
509
510 static void
register_bar(struct pci_devinst * pi,int idx)511 register_bar(struct pci_devinst *pi, int idx)
512 {
513
514 modify_bar_registration(pi, idx, 1);
515 }
516
517 /* Are we decoding i/o port accesses for the emulated pci device? */
518 static int
porten(struct pci_devinst * pi)519 porten(struct pci_devinst *pi)
520 {
521 uint16_t cmd;
522
523 cmd = pci_get_cfgdata16(pi, PCIR_COMMAND);
524
525 return (cmd & PCIM_CMD_PORTEN);
526 }
527
528 /* Are we decoding memory accesses for the emulated pci device? */
529 static int
memen(struct pci_devinst * pi)530 memen(struct pci_devinst *pi)
531 {
532 uint16_t cmd;
533
534 cmd = pci_get_cfgdata16(pi, PCIR_COMMAND);
535
536 return (cmd & PCIM_CMD_MEMEN);
537 }
538
539 /*
540 * Update the MMIO or I/O address that is decoded by the BAR register.
541 *
542 * If the pci device has enabled the address space decoding then intercept
543 * the address range decoded by the BAR register.
544 */
545 static void
update_bar_address(struct pci_devinst * pi,uint64_t addr,int idx,int type)546 update_bar_address(struct pci_devinst *pi, uint64_t addr, int idx, int type)
547 {
548 int decode;
549
550 if (pi->pi_bar[idx].type == PCIBAR_IO)
551 decode = porten(pi);
552 else
553 decode = memen(pi);
554
555 if (decode)
556 unregister_bar(pi, idx);
557
558 switch (type) {
559 case PCIBAR_IO:
560 case PCIBAR_MEM32:
561 pi->pi_bar[idx].addr = addr;
562 break;
563 case PCIBAR_MEM64:
564 pi->pi_bar[idx].addr &= ~0xffffffffUL;
565 pi->pi_bar[idx].addr |= addr;
566 break;
567 case PCIBAR_MEMHI64:
568 pi->pi_bar[idx].addr &= 0xffffffff;
569 pi->pi_bar[idx].addr |= addr;
570 break;
571 default:
572 assert(0);
573 }
574
575 if (decode)
576 register_bar(pi, idx);
577 }
578
579 int
pci_emul_alloc_bar(struct pci_devinst * pdi,int idx,enum pcibar_type type,uint64_t size)580 pci_emul_alloc_bar(struct pci_devinst *pdi, int idx, enum pcibar_type type,
581 uint64_t size)
582 {
583 int error;
584 uint64_t *baseptr, limit, addr, mask, lobits, bar;
585 uint16_t cmd, enbit;
586
587 assert(idx >= 0 && idx <= PCI_BARMAX);
588
589 if ((size & (size - 1)) != 0)
590 size = 1UL << flsl(size); /* round up to a power of 2 */
591
592 /* Enforce minimum BAR sizes required by the PCI standard */
593 if (type == PCIBAR_IO) {
594 if (size < 4)
595 size = 4;
596 } else {
597 if (size < 16)
598 size = 16;
599 }
600
601 switch (type) {
602 case PCIBAR_NONE:
603 baseptr = NULL;
604 addr = mask = lobits = enbit = 0;
605 break;
606 case PCIBAR_IO:
607 baseptr = &pci_emul_iobase;
608 limit = PCI_EMUL_IOLIMIT;
609 mask = PCIM_BAR_IO_BASE;
610 lobits = PCIM_BAR_IO_SPACE;
611 enbit = PCIM_CMD_PORTEN;
612 break;
613 case PCIBAR_MEM64:
614 /*
615 * XXX
616 * Some drivers do not work well if the 64-bit BAR is allocated
617 * above 4GB. Allow for this by allocating small requests under
618 * 4GB unless then allocation size is larger than some arbitrary
619 * number (128MB currently).
620 */
621 if (size > 128 * 1024 * 1024) {
622 baseptr = &pci_emul_membase64;
623 limit = pci_emul_memlim64;
624 mask = PCIM_BAR_MEM_BASE;
625 lobits = PCIM_BAR_MEM_SPACE | PCIM_BAR_MEM_64 |
626 PCIM_BAR_MEM_PREFETCH;
627 } else {
628 baseptr = &pci_emul_membase32;
629 limit = PCI_EMUL_MEMLIMIT32;
630 mask = PCIM_BAR_MEM_BASE;
631 lobits = PCIM_BAR_MEM_SPACE | PCIM_BAR_MEM_64;
632 }
633 enbit = PCIM_CMD_MEMEN;
634 break;
635 case PCIBAR_MEM32:
636 baseptr = &pci_emul_membase32;
637 limit = PCI_EMUL_MEMLIMIT32;
638 mask = PCIM_BAR_MEM_BASE;
639 lobits = PCIM_BAR_MEM_SPACE | PCIM_BAR_MEM_32;
640 enbit = PCIM_CMD_MEMEN;
641 break;
642 default:
643 printf("pci_emul_alloc_base: invalid bar type %d\n", type);
644 assert(0);
645 }
646
647 if (baseptr != NULL) {
648 error = pci_emul_alloc_resource(baseptr, limit, size, &addr);
649 if (error != 0)
650 return (error);
651 }
652
653 pdi->pi_bar[idx].type = type;
654 pdi->pi_bar[idx].addr = addr;
655 pdi->pi_bar[idx].size = size;
656
657 /* Initialize the BAR register in config space */
658 bar = (addr & mask) | lobits;
659 pci_set_cfgdata32(pdi, PCIR_BAR(idx), bar);
660
661 if (type == PCIBAR_MEM64) {
662 assert(idx + 1 <= PCI_BARMAX);
663 pdi->pi_bar[idx + 1].type = PCIBAR_MEMHI64;
664 pci_set_cfgdata32(pdi, PCIR_BAR(idx + 1), bar >> 32);
665 }
666
667 cmd = pci_get_cfgdata16(pdi, PCIR_COMMAND);
668 if ((cmd & enbit) != enbit)
669 pci_set_cfgdata16(pdi, PCIR_COMMAND, cmd | enbit);
670 register_bar(pdi, idx);
671
672 return (0);
673 }
674
675 #define CAP_START_OFFSET 0x40
676 static int
pci_emul_add_capability(struct pci_devinst * pi,u_char * capdata,int caplen)677 pci_emul_add_capability(struct pci_devinst *pi, u_char *capdata, int caplen)
678 {
679 int i, capoff, reallen;
680 uint16_t sts;
681
682 assert(caplen > 0);
683
684 reallen = roundup2(caplen, 4); /* dword aligned */
685
686 sts = pci_get_cfgdata16(pi, PCIR_STATUS);
687 if ((sts & PCIM_STATUS_CAPPRESENT) == 0)
688 capoff = CAP_START_OFFSET;
689 else
690 capoff = pi->pi_capend + 1;
691
692 /* Check if we have enough space */
693 if (capoff + reallen > PCI_REGMAX + 1)
694 return (-1);
695
696 /* Set the previous capability pointer */
697 if ((sts & PCIM_STATUS_CAPPRESENT) == 0) {
698 pci_set_cfgdata8(pi, PCIR_CAP_PTR, capoff);
699 pci_set_cfgdata16(pi, PCIR_STATUS, sts|PCIM_STATUS_CAPPRESENT);
700 } else
701 pci_set_cfgdata8(pi, pi->pi_prevcap + 1, capoff);
702
703 /* Copy the capability */
704 for (i = 0; i < caplen; i++)
705 pci_set_cfgdata8(pi, capoff + i, capdata[i]);
706
707 /* Set the next capability pointer */
708 pci_set_cfgdata8(pi, capoff + 1, 0);
709
710 pi->pi_prevcap = capoff;
711 pi->pi_capend = capoff + reallen - 1;
712 return (0);
713 }
714
715 static struct pci_devemu *
pci_emul_finddev(char * name)716 pci_emul_finddev(char *name)
717 {
718 struct pci_devemu **pdpp, *pdp;
719
720 SET_FOREACH(pdpp, pci_devemu_set) {
721 pdp = *pdpp;
722 if (!strcmp(pdp->pe_emu, name)) {
723 return (pdp);
724 }
725 }
726
727 return (NULL);
728 }
729
730 static int
pci_emul_init(struct vmctx * ctx,struct pci_devemu * pde,int bus,int slot,int func,struct funcinfo * fi)731 pci_emul_init(struct vmctx *ctx, struct pci_devemu *pde, int bus, int slot,
732 int func, struct funcinfo *fi)
733 {
734 struct pci_devinst *pdi;
735 int err;
736
737 pdi = calloc(1, sizeof(struct pci_devinst));
738
739 pdi->pi_vmctx = ctx;
740 pdi->pi_bus = bus;
741 pdi->pi_slot = slot;
742 pdi->pi_func = func;
743 pthread_mutex_init(&pdi->pi_lintr.lock, NULL);
744 pdi->pi_lintr.pin = 0;
745 pdi->pi_lintr.state = IDLE;
746 pdi->pi_lintr.pirq_pin = 0;
747 pdi->pi_lintr.ioapic_irq = 0;
748 pdi->pi_d = pde;
749 snprintf(pdi->pi_name, PI_NAMESZ, "%s-pci-%d", pde->pe_emu, slot);
750
751 /* Disable legacy interrupts */
752 pci_set_cfgdata8(pdi, PCIR_INTLINE, 255);
753 pci_set_cfgdata8(pdi, PCIR_INTPIN, 0);
754
755 pci_set_cfgdata8(pdi, PCIR_COMMAND, PCIM_CMD_BUSMASTEREN);
756
757 err = (*pde->pe_init)(ctx, pdi, fi->fi_param);
758 if (err == 0)
759 fi->fi_devi = pdi;
760 else
761 free(pdi);
762
763 return (err);
764 }
765
766 void
pci_populate_msicap(struct msicap * msicap,int msgnum,int nextptr)767 pci_populate_msicap(struct msicap *msicap, int msgnum, int nextptr)
768 {
769 int mmc;
770
771 /* Number of msi messages must be a power of 2 between 1 and 32 */
772 assert((msgnum & (msgnum - 1)) == 0 && msgnum >= 1 && msgnum <= 32);
773 mmc = ffs(msgnum) - 1;
774
775 bzero(msicap, sizeof(struct msicap));
776 msicap->capid = PCIY_MSI;
777 msicap->nextptr = nextptr;
778 msicap->msgctrl = PCIM_MSICTRL_64BIT | (mmc << 1);
779 }
780
781 int
pci_emul_add_msicap(struct pci_devinst * pi,int msgnum)782 pci_emul_add_msicap(struct pci_devinst *pi, int msgnum)
783 {
784 struct msicap msicap;
785
786 pci_populate_msicap(&msicap, msgnum, 0);
787
788 return (pci_emul_add_capability(pi, (u_char *)&msicap, sizeof(msicap)));
789 }
790
791 static void
pci_populate_msixcap(struct msixcap * msixcap,int msgnum,int barnum,uint32_t msix_tab_size)792 pci_populate_msixcap(struct msixcap *msixcap, int msgnum, int barnum,
793 uint32_t msix_tab_size)
794 {
795
796 assert(msix_tab_size % 4096 == 0);
797
798 bzero(msixcap, sizeof(struct msixcap));
799 msixcap->capid = PCIY_MSIX;
800
801 /*
802 * Message Control Register, all fields set to
803 * zero except for the Table Size.
804 * Note: Table size N is encoded as N-1
805 */
806 msixcap->msgctrl = msgnum - 1;
807
808 /*
809 * MSI-X BAR setup:
810 * - MSI-X table start at offset 0
811 * - PBA table starts at a 4K aligned offset after the MSI-X table
812 */
813 msixcap->table_info = barnum & PCIM_MSIX_BIR_MASK;
814 msixcap->pba_info = msix_tab_size | (barnum & PCIM_MSIX_BIR_MASK);
815 }
816
817 static void
pci_msix_table_init(struct pci_devinst * pi,int table_entries)818 pci_msix_table_init(struct pci_devinst *pi, int table_entries)
819 {
820 int i, table_size;
821
822 assert(table_entries > 0);
823 assert(table_entries <= MAX_MSIX_TABLE_ENTRIES);
824
825 table_size = table_entries * MSIX_TABLE_ENTRY_SIZE;
826 pi->pi_msix.table = calloc(1, table_size);
827
828 /* set mask bit of vector control register */
829 for (i = 0; i < table_entries; i++)
830 pi->pi_msix.table[i].vector_control |= PCIM_MSIX_VCTRL_MASK;
831 }
832
833 int
pci_emul_add_msixcap(struct pci_devinst * pi,int msgnum,int barnum)834 pci_emul_add_msixcap(struct pci_devinst *pi, int msgnum, int barnum)
835 {
836 uint32_t tab_size;
837 struct msixcap msixcap;
838
839 assert(msgnum >= 1 && msgnum <= MAX_MSIX_TABLE_ENTRIES);
840 assert(barnum >= 0 && barnum <= PCIR_MAX_BAR_0);
841
842 tab_size = msgnum * MSIX_TABLE_ENTRY_SIZE;
843
844 /* Align table size to nearest 4K */
845 tab_size = roundup2(tab_size, 4096);
846
847 pi->pi_msix.table_bar = barnum;
848 pi->pi_msix.pba_bar = barnum;
849 pi->pi_msix.table_offset = 0;
850 pi->pi_msix.table_count = msgnum;
851 pi->pi_msix.pba_offset = tab_size;
852 pi->pi_msix.pba_size = PBA_SIZE(msgnum);
853
854 pci_msix_table_init(pi, msgnum);
855
856 pci_populate_msixcap(&msixcap, msgnum, barnum, tab_size);
857
858 /* allocate memory for MSI-X Table and PBA */
859 pci_emul_alloc_bar(pi, barnum, PCIBAR_MEM32,
860 tab_size + pi->pi_msix.pba_size);
861
862 return (pci_emul_add_capability(pi, (u_char *)&msixcap,
863 sizeof(msixcap)));
864 }
865
866 static void
msixcap_cfgwrite(struct pci_devinst * pi,int capoff,int offset,int bytes,uint32_t val)867 msixcap_cfgwrite(struct pci_devinst *pi, int capoff, int offset,
868 int bytes, uint32_t val)
869 {
870 uint16_t msgctrl, rwmask;
871 int off;
872
873 off = offset - capoff;
874 /* Message Control Register */
875 if (off == 2 && bytes == 2) {
876 rwmask = PCIM_MSIXCTRL_MSIX_ENABLE | PCIM_MSIXCTRL_FUNCTION_MASK;
877 msgctrl = pci_get_cfgdata16(pi, offset);
878 msgctrl &= ~rwmask;
879 msgctrl |= val & rwmask;
880 val = msgctrl;
881
882 pi->pi_msix.enabled = val & PCIM_MSIXCTRL_MSIX_ENABLE;
883 pi->pi_msix.function_mask = val & PCIM_MSIXCTRL_FUNCTION_MASK;
884 pci_lintr_update(pi);
885 }
886
887 CFGWRITE(pi, offset, val, bytes);
888 }
889
890 static void
msicap_cfgwrite(struct pci_devinst * pi,int capoff,int offset,int bytes,uint32_t val)891 msicap_cfgwrite(struct pci_devinst *pi, int capoff, int offset,
892 int bytes, uint32_t val)
893 {
894 uint16_t msgctrl, rwmask, msgdata, mme;
895 uint32_t addrlo;
896
897 /*
898 * If guest is writing to the message control register make sure
899 * we do not overwrite read-only fields.
900 */
901 if ((offset - capoff) == 2 && bytes == 2) {
902 rwmask = PCIM_MSICTRL_MME_MASK | PCIM_MSICTRL_MSI_ENABLE;
903 msgctrl = pci_get_cfgdata16(pi, offset);
904 msgctrl &= ~rwmask;
905 msgctrl |= val & rwmask;
906 val = msgctrl;
907 }
908 CFGWRITE(pi, offset, val, bytes);
909
910 msgctrl = pci_get_cfgdata16(pi, capoff + 2);
911 addrlo = pci_get_cfgdata32(pi, capoff + 4);
912 if (msgctrl & PCIM_MSICTRL_64BIT)
913 msgdata = pci_get_cfgdata16(pi, capoff + 12);
914 else
915 msgdata = pci_get_cfgdata16(pi, capoff + 8);
916
917 mme = msgctrl & PCIM_MSICTRL_MME_MASK;
918 pi->pi_msi.enabled = msgctrl & PCIM_MSICTRL_MSI_ENABLE ? 1 : 0;
919 if (pi->pi_msi.enabled) {
920 pi->pi_msi.addr = addrlo;
921 pi->pi_msi.msg_data = msgdata;
922 pi->pi_msi.maxmsgnum = 1 << (mme >> 4);
923 } else {
924 pi->pi_msi.maxmsgnum = 0;
925 }
926 pci_lintr_update(pi);
927 }
928
929 void
pciecap_cfgwrite(struct pci_devinst * pi,int capoff,int offset,int bytes,uint32_t val)930 pciecap_cfgwrite(struct pci_devinst *pi, int capoff, int offset,
931 int bytes, uint32_t val)
932 {
933
934 /* XXX don't write to the readonly parts */
935 CFGWRITE(pi, offset, val, bytes);
936 }
937
938 #define PCIECAP_VERSION 0x2
939 int
pci_emul_add_pciecap(struct pci_devinst * pi,int type)940 pci_emul_add_pciecap(struct pci_devinst *pi, int type)
941 {
942 int err;
943 struct pciecap pciecap;
944
945 bzero(&pciecap, sizeof(pciecap));
946
947 /*
948 * Use the integrated endpoint type for endpoints on a root complex bus.
949 *
950 * NB: bhyve currently only supports a single PCI bus that is the root
951 * complex bus, so all endpoints are integrated.
952 */
953 if ((type == PCIEM_TYPE_ENDPOINT) && (pi->pi_bus == 0))
954 type = PCIEM_TYPE_ROOT_INT_EP;
955
956 pciecap.capid = PCIY_EXPRESS;
957 pciecap.pcie_capabilities = PCIECAP_VERSION | type;
958 if (type != PCIEM_TYPE_ROOT_INT_EP) {
959 pciecap.link_capabilities = 0x411; /* gen1, x1 */
960 pciecap.link_status = 0x11; /* gen1, x1 */
961 }
962
963 err = pci_emul_add_capability(pi, (u_char *)&pciecap, sizeof(pciecap));
964 return (err);
965 }
966
967 /*
968 * This function assumes that 'coff' is in the capabilities region of the
969 * config space. A capoff parameter of zero will force a search for the
970 * offset and type.
971 */
972 void
pci_emul_capwrite(struct pci_devinst * pi,int offset,int bytes,uint32_t val,uint8_t capoff,int capid)973 pci_emul_capwrite(struct pci_devinst *pi, int offset, int bytes, uint32_t val,
974 uint8_t capoff, int capid)
975 {
976 uint8_t nextoff;
977
978 /* Do not allow un-aligned writes */
979 if ((offset & (bytes - 1)) != 0)
980 return;
981
982 if (capoff == 0) {
983 /* Find the capability that we want to update */
984 capoff = CAP_START_OFFSET;
985 while (1) {
986 nextoff = pci_get_cfgdata8(pi, capoff + 1);
987 if (nextoff == 0)
988 break;
989 if (offset >= capoff && offset < nextoff)
990 break;
991
992 capoff = nextoff;
993 }
994 assert(offset >= capoff);
995 capid = pci_get_cfgdata8(pi, capoff);
996 }
997
998 /*
999 * Capability ID and Next Capability Pointer are readonly.
1000 * However, some o/s's do 4-byte writes that include these.
1001 * For this case, trim the write back to 2 bytes and adjust
1002 * the data.
1003 */
1004 if (offset == capoff || offset == capoff + 1) {
1005 if (offset == capoff && bytes == 4) {
1006 bytes = 2;
1007 offset += 2;
1008 val >>= 16;
1009 } else
1010 return;
1011 }
1012
1013 switch (capid) {
1014 case PCIY_MSI:
1015 msicap_cfgwrite(pi, capoff, offset, bytes, val);
1016 break;
1017 case PCIY_MSIX:
1018 msixcap_cfgwrite(pi, capoff, offset, bytes, val);
1019 break;
1020 case PCIY_EXPRESS:
1021 pciecap_cfgwrite(pi, capoff, offset, bytes, val);
1022 break;
1023 default:
1024 break;
1025 }
1026 }
1027
1028 static int
pci_emul_iscap(struct pci_devinst * pi,int offset)1029 pci_emul_iscap(struct pci_devinst *pi, int offset)
1030 {
1031 uint16_t sts;
1032
1033 sts = pci_get_cfgdata16(pi, PCIR_STATUS);
1034 if ((sts & PCIM_STATUS_CAPPRESENT) != 0) {
1035 if (offset >= CAP_START_OFFSET && offset <= pi->pi_capend)
1036 return (1);
1037 }
1038 return (0);
1039 }
1040
1041 static int
pci_emul_fallback_handler(struct vmctx * ctx,int vcpu,int dir,uint64_t addr,int size,uint64_t * val,void * arg1,long arg2)1042 pci_emul_fallback_handler(struct vmctx *ctx, int vcpu, int dir, uint64_t addr,
1043 int size, uint64_t *val, void *arg1, long arg2)
1044 {
1045 /*
1046 * Ignore writes; return 0xff's for reads. The mem read code
1047 * will take care of truncating to the correct size.
1048 */
1049 if (dir == MEM_F_READ) {
1050 *val = 0xffffffffffffffff;
1051 }
1052
1053 return (0);
1054 }
1055
1056 static int
pci_emul_ecfg_handler(struct vmctx * ctx,int vcpu,int dir,uint64_t addr,int bytes,uint64_t * val,void * arg1,long arg2)1057 pci_emul_ecfg_handler(struct vmctx *ctx, int vcpu, int dir, uint64_t addr,
1058 int bytes, uint64_t *val, void *arg1, long arg2)
1059 {
1060 int bus, slot, func, coff, in;
1061
1062 coff = addr & 0xfff;
1063 func = (addr >> 12) & 0x7;
1064 slot = (addr >> 15) & 0x1f;
1065 bus = (addr >> 20) & 0xff;
1066 in = (dir == MEM_F_READ);
1067 if (in)
1068 *val = ~0UL;
1069 pci_cfgrw(ctx, vcpu, in, bus, slot, func, coff, bytes, (uint32_t *)val);
1070 return (0);
1071 }
1072
1073 uint64_t
pci_ecfg_base(void)1074 pci_ecfg_base(void)
1075 {
1076
1077 return (PCI_EMUL_ECFG_BASE);
1078 }
1079
1080 #define BUSIO_ROUNDUP 32
1081 #define BUSMEM_ROUNDUP (1024 * 1024)
1082
1083 int
init_pci(struct vmctx * ctx)1084 init_pci(struct vmctx *ctx)
1085 {
1086 struct mem_range mr;
1087 struct pci_devemu *pde;
1088 struct businfo *bi;
1089 struct slotinfo *si;
1090 struct funcinfo *fi;
1091 size_t lowmem;
1092 uint64_t cpu_maxphysaddr, pci_emul_memresv64;
1093 u_int regs[4];
1094 int bus, slot, func, error;
1095
1096 pci_emul_iobase = PCI_EMUL_IOBASE;
1097 pci_emul_membase32 = vm_get_lowmem_limit(ctx);
1098
1099 do_cpuid(0x80000008, regs);
1100 cpu_maxphysaddr = 1ULL << (regs[0] & 0xff);
1101 if (cpu_maxphysaddr > VM_MAXUSER_ADDRESS)
1102 cpu_maxphysaddr = VM_MAXUSER_ADDRESS;
1103 pci_emul_memresv64 = cpu_maxphysaddr / 4;
1104 /*
1105 * Max power of 2 that is less then
1106 * cpu_maxphysaddr - pci_emul_memresv64.
1107 */
1108 pci_emul_membase64 = 1ULL << (flsl(cpu_maxphysaddr -
1109 pci_emul_memresv64) - 1);
1110 pci_emul_memlim64 = cpu_maxphysaddr;
1111
1112 for (bus = 0; bus < MAXBUSES; bus++) {
1113 if ((bi = pci_businfo[bus]) == NULL)
1114 continue;
1115 /*
1116 * Keep track of the i/o and memory resources allocated to
1117 * this bus.
1118 */
1119 bi->iobase = pci_emul_iobase;
1120 bi->membase32 = pci_emul_membase32;
1121 bi->membase64 = pci_emul_membase64;
1122
1123 for (slot = 0; slot < MAXSLOTS; slot++) {
1124 si = &bi->slotinfo[slot];
1125 for (func = 0; func < MAXFUNCS; func++) {
1126 fi = &si->si_funcs[func];
1127 if (fi->fi_name == NULL)
1128 continue;
1129 pde = pci_emul_finddev(fi->fi_name);
1130 assert(pde != NULL);
1131 error = pci_emul_init(ctx, pde, bus, slot,
1132 func, fi);
1133 if (error)
1134 return (error);
1135 }
1136 }
1137
1138 /*
1139 * Add some slop to the I/O and memory resources decoded by
1140 * this bus to give a guest some flexibility if it wants to
1141 * reprogram the BARs.
1142 */
1143 pci_emul_iobase += BUSIO_ROUNDUP;
1144 pci_emul_iobase = roundup2(pci_emul_iobase, BUSIO_ROUNDUP);
1145 bi->iolimit = pci_emul_iobase;
1146
1147 pci_emul_membase32 += BUSMEM_ROUNDUP;
1148 pci_emul_membase32 = roundup2(pci_emul_membase32,
1149 BUSMEM_ROUNDUP);
1150 bi->memlimit32 = pci_emul_membase32;
1151
1152 pci_emul_membase64 += BUSMEM_ROUNDUP;
1153 pci_emul_membase64 = roundup2(pci_emul_membase64,
1154 BUSMEM_ROUNDUP);
1155 bi->memlimit64 = pci_emul_membase64;
1156 }
1157
1158 /*
1159 * PCI backends are initialized before routing INTx interrupts
1160 * so that LPC devices are able to reserve ISA IRQs before
1161 * routing PIRQ pins.
1162 */
1163 for (bus = 0; bus < MAXBUSES; bus++) {
1164 if ((bi = pci_businfo[bus]) == NULL)
1165 continue;
1166
1167 for (slot = 0; slot < MAXSLOTS; slot++) {
1168 si = &bi->slotinfo[slot];
1169 for (func = 0; func < MAXFUNCS; func++) {
1170 fi = &si->si_funcs[func];
1171 if (fi->fi_devi == NULL)
1172 continue;
1173 pci_lintr_route(fi->fi_devi);
1174 }
1175 }
1176 }
1177 lpc_pirq_routed();
1178
1179 /*
1180 * The guest physical memory map looks like the following:
1181 * [0, lowmem) guest system memory
1182 * [lowmem, lowmem_limit) memory hole (may be absent)
1183 * [lowmem_limit, 0xE0000000) PCI hole (32-bit BAR allocation)
1184 * [0xE0000000, 0xF0000000) PCI extended config window
1185 * [0xF0000000, 4GB) LAPIC, IOAPIC, HPET, firmware
1186 * [4GB, 4GB + highmem)
1187 */
1188
1189 /*
1190 * Accesses to memory addresses that are not allocated to system
1191 * memory or PCI devices return 0xff's.
1192 */
1193 lowmem = vm_get_lowmem_size(ctx);
1194 bzero(&mr, sizeof(struct mem_range));
1195 mr.name = "PCI hole";
1196 mr.flags = MEM_F_RW | MEM_F_IMMUTABLE;
1197 mr.base = lowmem;
1198 mr.size = (4ULL * 1024 * 1024 * 1024) - lowmem;
1199 mr.handler = pci_emul_fallback_handler;
1200 error = register_mem_fallback(&mr);
1201 assert(error == 0);
1202
1203 /* PCI extended config space */
1204 bzero(&mr, sizeof(struct mem_range));
1205 mr.name = "PCI ECFG";
1206 mr.flags = MEM_F_RW | MEM_F_IMMUTABLE;
1207 mr.base = PCI_EMUL_ECFG_BASE;
1208 mr.size = PCI_EMUL_ECFG_SIZE;
1209 mr.handler = pci_emul_ecfg_handler;
1210 error = register_mem(&mr);
1211 assert(error == 0);
1212
1213 return (0);
1214 }
1215
1216 static void
pci_apic_prt_entry(int bus,int slot,int pin,int pirq_pin,int ioapic_irq,void * arg)1217 pci_apic_prt_entry(int bus, int slot, int pin, int pirq_pin, int ioapic_irq,
1218 void *arg)
1219 {
1220
1221 dsdt_line(" Package ()");
1222 dsdt_line(" {");
1223 dsdt_line(" 0x%X,", slot << 16 | 0xffff);
1224 dsdt_line(" 0x%02X,", pin - 1);
1225 dsdt_line(" Zero,");
1226 dsdt_line(" 0x%X", ioapic_irq);
1227 dsdt_line(" },");
1228 }
1229
1230 static void
pci_pirq_prt_entry(int bus,int slot,int pin,int pirq_pin,int ioapic_irq,void * arg)1231 pci_pirq_prt_entry(int bus, int slot, int pin, int pirq_pin, int ioapic_irq,
1232 void *arg)
1233 {
1234 char *name;
1235
1236 name = lpc_pirq_name(pirq_pin);
1237 if (name == NULL)
1238 return;
1239 dsdt_line(" Package ()");
1240 dsdt_line(" {");
1241 dsdt_line(" 0x%X,", slot << 16 | 0xffff);
1242 dsdt_line(" 0x%02X,", pin - 1);
1243 dsdt_line(" %s,", name);
1244 dsdt_line(" 0x00");
1245 dsdt_line(" },");
1246 free(name);
1247 }
1248
1249 /*
1250 * A bhyve virtual machine has a flat PCI hierarchy with a root port
1251 * corresponding to each PCI bus.
1252 */
1253 static void
pci_bus_write_dsdt(int bus)1254 pci_bus_write_dsdt(int bus)
1255 {
1256 struct businfo *bi;
1257 struct slotinfo *si;
1258 struct pci_devinst *pi;
1259 int count, func, slot;
1260
1261 /*
1262 * If there are no devices on this 'bus' then just return.
1263 */
1264 if ((bi = pci_businfo[bus]) == NULL) {
1265 /*
1266 * Bus 0 is special because it decodes the I/O ports used
1267 * for PCI config space access even if there are no devices
1268 * on it.
1269 */
1270 if (bus != 0)
1271 return;
1272 }
1273
1274 dsdt_line(" Device (PC%02X)", bus);
1275 dsdt_line(" {");
1276 dsdt_line(" Name (_HID, EisaId (\"PNP0A03\"))");
1277
1278 dsdt_line(" Method (_BBN, 0, NotSerialized)");
1279 dsdt_line(" {");
1280 dsdt_line(" Return (0x%08X)", bus);
1281 dsdt_line(" }");
1282 dsdt_line(" Name (_CRS, ResourceTemplate ()");
1283 dsdt_line(" {");
1284 dsdt_line(" WordBusNumber (ResourceProducer, MinFixed, "
1285 "MaxFixed, PosDecode,");
1286 dsdt_line(" 0x0000, // Granularity");
1287 dsdt_line(" 0x%04X, // Range Minimum", bus);
1288 dsdt_line(" 0x%04X, // Range Maximum", bus);
1289 dsdt_line(" 0x0000, // Translation Offset");
1290 dsdt_line(" 0x0001, // Length");
1291 dsdt_line(" ,, )");
1292
1293 if (bus == 0) {
1294 dsdt_indent(3);
1295 dsdt_fixed_ioport(0xCF8, 8);
1296 dsdt_unindent(3);
1297
1298 dsdt_line(" WordIO (ResourceProducer, MinFixed, MaxFixed, "
1299 "PosDecode, EntireRange,");
1300 dsdt_line(" 0x0000, // Granularity");
1301 dsdt_line(" 0x0000, // Range Minimum");
1302 dsdt_line(" 0x0CF7, // Range Maximum");
1303 dsdt_line(" 0x0000, // Translation Offset");
1304 dsdt_line(" 0x0CF8, // Length");
1305 dsdt_line(" ,, , TypeStatic)");
1306
1307 dsdt_line(" WordIO (ResourceProducer, MinFixed, MaxFixed, "
1308 "PosDecode, EntireRange,");
1309 dsdt_line(" 0x0000, // Granularity");
1310 dsdt_line(" 0x0D00, // Range Minimum");
1311 dsdt_line(" 0x%04X, // Range Maximum",
1312 PCI_EMUL_IOBASE - 1);
1313 dsdt_line(" 0x0000, // Translation Offset");
1314 dsdt_line(" 0x%04X, // Length",
1315 PCI_EMUL_IOBASE - 0x0D00);
1316 dsdt_line(" ,, , TypeStatic)");
1317
1318 if (bi == NULL) {
1319 dsdt_line(" })");
1320 goto done;
1321 }
1322 }
1323 assert(bi != NULL);
1324
1325 /* i/o window */
1326 dsdt_line(" WordIO (ResourceProducer, MinFixed, MaxFixed, "
1327 "PosDecode, EntireRange,");
1328 dsdt_line(" 0x0000, // Granularity");
1329 dsdt_line(" 0x%04X, // Range Minimum", bi->iobase);
1330 dsdt_line(" 0x%04X, // Range Maximum",
1331 bi->iolimit - 1);
1332 dsdt_line(" 0x0000, // Translation Offset");
1333 dsdt_line(" 0x%04X, // Length",
1334 bi->iolimit - bi->iobase);
1335 dsdt_line(" ,, , TypeStatic)");
1336
1337 /* mmio window (32-bit) */
1338 dsdt_line(" DWordMemory (ResourceProducer, PosDecode, "
1339 "MinFixed, MaxFixed, NonCacheable, ReadWrite,");
1340 dsdt_line(" 0x00000000, // Granularity");
1341 dsdt_line(" 0x%08X, // Range Minimum\n", bi->membase32);
1342 dsdt_line(" 0x%08X, // Range Maximum\n",
1343 bi->memlimit32 - 1);
1344 dsdt_line(" 0x00000000, // Translation Offset");
1345 dsdt_line(" 0x%08X, // Length\n",
1346 bi->memlimit32 - bi->membase32);
1347 dsdt_line(" ,, , AddressRangeMemory, TypeStatic)");
1348
1349 /* mmio window (64-bit) */
1350 dsdt_line(" QWordMemory (ResourceProducer, PosDecode, "
1351 "MinFixed, MaxFixed, NonCacheable, ReadWrite,");
1352 dsdt_line(" 0x0000000000000000, // Granularity");
1353 dsdt_line(" 0x%016lX, // Range Minimum\n", bi->membase64);
1354 dsdt_line(" 0x%016lX, // Range Maximum\n",
1355 bi->memlimit64 - 1);
1356 dsdt_line(" 0x0000000000000000, // Translation Offset");
1357 dsdt_line(" 0x%016lX, // Length\n",
1358 bi->memlimit64 - bi->membase64);
1359 dsdt_line(" ,, , AddressRangeMemory, TypeStatic)");
1360 dsdt_line(" })");
1361
1362 count = pci_count_lintr(bus);
1363 if (count != 0) {
1364 dsdt_indent(2);
1365 dsdt_line("Name (PPRT, Package ()");
1366 dsdt_line("{");
1367 pci_walk_lintr(bus, pci_pirq_prt_entry, NULL);
1368 dsdt_line("})");
1369 dsdt_line("Name (APRT, Package ()");
1370 dsdt_line("{");
1371 pci_walk_lintr(bus, pci_apic_prt_entry, NULL);
1372 dsdt_line("})");
1373 dsdt_line("Method (_PRT, 0, NotSerialized)");
1374 dsdt_line("{");
1375 dsdt_line(" If (PICM)");
1376 dsdt_line(" {");
1377 dsdt_line(" Return (APRT)");
1378 dsdt_line(" }");
1379 dsdt_line(" Else");
1380 dsdt_line(" {");
1381 dsdt_line(" Return (PPRT)");
1382 dsdt_line(" }");
1383 dsdt_line("}");
1384 dsdt_unindent(2);
1385 }
1386
1387 dsdt_indent(2);
1388 for (slot = 0; slot < MAXSLOTS; slot++) {
1389 si = &bi->slotinfo[slot];
1390 for (func = 0; func < MAXFUNCS; func++) {
1391 pi = si->si_funcs[func].fi_devi;
1392 if (pi != NULL && pi->pi_d->pe_write_dsdt != NULL)
1393 pi->pi_d->pe_write_dsdt(pi);
1394 }
1395 }
1396 dsdt_unindent(2);
1397 done:
1398 dsdt_line(" }");
1399 }
1400
1401 void
pci_write_dsdt(void)1402 pci_write_dsdt(void)
1403 {
1404 int bus;
1405
1406 dsdt_indent(1);
1407 dsdt_line("Name (PICM, 0x00)");
1408 dsdt_line("Method (_PIC, 1, NotSerialized)");
1409 dsdt_line("{");
1410 dsdt_line(" Store (Arg0, PICM)");
1411 dsdt_line("}");
1412 dsdt_line("");
1413 dsdt_line("Scope (_SB)");
1414 dsdt_line("{");
1415 for (bus = 0; bus < MAXBUSES; bus++)
1416 pci_bus_write_dsdt(bus);
1417 dsdt_line("}");
1418 dsdt_unindent(1);
1419 }
1420
1421 int
pci_bus_configured(int bus)1422 pci_bus_configured(int bus)
1423 {
1424 assert(bus >= 0 && bus < MAXBUSES);
1425 return (pci_businfo[bus] != NULL);
1426 }
1427
1428 int
pci_msi_enabled(struct pci_devinst * pi)1429 pci_msi_enabled(struct pci_devinst *pi)
1430 {
1431 return (pi->pi_msi.enabled);
1432 }
1433
1434 int
pci_msi_maxmsgnum(struct pci_devinst * pi)1435 pci_msi_maxmsgnum(struct pci_devinst *pi)
1436 {
1437 if (pi->pi_msi.enabled)
1438 return (pi->pi_msi.maxmsgnum);
1439 else
1440 return (0);
1441 }
1442
1443 int
pci_msix_enabled(struct pci_devinst * pi)1444 pci_msix_enabled(struct pci_devinst *pi)
1445 {
1446
1447 return (pi->pi_msix.enabled && !pi->pi_msi.enabled);
1448 }
1449
1450 void
pci_generate_msix(struct pci_devinst * pi,int index)1451 pci_generate_msix(struct pci_devinst *pi, int index)
1452 {
1453 struct msix_table_entry *mte;
1454
1455 if (!pci_msix_enabled(pi))
1456 return;
1457
1458 if (pi->pi_msix.function_mask)
1459 return;
1460
1461 if (index >= pi->pi_msix.table_count)
1462 return;
1463
1464 mte = &pi->pi_msix.table[index];
1465 if ((mte->vector_control & PCIM_MSIX_VCTRL_MASK) == 0) {
1466 /* XXX Set PBA bit if interrupt is disabled */
1467 vm_lapic_msi(pi->pi_vmctx, mte->addr, mte->msg_data);
1468 }
1469 }
1470
1471 void
pci_generate_msi(struct pci_devinst * pi,int index)1472 pci_generate_msi(struct pci_devinst *pi, int index)
1473 {
1474
1475 if (pci_msi_enabled(pi) && index < pci_msi_maxmsgnum(pi)) {
1476 vm_lapic_msi(pi->pi_vmctx, pi->pi_msi.addr,
1477 pi->pi_msi.msg_data + index);
1478 }
1479 }
1480
1481 static bool
pci_lintr_permitted(struct pci_devinst * pi)1482 pci_lintr_permitted(struct pci_devinst *pi)
1483 {
1484 uint16_t cmd;
1485
1486 cmd = pci_get_cfgdata16(pi, PCIR_COMMAND);
1487 return (!(pi->pi_msi.enabled || pi->pi_msix.enabled ||
1488 (cmd & PCIM_CMD_INTxDIS)));
1489 }
1490
1491 void
pci_lintr_request(struct pci_devinst * pi)1492 pci_lintr_request(struct pci_devinst *pi)
1493 {
1494 struct businfo *bi;
1495 struct slotinfo *si;
1496 int bestpin, bestcount, pin;
1497
1498 bi = pci_businfo[pi->pi_bus];
1499 assert(bi != NULL);
1500
1501 /*
1502 * Just allocate a pin from our slot. The pin will be
1503 * assigned IRQs later when interrupts are routed.
1504 */
1505 si = &bi->slotinfo[pi->pi_slot];
1506 bestpin = 0;
1507 bestcount = si->si_intpins[0].ii_count;
1508 for (pin = 1; pin < 4; pin++) {
1509 if (si->si_intpins[pin].ii_count < bestcount) {
1510 bestpin = pin;
1511 bestcount = si->si_intpins[pin].ii_count;
1512 }
1513 }
1514
1515 si->si_intpins[bestpin].ii_count++;
1516 pi->pi_lintr.pin = bestpin + 1;
1517 pci_set_cfgdata8(pi, PCIR_INTPIN, bestpin + 1);
1518 }
1519
1520 static void
pci_lintr_route(struct pci_devinst * pi)1521 pci_lintr_route(struct pci_devinst *pi)
1522 {
1523 struct businfo *bi;
1524 struct intxinfo *ii;
1525
1526 if (pi->pi_lintr.pin == 0)
1527 return;
1528
1529 bi = pci_businfo[pi->pi_bus];
1530 assert(bi != NULL);
1531 ii = &bi->slotinfo[pi->pi_slot].si_intpins[pi->pi_lintr.pin - 1];
1532
1533 /*
1534 * Attempt to allocate an I/O APIC pin for this intpin if one
1535 * is not yet assigned.
1536 */
1537 if (ii->ii_ioapic_irq == 0)
1538 ii->ii_ioapic_irq = ioapic_pci_alloc_irq(pi);
1539 assert(ii->ii_ioapic_irq > 0);
1540
1541 /*
1542 * Attempt to allocate a PIRQ pin for this intpin if one is
1543 * not yet assigned.
1544 */
1545 if (ii->ii_pirq_pin == 0)
1546 ii->ii_pirq_pin = pirq_alloc_pin(pi);
1547 assert(ii->ii_pirq_pin > 0);
1548
1549 pi->pi_lintr.ioapic_irq = ii->ii_ioapic_irq;
1550 pi->pi_lintr.pirq_pin = ii->ii_pirq_pin;
1551 pci_set_cfgdata8(pi, PCIR_INTLINE, pirq_irq(ii->ii_pirq_pin));
1552 }
1553
1554 void
pci_lintr_assert(struct pci_devinst * pi)1555 pci_lintr_assert(struct pci_devinst *pi)
1556 {
1557
1558 assert(pi->pi_lintr.pin > 0);
1559
1560 pthread_mutex_lock(&pi->pi_lintr.lock);
1561 if (pi->pi_lintr.state == IDLE) {
1562 if (pci_lintr_permitted(pi)) {
1563 pi->pi_lintr.state = ASSERTED;
1564 pci_irq_assert(pi);
1565 } else
1566 pi->pi_lintr.state = PENDING;
1567 }
1568 pthread_mutex_unlock(&pi->pi_lintr.lock);
1569 }
1570
1571 void
pci_lintr_deassert(struct pci_devinst * pi)1572 pci_lintr_deassert(struct pci_devinst *pi)
1573 {
1574
1575 assert(pi->pi_lintr.pin > 0);
1576
1577 pthread_mutex_lock(&pi->pi_lintr.lock);
1578 if (pi->pi_lintr.state == ASSERTED) {
1579 pi->pi_lintr.state = IDLE;
1580 pci_irq_deassert(pi);
1581 } else if (pi->pi_lintr.state == PENDING)
1582 pi->pi_lintr.state = IDLE;
1583 pthread_mutex_unlock(&pi->pi_lintr.lock);
1584 }
1585
1586 static void
pci_lintr_update(struct pci_devinst * pi)1587 pci_lintr_update(struct pci_devinst *pi)
1588 {
1589
1590 pthread_mutex_lock(&pi->pi_lintr.lock);
1591 if (pi->pi_lintr.state == ASSERTED && !pci_lintr_permitted(pi)) {
1592 pci_irq_deassert(pi);
1593 pi->pi_lintr.state = PENDING;
1594 } else if (pi->pi_lintr.state == PENDING && pci_lintr_permitted(pi)) {
1595 pi->pi_lintr.state = ASSERTED;
1596 pci_irq_assert(pi);
1597 }
1598 pthread_mutex_unlock(&pi->pi_lintr.lock);
1599 }
1600
1601 int
pci_count_lintr(int bus)1602 pci_count_lintr(int bus)
1603 {
1604 int count, slot, pin;
1605 struct slotinfo *slotinfo;
1606
1607 count = 0;
1608 if (pci_businfo[bus] != NULL) {
1609 for (slot = 0; slot < MAXSLOTS; slot++) {
1610 slotinfo = &pci_businfo[bus]->slotinfo[slot];
1611 for (pin = 0; pin < 4; pin++) {
1612 if (slotinfo->si_intpins[pin].ii_count != 0)
1613 count++;
1614 }
1615 }
1616 }
1617 return (count);
1618 }
1619
1620 void
pci_walk_lintr(int bus,pci_lintr_cb cb,void * arg)1621 pci_walk_lintr(int bus, pci_lintr_cb cb, void *arg)
1622 {
1623 struct businfo *bi;
1624 struct slotinfo *si;
1625 struct intxinfo *ii;
1626 int slot, pin;
1627
1628 if ((bi = pci_businfo[bus]) == NULL)
1629 return;
1630
1631 for (slot = 0; slot < MAXSLOTS; slot++) {
1632 si = &bi->slotinfo[slot];
1633 for (pin = 0; pin < 4; pin++) {
1634 ii = &si->si_intpins[pin];
1635 if (ii->ii_count != 0)
1636 cb(bus, slot, pin + 1, ii->ii_pirq_pin,
1637 ii->ii_ioapic_irq, arg);
1638 }
1639 }
1640 }
1641
1642 /*
1643 * Return 1 if the emulated device in 'slot' is a multi-function device.
1644 * Return 0 otherwise.
1645 */
1646 static int
pci_emul_is_mfdev(int bus,int slot)1647 pci_emul_is_mfdev(int bus, int slot)
1648 {
1649 struct businfo *bi;
1650 struct slotinfo *si;
1651 int f, numfuncs;
1652
1653 numfuncs = 0;
1654 if ((bi = pci_businfo[bus]) != NULL) {
1655 si = &bi->slotinfo[slot];
1656 for (f = 0; f < MAXFUNCS; f++) {
1657 if (si->si_funcs[f].fi_devi != NULL) {
1658 numfuncs++;
1659 }
1660 }
1661 }
1662 return (numfuncs > 1);
1663 }
1664
1665 /*
1666 * Ensure that the PCIM_MFDEV bit is properly set (or unset) depending on
1667 * whether or not is a multi-function being emulated in the pci 'slot'.
1668 */
1669 static void
pci_emul_hdrtype_fixup(int bus,int slot,int off,int bytes,uint32_t * rv)1670 pci_emul_hdrtype_fixup(int bus, int slot, int off, int bytes, uint32_t *rv)
1671 {
1672 int mfdev;
1673
1674 if (off <= PCIR_HDRTYPE && off + bytes > PCIR_HDRTYPE) {
1675 mfdev = pci_emul_is_mfdev(bus, slot);
1676 switch (bytes) {
1677 case 1:
1678 case 2:
1679 *rv &= ~PCIM_MFDEV;
1680 if (mfdev) {
1681 *rv |= PCIM_MFDEV;
1682 }
1683 break;
1684 case 4:
1685 *rv &= ~(PCIM_MFDEV << 16);
1686 if (mfdev) {
1687 *rv |= (PCIM_MFDEV << 16);
1688 }
1689 break;
1690 }
1691 }
1692 }
1693
1694 /*
1695 * Update device state in response to changes to the PCI command
1696 * register.
1697 */
1698 void
pci_emul_cmd_changed(struct pci_devinst * pi,uint16_t old)1699 pci_emul_cmd_changed(struct pci_devinst *pi, uint16_t old)
1700 {
1701 int i;
1702 uint16_t changed, new;
1703
1704 new = pci_get_cfgdata16(pi, PCIR_COMMAND);
1705 changed = old ^ new;
1706
1707 /*
1708 * If the MMIO or I/O address space decoding has changed then
1709 * register/unregister all BARs that decode that address space.
1710 */
1711 for (i = 0; i <= PCI_BARMAX; i++) {
1712 switch (pi->pi_bar[i].type) {
1713 case PCIBAR_NONE:
1714 case PCIBAR_MEMHI64:
1715 break;
1716 case PCIBAR_IO:
1717 /* I/O address space decoding changed? */
1718 if (changed & PCIM_CMD_PORTEN) {
1719 if (new & PCIM_CMD_PORTEN)
1720 register_bar(pi, i);
1721 else
1722 unregister_bar(pi, i);
1723 }
1724 break;
1725 case PCIBAR_MEM32:
1726 case PCIBAR_MEM64:
1727 /* MMIO address space decoding changed? */
1728 if (changed & PCIM_CMD_MEMEN) {
1729 if (new & PCIM_CMD_MEMEN)
1730 register_bar(pi, i);
1731 else
1732 unregister_bar(pi, i);
1733 }
1734 break;
1735 default:
1736 assert(0);
1737 }
1738 }
1739
1740 /*
1741 * If INTx has been unmasked and is pending, assert the
1742 * interrupt.
1743 */
1744 pci_lintr_update(pi);
1745 }
1746
1747 static void
pci_emul_cmdsts_write(struct pci_devinst * pi,int coff,uint32_t new,int bytes)1748 pci_emul_cmdsts_write(struct pci_devinst *pi, int coff, uint32_t new, int bytes)
1749 {
1750 int rshift;
1751 uint32_t cmd, old, readonly;
1752
1753 cmd = pci_get_cfgdata16(pi, PCIR_COMMAND); /* stash old value */
1754
1755 /*
1756 * From PCI Local Bus Specification 3.0 sections 6.2.2 and 6.2.3.
1757 *
1758 * XXX Bits 8, 11, 12, 13, 14 and 15 in the status register are
1759 * 'write 1 to clear'. However these bits are not set to '1' by
1760 * any device emulation so it is simpler to treat them as readonly.
1761 */
1762 rshift = (coff & 0x3) * 8;
1763 readonly = 0xFFFFF880 >> rshift;
1764
1765 old = CFGREAD(pi, coff, bytes);
1766 new &= ~readonly;
1767 new |= (old & readonly);
1768 CFGWRITE(pi, coff, new, bytes); /* update config */
1769
1770 pci_emul_cmd_changed(pi, cmd);
1771 }
1772
1773 static void
pci_cfgrw(struct vmctx * ctx,int vcpu,int in,int bus,int slot,int func,int coff,int bytes,uint32_t * eax)1774 pci_cfgrw(struct vmctx *ctx, int vcpu, int in, int bus, int slot, int func,
1775 int coff, int bytes, uint32_t *eax)
1776 {
1777 struct businfo *bi;
1778 struct slotinfo *si;
1779 struct pci_devinst *pi;
1780 struct pci_devemu *pe;
1781 int idx, needcfg;
1782 uint64_t addr, bar, mask;
1783
1784 if ((bi = pci_businfo[bus]) != NULL) {
1785 si = &bi->slotinfo[slot];
1786 pi = si->si_funcs[func].fi_devi;
1787 } else
1788 pi = NULL;
1789
1790 /*
1791 * Just return if there is no device at this slot:func or if the
1792 * the guest is doing an un-aligned access.
1793 */
1794 if (pi == NULL || (bytes != 1 && bytes != 2 && bytes != 4) ||
1795 (coff & (bytes - 1)) != 0) {
1796 if (in)
1797 *eax = 0xffffffff;
1798 return;
1799 }
1800
1801 /*
1802 * Ignore all writes beyond the standard config space and return all
1803 * ones on reads.
1804 */
1805 if (coff >= PCI_REGMAX + 1) {
1806 if (in) {
1807 *eax = 0xffffffff;
1808 /*
1809 * Extended capabilities begin at offset 256 in config
1810 * space. Absence of extended capabilities is signaled
1811 * with all 0s in the extended capability header at
1812 * offset 256.
1813 */
1814 if (coff <= PCI_REGMAX + 4)
1815 *eax = 0x00000000;
1816 }
1817 return;
1818 }
1819
1820 pe = pi->pi_d;
1821
1822 /*
1823 * Config read
1824 */
1825 if (in) {
1826 /* Let the device emulation override the default handler */
1827 if (pe->pe_cfgread != NULL) {
1828 needcfg = pe->pe_cfgread(ctx, vcpu, pi, coff, bytes,
1829 eax);
1830 } else {
1831 needcfg = 1;
1832 }
1833
1834 if (needcfg)
1835 *eax = CFGREAD(pi, coff, bytes);
1836
1837 pci_emul_hdrtype_fixup(bus, slot, coff, bytes, eax);
1838 } else {
1839 /* Let the device emulation override the default handler */
1840 if (pe->pe_cfgwrite != NULL &&
1841 (*pe->pe_cfgwrite)(ctx, vcpu, pi, coff, bytes, *eax) == 0)
1842 return;
1843
1844 /*
1845 * Special handling for write to BAR registers
1846 */
1847 if (coff >= PCIR_BAR(0) && coff < PCIR_BAR(PCI_BARMAX + 1)) {
1848 /*
1849 * Ignore writes to BAR registers that are not
1850 * 4-byte aligned.
1851 */
1852 if (bytes != 4 || (coff & 0x3) != 0)
1853 return;
1854 idx = (coff - PCIR_BAR(0)) / 4;
1855 mask = ~(pi->pi_bar[idx].size - 1);
1856 switch (pi->pi_bar[idx].type) {
1857 case PCIBAR_NONE:
1858 pi->pi_bar[idx].addr = bar = 0;
1859 break;
1860 case PCIBAR_IO:
1861 addr = *eax & mask;
1862 addr &= 0xffff;
1863 bar = addr | PCIM_BAR_IO_SPACE;
1864 /*
1865 * Register the new BAR value for interception
1866 */
1867 if (addr != pi->pi_bar[idx].addr) {
1868 update_bar_address(pi, addr, idx,
1869 PCIBAR_IO);
1870 }
1871 break;
1872 case PCIBAR_MEM32:
1873 addr = bar = *eax & mask;
1874 bar |= PCIM_BAR_MEM_SPACE | PCIM_BAR_MEM_32;
1875 if (addr != pi->pi_bar[idx].addr) {
1876 update_bar_address(pi, addr, idx,
1877 PCIBAR_MEM32);
1878 }
1879 break;
1880 case PCIBAR_MEM64:
1881 addr = bar = *eax & mask;
1882 bar |= PCIM_BAR_MEM_SPACE | PCIM_BAR_MEM_64 |
1883 PCIM_BAR_MEM_PREFETCH;
1884 if (addr != (uint32_t)pi->pi_bar[idx].addr) {
1885 update_bar_address(pi, addr, idx,
1886 PCIBAR_MEM64);
1887 }
1888 break;
1889 case PCIBAR_MEMHI64:
1890 mask = ~(pi->pi_bar[idx - 1].size - 1);
1891 addr = ((uint64_t)*eax << 32) & mask;
1892 bar = addr >> 32;
1893 if (bar != pi->pi_bar[idx - 1].addr >> 32) {
1894 update_bar_address(pi, addr, idx - 1,
1895 PCIBAR_MEMHI64);
1896 }
1897 break;
1898 default:
1899 assert(0);
1900 }
1901 pci_set_cfgdata32(pi, coff, bar);
1902
1903 } else if (pci_emul_iscap(pi, coff)) {
1904 pci_emul_capwrite(pi, coff, bytes, *eax, 0, 0);
1905 } else if (coff >= PCIR_COMMAND && coff < PCIR_REVID) {
1906 pci_emul_cmdsts_write(pi, coff, *eax, bytes);
1907 } else {
1908 CFGWRITE(pi, coff, *eax, bytes);
1909 }
1910 }
1911 }
1912
1913 static int cfgenable, cfgbus, cfgslot, cfgfunc, cfgoff;
1914
1915 static int
pci_emul_cfgaddr(struct vmctx * ctx,int vcpu,int in,int port,int bytes,uint32_t * eax,void * arg)1916 pci_emul_cfgaddr(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
1917 uint32_t *eax, void *arg)
1918 {
1919 uint32_t x;
1920
1921 if (bytes != 4) {
1922 if (in)
1923 *eax = (bytes == 2) ? 0xffff : 0xff;
1924 return (0);
1925 }
1926
1927 if (in) {
1928 x = (cfgbus << 16) | (cfgslot << 11) | (cfgfunc << 8) | cfgoff;
1929 if (cfgenable)
1930 x |= CONF1_ENABLE;
1931 *eax = x;
1932 } else {
1933 x = *eax;
1934 cfgenable = (x & CONF1_ENABLE) == CONF1_ENABLE;
1935 cfgoff = x & PCI_REGMAX;
1936 cfgfunc = (x >> 8) & PCI_FUNCMAX;
1937 cfgslot = (x >> 11) & PCI_SLOTMAX;
1938 cfgbus = (x >> 16) & PCI_BUSMAX;
1939 }
1940
1941 return (0);
1942 }
1943 INOUT_PORT(pci_cfgaddr, CONF1_ADDR_PORT, IOPORT_F_INOUT, pci_emul_cfgaddr);
1944
1945 static int
pci_emul_cfgdata(struct vmctx * ctx,int vcpu,int in,int port,int bytes,uint32_t * eax,void * arg)1946 pci_emul_cfgdata(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
1947 uint32_t *eax, void *arg)
1948 {
1949 int coff;
1950
1951 assert(bytes == 1 || bytes == 2 || bytes == 4);
1952
1953 coff = cfgoff + (port - CONF1_DATA_PORT);
1954 if (cfgenable) {
1955 pci_cfgrw(ctx, vcpu, in, cfgbus, cfgslot, cfgfunc, coff, bytes,
1956 eax);
1957 } else {
1958 /* Ignore accesses to cfgdata if not enabled by cfgaddr */
1959 if (in)
1960 *eax = 0xffffffff;
1961 }
1962 return (0);
1963 }
1964
1965 INOUT_PORT(pci_cfgdata, CONF1_DATA_PORT+0, IOPORT_F_INOUT, pci_emul_cfgdata);
1966 INOUT_PORT(pci_cfgdata, CONF1_DATA_PORT+1, IOPORT_F_INOUT, pci_emul_cfgdata);
1967 INOUT_PORT(pci_cfgdata, CONF1_DATA_PORT+2, IOPORT_F_INOUT, pci_emul_cfgdata);
1968 INOUT_PORT(pci_cfgdata, CONF1_DATA_PORT+3, IOPORT_F_INOUT, pci_emul_cfgdata);
1969
1970 #define PCI_EMUL_TEST
1971 #ifdef PCI_EMUL_TEST
1972 /*
1973 * Define a dummy test device
1974 */
1975 #define DIOSZ 8
1976 #define DMEMSZ 4096
1977 struct pci_emul_dsoftc {
1978 uint8_t ioregs[DIOSZ];
1979 uint8_t memregs[2][DMEMSZ];
1980 };
1981
1982 #define PCI_EMUL_MSI_MSGS 4
1983 #define PCI_EMUL_MSIX_MSGS 16
1984
1985 static int
pci_emul_dinit(struct vmctx * ctx,struct pci_devinst * pi,char * opts)1986 pci_emul_dinit(struct vmctx *ctx, struct pci_devinst *pi, char *opts)
1987 {
1988 int error;
1989 struct pci_emul_dsoftc *sc;
1990
1991 sc = calloc(1, sizeof(struct pci_emul_dsoftc));
1992
1993 pi->pi_arg = sc;
1994
1995 pci_set_cfgdata16(pi, PCIR_DEVICE, 0x0001);
1996 pci_set_cfgdata16(pi, PCIR_VENDOR, 0x10DD);
1997 pci_set_cfgdata8(pi, PCIR_CLASS, 0x02);
1998
1999 error = pci_emul_add_msicap(pi, PCI_EMUL_MSI_MSGS);
2000 assert(error == 0);
2001
2002 error = pci_emul_alloc_bar(pi, 0, PCIBAR_IO, DIOSZ);
2003 assert(error == 0);
2004
2005 error = pci_emul_alloc_bar(pi, 1, PCIBAR_MEM32, DMEMSZ);
2006 assert(error == 0);
2007
2008 error = pci_emul_alloc_bar(pi, 2, PCIBAR_MEM32, DMEMSZ);
2009 assert(error == 0);
2010
2011 return (0);
2012 }
2013
2014 static void
pci_emul_diow(struct vmctx * ctx,int vcpu,struct pci_devinst * pi,int baridx,uint64_t offset,int size,uint64_t value)2015 pci_emul_diow(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, int baridx,
2016 uint64_t offset, int size, uint64_t value)
2017 {
2018 int i;
2019 struct pci_emul_dsoftc *sc = pi->pi_arg;
2020
2021 if (baridx == 0) {
2022 if (offset + size > DIOSZ) {
2023 printf("diow: iow too large, offset %ld size %d\n",
2024 offset, size);
2025 return;
2026 }
2027
2028 if (size == 1) {
2029 sc->ioregs[offset] = value & 0xff;
2030 } else if (size == 2) {
2031 *(uint16_t *)&sc->ioregs[offset] = value & 0xffff;
2032 } else if (size == 4) {
2033 *(uint32_t *)&sc->ioregs[offset] = value;
2034 } else {
2035 printf("diow: iow unknown size %d\n", size);
2036 }
2037
2038 /*
2039 * Special magic value to generate an interrupt
2040 */
2041 if (offset == 4 && size == 4 && pci_msi_enabled(pi))
2042 pci_generate_msi(pi, value % pci_msi_maxmsgnum(pi));
2043
2044 if (value == 0xabcdef) {
2045 for (i = 0; i < pci_msi_maxmsgnum(pi); i++)
2046 pci_generate_msi(pi, i);
2047 }
2048 }
2049
2050 if (baridx == 1 || baridx == 2) {
2051 if (offset + size > DMEMSZ) {
2052 printf("diow: memw too large, offset %ld size %d\n",
2053 offset, size);
2054 return;
2055 }
2056
2057 i = baridx - 1; /* 'memregs' index */
2058
2059 if (size == 1) {
2060 sc->memregs[i][offset] = value;
2061 } else if (size == 2) {
2062 *(uint16_t *)&sc->memregs[i][offset] = value;
2063 } else if (size == 4) {
2064 *(uint32_t *)&sc->memregs[i][offset] = value;
2065 } else if (size == 8) {
2066 *(uint64_t *)&sc->memregs[i][offset] = value;
2067 } else {
2068 printf("diow: memw unknown size %d\n", size);
2069 }
2070
2071 /*
2072 * magic interrupt ??
2073 */
2074 }
2075
2076 if (baridx > 2 || baridx < 0) {
2077 printf("diow: unknown bar idx %d\n", baridx);
2078 }
2079 }
2080
2081 static uint64_t
pci_emul_dior(struct vmctx * ctx,int vcpu,struct pci_devinst * pi,int baridx,uint64_t offset,int size)2082 pci_emul_dior(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, int baridx,
2083 uint64_t offset, int size)
2084 {
2085 struct pci_emul_dsoftc *sc = pi->pi_arg;
2086 uint32_t value;
2087 int i;
2088
2089 if (baridx == 0) {
2090 if (offset + size > DIOSZ) {
2091 printf("dior: ior too large, offset %ld size %d\n",
2092 offset, size);
2093 return (0);
2094 }
2095
2096 value = 0;
2097 if (size == 1) {
2098 value = sc->ioregs[offset];
2099 } else if (size == 2) {
2100 value = *(uint16_t *) &sc->ioregs[offset];
2101 } else if (size == 4) {
2102 value = *(uint32_t *) &sc->ioregs[offset];
2103 } else {
2104 printf("dior: ior unknown size %d\n", size);
2105 }
2106 }
2107
2108 if (baridx == 1 || baridx == 2) {
2109 if (offset + size > DMEMSZ) {
2110 printf("dior: memr too large, offset %ld size %d\n",
2111 offset, size);
2112 return (0);
2113 }
2114
2115 i = baridx - 1; /* 'memregs' index */
2116
2117 if (size == 1) {
2118 value = sc->memregs[i][offset];
2119 } else if (size == 2) {
2120 value = *(uint16_t *) &sc->memregs[i][offset];
2121 } else if (size == 4) {
2122 value = *(uint32_t *) &sc->memregs[i][offset];
2123 } else if (size == 8) {
2124 value = *(uint64_t *) &sc->memregs[i][offset];
2125 } else {
2126 printf("dior: ior unknown size %d\n", size);
2127 }
2128 }
2129
2130
2131 if (baridx > 2 || baridx < 0) {
2132 printf("dior: unknown bar idx %d\n", baridx);
2133 return (0);
2134 }
2135
2136 return (value);
2137 }
2138
2139 struct pci_devemu pci_dummy = {
2140 .pe_emu = "dummy",
2141 .pe_init = pci_emul_dinit,
2142 .pe_barwrite = pci_emul_diow,
2143 .pe_barread = pci_emul_dior
2144 };
2145 PCI_EMUL_SET(pci_dummy);
2146
2147 #endif /* PCI_EMUL_TEST */
2148