1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2021 The FreeBSD Foundation 5 * 6 * Portions of this software were developed by Ka Ho Ng 7 * under sponsorship from the FreeBSD Foundation. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice unmodified, this list of conditions, and the following 14 * disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 #include <sys/cdefs.h> 32 33 #include <sys/param.h> 34 #include <sys/bus.h> 35 #include <sys/kernel.h> 36 #include <sys/module.h> 37 #include <sys/rman.h> 38 39 #include <dev/pci/pcireg.h> 40 #include <dev/pci/pcivar.h> 41 42 #include "amdvi_priv.h" 43 #include "ivhd_if.h" 44 45 struct amdiommu_softc { 46 struct resource *event_res; /* Event interrupt resource. */ 47 void *event_tag; /* Event interrupt tag. */ 48 int event_rid; 49 }; 50 51 static int amdiommu_probe(device_t); 52 static int amdiommu_attach(device_t); 53 static int amdiommu_detach(device_t); 54 static int ivhd_setup_intr(device_t, driver_intr_t, void *, 55 const char *); 56 static int ivhd_teardown_intr(device_t); 57 58 static device_method_t amdiommu_methods[] = { 59 /* device interface */ 60 DEVMETHOD(device_probe, amdiommu_probe), 61 DEVMETHOD(device_attach, amdiommu_attach), 62 DEVMETHOD(device_detach, amdiommu_detach), 63 DEVMETHOD(ivhd_setup_intr, ivhd_setup_intr), 64 DEVMETHOD(ivhd_teardown_intr, ivhd_teardown_intr), 65 DEVMETHOD_END 66 }; 67 static driver_t amdiommu_driver = { 68 "amdiommu", 69 amdiommu_methods, 70 sizeof (struct amdiommu_softc), 71 }; 72 73 static int 74 amdiommu_probe(device_t dev) 75 { 76 int error; 77 int capoff; 78 79 /* 80 * Check base class and sub-class 81 */ 82 if (pci_get_class(dev) != PCIC_BASEPERIPH || 83 pci_get_subclass(dev) != PCIS_BASEPERIPH_IOMMU) 84 return (ENXIO); 85 86 /* 87 * A IOMMU capability block carries a 0Fh capid. 88 */ 89 error = pci_find_cap(dev, PCIY_SECDEV, &capoff); 90 if (error) 91 return (ENXIO); 92 93 /* 94 * bit [18:16] == 011b indicates the capability block is IOMMU 95 * capability block. If the field is not set to 011b, bail out. 96 */ 97 if ((pci_read_config(dev, capoff + 2, 2) & 0x7) != 0x3) 98 return (ENXIO); 99 100 return (BUS_PROBE_SPECIFIC); 101 } 102 103 static int 104 amdiommu_attach(device_t dev) 105 { 106 107 device_set_desc(dev, "AMD-Vi/IOMMU PCI function"); 108 return (0); 109 } 110 111 static int 112 amdiommu_detach(device_t dev) 113 { 114 115 return (0); 116 } 117 118 static int 119 ivhd_setup_intr(device_t dev, driver_intr_t handler, void *arg, 120 const char *desc) 121 { 122 struct amdiommu_softc *sc; 123 int error, msicnt; 124 125 sc = device_get_softc(dev); 126 msicnt = 1; 127 if (sc->event_res != NULL) 128 panic("%s is called without intr teardown", __func__); 129 sc->event_rid = 1; 130 131 error = pci_alloc_msi(dev, &msicnt); 132 if (error) { 133 device_printf(dev, "Couldn't find event MSI IRQ resource.\n"); 134 return (ENOENT); 135 } 136 137 sc->event_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, 138 &sc->event_rid, RF_ACTIVE); 139 if (sc->event_res == NULL) { 140 device_printf(dev, "Unable to allocate event INTR resource.\n"); 141 error = ENOMEM; 142 goto fail; 143 } 144 145 error = bus_setup_intr(dev, sc->event_res, INTR_TYPE_MISC | INTR_MPSAFE, 146 NULL, handler, arg, &sc->event_tag); 147 if (error) { 148 device_printf(dev, "Fail to setup event intr\n"); 149 goto fail; 150 } 151 152 bus_describe_intr(dev, sc->event_res, sc->event_tag, "%s", desc); 153 return (0); 154 155 fail: 156 ivhd_teardown_intr(dev); 157 return (error); 158 } 159 160 static int 161 ivhd_teardown_intr(device_t dev) 162 { 163 struct amdiommu_softc *sc; 164 165 sc = device_get_softc(dev); 166 167 if (sc->event_tag != NULL) { 168 bus_teardown_intr(dev, sc->event_res, sc->event_tag); 169 sc->event_tag = NULL; 170 } 171 if (sc->event_res != NULL) { 172 bus_release_resource(dev, SYS_RES_IRQ, sc->event_rid, 173 sc->event_res); 174 sc->event_res = NULL; 175 } 176 pci_release_msi(dev); 177 return (0); 178 } 179 180 static devclass_t amdiommu_devclass; 181 182 /* This driver has to be loaded before ivhd */ 183 DRIVER_MODULE(amdiommu, pci, amdiommu_driver, amdiommu_devclass, 0, 0); 184 MODULE_DEPEND(amdiommu, pci, 1, 1, 1); 185