]>
Commit | Line | Data |
---|---|---|
10e5247f KA |
1 | /* |
2 | * Copyright (c) 2006, Intel Corporation. | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify it | |
5 | * under the terms and conditions of the GNU General Public License, | |
6 | * version 2, as published by the Free Software Foundation. | |
7 | * | |
8 | * This program is distributed in the hope it will be useful, but WITHOUT | |
9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
11 | * more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public License along with | |
14 | * this program; if not, write to the Free Software Foundation, Inc., 59 Temple | |
15 | * Place - Suite 330, Boston, MA 02111-1307 USA. | |
16 | * | |
17 | * Copyright (C) Ashok Raj <ashok.raj@intel.com> | |
18 | * Copyright (C) Shaohua Li <shaohua.li@intel.com> | |
19 | * Copyright (C) Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com> | |
20 | * | |
21 | * This file implements early detection/parsing of DMA Remapping Devices | |
22 | * reported to OS through BIOS via DMA remapping reporting (DMAR) ACPI | |
23 | * tables. | |
24 | */ | |
25 | ||
26 | #include <linux/pci.h> | |
27 | #include <linux/dmar.h> | |
093f87d2 | 28 | #include "iova.h" |
f661197e | 29 | #include "intel-iommu.h" |
10e5247f KA |
30 | |
31 | #undef PREFIX | |
32 | #define PREFIX "DMAR:" | |
33 | ||
34 | /* No locks are needed as DMA remapping hardware unit | |
35 | * list is constructed at boot time and hotplug of | |
36 | * these units are not supported by the architecture. | |
37 | */ | |
38 | LIST_HEAD(dmar_drhd_units); | |
39 | LIST_HEAD(dmar_rmrr_units); | |
40 | ||
41 | static struct acpi_table_header * __initdata dmar_tbl; | |
42 | ||
43 | static void __init dmar_register_drhd_unit(struct dmar_drhd_unit *drhd) | |
44 | { | |
45 | /* | |
46 | * add INCLUDE_ALL at the tail, so scan the list will find it at | |
47 | * the very end. | |
48 | */ | |
49 | if (drhd->include_all) | |
50 | list_add_tail(&drhd->list, &dmar_drhd_units); | |
51 | else | |
52 | list_add(&drhd->list, &dmar_drhd_units); | |
53 | } | |
54 | ||
55 | static void __init dmar_register_rmrr_unit(struct dmar_rmrr_unit *rmrr) | |
56 | { | |
57 | list_add(&rmrr->list, &dmar_rmrr_units); | |
58 | } | |
59 | ||
60 | static int __init dmar_parse_one_dev_scope(struct acpi_dmar_device_scope *scope, | |
61 | struct pci_dev **dev, u16 segment) | |
62 | { | |
63 | struct pci_bus *bus; | |
64 | struct pci_dev *pdev = NULL; | |
65 | struct acpi_dmar_pci_path *path; | |
66 | int count; | |
67 | ||
68 | bus = pci_find_bus(segment, scope->bus); | |
69 | path = (struct acpi_dmar_pci_path *)(scope + 1); | |
70 | count = (scope->length - sizeof(struct acpi_dmar_device_scope)) | |
71 | / sizeof(struct acpi_dmar_pci_path); | |
72 | ||
73 | while (count) { | |
74 | if (pdev) | |
75 | pci_dev_put(pdev); | |
76 | /* | |
77 | * Some BIOSes list non-exist devices in DMAR table, just | |
78 | * ignore it | |
79 | */ | |
80 | if (!bus) { | |
81 | printk(KERN_WARNING | |
82 | PREFIX "Device scope bus [%d] not found\n", | |
83 | scope->bus); | |
84 | break; | |
85 | } | |
86 | pdev = pci_get_slot(bus, PCI_DEVFN(path->dev, path->fn)); | |
87 | if (!pdev) { | |
88 | printk(KERN_WARNING PREFIX | |
89 | "Device scope device [%04x:%02x:%02x.%02x] not found\n", | |
90 | segment, bus->number, path->dev, path->fn); | |
91 | break; | |
92 | } | |
93 | path ++; | |
94 | count --; | |
95 | bus = pdev->subordinate; | |
96 | } | |
97 | if (!pdev) { | |
98 | printk(KERN_WARNING PREFIX | |
99 | "Device scope device [%04x:%02x:%02x.%02x] not found\n", | |
100 | segment, scope->bus, path->dev, path->fn); | |
101 | *dev = NULL; | |
102 | return 0; | |
103 | } | |
104 | if ((scope->entry_type == ACPI_DMAR_SCOPE_TYPE_ENDPOINT && \ | |
105 | pdev->subordinate) || (scope->entry_type == \ | |
106 | ACPI_DMAR_SCOPE_TYPE_BRIDGE && !pdev->subordinate)) { | |
107 | pci_dev_put(pdev); | |
108 | printk(KERN_WARNING PREFIX | |
109 | "Device scope type does not match for %s\n", | |
110 | pci_name(pdev)); | |
111 | return -EINVAL; | |
112 | } | |
113 | *dev = pdev; | |
114 | return 0; | |
115 | } | |
116 | ||
117 | static int __init dmar_parse_dev_scope(void *start, void *end, int *cnt, | |
118 | struct pci_dev ***devices, u16 segment) | |
119 | { | |
120 | struct acpi_dmar_device_scope *scope; | |
121 | void * tmp = start; | |
122 | int index; | |
123 | int ret; | |
124 | ||
125 | *cnt = 0; | |
126 | while (start < end) { | |
127 | scope = start; | |
128 | if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_ENDPOINT || | |
129 | scope->entry_type == ACPI_DMAR_SCOPE_TYPE_BRIDGE) | |
130 | (*cnt)++; | |
131 | else | |
132 | printk(KERN_WARNING PREFIX | |
133 | "Unsupported device scope\n"); | |
134 | start += scope->length; | |
135 | } | |
136 | if (*cnt == 0) | |
137 | return 0; | |
138 | ||
139 | *devices = kcalloc(*cnt, sizeof(struct pci_dev *), GFP_KERNEL); | |
140 | if (!*devices) | |
141 | return -ENOMEM; | |
142 | ||
143 | start = tmp; | |
144 | index = 0; | |
145 | while (start < end) { | |
146 | scope = start; | |
147 | if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_ENDPOINT || | |
148 | scope->entry_type == ACPI_DMAR_SCOPE_TYPE_BRIDGE) { | |
149 | ret = dmar_parse_one_dev_scope(scope, | |
150 | &(*devices)[index], segment); | |
151 | if (ret) { | |
152 | kfree(*devices); | |
153 | return ret; | |
154 | } | |
155 | index ++; | |
156 | } | |
157 | start += scope->length; | |
158 | } | |
159 | ||
160 | return 0; | |
161 | } | |
162 | ||
163 | /** | |
164 | * dmar_parse_one_drhd - parses exactly one DMA remapping hardware definition | |
165 | * structure which uniquely represent one DMA remapping hardware unit | |
166 | * present in the platform | |
167 | */ | |
168 | static int __init | |
169 | dmar_parse_one_drhd(struct acpi_dmar_header *header) | |
170 | { | |
171 | struct acpi_dmar_hardware_unit *drhd; | |
172 | struct dmar_drhd_unit *dmaru; | |
173 | int ret = 0; | |
174 | static int include_all; | |
175 | ||
176 | dmaru = kzalloc(sizeof(*dmaru), GFP_KERNEL); | |
177 | if (!dmaru) | |
178 | return -ENOMEM; | |
179 | ||
180 | drhd = (struct acpi_dmar_hardware_unit *)header; | |
181 | dmaru->reg_base_addr = drhd->address; | |
182 | dmaru->include_all = drhd->flags & 0x1; /* BIT0: INCLUDE_ALL */ | |
183 | ||
184 | if (!dmaru->include_all) | |
185 | ret = dmar_parse_dev_scope((void *)(drhd + 1), | |
186 | ((void *)drhd) + header->length, | |
187 | &dmaru->devices_cnt, &dmaru->devices, | |
188 | drhd->segment); | |
189 | else { | |
190 | /* Only allow one INCLUDE_ALL */ | |
191 | if (include_all) { | |
192 | printk(KERN_WARNING PREFIX "Only one INCLUDE_ALL " | |
193 | "device scope is allowed\n"); | |
194 | ret = -EINVAL; | |
195 | } | |
196 | include_all = 1; | |
197 | } | |
198 | ||
199 | if (ret || (dmaru->devices_cnt == 0 && !dmaru->include_all)) | |
200 | kfree(dmaru); | |
201 | else | |
202 | dmar_register_drhd_unit(dmaru); | |
203 | return ret; | |
204 | } | |
205 | ||
206 | static int __init | |
207 | dmar_parse_one_rmrr(struct acpi_dmar_header *header) | |
208 | { | |
209 | struct acpi_dmar_reserved_memory *rmrr; | |
210 | struct dmar_rmrr_unit *rmrru; | |
211 | int ret = 0; | |
212 | ||
213 | rmrru = kzalloc(sizeof(*rmrru), GFP_KERNEL); | |
214 | if (!rmrru) | |
215 | return -ENOMEM; | |
216 | ||
217 | rmrr = (struct acpi_dmar_reserved_memory *)header; | |
218 | rmrru->base_address = rmrr->base_address; | |
219 | rmrru->end_address = rmrr->end_address; | |
220 | ret = dmar_parse_dev_scope((void *)(rmrr + 1), | |
221 | ((void *)rmrr) + header->length, | |
222 | &rmrru->devices_cnt, &rmrru->devices, rmrr->segment); | |
223 | ||
224 | if (ret || (rmrru->devices_cnt == 0)) | |
225 | kfree(rmrru); | |
226 | else | |
227 | dmar_register_rmrr_unit(rmrru); | |
228 | return ret; | |
229 | } | |
230 | ||
231 | static void __init | |
232 | dmar_table_print_dmar_entry(struct acpi_dmar_header *header) | |
233 | { | |
234 | struct acpi_dmar_hardware_unit *drhd; | |
235 | struct acpi_dmar_reserved_memory *rmrr; | |
236 | ||
237 | switch (header->type) { | |
238 | case ACPI_DMAR_TYPE_HARDWARE_UNIT: | |
239 | drhd = (struct acpi_dmar_hardware_unit *)header; | |
240 | printk (KERN_INFO PREFIX | |
241 | "DRHD (flags: 0x%08x)base: 0x%016Lx\n", | |
242 | drhd->flags, drhd->address); | |
243 | break; | |
244 | case ACPI_DMAR_TYPE_RESERVED_MEMORY: | |
245 | rmrr = (struct acpi_dmar_reserved_memory *)header; | |
246 | ||
247 | printk (KERN_INFO PREFIX | |
248 | "RMRR base: 0x%016Lx end: 0x%016Lx\n", | |
249 | rmrr->base_address, rmrr->end_address); | |
250 | break; | |
251 | } | |
252 | } | |
253 | ||
254 | /** | |
255 | * parse_dmar_table - parses the DMA reporting table | |
256 | */ | |
257 | static int __init | |
258 | parse_dmar_table(void) | |
259 | { | |
260 | struct acpi_table_dmar *dmar; | |
261 | struct acpi_dmar_header *entry_header; | |
262 | int ret = 0; | |
263 | ||
264 | dmar = (struct acpi_table_dmar *)dmar_tbl; | |
265 | if (!dmar) | |
266 | return -ENODEV; | |
267 | ||
093f87d2 FY |
268 | if (dmar->width < PAGE_SHIFT_4K - 1) { |
269 | printk(KERN_WARNING PREFIX "Invalid DMAR haw\n"); | |
10e5247f KA |
270 | return -EINVAL; |
271 | } | |
272 | ||
273 | printk (KERN_INFO PREFIX "Host address width %d\n", | |
274 | dmar->width + 1); | |
275 | ||
276 | entry_header = (struct acpi_dmar_header *)(dmar + 1); | |
277 | while (((unsigned long)entry_header) < | |
278 | (((unsigned long)dmar) + dmar_tbl->length)) { | |
279 | dmar_table_print_dmar_entry(entry_header); | |
280 | ||
281 | switch (entry_header->type) { | |
282 | case ACPI_DMAR_TYPE_HARDWARE_UNIT: | |
283 | ret = dmar_parse_one_drhd(entry_header); | |
284 | break; | |
285 | case ACPI_DMAR_TYPE_RESERVED_MEMORY: | |
286 | ret = dmar_parse_one_rmrr(entry_header); | |
287 | break; | |
288 | default: | |
289 | printk(KERN_WARNING PREFIX | |
290 | "Unknown DMAR structure type\n"); | |
291 | ret = 0; /* for forward compatibility */ | |
292 | break; | |
293 | } | |
294 | if (ret) | |
295 | break; | |
296 | ||
297 | entry_header = ((void *)entry_header + entry_header->length); | |
298 | } | |
299 | return ret; | |
300 | } | |
301 | ||
302 | ||
303 | int __init dmar_table_init(void) | |
304 | { | |
305 | ||
093f87d2 FY |
306 | int ret; |
307 | ||
308 | ret = parse_dmar_table(); | |
309 | if (ret) { | |
310 | printk(KERN_INFO PREFIX "parse DMAR table failure.\n"); | |
311 | return ret; | |
312 | } | |
313 | ||
10e5247f KA |
314 | if (list_empty(&dmar_drhd_units)) { |
315 | printk(KERN_INFO PREFIX "No DMAR devices found\n"); | |
316 | return -ENODEV; | |
317 | } | |
093f87d2 FY |
318 | |
319 | if (list_empty(&dmar_rmrr_units)) { | |
320 | printk(KERN_INFO PREFIX "No RMRR found\n"); | |
321 | return -ENODEV; | |
322 | } | |
323 | ||
10e5247f KA |
324 | return 0; |
325 | } | |
326 | ||
327 | /** | |
328 | * early_dmar_detect - checks to see if the platform supports DMAR devices | |
329 | */ | |
330 | int __init early_dmar_detect(void) | |
331 | { | |
332 | acpi_status status = AE_OK; | |
333 | ||
334 | /* if we could find DMAR table, then there are DMAR devices */ | |
335 | status = acpi_get_table(ACPI_SIG_DMAR, 0, | |
336 | (struct acpi_table_header **)&dmar_tbl); | |
337 | ||
338 | if (ACPI_SUCCESS(status) && !dmar_tbl) { | |
339 | printk (KERN_WARNING PREFIX "Unable to map DMAR\n"); | |
340 | status = AE_NOT_FOUND; | |
341 | } | |
342 | ||
343 | return (ACPI_SUCCESS(status) ? 1 : 0); | |
344 | } |