2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2016, Anish Gupta (anish@freebsd.org)
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
37 #include <sys/malloc.h>
39 #include <machine/vmparam.h>
44 #include <contrib/dev/acpica/include/acpi.h>
45 #include <contrib/dev/acpica/include/accommon.h>
46 #include <dev/acpica/acpivar.h>
49 #include "amdvi_priv.h"
51 device_t *ivhd_devs; /* IVHD or AMD-Vi device list. */
52 int ivhd_count; /* Number of IVHD header. */
54 * Cached IVHD header list.
55 * Single entry for each IVHD, filtered the legacy one.
57 ACPI_IVRS_HARDWARE1 *ivhd_hdrs[10];
59 extern int amdvi_ptp_level; /* Page table levels. */
61 typedef int (*ivhd_iter_t)(ACPI_IVRS_HEADER *ptr, void *arg);
63 * Iterate IVRS table for IVHD and IVMD device type.
66 ivrs_hdr_iterate_tbl(ivhd_iter_t iter, void *arg)
68 ACPI_TABLE_IVRS *ivrs;
69 ACPI_IVRS_HEADER *ivrs_hdr, *end;
72 status = AcpiGetTable(ACPI_SIG_IVRS, 1, (ACPI_TABLE_HEADER **)&ivrs);
73 if (ACPI_FAILURE(status))
76 if (ivrs->Header.Length == 0) {
80 ivrs_hdr = (ACPI_IVRS_HEADER *)(ivrs + 1);
81 end = (ACPI_IVRS_HEADER *)((char *)ivrs + ivrs->Header.Length);
83 while (ivrs_hdr < end) {
84 if ((uint8_t *)ivrs_hdr + ivrs_hdr->Length > (uint8_t *)end) {
85 printf("AMD-Vi:IVHD/IVMD is corrupted, length : %d\n",
90 switch (ivrs_hdr->Type) {
91 case IVRS_TYPE_HARDWARE_LEGACY: /* Legacy */
92 case IVRS_TYPE_HARDWARE_EFR:
93 case IVRS_TYPE_HARDWARE_MIXED:
94 if (!iter(ivrs_hdr, arg))
98 case ACPI_IVRS_TYPE_MEMORY1:
99 case ACPI_IVRS_TYPE_MEMORY2:
100 case ACPI_IVRS_TYPE_MEMORY3:
101 if (!iter(ivrs_hdr, arg))
107 printf("AMD-Vi:Not IVHD/IVMD type(%d)", ivrs_hdr->Type);
111 ivrs_hdr = (ACPI_IVRS_HEADER *)((uint8_t *)ivrs_hdr +
117 ivrs_is_ivhd(UINT8 type)
121 case IVRS_TYPE_HARDWARE_LEGACY:
122 case IVRS_TYPE_HARDWARE_EFR:
123 case IVRS_TYPE_HARDWARE_MIXED:
131 /* Count the number of AMD-Vi devices in the system. */
133 ivhd_count_iter(ACPI_IVRS_HEADER * ivrs_he, void *arg)
136 if (ivrs_is_ivhd(ivrs_he->Type))
142 struct find_ivrs_hdr_args {
144 ACPI_IVRS_HEADER *ptr;
148 ivrs_hdr_find_iter(ACPI_IVRS_HEADER * ivrs_hdr, void *args)
150 struct find_ivrs_hdr_args *fi;
152 fi = (struct find_ivrs_hdr_args *)args;
153 if (ivrs_is_ivhd(ivrs_hdr->Type)) {
164 static ACPI_IVRS_HARDWARE1 *
165 ivhd_find_by_index(int idx)
167 struct find_ivrs_hdr_args fi;
172 ivrs_hdr_iterate_tbl(ivrs_hdr_find_iter, &fi);
174 return ((ACPI_IVRS_HARDWARE1 *)fi.ptr);
178 ivhd_dev_add_entry(struct amdvi_softc *softc, uint32_t start_id,
179 uint32_t end_id, uint8_t cfg, bool ats)
181 struct ivhd_dev_cfg *dev_cfg;
183 /* If device doesn't have special data, don't add it. */
187 dev_cfg = &softc->dev_cfg[softc->dev_cfg_cnt++];
188 dev_cfg->start_id = start_id;
189 dev_cfg->end_id = end_id;
191 dev_cfg->enable_ats = ats;
195 * Record device attributes as suggested by BIOS.
198 ivhd_dev_parse(ACPI_IVRS_HARDWARE1 *ivhd, struct amdvi_softc *softc)
200 ACPI_IVRS_DE_HEADER *de;
202 int range_start_id = 0, range_end_id = 0;
204 uint8_t all_data = 0, range_data = 0;
205 bool range_enable_ats = false, enable_ats;
207 softc->start_dev_rid = ~0;
208 softc->end_dev_rid = 0;
210 switch (ivhd->Header.Type) {
211 case IVRS_TYPE_HARDWARE_LEGACY:
212 p = (uint8_t *)ivhd + sizeof(ACPI_IVRS_HARDWARE1);
215 case IVRS_TYPE_HARDWARE_EFR:
216 case IVRS_TYPE_HARDWARE_MIXED:
217 p = (uint8_t *)ivhd + sizeof(ACPI_IVRS_HARDWARE2);
221 device_printf(softc->dev,
222 "unknown type: 0x%x\n", ivhd->Header.Type);
226 end = (uint8_t *)ivhd + ivhd->Header.Length;
229 de = (ACPI_IVRS_DE_HEADER *)p;
230 softc->start_dev_rid = MIN(softc->start_dev_rid, de->Id);
231 softc->end_dev_rid = MAX(softc->end_dev_rid, de->Id);
233 case ACPI_IVRS_TYPE_ALL:
234 all_data = de->DataSetting;
237 case ACPI_IVRS_TYPE_SELECT:
238 case ACPI_IVRS_TYPE_ALIAS_SELECT:
239 case ACPI_IVRS_TYPE_EXT_SELECT:
241 if (de->Type == ACPI_IVRS_TYPE_EXT_SELECT) {
242 extended = (uint32_t *)(de + 1);
244 (*extended & IVHD_DEV_EXT_ATS_DISABLE) ?
247 ivhd_dev_add_entry(softc, de->Id, de->Id,
248 de->DataSetting | all_data, enable_ats);
251 case ACPI_IVRS_TYPE_START:
252 case ACPI_IVRS_TYPE_ALIAS_START:
253 case ACPI_IVRS_TYPE_EXT_START:
254 range_start_id = de->Id;
255 range_data = de->DataSetting;
256 if (de->Type == ACPI_IVRS_TYPE_EXT_START) {
257 extended = (uint32_t *)(de + 1);
259 (*extended & IVHD_DEV_EXT_ATS_DISABLE) ?
264 case ACPI_IVRS_TYPE_END:
265 range_end_id = de->Id;
266 ivhd_dev_add_entry(softc, range_start_id, range_end_id,
267 range_data | all_data, range_enable_ats);
268 range_start_id = range_end_id = 0;
273 case ACPI_IVRS_TYPE_PAD4:
276 case ACPI_IVRS_TYPE_SPECIAL:
280 if ((de->Type < 5) ||
281 (de->Type >= ACPI_IVRS_TYPE_PAD8))
282 device_printf(softc->dev,
283 "Unknown dev entry:0x%x\n", de->Type);
286 if (softc->dev_cfg_cnt >
287 (sizeof(softc->dev_cfg) / sizeof(softc->dev_cfg[0]))) {
288 device_printf(softc->dev,
289 "WARN Too many device entries.\n");
293 p += sizeof(ACPI_IVRS_DEVICE4);
294 else if (de->Type < 0x80)
295 p += sizeof(ACPI_IVRS_DEVICE8A);
297 printf("Variable size IVHD type 0x%x not supported\n",
303 KASSERT((softc->end_dev_rid >= softc->start_dev_rid),
304 ("Device end[0x%x] < start[0x%x.\n",
305 softc->end_dev_rid, softc->start_dev_rid));
311 ivhd_is_newer(ACPI_IVRS_HEADER *old, ACPI_IVRS_HEADER *new)
314 * Newer IVRS header type take precedence.
316 if ((old->DeviceId == new->DeviceId) &&
317 (old->Type == IVRS_TYPE_HARDWARE_LEGACY) &&
318 ((new->Type == IVRS_TYPE_HARDWARE_EFR) ||
319 (new->Type == IVRS_TYPE_HARDWARE_MIXED))) {
327 ivhd_identify(driver_t *driver, device_t parent)
329 ACPI_TABLE_IVRS *ivrs;
330 ACPI_IVRS_HARDWARE1 *ivhd;
333 uint32_t ivrs_ivinfo;
335 if (acpi_disabled("ivhd"))
338 status = AcpiGetTable(ACPI_SIG_IVRS, 1, (ACPI_TABLE_HEADER **)&ivrs);
339 if (ACPI_FAILURE(status))
342 if (ivrs->Header.Length == 0) {
346 ivrs_ivinfo = ivrs->Info;
347 printf("AMD-Vi: IVRS Info VAsize = %d PAsize = %d GVAsize = %d"
349 REG_BITS(ivrs_ivinfo, 21, 15), REG_BITS(ivrs_ivinfo, 14, 8),
350 REG_BITS(ivrs_ivinfo, 7, 5), REG_BITS(ivrs_ivinfo, 22, 22),
353 ivrs_hdr_iterate_tbl(ivhd_count_iter, NULL);
357 for (i = 0; i < ivhd_count; i++) {
358 ivhd = ivhd_find_by_index(i);
359 KASSERT(ivhd, ("ivhd%d is NULL\n", i));
364 * Scan for presence of legacy and non-legacy device type
365 * for same AMD-Vi device and override the old one.
367 for (i = ivhd_count - 1 ; i > 0 ; i--){
368 if (ivhd_is_newer(&ivhd_hdrs[i-1]->Header,
369 &ivhd_hdrs[i]->Header)) {
370 ivhd_hdrs[i-1] = ivhd_hdrs[i];
375 ivhd_devs = malloc(sizeof(device_t) * ivhd_count, M_DEVBUF,
377 for (i = 0; i < ivhd_count; i++) {
379 KASSERT(ivhd, ("ivhd%d is NULL\n", i));
382 * Use a high order to ensure that this driver is probed after
383 * the Host-PCI bridge and the root PCI bus.
385 ivhd_devs[i] = BUS_ADD_CHILD(parent,
386 ACPI_DEV_BASE_ORDER + 10 * 10, "ivhd", i);
389 * XXX: In case device was not destroyed before, add will fail.
390 * locate the old device instance.
392 if (ivhd_devs[i] == NULL) {
393 ivhd_devs[i] = device_find_child(parent, "ivhd", i);
394 if (ivhd_devs[i] == NULL) {
395 printf("AMD-Vi: cant find ivhd%d\n", i);
403 * Update device count in case failed to attach.
409 ivhd_probe(device_t dev)
411 ACPI_IVRS_HARDWARE1 *ivhd;
414 if (acpi_get_handle(dev) != NULL)
417 unit = device_get_unit(dev);
418 KASSERT((unit < ivhd_count),
419 ("ivhd unit %d > count %d", unit, ivhd_count));
420 ivhd = ivhd_hdrs[unit];
421 KASSERT(ivhd, ("ivhd is NULL"));
423 switch (ivhd->Header.Type) {
424 case IVRS_TYPE_HARDWARE_EFR:
425 device_set_desc(dev, "AMD-Vi/IOMMU ivhd with EFR");
428 case IVRS_TYPE_HARDWARE_MIXED:
429 device_set_desc(dev, "AMD-Vi/IOMMU ivhd in mixed format");
432 case IVRS_TYPE_HARDWARE_LEGACY:
434 device_set_desc(dev, "AMD-Vi/IOMMU ivhd");
438 return (BUS_PROBE_NOWILDCARD);
442 ivhd_print_flag(device_t dev, enum IvrsType ivhd_type, uint8_t flag)
445 * IVHD lgeacy type has two extra high bits in flag which has
446 * been moved to EFR for non-legacy device.
449 case IVRS_TYPE_HARDWARE_LEGACY:
450 device_printf(dev, "Flag:%b\n", flag,
462 case IVRS_TYPE_HARDWARE_EFR:
463 case IVRS_TYPE_HARDWARE_MIXED:
464 device_printf(dev, "Flag:%b\n", flag,
475 device_printf(dev, "Can't decode flag of ivhd type :0x%x\n",
482 * Feature in legacy IVHD type(0x10) and attribute in newer type(0x11 and 0x40).
485 ivhd_print_feature(device_t dev, enum IvrsType ivhd_type, uint32_t feature)
488 case IVRS_TYPE_HARDWARE_LEGACY:
489 device_printf(dev, "Features(type:0x%x) HATS = %d GATS = %d"
490 " MsiNumPPR = %d PNBanks= %d PNCounters= %d\n",
492 REG_BITS(feature, 31, 30),
493 REG_BITS(feature, 29, 28),
494 REG_BITS(feature, 27, 23),
495 REG_BITS(feature, 22, 17),
496 REG_BITS(feature, 16, 13));
497 device_printf(dev, "max PASID = %d GLXSup = %d Feature:%b\n",
498 REG_BITS(feature, 12, 8),
499 REG_BITS(feature, 4, 3),
510 /* Fewer features or attributes are reported in non-legacy type. */
511 case IVRS_TYPE_HARDWARE_EFR:
512 case IVRS_TYPE_HARDWARE_MIXED:
513 device_printf(dev, "Features(type:0x%x) MsiNumPPR = %d"
514 " PNBanks= %d PNCounters= %d\n",
516 REG_BITS(feature, 27, 23),
517 REG_BITS(feature, 22, 17),
518 REG_BITS(feature, 16, 13));
521 default: /* Other ivhd type features are not decoded. */
522 device_printf(dev, "Can't decode ivhd type :0x%x\n", ivhd_type);
526 /* Print extended features of IOMMU. */
528 ivhd_print_ext_feature(device_t dev, uint64_t ext_feature)
530 uint32_t ext_low, ext_high;
535 ext_low = ext_feature;
536 device_printf(dev, "Extended features[31:0]:%b "
537 "HATS = 0x%x GATS = 0x%x "
538 "GLXSup = 0x%x SmiFSup = 0x%x SmiFRC = 0x%x "
539 "GAMSup = 0x%x DualPortLogSup = 0x%x DualEventLogSup = 0x%x\n",
552 REG_BITS(ext_low, 11, 10),
553 REG_BITS(ext_low, 13, 12),
554 REG_BITS(ext_low, 15, 14),
555 REG_BITS(ext_low, 17, 16),
556 REG_BITS(ext_low, 20, 18),
557 REG_BITS(ext_low, 23, 21),
558 REG_BITS(ext_low, 25, 24),
559 REG_BITS(ext_low, 29, 28));
561 ext_high = ext_feature >> 32;
562 device_printf(dev, "Extended features[62:32]:%b "
563 "Max PASID: 0x%x DevTblSegSup = 0x%x "
568 "\009PprOvrflwEarlySup"
579 REG_BITS(ext_high, 5, 0),
580 REG_BITS(ext_high, 8, 7),
581 REG_BITS(ext_high, 11, 10));
585 ivhd_print_cap(struct amdvi_softc *softc, ACPI_IVRS_HARDWARE1 * ivhd)
592 ivhd_print_flag(dev, softc->ivhd_type, softc->ivhd_flag);
593 ivhd_print_feature(dev, softc->ivhd_type, softc->ivhd_feature);
594 ivhd_print_ext_feature(dev, softc->ext_feature);
596 /* Make sure device support minimum page level as requested by user. */
597 if (max_ptp_level < amdvi_ptp_level) {
598 device_printf(dev, "insufficient PTP level:%d\n",
602 device_printf(softc->dev, "supported paging level:%d, will use only: %d\n",
603 max_ptp_level, amdvi_ptp_level);
606 device_printf(softc->dev, "device range: 0x%x - 0x%x\n",
607 softc->start_dev_rid, softc->end_dev_rid);
613 ivhd_attach(device_t dev)
615 ACPI_IVRS_HARDWARE1 *ivhd;
616 ACPI_IVRS_HARDWARE2 *ivhd_efr;
617 struct amdvi_softc *softc;
620 unit = device_get_unit(dev);
621 KASSERT((unit < ivhd_count),
622 ("ivhd unit %d > count %d", unit, ivhd_count));
623 /* Make sure its same device for which attach is called. */
624 KASSERT((ivhd_devs[unit] == dev),
625 ("Not same device old %p new %p", ivhd_devs[unit], dev));
627 softc = device_get_softc(dev);
629 ivhd = ivhd_hdrs[unit];
630 KASSERT(ivhd, ("ivhd is NULL"));
632 softc->ivhd_type = ivhd->Header.Type;
633 softc->pci_seg = ivhd->PciSegmentGroup;
634 softc->pci_rid = ivhd->Header.DeviceId;
635 softc->ivhd_flag = ivhd->Header.Flags;
637 * On lgeacy IVHD type(0x10), it is documented as feature
638 * but in newer type it is attribute.
640 softc->ivhd_feature = ivhd->FeatureReporting;
642 * PCI capability has more capabilities that are not part of IVRS.
644 softc->cap_off = ivhd->CapabilityOffset;
647 /* IVHD Info bit[4:0] is event MSI/X number. */
648 softc->event_msix = ivhd->Info & 0x1F;
650 switch (ivhd->Header.Type) {
651 case IVRS_TYPE_HARDWARE_EFR:
652 case IVRS_TYPE_HARDWARE_MIXED:
653 ivhd_efr = (ACPI_IVRS_HARDWARE2 *)ivhd;
654 softc->ext_feature = ivhd_efr->EfrRegisterImage;
658 softc->ctrl = (struct amdvi_ctrl *) PHYS_TO_DMAP(ivhd->BaseAddress);
659 status = ivhd_dev_parse(ivhd, softc);
662 "endpoint device parsing error=%d\n", status);
665 status = ivhd_print_cap(softc, ivhd);
670 status = amdvi_setup_hw(softc);
672 device_printf(dev, "couldn't be initialised, error=%d\n",
681 ivhd_detach(device_t dev)
683 struct amdvi_softc *softc;
685 softc = device_get_softc(dev);
687 amdvi_teardown_hw(softc);
690 * XXX: delete the device.
691 * don't allow detach, return EBUSY.
697 ivhd_suspend(device_t dev)
704 ivhd_resume(device_t dev)
710 static device_method_t ivhd_methods[] = {
711 DEVMETHOD(device_identify, ivhd_identify),
712 DEVMETHOD(device_probe, ivhd_probe),
713 DEVMETHOD(device_attach, ivhd_attach),
714 DEVMETHOD(device_detach, ivhd_detach),
715 DEVMETHOD(device_suspend, ivhd_suspend),
716 DEVMETHOD(device_resume, ivhd_resume),
720 static driver_t ivhd_driver = {
723 sizeof(struct amdvi_softc),
726 static devclass_t ivhd_devclass;
729 * Load this module at the end after PCI re-probing to configure interrupt.
731 DRIVER_MODULE_ORDERED(ivhd, acpi, ivhd_driver, ivhd_devclass, 0, 0,
733 MODULE_DEPEND(ivhd, acpi, 1, 1, 1);
734 MODULE_DEPEND(ivhd, pci, 1, 1, 1);