x86_64: Add MSI support for ICU MachIntrABI
[dragonfly.git] / sys / platform / pc64 / icu / icu_abi.c
index d787868..db6e8f3 100644 (file)
@@ -44,6 +44,7 @@
 #include <sys/kernel.h>
 #include <sys/machintr.h>
 #include <sys/interrupt.h>
+#include <sys/rman.h>
 #include <sys/bus.h>
 
 #include <machine/segments.h>
@@ -51,6 +52,7 @@
 #include <machine/intr_machdep.h>
 #include <machine/globaldata.h>
 #include <machine/smp.h>
+#include <machine/msi_var.h>
 
 #include <sys/thread2.h>
 
@@ -84,49 +86,107 @@ static inthand_t *icu_intr[ICU_HWI_VECTORS] = {
 static struct icu_irqmap {
        int                     im_type;        /* ICU_IMT_ */
        enum intr_trigger       im_trig;
-} icu_irqmaps[IDT_HWI_VECTORS];
+       int                     im_msi_base;
+} icu_irqmaps[MAXCPU][IDT_HWI_VECTORS];
+
+static struct lwkt_token icu_irqmap_tok =
+       LWKT_TOKEN_INITIALIZER(icu_irqmap_token);
 
 #define ICU_IMT_UNUSED         0       /* KEEP THIS */
 #define ICU_IMT_RESERVED       1
 #define ICU_IMT_LINE           2
 #define ICU_IMT_SYSCALL                3
+#define ICU_IMT_MSI            4
+
+#define ICU_IMT_ISHWI(map)     ((map)->im_type != ICU_IMT_RESERVED && \
+                                (map)->im_type != ICU_IMT_SYSCALL)
 
 extern void    ICU_INTREN(int);
 extern void    ICU_INTRDIS(int);
 
 extern int     imcr_present;
 
+static void    icu_abi_intr_enable(int);
+static void    icu_abi_intr_disable(int);
 static void    icu_abi_intr_setup(int, int);
 static void    icu_abi_intr_teardown(int);
 static void    icu_abi_intr_config(int, enum intr_trigger, enum intr_polarity);
 static int     icu_abi_intr_cpuid(int);
 
+static int     icu_abi_msi_alloc(int [], int, int);
+static void    icu_abi_msi_release(const int [], int, int);
+static void    icu_abi_msi_map(int, uint64_t *, uint32_t *, int);
+
 static void    icu_abi_finalize(void);
 static void    icu_abi_cleanup(void);
 static void    icu_abi_setdefault(void);
 static void    icu_abi_stabilize(void);
 static void    icu_abi_initmap(void);
+static void    icu_abi_rman_setup(struct rman *);
 
 struct machintr_abi MachIntrABI_ICU = {
        MACHINTR_ICU,
-       .intr_disable   = ICU_INTRDIS,
-       .intr_enable    = ICU_INTREN,
+       .intr_disable   = icu_abi_intr_disable,
+       .intr_enable    = icu_abi_intr_enable,
        .intr_setup     = icu_abi_intr_setup,
        .intr_teardown  = icu_abi_intr_teardown,
        .intr_config    = icu_abi_intr_config,
        .intr_cpuid     = icu_abi_intr_cpuid,
 
+       .msi_alloc      = icu_abi_msi_alloc,
+       .msi_release    = icu_abi_msi_release,
+       .msi_map        = icu_abi_msi_map,
+
        .finalize       = icu_abi_finalize,
        .cleanup        = icu_abi_cleanup,
        .setdefault     = icu_abi_setdefault,
        .stabilize      = icu_abi_stabilize,
-       .initmap        = icu_abi_initmap
+       .initmap        = icu_abi_initmap,
+       .rman_setup     = icu_abi_rman_setup
 };
 
+static int     icu_abi_msi_start;      /* NOTE: for testing only */
+
 /*
  * WARNING!  SMP builds can use the ICU now so this code must be MP safe.
  */
 
+static void
+icu_abi_intr_enable(int irq)
+{
+       const struct icu_irqmap *map;
+
+       KASSERT(irq >= 0 && irq < IDT_HWI_VECTORS,
+           ("icu enable, invalid irq %d\n", irq));
+
+       map = &icu_irqmaps[mycpuid][irq];
+       KASSERT(ICU_IMT_ISHWI(map),
+           ("icu enable, not hwi irq %d, type %d, cpu%d\n",
+            irq, map->im_type, mycpuid));
+       if (map->im_type != ICU_IMT_LINE)
+               return;
+
+       ICU_INTREN(irq);
+}
+
+static void
+icu_abi_intr_disable(int irq)
+{
+       const struct icu_irqmap *map;
+
+       KASSERT(irq >= 0 && irq < IDT_HWI_VECTORS,
+           ("icu disable, invalid irq %d\n", irq));
+
+       map = &icu_irqmaps[mycpuid][irq];
+       KASSERT(ICU_IMT_ISHWI(map),
+           ("icu disable, not hwi irq %d, type %d, cpu%d\n",
+            irq, map->im_type, mycpuid));
+       if (map->im_type != ICU_IMT_LINE)
+               return;
+
+       ICU_INTRDIS(irq);
+}
+
 /*
  * Called before interrupts are physically enabled
  */
@@ -136,8 +196,8 @@ icu_abi_stabilize(void)
        int intr;
 
        for (intr = 0; intr < ICU_HWI_VECTORS; ++intr)
-               machintr_intr_disable(intr);
-       machintr_intr_enable(ICU_IRQ_SLAVE);
+               ICU_INTRDIS(intr);
+       ICU_INTREN(ICU_IRQ_SLAVE);
 }
 
 /*
@@ -178,14 +238,23 @@ icu_abi_finalize(void)
 static void
 icu_abi_intr_setup(int intr, int flags)
 {
+       const struct icu_irqmap *map;
        register_t ef;
 
-       KKASSERT(intr >= 0 && intr < ICU_HWI_VECTORS && intr != ICU_IRQ_SLAVE);
+       KASSERT(intr >= 0 && intr < IDT_HWI_VECTORS,
+           ("icu setup, invalid irq %d\n", intr));
+
+       map = &icu_irqmaps[mycpuid][intr];
+       KASSERT(ICU_IMT_ISHWI(map),
+           ("icu setup, not hwi irq %d, type %d, cpu%d\n",
+            intr, map->im_type, mycpuid));
+       if (map->im_type != ICU_IMT_LINE)
+               return;
 
        ef = read_rflags();
        cpu_disable_intr();
 
-       machintr_intr_enable(intr);
+       ICU_INTREN(intr);
 
        write_rflags(ef);
 }
@@ -193,14 +262,23 @@ icu_abi_intr_setup(int intr, int flags)
 static void
 icu_abi_intr_teardown(int intr)
 {
+       const struct icu_irqmap *map;
        register_t ef;
 
-       KKASSERT(intr >= 0 && intr < ICU_HWI_VECTORS && intr != ICU_IRQ_SLAVE);
+       KASSERT(intr >= 0 && intr < IDT_HWI_VECTORS,
+           ("icu teardown, invalid irq %d\n", intr));
+
+       map = &icu_irqmaps[mycpuid][intr];
+       KASSERT(ICU_IMT_ISHWI(map),
+           ("icu teardown, not hwi irq %d, type %d, cpu%d\n",
+            intr, map->im_type, mycpuid));
+       if (map->im_type != ICU_IMT_LINE)
+               return;
 
        ef = read_rflags();
        cpu_disable_intr();
 
-       machintr_intr_disable(intr);
+       ICU_INTRDIS(intr);
 
        write_rflags(ef);
 }
@@ -213,31 +291,56 @@ icu_abi_setdefault(void)
        for (intr = 0; intr < ICU_HWI_VECTORS; ++intr) {
                if (intr == ICU_IRQ_SLAVE)
                        continue;
-               setidt(IDT_OFFSET + intr, icu_intr[intr], SDT_SYSIGT,
-                      SEL_KPL, 0);
+               setidt_global(IDT_OFFSET + intr, icu_intr[intr],
+                   SDT_SYSIGT, SEL_KPL, 0);
        }
 }
 
 static void
 icu_abi_initmap(void)
 {
-       int i;
-
-       for (i = 0; i < ICU_HWI_VECTORS; ++i)
-               icu_irqmaps[i].im_type = ICU_IMT_LINE;
-       icu_irqmaps[ICU_IRQ_SLAVE].im_type = ICU_IMT_RESERVED;
-
-       if (elcr_found) {
-               for (i = 0; i < ICU_HWI_VECTORS; ++i)
-                       icu_irqmaps[i].im_trig = elcr_read_trigger(i);
-       } else {
-               /*
-                * NOTE: Trigger mode does not matter at all
-                */
-               for (i = 0; i < ICU_HWI_VECTORS; ++i)
-                       icu_irqmaps[i].im_trig = INTR_TRIGGER_EDGE;
+       int cpu;
+
+       kgetenv_int("hw.icu.msi_start", &icu_abi_msi_start);
+       icu_abi_msi_start &= ~0x1f;     /* MUST be 32 aligned */
+
+       /*
+        * NOTE: ncpus is not ready yet
+        */
+       for (cpu = 0; cpu < MAXCPU; ++cpu) {
+               int i;
+
+               if (cpu != 0) {
+                       for (i = 0; i < ICU_HWI_VECTORS; ++i)
+                               icu_irqmaps[cpu][i].im_type = ICU_IMT_RESERVED;
+               } else {
+                       for (i = 0; i < ICU_HWI_VECTORS; ++i)
+                               icu_irqmaps[cpu][i].im_type = ICU_IMT_LINE;
+                       icu_irqmaps[cpu][ICU_IRQ_SLAVE].im_type =
+                           ICU_IMT_RESERVED;
+
+                       if (elcr_found) {
+                               for (i = 0; i < ICU_HWI_VECTORS; ++i) {
+                                       icu_irqmaps[cpu][i].im_trig =
+                                           elcr_read_trigger(i);
+                               }
+                       } else {
+                               /*
+                                * NOTE: Trigger mode does not matter at all
+                                */
+                               for (i = 0; i < ICU_HWI_VECTORS; ++i) {
+                                       icu_irqmaps[cpu][i].im_trig =
+                                           INTR_TRIGGER_EDGE;
+                               }
+                       }
+               }
+
+               for (i = 0; i < IDT_HWI_VECTORS; ++i)
+                       icu_irqmaps[cpu][i].im_msi_base = -1;
+
+               icu_irqmaps[cpu][IDT_OFFSET_SYSCALL - IDT_OFFSET].im_type =
+                   ICU_IMT_SYSCALL;
        }
-       icu_irqmaps[IDT_OFFSET_SYSCALL - IDT_OFFSET].im_type = ICU_IMT_SYSCALL;
 }
 
 static void
@@ -249,7 +352,7 @@ icu_abi_intr_config(int irq, enum intr_trigger trig,
        KKASSERT(trig == INTR_TRIGGER_EDGE || trig == INTR_TRIGGER_LEVEL);
 
        KKASSERT(irq >= 0 && irq < IDT_HWI_VECTORS);
-       map = &icu_irqmaps[irq];
+       map = &icu_irqmaps[0][irq];
 
        KKASSERT(map->im_type == ICU_IMT_LINE);
 
@@ -278,3 +381,208 @@ icu_abi_intr_cpuid(int irq __unused)
 {
        return 0;
 }
+
+static void
+icu_abi_rman_setup(struct rman *rm)
+{
+       int start, end, i;
+
+       KASSERT(rm->rm_cpuid >= 0 && rm->rm_cpuid < MAXCPU,
+           ("invalid rman cpuid %d", rm->rm_cpuid));
+
+       start = end = -1;
+       for (i = 0; i < IDT_HWI_VECTORS; ++i) {
+               const struct icu_irqmap *map = &icu_irqmaps[rm->rm_cpuid][i];
+
+               if (start < 0) {
+                       if (ICU_IMT_ISHWI(map))
+                               start = end = i;
+               } else {
+                       if (ICU_IMT_ISHWI(map)) {
+                               end = i;
+                       } else {
+                               KKASSERT(end >= 0);
+                               if (bootverbose) {
+                                       kprintf("ICU: rman cpu%d %d - %d\n",
+                                           rm->rm_cpuid, start, end);
+                               }
+                               if (rman_manage_region(rm, start, end)) {
+                                       panic("rman_manage_region"
+                                           "(cpu%d %d - %d)", rm->rm_cpuid,
+                                           start, end);
+                               }
+                               start = end = -1;
+                       }
+               }
+       }
+       if (start >= 0) {
+               KKASSERT(end >= 0);
+               if (bootverbose) {
+                       kprintf("ICU: rman cpu%d %d - %d\n",
+                           rm->rm_cpuid, start, end);
+               }
+               if (rman_manage_region(rm, start, end)) {
+                       panic("rman_manage_region(cpu%d %d - %d)",
+                           rm->rm_cpuid, start, end);
+               }
+       }
+}
+
+static int
+icu_abi_msi_alloc(int intrs[], int count, int cpuid)
+{
+       int i, error;
+
+       KASSERT(cpuid >= 0 && cpuid < ncpus,
+           ("invalid cpuid %d", cpuid));
+
+       KASSERT(count > 0 && count <= 32, ("invalid count %d\n", count));
+       KASSERT((count & (count - 1)) == 0,
+           ("count %d is not power of 2\n", count));
+
+       lwkt_gettoken(&icu_irqmap_tok);
+
+       /*
+        * NOTE:
+        * Since IDT_OFFSET is 32, which is the maximum valid 'count',
+        * we do not need to find out the first properly aligned
+        * interrupt vector.
+        */
+
+       error = EMSGSIZE;
+       for (i = icu_abi_msi_start; i < IDT_HWI_VECTORS; i += count) {
+               int j;
+
+               if (icu_irqmaps[cpuid][i].im_type != ICU_IMT_UNUSED)
+                       continue;
+
+               for (j = 1; j < count; ++j) {
+                       if (icu_irqmaps[cpuid][i + j].im_type != ICU_IMT_UNUSED)
+                               break;
+               }
+               if (j != count)
+                       continue;
+
+               for (j = 0; j < count; ++j) {
+                       struct icu_irqmap *map;
+                       int intr = i + j;
+
+                       map = &icu_irqmaps[cpuid][intr];
+                       KASSERT(map->im_msi_base < 0,
+                           ("intr %d, stale MSI-base %d\n",
+                            intr, map->im_msi_base));
+
+                       map->im_type = ICU_IMT_MSI;
+                       map->im_msi_base = i;
+
+                       intrs[j] = intr;
+                       msi_setup(intr, cpuid);
+
+                       if (bootverbose) {
+                               kprintf("alloc MSI intr %d on cpu%d\n",
+                                   intr, cpuid);
+                       }
+
+               }
+               error = 0;
+               break;
+       }
+
+       lwkt_reltoken(&icu_irqmap_tok);
+
+       return error;
+}
+
+static void
+icu_abi_msi_release(const int intrs[], int count, int cpuid)
+{
+       int i, msi_base = -1, intr_next = -1, mask;
+
+       KASSERT(cpuid >= 0 && cpuid < ncpus,
+           ("invalid cpuid %d", cpuid));
+
+       KASSERT(count > 0 && count <= 32, ("invalid count %d\n", count));
+
+       mask = count - 1;
+       KASSERT((count & mask) == 0, ("count %d is not power of 2\n", count));
+
+       lwkt_gettoken(&icu_irqmap_tok);
+
+       for (i = 0; i < count; ++i) {
+               struct icu_irqmap *map;
+               int intr = intrs[i];
+
+               KASSERT(intr >= 0 && intr < IDT_HWI_VECTORS,
+                   ("invalid intr %d\n", intr));
+
+               map = &icu_irqmaps[cpuid][intr];
+               KASSERT(map->im_type == ICU_IMT_MSI,
+                   ("try release non-MSI intr %d, type %d\n",
+                    intr, map->im_type));
+               KASSERT(map->im_msi_base >= 0 && map->im_msi_base <= intr,
+                   ("intr %d, invalid MSI-base %d\n", intr, map->im_msi_base));
+               KASSERT((map->im_msi_base & mask) == 0,
+                   ("intr %d, MSI-base %d is not proper aligned %d\n",
+                    intr, map->im_msi_base, count));
+
+               if (msi_base < 0) {
+                       msi_base = map->im_msi_base;
+               } else {
+                       KASSERT(map->im_msi_base == msi_base,
+                           ("intr %d, inconsistent MSI-base, "
+                            "was %d, now %d\n",
+                            intr, msi_base, map->im_msi_base));
+               }
+
+               if (intr_next < intr)
+                       intr_next = intr;
+
+               map->im_type = ICU_IMT_UNUSED;
+               map->im_msi_base = -1;
+
+               if (bootverbose)
+                       kprintf("release MSI intr %d on cpu%d\n", intr, cpuid);
+       }
+
+       KKASSERT(intr_next > 0);
+       KKASSERT(msi_base >= 0);
+
+       ++intr_next;
+       if (intr_next < IDT_HWI_VECTORS) {
+               const struct icu_irqmap *map = &icu_irqmaps[cpuid][intr_next];
+
+               if (map->im_type == ICU_IMT_MSI) {
+                       KASSERT(map->im_msi_base != msi_base,
+                           ("more than %d MSI was allocated\n", count));
+               }
+       }
+
+       lwkt_reltoken(&icu_irqmap_tok);
+}
+
+static void
+icu_abi_msi_map(int intr, uint64_t *addr, uint32_t *data, int cpuid)
+{
+       const struct icu_irqmap *map;
+
+       KASSERT(cpuid >= 0 && cpuid < ncpus,
+           ("invalid cpuid %d", cpuid));
+
+       KASSERT(intr >= 0 && intr < IDT_HWI_VECTORS,
+           ("invalid intr %d\n", intr));
+
+       lwkt_gettoken(&icu_irqmap_tok);
+
+       map = &icu_irqmaps[cpuid][intr];
+       KASSERT(map->im_type == ICU_IMT_MSI,
+           ("try map non-MSI intr %d, type %d\n", intr, map->im_type));
+       KASSERT(map->im_msi_base >= 0 && map->im_msi_base <= intr,
+           ("intr %d, invalid MSI-base %d\n", intr, map->im_msi_base));
+
+       msi_map(map->im_msi_base, addr, data, cpuid);
+
+       if (bootverbose)
+               kprintf("map MSI intr %d on cpu%d\n", intr, cpuid);
+
+       lwkt_reltoken(&icu_irqmap_tok);
+}