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