]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/riscv/riscv/sbi.c
Import tzdata 2020c
[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/kernel.h>
33 #include <sys/systm.h>
34 #include <sys/types.h>
35 #include <sys/eventhandler.h>
36 #include <sys/reboot.h>
37
38 #include <machine/md_var.h>
39 #include <machine/sbi.h>
40
41 /* SBI Implementation-Specific Definitions */
42 #define OPENSBI_VERSION_MAJOR_OFFSET    16
43 #define OPENSBI_VERSION_MINOR_MASK      0xFFFF
44
45 u_long sbi_spec_version;
46 u_long sbi_impl_id;
47 u_long sbi_impl_version;
48
49 static struct sbi_ret
50 sbi_get_spec_version(void)
51 {
52         return (SBI_CALL0(SBI_EXT_ID_BASE, SBI_BASE_GET_SPEC_VERSION));
53 }
54
55 static struct sbi_ret
56 sbi_get_impl_id(void)
57 {
58         return (SBI_CALL0(SBI_EXT_ID_BASE, SBI_BASE_GET_IMPL_ID));
59 }
60
61 static struct sbi_ret
62 sbi_get_impl_version(void)
63 {
64         return (SBI_CALL0(SBI_EXT_ID_BASE, SBI_BASE_GET_IMPL_VERSION));
65 }
66
67 static struct sbi_ret
68 sbi_get_mvendorid(void)
69 {
70         return (SBI_CALL0(SBI_EXT_ID_BASE, SBI_BASE_GET_MVENDORID));
71 }
72
73 static struct sbi_ret
74 sbi_get_marchid(void)
75 {
76         return (SBI_CALL0(SBI_EXT_ID_BASE, SBI_BASE_GET_MARCHID));
77 }
78
79 static struct sbi_ret
80 sbi_get_mimpid(void)
81 {
82         return (SBI_CALL0(SBI_EXT_ID_BASE, SBI_BASE_GET_MIMPID));
83 }
84
85 static void
86 sbi_shutdown_final(void *dummy __unused, int howto)
87 {
88         if ((howto & RB_POWEROFF) != 0)
89                 sbi_shutdown();
90 }
91
92 void
93 sbi_print_version(void)
94 {
95         u_int major;
96         u_int minor;
97
98         /* For legacy SBI implementations. */
99         if (sbi_spec_version == 0) {
100                 printf("SBI: Unknown (Legacy) Implementation\n");
101                 printf("SBI Specification Version: 0.1\n");
102                 return;
103         }
104
105         switch (sbi_impl_id) {
106         case (SBI_IMPL_ID_BBL):
107                 printf("SBI: Berkely Boot Loader %lu\n", sbi_impl_version);
108                 break;
109         case (SBI_IMPL_ID_OPENSBI):
110                 major = sbi_impl_version >> OPENSBI_VERSION_MAJOR_OFFSET;
111                 minor = sbi_impl_version & OPENSBI_VERSION_MINOR_MASK;
112                 printf("SBI: OpenSBI v%u.%u\n", major, minor);
113                 break;
114         default:
115                 printf("SBI: Unrecognized Implementation: %lu\n", sbi_impl_id);
116                 break;
117         }
118
119         major = (sbi_spec_version & SBI_SPEC_VERS_MAJOR_MASK) >>
120             SBI_SPEC_VERS_MAJOR_OFFSET;
121         minor = (sbi_spec_version & SBI_SPEC_VERS_MINOR_MASK);
122         printf("SBI Specification Version: %u.%u\n", major, minor);
123 }
124
125 int
126 sbi_hsm_hart_start(u_long hart, u_long start_addr, u_long priv)
127 {
128         struct sbi_ret ret;
129
130         ret = SBI_CALL3(SBI_EXT_ID_HSM, SBI_HSM_HART_START, hart, start_addr, priv);
131         return (ret.error != 0 ? (int)ret.error : 0);
132 }
133
134 void
135 sbi_hsm_hart_stop(void)
136 {
137         (void)SBI_CALL0(SBI_EXT_ID_HSM, SBI_HSM_HART_STOP);
138 }
139
140 int
141 sbi_hsm_hart_status(u_long hart)
142 {
143         struct sbi_ret ret;
144
145         ret = SBI_CALL1(SBI_EXT_ID_HSM, SBI_HSM_HART_STATUS, hart);
146
147         return (ret.error != 0 ? (int)ret.error : (int)ret.value);
148 }
149
150 void
151 sbi_init(void)
152 {
153         struct sbi_ret sret;
154
155         /*
156          * Get the spec version. For legacy SBI implementations this will
157          * return an error, otherwise it is guaranteed to succeed.
158          */
159         sret = sbi_get_spec_version();
160         if (sret.error != 0) {
161                 /* We are running a legacy SBI implementation. */
162                 sbi_spec_version = 0;
163                 return;
164         }
165
166         /* Set the SBI implementation info. */
167         sbi_spec_version = sret.value;
168         sbi_impl_id = sbi_get_impl_id().value;
169         sbi_impl_version = sbi_get_impl_version().value;
170
171         /* Set the hardware implementation info. */
172         mvendorid = sbi_get_mvendorid().value;
173         marchid = sbi_get_marchid().value;
174         mimpid = sbi_get_mimpid().value;
175
176         /*
177          * Probe for legacy extensions. Currently we rely on all of them
178          * to be implemented, but this is not guaranteed by the spec.
179          */
180         KASSERT(sbi_probe_extension(SBI_SET_TIMER) != 0,
181             ("SBI doesn't implement sbi_set_timer()"));
182         KASSERT(sbi_probe_extension(SBI_CONSOLE_PUTCHAR) != 0,
183             ("SBI doesn't implement sbi_console_putchar()"));
184         KASSERT(sbi_probe_extension(SBI_CONSOLE_GETCHAR) != 0,
185             ("SBI doesn't implement sbi_console_getchar()"));
186         KASSERT(sbi_probe_extension(SBI_CLEAR_IPI) != 0,
187             ("SBI doesn't implement sbi_clear_ipi()"));
188         KASSERT(sbi_probe_extension(SBI_SEND_IPI) != 0,
189             ("SBI doesn't implement sbi_send_ipi()"));
190         KASSERT(sbi_probe_extension(SBI_REMOTE_FENCE_I) != 0,
191             ("SBI doesn't implement sbi_remote_fence_i()"));
192         KASSERT(sbi_probe_extension(SBI_REMOTE_SFENCE_VMA) != 0,
193             ("SBI doesn't implement sbi_remote_sfence_vma()"));
194         KASSERT(sbi_probe_extension(SBI_REMOTE_SFENCE_VMA_ASID) != 0,
195             ("SBI doesn't implement sbi_remote_sfence_vma_asid()"));
196         KASSERT(sbi_probe_extension(SBI_SHUTDOWN) != 0,
197             ("SBI doesn't implement sbi_shutdown()"));
198 }
199
200 static void
201 sbi_late_init(void *dummy __unused)
202 {
203         EVENTHANDLER_REGISTER(shutdown_final, sbi_shutdown_final, NULL,
204             SHUTDOWN_PRI_LAST);
205 }
206
207 SYSINIT(sbi, SI_SUB_KLD, SI_ORDER_ANY, sbi_late_init, NULL);