summaryrefslogtreecommitdiff
path: root/xen/arch/arm/psci.c
blob: 0c90c2305c3425b35878c494065d7013f1836f68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
 * xen/arch/arm/psci.c
 *
 * PSCI host support
 *
 * Andre Przywara <andre.przywara@linaro.org>
 * Copyright (c) 2013 Linaro Limited.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */


#include <xen/types.h>
#include <xen/init.h>
#include <xen/mm.h>
#include <xen/smp.h>
#include <asm/cpufeature.h>
#include <asm/psci.h>
#include <asm/acpi.h>

/*
 * While a 64-bit OS can make calls with SMC32 calling conventions, for
 * some calls it is necessary to use SMC64 to pass or return 64-bit values.
 * For such calls PSCI_0_2_FN_NATIVE(x) will choose the appropriate
 * (native-width) function ID.
 */
#ifdef CONFIG_ARM_64
#define PSCI_0_2_FN_NATIVE(name)    PSCI_0_2_FN64_##name
#else
#define PSCI_0_2_FN_NATIVE(name)    PSCI_0_2_FN32_##name
#endif

uint32_t psci_ver;
uint32_t smccc_ver;

static uint32_t psci_cpu_on_nr;

#define PSCI_RET(res)   ((int32_t)(res).a0)

int call_psci_cpu_on(int cpu)
{
    struct arm_smccc_res res;

    arm_smccc_smc(psci_cpu_on_nr, cpu_logical_map(cpu), __pa(init_secondary),
                  &res);

    return PSCI_RET(res);
}

void call_psci_cpu_off(void)
{
    if ( psci_ver > PSCI_VERSION(0, 1) )
    {
        struct arm_smccc_res res;

        /* If successfull the PSCI cpu_off call doesn't return */
        arm_smccc_smc(PSCI_0_2_FN32_CPU_OFF, &res);
        panic("PSCI cpu off failed for CPU%d err=%d\n", smp_processor_id(),
              PSCI_RET(res));
    }
}

void call_psci_system_off(void)
{
    if ( psci_ver > PSCI_VERSION(0, 1) )
        arm_smccc_smc(PSCI_0_2_FN32_SYSTEM_OFF, NULL);
}

void call_psci_system_reset(void)
{
    if ( psci_ver > PSCI_VERSION(0, 1) )
        arm_smccc_smc(PSCI_0_2_FN32_SYSTEM_RESET, NULL);
}

static int __init psci_features(uint32_t psci_func_id)
{
    struct arm_smccc_res res;

    if ( psci_ver < PSCI_VERSION(1, 0) )
        return PSCI_NOT_SUPPORTED;

    arm_smccc_smc(PSCI_1_0_FN32_PSCI_FEATURES, psci_func_id, &res);

    return PSCI_RET(res);
}

static int __init psci_is_smc_method(const struct dt_device_node *psci)
{
    int ret;
    const char *prop_str;

    ret = dt_property_read_string(psci, "method", &prop_str);
    if ( ret )
    {
        printk("/psci node does not provide a method (%d)\n", ret);
        return -EINVAL;
    }

    /* Since Xen runs in HYP all of the time, it does not make sense to
     * let it call into HYP for PSCI handling, since the handler just
     * won't be there. So bail out with an error if "smc" is not used.
     */
    if ( strcmp(prop_str, "smc") )
    {
        printk("/psci method must be smc, but is: \"%s\"\n", prop_str);
        return -EINVAL;
    }

    return 0;
}

static void __init psci_init_smccc(void)
{
    /* PSCI is using at least SMCCC 1.0 calling convention. */
    smccc_ver = ARM_SMCCC_VERSION_1_0;

    if ( psci_features(ARM_SMCCC_VERSION_FID) != PSCI_NOT_SUPPORTED )
    {
        struct arm_smccc_res res;

        arm_smccc_smc(ARM_SMCCC_VERSION_FID, &res);
        if ( PSCI_RET(res) != ARM_SMCCC_NOT_SUPPORTED )
            smccc_ver = PSCI_RET(res);
    }

    if ( smccc_ver >= SMCCC_VERSION(1, 1) )
        cpus_set_cap(ARM_SMCCC_1_1);

    printk(XENLOG_INFO "Using SMC Calling Convention v%u.%u\n",
           SMCCC_VERSION_MAJOR(smccc_ver), SMCCC_VERSION_MINOR(smccc_ver));
}

static int __init psci_init_0_1(void)
{
    int ret;
    const struct dt_device_node *psci;

    if ( !acpi_disabled )
    {
        printk("PSCI 0.1 is not supported when using ACPI\n");
        return -EINVAL;
    }

    psci = dt_find_compatible_node(NULL, NULL, "arm,psci");
    if ( !psci )
        return -EOPNOTSUPP;

    ret = psci_is_smc_method(psci);
    if ( ret )
        return -EINVAL;

    if ( !dt_property_read_u32(psci, "cpu_on", &psci_cpu_on_nr) )
    {
        printk("/psci node is missing the \"cpu_on\" property\n");
        return -ENOENT;
    }

    psci_ver = PSCI_VERSION(0, 1);

    return 0;
}

static int __init psci_init_0_2(void)
{
    static const struct dt_device_match psci_ids[] __initconst =
    {
        DT_MATCH_COMPATIBLE("arm,psci-0.2"),
        DT_MATCH_COMPATIBLE("arm,psci-1.0"),
        { /* sentinel */ },
    };
    int ret;
    struct arm_smccc_res res;

    if ( acpi_disabled )
    {
        const struct dt_device_node *psci;

        psci = dt_find_matching_node(NULL, psci_ids);
        if ( !psci )
            return -EOPNOTSUPP;

        ret = psci_is_smc_method(psci);
        if ( ret )
            return -EINVAL;
    }
    else
    {
        if ( acpi_psci_hvc_present() ) {
            printk("PSCI conduit must be SMC, but is HVC\n");
            return -EINVAL;
        }
    }

    arm_smccc_smc(PSCI_0_2_FN32_PSCI_VERSION, &res);
    psci_ver = PSCI_RET(res);

    /* For the moment, we only support PSCI 0.2 and PSCI 1.x */
    if ( psci_ver != PSCI_VERSION(0, 2) && PSCI_VERSION_MAJOR(psci_ver) != 1 )
    {
        printk("Error: Unrecognized PSCI version %u.%u\n",
               PSCI_VERSION_MAJOR(psci_ver), PSCI_VERSION_MINOR(psci_ver));
        return -EOPNOTSUPP;
    }

    psci_cpu_on_nr = PSCI_0_2_FN_NATIVE(CPU_ON);

    return 0;
}

int __init psci_init(void)
{
    int ret;

    if ( !acpi_disabled && !acpi_psci_present() )
        return -EOPNOTSUPP;

    ret = psci_init_0_2();
    if ( ret )
        ret = psci_init_0_1();

    if ( ret )
        return ret;

    psci_init_smccc();

    printk(XENLOG_INFO "Using PSCI v%u.%u\n",
           PSCI_VERSION_MAJOR(psci_ver), PSCI_VERSION_MINOR(psci_ver));

    return 0;
}

/*
 * Local variables:
 * mode: C
 * c-file-style: "BSD"
 * c-basic-offset: 4
 * indent-tabs-mode: nil
 * End:
 */