Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 1 | /* |
| 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 gross | 98bcef5 | 2008-02-23 15:23:35 -0800 | [diff] [blame] | 17 | * 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 S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 21 | * |
Suresh Siddha | e61d98d | 2008-07-10 11:16:35 -0700 | [diff] [blame] | 22 | * This file implements early detection/parsing of Remapping Devices |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 23 | * reported to OS through BIOS via DMA remapping reporting (DMAR) ACPI |
| 24 | * tables. |
Suresh Siddha | e61d98d | 2008-07-10 11:16:35 -0700 | [diff] [blame] | 25 | * |
| 26 | * These routines are used by both DMA-remapping and Interrupt-remapping |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 27 | */ |
| 28 | |
Donald Dutile | e9071b0 | 2012-06-08 17:13:11 -0400 | [diff] [blame] | 29 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt /* has to precede printk.h */ |
| 30 | |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 31 | #include <linux/pci.h> |
| 32 | #include <linux/dmar.h> |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 33 | #include <linux/iova.h> |
| 34 | #include <linux/intel-iommu.h> |
Suresh Siddha | fe962e9 | 2008-07-10 11:16:42 -0700 | [diff] [blame] | 35 | #include <linux/timer.h> |
Suresh Siddha | 0ac2491 | 2009-03-16 17:04:54 -0700 | [diff] [blame] | 36 | #include <linux/irq.h> |
| 37 | #include <linux/interrupt.h> |
Shane Wang | 69575d3 | 2009-09-01 18:25:07 -0700 | [diff] [blame] | 38 | #include <linux/tboot.h> |
Len Brown | eb27cae | 2009-07-06 23:40:19 -0400 | [diff] [blame] | 39 | #include <linux/dmi.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 40 | #include <linux/slab.h> |
Alex Williamson | a5459cf | 2014-06-12 16:12:31 -0600 | [diff] [blame] | 41 | #include <linux/iommu.h> |
Suresh Siddha | 8a8f422 | 2012-03-30 11:47:08 -0700 | [diff] [blame] | 42 | #include <asm/irq_remapping.h> |
Konrad Rzeszutek Wilk | 4db77ff | 2010-08-26 13:58:04 -0400 | [diff] [blame] | 43 | #include <asm/iommu_table.h> |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 44 | |
Joerg Roedel | 078e1ee | 2012-09-26 12:44:43 +0200 | [diff] [blame] | 45 | #include "irq_remapping.h" |
| 46 | |
Jiang Liu | 3a5670e | 2014-02-19 14:07:33 +0800 | [diff] [blame] | 47 | /* |
| 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 S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 58 | */ |
Jiang Liu | 3a5670e | 2014-02-19 14:07:33 +0800 | [diff] [blame] | 59 | DECLARE_RWSEM(dmar_global_lock); |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 60 | LIST_HEAD(dmar_drhd_units); |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 61 | |
Suresh Siddha | 41750d3 | 2011-08-23 17:05:18 -0700 | [diff] [blame] | 62 | struct acpi_table_header * __initdata dmar_tbl; |
Yinghai Lu | 8e1568f | 2009-02-11 01:06:59 -0800 | [diff] [blame] | 63 | static acpi_size dmar_tbl_size; |
Jiang Liu | 2e45528 | 2014-02-19 14:07:36 +0800 | [diff] [blame] | 64 | static int dmar_dev_scope_status = 1; |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 65 | |
Jiang Liu | 694835d | 2014-01-06 14:18:16 +0800 | [diff] [blame] | 66 | static int alloc_iommu(struct dmar_drhd_unit *drhd); |
Jiang Liu | a868e6b | 2014-01-06 14:18:20 +0800 | [diff] [blame] | 67 | static void free_iommu(struct intel_iommu *iommu); |
Jiang Liu | 694835d | 2014-01-06 14:18:16 +0800 | [diff] [blame] | 68 | |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 69 | static 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 Liu | 0e24261 | 2014-02-19 14:07:34 +0800 | [diff] [blame] | 76 | list_add_tail_rcu(&drhd->list, &dmar_drhd_units); |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 77 | else |
Jiang Liu | 0e24261 | 2014-02-19 14:07:34 +0800 | [diff] [blame] | 78 | list_add_rcu(&drhd->list, &dmar_drhd_units); |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Jiang Liu | bb3a6b7 | 2014-02-19 14:07:24 +0800 | [diff] [blame] | 81 | void *dmar_alloc_dev_scope(void *start, void *end, int *cnt) |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 82 | { |
| 83 | struct acpi_dmar_device_scope *scope; |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 84 | |
| 85 | *cnt = 0; |
| 86 | while (start < end) { |
| 87 | scope = start; |
Bob Moore | 83118b0 | 2014-07-30 12:21:00 +0800 | [diff] [blame] | 88 | if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_NAMESPACE || |
David Woodhouse | 07cb52f | 2014-03-07 14:39:27 +0000 | [diff] [blame] | 89 | scope->entry_type == ACPI_DMAR_SCOPE_TYPE_ENDPOINT || |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 90 | scope->entry_type == ACPI_DMAR_SCOPE_TYPE_BRIDGE) |
| 91 | (*cnt)++; |
Linn Crosetto | ae3e7f3 | 2013-04-23 12:26:45 -0600 | [diff] [blame] | 92 | else if (scope->entry_type != ACPI_DMAR_SCOPE_TYPE_IOAPIC && |
| 93 | scope->entry_type != ACPI_DMAR_SCOPE_TYPE_HPET) { |
Donald Dutile | e9071b0 | 2012-06-08 17:13:11 -0400 | [diff] [blame] | 94 | pr_warn("Unsupported device scope\n"); |
Yinghai Lu | 5715f0f | 2010-04-08 19:58:22 +0100 | [diff] [blame] | 95 | } |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 96 | start += scope->length; |
| 97 | } |
| 98 | if (*cnt == 0) |
Jiang Liu | bb3a6b7 | 2014-02-19 14:07:24 +0800 | [diff] [blame] | 99 | return NULL; |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 100 | |
David Woodhouse | 832bd85 | 2014-03-07 15:08:36 +0000 | [diff] [blame] | 101 | return kcalloc(*cnt, sizeof(struct dmar_dev_scope), GFP_KERNEL); |
Jiang Liu | bb3a6b7 | 2014-02-19 14:07:24 +0800 | [diff] [blame] | 102 | } |
| 103 | |
David Woodhouse | 832bd85 | 2014-03-07 15:08:36 +0000 | [diff] [blame] | 104 | void dmar_free_dev_scope(struct dmar_dev_scope **devices, int *cnt) |
Jiang Liu | ada4d4b | 2014-01-06 14:18:09 +0800 | [diff] [blame] | 105 | { |
Jiang Liu | b683b23 | 2014-02-19 14:07:32 +0800 | [diff] [blame] | 106 | int i; |
David Woodhouse | 832bd85 | 2014-03-07 15:08:36 +0000 | [diff] [blame] | 107 | struct device *tmp_dev; |
Jiang Liu | b683b23 | 2014-02-19 14:07:32 +0800 | [diff] [blame] | 108 | |
Jiang Liu | ada4d4b | 2014-01-06 14:18:09 +0800 | [diff] [blame] | 109 | if (*devices && *cnt) { |
Jiang Liu | b683b23 | 2014-02-19 14:07:32 +0800 | [diff] [blame] | 110 | for_each_active_dev_scope(*devices, *cnt, i, tmp_dev) |
David Woodhouse | 832bd85 | 2014-03-07 15:08:36 +0000 | [diff] [blame] | 111 | put_device(tmp_dev); |
Jiang Liu | ada4d4b | 2014-01-06 14:18:09 +0800 | [diff] [blame] | 112 | kfree(*devices); |
Jiang Liu | ada4d4b | 2014-01-06 14:18:09 +0800 | [diff] [blame] | 113 | } |
Jiang Liu | 0e24261 | 2014-02-19 14:07:34 +0800 | [diff] [blame] | 114 | |
| 115 | *devices = NULL; |
| 116 | *cnt = 0; |
Jiang Liu | ada4d4b | 2014-01-06 14:18:09 +0800 | [diff] [blame] | 117 | } |
| 118 | |
Jiang Liu | 59ce051 | 2014-02-19 14:07:35 +0800 | [diff] [blame] | 119 | /* Optimize out kzalloc()/kfree() for normal cases */ |
| 120 | static char dmar_pci_notify_info_buf[64]; |
| 121 | |
| 122 | static struct dmar_pci_notify_info * |
| 123 | dmar_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 Liu | 2e45528 | 2014-02-19 14:07:36 +0800 | [diff] [blame] | 145 | if (dmar_dev_scope_status == 0) |
| 146 | dmar_dev_scope_status = -ENOMEM; |
Jiang Liu | 59ce051 | 2014-02-19 14:07:35 +0800 | [diff] [blame] | 147 | 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 Liu | 5ae0566 | 2014-04-15 10:35:35 +0800 | [diff] [blame] | 156 | for (tmp = dev; tmp; tmp = tmp->bus->self) { |
| 157 | level--; |
Joerg Roedel | 5738459 | 2014-10-02 11:50:25 +0200 | [diff] [blame^] | 158 | info->path[level].bus = tmp->bus->number; |
Jiang Liu | 59ce051 | 2014-02-19 14:07:35 +0800 | [diff] [blame] | 159 | 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 | |
| 169 | static 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 | |
| 175 | static 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 */ |
| 195 | int dmar_insert_dev_scope(struct dmar_pci_notify_info *info, |
| 196 | void *start, void*end, u16 segment, |
David Woodhouse | 832bd85 | 2014-03-07 15:08:36 +0000 | [diff] [blame] | 197 | struct dmar_dev_scope *devices, |
| 198 | int devices_cnt) |
Jiang Liu | 59ce051 | 2014-02-19 14:07:35 +0800 | [diff] [blame] | 199 | { |
| 200 | int i, level; |
David Woodhouse | 832bd85 | 2014-03-07 15:08:36 +0000 | [diff] [blame] | 201 | struct device *tmp, *dev = &info->dev->dev; |
Jiang Liu | 59ce051 | 2014-02-19 14:07:35 +0800 | [diff] [blame] | 202 | 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 Woodhouse | 832bd85 | 2014-03-07 15:08:36 +0000 | [diff] [blame] | 220 | (info->dev->hdr_type == PCI_HEADER_TYPE_NORMAL)) { |
Jiang Liu | 59ce051 | 2014-02-19 14:07:35 +0800 | [diff] [blame] | 221 | pr_warn("Device scope type does not match for %s\n", |
David Woodhouse | 832bd85 | 2014-03-07 15:08:36 +0000 | [diff] [blame] | 222 | pci_name(info->dev)); |
Jiang Liu | 59ce051 | 2014-02-19 14:07:35 +0800 | [diff] [blame] | 223 | return -EINVAL; |
| 224 | } |
| 225 | |
| 226 | for_each_dev_scope(devices, devices_cnt, i, tmp) |
| 227 | if (tmp == NULL) { |
David Woodhouse | 832bd85 | 2014-03-07 15:08:36 +0000 | [diff] [blame] | 228 | 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 Liu | 59ce051 | 2014-02-19 14:07:35 +0800 | [diff] [blame] | 232 | return 1; |
| 233 | } |
| 234 | BUG_ON(i >= devices_cnt); |
| 235 | } |
| 236 | |
| 237 | return 0; |
| 238 | } |
| 239 | |
| 240 | int dmar_remove_dev_scope(struct dmar_pci_notify_info *info, u16 segment, |
David Woodhouse | 832bd85 | 2014-03-07 15:08:36 +0000 | [diff] [blame] | 241 | struct dmar_dev_scope *devices, int count) |
Jiang Liu | 59ce051 | 2014-02-19 14:07:35 +0800 | [diff] [blame] | 242 | { |
| 243 | int index; |
David Woodhouse | 832bd85 | 2014-03-07 15:08:36 +0000 | [diff] [blame] | 244 | struct device *tmp; |
Jiang Liu | 59ce051 | 2014-02-19 14:07:35 +0800 | [diff] [blame] | 245 | |
| 246 | if (info->seg != segment) |
| 247 | return 0; |
| 248 | |
| 249 | for_each_active_dev_scope(devices, count, index, tmp) |
David Woodhouse | 832bd85 | 2014-03-07 15:08:36 +0000 | [diff] [blame] | 250 | if (tmp == &info->dev->dev) { |
Andreea-Cristina Bernat | eecbad7 | 2014-08-18 15:20:56 +0300 | [diff] [blame] | 251 | RCU_INIT_POINTER(devices[index].dev, NULL); |
Jiang Liu | 59ce051 | 2014-02-19 14:07:35 +0800 | [diff] [blame] | 252 | synchronize_rcu(); |
David Woodhouse | 832bd85 | 2014-03-07 15:08:36 +0000 | [diff] [blame] | 253 | put_device(tmp); |
Jiang Liu | 59ce051 | 2014-02-19 14:07:35 +0800 | [diff] [blame] | 254 | return 1; |
| 255 | } |
| 256 | |
| 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | static 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 Liu | 2e45528 | 2014-02-19 14:07:36 +0800 | [diff] [blame] | 281 | if (ret < 0 && dmar_dev_scope_status == 0) |
| 282 | dmar_dev_scope_status = ret; |
Jiang Liu | 59ce051 | 2014-02-19 14:07:35 +0800 | [diff] [blame] | 283 | |
| 284 | return ret; |
| 285 | } |
| 286 | |
| 287 | static 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 | |
| 298 | static 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 | |
| 326 | static struct notifier_block dmar_pci_bus_nb = { |
| 327 | .notifier_call = dmar_pci_bus_notifier, |
| 328 | .priority = INT_MIN, |
| 329 | }; |
| 330 | |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 331 | /** |
| 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 | */ |
| 336 | static int __init |
| 337 | dmar_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 S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 342 | |
David Woodhouse | e523b38 | 2009-04-10 22:27:48 -0700 | [diff] [blame] | 343 | drhd = (struct acpi_dmar_hardware_unit *)header; |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 344 | dmaru = kzalloc(sizeof(*dmaru), GFP_KERNEL); |
| 345 | if (!dmaru) |
| 346 | return -ENOMEM; |
| 347 | |
Suresh Siddha | 1886e8a | 2008-07-10 11:16:37 -0700 | [diff] [blame] | 348 | dmaru->hdr = header; |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 349 | dmaru->reg_base_addr = drhd->address; |
David Woodhouse | 276dbf99 | 2009-04-04 01:45:37 +0100 | [diff] [blame] | 350 | dmaru->segment = drhd->segment; |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 351 | dmaru->include_all = drhd->flags & 0x1; /* BIT0: INCLUDE_ALL */ |
David Woodhouse | 07cb52f | 2014-03-07 14:39:27 +0000 | [diff] [blame] | 352 | 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 Liu | 2e45528 | 2014-02-19 14:07:36 +0800 | [diff] [blame] | 358 | } |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 359 | |
Suresh Siddha | 1886e8a | 2008-07-10 11:16:37 -0700 | [diff] [blame] | 360 | ret = alloc_iommu(dmaru); |
| 361 | if (ret) { |
David Woodhouse | 07cb52f | 2014-03-07 14:39:27 +0000 | [diff] [blame] | 362 | dmar_free_dev_scope(&dmaru->devices, |
| 363 | &dmaru->devices_cnt); |
Suresh Siddha | 1886e8a | 2008-07-10 11:16:37 -0700 | [diff] [blame] | 364 | kfree(dmaru); |
| 365 | return ret; |
| 366 | } |
| 367 | dmar_register_drhd_unit(dmaru); |
| 368 | return 0; |
| 369 | } |
| 370 | |
Jiang Liu | a868e6b | 2014-01-06 14:18:20 +0800 | [diff] [blame] | 371 | static 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 Woodhouse | e625b4a | 2014-03-07 14:34:38 +0000 | [diff] [blame] | 380 | static 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 Moore | 83118b0 | 2014-07-30 12:21:00 +0800 | [diff] [blame] | 385 | if (strnlen(andd->device_name, header->length - 8) == header->length - 8) { |
David Woodhouse | e625b4a | 2014-03-07 14:34:38 +0000 | [diff] [blame] | 386 | 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 Moore | 83118b0 | 2014-07-30 12:21:00 +0800 | [diff] [blame] | 395 | andd->device_name); |
David Woodhouse | e625b4a | 2014-03-07 14:34:38 +0000 | [diff] [blame] | 396 | |
| 397 | return 0; |
| 398 | } |
| 399 | |
David Woodhouse | aa69707 | 2009-10-07 12:18:00 +0100 | [diff] [blame] | 400 | #ifdef CONFIG_ACPI_NUMA |
Suresh Siddha | ee34b32 | 2009-10-02 11:01:21 -0700 | [diff] [blame] | 401 | static int __init |
| 402 | dmar_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 Woodhouse | aa69707 | 2009-10-07 12:18:00 +0100 | [diff] [blame] | 408 | for_each_drhd_unit(drhd) { |
Suresh Siddha | ee34b32 | 2009-10-02 11:01:21 -0700 | [diff] [blame] | 409 | 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 Woodhouse | aa69707 | 2009-10-07 12:18:00 +0100 | [diff] [blame] | 415 | return 0; |
| 416 | } |
Suresh Siddha | ee34b32 | 2009-10-02 11:01:21 -0700 | [diff] [blame] | 417 | } |
Ben Hutchings | fd0c889 | 2010-04-03 19:38:43 +0100 | [diff] [blame] | 418 | 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 Siddha | ee34b32 | 2009-10-02 11:01:21 -0700 | [diff] [blame] | 426 | |
David Woodhouse | aa69707 | 2009-10-07 12:18:00 +0100 | [diff] [blame] | 427 | return 0; |
Suresh Siddha | ee34b32 | 2009-10-02 11:01:21 -0700 | [diff] [blame] | 428 | } |
David Woodhouse | aa69707 | 2009-10-07 12:18:00 +0100 | [diff] [blame] | 429 | #endif |
Suresh Siddha | ee34b32 | 2009-10-02 11:01:21 -0700 | [diff] [blame] | 430 | |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 431 | static void __init |
| 432 | dmar_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 Zhao | aa5d2b5 | 2009-05-18 13:51:34 +0800 | [diff] [blame] | 436 | struct acpi_dmar_atsr *atsr; |
Roland Dreier | 17b6097 | 2009-09-24 12:14:00 -0700 | [diff] [blame] | 437 | struct acpi_dmar_rhsa *rhsa; |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 438 | |
| 439 | switch (header->type) { |
| 440 | case ACPI_DMAR_TYPE_HARDWARE_UNIT: |
Yu Zhao | aa5d2b5 | 2009-05-18 13:51:34 +0800 | [diff] [blame] | 441 | drhd = container_of(header, struct acpi_dmar_hardware_unit, |
| 442 | header); |
Donald Dutile | e9071b0 | 2012-06-08 17:13:11 -0400 | [diff] [blame] | 443 | pr_info("DRHD base: %#016Lx flags: %#x\n", |
Yu Zhao | aa5d2b5 | 2009-05-18 13:51:34 +0800 | [diff] [blame] | 444 | (unsigned long long)drhd->address, drhd->flags); |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 445 | break; |
| 446 | case ACPI_DMAR_TYPE_RESERVED_MEMORY: |
Yu Zhao | aa5d2b5 | 2009-05-18 13:51:34 +0800 | [diff] [blame] | 447 | rmrr = container_of(header, struct acpi_dmar_reserved_memory, |
| 448 | header); |
Donald Dutile | e9071b0 | 2012-06-08 17:13:11 -0400 | [diff] [blame] | 449 | pr_info("RMRR base: %#016Lx end: %#016Lx\n", |
Fenghua Yu | 5b6985c | 2008-10-16 18:02:32 -0700 | [diff] [blame] | 450 | (unsigned long long)rmrr->base_address, |
| 451 | (unsigned long long)rmrr->end_address); |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 452 | break; |
Bob Moore | 83118b0 | 2014-07-30 12:21:00 +0800 | [diff] [blame] | 453 | case ACPI_DMAR_TYPE_ROOT_ATS: |
Yu Zhao | aa5d2b5 | 2009-05-18 13:51:34 +0800 | [diff] [blame] | 454 | atsr = container_of(header, struct acpi_dmar_atsr, header); |
Donald Dutile | e9071b0 | 2012-06-08 17:13:11 -0400 | [diff] [blame] | 455 | pr_info("ATSR flags: %#x\n", atsr->flags); |
Yu Zhao | aa5d2b5 | 2009-05-18 13:51:34 +0800 | [diff] [blame] | 456 | break; |
Bob Moore | 83118b0 | 2014-07-30 12:21:00 +0800 | [diff] [blame] | 457 | case ACPI_DMAR_TYPE_HARDWARE_AFFINITY: |
Roland Dreier | 17b6097 | 2009-09-24 12:14:00 -0700 | [diff] [blame] | 458 | rhsa = container_of(header, struct acpi_dmar_rhsa, header); |
Donald Dutile | e9071b0 | 2012-06-08 17:13:11 -0400 | [diff] [blame] | 459 | pr_info("RHSA base: %#016Lx proximity domain: %#x\n", |
Roland Dreier | 17b6097 | 2009-09-24 12:14:00 -0700 | [diff] [blame] | 460 | (unsigned long long)rhsa->base_address, |
| 461 | rhsa->proximity_domain); |
| 462 | break; |
Bob Moore | 83118b0 | 2014-07-30 12:21:00 +0800 | [diff] [blame] | 463 | case ACPI_DMAR_TYPE_NAMESPACE: |
David Woodhouse | e625b4a | 2014-03-07 14:34:38 +0000 | [diff] [blame] | 464 | /* 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 S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 467 | } |
| 468 | } |
| 469 | |
Yinghai Lu | f6dd5c3 | 2008-09-03 16:58:32 -0700 | [diff] [blame] | 470 | /** |
| 471 | * dmar_table_detect - checks to see if the platform supports DMAR devices |
| 472 | */ |
| 473 | static 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 Lu | 8e1568f | 2009-02-11 01:06:59 -0800 | [diff] [blame] | 478 | status = acpi_get_table_with_size(ACPI_SIG_DMAR, 0, |
| 479 | (struct acpi_table_header **)&dmar_tbl, |
| 480 | &dmar_tbl_size); |
Yinghai Lu | f6dd5c3 | 2008-09-03 16:58:32 -0700 | [diff] [blame] | 481 | |
| 482 | if (ACPI_SUCCESS(status) && !dmar_tbl) { |
Donald Dutile | e9071b0 | 2012-06-08 17:13:11 -0400 | [diff] [blame] | 483 | pr_warn("Unable to map DMAR\n"); |
Yinghai Lu | f6dd5c3 | 2008-09-03 16:58:32 -0700 | [diff] [blame] | 484 | status = AE_NOT_FOUND; |
| 485 | } |
| 486 | |
| 487 | return (ACPI_SUCCESS(status) ? 1 : 0); |
| 488 | } |
Suresh Siddha | aaa9d1d | 2008-07-10 11:16:38 -0700 | [diff] [blame] | 489 | |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 490 | /** |
| 491 | * parse_dmar_table - parses the DMA reporting table |
| 492 | */ |
| 493 | static int __init |
| 494 | parse_dmar_table(void) |
| 495 | { |
| 496 | struct acpi_table_dmar *dmar; |
| 497 | struct acpi_dmar_header *entry_header; |
| 498 | int ret = 0; |
Li, Zhen-Hua | 7cef334 | 2013-05-20 15:57:32 +0800 | [diff] [blame] | 499 | int drhd_count = 0; |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 500 | |
Yinghai Lu | f6dd5c3 | 2008-09-03 16:58:32 -0700 | [diff] [blame] | 501 | /* |
| 502 | * Do it again, earlier dmar_tbl mapping could be mapped with |
| 503 | * fixed map. |
| 504 | */ |
| 505 | dmar_table_detect(); |
| 506 | |
Joseph Cihula | a59b50e | 2009-06-30 19:31:10 -0700 | [diff] [blame] | 507 | /* |
| 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 S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 513 | dmar = (struct acpi_table_dmar *)dmar_tbl; |
| 514 | if (!dmar) |
| 515 | return -ENODEV; |
| 516 | |
Fenghua Yu | 5b6985c | 2008-10-16 18:02:32 -0700 | [diff] [blame] | 517 | if (dmar->width < PAGE_SHIFT - 1) { |
Donald Dutile | e9071b0 | 2012-06-08 17:13:11 -0400 | [diff] [blame] | 518 | pr_warn("Invalid DMAR haw\n"); |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 519 | return -EINVAL; |
| 520 | } |
| 521 | |
Donald Dutile | e9071b0 | 2012-06-08 17:13:11 -0400 | [diff] [blame] | 522 | pr_info("Host address width %d\n", dmar->width + 1); |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 523 | |
| 524 | entry_header = (struct acpi_dmar_header *)(dmar + 1); |
| 525 | while (((unsigned long)entry_header) < |
| 526 | (((unsigned long)dmar) + dmar_tbl->length)) { |
Tony Battersby | 084eb96 | 2009-02-11 13:24:19 -0800 | [diff] [blame] | 527 | /* Avoid looping forever on bad ACPI tables */ |
| 528 | if (entry_header->length == 0) { |
Donald Dutile | e9071b0 | 2012-06-08 17:13:11 -0400 | [diff] [blame] | 529 | pr_warn("Invalid 0-length structure\n"); |
Tony Battersby | 084eb96 | 2009-02-11 13:24:19 -0800 | [diff] [blame] | 530 | ret = -EINVAL; |
| 531 | break; |
| 532 | } |
| 533 | |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 534 | dmar_table_print_dmar_entry(entry_header); |
| 535 | |
| 536 | switch (entry_header->type) { |
| 537 | case ACPI_DMAR_TYPE_HARDWARE_UNIT: |
Li, Zhen-Hua | 7cef334 | 2013-05-20 15:57:32 +0800 | [diff] [blame] | 538 | drhd_count++; |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 539 | 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 Moore | 83118b0 | 2014-07-30 12:21:00 +0800 | [diff] [blame] | 544 | case ACPI_DMAR_TYPE_ROOT_ATS: |
Yu Zhao | aa5d2b5 | 2009-05-18 13:51:34 +0800 | [diff] [blame] | 545 | ret = dmar_parse_one_atsr(entry_header); |
Yu Zhao | aa5d2b5 | 2009-05-18 13:51:34 +0800 | [diff] [blame] | 546 | break; |
Bob Moore | 83118b0 | 2014-07-30 12:21:00 +0800 | [diff] [blame] | 547 | case ACPI_DMAR_TYPE_HARDWARE_AFFINITY: |
David Woodhouse | aa69707 | 2009-10-07 12:18:00 +0100 | [diff] [blame] | 548 | #ifdef CONFIG_ACPI_NUMA |
Suresh Siddha | ee34b32 | 2009-10-02 11:01:21 -0700 | [diff] [blame] | 549 | ret = dmar_parse_one_rhsa(entry_header); |
David Woodhouse | aa69707 | 2009-10-07 12:18:00 +0100 | [diff] [blame] | 550 | #endif |
Roland Dreier | 17b6097 | 2009-09-24 12:14:00 -0700 | [diff] [blame] | 551 | break; |
Bob Moore | 83118b0 | 2014-07-30 12:21:00 +0800 | [diff] [blame] | 552 | case ACPI_DMAR_TYPE_NAMESPACE: |
David Woodhouse | e625b4a | 2014-03-07 14:34:38 +0000 | [diff] [blame] | 553 | ret = dmar_parse_one_andd(entry_header); |
| 554 | break; |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 555 | default: |
Donald Dutile | e9071b0 | 2012-06-08 17:13:11 -0400 | [diff] [blame] | 556 | pr_warn("Unknown DMAR structure type %d\n", |
Roland Dreier | 4de75cf | 2009-09-24 01:01:29 +0100 | [diff] [blame] | 557 | entry_header->type); |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 558 | 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-Hua | 7cef334 | 2013-05-20 15:57:32 +0800 | [diff] [blame] | 566 | if (drhd_count == 0) |
| 567 | pr_warn(FW_BUG "No DRHD structure found in DMAR table\n"); |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 568 | return ret; |
| 569 | } |
| 570 | |
David Woodhouse | 832bd85 | 2014-03-07 15:08:36 +0000 | [diff] [blame] | 571 | static int dmar_pci_device_match(struct dmar_dev_scope devices[], |
| 572 | int cnt, struct pci_dev *dev) |
Suresh Siddha | e61d98d | 2008-07-10 11:16:35 -0700 | [diff] [blame] | 573 | { |
| 574 | int index; |
David Woodhouse | 832bd85 | 2014-03-07 15:08:36 +0000 | [diff] [blame] | 575 | struct device *tmp; |
Suresh Siddha | e61d98d | 2008-07-10 11:16:35 -0700 | [diff] [blame] | 576 | |
| 577 | while (dev) { |
Jiang Liu | b683b23 | 2014-02-19 14:07:32 +0800 | [diff] [blame] | 578 | for_each_active_dev_scope(devices, cnt, index, tmp) |
David Woodhouse | 832bd85 | 2014-03-07 15:08:36 +0000 | [diff] [blame] | 579 | if (dev_is_pci(tmp) && dev == to_pci_dev(tmp)) |
Suresh Siddha | e61d98d | 2008-07-10 11:16:35 -0700 | [diff] [blame] | 580 | return 1; |
| 581 | |
| 582 | /* Check our parent */ |
| 583 | dev = dev->bus->self; |
| 584 | } |
| 585 | |
| 586 | return 0; |
| 587 | } |
| 588 | |
| 589 | struct dmar_drhd_unit * |
| 590 | dmar_find_matched_drhd_unit(struct pci_dev *dev) |
| 591 | { |
Jiang Liu | 0e24261 | 2014-02-19 14:07:34 +0800 | [diff] [blame] | 592 | struct dmar_drhd_unit *dmaru; |
Yu Zhao | 2e824f7 | 2008-12-22 16:54:58 +0800 | [diff] [blame] | 593 | struct acpi_dmar_hardware_unit *drhd; |
Suresh Siddha | e61d98d | 2008-07-10 11:16:35 -0700 | [diff] [blame] | 594 | |
Yinghai | dda5654 | 2010-04-09 01:07:55 +0100 | [diff] [blame] | 595 | dev = pci_physfn(dev); |
| 596 | |
Jiang Liu | 0e24261 | 2014-02-19 14:07:34 +0800 | [diff] [blame] | 597 | rcu_read_lock(); |
Yijing Wang | 8b161f0 | 2013-10-31 17:25:16 +0800 | [diff] [blame] | 598 | for_each_drhd_unit(dmaru) { |
Yu Zhao | 2e824f7 | 2008-12-22 16:54:58 +0800 | [diff] [blame] | 599 | 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 Liu | 0e24261 | 2014-02-19 14:07:34 +0800 | [diff] [blame] | 605 | goto out; |
Yu Zhao | 2e824f7 | 2008-12-22 16:54:58 +0800 | [diff] [blame] | 606 | |
| 607 | if (dmar_pci_device_match(dmaru->devices, |
| 608 | dmaru->devices_cnt, dev)) |
Jiang Liu | 0e24261 | 2014-02-19 14:07:34 +0800 | [diff] [blame] | 609 | goto out; |
Suresh Siddha | e61d98d | 2008-07-10 11:16:35 -0700 | [diff] [blame] | 610 | } |
Jiang Liu | 0e24261 | 2014-02-19 14:07:34 +0800 | [diff] [blame] | 611 | dmaru = NULL; |
| 612 | out: |
| 613 | rcu_read_unlock(); |
Suresh Siddha | e61d98d | 2008-07-10 11:16:35 -0700 | [diff] [blame] | 614 | |
Jiang Liu | 0e24261 | 2014-02-19 14:07:34 +0800 | [diff] [blame] | 615 | return dmaru; |
Suresh Siddha | e61d98d | 2008-07-10 11:16:35 -0700 | [diff] [blame] | 616 | } |
| 617 | |
David Woodhouse | ed40356 | 2014-03-07 23:15:42 +0000 | [diff] [blame] | 618 | static 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 Moore | 83118b0 | 2014-07-30 12:21:00 +0800 | [diff] [blame] | 636 | if (scope->entry_type != ACPI_DMAR_SCOPE_TYPE_NAMESPACE) |
David Woodhouse | ed40356 | 2014-03-07 23:15:42 +0000 | [diff] [blame] | 637 | 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 | |
| 661 | static int __init dmar_acpi_dev_scope_init(void) |
| 662 | { |
Joerg Roedel | 11f1a77 | 2014-03-25 20:16:40 +0100 | [diff] [blame] | 663 | struct acpi_dmar_andd *andd; |
| 664 | |
| 665 | if (dmar_tbl == NULL) |
| 666 | return -ENODEV; |
| 667 | |
David Woodhouse | 7713ec0 | 2014-04-01 14:58:36 +0100 | [diff] [blame] | 668 | 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 Moore | 83118b0 | 2014-07-30 12:21:00 +0800 | [diff] [blame] | 671 | if (andd->header.type == ACPI_DMAR_TYPE_NAMESPACE) { |
David Woodhouse | ed40356 | 2014-03-07 23:15:42 +0000 | [diff] [blame] | 672 | acpi_handle h; |
| 673 | struct acpi_device *adev; |
| 674 | |
| 675 | if (!ACPI_SUCCESS(acpi_get_handle(ACPI_ROOT_OBJECT, |
Bob Moore | 83118b0 | 2014-07-30 12:21:00 +0800 | [diff] [blame] | 676 | andd->device_name, |
David Woodhouse | ed40356 | 2014-03-07 23:15:42 +0000 | [diff] [blame] | 677 | &h))) { |
| 678 | pr_err("Failed to find handle for ACPI object %s\n", |
Bob Moore | 83118b0 | 2014-07-30 12:21:00 +0800 | [diff] [blame] | 679 | andd->device_name); |
David Woodhouse | ed40356 | 2014-03-07 23:15:42 +0000 | [diff] [blame] | 680 | 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 Moore | 83118b0 | 2014-07-30 12:21:00 +0800 | [diff] [blame] | 685 | andd->device_name); |
David Woodhouse | ed40356 | 2014-03-07 23:15:42 +0000 | [diff] [blame] | 686 | continue; |
| 687 | } |
| 688 | dmar_acpi_insert_dev_scope(andd->device_number, adev); |
| 689 | } |
David Woodhouse | ed40356 | 2014-03-07 23:15:42 +0000 | [diff] [blame] | 690 | } |
| 691 | return 0; |
| 692 | } |
| 693 | |
Suresh Siddha | 1886e8a | 2008-07-10 11:16:37 -0700 | [diff] [blame] | 694 | int __init dmar_dev_scope_init(void) |
| 695 | { |
Jiang Liu | 2e45528 | 2014-02-19 14:07:36 +0800 | [diff] [blame] | 696 | struct pci_dev *dev = NULL; |
| 697 | struct dmar_pci_notify_info *info; |
Suresh Siddha | 1886e8a | 2008-07-10 11:16:37 -0700 | [diff] [blame] | 698 | |
Jiang Liu | 2e45528 | 2014-02-19 14:07:36 +0800 | [diff] [blame] | 699 | if (dmar_dev_scope_status != 1) |
| 700 | return dmar_dev_scope_status; |
Suresh Siddha | c2c7286 | 2011-08-23 17:05:19 -0700 | [diff] [blame] | 701 | |
Jiang Liu | 2e45528 | 2014-02-19 14:07:36 +0800 | [diff] [blame] | 702 | if (list_empty(&dmar_drhd_units)) { |
| 703 | dmar_dev_scope_status = -ENODEV; |
| 704 | } else { |
| 705 | dmar_dev_scope_status = 0; |
Suresh Siddha | 318fe7d | 2011-08-23 17:05:20 -0700 | [diff] [blame] | 706 | |
David Woodhouse | 63b4262 | 2014-03-28 11:28:40 +0000 | [diff] [blame] | 707 | dmar_acpi_dev_scope_init(); |
| 708 | |
Jiang Liu | 2e45528 | 2014-02-19 14:07:36 +0800 | [diff] [blame] | 709 | 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 Siddha | 1886e8a | 2008-07-10 11:16:37 -0700 | [diff] [blame] | 724 | } |
| 725 | |
Jiang Liu | 2e45528 | 2014-02-19 14:07:36 +0800 | [diff] [blame] | 726 | return dmar_dev_scope_status; |
Suresh Siddha | 1886e8a | 2008-07-10 11:16:37 -0700 | [diff] [blame] | 727 | } |
| 728 | |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 729 | |
| 730 | int __init dmar_table_init(void) |
| 731 | { |
Suresh Siddha | 1886e8a | 2008-07-10 11:16:37 -0700 | [diff] [blame] | 732 | static int dmar_table_initialized; |
Fenghua Yu | 093f87d | 2007-11-21 15:07:14 -0800 | [diff] [blame] | 733 | int ret; |
| 734 | |
Jiang Liu | cc05301 | 2014-01-06 14:18:24 +0800 | [diff] [blame] | 735 | 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 Siddha | 1886e8a | 2008-07-10 11:16:37 -0700 | [diff] [blame] | 744 | |
Jiang Liu | cc05301 | 2014-01-06 14:18:24 +0800 | [diff] [blame] | 745 | if (ret < 0) |
| 746 | dmar_table_initialized = ret; |
| 747 | else |
| 748 | dmar_table_initialized = 1; |
Fenghua Yu | 093f87d | 2007-11-21 15:07:14 -0800 | [diff] [blame] | 749 | } |
| 750 | |
Jiang Liu | cc05301 | 2014-01-06 14:18:24 +0800 | [diff] [blame] | 751 | return dmar_table_initialized < 0 ? dmar_table_initialized : 0; |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 752 | } |
| 753 | |
Ben Hutchings | 3a8663e | 2010-04-03 19:37:23 +0100 | [diff] [blame] | 754 | static void warn_invalid_dmar(u64 addr, const char *message) |
| 755 | { |
Ben Hutchings | fd0c889 | 2010-04-03 19:38:43 +0100 | [diff] [blame] | 756 | 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 Hutchings | 3a8663e | 2010-04-03 19:37:23 +0100 | [diff] [blame] | 764 | } |
David Woodhouse | 6ecbf01 | 2009-12-02 09:20:27 +0000 | [diff] [blame] | 765 | |
Rashika Kheria | 21004dc | 2013-12-18 12:01:46 +0530 | [diff] [blame] | 766 | static int __init check_zero_address(void) |
David Woodhouse | 86cf898 | 2009-11-09 22:15:15 +0000 | [diff] [blame] | 767 | { |
| 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 Dutile | e9071b0 | 2012-06-08 17:13:11 -0400 | [diff] [blame] | 779 | pr_warn("Invalid 0-length structure\n"); |
David Woodhouse | 86cf898 | 2009-11-09 22:15:15 +0000 | [diff] [blame] | 780 | return 0; |
| 781 | } |
| 782 | |
| 783 | if (entry_header->type == ACPI_DMAR_TYPE_HARDWARE_UNIT) { |
Chris Wright | 2c99220 | 2009-12-02 09:17:13 +0000 | [diff] [blame] | 784 | void __iomem *addr; |
| 785 | u64 cap, ecap; |
| 786 | |
David Woodhouse | 86cf898 | 2009-11-09 22:15:15 +0000 | [diff] [blame] | 787 | drhd = (void *)entry_header; |
| 788 | if (!drhd->address) { |
Ben Hutchings | 3a8663e | 2010-04-03 19:37:23 +0100 | [diff] [blame] | 789 | warn_invalid_dmar(0, ""); |
Chris Wright | 2c99220 | 2009-12-02 09:17:13 +0000 | [diff] [blame] | 790 | goto failed; |
David Woodhouse | 86cf898 | 2009-11-09 22:15:15 +0000 | [diff] [blame] | 791 | } |
Chris Wright | 2c99220 | 2009-12-02 09:17:13 +0000 | [diff] [blame] | 792 | |
| 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 Hutchings | 3a8663e | 2010-04-03 19:37:23 +0100 | [diff] [blame] | 802 | warn_invalid_dmar(drhd->address, |
| 803 | " returns all ones"); |
Chris Wright | 2c99220 | 2009-12-02 09:17:13 +0000 | [diff] [blame] | 804 | goto failed; |
| 805 | } |
David Woodhouse | 86cf898 | 2009-11-09 22:15:15 +0000 | [diff] [blame] | 806 | } |
| 807 | |
| 808 | entry_header = ((void *)entry_header + entry_header->length); |
| 809 | } |
| 810 | return 1; |
Chris Wright | 2c99220 | 2009-12-02 09:17:13 +0000 | [diff] [blame] | 811 | |
| 812 | failed: |
Chris Wright | 2c99220 | 2009-12-02 09:17:13 +0000 | [diff] [blame] | 813 | return 0; |
David Woodhouse | 86cf898 | 2009-11-09 22:15:15 +0000 | [diff] [blame] | 814 | } |
| 815 | |
Konrad Rzeszutek Wilk | 480125b | 2010-08-26 13:57:57 -0400 | [diff] [blame] | 816 | int __init detect_intel_iommu(void) |
Suresh Siddha | 2ae2101 | 2008-07-10 11:16:43 -0700 | [diff] [blame] | 817 | { |
| 818 | int ret; |
| 819 | |
Jiang Liu | 3a5670e | 2014-02-19 14:07:33 +0800 | [diff] [blame] | 820 | down_write(&dmar_global_lock); |
Yinghai Lu | f6dd5c3 | 2008-09-03 16:58:32 -0700 | [diff] [blame] | 821 | ret = dmar_table_detect(); |
David Woodhouse | 86cf898 | 2009-11-09 22:15:15 +0000 | [diff] [blame] | 822 | if (ret) |
| 823 | ret = check_zero_address(); |
Suresh Siddha | 2ae2101 | 2008-07-10 11:16:43 -0700 | [diff] [blame] | 824 | { |
Linus Torvalds | 11bd04f | 2009-12-11 12:18:16 -0800 | [diff] [blame] | 825 | if (ret && !no_iommu && !iommu_detected && !dmar_disabled) { |
Suresh Siddha | 2ae2101 | 2008-07-10 11:16:43 -0700 | [diff] [blame] | 826 | iommu_detected = 1; |
Chris Wright | 5d990b6 | 2009-12-04 12:15:21 -0800 | [diff] [blame] | 827 | /* Make sure ACS will be enabled */ |
| 828 | pci_request_acs(); |
| 829 | } |
Suresh Siddha | f5d1b97 | 2011-08-23 17:05:22 -0700 | [diff] [blame] | 830 | |
FUJITA Tomonori | 9d5ce73 | 2009-11-10 19:46:16 +0900 | [diff] [blame] | 831 | #ifdef CONFIG_X86 |
| 832 | if (ret) |
| 833 | x86_init.iommu.iommu_init = intel_iommu_init; |
| 834 | #endif |
Youquan Song | cacd421 | 2008-10-16 16:31:57 -0700 | [diff] [blame] | 835 | } |
Jiang Liu | b707cb0 | 2014-01-06 14:18:26 +0800 | [diff] [blame] | 836 | early_acpi_os_unmap_memory((void __iomem *)dmar_tbl, dmar_tbl_size); |
Yinghai Lu | f6dd5c3 | 2008-09-03 16:58:32 -0700 | [diff] [blame] | 837 | dmar_tbl = NULL; |
Jiang Liu | 3a5670e | 2014-02-19 14:07:33 +0800 | [diff] [blame] | 838 | up_write(&dmar_global_lock); |
Konrad Rzeszutek Wilk | 480125b | 2010-08-26 13:57:57 -0400 | [diff] [blame] | 839 | |
Konrad Rzeszutek Wilk | 4db77ff | 2010-08-26 13:58:04 -0400 | [diff] [blame] | 840 | return ret ? 1 : -ENODEV; |
Suresh Siddha | 2ae2101 | 2008-07-10 11:16:43 -0700 | [diff] [blame] | 841 | } |
| 842 | |
| 843 | |
Donald Dutile | 6f5cf52 | 2012-06-04 17:29:02 -0400 | [diff] [blame] | 844 | static 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 Dutile | e9071b0 | 2012-06-08 17:13:11 -0400 | [diff] [blame] | 854 | * |
Donald Dutile | 6f5cf52 | 2012-06-04 17:29:02 -0400 | [diff] [blame] | 855 | * Memory map the iommu's registers. Start w/ a single page, and |
Donald Dutile | e9071b0 | 2012-06-08 17:13:11 -0400 | [diff] [blame] | 856 | * possibly expand if that turns out to be insufficent. |
Donald Dutile | 6f5cf52 | 2012-06-04 17:29:02 -0400 | [diff] [blame] | 857 | */ |
| 858 | static 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 | |
| 911 | unmap: |
| 912 | iounmap(iommu->reg); |
| 913 | release: |
| 914 | release_mem_region(iommu->reg_phys, iommu->reg_size); |
| 915 | out: |
| 916 | return err; |
| 917 | } |
| 918 | |
Jiang Liu | 694835d | 2014-01-06 14:18:16 +0800 | [diff] [blame] | 919 | static int alloc_iommu(struct dmar_drhd_unit *drhd) |
Suresh Siddha | e61d98d | 2008-07-10 11:16:35 -0700 | [diff] [blame] | 920 | { |
Suresh Siddha | c42d9f3 | 2008-07-10 11:16:36 -0700 | [diff] [blame] | 921 | struct intel_iommu *iommu; |
Takao Indoh | 3a93c84 | 2013-04-23 17:35:03 +0900 | [diff] [blame] | 922 | u32 ver, sts; |
Suresh Siddha | c42d9f3 | 2008-07-10 11:16:36 -0700 | [diff] [blame] | 923 | static int iommu_allocated = 0; |
Joerg Roedel | 43f7392 | 2009-01-03 23:56:27 +0100 | [diff] [blame] | 924 | int agaw = 0; |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 925 | int msagaw = 0; |
Donald Dutile | 6f5cf52 | 2012-06-04 17:29:02 -0400 | [diff] [blame] | 926 | int err; |
Suresh Siddha | c42d9f3 | 2008-07-10 11:16:36 -0700 | [diff] [blame] | 927 | |
David Woodhouse | 6ecbf01 | 2009-12-02 09:20:27 +0000 | [diff] [blame] | 928 | if (!drhd->reg_base_addr) { |
Ben Hutchings | 3a8663e | 2010-04-03 19:37:23 +0100 | [diff] [blame] | 929 | warn_invalid_dmar(0, ""); |
David Woodhouse | 6ecbf01 | 2009-12-02 09:20:27 +0000 | [diff] [blame] | 930 | return -EINVAL; |
| 931 | } |
| 932 | |
Suresh Siddha | c42d9f3 | 2008-07-10 11:16:36 -0700 | [diff] [blame] | 933 | iommu = kzalloc(sizeof(*iommu), GFP_KERNEL); |
| 934 | if (!iommu) |
Suresh Siddha | 1886e8a | 2008-07-10 11:16:37 -0700 | [diff] [blame] | 935 | return -ENOMEM; |
Suresh Siddha | c42d9f3 | 2008-07-10 11:16:36 -0700 | [diff] [blame] | 936 | |
| 937 | iommu->seq_id = iommu_allocated++; |
Suresh Siddha | 9d783ba | 2009-03-16 17:04:55 -0700 | [diff] [blame] | 938 | sprintf (iommu->name, "dmar%d", iommu->seq_id); |
Suresh Siddha | e61d98d | 2008-07-10 11:16:35 -0700 | [diff] [blame] | 939 | |
Donald Dutile | 6f5cf52 | 2012-06-04 17:29:02 -0400 | [diff] [blame] | 940 | err = map_iommu(iommu, drhd->reg_base_addr); |
| 941 | if (err) { |
| 942 | pr_err("IOMMU: failed to map %s\n", iommu->name); |
Suresh Siddha | e61d98d | 2008-07-10 11:16:35 -0700 | [diff] [blame] | 943 | goto error; |
| 944 | } |
Suresh Siddha | e61d98d | 2008-07-10 11:16:35 -0700 | [diff] [blame] | 945 | |
Donald Dutile | 6f5cf52 | 2012-06-04 17:29:02 -0400 | [diff] [blame] | 946 | err = -EINVAL; |
Weidong Han | 1b57368 | 2008-12-08 15:34:06 +0800 | [diff] [blame] | 947 | agaw = iommu_calculate_agaw(iommu); |
| 948 | if (agaw < 0) { |
Donald Dutile | bf947fcb | 2012-06-04 17:29:01 -0400 | [diff] [blame] | 949 | pr_err("Cannot get a valid agaw for iommu (seq_id = %d)\n", |
| 950 | iommu->seq_id); |
David Woodhouse | 0815565 | 2009-08-04 09:17:20 +0100 | [diff] [blame] | 951 | goto err_unmap; |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 952 | } |
| 953 | msagaw = iommu_calculate_max_sagaw(iommu); |
| 954 | if (msagaw < 0) { |
Donald Dutile | bf947fcb | 2012-06-04 17:29:01 -0400 | [diff] [blame] | 955 | pr_err("Cannot get a valid max agaw for iommu (seq_id = %d)\n", |
Weidong Han | 1b57368 | 2008-12-08 15:34:06 +0800 | [diff] [blame] | 956 | iommu->seq_id); |
David Woodhouse | 0815565 | 2009-08-04 09:17:20 +0100 | [diff] [blame] | 957 | goto err_unmap; |
Weidong Han | 1b57368 | 2008-12-08 15:34:06 +0800 | [diff] [blame] | 958 | } |
| 959 | iommu->agaw = agaw; |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 960 | iommu->msagaw = msagaw; |
David Woodhouse | 67ccac4 | 2014-03-09 13:49:45 -0700 | [diff] [blame] | 961 | iommu->segment = drhd->segment; |
Weidong Han | 1b57368 | 2008-12-08 15:34:06 +0800 | [diff] [blame] | 962 | |
Suresh Siddha | ee34b32 | 2009-10-02 11:01:21 -0700 | [diff] [blame] | 963 | iommu->node = -1; |
| 964 | |
Suresh Siddha | e61d98d | 2008-07-10 11:16:35 -0700 | [diff] [blame] | 965 | ver = readl(iommu->reg + DMAR_VER_REG); |
Yinghai Lu | 680a752 | 2010-04-08 19:58:23 +0100 | [diff] [blame] | 966 | pr_info("IOMMU %d: reg_base_addr %llx ver %d:%d cap %llx ecap %llx\n", |
| 967 | iommu->seq_id, |
Fenghua Yu | 5b6985c | 2008-10-16 18:02:32 -0700 | [diff] [blame] | 968 | (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 Siddha | e61d98d | 2008-07-10 11:16:35 -0700 | [diff] [blame] | 972 | |
Takao Indoh | 3a93c84 | 2013-04-23 17:35:03 +0900 | [diff] [blame] | 973 | /* 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 Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 982 | raw_spin_lock_init(&iommu->register_lock); |
Suresh Siddha | e61d98d | 2008-07-10 11:16:35 -0700 | [diff] [blame] | 983 | |
| 984 | drhd->iommu = iommu; |
Alex Williamson | a5459cf | 2014-06-12 16:12:31 -0600 | [diff] [blame] | 985 | |
| 986 | if (intel_iommu_enabled) |
| 987 | iommu->iommu_dev = iommu_device_create(NULL, iommu, |
| 988 | intel_iommu_groups, |
| 989 | iommu->name); |
| 990 | |
Suresh Siddha | 1886e8a | 2008-07-10 11:16:37 -0700 | [diff] [blame] | 991 | return 0; |
David Woodhouse | 0815565 | 2009-08-04 09:17:20 +0100 | [diff] [blame] | 992 | |
| 993 | err_unmap: |
Donald Dutile | 6f5cf52 | 2012-06-04 17:29:02 -0400 | [diff] [blame] | 994 | unmap_iommu(iommu); |
David Woodhouse | 0815565 | 2009-08-04 09:17:20 +0100 | [diff] [blame] | 995 | error: |
Suresh Siddha | e61d98d | 2008-07-10 11:16:35 -0700 | [diff] [blame] | 996 | kfree(iommu); |
Donald Dutile | 6f5cf52 | 2012-06-04 17:29:02 -0400 | [diff] [blame] | 997 | return err; |
Suresh Siddha | e61d98d | 2008-07-10 11:16:35 -0700 | [diff] [blame] | 998 | } |
| 999 | |
Jiang Liu | a868e6b | 2014-01-06 14:18:20 +0800 | [diff] [blame] | 1000 | static void free_iommu(struct intel_iommu *iommu) |
Suresh Siddha | e61d98d | 2008-07-10 11:16:35 -0700 | [diff] [blame] | 1001 | { |
Alex Williamson | a5459cf | 2014-06-12 16:12:31 -0600 | [diff] [blame] | 1002 | iommu_device_destroy(iommu->iommu_dev); |
| 1003 | |
Jiang Liu | a868e6b | 2014-01-06 14:18:20 +0800 | [diff] [blame] | 1004 | if (iommu->irq) { |
| 1005 | free_irq(iommu->irq, iommu); |
| 1006 | irq_set_handler_data(iommu->irq, NULL); |
Thomas Gleixner | a553b14 | 2014-05-07 15:44:11 +0000 | [diff] [blame] | 1007 | dmar_free_hwirq(iommu->irq); |
Jiang Liu | a868e6b | 2014-01-06 14:18:20 +0800 | [diff] [blame] | 1008 | } |
Suresh Siddha | e61d98d | 2008-07-10 11:16:35 -0700 | [diff] [blame] | 1009 | |
Jiang Liu | a84da70 | 2014-01-06 14:18:23 +0800 | [diff] [blame] | 1010 | if (iommu->qi) { |
| 1011 | free_page((unsigned long)iommu->qi->desc); |
| 1012 | kfree(iommu->qi->desc_status); |
| 1013 | kfree(iommu->qi); |
| 1014 | } |
| 1015 | |
Suresh Siddha | e61d98d | 2008-07-10 11:16:35 -0700 | [diff] [blame] | 1016 | if (iommu->reg) |
Donald Dutile | 6f5cf52 | 2012-06-04 17:29:02 -0400 | [diff] [blame] | 1017 | unmap_iommu(iommu); |
| 1018 | |
Suresh Siddha | e61d98d | 2008-07-10 11:16:35 -0700 | [diff] [blame] | 1019 | kfree(iommu); |
| 1020 | } |
Suresh Siddha | fe962e9 | 2008-07-10 11:16:42 -0700 | [diff] [blame] | 1021 | |
| 1022 | /* |
| 1023 | * Reclaim all the submitted descriptors which have completed its work. |
| 1024 | */ |
| 1025 | static inline void reclaim_free_desc(struct q_inval *qi) |
| 1026 | { |
Yu Zhao | 6ba6c3a | 2009-05-18 13:51:35 +0800 | [diff] [blame] | 1027 | while (qi->desc_status[qi->free_tail] == QI_DONE || |
| 1028 | qi->desc_status[qi->free_tail] == QI_ABORT) { |
Suresh Siddha | fe962e9 | 2008-07-10 11:16:42 -0700 | [diff] [blame] | 1029 | 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 Zhao | 704126a | 2009-01-04 16:28:52 +0800 | [diff] [blame] | 1035 | static int qi_check_fault(struct intel_iommu *iommu, int index) |
| 1036 | { |
| 1037 | u32 fault; |
Yu Zhao | 6ba6c3a | 2009-05-18 13:51:35 +0800 | [diff] [blame] | 1038 | int head, tail; |
Yu Zhao | 704126a | 2009-01-04 16:28:52 +0800 | [diff] [blame] | 1039 | struct q_inval *qi = iommu->qi; |
| 1040 | int wait_index = (index + 1) % QI_LENGTH; |
| 1041 | |
Yu Zhao | 6ba6c3a | 2009-05-18 13:51:35 +0800 | [diff] [blame] | 1042 | if (qi->desc_status[wait_index] == QI_ABORT) |
| 1043 | return -EAGAIN; |
| 1044 | |
Yu Zhao | 704126a | 2009-01-04 16:28:52 +0800 | [diff] [blame] | 1045 | 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 Zhao | 6ba6c3a | 2009-05-18 13:51:35 +0800 | [diff] [blame] | 1054 | if ((head >> DMAR_IQ_SHIFT) == index) { |
Donald Dutile | bf947fcb | 2012-06-04 17:29:01 -0400 | [diff] [blame] | 1055 | pr_err("VT-d detected invalid descriptor: " |
Yu Zhao | 6ba6c3a | 2009-05-18 13:51:35 +0800 | [diff] [blame] | 1056 | "low=%llx, high=%llx\n", |
| 1057 | (unsigned long long)qi->desc[index].low, |
| 1058 | (unsigned long long)qi->desc[index].high); |
Yu Zhao | 704126a | 2009-01-04 16:28:52 +0800 | [diff] [blame] | 1059 | 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 Zhao | 6ba6c3a | 2009-05-18 13:51:35 +0800 | [diff] [blame] | 1068 | /* |
| 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 Zhao | 704126a | 2009-01-04 16:28:52 +0800 | [diff] [blame] | 1094 | return 0; |
| 1095 | } |
| 1096 | |
Suresh Siddha | fe962e9 | 2008-07-10 11:16:42 -0700 | [diff] [blame] | 1097 | /* |
| 1098 | * Submit the queued invalidation descriptor to the remapping |
| 1099 | * hardware unit and wait for its completion. |
| 1100 | */ |
Yu Zhao | 704126a | 2009-01-04 16:28:52 +0800 | [diff] [blame] | 1101 | int qi_submit_sync(struct qi_desc *desc, struct intel_iommu *iommu) |
Suresh Siddha | fe962e9 | 2008-07-10 11:16:42 -0700 | [diff] [blame] | 1102 | { |
Yu Zhao | 6ba6c3a | 2009-05-18 13:51:35 +0800 | [diff] [blame] | 1103 | int rc; |
Suresh Siddha | fe962e9 | 2008-07-10 11:16:42 -0700 | [diff] [blame] | 1104 | 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 Zhao | 704126a | 2009-01-04 16:28:52 +0800 | [diff] [blame] | 1110 | return 0; |
Suresh Siddha | fe962e9 | 2008-07-10 11:16:42 -0700 | [diff] [blame] | 1111 | |
| 1112 | hw = qi->desc; |
| 1113 | |
Yu Zhao | 6ba6c3a | 2009-05-18 13:51:35 +0800 | [diff] [blame] | 1114 | restart: |
| 1115 | rc = 0; |
| 1116 | |
Thomas Gleixner | 3b8f404 | 2011-07-19 17:02:07 +0200 | [diff] [blame] | 1117 | raw_spin_lock_irqsave(&qi->q_lock, flags); |
Suresh Siddha | fe962e9 | 2008-07-10 11:16:42 -0700 | [diff] [blame] | 1118 | while (qi->free_cnt < 3) { |
Thomas Gleixner | 3b8f404 | 2011-07-19 17:02:07 +0200 | [diff] [blame] | 1119 | raw_spin_unlock_irqrestore(&qi->q_lock, flags); |
Suresh Siddha | fe962e9 | 2008-07-10 11:16:42 -0700 | [diff] [blame] | 1120 | cpu_relax(); |
Thomas Gleixner | 3b8f404 | 2011-07-19 17:02:07 +0200 | [diff] [blame] | 1121 | raw_spin_lock_irqsave(&qi->q_lock, flags); |
Suresh Siddha | fe962e9 | 2008-07-10 11:16:42 -0700 | [diff] [blame] | 1122 | } |
| 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 Zhao | 704126a | 2009-01-04 16:28:52 +0800 | [diff] [blame] | 1131 | wait_desc.low = QI_IWD_STATUS_DATA(QI_DONE) | |
| 1132 | QI_IWD_STATUS_WRITE | QI_IWD_TYPE; |
Suresh Siddha | fe962e9 | 2008-07-10 11:16:42 -0700 | [diff] [blame] | 1133 | 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 Siddha | fe962e9 | 2008-07-10 11:16:42 -0700 | [diff] [blame] | 1143 | /* |
| 1144 | * update the HW tail register indicating the presence of |
| 1145 | * new descriptors. |
| 1146 | */ |
Yu Zhao | 6ba6c3a | 2009-05-18 13:51:35 +0800 | [diff] [blame] | 1147 | writel(qi->free_head << DMAR_IQ_SHIFT, iommu->reg + DMAR_IQT_REG); |
Suresh Siddha | fe962e9 | 2008-07-10 11:16:42 -0700 | [diff] [blame] | 1148 | |
| 1149 | while (qi->desc_status[wait_index] != QI_DONE) { |
Suresh Siddha | f05810c | 2008-10-16 16:31:54 -0700 | [diff] [blame] | 1150 | /* |
| 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 Zhao | 704126a | 2009-01-04 16:28:52 +0800 | [diff] [blame] | 1157 | rc = qi_check_fault(iommu, index); |
| 1158 | if (rc) |
Yu Zhao | 6ba6c3a | 2009-05-18 13:51:35 +0800 | [diff] [blame] | 1159 | break; |
Yu Zhao | 704126a | 2009-01-04 16:28:52 +0800 | [diff] [blame] | 1160 | |
Thomas Gleixner | 3b8f404 | 2011-07-19 17:02:07 +0200 | [diff] [blame] | 1161 | raw_spin_unlock(&qi->q_lock); |
Suresh Siddha | fe962e9 | 2008-07-10 11:16:42 -0700 | [diff] [blame] | 1162 | cpu_relax(); |
Thomas Gleixner | 3b8f404 | 2011-07-19 17:02:07 +0200 | [diff] [blame] | 1163 | raw_spin_lock(&qi->q_lock); |
Suresh Siddha | fe962e9 | 2008-07-10 11:16:42 -0700 | [diff] [blame] | 1164 | } |
Yu Zhao | 6ba6c3a | 2009-05-18 13:51:35 +0800 | [diff] [blame] | 1165 | |
| 1166 | qi->desc_status[index] = QI_DONE; |
Suresh Siddha | fe962e9 | 2008-07-10 11:16:42 -0700 | [diff] [blame] | 1167 | |
| 1168 | reclaim_free_desc(qi); |
Thomas Gleixner | 3b8f404 | 2011-07-19 17:02:07 +0200 | [diff] [blame] | 1169 | raw_spin_unlock_irqrestore(&qi->q_lock, flags); |
Yu Zhao | 704126a | 2009-01-04 16:28:52 +0800 | [diff] [blame] | 1170 | |
Yu Zhao | 6ba6c3a | 2009-05-18 13:51:35 +0800 | [diff] [blame] | 1171 | if (rc == -EAGAIN) |
| 1172 | goto restart; |
| 1173 | |
Yu Zhao | 704126a | 2009-01-04 16:28:52 +0800 | [diff] [blame] | 1174 | return rc; |
Suresh Siddha | fe962e9 | 2008-07-10 11:16:42 -0700 | [diff] [blame] | 1175 | } |
| 1176 | |
| 1177 | /* |
| 1178 | * Flush the global interrupt entry cache. |
| 1179 | */ |
| 1180 | void 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 Zhao | 704126a | 2009-01-04 16:28:52 +0800 | [diff] [blame] | 1187 | /* should never fail */ |
Suresh Siddha | fe962e9 | 2008-07-10 11:16:42 -0700 | [diff] [blame] | 1188 | qi_submit_sync(&desc, iommu); |
| 1189 | } |
| 1190 | |
David Woodhouse | 4c25a2c | 2009-05-10 17:16:06 +0100 | [diff] [blame] | 1191 | void qi_flush_context(struct intel_iommu *iommu, u16 did, u16 sid, u8 fm, |
| 1192 | u64 type) |
Youquan Song | 3481f21 | 2008-10-16 16:31:55 -0700 | [diff] [blame] | 1193 | { |
Youquan Song | 3481f21 | 2008-10-16 16:31:55 -0700 | [diff] [blame] | 1194 | struct qi_desc desc; |
| 1195 | |
Youquan Song | 3481f21 | 2008-10-16 16:31:55 -0700 | [diff] [blame] | 1196 | 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 Woodhouse | 4c25a2c | 2009-05-10 17:16:06 +0100 | [diff] [blame] | 1200 | qi_submit_sync(&desc, iommu); |
Youquan Song | 3481f21 | 2008-10-16 16:31:55 -0700 | [diff] [blame] | 1201 | } |
| 1202 | |
David Woodhouse | 1f0ef2a | 2009-05-10 19:58:49 +0100 | [diff] [blame] | 1203 | void qi_flush_iotlb(struct intel_iommu *iommu, u16 did, u64 addr, |
| 1204 | unsigned int size_order, u64 type) |
Youquan Song | 3481f21 | 2008-10-16 16:31:55 -0700 | [diff] [blame] | 1205 | { |
| 1206 | u8 dw = 0, dr = 0; |
| 1207 | |
| 1208 | struct qi_desc desc; |
| 1209 | int ih = 0; |
| 1210 | |
Youquan Song | 3481f21 | 2008-10-16 16:31:55 -0700 | [diff] [blame] | 1211 | 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 Woodhouse | 1f0ef2a | 2009-05-10 19:58:49 +0100 | [diff] [blame] | 1222 | qi_submit_sync(&desc, iommu); |
Youquan Song | 3481f21 | 2008-10-16 16:31:55 -0700 | [diff] [blame] | 1223 | } |
| 1224 | |
Yu Zhao | 6ba6c3a | 2009-05-18 13:51:35 +0800 | [diff] [blame] | 1225 | void 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 Siddha | fe962e9 | 2008-07-10 11:16:42 -0700 | [diff] [blame] | 1246 | /* |
Suresh Siddha | eba67e5 | 2009-03-16 17:04:56 -0700 | [diff] [blame] | 1247 | * Disable Queued Invalidation interface. |
| 1248 | */ |
| 1249 | void 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 Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 1258 | raw_spin_lock_irqsave(&iommu->register_lock, flags); |
Suresh Siddha | eba67e5 | 2009-03-16 17:04:56 -0700 | [diff] [blame] | 1259 | |
| 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 Siddha | eba67e5 | 2009-03-16 17:04:56 -0700 | [diff] [blame] | 1273 | writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG); |
| 1274 | |
| 1275 | IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG, readl, |
| 1276 | !(sts & DMA_GSTS_QIES), sts); |
| 1277 | end: |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 1278 | raw_spin_unlock_irqrestore(&iommu->register_lock, flags); |
Suresh Siddha | eba67e5 | 2009-03-16 17:04:56 -0700 | [diff] [blame] | 1279 | } |
| 1280 | |
| 1281 | /* |
Fenghua Yu | eb4a52b | 2009-03-27 14:22:43 -0700 | [diff] [blame] | 1282 | * Enable queued invalidation. |
| 1283 | */ |
| 1284 | static void __dmar_enable_qi(struct intel_iommu *iommu) |
| 1285 | { |
David Woodhouse | c416daa | 2009-05-10 20:30:58 +0100 | [diff] [blame] | 1286 | u32 sts; |
Fenghua Yu | eb4a52b | 2009-03-27 14:22:43 -0700 | [diff] [blame] | 1287 | 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 Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 1293 | raw_spin_lock_irqsave(&iommu->register_lock, flags); |
Fenghua Yu | eb4a52b | 2009-03-27 14:22:43 -0700 | [diff] [blame] | 1294 | |
| 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 Yu | eb4a52b | 2009-03-27 14:22:43 -0700 | [diff] [blame] | 1300 | iommu->gcmd |= DMA_GCMD_QIE; |
David Woodhouse | c416daa | 2009-05-10 20:30:58 +0100 | [diff] [blame] | 1301 | writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG); |
Fenghua Yu | eb4a52b | 2009-03-27 14:22:43 -0700 | [diff] [blame] | 1302 | |
| 1303 | /* Make sure hardware complete it */ |
| 1304 | IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG, readl, (sts & DMA_GSTS_QIES), sts); |
| 1305 | |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 1306 | raw_spin_unlock_irqrestore(&iommu->register_lock, flags); |
Fenghua Yu | eb4a52b | 2009-03-27 14:22:43 -0700 | [diff] [blame] | 1307 | } |
| 1308 | |
| 1309 | /* |
Suresh Siddha | fe962e9 | 2008-07-10 11:16:42 -0700 | [diff] [blame] | 1310 | * 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 | */ |
| 1314 | int dmar_enable_qi(struct intel_iommu *iommu) |
| 1315 | { |
Suresh Siddha | fe962e9 | 2008-07-10 11:16:42 -0700 | [diff] [blame] | 1316 | struct q_inval *qi; |
Suresh Siddha | 751cafe | 2009-10-02 11:01:22 -0700 | [diff] [blame] | 1317 | struct page *desc_page; |
Suresh Siddha | fe962e9 | 2008-07-10 11:16:42 -0700 | [diff] [blame] | 1318 | |
| 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 Siddha | fa4b57c | 2009-03-16 17:05:05 -0700 | [diff] [blame] | 1328 | iommu->qi = kmalloc(sizeof(*qi), GFP_ATOMIC); |
Suresh Siddha | fe962e9 | 2008-07-10 11:16:42 -0700 | [diff] [blame] | 1329 | if (!iommu->qi) |
| 1330 | return -ENOMEM; |
| 1331 | |
| 1332 | qi = iommu->qi; |
| 1333 | |
Suresh Siddha | 751cafe | 2009-10-02 11:01:22 -0700 | [diff] [blame] | 1334 | |
| 1335 | desc_page = alloc_pages_node(iommu->node, GFP_ATOMIC | __GFP_ZERO, 0); |
| 1336 | if (!desc_page) { |
Suresh Siddha | fe962e9 | 2008-07-10 11:16:42 -0700 | [diff] [blame] | 1337 | kfree(qi); |
Jiang Liu | b707cb0 | 2014-01-06 14:18:26 +0800 | [diff] [blame] | 1338 | iommu->qi = NULL; |
Suresh Siddha | fe962e9 | 2008-07-10 11:16:42 -0700 | [diff] [blame] | 1339 | return -ENOMEM; |
| 1340 | } |
| 1341 | |
Suresh Siddha | 751cafe | 2009-10-02 11:01:22 -0700 | [diff] [blame] | 1342 | qi->desc = page_address(desc_page); |
| 1343 | |
Hannes Reinecke | 37a4071 | 2013-02-06 09:50:10 +0100 | [diff] [blame] | 1344 | qi->desc_status = kzalloc(QI_LENGTH * sizeof(int), GFP_ATOMIC); |
Suresh Siddha | fe962e9 | 2008-07-10 11:16:42 -0700 | [diff] [blame] | 1345 | if (!qi->desc_status) { |
| 1346 | free_page((unsigned long) qi->desc); |
| 1347 | kfree(qi); |
Jiang Liu | b707cb0 | 2014-01-06 14:18:26 +0800 | [diff] [blame] | 1348 | iommu->qi = NULL; |
Suresh Siddha | fe962e9 | 2008-07-10 11:16:42 -0700 | [diff] [blame] | 1349 | return -ENOMEM; |
| 1350 | } |
| 1351 | |
Thomas Gleixner | 3b8f404 | 2011-07-19 17:02:07 +0200 | [diff] [blame] | 1352 | raw_spin_lock_init(&qi->q_lock); |
Suresh Siddha | fe962e9 | 2008-07-10 11:16:42 -0700 | [diff] [blame] | 1353 | |
Fenghua Yu | eb4a52b | 2009-03-27 14:22:43 -0700 | [diff] [blame] | 1354 | __dmar_enable_qi(iommu); |
Suresh Siddha | fe962e9 | 2008-07-10 11:16:42 -0700 | [diff] [blame] | 1355 | |
| 1356 | return 0; |
| 1357 | } |
Suresh Siddha | 0ac2491 | 2009-03-16 17:04:54 -0700 | [diff] [blame] | 1358 | |
| 1359 | /* iommu interrupt handling. Most stuff are MSI-like. */ |
| 1360 | |
Suresh Siddha | 9d783ba | 2009-03-16 17:04:55 -0700 | [diff] [blame] | 1361 | enum faulttype { |
| 1362 | DMA_REMAP, |
| 1363 | INTR_REMAP, |
| 1364 | UNKNOWN, |
| 1365 | }; |
| 1366 | |
| 1367 | static const char *dma_remap_fault_reasons[] = |
Suresh Siddha | 0ac2491 | 2009-03-16 17:04:54 -0700 | [diff] [blame] | 1368 | { |
| 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-Hua | 4ecccd9 | 2013-03-06 10:43:17 +0800 | [diff] [blame] | 1382 | "PCE for translation request specifies blocking", |
Suresh Siddha | 0ac2491 | 2009-03-16 17:04:54 -0700 | [diff] [blame] | 1383 | }; |
Suresh Siddha | 9d783ba | 2009-03-16 17:04:55 -0700 | [diff] [blame] | 1384 | |
Suresh Siddha | 95a02e9 | 2012-03-30 11:47:07 -0700 | [diff] [blame] | 1385 | static const char *irq_remap_fault_reasons[] = |
Suresh Siddha | 9d783ba | 2009-03-16 17:04:55 -0700 | [diff] [blame] | 1386 | { |
| 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 Kheria | 21004dc | 2013-12-18 12:01:46 +0530 | [diff] [blame] | 1396 | static const char *dmar_get_fault_reason(u8 fault_reason, int *fault_type) |
Suresh Siddha | 0ac2491 | 2009-03-16 17:04:54 -0700 | [diff] [blame] | 1397 | { |
Dan Carpenter | fefe1ed | 2012-05-13 20:09:38 +0300 | [diff] [blame] | 1398 | if (fault_reason >= 0x20 && (fault_reason - 0x20 < |
| 1399 | ARRAY_SIZE(irq_remap_fault_reasons))) { |
Suresh Siddha | 9d783ba | 2009-03-16 17:04:55 -0700 | [diff] [blame] | 1400 | *fault_type = INTR_REMAP; |
Suresh Siddha | 95a02e9 | 2012-03-30 11:47:07 -0700 | [diff] [blame] | 1401 | return irq_remap_fault_reasons[fault_reason - 0x20]; |
Suresh Siddha | 9d783ba | 2009-03-16 17:04:55 -0700 | [diff] [blame] | 1402 | } 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 Siddha | 0ac2491 | 2009-03-16 17:04:54 -0700 | [diff] [blame] | 1407 | return "Unknown"; |
Suresh Siddha | 9d783ba | 2009-03-16 17:04:55 -0700 | [diff] [blame] | 1408 | } |
Suresh Siddha | 0ac2491 | 2009-03-16 17:04:54 -0700 | [diff] [blame] | 1409 | } |
| 1410 | |
Thomas Gleixner | 5c2837f | 2010-09-28 17:15:11 +0200 | [diff] [blame] | 1411 | void dmar_msi_unmask(struct irq_data *data) |
Suresh Siddha | 0ac2491 | 2009-03-16 17:04:54 -0700 | [diff] [blame] | 1412 | { |
Thomas Gleixner | dced35a | 2011-03-28 17:49:12 +0200 | [diff] [blame] | 1413 | struct intel_iommu *iommu = irq_data_get_irq_handler_data(data); |
Suresh Siddha | 0ac2491 | 2009-03-16 17:04:54 -0700 | [diff] [blame] | 1414 | unsigned long flag; |
| 1415 | |
| 1416 | /* unmask it */ |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 1417 | raw_spin_lock_irqsave(&iommu->register_lock, flag); |
Suresh Siddha | 0ac2491 | 2009-03-16 17:04:54 -0700 | [diff] [blame] | 1418 | 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 Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 1421 | raw_spin_unlock_irqrestore(&iommu->register_lock, flag); |
Suresh Siddha | 0ac2491 | 2009-03-16 17:04:54 -0700 | [diff] [blame] | 1422 | } |
| 1423 | |
Thomas Gleixner | 5c2837f | 2010-09-28 17:15:11 +0200 | [diff] [blame] | 1424 | void dmar_msi_mask(struct irq_data *data) |
Suresh Siddha | 0ac2491 | 2009-03-16 17:04:54 -0700 | [diff] [blame] | 1425 | { |
| 1426 | unsigned long flag; |
Thomas Gleixner | dced35a | 2011-03-28 17:49:12 +0200 | [diff] [blame] | 1427 | struct intel_iommu *iommu = irq_data_get_irq_handler_data(data); |
Suresh Siddha | 0ac2491 | 2009-03-16 17:04:54 -0700 | [diff] [blame] | 1428 | |
| 1429 | /* mask it */ |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 1430 | raw_spin_lock_irqsave(&iommu->register_lock, flag); |
Suresh Siddha | 0ac2491 | 2009-03-16 17:04:54 -0700 | [diff] [blame] | 1431 | 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 Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 1434 | raw_spin_unlock_irqrestore(&iommu->register_lock, flag); |
Suresh Siddha | 0ac2491 | 2009-03-16 17:04:54 -0700 | [diff] [blame] | 1435 | } |
| 1436 | |
| 1437 | void dmar_msi_write(int irq, struct msi_msg *msg) |
| 1438 | { |
Thomas Gleixner | dced35a | 2011-03-28 17:49:12 +0200 | [diff] [blame] | 1439 | struct intel_iommu *iommu = irq_get_handler_data(irq); |
Suresh Siddha | 0ac2491 | 2009-03-16 17:04:54 -0700 | [diff] [blame] | 1440 | unsigned long flag; |
| 1441 | |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 1442 | raw_spin_lock_irqsave(&iommu->register_lock, flag); |
Suresh Siddha | 0ac2491 | 2009-03-16 17:04:54 -0700 | [diff] [blame] | 1443 | 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 Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 1446 | raw_spin_unlock_irqrestore(&iommu->register_lock, flag); |
Suresh Siddha | 0ac2491 | 2009-03-16 17:04:54 -0700 | [diff] [blame] | 1447 | } |
| 1448 | |
| 1449 | void dmar_msi_read(int irq, struct msi_msg *msg) |
| 1450 | { |
Thomas Gleixner | dced35a | 2011-03-28 17:49:12 +0200 | [diff] [blame] | 1451 | struct intel_iommu *iommu = irq_get_handler_data(irq); |
Suresh Siddha | 0ac2491 | 2009-03-16 17:04:54 -0700 | [diff] [blame] | 1452 | unsigned long flag; |
| 1453 | |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 1454 | raw_spin_lock_irqsave(&iommu->register_lock, flag); |
Suresh Siddha | 0ac2491 | 2009-03-16 17:04:54 -0700 | [diff] [blame] | 1455 | 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 Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 1458 | raw_spin_unlock_irqrestore(&iommu->register_lock, flag); |
Suresh Siddha | 0ac2491 | 2009-03-16 17:04:54 -0700 | [diff] [blame] | 1459 | } |
| 1460 | |
| 1461 | static 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 Siddha | 9d783ba | 2009-03-16 17:04:55 -0700 | [diff] [blame] | 1465 | int fault_type; |
Suresh Siddha | 0ac2491 | 2009-03-16 17:04:54 -0700 | [diff] [blame] | 1466 | |
Suresh Siddha | 9d783ba | 2009-03-16 17:04:55 -0700 | [diff] [blame] | 1467 | reason = dmar_get_fault_reason(fault_reason, &fault_type); |
Suresh Siddha | 0ac2491 | 2009-03-16 17:04:54 -0700 | [diff] [blame] | 1468 | |
Suresh Siddha | 9d783ba | 2009-03-16 17:04:55 -0700 | [diff] [blame] | 1469 | if (fault_type == INTR_REMAP) |
Donald Dutile | bf947fcb | 2012-06-04 17:29:01 -0400 | [diff] [blame] | 1470 | pr_err("INTR-REMAP: Request device [[%02x:%02x.%d] " |
Suresh Siddha | 9d783ba | 2009-03-16 17:04:55 -0700 | [diff] [blame] | 1471 | "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 Dutile | bf947fcb | 2012-06-04 17:29:01 -0400 | [diff] [blame] | 1477 | pr_err("DMAR:[%s] Request device [%02x:%02x.%d] " |
Suresh Siddha | 9d783ba | 2009-03-16 17:04:55 -0700 | [diff] [blame] | 1478 | "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 Siddha | 0ac2491 | 2009-03-16 17:04:54 -0700 | [diff] [blame] | 1483 | return 0; |
| 1484 | } |
| 1485 | |
| 1486 | #define PRIMARY_FAULT_REG_LEN (16) |
Suresh Siddha | 1531a6a | 2009-03-16 17:04:57 -0700 | [diff] [blame] | 1487 | irqreturn_t dmar_fault(int irq, void *dev_id) |
Suresh Siddha | 0ac2491 | 2009-03-16 17:04:54 -0700 | [diff] [blame] | 1488 | { |
| 1489 | struct intel_iommu *iommu = dev_id; |
| 1490 | int reg, fault_index; |
| 1491 | u32 fault_status; |
| 1492 | unsigned long flag; |
| 1493 | |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 1494 | raw_spin_lock_irqsave(&iommu->register_lock, flag); |
Suresh Siddha | 0ac2491 | 2009-03-16 17:04:54 -0700 | [diff] [blame] | 1495 | fault_status = readl(iommu->reg + DMAR_FSTS_REG); |
Suresh Siddha | 9d783ba | 2009-03-16 17:04:55 -0700 | [diff] [blame] | 1496 | if (fault_status) |
Donald Dutile | bf947fcb | 2012-06-04 17:29:01 -0400 | [diff] [blame] | 1497 | pr_err("DRHD: handling fault status reg %x\n", fault_status); |
Suresh Siddha | 0ac2491 | 2009-03-16 17:04:54 -0700 | [diff] [blame] | 1498 | |
| 1499 | /* TBD: ignore advanced fault log currently */ |
| 1500 | if (!(fault_status & DMA_FSTS_PPF)) |
Li, Zhen-Hua | bd5cdad | 2013-03-25 16:20:52 +0800 | [diff] [blame] | 1501 | goto unlock_exit; |
Suresh Siddha | 0ac2491 | 2009-03-16 17:04:54 -0700 | [diff] [blame] | 1502 | |
| 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 Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 1532 | raw_spin_unlock_irqrestore(&iommu->register_lock, flag); |
Suresh Siddha | 0ac2491 | 2009-03-16 17:04:54 -0700 | [diff] [blame] | 1533 | |
| 1534 | dmar_fault_do_one(iommu, type, fault_reason, |
| 1535 | source_id, guest_addr); |
| 1536 | |
| 1537 | fault_index++; |
Troy Heber | 8211a7b | 2009-08-19 15:26:11 -0600 | [diff] [blame] | 1538 | if (fault_index >= cap_num_fault_regs(iommu->cap)) |
Suresh Siddha | 0ac2491 | 2009-03-16 17:04:54 -0700 | [diff] [blame] | 1539 | fault_index = 0; |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 1540 | raw_spin_lock_irqsave(&iommu->register_lock, flag); |
Suresh Siddha | 0ac2491 | 2009-03-16 17:04:54 -0700 | [diff] [blame] | 1541 | } |
Suresh Siddha | 0ac2491 | 2009-03-16 17:04:54 -0700 | [diff] [blame] | 1542 | |
Li, Zhen-Hua | bd5cdad | 2013-03-25 16:20:52 +0800 | [diff] [blame] | 1543 | writel(DMA_FSTS_PFO | DMA_FSTS_PPF, iommu->reg + DMAR_FSTS_REG); |
| 1544 | |
| 1545 | unlock_exit: |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 1546 | raw_spin_unlock_irqrestore(&iommu->register_lock, flag); |
Suresh Siddha | 0ac2491 | 2009-03-16 17:04:54 -0700 | [diff] [blame] | 1547 | return IRQ_HANDLED; |
| 1548 | } |
| 1549 | |
| 1550 | int dmar_set_interrupt(struct intel_iommu *iommu) |
| 1551 | { |
| 1552 | int irq, ret; |
| 1553 | |
Suresh Siddha | 9d783ba | 2009-03-16 17:04:55 -0700 | [diff] [blame] | 1554 | /* |
| 1555 | * Check if the fault interrupt is already initialized. |
| 1556 | */ |
| 1557 | if (iommu->irq) |
| 1558 | return 0; |
| 1559 | |
Thomas Gleixner | a553b14 | 2014-05-07 15:44:11 +0000 | [diff] [blame] | 1560 | irq = dmar_alloc_hwirq(); |
Thomas Gleixner | aa5125a | 2014-05-07 15:44:10 +0000 | [diff] [blame] | 1561 | if (irq <= 0) { |
Donald Dutile | bf947fcb | 2012-06-04 17:29:01 -0400 | [diff] [blame] | 1562 | pr_err("IOMMU: no free vectors\n"); |
Suresh Siddha | 0ac2491 | 2009-03-16 17:04:54 -0700 | [diff] [blame] | 1563 | return -EINVAL; |
| 1564 | } |
| 1565 | |
Thomas Gleixner | dced35a | 2011-03-28 17:49:12 +0200 | [diff] [blame] | 1566 | irq_set_handler_data(irq, iommu); |
Suresh Siddha | 0ac2491 | 2009-03-16 17:04:54 -0700 | [diff] [blame] | 1567 | iommu->irq = irq; |
| 1568 | |
| 1569 | ret = arch_setup_dmar_msi(irq); |
| 1570 | if (ret) { |
Thomas Gleixner | dced35a | 2011-03-28 17:49:12 +0200 | [diff] [blame] | 1571 | irq_set_handler_data(irq, NULL); |
Suresh Siddha | 0ac2491 | 2009-03-16 17:04:54 -0700 | [diff] [blame] | 1572 | iommu->irq = 0; |
Thomas Gleixner | a553b14 | 2014-05-07 15:44:11 +0000 | [diff] [blame] | 1573 | dmar_free_hwirq(irq); |
Chris Wright | dd72643 | 2009-05-13 15:55:52 -0700 | [diff] [blame] | 1574 | return ret; |
Suresh Siddha | 0ac2491 | 2009-03-16 17:04:54 -0700 | [diff] [blame] | 1575 | } |
| 1576 | |
Thomas Gleixner | 477694e | 2011-07-19 16:25:42 +0200 | [diff] [blame] | 1577 | ret = request_irq(irq, dmar_fault, IRQF_NO_THREAD, iommu->name, iommu); |
Suresh Siddha | 0ac2491 | 2009-03-16 17:04:54 -0700 | [diff] [blame] | 1578 | if (ret) |
Donald Dutile | bf947fcb | 2012-06-04 17:29:01 -0400 | [diff] [blame] | 1579 | pr_err("IOMMU: can't request irq\n"); |
Suresh Siddha | 0ac2491 | 2009-03-16 17:04:54 -0700 | [diff] [blame] | 1580 | return ret; |
| 1581 | } |
Suresh Siddha | 9d783ba | 2009-03-16 17:04:55 -0700 | [diff] [blame] | 1582 | |
| 1583 | int __init enable_drhd_fault_handling(void) |
| 1584 | { |
| 1585 | struct dmar_drhd_unit *drhd; |
Jiang Liu | 7c91977 | 2014-01-06 14:18:18 +0800 | [diff] [blame] | 1586 | struct intel_iommu *iommu; |
Suresh Siddha | 9d783ba | 2009-03-16 17:04:55 -0700 | [diff] [blame] | 1587 | |
| 1588 | /* |
| 1589 | * Enable fault control interrupt. |
| 1590 | */ |
Jiang Liu | 7c91977 | 2014-01-06 14:18:18 +0800 | [diff] [blame] | 1591 | for_each_iommu(iommu, drhd) { |
Li, Zhen-Hua | bd5cdad | 2013-03-25 16:20:52 +0800 | [diff] [blame] | 1592 | u32 fault_status; |
Jiang Liu | 7c91977 | 2014-01-06 14:18:18 +0800 | [diff] [blame] | 1593 | int ret = dmar_set_interrupt(iommu); |
Suresh Siddha | 9d783ba | 2009-03-16 17:04:55 -0700 | [diff] [blame] | 1594 | |
| 1595 | if (ret) { |
Donald Dutile | e9071b0 | 2012-06-08 17:13:11 -0400 | [diff] [blame] | 1596 | pr_err("DRHD %Lx: failed to enable fault, interrupt, ret %d\n", |
Suresh Siddha | 9d783ba | 2009-03-16 17:04:55 -0700 | [diff] [blame] | 1597 | (unsigned long long)drhd->reg_base_addr, ret); |
| 1598 | return -1; |
| 1599 | } |
Suresh Siddha | 7f99d94 | 2010-11-30 22:22:29 -0800 | [diff] [blame] | 1600 | |
| 1601 | /* |
| 1602 | * Clear any previous faults. |
| 1603 | */ |
| 1604 | dmar_fault(iommu->irq, iommu); |
Li, Zhen-Hua | bd5cdad | 2013-03-25 16:20:52 +0800 | [diff] [blame] | 1605 | fault_status = readl(iommu->reg + DMAR_FSTS_REG); |
| 1606 | writel(fault_status, iommu->reg + DMAR_FSTS_REG); |
Suresh Siddha | 9d783ba | 2009-03-16 17:04:55 -0700 | [diff] [blame] | 1607 | } |
| 1608 | |
| 1609 | return 0; |
| 1610 | } |
Fenghua Yu | eb4a52b | 2009-03-27 14:22:43 -0700 | [diff] [blame] | 1611 | |
| 1612 | /* |
| 1613 | * Re-enable Queued Invalidation interface. |
| 1614 | */ |
| 1615 | int 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 Song | 074835f | 2009-09-09 12:05:39 -0400 | [diff] [blame] | 1636 | |
| 1637 | /* |
| 1638 | * Check interrupt remapping support in DMAR table description. |
| 1639 | */ |
Luck, Tony | 0b8973a | 2009-12-16 22:59:29 +0000 | [diff] [blame] | 1640 | int __init dmar_ir_support(void) |
Youquan Song | 074835f | 2009-09-09 12:05:39 -0400 | [diff] [blame] | 1641 | { |
| 1642 | struct acpi_table_dmar *dmar; |
| 1643 | dmar = (struct acpi_table_dmar *)dmar_tbl; |
Arnaud Patard | 4f506e0 | 2010-03-25 18:02:58 +0000 | [diff] [blame] | 1644 | if (!dmar) |
| 1645 | return 0; |
Youquan Song | 074835f | 2009-09-09 12:05:39 -0400 | [diff] [blame] | 1646 | return dmar->flags & 0x1; |
| 1647 | } |
Jiang Liu | 694835d | 2014-01-06 14:18:16 +0800 | [diff] [blame] | 1648 | |
Jiang Liu | a868e6b | 2014-01-06 14:18:20 +0800 | [diff] [blame] | 1649 | static 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 Liu | 2e45528 | 2014-02-19 14:07:36 +0800 | [diff] [blame] | 1657 | if (dmar_dev_scope_status != 1 && !list_empty(&dmar_drhd_units)) |
| 1658 | bus_unregister_notifier(&pci_bus_type, &dmar_pci_bus_nb); |
Jiang Liu | 59ce051 | 2014-02-19 14:07:35 +0800 | [diff] [blame] | 1659 | |
Jiang Liu | 3a5670e | 2014-02-19 14:07:33 +0800 | [diff] [blame] | 1660 | down_write(&dmar_global_lock); |
Jiang Liu | a868e6b | 2014-01-06 14:18:20 +0800 | [diff] [blame] | 1661 | list_for_each_entry_safe(dmaru, dmaru_n, &dmar_drhd_units, list) { |
| 1662 | list_del(&dmaru->list); |
| 1663 | dmar_free_drhd(dmaru); |
| 1664 | } |
Jiang Liu | 3a5670e | 2014-02-19 14:07:33 +0800 | [diff] [blame] | 1665 | up_write(&dmar_global_lock); |
Jiang Liu | a868e6b | 2014-01-06 14:18:20 +0800 | [diff] [blame] | 1666 | |
| 1667 | return 0; |
| 1668 | } |
| 1669 | |
| 1670 | late_initcall(dmar_free_unused_resources); |
Konrad Rzeszutek Wilk | 4db77ff | 2010-08-26 13:58:04 -0400 | [diff] [blame] | 1671 | IOMMU_INIT_POST(detect_intel_iommu); |