2 * Copyright (c) 2006, Intel Corporation.
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.
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
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.
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>
21 * This file implements early detection/parsing of DMA Remapping Devices
22 * reported to OS through BIOS via DMA remapping reporting (DMAR) ACPI
26 #include <linux/pci.h>
27 #include <linux/dmar.h>
30 #define PREFIX "DMAR:"
32 /* No locks are needed as DMA remapping hardware unit
33 * list is constructed at boot time and hotplug of
34 * these units are not supported by the architecture.
36 LIST_HEAD(dmar_drhd_units
);
37 LIST_HEAD(dmar_rmrr_units
);
39 static struct acpi_table_header
* __initdata dmar_tbl
;
41 static void __init
dmar_register_drhd_unit(struct dmar_drhd_unit
*drhd
)
44 * add INCLUDE_ALL at the tail, so scan the list will find it at
47 if (drhd
->include_all
)
48 list_add_tail(&drhd
->list
, &dmar_drhd_units
);
50 list_add(&drhd
->list
, &dmar_drhd_units
);
53 static void __init
dmar_register_rmrr_unit(struct dmar_rmrr_unit
*rmrr
)
55 list_add(&rmrr
->list
, &dmar_rmrr_units
);
58 static int __init
dmar_parse_one_dev_scope(struct acpi_dmar_device_scope
*scope
,
59 struct pci_dev
**dev
, u16 segment
)
62 struct pci_dev
*pdev
= NULL
;
63 struct acpi_dmar_pci_path
*path
;
66 bus
= pci_find_bus(segment
, scope
->bus
);
67 path
= (struct acpi_dmar_pci_path
*)(scope
+ 1);
68 count
= (scope
->length
- sizeof(struct acpi_dmar_device_scope
))
69 / sizeof(struct acpi_dmar_pci_path
);
75 * Some BIOSes list non-exist devices in DMAR table, just
80 PREFIX
"Device scope bus [%d] not found\n",
84 pdev
= pci_get_slot(bus
, PCI_DEVFN(path
->dev
, path
->fn
));
86 printk(KERN_WARNING PREFIX
87 "Device scope device [%04x:%02x:%02x.%02x] not found\n",
88 segment
, bus
->number
, path
->dev
, path
->fn
);
93 bus
= pdev
->subordinate
;
96 printk(KERN_WARNING PREFIX
97 "Device scope device [%04x:%02x:%02x.%02x] not found\n",
98 segment
, scope
->bus
, path
->dev
, path
->fn
);
102 if ((scope
->entry_type
== ACPI_DMAR_SCOPE_TYPE_ENDPOINT
&& \
103 pdev
->subordinate
) || (scope
->entry_type
== \
104 ACPI_DMAR_SCOPE_TYPE_BRIDGE
&& !pdev
->subordinate
)) {
106 printk(KERN_WARNING PREFIX
107 "Device scope type does not match for %s\n",
115 static int __init
dmar_parse_dev_scope(void *start
, void *end
, int *cnt
,
116 struct pci_dev
***devices
, u16 segment
)
118 struct acpi_dmar_device_scope
*scope
;
124 while (start
< end
) {
126 if (scope
->entry_type
== ACPI_DMAR_SCOPE_TYPE_ENDPOINT
||
127 scope
->entry_type
== ACPI_DMAR_SCOPE_TYPE_BRIDGE
)
130 printk(KERN_WARNING PREFIX
131 "Unsupported device scope\n");
132 start
+= scope
->length
;
137 *devices
= kcalloc(*cnt
, sizeof(struct pci_dev
*), GFP_KERNEL
);
143 while (start
< end
) {
145 if (scope
->entry_type
== ACPI_DMAR_SCOPE_TYPE_ENDPOINT
||
146 scope
->entry_type
== ACPI_DMAR_SCOPE_TYPE_BRIDGE
) {
147 ret
= dmar_parse_one_dev_scope(scope
,
148 &(*devices
)[index
], segment
);
155 start
+= scope
->length
;
162 * dmar_parse_one_drhd - parses exactly one DMA remapping hardware definition
163 * structure which uniquely represent one DMA remapping hardware unit
164 * present in the platform
167 dmar_parse_one_drhd(struct acpi_dmar_header
*header
)
169 struct acpi_dmar_hardware_unit
*drhd
;
170 struct dmar_drhd_unit
*dmaru
;
172 static int include_all
;
174 dmaru
= kzalloc(sizeof(*dmaru
), GFP_KERNEL
);
178 drhd
= (struct acpi_dmar_hardware_unit
*)header
;
179 dmaru
->reg_base_addr
= drhd
->address
;
180 dmaru
->include_all
= drhd
->flags
& 0x1; /* BIT0: INCLUDE_ALL */
182 if (!dmaru
->include_all
)
183 ret
= dmar_parse_dev_scope((void *)(drhd
+ 1),
184 ((void *)drhd
) + header
->length
,
185 &dmaru
->devices_cnt
, &dmaru
->devices
,
188 /* Only allow one INCLUDE_ALL */
190 printk(KERN_WARNING PREFIX
"Only one INCLUDE_ALL "
191 "device scope is allowed\n");
197 if (ret
|| (dmaru
->devices_cnt
== 0 && !dmaru
->include_all
))
200 dmar_register_drhd_unit(dmaru
);
205 dmar_parse_one_rmrr(struct acpi_dmar_header
*header
)
207 struct acpi_dmar_reserved_memory
*rmrr
;
208 struct dmar_rmrr_unit
*rmrru
;
211 rmrru
= kzalloc(sizeof(*rmrru
), GFP_KERNEL
);
215 rmrr
= (struct acpi_dmar_reserved_memory
*)header
;
216 rmrru
->base_address
= rmrr
->base_address
;
217 rmrru
->end_address
= rmrr
->end_address
;
218 ret
= dmar_parse_dev_scope((void *)(rmrr
+ 1),
219 ((void *)rmrr
) + header
->length
,
220 &rmrru
->devices_cnt
, &rmrru
->devices
, rmrr
->segment
);
222 if (ret
|| (rmrru
->devices_cnt
== 0))
225 dmar_register_rmrr_unit(rmrru
);
230 dmar_table_print_dmar_entry(struct acpi_dmar_header
*header
)
232 struct acpi_dmar_hardware_unit
*drhd
;
233 struct acpi_dmar_reserved_memory
*rmrr
;
235 switch (header
->type
) {
236 case ACPI_DMAR_TYPE_HARDWARE_UNIT
:
237 drhd
= (struct acpi_dmar_hardware_unit
*)header
;
238 printk (KERN_INFO PREFIX
239 "DRHD (flags: 0x%08x)base: 0x%016Lx\n",
240 drhd
->flags
, drhd
->address
);
242 case ACPI_DMAR_TYPE_RESERVED_MEMORY
:
243 rmrr
= (struct acpi_dmar_reserved_memory
*)header
;
245 printk (KERN_INFO PREFIX
246 "RMRR base: 0x%016Lx end: 0x%016Lx\n",
247 rmrr
->base_address
, rmrr
->end_address
);
253 * parse_dmar_table - parses the DMA reporting table
256 parse_dmar_table(void)
258 struct acpi_table_dmar
*dmar
;
259 struct acpi_dmar_header
*entry_header
;
262 dmar
= (struct acpi_table_dmar
*)dmar_tbl
;
267 printk (KERN_WARNING PREFIX
"Zero: Invalid DMAR haw\n");
271 printk (KERN_INFO PREFIX
"Host address width %d\n",
274 entry_header
= (struct acpi_dmar_header
*)(dmar
+ 1);
275 while (((unsigned long)entry_header
) <
276 (((unsigned long)dmar
) + dmar_tbl
->length
)) {
277 dmar_table_print_dmar_entry(entry_header
);
279 switch (entry_header
->type
) {
280 case ACPI_DMAR_TYPE_HARDWARE_UNIT
:
281 ret
= dmar_parse_one_drhd(entry_header
);
283 case ACPI_DMAR_TYPE_RESERVED_MEMORY
:
284 ret
= dmar_parse_one_rmrr(entry_header
);
287 printk(KERN_WARNING PREFIX
288 "Unknown DMAR structure type\n");
289 ret
= 0; /* for forward compatibility */
295 entry_header
= ((void *)entry_header
+ entry_header
->length
);
301 int __init
dmar_table_init(void)
305 if (list_empty(&dmar_drhd_units
)) {
306 printk(KERN_INFO PREFIX
"No DMAR devices found\n");
313 * early_dmar_detect - checks to see if the platform supports DMAR devices
315 int __init
early_dmar_detect(void)
317 acpi_status status
= AE_OK
;
319 /* if we could find DMAR table, then there are DMAR devices */
320 status
= acpi_get_table(ACPI_SIG_DMAR
, 0,
321 (struct acpi_table_header
**)&dmar_tbl
);
323 if (ACPI_SUCCESS(status
) && !dmar_tbl
) {
324 printk (KERN_WARNING PREFIX
"Unable to map DMAR\n");
325 status
= AE_NOT_FOUND
;
328 return (ACPI_SUCCESS(status
) ? 1 : 0);