move msi-related functionality to generic isrm
authorLunaixsky <lunaixsky@qq.com>
Tue, 8 Oct 2024 20:16:06 +0000 (21:16 +0100)
committerLunaixsky <lunaixsky@qq.com>
Tue, 8 Oct 2024 20:16:06 +0000 (21:16 +0100)
* msi-related functionality are now part of generic isrm, as message
  based interrupt are actually de facto part of modern interrupt
  infrastructure.
* move bindirq and bindiv to x86-specific extension of isrm.
* ldelf will check the architecture before proceed to read anything
  other than elf-header.
* other minor tweaks

19 files changed:
lunaix-os/arch/generic/includes/asm-generic/isrm.h
lunaix-os/arch/x86/arch.c
lunaix-os/arch/x86/exceptions/interrupts.c
lunaix-os/arch/x86/exceptions/intr_routines.c
lunaix-os/arch/x86/exceptions/isrm.c
lunaix-os/arch/x86/hal/apic_timer.c
lunaix-os/arch/x86/hal/mc146818a.c
lunaix-os/arch/x86/hal/pci.c
lunaix-os/arch/x86/hal/ps2kbd.c
lunaix-os/arch/x86/includes/asm/x86_isrm.h [new file with mode: 0644]
lunaix-os/hal/ahci/ahci_pci.c
lunaix-os/hal/bus/pci.c
lunaix-os/hal/char/uart/16x50_isa.c
lunaix-os/hal/char/uart/16x50_pci.c
lunaix-os/hal/char/uart/LConfig
lunaix-os/includes/hal/pci.h
lunaix-os/kernel/exe/elf-generic/elfbfmt.c
lunaix-os/kernel/exe/elf-generic/ldelf.c
lunaix-os/makefile

index 7e319b5a02f66859bf2700a63514bed479b9daf9..e8117bb4ed7f350e9c530a45b29adc259f9b1dd2 100644 (file)
 #include <lunaix/types.h>
 #include <lunaix/hart_state.h>
 
+#include <hal/devtree.h>
+
 typedef void (*isr_cb)(const struct hart_state*);
 
+typedef struct {
+    ptr_t msi_addr;
+    reg_t msi_data;
+    int mapped_iv;
+} msi_vector_t;
+#define msi_addr(msiv)   ((msiv).msi_addr)
+#define msi_data(msiv)   ((msiv).msi_data)
+#define msi_vect(msiv)   ((msiv).mapped_iv)
+
 void
 isrm_init();
 
@@ -44,21 +55,20 @@ int
 isrm_ivexalloc(isr_cb handler);
 
 /**
- * @brief Bind a given irq and associated handler to an iv
+ * @brief Allocate an iv resource for MSI use
  *
- * @param iv iv allocated by system
+ * @param iv
  */
-int
-isrm_bindirq(int irq, isr_cb irq_handler);
+msi_vector_t
+isrm_msialloc(isr_cb handler);
 
 /**
- * @brief Bind given iv with it's associated handler
+ * @brief Bind the iv according to given device tree node
  *
- * @param iv
- * @param handler
+ * @param node
  */
-void
-isrm_bindiv(int iv, isr_cb handler);
+int
+isrm_bind_dtnode(struct dt_intr_node* node);
 
 /**
  * @brief Get the handler associated with the given iv
@@ -75,9 +85,6 @@ isrm_get_payload(const struct hart_state*);
 void
 isrm_set_payload(int iv, ptr_t);
 
-void
-isrm_irq_attach(int irq, int iv, cpu_t dest, u32_t flags);
-
 /**
  * @brief Notify end of interrupt event
  *
index 533ded95c0d398c099a334bb6a1d0bbbfebc07c6..f29b59840941128b9a5dd912d6691997f243e146 100644 (file)
@@ -1,9 +1,10 @@
 #include <hal/hwtimer.h>
 
-#include <asm-generic/isrm.h>
 #include <lunaix/spike.h>
 #include <lunaix/process.h>
 
+#include <asm/x86_isrm.h>
+
 #include "asm/x86.h"
 #include "asm/hart.h"
 
index 6a9ff812425f4436d5793db505bdb489dee8d354..ceb3853150578fa1fb8cf28a3d583bc7a81eef7b 100644 (file)
@@ -1,13 +1,13 @@
 #include <asm/hart.h>
 #include "asm/x86.h"
 
-#include <asm-generic/isrm.h>
-
 #include <lunaix/mm/vmm.h>
 #include <lunaix/process.h>
 #include <lunaix/sched.h>
 #include <lunaix/syslog.h>
 
+#include <asm/x86_isrm.h>
+
 LOG_MODULE("INTR")
 
 static inline void
index 8bc1ca51f202d4504ebb38799e8421a81f4ced3c..5e18496f9a630700f1e14deb6401dd086f206c2f 100644 (file)
@@ -1,6 +1,5 @@
 #include <asm/hart.h>
 
-#include <asm-generic/isrm.h>
 #include <lunaix/process.h>
 #include <lunaix/sched.h>
 #include <lunaix/spike.h>
@@ -10,6 +9,7 @@
 
 #include <klibc/strfmt.h>
 
+#include <asm/x86_isrm.h>
 #include "asm/soc/apic.h"
 #include "asm/x86.h"
 
index dea130ccd546db6a13e54a9c19b5914c262ae536..a3c223d6833b3b52fd9d03c8b34d3e493574c5f4 100644 (file)
@@ -1,6 +1,6 @@
 #include <lunaix/spike.h>
 #include <lunaix/owloysius.h>
-#include <asm-generic/isrm.h>
+#include <asm/x86_isrm.h>
 
 #include "asm/x86.h"
 #include "asm/soc/ioapic.h"
@@ -165,6 +165,24 @@ isrm_notify_eos(cpu_t id)
     isrm_notify_eoi(id, LUNAIX_SCHED);
 }
 
+msi_vector_t
+isrm_msialloc(isr_cb handler)
+{
+    unsigned int iv = isrm_ivexalloc(handler);
+
+    return (msi_vector_t){ 
+        .msi_addr  = 0xfee00000,
+        .msi_data  = iv,
+        .mapped_iv = iv
+    };
+}
+
+int
+isrm_bind_dtnode(struct dt_intr_node* node)
+{
+    fail("not supported");
+}
+
 
 static void
 __intc_init()
index eae4c3feb76391ff1bf518520cc865ac9b0f9228..56cd41075e96a9663c8dd7b6b2b5a8197cb544e1 100644 (file)
@@ -3,10 +3,10 @@
 
 #include <lunaix/clock.h>
 #include <lunaix/compiler.h>
-#include <asm-generic/isrm.h>
 #include <lunaix/spike.h>
 #include <lunaix/syslog.h>
 
+#include <asm/x86_isrm.h>
 #include "asm/soc/apic.h"
 
 LOG_MODULE("APIC_TIMER")
index 879b4f5c2ced393e1c32a9b093557a433fccc404..9c788f9b9ffa5984fc7043690129a66d9de8504b 100644 (file)
@@ -10,7 +10,6 @@
  *
  */
 
-#include <asm-generic/isrm.h>
 #include <lunaix/mm/valloc.h>
 #include <lunaix/status.h>
 #include <lunaix/hart_state.h>
@@ -19,6 +18,7 @@
 
 #include <klibc/string.h>
 
+#include <asm/x86_isrm.h>
 #include <asm/x86_pmio.h>
 
 #define RTC_INDEX_PORT 0x70
index d871790b4fcb4359ed160f4ecd97d2261606b23f..599abf71341119f8b841dad8a419bfe0ff8b166c 100644 (file)
@@ -19,15 +19,4 @@ pci_write_cspace(ptr_t base, int offset, pci_reg_t data)
     port_wrdword(PCI_CONFIG_DATA, data);
 }
 
-#endif
-
-u16_t
-pci_config_msi_data(int vector) {
-    return vector;
-}
-
-ptr_t
-pci_get_msi_base() {
-    return 0xFEE00000;
-}
-
+#endif
\ No newline at end of file
index cd5518d8f14afba61c7ad9491de3024ea4e4b4eb..02e98d1d8532afc8254fd5ba8be66c6d0a1f05a5 100644 (file)
@@ -1,7 +1,6 @@
 #include <lunaix/clock.h>
 #include <lunaix/ds/mutex.h>
 #include <lunaix/input.h>
-#include <asm-generic/isrm.h>
 #include <lunaix/keyboard.h>
 #include <lunaix/syslog.h>
 #include <lunaix/timer.h>
@@ -12,6 +11,7 @@
 #include <klibc/string.h>
 
 #include "asm/x86_cpu.h"
+#include <asm/x86_isrm.h>
 #include <asm/x86_pmio.h>
 
 #define PS2_PORT_ENC_DATA 0x60
diff --git a/lunaix-os/arch/x86/includes/asm/x86_isrm.h b/lunaix-os/arch/x86/includes/asm/x86_isrm.h
new file mode 100644 (file)
index 0000000..a2bcacd
--- /dev/null
@@ -0,0 +1,27 @@
+#ifndef __LUNAIX_X86_ISRM_H
+#define __LUNAIX_X86_ISRM_H
+
+#include <asm-generic/isrm.h>
+
+/**
+ * @brief Bind a given irq and associated handler to an iv
+ *
+ * @param iv iv allocated by system
+ */
+int
+isrm_bindirq(int irq, isr_cb irq_handler);
+
+/**
+ * @brief Bind given iv with it's associated handler
+ *
+ * @param iv
+ * @param handler
+ */
+void
+isrm_bindiv(int iv, isr_cb handler);
+
+void
+isrm_irq_attach(int irq, int iv, cpu_t dest, u32_t flags);
+
+
+#endif /* __LUNAIX_X86_ISRM_H */
index 6c59cf8455e76935d7220cc9b95581171788c7fb..7c9288fa3b5b37b0677e6d1ae0005d76f8302a97 100644 (file)
@@ -9,6 +9,7 @@ ahci_pci_bind(struct device_def* def, struct device* dev)
     struct pci_device* ahci_dev;
     struct pci_base_addr* bar6;
     struct ahci_driver* ahci_drv;
+    msi_vector_t msiv;
 
     ahci_dev = PCI_DEVICE(dev);
     bar6 = pci_device_bar(ahci_dev, 5);
@@ -19,21 +20,16 @@ ahci_pci_bind(struct device_def* def, struct device* dev)
     pci_cmd_set_mmio(&cmd);
     pci_cmd_set_msi(&cmd);
     pci_apply_command(ahci_dev, cmd);
+    
+    assert(pci_capability_msi(ahci_dev));
 
-    int iv;
-    if (pci_capability_msi(ahci_dev)) {
-        iv = isrm_ivexalloc(ahci_hba_isr);
-        pci_setup_msi(ahci_dev, iv);
-    }
-    else {
-        iv = pci_intr_irq(ahci_dev);
-        iv = isrm_bindirq(iv, ahci_hba_isr);
-    }
+    msiv = isrm_msialloc(ahci_hba_isr);
+    pci_setup_msi(ahci_dev, msiv);
 
     struct ahci_driver_param param = {
         .mmio_base = bar6->start,
         .mmio_size = bar6->size,
-        .ahci_iv = iv,
+        .ahci_iv = msi_vect(msiv),
     };
 
     ahci_drv = ahci_driver_init(&param);
index 6d26d09d7d4c4e789267c6f934ed8a31403cd487..6b13b942e794db692649a8b89801d9ee77f15e89 100644 (file)
@@ -210,12 +210,12 @@ pci_probe_bar_info(struct pci_device* device)
 }
 
 void
-pci_setup_msi(struct pci_device* device, int vector)
+pci_setup_msi(struct pci_device* device, msi_vector_t msiv)
 {
     // PCI LB Spec. (Rev 3) Section 6.8 & 6.8.1
 
-    ptr_t msi_addr = pci_get_msi_base();
-    u32_t msi_data = pci_config_msi_data(vector);
+    ptr_t msi_addr = msi_addr(msiv);
+    u32_t msi_data = msi_data(msiv);
 
     pci_reg_t reg1 = pci_read_cspace(device->cspace_base, device->msi_loc);
     pci_reg_t msg_ctl = reg1 >> 16;
index 128803e29aa050a4c84fa1c11effcf3aed40146d..414dc334645ec3a63de6e279299066c024319066 100644 (file)
@@ -1,8 +1,8 @@
 #include <lunaix/device.h>
-#include <asm-generic/isrm.h>
 #include <lunaix/syslog.h>
 
 #include <asm/x86_pmio.h>
+#include <asm/x86_isrm.h>
 
 #include "16x50.h"
 
@@ -58,7 +58,7 @@ upiom_init(struct device_def* def)
 
 static struct device_def uart_pmio_def = {
     .class = DEVCLASS(DEVIF_SOC, DEVFN_CHAR, DEV_UART16550),
-    .name = "16550 UART (PIO)",
+    .name = "16550 UART (pmio, isa-bus)",
     .init = upiom_init
 };
 EXPORT_DEVICE(uart16550_pmio, &uart_pmio_def, load_onboot);
\ No newline at end of file
index 1d8afd874b1e2322e12843aef551e3678e19154f..59aac6c776dcc516f194bea19feb19f44f0a5a83 100644 (file)
@@ -52,11 +52,11 @@ pci16650_check_compat(struct pci_device_def* def,
 static int
 pci16650_binder(struct device_def* def, struct device* dev)
 {
-    int irq;
     struct pci_base_addr* bar;
     struct pci_device* pcidev;
     struct uart16550* uart;
     struct serial_dev* sdev;
+    msi_vector_t msiv;
     
     pcidev = PCI_DEVICE(dev);
 
@@ -71,14 +71,19 @@ pci16650_binder(struct device_def* def, struct device* dev)
         if (bar->size == 0) {
             continue;
         }
-                
+
         if (!pci_bar_mmio_space(bar)) {
+#ifdef CONFIG_PCI_PMIO
             pci_cmd_set_pmio(&cmd);
             pci_apply_command(pcidev, cmd);
 
             uart = uart16x50_pmio_create(bar->start);
-        }
-        else {
+#else
+            WARN("plaform configured to not support pmio access.")
+            continue;
+#endif
+        } else 
+        {
             pci_cmd_set_mmio(&cmd);
             pci_apply_command(pcidev, cmd);
 
@@ -90,23 +95,22 @@ pci16650_binder(struct device_def* def, struct device* dev)
             continue;
         }
 
-        if (pci_capability_msi(pcidev)) {
-            irq = isrm_ivexalloc(uart_msi_irq_handler);
-            isrm_set_payload(irq, __ptr(uart));
-            pci_setup_msi(pcidev, irq);
-        }
-        else {
-            irq = pci_intr_irq(pcidev);
-            irq = isrm_bindirq(irq, uart_intx_irq_handler);
+        if (!pci_capability_msi(pcidev)) {
+            WARN("failed to fallback to legacy INTx: not supported.");
+            continue;
         }
 
+        msiv = isrm_msialloc(uart_msi_irq_handler);
+        isrm_set_payload(msi_vect(msiv), __ptr(uart));
+        pci_setup_msi(pcidev, msiv);
+
         INFO("base: 0x%x (%s), irq=%d (%s)", 
                 bar->start, 
                 pci_bar_mmio_space(bar) ? "mmio" : "pmio",
-                irq
+                msi_vect(msiv)
                 pci_capability_msi(pcidev) ? "msi" : "intx, re-routed");
         
-        uart->iv = irq;
+        uart->iv = msi_vect(msiv);
 
         sdev = uart_create_serial(uart, &def->class, &pci_ports, "PCI");
         pci_bind_instance(pcidev, sdev);
index 9ed1f09ec4d63177d8437e5b264c01477234986c..bbb019df967d4a77c3947ee3daf9d4117bb91d7d 100644 (file)
@@ -12,8 +12,11 @@ def uart_16x50():
 
         type(bool)
 
+        default(True)
+        
         is_x86 = v(arch) in ["i386", "x86_64"]
-        default(is_x86)
+        if not is_x86:
+            set_value(False)
 
         return is_x86
     
index d457a1ed276da5d78398e9869ba1f05a1492e9e9..95ed7fd313eb0bc885507e2ca0ae290ca18b2d59 100644 (file)
@@ -6,6 +6,8 @@
 #include <lunaix/ds/llist.h>
 #include <lunaix/types.h>
 
+#include <asm-generic/isrm.h>
+
 #define EXPORT_PCI_DEVICE(id, pci_devdef, stage)                               \
     EXPORT_DEVICE(id, &(pci_devdef)->devdef, stage)
 
@@ -170,7 +172,7 @@ void
 pci_probe_bar_info(struct pci_device* device);
 
 void
-pci_setup_msi(struct pci_device* device, int vector);
+pci_setup_msi(struct pci_device* device, msi_vector_t msiv);
 
 void
 pci_probe_msi_info(struct pci_device* device);
@@ -263,11 +265,4 @@ pci_read_cspace(ptr_t base, int offset);
 void
 pci_write_cspace(ptr_t base, int offset, pci_reg_t data);
 
-u16_t
-pci_config_msi_data(int vector);
-
-ptr_t
-pci_get_msi_base();
-
-
 #endif /* __LUNAIX_PCI_H */
index 1dbfd4555d712adc28411899193c3d5f51d80833..575ff3f8acf60b7445933c2aeb6e1e2420a6652f 100644 (file)
@@ -24,6 +24,10 @@ elf_do_open(struct elf* elf, struct v_file* elf_file)
         return status;
     }
 
+    if (!elf_check_arch(elf)) {
+        return EINVAL;
+    }
+
     if ((status = elf_read_phdr(elf)) < 0) {
         elf_close(elf);
         return status;
index 124f9165acee3f71a928663f83c825002b2f8f3f..1f76dad435f1293aef44ceb3eb068073d40de959 100644 (file)
@@ -66,11 +66,6 @@ load_executable(struct load_context* context, const struct v_file* exefile)
         goto done;
     }
 
-    if (!elf_check_arch(&elf)) {
-        errno = EINVAL;
-        goto done;
-    }
-
     if (!(elf_check_exec(&elf, ET_EXEC) || elf_check_exec(&elf, ET_DYN))) {
         errno = ENOEXEC;
         goto done;
index 12cc1f5565c26a261b584e75dc58bcb5fa562b00..5b339d6a47992feedea3b62570311767335b2b66 100644 (file)
@@ -50,7 +50,7 @@ tool:
 .NOTPARALLEL:
 export KCMD=$(CMDLINE)
 export LBUILD ARCH MODE
-all: $(kbuild_dir) tool kernel usr/build
+all: $(kbuild_dir) tool kernel
 
 rootfs: usr/build
        $(call status,TASK,$(notdir $@))