[PATCH v7 3/5] iommu/vt-d: Add debugfs support to show register contents

2018-02-02 Thread Sohil Mehta
From: Gayatri Kammela 

Debugfs extension to dump all the register contents for each IOMMU
device to the user space via debugfs.

Example:
root@OTC-KBLH-01:~# cat /sys/kernel/debug/intel_iommu/iommu_regset
DMAR: dmar0: Register Base Address fed9
NameOffset  Contents
VER 0x000x0010
CAP 0x080x01cc40660462
ECAP0x100x00f0101a
GCMD0x180x
GSTS0x1c0xc700
RTADDR  0x200x0004071d3800
CCMD0x280x0800
FSTS0x340x
FECTL   0x380x
FEDATA  0x3c0xfee010044021

Cc: Fenghua Yu 
Cc: Jacob Pan 
Cc: Ashok Raj 
Co-Developed-by: Sohil Mehta 
Signed-off-by: Sohil Mehta 
Signed-off-by: Gayatri Kammela 
---

v7: Use macro for register set definitions
Fix compiler warning for readq with 32bit architecture
Remove leading '\n'

v6: No change

v5: No change

v4: Fix checkpatch.pl warnings
Remove error reporting for debugfs_create_file function
Remove redundant IOMMU null check under for_each_active_iommu

v3: Use a macro for seq file operations 
Change the intel_iommu_regset file name to iommu_regset
Add information for MTRR registers

v2: Fix seq_printf formatting

 drivers/iommu/intel-iommu-debug.c | 84 +++
 include/linux/intel-iommu.h   |  2 +
 2 files changed, 86 insertions(+)

diff --git a/drivers/iommu/intel-iommu-debug.c 
b/drivers/iommu/intel-iommu-debug.c
index 8253503..38651ad 100644
--- a/drivers/iommu/intel-iommu-debug.c
+++ b/drivers/iommu/intel-iommu-debug.c
@@ -38,6 +38,49 @@ static const struct file_operations __name ## _fops =
\
.owner  = THIS_MODULE,  \
 }
 
+struct iommu_regset {
+   int offset;
+   const char *regs;
+};
+
+#define IOMMU_REGSET_ENTRY(_reg_)  \
+   { DMAR_##_reg_##_REG, __stringify(_reg_) }
+static const struct iommu_regset iommu_regs[] = {
+   IOMMU_REGSET_ENTRY(VER),
+   IOMMU_REGSET_ENTRY(CAP),
+   IOMMU_REGSET_ENTRY(ECAP),
+   IOMMU_REGSET_ENTRY(GCMD),
+   IOMMU_REGSET_ENTRY(GSTS),
+   IOMMU_REGSET_ENTRY(RTADDR),
+   IOMMU_REGSET_ENTRY(CCMD),
+   IOMMU_REGSET_ENTRY(FSTS),
+   IOMMU_REGSET_ENTRY(FECTL),
+   IOMMU_REGSET_ENTRY(FEDATA),
+   IOMMU_REGSET_ENTRY(FEADDR),
+   IOMMU_REGSET_ENTRY(FEUADDR),
+   IOMMU_REGSET_ENTRY(AFLOG),
+   IOMMU_REGSET_ENTRY(PMEN),
+   IOMMU_REGSET_ENTRY(PLMBASE),
+   IOMMU_REGSET_ENTRY(PLMLIMIT),
+   IOMMU_REGSET_ENTRY(PHMBASE),
+   IOMMU_REGSET_ENTRY(PHMLIMIT),
+   IOMMU_REGSET_ENTRY(IQH),
+   IOMMU_REGSET_ENTRY(IQT),
+   IOMMU_REGSET_ENTRY(IQA),
+   IOMMU_REGSET_ENTRY(ICS),
+   IOMMU_REGSET_ENTRY(IRTA),
+   IOMMU_REGSET_ENTRY(PQH),
+   IOMMU_REGSET_ENTRY(PQT),
+   IOMMU_REGSET_ENTRY(PQA),
+   IOMMU_REGSET_ENTRY(PRS),
+   IOMMU_REGSET_ENTRY(PECTL),
+   IOMMU_REGSET_ENTRY(PEDATA),
+   IOMMU_REGSET_ENTRY(PEADDR),
+   IOMMU_REGSET_ENTRY(PEUADDR),
+   IOMMU_REGSET_ENTRY(MTRRCAP),
+   IOMMU_REGSET_ENTRY(MTRRDEF)
+};
+
 static void ctx_tbl_entry_show(struct seq_file *m, struct intel_iommu *iommu,
   int bus, bool ext)
 {
@@ -116,6 +159,45 @@ static int dmar_translation_struct_show(struct seq_file 
*m, void *unused)
 }
 DEFINE_SHOW_ATTRIBUTE(dmar_translation_struct);
 
+static int iommu_regset_show(struct seq_file *m, void *unused)
+{
+   struct dmar_drhd_unit *drhd;
+   struct intel_iommu *iommu;
+   unsigned long long base;
+   int i, ret = 0;
+   u64 value;
+
+   rcu_read_lock();
+   for_each_active_iommu(iommu, drhd) {
+   if (!drhd->reg_base_addr) {
+   seq_puts(m, "IOMMU: Invalid base address\n");
+   ret = -EINVAL;
+   goto out;
+   }
+
+   base = drhd->reg_base_addr;
+   seq_printf(m, "DMAR: %s: Register Base Address %llx\n",
+  iommu->name, base);
+   seq_puts(m, "Name\t\t\tOffset\t\tContents\n");
+   /*
+* Publish the contents of the 64-bit hardware registers
+* by adding the offset to the pointer (virtual address).
+*/
+   for (i = 0 ; i < ARRAY_SIZE(iommu_regs); i++) {
+   value = dmar_readq(iommu->reg + iommu_regs[i].offset);
+   seq_printf(m, "%-8s\t\t0x%02x\t\t0x%016llx\n",
+  iommu_regs[i].regs, 

[PATCH v7 4/5] iommu/vt-d: Add debugfs support to show Pasid table contents

2018-02-02 Thread Sohil Mehta
From: Gayatri Kammela 

Debugfs extension to dump the internals such as pasid table entries for
each IOMMU to the userspace.

Example of such dump in Kabylake:

root@OTC-KBLH-01:~# cat
/sys/kernel/debug/intel_iommu/dmar_translation_struct
IOMMU dmar1: Extended Root Table Address:4071d3800
Extended Root Table Entries:
Bus 0 L: 4071d7001 H: 0
Lower Context Table Entries for Bus: 0
[entry] Device B:D.FLow High
[16]:00:02.04071d6005   102
Higher Context Table Entries for Bus: 0
[16]:00:02.00   0
Pasid Table Address: 746cb0af
Pasid Table Entries for domain 0:
[Entry] Contents
[0] 12c409801

Cc: Fenghua Yu 
Cc: Jacob Pan 
Cc: Ashok Raj 
Co-Developed-by: Sohil Mehta 
Signed-off-by: Sohil Mehta 
Signed-off-by: Gayatri Kammela 
---

v7: Improve code indentation and formatting

v6: No change

v5: No change

v4: Remove the unused function parameter
Fix checkpatch.pl warnings

v3: No change

v2: Fix seq_printf formatting

 drivers/iommu/intel-iommu-debug.c | 31 +++
 drivers/iommu/intel-svm.c |  8 
 include/linux/intel-svm.h |  8 
 3 files changed, 39 insertions(+), 8 deletions(-)

diff --git a/drivers/iommu/intel-iommu-debug.c 
b/drivers/iommu/intel-iommu-debug.c
index 38651ad..a9a99aa 100644
--- a/drivers/iommu/intel-iommu-debug.c
+++ b/drivers/iommu/intel-iommu-debug.c
@@ -81,6 +81,36 @@ static const struct iommu_regset iommu_regs[] = {
IOMMU_REGSET_ENTRY(MTRRDEF)
 };
 
+#ifdef CONFIG_INTEL_IOMMU_SVM
+static void pasid_tbl_entry_show(struct seq_file *m, struct intel_iommu *iommu)
+{
+   int pasid_size = 0, i;
+
+   if (!ecap_pasid(iommu->ecap))
+   return;
+
+   pasid_size = intel_iommu_get_pts(iommu);
+   seq_printf(m, "Pasid Table Address: %p\n", iommu->pasid_table);
+
+   if (!iommu->pasid_table)
+   return;
+
+   seq_printf(m, "Pasid Table Entries for domain %d:\n", iommu->segment);
+   seq_puts(m, "[Entry]\t\tContents\n");
+
+   /* Publish the pasid table entries here */
+   for (i = 0; i < pasid_size; i++) {
+   if (!iommu->pasid_table[i].val)
+   continue;
+
+   seq_printf(m, "[%d]\t\t%04llx\n", i, iommu->pasid_table[i].val);
+   }
+}
+#else /* CONFIG_INTEL_IOMMU_SVM */
+static inline void
+pasid_tbl_entry_show(struct seq_file *m, struct intel_iommu *iommu) {}
+#endif /* CONFIG_INTEL_IOMMU_SVM */
+
 static void ctx_tbl_entry_show(struct seq_file *m, struct intel_iommu *iommu,
   int bus, bool ext)
 {
@@ -116,6 +146,7 @@ static void ctx_tbl_entry_show(struct seq_file *m, struct 
intel_iommu *iommu,
   iommu->segment, bus, PCI_SLOT(ctx), PCI_FUNC(ctx),
   context[1].lo, context[1].hi);
}
+   pasid_tbl_entry_show(m, iommu);
 out:
spin_unlock_irqrestore(>lock, flags);
 }
diff --git a/drivers/iommu/intel-svm.c b/drivers/iommu/intel-svm.c
index ed1cf7c..c646724 100644
--- a/drivers/iommu/intel-svm.c
+++ b/drivers/iommu/intel-svm.c
@@ -28,14 +28,6 @@
 
 static irqreturn_t prq_event_thread(int irq, void *d);
 
-struct pasid_entry {
-   u64 val;
-};
-
-struct pasid_state_entry {
-   u64 val;
-};
-
 int intel_svm_alloc_pasid_tables(struct intel_iommu *iommu)
 {
struct page *pages;
diff --git a/include/linux/intel-svm.h b/include/linux/intel-svm.h
index 733eaf9..a8abad6 100644
--- a/include/linux/intel-svm.h
+++ b/include/linux/intel-svm.h
@@ -18,6 +18,14 @@
 
 struct device;
 
+struct pasid_entry {
+   u64 val;
+};
+
+struct pasid_state_entry {
+   u64 val;
+};
+
 struct svm_dev_ops {
void (*fault_cb)(struct device *dev, int pasid, u64 address,
 u32 private, int rwxp, int response);
-- 
2.7.4

___
iommu mailing list
iommu@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/iommu


[PATCH v7 2/5] iommu/vt-d: Enable debugfs support to show context internals

2018-02-02 Thread Sohil Mehta
From: Gayatri Kammela 

Add a new config option CONFIG_INTEL_IOMMU_DEBUG and export Intel IOMMU
internals states, such as root and context in debugfs to the userspace.

Example of such dump in Kabylake:

root@OTC-KBLH-01:~# cat
/sys/kernel/debug/intel_iommu/dmar_translation_struct
IOMMU dmar1: Extended Root Table Address:4071d3800
Extended Root Table Entries:
Bus 0 L: 4071d7001 H: 0
Lower Context Table Entries for Bus: 0
[entry] Device B:D.FLow High
[16]:00:02.04071d6005   102
Higher Context Table Entries for Bus: 0
[16]:00:02.00   0

IOMMU dmar0: Extended Root Table Address:4071d4800

IOMMU dmar2: Root Table Address:4071d5000
Root Table Entries:
Bus 0 L: 406d13001 H: 0
Context Table Entries for Bus: 0
[entry] Device B:D.FLow High
[160]   :00:14.0406d12001   102
[184]   :00:17.0405756001   302
[248]   :00:1f.0406d3b001   202
[251]   :00:1f.3405497001   402
[254]   :00:1f.640662e001   502
Root Table Entries:
Bus 1 L: 401e03001 H: 0
Context Table Entries for Bus: 1
[entry] Device B:D.FLow High
[0] :01:00.0401e04001   602

Cc: Fenghua Yu 
Cc: Ashok Raj 
Co-Developed-by: Sohil Mehta 
Signed-off-by: Jacob Pan 
Signed-off-by: Sohil Mehta 
Signed-off-by: Gayatri Kammela 
---

v7: Split patch 1/5 and 2/5 differently
Update commit message and copyright year
Fix typo in a comment
Simplify code

v6: Change the order of includes to an alphabetical order
Change seq_printf formatting

v5: Change to a SPDX license tag
Fix seq_printf formatting

v4: Remove the unused function parameter
Fix checkpatch.pl warnings
Remove error reporting for debugfs_create_file function
Fix unnecessary reprogramming of the context entries
Simplify and merge the show context and extended context patch into one
Remove redundant IOMMU null check under for_each_active_iommu

v3: Add a macro for seq file operations 
Change the intel_iommu_ctx file name to dmar_translation_struct

v2: No change

 drivers/iommu/Kconfig |   8 +++
 drivers/iommu/Makefile|   1 +
 drivers/iommu/intel-iommu-debug.c | 129 ++
 drivers/iommu/intel-iommu.c   |   1 +
 include/linux/intel-iommu.h   |   6 ++
 5 files changed, 145 insertions(+)
 create mode 100644 drivers/iommu/intel-iommu-debug.c

diff --git a/drivers/iommu/Kconfig b/drivers/iommu/Kconfig
index f3a2134..332648f 100644
--- a/drivers/iommu/Kconfig
+++ b/drivers/iommu/Kconfig
@@ -152,6 +152,14 @@ config INTEL_IOMMU
  and include PCI device scope covered by these DMA
  remapping devices.
 
+config INTEL_IOMMU_DEBUG
+   bool "Export Intel IOMMU internals in Debugfs"
+   depends on INTEL_IOMMU && DEBUG_FS
+   help
+ Debugfs support to export IOMMU context internals, register contents,
+ PASID internals and interrupt remapping. To access this information in
+ sysfs, say Y.
+
 config INTEL_IOMMU_SVM
bool "Support for Shared Virtual Memory with Intel IOMMU"
depends on INTEL_IOMMU && X86
diff --git a/drivers/iommu/Makefile b/drivers/iommu/Makefile
index 1fb6958..fdbaf46 100644
--- a/drivers/iommu/Makefile
+++ b/drivers/iommu/Makefile
@@ -15,6 +15,7 @@ obj-$(CONFIG_ARM_SMMU) += arm-smmu.o
 obj-$(CONFIG_ARM_SMMU_V3) += arm-smmu-v3.o
 obj-$(CONFIG_DMAR_TABLE) += dmar.o
 obj-$(CONFIG_INTEL_IOMMU) += intel-iommu.o
+obj-$(CONFIG_INTEL_IOMMU_DEBUG) += intel-iommu-debug.o
 obj-$(CONFIG_INTEL_IOMMU_SVM) += intel-svm.o
 obj-$(CONFIG_IPMMU_VMSA) += ipmmu-vmsa.o
 obj-$(CONFIG_IRQ_REMAP) += intel_irq_remapping.o irq_remapping.o
diff --git a/drivers/iommu/intel-iommu-debug.c 
b/drivers/iommu/intel-iommu-debug.c
new file mode 100644
index 000..8253503
--- /dev/null
+++ b/drivers/iommu/intel-iommu-debug.c
@@ -0,0 +1,129 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright © 2018 Intel Corporation.
+ *
+ * Authors: Gayatri Kammela 
+ *  Jacob Pan 
+ *  Sohil Mehta 
+ */
+
+#define pr_fmt(fmt) "INTEL_IOMMU: " fmt
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#include "irq_remapping.h"
+
+#define TOTAL_BUS_NR   256 /* full bus range */
+#define DEFINE_SHOW_ATTRIBUTE(__name)  \
+static int __name ## _open(struct inode *inode, struct file *file) \
+{  \
+   return single_open(file, __name ## _show, inode->i_private);\
+}  \
+static const struct file_operations 

[PATCH v7 5/5] iommu/vt-d: Add debugfs support for Interrupt remapping

2018-02-02 Thread Sohil Mehta
Debugfs extension for Intel IOMMU to dump Interrupt remapping table
entries for Interrupt remapping and Interrupt posting.

The file /sys/kernel/debug/intel_iommu/ir_translation_struct provides
detailed information, such as Index, Source Id, Destination Id, Vector
and the IRTE values for entries with the present bit set, in the format
shown.

Remapped Interrupt supported on IOMMU: dmar7
 IR table address:85e50
 Index  SrcID DstIDVct IRTE_highIRTE_low
 1  f0f8  0100 30  0004f0f8 013d
 7  f0f8  0400 22  0004f0f8 0422000d

Posted Interrupt supported on IOMMU: dmar5
 IR table address:85ec0
 Index  SrcID PDA_high PDA_low  Vct IRTE_high   IRTE_low
 4  4300  000f ff765980 41  000f00044300ff76598000418001
 5  4300  000f ff765980 51  000f00044300ff76598000518001

Cc: Jacob Pan 
Cc: Fenghua Yu 
Cc: Ashok Raj 
Co-Developed-by: Gayatri Kammela 
Signed-off-by: Gayatri Kammela 
Signed-off-by: Sohil Mehta 
---

v7: Print the IR table physical base address
Simplify IR table formatting

v6: Change a couple of seq_puts to seq_putc

v5: Fix seq_puts formatting and remove leading '\n's

v4: Remove the unused function parameter
Fix checkpatch.pl warnings
Remove error reporting for debugfs_create_file function
Remove redundant IOMMU null check under for_each_active_iommu

v3: Use a macro for seq file operations 
Change the intel_iommu_interrupt_remap file name to ir_translation_struct

v2: Handle the case when IR is not enabled. Fix seq_printf formatting

 drivers/iommu/intel-iommu-debug.c | 94 +++
 1 file changed, 94 insertions(+)

diff --git a/drivers/iommu/intel-iommu-debug.c 
b/drivers/iommu/intel-iommu-debug.c
index a9a99aa..b66a073 100644
--- a/drivers/iommu/intel-iommu-debug.c
+++ b/drivers/iommu/intel-iommu-debug.c
@@ -229,6 +229,96 @@ static int iommu_regset_show(struct seq_file *m, void 
*unused)
 }
 DEFINE_SHOW_ATTRIBUTE(iommu_regset);
 
+#ifdef CONFIG_IRQ_REMAP
+static void ir_tbl_remap_entry_show(struct seq_file *m,
+   struct intel_iommu *iommu)
+{
+   struct irte *ri_entry;
+   int idx;
+
+   seq_puts(m, " Index  SrcID DstIDVct IRTE_high\t\tIRTE_low\n");
+
+   for (idx = 0; idx < INTR_REMAP_TABLE_ENTRIES; idx++) {
+   ri_entry = >ir_table->base[idx];
+   if (!ri_entry->present || ri_entry->p_pst)
+   continue;
+
+   seq_printf(m, " %d\t%04x  %08x %02x  %016llx\t%016llx\n", idx,
+  ri_entry->sid, ri_entry->dest_id, ri_entry->vector,
+  ri_entry->high, ri_entry->low);
+   }
+}
+
+static void ir_tbl_posted_entry_show(struct seq_file *m,
+struct intel_iommu *iommu)
+{
+   struct irte *pi_entry;
+   int idx;
+
+   seq_puts(m, " Index  SrcID PDA_high PDA_low  Vct 
IRTE_high\t\tIRTE_low\n");
+
+   for (idx = 0; idx < INTR_REMAP_TABLE_ENTRIES; idx++) {
+   pi_entry = >ir_table->base[idx];
+   if (!pi_entry->present || !pi_entry->p_pst)
+   continue;
+
+   seq_printf(m, " %d\t%04x  %08x %08x %02x  %016llx\t%016llx\n",
+  idx, pi_entry->sid, pi_entry->pda_h,
+  pi_entry->pda_l << 6, pi_entry->vector,
+  pi_entry->high, pi_entry->low);
+   }
+}
+
+/*
+ * For active IOMMUs go through the Interrupt remapping
+ * table and print valid entries in a table format for
+ * Remapped and Posted Interrupts.
+ */
+static int ir_translation_struct_show(struct seq_file *m, void *unused)
+{
+   struct dmar_drhd_unit *drhd;
+   struct intel_iommu *iommu;
+   u64 irta;
+
+   rcu_read_lock();
+   for_each_active_iommu(iommu, drhd) {
+   if (!ecap_ir_support(iommu->ecap))
+   continue;
+
+   irta = dmar_readq(iommu->reg + DMAR_IRTA_REG) & VTD_PAGE_MASK;
+   seq_printf(m, "Remapped Interrupt supported on IOMMU: %s\n"
+ " IR table address:%llx\n", iommu->name, irta);
+
+   if (iommu->ir_table && irta)
+   ir_tbl_remap_entry_show(m, iommu);
+   else
+   seq_puts(m, "Interrupt Remapping is not enabled\n");
+   seq_putc(m, '\n');
+   }
+
+   seq_puts(m, "\n\n");
+
+   for_each_active_iommu(iommu, drhd) {
+   if (!cap_pi_support(iommu->cap))
+   continue;
+
+   irta = dmar_readq(iommu->reg + DMAR_IRTA_REG) & VTD_PAGE_MASK;
+   seq_printf(m, "Posted Interrupt supported on IOMMU: %s\n"
+ " IR table 

[PATCH v7 1/5] iommu/vt-d: Relocate struct/function declarations to its header files

2018-02-02 Thread Sohil Mehta
From: Gayatri Kammela 

To reuse the static functions and the struct declarations, move them to
corresponding header files and export the needed functions.

Cc: Sohil Mehta 
Cc: Fenghua Yu 
Cc: Ashok Raj 
Signed-off-by: Jacob Pan 
Signed-off-by: Gayatri Kammela 
---

v7: Split patch 1/5 and 2/5 differently
Update the commit message

v6: No change

v5: No change

v4: No change

v3: No change

v2: No change

 drivers/iommu/intel-iommu.c | 33 -
 include/linux/intel-iommu.h | 31 +++
 include/linux/intel-svm.h   |  2 +-
 3 files changed, 36 insertions(+), 30 deletions(-)

diff --git a/drivers/iommu/intel-iommu.c b/drivers/iommu/intel-iommu.c
index 4a2de34..f6241f6 100644
--- a/drivers/iommu/intel-iommu.c
+++ b/drivers/iommu/intel-iommu.c
@@ -183,16 +183,6 @@ static int rwbf_quirk;
 static int force_on = 0;
 int intel_iommu_tboot_noforce;
 
-/*
- * 0: Present
- * 1-11: Reserved
- * 12-63: Context Ptr (12 - (haw-1))
- * 64-127: Reserved
- */
-struct root_entry {
-   u64 lo;
-   u64 hi;
-};
 #define ROOT_ENTRY_NR (VTD_PAGE_SIZE/sizeof(struct root_entry))
 
 /*
@@ -218,21 +208,6 @@ static phys_addr_t root_entry_uctp(struct root_entry *re)
 
return re->hi & VTD_PAGE_MASK;
 }
-/*
- * low 64 bits:
- * 0: present
- * 1: fault processing disable
- * 2-3: translation type
- * 12-63: address space root
- * high 64 bits:
- * 0-2: address width
- * 3-6: aval
- * 8-23: domain id
- */
-struct context_entry {
-   u64 lo;
-   u64 hi;
-};
 
 static inline void context_clear_pasid_enable(struct context_entry *context)
 {
@@ -259,7 +234,7 @@ static inline bool __context_present(struct context_entry 
*context)
return (context->lo & 1);
 }
 
-static inline bool context_present(struct context_entry *context)
+bool context_present(struct context_entry *context)
 {
return context_pasid_enabled(context) ?
 __context_present(context) :
@@ -819,8 +794,8 @@ static void domain_update_iommu_cap(struct dmar_domain 
*domain)
domain->iommu_superpage = domain_update_iommu_superpage(NULL);
 }
 
-static inline struct context_entry *iommu_context_addr(struct intel_iommu 
*iommu,
-  u8 bus, u8 devfn, int 
alloc)
+struct context_entry *iommu_context_addr(struct intel_iommu *iommu, u8 bus,
+u8 devfn, int alloc)
 {
struct root_entry *root = >root_entry[bus];
struct context_entry *context;
@@ -5208,7 +5183,7 @@ static void intel_iommu_put_resv_regions(struct device 
*dev,
 
 #ifdef CONFIG_INTEL_IOMMU_SVM
 #define MAX_NR_PASID_BITS (20)
-static inline unsigned long intel_iommu_get_pts(struct intel_iommu *iommu)
+unsigned long intel_iommu_get_pts(struct intel_iommu *iommu)
 {
/*
 * Convert ecap_pss to extend context entry pts encoding, also
diff --git a/include/linux/intel-iommu.h b/include/linux/intel-iommu.h
index f3274d9..78ec85a 100644
--- a/include/linux/intel-iommu.h
+++ b/include/linux/intel-iommu.h
@@ -383,6 +383,33 @@ struct pasid_entry;
 struct pasid_state_entry;
 struct page_req_dsc;
 
+/*
+ * 0: Present
+ * 1-11: Reserved
+ * 12-63: Context Ptr (12 - (haw-1))
+ * 64-127: Reserved
+ */
+struct root_entry {
+   u64 lo;
+   u64 hi;
+};
+
+/*
+ * low 64 bits:
+ * 0: present
+ * 1: fault processing disable
+ * 2-3: translation type
+ * 12-63: address space root
+ * high 64 bits:
+ * 0-2: address width
+ * 3-6: aval
+ * 8-23: domain id
+ */
+struct context_entry {
+   u64 lo;
+   u64 hi;
+};
+
 struct intel_iommu {
void __iomem*reg; /* Pointer to hardware regs, virtual addr */
u64 reg_phys; /* physical address of hw register set */
@@ -488,8 +515,12 @@ struct intel_svm {
 
 extern int intel_iommu_enable_pasid(struct intel_iommu *iommu, struct 
intel_svm_dev *sdev);
 extern struct intel_iommu *intel_svm_device_to_iommu(struct device *dev);
+extern unsigned long intel_iommu_get_pts(struct intel_iommu *iommu);
 #endif
 
 extern const struct attribute_group *intel_iommu_groups[];
+extern bool context_present(struct context_entry *context);
+extern struct context_entry *iommu_context_addr(struct intel_iommu *iommu,
+   u8 bus, u8 devfn, int alloc);
 
 #endif
diff --git a/include/linux/intel-svm.h b/include/linux/intel-svm.h
index 99bc5b3..733eaf9 100644
--- a/include/linux/intel-svm.h
+++ b/include/linux/intel-svm.h
@@ -130,7 +130,7 @@ static inline int intel_svm_unbind_mm(struct device *dev, 
int pasid)
BUG();
 }
 
-static int intel_svm_is_pasid_valid(struct device *dev, int pasid)
+static inline int intel_svm_is_pasid_valid(struct device *dev, int pasid)
 {
return -EINVAL;
 }
-- 
2.7.4

___

[PATCH v7 0/5] Add Intel IOMMU debugfs support

2018-02-02 Thread Sohil Mehta
Hi All,

This series aims to add debugfs support for Intel IOMMU. It exposes IOMMU
registers, internal context and dumps individual table entries to help debug
Intel IOMMUs.

The first patch does the ground work for the following patches by reorganizing
some Intel IOMMU data structures. The following patches create a new Kconfig
option - INTEL_IOMMU_DEBUG and add debugfs support for IOMMU context internals,
register contents, PASID internals, and Interrupt remapping in that order. The
information can be accessed in sysfs at '/sys/kernel/debug/intel_iommu/'.


Regards,
Sohil
 
Changes since v6:
 - Split patch 1/5 and 2/5 differently
 - Simplify and improve code formatting
 - Use macro for register set definitions
 - Fix compiler warning for readq
 - Add Co-Developed-by tag to commit messages

Changes since v5:
 - Change the order of includes to an alphabetical order
 - Change seq_printf and seq_puts formatting

Changes since v4:
 - Change to a SPDX license tag
 - Fix seq_printf formatting and remove leading '\n's

Changes since v3:
 - Remove an unused function parameter from some of the functions
 - Fix checkpatch.pl warnings
 - Remove error reporting for debugfs_create_file functions
 - Fix unnecessary reprogramming of the context entries
 - Simplify and merge the show context and extended context patch into one
 - Remove redundant IOMMU null check under for_each_active_iommu
 - Update the commit title to be consistent

Changes since v2:
 - Added a macro for seq file operations based on recommendation by Andy 
   Shevchenko. The marco can be moved to seq_file.h at a future point
 - Changed the debugfs file names to more relevant ones
 - Added information for MTRR registers in the regset file

Changes since v1:
 - Fixed seq_printf formatting
 - Handled the case when Interrupt remapping is not enabled

Gayatri Kammela (4):
  iommu/vt-d: Relocate struct/function declarations to its header files
  iommu/vt-d: Enable debugfs support to show context internals
  iommu/vt-d: Add debugfs support to show register contents
  iommu/vt-d: Add debugfs support to show Pasid table contents

Sohil Mehta (1):
  iommu/vt-d: Add debugfs support for Interrupt remapping

 drivers/iommu/Kconfig |   8 +
 drivers/iommu/Makefile|   1 +
 drivers/iommu/intel-iommu-debug.c | 338 ++
 drivers/iommu/intel-iommu.c   |  34 +---
 drivers/iommu/intel-svm.c |   8 -
 include/linux/intel-iommu.h   |  39 +
 include/linux/intel-svm.h |  10 +-
 7 files changed, 400 insertions(+), 38 deletions(-)
 create mode 100644 drivers/iommu/intel-iommu-debug.c

-- 
2.7.4

___
iommu mailing list
iommu@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/iommu


Re: [PATCH 1/2] iommu: Fix iommu_unmap and iommu_unmap_fast return type

2018-02-02 Thread kbuild test robot
Hi Suravee,

I love your patch! Perhaps something to improve:

[auto build test WARNING on iommu/next]
[also build test WARNING on v4.15 next-20180202]
[if your patch is applied to the wrong git tree, please drop us a note to help 
improve the system]

url:
https://github.com/0day-ci/linux/commits/Suravee-Suthikulpanit/iommu-Fix-iommu_unmap-and-iommu_unmap_fast-return-type/20180203-015316
base:   https://git.kernel.org/pub/scm/linux/kernel/git/joro/iommu.git next
reproduce:
# apt-get install sparse
make ARCH=x86_64 allmodconfig
make C=1 CF=-D__CHECK_ENDIAN__


sparse warnings: (new ones prefixed by >>)

>> drivers/iommu/qcom_iommu.c:592:27: sparse: incorrect type in initializer 
>> (different signedness) @@ expected long ( )( ... ) @@ got unsigned long ( )( 
>> ... ) @@
   drivers/iommu/qcom_iommu.c:592:27: expected long ( )( ... )
   drivers/iommu/qcom_iommu.c:592:27: got unsigned long ( )( ... )
   drivers/iommu/qcom_iommu.c:592:12: error: initialization from incompatible 
pointer type
.unmap = qcom_iommu_unmap,
^~~~
   drivers/iommu/qcom_iommu.c:592:12: note: (near initialization for 
'qcom_iommu_ops.unmap')
   cc1: some warnings being treated as errors

vim +592 drivers/iommu/qcom_iommu.c

0ae349a0f3 Rob Clark2017-08-09  584  
0ae349a0f3 Rob Clark2017-08-09  585  static const struct iommu_ops 
qcom_iommu_ops = {
0ae349a0f3 Rob Clark2017-08-09  586 .capable= 
qcom_iommu_capable,
0ae349a0f3 Rob Clark2017-08-09  587 .domain_alloc   = 
qcom_iommu_domain_alloc,
0ae349a0f3 Rob Clark2017-08-09  588 .domain_free= 
qcom_iommu_domain_free,
0ae349a0f3 Rob Clark2017-08-09  589 .attach_dev = 
qcom_iommu_attach_dev,
0ae349a0f3 Rob Clark2017-08-09  590 .detach_dev = 
qcom_iommu_detach_dev,
0ae349a0f3 Rob Clark2017-08-09  591 .map= 
qcom_iommu_map,
0ae349a0f3 Rob Clark2017-08-09 @592 .unmap  = 
qcom_iommu_unmap,
0ae349a0f3 Rob Clark2017-08-09  593 .map_sg = 
default_iommu_map_sg,
4d689b6194 Robin Murphy 2017-09-28  594 .flush_iotlb_all = 
qcom_iommu_iotlb_sync,
4d689b6194 Robin Murphy 2017-09-28  595 .iotlb_sync = 
qcom_iommu_iotlb_sync,
0ae349a0f3 Rob Clark2017-08-09  596 .iova_to_phys   = 
qcom_iommu_iova_to_phys,
0ae349a0f3 Rob Clark2017-08-09  597 .add_device = 
qcom_iommu_add_device,
0ae349a0f3 Rob Clark2017-08-09  598 .remove_device  = 
qcom_iommu_remove_device,
0ae349a0f3 Rob Clark2017-08-09  599 .device_group   = 
generic_device_group,
0ae349a0f3 Rob Clark2017-08-09  600 .of_xlate   = 
qcom_iommu_of_xlate,
0ae349a0f3 Rob Clark2017-08-09  601 .pgsize_bitmap  = SZ_4K | 
SZ_64K | SZ_1M | SZ_16M,
0ae349a0f3 Rob Clark2017-08-09  602  };
0ae349a0f3 Rob Clark2017-08-09  603  

:: The code at line 592 was first introduced by commit
:: 0ae349a0f33fb040a2bc228fdc6d60111455feab iommu/qcom: Add qcom_iommu

:: TO: Rob Clark <robdcl...@gmail.com>
:: CC: Joerg Roedel <jroe...@suse.de>

---
0-DAY kernel test infrastructureOpen Source Technology Center
https://lists.01.org/pipermail/kbuild-all   Intel Corporation
___
iommu mailing list
iommu@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/iommu


Re: [PATCH 1/2] iommu: Fix iommu_unmap and iommu_unmap_fast return type

2018-02-02 Thread kbuild test robot
Hi Suravee,

I love your patch! Yet something to improve:

[auto build test ERROR on iommu/next]
[also build test ERROR on v4.15 next-20180202]
[if your patch is applied to the wrong git tree, please drop us a note to help 
improve the system]

url:
https://github.com/0day-ci/linux/commits/Suravee-Suthikulpanit/iommu-Fix-iommu_unmap-and-iommu_unmap_fast-return-type/20180203-015316
base:   https://git.kernel.org/pub/scm/linux/kernel/git/joro/iommu.git next
config: sparc64-allyesconfig (attached as .config)
compiler: sparc64-linux-gnu-gcc (Debian 7.2.0-11) 7.2.0
reproduce:
wget 
https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O 
~/bin/make.cross
chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
make.cross ARCH=sparc64 

All errors (new ones prefixed by >>):

>> drivers/iommu/qcom_iommu.c:592:12: error: initialization from incompatible 
>> pointer type [-Werror=incompatible-pointer-types]
 .unmap  = qcom_iommu_unmap,
   ^~~~
   drivers/iommu/qcom_iommu.c:592:12: note: (near initialization for 
'qcom_iommu_ops.unmap')
   cc1: some warnings being treated as errors

vim +592 drivers/iommu/qcom_iommu.c

0ae349a0f3 Rob Clark2017-08-09  584  
0ae349a0f3 Rob Clark2017-08-09  585  static const struct iommu_ops 
qcom_iommu_ops = {
0ae349a0f3 Rob Clark2017-08-09  586 .capable= 
qcom_iommu_capable,
0ae349a0f3 Rob Clark2017-08-09  587 .domain_alloc   = 
qcom_iommu_domain_alloc,
0ae349a0f3 Rob Clark2017-08-09  588 .domain_free= 
qcom_iommu_domain_free,
0ae349a0f3 Rob Clark2017-08-09  589 .attach_dev = 
qcom_iommu_attach_dev,
0ae349a0f3 Rob Clark2017-08-09  590 .detach_dev = 
qcom_iommu_detach_dev,
0ae349a0f3 Rob Clark2017-08-09  591 .map= 
qcom_iommu_map,
0ae349a0f3 Rob Clark2017-08-09 @592 .unmap  = 
qcom_iommu_unmap,
0ae349a0f3 Rob Clark2017-08-09  593 .map_sg = 
default_iommu_map_sg,
4d689b6194 Robin Murphy 2017-09-28  594 .flush_iotlb_all = 
qcom_iommu_iotlb_sync,
4d689b6194 Robin Murphy 2017-09-28  595 .iotlb_sync = 
qcom_iommu_iotlb_sync,
0ae349a0f3 Rob Clark2017-08-09  596 .iova_to_phys   = 
qcom_iommu_iova_to_phys,
0ae349a0f3 Rob Clark2017-08-09  597 .add_device = 
qcom_iommu_add_device,
0ae349a0f3 Rob Clark2017-08-09  598 .remove_device  = 
qcom_iommu_remove_device,
0ae349a0f3 Rob Clark2017-08-09  599 .device_group   = 
generic_device_group,
0ae349a0f3 Rob Clark2017-08-09  600 .of_xlate   = 
qcom_iommu_of_xlate,
0ae349a0f3 Rob Clark2017-08-09  601 .pgsize_bitmap  = SZ_4K | 
SZ_64K | SZ_1M | SZ_16M,
0ae349a0f3 Rob Clark2017-08-09  602  };
0ae349a0f3 Rob Clark2017-08-09  603  

:: The code at line 592 was first introduced by commit
:: 0ae349a0f33fb040a2bc228fdc6d60111455feab iommu/qcom: Add qcom_iommu

:: TO: Rob Clark <robdcl...@gmail.com>
:: CC: Joerg Roedel <jroe...@suse.de>

---
0-DAY kernel test infrastructureOpen Source Technology Center
https://lists.01.org/pipermail/kbuild-all   Intel Corporation


.config.gz
Description: application/gzip
___
iommu mailing list
iommu@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/iommu

Re: [PATCH] iommu/vt-d: add NUMA awareness to intel_alloc_coherent()

2018-02-02 Thread Eric Dumazet via iommu
On Fri, Feb 2, 2018 at 10:53 AM, Christoph Hellwig  wrote:
> I've got patches pending to replace all that code with
> dma_direct_alloc, which will do the right thing.  They were
> submitted for 4.16, and I will resend them after -rc1.

I see, thanks Christoph !
___
iommu mailing list
iommu@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/iommu


Re: [PATCH 1/2] iommu: Fix iommu_unmap and iommu_unmap_fast return type

2018-02-02 Thread kbuild test robot
Hi Suravee,

I love your patch! Yet something to improve:

[auto build test ERROR on iommu/next]
[also build test ERROR on v4.15 next-20180202]
[if your patch is applied to the wrong git tree, please drop us a note to help 
improve the system]

url:
https://github.com/0day-ci/linux/commits/Suravee-Suthikulpanit/iommu-Fix-iommu_unmap-and-iommu_unmap_fast-return-type/20180203-015316
base:   https://git.kernel.org/pub/scm/linux/kernel/git/joro/iommu.git next
config: s390-allyesconfig (attached as .config)
compiler: s390x-linux-gnu-gcc (Debian 7.2.0-11) 7.2.0
reproduce:
wget 
https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O 
~/bin/make.cross
chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
make.cross ARCH=s390 

All errors (new ones prefixed by >>):

>> drivers//iommu/s390-iommu.c:373:11: error: initialization from incompatible 
>> pointer type [-Werror=incompatible-pointer-types]
 .unmap = s390_iommu_unmap,
  ^~~~
   drivers//iommu/s390-iommu.c:373:11: note: (near initialization for 
's390_iommu_ops.unmap')
   cc1: some warnings being treated as errors

vim +373 drivers//iommu/s390-iommu.c

f42c22351 Joerg Roedel2017-04-27  365  
cceb84519 Arvind Yadav2017-08-28  366  static const struct iommu_ops 
s390_iommu_ops = {
8128f23c4 Gerald Schaefer 2015-08-27  367   .capable = s390_iommu_capable,
8128f23c4 Gerald Schaefer 2015-08-27  368   .domain_alloc = 
s390_domain_alloc,
8128f23c4 Gerald Schaefer 2015-08-27  369   .domain_free = s390_domain_free,
8128f23c4 Gerald Schaefer 2015-08-27  370   .attach_dev = 
s390_iommu_attach_device,
8128f23c4 Gerald Schaefer 2015-08-27  371   .detach_dev = 
s390_iommu_detach_device,
8128f23c4 Gerald Schaefer 2015-08-27  372   .map = s390_iommu_map,
8128f23c4 Gerald Schaefer 2015-08-27 @373   .unmap = s390_iommu_unmap,
8128f23c4 Gerald Schaefer 2015-08-27  374   .iova_to_phys = 
s390_iommu_iova_to_phys,
8128f23c4 Gerald Schaefer 2015-08-27  375   .add_device = 
s390_iommu_add_device,
8128f23c4 Gerald Schaefer 2015-08-27  376   .remove_device = 
s390_iommu_remove_device,
0929deca4 Joerg Roedel2017-06-15  377   .device_group = 
generic_device_group,
8128f23c4 Gerald Schaefer 2015-08-27  378   .pgsize_bitmap = 
S390_IOMMU_PGSIZES,
8128f23c4 Gerald Schaefer 2015-08-27  379  };
8128f23c4 Gerald Schaefer 2015-08-27  380  

:: The code at line 373 was first introduced by commit
:: 8128f23c436d0dd4f72412e1bf9256e424479dc3 iommu/s390: Add iommu api for 
s390 pci devices

:: TO: Gerald Schaefer <gerald.schae...@de.ibm.com>
:: CC: Joerg Roedel <jroe...@suse.de>

---
0-DAY kernel test infrastructureOpen Source Technology Center
https://lists.01.org/pipermail/kbuild-all   Intel Corporation


.config.gz
Description: application/gzip
___
iommu mailing list
iommu@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/iommu

Re: [PATCH] iommu/vt-d: add NUMA awareness to intel_alloc_coherent()

2018-02-02 Thread Christoph Hellwig
I've got patches pending to replace all that code with
dma_direct_alloc, which will do the right thing.  They were
submitted for 4.16, and I will resend them after -rc1.
___
iommu mailing list
iommu@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/iommu


Re: [PATCH 22/34] dma-mapping: add an arch_dma_supported hook

2018-02-02 Thread Randy Dunlap
On 01/12/2018 12:42 AM, Christoph Hellwig wrote:
> To implement the x86 forbid_dac and iommu_sac_force we want an arch hook
> so that it can apply the global options across all dma_map_ops
> implementations.
> 
> Signed-off-by: Christoph Hellwig 
> ---
>  arch/x86/include/asm/dma-mapping.h |  3 +++
>  arch/x86/kernel/pci-dma.c  | 19 ---
>  include/linux/dma-mapping.h| 11 +++
>  3 files changed, 26 insertions(+), 7 deletions(-)

> diff --git a/include/linux/dma-mapping.h b/include/linux/dma-mapping.h
> index 88bcb1a8211d..d67742dad904 100644
> --- a/include/linux/dma-mapping.h
> +++ b/include/linux/dma-mapping.h
> @@ -576,6 +576,14 @@ static inline int dma_mapping_error(struct device *dev, 
> dma_addr_t dma_addr)
>   return 0;
>  }
>  
> +/*
> + * This is a hack for the legacy x86 forbid_dac and iommu_sac_force. Please
> + * don't use this is new code.

 in new code.

> + */
> +#ifndef arch_dma_supported
> +#define arch_dma_supported(dev, mask)(1)
> +#endif


-- 
~Randy
___
iommu mailing list
iommu@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/iommu


Re: [PATCH v6 4/6] iommu/arm-smmu: Add the device_link between masters and smmu

2018-02-02 Thread Sricharan R
Hi Robin,

On 2/2/2018 5:01 PM, Robin Murphy wrote:
> On 02/02/18 05:40, Sricharan R wrote:
>> Hi Robin/Vivek,
>>
>> On 2/1/2018 2:23 PM, Vivek Gautam wrote:
>>> Hi,
>>>
>>>
>>> On 1/31/2018 6:39 PM, Robin Murphy wrote:
 On 19/01/18 11:43, Vivek Gautam wrote:
> From: Sricharan R 
>
> Finally add the device link between the master device and
> smmu, so that the smmu gets runtime enabled/disabled only when the
> master needs it. This is done from add_device callback which gets
> called once when the master is added to the smmu.

 Don't we need to balance this with a device_link_del() in .remove_device 
 (like exynos-iommu does)?
>>>
>>> Right. Will add device_link_del() call. Thanks for pointing out.
>>
>>   The reason for not adding device_link_del from .remove_device was, the 
>> core device_del
>>   which calls the .remove_device from notifier, calls device_links_purge 
>> before that.
>>   That does the same thing as device_link_del. So by the time .remove_device 
>> is called,
>>   device_links for that device is already cleaned up. Vivek, you may want to 
>> check once that
>>   calling device_link_del from .remove_device has no effect, just to confirm 
>> once more.
> 
> There is at least one path in which .remove_device is not called via the 
> notifier from device_del(), which is in the cleanup path of iommu_bus_init(). 
> AFAICS any links created by .add_device during that process would be left 
> dangling, because the device(s) would be live but otherwise disassociated 
> from the IOMMU afterwards.
> 
> From a maintenance perspective it's easier to have the call in its logical 
> place even if it does nothing 99% of the time; that way we shouldn't have to 
> keep an eye out for subtle changes in the power management code or driver 
> core that might invalidate the device_del() reasoning above, and the power 
> management guys shouldn't have to comprehend the internals of the IOMMU API 
> to make sense of the unbalanced call if they ever want to change their API.

 Ha, for a moment was thinking that with probe deferral add/remove_iommu_group 
in iommu_bus_init is dummy.
 But that may not be true for all Archs.
 Surely agree for the maintainability reason as well. Thanks.

Regards,
 Sricharan

-- 
"QUALCOMM INDIA, on behalf of Qualcomm Innovation Center, Inc. is a member of 
Code Aurora Forum, hosted by The Linux Foundation
___
iommu mailing list
iommu@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/iommu

Re: [PATCH v6 4/6] iommu/arm-smmu: Add the device_link between masters and smmu

2018-02-02 Thread Robin Murphy

On 02/02/18 05:40, Sricharan R wrote:

Hi Robin/Vivek,

On 2/1/2018 2:23 PM, Vivek Gautam wrote:

Hi,


On 1/31/2018 6:39 PM, Robin Murphy wrote:

On 19/01/18 11:43, Vivek Gautam wrote:

From: Sricharan R 

Finally add the device link between the master device and
smmu, so that the smmu gets runtime enabled/disabled only when the
master needs it. This is done from add_device callback which gets
called once when the master is added to the smmu.


Don't we need to balance this with a device_link_del() in .remove_device (like 
exynos-iommu does)?


Right. Will add device_link_del() call. Thanks for pointing out.


  The reason for not adding device_link_del from .remove_device was, the core 
device_del
  which calls the .remove_device from notifier, calls device_links_purge before 
that.
  That does the same thing as device_link_del. So by the time .remove_device is 
called,
  device_links for that device is already cleaned up. Vivek, you may want to 
check once that
  calling device_link_del from .remove_device has no effect, just to confirm 
once more.


There is at least one path in which .remove_device is not called via the 
notifier from device_del(), which is in the cleanup path of 
iommu_bus_init(). AFAICS any links created by .add_device during that 
process would be left dangling, because the device(s) would be live but 
otherwise disassociated from the IOMMU afterwards.


From a maintenance perspective it's easier to have the call in its 
logical place even if it does nothing 99% of the time; that way we 
shouldn't have to keep an eye out for subtle changes in the power 
management code or driver core that might invalidate the device_del() 
reasoning above, and the power management guys shouldn't have to 
comprehend the internals of the IOMMU API to make sense of the 
unbalanced call if they ever want to change their API.


Thanks,
Robin.
___
iommu mailing list
iommu@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/iommu