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> |
Suresh Siddha | 8a8f422 | 2012-03-30 11:47:08 -0700 | [diff] [blame] | 41 | #include <asm/irq_remapping.h> |
Konrad Rzeszutek Wilk | 4db77ff | 2010-08-26 13:58:04 -0400 | [diff] [blame] | 42 | #include <asm/iommu_table.h> |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 43 | |
Joerg Roedel | 078e1ee | 2012-09-26 12:44:43 +0200 | [diff] [blame] | 44 | #include "irq_remapping.h" |
| 45 | |
Jiang Liu | 3a5670e | 2014-02-19 14:07:33 +0800 | [diff] [blame] | 46 | /* |
| 47 | * Assumptions: |
| 48 | * 1) The hotplug framework guarentees that DMAR unit will be hot-added |
| 49 | * before IO devices managed by that unit. |
| 50 | * 2) The hotplug framework guarantees that DMAR unit will be hot-removed |
| 51 | * after IO devices managed by that unit. |
| 52 | * 3) Hotplug events are rare. |
| 53 | * |
| 54 | * Locking rules for DMA and interrupt remapping related global data structures: |
| 55 | * 1) Use dmar_global_lock in process context |
| 56 | * 2) Use RCU in interrupt context |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 57 | */ |
Jiang Liu | 3a5670e | 2014-02-19 14:07:33 +0800 | [diff] [blame] | 58 | DECLARE_RWSEM(dmar_global_lock); |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 59 | LIST_HEAD(dmar_drhd_units); |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 60 | |
Suresh Siddha | 41750d3 | 2011-08-23 17:05:18 -0700 | [diff] [blame] | 61 | struct acpi_table_header * __initdata dmar_tbl; |
Yinghai Lu | 8e1568f | 2009-02-11 01:06:59 -0800 | [diff] [blame] | 62 | static acpi_size dmar_tbl_size; |
Jiang Liu | 2e45528 | 2014-02-19 14:07:36 +0800 | [diff] [blame] | 63 | static int dmar_dev_scope_status = 1; |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 64 | |
Jiang Liu | 694835d | 2014-01-06 14:18:16 +0800 | [diff] [blame] | 65 | static int alloc_iommu(struct dmar_drhd_unit *drhd); |
Jiang Liu | a868e6b | 2014-01-06 14:18:20 +0800 | [diff] [blame] | 66 | static void free_iommu(struct intel_iommu *iommu); |
Jiang Liu | 694835d | 2014-01-06 14:18:16 +0800 | [diff] [blame] | 67 | |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 68 | static void __init dmar_register_drhd_unit(struct dmar_drhd_unit *drhd) |
| 69 | { |
| 70 | /* |
| 71 | * add INCLUDE_ALL at the tail, so scan the list will find it at |
| 72 | * the very end. |
| 73 | */ |
| 74 | if (drhd->include_all) |
Jiang Liu | 0e24261 | 2014-02-19 14:07:34 +0800 | [diff] [blame] | 75 | list_add_tail_rcu(&drhd->list, &dmar_drhd_units); |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 76 | else |
Jiang Liu | 0e24261 | 2014-02-19 14:07:34 +0800 | [diff] [blame] | 77 | list_add_rcu(&drhd->list, &dmar_drhd_units); |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Jiang Liu | bb3a6b7 | 2014-02-19 14:07:24 +0800 | [diff] [blame] | 80 | void *dmar_alloc_dev_scope(void *start, void *end, int *cnt) |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 81 | { |
| 82 | struct acpi_dmar_device_scope *scope; |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 83 | |
| 84 | *cnt = 0; |
| 85 | while (start < end) { |
| 86 | scope = start; |
David Woodhouse | 07cb52f | 2014-03-07 14:39:27 +0000 | [diff] [blame] | 87 | if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_ACPI || |
| 88 | scope->entry_type == ACPI_DMAR_SCOPE_TYPE_ENDPOINT || |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 89 | scope->entry_type == ACPI_DMAR_SCOPE_TYPE_BRIDGE) |
| 90 | (*cnt)++; |
Linn Crosetto | ae3e7f3 | 2013-04-23 12:26:45 -0600 | [diff] [blame] | 91 | else if (scope->entry_type != ACPI_DMAR_SCOPE_TYPE_IOAPIC && |
| 92 | scope->entry_type != ACPI_DMAR_SCOPE_TYPE_HPET) { |
Donald Dutile | e9071b0 | 2012-06-08 17:13:11 -0400 | [diff] [blame] | 93 | pr_warn("Unsupported device scope\n"); |
Yinghai Lu | 5715f0f | 2010-04-08 19:58:22 +0100 | [diff] [blame] | 94 | } |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 95 | start += scope->length; |
| 96 | } |
| 97 | if (*cnt == 0) |
Jiang Liu | bb3a6b7 | 2014-02-19 14:07:24 +0800 | [diff] [blame] | 98 | return NULL; |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 99 | |
David Woodhouse | 832bd85 | 2014-03-07 15:08:36 +0000 | [diff] [blame^] | 100 | return kcalloc(*cnt, sizeof(struct dmar_dev_scope), GFP_KERNEL); |
Jiang Liu | bb3a6b7 | 2014-02-19 14:07:24 +0800 | [diff] [blame] | 101 | } |
| 102 | |
David Woodhouse | 832bd85 | 2014-03-07 15:08:36 +0000 | [diff] [blame^] | 103 | void dmar_free_dev_scope(struct dmar_dev_scope **devices, int *cnt) |
Jiang Liu | ada4d4b | 2014-01-06 14:18:09 +0800 | [diff] [blame] | 104 | { |
Jiang Liu | b683b23 | 2014-02-19 14:07:32 +0800 | [diff] [blame] | 105 | int i; |
David Woodhouse | 832bd85 | 2014-03-07 15:08:36 +0000 | [diff] [blame^] | 106 | struct device *tmp_dev; |
Jiang Liu | b683b23 | 2014-02-19 14:07:32 +0800 | [diff] [blame] | 107 | |
Jiang Liu | ada4d4b | 2014-01-06 14:18:09 +0800 | [diff] [blame] | 108 | if (*devices && *cnt) { |
Jiang Liu | b683b23 | 2014-02-19 14:07:32 +0800 | [diff] [blame] | 109 | for_each_active_dev_scope(*devices, *cnt, i, tmp_dev) |
David Woodhouse | 832bd85 | 2014-03-07 15:08:36 +0000 | [diff] [blame^] | 110 | put_device(tmp_dev); |
Jiang Liu | ada4d4b | 2014-01-06 14:18:09 +0800 | [diff] [blame] | 111 | kfree(*devices); |
Jiang Liu | ada4d4b | 2014-01-06 14:18:09 +0800 | [diff] [blame] | 112 | } |
Jiang Liu | 0e24261 | 2014-02-19 14:07:34 +0800 | [diff] [blame] | 113 | |
| 114 | *devices = NULL; |
| 115 | *cnt = 0; |
Jiang Liu | ada4d4b | 2014-01-06 14:18:09 +0800 | [diff] [blame] | 116 | } |
| 117 | |
Jiang Liu | 59ce051 | 2014-02-19 14:07:35 +0800 | [diff] [blame] | 118 | /* Optimize out kzalloc()/kfree() for normal cases */ |
| 119 | static char dmar_pci_notify_info_buf[64]; |
| 120 | |
| 121 | static struct dmar_pci_notify_info * |
| 122 | dmar_alloc_pci_notify_info(struct pci_dev *dev, unsigned long event) |
| 123 | { |
| 124 | int level = 0; |
| 125 | size_t size; |
| 126 | struct pci_dev *tmp; |
| 127 | struct dmar_pci_notify_info *info; |
| 128 | |
| 129 | BUG_ON(dev->is_virtfn); |
| 130 | |
| 131 | /* Only generate path[] for device addition event */ |
| 132 | if (event == BUS_NOTIFY_ADD_DEVICE) |
| 133 | for (tmp = dev; tmp; tmp = tmp->bus->self) |
| 134 | level++; |
| 135 | |
| 136 | size = sizeof(*info) + level * sizeof(struct acpi_dmar_pci_path); |
| 137 | if (size <= sizeof(dmar_pci_notify_info_buf)) { |
| 138 | info = (struct dmar_pci_notify_info *)dmar_pci_notify_info_buf; |
| 139 | } else { |
| 140 | info = kzalloc(size, GFP_KERNEL); |
| 141 | if (!info) { |
| 142 | pr_warn("Out of memory when allocating notify_info " |
| 143 | "for %s.\n", pci_name(dev)); |
Jiang Liu | 2e45528 | 2014-02-19 14:07:36 +0800 | [diff] [blame] | 144 | if (dmar_dev_scope_status == 0) |
| 145 | dmar_dev_scope_status = -ENOMEM; |
Jiang Liu | 59ce051 | 2014-02-19 14:07:35 +0800 | [diff] [blame] | 146 | return NULL; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | info->event = event; |
| 151 | info->dev = dev; |
| 152 | info->seg = pci_domain_nr(dev->bus); |
| 153 | info->level = level; |
| 154 | if (event == BUS_NOTIFY_ADD_DEVICE) { |
| 155 | for (tmp = dev, level--; tmp; tmp = tmp->bus->self) { |
| 156 | info->path[level].device = PCI_SLOT(tmp->devfn); |
| 157 | info->path[level].function = PCI_FUNC(tmp->devfn); |
| 158 | if (pci_is_root_bus(tmp->bus)) |
| 159 | info->bus = tmp->bus->number; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | return info; |
| 164 | } |
| 165 | |
| 166 | static inline void dmar_free_pci_notify_info(struct dmar_pci_notify_info *info) |
| 167 | { |
| 168 | if ((void *)info != dmar_pci_notify_info_buf) |
| 169 | kfree(info); |
| 170 | } |
| 171 | |
| 172 | static bool dmar_match_pci_path(struct dmar_pci_notify_info *info, int bus, |
| 173 | struct acpi_dmar_pci_path *path, int count) |
| 174 | { |
| 175 | int i; |
| 176 | |
| 177 | if (info->bus != bus) |
| 178 | return false; |
| 179 | if (info->level != count) |
| 180 | return false; |
| 181 | |
| 182 | for (i = 0; i < count; i++) { |
| 183 | if (path[i].device != info->path[i].device || |
| 184 | path[i].function != info->path[i].function) |
| 185 | return false; |
| 186 | } |
| 187 | |
| 188 | return true; |
| 189 | } |
| 190 | |
| 191 | /* Return: > 0 if match found, 0 if no match found, < 0 if error happens */ |
| 192 | int dmar_insert_dev_scope(struct dmar_pci_notify_info *info, |
| 193 | void *start, void*end, u16 segment, |
David Woodhouse | 832bd85 | 2014-03-07 15:08:36 +0000 | [diff] [blame^] | 194 | struct dmar_dev_scope *devices, |
| 195 | int devices_cnt) |
Jiang Liu | 59ce051 | 2014-02-19 14:07:35 +0800 | [diff] [blame] | 196 | { |
| 197 | int i, level; |
David Woodhouse | 832bd85 | 2014-03-07 15:08:36 +0000 | [diff] [blame^] | 198 | struct device *tmp, *dev = &info->dev->dev; |
Jiang Liu | 59ce051 | 2014-02-19 14:07:35 +0800 | [diff] [blame] | 199 | struct acpi_dmar_device_scope *scope; |
| 200 | struct acpi_dmar_pci_path *path; |
| 201 | |
| 202 | if (segment != info->seg) |
| 203 | return 0; |
| 204 | |
| 205 | for (; start < end; start += scope->length) { |
| 206 | scope = start; |
| 207 | if (scope->entry_type != ACPI_DMAR_SCOPE_TYPE_ENDPOINT && |
| 208 | scope->entry_type != ACPI_DMAR_SCOPE_TYPE_BRIDGE) |
| 209 | continue; |
| 210 | |
| 211 | path = (struct acpi_dmar_pci_path *)(scope + 1); |
| 212 | level = (scope->length - sizeof(*scope)) / sizeof(*path); |
| 213 | if (!dmar_match_pci_path(info, scope->bus, path, level)) |
| 214 | continue; |
| 215 | |
| 216 | if ((scope->entry_type == ACPI_DMAR_SCOPE_TYPE_ENDPOINT) ^ |
David Woodhouse | 832bd85 | 2014-03-07 15:08:36 +0000 | [diff] [blame^] | 217 | (info->dev->hdr_type == PCI_HEADER_TYPE_NORMAL)) { |
Jiang Liu | 59ce051 | 2014-02-19 14:07:35 +0800 | [diff] [blame] | 218 | pr_warn("Device scope type does not match for %s\n", |
David Woodhouse | 832bd85 | 2014-03-07 15:08:36 +0000 | [diff] [blame^] | 219 | pci_name(info->dev)); |
Jiang Liu | 59ce051 | 2014-02-19 14:07:35 +0800 | [diff] [blame] | 220 | return -EINVAL; |
| 221 | } |
| 222 | |
| 223 | for_each_dev_scope(devices, devices_cnt, i, tmp) |
| 224 | if (tmp == NULL) { |
David Woodhouse | 832bd85 | 2014-03-07 15:08:36 +0000 | [diff] [blame^] | 225 | devices[i].bus = info->dev->bus->number; |
| 226 | devices[i].devfn = info->dev->devfn; |
| 227 | rcu_assign_pointer(devices[i].dev, |
| 228 | get_device(dev)); |
Jiang Liu | 59ce051 | 2014-02-19 14:07:35 +0800 | [diff] [blame] | 229 | return 1; |
| 230 | } |
| 231 | BUG_ON(i >= devices_cnt); |
| 232 | } |
| 233 | |
| 234 | return 0; |
| 235 | } |
| 236 | |
| 237 | 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^] | 238 | struct dmar_dev_scope *devices, int count) |
Jiang Liu | 59ce051 | 2014-02-19 14:07:35 +0800 | [diff] [blame] | 239 | { |
| 240 | int index; |
David Woodhouse | 832bd85 | 2014-03-07 15:08:36 +0000 | [diff] [blame^] | 241 | struct device *tmp; |
Jiang Liu | 59ce051 | 2014-02-19 14:07:35 +0800 | [diff] [blame] | 242 | |
| 243 | if (info->seg != segment) |
| 244 | return 0; |
| 245 | |
| 246 | for_each_active_dev_scope(devices, count, index, tmp) |
David Woodhouse | 832bd85 | 2014-03-07 15:08:36 +0000 | [diff] [blame^] | 247 | if (tmp == &info->dev->dev) { |
| 248 | rcu_assign_pointer(devices[index].dev, NULL); |
Jiang Liu | 59ce051 | 2014-02-19 14:07:35 +0800 | [diff] [blame] | 249 | synchronize_rcu(); |
David Woodhouse | 832bd85 | 2014-03-07 15:08:36 +0000 | [diff] [blame^] | 250 | put_device(tmp); |
Jiang Liu | 59ce051 | 2014-02-19 14:07:35 +0800 | [diff] [blame] | 251 | return 1; |
| 252 | } |
| 253 | |
| 254 | return 0; |
| 255 | } |
| 256 | |
| 257 | static int dmar_pci_bus_add_dev(struct dmar_pci_notify_info *info) |
| 258 | { |
| 259 | int ret = 0; |
| 260 | struct dmar_drhd_unit *dmaru; |
| 261 | struct acpi_dmar_hardware_unit *drhd; |
| 262 | |
| 263 | for_each_drhd_unit(dmaru) { |
| 264 | if (dmaru->include_all) |
| 265 | continue; |
| 266 | |
| 267 | drhd = container_of(dmaru->hdr, |
| 268 | struct acpi_dmar_hardware_unit, header); |
| 269 | ret = dmar_insert_dev_scope(info, (void *)(drhd + 1), |
| 270 | ((void *)drhd) + drhd->header.length, |
| 271 | dmaru->segment, |
| 272 | dmaru->devices, dmaru->devices_cnt); |
| 273 | if (ret != 0) |
| 274 | break; |
| 275 | } |
| 276 | if (ret >= 0) |
| 277 | ret = dmar_iommu_notify_scope_dev(info); |
Jiang Liu | 2e45528 | 2014-02-19 14:07:36 +0800 | [diff] [blame] | 278 | if (ret < 0 && dmar_dev_scope_status == 0) |
| 279 | dmar_dev_scope_status = ret; |
Jiang Liu | 59ce051 | 2014-02-19 14:07:35 +0800 | [diff] [blame] | 280 | |
| 281 | return ret; |
| 282 | } |
| 283 | |
| 284 | static void dmar_pci_bus_del_dev(struct dmar_pci_notify_info *info) |
| 285 | { |
| 286 | struct dmar_drhd_unit *dmaru; |
| 287 | |
| 288 | for_each_drhd_unit(dmaru) |
| 289 | if (dmar_remove_dev_scope(info, dmaru->segment, |
| 290 | dmaru->devices, dmaru->devices_cnt)) |
| 291 | break; |
| 292 | dmar_iommu_notify_scope_dev(info); |
| 293 | } |
| 294 | |
| 295 | static int dmar_pci_bus_notifier(struct notifier_block *nb, |
| 296 | unsigned long action, void *data) |
| 297 | { |
| 298 | struct pci_dev *pdev = to_pci_dev(data); |
| 299 | struct dmar_pci_notify_info *info; |
| 300 | |
| 301 | /* Only care about add/remove events for physical functions */ |
| 302 | if (pdev->is_virtfn) |
| 303 | return NOTIFY_DONE; |
| 304 | if (action != BUS_NOTIFY_ADD_DEVICE && action != BUS_NOTIFY_DEL_DEVICE) |
| 305 | return NOTIFY_DONE; |
| 306 | |
| 307 | info = dmar_alloc_pci_notify_info(pdev, action); |
| 308 | if (!info) |
| 309 | return NOTIFY_DONE; |
| 310 | |
| 311 | down_write(&dmar_global_lock); |
| 312 | if (action == BUS_NOTIFY_ADD_DEVICE) |
| 313 | dmar_pci_bus_add_dev(info); |
| 314 | else if (action == BUS_NOTIFY_DEL_DEVICE) |
| 315 | dmar_pci_bus_del_dev(info); |
| 316 | up_write(&dmar_global_lock); |
| 317 | |
| 318 | dmar_free_pci_notify_info(info); |
| 319 | |
| 320 | return NOTIFY_OK; |
| 321 | } |
| 322 | |
| 323 | static struct notifier_block dmar_pci_bus_nb = { |
| 324 | .notifier_call = dmar_pci_bus_notifier, |
| 325 | .priority = INT_MIN, |
| 326 | }; |
| 327 | |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 328 | /** |
| 329 | * dmar_parse_one_drhd - parses exactly one DMA remapping hardware definition |
| 330 | * structure which uniquely represent one DMA remapping hardware unit |
| 331 | * present in the platform |
| 332 | */ |
| 333 | static int __init |
| 334 | dmar_parse_one_drhd(struct acpi_dmar_header *header) |
| 335 | { |
| 336 | struct acpi_dmar_hardware_unit *drhd; |
| 337 | struct dmar_drhd_unit *dmaru; |
| 338 | int ret = 0; |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 339 | |
David Woodhouse | e523b38 | 2009-04-10 22:27:48 -0700 | [diff] [blame] | 340 | drhd = (struct acpi_dmar_hardware_unit *)header; |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 341 | dmaru = kzalloc(sizeof(*dmaru), GFP_KERNEL); |
| 342 | if (!dmaru) |
| 343 | return -ENOMEM; |
| 344 | |
Suresh Siddha | 1886e8a | 2008-07-10 11:16:37 -0700 | [diff] [blame] | 345 | dmaru->hdr = header; |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 346 | dmaru->reg_base_addr = drhd->address; |
David Woodhouse | 276dbf9 | 2009-04-04 01:45:37 +0100 | [diff] [blame] | 347 | dmaru->segment = drhd->segment; |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 348 | dmaru->include_all = drhd->flags & 0x1; /* BIT0: INCLUDE_ALL */ |
David Woodhouse | 07cb52f | 2014-03-07 14:39:27 +0000 | [diff] [blame] | 349 | dmaru->devices = dmar_alloc_dev_scope((void *)(drhd + 1), |
| 350 | ((void *)drhd) + drhd->header.length, |
| 351 | &dmaru->devices_cnt); |
| 352 | if (dmaru->devices_cnt && dmaru->devices == NULL) { |
| 353 | kfree(dmaru); |
| 354 | return -ENOMEM; |
Jiang Liu | 2e45528 | 2014-02-19 14:07:36 +0800 | [diff] [blame] | 355 | } |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 356 | |
Suresh Siddha | 1886e8a | 2008-07-10 11:16:37 -0700 | [diff] [blame] | 357 | ret = alloc_iommu(dmaru); |
| 358 | if (ret) { |
David Woodhouse | 07cb52f | 2014-03-07 14:39:27 +0000 | [diff] [blame] | 359 | dmar_free_dev_scope(&dmaru->devices, |
| 360 | &dmaru->devices_cnt); |
Suresh Siddha | 1886e8a | 2008-07-10 11:16:37 -0700 | [diff] [blame] | 361 | kfree(dmaru); |
| 362 | return ret; |
| 363 | } |
| 364 | dmar_register_drhd_unit(dmaru); |
| 365 | return 0; |
| 366 | } |
| 367 | |
Jiang Liu | a868e6b | 2014-01-06 14:18:20 +0800 | [diff] [blame] | 368 | static void dmar_free_drhd(struct dmar_drhd_unit *dmaru) |
| 369 | { |
| 370 | if (dmaru->devices && dmaru->devices_cnt) |
| 371 | dmar_free_dev_scope(&dmaru->devices, &dmaru->devices_cnt); |
| 372 | if (dmaru->iommu) |
| 373 | free_iommu(dmaru->iommu); |
| 374 | kfree(dmaru); |
| 375 | } |
| 376 | |
David Woodhouse | e625b4a | 2014-03-07 14:34:38 +0000 | [diff] [blame] | 377 | static int __init dmar_parse_one_andd(struct acpi_dmar_header *header) |
| 378 | { |
| 379 | struct acpi_dmar_andd *andd = (void *)header; |
| 380 | |
| 381 | /* Check for NUL termination within the designated length */ |
| 382 | if (strnlen(andd->object_name, header->length - 8) == header->length - 8) { |
| 383 | WARN_TAINT(1, TAINT_FIRMWARE_WORKAROUND, |
| 384 | "Your BIOS is broken; ANDD object name is not NUL-terminated\n" |
| 385 | "BIOS vendor: %s; Ver: %s; Product Version: %s\n", |
| 386 | dmi_get_system_info(DMI_BIOS_VENDOR), |
| 387 | dmi_get_system_info(DMI_BIOS_VERSION), |
| 388 | dmi_get_system_info(DMI_PRODUCT_VERSION)); |
| 389 | return -EINVAL; |
| 390 | } |
| 391 | pr_info("ANDD device: %x name: %s\n", andd->device_number, |
| 392 | andd->object_name); |
| 393 | |
| 394 | return 0; |
| 395 | } |
| 396 | |
David Woodhouse | aa69707 | 2009-10-07 12:18:00 +0100 | [diff] [blame] | 397 | #ifdef CONFIG_ACPI_NUMA |
Suresh Siddha | ee34b32 | 2009-10-02 11:01:21 -0700 | [diff] [blame] | 398 | static int __init |
| 399 | dmar_parse_one_rhsa(struct acpi_dmar_header *header) |
| 400 | { |
| 401 | struct acpi_dmar_rhsa *rhsa; |
| 402 | struct dmar_drhd_unit *drhd; |
| 403 | |
| 404 | rhsa = (struct acpi_dmar_rhsa *)header; |
David Woodhouse | aa69707 | 2009-10-07 12:18:00 +0100 | [diff] [blame] | 405 | for_each_drhd_unit(drhd) { |
Suresh Siddha | ee34b32 | 2009-10-02 11:01:21 -0700 | [diff] [blame] | 406 | if (drhd->reg_base_addr == rhsa->base_address) { |
| 407 | int node = acpi_map_pxm_to_node(rhsa->proximity_domain); |
| 408 | |
| 409 | if (!node_online(node)) |
| 410 | node = -1; |
| 411 | drhd->iommu->node = node; |
David Woodhouse | aa69707 | 2009-10-07 12:18:00 +0100 | [diff] [blame] | 412 | return 0; |
| 413 | } |
Suresh Siddha | ee34b32 | 2009-10-02 11:01:21 -0700 | [diff] [blame] | 414 | } |
Ben Hutchings | fd0c889 | 2010-04-03 19:38:43 +0100 | [diff] [blame] | 415 | WARN_TAINT( |
| 416 | 1, TAINT_FIRMWARE_WORKAROUND, |
| 417 | "Your BIOS is broken; RHSA refers to non-existent DMAR unit at %llx\n" |
| 418 | "BIOS vendor: %s; Ver: %s; Product Version: %s\n", |
| 419 | drhd->reg_base_addr, |
| 420 | dmi_get_system_info(DMI_BIOS_VENDOR), |
| 421 | dmi_get_system_info(DMI_BIOS_VERSION), |
| 422 | dmi_get_system_info(DMI_PRODUCT_VERSION)); |
Suresh Siddha | ee34b32 | 2009-10-02 11:01:21 -0700 | [diff] [blame] | 423 | |
David Woodhouse | aa69707 | 2009-10-07 12:18:00 +0100 | [diff] [blame] | 424 | return 0; |
Suresh Siddha | ee34b32 | 2009-10-02 11:01:21 -0700 | [diff] [blame] | 425 | } |
David Woodhouse | aa69707 | 2009-10-07 12:18:00 +0100 | [diff] [blame] | 426 | #endif |
Suresh Siddha | ee34b32 | 2009-10-02 11:01:21 -0700 | [diff] [blame] | 427 | |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 428 | static void __init |
| 429 | dmar_table_print_dmar_entry(struct acpi_dmar_header *header) |
| 430 | { |
| 431 | struct acpi_dmar_hardware_unit *drhd; |
| 432 | struct acpi_dmar_reserved_memory *rmrr; |
Yu Zhao | aa5d2b5 | 2009-05-18 13:51:34 +0800 | [diff] [blame] | 433 | struct acpi_dmar_atsr *atsr; |
Roland Dreier | 17b6097 | 2009-09-24 12:14:00 -0700 | [diff] [blame] | 434 | struct acpi_dmar_rhsa *rhsa; |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 435 | |
| 436 | switch (header->type) { |
| 437 | case ACPI_DMAR_TYPE_HARDWARE_UNIT: |
Yu Zhao | aa5d2b5 | 2009-05-18 13:51:34 +0800 | [diff] [blame] | 438 | drhd = container_of(header, struct acpi_dmar_hardware_unit, |
| 439 | header); |
Donald Dutile | e9071b0 | 2012-06-08 17:13:11 -0400 | [diff] [blame] | 440 | pr_info("DRHD base: %#016Lx flags: %#x\n", |
Yu Zhao | aa5d2b5 | 2009-05-18 13:51:34 +0800 | [diff] [blame] | 441 | (unsigned long long)drhd->address, drhd->flags); |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 442 | break; |
| 443 | case ACPI_DMAR_TYPE_RESERVED_MEMORY: |
Yu Zhao | aa5d2b5 | 2009-05-18 13:51:34 +0800 | [diff] [blame] | 444 | rmrr = container_of(header, struct acpi_dmar_reserved_memory, |
| 445 | header); |
Donald Dutile | e9071b0 | 2012-06-08 17:13:11 -0400 | [diff] [blame] | 446 | pr_info("RMRR base: %#016Lx end: %#016Lx\n", |
Fenghua Yu | 5b6985c | 2008-10-16 18:02:32 -0700 | [diff] [blame] | 447 | (unsigned long long)rmrr->base_address, |
| 448 | (unsigned long long)rmrr->end_address); |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 449 | break; |
Yu Zhao | aa5d2b5 | 2009-05-18 13:51:34 +0800 | [diff] [blame] | 450 | case ACPI_DMAR_TYPE_ATSR: |
| 451 | atsr = container_of(header, struct acpi_dmar_atsr, header); |
Donald Dutile | e9071b0 | 2012-06-08 17:13:11 -0400 | [diff] [blame] | 452 | pr_info("ATSR flags: %#x\n", atsr->flags); |
Yu Zhao | aa5d2b5 | 2009-05-18 13:51:34 +0800 | [diff] [blame] | 453 | break; |
Roland Dreier | 17b6097 | 2009-09-24 12:14:00 -0700 | [diff] [blame] | 454 | case ACPI_DMAR_HARDWARE_AFFINITY: |
| 455 | rhsa = container_of(header, struct acpi_dmar_rhsa, header); |
Donald Dutile | e9071b0 | 2012-06-08 17:13:11 -0400 | [diff] [blame] | 456 | pr_info("RHSA base: %#016Lx proximity domain: %#x\n", |
Roland Dreier | 17b6097 | 2009-09-24 12:14:00 -0700 | [diff] [blame] | 457 | (unsigned long long)rhsa->base_address, |
| 458 | rhsa->proximity_domain); |
| 459 | break; |
David Woodhouse | e625b4a | 2014-03-07 14:34:38 +0000 | [diff] [blame] | 460 | case ACPI_DMAR_TYPE_ANDD: |
| 461 | /* We don't print this here because we need to sanity-check |
| 462 | it first. So print it in dmar_parse_one_andd() instead. */ |
| 463 | break; |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 464 | } |
| 465 | } |
| 466 | |
Yinghai Lu | f6dd5c3 | 2008-09-03 16:58:32 -0700 | [diff] [blame] | 467 | /** |
| 468 | * dmar_table_detect - checks to see if the platform supports DMAR devices |
| 469 | */ |
| 470 | static int __init dmar_table_detect(void) |
| 471 | { |
| 472 | acpi_status status = AE_OK; |
| 473 | |
| 474 | /* if we could find DMAR table, then there are DMAR devices */ |
Yinghai Lu | 8e1568f | 2009-02-11 01:06:59 -0800 | [diff] [blame] | 475 | status = acpi_get_table_with_size(ACPI_SIG_DMAR, 0, |
| 476 | (struct acpi_table_header **)&dmar_tbl, |
| 477 | &dmar_tbl_size); |
Yinghai Lu | f6dd5c3 | 2008-09-03 16:58:32 -0700 | [diff] [blame] | 478 | |
| 479 | if (ACPI_SUCCESS(status) && !dmar_tbl) { |
Donald Dutile | e9071b0 | 2012-06-08 17:13:11 -0400 | [diff] [blame] | 480 | pr_warn("Unable to map DMAR\n"); |
Yinghai Lu | f6dd5c3 | 2008-09-03 16:58:32 -0700 | [diff] [blame] | 481 | status = AE_NOT_FOUND; |
| 482 | } |
| 483 | |
| 484 | return (ACPI_SUCCESS(status) ? 1 : 0); |
| 485 | } |
Suresh Siddha | aaa9d1d | 2008-07-10 11:16:38 -0700 | [diff] [blame] | 486 | |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 487 | /** |
| 488 | * parse_dmar_table - parses the DMA reporting table |
| 489 | */ |
| 490 | static int __init |
| 491 | parse_dmar_table(void) |
| 492 | { |
| 493 | struct acpi_table_dmar *dmar; |
| 494 | struct acpi_dmar_header *entry_header; |
| 495 | int ret = 0; |
Li, Zhen-Hua | 7cef334 | 2013-05-20 15:57:32 +0800 | [diff] [blame] | 496 | int drhd_count = 0; |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 497 | |
Yinghai Lu | f6dd5c3 | 2008-09-03 16:58:32 -0700 | [diff] [blame] | 498 | /* |
| 499 | * Do it again, earlier dmar_tbl mapping could be mapped with |
| 500 | * fixed map. |
| 501 | */ |
| 502 | dmar_table_detect(); |
| 503 | |
Joseph Cihula | a59b50e | 2009-06-30 19:31:10 -0700 | [diff] [blame] | 504 | /* |
| 505 | * ACPI tables may not be DMA protected by tboot, so use DMAR copy |
| 506 | * SINIT saved in SinitMleData in TXT heap (which is DMA protected) |
| 507 | */ |
| 508 | dmar_tbl = tboot_get_dmar_table(dmar_tbl); |
| 509 | |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 510 | dmar = (struct acpi_table_dmar *)dmar_tbl; |
| 511 | if (!dmar) |
| 512 | return -ENODEV; |
| 513 | |
Fenghua Yu | 5b6985c | 2008-10-16 18:02:32 -0700 | [diff] [blame] | 514 | if (dmar->width < PAGE_SHIFT - 1) { |
Donald Dutile | e9071b0 | 2012-06-08 17:13:11 -0400 | [diff] [blame] | 515 | pr_warn("Invalid DMAR haw\n"); |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 516 | return -EINVAL; |
| 517 | } |
| 518 | |
Donald Dutile | e9071b0 | 2012-06-08 17:13:11 -0400 | [diff] [blame] | 519 | pr_info("Host address width %d\n", dmar->width + 1); |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 520 | |
| 521 | entry_header = (struct acpi_dmar_header *)(dmar + 1); |
| 522 | while (((unsigned long)entry_header) < |
| 523 | (((unsigned long)dmar) + dmar_tbl->length)) { |
Tony Battersby | 084eb96 | 2009-02-11 13:24:19 -0800 | [diff] [blame] | 524 | /* Avoid looping forever on bad ACPI tables */ |
| 525 | if (entry_header->length == 0) { |
Donald Dutile | e9071b0 | 2012-06-08 17:13:11 -0400 | [diff] [blame] | 526 | pr_warn("Invalid 0-length structure\n"); |
Tony Battersby | 084eb96 | 2009-02-11 13:24:19 -0800 | [diff] [blame] | 527 | ret = -EINVAL; |
| 528 | break; |
| 529 | } |
| 530 | |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 531 | dmar_table_print_dmar_entry(entry_header); |
| 532 | |
| 533 | switch (entry_header->type) { |
| 534 | case ACPI_DMAR_TYPE_HARDWARE_UNIT: |
Li, Zhen-Hua | 7cef334 | 2013-05-20 15:57:32 +0800 | [diff] [blame] | 535 | drhd_count++; |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 536 | ret = dmar_parse_one_drhd(entry_header); |
| 537 | break; |
| 538 | case ACPI_DMAR_TYPE_RESERVED_MEMORY: |
| 539 | ret = dmar_parse_one_rmrr(entry_header); |
| 540 | break; |
Yu Zhao | aa5d2b5 | 2009-05-18 13:51:34 +0800 | [diff] [blame] | 541 | case ACPI_DMAR_TYPE_ATSR: |
Yu Zhao | aa5d2b5 | 2009-05-18 13:51:34 +0800 | [diff] [blame] | 542 | ret = dmar_parse_one_atsr(entry_header); |
Yu Zhao | aa5d2b5 | 2009-05-18 13:51:34 +0800 | [diff] [blame] | 543 | break; |
Roland Dreier | 17b6097 | 2009-09-24 12:14:00 -0700 | [diff] [blame] | 544 | case ACPI_DMAR_HARDWARE_AFFINITY: |
David Woodhouse | aa69707 | 2009-10-07 12:18:00 +0100 | [diff] [blame] | 545 | #ifdef CONFIG_ACPI_NUMA |
Suresh Siddha | ee34b32 | 2009-10-02 11:01:21 -0700 | [diff] [blame] | 546 | ret = dmar_parse_one_rhsa(entry_header); |
David Woodhouse | aa69707 | 2009-10-07 12:18:00 +0100 | [diff] [blame] | 547 | #endif |
Roland Dreier | 17b6097 | 2009-09-24 12:14:00 -0700 | [diff] [blame] | 548 | break; |
David Woodhouse | e625b4a | 2014-03-07 14:34:38 +0000 | [diff] [blame] | 549 | case ACPI_DMAR_TYPE_ANDD: |
| 550 | ret = dmar_parse_one_andd(entry_header); |
| 551 | break; |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 552 | default: |
Donald Dutile | e9071b0 | 2012-06-08 17:13:11 -0400 | [diff] [blame] | 553 | pr_warn("Unknown DMAR structure type %d\n", |
Roland Dreier | 4de75cf | 2009-09-24 01:01:29 +0100 | [diff] [blame] | 554 | entry_header->type); |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 555 | ret = 0; /* for forward compatibility */ |
| 556 | break; |
| 557 | } |
| 558 | if (ret) |
| 559 | break; |
| 560 | |
| 561 | entry_header = ((void *)entry_header + entry_header->length); |
| 562 | } |
Li, Zhen-Hua | 7cef334 | 2013-05-20 15:57:32 +0800 | [diff] [blame] | 563 | if (drhd_count == 0) |
| 564 | 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] | 565 | return ret; |
| 566 | } |
| 567 | |
David Woodhouse | 832bd85 | 2014-03-07 15:08:36 +0000 | [diff] [blame^] | 568 | static int dmar_pci_device_match(struct dmar_dev_scope devices[], |
| 569 | int cnt, struct pci_dev *dev) |
Suresh Siddha | e61d98d | 2008-07-10 11:16:35 -0700 | [diff] [blame] | 570 | { |
| 571 | int index; |
David Woodhouse | 832bd85 | 2014-03-07 15:08:36 +0000 | [diff] [blame^] | 572 | struct device *tmp; |
Suresh Siddha | e61d98d | 2008-07-10 11:16:35 -0700 | [diff] [blame] | 573 | |
| 574 | while (dev) { |
Jiang Liu | b683b23 | 2014-02-19 14:07:32 +0800 | [diff] [blame] | 575 | for_each_active_dev_scope(devices, cnt, index, tmp) |
David Woodhouse | 832bd85 | 2014-03-07 15:08:36 +0000 | [diff] [blame^] | 576 | if (dev_is_pci(tmp) && dev == to_pci_dev(tmp)) |
Suresh Siddha | e61d98d | 2008-07-10 11:16:35 -0700 | [diff] [blame] | 577 | return 1; |
| 578 | |
| 579 | /* Check our parent */ |
| 580 | dev = dev->bus->self; |
| 581 | } |
| 582 | |
| 583 | return 0; |
| 584 | } |
| 585 | |
| 586 | struct dmar_drhd_unit * |
| 587 | dmar_find_matched_drhd_unit(struct pci_dev *dev) |
| 588 | { |
Jiang Liu | 0e24261 | 2014-02-19 14:07:34 +0800 | [diff] [blame] | 589 | struct dmar_drhd_unit *dmaru; |
Yu Zhao | 2e824f7 | 2008-12-22 16:54:58 +0800 | [diff] [blame] | 590 | struct acpi_dmar_hardware_unit *drhd; |
Suresh Siddha | e61d98d | 2008-07-10 11:16:35 -0700 | [diff] [blame] | 591 | |
Yinghai | dda5654 | 2010-04-09 01:07:55 +0100 | [diff] [blame] | 592 | dev = pci_physfn(dev); |
| 593 | |
Jiang Liu | 0e24261 | 2014-02-19 14:07:34 +0800 | [diff] [blame] | 594 | rcu_read_lock(); |
Yijing Wang | 8b161f0 | 2013-10-31 17:25:16 +0800 | [diff] [blame] | 595 | for_each_drhd_unit(dmaru) { |
Yu Zhao | 2e824f7 | 2008-12-22 16:54:58 +0800 | [diff] [blame] | 596 | drhd = container_of(dmaru->hdr, |
| 597 | struct acpi_dmar_hardware_unit, |
| 598 | header); |
| 599 | |
| 600 | if (dmaru->include_all && |
| 601 | drhd->segment == pci_domain_nr(dev->bus)) |
Jiang Liu | 0e24261 | 2014-02-19 14:07:34 +0800 | [diff] [blame] | 602 | goto out; |
Yu Zhao | 2e824f7 | 2008-12-22 16:54:58 +0800 | [diff] [blame] | 603 | |
| 604 | if (dmar_pci_device_match(dmaru->devices, |
| 605 | dmaru->devices_cnt, dev)) |
Jiang Liu | 0e24261 | 2014-02-19 14:07:34 +0800 | [diff] [blame] | 606 | goto out; |
Suresh Siddha | e61d98d | 2008-07-10 11:16:35 -0700 | [diff] [blame] | 607 | } |
Jiang Liu | 0e24261 | 2014-02-19 14:07:34 +0800 | [diff] [blame] | 608 | dmaru = NULL; |
| 609 | out: |
| 610 | rcu_read_unlock(); |
Suresh Siddha | e61d98d | 2008-07-10 11:16:35 -0700 | [diff] [blame] | 611 | |
Jiang Liu | 0e24261 | 2014-02-19 14:07:34 +0800 | [diff] [blame] | 612 | return dmaru; |
Suresh Siddha | e61d98d | 2008-07-10 11:16:35 -0700 | [diff] [blame] | 613 | } |
| 614 | |
Suresh Siddha | 1886e8a | 2008-07-10 11:16:37 -0700 | [diff] [blame] | 615 | int __init dmar_dev_scope_init(void) |
| 616 | { |
Jiang Liu | 2e45528 | 2014-02-19 14:07:36 +0800 | [diff] [blame] | 617 | struct pci_dev *dev = NULL; |
| 618 | struct dmar_pci_notify_info *info; |
Suresh Siddha | 1886e8a | 2008-07-10 11:16:37 -0700 | [diff] [blame] | 619 | |
Jiang Liu | 2e45528 | 2014-02-19 14:07:36 +0800 | [diff] [blame] | 620 | if (dmar_dev_scope_status != 1) |
| 621 | return dmar_dev_scope_status; |
Suresh Siddha | c2c7286 | 2011-08-23 17:05:19 -0700 | [diff] [blame] | 622 | |
Jiang Liu | 2e45528 | 2014-02-19 14:07:36 +0800 | [diff] [blame] | 623 | if (list_empty(&dmar_drhd_units)) { |
| 624 | dmar_dev_scope_status = -ENODEV; |
| 625 | } else { |
| 626 | dmar_dev_scope_status = 0; |
Suresh Siddha | 318fe7d | 2011-08-23 17:05:20 -0700 | [diff] [blame] | 627 | |
Jiang Liu | 2e45528 | 2014-02-19 14:07:36 +0800 | [diff] [blame] | 628 | for_each_pci_dev(dev) { |
| 629 | if (dev->is_virtfn) |
| 630 | continue; |
| 631 | |
| 632 | info = dmar_alloc_pci_notify_info(dev, |
| 633 | BUS_NOTIFY_ADD_DEVICE); |
| 634 | if (!info) { |
| 635 | return dmar_dev_scope_status; |
| 636 | } else { |
| 637 | dmar_pci_bus_add_dev(info); |
| 638 | dmar_free_pci_notify_info(info); |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | bus_register_notifier(&pci_bus_type, &dmar_pci_bus_nb); |
Suresh Siddha | 1886e8a | 2008-07-10 11:16:37 -0700 | [diff] [blame] | 643 | } |
| 644 | |
Jiang Liu | 2e45528 | 2014-02-19 14:07:36 +0800 | [diff] [blame] | 645 | return dmar_dev_scope_status; |
Suresh Siddha | 1886e8a | 2008-07-10 11:16:37 -0700 | [diff] [blame] | 646 | } |
| 647 | |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 648 | |
| 649 | int __init dmar_table_init(void) |
| 650 | { |
Suresh Siddha | 1886e8a | 2008-07-10 11:16:37 -0700 | [diff] [blame] | 651 | static int dmar_table_initialized; |
Fenghua Yu | 093f87d | 2007-11-21 15:07:14 -0800 | [diff] [blame] | 652 | int ret; |
| 653 | |
Jiang Liu | cc05301 | 2014-01-06 14:18:24 +0800 | [diff] [blame] | 654 | if (dmar_table_initialized == 0) { |
| 655 | ret = parse_dmar_table(); |
| 656 | if (ret < 0) { |
| 657 | if (ret != -ENODEV) |
| 658 | pr_info("parse DMAR table failure.\n"); |
| 659 | } else if (list_empty(&dmar_drhd_units)) { |
| 660 | pr_info("No DMAR devices found\n"); |
| 661 | ret = -ENODEV; |
| 662 | } |
Suresh Siddha | 1886e8a | 2008-07-10 11:16:37 -0700 | [diff] [blame] | 663 | |
Jiang Liu | cc05301 | 2014-01-06 14:18:24 +0800 | [diff] [blame] | 664 | if (ret < 0) |
| 665 | dmar_table_initialized = ret; |
| 666 | else |
| 667 | dmar_table_initialized = 1; |
Fenghua Yu | 093f87d | 2007-11-21 15:07:14 -0800 | [diff] [blame] | 668 | } |
| 669 | |
Jiang Liu | cc05301 | 2014-01-06 14:18:24 +0800 | [diff] [blame] | 670 | return dmar_table_initialized < 0 ? dmar_table_initialized : 0; |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 671 | } |
| 672 | |
Ben Hutchings | 3a8663e | 2010-04-03 19:37:23 +0100 | [diff] [blame] | 673 | static void warn_invalid_dmar(u64 addr, const char *message) |
| 674 | { |
Ben Hutchings | fd0c889 | 2010-04-03 19:38:43 +0100 | [diff] [blame] | 675 | WARN_TAINT_ONCE( |
| 676 | 1, TAINT_FIRMWARE_WORKAROUND, |
| 677 | "Your BIOS is broken; DMAR reported at address %llx%s!\n" |
| 678 | "BIOS vendor: %s; Ver: %s; Product Version: %s\n", |
| 679 | addr, message, |
| 680 | dmi_get_system_info(DMI_BIOS_VENDOR), |
| 681 | dmi_get_system_info(DMI_BIOS_VERSION), |
| 682 | dmi_get_system_info(DMI_PRODUCT_VERSION)); |
Ben Hutchings | 3a8663e | 2010-04-03 19:37:23 +0100 | [diff] [blame] | 683 | } |
David Woodhouse | 6ecbf01 | 2009-12-02 09:20:27 +0000 | [diff] [blame] | 684 | |
Rashika Kheria | 21004dc | 2013-12-18 12:01:46 +0530 | [diff] [blame] | 685 | static int __init check_zero_address(void) |
David Woodhouse | 86cf898 | 2009-11-09 22:15:15 +0000 | [diff] [blame] | 686 | { |
| 687 | struct acpi_table_dmar *dmar; |
| 688 | struct acpi_dmar_header *entry_header; |
| 689 | struct acpi_dmar_hardware_unit *drhd; |
| 690 | |
| 691 | dmar = (struct acpi_table_dmar *)dmar_tbl; |
| 692 | entry_header = (struct acpi_dmar_header *)(dmar + 1); |
| 693 | |
| 694 | while (((unsigned long)entry_header) < |
| 695 | (((unsigned long)dmar) + dmar_tbl->length)) { |
| 696 | /* Avoid looping forever on bad ACPI tables */ |
| 697 | if (entry_header->length == 0) { |
Donald Dutile | e9071b0 | 2012-06-08 17:13:11 -0400 | [diff] [blame] | 698 | pr_warn("Invalid 0-length structure\n"); |
David Woodhouse | 86cf898 | 2009-11-09 22:15:15 +0000 | [diff] [blame] | 699 | return 0; |
| 700 | } |
| 701 | |
| 702 | if (entry_header->type == ACPI_DMAR_TYPE_HARDWARE_UNIT) { |
Chris Wright | 2c99220 | 2009-12-02 09:17:13 +0000 | [diff] [blame] | 703 | void __iomem *addr; |
| 704 | u64 cap, ecap; |
| 705 | |
David Woodhouse | 86cf898 | 2009-11-09 22:15:15 +0000 | [diff] [blame] | 706 | drhd = (void *)entry_header; |
| 707 | if (!drhd->address) { |
Ben Hutchings | 3a8663e | 2010-04-03 19:37:23 +0100 | [diff] [blame] | 708 | warn_invalid_dmar(0, ""); |
Chris Wright | 2c99220 | 2009-12-02 09:17:13 +0000 | [diff] [blame] | 709 | goto failed; |
David Woodhouse | 86cf898 | 2009-11-09 22:15:15 +0000 | [diff] [blame] | 710 | } |
Chris Wright | 2c99220 | 2009-12-02 09:17:13 +0000 | [diff] [blame] | 711 | |
| 712 | addr = early_ioremap(drhd->address, VTD_PAGE_SIZE); |
| 713 | if (!addr ) { |
| 714 | printk("IOMMU: can't validate: %llx\n", drhd->address); |
| 715 | goto failed; |
| 716 | } |
| 717 | cap = dmar_readq(addr + DMAR_CAP_REG); |
| 718 | ecap = dmar_readq(addr + DMAR_ECAP_REG); |
| 719 | early_iounmap(addr, VTD_PAGE_SIZE); |
| 720 | if (cap == (uint64_t)-1 && ecap == (uint64_t)-1) { |
Ben Hutchings | 3a8663e | 2010-04-03 19:37:23 +0100 | [diff] [blame] | 721 | warn_invalid_dmar(drhd->address, |
| 722 | " returns all ones"); |
Chris Wright | 2c99220 | 2009-12-02 09:17:13 +0000 | [diff] [blame] | 723 | goto failed; |
| 724 | } |
David Woodhouse | 86cf898 | 2009-11-09 22:15:15 +0000 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | entry_header = ((void *)entry_header + entry_header->length); |
| 728 | } |
| 729 | return 1; |
Chris Wright | 2c99220 | 2009-12-02 09:17:13 +0000 | [diff] [blame] | 730 | |
| 731 | failed: |
Chris Wright | 2c99220 | 2009-12-02 09:17:13 +0000 | [diff] [blame] | 732 | return 0; |
David Woodhouse | 86cf898 | 2009-11-09 22:15:15 +0000 | [diff] [blame] | 733 | } |
| 734 | |
Konrad Rzeszutek Wilk | 480125b | 2010-08-26 13:57:57 -0400 | [diff] [blame] | 735 | int __init detect_intel_iommu(void) |
Suresh Siddha | 2ae2101 | 2008-07-10 11:16:43 -0700 | [diff] [blame] | 736 | { |
| 737 | int ret; |
| 738 | |
Jiang Liu | 3a5670e | 2014-02-19 14:07:33 +0800 | [diff] [blame] | 739 | down_write(&dmar_global_lock); |
Yinghai Lu | f6dd5c3 | 2008-09-03 16:58:32 -0700 | [diff] [blame] | 740 | ret = dmar_table_detect(); |
David Woodhouse | 86cf898 | 2009-11-09 22:15:15 +0000 | [diff] [blame] | 741 | if (ret) |
| 742 | ret = check_zero_address(); |
Suresh Siddha | 2ae2101 | 2008-07-10 11:16:43 -0700 | [diff] [blame] | 743 | { |
Linus Torvalds | 11bd04f | 2009-12-11 12:18:16 -0800 | [diff] [blame] | 744 | if (ret && !no_iommu && !iommu_detected && !dmar_disabled) { |
Suresh Siddha | 2ae2101 | 2008-07-10 11:16:43 -0700 | [diff] [blame] | 745 | iommu_detected = 1; |
Chris Wright | 5d990b6 | 2009-12-04 12:15:21 -0800 | [diff] [blame] | 746 | /* Make sure ACS will be enabled */ |
| 747 | pci_request_acs(); |
| 748 | } |
Suresh Siddha | f5d1b97 | 2011-08-23 17:05:22 -0700 | [diff] [blame] | 749 | |
FUJITA Tomonori | 9d5ce73 | 2009-11-10 19:46:16 +0900 | [diff] [blame] | 750 | #ifdef CONFIG_X86 |
| 751 | if (ret) |
| 752 | x86_init.iommu.iommu_init = intel_iommu_init; |
| 753 | #endif |
Youquan Song | cacd421 | 2008-10-16 16:31:57 -0700 | [diff] [blame] | 754 | } |
Jiang Liu | b707cb0 | 2014-01-06 14:18:26 +0800 | [diff] [blame] | 755 | early_acpi_os_unmap_memory((void __iomem *)dmar_tbl, dmar_tbl_size); |
Yinghai Lu | f6dd5c3 | 2008-09-03 16:58:32 -0700 | [diff] [blame] | 756 | dmar_tbl = NULL; |
Jiang Liu | 3a5670e | 2014-02-19 14:07:33 +0800 | [diff] [blame] | 757 | up_write(&dmar_global_lock); |
Konrad Rzeszutek Wilk | 480125b | 2010-08-26 13:57:57 -0400 | [diff] [blame] | 758 | |
Konrad Rzeszutek Wilk | 4db77ff | 2010-08-26 13:58:04 -0400 | [diff] [blame] | 759 | return ret ? 1 : -ENODEV; |
Suresh Siddha | 2ae2101 | 2008-07-10 11:16:43 -0700 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | |
Donald Dutile | 6f5cf52 | 2012-06-04 17:29:02 -0400 | [diff] [blame] | 763 | static void unmap_iommu(struct intel_iommu *iommu) |
| 764 | { |
| 765 | iounmap(iommu->reg); |
| 766 | release_mem_region(iommu->reg_phys, iommu->reg_size); |
| 767 | } |
| 768 | |
| 769 | /** |
| 770 | * map_iommu: map the iommu's registers |
| 771 | * @iommu: the iommu to map |
| 772 | * @phys_addr: the physical address of the base resgister |
Donald Dutile | e9071b0 | 2012-06-08 17:13:11 -0400 | [diff] [blame] | 773 | * |
Donald Dutile | 6f5cf52 | 2012-06-04 17:29:02 -0400 | [diff] [blame] | 774 | * Memory map the iommu's registers. Start w/ a single page, and |
Donald Dutile | e9071b0 | 2012-06-08 17:13:11 -0400 | [diff] [blame] | 775 | * possibly expand if that turns out to be insufficent. |
Donald Dutile | 6f5cf52 | 2012-06-04 17:29:02 -0400 | [diff] [blame] | 776 | */ |
| 777 | static int map_iommu(struct intel_iommu *iommu, u64 phys_addr) |
| 778 | { |
| 779 | int map_size, err=0; |
| 780 | |
| 781 | iommu->reg_phys = phys_addr; |
| 782 | iommu->reg_size = VTD_PAGE_SIZE; |
| 783 | |
| 784 | if (!request_mem_region(iommu->reg_phys, iommu->reg_size, iommu->name)) { |
| 785 | pr_err("IOMMU: can't reserve memory\n"); |
| 786 | err = -EBUSY; |
| 787 | goto out; |
| 788 | } |
| 789 | |
| 790 | iommu->reg = ioremap(iommu->reg_phys, iommu->reg_size); |
| 791 | if (!iommu->reg) { |
| 792 | pr_err("IOMMU: can't map the region\n"); |
| 793 | err = -ENOMEM; |
| 794 | goto release; |
| 795 | } |
| 796 | |
| 797 | iommu->cap = dmar_readq(iommu->reg + DMAR_CAP_REG); |
| 798 | iommu->ecap = dmar_readq(iommu->reg + DMAR_ECAP_REG); |
| 799 | |
| 800 | if (iommu->cap == (uint64_t)-1 && iommu->ecap == (uint64_t)-1) { |
| 801 | err = -EINVAL; |
| 802 | warn_invalid_dmar(phys_addr, " returns all ones"); |
| 803 | goto unmap; |
| 804 | } |
| 805 | |
| 806 | /* the registers might be more than one page */ |
| 807 | map_size = max_t(int, ecap_max_iotlb_offset(iommu->ecap), |
| 808 | cap_max_fault_reg_offset(iommu->cap)); |
| 809 | map_size = VTD_PAGE_ALIGN(map_size); |
| 810 | if (map_size > iommu->reg_size) { |
| 811 | iounmap(iommu->reg); |
| 812 | release_mem_region(iommu->reg_phys, iommu->reg_size); |
| 813 | iommu->reg_size = map_size; |
| 814 | if (!request_mem_region(iommu->reg_phys, iommu->reg_size, |
| 815 | iommu->name)) { |
| 816 | pr_err("IOMMU: can't reserve memory\n"); |
| 817 | err = -EBUSY; |
| 818 | goto out; |
| 819 | } |
| 820 | iommu->reg = ioremap(iommu->reg_phys, iommu->reg_size); |
| 821 | if (!iommu->reg) { |
| 822 | pr_err("IOMMU: can't map the region\n"); |
| 823 | err = -ENOMEM; |
| 824 | goto release; |
| 825 | } |
| 826 | } |
| 827 | err = 0; |
| 828 | goto out; |
| 829 | |
| 830 | unmap: |
| 831 | iounmap(iommu->reg); |
| 832 | release: |
| 833 | release_mem_region(iommu->reg_phys, iommu->reg_size); |
| 834 | out: |
| 835 | return err; |
| 836 | } |
| 837 | |
Jiang Liu | 694835d | 2014-01-06 14:18:16 +0800 | [diff] [blame] | 838 | static int alloc_iommu(struct dmar_drhd_unit *drhd) |
Suresh Siddha | e61d98d | 2008-07-10 11:16:35 -0700 | [diff] [blame] | 839 | { |
Suresh Siddha | c42d9f3 | 2008-07-10 11:16:36 -0700 | [diff] [blame] | 840 | struct intel_iommu *iommu; |
Takao Indoh | 3a93c84 | 2013-04-23 17:35:03 +0900 | [diff] [blame] | 841 | u32 ver, sts; |
Suresh Siddha | c42d9f3 | 2008-07-10 11:16:36 -0700 | [diff] [blame] | 842 | static int iommu_allocated = 0; |
Joerg Roedel | 43f7392 | 2009-01-03 23:56:27 +0100 | [diff] [blame] | 843 | int agaw = 0; |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 844 | int msagaw = 0; |
Donald Dutile | 6f5cf52 | 2012-06-04 17:29:02 -0400 | [diff] [blame] | 845 | int err; |
Suresh Siddha | c42d9f3 | 2008-07-10 11:16:36 -0700 | [diff] [blame] | 846 | |
David Woodhouse | 6ecbf01 | 2009-12-02 09:20:27 +0000 | [diff] [blame] | 847 | if (!drhd->reg_base_addr) { |
Ben Hutchings | 3a8663e | 2010-04-03 19:37:23 +0100 | [diff] [blame] | 848 | warn_invalid_dmar(0, ""); |
David Woodhouse | 6ecbf01 | 2009-12-02 09:20:27 +0000 | [diff] [blame] | 849 | return -EINVAL; |
| 850 | } |
| 851 | |
Suresh Siddha | c42d9f3 | 2008-07-10 11:16:36 -0700 | [diff] [blame] | 852 | iommu = kzalloc(sizeof(*iommu), GFP_KERNEL); |
| 853 | if (!iommu) |
Suresh Siddha | 1886e8a | 2008-07-10 11:16:37 -0700 | [diff] [blame] | 854 | return -ENOMEM; |
Suresh Siddha | c42d9f3 | 2008-07-10 11:16:36 -0700 | [diff] [blame] | 855 | |
| 856 | iommu->seq_id = iommu_allocated++; |
Suresh Siddha | 9d783ba | 2009-03-16 17:04:55 -0700 | [diff] [blame] | 857 | sprintf (iommu->name, "dmar%d", iommu->seq_id); |
Suresh Siddha | e61d98d | 2008-07-10 11:16:35 -0700 | [diff] [blame] | 858 | |
Donald Dutile | 6f5cf52 | 2012-06-04 17:29:02 -0400 | [diff] [blame] | 859 | err = map_iommu(iommu, drhd->reg_base_addr); |
| 860 | if (err) { |
| 861 | pr_err("IOMMU: failed to map %s\n", iommu->name); |
Suresh Siddha | e61d98d | 2008-07-10 11:16:35 -0700 | [diff] [blame] | 862 | goto error; |
| 863 | } |
Suresh Siddha | e61d98d | 2008-07-10 11:16:35 -0700 | [diff] [blame] | 864 | |
Donald Dutile | 6f5cf52 | 2012-06-04 17:29:02 -0400 | [diff] [blame] | 865 | err = -EINVAL; |
Weidong Han | 1b57368 | 2008-12-08 15:34:06 +0800 | [diff] [blame] | 866 | agaw = iommu_calculate_agaw(iommu); |
| 867 | if (agaw < 0) { |
Donald Dutile | bf947fcb | 2012-06-04 17:29:01 -0400 | [diff] [blame] | 868 | pr_err("Cannot get a valid agaw for iommu (seq_id = %d)\n", |
| 869 | iommu->seq_id); |
David Woodhouse | 0815565 | 2009-08-04 09:17:20 +0100 | [diff] [blame] | 870 | goto err_unmap; |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 871 | } |
| 872 | msagaw = iommu_calculate_max_sagaw(iommu); |
| 873 | if (msagaw < 0) { |
Donald Dutile | bf947fcb | 2012-06-04 17:29:01 -0400 | [diff] [blame] | 874 | 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] | 875 | iommu->seq_id); |
David Woodhouse | 0815565 | 2009-08-04 09:17:20 +0100 | [diff] [blame] | 876 | goto err_unmap; |
Weidong Han | 1b57368 | 2008-12-08 15:34:06 +0800 | [diff] [blame] | 877 | } |
| 878 | iommu->agaw = agaw; |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 879 | iommu->msagaw = msagaw; |
Weidong Han | 1b57368 | 2008-12-08 15:34:06 +0800 | [diff] [blame] | 880 | |
Suresh Siddha | ee34b32 | 2009-10-02 11:01:21 -0700 | [diff] [blame] | 881 | iommu->node = -1; |
| 882 | |
Suresh Siddha | e61d98d | 2008-07-10 11:16:35 -0700 | [diff] [blame] | 883 | ver = readl(iommu->reg + DMAR_VER_REG); |
Yinghai Lu | 680a752 | 2010-04-08 19:58:23 +0100 | [diff] [blame] | 884 | pr_info("IOMMU %d: reg_base_addr %llx ver %d:%d cap %llx ecap %llx\n", |
| 885 | iommu->seq_id, |
Fenghua Yu | 5b6985c | 2008-10-16 18:02:32 -0700 | [diff] [blame] | 886 | (unsigned long long)drhd->reg_base_addr, |
| 887 | DMAR_VER_MAJOR(ver), DMAR_VER_MINOR(ver), |
| 888 | (unsigned long long)iommu->cap, |
| 889 | (unsigned long long)iommu->ecap); |
Suresh Siddha | e61d98d | 2008-07-10 11:16:35 -0700 | [diff] [blame] | 890 | |
Takao Indoh | 3a93c84 | 2013-04-23 17:35:03 +0900 | [diff] [blame] | 891 | /* Reflect status in gcmd */ |
| 892 | sts = readl(iommu->reg + DMAR_GSTS_REG); |
| 893 | if (sts & DMA_GSTS_IRES) |
| 894 | iommu->gcmd |= DMA_GCMD_IRE; |
| 895 | if (sts & DMA_GSTS_TES) |
| 896 | iommu->gcmd |= DMA_GCMD_TE; |
| 897 | if (sts & DMA_GSTS_QIES) |
| 898 | iommu->gcmd |= DMA_GCMD_QIE; |
| 899 | |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 900 | raw_spin_lock_init(&iommu->register_lock); |
Suresh Siddha | e61d98d | 2008-07-10 11:16:35 -0700 | [diff] [blame] | 901 | |
| 902 | drhd->iommu = iommu; |
Suresh Siddha | 1886e8a | 2008-07-10 11:16:37 -0700 | [diff] [blame] | 903 | return 0; |
David Woodhouse | 0815565 | 2009-08-04 09:17:20 +0100 | [diff] [blame] | 904 | |
| 905 | err_unmap: |
Donald Dutile | 6f5cf52 | 2012-06-04 17:29:02 -0400 | [diff] [blame] | 906 | unmap_iommu(iommu); |
David Woodhouse | 0815565 | 2009-08-04 09:17:20 +0100 | [diff] [blame] | 907 | error: |
Suresh Siddha | e61d98d | 2008-07-10 11:16:35 -0700 | [diff] [blame] | 908 | kfree(iommu); |
Donald Dutile | 6f5cf52 | 2012-06-04 17:29:02 -0400 | [diff] [blame] | 909 | return err; |
Suresh Siddha | e61d98d | 2008-07-10 11:16:35 -0700 | [diff] [blame] | 910 | } |
| 911 | |
Jiang Liu | a868e6b | 2014-01-06 14:18:20 +0800 | [diff] [blame] | 912 | static void free_iommu(struct intel_iommu *iommu) |
Suresh Siddha | e61d98d | 2008-07-10 11:16:35 -0700 | [diff] [blame] | 913 | { |
Jiang Liu | a868e6b | 2014-01-06 14:18:20 +0800 | [diff] [blame] | 914 | if (iommu->irq) { |
| 915 | free_irq(iommu->irq, iommu); |
| 916 | irq_set_handler_data(iommu->irq, NULL); |
| 917 | destroy_irq(iommu->irq); |
| 918 | } |
Suresh Siddha | e61d98d | 2008-07-10 11:16:35 -0700 | [diff] [blame] | 919 | |
Jiang Liu | a84da70 | 2014-01-06 14:18:23 +0800 | [diff] [blame] | 920 | if (iommu->qi) { |
| 921 | free_page((unsigned long)iommu->qi->desc); |
| 922 | kfree(iommu->qi->desc_status); |
| 923 | kfree(iommu->qi); |
| 924 | } |
| 925 | |
Suresh Siddha | e61d98d | 2008-07-10 11:16:35 -0700 | [diff] [blame] | 926 | if (iommu->reg) |
Donald Dutile | 6f5cf52 | 2012-06-04 17:29:02 -0400 | [diff] [blame] | 927 | unmap_iommu(iommu); |
| 928 | |
Suresh Siddha | e61d98d | 2008-07-10 11:16:35 -0700 | [diff] [blame] | 929 | kfree(iommu); |
| 930 | } |
Suresh Siddha | fe962e9 | 2008-07-10 11:16:42 -0700 | [diff] [blame] | 931 | |
| 932 | /* |
| 933 | * Reclaim all the submitted descriptors which have completed its work. |
| 934 | */ |
| 935 | static inline void reclaim_free_desc(struct q_inval *qi) |
| 936 | { |
Yu Zhao | 6ba6c3a | 2009-05-18 13:51:35 +0800 | [diff] [blame] | 937 | while (qi->desc_status[qi->free_tail] == QI_DONE || |
| 938 | qi->desc_status[qi->free_tail] == QI_ABORT) { |
Suresh Siddha | fe962e9 | 2008-07-10 11:16:42 -0700 | [diff] [blame] | 939 | qi->desc_status[qi->free_tail] = QI_FREE; |
| 940 | qi->free_tail = (qi->free_tail + 1) % QI_LENGTH; |
| 941 | qi->free_cnt++; |
| 942 | } |
| 943 | } |
| 944 | |
Yu Zhao | 704126a | 2009-01-04 16:28:52 +0800 | [diff] [blame] | 945 | static int qi_check_fault(struct intel_iommu *iommu, int index) |
| 946 | { |
| 947 | u32 fault; |
Yu Zhao | 6ba6c3a | 2009-05-18 13:51:35 +0800 | [diff] [blame] | 948 | int head, tail; |
Yu Zhao | 704126a | 2009-01-04 16:28:52 +0800 | [diff] [blame] | 949 | struct q_inval *qi = iommu->qi; |
| 950 | int wait_index = (index + 1) % QI_LENGTH; |
| 951 | |
Yu Zhao | 6ba6c3a | 2009-05-18 13:51:35 +0800 | [diff] [blame] | 952 | if (qi->desc_status[wait_index] == QI_ABORT) |
| 953 | return -EAGAIN; |
| 954 | |
Yu Zhao | 704126a | 2009-01-04 16:28:52 +0800 | [diff] [blame] | 955 | fault = readl(iommu->reg + DMAR_FSTS_REG); |
| 956 | |
| 957 | /* |
| 958 | * If IQE happens, the head points to the descriptor associated |
| 959 | * with the error. No new descriptors are fetched until the IQE |
| 960 | * is cleared. |
| 961 | */ |
| 962 | if (fault & DMA_FSTS_IQE) { |
| 963 | head = readl(iommu->reg + DMAR_IQH_REG); |
Yu Zhao | 6ba6c3a | 2009-05-18 13:51:35 +0800 | [diff] [blame] | 964 | if ((head >> DMAR_IQ_SHIFT) == index) { |
Donald Dutile | bf947fcb | 2012-06-04 17:29:01 -0400 | [diff] [blame] | 965 | pr_err("VT-d detected invalid descriptor: " |
Yu Zhao | 6ba6c3a | 2009-05-18 13:51:35 +0800 | [diff] [blame] | 966 | "low=%llx, high=%llx\n", |
| 967 | (unsigned long long)qi->desc[index].low, |
| 968 | (unsigned long long)qi->desc[index].high); |
Yu Zhao | 704126a | 2009-01-04 16:28:52 +0800 | [diff] [blame] | 969 | memcpy(&qi->desc[index], &qi->desc[wait_index], |
| 970 | sizeof(struct qi_desc)); |
| 971 | __iommu_flush_cache(iommu, &qi->desc[index], |
| 972 | sizeof(struct qi_desc)); |
| 973 | writel(DMA_FSTS_IQE, iommu->reg + DMAR_FSTS_REG); |
| 974 | return -EINVAL; |
| 975 | } |
| 976 | } |
| 977 | |
Yu Zhao | 6ba6c3a | 2009-05-18 13:51:35 +0800 | [diff] [blame] | 978 | /* |
| 979 | * If ITE happens, all pending wait_desc commands are aborted. |
| 980 | * No new descriptors are fetched until the ITE is cleared. |
| 981 | */ |
| 982 | if (fault & DMA_FSTS_ITE) { |
| 983 | head = readl(iommu->reg + DMAR_IQH_REG); |
| 984 | head = ((head >> DMAR_IQ_SHIFT) - 1 + QI_LENGTH) % QI_LENGTH; |
| 985 | head |= 1; |
| 986 | tail = readl(iommu->reg + DMAR_IQT_REG); |
| 987 | tail = ((tail >> DMAR_IQ_SHIFT) - 1 + QI_LENGTH) % QI_LENGTH; |
| 988 | |
| 989 | writel(DMA_FSTS_ITE, iommu->reg + DMAR_FSTS_REG); |
| 990 | |
| 991 | do { |
| 992 | if (qi->desc_status[head] == QI_IN_USE) |
| 993 | qi->desc_status[head] = QI_ABORT; |
| 994 | head = (head - 2 + QI_LENGTH) % QI_LENGTH; |
| 995 | } while (head != tail); |
| 996 | |
| 997 | if (qi->desc_status[wait_index] == QI_ABORT) |
| 998 | return -EAGAIN; |
| 999 | } |
| 1000 | |
| 1001 | if (fault & DMA_FSTS_ICE) |
| 1002 | writel(DMA_FSTS_ICE, iommu->reg + DMAR_FSTS_REG); |
| 1003 | |
Yu Zhao | 704126a | 2009-01-04 16:28:52 +0800 | [diff] [blame] | 1004 | return 0; |
| 1005 | } |
| 1006 | |
Suresh Siddha | fe962e9 | 2008-07-10 11:16:42 -0700 | [diff] [blame] | 1007 | /* |
| 1008 | * Submit the queued invalidation descriptor to the remapping |
| 1009 | * hardware unit and wait for its completion. |
| 1010 | */ |
Yu Zhao | 704126a | 2009-01-04 16:28:52 +0800 | [diff] [blame] | 1011 | int qi_submit_sync(struct qi_desc *desc, struct intel_iommu *iommu) |
Suresh Siddha | fe962e9 | 2008-07-10 11:16:42 -0700 | [diff] [blame] | 1012 | { |
Yu Zhao | 6ba6c3a | 2009-05-18 13:51:35 +0800 | [diff] [blame] | 1013 | int rc; |
Suresh Siddha | fe962e9 | 2008-07-10 11:16:42 -0700 | [diff] [blame] | 1014 | struct q_inval *qi = iommu->qi; |
| 1015 | struct qi_desc *hw, wait_desc; |
| 1016 | int wait_index, index; |
| 1017 | unsigned long flags; |
| 1018 | |
| 1019 | if (!qi) |
Yu Zhao | 704126a | 2009-01-04 16:28:52 +0800 | [diff] [blame] | 1020 | return 0; |
Suresh Siddha | fe962e9 | 2008-07-10 11:16:42 -0700 | [diff] [blame] | 1021 | |
| 1022 | hw = qi->desc; |
| 1023 | |
Yu Zhao | 6ba6c3a | 2009-05-18 13:51:35 +0800 | [diff] [blame] | 1024 | restart: |
| 1025 | rc = 0; |
| 1026 | |
Thomas Gleixner | 3b8f404 | 2011-07-19 17:02:07 +0200 | [diff] [blame] | 1027 | raw_spin_lock_irqsave(&qi->q_lock, flags); |
Suresh Siddha | fe962e9 | 2008-07-10 11:16:42 -0700 | [diff] [blame] | 1028 | while (qi->free_cnt < 3) { |
Thomas Gleixner | 3b8f404 | 2011-07-19 17:02:07 +0200 | [diff] [blame] | 1029 | raw_spin_unlock_irqrestore(&qi->q_lock, flags); |
Suresh Siddha | fe962e9 | 2008-07-10 11:16:42 -0700 | [diff] [blame] | 1030 | cpu_relax(); |
Thomas Gleixner | 3b8f404 | 2011-07-19 17:02:07 +0200 | [diff] [blame] | 1031 | raw_spin_lock_irqsave(&qi->q_lock, flags); |
Suresh Siddha | fe962e9 | 2008-07-10 11:16:42 -0700 | [diff] [blame] | 1032 | } |
| 1033 | |
| 1034 | index = qi->free_head; |
| 1035 | wait_index = (index + 1) % QI_LENGTH; |
| 1036 | |
| 1037 | qi->desc_status[index] = qi->desc_status[wait_index] = QI_IN_USE; |
| 1038 | |
| 1039 | hw[index] = *desc; |
| 1040 | |
Yu Zhao | 704126a | 2009-01-04 16:28:52 +0800 | [diff] [blame] | 1041 | wait_desc.low = QI_IWD_STATUS_DATA(QI_DONE) | |
| 1042 | QI_IWD_STATUS_WRITE | QI_IWD_TYPE; |
Suresh Siddha | fe962e9 | 2008-07-10 11:16:42 -0700 | [diff] [blame] | 1043 | wait_desc.high = virt_to_phys(&qi->desc_status[wait_index]); |
| 1044 | |
| 1045 | hw[wait_index] = wait_desc; |
| 1046 | |
| 1047 | __iommu_flush_cache(iommu, &hw[index], sizeof(struct qi_desc)); |
| 1048 | __iommu_flush_cache(iommu, &hw[wait_index], sizeof(struct qi_desc)); |
| 1049 | |
| 1050 | qi->free_head = (qi->free_head + 2) % QI_LENGTH; |
| 1051 | qi->free_cnt -= 2; |
| 1052 | |
Suresh Siddha | fe962e9 | 2008-07-10 11:16:42 -0700 | [diff] [blame] | 1053 | /* |
| 1054 | * update the HW tail register indicating the presence of |
| 1055 | * new descriptors. |
| 1056 | */ |
Yu Zhao | 6ba6c3a | 2009-05-18 13:51:35 +0800 | [diff] [blame] | 1057 | writel(qi->free_head << DMAR_IQ_SHIFT, iommu->reg + DMAR_IQT_REG); |
Suresh Siddha | fe962e9 | 2008-07-10 11:16:42 -0700 | [diff] [blame] | 1058 | |
| 1059 | while (qi->desc_status[wait_index] != QI_DONE) { |
Suresh Siddha | f05810c | 2008-10-16 16:31:54 -0700 | [diff] [blame] | 1060 | /* |
| 1061 | * We will leave the interrupts disabled, to prevent interrupt |
| 1062 | * context to queue another cmd while a cmd is already submitted |
| 1063 | * and waiting for completion on this cpu. This is to avoid |
| 1064 | * a deadlock where the interrupt context can wait indefinitely |
| 1065 | * for free slots in the queue. |
| 1066 | */ |
Yu Zhao | 704126a | 2009-01-04 16:28:52 +0800 | [diff] [blame] | 1067 | rc = qi_check_fault(iommu, index); |
| 1068 | if (rc) |
Yu Zhao | 6ba6c3a | 2009-05-18 13:51:35 +0800 | [diff] [blame] | 1069 | break; |
Yu Zhao | 704126a | 2009-01-04 16:28:52 +0800 | [diff] [blame] | 1070 | |
Thomas Gleixner | 3b8f404 | 2011-07-19 17:02:07 +0200 | [diff] [blame] | 1071 | raw_spin_unlock(&qi->q_lock); |
Suresh Siddha | fe962e9 | 2008-07-10 11:16:42 -0700 | [diff] [blame] | 1072 | cpu_relax(); |
Thomas Gleixner | 3b8f404 | 2011-07-19 17:02:07 +0200 | [diff] [blame] | 1073 | raw_spin_lock(&qi->q_lock); |
Suresh Siddha | fe962e9 | 2008-07-10 11:16:42 -0700 | [diff] [blame] | 1074 | } |
Yu Zhao | 6ba6c3a | 2009-05-18 13:51:35 +0800 | [diff] [blame] | 1075 | |
| 1076 | qi->desc_status[index] = QI_DONE; |
Suresh Siddha | fe962e9 | 2008-07-10 11:16:42 -0700 | [diff] [blame] | 1077 | |
| 1078 | reclaim_free_desc(qi); |
Thomas Gleixner | 3b8f404 | 2011-07-19 17:02:07 +0200 | [diff] [blame] | 1079 | raw_spin_unlock_irqrestore(&qi->q_lock, flags); |
Yu Zhao | 704126a | 2009-01-04 16:28:52 +0800 | [diff] [blame] | 1080 | |
Yu Zhao | 6ba6c3a | 2009-05-18 13:51:35 +0800 | [diff] [blame] | 1081 | if (rc == -EAGAIN) |
| 1082 | goto restart; |
| 1083 | |
Yu Zhao | 704126a | 2009-01-04 16:28:52 +0800 | [diff] [blame] | 1084 | return rc; |
Suresh Siddha | fe962e9 | 2008-07-10 11:16:42 -0700 | [diff] [blame] | 1085 | } |
| 1086 | |
| 1087 | /* |
| 1088 | * Flush the global interrupt entry cache. |
| 1089 | */ |
| 1090 | void qi_global_iec(struct intel_iommu *iommu) |
| 1091 | { |
| 1092 | struct qi_desc desc; |
| 1093 | |
| 1094 | desc.low = QI_IEC_TYPE; |
| 1095 | desc.high = 0; |
| 1096 | |
Yu Zhao | 704126a | 2009-01-04 16:28:52 +0800 | [diff] [blame] | 1097 | /* should never fail */ |
Suresh Siddha | fe962e9 | 2008-07-10 11:16:42 -0700 | [diff] [blame] | 1098 | qi_submit_sync(&desc, iommu); |
| 1099 | } |
| 1100 | |
David Woodhouse | 4c25a2c | 2009-05-10 17:16:06 +0100 | [diff] [blame] | 1101 | void qi_flush_context(struct intel_iommu *iommu, u16 did, u16 sid, u8 fm, |
| 1102 | u64 type) |
Youquan Song | 3481f21 | 2008-10-16 16:31:55 -0700 | [diff] [blame] | 1103 | { |
Youquan Song | 3481f21 | 2008-10-16 16:31:55 -0700 | [diff] [blame] | 1104 | struct qi_desc desc; |
| 1105 | |
Youquan Song | 3481f21 | 2008-10-16 16:31:55 -0700 | [diff] [blame] | 1106 | desc.low = QI_CC_FM(fm) | QI_CC_SID(sid) | QI_CC_DID(did) |
| 1107 | | QI_CC_GRAN(type) | QI_CC_TYPE; |
| 1108 | desc.high = 0; |
| 1109 | |
David Woodhouse | 4c25a2c | 2009-05-10 17:16:06 +0100 | [diff] [blame] | 1110 | qi_submit_sync(&desc, iommu); |
Youquan Song | 3481f21 | 2008-10-16 16:31:55 -0700 | [diff] [blame] | 1111 | } |
| 1112 | |
David Woodhouse | 1f0ef2a | 2009-05-10 19:58:49 +0100 | [diff] [blame] | 1113 | void qi_flush_iotlb(struct intel_iommu *iommu, u16 did, u64 addr, |
| 1114 | unsigned int size_order, u64 type) |
Youquan Song | 3481f21 | 2008-10-16 16:31:55 -0700 | [diff] [blame] | 1115 | { |
| 1116 | u8 dw = 0, dr = 0; |
| 1117 | |
| 1118 | struct qi_desc desc; |
| 1119 | int ih = 0; |
| 1120 | |
Youquan Song | 3481f21 | 2008-10-16 16:31:55 -0700 | [diff] [blame] | 1121 | if (cap_write_drain(iommu->cap)) |
| 1122 | dw = 1; |
| 1123 | |
| 1124 | if (cap_read_drain(iommu->cap)) |
| 1125 | dr = 1; |
| 1126 | |
| 1127 | desc.low = QI_IOTLB_DID(did) | QI_IOTLB_DR(dr) | QI_IOTLB_DW(dw) |
| 1128 | | QI_IOTLB_GRAN(type) | QI_IOTLB_TYPE; |
| 1129 | desc.high = QI_IOTLB_ADDR(addr) | QI_IOTLB_IH(ih) |
| 1130 | | QI_IOTLB_AM(size_order); |
| 1131 | |
David Woodhouse | 1f0ef2a | 2009-05-10 19:58:49 +0100 | [diff] [blame] | 1132 | qi_submit_sync(&desc, iommu); |
Youquan Song | 3481f21 | 2008-10-16 16:31:55 -0700 | [diff] [blame] | 1133 | } |
| 1134 | |
Yu Zhao | 6ba6c3a | 2009-05-18 13:51:35 +0800 | [diff] [blame] | 1135 | void qi_flush_dev_iotlb(struct intel_iommu *iommu, u16 sid, u16 qdep, |
| 1136 | u64 addr, unsigned mask) |
| 1137 | { |
| 1138 | struct qi_desc desc; |
| 1139 | |
| 1140 | if (mask) { |
| 1141 | BUG_ON(addr & ((1 << (VTD_PAGE_SHIFT + mask)) - 1)); |
| 1142 | addr |= (1 << (VTD_PAGE_SHIFT + mask - 1)) - 1; |
| 1143 | desc.high = QI_DEV_IOTLB_ADDR(addr) | QI_DEV_IOTLB_SIZE; |
| 1144 | } else |
| 1145 | desc.high = QI_DEV_IOTLB_ADDR(addr); |
| 1146 | |
| 1147 | if (qdep >= QI_DEV_IOTLB_MAX_INVS) |
| 1148 | qdep = 0; |
| 1149 | |
| 1150 | desc.low = QI_DEV_IOTLB_SID(sid) | QI_DEV_IOTLB_QDEP(qdep) | |
| 1151 | QI_DIOTLB_TYPE; |
| 1152 | |
| 1153 | qi_submit_sync(&desc, iommu); |
| 1154 | } |
| 1155 | |
Suresh Siddha | fe962e9 | 2008-07-10 11:16:42 -0700 | [diff] [blame] | 1156 | /* |
Suresh Siddha | eba67e5 | 2009-03-16 17:04:56 -0700 | [diff] [blame] | 1157 | * Disable Queued Invalidation interface. |
| 1158 | */ |
| 1159 | void dmar_disable_qi(struct intel_iommu *iommu) |
| 1160 | { |
| 1161 | unsigned long flags; |
| 1162 | u32 sts; |
| 1163 | cycles_t start_time = get_cycles(); |
| 1164 | |
| 1165 | if (!ecap_qis(iommu->ecap)) |
| 1166 | return; |
| 1167 | |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 1168 | raw_spin_lock_irqsave(&iommu->register_lock, flags); |
Suresh Siddha | eba67e5 | 2009-03-16 17:04:56 -0700 | [diff] [blame] | 1169 | |
| 1170 | sts = dmar_readq(iommu->reg + DMAR_GSTS_REG); |
| 1171 | if (!(sts & DMA_GSTS_QIES)) |
| 1172 | goto end; |
| 1173 | |
| 1174 | /* |
| 1175 | * Give a chance to HW to complete the pending invalidation requests. |
| 1176 | */ |
| 1177 | while ((readl(iommu->reg + DMAR_IQT_REG) != |
| 1178 | readl(iommu->reg + DMAR_IQH_REG)) && |
| 1179 | (DMAR_OPERATION_TIMEOUT > (get_cycles() - start_time))) |
| 1180 | cpu_relax(); |
| 1181 | |
| 1182 | iommu->gcmd &= ~DMA_GCMD_QIE; |
Suresh Siddha | eba67e5 | 2009-03-16 17:04:56 -0700 | [diff] [blame] | 1183 | writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG); |
| 1184 | |
| 1185 | IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG, readl, |
| 1186 | !(sts & DMA_GSTS_QIES), sts); |
| 1187 | end: |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 1188 | raw_spin_unlock_irqrestore(&iommu->register_lock, flags); |
Suresh Siddha | eba67e5 | 2009-03-16 17:04:56 -0700 | [diff] [blame] | 1189 | } |
| 1190 | |
| 1191 | /* |
Fenghua Yu | eb4a52b | 2009-03-27 14:22:43 -0700 | [diff] [blame] | 1192 | * Enable queued invalidation. |
| 1193 | */ |
| 1194 | static void __dmar_enable_qi(struct intel_iommu *iommu) |
| 1195 | { |
David Woodhouse | c416daa | 2009-05-10 20:30:58 +0100 | [diff] [blame] | 1196 | u32 sts; |
Fenghua Yu | eb4a52b | 2009-03-27 14:22:43 -0700 | [diff] [blame] | 1197 | unsigned long flags; |
| 1198 | struct q_inval *qi = iommu->qi; |
| 1199 | |
| 1200 | qi->free_head = qi->free_tail = 0; |
| 1201 | qi->free_cnt = QI_LENGTH; |
| 1202 | |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 1203 | raw_spin_lock_irqsave(&iommu->register_lock, flags); |
Fenghua Yu | eb4a52b | 2009-03-27 14:22:43 -0700 | [diff] [blame] | 1204 | |
| 1205 | /* write zero to the tail reg */ |
| 1206 | writel(0, iommu->reg + DMAR_IQT_REG); |
| 1207 | |
| 1208 | dmar_writeq(iommu->reg + DMAR_IQA_REG, virt_to_phys(qi->desc)); |
| 1209 | |
Fenghua Yu | eb4a52b | 2009-03-27 14:22:43 -0700 | [diff] [blame] | 1210 | iommu->gcmd |= DMA_GCMD_QIE; |
David Woodhouse | c416daa | 2009-05-10 20:30:58 +0100 | [diff] [blame] | 1211 | writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG); |
Fenghua Yu | eb4a52b | 2009-03-27 14:22:43 -0700 | [diff] [blame] | 1212 | |
| 1213 | /* Make sure hardware complete it */ |
| 1214 | IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG, readl, (sts & DMA_GSTS_QIES), sts); |
| 1215 | |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 1216 | raw_spin_unlock_irqrestore(&iommu->register_lock, flags); |
Fenghua Yu | eb4a52b | 2009-03-27 14:22:43 -0700 | [diff] [blame] | 1217 | } |
| 1218 | |
| 1219 | /* |
Suresh Siddha | fe962e9 | 2008-07-10 11:16:42 -0700 | [diff] [blame] | 1220 | * Enable Queued Invalidation interface. This is a must to support |
| 1221 | * interrupt-remapping. Also used by DMA-remapping, which replaces |
| 1222 | * register based IOTLB invalidation. |
| 1223 | */ |
| 1224 | int dmar_enable_qi(struct intel_iommu *iommu) |
| 1225 | { |
Suresh Siddha | fe962e9 | 2008-07-10 11:16:42 -0700 | [diff] [blame] | 1226 | struct q_inval *qi; |
Suresh Siddha | 751cafe | 2009-10-02 11:01:22 -0700 | [diff] [blame] | 1227 | struct page *desc_page; |
Suresh Siddha | fe962e9 | 2008-07-10 11:16:42 -0700 | [diff] [blame] | 1228 | |
| 1229 | if (!ecap_qis(iommu->ecap)) |
| 1230 | return -ENOENT; |
| 1231 | |
| 1232 | /* |
| 1233 | * queued invalidation is already setup and enabled. |
| 1234 | */ |
| 1235 | if (iommu->qi) |
| 1236 | return 0; |
| 1237 | |
Suresh Siddha | fa4b57c | 2009-03-16 17:05:05 -0700 | [diff] [blame] | 1238 | iommu->qi = kmalloc(sizeof(*qi), GFP_ATOMIC); |
Suresh Siddha | fe962e9 | 2008-07-10 11:16:42 -0700 | [diff] [blame] | 1239 | if (!iommu->qi) |
| 1240 | return -ENOMEM; |
| 1241 | |
| 1242 | qi = iommu->qi; |
| 1243 | |
Suresh Siddha | 751cafe | 2009-10-02 11:01:22 -0700 | [diff] [blame] | 1244 | |
| 1245 | desc_page = alloc_pages_node(iommu->node, GFP_ATOMIC | __GFP_ZERO, 0); |
| 1246 | if (!desc_page) { |
Suresh Siddha | fe962e9 | 2008-07-10 11:16:42 -0700 | [diff] [blame] | 1247 | kfree(qi); |
Jiang Liu | b707cb0 | 2014-01-06 14:18:26 +0800 | [diff] [blame] | 1248 | iommu->qi = NULL; |
Suresh Siddha | fe962e9 | 2008-07-10 11:16:42 -0700 | [diff] [blame] | 1249 | return -ENOMEM; |
| 1250 | } |
| 1251 | |
Suresh Siddha | 751cafe | 2009-10-02 11:01:22 -0700 | [diff] [blame] | 1252 | qi->desc = page_address(desc_page); |
| 1253 | |
Hannes Reinecke | 37a4071 | 2013-02-06 09:50:10 +0100 | [diff] [blame] | 1254 | qi->desc_status = kzalloc(QI_LENGTH * sizeof(int), GFP_ATOMIC); |
Suresh Siddha | fe962e9 | 2008-07-10 11:16:42 -0700 | [diff] [blame] | 1255 | if (!qi->desc_status) { |
| 1256 | free_page((unsigned long) qi->desc); |
| 1257 | kfree(qi); |
Jiang Liu | b707cb0 | 2014-01-06 14:18:26 +0800 | [diff] [blame] | 1258 | iommu->qi = NULL; |
Suresh Siddha | fe962e9 | 2008-07-10 11:16:42 -0700 | [diff] [blame] | 1259 | return -ENOMEM; |
| 1260 | } |
| 1261 | |
| 1262 | qi->free_head = qi->free_tail = 0; |
| 1263 | qi->free_cnt = QI_LENGTH; |
| 1264 | |
Thomas Gleixner | 3b8f404 | 2011-07-19 17:02:07 +0200 | [diff] [blame] | 1265 | raw_spin_lock_init(&qi->q_lock); |
Suresh Siddha | fe962e9 | 2008-07-10 11:16:42 -0700 | [diff] [blame] | 1266 | |
Fenghua Yu | eb4a52b | 2009-03-27 14:22:43 -0700 | [diff] [blame] | 1267 | __dmar_enable_qi(iommu); |
Suresh Siddha | fe962e9 | 2008-07-10 11:16:42 -0700 | [diff] [blame] | 1268 | |
| 1269 | return 0; |
| 1270 | } |
Suresh Siddha | 0ac2491 | 2009-03-16 17:04:54 -0700 | [diff] [blame] | 1271 | |
| 1272 | /* iommu interrupt handling. Most stuff are MSI-like. */ |
| 1273 | |
Suresh Siddha | 9d783ba | 2009-03-16 17:04:55 -0700 | [diff] [blame] | 1274 | enum faulttype { |
| 1275 | DMA_REMAP, |
| 1276 | INTR_REMAP, |
| 1277 | UNKNOWN, |
| 1278 | }; |
| 1279 | |
| 1280 | static const char *dma_remap_fault_reasons[] = |
Suresh Siddha | 0ac2491 | 2009-03-16 17:04:54 -0700 | [diff] [blame] | 1281 | { |
| 1282 | "Software", |
| 1283 | "Present bit in root entry is clear", |
| 1284 | "Present bit in context entry is clear", |
| 1285 | "Invalid context entry", |
| 1286 | "Access beyond MGAW", |
| 1287 | "PTE Write access is not set", |
| 1288 | "PTE Read access is not set", |
| 1289 | "Next page table ptr is invalid", |
| 1290 | "Root table address invalid", |
| 1291 | "Context table ptr is invalid", |
| 1292 | "non-zero reserved fields in RTP", |
| 1293 | "non-zero reserved fields in CTP", |
| 1294 | "non-zero reserved fields in PTE", |
Li, Zhen-Hua | 4ecccd9 | 2013-03-06 10:43:17 +0800 | [diff] [blame] | 1295 | "PCE for translation request specifies blocking", |
Suresh Siddha | 0ac2491 | 2009-03-16 17:04:54 -0700 | [diff] [blame] | 1296 | }; |
Suresh Siddha | 9d783ba | 2009-03-16 17:04:55 -0700 | [diff] [blame] | 1297 | |
Suresh Siddha | 95a02e9 | 2012-03-30 11:47:07 -0700 | [diff] [blame] | 1298 | static const char *irq_remap_fault_reasons[] = |
Suresh Siddha | 9d783ba | 2009-03-16 17:04:55 -0700 | [diff] [blame] | 1299 | { |
| 1300 | "Detected reserved fields in the decoded interrupt-remapped request", |
| 1301 | "Interrupt index exceeded the interrupt-remapping table size", |
| 1302 | "Present field in the IRTE entry is clear", |
| 1303 | "Error accessing interrupt-remapping table pointed by IRTA_REG", |
| 1304 | "Detected reserved fields in the IRTE entry", |
| 1305 | "Blocked a compatibility format interrupt request", |
| 1306 | "Blocked an interrupt request due to source-id verification failure", |
| 1307 | }; |
| 1308 | |
Rashika Kheria | 21004dc | 2013-12-18 12:01:46 +0530 | [diff] [blame] | 1309 | 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] | 1310 | { |
Dan Carpenter | fefe1ed | 2012-05-13 20:09:38 +0300 | [diff] [blame] | 1311 | if (fault_reason >= 0x20 && (fault_reason - 0x20 < |
| 1312 | ARRAY_SIZE(irq_remap_fault_reasons))) { |
Suresh Siddha | 9d783ba | 2009-03-16 17:04:55 -0700 | [diff] [blame] | 1313 | *fault_type = INTR_REMAP; |
Suresh Siddha | 95a02e9 | 2012-03-30 11:47:07 -0700 | [diff] [blame] | 1314 | return irq_remap_fault_reasons[fault_reason - 0x20]; |
Suresh Siddha | 9d783ba | 2009-03-16 17:04:55 -0700 | [diff] [blame] | 1315 | } else if (fault_reason < ARRAY_SIZE(dma_remap_fault_reasons)) { |
| 1316 | *fault_type = DMA_REMAP; |
| 1317 | return dma_remap_fault_reasons[fault_reason]; |
| 1318 | } else { |
| 1319 | *fault_type = UNKNOWN; |
Suresh Siddha | 0ac2491 | 2009-03-16 17:04:54 -0700 | [diff] [blame] | 1320 | return "Unknown"; |
Suresh Siddha | 9d783ba | 2009-03-16 17:04:55 -0700 | [diff] [blame] | 1321 | } |
Suresh Siddha | 0ac2491 | 2009-03-16 17:04:54 -0700 | [diff] [blame] | 1322 | } |
| 1323 | |
Thomas Gleixner | 5c2837f | 2010-09-28 17:15:11 +0200 | [diff] [blame] | 1324 | void dmar_msi_unmask(struct irq_data *data) |
Suresh Siddha | 0ac2491 | 2009-03-16 17:04:54 -0700 | [diff] [blame] | 1325 | { |
Thomas Gleixner | dced35a | 2011-03-28 17:49:12 +0200 | [diff] [blame] | 1326 | struct intel_iommu *iommu = irq_data_get_irq_handler_data(data); |
Suresh Siddha | 0ac2491 | 2009-03-16 17:04:54 -0700 | [diff] [blame] | 1327 | unsigned long flag; |
| 1328 | |
| 1329 | /* unmask it */ |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 1330 | raw_spin_lock_irqsave(&iommu->register_lock, flag); |
Suresh Siddha | 0ac2491 | 2009-03-16 17:04:54 -0700 | [diff] [blame] | 1331 | writel(0, iommu->reg + DMAR_FECTL_REG); |
| 1332 | /* Read a reg to force flush the post write */ |
| 1333 | readl(iommu->reg + DMAR_FECTL_REG); |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 1334 | raw_spin_unlock_irqrestore(&iommu->register_lock, flag); |
Suresh Siddha | 0ac2491 | 2009-03-16 17:04:54 -0700 | [diff] [blame] | 1335 | } |
| 1336 | |
Thomas Gleixner | 5c2837f | 2010-09-28 17:15:11 +0200 | [diff] [blame] | 1337 | void dmar_msi_mask(struct irq_data *data) |
Suresh Siddha | 0ac2491 | 2009-03-16 17:04:54 -0700 | [diff] [blame] | 1338 | { |
| 1339 | unsigned long flag; |
Thomas Gleixner | dced35a | 2011-03-28 17:49:12 +0200 | [diff] [blame] | 1340 | struct intel_iommu *iommu = irq_data_get_irq_handler_data(data); |
Suresh Siddha | 0ac2491 | 2009-03-16 17:04:54 -0700 | [diff] [blame] | 1341 | |
| 1342 | /* mask it */ |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 1343 | raw_spin_lock_irqsave(&iommu->register_lock, flag); |
Suresh Siddha | 0ac2491 | 2009-03-16 17:04:54 -0700 | [diff] [blame] | 1344 | writel(DMA_FECTL_IM, iommu->reg + DMAR_FECTL_REG); |
| 1345 | /* Read a reg to force flush the post write */ |
| 1346 | readl(iommu->reg + DMAR_FECTL_REG); |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 1347 | raw_spin_unlock_irqrestore(&iommu->register_lock, flag); |
Suresh Siddha | 0ac2491 | 2009-03-16 17:04:54 -0700 | [diff] [blame] | 1348 | } |
| 1349 | |
| 1350 | void dmar_msi_write(int irq, struct msi_msg *msg) |
| 1351 | { |
Thomas Gleixner | dced35a | 2011-03-28 17:49:12 +0200 | [diff] [blame] | 1352 | struct intel_iommu *iommu = irq_get_handler_data(irq); |
Suresh Siddha | 0ac2491 | 2009-03-16 17:04:54 -0700 | [diff] [blame] | 1353 | unsigned long flag; |
| 1354 | |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 1355 | raw_spin_lock_irqsave(&iommu->register_lock, flag); |
Suresh Siddha | 0ac2491 | 2009-03-16 17:04:54 -0700 | [diff] [blame] | 1356 | writel(msg->data, iommu->reg + DMAR_FEDATA_REG); |
| 1357 | writel(msg->address_lo, iommu->reg + DMAR_FEADDR_REG); |
| 1358 | writel(msg->address_hi, iommu->reg + DMAR_FEUADDR_REG); |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 1359 | raw_spin_unlock_irqrestore(&iommu->register_lock, flag); |
Suresh Siddha | 0ac2491 | 2009-03-16 17:04:54 -0700 | [diff] [blame] | 1360 | } |
| 1361 | |
| 1362 | void dmar_msi_read(int irq, struct msi_msg *msg) |
| 1363 | { |
Thomas Gleixner | dced35a | 2011-03-28 17:49:12 +0200 | [diff] [blame] | 1364 | struct intel_iommu *iommu = irq_get_handler_data(irq); |
Suresh Siddha | 0ac2491 | 2009-03-16 17:04:54 -0700 | [diff] [blame] | 1365 | unsigned long flag; |
| 1366 | |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 1367 | raw_spin_lock_irqsave(&iommu->register_lock, flag); |
Suresh Siddha | 0ac2491 | 2009-03-16 17:04:54 -0700 | [diff] [blame] | 1368 | msg->data = readl(iommu->reg + DMAR_FEDATA_REG); |
| 1369 | msg->address_lo = readl(iommu->reg + DMAR_FEADDR_REG); |
| 1370 | msg->address_hi = readl(iommu->reg + DMAR_FEUADDR_REG); |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 1371 | raw_spin_unlock_irqrestore(&iommu->register_lock, flag); |
Suresh Siddha | 0ac2491 | 2009-03-16 17:04:54 -0700 | [diff] [blame] | 1372 | } |
| 1373 | |
| 1374 | static int dmar_fault_do_one(struct intel_iommu *iommu, int type, |
| 1375 | u8 fault_reason, u16 source_id, unsigned long long addr) |
| 1376 | { |
| 1377 | const char *reason; |
Suresh Siddha | 9d783ba | 2009-03-16 17:04:55 -0700 | [diff] [blame] | 1378 | int fault_type; |
Suresh Siddha | 0ac2491 | 2009-03-16 17:04:54 -0700 | [diff] [blame] | 1379 | |
Suresh Siddha | 9d783ba | 2009-03-16 17:04:55 -0700 | [diff] [blame] | 1380 | reason = dmar_get_fault_reason(fault_reason, &fault_type); |
Suresh Siddha | 0ac2491 | 2009-03-16 17:04:54 -0700 | [diff] [blame] | 1381 | |
Suresh Siddha | 9d783ba | 2009-03-16 17:04:55 -0700 | [diff] [blame] | 1382 | if (fault_type == INTR_REMAP) |
Donald Dutile | bf947fcb | 2012-06-04 17:29:01 -0400 | [diff] [blame] | 1383 | pr_err("INTR-REMAP: Request device [[%02x:%02x.%d] " |
Suresh Siddha | 9d783ba | 2009-03-16 17:04:55 -0700 | [diff] [blame] | 1384 | "fault index %llx\n" |
| 1385 | "INTR-REMAP:[fault reason %02d] %s\n", |
| 1386 | (source_id >> 8), PCI_SLOT(source_id & 0xFF), |
| 1387 | PCI_FUNC(source_id & 0xFF), addr >> 48, |
| 1388 | fault_reason, reason); |
| 1389 | else |
Donald Dutile | bf947fcb | 2012-06-04 17:29:01 -0400 | [diff] [blame] | 1390 | pr_err("DMAR:[%s] Request device [%02x:%02x.%d] " |
Suresh Siddha | 9d783ba | 2009-03-16 17:04:55 -0700 | [diff] [blame] | 1391 | "fault addr %llx \n" |
| 1392 | "DMAR:[fault reason %02d] %s\n", |
| 1393 | (type ? "DMA Read" : "DMA Write"), |
| 1394 | (source_id >> 8), PCI_SLOT(source_id & 0xFF), |
| 1395 | PCI_FUNC(source_id & 0xFF), addr, fault_reason, reason); |
Suresh Siddha | 0ac2491 | 2009-03-16 17:04:54 -0700 | [diff] [blame] | 1396 | return 0; |
| 1397 | } |
| 1398 | |
| 1399 | #define PRIMARY_FAULT_REG_LEN (16) |
Suresh Siddha | 1531a6a | 2009-03-16 17:04:57 -0700 | [diff] [blame] | 1400 | irqreturn_t dmar_fault(int irq, void *dev_id) |
Suresh Siddha | 0ac2491 | 2009-03-16 17:04:54 -0700 | [diff] [blame] | 1401 | { |
| 1402 | struct intel_iommu *iommu = dev_id; |
| 1403 | int reg, fault_index; |
| 1404 | u32 fault_status; |
| 1405 | unsigned long flag; |
| 1406 | |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 1407 | raw_spin_lock_irqsave(&iommu->register_lock, flag); |
Suresh Siddha | 0ac2491 | 2009-03-16 17:04:54 -0700 | [diff] [blame] | 1408 | fault_status = readl(iommu->reg + DMAR_FSTS_REG); |
Suresh Siddha | 9d783ba | 2009-03-16 17:04:55 -0700 | [diff] [blame] | 1409 | if (fault_status) |
Donald Dutile | bf947fcb | 2012-06-04 17:29:01 -0400 | [diff] [blame] | 1410 | pr_err("DRHD: handling fault status reg %x\n", fault_status); |
Suresh Siddha | 0ac2491 | 2009-03-16 17:04:54 -0700 | [diff] [blame] | 1411 | |
| 1412 | /* TBD: ignore advanced fault log currently */ |
| 1413 | if (!(fault_status & DMA_FSTS_PPF)) |
Li, Zhen-Hua | bd5cdad | 2013-03-25 16:20:52 +0800 | [diff] [blame] | 1414 | goto unlock_exit; |
Suresh Siddha | 0ac2491 | 2009-03-16 17:04:54 -0700 | [diff] [blame] | 1415 | |
| 1416 | fault_index = dma_fsts_fault_record_index(fault_status); |
| 1417 | reg = cap_fault_reg_offset(iommu->cap); |
| 1418 | while (1) { |
| 1419 | u8 fault_reason; |
| 1420 | u16 source_id; |
| 1421 | u64 guest_addr; |
| 1422 | int type; |
| 1423 | u32 data; |
| 1424 | |
| 1425 | /* highest 32 bits */ |
| 1426 | data = readl(iommu->reg + reg + |
| 1427 | fault_index * PRIMARY_FAULT_REG_LEN + 12); |
| 1428 | if (!(data & DMA_FRCD_F)) |
| 1429 | break; |
| 1430 | |
| 1431 | fault_reason = dma_frcd_fault_reason(data); |
| 1432 | type = dma_frcd_type(data); |
| 1433 | |
| 1434 | data = readl(iommu->reg + reg + |
| 1435 | fault_index * PRIMARY_FAULT_REG_LEN + 8); |
| 1436 | source_id = dma_frcd_source_id(data); |
| 1437 | |
| 1438 | guest_addr = dmar_readq(iommu->reg + reg + |
| 1439 | fault_index * PRIMARY_FAULT_REG_LEN); |
| 1440 | guest_addr = dma_frcd_page_addr(guest_addr); |
| 1441 | /* clear the fault */ |
| 1442 | writel(DMA_FRCD_F, iommu->reg + reg + |
| 1443 | fault_index * PRIMARY_FAULT_REG_LEN + 12); |
| 1444 | |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 1445 | raw_spin_unlock_irqrestore(&iommu->register_lock, flag); |
Suresh Siddha | 0ac2491 | 2009-03-16 17:04:54 -0700 | [diff] [blame] | 1446 | |
| 1447 | dmar_fault_do_one(iommu, type, fault_reason, |
| 1448 | source_id, guest_addr); |
| 1449 | |
| 1450 | fault_index++; |
Troy Heber | 8211a7b | 2009-08-19 15:26:11 -0600 | [diff] [blame] | 1451 | if (fault_index >= cap_num_fault_regs(iommu->cap)) |
Suresh Siddha | 0ac2491 | 2009-03-16 17:04:54 -0700 | [diff] [blame] | 1452 | fault_index = 0; |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 1453 | raw_spin_lock_irqsave(&iommu->register_lock, flag); |
Suresh Siddha | 0ac2491 | 2009-03-16 17:04:54 -0700 | [diff] [blame] | 1454 | } |
Suresh Siddha | 0ac2491 | 2009-03-16 17:04:54 -0700 | [diff] [blame] | 1455 | |
Li, Zhen-Hua | bd5cdad | 2013-03-25 16:20:52 +0800 | [diff] [blame] | 1456 | writel(DMA_FSTS_PFO | DMA_FSTS_PPF, iommu->reg + DMAR_FSTS_REG); |
| 1457 | |
| 1458 | unlock_exit: |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 1459 | raw_spin_unlock_irqrestore(&iommu->register_lock, flag); |
Suresh Siddha | 0ac2491 | 2009-03-16 17:04:54 -0700 | [diff] [blame] | 1460 | return IRQ_HANDLED; |
| 1461 | } |
| 1462 | |
| 1463 | int dmar_set_interrupt(struct intel_iommu *iommu) |
| 1464 | { |
| 1465 | int irq, ret; |
| 1466 | |
Suresh Siddha | 9d783ba | 2009-03-16 17:04:55 -0700 | [diff] [blame] | 1467 | /* |
| 1468 | * Check if the fault interrupt is already initialized. |
| 1469 | */ |
| 1470 | if (iommu->irq) |
| 1471 | return 0; |
| 1472 | |
Suresh Siddha | 0ac2491 | 2009-03-16 17:04:54 -0700 | [diff] [blame] | 1473 | irq = create_irq(); |
| 1474 | if (!irq) { |
Donald Dutile | bf947fcb | 2012-06-04 17:29:01 -0400 | [diff] [blame] | 1475 | pr_err("IOMMU: no free vectors\n"); |
Suresh Siddha | 0ac2491 | 2009-03-16 17:04:54 -0700 | [diff] [blame] | 1476 | return -EINVAL; |
| 1477 | } |
| 1478 | |
Thomas Gleixner | dced35a | 2011-03-28 17:49:12 +0200 | [diff] [blame] | 1479 | irq_set_handler_data(irq, iommu); |
Suresh Siddha | 0ac2491 | 2009-03-16 17:04:54 -0700 | [diff] [blame] | 1480 | iommu->irq = irq; |
| 1481 | |
| 1482 | ret = arch_setup_dmar_msi(irq); |
| 1483 | if (ret) { |
Thomas Gleixner | dced35a | 2011-03-28 17:49:12 +0200 | [diff] [blame] | 1484 | irq_set_handler_data(irq, NULL); |
Suresh Siddha | 0ac2491 | 2009-03-16 17:04:54 -0700 | [diff] [blame] | 1485 | iommu->irq = 0; |
| 1486 | destroy_irq(irq); |
Chris Wright | dd72643 | 2009-05-13 15:55:52 -0700 | [diff] [blame] | 1487 | return ret; |
Suresh Siddha | 0ac2491 | 2009-03-16 17:04:54 -0700 | [diff] [blame] | 1488 | } |
| 1489 | |
Thomas Gleixner | 477694e | 2011-07-19 16:25:42 +0200 | [diff] [blame] | 1490 | ret = request_irq(irq, dmar_fault, IRQF_NO_THREAD, iommu->name, iommu); |
Suresh Siddha | 0ac2491 | 2009-03-16 17:04:54 -0700 | [diff] [blame] | 1491 | if (ret) |
Donald Dutile | bf947fcb | 2012-06-04 17:29:01 -0400 | [diff] [blame] | 1492 | pr_err("IOMMU: can't request irq\n"); |
Suresh Siddha | 0ac2491 | 2009-03-16 17:04:54 -0700 | [diff] [blame] | 1493 | return ret; |
| 1494 | } |
Suresh Siddha | 9d783ba | 2009-03-16 17:04:55 -0700 | [diff] [blame] | 1495 | |
| 1496 | int __init enable_drhd_fault_handling(void) |
| 1497 | { |
| 1498 | struct dmar_drhd_unit *drhd; |
Jiang Liu | 7c91977 | 2014-01-06 14:18:18 +0800 | [diff] [blame] | 1499 | struct intel_iommu *iommu; |
Suresh Siddha | 9d783ba | 2009-03-16 17:04:55 -0700 | [diff] [blame] | 1500 | |
| 1501 | /* |
| 1502 | * Enable fault control interrupt. |
| 1503 | */ |
Jiang Liu | 7c91977 | 2014-01-06 14:18:18 +0800 | [diff] [blame] | 1504 | for_each_iommu(iommu, drhd) { |
Li, Zhen-Hua | bd5cdad | 2013-03-25 16:20:52 +0800 | [diff] [blame] | 1505 | u32 fault_status; |
Jiang Liu | 7c91977 | 2014-01-06 14:18:18 +0800 | [diff] [blame] | 1506 | int ret = dmar_set_interrupt(iommu); |
Suresh Siddha | 9d783ba | 2009-03-16 17:04:55 -0700 | [diff] [blame] | 1507 | |
| 1508 | if (ret) { |
Donald Dutile | e9071b0 | 2012-06-08 17:13:11 -0400 | [diff] [blame] | 1509 | pr_err("DRHD %Lx: failed to enable fault, interrupt, ret %d\n", |
Suresh Siddha | 9d783ba | 2009-03-16 17:04:55 -0700 | [diff] [blame] | 1510 | (unsigned long long)drhd->reg_base_addr, ret); |
| 1511 | return -1; |
| 1512 | } |
Suresh Siddha | 7f99d94 | 2010-11-30 22:22:29 -0800 | [diff] [blame] | 1513 | |
| 1514 | /* |
| 1515 | * Clear any previous faults. |
| 1516 | */ |
| 1517 | dmar_fault(iommu->irq, iommu); |
Li, Zhen-Hua | bd5cdad | 2013-03-25 16:20:52 +0800 | [diff] [blame] | 1518 | fault_status = readl(iommu->reg + DMAR_FSTS_REG); |
| 1519 | writel(fault_status, iommu->reg + DMAR_FSTS_REG); |
Suresh Siddha | 9d783ba | 2009-03-16 17:04:55 -0700 | [diff] [blame] | 1520 | } |
| 1521 | |
| 1522 | return 0; |
| 1523 | } |
Fenghua Yu | eb4a52b | 2009-03-27 14:22:43 -0700 | [diff] [blame] | 1524 | |
| 1525 | /* |
| 1526 | * Re-enable Queued Invalidation interface. |
| 1527 | */ |
| 1528 | int dmar_reenable_qi(struct intel_iommu *iommu) |
| 1529 | { |
| 1530 | if (!ecap_qis(iommu->ecap)) |
| 1531 | return -ENOENT; |
| 1532 | |
| 1533 | if (!iommu->qi) |
| 1534 | return -ENOENT; |
| 1535 | |
| 1536 | /* |
| 1537 | * First disable queued invalidation. |
| 1538 | */ |
| 1539 | dmar_disable_qi(iommu); |
| 1540 | /* |
| 1541 | * Then enable queued invalidation again. Since there is no pending |
| 1542 | * invalidation requests now, it's safe to re-enable queued |
| 1543 | * invalidation. |
| 1544 | */ |
| 1545 | __dmar_enable_qi(iommu); |
| 1546 | |
| 1547 | return 0; |
| 1548 | } |
Youquan Song | 074835f | 2009-09-09 12:05:39 -0400 | [diff] [blame] | 1549 | |
| 1550 | /* |
| 1551 | * Check interrupt remapping support in DMAR table description. |
| 1552 | */ |
Luck, Tony | 0b8973a | 2009-12-16 22:59:29 +0000 | [diff] [blame] | 1553 | int __init dmar_ir_support(void) |
Youquan Song | 074835f | 2009-09-09 12:05:39 -0400 | [diff] [blame] | 1554 | { |
| 1555 | struct acpi_table_dmar *dmar; |
| 1556 | dmar = (struct acpi_table_dmar *)dmar_tbl; |
Arnaud Patard | 4f506e0 | 2010-03-25 18:02:58 +0000 | [diff] [blame] | 1557 | if (!dmar) |
| 1558 | return 0; |
Youquan Song | 074835f | 2009-09-09 12:05:39 -0400 | [diff] [blame] | 1559 | return dmar->flags & 0x1; |
| 1560 | } |
Jiang Liu | 694835d | 2014-01-06 14:18:16 +0800 | [diff] [blame] | 1561 | |
Jiang Liu | a868e6b | 2014-01-06 14:18:20 +0800 | [diff] [blame] | 1562 | static int __init dmar_free_unused_resources(void) |
| 1563 | { |
| 1564 | struct dmar_drhd_unit *dmaru, *dmaru_n; |
| 1565 | |
| 1566 | /* DMAR units are in use */ |
| 1567 | if (irq_remapping_enabled || intel_iommu_enabled) |
| 1568 | return 0; |
| 1569 | |
Jiang Liu | 2e45528 | 2014-02-19 14:07:36 +0800 | [diff] [blame] | 1570 | if (dmar_dev_scope_status != 1 && !list_empty(&dmar_drhd_units)) |
| 1571 | bus_unregister_notifier(&pci_bus_type, &dmar_pci_bus_nb); |
Jiang Liu | 59ce051 | 2014-02-19 14:07:35 +0800 | [diff] [blame] | 1572 | |
Jiang Liu | 3a5670e | 2014-02-19 14:07:33 +0800 | [diff] [blame] | 1573 | down_write(&dmar_global_lock); |
Jiang Liu | a868e6b | 2014-01-06 14:18:20 +0800 | [diff] [blame] | 1574 | list_for_each_entry_safe(dmaru, dmaru_n, &dmar_drhd_units, list) { |
| 1575 | list_del(&dmaru->list); |
| 1576 | dmar_free_drhd(dmaru); |
| 1577 | } |
Jiang Liu | 3a5670e | 2014-02-19 14:07:33 +0800 | [diff] [blame] | 1578 | up_write(&dmar_global_lock); |
Jiang Liu | a868e6b | 2014-01-06 14:18:20 +0800 | [diff] [blame] | 1579 | |
| 1580 | return 0; |
| 1581 | } |
| 1582 | |
| 1583 | late_initcall(dmar_free_unused_resources); |
Konrad Rzeszutek Wilk | 4db77ff | 2010-08-26 13:58:04 -0400 | [diff] [blame] | 1584 | IOMMU_INIT_POST(detect_intel_iommu); |