blob: 7d2e49fd3e3be26c4ceccdd7bae85efb1527f158 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Masahiro Yamada573a3812017-04-14 11:10:24 +09002/*
3 * Copyright (C) 2017 Masahiro Yamada <yamada.masahiro@socionext.com>
4 *
5 * Based on drivers/firmware/psci.c from Linux:
6 * Copyright (C) 2015 ARM Limited
Masahiro Yamada573a3812017-04-14 11:10:24 +09007 */
8
9#include <common.h>
Simon Glass09140112020-05-10 11:40:03 -060010#include <command.h>
Simon Glass9d922452017-05-17 17:18:03 -060011#include <dm.h>
Simon Glass36bf4462019-11-14 12:57:42 -070012#include <irq_func.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060013#include <log.h>
Masahiro Yamada573a3812017-04-14 11:10:24 +090014#include <dm/lists.h>
Heinrich Schuchardt81ea0082018-10-18 12:29:40 +020015#include <efi_loader.h>
Simon Glassc05ed002020-05-10 11:40:11 -060016#include <linux/delay.h>
Masahiro Yamadab08c8c42018-03-05 01:20:11 +090017#include <linux/libfdt.h>
Masahiro Yamada573a3812017-04-14 11:10:24 +090018#include <linux/arm-smccc.h>
19#include <linux/errno.h>
Masahiro Yamadaaf4e6d32017-11-29 15:04:40 +090020#include <linux/printk.h>
Masahiro Yamada573a3812017-04-14 11:10:24 +090021#include <linux/psci.h>
22
Heinrich Schuchardt81ea0082018-10-18 12:29:40 +020023#define DRIVER_NAME "psci"
Masahiro Yamada573a3812017-04-14 11:10:24 +090024
Heinrich Schuchardt81ea0082018-10-18 12:29:40 +020025#define PSCI_METHOD_HVC 1
26#define PSCI_METHOD_SMC 2
27
Yann Gautier5cc7df72020-07-17 14:20:15 +020028#if CONFIG_IS_ENABLED(EFI_LOADER)
Heinrich Schuchardt81ea0082018-10-18 12:29:40 +020029int __efi_runtime_data psci_method;
Yann Gautier5cc7df72020-07-17 14:20:15 +020030#else
31int psci_method __attribute__ ((section(".data")));
32#endif
Heinrich Schuchardt81ea0082018-10-18 12:29:40 +020033
34unsigned long __efi_runtime invoke_psci_fn
35 (unsigned long function_id, unsigned long arg0,
36 unsigned long arg1, unsigned long arg2)
Masahiro Yamada573a3812017-04-14 11:10:24 +090037{
38 struct arm_smccc_res res;
39
Heinrich Schuchardt81ea0082018-10-18 12:29:40 +020040 /*
41 * In the __efi_runtime we need to avoid the switch statement. In some
42 * cases the compiler creates lookup tables to implement switch. These
43 * tables are not correctly relocated when SetVirtualAddressMap is
44 * called.
45 */
46 if (psci_method == PSCI_METHOD_SMC)
47 arm_smccc_smc(function_id, arg0, arg1, arg2, 0, 0, 0, 0, &res);
48 else if (psci_method == PSCI_METHOD_HVC)
49 arm_smccc_hvc(function_id, arg0, arg1, arg2, 0, 0, 0, 0, &res);
50 else
51 res.a0 = PSCI_RET_DISABLED;
Masahiro Yamada573a3812017-04-14 11:10:24 +090052 return res.a0;
53}
54
55static int psci_bind(struct udevice *dev)
56{
57 /* No SYSTEM_RESET support for PSCI 0.1 */
Simon Glass911f3ae2017-05-18 20:08:57 -060058 if (device_is_compatible(dev, "arm,psci-0.2") ||
59 device_is_compatible(dev, "arm,psci-1.0")) {
Masahiro Yamada573a3812017-04-14 11:10:24 +090060 int ret;
61
62 /* bind psci-sysreset optionally */
63 ret = device_bind_driver(dev, "psci-sysreset", "psci-sysreset",
64 NULL);
65 if (ret)
Masahiro Yamadaaf4e6d32017-11-29 15:04:40 +090066 pr_debug("PSCI System Reset was not bound.\n");
Masahiro Yamada573a3812017-04-14 11:10:24 +090067 }
68
69 return 0;
70}
71
72static int psci_probe(struct udevice *dev)
73{
Masahiro Yamada573a3812017-04-14 11:10:24 +090074 const char *method;
75
Jon Hunterfb264ef2020-06-18 12:54:38 +010076 method = ofnode_read_string(dev_ofnode(dev), "method");
Masahiro Yamada573a3812017-04-14 11:10:24 +090077 if (!method) {
Masahiro Yamadaaf4e6d32017-11-29 15:04:40 +090078 pr_warn("missing \"method\" property\n");
Masahiro Yamada573a3812017-04-14 11:10:24 +090079 return -ENXIO;
80 }
81
82 if (!strcmp("hvc", method)) {
Heinrich Schuchardt81ea0082018-10-18 12:29:40 +020083 psci_method = PSCI_METHOD_HVC;
Masahiro Yamada573a3812017-04-14 11:10:24 +090084 } else if (!strcmp("smc", method)) {
Heinrich Schuchardt81ea0082018-10-18 12:29:40 +020085 psci_method = PSCI_METHOD_SMC;
Masahiro Yamada573a3812017-04-14 11:10:24 +090086 } else {
Masahiro Yamadaaf4e6d32017-11-29 15:04:40 +090087 pr_warn("invalid \"method\" property: %s\n", method);
Masahiro Yamada573a3812017-04-14 11:10:24 +090088 return -EINVAL;
89 }
90
91 return 0;
92}
93
Heinrich Schuchardt81ea0082018-10-18 12:29:40 +020094/**
95 * void do_psci_probe() - probe PSCI firmware driver
96 *
97 * Ensure that psci_method is initialized.
98 */
99static void __maybe_unused do_psci_probe(void)
100{
101 struct udevice *dev;
102
103 uclass_get_device_by_name(UCLASS_FIRMWARE, DRIVER_NAME, &dev);
104}
105
106#if IS_ENABLED(CONFIG_EFI_LOADER) && IS_ENABLED(CONFIG_PSCI_RESET)
107efi_status_t efi_reset_system_init(void)
108{
109 do_psci_probe();
110 return EFI_SUCCESS;
111}
112
113void __efi_runtime EFIAPI efi_reset_system(enum efi_reset_type reset_type,
114 efi_status_t reset_status,
115 unsigned long data_size,
116 void *reset_data)
117{
118 if (reset_type == EFI_RESET_COLD ||
119 reset_type == EFI_RESET_WARM ||
120 reset_type == EFI_RESET_PLATFORM_SPECIFIC) {
121 invoke_psci_fn(PSCI_0_2_FN_SYSTEM_RESET, 0, 0, 0);
122 } else if (reset_type == EFI_RESET_SHUTDOWN) {
123 invoke_psci_fn(PSCI_0_2_FN_SYSTEM_OFF, 0, 0, 0);
124 }
125 while (1)
126 ;
127}
128#endif /* IS_ENABLED(CONFIG_EFI_LOADER) && IS_ENABLED(CONFIG_PSCI_RESET) */
129
130#ifdef CONFIG_PSCI_RESET
131void reset_misc(void)
132{
133 do_psci_probe();
134 invoke_psci_fn(PSCI_0_2_FN_SYSTEM_RESET, 0, 0, 0);
135}
136#endif /* CONFIG_PSCI_RESET */
137
138#ifdef CONFIG_CMD_POWEROFF
Simon Glass09140112020-05-10 11:40:03 -0600139int do_poweroff(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Heinrich Schuchardt81ea0082018-10-18 12:29:40 +0200140{
141 do_psci_probe();
142
143 puts("poweroff ...\n");
144 udelay(50000); /* wait 50 ms */
145
146 disable_interrupts();
147 invoke_psci_fn(PSCI_0_2_FN_SYSTEM_OFF, 0, 0, 0);
148 enable_interrupts();
149
150 log_err("Power off not supported on this platform\n");
151 return CMD_RET_FAILURE;
152}
153#endif
154
Masahiro Yamada573a3812017-04-14 11:10:24 +0900155static const struct udevice_id psci_of_match[] = {
156 { .compatible = "arm,psci" },
157 { .compatible = "arm,psci-0.2" },
158 { .compatible = "arm,psci-1.0" },
159 {},
160};
161
162U_BOOT_DRIVER(psci) = {
Heinrich Schuchardt81ea0082018-10-18 12:29:40 +0200163 .name = DRIVER_NAME,
Masahiro Yamada573a3812017-04-14 11:10:24 +0900164 .id = UCLASS_FIRMWARE,
165 .of_match = psci_of_match,
166 .bind = psci_bind,
167 .probe = psci_probe,
168};