Browse Source

Support device tree binary on K210 platform reference impl

luojia65 3 years ago
parent
commit
7249784ced

+ 1 - 0
CHANGELOG.md

@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 - S-level Illegal instruction exception is now delegated into S-level software handler
 - Support RFENCE extension in RustSBI framework
 - Added a test kernel to test SBI function on RustSBI implementations
+- Support device tree binary in K210 platform
 
 ### Modified
 - Function `rustsbi::ecall` now require 5 input parameters

+ 12 - 0
README.md

@@ -35,6 +35,15 @@ The test kernel is used to test SBI functions. Boot this kernel using your platf
 it will call all SBI calls and run instructions to test if underlying SBI environment is okay.
 if this kernel reports 'SUCCESS' and exits normally, it means that your SBI implementation is correct.
 
+## Frequently asked questions
+
+1. Can I use RustSBI on C based kernels?
+
+Yes, you can! RustSBI strictly follows RISC-V SBI standard. All features are prepares for all programming languages,
+as long as they support RISC-V SBI defined calling convention. 
+
+If your kernel language supports other SBI implementations, typically it will support RustSBI in the same way.
+
 ## Talks and documents
 
 This project is originally a part of rCore Summer of Code 2020 activities, now it is
@@ -70,3 +79,6 @@ This project is licensed under either of
 
 This project contains documents from [RISC-V SBI specification](https://github.com/riscv/riscv-sbi-doc)
 repository. These documents are (C) RISC-V community under CC-BY 4.0 license.
+
+Reference implementaion K210 includes Kendryte K210 DTS file from Western Digital, this file is
+(C) Western Digital Corporation or its affiliates under BSD-2-Clause license.

+ 12 - 0
platform/k210/README.md

@@ -58,3 +58,15 @@ mentioned above to this specific SBI extension.
 | Function Name | Function ID | Extension ID |
 |:-----|:----|:----|
 | sbi_rustsbi_k210_sext | 0x210 | 0x0A000004 |
+
+### Notes for implementation developers
+
+## Recompile device tree binary file
+
+Use following command:
+
+```bash
+dtc -I dts -O dtb -o kendryte-k210.dtb kendryte-k210.dts
+```
+
+You need to have `device-tree-compiler` package installed in linux before using this command.

+ 1 - 1
platform/k210/justfile

@@ -6,7 +6,7 @@ m-bin-file := build-path + "rustsbi-k210.bin"
 
 k210-serialport := "/dev/ttyS17"
 
-objdump := "rust-objdump"
+objdump := "riscv64-unknown-elf-objdump"
 objcopy := "rust-objcopy --binary-architecture=riscv64"
 
 build: firmware

BIN
platform/k210/kendryte-k210.dtb


+ 70 - 0
platform/k210/kendryte-k210.dts

@@ -0,0 +1,70 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2019 Western Digital Corporation or its affiliates.
+ *
+ * Authors:
+ *   Damien Le Moal <[email protected]>
+ */
+
+/dts-v1/;
+/ {
+	#address-cells = <2>;
+	#size-cells = <2>;
+	compatible = "kendryte,k210";
+
+	chosen {
+        bootargs = "console=hvc0 earlycon=sbi";
+	};
+
+	cpus {
+		#address-cells = <1>;
+		#size-cells = <0>;
+		cpu0: cpu@0 {
+			device_type = "cpu";
+			clock-frequency = <390000000>;
+			i-cache-size = <32768>;
+			d-cache-size = <32768>;
+			mmu-type = "none";
+			reg = <0>;
+			riscv,isa = "rv64imafdc";
+			status = "okay";
+			cpu0_intc: interrupt-controller {
+				#interrupt-cells = <1>;
+				compatible = "riscv,cpu-intc";
+				interrupt-controller;
+			};
+		};
+		cpu1: cpu@1 {
+			device_type = "cpu";
+			clock-frequency = <390000000>;
+			d-cache-size = <32768>;
+			i-cache-size = <32768>;
+			mmu-type = "none";
+			reg = <1>;
+			riscv,isa = "rv64imafdc";
+			status = "okay";
+			cpu1_intc: interrupt-controller {
+				#interrupt-cells = <1>;
+				compatible = "riscv,cpu-intc";
+				interrupt-controller;
+			};
+		};
+	};
+
+	memory@80000000 {
+		/* Bank 0: 4 MB, Bank 1: 2 MB, AI chip SRAM: 2MB */
+		device_type = "memory";
+		reg = <0x00000000 0x80000000 0x00000000 0x00800000>;
+	};
+
+	plic0: interrupt-controller@C000000 {
+		#interrupt-cells = <1>;
+		compatible = "riscv,plic0";
+		interrupt-controller;
+		interrupts-extended =
+			<&cpu0_intc 11 &cpu0_intc 9
+			 &cpu1_intc 11 &cpu1_intc 9>;
+		reg = <0x0 0xc000000 0x0 0x4000000>;
+	};
+};

+ 0 - 135
platform/k210/kendryte-k210.dtsi

@@ -1,135 +0,0 @@
-// Source: https://github.com/torvalds/linux/blob/master/arch/riscv/boot/dts/kendryte/k210.dtsi
-// SPDX-License-Identifier: GPL-2.0+
-/*
- * Copyright (C) 2019 Sean Anderson <[email protected]>
- * Copyright (C) 2020 Western Digital Corporation or its affiliates.
- */
-
-/*
-#define K210_CLK_PLL0   0
-#define K210_CLK_PLL1   0
-#define K210_CLK_ACLK   0
-#define K210_CLK_CPU    0
-*/
-
-// todo: generate clock speed in sbi binary
-
-/ {
-	/*
-	 * Although the K210 is a 64-bit CPU, the address bus is only 32-bits
-	 * wide, and the upper half of all addresses is ignored.
-	 */
-	#address-cells = <1>;
-	#size-cells = <1>;
-	compatible = "kendryte,k210";
-
-	aliases {
-		serial0 = &uarths0;
-	};
-
-	/*
-	 * The K210 has an sv39 MMU following the priviledge specification v1.9.
-	 * Since this is a non-ratified draft specification, the kernel does not
-	 * support it and the K210 support enabled only for the !MMU case.
-	 * Be consistent with this by setting the CPUs MMU type to "none".
-	 */
-    /* No, I want to use mmu in my application -- luojia65 */
-	cpus {
-		#address-cells = <1>;
-		#size-cells = <0>;
-		timebase-frequency = <7800000>;
-		cpu0: cpu@0 {
-			device_type = "cpu";
-			reg = <0>;
-			compatible = "kendryte,k210", "sifive,rocket0", "riscv";
-			riscv,isa = "rv64imafdc";
-			mmu-type = "none";
-			i-cache-size = <0x8000>;
-			i-cache-block-size = <64>;
-			d-cache-size = <0x8000>;
-			d-cache-block-size = <64>;
-			clocks = <&sysctl K210_CLK_CPU>;
-			clock-frequency = <390000000>;
-			cpu0_intc: interrupt-controller {
-				#interrupt-cells = <1>;
-				interrupt-controller;
-				compatible = "riscv,cpu-intc";
-			};
-		};
-		cpu1: cpu@1 {
-			device_type = "cpu";
-			reg = <1>;
-			compatible = "kendryte,k210", "sifive,rocket0", "riscv";
-			riscv,isa = "rv64imafdc";
-			mmu-type = "none";
-			i-cache-size = <0x8000>;
-			i-cache-block-size = <64>;
-			d-cache-size = <0x8000>;
-			d-cache-block-size = <64>;
-			clocks = <&sysctl K210_CLK_CPU>;
-			clock-frequency = <390000000>;
-			cpu1_intc: interrupt-controller {
-				#interrupt-cells = <1>;
-				interrupt-controller;
-				compatible = "riscv,cpu-intc";
-			};
-		};
-	};
-
-	sram: memory@80000000 {
-		device_type = "memory";
-		reg = <0x80000000 0x400000>,
-		      <0x80400000 0x200000>,
-		      <0x80600000 0x200000>;
-		reg-names = "sram0", "sram1", "aisram";
-	};
-
-	clocks {
-		in0: oscillator {
-			compatible = "fixed-clock";
-			#clock-cells = <0>;
-			clock-frequency = <26000000>;
-		};
-	};
-
-	soc {
-		#address-cells = <1>;
-		#size-cells = <1>;
-		compatible = "kendryte,k210-soc", "simple-bus";
-		ranges;
-		interrupt-parent = <&plic0>;
-
-		sysctl: sysctl@50440000 {
-			compatible = "kendryte,k210-sysctl", "simple-mfd";
-			reg = <0x50440000 0x1000>;
-			#clock-cells = <1>;
-		};
-
-		clint0: interrupt-controller@2000000 {
-			compatible = "riscv,clint0";
-			reg = <0x2000000 0xC000>;
-			interrupts-extended = <&cpu0_intc 3>,  <&cpu1_intc 3>;
-			clocks = <&sysctl K210_CLK_ACLK>;
-		};
-
-		plic0: interrupt-controller@c000000 {
-			#interrupt-cells = <1>;
-			interrupt-controller;
-			compatible = "kendryte,k210-plic0", "riscv,plic0";
-			reg = <0xC000000 0x4000000>;
-			interrupts-extended = <&cpu0_intc 11>, <&cpu0_intc 0xffffffff>,
-					      <&cpu1_intc 11>, <&cpu1_intc 0xffffffff>;
-			riscv,ndev = <65>;
-			riscv,max-priority = <7>;
-		};
-
-		uarths0: serial@38000000 {
-			compatible = "kendryte,k210-uarths", "sifive,uart0";
-			reg = <0x38000000 0x1000>;
-			interrupts = <33>;
-			clocks = <&sysctl K210_CLK_CPU>;
-		};
-
-		// todo: other peripherals -- luojia65
-	};
-};

+ 3 - 1
platform/k210/src/main.rs

@@ -25,6 +25,8 @@ static ALLOCATOR: LockedHeap = LockedHeap::empty();
 
 static mut DEVINTRENTRY: usize = 0;
 
+static DEVICE_TREE_BINARY: &[u8] = include_bytes!("../kendryte-k210.dtb");
+
 #[cfg(not(test))]
 #[panic_handler]
 fn panic(info: &PanicInfo) -> ! {
@@ -260,7 +262,7 @@ fn main() -> ! {
     unsafe {
         mepc::write(_s_mode_start as usize);
         mstatus::set_mpp(MPP::Supervisor);
-        enter_privileged(mhartid::read(), 0x2333333366666666);
+        enter_privileged(mhartid::read(), DEVICE_TREE_BINARY.as_ptr() as usize);
     }
 }