blob: f445c10df8dfd2beb53b8fb4628d7094d0e17b5a [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>
Suresh Siddha8a8f4222012-03-30 11:47:08 -070041#include <asm/irq_remapping.h>
Konrad Rzeszutek Wilk4db77ff2010-08-26 13:58:04 -040042#include <asm/iommu_table.h>
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -070043
Joerg Roedel078e1ee2012-09-26 12:44:43 +020044#include "irq_remapping.h"
45
Jiang Liu3a5670e2014-02-19 14:07:33 +080046/*
47 * Assumptions:
48 * 1) The hotplug framework guarentees that DMAR unit will be hot-added
49 * before IO devices managed by that unit.
50 * 2) The hotplug framework guarantees that DMAR unit will be hot-removed
51 * after IO devices managed by that unit.
52 * 3) Hotplug events are rare.
53 *
54 * Locking rules for DMA and interrupt remapping related global data structures:
55 * 1) Use dmar_global_lock in process context
56 * 2) Use RCU in interrupt context
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -070057 */
Jiang Liu3a5670e2014-02-19 14:07:33 +080058DECLARE_RWSEM(dmar_global_lock);
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -070059LIST_HEAD(dmar_drhd_units);
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -070060
Suresh Siddha41750d32011-08-23 17:05:18 -070061struct acpi_table_header * __initdata dmar_tbl;
Yinghai Lu8e1568f2009-02-11 01:06:59 -080062static acpi_size dmar_tbl_size;
Jiang Liu2e455282014-02-19 14:07:36 +080063static int dmar_dev_scope_status = 1;
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -070064
Jiang Liu694835d2014-01-06 14:18:16 +080065static int alloc_iommu(struct dmar_drhd_unit *drhd);
Jiang Liua868e6b2014-01-06 14:18:20 +080066static void free_iommu(struct intel_iommu *iommu);
Jiang Liu694835d2014-01-06 14:18:16 +080067
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -070068static void __init dmar_register_drhd_unit(struct dmar_drhd_unit *drhd)
69{
70 /*
71 * add INCLUDE_ALL at the tail, so scan the list will find it at
72 * the very end.
73 */
74 if (drhd->include_all)
Jiang Liu0e242612014-02-19 14:07:34 +080075 list_add_tail_rcu(&drhd->list, &dmar_drhd_units);
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -070076 else
Jiang Liu0e242612014-02-19 14:07:34 +080077 list_add_rcu(&drhd->list, &dmar_drhd_units);
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -070078}
79
Jiang Liubb3a6b72014-02-19 14:07:24 +080080void *dmar_alloc_dev_scope(void *start, void *end, int *cnt)
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -070081{
82 struct acpi_dmar_device_scope *scope;
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -070083
84 *cnt = 0;
85 while (start < end) {
86 scope = start;
David Woodhouse07cb52f2014-03-07 14:39:27 +000087 if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_ACPI ||
88 scope->entry_type == ACPI_DMAR_SCOPE_TYPE_ENDPOINT ||
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -070089 scope->entry_type == ACPI_DMAR_SCOPE_TYPE_BRIDGE)
90 (*cnt)++;
Linn Crosettoae3e7f32013-04-23 12:26:45 -060091 else if (scope->entry_type != ACPI_DMAR_SCOPE_TYPE_IOAPIC &&
92 scope->entry_type != ACPI_DMAR_SCOPE_TYPE_HPET) {
Donald Dutilee9071b02012-06-08 17:13:11 -040093 pr_warn("Unsupported device scope\n");
Yinghai Lu5715f0f2010-04-08 19:58:22 +010094 }
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -070095 start += scope->length;
96 }
97 if (*cnt == 0)
Jiang Liubb3a6b72014-02-19 14:07:24 +080098 return NULL;
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -070099
David Woodhouse832bd852014-03-07 15:08:36 +0000100 return kcalloc(*cnt, sizeof(struct dmar_dev_scope), GFP_KERNEL);
Jiang Liubb3a6b72014-02-19 14:07:24 +0800101}
102
David Woodhouse832bd852014-03-07 15:08:36 +0000103void dmar_free_dev_scope(struct dmar_dev_scope **devices, int *cnt)
Jiang Liuada4d4b2014-01-06 14:18:09 +0800104{
Jiang Liub683b232014-02-19 14:07:32 +0800105 int i;
David Woodhouse832bd852014-03-07 15:08:36 +0000106 struct device *tmp_dev;
Jiang Liub683b232014-02-19 14:07:32 +0800107
Jiang Liuada4d4b2014-01-06 14:18:09 +0800108 if (*devices && *cnt) {
Jiang Liub683b232014-02-19 14:07:32 +0800109 for_each_active_dev_scope(*devices, *cnt, i, tmp_dev)
David Woodhouse832bd852014-03-07 15:08:36 +0000110 put_device(tmp_dev);
Jiang Liuada4d4b2014-01-06 14:18:09 +0800111 kfree(*devices);
Jiang Liuada4d4b2014-01-06 14:18:09 +0800112 }
Jiang Liu0e242612014-02-19 14:07:34 +0800113
114 *devices = NULL;
115 *cnt = 0;
Jiang Liuada4d4b2014-01-06 14:18:09 +0800116}
117
Jiang Liu59ce0512014-02-19 14:07:35 +0800118/* Optimize out kzalloc()/kfree() for normal cases */
119static char dmar_pci_notify_info_buf[64];
120
121static struct dmar_pci_notify_info *
122dmar_alloc_pci_notify_info(struct pci_dev *dev, unsigned long event)
123{
124 int level = 0;
125 size_t size;
126 struct pci_dev *tmp;
127 struct dmar_pci_notify_info *info;
128
129 BUG_ON(dev->is_virtfn);
130
131 /* Only generate path[] for device addition event */
132 if (event == BUS_NOTIFY_ADD_DEVICE)
133 for (tmp = dev; tmp; tmp = tmp->bus->self)
134 level++;
135
136 size = sizeof(*info) + level * sizeof(struct acpi_dmar_pci_path);
137 if (size <= sizeof(dmar_pci_notify_info_buf)) {
138 info = (struct dmar_pci_notify_info *)dmar_pci_notify_info_buf;
139 } else {
140 info = kzalloc(size, GFP_KERNEL);
141 if (!info) {
142 pr_warn("Out of memory when allocating notify_info "
143 "for %s.\n", pci_name(dev));
Jiang Liu2e455282014-02-19 14:07:36 +0800144 if (dmar_dev_scope_status == 0)
145 dmar_dev_scope_status = -ENOMEM;
Jiang Liu59ce0512014-02-19 14:07:35 +0800146 return NULL;
147 }
148 }
149
150 info->event = event;
151 info->dev = dev;
152 info->seg = pci_domain_nr(dev->bus);
153 info->level = level;
154 if (event == BUS_NOTIFY_ADD_DEVICE) {
155 for (tmp = dev, level--; tmp; tmp = tmp->bus->self) {
156 info->path[level].device = PCI_SLOT(tmp->devfn);
157 info->path[level].function = PCI_FUNC(tmp->devfn);
158 if (pci_is_root_bus(tmp->bus))
159 info->bus = tmp->bus->number;
160 }
161 }
162
163 return info;
164}
165
166static inline void dmar_free_pci_notify_info(struct dmar_pci_notify_info *info)
167{
168 if ((void *)info != dmar_pci_notify_info_buf)
169 kfree(info);
170}
171
172static bool dmar_match_pci_path(struct dmar_pci_notify_info *info, int bus,
173 struct acpi_dmar_pci_path *path, int count)
174{
175 int i;
176
177 if (info->bus != bus)
178 return false;
179 if (info->level != count)
180 return false;
181
182 for (i = 0; i < count; i++) {
183 if (path[i].device != info->path[i].device ||
184 path[i].function != info->path[i].function)
185 return false;
186 }
187
188 return true;
189}
190
191/* Return: > 0 if match found, 0 if no match found, < 0 if error happens */
192int dmar_insert_dev_scope(struct dmar_pci_notify_info *info,
193 void *start, void*end, u16 segment,
David Woodhouse832bd852014-03-07 15:08:36 +0000194 struct dmar_dev_scope *devices,
195 int devices_cnt)
Jiang Liu59ce0512014-02-19 14:07:35 +0800196{
197 int i, level;
David Woodhouse832bd852014-03-07 15:08:36 +0000198 struct device *tmp, *dev = &info->dev->dev;
Jiang Liu59ce0512014-02-19 14:07:35 +0800199 struct acpi_dmar_device_scope *scope;
200 struct acpi_dmar_pci_path *path;
201
202 if (segment != info->seg)
203 return 0;
204
205 for (; start < end; start += scope->length) {
206 scope = start;
207 if (scope->entry_type != ACPI_DMAR_SCOPE_TYPE_ENDPOINT &&
208 scope->entry_type != ACPI_DMAR_SCOPE_TYPE_BRIDGE)
209 continue;
210
211 path = (struct acpi_dmar_pci_path *)(scope + 1);
212 level = (scope->length - sizeof(*scope)) / sizeof(*path);
213 if (!dmar_match_pci_path(info, scope->bus, path, level))
214 continue;
215
216 if ((scope->entry_type == ACPI_DMAR_SCOPE_TYPE_ENDPOINT) ^
David Woodhouse832bd852014-03-07 15:08:36 +0000217 (info->dev->hdr_type == PCI_HEADER_TYPE_NORMAL)) {
Jiang Liu59ce0512014-02-19 14:07:35 +0800218 pr_warn("Device scope type does not match for %s\n",
David Woodhouse832bd852014-03-07 15:08:36 +0000219 pci_name(info->dev));
Jiang Liu59ce0512014-02-19 14:07:35 +0800220 return -EINVAL;
221 }
222
223 for_each_dev_scope(devices, devices_cnt, i, tmp)
224 if (tmp == NULL) {
David Woodhouse832bd852014-03-07 15:08:36 +0000225 devices[i].bus = info->dev->bus->number;
226 devices[i].devfn = info->dev->devfn;
227 rcu_assign_pointer(devices[i].dev,
228 get_device(dev));
Jiang Liu59ce0512014-02-19 14:07:35 +0800229 return 1;
230 }
231 BUG_ON(i >= devices_cnt);
232 }
233
234 return 0;
235}
236
237int dmar_remove_dev_scope(struct dmar_pci_notify_info *info, u16 segment,
David Woodhouse832bd852014-03-07 15:08:36 +0000238 struct dmar_dev_scope *devices, int count)
Jiang Liu59ce0512014-02-19 14:07:35 +0800239{
240 int index;
David Woodhouse832bd852014-03-07 15:08:36 +0000241 struct device *tmp;
Jiang Liu59ce0512014-02-19 14:07:35 +0800242
243 if (info->seg != segment)
244 return 0;
245
246 for_each_active_dev_scope(devices, count, index, tmp)
David Woodhouse832bd852014-03-07 15:08:36 +0000247 if (tmp == &info->dev->dev) {
248 rcu_assign_pointer(devices[index].dev, NULL);
Jiang Liu59ce0512014-02-19 14:07:35 +0800249 synchronize_rcu();
David Woodhouse832bd852014-03-07 15:08:36 +0000250 put_device(tmp);
Jiang Liu59ce0512014-02-19 14:07:35 +0800251 return 1;
252 }
253
254 return 0;
255}
256
257static int dmar_pci_bus_add_dev(struct dmar_pci_notify_info *info)
258{
259 int ret = 0;
260 struct dmar_drhd_unit *dmaru;
261 struct acpi_dmar_hardware_unit *drhd;
262
263 for_each_drhd_unit(dmaru) {
264 if (dmaru->include_all)
265 continue;
266
267 drhd = container_of(dmaru->hdr,
268 struct acpi_dmar_hardware_unit, header);
269 ret = dmar_insert_dev_scope(info, (void *)(drhd + 1),
270 ((void *)drhd) + drhd->header.length,
271 dmaru->segment,
272 dmaru->devices, dmaru->devices_cnt);
273 if (ret != 0)
274 break;
275 }
276 if (ret >= 0)
277 ret = dmar_iommu_notify_scope_dev(info);
Jiang Liu2e455282014-02-19 14:07:36 +0800278 if (ret < 0 && dmar_dev_scope_status == 0)
279 dmar_dev_scope_status = ret;
Jiang Liu59ce0512014-02-19 14:07:35 +0800280
281 return ret;
282}
283
284static void dmar_pci_bus_del_dev(struct dmar_pci_notify_info *info)
285{
286 struct dmar_drhd_unit *dmaru;
287
288 for_each_drhd_unit(dmaru)
289 if (dmar_remove_dev_scope(info, dmaru->segment,
290 dmaru->devices, dmaru->devices_cnt))
291 break;
292 dmar_iommu_notify_scope_dev(info);
293}
294
295static int dmar_pci_bus_notifier(struct notifier_block *nb,
296 unsigned long action, void *data)
297{
298 struct pci_dev *pdev = to_pci_dev(data);
299 struct dmar_pci_notify_info *info;
300
301 /* Only care about add/remove events for physical functions */
302 if (pdev->is_virtfn)
303 return NOTIFY_DONE;
304 if (action != BUS_NOTIFY_ADD_DEVICE && action != BUS_NOTIFY_DEL_DEVICE)
305 return NOTIFY_DONE;
306
307 info = dmar_alloc_pci_notify_info(pdev, action);
308 if (!info)
309 return NOTIFY_DONE;
310
311 down_write(&dmar_global_lock);
312 if (action == BUS_NOTIFY_ADD_DEVICE)
313 dmar_pci_bus_add_dev(info);
314 else if (action == BUS_NOTIFY_DEL_DEVICE)
315 dmar_pci_bus_del_dev(info);
316 up_write(&dmar_global_lock);
317
318 dmar_free_pci_notify_info(info);
319
320 return NOTIFY_OK;
321}
322
323static struct notifier_block dmar_pci_bus_nb = {
324 .notifier_call = dmar_pci_bus_notifier,
325 .priority = INT_MIN,
326};
327
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -0700328/**
329 * dmar_parse_one_drhd - parses exactly one DMA remapping hardware definition
330 * structure which uniquely represent one DMA remapping hardware unit
331 * present in the platform
332 */
333static int __init
334dmar_parse_one_drhd(struct acpi_dmar_header *header)
335{
336 struct acpi_dmar_hardware_unit *drhd;
337 struct dmar_drhd_unit *dmaru;
338 int ret = 0;
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -0700339
David Woodhousee523b382009-04-10 22:27:48 -0700340 drhd = (struct acpi_dmar_hardware_unit *)header;
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -0700341 dmaru = kzalloc(sizeof(*dmaru), GFP_KERNEL);
342 if (!dmaru)
343 return -ENOMEM;
344
Suresh Siddha1886e8a2008-07-10 11:16:37 -0700345 dmaru->hdr = header;
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -0700346 dmaru->reg_base_addr = drhd->address;
David Woodhouse276dbf992009-04-04 01:45:37 +0100347 dmaru->segment = drhd->segment;
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -0700348 dmaru->include_all = drhd->flags & 0x1; /* BIT0: INCLUDE_ALL */
David Woodhouse07cb52f2014-03-07 14:39:27 +0000349 dmaru->devices = dmar_alloc_dev_scope((void *)(drhd + 1),
350 ((void *)drhd) + drhd->header.length,
351 &dmaru->devices_cnt);
352 if (dmaru->devices_cnt && dmaru->devices == NULL) {
353 kfree(dmaru);
354 return -ENOMEM;
Jiang Liu2e455282014-02-19 14:07:36 +0800355 }
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -0700356
Suresh Siddha1886e8a2008-07-10 11:16:37 -0700357 ret = alloc_iommu(dmaru);
358 if (ret) {
David Woodhouse07cb52f2014-03-07 14:39:27 +0000359 dmar_free_dev_scope(&dmaru->devices,
360 &dmaru->devices_cnt);
Suresh Siddha1886e8a2008-07-10 11:16:37 -0700361 kfree(dmaru);
362 return ret;
363 }
364 dmar_register_drhd_unit(dmaru);
365 return 0;
366}
367
Jiang Liua868e6b2014-01-06 14:18:20 +0800368static void dmar_free_drhd(struct dmar_drhd_unit *dmaru)
369{
370 if (dmaru->devices && dmaru->devices_cnt)
371 dmar_free_dev_scope(&dmaru->devices, &dmaru->devices_cnt);
372 if (dmaru->iommu)
373 free_iommu(dmaru->iommu);
374 kfree(dmaru);
375}
376
David Woodhousee625b4a2014-03-07 14:34:38 +0000377static int __init dmar_parse_one_andd(struct acpi_dmar_header *header)
378{
379 struct acpi_dmar_andd *andd = (void *)header;
380
381 /* Check for NUL termination within the designated length */
382 if (strnlen(andd->object_name, header->length - 8) == header->length - 8) {
383 WARN_TAINT(1, TAINT_FIRMWARE_WORKAROUND,
384 "Your BIOS is broken; ANDD object name is not NUL-terminated\n"
385 "BIOS vendor: %s; Ver: %s; Product Version: %s\n",
386 dmi_get_system_info(DMI_BIOS_VENDOR),
387 dmi_get_system_info(DMI_BIOS_VERSION),
388 dmi_get_system_info(DMI_PRODUCT_VERSION));
389 return -EINVAL;
390 }
391 pr_info("ANDD device: %x name: %s\n", andd->device_number,
392 andd->object_name);
393
394 return 0;
395}
396
David Woodhouseaa697072009-10-07 12:18:00 +0100397#ifdef CONFIG_ACPI_NUMA
Suresh Siddhaee34b322009-10-02 11:01:21 -0700398static int __init
399dmar_parse_one_rhsa(struct acpi_dmar_header *header)
400{
401 struct acpi_dmar_rhsa *rhsa;
402 struct dmar_drhd_unit *drhd;
403
404 rhsa = (struct acpi_dmar_rhsa *)header;
David Woodhouseaa697072009-10-07 12:18:00 +0100405 for_each_drhd_unit(drhd) {
Suresh Siddhaee34b322009-10-02 11:01:21 -0700406 if (drhd->reg_base_addr == rhsa->base_address) {
407 int node = acpi_map_pxm_to_node(rhsa->proximity_domain);
408
409 if (!node_online(node))
410 node = -1;
411 drhd->iommu->node = node;
David Woodhouseaa697072009-10-07 12:18:00 +0100412 return 0;
413 }
Suresh Siddhaee34b322009-10-02 11:01:21 -0700414 }
Ben Hutchingsfd0c8892010-04-03 19:38:43 +0100415 WARN_TAINT(
416 1, TAINT_FIRMWARE_WORKAROUND,
417 "Your BIOS is broken; RHSA refers to non-existent DMAR unit at %llx\n"
418 "BIOS vendor: %s; Ver: %s; Product Version: %s\n",
419 drhd->reg_base_addr,
420 dmi_get_system_info(DMI_BIOS_VENDOR),
421 dmi_get_system_info(DMI_BIOS_VERSION),
422 dmi_get_system_info(DMI_PRODUCT_VERSION));
Suresh Siddhaee34b322009-10-02 11:01:21 -0700423
David Woodhouseaa697072009-10-07 12:18:00 +0100424 return 0;
Suresh Siddhaee34b322009-10-02 11:01:21 -0700425}
David Woodhouseaa697072009-10-07 12:18:00 +0100426#endif
Suresh Siddhaee34b322009-10-02 11:01:21 -0700427
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -0700428static void __init
429dmar_table_print_dmar_entry(struct acpi_dmar_header *header)
430{
431 struct acpi_dmar_hardware_unit *drhd;
432 struct acpi_dmar_reserved_memory *rmrr;
Yu Zhaoaa5d2b52009-05-18 13:51:34 +0800433 struct acpi_dmar_atsr *atsr;
Roland Dreier17b60972009-09-24 12:14:00 -0700434 struct acpi_dmar_rhsa *rhsa;
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -0700435
436 switch (header->type) {
437 case ACPI_DMAR_TYPE_HARDWARE_UNIT:
Yu Zhaoaa5d2b52009-05-18 13:51:34 +0800438 drhd = container_of(header, struct acpi_dmar_hardware_unit,
439 header);
Donald Dutilee9071b02012-06-08 17:13:11 -0400440 pr_info("DRHD base: %#016Lx flags: %#x\n",
Yu Zhaoaa5d2b52009-05-18 13:51:34 +0800441 (unsigned long long)drhd->address, drhd->flags);
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -0700442 break;
443 case ACPI_DMAR_TYPE_RESERVED_MEMORY:
Yu Zhaoaa5d2b52009-05-18 13:51:34 +0800444 rmrr = container_of(header, struct acpi_dmar_reserved_memory,
445 header);
Donald Dutilee9071b02012-06-08 17:13:11 -0400446 pr_info("RMRR base: %#016Lx end: %#016Lx\n",
Fenghua Yu5b6985c2008-10-16 18:02:32 -0700447 (unsigned long long)rmrr->base_address,
448 (unsigned long long)rmrr->end_address);
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -0700449 break;
Yu Zhaoaa5d2b52009-05-18 13:51:34 +0800450 case ACPI_DMAR_TYPE_ATSR:
451 atsr = container_of(header, struct acpi_dmar_atsr, header);
Donald Dutilee9071b02012-06-08 17:13:11 -0400452 pr_info("ATSR flags: %#x\n", atsr->flags);
Yu Zhaoaa5d2b52009-05-18 13:51:34 +0800453 break;
Roland Dreier17b60972009-09-24 12:14:00 -0700454 case ACPI_DMAR_HARDWARE_AFFINITY:
455 rhsa = container_of(header, struct acpi_dmar_rhsa, header);
Donald Dutilee9071b02012-06-08 17:13:11 -0400456 pr_info("RHSA base: %#016Lx proximity domain: %#x\n",
Roland Dreier17b60972009-09-24 12:14:00 -0700457 (unsigned long long)rhsa->base_address,
458 rhsa->proximity_domain);
459 break;
David Woodhousee625b4a2014-03-07 14:34:38 +0000460 case ACPI_DMAR_TYPE_ANDD:
461 /* We don't print this here because we need to sanity-check
462 it first. So print it in dmar_parse_one_andd() instead. */
463 break;
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -0700464 }
465}
466
Yinghai Luf6dd5c32008-09-03 16:58:32 -0700467/**
468 * dmar_table_detect - checks to see if the platform supports DMAR devices
469 */
470static int __init dmar_table_detect(void)
471{
472 acpi_status status = AE_OK;
473
474 /* if we could find DMAR table, then there are DMAR devices */
Yinghai Lu8e1568f2009-02-11 01:06:59 -0800475 status = acpi_get_table_with_size(ACPI_SIG_DMAR, 0,
476 (struct acpi_table_header **)&dmar_tbl,
477 &dmar_tbl_size);
Yinghai Luf6dd5c32008-09-03 16:58:32 -0700478
479 if (ACPI_SUCCESS(status) && !dmar_tbl) {
Donald Dutilee9071b02012-06-08 17:13:11 -0400480 pr_warn("Unable to map DMAR\n");
Yinghai Luf6dd5c32008-09-03 16:58:32 -0700481 status = AE_NOT_FOUND;
482 }
483
484 return (ACPI_SUCCESS(status) ? 1 : 0);
485}
Suresh Siddhaaaa9d1d2008-07-10 11:16:38 -0700486
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -0700487/**
488 * parse_dmar_table - parses the DMA reporting table
489 */
490static int __init
491parse_dmar_table(void)
492{
493 struct acpi_table_dmar *dmar;
494 struct acpi_dmar_header *entry_header;
495 int ret = 0;
Li, Zhen-Hua7cef3342013-05-20 15:57:32 +0800496 int drhd_count = 0;
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -0700497
Yinghai Luf6dd5c32008-09-03 16:58:32 -0700498 /*
499 * Do it again, earlier dmar_tbl mapping could be mapped with
500 * fixed map.
501 */
502 dmar_table_detect();
503
Joseph Cihulaa59b50e2009-06-30 19:31:10 -0700504 /*
505 * ACPI tables may not be DMA protected by tboot, so use DMAR copy
506 * SINIT saved in SinitMleData in TXT heap (which is DMA protected)
507 */
508 dmar_tbl = tboot_get_dmar_table(dmar_tbl);
509
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -0700510 dmar = (struct acpi_table_dmar *)dmar_tbl;
511 if (!dmar)
512 return -ENODEV;
513
Fenghua Yu5b6985c2008-10-16 18:02:32 -0700514 if (dmar->width < PAGE_SHIFT - 1) {
Donald Dutilee9071b02012-06-08 17:13:11 -0400515 pr_warn("Invalid DMAR haw\n");
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -0700516 return -EINVAL;
517 }
518
Donald Dutilee9071b02012-06-08 17:13:11 -0400519 pr_info("Host address width %d\n", dmar->width + 1);
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -0700520
521 entry_header = (struct acpi_dmar_header *)(dmar + 1);
522 while (((unsigned long)entry_header) <
523 (((unsigned long)dmar) + dmar_tbl->length)) {
Tony Battersby084eb962009-02-11 13:24:19 -0800524 /* Avoid looping forever on bad ACPI tables */
525 if (entry_header->length == 0) {
Donald Dutilee9071b02012-06-08 17:13:11 -0400526 pr_warn("Invalid 0-length structure\n");
Tony Battersby084eb962009-02-11 13:24:19 -0800527 ret = -EINVAL;
528 break;
529 }
530
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -0700531 dmar_table_print_dmar_entry(entry_header);
532
533 switch (entry_header->type) {
534 case ACPI_DMAR_TYPE_HARDWARE_UNIT:
Li, Zhen-Hua7cef3342013-05-20 15:57:32 +0800535 drhd_count++;
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -0700536 ret = dmar_parse_one_drhd(entry_header);
537 break;
538 case ACPI_DMAR_TYPE_RESERVED_MEMORY:
539 ret = dmar_parse_one_rmrr(entry_header);
540 break;
Yu Zhaoaa5d2b52009-05-18 13:51:34 +0800541 case ACPI_DMAR_TYPE_ATSR:
Yu Zhaoaa5d2b52009-05-18 13:51:34 +0800542 ret = dmar_parse_one_atsr(entry_header);
Yu Zhaoaa5d2b52009-05-18 13:51:34 +0800543 break;
Roland Dreier17b60972009-09-24 12:14:00 -0700544 case ACPI_DMAR_HARDWARE_AFFINITY:
David Woodhouseaa697072009-10-07 12:18:00 +0100545#ifdef CONFIG_ACPI_NUMA
Suresh Siddhaee34b322009-10-02 11:01:21 -0700546 ret = dmar_parse_one_rhsa(entry_header);
David Woodhouseaa697072009-10-07 12:18:00 +0100547#endif
Roland Dreier17b60972009-09-24 12:14:00 -0700548 break;
David Woodhousee625b4a2014-03-07 14:34:38 +0000549 case ACPI_DMAR_TYPE_ANDD:
550 ret = dmar_parse_one_andd(entry_header);
551 break;
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -0700552 default:
Donald Dutilee9071b02012-06-08 17:13:11 -0400553 pr_warn("Unknown DMAR structure type %d\n",
Roland Dreier4de75cf2009-09-24 01:01:29 +0100554 entry_header->type);
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -0700555 ret = 0; /* for forward compatibility */
556 break;
557 }
558 if (ret)
559 break;
560
561 entry_header = ((void *)entry_header + entry_header->length);
562 }
Li, Zhen-Hua7cef3342013-05-20 15:57:32 +0800563 if (drhd_count == 0)
564 pr_warn(FW_BUG "No DRHD structure found in DMAR table\n");
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -0700565 return ret;
566}
567
David Woodhouse832bd852014-03-07 15:08:36 +0000568static int dmar_pci_device_match(struct dmar_dev_scope devices[],
569 int cnt, struct pci_dev *dev)
Suresh Siddhae61d98d2008-07-10 11:16:35 -0700570{
571 int index;
David Woodhouse832bd852014-03-07 15:08:36 +0000572 struct device *tmp;
Suresh Siddhae61d98d2008-07-10 11:16:35 -0700573
574 while (dev) {
Jiang Liub683b232014-02-19 14:07:32 +0800575 for_each_active_dev_scope(devices, cnt, index, tmp)
David Woodhouse832bd852014-03-07 15:08:36 +0000576 if (dev_is_pci(tmp) && dev == to_pci_dev(tmp))
Suresh Siddhae61d98d2008-07-10 11:16:35 -0700577 return 1;
578
579 /* Check our parent */
580 dev = dev->bus->self;
581 }
582
583 return 0;
584}
585
586struct dmar_drhd_unit *
587dmar_find_matched_drhd_unit(struct pci_dev *dev)
588{
Jiang Liu0e242612014-02-19 14:07:34 +0800589 struct dmar_drhd_unit *dmaru;
Yu Zhao2e824f72008-12-22 16:54:58 +0800590 struct acpi_dmar_hardware_unit *drhd;
Suresh Siddhae61d98d2008-07-10 11:16:35 -0700591
Yinghaidda56542010-04-09 01:07:55 +0100592 dev = pci_physfn(dev);
593
Jiang Liu0e242612014-02-19 14:07:34 +0800594 rcu_read_lock();
Yijing Wang8b161f02013-10-31 17:25:16 +0800595 for_each_drhd_unit(dmaru) {
Yu Zhao2e824f72008-12-22 16:54:58 +0800596 drhd = container_of(dmaru->hdr,
597 struct acpi_dmar_hardware_unit,
598 header);
599
600 if (dmaru->include_all &&
601 drhd->segment == pci_domain_nr(dev->bus))
Jiang Liu0e242612014-02-19 14:07:34 +0800602 goto out;
Yu Zhao2e824f72008-12-22 16:54:58 +0800603
604 if (dmar_pci_device_match(dmaru->devices,
605 dmaru->devices_cnt, dev))
Jiang Liu0e242612014-02-19 14:07:34 +0800606 goto out;
Suresh Siddhae61d98d2008-07-10 11:16:35 -0700607 }
Jiang Liu0e242612014-02-19 14:07:34 +0800608 dmaru = NULL;
609out:
610 rcu_read_unlock();
Suresh Siddhae61d98d2008-07-10 11:16:35 -0700611
Jiang Liu0e242612014-02-19 14:07:34 +0800612 return dmaru;
Suresh Siddhae61d98d2008-07-10 11:16:35 -0700613}
614
David Woodhouseed403562014-03-07 23:15:42 +0000615static void __init dmar_acpi_insert_dev_scope(u8 device_number,
616 struct acpi_device *adev)
617{
618 struct dmar_drhd_unit *dmaru;
619 struct acpi_dmar_hardware_unit *drhd;
620 struct acpi_dmar_device_scope *scope;
621 struct device *tmp;
622 int i;
623 struct acpi_dmar_pci_path *path;
624
625 for_each_drhd_unit(dmaru) {
626 drhd = container_of(dmaru->hdr,
627 struct acpi_dmar_hardware_unit,
628 header);
629
630 for (scope = (void *)(drhd + 1);
631 (unsigned long)scope < ((unsigned long)drhd) + drhd->header.length;
632 scope = ((void *)scope) + scope->length) {
633 if (scope->entry_type != ACPI_DMAR_SCOPE_TYPE_ACPI)
634 continue;
635 if (scope->enumeration_id != device_number)
636 continue;
637
638 path = (void *)(scope + 1);
639 pr_info("ACPI device \"%s\" under DMAR at %llx as %02x:%02x.%d\n",
640 dev_name(&adev->dev), dmaru->reg_base_addr,
641 scope->bus, path->device, path->function);
642 for_each_dev_scope(dmaru->devices, dmaru->devices_cnt, i, tmp)
643 if (tmp == NULL) {
644 dmaru->devices[i].bus = scope->bus;
645 dmaru->devices[i].devfn = PCI_DEVFN(path->device,
646 path->function);
647 rcu_assign_pointer(dmaru->devices[i].dev,
648 get_device(&adev->dev));
649 return;
650 }
651 BUG_ON(i >= dmaru->devices_cnt);
652 }
653 }
654 pr_warn("No IOMMU scope found for ANDD enumeration ID %d (%s)\n",
655 device_number, dev_name(&adev->dev));
656}
657
658static int __init dmar_acpi_dev_scope_init(void)
659{
Joerg Roedel11f1a772014-03-25 20:16:40 +0100660 struct acpi_dmar_andd *andd;
661
662 if (dmar_tbl == NULL)
663 return -ENODEV;
664
David Woodhouse7713ec02014-04-01 14:58:36 +0100665 for (andd = (void *)dmar_tbl + sizeof(struct acpi_table_dmar);
666 ((unsigned long)andd) < ((unsigned long)dmar_tbl) + dmar_tbl->length;
667 andd = ((void *)andd) + andd->header.length) {
David Woodhouseed403562014-03-07 23:15:42 +0000668 if (andd->header.type == ACPI_DMAR_TYPE_ANDD) {
669 acpi_handle h;
670 struct acpi_device *adev;
671
672 if (!ACPI_SUCCESS(acpi_get_handle(ACPI_ROOT_OBJECT,
673 andd->object_name,
674 &h))) {
675 pr_err("Failed to find handle for ACPI object %s\n",
676 andd->object_name);
677 continue;
678 }
679 acpi_bus_get_device(h, &adev);
680 if (!adev) {
681 pr_err("Failed to get device for ACPI object %s\n",
682 andd->object_name);
683 continue;
684 }
685 dmar_acpi_insert_dev_scope(andd->device_number, adev);
686 }
David Woodhouseed403562014-03-07 23:15:42 +0000687 }
688 return 0;
689}
690
Suresh Siddha1886e8a2008-07-10 11:16:37 -0700691int __init dmar_dev_scope_init(void)
692{
Jiang Liu2e455282014-02-19 14:07:36 +0800693 struct pci_dev *dev = NULL;
694 struct dmar_pci_notify_info *info;
Suresh Siddha1886e8a2008-07-10 11:16:37 -0700695
Jiang Liu2e455282014-02-19 14:07:36 +0800696 if (dmar_dev_scope_status != 1)
697 return dmar_dev_scope_status;
Suresh Siddhac2c72862011-08-23 17:05:19 -0700698
Jiang Liu2e455282014-02-19 14:07:36 +0800699 if (list_empty(&dmar_drhd_units)) {
700 dmar_dev_scope_status = -ENODEV;
701 } else {
702 dmar_dev_scope_status = 0;
Suresh Siddha318fe7d2011-08-23 17:05:20 -0700703
David Woodhouse63b42622014-03-28 11:28:40 +0000704 dmar_acpi_dev_scope_init();
705
Jiang Liu2e455282014-02-19 14:07:36 +0800706 for_each_pci_dev(dev) {
707 if (dev->is_virtfn)
708 continue;
709
710 info = dmar_alloc_pci_notify_info(dev,
711 BUS_NOTIFY_ADD_DEVICE);
712 if (!info) {
713 return dmar_dev_scope_status;
714 } else {
715 dmar_pci_bus_add_dev(info);
716 dmar_free_pci_notify_info(info);
717 }
718 }
719
720 bus_register_notifier(&pci_bus_type, &dmar_pci_bus_nb);
Suresh Siddha1886e8a2008-07-10 11:16:37 -0700721 }
722
Jiang Liu2e455282014-02-19 14:07:36 +0800723 return dmar_dev_scope_status;
Suresh Siddha1886e8a2008-07-10 11:16:37 -0700724}
725
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -0700726
727int __init dmar_table_init(void)
728{
Suresh Siddha1886e8a2008-07-10 11:16:37 -0700729 static int dmar_table_initialized;
Fenghua Yu093f87d2007-11-21 15:07:14 -0800730 int ret;
731
Jiang Liucc053012014-01-06 14:18:24 +0800732 if (dmar_table_initialized == 0) {
733 ret = parse_dmar_table();
734 if (ret < 0) {
735 if (ret != -ENODEV)
736 pr_info("parse DMAR table failure.\n");
737 } else if (list_empty(&dmar_drhd_units)) {
738 pr_info("No DMAR devices found\n");
739 ret = -ENODEV;
740 }
Suresh Siddha1886e8a2008-07-10 11:16:37 -0700741
Jiang Liucc053012014-01-06 14:18:24 +0800742 if (ret < 0)
743 dmar_table_initialized = ret;
744 else
745 dmar_table_initialized = 1;
Fenghua Yu093f87d2007-11-21 15:07:14 -0800746 }
747
Jiang Liucc053012014-01-06 14:18:24 +0800748 return dmar_table_initialized < 0 ? dmar_table_initialized : 0;
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -0700749}
750
Ben Hutchings3a8663e2010-04-03 19:37:23 +0100751static void warn_invalid_dmar(u64 addr, const char *message)
752{
Ben Hutchingsfd0c8892010-04-03 19:38:43 +0100753 WARN_TAINT_ONCE(
754 1, TAINT_FIRMWARE_WORKAROUND,
755 "Your BIOS is broken; DMAR reported at address %llx%s!\n"
756 "BIOS vendor: %s; Ver: %s; Product Version: %s\n",
757 addr, message,
758 dmi_get_system_info(DMI_BIOS_VENDOR),
759 dmi_get_system_info(DMI_BIOS_VERSION),
760 dmi_get_system_info(DMI_PRODUCT_VERSION));
Ben Hutchings3a8663e2010-04-03 19:37:23 +0100761}
David Woodhouse6ecbf012009-12-02 09:20:27 +0000762
Rashika Kheria21004dc2013-12-18 12:01:46 +0530763static int __init check_zero_address(void)
David Woodhouse86cf8982009-11-09 22:15:15 +0000764{
765 struct acpi_table_dmar *dmar;
766 struct acpi_dmar_header *entry_header;
767 struct acpi_dmar_hardware_unit *drhd;
768
769 dmar = (struct acpi_table_dmar *)dmar_tbl;
770 entry_header = (struct acpi_dmar_header *)(dmar + 1);
771
772 while (((unsigned long)entry_header) <
773 (((unsigned long)dmar) + dmar_tbl->length)) {
774 /* Avoid looping forever on bad ACPI tables */
775 if (entry_header->length == 0) {
Donald Dutilee9071b02012-06-08 17:13:11 -0400776 pr_warn("Invalid 0-length structure\n");
David Woodhouse86cf8982009-11-09 22:15:15 +0000777 return 0;
778 }
779
780 if (entry_header->type == ACPI_DMAR_TYPE_HARDWARE_UNIT) {
Chris Wright2c992202009-12-02 09:17:13 +0000781 void __iomem *addr;
782 u64 cap, ecap;
783
David Woodhouse86cf8982009-11-09 22:15:15 +0000784 drhd = (void *)entry_header;
785 if (!drhd->address) {
Ben Hutchings3a8663e2010-04-03 19:37:23 +0100786 warn_invalid_dmar(0, "");
Chris Wright2c992202009-12-02 09:17:13 +0000787 goto failed;
David Woodhouse86cf8982009-11-09 22:15:15 +0000788 }
Chris Wright2c992202009-12-02 09:17:13 +0000789
790 addr = early_ioremap(drhd->address, VTD_PAGE_SIZE);
791 if (!addr ) {
792 printk("IOMMU: can't validate: %llx\n", drhd->address);
793 goto failed;
794 }
795 cap = dmar_readq(addr + DMAR_CAP_REG);
796 ecap = dmar_readq(addr + DMAR_ECAP_REG);
797 early_iounmap(addr, VTD_PAGE_SIZE);
798 if (cap == (uint64_t)-1 && ecap == (uint64_t)-1) {
Ben Hutchings3a8663e2010-04-03 19:37:23 +0100799 warn_invalid_dmar(drhd->address,
800 " returns all ones");
Chris Wright2c992202009-12-02 09:17:13 +0000801 goto failed;
802 }
David Woodhouse86cf8982009-11-09 22:15:15 +0000803 }
804
805 entry_header = ((void *)entry_header + entry_header->length);
806 }
807 return 1;
Chris Wright2c992202009-12-02 09:17:13 +0000808
809failed:
Chris Wright2c992202009-12-02 09:17:13 +0000810 return 0;
David Woodhouse86cf8982009-11-09 22:15:15 +0000811}
812
Konrad Rzeszutek Wilk480125b2010-08-26 13:57:57 -0400813int __init detect_intel_iommu(void)
Suresh Siddha2ae21012008-07-10 11:16:43 -0700814{
815 int ret;
816
Jiang Liu3a5670e2014-02-19 14:07:33 +0800817 down_write(&dmar_global_lock);
Yinghai Luf6dd5c32008-09-03 16:58:32 -0700818 ret = dmar_table_detect();
David Woodhouse86cf8982009-11-09 22:15:15 +0000819 if (ret)
820 ret = check_zero_address();
Suresh Siddha2ae21012008-07-10 11:16:43 -0700821 {
Linus Torvalds11bd04f2009-12-11 12:18:16 -0800822 if (ret && !no_iommu && !iommu_detected && !dmar_disabled) {
Suresh Siddha2ae21012008-07-10 11:16:43 -0700823 iommu_detected = 1;
Chris Wright5d990b62009-12-04 12:15:21 -0800824 /* Make sure ACS will be enabled */
825 pci_request_acs();
826 }
Suresh Siddhaf5d1b972011-08-23 17:05:22 -0700827
FUJITA Tomonori9d5ce732009-11-10 19:46:16 +0900828#ifdef CONFIG_X86
829 if (ret)
830 x86_init.iommu.iommu_init = intel_iommu_init;
831#endif
Youquan Songcacd4212008-10-16 16:31:57 -0700832 }
Jiang Liub707cb02014-01-06 14:18:26 +0800833 early_acpi_os_unmap_memory((void __iomem *)dmar_tbl, dmar_tbl_size);
Yinghai Luf6dd5c32008-09-03 16:58:32 -0700834 dmar_tbl = NULL;
Jiang Liu3a5670e2014-02-19 14:07:33 +0800835 up_write(&dmar_global_lock);
Konrad Rzeszutek Wilk480125b2010-08-26 13:57:57 -0400836
Konrad Rzeszutek Wilk4db77ff2010-08-26 13:58:04 -0400837 return ret ? 1 : -ENODEV;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700838}
839
840
Donald Dutile6f5cf522012-06-04 17:29:02 -0400841static void unmap_iommu(struct intel_iommu *iommu)
842{
843 iounmap(iommu->reg);
844 release_mem_region(iommu->reg_phys, iommu->reg_size);
845}
846
847/**
848 * map_iommu: map the iommu's registers
849 * @iommu: the iommu to map
850 * @phys_addr: the physical address of the base resgister
Donald Dutilee9071b02012-06-08 17:13:11 -0400851 *
Donald Dutile6f5cf522012-06-04 17:29:02 -0400852 * Memory map the iommu's registers. Start w/ a single page, and
Donald Dutilee9071b02012-06-08 17:13:11 -0400853 * possibly expand if that turns out to be insufficent.
Donald Dutile6f5cf522012-06-04 17:29:02 -0400854 */
855static int map_iommu(struct intel_iommu *iommu, u64 phys_addr)
856{
857 int map_size, err=0;
858
859 iommu->reg_phys = phys_addr;
860 iommu->reg_size = VTD_PAGE_SIZE;
861
862 if (!request_mem_region(iommu->reg_phys, iommu->reg_size, iommu->name)) {
863 pr_err("IOMMU: can't reserve memory\n");
864 err = -EBUSY;
865 goto out;
866 }
867
868 iommu->reg = ioremap(iommu->reg_phys, iommu->reg_size);
869 if (!iommu->reg) {
870 pr_err("IOMMU: can't map the region\n");
871 err = -ENOMEM;
872 goto release;
873 }
874
875 iommu->cap = dmar_readq(iommu->reg + DMAR_CAP_REG);
876 iommu->ecap = dmar_readq(iommu->reg + DMAR_ECAP_REG);
877
878 if (iommu->cap == (uint64_t)-1 && iommu->ecap == (uint64_t)-1) {
879 err = -EINVAL;
880 warn_invalid_dmar(phys_addr, " returns all ones");
881 goto unmap;
882 }
883
884 /* the registers might be more than one page */
885 map_size = max_t(int, ecap_max_iotlb_offset(iommu->ecap),
886 cap_max_fault_reg_offset(iommu->cap));
887 map_size = VTD_PAGE_ALIGN(map_size);
888 if (map_size > iommu->reg_size) {
889 iounmap(iommu->reg);
890 release_mem_region(iommu->reg_phys, iommu->reg_size);
891 iommu->reg_size = map_size;
892 if (!request_mem_region(iommu->reg_phys, iommu->reg_size,
893 iommu->name)) {
894 pr_err("IOMMU: can't reserve memory\n");
895 err = -EBUSY;
896 goto out;
897 }
898 iommu->reg = ioremap(iommu->reg_phys, iommu->reg_size);
899 if (!iommu->reg) {
900 pr_err("IOMMU: can't map the region\n");
901 err = -ENOMEM;
902 goto release;
903 }
904 }
905 err = 0;
906 goto out;
907
908unmap:
909 iounmap(iommu->reg);
910release:
911 release_mem_region(iommu->reg_phys, iommu->reg_size);
912out:
913 return err;
914}
915
Jiang Liu694835d2014-01-06 14:18:16 +0800916static int alloc_iommu(struct dmar_drhd_unit *drhd)
Suresh Siddhae61d98d2008-07-10 11:16:35 -0700917{
Suresh Siddhac42d9f32008-07-10 11:16:36 -0700918 struct intel_iommu *iommu;
Takao Indoh3a93c842013-04-23 17:35:03 +0900919 u32 ver, sts;
Suresh Siddhac42d9f32008-07-10 11:16:36 -0700920 static int iommu_allocated = 0;
Joerg Roedel43f73922009-01-03 23:56:27 +0100921 int agaw = 0;
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -0700922 int msagaw = 0;
Donald Dutile6f5cf522012-06-04 17:29:02 -0400923 int err;
Suresh Siddhac42d9f32008-07-10 11:16:36 -0700924
David Woodhouse6ecbf012009-12-02 09:20:27 +0000925 if (!drhd->reg_base_addr) {
Ben Hutchings3a8663e2010-04-03 19:37:23 +0100926 warn_invalid_dmar(0, "");
David Woodhouse6ecbf012009-12-02 09:20:27 +0000927 return -EINVAL;
928 }
929
Suresh Siddhac42d9f32008-07-10 11:16:36 -0700930 iommu = kzalloc(sizeof(*iommu), GFP_KERNEL);
931 if (!iommu)
Suresh Siddha1886e8a2008-07-10 11:16:37 -0700932 return -ENOMEM;
Suresh Siddhac42d9f32008-07-10 11:16:36 -0700933
934 iommu->seq_id = iommu_allocated++;
Suresh Siddha9d783ba2009-03-16 17:04:55 -0700935 sprintf (iommu->name, "dmar%d", iommu->seq_id);
Suresh Siddhae61d98d2008-07-10 11:16:35 -0700936
Donald Dutile6f5cf522012-06-04 17:29:02 -0400937 err = map_iommu(iommu, drhd->reg_base_addr);
938 if (err) {
939 pr_err("IOMMU: failed to map %s\n", iommu->name);
Suresh Siddhae61d98d2008-07-10 11:16:35 -0700940 goto error;
941 }
Suresh Siddhae61d98d2008-07-10 11:16:35 -0700942
Donald Dutile6f5cf522012-06-04 17:29:02 -0400943 err = -EINVAL;
Weidong Han1b573682008-12-08 15:34:06 +0800944 agaw = iommu_calculate_agaw(iommu);
945 if (agaw < 0) {
Donald Dutilebf947fcb2012-06-04 17:29:01 -0400946 pr_err("Cannot get a valid agaw for iommu (seq_id = %d)\n",
947 iommu->seq_id);
David Woodhouse08155652009-08-04 09:17:20 +0100948 goto err_unmap;
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -0700949 }
950 msagaw = iommu_calculate_max_sagaw(iommu);
951 if (msagaw < 0) {
Donald Dutilebf947fcb2012-06-04 17:29:01 -0400952 pr_err("Cannot get a valid max agaw for iommu (seq_id = %d)\n",
Weidong Han1b573682008-12-08 15:34:06 +0800953 iommu->seq_id);
David Woodhouse08155652009-08-04 09:17:20 +0100954 goto err_unmap;
Weidong Han1b573682008-12-08 15:34:06 +0800955 }
956 iommu->agaw = agaw;
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -0700957 iommu->msagaw = msagaw;
David Woodhouse67ccac42014-03-09 13:49:45 -0700958 iommu->segment = drhd->segment;
Weidong Han1b573682008-12-08 15:34:06 +0800959
Suresh Siddhaee34b322009-10-02 11:01:21 -0700960 iommu->node = -1;
961
Suresh Siddhae61d98d2008-07-10 11:16:35 -0700962 ver = readl(iommu->reg + DMAR_VER_REG);
Yinghai Lu680a7522010-04-08 19:58:23 +0100963 pr_info("IOMMU %d: reg_base_addr %llx ver %d:%d cap %llx ecap %llx\n",
964 iommu->seq_id,
Fenghua Yu5b6985c2008-10-16 18:02:32 -0700965 (unsigned long long)drhd->reg_base_addr,
966 DMAR_VER_MAJOR(ver), DMAR_VER_MINOR(ver),
967 (unsigned long long)iommu->cap,
968 (unsigned long long)iommu->ecap);
Suresh Siddhae61d98d2008-07-10 11:16:35 -0700969
Takao Indoh3a93c842013-04-23 17:35:03 +0900970 /* Reflect status in gcmd */
971 sts = readl(iommu->reg + DMAR_GSTS_REG);
972 if (sts & DMA_GSTS_IRES)
973 iommu->gcmd |= DMA_GCMD_IRE;
974 if (sts & DMA_GSTS_TES)
975 iommu->gcmd |= DMA_GCMD_TE;
976 if (sts & DMA_GSTS_QIES)
977 iommu->gcmd |= DMA_GCMD_QIE;
978
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +0200979 raw_spin_lock_init(&iommu->register_lock);
Suresh Siddhae61d98d2008-07-10 11:16:35 -0700980
981 drhd->iommu = iommu;
Suresh Siddha1886e8a2008-07-10 11:16:37 -0700982 return 0;
David Woodhouse08155652009-08-04 09:17:20 +0100983
984 err_unmap:
Donald Dutile6f5cf522012-06-04 17:29:02 -0400985 unmap_iommu(iommu);
David Woodhouse08155652009-08-04 09:17:20 +0100986 error:
Suresh Siddhae61d98d2008-07-10 11:16:35 -0700987 kfree(iommu);
Donald Dutile6f5cf522012-06-04 17:29:02 -0400988 return err;
Suresh Siddhae61d98d2008-07-10 11:16:35 -0700989}
990
Jiang Liua868e6b2014-01-06 14:18:20 +0800991static void free_iommu(struct intel_iommu *iommu)
Suresh Siddhae61d98d2008-07-10 11:16:35 -0700992{
Jiang Liua868e6b2014-01-06 14:18:20 +0800993 if (iommu->irq) {
994 free_irq(iommu->irq, iommu);
995 irq_set_handler_data(iommu->irq, NULL);
996 destroy_irq(iommu->irq);
997 }
Suresh Siddhae61d98d2008-07-10 11:16:35 -0700998
Jiang Liua84da702014-01-06 14:18:23 +0800999 if (iommu->qi) {
1000 free_page((unsigned long)iommu->qi->desc);
1001 kfree(iommu->qi->desc_status);
1002 kfree(iommu->qi);
1003 }
1004
Suresh Siddhae61d98d2008-07-10 11:16:35 -07001005 if (iommu->reg)
Donald Dutile6f5cf522012-06-04 17:29:02 -04001006 unmap_iommu(iommu);
1007
Suresh Siddhae61d98d2008-07-10 11:16:35 -07001008 kfree(iommu);
1009}
Suresh Siddhafe962e92008-07-10 11:16:42 -07001010
1011/*
1012 * Reclaim all the submitted descriptors which have completed its work.
1013 */
1014static inline void reclaim_free_desc(struct q_inval *qi)
1015{
Yu Zhao6ba6c3a2009-05-18 13:51:35 +08001016 while (qi->desc_status[qi->free_tail] == QI_DONE ||
1017 qi->desc_status[qi->free_tail] == QI_ABORT) {
Suresh Siddhafe962e92008-07-10 11:16:42 -07001018 qi->desc_status[qi->free_tail] = QI_FREE;
1019 qi->free_tail = (qi->free_tail + 1) % QI_LENGTH;
1020 qi->free_cnt++;
1021 }
1022}
1023
Yu Zhao704126a2009-01-04 16:28:52 +08001024static int qi_check_fault(struct intel_iommu *iommu, int index)
1025{
1026 u32 fault;
Yu Zhao6ba6c3a2009-05-18 13:51:35 +08001027 int head, tail;
Yu Zhao704126a2009-01-04 16:28:52 +08001028 struct q_inval *qi = iommu->qi;
1029 int wait_index = (index + 1) % QI_LENGTH;
1030
Yu Zhao6ba6c3a2009-05-18 13:51:35 +08001031 if (qi->desc_status[wait_index] == QI_ABORT)
1032 return -EAGAIN;
1033
Yu Zhao704126a2009-01-04 16:28:52 +08001034 fault = readl(iommu->reg + DMAR_FSTS_REG);
1035
1036 /*
1037 * If IQE happens, the head points to the descriptor associated
1038 * with the error. No new descriptors are fetched until the IQE
1039 * is cleared.
1040 */
1041 if (fault & DMA_FSTS_IQE) {
1042 head = readl(iommu->reg + DMAR_IQH_REG);
Yu Zhao6ba6c3a2009-05-18 13:51:35 +08001043 if ((head >> DMAR_IQ_SHIFT) == index) {
Donald Dutilebf947fcb2012-06-04 17:29:01 -04001044 pr_err("VT-d detected invalid descriptor: "
Yu Zhao6ba6c3a2009-05-18 13:51:35 +08001045 "low=%llx, high=%llx\n",
1046 (unsigned long long)qi->desc[index].low,
1047 (unsigned long long)qi->desc[index].high);
Yu Zhao704126a2009-01-04 16:28:52 +08001048 memcpy(&qi->desc[index], &qi->desc[wait_index],
1049 sizeof(struct qi_desc));
1050 __iommu_flush_cache(iommu, &qi->desc[index],
1051 sizeof(struct qi_desc));
1052 writel(DMA_FSTS_IQE, iommu->reg + DMAR_FSTS_REG);
1053 return -EINVAL;
1054 }
1055 }
1056
Yu Zhao6ba6c3a2009-05-18 13:51:35 +08001057 /*
1058 * If ITE happens, all pending wait_desc commands are aborted.
1059 * No new descriptors are fetched until the ITE is cleared.
1060 */
1061 if (fault & DMA_FSTS_ITE) {
1062 head = readl(iommu->reg + DMAR_IQH_REG);
1063 head = ((head >> DMAR_IQ_SHIFT) - 1 + QI_LENGTH) % QI_LENGTH;
1064 head |= 1;
1065 tail = readl(iommu->reg + DMAR_IQT_REG);
1066 tail = ((tail >> DMAR_IQ_SHIFT) - 1 + QI_LENGTH) % QI_LENGTH;
1067
1068 writel(DMA_FSTS_ITE, iommu->reg + DMAR_FSTS_REG);
1069
1070 do {
1071 if (qi->desc_status[head] == QI_IN_USE)
1072 qi->desc_status[head] = QI_ABORT;
1073 head = (head - 2 + QI_LENGTH) % QI_LENGTH;
1074 } while (head != tail);
1075
1076 if (qi->desc_status[wait_index] == QI_ABORT)
1077 return -EAGAIN;
1078 }
1079
1080 if (fault & DMA_FSTS_ICE)
1081 writel(DMA_FSTS_ICE, iommu->reg + DMAR_FSTS_REG);
1082
Yu Zhao704126a2009-01-04 16:28:52 +08001083 return 0;
1084}
1085
Suresh Siddhafe962e92008-07-10 11:16:42 -07001086/*
1087 * Submit the queued invalidation descriptor to the remapping
1088 * hardware unit and wait for its completion.
1089 */
Yu Zhao704126a2009-01-04 16:28:52 +08001090int qi_submit_sync(struct qi_desc *desc, struct intel_iommu *iommu)
Suresh Siddhafe962e92008-07-10 11:16:42 -07001091{
Yu Zhao6ba6c3a2009-05-18 13:51:35 +08001092 int rc;
Suresh Siddhafe962e92008-07-10 11:16:42 -07001093 struct q_inval *qi = iommu->qi;
1094 struct qi_desc *hw, wait_desc;
1095 int wait_index, index;
1096 unsigned long flags;
1097
1098 if (!qi)
Yu Zhao704126a2009-01-04 16:28:52 +08001099 return 0;
Suresh Siddhafe962e92008-07-10 11:16:42 -07001100
1101 hw = qi->desc;
1102
Yu Zhao6ba6c3a2009-05-18 13:51:35 +08001103restart:
1104 rc = 0;
1105
Thomas Gleixner3b8f4042011-07-19 17:02:07 +02001106 raw_spin_lock_irqsave(&qi->q_lock, flags);
Suresh Siddhafe962e92008-07-10 11:16:42 -07001107 while (qi->free_cnt < 3) {
Thomas Gleixner3b8f4042011-07-19 17:02:07 +02001108 raw_spin_unlock_irqrestore(&qi->q_lock, flags);
Suresh Siddhafe962e92008-07-10 11:16:42 -07001109 cpu_relax();
Thomas Gleixner3b8f4042011-07-19 17:02:07 +02001110 raw_spin_lock_irqsave(&qi->q_lock, flags);
Suresh Siddhafe962e92008-07-10 11:16:42 -07001111 }
1112
1113 index = qi->free_head;
1114 wait_index = (index + 1) % QI_LENGTH;
1115
1116 qi->desc_status[index] = qi->desc_status[wait_index] = QI_IN_USE;
1117
1118 hw[index] = *desc;
1119
Yu Zhao704126a2009-01-04 16:28:52 +08001120 wait_desc.low = QI_IWD_STATUS_DATA(QI_DONE) |
1121 QI_IWD_STATUS_WRITE | QI_IWD_TYPE;
Suresh Siddhafe962e92008-07-10 11:16:42 -07001122 wait_desc.high = virt_to_phys(&qi->desc_status[wait_index]);
1123
1124 hw[wait_index] = wait_desc;
1125
1126 __iommu_flush_cache(iommu, &hw[index], sizeof(struct qi_desc));
1127 __iommu_flush_cache(iommu, &hw[wait_index], sizeof(struct qi_desc));
1128
1129 qi->free_head = (qi->free_head + 2) % QI_LENGTH;
1130 qi->free_cnt -= 2;
1131
Suresh Siddhafe962e92008-07-10 11:16:42 -07001132 /*
1133 * update the HW tail register indicating the presence of
1134 * new descriptors.
1135 */
Yu Zhao6ba6c3a2009-05-18 13:51:35 +08001136 writel(qi->free_head << DMAR_IQ_SHIFT, iommu->reg + DMAR_IQT_REG);
Suresh Siddhafe962e92008-07-10 11:16:42 -07001137
1138 while (qi->desc_status[wait_index] != QI_DONE) {
Suresh Siddhaf05810c2008-10-16 16:31:54 -07001139 /*
1140 * We will leave the interrupts disabled, to prevent interrupt
1141 * context to queue another cmd while a cmd is already submitted
1142 * and waiting for completion on this cpu. This is to avoid
1143 * a deadlock where the interrupt context can wait indefinitely
1144 * for free slots in the queue.
1145 */
Yu Zhao704126a2009-01-04 16:28:52 +08001146 rc = qi_check_fault(iommu, index);
1147 if (rc)
Yu Zhao6ba6c3a2009-05-18 13:51:35 +08001148 break;
Yu Zhao704126a2009-01-04 16:28:52 +08001149
Thomas Gleixner3b8f4042011-07-19 17:02:07 +02001150 raw_spin_unlock(&qi->q_lock);
Suresh Siddhafe962e92008-07-10 11:16:42 -07001151 cpu_relax();
Thomas Gleixner3b8f4042011-07-19 17:02:07 +02001152 raw_spin_lock(&qi->q_lock);
Suresh Siddhafe962e92008-07-10 11:16:42 -07001153 }
Yu Zhao6ba6c3a2009-05-18 13:51:35 +08001154
1155 qi->desc_status[index] = QI_DONE;
Suresh Siddhafe962e92008-07-10 11:16:42 -07001156
1157 reclaim_free_desc(qi);
Thomas Gleixner3b8f4042011-07-19 17:02:07 +02001158 raw_spin_unlock_irqrestore(&qi->q_lock, flags);
Yu Zhao704126a2009-01-04 16:28:52 +08001159
Yu Zhao6ba6c3a2009-05-18 13:51:35 +08001160 if (rc == -EAGAIN)
1161 goto restart;
1162
Yu Zhao704126a2009-01-04 16:28:52 +08001163 return rc;
Suresh Siddhafe962e92008-07-10 11:16:42 -07001164}
1165
1166/*
1167 * Flush the global interrupt entry cache.
1168 */
1169void qi_global_iec(struct intel_iommu *iommu)
1170{
1171 struct qi_desc desc;
1172
1173 desc.low = QI_IEC_TYPE;
1174 desc.high = 0;
1175
Yu Zhao704126a2009-01-04 16:28:52 +08001176 /* should never fail */
Suresh Siddhafe962e92008-07-10 11:16:42 -07001177 qi_submit_sync(&desc, iommu);
1178}
1179
David Woodhouse4c25a2c2009-05-10 17:16:06 +01001180void qi_flush_context(struct intel_iommu *iommu, u16 did, u16 sid, u8 fm,
1181 u64 type)
Youquan Song3481f212008-10-16 16:31:55 -07001182{
Youquan Song3481f212008-10-16 16:31:55 -07001183 struct qi_desc desc;
1184
Youquan Song3481f212008-10-16 16:31:55 -07001185 desc.low = QI_CC_FM(fm) | QI_CC_SID(sid) | QI_CC_DID(did)
1186 | QI_CC_GRAN(type) | QI_CC_TYPE;
1187 desc.high = 0;
1188
David Woodhouse4c25a2c2009-05-10 17:16:06 +01001189 qi_submit_sync(&desc, iommu);
Youquan Song3481f212008-10-16 16:31:55 -07001190}
1191
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01001192void qi_flush_iotlb(struct intel_iommu *iommu, u16 did, u64 addr,
1193 unsigned int size_order, u64 type)
Youquan Song3481f212008-10-16 16:31:55 -07001194{
1195 u8 dw = 0, dr = 0;
1196
1197 struct qi_desc desc;
1198 int ih = 0;
1199
Youquan Song3481f212008-10-16 16:31:55 -07001200 if (cap_write_drain(iommu->cap))
1201 dw = 1;
1202
1203 if (cap_read_drain(iommu->cap))
1204 dr = 1;
1205
1206 desc.low = QI_IOTLB_DID(did) | QI_IOTLB_DR(dr) | QI_IOTLB_DW(dw)
1207 | QI_IOTLB_GRAN(type) | QI_IOTLB_TYPE;
1208 desc.high = QI_IOTLB_ADDR(addr) | QI_IOTLB_IH(ih)
1209 | QI_IOTLB_AM(size_order);
1210
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01001211 qi_submit_sync(&desc, iommu);
Youquan Song3481f212008-10-16 16:31:55 -07001212}
1213
Yu Zhao6ba6c3a2009-05-18 13:51:35 +08001214void qi_flush_dev_iotlb(struct intel_iommu *iommu, u16 sid, u16 qdep,
1215 u64 addr, unsigned mask)
1216{
1217 struct qi_desc desc;
1218
1219 if (mask) {
1220 BUG_ON(addr & ((1 << (VTD_PAGE_SHIFT + mask)) - 1));
1221 addr |= (1 << (VTD_PAGE_SHIFT + mask - 1)) - 1;
1222 desc.high = QI_DEV_IOTLB_ADDR(addr) | QI_DEV_IOTLB_SIZE;
1223 } else
1224 desc.high = QI_DEV_IOTLB_ADDR(addr);
1225
1226 if (qdep >= QI_DEV_IOTLB_MAX_INVS)
1227 qdep = 0;
1228
1229 desc.low = QI_DEV_IOTLB_SID(sid) | QI_DEV_IOTLB_QDEP(qdep) |
1230 QI_DIOTLB_TYPE;
1231
1232 qi_submit_sync(&desc, iommu);
1233}
1234
Suresh Siddhafe962e92008-07-10 11:16:42 -07001235/*
Suresh Siddhaeba67e52009-03-16 17:04:56 -07001236 * Disable Queued Invalidation interface.
1237 */
1238void dmar_disable_qi(struct intel_iommu *iommu)
1239{
1240 unsigned long flags;
1241 u32 sts;
1242 cycles_t start_time = get_cycles();
1243
1244 if (!ecap_qis(iommu->ecap))
1245 return;
1246
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001247 raw_spin_lock_irqsave(&iommu->register_lock, flags);
Suresh Siddhaeba67e52009-03-16 17:04:56 -07001248
1249 sts = dmar_readq(iommu->reg + DMAR_GSTS_REG);
1250 if (!(sts & DMA_GSTS_QIES))
1251 goto end;
1252
1253 /*
1254 * Give a chance to HW to complete the pending invalidation requests.
1255 */
1256 while ((readl(iommu->reg + DMAR_IQT_REG) !=
1257 readl(iommu->reg + DMAR_IQH_REG)) &&
1258 (DMAR_OPERATION_TIMEOUT > (get_cycles() - start_time)))
1259 cpu_relax();
1260
1261 iommu->gcmd &= ~DMA_GCMD_QIE;
Suresh Siddhaeba67e52009-03-16 17:04:56 -07001262 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
1263
1264 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG, readl,
1265 !(sts & DMA_GSTS_QIES), sts);
1266end:
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001267 raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
Suresh Siddhaeba67e52009-03-16 17:04:56 -07001268}
1269
1270/*
Fenghua Yueb4a52b2009-03-27 14:22:43 -07001271 * Enable queued invalidation.
1272 */
1273static void __dmar_enable_qi(struct intel_iommu *iommu)
1274{
David Woodhousec416daa2009-05-10 20:30:58 +01001275 u32 sts;
Fenghua Yueb4a52b2009-03-27 14:22:43 -07001276 unsigned long flags;
1277 struct q_inval *qi = iommu->qi;
1278
1279 qi->free_head = qi->free_tail = 0;
1280 qi->free_cnt = QI_LENGTH;
1281
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001282 raw_spin_lock_irqsave(&iommu->register_lock, flags);
Fenghua Yueb4a52b2009-03-27 14:22:43 -07001283
1284 /* write zero to the tail reg */
1285 writel(0, iommu->reg + DMAR_IQT_REG);
1286
1287 dmar_writeq(iommu->reg + DMAR_IQA_REG, virt_to_phys(qi->desc));
1288
Fenghua Yueb4a52b2009-03-27 14:22:43 -07001289 iommu->gcmd |= DMA_GCMD_QIE;
David Woodhousec416daa2009-05-10 20:30:58 +01001290 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
Fenghua Yueb4a52b2009-03-27 14:22:43 -07001291
1292 /* Make sure hardware complete it */
1293 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG, readl, (sts & DMA_GSTS_QIES), sts);
1294
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001295 raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
Fenghua Yueb4a52b2009-03-27 14:22:43 -07001296}
1297
1298/*
Suresh Siddhafe962e92008-07-10 11:16:42 -07001299 * Enable Queued Invalidation interface. This is a must to support
1300 * interrupt-remapping. Also used by DMA-remapping, which replaces
1301 * register based IOTLB invalidation.
1302 */
1303int dmar_enable_qi(struct intel_iommu *iommu)
1304{
Suresh Siddhafe962e92008-07-10 11:16:42 -07001305 struct q_inval *qi;
Suresh Siddha751cafe2009-10-02 11:01:22 -07001306 struct page *desc_page;
Suresh Siddhafe962e92008-07-10 11:16:42 -07001307
1308 if (!ecap_qis(iommu->ecap))
1309 return -ENOENT;
1310
1311 /*
1312 * queued invalidation is already setup and enabled.
1313 */
1314 if (iommu->qi)
1315 return 0;
1316
Suresh Siddhafa4b57c2009-03-16 17:05:05 -07001317 iommu->qi = kmalloc(sizeof(*qi), GFP_ATOMIC);
Suresh Siddhafe962e92008-07-10 11:16:42 -07001318 if (!iommu->qi)
1319 return -ENOMEM;
1320
1321 qi = iommu->qi;
1322
Suresh Siddha751cafe2009-10-02 11:01:22 -07001323
1324 desc_page = alloc_pages_node(iommu->node, GFP_ATOMIC | __GFP_ZERO, 0);
1325 if (!desc_page) {
Suresh Siddhafe962e92008-07-10 11:16:42 -07001326 kfree(qi);
Jiang Liub707cb02014-01-06 14:18:26 +08001327 iommu->qi = NULL;
Suresh Siddhafe962e92008-07-10 11:16:42 -07001328 return -ENOMEM;
1329 }
1330
Suresh Siddha751cafe2009-10-02 11:01:22 -07001331 qi->desc = page_address(desc_page);
1332
Hannes Reinecke37a40712013-02-06 09:50:10 +01001333 qi->desc_status = kzalloc(QI_LENGTH * sizeof(int), GFP_ATOMIC);
Suresh Siddhafe962e92008-07-10 11:16:42 -07001334 if (!qi->desc_status) {
1335 free_page((unsigned long) qi->desc);
1336 kfree(qi);
Jiang Liub707cb02014-01-06 14:18:26 +08001337 iommu->qi = NULL;
Suresh Siddhafe962e92008-07-10 11:16:42 -07001338 return -ENOMEM;
1339 }
1340
1341 qi->free_head = qi->free_tail = 0;
1342 qi->free_cnt = QI_LENGTH;
1343
Thomas Gleixner3b8f4042011-07-19 17:02:07 +02001344 raw_spin_lock_init(&qi->q_lock);
Suresh Siddhafe962e92008-07-10 11:16:42 -07001345
Fenghua Yueb4a52b2009-03-27 14:22:43 -07001346 __dmar_enable_qi(iommu);
Suresh Siddhafe962e92008-07-10 11:16:42 -07001347
1348 return 0;
1349}
Suresh Siddha0ac24912009-03-16 17:04:54 -07001350
1351/* iommu interrupt handling. Most stuff are MSI-like. */
1352
Suresh Siddha9d783ba2009-03-16 17:04:55 -07001353enum faulttype {
1354 DMA_REMAP,
1355 INTR_REMAP,
1356 UNKNOWN,
1357};
1358
1359static const char *dma_remap_fault_reasons[] =
Suresh Siddha0ac24912009-03-16 17:04:54 -07001360{
1361 "Software",
1362 "Present bit in root entry is clear",
1363 "Present bit in context entry is clear",
1364 "Invalid context entry",
1365 "Access beyond MGAW",
1366 "PTE Write access is not set",
1367 "PTE Read access is not set",
1368 "Next page table ptr is invalid",
1369 "Root table address invalid",
1370 "Context table ptr is invalid",
1371 "non-zero reserved fields in RTP",
1372 "non-zero reserved fields in CTP",
1373 "non-zero reserved fields in PTE",
Li, Zhen-Hua4ecccd92013-03-06 10:43:17 +08001374 "PCE for translation request specifies blocking",
Suresh Siddha0ac24912009-03-16 17:04:54 -07001375};
Suresh Siddha9d783ba2009-03-16 17:04:55 -07001376
Suresh Siddha95a02e92012-03-30 11:47:07 -07001377static const char *irq_remap_fault_reasons[] =
Suresh Siddha9d783ba2009-03-16 17:04:55 -07001378{
1379 "Detected reserved fields in the decoded interrupt-remapped request",
1380 "Interrupt index exceeded the interrupt-remapping table size",
1381 "Present field in the IRTE entry is clear",
1382 "Error accessing interrupt-remapping table pointed by IRTA_REG",
1383 "Detected reserved fields in the IRTE entry",
1384 "Blocked a compatibility format interrupt request",
1385 "Blocked an interrupt request due to source-id verification failure",
1386};
1387
Rashika Kheria21004dc2013-12-18 12:01:46 +05301388static const char *dmar_get_fault_reason(u8 fault_reason, int *fault_type)
Suresh Siddha0ac24912009-03-16 17:04:54 -07001389{
Dan Carpenterfefe1ed2012-05-13 20:09:38 +03001390 if (fault_reason >= 0x20 && (fault_reason - 0x20 <
1391 ARRAY_SIZE(irq_remap_fault_reasons))) {
Suresh Siddha9d783ba2009-03-16 17:04:55 -07001392 *fault_type = INTR_REMAP;
Suresh Siddha95a02e92012-03-30 11:47:07 -07001393 return irq_remap_fault_reasons[fault_reason - 0x20];
Suresh Siddha9d783ba2009-03-16 17:04:55 -07001394 } else if (fault_reason < ARRAY_SIZE(dma_remap_fault_reasons)) {
1395 *fault_type = DMA_REMAP;
1396 return dma_remap_fault_reasons[fault_reason];
1397 } else {
1398 *fault_type = UNKNOWN;
Suresh Siddha0ac24912009-03-16 17:04:54 -07001399 return "Unknown";
Suresh Siddha9d783ba2009-03-16 17:04:55 -07001400 }
Suresh Siddha0ac24912009-03-16 17:04:54 -07001401}
1402
Thomas Gleixner5c2837f2010-09-28 17:15:11 +02001403void dmar_msi_unmask(struct irq_data *data)
Suresh Siddha0ac24912009-03-16 17:04:54 -07001404{
Thomas Gleixnerdced35a2011-03-28 17:49:12 +02001405 struct intel_iommu *iommu = irq_data_get_irq_handler_data(data);
Suresh Siddha0ac24912009-03-16 17:04:54 -07001406 unsigned long flag;
1407
1408 /* unmask it */
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001409 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Suresh Siddha0ac24912009-03-16 17:04:54 -07001410 writel(0, iommu->reg + DMAR_FECTL_REG);
1411 /* Read a reg to force flush the post write */
1412 readl(iommu->reg + DMAR_FECTL_REG);
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001413 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Suresh Siddha0ac24912009-03-16 17:04:54 -07001414}
1415
Thomas Gleixner5c2837f2010-09-28 17:15:11 +02001416void dmar_msi_mask(struct irq_data *data)
Suresh Siddha0ac24912009-03-16 17:04:54 -07001417{
1418 unsigned long flag;
Thomas Gleixnerdced35a2011-03-28 17:49:12 +02001419 struct intel_iommu *iommu = irq_data_get_irq_handler_data(data);
Suresh Siddha0ac24912009-03-16 17:04:54 -07001420
1421 /* mask it */
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001422 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Suresh Siddha0ac24912009-03-16 17:04:54 -07001423 writel(DMA_FECTL_IM, iommu->reg + DMAR_FECTL_REG);
1424 /* Read a reg to force flush the post write */
1425 readl(iommu->reg + DMAR_FECTL_REG);
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001426 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Suresh Siddha0ac24912009-03-16 17:04:54 -07001427}
1428
1429void dmar_msi_write(int irq, struct msi_msg *msg)
1430{
Thomas Gleixnerdced35a2011-03-28 17:49:12 +02001431 struct intel_iommu *iommu = irq_get_handler_data(irq);
Suresh Siddha0ac24912009-03-16 17:04:54 -07001432 unsigned long flag;
1433
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001434 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Suresh Siddha0ac24912009-03-16 17:04:54 -07001435 writel(msg->data, iommu->reg + DMAR_FEDATA_REG);
1436 writel(msg->address_lo, iommu->reg + DMAR_FEADDR_REG);
1437 writel(msg->address_hi, iommu->reg + DMAR_FEUADDR_REG);
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001438 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Suresh Siddha0ac24912009-03-16 17:04:54 -07001439}
1440
1441void dmar_msi_read(int irq, struct msi_msg *msg)
1442{
Thomas Gleixnerdced35a2011-03-28 17:49:12 +02001443 struct intel_iommu *iommu = irq_get_handler_data(irq);
Suresh Siddha0ac24912009-03-16 17:04:54 -07001444 unsigned long flag;
1445
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001446 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Suresh Siddha0ac24912009-03-16 17:04:54 -07001447 msg->data = readl(iommu->reg + DMAR_FEDATA_REG);
1448 msg->address_lo = readl(iommu->reg + DMAR_FEADDR_REG);
1449 msg->address_hi = readl(iommu->reg + DMAR_FEUADDR_REG);
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001450 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Suresh Siddha0ac24912009-03-16 17:04:54 -07001451}
1452
1453static int dmar_fault_do_one(struct intel_iommu *iommu, int type,
1454 u8 fault_reason, u16 source_id, unsigned long long addr)
1455{
1456 const char *reason;
Suresh Siddha9d783ba2009-03-16 17:04:55 -07001457 int fault_type;
Suresh Siddha0ac24912009-03-16 17:04:54 -07001458
Suresh Siddha9d783ba2009-03-16 17:04:55 -07001459 reason = dmar_get_fault_reason(fault_reason, &fault_type);
Suresh Siddha0ac24912009-03-16 17:04:54 -07001460
Suresh Siddha9d783ba2009-03-16 17:04:55 -07001461 if (fault_type == INTR_REMAP)
Donald Dutilebf947fcb2012-06-04 17:29:01 -04001462 pr_err("INTR-REMAP: Request device [[%02x:%02x.%d] "
Suresh Siddha9d783ba2009-03-16 17:04:55 -07001463 "fault index %llx\n"
1464 "INTR-REMAP:[fault reason %02d] %s\n",
1465 (source_id >> 8), PCI_SLOT(source_id & 0xFF),
1466 PCI_FUNC(source_id & 0xFF), addr >> 48,
1467 fault_reason, reason);
1468 else
Donald Dutilebf947fcb2012-06-04 17:29:01 -04001469 pr_err("DMAR:[%s] Request device [%02x:%02x.%d] "
Suresh Siddha9d783ba2009-03-16 17:04:55 -07001470 "fault addr %llx \n"
1471 "DMAR:[fault reason %02d] %s\n",
1472 (type ? "DMA Read" : "DMA Write"),
1473 (source_id >> 8), PCI_SLOT(source_id & 0xFF),
1474 PCI_FUNC(source_id & 0xFF), addr, fault_reason, reason);
Suresh Siddha0ac24912009-03-16 17:04:54 -07001475 return 0;
1476}
1477
1478#define PRIMARY_FAULT_REG_LEN (16)
Suresh Siddha1531a6a2009-03-16 17:04:57 -07001479irqreturn_t dmar_fault(int irq, void *dev_id)
Suresh Siddha0ac24912009-03-16 17:04:54 -07001480{
1481 struct intel_iommu *iommu = dev_id;
1482 int reg, fault_index;
1483 u32 fault_status;
1484 unsigned long flag;
1485
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001486 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Suresh Siddha0ac24912009-03-16 17:04:54 -07001487 fault_status = readl(iommu->reg + DMAR_FSTS_REG);
Suresh Siddha9d783ba2009-03-16 17:04:55 -07001488 if (fault_status)
Donald Dutilebf947fcb2012-06-04 17:29:01 -04001489 pr_err("DRHD: handling fault status reg %x\n", fault_status);
Suresh Siddha0ac24912009-03-16 17:04:54 -07001490
1491 /* TBD: ignore advanced fault log currently */
1492 if (!(fault_status & DMA_FSTS_PPF))
Li, Zhen-Huabd5cdad2013-03-25 16:20:52 +08001493 goto unlock_exit;
Suresh Siddha0ac24912009-03-16 17:04:54 -07001494
1495 fault_index = dma_fsts_fault_record_index(fault_status);
1496 reg = cap_fault_reg_offset(iommu->cap);
1497 while (1) {
1498 u8 fault_reason;
1499 u16 source_id;
1500 u64 guest_addr;
1501 int type;
1502 u32 data;
1503
1504 /* highest 32 bits */
1505 data = readl(iommu->reg + reg +
1506 fault_index * PRIMARY_FAULT_REG_LEN + 12);
1507 if (!(data & DMA_FRCD_F))
1508 break;
1509
1510 fault_reason = dma_frcd_fault_reason(data);
1511 type = dma_frcd_type(data);
1512
1513 data = readl(iommu->reg + reg +
1514 fault_index * PRIMARY_FAULT_REG_LEN + 8);
1515 source_id = dma_frcd_source_id(data);
1516
1517 guest_addr = dmar_readq(iommu->reg + reg +
1518 fault_index * PRIMARY_FAULT_REG_LEN);
1519 guest_addr = dma_frcd_page_addr(guest_addr);
1520 /* clear the fault */
1521 writel(DMA_FRCD_F, iommu->reg + reg +
1522 fault_index * PRIMARY_FAULT_REG_LEN + 12);
1523
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001524 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Suresh Siddha0ac24912009-03-16 17:04:54 -07001525
1526 dmar_fault_do_one(iommu, type, fault_reason,
1527 source_id, guest_addr);
1528
1529 fault_index++;
Troy Heber8211a7b2009-08-19 15:26:11 -06001530 if (fault_index >= cap_num_fault_regs(iommu->cap))
Suresh Siddha0ac24912009-03-16 17:04:54 -07001531 fault_index = 0;
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001532 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Suresh Siddha0ac24912009-03-16 17:04:54 -07001533 }
Suresh Siddha0ac24912009-03-16 17:04:54 -07001534
Li, Zhen-Huabd5cdad2013-03-25 16:20:52 +08001535 writel(DMA_FSTS_PFO | DMA_FSTS_PPF, iommu->reg + DMAR_FSTS_REG);
1536
1537unlock_exit:
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001538 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Suresh Siddha0ac24912009-03-16 17:04:54 -07001539 return IRQ_HANDLED;
1540}
1541
1542int dmar_set_interrupt(struct intel_iommu *iommu)
1543{
1544 int irq, ret;
1545
Suresh Siddha9d783ba2009-03-16 17:04:55 -07001546 /*
1547 * Check if the fault interrupt is already initialized.
1548 */
1549 if (iommu->irq)
1550 return 0;
1551
Suresh Siddha0ac24912009-03-16 17:04:54 -07001552 irq = create_irq();
1553 if (!irq) {
Donald Dutilebf947fcb2012-06-04 17:29:01 -04001554 pr_err("IOMMU: no free vectors\n");
Suresh Siddha0ac24912009-03-16 17:04:54 -07001555 return -EINVAL;
1556 }
1557
Thomas Gleixnerdced35a2011-03-28 17:49:12 +02001558 irq_set_handler_data(irq, iommu);
Suresh Siddha0ac24912009-03-16 17:04:54 -07001559 iommu->irq = irq;
1560
1561 ret = arch_setup_dmar_msi(irq);
1562 if (ret) {
Thomas Gleixnerdced35a2011-03-28 17:49:12 +02001563 irq_set_handler_data(irq, NULL);
Suresh Siddha0ac24912009-03-16 17:04:54 -07001564 iommu->irq = 0;
1565 destroy_irq(irq);
Chris Wrightdd726432009-05-13 15:55:52 -07001566 return ret;
Suresh Siddha0ac24912009-03-16 17:04:54 -07001567 }
1568
Thomas Gleixner477694e2011-07-19 16:25:42 +02001569 ret = request_irq(irq, dmar_fault, IRQF_NO_THREAD, iommu->name, iommu);
Suresh Siddha0ac24912009-03-16 17:04:54 -07001570 if (ret)
Donald Dutilebf947fcb2012-06-04 17:29:01 -04001571 pr_err("IOMMU: can't request irq\n");
Suresh Siddha0ac24912009-03-16 17:04:54 -07001572 return ret;
1573}
Suresh Siddha9d783ba2009-03-16 17:04:55 -07001574
1575int __init enable_drhd_fault_handling(void)
1576{
1577 struct dmar_drhd_unit *drhd;
Jiang Liu7c919772014-01-06 14:18:18 +08001578 struct intel_iommu *iommu;
Suresh Siddha9d783ba2009-03-16 17:04:55 -07001579
1580 /*
1581 * Enable fault control interrupt.
1582 */
Jiang Liu7c919772014-01-06 14:18:18 +08001583 for_each_iommu(iommu, drhd) {
Li, Zhen-Huabd5cdad2013-03-25 16:20:52 +08001584 u32 fault_status;
Jiang Liu7c919772014-01-06 14:18:18 +08001585 int ret = dmar_set_interrupt(iommu);
Suresh Siddha9d783ba2009-03-16 17:04:55 -07001586
1587 if (ret) {
Donald Dutilee9071b02012-06-08 17:13:11 -04001588 pr_err("DRHD %Lx: failed to enable fault, interrupt, ret %d\n",
Suresh Siddha9d783ba2009-03-16 17:04:55 -07001589 (unsigned long long)drhd->reg_base_addr, ret);
1590 return -1;
1591 }
Suresh Siddha7f99d942010-11-30 22:22:29 -08001592
1593 /*
1594 * Clear any previous faults.
1595 */
1596 dmar_fault(iommu->irq, iommu);
Li, Zhen-Huabd5cdad2013-03-25 16:20:52 +08001597 fault_status = readl(iommu->reg + DMAR_FSTS_REG);
1598 writel(fault_status, iommu->reg + DMAR_FSTS_REG);
Suresh Siddha9d783ba2009-03-16 17:04:55 -07001599 }
1600
1601 return 0;
1602}
Fenghua Yueb4a52b2009-03-27 14:22:43 -07001603
1604/*
1605 * Re-enable Queued Invalidation interface.
1606 */
1607int dmar_reenable_qi(struct intel_iommu *iommu)
1608{
1609 if (!ecap_qis(iommu->ecap))
1610 return -ENOENT;
1611
1612 if (!iommu->qi)
1613 return -ENOENT;
1614
1615 /*
1616 * First disable queued invalidation.
1617 */
1618 dmar_disable_qi(iommu);
1619 /*
1620 * Then enable queued invalidation again. Since there is no pending
1621 * invalidation requests now, it's safe to re-enable queued
1622 * invalidation.
1623 */
1624 __dmar_enable_qi(iommu);
1625
1626 return 0;
1627}
Youquan Song074835f2009-09-09 12:05:39 -04001628
1629/*
1630 * Check interrupt remapping support in DMAR table description.
1631 */
Luck, Tony0b8973a2009-12-16 22:59:29 +00001632int __init dmar_ir_support(void)
Youquan Song074835f2009-09-09 12:05:39 -04001633{
1634 struct acpi_table_dmar *dmar;
1635 dmar = (struct acpi_table_dmar *)dmar_tbl;
Arnaud Patard4f506e02010-03-25 18:02:58 +00001636 if (!dmar)
1637 return 0;
Youquan Song074835f2009-09-09 12:05:39 -04001638 return dmar->flags & 0x1;
1639}
Jiang Liu694835d2014-01-06 14:18:16 +08001640
Jiang Liua868e6b2014-01-06 14:18:20 +08001641static int __init dmar_free_unused_resources(void)
1642{
1643 struct dmar_drhd_unit *dmaru, *dmaru_n;
1644
1645 /* DMAR units are in use */
1646 if (irq_remapping_enabled || intel_iommu_enabled)
1647 return 0;
1648
Jiang Liu2e455282014-02-19 14:07:36 +08001649 if (dmar_dev_scope_status != 1 && !list_empty(&dmar_drhd_units))
1650 bus_unregister_notifier(&pci_bus_type, &dmar_pci_bus_nb);
Jiang Liu59ce0512014-02-19 14:07:35 +08001651
Jiang Liu3a5670e2014-02-19 14:07:33 +08001652 down_write(&dmar_global_lock);
Jiang Liua868e6b2014-01-06 14:18:20 +08001653 list_for_each_entry_safe(dmaru, dmaru_n, &dmar_drhd_units, list) {
1654 list_del(&dmaru->list);
1655 dmar_free_drhd(dmaru);
1656 }
Jiang Liu3a5670e2014-02-19 14:07:33 +08001657 up_write(&dmar_global_lock);
Jiang Liua868e6b2014-01-06 14:18:20 +08001658
1659 return 0;
1660}
1661
1662late_initcall(dmar_free_unused_resources);
Konrad Rzeszutek Wilk4db77ff2010-08-26 13:58:04 -04001663IOMMU_INIT_POST(detect_intel_iommu);