Naman Govil (namangov(a)gmail.com) just uploaded a new patch set to gerrit, which you can find at http://review.coreboot.org/10527
-gerrit
commit fdf9ec2941d99bc2afe39bdb8845ff1571e4a0a8
Author: Naman Govil <namangov(a)gmail.com>
Date: Wed Jul 15 02:51:36 2015 -0400
armv8 : coreboot for qemu aarch64 #Work in progress#
This patchset aims to add a new mainboard
(emulation) for arm64. By the
end of this work, we will have coreboot running
on a qemu-system-aarch64.
Change-Id: I5550dcaae9981908e0c3bf6961206a70bebac5d1
Signed-off-by: Naman Govil <namangov(a)gmail.com>
---
src/arch/arm64/armv8/bootblock_simple.c | 2 +-
src/arch/arm64/c_entry.c | 22 ++-
src/arch/arm64/cpu.c | 3 +-
src/arch/arm64/cpu.h | 204 +++++++++++++++++++++
src/arch/arm64/include/arch/cpu.h | 204 +++++++++++++++++++++
src/arch/arm64/include/armv8/arch/secmon.h | 4 +-
src/cpu/armltd/armv8/Kconfig | 11 ++
src/mainboard/emulation/qemu-armv8/Kconfig | 57 ++++++
src/mainboard/emulation/qemu-armv8/Kconfig.name | 2 +
src/mainboard/emulation/qemu-armv8/Makefile.inc | 40 ++++
src/mainboard/emulation/qemu-armv8/board_info.txt | 3 +
.../emulation/qemu-armv8/bootblock-common.h | 19 ++
src/mainboard/emulation/qemu-armv8/bootblock.c | 22 +++
src/mainboard/emulation/qemu-armv8/cbmem.c | 25 +++
src/mainboard/emulation/qemu-armv8/cpu_lib.S | 29 +++
src/mainboard/emulation/qemu-armv8/devicetree.cb | 20 ++
src/mainboard/emulation/qemu-armv8/mainboard.c | 27 +++
src/mainboard/emulation/qemu-armv8/media.c | 25 +++
src/mainboard/emulation/qemu-armv8/memlayout.ld | 48 +++++
src/mainboard/emulation/qemu-armv8/romstage.c | 24 +++
src/mainboard/emulation/qemu-armv8/timer.c | 27 +++
src/mainboard/emulation/qemu-armv8/uart.c | 24 +++
22 files changed, 829 insertions(+), 13 deletions(-)
diff --git a/src/arch/arm64/armv8/bootblock_simple.c b/src/arch/arm64/armv8/bootblock_simple.c
index c93a100..681bbf9 100644
--- a/src/arch/arm64/armv8/bootblock_simple.c
+++ b/src/arch/arm64/armv8/bootblock_simple.c
@@ -51,7 +51,7 @@ void main(void)
*/
if (boot_cpu()) {
- bootblock_cpu_init();
+ //bootblock_cpu_init();
bootblock_mainboard_init();
}
diff --git a/src/arch/arm64/c_entry.c b/src/arch/arm64/c_entry.c
index 5970405..3f672a3 100644
--- a/src/arch/arm64/c_entry.c
+++ b/src/arch/arm64/c_entry.c
@@ -63,22 +63,26 @@ static void arm64_init(void)
main();
}
-static void secondary_cpu_start(void)
-{
-#ifndef __PRE_RAM__
- mmu_enable();
- exception_hwinit();
+//static void secondary_cpu_start(void)
+//{
+//#ifndef __PRE_RAM__
+// mmu_enable();
+// exception_hwinit();
/* This will never return. */
- arch_secondary_cpu_init();
-#endif
-}
+// arch_secondary_cpu_init();
+//#endif
+//}
/*
* This variable holds entry point for CPUs starting up. The first
* element is the BSP path, and the second is the non-BSP path.
*/
-void (*c_entry[2])(void) = { &arm64_init, &arch_secondary_cpu_init };
+#ifdef __PRE_RAM__
+void (*c_entry[2])(void) = { &arm64_init, NULL};
+#else
+void (*c_entry[2])(void) = { &arm64_init, &arch_secondary_cpu_init};
+#endif
void *prepare_secondary_cpu_startup(void)
{
diff --git a/src/arch/arm64/cpu.c b/src/arch/arm64/cpu.c
index 3cf73ae..1026434 100644
--- a/src/arch/arm64/cpu.c
+++ b/src/arch/arm64/cpu.c
@@ -219,8 +219,9 @@ void arch_cpu_wait_for_action(void)
action_queue_complete(q, orig);
}
}
-
+#ifdef __PRE_RAM__
int boot_cpu(void)
{
return cpu_is_bsp();
}
+#endif
diff --git a/src/arch/arm64/cpu.h b/src/arch/arm64/cpu.h
new file mode 100644
index 0000000..ae16fa1
--- /dev/null
+++ b/src/arch/arm64/cpu.h
@@ -0,0 +1,204 @@
+/*
+ * This file is part of the coreboot project.
+ *
+ * Copyright 2014 Google Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc.
+ */
+
+#ifndef __ARCH_CPU_H__
+#define __ARCH_CPU_H__
+
+#define asmlinkage
+
+#if !defined(__PRE_RAM__)
+#include <arch/barrier.h>
+#include <arch/mpidr.h>
+#include <device/device.h>
+
+enum {
+ CPU_ID_END = 0x00000000,
+};
+
+struct cpu_device_id {
+ uint32_t midr;
+};
+
+struct cpu_driver {
+ /* This is excessive as init() is the only one called. */
+ struct device_operations *ops;
+ const struct cpu_device_id *id_table;
+};
+
+/* Action to run. */
+struct cpu_action {
+ void (*run)(void *arg);
+ void *arg;
+};
+
+/*
+ * Actions are queued to 'todo'. When picked up 'todo' is cleared. The
+ * 'completed' field is set to the original 'todo' value when the action
+ * is complete.
+ */
+struct cpu_action_queue {
+ struct cpu_action *todo;
+ struct cpu_action *completed;
+};
+
+struct cpu_info {
+ device_t cpu;
+ struct cpu_action_queue action_queue;
+ unsigned int online;
+ /* Current assumption is that id matches smp_processor_id(). */
+ unsigned int id;
+ uint64_t mpidr;
+};
+
+/* Obtain cpu_info for current executing CPU. */
+struct cpu_info *cpu_info(void);
+
+extern struct cpu_info *bsp_cpu_info;
+extern struct cpu_info cpu_infos[CONFIG_MAX_CPUS];
+
+static inline struct cpu_info *cpu_info_for_cpu(unsigned int id)
+{
+ return &cpu_infos[id];
+}
+
+/* Ran only by BSP at initial boot strapping. */
+static inline void cpu_set_bsp(void)
+{
+ bsp_cpu_info = cpu_info();
+}
+
+static inline int cpu_is_bsp(void)
+{
+ return cpu_info() == bsp_cpu_info;
+}
+
+static inline int cpu_online(struct cpu_info *ci)
+{
+ return load_acquire(&ci->online) != 0;
+}
+
+static inline void cpu_mark_online(struct cpu_info *ci)
+{
+ ci->mpidr = read_affinity_mpidr();
+ store_release(&ci->online, 1);
+}
+
+/* Provide number of CPUs online. */
+size_t cpus_online(void);
+
+/* Control routines for starting CPUs. */
+struct cpu_control_ops {
+ /* Return the maximum number of CPUs supported. */
+ size_t (*total_cpus)(void);
+ /*
+ * Start the requested CPU and have it start running entry().
+ * Returns 0 on success, < 0 on error.
+ */
+ int (*start_cpu)(unsigned int id, void (*entry)(void));
+};
+
+/*
+ * Initialize all DEVICE_PATH_CPUS under the DEVICE_PATH_CPU_CLUSTER cluster.
+ * type DEVICE_PATH_CPUS. Start up is controlled by cntrl_ops.
+ */
+void arch_initialize_cpus(device_t cluster, struct cpu_control_ops *cntrl_ops);
+
+/*
+ * Run cpu_action returning < 0 on error, 0 on success. There are synchronous
+ * and asynchronous methods. Both cases ensure the action has been picked up
+ * by the target cpu. The synchronous variants will wait for the action to
+ * be completed before returning.
+ *
+ * Though the current implementation allows queuing actions on the main cpu,
+ * the main cpu doesn't process its own queue.
+ */
+int arch_run_on_cpu(unsigned int cpu, struct cpu_action *action);
+int arch_run_on_all_cpus(struct cpu_action *action);
+int arch_run_on_all_cpus_but_self(struct cpu_action *action);
+int arch_run_on_cpu_async(unsigned int cpu, struct cpu_action *action);
+int arch_run_on_all_cpus_async(struct cpu_action *action);
+int arch_run_on_all_cpus_but_self_async(struct cpu_action *action);
+
+/* Wait for actions to be perfomed. */
+void arch_cpu_wait_for_action(void);
+
+#endif /* !__PRE_RAM__ */
+
+/*
+ * Returns logical cpu in range [0:MAX_CPUS). SoC should define this.
+ * Additionally, this is needed early in arm64 init so it should not
+ * rely on a stack. Standard clobber list is fair game: x0-x7 and x0
+ * returns the logical cpu number.
+ */
+unsigned int smp_processor_id(void);
+
+/*
+ * Stages and rmodules have 2 entry points: BSP and non-BSP. Provided
+ * a pointer the correct non-BSP entry point will be returned. The
+ * first instruction is for BSP and the 2nd is for non-BSP. Instructions
+ * are all 32-bit on arm64.
+ */
+static inline void *secondary_entry_point(void *e)
+{
+ uintptr_t nonbsp = (uintptr_t)e;
+
+ return (void *)(nonbsp + sizeof(uint32_t));
+}
+
+/*
+ * The arm64_cpu_startup() initializes a CPU's exception stack and regular
+ * stack as well initializing the C environment for the processor. It
+ * calls into the array of function pointers at symbol c_entry depending
+ * on BSP state. Note that arm64_cpu_startup contains secondary entry
+ * point which can be obtained by secondary_entry_point().
+ */
+void arm64_cpu_startup(void);
+
+/*
+ * The arm64_cpu_startup_resume() initializes a CPU's exception stack and
+ * regular stack as well initializing the C environment for the processor. It
+ * calls into the array of function pointers at symbol c_entry depending
+ * on BSP state. Note that arm64_cpu_startup contains secondary entry
+ * point which can be obtained by secondary_entry_point().
+ * Additionally, it also restores saved register data and enables MMU, caches
+ * and exceptions before jumping to C environment for both BSP and non-BSP CPUs.
+ */
+void arm64_cpu_startup_resume(void);
+
+/*
+ * The arm64_arch_timer_init() initializes the per CPU's cntfrq register of
+ * ARM arch timer.
+ */
+void arm64_arch_timer_init(void);
+
+/*
+ * The cortex_a57_cpu_power_down sequence as per A57/A53/A72 TRM.
+ * L2 flush by HW(0) or SW(1), if system/HW driven L2 flush is supported.
+ */
+#define NO_L2_FLUSH 0
+#define L2_FLUSH_HW 0
+#define L2_FLUSH_SW 1
+
+#if IS_ENABLED(CONFIG_ARCH_ARM64_CORTEX_A57_POWER_DOWN_SUPPORT)
+void cortex_a57_cpu_power_down(int l2_flush);
+#else
+static inline void cortex_a57_cpu_power_down(int l2_flush) {}
+#endif
+
+#endif /* __ARCH_CPU_H__ */
diff --git a/src/arch/arm64/include/arch/cpu.h b/src/arch/arm64/include/arch/cpu.h
new file mode 100644
index 0000000..ae16fa1
--- /dev/null
+++ b/src/arch/arm64/include/arch/cpu.h
@@ -0,0 +1,204 @@
+/*
+ * This file is part of the coreboot project.
+ *
+ * Copyright 2014 Google Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc.
+ */
+
+#ifndef __ARCH_CPU_H__
+#define __ARCH_CPU_H__
+
+#define asmlinkage
+
+#if !defined(__PRE_RAM__)
+#include <arch/barrier.h>
+#include <arch/mpidr.h>
+#include <device/device.h>
+
+enum {
+ CPU_ID_END = 0x00000000,
+};
+
+struct cpu_device_id {
+ uint32_t midr;
+};
+
+struct cpu_driver {
+ /* This is excessive as init() is the only one called. */
+ struct device_operations *ops;
+ const struct cpu_device_id *id_table;
+};
+
+/* Action to run. */
+struct cpu_action {
+ void (*run)(void *arg);
+ void *arg;
+};
+
+/*
+ * Actions are queued to 'todo'. When picked up 'todo' is cleared. The
+ * 'completed' field is set to the original 'todo' value when the action
+ * is complete.
+ */
+struct cpu_action_queue {
+ struct cpu_action *todo;
+ struct cpu_action *completed;
+};
+
+struct cpu_info {
+ device_t cpu;
+ struct cpu_action_queue action_queue;
+ unsigned int online;
+ /* Current assumption is that id matches smp_processor_id(). */
+ unsigned int id;
+ uint64_t mpidr;
+};
+
+/* Obtain cpu_info for current executing CPU. */
+struct cpu_info *cpu_info(void);
+
+extern struct cpu_info *bsp_cpu_info;
+extern struct cpu_info cpu_infos[CONFIG_MAX_CPUS];
+
+static inline struct cpu_info *cpu_info_for_cpu(unsigned int id)
+{
+ return &cpu_infos[id];
+}
+
+/* Ran only by BSP at initial boot strapping. */
+static inline void cpu_set_bsp(void)
+{
+ bsp_cpu_info = cpu_info();
+}
+
+static inline int cpu_is_bsp(void)
+{
+ return cpu_info() == bsp_cpu_info;
+}
+
+static inline int cpu_online(struct cpu_info *ci)
+{
+ return load_acquire(&ci->online) != 0;
+}
+
+static inline void cpu_mark_online(struct cpu_info *ci)
+{
+ ci->mpidr = read_affinity_mpidr();
+ store_release(&ci->online, 1);
+}
+
+/* Provide number of CPUs online. */
+size_t cpus_online(void);
+
+/* Control routines for starting CPUs. */
+struct cpu_control_ops {
+ /* Return the maximum number of CPUs supported. */
+ size_t (*total_cpus)(void);
+ /*
+ * Start the requested CPU and have it start running entry().
+ * Returns 0 on success, < 0 on error.
+ */
+ int (*start_cpu)(unsigned int id, void (*entry)(void));
+};
+
+/*
+ * Initialize all DEVICE_PATH_CPUS under the DEVICE_PATH_CPU_CLUSTER cluster.
+ * type DEVICE_PATH_CPUS. Start up is controlled by cntrl_ops.
+ */
+void arch_initialize_cpus(device_t cluster, struct cpu_control_ops *cntrl_ops);
+
+/*
+ * Run cpu_action returning < 0 on error, 0 on success. There are synchronous
+ * and asynchronous methods. Both cases ensure the action has been picked up
+ * by the target cpu. The synchronous variants will wait for the action to
+ * be completed before returning.
+ *
+ * Though the current implementation allows queuing actions on the main cpu,
+ * the main cpu doesn't process its own queue.
+ */
+int arch_run_on_cpu(unsigned int cpu, struct cpu_action *action);
+int arch_run_on_all_cpus(struct cpu_action *action);
+int arch_run_on_all_cpus_but_self(struct cpu_action *action);
+int arch_run_on_cpu_async(unsigned int cpu, struct cpu_action *action);
+int arch_run_on_all_cpus_async(struct cpu_action *action);
+int arch_run_on_all_cpus_but_self_async(struct cpu_action *action);
+
+/* Wait for actions to be perfomed. */
+void arch_cpu_wait_for_action(void);
+
+#endif /* !__PRE_RAM__ */
+
+/*
+ * Returns logical cpu in range [0:MAX_CPUS). SoC should define this.
+ * Additionally, this is needed early in arm64 init so it should not
+ * rely on a stack. Standard clobber list is fair game: x0-x7 and x0
+ * returns the logical cpu number.
+ */
+unsigned int smp_processor_id(void);
+
+/*
+ * Stages and rmodules have 2 entry points: BSP and non-BSP. Provided
+ * a pointer the correct non-BSP entry point will be returned. The
+ * first instruction is for BSP and the 2nd is for non-BSP. Instructions
+ * are all 32-bit on arm64.
+ */
+static inline void *secondary_entry_point(void *e)
+{
+ uintptr_t nonbsp = (uintptr_t)e;
+
+ return (void *)(nonbsp + sizeof(uint32_t));
+}
+
+/*
+ * The arm64_cpu_startup() initializes a CPU's exception stack and regular
+ * stack as well initializing the C environment for the processor. It
+ * calls into the array of function pointers at symbol c_entry depending
+ * on BSP state. Note that arm64_cpu_startup contains secondary entry
+ * point which can be obtained by secondary_entry_point().
+ */
+void arm64_cpu_startup(void);
+
+/*
+ * The arm64_cpu_startup_resume() initializes a CPU's exception stack and
+ * regular stack as well initializing the C environment for the processor. It
+ * calls into the array of function pointers at symbol c_entry depending
+ * on BSP state. Note that arm64_cpu_startup contains secondary entry
+ * point which can be obtained by secondary_entry_point().
+ * Additionally, it also restores saved register data and enables MMU, caches
+ * and exceptions before jumping to C environment for both BSP and non-BSP CPUs.
+ */
+void arm64_cpu_startup_resume(void);
+
+/*
+ * The arm64_arch_timer_init() initializes the per CPU's cntfrq register of
+ * ARM arch timer.
+ */
+void arm64_arch_timer_init(void);
+
+/*
+ * The cortex_a57_cpu_power_down sequence as per A57/A53/A72 TRM.
+ * L2 flush by HW(0) or SW(1), if system/HW driven L2 flush is supported.
+ */
+#define NO_L2_FLUSH 0
+#define L2_FLUSH_HW 0
+#define L2_FLUSH_SW 1
+
+#if IS_ENABLED(CONFIG_ARCH_ARM64_CORTEX_A57_POWER_DOWN_SUPPORT)
+void cortex_a57_cpu_power_down(int l2_flush);
+#else
+static inline void cortex_a57_cpu_power_down(int l2_flush) {}
+#endif
+
+#endif /* __ARCH_CPU_H__ */
diff --git a/src/arch/arm64/include/armv8/arch/secmon.h b/src/arch/arm64/include/armv8/arch/secmon.h
index f8351b5..6458893 100644
--- a/src/arch/arm64/include/armv8/arch/secmon.h
+++ b/src/arch/arm64/include/armv8/arch/secmon.h
@@ -24,8 +24,8 @@
struct secmon_params {
size_t online_cpus;
- struct cpu_action bsp;
- struct cpu_action secondary;
+ struct cpu_action *bsp;
+ struct cpu_action *secondary;
};
void secmon_run(void (*entry)(void *), void *arg);
diff --git a/src/cpu/armltd/armv8/Kconfig b/src/cpu/armltd/armv8/Kconfig
new file mode 100644
index 0000000..e73980c
--- /dev/null
+++ b/src/cpu/armltd/armv8/Kconfig
@@ -0,0 +1,11 @@
+config CPU_ARMLTD_ARMV8
+ bool
+ select ARCH_BOOTBLOCK_ARMV8_64
+ select ARCH_VERSTAGE_ARMV8_64
+ select ARCH_ROMSTAGE_ARMV8_64
+ select ARCH_RAMSTAGE_ARMV8_64
+ default n
+
+if CPU_ARMLTD_ARMV8
+
+endif
diff --git a/src/mainboard/emulation/qemu-armv8/Kconfig b/src/mainboard/emulation/qemu-armv8/Kconfig
new file mode 100644
index 0000000..f1ef32b
--- /dev/null
+++ b/src/mainboard/emulation/qemu-armv8/Kconfig
@@ -0,0 +1,57 @@
+##
+## This file is part of the coreboot project.
+##
+## Copyright (C) 2015, Naman Govil <namangov(a)gmail.com>
+##
+## This software is licensed under the terms of the GNU General Public
+## License version 2, as published by the Free Software Foundation, and
+## may be copied, distributed, and modified under those terms.
+##
+## This program is distributed in the hope that it will be useful,
+## but WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+## GNU General Public License for more details.
+
+# WRITE THE INSTRUCTIONS TO EXECUTE
+# To execute, do:
+# export QEMU_AUDIO_DRV=none
+# qemu-system-arm -M vexpress-a9 -m 1024M -nographic -kernel build/coreboot.rom
+
+if BOARD_EMULATION_QEMU_ARMV8
+
+config BOARD_SPECIFIC_OPTIONS # dummy
+ def_bool y
+ select ARCH_ARM64_CPU_CORTEX_A57
+ select ARCH_ARM64_CORTEX_A57_POWER_DOWN_SUPPORT
+ select DRIVERS_UART_PL011
+ select BOOTBLOCK_CONSOLE
+ select EARLY_CONSOLE
+ select CONSOLE_SERIAL
+ select ARCH_BOOTBLOCK_ARMV8_64
+ select ARCH_VERSTAGE_ARMV8_64
+ select ARCH_ROMSTAGE_ARMV8_64
+ select ARCH_RAMSTAGE_ARMV8_64
+ select BOARD_ROMSIZE_KB_4096
+
+
+config MAINBOARD_DIR
+ string
+ default emulation/qemu-armv8
+
+config MAINBOARD_PART_NUMBER
+ string
+ default "QEMU ARMv8"
+
+config MAX_CPUS
+ int
+ default 2
+
+config MAINBOARD_VENDOR
+ string
+ default "ARM Ltd."
+
+config DRAM_SIZE_MB
+ int
+ default 1024
+
+endif # BOARD_EMULATION_QEMU_ARMV8
diff --git a/src/mainboard/emulation/qemu-armv8/Kconfig.name b/src/mainboard/emulation/qemu-armv8/Kconfig.name
new file mode 100644
index 0000000..bb3a0e4
--- /dev/null
+++ b/src/mainboard/emulation/qemu-armv8/Kconfig.name
@@ -0,0 +1,2 @@
+config BOARD_EMULATION_QEMU_ARMV8
+ bool "QEMU armv8"
diff --git a/src/mainboard/emulation/qemu-armv8/Makefile.inc b/src/mainboard/emulation/qemu-armv8/Makefile.inc
new file mode 100644
index 0000000..7fa8880
--- /dev/null
+++ b/src/mainboard/emulation/qemu-armv8/Makefile.inc
@@ -0,0 +1,40 @@
+##
+## This file is part of the coreboot project.
+##
+## Copyright (C) 2013 Google Inc.
+## Copyright (C) 2015, Naman Govil <namangov(a)gmail.com>
+##
+## This software is licensed under the terms of the GNU General Public
+## License version 2, as published by the Free Software Foundation, and
+## may be copied, distributed, and modified under those terms.
+##
+## This program is distributed in the hope that it will be useful,
+## but WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+## GNU General Public License for more details.
+
+romstage-y += romstage.c
+
+romstage-y += cbmem.c
+ramstage-y += cbmem.c
+
+bootblock-y += media.c
+romstage-y += media.c
+ramstage-y += media.c
+
+bootblock-y += timer.c
+romstage-y += timer.c
+ramstage-y += timer.c
+
+bootblock-y += uart.c
+romstage-y += uart.c
+ramstage-y += uart.c
+
+bootblock-y += memlayout.ld
+romstage-y += memlayout.ld
+ramstage-y += memlayout.ld
+
+bootblock-y += cpu_lib.S
+romstage-y += cpu_lib.S
+ramstage-y += cpu_lib.S
+
diff --git a/src/mainboard/emulation/qemu-armv8/board_info.txt b/src/mainboard/emulation/qemu-armv8/board_info.txt
new file mode 100644
index 0000000..69c5eb6
--- /dev/null
+++ b/src/mainboard/emulation/qemu-armv8/board_info.txt
@@ -0,0 +1,3 @@
+Board name: QEMU armv8
+Category: emulation
+Board URL: http://fabrice.bellard.free.fr/qemu/
diff --git a/src/mainboard/emulation/qemu-armv8/bootblock-common.h b/src/mainboard/emulation/qemu-armv8/bootblock-common.h
new file mode 100644
index 0000000..09cff10
--- /dev/null
+++ b/src/mainboard/emulation/qemu-armv8/bootblock-common.h
@@ -0,0 +1,19 @@
+/*
+ * This file is part of the coreboot project.
+ *
+ * Copyright (C) 2015 Naman Govil, <namangov(a)gmail.com>
+ *
+ * This software is licensed under the terms of the GNU General Public
+ * License version 2, as published by the Free Software Foundation, and
+ * may be copied, distributed, and modified under those terms.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+#include <stdlib.h>
+#include <console/console.h>
+
+void bootblock_cpu_init(void);
diff --git a/src/mainboard/emulation/qemu-armv8/bootblock.c b/src/mainboard/emulation/qemu-armv8/bootblock.c
new file mode 100644
index 0000000..d198a43
--- /dev/null
+++ b/src/mainboard/emulation/qemu-armv8/bootblock.c
@@ -0,0 +1,22 @@
+/*
+ * This file is part of the coreboot project.
+ *
+ * Copyright (C) 2015 Naman Govil, <namangov(a)gmail.com>
+ *
+ * This software is licensed under the terms of the GNU General Public
+ * License version 2, as published by the Free Software Foundation, and
+ * may be copied, distributed, and modified under those terms.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+#include <stdlib.h>
+#include <console/console.h>
+#include "bootblock-common.h"
+
+void bootblock_cpu_init(void)
+{
+}
diff --git a/src/mainboard/emulation/qemu-armv8/cbmem.c b/src/mainboard/emulation/qemu-armv8/cbmem.c
new file mode 100644
index 0000000..d3a2d6f
--- /dev/null
+++ b/src/mainboard/emulation/qemu-armv8/cbmem.c
@@ -0,0 +1,25 @@
+/*
+ * This file is part of the coreboot project.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc.
+ */
+
+#include <stddef.h>
+#include <cbmem.h>
+#include <symbols.h>
+
+void *cbmem_top(void)
+{
+ return _dram + (CONFIG_DRAM_SIZE_MB << 20);
+}
diff --git a/src/mainboard/emulation/qemu-armv8/cpu_lib.S b/src/mainboard/emulation/qemu-armv8/cpu_lib.S
new file mode 100644
index 0000000..13eed4b
--- /dev/null
+++ b/src/mainboard/emulation/qemu-armv8/cpu_lib.S
@@ -0,0 +1,29 @@
+/*
+ * This file is part of the coreboot project.
+ *
+ * Copyright 2014 Google Inc.
+ * Copyright 2015, Naman Govil <namangov(a)gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc.
+ */
+
+
+.text
+.global smp_processor_id
+smp_processor_id:
+ /* Core 0 and 1 are encoded in the Aff0 (7:0) field of MPIDR_EL1. */
+ /* Multiprocessor Afinity Register (MPIDR) contains CPU ID for a57 */
+ mrs x0, mpidr_el1
+ uxtb w0, w0
+ ret
diff --git a/src/mainboard/emulation/qemu-armv8/devicetree.cb b/src/mainboard/emulation/qemu-armv8/devicetree.cb
new file mode 100644
index 0000000..fa3c03a
--- /dev/null
+++ b/src/mainboard/emulation/qemu-armv8/devicetree.cb
@@ -0,0 +1,20 @@
+##
+## This file is part of the coreboot project.
+##
+## Copyright (C) 2013 Google, Inc.
+##
+## This software is licensed under the terms of the GNU General Public
+## License version 2, as published by the Free Software Foundation, and
+## may be copied, distributed, and modified under those terms.
+##
+## This program is distributed in the hope that it will be useful,
+## but WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+## GNU General Public License for more details.
+
+# TODO fill with Versatile Express board data in QEMU.
+chip cpu/armltd/armv8
+ chip drivers/generic/generic # I2C0 controller
+ device i2c 6 on end # Fake component for testing
+ end
+end
diff --git a/src/mainboard/emulation/qemu-armv8/mainboard.c b/src/mainboard/emulation/qemu-armv8/mainboard.c
new file mode 100644
index 0000000..b73ab9d
--- /dev/null
+++ b/src/mainboard/emulation/qemu-armv8/mainboard.c
@@ -0,0 +1,27 @@
+/*
+ * This file is part of the coreboot project.
+ *
+ * Copyright (C) 2013 Google, Inc.
+ * Copyright (C) 2015 Naman Govil, <namangov(a)gmail.com>
+ *
+ * This software is licensed under the terms of the GNU General Public
+ * License version 2, as published by the Free Software Foundation, and
+ * may be copied, distributed, and modified under those terms.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+#include <console/console.h>
+#include <device/device.h>
+
+static void mainboard_enable(device_t dev)
+{
+ printk(BIOS_INFO, "Enable qemu/armv8 device...\n");
+}
+
+struct chip_operations mainboard_ops = {
+ .enable_dev = mainboard_enable,
+};
diff --git a/src/mainboard/emulation/qemu-armv8/media.c b/src/mainboard/emulation/qemu-armv8/media.c
new file mode 100644
index 0000000..4d4edd8
--- /dev/null
+++ b/src/mainboard/emulation/qemu-armv8/media.c
@@ -0,0 +1,25 @@
+/*
+ * This file is part of the coreboot project.
+ *
+ * Copyright (C) 2013 Google, Inc.
+ *
+ * This software is licensed under the terms of the GNU General Public
+ * License version 2, as published by the Free Software Foundation, and
+ * may be copied, distributed, and modified under those terms.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+#include <boot_device.h>
+
+/* Maps directly to qemu memory mapped space of 0x10000 up to rom size. */
+static const struct mem_region_device boot_dev =
+ MEM_REGION_DEV_INIT((void *)0x10000, CONFIG_ROM_SIZE);
+
+const struct region_device *boot_device_ro(void)
+{
+ return &boot_dev.rdev;
+}
diff --git a/src/mainboard/emulation/qemu-armv8/memlayout.ld b/src/mainboard/emulation/qemu-armv8/memlayout.ld
new file mode 100644
index 0000000..ee8132d
--- /dev/null
+++ b/src/mainboard/emulation/qemu-armv8/memlayout.ld
@@ -0,0 +1,48 @@
+/*
+ * This file is part of the coreboot project.
+ *
+ * Copyright (C) 2014 Google Inc.
+ * Copyright (C )2015, Naman Govil <namangov(a)gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc.
+ */
+
+#include <memlayout.h>
+
+#include <arch/header.ld>
+
+/*
+ * Memory map for qemu armv8:
+ *
+ * 0x0000_0000: jump instruction (required by qemu)
+ * 0x0001_0000: bootblock (entry of kernel / firmware)
+ * 0x0002_0000: romstage, assume up to 128KB in size.
+ * 0x0007_ff00: stack pointer
+ * 0x0010_0000: CBFS header
+ * 0x0011_0000: CBFS data
+ * 0x0100_0000: reserved for ramstage
+ * 0x1000_0000: I/O map address
+ */
+
+SECTIONS
+{
+ /* TODO: does this thing emulate SRAM? */
+
+ BOOTBLOCK(0x10000, 64K)
+ ROMSTAGE(0x20000, 128K)
+ STACK(0x000FC000, 16K)
+
+ DRAM_START(0x01000000)
+ RAMSTAGE(0x01000000, 16M)
+}
diff --git a/src/mainboard/emulation/qemu-armv8/romstage.c b/src/mainboard/emulation/qemu-armv8/romstage.c
new file mode 100644
index 0000000..158989f
--- /dev/null
+++ b/src/mainboard/emulation/qemu-armv8/romstage.c
@@ -0,0 +1,24 @@
+/*
+ * This file is part of the coreboot project.
+ *
+ * Copyright (C) 2013 Google, Inc.
+ * Copyright (C) 2015, Naman Govil <namangov(a)gmail.com>
+ *
+ * This software is licensed under the terms of the GNU General Public
+ * License version 2, as published by the Free Software Foundation, and
+ * may be copied, distributed, and modified under those terms.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+#include <console/console.h>
+#include <program_loading.h>
+
+void main(void)
+{
+ console_init();
+ run_ramstage();
+}
diff --git a/src/mainboard/emulation/qemu-armv8/timer.c b/src/mainboard/emulation/qemu-armv8/timer.c
new file mode 100644
index 0000000..b6d048b
--- /dev/null
+++ b/src/mainboard/emulation/qemu-armv8/timer.c
@@ -0,0 +1,27 @@
+/*
+ * This file is part of the coreboot project.
+ *
+ * Copyright (C) 2013 Google, Inc.
+ * Copyright (C) 2015, Naman Govil <namangov(a)gmail.com>
+ *
+ * This software is licensed under the terms of the GNU General Public
+ * License version 2, as published by the Free Software Foundation, and
+ * may be copied, distributed, and modified under those terms.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+void udelay(unsigned int n);
+void udelay(unsigned int n)
+{
+ /* TODO provide delay here. */
+}
+
+int init_timer(void);
+int init_timer(void)
+{
+ return 0;
+}
diff --git a/src/mainboard/emulation/qemu-armv8/uart.c b/src/mainboard/emulation/qemu-armv8/uart.c
new file mode 100644
index 0000000..d751b15
--- /dev/null
+++ b/src/mainboard/emulation/qemu-armv8/uart.c
@@ -0,0 +1,24 @@
+/*
+ * This file is part of the coreboot project.
+ *
+ * Copyright (C) 2015 Naman Govil, <namangov(a)gmail.com>
+ *
+ * This software is licensed under the terms of the GNU General Public
+ * License version 2, as published by the Free Software Foundation, and
+ * may be copied, distributed, and modified under those terms.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+#include <console/console.h>
+#include <console/uart.h>
+
+#define UART0_IO_ADDRESS (0x90000000)
+
+uintptr_t uart_platform_base(int idx)
+{
+ return UART0_IO_ADDRESS;
+}
Naman Govil (namangov(a)gmail.com) just uploaded a new patch set to gerrit, which you can find at http://review.coreboot.org/11371
-gerrit
commit 6c73bbb91a228abeddc45be9bd41f8399ff963d9
Author: Naman Govil <namangov(a)gmail.com>
Date: Thu Aug 27 05:46:45 2015 +0530
Alignment Issue in arm64
The qemu-armv8 was unable to branch and link to appropriate location
(smp_processor_id) due to misalignment by 1 byte.
Change-Id: I38634cc075bfb1b72098b1dc4c9e488531f2c4e8
Signed-off-by: Naman Govil <namangov(a)gmail.com>
---
src/arch/arm64/id.S | 1 +
1 file changed, 1 insertion(+)
diff --git a/src/arch/arm64/id.S b/src/arch/arm64/id.S
index a588f1e..c5e21cb 100644
--- a/src/arch/arm64/id.S
+++ b/src/arch/arm64/id.S
@@ -1,6 +1,7 @@
#include <build.h>
.section ".id", "a", %progbits
+ .align 2
.globl __id_start
__id_start:
Naman Govil (namangov(a)gmail.com) just uploaded a new patch set to gerrit, which you can find at http://review.coreboot.org/11371
-gerrit
commit 31dd427a6bbf7e3a99a253c0597fc641015169c5
Author: Naman Govil <namangov(a)gmail.com>
Date: Thu Aug 27 05:46:45 2015 +0530
Alignment Issue in arm64
The qemu-armv8 was unable to branch and link to appropriate location
(smp_processor_id) due to misalignment by 1 byte.
Change-Id: I38634cc075bfb1b72098b1dc4c9e488531f2c4e8
Signed-off-by: Naman Govil <namangov(a)gmail.com>
---
src/arch/arm64/id.S | 1 +
1 file changed, 1 insertion(+)
diff --git a/src/arch/arm64/id.S b/src/arch/arm64/id.S
index a588f1e..c5e21cb 100644
--- a/src/arch/arm64/id.S
+++ b/src/arch/arm64/id.S
@@ -1,6 +1,7 @@
#include <build.h>
.section ".id", "a", %progbits
+ .align 2
.globl __id_start
__id_start:
the following patch was just integrated into master:
commit 31f0521a996791f842af94f0c561c18fd2aec2ba
Author: Thaminda Edirisooriya <thaminda(a)google.com>
Date: Wed Aug 26 12:22:29 2015 -0700
riscv-trap-handling: Add preliminary trap handling for riscv
RISCV requires a trap handler at the machine stage to deal with
misaligned loads/stores, as well as to deal with calls that a linux
payload will make in its setup. Put required assembly for jumping
into and out of a trap here to be set up by the bootblock in a later
commit.
Change-Id: Ibf6b18e477aaa1c415a31dbeffa50a2470a7ab2e
Signed-off-by: Thaminda Edirisooriya <thaminda(a)google.com>
Reviewed-on: http://review.coreboot.org/11367
Reviewed-by: Ronald G. Minnich <rminnich(a)gmail.com>
Tested-by: build bot (Jenkins)
See http://review.coreboot.org/11367 for details.
-gerrit
Thaminda Edirisooriya (thaminda(a)google.com) just uploaded a new patch set to gerrit, which you can find at http://review.coreboot.org/11367
-gerrit
commit 78401a823bd83eb813f6895d3a2db07a574485cf
Author: Thaminda Edirisooriya <thaminda(a)google.com>
Date: Wed Aug 26 12:22:29 2015 -0700
riscv-trap-handling: Add preliminary trap handling for riscv
RISCV requires a trap handler at the machine stage to deal with
misaligned loads/stores, as well as to deal with calls that a linux
payload will make in its setup. Put required assembly for jumping
into and out of a trap here to be set up by the bootblock in a later
commit.
Change-Id: Ibf6b18e477aaa1c415a31dbeffa50a2470a7ab2e
Signed-off-by: Thaminda Edirisooriya <thaminda(a)google.com>
---
src/arch/riscv/include/bits.h | 57 ++++++++++++++++++
src/arch/riscv/trap_util.S | 136 ++++++++++++++++++++++++++++++++++++++++++
2 files changed, 193 insertions(+)
diff --git a/src/arch/riscv/include/bits.h b/src/arch/riscv/include/bits.h
new file mode 100644
index 0000000..f69c7ec
--- /dev/null
+++ b/src/arch/riscv/include/bits.h
@@ -0,0 +1,57 @@
+/*
+ * Copyright (c) 2013, The Regents of the University of California (Regents).
+ * All Rights Reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the Regents nor the
+ * names of its contributors may be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
+ * SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING
+ * OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF REGENTS HAS
+ * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE. THE SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED
+ * HEREUNDER IS PROVIDED "AS IS". REGENTS HAS NO OBLIGATION TO PROVIDE
+ * MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
+ */
+
+#ifndef _BITS_H
+#define _BITS_H
+
+#define CONST_POPCOUNT2(x) ((((x) >> 0) & 1) + (((x) >> 1) & 1))
+#define CONST_POPCOUNT4(x) (CONST_POPCOUNT2(x) + CONST_POPCOUNT2((x)>>2))
+#define CONST_POPCOUNT8(x) (CONST_POPCOUNT4(x) + CONST_POPCOUNT4((x)>>4))
+#define CONST_POPCOUNT16(x) (CONST_POPCOUNT8(x) + CONST_POPCOUNT8((x)>>8))
+#define CONST_POPCOUNT32(x) (CONST_POPCOUNT16(x) + CONST_POPCOUNT16((x)>>16))
+#define CONST_POPCOUNT64(x) (CONST_POPCOUNT32(x) + CONST_POPCOUNT32((x)>>32))
+#define CONST_POPCOUNT(x) CONST_POPCOUNT64(x)
+
+#define CONST_CTZ2(x) CONST_POPCOUNT2(((x) & -(x))-1)
+#define CONST_CTZ4(x) CONST_POPCOUNT4(((x) & -(x))-1)
+#define CONST_CTZ8(x) CONST_POPCOUNT8(((x) & -(x))-1)
+#define CONST_CTZ16(x) CONST_POPCOUNT16(((x) & -(x))-1)
+#define CONST_CTZ32(x) CONST_POPCOUNT32(((x) & -(x))-1)
+#define CONST_CTZ64(x) CONST_POPCOUNT64(((x) & -(x))-1)
+#define CONST_CTZ(x) CONST_CTZ64(x)
+
+#define STR(x) XSTR(x)
+#define XSTR(x) #x
+
+# define SLL32 sllw
+# define STORE sd
+# define LOAD ld
+# define LOG_REGBYTES 3
+
+#define REGBYTES (1 << LOG_REGBYTES)
+
+#endif
diff --git a/src/arch/riscv/trap_util.S b/src/arch/riscv/trap_util.S
new file mode 100644
index 0000000..08061eb
--- /dev/null
+++ b/src/arch/riscv/trap_util.S
@@ -0,0 +1,136 @@
+/*
+ * Early initialization code for riscv
+ *
+ * Copyright 2015 Google Inc.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; version 2 of
+ * the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc.
+ */
+
+#include <bits.h>
+.macro restore_regs
+ # restore x registers
+ LOAD x1,1*REGBYTES(a0)
+ LOAD x2,2*REGBYTES(a0)
+ LOAD x3,3*REGBYTES(a0)
+ LOAD x4,4*REGBYTES(a0)
+ LOAD x5,5*REGBYTES(a0)
+ LOAD x6,6*REGBYTES(a0)
+ LOAD x7,7*REGBYTES(a0)
+ LOAD x8,8*REGBYTES(a0)
+ LOAD x9,9*REGBYTES(a0)
+ LOAD x11,11*REGBYTES(a0)
+ LOAD x12,12*REGBYTES(a0)
+ LOAD x13,13*REGBYTES(a0)
+ LOAD x14,14*REGBYTES(a0)
+ LOAD x15,15*REGBYTES(a0)
+ LOAD x16,16*REGBYTES(a0)
+ LOAD x17,17*REGBYTES(a0)
+ LOAD x18,18*REGBYTES(a0)
+ LOAD x19,19*REGBYTES(a0)
+ LOAD x20,20*REGBYTES(a0)
+ LOAD x21,21*REGBYTES(a0)
+ LOAD x22,22*REGBYTES(a0)
+ LOAD x23,23*REGBYTES(a0)
+ LOAD x24,24*REGBYTES(a0)
+ LOAD x25,25*REGBYTES(a0)
+ LOAD x26,26*REGBYTES(a0)
+ LOAD x27,27*REGBYTES(a0)
+ LOAD x28,28*REGBYTES(a0)
+ LOAD x29,29*REGBYTES(a0)
+ LOAD x30,30*REGBYTES(a0)
+ LOAD x31,31*REGBYTES(a0)
+ # restore a0 last
+ LOAD x10,10*REGBYTES(a0)
+
+
+ .endm
+.macro save_tf
+ # save gprs
+ STORE x1,1*REGBYTES(x2)
+ STORE x3,3*REGBYTES(x2)
+ STORE x4,4*REGBYTES(x2)
+ STORE x5,5*REGBYTES(x2)
+ STORE x6,6*REGBYTES(x2)
+ STORE x7,7*REGBYTES(x2)
+ STORE x8,8*REGBYTES(x2)
+ STORE x9,9*REGBYTES(x2)
+ STORE x10,10*REGBYTES(x2)
+ STORE x11,11*REGBYTES(x2)
+ STORE x12,12*REGBYTES(x2)
+ STORE x13,13*REGBYTES(x2)
+ STORE x14,14*REGBYTES(x2)
+ STORE x15,15*REGBYTES(x2)
+ STORE x16,16*REGBYTES(x2)
+ STORE x17,17*REGBYTES(x2)
+ STORE x18,18*REGBYTES(x2)
+ STORE x19,19*REGBYTES(x2)
+ STORE x20,20*REGBYTES(x2)
+ STORE x21,21*REGBYTES(x2)
+ STORE x22,22*REGBYTES(x2)
+ STORE x23,23*REGBYTES(x2)
+ STORE x24,24*REGBYTES(x2)
+ STORE x25,25*REGBYTES(x2)
+ STORE x26,26*REGBYTES(x2)
+ STORE x27,27*REGBYTES(x2)
+ STORE x28,28*REGBYTES(x2)
+ STORE x29,29*REGBYTES(x2)
+ STORE x30,30*REGBYTES(x2)
+ STORE x31,31*REGBYTES(x2)
+
+ # get sr, epc, badvaddr, cause
+ csrrw t0,mscratch,x0
+ csrr s0,mstatus
+ csrr t1,mepc
+ csrr t2,mbadaddr
+ csrr t3,mcause
+ STORE t0,2*REGBYTES(x2)
+ STORE s0,32*REGBYTES(x2)
+ STORE t1,33*REGBYTES(x2)
+ STORE t2,34*REGBYTES(x2)
+ STORE t3,35*REGBYTES(x2)
+
+ # get faulting insn, if it wasn't a fetch-related trap
+ li x5,-1
+ STORE x5,36*REGBYTES(x2)
+1:
+ .endm
+
+ .text
+ .global supervisor_trap_entry
+supervisor_trap_entry:
+ csrw mscratch, sp
+ # load in the top of the machine stack
+ la sp, 0x80FFF0
+ 1:addi sp,sp,-320
+ save_tf
+ move a0,sp
+ jal trap_handler
+ .global trap_entry
+trap_entry:
+ csrw mscratch, sp
+ 1:addi sp,sp,-320
+ save_tf_
+ move a0,sp
+ jal trap_handler
+ .global supervisor_call_return
+supervisor_call_return:
+ csrr a0, mscratch
+ restore_regs
+ eret # go back into supervisor call
+ .global machine_call_return
+machine_call_return:
+ csrr a0, mscratch
+ restore_regs
+ eret # go back into machine call
Thaminda Edirisooriya (thaminda(a)google.com) just uploaded a new patch set to gerrit, which you can find at http://review.coreboot.org/11369
-gerrit
commit 981098c2d1087bebf910ba4ea823ebd5bad48de1
Author: Thaminda Edirisooriya <thaminda(a)google.com>
Date: Wed Aug 26 15:28:04 2015 -0700
riscv-virtual-memory: Add virtual memory setup
Execution in supervisor level code in RISCV requires early setup of
virtual memory. Add initialization calls in
src/arch/riscv/virtual_memory.c to implement the required page table
setup, and helper functions to use when jumping to the payload correctly
in riscv.
Change-Id: I46e080e0ee8dc13277d567dcd4bf0f61a4507b76
Signed-off-by: Thaminda Edirisooriya <thaminda(a)google.com>
---
src/arch/riscv/include/vm.h | 74 +++++++++++++++++++++
src/arch/riscv/virtual_memory.c | 142 ++++++++++++++++++++++++++++++++++++++++
2 files changed, 216 insertions(+)
diff --git a/src/arch/riscv/include/vm.h b/src/arch/riscv/include/vm.h
new file mode 100644
index 0000000..f9ffc40
--- /dev/null
+++ b/src/arch/riscv/include/vm.h
@@ -0,0 +1,74 @@
+/*
+ * Copyright (c) 2013, The Regents of the University of California (Regents).
+ * All Rights Reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the Regents nor the
+ * names of its contributors may be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
+ * SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING
+ * OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF REGENTS HAS
+ * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE. THE SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED
+ * HEREUNDER IS PROVIDED "AS IS". REGENTS HAS NO OBLIGATION TO PROVIDE
+ * MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
+ */
+
+#ifndef _VM_H
+#define _VM_H
+
+#include <string.h>
+#include <stdint.h>
+
+#define SUPERPAGE_SIZE ((uintptr_t)(RISCV_PGSIZE << RISCV_PGLEVEL_BITS))
+#define VM_CHOICE VM_SV39
+#define VA_BITS 39
+#define MEGAPAGE_SIZE (SUPERPAGE_SIZE << RISCV_PGLEVEL_BITS)
+
+#define PROT_READ 1
+#define PROT_WRITE 2
+#define PROT_EXEC 4
+
+#define MAP_PRIVATE 0x2
+#define MAP_FIXED 0x10
+#define MAP_ANONYMOUS 0x20
+#define MAP_POPULATE 0x8000
+#define MREMAP_FIXED 0x2
+
+#define EXTRACT_FIELD(val, which) (((val) & (which)) / ((which) & ~((which)-1)))
+#define INSERT_FIELD(val, which, fieldval) (((val) & ~(which)) | ((fieldval) * ((which) & ~((which)-1))))
+
+#define supervisor_paddr_valid(start, length) \
+ ((uintptr_t)(start) >= current.first_user_vaddr + current.bias \
+ && (uintptr_t)(start) + (length) < mem_size \
+ && (uintptr_t)(start) + (length) >= (uintptr_t)(start))
+
+typedef uintptr_t pte_t;
+extern pte_t* root_page_table;
+
+void enter_supervisor(void);
+void initVirtualMemory(void);
+
+size_t pte_ppn(pte_t pte);
+pte_t ptd_create(uintptr_t ppn);
+pte_t pte_create(uintptr_t ppn, int prot, int user);
+
+void walk_page_table(void);
+
+void init_vm(uintptr_t virtMemStart, uintptr_t physMemStart, uintptr_t pageTableStart);
+void mstatus_init(void); // need to setup mstatus so we know we have virtual memory
+
+void flush_tlb(void);
+
+#endif
diff --git a/src/arch/riscv/virtual_memory.c b/src/arch/riscv/virtual_memory.c
new file mode 100644
index 0000000..78405dd
--- /dev/null
+++ b/src/arch/riscv/virtual_memory.c
@@ -0,0 +1,142 @@
+/*
+ * Early initialization code for riscv virtual memory
+ *
+ * Copyright 2015 Google Inc.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; version 2 of
+ * the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc.
+ */
+
+#include <vm.h>
+#include <arch/encoding.h>
+#include <atomic.h>
+#include <stdint.h>
+#include <console/console.h>
+
+pte_t* root_page_table;
+
+void walk_page_table(void) {
+ // TODO: implement a full walk to make sure memory was set up
+ //const size_t pte_per_page = RISCV_PGSIZE/sizeof(void*);
+ pte_t* t = root_page_table;
+ printk(BIOS_DEBUG, "root_page_table: %p\n", t);
+}
+
+void enter_supervisor(void) {
+ // enter supervisor mode
+ asm volatile("la t0, 1f; csrw mepc, t0; eret; 1:" ::: "t0");
+}
+
+void flush_tlb(void)
+{
+ asm volatile("sfence.vm");
+}
+
+size_t pte_ppn(pte_t pte)
+{
+ return pte >> PTE_PPN_SHIFT;
+}
+
+pte_t ptd_create(uintptr_t ppn)
+{
+ return (ppn << PTE_PPN_SHIFT) | PTE_V | PTE_TYPE_TABLE;
+}
+
+pte_t pte_create(uintptr_t ppn, int prot, int user)
+{
+ pte_t pte = (ppn << PTE_PPN_SHIFT) | PTE_V;
+ if (prot & PROT_WRITE) pte |= PTE_TYPE_URW_SRW;
+ if (prot & PROT_EXEC) pte |= PTE_TYPE_URX_SRX;
+ if (!user) pte |= PTE_TYPE_SR;
+ return pte;
+}
+
+void init_vm(uintptr_t virtMemStart, uintptr_t physMemStart, uintptr_t pageTableStart) {
+ pte_t* sbi_pt = (pte_t*) pageTableStart;
+ memset(sbi_pt, 0, RISCV_PGSIZE);
+ // need to leave room for sbi page
+ uintptr_t memorySize = 0x7F000000; // 0xFFF... - 0xFFFFFFFF81000000 - RISCV_PGSIZE
+
+ // middle page table
+ pte_t* middle_pt = (void*)sbi_pt + RISCV_PGSIZE;
+ size_t num_middle_pts = 2; // 3 level page table, 39 bit virtual address space for now
+
+ // root page table
+ pte_t* root_pt = (void*)middle_pt + num_middle_pts * RISCV_PGSIZE;
+ memset(middle_pt, 0, (num_middle_pts + 1) * RISCV_PGSIZE); // 0's out middle_pt and root_pt
+ for (size_t i = 0; i < num_middle_pts; i++)
+ root_pt[(1<<RISCV_PGLEVEL_BITS)-num_middle_pts+i] = ptd_create(((uintptr_t)middle_pt >> RISCV_PGSHIFT) + i);
+
+ // fill the middle page table
+ for (uintptr_t vaddr = virtMemStart, paddr = physMemStart; paddr < memorySize; vaddr += SUPERPAGE_SIZE, paddr += SUPERPAGE_SIZE) {
+ int l2_shift = RISCV_PGLEVEL_BITS + RISCV_PGSHIFT;
+ size_t l2_idx = (virtMemStart >> l2_shift) & ((1 << RISCV_PGLEVEL_BITS)-1);
+ l2_idx += ((vaddr - virtMemStart) >> l2_shift);
+ middle_pt[l2_idx] = pte_create(paddr >> RISCV_PGSHIFT, PROT_READ|PROT_WRITE|PROT_EXEC, 0);
+ }
+
+ // map SBI at top of vaddr space
+ uintptr_t num_sbi_pages = 1; // only need to map a single page for sbi interface
+ uintptr_t sbiStartAddress = 0x2000; // the start of the sbi mapping
+ uintptr_t sbiAddr = sbiStartAddress;
+ for (uintptr_t i = 0; i < num_sbi_pages; i++) {
+ uintptr_t idx = (1 << RISCV_PGLEVEL_BITS) - num_sbi_pages + i;
+ sbi_pt[idx] = pte_create(sbiAddr >> RISCV_PGSHIFT, PROT_READ|PROT_EXEC, 0);
+ sbiAddr += RISCV_PGSIZE;
+ }
+ pte_t* sbi_pte = middle_pt + ((num_middle_pts << RISCV_PGLEVEL_BITS)-1);
+ *sbi_pte = ptd_create((uintptr_t)sbi_pt >> RISCV_PGSHIFT);
+
+ mb();
+ root_page_table = root_pt;
+ write_csr(sptbr, root_pt);
+}
+
+void initVirtualMemory(void) {
+ printk(BIOS_DEBUG, "Initializing virtual memory...\n");
+ uintptr_t physicalStart = 0x1000000; // TODO: Figure out how to grab this from cbfs
+ uintptr_t virtualStart = 0xffffffff81000000;
+ uintptr_t pageTableStart = 0x1f0000;
+ init_vm(virtualStart, physicalStart, pageTableStart);
+ mb();
+ printk(BIOS_DEBUG, "Finished initializing virtual memory, starting walk...\n");
+ walk_page_table();
+}
+
+void mstatus_init(void)
+{
+ // supervisor support is required
+
+ uintptr_t ms = 0;
+ ms = INSERT_FIELD(ms, MSTATUS_PRV, PRV_M);
+ ms = INSERT_FIELD(ms, MSTATUS_PRV1, PRV_S);
+ ms = INSERT_FIELD(ms, MSTATUS_PRV2, PRV_U);
+ ms = INSERT_FIELD(ms, MSTATUS_IE2, 1);
+ ms = INSERT_FIELD(ms, MSTATUS_VM, VM_CHOICE);
+ ms = INSERT_FIELD(ms, MSTATUS_FS, 3);
+ ms = INSERT_FIELD(ms, MSTATUS_XS, 3);
+ write_csr(mstatus, ms);
+ ms = read_csr(mstatus);
+
+ if (EXTRACT_FIELD(ms, MSTATUS_VM) != VM_CHOICE) {
+ printk(BIOS_DEBUG, "we don't have virtual memory...\n");
+ } else {
+ printk(BIOS_DEBUG, "-----------------------------\n");
+ printk(BIOS_DEBUG, "virtual memory status enabled\n");
+ printk(BIOS_DEBUG, "-----------------------------\n");
+ }
+
+ clear_csr(mip, MIP_MSIP);
+ set_csr(mie, MIP_MSIP);
+}
Thaminda Edirisooriya (thaminda(a)google.com) just uploaded a new patch set to gerrit, which you can find at http://review.coreboot.org/11368
-gerrit
commit b4db6e1f50f0602d8f961a63e9225e1d0d47cc7c
Author: Thaminda Edirisooriya <thaminda(a)google.com>
Date: Wed Aug 26 14:54:31 2015 -0700
riscv-trap-handling: Add implementation for trap calls in riscv
RISCV requires the bios/bootloader to set up an interface by which it
can get information about memory, talk to host devices, etc. Put
implementation for spike in
src/mainboard/emulation/spike-riscv/spike_util.c, and
src/arch/riscv/trap_handler.c
Change-Id: Ie1d5f361595e48fa6cc1fac25485ad623ecdc717
Signed-off-by: Thaminda Edirisooriya <thaminda(a)google.com>
---
src/arch/riscv/Makefile.inc | 2 +
src/arch/riscv/include/arch/errno.h | 66 ++++++++
src/arch/riscv/include/arch/exception.h | 22 +++
src/arch/riscv/include/spike_util.h | 17 ++
src/arch/riscv/trap_handler.c | 193 +++++++++++++++++++++++
src/arch/riscv/trap_util.S | 2 +-
src/mainboard/emulation/spike-riscv/spike_util.c | 139 ++++++++++++++++
7 files changed, 440 insertions(+), 1 deletion(-)
diff --git a/src/arch/riscv/Makefile.inc b/src/arch/riscv/Makefile.inc
index 0f3eb0f..c7a2c16 100644
--- a/src/arch/riscv/Makefile.inc
+++ b/src/arch/riscv/Makefile.inc
@@ -29,6 +29,8 @@ riscv_asm_flags =
ifeq ($(CONFIG_ARCH_BOOTBLOCK_RISCV),y)
bootblock-y = bootblock.S stages.c
+bootblock-y += trap_util.S
+bootblock-y += trap_handler.c
bootblock-y += boot.c
bootblock-y += rom_media.c
bootblock-y += \
diff --git a/src/arch/riscv/include/arch/errno.h b/src/arch/riscv/include/arch/errno.h
new file mode 100644
index 0000000..6f80ee5
--- /dev/null
+++ b/src/arch/riscv/include/arch/errno.h
@@ -0,0 +1,66 @@
+/*
+ * Copyright (c) 2013, The Regents of the University of California (Regents).
+ * All Rights Reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the Regents nor the
+ * names of its contributors may be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
+ * SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING
+ * OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF REGENTS HAS
+ * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE. THE SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED
+ * HEREUNDER IS PROVIDED "AS IS". REGENTS HAS NO OBLIGATION TO PROVIDE
+ * MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
+ */
+
+#ifndef _RISCV_ERRNO_BASE_H
+#define _RISCV_ERRNO_BASE_H
+
+#define EPERM 1 /* Operation not permitted */
+#define ENOENT 2 /* No such file or directory */
+#define ESRCH 3 /* No such process */
+#define EINTR 4 /* Interrupted system call */
+#define EIO 5 /* I/O error */
+#define ENXIO 6 /* No such device or address */
+#define E2BIG 7 /* Argument list too long */
+#define ENOEXEC 8 /* Exec format error */
+#define EBADF 9 /* Bad file number */
+#define ECHILD 10 /* No child processes */
+#define EAGAIN 11 /* Try again */
+#define ENOMEM 12 /* Out of memory */
+#define EACCES 13 /* Permission denied */
+#define EFAULT 14 /* Bad address */
+#define ENOTBLK 15 /* Block device required */
+#define EBUSY 16 /* Device or resource busy */
+#define EEXIST 17 /* File exists */
+#define EXDEV 18 /* Cross-device link */
+#define ENODEV 19 /* No such device */
+#define ENOTDIR 20 /* Not a directory */
+#define EISDIR 21 /* Is a directory */
+#define EINVAL 22 /* Invalid argument */
+#define ENFILE 23 /* File table overflow */
+#define EMFILE 24 /* Too many open files */
+#define ENOTTY 25 /* Not a typewriter */
+#define ETXTBSY 26 /* Text file busy */
+#define EFBIG 27 /* File too large */
+#define ENOSPC 28 /* No space left on device */
+#define ESPIPE 29 /* Illegal seek */
+#define EROFS 30 /* Read-only file system */
+#define EMLINK 31 /* Too many links */
+#define EPIPE 32 /* Broken pipe */
+#define EDOM 33 /* Math argument out of domain of func */
+#define ERANGE 34 /* Math result not representable */
+
+#endif
diff --git a/src/arch/riscv/include/arch/exception.h b/src/arch/riscv/include/arch/exception.h
index befab1f..8ed295d 100644
--- a/src/arch/riscv/include/arch/exception.h
+++ b/src/arch/riscv/include/arch/exception.h
@@ -32,8 +32,30 @@
#include <stdint.h>
+typedef struct
+{
+ long gpr[32];
+ long status;
+ long epc;
+ long badvaddr;
+ long cause;
+ long insn;
+} trapframe;
+
+typedef uint32_t insn_t;
+
+typedef struct {
+ uintptr_t error;
+ insn_t insn;
+} insn_fetch_t;
+
static inline void exception_init(void)
{
}
+void trap_handler(trapframe* tf);
+void handleSupervisorCall(trapframe* tf);
+//void handleMisalignedLoad(trapframe *tf);
+void handleMisalignedStore(trapframe *tf);
+
#endif
diff --git a/src/arch/riscv/include/spike_util.h b/src/arch/riscv/include/spike_util.h
index a9d14cc..e9c0300 100644
--- a/src/arch/riscv/include/spike_util.h
+++ b/src/arch/riscv/include/spike_util.h
@@ -40,6 +40,12 @@
#define FROMHOST_DATA(fromhost_value) ((uint64_t)(fromhost_value) << 16 >> 16)
typedef struct {
+ unsigned long base;
+ unsigned long size;
+ unsigned long node_id;
+} memory_block_info;
+
+typedef struct {
unsigned long dev;
unsigned long cmd;
unsigned long data;
@@ -63,11 +69,22 @@ typedef struct {
// hart-local storage, at top of stack
#define HLS() ((hls_t*)(MACHINE_STACK_TOP() - HLS_SIZE))
+#define OTHER_HLS(id) ((hls_t*)((void*)HLS() + RISCV_PGSIZE * ((id) - HLS()->hart_id)))
#define MACHINE_STACK_SIZE RISCV_PGSIZE
+uintptr_t translate_address(uintptr_t vAddr);
+uintptr_t mcall_query_memory(uintptr_t id, memory_block_info *p);
+uintptr_t mcall_hart_id(void);
uintptr_t htif_interrupt(uintptr_t mcause, uintptr_t* regs);
uintptr_t mcall_console_putchar(uint8_t ch);
void testPrint(void);
+uintptr_t mcall_dev_req(sbi_device_message *m);
+uintptr_t mcall_dev_resp(void);
+uintptr_t mcall_set_timer(unsigned long long when);
+uintptr_t mcall_clear_ipi(void);
+uintptr_t mcall_send_ipi(uintptr_t recipient);
+uintptr_t mcall_shutdown(void);
+void hls_init(uint32_t hart_id); // need to call this before launching linux
#endif
diff --git a/src/arch/riscv/trap_handler.c b/src/arch/riscv/trap_handler.c
new file mode 100644
index 0000000..1f20c7f
--- /dev/null
+++ b/src/arch/riscv/trap_handler.c
@@ -0,0 +1,193 @@
+/*
+ * Early initialization code for riscv
+ *
+ * Copyright 2015 Google Inc.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; version 2 of
+ * the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc.
+ */
+
+#include <console/console.h>
+#include <arch/exception.h>
+#include <spike_util.h>
+#include <string.h>
+
+#define HART_ID 0
+#define CONSOLE_PUT 1
+#define SEND_DEVICE_REQUEST 2
+#define RECEIVE_DEVICE_RESPONSE 3
+#define SEND_IPI 4
+#define CLEAR_IPI 5
+#define SHUTDOWN 6
+#define SET_TIMER 7
+#define QUERY_MEMORY 8
+
+int loopBreak2 = 1;
+
+void handleSupervisorCall(trapframe *tf) {
+ uintptr_t call = tf->gpr[17];
+ uintptr_t arg0 = tf->gpr[10];
+ uintptr_t arg1 = tf->gpr[11];
+ uintptr_t returnValue;
+ switch(call) {
+ case HART_ID:
+ printk(BIOS_DEBUG, "Getting hart id...\n");
+ returnValue = mcall_hart_id();
+ break;
+ case CONSOLE_PUT:
+ returnValue = mcall_console_putchar(arg0);
+ break;
+ case SEND_DEVICE_REQUEST:
+ printk(BIOS_DEBUG, "Sending device request...\n");
+ returnValue = mcall_dev_req((sbi_device_message*) arg0);
+ break;
+ case RECEIVE_DEVICE_RESPONSE:
+ printk(BIOS_DEBUG, "Getting device response...\n");
+ returnValue = mcall_dev_resp();
+ break;
+ case SEND_IPI:
+ printk(BIOS_DEBUG, "Sending IPI...\n");
+ returnValue = mcall_send_ipi(arg0);
+ break;
+ case CLEAR_IPI:
+ printk(BIOS_DEBUG, "Clearing IPI...\n");
+ returnValue = mcall_clear_ipi();
+ break;
+ case SHUTDOWN:
+ printk(BIOS_DEBUG, "Shutting down...\n");
+ returnValue = mcall_shutdown();
+ break;
+ case SET_TIMER:
+ printk(BIOS_DEBUG, "Setting timer...\n");
+ returnValue = mcall_set_timer(arg0);
+ break;
+ case QUERY_MEMORY:
+ printk(BIOS_DEBUG, "Querying memory, CPU #%lld...\n", arg0);
+ returnValue = mcall_query_memory(arg0, (memory_block_info*) arg1);
+ break;
+ default:
+ printk(BIOS_DEBUG, "ERROR! Unrecognized system call\n");
+ returnValue = 0;
+ break; // note: system call we do not know how to handle
+ }
+ tf->gpr[10] = returnValue;
+ write_csr(mepc, read_csr(mepc) + 4);
+ asm volatile("j supervisor_call_return");
+}
+
+void trap_handler(trapframe *tf) {
+ write_csr(mscratch, tf);
+ int cause = 0;
+ void* epc = 0;
+ void* badAddr = 0;
+
+ // extract cause
+ asm("csrr t0, mcause");
+ asm("move %0, t0" : "=r"(cause));
+
+ // extract faulting Instruction pc
+ epc = (void*) tf->epc;
+
+ // extract bad address
+ asm("csrr t0, mbadaddr");
+ asm("move %0, t0" : "=r"(badAddr));
+
+ switch(cause) {
+ case 0:
+ printk(BIOS_DEBUG, "Trap: Instruction address misaligned\n");
+ break;
+ case 1:
+ printk(BIOS_DEBUG, "Trap: Instruction access fault\n");
+ printk(BIOS_DEBUG, "Bad instruction pc: %p\n", epc);
+ printk(BIOS_DEBUG, "Address: %p\n", badAddr);
+ break;
+ case 2:
+ printk(BIOS_DEBUG, "Trap: Illegal instruction\n");
+ printk(BIOS_DEBUG, "Bad instruction pc: %p\n", epc);
+ printk(BIOS_DEBUG, "Address: %p\n", badAddr);
+ break;
+ case 3:
+ printk(BIOS_DEBUG, "Trap: Breakpoint\n");
+ break;
+ case 4:
+ printk(BIOS_DEBUG, "Trap: Load address misaligned\n");
+ //handleMisalignedLoad(tf);
+ break;
+ case 5:
+ printk(BIOS_DEBUG, "Trap: Load access fault\n");
+ break;
+ case 6:
+ printk(BIOS_DEBUG, "Trap: Store address misaligned\n");
+ printk(BIOS_DEBUG, "Bad instruction pc: %p\n", epc);
+ printk(BIOS_DEBUG, "Store Address: %p\n", badAddr);
+ handleMisalignedStore(tf);
+ break;
+ case 7:
+ printk(BIOS_DEBUG, "Trap: Store access fault\n");
+ printk(BIOS_DEBUG, "Bad instruction pc: %p\n", epc);
+ printk(BIOS_DEBUG, "Store Address: %p\n", badAddr);
+ break;
+ case 8:
+ printk(BIOS_DEBUG, "Trap: Environment call from U-mode\n");
+ break;
+ case 9:
+ // Don't print so we make console putchar calls look the way they should
+ // printk(BIOS_DEBUG, "Trap: Environment call from S-mode\n");
+ handleSupervisorCall(tf);
+ break;
+ case 10:
+ printk(BIOS_DEBUG, "Trap: Environment call from H-mode\n");
+ break;
+ case 11:
+ printk(BIOS_DEBUG, "Trap: Environment call from M-mode\n");
+ break;
+ default:
+ printk(BIOS_DEBUG, "Trap: Unknown cause\n");
+ break;
+ }
+ printk(BIOS_DEBUG, "Stored ra: %p\n", (void*) tf->gpr[1]);
+ printk(BIOS_DEBUG, "Stored sp: %p\n", (void*) tf->gpr[2]);
+ printk(BIOS_DEBUG, "looping...\n");
+ while(1);
+}
+
+void handleMisalignedStore(trapframe *tf) {
+ printk(BIOS_DEBUG, "Trapframe ptr: %p\n", tf);
+ printk(BIOS_DEBUG, "Stored sp: %p\n", (void*) tf->gpr[2]);
+ insn_t faultingInstruction = 0;
+ uintptr_t faultingInstructionAddr = tf->epc;
+ asm("move t0, %0" : /* No outputs */ : "r"(faultingInstructionAddr));
+ asm("lw t0, 0(t0)");
+ asm("move %0, t0" : "=r"(faultingInstruction));
+ printk(BIOS_DEBUG, "Faulting instruction: 0x%x\n", faultingInstruction);
+ insn_t widthMask = 0x7000;
+ insn_t memWidth = (faultingInstruction & widthMask) >> 12;
+ insn_t srcMask = 0x1F00000;
+ insn_t srcRegister = (faultingInstruction & srcMask) >> 20;
+ printk(BIOS_DEBUG, "Width: 0x%x\n", memWidth);
+ if (memWidth == 3) {
+ // store double, handle the issue
+ void* badAddress = (void*) tf->badvaddr;
+ long valueToStore = tf->gpr[srcRegister];
+ memcpy(badAddress, &valueToStore, 8);
+ } else {
+ // panic, this should not have happened
+ printk(BIOS_DEBUG, "Code should not reach this path, misaligned on a non-64 bit store/load\n");
+ while(1);
+ }
+
+ // return to where we came from
+ write_csr(mepc, read_csr(mepc) + 4);
+ asm volatile("j machine_call_return");
+}
diff --git a/src/arch/riscv/trap_util.S b/src/arch/riscv/trap_util.S
index 08061eb..9701aaf 100644
--- a/src/arch/riscv/trap_util.S
+++ b/src/arch/riscv/trap_util.S
@@ -121,7 +121,7 @@ supervisor_trap_entry:
trap_entry:
csrw mscratch, sp
1:addi sp,sp,-320
- save_tf_
+ save_tf
move a0,sp
jal trap_handler
.global supervisor_call_return
diff --git a/src/mainboard/emulation/spike-riscv/spike_util.c b/src/mainboard/emulation/spike-riscv/spike_util.c
index b34ff4a..1ee8b77 100644
--- a/src/mainboard/emulation/spike-riscv/spike_util.c
+++ b/src/mainboard/emulation/spike-riscv/spike_util.c
@@ -1,4 +1,143 @@
+/*
+ * Copyright (c) 2013, The Regents of the University of California (Regents).
+ * All Rights Reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the Regents nor the
+ * names of its contributors may be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
+ * SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING
+ * OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF REGENTS HAS
+ * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE. THE SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED
+ * HEREUNDER IS PROVIDED "AS IS". REGENTS HAS NO OBLIGATION TO PROVIDE
+ * MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
+ */
+
#include <spike_util.h>
+#include <arch/errno.h>
+#include <atomic.h>
+#include <string.h>
+#include <console/console.h>
+
+uintptr_t translate_address(uintptr_t vAddr) {
+ // TODO: implement the page table translation algorithm
+ //uintptr_t pageTableRoot = read_csr(sptbr);
+ uintptr_t physAddrMask = 0xfffffff;
+ uintptr_t translationResult = vAddr & physAddrMask;
+ printk(BIOS_DEBUG, "Translated virtual address 0x%llx to physical address 0x%llx\n", vAddr, translationResult);
+ return translationResult;
+}
+
+uintptr_t mcall_query_memory(uintptr_t id, memory_block_info *p)
+{
+ uintptr_t physicalAddr = translate_address((uintptr_t) p);
+ memory_block_info *info = (memory_block_info*) physicalAddr;
+ if (id == 0) {
+ info->base = 0x1000000; // hard coded for now, but we can put these values somewhere later
+ info->size = 0x7F000000 - info->base;
+ return 0;
+ }
+
+ return -1;
+}
+
+uintptr_t mcall_send_ipi(uintptr_t recipient)
+{
+ //if (recipient >= num_harts)
+ //return -1;
+
+ if (atomic_swap(&OTHER_HLS(recipient)->ipi_pending, 1) == 0) {
+ mb();
+ write_csr(send_ipi, recipient);
+ }
+
+ return 0;
+}
+
+uintptr_t mcall_clear_ipi(void)
+{
+ // only clear SSIP if no other events are pending
+ if (HLS()->device_response_queue_head == NULL) {
+ clear_csr(mip, MIP_SSIP);
+ mb();
+ }
+
+ return atomic_swap(&HLS()->ipi_pending, 0);
+}
+
+uintptr_t mcall_shutdown(void)
+{
+ while (1) write_csr(mtohost, 1);
+ return 0;
+}
+
+uintptr_t mcall_set_timer(unsigned long long when)
+{
+ write_csr(mtimecmp, when);
+ #ifndef __riscv64
+ write_csr(mtimecmph, when >> 32);
+ #endif
+ clear_csr(mip, MIP_STIP);
+ set_csr(mie, MIP_MTIP);
+ return 0;
+}
+
+uintptr_t mcall_dev_req(sbi_device_message *m)
+{
+ if ((m->dev > 0xFFU) | (m->cmd > 0xFFU) | (m->data > 0x0000FFFFFFFFFFFFU)) return -EINVAL;
+
+ while (swap_csr(mtohost, TOHOST_CMD(m->dev, m->cmd, m->data)) != 0);
+
+ m->sbi_private_data = (uintptr_t)HLS()->device_request_queue_head;
+ HLS()->device_request_queue_head = m;
+ HLS()->device_request_queue_size++;
+
+ return 0;
+}
+
+uintptr_t mcall_dev_resp(void)
+{
+ htif_interrupt(0, 0);
+
+ sbi_device_message* m = HLS()->device_response_queue_head;
+ if (m) {
+ //printm("resp %p\n", m);
+ sbi_device_message* next = (void*)atomic_read(&m->sbi_private_data);
+ HLS()->device_response_queue_head = next;
+ if (!next) {
+ HLS()->device_response_queue_tail = 0;
+
+ // only clear SSIP if no other events are pending
+ clear_csr(mip, MIP_SSIP);
+ mb();
+ if (HLS()->ipi_pending) set_csr(mip, MIP_SSIP);
+ }
+ }
+ return (uintptr_t)m;
+}
+
+uintptr_t mcall_hart_id(void)
+{
+ return HLS()->hart_id;
+}
+
+void hls_init(uint32_t hart_id)
+{
+ memset(HLS(), 0, sizeof(*HLS()));
+ HLS()->hart_id = hart_id;
+}
uintptr_t htif_interrupt(uintptr_t mcause, uintptr_t* regs) {
uintptr_t fromhost = swap_csr(mfromhost, 0);