blob: bcc419da0b919e8033203af2e75dc816f1b1aa09 [file] [log] [blame]
Marc Zyngier0237c632014-07-18 21:06:38 +01001/*
2 * Copyright (C) 2013 - ARM Ltd
3 * Author: Marc Zyngier <marc.zyngier@arm.com>
4 *
5 * Based on code by Carl van Schaik <carl@ok-labs.com>.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <config.h>
Jan Kiszkafdb0ff72014-12-31 13:46:40 +010021#include <asm/gic.h>
Jan Kiszka2ecdd682015-04-21 07:18:26 +020022#include <asm/macro.h>
Marc Zyngier0237c632014-07-18 21:06:38 +010023#include <asm/psci.h>
24#include <asm/arch/cpu.h>
25
26/*
27 * Memory layout:
28 *
29 * SECURE_RAM to text_end :
30 * ._secure_text section
31 * text_end to ALIGN_PAGE(text_end):
32 * nothing
33 * ALIGN_PAGE(text_end) to ALIGN_PAGE(text_end) + 0x1000)
34 * 1kB of stack per CPU (4 CPUs max).
35 */
36
37 .pushsection ._secure.text, "ax"
38
39 .arch_extension sec
40
Iain Paton630df142015-03-28 10:26:38 +000041#define ONE_MS (CONFIG_TIMER_CLK_FREQ / 1000)
Marc Zyngier0237c632014-07-18 21:06:38 +010042#define TEN_MS (10 * ONE_MS)
Jan Kiszkafdb0ff72014-12-31 13:46:40 +010043#define GICD_BASE 0x1c81000
44#define GICC_BASE 0x1c82000
Marc Zyngier0237c632014-07-18 21:06:38 +010045
46.macro timer_wait reg, ticks
47 @ Program CNTP_TVAL
48 movw \reg, #(\ticks & 0xffff)
49 movt \reg, #(\ticks >> 16)
50 mcr p15, 0, \reg, c14, c2, 0
51 isb
52 @ Enable physical timer, mask interrupt
53 mov \reg, #3
54 mcr p15, 0, \reg, c14, c2, 1
55 @ Poll physical timer until ISTATUS is on
561: isb
57 mrc p15, 0, \reg, c14, c2, 1
58 ands \reg, \reg, #4
59 bne 1b
60 @ Disable timer
61 mov \reg, #0
62 mcr p15, 0, \reg, c14, c2, 1
63 isb
64.endm
65
Jan Kiszkafdb0ff72014-12-31 13:46:40 +010066.globl psci_fiq_enter
67psci_fiq_enter:
68 push {r0-r12}
69
70 @ Switch to secure
71 mrc p15, 0, r7, c1, c1, 0
72 bic r8, r7, #1
73 mcr p15, 0, r8, c1, c1, 0
74 isb
75
76 @ Validate reason based on IAR and acknowledge
77 movw r8, #(GICC_BASE & 0xffff)
78 movt r8, #(GICC_BASE >> 16)
79 ldr r9, [r8, #GICC_IAR]
80 movw r10, #0x3ff
81 movt r10, #0
82 cmp r9, r10 @ skip spurious interrupt 1023
83 beq out
84 movw r10, #0x3fe @ ...and 1022
85 cmp r9, r10
86 beq out
87 str r9, [r8, #GICC_EOIR] @ acknowledge the interrupt
88 dsb
89
90 @ Compute CPU number
91 lsr r9, r9, #10
92 and r9, r9, #0xf
93
94 movw r8, #(SUN7I_CPUCFG_BASE & 0xffff)
95 movt r8, #(SUN7I_CPUCFG_BASE >> 16)
96
97 @ Wait for the core to enter WFI
98 lsl r11, r9, #6 @ x64
99 add r11, r11, r8
100
1011: ldr r10, [r11, #0x48]
102 tst r10, #(1 << 2)
103 bne 2f
104 timer_wait r10, ONE_MS
105 b 1b
106
107 @ Reset CPU
1082: mov r10, #0
109 str r10, [r11, #0x40]
110
111 @ Lock CPU
112 mov r10, #1
113 lsl r9, r10, r9 @ r9 is now CPU mask
114 ldr r10, [r8, #0x1e4]
115 bic r10, r10, r9
116 str r10, [r8, #0x1e4]
117
118 @ Set power gating
119 ldr r10, [r8, #0x1b4]
120 orr r10, r10, #1
121 str r10, [r8, #0x1b4]
122 timer_wait r10, ONE_MS
123
124 @ Activate power clamp
125 mov r10, #1
1261: str r10, [r8, #0x1b0]
127 lsl r10, r10, #1
128 orr r10, r10, #1
129 tst r10, #0x100
130 beq 1b
131
132 @ Restore security level
133out: mcr p15, 0, r7, c1, c1, 0
134
135 pop {r0-r12}
136 subs pc, lr, #4
137
Marc Zyngier0237c632014-07-18 21:06:38 +0100138 @ r1 = target CPU
139 @ r2 = target PC
140.globl psci_cpu_on
141psci_cpu_on:
142 adr r0, _target_pc
143 str r2, [r0]
144 dsb
145
Hans de Goede316e29b2014-10-27 23:59:27 +0100146 movw r0, #(SUN7I_CPUCFG_BASE & 0xffff)
147 movt r0, #(SUN7I_CPUCFG_BASE >> 16)
Marc Zyngier0237c632014-07-18 21:06:38 +0100148
149 @ CPU mask
150 and r1, r1, #3 @ only care about first cluster
151 mov r4, #1
152 lsl r4, r4, r1
153
154 adr r6, _sunxi_cpu_entry
155 str r6, [r0, #0x1a4] @ PRIVATE_REG (boot vector)
156
157 @ Assert reset on target CPU
158 mov r6, #0
159 lsl r5, r1, #6 @ 64 bytes per CPU
160 add r5, r5, #0x40 @ Offset from base
161 add r5, r5, r0 @ CPU control block
162 str r6, [r5] @ Reset CPU
163
164 @ l1 invalidate
165 ldr r6, [r0, #0x184]
166 bic r6, r6, r4
167 str r6, [r0, #0x184]
168
169 @ Lock CPU
170 ldr r6, [r0, #0x1e4]
171 bic r6, r6, r4
172 str r6, [r0, #0x1e4]
173
174 @ Release power clamp
175 movw r6, #0x1ff
176 movt r6, #0
1771: lsrs r6, r6, #1
178 str r6, [r0, #0x1b0]
179 bne 1b
180
181 timer_wait r1, TEN_MS
182
183 @ Clear power gating
184 ldr r6, [r0, #0x1b4]
185 bic r6, r6, #1
186 str r6, [r0, #0x1b4]
187
188 @ Deassert reset on target CPU
189 mov r6, #3
190 str r6, [r5]
191
192 @ Unlock CPU
193 ldr r6, [r0, #0x1e4]
194 orr r6, r6, r4
195 str r6, [r0, #0x1e4]
196
197 mov r0, #ARM_PSCI_RET_SUCCESS @ Return PSCI_RET_SUCCESS
198 mov pc, lr
199
200_target_pc:
201 .word 0
202
Jan Kiszkafdb0ff72014-12-31 13:46:40 +0100203/* Imported from Linux kernel */
204v7_flush_dcache_all:
205 dmb @ ensure ordering with previous memory accesses
206 mrc p15, 1, r0, c0, c0, 1 @ read clidr
207 ands r3, r0, #0x7000000 @ extract loc from clidr
208 mov r3, r3, lsr #23 @ left align loc bit field
209 beq finished @ if loc is 0, then no need to clean
210 mov r10, #0 @ start clean at cache level 0
211flush_levels:
212 add r2, r10, r10, lsr #1 @ work out 3x current cache level
213 mov r1, r0, lsr r2 @ extract cache type bits from clidr
214 and r1, r1, #7 @ mask of the bits for current cache only
215 cmp r1, #2 @ see what cache we have at this level
216 blt skip @ skip if no cache, or just i-cache
217 mrs r9, cpsr @ make cssr&csidr read atomic
218 mcr p15, 2, r10, c0, c0, 0 @ select current cache level in cssr
219 isb @ isb to sych the new cssr&csidr
220 mrc p15, 1, r1, c0, c0, 0 @ read the new csidr
221 msr cpsr_c, r9
222 and r2, r1, #7 @ extract the length of the cache lines
223 add r2, r2, #4 @ add 4 (line length offset)
224 ldr r4, =0x3ff
225 ands r4, r4, r1, lsr #3 @ find maximum number on the way size
226 clz r5, r4 @ find bit position of way size increment
227 ldr r7, =0x7fff
228 ands r7, r7, r1, lsr #13 @ extract max number of the index size
229loop1:
230 mov r9, r7 @ create working copy of max index
231loop2:
232 orr r11, r10, r4, lsl r5 @ factor way and cache number into r11
233 orr r11, r11, r9, lsl r2 @ factor index number into r11
234 mcr p15, 0, r11, c7, c14, 2 @ clean & invalidate by set/way
235 subs r9, r9, #1 @ decrement the index
236 bge loop2
237 subs r4, r4, #1 @ decrement the way
238 bge loop1
239skip:
240 add r10, r10, #2 @ increment cache number
241 cmp r3, r10
242 bgt flush_levels
243finished:
244 mov r10, #0 @ swith back to cache level 0
245 mcr p15, 2, r10, c0, c0, 0 @ select current cache level in cssr
246 dsb st
247 isb
248 bx lr
249
Marc Zyngier0237c632014-07-18 21:06:38 +0100250_sunxi_cpu_entry:
251 @ Set SMP bit
252 mrc p15, 0, r0, c1, c0, 1
253 orr r0, r0, #0x40
254 mcr p15, 0, r0, c1, c0, 1
255 isb
256
257 bl _nonsec_init
Marc Zyngier0237c632014-07-18 21:06:38 +0100258
259 adr r0, _target_pc
260 ldr r0, [r0]
261 b _do_nonsec_entry
262
Jan Kiszkafdb0ff72014-12-31 13:46:40 +0100263.globl psci_cpu_off
264psci_cpu_off:
265 mrc p15, 0, r0, c1, c0, 0 @ SCTLR
266 bic r0, r0, #(1 << 2) @ Clear C bit
267 mcr p15, 0, r0, c1, c0, 0 @ SCTLR
268 isb
269 dsb
270
271 bl v7_flush_dcache_all
272
273 clrex @ Why???
274
275 mrc p15, 0, r0, c1, c0, 1 @ ACTLR
276 bic r0, r0, #(1 << 6) @ Clear SMP bit
277 mcr p15, 0, r0, c1, c0, 1 @ ACTLR
278 isb
279 dsb
280
281 @ Ask CPU0 to pull the rug...
282 movw r0, #(GICD_BASE & 0xffff)
283 movt r0, #(GICD_BASE >> 16)
284 movw r1, #15 @ SGI15
285 movt r1, #1 @ Target is CPU0
286 str r1, [r0, #GICD_SGIR]
287 dsb
288
2891: wfi
290 b 1b
291
Jan Kiszkaf2f9a802014-12-31 13:46:42 +0100292.globl psci_arch_init
293psci_arch_init:
Jan Kiszka2ecdd682015-04-21 07:18:26 +0200294 mov r6, lr
295
Jan Kiszkaf2f9a802014-12-31 13:46:42 +0100296 movw r4, #(GICD_BASE & 0xffff)
297 movt r4, #(GICD_BASE >> 16)
298
299 ldr r5, [r4, #GICD_IGROUPRn]
300 bic r5, r5, #(1 << 15) @ SGI15 as Group-0
301 str r5, [r4, #GICD_IGROUPRn]
302
303 mov r5, #0 @ Set SGI15 priority to 0
304 strb r5, [r4, #(GICD_IPRIORITYRn + 15)]
305
306 add r4, r4, #0x1000 @ GICC address
307
308 mov r5, #0xff
309 str r5, [r4, #GICC_PMR] @ Be cool with non-secure
310
311 ldr r5, [r4, #GICC_CTLR]
312 orr r5, r5, #(1 << 3) @ Switch FIQEn on
313 str r5, [r4, #GICC_CTLR]
314
315 mrc p15, 0, r5, c1, c1, 0 @ Read SCR
316 orr r5, r5, #4 @ Enable FIQ in monitor mode
317 bic r5, r5, #1 @ Secure mode
318 mcr p15, 0, r5, c1, c1, 0 @ Write SCR
319 isb
320
Jan Kiszka2ecdd682015-04-21 07:18:26 +0200321 bl psci_get_cpu_id
Jan Kiszkaf2f9a802014-12-31 13:46:42 +0100322 mov r5, #0x400 @ 1kB of stack per CPU
Jan Kiszka2ecdd682015-04-21 07:18:26 +0200323 mul r0, r0, r5
Jan Kiszkaf2f9a802014-12-31 13:46:42 +0100324
325 adr r5, text_end @ end of text
326 add r5, r5, #0x2000 @ Skip two pages
327 lsr r5, r5, #12 @ Align to start of page
328 lsl r5, r5, #12
Jan Kiszka2ecdd682015-04-21 07:18:26 +0200329 sub sp, r5, r0 @ here's our stack!
Jan Kiszkaf2f9a802014-12-31 13:46:42 +0100330
Jan Kiszka2ecdd682015-04-21 07:18:26 +0200331 bx r6
Jan Kiszkaf2f9a802014-12-31 13:46:42 +0100332
Marc Zyngier0237c632014-07-18 21:06:38 +0100333text_end:
334 .popsection