1
2
3
4
5
6
7
8
9
10
11
12
13
14#include <stdarg.h>
15#include <linux/smp.h>
16#include <linux/io.h>
17#include <linux/ioport.h>
18
19#include <asm/cacheflush.h>
20#include <linux/of_address.h>
21
22#include "bcm_kona_smc.h"
23
24static u32 bcm_smc_buffer_phys;
25static void __iomem *bcm_smc_buffer;
26
27struct bcm_kona_smc_data {
28 unsigned service_id;
29 unsigned arg0;
30 unsigned arg1;
31 unsigned arg2;
32 unsigned arg3;
33 unsigned result;
34};
35
36static const struct of_device_id bcm_kona_smc_ids[] __initconst = {
37 {.compatible = "brcm,kona-smc"},
38 {.compatible = "bcm,kona-smc"},
39 {},
40};
41
42
43int __init bcm_kona_smc_init(void)
44{
45 struct device_node *node;
46 const __be32 *prop_val;
47 u64 prop_size = 0;
48 unsigned long buffer_size;
49 u32 buffer_phys;
50
51
52 node = of_find_matching_node(NULL, bcm_kona_smc_ids);
53 if (!node)
54 return -ENODEV;
55
56 prop_val = of_get_address(node, 0, &prop_size, NULL);
57 if (!prop_val)
58 return -EINVAL;
59
60
61 if (prop_size < 4 * sizeof(u32) || prop_size > (u64)ULONG_MAX)
62 return -EINVAL;
63 buffer_size = (unsigned long)prop_size;
64
65 buffer_phys = be32_to_cpup(prop_val);
66 if (!buffer_phys)
67 return -EINVAL;
68
69 bcm_smc_buffer = ioremap(buffer_phys, buffer_size);
70 if (!bcm_smc_buffer)
71 return -ENOMEM;
72 bcm_smc_buffer_phys = buffer_phys;
73
74 pr_info("Kona Secure API initialized\n");
75
76 return 0;
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
109static int bcm_kona_do_smc(u32 service_id, u32 buffer_phys)
110{
111 register u32 ip asm("ip");
112 register u32 r0 asm("r0");
113 register u32 r4 asm("r4");
114 register u32 r5 asm("r5");
115 register u32 r6 asm("r6");
116
117 r4 = service_id;
118 r5 = 0x3;
119 r6 = buffer_phys;
120
121 asm volatile (
122
123 __asmeq("%0", "ip")
124 __asmeq("%1", "r0")
125 __asmeq("%2", "r4")
126 __asmeq("%3", "r5")
127 __asmeq("%4", "r6")
128#ifdef REQUIRES_SEC
129 ".arch_extension sec\n"
130#endif
131 " smc #0\n"
132 : "=r" (ip), "=r" (r0)
133 : "r" (r4), "r" (r5), "r" (r6)
134 : "r1", "r2", "r3", "r7", "lr");
135
136 BUG_ON(ip != SEC_EXIT_NORMAL);
137
138 return r0;
139}
140
141
142static void __bcm_kona_smc(void *info)
143{
144 struct bcm_kona_smc_data *data = info;
145 u32 *args = bcm_smc_buffer;
146
147 BUG_ON(smp_processor_id() != 0);
148 BUG_ON(!args);
149
150
151 writel_relaxed(data->arg0, args++);
152 writel_relaxed(data->arg1, args++);
153 writel_relaxed(data->arg2, args++);
154 writel(data->arg3, args);
155
156
157 flush_cache_all();
158
159
160 data->result = bcm_kona_do_smc(data->service_id, bcm_smc_buffer_phys);
161}
162
163unsigned bcm_kona_smc(unsigned service_id, unsigned arg0, unsigned arg1,
164 unsigned arg2, unsigned arg3)
165{
166 struct bcm_kona_smc_data data;
167
168 data.service_id = service_id;
169 data.arg0 = arg0;
170 data.arg1 = arg1;
171 data.arg2 = arg2;
172 data.arg3 = arg3;
173 data.result = 0;
174
175
176
177
178
179 smp_call_function_single(0, __bcm_kona_smc, &data, 1);
180
181 return data.result;
182}
183