From d52256b60fe0c5b4990d742ec2655fddd9d77e19 Mon Sep 17 00:00:00 2001 From: Palmer Dabbelt Date: Mon, 12 Jun 2017 09:18:05 -0700 Subject: Call puts instead of printf when printing constants printf doesn't fit in the scratchpad, but since there's really no reason to call it on constant strings it can be replaced with puts. With this change, the "hello" example fits in the scratchpad. --- software/hello/hello.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'software') diff --git a/software/hello/hello.c b/software/hello/hello.c index 85c5d87..befc6ee 100644 --- a/software/hello/hello.c +++ b/software/hello/hello.c @@ -2,7 +2,7 @@ int main() { - printf("hello world!\n"); + puts("hello world!\n"); return 0; } -- cgit v1.2.3 From b22853b2f4bdd1ed31d601ebd331083d67d40f11 Mon Sep 17 00:00:00 2001 From: Palmer Dabbelt Date: Tue, 13 Jun 2017 15:14:38 -0700 Subject: Add a SMP example This just prints "hello world" on two cores. It contains an example of how to initialize a multi-core system using IPIs, and a simple spin lock. --- software/smp/Makefile | 6 ++++++ software/smp/atomic.h | 29 +++++++++++++++++++++++++++++ software/smp/smp.c | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+) create mode 100644 software/smp/Makefile create mode 100644 software/smp/atomic.h create mode 100644 software/smp/smp.c (limited to 'software') diff --git a/software/smp/Makefile b/software/smp/Makefile new file mode 100644 index 0000000..4131ffc --- /dev/null +++ b/software/smp/Makefile @@ -0,0 +1,6 @@ +TARGET = smp +C_SRCS += smp.c +CFLAGS += -O2 -fno-builtin-printf -DENABLE_SMP + +BSP_BASE = ../../bsp +include $(BSP_BASE)/env/common.mk diff --git a/software/smp/atomic.h b/software/smp/atomic.h new file mode 100644 index 0000000..074c6a2 --- /dev/null +++ b/software/smp/atomic.h @@ -0,0 +1,29 @@ +#ifndef SIFIVE_ATOMIC_H +#define SIFIVE_ATOMIC_H + +#define ATOMIC_INIT(x) \ + { \ + .counter = (x), \ + } + +typedef struct { + int counter; +} atomic_t; + +static inline int atomic_xchg(atomic_t *v, int n) +{ + register int c; + + __asm__ __volatile__ ( + "amoswap.w.aqrl %0, %2, %1" + : "=r" (c), "+A" (v->counter) + : "r" (n)); + return c; +} + +static inline void mb(void) +{ + __asm__ __volatile__ ("fence"); +} + +#endif diff --git a/software/smp/smp.c b/software/smp/smp.c new file mode 100644 index 0000000..36c9db9 --- /dev/null +++ b/software/smp/smp.c @@ -0,0 +1,40 @@ +#include +#include +#include "atomic.h" +void write_hex(int, unsigned long); + +atomic_t tty_lock = ATOMIC_INIT(0); + +void get_lock(atomic_t *lock) +{ + while (atomic_xchg(lock, 1) == 1); + mb(); +} + +void put_lock(atomic_t *lock) +{ + mb(); + atomic_xchg(lock, 0); +} + +int secondary_main(int hartid) +{ + volatile int counter; + + while (1) { + get_lock(&tty_lock); + write(1, "hello world from hart ", 22); + char s[] = {'0', '\n', '\0'}; + s[0] += hartid; + write(1, s, 2); + put_lock(&tty_lock); + + for (counter = 0; counter < 10000 + 100*hartid; ++counter) + mb(); + } +} + +int main() +{ + return secondary_main(0); +} -- cgit v1.2.3 From 99a8e6a697a340a5f44db82e68ab8ab9be604131 Mon Sep 17 00:00:00 2001 From: Palmer Dabbelt Date: Tue, 13 Jun 2017 15:11:18 -0700 Subject: led_fade doesn't build on the e300-arty --- software/led_fade/.unsupported-boards | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'software') diff --git a/software/led_fade/.unsupported-boards b/software/led_fade/.unsupported-boards index ad80e34..f9e9897 100644 --- a/software/led_fade/.unsupported-boards +++ b/software/led_fade/.unsupported-boards @@ -1,4 +1,4 @@ freedom-e300-arty coreplexip-e31-arty coreplexip-e51-arty - +freedom-e300-arty -- cgit v1.2.3