Re: [PATCH 2/3] HYPERV/IOMMU: Add Hyper-V stub IOMMU driver

2019-02-01 Thread Tianyu Lan
Hi Robin:
   Thanks for your review.

On Sat, Feb 2, 2019 at 1:00 AM Robin Murphy  wrote:
>
> On 31/01/2019 10:17, lantianyu1...@gmail.com wrote:
> > From: Lan Tianyu 
> >
> > On the bare metal, enabling X2APIC mode requires interrupt remapping
> > function which helps to deliver irq to cpu with 32-bit APIC ID.
> > Hyper-V doesn't provide interrupt remapping function so far and Hyper-V
> > MSI protocol already supports to deliver interrupt to the CPU whose
> > virtual processor index is more than 255. IO-APIC interrupt still has
> > 8-bit APIC ID limitation.
> >
> > This patch is to add Hyper-V stub IOMMU driver in order to enable
> > X2APIC mode successfully in Hyper-V Linux guest. The driver returns X2APIC
> > interrupt remapping capability when X2APIC mode is available. Otherwise,
> > it creates a Hyper-V irq domain to limit IO-APIC interrupts' affinity
> > and make sure cpus assigned with IO-APIC interrupt have 8-bit APIC ID.
>
> AFAICS this is a Hyper-V IOAPIC driver which neither implements nor even
> touches the IOMMU API, so having it in drivers/iommu rather than, say,
> drivers/irqchip seems a bit of a stretch to me :/
>
> Maybe this could be a good push to clean up and properly factor out the
> x86 IRQ remapping stuff, possibly to live somewhere closer to other IRQ
> layer code? I appreciate it's a bit muddied by the major x86 vendors
> both keeping their DMA virtualisation and IRQ virtualisation
> architectures together under one umbrella "IOMMU" spec, but IIRC the
> fact that intel-iommu supports building for !IOMMU_API already causes us
> a bit of grief.
>
> Of course, I'm coming from a particular not-entirely-objective viewpoint
> where DMA remapping (SMMU) code in drivers/iommu, IRQ remapping (ITS)
> code in drivers/irqchip, and the relevant ACPI table handling (IORT) in
> drivers/acpi is the norm, but even though that largely falls out of a
> less-tightly-coupled system architecture it does seem perfectly logical
> either way.

I think the different between arm and x86 about interrupt remapping is
that interrupt remapping
function is provided by IOMMU on x86 while it's provided by GIC or
other component on ARM.
As part of IOMMU on x86, the irq remapping code should be in the IOMMU
driver. if some thing
wrong, please correct me. Thanks.


>
> Robin.
>
> (who has admittedly been drinking the Cambridge water for many years now...)
>
> > Signed-off-by: Lan Tianyu 
> > ---
> >   drivers/iommu/Kconfig |   7 ++
> >   drivers/iommu/Makefile|   1 +
> >   drivers/iommu/hyperv-iommu.c  | 189 
> > ++
> >   drivers/iommu/irq_remapping.c |   3 +
> >   drivers/iommu/irq_remapping.h |   1 +
> >   5 files changed, 201 insertions(+)
> >   create mode 100644 drivers/iommu/hyperv-iommu.c
> >
> > diff --git a/drivers/iommu/Kconfig b/drivers/iommu/Kconfig
> > index 45d7021..5c397c0 100644
> > --- a/drivers/iommu/Kconfig
> > +++ b/drivers/iommu/Kconfig
> > @@ -437,4 +437,11 @@ config QCOM_IOMMU
> >   help
> > Support for IOMMU on certain Qualcomm SoCs.
> >
> > +config HYPERV_IOMMU
> > + bool "Hyper-V stub IOMMU support"
> > + depends on HYPERV
> > + help
> > + Hyper-V stub IOMMU driver provides capability to run
> > + Linux guest with X2APIC mode enabled.
> > +
> >   endif # IOMMU_SUPPORT
> > diff --git a/drivers/iommu/Makefile b/drivers/iommu/Makefile
> > index a158a68..8c71a15 100644
> > --- a/drivers/iommu/Makefile
> > +++ b/drivers/iommu/Makefile
> > @@ -32,3 +32,4 @@ obj-$(CONFIG_EXYNOS_IOMMU) += exynos-iommu.o
> >   obj-$(CONFIG_FSL_PAMU) += fsl_pamu.o fsl_pamu_domain.o
> >   obj-$(CONFIG_S390_IOMMU) += s390-iommu.o
> >   obj-$(CONFIG_QCOM_IOMMU) += qcom_iommu.o
> > +obj-$(CONFIG_HYPERV_IOMMU) += hyperv-iommu.o
> > diff --git a/drivers/iommu/hyperv-iommu.c b/drivers/iommu/hyperv-iommu.c
> > new file mode 100644
> > index 000..a64b747
> > --- /dev/null
> > +++ b/drivers/iommu/hyperv-iommu.c
> > @@ -0,0 +1,189 @@
> > +// SPDX-License-Identifier: GPL-2.0
> > +
> > +#define pr_fmt(fmt) "HYPERV-IR: " fmt
> > +
> > +#include 
> > +#include 
> > +#include 
> > +#include 
> > +#include 
> > +
> > +#include 
> > +#include 
> > +#include 
> > +#include 
> > +
> > +#include "irq_remapping.h"
> > +
> > +/*
> > + * According IO-APIC spec, IO APIC has a 24-entry Interrupt
> > + * Redirection Table.
> > + */
> > +#define IOAPIC_REMAPPING_ENTRY 24
> > +
> > +static cpumask_t ioapic_max_cpumask = { CPU_BITS_NONE };
> > +struct irq_domain *ioapic_ir_domain;
> > +
> > +static int hyperv_ir_set_affinity(struct irq_data *data,
> > + const struct cpumask *mask, bool force)
> > +{
> > + struct irq_data *parent = data->parent_data;
> > + struct irq_cfg *cfg = irqd_cfg(data);
> > + struct IO_APIC_route_entry *entry;
> > + cpumask_t cpumask;
> > + int ret;
> > +
> > + cpumask_andnot(&cpumask, mask, &ioapic_max_cpumask);
> > +
> > + /* Return error If new irq affinity is out of ioapic

Re: [PATCH 2/3] HYPERV/IOMMU: Add Hyper-V stub IOMMU driver

2019-02-01 Thread Tianyu Lan
Hi Sasha:
 Thanks for your review.

On Fri, Feb 1, 2019 at 10:51 PM Sasha Levin  wrote:
>
> Hi Tianyu,
>
> Few comments below.
>
> On Thu, Jan 31, 2019 at 06:17:32PM +0800, lantianyu1...@gmail.com wrote:
> >From: Lan Tianyu 
> >
> >On the bare metal, enabling X2APIC mode requires interrupt remapping
> >function which helps to deliver irq to cpu with 32-bit APIC ID.
> >Hyper-V doesn't provide interrupt remapping function so far and Hyper-V
> >MSI protocol already supports to deliver interrupt to the CPU whose
> >virtual processor index is more than 255. IO-APIC interrupt still has
> >8-bit APIC ID limitation.
> >
> >This patch is to add Hyper-V stub IOMMU driver in order to enable
> >X2APIC mode successfully in Hyper-V Linux guest. The driver returns X2APIC
> >interrupt remapping capability when X2APIC mode is available. Otherwise,
> >it creates a Hyper-V irq domain to limit IO-APIC interrupts' affinity
> >and make sure cpus assigned with IO-APIC interrupt have 8-bit APIC ID.
> >
> >Signed-off-by: Lan Tianyu 
> >---
> > drivers/iommu/Kconfig |   7 ++
> > drivers/iommu/Makefile|   1 +
> > drivers/iommu/hyperv-iommu.c  | 189 
> > ++
> > drivers/iommu/irq_remapping.c |   3 +
> > drivers/iommu/irq_remapping.h |   1 +
> > 5 files changed, 201 insertions(+)
> > create mode 100644 drivers/iommu/hyperv-iommu.c
> >
> >diff --git a/drivers/iommu/Kconfig b/drivers/iommu/Kconfig
> >index 45d7021..5c397c0 100644
> >--- a/drivers/iommu/Kconfig
> >+++ b/drivers/iommu/Kconfig
> >@@ -437,4 +437,11 @@ config QCOM_IOMMU
> >   help
> > Support for IOMMU on certain Qualcomm SoCs.
> >
> >+config HYPERV_IOMMU
> >+  bool "Hyper-V stub IOMMU support"
> >+  depends on HYPERV
>
> select IOMMU_API ?

Yes。 will  update.

>
> >+  help
> >+  Hyper-V stub IOMMU driver provides capability to run
> >+  Linux guest with X2APIC mode enabled.
> >+
> > endif # IOMMU_SUPPORT
> >diff --git a/drivers/iommu/Makefile b/drivers/iommu/Makefile
> >index a158a68..8c71a15 100644
> >--- a/drivers/iommu/Makefile
> >+++ b/drivers/iommu/Makefile
> >@@ -32,3 +32,4 @@ obj-$(CONFIG_EXYNOS_IOMMU) += exynos-iommu.o
> > obj-$(CONFIG_FSL_PAMU) += fsl_pamu.o fsl_pamu_domain.o
> > obj-$(CONFIG_S390_IOMMU) += s390-iommu.o
> > obj-$(CONFIG_QCOM_IOMMU) += qcom_iommu.o
> >+obj-$(CONFIG_HYPERV_IOMMU) += hyperv-iommu.o
> >diff --git a/drivers/iommu/hyperv-iommu.c b/drivers/iommu/hyperv-iommu.c
> >new file mode 100644
> >index 000..a64b747
> >--- /dev/null
> >+++ b/drivers/iommu/hyperv-iommu.c
> >@@ -0,0 +1,189 @@
> >+// SPDX-License-Identifier: GPL-2.0
> >+
> >+#define pr_fmt(fmt) "HYPERV-IR: " fmt
> >+
> >+#include 
> >+#include 
> >+#include 
> >+#include 
> >+#include 
> >+
> >+#include 
> >+#include 
> >+#include 
> >+#include 
> >+
> >+#include "irq_remapping.h"
> >+
> >+/*
> >+ * According IO-APIC spec, IO APIC has a 24-entry Interrupt
> >+ * Redirection Table.
>
> Can the spec be linked somewhere? In the commit message, or here?

Sure. Will update.

>
> >+ */
> >+#define IOAPIC_REMAPPING_ENTRY 24
> >+
> >+static cpumask_t ioapic_max_cpumask = { CPU_BITS_NONE };
> >+struct irq_domain *ioapic_ir_domain;
> >+
> >+static int hyperv_ir_set_affinity(struct irq_data *data,
> >+  const struct cpumask *mask, bool force)
> >+{
> >+  struct irq_data *parent = data->parent_data;
> >+  struct irq_cfg *cfg = irqd_cfg(data);
> >+  struct IO_APIC_route_entry *entry;
> >+  cpumask_t cpumask;
> >+  int ret;
> >+
> >+  cpumask_andnot(&cpumask, mask, &ioapic_max_cpumask);
> >+
> >+  /* Return error If new irq affinity is out of ioapic_max_cpumask. */
> >+  if (!cpumask_empty(&cpumask))
> >+  return -EINVAL;
> >+
> >+  ret = parent->chip->irq_set_affinity(parent, mask, force);
> >+  if (ret < 0 || ret == IRQ_SET_MASK_OK_DONE)
> >+  return ret;
> >+
> >+  entry = data->chip_data;
> >+  entry->dest = cfg->dest_apicid;
> >+  entry->vector = cfg->vector;
> >+  send_cleanup_vector(cfg);
> >+
> >+  return 0;
> >+}
> >+
> >+static struct irq_chip hyperv_ir_chip = {
> >+  .name   = "HYPERV-IR",
> >+  .irq_ack= apic_ack_irq,
> >+  .irq_set_affinity   = hyperv_ir_set_affinity,
> >+};
> >+
> >+static int hyperv_irq_remapping_alloc(struct irq_domain *domain,
> >+   unsigned int virq, unsigned int nr_irqs,
> >+   void *arg)
> >+{
> >+  struct irq_alloc_info *info = arg;
> >+  struct IO_APIC_route_entry *entry;
>
> What's the role of this variable? We set it, once, later on in the
> function but that's all?
>

This one should be removed.

> >+  struct irq_data *irq_data;
> >+  struct irq_desc *desc;
> >+  struct irq_cfg *cfg;
> >+  int ret = 0;
> >+
> >+  if (!info || info->type != X86_IRQ_ALLOC_TYPE_IOAPIC || nr_irqs > 1)
> >+  return

Re: [PATCH 2/3] HYPERV/IOMMU: Add Hyper-V stub IOMMU driver

2019-02-01 Thread Tianyu Lan
Hi Joerg:
 Thanks for your review.

On Sat, Feb 2, 2019 at 12:34 AM Joerg Roedel  wrote:
>
> Hi,
>
> On Thu, Jan 31, 2019 at 06:17:32PM +0800, lantianyu1...@gmail.com wrote:
> > +config HYPERV_IOMMU
> > + bool "Hyper-V stub IOMMU support"
>
> This is not a real IOMMU driver, it only implements IRQ remapping
> capabilities. Please change the name to reflect that, e.g. to
> "Hyper-V IRQ Remapping Support" or something like that.

Yes, that makes sense. Will update.

>
> > +static int __init hyperv_prepare_irq_remapping(void)
> > +{
> > + struct fwnode_handle *fn;
> > + u32 apic_id;
> > + int i;
> > +
> > + if (x86_hyper_type != X86_HYPER_MS_HYPERV ||
> > + !x2apic_supported())
> > + return -ENODEV;
> > +
> > + fn = irq_domain_alloc_named_id_fwnode("HYPERV-IR", 0);
> > + if (!fn)
> > + return -EFAULT;
>
> Why does this return -EFAULT? I guess there is no fault happening in
> irq_domain_alloc_named_id_fwnode()...

Yes, “-ENOMEM” should be more accurate.

-- 
Best regards
Tianyu Lan


Re: [PATCH 2/3] HYPERV/IOMMU: Add Hyper-V stub IOMMU driver

2019-02-01 Thread Robin Murphy

On 31/01/2019 10:17, lantianyu1...@gmail.com wrote:

From: Lan Tianyu 

On the bare metal, enabling X2APIC mode requires interrupt remapping
function which helps to deliver irq to cpu with 32-bit APIC ID.
Hyper-V doesn't provide interrupt remapping function so far and Hyper-V
MSI protocol already supports to deliver interrupt to the CPU whose
virtual processor index is more than 255. IO-APIC interrupt still has
8-bit APIC ID limitation.

This patch is to add Hyper-V stub IOMMU driver in order to enable
X2APIC mode successfully in Hyper-V Linux guest. The driver returns X2APIC
interrupt remapping capability when X2APIC mode is available. Otherwise,
it creates a Hyper-V irq domain to limit IO-APIC interrupts' affinity
and make sure cpus assigned with IO-APIC interrupt have 8-bit APIC ID.


AFAICS this is a Hyper-V IOAPIC driver which neither implements nor even 
touches the IOMMU API, so having it in drivers/iommu rather than, say, 
drivers/irqchip seems a bit of a stretch to me :/


Maybe this could be a good push to clean up and properly factor out the 
x86 IRQ remapping stuff, possibly to live somewhere closer to other IRQ 
layer code? I appreciate it's a bit muddied by the major x86 vendors 
both keeping their DMA virtualisation and IRQ virtualisation 
architectures together under one umbrella "IOMMU" spec, but IIRC the 
fact that intel-iommu supports building for !IOMMU_API already causes us 
a bit of grief.


Of course, I'm coming from a particular not-entirely-objective viewpoint 
where DMA remapping (SMMU) code in drivers/iommu, IRQ remapping (ITS) 
code in drivers/irqchip, and the relevant ACPI table handling (IORT) in 
drivers/acpi is the norm, but even though that largely falls out of a 
less-tightly-coupled system architecture it does seem perfectly logical 
either way.


Robin.

(who has admittedly been drinking the Cambridge water for many years now...)


Signed-off-by: Lan Tianyu 
---
  drivers/iommu/Kconfig |   7 ++
  drivers/iommu/Makefile|   1 +
  drivers/iommu/hyperv-iommu.c  | 189 ++
  drivers/iommu/irq_remapping.c |   3 +
  drivers/iommu/irq_remapping.h |   1 +
  5 files changed, 201 insertions(+)
  create mode 100644 drivers/iommu/hyperv-iommu.c

diff --git a/drivers/iommu/Kconfig b/drivers/iommu/Kconfig
index 45d7021..5c397c0 100644
--- a/drivers/iommu/Kconfig
+++ b/drivers/iommu/Kconfig
@@ -437,4 +437,11 @@ config QCOM_IOMMU
help
  Support for IOMMU on certain Qualcomm SoCs.
  
+config HYPERV_IOMMU

+   bool "Hyper-V stub IOMMU support"
+   depends on HYPERV
+   help
+   Hyper-V stub IOMMU driver provides capability to run
+   Linux guest with X2APIC mode enabled.
+
  endif # IOMMU_SUPPORT
diff --git a/drivers/iommu/Makefile b/drivers/iommu/Makefile
index a158a68..8c71a15 100644
--- a/drivers/iommu/Makefile
+++ b/drivers/iommu/Makefile
@@ -32,3 +32,4 @@ obj-$(CONFIG_EXYNOS_IOMMU) += exynos-iommu.o
  obj-$(CONFIG_FSL_PAMU) += fsl_pamu.o fsl_pamu_domain.o
  obj-$(CONFIG_S390_IOMMU) += s390-iommu.o
  obj-$(CONFIG_QCOM_IOMMU) += qcom_iommu.o
+obj-$(CONFIG_HYPERV_IOMMU) += hyperv-iommu.o
diff --git a/drivers/iommu/hyperv-iommu.c b/drivers/iommu/hyperv-iommu.c
new file mode 100644
index 000..a64b747
--- /dev/null
+++ b/drivers/iommu/hyperv-iommu.c
@@ -0,0 +1,189 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#define pr_fmt(fmt) "HYPERV-IR: " fmt
+
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#include 
+#include 
+#include 
+#include 
+
+#include "irq_remapping.h"
+
+/*
+ * According IO-APIC spec, IO APIC has a 24-entry Interrupt
+ * Redirection Table.
+ */
+#define IOAPIC_REMAPPING_ENTRY 24
+
+static cpumask_t ioapic_max_cpumask = { CPU_BITS_NONE };
+struct irq_domain *ioapic_ir_domain;
+
+static int hyperv_ir_set_affinity(struct irq_data *data,
+   const struct cpumask *mask, bool force)
+{
+   struct irq_data *parent = data->parent_data;
+   struct irq_cfg *cfg = irqd_cfg(data);
+   struct IO_APIC_route_entry *entry;
+   cpumask_t cpumask;
+   int ret;
+
+   cpumask_andnot(&cpumask, mask, &ioapic_max_cpumask);
+
+   /* Return error If new irq affinity is out of ioapic_max_cpumask. */
+   if (!cpumask_empty(&cpumask))
+   return -EINVAL;
+
+   ret = parent->chip->irq_set_affinity(parent, mask, force);
+   if (ret < 0 || ret == IRQ_SET_MASK_OK_DONE)
+   return ret;
+
+   entry = data->chip_data;
+   entry->dest = cfg->dest_apicid;
+   entry->vector = cfg->vector;
+   send_cleanup_vector(cfg);
+
+   return 0;
+}
+
+static struct irq_chip hyperv_ir_chip = {
+   .name   = "HYPERV-IR",
+   .irq_ack= apic_ack_irq,
+   .irq_set_affinity   = hyperv_ir_set_affinity,
+};
+
+static int hyperv_irq_remapping_alloc(struct irq_domain *domain,
+unsigned int virq, unsigned int nr_irqs,
+   

Re: [PATCH 2/3] HYPERV/IOMMU: Add Hyper-V stub IOMMU driver

2019-02-01 Thread Joerg Roedel
Hi,

On Thu, Jan 31, 2019 at 06:17:32PM +0800, lantianyu1...@gmail.com wrote:
> +config HYPERV_IOMMU
> + bool "Hyper-V stub IOMMU support"

This is not a real IOMMU driver, it only implements IRQ remapping
capabilities. Please change the name to reflect that, e.g. to
"Hyper-V IRQ Remapping Support" or something like that.

> +static int __init hyperv_prepare_irq_remapping(void)
> +{
> + struct fwnode_handle *fn;
> + u32 apic_id;
> + int i;
> +
> + if (x86_hyper_type != X86_HYPER_MS_HYPERV ||
> + !x2apic_supported())
> + return -ENODEV;
> +
> + fn = irq_domain_alloc_named_id_fwnode("HYPERV-IR", 0);
> + if (!fn)
> + return -EFAULT;

Why does this return -EFAULT? I guess there is no fault happening in
irq_domain_alloc_named_id_fwnode()...


Joerg


Re: [PATCH 2/3] HYPERV/IOMMU: Add Hyper-V stub IOMMU driver

2019-01-31 Thread Tianyu Lan
Hi Vitaly:
Thanks for your review.

On Thu, Jan 31, 2019 at 10:04 PM Vitaly Kuznetsov  wrote:
>
> lantianyu1...@gmail.com writes:
>
> > From: Lan Tianyu 
> >
> > On the bare metal, enabling X2APIC mode requires interrupt remapping
> > function which helps to deliver irq to cpu with 32-bit APIC ID.
> > Hyper-V doesn't provide interrupt remapping function so far and Hyper-V
> > MSI protocol already supports to deliver interrupt to the CPU whose
> > virtual processor index is more than 255. IO-APIC interrupt still has
> > 8-bit APIC ID limitation.
> >
> > This patch is to add Hyper-V stub IOMMU driver in order to enable
> > X2APIC mode successfully in Hyper-V Linux guest. The driver returns X2APIC
> > interrupt remapping capability when X2APIC mode is available. Otherwise,
> > it creates a Hyper-V irq domain to limit IO-APIC interrupts' affinity
> > and make sure cpus assigned with IO-APIC interrupt have 8-bit APIC ID.
> >
> > Signed-off-by: Lan Tianyu 
> > ---
> >  drivers/iommu/Kconfig |   7 ++
> >  drivers/iommu/Makefile|   1 +
> >  drivers/iommu/hyperv-iommu.c  | 189 
> > ++
> >  drivers/iommu/irq_remapping.c |   3 +
> >  drivers/iommu/irq_remapping.h |   1 +
> >  5 files changed, 201 insertions(+)
> >  create mode 100644 drivers/iommu/hyperv-iommu.c
> >
> > diff --git a/drivers/iommu/Kconfig b/drivers/iommu/Kconfig
> > index 45d7021..5c397c0 100644
> > --- a/drivers/iommu/Kconfig
> > +++ b/drivers/iommu/Kconfig
> > @@ -437,4 +437,11 @@ config QCOM_IOMMU
> >   help
> > Support for IOMMU on certain Qualcomm SoCs.
> >
> > +config HYPERV_IOMMU
> > + bool "Hyper-V stub IOMMU support"
> > + depends on HYPERV
> > + help
> > + Hyper-V stub IOMMU driver provides capability to run
> > + Linux guest with X2APIC mode enabled.
> > +
> >  endif # IOMMU_SUPPORT
> > diff --git a/drivers/iommu/Makefile b/drivers/iommu/Makefile
> > index a158a68..8c71a15 100644
> > --- a/drivers/iommu/Makefile
> > +++ b/drivers/iommu/Makefile
> > @@ -32,3 +32,4 @@ obj-$(CONFIG_EXYNOS_IOMMU) += exynos-iommu.o
> >  obj-$(CONFIG_FSL_PAMU) += fsl_pamu.o fsl_pamu_domain.o
> >  obj-$(CONFIG_S390_IOMMU) += s390-iommu.o
> >  obj-$(CONFIG_QCOM_IOMMU) += qcom_iommu.o
> > +obj-$(CONFIG_HYPERV_IOMMU) += hyperv-iommu.o
> > diff --git a/drivers/iommu/hyperv-iommu.c b/drivers/iommu/hyperv-iommu.c
> > new file mode 100644
> > index 000..a64b747
> > --- /dev/null
> > +++ b/drivers/iommu/hyperv-iommu.c
> > @@ -0,0 +1,189 @@
> > +// SPDX-License-Identifier: GPL-2.0
> > +
> > +#define pr_fmt(fmt) "HYPERV-IR: " fmt
> > +
> > +#include 
> > +#include 
> > +#include 
> > +#include 
> > +#include 
> > +
> > +#include 
> > +#include 
> > +#include 
> > +#include 
> > +
> > +#include "irq_remapping.h"
> > +
> > +/*
> > + * According IO-APIC spec, IO APIC has a 24-entry Interrupt
> > + * Redirection Table.
> > + */
> > +#define IOAPIC_REMAPPING_ENTRY 24
>
> KVM already defines KVM_IOAPIC_NUM_PINS - is this the same thing?

It's the same purpose  IOMMU driver is out of KVM scope and so define a new one.
Otherwise, this maybe changed in the future when add interrupt
remapping function.

>
> > +
> > +static cpumask_t ioapic_max_cpumask = { CPU_BITS_NONE };
> > +struct irq_domain *ioapic_ir_domain;
> > +
> > +static int hyperv_ir_set_affinity(struct irq_data *data,
> > + const struct cpumask *mask, bool force)
> > +{
> > + struct irq_data *parent = data->parent_data;
> > + struct irq_cfg *cfg = irqd_cfg(data);
> > + struct IO_APIC_route_entry *entry;
> > + cpumask_t cpumask;
> > + int ret;
> > +
> > + cpumask_andnot(&cpumask, mask, &ioapic_max_cpumask);
> > +
> > + /* Return error If new irq affinity is out of ioapic_max_cpumask. */
> > + if (!cpumask_empty(&cpumask))
> > + return -EINVAL;
> > +
> > + ret = parent->chip->irq_set_affinity(parent, mask, force);
> > + if (ret < 0 || ret == IRQ_SET_MASK_OK_DONE)
> > + return ret;
> > +
> > + entry = data->chip_data;
> > + entry->dest = cfg->dest_apicid;
> > + entry->vector = cfg->vector;
> > + send_cleanup_vector(cfg);
> > +
> > + return 0;
> > +}
> > +
> > +static struct irq_chip hyperv_ir_chip = {
> > + .name   = "HYPERV-IR",
> > + .irq_ack= apic_ack_irq,
> > + .irq_set_affinity   = hyperv_ir_set_affinity,
> > +};
> > +
> > +static int hyperv_irq_remapping_alloc(struct irq_domain *domain,
> > +  unsigned int virq, unsigned int nr_irqs,
> > +  void *arg)
> > +{
> > + struct irq_alloc_info *info = arg;
> > + struct IO_APIC_route_entry *entry;
> > + struct irq_data *irq_data;
> > + struct irq_desc *desc;
> > + struct irq_cfg *cfg;
> > + int ret = 0;
> > +
> > + if (!info || info->type != X86_IRQ_ALLOC_TYPE_IOAPIC || nr_irqs > 1)
> > + return -EINVAL;
> > +
> > + 

Re: [PATCH 2/3] HYPERV/IOMMU: Add Hyper-V stub IOMMU driver

2019-01-31 Thread Vitaly Kuznetsov
lantianyu1...@gmail.com writes:

> From: Lan Tianyu 
>
> On the bare metal, enabling X2APIC mode requires interrupt remapping
> function which helps to deliver irq to cpu with 32-bit APIC ID.
> Hyper-V doesn't provide interrupt remapping function so far and Hyper-V
> MSI protocol already supports to deliver interrupt to the CPU whose
> virtual processor index is more than 255. IO-APIC interrupt still has
> 8-bit APIC ID limitation.
>
> This patch is to add Hyper-V stub IOMMU driver in order to enable
> X2APIC mode successfully in Hyper-V Linux guest. The driver returns X2APIC
> interrupt remapping capability when X2APIC mode is available. Otherwise,
> it creates a Hyper-V irq domain to limit IO-APIC interrupts' affinity
> and make sure cpus assigned with IO-APIC interrupt have 8-bit APIC ID.
>
> Signed-off-by: Lan Tianyu 
> ---
>  drivers/iommu/Kconfig |   7 ++
>  drivers/iommu/Makefile|   1 +
>  drivers/iommu/hyperv-iommu.c  | 189 
> ++
>  drivers/iommu/irq_remapping.c |   3 +
>  drivers/iommu/irq_remapping.h |   1 +
>  5 files changed, 201 insertions(+)
>  create mode 100644 drivers/iommu/hyperv-iommu.c
>
> diff --git a/drivers/iommu/Kconfig b/drivers/iommu/Kconfig
> index 45d7021..5c397c0 100644
> --- a/drivers/iommu/Kconfig
> +++ b/drivers/iommu/Kconfig
> @@ -437,4 +437,11 @@ config QCOM_IOMMU
>   help
> Support for IOMMU on certain Qualcomm SoCs.
>  
> +config HYPERV_IOMMU
> + bool "Hyper-V stub IOMMU support"
> + depends on HYPERV
> + help
> + Hyper-V stub IOMMU driver provides capability to run
> + Linux guest with X2APIC mode enabled.
> +
>  endif # IOMMU_SUPPORT
> diff --git a/drivers/iommu/Makefile b/drivers/iommu/Makefile
> index a158a68..8c71a15 100644
> --- a/drivers/iommu/Makefile
> +++ b/drivers/iommu/Makefile
> @@ -32,3 +32,4 @@ obj-$(CONFIG_EXYNOS_IOMMU) += exynos-iommu.o
>  obj-$(CONFIG_FSL_PAMU) += fsl_pamu.o fsl_pamu_domain.o
>  obj-$(CONFIG_S390_IOMMU) += s390-iommu.o
>  obj-$(CONFIG_QCOM_IOMMU) += qcom_iommu.o
> +obj-$(CONFIG_HYPERV_IOMMU) += hyperv-iommu.o
> diff --git a/drivers/iommu/hyperv-iommu.c b/drivers/iommu/hyperv-iommu.c
> new file mode 100644
> index 000..a64b747
> --- /dev/null
> +++ b/drivers/iommu/hyperv-iommu.c
> @@ -0,0 +1,189 @@
> +// SPDX-License-Identifier: GPL-2.0
> +
> +#define pr_fmt(fmt) "HYPERV-IR: " fmt
> +
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +
> +#include 
> +#include 
> +#include 
> +#include 
> +
> +#include "irq_remapping.h"
> +
> +/*
> + * According IO-APIC spec, IO APIC has a 24-entry Interrupt
> + * Redirection Table.
> + */
> +#define IOAPIC_REMAPPING_ENTRY 24

KVM already defines KVM_IOAPIC_NUM_PINS - is this the same thing?

> +
> +static cpumask_t ioapic_max_cpumask = { CPU_BITS_NONE };
> +struct irq_domain *ioapic_ir_domain;
> +
> +static int hyperv_ir_set_affinity(struct irq_data *data,
> + const struct cpumask *mask, bool force)
> +{
> + struct irq_data *parent = data->parent_data;
> + struct irq_cfg *cfg = irqd_cfg(data);
> + struct IO_APIC_route_entry *entry;
> + cpumask_t cpumask;
> + int ret;
> +
> + cpumask_andnot(&cpumask, mask, &ioapic_max_cpumask);
> +
> + /* Return error If new irq affinity is out of ioapic_max_cpumask. */
> + if (!cpumask_empty(&cpumask))
> + return -EINVAL;
> +
> + ret = parent->chip->irq_set_affinity(parent, mask, force);
> + if (ret < 0 || ret == IRQ_SET_MASK_OK_DONE)
> + return ret;
> +
> + entry = data->chip_data;
> + entry->dest = cfg->dest_apicid;
> + entry->vector = cfg->vector;
> + send_cleanup_vector(cfg);
> +
> + return 0;
> +}
> +
> +static struct irq_chip hyperv_ir_chip = {
> + .name   = "HYPERV-IR",
> + .irq_ack= apic_ack_irq,
> + .irq_set_affinity   = hyperv_ir_set_affinity,
> +};
> +
> +static int hyperv_irq_remapping_alloc(struct irq_domain *domain,
> +  unsigned int virq, unsigned int nr_irqs,
> +  void *arg)
> +{
> + struct irq_alloc_info *info = arg;
> + struct IO_APIC_route_entry *entry;
> + struct irq_data *irq_data;
> + struct irq_desc *desc;
> + struct irq_cfg *cfg;
> + int ret = 0;
> +
> + if (!info || info->type != X86_IRQ_ALLOC_TYPE_IOAPIC || nr_irqs > 1)
> + return -EINVAL;
> +
> + ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, arg);
> + if (ret < 0)
> + goto fail;
> +
> + irq_data = irq_domain_get_irq_data(domain, virq);
> + cfg = irqd_cfg(irq_data);
> + if (!irq_data || !cfg) {
> + ret = -EINVAL;
> + goto fail;
> + }

fail: label doesn't do anything, you can just return (and you actually
do on the first failure path).

> +
> + irq_data->chip = &hyperv_ir_chip;
> +
> + /*
> +  * Save IOAPIC entry pointer here in order to set vector an

Re: [PATCH 2/3] HYPERV/IOMMU: Add Hyper-V stub IOMMU driver

2019-01-31 Thread Tianyu Lan
On Thu, Jan 31, 2019 at 7:59 PM Greg KH  wrote:
>
> On Thu, Jan 31, 2019 at 06:17:32PM +0800, lantianyu1...@gmail.com wrote:
> > --- /dev/null
> > +++ b/drivers/iommu/hyperv-iommu.c
> > @@ -0,0 +1,189 @@
> > +// SPDX-License-Identifier: GPL-2.0
> > +
> > +#define pr_fmt(fmt) "HYPERV-IR: " fmt
>
> Minor nit, you never do any pr_*() calls, so this isn't needed, right?

Yes, you are right. I will remove it. Sorry. I used pr_info() during
development stage and removed
them before sending patch out. Thanks.

>
> > +static cpumask_t ioapic_max_cpumask = { CPU_BITS_NONE };
> > +struct irq_domain *ioapic_ir_domain;
>
> Global?  Why?

It should be "static" here.

-- 
Best regards
Tianyu Lan


Re: [PATCH 2/3] HYPERV/IOMMU: Add Hyper-V stub IOMMU driver

2019-01-31 Thread Greg KH
On Thu, Jan 31, 2019 at 06:17:32PM +0800, lantianyu1...@gmail.com wrote:
> --- /dev/null
> +++ b/drivers/iommu/hyperv-iommu.c
> @@ -0,0 +1,189 @@
> +// SPDX-License-Identifier: GPL-2.0
> +
> +#define pr_fmt(fmt) "HYPERV-IR: " fmt

Minor nit, you never do any pr_*() calls, so this isn't needed, right?

> +static cpumask_t ioapic_max_cpumask = { CPU_BITS_NONE };
> +struct irq_domain *ioapic_ir_domain;

Global?  Why?

thanks,

greg k-h


[PATCH 2/3] HYPERV/IOMMU: Add Hyper-V stub IOMMU driver

2019-01-31 Thread lantianyu1986
From: Lan Tianyu 

On the bare metal, enabling X2APIC mode requires interrupt remapping
function which helps to deliver irq to cpu with 32-bit APIC ID.
Hyper-V doesn't provide interrupt remapping function so far and Hyper-V
MSI protocol already supports to deliver interrupt to the CPU whose
virtual processor index is more than 255. IO-APIC interrupt still has
8-bit APIC ID limitation.

This patch is to add Hyper-V stub IOMMU driver in order to enable
X2APIC mode successfully in Hyper-V Linux guest. The driver returns X2APIC
interrupt remapping capability when X2APIC mode is available. Otherwise,
it creates a Hyper-V irq domain to limit IO-APIC interrupts' affinity
and make sure cpus assigned with IO-APIC interrupt have 8-bit APIC ID.

Signed-off-by: Lan Tianyu 
---
 drivers/iommu/Kconfig |   7 ++
 drivers/iommu/Makefile|   1 +
 drivers/iommu/hyperv-iommu.c  | 189 ++
 drivers/iommu/irq_remapping.c |   3 +
 drivers/iommu/irq_remapping.h |   1 +
 5 files changed, 201 insertions(+)
 create mode 100644 drivers/iommu/hyperv-iommu.c

diff --git a/drivers/iommu/Kconfig b/drivers/iommu/Kconfig
index 45d7021..5c397c0 100644
--- a/drivers/iommu/Kconfig
+++ b/drivers/iommu/Kconfig
@@ -437,4 +437,11 @@ config QCOM_IOMMU
help
  Support for IOMMU on certain Qualcomm SoCs.
 
+config HYPERV_IOMMU
+   bool "Hyper-V stub IOMMU support"
+   depends on HYPERV
+   help
+   Hyper-V stub IOMMU driver provides capability to run
+   Linux guest with X2APIC mode enabled.
+
 endif # IOMMU_SUPPORT
diff --git a/drivers/iommu/Makefile b/drivers/iommu/Makefile
index a158a68..8c71a15 100644
--- a/drivers/iommu/Makefile
+++ b/drivers/iommu/Makefile
@@ -32,3 +32,4 @@ obj-$(CONFIG_EXYNOS_IOMMU) += exynos-iommu.o
 obj-$(CONFIG_FSL_PAMU) += fsl_pamu.o fsl_pamu_domain.o
 obj-$(CONFIG_S390_IOMMU) += s390-iommu.o
 obj-$(CONFIG_QCOM_IOMMU) += qcom_iommu.o
+obj-$(CONFIG_HYPERV_IOMMU) += hyperv-iommu.o
diff --git a/drivers/iommu/hyperv-iommu.c b/drivers/iommu/hyperv-iommu.c
new file mode 100644
index 000..a64b747
--- /dev/null
+++ b/drivers/iommu/hyperv-iommu.c
@@ -0,0 +1,189 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#define pr_fmt(fmt) "HYPERV-IR: " fmt
+
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#include 
+#include 
+#include 
+#include 
+
+#include "irq_remapping.h"
+
+/*
+ * According IO-APIC spec, IO APIC has a 24-entry Interrupt
+ * Redirection Table.
+ */
+#define IOAPIC_REMAPPING_ENTRY 24
+
+static cpumask_t ioapic_max_cpumask = { CPU_BITS_NONE };
+struct irq_domain *ioapic_ir_domain;
+
+static int hyperv_ir_set_affinity(struct irq_data *data,
+   const struct cpumask *mask, bool force)
+{
+   struct irq_data *parent = data->parent_data;
+   struct irq_cfg *cfg = irqd_cfg(data);
+   struct IO_APIC_route_entry *entry;
+   cpumask_t cpumask;
+   int ret;
+
+   cpumask_andnot(&cpumask, mask, &ioapic_max_cpumask);
+
+   /* Return error If new irq affinity is out of ioapic_max_cpumask. */
+   if (!cpumask_empty(&cpumask))
+   return -EINVAL;
+
+   ret = parent->chip->irq_set_affinity(parent, mask, force);
+   if (ret < 0 || ret == IRQ_SET_MASK_OK_DONE)
+   return ret;
+
+   entry = data->chip_data;
+   entry->dest = cfg->dest_apicid;
+   entry->vector = cfg->vector;
+   send_cleanup_vector(cfg);
+
+   return 0;
+}
+
+static struct irq_chip hyperv_ir_chip = {
+   .name   = "HYPERV-IR",
+   .irq_ack= apic_ack_irq,
+   .irq_set_affinity   = hyperv_ir_set_affinity,
+};
+
+static int hyperv_irq_remapping_alloc(struct irq_domain *domain,
+unsigned int virq, unsigned int nr_irqs,
+void *arg)
+{
+   struct irq_alloc_info *info = arg;
+   struct IO_APIC_route_entry *entry;
+   struct irq_data *irq_data;
+   struct irq_desc *desc;
+   struct irq_cfg *cfg;
+   int ret = 0;
+
+   if (!info || info->type != X86_IRQ_ALLOC_TYPE_IOAPIC || nr_irqs > 1)
+   return -EINVAL;
+
+   ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, arg);
+   if (ret < 0)
+   goto fail;
+
+   irq_data = irq_domain_get_irq_data(domain, virq);
+   cfg = irqd_cfg(irq_data);
+   if (!irq_data || !cfg) {
+   ret = -EINVAL;
+   goto fail;
+   }
+
+   irq_data->chip = &hyperv_ir_chip;
+
+   /*
+* Save IOAPIC entry pointer here in order to set vector and
+* and dest_apicid in the hyperv_irq_remappng_activate()
+* and hyperv_ir_set_affinity(). IOAPIC driver ignores
+* cfg->dest_apicid and cfg->vector when irq remapping
+* mode is enabled. Detail see ioapic_configure_entry().
+*/
+   irq_data->chip_data = entry = info->ioapic_entry;
+
+   /*
+* Hypver-V IO APIC irq affinity should be