├── .clang-format ├── .editorconfig ├── .github └── workflows │ └── repo-lockdown.yml ├── .gitignore ├── CONTRIBUTORS.md ├── COPYING.BSD ├── Kconfig ├── Makefile ├── README.md ├── ThirdPartyNotices.md ├── docs ├── contributing.md ├── domain_support.md ├── doxygen.cfg ├── external │ └── coreboot.md ├── firmware │ ├── fw.md │ ├── fw_dynamic.md │ ├── fw_jump.md │ ├── fw_payload.md │ ├── payload_linux.md │ └── payload_uboot.md ├── library_usage.md ├── opensbi_config.md ├── platform │ ├── andes-ae350.md │ ├── fpga-ariane.md │ ├── fpga-openpiton.md │ ├── generic.md │ ├── nuclei_ux600.md │ ├── platform.md │ ├── qemu_virt.md │ ├── renesas-rzfive.md │ ├── shakti_cclass.md │ ├── sifive_fu540.md │ ├── spike.md │ └── thead-c9xx.md ├── platform_guide.md ├── platform_requirements.md ├── pmu_support.md ├── riscv_opensbi_logo_final_color.png ├── riscv_opensbi_logo_final_grey.png └── writing_tests.md ├── firmware ├── Kconfig ├── external_deps.mk ├── fw_base.S ├── fw_base.ldS ├── fw_dynamic.S ├── fw_dynamic.elf.ldS ├── fw_jump.S ├── fw_jump.elf.ldS ├── fw_payload.S ├── fw_payload.elf.ldS ├── objects.mk └── payloads │ ├── objects.mk │ ├── test.elf.ldS │ ├── test_head.S │ └── test_main.c ├── include ├── sbi │ ├── fw_dynamic.h │ ├── riscv_asm.h │ ├── riscv_atomic.h │ ├── riscv_barrier.h │ ├── riscv_dbtr.h │ ├── riscv_elf.h │ ├── riscv_encoding.h │ ├── riscv_fp.h │ ├── riscv_io.h │ ├── riscv_locks.h │ ├── sbi_bitmap.h │ ├── sbi_bitops.h │ ├── sbi_byteorder.h │ ├── sbi_console.h │ ├── sbi_const.h │ ├── sbi_cppc.h │ ├── sbi_csr_detect.h │ ├── sbi_dbtr.h │ ├── sbi_domain.h │ ├── sbi_domain_context.h │ ├── sbi_domain_data.h │ ├── sbi_double_trap.h │ ├── sbi_ecall.h │ ├── sbi_ecall_interface.h │ ├── sbi_emulate_csr.h │ ├── sbi_error.h │ ├── sbi_fifo.h │ ├── sbi_fwft.h │ ├── sbi_hart.h │ ├── sbi_hartmask.h │ ├── sbi_heap.h │ ├── sbi_hfence.h │ ├── sbi_hsm.h │ ├── sbi_illegal_atomic.h │ ├── sbi_illegal_insn.h │ ├── sbi_init.h │ ├── sbi_ipi.h │ ├── sbi_irqchip.h │ ├── sbi_list.h │ ├── sbi_math.h │ ├── sbi_mpxy.h │ ├── sbi_platform.h │ ├── sbi_pmu.h │ ├── sbi_scratch.h │ ├── sbi_slist.h │ ├── sbi_sse.h │ ├── sbi_string.h │ ├── sbi_system.h │ ├── sbi_timer.h │ ├── sbi_tlb.h │ ├── sbi_trap.h │ ├── sbi_trap_ldst.h │ ├── sbi_types.h │ ├── sbi_unit_test.h │ ├── sbi_unpriv.h │ ├── sbi_version.h │ └── sbi_visibility.h └── sbi_utils │ ├── fdt │ ├── fdt_domain.h │ ├── fdt_driver.h │ ├── fdt_fixup.h │ ├── fdt_helper.h │ └── fdt_pmu.h │ ├── gpio │ ├── fdt_gpio.h │ └── gpio.h │ ├── i2c │ ├── dw_i2c.h │ ├── fdt_i2c.h │ └── i2c.h │ ├── ipi │ ├── aclint_mswi.h │ ├── andes_plicsw.h │ └── fdt_ipi.h │ ├── irqchip │ ├── aplic.h │ ├── fdt_irqchip.h │ ├── imsic.h │ └── plic.h │ ├── mailbox │ ├── fdt_mailbox.h │ ├── mailbox.h │ ├── rpmi_mailbox.h │ └── rpmi_msgprot.h │ ├── mpxy │ ├── fdt_mpxy.h │ └── fdt_mpxy_rpmi_mbox.h │ ├── regmap │ ├── fdt_regmap.h │ └── regmap.h │ ├── serial │ ├── cadence-uart.h │ ├── fdt_serial.h │ ├── gaisler-uart.h │ ├── litex-uart.h │ ├── renesas-scif.h │ ├── semihosting.h │ ├── shakti-uart.h │ ├── sifive-uart.h │ ├── uart8250.h │ └── xlnx_uartlite.h │ ├── sys │ ├── atcsmu.h │ └── htif.h │ └── timer │ ├── aclint_mtimer.h │ ├── andes_plmt.h │ └── fdt_timer.h ├── lib ├── sbi │ ├── Kconfig │ ├── objects.mk │ ├── riscv_asm.c │ ├── riscv_atomic.c │ ├── riscv_hardfp.S │ ├── riscv_locks.c │ ├── sbi_bitmap.c │ ├── sbi_bitops.c │ ├── sbi_console.c │ ├── sbi_cppc.c │ ├── sbi_dbtr.c │ ├── sbi_domain.c │ ├── sbi_domain_context.c │ ├── sbi_domain_data.c │ ├── sbi_double_trap.c │ ├── sbi_ecall.c │ ├── sbi_ecall_base.c │ ├── sbi_ecall_cppc.c │ ├── sbi_ecall_dbcn.c │ ├── sbi_ecall_dbtr.c │ ├── sbi_ecall_exts.carray │ ├── sbi_ecall_fwft.c │ ├── sbi_ecall_hsm.c │ ├── sbi_ecall_ipi.c │ ├── sbi_ecall_legacy.c │ ├── sbi_ecall_mpxy.c │ ├── sbi_ecall_pmu.c │ ├── sbi_ecall_rfence.c │ ├── sbi_ecall_srst.c │ ├── sbi_ecall_sse.c │ ├── sbi_ecall_susp.c │ ├── sbi_ecall_time.c │ ├── sbi_ecall_vendor.c │ ├── sbi_emulate_csr.c │ ├── sbi_expected_trap.S │ ├── sbi_fifo.c │ ├── sbi_fwft.c │ ├── sbi_hart.c │ ├── sbi_heap.c │ ├── sbi_hfence.S │ ├── sbi_hsm.c │ ├── sbi_illegal_atomic.c │ ├── sbi_illegal_insn.c │ ├── sbi_init.c │ ├── sbi_ipi.c │ ├── sbi_irqchip.c │ ├── sbi_math.c │ ├── sbi_mpxy.c │ ├── sbi_platform.c │ ├── sbi_pmu.c │ ├── sbi_scratch.c │ ├── sbi_sse.c │ ├── sbi_string.c │ ├── sbi_system.c │ ├── sbi_timer.c │ ├── sbi_tlb.c │ ├── sbi_trap.c │ ├── sbi_trap_ldst.c │ ├── sbi_trap_v_ldst.c │ ├── sbi_unpriv.c │ └── tests │ │ ├── objects.mk │ │ ├── riscv_atomic_test.c │ │ ├── riscv_locks_test.c │ │ ├── sbi_bitmap_test.c │ │ ├── sbi_bitops_test.c │ │ ├── sbi_console_test.c │ │ ├── sbi_ecall_test.c │ │ ├── sbi_math_test.c │ │ ├── sbi_unit_test.c │ │ └── sbi_unit_tests.carray └── utils │ ├── Kconfig │ ├── cppc │ ├── Kconfig │ ├── fdt_cppc_rpmi.c │ └── objects.mk │ ├── fdt │ ├── Kconfig │ ├── fdt_domain.c │ ├── fdt_driver.c │ ├── fdt_early_drivers.carray │ ├── fdt_fixup.c │ ├── fdt_helper.c │ ├── fdt_pmu.c │ └── objects.mk │ ├── gpio │ ├── Kconfig │ ├── fdt_gpio.c │ ├── fdt_gpio_designware.c │ ├── fdt_gpio_drivers.carray │ ├── fdt_gpio_sifive.c │ ├── fdt_gpio_starfive.c │ ├── gpio.c │ └── objects.mk │ ├── hsm │ ├── Kconfig │ ├── fdt_hsm_rpmi.c │ └── objects.mk │ ├── i2c │ ├── Kconfig │ ├── dw_i2c.c │ ├── fdt_i2c.c │ ├── fdt_i2c_adapter_drivers.carray │ ├── fdt_i2c_dw.c │ ├── fdt_i2c_sifive.c │ ├── i2c.c │ └── objects.mk │ ├── ipi │ ├── Kconfig │ ├── aclint_mswi.c │ ├── andes_plicsw.c │ ├── fdt_ipi.c │ ├── fdt_ipi_drivers.carray │ ├── fdt_ipi_mswi.c │ ├── fdt_ipi_plicsw.c │ └── objects.mk │ ├── irqchip │ ├── Kconfig │ ├── aplic.c │ ├── fdt_irqchip.c │ ├── fdt_irqchip_aplic.c │ ├── fdt_irqchip_drivers.carray │ ├── fdt_irqchip_imsic.c │ ├── fdt_irqchip_plic.c │ ├── imsic.c │ ├── objects.mk │ └── plic.c │ ├── libfdt │ ├── .clang-format │ ├── Kconfig │ ├── Makefile.libfdt │ ├── TODO │ ├── fdt.c │ ├── fdt.h │ ├── fdt_addresses.c │ ├── fdt_check.c │ ├── fdt_empty_tree.c │ ├── fdt_overlay.c │ ├── fdt_ro.c │ ├── fdt_rw.c │ ├── fdt_strerror.c │ ├── fdt_sw.c │ ├── fdt_wip.c │ ├── libfdt.h │ ├── libfdt_env.h │ ├── libfdt_internal.h │ ├── objects.mk │ └── version.lds │ ├── libquad │ ├── divdi3.c │ ├── include │ │ ├── limits.h │ │ └── sys │ │ │ ├── cdefs.h │ │ │ └── types.h │ ├── moddi3.c │ ├── objects.mk │ ├── qdivrem.c │ ├── quad.h │ ├── udivdi3.c │ └── umoddi3.c │ ├── mailbox │ ├── Kconfig │ ├── fdt_mailbox.c │ ├── fdt_mailbox_drivers.carray │ ├── fdt_mailbox_rpmi_shmem.c │ ├── mailbox.c │ ├── objects.mk │ └── rpmi_mailbox.c │ ├── mpxy │ ├── Kconfig │ ├── fdt_mpxy.c │ ├── fdt_mpxy_drivers.carray │ ├── fdt_mpxy_rpmi_clock.c │ ├── fdt_mpxy_rpmi_mbox.c │ ├── fdt_mpxy_rpmi_sysmsi.c │ └── objects.mk │ ├── regmap │ ├── Kconfig │ ├── fdt_regmap.c │ ├── fdt_regmap_drivers.carray │ ├── fdt_regmap_syscon.c │ ├── objects.mk │ └── regmap.c │ ├── reset │ ├── Kconfig │ ├── fdt_reset_atcwdt200.c │ ├── fdt_reset_gpio.c │ ├── fdt_reset_htif.c │ ├── fdt_reset_rpmi.c │ ├── fdt_reset_sg2042_hwmon_mcu.c │ ├── fdt_reset_sunxi_wdt.c │ ├── fdt_reset_syscon.c │ └── objects.mk │ ├── serial │ ├── Kconfig │ ├── cadence-uart.c │ ├── fdt_serial.c │ ├── fdt_serial_cadence.c │ ├── fdt_serial_drivers.carray │ ├── fdt_serial_gaisler.c │ ├── fdt_serial_htif.c │ ├── fdt_serial_litex.c │ ├── fdt_serial_renesas_scif.c │ ├── fdt_serial_shakti.c │ ├── fdt_serial_sifive.c │ ├── fdt_serial_uart8250.c │ ├── fdt_serial_xlnx_uartlite.c │ ├── gaisler-uart.c │ ├── litex-uart.c │ ├── objects.mk │ ├── renesas_scif.c │ ├── semihosting.c │ ├── shakti-uart.c │ ├── sifive-uart.c │ ├── uart8250.c │ └── xlnx-uartlite.c │ ├── suspend │ ├── Kconfig │ ├── fdt_suspend_rpmi.c │ └── objects.mk │ ├── sys │ ├── Kconfig │ ├── atcsmu.c │ ├── htif.c │ └── objects.mk │ └── timer │ ├── Kconfig │ ├── aclint_mtimer.c │ ├── andes_plmt.c │ ├── fdt_timer.c │ ├── fdt_timer_drivers.carray │ ├── fdt_timer_mtimer.c │ ├── fdt_timer_plmt.c │ └── objects.mk ├── platform ├── fpga │ ├── ariane │ │ ├── Kconfig │ │ ├── configs │ │ │ └── defconfig │ │ ├── objects.mk │ │ └── platform.c │ └── openpiton │ │ ├── Kconfig │ │ ├── configs │ │ └── defconfig │ │ ├── objects.mk │ │ └── platform.c ├── generic │ ├── Kconfig │ ├── allwinner │ │ ├── objects.mk │ │ └── sun20i-d1.c │ ├── andes │ │ ├── Kconfig │ │ ├── ae350.c │ │ ├── andes_pma.c │ │ ├── andes_pmu.c │ │ ├── andes_sbi.c │ │ ├── objects.mk │ │ └── sleep.S │ ├── configs │ │ └── defconfig │ ├── include │ │ ├── andes │ │ │ ├── andes.h │ │ │ ├── andes_pma.h │ │ │ ├── andes_pmu.h │ │ │ └── andes_sbi.h │ │ ├── platform_override.h │ │ └── thead │ │ │ ├── c9xx_encoding.h │ │ │ ├── c9xx_errata.h │ │ │ └── c9xx_pmu.h │ ├── objects.mk │ ├── platform.c │ ├── platform_override_modules.carray │ ├── renesas │ │ └── rzfive │ │ │ ├── objects.mk │ │ │ └── rzfive.c │ ├── sifive │ │ ├── fu540.c │ │ ├── fu740.c │ │ └── objects.mk │ ├── sophgo │ │ ├── objects.mk │ │ └── sg2042.c │ ├── starfive │ │ ├── jh7110.c │ │ └── objects.mk │ └── thead │ │ ├── Kconfig │ │ ├── objects.mk │ │ ├── thead-generic.c │ │ ├── thead_c9xx_errata_tlb_flush.c │ │ ├── thead_c9xx_pmu.c │ │ └── thead_c9xx_tlb_trap_handler.S ├── kendryte │ └── k210 │ │ ├── Kconfig │ │ ├── configs │ │ └── defconfig │ │ ├── k210.dts │ │ ├── objects.mk │ │ ├── platform.c │ │ └── platform.h ├── nuclei │ └── ux600 │ │ ├── Kconfig │ │ ├── configs │ │ └── defconfig │ │ ├── objects.mk │ │ └── platform.c └── template │ ├── Kconfig │ ├── configs │ └── defconfig │ ├── objects.mk │ └── platform.c └── scripts ├── Kconfiglib ├── LICENSE.txt ├── allnoconfig.py ├── allyesconfig.py ├── defconfig.py ├── genconfig.py ├── kconfiglib.py ├── menuconfig.py ├── oldconfig.py ├── olddefconfig.py ├── savedefconfig.py └── setconfig.py ├── carray.sh ├── create-binary-archive.sh └── d2c.sh /.clang-format: -------------------------------------------------------------------------------- 1 | AlignConsecutiveAssignments: true 2 | AlignEscapedNewlines: Left 3 | AlignTrailingComments: true 4 | AllowShortFunctionsOnASingleLine: None 5 | BraceWrapping: 6 | AfterFunction: true 7 | BreakBeforeBraces: Custom 8 | BreakStringLiterals: false 9 | ContinuationIndentWidth: 8 10 | Cpp11BracedListStyle: false 11 | IndentWidth: 8 12 | ReflowComments: false 13 | SortIncludes: false 14 | SpacesInContainerLiterals: false 15 | TabWidth: 8 16 | UseTab: Always 17 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # SPDX-License-Identifier: GPL-2.0-only 2 | # See here for more information about the format and editor support: 3 | # https://editorconfig.org/ 4 | 5 | root = true 6 | 7 | [{*.{c,dts,h,lds,ldS,mk,s,S},Kconfig,Makefile,Makefile.*}] 8 | charset = utf-8 9 | end_of_line = lf 10 | trim_trailing_whitespace = true 11 | insert_final_newline = true 12 | indent_style = tab 13 | indent_size = 8 14 | 15 | [*.py] 16 | charset = utf-8 17 | end_of_line = lf 18 | trim_trailing_whitespace = true 19 | insert_final_newline = true 20 | indent_style = space 21 | indent_size = 4 22 | -------------------------------------------------------------------------------- /.github/workflows/repo-lockdown.yml: -------------------------------------------------------------------------------- 1 | name: 'Repo Lockdown' 2 | 3 | on: 4 | pull_request_target: 5 | types: opened 6 | 7 | permissions: 8 | pull-requests: write 9 | 10 | jobs: 11 | action: 12 | runs-on: ubuntu-latest 13 | steps: 14 | - uses: dessant/repo-lockdown@v4 15 | with: 16 | pr-comment: | 17 | We have mailing list based patch review so it would be great if you can send these patchs to OpenSBI mailing list. 18 | 19 | You need to join OpenSBI mailing list using following link 20 | http://lists.infradead.org/mailman/listinfo/opensbi 21 | 22 | Make sure you use "git send-email" to send the patches. 23 | 24 | Thanks for your contribution to OpenSBI project. 25 | lock-pr: true 26 | close-pr: true 27 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # ignore anything begin with dot 2 | .* 3 | 4 | # exceptions we need even begin with dot 5 | !.clang-format 6 | !.gitignore 7 | !.github 8 | !.editorconfig 9 | 10 | # Object files 11 | *.o 12 | *.a 13 | *.dep 14 | 15 | #Build & install directories 16 | build/ 17 | install/ 18 | 19 | # Development friendly files 20 | tags 21 | cscope* 22 | *~ 23 | -------------------------------------------------------------------------------- /CONTRIBUTORS.md: -------------------------------------------------------------------------------- 1 | 2 | List of OpenSBI Contributors (Alphabetically sorted) 3 | ==================================================== 4 | 5 | * **[Western Digital Corporation](https://www.wdc.com/)** 6 | * Project initiator and maintainer 7 | * Copyright (c) 2019 Western Digital Corporation or its affiliates 8 | 9 | * Alistair Francis 10 | 11 | * Andreas Schwab 12 | 13 | * Anup Patel 14 | 15 | * Atish Patra 16 | 17 | * Bin Meng 18 | 19 | * Damien Le Moal 20 | 21 | * Karsten Merker 22 | 23 | * Nick Kossifidis 24 | 25 | * Shawn Chang 26 | 27 | * Xiang Wang 28 | -------------------------------------------------------------------------------- /COPYING.BSD: -------------------------------------------------------------------------------- 1 | The 2-Clause BSD License 2 | SPDX short identifier: BSD-2-Clause 3 | 4 | Copyright (c) 2019 Western Digital Corporation or its affiliates and other 5 | contributors. 6 | 7 | Redistribution and use in source and binary forms, with or without 8 | modification, are permitted provided that the following conditions are met: 9 | 10 | 1. Redistributions of source code must retain the above copyright notice, this 11 | list of conditions and the following disclaimer. 12 | 2. Redistributions in binary form must reproduce the above copyright notice, 13 | this list of conditions and the following disclaimer in the documentation 14 | and/or other materials provided with the distribution. 15 | 16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 20 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | -------------------------------------------------------------------------------- /Kconfig: -------------------------------------------------------------------------------- 1 | # SPDX-License-Identifier: BSD-2-Clause 2 | 3 | mainmenu "OpenSBI $(OPENSBI_PLATFORM) Configuration" 4 | 5 | config OPENSBI_SRC_DIR 6 | string 7 | option env="OPENSBI_SRC_DIR" 8 | 9 | config OPENSBI_PLATFORM 10 | string 11 | option env="OPENSBI_PLATFORM" 12 | 13 | config OPENSBI_PLATFORM_SRC_DIR 14 | string 15 | option env="OPENSBI_PLATFORM_SRC_DIR" 16 | 17 | menu "Platform Options" 18 | source "$(OPENSBI_PLATFORM_SRC_DIR)/Kconfig" 19 | endmenu 20 | 21 | source "$(OPENSBI_SRC_DIR)/lib/sbi/Kconfig" 22 | 23 | source "$(OPENSBI_SRC_DIR)/lib/utils/Kconfig" 24 | 25 | source "$(OPENSBI_SRC_DIR)/firmware/Kconfig" 26 | -------------------------------------------------------------------------------- /ThirdPartyNotices.md: -------------------------------------------------------------------------------- 1 | Copyright (c) 2019 Western Digital Corporation or its affiliates. 2 | 3 | Third Party Notices 4 | =================== 5 | 6 | This project includes or partly uses code from the following open source 7 | software subject to the following open source licenses. 8 | 9 | libfdt 10 | ------ 11 | 12 | Copyright (C) 2016 Free Electrons 13 | Copyright (C) 2016 NextThing Co. 14 | 15 | The libfdt source code is disjunctively dual licensed (GPL-2.0+ or 16 | BSD-2-Clause). Some of this project code is used in OpenSBI under the terms of 17 | the BSD 2-Clause license. The full text of this license can be found in the 18 | file [COPYING.BSD](COPYING.BSD). 19 | -------------------------------------------------------------------------------- /docs/external/coreboot.md: -------------------------------------------------------------------------------- 1 | OpenSBI as coreboot payload 2 | =========================== 3 | 4 | [coreboot] is a free/libre and open source firmware platform support multiple 5 | hardware architectures(x86, ARMv7, arm64, PowerPC64, MIPS and RISC-V) and 6 | diverse hardware models. In RISC-V world, coreboot currently support HiFive 7 | Unleashed with OpenSBI as a payload to boot GNU/Linux: 8 | 9 | ``` 10 | SiFive HiFive unleashed's original firmware boot process: 11 | +-----------+ 12 | +------+ +------+ +------+ | BBL | 13 | | MSEL |--->| ZSBL |--->| FSBL |--->| +-------+ 14 | +------+ +------+ +------+ | | linux | 15 | +---+-------+ 16 | 17 | coreboot boot process: 18 | +---------------------------------------------------------------------+ 19 | | coreboot | 20 | +------+ +------+ | +-----------+ +----------+ +----------+ +-----------------------+ 21 | | MSEL |-->| ZSBL |-->| | bootblock |->| romstage |->| ramstage |->| payload ( OpenSBI) | 22 | +------+ +------+ | +-----------+ +----------+ +----------+ | +-------+ | 23 | | | | linux | | 24 | +---------------------------------------------+-------------+-------+-+ 25 | ``` 26 | 27 | The upstreaming work is still in progress. There's a [documentation] about how 28 | to build [out-of-tree code] to load OpenSBI. 29 | 30 | [coreboot]: https://www.coreboot.org/ 31 | [documentation]: https://github.com/hardenedlinux/embedded-iot_profile/blob/master/docs/riscv/hifiveunleashed_coreboot_notes-en.md 32 | [out-of-tree code]: https://github.com/hardenedlinux/coreboot-HiFiveUnleashed 33 | -------------------------------------------------------------------------------- /docs/firmware/fw_dynamic.md: -------------------------------------------------------------------------------- 1 | OpenSBI Firmware with Dynamic Information (FW_DYNAMIC) 2 | ====================================================== 3 | 4 | OpenSBI **firmware with dynamic info (FW_DYNAMIC)** is a firmware which gets 5 | information about next booting stage (e.g. a bootloader or an OS) and runtime 6 | OpenSBI library options from previous booting stage. 7 | 8 | The previous booting stage will pass information to *FW_DYNAMIC* by creating 9 | *struct fw_dynamic_info* in memory and passing its address to *FW_DYNAMIC* 10 | via *a2* register of RISC-V CPU. The address must be aligned to 8 bytes on 11 | RV64 and 4 bytes on RV32. 12 | 13 | A *FW_DYNAMIC* firmware is particularly useful when the booting stage executed 14 | prior to OpenSBI firmware is capable of loading both the OpenSBI firmware and 15 | the booting stage binary to follow OpenSBI firmware. 16 | 17 | *FW_DYNAMIC* Compilation 18 | ------------------------ 19 | 20 | A platform can enable *FW_DYNAMIC* firmware using any of the following methods. 21 | 22 | 1. Specifying `FW_DYNAMIC=y` on the top level `make` command line. 23 | 2. Specifying `FW_DYNAMIC=y` in the target platform *objects.mk* configuration 24 | file. 25 | 26 | The compiled *FW_DYNAMIC* firmware ELF file is named *fw_dynamic.elf*. It's 27 | expanded image file is *fw_dynamic.bin*. Both files are created in the platform 28 | specific build directory under the *build/platform//firmware* 29 | directory. 30 | 31 | *FW_DYNAMIC* Firmware Configuration Options 32 | ------------------------------------------- 33 | 34 | The *FW_DYNAMIC* firmware does not require any platform specific configuration 35 | parameters because all required information is passed by previous booting stage 36 | at runtime via *struct fw_dynamic_info*. 37 | -------------------------------------------------------------------------------- /docs/firmware/payload_linux.md: -------------------------------------------------------------------------------- 1 | Linux as a direct payload to OpenSBI 2 | ==================================== 3 | 4 | OpenSBI has the capability to load a Linux kernel image directly in supervisor 5 | mode. The flattened image generated by the Linux kernel build process can be 6 | provided as a payload to OpenSBI. 7 | 8 | Detailed examples can be found in both the [QEMU](../platform/qemu_virt.md) 9 | and the [HiFive Unleashed](../platform/sifive_fu540.md) platform guides. 10 | -------------------------------------------------------------------------------- /docs/firmware/payload_uboot.md: -------------------------------------------------------------------------------- 1 | U-Boot as a payload to OpenSBI 2 | ============================== 3 | 4 | [U-Boot](https://www.denx.de/wiki/U-Boot) is an open-source primary boot loader. 5 | It can be used as first and/or second stage boot loader in an embedded 6 | environment. In the context of OpenSBI, U-Boot can be specified as a payload to 7 | the OpenSBI firmware, becoming the boot stage following the OpenSBI firmware 8 | execution. 9 | 10 | Building and Generating U-Boot images 11 | ===================================== 12 | Please refer to the U-Boot build documentation for detailed instructions on 13 | how to build U-Boot image and boot high level operating systems from U-Boot 14 | prompt. 15 | 16 | -------------------------------------------------------------------------------- /docs/platform/fpga-ariane.md: -------------------------------------------------------------------------------- 1 | Ariane FPGA SoC Platform 2 | ======================== 3 | Ariane is a 6-stage, single issue, in-order CPU which implements the 64-bit 4 | RISC-V instruction set. The Ariane FPGA development platform is based on FPGA 5 | SoC (which currently supports only Genesys 2 board) and is capable of running 6 | Linux. 7 | 8 | The FPGA SoC currently contains the following peripherals: 9 | - DDR3 memory controller 10 | - SPI controller to connect to an SDCard 11 | - Ethernet controller 12 | - JTAG port (see debugging section below) 13 | - Bootrom containing zero stage bootloader and device tree. 14 | 15 | To build platform specific library and firmwares, provide the 16 | *PLATFORM=fpga/ariane* parameter to the top level `make` command. 17 | 18 | Platform Options 19 | ---------------- 20 | 21 | The *Ariane FPGA* platform does not have any platform-specific options. 22 | 23 | Building Ariane FPGA Platform 24 | ----------------------------- 25 | 26 | **Linux Kernel Payload** 27 | 28 | ``` 29 | make PLATFORM=fpga/ariane FW_PAYLOAD_PATH=/arch/riscv/boot/Image 30 | ``` 31 | 32 | Booting Ariane FPGA Platform 33 | ---------------------------- 34 | 35 | **Linux Kernel Payload** 36 | 37 | As Linux kernel image is embedded in the OpenSBI firmware binary, Ariane will 38 | directly boot into Linux directly after powered on. 39 | -------------------------------------------------------------------------------- /docs/platform/fpga-openpiton.md: -------------------------------------------------------------------------------- 1 | OpenPiton FPGA SoC Platform 2 | ======================== 3 | OpenPiton is the world's first open source, general purpose, multithreaded 4 | manycore processor. It is a tiled manycore framework scalable from one to 5 | 1/2 billion cores. Currently, OpenPiton supports the 64bit Ariane RISC-V 6 | processor from ETH Zurich. To this end, Ariane has been equipped with a 7 | different L1 cache subsystem that follows a write-through protocol and that has 8 | support for cache invalidations and atomics. 9 | 10 | To build platform specific library and firmwares, provide the 11 | *PLATFORM=fpga/openpiton* parameter to the top level `make` command. 12 | 13 | Platform Options 14 | ---------------- 15 | 16 | The *OpenPiton* platform does not have any platform-specific options. 17 | 18 | Building Ariane FPGA Platform 19 | ----------------------------- 20 | 21 | **Linux Kernel Payload** 22 | 23 | ``` 24 | make PLATFORM=fpga/openpiton FW_PAYLOAD_PATH=/arch/riscv/boot/Image 25 | ``` 26 | 27 | Booting Ariane FPGA Platform 28 | ---------------------------- 29 | 30 | **Linux Kernel Payload** 31 | 32 | As Linux kernel image is embedded in the OpenSBI firmware binary, Ariane will 33 | directly boot into Linux directly after powered on. 34 | -------------------------------------------------------------------------------- /docs/platform/nuclei_ux600.md: -------------------------------------------------------------------------------- 1 | 2 | Nuclei UX600 Platform 3 | ===================== 4 | 5 | The **Nuclei UX600** is a 64-bit RISC-V Core which is capable of running Linux. 6 | 7 | > Nuclei UX600: single core, pipeline as single-issue and 6~9 variable stages, in-order dispatch and out-of-order write-back, running up to >1.2GHz 8 | 9 | To build the platform-specific library and firmware images, provide the 10 | *PLATFORM=nuclei/ux600* parameter to the top level `make` command. 11 | 12 | Platform Options 13 | ---------------- 14 | 15 | The *Nuclei UX600* platform does not have any platform-specific options. 16 | 17 | Building Nuclei UX600 Platform 18 | ------------------------------ 19 | 20 | ``` 21 | make PLATFORM=nuclei/ux600 clean all 22 | ``` 23 | -------------------------------------------------------------------------------- /docs/platform/shakti_cclass.md: -------------------------------------------------------------------------------- 1 | Shakti C-class SoC Platform 2 | =========================== 3 | C-Class is a member of the SHAKTI family of processors from 4 | Indian Institute of Technology - Madras (IIT-M). 5 | 6 | It is an extremely configurable and commercial-grade 5-stage 7 | in-order core supporting the standard RV64GCSUN ISA extensions. 8 | 9 | For more details, refer: 10 | * https://gitlab.com/shaktiproject/cores/c-class/blob/master/README.md 11 | * https://c-class.readthedocs.io/en/latest 12 | * https://shakti.org.in 13 | 14 | Platform Options 15 | ---------------- 16 | 17 | The *Shakti C-class SoC* platform does not have any platform-specific 18 | options. 19 | 20 | Building Shakti C-class Platform 21 | -------------------------------- 22 | 23 | **Linux Kernel Payload** 24 | 25 | ``` 26 | make PLATFORM=generic FW_PAYLOAD_PATH=/arch/riscv/boot/Image FW_FDT_PATH= 27 | ``` 28 | 29 | **Test Payload** 30 | 31 | ``` 32 | make PLATFORM=generic FW_FDT_PATH= 33 | ``` 34 | -------------------------------------------------------------------------------- /docs/platform/thead-c9xx.md: -------------------------------------------------------------------------------- 1 | T-HEAD C9xx Series Processors 2 | ============================= 3 | 4 | The C9xx series processors are high-performance RISC-V architecture 5 | multi-core processors with AI vector acceleration engine. 6 | 7 | For more details, refer [T-HEAD.CN](https://www.t-head.cn/) 8 | 9 | To build the platform-specific library and firmware images, provide the 10 | *PLATFORM=generic* parameter to the top level `make` command. 11 | 12 | Platform Options 13 | ---------------- 14 | 15 | The T-HEAD C9xx does not have any platform-specific compile options 16 | because it uses generic platform. 17 | 18 | ``` 19 | CROSS_COMPILE=riscv64-linux-gnu- PLATFORM=generic make 20 | ``` 21 | 22 | Here is the simplest boot flow for a fpga prototype: 23 | 24 | (Jtag gdbinit) -> (zsb) -> (opensbi) -> (linux) 25 | 26 | For more details, refer: 27 | [zero stage boot](https://github.com/c-sky/zero_stage_boot) 28 | -------------------------------------------------------------------------------- /docs/riscv_opensbi_logo_final_color.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/riscv-software-src/opensbi/69a0f0245f39ea3af4685cab4cb2dda90acd17cd/docs/riscv_opensbi_logo_final_color.png -------------------------------------------------------------------------------- /docs/riscv_opensbi_logo_final_grey.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/riscv-software-src/opensbi/69a0f0245f39ea3af4685cab4cb2dda90acd17cd/docs/riscv_opensbi_logo_final_grey.png -------------------------------------------------------------------------------- /firmware/Kconfig: -------------------------------------------------------------------------------- 1 | # SPDX-License-Identifier: BSD-2-Clause 2 | -------------------------------------------------------------------------------- /firmware/external_deps.mk: -------------------------------------------------------------------------------- 1 | # 2 | # SPDX-License-Identifier: BSD-2-Clause 3 | # 4 | # Copyright (c) 2019 Western Digital Corporation or its affiliates. 5 | # 6 | # Authors: 7 | # Anup Patel 8 | # 9 | 10 | $(platform_build_dir)/firmware/fw_dynamic.o: $(FW_FDT_PATH) 11 | $(platform_build_dir)/firmware/fw_jump.o: $(FW_FDT_PATH) 12 | $(platform_build_dir)/firmware/fw_payload.o: $(FW_FDT_PATH) 13 | 14 | $(platform_build_dir)/firmware/fw_payload.o: $(FW_PAYLOAD_PATH_FINAL) 15 | -------------------------------------------------------------------------------- /firmware/fw_dynamic.elf.ldS: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2019 Western Digital Corporation or its affiliates. 5 | * 6 | * Authors: 7 | * Anup Patel 8 | */ 9 | 10 | OUTPUT_ARCH(riscv) 11 | ENTRY(_start) 12 | 13 | SECTIONS 14 | { 15 | #include "fw_base.ldS" 16 | 17 | PROVIDE(_fw_reloc_end = .); 18 | } 19 | -------------------------------------------------------------------------------- /firmware/fw_jump.elf.ldS: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2019 Western Digital Corporation or its affiliates. 5 | * 6 | * Authors: 7 | * Anup Patel 8 | */ 9 | 10 | OUTPUT_ARCH(riscv) 11 | ENTRY(_start) 12 | 13 | SECTIONS 14 | { 15 | #include "fw_base.ldS" 16 | 17 | PROVIDE(_fw_reloc_end = .); 18 | } 19 | -------------------------------------------------------------------------------- /firmware/fw_payload.elf.ldS: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2019 Western Digital Corporation or its affiliates. 5 | * 6 | * Authors: 7 | * Anup Patel 8 | */ 9 | 10 | OUTPUT_ARCH(riscv) 11 | ENTRY(_start) 12 | 13 | SECTIONS 14 | { 15 | #include "fw_base.ldS" 16 | 17 | #ifdef FW_PAYLOAD_OFFSET 18 | . = FW_TEXT_START + FW_PAYLOAD_OFFSET; 19 | #else 20 | . = ALIGN(FW_PAYLOAD_ALIGN); 21 | #endif 22 | 23 | .payload : 24 | { 25 | PROVIDE(_payload_start = .); 26 | *(.payload) 27 | . = ALIGN(8); 28 | PROVIDE(_payload_end = .); 29 | } 30 | 31 | PROVIDE(_fw_reloc_end = .); 32 | } 33 | -------------------------------------------------------------------------------- /firmware/payloads/objects.mk: -------------------------------------------------------------------------------- 1 | # 2 | # SPDX-License-Identifier: BSD-2-Clause 3 | # 4 | # Copyright (c) 2019 Western Digital Corporation or its affiliates. 5 | # 6 | # Authors: 7 | # Anup Patel 8 | # 9 | 10 | firmware-bins-$(FW_PAYLOAD) += payloads/test.bin 11 | 12 | test-y += test_head.o 13 | test-y += test_main.o 14 | 15 | %/test.o: $(foreach obj,$(test-y),%/$(obj)) 16 | $(call merge_objs,$@,$^) 17 | 18 | %/test.dep: $(foreach dep,$(test-y:.o=.dep),%/$(dep)) 19 | $(call merge_deps,$@,$^) 20 | -------------------------------------------------------------------------------- /firmware/payloads/test.elf.ldS: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2019 Western Digital Corporation or its affiliates. 5 | * 6 | * Authors: 7 | * Anup Patel 8 | */ 9 | 10 | OUTPUT_ARCH(riscv) 11 | ENTRY(_start) 12 | 13 | SECTIONS 14 | { 15 | #ifdef FW_PAYLOAD_OFFSET 16 | . = FW_TEXT_START + FW_PAYLOAD_OFFSET; 17 | #else 18 | . = ALIGN(FW_PAYLOAD_ALIGN); 19 | #endif 20 | 21 | PROVIDE(_payload_start = .); 22 | 23 | . = ALIGN(0x1000); /* Need this to create proper sections */ 24 | 25 | /* Beginning of the code section */ 26 | 27 | .text : 28 | { 29 | PROVIDE(_text_start = .); 30 | *(.entry) 31 | *(.text) 32 | . = ALIGN(8); 33 | PROVIDE(_text_end = .); 34 | } 35 | 36 | /* End of the code sections */ 37 | 38 | . = ALIGN(0x1000); /* Ensure next section is page aligned */ 39 | 40 | /* Beginning of the read-only data sections */ 41 | 42 | .rodata : 43 | { 44 | PROVIDE(_rodata_start = .); 45 | *(.rodata .rodata.*) 46 | . = ALIGN(8); 47 | PROVIDE(_rodata_end = .); 48 | } 49 | 50 | /* End of the read-only data sections */ 51 | 52 | . = ALIGN(0x1000); /* Ensure next section is page aligned */ 53 | 54 | /* Beginning of the read-write data sections */ 55 | 56 | .data : 57 | { 58 | PROVIDE(_data_start = .); 59 | 60 | *(.data) 61 | *(.data.*) 62 | *(.readmostly.data) 63 | *(*.data) 64 | . = ALIGN(8); 65 | 66 | PROVIDE(_data_end = .); 67 | } 68 | 69 | . = ALIGN(0x1000); /* Ensure next section is page aligned */ 70 | 71 | .bss : 72 | { 73 | PROVIDE(_bss_start = .); 74 | *(.bss) 75 | *(.bss.*) 76 | . = ALIGN(8); 77 | PROVIDE(_bss_end = .); 78 | } 79 | 80 | /* End of the read-write data sections */ 81 | 82 | . = ALIGN(0x1000); /* Need this to create proper sections */ 83 | 84 | PROVIDE(_payload_end = .); 85 | } 86 | -------------------------------------------------------------------------------- /firmware/payloads/test_main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2019 Western Digital Corporation or its affiliates. 5 | * 6 | * Authors: 7 | * Anup Patel 8 | */ 9 | 10 | #include 11 | #include 12 | 13 | struct sbiret { 14 | unsigned long error; 15 | unsigned long value; 16 | }; 17 | 18 | struct sbiret sbi_ecall(int ext, int fid, unsigned long arg0, 19 | unsigned long arg1, unsigned long arg2, 20 | unsigned long arg3, unsigned long arg4, 21 | unsigned long arg5) 22 | { 23 | struct sbiret ret; 24 | 25 | register unsigned long a0 asm ("a0") = (unsigned long)(arg0); 26 | register unsigned long a1 asm ("a1") = (unsigned long)(arg1); 27 | register unsigned long a2 asm ("a2") = (unsigned long)(arg2); 28 | register unsigned long a3 asm ("a3") = (unsigned long)(arg3); 29 | register unsigned long a4 asm ("a4") = (unsigned long)(arg4); 30 | register unsigned long a5 asm ("a5") = (unsigned long)(arg5); 31 | register unsigned long a6 asm ("a6") = (unsigned long)(fid); 32 | register unsigned long a7 asm ("a7") = (unsigned long)(ext); 33 | asm volatile ("ecall" 34 | : "+r" (a0), "+r" (a1) 35 | : "r" (a2), "r" (a3), "r" (a4), "r" (a5), "r" (a6), "r" (a7) 36 | : "memory"); 37 | ret.error = a0; 38 | ret.value = a1; 39 | 40 | return ret; 41 | } 42 | 43 | static inline void sbi_ecall_console_puts(const char *str) 44 | { 45 | sbi_ecall(SBI_EXT_DBCN, SBI_EXT_DBCN_CONSOLE_WRITE, 46 | sbi_strlen(str), (unsigned long)str, 0, 0, 0, 0); 47 | } 48 | 49 | #define wfi() \ 50 | do { \ 51 | __asm__ __volatile__("wfi" ::: "memory"); \ 52 | } while (0) 53 | 54 | void test_main(unsigned long a0, unsigned long a1) 55 | { 56 | sbi_ecall_console_puts("\nTest payload running\n"); 57 | 58 | while (1) 59 | wfi(); 60 | } 61 | -------------------------------------------------------------------------------- /include/sbi/riscv_atomic.h: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2019 Western Digital Corporation or its affiliates. 5 | * 6 | * Authors: 7 | * Anup Patel 8 | */ 9 | 10 | #ifndef __RISCV_ATOMIC_H__ 11 | #define __RISCV_ATOMIC_H__ 12 | 13 | typedef struct { 14 | volatile long counter; 15 | } atomic_t; 16 | 17 | #define ATOMIC_INIT(_lptr, val) (_lptr)->counter = (val) 18 | 19 | #define ATOMIC_INITIALIZER(val) \ 20 | { \ 21 | .counter = (val), \ 22 | } 23 | 24 | long atomic_read(atomic_t *atom); 25 | 26 | void atomic_write(atomic_t *atom, long value); 27 | 28 | long atomic_add_return(atomic_t *atom, long value); 29 | 30 | long atomic_sub_return(atomic_t *atom, long value); 31 | 32 | long atomic_cmpxchg(atomic_t *atom, long oldval, long newval); 33 | 34 | long atomic_xchg(atomic_t *atom, long newval); 35 | 36 | unsigned int atomic_raw_xchg_uint(volatile unsigned int *ptr, 37 | unsigned int newval); 38 | 39 | unsigned long atomic_raw_xchg_ulong(volatile unsigned long *ptr, 40 | unsigned long newval); 41 | /** 42 | * Set a bit in an atomic variable and return the value of bit before modify. 43 | * @nr : Bit to set. 44 | * @atom: atomic variable to modify 45 | */ 46 | int atomic_set_bit(int nr, atomic_t *atom); 47 | 48 | /** 49 | * Clear a bit in an atomic variable and return the value of bit before modify. 50 | * @nr : Bit to set. 51 | * @atom: atomic variable to modify 52 | */ 53 | 54 | int atomic_clear_bit(int nr, atomic_t *atom); 55 | 56 | /** 57 | * Set a bit in any address and return the value of bit before modify. 58 | * @nr : Bit to set. 59 | * @addr: Address to modify 60 | */ 61 | int atomic_raw_set_bit(int nr, volatile unsigned long *addr); 62 | 63 | /** 64 | * Clear a bit in any address and return the value of bit before modify. 65 | * @nr : Bit to set. 66 | * @addr: Address to modify 67 | */ 68 | int atomic_raw_clear_bit(int nr, volatile unsigned long *addr); 69 | 70 | #endif 71 | -------------------------------------------------------------------------------- /include/sbi/riscv_barrier.h: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2019 Western Digital Corporation or its affiliates. 5 | * 6 | * Authors: 7 | * Anup Patel 8 | */ 9 | 10 | #ifndef __RISCV_BARRIER_H__ 11 | #define __RISCV_BARRIER_H__ 12 | 13 | /* clang-format off */ 14 | 15 | #define RISCV_ACQUIRE_BARRIER "\tfence r , rw\n" 16 | #define RISCV_RELEASE_BARRIER "\tfence rw, w\n" 17 | 18 | #define RISCV_FENCE(p, s) \ 19 | __asm__ __volatile__ ("fence " #p "," #s : : : "memory") 20 | 21 | #define RISCV_FENCE_I \ 22 | __asm__ __volatile__ ("fence.i" : : : "memory") 23 | 24 | /* Read & Write Memory barrier */ 25 | #define mb() RISCV_FENCE(iorw,iorw) 26 | 27 | /* Read Memory barrier */ 28 | #define rmb() RISCV_FENCE(ir,ir) 29 | 30 | /* Write Memory barrier */ 31 | #define wmb() RISCV_FENCE(ow,ow) 32 | 33 | /* SMP Read & Write Memory barrier */ 34 | #define smp_mb() RISCV_FENCE(rw,rw) 35 | 36 | /* SMP Read Memory barrier */ 37 | #define smp_rmb() RISCV_FENCE(r,r) 38 | 39 | /* SMP Write Memory barrier */ 40 | #define smp_wmb() RISCV_FENCE(w,w) 41 | 42 | /* CPU relax for busy loop */ 43 | #define cpu_relax() \ 44 | do { \ 45 | unsigned long __t; \ 46 | __asm__ __volatile__ ("div %0, %0, zero" : "=r" (__t)); \ 47 | } while (0) 48 | 49 | /* clang-format on */ 50 | 51 | #define __smp_store_release(p, v) \ 52 | do { \ 53 | RISCV_FENCE(rw, w); \ 54 | *(p) = (v); \ 55 | } while (0) 56 | 57 | #define __smp_load_acquire(p) \ 58 | ({ \ 59 | typeof(*p) ___p1 = *(p); \ 60 | RISCV_FENCE(r, rw); \ 61 | ___p1; \ 62 | }) 63 | 64 | #endif 65 | -------------------------------------------------------------------------------- /include/sbi/riscv_elf.h: -------------------------------------------------------------------------------- 1 | #ifndef __RISCV_ELF_H__ 2 | #define __RISCV_ELF_H__ 3 | 4 | #define R_RISCV_RELATIVE 3 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /include/sbi/riscv_locks.h: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2019 Western Digital Corporation or its affiliates. 5 | * Copyright (c) 2021 Christoph Müllner 6 | */ 7 | 8 | #ifndef __RISCV_LOCKS_H__ 9 | #define __RISCV_LOCKS_H__ 10 | 11 | #include 12 | 13 | #define TICKET_SHIFT 16 14 | 15 | typedef struct { 16 | #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ 17 | u16 next; 18 | u16 owner; 19 | #else 20 | u16 owner; 21 | u16 next; 22 | #endif 23 | } __aligned(4) spinlock_t; 24 | 25 | #define __SPIN_LOCK_UNLOCKED \ 26 | (spinlock_t) { 0, 0 } 27 | 28 | #define SPIN_LOCK_INIT(x) \ 29 | x = __SPIN_LOCK_UNLOCKED 30 | 31 | #define SPIN_LOCK_INITIALIZER \ 32 | __SPIN_LOCK_UNLOCKED 33 | 34 | #define DEFINE_SPIN_LOCK(x) \ 35 | spinlock_t SPIN_LOCK_INIT(x) 36 | 37 | bool spin_lock_check(spinlock_t *lock); 38 | 39 | bool spin_trylock(spinlock_t *lock); 40 | 41 | void spin_lock(spinlock_t *lock); 42 | 43 | void spin_unlock(spinlock_t *lock); 44 | 45 | #endif 46 | -------------------------------------------------------------------------------- /include/sbi/sbi_console.h: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2019 Western Digital Corporation or its affiliates. 5 | * 6 | * Authors: 7 | * Anup Patel 8 | */ 9 | 10 | #ifndef __SBI_CONSOLE_H__ 11 | #define __SBI_CONSOLE_H__ 12 | 13 | #include 14 | 15 | struct sbi_console_device { 16 | /** Name of the console device */ 17 | char name[32]; 18 | 19 | /** Write a character to the console output */ 20 | void (*console_putc)(char ch); 21 | 22 | /** Write a character string to the console output */ 23 | unsigned long (*console_puts)(const char *str, unsigned long len); 24 | 25 | /** Read a character from the console input */ 26 | int (*console_getc)(void); 27 | }; 28 | 29 | #define __printf(a, b) __attribute__((format(printf, a, b))) 30 | 31 | bool sbi_isprintable(char ch); 32 | 33 | int sbi_getc(void); 34 | 35 | void sbi_putc(char ch); 36 | 37 | void sbi_puts(const char *str); 38 | 39 | unsigned long sbi_nputs(const char *str, unsigned long len); 40 | 41 | void sbi_gets(char *s, int maxwidth, char endchar); 42 | 43 | unsigned long sbi_ngets(char *str, unsigned long len); 44 | 45 | int __printf(2, 3) sbi_sprintf(char *out, const char *format, ...); 46 | 47 | int __printf(3, 4) sbi_snprintf(char *out, u32 out_sz, const char *format, ...); 48 | 49 | int __printf(1, 2) sbi_printf(const char *format, ...); 50 | 51 | int __printf(1, 2) sbi_dprintf(const char *format, ...); 52 | 53 | void __printf(1, 2) __attribute__((noreturn)) sbi_panic(const char *format, ...); 54 | 55 | const struct sbi_console_device *sbi_console_get_device(void); 56 | 57 | void sbi_console_set_device(const struct sbi_console_device *dev); 58 | 59 | struct sbi_scratch; 60 | 61 | #define SBI_ASSERT(cond, args) do { \ 62 | if (unlikely(!(cond))) \ 63 | sbi_panic args; \ 64 | } while (0) 65 | 66 | #endif 67 | -------------------------------------------------------------------------------- /include/sbi/sbi_const.h: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2019 Western Digital Corporation or its affiliates. 5 | * 6 | * Authors: 7 | * Anup Patel 8 | */ 9 | 10 | #ifndef __SBI_CONST_H__ 11 | #define __SBI_CONST_H__ 12 | 13 | /* 14 | * Some constant macros are used in both assembler and 15 | * C code. Therefore we cannot annotate them always with 16 | * 'UL' and other type specifiers unilaterally. We 17 | * use the following macros to deal with this. 18 | * 19 | * Similarly, _AT() will cast an expression with a type in C, but 20 | * leave it unchanged in asm. 21 | */ 22 | 23 | /* clang-format off */ 24 | 25 | #ifdef __ASSEMBLER__ 26 | #define _AC(X,Y) X 27 | #define _AT(T,X) X 28 | #else 29 | #define __AC(X,Y) (X##Y) 30 | #define _AC(X,Y) __AC(X,Y) 31 | #define _AT(T,X) ((T)(X)) 32 | #endif 33 | 34 | #define _UL(x) (_AC(x, UL)) 35 | #define _ULL(x) (_AC(x, ULL)) 36 | 37 | #define _BITUL(x) (_UL(1) << (x)) 38 | #define _BITULL(x) (_ULL(1) << (x)) 39 | 40 | #define UL(x) (_UL(x)) 41 | #define ULL(x) (_ULL(x)) 42 | 43 | #define __STR(s) #s 44 | #define STRINGIFY(s) __STR(s) 45 | 46 | /* clang-format on */ 47 | 48 | #endif 49 | -------------------------------------------------------------------------------- /include/sbi/sbi_cppc.h: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2023 Ventana Micro Systems Inc. 5 | * 6 | */ 7 | 8 | #ifndef __SBI_CPPC_H__ 9 | #define __SBI_CPPC_H__ 10 | 11 | #include 12 | 13 | /** CPPC device */ 14 | struct sbi_cppc_device { 15 | /** Name of the CPPC device */ 16 | char name[32]; 17 | 18 | /** probe - returns register width if implemented, 0 otherwise */ 19 | int (*cppc_probe)(unsigned long reg); 20 | 21 | /** read the cppc register*/ 22 | int (*cppc_read)(unsigned long reg, uint64_t *val); 23 | 24 | /** write to the cppc register*/ 25 | int (*cppc_write)(unsigned long reg, uint64_t val); 26 | }; 27 | 28 | int sbi_cppc_probe(unsigned long reg); 29 | int sbi_cppc_read(unsigned long reg, uint64_t *val); 30 | int sbi_cppc_write(unsigned long reg, uint64_t val); 31 | 32 | const struct sbi_cppc_device *sbi_cppc_get_device(void); 33 | void sbi_cppc_set_device(const struct sbi_cppc_device *dev); 34 | 35 | #endif 36 | -------------------------------------------------------------------------------- /include/sbi/sbi_csr_detect.h: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2020 Western Digital Corporation or its affiliates. 5 | * 6 | * Authors: 7 | * Atish Patra 8 | */ 9 | 10 | #ifndef __SBI_CSR_DETECT__H 11 | #define __SBI_CSR_DETECT__H 12 | 13 | #include 14 | #include 15 | #include 16 | 17 | #define csr_read_allowed(csr_num, trap) \ 18 | ({ \ 19 | register ulong tinfo asm("a3") = (ulong)trap; \ 20 | register ulong ttmp asm("a4"); \ 21 | register ulong mtvec = sbi_hart_expected_trap_addr(); \ 22 | register ulong ret = 0; \ 23 | ((struct sbi_trap_info *)(trap))->cause = 0; \ 24 | asm volatile( \ 25 | "add %[ttmp], %[tinfo], zero\n" \ 26 | "csrrw %[mtvec], " STR(CSR_MTVEC) ", %[mtvec]\n" \ 27 | "csrr %[ret], %[csr]\n" \ 28 | "csrw " STR(CSR_MTVEC) ", %[mtvec]" \ 29 | : [mtvec] "+&r"(mtvec), [tinfo] "+&r"(tinfo), \ 30 | [ttmp] "+&r"(ttmp), [ret] "=&r" (ret) \ 31 | : [csr] "i" (csr_num) \ 32 | : "memory"); \ 33 | ret; \ 34 | }) \ 35 | 36 | #define csr_write_allowed(csr_num, trap, value) \ 37 | ({ \ 38 | register ulong tinfo asm("a3") = (ulong)trap; \ 39 | register ulong ttmp asm("a4"); \ 40 | register ulong mtvec = sbi_hart_expected_trap_addr(); \ 41 | ((struct sbi_trap_info *)(trap))->cause = 0; \ 42 | asm volatile( \ 43 | "add %[ttmp], %[tinfo], zero\n" \ 44 | "csrrw %[mtvec], " STR(CSR_MTVEC) ", %[mtvec]\n" \ 45 | "csrw %[csr], %[val]\n" \ 46 | "csrw " STR(CSR_MTVEC) ", %[mtvec]" \ 47 | : [mtvec] "+&r"(mtvec), \ 48 | [tinfo] "+&r"(tinfo), [ttmp] "+&r"(ttmp) \ 49 | : [csr] "i" (csr_num), [val] "r" (value) \ 50 | : "memory"); \ 51 | }) \ 52 | 53 | #endif 54 | -------------------------------------------------------------------------------- /include/sbi/sbi_domain_context.h: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) IPADS@SJTU 2023. All rights reserved. 5 | */ 6 | 7 | #ifndef __SBI_DOMAIN_CONTEXT_H__ 8 | #define __SBI_DOMAIN_CONTEXT_H__ 9 | 10 | #include 11 | 12 | struct sbi_domain; 13 | 14 | /** 15 | * Enter a specific domain context synchronously 16 | * @param dom pointer to domain 17 | * 18 | * @return 0 on success and negative error code on failure 19 | */ 20 | int sbi_domain_context_enter(struct sbi_domain *dom); 21 | 22 | /** 23 | * Exit the current domain context, and then return to the caller 24 | * of sbi_domain_context_enter or attempt to start the next domain 25 | * context to be initialized 26 | * 27 | * @return 0 on success and negative error code on failure 28 | */ 29 | int sbi_domain_context_exit(void); 30 | 31 | /** 32 | * Initialize domain context support 33 | * 34 | * @return 0 on success and negative error code on failure 35 | */ 36 | int sbi_domain_context_init(void); 37 | 38 | /* Deinitialize domain context support */ 39 | void sbi_domain_context_deinit(void); 40 | 41 | #endif // __SBI_DOMAIN_CONTEXT_H__ 42 | -------------------------------------------------------------------------------- /include/sbi/sbi_double_trap.h: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2025 Rivos Inc. 5 | * 6 | * Authors: 7 | * Clément Léger 8 | */ 9 | 10 | #ifndef __SBI_DOUBLE_TRAP_H__ 11 | #define __SBI_DOUBLE_TRAP_H__ 12 | 13 | #include 14 | #include 15 | 16 | int sbi_double_trap_handler(struct sbi_trap_context *tcntx); 17 | 18 | void sbi_double_trap_init(struct sbi_scratch *scratch); 19 | 20 | #endif 21 | -------------------------------------------------------------------------------- /include/sbi/sbi_emulate_csr.h: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2019 Western Digital Corporation or its affiliates. 5 | * 6 | * Authors: 7 | * Anup Patel 8 | */ 9 | 10 | #ifndef __SBI_EMULATE_CSR_H__ 11 | #define __SBI_EMULATE_CSR_H__ 12 | 13 | #include 14 | 15 | struct sbi_trap_regs; 16 | 17 | int sbi_emulate_csr_read(int csr_num, struct sbi_trap_regs *regs, 18 | ulong *csr_val); 19 | 20 | int sbi_emulate_csr_write(int csr_num, struct sbi_trap_regs *regs, 21 | ulong csr_val); 22 | 23 | #endif 24 | -------------------------------------------------------------------------------- /include/sbi/sbi_error.h: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2019 Western Digital Corporation or its affiliates. 5 | * 6 | * Authors: 7 | * Anup Patel 8 | */ 9 | 10 | #ifndef __SBI_ERROR_H__ 11 | #define __SBI_ERROR_H__ 12 | 13 | #include 14 | 15 | /* clang-format off */ 16 | 17 | #define SBI_OK 0 18 | #define SBI_EFAIL SBI_ERR_FAILED 19 | #define SBI_ENOTSUPP SBI_ERR_NOT_SUPPORTED 20 | #define SBI_EINVAL SBI_ERR_INVALID_PARAM 21 | #define SBI_EDENIED SBI_ERR_DENIED 22 | #define SBI_EINVALID_ADDR SBI_ERR_INVALID_ADDRESS 23 | #define SBI_EALREADY SBI_ERR_ALREADY_AVAILABLE 24 | #define SBI_EALREADY_STARTED SBI_ERR_ALREADY_STARTED 25 | #define SBI_EALREADY_STOPPED SBI_ERR_ALREADY_STOPPED 26 | #define SBI_ENO_SHMEM SBI_ERR_NO_SHMEM 27 | #define SBI_EINVALID_STATE SBI_ERR_INVALID_STATE 28 | #define SBI_EBAD_RANGE SBI_ERR_BAD_RANGE 29 | #define SBI_ETIMEOUT SBI_ERR_TIMEOUT 30 | #define SBI_ETIMEDOUT SBI_ERR_TIMEOUT 31 | #define SBI_EIO SBI_ERR_IO 32 | #define SBI_EDENIED_LOCKED SBI_ERR_DENIED_LOCKED 33 | 34 | #define SBI_ENODEV -1000 35 | #define SBI_ENOSYS -1001 36 | #define SBI_EILL -1002 37 | #define SBI_ENOSPC -1003 38 | #define SBI_ENOMEM -1004 39 | #define SBI_EUNKNOWN -1005 40 | #define SBI_ENOENT -1006 41 | 42 | /* clang-format on */ 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /include/sbi/sbi_fifo.h: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2019 Western Digital Corporation or its affiliates. 5 | * 6 | * Authors: 7 | * Atish Patra 8 | * 9 | */ 10 | 11 | #ifndef __SBI_FIFO_H__ 12 | #define __SBI_FIFO_H__ 13 | 14 | #include 15 | #include 16 | 17 | struct sbi_fifo { 18 | void *queue; 19 | spinlock_t qlock; 20 | u16 entry_size; 21 | u16 num_entries; 22 | u16 avail; 23 | u16 tail; 24 | }; 25 | 26 | #define SBI_FIFO_INITIALIZER(__queue_mem, __entries, __entry_size) \ 27 | { .queue = __queue_mem, \ 28 | .qlock = SPIN_LOCK_INITIALIZER, \ 29 | .num_entries = __entries, \ 30 | .entry_size = __entry_size, \ 31 | .avail = 0, \ 32 | .tail = 0, \ 33 | } 34 | 35 | #define SBI_FIFO_DEFINE(__name, __queue_mem, __entries, __entry_size) \ 36 | struct sbi_fifo __name = SBI_FIFO_INITIALIZER(__queue_mem, __entries, __entry_size) 37 | 38 | enum sbi_fifo_inplace_update_types { 39 | SBI_FIFO_SKIP, 40 | SBI_FIFO_UPDATED, 41 | SBI_FIFO_UNCHANGED, 42 | }; 43 | 44 | int sbi_fifo_dequeue(struct sbi_fifo *fifo, void *data); 45 | int sbi_fifo_enqueue(struct sbi_fifo *fifo, void *data, bool force); 46 | void sbi_fifo_init(struct sbi_fifo *fifo, void *queue_mem, u16 entries, 47 | u16 entry_size); 48 | int sbi_fifo_is_empty(struct sbi_fifo *fifo); 49 | int sbi_fifo_is_full(struct sbi_fifo *fifo); 50 | int sbi_fifo_inplace_update(struct sbi_fifo *fifo, void *in, 51 | int (*fptr)(void *in, void *data)); 52 | u16 sbi_fifo_avail(struct sbi_fifo *fifo); 53 | 54 | #endif 55 | -------------------------------------------------------------------------------- /include/sbi/sbi_fwft.h: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2024 Rivos Inc. 5 | * 6 | * Authors: 7 | * Clément Léger 8 | */ 9 | 10 | #ifndef __SBI_FW_FEATURE_H__ 11 | #define __SBI_FW_FEATURE_H__ 12 | 13 | #include 14 | 15 | struct sbi_scratch; 16 | 17 | int sbi_fwft_set(enum sbi_fwft_feature_t feature, unsigned long value, 18 | unsigned long flags); 19 | int sbi_fwft_get(enum sbi_fwft_feature_t feature, unsigned long *out_val); 20 | 21 | int sbi_fwft_init(struct sbi_scratch *scratch, bool cold_boot); 22 | 23 | #endif 24 | -------------------------------------------------------------------------------- /include/sbi/sbi_hfence.h: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2019 Western Digital Corporation or its affiliates. 5 | * 6 | * Authors: 7 | * Atish Patra 8 | * Anup Patel 9 | */ 10 | 11 | #ifndef __SBI_FENCE_H__ 12 | #define __SBI_FENCE_H__ 13 | 14 | /** Invalidate Stage2 TLBs for given VMID and guest physical address */ 15 | void __sbi_hfence_gvma_vmid_gpa(unsigned long gpa_divby_4, 16 | unsigned long vmid); 17 | 18 | /** Invalidate Stage2 TLBs for given VMID */ 19 | void __sbi_hfence_gvma_vmid(unsigned long vmid); 20 | 21 | /** Invalidate Stage2 TLBs for given guest physical address */ 22 | void __sbi_hfence_gvma_gpa(unsigned long gpa_divby_4); 23 | 24 | /** Invalidate all possible Stage2 TLBs */ 25 | void __sbi_hfence_gvma_all(void); 26 | 27 | /** Invalidate unified TLB entries for given asid and guest virtual address */ 28 | void __sbi_hfence_vvma_asid_va(unsigned long va, unsigned long asid); 29 | 30 | /** Invalidate unified TLB entries for given ASID for a guest*/ 31 | void __sbi_hfence_vvma_asid(unsigned long asid); 32 | 33 | /** Invalidate unified TLB entries for a given guest virtual address */ 34 | void __sbi_hfence_vvma_va(unsigned long va); 35 | 36 | /** Invalidate all possible Stage2 TLBs */ 37 | void __sbi_hfence_vvma_all(void); 38 | 39 | #endif 40 | -------------------------------------------------------------------------------- /include/sbi/sbi_illegal_atomic.h: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2025 MIPS 5 | * 6 | */ 7 | 8 | #ifndef __SBI_ILLEGAL_ATOMIC_H__ 9 | #define __SBI_ILLEGAL_ATOMIC_H__ 10 | 11 | #include 12 | 13 | struct sbi_trap_regs; 14 | 15 | int sbi_illegal_atomic(ulong insn, struct sbi_trap_regs *regs); 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /include/sbi/sbi_illegal_insn.h: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2019 Western Digital Corporation or its affiliates. 5 | * 6 | * Authors: 7 | * Anup Patel 8 | */ 9 | 10 | #ifndef __SBI_ILLEGAl_INSN_H__ 11 | #define __SBI_ILLEGAl_INSN_H__ 12 | 13 | #include 14 | 15 | struct sbi_trap_context; 16 | 17 | typedef int (*illegal_insn_func)(ulong insn, struct sbi_trap_regs *regs); 18 | 19 | int truly_illegal_insn(ulong insn, struct sbi_trap_regs *regs); 20 | 21 | int sbi_illegal_insn_handler(struct sbi_trap_context *tcntx); 22 | 23 | #endif 24 | -------------------------------------------------------------------------------- /include/sbi/sbi_init.h: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2019 Western Digital Corporation or its affiliates. 5 | * 6 | * Authors: 7 | * Anup Patel 8 | */ 9 | 10 | #ifndef __SBI_INIT_H__ 11 | #define __SBI_INIT_H__ 12 | 13 | #include 14 | 15 | struct sbi_scratch; 16 | 17 | void __noreturn sbi_init(struct sbi_scratch *scratch); 18 | 19 | unsigned long sbi_entry_count(u32 hartindex); 20 | 21 | unsigned long sbi_init_count(u32 hartindex); 22 | 23 | void __noreturn sbi_exit(struct sbi_scratch *scratch); 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /include/sbi/sbi_irqchip.h: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2022 Ventana Micro Systems Inc. 5 | * 6 | * Authors: 7 | * Anup Patel 8 | */ 9 | 10 | #ifndef __SBI_IRQCHIP_H__ 11 | #define __SBI_IRQCHIP_H__ 12 | 13 | #include 14 | #include 15 | 16 | struct sbi_scratch; 17 | 18 | /** irqchip hardware device */ 19 | struct sbi_irqchip_device { 20 | /** Node in the list of irqchip devices */ 21 | struct sbi_dlist node; 22 | 23 | /** Initialize per-hart state for the current hart */ 24 | int (*warm_init)(struct sbi_irqchip_device *dev); 25 | 26 | /** Handle an IRQ from this irqchip */ 27 | int (*irq_handle)(void); 28 | }; 29 | 30 | /** 31 | * Process external interrupts 32 | * 33 | * This function is called by sbi_trap_handler() to handle external 34 | * interrupts. 35 | * 36 | * @param regs pointer for trap registers 37 | */ 38 | int sbi_irqchip_process(void); 39 | 40 | /** Register an irqchip device to receive callbacks */ 41 | void sbi_irqchip_add_device(struct sbi_irqchip_device *dev); 42 | 43 | /** Initialize interrupt controllers */ 44 | int sbi_irqchip_init(struct sbi_scratch *scratch, bool cold_boot); 45 | 46 | /** Exit interrupt controllers */ 47 | void sbi_irqchip_exit(struct sbi_scratch *scratch); 48 | 49 | #endif 50 | -------------------------------------------------------------------------------- /include/sbi/sbi_math.h: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2020 Western Digital Corporation or its affiliates. 5 | * 6 | * Authors: 7 | * Atish Patra 8 | */ 9 | 10 | #ifndef __SBI_MATH_H__ 11 | #define __SBI_MATH_H__ 12 | 13 | unsigned long log2roundup(unsigned long x); 14 | 15 | #endif 16 | -------------------------------------------------------------------------------- /include/sbi/sbi_slist.h: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Simple simply-linked list library. 5 | * 6 | * Copyright (c) 2025 Rivos Inc. 7 | * 8 | * Authors: 9 | * Clément Léger 10 | */ 11 | 12 | #ifndef __SBI_SLIST_H__ 13 | #define __SBI_SLIST_H__ 14 | 15 | #include 16 | 17 | #define SBI_SLIST_HEAD_INIT(_ptr) (_ptr) 18 | #define SBI_SLIST_HEAD(_lname, _stype) struct _stype *_lname 19 | #define SBI_SLIST_NODE(_stype) SBI_SLIST_HEAD(next, _stype) 20 | #define SBI_SLIST_NODE_INIT(_ptr) .next = _ptr 21 | 22 | #define SBI_INIT_SLIST_HEAD(_head) (_head) = NULL 23 | 24 | #define SBI_SLIST_ADD(_ptr, _head) \ 25 | do { \ 26 | (_ptr)->next = _head; \ 27 | (_head) = _ptr; \ 28 | } while (0) 29 | 30 | #define SBI_SLIST_FOR_EACH_ENTRY(_ptr, _head) \ 31 | for (_ptr = _head; _ptr; _ptr = _ptr->next) 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /include/sbi/sbi_string.h: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2019 Western Digital Corporation or its affiliates. 5 | * 6 | * Authors: 7 | * Atish Patra 8 | */ 9 | 10 | #ifndef __STRING_H__ 11 | #define __STRING_H__ 12 | 13 | #include 14 | 15 | /* 16 | Provides sbi_strcmp for the completeness of supporting string functions. 17 | it is not recommended to use sbi_strcmp() but use sbi_strncmp instead. 18 | */ 19 | 20 | int sbi_strcmp(const char *a, const char *b); 21 | 22 | int sbi_strncmp(const char *a, const char *b, size_t count); 23 | 24 | size_t sbi_strlen(const char *str); 25 | 26 | size_t sbi_strnlen(const char *str, size_t count); 27 | 28 | char *sbi_strcpy(char *dest, const char *src); 29 | 30 | char *sbi_strncpy(char *dest, const char *src, size_t count); 31 | 32 | char *sbi_strchr(const char *s, int c); 33 | 34 | char *sbi_strrchr(const char *s, int c); 35 | 36 | void *sbi_memset(void *s, int c, size_t count); 37 | 38 | void *sbi_memcpy(void *dest, const void *src, size_t count); 39 | 40 | void *sbi_memmove(void *dest, const void *src, size_t count); 41 | 42 | int sbi_memcmp(const void *s1, const void *s2, size_t count); 43 | 44 | void *sbi_memchr(const void *s, int c, size_t count); 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /include/sbi/sbi_tlb.h: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2019 Western Digital Corporation or its affiliates. 5 | * 6 | * Authors: 7 | * Atish Patra 8 | * Anup Patel 9 | */ 10 | 11 | #ifndef __SBI_TLB_H__ 12 | #define __SBI_TLB_H__ 13 | 14 | #include 15 | #include 16 | 17 | /* clang-format off */ 18 | 19 | #define SBI_TLB_FLUSH_ALL ((unsigned long)-1) 20 | 21 | /* clang-format on */ 22 | 23 | struct sbi_scratch; 24 | 25 | enum sbi_tlb_type { 26 | SBI_TLB_FENCE_I = 0, 27 | SBI_TLB_SFENCE_VMA, 28 | SBI_TLB_SFENCE_VMA_ASID, 29 | SBI_TLB_HFENCE_GVMA_VMID, 30 | SBI_TLB_HFENCE_GVMA, 31 | SBI_TLB_HFENCE_VVMA_ASID, 32 | SBI_TLB_HFENCE_VVMA, 33 | SBI_TLB_TYPE_MAX, 34 | }; 35 | 36 | struct sbi_tlb_info { 37 | unsigned long start; 38 | unsigned long size; 39 | uint16_t asid; 40 | uint16_t vmid; 41 | enum sbi_tlb_type type; 42 | struct sbi_hartmask smask; 43 | }; 44 | 45 | #define SBI_TLB_INFO_INIT(__p, __start, __size, __asid, __vmid, __type, __src) \ 46 | do { \ 47 | (__p)->start = (__start); \ 48 | (__p)->size = (__size); \ 49 | (__p)->asid = (__asid); \ 50 | (__p)->vmid = (__vmid); \ 51 | (__p)->type = (__type); \ 52 | SBI_HARTMASK_INIT_EXCEPT(&(__p)->smask, (__src)); \ 53 | } while (0) 54 | 55 | #define SBI_TLB_INFO_SIZE sizeof(struct sbi_tlb_info) 56 | 57 | int sbi_tlb_request(ulong hmask, ulong hbase, struct sbi_tlb_info *tinfo); 58 | 59 | int sbi_tlb_init(struct sbi_scratch *scratch, bool cold_boot); 60 | 61 | #endif 62 | -------------------------------------------------------------------------------- /include/sbi/sbi_trap_ldst.h: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2019 Western Digital Corporation or its affiliates. 5 | * 6 | * Authors: 7 | * Anup Patel 8 | */ 9 | 10 | #ifndef __SBI_TRAP_LDST_H__ 11 | #define __SBI_TRAP_LDST_H__ 12 | 13 | #include 14 | #include 15 | 16 | union sbi_ldst_data { 17 | u64 data_u64; 18 | u32 data_u32; 19 | u8 data_bytes[8]; 20 | ulong data_ulong; 21 | }; 22 | 23 | int sbi_misaligned_load_handler(struct sbi_trap_context *tcntx); 24 | 25 | int sbi_misaligned_store_handler(struct sbi_trap_context *tcntx); 26 | 27 | int sbi_load_access_handler(struct sbi_trap_context *tcntx); 28 | 29 | int sbi_store_access_handler(struct sbi_trap_context *tcntx); 30 | 31 | ulong sbi_misaligned_tinst_fixup(ulong orig_tinst, ulong new_tinst, 32 | ulong addr_offset); 33 | 34 | int sbi_misaligned_v_ld_emulator(int rlen, union sbi_ldst_data *out_val, 35 | struct sbi_trap_context *tcntx); 36 | 37 | int sbi_misaligned_v_st_emulator(int wlen, union sbi_ldst_data in_val, 38 | struct sbi_trap_context *tcntx); 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /include/sbi/sbi_unpriv.h: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2019 Western Digital Corporation or its affiliates. 5 | * 6 | * Authors: 7 | * Anup Patel 8 | */ 9 | 10 | #ifndef __SBI_UNPRIV_H__ 11 | #define __SBI_UNPRIV_H__ 12 | 13 | #include 14 | 15 | struct sbi_scratch; 16 | struct sbi_trap_info; 17 | 18 | #define DECLARE_UNPRIVILEGED_LOAD_FUNCTION(type) \ 19 | type sbi_load_##type(const type *addr, \ 20 | struct sbi_trap_info *trap); 21 | 22 | #define DECLARE_UNPRIVILEGED_STORE_FUNCTION(type) \ 23 | void sbi_store_##type(type *addr, type val, \ 24 | struct sbi_trap_info *trap); 25 | 26 | DECLARE_UNPRIVILEGED_LOAD_FUNCTION(u8) 27 | DECLARE_UNPRIVILEGED_LOAD_FUNCTION(u16) 28 | DECLARE_UNPRIVILEGED_LOAD_FUNCTION(s8) 29 | DECLARE_UNPRIVILEGED_LOAD_FUNCTION(s16) 30 | DECLARE_UNPRIVILEGED_LOAD_FUNCTION(s32) 31 | DECLARE_UNPRIVILEGED_STORE_FUNCTION(u8) 32 | DECLARE_UNPRIVILEGED_STORE_FUNCTION(u16) 33 | DECLARE_UNPRIVILEGED_STORE_FUNCTION(u32) 34 | DECLARE_UNPRIVILEGED_LOAD_FUNCTION(u32) 35 | DECLARE_UNPRIVILEGED_LOAD_FUNCTION(u64) 36 | DECLARE_UNPRIVILEGED_STORE_FUNCTION(u64) 37 | DECLARE_UNPRIVILEGED_LOAD_FUNCTION(ulong) 38 | 39 | ulong sbi_get_insn(ulong mepc, struct sbi_trap_info *trap); 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /include/sbi/sbi_version.h: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2019 Western Digital Corporation or its affiliates. 5 | * 6 | * Authors: 7 | * Anup Patel 8 | */ 9 | 10 | #ifndef __SBI_VERSION_H__ 11 | #define __SBI_VERSION_H__ 12 | 13 | #define OPENSBI_VERSION_MAJOR 1 14 | #define OPENSBI_VERSION_MINOR 6 15 | 16 | /** 17 | * OpenSBI 32-bit version with: 18 | * 1. upper 16-bits as major number 19 | * 2. lower 16-bits as minor number 20 | */ 21 | #define OPENSBI_VERSION ((OPENSBI_VERSION_MAJOR << 16) | \ 22 | (OPENSBI_VERSION_MINOR)) 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /include/sbi/sbi_visibility.h: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2025 SiFive 5 | */ 6 | 7 | #ifndef __SBI_VISIBILITY_H__ 8 | #define __SBI_VISIBILITY_H__ 9 | 10 | /* 11 | * Declare all global objects with hidden visibility so access is PC-relative 12 | * instead of going through the GOT. 13 | */ 14 | #pragma GCC visibility push(hidden) 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /include/sbi_utils/fdt/fdt_pmu.h: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: BSD-2-Clause 2 | /* 3 | * fdt_pmu.c - Flat Device Tree PMU helper routines 4 | * 5 | * Copyright (c) 2021 Western Digital Corporation or its affiliates. 6 | * 7 | * Authors: 8 | * Atish Patra 9 | */ 10 | 11 | #ifndef __FDT_PMU_H__ 12 | #define __FDT_PMU_H__ 13 | 14 | #include 15 | 16 | struct fdt_pmu_hw_event_select_map { 17 | uint32_t eidx; 18 | uint64_t select; 19 | }; 20 | 21 | struct fdt_pmu_hw_event_counter_map { 22 | uint32_t eidx_start; 23 | uint32_t eidx_end; 24 | uint32_t ctr_map; 25 | }; 26 | 27 | struct fdt_pmu_raw_event_counter_map { 28 | uint64_t select; 29 | uint64_t select_mask; 30 | uint32_t ctr_map; 31 | }; 32 | 33 | #ifdef CONFIG_FDT_PMU 34 | 35 | /** 36 | * Fix up the PMU node in the device tree 37 | * 38 | * This routine: 39 | * 1. Disables opensbi specific properties from the DT 40 | * 41 | * It is recommended that platform support call this function in 42 | * their final_init() platform operation. 43 | * 44 | * @param fdt device tree blob 45 | */ 46 | int fdt_pmu_fixup(void *fdt); 47 | 48 | /** 49 | * Setup PMU data from device tree 50 | * 51 | * @param fdt device tree blob 52 | * 53 | * @return 0 on success and negative error code on failure 54 | */ 55 | int fdt_pmu_setup(const void *fdt); 56 | 57 | /** 58 | * Get the mhpmevent select value read from DT for a given event 59 | * @param event_idx Event ID of the given event 60 | * 61 | * @return The select value read from DT or 0 if given index was not found 62 | */ 63 | uint64_t fdt_pmu_get_select_value(uint32_t event_idx); 64 | 65 | #else 66 | 67 | static inline void fdt_pmu_fixup(void *fdt) { } 68 | static inline int fdt_pmu_setup(const void *fdt) { return 0; } 69 | static inline uint64_t fdt_pmu_get_select_value(uint32_t event_idx) { return 0; } 70 | 71 | #endif 72 | 73 | #endif 74 | -------------------------------------------------------------------------------- /include/sbi_utils/gpio/fdt_gpio.h: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2021 Western Digital Corporation or its affiliates. 5 | * 6 | * Authors: 7 | * Anup Patel 8 | */ 9 | 10 | #ifndef __FDT_GPIO_H__ 11 | #define __FDT_GPIO_H__ 12 | 13 | #include 14 | #include 15 | 16 | struct fdt_phandle_args; 17 | 18 | /** FDT based GPIO driver */ 19 | struct fdt_gpio { 20 | struct fdt_driver driver; 21 | int (*xlate)(struct gpio_chip *chip, 22 | const struct fdt_phandle_args *pargs, 23 | struct gpio_pin *out_pin); 24 | }; 25 | 26 | /** Get a GPIO pin using "gpios" DT property of client DT node */ 27 | int fdt_gpio_pin_get(const void *fdt, int nodeoff, int index, 28 | struct gpio_pin *out_pin); 29 | 30 | /** Simple xlate function to convert two GPIO FDT cells into GPIO pin */ 31 | int fdt_gpio_simple_xlate(struct gpio_chip *chip, 32 | const struct fdt_phandle_args *pargs, 33 | struct gpio_pin *out_pin); 34 | 35 | #endif 36 | -------------------------------------------------------------------------------- /include/sbi_utils/i2c/dw_i2c.h: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2022 StarFive Technology Co., Ltd. 5 | * 6 | * Author: Minda Chen 7 | */ 8 | 9 | #ifndef __DW_I2C_H__ 10 | #define __DW_I2C_H__ 11 | 12 | #include 13 | 14 | int dw_i2c_init(struct i2c_adapter *, int nodeoff); 15 | 16 | struct dw_i2c_adapter { 17 | unsigned long addr; 18 | struct i2c_adapter adapter; 19 | }; 20 | 21 | #endif 22 | -------------------------------------------------------------------------------- /include/sbi_utils/i2c/fdt_i2c.h: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2021 YADRO 5 | * 6 | * Authors: 7 | * Nikita Shubin 8 | */ 9 | 10 | #ifndef __FDT_I2C_H__ 11 | #define __FDT_I2C_H__ 12 | 13 | #include 14 | #include 15 | 16 | /** Get I2C adapter identified by nodeoff */ 17 | int fdt_i2c_adapter_get(const void *fdt, int nodeoff, 18 | struct i2c_adapter **out_adapter); 19 | 20 | #endif 21 | -------------------------------------------------------------------------------- /include/sbi_utils/ipi/aclint_mswi.h: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2021 Western Digital Corporation or its affiliates. 5 | * 6 | * Authors: 7 | * Anup Patel 8 | */ 9 | 10 | #ifndef __IPI_ACLINT_MSWI_H__ 11 | #define __IPI_ACLINT_MSWI_H__ 12 | 13 | #include 14 | 15 | #define ACLINT_MSWI_ALIGN 0x1000 16 | #define ACLINT_MSWI_SIZE 0x4000 17 | #define ACLINT_MSWI_MAX_HARTS 4095 18 | 19 | #define CLINT_MSWI_OFFSET 0x0000 20 | 21 | struct aclint_mswi_data { 22 | /* Public details */ 23 | unsigned long addr; 24 | unsigned long size; 25 | u32 first_hartid; 26 | u32 hart_count; 27 | }; 28 | 29 | int aclint_mswi_cold_init(struct aclint_mswi_data *mswi); 30 | 31 | #endif 32 | -------------------------------------------------------------------------------- /include/sbi_utils/ipi/andes_plicsw.h: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2022 Andes Technology Corporation 5 | * 6 | * Authors: 7 | * Zong Li 8 | * Nylon Chen 9 | * Leo Yu-Chi Liang 10 | * Yu Chien Peter Lin 11 | */ 12 | 13 | #ifndef _IPI_ANDES_PLICSW_H_ 14 | #define _IPI_ANDES_PLICSW_H_ 15 | 16 | #define PLICSW_PRIORITY_BASE 0x4 17 | 18 | #define PLICSW_PENDING_BASE 0x1000 19 | 20 | #define PLICSW_ENABLE_BASE 0x2000 21 | #define PLICSW_ENABLE_STRIDE 0x80 22 | 23 | #define PLICSW_CONTEXT_BASE 0x200000 24 | #define PLICSW_CONTEXT_STRIDE 0x1000 25 | #define PLICSW_CONTEXT_CLAIM 0x4 26 | 27 | #define PLICSW_REGION_ALIGN 0x1000 28 | 29 | struct plicsw_data { 30 | unsigned long addr; 31 | unsigned long size; 32 | uint32_t hart_count; 33 | }; 34 | 35 | int plicsw_cold_ipi_init(struct plicsw_data *plicsw); 36 | 37 | #endif /* _IPI_ANDES_PLICSW_H_ */ 38 | -------------------------------------------------------------------------------- /include/sbi_utils/ipi/fdt_ipi.h: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2020 Western Digital Corporation or its affiliates. 5 | * 6 | * Authors: 7 | * Anup Patel 8 | */ 9 | 10 | #ifndef __FDT_IPI_H__ 11 | #define __FDT_IPI_H__ 12 | 13 | #include 14 | #include 15 | 16 | #ifdef CONFIG_FDT_IPI 17 | 18 | int fdt_ipi_init(void); 19 | 20 | #else 21 | 22 | static inline int fdt_ipi_init(void) { return 0; } 23 | 24 | #endif 25 | 26 | #endif 27 | -------------------------------------------------------------------------------- /include/sbi_utils/irqchip/aplic.h: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2021 Western Digital Corporation or its affiliates. 5 | * Copyright (c) 2022 Ventana Micro Systems Inc. 6 | * 7 | * Authors: 8 | * Anup Patel 9 | */ 10 | 11 | #ifndef __IRQCHIP_APLIC_H__ 12 | #define __IRQCHIP_APLIC_H__ 13 | 14 | #include 15 | #include 16 | 17 | #define APLIC_MAX_DELEGATE 16 18 | 19 | struct aplic_msicfg_data { 20 | unsigned long lhxs; 21 | unsigned long lhxw; 22 | unsigned long hhxs; 23 | unsigned long hhxw; 24 | unsigned long base_addr; 25 | }; 26 | 27 | struct aplic_delegate_data { 28 | u32 first_irq; 29 | u32 last_irq; 30 | u32 child_index; 31 | }; 32 | 33 | struct aplic_data { 34 | /* Private members */ 35 | struct sbi_irqchip_device irqchip; 36 | /* Public members */ 37 | unsigned long addr; 38 | unsigned long size; 39 | unsigned long num_idc; 40 | unsigned long num_source; 41 | bool targets_mmode; 42 | bool has_msicfg_mmode; 43 | struct aplic_msicfg_data msicfg_mmode; 44 | bool has_msicfg_smode; 45 | struct aplic_msicfg_data msicfg_smode; 46 | struct aplic_delegate_data delegate[APLIC_MAX_DELEGATE]; 47 | }; 48 | 49 | int aplic_cold_irqchip_init(struct aplic_data *aplic); 50 | 51 | #endif 52 | -------------------------------------------------------------------------------- /include/sbi_utils/irqchip/fdt_irqchip.h: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2020 Western Digital Corporation or its affiliates. 5 | * 6 | * Authors: 7 | * Anup Patel 8 | */ 9 | 10 | #ifndef __FDT_IRQCHIP_H__ 11 | #define __FDT_IRQCHIP_H__ 12 | 13 | #include 14 | #include 15 | 16 | #ifdef CONFIG_FDT_IRQCHIP 17 | 18 | int fdt_irqchip_init(void); 19 | 20 | #else 21 | 22 | static inline int fdt_irqchip_init(void) { return 0; } 23 | 24 | #endif 25 | 26 | #endif 27 | -------------------------------------------------------------------------------- /include/sbi_utils/irqchip/imsic.h: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2021 Western Digital Corporation or its affiliates. 5 | * Copyright (c) 2022 Ventana Micro Systems Inc. 6 | * 7 | * Authors: 8 | * Anup Patel 9 | */ 10 | 11 | #ifndef __IRQCHIP_IMSIC_H__ 12 | #define __IRQCHIP_IMSIC_H__ 13 | 14 | #include 15 | 16 | #define IMSIC_MMIO_PAGE_SHIFT 12 17 | #define IMSIC_MMIO_PAGE_SZ (1UL << IMSIC_MMIO_PAGE_SHIFT) 18 | 19 | #define IMSIC_MAX_REGS 16 20 | 21 | struct imsic_regs { 22 | unsigned long addr; 23 | unsigned long size; 24 | }; 25 | 26 | struct imsic_data { 27 | bool targets_mmode; 28 | u32 guest_index_bits; 29 | u32 hart_index_bits; 30 | u32 group_index_bits; 31 | u32 group_index_shift; 32 | unsigned long num_ids; 33 | struct imsic_regs regs[IMSIC_MAX_REGS]; 34 | }; 35 | 36 | #ifdef CONFIG_IRQCHIP_IMSIC 37 | 38 | int imsic_map_hartid_to_data(u32 hartid, struct imsic_data *imsic, int file); 39 | 40 | struct imsic_data *imsic_get_data(u32 hartindex); 41 | 42 | int imsic_get_target_file(u32 hartindex); 43 | 44 | void imsic_local_irqchip_init(void); 45 | 46 | int imsic_data_check(struct imsic_data *imsic); 47 | 48 | int imsic_cold_irqchip_init(struct imsic_data *imsic); 49 | 50 | #else 51 | 52 | static inline void imsic_local_irqchip_init(void) { } 53 | 54 | static inline int imsic_data_check(struct imsic_data *imsic) { return 0; } 55 | 56 | #endif 57 | 58 | #endif 59 | -------------------------------------------------------------------------------- /include/sbi_utils/irqchip/plic.h: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2019 Western Digital Corporation or its affiliates. 5 | * 6 | * Authors: 7 | * Anup Patel 8 | */ 9 | 10 | #ifndef __IRQCHIP_PLIC_H__ 11 | #define __IRQCHIP_PLIC_H__ 12 | 13 | #include 14 | #include 15 | 16 | struct plic_data { 17 | /* Private members */ 18 | struct sbi_irqchip_device irqchip; 19 | /* Public members */ 20 | unsigned long addr; 21 | unsigned long size; 22 | unsigned long num_src; 23 | unsigned long flags; 24 | void *pm_data; 25 | s16 context_map[][2]; 26 | }; 27 | 28 | /** Work around a bug on Ariane that requires enabling interrupts at boot */ 29 | #define PLIC_FLAG_ARIANE_BUG BIT(0) 30 | /** PLIC must be delegated to S-mode like T-HEAD C906 and C910 */ 31 | #define PLIC_FLAG_THEAD_DELEGATION BIT(1) 32 | /** Allocate space for power management save/restore operations */ 33 | #define PLIC_FLAG_ENABLE_PM BIT(2) 34 | 35 | #define PLIC_M_CONTEXT 0 36 | #define PLIC_S_CONTEXT 1 37 | 38 | #define PLIC_DATA_SIZE(__hart_count) (sizeof(struct plic_data) + \ 39 | (__hart_count) * 2 * sizeof(s16)) 40 | 41 | #define PLIC_IE_WORDS(__p) ((__p)->num_src / 32 + 1) 42 | 43 | struct plic_data *plic_get(void); 44 | 45 | void plic_suspend(void); 46 | 47 | void plic_resume(void); 48 | 49 | int plic_cold_irqchip_init(struct plic_data *plic); 50 | 51 | #endif 52 | -------------------------------------------------------------------------------- /include/sbi_utils/mailbox/fdt_mailbox.h: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2024 Ventana Micro Systems Inc. 5 | * 6 | * Authors: 7 | * Anup Patel 8 | */ 9 | 10 | #ifndef __FDT_MAILBOX_H__ 11 | #define __FDT_MAILBOX_H__ 12 | 13 | #include 14 | #include 15 | 16 | struct fdt_phandle_args; 17 | 18 | /** FDT based mailbox driver */ 19 | struct fdt_mailbox { 20 | struct fdt_driver driver; 21 | int (*xlate)(struct mbox_controller *mbox, 22 | const struct fdt_phandle_args *pargs, 23 | u32 *out_chan_args); 24 | }; 25 | 26 | /** Request a mailbox channel using "mboxes" DT property of client DT node */ 27 | int fdt_mailbox_request_chan(const void *fdt, int nodeoff, int index, 28 | struct mbox_chan **out_chan); 29 | 30 | /** Simple xlate function to convert one mailbox FDT cell into channel args */ 31 | int fdt_mailbox_simple_xlate(struct mbox_controller *mbox, 32 | const struct fdt_phandle_args *pargs, 33 | u32 *out_chan_args); 34 | 35 | #endif 36 | -------------------------------------------------------------------------------- /include/sbi_utils/mailbox/rpmi_mailbox.h: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2023 Ventana Micro Systems Inc. 5 | * 6 | * Authors: 7 | * Anup Patel 8 | */ 9 | 10 | #ifndef __RPMI_MAILBOX_H__ 11 | #define __RPMI_MAILBOX_H__ 12 | 13 | #include 14 | #include 15 | #include 16 | 17 | #define rpmi_u32_count(__var) (sizeof(__var) / sizeof(u32)) 18 | 19 | /** Convert RPMI error to SBI error */ 20 | int rpmi_xlate_error(enum rpmi_error error); 21 | 22 | /** Typical RPMI normal request with at least status code in response */ 23 | int rpmi_normal_request_with_status( 24 | struct mbox_chan *chan, u32 service_id, 25 | void *req, u32 req_words, u32 req_endian_words, 26 | void *resp, u32 resp_words, u32 resp_endian_words); 27 | 28 | /* RPMI posted request which is without any response*/ 29 | int rpmi_posted_request( 30 | struct mbox_chan *chan, u32 service_id, 31 | void *req, u32 req_words, u32 req_endian_words); 32 | 33 | #endif /* !__RPMI_MAILBOX_H__ */ 34 | -------------------------------------------------------------------------------- /include/sbi_utils/mpxy/fdt_mpxy.h: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2024 Ventana Micro Systems Inc. 5 | * 6 | * Authors: 7 | * Anup Patel 8 | */ 9 | 10 | #ifndef __FDT_MPXY_H__ 11 | #define __FDT_MPXY_H__ 12 | 13 | #include 14 | #include 15 | 16 | #ifdef CONFIG_FDT_MPXY 17 | 18 | int fdt_mpxy_init(const void *fdt); 19 | 20 | #else 21 | 22 | static inline int fdt_mpxy_init(const void *fdt) { return 0; } 23 | 24 | #endif 25 | 26 | #endif 27 | -------------------------------------------------------------------------------- /include/sbi_utils/regmap/fdt_regmap.h: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2023 Ventana Micro Systems Inc. 5 | * 6 | * Authors: 7 | * Anup Patel 8 | */ 9 | 10 | #ifndef __FDT_REGMAP_H__ 11 | #define __FDT_REGMAP_H__ 12 | 13 | #include 14 | #include 15 | 16 | struct fdt_phandle_args; 17 | 18 | /** Get regmap instance based on phandle */ 19 | int fdt_regmap_get_by_phandle(const void *fdt, u32 phandle, 20 | struct regmap **out_rmap); 21 | 22 | /** Get regmap instance based on "regmap" property of the specified DT node */ 23 | int fdt_regmap_get(const void *fdt, int nodeoff, struct regmap **out_rmap); 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /include/sbi_utils/regmap/regmap.h: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2023 Ventana Micro Systems Inc. 5 | * 6 | * Authors: 7 | * Anup Patel 8 | */ 9 | 10 | #ifndef __REGMAP_H__ 11 | #define __REGMAP_H__ 12 | 13 | #include 14 | #include 15 | 16 | /** Representation of a regmap instance */ 17 | struct regmap { 18 | /** Uniquie ID of the regmap instance assigned by the driver */ 19 | unsigned int id; 20 | 21 | /** Configuration of regmap registers */ 22 | int reg_shift; 23 | int reg_stride; 24 | unsigned int reg_base; 25 | unsigned int reg_max; 26 | 27 | /** Read a regmap register */ 28 | int (*reg_read)(struct regmap *rmap, unsigned int reg, 29 | unsigned int *val); 30 | 31 | /** Write a regmap register */ 32 | int (*reg_write)(struct regmap *rmap, unsigned int reg, 33 | unsigned int val); 34 | 35 | /** Read-modify-write a regmap register */ 36 | int (*reg_update_bits)(struct regmap *rmap, unsigned int reg, 37 | unsigned int mask, unsigned int val); 38 | 39 | /** List */ 40 | struct sbi_dlist node; 41 | }; 42 | 43 | static inline struct regmap *to_regmap(struct sbi_dlist *node) 44 | { 45 | return container_of(node, struct regmap, node); 46 | } 47 | 48 | /** Find a registered regmap instance */ 49 | struct regmap *regmap_find(unsigned int id); 50 | 51 | /** Register a regmap instance */ 52 | int regmap_add(struct regmap *rmap); 53 | 54 | /** Un-register a regmap instance */ 55 | void regmap_remove(struct regmap *rmap); 56 | 57 | /** Read a register in a regmap instance */ 58 | int regmap_read(struct regmap *rmap, unsigned int reg, unsigned int *val); 59 | 60 | /** Write a register in a regmap instance */ 61 | int regmap_write(struct regmap *rmap, unsigned int reg, unsigned int val); 62 | 63 | /** Read-modify-write a register in a regmap instance */ 64 | int regmap_update_bits(struct regmap *rmap, unsigned int reg, 65 | unsigned int mask, unsigned int val); 66 | 67 | #endif 68 | -------------------------------------------------------------------------------- /include/sbi_utils/serial/cadence-uart.h: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2022 StarFive Technology Co., Ltd. 5 | * 6 | * Author: Jun Liang Tan 7 | */ 8 | 9 | #ifndef __SERIAL_CADENCE_UART_H__ 10 | #define __SERIAL_CADENCE_UART_H__ 11 | 12 | #include 13 | 14 | int cadence_uart_init(unsigned long base, u32 in_freq, u32 baudrate); 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /include/sbi_utils/serial/fdt_serial.h: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2020 Western Digital Corporation or its affiliates. 5 | * 6 | * Authors: 7 | * Anup Patel 8 | */ 9 | 10 | #ifndef __FDT_SERIAL_H__ 11 | #define __FDT_SERIAL_H__ 12 | 13 | #include 14 | #include 15 | 16 | #ifdef CONFIG_FDT_SERIAL 17 | 18 | int fdt_serial_init(const void *fdt); 19 | 20 | #else 21 | 22 | static inline int fdt_serial_init(const void *fdt) { return 0; } 23 | 24 | #endif 25 | 26 | #endif 27 | -------------------------------------------------------------------------------- /include/sbi_utils/serial/gaisler-uart.h: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2021 Cobham Gaisler AB. 5 | * 6 | * Authors: 7 | * Daniel Cederman 8 | */ 9 | 10 | #ifndef __SERIAL_GAISLER_APBUART_H__ 11 | #define __SERIAL_GAISLER_APBUART_H__ 12 | 13 | #include 14 | 15 | int gaisler_uart_init(unsigned long base, u32 in_freq, u32 baudrate); 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /include/sbi_utils/serial/litex-uart.h: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2021 Gabriel Somlo 5 | * 6 | * Authors: 7 | * Gabriel Somlo 8 | */ 9 | 10 | #ifndef __SERIAL_LITEX_UART_H__ 11 | #define __SERIAL_LITEX_UART_H__ 12 | 13 | #include 14 | 15 | int litex_uart_init(unsigned long base); 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /include/sbi_utils/serial/renesas-scif.h: -------------------------------------------------------------------------------- 1 | /* SPDX-License-Identifier: BSD-2-Clause */ 2 | /* 3 | * Copyright (C) 2022 Renesas Electronics Corporation 4 | */ 5 | 6 | #ifndef __SERIAL_RENESAS_SCIF_H__ 7 | #define __SERIAL_RENESAS_SCIF_H__ 8 | 9 | int renesas_scif_init(unsigned long base, u32 in_freq, u32 baudrate); 10 | 11 | #endif /* __SERIAL_RENESAS_SCIF_H__ */ 12 | -------------------------------------------------------------------------------- /include/sbi_utils/serial/semihosting.h: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2022 Ventana Micro Systems Inc. 5 | * 6 | * Authors: 7 | * Anup Patel 8 | * Kautuk Consul 9 | */ 10 | 11 | #ifndef __SERIAL_SEMIHOSTING_H__ 12 | #define __SERIAL_SEMIHOSTING_H__ 13 | 14 | #include 15 | 16 | /** 17 | * enum semihosting_open_mode - Numeric file modes for use with semihosting_open() 18 | * MODE_READ: 'r' 19 | * MODE_BINARY: 'b' 20 | * MODE_PLUS: '+' 21 | * MODE_WRITE: 'w' 22 | * MODE_APPEND: 'a' 23 | * 24 | * These modes represent the mode string used by fopen(3) in a form which can 25 | * be passed to semihosting_open(). These do NOT correspond directly to %O_RDONLY, 26 | * %O_CREAT, etc; see fopen(3) for details. In particular, @MODE_PLUS 27 | * effectively results in adding %O_RDWR, and @MODE_WRITE will add %O_TRUNC. 28 | * For compatibility, @MODE_BINARY should be added when opening non-text files 29 | * (such as images). 30 | */ 31 | enum semihosting_open_mode { 32 | MODE_READ = 0x0, 33 | MODE_BINARY = 0x1, 34 | MODE_PLUS = 0x2, 35 | MODE_WRITE = 0x4, 36 | MODE_APPEND = 0x8, 37 | }; 38 | 39 | #ifdef CONFIG_SERIAL_SEMIHOSTING 40 | int semihosting_init(void); 41 | bool semihosting_enabled(void); 42 | #else 43 | static inline int semihosting_init(void) { return SBI_ENODEV; } 44 | static inline bool semihosting_enabled(void) { return false; } 45 | #endif 46 | 47 | #endif 48 | -------------------------------------------------------------------------------- /include/sbi_utils/serial/shakti-uart.h: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2020 Vijai Kumar K 5 | */ 6 | 7 | #ifndef __SERIAL_SHAKTI_UART_H__ 8 | #define __SERIAL_SHAKTI_UART_H__ 9 | 10 | #include 11 | 12 | int shakti_uart_init(unsigned long base, u32 in_freq, u32 baudrate); 13 | 14 | #endif 15 | -------------------------------------------------------------------------------- /include/sbi_utils/serial/sifive-uart.h: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2019 Western Digital Corporation or its affiliates. 5 | * 6 | * Authors: 7 | * Anup Patel 8 | */ 9 | 10 | #ifndef __SERIAL_SIFIVE_UART_H__ 11 | #define __SERIAL_SIFIVE_UART_H__ 12 | 13 | #include 14 | 15 | int sifive_uart_init(unsigned long base, u32 in_freq, u32 baudrate); 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /include/sbi_utils/serial/uart8250.h: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2019 Western Digital Corporation or its affiliates. 5 | * 6 | * Authors: 7 | * Anup Patel 8 | */ 9 | 10 | #ifndef __SERIAL_UART8250_H__ 11 | #define __SERIAL_UART8250_H__ 12 | 13 | #include 14 | 15 | #define UART_CAP_UUE BIT(0) /* Check UUE capability for XScale PXA UARTs */ 16 | 17 | int uart8250_init(unsigned long base, u32 in_freq, u32 baudrate, u32 reg_shift, 18 | u32 reg_width, u32 reg_offset, u32 caps); 19 | 20 | #endif 21 | -------------------------------------------------------------------------------- /include/sbi_utils/serial/xlnx_uartlite.h: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2022 Western Digital Corporation or its affiliates. 5 | * 6 | * Authors: 7 | * Alistair Francis 8 | */ 9 | #ifndef __SERIAL_XLNX_UARTLITE_H__ 10 | #define __SERIAL_XLNX_UARTLITE_H__ 11 | 12 | #include 13 | 14 | int xlnx_uartlite_init(unsigned long base); 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /include/sbi_utils/sys/htif.h: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-3-Clause 3 | * 4 | * Copyright (c) 2010-2020, The Regents of the University of California 5 | * (Regents). All Rights Reserved. 6 | */ 7 | 8 | #ifndef __SYS_HTIF_H__ 9 | #define __SYS_HTIF_H__ 10 | 11 | #include 12 | 13 | int htif_serial_init(bool custom_addr, 14 | unsigned long custom_fromhost_addr, 15 | unsigned long custom_tohost_addr); 16 | 17 | int htif_system_reset_init(bool custom_addr, 18 | unsigned long custom_fromhost_addr, 19 | unsigned long custom_tohost_addr); 20 | 21 | #endif 22 | -------------------------------------------------------------------------------- /include/sbi_utils/timer/aclint_mtimer.h: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2021 Western Digital Corporation or its affiliates. 5 | * 6 | * Authors: 7 | * Anup Patel 8 | */ 9 | 10 | #ifndef __TIMER_ACLINT_MTIMER_H__ 11 | #define __TIMER_ACLINT_MTIMER_H__ 12 | 13 | #include 14 | 15 | #define ACLINT_MTIMER_ALIGN 0x8 16 | #define ACLINT_MTIMER_MAX_HARTS 4095 17 | 18 | #define ACLINT_DEFAULT_MTIME_OFFSET 0x7ff8 19 | #define ACLINT_DEFAULT_MTIME_SIZE 0x8 20 | #define ACLINT_DEFAULT_MTIMECMP_OFFSET 0x0000 21 | #define ACLINT_DEFAULT_MTIMECMP_SIZE 0x7ff8 22 | 23 | #define CLINT_MTIMER_OFFSET 0x4000 24 | 25 | #define MTIMER_REGION_ALIGN 0x1000 26 | 27 | struct aclint_mtimer_data { 28 | /* Public details */ 29 | unsigned long mtime_freq; 30 | unsigned long mtime_addr; 31 | unsigned long mtime_size; 32 | unsigned long mtimecmp_addr; 33 | unsigned long mtimecmp_size; 34 | u32 first_hartid; 35 | u32 hart_count; 36 | bool has_64bit_mmio; 37 | bool has_shared_mtime; 38 | /* Private details (initialized and used by ACLINT MTIMER library) */ 39 | struct aclint_mtimer_data *time_delta_reference; 40 | unsigned long time_delta_computed; 41 | u64 (*time_rd)(volatile u64 *addr); 42 | void (*time_wr)(bool timecmp, u64 value, volatile u64 *addr); 43 | }; 44 | 45 | void aclint_mtimer_sync(struct aclint_mtimer_data *mt); 46 | 47 | void aclint_mtimer_set_reference(struct aclint_mtimer_data *mt, 48 | struct aclint_mtimer_data *ref); 49 | 50 | int aclint_mtimer_cold_init(struct aclint_mtimer_data *mt, 51 | struct aclint_mtimer_data *reference); 52 | 53 | #endif 54 | -------------------------------------------------------------------------------- /include/sbi_utils/timer/andes_plmt.h: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2022 Andes Technology Corporation 5 | * 6 | * Authors: 7 | * Zong Li 8 | * Nylon Chen 9 | * Yu Chien Peter Lin 10 | */ 11 | 12 | #ifndef __TIMER_ANDES_PLMT_H__ 13 | #define __TIMER_ANDES_PLMT_H__ 14 | 15 | #define DEFAULT_AE350_PLMT_FREQ 60000000 16 | #define PLMT_REGION_ALIGN 0x1000 17 | 18 | struct plmt_data { 19 | u32 hart_count; 20 | unsigned long size; 21 | unsigned long timer_freq; 22 | volatile u64 *time_val; 23 | volatile u64 *time_cmp; 24 | }; 25 | 26 | int plmt_cold_timer_init(struct plmt_data *plmt); 27 | 28 | #endif /* __TIMER_ANDES_PLMT_H__ */ 29 | -------------------------------------------------------------------------------- /include/sbi_utils/timer/fdt_timer.h: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2020 Western Digital Corporation or its affiliates. 5 | * 6 | * Authors: 7 | * Anup Patel 8 | */ 9 | 10 | #ifndef __FDT_TIMER_H__ 11 | #define __FDT_TIMER_H__ 12 | 13 | #include 14 | #include 15 | 16 | #ifdef CONFIG_FDT_TIMER 17 | 18 | int fdt_timer_init(void); 19 | 20 | #else 21 | 22 | static inline int fdt_timer_init(void) { return 0; } 23 | 24 | #endif 25 | 26 | #endif 27 | -------------------------------------------------------------------------------- /lib/sbi/Kconfig: -------------------------------------------------------------------------------- 1 | # SPDX-License-Identifier: BSD-2-Clause 2 | 3 | menu "Generic SBI Support" 4 | 5 | config CONSOLE_EARLY_BUFFER_SIZE 6 | int "Early console buffer size (bytes)" 7 | default 256 8 | 9 | config SBI_ECALL_TIME 10 | bool "Timer extension" 11 | default y 12 | 13 | config SBI_ECALL_RFENCE 14 | bool "RFENCE extension" 15 | default y 16 | 17 | config SBI_ECALL_IPI 18 | bool "IPI extension" 19 | default y 20 | 21 | config SBI_ECALL_HSM 22 | bool "Hart State Management extension" 23 | default y 24 | 25 | config SBI_ECALL_SRST 26 | bool "System Reset extension" 27 | default y 28 | 29 | config SBI_ECALL_SUSP 30 | bool "System Suspend extension" 31 | default y 32 | 33 | config SBI_ECALL_PMU 34 | bool "Performance Monitoring Unit extension" 35 | default y 36 | 37 | config SBI_ECALL_DBCN 38 | bool "Debug Console extension" 39 | default y 40 | 41 | config SBI_ECALL_CPPC 42 | bool "CPPC extension" 43 | default y 44 | 45 | config SBI_ECALL_FWFT 46 | bool "Firmware Feature extension" 47 | default y 48 | 49 | config SBI_ECALL_LEGACY 50 | bool "SBI v0.1 legacy extensions" 51 | default y 52 | 53 | config SBI_ECALL_VENDOR 54 | bool "Platform-defined vendor extensions" 55 | default y 56 | 57 | config SBI_ECALL_DBTR 58 | bool "Debug Trigger Extension" 59 | default y 60 | 61 | config SBIUNIT 62 | bool "Enable SBIUNIT tests" 63 | default n 64 | 65 | config SBI_ECALL_SSE 66 | bool "SSE extension" 67 | default y 68 | 69 | config SBI_ECALL_MPXY 70 | bool "MPXY extension" 71 | default y 72 | endmenu 73 | -------------------------------------------------------------------------------- /lib/sbi/sbi_bitmap.c: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2020 Western Digital Corporation or its affiliates. 5 | * 6 | * Authors: 7 | * Anup Patel 8 | */ 9 | 10 | #include 11 | 12 | void __bitmap_and(unsigned long *dst, const unsigned long *bitmap1, 13 | const unsigned long *bitmap2, int bits) 14 | { 15 | int k; 16 | int nr = BITS_TO_LONGS(bits); 17 | 18 | for (k = 0; k < nr; k++) 19 | dst[k] = bitmap1[k] & bitmap2[k]; 20 | } 21 | 22 | void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1, 23 | const unsigned long *bitmap2, int bits) 24 | { 25 | int k; 26 | int nr = BITS_TO_LONGS(bits); 27 | 28 | for (k = 0; k < nr; k++) 29 | dst[k] = bitmap1[k] | bitmap2[k]; 30 | } 31 | 32 | void __bitmap_xor(unsigned long *dst, const unsigned long *bitmap1, 33 | const unsigned long *bitmap2, int bits) 34 | { 35 | int k; 36 | int nr = BITS_TO_LONGS(bits); 37 | 38 | for (k = 0; k < nr; k++) 39 | dst[k] = bitmap1[k] ^ bitmap2[k]; 40 | } 41 | -------------------------------------------------------------------------------- /lib/sbi/sbi_double_trap.c: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2024 Rivos Inc. 5 | * 6 | * Authors: 7 | * Clément Léger 8 | */ 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | int sbi_double_trap_handler(struct sbi_trap_context *tcntx) 18 | { 19 | struct sbi_trap_regs *regs = &tcntx->regs; 20 | const struct sbi_trap_info *trap = &tcntx->trap; 21 | bool prev_virt = sbi_regs_from_virt(regs); 22 | 23 | if (sbi_mstatus_prev_mode(regs->mstatus) != PRV_S) 24 | return SBI_ERR_INVALID_PARAM; 25 | 26 | /* Exception was taken in VS-mode, redirect it to S-mode */ 27 | if (prev_virt) 28 | return sbi_trap_redirect(regs, trap); 29 | 30 | return sbi_sse_inject_event(SBI_SSE_EVENT_LOCAL_DOUBLE_TRAP); 31 | } 32 | 33 | void sbi_double_trap_init(struct sbi_scratch *scratch) 34 | { 35 | if (sbi_hart_has_extension(scratch, SBI_HART_EXT_SSDBLTRP)) 36 | sbi_sse_add_event(SBI_SSE_EVENT_LOCAL_DOUBLE_TRAP, NULL); 37 | } -------------------------------------------------------------------------------- /lib/sbi/sbi_ecall_cppc.c: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2023 Ventana Micro Systems Inc. 5 | * 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | static int sbi_ecall_cppc_handler(unsigned long extid, unsigned long funcid, 15 | struct sbi_trap_regs *regs, 16 | struct sbi_ecall_return *out) 17 | { 18 | int ret = 0; 19 | uint64_t temp; 20 | 21 | switch (funcid) { 22 | case SBI_EXT_CPPC_READ: 23 | ret = sbi_cppc_read(regs->a0, &temp); 24 | out->value = temp; 25 | break; 26 | case SBI_EXT_CPPC_READ_HI: 27 | #if __riscv_xlen == 32 28 | ret = sbi_cppc_read(regs->a0, &temp); 29 | out->value = temp >> 32; 30 | #else 31 | out->value = 0; 32 | #endif 33 | break; 34 | case SBI_EXT_CPPC_WRITE: 35 | #if __riscv_xlen == 32 36 | temp = regs->a2; 37 | temp = (temp << 32) | regs->a1; 38 | #else 39 | temp = regs->a1; 40 | #endif 41 | ret = sbi_cppc_write(regs->a0, temp); 42 | break; 43 | case SBI_EXT_CPPC_PROBE: 44 | ret = sbi_cppc_probe(regs->a0); 45 | if (ret >= 0) { 46 | out->value = ret; 47 | ret = 0; 48 | } 49 | break; 50 | default: 51 | ret = SBI_ENOTSUPP; 52 | } 53 | 54 | return ret; 55 | } 56 | 57 | struct sbi_ecall_extension ecall_cppc; 58 | 59 | static int sbi_ecall_cppc_register_extensions(void) 60 | { 61 | if (!sbi_cppc_get_device()) 62 | return 0; 63 | 64 | return sbi_ecall_register_extension(&ecall_cppc); 65 | } 66 | 67 | struct sbi_ecall_extension ecall_cppc = { 68 | .name = "cppc", 69 | .extid_start = SBI_EXT_CPPC, 70 | .extid_end = SBI_EXT_CPPC, 71 | .register_extensions = sbi_ecall_cppc_register_extensions, 72 | .handle = sbi_ecall_cppc_handler, 73 | }; 74 | -------------------------------------------------------------------------------- /lib/sbi/sbi_ecall_exts.carray: -------------------------------------------------------------------------------- 1 | HEADER: sbi/sbi_ecall.h 2 | TYPE: struct sbi_ecall_extension 3 | NAME: sbi_ecall_exts 4 | -------------------------------------------------------------------------------- /lib/sbi/sbi_ecall_fwft.c: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2024 Rivos Inc. 5 | * 6 | * Authors: 7 | * Clément Léger 8 | */ 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | static int sbi_ecall_fwft_handler(unsigned long extid, unsigned long funcid, 17 | struct sbi_trap_regs *regs, 18 | struct sbi_ecall_return *out) 19 | { 20 | int ret = 0; 21 | 22 | switch (funcid) { 23 | case SBI_EXT_FWFT_SET: 24 | ret = sbi_fwft_set(regs->a0, regs->a1, regs->a2); 25 | break; 26 | case SBI_EXT_FWFT_GET: 27 | ret = sbi_fwft_get(regs->a0, &out->value); 28 | break; 29 | default: 30 | ret = SBI_ENOTSUPP; 31 | break; 32 | } 33 | 34 | return ret; 35 | } 36 | 37 | struct sbi_ecall_extension ecall_fwft; 38 | 39 | static int sbi_ecall_fwft_register_extensions(void) 40 | { 41 | return sbi_ecall_register_extension(&ecall_fwft); 42 | } 43 | 44 | struct sbi_ecall_extension ecall_fwft = { 45 | .name = "fwft", 46 | .extid_start = SBI_EXT_FWFT, 47 | .extid_end = SBI_EXT_FWFT, 48 | .register_extensions = sbi_ecall_fwft_register_extensions, 49 | .handle = sbi_ecall_fwft_handler, 50 | }; 51 | -------------------------------------------------------------------------------- /lib/sbi/sbi_ecall_hsm.c: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2020 Western Digital Corporation or its affiliates. 5 | * 6 | * Authors: 7 | * Atish Patra 8 | */ 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | 19 | static int sbi_ecall_hsm_handler(unsigned long extid, unsigned long funcid, 20 | struct sbi_trap_regs *regs, 21 | struct sbi_ecall_return *out) 22 | { 23 | int ret = 0; 24 | struct sbi_scratch *scratch = sbi_scratch_thishart_ptr(); 25 | ulong smode = (csr_read(CSR_MSTATUS) & MSTATUS_MPP) >> 26 | MSTATUS_MPP_SHIFT; 27 | 28 | switch (funcid) { 29 | case SBI_EXT_HSM_HART_START: 30 | ret = sbi_hsm_hart_start(scratch, sbi_domain_thishart_ptr(), 31 | regs->a0, regs->a1, smode, regs->a2); 32 | break; 33 | case SBI_EXT_HSM_HART_STOP: 34 | ret = sbi_hsm_hart_stop(scratch, true); 35 | break; 36 | case SBI_EXT_HSM_HART_GET_STATUS: 37 | ret = sbi_hsm_hart_get_state(sbi_domain_thishart_ptr(), 38 | regs->a0); 39 | break; 40 | case SBI_EXT_HSM_HART_SUSPEND: 41 | ret = sbi_hsm_hart_suspend(scratch, regs->a0, regs->a1, 42 | smode, regs->a2); 43 | break; 44 | default: 45 | ret = SBI_ENOTSUPP; 46 | } 47 | 48 | if (ret >= 0) { 49 | out->value = ret; 50 | ret = 0; 51 | } 52 | 53 | return ret; 54 | } 55 | 56 | struct sbi_ecall_extension ecall_hsm; 57 | 58 | static int sbi_ecall_hsm_register_extensions(void) 59 | { 60 | return sbi_ecall_register_extension(&ecall_hsm); 61 | } 62 | 63 | struct sbi_ecall_extension ecall_hsm = { 64 | .name = "hsm", 65 | .extid_start = SBI_EXT_HSM, 66 | .extid_end = SBI_EXT_HSM, 67 | .register_extensions = sbi_ecall_hsm_register_extensions, 68 | .handle = sbi_ecall_hsm_handler, 69 | }; 70 | -------------------------------------------------------------------------------- /lib/sbi/sbi_ecall_ipi.c: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2020 Western Digital Corporation or its affiliates. 5 | * 6 | * Authors: 7 | * Anup Patel 8 | * Atish Patra 9 | */ 10 | 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | static int sbi_ecall_ipi_handler(unsigned long extid, unsigned long funcid, 18 | struct sbi_trap_regs *regs, 19 | struct sbi_ecall_return *out) 20 | { 21 | int ret = 0; 22 | 23 | if (funcid == SBI_EXT_IPI_SEND_IPI) 24 | ret = sbi_ipi_send_smode(regs->a0, regs->a1); 25 | else 26 | ret = SBI_ENOTSUPP; 27 | 28 | return ret; 29 | } 30 | 31 | struct sbi_ecall_extension ecall_ipi; 32 | 33 | static int sbi_ecall_ipi_register_extensions(void) 34 | { 35 | return sbi_ecall_register_extension(&ecall_ipi); 36 | } 37 | 38 | struct sbi_ecall_extension ecall_ipi = { 39 | .name = "ipi", 40 | .extid_start = SBI_EXT_IPI, 41 | .extid_end = SBI_EXT_IPI, 42 | .register_extensions = sbi_ecall_ipi_register_extensions, 43 | .handle = sbi_ecall_ipi_handler, 44 | }; 45 | -------------------------------------------------------------------------------- /lib/sbi/sbi_ecall_sse.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | static int sbi_ecall_sse_handler(unsigned long extid, unsigned long funcid, 7 | struct sbi_trap_regs *regs, 8 | struct sbi_ecall_return *out) 9 | { 10 | int ret; 11 | 12 | switch (funcid) { 13 | case SBI_EXT_SSE_READ_ATTR: 14 | ret = sbi_sse_read_attrs(regs->a0, regs->a1, regs->a2, 15 | regs->a3, regs->a4); 16 | break; 17 | case SBI_EXT_SSE_WRITE_ATTR: 18 | ret = sbi_sse_write_attrs(regs->a0, regs->a1, regs->a2, 19 | regs->a3, regs->a4); 20 | break; 21 | case SBI_EXT_SSE_REGISTER: 22 | ret = sbi_sse_register(regs->a0, regs->a1, regs->a2); 23 | break; 24 | case SBI_EXT_SSE_UNREGISTER: 25 | ret = sbi_sse_unregister(regs->a0); 26 | break; 27 | case SBI_EXT_SSE_ENABLE: 28 | ret = sbi_sse_enable(regs->a0); 29 | break; 30 | case SBI_EXT_SSE_DISABLE: 31 | ret = sbi_sse_disable(regs->a0); 32 | break; 33 | case SBI_EXT_SSE_COMPLETE: 34 | ret = sbi_sse_complete(regs, out); 35 | break; 36 | case SBI_EXT_SSE_INJECT: 37 | ret = sbi_sse_inject_from_ecall(regs->a0, regs->a1, out); 38 | break; 39 | case SBI_EXT_SSE_HART_MASK: 40 | ret = sbi_sse_hart_mask(); 41 | break; 42 | case SBI_EXT_SSE_HART_UNMASK: 43 | ret = sbi_sse_hart_unmask(); 44 | break; 45 | default: 46 | ret = SBI_ENOTSUPP; 47 | } 48 | return ret; 49 | } 50 | 51 | struct sbi_ecall_extension ecall_sse; 52 | 53 | static int sbi_ecall_sse_register_extensions(void) 54 | { 55 | return sbi_ecall_register_extension(&ecall_sse); 56 | } 57 | 58 | struct sbi_ecall_extension ecall_sse = { 59 | .name = "sse", 60 | .extid_start = SBI_EXT_SSE, 61 | .extid_end = SBI_EXT_SSE, 62 | .register_extensions = sbi_ecall_sse_register_extensions, 63 | .handle = sbi_ecall_sse_handler, 64 | }; 65 | -------------------------------------------------------------------------------- /lib/sbi/sbi_ecall_susp.c: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: BSD-2-Clause 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | static int sbi_ecall_susp_handler(unsigned long extid, unsigned long funcid, 9 | struct sbi_trap_regs *regs, 10 | struct sbi_ecall_return *out) 11 | { 12 | int ret = SBI_ENOTSUPP; 13 | 14 | if (funcid == SBI_EXT_SUSP_SUSPEND) 15 | ret = sbi_system_suspend(regs->a0, regs->a1, regs->a2); 16 | 17 | if (ret >= 0) { 18 | out->value = ret; 19 | ret = 0; 20 | } 21 | 22 | return ret; 23 | } 24 | 25 | static bool susp_available(void) 26 | { 27 | u32 type; 28 | 29 | /* 30 | * At least one suspend type should be supported by the 31 | * platform for the SBI SUSP extension to be usable. 32 | */ 33 | for (type = 0; type <= SBI_SUSP_SLEEP_TYPE_LAST; type++) { 34 | if (sbi_system_suspend_supported(type)) 35 | return true; 36 | } 37 | 38 | return false; 39 | } 40 | 41 | struct sbi_ecall_extension ecall_susp; 42 | 43 | static int sbi_ecall_susp_register_extensions(void) 44 | { 45 | if (!susp_available()) 46 | return 0; 47 | 48 | return sbi_ecall_register_extension(&ecall_susp); 49 | } 50 | 51 | struct sbi_ecall_extension ecall_susp = { 52 | .name = "susp", 53 | .extid_start = SBI_EXT_SUSP, 54 | .extid_end = SBI_EXT_SUSP, 55 | .register_extensions = sbi_ecall_susp_register_extensions, 56 | .handle = sbi_ecall_susp_handler, 57 | }; 58 | -------------------------------------------------------------------------------- /lib/sbi/sbi_ecall_time.c: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2020 Western Digital Corporation or its affiliates. 5 | * 6 | * Authors: 7 | * Anup Patel 8 | * Atish Patra 9 | */ 10 | 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | static int sbi_ecall_time_handler(unsigned long extid, unsigned long funcid, 18 | struct sbi_trap_regs *regs, 19 | struct sbi_ecall_return *out) 20 | { 21 | int ret = 0; 22 | 23 | if (funcid == SBI_EXT_TIME_SET_TIMER) { 24 | #if __riscv_xlen == 32 25 | sbi_timer_event_start((((u64)regs->a1 << 32) | (u64)regs->a0)); 26 | #else 27 | sbi_timer_event_start((u64)regs->a0); 28 | #endif 29 | } else 30 | ret = SBI_ENOTSUPP; 31 | 32 | return ret; 33 | } 34 | 35 | struct sbi_ecall_extension ecall_time; 36 | 37 | static int sbi_ecall_time_register_extensions(void) 38 | { 39 | return sbi_ecall_register_extension(&ecall_time); 40 | } 41 | 42 | struct sbi_ecall_extension ecall_time = { 43 | .name = "time", 44 | .extid_start = SBI_EXT_TIME, 45 | .extid_end = SBI_EXT_TIME, 46 | .register_extensions = sbi_ecall_time_register_extensions, 47 | .handle = sbi_ecall_time_handler, 48 | }; 49 | -------------------------------------------------------------------------------- /lib/sbi/sbi_ecall_vendor.c: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2020 Western Digital Corporation or its affiliates. 5 | * 6 | * Authors: 7 | * Anup Patel 8 | * Atish Patra 9 | */ 10 | 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | 18 | static inline unsigned long sbi_ecall_vendor_id(void) 19 | { 20 | return SBI_EXT_VENDOR_START + 21 | (csr_read(CSR_MVENDORID) & 22 | (SBI_EXT_VENDOR_END - SBI_EXT_VENDOR_START)); 23 | } 24 | 25 | static int sbi_ecall_vendor_handler(unsigned long extid, unsigned long funcid, 26 | struct sbi_trap_regs *regs, 27 | struct sbi_ecall_return *out) 28 | { 29 | return sbi_platform_vendor_ext_provider(sbi_platform_thishart_ptr(), 30 | funcid, regs, out); 31 | } 32 | 33 | struct sbi_ecall_extension ecall_vendor; 34 | 35 | static int sbi_ecall_vendor_register_extensions(void) 36 | { 37 | unsigned long extid = sbi_ecall_vendor_id(); 38 | 39 | if (!sbi_platform_vendor_ext_check(sbi_platform_thishart_ptr())) 40 | return 0; 41 | 42 | ecall_vendor.extid_start = extid; 43 | ecall_vendor.extid_end = extid; 44 | 45 | return sbi_ecall_register_extension(&ecall_vendor); 46 | } 47 | 48 | struct sbi_ecall_extension ecall_vendor = { 49 | .name = "vendor", 50 | .extid_start = SBI_EXT_VENDOR_START, 51 | .extid_end = SBI_EXT_VENDOR_END, 52 | .register_extensions = sbi_ecall_vendor_register_extensions, 53 | .handle = sbi_ecall_vendor_handler, 54 | }; 55 | -------------------------------------------------------------------------------- /lib/sbi/sbi_expected_trap.S: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2020 Western Digital Corporation or its affiliates. 5 | * 6 | * Authors: 7 | * Anup Patel 8 | */ 9 | 10 | #include 11 | #include 12 | 13 | /* 14 | * We assume that faulting instruction is 4-byte long and blindly 15 | * increment SEPC by 4. 16 | * 17 | * The trap info will be saved as follows: 18 | * A3 <- pointer struct sbi_trap_info 19 | * A4 <- temporary 20 | */ 21 | 22 | .align 3 23 | .global __sbi_expected_trap 24 | __sbi_expected_trap: 25 | /* Without H-extension so, MTVAL2 and MTINST CSRs and GVA not available */ 26 | csrr a4, CSR_MCAUSE 27 | REG_S a4, SBI_TRAP_INFO_OFFSET(cause)(a3) 28 | csrr a4, CSR_MTVAL 29 | REG_S a4, SBI_TRAP_INFO_OFFSET(tval)(a3) 30 | REG_S zero, SBI_TRAP_INFO_OFFSET(tval2)(a3) 31 | REG_S zero, SBI_TRAP_INFO_OFFSET(tinst)(a3) 32 | REG_S zero, SBI_TRAP_INFO_OFFSET(gva)(a3) 33 | csrr a4, CSR_MEPC 34 | addi a4, a4, 4 35 | csrw CSR_MEPC, a4 36 | mret 37 | 38 | .align 3 39 | .global __sbi_expected_trap_hext 40 | __sbi_expected_trap_hext: 41 | /* With H-extension so, MTVAL2 and MTINST CSRs and GVA available */ 42 | csrr a4, CSR_MCAUSE 43 | REG_S a4, SBI_TRAP_INFO_OFFSET(cause)(a3) 44 | csrr a4, CSR_MTVAL 45 | REG_S a4, SBI_TRAP_INFO_OFFSET(tval)(a3) 46 | csrr a4, CSR_MTVAL2 47 | REG_S a4, SBI_TRAP_INFO_OFFSET(tval2)(a3) 48 | csrr a4, CSR_MTINST 49 | REG_S a4, SBI_TRAP_INFO_OFFSET(tinst)(a3) 50 | 51 | /* Extract GVA bit from MSTATUS or MSTATUSH */ 52 | #if __riscv_xlen == 32 53 | csrr a4, CSR_MSTATUSH 54 | srli a4, a4, MSTATUSH_GVA_SHIFT 55 | #else 56 | csrr a4, CSR_MSTATUS 57 | srli a4, a4, MSTATUS_GVA_SHIFT 58 | #endif 59 | andi a4, a4, 1 60 | REG_S a4, SBI_TRAP_INFO_OFFSET(gva)(a3) 61 | 62 | csrr a4, CSR_MEPC 63 | addi a4, a4, 4 64 | csrw CSR_MEPC, a4 65 | mret 66 | -------------------------------------------------------------------------------- /lib/sbi/sbi_irqchip.c: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2022 Ventana Micro Systems Inc. 5 | * 6 | * Authors: 7 | * Anup Patel 8 | */ 9 | 10 | #include 11 | #include 12 | #include 13 | 14 | static SBI_LIST_HEAD(irqchip_list); 15 | 16 | static int default_irqfn(void) 17 | { 18 | return SBI_ENODEV; 19 | } 20 | 21 | static int (*ext_irqfn)(void) = default_irqfn; 22 | 23 | int sbi_irqchip_process(void) 24 | { 25 | return ext_irqfn(); 26 | } 27 | 28 | void sbi_irqchip_add_device(struct sbi_irqchip_device *dev) 29 | { 30 | sbi_list_add_tail(&dev->node, &irqchip_list); 31 | 32 | if (dev->irq_handle) 33 | ext_irqfn = dev->irq_handle; 34 | } 35 | 36 | int sbi_irqchip_init(struct sbi_scratch *scratch, bool cold_boot) 37 | { 38 | int rc; 39 | const struct sbi_platform *plat = sbi_platform_ptr(scratch); 40 | struct sbi_irqchip_device *dev; 41 | 42 | if (cold_boot) { 43 | rc = sbi_platform_irqchip_init(plat); 44 | if (rc) 45 | return rc; 46 | } 47 | 48 | sbi_list_for_each_entry(dev, &irqchip_list, node) { 49 | if (!dev->warm_init) 50 | continue; 51 | rc = dev->warm_init(dev); 52 | if (rc) 53 | return rc; 54 | } 55 | 56 | if (ext_irqfn != default_irqfn) 57 | csr_set(CSR_MIE, MIP_MEIP); 58 | 59 | return 0; 60 | } 61 | 62 | void sbi_irqchip_exit(struct sbi_scratch *scratch) 63 | { 64 | if (ext_irqfn != default_irqfn) 65 | csr_clear(CSR_MIE, MIP_MEIP); 66 | } 67 | -------------------------------------------------------------------------------- /lib/sbi/sbi_math.c: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2020 Western Digital Corporation or its affiliates. 5 | * 6 | * Common helper functions used across OpenSBI project. 7 | * 8 | * Authors: 9 | * Atish Patra 10 | */ 11 | 12 | unsigned long log2roundup(unsigned long x) 13 | { 14 | unsigned long ret = 0; 15 | 16 | while (ret < __riscv_xlen) { 17 | if (x <= (1UL << ret)) 18 | break; 19 | ret++; 20 | } 21 | 22 | return ret; 23 | } 24 | -------------------------------------------------------------------------------- /lib/sbi/sbi_platform.c: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2020 Western Digital Corporation or its affiliates. 5 | * 6 | * Authors: 7 | * Atish Patra 8 | */ 9 | 10 | #include 11 | #include 12 | #include 13 | 14 | static inline char *sbi_platform_feature_id2string(unsigned long feature) 15 | { 16 | char *fstr = NULL; 17 | 18 | if (!feature) 19 | return NULL; 20 | 21 | switch (feature) { 22 | case SBI_PLATFORM_HAS_MFAULTS_DELEGATION: 23 | fstr = "medeleg"; 24 | break; 25 | default: 26 | break; 27 | } 28 | 29 | return fstr; 30 | } 31 | 32 | void sbi_platform_get_features_str(const struct sbi_platform *plat, 33 | char *features_str, int nfstr) 34 | { 35 | unsigned long features, feat = 1UL; 36 | char *temp; 37 | int offset = 0; 38 | 39 | if (!plat || !features_str || !nfstr) 40 | return; 41 | sbi_memset(features_str, 0, nfstr); 42 | 43 | features = sbi_platform_get_features(plat); 44 | if (!features) 45 | goto done; 46 | 47 | do { 48 | if (features & feat) { 49 | temp = sbi_platform_feature_id2string(feat); 50 | if (temp) { 51 | int len = sbi_snprintf(features_str + offset, 52 | nfstr - offset, 53 | "%s,", temp); 54 | if (len < 0) 55 | break; 56 | 57 | if (offset + len >= nfstr) { 58 | /* No more space for features */ 59 | offset = nfstr; 60 | break; 61 | } else 62 | offset = offset + len; 63 | } 64 | } 65 | feat = feat << 1; 66 | } while (feat <= SBI_PLATFORM_HAS_LAST_FEATURE); 67 | 68 | done: 69 | if (offset) 70 | features_str[offset - 1] = '\0'; 71 | else 72 | sbi_strncpy(features_str, "none", nfstr); 73 | } 74 | -------------------------------------------------------------------------------- /lib/sbi/tests/objects.mk: -------------------------------------------------------------------------------- 1 | libsbi-objs-$(CONFIG_SBIUNIT) += tests/sbi_unit_test.o 2 | libsbi-objs-$(CONFIG_SBIUNIT) += tests/sbi_unit_tests.carray.o 3 | 4 | carray-sbi_unit_tests-$(CONFIG_SBIUNIT) += bitmap_test_suite 5 | libsbi-objs-$(CONFIG_SBIUNIT) += tests/sbi_bitmap_test.o 6 | 7 | carray-sbi_unit_tests-$(CONFIG_SBIUNIT) += console_test_suite 8 | libsbi-objs-$(CONFIG_SBIUNIT) += tests/sbi_console_test.o 9 | 10 | carray-sbi_unit_tests-$(CONFIG_SBIUNIT) += atomic_test_suite 11 | libsbi-objs-$(CONFIG_SBIUNIT) += tests/riscv_atomic_test.o 12 | 13 | carray-sbi_unit_tests-$(CONFIG_SBIUNIT) += locks_test_suite 14 | libsbi-objs-$(CONFIG_SBIUNIT) += tests/riscv_locks_test.o 15 | 16 | carray-sbi_unit_tests-$(CONFIG_SBIUNIT) += math_test_suite 17 | libsbi-objs-$(CONFIG_SBIUNIT) += tests/sbi_math_test.o 18 | 19 | carray-sbi_unit_tests-$(CONFIG_SBIUNIT) += ecall_test_suite 20 | libsbi-objs-$(CONFIG_SBIUNIT) += tests/sbi_ecall_test.o 21 | 22 | carray-sbi_unit_tests-$(CONFIG_SBIUNIT) += bitops_test_suite 23 | libsbi-objs-$(CONFIG_SBIUNIT) += tests/sbi_bitops_test.o 24 | -------------------------------------------------------------------------------- /lib/sbi/tests/riscv_locks_test.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | static spinlock_t test_lock = SPIN_LOCK_INITIALIZER; 5 | 6 | static void spin_lock_test(struct sbiunit_test_case *test) 7 | { 8 | /* We don't want to accidentally get locked */ 9 | SBIUNIT_ASSERT(test, !spin_lock_check(&test_lock)); 10 | 11 | spin_lock(&test_lock); 12 | SBIUNIT_EXPECT(test, spin_lock_check(&test_lock)); 13 | spin_unlock(&test_lock); 14 | 15 | SBIUNIT_ASSERT(test, !spin_lock_check(&test_lock)); 16 | } 17 | 18 | static void spin_trylock_fail(struct sbiunit_test_case *test) 19 | { 20 | /* We don't want to accidentally get locked */ 21 | SBIUNIT_ASSERT(test, !spin_lock_check(&test_lock)); 22 | 23 | spin_lock(&test_lock); 24 | SBIUNIT_EXPECT(test, !spin_trylock(&test_lock)); 25 | spin_unlock(&test_lock); 26 | } 27 | 28 | static void spin_trylock_success(struct sbiunit_test_case *test) 29 | { 30 | SBIUNIT_EXPECT(test, spin_trylock(&test_lock)); 31 | spin_unlock(&test_lock); 32 | } 33 | 34 | static struct sbiunit_test_case locks_test_cases[] = { 35 | SBIUNIT_TEST_CASE(spin_lock_test), 36 | SBIUNIT_TEST_CASE(spin_trylock_fail), 37 | SBIUNIT_TEST_CASE(spin_trylock_success), 38 | SBIUNIT_END_CASE, 39 | }; 40 | 41 | SBIUNIT_TEST_SUITE(locks_test_suite, locks_test_cases); 42 | -------------------------------------------------------------------------------- /lib/sbi/tests/sbi_ecall_test.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | static void test_sbi_ecall_version(struct sbiunit_test_case *test) 6 | { 7 | SBIUNIT_EXPECT_EQ(test, sbi_ecall_version_major(), SBI_ECALL_VERSION_MAJOR); 8 | SBIUNIT_EXPECT_EQ(test, sbi_ecall_version_minor(), SBI_ECALL_VERSION_MINOR); 9 | } 10 | 11 | static void test_sbi_ecall_impid(struct sbiunit_test_case *test) 12 | { 13 | unsigned long old_impid = sbi_ecall_get_impid(); 14 | sbi_ecall_set_impid(42); 15 | SBIUNIT_EXPECT_EQ(test, sbi_ecall_get_impid(), 42); 16 | sbi_ecall_set_impid(old_impid); 17 | } 18 | 19 | static int dummy_handler(unsigned long extid, unsigned long funcid, 20 | struct sbi_trap_regs *regs, 21 | struct sbi_ecall_return *out) 22 | { 23 | return 0; 24 | } 25 | 26 | static void test_sbi_ecall_register_find_extension(struct sbiunit_test_case *test) 27 | { 28 | struct sbi_ecall_extension test_ext = { 29 | /* Use experimental extension space for no overlap */ 30 | .extid_start = SBI_EXT_EXPERIMENTAL_START, 31 | .extid_end = SBI_EXT_EXPERIMENTAL_START, 32 | .name = "TestExt", 33 | .handle = dummy_handler, 34 | }; 35 | 36 | SBIUNIT_EXPECT_EQ(test, sbi_ecall_register_extension(&test_ext), 0); 37 | SBIUNIT_EXPECT_EQ(test, sbi_ecall_find_extension(SBI_EXT_EXPERIMENTAL_START), &test_ext); 38 | 39 | sbi_ecall_unregister_extension(&test_ext); 40 | SBIUNIT_EXPECT_EQ(test, sbi_ecall_find_extension(SBI_EXT_EXPERIMENTAL_START), NULL); 41 | } 42 | 43 | static struct sbiunit_test_case ecall_tests[] = { 44 | SBIUNIT_TEST_CASE(test_sbi_ecall_version), 45 | SBIUNIT_TEST_CASE(test_sbi_ecall_impid), 46 | SBIUNIT_TEST_CASE(test_sbi_ecall_register_find_extension), 47 | SBIUNIT_END_CASE, 48 | }; 49 | 50 | SBIUNIT_TEST_SUITE(ecall_test_suite, ecall_tests); 51 | -------------------------------------------------------------------------------- /lib/sbi/tests/sbi_math_test.c: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright 2024 Beijing ESWIN Computing Technology Co., Ltd. 5 | * 6 | * Author: Dongdong Zhang 7 | */ 8 | #include 9 | #include 10 | 11 | static void log2roundup_test(struct sbiunit_test_case *test) 12 | { 13 | struct { 14 | unsigned long input; 15 | unsigned long expected; 16 | } cases[] = { 17 | {1, 0}, 18 | {2, 1}, 19 | {3, 2}, 20 | {4, 2}, 21 | {5, 3}, 22 | {8, 3}, 23 | {9, 4}, 24 | {15, 4}, 25 | {16, 4}, 26 | {17, 5}, 27 | {31, 5}, 28 | {32, 5}, 29 | {33, 6}, 30 | {63, 6}, 31 | {64, 6}, 32 | {65, 7}, 33 | }; 34 | 35 | for (int i = 0; i < sizeof(cases)/sizeof(cases[0]); i++) { 36 | unsigned long result = log2roundup(cases[i].input); 37 | SBIUNIT_EXPECT_EQ(test, result, cases[i].expected); 38 | } 39 | } 40 | 41 | static struct sbiunit_test_case math_test_cases[] = { 42 | SBIUNIT_TEST_CASE(log2roundup_test), 43 | SBIUNIT_END_CASE, 44 | }; 45 | 46 | SBIUNIT_TEST_SUITE(math_test_suite, math_test_cases); 47 | -------------------------------------------------------------------------------- /lib/sbi/tests/sbi_unit_test.c: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Author: Ivan Orlov 5 | */ 6 | #include 7 | #include 8 | #include 9 | 10 | #define ANSI_COLOR_GREEN "\x1b[32m" 11 | #define ANSI_COLOR_RED "\x1b[31m" 12 | #define ANSI_COLOR_RESET "\x1b[0m" 13 | 14 | extern struct sbiunit_test_suite *const sbi_unit_tests[]; 15 | 16 | static void run_test_suite(struct sbiunit_test_suite *suite) 17 | { 18 | struct sbiunit_test_case *s_case; 19 | u32 count_pass = 0, count_fail = 0; 20 | 21 | sbi_printf("## Running test suite: %s\n", suite->name); 22 | 23 | if (suite->init) 24 | suite->init(); 25 | 26 | s_case = suite->cases; 27 | while (s_case->test_func) { 28 | s_case->test_func(s_case); 29 | if (s_case->failed) 30 | count_fail++; 31 | else 32 | count_pass++; 33 | sbi_printf("%s[%s]%s %s\n", 34 | s_case->failed ? ANSI_COLOR_RED : ANSI_COLOR_GREEN, 35 | s_case->failed ? "FAILED" : "PASSED", 36 | ANSI_COLOR_RESET, s_case->name); 37 | s_case++; 38 | } 39 | 40 | sbi_printf("%u PASSED / %u FAILED / %u TOTAL\n", count_pass, count_fail, 41 | count_pass + count_fail); 42 | } 43 | 44 | void run_all_tests(void) 45 | { 46 | u32 i; 47 | 48 | sbi_printf("\n# Running SBIUNIT tests #\n"); 49 | 50 | for (i = 0; sbi_unit_tests[i]; i++) 51 | run_test_suite(sbi_unit_tests[i]); 52 | } 53 | -------------------------------------------------------------------------------- /lib/sbi/tests/sbi_unit_tests.carray: -------------------------------------------------------------------------------- 1 | HEADER: sbi/sbi_unit_test.h 2 | TYPE: struct sbiunit_test_suite 3 | NAME: sbi_unit_tests 4 | -------------------------------------------------------------------------------- /lib/utils/Kconfig: -------------------------------------------------------------------------------- 1 | # SPDX-License-Identifier: BSD-2-Clause 2 | 3 | menu "Utils and Drivers Support" 4 | 5 | source "$(OPENSBI_SRC_DIR)/lib/utils/cppc/Kconfig" 6 | 7 | source "$(OPENSBI_SRC_DIR)/lib/utils/fdt/Kconfig" 8 | 9 | source "$(OPENSBI_SRC_DIR)/lib/utils/gpio/Kconfig" 10 | 11 | source "$(OPENSBI_SRC_DIR)/lib/utils/hsm/Kconfig" 12 | 13 | source "$(OPENSBI_SRC_DIR)/lib/utils/i2c/Kconfig" 14 | 15 | source "$(OPENSBI_SRC_DIR)/lib/utils/ipi/Kconfig" 16 | 17 | source "$(OPENSBI_SRC_DIR)/lib/utils/irqchip/Kconfig" 18 | 19 | source "$(OPENSBI_SRC_DIR)/lib/utils/libfdt/Kconfig" 20 | 21 | source "$(OPENSBI_SRC_DIR)/lib/utils/mailbox/Kconfig" 22 | 23 | source "$(OPENSBI_SRC_DIR)/lib/utils/regmap/Kconfig" 24 | 25 | source "$(OPENSBI_SRC_DIR)/lib/utils/reset/Kconfig" 26 | 27 | source "$(OPENSBI_SRC_DIR)/lib/utils/serial/Kconfig" 28 | 29 | source "$(OPENSBI_SRC_DIR)/lib/utils/suspend/Kconfig" 30 | 31 | source "$(OPENSBI_SRC_DIR)/lib/utils/sys/Kconfig" 32 | 33 | source "$(OPENSBI_SRC_DIR)/lib/utils/timer/Kconfig" 34 | 35 | source "$(OPENSBI_SRC_DIR)/lib/utils/mpxy/Kconfig" 36 | 37 | endmenu 38 | -------------------------------------------------------------------------------- /lib/utils/cppc/Kconfig: -------------------------------------------------------------------------------- 1 | # SPDX-License-Identifier: BSD-2-Clause 2 | 3 | menu "CPPC Device Support" 4 | 5 | config FDT_CPPC 6 | bool "FDT based CPPC drivers" 7 | depends on FDT 8 | default n 9 | 10 | if FDT_CPPC 11 | 12 | config FDT_CPPC_RPMI 13 | bool "FDT RPMI CPPC driver" 14 | depends on FDT_MAILBOX && RPMI_MAILBOX 15 | default n 16 | 17 | endif 18 | 19 | endmenu 20 | -------------------------------------------------------------------------------- /lib/utils/cppc/objects.mk: -------------------------------------------------------------------------------- 1 | # 2 | # SPDX-License-Identifier: BSD-2-Clause 3 | # 4 | # Copyright (c) 2024 Ventana Micro Systems Inc. 5 | # 6 | # Authors: 7 | # Anup Patel 8 | # 9 | 10 | carray-fdt_early_drivers-$(CONFIG_FDT_CPPC_RPMI) += fdt_cppc_rpmi 11 | libsbiutils-objs-$(CONFIG_FDT_CPPC_RPMI) += cppc/fdt_cppc_rpmi.o 12 | -------------------------------------------------------------------------------- /lib/utils/fdt/Kconfig: -------------------------------------------------------------------------------- 1 | # SPDX-License-Identifier: BSD-2-Clause 2 | 3 | menuconfig FDT 4 | bool "Flattened Device Tree (FDT) support" 5 | select LIBFDT 6 | default n 7 | 8 | if FDT 9 | 10 | config FDT_DOMAIN 11 | bool "FDT domain support" 12 | default n 13 | 14 | config FDT_PMU 15 | bool "FDT performance monitoring unit (PMU) support" 16 | default n 17 | 18 | config FDT_FIXUPS_PRESERVE_PMU_NODE 19 | bool "Preserve PMU node in device-tree" 20 | depends on FDT_PMU 21 | default n 22 | help 23 | Preserve PMU node properties for debugging purposes. 24 | 25 | endif 26 | -------------------------------------------------------------------------------- /lib/utils/fdt/fdt_early_drivers.carray: -------------------------------------------------------------------------------- 1 | HEADER: sbi_utils/fdt/fdt_driver.h 2 | TYPE: const struct fdt_driver 3 | NAME: fdt_early_drivers 4 | -------------------------------------------------------------------------------- /lib/utils/fdt/objects.mk: -------------------------------------------------------------------------------- 1 | # 2 | # SPDX-License-Identifier: BSD-2-Clause 3 | # 4 | # Copyright (C) 2020 Bin Meng 5 | # 6 | 7 | libsbiutils-objs-$(CONFIG_FDT) += fdt/fdt_early_drivers.carray.o 8 | 9 | libsbiutils-objs-$(CONFIG_FDT_DOMAIN) += fdt/fdt_domain.o 10 | libsbiutils-objs-$(CONFIG_FDT_PMU) += fdt/fdt_pmu.o 11 | libsbiutils-objs-$(CONFIG_FDT) += fdt/fdt_helper.o 12 | libsbiutils-objs-$(CONFIG_FDT) += fdt/fdt_driver.o 13 | libsbiutils-objs-$(CONFIG_FDT) += fdt/fdt_fixup.o 14 | -------------------------------------------------------------------------------- /lib/utils/gpio/Kconfig: -------------------------------------------------------------------------------- 1 | # SPDX-License-Identifier: BSD-2-Clause 2 | 3 | menu "GPIO Support" 4 | 5 | config FDT_GPIO 6 | bool "FDT based GPIO drivers" 7 | depends on FDT 8 | select GPIO 9 | default n 10 | 11 | if FDT_GPIO 12 | 13 | config FDT_GPIO_DESIGNWARE 14 | bool "DesignWare GPIO driver" 15 | default n 16 | 17 | config FDT_GPIO_SIFIVE 18 | bool "SiFive GPIO FDT driver" 19 | default n 20 | 21 | config FDT_GPIO_STARFIVE 22 | bool "StarFive GPIO FDT driver" 23 | default n 24 | endif 25 | 26 | config GPIO 27 | bool "GPIO support" 28 | default n 29 | 30 | endmenu 31 | -------------------------------------------------------------------------------- /lib/utils/gpio/fdt_gpio_drivers.carray: -------------------------------------------------------------------------------- 1 | HEADER: sbi_utils/gpio/fdt_gpio.h 2 | TYPE: const struct fdt_gpio 3 | NAME: fdt_gpio_drivers 4 | MEMBER-NAME: driver 5 | MEMBER-TYPE: const struct fdt_driver 6 | -------------------------------------------------------------------------------- /lib/utils/gpio/objects.mk: -------------------------------------------------------------------------------- 1 | # 2 | # SPDX-License-Identifier: BSD-2-Clause 3 | # 4 | # Copyright (c) 2021 Western Digital Corporation or its affiliates. 5 | # 6 | # Authors: 7 | # Anup Patel 8 | # 9 | 10 | libsbiutils-objs-$(CONFIG_FDT_GPIO) += gpio/fdt_gpio.o 11 | libsbiutils-objs-$(CONFIG_FDT_GPIO) += gpio/fdt_gpio_drivers.carray.o 12 | 13 | carray-fdt_gpio_drivers-$(CONFIG_FDT_GPIO_DESIGNWARE) += fdt_gpio_designware 14 | libsbiutils-objs-$(CONFIG_FDT_GPIO_DESIGNWARE) += gpio/fdt_gpio_designware.o 15 | 16 | carray-fdt_gpio_drivers-$(CONFIG_FDT_GPIO_SIFIVE) += fdt_gpio_sifive 17 | libsbiutils-objs-$(CONFIG_FDT_GPIO_SIFIVE) += gpio/fdt_gpio_sifive.o 18 | 19 | carray-fdt_gpio_drivers-$(CONFIG_FDT_GPIO_STARFIVE) += fdt_gpio_starfive 20 | libsbiutils-objs-$(CONFIG_FDT_GPIO_STARFIVE) += gpio/fdt_gpio_starfive.o 21 | 22 | libsbiutils-objs-$(CONFIG_GPIO) += gpio/gpio.o 23 | -------------------------------------------------------------------------------- /lib/utils/hsm/Kconfig: -------------------------------------------------------------------------------- 1 | # SPDX-License-Identifier: BSD-2-Clause 2 | 3 | menu "HSM Device Support" 4 | 5 | config FDT_HSM 6 | bool "FDT based HSM drivers" 7 | depends on FDT 8 | default n 9 | 10 | if FDT_HSM 11 | 12 | config FDT_HSM_RPMI 13 | bool "FDT RPMI HSM driver" 14 | depends on FDT_MAILBOX && RPMI_MAILBOX 15 | default n 16 | 17 | endif 18 | 19 | endmenu 20 | -------------------------------------------------------------------------------- /lib/utils/hsm/objects.mk: -------------------------------------------------------------------------------- 1 | # 2 | # SPDX-License-Identifier: BSD-2-Clause 3 | # 4 | # Copyright (c) 2024 Ventana Micro Systems Inc. 5 | # 6 | # Authors: 7 | # Anup Patel 8 | # 9 | 10 | carray-fdt_early_drivers-$(CONFIG_FDT_HSM_RPMI) += fdt_hsm_rpmi 11 | libsbiutils-objs-$(CONFIG_FDT_HSM_RPMI) += hsm/fdt_hsm_rpmi.o 12 | -------------------------------------------------------------------------------- /lib/utils/i2c/Kconfig: -------------------------------------------------------------------------------- 1 | # SPDX-License-Identifier: BSD-2-Clause 2 | 3 | menu "I2C Support" 4 | 5 | config FDT_I2C 6 | bool "FDT based I2C drivers" 7 | depends on FDT 8 | select I2C 9 | default n 10 | 11 | if FDT_I2C 12 | 13 | config FDT_I2C_SIFIVE 14 | bool "SiFive I2C FDT driver" 15 | default n 16 | 17 | config FDT_I2C_DW 18 | bool "Synopsys Designware I2C FDT driver" 19 | select I2C_DW 20 | default n 21 | endif 22 | 23 | config I2C_DW 24 | bool "Synopsys Designware I2C support" 25 | default n 26 | 27 | config I2C 28 | bool "I2C support" 29 | default n 30 | 31 | endmenu 32 | -------------------------------------------------------------------------------- /lib/utils/i2c/fdt_i2c.c: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2021 YADRO 5 | * 6 | * Authors: 7 | * Nikita Shubin 8 | * 9 | * derivate: lib/utils/gpio/fdt_gpio.c 10 | * Authors: 11 | * Anup Patel 12 | */ 13 | 14 | #include 15 | #include 16 | 17 | /* List of FDT i2c adapter drivers generated at compile time */ 18 | extern const struct fdt_driver *const fdt_i2c_adapter_drivers[]; 19 | 20 | static int fdt_i2c_adapter_find(const void *fdt, int nodeoff, 21 | struct i2c_adapter **out_adapter) 22 | { 23 | int rc; 24 | struct i2c_adapter *adapter = i2c_adapter_find(nodeoff); 25 | 26 | if (!adapter) { 27 | /* I2C adapter not found so initialize matching driver */ 28 | rc = fdt_driver_init_by_offset(fdt, nodeoff, 29 | fdt_i2c_adapter_drivers); 30 | if (rc) 31 | return rc; 32 | 33 | /* Try to find I2C adapter again */ 34 | adapter = i2c_adapter_find(nodeoff); 35 | if (!adapter) 36 | return SBI_ENOSYS; 37 | } 38 | 39 | if (out_adapter) 40 | *out_adapter = adapter; 41 | 42 | return 0; 43 | } 44 | 45 | int fdt_i2c_adapter_get(const void *fdt, int nodeoff, 46 | struct i2c_adapter **out_adapter) 47 | { 48 | int rc; 49 | struct i2c_adapter *adapter; 50 | 51 | if (!fdt || (nodeoff < 0) || !out_adapter) 52 | return SBI_EINVAL; 53 | 54 | rc = fdt_i2c_adapter_find(fdt, nodeoff, &adapter); 55 | if (rc) 56 | return rc; 57 | 58 | *out_adapter = adapter; 59 | 60 | return 0; 61 | } 62 | -------------------------------------------------------------------------------- /lib/utils/i2c/fdt_i2c_adapter_drivers.carray: -------------------------------------------------------------------------------- 1 | HEADER: sbi_utils/i2c/fdt_i2c.h 2 | TYPE: const struct fdt_driver 3 | NAME: fdt_i2c_adapter_drivers 4 | -------------------------------------------------------------------------------- /lib/utils/i2c/fdt_i2c_dw.c: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2022 starfivetech.com 5 | * 6 | * Authors: 7 | * Minda Chen 8 | */ 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | 18 | static int fdt_dw_i2c_init(const void *fdt, int nodeoff, 19 | const struct fdt_match *match) 20 | { 21 | int rc; 22 | struct dw_i2c_adapter *adapter; 23 | u64 addr; 24 | 25 | adapter = sbi_zalloc(sizeof(*adapter)); 26 | if (!adapter) 27 | return SBI_ENOMEM; 28 | 29 | rc = fdt_get_node_addr_size(fdt, nodeoff, 0, &addr, NULL); 30 | if (rc) { 31 | sbi_free(adapter); 32 | return rc; 33 | } 34 | 35 | adapter->addr = addr; 36 | 37 | rc = dw_i2c_init(&adapter->adapter, nodeoff); 38 | if (rc) { 39 | sbi_free(adapter); 40 | return rc; 41 | } 42 | 43 | return 0; 44 | } 45 | 46 | static const struct fdt_match fdt_dw_i2c_match[] = { 47 | { .compatible = "snps,designware-i2c" }, 48 | { .compatible = "starfive,jh7110-i2c" }, 49 | { }, 50 | }; 51 | 52 | const struct fdt_driver fdt_i2c_adapter_dw = { 53 | .match_table = fdt_dw_i2c_match, 54 | .init = fdt_dw_i2c_init, 55 | }; 56 | -------------------------------------------------------------------------------- /lib/utils/i2c/i2c.c: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2021 YADRO 5 | * 6 | * Authors: 7 | * Nikita Shubin 8 | * 9 | * derivate: lib/utils/gpio/gpio.c 10 | * Authors: 11 | * Anup Patel 12 | */ 13 | 14 | #include 15 | #include 16 | 17 | static SBI_LIST_HEAD(i2c_adapters_list); 18 | 19 | struct i2c_adapter *i2c_adapter_find(int id) 20 | { 21 | struct sbi_dlist *pos; 22 | 23 | sbi_list_for_each(pos, &(i2c_adapters_list)) { 24 | struct i2c_adapter *adap = to_i2c_adapter(pos); 25 | 26 | if (adap->id == id) 27 | return adap; 28 | } 29 | 30 | return NULL; 31 | } 32 | 33 | int i2c_adapter_add(struct i2c_adapter *ia) 34 | { 35 | if (!ia) 36 | return SBI_EINVAL; 37 | 38 | if (i2c_adapter_find(ia->id)) 39 | return SBI_EALREADY; 40 | 41 | sbi_list_add(&(ia->node), &(i2c_adapters_list)); 42 | 43 | return 0; 44 | } 45 | 46 | void i2c_adapter_remove(struct i2c_adapter *ia) 47 | { 48 | if (!ia) 49 | return; 50 | 51 | sbi_list_del(&(ia->node)); 52 | } 53 | 54 | int i2c_adapter_write(struct i2c_adapter *ia, uint8_t addr, uint8_t reg, 55 | uint8_t *buffer, int len) 56 | { 57 | if (!ia) 58 | return SBI_EINVAL; 59 | if (!ia->write) 60 | return SBI_ENOSYS; 61 | 62 | return ia->write(ia, addr, reg, buffer, len); 63 | } 64 | 65 | 66 | int i2c_adapter_read(struct i2c_adapter *ia, uint8_t addr, uint8_t reg, 67 | uint8_t *buffer, int len) 68 | { 69 | if (!ia) 70 | return SBI_EINVAL; 71 | if (!ia->read) 72 | return SBI_ENOSYS; 73 | 74 | return ia->read(ia, addr, reg, buffer, len); 75 | } 76 | -------------------------------------------------------------------------------- /lib/utils/i2c/objects.mk: -------------------------------------------------------------------------------- 1 | # 2 | # SPDX-License-Identifier: BSD-2-Clause 3 | # 4 | # Copyright (c) 2021 YADRO 5 | # 6 | # Authors: 7 | # Nikita Shubin 8 | # 9 | 10 | libsbiutils-objs-$(CONFIG_I2C) += i2c/i2c.o 11 | 12 | libsbiutils-objs-$(CONFIG_FDT_I2C) += i2c/fdt_i2c.o 13 | libsbiutils-objs-$(CONFIG_FDT_I2C) += i2c/fdt_i2c_adapter_drivers.carray.o 14 | 15 | carray-fdt_i2c_adapter_drivers-$(CONFIG_FDT_I2C_SIFIVE) += fdt_i2c_adapter_sifive 16 | libsbiutils-objs-$(CONFIG_FDT_I2C_SIFIVE) += i2c/fdt_i2c_sifive.o 17 | 18 | carray-fdt_i2c_adapter_drivers-$(CONFIG_FDT_I2C_DW) += fdt_i2c_adapter_dw 19 | libsbiutils-objs-$(CONFIG_FDT_I2C_DW) += i2c/fdt_i2c_dw.o 20 | 21 | libsbiutils-objs-$(CONFIG_I2C_DW) += i2c/dw_i2c.o 22 | -------------------------------------------------------------------------------- /lib/utils/ipi/Kconfig: -------------------------------------------------------------------------------- 1 | # SPDX-License-Identifier: BSD-2-Clause 2 | 3 | menu "IPI Device Support" 4 | 5 | config FDT_IPI 6 | bool "FDT based ipi drivers" 7 | depends on FDT 8 | default n 9 | 10 | if FDT_IPI 11 | 12 | config FDT_IPI_MSWI 13 | bool "ACLINT MSWI FDT driver" 14 | select IPI_MSWI 15 | default n 16 | 17 | config FDT_IPI_PLICSW 18 | bool "Andes PLICSW FDT driver" 19 | select IPI_PLICSW 20 | default n 21 | 22 | endif 23 | 24 | config IPI_MSWI 25 | bool "ACLINT MSWI support" 26 | default n 27 | 28 | config IPI_PLICSW 29 | bool "Andes PLICSW support" 30 | default n 31 | 32 | endmenu 33 | -------------------------------------------------------------------------------- /lib/utils/ipi/fdt_ipi.c: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2020 Western Digital Corporation or its affiliates. 5 | * 6 | * Authors: 7 | * Anup Patel 8 | */ 9 | 10 | #include 11 | 12 | /* List of FDT ipi drivers generated at compile time */ 13 | extern const struct fdt_driver *const fdt_ipi_drivers[]; 14 | 15 | int fdt_ipi_init(void) 16 | { 17 | /* 18 | * On some single-hart system there is no need for IPIs, 19 | * so do not return a failure if no device is found. 20 | */ 21 | return fdt_driver_init_all(fdt_get_address(), fdt_ipi_drivers); 22 | } 23 | -------------------------------------------------------------------------------- /lib/utils/ipi/fdt_ipi_drivers.carray: -------------------------------------------------------------------------------- 1 | HEADER: sbi_utils/ipi/fdt_ipi.h 2 | TYPE: const struct fdt_driver 3 | NAME: fdt_ipi_drivers 4 | -------------------------------------------------------------------------------- /lib/utils/ipi/fdt_ipi_mswi.c: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2021 Western Digital Corporation or its affiliates. 5 | * 6 | * Authors: 7 | * Anup Patel 8 | */ 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | static int ipi_mswi_cold_init(const void *fdt, int nodeoff, 17 | const struct fdt_match *match) 18 | { 19 | int rc; 20 | unsigned long offset; 21 | struct aclint_mswi_data *ms; 22 | 23 | ms = sbi_zalloc(sizeof(*ms)); 24 | if (!ms) 25 | return SBI_ENOMEM; 26 | 27 | rc = fdt_parse_aclint_node(fdt, nodeoff, false, false, 28 | &ms->addr, &ms->size, NULL, NULL, 29 | &ms->first_hartid, &ms->hart_count); 30 | if (rc) { 31 | sbi_free(ms); 32 | return rc; 33 | } 34 | 35 | if (match->data) { 36 | /* Adjust MSWI address and size for CLINT device */ 37 | offset = *((unsigned long *)match->data); 38 | ms->addr += offset; 39 | if ((ms->size - offset) < ACLINT_MSWI_SIZE) 40 | return SBI_EINVAL; 41 | ms->size = ACLINT_MSWI_SIZE; 42 | } 43 | 44 | rc = aclint_mswi_cold_init(ms); 45 | if (rc) { 46 | sbi_free(ms); 47 | return rc; 48 | } 49 | 50 | return 0; 51 | } 52 | 53 | static const unsigned long clint_offset = CLINT_MSWI_OFFSET; 54 | 55 | static const struct fdt_match ipi_mswi_match[] = { 56 | { .compatible = "riscv,clint0", .data = &clint_offset }, 57 | { .compatible = "sifive,clint0", .data = &clint_offset }, 58 | { .compatible = "thead,c900-clint", .data = &clint_offset }, 59 | { .compatible = "thead,c900-aclint-mswi" }, 60 | { .compatible = "riscv,aclint-mswi" }, 61 | { }, 62 | }; 63 | 64 | const struct fdt_driver fdt_ipi_mswi = { 65 | .match_table = ipi_mswi_match, 66 | .init = ipi_mswi_cold_init, 67 | }; 68 | -------------------------------------------------------------------------------- /lib/utils/ipi/fdt_ipi_plicsw.c: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2022 Andes Technology Corporation 5 | * 6 | * Authors: 7 | * Zong Li 8 | * Nylon Chen 9 | * Leo Yu-Chi Liang 10 | * Yu Chien Peter Lin 11 | */ 12 | 13 | #include 14 | #include 15 | #include 16 | #include 17 | 18 | extern struct plicsw_data plicsw; 19 | 20 | int fdt_plicsw_cold_ipi_init(const void *fdt, int nodeoff, 21 | const struct fdt_match *match) 22 | { 23 | int rc; 24 | 25 | rc = fdt_parse_plicsw_node(fdt, nodeoff, &plicsw.addr, &plicsw.size, 26 | &plicsw.hart_count); 27 | if (rc) 28 | return rc; 29 | 30 | rc = plicsw_cold_ipi_init(&plicsw); 31 | if (rc) 32 | return rc; 33 | 34 | return 0; 35 | } 36 | 37 | static const struct fdt_match ipi_plicsw_match[] = { 38 | { .compatible = "andestech,plicsw" }, 39 | {}, 40 | }; 41 | 42 | const struct fdt_driver fdt_ipi_plicsw = { 43 | .match_table = ipi_plicsw_match, 44 | .init = fdt_plicsw_cold_ipi_init, 45 | }; 46 | -------------------------------------------------------------------------------- /lib/utils/ipi/objects.mk: -------------------------------------------------------------------------------- 1 | # 2 | # SPDX-License-Identifier: BSD-2-Clause 3 | # 4 | # Copyright (c) 2020 Western Digital Corporation or its affiliates. 5 | # 6 | # Authors: 7 | # Anup Patel 8 | # 9 | 10 | libsbiutils-objs-$(CONFIG_IPI_MSWI) += ipi/aclint_mswi.o 11 | libsbiutils-objs-$(CONFIG_IPI_PLICSW) += ipi/andes_plicsw.o 12 | 13 | libsbiutils-objs-$(CONFIG_FDT_IPI) += ipi/fdt_ipi.o 14 | libsbiutils-objs-$(CONFIG_FDT_IPI) += ipi/fdt_ipi_drivers.carray.o 15 | 16 | carray-fdt_ipi_drivers-$(CONFIG_FDT_IPI_MSWI) += fdt_ipi_mswi 17 | libsbiutils-objs-$(CONFIG_FDT_IPI_MSWI) += ipi/fdt_ipi_mswi.o 18 | 19 | carray-fdt_ipi_drivers-$(CONFIG_FDT_IPI_PLICSW) += fdt_ipi_plicsw 20 | libsbiutils-objs-$(CONFIG_FDT_IPI_PLICSW) += ipi/fdt_ipi_plicsw.o 21 | -------------------------------------------------------------------------------- /lib/utils/irqchip/Kconfig: -------------------------------------------------------------------------------- 1 | # SPDX-License-Identifier: BSD-2-Clause 2 | 3 | menu "Interrupt Controller Support" 4 | 5 | config FDT_IRQCHIP 6 | bool "FDT based interrupt controller drivers" 7 | depends on FDT 8 | default n 9 | 10 | if FDT_IRQCHIP 11 | 12 | config FDT_IRQCHIP_APLIC 13 | bool "Advanced Platform Level Interrupt Controller (APLIC) FDT driver" 14 | select IRQCHIP_APLIC 15 | default n 16 | 17 | config FDT_IRQCHIP_IMSIC 18 | bool "Incoming Message Signalled Interrupt Controller (IMSIC) FDT driver" 19 | select IRQCHIP_IMSIC 20 | default n 21 | 22 | config FDT_IRQCHIP_PLIC 23 | bool "Platform Level Interrupt Controller (PLIC) FDT driver" 24 | select IRQCHIP_PLIC 25 | default n 26 | 27 | endif 28 | 29 | config IRQCHIP_APLIC 30 | bool "Advanced Platform Level Interrupt Controller (APLIC) support" 31 | default n 32 | 33 | config IRQCHIP_IMSIC 34 | bool "Incoming Message Signalled Interrupt Controller (IMSIC) support" 35 | default n 36 | 37 | config IRQCHIP_PLIC 38 | bool "Platform Level Interrupt Controller (PLIC) support" 39 | default n 40 | 41 | endmenu 42 | -------------------------------------------------------------------------------- /lib/utils/irqchip/fdt_irqchip.c: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2020 Western Digital Corporation or its affiliates. 5 | * 6 | * Authors: 7 | * Anup Patel 8 | */ 9 | 10 | #include 11 | 12 | /* List of FDT irqchip drivers generated at compile time */ 13 | extern const struct fdt_driver *const fdt_irqchip_drivers[]; 14 | 15 | int fdt_irqchip_init(void) 16 | { 17 | return fdt_driver_init_all(fdt_get_address(), fdt_irqchip_drivers); 18 | } 19 | -------------------------------------------------------------------------------- /lib/utils/irqchip/fdt_irqchip_aplic.c: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2021 Western Digital Corporation or its affiliates. 5 | * Copyright (c) 2022 Ventana Micro Systems Inc. 6 | * 7 | * Authors: 8 | * Anup Patel 9 | */ 10 | 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | 19 | static int irqchip_aplic_cold_init(const void *fdt, int nodeoff, 20 | const struct fdt_match *match) 21 | { 22 | int rc; 23 | struct aplic_data *pd; 24 | 25 | pd = sbi_zalloc(sizeof(*pd)); 26 | if (!pd) 27 | return SBI_ENOMEM; 28 | 29 | rc = fdt_parse_aplic_node(fdt, nodeoff, pd); 30 | if (rc) 31 | goto fail_free_data; 32 | 33 | rc = aplic_cold_irqchip_init(pd); 34 | if (rc) 35 | goto fail_free_data; 36 | 37 | return 0; 38 | 39 | fail_free_data: 40 | sbi_free(pd); 41 | return rc; 42 | } 43 | 44 | static const struct fdt_match irqchip_aplic_match[] = { 45 | { .compatible = "riscv,aplic" }, 46 | { }, 47 | }; 48 | 49 | const struct fdt_driver fdt_irqchip_aplic = { 50 | .match_table = irqchip_aplic_match, 51 | .init = irqchip_aplic_cold_init, 52 | }; 53 | -------------------------------------------------------------------------------- /lib/utils/irqchip/fdt_irqchip_drivers.carray: -------------------------------------------------------------------------------- 1 | HEADER: sbi_utils/irqchip/fdt_irqchip.h 2 | TYPE: const struct fdt_driver 3 | NAME: fdt_irqchip_drivers 4 | -------------------------------------------------------------------------------- /lib/utils/irqchip/objects.mk: -------------------------------------------------------------------------------- 1 | # 2 | # SPDX-License-Identifier: BSD-2-Clause 3 | # 4 | # Copyright (c) 2019 Western Digital Corporation or its affiliates. 5 | # 6 | # Authors: 7 | # Anup Patel 8 | # 9 | 10 | libsbiutils-objs-$(CONFIG_FDT_IRQCHIP) += irqchip/fdt_irqchip.o 11 | libsbiutils-objs-$(CONFIG_FDT_IRQCHIP) += irqchip/fdt_irqchip_drivers.carray.o 12 | 13 | carray-fdt_irqchip_drivers-$(CONFIG_FDT_IRQCHIP_APLIC) += fdt_irqchip_aplic 14 | libsbiutils-objs-$(CONFIG_FDT_IRQCHIP_APLIC) += irqchip/fdt_irqchip_aplic.o 15 | 16 | carray-fdt_irqchip_drivers-$(CONFIG_FDT_IRQCHIP_IMSIC) += fdt_irqchip_imsic 17 | libsbiutils-objs-$(CONFIG_FDT_IRQCHIP_IMSIC) += irqchip/fdt_irqchip_imsic.o 18 | 19 | carray-fdt_irqchip_drivers-$(CONFIG_FDT_IRQCHIP_PLIC) += fdt_irqchip_plic 20 | libsbiutils-objs-$(CONFIG_FDT_IRQCHIP_PLIC) += irqchip/fdt_irqchip_plic.o 21 | 22 | libsbiutils-objs-$(CONFIG_IRQCHIP_APLIC) += irqchip/aplic.o 23 | libsbiutils-objs-$(CONFIG_IRQCHIP_IMSIC) += irqchip/imsic.o 24 | libsbiutils-objs-$(CONFIG_IRQCHIP_PLIC) += irqchip/plic.o 25 | -------------------------------------------------------------------------------- /lib/utils/libfdt/.clang-format: -------------------------------------------------------------------------------- 1 | DisableFormat: true 2 | -------------------------------------------------------------------------------- /lib/utils/libfdt/Kconfig: -------------------------------------------------------------------------------- 1 | # SPDX-License-Identifier: BSD-2-Clause 2 | 3 | config LIBFDT 4 | bool 5 | default n 6 | -------------------------------------------------------------------------------- /lib/utils/libfdt/Makefile.libfdt: -------------------------------------------------------------------------------- 1 | # SPDX-License-Identifier: GPL-2.0-or-later OR BSD-2-Clause 2 | # Makefile.libfdt 3 | # 4 | # This is not a complete Makefile of itself. Instead, it is designed to 5 | # be easily embeddable into other systems of Makefiles. 6 | # 7 | LIBFDT_soname = libfdt.$(SHAREDLIB_EXT).1 8 | LIBFDT_INCLUDES = fdt.h libfdt.h libfdt_env.h 9 | LIBFDT_VERSION = version.lds 10 | LIBFDT_SRCS = fdt.c fdt_ro.c fdt_wip.c fdt_sw.c fdt_rw.c fdt_strerror.c fdt_empty_tree.c \ 11 | fdt_addresses.c fdt_overlay.c fdt_check.c 12 | LIBFDT_OBJS = $(LIBFDT_SRCS:%.c=%.o) 13 | LIBFDT_LIB = libfdt-$(DTC_VERSION).$(SHAREDLIB_EXT) 14 | 15 | libfdt_clean: 16 | @$(VECHO) CLEAN "(libfdt)" 17 | rm -f $(STD_CLEANFILES:%=$(LIBFDT_dir)/%) 18 | rm -f $(LIBFDT_dir)/$(LIBFDT_soname) 19 | -------------------------------------------------------------------------------- /lib/utils/libfdt/TODO: -------------------------------------------------------------------------------- 1 | - Tree traversal functions 2 | - Graft function 3 | - Complete libfdt.h documenting comments 4 | -------------------------------------------------------------------------------- /lib/utils/libfdt/fdt.h: -------------------------------------------------------------------------------- 1 | /* SPDX-License-Identifier: GPL-2.0-or-later OR BSD-2-Clause */ 2 | #ifndef FDT_H 3 | #define FDT_H 4 | /* 5 | * libfdt - Flat Device Tree manipulation 6 | * Copyright (C) 2006 David Gibson, IBM Corporation. 7 | * Copyright 2012 Kim Phillips, Freescale Semiconductor. 8 | */ 9 | 10 | #ifndef __ASSEMBLY__ 11 | 12 | struct fdt_header { 13 | fdt32_t magic; /* magic word FDT_MAGIC */ 14 | fdt32_t totalsize; /* total size of DT block */ 15 | fdt32_t off_dt_struct; /* offset to structure */ 16 | fdt32_t off_dt_strings; /* offset to strings */ 17 | fdt32_t off_mem_rsvmap; /* offset to memory reserve map */ 18 | fdt32_t version; /* format version */ 19 | fdt32_t last_comp_version; /* last compatible version */ 20 | 21 | /* version 2 fields below */ 22 | fdt32_t boot_cpuid_phys; /* Which physical CPU id we're 23 | booting on */ 24 | /* version 3 fields below */ 25 | fdt32_t size_dt_strings; /* size of the strings block */ 26 | 27 | /* version 17 fields below */ 28 | fdt32_t size_dt_struct; /* size of the structure block */ 29 | }; 30 | 31 | struct fdt_reserve_entry { 32 | fdt64_t address; 33 | fdt64_t size; 34 | }; 35 | 36 | struct fdt_node_header { 37 | fdt32_t tag; 38 | char name[0]; 39 | }; 40 | 41 | struct fdt_property { 42 | fdt32_t tag; 43 | fdt32_t len; 44 | fdt32_t nameoff; 45 | char data[0]; 46 | }; 47 | 48 | #endif /* !__ASSEMBLY */ 49 | 50 | #define FDT_MAGIC 0xd00dfeed /* 4: version, 4: total size */ 51 | #define FDT_TAGSIZE sizeof(fdt32_t) 52 | 53 | #define FDT_BEGIN_NODE 0x1 /* Start node: full name */ 54 | #define FDT_END_NODE 0x2 /* End node */ 55 | #define FDT_PROP 0x3 /* Property: name off, 56 | size, content */ 57 | #define FDT_NOP 0x4 /* nop */ 58 | #define FDT_END 0x9 59 | 60 | #define FDT_V1_SIZE (7*sizeof(fdt32_t)) 61 | #define FDT_V2_SIZE (FDT_V1_SIZE + sizeof(fdt32_t)) 62 | #define FDT_V3_SIZE (FDT_V2_SIZE + sizeof(fdt32_t)) 63 | #define FDT_V16_SIZE FDT_V3_SIZE 64 | #define FDT_V17_SIZE (FDT_V16_SIZE + sizeof(fdt32_t)) 65 | 66 | #endif /* FDT_H */ 67 | -------------------------------------------------------------------------------- /lib/utils/libfdt/fdt_empty_tree.c: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: GPL-2.0-or-later OR BSD-2-Clause 2 | /* 3 | * libfdt - Flat Device Tree manipulation 4 | * Copyright (C) 2012 David Gibson, IBM Corporation. 5 | */ 6 | #include "libfdt_env.h" 7 | 8 | #include 9 | #include 10 | 11 | #include "libfdt_internal.h" 12 | 13 | int fdt_create_empty_tree(void *buf, int bufsize) 14 | { 15 | int err; 16 | 17 | err = fdt_create(buf, bufsize); 18 | if (err) 19 | return err; 20 | 21 | err = fdt_finish_reservemap(buf); 22 | if (err) 23 | return err; 24 | 25 | err = fdt_begin_node(buf, ""); 26 | if (err) 27 | return err; 28 | 29 | err = fdt_end_node(buf); 30 | if (err) 31 | return err; 32 | 33 | err = fdt_finish(buf); 34 | if (err) 35 | return err; 36 | 37 | return fdt_open_into(buf, buf, bufsize); 38 | } 39 | -------------------------------------------------------------------------------- /lib/utils/libfdt/fdt_strerror.c: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: GPL-2.0-or-later OR BSD-2-Clause 2 | /* 3 | * libfdt - Flat Device Tree manipulation 4 | * Copyright (C) 2006 David Gibson, IBM Corporation. 5 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 6 | */ 7 | #include "libfdt_env.h" 8 | 9 | #include 10 | #include 11 | 12 | #include "libfdt_internal.h" 13 | 14 | struct fdt_errtabent { 15 | const char *str; 16 | }; 17 | 18 | #define FDT_ERRTABENT(val) \ 19 | [(val)] = { .str = #val, } 20 | 21 | static struct fdt_errtabent fdt_errtable[] = { 22 | FDT_ERRTABENT(FDT_ERR_NOTFOUND), 23 | FDT_ERRTABENT(FDT_ERR_EXISTS), 24 | FDT_ERRTABENT(FDT_ERR_NOSPACE), 25 | 26 | FDT_ERRTABENT(FDT_ERR_BADOFFSET), 27 | FDT_ERRTABENT(FDT_ERR_BADPATH), 28 | FDT_ERRTABENT(FDT_ERR_BADPHANDLE), 29 | FDT_ERRTABENT(FDT_ERR_BADSTATE), 30 | 31 | FDT_ERRTABENT(FDT_ERR_TRUNCATED), 32 | FDT_ERRTABENT(FDT_ERR_BADMAGIC), 33 | FDT_ERRTABENT(FDT_ERR_BADVERSION), 34 | FDT_ERRTABENT(FDT_ERR_BADSTRUCTURE), 35 | FDT_ERRTABENT(FDT_ERR_BADLAYOUT), 36 | FDT_ERRTABENT(FDT_ERR_INTERNAL), 37 | FDT_ERRTABENT(FDT_ERR_BADNCELLS), 38 | FDT_ERRTABENT(FDT_ERR_BADVALUE), 39 | FDT_ERRTABENT(FDT_ERR_BADOVERLAY), 40 | FDT_ERRTABENT(FDT_ERR_NOPHANDLES), 41 | FDT_ERRTABENT(FDT_ERR_BADFLAGS), 42 | }; 43 | #define FDT_ERRTABSIZE ((int)(sizeof(fdt_errtable) / sizeof(fdt_errtable[0]))) 44 | 45 | const char *fdt_strerror(int errval) 46 | { 47 | if (errval > 0) 48 | return ""; 49 | else if (errval == 0) 50 | return ""; 51 | else if (-errval < FDT_ERRTABSIZE) { 52 | const char *s = fdt_errtable[-errval].str; 53 | 54 | if (s) 55 | return s; 56 | } 57 | 58 | return ""; 59 | } 60 | -------------------------------------------------------------------------------- /lib/utils/libfdt/objects.mk: -------------------------------------------------------------------------------- 1 | # 2 | # SPDX-License-Identifier: BSD-2-Clause 3 | # 4 | # Copyright (c) 2019 Western Digital Corporation or its affiliates. 5 | # 6 | # Authors: 7 | # Atish Patra 8 | # 9 | 10 | libfdt_files = fdt.o fdt_addresses.o fdt_check.o fdt_empty_tree.o fdt_ro.o fdt_rw.o \ 11 | fdt_strerror.o fdt_sw.o fdt_wip.o 12 | $(foreach file, $(libfdt_files), \ 13 | $(eval CFLAGS_$(file) = -I$(src)/../../utils/libfdt)) 14 | 15 | libsbiutils-objs-$(CONFIG_LIBFDT) += $(addprefix libfdt/,$(libfdt_files)) 16 | libsbiutils-genflags-y += -I$(libsbiutils_dir)/libfdt/ 17 | -------------------------------------------------------------------------------- /lib/utils/libquad/include/limits.h: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2021 Jessica Clarke 5 | */ 6 | 7 | #ifndef __LIMITS_H__ 8 | #define __LIMITS_H__ 9 | 10 | #define CHAR_BIT 8 11 | 12 | #endif 13 | -------------------------------------------------------------------------------- /lib/utils/libquad/include/sys/cdefs.h: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2021 Jessica Clarke 5 | */ 6 | 7 | #ifndef __SYS_CDEFS_H__ 8 | #define __SYS_CDEFS_H__ 9 | 10 | #define __FBSDID(s) struct __hack 11 | 12 | #endif 13 | -------------------------------------------------------------------------------- /lib/utils/libquad/include/sys/types.h: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2021 Jessica Clarke 5 | */ 6 | 7 | #ifndef __SYS_TYPES_H__ 8 | #define __SYS_TYPES_H__ 9 | 10 | #include 11 | 12 | typedef unsigned long u_long; 13 | 14 | typedef int64_t quad_t; 15 | typedef uint64_t u_quad_t; 16 | 17 | #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ 18 | #define _QUAD_LOWWORD 1 19 | #define _QUAD_HIGHWORD 0 20 | #else 21 | #define _QUAD_LOWWORD 0 22 | #define _QUAD_HIGHWORD 1 23 | #endif 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /lib/utils/libquad/objects.mk: -------------------------------------------------------------------------------- 1 | # 2 | # SPDX-License-Identifier: BSD-2-Clause 3 | # 4 | # Copyright (c) 2021 Jessica Clarke 5 | # 6 | 7 | ifeq ($(PLATFORM_RISCV_XLEN),32) 8 | libsbiutils-objs-y += libquad/divdi3.o 9 | libsbiutils-objs-y += libquad/moddi3.o 10 | libsbiutils-objs-y += libquad/qdivrem.o 11 | libsbiutils-objs-y += libquad/udivdi3.o 12 | libsbiutils-objs-y += libquad/umoddi3.o 13 | libsbiutils-genflags-y += -I$(libsbiutils_dir)/libquad/include 14 | endif 15 | -------------------------------------------------------------------------------- /lib/utils/mailbox/Kconfig: -------------------------------------------------------------------------------- 1 | # SPDX-License-Identifier: BSD-2-Clause 2 | 3 | menu "Mailbox Support" 4 | 5 | config FDT_MAILBOX 6 | bool "FDT based mailbox drivers" 7 | depends on FDT 8 | select MAILBOX 9 | default n 10 | 11 | config RPMI_MAILBOX 12 | bool "RPMI based mailbox drivers" 13 | select MAILBOX 14 | default n 15 | 16 | config MAILBOX 17 | bool "Mailbox support" 18 | default n 19 | 20 | if FDT_MAILBOX 21 | 22 | config FDT_MAILBOX_RPMI_SHMEM 23 | bool "RPMI Shared Memory Mailbox Controller" 24 | depends on RPMI_MAILBOX 25 | default n 26 | 27 | endif 28 | 29 | endmenu 30 | -------------------------------------------------------------------------------- /lib/utils/mailbox/fdt_mailbox_drivers.carray: -------------------------------------------------------------------------------- 1 | HEADER: sbi_utils/mailbox/fdt_mailbox.h 2 | TYPE: const struct fdt_mailbox 3 | NAME: fdt_mailbox_drivers 4 | MEMBER-NAME: driver 5 | MEMBER-TYPE: const struct fdt_driver 6 | -------------------------------------------------------------------------------- /lib/utils/mailbox/objects.mk: -------------------------------------------------------------------------------- 1 | # 2 | # SPDX-License-Identifier: BSD-2-Clause 3 | # 4 | # Copyright (c) 2024 Ventana Micro Systems Inc. 5 | # 6 | # Authors: 7 | # Anup Patel 8 | # 9 | 10 | libsbiutils-objs-$(CONFIG_FDT_MAILBOX) += mailbox/fdt_mailbox.o 11 | libsbiutils-objs-$(CONFIG_FDT_MAILBOX) += mailbox/fdt_mailbox_drivers.carray.o 12 | 13 | libsbiutils-objs-$(CONFIG_MAILBOX) += mailbox/mailbox.o 14 | 15 | libsbiutils-objs-$(CONFIG_RPMI_MAILBOX) += mailbox/rpmi_mailbox.o 16 | 17 | carray-fdt_mailbox_drivers-$(CONFIG_FDT_MAILBOX_RPMI_SHMEM) += fdt_mailbox_rpmi_shmem 18 | libsbiutils-objs-$(CONFIG_FDT_MAILBOX_RPMI_SHMEM) += mailbox/fdt_mailbox_rpmi_shmem.o 19 | -------------------------------------------------------------------------------- /lib/utils/mpxy/Kconfig: -------------------------------------------------------------------------------- 1 | # SPDX-License-Identifier: BSD-2-Clause 2 | 3 | menu "MPXY Device Support" 4 | 5 | config FDT_MPXY 6 | bool "FDT based MPXY drivers" 7 | depends on FDT 8 | default n 9 | 10 | config FDT_MPXY_RPMI_MBOX 11 | bool "MPXY drivers as RPMI mailbox client" 12 | depends on FDT_MAILBOX && FDT_MPXY 13 | default n 14 | 15 | if FDT_MPXY_RPMI_MBOX 16 | 17 | config FDT_MPXY_RPMI_CLOCK 18 | bool "MPXY driver for RPMI clock service group" 19 | default n 20 | 21 | config FDT_MPXY_RPMI_SYSMSI 22 | bool "MPXY driver for RPMI system MSI service group" 23 | default n 24 | 25 | endif 26 | 27 | endmenu 28 | -------------------------------------------------------------------------------- /lib/utils/mpxy/fdt_mpxy.c: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2024 Ventana Micro Systems Inc. 5 | * 6 | * Authors: 7 | * Anup Patel 8 | */ 9 | 10 | #include 11 | 12 | /* List of FDT MPXY drivers generated at compile time */ 13 | extern const struct fdt_driver *const fdt_mpxy_drivers[]; 14 | 15 | int fdt_mpxy_init(const void *fdt) 16 | { 17 | return fdt_driver_init_all(fdt, fdt_mpxy_drivers); 18 | } 19 | -------------------------------------------------------------------------------- /lib/utils/mpxy/fdt_mpxy_drivers.carray: -------------------------------------------------------------------------------- 1 | HEADER: sbi_utils/mpxy/fdt_mpxy.h 2 | TYPE: const struct fdt_driver 3 | NAME: fdt_mpxy_drivers 4 | -------------------------------------------------------------------------------- /lib/utils/mpxy/objects.mk: -------------------------------------------------------------------------------- 1 | # 2 | # SPDX-License-Identifier: BSD-2-Clause 3 | # 4 | # Copyright (c) 2024 Ventana Micro Systems Inc. 5 | # 6 | # Authors: 7 | # Anup Patel 8 | # 9 | 10 | libsbiutils-objs-$(CONFIG_FDT_MPXY) += mpxy/fdt_mpxy.o 11 | libsbiutils-objs-$(CONFIG_FDT_MPXY) += mpxy/fdt_mpxy_drivers.carray.o 12 | 13 | libsbiutils-objs-$(CONFIG_FDT_MPXY_RPMI_MBOX) += mpxy/fdt_mpxy_rpmi_mbox.o 14 | 15 | carray-fdt_mpxy_drivers-$(CONFIG_FDT_MPXY_RPMI_CLOCK) += fdt_mpxy_rpmi_clock 16 | libsbiutils-objs-$(CONFIG_FDT_MPXY_RPMI_CLOCK) += mpxy/fdt_mpxy_rpmi_clock.o 17 | 18 | carray-fdt_mpxy_drivers-$(CONFIG_FDT_MPXY_RPMI_SYSMSI) += fdt_mpxy_rpmi_sysmsi 19 | libsbiutils-objs-$(CONFIG_FDT_MPXY_RPMI_SYSMSI) += mpxy/fdt_mpxy_rpmi_sysmsi.o 20 | -------------------------------------------------------------------------------- /lib/utils/regmap/Kconfig: -------------------------------------------------------------------------------- 1 | # SPDX-License-Identifier: BSD-2-Clause 2 | 3 | menu "Regmap Support" 4 | 5 | config FDT_REGMAP 6 | bool "FDT based regmap drivers" 7 | depends on FDT 8 | select REGMAP 9 | default n 10 | 11 | if FDT_REGMAP 12 | 13 | config FDT_REGMAP_SYSCON 14 | bool "Syscon regmap FDT driver" 15 | default n 16 | endif 17 | 18 | config REGMAP 19 | bool "Regmap support" 20 | default n 21 | 22 | endmenu 23 | -------------------------------------------------------------------------------- /lib/utils/regmap/fdt_regmap.c: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2023 Ventana Micro Systems Inc. 5 | * 6 | * Authors: 7 | * Anup Patel 8 | */ 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | /* List of FDT regmap drivers generated at compile time */ 16 | extern const struct fdt_driver *const fdt_regmap_drivers[]; 17 | 18 | static int fdt_regmap_find(const void *fdt, int nodeoff, 19 | struct regmap **out_rmap) 20 | { 21 | int rc; 22 | struct regmap *rmap = regmap_find(nodeoff); 23 | 24 | if (!rmap) { 25 | /* Regmap not found so initialize matching driver */ 26 | rc = fdt_driver_init_by_offset(fdt, nodeoff, 27 | fdt_regmap_drivers); 28 | if (rc) 29 | return rc; 30 | 31 | /* Try to find regmap again */ 32 | rmap = regmap_find(nodeoff); 33 | if (!rmap) 34 | return SBI_ENOSYS; 35 | } 36 | 37 | if (out_rmap) 38 | *out_rmap = rmap; 39 | 40 | return 0; 41 | } 42 | 43 | int fdt_regmap_get_by_phandle(const void *fdt, u32 phandle, 44 | struct regmap **out_rmap) 45 | { 46 | int pnodeoff; 47 | 48 | if (!fdt || !out_rmap) 49 | return SBI_EINVAL; 50 | 51 | pnodeoff = fdt_node_offset_by_phandle(fdt, phandle); 52 | if (pnodeoff < 0) 53 | return pnodeoff; 54 | 55 | return fdt_regmap_find(fdt, pnodeoff, out_rmap); 56 | } 57 | 58 | int fdt_regmap_get(const void *fdt, int nodeoff, struct regmap **out_rmap) 59 | { 60 | int len; 61 | const fdt32_t *val; 62 | 63 | if (!fdt || (nodeoff < 0) || !out_rmap) 64 | return SBI_EINVAL; 65 | 66 | val = fdt_getprop(fdt, nodeoff, "regmap", &len); 67 | if (!val) 68 | return SBI_ENOENT; 69 | 70 | return fdt_regmap_get_by_phandle(fdt, fdt32_to_cpu(*val), out_rmap); 71 | } 72 | -------------------------------------------------------------------------------- /lib/utils/regmap/fdt_regmap_drivers.carray: -------------------------------------------------------------------------------- 1 | HEADER: sbi_utils/regmap/fdt_regmap.h 2 | TYPE: const struct fdt_driver 3 | NAME: fdt_regmap_drivers 4 | -------------------------------------------------------------------------------- /lib/utils/regmap/objects.mk: -------------------------------------------------------------------------------- 1 | # 2 | # SPDX-License-Identifier: BSD-2-Clause 3 | # 4 | # Copyright (c) 2023 Ventana Micro Systems Inc. 5 | # 6 | # Authors: 7 | # Anup Patel 8 | # 9 | 10 | libsbiutils-objs-$(CONFIG_FDT_REGMAP) += regmap/fdt_regmap.o 11 | libsbiutils-objs-$(CONFIG_FDT_REGMAP) += regmap/fdt_regmap_drivers.carray.o 12 | 13 | carray-fdt_regmap_drivers-$(CONFIG_FDT_REGMAP_SYSCON) += fdt_regmap_syscon 14 | libsbiutils-objs-$(CONFIG_FDT_REGMAP_SYSCON) += regmap/fdt_regmap_syscon.o 15 | 16 | libsbiutils-objs-$(CONFIG_REGMAP) += regmap/regmap.o 17 | -------------------------------------------------------------------------------- /lib/utils/reset/Kconfig: -------------------------------------------------------------------------------- 1 | # SPDX-License-Identifier: BSD-2-Clause 2 | 3 | menu "System Reset Support" 4 | 5 | config FDT_RESET 6 | bool "FDT based reset drivers" 7 | depends on FDT 8 | default n 9 | 10 | if FDT_RESET 11 | 12 | config FDT_RESET_ATCWDT200 13 | bool "Andes WDT FDT reset driver" 14 | depends on SYS_ATCSMU 15 | default n 16 | 17 | config FDT_RESET_GPIO 18 | bool "GPIO FDT reset driver" 19 | depends on FDT_GPIO 20 | default n 21 | 22 | config FDT_RESET_HTIF 23 | bool "Host transfer interface (HTIF) FDT reset driver" 24 | select SYS_HTIF 25 | default n 26 | 27 | config FDT_RESET_RPMI 28 | bool "RPMI FDT reset driver" 29 | depends on FDT_MAILBOX && RPMI_MAILBOX 30 | default n 31 | 32 | config FDT_RESET_SG2042_HWMON_MCU 33 | bool "Sophgo SG2042 hwmon MCU FDT reset driver" 34 | default n 35 | 36 | config FDT_RESET_SUNXI_WDT 37 | bool "Sunxi WDT FDT reset driver" 38 | default n 39 | 40 | config FDT_RESET_SYSCON 41 | bool "Syscon FDT reset driver" 42 | depends on FDT_REGMAP 43 | default n 44 | 45 | endif 46 | 47 | endmenu 48 | -------------------------------------------------------------------------------- /lib/utils/reset/fdt_reset_htif.c: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2020 Western Digital Corporation or its affiliates. 5 | * 6 | * Authors: 7 | * Anup Patel 8 | */ 9 | 10 | #include 11 | #include 12 | #include 13 | 14 | static int htif_reset_init(const void *fdt, int nodeoff, 15 | const struct fdt_match *match) 16 | { 17 | bool custom = false; 18 | uint64_t fromhost_addr = 0, tohost_addr = 0; 19 | 20 | if (!fdt_get_node_addr_size(fdt, nodeoff, 0, &fromhost_addr, NULL)) { 21 | custom = true; 22 | tohost_addr = fromhost_addr + sizeof(uint64_t); 23 | } 24 | 25 | fdt_get_node_addr_size(fdt, nodeoff, 1, &tohost_addr, NULL); 26 | 27 | return htif_system_reset_init(custom, fromhost_addr, tohost_addr); 28 | } 29 | 30 | static const struct fdt_match htif_reset_match[] = { 31 | { .compatible = "ucb,htif0" }, 32 | { }, 33 | }; 34 | 35 | const struct fdt_driver fdt_reset_htif = { 36 | .match_table = htif_reset_match, 37 | .init = htif_reset_init 38 | }; 39 | -------------------------------------------------------------------------------- /lib/utils/reset/objects.mk: -------------------------------------------------------------------------------- 1 | # 2 | # SPDX-License-Identifier: BSD-2-Clause 3 | # 4 | # Copyright (c) 2020 Western Digital Corporation or its affiliates. 5 | # 6 | # Authors: 7 | # Anup Patel 8 | # 9 | 10 | carray-fdt_early_drivers-$(CONFIG_FDT_RESET_ATCWDT200) += fdt_reset_atcwdt200 11 | libsbiutils-objs-$(CONFIG_FDT_RESET_ATCWDT200) += reset/fdt_reset_atcwdt200.o 12 | 13 | carray-fdt_early_drivers-$(CONFIG_FDT_RESET_GPIO) += fdt_poweroff_gpio 14 | carray-fdt_early_drivers-$(CONFIG_FDT_RESET_GPIO) += fdt_reset_gpio 15 | libsbiutils-objs-$(CONFIG_FDT_RESET_GPIO) += reset/fdt_reset_gpio.o 16 | 17 | carray-fdt_early_drivers-$(CONFIG_FDT_RESET_HTIF) += fdt_reset_htif 18 | libsbiutils-objs-$(CONFIG_FDT_RESET_HTIF) += reset/fdt_reset_htif.o 19 | 20 | carray-fdt_early_drivers-$(CONFIG_FDT_RESET_SG2042_HWMON_MCU) += fdt_reset_sg2042_mcu 21 | libsbiutils-objs-$(CONFIG_FDT_RESET_SG2042_HWMON_MCU) += reset/fdt_reset_sg2042_hwmon_mcu.o 22 | 23 | carray-fdt_early_drivers-$(CONFIG_FDT_RESET_SUNXI_WDT) += fdt_reset_sunxi_wdt 24 | libsbiutils-objs-$(CONFIG_FDT_RESET_SUNXI_WDT) += reset/fdt_reset_sunxi_wdt.o 25 | 26 | carray-fdt_early_drivers-$(CONFIG_FDT_RESET_SYSCON) += fdt_syscon_poweroff 27 | carray-fdt_early_drivers-$(CONFIG_FDT_RESET_SYSCON) += fdt_syscon_reboot 28 | libsbiutils-objs-$(CONFIG_FDT_RESET_SYSCON) += reset/fdt_reset_syscon.o 29 | 30 | carray-fdt_early_drivers-$(CONFIG_FDT_RESET_RPMI) += fdt_reset_rpmi 31 | libsbiutils-objs-$(CONFIG_FDT_RESET_RPMI) += reset/fdt_reset_rpmi.o 32 | -------------------------------------------------------------------------------- /lib/utils/serial/fdt_serial.c: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2020 Western Digital Corporation or its affiliates. 5 | * 6 | * Authors: 7 | * Anup Patel 8 | */ 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | /* List of FDT serial drivers generated at compile time */ 16 | extern const struct fdt_driver *const fdt_serial_drivers[]; 17 | 18 | int fdt_serial_init(const void *fdt) 19 | { 20 | const void *prop; 21 | int noff = -1, len, coff, rc; 22 | 23 | /* Find offset of node pointed to by stdout-path */ 24 | coff = fdt_path_offset(fdt, "/chosen"); 25 | if (-1 < coff) { 26 | prop = fdt_getprop(fdt, coff, "stdout-path", &len); 27 | if (prop && len) { 28 | const char *sep, *start = prop; 29 | 30 | /* The device path may be followed by ':' */ 31 | sep = strchr(start, ':'); 32 | if (sep) 33 | noff = fdt_path_offset_namelen(fdt, prop, 34 | sep - start); 35 | else 36 | noff = fdt_path_offset(fdt, prop); 37 | } 38 | } 39 | 40 | /* First check DT node pointed by stdout-path */ 41 | if (-1 < noff) { 42 | rc = fdt_driver_init_by_offset(fdt, noff, fdt_serial_drivers); 43 | if (rc != SBI_ENODEV) 44 | return rc; 45 | } 46 | 47 | /* Lastly check all DT nodes */ 48 | return fdt_driver_init_one(fdt, fdt_serial_drivers); 49 | } 50 | -------------------------------------------------------------------------------- /lib/utils/serial/fdt_serial_cadence.c: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2022 StarFive Technology Co., Ltd. 5 | * 6 | * Author: Jun Liang Tan 7 | */ 8 | 9 | #include 10 | #include 11 | #include 12 | 13 | static int serial_cadence_init(const void *fdt, int nodeoff, 14 | const struct fdt_match *match) 15 | { 16 | int rc; 17 | struct platform_uart_data uart = { 0 }; 18 | 19 | rc = fdt_parse_uart_node(fdt, nodeoff, &uart); 20 | if (rc) 21 | return rc; 22 | 23 | return cadence_uart_init(uart.addr, uart.freq, uart.baud); 24 | } 25 | 26 | static const struct fdt_match serial_cadence_match[] = { 27 | { .compatible = "cdns,uart-r1p8", }, 28 | { .compatible = "cdns,uart-r1p12" }, 29 | { .compatible = "starfive,jh8100-uart" }, 30 | { }, 31 | }; 32 | 33 | const struct fdt_driver fdt_serial_cadence = { 34 | .match_table = serial_cadence_match, 35 | .init = serial_cadence_init 36 | }; 37 | -------------------------------------------------------------------------------- /lib/utils/serial/fdt_serial_drivers.carray: -------------------------------------------------------------------------------- 1 | HEADER: sbi_utils/serial/fdt_serial.h 2 | TYPE: const struct fdt_driver 3 | NAME: fdt_serial_drivers 4 | -------------------------------------------------------------------------------- /lib/utils/serial/fdt_serial_gaisler.c: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2021 Cobham Gaisler AB. 5 | * 6 | * Authors: 7 | * Daniel Cederman 8 | */ 9 | 10 | #include 11 | #include 12 | #include 13 | 14 | static int serial_gaisler_init(const void *fdt, int nodeoff, 15 | const struct fdt_match *match) 16 | { 17 | int rc; 18 | struct platform_uart_data uart = { 0 }; 19 | 20 | rc = fdt_parse_gaisler_uart_node(fdt, nodeoff, &uart); 21 | if (rc) 22 | return rc; 23 | 24 | return gaisler_uart_init(uart.addr, uart.freq, uart.baud); 25 | } 26 | 27 | static const struct fdt_match serial_gaisler_match[] = { 28 | { .compatible = "gaisler,apbuart" }, 29 | {}, 30 | }; 31 | 32 | const struct fdt_driver fdt_serial_gaisler = { 33 | .match_table = serial_gaisler_match, 34 | .init = serial_gaisler_init 35 | }; 36 | -------------------------------------------------------------------------------- /lib/utils/serial/fdt_serial_htif.c: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2020 Western Digital Corporation or its affiliates. 5 | * 6 | * Authors: 7 | * Anup Patel 8 | */ 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | static const struct fdt_match serial_htif_match[] = { 17 | { .compatible = "ucb,htif0" }, 18 | { }, 19 | }; 20 | 21 | static int serial_htif_init(const void *fdt, int nodeoff, 22 | const struct fdt_match *match) 23 | { 24 | int rc; 25 | bool custom = false; 26 | uint64_t fromhost_addr = 0, tohost_addr = 0; 27 | 28 | if (!fdt_get_node_addr_size(fdt, nodeoff, 0, &fromhost_addr, NULL)) { 29 | custom = true; 30 | tohost_addr = fromhost_addr + sizeof(uint64_t); 31 | } 32 | 33 | fdt_get_node_addr_size(fdt, nodeoff, 1, &tohost_addr, NULL); 34 | 35 | rc = sbi_domain_root_add_memrange(fromhost_addr, PAGE_SIZE, PAGE_SIZE, 36 | (SBI_DOMAIN_MEMREGION_MMIO | 37 | SBI_DOMAIN_MEMREGION_SHARED_SURW_MRW)); 38 | if (rc) 39 | return rc; 40 | 41 | return htif_serial_init(custom, fromhost_addr, tohost_addr); 42 | } 43 | 44 | const struct fdt_driver fdt_serial_htif = { 45 | .match_table = serial_htif_match, 46 | .init = serial_htif_init 47 | }; 48 | -------------------------------------------------------------------------------- /lib/utils/serial/fdt_serial_litex.c: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2021 Gabriel Somlo 5 | * 6 | * Authors: 7 | * Gabriel Somlo 8 | */ 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | static int serial_litex_init(const void *fdt, int nodeoff, 16 | const struct fdt_match *match) 17 | { 18 | uint64_t reg_addr, reg_size; 19 | int rc; 20 | 21 | if (nodeoff < 0 || !fdt) 22 | return SBI_ENODEV; 23 | 24 | rc = fdt_get_node_addr_size(fdt, nodeoff, 0, ®_addr, ®_size); 25 | if (rc < 0 || !reg_addr || !reg_size) 26 | return SBI_ENODEV; 27 | 28 | return litex_uart_init(reg_addr); 29 | } 30 | 31 | static const struct fdt_match serial_litex_match[] = { 32 | { .compatible = "litex,liteuart" }, 33 | { }, 34 | }; 35 | 36 | const struct fdt_driver fdt_serial_litex = { 37 | .match_table = serial_litex_match, 38 | .init = serial_litex_init 39 | }; 40 | -------------------------------------------------------------------------------- /lib/utils/serial/fdt_serial_renesas_scif.c: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: BSD-2-Clause 2 | /* 3 | * Copyright (C) 2022 Renesas Electronics Corporation 4 | */ 5 | 6 | #include 7 | #include 8 | #include 9 | 10 | static int serial_renesas_scif_init(const void *fdt, int nodeoff, 11 | const struct fdt_match *match) 12 | { 13 | struct platform_uart_data uart = { 0 }; 14 | int ret; 15 | 16 | ret = fdt_parse_renesas_scif_node(fdt, nodeoff, &uart); 17 | if (ret) 18 | return ret; 19 | 20 | return renesas_scif_init(uart.addr, uart.freq, uart.baud); 21 | } 22 | 23 | static const struct fdt_match serial_renesas_scif_match[] = { 24 | { .compatible = "renesas,scif-r9a07g043" }, 25 | { /* sentinel */ } 26 | }; 27 | 28 | const struct fdt_driver fdt_serial_renesas_scif = { 29 | .match_table = serial_renesas_scif_match, 30 | .init = serial_renesas_scif_init 31 | }; 32 | -------------------------------------------------------------------------------- /lib/utils/serial/fdt_serial_shakti.c: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2020 Vijai Kumar K 5 | * 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | static int serial_shakti_init(const void *fdt, int nodeoff, 13 | const struct fdt_match *match) 14 | { 15 | int rc; 16 | struct platform_uart_data uart = { 0 }; 17 | 18 | rc = fdt_parse_shakti_uart_node(fdt, nodeoff, &uart); 19 | if (rc) 20 | return rc; 21 | 22 | return shakti_uart_init(uart.addr, uart.freq, uart.baud); 23 | } 24 | 25 | static const struct fdt_match serial_shakti_match[] = { 26 | { .compatible = "shakti,uart0" }, 27 | { }, 28 | }; 29 | 30 | const struct fdt_driver fdt_serial_shakti = { 31 | .match_table = serial_shakti_match, 32 | .init = serial_shakti_init 33 | }; 34 | -------------------------------------------------------------------------------- /lib/utils/serial/fdt_serial_sifive.c: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2020 Western Digital Corporation or its affiliates. 5 | * 6 | * Authors: 7 | * Anup Patel 8 | */ 9 | 10 | #include 11 | #include 12 | #include 13 | 14 | static int serial_sifive_init(const void *fdt, int nodeoff, 15 | const struct fdt_match *match) 16 | { 17 | int rc; 18 | struct platform_uart_data uart = { 0 }; 19 | 20 | rc = fdt_parse_sifive_uart_node(fdt, nodeoff, &uart); 21 | if (rc) 22 | return rc; 23 | 24 | return sifive_uart_init(uart.addr, uart.freq, uart.baud); 25 | } 26 | 27 | static const struct fdt_match serial_sifive_match[] = { 28 | { .compatible = "sifive,fu540-c000-uart" }, 29 | { .compatible = "sifive,uart0" }, 30 | { }, 31 | }; 32 | 33 | const struct fdt_driver fdt_serial_sifive = { 34 | .match_table = serial_sifive_match, 35 | .init = serial_sifive_init 36 | }; 37 | -------------------------------------------------------------------------------- /lib/utils/serial/fdt_serial_uart8250.c: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2020 Western Digital Corporation or its affiliates. 5 | * 6 | * Authors: 7 | * Anup Patel 8 | */ 9 | 10 | #include 11 | #include 12 | #include 13 | 14 | static int serial_uart8250_init(const void *fdt, int nodeoff, 15 | const struct fdt_match *match) 16 | { 17 | struct platform_uart_data uart = { 0 }; 18 | ulong caps = (ulong)match->data; 19 | int rc; 20 | 21 | rc = fdt_parse_uart_node(fdt, nodeoff, &uart); 22 | if (rc) 23 | return rc; 24 | 25 | return uart8250_init(uart.addr, uart.freq, uart.baud, 26 | uart.reg_shift, uart.reg_io_width, 27 | uart.reg_offset, caps); 28 | } 29 | 30 | static const struct fdt_match serial_uart8250_match[] = { 31 | { .compatible = "ns16550" }, 32 | { .compatible = "ns16550a" }, 33 | { .compatible = "snps,dw-apb-uart" }, 34 | { .compatible = "intel,xscale-uart", 35 | .data = (void *)UART_CAP_UUE }, 36 | { }, 37 | }; 38 | 39 | const struct fdt_driver fdt_serial_uart8250 = { 40 | .match_table = serial_uart8250_match, 41 | .init = serial_uart8250_init, 42 | }; 43 | -------------------------------------------------------------------------------- /lib/utils/serial/fdt_serial_xlnx_uartlite.c: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2022 Western Digital Corporation or its affiliates. 5 | * 6 | * Authors: 7 | * Alistair Francis 8 | */ 9 | 10 | #include 11 | #include 12 | #include 13 | 14 | static int serial_xlnx_uartlite_init(const void *fdt, int nodeoff, 15 | const struct fdt_match *match) 16 | { 17 | int rc; 18 | struct platform_uart_data uart = { 0 }; 19 | 20 | rc = fdt_parse_xlnx_uartlite_node(fdt, nodeoff, &uart); 21 | if (rc) 22 | return rc; 23 | 24 | return xlnx_uartlite_init(uart.addr); 25 | } 26 | 27 | static const struct fdt_match serial_xlnx_uartlite_match[] = { 28 | { .compatible = "xlnx,xps-uartlite-1.00.a" }, 29 | { }, 30 | }; 31 | 32 | const struct fdt_driver fdt_serial_xlnx_uartlite = { 33 | .match_table = serial_xlnx_uartlite_match, 34 | .init = serial_xlnx_uartlite_init, 35 | }; 36 | -------------------------------------------------------------------------------- /lib/utils/serial/litex-uart.c: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2021 Gabriel Somlo 5 | * 6 | * Authors: 7 | * Gabriel Somlo 8 | */ 9 | 10 | #include 11 | #include 12 | #include 13 | 14 | /* clang-format off */ 15 | 16 | #define UART_REG_RXTX 0 17 | #define UART_REG_TXFULL 1 18 | #define UART_REG_RXEMPTY 2 19 | #define UART_REG_EV_STATUS 3 20 | #define UART_REG_EV_PENDING 4 21 | #define UART_REG_EV_ENABLE 5 22 | 23 | #define UART_EV_RX 0x2 24 | 25 | /* clang-format on */ 26 | 27 | static volatile u32 *uart_base; 28 | 29 | static u8 get_reg(u8 reg) 30 | { 31 | return readb(uart_base + reg); 32 | } 33 | 34 | static void set_reg(u8 reg, u8 val) 35 | { 36 | writeb(val, uart_base + reg); 37 | } 38 | 39 | static void litex_uart_putc(char ch) 40 | { 41 | while (get_reg(UART_REG_TXFULL)); 42 | set_reg(UART_REG_RXTX, ch); 43 | } 44 | 45 | static int litex_uart_getc(void) 46 | { 47 | int ret; 48 | 49 | if (get_reg(UART_REG_RXEMPTY)) 50 | return -1; 51 | 52 | ret = get_reg(UART_REG_RXTX); 53 | set_reg(UART_REG_EV_PENDING, UART_EV_RX); /* ack. char read */ 54 | return ret; 55 | } 56 | 57 | static struct sbi_console_device litex_console = { 58 | .name = "litex_uart", 59 | .console_putc = litex_uart_putc, 60 | .console_getc = litex_uart_getc 61 | }; 62 | 63 | int litex_uart_init(unsigned long base) 64 | { 65 | uart_base = (volatile u32 *)base; 66 | set_reg(UART_REG_EV_ENABLE, 0); /* UART in polling mode: disable IRQ */ 67 | sbi_console_set_device(&litex_console); 68 | return 0; 69 | } 70 | -------------------------------------------------------------------------------- /lib/utils/serial/shakti-uart.c: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2020 Vijai Kumar K 5 | */ 6 | 7 | #include 8 | #include 9 | #include 10 | 11 | #define REG_BAUD 0x00 12 | #define REG_TX 0x04 13 | #define REG_RX 0x08 14 | #define REG_STATUS 0x0C 15 | #define REG_DELAY 0x10 16 | #define REG_CONTROL 0x14 17 | #define REG_INT_EN 0x18 18 | #define REG_IQ_CYCLES 0x1C 19 | #define REG_RX_THRES 0x20 20 | 21 | #define UART_TX_FULL 0x2 22 | #define UART_RX_NOT_EMPTY 0x4 23 | #define UART_RX_FULL 0x8 24 | 25 | static volatile char *uart_base; 26 | 27 | static void shakti_uart_putc(char ch) 28 | { 29 | while ((readb(uart_base + REG_STATUS) & UART_TX_FULL)) 30 | ; 31 | writeb(ch, uart_base + REG_TX); 32 | } 33 | 34 | static int shakti_uart_getc(void) 35 | { 36 | if (readb(uart_base + REG_STATUS) & UART_RX_NOT_EMPTY) 37 | return readb(uart_base + REG_RX); 38 | return -1; 39 | } 40 | 41 | static struct sbi_console_device shakti_console = { 42 | .name = "shakti_uart", 43 | .console_putc = shakti_uart_putc, 44 | .console_getc = shakti_uart_getc 45 | }; 46 | 47 | int shakti_uart_init(unsigned long base, u32 in_freq, u32 baudrate) 48 | { 49 | uart_base = (volatile char *)base; 50 | u16 baud; 51 | 52 | if (baudrate) { 53 | baud = (u16)(in_freq / (16 * baudrate)); 54 | writew(baud, uart_base + REG_BAUD); 55 | } 56 | 57 | sbi_console_set_device(&shakti_console); 58 | 59 | return 0; 60 | } 61 | -------------------------------------------------------------------------------- /lib/utils/serial/xlnx-uartlite.c: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2022 Western Digital Corporation or its affiliates. 5 | * 6 | * Authors: 7 | * Alistair Francis 8 | */ 9 | 10 | #include 11 | #include 12 | #include 13 | 14 | /* clang-format off */ 15 | 16 | #define UART_RX_OFFSET 0x00 17 | #define UART_TX_OFFSET 0x04 18 | #define UART_STATUS_OFFSET 0x08 19 | # define UART_STATUS_RXVALID 0x01 20 | # define UART_STATUS_RXFULL 0x02 21 | # define UART_STATUS_TXEMPTY 0x04 22 | # define UART_STATUS_TXFULL 0x08 23 | # define UART_STATUS_IE 0x10 24 | # define UART_STATUS_OVERRUN 0x20 25 | # define UART_STATUS_FRAME 0x40 26 | # define UART_STATUS_PARITY 0x80 27 | #define UART_CTRL_OFFSET 0x0C 28 | # define UART_CTRL_RST_TX 0x01 29 | # define UART_CTRL_RST_RX 0x02 30 | # define UART_CTRL_IE 0x10 31 | 32 | /* clang-format on */ 33 | 34 | static volatile char *xlnx_uartlite_base; 35 | 36 | static void xlnx_uartlite_putc(char ch) 37 | { 38 | while((readb(xlnx_uartlite_base + UART_STATUS_OFFSET) & UART_STATUS_TXFULL)) 39 | ; 40 | 41 | writeb(ch, xlnx_uartlite_base + UART_TX_OFFSET); 42 | } 43 | 44 | static int xlnx_uartlite_getc(void) 45 | { 46 | u16 status = readb(xlnx_uartlite_base + UART_STATUS_OFFSET); 47 | 48 | if (status & UART_STATUS_RXVALID) 49 | return readb(xlnx_uartlite_base + UART_RX_OFFSET); 50 | 51 | return -1; 52 | } 53 | 54 | static struct sbi_console_device xlnx_uartlite_console = { 55 | .name = "xlnx-uartlite", 56 | .console_putc = xlnx_uartlite_putc, 57 | .console_getc = xlnx_uartlite_getc 58 | }; 59 | 60 | int xlnx_uartlite_init(unsigned long base) 61 | { 62 | xlnx_uartlite_base = (volatile char *)base; 63 | 64 | sbi_console_set_device(&xlnx_uartlite_console); 65 | 66 | return 0; 67 | } 68 | -------------------------------------------------------------------------------- /lib/utils/suspend/Kconfig: -------------------------------------------------------------------------------- 1 | # SPDX-License-Identifier: BSD-2-Clause 2 | 3 | menu "System Suspend Support" 4 | 5 | config FDT_SUSPEND 6 | bool "FDT based suspend drivers" 7 | depends on FDT 8 | default n 9 | 10 | if FDT_SUSPEND 11 | 12 | config FDT_SUSPEND_RPMI 13 | bool "FDT RPMI suspend driver" 14 | depends on FDT_MAILBOX && RPMI_MAILBOX 15 | default n 16 | 17 | endif 18 | 19 | endmenu 20 | -------------------------------------------------------------------------------- /lib/utils/suspend/objects.mk: -------------------------------------------------------------------------------- 1 | # 2 | # SPDX-License-Identifier: BSD-2-Clause 3 | # 4 | # Copyright (c) 2024 Ventana Micro Systems Inc. 5 | # 6 | # Authors: 7 | # Anup Patel 8 | # 9 | 10 | carray-fdt_early_drivers-$(CONFIG_FDT_SUSPEND_RPMI) += fdt_suspend_rpmi 11 | libsbiutils-objs-$(CONFIG_FDT_SUSPEND_RPMI) += suspend/fdt_suspend_rpmi.o 12 | -------------------------------------------------------------------------------- /lib/utils/sys/Kconfig: -------------------------------------------------------------------------------- 1 | # SPDX-License-Identifier: BSD-2-Clause 2 | 3 | menu "System Device Support" 4 | 5 | config SYS_ATCSMU 6 | bool "Andes System Management Unit (SMU) support" 7 | default n 8 | 9 | config SYS_HTIF 10 | bool "Host transfere interface (HTIF) support" 11 | default n 12 | 13 | endmenu 14 | -------------------------------------------------------------------------------- /lib/utils/sys/objects.mk: -------------------------------------------------------------------------------- 1 | # 2 | # SPDX-License-Identifier: BSD-2-Clause 3 | # 4 | # Copyright (c) 2019 Western Digital Corporation or its affiliates. 5 | # 6 | # Authors: 7 | # Anup Patel 8 | # 9 | 10 | libsbiutils-objs-$(CONFIG_SYS_HTIF) += sys/htif.o 11 | libsbiutils-objs-$(CONFIG_SYS_ATCSMU) += sys/atcsmu.o 12 | -------------------------------------------------------------------------------- /lib/utils/timer/Kconfig: -------------------------------------------------------------------------------- 1 | # SPDX-License-Identifier: BSD-2-Clause 2 | 3 | menu "Timer Device Support" 4 | 5 | config FDT_TIMER 6 | bool "FDT based timer drivers" 7 | depends on FDT 8 | default n 9 | 10 | if FDT_TIMER 11 | 12 | config FDT_TIMER_MTIMER 13 | bool "ACLINT MTIMER FDT driver" 14 | select TIMER_MTIMER 15 | default n 16 | 17 | config FDT_TIMER_PLMT 18 | bool "Andes PLMT FDT driver" 19 | select TIMER_PLMT 20 | default n 21 | 22 | endif 23 | 24 | config TIMER_MTIMER 25 | bool "ACLINT MTIMER support" 26 | default n 27 | 28 | config TIMER_PLMT 29 | bool "Andes PLMT support" 30 | default n 31 | 32 | endmenu 33 | -------------------------------------------------------------------------------- /lib/utils/timer/fdt_timer.c: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2020 Western Digital Corporation or its affiliates. 5 | * 6 | * Authors: 7 | * Anup Patel 8 | */ 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | /* List of FDT timer drivers generated at compile time */ 16 | extern const struct fdt_driver *const fdt_timer_drivers[]; 17 | 18 | int fdt_timer_init(void) 19 | { 20 | /* 21 | * Systems with Sstc might not provide any node in the FDT, 22 | * so do not return a failure if no device is found. 23 | */ 24 | return fdt_driver_init_all(fdt_get_address(), fdt_timer_drivers); 25 | } 26 | -------------------------------------------------------------------------------- /lib/utils/timer/fdt_timer_drivers.carray: -------------------------------------------------------------------------------- 1 | HEADER: sbi_utils/timer/fdt_timer.h 2 | TYPE: const struct fdt_driver 3 | NAME: fdt_timer_drivers 4 | -------------------------------------------------------------------------------- /lib/utils/timer/fdt_timer_plmt.c: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2022 Andes Technology Corporation 5 | * 6 | * Authors: 7 | * Yu Chien Peter Lin 8 | */ 9 | 10 | #include 11 | #include 12 | #include 13 | 14 | extern struct plmt_data plmt; 15 | 16 | static int fdt_plmt_cold_timer_init(const void *fdt, int nodeoff, 17 | const struct fdt_match *match) 18 | { 19 | int rc; 20 | unsigned long plmt_base; 21 | 22 | rc = fdt_parse_plmt_node(fdt, nodeoff, &plmt_base, &plmt.size, 23 | &plmt.hart_count); 24 | if (rc) 25 | return rc; 26 | 27 | plmt.time_val = (u64 *)plmt_base; 28 | plmt.time_cmp = (u64 *)(plmt_base + 0x8); 29 | 30 | rc = fdt_parse_timebase_frequency(fdt, &plmt.timer_freq); 31 | if (rc) 32 | return rc; 33 | 34 | rc = plmt_cold_timer_init(&plmt); 35 | if (rc) 36 | return rc; 37 | 38 | return 0; 39 | } 40 | 41 | static const struct fdt_match timer_plmt_match[] = { 42 | { .compatible = "andestech,plmt0" }, 43 | {}, 44 | }; 45 | 46 | const struct fdt_driver fdt_timer_plmt = { 47 | .match_table = timer_plmt_match, 48 | .init = fdt_plmt_cold_timer_init, 49 | }; 50 | -------------------------------------------------------------------------------- /lib/utils/timer/objects.mk: -------------------------------------------------------------------------------- 1 | # 2 | # SPDX-License-Identifier: BSD-2-Clause 3 | # 4 | # Copyright (c) 2020 Western Digital Corporation or its affiliates. 5 | # 6 | # Authors: 7 | # Anup Patel 8 | # 9 | 10 | libsbiutils-objs-$(CONFIG_TIMER_MTIMER) += timer/aclint_mtimer.o 11 | libsbiutils-objs-$(CONFIG_TIMER_PLMT) += timer/andes_plmt.o 12 | 13 | libsbiutils-objs-$(CONFIG_FDT_TIMER) += timer/fdt_timer.o 14 | libsbiutils-objs-$(CONFIG_FDT_TIMER) += timer/fdt_timer_drivers.carray.o 15 | 16 | carray-fdt_timer_drivers-$(CONFIG_FDT_TIMER_MTIMER) += fdt_timer_mtimer 17 | libsbiutils-objs-$(CONFIG_FDT_TIMER_MTIMER) += timer/fdt_timer_mtimer.o 18 | 19 | carray-fdt_timer_drivers-$(CONFIG_FDT_TIMER_PLMT) += fdt_timer_plmt 20 | libsbiutils-objs-$(CONFIG_FDT_TIMER_PLMT) += timer/fdt_timer_plmt.o 21 | -------------------------------------------------------------------------------- /platform/fpga/ariane/Kconfig: -------------------------------------------------------------------------------- 1 | # SPDX-License-Identifier: BSD-2-Clause 2 | 3 | config PLATFORM_ARIANE_FPGA 4 | bool 5 | select FDT 6 | select IPI_MSWI 7 | select IRQCHIP_PLIC 8 | select SERIAL_UART8250 9 | select TIMER_MTIMER 10 | default y 11 | -------------------------------------------------------------------------------- /platform/fpga/ariane/configs/defconfig: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/riscv-software-src/opensbi/69a0f0245f39ea3af4685cab4cb2dda90acd17cd/platform/fpga/ariane/configs/defconfig -------------------------------------------------------------------------------- /platform/fpga/ariane/objects.mk: -------------------------------------------------------------------------------- 1 | # 2 | # SPDX-License-Identifier: BSD-2-Clause 3 | # 4 | # Copyright (C) 2019 FORTH-ICS/CARV 5 | # Panagiotis Peristerakis 6 | # 7 | 8 | # Compiler flags 9 | platform-cppflags-y = 10 | platform-cflags-y = 11 | platform-asflags-y = 12 | platform-ldflags-y = 13 | 14 | # Object to build 15 | platform-objs-y += platform.o 16 | 17 | PLATFORM_RISCV_XLEN = 64 18 | 19 | # Blobs to build 20 | FW_JUMP=n 21 | 22 | ifeq ($(PLATFORM_RISCV_XLEN), 32) 23 | # This needs to be 4MB aligned for 32-bit support 24 | FW_JUMP_ADDR=0x80400000 25 | else 26 | # This needs to be 2MB aligned for 64-bit support 27 | FW_JUMP_ADDR=0x80200000 28 | endif 29 | FW_JUMP_FDT_ADDR=0x82200000 30 | 31 | # Firmware with payload configuration. 32 | FW_PAYLOAD=y 33 | 34 | ifeq ($(PLATFORM_RISCV_XLEN), 32) 35 | # This needs to be 4MB aligned for 32-bit support 36 | FW_PAYLOAD_OFFSET=0x400000 37 | else 38 | # This needs to be 2MB aligned for 64-bit support 39 | FW_PAYLOAD_OFFSET=0x200000 40 | endif 41 | FW_PAYLOAD_FDT_ADDR=0x82200000 42 | FW_PAYLOAD_ALIGN=0x1000 43 | -------------------------------------------------------------------------------- /platform/fpga/openpiton/Kconfig: -------------------------------------------------------------------------------- 1 | # SPDX-License-Identifier: BSD-2-Clause 2 | 3 | config PLATFORM_OPENPITON_FPGA 4 | bool 5 | select FDT 6 | select IPI_MSWI 7 | select IRQCHIP_PLIC 8 | select SERIAL_UART8250 9 | select TIMER_MTIMER 10 | default y 11 | -------------------------------------------------------------------------------- /platform/fpga/openpiton/configs/defconfig: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/riscv-software-src/opensbi/69a0f0245f39ea3af4685cab4cb2dda90acd17cd/platform/fpga/openpiton/configs/defconfig -------------------------------------------------------------------------------- /platform/fpga/openpiton/objects.mk: -------------------------------------------------------------------------------- 1 | # 2 | # SPDX-License-Identifier: BSD-2-Clause 3 | # 4 | # Copyright (c) 2020 Western Digital Corporation or its affiliates. 5 | # 6 | 7 | # Compiler flags 8 | platform-cppflags-y = 9 | platform-cflags-y = 10 | platform-asflags-y = 11 | platform-ldflags-y = 12 | 13 | # Objects to build 14 | platform-objs-y += platform.o 15 | 16 | PLATFORM_RISCV_XLEN = 64 17 | 18 | # Blobs to build 19 | FW_JUMP=n 20 | 21 | ifeq ($(PLATFORM_RISCV_XLEN), 32) 22 | # This needs to be 4MB aligned for 32-bit support 23 | FW_JUMP_ADDR=0x80400000 24 | else 25 | # This needs to be 2MB aligned for 64-bit support 26 | FW_JUMP_ADDR=0x80200000 27 | endif 28 | FW_JUMP_FDT_ADDR=0x82200000 29 | 30 | # Firmware with payload configuration. 31 | FW_PAYLOAD=y 32 | 33 | ifeq ($(PLATFORM_RISCV_XLEN), 32) 34 | # This needs to be 4MB aligned for 32-bit support 35 | FW_PAYLOAD_OFFSET=0x400000 36 | else 37 | # This needs to be 2MB aligned for 64-bit support 38 | FW_PAYLOAD_OFFSET=0x200000 39 | endif 40 | FW_PAYLOAD_FDT_ADDR=0x82200000 41 | FW_PAYLOAD_ALIGN=0x1000 42 | -------------------------------------------------------------------------------- /platform/generic/Kconfig: -------------------------------------------------------------------------------- 1 | # SPDX-License-Identifier: BSD-2-Clause 2 | 3 | config PLATFORM_GENERIC 4 | bool 5 | select FDT 6 | select FDT_DOMAIN 7 | select FDT_PMU 8 | default y 9 | 10 | if PLATFORM_GENERIC 11 | 12 | config PLATFORM_GENERIC_NAME 13 | string "Platform default name" 14 | default "Generic" 15 | 16 | config PLATFORM_GENERIC_MAJOR_VER 17 | int "Platform major version" 18 | range 0 65535 19 | default 0 20 | 21 | config PLATFORM_GENERIC_MINOR_VER 22 | int "Platform minor version" 23 | range 0 65535 24 | default 1 25 | 26 | config PLATFORM_ALLWINNER_D1 27 | bool "Allwinner D1 support" 28 | depends on FDT_IRQCHIP_PLIC 29 | select THEAD_C9XX_PMU 30 | default n 31 | 32 | config PLATFORM_ANDES_AE350 33 | bool "Andes AE350 support" 34 | select SYS_ATCSMU 35 | select ANDES_PMU 36 | select ANDES_PMA 37 | default n 38 | 39 | config PLATFORM_RENESAS_RZFIVE 40 | bool "Renesas RZ/Five support" 41 | select ANDES_PMA 42 | select ANDES_SBI 43 | select ANDES_PMU 44 | default n 45 | 46 | config PLATFORM_SIFIVE_FU540 47 | bool "SiFive FU540 support" 48 | default n 49 | 50 | config PLATFORM_SIFIVE_FU740 51 | bool "SiFive FU740 support" 52 | depends on FDT_RESET && FDT_I2C 53 | default n 54 | 55 | config PLATFORM_SOPHGO_SG2042 56 | bool "Sophgo sg2042 support" 57 | select THEAD_C9XX_ERRATA 58 | select THEAD_C9XX_PMU 59 | default n 60 | 61 | config PLATFORM_STARFIVE_JH7110 62 | bool "StarFive JH7110 support" 63 | default n 64 | 65 | config PLATFORM_THEAD 66 | bool "THEAD C9xx support" 67 | select THEAD_C9XX_ERRATA 68 | select THEAD_C9XX_PMU 69 | default n 70 | 71 | source "$(OPENSBI_SRC_DIR)/platform/generic/andes/Kconfig" 72 | source "$(OPENSBI_SRC_DIR)/platform/generic/thead/Kconfig" 73 | 74 | endif 75 | -------------------------------------------------------------------------------- /platform/generic/allwinner/objects.mk: -------------------------------------------------------------------------------- 1 | # 2 | # SPDX-License-Identifier: BSD-2-Clause 3 | # 4 | 5 | carray-platform_override_modules-$(CONFIG_PLATFORM_ALLWINNER_D1) += sun20i_d1 6 | platform-objs-$(CONFIG_PLATFORM_ALLWINNER_D1) += allwinner/sun20i-d1.o 7 | -------------------------------------------------------------------------------- /platform/generic/andes/Kconfig: -------------------------------------------------------------------------------- 1 | # SPDX-License-Identifier: BSD-2-Clause 2 | 3 | config ANDES_PMA 4 | bool "Andes PMA support" 5 | default n 6 | help 7 | Programmable PMA(PPMA) is a feature for Andes. PPMA allows 8 | dynamic adjustment of memory attributes in the runtime. 9 | It contains a configurable amount of PMA entries implemented 10 | as CSRs to control the attributes of memory locations in interest. 11 | 12 | config ANDES_SBI 13 | bool "Andes SBI support" 14 | default n 15 | 16 | config ANDES_PMU 17 | bool "Andes PMU extension (XAndesPMU) support" 18 | default n 19 | help 20 | Andes PMU extension supports the event counter overflow 21 | interrupt and mode filtering, similar to the standard 22 | Sscofpmf and Smcntrpmf. 23 | -------------------------------------------------------------------------------- /platform/generic/andes/andes_sbi.c: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: BSD-2-Clause 2 | /* 3 | * Copyright (C) 2023 Renesas Electronics Corp. 4 | * 5 | */ 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | enum sbi_ext_andes_fid { 13 | SBI_EXT_ANDES_FID0 = 0, /* Reserved for future use */ 14 | SBI_EXT_ANDES_IOCP_SW_WORKAROUND, 15 | SBI_EXT_ANDES_PMA_PROBE, 16 | SBI_EXT_ANDES_PMA_SET, 17 | SBI_EXT_ANDES_PMA_FREE, 18 | }; 19 | 20 | static bool andes_cache_controllable(void) 21 | { 22 | return (((csr_read(CSR_MICM_CFG) & MICM_CFG_ISZ_MASK) || 23 | (csr_read(CSR_MDCM_CFG) & MDCM_CFG_DSZ_MASK)) && 24 | (csr_read(CSR_MMSC_CFG) & MMSC_CFG_CCTLCSR_MASK) && 25 | (csr_read(CSR_MCACHE_CTL) & MCACHE_CTL_CCTL_SUEN_MASK) && 26 | misa_extension('U')); 27 | } 28 | 29 | static bool andes_iocp_disabled(void) 30 | { 31 | return (csr_read(CSR_MMSC_CFG) & MMSC_IOCP_MASK) ? false : true; 32 | } 33 | 34 | static bool andes_apply_iocp_sw_workaround(void) 35 | { 36 | return andes_cache_controllable() && andes_iocp_disabled(); 37 | } 38 | 39 | int andes_sbi_vendor_ext_provider(long funcid, 40 | struct sbi_trap_regs *regs, 41 | struct sbi_ecall_return *out) 42 | { 43 | int ret = 0; 44 | 45 | switch (funcid) { 46 | case SBI_EXT_ANDES_IOCP_SW_WORKAROUND: 47 | out->value = andes_apply_iocp_sw_workaround(); 48 | break; 49 | case SBI_EXT_ANDES_PMA_PROBE: 50 | out->value = andes_sbi_probe_pma(); 51 | break; 52 | case SBI_EXT_ANDES_PMA_SET: 53 | ret = andes_sbi_set_pma(regs->a0, regs->a1, regs->a2); 54 | break; 55 | case SBI_EXT_ANDES_PMA_FREE: 56 | ret = andes_sbi_free_pma(regs->a0); 57 | break; 58 | default: 59 | ret = SBI_ENOTSUPP; 60 | } 61 | 62 | return ret; 63 | } 64 | -------------------------------------------------------------------------------- /platform/generic/andes/objects.mk: -------------------------------------------------------------------------------- 1 | # 2 | # SPDX-License-Identifier: BSD-2-Clause 3 | # 4 | 5 | carray-platform_override_modules-$(CONFIG_PLATFORM_ANDES_AE350) += andes_ae350 6 | platform-objs-$(CONFIG_PLATFORM_ANDES_AE350) += andes/ae350.o andes/sleep.o 7 | 8 | platform-objs-$(CONFIG_ANDES_PMA) += andes/andes_pma.o 9 | platform-objs-$(CONFIG_ANDES_SBI) += andes/andes_sbi.o 10 | platform-objs-$(CONFIG_ANDES_PMU) += andes/andes_pmu.o 11 | -------------------------------------------------------------------------------- /platform/generic/andes/sleep.S: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2023 Andes Technology Corporation 5 | * 6 | * Authors: 7 | * Yu Chien Peter Lin 8 | */ 9 | 10 | #include 11 | #include 12 | #include 13 | 14 | .section .text, "ax", %progbits 15 | .align 3 16 | .global __ae350_disable_coherency 17 | __ae350_disable_coherency: 18 | /* flush d-cache */ 19 | csrw CSR_MCCTLCOMMAND, 0x6 20 | /* disable i/d-cache */ 21 | csrc CSR_MCACHE_CTL, 0x3 22 | /* disable d-cache coherency */ 23 | lui t1, 0x80 24 | csrc CSR_MCACHE_CTL, t1 25 | /* 26 | * wait for mcache_ctl.DC_COHSTA to be cleared, 27 | * the bit is hard-wired 0 on platforms w/o CM 28 | * (Coherence Manager) 29 | */ 30 | check_cm_disabled: 31 | csrr t1, CSR_MCACHE_CTL 32 | srli t1, t1, 20 33 | andi t1, t1, 0x1 34 | bnez t1, check_cm_disabled 35 | 36 | ret 37 | 38 | .section .text, "ax", %progbits 39 | .align 3 40 | .global __ae350_enable_coherency 41 | __ae350_enable_coherency: 42 | /* enable d-cache coherency */ 43 | lui t1, 0x80 44 | csrs CSR_MCACHE_CTL, t1 45 | /* 46 | * mcache_ctl.DC_COHEN is hard-wired 0 on platforms 47 | * w/o CM support 48 | */ 49 | csrr t1, CSR_MCACHE_CTL 50 | srli t1, t1, 19 51 | andi t1, t1, 0x1 52 | beqz t1, enable_L1_cache 53 | /* wait for mcache_ctl.DC_COHSTA to be set */ 54 | check_cm_enabled: 55 | csrr t1, CSR_MCACHE_CTL 56 | srli t1, t1, 20 57 | andi t1, t1, 0x1 58 | beqz t1, check_cm_enabled 59 | enable_L1_cache: 60 | /* enable i/d-cache */ 61 | csrs CSR_MCACHE_CTL, 0x3 62 | 63 | ret 64 | 65 | .section .text, "ax", %progbits 66 | .align 3 67 | .global __ae350_enable_coherency_warmboot 68 | __ae350_enable_coherency_warmboot: 69 | call ra, __ae350_enable_coherency 70 | j _start_warm 71 | -------------------------------------------------------------------------------- /platform/generic/configs/defconfig: -------------------------------------------------------------------------------- 1 | CONFIG_PLATFORM_ALLWINNER_D1=y 2 | CONFIG_PLATFORM_ANDES_AE350=y 3 | CONFIG_PLATFORM_RENESAS_RZFIVE=y 4 | CONFIG_PLATFORM_SIFIVE_FU540=y 5 | CONFIG_PLATFORM_SIFIVE_FU740=y 6 | CONFIG_PLATFORM_SOPHGO_SG2042=y 7 | CONFIG_PLATFORM_STARFIVE_JH7110=y 8 | CONFIG_PLATFORM_THEAD=y 9 | CONFIG_FDT_CPPC=y 10 | CONFIG_FDT_CPPC_RPMI=y 11 | CONFIG_FDT_GPIO=y 12 | CONFIG_FDT_GPIO_DESIGNWARE=y 13 | CONFIG_FDT_GPIO_SIFIVE=y 14 | CONFIG_FDT_GPIO_STARFIVE=y 15 | CONFIG_FDT_HSM=y 16 | CONFIG_FDT_HSM_RPMI=y 17 | CONFIG_FDT_I2C=y 18 | CONFIG_FDT_I2C_SIFIVE=y 19 | CONFIG_FDT_I2C_DW=y 20 | CONFIG_FDT_IPI=y 21 | CONFIG_FDT_IPI_MSWI=y 22 | CONFIG_FDT_IPI_PLICSW=y 23 | CONFIG_FDT_IRQCHIP=y 24 | CONFIG_FDT_IRQCHIP_APLIC=y 25 | CONFIG_FDT_IRQCHIP_IMSIC=y 26 | CONFIG_FDT_IRQCHIP_PLIC=y 27 | CONFIG_FDT_MAILBOX=y 28 | CONFIG_RPMI_MAILBOX=y 29 | CONFIG_FDT_MAILBOX_RPMI_SHMEM=y 30 | CONFIG_FDT_REGMAP=y 31 | CONFIG_FDT_REGMAP_SYSCON=y 32 | CONFIG_FDT_RESET=y 33 | CONFIG_FDT_RESET_ATCWDT200=y 34 | CONFIG_FDT_RESET_GPIO=y 35 | CONFIG_FDT_RESET_HTIF=y 36 | CONFIG_FDT_RESET_RPMI=y 37 | CONFIG_FDT_RESET_SUNXI_WDT=y 38 | CONFIG_FDT_RESET_SG2042_HWMON_MCU=y 39 | CONFIG_FDT_RESET_SYSCON=y 40 | CONFIG_FDT_SERIAL=y 41 | CONFIG_FDT_SERIAL_CADENCE=y 42 | CONFIG_FDT_SERIAL_GAISLER=y 43 | CONFIG_FDT_SERIAL_HTIF=y 44 | CONFIG_FDT_SERIAL_RENESAS_SCIF=y 45 | CONFIG_FDT_SERIAL_SHAKTI=y 46 | CONFIG_FDT_SERIAL_SIFIVE=y 47 | CONFIG_FDT_SERIAL_LITEX=y 48 | CONFIG_FDT_SERIAL_UART8250=y 49 | CONFIG_FDT_SERIAL_XILINX_UARTLITE=y 50 | CONFIG_SERIAL_SEMIHOSTING=y 51 | CONFIG_FDT_SUSPEND=y 52 | CONFIG_FDT_SUSPEND_RPMI=y 53 | CONFIG_FDT_TIMER=y 54 | CONFIG_FDT_TIMER_MTIMER=y 55 | CONFIG_FDT_TIMER_PLMT=y 56 | CONFIG_FDT_MPXY=y 57 | CONFIG_FDT_MPXY_RPMI_MBOX=y 58 | CONFIG_FDT_MPXY_RPMI_CLOCK=y 59 | CONFIG_FDT_MPXY_RPMI_SYSMSI=y 60 | -------------------------------------------------------------------------------- /platform/generic/include/andes/andes_pmu.h: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) Copyright (c) 2023 Andes Technology Corporation 5 | */ 6 | 7 | #ifndef _RISCV_ANDES_PMU_H 8 | #define _RISCV_ANDES_PMU_H 9 | 10 | #include 11 | 12 | int andes_pmu_init(void); 13 | int andes_pmu_extensions_init(struct sbi_hart_features *hfeatures); 14 | 15 | #endif /* _RISCV_ANDES_PMU_H */ 16 | -------------------------------------------------------------------------------- /platform/generic/include/andes/andes_sbi.h: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: BSD-2-Clause 2 | 3 | #ifndef _RISCV_ANDES_SBI_H 4 | #define _RISCV_ANDES_SBI_H 5 | 6 | #include 7 | #include 8 | 9 | int andes_sbi_vendor_ext_provider(long funcid, 10 | struct sbi_trap_regs *regs, 11 | struct sbi_ecall_return *out); 12 | 13 | #endif /* _RISCV_ANDES_SBI_H */ 14 | -------------------------------------------------------------------------------- /platform/generic/include/platform_override.h: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2020 Western Digital Corporation or its affiliates. 5 | * 6 | * Authors: 7 | * Anup Patel 8 | */ 9 | 10 | #ifndef __PLATFORM_OVERRIDE_H__ 11 | #define __PLATFORM_OVERRIDE_H__ 12 | 13 | #include 14 | #include 15 | #include 16 | #include 17 | 18 | bool generic_cold_boot_allowed(u32 hartid); 19 | int generic_nascent_init(void); 20 | int generic_early_init(bool cold_boot); 21 | int generic_final_init(bool cold_boot); 22 | int generic_extensions_init(struct sbi_hart_features *hfeatures); 23 | int generic_domains_init(void); 24 | int generic_pmu_init(void); 25 | uint64_t generic_pmu_xlate_to_mhpmevent(uint32_t event_idx, uint64_t data); 26 | u64 generic_tlbr_flush_limit(void); 27 | u32 generic_tlb_num_entries(void); 28 | int generic_mpxy_init(void); 29 | 30 | extern struct sbi_platform_operations generic_platform_ops; 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /platform/generic/include/thead/c9xx_errata.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef __RISCV_THEAD_C9XX_ERRATA_H____ 3 | #define __RISCV_THEAD_C9XX_ERRATA_H____ 4 | 5 | /** 6 | * T-HEAD board with this quirk need to execute sfence.vma to flush 7 | * stale entrie avoid incorrect memory access. 8 | */ 9 | #define THEAD_QUIRK_ERRATA_TLB_FLUSH BIT(0) 10 | #define THEAD_QUIRK_ERRATA_THEAD_PMU BIT(1) 11 | 12 | void thead_register_tlb_flush_trap_handler(void); 13 | 14 | #endif // __RISCV_THEAD_C9XX_ERRATA_H____ 15 | -------------------------------------------------------------------------------- /platform/generic/include/thead/c9xx_pmu.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef __RISCV_THEAD_C9XX_PMU_H____ 3 | #define __RISCV_THEAD_C9XX_PMU_H____ 4 | 5 | void thead_c9xx_register_pmu_device(void); 6 | 7 | #endif // __RISCV_THEAD_C9XX_PMU_H____ 8 | -------------------------------------------------------------------------------- /platform/generic/objects.mk: -------------------------------------------------------------------------------- 1 | # 2 | # SPDX-License-Identifier: BSD-2-Clause 3 | # 4 | # Copyright (c) 2020 Western Digital Corporation or its affiliates. 5 | # 6 | # Authors: 7 | # Anup Patel 8 | # 9 | 10 | # Compiler flags 11 | platform-cppflags-y = 12 | platform-cflags-y = 13 | platform-asflags-y = 14 | platform-ldflags-y = 15 | 16 | # Command for platform specific "make run" 17 | platform-runcmd = qemu-system-riscv$(PLATFORM_RISCV_XLEN) -M virt -m 256M \ 18 | -nographic -bios $(build_dir)/platform/generic/firmware/fw_payload.bin 19 | 20 | # Objects to build 21 | platform-objs-y += platform.o 22 | platform-objs-y += platform_override_modules.carray.o 23 | 24 | # Blobs to build 25 | FW_DYNAMIC=y 26 | FW_JUMP=y 27 | ifeq ($(PLATFORM_RISCV_XLEN), 32) 28 | # This needs to be 4MB aligned for 32-bit system 29 | FW_JUMP_OFFSET=0x400000 30 | else 31 | # This needs to be 2MB aligned for 64-bit system 32 | FW_JUMP_OFFSET=0x200000 33 | endif 34 | FW_JUMP_FDT_OFFSET=0x2200000 35 | FW_PAYLOAD=y 36 | ifeq ($(PLATFORM_RISCV_XLEN), 32) 37 | # This needs to be 4MB aligned for 32-bit system 38 | FW_PAYLOAD_OFFSET=0x400000 39 | else 40 | # This needs to be 2MB aligned for 64-bit system 41 | FW_PAYLOAD_OFFSET=0x200000 42 | endif 43 | FW_PAYLOAD_FDT_OFFSET=$(FW_JUMP_FDT_OFFSET) 44 | -------------------------------------------------------------------------------- /platform/generic/platform_override_modules.carray: -------------------------------------------------------------------------------- 1 | HEADER: platform_override.h 2 | TYPE: const struct fdt_driver 3 | NAME: platform_override_modules 4 | -------------------------------------------------------------------------------- /platform/generic/renesas/rzfive/objects.mk: -------------------------------------------------------------------------------- 1 | # 2 | # SPDX-License-Identifier: BSD-2-Clause 3 | # 4 | # Copyright (C) 2022 Renesas Electronics Corp. 5 | # 6 | 7 | carray-platform_override_modules-$(CONFIG_PLATFORM_RENESAS_RZFIVE) += renesas_rzfive 8 | platform-objs-$(CONFIG_PLATFORM_RENESAS_RZFIVE) += renesas/rzfive/rzfive.o 9 | -------------------------------------------------------------------------------- /platform/generic/sifive/fu540.c: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2020 Western Digital Corporation or its affiliates. 5 | * 6 | * Authors: 7 | * Anup Patel 8 | */ 9 | 10 | #include 11 | #include 12 | #include 13 | 14 | static u64 sifive_fu540_tlbr_flush_limit(void) 15 | { 16 | /* 17 | * The sfence.vma by virtual address does not work on 18 | * SiFive FU540 so we return remote TLB flush limit as zero. 19 | */ 20 | return 0; 21 | } 22 | 23 | static int sifive_fu540_platform_init(const void *fdt, int nodeoff, const struct fdt_match *match) 24 | { 25 | generic_platform_ops.get_tlbr_flush_limit = sifive_fu540_tlbr_flush_limit; 26 | 27 | return 0; 28 | } 29 | 30 | static const struct fdt_match sifive_fu540_match[] = { 31 | { .compatible = "sifive,fu540" }, 32 | { .compatible = "sifive,fu540g" }, 33 | { .compatible = "sifive,fu540-c000" }, 34 | { .compatible = "sifive,hifive-unleashed-a00" }, 35 | { }, 36 | }; 37 | 38 | const struct fdt_driver sifive_fu540 = { 39 | .match_table = sifive_fu540_match, 40 | .init = sifive_fu540_platform_init, 41 | }; 42 | -------------------------------------------------------------------------------- /platform/generic/sifive/objects.mk: -------------------------------------------------------------------------------- 1 | # 2 | # SPDX-License-Identifier: BSD-2-Clause 3 | # 4 | 5 | carray-platform_override_modules-$(CONFIG_PLATFORM_SIFIVE_FU540) += sifive_fu540 6 | platform-objs-$(CONFIG_PLATFORM_SIFIVE_FU540) += sifive/fu540.o 7 | 8 | carray-platform_override_modules-$(CONFIG_PLATFORM_SIFIVE_FU740) += sifive_fu740 9 | platform-objs-$(CONFIG_PLATFORM_SIFIVE_FU740) += sifive/fu740.o 10 | -------------------------------------------------------------------------------- /platform/generic/sophgo/objects.mk: -------------------------------------------------------------------------------- 1 | # 2 | # SPDX-License-Identifier: BSD-2-Clause 3 | # 4 | # Copyright (C) 2023 Inochi Amaoto 5 | # Copyright (C) 2023 Alibaba Group Holding Limited. 6 | # 7 | 8 | carray-platform_override_modules-$(CONFIG_PLATFORM_SOPHGO_SG2042) += sophgo_sg2042 9 | platform-objs-$(CONFIG_PLATFORM_SOPHGO_SG2042) += sophgo/sg2042.o 10 | -------------------------------------------------------------------------------- /platform/generic/starfive/objects.mk: -------------------------------------------------------------------------------- 1 | # 2 | # SPDX-License-Identifier: BSD-2-Clause 3 | # 4 | 5 | carray-platform_override_modules-$(CONFIG_PLATFORM_STARFIVE_JH7110) += starfive_jh7110 6 | platform-objs-$(CONFIG_PLATFORM_STARFIVE_JH7110) += starfive/jh7110.o 7 | -------------------------------------------------------------------------------- /platform/generic/thead/Kconfig: -------------------------------------------------------------------------------- 1 | # SPDX-License-Identifier: BSD-2-Clause 2 | 3 | config THEAD_C9XX_PMU 4 | bool "T-HEAD c9xx M-mode PMU support" 5 | default n 6 | 7 | config THEAD_C9XX_ERRATA 8 | bool "T-HEAD c9xx errata support" 9 | default n 10 | -------------------------------------------------------------------------------- /platform/generic/thead/objects.mk: -------------------------------------------------------------------------------- 1 | # 2 | # SPDX-License-Identifier: BSD-2-Clause 3 | # 4 | # Copyright (C) 2023 Inochi Amaoto 5 | # Copyright (C) 2023 Alibaba Group Holding Limited. 6 | # 7 | 8 | platform-objs-$(CONFIG_THEAD_C9XX_PMU) += thead/thead_c9xx_pmu.o 9 | 10 | platform-objs-$(CONFIG_THEAD_C9XX_ERRATA) += thead/thead_c9xx_tlb_trap_handler.o 11 | platform-objs-$(CONFIG_THEAD_C9XX_ERRATA) += thead/thead_c9xx_errata_tlb_flush.o 12 | 13 | carray-platform_override_modules-$(CONFIG_PLATFORM_THEAD) += thead_generic 14 | platform-objs-$(CONFIG_PLATFORM_THEAD) += thead/thead-generic.o 15 | -------------------------------------------------------------------------------- /platform/generic/thead/thead_c9xx_errata_tlb_flush.c: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Authors: 5 | * Inochi Amaoto 6 | * 7 | */ 8 | 9 | #include 10 | #include 11 | 12 | void _thead_tlb_flush_fixup_trap_handler(void); 13 | 14 | void thead_register_tlb_flush_trap_handler(void) 15 | { 16 | csr_write(CSR_MTVEC, &_thead_tlb_flush_fixup_trap_handler); 17 | } 18 | -------------------------------------------------------------------------------- /platform/generic/thead/thead_c9xx_tlb_trap_handler.S: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Authors: 5 | * Inochi Amaoto 6 | * 7 | */ 8 | .section .entry, "ax", %progbits 9 | .align 3 10 | .globl _thead_tlb_flush_fixup_trap_handler 11 | _thead_tlb_flush_fixup_trap_handler: 12 | sfence.vma zero, t0 13 | j _trap_handler 14 | -------------------------------------------------------------------------------- /platform/kendryte/k210/Kconfig: -------------------------------------------------------------------------------- 1 | # SPDX-License-Identifier: BSD-2-Clause 2 | 3 | config PLATFORM_KENDRYTE_K210 4 | bool 5 | select FDT 6 | select IPI_MSWI 7 | select IRQCHIP_PLIC 8 | select SERIAL_SIFIVE 9 | select TIMER_MTIMER 10 | default y 11 | -------------------------------------------------------------------------------- /platform/kendryte/k210/configs/defconfig: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/riscv-software-src/opensbi/69a0f0245f39ea3af4685cab4cb2dda90acd17cd/platform/kendryte/k210/configs/defconfig -------------------------------------------------------------------------------- /platform/kendryte/k210/k210.dts: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2019 Western Digital Corporation or its affiliates. 5 | * 6 | * Authors: 7 | * Damien Le Moal 8 | */ 9 | 10 | /dts-v1/; 11 | / { 12 | #address-cells = <2>; 13 | #size-cells = <2>; 14 | compatible = "kendryte,k210"; 15 | 16 | chosen { 17 | bootargs = "console=hvc0 earlycon=sbi"; 18 | }; 19 | 20 | cpus { 21 | #address-cells = <1>; 22 | #size-cells = <0>; 23 | cpu0: cpu@0 { 24 | device_type = "cpu"; 25 | clock-frequency = <390000000>; 26 | i-cache-size = <32768>; 27 | d-cache-size = <32768>; 28 | mmu-type = "none"; 29 | reg = <0>; 30 | riscv,isa = "rv64imafdc"; 31 | status = "okay"; 32 | cpu0_intc: interrupt-controller { 33 | #interrupt-cells = <1>; 34 | compatible = "riscv,cpu-intc"; 35 | interrupt-controller; 36 | }; 37 | }; 38 | cpu1: cpu@1 { 39 | device_type = "cpu"; 40 | clock-frequency = <390000000>; 41 | d-cache-size = <32768>; 42 | i-cache-size = <32768>; 43 | mmu-type = "none"; 44 | reg = <1>; 45 | riscv,isa = "rv64imafdc"; 46 | status = "okay"; 47 | cpu1_intc: interrupt-controller { 48 | #interrupt-cells = <1>; 49 | compatible = "riscv,cpu-intc"; 50 | interrupt-controller; 51 | }; 52 | }; 53 | }; 54 | 55 | memory@80000000 { 56 | /* Bank 0: 4 MB, Bank 1: 2 MB, AI chip SRAM: 2MB */ 57 | device_type = "memory"; 58 | reg = <0x00000000 0x80000000 0x00000000 0x00800000>; 59 | }; 60 | 61 | plic0: interrupt-controller@C000000 { 62 | #interrupt-cells = <1>; 63 | compatible = "riscv,plic0"; 64 | interrupt-controller; 65 | interrupts-extended = 66 | <&cpu0_intc 11 &cpu0_intc 9 67 | &cpu1_intc 11 &cpu1_intc 9>; 68 | reg = <0x0 0xc000000 0x0 0x4000000>; 69 | }; 70 | }; 71 | -------------------------------------------------------------------------------- /platform/kendryte/k210/objects.mk: -------------------------------------------------------------------------------- 1 | # 2 | # SPDX-License-Identifier: BSD-2-Clause 3 | # 4 | # Copyright (c) 2019 Western Digital Corporation or its affiliates. 5 | # 6 | # Authors: 7 | # Damien Le Moal 8 | # 9 | 10 | # Compiler flags 11 | platform-cppflags-y = 12 | platform-cflags-y = 13 | platform-asflags-y = 14 | platform-ldflags-y = 15 | 16 | # Objects to build 17 | platform-objs-y += platform.o 18 | 19 | platform-objs-y += k210.o 20 | platform-varprefix-k210.o = dt_k210 21 | platform-padding-k210.o = 2048 22 | 23 | # Blobs to build 24 | FW_PAYLOAD=y 25 | FW_PAYLOAD_ALIGN=0x1000 26 | -------------------------------------------------------------------------------- /platform/kendryte/k210/platform.h: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2019 Western Digital Corporation or its affiliates. 5 | * 6 | * Authors: 7 | * Damien Le Moal 8 | */ 9 | #ifndef _K210_PLATFORM_H_ 10 | #define _K210_PLATFORM_H_ 11 | 12 | #include 13 | 14 | #define K210_HART_COUNT 2 15 | 16 | #define K210_UART_BAUDRATE 115200 17 | #define K210_ACLINT_MTIMER_FREQ 7800000 18 | #define K210_CLK0_FREQ 26000000UL 19 | #define K210_PLIC_NUM_SOURCES 65 20 | 21 | /* Registers base address */ 22 | #define K210_SYSCTL_BASE_ADDR 0x50440000ULL 23 | #define K210_UART_BASE_ADDR 0x38000000ULL 24 | #define K210_CLINT_BASE_ADDR 0x02000000ULL 25 | #define K210_ACLINT_MSWI_ADDR \ 26 | (K210_CLINT_BASE_ADDR + CLINT_MSWI_OFFSET) 27 | #define K210_ACLINT_MTIMER_ADDR \ 28 | (K210_CLINT_BASE_ADDR + CLINT_MTIMER_OFFSET) 29 | #define K210_PLIC_BASE_ADDR 0x0C000000ULL 30 | #define K210_PLIC_BASE_SIZE (0x200000ULL + (K210_HART_COUNT * 0x1000)) 31 | 32 | /* Registers */ 33 | #define K210_PLL0 0x08 34 | #define K210_CLKSEL0 0x20 35 | #define K210_RESET 0x30 36 | 37 | /* Register bit masks */ 38 | #define K210_RESET_MASK 0x01 39 | 40 | static inline u32 k210_read_sysreg(u32 reg) 41 | { 42 | return readl((volatile void *)(K210_SYSCTL_BASE_ADDR + reg)); 43 | } 44 | 45 | static inline void k210_write_sysreg(u32 val, u32 reg) 46 | { 47 | writel(val, (volatile void *)(K210_SYSCTL_BASE_ADDR + reg)); 48 | } 49 | 50 | #endif /* _K210_PLATFORM_H_ */ 51 | -------------------------------------------------------------------------------- /platform/nuclei/ux600/Kconfig: -------------------------------------------------------------------------------- 1 | # SPDX-License-Identifier: BSD-2-Clause 2 | 3 | config PLATFORM_NUCLEI_UX600 4 | bool 5 | select FDT 6 | select IPI_MSWI 7 | select IRQCHIP_PLIC 8 | select SERIAL_SIFIVE 9 | select TIMER_MTIMER 10 | default y 11 | -------------------------------------------------------------------------------- /platform/nuclei/ux600/configs/defconfig: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/riscv-software-src/opensbi/69a0f0245f39ea3af4685cab4cb2dda90acd17cd/platform/nuclei/ux600/configs/defconfig -------------------------------------------------------------------------------- /platform/nuclei/ux600/objects.mk: -------------------------------------------------------------------------------- 1 | # 2 | # SPDX-License-Identifier: BSD-2-Clause 3 | # 4 | # Copyright (c) 2020 Nuclei Corporation or its affiliates. 5 | # 6 | # Authors: 7 | # lujun 8 | # hqfang <578567190@qq.com> 9 | # 10 | 11 | # Compiler flags 12 | platform-cppflags-y = 13 | platform-cflags-y = 14 | platform-asflags-y = 15 | platform-ldflags-y = 16 | 17 | # Command for platform specific "make run" 18 | platform-runcmd = xl_spike \ 19 | $(build_dir)/platform/nuclei/ux600/firmware/fw_payload.elf 20 | 21 | # Objects to build 22 | platform-objs-y += platform.o 23 | 24 | # Blobs to build 25 | FW_DYNAMIC=y 26 | FW_JUMP=y 27 | 28 | FW_JUMP_ADDR=0xA0200000 29 | FW_JUMP_FDT_ADDR=0xA8000000 30 | FW_PAYLOAD=y 31 | FW_PAYLOAD_OFFSET=0x200000 32 | FW_PAYLOAD_FDT_ADDR=0xA8000000 33 | -------------------------------------------------------------------------------- /platform/template/Kconfig: -------------------------------------------------------------------------------- 1 | # SPDX-License-Identifier: BSD-2-Clause 2 | 3 | # 4 | # All mandatory drivers or libraries for this platform should 5 | # be directly selected by the PLATFORM_xyz kconfig symbol. 6 | # 7 | # All optional drivers or libraries for this platform should 8 | # be enabled via configs/defconfig of this platform. 9 | # 10 | config PLATFORM_TEMPLATE 11 | bool 12 | select IPI_MSWI 13 | select IRQCHIP_PLIC 14 | select SERIAL_UART8250 15 | select TIMER_MTIMER 16 | default y 17 | -------------------------------------------------------------------------------- /platform/template/configs/defconfig: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/riscv-software-src/opensbi/69a0f0245f39ea3af4685cab4cb2dda90acd17cd/platform/template/configs/defconfig -------------------------------------------------------------------------------- /scripts/Kconfiglib/LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2011-2019, Ulf Magnusson 2 | 3 | Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. 4 | 5 | THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 6 | -------------------------------------------------------------------------------- /scripts/Kconfiglib/allnoconfig.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | # Copyright (c) 2018-2019, Ulf Magnusson 4 | # SPDX-License-Identifier: ISC 5 | 6 | """ 7 | Writes a configuration file where as many symbols as possible are set to 'n'. 8 | 9 | The default output filename is '.config'. A different filename can be passed 10 | in the KCONFIG_CONFIG environment variable. 11 | 12 | Usage for the Linux kernel: 13 | 14 | $ make [ARCH=] scriptconfig SCRIPT=Kconfiglib/allnoconfig.py 15 | """ 16 | 17 | # See examples/allnoconfig_walk.py for another way to implement this script 18 | 19 | import kconfiglib 20 | 21 | 22 | def main(): 23 | kconf = kconfiglib.standard_kconfig(__doc__) 24 | 25 | # Avoid warnings that would otherwise get printed by Kconfiglib for the 26 | # following: 27 | # 28 | # 1. Assigning a value to a symbol without a prompt, which never has any 29 | # effect 30 | # 31 | # 2. Assigning values invalid for the type (only bool/tristate symbols 32 | # accept 0/1/2, for n/m/y). The assignments will be ignored for other 33 | # symbol types, which is what we want. 34 | kconf.warn = False 35 | for sym in kconf.unique_defined_syms: 36 | sym.set_value(2 if sym.is_allnoconfig_y else 0) 37 | kconf.warn = True 38 | 39 | kconf.load_allconfig("allno.config") 40 | 41 | print(kconf.write_config()) 42 | 43 | 44 | if __name__ == "__main__": 45 | main() 46 | -------------------------------------------------------------------------------- /scripts/Kconfiglib/allyesconfig.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | # Copyright (c) 2018-2019, Ulf Magnusson 4 | # SPDX-License-Identifier: ISC 5 | 6 | """ 7 | Writes a configuration file where as many symbols as possible are set to 'y'. 8 | 9 | The default output filename is '.config'. A different filename can be passed 10 | in the KCONFIG_CONFIG environment variable. 11 | 12 | Usage for the Linux kernel: 13 | 14 | $ make [ARCH=] scriptconfig SCRIPT=Kconfiglib/allyesconfig.py 15 | """ 16 | import kconfiglib 17 | 18 | 19 | def main(): 20 | kconf = kconfiglib.standard_kconfig(__doc__) 21 | 22 | # See allnoconfig.py 23 | kconf.warn = False 24 | 25 | # Try to set all symbols to 'y'. Dependencies might truncate the value down 26 | # later, but this will at least give the highest possible value. 27 | # 28 | # Assigning 0/1/2 to non-bool/tristate symbols has no effect (int/hex 29 | # symbols still take a string, because they preserve formatting). 30 | for sym in kconf.unique_defined_syms: 31 | # Set choice symbols to 'm'. This value will be ignored for choices in 32 | # 'y' mode (the "normal" mode), which will instead just get their 33 | # default selection, but will set all symbols in m-mode choices to 'm', 34 | # which is as high as they can go. 35 | # 36 | # Here's a convoluted example of how you might get an m-mode choice 37 | # even during allyesconfig: 38 | # 39 | # choice 40 | # tristate "weird choice" 41 | # depends on m 42 | sym.set_value(1 if sym.choice else 2) 43 | 44 | # Set all choices to the highest possible mode 45 | for choice in kconf.unique_choices: 46 | choice.set_value(2) 47 | 48 | kconf.warn = True 49 | 50 | kconf.load_allconfig("allyes.config") 51 | 52 | print(kconf.write_config()) 53 | 54 | 55 | if __name__ == "__main__": 56 | main() 57 | -------------------------------------------------------------------------------- /scripts/Kconfiglib/defconfig.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | # Copyright (c) 2019, Ulf Magnusson 4 | # SPDX-License-Identifier: ISC 5 | 6 | """ 7 | Reads a specified configuration file, then writes a new configuration file. 8 | This can be used to initialize the configuration from e.g. an arch-specific 9 | configuration file. This input configuration file would usually be a minimal 10 | configuration file, as generated by e.g. savedefconfig. 11 | 12 | The default output filename is '.config'. A different filename can be passed in 13 | the KCONFIG_CONFIG environment variable. 14 | """ 15 | import argparse 16 | 17 | import kconfiglib 18 | 19 | 20 | def main(): 21 | parser = argparse.ArgumentParser( 22 | formatter_class=argparse.RawDescriptionHelpFormatter, 23 | description=__doc__) 24 | 25 | parser.add_argument( 26 | "--kconfig", 27 | default="Kconfig", 28 | help="Top-level Kconfig file (default: Kconfig)") 29 | 30 | parser.add_argument( 31 | "config", 32 | metavar="CONFIGURATION", 33 | help="Input configuration file") 34 | 35 | args = parser.parse_args() 36 | 37 | kconf = kconfiglib.Kconfig(args.kconfig, suppress_traceback=True) 38 | print(kconf.load_config(args.config)) 39 | print(kconf.write_config()) 40 | 41 | 42 | if __name__ == "__main__": 43 | main() 44 | -------------------------------------------------------------------------------- /scripts/Kconfiglib/olddefconfig.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | # Copyright (c) 2018-2019, Ulf Magnusson 4 | # SPDX-License-Identifier: ISC 5 | 6 | """ 7 | Updates an old .config file or creates a new one, by filling in default values 8 | for all new symbols. This is the same as picking the default selection for all 9 | symbols in oldconfig, or entering the menuconfig interface and immediately 10 | saving. 11 | 12 | The default input/output filename is '.config'. A different filename can be 13 | passed in the KCONFIG_CONFIG environment variable. 14 | 15 | When overwriting a configuration file, the old version is saved to 16 | .old (e.g. .config.old). 17 | """ 18 | import kconfiglib 19 | 20 | 21 | def main(): 22 | kconf = kconfiglib.standard_kconfig(__doc__) 23 | print(kconf.load_config()) 24 | print(kconf.write_config()) 25 | 26 | 27 | if __name__ == "__main__": 28 | main() 29 | -------------------------------------------------------------------------------- /scripts/Kconfiglib/savedefconfig.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | # Copyright (c) 2019, Ulf Magnusson 4 | # SPDX-License-Identifier: ISC 5 | 6 | """ 7 | Saves a minimal configuration file that only lists symbols that differ in value 8 | from their defaults. Loading such a configuration file is equivalent to loading 9 | the "full" configuration file. 10 | 11 | Minimal configuration files are handy to start from when editing configuration 12 | files by hand. 13 | 14 | The default input configuration file is '.config'. A different input filename 15 | can be passed in the KCONFIG_CONFIG environment variable. 16 | 17 | Note: Minimal configurations can also be generated from within the menuconfig 18 | interface. 19 | """ 20 | import argparse 21 | 22 | import kconfiglib 23 | 24 | 25 | def main(): 26 | parser = argparse.ArgumentParser( 27 | formatter_class=argparse.RawDescriptionHelpFormatter, 28 | description=__doc__) 29 | 30 | parser.add_argument( 31 | "--kconfig", 32 | default="Kconfig", 33 | help="Top-level Kconfig file (default: Kconfig)") 34 | 35 | parser.add_argument( 36 | "--out", 37 | metavar="MINIMAL_CONFIGURATION", 38 | default="defconfig", 39 | help="Output filename for minimal configuration (default: defconfig)") 40 | 41 | args = parser.parse_args() 42 | 43 | kconf = kconfiglib.Kconfig(args.kconfig, suppress_traceback=True) 44 | print(kconf.load_config()) 45 | print(kconf.write_min_config(args.out)) 46 | 47 | 48 | if __name__ == "__main__": 49 | main() 50 | -------------------------------------------------------------------------------- /scripts/d2c.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | function usage() 4 | { 5 | echo "Usage:" 6 | echo " $0 [options]" 7 | echo "Options:" 8 | echo " -h Display help or usage" 9 | echo " -i Input binary file path" 10 | echo " -a Output C array alignment" 11 | echo " -p Output C array name prefix" 12 | echo " -t Output padding zero bytes" 13 | exit 1; 14 | } 15 | 16 | # Command line options 17 | INPUT_PATH="" 18 | OUTPUT_C_ALIGN="" 19 | OUTPUT_C_PREFIX="" 20 | NUM_ZERO_BYTES=0 21 | 22 | while getopts "hi:a:p:t:" o; do 23 | case "${o}" in 24 | h) 25 | usage 26 | ;; 27 | i) 28 | INPUT_PATH=${OPTARG} 29 | ;; 30 | a) 31 | OUTPUT_C_ALIGN=${OPTARG} 32 | ;; 33 | p) 34 | OUTPUT_C_PREFIX=${OPTARG} 35 | ;; 36 | t) 37 | NUM_ZERO_BYTES=${OPTARG} 38 | ;; 39 | *) 40 | usage 41 | ;; 42 | esac 43 | done 44 | shift $((OPTIND-1)) 45 | 46 | if [ -z "${INPUT_PATH}" ]; then 47 | echo "Must specify input file path" 48 | usage 49 | fi 50 | 51 | if [ ! -f "${INPUT_PATH}" ]; then 52 | echo "The input path should be a file" 53 | usage 54 | fi 55 | 56 | if [ -z "${OUTPUT_C_ALIGN}" ]; then 57 | echo "Must provide output C array alignment" 58 | usage 59 | fi 60 | 61 | if [ -z "${OUTPUT_C_PREFIX}" ]; then 62 | echo "Must provide output C array name prefix" 63 | usage 64 | fi 65 | 66 | printf "const char __attribute__((aligned(%s))) %s_start[] = {\n" "${OUTPUT_C_ALIGN}" "${OUTPUT_C_PREFIX}" 67 | 68 | od -v -t x1 -An ${INPUT_PATH} | awk '{for (i=1; i<=NF; i++) printf " 0x%s,", $i; printf "\n"; }' 69 | 70 | echo __dummy__ | awk "{for (i=1; i<=${NUM_ZERO_BYTES}; i++) { printf \" 0x00,\"; if (i % 16 == 0) printf \"\n\"; } }" 71 | 72 | printf "};\n" 73 | 74 | printf "const unsigned long %s_size = sizeof(%s_start);\n" "${OUTPUT_C_PREFIX}" "${OUTPUT_C_PREFIX}" 75 | --------------------------------------------------------------------------------