2 * Copyright (c) 2016, Anish Gupta (anish@freebsd.org)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
33 #include <sys/kernel.h>
34 #include <sys/module.h>
35 #include <sys/malloc.h>
37 #include <machine/vmparam.h>
42 #include <contrib/dev/acpica/include/acpi.h>
43 #include <contrib/dev/acpica/include/accommon.h>
44 #include <dev/acpica/acpivar.h>
47 #include "amdvi_priv.h"
49 device_t *ivhd_devs; /* IVHD or AMD-Vi device list. */
50 int ivhd_count; /* Number of IVHD or AMD-Vi devices. */
52 extern int amdvi_ptp_level; /* Page table levels. */
54 typedef int (*ivhd_iter_t)(ACPI_IVRS_HEADER * ptr, void *arg);
57 * Iterate IVRS table for IVHD and IVMD device type.
60 ivrs_hdr_iterate_tbl(ivhd_iter_t iter, void *arg)
62 ACPI_TABLE_IVRS *ivrs;
63 ACPI_IVRS_HEADER *ivrs_hdr, *end;
66 status = AcpiGetTable(ACPI_SIG_IVRS, 1, (ACPI_TABLE_HEADER **)&ivrs);
67 if (ACPI_FAILURE(status))
70 if (ivrs->Header.Length == 0) {
74 ivrs_hdr = (ACPI_IVRS_HEADER *)(ivrs + 1);
75 end = (ACPI_IVRS_HEADER *)((char *)ivrs + ivrs->Header.Length);
77 while (ivrs_hdr < end) {
78 switch (ivrs_hdr->Type) {
79 case ACPI_IVRS_TYPE_HARDWARE: /* Legacy */
81 case 0x40: /* ACPI HID */
82 if (!iter(ivrs_hdr, arg))
86 case ACPI_IVRS_TYPE_MEMORY1:
87 case ACPI_IVRS_TYPE_MEMORY2:
88 case ACPI_IVRS_TYPE_MEMORY3:
89 if (!iter(ivrs_hdr, arg))
95 printf("AMD-Vi:Not IVHD/IVMD type(%d)", ivrs_hdr->Type);
99 ivrs_hdr = (ACPI_IVRS_HEADER *)((uint8_t *)ivrs_hdr +
101 if (ivrs_hdr->Length < 0) {
102 printf("AMD-Vi:IVHD/IVMD is corrupted, length : %d\n", ivrs_hdr->Length);
109 ivrs_is_ivhd(UINT8 type)
112 if ((type == ACPI_IVRS_TYPE_HARDWARE) || (type == 0x11) || (type == 0x40))
118 /* Count the number of AMD-Vi devices in the system. */
120 ivhd_count_iter(ACPI_IVRS_HEADER * ivrs_he, void *arg)
123 if (ivrs_is_ivhd(ivrs_he->Type))
129 struct find_ivrs_hdr_args {
131 ACPI_IVRS_HEADER *ptr;
135 ivrs_hdr_find_iter(ACPI_IVRS_HEADER * ivrs_hdr, void *args)
137 struct find_ivrs_hdr_args *fi;
139 fi = (struct find_ivrs_hdr_args *)args;
140 if (ivrs_is_ivhd(ivrs_hdr->Type)) {
151 static ACPI_IVRS_HARDWARE *
152 ivhd_find_by_index(int idx)
154 struct find_ivrs_hdr_args fi;
159 ivrs_hdr_iterate_tbl(ivrs_hdr_find_iter, &fi);
161 return ((ACPI_IVRS_HARDWARE *)fi.ptr);
165 ivhd_dev_add_entry(struct amdvi_softc *softc, uint32_t start_id,
166 uint32_t end_id, uint8_t cfg, bool ats)
168 struct ivhd_dev_cfg *dev_cfg;
170 /* If device doesn't have special data, don't add it. */
174 dev_cfg = &softc->dev_cfg[softc->dev_cfg_cnt++];
175 dev_cfg->start_id = start_id;
176 dev_cfg->end_id = end_id;
178 dev_cfg->enable_ats = ats;
182 * Record device attributes as suggested by BIOS.
185 ivhd_dev_parse(ACPI_IVRS_HARDWARE * ivhd, struct amdvi_softc *softc)
187 ACPI_IVRS_DE_HEADER *de, *end;
188 int range_start_id = 0, range_end_id = 0;
190 uint8_t all_data = 0, range_data = 0;
191 bool range_enable_ats = false, enable_ats;
193 softc->start_dev_rid = ~0;
194 softc->end_dev_rid = 0;
196 de = (ACPI_IVRS_DE_HEADER *) ((uint8_t *)ivhd +
197 sizeof(ACPI_IVRS_HARDWARE));
198 end = (ACPI_IVRS_DE_HEADER *) ((uint8_t *)ivhd +
199 ivhd->Header.Length);
201 while (de < (ACPI_IVRS_DE_HEADER *) end) {
202 softc->start_dev_rid = MIN(softc->start_dev_rid, de->Id);
203 softc->end_dev_rid = MAX(softc->end_dev_rid, de->Id);
205 case ACPI_IVRS_TYPE_ALL:
206 all_data = de->DataSetting;
209 case ACPI_IVRS_TYPE_SELECT:
210 case ACPI_IVRS_TYPE_ALIAS_SELECT:
211 case ACPI_IVRS_TYPE_EXT_SELECT:
213 if (de->Type == ACPI_IVRS_TYPE_EXT_SELECT) {
214 extended = (uint32_t *)(de + 1);
216 (*extended & IVHD_DEV_EXT_ATS_DISABLE) ?
219 ivhd_dev_add_entry(softc, de->Id, de->Id,
220 de->DataSetting | all_data, enable_ats);
223 case ACPI_IVRS_TYPE_START:
224 case ACPI_IVRS_TYPE_ALIAS_START:
225 case ACPI_IVRS_TYPE_EXT_START:
226 range_start_id = de->Id;
227 range_data = de->DataSetting;
228 if (de->Type == ACPI_IVRS_TYPE_EXT_START) {
229 extended = (uint32_t *)(de + 1);
231 (*extended & IVHD_DEV_EXT_ATS_DISABLE) ?
236 case ACPI_IVRS_TYPE_END:
237 range_end_id = de->Id;
238 ivhd_dev_add_entry(softc, range_start_id, range_end_id,
239 range_data | all_data, range_enable_ats);
240 range_start_id = range_end_id = 0;
245 case ACPI_IVRS_TYPE_PAD4:
248 case ACPI_IVRS_TYPE_SPECIAL:
252 if ((de->Type < 5) ||
253 (de->Type >= ACPI_IVRS_TYPE_PAD8))
254 device_printf(softc->dev,
255 "Unknown dev entry:0x%x\n", de->Type);
258 if (softc->dev_cfg_cnt >
259 (sizeof(softc->dev_cfg) / sizeof(softc->dev_cfg[0]))) {
260 device_printf(softc->dev,
261 "WARN Too many device entries.\n");
267 KASSERT((softc->end_dev_rid >= softc->start_dev_rid),
268 ("Device end[0x%x] < start[0x%x.\n",
269 softc->end_dev_rid, softc->start_dev_rid));
275 ivhd_identify(driver_t *driver, device_t parent)
277 ACPI_TABLE_IVRS *ivrs;
278 ACPI_IVRS_HARDWARE *ivhd;
283 if (acpi_disabled("ivhd"))
286 status = AcpiGetTable(ACPI_SIG_IVRS, 1, (ACPI_TABLE_HEADER **)&ivrs);
287 if (ACPI_FAILURE(status))
290 if (ivrs->Header.Length == 0) {
295 printf("AMD-Vi IVRS VAsize = %d PAsize = %d GVAsize = %d flags:%b\n",
296 REG_BITS(info, 21, 15), REG_BITS(info, 14, 8),
297 REG_BITS(info, 7, 5), REG_BITS(info, 22, 22),
298 "\020\001HtAtsResv");
300 ivrs_hdr_iterate_tbl(ivhd_count_iter, NULL);
304 ivhd_devs = malloc(sizeof(device_t) * ivhd_count, M_DEVBUF,
306 for (i = 0; i < ivhd_count; i++) {
307 ivhd = ivhd_find_by_index(i);
309 printf("Can't find IVHD entry%d\n", i);
313 ivhd_devs[i] = BUS_ADD_CHILD(parent, 1, "ivhd", i);
315 * XXX: In case device was not destroyed before, add will fail.
316 * locate the old device instance.
318 if (ivhd_devs[i] == NULL) {
319 ivhd_devs[i] = device_find_child(parent, "ivhd", i);
320 if (ivhd_devs[i] == NULL) {
321 printf("AMD-Vi: cant find AMD-Vi dev%d\n", i);
329 * Update device count in case failed to attach.
335 ivhd_probe(device_t dev)
338 if (acpi_get_handle(dev) != NULL)
340 device_set_desc(dev, "AMD-Vi/IOMMU or ivhd");
342 return (BUS_PROBE_NOWILDCARD);
346 ivhd_print_cap(struct amdvi_softc *softc, ACPI_IVRS_HARDWARE * ivhd)
352 device_printf(dev, "Flag:%b\n", softc->ivhd_flag,
353 "\020\001HtTunEn\002PassPW\003ResPassPW\004Isoc\005IotlbSup"
354 "\006Coherent\007PreFSup\008PPRSup");
356 * If no extended feature[EFR], its rev1 with maximum paging level as 7.
359 if (softc->ivhd_efr) {
360 device_printf(dev, "EFR HATS = %d GATS = %d GLXSup = %d "
361 "MsiNumPr = %d PNBanks= %d PNCounters= %d\n"
362 "max PASID = %d EFR: %b \n",
363 REG_BITS(softc->ivhd_efr, 31, 30),
364 REG_BITS(softc->ivhd_efr, 29, 28),
365 REG_BITS(softc->ivhd_efr, 4, 3),
366 REG_BITS(softc->ivhd_efr, 27, 23),
367 REG_BITS(softc->ivhd_efr, 22, 17),
368 REG_BITS(softc->ivhd_efr, 16, 13),
369 REG_BITS(softc->ivhd_efr, 12, 8),
370 softc->ivhd_efr, "\020\001XTSup\002NXSup\003GTSup\005IASup"
371 "\006GASup\007HESup\008PPRSup");
373 max_ptp_level = REG_BITS(softc->ivhd_efr, 31, 30) + 4;
376 /* Make sure device support minimum page level as requested by user. */
377 if (max_ptp_level < amdvi_ptp_level) {
378 device_printf(dev, "Insufficient PTP level:%d\n",
383 device_printf(softc->dev, "max supported paging level:%d restricting to: %d\n",
384 max_ptp_level, amdvi_ptp_level);
385 device_printf(softc->dev, "device supported range "
386 "[0x%x - 0x%x]\n", softc->start_dev_rid, softc->end_dev_rid);
392 ivhd_attach(device_t dev)
394 ACPI_IVRS_HARDWARE *ivhd;
395 struct amdvi_softc *softc;
398 unit = device_get_unit(dev);
399 /* Make sure its same device for which attach is called. */
400 if (ivhd_devs[unit] != dev)
401 panic("Not same device old %p new %p", ivhd_devs[unit], dev);
403 softc = device_get_softc(dev);
405 ivhd = ivhd_find_by_index(unit);
409 softc->pci_seg = ivhd->PciSegmentGroup;
410 softc->pci_rid = ivhd->Header.DeviceId;
411 softc->ivhd_flag = ivhd->Header.Flags;
412 softc->ivhd_efr = ivhd->Reserved;
414 * PCI capability has more capabilities that are not part of IVRS.
416 softc->cap_off = ivhd->CapabilityOffset;
419 /* IVHD Info bit[4:0] is event MSI/X number. */
420 softc->event_msix = ivhd->Info & 0x1F;
422 softc->ctrl = (struct amdvi_ctrl *) PHYS_TO_DMAP(ivhd->BaseAddress);
423 status = ivhd_dev_parse(ivhd, softc);
426 "endpoint device parsing error=%d\n", status);
429 status = ivhd_print_cap(softc, ivhd);
434 status = amdvi_setup_hw(softc);
436 device_printf(dev, "couldn't be initialised, error=%d\n",
445 ivhd_detach(device_t dev)
447 struct amdvi_softc *softc;
449 softc = device_get_softc(dev);
451 amdvi_teardown_hw(softc);
454 * XXX: delete the device.
455 * don't allow detach, return EBUSY.
461 ivhd_suspend(device_t dev)
468 ivhd_resume(device_t dev)
474 static device_method_t ivhd_methods[] = {
475 DEVMETHOD(device_identify, ivhd_identify),
476 DEVMETHOD(device_probe, ivhd_probe),
477 DEVMETHOD(device_attach, ivhd_attach),
478 DEVMETHOD(device_detach, ivhd_detach),
479 DEVMETHOD(device_suspend, ivhd_suspend),
480 DEVMETHOD(device_resume, ivhd_resume),
484 static driver_t ivhd_driver = {
487 sizeof(struct amdvi_softc),
490 static devclass_t ivhd_devclass;
493 * Load this module at the end after PCI re-probing to configure interrupt.
495 DRIVER_MODULE_ORDERED(ivhd, acpi, ivhd_driver, ivhd_devclass, 0, 0,
497 MODULE_DEPEND(ivhd, acpi, 1, 1, 1);
498 MODULE_DEPEND(ivhd, pci, 1, 1, 1);