diff --git a/arch/risc-v/include/jh7110/irq.h b/arch/risc-v/include/jh7110/irq.h new file mode 100644 index 0000000000000..305e30fdd9106 --- /dev/null +++ b/arch/risc-v/include/jh7110/irq.h @@ -0,0 +1,36 @@ +/**************************************************************************** + * arch/risc-v/include/qemu-rv/irq.h + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +#ifndef __ARCH_RISCV_INCLUDE_QEMU_RV_IRQ_H +#define __ARCH_RISCV_INCLUDE_QEMU_RV_IRQ_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* Map RISC-V exception code to NuttX IRQ */ + +#define NR_IRQS (RISCV_IRQ_SEXT + 127) + +#endif /* __ARCH_RISCV_INCLUDE_QEMU_RV_IRQ_H */ diff --git a/arch/risc-v/src/jh7110/Make.defs b/arch/risc-v/src/jh7110/Make.defs new file mode 100644 index 0000000000000..f8e15c487507b --- /dev/null +++ b/arch/risc-v/src/jh7110/Make.defs @@ -0,0 +1,30 @@ +############################################################################ +# arch/risc-v/src/qemu-rv/Make.defs +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. The +# ASF licenses this file to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance with the +# License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# +############################################################################ + +include common/Make.defs + +# Specify our HEAD assembly file. This will be linked as +# the first object file, so it will appear at address 0 +HEAD_ASRC = qemu_rv_head.S + +# Specify our C code within this directory to be included +CHIP_CSRCS = qemu_rv_start.c qemu_rv_irq_dispatch.c qemu_rv_irq.c +CHIP_CSRCS += qemu_rv_timerisr.c qemu_rv_allocateheap.c +CHIP_CSRCS += qemu_rv_mm_init.c qemu_rv_pgalloc.c diff --git a/arch/risc-v/src/jh7110/chip.h b/arch/risc-v/src/jh7110/chip.h new file mode 100644 index 0000000000000..557fc4ced3960 --- /dev/null +++ b/arch/risc-v/src/jh7110/chip.h @@ -0,0 +1,75 @@ +/**************************************************************************** + * arch/risc-v/src/qemu-rv/chip.h + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +#ifndef __ARCH_RISCV_SRC_QEMU_RV_CHIP_H +#define __ARCH_RISCV_SRC_QEMU_RV_CHIP_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +/* Include the chip capabilities file */ + +#include + +#include "qemu_rv_memorymap.h" + +#include "hardware/qemu_rv_memorymap.h" +#include "hardware/qemu_rv_plic.h" + +#include "riscv_internal.h" +#include "riscv_percpu.h" + +/**************************************************************************** + * Macro Definitions + ****************************************************************************/ + +#ifdef __ASSEMBLY__ + +/**************************************************************************** + * Name: setintstack + * + * Description: + * Set the current stack pointer to the "top" the correct interrupt stack + * for the current CPU. + * + ****************************************************************************/ + +#if defined(CONFIG_SMP) && CONFIG_ARCH_INTERRUPTSTACK > 15 +.macro setintstack tmp0, tmp1 + riscv_mhartid \tmp0 + li \tmp1, STACK_ALIGN_DOWN(CONFIG_ARCH_INTERRUPTSTACK) + mul \tmp1, \tmp0, \tmp1 + la \tmp0, g_intstacktop + sub sp, \tmp0, \tmp1 +.endm +#endif /* CONFIG_SMP && CONFIG_ARCH_INTERRUPTSTACK > 15 */ + +#if CONFIG_ARCH_INTERRUPTSTACK > 15 +#if !defined(CONFIG_SMP) && defined(CONFIG_ARCH_USE_S_MODE) +.macro setintstack tmp0, tmp1 + csrr \tmp0, CSR_SCRATCH + REGLOAD sp, RISCV_PERCPU_IRQSTACK(\tmp0) +.endm +#endif /* !defined(CONFIG_SMP) && defined(CONFIG_ARCH_USE_S_MODE) */ +#endif /* CONFIG_ARCH_INTERRUPTSTACK > 15 */ + +#endif /* __ASSEMBLY__ */ +#endif /* __ARCH_RISCV_SRC_QEMU_RV_CHIP_H */ diff --git a/arch/risc-v/src/jh7110/hardware/jh7110_memorymap.h b/arch/risc-v/src/jh7110/hardware/jh7110_memorymap.h new file mode 100644 index 0000000000000..6552e8903dc5b --- /dev/null +++ b/arch/risc-v/src/jh7110/hardware/jh7110_memorymap.h @@ -0,0 +1,32 @@ +/**************************************************************************** + * arch/risc-v/src/qemu-rv/hardware/qemu_rv_memorymap.h + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +#ifndef __ARCH_RISCV_SRC_QEMU_RV_HARDWARE_QEMU_RV_MEMORYMAP_H +#define __ARCH_RISCV_SRC_QEMU_RV_HARDWARE_QEMU_RV_MEMORYMAP_H + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* Register Base Address ****************************************************/ + +#define QEMU_RV_PLIC_BASE 0x0c000000 + +#endif /* __ARCH_RISCV_SRC_QEMU_RV_HARDWARE_QEMU_RV_MEMORYMAP_H */ diff --git a/arch/risc-v/src/jh7110/hardware/jh7110_plic.h b/arch/risc-v/src/jh7110/hardware/jh7110_plic.h new file mode 100644 index 0000000000000..fc2ef122a990d --- /dev/null +++ b/arch/risc-v/src/jh7110/hardware/jh7110_plic.h @@ -0,0 +1,51 @@ +/**************************************************************************** + * arch/risc-v/src/qemu-rv/hardware/qemu_rv_plic.h + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +#ifndef __ARCH_RISCV_SRC_QEMU_RV_HARDWARE_QEMU_RV_PLIC_H +#define __ARCH_RISCV_SRC_QEMU_RV_HARDWARE_QEMU_RV_PLIC_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* Interrupt Priority */ + +#define QEMU_RV_PLIC_PRIORITY (QEMU_RV_PLIC_BASE + 0x000000) + +/* Hart 1 S-Mode Interrupt Enable */ + +#define QEMU_RV_PLIC_ENABLE1 (QEMU_RV_PLIC_BASE + 0x002100) +#define QEMU_RV_PLIC_ENABLE2 (QEMU_RV_PLIC_BASE + 0x002104) + +/* Hart 1 S-Mode Priority Threshold */ + +#define QEMU_RV_PLIC_THRESHOLD (QEMU_RV_PLIC_BASE + 0x202000) + +/* Hart 1 S-Mode Claim / Complete */ + +#define QEMU_RV_PLIC_CLAIM (QEMU_RV_PLIC_BASE + 0x202004) + +#endif /* __ARCH_RISCV_SRC_QEMU_RV_HARDWARE_QEMU_RV_PLIC_H */ diff --git a/arch/risc-v/src/jh7110/jh7110_allocateheap.c b/arch/risc-v/src/jh7110/jh7110_allocateheap.c new file mode 100644 index 0000000000000..aeee9d1b72d26 --- /dev/null +++ b/arch/risc-v/src/jh7110/jh7110_allocateheap.c @@ -0,0 +1,145 @@ +/**************************************************************************** + * arch/risc-v/src/qemu-rv/qemu_rv_allocateheap.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include + +#include + +#ifdef CONFIG_MM_KERNEL_HEAP +#include +#endif + +#include "riscv_internal.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#ifdef CONFIG_MM_KERNEL_HEAP +#define KRAM_END KSRAM_END +#else +#define KRAM_END CONFIG_RAM_END +#endif + +/**************************************************************************** + * Public Data + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: up_allocate_heap + * + * Description: + * This function will be called to dynamically set aside the heap region. + * + * For the kernel build (CONFIG_BUILD_PROTECTED=y) with both kernel- and + * user-space heaps (CONFIG_MM_KERNEL_HEAP=y), this function provides the + * size of the unprotected, user-space heap. + * + * If a protected kernel-space heap is provided, the kernel heap must be + * allocated (and protected) by an analogous up_allocate_kheap(). + * + * The following memory map is assumed for the flat build: + * + * .data region. Size determined at link time. + * .bss region Size determined at link time. + * IDLE thread stack. Size determined by CONFIG_IDLETHREAD_STACKSIZE. + * Heap. Extends to the end of User SRAM. + * + * The following memory map is assumed for the protect build. + * The kernel and user space have it's own dedicated heap space. + * + * User .data region Size determined at link time + * User .bss region Size determined at link time + * User heap Extends to the end of User SRAM + * Kernel .data region Size determined at link time + * Kernel .bss region Size determined at link time + * Kernel IDLE thread stack Size determined by CONFIG_IDLETHREAD_STACKSIZE + * Kernel heap Size determined by CONFIG_MM_KERNEL_HEAPSIZE + * + ****************************************************************************/ + +void up_allocate_kheap(void **heap_start, size_t *heap_size) +{ +#if defined(CONFIG_BUILD_PROTECTED) && defined(CONFIG_MM_KERNEL_HEAP) + /* Get the size and position of the user-space heap. + * This heap begins after the user-space .bss section. + */ + + uintptr_t ubase = (uintptr_t)USERSPACE->us_bssend; + size_t usize = (uintptr_t)USERSPACE->us_heapend - ubase; + + /* Return the user-space heap settings */ + + *heap_start = (void *)ubase; + *heap_size = usize; + + /* Allow user-mode access to the user heap memory in PMP + * is already done in qemu_rv_userspace(). + */ + +#else + /* Return the heap settings */ + + *heap_start = (void *)g_idle_topstack; + *heap_size = KRAM_END - g_idle_topstack; +#endif /* CONFIG_BUILD_PROTECTED && CONFIG_MM_KERNEL_HEAP */ +} + +/**************************************************************************** + * Name: up_allocate_kheap + * + * Description: + * For the kernel build (CONFIG_BUILD_PROTECTED=y) with both kernel- and + * user-space heaps (CONFIG_MM_KERNEL_HEAP=y), this function allocates + * (and protects) the kernel-space heap. + * + ****************************************************************************/ + +#if defined(CONFIG_BUILD_PROTECTED) && defined(CONFIG_MM_KERNEL_HEAP) && \ + defined(__KERNEL__) +void up_allocate_kheap(void **heap_start, size_t *heap_size) +{ + /* Return the kernel heap settings. */ + + *heap_start = (void *)g_idle_topstack; + *heap_size = KRAM_END - g_idle_topstack; +} +#endif /* CONFIG_BUILD_PROTECTED && CONFIG_MM_KERNEL_HEAP */ + +/**************************************************************************** + * Name: riscv_addregion + ****************************************************************************/ + +#if CONFIG_MM_REGIONS > 1 +void riscv_addregion(void) +{ +} +#endif diff --git a/arch/risc-v/src/jh7110/jh7110_head.S b/arch/risc-v/src/jh7110/jh7110_head.S new file mode 100644 index 0000000000000..a52393b13320a --- /dev/null +++ b/arch/risc-v/src/jh7110/jh7110_head.S @@ -0,0 +1,150 @@ +/**************************************************************************** + * arch/risc-v/src/qemu-rv/qemu_rv_head.S + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include + +#include "chip.h" +#include "riscv_internal.h" + +/**************************************************************************** + * Public Symbols + ****************************************************************************/ + + /* Exported Symbols */ + + .section .text + .global __start + +__start: + /* Begin Test */ + + /* DO NOT MODIFY. Image Header expected by Linux bootloaders. + * + * This `li` instruction has no meaningful effect except that + * its opcode forms the magic "MZ" signature of a PE/COFF file + * that is required for UEFI applications. + * + * Some bootloaders check the magic "MZ" to see if the image is a valid + * Linux image. But modifying the bootLoader is unnecessary unless we + * need to do a customized secure boot. So we just put "MZ" in the + * header to make the bootloader happy. + */ + + c.li s4, -13 /* Magic Signature "MZ" (2 bytes) */ + j real_start /* Jump to Kernel Start (2 bytes) */ + .long 0 /* Executable Code padded to 8 bytes */ + .quad 0x200000 /* Image load offset from start of RAM */ + .quad _ebss - __start /* Effective size of kernel image */ + .quad 0 /* Kernel flags, little-endian */ + .long 2 /* Version of this header */ + .long 0 /* Reserved */ + .quad 0 /* Reserved */ + .ascii "RISCV\x00\x00\x00" /* Magic number, "RISCV" (8 bytes) */ + .ascii "RSC\x05" /* Magic number 2, "RSC\x05" (4 bytes) */ + .long 0 /* Reserved for PE COFF offset */ + +real_start: + + /* We assume that OpenSBI has passed Hart ID (value 1) in Register a0. + * But NuttX expects Hart ID to start at 0, so we subtract 1. + */ + + addi a0, a0, -1 + + /* Set stack pointer to the idle thread stack */ + + bnez a0, 1f + la sp, QEMU_RV_IDLESTACK_TOP + j 2f +1: + + /* Load the number of CPUs that the kernel supports */ + +#ifdef CONFIG_SMP + li t1, CONFIG_SMP_NCPUS +#else + li t1, 1 +#endif + + /* If a0 (hartid) >= t1 (the number of CPUs), stop here */ + + blt a0, t1, 3f + csrw sie, zero + wfi + +3: + /* To get g_cpu_basestack[hartid], must get g_cpu_basestack first */ + + la t0, g_cpu_basestack + + /* Offset = pointer width * hart id */ + +#ifdef CONFIG_ARCH_RV32 + slli t1, a0, 2 +#else + slli t1, a0, 3 +#endif + add t0, t0, t1 + + /* Load idle stack base to sp */ + + REGLOAD sp, 0(t0) + + /* + * sp (stack top) = sp + idle stack size - XCPTCONTEXT_SIZE + * + * Note: Reserve some space used by up_initial_state since we are already + * running and using the per CPU idle stack. + */ + + li t0, STACK_ALIGN_UP(CONFIG_IDLETHREAD_STACKSIZE - XCPTCONTEXT_SIZE) + add sp, sp, t0 + +2: + + /* Disable all interrupts (i.e. timer, external) in sie */ + + csrw sie, zero + + la t0, __trap_vec + csrw stvec, t0 + + /* Jump to qemu_rv_start */ + + jal x1, qemu_rv_start + + /* We shouldn't return from _start */ + + .global _init + .global _fini + +_init: +_fini: + + /* These don't have to do anything since we use init_array/fini_array. */ + + ret diff --git a/arch/risc-v/src/jh7110/jh7110_irq.c b/arch/risc-v/src/jh7110/jh7110_irq.c new file mode 100644 index 0000000000000..4a140fa521662 --- /dev/null +++ b/arch/risc-v/src/jh7110/jh7110_irq.c @@ -0,0 +1,194 @@ +/**************************************************************************** + * arch/risc-v/src/qemu-rv/qemu_rv_irq.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include +#include +#include + +#include +#include + +#include "riscv_internal.h" +#include "chip.h" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: up_irqinitialize + ****************************************************************************/ + +void up_irqinitialize(void) +{ + /* Disable Machine interrupts */ + + up_irq_save(); + + /* Disable all global interrupts */ + + putreg32(0x0, QEMU_RV_PLIC_ENABLE1); + putreg32(0x0, QEMU_RV_PLIC_ENABLE2); + + /* Colorize the interrupt stack for debug purposes */ + +#if defined(CONFIG_STACK_COLORATION) && CONFIG_ARCH_INTERRUPTSTACK > 15 + size_t intstack_size = (CONFIG_ARCH_INTERRUPTSTACK & ~15); + riscv_stack_color(g_intstackalloc, intstack_size); +#endif + + /* Set priority for all global interrupts to 1 (lowest) */ + + int id; + + for (id = 1; id <= NR_IRQS; id++) + { + putreg32(1, (uintptr_t)(QEMU_RV_PLIC_PRIORITY + 4 * id)); + } + + /* Set irq threshold to 0 (permits all global interrupts) */ + + putreg32(0, QEMU_RV_PLIC_THRESHOLD); + + /* Attach the common interrupt handler */ + + riscv_exception_attach(); + +#ifdef CONFIG_SMP + /* Clear RISCV_IPI for CPU0 */ + + putreg32(0, RISCV_IPI); + + up_enable_irq(RISCV_IRQ_SOFT); +#endif + +#ifndef CONFIG_SUPPRESS_INTERRUPTS + + /* And finally, enable interrupts */ + + up_irq_enable(); +#endif +} + +/**************************************************************************** + * Name: up_disable_irq + * + * Description: + * Disable the IRQ specified by 'irq' + * + ****************************************************************************/ + +void up_disable_irq(int irq) +{ + int extirq; + + if (irq == RISCV_IRQ_SOFT) + { + /* Read m/sstatus & clear machine software interrupt enable in m/sie */ + + CLEAR_CSR(CSR_IE, IE_SIE); + } + else if (irq == RISCV_IRQ_TIMER) + { + /* Read m/sstatus & clear timer interrupt enable in m/sie */ + + CLEAR_CSR(CSR_IE, IE_TIE); + } + else if (irq > RISCV_IRQ_EXT) + { + extirq = irq - RISCV_IRQ_EXT; + + /* Clear enable bit for the irq */ + + if (0 <= extirq && extirq <= 63) + { + modifyreg32(QEMU_RV_PLIC_ENABLE1 + (4 * (extirq / 32)), + 1 << (extirq % 32), 0); + } + else + { + PANIC(); + } + } +} + +/**************************************************************************** + * Name: up_enable_irq + * + * Description: + * Enable the IRQ specified by 'irq' + * + ****************************************************************************/ + +void up_enable_irq(int irq) +{ + int extirq; + + if (irq == RISCV_IRQ_SOFT) + { + /* Read sstatus & set machine software interrupt enable in sie */ + + SET_CSR(CSR_IE, IE_SIE); + } + else if (irq == RISCV_IRQ_TIMER) + { + /* Read sstatus & set timer interrupt enable in sie */ + + SET_CSR(CSR_IE, IE_TIE); + } + else if (irq > RISCV_IRQ_EXT) + { + extirq = irq - RISCV_IRQ_EXT; + + /* Set enable bit for the irq */ + + if (0 <= extirq && extirq <= 63) + { + modifyreg32(QEMU_RV_PLIC_ENABLE1 + (4 * (extirq / 32)), + 0, 1 << (extirq % 32)); + } + else + { + PANIC(); + } + } +} + +irqstate_t up_irq_enable(void) +{ + irqstate_t oldstat; + + /* Enable external interrupts (sie) */ + + SET_CSR(CSR_IE, IE_EIE); + + /* Read and enable global interrupts (sie) in sstatus */ + + oldstat = READ_AND_SET_CSR(CSR_STATUS, STATUS_IE); + + return oldstat; +} diff --git a/arch/risc-v/src/jh7110/jh7110_irq_dispatch.c b/arch/risc-v/src/jh7110/jh7110_irq_dispatch.c new file mode 100644 index 0000000000000..eb1acd66dc470 --- /dev/null +++ b/arch/risc-v/src/jh7110/jh7110_irq_dispatch.c @@ -0,0 +1,84 @@ +/**************************************************************************** + * arch/risc-v/src/qemu-rv/qemu_rv_irq_dispatch.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include + +#include +#include +#include + +#include "riscv_internal.h" +#include "hardware/qemu_rv_memorymap.h" +#include "hardware/qemu_rv_plic.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define RV_IRQ_MASK 59 + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * riscv_dispatch_irq + ****************************************************************************/ + +void *riscv_dispatch_irq(uintptr_t vector, uintptr_t *regs) +{ + int irq = (vector >> RV_IRQ_MASK) | (vector & 0xf); + + /* Firstly, check if the irq is machine external interrupt */ + + if (RISCV_IRQ_EXT == irq) + { + uintptr_t val = getreg32(QEMU_RV_PLIC_CLAIM); + + /* Add the value to nuttx irq which is offset to the mext */ + + irq += val; + } + + /* EXT means no interrupt */ + + if (RISCV_IRQ_EXT != irq) + { + /* Deliver the IRQ */ + + regs = riscv_doirq(irq, regs); + } + + if (RISCV_IRQ_EXT <= irq) + { + /* Then write PLIC_CLAIM to clear pending in PLIC */ + + putreg32(irq - RISCV_IRQ_EXT, QEMU_RV_PLIC_CLAIM); + } + + return regs; +} diff --git a/arch/risc-v/src/jh7110/jh7110_mm_init.c b/arch/risc-v/src/jh7110/jh7110_mm_init.c new file mode 100644 index 0000000000000..2741820d3890d --- /dev/null +++ b/arch/risc-v/src/jh7110/jh7110_mm_init.c @@ -0,0 +1,284 @@ +/**************************************************************************** + * arch/risc-v/src/qemu-rv/qemu_rv_mm_init.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include + +#include +#include +#include + +#include + +#include "qemu_rv_memorymap.h" + +#include "riscv_internal.h" +#include "riscv_mmu.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* Ramdisk Load Address from U-Boot */ + +#define RAMDISK_ADDR_R (0x46100000) + +/* Map the whole I/O memory with vaddr = paddr mappings */ + +#define MMU_IO_BASE (0x00000000) +#define MMU_IO_SIZE (0x40000000) + +/* Physical and virtual addresses to page tables (vaddr = paddr mapping) */ + +#define PGT_L1_PBASE (uintptr_t)&m_l1_pgtable +#define PGT_L2_PBASE (uintptr_t)&m_l2_pgtable +#define PGT_L3_PBASE (uintptr_t)&m_l3_pgtable +#define PGT_L1_VBASE PGT_L1_PBASE +#define PGT_L2_VBASE PGT_L2_PBASE +#define PGT_L3_VBASE PGT_L3_PBASE + +#define PGT_L1_SIZE (512) /* Enough to map 512 GiB */ +#define PGT_L2_SIZE (512) /* Enough to map 1 GiB */ +#define PGT_L3_SIZE (1024) /* Enough to map 4 MiB (2MiB x 2) */ + +#define SLAB_COUNT (sizeof(m_l3_pgtable) / RV_MMU_PAGE_SIZE) + +#define KMM_PAGE_SIZE RV_MMU_L3_PAGE_SIZE +#define KMM_PBASE PGT_L3_PBASE +#define KMM_PBASE_IDX 3 +#define KMM_SPBASE PGT_L2_PBASE +#define KMM_SPBASE_IDX 2 + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +struct pgalloc_slab_s +{ + sq_entry_t *next; + void *memory; +}; +typedef struct pgalloc_slab_s pgalloc_slab_t; + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/* Kernel mappings simply here, mapping is vaddr=paddr */ + +static size_t m_l1_pgtable[PGT_L1_SIZE] locate_data(".pgtables"); +static size_t m_l2_pgtable[PGT_L2_SIZE] locate_data(".pgtables"); +static size_t m_l3_pgtable[PGT_L3_SIZE] locate_data(".pgtables"); + +/* Kernel mappings (L1 base) */ + +uintptr_t g_kernel_mappings = PGT_L1_VBASE; +uintptr_t g_kernel_pgt_pbase = PGT_L1_PBASE; + +/* L3 page table allocator */ + +static sq_queue_t g_free_slabs; +static pgalloc_slab_t g_slabs[SLAB_COUNT]; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: slab_init + * + * Description: + * Initialize slab allocator for L2 or L3 page table entries + * + * L2 Page table is used for SV32. L3 used for SV39 + * + * Input Parameters: + * start - Beginning of the L2 or L3 page table pool + * + ****************************************************************************/ + +static void slab_init(uintptr_t start) +{ + int i; + + sq_init(&g_free_slabs); + + for (i = 0; i < SLAB_COUNT; i++) + { + g_slabs[i].memory = (void *)start; + sq_addlast((sq_entry_t *)&g_slabs[i], (sq_queue_t *)&g_free_slabs); + start += RV_MMU_PAGE_SIZE; + } +} + +/**************************************************************************** + * Name: slab_alloc + * + * Description: + * Allocate single slab for L2/L3 page table entry + * + * L2 Page table is used for SV32. L3 used for SV39 + * + ****************************************************************************/ + +static uintptr_t slab_alloc(void) +{ + pgalloc_slab_t *slab = (pgalloc_slab_t *)sq_remfirst(&g_free_slabs); + return slab ? (uintptr_t)slab->memory : 0; +} + +/**************************************************************************** + * Name: map_region + * + * Description: + * Map a region of physical memory to the L3 page table + * + * Input Parameters: + * paddr - Beginning of the physical address mapping + * vaddr - Beginning of the virtual address mapping + * size - Size of the region in bytes + * mmuflags - The MMU flags to use in the mapping + * + ****************************************************************************/ + +static void map_region(uintptr_t paddr, uintptr_t vaddr, size_t size, + uint32_t mmuflags) +{ + uintptr_t endaddr; + uintptr_t pbase; + int npages; + int i; + int j; + + /* How many pages */ + + npages = (size + RV_MMU_PAGE_MASK) >> RV_MMU_PAGE_SHIFT; + endaddr = vaddr + size; + + for (i = 0; i < npages; i += RV_MMU_PAGE_ENTRIES) + { + /* See if a mapping exists ? */ + + pbase = mmu_pte_to_paddr(mmu_ln_getentry( + KMM_SPBASE_IDX, KMM_SPBASE, vaddr)); + if (!pbase) + { + /* No, allocate 1 page, this must not fail */ + + pbase = slab_alloc(); + DEBUGASSERT(pbase); + + /* Map it to the new table */ + + mmu_ln_setentry( + KMM_SPBASE_IDX, KMM_SPBASE, pbase, vaddr, MMU_UPGT_FLAGS); + } + + /* Then add the mappings */ + + for (j = 0; j < RV_MMU_PAGE_ENTRIES && vaddr < endaddr; j++) + { + mmu_ln_setentry(KMM_PBASE_IDX, pbase, paddr, vaddr, mmuflags); + paddr += KMM_PAGE_SIZE; + vaddr += KMM_PAGE_SIZE; + } + } +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: qemu_rv_kernel_mappings + * + * Description: + * Setup kernel mappings when using CONFIG_BUILD_KERNEL. Sets up the kernel + * MMU mappings. + * + ****************************************************************************/ + +void qemu_rv_kernel_mappings(void) +{ + /* Initialize slab allocator for the L2/L3 page tables */ + + slab_init(KMM_PBASE); + + /* Begin mapping memory to MMU; note that at this point the MMU is not yet + * active, so the page table virtual addresses are actually physical + * addresses and so forth. M-mode does not perform translations anyhow, so + * this mapping is quite simple to do + */ + + /* Map I/O region, use enough large page tables for the IO region. */ + + binfo("map I/O regions\n"); + mmu_ln_map_region(1, PGT_L1_VBASE, MMU_IO_BASE, MMU_IO_BASE, + MMU_IO_SIZE, MMU_IO_FLAGS); + + /* Map the kernel text and data for L2/L3 */ + + binfo("map kernel text\n"); + map_region(KFLASH_START, KFLASH_START, KFLASH_SIZE, MMU_KTEXT_FLAGS); + + binfo("map kernel data\n"); + map_region(KSRAM_START, KSRAM_START, KSRAM_SIZE, MMU_KDATA_FLAGS); + + /* Connect the L1 and L2 page tables for the kernel text and data */ + + binfo("connect the L1 and L2 page tables\n"); + mmu_ln_setentry(1, PGT_L1_VBASE, PGT_L2_PBASE, KFLASH_START, PTE_G); + + /* Map the page pool */ + + binfo("map the page pool\n"); + mmu_ln_map_region(2, PGT_L2_VBASE, PGPOOL_START, PGPOOL_START, PGPOOL_SIZE, + MMU_KDATA_FLAGS); +} + +/**************************************************************************** + * Name: qemu_rv_mm_init + * + * Description: + * Setup kernel mappings when using CONFIG_BUILD_KERNEL. Sets up kernel MMU + * mappings. Function also sets the first address environment (satp value). + * + ****************************************************************************/ + +void qemu_rv_mm_init(void) +{ + /* Copy Ramdisk from U-Boot Ramdisk Load Address */ + + memcpy((void *)__ramdisk_start, (void *)RAMDISK_ADDR_R, + (size_t)__ramdisk_size); + + /* Setup the kernel mappings */ + + qemu_rv_kernel_mappings(); + + /* Enable MMU (note: system is still in M-mode) */ + + binfo("mmu_enable: satp=%" PRIuPTR "\n", g_kernel_pgt_pbase); + mmu_enable(g_kernel_pgt_pbase, 0); +} diff --git a/arch/risc-v/src/jh7110/jh7110_start.c b/arch/risc-v/src/jh7110/jh7110_start.c new file mode 100644 index 0000000000000..0d8126f422d6e --- /dev/null +++ b/arch/risc-v/src/jh7110/jh7110_start.c @@ -0,0 +1,176 @@ +/**************************************************************************** + * arch/risc-v/src/qemu-rv/qemu_rv_start.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include +#include +#include +#include + +#include "riscv_internal.h" +#include "chip.h" +#include "qemu_rv_mm_init.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#ifdef CONFIG_DEBUG_FEATURES +#define showprogress(c) up_putc(c) +#else +#define showprogress(c) +#endif + +/**************************************************************************** + * Extern Function Declarations + ****************************************************************************/ + +extern void __trap_vec(void); +extern void __trap_vec_m(void); +extern void up_mtimer_initialize(void); + +/**************************************************************************** + * Public Data + ****************************************************************************/ + +/* NOTE: g_idle_topstack needs to point the top of the idle stack + * for CPU0 and this value is used in up_initial_state() + */ + +uintptr_t g_idle_topstack = QEMU_RV_IDLESTACK_TOP; + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: qemu_rv_clear_bss + ****************************************************************************/ + +void qemu_rv_clear_bss(void) +{ + uint32_t *dest; + + /* Clear .bss. We'll do this inline (vs. calling memset) just to be + * certain that there are no issues with the state of global variables. + */ + + for (dest = (uint32_t *)_sbss; dest < (uint32_t *)_ebss; ) + { + *dest++ = 0; + } +} + +/**************************************************************************** + * Name: qemu_rv_start + ****************************************************************************/ + +void qemu_rv_start_s(int mhartid) +{ + /* Configure FPU */ + + riscv_fpuconfig(); + + if (mhartid > 0) + { + goto cpux; + } + +#ifndef CONFIG_BUILD_KERNEL + qemu_rv_clear_bss(); +#endif + + showprogress('A'); + +#ifdef USE_EARLYSERIALINIT + riscv_earlyserialinit(); +#endif + + showprogress('B'); + + /* Do board initialization */ + + showprogress('C'); + +#ifdef CONFIG_BUILD_KERNEL + /* Setup page tables for kernel and enable MMU */ + + qemu_rv_mm_init(); +#endif + + /* Call nx_start() */ + + nx_start(); + +cpux: + +#ifdef CONFIG_SMP + riscv_cpu_boot(mhartid); +#endif + + while (true) + { + asm("WFI"); + } +} + +/**************************************************************************** + * Name: qemu_rv_start + ****************************************************************************/ + +void qemu_rv_start(int mhartid) +{ + DEBUGASSERT(mhartid == 0); /* Only Hart 0 supported for now */ + + if (0 == mhartid) + { + qemu_rv_clear_bss(); + + /* Initialize the per CPU areas */ + + riscv_percpu_add_hart(mhartid); + } + + /* Disable MMU */ + + WRITE_CSR(satp, 0x0); + + /* Set the trap vector for S-mode */ + + WRITE_CSR(stvec, (uintptr_t)__trap_vec); + + /* Start S-mode */ + + qemu_rv_start_s(mhartid); +} + +void riscv_earlyserialinit(void) +{ + u16550_earlyserialinit(); +} + +void riscv_serialinit(void) +{ + u16550_serialinit(); +} diff --git a/arch/risc-v/src/jh7110/jh7110_timerisr.c b/arch/risc-v/src/jh7110/jh7110_timerisr.c new file mode 100644 index 0000000000000..157eb0d3e023f --- /dev/null +++ b/arch/risc-v/src/jh7110/jh7110_timerisr.c @@ -0,0 +1,113 @@ +/**************************************************************************** + * arch/risc-v/src/qemu-rv/qemu_rv_timerisr.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#include "riscv_internal.h" +#include "riscv_mtimer.h" +#include "riscv_percpu.h" +#include "hardware/qemu_rv_memorymap.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define MTIMER_FREQ 10000000 +#define TICK_COUNT (10000000 / TICK_PER_SEC) + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static uint32_t g_stimer_pending = false; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: qemu_rv_ssoft_interrupt + * + * Description: + * This function is S-mode software interrupt handler to proceed + * the OS timer + * + ****************************************************************************/ + +static int qemu_rv_ssoft_interrupt(int irq, void *context, void *arg) +{ + /* Cleaer Supervisor Software Interrupt */ + + CLEAR_CSR(sip, SIP_SSIP); + + if (g_stimer_pending) + { + g_stimer_pending = false; + + /* Proceed the OS timer */ + + nxsched_process_timer(); + } +#ifdef CONFIG_SMP + else + { + /* We assume IPI has been issued */ + + riscv_pause_handler(irq, context, arg); + } +#endif + + return 0; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: up_timer_initialize + * + * Description: + * This function is called during start-up to initialize + * the timer interrupt. + * + ****************************************************************************/ + +void up_timer_initialize(void) +{ + irq_attach(RISCV_IRQ_SSOFT, qemu_rv_ssoft_interrupt, NULL); + up_enable_irq(RISCV_IRQ_SSOFT); +} diff --git a/boards/risc-v/jh7110/star64/configs/nsh/defconfig b/boards/risc-v/jh7110/star64/configs/nsh/defconfig new file mode 100644 index 0000000000000..4b5cbde90d573 --- /dev/null +++ b/boards/risc-v/jh7110/star64/configs/nsh/defconfig @@ -0,0 +1,110 @@ +# +# This file is autogenerated: PLEASE DO NOT EDIT IT. +# +# You can use "make menuconfig" to make any modifications to the installed .config file. +# You can then do "make savedefconfig" to generate a new defconfig file that includes your +# modifications. +# +# CONFIG_DISABLE_OS_API is not set +# CONFIG_NSH_DISABLE_LOSMART is not set +CONFIG_16550_ADDRWIDTH=0 +CONFIG_16550_REGINCR=4 +CONFIG_16550_UART0=y +CONFIG_16550_UART0_BASE=0x10000000 +CONFIG_16550_UART0_CLOCK=23040000 +CONFIG_16550_UART0_IRQ=57 +CONFIG_16550_UART0_SERIAL_CONSOLE=y +CONFIG_16550_UART=y +CONFIG_16550_WAIT_LCR=y +CONFIG_ARCH="risc-v" +CONFIG_ARCH_ADDRENV=y +CONFIG_ARCH_BOARD="rv-virt" +CONFIG_ARCH_BOARD_QEMU_RV_VIRT=y +CONFIG_ARCH_CHIP="qemu-rv" +CONFIG_ARCH_CHIP_QEMU_RV64=y +CONFIG_ARCH_CHIP_QEMU_RV=y +CONFIG_ARCH_CHIP_QEMU_RV_ISA_A=y +CONFIG_ARCH_CHIP_QEMU_RV_ISA_C=y +CONFIG_ARCH_CHIP_QEMU_RV_ISA_M=y +CONFIG_ARCH_DATA_NPAGES=128 +CONFIG_ARCH_DATA_VBASE=0xC0100000 +CONFIG_ARCH_HEAP_NPAGES=128 +CONFIG_ARCH_HEAP_VBASE=0xC0200000 +CONFIG_ARCH_INTERRUPTSTACK=2048 +CONFIG_ARCH_KERNEL_STACKSIZE=3072 +CONFIG_ARCH_PGPOOL_MAPPING=y +CONFIG_ARCH_PGPOOL_PBASE=0x40600000 +CONFIG_ARCH_PGPOOL_SIZE=4194304 +CONFIG_ARCH_PGPOOL_VBASE=0x40600000 +CONFIG_ARCH_RISCV=y +CONFIG_ARCH_STACKDUMP=y +CONFIG_ARCH_TEXT_NPAGES=128 +CONFIG_ARCH_TEXT_VBASE=0xC0000000 +CONFIG_ARCH_USE_MMU=y +CONFIG_ARCH_USE_MPU=y +CONFIG_ARCH_USE_S_MODE=y +CONFIG_BOARDCTL_ROMDISK=y +CONFIG_BOARD_LATE_INITIALIZE=y +CONFIG_BOARD_LOOPSPERMSEC=116524 +CONFIG_BUILD_KERNEL=y +CONFIG_DEBUG_ASSERTIONS=y +CONFIG_DEBUG_ASSERTIONS_EXPRESSION=y +CONFIG_DEBUG_BINFMT=y +CONFIG_DEBUG_BINFMT_ERROR=y +CONFIG_DEBUG_BINFMT_WARN=y +CONFIG_DEBUG_ERROR=y +CONFIG_DEBUG_FEATURES=y +CONFIG_DEBUG_FS=y +CONFIG_DEBUG_FS_ERROR=y +CONFIG_DEBUG_FS_WARN=y +CONFIG_DEBUG_FULLOPT=y +CONFIG_DEBUG_INFO=y +CONFIG_DEBUG_MM=y +CONFIG_DEBUG_MM_ERROR=y +CONFIG_DEBUG_MM_WARN=y +CONFIG_DEBUG_SCHED=y +CONFIG_DEBUG_SCHED_ERROR=y +CONFIG_DEBUG_SCHED_INFO=y +CONFIG_DEBUG_SCHED_WARN=y +CONFIG_DEBUG_SYMBOLS=y +CONFIG_DEBUG_WARN=y +CONFIG_DEV_ZERO=y +CONFIG_ELF=y +CONFIG_EXAMPLES_HELLO=m +CONFIG_FS_PROCFS=y +CONFIG_FS_ROMFS=y +CONFIG_IDLETHREAD_STACKSIZE=3072 +CONFIG_INIT_FILEPATH="/system/bin/init" +CONFIG_INIT_MOUNT=y +CONFIG_INIT_MOUNT_FLAGS=0x1 +CONFIG_INIT_MOUNT_TARGET="/system/bin" +CONFIG_INIT_STACKSIZE=3072 +CONFIG_INTELHEX_BINARY=y +CONFIG_LIBC_ENVPATH=y +CONFIG_LIBC_EXECFUNCS=y +CONFIG_LIBC_PERROR_STDOUT=y +CONFIG_LIBC_STRERROR=y +CONFIG_MEMSET_64BIT=y +CONFIG_MEMSET_OPTSPEED=y +CONFIG_MM_PGALLOC=y +CONFIG_NFILE_DESCRIPTORS_PER_BLOCK=6 +CONFIG_NSH_ARCHINIT=y +CONFIG_NSH_FILEIOSIZE=512 +CONFIG_NSH_FILE_APPS=y +CONFIG_NSH_READLINE=y +CONFIG_PATH_INITIAL="/system/bin" +CONFIG_RAM_SIZE=1048576 +CONFIG_RAM_START=0x40200000 +CONFIG_READLINE_CMD_HISTORY=y +CONFIG_RR_INTERVAL=200 +CONFIG_SCHED_LPWORK=y +CONFIG_SCHED_WAITPID=y +CONFIG_SERIAL_UART_ARCH_MMIO=y +CONFIG_STACK_COLORATION=y +CONFIG_START_MONTH=12 +CONFIG_START_YEAR=2021 +CONFIG_SYMTAB_ORDEREDBYNAME=y +CONFIG_SYSTEM_NSH=y +CONFIG_SYSTEM_NSH_PROGNAME="init" +CONFIG_TESTING_GETPRIME=y +CONFIG_USEC_PER_TICK=1000 diff --git a/boards/risc-v/jh7110/star64/include/board_memorymap.h b/boards/risc-v/jh7110/star64/include/board_memorymap.h new file mode 100644 index 0000000000000..cc232f3c13d21 --- /dev/null +++ b/boards/risc-v/jh7110/star64/include/board_memorymap.h @@ -0,0 +1,88 @@ +/**************************************************************************** + * boards/risc-v/qemu-rv/rv-virt/include/board_memorymap.h + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +#ifndef __BOARDS_RISCV_QEMURV_RVVIRT_INCLUDE_BOARD_MEMORYMAP_H +#define __BOARDS_RISCV_QEMURV_RVVIRT_INCLUDE_BOARD_MEMORYMAP_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* DDR start address */ + +#define QEMURV_DDR_BASE (0x40200000) +#define QEMURV_DDR_SIZE (0x40000000) + +/* Kernel code memory (RX) */ + +#define KFLASH_START (uintptr_t)__kflash_start +#define KFLASH_SIZE (uintptr_t)__kflash_size +#define KSRAM_START (uintptr_t)__ksram_start +#define KSRAM_SIZE (uintptr_t)__ksram_size +#define KSRAM_END (uintptr_t)__ksram_end + +/* Kernel RAM (RW) */ + +#define PGPOOL_START (uintptr_t)__pgheap_start +#define PGPOOL_SIZE (uintptr_t)__pgheap_size + +/* Page pool (RWX) */ + +#define PGPOOL_START (uintptr_t)__pgheap_start +#define PGPOOL_SIZE (uintptr_t)__pgheap_size +#define PGPOOL_END (PGPOOL_START + PGPOOL_SIZE) + +/* Ramdisk */ + +#define RAMDISK_START (uintptr_t)__ramdisk_start +#define RAMDISK_SIZE (uintptr_t)__ramdisk_size + +/**************************************************************************** + * Public Data + ****************************************************************************/ + +/* Kernel code memory (RX) */ + +extern uint8_t __kflash_start[]; +extern uint8_t __kflash_size[]; + +/* Kernel RAM (RW) */ + +extern uint8_t __ksram_start[]; +extern uint8_t __ksram_size[]; +extern uint8_t __ksram_end[]; + +/* Page pool (RWX) */ + +extern uint8_t __pgheap_start[]; +extern uint8_t __pgheap_size[]; + +/* Ramdisk (RW) */ + +extern uint8_t __ramdisk_start[]; +extern uint8_t __ramdisk_size[]; + +#endif /* __BOARDS_RISC_V_QEMURV_RVVIRT_INCLUDE_BOARD_MEMORYMAP_H */ diff --git a/boards/risc-v/jh7110/star64/scripts/Make.defs b/boards/risc-v/jh7110/star64/scripts/Make.defs new file mode 100644 index 0000000000000..6ec964f2ad09c --- /dev/null +++ b/boards/risc-v/jh7110/star64/scripts/Make.defs @@ -0,0 +1,45 @@ +############################################################################ +# boards/risc-v/qemu-rv/rv-virt/scripts/Make.defs +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. The +# ASF licenses this file to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance with the +# License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# +############################################################################ + +include $(TOPDIR)/.config +include $(TOPDIR)/tools/Config.mk +include $(TOPDIR)/arch/risc-v/src/common/Toolchain.defs + +LDSCRIPT = ld.script +ARCHSCRIPT += $(BOARD_DIR)$(DELIM)scripts$(DELIM)$(LDSCRIPT) + +ARCHCPUFLAGS += -mcmodel=medany +ARCHPICFLAGS = -fpic -msingle-pic-base + +CFLAGS := $(ARCHCFLAGS) $(ARCHOPTIMIZATION) $(ARCHCPUFLAGS) $(ARCHINCLUDES) $(ARCHDEFINES) $(EXTRAFLAGS) -pipe +CPICFLAGS = $(ARCHPICFLAGS) $(CFLAGS) +CXXFLAGS := $(ARCHCXXFLAGS) $(ARCHOPTIMIZATION) $(ARCHCPUFLAGS) $(ARCHXXINCLUDES) $(ARCHDEFINES) $(EXTRAFLAGS) -pipe +CXXPICFLAGS = $(ARCHPICFLAGS) $(CXXFLAGS) +CPPFLAGS := $(ARCHINCLUDES) $(ARCHDEFINES) $(EXTRAFLAGS) +AFLAGS += $(CFLAGS) -D__ASSEMBLY__ + +# ELF module definitions + +CELFFLAGS = $(CFLAGS) +CXXELFFLAGS = $(CXXFLAGS) + +LDELFFLAGS = --oformat elf64-littleriscv +LDELFFLAGS += -r -e main +LDELFFLAGS += -T $(call CONVERT_PATH,$(TOPDIR)/binfmt/libelf/gnu-elf.ld) diff --git a/boards/risc-v/jh7110/star64/scripts/ld.script b/boards/risc-v/jh7110/star64/scripts/ld.script new file mode 100644 index 0000000000000..f168efd6b24bc --- /dev/null +++ b/boards/risc-v/jh7110/star64/scripts/ld.script @@ -0,0 +1,150 @@ +/**************************************************************************** + * boards/risc-v/qemu-rv/rv-virt/scripts/ld.script + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +MEMORY +{ + kflash (rx) : ORIGIN = 0x40200000, LENGTH = 2048K /* w/ cache */ + ksram (rwx) : ORIGIN = 0x40400000, LENGTH = 2048K /* w/ cache */ + pgram (rwx) : ORIGIN = 0x40600000, LENGTH = 4096K /* w/ cache */ + ramdisk (rwx) : ORIGIN = 0x40A00000, LENGTH = 16M /* w/ cache */ +} + +OUTPUT_ARCH("riscv") + +/* Provide the kernel boundaries */ + +__kflash_start = ORIGIN(kflash); +__kflash_size = LENGTH(kflash); +__ksram_start = ORIGIN(ksram); +__ksram_size = LENGTH(ksram); +__ksram_end = ORIGIN(ksram) + LENGTH(ksram); + +/* Page heap */ + +__pgheap_start = ORIGIN(pgram); +__pgheap_size = LENGTH(pgram) + LENGTH(ramdisk); + +/* Application ramdisk */ + +__ramdisk_start = ORIGIN(ramdisk); +__ramdisk_size = LENGTH(ramdisk); +__ramdisk_end = ORIGIN(ramdisk) + LENGTH(ramdisk); + +SECTIONS +{ + . = 0x40200000; + + .text : + { + _stext = . ; + *(.text) + *(.text.*) + *(.gnu.warning) + *(.stub) + *(.glue_7) + *(.glue_7t) + *(.jcr) + + /* C++ support: The .init and .fini sections contain specific logic + * to manage static constructors and destructors. + */ + + *(.gnu.linkonce.t.*) + *(.init) /* Old ABI */ + *(.fini) /* Old ABI */ + _etext = . ; + } + + .rodata : + { + _srodata = . ; + *(.rodata) + *(.rodata1) + *(.rodata.*) + *(.gnu.linkonce.r*) + _erodata = . ; + } + + .tdata : { + _stdata = ABSOLUTE(.); + *(.tdata .tdata.* .gnu.linkonce.td.*); + _etdata = ABSOLUTE(.); + } + + .tbss : { + _stbss = ABSOLUTE(.); + *(.tbss .tbss.* .gnu.linkonce.tb.* .tcommon); + _etbss = ABSOLUTE(.); + } + + _eronly = ABSOLUTE(.); + + .data : + { + _sdata = . ; + *(.data) + *(.data1) + *(.data.*) + *(.gnu.linkonce.d*) + . = ALIGN(4); + _edata = . ; + } + + .bss : + { + _sbss = . ; + *(.bss) + *(.bss.*) + *(.sbss) + *(.sbss.*) + *(.gnu.linkonce.b*) + *(COMMON) + _ebss = . ; + } > ksram + + /* Page tables here, align to 4K boundary */ + + .pgtables (NOLOAD) : ALIGN(0x1000) { + *(.pgtables) + . = ALIGN(4); + } > ksram + + /* Stack top */ + + .stack_top : { + . = ALIGN(32); + _ebss = ABSOLUTE(.); + } > ksram + + /* Stabs debugging sections. */ + + .stab 0 : { *(.stab) } + .stabstr 0 : { *(.stabstr) } + .stab.excl 0 : { *(.stab.excl) } + .stab.exclstr 0 : { *(.stab.exclstr) } + .stab.index 0 : { *(.stab.index) } + .stab.indexstr 0 : { *(.stab.indexstr) } + .comment 0 : { *(.comment) } + .debug_abbrev 0 : { *(.debug_abbrev) } + .debug_info 0 : { *(.debug_info) } + .debug_line 0 : { *(.debug_line) } + .debug_pubnames 0 : { *(.debug_pubnames) } + .debug_aranges 0 : { *(.debug_aranges) } +} diff --git a/boards/risc-v/jh7110/star64/src/jh7110_appinit.c b/boards/risc-v/jh7110/star64/src/jh7110_appinit.c new file mode 100644 index 0000000000000..1f1084a5ffd3d --- /dev/null +++ b/boards/risc-v/jh7110/star64/src/jh7110_appinit.c @@ -0,0 +1,167 @@ +/**************************************************************************** + * boards/risc-v/qemu-rv/rv-virt/src/qemu_rv_appinit.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* Ramdisk Definition */ + +#define SECTORSIZE 512 +#define NSECTORS(b) (((b) + SECTORSIZE - 1) / SECTORSIZE) +#define RAMDISK_DEVICE_MINOR 0 + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: mount_ramdisk + * + * Description: + * Mount a ramdisk defined in the ld.script to /dev/ramX. The ramdisk is + * intended to contain a romfs with applications which can be spawned at + * runtime. + * + * Returned Value: + * OK is returned on success. + * -ERRORNO is returned on failure. + * + ****************************************************************************/ + +int mount_ramdisk(void) +{ + int ret; + struct boardioc_romdisk_s desc; + + desc.minor = RAMDISK_DEVICE_MINOR; + desc.nsectors = NSECTORS((ssize_t)__ramdisk_size); + desc.sectsize = SECTORSIZE; + desc.image = __ramdisk_start; + + ret = boardctl(BOARDIOC_ROMDISK, (uintptr_t)&desc); + if (ret < 0) + { + syslog(LOG_ERR, "Ramdisk register failed: %s\n", strerror(errno)); + syslog(LOG_ERR, "Ramdisk mountpoint /dev/ram%d\n", + RAMDISK_DEVICE_MINOR); + syslog(LOG_ERR, "Ramdisk length %lu, origin %lx\n", + (ssize_t)__ramdisk_size, (uintptr_t)__ramdisk_start); + } + + return ret; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: board_app_initialize + * + * Description: + * Perform architecture specific initialization + * + * Input Parameters: + * arg - The boardctl() argument is passed to the board_app_initialize() + * implementation without modification. The argument has no + * meaning to NuttX; the meaning of the argument is a contract + * between the board-specific initialization logic and the + * matching application logic. The value could be such things as a + * mode enumeration value, a set of DIP switch switch settings, a + * pointer to configuration data read from a file or serial FLASH, + * or whatever you would like to do with it. Every implementation + * should accept zero/NULL as a default configuration. + * + * Returned Value: + * Zero (OK) is returned on success; a negated errno value is returned on + * any failure to indicate the nature of the failure. + * + ****************************************************************************/ + +int board_app_initialize(uintptr_t arg) +{ +#ifdef CONFIG_BOARD_LATE_INITIALIZE + /* Board initialization already performed by board_late_initialize() */ + + return OK; +#else + /* Perform board-specific initialization */ + +#ifdef CONFIG_NSH_ARCHINIT + + mount(NULL, "/proc", "procfs", 0, NULL); + +#endif + + return OK; +#endif +} + +/**************************************************************************** + * Name: board_late_initialize + * + * Description: + * If CONFIG_BOARD_LATE_INITIALIZE is selected, then an additional + * initialization call will be performed in the boot-up sequence to a + * function called board_late_initialize(). board_late_initialize() will + * be called after up_initialize() and board_early_initialize() and just + * before the initial application is started. This additional + * initialization phase may be used, for example, to initialize board- + * specific device drivers for which board_early_initialize() is not + * suitable. + * + * Waiting for events, use of I2C, SPI, etc are permissible in the context + * of board_late_initialize(). That is because board_late_initialize() + * will run on a temporary, internal kernel thread. + * + ****************************************************************************/ + +void board_late_initialize(void) +{ + /* Mount the RAM Disk */ + + mount_ramdisk(); + + /* Perform board-specific initialization */ + +#ifdef CONFIG_NSH_ARCHINIT + + mount(NULL, "/proc", "procfs", 0, NULL); + +#endif +}