blob: 647ee016a300dab1df01bfaf21bd04b601e5fab4 [file] [log] [blame]
Jens Wiklander52c798e2015-12-07 14:37:10 +01001/*
2 * Copyright (c) 2016, ARM Limited and Contributors. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
9 *
10 * Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 *
14 * Neither the name of ARM nor the names of its contributors may be used
15 * to endorse or promote products derived from this software without specific
16 * prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 */
30#include <console.h>
31#include <debug.h>
32#include <libfdt.h>
33#include <psci.h>
34#include "qemu_private.h"
35#include <string.h>
36
37static int append_psci_compatible(void *fdt, int offs, const char *str)
38{
39 return fdt_appendprop(fdt, offs, "compatible", str, strlen(str) + 1);
40}
41
42int dt_add_psci_node(void *fdt)
43{
44 int offs;
45
46 if (fdt_path_offset(fdt, "/psci") >= 0) {
47 WARN("PSCI Device Tree node already exists!\n");
48 return 0;
49 }
50
51 offs = fdt_path_offset(fdt, "/");
52 if (offs < 0)
53 return -1;
54 offs = fdt_add_subnode(fdt, offs, "psci");
55 if (offs < 0)
56 return -1;
57 if (append_psci_compatible(fdt, offs, "arm,psci-1.0"))
58 return -1;
59 if (append_psci_compatible(fdt, offs, "arm,psci-0.2"))
60 return -1;
61 if (append_psci_compatible(fdt, offs, "arm,psci"))
62 return -1;
63 if (fdt_setprop_string(fdt, offs, "method", "smc"))
64 return -1;
65 if (fdt_setprop_u32(fdt, offs, "cpu_suspend", PSCI_CPU_SUSPEND_AARCH64))
66 return -1;
67 if (fdt_setprop_u32(fdt, offs, "cpu_off", PSCI_CPU_OFF))
68 return -1;
69 if (fdt_setprop_u32(fdt, offs, "cpu_on", PSCI_CPU_ON_AARCH64))
70 return -1;
71 if (fdt_setprop_u32(fdt, offs, "sys_poweroff", PSCI_SYSTEM_OFF))
72 return -1;
73 if (fdt_setprop_u32(fdt, offs, "sys_reset", PSCI_SYSTEM_RESET))
74 return -1;
75 return 0;
76}
77
78static int check_node_compat_prefix(void *fdt, int offs, const char *prefix)
79{
80 const size_t prefix_len = strlen(prefix);
81 size_t l;
82 int plen;
83 const char *prop;
84
85 prop = fdt_getprop(fdt, offs, "compatible", &plen);
86 if (!prop)
87 return -1;
88
89 while (plen > 0) {
90 if (memcmp(prop, prefix, prefix_len) == 0)
91 return 0; /* match */
92
93 l = strlen(prop) + 1;
94 prop += l;
95 plen -= l;
96 }
97
98 return -1;
99}
100
101int dt_add_psci_cpu_enable_methods(void *fdt)
102{
103 int offs = 0;
104
105 while (1) {
106 offs = fdt_next_node(fdt, offs, NULL);
107 if (offs < 0)
108 break;
109 if (fdt_getprop(fdt, offs, "enable-method", NULL))
110 continue; /* already set */
111 if (check_node_compat_prefix(fdt, offs, "arm,cortex-a"))
112 continue; /* no compatible */
113 if (fdt_setprop_string(fdt, offs, "enable-method", "psci"))
114 return -1;
115 /* Need to restart scanning as offsets may have changed */
116 offs = 0;
117 }
118 return 0;
119}