Commit | Line | Data |
---|---|---|
b6c02715 | 1 | /* |
5d0d7156 | 2 | * Copyright (C) 2007-2010 Advanced Micro Devices, Inc. |
b6c02715 JR |
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> | |
cb41ed85 | 21 | #include <linux/pci-ats.h> |
a66022c4 | 22 | #include <linux/bitmap.h> |
5a0e3ad6 | 23 | #include <linux/slab.h> |
7f26508b | 24 | #include <linux/debugfs.h> |
b6c02715 | 25 | #include <linux/scatterlist.h> |
51491367 | 26 | #include <linux/dma-mapping.h> |
b6c02715 | 27 | #include <linux/iommu-helper.h> |
c156e347 | 28 | #include <linux/iommu.h> |
815b33fd | 29 | #include <linux/delay.h> |
b6c02715 | 30 | #include <asm/proto.h> |
46a7fa27 | 31 | #include <asm/iommu.h> |
1d9b16d1 | 32 | #include <asm/gart.h> |
27c2127a | 33 | #include <asm/dma.h> |
6a9401a7 | 34 | #include <asm/amd_iommu_proto.h> |
b6c02715 | 35 | #include <asm/amd_iommu_types.h> |
c6da992e | 36 | #include <asm/amd_iommu.h> |
b6c02715 JR |
37 | |
38 | #define CMD_SET_TYPE(cmd, t) ((cmd)->data[1] |= ((t) << 28)) | |
39 | ||
815b33fd | 40 | #define LOOP_TIMEOUT 100000 |
136f78a1 | 41 | |
b6c02715 JR |
42 | static DEFINE_RWLOCK(amd_iommu_devtable_lock); |
43 | ||
bd60b735 JR |
44 | /* A list of preallocated protection domains */ |
45 | static LIST_HEAD(iommu_pd_list); | |
46 | static DEFINE_SPINLOCK(iommu_pd_list_lock); | |
47 | ||
8fa5f802 JR |
48 | /* List of all available dev_data structures */ |
49 | static LIST_HEAD(dev_data_list); | |
50 | static DEFINE_SPINLOCK(dev_data_list_lock); | |
51 | ||
0feae533 JR |
52 | /* |
53 | * Domain for untranslated devices - only allocated | |
54 | * if iommu=pt passed on kernel cmd line. | |
55 | */ | |
56 | static struct protection_domain *pt_domain; | |
57 | ||
26961efe | 58 | static struct iommu_ops amd_iommu_ops; |
26961efe | 59 | |
431b2a20 JR |
60 | /* |
61 | * general struct to manage commands send to an IOMMU | |
62 | */ | |
d6449536 | 63 | struct iommu_cmd { |
b6c02715 JR |
64 | u32 data[4]; |
65 | }; | |
66 | ||
04bfdd84 | 67 | static void update_domain(struct protection_domain *domain); |
c1eee67b | 68 | |
15898bbc JR |
69 | /**************************************************************************** |
70 | * | |
71 | * Helper functions | |
72 | * | |
73 | ****************************************************************************/ | |
74 | ||
f62dda66 | 75 | static struct iommu_dev_data *alloc_dev_data(u16 devid) |
8fa5f802 JR |
76 | { |
77 | struct iommu_dev_data *dev_data; | |
78 | unsigned long flags; | |
79 | ||
80 | dev_data = kzalloc(sizeof(*dev_data), GFP_KERNEL); | |
81 | if (!dev_data) | |
82 | return NULL; | |
83 | ||
f62dda66 | 84 | dev_data->devid = devid; |
8fa5f802 JR |
85 | atomic_set(&dev_data->bind, 0); |
86 | ||
87 | spin_lock_irqsave(&dev_data_list_lock, flags); | |
88 | list_add_tail(&dev_data->dev_data_list, &dev_data_list); | |
89 | spin_unlock_irqrestore(&dev_data_list_lock, flags); | |
90 | ||
91 | return dev_data; | |
92 | } | |
93 | ||
94 | static void free_dev_data(struct iommu_dev_data *dev_data) | |
95 | { | |
96 | unsigned long flags; | |
97 | ||
98 | spin_lock_irqsave(&dev_data_list_lock, flags); | |
99 | list_del(&dev_data->dev_data_list); | |
100 | spin_unlock_irqrestore(&dev_data_list_lock, flags); | |
101 | ||
102 | kfree(dev_data); | |
103 | } | |
104 | ||
15898bbc JR |
105 | static inline u16 get_device_id(struct device *dev) |
106 | { | |
107 | struct pci_dev *pdev = to_pci_dev(dev); | |
108 | ||
109 | return calc_devid(pdev->bus->number, pdev->devfn); | |
110 | } | |
111 | ||
657cbb6b JR |
112 | static struct iommu_dev_data *get_dev_data(struct device *dev) |
113 | { | |
114 | return dev->archdata.iommu; | |
115 | } | |
116 | ||
71c70984 JR |
117 | /* |
118 | * In this function the list of preallocated protection domains is traversed to | |
119 | * find the domain for a specific device | |
120 | */ | |
121 | static struct dma_ops_domain *find_protection_domain(u16 devid) | |
122 | { | |
123 | struct dma_ops_domain *entry, *ret = NULL; | |
124 | unsigned long flags; | |
125 | u16 alias = amd_iommu_alias_table[devid]; | |
126 | ||
127 | if (list_empty(&iommu_pd_list)) | |
128 | return NULL; | |
129 | ||
130 | spin_lock_irqsave(&iommu_pd_list_lock, flags); | |
131 | ||
132 | list_for_each_entry(entry, &iommu_pd_list, list) { | |
133 | if (entry->target_dev == devid || | |
134 | entry->target_dev == alias) { | |
135 | ret = entry; | |
136 | break; | |
137 | } | |
138 | } | |
139 | ||
140 | spin_unlock_irqrestore(&iommu_pd_list_lock, flags); | |
141 | ||
142 | return ret; | |
143 | } | |
144 | ||
98fc5a69 JR |
145 | /* |
146 | * This function checks if the driver got a valid device from the caller to | |
147 | * avoid dereferencing invalid pointers. | |
148 | */ | |
149 | static bool check_device(struct device *dev) | |
150 | { | |
151 | u16 devid; | |
152 | ||
153 | if (!dev || !dev->dma_mask) | |
154 | return false; | |
155 | ||
156 | /* No device or no PCI device */ | |
339d3261 | 157 | if (dev->bus != &pci_bus_type) |
98fc5a69 JR |
158 | return false; |
159 | ||
160 | devid = get_device_id(dev); | |
161 | ||
162 | /* Out of our scope? */ | |
163 | if (devid > amd_iommu_last_bdf) | |
164 | return false; | |
165 | ||
166 | if (amd_iommu_rlookup_table[devid] == NULL) | |
167 | return false; | |
168 | ||
169 | return true; | |
170 | } | |
171 | ||
657cbb6b JR |
172 | static int iommu_init_device(struct device *dev) |
173 | { | |
174 | struct iommu_dev_data *dev_data; | |
175 | struct pci_dev *pdev; | |
8fa5f802 | 176 | u16 alias; |
657cbb6b JR |
177 | |
178 | if (dev->archdata.iommu) | |
179 | return 0; | |
180 | ||
f62dda66 | 181 | dev_data = alloc_dev_data(get_device_id(dev)); |
657cbb6b JR |
182 | if (!dev_data) |
183 | return -ENOMEM; | |
184 | ||
f62dda66 | 185 | alias = amd_iommu_alias_table[dev_data->devid]; |
2b02b091 JR |
186 | if (alias != dev_data->devid) { |
187 | pdev = pci_get_bus_and_slot(PCI_BUS(alias), alias & 0xff); | |
188 | if (pdev) | |
189 | dev_data->alias = &pdev->dev; | |
190 | else { | |
191 | free_dev_data(dev_data); | |
192 | return -ENOTSUPP; | |
193 | } | |
26018874 | 194 | } |
657cbb6b JR |
195 | |
196 | dev->archdata.iommu = dev_data; | |
197 | ||
657cbb6b JR |
198 | return 0; |
199 | } | |
200 | ||
26018874 JR |
201 | static void iommu_ignore_device(struct device *dev) |
202 | { | |
203 | u16 devid, alias; | |
204 | ||
205 | devid = get_device_id(dev); | |
206 | alias = amd_iommu_alias_table[devid]; | |
207 | ||
208 | memset(&amd_iommu_dev_table[devid], 0, sizeof(struct dev_table_entry)); | |
209 | memset(&amd_iommu_dev_table[alias], 0, sizeof(struct dev_table_entry)); | |
210 | ||
211 | amd_iommu_rlookup_table[devid] = NULL; | |
212 | amd_iommu_rlookup_table[alias] = NULL; | |
213 | } | |
214 | ||
657cbb6b JR |
215 | static void iommu_uninit_device(struct device *dev) |
216 | { | |
8fa5f802 JR |
217 | /* |
218 | * Nothing to do here - we keep dev_data around for unplugged devices | |
219 | * and reuse it when the device is re-plugged - not doing so would | |
220 | * introduce a ton of races. | |
221 | */ | |
657cbb6b | 222 | } |
b7cc9554 JR |
223 | |
224 | void __init amd_iommu_uninit_devices(void) | |
225 | { | |
8fa5f802 | 226 | struct iommu_dev_data *dev_data, *n; |
b7cc9554 JR |
227 | struct pci_dev *pdev = NULL; |
228 | ||
229 | for_each_pci_dev(pdev) { | |
230 | ||
231 | if (!check_device(&pdev->dev)) | |
232 | continue; | |
233 | ||
234 | iommu_uninit_device(&pdev->dev); | |
235 | } | |
8fa5f802 JR |
236 | |
237 | /* Free all of our dev_data structures */ | |
238 | list_for_each_entry_safe(dev_data, n, &dev_data_list, dev_data_list) | |
239 | free_dev_data(dev_data); | |
b7cc9554 JR |
240 | } |
241 | ||
242 | int __init amd_iommu_init_devices(void) | |
243 | { | |
244 | struct pci_dev *pdev = NULL; | |
245 | int ret = 0; | |
246 | ||
247 | for_each_pci_dev(pdev) { | |
248 | ||
249 | if (!check_device(&pdev->dev)) | |
250 | continue; | |
251 | ||
252 | ret = iommu_init_device(&pdev->dev); | |
26018874 JR |
253 | if (ret == -ENOTSUPP) |
254 | iommu_ignore_device(&pdev->dev); | |
255 | else if (ret) | |
b7cc9554 JR |
256 | goto out_free; |
257 | } | |
258 | ||
259 | return 0; | |
260 | ||
261 | out_free: | |
262 | ||
263 | amd_iommu_uninit_devices(); | |
264 | ||
265 | return ret; | |
266 | } | |
7f26508b JR |
267 | #ifdef CONFIG_AMD_IOMMU_STATS |
268 | ||
269 | /* | |
270 | * Initialization code for statistics collection | |
271 | */ | |
272 | ||
da49f6df | 273 | DECLARE_STATS_COUNTER(compl_wait); |
0f2a86f2 | 274 | DECLARE_STATS_COUNTER(cnt_map_single); |
146a6917 | 275 | DECLARE_STATS_COUNTER(cnt_unmap_single); |
d03f067a | 276 | DECLARE_STATS_COUNTER(cnt_map_sg); |
55877a6b | 277 | DECLARE_STATS_COUNTER(cnt_unmap_sg); |
c8f0fb36 | 278 | DECLARE_STATS_COUNTER(cnt_alloc_coherent); |
5d31ee7e | 279 | DECLARE_STATS_COUNTER(cnt_free_coherent); |
c1858976 | 280 | DECLARE_STATS_COUNTER(cross_page); |
f57d98ae | 281 | DECLARE_STATS_COUNTER(domain_flush_single); |
18811f55 | 282 | DECLARE_STATS_COUNTER(domain_flush_all); |
5774f7c5 | 283 | DECLARE_STATS_COUNTER(alloced_io_mem); |
8ecaf8f1 | 284 | DECLARE_STATS_COUNTER(total_map_requests); |
da49f6df | 285 | |
7f26508b | 286 | static struct dentry *stats_dir; |
7f26508b JR |
287 | static struct dentry *de_fflush; |
288 | ||
289 | static void amd_iommu_stats_add(struct __iommu_counter *cnt) | |
290 | { | |
291 | if (stats_dir == NULL) | |
292 | return; | |
293 | ||
294 | cnt->dent = debugfs_create_u64(cnt->name, 0444, stats_dir, | |
295 | &cnt->value); | |
296 | } | |
297 | ||
298 | static void amd_iommu_stats_init(void) | |
299 | { | |
300 | stats_dir = debugfs_create_dir("amd-iommu", NULL); | |
301 | if (stats_dir == NULL) | |
302 | return; | |
303 | ||
7f26508b JR |
304 | de_fflush = debugfs_create_bool("fullflush", 0444, stats_dir, |
305 | (u32 *)&amd_iommu_unmap_flush); | |
da49f6df JR |
306 | |
307 | amd_iommu_stats_add(&compl_wait); | |
0f2a86f2 | 308 | amd_iommu_stats_add(&cnt_map_single); |
146a6917 | 309 | amd_iommu_stats_add(&cnt_unmap_single); |
d03f067a | 310 | amd_iommu_stats_add(&cnt_map_sg); |
55877a6b | 311 | amd_iommu_stats_add(&cnt_unmap_sg); |
c8f0fb36 | 312 | amd_iommu_stats_add(&cnt_alloc_coherent); |
5d31ee7e | 313 | amd_iommu_stats_add(&cnt_free_coherent); |
c1858976 | 314 | amd_iommu_stats_add(&cross_page); |
f57d98ae | 315 | amd_iommu_stats_add(&domain_flush_single); |
18811f55 | 316 | amd_iommu_stats_add(&domain_flush_all); |
5774f7c5 | 317 | amd_iommu_stats_add(&alloced_io_mem); |
8ecaf8f1 | 318 | amd_iommu_stats_add(&total_map_requests); |
7f26508b JR |
319 | } |
320 | ||
321 | #endif | |
322 | ||
a80dc3e0 JR |
323 | /**************************************************************************** |
324 | * | |
325 | * Interrupt handling functions | |
326 | * | |
327 | ****************************************************************************/ | |
328 | ||
e3e59876 JR |
329 | static void dump_dte_entry(u16 devid) |
330 | { | |
331 | int i; | |
332 | ||
333 | for (i = 0; i < 8; ++i) | |
334 | pr_err("AMD-Vi: DTE[%d]: %08x\n", i, | |
335 | amd_iommu_dev_table[devid].data[i]); | |
336 | } | |
337 | ||
945b4ac4 JR |
338 | static void dump_command(unsigned long phys_addr) |
339 | { | |
340 | struct iommu_cmd *cmd = phys_to_virt(phys_addr); | |
341 | int i; | |
342 | ||
343 | for (i = 0; i < 4; ++i) | |
344 | pr_err("AMD-Vi: CMD[%d]: %08x\n", i, cmd->data[i]); | |
345 | } | |
346 | ||
a345b23b | 347 | static void iommu_print_event(struct amd_iommu *iommu, void *__evt) |
90008ee4 JR |
348 | { |
349 | u32 *event = __evt; | |
350 | int type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK; | |
351 | int devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK; | |
352 | int domid = (event[1] >> EVENT_DOMID_SHIFT) & EVENT_DOMID_MASK; | |
353 | int flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK; | |
354 | u64 address = (u64)(((u64)event[3]) << 32) | event[2]; | |
355 | ||
4c6f40d4 | 356 | printk(KERN_ERR "AMD-Vi: Event logged ["); |
90008ee4 JR |
357 | |
358 | switch (type) { | |
359 | case EVENT_TYPE_ILL_DEV: | |
360 | printk("ILLEGAL_DEV_TABLE_ENTRY device=%02x:%02x.%x " | |
361 | "address=0x%016llx flags=0x%04x]\n", | |
362 | PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid), | |
363 | address, flags); | |
e3e59876 | 364 | dump_dte_entry(devid); |
90008ee4 JR |
365 | break; |
366 | case EVENT_TYPE_IO_FAULT: | |
367 | printk("IO_PAGE_FAULT device=%02x:%02x.%x " | |
368 | "domain=0x%04x address=0x%016llx flags=0x%04x]\n", | |
369 | PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid), | |
370 | domid, address, flags); | |
371 | break; | |
372 | case EVENT_TYPE_DEV_TAB_ERR: | |
373 | printk("DEV_TAB_HARDWARE_ERROR device=%02x:%02x.%x " | |
374 | "address=0x%016llx flags=0x%04x]\n", | |
375 | PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid), | |
376 | address, flags); | |
377 | break; | |
378 | case EVENT_TYPE_PAGE_TAB_ERR: | |
379 | printk("PAGE_TAB_HARDWARE_ERROR device=%02x:%02x.%x " | |
380 | "domain=0x%04x address=0x%016llx flags=0x%04x]\n", | |
381 | PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid), | |
382 | domid, address, flags); | |
383 | break; | |
384 | case EVENT_TYPE_ILL_CMD: | |
385 | printk("ILLEGAL_COMMAND_ERROR address=0x%016llx]\n", address); | |
945b4ac4 | 386 | dump_command(address); |
90008ee4 JR |
387 | break; |
388 | case EVENT_TYPE_CMD_HARD_ERR: | |
389 | printk("COMMAND_HARDWARE_ERROR address=0x%016llx " | |
390 | "flags=0x%04x]\n", address, flags); | |
391 | break; | |
392 | case EVENT_TYPE_IOTLB_INV_TO: | |
393 | printk("IOTLB_INV_TIMEOUT device=%02x:%02x.%x " | |
394 | "address=0x%016llx]\n", | |
395 | PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid), | |
396 | address); | |
397 | break; | |
398 | case EVENT_TYPE_INV_DEV_REQ: | |
399 | printk("INVALID_DEVICE_REQUEST device=%02x:%02x.%x " | |
400 | "address=0x%016llx flags=0x%04x]\n", | |
401 | PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid), | |
402 | address, flags); | |
403 | break; | |
404 | default: | |
405 | printk(KERN_ERR "UNKNOWN type=0x%02x]\n", type); | |
406 | } | |
407 | } | |
408 | ||
409 | static void iommu_poll_events(struct amd_iommu *iommu) | |
410 | { | |
411 | u32 head, tail; | |
412 | unsigned long flags; | |
413 | ||
414 | spin_lock_irqsave(&iommu->lock, flags); | |
415 | ||
416 | head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET); | |
417 | tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET); | |
418 | ||
419 | while (head != tail) { | |
a345b23b | 420 | iommu_print_event(iommu, iommu->evt_buf + head); |
90008ee4 JR |
421 | head = (head + EVENT_ENTRY_SIZE) % iommu->evt_buf_size; |
422 | } | |
423 | ||
424 | writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET); | |
425 | ||
426 | spin_unlock_irqrestore(&iommu->lock, flags); | |
427 | } | |
428 | ||
72fe00f0 | 429 | irqreturn_t amd_iommu_int_thread(int irq, void *data) |
a80dc3e0 | 430 | { |
90008ee4 JR |
431 | struct amd_iommu *iommu; |
432 | ||
3bd22172 | 433 | for_each_iommu(iommu) |
90008ee4 JR |
434 | iommu_poll_events(iommu); |
435 | ||
436 | return IRQ_HANDLED; | |
a80dc3e0 JR |
437 | } |
438 | ||
72fe00f0 JR |
439 | irqreturn_t amd_iommu_int_handler(int irq, void *data) |
440 | { | |
441 | return IRQ_WAKE_THREAD; | |
442 | } | |
443 | ||
431b2a20 JR |
444 | /**************************************************************************** |
445 | * | |
446 | * IOMMU command queuing functions | |
447 | * | |
448 | ****************************************************************************/ | |
449 | ||
ac0ea6e9 JR |
450 | static int wait_on_sem(volatile u64 *sem) |
451 | { | |
452 | int i = 0; | |
453 | ||
454 | while (*sem == 0 && i < LOOP_TIMEOUT) { | |
455 | udelay(1); | |
456 | i += 1; | |
457 | } | |
458 | ||
459 | if (i == LOOP_TIMEOUT) { | |
460 | pr_alert("AMD-Vi: Completion-Wait loop timed out\n"); | |
461 | return -EIO; | |
462 | } | |
463 | ||
464 | return 0; | |
465 | } | |
466 | ||
467 | static void copy_cmd_to_buffer(struct amd_iommu *iommu, | |
468 | struct iommu_cmd *cmd, | |
469 | u32 tail) | |
a19ae1ec | 470 | { |
a19ae1ec JR |
471 | u8 *target; |
472 | ||
8a7c5ef3 | 473 | target = iommu->cmd_buf + tail; |
ac0ea6e9 JR |
474 | tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size; |
475 | ||
476 | /* Copy command to buffer */ | |
477 | memcpy(target, cmd, sizeof(*cmd)); | |
478 | ||
479 | /* Tell the IOMMU about it */ | |
a19ae1ec | 480 | writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET); |
ac0ea6e9 | 481 | } |
a19ae1ec | 482 | |
815b33fd | 483 | static void build_completion_wait(struct iommu_cmd *cmd, u64 address) |
ded46737 | 484 | { |
815b33fd JR |
485 | WARN_ON(address & 0x7ULL); |
486 | ||
ded46737 | 487 | memset(cmd, 0, sizeof(*cmd)); |
815b33fd JR |
488 | cmd->data[0] = lower_32_bits(__pa(address)) | CMD_COMPL_WAIT_STORE_MASK; |
489 | cmd->data[1] = upper_32_bits(__pa(address)); | |
490 | cmd->data[2] = 1; | |
ded46737 JR |
491 | CMD_SET_TYPE(cmd, CMD_COMPL_WAIT); |
492 | } | |
493 | ||
94fe79e2 JR |
494 | static void build_inv_dte(struct iommu_cmd *cmd, u16 devid) |
495 | { | |
496 | memset(cmd, 0, sizeof(*cmd)); | |
497 | cmd->data[0] = devid; | |
498 | CMD_SET_TYPE(cmd, CMD_INV_DEV_ENTRY); | |
499 | } | |
500 | ||
11b6402c JR |
501 | static void build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address, |
502 | size_t size, u16 domid, int pde) | |
503 | { | |
504 | u64 pages; | |
505 | int s; | |
506 | ||
507 | pages = iommu_num_pages(address, size, PAGE_SIZE); | |
508 | s = 0; | |
509 | ||
510 | if (pages > 1) { | |
511 | /* | |
512 | * If we have to flush more than one page, flush all | |
513 | * TLB entries for this domain | |
514 | */ | |
515 | address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS; | |
516 | s = 1; | |
517 | } | |
518 | ||
519 | address &= PAGE_MASK; | |
520 | ||
521 | memset(cmd, 0, sizeof(*cmd)); | |
522 | cmd->data[1] |= domid; | |
523 | cmd->data[2] = lower_32_bits(address); | |
524 | cmd->data[3] = upper_32_bits(address); | |
525 | CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES); | |
526 | if (s) /* size bit - we flush more than one 4kb page */ | |
527 | cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK; | |
528 | if (pde) /* PDE bit - we wan't flush everything not only the PTEs */ | |
529 | cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK; | |
530 | } | |
531 | ||
cb41ed85 JR |
532 | static void build_inv_iotlb_pages(struct iommu_cmd *cmd, u16 devid, int qdep, |
533 | u64 address, size_t size) | |
534 | { | |
535 | u64 pages; | |
536 | int s; | |
537 | ||
538 | pages = iommu_num_pages(address, size, PAGE_SIZE); | |
539 | s = 0; | |
540 | ||
541 | if (pages > 1) { | |
542 | /* | |
543 | * If we have to flush more than one page, flush all | |
544 | * TLB entries for this domain | |
545 | */ | |
546 | address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS; | |
547 | s = 1; | |
548 | } | |
549 | ||
550 | address &= PAGE_MASK; | |
551 | ||
552 | memset(cmd, 0, sizeof(*cmd)); | |
553 | cmd->data[0] = devid; | |
554 | cmd->data[0] |= (qdep & 0xff) << 24; | |
555 | cmd->data[1] = devid; | |
556 | cmd->data[2] = lower_32_bits(address); | |
557 | cmd->data[3] = upper_32_bits(address); | |
558 | CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES); | |
559 | if (s) | |
560 | cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK; | |
561 | } | |
562 | ||
58fc7f14 JR |
563 | static void build_inv_all(struct iommu_cmd *cmd) |
564 | { | |
565 | memset(cmd, 0, sizeof(*cmd)); | |
566 | CMD_SET_TYPE(cmd, CMD_INV_ALL); | |
a19ae1ec JR |
567 | } |
568 | ||
431b2a20 | 569 | /* |
431b2a20 | 570 | * Writes the command to the IOMMUs command buffer and informs the |
ac0ea6e9 | 571 | * hardware about the new command. |
431b2a20 | 572 | */ |
d6449536 | 573 | static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd) |
a19ae1ec | 574 | { |
ac0ea6e9 | 575 | u32 left, tail, head, next_tail; |
a19ae1ec | 576 | unsigned long flags; |
a19ae1ec | 577 | |
549c90dc | 578 | WARN_ON(iommu->cmd_buf_size & CMD_BUFFER_UNINITIALIZED); |
ac0ea6e9 JR |
579 | |
580 | again: | |
a19ae1ec | 581 | spin_lock_irqsave(&iommu->lock, flags); |
a19ae1ec | 582 | |
ac0ea6e9 JR |
583 | head = readl(iommu->mmio_base + MMIO_CMD_HEAD_OFFSET); |
584 | tail = readl(iommu->mmio_base + MMIO_CMD_TAIL_OFFSET); | |
585 | next_tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size; | |
586 | left = (head - next_tail) % iommu->cmd_buf_size; | |
a19ae1ec | 587 | |
ac0ea6e9 JR |
588 | if (left <= 2) { |
589 | struct iommu_cmd sync_cmd; | |
590 | volatile u64 sem = 0; | |
591 | int ret; | |
8d201968 | 592 | |
ac0ea6e9 JR |
593 | build_completion_wait(&sync_cmd, (u64)&sem); |
594 | copy_cmd_to_buffer(iommu, &sync_cmd, tail); | |
da49f6df | 595 | |
ac0ea6e9 JR |
596 | spin_unlock_irqrestore(&iommu->lock, flags); |
597 | ||
598 | if ((ret = wait_on_sem(&sem)) != 0) | |
599 | return ret; | |
600 | ||
601 | goto again; | |
8d201968 JR |
602 | } |
603 | ||
ac0ea6e9 JR |
604 | copy_cmd_to_buffer(iommu, cmd, tail); |
605 | ||
606 | /* We need to sync now to make sure all commands are processed */ | |
815b33fd | 607 | iommu->need_sync = true; |
ac0ea6e9 | 608 | |
a19ae1ec | 609 | spin_unlock_irqrestore(&iommu->lock, flags); |
8d201968 | 610 | |
815b33fd | 611 | return 0; |
8d201968 JR |
612 | } |
613 | ||
614 | /* | |
615 | * This function queues a completion wait command into the command | |
616 | * buffer of an IOMMU | |
617 | */ | |
a19ae1ec | 618 | static int iommu_completion_wait(struct amd_iommu *iommu) |
8d201968 JR |
619 | { |
620 | struct iommu_cmd cmd; | |
815b33fd | 621 | volatile u64 sem = 0; |
ac0ea6e9 | 622 | int ret; |
8d201968 | 623 | |
09ee17eb | 624 | if (!iommu->need_sync) |
815b33fd | 625 | return 0; |
09ee17eb | 626 | |
815b33fd | 627 | build_completion_wait(&cmd, (u64)&sem); |
a19ae1ec | 628 | |
815b33fd | 629 | ret = iommu_queue_command(iommu, &cmd); |
a19ae1ec | 630 | if (ret) |
815b33fd | 631 | return ret; |
8d201968 | 632 | |
ac0ea6e9 | 633 | return wait_on_sem(&sem); |
8d201968 JR |
634 | } |
635 | ||
d8c13085 | 636 | static int iommu_flush_dte(struct amd_iommu *iommu, u16 devid) |
a19ae1ec | 637 | { |
d8c13085 | 638 | struct iommu_cmd cmd; |
a19ae1ec | 639 | |
d8c13085 | 640 | build_inv_dte(&cmd, devid); |
7e4f88da | 641 | |
d8c13085 JR |
642 | return iommu_queue_command(iommu, &cmd); |
643 | } | |
09ee17eb | 644 | |
7d0c5cc5 JR |
645 | static void iommu_flush_dte_all(struct amd_iommu *iommu) |
646 | { | |
647 | u32 devid; | |
09ee17eb | 648 | |
7d0c5cc5 JR |
649 | for (devid = 0; devid <= 0xffff; ++devid) |
650 | iommu_flush_dte(iommu, devid); | |
a19ae1ec | 651 | |
7d0c5cc5 JR |
652 | iommu_completion_wait(iommu); |
653 | } | |
84df8175 | 654 | |
7d0c5cc5 JR |
655 | /* |
656 | * This function uses heavy locking and may disable irqs for some time. But | |
657 | * this is no issue because it is only called during resume. | |
658 | */ | |
659 | static void iommu_flush_tlb_all(struct amd_iommu *iommu) | |
660 | { | |
661 | u32 dom_id; | |
a19ae1ec | 662 | |
7d0c5cc5 JR |
663 | for (dom_id = 0; dom_id <= 0xffff; ++dom_id) { |
664 | struct iommu_cmd cmd; | |
665 | build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, | |
666 | dom_id, 1); | |
667 | iommu_queue_command(iommu, &cmd); | |
668 | } | |
8eed9833 | 669 | |
7d0c5cc5 | 670 | iommu_completion_wait(iommu); |
a19ae1ec JR |
671 | } |
672 | ||
58fc7f14 | 673 | static void iommu_flush_all(struct amd_iommu *iommu) |
0518a3a4 | 674 | { |
58fc7f14 | 675 | struct iommu_cmd cmd; |
0518a3a4 | 676 | |
58fc7f14 | 677 | build_inv_all(&cmd); |
0518a3a4 | 678 | |
58fc7f14 JR |
679 | iommu_queue_command(iommu, &cmd); |
680 | iommu_completion_wait(iommu); | |
681 | } | |
682 | ||
7d0c5cc5 JR |
683 | void iommu_flush_all_caches(struct amd_iommu *iommu) |
684 | { | |
58fc7f14 JR |
685 | if (iommu_feature(iommu, FEATURE_IA)) { |
686 | iommu_flush_all(iommu); | |
687 | } else { | |
688 | iommu_flush_dte_all(iommu); | |
689 | iommu_flush_tlb_all(iommu); | |
0518a3a4 JR |
690 | } |
691 | } | |
692 | ||
431b2a20 | 693 | /* |
cb41ed85 | 694 | * Command send function for flushing on-device TLB |
431b2a20 | 695 | */ |
6c542047 JR |
696 | static int device_flush_iotlb(struct iommu_dev_data *dev_data, |
697 | u64 address, size_t size) | |
3fa43655 JR |
698 | { |
699 | struct amd_iommu *iommu; | |
b00d3bcf | 700 | struct iommu_cmd cmd; |
cb41ed85 | 701 | int qdep; |
3fa43655 | 702 | |
ea61cddb JR |
703 | qdep = dev_data->ats.qdep; |
704 | iommu = amd_iommu_rlookup_table[dev_data->devid]; | |
3fa43655 | 705 | |
ea61cddb | 706 | build_inv_iotlb_pages(&cmd, dev_data->devid, qdep, address, size); |
b00d3bcf JR |
707 | |
708 | return iommu_queue_command(iommu, &cmd); | |
3fa43655 JR |
709 | } |
710 | ||
431b2a20 | 711 | /* |
431b2a20 | 712 | * Command send function for invalidating a device table entry |
431b2a20 | 713 | */ |
6c542047 | 714 | static int device_flush_dte(struct iommu_dev_data *dev_data) |
a19ae1ec | 715 | { |
3fa43655 | 716 | struct amd_iommu *iommu; |
ee2fa743 | 717 | int ret; |
a19ae1ec | 718 | |
6c542047 | 719 | iommu = amd_iommu_rlookup_table[dev_data->devid]; |
a19ae1ec | 720 | |
f62dda66 | 721 | ret = iommu_flush_dte(iommu, dev_data->devid); |
cb41ed85 JR |
722 | if (ret) |
723 | return ret; | |
724 | ||
ea61cddb | 725 | if (dev_data->ats.enabled) |
6c542047 | 726 | ret = device_flush_iotlb(dev_data, 0, ~0UL); |
ee2fa743 | 727 | |
ee2fa743 | 728 | return ret; |
a19ae1ec JR |
729 | } |
730 | ||
431b2a20 JR |
731 | /* |
732 | * TLB invalidation function which is called from the mapping functions. | |
733 | * It invalidates a single PTE if the range to flush is within a single | |
734 | * page. Otherwise it flushes the whole TLB of the IOMMU. | |
735 | */ | |
17b124bf JR |
736 | static void __domain_flush_pages(struct protection_domain *domain, |
737 | u64 address, size_t size, int pde) | |
a19ae1ec | 738 | { |
cb41ed85 | 739 | struct iommu_dev_data *dev_data; |
11b6402c JR |
740 | struct iommu_cmd cmd; |
741 | int ret = 0, i; | |
a19ae1ec | 742 | |
11b6402c | 743 | build_inv_iommu_pages(&cmd, address, size, domain->id, pde); |
999ba417 | 744 | |
6de8ad9b JR |
745 | for (i = 0; i < amd_iommus_present; ++i) { |
746 | if (!domain->dev_iommu[i]) | |
747 | continue; | |
748 | ||
749 | /* | |
750 | * Devices of this domain are behind this IOMMU | |
751 | * We need a TLB flush | |
752 | */ | |
11b6402c | 753 | ret |= iommu_queue_command(amd_iommus[i], &cmd); |
6de8ad9b JR |
754 | } |
755 | ||
cb41ed85 | 756 | list_for_each_entry(dev_data, &domain->dev_list, list) { |
cb41ed85 | 757 | |
ea61cddb | 758 | if (!dev_data->ats.enabled) |
cb41ed85 JR |
759 | continue; |
760 | ||
6c542047 | 761 | ret |= device_flush_iotlb(dev_data, address, size); |
cb41ed85 JR |
762 | } |
763 | ||
11b6402c | 764 | WARN_ON(ret); |
6de8ad9b JR |
765 | } |
766 | ||
17b124bf JR |
767 | static void domain_flush_pages(struct protection_domain *domain, |
768 | u64 address, size_t size) | |
6de8ad9b | 769 | { |
17b124bf | 770 | __domain_flush_pages(domain, address, size, 0); |
a19ae1ec | 771 | } |
b6c02715 | 772 | |
1c655773 | 773 | /* Flush the whole IO/TLB for a given protection domain */ |
17b124bf | 774 | static void domain_flush_tlb(struct protection_domain *domain) |
1c655773 | 775 | { |
17b124bf | 776 | __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 0); |
1c655773 JR |
777 | } |
778 | ||
42a49f96 | 779 | /* Flush the whole IO/TLB for a given protection domain - including PDE */ |
17b124bf | 780 | static void domain_flush_tlb_pde(struct protection_domain *domain) |
42a49f96 | 781 | { |
17b124bf | 782 | __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 1); |
42a49f96 CW |
783 | } |
784 | ||
17b124bf | 785 | static void domain_flush_complete(struct protection_domain *domain) |
b00d3bcf | 786 | { |
17b124bf | 787 | int i; |
18811f55 | 788 | |
17b124bf JR |
789 | for (i = 0; i < amd_iommus_present; ++i) { |
790 | if (!domain->dev_iommu[i]) | |
791 | continue; | |
bfd1be18 | 792 | |
17b124bf JR |
793 | /* |
794 | * Devices of this domain are behind this IOMMU | |
795 | * We need to wait for completion of all commands. | |
796 | */ | |
797 | iommu_completion_wait(amd_iommus[i]); | |
bfd1be18 | 798 | } |
e394d72a JR |
799 | } |
800 | ||
b00d3bcf | 801 | |
09b42804 | 802 | /* |
b00d3bcf | 803 | * This function flushes the DTEs for all devices in domain |
09b42804 | 804 | */ |
17b124bf | 805 | static void domain_flush_devices(struct protection_domain *domain) |
e394d72a | 806 | { |
b00d3bcf | 807 | struct iommu_dev_data *dev_data; |
09b42804 JR |
808 | unsigned long flags; |
809 | ||
b00d3bcf | 810 | spin_lock_irqsave(&domain->lock, flags); |
b26e81b8 | 811 | |
b00d3bcf | 812 | list_for_each_entry(dev_data, &domain->dev_list, list) |
6c542047 | 813 | device_flush_dte(dev_data); |
b26e81b8 | 814 | |
b00d3bcf | 815 | spin_unlock_irqrestore(&domain->lock, flags); |
a345b23b JR |
816 | } |
817 | ||
431b2a20 JR |
818 | /**************************************************************************** |
819 | * | |
820 | * The functions below are used the create the page table mappings for | |
821 | * unity mapped regions. | |
822 | * | |
823 | ****************************************************************************/ | |
824 | ||
308973d3 JR |
825 | /* |
826 | * This function is used to add another level to an IO page table. Adding | |
827 | * another level increases the size of the address space by 9 bits to a size up | |
828 | * to 64 bits. | |
829 | */ | |
830 | static bool increase_address_space(struct protection_domain *domain, | |
831 | gfp_t gfp) | |
832 | { | |
833 | u64 *pte; | |
834 | ||
835 | if (domain->mode == PAGE_MODE_6_LEVEL) | |
836 | /* address space already 64 bit large */ | |
837 | return false; | |
838 | ||
839 | pte = (void *)get_zeroed_page(gfp); | |
840 | if (!pte) | |
841 | return false; | |
842 | ||
843 | *pte = PM_LEVEL_PDE(domain->mode, | |
844 | virt_to_phys(domain->pt_root)); | |
845 | domain->pt_root = pte; | |
846 | domain->mode += 1; | |
847 | domain->updated = true; | |
848 | ||
849 | return true; | |
850 | } | |
851 | ||
852 | static u64 *alloc_pte(struct protection_domain *domain, | |
853 | unsigned long address, | |
cbb9d729 | 854 | unsigned long page_size, |
308973d3 JR |
855 | u64 **pte_page, |
856 | gfp_t gfp) | |
857 | { | |
cbb9d729 | 858 | int level, end_lvl; |
308973d3 | 859 | u64 *pte, *page; |
cbb9d729 JR |
860 | |
861 | BUG_ON(!is_power_of_2(page_size)); | |
308973d3 JR |
862 | |
863 | while (address > PM_LEVEL_SIZE(domain->mode)) | |
864 | increase_address_space(domain, gfp); | |
865 | ||
cbb9d729 JR |
866 | level = domain->mode - 1; |
867 | pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)]; | |
868 | address = PAGE_SIZE_ALIGN(address, page_size); | |
869 | end_lvl = PAGE_SIZE_LEVEL(page_size); | |
308973d3 JR |
870 | |
871 | while (level > end_lvl) { | |
872 | if (!IOMMU_PTE_PRESENT(*pte)) { | |
873 | page = (u64 *)get_zeroed_page(gfp); | |
874 | if (!page) | |
875 | return NULL; | |
876 | *pte = PM_LEVEL_PDE(level, virt_to_phys(page)); | |
877 | } | |
878 | ||
cbb9d729 JR |
879 | /* No level skipping support yet */ |
880 | if (PM_PTE_LEVEL(*pte) != level) | |
881 | return NULL; | |
882 | ||
308973d3 JR |
883 | level -= 1; |
884 | ||
885 | pte = IOMMU_PTE_PAGE(*pte); | |
886 | ||
887 | if (pte_page && level == end_lvl) | |
888 | *pte_page = pte; | |
889 | ||
890 | pte = &pte[PM_LEVEL_INDEX(level, address)]; | |
891 | } | |
892 | ||
893 | return pte; | |
894 | } | |
895 | ||
896 | /* | |
897 | * This function checks if there is a PTE for a given dma address. If | |
898 | * there is one, it returns the pointer to it. | |
899 | */ | |
24cd7723 | 900 | static u64 *fetch_pte(struct protection_domain *domain, unsigned long address) |
308973d3 JR |
901 | { |
902 | int level; | |
903 | u64 *pte; | |
904 | ||
24cd7723 JR |
905 | if (address > PM_LEVEL_SIZE(domain->mode)) |
906 | return NULL; | |
907 | ||
908 | level = domain->mode - 1; | |
909 | pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)]; | |
308973d3 | 910 | |
24cd7723 JR |
911 | while (level > 0) { |
912 | ||
913 | /* Not Present */ | |
308973d3 JR |
914 | if (!IOMMU_PTE_PRESENT(*pte)) |
915 | return NULL; | |
916 | ||
24cd7723 JR |
917 | /* Large PTE */ |
918 | if (PM_PTE_LEVEL(*pte) == 0x07) { | |
919 | unsigned long pte_mask, __pte; | |
920 | ||
921 | /* | |
922 | * If we have a series of large PTEs, make | |
923 | * sure to return a pointer to the first one. | |
924 | */ | |
925 | pte_mask = PTE_PAGE_SIZE(*pte); | |
926 | pte_mask = ~((PAGE_SIZE_PTE_COUNT(pte_mask) << 3) - 1); | |
927 | __pte = ((unsigned long)pte) & pte_mask; | |
928 | ||
929 | return (u64 *)__pte; | |
930 | } | |
931 | ||
932 | /* No level skipping support yet */ | |
933 | if (PM_PTE_LEVEL(*pte) != level) | |
934 | return NULL; | |
935 | ||
308973d3 JR |
936 | level -= 1; |
937 | ||
24cd7723 | 938 | /* Walk to the next level */ |
308973d3 JR |
939 | pte = IOMMU_PTE_PAGE(*pte); |
940 | pte = &pte[PM_LEVEL_INDEX(level, address)]; | |
308973d3 JR |
941 | } |
942 | ||
943 | return pte; | |
944 | } | |
945 | ||
431b2a20 JR |
946 | /* |
947 | * Generic mapping functions. It maps a physical address into a DMA | |
948 | * address space. It allocates the page table pages if necessary. | |
949 | * In the future it can be extended to a generic mapping function | |
950 | * supporting all features of AMD IOMMU page tables like level skipping | |
951 | * and full 64 bit address spaces. | |
952 | */ | |
38e817fe JR |
953 | static int iommu_map_page(struct protection_domain *dom, |
954 | unsigned long bus_addr, | |
955 | unsigned long phys_addr, | |
abdc5eb3 | 956 | int prot, |
cbb9d729 | 957 | unsigned long page_size) |
bd0e5211 | 958 | { |
8bda3092 | 959 | u64 __pte, *pte; |
cbb9d729 | 960 | int i, count; |
abdc5eb3 | 961 | |
bad1cac2 | 962 | if (!(prot & IOMMU_PROT_MASK)) |
bd0e5211 JR |
963 | return -EINVAL; |
964 | ||
cbb9d729 JR |
965 | bus_addr = PAGE_ALIGN(bus_addr); |
966 | phys_addr = PAGE_ALIGN(phys_addr); | |
967 | count = PAGE_SIZE_PTE_COUNT(page_size); | |
968 | pte = alloc_pte(dom, bus_addr, page_size, NULL, GFP_KERNEL); | |
969 | ||
970 | for (i = 0; i < count; ++i) | |
971 | if (IOMMU_PTE_PRESENT(pte[i])) | |
972 | return -EBUSY; | |
bd0e5211 | 973 | |
cbb9d729 JR |
974 | if (page_size > PAGE_SIZE) { |
975 | __pte = PAGE_SIZE_PTE(phys_addr, page_size); | |
976 | __pte |= PM_LEVEL_ENC(7) | IOMMU_PTE_P | IOMMU_PTE_FC; | |
977 | } else | |
978 | __pte = phys_addr | IOMMU_PTE_P | IOMMU_PTE_FC; | |
bd0e5211 | 979 | |
bd0e5211 JR |
980 | if (prot & IOMMU_PROT_IR) |
981 | __pte |= IOMMU_PTE_IR; | |
982 | if (prot & IOMMU_PROT_IW) | |
983 | __pte |= IOMMU_PTE_IW; | |
984 | ||
cbb9d729 JR |
985 | for (i = 0; i < count; ++i) |
986 | pte[i] = __pte; | |
bd0e5211 | 987 | |
04bfdd84 JR |
988 | update_domain(dom); |
989 | ||
bd0e5211 JR |
990 | return 0; |
991 | } | |
992 | ||
24cd7723 JR |
993 | static unsigned long iommu_unmap_page(struct protection_domain *dom, |
994 | unsigned long bus_addr, | |
995 | unsigned long page_size) | |
eb74ff6c | 996 | { |
24cd7723 JR |
997 | unsigned long long unmap_size, unmapped; |
998 | u64 *pte; | |
999 | ||
1000 | BUG_ON(!is_power_of_2(page_size)); | |
1001 | ||
1002 | unmapped = 0; | |
eb74ff6c | 1003 | |
24cd7723 JR |
1004 | while (unmapped < page_size) { |
1005 | ||
1006 | pte = fetch_pte(dom, bus_addr); | |
1007 | ||
1008 | if (!pte) { | |
1009 | /* | |
1010 | * No PTE for this address | |
1011 | * move forward in 4kb steps | |
1012 | */ | |
1013 | unmap_size = PAGE_SIZE; | |
1014 | } else if (PM_PTE_LEVEL(*pte) == 0) { | |
1015 | /* 4kb PTE found for this address */ | |
1016 | unmap_size = PAGE_SIZE; | |
1017 | *pte = 0ULL; | |
1018 | } else { | |
1019 | int count, i; | |
1020 | ||
1021 | /* Large PTE found which maps this address */ | |
1022 | unmap_size = PTE_PAGE_SIZE(*pte); | |
1023 | count = PAGE_SIZE_PTE_COUNT(unmap_size); | |
1024 | for (i = 0; i < count; i++) | |
1025 | pte[i] = 0ULL; | |
1026 | } | |
1027 | ||
1028 | bus_addr = (bus_addr & ~(unmap_size - 1)) + unmap_size; | |
1029 | unmapped += unmap_size; | |
1030 | } | |
1031 | ||
1032 | BUG_ON(!is_power_of_2(unmapped)); | |
eb74ff6c | 1033 | |
24cd7723 | 1034 | return unmapped; |
eb74ff6c | 1035 | } |
eb74ff6c | 1036 | |
431b2a20 JR |
1037 | /* |
1038 | * This function checks if a specific unity mapping entry is needed for | |
1039 | * this specific IOMMU. | |
1040 | */ | |
bd0e5211 JR |
1041 | static int iommu_for_unity_map(struct amd_iommu *iommu, |
1042 | struct unity_map_entry *entry) | |
1043 | { | |
1044 | u16 bdf, i; | |
1045 | ||
1046 | for (i = entry->devid_start; i <= entry->devid_end; ++i) { | |
1047 | bdf = amd_iommu_alias_table[i]; | |
1048 | if (amd_iommu_rlookup_table[bdf] == iommu) | |
1049 | return 1; | |
1050 | } | |
1051 | ||
1052 | return 0; | |
1053 | } | |
1054 | ||
431b2a20 JR |
1055 | /* |
1056 | * This function actually applies the mapping to the page table of the | |
1057 | * dma_ops domain. | |
1058 | */ | |
bd0e5211 JR |
1059 | static int dma_ops_unity_map(struct dma_ops_domain *dma_dom, |
1060 | struct unity_map_entry *e) | |
1061 | { | |
1062 | u64 addr; | |
1063 | int ret; | |
1064 | ||
1065 | for (addr = e->address_start; addr < e->address_end; | |
1066 | addr += PAGE_SIZE) { | |
abdc5eb3 | 1067 | ret = iommu_map_page(&dma_dom->domain, addr, addr, e->prot, |
cbb9d729 | 1068 | PAGE_SIZE); |
bd0e5211 JR |
1069 | if (ret) |
1070 | return ret; | |
1071 | /* | |
1072 | * if unity mapping is in aperture range mark the page | |
1073 | * as allocated in the aperture | |
1074 | */ | |
1075 | if (addr < dma_dom->aperture_size) | |
c3239567 | 1076 | __set_bit(addr >> PAGE_SHIFT, |
384de729 | 1077 | dma_dom->aperture[0]->bitmap); |
bd0e5211 JR |
1078 | } |
1079 | ||
1080 | return 0; | |
1081 | } | |
1082 | ||
171e7b37 JR |
1083 | /* |
1084 | * Init the unity mappings for a specific IOMMU in the system | |
1085 | * | |
1086 | * Basically iterates over all unity mapping entries and applies them to | |
1087 | * the default domain DMA of that IOMMU if necessary. | |
1088 | */ | |
1089 | static int iommu_init_unity_mappings(struct amd_iommu *iommu) | |
1090 | { | |
1091 | struct unity_map_entry *entry; | |
1092 | int ret; | |
1093 | ||
1094 | list_for_each_entry(entry, &amd_iommu_unity_map, list) { | |
1095 | if (!iommu_for_unity_map(iommu, entry)) | |
1096 | continue; | |
1097 | ret = dma_ops_unity_map(iommu->default_dom, entry); | |
1098 | if (ret) | |
1099 | return ret; | |
1100 | } | |
1101 | ||
1102 | return 0; | |
1103 | } | |
1104 | ||
431b2a20 JR |
1105 | /* |
1106 | * Inits the unity mappings required for a specific device | |
1107 | */ | |
bd0e5211 JR |
1108 | static int init_unity_mappings_for_device(struct dma_ops_domain *dma_dom, |
1109 | u16 devid) | |
1110 | { | |
1111 | struct unity_map_entry *e; | |
1112 | int ret; | |
1113 | ||
1114 | list_for_each_entry(e, &amd_iommu_unity_map, list) { | |
1115 | if (!(devid >= e->devid_start && devid <= e->devid_end)) | |
1116 | continue; | |
1117 | ret = dma_ops_unity_map(dma_dom, e); | |
1118 | if (ret) | |
1119 | return ret; | |
1120 | } | |
1121 | ||
1122 | return 0; | |
1123 | } | |
1124 | ||
431b2a20 JR |
1125 | /**************************************************************************** |
1126 | * | |
1127 | * The next functions belong to the address allocator for the dma_ops | |
1128 | * interface functions. They work like the allocators in the other IOMMU | |
1129 | * drivers. Its basically a bitmap which marks the allocated pages in | |
1130 | * the aperture. Maybe it could be enhanced in the future to a more | |
1131 | * efficient allocator. | |
1132 | * | |
1133 | ****************************************************************************/ | |
d3086444 | 1134 | |
431b2a20 | 1135 | /* |
384de729 | 1136 | * The address allocator core functions. |
431b2a20 JR |
1137 | * |
1138 | * called with domain->lock held | |
1139 | */ | |
384de729 | 1140 | |
171e7b37 JR |
1141 | /* |
1142 | * Used to reserve address ranges in the aperture (e.g. for exclusion | |
1143 | * ranges. | |
1144 | */ | |
1145 | static void dma_ops_reserve_addresses(struct dma_ops_domain *dom, | |
1146 | unsigned long start_page, | |
1147 | unsigned int pages) | |
1148 | { | |
1149 | unsigned int i, last_page = dom->aperture_size >> PAGE_SHIFT; | |
1150 | ||
1151 | if (start_page + pages > last_page) | |
1152 | pages = last_page - start_page; | |
1153 | ||
1154 | for (i = start_page; i < start_page + pages; ++i) { | |
1155 | int index = i / APERTURE_RANGE_PAGES; | |
1156 | int page = i % APERTURE_RANGE_PAGES; | |
1157 | __set_bit(page, dom->aperture[index]->bitmap); | |
1158 | } | |
1159 | } | |
1160 | ||
9cabe89b JR |
1161 | /* |
1162 | * This function is used to add a new aperture range to an existing | |
1163 | * aperture in case of dma_ops domain allocation or address allocation | |
1164 | * failure. | |
1165 | */ | |
576175c2 | 1166 | static int alloc_new_range(struct dma_ops_domain *dma_dom, |
9cabe89b JR |
1167 | bool populate, gfp_t gfp) |
1168 | { | |
1169 | int index = dma_dom->aperture_size >> APERTURE_RANGE_SHIFT; | |
576175c2 | 1170 | struct amd_iommu *iommu; |
d91afd15 | 1171 | unsigned long i; |
9cabe89b | 1172 | |
f5e9705c JR |
1173 | #ifdef CONFIG_IOMMU_STRESS |
1174 | populate = false; | |
1175 | #endif | |
1176 | ||
9cabe89b JR |
1177 | if (index >= APERTURE_MAX_RANGES) |
1178 | return -ENOMEM; | |
1179 | ||
1180 | dma_dom->aperture[index] = kzalloc(sizeof(struct aperture_range), gfp); | |
1181 | if (!dma_dom->aperture[index]) | |
1182 | return -ENOMEM; | |
1183 | ||
1184 | dma_dom->aperture[index]->bitmap = (void *)get_zeroed_page(gfp); | |
1185 | if (!dma_dom->aperture[index]->bitmap) | |
1186 | goto out_free; | |
1187 | ||
1188 | dma_dom->aperture[index]->offset = dma_dom->aperture_size; | |
1189 | ||
1190 | if (populate) { | |
1191 | unsigned long address = dma_dom->aperture_size; | |
1192 | int i, num_ptes = APERTURE_RANGE_PAGES / 512; | |
1193 | u64 *pte, *pte_page; | |
1194 | ||
1195 | for (i = 0; i < num_ptes; ++i) { | |
cbb9d729 | 1196 | pte = alloc_pte(&dma_dom->domain, address, PAGE_SIZE, |
9cabe89b JR |
1197 | &pte_page, gfp); |
1198 | if (!pte) | |
1199 | goto out_free; | |
1200 | ||
1201 | dma_dom->aperture[index]->pte_pages[i] = pte_page; | |
1202 | ||
1203 | address += APERTURE_RANGE_SIZE / 64; | |
1204 | } | |
1205 | } | |
1206 | ||
1207 | dma_dom->aperture_size += APERTURE_RANGE_SIZE; | |
1208 | ||
b595076a | 1209 | /* Initialize the exclusion range if necessary */ |
576175c2 JR |
1210 | for_each_iommu(iommu) { |
1211 | if (iommu->exclusion_start && | |
1212 | iommu->exclusion_start >= dma_dom->aperture[index]->offset | |
1213 | && iommu->exclusion_start < dma_dom->aperture_size) { | |
1214 | unsigned long startpage; | |
1215 | int pages = iommu_num_pages(iommu->exclusion_start, | |
1216 | iommu->exclusion_length, | |
1217 | PAGE_SIZE); | |
1218 | startpage = iommu->exclusion_start >> PAGE_SHIFT; | |
1219 | dma_ops_reserve_addresses(dma_dom, startpage, pages); | |
1220 | } | |
00cd122a JR |
1221 | } |
1222 | ||
1223 | /* | |
1224 | * Check for areas already mapped as present in the new aperture | |
1225 | * range and mark those pages as reserved in the allocator. Such | |
1226 | * mappings may already exist as a result of requested unity | |
1227 | * mappings for devices. | |
1228 | */ | |
1229 | for (i = dma_dom->aperture[index]->offset; | |
1230 | i < dma_dom->aperture_size; | |
1231 | i += PAGE_SIZE) { | |
24cd7723 | 1232 | u64 *pte = fetch_pte(&dma_dom->domain, i); |
00cd122a JR |
1233 | if (!pte || !IOMMU_PTE_PRESENT(*pte)) |
1234 | continue; | |
1235 | ||
1236 | dma_ops_reserve_addresses(dma_dom, i << PAGE_SHIFT, 1); | |
1237 | } | |
1238 | ||
04bfdd84 JR |
1239 | update_domain(&dma_dom->domain); |
1240 | ||
9cabe89b JR |
1241 | return 0; |
1242 | ||
1243 | out_free: | |
04bfdd84 JR |
1244 | update_domain(&dma_dom->domain); |
1245 | ||
9cabe89b JR |
1246 | free_page((unsigned long)dma_dom->aperture[index]->bitmap); |
1247 | ||
1248 | kfree(dma_dom->aperture[index]); | |
1249 | dma_dom->aperture[index] = NULL; | |
1250 | ||
1251 | return -ENOMEM; | |
1252 | } | |
1253 | ||
384de729 JR |
1254 | static unsigned long dma_ops_area_alloc(struct device *dev, |
1255 | struct dma_ops_domain *dom, | |
1256 | unsigned int pages, | |
1257 | unsigned long align_mask, | |
1258 | u64 dma_mask, | |
1259 | unsigned long start) | |
1260 | { | |
803b8cb4 | 1261 | unsigned long next_bit = dom->next_address % APERTURE_RANGE_SIZE; |
384de729 JR |
1262 | int max_index = dom->aperture_size >> APERTURE_RANGE_SHIFT; |
1263 | int i = start >> APERTURE_RANGE_SHIFT; | |
1264 | unsigned long boundary_size; | |
1265 | unsigned long address = -1; | |
1266 | unsigned long limit; | |
1267 | ||
803b8cb4 JR |
1268 | next_bit >>= PAGE_SHIFT; |
1269 | ||
384de729 JR |
1270 | boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1, |
1271 | PAGE_SIZE) >> PAGE_SHIFT; | |
1272 | ||
1273 | for (;i < max_index; ++i) { | |
1274 | unsigned long offset = dom->aperture[i]->offset >> PAGE_SHIFT; | |
1275 | ||
1276 | if (dom->aperture[i]->offset >= dma_mask) | |
1277 | break; | |
1278 | ||
1279 | limit = iommu_device_max_index(APERTURE_RANGE_PAGES, offset, | |
1280 | dma_mask >> PAGE_SHIFT); | |
1281 | ||
1282 | address = iommu_area_alloc(dom->aperture[i]->bitmap, | |
1283 | limit, next_bit, pages, 0, | |
1284 | boundary_size, align_mask); | |
1285 | if (address != -1) { | |
1286 | address = dom->aperture[i]->offset + | |
1287 | (address << PAGE_SHIFT); | |
803b8cb4 | 1288 | dom->next_address = address + (pages << PAGE_SHIFT); |
384de729 JR |
1289 | break; |
1290 | } | |
1291 | ||
1292 | next_bit = 0; | |
1293 | } | |
1294 | ||
1295 | return address; | |
1296 | } | |
1297 | ||
d3086444 JR |
1298 | static unsigned long dma_ops_alloc_addresses(struct device *dev, |
1299 | struct dma_ops_domain *dom, | |
6d4f343f | 1300 | unsigned int pages, |
832a90c3 JR |
1301 | unsigned long align_mask, |
1302 | u64 dma_mask) | |
d3086444 | 1303 | { |
d3086444 | 1304 | unsigned long address; |
d3086444 | 1305 | |
fe16f088 JR |
1306 | #ifdef CONFIG_IOMMU_STRESS |
1307 | dom->next_address = 0; | |
1308 | dom->need_flush = true; | |
1309 | #endif | |
d3086444 | 1310 | |
384de729 | 1311 | address = dma_ops_area_alloc(dev, dom, pages, align_mask, |
803b8cb4 | 1312 | dma_mask, dom->next_address); |
d3086444 | 1313 | |
1c655773 | 1314 | if (address == -1) { |
803b8cb4 | 1315 | dom->next_address = 0; |
384de729 JR |
1316 | address = dma_ops_area_alloc(dev, dom, pages, align_mask, |
1317 | dma_mask, 0); | |
1c655773 JR |
1318 | dom->need_flush = true; |
1319 | } | |
d3086444 | 1320 | |
384de729 | 1321 | if (unlikely(address == -1)) |
8fd524b3 | 1322 | address = DMA_ERROR_CODE; |
d3086444 JR |
1323 | |
1324 | WARN_ON((address + (PAGE_SIZE*pages)) > dom->aperture_size); | |
1325 | ||
1326 | return address; | |
1327 | } | |
1328 | ||
431b2a20 JR |
1329 | /* |
1330 | * The address free function. | |
1331 | * | |
1332 | * called with domain->lock held | |
1333 | */ | |
d3086444 JR |
1334 | static void dma_ops_free_addresses(struct dma_ops_domain *dom, |
1335 | unsigned long address, | |
1336 | unsigned int pages) | |
1337 | { | |
384de729 JR |
1338 | unsigned i = address >> APERTURE_RANGE_SHIFT; |
1339 | struct aperture_range *range = dom->aperture[i]; | |
80be308d | 1340 | |
384de729 JR |
1341 | BUG_ON(i >= APERTURE_MAX_RANGES || range == NULL); |
1342 | ||
47bccd6b JR |
1343 | #ifdef CONFIG_IOMMU_STRESS |
1344 | if (i < 4) | |
1345 | return; | |
1346 | #endif | |
80be308d | 1347 | |
803b8cb4 | 1348 | if (address >= dom->next_address) |
80be308d | 1349 | dom->need_flush = true; |
384de729 JR |
1350 | |
1351 | address = (address % APERTURE_RANGE_SIZE) >> PAGE_SHIFT; | |
803b8cb4 | 1352 | |
a66022c4 | 1353 | bitmap_clear(range->bitmap, address, pages); |
384de729 | 1354 | |
d3086444 JR |
1355 | } |
1356 | ||
431b2a20 JR |
1357 | /**************************************************************************** |
1358 | * | |
1359 | * The next functions belong to the domain allocation. A domain is | |
1360 | * allocated for every IOMMU as the default domain. If device isolation | |
1361 | * is enabled, every device get its own domain. The most important thing | |
1362 | * about domains is the page table mapping the DMA address space they | |
1363 | * contain. | |
1364 | * | |
1365 | ****************************************************************************/ | |
1366 | ||
aeb26f55 JR |
1367 | /* |
1368 | * This function adds a protection domain to the global protection domain list | |
1369 | */ | |
1370 | static void add_domain_to_list(struct protection_domain *domain) | |
1371 | { | |
1372 | unsigned long flags; | |
1373 | ||
1374 | spin_lock_irqsave(&amd_iommu_pd_lock, flags); | |
1375 | list_add(&domain->list, &amd_iommu_pd_list); | |
1376 | spin_unlock_irqrestore(&amd_iommu_pd_lock, flags); | |
1377 | } | |
1378 | ||
1379 | /* | |
1380 | * This function removes a protection domain to the global | |
1381 | * protection domain list | |
1382 | */ | |
1383 | static void del_domain_from_list(struct protection_domain *domain) | |
1384 | { | |
1385 | unsigned long flags; | |
1386 | ||
1387 | spin_lock_irqsave(&amd_iommu_pd_lock, flags); | |
1388 | list_del(&domain->list); | |
1389 | spin_unlock_irqrestore(&amd_iommu_pd_lock, flags); | |
1390 | } | |
1391 | ||
ec487d1a JR |
1392 | static u16 domain_id_alloc(void) |
1393 | { | |
1394 | unsigned long flags; | |
1395 | int id; | |
1396 | ||
1397 | write_lock_irqsave(&amd_iommu_devtable_lock, flags); | |
1398 | id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID); | |
1399 | BUG_ON(id == 0); | |
1400 | if (id > 0 && id < MAX_DOMAIN_ID) | |
1401 | __set_bit(id, amd_iommu_pd_alloc_bitmap); | |
1402 | else | |
1403 | id = 0; | |
1404 | write_unlock_irqrestore(&amd_iommu_devtable_lock, flags); | |
1405 | ||
1406 | return id; | |
1407 | } | |
1408 | ||
a2acfb75 JR |
1409 | static void domain_id_free(int id) |
1410 | { | |
1411 | unsigned long flags; | |
1412 | ||
1413 | write_lock_irqsave(&amd_iommu_devtable_lock, flags); | |
1414 | if (id > 0 && id < MAX_DOMAIN_ID) | |
1415 | __clear_bit(id, amd_iommu_pd_alloc_bitmap); | |
1416 | write_unlock_irqrestore(&amd_iommu_devtable_lock, flags); | |
1417 | } | |
a2acfb75 | 1418 | |
86db2e5d | 1419 | static void free_pagetable(struct protection_domain *domain) |
ec487d1a JR |
1420 | { |
1421 | int i, j; | |
1422 | u64 *p1, *p2, *p3; | |
1423 | ||
86db2e5d | 1424 | p1 = domain->pt_root; |
ec487d1a JR |
1425 | |
1426 | if (!p1) | |
1427 | return; | |
1428 | ||
1429 | for (i = 0; i < 512; ++i) { | |
1430 | if (!IOMMU_PTE_PRESENT(p1[i])) | |
1431 | continue; | |
1432 | ||
1433 | p2 = IOMMU_PTE_PAGE(p1[i]); | |
3cc3d84b | 1434 | for (j = 0; j < 512; ++j) { |
ec487d1a JR |
1435 | if (!IOMMU_PTE_PRESENT(p2[j])) |
1436 | continue; | |
1437 | p3 = IOMMU_PTE_PAGE(p2[j]); | |
1438 | free_page((unsigned long)p3); | |
1439 | } | |
1440 | ||
1441 | free_page((unsigned long)p2); | |
1442 | } | |
1443 | ||
1444 | free_page((unsigned long)p1); | |
86db2e5d JR |
1445 | |
1446 | domain->pt_root = NULL; | |
ec487d1a JR |
1447 | } |
1448 | ||
431b2a20 JR |
1449 | /* |
1450 | * Free a domain, only used if something went wrong in the | |
1451 | * allocation path and we need to free an already allocated page table | |
1452 | */ | |
ec487d1a JR |
1453 | static void dma_ops_domain_free(struct dma_ops_domain *dom) |
1454 | { | |
384de729 JR |
1455 | int i; |
1456 | ||
ec487d1a JR |
1457 | if (!dom) |
1458 | return; | |
1459 | ||
aeb26f55 JR |
1460 | del_domain_from_list(&dom->domain); |
1461 | ||
86db2e5d | 1462 | free_pagetable(&dom->domain); |
ec487d1a | 1463 | |
384de729 JR |
1464 | for (i = 0; i < APERTURE_MAX_RANGES; ++i) { |
1465 | if (!dom->aperture[i]) | |
1466 | continue; | |
1467 | free_page((unsigned long)dom->aperture[i]->bitmap); | |
1468 | kfree(dom->aperture[i]); | |
1469 | } | |
ec487d1a JR |
1470 | |
1471 | kfree(dom); | |
1472 | } | |
1473 | ||
431b2a20 JR |
1474 | /* |
1475 | * Allocates a new protection domain usable for the dma_ops functions. | |
b595076a | 1476 | * It also initializes the page table and the address allocator data |
431b2a20 JR |
1477 | * structures required for the dma_ops interface |
1478 | */ | |
87a64d52 | 1479 | static struct dma_ops_domain *dma_ops_domain_alloc(void) |
ec487d1a JR |
1480 | { |
1481 | struct dma_ops_domain *dma_dom; | |
ec487d1a JR |
1482 | |
1483 | dma_dom = kzalloc(sizeof(struct dma_ops_domain), GFP_KERNEL); | |
1484 | if (!dma_dom) | |
1485 | return NULL; | |
1486 | ||
1487 | spin_lock_init(&dma_dom->domain.lock); | |
1488 | ||
1489 | dma_dom->domain.id = domain_id_alloc(); | |
1490 | if (dma_dom->domain.id == 0) | |
1491 | goto free_dma_dom; | |
7c392cbe | 1492 | INIT_LIST_HEAD(&dma_dom->domain.dev_list); |
8f7a017c | 1493 | dma_dom->domain.mode = PAGE_MODE_2_LEVEL; |
ec487d1a | 1494 | dma_dom->domain.pt_root = (void *)get_zeroed_page(GFP_KERNEL); |
9fdb19d6 | 1495 | dma_dom->domain.flags = PD_DMA_OPS_MASK; |
ec487d1a JR |
1496 | dma_dom->domain.priv = dma_dom; |
1497 | if (!dma_dom->domain.pt_root) | |
1498 | goto free_dma_dom; | |
ec487d1a | 1499 | |
1c655773 | 1500 | dma_dom->need_flush = false; |
bd60b735 | 1501 | dma_dom->target_dev = 0xffff; |
1c655773 | 1502 | |
aeb26f55 JR |
1503 | add_domain_to_list(&dma_dom->domain); |
1504 | ||
576175c2 | 1505 | if (alloc_new_range(dma_dom, true, GFP_KERNEL)) |
ec487d1a | 1506 | goto free_dma_dom; |
ec487d1a | 1507 | |
431b2a20 | 1508 | /* |
ec487d1a JR |
1509 | * mark the first page as allocated so we never return 0 as |
1510 | * a valid dma-address. So we can use 0 as error value | |
431b2a20 | 1511 | */ |
384de729 | 1512 | dma_dom->aperture[0]->bitmap[0] = 1; |
803b8cb4 | 1513 | dma_dom->next_address = 0; |
ec487d1a | 1514 | |
ec487d1a JR |
1515 | |
1516 | return dma_dom; | |
1517 | ||
1518 | free_dma_dom: | |
1519 | dma_ops_domain_free(dma_dom); | |
1520 | ||
1521 | return NULL; | |
1522 | } | |
1523 | ||
5b28df6f JR |
1524 | /* |
1525 | * little helper function to check whether a given protection domain is a | |
1526 | * dma_ops domain | |
1527 | */ | |
1528 | static bool dma_ops_domain(struct protection_domain *domain) | |
1529 | { | |
1530 | return domain->flags & PD_DMA_OPS_MASK; | |
1531 | } | |
1532 | ||
fd7b5535 | 1533 | static void set_dte_entry(u16 devid, struct protection_domain *domain, bool ats) |
b20ac0d4 | 1534 | { |
b20ac0d4 | 1535 | u64 pte_root = virt_to_phys(domain->pt_root); |
fd7b5535 | 1536 | u32 flags = 0; |
863c74eb | 1537 | |
38ddf41b JR |
1538 | pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK) |
1539 | << DEV_ENTRY_MODE_SHIFT; | |
1540 | pte_root |= IOMMU_PTE_IR | IOMMU_PTE_IW | IOMMU_PTE_P | IOMMU_PTE_TV; | |
b20ac0d4 | 1541 | |
fd7b5535 JR |
1542 | if (ats) |
1543 | flags |= DTE_FLAG_IOTLB; | |
1544 | ||
1545 | amd_iommu_dev_table[devid].data[3] |= flags; | |
1546 | amd_iommu_dev_table[devid].data[2] = domain->id; | |
1547 | amd_iommu_dev_table[devid].data[1] = upper_32_bits(pte_root); | |
1548 | amd_iommu_dev_table[devid].data[0] = lower_32_bits(pte_root); | |
15898bbc JR |
1549 | } |
1550 | ||
1551 | static void clear_dte_entry(u16 devid) | |
1552 | { | |
15898bbc JR |
1553 | /* remove entry from the device table seen by the hardware */ |
1554 | amd_iommu_dev_table[devid].data[0] = IOMMU_PTE_P | IOMMU_PTE_TV; | |
1555 | amd_iommu_dev_table[devid].data[1] = 0; | |
1556 | amd_iommu_dev_table[devid].data[2] = 0; | |
1557 | ||
1558 | amd_iommu_apply_erratum_63(devid); | |
7f760ddd JR |
1559 | } |
1560 | ||
ec9e79ef JR |
1561 | static void do_attach(struct iommu_dev_data *dev_data, |
1562 | struct protection_domain *domain) | |
7f760ddd | 1563 | { |
7f760ddd | 1564 | struct amd_iommu *iommu; |
ec9e79ef | 1565 | bool ats; |
fd7b5535 | 1566 | |
ec9e79ef JR |
1567 | iommu = amd_iommu_rlookup_table[dev_data->devid]; |
1568 | ats = dev_data->ats.enabled; | |
7f760ddd JR |
1569 | |
1570 | /* Update data structures */ | |
1571 | dev_data->domain = domain; | |
1572 | list_add(&dev_data->list, &domain->dev_list); | |
f62dda66 | 1573 | set_dte_entry(dev_data->devid, domain, ats); |
7f760ddd JR |
1574 | |
1575 | /* Do reference counting */ | |
1576 | domain->dev_iommu[iommu->index] += 1; | |
1577 | domain->dev_cnt += 1; | |
1578 | ||
1579 | /* Flush the DTE entry */ | |
6c542047 | 1580 | device_flush_dte(dev_data); |
7f760ddd JR |
1581 | } |
1582 | ||
ec9e79ef | 1583 | static void do_detach(struct iommu_dev_data *dev_data) |
7f760ddd | 1584 | { |
7f760ddd | 1585 | struct amd_iommu *iommu; |
7f760ddd | 1586 | |
ec9e79ef | 1587 | iommu = amd_iommu_rlookup_table[dev_data->devid]; |
15898bbc JR |
1588 | |
1589 | /* decrease reference counters */ | |
7f760ddd JR |
1590 | dev_data->domain->dev_iommu[iommu->index] -= 1; |
1591 | dev_data->domain->dev_cnt -= 1; | |
1592 | ||
1593 | /* Update data structures */ | |
1594 | dev_data->domain = NULL; | |
1595 | list_del(&dev_data->list); | |
f62dda66 | 1596 | clear_dte_entry(dev_data->devid); |
15898bbc | 1597 | |
7f760ddd | 1598 | /* Flush the DTE entry */ |
6c542047 | 1599 | device_flush_dte(dev_data); |
2b681faf JR |
1600 | } |
1601 | ||
1602 | /* | |
1603 | * If a device is not yet associated with a domain, this function does | |
1604 | * assigns it visible for the hardware | |
1605 | */ | |
ec9e79ef | 1606 | static int __attach_device(struct iommu_dev_data *dev_data, |
15898bbc | 1607 | struct protection_domain *domain) |
2b681faf | 1608 | { |
84fe6c19 | 1609 | int ret; |
657cbb6b | 1610 | |
2b681faf JR |
1611 | /* lock domain */ |
1612 | spin_lock(&domain->lock); | |
1613 | ||
2b02b091 JR |
1614 | if (dev_data->alias != NULL) { |
1615 | struct iommu_dev_data *alias_data; | |
1616 | ||
1617 | alias_data = get_dev_data(dev_data->alias); | |
eba6ac60 | 1618 | |
2b02b091 JR |
1619 | ret = -EINVAL; |
1620 | if (!alias_data) | |
1621 | goto out_unlock; | |
15898bbc | 1622 | |
2b02b091 JR |
1623 | /* Some sanity checks */ |
1624 | ret = -EBUSY; | |
1625 | if (alias_data->domain != NULL && | |
1626 | alias_data->domain != domain) | |
1627 | goto out_unlock; | |
1628 | ||
1629 | if (dev_data->domain != NULL && | |
1630 | dev_data->domain != domain) | |
1631 | goto out_unlock; | |
1632 | ||
1633 | /* Do real assignment */ | |
7f760ddd | 1634 | if (alias_data->domain == NULL) |
ec9e79ef | 1635 | do_attach(alias_data, domain); |
24100055 JR |
1636 | |
1637 | atomic_inc(&alias_data->bind); | |
657cbb6b | 1638 | } |
15898bbc | 1639 | |
7f760ddd | 1640 | if (dev_data->domain == NULL) |
ec9e79ef | 1641 | do_attach(dev_data, domain); |
eba6ac60 | 1642 | |
24100055 JR |
1643 | atomic_inc(&dev_data->bind); |
1644 | ||
84fe6c19 JL |
1645 | ret = 0; |
1646 | ||
1647 | out_unlock: | |
1648 | ||
eba6ac60 JR |
1649 | /* ready */ |
1650 | spin_unlock(&domain->lock); | |
15898bbc | 1651 | |
84fe6c19 | 1652 | return ret; |
0feae533 | 1653 | } |
b20ac0d4 | 1654 | |
407d733e JR |
1655 | /* |
1656 | * If a device is not yet associated with a domain, this function does | |
1657 | * assigns it visible for the hardware | |
1658 | */ | |
15898bbc JR |
1659 | static int attach_device(struct device *dev, |
1660 | struct protection_domain *domain) | |
0feae533 | 1661 | { |
fd7b5535 | 1662 | struct pci_dev *pdev = to_pci_dev(dev); |
ea61cddb | 1663 | struct iommu_dev_data *dev_data; |
eba6ac60 | 1664 | unsigned long flags; |
15898bbc | 1665 | int ret; |
eba6ac60 | 1666 | |
ea61cddb JR |
1667 | dev_data = get_dev_data(dev); |
1668 | ||
1669 | if (amd_iommu_iotlb_sup && pci_enable_ats(pdev, PAGE_SHIFT) == 0) { | |
1670 | dev_data->ats.enabled = true; | |
1671 | dev_data->ats.qdep = pci_ats_queue_depth(pdev); | |
1672 | } | |
fd7b5535 | 1673 | |
eba6ac60 | 1674 | write_lock_irqsave(&amd_iommu_devtable_lock, flags); |
ec9e79ef | 1675 | ret = __attach_device(dev_data, domain); |
b20ac0d4 JR |
1676 | write_unlock_irqrestore(&amd_iommu_devtable_lock, flags); |
1677 | ||
0feae533 JR |
1678 | /* |
1679 | * We might boot into a crash-kernel here. The crashed kernel | |
1680 | * left the caches in the IOMMU dirty. So we have to flush | |
1681 | * here to evict all dirty stuff. | |
1682 | */ | |
17b124bf | 1683 | domain_flush_tlb_pde(domain); |
15898bbc JR |
1684 | |
1685 | return ret; | |
b20ac0d4 JR |
1686 | } |
1687 | ||
355bf553 JR |
1688 | /* |
1689 | * Removes a device from a protection domain (unlocked) | |
1690 | */ | |
ec9e79ef | 1691 | static void __detach_device(struct iommu_dev_data *dev_data) |
355bf553 | 1692 | { |
24100055 | 1693 | struct iommu_dev_data *alias_data; |
2ca76279 | 1694 | struct protection_domain *domain; |
7c392cbe | 1695 | unsigned long flags; |
c4596114 | 1696 | |
7f760ddd | 1697 | BUG_ON(!dev_data->domain); |
355bf553 | 1698 | |
2ca76279 JR |
1699 | domain = dev_data->domain; |
1700 | ||
1701 | spin_lock_irqsave(&domain->lock, flags); | |
24100055 | 1702 | |
2b02b091 JR |
1703 | if (dev_data->alias) { |
1704 | alias_data = get_dev_data(dev_data->alias); | |
7f760ddd | 1705 | if (atomic_dec_and_test(&alias_data->bind)) |
ec9e79ef | 1706 | do_detach(alias_data); |
24100055 JR |
1707 | } |
1708 | ||
7f760ddd | 1709 | if (atomic_dec_and_test(&dev_data->bind)) |
ec9e79ef | 1710 | do_detach(dev_data); |
7f760ddd | 1711 | |
2ca76279 | 1712 | spin_unlock_irqrestore(&domain->lock, flags); |
21129f78 JR |
1713 | |
1714 | /* | |
1715 | * If we run in passthrough mode the device must be assigned to the | |
d3ad9373 JR |
1716 | * passthrough domain if it is detached from any other domain. |
1717 | * Make sure we can deassign from the pt_domain itself. | |
21129f78 | 1718 | */ |
d3ad9373 JR |
1719 | if (iommu_pass_through && |
1720 | (dev_data->domain == NULL && domain != pt_domain)) | |
ec9e79ef | 1721 | __attach_device(dev_data, pt_domain); |
355bf553 JR |
1722 | } |
1723 | ||
1724 | /* | |
1725 | * Removes a device from a protection domain (with devtable_lock held) | |
1726 | */ | |
15898bbc | 1727 | static void detach_device(struct device *dev) |
355bf553 | 1728 | { |
ea61cddb | 1729 | struct iommu_dev_data *dev_data; |
355bf553 JR |
1730 | unsigned long flags; |
1731 | ||
ec9e79ef JR |
1732 | dev_data = get_dev_data(dev); |
1733 | ||
355bf553 JR |
1734 | /* lock device table */ |
1735 | write_lock_irqsave(&amd_iommu_devtable_lock, flags); | |
ec9e79ef | 1736 | __detach_device(dev_data); |
355bf553 | 1737 | write_unlock_irqrestore(&amd_iommu_devtable_lock, flags); |
fd7b5535 | 1738 | |
ea61cddb JR |
1739 | if (dev_data->ats.enabled) { |
1740 | pci_disable_ats(to_pci_dev(dev)); | |
1741 | dev_data->ats.enabled = false; | |
1742 | } | |
355bf553 | 1743 | } |
e275a2a0 | 1744 | |
15898bbc JR |
1745 | /* |
1746 | * Find out the protection domain structure for a given PCI device. This | |
1747 | * will give us the pointer to the page table root for example. | |
1748 | */ | |
1749 | static struct protection_domain *domain_for_device(struct device *dev) | |
1750 | { | |
657cbb6b | 1751 | struct iommu_dev_data *dev_data, *alias_data; |
2b02b091 | 1752 | struct protection_domain *dom = NULL; |
15898bbc | 1753 | unsigned long flags; |
15898bbc | 1754 | |
657cbb6b | 1755 | dev_data = get_dev_data(dev); |
15898bbc | 1756 | |
2b02b091 JR |
1757 | if (dev_data->domain) |
1758 | return dev_data->domain; | |
15898bbc | 1759 | |
2b02b091 JR |
1760 | if (dev_data->alias != NULL) { |
1761 | alias_data = get_dev_data(dev_data->alias); | |
1762 | ||
1763 | read_lock_irqsave(&amd_iommu_devtable_lock, flags); | |
1764 | if (alias_data->domain != NULL) { | |
1765 | __attach_device(dev_data, alias_data->domain); | |
1766 | dom = alias_data->domain; | |
1767 | } | |
1768 | read_unlock_irqrestore(&amd_iommu_devtable_lock, flags); | |
1769 | } | |
15898bbc JR |
1770 | |
1771 | return dom; | |
1772 | } | |
1773 | ||
e275a2a0 JR |
1774 | static int device_change_notifier(struct notifier_block *nb, |
1775 | unsigned long action, void *data) | |
1776 | { | |
1777 | struct device *dev = data; | |
98fc5a69 | 1778 | u16 devid; |
e275a2a0 JR |
1779 | struct protection_domain *domain; |
1780 | struct dma_ops_domain *dma_domain; | |
1781 | struct amd_iommu *iommu; | |
1ac4cbbc | 1782 | unsigned long flags; |
e275a2a0 | 1783 | |
98fc5a69 JR |
1784 | if (!check_device(dev)) |
1785 | return 0; | |
e275a2a0 | 1786 | |
98fc5a69 JR |
1787 | devid = get_device_id(dev); |
1788 | iommu = amd_iommu_rlookup_table[devid]; | |
e275a2a0 JR |
1789 | |
1790 | switch (action) { | |
c1eee67b | 1791 | case BUS_NOTIFY_UNBOUND_DRIVER: |
657cbb6b JR |
1792 | |
1793 | domain = domain_for_device(dev); | |
1794 | ||
e275a2a0 JR |
1795 | if (!domain) |
1796 | goto out; | |
a1ca331c JR |
1797 | if (iommu_pass_through) |
1798 | break; | |
15898bbc | 1799 | detach_device(dev); |
1ac4cbbc JR |
1800 | break; |
1801 | case BUS_NOTIFY_ADD_DEVICE: | |
657cbb6b JR |
1802 | |
1803 | iommu_init_device(dev); | |
1804 | ||
1805 | domain = domain_for_device(dev); | |
1806 | ||
1ac4cbbc JR |
1807 | /* allocate a protection domain if a device is added */ |
1808 | dma_domain = find_protection_domain(devid); | |
1809 | if (dma_domain) | |
1810 | goto out; | |
87a64d52 | 1811 | dma_domain = dma_ops_domain_alloc(); |
1ac4cbbc JR |
1812 | if (!dma_domain) |
1813 | goto out; | |
1814 | dma_domain->target_dev = devid; | |
1815 | ||
1816 | spin_lock_irqsave(&iommu_pd_list_lock, flags); | |
1817 | list_add_tail(&dma_domain->list, &iommu_pd_list); | |
1818 | spin_unlock_irqrestore(&iommu_pd_list_lock, flags); | |
1819 | ||
e275a2a0 | 1820 | break; |
657cbb6b JR |
1821 | case BUS_NOTIFY_DEL_DEVICE: |
1822 | ||
1823 | iommu_uninit_device(dev); | |
1824 | ||
e275a2a0 JR |
1825 | default: |
1826 | goto out; | |
1827 | } | |
1828 | ||
e275a2a0 JR |
1829 | iommu_completion_wait(iommu); |
1830 | ||
1831 | out: | |
1832 | return 0; | |
1833 | } | |
1834 | ||
b25ae679 | 1835 | static struct notifier_block device_nb = { |
e275a2a0 JR |
1836 | .notifier_call = device_change_notifier, |
1837 | }; | |
355bf553 | 1838 | |
8638c491 JR |
1839 | void amd_iommu_init_notifier(void) |
1840 | { | |
1841 | bus_register_notifier(&pci_bus_type, &device_nb); | |
1842 | } | |
1843 | ||
431b2a20 JR |
1844 | /***************************************************************************** |
1845 | * | |
1846 | * The next functions belong to the dma_ops mapping/unmapping code. | |
1847 | * | |
1848 | *****************************************************************************/ | |
1849 | ||
1850 | /* | |
1851 | * In the dma_ops path we only have the struct device. This function | |
1852 | * finds the corresponding IOMMU, the protection domain and the | |
1853 | * requestor id for a given device. | |
1854 | * If the device is not yet associated with a domain this is also done | |
1855 | * in this function. | |
1856 | */ | |
94f6d190 | 1857 | static struct protection_domain *get_domain(struct device *dev) |
b20ac0d4 | 1858 | { |
94f6d190 | 1859 | struct protection_domain *domain; |
b20ac0d4 | 1860 | struct dma_ops_domain *dma_dom; |
94f6d190 | 1861 | u16 devid = get_device_id(dev); |
b20ac0d4 | 1862 | |
f99c0f1c | 1863 | if (!check_device(dev)) |
94f6d190 | 1864 | return ERR_PTR(-EINVAL); |
b20ac0d4 | 1865 | |
94f6d190 JR |
1866 | domain = domain_for_device(dev); |
1867 | if (domain != NULL && !dma_ops_domain(domain)) | |
1868 | return ERR_PTR(-EBUSY); | |
f99c0f1c | 1869 | |
94f6d190 JR |
1870 | if (domain != NULL) |
1871 | return domain; | |
b20ac0d4 | 1872 | |
15898bbc | 1873 | /* Device not bount yet - bind it */ |
94f6d190 | 1874 | dma_dom = find_protection_domain(devid); |
15898bbc | 1875 | if (!dma_dom) |
94f6d190 JR |
1876 | dma_dom = amd_iommu_rlookup_table[devid]->default_dom; |
1877 | attach_device(dev, &dma_dom->domain); | |
15898bbc | 1878 | DUMP_printk("Using protection domain %d for device %s\n", |
94f6d190 | 1879 | dma_dom->domain.id, dev_name(dev)); |
f91ba190 | 1880 | |
94f6d190 | 1881 | return &dma_dom->domain; |
b20ac0d4 JR |
1882 | } |
1883 | ||
04bfdd84 JR |
1884 | static void update_device_table(struct protection_domain *domain) |
1885 | { | |
492667da | 1886 | struct iommu_dev_data *dev_data; |
04bfdd84 | 1887 | |
ea61cddb JR |
1888 | list_for_each_entry(dev_data, &domain->dev_list, list) |
1889 | set_dte_entry(dev_data->devid, domain, dev_data->ats.enabled); | |
04bfdd84 JR |
1890 | } |
1891 | ||
1892 | static void update_domain(struct protection_domain *domain) | |
1893 | { | |
1894 | if (!domain->updated) | |
1895 | return; | |
1896 | ||
1897 | update_device_table(domain); | |
17b124bf JR |
1898 | |
1899 | domain_flush_devices(domain); | |
1900 | domain_flush_tlb_pde(domain); | |
04bfdd84 JR |
1901 | |
1902 | domain->updated = false; | |
1903 | } | |
1904 | ||
8bda3092 JR |
1905 | /* |
1906 | * This function fetches the PTE for a given address in the aperture | |
1907 | */ | |
1908 | static u64* dma_ops_get_pte(struct dma_ops_domain *dom, | |
1909 | unsigned long address) | |
1910 | { | |
384de729 | 1911 | struct aperture_range *aperture; |
8bda3092 JR |
1912 | u64 *pte, *pte_page; |
1913 | ||
384de729 JR |
1914 | aperture = dom->aperture[APERTURE_RANGE_INDEX(address)]; |
1915 | if (!aperture) | |
1916 | return NULL; | |
1917 | ||
1918 | pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)]; | |
8bda3092 | 1919 | if (!pte) { |
cbb9d729 | 1920 | pte = alloc_pte(&dom->domain, address, PAGE_SIZE, &pte_page, |
abdc5eb3 | 1921 | GFP_ATOMIC); |
384de729 JR |
1922 | aperture->pte_pages[APERTURE_PAGE_INDEX(address)] = pte_page; |
1923 | } else | |
8c8c143c | 1924 | pte += PM_LEVEL_INDEX(0, address); |
8bda3092 | 1925 | |
04bfdd84 | 1926 | update_domain(&dom->domain); |
8bda3092 JR |
1927 | |
1928 | return pte; | |
1929 | } | |
1930 | ||
431b2a20 JR |
1931 | /* |
1932 | * This is the generic map function. It maps one 4kb page at paddr to | |
1933 | * the given address in the DMA address space for the domain. | |
1934 | */ | |
680525e0 | 1935 | static dma_addr_t dma_ops_domain_map(struct dma_ops_domain *dom, |
cb76c322 JR |
1936 | unsigned long address, |
1937 | phys_addr_t paddr, | |
1938 | int direction) | |
1939 | { | |
1940 | u64 *pte, __pte; | |
1941 | ||
1942 | WARN_ON(address > dom->aperture_size); | |
1943 | ||
1944 | paddr &= PAGE_MASK; | |
1945 | ||
8bda3092 | 1946 | pte = dma_ops_get_pte(dom, address); |
53812c11 | 1947 | if (!pte) |
8fd524b3 | 1948 | return DMA_ERROR_CODE; |
cb76c322 JR |
1949 | |
1950 | __pte = paddr | IOMMU_PTE_P | IOMMU_PTE_FC; | |
1951 | ||
1952 | if (direction == DMA_TO_DEVICE) | |
1953 | __pte |= IOMMU_PTE_IR; | |
1954 | else if (direction == DMA_FROM_DEVICE) | |
1955 | __pte |= IOMMU_PTE_IW; | |
1956 | else if (direction == DMA_BIDIRECTIONAL) | |
1957 | __pte |= IOMMU_PTE_IR | IOMMU_PTE_IW; | |
1958 | ||
1959 | WARN_ON(*pte); | |
1960 | ||
1961 | *pte = __pte; | |
1962 | ||
1963 | return (dma_addr_t)address; | |
1964 | } | |
1965 | ||
431b2a20 JR |
1966 | /* |
1967 | * The generic unmapping function for on page in the DMA address space. | |
1968 | */ | |
680525e0 | 1969 | static void dma_ops_domain_unmap(struct dma_ops_domain *dom, |
cb76c322 JR |
1970 | unsigned long address) |
1971 | { | |
384de729 | 1972 | struct aperture_range *aperture; |
cb76c322 JR |
1973 | u64 *pte; |
1974 | ||
1975 | if (address >= dom->aperture_size) | |
1976 | return; | |
1977 | ||
384de729 JR |
1978 | aperture = dom->aperture[APERTURE_RANGE_INDEX(address)]; |
1979 | if (!aperture) | |
1980 | return; | |
1981 | ||
1982 | pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)]; | |
1983 | if (!pte) | |
1984 | return; | |
cb76c322 | 1985 | |
8c8c143c | 1986 | pte += PM_LEVEL_INDEX(0, address); |
cb76c322 JR |
1987 | |
1988 | WARN_ON(!*pte); | |
1989 | ||
1990 | *pte = 0ULL; | |
1991 | } | |
1992 | ||
431b2a20 JR |
1993 | /* |
1994 | * This function contains common code for mapping of a physically | |
24f81160 JR |
1995 | * contiguous memory region into DMA address space. It is used by all |
1996 | * mapping functions provided with this IOMMU driver. | |
431b2a20 JR |
1997 | * Must be called with the domain lock held. |
1998 | */ | |
cb76c322 | 1999 | static dma_addr_t __map_single(struct device *dev, |
cb76c322 JR |
2000 | struct dma_ops_domain *dma_dom, |
2001 | phys_addr_t paddr, | |
2002 | size_t size, | |
6d4f343f | 2003 | int dir, |
832a90c3 JR |
2004 | bool align, |
2005 | u64 dma_mask) | |
cb76c322 JR |
2006 | { |
2007 | dma_addr_t offset = paddr & ~PAGE_MASK; | |
53812c11 | 2008 | dma_addr_t address, start, ret; |
cb76c322 | 2009 | unsigned int pages; |
6d4f343f | 2010 | unsigned long align_mask = 0; |
cb76c322 JR |
2011 | int i; |
2012 | ||
e3c449f5 | 2013 | pages = iommu_num_pages(paddr, size, PAGE_SIZE); |
cb76c322 JR |
2014 | paddr &= PAGE_MASK; |
2015 | ||
8ecaf8f1 JR |
2016 | INC_STATS_COUNTER(total_map_requests); |
2017 | ||
c1858976 JR |
2018 | if (pages > 1) |
2019 | INC_STATS_COUNTER(cross_page); | |
2020 | ||
6d4f343f JR |
2021 | if (align) |
2022 | align_mask = (1UL << get_order(size)) - 1; | |
2023 | ||
11b83888 | 2024 | retry: |
832a90c3 JR |
2025 | address = dma_ops_alloc_addresses(dev, dma_dom, pages, align_mask, |
2026 | dma_mask); | |
8fd524b3 | 2027 | if (unlikely(address == DMA_ERROR_CODE)) { |
11b83888 JR |
2028 | /* |
2029 | * setting next_address here will let the address | |
2030 | * allocator only scan the new allocated range in the | |
2031 | * first run. This is a small optimization. | |
2032 | */ | |
2033 | dma_dom->next_address = dma_dom->aperture_size; | |
2034 | ||
576175c2 | 2035 | if (alloc_new_range(dma_dom, false, GFP_ATOMIC)) |
11b83888 JR |
2036 | goto out; |
2037 | ||
2038 | /* | |
af901ca1 | 2039 | * aperture was successfully enlarged by 128 MB, try |
11b83888 JR |
2040 | * allocation again |
2041 | */ | |
2042 | goto retry; | |
2043 | } | |
cb76c322 JR |
2044 | |
2045 | start = address; | |
2046 | for (i = 0; i < pages; ++i) { | |
680525e0 | 2047 | ret = dma_ops_domain_map(dma_dom, start, paddr, dir); |
8fd524b3 | 2048 | if (ret == DMA_ERROR_CODE) |
53812c11 JR |
2049 | goto out_unmap; |
2050 | ||
cb76c322 JR |
2051 | paddr += PAGE_SIZE; |
2052 | start += PAGE_SIZE; | |
2053 | } | |
2054 | address += offset; | |
2055 | ||
5774f7c5 JR |
2056 | ADD_STATS_COUNTER(alloced_io_mem, size); |
2057 | ||
afa9fdc2 | 2058 | if (unlikely(dma_dom->need_flush && !amd_iommu_unmap_flush)) { |
17b124bf | 2059 | domain_flush_tlb(&dma_dom->domain); |
1c655773 | 2060 | dma_dom->need_flush = false; |
318afd41 | 2061 | } else if (unlikely(amd_iommu_np_cache)) |
17b124bf | 2062 | domain_flush_pages(&dma_dom->domain, address, size); |
270cab24 | 2063 | |
cb76c322 JR |
2064 | out: |
2065 | return address; | |
53812c11 JR |
2066 | |
2067 | out_unmap: | |
2068 | ||
2069 | for (--i; i >= 0; --i) { | |
2070 | start -= PAGE_SIZE; | |
680525e0 | 2071 | dma_ops_domain_unmap(dma_dom, start); |
53812c11 JR |
2072 | } |
2073 | ||
2074 | dma_ops_free_addresses(dma_dom, address, pages); | |
2075 | ||
8fd524b3 | 2076 | return DMA_ERROR_CODE; |
cb76c322 JR |
2077 | } |
2078 | ||
431b2a20 JR |
2079 | /* |
2080 | * Does the reverse of the __map_single function. Must be called with | |
2081 | * the domain lock held too | |
2082 | */ | |
cd8c82e8 | 2083 | static void __unmap_single(struct dma_ops_domain *dma_dom, |
cb76c322 JR |
2084 | dma_addr_t dma_addr, |
2085 | size_t size, | |
2086 | int dir) | |
2087 | { | |
04e0463e | 2088 | dma_addr_t flush_addr; |
cb76c322 JR |
2089 | dma_addr_t i, start; |
2090 | unsigned int pages; | |
2091 | ||
8fd524b3 | 2092 | if ((dma_addr == DMA_ERROR_CODE) || |
b8d9905d | 2093 | (dma_addr + size > dma_dom->aperture_size)) |
cb76c322 JR |
2094 | return; |
2095 | ||
04e0463e | 2096 | flush_addr = dma_addr; |
e3c449f5 | 2097 | pages = iommu_num_pages(dma_addr, size, PAGE_SIZE); |
cb76c322 JR |
2098 | dma_addr &= PAGE_MASK; |
2099 | start = dma_addr; | |
2100 | ||
2101 | for (i = 0; i < pages; ++i) { | |
680525e0 | 2102 | dma_ops_domain_unmap(dma_dom, start); |
cb76c322 JR |
2103 | start += PAGE_SIZE; |
2104 | } | |
2105 | ||
5774f7c5 JR |
2106 | SUB_STATS_COUNTER(alloced_io_mem, size); |
2107 | ||
cb76c322 | 2108 | dma_ops_free_addresses(dma_dom, dma_addr, pages); |
270cab24 | 2109 | |
80be308d | 2110 | if (amd_iommu_unmap_flush || dma_dom->need_flush) { |
17b124bf | 2111 | domain_flush_pages(&dma_dom->domain, flush_addr, size); |
80be308d JR |
2112 | dma_dom->need_flush = false; |
2113 | } | |
cb76c322 JR |
2114 | } |
2115 | ||
431b2a20 JR |
2116 | /* |
2117 | * The exported map_single function for dma_ops. | |
2118 | */ | |
51491367 FT |
2119 | static dma_addr_t map_page(struct device *dev, struct page *page, |
2120 | unsigned long offset, size_t size, | |
2121 | enum dma_data_direction dir, | |
2122 | struct dma_attrs *attrs) | |
4da70b9e JR |
2123 | { |
2124 | unsigned long flags; | |
4da70b9e | 2125 | struct protection_domain *domain; |
4da70b9e | 2126 | dma_addr_t addr; |
832a90c3 | 2127 | u64 dma_mask; |
51491367 | 2128 | phys_addr_t paddr = page_to_phys(page) + offset; |
4da70b9e | 2129 | |
0f2a86f2 JR |
2130 | INC_STATS_COUNTER(cnt_map_single); |
2131 | ||
94f6d190 JR |
2132 | domain = get_domain(dev); |
2133 | if (PTR_ERR(domain) == -EINVAL) | |
4da70b9e | 2134 | return (dma_addr_t)paddr; |
94f6d190 JR |
2135 | else if (IS_ERR(domain)) |
2136 | return DMA_ERROR_CODE; | |
4da70b9e | 2137 | |
f99c0f1c JR |
2138 | dma_mask = *dev->dma_mask; |
2139 | ||
4da70b9e | 2140 | spin_lock_irqsave(&domain->lock, flags); |
94f6d190 | 2141 | |
cd8c82e8 | 2142 | addr = __map_single(dev, domain->priv, paddr, size, dir, false, |
832a90c3 | 2143 | dma_mask); |
8fd524b3 | 2144 | if (addr == DMA_ERROR_CODE) |
4da70b9e JR |
2145 | goto out; |
2146 | ||
17b124bf | 2147 | domain_flush_complete(domain); |
4da70b9e JR |
2148 | |
2149 | out: | |
2150 | spin_unlock_irqrestore(&domain->lock, flags); | |
2151 | ||
2152 | return addr; | |
2153 | } | |
2154 | ||
431b2a20 JR |
2155 | /* |
2156 | * The exported unmap_single function for dma_ops. | |
2157 | */ | |
51491367 FT |
2158 | static void unmap_page(struct device *dev, dma_addr_t dma_addr, size_t size, |
2159 | enum dma_data_direction dir, struct dma_attrs *attrs) | |
4da70b9e JR |
2160 | { |
2161 | unsigned long flags; | |
4da70b9e | 2162 | struct protection_domain *domain; |
4da70b9e | 2163 | |
146a6917 JR |
2164 | INC_STATS_COUNTER(cnt_unmap_single); |
2165 | ||
94f6d190 JR |
2166 | domain = get_domain(dev); |
2167 | if (IS_ERR(domain)) | |
5b28df6f JR |
2168 | return; |
2169 | ||
4da70b9e JR |
2170 | spin_lock_irqsave(&domain->lock, flags); |
2171 | ||
cd8c82e8 | 2172 | __unmap_single(domain->priv, dma_addr, size, dir); |
4da70b9e | 2173 | |
17b124bf | 2174 | domain_flush_complete(domain); |
4da70b9e JR |
2175 | |
2176 | spin_unlock_irqrestore(&domain->lock, flags); | |
2177 | } | |
2178 | ||
431b2a20 JR |
2179 | /* |
2180 | * This is a special map_sg function which is used if we should map a | |
2181 | * device which is not handled by an AMD IOMMU in the system. | |
2182 | */ | |
65b050ad JR |
2183 | static int map_sg_no_iommu(struct device *dev, struct scatterlist *sglist, |
2184 | int nelems, int dir) | |
2185 | { | |
2186 | struct scatterlist *s; | |
2187 | int i; | |
2188 | ||
2189 | for_each_sg(sglist, s, nelems, i) { | |
2190 | s->dma_address = (dma_addr_t)sg_phys(s); | |
2191 | s->dma_length = s->length; | |
2192 | } | |
2193 | ||
2194 | return nelems; | |
2195 | } | |
2196 | ||
431b2a20 JR |
2197 | /* |
2198 | * The exported map_sg function for dma_ops (handles scatter-gather | |
2199 | * lists). | |
2200 | */ | |
65b050ad | 2201 | static int map_sg(struct device *dev, struct scatterlist *sglist, |
160c1d8e FT |
2202 | int nelems, enum dma_data_direction dir, |
2203 | struct dma_attrs *attrs) | |
65b050ad JR |
2204 | { |
2205 | unsigned long flags; | |
65b050ad | 2206 | struct protection_domain *domain; |
65b050ad JR |
2207 | int i; |
2208 | struct scatterlist *s; | |
2209 | phys_addr_t paddr; | |
2210 | int mapped_elems = 0; | |
832a90c3 | 2211 | u64 dma_mask; |
65b050ad | 2212 | |
d03f067a JR |
2213 | INC_STATS_COUNTER(cnt_map_sg); |
2214 | ||
94f6d190 JR |
2215 | domain = get_domain(dev); |
2216 | if (PTR_ERR(domain) == -EINVAL) | |
f99c0f1c | 2217 | return map_sg_no_iommu(dev, sglist, nelems, dir); |
94f6d190 JR |
2218 | else if (IS_ERR(domain)) |
2219 | return 0; | |
dbcc112e | 2220 | |
832a90c3 | 2221 | dma_mask = *dev->dma_mask; |
65b050ad | 2222 | |
65b050ad JR |
2223 | spin_lock_irqsave(&domain->lock, flags); |
2224 | ||
2225 | for_each_sg(sglist, s, nelems, i) { | |
2226 | paddr = sg_phys(s); | |
2227 | ||
cd8c82e8 | 2228 | s->dma_address = __map_single(dev, domain->priv, |
832a90c3 JR |
2229 | paddr, s->length, dir, false, |
2230 | dma_mask); | |
65b050ad JR |
2231 | |
2232 | if (s->dma_address) { | |
2233 | s->dma_length = s->length; | |
2234 | mapped_elems++; | |
2235 | } else | |
2236 | goto unmap; | |
65b050ad JR |
2237 | } |
2238 | ||
17b124bf | 2239 | domain_flush_complete(domain); |
65b050ad JR |
2240 | |
2241 | out: | |
2242 | spin_unlock_irqrestore(&domain->lock, flags); | |
2243 | ||
2244 | return mapped_elems; | |
2245 | unmap: | |
2246 | for_each_sg(sglist, s, mapped_elems, i) { | |
2247 | if (s->dma_address) | |
cd8c82e8 | 2248 | __unmap_single(domain->priv, s->dma_address, |
65b050ad JR |
2249 | s->dma_length, dir); |
2250 | s->dma_address = s->dma_length = 0; | |
2251 | } | |
2252 | ||
2253 | mapped_elems = 0; | |
2254 | ||
2255 | goto out; | |
2256 | } | |
2257 | ||
431b2a20 JR |
2258 | /* |
2259 | * The exported map_sg function for dma_ops (handles scatter-gather | |
2260 | * lists). | |
2261 | */ | |
65b050ad | 2262 | static void unmap_sg(struct device *dev, struct scatterlist *sglist, |
160c1d8e FT |
2263 | int nelems, enum dma_data_direction dir, |
2264 | struct dma_attrs *attrs) | |
65b050ad JR |
2265 | { |
2266 | unsigned long flags; | |
65b050ad JR |
2267 | struct protection_domain *domain; |
2268 | struct scatterlist *s; | |
65b050ad JR |
2269 | int i; |
2270 | ||
55877a6b JR |
2271 | INC_STATS_COUNTER(cnt_unmap_sg); |
2272 | ||
94f6d190 JR |
2273 | domain = get_domain(dev); |
2274 | if (IS_ERR(domain)) | |
5b28df6f JR |
2275 | return; |
2276 | ||
65b050ad JR |
2277 | spin_lock_irqsave(&domain->lock, flags); |
2278 | ||
2279 | for_each_sg(sglist, s, nelems, i) { | |
cd8c82e8 | 2280 | __unmap_single(domain->priv, s->dma_address, |
65b050ad | 2281 | s->dma_length, dir); |
65b050ad JR |
2282 | s->dma_address = s->dma_length = 0; |
2283 | } | |
2284 | ||
17b124bf | 2285 | domain_flush_complete(domain); |
65b050ad JR |
2286 | |
2287 | spin_unlock_irqrestore(&domain->lock, flags); | |
2288 | } | |
2289 | ||
431b2a20 JR |
2290 | /* |
2291 | * The exported alloc_coherent function for dma_ops. | |
2292 | */ | |
5d8b53cf JR |
2293 | static void *alloc_coherent(struct device *dev, size_t size, |
2294 | dma_addr_t *dma_addr, gfp_t flag) | |
2295 | { | |
2296 | unsigned long flags; | |
2297 | void *virt_addr; | |
5d8b53cf | 2298 | struct protection_domain *domain; |
5d8b53cf | 2299 | phys_addr_t paddr; |
832a90c3 | 2300 | u64 dma_mask = dev->coherent_dma_mask; |
5d8b53cf | 2301 | |
c8f0fb36 JR |
2302 | INC_STATS_COUNTER(cnt_alloc_coherent); |
2303 | ||
94f6d190 JR |
2304 | domain = get_domain(dev); |
2305 | if (PTR_ERR(domain) == -EINVAL) { | |
f99c0f1c JR |
2306 | virt_addr = (void *)__get_free_pages(flag, get_order(size)); |
2307 | *dma_addr = __pa(virt_addr); | |
2308 | return virt_addr; | |
94f6d190 JR |
2309 | } else if (IS_ERR(domain)) |
2310 | return NULL; | |
5d8b53cf | 2311 | |
f99c0f1c JR |
2312 | dma_mask = dev->coherent_dma_mask; |
2313 | flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32); | |
2314 | flag |= __GFP_ZERO; | |
5d8b53cf JR |
2315 | |
2316 | virt_addr = (void *)__get_free_pages(flag, get_order(size)); | |
2317 | if (!virt_addr) | |
b25ae679 | 2318 | return NULL; |
5d8b53cf | 2319 | |
5d8b53cf JR |
2320 | paddr = virt_to_phys(virt_addr); |
2321 | ||
832a90c3 JR |
2322 | if (!dma_mask) |
2323 | dma_mask = *dev->dma_mask; | |
2324 | ||
5d8b53cf JR |
2325 | spin_lock_irqsave(&domain->lock, flags); |
2326 | ||
cd8c82e8 | 2327 | *dma_addr = __map_single(dev, domain->priv, paddr, |
832a90c3 | 2328 | size, DMA_BIDIRECTIONAL, true, dma_mask); |
5d8b53cf | 2329 | |
8fd524b3 | 2330 | if (*dma_addr == DMA_ERROR_CODE) { |
367d04c4 | 2331 | spin_unlock_irqrestore(&domain->lock, flags); |
5b28df6f | 2332 | goto out_free; |
367d04c4 | 2333 | } |
5d8b53cf | 2334 | |
17b124bf | 2335 | domain_flush_complete(domain); |
5d8b53cf | 2336 | |
5d8b53cf JR |
2337 | spin_unlock_irqrestore(&domain->lock, flags); |
2338 | ||
2339 | return virt_addr; | |
5b28df6f JR |
2340 | |
2341 | out_free: | |
2342 | ||
2343 | free_pages((unsigned long)virt_addr, get_order(size)); | |
2344 | ||
2345 | return NULL; | |
5d8b53cf JR |
2346 | } |
2347 | ||
431b2a20 JR |
2348 | /* |
2349 | * The exported free_coherent function for dma_ops. | |
431b2a20 | 2350 | */ |
5d8b53cf JR |
2351 | static void free_coherent(struct device *dev, size_t size, |
2352 | void *virt_addr, dma_addr_t dma_addr) | |
2353 | { | |
2354 | unsigned long flags; | |
5d8b53cf | 2355 | struct protection_domain *domain; |
5d8b53cf | 2356 | |
5d31ee7e JR |
2357 | INC_STATS_COUNTER(cnt_free_coherent); |
2358 | ||
94f6d190 JR |
2359 | domain = get_domain(dev); |
2360 | if (IS_ERR(domain)) | |
5b28df6f JR |
2361 | goto free_mem; |
2362 | ||
5d8b53cf JR |
2363 | spin_lock_irqsave(&domain->lock, flags); |
2364 | ||
cd8c82e8 | 2365 | __unmap_single(domain->priv, dma_addr, size, DMA_BIDIRECTIONAL); |
5d8b53cf | 2366 | |
17b124bf | 2367 | domain_flush_complete(domain); |
5d8b53cf JR |
2368 | |
2369 | spin_unlock_irqrestore(&domain->lock, flags); | |
2370 | ||
2371 | free_mem: | |
2372 | free_pages((unsigned long)virt_addr, get_order(size)); | |
2373 | } | |
2374 | ||
b39ba6ad JR |
2375 | /* |
2376 | * This function is called by the DMA layer to find out if we can handle a | |
2377 | * particular device. It is part of the dma_ops. | |
2378 | */ | |
2379 | static int amd_iommu_dma_supported(struct device *dev, u64 mask) | |
2380 | { | |
420aef8a | 2381 | return check_device(dev); |
b39ba6ad JR |
2382 | } |
2383 | ||
c432f3df | 2384 | /* |
431b2a20 JR |
2385 | * The function for pre-allocating protection domains. |
2386 | * | |
c432f3df JR |
2387 | * If the driver core informs the DMA layer if a driver grabs a device |
2388 | * we don't need to preallocate the protection domains anymore. | |
2389 | * For now we have to. | |
2390 | */ | |
0e93dd88 | 2391 | static void prealloc_protection_domains(void) |
c432f3df JR |
2392 | { |
2393 | struct pci_dev *dev = NULL; | |
2394 | struct dma_ops_domain *dma_dom; | |
98fc5a69 | 2395 | u16 devid; |
c432f3df | 2396 | |
d18c69d3 | 2397 | for_each_pci_dev(dev) { |
98fc5a69 JR |
2398 | |
2399 | /* Do we handle this device? */ | |
2400 | if (!check_device(&dev->dev)) | |
c432f3df | 2401 | continue; |
98fc5a69 JR |
2402 | |
2403 | /* Is there already any domain for it? */ | |
15898bbc | 2404 | if (domain_for_device(&dev->dev)) |
c432f3df | 2405 | continue; |
98fc5a69 JR |
2406 | |
2407 | devid = get_device_id(&dev->dev); | |
2408 | ||
87a64d52 | 2409 | dma_dom = dma_ops_domain_alloc(); |
c432f3df JR |
2410 | if (!dma_dom) |
2411 | continue; | |
2412 | init_unity_mappings_for_device(dma_dom, devid); | |
bd60b735 JR |
2413 | dma_dom->target_dev = devid; |
2414 | ||
15898bbc | 2415 | attach_device(&dev->dev, &dma_dom->domain); |
be831297 | 2416 | |
bd60b735 | 2417 | list_add_tail(&dma_dom->list, &iommu_pd_list); |
c432f3df JR |
2418 | } |
2419 | } | |
2420 | ||
160c1d8e | 2421 | static struct dma_map_ops amd_iommu_dma_ops = { |
6631ee9d JR |
2422 | .alloc_coherent = alloc_coherent, |
2423 | .free_coherent = free_coherent, | |
51491367 FT |
2424 | .map_page = map_page, |
2425 | .unmap_page = unmap_page, | |
6631ee9d JR |
2426 | .map_sg = map_sg, |
2427 | .unmap_sg = unmap_sg, | |
b39ba6ad | 2428 | .dma_supported = amd_iommu_dma_supported, |
6631ee9d JR |
2429 | }; |
2430 | ||
27c2127a JR |
2431 | static unsigned device_dma_ops_init(void) |
2432 | { | |
2433 | struct pci_dev *pdev = NULL; | |
2434 | unsigned unhandled = 0; | |
2435 | ||
2436 | for_each_pci_dev(pdev) { | |
2437 | if (!check_device(&pdev->dev)) { | |
2438 | unhandled += 1; | |
2439 | continue; | |
2440 | } | |
2441 | ||
2442 | pdev->dev.archdata.dma_ops = &amd_iommu_dma_ops; | |
2443 | } | |
2444 | ||
2445 | return unhandled; | |
2446 | } | |
2447 | ||
431b2a20 JR |
2448 | /* |
2449 | * The function which clues the AMD IOMMU driver into dma_ops. | |
2450 | */ | |
f5325094 JR |
2451 | |
2452 | void __init amd_iommu_init_api(void) | |
2453 | { | |
2454 | register_iommu(&amd_iommu_ops); | |
2455 | } | |
2456 | ||
6631ee9d JR |
2457 | int __init amd_iommu_init_dma_ops(void) |
2458 | { | |
2459 | struct amd_iommu *iommu; | |
27c2127a | 2460 | int ret, unhandled; |
6631ee9d | 2461 | |
431b2a20 JR |
2462 | /* |
2463 | * first allocate a default protection domain for every IOMMU we | |
2464 | * found in the system. Devices not assigned to any other | |
2465 | * protection domain will be assigned to the default one. | |
2466 | */ | |
3bd22172 | 2467 | for_each_iommu(iommu) { |
87a64d52 | 2468 | iommu->default_dom = dma_ops_domain_alloc(); |
6631ee9d JR |
2469 | if (iommu->default_dom == NULL) |
2470 | return -ENOMEM; | |
e2dc14a2 | 2471 | iommu->default_dom->domain.flags |= PD_DEFAULT_MASK; |
6631ee9d JR |
2472 | ret = iommu_init_unity_mappings(iommu); |
2473 | if (ret) | |
2474 | goto free_domains; | |
2475 | } | |
2476 | ||
431b2a20 | 2477 | /* |
8793abeb | 2478 | * Pre-allocate the protection domains for each device. |
431b2a20 | 2479 | */ |
8793abeb | 2480 | prealloc_protection_domains(); |
6631ee9d JR |
2481 | |
2482 | iommu_detected = 1; | |
75f1cdf1 | 2483 | swiotlb = 0; |
6631ee9d | 2484 | |
431b2a20 | 2485 | /* Make the driver finally visible to the drivers */ |
27c2127a JR |
2486 | unhandled = device_dma_ops_init(); |
2487 | if (unhandled && max_pfn > MAX_DMA32_PFN) { | |
2488 | /* There are unhandled devices - initialize swiotlb for them */ | |
2489 | swiotlb = 1; | |
2490 | } | |
6631ee9d | 2491 | |
7f26508b JR |
2492 | amd_iommu_stats_init(); |
2493 | ||
6631ee9d JR |
2494 | return 0; |
2495 | ||
2496 | free_domains: | |
2497 | ||
3bd22172 | 2498 | for_each_iommu(iommu) { |
6631ee9d JR |
2499 | if (iommu->default_dom) |
2500 | dma_ops_domain_free(iommu->default_dom); | |
2501 | } | |
2502 | ||
2503 | return ret; | |
2504 | } | |
6d98cd80 JR |
2505 | |
2506 | /***************************************************************************** | |
2507 | * | |
2508 | * The following functions belong to the exported interface of AMD IOMMU | |
2509 | * | |
2510 | * This interface allows access to lower level functions of the IOMMU | |
2511 | * like protection domain handling and assignement of devices to domains | |
2512 | * which is not possible with the dma_ops interface. | |
2513 | * | |
2514 | *****************************************************************************/ | |
2515 | ||
6d98cd80 JR |
2516 | static void cleanup_domain(struct protection_domain *domain) |
2517 | { | |
492667da | 2518 | struct iommu_dev_data *dev_data, *next; |
6d98cd80 | 2519 | unsigned long flags; |
6d98cd80 JR |
2520 | |
2521 | write_lock_irqsave(&amd_iommu_devtable_lock, flags); | |
2522 | ||
492667da | 2523 | list_for_each_entry_safe(dev_data, next, &domain->dev_list, list) { |
ec9e79ef | 2524 | __detach_device(dev_data); |
492667da JR |
2525 | atomic_set(&dev_data->bind, 0); |
2526 | } | |
6d98cd80 JR |
2527 | |
2528 | write_unlock_irqrestore(&amd_iommu_devtable_lock, flags); | |
2529 | } | |
2530 | ||
2650815f JR |
2531 | static void protection_domain_free(struct protection_domain *domain) |
2532 | { | |
2533 | if (!domain) | |
2534 | return; | |
2535 | ||
aeb26f55 JR |
2536 | del_domain_from_list(domain); |
2537 | ||
2650815f JR |
2538 | if (domain->id) |
2539 | domain_id_free(domain->id); | |
2540 | ||
2541 | kfree(domain); | |
2542 | } | |
2543 | ||
2544 | static struct protection_domain *protection_domain_alloc(void) | |
c156e347 JR |
2545 | { |
2546 | struct protection_domain *domain; | |
2547 | ||
2548 | domain = kzalloc(sizeof(*domain), GFP_KERNEL); | |
2549 | if (!domain) | |
2650815f | 2550 | return NULL; |
c156e347 JR |
2551 | |
2552 | spin_lock_init(&domain->lock); | |
5d214fe6 | 2553 | mutex_init(&domain->api_lock); |
c156e347 JR |
2554 | domain->id = domain_id_alloc(); |
2555 | if (!domain->id) | |
2650815f | 2556 | goto out_err; |
7c392cbe | 2557 | INIT_LIST_HEAD(&domain->dev_list); |
2650815f | 2558 | |
aeb26f55 JR |
2559 | add_domain_to_list(domain); |
2560 | ||
2650815f JR |
2561 | return domain; |
2562 | ||
2563 | out_err: | |
2564 | kfree(domain); | |
2565 | ||
2566 | return NULL; | |
2567 | } | |
2568 | ||
2569 | static int amd_iommu_domain_init(struct iommu_domain *dom) | |
2570 | { | |
2571 | struct protection_domain *domain; | |
2572 | ||
2573 | domain = protection_domain_alloc(); | |
2574 | if (!domain) | |
c156e347 | 2575 | goto out_free; |
2650815f JR |
2576 | |
2577 | domain->mode = PAGE_MODE_3_LEVEL; | |
c156e347 JR |
2578 | domain->pt_root = (void *)get_zeroed_page(GFP_KERNEL); |
2579 | if (!domain->pt_root) | |
2580 | goto out_free; | |
2581 | ||
2582 | dom->priv = domain; | |
2583 | ||
2584 | return 0; | |
2585 | ||
2586 | out_free: | |
2650815f | 2587 | protection_domain_free(domain); |
c156e347 JR |
2588 | |
2589 | return -ENOMEM; | |
2590 | } | |
2591 | ||
98383fc3 JR |
2592 | static void amd_iommu_domain_destroy(struct iommu_domain *dom) |
2593 | { | |
2594 | struct protection_domain *domain = dom->priv; | |
2595 | ||
2596 | if (!domain) | |
2597 | return; | |
2598 | ||
2599 | if (domain->dev_cnt > 0) | |
2600 | cleanup_domain(domain); | |
2601 | ||
2602 | BUG_ON(domain->dev_cnt != 0); | |
2603 | ||
2604 | free_pagetable(domain); | |
2605 | ||
8b408fe4 | 2606 | protection_domain_free(domain); |
98383fc3 JR |
2607 | |
2608 | dom->priv = NULL; | |
2609 | } | |
2610 | ||
684f2888 JR |
2611 | static void amd_iommu_detach_device(struct iommu_domain *dom, |
2612 | struct device *dev) | |
2613 | { | |
657cbb6b | 2614 | struct iommu_dev_data *dev_data = dev->archdata.iommu; |
684f2888 | 2615 | struct amd_iommu *iommu; |
684f2888 JR |
2616 | u16 devid; |
2617 | ||
98fc5a69 | 2618 | if (!check_device(dev)) |
684f2888 JR |
2619 | return; |
2620 | ||
98fc5a69 | 2621 | devid = get_device_id(dev); |
684f2888 | 2622 | |
657cbb6b | 2623 | if (dev_data->domain != NULL) |
15898bbc | 2624 | detach_device(dev); |
684f2888 JR |
2625 | |
2626 | iommu = amd_iommu_rlookup_table[devid]; | |
2627 | if (!iommu) | |
2628 | return; | |
2629 | ||
684f2888 JR |
2630 | iommu_completion_wait(iommu); |
2631 | } | |
2632 | ||
01106066 JR |
2633 | static int amd_iommu_attach_device(struct iommu_domain *dom, |
2634 | struct device *dev) | |
2635 | { | |
2636 | struct protection_domain *domain = dom->priv; | |
657cbb6b | 2637 | struct iommu_dev_data *dev_data; |
01106066 | 2638 | struct amd_iommu *iommu; |
15898bbc | 2639 | int ret; |
01106066 | 2640 | |
98fc5a69 | 2641 | if (!check_device(dev)) |
01106066 JR |
2642 | return -EINVAL; |
2643 | ||
657cbb6b JR |
2644 | dev_data = dev->archdata.iommu; |
2645 | ||
f62dda66 | 2646 | iommu = amd_iommu_rlookup_table[dev_data->devid]; |
01106066 JR |
2647 | if (!iommu) |
2648 | return -EINVAL; | |
2649 | ||
657cbb6b | 2650 | if (dev_data->domain) |
15898bbc | 2651 | detach_device(dev); |
01106066 | 2652 | |
15898bbc | 2653 | ret = attach_device(dev, domain); |
01106066 JR |
2654 | |
2655 | iommu_completion_wait(iommu); | |
2656 | ||
15898bbc | 2657 | return ret; |
01106066 JR |
2658 | } |
2659 | ||
468e2366 JR |
2660 | static int amd_iommu_map(struct iommu_domain *dom, unsigned long iova, |
2661 | phys_addr_t paddr, int gfp_order, int iommu_prot) | |
c6229ca6 | 2662 | { |
468e2366 | 2663 | unsigned long page_size = 0x1000UL << gfp_order; |
c6229ca6 | 2664 | struct protection_domain *domain = dom->priv; |
c6229ca6 JR |
2665 | int prot = 0; |
2666 | int ret; | |
2667 | ||
2668 | if (iommu_prot & IOMMU_READ) | |
2669 | prot |= IOMMU_PROT_IR; | |
2670 | if (iommu_prot & IOMMU_WRITE) | |
2671 | prot |= IOMMU_PROT_IW; | |
2672 | ||
5d214fe6 | 2673 | mutex_lock(&domain->api_lock); |
795e74f7 | 2674 | ret = iommu_map_page(domain, iova, paddr, prot, page_size); |
5d214fe6 JR |
2675 | mutex_unlock(&domain->api_lock); |
2676 | ||
795e74f7 | 2677 | return ret; |
c6229ca6 JR |
2678 | } |
2679 | ||
468e2366 JR |
2680 | static int amd_iommu_unmap(struct iommu_domain *dom, unsigned long iova, |
2681 | int gfp_order) | |
eb74ff6c | 2682 | { |
eb74ff6c | 2683 | struct protection_domain *domain = dom->priv; |
468e2366 | 2684 | unsigned long page_size, unmap_size; |
eb74ff6c | 2685 | |
468e2366 | 2686 | page_size = 0x1000UL << gfp_order; |
eb74ff6c | 2687 | |
5d214fe6 | 2688 | mutex_lock(&domain->api_lock); |
468e2366 | 2689 | unmap_size = iommu_unmap_page(domain, iova, page_size); |
795e74f7 | 2690 | mutex_unlock(&domain->api_lock); |
eb74ff6c | 2691 | |
17b124bf | 2692 | domain_flush_tlb_pde(domain); |
5d214fe6 | 2693 | |
468e2366 | 2694 | return get_order(unmap_size); |
eb74ff6c JR |
2695 | } |
2696 | ||
645c4c8d JR |
2697 | static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom, |
2698 | unsigned long iova) | |
2699 | { | |
2700 | struct protection_domain *domain = dom->priv; | |
f03152bb | 2701 | unsigned long offset_mask; |
645c4c8d | 2702 | phys_addr_t paddr; |
f03152bb | 2703 | u64 *pte, __pte; |
645c4c8d | 2704 | |
24cd7723 | 2705 | pte = fetch_pte(domain, iova); |
645c4c8d | 2706 | |
a6d41a40 | 2707 | if (!pte || !IOMMU_PTE_PRESENT(*pte)) |
645c4c8d JR |
2708 | return 0; |
2709 | ||
f03152bb JR |
2710 | if (PM_PTE_LEVEL(*pte) == 0) |
2711 | offset_mask = PAGE_SIZE - 1; | |
2712 | else | |
2713 | offset_mask = PTE_PAGE_SIZE(*pte) - 1; | |
2714 | ||
2715 | __pte = *pte & PM_ADDR_MASK; | |
2716 | paddr = (__pte & ~offset_mask) | (iova & offset_mask); | |
645c4c8d JR |
2717 | |
2718 | return paddr; | |
2719 | } | |
2720 | ||
dbb9fd86 SY |
2721 | static int amd_iommu_domain_has_cap(struct iommu_domain *domain, |
2722 | unsigned long cap) | |
2723 | { | |
80a506b8 JR |
2724 | switch (cap) { |
2725 | case IOMMU_CAP_CACHE_COHERENCY: | |
2726 | return 1; | |
2727 | } | |
2728 | ||
dbb9fd86 SY |
2729 | return 0; |
2730 | } | |
2731 | ||
26961efe JR |
2732 | static struct iommu_ops amd_iommu_ops = { |
2733 | .domain_init = amd_iommu_domain_init, | |
2734 | .domain_destroy = amd_iommu_domain_destroy, | |
2735 | .attach_dev = amd_iommu_attach_device, | |
2736 | .detach_dev = amd_iommu_detach_device, | |
468e2366 JR |
2737 | .map = amd_iommu_map, |
2738 | .unmap = amd_iommu_unmap, | |
26961efe | 2739 | .iova_to_phys = amd_iommu_iova_to_phys, |
dbb9fd86 | 2740 | .domain_has_cap = amd_iommu_domain_has_cap, |
26961efe JR |
2741 | }; |
2742 | ||
0feae533 JR |
2743 | /***************************************************************************** |
2744 | * | |
2745 | * The next functions do a basic initialization of IOMMU for pass through | |
2746 | * mode | |
2747 | * | |
2748 | * In passthrough mode the IOMMU is initialized and enabled but not used for | |
2749 | * DMA-API translation. | |
2750 | * | |
2751 | *****************************************************************************/ | |
2752 | ||
2753 | int __init amd_iommu_init_passthrough(void) | |
2754 | { | |
15898bbc | 2755 | struct amd_iommu *iommu; |
0feae533 | 2756 | struct pci_dev *dev = NULL; |
15898bbc | 2757 | u16 devid; |
0feae533 | 2758 | |
af901ca1 | 2759 | /* allocate passthrough domain */ |
0feae533 JR |
2760 | pt_domain = protection_domain_alloc(); |
2761 | if (!pt_domain) | |
2762 | return -ENOMEM; | |
2763 | ||
2764 | pt_domain->mode |= PAGE_MODE_NONE; | |
2765 | ||
6c54aabd | 2766 | for_each_pci_dev(dev) { |
98fc5a69 | 2767 | if (!check_device(&dev->dev)) |
0feae533 JR |
2768 | continue; |
2769 | ||
98fc5a69 JR |
2770 | devid = get_device_id(&dev->dev); |
2771 | ||
15898bbc | 2772 | iommu = amd_iommu_rlookup_table[devid]; |
0feae533 JR |
2773 | if (!iommu) |
2774 | continue; | |
2775 | ||
15898bbc | 2776 | attach_device(&dev->dev, pt_domain); |
0feae533 JR |
2777 | } |
2778 | ||
2779 | pr_info("AMD-Vi: Initialized for Passthrough Mode\n"); | |
2780 | ||
2781 | return 0; | |
2782 | } |