bpf: Prevent memory disambiguation attack
[linux/fpc-iii.git] / drivers / soc / samsung / pm_domains.c
blobb6a436594a199df1b8193d4dd1de032b53c64f06
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // Exynos Generic power domain support.
4 //
5 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
6 // http://www.samsung.com
7 //
8 // Implementation of Exynos specific power domain control which is used in
9 // conjunction with runtime-pm. Support for both device-tree and non-device-tree
10 // based power domain support is included.
12 #include <linux/io.h>
13 #include <linux/err.h>
14 #include <linux/slab.h>
15 #include <linux/pm_domain.h>
16 #include <linux/clk.h>
17 #include <linux/delay.h>
18 #include <linux/of_address.h>
19 #include <linux/of_platform.h>
20 #include <linux/sched.h>
22 #define MAX_CLK_PER_DOMAIN 4
24 struct exynos_pm_domain_config {
25 /* Value for LOCAL_PWR_CFG and STATUS fields for each domain */
26 u32 local_pwr_cfg;
30 * Exynos specific wrapper around the generic power domain
32 struct exynos_pm_domain {
33 void __iomem *base;
34 bool is_off;
35 struct generic_pm_domain pd;
36 struct clk *oscclk;
37 struct clk *clk[MAX_CLK_PER_DOMAIN];
38 struct clk *pclk[MAX_CLK_PER_DOMAIN];
39 struct clk *asb_clk[MAX_CLK_PER_DOMAIN];
40 u32 local_pwr_cfg;
43 static int exynos_pd_power(struct generic_pm_domain *domain, bool power_on)
45 struct exynos_pm_domain *pd;
46 void __iomem *base;
47 u32 timeout, pwr;
48 char *op;
49 int i;
51 pd = container_of(domain, struct exynos_pm_domain, pd);
52 base = pd->base;
54 for (i = 0; i < MAX_CLK_PER_DOMAIN; i++) {
55 if (IS_ERR(pd->asb_clk[i]))
56 break;
57 clk_prepare_enable(pd->asb_clk[i]);
60 /* Set oscclk before powering off a domain*/
61 if (!power_on) {
62 for (i = 0; i < MAX_CLK_PER_DOMAIN; i++) {
63 if (IS_ERR(pd->clk[i]))
64 break;
65 pd->pclk[i] = clk_get_parent(pd->clk[i]);
66 if (clk_set_parent(pd->clk[i], pd->oscclk))
67 pr_err("%s: error setting oscclk as parent to clock %d\n",
68 domain->name, i);
72 pwr = power_on ? pd->local_pwr_cfg : 0;
73 writel_relaxed(pwr, base);
75 /* Wait max 1ms */
76 timeout = 10;
78 while ((readl_relaxed(base + 0x4) & pd->local_pwr_cfg) != pwr) {
79 if (!timeout) {
80 op = (power_on) ? "enable" : "disable";
81 pr_err("Power domain %s %s failed\n", domain->name, op);
82 return -ETIMEDOUT;
84 timeout--;
85 cpu_relax();
86 usleep_range(80, 100);
89 /* Restore clocks after powering on a domain*/
90 if (power_on) {
91 for (i = 0; i < MAX_CLK_PER_DOMAIN; i++) {
92 if (IS_ERR(pd->clk[i]))
93 break;
95 if (IS_ERR(pd->pclk[i]))
96 continue; /* Skip on first power up */
97 if (clk_set_parent(pd->clk[i], pd->pclk[i]))
98 pr_err("%s: error setting parent to clock%d\n",
99 domain->name, i);
103 for (i = 0; i < MAX_CLK_PER_DOMAIN; i++) {
104 if (IS_ERR(pd->asb_clk[i]))
105 break;
106 clk_disable_unprepare(pd->asb_clk[i]);
109 return 0;
112 static int exynos_pd_power_on(struct generic_pm_domain *domain)
114 return exynos_pd_power(domain, true);
117 static int exynos_pd_power_off(struct generic_pm_domain *domain)
119 return exynos_pd_power(domain, false);
122 static const struct exynos_pm_domain_config exynos4210_cfg __initconst = {
123 .local_pwr_cfg = 0x7,
126 static const struct exynos_pm_domain_config exynos5433_cfg __initconst = {
127 .local_pwr_cfg = 0xf,
130 static const struct of_device_id exynos_pm_domain_of_match[] __initconst = {
132 .compatible = "samsung,exynos4210-pd",
133 .data = &exynos4210_cfg,
134 }, {
135 .compatible = "samsung,exynos5433-pd",
136 .data = &exynos5433_cfg,
138 { },
141 static __init const char *exynos_get_domain_name(struct device_node *node)
143 const char *name;
145 if (of_property_read_string(node, "label", &name) < 0)
146 name = kbasename(node->full_name);
147 return kstrdup_const(name, GFP_KERNEL);
150 static __init int exynos4_pm_init_power_domain(void)
152 struct device_node *np;
153 const struct of_device_id *match;
155 for_each_matching_node_and_match(np, exynos_pm_domain_of_match, &match) {
156 const struct exynos_pm_domain_config *pm_domain_cfg;
157 struct exynos_pm_domain *pd;
158 int on, i;
160 pm_domain_cfg = match->data;
162 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
163 if (!pd) {
164 of_node_put(np);
165 return -ENOMEM;
167 pd->pd.name = exynos_get_domain_name(np);
168 if (!pd->pd.name) {
169 kfree(pd);
170 of_node_put(np);
171 return -ENOMEM;
174 pd->base = of_iomap(np, 0);
175 if (!pd->base) {
176 pr_warn("%s: failed to map memory\n", __func__);
177 kfree_const(pd->pd.name);
178 kfree(pd);
179 continue;
182 pd->pd.power_off = exynos_pd_power_off;
183 pd->pd.power_on = exynos_pd_power_on;
184 pd->local_pwr_cfg = pm_domain_cfg->local_pwr_cfg;
186 for (i = 0; i < MAX_CLK_PER_DOMAIN; i++) {
187 char clk_name[8];
189 snprintf(clk_name, sizeof(clk_name), "asb%d", i);
190 pd->asb_clk[i] = of_clk_get_by_name(np, clk_name);
191 if (IS_ERR(pd->asb_clk[i]))
192 break;
195 pd->oscclk = of_clk_get_by_name(np, "oscclk");
196 if (IS_ERR(pd->oscclk))
197 goto no_clk;
199 for (i = 0; i < MAX_CLK_PER_DOMAIN; i++) {
200 char clk_name[8];
202 snprintf(clk_name, sizeof(clk_name), "clk%d", i);
203 pd->clk[i] = of_clk_get_by_name(np, clk_name);
204 if (IS_ERR(pd->clk[i]))
205 break;
207 * Skip setting parent on first power up.
208 * The parent at this time may not be useful at all.
210 pd->pclk[i] = ERR_PTR(-EINVAL);
213 if (IS_ERR(pd->clk[0]))
214 clk_put(pd->oscclk);
216 no_clk:
217 on = readl_relaxed(pd->base + 0x4) & pd->local_pwr_cfg;
219 pm_genpd_init(&pd->pd, NULL, !on);
220 of_genpd_add_provider_simple(np, &pd->pd);
223 /* Assign the child power domains to their parents */
224 for_each_matching_node(np, exynos_pm_domain_of_match) {
225 struct of_phandle_args child, parent;
227 child.np = np;
228 child.args_count = 0;
230 if (of_parse_phandle_with_args(np, "power-domains",
231 "#power-domain-cells", 0,
232 &parent) != 0)
233 continue;
235 if (of_genpd_add_subdomain(&parent, &child))
236 pr_warn("%pOF failed to add subdomain: %pOF\n",
237 parent.np, child.np);
238 else
239 pr_info("%pOF has as child subdomain: %pOF.\n",
240 parent.np, child.np);
243 return 0;
245 core_initcall(exynos4_pm_init_power_domain);