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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
|
// SPDX-License-Identifier: GPL-2.0-only
#include <linux/acpi.h>
#include <linux/acpi_iovt.h>
#include <linux/pci.h>
#include "init.h"
struct iovt_device_entry {
struct list_head list;
int start_devid, end_devid;
};
struct iovt_fwnode {
struct list_head list;
struct fwnode_handle *fwnode;
int flag;
int nid, segment, devid;
struct list_head ep_list;
};
static bool checksum_valid;
static LIST_HEAD(iovt_fwnode_list);
static DEFINE_SPINLOCK(iovt_fwnode_lock);
static u8 acpi_iovt_checksum(u8 *buffer, u32 len)
{
u8 sum = 0, *end = buffer + len;
while (buffer < end)
sum += *buffer++;
return sum;
}
#ifdef CONFIG_PCI
static void __init iovt_enable_acs(struct acpi_iovt_iommu *iommu)
{
static bool acs_enabled __initdata;
if (acs_enabled)
return;
/* IOMMU V1 only supports PCI device management */
if ((iommu->header.type == ACPI_IOVT_IOMMU_V1) ||
(iommu->flags & (ACPI_IOVT_IOMMU_PCI_DEVICE | ACPI_IOVT_IOMMU_MAGAGE_BY_SEGMENT))) {
pci_request_acs();
acs_enabled = true;
}
}
#else
static inline void iovt_enable_acs(struct acpi_iovt_iommu *iommu) { }
#endif
static int __init iovt_get_pci_iommu_fwnode(struct iovt_fwnode *np, u16 segment, u16 bdf)
{
struct pci_dev *pdev;
struct fwnode_handle *fwnode;
pdev = pci_get_domain_bus_and_slot(segment, PCI_BUS_NUM(bdf), bdf & 0xff);
if (!pdev) {
pr_err("No PCI IOMMU found for segment 0x%x bdf 0x%x\n", segment, bdf);
return -ENODEV;
}
fwnode = dev_fwnode(&pdev->dev);
if (!fwnode) {
/*
* PCI devices aren't necessarily described by ACPI. Create
* a fwnode so the IOMMU subsystem can identify this device.
*/
fwnode = acpi_alloc_fwnode_static();
if (!fwnode) {
pci_dev_put(pdev);
return -ENOMEM;
}
set_primary_fwnode(&pdev->dev, fwnode);
}
np->fwnode = dev_fwnode(&pdev->dev);
if (np->flag & ACPI_IOVT_IOMMU_PXM_VALID)
set_dev_node(&pdev->dev, np->nid);
pci_dev_put(pdev);
return 0;
}
static int __init iovt_validate_entries(struct acpi_iovt_iommu *iommu)
{
int i, done = 1;
struct acpi_iovt_device_entry *ep;
ep = ACPI_ADD_PTR(struct acpi_iovt_device_entry, iommu, iommu->device_entry_offset);
for (i = 0; i < iommu->device_entry_num; i++) {
switch (ep->type) {
case ACPI_IOVT_DEVICE_ENTRY_START:
if (!done)
return -EINVAL;
done = 0;
break;
case ACPI_IOVT_DEVICE_ENTRY_END:
if (done)
return -EINVAL;
done = 1;
break;
case ACPI_IOVT_DEVICE_ENTRY_SINGLE:
default: /* Reserved for future */
if (!done)
return -EINVAL;
done = 1;
break;
}
ep = ACPI_ADD_PTR(struct acpi_iovt_device_entry, ep, ep->length);
}
return done ? 0 : -EINVAL;
}
static int __init iovt_add_iommu(struct acpi_iovt_iommu *iommu)
{
int i, ret;
struct iovt_fwnode *np;
struct iovt_device_entry *entry;
struct acpi_iovt_device_entry *ep;
np = kzalloc_obj(struct iovt_fwnode, GFP_ATOMIC);
if (WARN_ON(!np))
return -ENOMEM;
INIT_LIST_HEAD(&np->list);
np->flag = iommu->flags;
np->segment = iommu->segment;
if (np->flag & ACPI_IOVT_IOMMU_PXM_VALID)
np->nid = pxm_to_node(iommu->proximity_domain);
if (np->flag & ACPI_IOVT_IOMMU_PCI_DEVICE) {
np->devid = iommu->device_id;
ret = iovt_get_pci_iommu_fwnode(np, np->segment, np->devid);
if (ret < 0) {
kfree(np);
return ret;
}
} else {
np->fwnode = acpi_alloc_fwnode_static();
if (!np->fwnode) {
kfree(np);
return -ENOMEM;
}
}
/* All devices in the segment are managed by this IOMMU */
if (np->flag & ACPI_IOVT_IOMMU_MAGAGE_BY_SEGMENT)
goto skip;
if (iovt_validate_entries(iommu) < 0) {
kfree(np);
return -EINVAL;
}
INIT_LIST_HEAD(&np->ep_list);
ep = ACPI_ADD_PTR(struct acpi_iovt_device_entry, iommu, iommu->device_entry_offset);
for (i = 0; i < iommu->device_entry_num; i++) {
switch (ep->type) {
case ACPI_IOVT_DEVICE_ENTRY_START:
entry = kzalloc_obj(struct iovt_device_entry, GFP_ATOMIC);
if (!entry)
return -ENOMEM;
entry->start_devid = ep->device_id;
break;
case ACPI_IOVT_DEVICE_ENTRY_END:
/* START & END are paired */
entry->end_devid = ep->device_id;
list_add_tail(&entry->list, &np->ep_list);
break;
case ACPI_IOVT_DEVICE_ENTRY_SINGLE:
entry = kzalloc_obj(struct iovt_device_entry, GFP_ATOMIC);
if (!entry)
return -ENOMEM;
entry->start_devid = ep->device_id;
entry->end_devid = ep->device_id;
list_add_tail(&entry->list, &np->ep_list);
break;
default:
break;
}
ep = ACPI_ADD_PTR(struct acpi_iovt_device_entry, ep, ep->length);
}
skip:
spin_lock(&iovt_fwnode_lock);
list_add_tail(&np->list, &iovt_fwnode_list);
spin_unlock(&iovt_fwnode_lock);
return 0;
}
static void __init iovt_init_devices(struct acpi_table_header *header)
{
int i;
struct acpi_table_iovt *iovt;
struct acpi_iovt_iommu *iommu;
/* Get the first IOVT node */
iovt = (struct acpi_table_iovt *)header;
iommu = ACPI_ADD_PTR(struct acpi_iovt_iommu, iovt, iovt->iommu_offset);
for (i = 0; i < iovt->iommu_count; i++) {
iovt_add_iommu(iommu);
iommu = ACPI_ADD_PTR(struct acpi_iovt_iommu, iommu, iommu->header.length);
}
}
void __init acpi_iovt_init(void)
{
int i;
acpi_status status;
struct acpi_table_header *hdr;
struct acpi_table_iovt *iovt;
struct acpi_iovt_iommu *iommu;
status = acpi_get_table(ACPI_SIG_IOVT, 0, &hdr);
if (ACPI_FAILURE(status)) {
if (status != AE_NOT_FOUND)
pr_err("Failed to get table, %s\n", acpi_format_exception(status));
return;
}
if (acpi_iovt_checksum((unsigned char *)hdr, hdr->length)) {
pr_err("IOVT invalid checksum\n");
acpi_put_table(hdr);
return;
}
checksum_valid = true;
iovt = (struct acpi_table_iovt *)hdr;
iommu = ACPI_ADD_PTR(struct acpi_iovt_iommu, iovt, iovt->iommu_offset);
for (i = 0; i < iovt->iommu_count; i++) {
iovt_enable_acs(iommu);
iommu = ACPI_ADD_PTR(struct acpi_iovt_iommu, iommu, iommu->header.length);
}
acpi_put_table(hdr);
}
void __init acpi_iovt_late_init(void)
{
acpi_status status;
struct acpi_table_header *hdr;
if (!checksum_valid)
return;
status = acpi_get_table(ACPI_SIG_IOVT, 0, &hdr);
if (ACPI_FAILURE(status)) {
if (status != AE_NOT_FOUND)
pr_err("Failed to get table, %s\n", acpi_format_exception(status));
return;
}
iovt_init_devices(hdr);
acpi_put_table(hdr);
}
static int iovt_pci_iommu_init(struct pci_dev *pdev, u16 dev_id, void *data)
{
struct iovt_fwnode *np;
struct iovt_device_entry *entry;
struct device *aliased_dev = data;
u32 domain = pci_domain_nr(pdev->bus);
list_for_each_entry(np, &iovt_fwnode_list, list) {
if (domain != np->segment)
continue;
/* We're not translating ourself */
if (dev_id == np->devid)
return -EINVAL;
if (np->flag & ACPI_IOVT_IOMMU_MAGAGE_BY_SEGMENT)
goto found;
list_for_each_entry(entry, &np->ep_list, list) {
if (dev_id >= entry->start_devid && dev_id <= entry->end_devid)
goto found;
}
}
return -ENODEV;
found:
return acpi_iommu_fwspec_init(aliased_dev, (domain << 16) + dev_id, np->fwnode);
}
/*
* iovt_iommu_configure_id - Set-up IOMMU configuration for a device.
*
* @dev: device to configure
* @id_in: optional input id const value pointer
*
* Returns: 0 on success, <0 on failure
*/
int iovt_iommu_configure_id(struct device *dev, const u32 *id_in)
{
if (!dev_is_pci(dev))
return -ENODEV;
return pci_for_each_dma_alias(to_pci_dev(dev), iovt_pci_iommu_init, dev);
}
|