Joerg Roedel | f6e2e6b | 2008-06-26 21:27:39 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007-2008 Advanced Micro Devices, Inc. |
| 3 | * Author: Joerg Roedel <joerg.roedel@amd.com> |
| 4 | * Leo Duran <leo.duran@amd.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms of the GNU General Public License version 2 as published |
| 8 | * by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | */ |
| 19 | |
| 20 | #include <linux/pci.h> |
| 21 | #include <linux/acpi.h> |
| 22 | #include <linux/gfp.h> |
| 23 | #include <linux/list.h> |
| 24 | #include <asm/pci-direct.h> |
| 25 | #include <asm/amd_iommu_types.h> |
| 26 | #include <asm/gart.h> |
| 27 | |
| 28 | /* |
| 29 | * definitions for the ACPI scanning code |
| 30 | */ |
| 31 | #define UPDATE_LAST_BDF(x) do {\ |
| 32 | if ((x) > amd_iommu_last_bdf) \ |
| 33 | amd_iommu_last_bdf = (x); \ |
| 34 | } while (0); |
| 35 | |
| 36 | #define DEVID(bus, devfn) (((bus) << 8) | (devfn)) |
| 37 | #define PCI_BUS(x) (((x) >> 8) & 0xff) |
| 38 | #define IVRS_HEADER_LENGTH 48 |
| 39 | #define TBL_SIZE(x) (1 << (PAGE_SHIFT + get_order(amd_iommu_last_bdf * (x)))) |
| 40 | |
| 41 | #define ACPI_IVHD_TYPE 0x10 |
| 42 | #define ACPI_IVMD_TYPE_ALL 0x20 |
| 43 | #define ACPI_IVMD_TYPE 0x21 |
| 44 | #define ACPI_IVMD_TYPE_RANGE 0x22 |
| 45 | |
| 46 | #define IVHD_DEV_ALL 0x01 |
| 47 | #define IVHD_DEV_SELECT 0x02 |
| 48 | #define IVHD_DEV_SELECT_RANGE_START 0x03 |
| 49 | #define IVHD_DEV_RANGE_END 0x04 |
| 50 | #define IVHD_DEV_ALIAS 0x42 |
| 51 | #define IVHD_DEV_ALIAS_RANGE 0x43 |
| 52 | #define IVHD_DEV_EXT_SELECT 0x46 |
| 53 | #define IVHD_DEV_EXT_SELECT_RANGE 0x47 |
| 54 | |
| 55 | #define IVHD_FLAG_HT_TUN_EN 0x00 |
| 56 | #define IVHD_FLAG_PASSPW_EN 0x01 |
| 57 | #define IVHD_FLAG_RESPASSPW_EN 0x02 |
| 58 | #define IVHD_FLAG_ISOC_EN 0x03 |
| 59 | |
| 60 | #define IVMD_FLAG_EXCL_RANGE 0x08 |
| 61 | #define IVMD_FLAG_UNITY_MAP 0x01 |
| 62 | |
| 63 | #define ACPI_DEVFLAG_INITPASS 0x01 |
| 64 | #define ACPI_DEVFLAG_EXTINT 0x02 |
| 65 | #define ACPI_DEVFLAG_NMI 0x04 |
| 66 | #define ACPI_DEVFLAG_SYSMGT1 0x10 |
| 67 | #define ACPI_DEVFLAG_SYSMGT2 0x20 |
| 68 | #define ACPI_DEVFLAG_LINT0 0x40 |
| 69 | #define ACPI_DEVFLAG_LINT1 0x80 |
| 70 | #define ACPI_DEVFLAG_ATSDIS 0x10000000 |
| 71 | |
| 72 | struct ivhd_header { |
| 73 | u8 type; |
| 74 | u8 flags; |
| 75 | u16 length; |
| 76 | u16 devid; |
| 77 | u16 cap_ptr; |
| 78 | u64 mmio_phys; |
| 79 | u16 pci_seg; |
| 80 | u16 info; |
| 81 | u32 reserved; |
| 82 | } __attribute__((packed)); |
| 83 | |
| 84 | struct ivhd_entry { |
| 85 | u8 type; |
| 86 | u16 devid; |
| 87 | u8 flags; |
| 88 | u32 ext; |
| 89 | } __attribute__((packed)); |
| 90 | |
| 91 | struct ivmd_header { |
| 92 | u8 type; |
| 93 | u8 flags; |
| 94 | u16 length; |
| 95 | u16 devid; |
| 96 | u16 aux; |
| 97 | u64 resv; |
| 98 | u64 range_start; |
| 99 | u64 range_length; |
| 100 | } __attribute__((packed)); |
| 101 | |
Joerg Roedel | 928abd2 | 2008-06-26 21:27:40 +0200 | [diff] [blame] | 102 | static int __initdata amd_iommu_disable; |
| 103 | |
| 104 | u16 amd_iommu_last_bdf; |
| 105 | struct list_head amd_iommu_unity_map; |
| 106 | unsigned amd_iommu_aperture_order = 26; |
| 107 | int amd_iommu_isolate; |
| 108 | |
| 109 | struct list_head amd_iommu_list; |
| 110 | struct dev_table_entry *amd_iommu_dev_table; |
| 111 | u16 *amd_iommu_alias_table; |
| 112 | struct amd_iommu **amd_iommu_rlookup_table; |
| 113 | struct protection_domain **amd_iommu_pd_table; |
| 114 | unsigned long *amd_iommu_pd_alloc_bitmap; |
| 115 | |
| 116 | static u32 dev_table_size; |
| 117 | static u32 alias_table_size; |
| 118 | static u32 rlookup_table_size; |
Joerg Roedel | 3e8064b | 2008-06-26 21:27:41 +0200 | [diff] [blame] | 119 | |
Joerg Roedel | b2026aa | 2008-06-26 21:27:44 +0200 | [diff] [blame] | 120 | static void __init iommu_set_exclusion_range(struct amd_iommu *iommu) |
| 121 | { |
| 122 | u64 start = iommu->exclusion_start & PAGE_MASK; |
| 123 | u64 limit = (start + iommu->exclusion_length) & PAGE_MASK; |
| 124 | u64 entry; |
| 125 | |
| 126 | if (!iommu->exclusion_start) |
| 127 | return; |
| 128 | |
| 129 | entry = start | MMIO_EXCL_ENABLE_MASK; |
| 130 | memcpy_toio(iommu->mmio_base + MMIO_EXCL_BASE_OFFSET, |
| 131 | &entry, sizeof(entry)); |
| 132 | |
| 133 | entry = limit; |
| 134 | memcpy_toio(iommu->mmio_base + MMIO_EXCL_LIMIT_OFFSET, |
| 135 | &entry, sizeof(entry)); |
| 136 | } |
| 137 | |
| 138 | static void __init iommu_set_device_table(struct amd_iommu *iommu) |
| 139 | { |
| 140 | u32 entry; |
| 141 | |
| 142 | BUG_ON(iommu->mmio_base == NULL); |
| 143 | |
| 144 | entry = virt_to_phys(amd_iommu_dev_table); |
| 145 | entry |= (dev_table_size >> 12) - 1; |
| 146 | memcpy_toio(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET, |
| 147 | &entry, sizeof(entry)); |
| 148 | } |
| 149 | |
| 150 | static void __init iommu_feature_enable(struct amd_iommu *iommu, u8 bit) |
| 151 | { |
| 152 | u32 ctrl; |
| 153 | |
| 154 | ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET); |
| 155 | ctrl |= (1 << bit); |
| 156 | writel(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET); |
| 157 | } |
| 158 | |
| 159 | static void __init iommu_feature_disable(struct amd_iommu *iommu, u8 bit) |
| 160 | { |
| 161 | u32 ctrl; |
| 162 | |
| 163 | ctrl = (u64)readl(iommu->mmio_base + MMIO_CONTROL_OFFSET); |
| 164 | ctrl &= ~(1 << bit); |
| 165 | writel(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET); |
| 166 | } |
| 167 | |
| 168 | void __init iommu_enable(struct amd_iommu *iommu) |
| 169 | { |
| 170 | u32 ctrl; |
| 171 | |
| 172 | printk(KERN_INFO "AMD IOMMU: Enabling IOMMU at "); |
| 173 | print_devid(iommu->devid, 0); |
| 174 | printk(" cap 0x%hx\n", iommu->cap_ptr); |
| 175 | |
| 176 | iommu_feature_enable(iommu, CONTROL_IOMMU_EN); |
| 177 | ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET); |
| 178 | } |
| 179 | |
Joerg Roedel | 6c56747 | 2008-06-26 21:27:43 +0200 | [diff] [blame] | 180 | static u8 * __init iommu_map_mmio_space(u64 address) |
| 181 | { |
| 182 | u8 *ret; |
| 183 | |
| 184 | if (!request_mem_region(address, MMIO_REGION_LENGTH, "amd_iommu")) |
| 185 | return NULL; |
| 186 | |
| 187 | ret = ioremap_nocache(address, MMIO_REGION_LENGTH); |
| 188 | if (ret != NULL) |
| 189 | return ret; |
| 190 | |
| 191 | release_mem_region(address, MMIO_REGION_LENGTH); |
| 192 | |
| 193 | return NULL; |
| 194 | } |
| 195 | |
| 196 | static void __init iommu_unmap_mmio_space(struct amd_iommu *iommu) |
| 197 | { |
| 198 | if (iommu->mmio_base) |
| 199 | iounmap(iommu->mmio_base); |
| 200 | release_mem_region(iommu->mmio_phys, MMIO_REGION_LENGTH); |
| 201 | } |
| 202 | |
Joerg Roedel | 3e8064b | 2008-06-26 21:27:41 +0200 | [diff] [blame] | 203 | static int __init find_last_devid_on_pci(int bus, int dev, int fn, int cap_ptr) |
| 204 | { |
| 205 | u32 cap; |
| 206 | |
| 207 | cap = read_pci_config(bus, dev, fn, cap_ptr+MMIO_RANGE_OFFSET); |
| 208 | UPDATE_LAST_BDF(DEVID(MMIO_GET_BUS(cap), MMIO_GET_LD(cap))); |
| 209 | |
| 210 | return 0; |
| 211 | } |
| 212 | |
| 213 | static int __init find_last_devid_from_ivhd(struct ivhd_header *h) |
| 214 | { |
| 215 | u8 *p = (void *)h, *end = (void *)h; |
| 216 | struct ivhd_entry *dev; |
| 217 | |
| 218 | p += sizeof(*h); |
| 219 | end += h->length; |
| 220 | |
| 221 | find_last_devid_on_pci(PCI_BUS(h->devid), |
| 222 | PCI_SLOT(h->devid), |
| 223 | PCI_FUNC(h->devid), |
| 224 | h->cap_ptr); |
| 225 | |
| 226 | while (p < end) { |
| 227 | dev = (struct ivhd_entry *)p; |
| 228 | switch (dev->type) { |
| 229 | case IVHD_DEV_SELECT: |
| 230 | case IVHD_DEV_RANGE_END: |
| 231 | case IVHD_DEV_ALIAS: |
| 232 | case IVHD_DEV_EXT_SELECT: |
| 233 | UPDATE_LAST_BDF(dev->devid); |
| 234 | break; |
| 235 | default: |
| 236 | break; |
| 237 | } |
| 238 | p += 0x04 << (*p >> 6); |
| 239 | } |
| 240 | |
| 241 | WARN_ON(p != end); |
| 242 | |
| 243 | return 0; |
| 244 | } |
| 245 | |
| 246 | static int __init find_last_devid_acpi(struct acpi_table_header *table) |
| 247 | { |
| 248 | int i; |
| 249 | u8 checksum = 0, *p = (u8 *)table, *end = (u8 *)table; |
| 250 | struct ivhd_header *h; |
| 251 | |
| 252 | /* |
| 253 | * Validate checksum here so we don't need to do it when |
| 254 | * we actually parse the table |
| 255 | */ |
| 256 | for (i = 0; i < table->length; ++i) |
| 257 | checksum += p[i]; |
| 258 | if (checksum != 0) |
| 259 | /* ACPI table corrupt */ |
| 260 | return -ENODEV; |
| 261 | |
| 262 | p += IVRS_HEADER_LENGTH; |
| 263 | |
| 264 | end += table->length; |
| 265 | while (p < end) { |
| 266 | h = (struct ivhd_header *)p; |
| 267 | switch (h->type) { |
| 268 | case ACPI_IVHD_TYPE: |
| 269 | find_last_devid_from_ivhd(h); |
| 270 | break; |
| 271 | default: |
| 272 | break; |
| 273 | } |
| 274 | p += h->length; |
| 275 | } |
| 276 | WARN_ON(p != end); |
| 277 | |
| 278 | return 0; |
| 279 | } |
| 280 | |
Joerg Roedel | b36ca91 | 2008-06-26 21:27:45 +0200 | [diff] [blame^] | 281 | static u8 * __init alloc_command_buffer(struct amd_iommu *iommu) |
| 282 | { |
| 283 | u8 *cmd_buf = (u8 *)__get_free_pages(GFP_KERNEL, |
| 284 | get_order(CMD_BUFFER_SIZE)); |
| 285 | u64 entry = 0; |
| 286 | |
| 287 | if (cmd_buf == NULL) |
| 288 | return NULL; |
| 289 | |
| 290 | iommu->cmd_buf_size = CMD_BUFFER_SIZE; |
| 291 | |
| 292 | memset(cmd_buf, 0, CMD_BUFFER_SIZE); |
| 293 | |
| 294 | entry = (u64)virt_to_phys(cmd_buf); |
| 295 | entry |= MMIO_CMD_SIZE_512; |
| 296 | memcpy_toio(iommu->mmio_base + MMIO_CMD_BUF_OFFSET, |
| 297 | &entry, sizeof(entry)); |
| 298 | |
| 299 | iommu_feature_enable(iommu, CONTROL_CMDBUF_EN); |
| 300 | |
| 301 | return cmd_buf; |
| 302 | } |
| 303 | |
| 304 | static void __init free_command_buffer(struct amd_iommu *iommu) |
| 305 | { |
| 306 | if (iommu->cmd_buf) |
| 307 | free_pages((unsigned long)iommu->cmd_buf, |
| 308 | get_order(CMD_BUFFER_SIZE)); |
| 309 | } |
| 310 | |