]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/riscv/riscv/sbi.c
Update Subversion to 1.14.0 LTS. See contrib/subversion/CHANGES for a
[FreeBSD/FreeBSD.git] / sys / riscv / riscv / sbi.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2019 Mitchell Horne <mhorne@FreeBSD.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/types.h>
34
35 #include <machine/md_var.h>
36 #include <machine/sbi.h>
37
38 /* SBI Implementation-Specific Definitions */
39 #define OPENSBI_VERSION_MAJOR_OFFSET    16
40 #define OPENSBI_VERSION_MINOR_MASK      0xFFFF
41
42 u_long sbi_spec_version;
43 u_long sbi_impl_id;
44 u_long sbi_impl_version;
45
46 static struct sbi_ret
47 sbi_get_spec_version(void)
48 {
49         return (SBI_CALL0(SBI_EXT_ID_BASE, SBI_BASE_GET_SPEC_VERSION));
50 }
51
52 static struct sbi_ret
53 sbi_get_impl_id(void)
54 {
55         return (SBI_CALL0(SBI_EXT_ID_BASE, SBI_BASE_GET_IMPL_ID));
56 }
57
58 static struct sbi_ret
59 sbi_get_impl_version(void)
60 {
61         return (SBI_CALL0(SBI_EXT_ID_BASE, SBI_BASE_GET_IMPL_VERSION));
62 }
63
64 static struct sbi_ret
65 sbi_get_mvendorid(void)
66 {
67         return (SBI_CALL0(SBI_EXT_ID_BASE, SBI_BASE_GET_MVENDORID));
68 }
69
70
71 static struct sbi_ret
72 sbi_get_marchid(void)
73 {
74         return (SBI_CALL0(SBI_EXT_ID_BASE, SBI_BASE_GET_MARCHID));
75 }
76
77 static struct sbi_ret
78 sbi_get_mimpid(void)
79 {
80         return (SBI_CALL0(SBI_EXT_ID_BASE, SBI_BASE_GET_MIMPID));
81 }
82
83 void
84 sbi_print_version(void)
85 {
86         u_int major;
87         u_int minor;
88
89         /* For legacy SBI implementations. */
90         if (sbi_spec_version == 0) {
91                 printf("SBI: Unknown (Legacy) Implementation\n");
92                 printf("SBI Specification Version: 0.1\n");
93                 return;
94         }
95
96         switch (sbi_impl_id) {
97         case (SBI_IMPL_ID_BBL):
98                 printf("SBI: Berkely Boot Loader %u\n", sbi_impl_version);
99                 break;
100         case (SBI_IMPL_ID_OPENSBI):
101                 major = sbi_impl_version >> OPENSBI_VERSION_MAJOR_OFFSET;
102                 minor = sbi_impl_version & OPENSBI_VERSION_MINOR_MASK;
103                 printf("SBI: OpenSBI v%u.%u\n", major, minor);
104                 break;
105         default:
106                 printf("SBI: Unrecognized Implementation: %u\n", sbi_impl_id);
107                 break;
108         }
109
110         major = (sbi_spec_version & SBI_SPEC_VERS_MAJOR_MASK) >>
111             SBI_SPEC_VERS_MAJOR_OFFSET;
112         minor = (sbi_spec_version & SBI_SPEC_VERS_MINOR_MASK);
113         printf("SBI Specification Version: %u.%u\n", major, minor);
114 }
115
116 int
117 sbi_hsm_hart_start(u_long hart, u_long start_addr, u_long priv)
118 {
119         struct sbi_ret ret;
120
121         ret = SBI_CALL3(SBI_EXT_ID_HSM, SBI_HSM_HART_START, hart, start_addr, priv);
122         return (ret.error != 0 ? (int)ret.error : 0);
123 }
124
125 void
126 sbi_hsm_hart_stop(void)
127 {
128         (void)SBI_CALL0(SBI_EXT_ID_HSM, SBI_HSM_HART_STOP);
129 }
130
131 int
132 sbi_hsm_hart_status(u_long hart)
133 {
134         struct sbi_ret ret;
135
136         ret = SBI_CALL1(SBI_EXT_ID_HSM, SBI_HSM_HART_STATUS, hart);
137
138         return (ret.error != 0 ? (int)ret.error : (int)ret.value);
139 }
140
141 void
142 sbi_init(void)
143 {
144         struct sbi_ret sret;
145
146         /*
147          * Get the spec version. For legacy SBI implementations this will
148          * return an error, otherwise it is guaranteed to succeed.
149          */
150         sret = sbi_get_spec_version();
151         if (sret.error != 0) {
152                 /* We are running a legacy SBI implementation. */
153                 sbi_spec_version = 0;
154                 return;
155         }
156
157         /* Set the SBI implementation info. */
158         sbi_spec_version = sret.value;
159         sbi_impl_id = sbi_get_impl_id().value;
160         sbi_impl_version = sbi_get_impl_version().value;
161
162         /* Set the hardware implementation info. */
163         mvendorid = sbi_get_mvendorid().value;
164         marchid = sbi_get_marchid().value;
165         mimpid = sbi_get_mimpid().value;
166
167         /*
168          * Probe for legacy extensions. Currently we rely on all of them
169          * to be implemented, but this is not guaranteed by the spec.
170          */
171         KASSERT(sbi_probe_extension(SBI_SET_TIMER) != 0,
172             ("SBI doesn't implement sbi_set_timer()"));
173         KASSERT(sbi_probe_extension(SBI_CONSOLE_PUTCHAR) != 0,
174             ("SBI doesn't implement sbi_console_putchar()"));
175         KASSERT(sbi_probe_extension(SBI_CONSOLE_GETCHAR) != 0,
176             ("SBI doesn't implement sbi_console_getchar()"));
177         KASSERT(sbi_probe_extension(SBI_CLEAR_IPI) != 0,
178             ("SBI doesn't implement sbi_clear_ipi()"));
179         KASSERT(sbi_probe_extension(SBI_SEND_IPI) != 0,
180             ("SBI doesn't implement sbi_send_ipi()"));
181         KASSERT(sbi_probe_extension(SBI_REMOTE_FENCE_I) != 0,
182             ("SBI doesn't implement sbi_remote_fence_i()"));
183         KASSERT(sbi_probe_extension(SBI_REMOTE_SFENCE_VMA) != 0,
184             ("SBI doesn't implement sbi_remote_sfence_vma()"));
185         KASSERT(sbi_probe_extension(SBI_REMOTE_SFENCE_VMA_ASID) != 0,
186             ("SBI doesn't implement sbi_remote_sfence_vma_asid()"));
187         KASSERT(sbi_probe_extension(SBI_SHUTDOWN) != 0,
188             ("SBI doesn't implement sbi_shutdown()"));
189 }