lib: sbi: Implement SBI CPPC extension
[platform/kernel/opensbi.git] / README.md
1 RISC-V Open Source Supervisor Binary Interface (OpenSBI)
2 ========================================================
3
4 Copyright and License
5 ---------------------
6
7 The OpenSBI project is copyright (c) 2019 Western Digital Corporation
8 or its affiliates and other contributors.
9
10 It is distributed under the terms of the BSD 2-clause license
11 ("Simplified BSD License" or "FreeBSD License", SPDX: *BSD-2-Clause*).
12 A copy of this license with OpenSBI copyright can be found in the file
13 [COPYING.BSD].
14
15 All source files in OpenSBI contain the 2-Clause BSD license SPDX short
16 identifier in place of the full license text.
17
18 ```
19 SPDX-License-Identifier:    BSD-2-Clause
20 ```
21
22 This enables machine processing of license information based on the SPDX
23 License Identifiers that are available on the [SPDX] web site.
24
25 OpenSBI source code also contains code reused from other projects as listed
26 below. The original license text of these projects is included in the source
27 files where the reused code is present.
28
29 * The libfdt source code is disjunctively dual licensed
30   (GPL-2.0+ OR BSD-2-Clause). Some of this project code is used in OpenSBI
31   under the terms of the BSD 2-Clause license. Any contributions to this
32   code must be made under the terms of both licenses.
33
34 See also the [third party notices] file for more information.
35
36 Introduction
37 ------------
38
39 The **RISC-V Supervisor Binary Interface (SBI)** is the recommended interface
40 between:
41
42 1. A platform-specific firmware running in M-mode and a bootloader, a
43    hypervisor or a general-purpose OS executing in S-mode or HS-mode.
44 2. A hypervisor running in HS-mode and a bootloader or a general-purpose OS
45    executing in VS-mode.
46
47 The *RISC-V SBI specification* is maintained as an independent project by the
48 RISC-V Foundation on [Github].
49
50 The goal of the OpenSBI project is to provide an open-source reference
51 implementation of the RISC-V SBI specifications for platform-specific firmwares
52 executing in M-mode (case 1 mentioned above). An OpenSBI implementation can be
53 easily extended by RISC-V platform and system-on-chip vendors to fit a
54 particular hardware configuration.
55
56 The main component of OpenSBI is provided in the form of a platform-independent
57 static library **libsbi.a** implementing the SBI interface. A firmware or
58 bootloader implementation can link against this library to ensure conformance
59 with the SBI interface specifications. *libsbi.a* also defines an interface for
60 integrating with platform-specific operations provided by the platform firmware
61 implementation (e.g. console access functions, inter-processor interrupt
62 control, etc).
63
64 To illustrate the use of the *libsbi.a* library, OpenSBI also provides a set of
65 platform-specific support examples. For each example, a platform-specific
66 static library *libplatsbi.a* can be compiled. This library implements
67 SBI call processing by integrating *libsbi.a* with the necessary
68 platform-dependent hardware manipulation functions. For all supported platforms,
69 OpenSBI also provides several runtime firmware examples built using the platform
70 *libplatsbi.a*. These example firmwares can be used to replace the legacy
71 *riscv-pk* bootloader (aka BBL) and enable the use of well-known bootloaders
72 such as [U-Boot].
73
74 Supported SBI version
75 ---------------------
76 Currently, OpenSBI fully supports SBI specification *v0.2*. OpenSBI also
77 supports Hart State Management (HSM) SBI extension starting from OpenSBI v0.7.
78 HSM extension allows S-mode software to boot all the harts a defined order
79 rather than legacy method of random booting of harts. As a result, many
80 required features such as CPU hotplug, kexec/kdump can also be supported easily
81 in S-mode. HSM extension in OpenSBI is implemented in a non-backward compatible
82 manner to reduce the maintenance burden and avoid confusion. That's why, any
83 S-mode software using OpenSBI will not be able to boot more than 1 hart if HSM
84 extension is not supported in S-mode.
85
86 Linux kernel already supports SBI v0.2 and HSM SBI extension starting from
87 **v5.7-rc1**. If you are using an Linux kernel older than **5.7-rc1** or any
88 other S-mode software without HSM SBI extension, you should stick to OpenSBI
89 v0.6 to boot all the harts. For a UMP systems, it doesn't matter.
90
91 N.B. Any S-mode boot loader (i.e. U-Boot) doesn't need to support HSM extension,
92 as it doesn't need to boot all the harts. The operating system should be
93 capable enough to bring up all other non-booting harts using HSM extension.
94
95 Required Toolchain and Packages
96 -------------------------------
97
98 OpenSBI can be compiled natively or cross-compiled on a x86 host. For
99 cross-compilation, you can build your own toolchain, download a prebuilt one
100 from the [Bootlin toolchain repository] or install a distribution-provided
101 toolchain; if you opt to use LLVM/Clang, most distribution toolchains will
102 support cross-compiling for RISC-V using the same toolchain as your native
103 LLVM/Clang toolchain due to LLVM's ability to support multiple backends in the
104 same binary, so is often an easy way to obtain a working cross-compilation
105 toolchain.
106
107 Basically, we prefer toolchains with Position Independent Executable (PIE)
108 support like *riscv64-linux-gnu-gcc*, *riscv64-unknown-freebsd-gcc*, or
109 *Clang/LLVM* as they generate PIE firmware images that can run at arbitrary
110 address with appropriate alignment. If a bare-metal GNU toolchain (e.g.
111 *riscv64-unknown-elf-gcc*) is used, static linked firmware images are
112 generated instead. *Clang/LLVM* can still generate PIE images if a bare-metal
113 triple is used (e.g. *-target riscv64-unknown-elf*).
114
115 Please note that only a 64-bit version of the toolchain is available in
116 the Bootlin toolchain repository for now.
117
118 In addition to a toolchain, OpenSBI also requires the following packages on
119 the host:
120
121 1. device-tree-compiler: The device tree compiler for compiling device
122    tree sources (DTS files).
123 2. python3: The python 3.0 (or compatible) language support for various
124    scripts.
125
126 Building and Installing the OpenSBI Platform-Independent Library
127 ----------------------------------------------------------------
128
129 The OpenSBI platform-independent static library *libsbi.a* can be compiled
130 natively or it can be cross-compiled on a host with a different base
131 architecture than RISC-V.
132
133 For cross-compiling, the environment variable *CROSS_COMPILE* must be defined
134 to specify the name prefix of the RISC-V compiler toolchain executables, e.g.
135 *riscv64-linux-gnu-* if the gcc executable used is *riscv64-linux-gnu-gcc*.
136
137 To build *libsbi.a* simply execute:
138 ```
139 make
140 ```
141
142 All compiled binaries as well as the resulting *libsbi.a* static library file
143 will be placed in the *build/lib* directory. To specify an alternate build root
144 directory path, run:
145 ```
146 make O=<build_directory>
147 ```
148
149 To generate files to be installed for using *libsbi.a* in other projects, run:
150 ```
151 make install
152 ```
153
154 This will create the *install* directory with all necessary include files
155 copied under the *install/include* directory and the library file copied into
156 the *install/lib* directory. To specify an alternate installation root
157 directory path, run:
158 ```
159 make I=<install_directory> install
160 ```
161
162 Building and Installing a Reference Platform Static Library and Firmware
163 ------------------------------------------------------------------------
164
165 When the *PLATFORM=<platform_subdir>* argument is specified on the make command
166 line, the platform-specific static library *libplatsbi.a* and firmware examples
167 are built for the platform *<platform_subdir>* present in the directory
168 *platform* in the OpenSBI top directory. For example, to compile the platform
169 library and the firmware examples for the QEMU RISC-V *virt* machine,
170 *<platform_subdir>* should be *generic*.
171
172 To build *libsbi.a*, *libplatsbi.a* and the firmware for one of the supported
173 platforms, run:
174 ```
175 make PLATFORM=<platform_subdir>
176 ```
177
178 An alternate build directory path can also be specified:
179 ```
180 make PLATFORM=<platform_subdir> O=<build_directory>
181 ```
182
183 The platform-specific library *libplatsbi.a* will be generated in the
184 *build/platform/<platform_subdir>/lib* directory. The platform firmware files
185 will be under the *build/platform/<platform_subdir>/firmware* directory.
186 The compiled firmwares will be available in two different formats: an ELF file
187 and an expanded image file.
188
189 To install *libsbi.a*, *libplatsbi.a*, and the compiled firmwares, run:
190 ```
191 make PLATFORM=<platform_subdir> install
192 ```
193
194 This will copy the compiled platform-specific libraries and firmware files
195 under the *install/platform/<platform_subdir>/* directory. An alternate
196 install root directory path can be specified as follows:
197 ```
198 make PLATFORM=<platform_subdir> I=<install_directory> install
199 ```
200
201 In addition, platform-specific configuration options can be specified with the
202 top-level make command line. These options, such as *PLATFORM_<xyz>* or
203 *FW_<abc>*, are platform-specific and described in more details in the
204 *docs/platform/<platform_name>.md* files and
205 *docs/firmware/<firmware_name>.md* files.
206
207 All OpenSBI platforms support Kconfig style build-time configuration. Users
208 can change the build-time configuration of a platform using a graphical
209 interface as follows:
210 ```
211 make PLATFORM=<platform_subdir> menuconfig
212 ```
213
214 Alternately, an OpenSBI platform can have multiple default configurations
215 and users can select a custom default configuration as follows:
216 ```
217 make PLATFORM=<platform_subdir> PLATFORM_DEFCONFIG=<platform_custom_defconfig>
218 ```
219
220 Building 32-bit / 64-bit OpenSBI Images
221 ---------------------------------------
222 By default, building OpenSBI generates 32-bit or 64-bit images based on the
223 supplied RISC-V cross-compile toolchain. For example if *CROSS_COMPILE* is set
224 to *riscv64-linux-gnu-*, 64-bit OpenSBI images will be generated. If building
225 32-bit OpenSBI images, *CROSS_COMPILE* should be set to a toolchain that is
226 pre-configured to generate 32-bit RISC-V codes, like *riscv32-linux-gnu-*.
227
228 However it's possible to explicitly specify the image bits we want to build with
229 a given RISC-V toolchain. This can be done by setting the environment variable
230 *PLATFORM_RISCV_XLEN* to the desired width, for example:
231
232 ```
233 export CROSS_COMPILE=riscv64-linux-gnu-
234 export PLATFORM_RISCV_XLEN=32
235 ```
236
237 will generate 32-bit OpenSBI images. And vice vesa.
238
239 Building with Clang/LLVM
240 ------------------------
241
242 OpenSBI can also be built with Clang/LLVM. To build with just Clang but keep
243 the default binutils (which will still use the *CROSS_COMPILE* prefix if
244 defined), override the *CC* make variable with:
245 ```
246 make CC=clang
247 ```
248
249 To build with a full LLVM-based toolchain, not just Clang, enable the *LLVM*
250 option with:
251 ```
252 make LLVM=1
253 ```
254
255 When using Clang, *CROSS_COMPILE* often does not need to be defined unless
256 using GNU binutils with prefixed binary names. *PLATFORM_RISCV_XLEN* will be
257 used to infer a default triple to pass to Clang, so if *PLATFORM_RISCV_XLEN*
258 itself defaults to an undesired value then prefer setting that rather than the
259 full triple via *CROSS_COMPILE*. If *CROSS_COMPILE* is nonetheless defined,
260 rather than being used as a prefix for the executable name, it will instead be
261 passed via the `--target` option with the trailing `-` removed, so must be a
262 valid triple.
263
264 These can also be mixed; for example using a GCC cross-compiler but LLVM
265 binutils would be:
266 ```
267 make CC=riscv64-linux-gnu-gcc LLVM=1
268 ```
269
270 These variables must be passed for all the make invocations described in this
271 document.
272
273 NOTE: Using Clang with a `riscv*-linux-gnu` GNU binutils linker has been seen
274 to produce broken binaries with missing relocations; it is therefore currently
275 recommended that this combination be avoided or *FW_PIC=n* be used to disable
276 building OpenSBI as a position-independent binary.
277
278 Building with timestamp and compiler info
279 -----------------------------------------
280
281 When doing development, we may want to know the build time and compiler info
282 for debug purpose. OpenSBI can also be built with timestamp and compiler info.
283 To build with those info and print it out at boot time, we can just simply add
284 `BUILD_INFO=y`, like:
285 ```
286 make BUILD_INFO=y
287 ```
288
289 But if you have used `BUILD_INFO=y`, and want to switch back to `BUILD_INFO=n`,
290 you must do
291 ```
292 make clean
293 ```
294 before the next build.
295
296 NOTE: Using `BUILD_INFO=y` without specifying SOURCE_DATE_EPOCH will violate
297 [reproducible builds]. This definition is ONLY for development and debug
298 purpose, and should NOT be used in a product which follows "reproducible
299 builds".
300
301 Building with optimization off for debugging
302 --------------------------------------------
303
304 When debugging OpenSBI, we may want to turn off the compiler optimization and
305 make debugging produce the expected results for a better debugging experience.
306 To build with optimization off we can just simply add `DEBUG=1`, like:
307 ```
308 make DEBUG=1
309 ```
310
311 This definition is ONLY for development and debug purpose, and should NOT be
312 used in a product build.
313
314 Contributing to OpenSBI
315 -----------------------
316
317 The OpenSBI project encourages and welcomes contributions. Contributions should
318 follow the rules described in the OpenSBI [Contribution Guideline] document.
319 In particular, all patches sent should contain a Signed-off-by tag.
320
321 The [Contributors List] document provides a list of individuals and
322 organizations actively contributing to the OpenSBI project.
323
324 Documentation
325 -------------
326
327 Detailed documentation of various aspects of OpenSBI can be found under the
328 *docs* directory. The documentation covers the following topics.
329
330 * [Contribution Guideline]: Guideline for contributing code to OpenSBI project
331 * [Library Usage]: API documentation of OpenSBI static library *libsbi.a*
332 * [Platform Requirements]: Requirements for using OpenSBI on a platform
333 * [Platform Support Guide]: Guideline for implementing support for new platforms
334 * [Platform Documentation]: Documentation of the platforms currently supported.
335 * [Firmware Documentation]: Documentation for the different types of firmware
336   examples build supported by OpenSBI.
337 * [Domain Support]: Documentation for the OpenSBI domain support which helps
338   users achieve system-level partitioning using OpenSBI.
339
340 OpenSBI source code is also well documented. For source level documentation,
341 doxygen style is used. Please refer to the [Doxygen manual] for details on this
342 format.
343
344 Doxygen can be installed on Linux distributions using *.deb* packages using
345 the following command.
346 ```
347 sudo apt-get install doxygen doxygen-latex doxygen-doc doxygen-gui graphviz
348 ```
349
350 For *.rpm* based Linux distributions, the following commands can be used.
351 ```
352 sudo yum install doxygen doxygen-latex doxywizard graphviz
353 ```
354 or
355 ```
356 sudo yum install doxygen doxygen-latex doxywizard graphviz
357 ```
358
359 To build a consolidated *refman.pdf* of all documentation, run:
360 ```
361 make docs
362 ```
363 or
364 ```
365 make O=<build_directory> docs
366 ```
367
368 the resulting *refman.pdf* will be available under the directory
369 *<build_directory>/docs/latex*. To install this file, run:
370 ```
371 make install_docs
372 ```
373 or
374 ```
375 make I=<install_directory> install_docs
376 ```
377
378 *refman.pdf* will be installed under *<install_directory>/docs*.
379
380 [Github]: https://github.com/riscv/riscv-sbi-doc
381 [U-Boot]: https://www.denx.de/wiki/U-Boot/SourceCode
382 [Bootlin toolchain repository]: https://toolchains.bootlin.com/
383 [COPYING.BSD]: COPYING.BSD
384 [SPDX]: http://spdx.org/licenses/
385 [Contribution Guideline]: docs/contributing.md
386 [Contributors List]: CONTRIBUTORS.md
387 [Library Usage]: docs/library_usage.md
388 [Platform Requirements]: docs/platform_requirements.md
389 [Platform Support Guide]: docs/platform_guide.md
390 [Platform Documentation]: docs/platform/platform.md
391 [Firmware Documentation]: docs/firmware/fw.md
392 [Domain Support]: docs/domain_support.md
393 [Doxygen manual]: http://www.doxygen.nl/manual/index.html
394 [Kendryte standalone SDK]: https://github.com/kendryte/kendryte-standalone-sdk
395 [third party notices]: ThirdPartyNotices.md
396 [reproducible builds]: https://reproducible-builds.org