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