WIP acpi

Change-Id: I573cf74247e6d1e7ebd5dcf949ee132a9110162e
diff --git a/kernel/lib/acpi_lite/acpi_lite.cpp b/kernel/lib/acpi_lite/acpi_lite.cpp
index dc8e7a6..8ae4008 100644
--- a/kernel/lib/acpi_lite/acpi_lite.cpp
+++ b/kernel/lib/acpi_lite/acpi_lite.cpp
@@ -248,7 +248,7 @@
         uint8_t length = madt_array[off + 1];
 
         if (type == search_type) {
-            callback(static_cast<const void *>(&madt_array[off]));
+            callback(static_cast<const void *>(&madt_array[off]) , length);
         }
 
         off += length;
diff --git a/kernel/lib/acpi_lite/include/lib/acpi_lite.h b/kernel/lib/acpi_lite/include/lib/acpi_lite.h
index 72cc72b..e26f72d 100644
--- a/kernel/lib/acpi_lite/include/lib/acpi_lite.h
+++ b/kernel/lib/acpi_lite/include/lib/acpi_lite.h
@@ -93,33 +93,9 @@
 
 
 // MADT table describes processors and interrupt controllers
-template <typename T, typename C>
-static inline zx_status_t acpi_process_madt_entries(const uint8_t search_type, C callback) {
-    const acpi_madt_table *madt = reinterpret_cast<const acpi_madt_table *>(acpi_get_table_by_sig(ACPI_MADT_SIG));
-    if (!madt) {
-        return ZX_ERR_NOT_FOUND;
-    }
-
-    // bytewise array of the same table
-    const uint8_t *madt_array = (const uint8_t *)madt;
-
-    // walk the table off the end of the header, looking for the requested type
-    size_t off = sizeof(*madt);
-    while (off < madt->header.length) {
-        uint8_t type = madt_array[off];
-        uint8_t length = madt_array[off + 1];
-
-        if (type == search_type) {
-            callback(reinterpret_cast<const T *>(&madt_array[off]));
-        }
-
-        off += length;
-    }
-
-    return ZX_OK;
-}
 
 // type 0: local apic
+// #define ACPI_MADT_TYPE_LOCAL_APIC 0
 struct acpi_madt_local_apic_entry {
     acpi_sub_table_header header;
     uint8_t processor_id;
@@ -130,6 +106,7 @@
 #define ACPI_MADT_FLAG_ENABLED 0x1
 
 // type 1: io apic
+// #define ACPI_MADT_TYPE_IO_APIC 1
 struct acpi_madt_io_apic_entry {
     acpi_sub_table_header header;
     uint8_t io_apic_id;
@@ -139,6 +116,7 @@
 } __PACKED;
 
 // type 2: interrupt source override
+// #define ACPI_MADT_TYPE_INT_SOURCE_OVERRIDE 2
 struct acpi_madt_int_source_override_entry {
     acpi_sub_table_header header;
     uint8_t bus;
@@ -157,20 +135,6 @@
 #define ACPI_MADT_FLAG_TRIGGER_LEVEL        0b1100
 #define ACPI_MADT_FLAG_TRIGGER_MASK         0b1100
 
-using MadtEntryCallback = fbl::Function<void(const void* ptr)>;
+// A routine to iterate over all the MADT entries of a particular type via a callback
+using MadtEntryCallback = fbl::Function<void(const void* entry, size_t entry_len)>;
 zx_status_t acpi_process_madt_entries_etc(const uint8_t search_type, const MadtEntryCallback&);
-
-template <typename C>
-static inline zx_status_t acpi_process_madt_local_apic_entries(C callback) {
-    return acpi_process_madt_entries<acpi_madt_local_apic_entry, C>(0, callback);
-}
-
-template <typename C>
-static inline zx_status_t acpi_process_madt_io_apic_entries(C callback) {
-    return acpi_process_madt_entries<acpi_madt_io_apic_entry, C>(1, callback);
-}
-
-template <typename C>
-static inline zx_status_t acpi_process_madt_int_source_override_entries(C callback) {
-    return acpi_process_madt_entries<acpi_madt_int_source_override_entry, C>(2, callback);
-}
diff --git a/kernel/platform/pc/acpi.cpp b/kernel/platform/pc/acpi.cpp
index ada6380..0f72ff2 100644
--- a/kernel/platform/pc/acpi.cpp
+++ b/kernel/platform/pc/acpi.cpp
@@ -98,7 +98,10 @@
 
     // for every local apic entry that is enabled, remember the apic id
     uint32_t count = 0;
-    acpi_process_madt_local_apic_entries([apic_ids, &count, len](const acpi_madt_local_apic_entry *entry) {
+    acpi_process_madt_entries_etc(ACPI_MADT_TYPE_LOCAL_APIC,
+            [apic_ids, &count, len](const void* _entry, size_t entry_len) {
+        auto entry = static_cast<const acpi_madt_local_apic_entry*>(_entry);
+
         LTRACEF("MADT entry %p: processor id %d apic id %d flags %#x\n",
                 entry, entry->processor_id, entry->apic_id, entry->flags);
 
@@ -138,7 +141,10 @@
 
     // for every io apic entry, remember some information
     uint32_t count = 0;
-    acpi_process_madt_io_apic_entries([io_apics, &count, len](const acpi_madt_io_apic_entry *entry) {
+    acpi_process_madt_entries_etc(ACPI_MADT_TYPE_IO_APIC,
+            [io_apics, &count, len](const void* _entry, size_t entry_len) {
+        auto entry = static_cast<const acpi_madt_io_apic_entry*>(_entry);
+
         LTRACEF("MADT entry %p: apic id %d address %#x irq base %u\n",
                 entry, entry->io_apic_id, entry->io_apic_address, entry->global_system_interrupt_base);
 
@@ -174,7 +180,10 @@
     }
 
     uint32_t count = 0;
-    acpi_process_madt_int_source_override_entries([isos, &count, len](const acpi_madt_int_source_override_entry *entry) {
+    acpi_process_madt_entries_etc(ACPI_MADT_TYPE_INTERRUPT_OVERRIDE,
+            [isos, &count, len](const void *_entry, size_t entry_len) {
+        auto entry = static_cast<const acpi_madt_int_source_override_entry*>(_entry);
+
         LTRACEF("MADT entry %p: bus %d source %d gsi %u flags %#x\n",
                 entry, entry->bus, entry->source, entry->global_sys_interrupt, entry->flags);
         if (isos != NULL && count < len) {