]>
Commit | Line | Data |
---|---|---|
f6e2e6b6 JR |
1 | /* |
2 | * Copyright (C) 2007-2008 Advanced Micro Devices, Inc. | |
3 | * Author: Joerg Roedel <[email protected]> | |
4 | * Leo Duran <[email protected]> | |
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> | |
7441e9cb | 24 | #include <linux/sysdev.h> |
f6e2e6b6 JR |
25 | #include <asm/pci-direct.h> |
26 | #include <asm/amd_iommu_types.h> | |
c6da992e | 27 | #include <asm/amd_iommu.h> |
f6e2e6b6 JR |
28 | #include <asm/gart.h> |
29 | ||
30 | /* | |
31 | * definitions for the ACPI scanning code | |
32 | */ | |
33 | #define UPDATE_LAST_BDF(x) do {\ | |
34 | if ((x) > amd_iommu_last_bdf) \ | |
35 | amd_iommu_last_bdf = (x); \ | |
36 | } while (0); | |
37 | ||
38 | #define DEVID(bus, devfn) (((bus) << 8) | (devfn)) | |
39 | #define PCI_BUS(x) (((x) >> 8) & 0xff) | |
40 | #define IVRS_HEADER_LENGTH 48 | |
41 | #define TBL_SIZE(x) (1 << (PAGE_SHIFT + get_order(amd_iommu_last_bdf * (x)))) | |
42 | ||
43 | #define ACPI_IVHD_TYPE 0x10 | |
44 | #define ACPI_IVMD_TYPE_ALL 0x20 | |
45 | #define ACPI_IVMD_TYPE 0x21 | |
46 | #define ACPI_IVMD_TYPE_RANGE 0x22 | |
47 | ||
48 | #define IVHD_DEV_ALL 0x01 | |
49 | #define IVHD_DEV_SELECT 0x02 | |
50 | #define IVHD_DEV_SELECT_RANGE_START 0x03 | |
51 | #define IVHD_DEV_RANGE_END 0x04 | |
52 | #define IVHD_DEV_ALIAS 0x42 | |
53 | #define IVHD_DEV_ALIAS_RANGE 0x43 | |
54 | #define IVHD_DEV_EXT_SELECT 0x46 | |
55 | #define IVHD_DEV_EXT_SELECT_RANGE 0x47 | |
56 | ||
57 | #define IVHD_FLAG_HT_TUN_EN 0x00 | |
58 | #define IVHD_FLAG_PASSPW_EN 0x01 | |
59 | #define IVHD_FLAG_RESPASSPW_EN 0x02 | |
60 | #define IVHD_FLAG_ISOC_EN 0x03 | |
61 | ||
62 | #define IVMD_FLAG_EXCL_RANGE 0x08 | |
63 | #define IVMD_FLAG_UNITY_MAP 0x01 | |
64 | ||
65 | #define ACPI_DEVFLAG_INITPASS 0x01 | |
66 | #define ACPI_DEVFLAG_EXTINT 0x02 | |
67 | #define ACPI_DEVFLAG_NMI 0x04 | |
68 | #define ACPI_DEVFLAG_SYSMGT1 0x10 | |
69 | #define ACPI_DEVFLAG_SYSMGT2 0x20 | |
70 | #define ACPI_DEVFLAG_LINT0 0x40 | |
71 | #define ACPI_DEVFLAG_LINT1 0x80 | |
72 | #define ACPI_DEVFLAG_ATSDIS 0x10000000 | |
73 | ||
b65233a9 JR |
74 | /* |
75 | * ACPI table definitions | |
76 | * | |
77 | * These data structures are laid over the table to parse the important values | |
78 | * out of it. | |
79 | */ | |
80 | ||
81 | /* | |
82 | * structure describing one IOMMU in the ACPI table. Typically followed by one | |
83 | * or more ivhd_entrys. | |
84 | */ | |
f6e2e6b6 JR |
85 | struct ivhd_header { |
86 | u8 type; | |
87 | u8 flags; | |
88 | u16 length; | |
89 | u16 devid; | |
90 | u16 cap_ptr; | |
91 | u64 mmio_phys; | |
92 | u16 pci_seg; | |
93 | u16 info; | |
94 | u32 reserved; | |
95 | } __attribute__((packed)); | |
96 | ||
b65233a9 JR |
97 | /* |
98 | * A device entry describing which devices a specific IOMMU translates and | |
99 | * which requestor ids they use. | |
100 | */ | |
f6e2e6b6 JR |
101 | struct ivhd_entry { |
102 | u8 type; | |
103 | u16 devid; | |
104 | u8 flags; | |
105 | u32 ext; | |
106 | } __attribute__((packed)); | |
107 | ||
b65233a9 JR |
108 | /* |
109 | * An AMD IOMMU memory definition structure. It defines things like exclusion | |
110 | * ranges for devices and regions that should be unity mapped. | |
111 | */ | |
f6e2e6b6 JR |
112 | struct ivmd_header { |
113 | u8 type; | |
114 | u8 flags; | |
115 | u16 length; | |
116 | u16 devid; | |
117 | u16 aux; | |
118 | u64 resv; | |
119 | u64 range_start; | |
120 | u64 range_length; | |
121 | } __attribute__((packed)); | |
122 | ||
c1cbebee JR |
123 | static int __initdata amd_iommu_detected; |
124 | ||
b65233a9 JR |
125 | u16 amd_iommu_last_bdf; /* largest PCI device id we have |
126 | to handle */ | |
127 | struct list_head amd_iommu_unity_map; /* a list of required unity mappings | |
128 | we find in ACPI */ | |
129 | unsigned amd_iommu_aperture_order = 26; /* size of aperture in power of 2 */ | |
130 | int amd_iommu_isolate; /* if 1, device isolation is enabled */ | |
928abd25 | 131 | |
b65233a9 JR |
132 | struct list_head amd_iommu_list; /* list of all AMD IOMMUs in the |
133 | system */ | |
134 | ||
135 | /* | |
136 | * Pointer to the device table which is shared by all AMD IOMMUs | |
137 | * it is indexed by the PCI device id or the HT unit id and contains | |
138 | * information about the domain the device belongs to as well as the | |
139 | * page table root pointer. | |
140 | */ | |
928abd25 | 141 | struct dev_table_entry *amd_iommu_dev_table; |
b65233a9 JR |
142 | |
143 | /* | |
144 | * The alias table is a driver specific data structure which contains the | |
145 | * mappings of the PCI device ids to the actual requestor ids on the IOMMU. | |
146 | * More than one device can share the same requestor id. | |
147 | */ | |
928abd25 | 148 | u16 *amd_iommu_alias_table; |
b65233a9 JR |
149 | |
150 | /* | |
151 | * The rlookup table is used to find the IOMMU which is responsible | |
152 | * for a specific device. It is also indexed by the PCI device id. | |
153 | */ | |
928abd25 | 154 | struct amd_iommu **amd_iommu_rlookup_table; |
b65233a9 JR |
155 | |
156 | /* | |
157 | * The pd table (protection domain table) is used to find the protection domain | |
158 | * data structure a device belongs to. Indexed with the PCI device id too. | |
159 | */ | |
928abd25 | 160 | struct protection_domain **amd_iommu_pd_table; |
b65233a9 JR |
161 | |
162 | /* | |
163 | * AMD IOMMU allows up to 2^16 differend protection domains. This is a bitmap | |
164 | * to know which ones are already in use. | |
165 | */ | |
928abd25 JR |
166 | unsigned long *amd_iommu_pd_alloc_bitmap; |
167 | ||
b65233a9 JR |
168 | static u32 dev_table_size; /* size of the device table */ |
169 | static u32 alias_table_size; /* size of the alias table */ | |
170 | static u32 rlookup_table_size; /* size if the rlookup table */ | |
171 | ||
172 | /**************************************************************************** | |
173 | * | |
174 | * AMD IOMMU MMIO register space handling functions | |
175 | * | |
176 | * These functions are used to program the IOMMU device registers in | |
177 | * MMIO space required for that driver. | |
178 | * | |
179 | ****************************************************************************/ | |
3e8064ba | 180 | |
b65233a9 JR |
181 | /* |
182 | * This function set the exclusion range in the IOMMU. DMA accesses to the | |
183 | * exclusion range are passed through untranslated | |
184 | */ | |
b2026aa2 JR |
185 | static void __init iommu_set_exclusion_range(struct amd_iommu *iommu) |
186 | { | |
187 | u64 start = iommu->exclusion_start & PAGE_MASK; | |
188 | u64 limit = (start + iommu->exclusion_length) & PAGE_MASK; | |
189 | u64 entry; | |
190 | ||
191 | if (!iommu->exclusion_start) | |
192 | return; | |
193 | ||
194 | entry = start | MMIO_EXCL_ENABLE_MASK; | |
195 | memcpy_toio(iommu->mmio_base + MMIO_EXCL_BASE_OFFSET, | |
196 | &entry, sizeof(entry)); | |
197 | ||
198 | entry = limit; | |
199 | memcpy_toio(iommu->mmio_base + MMIO_EXCL_LIMIT_OFFSET, | |
200 | &entry, sizeof(entry)); | |
201 | } | |
202 | ||
b65233a9 | 203 | /* Programs the physical address of the device table into the IOMMU hardware */ |
b2026aa2 JR |
204 | static void __init iommu_set_device_table(struct amd_iommu *iommu) |
205 | { | |
206 | u32 entry; | |
207 | ||
208 | BUG_ON(iommu->mmio_base == NULL); | |
209 | ||
210 | entry = virt_to_phys(amd_iommu_dev_table); | |
211 | entry |= (dev_table_size >> 12) - 1; | |
212 | memcpy_toio(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET, | |
213 | &entry, sizeof(entry)); | |
214 | } | |
215 | ||
b65233a9 | 216 | /* Generic functions to enable/disable certain features of the IOMMU. */ |
b2026aa2 JR |
217 | static void __init iommu_feature_enable(struct amd_iommu *iommu, u8 bit) |
218 | { | |
219 | u32 ctrl; | |
220 | ||
221 | ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET); | |
222 | ctrl |= (1 << bit); | |
223 | writel(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET); | |
224 | } | |
225 | ||
226 | static void __init iommu_feature_disable(struct amd_iommu *iommu, u8 bit) | |
227 | { | |
228 | u32 ctrl; | |
229 | ||
230 | ctrl = (u64)readl(iommu->mmio_base + MMIO_CONTROL_OFFSET); | |
231 | ctrl &= ~(1 << bit); | |
232 | writel(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET); | |
233 | } | |
234 | ||
b65233a9 | 235 | /* Function to enable the hardware */ |
b2026aa2 JR |
236 | void __init iommu_enable(struct amd_iommu *iommu) |
237 | { | |
b2026aa2 JR |
238 | printk(KERN_INFO "AMD IOMMU: Enabling IOMMU at "); |
239 | print_devid(iommu->devid, 0); | |
240 | printk(" cap 0x%hx\n", iommu->cap_ptr); | |
241 | ||
242 | iommu_feature_enable(iommu, CONTROL_IOMMU_EN); | |
b2026aa2 JR |
243 | } |
244 | ||
b65233a9 JR |
245 | /* |
246 | * mapping and unmapping functions for the IOMMU MMIO space. Each AMD IOMMU in | |
247 | * the system has one. | |
248 | */ | |
6c56747b JR |
249 | static u8 * __init iommu_map_mmio_space(u64 address) |
250 | { | |
251 | u8 *ret; | |
252 | ||
253 | if (!request_mem_region(address, MMIO_REGION_LENGTH, "amd_iommu")) | |
254 | return NULL; | |
255 | ||
256 | ret = ioremap_nocache(address, MMIO_REGION_LENGTH); | |
257 | if (ret != NULL) | |
258 | return ret; | |
259 | ||
260 | release_mem_region(address, MMIO_REGION_LENGTH); | |
261 | ||
262 | return NULL; | |
263 | } | |
264 | ||
265 | static void __init iommu_unmap_mmio_space(struct amd_iommu *iommu) | |
266 | { | |
267 | if (iommu->mmio_base) | |
268 | iounmap(iommu->mmio_base); | |
269 | release_mem_region(iommu->mmio_phys, MMIO_REGION_LENGTH); | |
270 | } | |
271 | ||
b65233a9 JR |
272 | /**************************************************************************** |
273 | * | |
274 | * The functions below belong to the first pass of AMD IOMMU ACPI table | |
275 | * parsing. In this pass we try to find out the highest device id this | |
276 | * code has to handle. Upon this information the size of the shared data | |
277 | * structures is determined later. | |
278 | * | |
279 | ****************************************************************************/ | |
280 | ||
281 | /* | |
282 | * This function reads the last device id the IOMMU has to handle from the PCI | |
283 | * capability header for this IOMMU | |
284 | */ | |
3e8064ba JR |
285 | static int __init find_last_devid_on_pci(int bus, int dev, int fn, int cap_ptr) |
286 | { | |
287 | u32 cap; | |
288 | ||
289 | cap = read_pci_config(bus, dev, fn, cap_ptr+MMIO_RANGE_OFFSET); | |
290 | UPDATE_LAST_BDF(DEVID(MMIO_GET_BUS(cap), MMIO_GET_LD(cap))); | |
291 | ||
292 | return 0; | |
293 | } | |
294 | ||
b65233a9 JR |
295 | /* |
296 | * After reading the highest device id from the IOMMU PCI capability header | |
297 | * this function looks if there is a higher device id defined in the ACPI table | |
298 | */ | |
3e8064ba JR |
299 | static int __init find_last_devid_from_ivhd(struct ivhd_header *h) |
300 | { | |
301 | u8 *p = (void *)h, *end = (void *)h; | |
302 | struct ivhd_entry *dev; | |
303 | ||
304 | p += sizeof(*h); | |
305 | end += h->length; | |
306 | ||
307 | find_last_devid_on_pci(PCI_BUS(h->devid), | |
308 | PCI_SLOT(h->devid), | |
309 | PCI_FUNC(h->devid), | |
310 | h->cap_ptr); | |
311 | ||
312 | while (p < end) { | |
313 | dev = (struct ivhd_entry *)p; | |
314 | switch (dev->type) { | |
315 | case IVHD_DEV_SELECT: | |
316 | case IVHD_DEV_RANGE_END: | |
317 | case IVHD_DEV_ALIAS: | |
318 | case IVHD_DEV_EXT_SELECT: | |
b65233a9 | 319 | /* all the above subfield types refer to device ids */ |
3e8064ba JR |
320 | UPDATE_LAST_BDF(dev->devid); |
321 | break; | |
322 | default: | |
323 | break; | |
324 | } | |
325 | p += 0x04 << (*p >> 6); | |
326 | } | |
327 | ||
328 | WARN_ON(p != end); | |
329 | ||
330 | return 0; | |
331 | } | |
332 | ||
b65233a9 JR |
333 | /* |
334 | * Iterate over all IVHD entries in the ACPI table and find the highest device | |
335 | * id which we need to handle. This is the first of three functions which parse | |
336 | * the ACPI table. So we check the checksum here. | |
337 | */ | |
3e8064ba JR |
338 | static int __init find_last_devid_acpi(struct acpi_table_header *table) |
339 | { | |
340 | int i; | |
341 | u8 checksum = 0, *p = (u8 *)table, *end = (u8 *)table; | |
342 | struct ivhd_header *h; | |
343 | ||
344 | /* | |
345 | * Validate checksum here so we don't need to do it when | |
346 | * we actually parse the table | |
347 | */ | |
348 | for (i = 0; i < table->length; ++i) | |
349 | checksum += p[i]; | |
350 | if (checksum != 0) | |
351 | /* ACPI table corrupt */ | |
352 | return -ENODEV; | |
353 | ||
354 | p += IVRS_HEADER_LENGTH; | |
355 | ||
356 | end += table->length; | |
357 | while (p < end) { | |
358 | h = (struct ivhd_header *)p; | |
359 | switch (h->type) { | |
360 | case ACPI_IVHD_TYPE: | |
361 | find_last_devid_from_ivhd(h); | |
362 | break; | |
363 | default: | |
364 | break; | |
365 | } | |
366 | p += h->length; | |
367 | } | |
368 | WARN_ON(p != end); | |
369 | ||
370 | return 0; | |
371 | } | |
372 | ||
b65233a9 JR |
373 | /**************************************************************************** |
374 | * | |
375 | * The following functions belong the the code path which parses the ACPI table | |
376 | * the second time. In this ACPI parsing iteration we allocate IOMMU specific | |
377 | * data structures, initialize the device/alias/rlookup table and also | |
378 | * basically initialize the hardware. | |
379 | * | |
380 | ****************************************************************************/ | |
381 | ||
382 | /* | |
383 | * Allocates the command buffer. This buffer is per AMD IOMMU. We can | |
384 | * write commands to that buffer later and the IOMMU will execute them | |
385 | * asynchronously | |
386 | */ | |
b36ca91e JR |
387 | static u8 * __init alloc_command_buffer(struct amd_iommu *iommu) |
388 | { | |
389 | u8 *cmd_buf = (u8 *)__get_free_pages(GFP_KERNEL, | |
390 | get_order(CMD_BUFFER_SIZE)); | |
391 | u64 entry = 0; | |
392 | ||
393 | if (cmd_buf == NULL) | |
394 | return NULL; | |
395 | ||
396 | iommu->cmd_buf_size = CMD_BUFFER_SIZE; | |
397 | ||
398 | memset(cmd_buf, 0, CMD_BUFFER_SIZE); | |
399 | ||
400 | entry = (u64)virt_to_phys(cmd_buf); | |
401 | entry |= MMIO_CMD_SIZE_512; | |
402 | memcpy_toio(iommu->mmio_base + MMIO_CMD_BUF_OFFSET, | |
403 | &entry, sizeof(entry)); | |
404 | ||
405 | iommu_feature_enable(iommu, CONTROL_CMDBUF_EN); | |
406 | ||
407 | return cmd_buf; | |
408 | } | |
409 | ||
410 | static void __init free_command_buffer(struct amd_iommu *iommu) | |
411 | { | |
412 | if (iommu->cmd_buf) | |
413 | free_pages((unsigned long)iommu->cmd_buf, | |
414 | get_order(CMD_BUFFER_SIZE)); | |
415 | } | |
416 | ||
b65233a9 | 417 | /* sets a specific bit in the device table entry. */ |
3566b778 JR |
418 | static void set_dev_entry_bit(u16 devid, u8 bit) |
419 | { | |
420 | int i = (bit >> 5) & 0x07; | |
421 | int _bit = bit & 0x1f; | |
422 | ||
423 | amd_iommu_dev_table[devid].data[i] |= (1 << _bit); | |
424 | } | |
425 | ||
b65233a9 JR |
426 | /* |
427 | * This function takes the device specific flags read from the ACPI | |
428 | * table and sets up the device table entry with that information | |
429 | */ | |
3566b778 JR |
430 | static void __init set_dev_entry_from_acpi(u16 devid, u32 flags, u32 ext_flags) |
431 | { | |
432 | if (flags & ACPI_DEVFLAG_INITPASS) | |
433 | set_dev_entry_bit(devid, DEV_ENTRY_INIT_PASS); | |
434 | if (flags & ACPI_DEVFLAG_EXTINT) | |
435 | set_dev_entry_bit(devid, DEV_ENTRY_EINT_PASS); | |
436 | if (flags & ACPI_DEVFLAG_NMI) | |
437 | set_dev_entry_bit(devid, DEV_ENTRY_NMI_PASS); | |
438 | if (flags & ACPI_DEVFLAG_SYSMGT1) | |
439 | set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT1); | |
440 | if (flags & ACPI_DEVFLAG_SYSMGT2) | |
441 | set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT2); | |
442 | if (flags & ACPI_DEVFLAG_LINT0) | |
443 | set_dev_entry_bit(devid, DEV_ENTRY_LINT0_PASS); | |
444 | if (flags & ACPI_DEVFLAG_LINT1) | |
445 | set_dev_entry_bit(devid, DEV_ENTRY_LINT1_PASS); | |
446 | } | |
447 | ||
b65233a9 | 448 | /* Writes the specific IOMMU for a device into the rlookup table */ |
3566b778 JR |
449 | static void __init set_iommu_for_device(struct amd_iommu *iommu, u16 devid) |
450 | { | |
451 | amd_iommu_rlookup_table[devid] = iommu; | |
452 | } | |
453 | ||
b65233a9 JR |
454 | /* |
455 | * Reads the device exclusion range from ACPI and initialize IOMMU with | |
456 | * it | |
457 | */ | |
3566b778 JR |
458 | static void __init set_device_exclusion_range(u16 devid, struct ivmd_header *m) |
459 | { | |
460 | struct amd_iommu *iommu = amd_iommu_rlookup_table[devid]; | |
461 | ||
462 | if (!(m->flags & IVMD_FLAG_EXCL_RANGE)) | |
463 | return; | |
464 | ||
465 | if (iommu) { | |
b65233a9 JR |
466 | /* |
467 | * We only can configure exclusion ranges per IOMMU, not | |
468 | * per device. But we can enable the exclusion range per | |
469 | * device. This is done here | |
470 | */ | |
3566b778 JR |
471 | set_dev_entry_bit(m->devid, DEV_ENTRY_EX); |
472 | iommu->exclusion_start = m->range_start; | |
473 | iommu->exclusion_length = m->range_length; | |
474 | } | |
475 | } | |
476 | ||
b65233a9 JR |
477 | /* |
478 | * This function reads some important data from the IOMMU PCI space and | |
479 | * initializes the driver data structure with it. It reads the hardware | |
480 | * capabilities and the first/last device entries | |
481 | */ | |
5d0c8e49 JR |
482 | static void __init init_iommu_from_pci(struct amd_iommu *iommu) |
483 | { | |
484 | int bus = PCI_BUS(iommu->devid); | |
485 | int dev = PCI_SLOT(iommu->devid); | |
486 | int fn = PCI_FUNC(iommu->devid); | |
487 | int cap_ptr = iommu->cap_ptr; | |
488 | u32 range; | |
489 | ||
490 | iommu->cap = read_pci_config(bus, dev, fn, cap_ptr+MMIO_CAP_HDR_OFFSET); | |
491 | ||
492 | range = read_pci_config(bus, dev, fn, cap_ptr+MMIO_RANGE_OFFSET); | |
493 | iommu->first_device = DEVID(MMIO_GET_BUS(range), MMIO_GET_FD(range)); | |
494 | iommu->last_device = DEVID(MMIO_GET_BUS(range), MMIO_GET_LD(range)); | |
495 | } | |
496 | ||
b65233a9 JR |
497 | /* |
498 | * Takes a pointer to an AMD IOMMU entry in the ACPI table and | |
499 | * initializes the hardware and our data structures with it. | |
500 | */ | |
5d0c8e49 JR |
501 | static void __init init_iommu_from_acpi(struct amd_iommu *iommu, |
502 | struct ivhd_header *h) | |
503 | { | |
504 | u8 *p = (u8 *)h; | |
505 | u8 *end = p, flags = 0; | |
506 | u16 dev_i, devid = 0, devid_start = 0, devid_to = 0; | |
507 | u32 ext_flags = 0; | |
508 | bool alias = 0; | |
509 | struct ivhd_entry *e; | |
510 | ||
511 | /* | |
512 | * First set the recommended feature enable bits from ACPI | |
513 | * into the IOMMU control registers | |
514 | */ | |
515 | h->flags & IVHD_FLAG_HT_TUN_EN ? | |
516 | iommu_feature_enable(iommu, CONTROL_HT_TUN_EN) : | |
517 | iommu_feature_disable(iommu, CONTROL_HT_TUN_EN); | |
518 | ||
519 | h->flags & IVHD_FLAG_PASSPW_EN ? | |
520 | iommu_feature_enable(iommu, CONTROL_PASSPW_EN) : | |
521 | iommu_feature_disable(iommu, CONTROL_PASSPW_EN); | |
522 | ||
523 | h->flags & IVHD_FLAG_RESPASSPW_EN ? | |
524 | iommu_feature_enable(iommu, CONTROL_RESPASSPW_EN) : | |
525 | iommu_feature_disable(iommu, CONTROL_RESPASSPW_EN); | |
526 | ||
527 | h->flags & IVHD_FLAG_ISOC_EN ? | |
528 | iommu_feature_enable(iommu, CONTROL_ISOC_EN) : | |
529 | iommu_feature_disable(iommu, CONTROL_ISOC_EN); | |
530 | ||
531 | /* | |
532 | * make IOMMU memory accesses cache coherent | |
533 | */ | |
534 | iommu_feature_enable(iommu, CONTROL_COHERENT_EN); | |
535 | ||
536 | /* | |
537 | * Done. Now parse the device entries | |
538 | */ | |
539 | p += sizeof(struct ivhd_header); | |
540 | end += h->length; | |
541 | ||
542 | while (p < end) { | |
543 | e = (struct ivhd_entry *)p; | |
544 | switch (e->type) { | |
545 | case IVHD_DEV_ALL: | |
546 | for (dev_i = iommu->first_device; | |
547 | dev_i <= iommu->last_device; ++dev_i) | |
548 | set_dev_entry_from_acpi(dev_i, e->flags, 0); | |
549 | break; | |
550 | case IVHD_DEV_SELECT: | |
551 | devid = e->devid; | |
552 | set_dev_entry_from_acpi(devid, e->flags, 0); | |
553 | break; | |
554 | case IVHD_DEV_SELECT_RANGE_START: | |
555 | devid_start = e->devid; | |
556 | flags = e->flags; | |
557 | ext_flags = 0; | |
558 | alias = 0; | |
559 | break; | |
560 | case IVHD_DEV_ALIAS: | |
561 | devid = e->devid; | |
562 | devid_to = e->ext >> 8; | |
563 | set_dev_entry_from_acpi(devid, e->flags, 0); | |
564 | amd_iommu_alias_table[devid] = devid_to; | |
565 | break; | |
566 | case IVHD_DEV_ALIAS_RANGE: | |
567 | devid_start = e->devid; | |
568 | flags = e->flags; | |
569 | devid_to = e->ext >> 8; | |
570 | ext_flags = 0; | |
571 | alias = 1; | |
572 | break; | |
573 | case IVHD_DEV_EXT_SELECT: | |
574 | devid = e->devid; | |
575 | set_dev_entry_from_acpi(devid, e->flags, e->ext); | |
576 | break; | |
577 | case IVHD_DEV_EXT_SELECT_RANGE: | |
578 | devid_start = e->devid; | |
579 | flags = e->flags; | |
580 | ext_flags = e->ext; | |
581 | alias = 0; | |
582 | break; | |
583 | case IVHD_DEV_RANGE_END: | |
584 | devid = e->devid; | |
585 | for (dev_i = devid_start; dev_i <= devid; ++dev_i) { | |
586 | if (alias) | |
587 | amd_iommu_alias_table[dev_i] = devid_to; | |
588 | set_dev_entry_from_acpi( | |
589 | amd_iommu_alias_table[dev_i], | |
590 | flags, ext_flags); | |
591 | } | |
592 | break; | |
593 | default: | |
594 | break; | |
595 | } | |
596 | ||
597 | p += 0x04 << (e->type >> 6); | |
598 | } | |
599 | } | |
600 | ||
b65233a9 | 601 | /* Initializes the device->iommu mapping for the driver */ |
5d0c8e49 JR |
602 | static int __init init_iommu_devices(struct amd_iommu *iommu) |
603 | { | |
604 | u16 i; | |
605 | ||
606 | for (i = iommu->first_device; i <= iommu->last_device; ++i) | |
607 | set_iommu_for_device(iommu, i); | |
608 | ||
609 | return 0; | |
610 | } | |
611 | ||
e47d402d JR |
612 | static void __init free_iommu_one(struct amd_iommu *iommu) |
613 | { | |
614 | free_command_buffer(iommu); | |
615 | iommu_unmap_mmio_space(iommu); | |
616 | } | |
617 | ||
618 | static void __init free_iommu_all(void) | |
619 | { | |
620 | struct amd_iommu *iommu, *next; | |
621 | ||
622 | list_for_each_entry_safe(iommu, next, &amd_iommu_list, list) { | |
623 | list_del(&iommu->list); | |
624 | free_iommu_one(iommu); | |
625 | kfree(iommu); | |
626 | } | |
627 | } | |
628 | ||
b65233a9 JR |
629 | /* |
630 | * This function clues the initialization function for one IOMMU | |
631 | * together and also allocates the command buffer and programs the | |
632 | * hardware. It does NOT enable the IOMMU. This is done afterwards. | |
633 | */ | |
e47d402d JR |
634 | static int __init init_iommu_one(struct amd_iommu *iommu, struct ivhd_header *h) |
635 | { | |
636 | spin_lock_init(&iommu->lock); | |
637 | list_add_tail(&iommu->list, &amd_iommu_list); | |
638 | ||
639 | /* | |
640 | * Copy data from ACPI table entry to the iommu struct | |
641 | */ | |
642 | iommu->devid = h->devid; | |
643 | iommu->cap_ptr = h->cap_ptr; | |
644 | iommu->mmio_phys = h->mmio_phys; | |
645 | iommu->mmio_base = iommu_map_mmio_space(h->mmio_phys); | |
646 | if (!iommu->mmio_base) | |
647 | return -ENOMEM; | |
648 | ||
649 | iommu_set_device_table(iommu); | |
650 | iommu->cmd_buf = alloc_command_buffer(iommu); | |
651 | if (!iommu->cmd_buf) | |
652 | return -ENOMEM; | |
653 | ||
654 | init_iommu_from_pci(iommu); | |
655 | init_iommu_from_acpi(iommu, h); | |
656 | init_iommu_devices(iommu); | |
657 | ||
658 | return 0; | |
659 | } | |
660 | ||
b65233a9 JR |
661 | /* |
662 | * Iterates over all IOMMU entries in the ACPI table, allocates the | |
663 | * IOMMU structure and initializes it with init_iommu_one() | |
664 | */ | |
e47d402d JR |
665 | static int __init init_iommu_all(struct acpi_table_header *table) |
666 | { | |
667 | u8 *p = (u8 *)table, *end = (u8 *)table; | |
668 | struct ivhd_header *h; | |
669 | struct amd_iommu *iommu; | |
670 | int ret; | |
671 | ||
672 | INIT_LIST_HEAD(&amd_iommu_list); | |
673 | ||
674 | end += table->length; | |
675 | p += IVRS_HEADER_LENGTH; | |
676 | ||
677 | while (p < end) { | |
678 | h = (struct ivhd_header *)p; | |
679 | switch (*p) { | |
680 | case ACPI_IVHD_TYPE: | |
681 | iommu = kzalloc(sizeof(struct amd_iommu), GFP_KERNEL); | |
682 | if (iommu == NULL) | |
683 | return -ENOMEM; | |
684 | ret = init_iommu_one(iommu, h); | |
685 | if (ret) | |
686 | return ret; | |
687 | break; | |
688 | default: | |
689 | break; | |
690 | } | |
691 | p += h->length; | |
692 | ||
693 | } | |
694 | WARN_ON(p != end); | |
695 | ||
696 | return 0; | |
697 | } | |
698 | ||
b65233a9 JR |
699 | /**************************************************************************** |
700 | * | |
701 | * The next functions belong to the third pass of parsing the ACPI | |
702 | * table. In this last pass the memory mapping requirements are | |
703 | * gathered (like exclusion and unity mapping reanges). | |
704 | * | |
705 | ****************************************************************************/ | |
706 | ||
be2a022c JR |
707 | static void __init free_unity_maps(void) |
708 | { | |
709 | struct unity_map_entry *entry, *next; | |
710 | ||
711 | list_for_each_entry_safe(entry, next, &amd_iommu_unity_map, list) { | |
712 | list_del(&entry->list); | |
713 | kfree(entry); | |
714 | } | |
715 | } | |
716 | ||
b65233a9 | 717 | /* called when we find an exclusion range definition in ACPI */ |
be2a022c JR |
718 | static int __init init_exclusion_range(struct ivmd_header *m) |
719 | { | |
720 | int i; | |
721 | ||
722 | switch (m->type) { | |
723 | case ACPI_IVMD_TYPE: | |
724 | set_device_exclusion_range(m->devid, m); | |
725 | break; | |
726 | case ACPI_IVMD_TYPE_ALL: | |
727 | for (i = 0; i < amd_iommu_last_bdf; ++i) | |
728 | set_device_exclusion_range(i, m); | |
729 | break; | |
730 | case ACPI_IVMD_TYPE_RANGE: | |
731 | for (i = m->devid; i <= m->aux; ++i) | |
732 | set_device_exclusion_range(i, m); | |
733 | break; | |
734 | default: | |
735 | break; | |
736 | } | |
737 | ||
738 | return 0; | |
739 | } | |
740 | ||
b65233a9 | 741 | /* called for unity map ACPI definition */ |
be2a022c JR |
742 | static int __init init_unity_map_range(struct ivmd_header *m) |
743 | { | |
744 | struct unity_map_entry *e = 0; | |
745 | ||
746 | e = kzalloc(sizeof(*e), GFP_KERNEL); | |
747 | if (e == NULL) | |
748 | return -ENOMEM; | |
749 | ||
750 | switch (m->type) { | |
751 | default: | |
752 | case ACPI_IVMD_TYPE: | |
753 | e->devid_start = e->devid_end = m->devid; | |
754 | break; | |
755 | case ACPI_IVMD_TYPE_ALL: | |
756 | e->devid_start = 0; | |
757 | e->devid_end = amd_iommu_last_bdf; | |
758 | break; | |
759 | case ACPI_IVMD_TYPE_RANGE: | |
760 | e->devid_start = m->devid; | |
761 | e->devid_end = m->aux; | |
762 | break; | |
763 | } | |
764 | e->address_start = PAGE_ALIGN(m->range_start); | |
765 | e->address_end = e->address_start + PAGE_ALIGN(m->range_length); | |
766 | e->prot = m->flags >> 1; | |
767 | ||
768 | list_add_tail(&e->list, &amd_iommu_unity_map); | |
769 | ||
770 | return 0; | |
771 | } | |
772 | ||
b65233a9 | 773 | /* iterates over all memory definitions we find in the ACPI table */ |
be2a022c JR |
774 | static int __init init_memory_definitions(struct acpi_table_header *table) |
775 | { | |
776 | u8 *p = (u8 *)table, *end = (u8 *)table; | |
777 | struct ivmd_header *m; | |
778 | ||
779 | INIT_LIST_HEAD(&amd_iommu_unity_map); | |
780 | ||
781 | end += table->length; | |
782 | p += IVRS_HEADER_LENGTH; | |
783 | ||
784 | while (p < end) { | |
785 | m = (struct ivmd_header *)p; | |
786 | if (m->flags & IVMD_FLAG_EXCL_RANGE) | |
787 | init_exclusion_range(m); | |
788 | else if (m->flags & IVMD_FLAG_UNITY_MAP) | |
789 | init_unity_map_range(m); | |
790 | ||
791 | p += m->length; | |
792 | } | |
793 | ||
794 | return 0; | |
795 | } | |
796 | ||
b65233a9 JR |
797 | /* |
798 | * This function finally enables all IOMMUs found in the system after | |
799 | * they have been initialized | |
800 | */ | |
8736197b JR |
801 | static void __init enable_iommus(void) |
802 | { | |
803 | struct amd_iommu *iommu; | |
804 | ||
805 | list_for_each_entry(iommu, &amd_iommu_list, list) { | |
806 | iommu_set_exclusion_range(iommu); | |
807 | iommu_enable(iommu); | |
808 | } | |
809 | } | |
810 | ||
7441e9cb JR |
811 | /* |
812 | * Suspend/Resume support | |
813 | * disable suspend until real resume implemented | |
814 | */ | |
815 | ||
816 | static int amd_iommu_resume(struct sys_device *dev) | |
817 | { | |
818 | return 0; | |
819 | } | |
820 | ||
821 | static int amd_iommu_suspend(struct sys_device *dev, pm_message_t state) | |
822 | { | |
823 | return -EINVAL; | |
824 | } | |
825 | ||
826 | static struct sysdev_class amd_iommu_sysdev_class = { | |
827 | .name = "amd_iommu", | |
828 | .suspend = amd_iommu_suspend, | |
829 | .resume = amd_iommu_resume, | |
830 | }; | |
831 | ||
832 | static struct sys_device device_amd_iommu = { | |
833 | .id = 0, | |
834 | .cls = &amd_iommu_sysdev_class, | |
835 | }; | |
836 | ||
b65233a9 JR |
837 | /* |
838 | * This is the core init function for AMD IOMMU hardware in the system. | |
839 | * This function is called from the generic x86 DMA layer initialization | |
840 | * code. | |
841 | * | |
842 | * This function basically parses the ACPI table for AMD IOMMU (IVRS) | |
843 | * three times: | |
844 | * | |
845 | * 1 pass) Find the highest PCI device id the driver has to handle. | |
846 | * Upon this information the size of the data structures is | |
847 | * determined that needs to be allocated. | |
848 | * | |
849 | * 2 pass) Initialize the data structures just allocated with the | |
850 | * information in the ACPI table about available AMD IOMMUs | |
851 | * in the system. It also maps the PCI devices in the | |
852 | * system to specific IOMMUs | |
853 | * | |
854 | * 3 pass) After the basic data structures are allocated and | |
855 | * initialized we update them with information about memory | |
856 | * remapping requirements parsed out of the ACPI table in | |
857 | * this last pass. | |
858 | * | |
859 | * After that the hardware is initialized and ready to go. In the last | |
860 | * step we do some Linux specific things like registering the driver in | |
861 | * the dma_ops interface and initializing the suspend/resume support | |
862 | * functions. Finally it prints some information about AMD IOMMUs and | |
863 | * the driver state and enables the hardware. | |
864 | */ | |
fe74c9cf JR |
865 | int __init amd_iommu_init(void) |
866 | { | |
867 | int i, ret = 0; | |
868 | ||
869 | ||
8b14518f | 870 | if (no_iommu) { |
fe74c9cf JR |
871 | printk(KERN_INFO "AMD IOMMU disabled by kernel command line\n"); |
872 | return 0; | |
873 | } | |
874 | ||
c1cbebee JR |
875 | if (!amd_iommu_detected) |
876 | return -ENODEV; | |
877 | ||
fe74c9cf JR |
878 | /* |
879 | * First parse ACPI tables to find the largest Bus/Dev/Func | |
880 | * we need to handle. Upon this information the shared data | |
881 | * structures for the IOMMUs in the system will be allocated | |
882 | */ | |
883 | if (acpi_table_parse("IVRS", find_last_devid_acpi) != 0) | |
884 | return -ENODEV; | |
885 | ||
886 | dev_table_size = TBL_SIZE(DEV_TABLE_ENTRY_SIZE); | |
887 | alias_table_size = TBL_SIZE(ALIAS_TABLE_ENTRY_SIZE); | |
888 | rlookup_table_size = TBL_SIZE(RLOOKUP_TABLE_ENTRY_SIZE); | |
889 | ||
890 | ret = -ENOMEM; | |
891 | ||
892 | /* Device table - directly used by all IOMMUs */ | |
893 | amd_iommu_dev_table = (void *)__get_free_pages(GFP_KERNEL, | |
894 | get_order(dev_table_size)); | |
895 | if (amd_iommu_dev_table == NULL) | |
896 | goto out; | |
897 | ||
898 | /* | |
899 | * Alias table - map PCI Bus/Dev/Func to Bus/Dev/Func the | |
900 | * IOMMU see for that device | |
901 | */ | |
902 | amd_iommu_alias_table = (void *)__get_free_pages(GFP_KERNEL, | |
903 | get_order(alias_table_size)); | |
904 | if (amd_iommu_alias_table == NULL) | |
905 | goto free; | |
906 | ||
907 | /* IOMMU rlookup table - find the IOMMU for a specific device */ | |
908 | amd_iommu_rlookup_table = (void *)__get_free_pages(GFP_KERNEL, | |
909 | get_order(rlookup_table_size)); | |
910 | if (amd_iommu_rlookup_table == NULL) | |
911 | goto free; | |
912 | ||
913 | /* | |
914 | * Protection Domain table - maps devices to protection domains | |
915 | * This table has the same size as the rlookup_table | |
916 | */ | |
917 | amd_iommu_pd_table = (void *)__get_free_pages(GFP_KERNEL, | |
918 | get_order(rlookup_table_size)); | |
919 | if (amd_iommu_pd_table == NULL) | |
920 | goto free; | |
921 | ||
922 | amd_iommu_pd_alloc_bitmap = (void *)__get_free_pages(GFP_KERNEL, | |
923 | get_order(MAX_DOMAIN_ID/8)); | |
924 | if (amd_iommu_pd_alloc_bitmap == NULL) | |
925 | goto free; | |
926 | ||
927 | /* | |
928 | * memory is allocated now; initialize the device table with all zeroes | |
929 | * and let all alias entries point to itself | |
930 | */ | |
931 | memset(amd_iommu_dev_table, 0, dev_table_size); | |
932 | for (i = 0; i < amd_iommu_last_bdf; ++i) | |
933 | amd_iommu_alias_table[i] = i; | |
934 | ||
935 | memset(amd_iommu_pd_table, 0, rlookup_table_size); | |
936 | memset(amd_iommu_pd_alloc_bitmap, 0, MAX_DOMAIN_ID / 8); | |
937 | ||
938 | /* | |
939 | * never allocate domain 0 because its used as the non-allocated and | |
940 | * error value placeholder | |
941 | */ | |
942 | amd_iommu_pd_alloc_bitmap[0] = 1; | |
943 | ||
944 | /* | |
945 | * now the data structures are allocated and basically initialized | |
946 | * start the real acpi table scan | |
947 | */ | |
948 | ret = -ENODEV; | |
949 | if (acpi_table_parse("IVRS", init_iommu_all) != 0) | |
950 | goto free; | |
951 | ||
952 | if (acpi_table_parse("IVRS", init_memory_definitions) != 0) | |
953 | goto free; | |
954 | ||
8736197b JR |
955 | ret = amd_iommu_init_dma_ops(); |
956 | if (ret) | |
957 | goto free; | |
958 | ||
7441e9cb JR |
959 | ret = sysdev_class_register(&amd_iommu_sysdev_class); |
960 | if (ret) | |
961 | goto free; | |
962 | ||
963 | ret = sysdev_register(&device_amd_iommu); | |
964 | if (ret) | |
965 | goto free; | |
966 | ||
8736197b JR |
967 | enable_iommus(); |
968 | ||
fe74c9cf JR |
969 | printk(KERN_INFO "AMD IOMMU: aperture size is %d MB\n", |
970 | (1 << (amd_iommu_aperture_order-20))); | |
971 | ||
972 | printk(KERN_INFO "AMD IOMMU: device isolation "); | |
973 | if (amd_iommu_isolate) | |
974 | printk("enabled\n"); | |
975 | else | |
976 | printk("disabled\n"); | |
977 | ||
978 | out: | |
979 | return ret; | |
980 | ||
981 | free: | |
982 | if (amd_iommu_pd_alloc_bitmap) | |
983 | free_pages((unsigned long)amd_iommu_pd_alloc_bitmap, 1); | |
984 | ||
985 | if (amd_iommu_pd_table) | |
986 | free_pages((unsigned long)amd_iommu_pd_table, | |
987 | get_order(rlookup_table_size)); | |
988 | ||
989 | if (amd_iommu_rlookup_table) | |
990 | free_pages((unsigned long)amd_iommu_rlookup_table, | |
991 | get_order(rlookup_table_size)); | |
992 | ||
993 | if (amd_iommu_alias_table) | |
994 | free_pages((unsigned long)amd_iommu_alias_table, | |
995 | get_order(alias_table_size)); | |
996 | ||
997 | if (amd_iommu_dev_table) | |
998 | free_pages((unsigned long)amd_iommu_dev_table, | |
999 | get_order(dev_table_size)); | |
1000 | ||
1001 | free_iommu_all(); | |
1002 | ||
1003 | free_unity_maps(); | |
1004 | ||
1005 | goto out; | |
1006 | } | |
1007 | ||
b65233a9 JR |
1008 | /**************************************************************************** |
1009 | * | |
1010 | * Early detect code. This code runs at IOMMU detection time in the DMA | |
1011 | * layer. It just looks if there is an IVRS ACPI table to detect AMD | |
1012 | * IOMMUs | |
1013 | * | |
1014 | ****************************************************************************/ | |
ae7877de JR |
1015 | static int __init early_amd_iommu_detect(struct acpi_table_header *table) |
1016 | { | |
1017 | return 0; | |
1018 | } | |
1019 | ||
1020 | void __init amd_iommu_detect(void) | |
1021 | { | |
299a140d | 1022 | if (swiotlb || no_iommu || (iommu_detected && !gart_iommu_aperture)) |
ae7877de JR |
1023 | return; |
1024 | ||
ae7877de JR |
1025 | if (acpi_table_parse("IVRS", early_amd_iommu_detect) == 0) { |
1026 | iommu_detected = 1; | |
c1cbebee | 1027 | amd_iommu_detected = 1; |
92af4e29 | 1028 | #ifdef CONFIG_GART_IOMMU |
ae7877de JR |
1029 | gart_iommu_aperture_disabled = 1; |
1030 | gart_iommu_aperture = 0; | |
92af4e29 | 1031 | #endif |
ae7877de JR |
1032 | } |
1033 | } | |
1034 | ||
b65233a9 JR |
1035 | /**************************************************************************** |
1036 | * | |
1037 | * Parsing functions for the AMD IOMMU specific kernel command line | |
1038 | * options. | |
1039 | * | |
1040 | ****************************************************************************/ | |
1041 | ||
918ad6c5 JR |
1042 | static int __init parse_amd_iommu_options(char *str) |
1043 | { | |
1044 | for (; *str; ++str) { | |
918ad6c5 JR |
1045 | if (strcmp(str, "isolate") == 0) |
1046 | amd_iommu_isolate = 1; | |
1047 | } | |
1048 | ||
1049 | return 1; | |
1050 | } | |
1051 | ||
1052 | static int __init parse_amd_iommu_size_options(char *str) | |
1053 | { | |
1054 | for (; *str; ++str) { | |
1055 | if (strcmp(str, "32M") == 0) | |
1056 | amd_iommu_aperture_order = 25; | |
1057 | if (strcmp(str, "64M") == 0) | |
1058 | amd_iommu_aperture_order = 26; | |
1059 | if (strcmp(str, "128M") == 0) | |
1060 | amd_iommu_aperture_order = 27; | |
1061 | if (strcmp(str, "256M") == 0) | |
1062 | amd_iommu_aperture_order = 28; | |
1063 | if (strcmp(str, "512M") == 0) | |
1064 | amd_iommu_aperture_order = 29; | |
1065 | if (strcmp(str, "1G") == 0) | |
1066 | amd_iommu_aperture_order = 30; | |
1067 | } | |
1068 | ||
1069 | return 1; | |
1070 | } | |
1071 | ||
1072 | __setup("amd_iommu=", parse_amd_iommu_options); | |
1073 | __setup("amd_iommu_size=", parse_amd_iommu_size_options); |