blob: 586dd2aa2ca235d269e96d5c97e7911eba196b88 [file] [log] [blame]
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -07001/*
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 *
mark gross98bcef52008-02-23 15:23:35 -080017 * Copyright (C) 2006-2008 Intel Corporation
18 * Author: Ashok Raj <ashok.raj@intel.com>
19 * Author: Shaohua Li <shaohua.li@intel.com>
20 * Author: Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -070021 *
Suresh Siddhae61d98d2008-07-10 11:16:35 -070022 * This file implements early detection/parsing of Remapping Devices
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -070023 * reported to OS through BIOS via DMA remapping reporting (DMAR) ACPI
24 * tables.
Suresh Siddhae61d98d2008-07-10 11:16:35 -070025 *
26 * These routines are used by both DMA-remapping and Interrupt-remapping
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -070027 */
28
Donald Dutilee9071b02012-06-08 17:13:11 -040029#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt /* has to precede printk.h */
30
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -070031#include <linux/pci.h>
32#include <linux/dmar.h>
Kay, Allen M38717942008-09-09 18:37:29 +030033#include <linux/iova.h>
34#include <linux/intel-iommu.h>
Suresh Siddhafe962e92008-07-10 11:16:42 -070035#include <linux/timer.h>
Suresh Siddha0ac24912009-03-16 17:04:54 -070036#include <linux/irq.h>
37#include <linux/interrupt.h>
Shane Wang69575d32009-09-01 18:25:07 -070038#include <linux/tboot.h>
Len Browneb27cae2009-07-06 23:40:19 -040039#include <linux/dmi.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090040#include <linux/slab.h>
Alex Williamsona5459cf2014-06-12 16:12:31 -060041#include <linux/iommu.h>
Suresh Siddha8a8f4222012-03-30 11:47:08 -070042#include <asm/irq_remapping.h>
Konrad Rzeszutek Wilk4db77ff2010-08-26 13:58:04 -040043#include <asm/iommu_table.h>
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -070044
Joerg Roedel078e1ee2012-09-26 12:44:43 +020045#include "irq_remapping.h"
46
Jiang Liuc2a0b532014-11-09 22:47:56 +080047typedef int (*dmar_res_handler_t)(struct acpi_dmar_header *, void *);
48struct dmar_res_callback {
49 dmar_res_handler_t cb[ACPI_DMAR_TYPE_RESERVED];
50 void *arg[ACPI_DMAR_TYPE_RESERVED];
51 bool ignore_unhandled;
52 bool print_entry;
53};
54
Jiang Liu3a5670e2014-02-19 14:07:33 +080055/*
56 * Assumptions:
57 * 1) The hotplug framework guarentees that DMAR unit will be hot-added
58 * before IO devices managed by that unit.
59 * 2) The hotplug framework guarantees that DMAR unit will be hot-removed
60 * after IO devices managed by that unit.
61 * 3) Hotplug events are rare.
62 *
63 * Locking rules for DMA and interrupt remapping related global data structures:
64 * 1) Use dmar_global_lock in process context
65 * 2) Use RCU in interrupt context
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -070066 */
Jiang Liu3a5670e2014-02-19 14:07:33 +080067DECLARE_RWSEM(dmar_global_lock);
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -070068LIST_HEAD(dmar_drhd_units);
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -070069
Suresh Siddha41750d32011-08-23 17:05:18 -070070struct acpi_table_header * __initdata dmar_tbl;
Yinghai Lu8e1568f2009-02-11 01:06:59 -080071static acpi_size dmar_tbl_size;
Jiang Liu2e455282014-02-19 14:07:36 +080072static int dmar_dev_scope_status = 1;
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -070073
Jiang Liu694835d2014-01-06 14:18:16 +080074static int alloc_iommu(struct dmar_drhd_unit *drhd);
Jiang Liua868e6b2014-01-06 14:18:20 +080075static void free_iommu(struct intel_iommu *iommu);
Jiang Liu694835d2014-01-06 14:18:16 +080076
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -070077static void __init dmar_register_drhd_unit(struct dmar_drhd_unit *drhd)
78{
79 /*
80 * add INCLUDE_ALL at the tail, so scan the list will find it at
81 * the very end.
82 */
83 if (drhd->include_all)
Jiang Liu0e242612014-02-19 14:07:34 +080084 list_add_tail_rcu(&drhd->list, &dmar_drhd_units);
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -070085 else
Jiang Liu0e242612014-02-19 14:07:34 +080086 list_add_rcu(&drhd->list, &dmar_drhd_units);
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -070087}
88
Jiang Liubb3a6b72014-02-19 14:07:24 +080089void *dmar_alloc_dev_scope(void *start, void *end, int *cnt)
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -070090{
91 struct acpi_dmar_device_scope *scope;
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -070092
93 *cnt = 0;
94 while (start < end) {
95 scope = start;
Bob Moore83118b02014-07-30 12:21:00 +080096 if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_NAMESPACE ||
David Woodhouse07cb52f2014-03-07 14:39:27 +000097 scope->entry_type == ACPI_DMAR_SCOPE_TYPE_ENDPOINT ||
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -070098 scope->entry_type == ACPI_DMAR_SCOPE_TYPE_BRIDGE)
99 (*cnt)++;
Linn Crosettoae3e7f32013-04-23 12:26:45 -0600100 else if (scope->entry_type != ACPI_DMAR_SCOPE_TYPE_IOAPIC &&
101 scope->entry_type != ACPI_DMAR_SCOPE_TYPE_HPET) {
Donald Dutilee9071b02012-06-08 17:13:11 -0400102 pr_warn("Unsupported device scope\n");
Yinghai Lu5715f0f2010-04-08 19:58:22 +0100103 }
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -0700104 start += scope->length;
105 }
106 if (*cnt == 0)
Jiang Liubb3a6b72014-02-19 14:07:24 +0800107 return NULL;
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -0700108
David Woodhouse832bd852014-03-07 15:08:36 +0000109 return kcalloc(*cnt, sizeof(struct dmar_dev_scope), GFP_KERNEL);
Jiang Liubb3a6b72014-02-19 14:07:24 +0800110}
111
David Woodhouse832bd852014-03-07 15:08:36 +0000112void dmar_free_dev_scope(struct dmar_dev_scope **devices, int *cnt)
Jiang Liuada4d4b2014-01-06 14:18:09 +0800113{
Jiang Liub683b232014-02-19 14:07:32 +0800114 int i;
David Woodhouse832bd852014-03-07 15:08:36 +0000115 struct device *tmp_dev;
Jiang Liub683b232014-02-19 14:07:32 +0800116
Jiang Liuada4d4b2014-01-06 14:18:09 +0800117 if (*devices && *cnt) {
Jiang Liub683b232014-02-19 14:07:32 +0800118 for_each_active_dev_scope(*devices, *cnt, i, tmp_dev)
David Woodhouse832bd852014-03-07 15:08:36 +0000119 put_device(tmp_dev);
Jiang Liuada4d4b2014-01-06 14:18:09 +0800120 kfree(*devices);
Jiang Liuada4d4b2014-01-06 14:18:09 +0800121 }
Jiang Liu0e242612014-02-19 14:07:34 +0800122
123 *devices = NULL;
124 *cnt = 0;
Jiang Liuada4d4b2014-01-06 14:18:09 +0800125}
126
Jiang Liu59ce0512014-02-19 14:07:35 +0800127/* Optimize out kzalloc()/kfree() for normal cases */
128static char dmar_pci_notify_info_buf[64];
129
130static struct dmar_pci_notify_info *
131dmar_alloc_pci_notify_info(struct pci_dev *dev, unsigned long event)
132{
133 int level = 0;
134 size_t size;
135 struct pci_dev *tmp;
136 struct dmar_pci_notify_info *info;
137
138 BUG_ON(dev->is_virtfn);
139
140 /* Only generate path[] for device addition event */
141 if (event == BUS_NOTIFY_ADD_DEVICE)
142 for (tmp = dev; tmp; tmp = tmp->bus->self)
143 level++;
144
145 size = sizeof(*info) + level * sizeof(struct acpi_dmar_pci_path);
146 if (size <= sizeof(dmar_pci_notify_info_buf)) {
147 info = (struct dmar_pci_notify_info *)dmar_pci_notify_info_buf;
148 } else {
149 info = kzalloc(size, GFP_KERNEL);
150 if (!info) {
151 pr_warn("Out of memory when allocating notify_info "
152 "for %s.\n", pci_name(dev));
Jiang Liu2e455282014-02-19 14:07:36 +0800153 if (dmar_dev_scope_status == 0)
154 dmar_dev_scope_status = -ENOMEM;
Jiang Liu59ce0512014-02-19 14:07:35 +0800155 return NULL;
156 }
157 }
158
159 info->event = event;
160 info->dev = dev;
161 info->seg = pci_domain_nr(dev->bus);
162 info->level = level;
163 if (event == BUS_NOTIFY_ADD_DEVICE) {
Jiang Liu5ae05662014-04-15 10:35:35 +0800164 for (tmp = dev; tmp; tmp = tmp->bus->self) {
165 level--;
Joerg Roedel57384592014-10-02 11:50:25 +0200166 info->path[level].bus = tmp->bus->number;
Jiang Liu59ce0512014-02-19 14:07:35 +0800167 info->path[level].device = PCI_SLOT(tmp->devfn);
168 info->path[level].function = PCI_FUNC(tmp->devfn);
169 if (pci_is_root_bus(tmp->bus))
170 info->bus = tmp->bus->number;
171 }
172 }
173
174 return info;
175}
176
177static inline void dmar_free_pci_notify_info(struct dmar_pci_notify_info *info)
178{
179 if ((void *)info != dmar_pci_notify_info_buf)
180 kfree(info);
181}
182
183static bool dmar_match_pci_path(struct dmar_pci_notify_info *info, int bus,
184 struct acpi_dmar_pci_path *path, int count)
185{
186 int i;
187
188 if (info->bus != bus)
Joerg Roedel80f7b3d2014-09-22 16:30:22 +0200189 goto fallback;
Jiang Liu59ce0512014-02-19 14:07:35 +0800190 if (info->level != count)
Joerg Roedel80f7b3d2014-09-22 16:30:22 +0200191 goto fallback;
Jiang Liu59ce0512014-02-19 14:07:35 +0800192
193 for (i = 0; i < count; i++) {
194 if (path[i].device != info->path[i].device ||
195 path[i].function != info->path[i].function)
Joerg Roedel80f7b3d2014-09-22 16:30:22 +0200196 goto fallback;
Jiang Liu59ce0512014-02-19 14:07:35 +0800197 }
198
199 return true;
Joerg Roedel80f7b3d2014-09-22 16:30:22 +0200200
201fallback:
202
203 if (count != 1)
204 return false;
205
206 i = info->level - 1;
207 if (bus == info->path[i].bus &&
208 path[0].device == info->path[i].device &&
209 path[0].function == info->path[i].function) {
210 pr_info(FW_BUG "RMRR entry for device %02x:%02x.%x is broken - applying workaround\n",
211 bus, path[0].device, path[0].function);
212 return true;
213 }
214
215 return false;
Jiang Liu59ce0512014-02-19 14:07:35 +0800216}
217
218/* Return: > 0 if match found, 0 if no match found, < 0 if error happens */
219int dmar_insert_dev_scope(struct dmar_pci_notify_info *info,
220 void *start, void*end, u16 segment,
David Woodhouse832bd852014-03-07 15:08:36 +0000221 struct dmar_dev_scope *devices,
222 int devices_cnt)
Jiang Liu59ce0512014-02-19 14:07:35 +0800223{
224 int i, level;
David Woodhouse832bd852014-03-07 15:08:36 +0000225 struct device *tmp, *dev = &info->dev->dev;
Jiang Liu59ce0512014-02-19 14:07:35 +0800226 struct acpi_dmar_device_scope *scope;
227 struct acpi_dmar_pci_path *path;
228
229 if (segment != info->seg)
230 return 0;
231
232 for (; start < end; start += scope->length) {
233 scope = start;
234 if (scope->entry_type != ACPI_DMAR_SCOPE_TYPE_ENDPOINT &&
235 scope->entry_type != ACPI_DMAR_SCOPE_TYPE_BRIDGE)
236 continue;
237
238 path = (struct acpi_dmar_pci_path *)(scope + 1);
239 level = (scope->length - sizeof(*scope)) / sizeof(*path);
240 if (!dmar_match_pci_path(info, scope->bus, path, level))
241 continue;
242
243 if ((scope->entry_type == ACPI_DMAR_SCOPE_TYPE_ENDPOINT) ^
David Woodhouse832bd852014-03-07 15:08:36 +0000244 (info->dev->hdr_type == PCI_HEADER_TYPE_NORMAL)) {
Jiang Liu59ce0512014-02-19 14:07:35 +0800245 pr_warn("Device scope type does not match for %s\n",
David Woodhouse832bd852014-03-07 15:08:36 +0000246 pci_name(info->dev));
Jiang Liu59ce0512014-02-19 14:07:35 +0800247 return -EINVAL;
248 }
249
250 for_each_dev_scope(devices, devices_cnt, i, tmp)
251 if (tmp == NULL) {
David Woodhouse832bd852014-03-07 15:08:36 +0000252 devices[i].bus = info->dev->bus->number;
253 devices[i].devfn = info->dev->devfn;
254 rcu_assign_pointer(devices[i].dev,
255 get_device(dev));
Jiang Liu59ce0512014-02-19 14:07:35 +0800256 return 1;
257 }
258 BUG_ON(i >= devices_cnt);
259 }
260
261 return 0;
262}
263
264int dmar_remove_dev_scope(struct dmar_pci_notify_info *info, u16 segment,
David Woodhouse832bd852014-03-07 15:08:36 +0000265 struct dmar_dev_scope *devices, int count)
Jiang Liu59ce0512014-02-19 14:07:35 +0800266{
267 int index;
David Woodhouse832bd852014-03-07 15:08:36 +0000268 struct device *tmp;
Jiang Liu59ce0512014-02-19 14:07:35 +0800269
270 if (info->seg != segment)
271 return 0;
272
273 for_each_active_dev_scope(devices, count, index, tmp)
David Woodhouse832bd852014-03-07 15:08:36 +0000274 if (tmp == &info->dev->dev) {
Andreea-Cristina Bernateecbad72014-08-18 15:20:56 +0300275 RCU_INIT_POINTER(devices[index].dev, NULL);
Jiang Liu59ce0512014-02-19 14:07:35 +0800276 synchronize_rcu();
David Woodhouse832bd852014-03-07 15:08:36 +0000277 put_device(tmp);
Jiang Liu59ce0512014-02-19 14:07:35 +0800278 return 1;
279 }
280
281 return 0;
282}
283
284static int dmar_pci_bus_add_dev(struct dmar_pci_notify_info *info)
285{
286 int ret = 0;
287 struct dmar_drhd_unit *dmaru;
288 struct acpi_dmar_hardware_unit *drhd;
289
290 for_each_drhd_unit(dmaru) {
291 if (dmaru->include_all)
292 continue;
293
294 drhd = container_of(dmaru->hdr,
295 struct acpi_dmar_hardware_unit, header);
296 ret = dmar_insert_dev_scope(info, (void *)(drhd + 1),
297 ((void *)drhd) + drhd->header.length,
298 dmaru->segment,
299 dmaru->devices, dmaru->devices_cnt);
300 if (ret != 0)
301 break;
302 }
303 if (ret >= 0)
304 ret = dmar_iommu_notify_scope_dev(info);
Jiang Liu2e455282014-02-19 14:07:36 +0800305 if (ret < 0 && dmar_dev_scope_status == 0)
306 dmar_dev_scope_status = ret;
Jiang Liu59ce0512014-02-19 14:07:35 +0800307
308 return ret;
309}
310
311static void dmar_pci_bus_del_dev(struct dmar_pci_notify_info *info)
312{
313 struct dmar_drhd_unit *dmaru;
314
315 for_each_drhd_unit(dmaru)
316 if (dmar_remove_dev_scope(info, dmaru->segment,
317 dmaru->devices, dmaru->devices_cnt))
318 break;
319 dmar_iommu_notify_scope_dev(info);
320}
321
322static int dmar_pci_bus_notifier(struct notifier_block *nb,
323 unsigned long action, void *data)
324{
325 struct pci_dev *pdev = to_pci_dev(data);
326 struct dmar_pci_notify_info *info;
327
328 /* Only care about add/remove events for physical functions */
329 if (pdev->is_virtfn)
330 return NOTIFY_DONE;
331 if (action != BUS_NOTIFY_ADD_DEVICE && action != BUS_NOTIFY_DEL_DEVICE)
332 return NOTIFY_DONE;
333
334 info = dmar_alloc_pci_notify_info(pdev, action);
335 if (!info)
336 return NOTIFY_DONE;
337
338 down_write(&dmar_global_lock);
339 if (action == BUS_NOTIFY_ADD_DEVICE)
340 dmar_pci_bus_add_dev(info);
341 else if (action == BUS_NOTIFY_DEL_DEVICE)
342 dmar_pci_bus_del_dev(info);
343 up_write(&dmar_global_lock);
344
345 dmar_free_pci_notify_info(info);
346
347 return NOTIFY_OK;
348}
349
350static struct notifier_block dmar_pci_bus_nb = {
351 .notifier_call = dmar_pci_bus_notifier,
352 .priority = INT_MIN,
353};
354
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -0700355/**
356 * dmar_parse_one_drhd - parses exactly one DMA remapping hardware definition
357 * structure which uniquely represent one DMA remapping hardware unit
358 * present in the platform
359 */
360static int __init
Jiang Liuc2a0b532014-11-09 22:47:56 +0800361dmar_parse_one_drhd(struct acpi_dmar_header *header, void *arg)
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -0700362{
363 struct acpi_dmar_hardware_unit *drhd;
364 struct dmar_drhd_unit *dmaru;
365 int ret = 0;
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -0700366
David Woodhousee523b382009-04-10 22:27:48 -0700367 drhd = (struct acpi_dmar_hardware_unit *)header;
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -0700368 dmaru = kzalloc(sizeof(*dmaru), GFP_KERNEL);
369 if (!dmaru)
370 return -ENOMEM;
371
Suresh Siddha1886e8a2008-07-10 11:16:37 -0700372 dmaru->hdr = header;
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -0700373 dmaru->reg_base_addr = drhd->address;
David Woodhouse276dbf92009-04-04 01:45:37 +0100374 dmaru->segment = drhd->segment;
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -0700375 dmaru->include_all = drhd->flags & 0x1; /* BIT0: INCLUDE_ALL */
David Woodhouse07cb52f2014-03-07 14:39:27 +0000376 dmaru->devices = dmar_alloc_dev_scope((void *)(drhd + 1),
377 ((void *)drhd) + drhd->header.length,
378 &dmaru->devices_cnt);
379 if (dmaru->devices_cnt && dmaru->devices == NULL) {
380 kfree(dmaru);
381 return -ENOMEM;
Jiang Liu2e455282014-02-19 14:07:36 +0800382 }
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -0700383
Suresh Siddha1886e8a2008-07-10 11:16:37 -0700384 ret = alloc_iommu(dmaru);
385 if (ret) {
David Woodhouse07cb52f2014-03-07 14:39:27 +0000386 dmar_free_dev_scope(&dmaru->devices,
387 &dmaru->devices_cnt);
Suresh Siddha1886e8a2008-07-10 11:16:37 -0700388 kfree(dmaru);
389 return ret;
390 }
391 dmar_register_drhd_unit(dmaru);
Jiang Liuc2a0b532014-11-09 22:47:56 +0800392
393 if (arg)
394 (*(int *)arg)++;
395
Suresh Siddha1886e8a2008-07-10 11:16:37 -0700396 return 0;
397}
398
Jiang Liua868e6b2014-01-06 14:18:20 +0800399static void dmar_free_drhd(struct dmar_drhd_unit *dmaru)
400{
401 if (dmaru->devices && dmaru->devices_cnt)
402 dmar_free_dev_scope(&dmaru->devices, &dmaru->devices_cnt);
403 if (dmaru->iommu)
404 free_iommu(dmaru->iommu);
405 kfree(dmaru);
406}
407
Jiang Liuc2a0b532014-11-09 22:47:56 +0800408static int __init dmar_parse_one_andd(struct acpi_dmar_header *header,
409 void *arg)
David Woodhousee625b4a2014-03-07 14:34:38 +0000410{
411 struct acpi_dmar_andd *andd = (void *)header;
412
413 /* Check for NUL termination within the designated length */
Bob Moore83118b02014-07-30 12:21:00 +0800414 if (strnlen(andd->device_name, header->length - 8) == header->length - 8) {
David Woodhousee625b4a2014-03-07 14:34:38 +0000415 WARN_TAINT(1, TAINT_FIRMWARE_WORKAROUND,
416 "Your BIOS is broken; ANDD object name is not NUL-terminated\n"
417 "BIOS vendor: %s; Ver: %s; Product Version: %s\n",
418 dmi_get_system_info(DMI_BIOS_VENDOR),
419 dmi_get_system_info(DMI_BIOS_VERSION),
420 dmi_get_system_info(DMI_PRODUCT_VERSION));
421 return -EINVAL;
422 }
423 pr_info("ANDD device: %x name: %s\n", andd->device_number,
Bob Moore83118b02014-07-30 12:21:00 +0800424 andd->device_name);
David Woodhousee625b4a2014-03-07 14:34:38 +0000425
426 return 0;
427}
428
David Woodhouseaa697072009-10-07 12:18:00 +0100429#ifdef CONFIG_ACPI_NUMA
Suresh Siddhaee34b322009-10-02 11:01:21 -0700430static int __init
Jiang Liuc2a0b532014-11-09 22:47:56 +0800431dmar_parse_one_rhsa(struct acpi_dmar_header *header, void *arg)
Suresh Siddhaee34b322009-10-02 11:01:21 -0700432{
433 struct acpi_dmar_rhsa *rhsa;
434 struct dmar_drhd_unit *drhd;
435
436 rhsa = (struct acpi_dmar_rhsa *)header;
David Woodhouseaa697072009-10-07 12:18:00 +0100437 for_each_drhd_unit(drhd) {
Suresh Siddhaee34b322009-10-02 11:01:21 -0700438 if (drhd->reg_base_addr == rhsa->base_address) {
439 int node = acpi_map_pxm_to_node(rhsa->proximity_domain);
440
441 if (!node_online(node))
442 node = -1;
443 drhd->iommu->node = node;
David Woodhouseaa697072009-10-07 12:18:00 +0100444 return 0;
445 }
Suresh Siddhaee34b322009-10-02 11:01:21 -0700446 }
Ben Hutchingsfd0c8892010-04-03 19:38:43 +0100447 WARN_TAINT(
448 1, TAINT_FIRMWARE_WORKAROUND,
449 "Your BIOS is broken; RHSA refers to non-existent DMAR unit at %llx\n"
450 "BIOS vendor: %s; Ver: %s; Product Version: %s\n",
451 drhd->reg_base_addr,
452 dmi_get_system_info(DMI_BIOS_VENDOR),
453 dmi_get_system_info(DMI_BIOS_VERSION),
454 dmi_get_system_info(DMI_PRODUCT_VERSION));
Suresh Siddhaee34b322009-10-02 11:01:21 -0700455
David Woodhouseaa697072009-10-07 12:18:00 +0100456 return 0;
Suresh Siddhaee34b322009-10-02 11:01:21 -0700457}
Jiang Liuc2a0b532014-11-09 22:47:56 +0800458#else
459#define dmar_parse_one_rhsa dmar_res_noop
David Woodhouseaa697072009-10-07 12:18:00 +0100460#endif
Suresh Siddhaee34b322009-10-02 11:01:21 -0700461
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -0700462static void __init
463dmar_table_print_dmar_entry(struct acpi_dmar_header *header)
464{
465 struct acpi_dmar_hardware_unit *drhd;
466 struct acpi_dmar_reserved_memory *rmrr;
Yu Zhaoaa5d2b52009-05-18 13:51:34 +0800467 struct acpi_dmar_atsr *atsr;
Roland Dreier17b60972009-09-24 12:14:00 -0700468 struct acpi_dmar_rhsa *rhsa;
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -0700469
470 switch (header->type) {
471 case ACPI_DMAR_TYPE_HARDWARE_UNIT:
Yu Zhaoaa5d2b52009-05-18 13:51:34 +0800472 drhd = container_of(header, struct acpi_dmar_hardware_unit,
473 header);
Donald Dutilee9071b02012-06-08 17:13:11 -0400474 pr_info("DRHD base: %#016Lx flags: %#x\n",
Yu Zhaoaa5d2b52009-05-18 13:51:34 +0800475 (unsigned long long)drhd->address, drhd->flags);
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -0700476 break;
477 case ACPI_DMAR_TYPE_RESERVED_MEMORY:
Yu Zhaoaa5d2b52009-05-18 13:51:34 +0800478 rmrr = container_of(header, struct acpi_dmar_reserved_memory,
479 header);
Donald Dutilee9071b02012-06-08 17:13:11 -0400480 pr_info("RMRR base: %#016Lx end: %#016Lx\n",
Fenghua Yu5b6985c2008-10-16 18:02:32 -0700481 (unsigned long long)rmrr->base_address,
482 (unsigned long long)rmrr->end_address);
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -0700483 break;
Bob Moore83118b02014-07-30 12:21:00 +0800484 case ACPI_DMAR_TYPE_ROOT_ATS:
Yu Zhaoaa5d2b52009-05-18 13:51:34 +0800485 atsr = container_of(header, struct acpi_dmar_atsr, header);
Donald Dutilee9071b02012-06-08 17:13:11 -0400486 pr_info("ATSR flags: %#x\n", atsr->flags);
Yu Zhaoaa5d2b52009-05-18 13:51:34 +0800487 break;
Bob Moore83118b02014-07-30 12:21:00 +0800488 case ACPI_DMAR_TYPE_HARDWARE_AFFINITY:
Roland Dreier17b60972009-09-24 12:14:00 -0700489 rhsa = container_of(header, struct acpi_dmar_rhsa, header);
Donald Dutilee9071b02012-06-08 17:13:11 -0400490 pr_info("RHSA base: %#016Lx proximity domain: %#x\n",
Roland Dreier17b60972009-09-24 12:14:00 -0700491 (unsigned long long)rhsa->base_address,
492 rhsa->proximity_domain);
493 break;
Bob Moore83118b02014-07-30 12:21:00 +0800494 case ACPI_DMAR_TYPE_NAMESPACE:
David Woodhousee625b4a2014-03-07 14:34:38 +0000495 /* We don't print this here because we need to sanity-check
496 it first. So print it in dmar_parse_one_andd() instead. */
497 break;
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -0700498 }
499}
500
Yinghai Luf6dd5c32008-09-03 16:58:32 -0700501/**
502 * dmar_table_detect - checks to see if the platform supports DMAR devices
503 */
504static int __init dmar_table_detect(void)
505{
506 acpi_status status = AE_OK;
507
508 /* if we could find DMAR table, then there are DMAR devices */
Yinghai Lu8e1568f2009-02-11 01:06:59 -0800509 status = acpi_get_table_with_size(ACPI_SIG_DMAR, 0,
510 (struct acpi_table_header **)&dmar_tbl,
511 &dmar_tbl_size);
Yinghai Luf6dd5c32008-09-03 16:58:32 -0700512
513 if (ACPI_SUCCESS(status) && !dmar_tbl) {
Donald Dutilee9071b02012-06-08 17:13:11 -0400514 pr_warn("Unable to map DMAR\n");
Yinghai Luf6dd5c32008-09-03 16:58:32 -0700515 status = AE_NOT_FOUND;
516 }
517
518 return (ACPI_SUCCESS(status) ? 1 : 0);
519}
Suresh Siddhaaaa9d1d2008-07-10 11:16:38 -0700520
Jiang Liuc2a0b532014-11-09 22:47:56 +0800521static int dmar_walk_remapping_entries(struct acpi_dmar_header *start,
522 size_t len, struct dmar_res_callback *cb)
523{
524 int ret = 0;
525 struct acpi_dmar_header *iter, *next;
526 struct acpi_dmar_header *end = ((void *)start) + len;
527
528 for (iter = start; iter < end && ret == 0; iter = next) {
529 next = (void *)iter + iter->length;
530 if (iter->length == 0) {
531 /* Avoid looping forever on bad ACPI tables */
532 pr_debug(FW_BUG "Invalid 0-length structure\n");
533 break;
534 } else if (next > end) {
535 /* Avoid passing table end */
536 pr_warn(FW_BUG "record passes table end\n");
537 ret = -EINVAL;
538 break;
539 }
540
541 if (cb->print_entry)
542 dmar_table_print_dmar_entry(iter);
543
544 if (iter->type >= ACPI_DMAR_TYPE_RESERVED) {
545 /* continue for forward compatibility */
546 pr_debug("Unknown DMAR structure type %d\n",
547 iter->type);
548 } else if (cb->cb[iter->type]) {
549 ret = cb->cb[iter->type](iter, cb->arg[iter->type]);
550 } else if (!cb->ignore_unhandled) {
551 pr_warn("No handler for DMAR structure type %d\n",
552 iter->type);
553 ret = -EINVAL;
554 }
555 }
556
557 return ret;
558}
559
560static inline int dmar_walk_dmar_table(struct acpi_table_dmar *dmar,
561 struct dmar_res_callback *cb)
562{
563 return dmar_walk_remapping_entries((void *)(dmar + 1),
564 dmar->header.length - sizeof(*dmar), cb);
565}
566
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -0700567/**
568 * parse_dmar_table - parses the DMA reporting table
569 */
570static int __init
571parse_dmar_table(void)
572{
573 struct acpi_table_dmar *dmar;
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -0700574 int ret = 0;
Li, Zhen-Hua7cef3342013-05-20 15:57:32 +0800575 int drhd_count = 0;
Jiang Liuc2a0b532014-11-09 22:47:56 +0800576 struct dmar_res_callback cb = {
577 .print_entry = true,
578 .ignore_unhandled = true,
579 .arg[ACPI_DMAR_TYPE_HARDWARE_UNIT] = &drhd_count,
580 .cb[ACPI_DMAR_TYPE_HARDWARE_UNIT] = &dmar_parse_one_drhd,
581 .cb[ACPI_DMAR_TYPE_RESERVED_MEMORY] = &dmar_parse_one_rmrr,
582 .cb[ACPI_DMAR_TYPE_ROOT_ATS] = &dmar_parse_one_atsr,
583 .cb[ACPI_DMAR_TYPE_HARDWARE_AFFINITY] = &dmar_parse_one_rhsa,
584 .cb[ACPI_DMAR_TYPE_NAMESPACE] = &dmar_parse_one_andd,
585 };
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -0700586
Yinghai Luf6dd5c32008-09-03 16:58:32 -0700587 /*
588 * Do it again, earlier dmar_tbl mapping could be mapped with
589 * fixed map.
590 */
591 dmar_table_detect();
592
Joseph Cihulaa59b50e2009-06-30 19:31:10 -0700593 /*
594 * ACPI tables may not be DMA protected by tboot, so use DMAR copy
595 * SINIT saved in SinitMleData in TXT heap (which is DMA protected)
596 */
597 dmar_tbl = tboot_get_dmar_table(dmar_tbl);
598
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -0700599 dmar = (struct acpi_table_dmar *)dmar_tbl;
600 if (!dmar)
601 return -ENODEV;
602
Fenghua Yu5b6985c2008-10-16 18:02:32 -0700603 if (dmar->width < PAGE_SHIFT - 1) {
Donald Dutilee9071b02012-06-08 17:13:11 -0400604 pr_warn("Invalid DMAR haw\n");
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -0700605 return -EINVAL;
606 }
607
Donald Dutilee9071b02012-06-08 17:13:11 -0400608 pr_info("Host address width %d\n", dmar->width + 1);
Jiang Liuc2a0b532014-11-09 22:47:56 +0800609 ret = dmar_walk_dmar_table(dmar, &cb);
610 if (ret == 0 && drhd_count == 0)
Li, Zhen-Hua7cef3342013-05-20 15:57:32 +0800611 pr_warn(FW_BUG "No DRHD structure found in DMAR table\n");
Jiang Liuc2a0b532014-11-09 22:47:56 +0800612
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -0700613 return ret;
614}
615
David Woodhouse832bd852014-03-07 15:08:36 +0000616static int dmar_pci_device_match(struct dmar_dev_scope devices[],
617 int cnt, struct pci_dev *dev)
Suresh Siddhae61d98d2008-07-10 11:16:35 -0700618{
619 int index;
David Woodhouse832bd852014-03-07 15:08:36 +0000620 struct device *tmp;
Suresh Siddhae61d98d2008-07-10 11:16:35 -0700621
622 while (dev) {
Jiang Liub683b232014-02-19 14:07:32 +0800623 for_each_active_dev_scope(devices, cnt, index, tmp)
David Woodhouse832bd852014-03-07 15:08:36 +0000624 if (dev_is_pci(tmp) && dev == to_pci_dev(tmp))
Suresh Siddhae61d98d2008-07-10 11:16:35 -0700625 return 1;
626
627 /* Check our parent */
628 dev = dev->bus->self;
629 }
630
631 return 0;
632}
633
634struct dmar_drhd_unit *
635dmar_find_matched_drhd_unit(struct pci_dev *dev)
636{
Jiang Liu0e242612014-02-19 14:07:34 +0800637 struct dmar_drhd_unit *dmaru;
Yu Zhao2e824f72008-12-22 16:54:58 +0800638 struct acpi_dmar_hardware_unit *drhd;
Suresh Siddhae61d98d2008-07-10 11:16:35 -0700639
Yinghaidda56542010-04-09 01:07:55 +0100640 dev = pci_physfn(dev);
641
Jiang Liu0e242612014-02-19 14:07:34 +0800642 rcu_read_lock();
Yijing Wang8b161f02013-10-31 17:25:16 +0800643 for_each_drhd_unit(dmaru) {
Yu Zhao2e824f72008-12-22 16:54:58 +0800644 drhd = container_of(dmaru->hdr,
645 struct acpi_dmar_hardware_unit,
646 header);
647
648 if (dmaru->include_all &&
649 drhd->segment == pci_domain_nr(dev->bus))
Jiang Liu0e242612014-02-19 14:07:34 +0800650 goto out;
Yu Zhao2e824f72008-12-22 16:54:58 +0800651
652 if (dmar_pci_device_match(dmaru->devices,
653 dmaru->devices_cnt, dev))
Jiang Liu0e242612014-02-19 14:07:34 +0800654 goto out;
Suresh Siddhae61d98d2008-07-10 11:16:35 -0700655 }
Jiang Liu0e242612014-02-19 14:07:34 +0800656 dmaru = NULL;
657out:
658 rcu_read_unlock();
Suresh Siddhae61d98d2008-07-10 11:16:35 -0700659
Jiang Liu0e242612014-02-19 14:07:34 +0800660 return dmaru;
Suresh Siddhae61d98d2008-07-10 11:16:35 -0700661}
662
David Woodhouseed403562014-03-07 23:15:42 +0000663static void __init dmar_acpi_insert_dev_scope(u8 device_number,
664 struct acpi_device *adev)
665{
666 struct dmar_drhd_unit *dmaru;
667 struct acpi_dmar_hardware_unit *drhd;
668 struct acpi_dmar_device_scope *scope;
669 struct device *tmp;
670 int i;
671 struct acpi_dmar_pci_path *path;
672
673 for_each_drhd_unit(dmaru) {
674 drhd = container_of(dmaru->hdr,
675 struct acpi_dmar_hardware_unit,
676 header);
677
678 for (scope = (void *)(drhd + 1);
679 (unsigned long)scope < ((unsigned long)drhd) + drhd->header.length;
680 scope = ((void *)scope) + scope->length) {
Bob Moore83118b02014-07-30 12:21:00 +0800681 if (scope->entry_type != ACPI_DMAR_SCOPE_TYPE_NAMESPACE)
David Woodhouseed403562014-03-07 23:15:42 +0000682 continue;
683 if (scope->enumeration_id != device_number)
684 continue;
685
686 path = (void *)(scope + 1);
687 pr_info("ACPI device \"%s\" under DMAR at %llx as %02x:%02x.%d\n",
688 dev_name(&adev->dev), dmaru->reg_base_addr,
689 scope->bus, path->device, path->function);
690 for_each_dev_scope(dmaru->devices, dmaru->devices_cnt, i, tmp)
691 if (tmp == NULL) {
692 dmaru->devices[i].bus = scope->bus;
693 dmaru->devices[i].devfn = PCI_DEVFN(path->device,
694 path->function);
695 rcu_assign_pointer(dmaru->devices[i].dev,
696 get_device(&adev->dev));
697 return;
698 }
699 BUG_ON(i >= dmaru->devices_cnt);
700 }
701 }
702 pr_warn("No IOMMU scope found for ANDD enumeration ID %d (%s)\n",
703 device_number, dev_name(&adev->dev));
704}
705
706static int __init dmar_acpi_dev_scope_init(void)
707{
Joerg Roedel11f1a772014-03-25 20:16:40 +0100708 struct acpi_dmar_andd *andd;
709
710 if (dmar_tbl == NULL)
711 return -ENODEV;
712
David Woodhouse7713ec02014-04-01 14:58:36 +0100713 for (andd = (void *)dmar_tbl + sizeof(struct acpi_table_dmar);
714 ((unsigned long)andd) < ((unsigned long)dmar_tbl) + dmar_tbl->length;
715 andd = ((void *)andd) + andd->header.length) {
Bob Moore83118b02014-07-30 12:21:00 +0800716 if (andd->header.type == ACPI_DMAR_TYPE_NAMESPACE) {
David Woodhouseed403562014-03-07 23:15:42 +0000717 acpi_handle h;
718 struct acpi_device *adev;
719
720 if (!ACPI_SUCCESS(acpi_get_handle(ACPI_ROOT_OBJECT,
Bob Moore83118b02014-07-30 12:21:00 +0800721 andd->device_name,
David Woodhouseed403562014-03-07 23:15:42 +0000722 &h))) {
723 pr_err("Failed to find handle for ACPI object %s\n",
Bob Moore83118b02014-07-30 12:21:00 +0800724 andd->device_name);
David Woodhouseed403562014-03-07 23:15:42 +0000725 continue;
726 }
Joerg Roedelc0df9752014-08-21 23:06:48 +0200727 if (acpi_bus_get_device(h, &adev)) {
David Woodhouseed403562014-03-07 23:15:42 +0000728 pr_err("Failed to get device for ACPI object %s\n",
Bob Moore83118b02014-07-30 12:21:00 +0800729 andd->device_name);
David Woodhouseed403562014-03-07 23:15:42 +0000730 continue;
731 }
732 dmar_acpi_insert_dev_scope(andd->device_number, adev);
733 }
David Woodhouseed403562014-03-07 23:15:42 +0000734 }
735 return 0;
736}
737
Suresh Siddha1886e8a2008-07-10 11:16:37 -0700738int __init dmar_dev_scope_init(void)
739{
Jiang Liu2e455282014-02-19 14:07:36 +0800740 struct pci_dev *dev = NULL;
741 struct dmar_pci_notify_info *info;
Suresh Siddha1886e8a2008-07-10 11:16:37 -0700742
Jiang Liu2e455282014-02-19 14:07:36 +0800743 if (dmar_dev_scope_status != 1)
744 return dmar_dev_scope_status;
Suresh Siddhac2c72862011-08-23 17:05:19 -0700745
Jiang Liu2e455282014-02-19 14:07:36 +0800746 if (list_empty(&dmar_drhd_units)) {
747 dmar_dev_scope_status = -ENODEV;
748 } else {
749 dmar_dev_scope_status = 0;
Suresh Siddha318fe7d2011-08-23 17:05:20 -0700750
David Woodhouse63b42622014-03-28 11:28:40 +0000751 dmar_acpi_dev_scope_init();
752
Jiang Liu2e455282014-02-19 14:07:36 +0800753 for_each_pci_dev(dev) {
754 if (dev->is_virtfn)
755 continue;
756
757 info = dmar_alloc_pci_notify_info(dev,
758 BUS_NOTIFY_ADD_DEVICE);
759 if (!info) {
760 return dmar_dev_scope_status;
761 } else {
762 dmar_pci_bus_add_dev(info);
763 dmar_free_pci_notify_info(info);
764 }
765 }
766
767 bus_register_notifier(&pci_bus_type, &dmar_pci_bus_nb);
Suresh Siddha1886e8a2008-07-10 11:16:37 -0700768 }
769
Jiang Liu2e455282014-02-19 14:07:36 +0800770 return dmar_dev_scope_status;
Suresh Siddha1886e8a2008-07-10 11:16:37 -0700771}
772
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -0700773
774int __init dmar_table_init(void)
775{
Suresh Siddha1886e8a2008-07-10 11:16:37 -0700776 static int dmar_table_initialized;
Fenghua Yu093f87d2007-11-21 15:07:14 -0800777 int ret;
778
Jiang Liucc053012014-01-06 14:18:24 +0800779 if (dmar_table_initialized == 0) {
780 ret = parse_dmar_table();
781 if (ret < 0) {
782 if (ret != -ENODEV)
783 pr_info("parse DMAR table failure.\n");
784 } else if (list_empty(&dmar_drhd_units)) {
785 pr_info("No DMAR devices found\n");
786 ret = -ENODEV;
787 }
Suresh Siddha1886e8a2008-07-10 11:16:37 -0700788
Jiang Liucc053012014-01-06 14:18:24 +0800789 if (ret < 0)
790 dmar_table_initialized = ret;
791 else
792 dmar_table_initialized = 1;
Fenghua Yu093f87d2007-11-21 15:07:14 -0800793 }
794
Jiang Liucc053012014-01-06 14:18:24 +0800795 return dmar_table_initialized < 0 ? dmar_table_initialized : 0;
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -0700796}
797
Ben Hutchings3a8663e2010-04-03 19:37:23 +0100798static void warn_invalid_dmar(u64 addr, const char *message)
799{
Ben Hutchingsfd0c8892010-04-03 19:38:43 +0100800 WARN_TAINT_ONCE(
801 1, TAINT_FIRMWARE_WORKAROUND,
802 "Your BIOS is broken; DMAR reported at address %llx%s!\n"
803 "BIOS vendor: %s; Ver: %s; Product Version: %s\n",
804 addr, message,
805 dmi_get_system_info(DMI_BIOS_VENDOR),
806 dmi_get_system_info(DMI_BIOS_VERSION),
807 dmi_get_system_info(DMI_PRODUCT_VERSION));
Ben Hutchings3a8663e2010-04-03 19:37:23 +0100808}
David Woodhouse6ecbf012009-12-02 09:20:27 +0000809
Jiang Liuc2a0b532014-11-09 22:47:56 +0800810static int __ref
811dmar_validate_one_drhd(struct acpi_dmar_header *entry, void *arg)
David Woodhouse86cf8982009-11-09 22:15:15 +0000812{
David Woodhouse86cf8982009-11-09 22:15:15 +0000813 struct acpi_dmar_hardware_unit *drhd;
Jiang Liuc2a0b532014-11-09 22:47:56 +0800814 void __iomem *addr;
815 u64 cap, ecap;
David Woodhouse86cf8982009-11-09 22:15:15 +0000816
Jiang Liuc2a0b532014-11-09 22:47:56 +0800817 drhd = (void *)entry;
818 if (!drhd->address) {
819 warn_invalid_dmar(0, "");
820 return -EINVAL;
David Woodhouse86cf8982009-11-09 22:15:15 +0000821 }
Chris Wright2c992202009-12-02 09:17:13 +0000822
Jiang Liuc2a0b532014-11-09 22:47:56 +0800823 addr = early_ioremap(drhd->address, VTD_PAGE_SIZE);
824 if (!addr) {
825 pr_warn("IOMMU: can't validate: %llx\n", drhd->address);
826 return -EINVAL;
827 }
828 cap = dmar_readq(addr + DMAR_CAP_REG);
829 ecap = dmar_readq(addr + DMAR_ECAP_REG);
830 early_iounmap(addr, VTD_PAGE_SIZE);
831
832 if (cap == (uint64_t)-1 && ecap == (uint64_t)-1) {
833 warn_invalid_dmar(drhd->address, " returns all ones");
834 return -EINVAL;
835 }
836
Chris Wright2c992202009-12-02 09:17:13 +0000837 return 0;
David Woodhouse86cf8982009-11-09 22:15:15 +0000838}
839
Konrad Rzeszutek Wilk480125b2010-08-26 13:57:57 -0400840int __init detect_intel_iommu(void)
Suresh Siddha2ae21012008-07-10 11:16:43 -0700841{
842 int ret;
Jiang Liuc2a0b532014-11-09 22:47:56 +0800843 struct dmar_res_callback validate_drhd_cb = {
844 .cb[ACPI_DMAR_TYPE_HARDWARE_UNIT] = &dmar_validate_one_drhd,
845 .ignore_unhandled = true,
846 };
Suresh Siddha2ae21012008-07-10 11:16:43 -0700847
Jiang Liu3a5670e2014-02-19 14:07:33 +0800848 down_write(&dmar_global_lock);
Yinghai Luf6dd5c32008-09-03 16:58:32 -0700849 ret = dmar_table_detect();
David Woodhouse86cf8982009-11-09 22:15:15 +0000850 if (ret)
Jiang Liuc2a0b532014-11-09 22:47:56 +0800851 ret = !dmar_walk_dmar_table((struct acpi_table_dmar *)dmar_tbl,
852 &validate_drhd_cb);
853 if (ret && !no_iommu && !iommu_detected && !dmar_disabled) {
854 iommu_detected = 1;
855 /* Make sure ACS will be enabled */
856 pci_request_acs();
857 }
Suresh Siddhaf5d1b972011-08-23 17:05:22 -0700858
FUJITA Tomonori9d5ce732009-11-10 19:46:16 +0900859#ifdef CONFIG_X86
Jiang Liuc2a0b532014-11-09 22:47:56 +0800860 if (ret)
861 x86_init.iommu.iommu_init = intel_iommu_init;
FUJITA Tomonori9d5ce732009-11-10 19:46:16 +0900862#endif
Jiang Liuc2a0b532014-11-09 22:47:56 +0800863
Jiang Liub707cb02014-01-06 14:18:26 +0800864 early_acpi_os_unmap_memory((void __iomem *)dmar_tbl, dmar_tbl_size);
Yinghai Luf6dd5c32008-09-03 16:58:32 -0700865 dmar_tbl = NULL;
Jiang Liu3a5670e2014-02-19 14:07:33 +0800866 up_write(&dmar_global_lock);
Konrad Rzeszutek Wilk480125b2010-08-26 13:57:57 -0400867
Konrad Rzeszutek Wilk4db77ff2010-08-26 13:58:04 -0400868 return ret ? 1 : -ENODEV;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700869}
870
871
Donald Dutile6f5cf522012-06-04 17:29:02 -0400872static void unmap_iommu(struct intel_iommu *iommu)
873{
874 iounmap(iommu->reg);
875 release_mem_region(iommu->reg_phys, iommu->reg_size);
876}
877
878/**
879 * map_iommu: map the iommu's registers
880 * @iommu: the iommu to map
881 * @phys_addr: the physical address of the base resgister
Donald Dutilee9071b02012-06-08 17:13:11 -0400882 *
Donald Dutile6f5cf522012-06-04 17:29:02 -0400883 * Memory map the iommu's registers. Start w/ a single page, and
Donald Dutilee9071b02012-06-08 17:13:11 -0400884 * possibly expand if that turns out to be insufficent.
Donald Dutile6f5cf522012-06-04 17:29:02 -0400885 */
886static int map_iommu(struct intel_iommu *iommu, u64 phys_addr)
887{
888 int map_size, err=0;
889
890 iommu->reg_phys = phys_addr;
891 iommu->reg_size = VTD_PAGE_SIZE;
892
893 if (!request_mem_region(iommu->reg_phys, iommu->reg_size, iommu->name)) {
894 pr_err("IOMMU: can't reserve memory\n");
895 err = -EBUSY;
896 goto out;
897 }
898
899 iommu->reg = ioremap(iommu->reg_phys, iommu->reg_size);
900 if (!iommu->reg) {
901 pr_err("IOMMU: can't map the region\n");
902 err = -ENOMEM;
903 goto release;
904 }
905
906 iommu->cap = dmar_readq(iommu->reg + DMAR_CAP_REG);
907 iommu->ecap = dmar_readq(iommu->reg + DMAR_ECAP_REG);
908
909 if (iommu->cap == (uint64_t)-1 && iommu->ecap == (uint64_t)-1) {
910 err = -EINVAL;
911 warn_invalid_dmar(phys_addr, " returns all ones");
912 goto unmap;
913 }
914
915 /* the registers might be more than one page */
916 map_size = max_t(int, ecap_max_iotlb_offset(iommu->ecap),
917 cap_max_fault_reg_offset(iommu->cap));
918 map_size = VTD_PAGE_ALIGN(map_size);
919 if (map_size > iommu->reg_size) {
920 iounmap(iommu->reg);
921 release_mem_region(iommu->reg_phys, iommu->reg_size);
922 iommu->reg_size = map_size;
923 if (!request_mem_region(iommu->reg_phys, iommu->reg_size,
924 iommu->name)) {
925 pr_err("IOMMU: can't reserve memory\n");
926 err = -EBUSY;
927 goto out;
928 }
929 iommu->reg = ioremap(iommu->reg_phys, iommu->reg_size);
930 if (!iommu->reg) {
931 pr_err("IOMMU: can't map the region\n");
932 err = -ENOMEM;
933 goto release;
934 }
935 }
936 err = 0;
937 goto out;
938
939unmap:
940 iounmap(iommu->reg);
941release:
942 release_mem_region(iommu->reg_phys, iommu->reg_size);
943out:
944 return err;
945}
946
Jiang Liu694835d2014-01-06 14:18:16 +0800947static int alloc_iommu(struct dmar_drhd_unit *drhd)
Suresh Siddhae61d98d2008-07-10 11:16:35 -0700948{
Suresh Siddhac42d9f32008-07-10 11:16:36 -0700949 struct intel_iommu *iommu;
Takao Indoh3a93c842013-04-23 17:35:03 +0900950 u32 ver, sts;
Suresh Siddhac42d9f32008-07-10 11:16:36 -0700951 static int iommu_allocated = 0;
Joerg Roedel43f73922009-01-03 23:56:27 +0100952 int agaw = 0;
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -0700953 int msagaw = 0;
Donald Dutile6f5cf522012-06-04 17:29:02 -0400954 int err;
Suresh Siddhac42d9f32008-07-10 11:16:36 -0700955
David Woodhouse6ecbf012009-12-02 09:20:27 +0000956 if (!drhd->reg_base_addr) {
Ben Hutchings3a8663e2010-04-03 19:37:23 +0100957 warn_invalid_dmar(0, "");
David Woodhouse6ecbf012009-12-02 09:20:27 +0000958 return -EINVAL;
959 }
960
Suresh Siddhac42d9f32008-07-10 11:16:36 -0700961 iommu = kzalloc(sizeof(*iommu), GFP_KERNEL);
962 if (!iommu)
Suresh Siddha1886e8a2008-07-10 11:16:37 -0700963 return -ENOMEM;
Suresh Siddhac42d9f32008-07-10 11:16:36 -0700964
965 iommu->seq_id = iommu_allocated++;
Suresh Siddha9d783ba2009-03-16 17:04:55 -0700966 sprintf (iommu->name, "dmar%d", iommu->seq_id);
Suresh Siddhae61d98d2008-07-10 11:16:35 -0700967
Donald Dutile6f5cf522012-06-04 17:29:02 -0400968 err = map_iommu(iommu, drhd->reg_base_addr);
969 if (err) {
970 pr_err("IOMMU: failed to map %s\n", iommu->name);
Suresh Siddhae61d98d2008-07-10 11:16:35 -0700971 goto error;
972 }
Suresh Siddhae61d98d2008-07-10 11:16:35 -0700973
Donald Dutile6f5cf522012-06-04 17:29:02 -0400974 err = -EINVAL;
Weidong Han1b573682008-12-08 15:34:06 +0800975 agaw = iommu_calculate_agaw(iommu);
976 if (agaw < 0) {
Donald Dutilebf947fcb2012-06-04 17:29:01 -0400977 pr_err("Cannot get a valid agaw for iommu (seq_id = %d)\n",
978 iommu->seq_id);
David Woodhouse08155652009-08-04 09:17:20 +0100979 goto err_unmap;
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -0700980 }
981 msagaw = iommu_calculate_max_sagaw(iommu);
982 if (msagaw < 0) {
Donald Dutilebf947fcb2012-06-04 17:29:01 -0400983 pr_err("Cannot get a valid max agaw for iommu (seq_id = %d)\n",
Weidong Han1b573682008-12-08 15:34:06 +0800984 iommu->seq_id);
David Woodhouse08155652009-08-04 09:17:20 +0100985 goto err_unmap;
Weidong Han1b573682008-12-08 15:34:06 +0800986 }
987 iommu->agaw = agaw;
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -0700988 iommu->msagaw = msagaw;
David Woodhouse67ccac42014-03-09 13:49:45 -0700989 iommu->segment = drhd->segment;
Weidong Han1b573682008-12-08 15:34:06 +0800990
Suresh Siddhaee34b322009-10-02 11:01:21 -0700991 iommu->node = -1;
992
Suresh Siddhae61d98d2008-07-10 11:16:35 -0700993 ver = readl(iommu->reg + DMAR_VER_REG);
Yinghai Lu680a7522010-04-08 19:58:23 +0100994 pr_info("IOMMU %d: reg_base_addr %llx ver %d:%d cap %llx ecap %llx\n",
995 iommu->seq_id,
Fenghua Yu5b6985c2008-10-16 18:02:32 -0700996 (unsigned long long)drhd->reg_base_addr,
997 DMAR_VER_MAJOR(ver), DMAR_VER_MINOR(ver),
998 (unsigned long long)iommu->cap,
999 (unsigned long long)iommu->ecap);
Suresh Siddhae61d98d2008-07-10 11:16:35 -07001000
Takao Indoh3a93c842013-04-23 17:35:03 +09001001 /* Reflect status in gcmd */
1002 sts = readl(iommu->reg + DMAR_GSTS_REG);
1003 if (sts & DMA_GSTS_IRES)
1004 iommu->gcmd |= DMA_GCMD_IRE;
1005 if (sts & DMA_GSTS_TES)
1006 iommu->gcmd |= DMA_GCMD_TE;
1007 if (sts & DMA_GSTS_QIES)
1008 iommu->gcmd |= DMA_GCMD_QIE;
1009
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001010 raw_spin_lock_init(&iommu->register_lock);
Suresh Siddhae61d98d2008-07-10 11:16:35 -07001011
1012 drhd->iommu = iommu;
Alex Williamsona5459cf2014-06-12 16:12:31 -06001013
1014 if (intel_iommu_enabled)
1015 iommu->iommu_dev = iommu_device_create(NULL, iommu,
1016 intel_iommu_groups,
1017 iommu->name);
1018
Suresh Siddha1886e8a2008-07-10 11:16:37 -07001019 return 0;
David Woodhouse08155652009-08-04 09:17:20 +01001020
1021 err_unmap:
Donald Dutile6f5cf522012-06-04 17:29:02 -04001022 unmap_iommu(iommu);
David Woodhouse08155652009-08-04 09:17:20 +01001023 error:
Suresh Siddhae61d98d2008-07-10 11:16:35 -07001024 kfree(iommu);
Donald Dutile6f5cf522012-06-04 17:29:02 -04001025 return err;
Suresh Siddhae61d98d2008-07-10 11:16:35 -07001026}
1027
Jiang Liua868e6b2014-01-06 14:18:20 +08001028static void free_iommu(struct intel_iommu *iommu)
Suresh Siddhae61d98d2008-07-10 11:16:35 -07001029{
Alex Williamsona5459cf2014-06-12 16:12:31 -06001030 iommu_device_destroy(iommu->iommu_dev);
1031
Jiang Liua868e6b2014-01-06 14:18:20 +08001032 if (iommu->irq) {
1033 free_irq(iommu->irq, iommu);
1034 irq_set_handler_data(iommu->irq, NULL);
Thomas Gleixnera553b142014-05-07 15:44:11 +00001035 dmar_free_hwirq(iommu->irq);
Jiang Liua868e6b2014-01-06 14:18:20 +08001036 }
Suresh Siddhae61d98d2008-07-10 11:16:35 -07001037
Jiang Liua84da702014-01-06 14:18:23 +08001038 if (iommu->qi) {
1039 free_page((unsigned long)iommu->qi->desc);
1040 kfree(iommu->qi->desc_status);
1041 kfree(iommu->qi);
1042 }
1043
Suresh Siddhae61d98d2008-07-10 11:16:35 -07001044 if (iommu->reg)
Donald Dutile6f5cf522012-06-04 17:29:02 -04001045 unmap_iommu(iommu);
1046
Suresh Siddhae61d98d2008-07-10 11:16:35 -07001047 kfree(iommu);
1048}
Suresh Siddhafe962e92008-07-10 11:16:42 -07001049
1050/*
1051 * Reclaim all the submitted descriptors which have completed its work.
1052 */
1053static inline void reclaim_free_desc(struct q_inval *qi)
1054{
Yu Zhao6ba6c3a2009-05-18 13:51:35 +08001055 while (qi->desc_status[qi->free_tail] == QI_DONE ||
1056 qi->desc_status[qi->free_tail] == QI_ABORT) {
Suresh Siddhafe962e92008-07-10 11:16:42 -07001057 qi->desc_status[qi->free_tail] = QI_FREE;
1058 qi->free_tail = (qi->free_tail + 1) % QI_LENGTH;
1059 qi->free_cnt++;
1060 }
1061}
1062
Yu Zhao704126a2009-01-04 16:28:52 +08001063static int qi_check_fault(struct intel_iommu *iommu, int index)
1064{
1065 u32 fault;
Yu Zhao6ba6c3a2009-05-18 13:51:35 +08001066 int head, tail;
Yu Zhao704126a2009-01-04 16:28:52 +08001067 struct q_inval *qi = iommu->qi;
1068 int wait_index = (index + 1) % QI_LENGTH;
1069
Yu Zhao6ba6c3a2009-05-18 13:51:35 +08001070 if (qi->desc_status[wait_index] == QI_ABORT)
1071 return -EAGAIN;
1072
Yu Zhao704126a2009-01-04 16:28:52 +08001073 fault = readl(iommu->reg + DMAR_FSTS_REG);
1074
1075 /*
1076 * If IQE happens, the head points to the descriptor associated
1077 * with the error. No new descriptors are fetched until the IQE
1078 * is cleared.
1079 */
1080 if (fault & DMA_FSTS_IQE) {
1081 head = readl(iommu->reg + DMAR_IQH_REG);
Yu Zhao6ba6c3a2009-05-18 13:51:35 +08001082 if ((head >> DMAR_IQ_SHIFT) == index) {
Donald Dutilebf947fcb2012-06-04 17:29:01 -04001083 pr_err("VT-d detected invalid descriptor: "
Yu Zhao6ba6c3a2009-05-18 13:51:35 +08001084 "low=%llx, high=%llx\n",
1085 (unsigned long long)qi->desc[index].low,
1086 (unsigned long long)qi->desc[index].high);
Yu Zhao704126a2009-01-04 16:28:52 +08001087 memcpy(&qi->desc[index], &qi->desc[wait_index],
1088 sizeof(struct qi_desc));
1089 __iommu_flush_cache(iommu, &qi->desc[index],
1090 sizeof(struct qi_desc));
1091 writel(DMA_FSTS_IQE, iommu->reg + DMAR_FSTS_REG);
1092 return -EINVAL;
1093 }
1094 }
1095
Yu Zhao6ba6c3a2009-05-18 13:51:35 +08001096 /*
1097 * If ITE happens, all pending wait_desc commands are aborted.
1098 * No new descriptors are fetched until the ITE is cleared.
1099 */
1100 if (fault & DMA_FSTS_ITE) {
1101 head = readl(iommu->reg + DMAR_IQH_REG);
1102 head = ((head >> DMAR_IQ_SHIFT) - 1 + QI_LENGTH) % QI_LENGTH;
1103 head |= 1;
1104 tail = readl(iommu->reg + DMAR_IQT_REG);
1105 tail = ((tail >> DMAR_IQ_SHIFT) - 1 + QI_LENGTH) % QI_LENGTH;
1106
1107 writel(DMA_FSTS_ITE, iommu->reg + DMAR_FSTS_REG);
1108
1109 do {
1110 if (qi->desc_status[head] == QI_IN_USE)
1111 qi->desc_status[head] = QI_ABORT;
1112 head = (head - 2 + QI_LENGTH) % QI_LENGTH;
1113 } while (head != tail);
1114
1115 if (qi->desc_status[wait_index] == QI_ABORT)
1116 return -EAGAIN;
1117 }
1118
1119 if (fault & DMA_FSTS_ICE)
1120 writel(DMA_FSTS_ICE, iommu->reg + DMAR_FSTS_REG);
1121
Yu Zhao704126a2009-01-04 16:28:52 +08001122 return 0;
1123}
1124
Suresh Siddhafe962e92008-07-10 11:16:42 -07001125/*
1126 * Submit the queued invalidation descriptor to the remapping
1127 * hardware unit and wait for its completion.
1128 */
Yu Zhao704126a2009-01-04 16:28:52 +08001129int qi_submit_sync(struct qi_desc *desc, struct intel_iommu *iommu)
Suresh Siddhafe962e92008-07-10 11:16:42 -07001130{
Yu Zhao6ba6c3a2009-05-18 13:51:35 +08001131 int rc;
Suresh Siddhafe962e92008-07-10 11:16:42 -07001132 struct q_inval *qi = iommu->qi;
1133 struct qi_desc *hw, wait_desc;
1134 int wait_index, index;
1135 unsigned long flags;
1136
1137 if (!qi)
Yu Zhao704126a2009-01-04 16:28:52 +08001138 return 0;
Suresh Siddhafe962e92008-07-10 11:16:42 -07001139
1140 hw = qi->desc;
1141
Yu Zhao6ba6c3a2009-05-18 13:51:35 +08001142restart:
1143 rc = 0;
1144
Thomas Gleixner3b8f4042011-07-19 17:02:07 +02001145 raw_spin_lock_irqsave(&qi->q_lock, flags);
Suresh Siddhafe962e92008-07-10 11:16:42 -07001146 while (qi->free_cnt < 3) {
Thomas Gleixner3b8f4042011-07-19 17:02:07 +02001147 raw_spin_unlock_irqrestore(&qi->q_lock, flags);
Suresh Siddhafe962e92008-07-10 11:16:42 -07001148 cpu_relax();
Thomas Gleixner3b8f4042011-07-19 17:02:07 +02001149 raw_spin_lock_irqsave(&qi->q_lock, flags);
Suresh Siddhafe962e92008-07-10 11:16:42 -07001150 }
1151
1152 index = qi->free_head;
1153 wait_index = (index + 1) % QI_LENGTH;
1154
1155 qi->desc_status[index] = qi->desc_status[wait_index] = QI_IN_USE;
1156
1157 hw[index] = *desc;
1158
Yu Zhao704126a2009-01-04 16:28:52 +08001159 wait_desc.low = QI_IWD_STATUS_DATA(QI_DONE) |
1160 QI_IWD_STATUS_WRITE | QI_IWD_TYPE;
Suresh Siddhafe962e92008-07-10 11:16:42 -07001161 wait_desc.high = virt_to_phys(&qi->desc_status[wait_index]);
1162
1163 hw[wait_index] = wait_desc;
1164
1165 __iommu_flush_cache(iommu, &hw[index], sizeof(struct qi_desc));
1166 __iommu_flush_cache(iommu, &hw[wait_index], sizeof(struct qi_desc));
1167
1168 qi->free_head = (qi->free_head + 2) % QI_LENGTH;
1169 qi->free_cnt -= 2;
1170
Suresh Siddhafe962e92008-07-10 11:16:42 -07001171 /*
1172 * update the HW tail register indicating the presence of
1173 * new descriptors.
1174 */
Yu Zhao6ba6c3a2009-05-18 13:51:35 +08001175 writel(qi->free_head << DMAR_IQ_SHIFT, iommu->reg + DMAR_IQT_REG);
Suresh Siddhafe962e92008-07-10 11:16:42 -07001176
1177 while (qi->desc_status[wait_index] != QI_DONE) {
Suresh Siddhaf05810c2008-10-16 16:31:54 -07001178 /*
1179 * We will leave the interrupts disabled, to prevent interrupt
1180 * context to queue another cmd while a cmd is already submitted
1181 * and waiting for completion on this cpu. This is to avoid
1182 * a deadlock where the interrupt context can wait indefinitely
1183 * for free slots in the queue.
1184 */
Yu Zhao704126a2009-01-04 16:28:52 +08001185 rc = qi_check_fault(iommu, index);
1186 if (rc)
Yu Zhao6ba6c3a2009-05-18 13:51:35 +08001187 break;
Yu Zhao704126a2009-01-04 16:28:52 +08001188
Thomas Gleixner3b8f4042011-07-19 17:02:07 +02001189 raw_spin_unlock(&qi->q_lock);
Suresh Siddhafe962e92008-07-10 11:16:42 -07001190 cpu_relax();
Thomas Gleixner3b8f4042011-07-19 17:02:07 +02001191 raw_spin_lock(&qi->q_lock);
Suresh Siddhafe962e92008-07-10 11:16:42 -07001192 }
Yu Zhao6ba6c3a2009-05-18 13:51:35 +08001193
1194 qi->desc_status[index] = QI_DONE;
Suresh Siddhafe962e92008-07-10 11:16:42 -07001195
1196 reclaim_free_desc(qi);
Thomas Gleixner3b8f4042011-07-19 17:02:07 +02001197 raw_spin_unlock_irqrestore(&qi->q_lock, flags);
Yu Zhao704126a2009-01-04 16:28:52 +08001198
Yu Zhao6ba6c3a2009-05-18 13:51:35 +08001199 if (rc == -EAGAIN)
1200 goto restart;
1201
Yu Zhao704126a2009-01-04 16:28:52 +08001202 return rc;
Suresh Siddhafe962e92008-07-10 11:16:42 -07001203}
1204
1205/*
1206 * Flush the global interrupt entry cache.
1207 */
1208void qi_global_iec(struct intel_iommu *iommu)
1209{
1210 struct qi_desc desc;
1211
1212 desc.low = QI_IEC_TYPE;
1213 desc.high = 0;
1214
Yu Zhao704126a2009-01-04 16:28:52 +08001215 /* should never fail */
Suresh Siddhafe962e92008-07-10 11:16:42 -07001216 qi_submit_sync(&desc, iommu);
1217}
1218
David Woodhouse4c25a2c2009-05-10 17:16:06 +01001219void qi_flush_context(struct intel_iommu *iommu, u16 did, u16 sid, u8 fm,
1220 u64 type)
Youquan Song3481f212008-10-16 16:31:55 -07001221{
Youquan Song3481f212008-10-16 16:31:55 -07001222 struct qi_desc desc;
1223
Youquan Song3481f212008-10-16 16:31:55 -07001224 desc.low = QI_CC_FM(fm) | QI_CC_SID(sid) | QI_CC_DID(did)
1225 | QI_CC_GRAN(type) | QI_CC_TYPE;
1226 desc.high = 0;
1227
David Woodhouse4c25a2c2009-05-10 17:16:06 +01001228 qi_submit_sync(&desc, iommu);
Youquan Song3481f212008-10-16 16:31:55 -07001229}
1230
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01001231void qi_flush_iotlb(struct intel_iommu *iommu, u16 did, u64 addr,
1232 unsigned int size_order, u64 type)
Youquan Song3481f212008-10-16 16:31:55 -07001233{
1234 u8 dw = 0, dr = 0;
1235
1236 struct qi_desc desc;
1237 int ih = 0;
1238
Youquan Song3481f212008-10-16 16:31:55 -07001239 if (cap_write_drain(iommu->cap))
1240 dw = 1;
1241
1242 if (cap_read_drain(iommu->cap))
1243 dr = 1;
1244
1245 desc.low = QI_IOTLB_DID(did) | QI_IOTLB_DR(dr) | QI_IOTLB_DW(dw)
1246 | QI_IOTLB_GRAN(type) | QI_IOTLB_TYPE;
1247 desc.high = QI_IOTLB_ADDR(addr) | QI_IOTLB_IH(ih)
1248 | QI_IOTLB_AM(size_order);
1249
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01001250 qi_submit_sync(&desc, iommu);
Youquan Song3481f212008-10-16 16:31:55 -07001251}
1252
Yu Zhao6ba6c3a2009-05-18 13:51:35 +08001253void qi_flush_dev_iotlb(struct intel_iommu *iommu, u16 sid, u16 qdep,
1254 u64 addr, unsigned mask)
1255{
1256 struct qi_desc desc;
1257
1258 if (mask) {
1259 BUG_ON(addr & ((1 << (VTD_PAGE_SHIFT + mask)) - 1));
1260 addr |= (1 << (VTD_PAGE_SHIFT + mask - 1)) - 1;
1261 desc.high = QI_DEV_IOTLB_ADDR(addr) | QI_DEV_IOTLB_SIZE;
1262 } else
1263 desc.high = QI_DEV_IOTLB_ADDR(addr);
1264
1265 if (qdep >= QI_DEV_IOTLB_MAX_INVS)
1266 qdep = 0;
1267
1268 desc.low = QI_DEV_IOTLB_SID(sid) | QI_DEV_IOTLB_QDEP(qdep) |
1269 QI_DIOTLB_TYPE;
1270
1271 qi_submit_sync(&desc, iommu);
1272}
1273
Suresh Siddhafe962e92008-07-10 11:16:42 -07001274/*
Suresh Siddhaeba67e52009-03-16 17:04:56 -07001275 * Disable Queued Invalidation interface.
1276 */
1277void dmar_disable_qi(struct intel_iommu *iommu)
1278{
1279 unsigned long flags;
1280 u32 sts;
1281 cycles_t start_time = get_cycles();
1282
1283 if (!ecap_qis(iommu->ecap))
1284 return;
1285
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001286 raw_spin_lock_irqsave(&iommu->register_lock, flags);
Suresh Siddhaeba67e52009-03-16 17:04:56 -07001287
1288 sts = dmar_readq(iommu->reg + DMAR_GSTS_REG);
1289 if (!(sts & DMA_GSTS_QIES))
1290 goto end;
1291
1292 /*
1293 * Give a chance to HW to complete the pending invalidation requests.
1294 */
1295 while ((readl(iommu->reg + DMAR_IQT_REG) !=
1296 readl(iommu->reg + DMAR_IQH_REG)) &&
1297 (DMAR_OPERATION_TIMEOUT > (get_cycles() - start_time)))
1298 cpu_relax();
1299
1300 iommu->gcmd &= ~DMA_GCMD_QIE;
Suresh Siddhaeba67e52009-03-16 17:04:56 -07001301 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
1302
1303 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG, readl,
1304 !(sts & DMA_GSTS_QIES), sts);
1305end:
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001306 raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
Suresh Siddhaeba67e52009-03-16 17:04:56 -07001307}
1308
1309/*
Fenghua Yueb4a52b2009-03-27 14:22:43 -07001310 * Enable queued invalidation.
1311 */
1312static void __dmar_enable_qi(struct intel_iommu *iommu)
1313{
David Woodhousec416daa2009-05-10 20:30:58 +01001314 u32 sts;
Fenghua Yueb4a52b2009-03-27 14:22:43 -07001315 unsigned long flags;
1316 struct q_inval *qi = iommu->qi;
1317
1318 qi->free_head = qi->free_tail = 0;
1319 qi->free_cnt = QI_LENGTH;
1320
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001321 raw_spin_lock_irqsave(&iommu->register_lock, flags);
Fenghua Yueb4a52b2009-03-27 14:22:43 -07001322
1323 /* write zero to the tail reg */
1324 writel(0, iommu->reg + DMAR_IQT_REG);
1325
1326 dmar_writeq(iommu->reg + DMAR_IQA_REG, virt_to_phys(qi->desc));
1327
Fenghua Yueb4a52b2009-03-27 14:22:43 -07001328 iommu->gcmd |= DMA_GCMD_QIE;
David Woodhousec416daa2009-05-10 20:30:58 +01001329 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
Fenghua Yueb4a52b2009-03-27 14:22:43 -07001330
1331 /* Make sure hardware complete it */
1332 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG, readl, (sts & DMA_GSTS_QIES), sts);
1333
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001334 raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
Fenghua Yueb4a52b2009-03-27 14:22:43 -07001335}
1336
1337/*
Suresh Siddhafe962e92008-07-10 11:16:42 -07001338 * Enable Queued Invalidation interface. This is a must to support
1339 * interrupt-remapping. Also used by DMA-remapping, which replaces
1340 * register based IOTLB invalidation.
1341 */
1342int dmar_enable_qi(struct intel_iommu *iommu)
1343{
Suresh Siddhafe962e92008-07-10 11:16:42 -07001344 struct q_inval *qi;
Suresh Siddha751cafe2009-10-02 11:01:22 -07001345 struct page *desc_page;
Suresh Siddhafe962e92008-07-10 11:16:42 -07001346
1347 if (!ecap_qis(iommu->ecap))
1348 return -ENOENT;
1349
1350 /*
1351 * queued invalidation is already setup and enabled.
1352 */
1353 if (iommu->qi)
1354 return 0;
1355
Suresh Siddhafa4b57c2009-03-16 17:05:05 -07001356 iommu->qi = kmalloc(sizeof(*qi), GFP_ATOMIC);
Suresh Siddhafe962e92008-07-10 11:16:42 -07001357 if (!iommu->qi)
1358 return -ENOMEM;
1359
1360 qi = iommu->qi;
1361
Suresh Siddha751cafe2009-10-02 11:01:22 -07001362
1363 desc_page = alloc_pages_node(iommu->node, GFP_ATOMIC | __GFP_ZERO, 0);
1364 if (!desc_page) {
Suresh Siddhafe962e92008-07-10 11:16:42 -07001365 kfree(qi);
Jiang Liub707cb02014-01-06 14:18:26 +08001366 iommu->qi = NULL;
Suresh Siddhafe962e92008-07-10 11:16:42 -07001367 return -ENOMEM;
1368 }
1369
Suresh Siddha751cafe2009-10-02 11:01:22 -07001370 qi->desc = page_address(desc_page);
1371
Hannes Reinecke37a40712013-02-06 09:50:10 +01001372 qi->desc_status = kzalloc(QI_LENGTH * sizeof(int), GFP_ATOMIC);
Suresh Siddhafe962e92008-07-10 11:16:42 -07001373 if (!qi->desc_status) {
1374 free_page((unsigned long) qi->desc);
1375 kfree(qi);
Jiang Liub707cb02014-01-06 14:18:26 +08001376 iommu->qi = NULL;
Suresh Siddhafe962e92008-07-10 11:16:42 -07001377 return -ENOMEM;
1378 }
1379
Thomas Gleixner3b8f4042011-07-19 17:02:07 +02001380 raw_spin_lock_init(&qi->q_lock);
Suresh Siddhafe962e92008-07-10 11:16:42 -07001381
Fenghua Yueb4a52b2009-03-27 14:22:43 -07001382 __dmar_enable_qi(iommu);
Suresh Siddhafe962e92008-07-10 11:16:42 -07001383
1384 return 0;
1385}
Suresh Siddha0ac24912009-03-16 17:04:54 -07001386
1387/* iommu interrupt handling. Most stuff are MSI-like. */
1388
Suresh Siddha9d783ba2009-03-16 17:04:55 -07001389enum faulttype {
1390 DMA_REMAP,
1391 INTR_REMAP,
1392 UNKNOWN,
1393};
1394
1395static const char *dma_remap_fault_reasons[] =
Suresh Siddha0ac24912009-03-16 17:04:54 -07001396{
1397 "Software",
1398 "Present bit in root entry is clear",
1399 "Present bit in context entry is clear",
1400 "Invalid context entry",
1401 "Access beyond MGAW",
1402 "PTE Write access is not set",
1403 "PTE Read access is not set",
1404 "Next page table ptr is invalid",
1405 "Root table address invalid",
1406 "Context table ptr is invalid",
1407 "non-zero reserved fields in RTP",
1408 "non-zero reserved fields in CTP",
1409 "non-zero reserved fields in PTE",
Li, Zhen-Hua4ecccd92013-03-06 10:43:17 +08001410 "PCE for translation request specifies blocking",
Suresh Siddha0ac24912009-03-16 17:04:54 -07001411};
Suresh Siddha9d783ba2009-03-16 17:04:55 -07001412
Suresh Siddha95a02e92012-03-30 11:47:07 -07001413static const char *irq_remap_fault_reasons[] =
Suresh Siddha9d783ba2009-03-16 17:04:55 -07001414{
1415 "Detected reserved fields in the decoded interrupt-remapped request",
1416 "Interrupt index exceeded the interrupt-remapping table size",
1417 "Present field in the IRTE entry is clear",
1418 "Error accessing interrupt-remapping table pointed by IRTA_REG",
1419 "Detected reserved fields in the IRTE entry",
1420 "Blocked a compatibility format interrupt request",
1421 "Blocked an interrupt request due to source-id verification failure",
1422};
1423
Rashika Kheria21004dc2013-12-18 12:01:46 +05301424static const char *dmar_get_fault_reason(u8 fault_reason, int *fault_type)
Suresh Siddha0ac24912009-03-16 17:04:54 -07001425{
Dan Carpenterfefe1ed2012-05-13 20:09:38 +03001426 if (fault_reason >= 0x20 && (fault_reason - 0x20 <
1427 ARRAY_SIZE(irq_remap_fault_reasons))) {
Suresh Siddha9d783ba2009-03-16 17:04:55 -07001428 *fault_type = INTR_REMAP;
Suresh Siddha95a02e92012-03-30 11:47:07 -07001429 return irq_remap_fault_reasons[fault_reason - 0x20];
Suresh Siddha9d783ba2009-03-16 17:04:55 -07001430 } else if (fault_reason < ARRAY_SIZE(dma_remap_fault_reasons)) {
1431 *fault_type = DMA_REMAP;
1432 return dma_remap_fault_reasons[fault_reason];
1433 } else {
1434 *fault_type = UNKNOWN;
Suresh Siddha0ac24912009-03-16 17:04:54 -07001435 return "Unknown";
Suresh Siddha9d783ba2009-03-16 17:04:55 -07001436 }
Suresh Siddha0ac24912009-03-16 17:04:54 -07001437}
1438
Thomas Gleixner5c2837f2010-09-28 17:15:11 +02001439void dmar_msi_unmask(struct irq_data *data)
Suresh Siddha0ac24912009-03-16 17:04:54 -07001440{
Thomas Gleixnerdced35a2011-03-28 17:49:12 +02001441 struct intel_iommu *iommu = irq_data_get_irq_handler_data(data);
Suresh Siddha0ac24912009-03-16 17:04:54 -07001442 unsigned long flag;
1443
1444 /* unmask it */
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001445 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Suresh Siddha0ac24912009-03-16 17:04:54 -07001446 writel(0, iommu->reg + DMAR_FECTL_REG);
1447 /* Read a reg to force flush the post write */
1448 readl(iommu->reg + DMAR_FECTL_REG);
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001449 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Suresh Siddha0ac24912009-03-16 17:04:54 -07001450}
1451
Thomas Gleixner5c2837f2010-09-28 17:15:11 +02001452void dmar_msi_mask(struct irq_data *data)
Suresh Siddha0ac24912009-03-16 17:04:54 -07001453{
1454 unsigned long flag;
Thomas Gleixnerdced35a2011-03-28 17:49:12 +02001455 struct intel_iommu *iommu = irq_data_get_irq_handler_data(data);
Suresh Siddha0ac24912009-03-16 17:04:54 -07001456
1457 /* mask it */
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001458 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Suresh Siddha0ac24912009-03-16 17:04:54 -07001459 writel(DMA_FECTL_IM, iommu->reg + DMAR_FECTL_REG);
1460 /* Read a reg to force flush the post write */
1461 readl(iommu->reg + DMAR_FECTL_REG);
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001462 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Suresh Siddha0ac24912009-03-16 17:04:54 -07001463}
1464
1465void dmar_msi_write(int irq, struct msi_msg *msg)
1466{
Thomas Gleixnerdced35a2011-03-28 17:49:12 +02001467 struct intel_iommu *iommu = irq_get_handler_data(irq);
Suresh Siddha0ac24912009-03-16 17:04:54 -07001468 unsigned long flag;
1469
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001470 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Suresh Siddha0ac24912009-03-16 17:04:54 -07001471 writel(msg->data, iommu->reg + DMAR_FEDATA_REG);
1472 writel(msg->address_lo, iommu->reg + DMAR_FEADDR_REG);
1473 writel(msg->address_hi, iommu->reg + DMAR_FEUADDR_REG);
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001474 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Suresh Siddha0ac24912009-03-16 17:04:54 -07001475}
1476
1477void dmar_msi_read(int irq, struct msi_msg *msg)
1478{
Thomas Gleixnerdced35a2011-03-28 17:49:12 +02001479 struct intel_iommu *iommu = irq_get_handler_data(irq);
Suresh Siddha0ac24912009-03-16 17:04:54 -07001480 unsigned long flag;
1481
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001482 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Suresh Siddha0ac24912009-03-16 17:04:54 -07001483 msg->data = readl(iommu->reg + DMAR_FEDATA_REG);
1484 msg->address_lo = readl(iommu->reg + DMAR_FEADDR_REG);
1485 msg->address_hi = readl(iommu->reg + DMAR_FEUADDR_REG);
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001486 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Suresh Siddha0ac24912009-03-16 17:04:54 -07001487}
1488
1489static int dmar_fault_do_one(struct intel_iommu *iommu, int type,
1490 u8 fault_reason, u16 source_id, unsigned long long addr)
1491{
1492 const char *reason;
Suresh Siddha9d783ba2009-03-16 17:04:55 -07001493 int fault_type;
Suresh Siddha0ac24912009-03-16 17:04:54 -07001494
Suresh Siddha9d783ba2009-03-16 17:04:55 -07001495 reason = dmar_get_fault_reason(fault_reason, &fault_type);
Suresh Siddha0ac24912009-03-16 17:04:54 -07001496
Suresh Siddha9d783ba2009-03-16 17:04:55 -07001497 if (fault_type == INTR_REMAP)
Donald Dutilebf947fcb2012-06-04 17:29:01 -04001498 pr_err("INTR-REMAP: Request device [[%02x:%02x.%d] "
Suresh Siddha9d783ba2009-03-16 17:04:55 -07001499 "fault index %llx\n"
1500 "INTR-REMAP:[fault reason %02d] %s\n",
1501 (source_id >> 8), PCI_SLOT(source_id & 0xFF),
1502 PCI_FUNC(source_id & 0xFF), addr >> 48,
1503 fault_reason, reason);
1504 else
Donald Dutilebf947fcb2012-06-04 17:29:01 -04001505 pr_err("DMAR:[%s] Request device [%02x:%02x.%d] "
Suresh Siddha9d783ba2009-03-16 17:04:55 -07001506 "fault addr %llx \n"
1507 "DMAR:[fault reason %02d] %s\n",
1508 (type ? "DMA Read" : "DMA Write"),
1509 (source_id >> 8), PCI_SLOT(source_id & 0xFF),
1510 PCI_FUNC(source_id & 0xFF), addr, fault_reason, reason);
Suresh Siddha0ac24912009-03-16 17:04:54 -07001511 return 0;
1512}
1513
1514#define PRIMARY_FAULT_REG_LEN (16)
Suresh Siddha1531a6a2009-03-16 17:04:57 -07001515irqreturn_t dmar_fault(int irq, void *dev_id)
Suresh Siddha0ac24912009-03-16 17:04:54 -07001516{
1517 struct intel_iommu *iommu = dev_id;
1518 int reg, fault_index;
1519 u32 fault_status;
1520 unsigned long flag;
1521
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001522 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Suresh Siddha0ac24912009-03-16 17:04:54 -07001523 fault_status = readl(iommu->reg + DMAR_FSTS_REG);
Suresh Siddha9d783ba2009-03-16 17:04:55 -07001524 if (fault_status)
Donald Dutilebf947fcb2012-06-04 17:29:01 -04001525 pr_err("DRHD: handling fault status reg %x\n", fault_status);
Suresh Siddha0ac24912009-03-16 17:04:54 -07001526
1527 /* TBD: ignore advanced fault log currently */
1528 if (!(fault_status & DMA_FSTS_PPF))
Li, Zhen-Huabd5cdad2013-03-25 16:20:52 +08001529 goto unlock_exit;
Suresh Siddha0ac24912009-03-16 17:04:54 -07001530
1531 fault_index = dma_fsts_fault_record_index(fault_status);
1532 reg = cap_fault_reg_offset(iommu->cap);
1533 while (1) {
1534 u8 fault_reason;
1535 u16 source_id;
1536 u64 guest_addr;
1537 int type;
1538 u32 data;
1539
1540 /* highest 32 bits */
1541 data = readl(iommu->reg + reg +
1542 fault_index * PRIMARY_FAULT_REG_LEN + 12);
1543 if (!(data & DMA_FRCD_F))
1544 break;
1545
1546 fault_reason = dma_frcd_fault_reason(data);
1547 type = dma_frcd_type(data);
1548
1549 data = readl(iommu->reg + reg +
1550 fault_index * PRIMARY_FAULT_REG_LEN + 8);
1551 source_id = dma_frcd_source_id(data);
1552
1553 guest_addr = dmar_readq(iommu->reg + reg +
1554 fault_index * PRIMARY_FAULT_REG_LEN);
1555 guest_addr = dma_frcd_page_addr(guest_addr);
1556 /* clear the fault */
1557 writel(DMA_FRCD_F, iommu->reg + reg +
1558 fault_index * PRIMARY_FAULT_REG_LEN + 12);
1559
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001560 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Suresh Siddha0ac24912009-03-16 17:04:54 -07001561
1562 dmar_fault_do_one(iommu, type, fault_reason,
1563 source_id, guest_addr);
1564
1565 fault_index++;
Troy Heber8211a7b2009-08-19 15:26:11 -06001566 if (fault_index >= cap_num_fault_regs(iommu->cap))
Suresh Siddha0ac24912009-03-16 17:04:54 -07001567 fault_index = 0;
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001568 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Suresh Siddha0ac24912009-03-16 17:04:54 -07001569 }
Suresh Siddha0ac24912009-03-16 17:04:54 -07001570
Li, Zhen-Huabd5cdad2013-03-25 16:20:52 +08001571 writel(DMA_FSTS_PFO | DMA_FSTS_PPF, iommu->reg + DMAR_FSTS_REG);
1572
1573unlock_exit:
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001574 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Suresh Siddha0ac24912009-03-16 17:04:54 -07001575 return IRQ_HANDLED;
1576}
1577
1578int dmar_set_interrupt(struct intel_iommu *iommu)
1579{
1580 int irq, ret;
1581
Suresh Siddha9d783ba2009-03-16 17:04:55 -07001582 /*
1583 * Check if the fault interrupt is already initialized.
1584 */
1585 if (iommu->irq)
1586 return 0;
1587
Thomas Gleixnera553b142014-05-07 15:44:11 +00001588 irq = dmar_alloc_hwirq();
Thomas Gleixneraa5125a2014-05-07 15:44:10 +00001589 if (irq <= 0) {
Donald Dutilebf947fcb2012-06-04 17:29:01 -04001590 pr_err("IOMMU: no free vectors\n");
Suresh Siddha0ac24912009-03-16 17:04:54 -07001591 return -EINVAL;
1592 }
1593
Thomas Gleixnerdced35a2011-03-28 17:49:12 +02001594 irq_set_handler_data(irq, iommu);
Suresh Siddha0ac24912009-03-16 17:04:54 -07001595 iommu->irq = irq;
1596
1597 ret = arch_setup_dmar_msi(irq);
1598 if (ret) {
Thomas Gleixnerdced35a2011-03-28 17:49:12 +02001599 irq_set_handler_data(irq, NULL);
Suresh Siddha0ac24912009-03-16 17:04:54 -07001600 iommu->irq = 0;
Thomas Gleixnera553b142014-05-07 15:44:11 +00001601 dmar_free_hwirq(irq);
Chris Wrightdd726432009-05-13 15:55:52 -07001602 return ret;
Suresh Siddha0ac24912009-03-16 17:04:54 -07001603 }
1604
Thomas Gleixner477694e2011-07-19 16:25:42 +02001605 ret = request_irq(irq, dmar_fault, IRQF_NO_THREAD, iommu->name, iommu);
Suresh Siddha0ac24912009-03-16 17:04:54 -07001606 if (ret)
Donald Dutilebf947fcb2012-06-04 17:29:01 -04001607 pr_err("IOMMU: can't request irq\n");
Suresh Siddha0ac24912009-03-16 17:04:54 -07001608 return ret;
1609}
Suresh Siddha9d783ba2009-03-16 17:04:55 -07001610
1611int __init enable_drhd_fault_handling(void)
1612{
1613 struct dmar_drhd_unit *drhd;
Jiang Liu7c919772014-01-06 14:18:18 +08001614 struct intel_iommu *iommu;
Suresh Siddha9d783ba2009-03-16 17:04:55 -07001615
1616 /*
1617 * Enable fault control interrupt.
1618 */
Jiang Liu7c919772014-01-06 14:18:18 +08001619 for_each_iommu(iommu, drhd) {
Li, Zhen-Huabd5cdad2013-03-25 16:20:52 +08001620 u32 fault_status;
Jiang Liu7c919772014-01-06 14:18:18 +08001621 int ret = dmar_set_interrupt(iommu);
Suresh Siddha9d783ba2009-03-16 17:04:55 -07001622
1623 if (ret) {
Donald Dutilee9071b02012-06-08 17:13:11 -04001624 pr_err("DRHD %Lx: failed to enable fault, interrupt, ret %d\n",
Suresh Siddha9d783ba2009-03-16 17:04:55 -07001625 (unsigned long long)drhd->reg_base_addr, ret);
1626 return -1;
1627 }
Suresh Siddha7f99d942010-11-30 22:22:29 -08001628
1629 /*
1630 * Clear any previous faults.
1631 */
1632 dmar_fault(iommu->irq, iommu);
Li, Zhen-Huabd5cdad2013-03-25 16:20:52 +08001633 fault_status = readl(iommu->reg + DMAR_FSTS_REG);
1634 writel(fault_status, iommu->reg + DMAR_FSTS_REG);
Suresh Siddha9d783ba2009-03-16 17:04:55 -07001635 }
1636
1637 return 0;
1638}
Fenghua Yueb4a52b2009-03-27 14:22:43 -07001639
1640/*
1641 * Re-enable Queued Invalidation interface.
1642 */
1643int dmar_reenable_qi(struct intel_iommu *iommu)
1644{
1645 if (!ecap_qis(iommu->ecap))
1646 return -ENOENT;
1647
1648 if (!iommu->qi)
1649 return -ENOENT;
1650
1651 /*
1652 * First disable queued invalidation.
1653 */
1654 dmar_disable_qi(iommu);
1655 /*
1656 * Then enable queued invalidation again. Since there is no pending
1657 * invalidation requests now, it's safe to re-enable queued
1658 * invalidation.
1659 */
1660 __dmar_enable_qi(iommu);
1661
1662 return 0;
1663}
Youquan Song074835f2009-09-09 12:05:39 -04001664
1665/*
1666 * Check interrupt remapping support in DMAR table description.
1667 */
Luck, Tony0b8973a2009-12-16 22:59:29 +00001668int __init dmar_ir_support(void)
Youquan Song074835f2009-09-09 12:05:39 -04001669{
1670 struct acpi_table_dmar *dmar;
1671 dmar = (struct acpi_table_dmar *)dmar_tbl;
Arnaud Patard4f506e02010-03-25 18:02:58 +00001672 if (!dmar)
1673 return 0;
Youquan Song074835f2009-09-09 12:05:39 -04001674 return dmar->flags & 0x1;
1675}
Jiang Liu694835d2014-01-06 14:18:16 +08001676
Jiang Liua868e6b2014-01-06 14:18:20 +08001677static int __init dmar_free_unused_resources(void)
1678{
1679 struct dmar_drhd_unit *dmaru, *dmaru_n;
1680
1681 /* DMAR units are in use */
1682 if (irq_remapping_enabled || intel_iommu_enabled)
1683 return 0;
1684
Jiang Liu2e455282014-02-19 14:07:36 +08001685 if (dmar_dev_scope_status != 1 && !list_empty(&dmar_drhd_units))
1686 bus_unregister_notifier(&pci_bus_type, &dmar_pci_bus_nb);
Jiang Liu59ce0512014-02-19 14:07:35 +08001687
Jiang Liu3a5670e2014-02-19 14:07:33 +08001688 down_write(&dmar_global_lock);
Jiang Liua868e6b2014-01-06 14:18:20 +08001689 list_for_each_entry_safe(dmaru, dmaru_n, &dmar_drhd_units, list) {
1690 list_del(&dmaru->list);
1691 dmar_free_drhd(dmaru);
1692 }
Jiang Liu3a5670e2014-02-19 14:07:33 +08001693 up_write(&dmar_global_lock);
Jiang Liua868e6b2014-01-06 14:18:20 +08001694
1695 return 0;
1696}
1697
1698late_initcall(dmar_free_unused_resources);
Konrad Rzeszutek Wilk4db77ff2010-08-26 13:58:04 -04001699IOMMU_INIT_POST(detect_intel_iommu);