xref: /linux/arch/riscv/kernel/sbi.c (revision 20bed0d6ff62d985fe8222958825504b8aad4e88)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * SBI initialilization and all extension implementation.
4  *
5  * Copyright (c) 2020 Western Digital Corporation or its affiliates.
6  */
7 
8 #include <linux/bits.h>
9 #include <linux/init.h>
10 #include <linux/mm.h>
11 #include <linux/pm.h>
12 #include <linux/reboot.h>
13 #include <asm/sbi.h>
14 #include <asm/smp.h>
15 #include <asm/tlbflush.h>
16 
17 /* default SBI version is 0.1 */
18 unsigned long sbi_spec_version __ro_after_init = SBI_SPEC_VERSION_DEFAULT;
19 EXPORT_SYMBOL(sbi_spec_version);
20 
21 static void (*__sbi_set_timer)(uint64_t stime) __ro_after_init;
22 static void (*__sbi_send_ipi)(unsigned int cpu) __ro_after_init;
23 static int (*__sbi_rfence)(int fid, const struct cpumask *cpu_mask,
24 			   unsigned long start, unsigned long size,
25 			   unsigned long arg4, unsigned long arg5) __ro_after_init;
26 
27 #ifdef CONFIG_RISCV_SBI_V01
28 static unsigned long __sbi_v01_cpumask_to_hartmask(const struct cpumask *cpu_mask)
29 {
30 	unsigned long cpuid, hartid;
31 	unsigned long hmask = 0;
32 
33 	/*
34 	 * There is no maximum hartid concept in RISC-V and NR_CPUS must not be
35 	 * associated with hartid. As SBI v0.1 is only kept for backward compatibility
36 	 * and will be removed in the future, there is no point in supporting hartid
37 	 * greater than BITS_PER_LONG (32 for RV32 and 64 for RV64). Ideally, SBI v0.2
38 	 * should be used for platforms with hartid greater than BITS_PER_LONG.
39 	 */
40 	for_each_cpu(cpuid, cpu_mask) {
41 		hartid = cpuid_to_hartid_map(cpuid);
42 		if (hartid >= BITS_PER_LONG) {
43 			pr_warn("Unable to send any request to hartid > BITS_PER_LONG for SBI v0.1\n");
44 			break;
45 		}
46 		hmask |= BIT(hartid);
47 	}
48 
49 	return hmask;
50 }
51 
52 /**
53  * sbi_console_putchar() - Writes given character to the console device.
54  * @ch: The data to be written to the console.
55  *
56  * Return: None
57  */
58 void sbi_console_putchar(int ch)
59 {
60 	sbi_ecall(SBI_EXT_0_1_CONSOLE_PUTCHAR, 0, ch);
61 }
62 EXPORT_SYMBOL(sbi_console_putchar);
63 
64 /**
65  * sbi_console_getchar() - Reads a byte from console device.
66  *
67  * Returns the value read from console.
68  */
69 int sbi_console_getchar(void)
70 {
71 	struct sbiret ret;
72 
73 	ret = sbi_ecall(SBI_EXT_0_1_CONSOLE_GETCHAR, 0);
74 
75 	return ret.error;
76 }
77 EXPORT_SYMBOL(sbi_console_getchar);
78 
79 /**
80  * sbi_shutdown() - Remove all the harts from executing supervisor code.
81  *
82  * Return: None
83  */
84 void sbi_shutdown(void)
85 {
86 	sbi_ecall(SBI_EXT_0_1_SHUTDOWN, 0);
87 }
88 EXPORT_SYMBOL(sbi_shutdown);
89 
90 /**
91  * __sbi_set_timer_v01() - Program the timer for next timer event.
92  * @stime_value: The value after which next timer event should fire.
93  *
94  * Return: None
95  */
96 static void __sbi_set_timer_v01(uint64_t stime_value)
97 {
98 #if __riscv_xlen == 32
99 	sbi_ecall(SBI_EXT_0_1_SET_TIMER, 0, stime_value,
100 		  stime_value >> 32);
101 #else
102 	sbi_ecall(SBI_EXT_0_1_SET_TIMER, 0, stime_value);
103 #endif
104 }
105 
106 static void __sbi_send_ipi_v01(unsigned int cpu)
107 {
108 	unsigned long hart_mask =
109 		__sbi_v01_cpumask_to_hartmask(cpumask_of(cpu));
110 	sbi_ecall(SBI_EXT_0_1_SEND_IPI, 0, (unsigned long)(&hart_mask));
111 }
112 
113 static int __sbi_rfence_v01(int fid, const struct cpumask *cpu_mask,
114 			    unsigned long start, unsigned long size,
115 			    unsigned long arg4, unsigned long arg5)
116 {
117 	int result = 0;
118 	unsigned long hart_mask;
119 
120 	if (!cpu_mask || cpumask_empty(cpu_mask))
121 		cpu_mask = cpu_online_mask;
122 	hart_mask = __sbi_v01_cpumask_to_hartmask(cpu_mask);
123 
124 	/* v0.2 function IDs are equivalent to v0.1 extension IDs */
125 	switch (fid) {
126 	case SBI_EXT_RFENCE_REMOTE_FENCE_I:
127 		sbi_ecall(SBI_EXT_0_1_REMOTE_FENCE_I, 0,
128 			  (unsigned long)&hart_mask);
129 		break;
130 	case SBI_EXT_RFENCE_REMOTE_SFENCE_VMA:
131 		sbi_ecall(SBI_EXT_0_1_REMOTE_SFENCE_VMA, 0,
132 			  (unsigned long)&hart_mask, start, size);
133 		break;
134 	case SBI_EXT_RFENCE_REMOTE_SFENCE_VMA_ASID:
135 		sbi_ecall(SBI_EXT_0_1_REMOTE_SFENCE_VMA_ASID, 0,
136 			  (unsigned long)&hart_mask, start, size,
137 			  arg4);
138 		break;
139 	default:
140 		pr_err("SBI call [%d]not supported in SBI v0.1\n", fid);
141 		result = -EINVAL;
142 	}
143 
144 	return result;
145 }
146 
147 static void sbi_set_power_off(void)
148 {
149 	register_platform_power_off(sbi_shutdown);
150 }
151 #else
152 static void __sbi_set_timer_v01(uint64_t stime_value)
153 {
154 	pr_warn("Timer extension is not available in SBI v%lu.%lu\n",
155 		sbi_major_version(), sbi_minor_version());
156 }
157 
158 static void __sbi_send_ipi_v01(unsigned int cpu)
159 {
160 	pr_warn("IPI extension is not available in SBI v%lu.%lu\n",
161 		sbi_major_version(), sbi_minor_version());
162 }
163 
164 static int __sbi_rfence_v01(int fid, const struct cpumask *cpu_mask,
165 			    unsigned long start, unsigned long size,
166 			    unsigned long arg4, unsigned long arg5)
167 {
168 	pr_warn("remote fence extension is not available in SBI v%lu.%lu\n",
169 		sbi_major_version(), sbi_minor_version());
170 
171 	return 0;
172 }
173 
174 static void sbi_set_power_off(void) {}
175 #endif /* CONFIG_RISCV_SBI_V01 */
176 
177 static void __sbi_set_timer_v02(uint64_t stime_value)
178 {
179 #if __riscv_xlen == 32
180 	sbi_ecall(SBI_EXT_TIME, SBI_EXT_TIME_SET_TIMER, stime_value,
181 		  stime_value >> 32);
182 #else
183 	sbi_ecall(SBI_EXT_TIME, SBI_EXT_TIME_SET_TIMER, stime_value);
184 #endif
185 }
186 
187 static void __sbi_send_ipi_v02(unsigned int cpu)
188 {
189 	int result;
190 	struct sbiret ret = {0};
191 
192 	ret = sbi_ecall(SBI_EXT_IPI, SBI_EXT_IPI_SEND_IPI,
193 			1UL, cpuid_to_hartid_map(cpu));
194 	if (ret.error) {
195 		result = sbi_err_map_linux_errno(ret.error);
196 		pr_err("%s: hbase = [%lu] failed (error [%d])\n",
197 			__func__, cpuid_to_hartid_map(cpu), result);
198 	}
199 }
200 
201 static int __sbi_rfence_v02_call(unsigned long fid, unsigned long hmask,
202 				 unsigned long hbase, unsigned long start,
203 				 unsigned long size, unsigned long arg4,
204 				 unsigned long arg5)
205 {
206 	struct sbiret ret = {0};
207 	int ext = SBI_EXT_RFENCE;
208 	int result = 0;
209 
210 	switch (fid) {
211 	case SBI_EXT_RFENCE_REMOTE_FENCE_I:
212 		ret = sbi_ecall(ext, fid, hmask, hbase);
213 		break;
214 	case SBI_EXT_RFENCE_REMOTE_SFENCE_VMA:
215 		ret = sbi_ecall(ext, fid, hmask, hbase, start,
216 				size);
217 		break;
218 	case SBI_EXT_RFENCE_REMOTE_SFENCE_VMA_ASID:
219 		ret = sbi_ecall(ext, fid, hmask, hbase, start,
220 				size, arg4);
221 		break;
222 
223 	case SBI_EXT_RFENCE_REMOTE_HFENCE_GVMA:
224 		ret = sbi_ecall(ext, fid, hmask, hbase, start,
225 				size);
226 		break;
227 	case SBI_EXT_RFENCE_REMOTE_HFENCE_GVMA_VMID:
228 		ret = sbi_ecall(ext, fid, hmask, hbase, start,
229 				size, arg4);
230 		break;
231 	case SBI_EXT_RFENCE_REMOTE_HFENCE_VVMA:
232 		ret = sbi_ecall(ext, fid, hmask, hbase, start,
233 				size);
234 		break;
235 	case SBI_EXT_RFENCE_REMOTE_HFENCE_VVMA_ASID:
236 		ret = sbi_ecall(ext, fid, hmask, hbase, start,
237 				size, arg4);
238 		break;
239 	default:
240 		pr_err("unknown function ID [%lu] for SBI extension [%d]\n",
241 		       fid, ext);
242 		result = -EINVAL;
243 	}
244 
245 	if (ret.error) {
246 		result = sbi_err_map_linux_errno(ret.error);
247 		pr_err("%s: hbase = [%lu] hmask = [0x%lx] failed (error [%d])\n",
248 		       __func__, hbase, hmask, result);
249 	}
250 
251 	return result;
252 }
253 
254 static int __sbi_rfence_v02(int fid, const struct cpumask *cpu_mask,
255 			    unsigned long start, unsigned long size,
256 			    unsigned long arg4, unsigned long arg5)
257 {
258 	unsigned long hartid, cpuid, hmask = 0, hbase = 0, htop = 0;
259 	int result;
260 
261 	if (!cpu_mask || cpumask_empty(cpu_mask))
262 		cpu_mask = cpu_online_mask;
263 
264 	for_each_cpu(cpuid, cpu_mask) {
265 		hartid = cpuid_to_hartid_map(cpuid);
266 		if (hmask) {
267 			if (hartid + BITS_PER_LONG <= htop ||
268 			    hbase + BITS_PER_LONG <= hartid) {
269 				result = __sbi_rfence_v02_call(fid, hmask,
270 						hbase, start, size, arg4, arg5);
271 				if (result)
272 					return result;
273 				hmask = 0;
274 			} else if (hartid < hbase) {
275 				/* shift the mask to fit lower hartid */
276 				hmask <<= hbase - hartid;
277 				hbase = hartid;
278 			}
279 		}
280 		if (!hmask) {
281 			hbase = hartid;
282 			htop = hartid;
283 		} else if (hartid > htop) {
284 			htop = hartid;
285 		}
286 		hmask |= BIT(hartid - hbase);
287 	}
288 
289 	if (hmask) {
290 		result = __sbi_rfence_v02_call(fid, hmask, hbase,
291 					       start, size, arg4, arg5);
292 		if (result)
293 			return result;
294 	}
295 
296 	return 0;
297 }
298 
299 static bool sbi_fwft_supported;
300 
301 struct fwft_set_req {
302 	u32 feature;
303 	unsigned long value;
304 	unsigned long flags;
305 	atomic_t error;
306 };
307 
308 static void cpu_sbi_fwft_set(void *arg)
309 {
310 	struct fwft_set_req *req = arg;
311 	int ret;
312 
313 	ret = sbi_fwft_set(req->feature, req->value, req->flags);
314 	if (ret)
315 		atomic_set(&req->error, ret);
316 }
317 
318 /**
319  * sbi_fwft_set() - Set a feature on the local hart
320  * @feature: The feature ID to be set
321  * @value: The feature value to be set
322  * @flags: FWFT feature set flags
323  *
324  * Return: 0 on success, appropriate linux error code otherwise.
325  */
326 int sbi_fwft_set(u32 feature, unsigned long value, unsigned long flags)
327 {
328 	struct sbiret ret;
329 
330 	if (!sbi_fwft_supported)
331 		return -EOPNOTSUPP;
332 
333 	ret = sbi_ecall(SBI_EXT_FWFT, SBI_EXT_FWFT_SET,
334 			feature, value, flags);
335 
336 	return sbi_err_map_linux_errno(ret.error);
337 }
338 
339 /**
340  * sbi_fwft_set_cpumask() - Set a feature for the specified cpumask
341  * @mask: CPU mask of cpus that need the feature to be set
342  * @feature: The feature ID to be set
343  * @value: The feature value to be set
344  * @flags: FWFT feature set flags
345  *
346  * Return: 0 on success, appropriate linux error code otherwise.
347  */
348 int sbi_fwft_set_cpumask(const cpumask_t *mask, u32 feature,
349 			       unsigned long value, unsigned long flags)
350 {
351 	struct fwft_set_req req = {
352 		.feature = feature,
353 		.value = value,
354 		.flags = flags,
355 		.error = ATOMIC_INIT(0),
356 	};
357 
358 	if (!sbi_fwft_supported)
359 		return -EOPNOTSUPP;
360 
361 	if (feature & SBI_FWFT_GLOBAL_FEATURE_BIT)
362 		return -EINVAL;
363 
364 	on_each_cpu_mask(mask, cpu_sbi_fwft_set, &req, 1);
365 
366 	return atomic_read(&req.error);
367 }
368 
369 /**
370  * sbi_set_timer() - Program the timer for next timer event.
371  * @stime_value: The value after which next timer event should fire.
372  *
373  * Return: None.
374  */
375 void sbi_set_timer(uint64_t stime_value)
376 {
377 	__sbi_set_timer(stime_value);
378 }
379 
380 /**
381  * sbi_send_ipi() - Send an IPI to any hart.
382  * @cpu: Logical id of the target CPU.
383  */
384 void sbi_send_ipi(unsigned int cpu)
385 {
386 	__sbi_send_ipi(cpu);
387 }
388 EXPORT_SYMBOL(sbi_send_ipi);
389 
390 /**
391  * sbi_remote_fence_i() - Execute FENCE.I instruction on given remote harts.
392  * @cpu_mask: A cpu mask containing all the target harts.
393  *
394  * Return: 0 on success, appropriate linux error code otherwise.
395  */
396 int sbi_remote_fence_i(const struct cpumask *cpu_mask)
397 {
398 	return __sbi_rfence(SBI_EXT_RFENCE_REMOTE_FENCE_I,
399 			    cpu_mask, 0, 0, 0, 0);
400 }
401 EXPORT_SYMBOL(sbi_remote_fence_i);
402 
403 /**
404  * sbi_remote_sfence_vma_asid() - Execute SFENCE.VMA instructions on given
405  * remote harts for a virtual address range belonging to a specific ASID or not.
406  *
407  * @cpu_mask: A cpu mask containing all the target harts.
408  * @start: Start of the virtual address
409  * @size: Total size of the virtual address range.
410  * @asid: The value of address space identifier (ASID), or FLUSH_TLB_NO_ASID
411  * for flushing all address spaces.
412  *
413  * Return: 0 on success, appropriate linux error code otherwise.
414  */
415 int sbi_remote_sfence_vma_asid(const struct cpumask *cpu_mask,
416 				unsigned long start,
417 				unsigned long size,
418 				unsigned long asid)
419 {
420 	if (asid == FLUSH_TLB_NO_ASID)
421 		return __sbi_rfence(SBI_EXT_RFENCE_REMOTE_SFENCE_VMA,
422 				    cpu_mask, start, size, 0, 0);
423 	else
424 		return __sbi_rfence(SBI_EXT_RFENCE_REMOTE_SFENCE_VMA_ASID,
425 				    cpu_mask, start, size, asid, 0);
426 }
427 EXPORT_SYMBOL(sbi_remote_sfence_vma_asid);
428 
429 /**
430  * sbi_remote_hfence_gvma() - Execute HFENCE.GVMA instructions on given remote
431  *			   harts for the specified guest physical address range.
432  * @cpu_mask: A cpu mask containing all the target harts.
433  * @start: Start of the guest physical address
434  * @size: Total size of the guest physical address range.
435  *
436  * Return: None
437  */
438 int sbi_remote_hfence_gvma(const struct cpumask *cpu_mask,
439 			   unsigned long start,
440 			   unsigned long size)
441 {
442 	return __sbi_rfence(SBI_EXT_RFENCE_REMOTE_HFENCE_GVMA,
443 			    cpu_mask, start, size, 0, 0);
444 }
445 EXPORT_SYMBOL_GPL(sbi_remote_hfence_gvma);
446 
447 /**
448  * sbi_remote_hfence_gvma_vmid() - Execute HFENCE.GVMA instructions on given
449  * remote harts for a guest physical address range belonging to a specific VMID.
450  *
451  * @cpu_mask: A cpu mask containing all the target harts.
452  * @start: Start of the guest physical address
453  * @size: Total size of the guest physical address range.
454  * @vmid: The value of guest ID (VMID).
455  *
456  * Return: 0 if success, Error otherwise.
457  */
458 int sbi_remote_hfence_gvma_vmid(const struct cpumask *cpu_mask,
459 				unsigned long start,
460 				unsigned long size,
461 				unsigned long vmid)
462 {
463 	return __sbi_rfence(SBI_EXT_RFENCE_REMOTE_HFENCE_GVMA_VMID,
464 			    cpu_mask, start, size, vmid, 0);
465 }
466 EXPORT_SYMBOL(sbi_remote_hfence_gvma_vmid);
467 
468 /**
469  * sbi_remote_hfence_vvma() - Execute HFENCE.VVMA instructions on given remote
470  *			     harts for the current guest virtual address range.
471  * @cpu_mask: A cpu mask containing all the target harts.
472  * @start: Start of the current guest virtual address
473  * @size: Total size of the current guest virtual address range.
474  *
475  * Return: None
476  */
477 int sbi_remote_hfence_vvma(const struct cpumask *cpu_mask,
478 			   unsigned long start,
479 			   unsigned long size)
480 {
481 	return __sbi_rfence(SBI_EXT_RFENCE_REMOTE_HFENCE_VVMA,
482 			    cpu_mask, start, size, 0, 0);
483 }
484 EXPORT_SYMBOL(sbi_remote_hfence_vvma);
485 
486 /**
487  * sbi_remote_hfence_vvma_asid() - Execute HFENCE.VVMA instructions on given
488  * remote harts for current guest virtual address range belonging to a specific
489  * ASID.
490  *
491  * @cpu_mask: A cpu mask containing all the target harts.
492  * @start: Start of the current guest virtual address
493  * @size: Total size of the current guest virtual address range.
494  * @asid: The value of address space identifier (ASID).
495  *
496  * Return: None
497  */
498 int sbi_remote_hfence_vvma_asid(const struct cpumask *cpu_mask,
499 				unsigned long start,
500 				unsigned long size,
501 				unsigned long asid)
502 {
503 	return __sbi_rfence(SBI_EXT_RFENCE_REMOTE_HFENCE_VVMA_ASID,
504 			    cpu_mask, start, size, asid, 0);
505 }
506 EXPORT_SYMBOL(sbi_remote_hfence_vvma_asid);
507 
508 static void sbi_srst_reset(unsigned long type, unsigned long reason)
509 {
510 	sbi_ecall(SBI_EXT_SRST, SBI_EXT_SRST_RESET, type, reason);
511 	pr_warn("%s: type=0x%lx reason=0x%lx failed\n",
512 		__func__, type, reason);
513 }
514 
515 static int sbi_srst_reboot(struct notifier_block *this,
516 			   unsigned long mode, void *cmd)
517 {
518 	sbi_srst_reset((mode == REBOOT_WARM || mode == REBOOT_SOFT) ?
519 		       SBI_SRST_RESET_TYPE_WARM_REBOOT :
520 		       SBI_SRST_RESET_TYPE_COLD_REBOOT,
521 		       SBI_SRST_RESET_REASON_NONE);
522 	return NOTIFY_DONE;
523 }
524 
525 static struct notifier_block sbi_srst_reboot_nb;
526 
527 static void sbi_srst_power_off(void)
528 {
529 	sbi_srst_reset(SBI_SRST_RESET_TYPE_SHUTDOWN,
530 		       SBI_SRST_RESET_REASON_NONE);
531 }
532 
533 /**
534  * sbi_probe_extension() - Check if an SBI extension ID is supported or not.
535  * @extid: The extension ID to be probed.
536  *
537  * Return: 1 or an extension specific nonzero value if yes, 0 otherwise.
538  */
539 long sbi_probe_extension(int extid)
540 {
541 	struct sbiret ret;
542 
543 	ret = sbi_ecall(SBI_EXT_BASE, SBI_EXT_BASE_PROBE_EXT, extid);
544 	if (!ret.error)
545 		return ret.value;
546 
547 	return 0;
548 }
549 EXPORT_SYMBOL(sbi_probe_extension);
550 
551 static inline long sbi_get_spec_version(void)
552 {
553 	return __sbi_base_ecall(SBI_EXT_BASE_GET_SPEC_VERSION);
554 }
555 
556 static inline long sbi_get_firmware_id(void)
557 {
558 	return __sbi_base_ecall(SBI_EXT_BASE_GET_IMP_ID);
559 }
560 
561 static inline long sbi_get_firmware_version(void)
562 {
563 	return __sbi_base_ecall(SBI_EXT_BASE_GET_IMP_VERSION);
564 }
565 
566 long sbi_get_mvendorid(void)
567 {
568 	return __sbi_base_ecall(SBI_EXT_BASE_GET_MVENDORID);
569 }
570 EXPORT_SYMBOL_GPL(sbi_get_mvendorid);
571 
572 long sbi_get_marchid(void)
573 {
574 	return __sbi_base_ecall(SBI_EXT_BASE_GET_MARCHID);
575 }
576 EXPORT_SYMBOL_GPL(sbi_get_marchid);
577 
578 long sbi_get_mimpid(void)
579 {
580 	return __sbi_base_ecall(SBI_EXT_BASE_GET_MIMPID);
581 }
582 EXPORT_SYMBOL_GPL(sbi_get_mimpid);
583 
584 bool sbi_debug_console_available;
585 
586 int sbi_debug_console_write(const char *bytes, unsigned int num_bytes)
587 {
588 	phys_addr_t base_addr;
589 	struct sbiret ret;
590 
591 	if (!sbi_debug_console_available)
592 		return -EOPNOTSUPP;
593 
594 	if (is_vmalloc_addr(bytes))
595 		base_addr = page_to_phys(vmalloc_to_page(bytes)) +
596 			    offset_in_page(bytes);
597 	else
598 		base_addr = __pa(bytes);
599 	if (PAGE_SIZE < (offset_in_page(bytes) + num_bytes))
600 		num_bytes = PAGE_SIZE - offset_in_page(bytes);
601 
602 	if (IS_ENABLED(CONFIG_32BIT))
603 		ret = sbi_ecall(SBI_EXT_DBCN, SBI_EXT_DBCN_CONSOLE_WRITE,
604 				num_bytes, lower_32_bits(base_addr),
605 				upper_32_bits(base_addr));
606 	else
607 		ret = sbi_ecall(SBI_EXT_DBCN, SBI_EXT_DBCN_CONSOLE_WRITE,
608 				num_bytes, base_addr, 0);
609 
610 	if (ret.error == SBI_ERR_FAILURE)
611 		return -EIO;
612 	return ret.error ? sbi_err_map_linux_errno(ret.error) : ret.value;
613 }
614 
615 int sbi_debug_console_read(char *bytes, unsigned int num_bytes)
616 {
617 	phys_addr_t base_addr;
618 	struct sbiret ret;
619 
620 	if (!sbi_debug_console_available)
621 		return -EOPNOTSUPP;
622 
623 	if (is_vmalloc_addr(bytes))
624 		base_addr = page_to_phys(vmalloc_to_page(bytes)) +
625 			    offset_in_page(bytes);
626 	else
627 		base_addr = __pa(bytes);
628 	if (PAGE_SIZE < (offset_in_page(bytes) + num_bytes))
629 		num_bytes = PAGE_SIZE - offset_in_page(bytes);
630 
631 	if (IS_ENABLED(CONFIG_32BIT))
632 		ret = sbi_ecall(SBI_EXT_DBCN, SBI_EXT_DBCN_CONSOLE_READ,
633 				num_bytes, lower_32_bits(base_addr),
634 				upper_32_bits(base_addr));
635 	else
636 		ret = sbi_ecall(SBI_EXT_DBCN, SBI_EXT_DBCN_CONSOLE_READ,
637 				num_bytes, base_addr, 0);
638 
639 	if (ret.error == SBI_ERR_FAILURE)
640 		return -EIO;
641 	return ret.error ? sbi_err_map_linux_errno(ret.error) : ret.value;
642 }
643 
644 void __init sbi_init(void)
645 {
646 	bool srst_power_off = false;
647 	int ret;
648 
649 	ret = sbi_get_spec_version();
650 	if (ret > 0)
651 		sbi_spec_version = ret;
652 
653 	pr_info("SBI specification v%lu.%lu detected\n",
654 		sbi_major_version(), sbi_minor_version());
655 
656 	if (!sbi_spec_is_0_1()) {
657 		pr_info("SBI implementation ID=0x%lx Version=0x%lx\n",
658 			sbi_get_firmware_id(), sbi_get_firmware_version());
659 		if (sbi_probe_extension(SBI_EXT_TIME)) {
660 			__sbi_set_timer = __sbi_set_timer_v02;
661 			pr_info("SBI TIME extension detected\n");
662 		} else {
663 			__sbi_set_timer = __sbi_set_timer_v01;
664 		}
665 		if (sbi_probe_extension(SBI_EXT_IPI)) {
666 			__sbi_send_ipi	= __sbi_send_ipi_v02;
667 			pr_info("SBI IPI extension detected\n");
668 		} else {
669 			__sbi_send_ipi	= __sbi_send_ipi_v01;
670 		}
671 		if (sbi_probe_extension(SBI_EXT_RFENCE)) {
672 			__sbi_rfence	= __sbi_rfence_v02;
673 			pr_info("SBI RFENCE extension detected\n");
674 		} else {
675 			__sbi_rfence	= __sbi_rfence_v01;
676 		}
677 		if (sbi_spec_version >= sbi_mk_version(0, 3) &&
678 		    sbi_probe_extension(SBI_EXT_SRST)) {
679 			pr_info("SBI SRST extension detected\n");
680 			register_platform_power_off(sbi_srst_power_off);
681 			srst_power_off = true;
682 			sbi_srst_reboot_nb.notifier_call = sbi_srst_reboot;
683 			sbi_srst_reboot_nb.priority = 192;
684 			register_restart_handler(&sbi_srst_reboot_nb);
685 		}
686 		if (sbi_spec_version >= sbi_mk_version(2, 0) &&
687 		    sbi_probe_extension(SBI_EXT_DBCN) > 0) {
688 			pr_info("SBI DBCN extension detected\n");
689 			sbi_debug_console_available = true;
690 		}
691 		if (sbi_spec_version >= sbi_mk_version(3, 0) &&
692 		    sbi_probe_extension(SBI_EXT_FWFT)) {
693 			pr_info("SBI FWFT extension detected\n");
694 			sbi_fwft_supported = true;
695 		}
696 	} else {
697 		__sbi_set_timer = __sbi_set_timer_v01;
698 		__sbi_send_ipi	= __sbi_send_ipi_v01;
699 		__sbi_rfence	= __sbi_rfence_v01;
700 	}
701 
702 	if (!srst_power_off)
703 		sbi_set_power_off();
704 }
705