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 | |
| 29 | #include <linux/pci.h> |
| 30 | #include <linux/dmar.h> |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 31 | #include <linux/iova.h> |
| 32 | #include <linux/intel-iommu.h> |
Suresh Siddha | fe962e9 | 2008-07-10 11:16:42 -0700 | [diff] [blame] | 33 | #include <linux/timer.h> |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 34 | |
| 35 | #undef PREFIX |
| 36 | #define PREFIX "DMAR:" |
| 37 | |
| 38 | /* No locks are needed as DMA remapping hardware unit |
| 39 | * list is constructed at boot time and hotplug of |
| 40 | * these units are not supported by the architecture. |
| 41 | */ |
| 42 | LIST_HEAD(dmar_drhd_units); |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 43 | |
| 44 | static struct acpi_table_header * __initdata dmar_tbl; |
| 45 | |
| 46 | static void __init dmar_register_drhd_unit(struct dmar_drhd_unit *drhd) |
| 47 | { |
| 48 | /* |
| 49 | * add INCLUDE_ALL at the tail, so scan the list will find it at |
| 50 | * the very end. |
| 51 | */ |
| 52 | if (drhd->include_all) |
| 53 | list_add_tail(&drhd->list, &dmar_drhd_units); |
| 54 | else |
| 55 | list_add(&drhd->list, &dmar_drhd_units); |
| 56 | } |
| 57 | |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 58 | static int __init dmar_parse_one_dev_scope(struct acpi_dmar_device_scope *scope, |
| 59 | struct pci_dev **dev, u16 segment) |
| 60 | { |
| 61 | struct pci_bus *bus; |
| 62 | struct pci_dev *pdev = NULL; |
| 63 | struct acpi_dmar_pci_path *path; |
| 64 | int count; |
| 65 | |
| 66 | bus = pci_find_bus(segment, scope->bus); |
| 67 | path = (struct acpi_dmar_pci_path *)(scope + 1); |
| 68 | count = (scope->length - sizeof(struct acpi_dmar_device_scope)) |
| 69 | / sizeof(struct acpi_dmar_pci_path); |
| 70 | |
| 71 | while (count) { |
| 72 | if (pdev) |
| 73 | pci_dev_put(pdev); |
| 74 | /* |
| 75 | * Some BIOSes list non-exist devices in DMAR table, just |
| 76 | * ignore it |
| 77 | */ |
| 78 | if (!bus) { |
| 79 | printk(KERN_WARNING |
| 80 | PREFIX "Device scope bus [%d] not found\n", |
| 81 | scope->bus); |
| 82 | break; |
| 83 | } |
| 84 | pdev = pci_get_slot(bus, PCI_DEVFN(path->dev, path->fn)); |
| 85 | if (!pdev) { |
| 86 | printk(KERN_WARNING PREFIX |
| 87 | "Device scope device [%04x:%02x:%02x.%02x] not found\n", |
| 88 | segment, bus->number, path->dev, path->fn); |
| 89 | break; |
| 90 | } |
| 91 | path ++; |
| 92 | count --; |
| 93 | bus = pdev->subordinate; |
| 94 | } |
| 95 | if (!pdev) { |
| 96 | printk(KERN_WARNING PREFIX |
| 97 | "Device scope device [%04x:%02x:%02x.%02x] not found\n", |
| 98 | segment, scope->bus, path->dev, path->fn); |
| 99 | *dev = NULL; |
| 100 | return 0; |
| 101 | } |
| 102 | if ((scope->entry_type == ACPI_DMAR_SCOPE_TYPE_ENDPOINT && \ |
| 103 | pdev->subordinate) || (scope->entry_type == \ |
| 104 | ACPI_DMAR_SCOPE_TYPE_BRIDGE && !pdev->subordinate)) { |
| 105 | pci_dev_put(pdev); |
| 106 | printk(KERN_WARNING PREFIX |
| 107 | "Device scope type does not match for %s\n", |
| 108 | pci_name(pdev)); |
| 109 | return -EINVAL; |
| 110 | } |
| 111 | *dev = pdev; |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | static int __init dmar_parse_dev_scope(void *start, void *end, int *cnt, |
| 116 | struct pci_dev ***devices, u16 segment) |
| 117 | { |
| 118 | struct acpi_dmar_device_scope *scope; |
| 119 | void * tmp = start; |
| 120 | int index; |
| 121 | int ret; |
| 122 | |
| 123 | *cnt = 0; |
| 124 | while (start < end) { |
| 125 | scope = start; |
| 126 | if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_ENDPOINT || |
| 127 | scope->entry_type == ACPI_DMAR_SCOPE_TYPE_BRIDGE) |
| 128 | (*cnt)++; |
| 129 | else |
| 130 | printk(KERN_WARNING PREFIX |
| 131 | "Unsupported device scope\n"); |
| 132 | start += scope->length; |
| 133 | } |
| 134 | if (*cnt == 0) |
| 135 | return 0; |
| 136 | |
| 137 | *devices = kcalloc(*cnt, sizeof(struct pci_dev *), GFP_KERNEL); |
| 138 | if (!*devices) |
| 139 | return -ENOMEM; |
| 140 | |
| 141 | start = tmp; |
| 142 | index = 0; |
| 143 | while (start < end) { |
| 144 | scope = start; |
| 145 | if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_ENDPOINT || |
| 146 | scope->entry_type == ACPI_DMAR_SCOPE_TYPE_BRIDGE) { |
| 147 | ret = dmar_parse_one_dev_scope(scope, |
| 148 | &(*devices)[index], segment); |
| 149 | if (ret) { |
| 150 | kfree(*devices); |
| 151 | return ret; |
| 152 | } |
| 153 | index ++; |
| 154 | } |
| 155 | start += scope->length; |
| 156 | } |
| 157 | |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * dmar_parse_one_drhd - parses exactly one DMA remapping hardware definition |
| 163 | * structure which uniquely represent one DMA remapping hardware unit |
| 164 | * present in the platform |
| 165 | */ |
| 166 | static int __init |
| 167 | dmar_parse_one_drhd(struct acpi_dmar_header *header) |
| 168 | { |
| 169 | struct acpi_dmar_hardware_unit *drhd; |
| 170 | struct dmar_drhd_unit *dmaru; |
| 171 | int ret = 0; |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 172 | |
| 173 | dmaru = kzalloc(sizeof(*dmaru), GFP_KERNEL); |
| 174 | if (!dmaru) |
| 175 | return -ENOMEM; |
| 176 | |
Suresh Siddha | 1886e8a | 2008-07-10 11:16:37 -0700 | [diff] [blame] | 177 | dmaru->hdr = header; |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 178 | drhd = (struct acpi_dmar_hardware_unit *)header; |
| 179 | dmaru->reg_base_addr = drhd->address; |
| 180 | dmaru->include_all = drhd->flags & 0x1; /* BIT0: INCLUDE_ALL */ |
| 181 | |
Suresh Siddha | 1886e8a | 2008-07-10 11:16:37 -0700 | [diff] [blame] | 182 | ret = alloc_iommu(dmaru); |
| 183 | if (ret) { |
| 184 | kfree(dmaru); |
| 185 | return ret; |
| 186 | } |
| 187 | dmar_register_drhd_unit(dmaru); |
| 188 | return 0; |
| 189 | } |
| 190 | |
David Woodhouse | f82851a | 2008-10-18 15:43:14 +0100 | [diff] [blame^] | 191 | static int __init dmar_parse_dev(struct dmar_drhd_unit *dmaru) |
Suresh Siddha | 1886e8a | 2008-07-10 11:16:37 -0700 | [diff] [blame] | 192 | { |
| 193 | struct acpi_dmar_hardware_unit *drhd; |
| 194 | static int include_all; |
David Woodhouse | f82851a | 2008-10-18 15:43:14 +0100 | [diff] [blame^] | 195 | int ret = 0; |
Suresh Siddha | 1886e8a | 2008-07-10 11:16:37 -0700 | [diff] [blame] | 196 | |
| 197 | drhd = (struct acpi_dmar_hardware_unit *) dmaru->hdr; |
| 198 | |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 199 | if (!dmaru->include_all) |
| 200 | ret = dmar_parse_dev_scope((void *)(drhd + 1), |
Suresh Siddha | 1886e8a | 2008-07-10 11:16:37 -0700 | [diff] [blame] | 201 | ((void *)drhd) + drhd->header.length, |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 202 | &dmaru->devices_cnt, &dmaru->devices, |
| 203 | drhd->segment); |
| 204 | else { |
| 205 | /* Only allow one INCLUDE_ALL */ |
| 206 | if (include_all) { |
| 207 | printk(KERN_WARNING PREFIX "Only one INCLUDE_ALL " |
| 208 | "device scope is allowed\n"); |
| 209 | ret = -EINVAL; |
| 210 | } |
| 211 | include_all = 1; |
| 212 | } |
| 213 | |
Suresh Siddha | 1886e8a | 2008-07-10 11:16:37 -0700 | [diff] [blame] | 214 | if (ret || (dmaru->devices_cnt == 0 && !dmaru->include_all)) { |
| 215 | list_del(&dmaru->list); |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 216 | kfree(dmaru); |
Suresh Siddha | 1886e8a | 2008-07-10 11:16:37 -0700 | [diff] [blame] | 217 | } |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 218 | return ret; |
| 219 | } |
| 220 | |
Suresh Siddha | aaa9d1d | 2008-07-10 11:16:38 -0700 | [diff] [blame] | 221 | #ifdef CONFIG_DMAR |
| 222 | LIST_HEAD(dmar_rmrr_units); |
| 223 | |
| 224 | static void __init dmar_register_rmrr_unit(struct dmar_rmrr_unit *rmrr) |
| 225 | { |
| 226 | list_add(&rmrr->list, &dmar_rmrr_units); |
| 227 | } |
| 228 | |
| 229 | |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 230 | static int __init |
| 231 | dmar_parse_one_rmrr(struct acpi_dmar_header *header) |
| 232 | { |
| 233 | struct acpi_dmar_reserved_memory *rmrr; |
| 234 | struct dmar_rmrr_unit *rmrru; |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 235 | |
| 236 | rmrru = kzalloc(sizeof(*rmrru), GFP_KERNEL); |
| 237 | if (!rmrru) |
| 238 | return -ENOMEM; |
| 239 | |
Suresh Siddha | 1886e8a | 2008-07-10 11:16:37 -0700 | [diff] [blame] | 240 | rmrru->hdr = header; |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 241 | rmrr = (struct acpi_dmar_reserved_memory *)header; |
| 242 | rmrru->base_address = rmrr->base_address; |
| 243 | rmrru->end_address = rmrr->end_address; |
Suresh Siddha | 1886e8a | 2008-07-10 11:16:37 -0700 | [diff] [blame] | 244 | |
| 245 | dmar_register_rmrr_unit(rmrru); |
| 246 | return 0; |
| 247 | } |
| 248 | |
| 249 | static int __init |
| 250 | rmrr_parse_dev(struct dmar_rmrr_unit *rmrru) |
| 251 | { |
| 252 | struct acpi_dmar_reserved_memory *rmrr; |
| 253 | int ret; |
| 254 | |
| 255 | rmrr = (struct acpi_dmar_reserved_memory *) rmrru->hdr; |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 256 | ret = dmar_parse_dev_scope((void *)(rmrr + 1), |
Suresh Siddha | 1886e8a | 2008-07-10 11:16:37 -0700 | [diff] [blame] | 257 | ((void *)rmrr) + rmrr->header.length, |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 258 | &rmrru->devices_cnt, &rmrru->devices, rmrr->segment); |
| 259 | |
Suresh Siddha | 1886e8a | 2008-07-10 11:16:37 -0700 | [diff] [blame] | 260 | if (ret || (rmrru->devices_cnt == 0)) { |
| 261 | list_del(&rmrru->list); |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 262 | kfree(rmrru); |
Suresh Siddha | 1886e8a | 2008-07-10 11:16:37 -0700 | [diff] [blame] | 263 | } |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 264 | return ret; |
| 265 | } |
Suresh Siddha | aaa9d1d | 2008-07-10 11:16:38 -0700 | [diff] [blame] | 266 | #endif |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 267 | |
| 268 | static void __init |
| 269 | dmar_table_print_dmar_entry(struct acpi_dmar_header *header) |
| 270 | { |
| 271 | struct acpi_dmar_hardware_unit *drhd; |
| 272 | struct acpi_dmar_reserved_memory *rmrr; |
| 273 | |
| 274 | switch (header->type) { |
| 275 | case ACPI_DMAR_TYPE_HARDWARE_UNIT: |
| 276 | drhd = (struct acpi_dmar_hardware_unit *)header; |
| 277 | printk (KERN_INFO PREFIX |
| 278 | "DRHD (flags: 0x%08x)base: 0x%016Lx\n", |
Fenghua Yu | 5b6985c | 2008-10-16 18:02:32 -0700 | [diff] [blame] | 279 | drhd->flags, (unsigned long long)drhd->address); |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 280 | break; |
| 281 | case ACPI_DMAR_TYPE_RESERVED_MEMORY: |
| 282 | rmrr = (struct acpi_dmar_reserved_memory *)header; |
| 283 | |
| 284 | printk (KERN_INFO PREFIX |
| 285 | "RMRR base: 0x%016Lx end: 0x%016Lx\n", |
Fenghua Yu | 5b6985c | 2008-10-16 18:02:32 -0700 | [diff] [blame] | 286 | (unsigned long long)rmrr->base_address, |
| 287 | (unsigned long long)rmrr->end_address); |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 288 | break; |
| 289 | } |
| 290 | } |
| 291 | |
Suresh Siddha | aaa9d1d | 2008-07-10 11:16:38 -0700 | [diff] [blame] | 292 | |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 293 | /** |
| 294 | * parse_dmar_table - parses the DMA reporting table |
| 295 | */ |
| 296 | static int __init |
| 297 | parse_dmar_table(void) |
| 298 | { |
| 299 | struct acpi_table_dmar *dmar; |
| 300 | struct acpi_dmar_header *entry_header; |
| 301 | int ret = 0; |
| 302 | |
| 303 | dmar = (struct acpi_table_dmar *)dmar_tbl; |
| 304 | if (!dmar) |
| 305 | return -ENODEV; |
| 306 | |
Fenghua Yu | 5b6985c | 2008-10-16 18:02:32 -0700 | [diff] [blame] | 307 | if (dmar->width < PAGE_SHIFT - 1) { |
Fenghua Yu | 093f87d | 2007-11-21 15:07:14 -0800 | [diff] [blame] | 308 | printk(KERN_WARNING PREFIX "Invalid DMAR haw\n"); |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 309 | return -EINVAL; |
| 310 | } |
| 311 | |
| 312 | printk (KERN_INFO PREFIX "Host address width %d\n", |
| 313 | dmar->width + 1); |
| 314 | |
| 315 | entry_header = (struct acpi_dmar_header *)(dmar + 1); |
| 316 | while (((unsigned long)entry_header) < |
| 317 | (((unsigned long)dmar) + dmar_tbl->length)) { |
| 318 | dmar_table_print_dmar_entry(entry_header); |
| 319 | |
| 320 | switch (entry_header->type) { |
| 321 | case ACPI_DMAR_TYPE_HARDWARE_UNIT: |
| 322 | ret = dmar_parse_one_drhd(entry_header); |
| 323 | break; |
| 324 | case ACPI_DMAR_TYPE_RESERVED_MEMORY: |
Suresh Siddha | aaa9d1d | 2008-07-10 11:16:38 -0700 | [diff] [blame] | 325 | #ifdef CONFIG_DMAR |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 326 | ret = dmar_parse_one_rmrr(entry_header); |
Suresh Siddha | aaa9d1d | 2008-07-10 11:16:38 -0700 | [diff] [blame] | 327 | #endif |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 328 | break; |
| 329 | default: |
| 330 | printk(KERN_WARNING PREFIX |
| 331 | "Unknown DMAR structure type\n"); |
| 332 | ret = 0; /* for forward compatibility */ |
| 333 | break; |
| 334 | } |
| 335 | if (ret) |
| 336 | break; |
| 337 | |
| 338 | entry_header = ((void *)entry_header + entry_header->length); |
| 339 | } |
| 340 | return ret; |
| 341 | } |
| 342 | |
Suresh Siddha | e61d98d | 2008-07-10 11:16:35 -0700 | [diff] [blame] | 343 | int dmar_pci_device_match(struct pci_dev *devices[], int cnt, |
| 344 | struct pci_dev *dev) |
| 345 | { |
| 346 | int index; |
| 347 | |
| 348 | while (dev) { |
| 349 | for (index = 0; index < cnt; index++) |
| 350 | if (dev == devices[index]) |
| 351 | return 1; |
| 352 | |
| 353 | /* Check our parent */ |
| 354 | dev = dev->bus->self; |
| 355 | } |
| 356 | |
| 357 | return 0; |
| 358 | } |
| 359 | |
| 360 | struct dmar_drhd_unit * |
| 361 | dmar_find_matched_drhd_unit(struct pci_dev *dev) |
| 362 | { |
| 363 | struct dmar_drhd_unit *drhd = NULL; |
| 364 | |
| 365 | list_for_each_entry(drhd, &dmar_drhd_units, list) { |
| 366 | if (drhd->include_all || dmar_pci_device_match(drhd->devices, |
| 367 | drhd->devices_cnt, dev)) |
| 368 | return drhd; |
| 369 | } |
| 370 | |
| 371 | return NULL; |
| 372 | } |
| 373 | |
Suresh Siddha | 1886e8a | 2008-07-10 11:16:37 -0700 | [diff] [blame] | 374 | int __init dmar_dev_scope_init(void) |
| 375 | { |
| 376 | struct dmar_drhd_unit *drhd; |
Suresh Siddha | 1886e8a | 2008-07-10 11:16:37 -0700 | [diff] [blame] | 377 | int ret = -ENODEV; |
| 378 | |
| 379 | for_each_drhd_unit(drhd) { |
| 380 | ret = dmar_parse_dev(drhd); |
| 381 | if (ret) |
| 382 | return ret; |
| 383 | } |
| 384 | |
Suresh Siddha | aaa9d1d | 2008-07-10 11:16:38 -0700 | [diff] [blame] | 385 | #ifdef CONFIG_DMAR |
| 386 | { |
| 387 | struct dmar_rmrr_unit *rmrr; |
| 388 | for_each_rmrr_units(rmrr) { |
| 389 | ret = rmrr_parse_dev(rmrr); |
| 390 | if (ret) |
| 391 | return ret; |
| 392 | } |
Suresh Siddha | 1886e8a | 2008-07-10 11:16:37 -0700 | [diff] [blame] | 393 | } |
Suresh Siddha | aaa9d1d | 2008-07-10 11:16:38 -0700 | [diff] [blame] | 394 | #endif |
Suresh Siddha | 1886e8a | 2008-07-10 11:16:37 -0700 | [diff] [blame] | 395 | |
| 396 | return ret; |
| 397 | } |
| 398 | |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 399 | |
| 400 | int __init dmar_table_init(void) |
| 401 | { |
Suresh Siddha | 1886e8a | 2008-07-10 11:16:37 -0700 | [diff] [blame] | 402 | static int dmar_table_initialized; |
Fenghua Yu | 093f87d | 2007-11-21 15:07:14 -0800 | [diff] [blame] | 403 | int ret; |
| 404 | |
Suresh Siddha | 1886e8a | 2008-07-10 11:16:37 -0700 | [diff] [blame] | 405 | if (dmar_table_initialized) |
| 406 | return 0; |
| 407 | |
| 408 | dmar_table_initialized = 1; |
| 409 | |
Fenghua Yu | 093f87d | 2007-11-21 15:07:14 -0800 | [diff] [blame] | 410 | ret = parse_dmar_table(); |
| 411 | if (ret) { |
Suresh Siddha | 1886e8a | 2008-07-10 11:16:37 -0700 | [diff] [blame] | 412 | if (ret != -ENODEV) |
| 413 | printk(KERN_INFO PREFIX "parse DMAR table failure.\n"); |
Fenghua Yu | 093f87d | 2007-11-21 15:07:14 -0800 | [diff] [blame] | 414 | return ret; |
| 415 | } |
| 416 | |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 417 | if (list_empty(&dmar_drhd_units)) { |
| 418 | printk(KERN_INFO PREFIX "No DMAR devices found\n"); |
| 419 | return -ENODEV; |
| 420 | } |
Fenghua Yu | 093f87d | 2007-11-21 15:07:14 -0800 | [diff] [blame] | 421 | |
Suresh Siddha | aaa9d1d | 2008-07-10 11:16:38 -0700 | [diff] [blame] | 422 | #ifdef CONFIG_DMAR |
Suresh Siddha | 2d6b5f8 | 2008-07-10 11:16:39 -0700 | [diff] [blame] | 423 | if (list_empty(&dmar_rmrr_units)) |
Fenghua Yu | 093f87d | 2007-11-21 15:07:14 -0800 | [diff] [blame] | 424 | printk(KERN_INFO PREFIX "No RMRR found\n"); |
Suresh Siddha | aaa9d1d | 2008-07-10 11:16:38 -0700 | [diff] [blame] | 425 | #endif |
Fenghua Yu | 093f87d | 2007-11-21 15:07:14 -0800 | [diff] [blame] | 426 | |
Suresh Siddha | ad3ad3f | 2008-07-10 11:16:40 -0700 | [diff] [blame] | 427 | #ifdef CONFIG_INTR_REMAP |
| 428 | parse_ioapics_under_ir(); |
| 429 | #endif |
Keshavamurthy, Anil S | 10e5247 | 2007-10-21 16:41:41 -0700 | [diff] [blame] | 430 | return 0; |
| 431 | } |
| 432 | |
| 433 | /** |
| 434 | * early_dmar_detect - checks to see if the platform supports DMAR devices |
| 435 | */ |
| 436 | int __init early_dmar_detect(void) |
| 437 | { |
| 438 | acpi_status status = AE_OK; |
| 439 | |
| 440 | /* if we could find DMAR table, then there are DMAR devices */ |
| 441 | status = acpi_get_table(ACPI_SIG_DMAR, 0, |
| 442 | (struct acpi_table_header **)&dmar_tbl); |
| 443 | |
| 444 | if (ACPI_SUCCESS(status) && !dmar_tbl) { |
| 445 | printk (KERN_WARNING PREFIX "Unable to map DMAR\n"); |
| 446 | status = AE_NOT_FOUND; |
| 447 | } |
| 448 | |
| 449 | return (ACPI_SUCCESS(status) ? 1 : 0); |
| 450 | } |
Suresh Siddha | e61d98d | 2008-07-10 11:16:35 -0700 | [diff] [blame] | 451 | |
Suresh Siddha | 2ae2101 | 2008-07-10 11:16:43 -0700 | [diff] [blame] | 452 | void __init detect_intel_iommu(void) |
| 453 | { |
| 454 | int ret; |
| 455 | |
| 456 | ret = early_dmar_detect(); |
| 457 | |
Suresh Siddha | 2ae2101 | 2008-07-10 11:16:43 -0700 | [diff] [blame] | 458 | { |
Youquan Song | cacd421 | 2008-10-16 16:31:57 -0700 | [diff] [blame] | 459 | #ifdef CONFIG_INTR_REMAP |
Suresh Siddha | 1cb1158 | 2008-07-10 11:16:51 -0700 | [diff] [blame] | 460 | struct acpi_table_dmar *dmar; |
| 461 | /* |
| 462 | * for now we will disable dma-remapping when interrupt |
| 463 | * remapping is enabled. |
| 464 | * When support for queued invalidation for IOTLB invalidation |
| 465 | * is added, we will not need this any more. |
| 466 | */ |
| 467 | dmar = (struct acpi_table_dmar *) dmar_tbl; |
Youquan Song | cacd421 | 2008-10-16 16:31:57 -0700 | [diff] [blame] | 468 | if (ret && cpu_has_x2apic && dmar->flags & 0x1) |
Suresh Siddha | 1cb1158 | 2008-07-10 11:16:51 -0700 | [diff] [blame] | 469 | printk(KERN_INFO |
| 470 | "Queued invalidation will be enabled to support " |
| 471 | "x2apic and Intr-remapping.\n"); |
Youquan Song | cacd421 | 2008-10-16 16:31:57 -0700 | [diff] [blame] | 472 | #endif |
Suresh Siddha | 1cb1158 | 2008-07-10 11:16:51 -0700 | [diff] [blame] | 473 | |
Youquan Song | cacd421 | 2008-10-16 16:31:57 -0700 | [diff] [blame] | 474 | #ifdef CONFIG_DMAR |
Suresh Siddha | 2ae2101 | 2008-07-10 11:16:43 -0700 | [diff] [blame] | 475 | if (ret && !no_iommu && !iommu_detected && !swiotlb && |
| 476 | !dmar_disabled) |
| 477 | iommu_detected = 1; |
Suresh Siddha | 2ae2101 | 2008-07-10 11:16:43 -0700 | [diff] [blame] | 478 | #endif |
Youquan Song | cacd421 | 2008-10-16 16:31:57 -0700 | [diff] [blame] | 479 | } |
Suresh Siddha | 2ae2101 | 2008-07-10 11:16:43 -0700 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | |
Suresh Siddha | 1886e8a | 2008-07-10 11:16:37 -0700 | [diff] [blame] | 483 | int alloc_iommu(struct dmar_drhd_unit *drhd) |
Suresh Siddha | e61d98d | 2008-07-10 11:16:35 -0700 | [diff] [blame] | 484 | { |
Suresh Siddha | c42d9f3 | 2008-07-10 11:16:36 -0700 | [diff] [blame] | 485 | struct intel_iommu *iommu; |
Suresh Siddha | e61d98d | 2008-07-10 11:16:35 -0700 | [diff] [blame] | 486 | int map_size; |
| 487 | u32 ver; |
Suresh Siddha | c42d9f3 | 2008-07-10 11:16:36 -0700 | [diff] [blame] | 488 | static int iommu_allocated = 0; |
| 489 | |
| 490 | iommu = kzalloc(sizeof(*iommu), GFP_KERNEL); |
| 491 | if (!iommu) |
Suresh Siddha | 1886e8a | 2008-07-10 11:16:37 -0700 | [diff] [blame] | 492 | return -ENOMEM; |
Suresh Siddha | c42d9f3 | 2008-07-10 11:16:36 -0700 | [diff] [blame] | 493 | |
| 494 | iommu->seq_id = iommu_allocated++; |
Suresh Siddha | e61d98d | 2008-07-10 11:16:35 -0700 | [diff] [blame] | 495 | |
Fenghua Yu | 5b6985c | 2008-10-16 18:02:32 -0700 | [diff] [blame] | 496 | iommu->reg = ioremap(drhd->reg_base_addr, VTD_PAGE_SIZE); |
Suresh Siddha | e61d98d | 2008-07-10 11:16:35 -0700 | [diff] [blame] | 497 | if (!iommu->reg) { |
| 498 | printk(KERN_ERR "IOMMU: can't map the region\n"); |
| 499 | goto error; |
| 500 | } |
| 501 | iommu->cap = dmar_readq(iommu->reg + DMAR_CAP_REG); |
| 502 | iommu->ecap = dmar_readq(iommu->reg + DMAR_ECAP_REG); |
| 503 | |
| 504 | /* the registers might be more than one page */ |
| 505 | map_size = max_t(int, ecap_max_iotlb_offset(iommu->ecap), |
| 506 | cap_max_fault_reg_offset(iommu->cap)); |
Fenghua Yu | 5b6985c | 2008-10-16 18:02:32 -0700 | [diff] [blame] | 507 | map_size = VTD_PAGE_ALIGN(map_size); |
| 508 | if (map_size > VTD_PAGE_SIZE) { |
Suresh Siddha | e61d98d | 2008-07-10 11:16:35 -0700 | [diff] [blame] | 509 | iounmap(iommu->reg); |
| 510 | iommu->reg = ioremap(drhd->reg_base_addr, map_size); |
| 511 | if (!iommu->reg) { |
| 512 | printk(KERN_ERR "IOMMU: can't map the region\n"); |
| 513 | goto error; |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | ver = readl(iommu->reg + DMAR_VER_REG); |
| 518 | pr_debug("IOMMU %llx: ver %d:%d cap %llx ecap %llx\n", |
Fenghua Yu | 5b6985c | 2008-10-16 18:02:32 -0700 | [diff] [blame] | 519 | (unsigned long long)drhd->reg_base_addr, |
| 520 | DMAR_VER_MAJOR(ver), DMAR_VER_MINOR(ver), |
| 521 | (unsigned long long)iommu->cap, |
| 522 | (unsigned long long)iommu->ecap); |
Suresh Siddha | e61d98d | 2008-07-10 11:16:35 -0700 | [diff] [blame] | 523 | |
| 524 | spin_lock_init(&iommu->register_lock); |
| 525 | |
| 526 | drhd->iommu = iommu; |
Suresh Siddha | 1886e8a | 2008-07-10 11:16:37 -0700 | [diff] [blame] | 527 | return 0; |
Suresh Siddha | e61d98d | 2008-07-10 11:16:35 -0700 | [diff] [blame] | 528 | error: |
| 529 | kfree(iommu); |
Suresh Siddha | 1886e8a | 2008-07-10 11:16:37 -0700 | [diff] [blame] | 530 | return -1; |
Suresh Siddha | e61d98d | 2008-07-10 11:16:35 -0700 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | void free_iommu(struct intel_iommu *iommu) |
| 534 | { |
| 535 | if (!iommu) |
| 536 | return; |
| 537 | |
| 538 | #ifdef CONFIG_DMAR |
| 539 | free_dmar_iommu(iommu); |
| 540 | #endif |
| 541 | |
| 542 | if (iommu->reg) |
| 543 | iounmap(iommu->reg); |
| 544 | kfree(iommu); |
| 545 | } |
Suresh Siddha | fe962e9 | 2008-07-10 11:16:42 -0700 | [diff] [blame] | 546 | |
| 547 | /* |
| 548 | * Reclaim all the submitted descriptors which have completed its work. |
| 549 | */ |
| 550 | static inline void reclaim_free_desc(struct q_inval *qi) |
| 551 | { |
| 552 | while (qi->desc_status[qi->free_tail] == QI_DONE) { |
| 553 | qi->desc_status[qi->free_tail] = QI_FREE; |
| 554 | qi->free_tail = (qi->free_tail + 1) % QI_LENGTH; |
| 555 | qi->free_cnt++; |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | /* |
| 560 | * Submit the queued invalidation descriptor to the remapping |
| 561 | * hardware unit and wait for its completion. |
| 562 | */ |
| 563 | void qi_submit_sync(struct qi_desc *desc, struct intel_iommu *iommu) |
| 564 | { |
| 565 | struct q_inval *qi = iommu->qi; |
| 566 | struct qi_desc *hw, wait_desc; |
| 567 | int wait_index, index; |
| 568 | unsigned long flags; |
| 569 | |
| 570 | if (!qi) |
| 571 | return; |
| 572 | |
| 573 | hw = qi->desc; |
| 574 | |
Suresh Siddha | f05810c | 2008-10-16 16:31:54 -0700 | [diff] [blame] | 575 | spin_lock_irqsave(&qi->q_lock, flags); |
Suresh Siddha | fe962e9 | 2008-07-10 11:16:42 -0700 | [diff] [blame] | 576 | while (qi->free_cnt < 3) { |
Suresh Siddha | f05810c | 2008-10-16 16:31:54 -0700 | [diff] [blame] | 577 | spin_unlock_irqrestore(&qi->q_lock, flags); |
Suresh Siddha | fe962e9 | 2008-07-10 11:16:42 -0700 | [diff] [blame] | 578 | cpu_relax(); |
Suresh Siddha | f05810c | 2008-10-16 16:31:54 -0700 | [diff] [blame] | 579 | spin_lock_irqsave(&qi->q_lock, flags); |
Suresh Siddha | fe962e9 | 2008-07-10 11:16:42 -0700 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | index = qi->free_head; |
| 583 | wait_index = (index + 1) % QI_LENGTH; |
| 584 | |
| 585 | qi->desc_status[index] = qi->desc_status[wait_index] = QI_IN_USE; |
| 586 | |
| 587 | hw[index] = *desc; |
| 588 | |
| 589 | wait_desc.low = QI_IWD_STATUS_DATA(2) | QI_IWD_STATUS_WRITE | QI_IWD_TYPE; |
| 590 | wait_desc.high = virt_to_phys(&qi->desc_status[wait_index]); |
| 591 | |
| 592 | hw[wait_index] = wait_desc; |
| 593 | |
| 594 | __iommu_flush_cache(iommu, &hw[index], sizeof(struct qi_desc)); |
| 595 | __iommu_flush_cache(iommu, &hw[wait_index], sizeof(struct qi_desc)); |
| 596 | |
| 597 | qi->free_head = (qi->free_head + 2) % QI_LENGTH; |
| 598 | qi->free_cnt -= 2; |
| 599 | |
Suresh Siddha | f05810c | 2008-10-16 16:31:54 -0700 | [diff] [blame] | 600 | spin_lock(&iommu->register_lock); |
Suresh Siddha | fe962e9 | 2008-07-10 11:16:42 -0700 | [diff] [blame] | 601 | /* |
| 602 | * update the HW tail register indicating the presence of |
| 603 | * new descriptors. |
| 604 | */ |
| 605 | writel(qi->free_head << 4, iommu->reg + DMAR_IQT_REG); |
Suresh Siddha | f05810c | 2008-10-16 16:31:54 -0700 | [diff] [blame] | 606 | spin_unlock(&iommu->register_lock); |
Suresh Siddha | fe962e9 | 2008-07-10 11:16:42 -0700 | [diff] [blame] | 607 | |
| 608 | while (qi->desc_status[wait_index] != QI_DONE) { |
Suresh Siddha | f05810c | 2008-10-16 16:31:54 -0700 | [diff] [blame] | 609 | /* |
| 610 | * We will leave the interrupts disabled, to prevent interrupt |
| 611 | * context to queue another cmd while a cmd is already submitted |
| 612 | * and waiting for completion on this cpu. This is to avoid |
| 613 | * a deadlock where the interrupt context can wait indefinitely |
| 614 | * for free slots in the queue. |
| 615 | */ |
Suresh Siddha | fe962e9 | 2008-07-10 11:16:42 -0700 | [diff] [blame] | 616 | spin_unlock(&qi->q_lock); |
| 617 | cpu_relax(); |
| 618 | spin_lock(&qi->q_lock); |
| 619 | } |
| 620 | |
| 621 | qi->desc_status[index] = QI_DONE; |
| 622 | |
| 623 | reclaim_free_desc(qi); |
Suresh Siddha | f05810c | 2008-10-16 16:31:54 -0700 | [diff] [blame] | 624 | spin_unlock_irqrestore(&qi->q_lock, flags); |
Suresh Siddha | fe962e9 | 2008-07-10 11:16:42 -0700 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | /* |
| 628 | * Flush the global interrupt entry cache. |
| 629 | */ |
| 630 | void qi_global_iec(struct intel_iommu *iommu) |
| 631 | { |
| 632 | struct qi_desc desc; |
| 633 | |
| 634 | desc.low = QI_IEC_TYPE; |
| 635 | desc.high = 0; |
| 636 | |
| 637 | qi_submit_sync(&desc, iommu); |
| 638 | } |
| 639 | |
Youquan Song | 3481f21 | 2008-10-16 16:31:55 -0700 | [diff] [blame] | 640 | int qi_flush_context(struct intel_iommu *iommu, u16 did, u16 sid, u8 fm, |
| 641 | u64 type, int non_present_entry_flush) |
| 642 | { |
| 643 | |
| 644 | struct qi_desc desc; |
| 645 | |
| 646 | if (non_present_entry_flush) { |
| 647 | if (!cap_caching_mode(iommu->cap)) |
| 648 | return 1; |
| 649 | else |
| 650 | did = 0; |
| 651 | } |
| 652 | |
| 653 | desc.low = QI_CC_FM(fm) | QI_CC_SID(sid) | QI_CC_DID(did) |
| 654 | | QI_CC_GRAN(type) | QI_CC_TYPE; |
| 655 | desc.high = 0; |
| 656 | |
| 657 | qi_submit_sync(&desc, iommu); |
| 658 | |
| 659 | return 0; |
| 660 | |
| 661 | } |
| 662 | |
| 663 | int qi_flush_iotlb(struct intel_iommu *iommu, u16 did, u64 addr, |
| 664 | unsigned int size_order, u64 type, |
| 665 | int non_present_entry_flush) |
| 666 | { |
| 667 | u8 dw = 0, dr = 0; |
| 668 | |
| 669 | struct qi_desc desc; |
| 670 | int ih = 0; |
| 671 | |
| 672 | if (non_present_entry_flush) { |
| 673 | if (!cap_caching_mode(iommu->cap)) |
| 674 | return 1; |
| 675 | else |
| 676 | did = 0; |
| 677 | } |
| 678 | |
| 679 | if (cap_write_drain(iommu->cap)) |
| 680 | dw = 1; |
| 681 | |
| 682 | if (cap_read_drain(iommu->cap)) |
| 683 | dr = 1; |
| 684 | |
| 685 | desc.low = QI_IOTLB_DID(did) | QI_IOTLB_DR(dr) | QI_IOTLB_DW(dw) |
| 686 | | QI_IOTLB_GRAN(type) | QI_IOTLB_TYPE; |
| 687 | desc.high = QI_IOTLB_ADDR(addr) | QI_IOTLB_IH(ih) |
| 688 | | QI_IOTLB_AM(size_order); |
| 689 | |
| 690 | qi_submit_sync(&desc, iommu); |
| 691 | |
| 692 | return 0; |
| 693 | |
| 694 | } |
| 695 | |
Suresh Siddha | fe962e9 | 2008-07-10 11:16:42 -0700 | [diff] [blame] | 696 | /* |
| 697 | * Enable Queued Invalidation interface. This is a must to support |
| 698 | * interrupt-remapping. Also used by DMA-remapping, which replaces |
| 699 | * register based IOTLB invalidation. |
| 700 | */ |
| 701 | int dmar_enable_qi(struct intel_iommu *iommu) |
| 702 | { |
| 703 | u32 cmd, sts; |
| 704 | unsigned long flags; |
| 705 | struct q_inval *qi; |
| 706 | |
| 707 | if (!ecap_qis(iommu->ecap)) |
| 708 | return -ENOENT; |
| 709 | |
| 710 | /* |
| 711 | * queued invalidation is already setup and enabled. |
| 712 | */ |
| 713 | if (iommu->qi) |
| 714 | return 0; |
| 715 | |
| 716 | iommu->qi = kmalloc(sizeof(*qi), GFP_KERNEL); |
| 717 | if (!iommu->qi) |
| 718 | return -ENOMEM; |
| 719 | |
| 720 | qi = iommu->qi; |
| 721 | |
| 722 | qi->desc = (void *)(get_zeroed_page(GFP_KERNEL)); |
| 723 | if (!qi->desc) { |
| 724 | kfree(qi); |
| 725 | iommu->qi = 0; |
| 726 | return -ENOMEM; |
| 727 | } |
| 728 | |
| 729 | qi->desc_status = kmalloc(QI_LENGTH * sizeof(int), GFP_KERNEL); |
| 730 | if (!qi->desc_status) { |
| 731 | free_page((unsigned long) qi->desc); |
| 732 | kfree(qi); |
| 733 | iommu->qi = 0; |
| 734 | return -ENOMEM; |
| 735 | } |
| 736 | |
| 737 | qi->free_head = qi->free_tail = 0; |
| 738 | qi->free_cnt = QI_LENGTH; |
| 739 | |
| 740 | spin_lock_init(&qi->q_lock); |
| 741 | |
| 742 | spin_lock_irqsave(&iommu->register_lock, flags); |
| 743 | /* write zero to the tail reg */ |
| 744 | writel(0, iommu->reg + DMAR_IQT_REG); |
| 745 | |
| 746 | dmar_writeq(iommu->reg + DMAR_IQA_REG, virt_to_phys(qi->desc)); |
| 747 | |
| 748 | cmd = iommu->gcmd | DMA_GCMD_QIE; |
| 749 | iommu->gcmd |= DMA_GCMD_QIE; |
| 750 | writel(cmd, iommu->reg + DMAR_GCMD_REG); |
| 751 | |
| 752 | /* Make sure hardware complete it */ |
| 753 | IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG, readl, (sts & DMA_GSTS_QIES), sts); |
| 754 | spin_unlock_irqrestore(&iommu->register_lock, flags); |
| 755 | |
| 756 | return 0; |
| 757 | } |