blob: 68da1ab0f2cd0613974479a94b0145d1fcc2d391 [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 Liu3a5670e2014-02-19 14:07:33 +080047/*
48 * Assumptions:
49 * 1) The hotplug framework guarentees that DMAR unit will be hot-added
50 * before IO devices managed by that unit.
51 * 2) The hotplug framework guarantees that DMAR unit will be hot-removed
52 * after IO devices managed by that unit.
53 * 3) Hotplug events are rare.
54 *
55 * Locking rules for DMA and interrupt remapping related global data structures:
56 * 1) Use dmar_global_lock in process context
57 * 2) Use RCU in interrupt context
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -070058 */
Jiang Liu3a5670e2014-02-19 14:07:33 +080059DECLARE_RWSEM(dmar_global_lock);
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -070060LIST_HEAD(dmar_drhd_units);
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -070061
Suresh Siddha41750d32011-08-23 17:05:18 -070062struct acpi_table_header * __initdata dmar_tbl;
Yinghai Lu8e1568f2009-02-11 01:06:59 -080063static acpi_size dmar_tbl_size;
Jiang Liu2e455282014-02-19 14:07:36 +080064static int dmar_dev_scope_status = 1;
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -070065
Jiang Liu694835d2014-01-06 14:18:16 +080066static int alloc_iommu(struct dmar_drhd_unit *drhd);
Jiang Liua868e6b2014-01-06 14:18:20 +080067static void free_iommu(struct intel_iommu *iommu);
Jiang Liu694835d2014-01-06 14:18:16 +080068
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -070069static void __init dmar_register_drhd_unit(struct dmar_drhd_unit *drhd)
70{
71 /*
72 * add INCLUDE_ALL at the tail, so scan the list will find it at
73 * the very end.
74 */
75 if (drhd->include_all)
Jiang Liu0e242612014-02-19 14:07:34 +080076 list_add_tail_rcu(&drhd->list, &dmar_drhd_units);
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -070077 else
Jiang Liu0e242612014-02-19 14:07:34 +080078 list_add_rcu(&drhd->list, &dmar_drhd_units);
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -070079}
80
Jiang Liubb3a6b72014-02-19 14:07:24 +080081void *dmar_alloc_dev_scope(void *start, void *end, int *cnt)
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -070082{
83 struct acpi_dmar_device_scope *scope;
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -070084
85 *cnt = 0;
86 while (start < end) {
87 scope = start;
Bob Moore83118b02014-07-30 12:21:00 +080088 if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_NAMESPACE ||
David Woodhouse07cb52f2014-03-07 14:39:27 +000089 scope->entry_type == ACPI_DMAR_SCOPE_TYPE_ENDPOINT ||
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -070090 scope->entry_type == ACPI_DMAR_SCOPE_TYPE_BRIDGE)
91 (*cnt)++;
Linn Crosettoae3e7f32013-04-23 12:26:45 -060092 else if (scope->entry_type != ACPI_DMAR_SCOPE_TYPE_IOAPIC &&
93 scope->entry_type != ACPI_DMAR_SCOPE_TYPE_HPET) {
Donald Dutilee9071b02012-06-08 17:13:11 -040094 pr_warn("Unsupported device scope\n");
Yinghai Lu5715f0f2010-04-08 19:58:22 +010095 }
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -070096 start += scope->length;
97 }
98 if (*cnt == 0)
Jiang Liubb3a6b72014-02-19 14:07:24 +080099 return NULL;
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -0700100
David Woodhouse832bd852014-03-07 15:08:36 +0000101 return kcalloc(*cnt, sizeof(struct dmar_dev_scope), GFP_KERNEL);
Jiang Liubb3a6b72014-02-19 14:07:24 +0800102}
103
David Woodhouse832bd852014-03-07 15:08:36 +0000104void dmar_free_dev_scope(struct dmar_dev_scope **devices, int *cnt)
Jiang Liuada4d4b2014-01-06 14:18:09 +0800105{
Jiang Liub683b232014-02-19 14:07:32 +0800106 int i;
David Woodhouse832bd852014-03-07 15:08:36 +0000107 struct device *tmp_dev;
Jiang Liub683b232014-02-19 14:07:32 +0800108
Jiang Liuada4d4b2014-01-06 14:18:09 +0800109 if (*devices && *cnt) {
Jiang Liub683b232014-02-19 14:07:32 +0800110 for_each_active_dev_scope(*devices, *cnt, i, tmp_dev)
David Woodhouse832bd852014-03-07 15:08:36 +0000111 put_device(tmp_dev);
Jiang Liuada4d4b2014-01-06 14:18:09 +0800112 kfree(*devices);
Jiang Liuada4d4b2014-01-06 14:18:09 +0800113 }
Jiang Liu0e242612014-02-19 14:07:34 +0800114
115 *devices = NULL;
116 *cnt = 0;
Jiang Liuada4d4b2014-01-06 14:18:09 +0800117}
118
Jiang Liu59ce0512014-02-19 14:07:35 +0800119/* Optimize out kzalloc()/kfree() for normal cases */
120static char dmar_pci_notify_info_buf[64];
121
122static struct dmar_pci_notify_info *
123dmar_alloc_pci_notify_info(struct pci_dev *dev, unsigned long event)
124{
125 int level = 0;
126 size_t size;
127 struct pci_dev *tmp;
128 struct dmar_pci_notify_info *info;
129
130 BUG_ON(dev->is_virtfn);
131
132 /* Only generate path[] for device addition event */
133 if (event == BUS_NOTIFY_ADD_DEVICE)
134 for (tmp = dev; tmp; tmp = tmp->bus->self)
135 level++;
136
137 size = sizeof(*info) + level * sizeof(struct acpi_dmar_pci_path);
138 if (size <= sizeof(dmar_pci_notify_info_buf)) {
139 info = (struct dmar_pci_notify_info *)dmar_pci_notify_info_buf;
140 } else {
141 info = kzalloc(size, GFP_KERNEL);
142 if (!info) {
143 pr_warn("Out of memory when allocating notify_info "
144 "for %s.\n", pci_name(dev));
Jiang Liu2e455282014-02-19 14:07:36 +0800145 if (dmar_dev_scope_status == 0)
146 dmar_dev_scope_status = -ENOMEM;
Jiang Liu59ce0512014-02-19 14:07:35 +0800147 return NULL;
148 }
149 }
150
151 info->event = event;
152 info->dev = dev;
153 info->seg = pci_domain_nr(dev->bus);
154 info->level = level;
155 if (event == BUS_NOTIFY_ADD_DEVICE) {
Jiang Liu5ae05662014-04-15 10:35:35 +0800156 for (tmp = dev; tmp; tmp = tmp->bus->self) {
157 level--;
Joerg Roedel57384592014-10-02 11:50:25 +0200158 info->path[level].bus = tmp->bus->number;
Jiang Liu59ce0512014-02-19 14:07:35 +0800159 info->path[level].device = PCI_SLOT(tmp->devfn);
160 info->path[level].function = PCI_FUNC(tmp->devfn);
161 if (pci_is_root_bus(tmp->bus))
162 info->bus = tmp->bus->number;
163 }
164 }
165
166 return info;
167}
168
169static inline void dmar_free_pci_notify_info(struct dmar_pci_notify_info *info)
170{
171 if ((void *)info != dmar_pci_notify_info_buf)
172 kfree(info);
173}
174
175static bool dmar_match_pci_path(struct dmar_pci_notify_info *info, int bus,
176 struct acpi_dmar_pci_path *path, int count)
177{
178 int i;
179
180 if (info->bus != bus)
181 return false;
182 if (info->level != count)
183 return false;
184
185 for (i = 0; i < count; i++) {
186 if (path[i].device != info->path[i].device ||
187 path[i].function != info->path[i].function)
188 return false;
189 }
190
191 return true;
192}
193
194/* Return: > 0 if match found, 0 if no match found, < 0 if error happens */
195int dmar_insert_dev_scope(struct dmar_pci_notify_info *info,
196 void *start, void*end, u16 segment,
David Woodhouse832bd852014-03-07 15:08:36 +0000197 struct dmar_dev_scope *devices,
198 int devices_cnt)
Jiang Liu59ce0512014-02-19 14:07:35 +0800199{
200 int i, level;
David Woodhouse832bd852014-03-07 15:08:36 +0000201 struct device *tmp, *dev = &info->dev->dev;
Jiang Liu59ce0512014-02-19 14:07:35 +0800202 struct acpi_dmar_device_scope *scope;
203 struct acpi_dmar_pci_path *path;
204
205 if (segment != info->seg)
206 return 0;
207
208 for (; start < end; start += scope->length) {
209 scope = start;
210 if (scope->entry_type != ACPI_DMAR_SCOPE_TYPE_ENDPOINT &&
211 scope->entry_type != ACPI_DMAR_SCOPE_TYPE_BRIDGE)
212 continue;
213
214 path = (struct acpi_dmar_pci_path *)(scope + 1);
215 level = (scope->length - sizeof(*scope)) / sizeof(*path);
216 if (!dmar_match_pci_path(info, scope->bus, path, level))
217 continue;
218
219 if ((scope->entry_type == ACPI_DMAR_SCOPE_TYPE_ENDPOINT) ^
David Woodhouse832bd852014-03-07 15:08:36 +0000220 (info->dev->hdr_type == PCI_HEADER_TYPE_NORMAL)) {
Jiang Liu59ce0512014-02-19 14:07:35 +0800221 pr_warn("Device scope type does not match for %s\n",
David Woodhouse832bd852014-03-07 15:08:36 +0000222 pci_name(info->dev));
Jiang Liu59ce0512014-02-19 14:07:35 +0800223 return -EINVAL;
224 }
225
226 for_each_dev_scope(devices, devices_cnt, i, tmp)
227 if (tmp == NULL) {
David Woodhouse832bd852014-03-07 15:08:36 +0000228 devices[i].bus = info->dev->bus->number;
229 devices[i].devfn = info->dev->devfn;
230 rcu_assign_pointer(devices[i].dev,
231 get_device(dev));
Jiang Liu59ce0512014-02-19 14:07:35 +0800232 return 1;
233 }
234 BUG_ON(i >= devices_cnt);
235 }
236
237 return 0;
238}
239
240int dmar_remove_dev_scope(struct dmar_pci_notify_info *info, u16 segment,
David Woodhouse832bd852014-03-07 15:08:36 +0000241 struct dmar_dev_scope *devices, int count)
Jiang Liu59ce0512014-02-19 14:07:35 +0800242{
243 int index;
David Woodhouse832bd852014-03-07 15:08:36 +0000244 struct device *tmp;
Jiang Liu59ce0512014-02-19 14:07:35 +0800245
246 if (info->seg != segment)
247 return 0;
248
249 for_each_active_dev_scope(devices, count, index, tmp)
David Woodhouse832bd852014-03-07 15:08:36 +0000250 if (tmp == &info->dev->dev) {
Andreea-Cristina Bernateecbad72014-08-18 15:20:56 +0300251 RCU_INIT_POINTER(devices[index].dev, NULL);
Jiang Liu59ce0512014-02-19 14:07:35 +0800252 synchronize_rcu();
David Woodhouse832bd852014-03-07 15:08:36 +0000253 put_device(tmp);
Jiang Liu59ce0512014-02-19 14:07:35 +0800254 return 1;
255 }
256
257 return 0;
258}
259
260static int dmar_pci_bus_add_dev(struct dmar_pci_notify_info *info)
261{
262 int ret = 0;
263 struct dmar_drhd_unit *dmaru;
264 struct acpi_dmar_hardware_unit *drhd;
265
266 for_each_drhd_unit(dmaru) {
267 if (dmaru->include_all)
268 continue;
269
270 drhd = container_of(dmaru->hdr,
271 struct acpi_dmar_hardware_unit, header);
272 ret = dmar_insert_dev_scope(info, (void *)(drhd + 1),
273 ((void *)drhd) + drhd->header.length,
274 dmaru->segment,
275 dmaru->devices, dmaru->devices_cnt);
276 if (ret != 0)
277 break;
278 }
279 if (ret >= 0)
280 ret = dmar_iommu_notify_scope_dev(info);
Jiang Liu2e455282014-02-19 14:07:36 +0800281 if (ret < 0 && dmar_dev_scope_status == 0)
282 dmar_dev_scope_status = ret;
Jiang Liu59ce0512014-02-19 14:07:35 +0800283
284 return ret;
285}
286
287static void dmar_pci_bus_del_dev(struct dmar_pci_notify_info *info)
288{
289 struct dmar_drhd_unit *dmaru;
290
291 for_each_drhd_unit(dmaru)
292 if (dmar_remove_dev_scope(info, dmaru->segment,
293 dmaru->devices, dmaru->devices_cnt))
294 break;
295 dmar_iommu_notify_scope_dev(info);
296}
297
298static int dmar_pci_bus_notifier(struct notifier_block *nb,
299 unsigned long action, void *data)
300{
301 struct pci_dev *pdev = to_pci_dev(data);
302 struct dmar_pci_notify_info *info;
303
304 /* Only care about add/remove events for physical functions */
305 if (pdev->is_virtfn)
306 return NOTIFY_DONE;
307 if (action != BUS_NOTIFY_ADD_DEVICE && action != BUS_NOTIFY_DEL_DEVICE)
308 return NOTIFY_DONE;
309
310 info = dmar_alloc_pci_notify_info(pdev, action);
311 if (!info)
312 return NOTIFY_DONE;
313
314 down_write(&dmar_global_lock);
315 if (action == BUS_NOTIFY_ADD_DEVICE)
316 dmar_pci_bus_add_dev(info);
317 else if (action == BUS_NOTIFY_DEL_DEVICE)
318 dmar_pci_bus_del_dev(info);
319 up_write(&dmar_global_lock);
320
321 dmar_free_pci_notify_info(info);
322
323 return NOTIFY_OK;
324}
325
326static struct notifier_block dmar_pci_bus_nb = {
327 .notifier_call = dmar_pci_bus_notifier,
328 .priority = INT_MIN,
329};
330
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -0700331/**
332 * dmar_parse_one_drhd - parses exactly one DMA remapping hardware definition
333 * structure which uniquely represent one DMA remapping hardware unit
334 * present in the platform
335 */
336static int __init
337dmar_parse_one_drhd(struct acpi_dmar_header *header)
338{
339 struct acpi_dmar_hardware_unit *drhd;
340 struct dmar_drhd_unit *dmaru;
341 int ret = 0;
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -0700342
David Woodhousee523b382009-04-10 22:27:48 -0700343 drhd = (struct acpi_dmar_hardware_unit *)header;
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -0700344 dmaru = kzalloc(sizeof(*dmaru), GFP_KERNEL);
345 if (!dmaru)
346 return -ENOMEM;
347
Suresh Siddha1886e8a2008-07-10 11:16:37 -0700348 dmaru->hdr = header;
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -0700349 dmaru->reg_base_addr = drhd->address;
David Woodhouse276dbf992009-04-04 01:45:37 +0100350 dmaru->segment = drhd->segment;
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -0700351 dmaru->include_all = drhd->flags & 0x1; /* BIT0: INCLUDE_ALL */
David Woodhouse07cb52f2014-03-07 14:39:27 +0000352 dmaru->devices = dmar_alloc_dev_scope((void *)(drhd + 1),
353 ((void *)drhd) + drhd->header.length,
354 &dmaru->devices_cnt);
355 if (dmaru->devices_cnt && dmaru->devices == NULL) {
356 kfree(dmaru);
357 return -ENOMEM;
Jiang Liu2e455282014-02-19 14:07:36 +0800358 }
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -0700359
Suresh Siddha1886e8a2008-07-10 11:16:37 -0700360 ret = alloc_iommu(dmaru);
361 if (ret) {
David Woodhouse07cb52f2014-03-07 14:39:27 +0000362 dmar_free_dev_scope(&dmaru->devices,
363 &dmaru->devices_cnt);
Suresh Siddha1886e8a2008-07-10 11:16:37 -0700364 kfree(dmaru);
365 return ret;
366 }
367 dmar_register_drhd_unit(dmaru);
368 return 0;
369}
370
Jiang Liua868e6b2014-01-06 14:18:20 +0800371static void dmar_free_drhd(struct dmar_drhd_unit *dmaru)
372{
373 if (dmaru->devices && dmaru->devices_cnt)
374 dmar_free_dev_scope(&dmaru->devices, &dmaru->devices_cnt);
375 if (dmaru->iommu)
376 free_iommu(dmaru->iommu);
377 kfree(dmaru);
378}
379
David Woodhousee625b4a2014-03-07 14:34:38 +0000380static int __init dmar_parse_one_andd(struct acpi_dmar_header *header)
381{
382 struct acpi_dmar_andd *andd = (void *)header;
383
384 /* Check for NUL termination within the designated length */
Bob Moore83118b02014-07-30 12:21:00 +0800385 if (strnlen(andd->device_name, header->length - 8) == header->length - 8) {
David Woodhousee625b4a2014-03-07 14:34:38 +0000386 WARN_TAINT(1, TAINT_FIRMWARE_WORKAROUND,
387 "Your BIOS is broken; ANDD object name is not NUL-terminated\n"
388 "BIOS vendor: %s; Ver: %s; Product Version: %s\n",
389 dmi_get_system_info(DMI_BIOS_VENDOR),
390 dmi_get_system_info(DMI_BIOS_VERSION),
391 dmi_get_system_info(DMI_PRODUCT_VERSION));
392 return -EINVAL;
393 }
394 pr_info("ANDD device: %x name: %s\n", andd->device_number,
Bob Moore83118b02014-07-30 12:21:00 +0800395 andd->device_name);
David Woodhousee625b4a2014-03-07 14:34:38 +0000396
397 return 0;
398}
399
David Woodhouseaa697072009-10-07 12:18:00 +0100400#ifdef CONFIG_ACPI_NUMA
Suresh Siddhaee34b322009-10-02 11:01:21 -0700401static int __init
402dmar_parse_one_rhsa(struct acpi_dmar_header *header)
403{
404 struct acpi_dmar_rhsa *rhsa;
405 struct dmar_drhd_unit *drhd;
406
407 rhsa = (struct acpi_dmar_rhsa *)header;
David Woodhouseaa697072009-10-07 12:18:00 +0100408 for_each_drhd_unit(drhd) {
Suresh Siddhaee34b322009-10-02 11:01:21 -0700409 if (drhd->reg_base_addr == rhsa->base_address) {
410 int node = acpi_map_pxm_to_node(rhsa->proximity_domain);
411
412 if (!node_online(node))
413 node = -1;
414 drhd->iommu->node = node;
David Woodhouseaa697072009-10-07 12:18:00 +0100415 return 0;
416 }
Suresh Siddhaee34b322009-10-02 11:01:21 -0700417 }
Ben Hutchingsfd0c8892010-04-03 19:38:43 +0100418 WARN_TAINT(
419 1, TAINT_FIRMWARE_WORKAROUND,
420 "Your BIOS is broken; RHSA refers to non-existent DMAR unit at %llx\n"
421 "BIOS vendor: %s; Ver: %s; Product Version: %s\n",
422 drhd->reg_base_addr,
423 dmi_get_system_info(DMI_BIOS_VENDOR),
424 dmi_get_system_info(DMI_BIOS_VERSION),
425 dmi_get_system_info(DMI_PRODUCT_VERSION));
Suresh Siddhaee34b322009-10-02 11:01:21 -0700426
David Woodhouseaa697072009-10-07 12:18:00 +0100427 return 0;
Suresh Siddhaee34b322009-10-02 11:01:21 -0700428}
David Woodhouseaa697072009-10-07 12:18:00 +0100429#endif
Suresh Siddhaee34b322009-10-02 11:01:21 -0700430
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -0700431static void __init
432dmar_table_print_dmar_entry(struct acpi_dmar_header *header)
433{
434 struct acpi_dmar_hardware_unit *drhd;
435 struct acpi_dmar_reserved_memory *rmrr;
Yu Zhaoaa5d2b52009-05-18 13:51:34 +0800436 struct acpi_dmar_atsr *atsr;
Roland Dreier17b60972009-09-24 12:14:00 -0700437 struct acpi_dmar_rhsa *rhsa;
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -0700438
439 switch (header->type) {
440 case ACPI_DMAR_TYPE_HARDWARE_UNIT:
Yu Zhaoaa5d2b52009-05-18 13:51:34 +0800441 drhd = container_of(header, struct acpi_dmar_hardware_unit,
442 header);
Donald Dutilee9071b02012-06-08 17:13:11 -0400443 pr_info("DRHD base: %#016Lx flags: %#x\n",
Yu Zhaoaa5d2b52009-05-18 13:51:34 +0800444 (unsigned long long)drhd->address, drhd->flags);
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -0700445 break;
446 case ACPI_DMAR_TYPE_RESERVED_MEMORY:
Yu Zhaoaa5d2b52009-05-18 13:51:34 +0800447 rmrr = container_of(header, struct acpi_dmar_reserved_memory,
448 header);
Donald Dutilee9071b02012-06-08 17:13:11 -0400449 pr_info("RMRR base: %#016Lx end: %#016Lx\n",
Fenghua Yu5b6985c2008-10-16 18:02:32 -0700450 (unsigned long long)rmrr->base_address,
451 (unsigned long long)rmrr->end_address);
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -0700452 break;
Bob Moore83118b02014-07-30 12:21:00 +0800453 case ACPI_DMAR_TYPE_ROOT_ATS:
Yu Zhaoaa5d2b52009-05-18 13:51:34 +0800454 atsr = container_of(header, struct acpi_dmar_atsr, header);
Donald Dutilee9071b02012-06-08 17:13:11 -0400455 pr_info("ATSR flags: %#x\n", atsr->flags);
Yu Zhaoaa5d2b52009-05-18 13:51:34 +0800456 break;
Bob Moore83118b02014-07-30 12:21:00 +0800457 case ACPI_DMAR_TYPE_HARDWARE_AFFINITY:
Roland Dreier17b60972009-09-24 12:14:00 -0700458 rhsa = container_of(header, struct acpi_dmar_rhsa, header);
Donald Dutilee9071b02012-06-08 17:13:11 -0400459 pr_info("RHSA base: %#016Lx proximity domain: %#x\n",
Roland Dreier17b60972009-09-24 12:14:00 -0700460 (unsigned long long)rhsa->base_address,
461 rhsa->proximity_domain);
462 break;
Bob Moore83118b02014-07-30 12:21:00 +0800463 case ACPI_DMAR_TYPE_NAMESPACE:
David Woodhousee625b4a2014-03-07 14:34:38 +0000464 /* We don't print this here because we need to sanity-check
465 it first. So print it in dmar_parse_one_andd() instead. */
466 break;
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -0700467 }
468}
469
Yinghai Luf6dd5c32008-09-03 16:58:32 -0700470/**
471 * dmar_table_detect - checks to see if the platform supports DMAR devices
472 */
473static int __init dmar_table_detect(void)
474{
475 acpi_status status = AE_OK;
476
477 /* if we could find DMAR table, then there are DMAR devices */
Yinghai Lu8e1568f2009-02-11 01:06:59 -0800478 status = acpi_get_table_with_size(ACPI_SIG_DMAR, 0,
479 (struct acpi_table_header **)&dmar_tbl,
480 &dmar_tbl_size);
Yinghai Luf6dd5c32008-09-03 16:58:32 -0700481
482 if (ACPI_SUCCESS(status) && !dmar_tbl) {
Donald Dutilee9071b02012-06-08 17:13:11 -0400483 pr_warn("Unable to map DMAR\n");
Yinghai Luf6dd5c32008-09-03 16:58:32 -0700484 status = AE_NOT_FOUND;
485 }
486
487 return (ACPI_SUCCESS(status) ? 1 : 0);
488}
Suresh Siddhaaaa9d1d2008-07-10 11:16:38 -0700489
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -0700490/**
491 * parse_dmar_table - parses the DMA reporting table
492 */
493static int __init
494parse_dmar_table(void)
495{
496 struct acpi_table_dmar *dmar;
497 struct acpi_dmar_header *entry_header;
498 int ret = 0;
Li, Zhen-Hua7cef3342013-05-20 15:57:32 +0800499 int drhd_count = 0;
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -0700500
Yinghai Luf6dd5c32008-09-03 16:58:32 -0700501 /*
502 * Do it again, earlier dmar_tbl mapping could be mapped with
503 * fixed map.
504 */
505 dmar_table_detect();
506
Joseph Cihulaa59b50e2009-06-30 19:31:10 -0700507 /*
508 * ACPI tables may not be DMA protected by tboot, so use DMAR copy
509 * SINIT saved in SinitMleData in TXT heap (which is DMA protected)
510 */
511 dmar_tbl = tboot_get_dmar_table(dmar_tbl);
512
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -0700513 dmar = (struct acpi_table_dmar *)dmar_tbl;
514 if (!dmar)
515 return -ENODEV;
516
Fenghua Yu5b6985c2008-10-16 18:02:32 -0700517 if (dmar->width < PAGE_SHIFT - 1) {
Donald Dutilee9071b02012-06-08 17:13:11 -0400518 pr_warn("Invalid DMAR haw\n");
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -0700519 return -EINVAL;
520 }
521
Donald Dutilee9071b02012-06-08 17:13:11 -0400522 pr_info("Host address width %d\n", dmar->width + 1);
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -0700523
524 entry_header = (struct acpi_dmar_header *)(dmar + 1);
525 while (((unsigned long)entry_header) <
526 (((unsigned long)dmar) + dmar_tbl->length)) {
Tony Battersby084eb962009-02-11 13:24:19 -0800527 /* Avoid looping forever on bad ACPI tables */
528 if (entry_header->length == 0) {
Donald Dutilee9071b02012-06-08 17:13:11 -0400529 pr_warn("Invalid 0-length structure\n");
Tony Battersby084eb962009-02-11 13:24:19 -0800530 ret = -EINVAL;
531 break;
532 }
533
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -0700534 dmar_table_print_dmar_entry(entry_header);
535
536 switch (entry_header->type) {
537 case ACPI_DMAR_TYPE_HARDWARE_UNIT:
Li, Zhen-Hua7cef3342013-05-20 15:57:32 +0800538 drhd_count++;
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -0700539 ret = dmar_parse_one_drhd(entry_header);
540 break;
541 case ACPI_DMAR_TYPE_RESERVED_MEMORY:
542 ret = dmar_parse_one_rmrr(entry_header);
543 break;
Bob Moore83118b02014-07-30 12:21:00 +0800544 case ACPI_DMAR_TYPE_ROOT_ATS:
Yu Zhaoaa5d2b52009-05-18 13:51:34 +0800545 ret = dmar_parse_one_atsr(entry_header);
Yu Zhaoaa5d2b52009-05-18 13:51:34 +0800546 break;
Bob Moore83118b02014-07-30 12:21:00 +0800547 case ACPI_DMAR_TYPE_HARDWARE_AFFINITY:
David Woodhouseaa697072009-10-07 12:18:00 +0100548#ifdef CONFIG_ACPI_NUMA
Suresh Siddhaee34b322009-10-02 11:01:21 -0700549 ret = dmar_parse_one_rhsa(entry_header);
David Woodhouseaa697072009-10-07 12:18:00 +0100550#endif
Roland Dreier17b60972009-09-24 12:14:00 -0700551 break;
Bob Moore83118b02014-07-30 12:21:00 +0800552 case ACPI_DMAR_TYPE_NAMESPACE:
David Woodhousee625b4a2014-03-07 14:34:38 +0000553 ret = dmar_parse_one_andd(entry_header);
554 break;
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -0700555 default:
Donald Dutilee9071b02012-06-08 17:13:11 -0400556 pr_warn("Unknown DMAR structure type %d\n",
Roland Dreier4de75cf2009-09-24 01:01:29 +0100557 entry_header->type);
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -0700558 ret = 0; /* for forward compatibility */
559 break;
560 }
561 if (ret)
562 break;
563
564 entry_header = ((void *)entry_header + entry_header->length);
565 }
Li, Zhen-Hua7cef3342013-05-20 15:57:32 +0800566 if (drhd_count == 0)
567 pr_warn(FW_BUG "No DRHD structure found in DMAR table\n");
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -0700568 return ret;
569}
570
David Woodhouse832bd852014-03-07 15:08:36 +0000571static int dmar_pci_device_match(struct dmar_dev_scope devices[],
572 int cnt, struct pci_dev *dev)
Suresh Siddhae61d98d2008-07-10 11:16:35 -0700573{
574 int index;
David Woodhouse832bd852014-03-07 15:08:36 +0000575 struct device *tmp;
Suresh Siddhae61d98d2008-07-10 11:16:35 -0700576
577 while (dev) {
Jiang Liub683b232014-02-19 14:07:32 +0800578 for_each_active_dev_scope(devices, cnt, index, tmp)
David Woodhouse832bd852014-03-07 15:08:36 +0000579 if (dev_is_pci(tmp) && dev == to_pci_dev(tmp))
Suresh Siddhae61d98d2008-07-10 11:16:35 -0700580 return 1;
581
582 /* Check our parent */
583 dev = dev->bus->self;
584 }
585
586 return 0;
587}
588
589struct dmar_drhd_unit *
590dmar_find_matched_drhd_unit(struct pci_dev *dev)
591{
Jiang Liu0e242612014-02-19 14:07:34 +0800592 struct dmar_drhd_unit *dmaru;
Yu Zhao2e824f72008-12-22 16:54:58 +0800593 struct acpi_dmar_hardware_unit *drhd;
Suresh Siddhae61d98d2008-07-10 11:16:35 -0700594
Yinghaidda56542010-04-09 01:07:55 +0100595 dev = pci_physfn(dev);
596
Jiang Liu0e242612014-02-19 14:07:34 +0800597 rcu_read_lock();
Yijing Wang8b161f02013-10-31 17:25:16 +0800598 for_each_drhd_unit(dmaru) {
Yu Zhao2e824f72008-12-22 16:54:58 +0800599 drhd = container_of(dmaru->hdr,
600 struct acpi_dmar_hardware_unit,
601 header);
602
603 if (dmaru->include_all &&
604 drhd->segment == pci_domain_nr(dev->bus))
Jiang Liu0e242612014-02-19 14:07:34 +0800605 goto out;
Yu Zhao2e824f72008-12-22 16:54:58 +0800606
607 if (dmar_pci_device_match(dmaru->devices,
608 dmaru->devices_cnt, dev))
Jiang Liu0e242612014-02-19 14:07:34 +0800609 goto out;
Suresh Siddhae61d98d2008-07-10 11:16:35 -0700610 }
Jiang Liu0e242612014-02-19 14:07:34 +0800611 dmaru = NULL;
612out:
613 rcu_read_unlock();
Suresh Siddhae61d98d2008-07-10 11:16:35 -0700614
Jiang Liu0e242612014-02-19 14:07:34 +0800615 return dmaru;
Suresh Siddhae61d98d2008-07-10 11:16:35 -0700616}
617
David Woodhouseed403562014-03-07 23:15:42 +0000618static void __init dmar_acpi_insert_dev_scope(u8 device_number,
619 struct acpi_device *adev)
620{
621 struct dmar_drhd_unit *dmaru;
622 struct acpi_dmar_hardware_unit *drhd;
623 struct acpi_dmar_device_scope *scope;
624 struct device *tmp;
625 int i;
626 struct acpi_dmar_pci_path *path;
627
628 for_each_drhd_unit(dmaru) {
629 drhd = container_of(dmaru->hdr,
630 struct acpi_dmar_hardware_unit,
631 header);
632
633 for (scope = (void *)(drhd + 1);
634 (unsigned long)scope < ((unsigned long)drhd) + drhd->header.length;
635 scope = ((void *)scope) + scope->length) {
Bob Moore83118b02014-07-30 12:21:00 +0800636 if (scope->entry_type != ACPI_DMAR_SCOPE_TYPE_NAMESPACE)
David Woodhouseed403562014-03-07 23:15:42 +0000637 continue;
638 if (scope->enumeration_id != device_number)
639 continue;
640
641 path = (void *)(scope + 1);
642 pr_info("ACPI device \"%s\" under DMAR at %llx as %02x:%02x.%d\n",
643 dev_name(&adev->dev), dmaru->reg_base_addr,
644 scope->bus, path->device, path->function);
645 for_each_dev_scope(dmaru->devices, dmaru->devices_cnt, i, tmp)
646 if (tmp == NULL) {
647 dmaru->devices[i].bus = scope->bus;
648 dmaru->devices[i].devfn = PCI_DEVFN(path->device,
649 path->function);
650 rcu_assign_pointer(dmaru->devices[i].dev,
651 get_device(&adev->dev));
652 return;
653 }
654 BUG_ON(i >= dmaru->devices_cnt);
655 }
656 }
657 pr_warn("No IOMMU scope found for ANDD enumeration ID %d (%s)\n",
658 device_number, dev_name(&adev->dev));
659}
660
661static int __init dmar_acpi_dev_scope_init(void)
662{
Joerg Roedel11f1a772014-03-25 20:16:40 +0100663 struct acpi_dmar_andd *andd;
664
665 if (dmar_tbl == NULL)
666 return -ENODEV;
667
David Woodhouse7713ec02014-04-01 14:58:36 +0100668 for (andd = (void *)dmar_tbl + sizeof(struct acpi_table_dmar);
669 ((unsigned long)andd) < ((unsigned long)dmar_tbl) + dmar_tbl->length;
670 andd = ((void *)andd) + andd->header.length) {
Bob Moore83118b02014-07-30 12:21:00 +0800671 if (andd->header.type == ACPI_DMAR_TYPE_NAMESPACE) {
David Woodhouseed403562014-03-07 23:15:42 +0000672 acpi_handle h;
673 struct acpi_device *adev;
674
675 if (!ACPI_SUCCESS(acpi_get_handle(ACPI_ROOT_OBJECT,
Bob Moore83118b02014-07-30 12:21:00 +0800676 andd->device_name,
David Woodhouseed403562014-03-07 23:15:42 +0000677 &h))) {
678 pr_err("Failed to find handle for ACPI object %s\n",
Bob Moore83118b02014-07-30 12:21:00 +0800679 andd->device_name);
David Woodhouseed403562014-03-07 23:15:42 +0000680 continue;
681 }
682 acpi_bus_get_device(h, &adev);
683 if (!adev) {
684 pr_err("Failed to get device for ACPI object %s\n",
Bob Moore83118b02014-07-30 12:21:00 +0800685 andd->device_name);
David Woodhouseed403562014-03-07 23:15:42 +0000686 continue;
687 }
688 dmar_acpi_insert_dev_scope(andd->device_number, adev);
689 }
David Woodhouseed403562014-03-07 23:15:42 +0000690 }
691 return 0;
692}
693
Suresh Siddha1886e8a2008-07-10 11:16:37 -0700694int __init dmar_dev_scope_init(void)
695{
Jiang Liu2e455282014-02-19 14:07:36 +0800696 struct pci_dev *dev = NULL;
697 struct dmar_pci_notify_info *info;
Suresh Siddha1886e8a2008-07-10 11:16:37 -0700698
Jiang Liu2e455282014-02-19 14:07:36 +0800699 if (dmar_dev_scope_status != 1)
700 return dmar_dev_scope_status;
Suresh Siddhac2c72862011-08-23 17:05:19 -0700701
Jiang Liu2e455282014-02-19 14:07:36 +0800702 if (list_empty(&dmar_drhd_units)) {
703 dmar_dev_scope_status = -ENODEV;
704 } else {
705 dmar_dev_scope_status = 0;
Suresh Siddha318fe7d2011-08-23 17:05:20 -0700706
David Woodhouse63b42622014-03-28 11:28:40 +0000707 dmar_acpi_dev_scope_init();
708
Jiang Liu2e455282014-02-19 14:07:36 +0800709 for_each_pci_dev(dev) {
710 if (dev->is_virtfn)
711 continue;
712
713 info = dmar_alloc_pci_notify_info(dev,
714 BUS_NOTIFY_ADD_DEVICE);
715 if (!info) {
716 return dmar_dev_scope_status;
717 } else {
718 dmar_pci_bus_add_dev(info);
719 dmar_free_pci_notify_info(info);
720 }
721 }
722
723 bus_register_notifier(&pci_bus_type, &dmar_pci_bus_nb);
Suresh Siddha1886e8a2008-07-10 11:16:37 -0700724 }
725
Jiang Liu2e455282014-02-19 14:07:36 +0800726 return dmar_dev_scope_status;
Suresh Siddha1886e8a2008-07-10 11:16:37 -0700727}
728
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -0700729
730int __init dmar_table_init(void)
731{
Suresh Siddha1886e8a2008-07-10 11:16:37 -0700732 static int dmar_table_initialized;
Fenghua Yu093f87d2007-11-21 15:07:14 -0800733 int ret;
734
Jiang Liucc053012014-01-06 14:18:24 +0800735 if (dmar_table_initialized == 0) {
736 ret = parse_dmar_table();
737 if (ret < 0) {
738 if (ret != -ENODEV)
739 pr_info("parse DMAR table failure.\n");
740 } else if (list_empty(&dmar_drhd_units)) {
741 pr_info("No DMAR devices found\n");
742 ret = -ENODEV;
743 }
Suresh Siddha1886e8a2008-07-10 11:16:37 -0700744
Jiang Liucc053012014-01-06 14:18:24 +0800745 if (ret < 0)
746 dmar_table_initialized = ret;
747 else
748 dmar_table_initialized = 1;
Fenghua Yu093f87d2007-11-21 15:07:14 -0800749 }
750
Jiang Liucc053012014-01-06 14:18:24 +0800751 return dmar_table_initialized < 0 ? dmar_table_initialized : 0;
Keshavamurthy, Anil S10e52472007-10-21 16:41:41 -0700752}
753
Ben Hutchings3a8663e2010-04-03 19:37:23 +0100754static void warn_invalid_dmar(u64 addr, const char *message)
755{
Ben Hutchingsfd0c8892010-04-03 19:38:43 +0100756 WARN_TAINT_ONCE(
757 1, TAINT_FIRMWARE_WORKAROUND,
758 "Your BIOS is broken; DMAR reported at address %llx%s!\n"
759 "BIOS vendor: %s; Ver: %s; Product Version: %s\n",
760 addr, message,
761 dmi_get_system_info(DMI_BIOS_VENDOR),
762 dmi_get_system_info(DMI_BIOS_VERSION),
763 dmi_get_system_info(DMI_PRODUCT_VERSION));
Ben Hutchings3a8663e2010-04-03 19:37:23 +0100764}
David Woodhouse6ecbf012009-12-02 09:20:27 +0000765
Rashika Kheria21004dc2013-12-18 12:01:46 +0530766static int __init check_zero_address(void)
David Woodhouse86cf8982009-11-09 22:15:15 +0000767{
768 struct acpi_table_dmar *dmar;
769 struct acpi_dmar_header *entry_header;
770 struct acpi_dmar_hardware_unit *drhd;
771
772 dmar = (struct acpi_table_dmar *)dmar_tbl;
773 entry_header = (struct acpi_dmar_header *)(dmar + 1);
774
775 while (((unsigned long)entry_header) <
776 (((unsigned long)dmar) + dmar_tbl->length)) {
777 /* Avoid looping forever on bad ACPI tables */
778 if (entry_header->length == 0) {
Donald Dutilee9071b02012-06-08 17:13:11 -0400779 pr_warn("Invalid 0-length structure\n");
David Woodhouse86cf8982009-11-09 22:15:15 +0000780 return 0;
781 }
782
783 if (entry_header->type == ACPI_DMAR_TYPE_HARDWARE_UNIT) {
Chris Wright2c992202009-12-02 09:17:13 +0000784 void __iomem *addr;
785 u64 cap, ecap;
786
David Woodhouse86cf8982009-11-09 22:15:15 +0000787 drhd = (void *)entry_header;
788 if (!drhd->address) {
Ben Hutchings3a8663e2010-04-03 19:37:23 +0100789 warn_invalid_dmar(0, "");
Chris Wright2c992202009-12-02 09:17:13 +0000790 goto failed;
David Woodhouse86cf8982009-11-09 22:15:15 +0000791 }
Chris Wright2c992202009-12-02 09:17:13 +0000792
793 addr = early_ioremap(drhd->address, VTD_PAGE_SIZE);
794 if (!addr ) {
795 printk("IOMMU: can't validate: %llx\n", drhd->address);
796 goto failed;
797 }
798 cap = dmar_readq(addr + DMAR_CAP_REG);
799 ecap = dmar_readq(addr + DMAR_ECAP_REG);
800 early_iounmap(addr, VTD_PAGE_SIZE);
801 if (cap == (uint64_t)-1 && ecap == (uint64_t)-1) {
Ben Hutchings3a8663e2010-04-03 19:37:23 +0100802 warn_invalid_dmar(drhd->address,
803 " returns all ones");
Chris Wright2c992202009-12-02 09:17:13 +0000804 goto failed;
805 }
David Woodhouse86cf8982009-11-09 22:15:15 +0000806 }
807
808 entry_header = ((void *)entry_header + entry_header->length);
809 }
810 return 1;
Chris Wright2c992202009-12-02 09:17:13 +0000811
812failed:
Chris Wright2c992202009-12-02 09:17:13 +0000813 return 0;
David Woodhouse86cf8982009-11-09 22:15:15 +0000814}
815
Konrad Rzeszutek Wilk480125b2010-08-26 13:57:57 -0400816int __init detect_intel_iommu(void)
Suresh Siddha2ae21012008-07-10 11:16:43 -0700817{
818 int ret;
819
Jiang Liu3a5670e2014-02-19 14:07:33 +0800820 down_write(&dmar_global_lock);
Yinghai Luf6dd5c32008-09-03 16:58:32 -0700821 ret = dmar_table_detect();
David Woodhouse86cf8982009-11-09 22:15:15 +0000822 if (ret)
823 ret = check_zero_address();
Suresh Siddha2ae21012008-07-10 11:16:43 -0700824 {
Linus Torvalds11bd04f2009-12-11 12:18:16 -0800825 if (ret && !no_iommu && !iommu_detected && !dmar_disabled) {
Suresh Siddha2ae21012008-07-10 11:16:43 -0700826 iommu_detected = 1;
Chris Wright5d990b62009-12-04 12:15:21 -0800827 /* Make sure ACS will be enabled */
828 pci_request_acs();
829 }
Suresh Siddhaf5d1b972011-08-23 17:05:22 -0700830
FUJITA Tomonori9d5ce732009-11-10 19:46:16 +0900831#ifdef CONFIG_X86
832 if (ret)
833 x86_init.iommu.iommu_init = intel_iommu_init;
834#endif
Youquan Songcacd4212008-10-16 16:31:57 -0700835 }
Jiang Liub707cb02014-01-06 14:18:26 +0800836 early_acpi_os_unmap_memory((void __iomem *)dmar_tbl, dmar_tbl_size);
Yinghai Luf6dd5c32008-09-03 16:58:32 -0700837 dmar_tbl = NULL;
Jiang Liu3a5670e2014-02-19 14:07:33 +0800838 up_write(&dmar_global_lock);
Konrad Rzeszutek Wilk480125b2010-08-26 13:57:57 -0400839
Konrad Rzeszutek Wilk4db77ff2010-08-26 13:58:04 -0400840 return ret ? 1 : -ENODEV;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700841}
842
843
Donald Dutile6f5cf522012-06-04 17:29:02 -0400844static void unmap_iommu(struct intel_iommu *iommu)
845{
846 iounmap(iommu->reg);
847 release_mem_region(iommu->reg_phys, iommu->reg_size);
848}
849
850/**
851 * map_iommu: map the iommu's registers
852 * @iommu: the iommu to map
853 * @phys_addr: the physical address of the base resgister
Donald Dutilee9071b02012-06-08 17:13:11 -0400854 *
Donald Dutile6f5cf522012-06-04 17:29:02 -0400855 * Memory map the iommu's registers. Start w/ a single page, and
Donald Dutilee9071b02012-06-08 17:13:11 -0400856 * possibly expand if that turns out to be insufficent.
Donald Dutile6f5cf522012-06-04 17:29:02 -0400857 */
858static int map_iommu(struct intel_iommu *iommu, u64 phys_addr)
859{
860 int map_size, err=0;
861
862 iommu->reg_phys = phys_addr;
863 iommu->reg_size = VTD_PAGE_SIZE;
864
865 if (!request_mem_region(iommu->reg_phys, iommu->reg_size, iommu->name)) {
866 pr_err("IOMMU: can't reserve memory\n");
867 err = -EBUSY;
868 goto out;
869 }
870
871 iommu->reg = ioremap(iommu->reg_phys, iommu->reg_size);
872 if (!iommu->reg) {
873 pr_err("IOMMU: can't map the region\n");
874 err = -ENOMEM;
875 goto release;
876 }
877
878 iommu->cap = dmar_readq(iommu->reg + DMAR_CAP_REG);
879 iommu->ecap = dmar_readq(iommu->reg + DMAR_ECAP_REG);
880
881 if (iommu->cap == (uint64_t)-1 && iommu->ecap == (uint64_t)-1) {
882 err = -EINVAL;
883 warn_invalid_dmar(phys_addr, " returns all ones");
884 goto unmap;
885 }
886
887 /* the registers might be more than one page */
888 map_size = max_t(int, ecap_max_iotlb_offset(iommu->ecap),
889 cap_max_fault_reg_offset(iommu->cap));
890 map_size = VTD_PAGE_ALIGN(map_size);
891 if (map_size > iommu->reg_size) {
892 iounmap(iommu->reg);
893 release_mem_region(iommu->reg_phys, iommu->reg_size);
894 iommu->reg_size = map_size;
895 if (!request_mem_region(iommu->reg_phys, iommu->reg_size,
896 iommu->name)) {
897 pr_err("IOMMU: can't reserve memory\n");
898 err = -EBUSY;
899 goto out;
900 }
901 iommu->reg = ioremap(iommu->reg_phys, iommu->reg_size);
902 if (!iommu->reg) {
903 pr_err("IOMMU: can't map the region\n");
904 err = -ENOMEM;
905 goto release;
906 }
907 }
908 err = 0;
909 goto out;
910
911unmap:
912 iounmap(iommu->reg);
913release:
914 release_mem_region(iommu->reg_phys, iommu->reg_size);
915out:
916 return err;
917}
918
Jiang Liu694835d2014-01-06 14:18:16 +0800919static int alloc_iommu(struct dmar_drhd_unit *drhd)
Suresh Siddhae61d98d2008-07-10 11:16:35 -0700920{
Suresh Siddhac42d9f32008-07-10 11:16:36 -0700921 struct intel_iommu *iommu;
Takao Indoh3a93c842013-04-23 17:35:03 +0900922 u32 ver, sts;
Suresh Siddhac42d9f32008-07-10 11:16:36 -0700923 static int iommu_allocated = 0;
Joerg Roedel43f73922009-01-03 23:56:27 +0100924 int agaw = 0;
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -0700925 int msagaw = 0;
Donald Dutile6f5cf522012-06-04 17:29:02 -0400926 int err;
Suresh Siddhac42d9f32008-07-10 11:16:36 -0700927
David Woodhouse6ecbf012009-12-02 09:20:27 +0000928 if (!drhd->reg_base_addr) {
Ben Hutchings3a8663e2010-04-03 19:37:23 +0100929 warn_invalid_dmar(0, "");
David Woodhouse6ecbf012009-12-02 09:20:27 +0000930 return -EINVAL;
931 }
932
Suresh Siddhac42d9f32008-07-10 11:16:36 -0700933 iommu = kzalloc(sizeof(*iommu), GFP_KERNEL);
934 if (!iommu)
Suresh Siddha1886e8a2008-07-10 11:16:37 -0700935 return -ENOMEM;
Suresh Siddhac42d9f32008-07-10 11:16:36 -0700936
937 iommu->seq_id = iommu_allocated++;
Suresh Siddha9d783ba2009-03-16 17:04:55 -0700938 sprintf (iommu->name, "dmar%d", iommu->seq_id);
Suresh Siddhae61d98d2008-07-10 11:16:35 -0700939
Donald Dutile6f5cf522012-06-04 17:29:02 -0400940 err = map_iommu(iommu, drhd->reg_base_addr);
941 if (err) {
942 pr_err("IOMMU: failed to map %s\n", iommu->name);
Suresh Siddhae61d98d2008-07-10 11:16:35 -0700943 goto error;
944 }
Suresh Siddhae61d98d2008-07-10 11:16:35 -0700945
Donald Dutile6f5cf522012-06-04 17:29:02 -0400946 err = -EINVAL;
Weidong Han1b573682008-12-08 15:34:06 +0800947 agaw = iommu_calculate_agaw(iommu);
948 if (agaw < 0) {
Donald Dutilebf947fcb2012-06-04 17:29:01 -0400949 pr_err("Cannot get a valid agaw for iommu (seq_id = %d)\n",
950 iommu->seq_id);
David Woodhouse08155652009-08-04 09:17:20 +0100951 goto err_unmap;
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -0700952 }
953 msagaw = iommu_calculate_max_sagaw(iommu);
954 if (msagaw < 0) {
Donald Dutilebf947fcb2012-06-04 17:29:01 -0400955 pr_err("Cannot get a valid max agaw for iommu (seq_id = %d)\n",
Weidong Han1b573682008-12-08 15:34:06 +0800956 iommu->seq_id);
David Woodhouse08155652009-08-04 09:17:20 +0100957 goto err_unmap;
Weidong Han1b573682008-12-08 15:34:06 +0800958 }
959 iommu->agaw = agaw;
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -0700960 iommu->msagaw = msagaw;
David Woodhouse67ccac42014-03-09 13:49:45 -0700961 iommu->segment = drhd->segment;
Weidong Han1b573682008-12-08 15:34:06 +0800962
Suresh Siddhaee34b322009-10-02 11:01:21 -0700963 iommu->node = -1;
964
Suresh Siddhae61d98d2008-07-10 11:16:35 -0700965 ver = readl(iommu->reg + DMAR_VER_REG);
Yinghai Lu680a7522010-04-08 19:58:23 +0100966 pr_info("IOMMU %d: reg_base_addr %llx ver %d:%d cap %llx ecap %llx\n",
967 iommu->seq_id,
Fenghua Yu5b6985c2008-10-16 18:02:32 -0700968 (unsigned long long)drhd->reg_base_addr,
969 DMAR_VER_MAJOR(ver), DMAR_VER_MINOR(ver),
970 (unsigned long long)iommu->cap,
971 (unsigned long long)iommu->ecap);
Suresh Siddhae61d98d2008-07-10 11:16:35 -0700972
Takao Indoh3a93c842013-04-23 17:35:03 +0900973 /* Reflect status in gcmd */
974 sts = readl(iommu->reg + DMAR_GSTS_REG);
975 if (sts & DMA_GSTS_IRES)
976 iommu->gcmd |= DMA_GCMD_IRE;
977 if (sts & DMA_GSTS_TES)
978 iommu->gcmd |= DMA_GCMD_TE;
979 if (sts & DMA_GSTS_QIES)
980 iommu->gcmd |= DMA_GCMD_QIE;
981
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +0200982 raw_spin_lock_init(&iommu->register_lock);
Suresh Siddhae61d98d2008-07-10 11:16:35 -0700983
984 drhd->iommu = iommu;
Alex Williamsona5459cf2014-06-12 16:12:31 -0600985
986 if (intel_iommu_enabled)
987 iommu->iommu_dev = iommu_device_create(NULL, iommu,
988 intel_iommu_groups,
989 iommu->name);
990
Suresh Siddha1886e8a2008-07-10 11:16:37 -0700991 return 0;
David Woodhouse08155652009-08-04 09:17:20 +0100992
993 err_unmap:
Donald Dutile6f5cf522012-06-04 17:29:02 -0400994 unmap_iommu(iommu);
David Woodhouse08155652009-08-04 09:17:20 +0100995 error:
Suresh Siddhae61d98d2008-07-10 11:16:35 -0700996 kfree(iommu);
Donald Dutile6f5cf522012-06-04 17:29:02 -0400997 return err;
Suresh Siddhae61d98d2008-07-10 11:16:35 -0700998}
999
Jiang Liua868e6b2014-01-06 14:18:20 +08001000static void free_iommu(struct intel_iommu *iommu)
Suresh Siddhae61d98d2008-07-10 11:16:35 -07001001{
Alex Williamsona5459cf2014-06-12 16:12:31 -06001002 iommu_device_destroy(iommu->iommu_dev);
1003
Jiang Liua868e6b2014-01-06 14:18:20 +08001004 if (iommu->irq) {
1005 free_irq(iommu->irq, iommu);
1006 irq_set_handler_data(iommu->irq, NULL);
Thomas Gleixnera553b142014-05-07 15:44:11 +00001007 dmar_free_hwirq(iommu->irq);
Jiang Liua868e6b2014-01-06 14:18:20 +08001008 }
Suresh Siddhae61d98d2008-07-10 11:16:35 -07001009
Jiang Liua84da702014-01-06 14:18:23 +08001010 if (iommu->qi) {
1011 free_page((unsigned long)iommu->qi->desc);
1012 kfree(iommu->qi->desc_status);
1013 kfree(iommu->qi);
1014 }
1015
Suresh Siddhae61d98d2008-07-10 11:16:35 -07001016 if (iommu->reg)
Donald Dutile6f5cf522012-06-04 17:29:02 -04001017 unmap_iommu(iommu);
1018
Suresh Siddhae61d98d2008-07-10 11:16:35 -07001019 kfree(iommu);
1020}
Suresh Siddhafe962e92008-07-10 11:16:42 -07001021
1022/*
1023 * Reclaim all the submitted descriptors which have completed its work.
1024 */
1025static inline void reclaim_free_desc(struct q_inval *qi)
1026{
Yu Zhao6ba6c3a2009-05-18 13:51:35 +08001027 while (qi->desc_status[qi->free_tail] == QI_DONE ||
1028 qi->desc_status[qi->free_tail] == QI_ABORT) {
Suresh Siddhafe962e92008-07-10 11:16:42 -07001029 qi->desc_status[qi->free_tail] = QI_FREE;
1030 qi->free_tail = (qi->free_tail + 1) % QI_LENGTH;
1031 qi->free_cnt++;
1032 }
1033}
1034
Yu Zhao704126a2009-01-04 16:28:52 +08001035static int qi_check_fault(struct intel_iommu *iommu, int index)
1036{
1037 u32 fault;
Yu Zhao6ba6c3a2009-05-18 13:51:35 +08001038 int head, tail;
Yu Zhao704126a2009-01-04 16:28:52 +08001039 struct q_inval *qi = iommu->qi;
1040 int wait_index = (index + 1) % QI_LENGTH;
1041
Yu Zhao6ba6c3a2009-05-18 13:51:35 +08001042 if (qi->desc_status[wait_index] == QI_ABORT)
1043 return -EAGAIN;
1044
Yu Zhao704126a2009-01-04 16:28:52 +08001045 fault = readl(iommu->reg + DMAR_FSTS_REG);
1046
1047 /*
1048 * If IQE happens, the head points to the descriptor associated
1049 * with the error. No new descriptors are fetched until the IQE
1050 * is cleared.
1051 */
1052 if (fault & DMA_FSTS_IQE) {
1053 head = readl(iommu->reg + DMAR_IQH_REG);
Yu Zhao6ba6c3a2009-05-18 13:51:35 +08001054 if ((head >> DMAR_IQ_SHIFT) == index) {
Donald Dutilebf947fcb2012-06-04 17:29:01 -04001055 pr_err("VT-d detected invalid descriptor: "
Yu Zhao6ba6c3a2009-05-18 13:51:35 +08001056 "low=%llx, high=%llx\n",
1057 (unsigned long long)qi->desc[index].low,
1058 (unsigned long long)qi->desc[index].high);
Yu Zhao704126a2009-01-04 16:28:52 +08001059 memcpy(&qi->desc[index], &qi->desc[wait_index],
1060 sizeof(struct qi_desc));
1061 __iommu_flush_cache(iommu, &qi->desc[index],
1062 sizeof(struct qi_desc));
1063 writel(DMA_FSTS_IQE, iommu->reg + DMAR_FSTS_REG);
1064 return -EINVAL;
1065 }
1066 }
1067
Yu Zhao6ba6c3a2009-05-18 13:51:35 +08001068 /*
1069 * If ITE happens, all pending wait_desc commands are aborted.
1070 * No new descriptors are fetched until the ITE is cleared.
1071 */
1072 if (fault & DMA_FSTS_ITE) {
1073 head = readl(iommu->reg + DMAR_IQH_REG);
1074 head = ((head >> DMAR_IQ_SHIFT) - 1 + QI_LENGTH) % QI_LENGTH;
1075 head |= 1;
1076 tail = readl(iommu->reg + DMAR_IQT_REG);
1077 tail = ((tail >> DMAR_IQ_SHIFT) - 1 + QI_LENGTH) % QI_LENGTH;
1078
1079 writel(DMA_FSTS_ITE, iommu->reg + DMAR_FSTS_REG);
1080
1081 do {
1082 if (qi->desc_status[head] == QI_IN_USE)
1083 qi->desc_status[head] = QI_ABORT;
1084 head = (head - 2 + QI_LENGTH) % QI_LENGTH;
1085 } while (head != tail);
1086
1087 if (qi->desc_status[wait_index] == QI_ABORT)
1088 return -EAGAIN;
1089 }
1090
1091 if (fault & DMA_FSTS_ICE)
1092 writel(DMA_FSTS_ICE, iommu->reg + DMAR_FSTS_REG);
1093
Yu Zhao704126a2009-01-04 16:28:52 +08001094 return 0;
1095}
1096
Suresh Siddhafe962e92008-07-10 11:16:42 -07001097/*
1098 * Submit the queued invalidation descriptor to the remapping
1099 * hardware unit and wait for its completion.
1100 */
Yu Zhao704126a2009-01-04 16:28:52 +08001101int qi_submit_sync(struct qi_desc *desc, struct intel_iommu *iommu)
Suresh Siddhafe962e92008-07-10 11:16:42 -07001102{
Yu Zhao6ba6c3a2009-05-18 13:51:35 +08001103 int rc;
Suresh Siddhafe962e92008-07-10 11:16:42 -07001104 struct q_inval *qi = iommu->qi;
1105 struct qi_desc *hw, wait_desc;
1106 int wait_index, index;
1107 unsigned long flags;
1108
1109 if (!qi)
Yu Zhao704126a2009-01-04 16:28:52 +08001110 return 0;
Suresh Siddhafe962e92008-07-10 11:16:42 -07001111
1112 hw = qi->desc;
1113
Yu Zhao6ba6c3a2009-05-18 13:51:35 +08001114restart:
1115 rc = 0;
1116
Thomas Gleixner3b8f4042011-07-19 17:02:07 +02001117 raw_spin_lock_irqsave(&qi->q_lock, flags);
Suresh Siddhafe962e92008-07-10 11:16:42 -07001118 while (qi->free_cnt < 3) {
Thomas Gleixner3b8f4042011-07-19 17:02:07 +02001119 raw_spin_unlock_irqrestore(&qi->q_lock, flags);
Suresh Siddhafe962e92008-07-10 11:16:42 -07001120 cpu_relax();
Thomas Gleixner3b8f4042011-07-19 17:02:07 +02001121 raw_spin_lock_irqsave(&qi->q_lock, flags);
Suresh Siddhafe962e92008-07-10 11:16:42 -07001122 }
1123
1124 index = qi->free_head;
1125 wait_index = (index + 1) % QI_LENGTH;
1126
1127 qi->desc_status[index] = qi->desc_status[wait_index] = QI_IN_USE;
1128
1129 hw[index] = *desc;
1130
Yu Zhao704126a2009-01-04 16:28:52 +08001131 wait_desc.low = QI_IWD_STATUS_DATA(QI_DONE) |
1132 QI_IWD_STATUS_WRITE | QI_IWD_TYPE;
Suresh Siddhafe962e92008-07-10 11:16:42 -07001133 wait_desc.high = virt_to_phys(&qi->desc_status[wait_index]);
1134
1135 hw[wait_index] = wait_desc;
1136
1137 __iommu_flush_cache(iommu, &hw[index], sizeof(struct qi_desc));
1138 __iommu_flush_cache(iommu, &hw[wait_index], sizeof(struct qi_desc));
1139
1140 qi->free_head = (qi->free_head + 2) % QI_LENGTH;
1141 qi->free_cnt -= 2;
1142
Suresh Siddhafe962e92008-07-10 11:16:42 -07001143 /*
1144 * update the HW tail register indicating the presence of
1145 * new descriptors.
1146 */
Yu Zhao6ba6c3a2009-05-18 13:51:35 +08001147 writel(qi->free_head << DMAR_IQ_SHIFT, iommu->reg + DMAR_IQT_REG);
Suresh Siddhafe962e92008-07-10 11:16:42 -07001148
1149 while (qi->desc_status[wait_index] != QI_DONE) {
Suresh Siddhaf05810c2008-10-16 16:31:54 -07001150 /*
1151 * We will leave the interrupts disabled, to prevent interrupt
1152 * context to queue another cmd while a cmd is already submitted
1153 * and waiting for completion on this cpu. This is to avoid
1154 * a deadlock where the interrupt context can wait indefinitely
1155 * for free slots in the queue.
1156 */
Yu Zhao704126a2009-01-04 16:28:52 +08001157 rc = qi_check_fault(iommu, index);
1158 if (rc)
Yu Zhao6ba6c3a2009-05-18 13:51:35 +08001159 break;
Yu Zhao704126a2009-01-04 16:28:52 +08001160
Thomas Gleixner3b8f4042011-07-19 17:02:07 +02001161 raw_spin_unlock(&qi->q_lock);
Suresh Siddhafe962e92008-07-10 11:16:42 -07001162 cpu_relax();
Thomas Gleixner3b8f4042011-07-19 17:02:07 +02001163 raw_spin_lock(&qi->q_lock);
Suresh Siddhafe962e92008-07-10 11:16:42 -07001164 }
Yu Zhao6ba6c3a2009-05-18 13:51:35 +08001165
1166 qi->desc_status[index] = QI_DONE;
Suresh Siddhafe962e92008-07-10 11:16:42 -07001167
1168 reclaim_free_desc(qi);
Thomas Gleixner3b8f4042011-07-19 17:02:07 +02001169 raw_spin_unlock_irqrestore(&qi->q_lock, flags);
Yu Zhao704126a2009-01-04 16:28:52 +08001170
Yu Zhao6ba6c3a2009-05-18 13:51:35 +08001171 if (rc == -EAGAIN)
1172 goto restart;
1173
Yu Zhao704126a2009-01-04 16:28:52 +08001174 return rc;
Suresh Siddhafe962e92008-07-10 11:16:42 -07001175}
1176
1177/*
1178 * Flush the global interrupt entry cache.
1179 */
1180void qi_global_iec(struct intel_iommu *iommu)
1181{
1182 struct qi_desc desc;
1183
1184 desc.low = QI_IEC_TYPE;
1185 desc.high = 0;
1186
Yu Zhao704126a2009-01-04 16:28:52 +08001187 /* should never fail */
Suresh Siddhafe962e92008-07-10 11:16:42 -07001188 qi_submit_sync(&desc, iommu);
1189}
1190
David Woodhouse4c25a2c2009-05-10 17:16:06 +01001191void qi_flush_context(struct intel_iommu *iommu, u16 did, u16 sid, u8 fm,
1192 u64 type)
Youquan Song3481f212008-10-16 16:31:55 -07001193{
Youquan Song3481f212008-10-16 16:31:55 -07001194 struct qi_desc desc;
1195
Youquan Song3481f212008-10-16 16:31:55 -07001196 desc.low = QI_CC_FM(fm) | QI_CC_SID(sid) | QI_CC_DID(did)
1197 | QI_CC_GRAN(type) | QI_CC_TYPE;
1198 desc.high = 0;
1199
David Woodhouse4c25a2c2009-05-10 17:16:06 +01001200 qi_submit_sync(&desc, iommu);
Youquan Song3481f212008-10-16 16:31:55 -07001201}
1202
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01001203void qi_flush_iotlb(struct intel_iommu *iommu, u16 did, u64 addr,
1204 unsigned int size_order, u64 type)
Youquan Song3481f212008-10-16 16:31:55 -07001205{
1206 u8 dw = 0, dr = 0;
1207
1208 struct qi_desc desc;
1209 int ih = 0;
1210
Youquan Song3481f212008-10-16 16:31:55 -07001211 if (cap_write_drain(iommu->cap))
1212 dw = 1;
1213
1214 if (cap_read_drain(iommu->cap))
1215 dr = 1;
1216
1217 desc.low = QI_IOTLB_DID(did) | QI_IOTLB_DR(dr) | QI_IOTLB_DW(dw)
1218 | QI_IOTLB_GRAN(type) | QI_IOTLB_TYPE;
1219 desc.high = QI_IOTLB_ADDR(addr) | QI_IOTLB_IH(ih)
1220 | QI_IOTLB_AM(size_order);
1221
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01001222 qi_submit_sync(&desc, iommu);
Youquan Song3481f212008-10-16 16:31:55 -07001223}
1224
Yu Zhao6ba6c3a2009-05-18 13:51:35 +08001225void qi_flush_dev_iotlb(struct intel_iommu *iommu, u16 sid, u16 qdep,
1226 u64 addr, unsigned mask)
1227{
1228 struct qi_desc desc;
1229
1230 if (mask) {
1231 BUG_ON(addr & ((1 << (VTD_PAGE_SHIFT + mask)) - 1));
1232 addr |= (1 << (VTD_PAGE_SHIFT + mask - 1)) - 1;
1233 desc.high = QI_DEV_IOTLB_ADDR(addr) | QI_DEV_IOTLB_SIZE;
1234 } else
1235 desc.high = QI_DEV_IOTLB_ADDR(addr);
1236
1237 if (qdep >= QI_DEV_IOTLB_MAX_INVS)
1238 qdep = 0;
1239
1240 desc.low = QI_DEV_IOTLB_SID(sid) | QI_DEV_IOTLB_QDEP(qdep) |
1241 QI_DIOTLB_TYPE;
1242
1243 qi_submit_sync(&desc, iommu);
1244}
1245
Suresh Siddhafe962e92008-07-10 11:16:42 -07001246/*
Suresh Siddhaeba67e52009-03-16 17:04:56 -07001247 * Disable Queued Invalidation interface.
1248 */
1249void dmar_disable_qi(struct intel_iommu *iommu)
1250{
1251 unsigned long flags;
1252 u32 sts;
1253 cycles_t start_time = get_cycles();
1254
1255 if (!ecap_qis(iommu->ecap))
1256 return;
1257
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001258 raw_spin_lock_irqsave(&iommu->register_lock, flags);
Suresh Siddhaeba67e52009-03-16 17:04:56 -07001259
1260 sts = dmar_readq(iommu->reg + DMAR_GSTS_REG);
1261 if (!(sts & DMA_GSTS_QIES))
1262 goto end;
1263
1264 /*
1265 * Give a chance to HW to complete the pending invalidation requests.
1266 */
1267 while ((readl(iommu->reg + DMAR_IQT_REG) !=
1268 readl(iommu->reg + DMAR_IQH_REG)) &&
1269 (DMAR_OPERATION_TIMEOUT > (get_cycles() - start_time)))
1270 cpu_relax();
1271
1272 iommu->gcmd &= ~DMA_GCMD_QIE;
Suresh Siddhaeba67e52009-03-16 17:04:56 -07001273 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
1274
1275 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG, readl,
1276 !(sts & DMA_GSTS_QIES), sts);
1277end:
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001278 raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
Suresh Siddhaeba67e52009-03-16 17:04:56 -07001279}
1280
1281/*
Fenghua Yueb4a52b2009-03-27 14:22:43 -07001282 * Enable queued invalidation.
1283 */
1284static void __dmar_enable_qi(struct intel_iommu *iommu)
1285{
David Woodhousec416daa2009-05-10 20:30:58 +01001286 u32 sts;
Fenghua Yueb4a52b2009-03-27 14:22:43 -07001287 unsigned long flags;
1288 struct q_inval *qi = iommu->qi;
1289
1290 qi->free_head = qi->free_tail = 0;
1291 qi->free_cnt = QI_LENGTH;
1292
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001293 raw_spin_lock_irqsave(&iommu->register_lock, flags);
Fenghua Yueb4a52b2009-03-27 14:22:43 -07001294
1295 /* write zero to the tail reg */
1296 writel(0, iommu->reg + DMAR_IQT_REG);
1297
1298 dmar_writeq(iommu->reg + DMAR_IQA_REG, virt_to_phys(qi->desc));
1299
Fenghua Yueb4a52b2009-03-27 14:22:43 -07001300 iommu->gcmd |= DMA_GCMD_QIE;
David Woodhousec416daa2009-05-10 20:30:58 +01001301 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
Fenghua Yueb4a52b2009-03-27 14:22:43 -07001302
1303 /* Make sure hardware complete it */
1304 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG, readl, (sts & DMA_GSTS_QIES), sts);
1305
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001306 raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
Fenghua Yueb4a52b2009-03-27 14:22:43 -07001307}
1308
1309/*
Suresh Siddhafe962e92008-07-10 11:16:42 -07001310 * Enable Queued Invalidation interface. This is a must to support
1311 * interrupt-remapping. Also used by DMA-remapping, which replaces
1312 * register based IOTLB invalidation.
1313 */
1314int dmar_enable_qi(struct intel_iommu *iommu)
1315{
Suresh Siddhafe962e92008-07-10 11:16:42 -07001316 struct q_inval *qi;
Suresh Siddha751cafe2009-10-02 11:01:22 -07001317 struct page *desc_page;
Suresh Siddhafe962e92008-07-10 11:16:42 -07001318
1319 if (!ecap_qis(iommu->ecap))
1320 return -ENOENT;
1321
1322 /*
1323 * queued invalidation is already setup and enabled.
1324 */
1325 if (iommu->qi)
1326 return 0;
1327
Suresh Siddhafa4b57c2009-03-16 17:05:05 -07001328 iommu->qi = kmalloc(sizeof(*qi), GFP_ATOMIC);
Suresh Siddhafe962e92008-07-10 11:16:42 -07001329 if (!iommu->qi)
1330 return -ENOMEM;
1331
1332 qi = iommu->qi;
1333
Suresh Siddha751cafe2009-10-02 11:01:22 -07001334
1335 desc_page = alloc_pages_node(iommu->node, GFP_ATOMIC | __GFP_ZERO, 0);
1336 if (!desc_page) {
Suresh Siddhafe962e92008-07-10 11:16:42 -07001337 kfree(qi);
Jiang Liub707cb02014-01-06 14:18:26 +08001338 iommu->qi = NULL;
Suresh Siddhafe962e92008-07-10 11:16:42 -07001339 return -ENOMEM;
1340 }
1341
Suresh Siddha751cafe2009-10-02 11:01:22 -07001342 qi->desc = page_address(desc_page);
1343
Hannes Reinecke37a40712013-02-06 09:50:10 +01001344 qi->desc_status = kzalloc(QI_LENGTH * sizeof(int), GFP_ATOMIC);
Suresh Siddhafe962e92008-07-10 11:16:42 -07001345 if (!qi->desc_status) {
1346 free_page((unsigned long) qi->desc);
1347 kfree(qi);
Jiang Liub707cb02014-01-06 14:18:26 +08001348 iommu->qi = NULL;
Suresh Siddhafe962e92008-07-10 11:16:42 -07001349 return -ENOMEM;
1350 }
1351
Thomas Gleixner3b8f4042011-07-19 17:02:07 +02001352 raw_spin_lock_init(&qi->q_lock);
Suresh Siddhafe962e92008-07-10 11:16:42 -07001353
Fenghua Yueb4a52b2009-03-27 14:22:43 -07001354 __dmar_enable_qi(iommu);
Suresh Siddhafe962e92008-07-10 11:16:42 -07001355
1356 return 0;
1357}
Suresh Siddha0ac24912009-03-16 17:04:54 -07001358
1359/* iommu interrupt handling. Most stuff are MSI-like. */
1360
Suresh Siddha9d783ba2009-03-16 17:04:55 -07001361enum faulttype {
1362 DMA_REMAP,
1363 INTR_REMAP,
1364 UNKNOWN,
1365};
1366
1367static const char *dma_remap_fault_reasons[] =
Suresh Siddha0ac24912009-03-16 17:04:54 -07001368{
1369 "Software",
1370 "Present bit in root entry is clear",
1371 "Present bit in context entry is clear",
1372 "Invalid context entry",
1373 "Access beyond MGAW",
1374 "PTE Write access is not set",
1375 "PTE Read access is not set",
1376 "Next page table ptr is invalid",
1377 "Root table address invalid",
1378 "Context table ptr is invalid",
1379 "non-zero reserved fields in RTP",
1380 "non-zero reserved fields in CTP",
1381 "non-zero reserved fields in PTE",
Li, Zhen-Hua4ecccd92013-03-06 10:43:17 +08001382 "PCE for translation request specifies blocking",
Suresh Siddha0ac24912009-03-16 17:04:54 -07001383};
Suresh Siddha9d783ba2009-03-16 17:04:55 -07001384
Suresh Siddha95a02e92012-03-30 11:47:07 -07001385static const char *irq_remap_fault_reasons[] =
Suresh Siddha9d783ba2009-03-16 17:04:55 -07001386{
1387 "Detected reserved fields in the decoded interrupt-remapped request",
1388 "Interrupt index exceeded the interrupt-remapping table size",
1389 "Present field in the IRTE entry is clear",
1390 "Error accessing interrupt-remapping table pointed by IRTA_REG",
1391 "Detected reserved fields in the IRTE entry",
1392 "Blocked a compatibility format interrupt request",
1393 "Blocked an interrupt request due to source-id verification failure",
1394};
1395
Rashika Kheria21004dc2013-12-18 12:01:46 +05301396static const char *dmar_get_fault_reason(u8 fault_reason, int *fault_type)
Suresh Siddha0ac24912009-03-16 17:04:54 -07001397{
Dan Carpenterfefe1ed2012-05-13 20:09:38 +03001398 if (fault_reason >= 0x20 && (fault_reason - 0x20 <
1399 ARRAY_SIZE(irq_remap_fault_reasons))) {
Suresh Siddha9d783ba2009-03-16 17:04:55 -07001400 *fault_type = INTR_REMAP;
Suresh Siddha95a02e92012-03-30 11:47:07 -07001401 return irq_remap_fault_reasons[fault_reason - 0x20];
Suresh Siddha9d783ba2009-03-16 17:04:55 -07001402 } else if (fault_reason < ARRAY_SIZE(dma_remap_fault_reasons)) {
1403 *fault_type = DMA_REMAP;
1404 return dma_remap_fault_reasons[fault_reason];
1405 } else {
1406 *fault_type = UNKNOWN;
Suresh Siddha0ac24912009-03-16 17:04:54 -07001407 return "Unknown";
Suresh Siddha9d783ba2009-03-16 17:04:55 -07001408 }
Suresh Siddha0ac24912009-03-16 17:04:54 -07001409}
1410
Thomas Gleixner5c2837f2010-09-28 17:15:11 +02001411void dmar_msi_unmask(struct irq_data *data)
Suresh Siddha0ac24912009-03-16 17:04:54 -07001412{
Thomas Gleixnerdced35a2011-03-28 17:49:12 +02001413 struct intel_iommu *iommu = irq_data_get_irq_handler_data(data);
Suresh Siddha0ac24912009-03-16 17:04:54 -07001414 unsigned long flag;
1415
1416 /* unmask it */
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001417 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Suresh Siddha0ac24912009-03-16 17:04:54 -07001418 writel(0, iommu->reg + DMAR_FECTL_REG);
1419 /* Read a reg to force flush the post write */
1420 readl(iommu->reg + DMAR_FECTL_REG);
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001421 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Suresh Siddha0ac24912009-03-16 17:04:54 -07001422}
1423
Thomas Gleixner5c2837f2010-09-28 17:15:11 +02001424void dmar_msi_mask(struct irq_data *data)
Suresh Siddha0ac24912009-03-16 17:04:54 -07001425{
1426 unsigned long flag;
Thomas Gleixnerdced35a2011-03-28 17:49:12 +02001427 struct intel_iommu *iommu = irq_data_get_irq_handler_data(data);
Suresh Siddha0ac24912009-03-16 17:04:54 -07001428
1429 /* mask it */
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001430 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Suresh Siddha0ac24912009-03-16 17:04:54 -07001431 writel(DMA_FECTL_IM, iommu->reg + DMAR_FECTL_REG);
1432 /* Read a reg to force flush the post write */
1433 readl(iommu->reg + DMAR_FECTL_REG);
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001434 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Suresh Siddha0ac24912009-03-16 17:04:54 -07001435}
1436
1437void dmar_msi_write(int irq, struct msi_msg *msg)
1438{
Thomas Gleixnerdced35a2011-03-28 17:49:12 +02001439 struct intel_iommu *iommu = irq_get_handler_data(irq);
Suresh Siddha0ac24912009-03-16 17:04:54 -07001440 unsigned long flag;
1441
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001442 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Suresh Siddha0ac24912009-03-16 17:04:54 -07001443 writel(msg->data, iommu->reg + DMAR_FEDATA_REG);
1444 writel(msg->address_lo, iommu->reg + DMAR_FEADDR_REG);
1445 writel(msg->address_hi, iommu->reg + DMAR_FEUADDR_REG);
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001446 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Suresh Siddha0ac24912009-03-16 17:04:54 -07001447}
1448
1449void dmar_msi_read(int irq, struct msi_msg *msg)
1450{
Thomas Gleixnerdced35a2011-03-28 17:49:12 +02001451 struct intel_iommu *iommu = irq_get_handler_data(irq);
Suresh Siddha0ac24912009-03-16 17:04:54 -07001452 unsigned long flag;
1453
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001454 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Suresh Siddha0ac24912009-03-16 17:04:54 -07001455 msg->data = readl(iommu->reg + DMAR_FEDATA_REG);
1456 msg->address_lo = readl(iommu->reg + DMAR_FEADDR_REG);
1457 msg->address_hi = readl(iommu->reg + DMAR_FEUADDR_REG);
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001458 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Suresh Siddha0ac24912009-03-16 17:04:54 -07001459}
1460
1461static int dmar_fault_do_one(struct intel_iommu *iommu, int type,
1462 u8 fault_reason, u16 source_id, unsigned long long addr)
1463{
1464 const char *reason;
Suresh Siddha9d783ba2009-03-16 17:04:55 -07001465 int fault_type;
Suresh Siddha0ac24912009-03-16 17:04:54 -07001466
Suresh Siddha9d783ba2009-03-16 17:04:55 -07001467 reason = dmar_get_fault_reason(fault_reason, &fault_type);
Suresh Siddha0ac24912009-03-16 17:04:54 -07001468
Suresh Siddha9d783ba2009-03-16 17:04:55 -07001469 if (fault_type == INTR_REMAP)
Donald Dutilebf947fcb2012-06-04 17:29:01 -04001470 pr_err("INTR-REMAP: Request device [[%02x:%02x.%d] "
Suresh Siddha9d783ba2009-03-16 17:04:55 -07001471 "fault index %llx\n"
1472 "INTR-REMAP:[fault reason %02d] %s\n",
1473 (source_id >> 8), PCI_SLOT(source_id & 0xFF),
1474 PCI_FUNC(source_id & 0xFF), addr >> 48,
1475 fault_reason, reason);
1476 else
Donald Dutilebf947fcb2012-06-04 17:29:01 -04001477 pr_err("DMAR:[%s] Request device [%02x:%02x.%d] "
Suresh Siddha9d783ba2009-03-16 17:04:55 -07001478 "fault addr %llx \n"
1479 "DMAR:[fault reason %02d] %s\n",
1480 (type ? "DMA Read" : "DMA Write"),
1481 (source_id >> 8), PCI_SLOT(source_id & 0xFF),
1482 PCI_FUNC(source_id & 0xFF), addr, fault_reason, reason);
Suresh Siddha0ac24912009-03-16 17:04:54 -07001483 return 0;
1484}
1485
1486#define PRIMARY_FAULT_REG_LEN (16)
Suresh Siddha1531a6a2009-03-16 17:04:57 -07001487irqreturn_t dmar_fault(int irq, void *dev_id)
Suresh Siddha0ac24912009-03-16 17:04:54 -07001488{
1489 struct intel_iommu *iommu = dev_id;
1490 int reg, fault_index;
1491 u32 fault_status;
1492 unsigned long flag;
1493
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001494 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Suresh Siddha0ac24912009-03-16 17:04:54 -07001495 fault_status = readl(iommu->reg + DMAR_FSTS_REG);
Suresh Siddha9d783ba2009-03-16 17:04:55 -07001496 if (fault_status)
Donald Dutilebf947fcb2012-06-04 17:29:01 -04001497 pr_err("DRHD: handling fault status reg %x\n", fault_status);
Suresh Siddha0ac24912009-03-16 17:04:54 -07001498
1499 /* TBD: ignore advanced fault log currently */
1500 if (!(fault_status & DMA_FSTS_PPF))
Li, Zhen-Huabd5cdad2013-03-25 16:20:52 +08001501 goto unlock_exit;
Suresh Siddha0ac24912009-03-16 17:04:54 -07001502
1503 fault_index = dma_fsts_fault_record_index(fault_status);
1504 reg = cap_fault_reg_offset(iommu->cap);
1505 while (1) {
1506 u8 fault_reason;
1507 u16 source_id;
1508 u64 guest_addr;
1509 int type;
1510 u32 data;
1511
1512 /* highest 32 bits */
1513 data = readl(iommu->reg + reg +
1514 fault_index * PRIMARY_FAULT_REG_LEN + 12);
1515 if (!(data & DMA_FRCD_F))
1516 break;
1517
1518 fault_reason = dma_frcd_fault_reason(data);
1519 type = dma_frcd_type(data);
1520
1521 data = readl(iommu->reg + reg +
1522 fault_index * PRIMARY_FAULT_REG_LEN + 8);
1523 source_id = dma_frcd_source_id(data);
1524
1525 guest_addr = dmar_readq(iommu->reg + reg +
1526 fault_index * PRIMARY_FAULT_REG_LEN);
1527 guest_addr = dma_frcd_page_addr(guest_addr);
1528 /* clear the fault */
1529 writel(DMA_FRCD_F, iommu->reg + reg +
1530 fault_index * PRIMARY_FAULT_REG_LEN + 12);
1531
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001532 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Suresh Siddha0ac24912009-03-16 17:04:54 -07001533
1534 dmar_fault_do_one(iommu, type, fault_reason,
1535 source_id, guest_addr);
1536
1537 fault_index++;
Troy Heber8211a7b2009-08-19 15:26:11 -06001538 if (fault_index >= cap_num_fault_regs(iommu->cap))
Suresh Siddha0ac24912009-03-16 17:04:54 -07001539 fault_index = 0;
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001540 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Suresh Siddha0ac24912009-03-16 17:04:54 -07001541 }
Suresh Siddha0ac24912009-03-16 17:04:54 -07001542
Li, Zhen-Huabd5cdad2013-03-25 16:20:52 +08001543 writel(DMA_FSTS_PFO | DMA_FSTS_PPF, iommu->reg + DMAR_FSTS_REG);
1544
1545unlock_exit:
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001546 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Suresh Siddha0ac24912009-03-16 17:04:54 -07001547 return IRQ_HANDLED;
1548}
1549
1550int dmar_set_interrupt(struct intel_iommu *iommu)
1551{
1552 int irq, ret;
1553
Suresh Siddha9d783ba2009-03-16 17:04:55 -07001554 /*
1555 * Check if the fault interrupt is already initialized.
1556 */
1557 if (iommu->irq)
1558 return 0;
1559
Thomas Gleixnera553b142014-05-07 15:44:11 +00001560 irq = dmar_alloc_hwirq();
Thomas Gleixneraa5125a2014-05-07 15:44:10 +00001561 if (irq <= 0) {
Donald Dutilebf947fcb2012-06-04 17:29:01 -04001562 pr_err("IOMMU: no free vectors\n");
Suresh Siddha0ac24912009-03-16 17:04:54 -07001563 return -EINVAL;
1564 }
1565
Thomas Gleixnerdced35a2011-03-28 17:49:12 +02001566 irq_set_handler_data(irq, iommu);
Suresh Siddha0ac24912009-03-16 17:04:54 -07001567 iommu->irq = irq;
1568
1569 ret = arch_setup_dmar_msi(irq);
1570 if (ret) {
Thomas Gleixnerdced35a2011-03-28 17:49:12 +02001571 irq_set_handler_data(irq, NULL);
Suresh Siddha0ac24912009-03-16 17:04:54 -07001572 iommu->irq = 0;
Thomas Gleixnera553b142014-05-07 15:44:11 +00001573 dmar_free_hwirq(irq);
Chris Wrightdd726432009-05-13 15:55:52 -07001574 return ret;
Suresh Siddha0ac24912009-03-16 17:04:54 -07001575 }
1576
Thomas Gleixner477694e2011-07-19 16:25:42 +02001577 ret = request_irq(irq, dmar_fault, IRQF_NO_THREAD, iommu->name, iommu);
Suresh Siddha0ac24912009-03-16 17:04:54 -07001578 if (ret)
Donald Dutilebf947fcb2012-06-04 17:29:01 -04001579 pr_err("IOMMU: can't request irq\n");
Suresh Siddha0ac24912009-03-16 17:04:54 -07001580 return ret;
1581}
Suresh Siddha9d783ba2009-03-16 17:04:55 -07001582
1583int __init enable_drhd_fault_handling(void)
1584{
1585 struct dmar_drhd_unit *drhd;
Jiang Liu7c919772014-01-06 14:18:18 +08001586 struct intel_iommu *iommu;
Suresh Siddha9d783ba2009-03-16 17:04:55 -07001587
1588 /*
1589 * Enable fault control interrupt.
1590 */
Jiang Liu7c919772014-01-06 14:18:18 +08001591 for_each_iommu(iommu, drhd) {
Li, Zhen-Huabd5cdad2013-03-25 16:20:52 +08001592 u32 fault_status;
Jiang Liu7c919772014-01-06 14:18:18 +08001593 int ret = dmar_set_interrupt(iommu);
Suresh Siddha9d783ba2009-03-16 17:04:55 -07001594
1595 if (ret) {
Donald Dutilee9071b02012-06-08 17:13:11 -04001596 pr_err("DRHD %Lx: failed to enable fault, interrupt, ret %d\n",
Suresh Siddha9d783ba2009-03-16 17:04:55 -07001597 (unsigned long long)drhd->reg_base_addr, ret);
1598 return -1;
1599 }
Suresh Siddha7f99d942010-11-30 22:22:29 -08001600
1601 /*
1602 * Clear any previous faults.
1603 */
1604 dmar_fault(iommu->irq, iommu);
Li, Zhen-Huabd5cdad2013-03-25 16:20:52 +08001605 fault_status = readl(iommu->reg + DMAR_FSTS_REG);
1606 writel(fault_status, iommu->reg + DMAR_FSTS_REG);
Suresh Siddha9d783ba2009-03-16 17:04:55 -07001607 }
1608
1609 return 0;
1610}
Fenghua Yueb4a52b2009-03-27 14:22:43 -07001611
1612/*
1613 * Re-enable Queued Invalidation interface.
1614 */
1615int dmar_reenable_qi(struct intel_iommu *iommu)
1616{
1617 if (!ecap_qis(iommu->ecap))
1618 return -ENOENT;
1619
1620 if (!iommu->qi)
1621 return -ENOENT;
1622
1623 /*
1624 * First disable queued invalidation.
1625 */
1626 dmar_disable_qi(iommu);
1627 /*
1628 * Then enable queued invalidation again. Since there is no pending
1629 * invalidation requests now, it's safe to re-enable queued
1630 * invalidation.
1631 */
1632 __dmar_enable_qi(iommu);
1633
1634 return 0;
1635}
Youquan Song074835f2009-09-09 12:05:39 -04001636
1637/*
1638 * Check interrupt remapping support in DMAR table description.
1639 */
Luck, Tony0b8973a2009-12-16 22:59:29 +00001640int __init dmar_ir_support(void)
Youquan Song074835f2009-09-09 12:05:39 -04001641{
1642 struct acpi_table_dmar *dmar;
1643 dmar = (struct acpi_table_dmar *)dmar_tbl;
Arnaud Patard4f506e02010-03-25 18:02:58 +00001644 if (!dmar)
1645 return 0;
Youquan Song074835f2009-09-09 12:05:39 -04001646 return dmar->flags & 0x1;
1647}
Jiang Liu694835d2014-01-06 14:18:16 +08001648
Jiang Liua868e6b2014-01-06 14:18:20 +08001649static int __init dmar_free_unused_resources(void)
1650{
1651 struct dmar_drhd_unit *dmaru, *dmaru_n;
1652
1653 /* DMAR units are in use */
1654 if (irq_remapping_enabled || intel_iommu_enabled)
1655 return 0;
1656
Jiang Liu2e455282014-02-19 14:07:36 +08001657 if (dmar_dev_scope_status != 1 && !list_empty(&dmar_drhd_units))
1658 bus_unregister_notifier(&pci_bus_type, &dmar_pci_bus_nb);
Jiang Liu59ce0512014-02-19 14:07:35 +08001659
Jiang Liu3a5670e2014-02-19 14:07:33 +08001660 down_write(&dmar_global_lock);
Jiang Liua868e6b2014-01-06 14:18:20 +08001661 list_for_each_entry_safe(dmaru, dmaru_n, &dmar_drhd_units, list) {
1662 list_del(&dmaru->list);
1663 dmar_free_drhd(dmaru);
1664 }
Jiang Liu3a5670e2014-02-19 14:07:33 +08001665 up_write(&dmar_global_lock);
Jiang Liua868e6b2014-01-06 14:18:20 +08001666
1667 return 0;
1668}
1669
1670late_initcall(dmar_free_unused_resources);
Konrad Rzeszutek Wilk4db77ff2010-08-26 13:58:04 -04001671IOMMU_INIT_POST(detect_intel_iommu);