lupyuen opened a new pull request, #12727:
URL: https://github.com/apache/nuttx/pull/12727

   ## Summary
   
   This PR configures the BL808 and SG2000 MMU (inside T-Head C906) to cache 
the the User Text, Data and Heap.  We set the MMU Flags (Shareable, Bufferable 
and Cacheable) as [explained in this 
article](https://lupyuen.github.io/articles/plic3#appendix-mmu-caching-for-t-head-c906).
   
   This PR fixes the Slow Memory Access for NuttX Apps on BL808 and SG2000: 
https://github.com/apache/nuttx/issues/12696. With this fix, SG2000 NuttX 
CoreMark jumps from 21 to 2,423. (Close to SG2000 Debian CoreMark)
   
   We introduce 2 Kconfig Options for injecting the Extra MMU Flags into User 
Text, Data and Heap:
   
   `ARCH_MMU_UDATA_EXTRAFLAGS` ("System Type > Extra MMU flags for user .data")
   - Upper 32 bits of the Extra MMU flags for User Data and Heap. Used for 
Svpbmt
   and T-Head MMU. For example, `0x70000000` enables the Shareable,
   Bufferable and Cacheable flags for T-Head MMU.
   
   `ARCH_MMU_UTEXT_EXTRAFLAGS` ("System Type > Extra MMU flags for user .text")
   - Same as above, but for User Text
   
   ### Modified Files
   
   `arch/risc-v/Kconfig`: Added Kconfig Options `ARCH_MMU_UDATA_EXTRAFLAGS` and 
`ARCH_MMU_UTEXT_EXTRAFLAGS` for the Extra MMU Flags
   
   `arch/risc-v/src/common/riscv_addrenv.c`: Inject the Extra MMU Flags into 
User Text, Data and Heap
   
   `boards/risc-v/sg2000/milkv_duos/configs/nsh/defconfig`: Added Extra MMU 
Flags (Shareable, Bufferable and Cacheable) for SG2000 User Text, Data and Heap
   
   `boards/risc-v/bl808/ox64/configs/nsh/defconfig`, `adc/defconfig`, 
`spi/defconfig`: Added Extra MMU Flags (Shareable, Bufferable and Cacheable) 
for BL808 User Text, Data and Heap
   
   ## Impact
   
   With this PR, `ARCH_MMU_UDATA_EXTRAFLAGS` and `ARCH_MMU_UTEXT_EXTRAFLAGS` 
are exposed to all RISC-V Platforms with MMU, like `knsh64`.
   
   For Backward Compatibility: Our code `#defines` the options as 0 if they are 
missing from the NuttX Config.
   
   ## Testing
   
   We test Ox64 BL808 SBC and Milk-V Duo S SG2000 SBC with a [NOP 
Loop](https://github.com/lupyuen2/wip-nuttx-apps/pull/9/files) that iterates 
40,000,000 times:
   ```text
   nsh> hello
   Hello, World!!
   0
   [NOP Loop Delay]
   1
   ```
   - __Without MMU Caching:__ NOP Loop completes after 10 seconds
   
     [Ox64 Video](https://youtu.be/nPChSAS7CHI) / [Ox64 
Log](https://gist.github.com/lupyuen/86980e832ba70748dc037f8cf05a440b#file-ox64-mmu-delay2-log)
   
     [Duo S Video](https://youtu.be/iHLN9_drmlk) / [Duo S 
Log](https://gist.github.com/lupyuen/335cd4b03400c0b198d1d825c066ecf9#file-sg2000-mmu-delay5-log)
   
   - __With MMU Caching:__ NOP Loop completes immediately
   
     [Ox64 Video](https://youtu.be/x9g8Q2mLT4c) / [Ox64 
Log](https://gist.github.com/lupyuen/a16f9ab9f4d7d8d15284c9a55aaa4a4b)
   
     [Duo S Video](https://youtu.be/bIBIhOen7HY) / [Duo S 
Log](https://gist.github.com/lupyuen/f0b3f90c1e588f1270b8fe8c793dd93a)
   
   ### Performance Testing
   
   To measure the performance, we ran CoreMark ("Application Configuration > 
Benchmark Applications > CoreMark Benchmark"). The NuttX CoreMark Score is now 
close to Debian CoreMark:
   - [SG2000 NuttX CoreMark 
`-O2`](https://gist.github.com/lupyuen/f0b3f90c1e588f1270b8fe8c793dd93a#file-sg2000-mmu-delay6-log-L182):
 __`2,423`__
   - [SG2000 Debian CoreMark 
`-O2`](https://gist.github.com/lupyuen/b7001b870b37c8f2f618d3c4037e81a6#file-sg2000-debian-coremark-log-L1413):
 __`2,470`__
   
   Note that SG2000 NuttX [CoreMark is 
1,758](https://gist.github.com/lupyuen/eb9b4a05673326c3d675675503c74fa4#file-sg2000-nuttx-coremark8-log-L188)
 with default settings `-Os` and `-g`. So `-O2` really makes a difference. How 
we compiled CoreMark for `-O2`:
   ```bash
   rm ../apps/benchmarks/coremark/*.o
   ## Edit arch/risc-v/src/common/Toolchain.defs
   ## Change `ARCHOPTIMIZATION += -Os` to `ARCHOPTIMIZATION += -O2`
   ## Change `ARCHOPTIMIZATION += -g`  to `ARCHOPTIMIZATION +=`
   ## Refresh the NuttX Build: Re-run `make`, `make export`, `make import`, ...
   ## Note: Don't rebuild the NuttX Kernel, it won't boot with `-O2` (why?)
   ```
   
   ### Regression Testing
   
   For Regression Testing: We successfully tested OSTest on Ox64 BL808 SBC and 
Milk-V Duo S SG2000 SBC:
   ```text
   $ tools/configure.sh ox64:nsh / milkv_duos:nsh
   ...
   nsh> uname -a
   NuttX 12.4.0 0e12d8a3f28-dirty Jul 18 2024 09:22:45 risc-v ox64 / milkv_duos
   nsh> ostest
   ostest_main: Exiting with status 0
   ```
   - [Ox64 Video](https://youtu.be/x9g8Q2mLT4c) / [Ox64 
Log](https://gist.github.com/lupyuen/a16f9ab9f4d7d8d15284c9a55aaa4a4b)
   
   - [Duo S Video](https://youtu.be/bIBIhOen7HY) / [Duo S 
Log](https://gist.github.com/lupyuen/f0b3f90c1e588f1270b8fe8c793dd93a)
   
   We also tested OSTest on QEMU RISC-V 32-bit and 64-bit (Flat Build and 
Kernel Build):
   ```text
   $ tools/configure.sh rv-virt:nsh / nsh64 / knsh32 / knsh64
   ...
   nsh> uname -a
   NuttX 12.4.0 0e12d8a3f2 Jul 18 2024 01:47:14 risc-v rv-virt
   nsh> ostest
   ostest_main: Exiting with status 0
   ```
   - [rv-virt:nsh 
Log](https://github.com/lupyuen/nuttx-riscv64/actions/runs/9984139606/job/27592872340)
   - [rv-virt:nsh64 
Log](https://github.com/lupyuen/nuttx-riscv64/actions/runs/9984139611/job/27592872406)
   - [rv-virt:knsh32 
Log](https://github.com/lupyuen/nuttx-riscv64/actions/runs/9984139612/job/27592872353)
   - [rv-virt:knsh64 
Log](https://gist.github.com/lupyuen/867b0c69036054d208451daddd806362)
   
   Thanks again to @henryrov for tracking down the problem.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to