libplatsupport: fix previous reverts

Due to my messing up of changelists, the revert
did not go through as cleanly as I'd hoped.

This commit reinstates
  "libplatsupport: riscv plic irqchip parser"
  23092562396ae9209679c83d75d25a953b70c2c6
and picks changes necessary from
  "libplatsupport: hifive ltimer refactor"
  c0c8ddcfac95a09b50e82cfa88367145d7aeaa5e
in order to build
  "libplatsupport: refactor more timers"
  ef1dfb7da52b8324281ee6999951bf603d44bdd7
diff --git a/libplatsupport/CMakeLists.txt b/libplatsupport/CMakeLists.txt
index e750e6f..a6d845b 100644
--- a/libplatsupport/CMakeLists.txt
+++ b/libplatsupport/CMakeLists.txt
@@ -63,6 +63,9 @@
 elseif(${KernelArch} STREQUAL "x86")
     list(APPEND deps src/arch/x86/delay.c)
     list(APPEND deps src/arch/x86/tsc.c)
+elseif(${KernelArch} STREQUAL "riscv")
+    list(APPEND deps src/arch/riscv/irqchip/plic.c)
+    list(APPEND irqchip_modules "-Wl,--undefined=riscv_plic_ptr")
 endif()
 
 if(KernelPlatformQEMUArmVirt)
diff --git a/libplatsupport/src/arch/riscv/irqchip/plic.c b/libplatsupport/src/arch/riscv/irqchip/plic.c
new file mode 100644
index 0000000..bb2cd17
--- /dev/null
+++ b/libplatsupport/src/arch/riscv/irqchip/plic.c
@@ -0,0 +1,86 @@
+/*
+ * Copyright 2020, Data61
+ * Commonwealth Scientific and Industrial Research Organisation (CSIRO)
+ * ABN 41 687 119 230.
+ *
+ * This software may be distributed and modified according to the terms of
+ * the BSD 2-Clause license. Note that NO WARRANTY is provided.
+ * See "LICENSE_BSD2.txt" for details.
+ *
+ * @TAG(DATA61_BSD)
+ */
+
+#include <assert.h>
+#include <stdbool.h>
+
+#include <utils/util.h>
+
+#include "../../../irqchip.h"
+
+/*
+ * Interrupt parser for the RISC-V PLIC.
+ *
+ * RISC-V PLIC interrupts specified as single-cell and each device node
+ * may specify one more more interrupts that it uses.
+ *
+ * Most properties in the `interrupt-controller` is useless to us - the
+ * kernel takes care of that.
+ *
+ * We just want to confirm that the `interrupt-controller` is specified as
+ * we expect, and then return the interrupts specified in the device node.
+ */
+
+static int parse_riscv_plic_interrupts(char *dtb_blob, int node_offset, int intr_controller_phandle,
+                                       irq_walk_cb_fn_t callback, void *token)
+{
+    bool is_extended = false;
+    int prop_len = 0;
+    const void *interrupts_prop = get_interrupts_prop(dtb_blob, node_offset, &is_extended, &prop_len);
+    assert(interrupts_prop != NULL);
+    int total_cells = prop_len / sizeof(uint32_t);
+
+    if (is_extended) {
+        ZF_LOGE("This parser doesn't understand extended interrupts");
+        return ENODEV;
+    }
+
+    /* Make sure that the interrupt we are parsing is 1-cell format */
+    int intr_controller_offset = fdt_node_offset_by_phandle(dtb_blob, intr_controller_phandle);
+    if (intr_controller_offset < 0) {
+        ZF_LOGE("Can't find the interrupt controller's node offset");
+        return intr_controller_offset;
+    }
+    const void *interrupt_cells_prop = fdt_getprop(dtb_blob, intr_controller_offset, "#interrupt-cells", NULL);
+    if (!interrupt_cells_prop) {
+        ZF_LOGE("No '#interrupt-cells' property!");
+        return ENODEV;
+    }
+    uint32_t num_interrupt_cells = READ_CELL(1, interrupt_cells_prop, 0);
+    if (num_interrupt_cells != 1) {
+        ZF_LOGE("This parser doesn't understand multi-cell interrupts");
+        return ENODEV;
+    }
+
+    /* Loop through each cell and call the callback */
+    for (int i = 0; i < total_cells; i++) {
+        ps_irq_t irq = {
+            .type = PS_INTERRUPT,
+            .irq = {
+                .number = READ_CELL(1, interrupts_prop, i)
+            }
+        };
+
+        int error = callback(irq, i, total_cells, token);
+        if (error) {
+            return error;
+        }
+    }
+
+    return 0;
+}
+
+char *riscv_plic_compatible_list[] = {
+    "riscv,plic0",
+    NULL
+};
+DEFINE_IRQCHIP_PARSER(riscv_plic, riscv_plic_compatible_list, parse_riscv_plic_interrupts);
diff --git a/libplatsupport/src/fdt.c b/libplatsupport/src/fdt.c
index e5af135..dbe4243 100644
--- a/libplatsupport/src/fdt.c
+++ b/libplatsupport/src/fdt.c
@@ -298,7 +298,7 @@
 static int irq_index_helper_walker(ps_irq_t irq, unsigned curr_num, size_t num_irqs, void *token)
 {
     index_helper_token_t *helper_token = token;
-    if (helper_token->desired_offset > num_irqs) {
+    if (helper_token->desired_offset >= num_irqs) {
         /* Bail early if we will never find it */
         return -ENOENT;
     }
@@ -338,6 +338,7 @@
 
     int error = ps_fdt_walk_irqs(&io_ops->io_fdt, cookie, irq_index_helper_walker, &token);
     if (error) {
+        assert(error <= PS_INVALID_IRQ_ID);
         return error;
     }
 
diff --git a/libplatsupport/src/ltimer.h b/libplatsupport/src/ltimer.h
index c6e6ee1..5651d07 100644
--- a/libplatsupport/src/ltimer.h
+++ b/libplatsupport/src/ltimer.h
@@ -12,6 +12,7 @@
 
 #include <platsupport/ltimer.h>
 #include <platsupport/irq.h>
+#include <platsupport/fdt.h>
 
 /* Per-platform ltimer IRQ handling function type */
 typedef int (*ltimer_handle_irq_fn_t)(void *data, ps_irq_t *irq);
@@ -45,3 +46,89 @@
     error = acknowledge_fn(ack_data);
     assert(!error);
 }
+
+static int helper_fdt_alloc_simple(
+    ps_io_ops_t *ops, char *fdt_path,
+    unsigned reg_choice, unsigned irq_choice,
+    void **vmap, pmem_region_t *pmem, irq_id_t *irq_id,
+    irq_callback_fn_t handler, void *handler_token
+)
+{
+    assert(fdt_path != NULL);
+    assert(vmap != NULL && pmem != NULL && irq_id != NULL);
+
+    int error;
+    void *temp_vmap;
+    pmem_region_t temp_pmem;
+    irq_id_t temp_irq_id;
+
+    *vmap = NULL;
+    *irq_id = PS_INVALID_IRQ_ID;
+
+    /* Gather FDT info */
+    ps_fdt_cookie_t *cookie = NULL;
+    error = ps_fdt_read_path(&ops->io_fdt, &ops->malloc_ops, fdt_path, &cookie);
+    if (error) {
+        ZF_LOGE("Simple FDT helper failed to read path (%d, %s)", error, fdt_path);
+        return error;
+    }
+
+    temp_vmap = ps_fdt_index_map_register(ops, cookie, reg_choice, &temp_pmem);
+    if (temp_vmap == NULL) {
+        ZF_LOGE("Simple FDT helper failed to map registers");
+        return ENODEV;
+    }
+
+    temp_irq_id = ps_fdt_index_register_irq(ops, cookie, irq_choice, handler, handler_token);
+    if (temp_irq_id <= PS_INVALID_IRQ_ID) {
+        ZF_LOGE("Simple FDT helper failed to register irqs (%d)", temp_irq_id);
+        return temp_irq_id;
+    }
+
+    error = ps_fdt_cleanup_cookie(&ops->malloc_ops, cookie);
+    if (error) {
+        ZF_LOGE("Simple FDT helper failed to clean up cookie (%d)", error);
+        return error;
+    }
+
+    *vmap = temp_vmap;
+    *pmem = temp_pmem;
+    *irq_id = temp_irq_id;
+
+    return 0;
+}
+
+static int get_resolution_dummy(void *data, uint64_t *resolution)
+{
+    return ENOSYS;
+}
+
+static int create_ltimer_simple(
+    ltimer_t *ltimer, ps_io_ops_t ops, size_t sz,
+    int (*get_time)(void *data, uint64_t *time),
+    int (*set_timeout)(void *data, uint64_t ns, timeout_type_t type),
+    int (*reset)(void *data),
+    void (*destroy)(void *data)
+)
+{
+    assert(ltimer != NULL);
+
+    ltimer->get_time = get_time;
+    ltimer->get_resolution = get_resolution_dummy;
+    ltimer->set_timeout = set_timeout;
+    ltimer->reset = reset;
+    ltimer->destroy = destroy;
+    ltimer->get_nth_irq = NULL;
+    ltimer->get_nth_pmem = NULL;
+    ltimer->get_num_irqs = NULL;
+    ltimer->get_num_pmems = NULL;
+
+    int error = ps_calloc(&ops.malloc_ops, 1, sz, &ltimer->data);
+    if (error) {
+        ZF_LOGE("Unable to allocate ltimer data");
+        return error;
+    }
+    assert(ltimer->data != NULL);
+
+    return 0;
+}