[Feature]add MT2731_MP2_MR2_SVN388 baseline version

Change-Id: Ief04314834b31e27effab435d3ca8ba33b499059
diff --git a/src/bsp/lk/top/init.c b/src/bsp/lk/top/init.c
new file mode 100644
index 0000000..72d33ce
--- /dev/null
+++ b/src/bsp/lk/top/init.c
@@ -0,0 +1,134 @@
+/*
+ * Copyright (c) 2013 Travis Geiselbrecht
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files
+ * (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge,
+ * publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+/*
+ * Global init hook mechanism. Allows code anywhere in the system to define
+ * a init hook that is called at increasing init levels as the system is
+ * initialized.
+ */
+#include <arch/ops.h>
+#include <lk/init.h>
+
+#include <assert.h>
+#include <compiler.h>
+#include <debug.h>
+#include <trace.h>
+
+#define LOCAL_TRACE 0
+#define TRACE_INIT (LK_DEBUGLEVEL >= 2)
+#ifndef EARLIEST_TRACE_LEVEL
+#define EARLIEST_TRACE_LEVEL LK_INIT_LEVEL_TARGET_EARLY
+#endif
+
+extern const struct lk_init_struct __lk_init[];
+extern const struct lk_init_struct __lk_init_end[];
+
+void lk_init_level(enum lk_init_flags required_flag, uint start_level, uint stop_level)
+{
+    LTRACEF("flags %#x, start_level %#x, stop_level %#x\n",
+            required_flag, start_level, stop_level);
+
+    ASSERT(start_level > 0);
+    uint last_called_level = start_level - 1;
+    const struct lk_init_struct *last = NULL;
+    for (;;) {
+        /* search for the lowest uncalled hook to call */
+        LTRACEF("last %p, last_called_level %#x\n", last, last_called_level);
+
+        const struct lk_init_struct *found = NULL;
+        bool seen_last = false;
+        for (const struct lk_init_struct *ptr = __lk_init; ptr != __lk_init_end; ptr++) {
+            LTRACEF("looking at %p (%s) level %#x, flags %#x, seen_last %d\n", ptr, ptr->name, ptr->level, ptr->flags, seen_last);
+
+            if (ptr == last)
+                seen_last = true;
+
+            /* reject the easy ones */
+            if (!(ptr->flags & required_flag))
+                continue;
+            if (ptr->level > stop_level)
+                continue;
+            if (ptr->level < last_called_level)
+                continue;
+            if (found && found->level <= ptr->level)
+                continue;
+
+            /* keep the lowest one we haven't called yet */
+            if (ptr->level >= start_level && ptr->level > last_called_level) {
+                found = ptr;
+                continue;
+            }
+
+            /* if we're at the same level as the last one we called and we've
+             * already passed over it this time around, we can mark this one
+             * and early terminate the loop.
+             */
+            if (ptr->level == last_called_level && ptr != last && seen_last) {
+                found = ptr;
+                break;
+            }
+        }
+
+        if (!found)
+            break;
+
+#if TRACE_INIT
+        if (found->level >= EARLIEST_TRACE_LEVEL) {
+            printf("INIT: cpu %d, calling hook %p (%s) at level %#x, flags %#x\n",
+                   arch_curr_cpu_num(), found->hook, found->name, found->level, found->flags);
+        }
+#endif
+        found->hook(found->level);
+        last_called_level = found->level;
+        last = found;
+    }
+}
+
+#if 0
+void test_hook(uint level)
+{
+    LTRACEF("level %#x\n", level);
+}
+void test_hook1(uint level)
+{
+    LTRACEF("level %#x\n", level);
+}
+void test_hook1a(uint level)
+{
+    LTRACEF("level %#x\n", level);
+}
+void test_hook1b(uint level)
+{
+    LTRACEF("level %#x\n", level);
+}
+void test_hook2(uint level)
+{
+    LTRACEF("level %#x\n", level);
+}
+
+LK_INIT_HOOK(test, test_hook, 1);
+LK_INIT_HOOK(test1, test_hook1, 1);
+LK_INIT_HOOK(test2, test_hook2, 2);
+LK_INIT_HOOK(test1a, test_hook1a, 1);
+LK_INIT_HOOK(test1b, test_hook1b, 1);
+#endif
diff --git a/src/bsp/lk/top/init.ld b/src/bsp/lk/top/init.ld
new file mode 100644
index 0000000..89a35b2
--- /dev/null
+++ b/src/bsp/lk/top/init.ld
@@ -0,0 +1,8 @@
+SECTIONS {
+    .lk_init : {
+        __lk_init = .;
+        KEEP (*(.lk_init))
+        __lk_init_end = .;
+    }
+}
+INSERT AFTER .rodata;
diff --git a/src/bsp/lk/top/main.c b/src/bsp/lk/top/main.c
new file mode 100644
index 0000000..36b5a31
--- /dev/null
+++ b/src/bsp/lk/top/main.c
@@ -0,0 +1,195 @@
+/*
+ * Copyright (c) 2013-2015 Travis Geiselbrecht
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files
+ * (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge,
+ * publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+/*
+ * Main entry point to the OS. Initializes modules in order and creates
+ * the default thread.
+ */
+#include <compiler.h>
+#include <debug.h>
+#include <string.h>
+#include <app.h>
+#include <arch.h>
+#include <platform.h>
+#include <target.h>
+#include <lib/heap.h>
+#include <kernel/mutex.h>
+#include <kernel/novm.h>
+#include <kernel/thread.h>
+#include <lk/init.h>
+#include <lk/main.h>
+
+/* saved boot arguments from whoever loaded the system */
+ulong lk_boot_args[4];
+
+extern void (*__ctor_list[])(void);
+extern void (*__ctor_end[])(void);
+extern int __bss_start;
+extern int _end;
+
+#if WITH_SMP
+static thread_t *secondary_bootstrap_threads[SMP_MAX_CPUS - 1];
+static uint secondary_bootstrap_thread_count;
+#endif
+
+static int bootstrap2(void *arg);
+
+extern void kernel_init(void);
+
+static void call_constructors(void)
+{
+    void (**ctor)(void);
+
+    for (ctor = __ctor_list; ctor != __ctor_end; ctor++)
+        (*ctor)();
+}
+
+/* called from arch code */
+void lk_main(ulong arg0, ulong arg1, ulong arg2, ulong arg3)
+{
+    // save the boot args
+    lk_boot_args[0] = arg0;
+    lk_boot_args[1] = arg1;
+    lk_boot_args[2] = arg2;
+    lk_boot_args[3] = arg3;
+
+    // get us into some sort of thread context
+    thread_init_early();
+
+    // early arch stuff
+    lk_primary_cpu_init_level(LK_INIT_LEVEL_EARLIEST, LK_INIT_LEVEL_ARCH_EARLY - 1);
+    arch_early_init();
+
+    // do any super early platform initialization
+    lk_primary_cpu_init_level(LK_INIT_LEVEL_ARCH_EARLY, LK_INIT_LEVEL_PLATFORM_EARLY - 1);
+    platform_early_init();
+
+    // do any super early target initialization
+    lk_primary_cpu_init_level(LK_INIT_LEVEL_PLATFORM_EARLY, LK_INIT_LEVEL_TARGET_EARLY - 1);
+    target_early_init();
+
+#if WITH_SMP
+    dprintf(INFO, "\nwelcome to lk/MP\n\n");
+#else
+    dprintf(INFO, "\nwelcome to lk\n\n");
+#endif
+    dprintf(INFO, "boot args 0x%lx 0x%lx 0x%lx 0x%lx\n",
+            lk_boot_args[0], lk_boot_args[1], lk_boot_args[2], lk_boot_args[3]);
+
+    // bring up the kernel heap
+    lk_primary_cpu_init_level(LK_INIT_LEVEL_TARGET_EARLY, LK_INIT_LEVEL_HEAP - 1);
+    dprintf(SPEW, "initializing heap\n");
+    heap_init();
+
+    // deal with any static constructors
+    dprintf(SPEW, "calling constructors\n");
+    call_constructors();
+
+    // initialize the kernel
+    lk_primary_cpu_init_level(LK_INIT_LEVEL_HEAP, LK_INIT_LEVEL_KERNEL - 1);
+    kernel_init();
+
+    lk_primary_cpu_init_level(LK_INIT_LEVEL_KERNEL, LK_INIT_LEVEL_THREADING - 1);
+
+    // create a thread to complete system initialization
+    dprintf(SPEW, "creating bootstrap completion thread\n");
+    thread_t *t = thread_create("bootstrap2", &bootstrap2, NULL, DEFAULT_PRIORITY, DEFAULT_STACK_SIZE);
+    thread_set_pinned_cpu(t, 0);
+    thread_detach(t);
+    thread_resume(t);
+
+    // become the idle thread and enable interrupts to start the scheduler
+    thread_become_idle();
+}
+
+static int bootstrap2(void *arg)
+{
+    dprintf(SPEW, "top of bootstrap2()\n");
+
+    lk_primary_cpu_init_level(LK_INIT_LEVEL_THREADING, LK_INIT_LEVEL_ARCH - 1);
+    arch_init();
+
+    // initialize the rest of the platform
+    dprintf(SPEW, "initializing platform\n");
+    lk_primary_cpu_init_level(LK_INIT_LEVEL_ARCH, LK_INIT_LEVEL_PLATFORM - 1);
+    platform_init();
+
+    // initialize the target
+    dprintf(SPEW, "initializing target\n");
+    lk_primary_cpu_init_level(LK_INIT_LEVEL_PLATFORM, LK_INIT_LEVEL_TARGET - 1);
+    target_init();
+
+    dprintf(SPEW, "calling apps_init()\n");
+    lk_primary_cpu_init_level(LK_INIT_LEVEL_TARGET, LK_INIT_LEVEL_APPS - 1);
+    apps_init();
+
+    lk_primary_cpu_init_level(LK_INIT_LEVEL_APPS, LK_INIT_LEVEL_LAST);
+
+    return 0;
+}
+
+#if WITH_SMP
+void lk_secondary_cpu_entry(void)
+{
+    uint cpu = arch_curr_cpu_num();
+
+    if (cpu > secondary_bootstrap_thread_count) {
+        dprintf(CRITICAL, "Invalid secondary cpu num %d, SMP_MAX_CPUS %d, secondary_bootstrap_thread_count %d\n",
+                cpu, SMP_MAX_CPUS, secondary_bootstrap_thread_count);
+        return;
+    }
+
+    thread_secondary_cpu_init_early();
+    thread_resume(secondary_bootstrap_threads[cpu - 1]);
+
+    dprintf(SPEW, "entering scheduler on cpu %d\n", cpu);
+    thread_secondary_cpu_entry();
+}
+
+static int secondary_cpu_bootstrap2(void *arg)
+{
+    /* secondary cpu initialize from threading level up. 0 to threading was handled in arch */
+    lk_init_level(LK_INIT_FLAG_SECONDARY_CPUS, LK_INIT_LEVEL_THREADING, LK_INIT_LEVEL_LAST);
+
+    return 0;
+}
+
+void lk_init_secondary_cpus(uint secondary_cpu_count)
+{
+    if (secondary_cpu_count >= SMP_MAX_CPUS) {
+        dprintf(CRITICAL, "Invalid secondary_cpu_count %d, SMP_MAX_CPUS %d\n",
+                secondary_cpu_count, SMP_MAX_CPUS);
+        secondary_cpu_count = SMP_MAX_CPUS - 1;
+    }
+    for (uint i = 0; i < secondary_cpu_count; i++) {
+        dprintf(SPEW, "creating bootstrap completion thread for cpu %d\n", i + 1);
+        thread_t *t = thread_create("secondarybootstrap2",
+                                    &secondary_cpu_bootstrap2, NULL,
+                                    DEFAULT_PRIORITY, DEFAULT_STACK_SIZE);
+        t->pinned_cpu = i + 1;
+        thread_detach(t);
+        secondary_bootstrap_threads[i] = t;
+    }
+    secondary_bootstrap_thread_count = secondary_cpu_count;
+}
+#endif
diff --git a/src/bsp/lk/top/rules.mk b/src/bsp/lk/top/rules.mk
new file mode 100644
index 0000000..746edb5
--- /dev/null
+++ b/src/bsp/lk/top/rules.mk
@@ -0,0 +1,18 @@
+LOCAL_DIR := $(GET_LOCAL_DIR)
+
+MODULE := $(LOCAL_DIR)
+
+MODULE_DEPS := \
+	platform \
+	target \
+	app \
+	dev \
+	kernel
+
+MODULE_SRCS := \
+	$(LOCAL_DIR)/init.c \
+	$(LOCAL_DIR)/main.c \
+
+EXTRA_LINKER_SCRIPTS += $(LOCAL_DIR)/init.ld
+
+include make/module.mk