blob: 9a3aec97ac091bd8503e539544b3a62bec0bd143 [file] [log] [blame]
Lorenzo Pieralisi95322522013-07-22 12:22:13 +01001#include <linux/errno.h>
2#include <linux/linkage.h>
3#include <asm/asm-offsets.h>
4#include <asm/assembler.h>
5
6 .text
7/*
8 * Implementation of MPIDR_EL1 hash algorithm through shifting
9 * and OR'ing.
10 *
11 * @dst: register containing hash result
12 * @rs0: register containing affinity level 0 bit shift
13 * @rs1: register containing affinity level 1 bit shift
14 * @rs2: register containing affinity level 2 bit shift
15 * @rs3: register containing affinity level 3 bit shift
16 * @mpidr: register containing MPIDR_EL1 value
17 * @mask: register containing MPIDR mask
18 *
19 * Pseudo C-code:
20 *
21 *u32 dst;
22 *
23 *compute_mpidr_hash(u32 rs0, u32 rs1, u32 rs2, u32 rs3, u64 mpidr, u64 mask) {
24 * u32 aff0, aff1, aff2, aff3;
25 * u64 mpidr_masked = mpidr & mask;
26 * aff0 = mpidr_masked & 0xff;
27 * aff1 = mpidr_masked & 0xff00;
28 * aff2 = mpidr_masked & 0xff0000;
29 * aff2 = mpidr_masked & 0xff00000000;
30 * dst = (aff0 >> rs0 | aff1 >> rs1 | aff2 >> rs2 | aff3 >> rs3);
31 *}
32 * Input registers: rs0, rs1, rs2, rs3, mpidr, mask
33 * Output register: dst
34 * Note: input and output registers must be disjoint register sets
35 (eg: a macro instance with mpidr = x1 and dst = x1 is invalid)
36 */
37 .macro compute_mpidr_hash dst, rs0, rs1, rs2, rs3, mpidr, mask
38 and \mpidr, \mpidr, \mask // mask out MPIDR bits
39 and \dst, \mpidr, #0xff // mask=aff0
40 lsr \dst ,\dst, \rs0 // dst=aff0>>rs0
41 and \mask, \mpidr, #0xff00 // mask = aff1
42 lsr \mask ,\mask, \rs1
43 orr \dst, \dst, \mask // dst|=(aff1>>rs1)
44 and \mask, \mpidr, #0xff0000 // mask = aff2
45 lsr \mask ,\mask, \rs2
46 orr \dst, \dst, \mask // dst|=(aff2>>rs2)
47 and \mask, \mpidr, #0xff00000000 // mask = aff3
48 lsr \mask ,\mask, \rs3
49 orr \dst, \dst, \mask // dst|=(aff3>>rs3)
50 .endm
51/*
James Morseadc9b2d2016-04-27 17:47:06 +010052 * Save CPU state in the provided sleep_stack_data area, and publish its
53 * location for cpu_resume()'s use in sleep_save_stash.
Lorenzo Pieralisi95322522013-07-22 12:22:13 +010054 *
James Morseadc9b2d2016-04-27 17:47:06 +010055 * cpu_resume() will restore this saved state, and return. Because the
56 * link-register is saved and restored, it will appear to return from this
57 * function. So that the caller can tell the suspend/resume paths apart,
58 * __cpu_suspend_enter() will always return a non-zero value, whereas the
59 * path through cpu_resume() will return 0.
60 *
61 * x0 = struct sleep_stack_data area
Lorenzo Pieralisi95322522013-07-22 12:22:13 +010062 */
Lorenzo Pieralisi714f5992014-08-07 14:54:50 +010063ENTRY(__cpu_suspend_enter)
James Morseadc9b2d2016-04-27 17:47:06 +010064 stp x29, lr, [x0, #SLEEP_STACK_DATA_CALLEE_REGS]
65 stp x19, x20, [x0,#SLEEP_STACK_DATA_CALLEE_REGS+16]
66 stp x21, x22, [x0,#SLEEP_STACK_DATA_CALLEE_REGS+32]
67 stp x23, x24, [x0,#SLEEP_STACK_DATA_CALLEE_REGS+48]
68 stp x25, x26, [x0,#SLEEP_STACK_DATA_CALLEE_REGS+64]
69 stp x27, x28, [x0,#SLEEP_STACK_DATA_CALLEE_REGS+80]
70
71 /* save the sp in cpu_suspend_ctx */
Lorenzo Pieralisi95322522013-07-22 12:22:13 +010072 mov x2, sp
James Morseadc9b2d2016-04-27 17:47:06 +010073 str x2, [x0, #SLEEP_STACK_DATA_SYSTEM_REGS + CPU_CTX_SP]
74
75 /* find the mpidr_hash */
James Morsecabe1c82016-04-27 17:47:07 +010076 ldr x1, =sleep_save_stash
77 ldr x1, [x1]
Lorenzo Pieralisi95322522013-07-22 12:22:13 +010078 mrs x7, mpidr_el1
79 ldr x9, =mpidr_hash
80 ldr x10, [x9, #MPIDR_HASH_MASK]
81 /*
82 * Following code relies on the struct mpidr_hash
83 * members size.
84 */
85 ldp w3, w4, [x9, #MPIDR_HASH_SHIFTS]
86 ldp w5, w6, [x9, #(MPIDR_HASH_SHIFTS + 8)]
87 compute_mpidr_hash x8, x3, x4, x5, x6, x7, x10
Lorenzo Pieralisi714f5992014-08-07 14:54:50 +010088 add x1, x1, x8, lsl #3
James Morseadc9b2d2016-04-27 17:47:06 +010089
James Morsecabe1c82016-04-27 17:47:07 +010090 str x0, [x1]
91 add x0, x0, #SLEEP_STACK_DATA_SYSTEM_REGS
James Morseadc9b2d2016-04-27 17:47:06 +010092 stp x29, lr, [sp, #-16]!
James Morsecabe1c82016-04-27 17:47:07 +010093 bl cpu_do_suspend
James Morseadc9b2d2016-04-27 17:47:06 +010094 ldp x29, lr, [sp], #16
95 mov x0, #1
Lorenzo Pieralisi95322522013-07-22 12:22:13 +010096 ret
Lorenzo Pieralisi714f5992014-08-07 14:54:50 +010097ENDPROC(__cpu_suspend_enter)
Lorenzo Pieralisi95322522013-07-22 12:22:13 +010098 .ltorg
99
Lorenzo Pieralisi95322522013-07-22 12:22:13 +0100100ENTRY(cpu_resume)
101 bl el2_setup // if in EL2 drop to EL1 cleanly
James Morsecabe1c82016-04-27 17:47:07 +0100102 /* enable the MMU early - so we can access sleep_save_stash by va */
103 adr_l lr, __enable_mmu /* __cpu_setup will return here */
104 ldr x27, =_cpu_resume /* __enable_mmu will branch here */
105 adrp x25, idmap_pg_dir
106 adrp x26, swapper_pg_dir
107 b __cpu_setup
108ENDPROC(cpu_resume)
109
110ENTRY(_cpu_resume)
Lorenzo Pieralisi95322522013-07-22 12:22:13 +0100111 mrs x1, mpidr_el1
Laura Abbottc3684fb2014-11-21 21:50:40 +0000112 adrp x8, mpidr_hash
113 add x8, x8, #:lo12:mpidr_hash // x8 = struct mpidr_hash phys address
Lorenzo Pieralisi95322522013-07-22 12:22:13 +0100114 /* retrieve mpidr_hash members to compute the hash */
115 ldr x2, [x8, #MPIDR_HASH_MASK]
116 ldp w3, w4, [x8, #MPIDR_HASH_SHIFTS]
117 ldp w5, w6, [x8, #(MPIDR_HASH_SHIFTS + 8)]
118 compute_mpidr_hash x7, x3, x4, x5, x6, x1, x2
119 /* x7 contains hash index, let's use it to grab context pointer */
James Morsecabe1c82016-04-27 17:47:07 +0100120 ldr_l x0, sleep_save_stash
Lorenzo Pieralisi95322522013-07-22 12:22:13 +0100121 ldr x0, [x0, x7, lsl #3]
James Morseadc9b2d2016-04-27 17:47:06 +0100122 add x29, x0, #SLEEP_STACK_DATA_CALLEE_REGS
123 add x0, x0, #SLEEP_STACK_DATA_SYSTEM_REGS
Lorenzo Pieralisi95322522013-07-22 12:22:13 +0100124 /* load sp from context */
125 ldr x2, [x0, #CPU_CTX_SP]
Lorenzo Pieralisi95322522013-07-22 12:22:13 +0100126 mov sp, x2
Jungseok Lee6cdf9c72015-12-04 11:02:25 +0000127 /* save thread_info */
128 and x2, x2, #~(THREAD_SIZE - 1)
129 msr sp_el0, x2
Lorenzo Pieralisi95322522013-07-22 12:22:13 +0100130 /*
James Morsecabe1c82016-04-27 17:47:07 +0100131 * cpu_do_resume expects x0 to contain context address pointer
Lorenzo Pieralisi95322522013-07-22 12:22:13 +0100132 */
James Morsecabe1c82016-04-27 17:47:07 +0100133 bl cpu_do_resume
134
135#ifdef CONFIG_KASAN
136 mov x0, sp
137 bl kasan_unpoison_remaining_stack
138#endif
139
James Morseadc9b2d2016-04-27 17:47:06 +0100140 ldp x19, x20, [x29, #16]
141 ldp x21, x22, [x29, #32]
142 ldp x23, x24, [x29, #48]
143 ldp x25, x26, [x29, #64]
144 ldp x27, x28, [x29, #80]
145 ldp x29, lr, [x29]
James Morsecabe1c82016-04-27 17:47:07 +0100146 mov x0, #0
147 ret
148ENDPROC(_cpu_resume)