Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / drivers / pci / dmar.c
blob9709cdceeb7cfbca73a41d9ae16a7a46c7945390
1 /*
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
11 * more details.
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 <<<<<<< HEAD:drivers/pci/dmar.c
18 * Copyright (C) Ashok Raj <ashok.raj@intel.com>
19 * Copyright (C) Shaohua Li <shaohua.li@intel.com>
20 * Copyright (C) Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>
21 =======
22 * Copyright (C) 2006-2008 Intel Corporation
23 * Author: Ashok Raj <ashok.raj@intel.com>
24 * Author: Shaohua Li <shaohua.li@intel.com>
25 * Author: Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>
26 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/pci/dmar.c
28 <<<<<<< HEAD:drivers/pci/dmar.c
29 * This file implements early detection/parsing of DMA Remapping Devices
30 =======
31 * This file implements early detection/parsing of DMA Remapping Devices
32 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/pci/dmar.c
33 * reported to OS through BIOS via DMA remapping reporting (DMAR) ACPI
34 * tables.
37 #include <linux/pci.h>
38 #include <linux/dmar.h>
39 #include "iova.h"
40 #include "intel-iommu.h"
42 #undef PREFIX
43 #define PREFIX "DMAR:"
45 /* No locks are needed as DMA remapping hardware unit
46 * list is constructed at boot time and hotplug of
47 * these units are not supported by the architecture.
49 LIST_HEAD(dmar_drhd_units);
50 LIST_HEAD(dmar_rmrr_units);
52 static struct acpi_table_header * __initdata dmar_tbl;
54 static void __init dmar_register_drhd_unit(struct dmar_drhd_unit *drhd)
57 * add INCLUDE_ALL at the tail, so scan the list will find it at
58 * the very end.
60 if (drhd->include_all)
61 list_add_tail(&drhd->list, &dmar_drhd_units);
62 else
63 list_add(&drhd->list, &dmar_drhd_units);
66 static void __init dmar_register_rmrr_unit(struct dmar_rmrr_unit *rmrr)
68 list_add(&rmrr->list, &dmar_rmrr_units);
71 static int __init dmar_parse_one_dev_scope(struct acpi_dmar_device_scope *scope,
72 struct pci_dev **dev, u16 segment)
74 struct pci_bus *bus;
75 struct pci_dev *pdev = NULL;
76 struct acpi_dmar_pci_path *path;
77 int count;
79 bus = pci_find_bus(segment, scope->bus);
80 path = (struct acpi_dmar_pci_path *)(scope + 1);
81 count = (scope->length - sizeof(struct acpi_dmar_device_scope))
82 / sizeof(struct acpi_dmar_pci_path);
84 while (count) {
85 if (pdev)
86 pci_dev_put(pdev);
88 * Some BIOSes list non-exist devices in DMAR table, just
89 * ignore it
91 if (!bus) {
92 printk(KERN_WARNING
93 PREFIX "Device scope bus [%d] not found\n",
94 scope->bus);
95 break;
97 pdev = pci_get_slot(bus, PCI_DEVFN(path->dev, path->fn));
98 if (!pdev) {
99 printk(KERN_WARNING PREFIX
100 "Device scope device [%04x:%02x:%02x.%02x] not found\n",
101 segment, bus->number, path->dev, path->fn);
102 break;
104 path ++;
105 count --;
106 bus = pdev->subordinate;
108 if (!pdev) {
109 printk(KERN_WARNING PREFIX
110 "Device scope device [%04x:%02x:%02x.%02x] not found\n",
111 segment, scope->bus, path->dev, path->fn);
112 *dev = NULL;
113 return 0;
115 if ((scope->entry_type == ACPI_DMAR_SCOPE_TYPE_ENDPOINT && \
116 pdev->subordinate) || (scope->entry_type == \
117 ACPI_DMAR_SCOPE_TYPE_BRIDGE && !pdev->subordinate)) {
118 pci_dev_put(pdev);
119 printk(KERN_WARNING PREFIX
120 "Device scope type does not match for %s\n",
121 pci_name(pdev));
122 return -EINVAL;
124 *dev = pdev;
125 return 0;
128 static int __init dmar_parse_dev_scope(void *start, void *end, int *cnt,
129 struct pci_dev ***devices, u16 segment)
131 struct acpi_dmar_device_scope *scope;
132 void * tmp = start;
133 int index;
134 int ret;
136 *cnt = 0;
137 while (start < end) {
138 scope = start;
139 if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_ENDPOINT ||
140 scope->entry_type == ACPI_DMAR_SCOPE_TYPE_BRIDGE)
141 (*cnt)++;
142 else
143 printk(KERN_WARNING PREFIX
144 "Unsupported device scope\n");
145 start += scope->length;
147 if (*cnt == 0)
148 return 0;
150 *devices = kcalloc(*cnt, sizeof(struct pci_dev *), GFP_KERNEL);
151 if (!*devices)
152 return -ENOMEM;
154 start = tmp;
155 index = 0;
156 while (start < end) {
157 scope = start;
158 if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_ENDPOINT ||
159 scope->entry_type == ACPI_DMAR_SCOPE_TYPE_BRIDGE) {
160 ret = dmar_parse_one_dev_scope(scope,
161 &(*devices)[index], segment);
162 if (ret) {
163 kfree(*devices);
164 return ret;
166 index ++;
168 start += scope->length;
171 return 0;
175 * dmar_parse_one_drhd - parses exactly one DMA remapping hardware definition
176 * structure which uniquely represent one DMA remapping hardware unit
177 * present in the platform
179 static int __init
180 dmar_parse_one_drhd(struct acpi_dmar_header *header)
182 struct acpi_dmar_hardware_unit *drhd;
183 struct dmar_drhd_unit *dmaru;
184 int ret = 0;
185 static int include_all;
187 dmaru = kzalloc(sizeof(*dmaru), GFP_KERNEL);
188 if (!dmaru)
189 return -ENOMEM;
191 drhd = (struct acpi_dmar_hardware_unit *)header;
192 dmaru->reg_base_addr = drhd->address;
193 dmaru->include_all = drhd->flags & 0x1; /* BIT0: INCLUDE_ALL */
195 if (!dmaru->include_all)
196 ret = dmar_parse_dev_scope((void *)(drhd + 1),
197 ((void *)drhd) + header->length,
198 &dmaru->devices_cnt, &dmaru->devices,
199 drhd->segment);
200 else {
201 /* Only allow one INCLUDE_ALL */
202 if (include_all) {
203 printk(KERN_WARNING PREFIX "Only one INCLUDE_ALL "
204 "device scope is allowed\n");
205 ret = -EINVAL;
207 include_all = 1;
210 if (ret || (dmaru->devices_cnt == 0 && !dmaru->include_all))
211 kfree(dmaru);
212 else
213 dmar_register_drhd_unit(dmaru);
214 return ret;
217 static int __init
218 dmar_parse_one_rmrr(struct acpi_dmar_header *header)
220 struct acpi_dmar_reserved_memory *rmrr;
221 struct dmar_rmrr_unit *rmrru;
222 int ret = 0;
224 rmrru = kzalloc(sizeof(*rmrru), GFP_KERNEL);
225 if (!rmrru)
226 return -ENOMEM;
228 rmrr = (struct acpi_dmar_reserved_memory *)header;
229 rmrru->base_address = rmrr->base_address;
230 rmrru->end_address = rmrr->end_address;
231 ret = dmar_parse_dev_scope((void *)(rmrr + 1),
232 ((void *)rmrr) + header->length,
233 &rmrru->devices_cnt, &rmrru->devices, rmrr->segment);
235 if (ret || (rmrru->devices_cnt == 0))
236 kfree(rmrru);
237 else
238 dmar_register_rmrr_unit(rmrru);
239 return ret;
242 static void __init
243 dmar_table_print_dmar_entry(struct acpi_dmar_header *header)
245 struct acpi_dmar_hardware_unit *drhd;
246 struct acpi_dmar_reserved_memory *rmrr;
248 switch (header->type) {
249 case ACPI_DMAR_TYPE_HARDWARE_UNIT:
250 drhd = (struct acpi_dmar_hardware_unit *)header;
251 printk (KERN_INFO PREFIX
252 "DRHD (flags: 0x%08x)base: 0x%016Lx\n",
253 drhd->flags, drhd->address);
254 break;
255 case ACPI_DMAR_TYPE_RESERVED_MEMORY:
256 rmrr = (struct acpi_dmar_reserved_memory *)header;
258 printk (KERN_INFO PREFIX
259 "RMRR base: 0x%016Lx end: 0x%016Lx\n",
260 rmrr->base_address, rmrr->end_address);
261 break;
266 * parse_dmar_table - parses the DMA reporting table
268 static int __init
269 parse_dmar_table(void)
271 struct acpi_table_dmar *dmar;
272 struct acpi_dmar_header *entry_header;
273 int ret = 0;
275 dmar = (struct acpi_table_dmar *)dmar_tbl;
276 if (!dmar)
277 return -ENODEV;
279 if (dmar->width < PAGE_SHIFT_4K - 1) {
280 printk(KERN_WARNING PREFIX "Invalid DMAR haw\n");
281 return -EINVAL;
284 printk (KERN_INFO PREFIX "Host address width %d\n",
285 dmar->width + 1);
287 entry_header = (struct acpi_dmar_header *)(dmar + 1);
288 while (((unsigned long)entry_header) <
289 (((unsigned long)dmar) + dmar_tbl->length)) {
290 dmar_table_print_dmar_entry(entry_header);
292 switch (entry_header->type) {
293 case ACPI_DMAR_TYPE_HARDWARE_UNIT:
294 ret = dmar_parse_one_drhd(entry_header);
295 break;
296 case ACPI_DMAR_TYPE_RESERVED_MEMORY:
297 ret = dmar_parse_one_rmrr(entry_header);
298 break;
299 default:
300 printk(KERN_WARNING PREFIX
301 "Unknown DMAR structure type\n");
302 ret = 0; /* for forward compatibility */
303 break;
305 if (ret)
306 break;
308 entry_header = ((void *)entry_header + entry_header->length);
310 return ret;
314 int __init dmar_table_init(void)
317 int ret;
319 ret = parse_dmar_table();
320 if (ret) {
321 printk(KERN_INFO PREFIX "parse DMAR table failure.\n");
322 return ret;
325 if (list_empty(&dmar_drhd_units)) {
326 printk(KERN_INFO PREFIX "No DMAR devices found\n");
327 return -ENODEV;
330 if (list_empty(&dmar_rmrr_units)) {
331 printk(KERN_INFO PREFIX "No RMRR found\n");
332 return -ENODEV;
335 return 0;
339 * early_dmar_detect - checks to see if the platform supports DMAR devices
341 int __init early_dmar_detect(void)
343 acpi_status status = AE_OK;
345 /* if we could find DMAR table, then there are DMAR devices */
346 status = acpi_get_table(ACPI_SIG_DMAR, 0,
347 (struct acpi_table_header **)&dmar_tbl);
349 if (ACPI_SUCCESS(status) && !dmar_tbl) {
350 printk (KERN_WARNING PREFIX "Unable to map DMAR\n");
351 status = AE_NOT_FOUND;
354 return (ACPI_SUCCESS(status) ? 1 : 0);