aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--arch/x86/Kconfig1
-rw-r--r--arch/x86/include/asm/amd_iommu_proto.h12
-rw-r--r--arch/x86/include/asm/amd_iommu_types.h28
-rw-r--r--arch/x86/kernel/amd_iommu.c519
-rw-r--r--arch/x86/kernel/amd_iommu_init.c39
-rw-r--r--drivers/pci/intel-iommu.c1
-rw-r--r--drivers/pci/iov.c1
-rw-r--r--drivers/pci/pci.h37
-rw-r--r--include/linux/pci-ats.h52
-rw-r--r--lib/dma-debug.c18
10 files changed, 438 insertions, 270 deletions
diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig
index cc6c53a95bfd..8cc29da2d689 100644
--- a/arch/x86/Kconfig
+++ b/arch/x86/Kconfig
@@ -690,6 +690,7 @@ config AMD_IOMMU
690 bool "AMD IOMMU support" 690 bool "AMD IOMMU support"
691 select SWIOTLB 691 select SWIOTLB
692 select PCI_MSI 692 select PCI_MSI
693 select PCI_IOV
693 depends on X86_64 && PCI && ACPI 694 depends on X86_64 && PCI && ACPI
694 ---help--- 695 ---help---
695 With this option you can enable support for AMD IOMMU hardware in 696 With this option you can enable support for AMD IOMMU hardware in
diff --git a/arch/x86/include/asm/amd_iommu_proto.h b/arch/x86/include/asm/amd_iommu_proto.h
index 916bc8111a01..a4ae6c3875eb 100644
--- a/arch/x86/include/asm/amd_iommu_proto.h
+++ b/arch/x86/include/asm/amd_iommu_proto.h
@@ -19,13 +19,11 @@
19#ifndef _ASM_X86_AMD_IOMMU_PROTO_H 19#ifndef _ASM_X86_AMD_IOMMU_PROTO_H
20#define _ASM_X86_AMD_IOMMU_PROTO_H 20#define _ASM_X86_AMD_IOMMU_PROTO_H
21 21
22struct amd_iommu; 22#include <asm/amd_iommu_types.h>
23 23
24extern int amd_iommu_init_dma_ops(void); 24extern int amd_iommu_init_dma_ops(void);
25extern int amd_iommu_init_passthrough(void); 25extern int amd_iommu_init_passthrough(void);
26extern irqreturn_t amd_iommu_int_handler(int irq, void *data); 26extern irqreturn_t amd_iommu_int_handler(int irq, void *data);
27extern void amd_iommu_flush_all_domains(void);
28extern void amd_iommu_flush_all_devices(void);
29extern void amd_iommu_apply_erratum_63(u16 devid); 27extern void amd_iommu_apply_erratum_63(u16 devid);
30extern void amd_iommu_reset_cmd_buffer(struct amd_iommu *iommu); 28extern void amd_iommu_reset_cmd_buffer(struct amd_iommu *iommu);
31extern int amd_iommu_init_devices(void); 29extern int amd_iommu_init_devices(void);
@@ -44,4 +42,12 @@ static inline bool is_rd890_iommu(struct pci_dev *pdev)
44 (pdev->device == PCI_DEVICE_ID_RD890_IOMMU); 42 (pdev->device == PCI_DEVICE_ID_RD890_IOMMU);
45} 43}
46 44
45static inline bool iommu_feature(struct amd_iommu *iommu, u64 f)
46{
47 if (!(iommu->cap & (1 << IOMMU_CAP_EFR)))
48 return false;
49
50 return !!(iommu->features & f);
51}
52
47#endif /* _ASM_X86_AMD_IOMMU_PROTO_H */ 53#endif /* _ASM_X86_AMD_IOMMU_PROTO_H */
diff --git a/arch/x86/include/asm/amd_iommu_types.h b/arch/x86/include/asm/amd_iommu_types.h
index e3509fc303bf..4c9982995414 100644
--- a/arch/x86/include/asm/amd_iommu_types.h
+++ b/arch/x86/include/asm/amd_iommu_types.h
@@ -68,12 +68,25 @@
68#define MMIO_CONTROL_OFFSET 0x0018 68#define MMIO_CONTROL_OFFSET 0x0018
69#define MMIO_EXCL_BASE_OFFSET 0x0020 69#define MMIO_EXCL_BASE_OFFSET 0x0020
70#define MMIO_EXCL_LIMIT_OFFSET 0x0028 70#define MMIO_EXCL_LIMIT_OFFSET 0x0028
71#define MMIO_EXT_FEATURES 0x0030
71#define MMIO_CMD_HEAD_OFFSET 0x2000 72#define MMIO_CMD_HEAD_OFFSET 0x2000
72#define MMIO_CMD_TAIL_OFFSET 0x2008 73#define MMIO_CMD_TAIL_OFFSET 0x2008
73#define MMIO_EVT_HEAD_OFFSET 0x2010 74#define MMIO_EVT_HEAD_OFFSET 0x2010
74#define MMIO_EVT_TAIL_OFFSET 0x2018 75#define MMIO_EVT_TAIL_OFFSET 0x2018
75#define MMIO_STATUS_OFFSET 0x2020 76#define MMIO_STATUS_OFFSET 0x2020
76 77
78
79/* Extended Feature Bits */
80#define FEATURE_PREFETCH (1ULL<<0)
81#define FEATURE_PPR (1ULL<<1)
82#define FEATURE_X2APIC (1ULL<<2)
83#define FEATURE_NX (1ULL<<3)
84#define FEATURE_GT (1ULL<<4)
85#define FEATURE_IA (1ULL<<6)
86#define FEATURE_GA (1ULL<<7)
87#define FEATURE_HE (1ULL<<8)
88#define FEATURE_PC (1ULL<<9)
89
77/* MMIO status bits */ 90/* MMIO status bits */
78#define MMIO_STATUS_COM_WAIT_INT_MASK 0x04 91#define MMIO_STATUS_COM_WAIT_INT_MASK 0x04
79 92
@@ -113,7 +126,9 @@
113/* command specific defines */ 126/* command specific defines */
114#define CMD_COMPL_WAIT 0x01 127#define CMD_COMPL_WAIT 0x01
115#define CMD_INV_DEV_ENTRY 0x02 128#define CMD_INV_DEV_ENTRY 0x02
116#define CMD_INV_IOMMU_PAGES 0x03 129#define CMD_INV_IOMMU_PAGES 0x03
130#define CMD_INV_IOTLB_PAGES 0x04
131#define CMD_INV_ALL 0x08
117 132
118#define CMD_COMPL_WAIT_STORE_MASK 0x01 133#define CMD_COMPL_WAIT_STORE_MASK 0x01
119#define CMD_COMPL_WAIT_INT_MASK 0x02 134#define CMD_COMPL_WAIT_INT_MASK 0x02
@@ -215,6 +230,8 @@
215#define IOMMU_PTE_IR (1ULL << 61) 230#define IOMMU_PTE_IR (1ULL << 61)
216#define IOMMU_PTE_IW (1ULL << 62) 231#define IOMMU_PTE_IW (1ULL << 62)
217 232
233#define DTE_FLAG_IOTLB 0x01
234
218#define IOMMU_PAGE_MASK (((1ULL << 52) - 1) & ~0xfffULL) 235#define IOMMU_PAGE_MASK (((1ULL << 52) - 1) & ~0xfffULL)
219#define IOMMU_PTE_PRESENT(pte) ((pte) & IOMMU_PTE_P) 236#define IOMMU_PTE_PRESENT(pte) ((pte) & IOMMU_PTE_P)
220#define IOMMU_PTE_PAGE(pte) (phys_to_virt((pte) & IOMMU_PAGE_MASK)) 237#define IOMMU_PTE_PAGE(pte) (phys_to_virt((pte) & IOMMU_PAGE_MASK))
@@ -227,6 +244,7 @@
227/* IOMMU capabilities */ 244/* IOMMU capabilities */
228#define IOMMU_CAP_IOTLB 24 245#define IOMMU_CAP_IOTLB 24
229#define IOMMU_CAP_NPCACHE 26 246#define IOMMU_CAP_NPCACHE 26
247#define IOMMU_CAP_EFR 27
230 248
231#define MAX_DOMAIN_ID 65536 249#define MAX_DOMAIN_ID 65536
232 250
@@ -249,6 +267,8 @@ extern bool amd_iommu_dump;
249 267
250/* global flag if IOMMUs cache non-present entries */ 268/* global flag if IOMMUs cache non-present entries */
251extern bool amd_iommu_np_cache; 269extern bool amd_iommu_np_cache;
270/* Only true if all IOMMUs support device IOTLBs */
271extern bool amd_iommu_iotlb_sup;
252 272
253/* 273/*
254 * Make iterating over all IOMMUs easier 274 * Make iterating over all IOMMUs easier
@@ -371,6 +391,9 @@ struct amd_iommu {
371 /* flags read from acpi table */ 391 /* flags read from acpi table */
372 u8 acpi_flags; 392 u8 acpi_flags;
373 393
394 /* Extended features */
395 u64 features;
396
374 /* 397 /*
375 * Capability pointer. There could be more than one IOMMU per PCI 398 * Capability pointer. There could be more than one IOMMU per PCI
376 * device function if there are more than one AMD IOMMU capability 399 * device function if there are more than one AMD IOMMU capability
@@ -409,9 +432,6 @@ struct amd_iommu {
409 /* if one, we need to send a completion wait command */ 432 /* if one, we need to send a completion wait command */
410 bool need_sync; 433 bool need_sync;
411 434
412 /* becomes true if a command buffer reset is running */
413 bool reset_in_progress;
414
415 /* default dma_ops domain for that IOMMU */ 435 /* default dma_ops domain for that IOMMU */
416 struct dma_ops_domain *default_dom; 436 struct dma_ops_domain *default_dom;
417 437
diff --git a/arch/x86/kernel/amd_iommu.c b/arch/x86/kernel/amd_iommu.c
index 57ca77787220..dc5dddafe5c2 100644
--- a/arch/x86/kernel/amd_iommu.c
+++ b/arch/x86/kernel/amd_iommu.c
@@ -18,6 +18,7 @@
18 */ 18 */
19 19
20#include <linux/pci.h> 20#include <linux/pci.h>
21#include <linux/pci-ats.h>
21#include <linux/bitmap.h> 22#include <linux/bitmap.h>
22#include <linux/slab.h> 23#include <linux/slab.h>
23#include <linux/debugfs.h> 24#include <linux/debugfs.h>
@@ -25,6 +26,7 @@
25#include <linux/dma-mapping.h> 26#include <linux/dma-mapping.h>
26#include <linux/iommu-helper.h> 27#include <linux/iommu-helper.h>
27#include <linux/iommu.h> 28#include <linux/iommu.h>
29#include <linux/delay.h>
28#include <asm/proto.h> 30#include <asm/proto.h>
29#include <asm/iommu.h> 31#include <asm/iommu.h>
30#include <asm/gart.h> 32#include <asm/gart.h>
@@ -34,7 +36,7 @@
34 36
35#define CMD_SET_TYPE(cmd, t) ((cmd)->data[1] |= ((t) << 28)) 37#define CMD_SET_TYPE(cmd, t) ((cmd)->data[1] |= ((t) << 28))
36 38
37#define EXIT_LOOP_COUNT 10000000 39#define LOOP_TIMEOUT 100000
38 40
39static DEFINE_RWLOCK(amd_iommu_devtable_lock); 41static DEFINE_RWLOCK(amd_iommu_devtable_lock);
40 42
@@ -57,7 +59,6 @@ struct iommu_cmd {
57 u32 data[4]; 59 u32 data[4];
58}; 60};
59 61
60static void reset_iommu_command_buffer(struct amd_iommu *iommu);
61static void update_domain(struct protection_domain *domain); 62static void update_domain(struct protection_domain *domain);
62 63
63/**************************************************************************** 64/****************************************************************************
@@ -322,8 +323,6 @@ static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
322 break; 323 break;
323 case EVENT_TYPE_ILL_CMD: 324 case EVENT_TYPE_ILL_CMD:
324 printk("ILLEGAL_COMMAND_ERROR address=0x%016llx]\n", address); 325 printk("ILLEGAL_COMMAND_ERROR address=0x%016llx]\n", address);
325 iommu->reset_in_progress = true;
326 reset_iommu_command_buffer(iommu);
327 dump_command(address); 326 dump_command(address);
328 break; 327 break;
329 case EVENT_TYPE_CMD_HARD_ERR: 328 case EVENT_TYPE_CMD_HARD_ERR:
@@ -383,186 +382,289 @@ irqreturn_t amd_iommu_int_handler(int irq, void *data)
383 * 382 *
384 ****************************************************************************/ 383 ****************************************************************************/
385 384
386/* 385static int wait_on_sem(volatile u64 *sem)
387 * Writes the command to the IOMMUs command buffer and informs the 386{
388 * hardware about the new command. Must be called with iommu->lock held. 387 int i = 0;
389 */ 388
390static int __iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd) 389 while (*sem == 0 && i < LOOP_TIMEOUT) {
390 udelay(1);
391 i += 1;
392 }
393
394 if (i == LOOP_TIMEOUT) {
395 pr_alert("AMD-Vi: Completion-Wait loop timed out\n");
396 return -EIO;
397 }
398
399 return 0;
400}
401
402static void copy_cmd_to_buffer(struct amd_iommu *iommu,
403 struct iommu_cmd *cmd,
404 u32 tail)
391{ 405{
392 u32 tail, head;
393 u8 *target; 406 u8 *target;
394 407
395 WARN_ON(iommu->cmd_buf_size & CMD_BUFFER_UNINITIALIZED);
396 tail = readl(iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
397 target = iommu->cmd_buf + tail; 408 target = iommu->cmd_buf + tail;
398 memcpy_toio(target, cmd, sizeof(*cmd)); 409 tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
399 tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size; 410
400 head = readl(iommu->mmio_base + MMIO_CMD_HEAD_OFFSET); 411 /* Copy command to buffer */
401 if (tail == head) 412 memcpy(target, cmd, sizeof(*cmd));
402 return -ENOMEM; 413
414 /* Tell the IOMMU about it */
403 writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET); 415 writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
416}
404 417
405 return 0; 418static void build_completion_wait(struct iommu_cmd *cmd, u64 address)
419{
420 WARN_ON(address & 0x7ULL);
421
422 memset(cmd, 0, sizeof(*cmd));
423 cmd->data[0] = lower_32_bits(__pa(address)) | CMD_COMPL_WAIT_STORE_MASK;
424 cmd->data[1] = upper_32_bits(__pa(address));
425 cmd->data[2] = 1;
426 CMD_SET_TYPE(cmd, CMD_COMPL_WAIT);
427}
428
429static void build_inv_dte(struct iommu_cmd *cmd, u16 devid)
430{
431 memset(cmd, 0, sizeof(*cmd));
432 cmd->data[0] = devid;
433 CMD_SET_TYPE(cmd, CMD_INV_DEV_ENTRY);
434}
435
436static void build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address,
437 size_t size, u16 domid, int pde)
438{
439 u64 pages;
440 int s;
441
442 pages = iommu_num_pages(address, size, PAGE_SIZE);
443 s = 0;
444
445 if (pages > 1) {
446 /*
447 * If we have to flush more than one page, flush all
448 * TLB entries for this domain
449 */
450 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
451 s = 1;
452 }
453
454 address &= PAGE_MASK;
455
456 memset(cmd, 0, sizeof(*cmd));
457 cmd->data[1] |= domid;
458 cmd->data[2] = lower_32_bits(address);
459 cmd->data[3] = upper_32_bits(address);
460 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
461 if (s) /* size bit - we flush more than one 4kb page */
462 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
463 if (pde) /* PDE bit - we wan't flush everything not only the PTEs */
464 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
465}
466
467static void build_inv_iotlb_pages(struct iommu_cmd *cmd, u16 devid, int qdep,
468 u64 address, size_t size)
469{
470 u64 pages;
471 int s;
472
473 pages = iommu_num_pages(address, size, PAGE_SIZE);
474 s = 0;
475
476 if (pages > 1) {
477 /*
478 * If we have to flush more than one page, flush all
479 * TLB entries for this domain
480 */
481 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
482 s = 1;
483 }
484
485 address &= PAGE_MASK;
486
487 memset(cmd, 0, sizeof(*cmd));
488 cmd->data[0] = devid;
489 cmd->data[0] |= (qdep & 0xff) << 24;
490 cmd->data[1] = devid;
491 cmd->data[2] = lower_32_bits(address);
492 cmd->data[3] = upper_32_bits(address);
493 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
494 if (s)
495 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
496}
497
498static void build_inv_all(struct iommu_cmd *cmd)
499{
500 memset(cmd, 0, sizeof(*cmd));
501 CMD_SET_TYPE(cmd, CMD_INV_ALL);
406} 502}
407 503
408/* 504/*
409 * General queuing function for commands. Takes iommu->lock and calls 505 * Writes the command to the IOMMUs command buffer and informs the
410 * __iommu_queue_command(). 506 * hardware about the new command.
411 */ 507 */
412static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd) 508static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
413{ 509{
510 u32 left, tail, head, next_tail;
414 unsigned long flags; 511 unsigned long flags;
415 int ret;
416 512
513 WARN_ON(iommu->cmd_buf_size & CMD_BUFFER_UNINITIALIZED);
514
515again:
417 spin_lock_irqsave(&iommu->lock, flags); 516 spin_lock_irqsave(&iommu->lock, flags);
418 ret = __iommu_queue_command(iommu, cmd);
419 if (!ret)
420 iommu->need_sync = true;
421 spin_unlock_irqrestore(&iommu->lock, flags);
422 517
423 return ret; 518 head = readl(iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
424} 519 tail = readl(iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
520 next_tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
521 left = (head - next_tail) % iommu->cmd_buf_size;
425 522
426/* 523 if (left <= 2) {
427 * This function waits until an IOMMU has completed a completion 524 struct iommu_cmd sync_cmd;
428 * wait command 525 volatile u64 sem = 0;
429 */ 526 int ret;
430static void __iommu_wait_for_completion(struct amd_iommu *iommu)
431{
432 int ready = 0;
433 unsigned status = 0;
434 unsigned long i = 0;
435 527
436 INC_STATS_COUNTER(compl_wait); 528 build_completion_wait(&sync_cmd, (u64)&sem);
529 copy_cmd_to_buffer(iommu, &sync_cmd, tail);
437 530
438 while (!ready && (i < EXIT_LOOP_COUNT)) { 531 spin_unlock_irqrestore(&iommu->lock, flags);
439 ++i; 532
440 /* wait for the bit to become one */ 533 if ((ret = wait_on_sem(&sem)) != 0)
441 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET); 534 return ret;
442 ready = status & MMIO_STATUS_COM_WAIT_INT_MASK; 535
536 goto again;
443 } 537 }
444 538
445 /* set bit back to zero */ 539 copy_cmd_to_buffer(iommu, cmd, tail);
446 status &= ~MMIO_STATUS_COM_WAIT_INT_MASK; 540
447 writel(status, iommu->mmio_base + MMIO_STATUS_OFFSET); 541 /* We need to sync now to make sure all commands are processed */
542 iommu->need_sync = true;
543
544 spin_unlock_irqrestore(&iommu->lock, flags);
448 545
449 if (unlikely(i == EXIT_LOOP_COUNT)) 546 return 0;
450 iommu->reset_in_progress = true;
451} 547}
452 548
453/* 549/*
454 * This function queues a completion wait command into the command 550 * This function queues a completion wait command into the command
455 * buffer of an IOMMU 551 * buffer of an IOMMU
456 */ 552 */
457static int __iommu_completion_wait(struct amd_iommu *iommu) 553static int iommu_completion_wait(struct amd_iommu *iommu)
458{ 554{
459 struct iommu_cmd cmd; 555 struct iommu_cmd cmd;
556 volatile u64 sem = 0;
557 int ret;
460 558
461 memset(&cmd, 0, sizeof(cmd)); 559 if (!iommu->need_sync)
462 cmd.data[0] = CMD_COMPL_WAIT_INT_MASK; 560 return 0;
463 CMD_SET_TYPE(&cmd, CMD_COMPL_WAIT); 561
562 build_completion_wait(&cmd, (u64)&sem);
563
564 ret = iommu_queue_command(iommu, &cmd);
565 if (ret)
566 return ret;
464 567
465 return __iommu_queue_command(iommu, &cmd); 568 return wait_on_sem(&sem);
466} 569}
467 570
468/* 571static int iommu_flush_dte(struct amd_iommu *iommu, u16 devid)
469 * This function is called whenever we need to ensure that the IOMMU has
470 * completed execution of all commands we sent. It sends a
471 * COMPLETION_WAIT command and waits for it to finish. The IOMMU informs
472 * us about that by writing a value to a physical address we pass with
473 * the command.
474 */
475static int iommu_completion_wait(struct amd_iommu *iommu)
476{ 572{
477 int ret = 0; 573 struct iommu_cmd cmd;
478 unsigned long flags;
479 574
480 spin_lock_irqsave(&iommu->lock, flags); 575 build_inv_dte(&cmd, devid);
481 576
482 if (!iommu->need_sync) 577 return iommu_queue_command(iommu, &cmd);
483 goto out; 578}
484 579
485 ret = __iommu_completion_wait(iommu); 580static void iommu_flush_dte_all(struct amd_iommu *iommu)
581{
582 u32 devid;
486 583
487 iommu->need_sync = false; 584 for (devid = 0; devid <= 0xffff; ++devid)
585 iommu_flush_dte(iommu, devid);
488 586
489 if (ret) 587 iommu_completion_wait(iommu);
490 goto out; 588}
491
492 __iommu_wait_for_completion(iommu);
493 589
494out: 590/*
495 spin_unlock_irqrestore(&iommu->lock, flags); 591 * This function uses heavy locking and may disable irqs for some time. But
592 * this is no issue because it is only called during resume.
593 */
594static void iommu_flush_tlb_all(struct amd_iommu *iommu)
595{
596 u32 dom_id;
496 597
497 if (iommu->reset_in_progress) 598 for (dom_id = 0; dom_id <= 0xffff; ++dom_id) {
498 reset_iommu_command_buffer(iommu); 599 struct iommu_cmd cmd;
600 build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
601 dom_id, 1);
602 iommu_queue_command(iommu, &cmd);
603 }
499 604
500 return 0; 605 iommu_completion_wait(iommu);
501} 606}
502 607
503static void iommu_flush_complete(struct protection_domain *domain) 608static void iommu_flush_all(struct amd_iommu *iommu)
504{ 609{
505 int i; 610 struct iommu_cmd cmd;
506 611
507 for (i = 0; i < amd_iommus_present; ++i) { 612 build_inv_all(&cmd);
508 if (!domain->dev_iommu[i])
509 continue;
510 613
511 /* 614 iommu_queue_command(iommu, &cmd);
512 * Devices of this domain are behind this IOMMU 615 iommu_completion_wait(iommu);
513 * We need to wait for completion of all commands. 616}
514 */ 617
515 iommu_completion_wait(amd_iommus[i]); 618void iommu_flush_all_caches(struct amd_iommu *iommu)
619{
620 if (iommu_feature(iommu, FEATURE_IA)) {
621 iommu_flush_all(iommu);
622 } else {
623 iommu_flush_dte_all(iommu);
624 iommu_flush_tlb_all(iommu);
516 } 625 }
517} 626}
518 627
519/* 628/*
520 * Command send function for invalidating a device table entry 629 * Command send function for flushing on-device TLB
521 */ 630 */
522static int iommu_flush_device(struct device *dev) 631static int device_flush_iotlb(struct device *dev, u64 address, size_t size)
523{ 632{
633 struct pci_dev *pdev = to_pci_dev(dev);
524 struct amd_iommu *iommu; 634 struct amd_iommu *iommu;
525 struct iommu_cmd cmd; 635 struct iommu_cmd cmd;
526 u16 devid; 636 u16 devid;
637 int qdep;
527 638
639 qdep = pci_ats_queue_depth(pdev);
528 devid = get_device_id(dev); 640 devid = get_device_id(dev);
529 iommu = amd_iommu_rlookup_table[devid]; 641 iommu = amd_iommu_rlookup_table[devid];
530 642
531 /* Build command */ 643 build_inv_iotlb_pages(&cmd, devid, qdep, address, size);
532 memset(&cmd, 0, sizeof(cmd));
533 CMD_SET_TYPE(&cmd, CMD_INV_DEV_ENTRY);
534 cmd.data[0] = devid;
535 644
536 return iommu_queue_command(iommu, &cmd); 645 return iommu_queue_command(iommu, &cmd);
537} 646}
538 647
539static void __iommu_build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address,
540 u16 domid, int pde, int s)
541{
542 memset(cmd, 0, sizeof(*cmd));
543 address &= PAGE_MASK;
544 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
545 cmd->data[1] |= domid;
546 cmd->data[2] = lower_32_bits(address);
547 cmd->data[3] = upper_32_bits(address);
548 if (s) /* size bit - we flush more than one 4kb page */
549 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
550 if (pde) /* PDE bit - we wan't flush everything not only the PTEs */
551 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
552}
553
554/* 648/*
555 * Generic command send function for invalidaing TLB entries 649 * Command send function for invalidating a device table entry
556 */ 650 */
557static int iommu_queue_inv_iommu_pages(struct amd_iommu *iommu, 651static int device_flush_dte(struct device *dev)
558 u64 address, u16 domid, int pde, int s)
559{ 652{
560 struct iommu_cmd cmd; 653 struct amd_iommu *iommu;
654 struct pci_dev *pdev;
655 u16 devid;
561 int ret; 656 int ret;
562 657
563 __iommu_build_inv_iommu_pages(&cmd, address, domid, pde, s); 658 pdev = to_pci_dev(dev);
659 devid = get_device_id(dev);
660 iommu = amd_iommu_rlookup_table[devid];
564 661
565 ret = iommu_queue_command(iommu, &cmd); 662 ret = iommu_flush_dte(iommu, devid);
663 if (ret)
664 return ret;
665
666 if (pci_ats_enabled(pdev))
667 ret = device_flush_iotlb(dev, 0, ~0UL);
566 668
567 return ret; 669 return ret;
568} 670}
@@ -572,23 +674,14 @@ static int iommu_queue_inv_iommu_pages(struct amd_iommu *iommu,
572 * It invalidates a single PTE if the range to flush is within a single 674 * It invalidates a single PTE if the range to flush is within a single
573 * page. Otherwise it flushes the whole TLB of the IOMMU. 675 * page. Otherwise it flushes the whole TLB of the IOMMU.
574 */ 676 */
575static void __iommu_flush_pages(struct protection_domain *domain, 677static void __domain_flush_pages(struct protection_domain *domain,
576 u64 address, size_t size, int pde) 678 u64 address, size_t size, int pde)
577{ 679{
578 int s = 0, i; 680 struct iommu_dev_data *dev_data;
579 unsigned long pages = iommu_num_pages(address, size, PAGE_SIZE); 681 struct iommu_cmd cmd;
580 682 int ret = 0, i;
581 address &= PAGE_MASK;
582
583 if (pages > 1) {
584 /*
585 * If we have to flush more than one page, flush all
586 * TLB entries for this domain
587 */
588 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
589 s = 1;
590 }
591 683
684 build_inv_iommu_pages(&cmd, address, size, domain->id, pde);
592 685
593 for (i = 0; i < amd_iommus_present; ++i) { 686 for (i = 0; i < amd_iommus_present; ++i) {
594 if (!domain->dev_iommu[i]) 687 if (!domain->dev_iommu[i])
@@ -598,101 +691,70 @@ static void __iommu_flush_pages(struct protection_domain *domain,
598 * Devices of this domain are behind this IOMMU 691 * Devices of this domain are behind this IOMMU
599 * We need a TLB flush 692 * We need a TLB flush
600 */ 693 */
601 iommu_queue_inv_iommu_pages(amd_iommus[i], address, 694 ret |= iommu_queue_command(amd_iommus[i], &cmd);
602 domain->id, pde, s);
603 } 695 }
604 696
605 return; 697 list_for_each_entry(dev_data, &domain->dev_list, list) {
698 struct pci_dev *pdev = to_pci_dev(dev_data->dev);
699
700 if (!pci_ats_enabled(pdev))
701 continue;
702
703 ret |= device_flush_iotlb(dev_data->dev, address, size);
704 }
705
706 WARN_ON(ret);
606} 707}
607 708
608static void iommu_flush_pages(struct protection_domain *domain, 709static void domain_flush_pages(struct protection_domain *domain,
609 u64 address, size_t size) 710 u64 address, size_t size)
610{ 711{
611 __iommu_flush_pages(domain, address, size, 0); 712 __domain_flush_pages(domain, address, size, 0);
612} 713}
613 714
614/* Flush the whole IO/TLB for a given protection domain */ 715/* Flush the whole IO/TLB for a given protection domain */
615static void iommu_flush_tlb(struct protection_domain *domain) 716static void domain_flush_tlb(struct protection_domain *domain)
616{ 717{
617 __iommu_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 0); 718 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 0);
618} 719}
619 720
620/* Flush the whole IO/TLB for a given protection domain - including PDE */ 721/* Flush the whole IO/TLB for a given protection domain - including PDE */
621static void iommu_flush_tlb_pde(struct protection_domain *domain) 722static void domain_flush_tlb_pde(struct protection_domain *domain)
622{ 723{
623 __iommu_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 1); 724 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 1);
624} 725}
625 726
626 727static void domain_flush_complete(struct protection_domain *domain)
627/*
628 * This function flushes the DTEs for all devices in domain
629 */
630static void iommu_flush_domain_devices(struct protection_domain *domain)
631{ 728{
632 struct iommu_dev_data *dev_data; 729 int i;
633 unsigned long flags;
634
635 spin_lock_irqsave(&domain->lock, flags);
636
637 list_for_each_entry(dev_data, &domain->dev_list, list)
638 iommu_flush_device(dev_data->dev);
639
640 spin_unlock_irqrestore(&domain->lock, flags);
641}
642
643static void iommu_flush_all_domain_devices(void)
644{
645 struct protection_domain *domain;
646 unsigned long flags;
647 730
648 spin_lock_irqsave(&amd_iommu_pd_lock, flags); 731 for (i = 0; i < amd_iommus_present; ++i) {
732 if (!domain->dev_iommu[i])
733 continue;
649 734
650 list_for_each_entry(domain, &amd_iommu_pd_list, list) { 735 /*
651 iommu_flush_domain_devices(domain); 736 * Devices of this domain are behind this IOMMU
652 iommu_flush_complete(domain); 737 * We need to wait for completion of all commands.
738 */
739 iommu_completion_wait(amd_iommus[i]);
653 } 740 }
654
655 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
656} 741}
657 742
658void amd_iommu_flush_all_devices(void)
659{
660 iommu_flush_all_domain_devices();
661}
662 743
663/* 744/*
664 * This function uses heavy locking and may disable irqs for some time. But 745 * This function flushes the DTEs for all devices in domain
665 * this is no issue because it is only called during resume.
666 */ 746 */
667void amd_iommu_flush_all_domains(void) 747static void domain_flush_devices(struct protection_domain *domain)
668{ 748{
669 struct protection_domain *domain; 749 struct iommu_dev_data *dev_data;
670 unsigned long flags; 750 unsigned long flags;
671 751
672 spin_lock_irqsave(&amd_iommu_pd_lock, flags); 752 spin_lock_irqsave(&domain->lock, flags);
673
674 list_for_each_entry(domain, &amd_iommu_pd_list, list) {
675 spin_lock(&domain->lock);
676 iommu_flush_tlb_pde(domain);
677 iommu_flush_complete(domain);
678 spin_unlock(&domain->lock);
679 }
680
681 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
682}
683
684static void reset_iommu_command_buffer(struct amd_iommu *iommu)
685{
686 pr_err("AMD-Vi: Resetting IOMMU command buffer\n");
687
688 if (iommu->reset_in_progress)
689 panic("AMD-Vi: ILLEGAL_COMMAND_ERROR while resetting command buffer\n");
690 753
691 amd_iommu_reset_cmd_buffer(iommu); 754 list_for_each_entry(dev_data, &domain->dev_list, list)
692 amd_iommu_flush_all_devices(); 755 device_flush_dte(dev_data->dev);
693 amd_iommu_flush_all_domains();
694 756
695 iommu->reset_in_progress = false; 757 spin_unlock_irqrestore(&domain->lock, flags);
696} 758}
697 759
698/**************************************************************************** 760/****************************************************************************
@@ -1410,17 +1472,22 @@ static bool dma_ops_domain(struct protection_domain *domain)
1410 return domain->flags & PD_DMA_OPS_MASK; 1472 return domain->flags & PD_DMA_OPS_MASK;
1411} 1473}
1412 1474
1413static void set_dte_entry(u16 devid, struct protection_domain *domain) 1475static void set_dte_entry(u16 devid, struct protection_domain *domain, bool ats)
1414{ 1476{
1415 u64 pte_root = virt_to_phys(domain->pt_root); 1477 u64 pte_root = virt_to_phys(domain->pt_root);
1478 u32 flags = 0;
1416 1479
1417 pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK) 1480 pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK)
1418 << DEV_ENTRY_MODE_SHIFT; 1481 << DEV_ENTRY_MODE_SHIFT;
1419 pte_root |= IOMMU_PTE_IR | IOMMU_PTE_IW | IOMMU_PTE_P | IOMMU_PTE_TV; 1482 pte_root |= IOMMU_PTE_IR | IOMMU_PTE_IW | IOMMU_PTE_P | IOMMU_PTE_TV;
1420 1483
1421 amd_iommu_dev_table[devid].data[2] = domain->id; 1484 if (ats)
1422 amd_iommu_dev_table[devid].data[1] = upper_32_bits(pte_root); 1485 flags |= DTE_FLAG_IOTLB;
1423 amd_iommu_dev_table[devid].data[0] = lower_32_bits(pte_root); 1486
1487 amd_iommu_dev_table[devid].data[3] |= flags;
1488 amd_iommu_dev_table[devid].data[2] = domain->id;
1489 amd_iommu_dev_table[devid].data[1] = upper_32_bits(pte_root);
1490 amd_iommu_dev_table[devid].data[0] = lower_32_bits(pte_root);
1424} 1491}
1425 1492
1426static void clear_dte_entry(u16 devid) 1493static void clear_dte_entry(u16 devid)
@@ -1437,34 +1504,42 @@ static void do_attach(struct device *dev, struct protection_domain *domain)
1437{ 1504{
1438 struct iommu_dev_data *dev_data; 1505 struct iommu_dev_data *dev_data;
1439 struct amd_iommu *iommu; 1506 struct amd_iommu *iommu;
1507 struct pci_dev *pdev;
1508 bool ats = false;
1440 u16 devid; 1509 u16 devid;
1441 1510
1442 devid = get_device_id(dev); 1511 devid = get_device_id(dev);
1443 iommu = amd_iommu_rlookup_table[devid]; 1512 iommu = amd_iommu_rlookup_table[devid];
1444 dev_data = get_dev_data(dev); 1513 dev_data = get_dev_data(dev);
1514 pdev = to_pci_dev(dev);
1515
1516 if (amd_iommu_iotlb_sup)
1517 ats = pci_ats_enabled(pdev);
1445 1518
1446 /* Update data structures */ 1519 /* Update data structures */
1447 dev_data->domain = domain; 1520 dev_data->domain = domain;
1448 list_add(&dev_data->list, &domain->dev_list); 1521 list_add(&dev_data->list, &domain->dev_list);
1449 set_dte_entry(devid, domain); 1522 set_dte_entry(devid, domain, ats);
1450 1523
1451 /* Do reference counting */ 1524 /* Do reference counting */
1452 domain->dev_iommu[iommu->index] += 1; 1525 domain->dev_iommu[iommu->index] += 1;
1453 domain->dev_cnt += 1; 1526 domain->dev_cnt += 1;
1454 1527
1455 /* Flush the DTE entry */ 1528 /* Flush the DTE entry */
1456 iommu_flush_device(dev); 1529 device_flush_dte(dev);
1457} 1530}
1458 1531
1459static void do_detach(struct device *dev) 1532static void do_detach(struct device *dev)
1460{ 1533{
1461 struct iommu_dev_data *dev_data; 1534 struct iommu_dev_data *dev_data;
1462 struct amd_iommu *iommu; 1535 struct amd_iommu *iommu;
1536 struct pci_dev *pdev;
1463 u16 devid; 1537 u16 devid;
1464 1538
1465 devid = get_device_id(dev); 1539 devid = get_device_id(dev);
1466 iommu = amd_iommu_rlookup_table[devid]; 1540 iommu = amd_iommu_rlookup_table[devid];
1467 dev_data = get_dev_data(dev); 1541 dev_data = get_dev_data(dev);
1542 pdev = to_pci_dev(dev);
1468 1543
1469 /* decrease reference counters */ 1544 /* decrease reference counters */
1470 dev_data->domain->dev_iommu[iommu->index] -= 1; 1545 dev_data->domain->dev_iommu[iommu->index] -= 1;
@@ -1476,7 +1551,7 @@ static void do_detach(struct device *dev)
1476 clear_dte_entry(devid); 1551 clear_dte_entry(devid);
1477 1552
1478 /* Flush the DTE entry */ 1553 /* Flush the DTE entry */
1479 iommu_flush_device(dev); 1554 device_flush_dte(dev);
1480} 1555}
1481 1556
1482/* 1557/*
@@ -1539,9 +1614,13 @@ out_unlock:
1539static int attach_device(struct device *dev, 1614static int attach_device(struct device *dev,
1540 struct protection_domain *domain) 1615 struct protection_domain *domain)
1541{ 1616{
1617 struct pci_dev *pdev = to_pci_dev(dev);
1542 unsigned long flags; 1618 unsigned long flags;
1543 int ret; 1619 int ret;
1544 1620
1621 if (amd_iommu_iotlb_sup)
1622 pci_enable_ats(pdev, PAGE_SHIFT);
1623
1545 write_lock_irqsave(&amd_iommu_devtable_lock, flags); 1624 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1546 ret = __attach_device(dev, domain); 1625 ret = __attach_device(dev, domain);
1547 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags); 1626 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
@@ -1551,7 +1630,7 @@ static int attach_device(struct device *dev,
1551 * left the caches in the IOMMU dirty. So we have to flush 1630 * left the caches in the IOMMU dirty. So we have to flush
1552 * here to evict all dirty stuff. 1631 * here to evict all dirty stuff.
1553 */ 1632 */
1554 iommu_flush_tlb_pde(domain); 1633 domain_flush_tlb_pde(domain);
1555 1634
1556 return ret; 1635 return ret;
1557} 1636}
@@ -1598,12 +1677,16 @@ static void __detach_device(struct device *dev)
1598 */ 1677 */
1599static void detach_device(struct device *dev) 1678static void detach_device(struct device *dev)
1600{ 1679{
1680 struct pci_dev *pdev = to_pci_dev(dev);
1601 unsigned long flags; 1681 unsigned long flags;
1602 1682
1603 /* lock device table */ 1683 /* lock device table */
1604 write_lock_irqsave(&amd_iommu_devtable_lock, flags); 1684 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1605 __detach_device(dev); 1685 __detach_device(dev);
1606 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags); 1686 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1687
1688 if (amd_iommu_iotlb_sup && pci_ats_enabled(pdev))
1689 pci_disable_ats(pdev);
1607} 1690}
1608 1691
1609/* 1692/*
@@ -1692,7 +1775,7 @@ static int device_change_notifier(struct notifier_block *nb,
1692 goto out; 1775 goto out;
1693 } 1776 }
1694 1777
1695 iommu_flush_device(dev); 1778 device_flush_dte(dev);
1696 iommu_completion_wait(iommu); 1779 iommu_completion_wait(iommu);
1697 1780
1698out: 1781out:
@@ -1753,8 +1836,9 @@ static void update_device_table(struct protection_domain *domain)
1753 struct iommu_dev_data *dev_data; 1836 struct iommu_dev_data *dev_data;
1754 1837
1755 list_for_each_entry(dev_data, &domain->dev_list, list) { 1838 list_for_each_entry(dev_data, &domain->dev_list, list) {
1839 struct pci_dev *pdev = to_pci_dev(dev_data->dev);
1756 u16 devid = get_device_id(dev_data->dev); 1840 u16 devid = get_device_id(dev_data->dev);
1757 set_dte_entry(devid, domain); 1841 set_dte_entry(devid, domain, pci_ats_enabled(pdev));
1758 } 1842 }
1759} 1843}
1760 1844
@@ -1764,8 +1848,9 @@ static void update_domain(struct protection_domain *domain)
1764 return; 1848 return;
1765 1849
1766 update_device_table(domain); 1850 update_device_table(domain);
1767 iommu_flush_domain_devices(domain); 1851
1768 iommu_flush_tlb_pde(domain); 1852 domain_flush_devices(domain);
1853 domain_flush_tlb_pde(domain);
1769 1854
1770 domain->updated = false; 1855 domain->updated = false;
1771} 1856}
@@ -1924,10 +2009,10 @@ retry:
1924 ADD_STATS_COUNTER(alloced_io_mem, size); 2009 ADD_STATS_COUNTER(alloced_io_mem, size);
1925 2010
1926 if (unlikely(dma_dom->need_flush && !amd_iommu_unmap_flush)) { 2011 if (unlikely(dma_dom->need_flush && !amd_iommu_unmap_flush)) {
1927 iommu_flush_tlb(&dma_dom->domain); 2012 domain_flush_tlb(&dma_dom->domain);
1928 dma_dom->need_flush = false; 2013 dma_dom->need_flush = false;
1929 } else if (unlikely(amd_iommu_np_cache)) 2014 } else if (unlikely(amd_iommu_np_cache))
1930 iommu_flush_pages(&dma_dom->domain, address, size); 2015 domain_flush_pages(&dma_dom->domain, address, size);
1931 2016
1932out: 2017out:
1933 return address; 2018 return address;
@@ -1976,7 +2061,7 @@ static void __unmap_single(struct dma_ops_domain *dma_dom,
1976 dma_ops_free_addresses(dma_dom, dma_addr, pages); 2061 dma_ops_free_addresses(dma_dom, dma_addr, pages);
1977 2062
1978 if (amd_iommu_unmap_flush || dma_dom->need_flush) { 2063 if (amd_iommu_unmap_flush || dma_dom->need_flush) {
1979 iommu_flush_pages(&dma_dom->domain, flush_addr, size); 2064 domain_flush_pages(&dma_dom->domain, flush_addr, size);
1980 dma_dom->need_flush = false; 2065 dma_dom->need_flush = false;
1981 } 2066 }
1982} 2067}
@@ -2012,7 +2097,7 @@ static dma_addr_t map_page(struct device *dev, struct page *page,
2012 if (addr == DMA_ERROR_CODE) 2097 if (addr == DMA_ERROR_CODE)
2013 goto out; 2098 goto out;
2014 2099
2015 iommu_flush_complete(domain); 2100 domain_flush_complete(domain);
2016 2101
2017out: 2102out:
2018 spin_unlock_irqrestore(&domain->lock, flags); 2103 spin_unlock_irqrestore(&domain->lock, flags);
@@ -2039,7 +2124,7 @@ static void unmap_page(struct device *dev, dma_addr_t dma_addr, size_t size,
2039 2124
2040 __unmap_single(domain->priv, dma_addr, size, dir); 2125 __unmap_single(domain->priv, dma_addr, size, dir);
2041 2126
2042 iommu_flush_complete(domain); 2127 domain_flush_complete(domain);
2043 2128
2044 spin_unlock_irqrestore(&domain->lock, flags); 2129 spin_unlock_irqrestore(&domain->lock, flags);
2045} 2130}
@@ -2104,7 +2189,7 @@ static int map_sg(struct device *dev, struct scatterlist *sglist,
2104 goto unmap; 2189 goto unmap;
2105 } 2190 }
2106 2191
2107 iommu_flush_complete(domain); 2192 domain_flush_complete(domain);
2108 2193
2109out: 2194out:
2110 spin_unlock_irqrestore(&domain->lock, flags); 2195 spin_unlock_irqrestore(&domain->lock, flags);
@@ -2150,7 +2235,7 @@ static void unmap_sg(struct device *dev, struct scatterlist *sglist,
2150 s->dma_address = s->dma_length = 0; 2235 s->dma_address = s->dma_length = 0;
2151 } 2236 }
2152 2237
2153 iommu_flush_complete(domain); 2238 domain_flush_complete(domain);
2154 2239
2155 spin_unlock_irqrestore(&domain->lock, flags); 2240 spin_unlock_irqrestore(&domain->lock, flags);
2156} 2241}
@@ -2200,7 +2285,7 @@ static void *alloc_coherent(struct device *dev, size_t size,
2200 goto out_free; 2285 goto out_free;
2201 } 2286 }
2202 2287
2203 iommu_flush_complete(domain); 2288 domain_flush_complete(domain);
2204 2289
2205 spin_unlock_irqrestore(&domain->lock, flags); 2290 spin_unlock_irqrestore(&domain->lock, flags);
2206 2291
@@ -2232,7 +2317,7 @@ static void free_coherent(struct device *dev, size_t size,
2232 2317
2233 __unmap_single(domain->priv, dma_addr, size, DMA_BIDIRECTIONAL); 2318 __unmap_single(domain->priv, dma_addr, size, DMA_BIDIRECTIONAL);
2234 2319
2235 iommu_flush_complete(domain); 2320 domain_flush_complete(domain);
2236 2321
2237 spin_unlock_irqrestore(&domain->lock, flags); 2322 spin_unlock_irqrestore(&domain->lock, flags);
2238 2323
@@ -2476,7 +2561,7 @@ static void amd_iommu_detach_device(struct iommu_domain *dom,
2476 if (!iommu) 2561 if (!iommu)
2477 return; 2562 return;
2478 2563
2479 iommu_flush_device(dev); 2564 device_flush_dte(dev);
2480 iommu_completion_wait(iommu); 2565 iommu_completion_wait(iommu);
2481} 2566}
2482 2567
@@ -2542,7 +2627,7 @@ static int amd_iommu_unmap(struct iommu_domain *dom, unsigned long iova,
2542 unmap_size = iommu_unmap_page(domain, iova, page_size); 2627 unmap_size = iommu_unmap_page(domain, iova, page_size);
2543 mutex_unlock(&domain->api_lock); 2628 mutex_unlock(&domain->api_lock);
2544 2629
2545 iommu_flush_tlb_pde(domain); 2630 domain_flush_tlb_pde(domain);
2546 2631
2547 return get_order(unmap_size); 2632 return get_order(unmap_size);
2548} 2633}
diff --git a/arch/x86/kernel/amd_iommu_init.c b/arch/x86/kernel/amd_iommu_init.c
index 246d727b65b7..28b078133688 100644
--- a/arch/x86/kernel/amd_iommu_init.c
+++ b/arch/x86/kernel/amd_iommu_init.c
@@ -137,6 +137,7 @@ int amd_iommus_present;
137 137
138/* IOMMUs have a non-present cache? */ 138/* IOMMUs have a non-present cache? */
139bool amd_iommu_np_cache __read_mostly; 139bool amd_iommu_np_cache __read_mostly;
140bool amd_iommu_iotlb_sup __read_mostly = true;
140 141
141/* 142/*
142 * The ACPI table parsing functions set this variable on an error 143 * The ACPI table parsing functions set this variable on an error
@@ -180,6 +181,12 @@ static u32 dev_table_size; /* size of the device table */
180static u32 alias_table_size; /* size of the alias table */ 181static u32 alias_table_size; /* size of the alias table */
181static u32 rlookup_table_size; /* size if the rlookup table */ 182static u32 rlookup_table_size; /* size if the rlookup table */
182 183
184/*
185 * This function flushes all internal caches of
186 * the IOMMU used by this driver.
187 */
188extern void iommu_flush_all_caches(struct amd_iommu *iommu);
189
183static inline void update_last_devid(u16 devid) 190static inline void update_last_devid(u16 devid)
184{ 191{
185 if (devid > amd_iommu_last_bdf) 192 if (devid > amd_iommu_last_bdf)
@@ -293,9 +300,23 @@ static void iommu_feature_disable(struct amd_iommu *iommu, u8 bit)
293/* Function to enable the hardware */ 300/* Function to enable the hardware */
294static void iommu_enable(struct amd_iommu *iommu) 301static void iommu_enable(struct amd_iommu *iommu)
295{ 302{
296 printk(KERN_INFO "AMD-Vi: Enabling IOMMU at %s cap 0x%hx\n", 303 static const char * const feat_str[] = {
304 "PreF", "PPR", "X2APIC", "NX", "GT", "[5]",
305 "IA", "GA", "HE", "PC", NULL
306 };
307 int i;
308
309 printk(KERN_INFO "AMD-Vi: Enabling IOMMU at %s cap 0x%hx",
297 dev_name(&iommu->dev->dev), iommu->cap_ptr); 310 dev_name(&iommu->dev->dev), iommu->cap_ptr);
298 311
312 if (iommu->cap & (1 << IOMMU_CAP_EFR)) {
313 printk(KERN_CONT " extended features: ");
314 for (i = 0; feat_str[i]; ++i)
315 if (iommu_feature(iommu, (1ULL << i)))
316 printk(KERN_CONT " %s", feat_str[i]);
317 }
318 printk(KERN_CONT "\n");
319
299 iommu_feature_enable(iommu, CONTROL_IOMMU_EN); 320 iommu_feature_enable(iommu, CONTROL_IOMMU_EN);
300} 321}
301 322
@@ -651,7 +672,7 @@ static void __init set_device_exclusion_range(u16 devid, struct ivmd_header *m)
651static void __init init_iommu_from_pci(struct amd_iommu *iommu) 672static void __init init_iommu_from_pci(struct amd_iommu *iommu)
652{ 673{
653 int cap_ptr = iommu->cap_ptr; 674 int cap_ptr = iommu->cap_ptr;
654 u32 range, misc; 675 u32 range, misc, low, high;
655 int i, j; 676 int i, j;
656 677
657 pci_read_config_dword(iommu->dev, cap_ptr + MMIO_CAP_HDR_OFFSET, 678 pci_read_config_dword(iommu->dev, cap_ptr + MMIO_CAP_HDR_OFFSET,
@@ -667,6 +688,15 @@ static void __init init_iommu_from_pci(struct amd_iommu *iommu)
667 MMIO_GET_LD(range)); 688 MMIO_GET_LD(range));
668 iommu->evt_msi_num = MMIO_MSI_NUM(misc); 689 iommu->evt_msi_num = MMIO_MSI_NUM(misc);
669 690
691 if (!(iommu->cap & (1 << IOMMU_CAP_IOTLB)))
692 amd_iommu_iotlb_sup = false;
693
694 /* read extended feature bits */
695 low = readl(iommu->mmio_base + MMIO_EXT_FEATURES);
696 high = readl(iommu->mmio_base + MMIO_EXT_FEATURES + 4);
697
698 iommu->features = ((u64)high << 32) | low;
699
670 if (!is_rd890_iommu(iommu->dev)) 700 if (!is_rd890_iommu(iommu->dev))
671 return; 701 return;
672 702
@@ -1244,6 +1274,7 @@ static void enable_iommus(void)
1244 iommu_set_exclusion_range(iommu); 1274 iommu_set_exclusion_range(iommu);
1245 iommu_init_msi(iommu); 1275 iommu_init_msi(iommu);
1246 iommu_enable(iommu); 1276 iommu_enable(iommu);
1277 iommu_flush_all_caches(iommu);
1247 } 1278 }
1248} 1279}
1249 1280
@@ -1274,8 +1305,8 @@ static void amd_iommu_resume(void)
1274 * we have to flush after the IOMMUs are enabled because a 1305 * we have to flush after the IOMMUs are enabled because a
1275 * disabled IOMMU will never execute the commands we send 1306 * disabled IOMMU will never execute the commands we send
1276 */ 1307 */
1277 amd_iommu_flush_all_devices(); 1308 for_each_iommu(iommu)
1278 amd_iommu_flush_all_domains(); 1309 iommu_flush_all_caches(iommu);
1279} 1310}
1280 1311
1281static int amd_iommu_suspend(void) 1312static int amd_iommu_suspend(void)
diff --git a/drivers/pci/intel-iommu.c b/drivers/pci/intel-iommu.c
index d552d2c77844..6af6b628175b 100644
--- a/drivers/pci/intel-iommu.c
+++ b/drivers/pci/intel-iommu.c
@@ -39,6 +39,7 @@
39#include <linux/syscore_ops.h> 39#include <linux/syscore_ops.h>
40#include <linux/tboot.h> 40#include <linux/tboot.h>
41#include <linux/dmi.h> 41#include <linux/dmi.h>
42#include <linux/pci-ats.h>
42#include <asm/cacheflush.h> 43#include <asm/cacheflush.h>
43#include <asm/iommu.h> 44#include <asm/iommu.h>
44#include "pci.h" 45#include "pci.h"
diff --git a/drivers/pci/iov.c b/drivers/pci/iov.c
index 553d8ee55c1c..42fae4776515 100644
--- a/drivers/pci/iov.c
+++ b/drivers/pci/iov.c
@@ -13,6 +13,7 @@
13#include <linux/mutex.h> 13#include <linux/mutex.h>
14#include <linux/string.h> 14#include <linux/string.h>
15#include <linux/delay.h> 15#include <linux/delay.h>
16#include <linux/pci-ats.h>
16#include "pci.h" 17#include "pci.h"
17 18
18#define VIRTFN_ID_LEN 16 19#define VIRTFN_ID_LEN 16
diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h
index a6ec200fe5ee..4020025f854e 100644
--- a/drivers/pci/pci.h
+++ b/drivers/pci/pci.h
@@ -250,15 +250,6 @@ struct pci_sriov {
250 u8 __iomem *mstate; /* VF Migration State Array */ 250 u8 __iomem *mstate; /* VF Migration State Array */
251}; 251};
252 252
253/* Address Translation Service */
254struct pci_ats {
255 int pos; /* capability position */
256 int stu; /* Smallest Translation Unit */
257 int qdep; /* Invalidate Queue Depth */
258 int ref_cnt; /* Physical Function reference count */
259 unsigned int is_enabled:1; /* Enable bit is set */
260};
261
262#ifdef CONFIG_PCI_IOV 253#ifdef CONFIG_PCI_IOV
263extern int pci_iov_init(struct pci_dev *dev); 254extern int pci_iov_init(struct pci_dev *dev);
264extern void pci_iov_release(struct pci_dev *dev); 255extern void pci_iov_release(struct pci_dev *dev);
@@ -269,19 +260,6 @@ extern resource_size_t pci_sriov_resource_alignment(struct pci_dev *dev,
269extern void pci_restore_iov_state(struct pci_dev *dev); 260extern void pci_restore_iov_state(struct pci_dev *dev);
270extern int pci_iov_bus_range(struct pci_bus *bus); 261extern int pci_iov_bus_range(struct pci_bus *bus);
271 262
272extern int pci_enable_ats(struct pci_dev *dev, int ps);
273extern void pci_disable_ats(struct pci_dev *dev);
274extern int pci_ats_queue_depth(struct pci_dev *dev);
275/**
276 * pci_ats_enabled - query the ATS status
277 * @dev: the PCI device
278 *
279 * Returns 1 if ATS capability is enabled, or 0 if not.
280 */
281static inline int pci_ats_enabled(struct pci_dev *dev)
282{
283 return dev->ats && dev->ats->is_enabled;
284}
285#else 263#else
286static inline int pci_iov_init(struct pci_dev *dev) 264static inline int pci_iov_init(struct pci_dev *dev)
287{ 265{
@@ -304,21 +282,6 @@ static inline int pci_iov_bus_range(struct pci_bus *bus)
304 return 0; 282 return 0;
305} 283}
306 284
307static inline int pci_enable_ats(struct pci_dev *dev, int ps)
308{
309 return -ENODEV;
310}
311static inline void pci_disable_ats(struct pci_dev *dev)
312{
313}
314static inline int pci_ats_queue_depth(struct pci_dev *dev)
315{
316 return -ENODEV;
317}
318static inline int pci_ats_enabled(struct pci_dev *dev)
319{
320 return 0;
321}
322#endif /* CONFIG_PCI_IOV */ 285#endif /* CONFIG_PCI_IOV */
323 286
324static inline resource_size_t pci_resource_alignment(struct pci_dev *dev, 287static inline resource_size_t pci_resource_alignment(struct pci_dev *dev,
diff --git a/include/linux/pci-ats.h b/include/linux/pci-ats.h
new file mode 100644
index 000000000000..655824fa4c76
--- /dev/null
+++ b/include/linux/pci-ats.h
@@ -0,0 +1,52 @@
1#ifndef LINUX_PCI_ATS_H
2#define LINUX_PCI_ATS_H
3
4/* Address Translation Service */
5struct pci_ats {
6 int pos; /* capability position */
7 int stu; /* Smallest Translation Unit */
8 int qdep; /* Invalidate Queue Depth */
9 int ref_cnt; /* Physical Function reference count */
10 unsigned int is_enabled:1; /* Enable bit is set */
11};
12
13#ifdef CONFIG_PCI_IOV
14
15extern int pci_enable_ats(struct pci_dev *dev, int ps);
16extern void pci_disable_ats(struct pci_dev *dev);
17extern int pci_ats_queue_depth(struct pci_dev *dev);
18/**
19 * pci_ats_enabled - query the ATS status
20 * @dev: the PCI device
21 *
22 * Returns 1 if ATS capability is enabled, or 0 if not.
23 */
24static inline int pci_ats_enabled(struct pci_dev *dev)
25{
26 return dev->ats && dev->ats->is_enabled;
27}
28
29#else /* CONFIG_PCI_IOV */
30
31static inline int pci_enable_ats(struct pci_dev *dev, int ps)
32{
33 return -ENODEV;
34}
35
36static inline void pci_disable_ats(struct pci_dev *dev)
37{
38}
39
40static inline int pci_ats_queue_depth(struct pci_dev *dev)
41{
42 return -ENODEV;
43}
44
45static inline int pci_ats_enabled(struct pci_dev *dev)
46{
47 return 0;
48}
49
50#endif /* CONFIG_PCI_IOV */
51
52#endif /* LINUX_PCI_ATS_H*/
diff --git a/lib/dma-debug.c b/lib/dma-debug.c
index 4bfb0471f106..db07bfd9298e 100644
--- a/lib/dma-debug.c
+++ b/lib/dma-debug.c
@@ -649,7 +649,7 @@ out_err:
649 return -ENOMEM; 649 return -ENOMEM;
650} 650}
651 651
652static int device_dma_allocations(struct device *dev) 652static int device_dma_allocations(struct device *dev, struct dma_debug_entry **out_entry)
653{ 653{
654 struct dma_debug_entry *entry; 654 struct dma_debug_entry *entry;
655 unsigned long flags; 655 unsigned long flags;
@@ -660,8 +660,10 @@ static int device_dma_allocations(struct device *dev)
660 for (i = 0; i < HASH_SIZE; ++i) { 660 for (i = 0; i < HASH_SIZE; ++i) {
661 spin_lock(&dma_entry_hash[i].lock); 661 spin_lock(&dma_entry_hash[i].lock);
662 list_for_each_entry(entry, &dma_entry_hash[i].list, list) { 662 list_for_each_entry(entry, &dma_entry_hash[i].list, list) {
663 if (entry->dev == dev) 663 if (entry->dev == dev) {
664 count += 1; 664 count += 1;
665 *out_entry = entry;
666 }
665 } 667 }
666 spin_unlock(&dma_entry_hash[i].lock); 668 spin_unlock(&dma_entry_hash[i].lock);
667 } 669 }
@@ -674,6 +676,7 @@ static int device_dma_allocations(struct device *dev)
674static int dma_debug_device_change(struct notifier_block *nb, unsigned long action, void *data) 676static int dma_debug_device_change(struct notifier_block *nb, unsigned long action, void *data)
675{ 677{
676 struct device *dev = data; 678 struct device *dev = data;
679 struct dma_debug_entry *uninitialized_var(entry);
677 int count; 680 int count;
678 681
679 if (global_disable) 682 if (global_disable)
@@ -681,12 +684,17 @@ static int dma_debug_device_change(struct notifier_block *nb, unsigned long acti
681 684
682 switch (action) { 685 switch (action) {
683 case BUS_NOTIFY_UNBOUND_DRIVER: 686 case BUS_NOTIFY_UNBOUND_DRIVER:
684 count = device_dma_allocations(dev); 687 count = device_dma_allocations(dev, &entry);
685 if (count == 0) 688 if (count == 0)
686 break; 689 break;
687 err_printk(dev, NULL, "DMA-API: device driver has pending " 690 err_printk(dev, entry, "DMA-API: device driver has pending "
688 "DMA allocations while released from device " 691 "DMA allocations while released from device "
689 "[count=%d]\n", count); 692 "[count=%d]\n"
693 "One of leaked entries details: "
694 "[device address=0x%016llx] [size=%llu bytes] "
695 "[mapped with %s] [mapped as %s]\n",
696 count, entry->dev_addr, entry->size,
697 dir2name[entry->direction], type2name[entry->type]);
690 break; 698 break;
691 default: 699 default:
692 break; 700 break;