Loading...
1/* SPDX-License-Identifier: GPL-2.0-or-later */
2/*
3 * This file contains miscellaneous low-level functions.
4 * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
5 *
6 * Largely rewritten by Cort Dougan (cort@cs.nmt.edu)
7 * and Paul Mackerras.
8 *
9 */
10
11#include <linux/sys.h>
12#include <asm/unistd.h>
13#include <asm/errno.h>
14#include <asm/reg.h>
15#include <asm/page.h>
16#include <asm/cache.h>
17#include <asm/cputable.h>
18#include <asm/mmu.h>
19#include <asm/ppc_asm.h>
20#include <asm/thread_info.h>
21#include <asm/asm-offsets.h>
22#include <asm/processor.h>
23#include <asm/bug.h>
24#include <asm/ptrace.h>
25#include <asm/export.h>
26#include <asm/feature-fixups.h>
27
28 .text
29
30/*
31 * This returns the high 64 bits of the product of two 64-bit numbers.
32 */
33_GLOBAL(mulhdu)
34 cmpwi r6,0
35 cmpwi cr1,r3,0
36 mr r10,r4
37 mulhwu r4,r4,r5
38 beq 1f
39 mulhwu r0,r10,r6
40 mullw r7,r10,r5
41 addc r7,r0,r7
42 addze r4,r4
431: beqlr cr1 /* all done if high part of A is 0 */
44 mullw r9,r3,r5
45 mulhwu r10,r3,r5
46 beq 2f
47 mullw r0,r3,r6
48 mulhwu r8,r3,r6
49 addc r7,r0,r7
50 adde r4,r4,r8
51 addze r10,r10
522: addc r4,r4,r9
53 addze r3,r10
54 blr
55
56/*
57 * reloc_got2 runs through the .got2 section adding an offset
58 * to each entry.
59 */
60_GLOBAL(reloc_got2)
61 mflr r11
62 lis r7,__got2_start@ha
63 addi r7,r7,__got2_start@l
64 lis r8,__got2_end@ha
65 addi r8,r8,__got2_end@l
66 subf r8,r7,r8
67 srwi. r8,r8,2
68 beqlr
69 mtctr r8
70 bcl 20,31,$+4
711: mflr r0
72 lis r4,1b@ha
73 addi r4,r4,1b@l
74 subf r0,r4,r0
75 add r7,r0,r7
762: lwz r0,0(r7)
77 add r0,r0,r3
78 stw r0,0(r7)
79 addi r7,r7,4
80 bdnz 2b
81 mtlr r11
82 blr
83
84/*
85 * call_setup_cpu - call the setup_cpu function for this cpu
86 * r3 = data offset, r24 = cpu number
87 *
88 * Setup function is called with:
89 * r3 = data offset
90 * r4 = ptr to CPU spec (relocated)
91 */
92_GLOBAL(call_setup_cpu)
93 addis r4,r3,cur_cpu_spec@ha
94 addi r4,r4,cur_cpu_spec@l
95 lwz r4,0(r4)
96 add r4,r4,r3
97 lwz r5,CPU_SPEC_SETUP(r4)
98 cmpwi 0,r5,0
99 add r5,r5,r3
100 beqlr
101 mtctr r5
102 bctr
103
104#if defined(CONFIG_CPU_FREQ_PMAC) && defined(CONFIG_PPC_BOOK3S_32)
105
106/* This gets called by via-pmu.c to switch the PLL selection
107 * on 750fx CPU. This function should really be moved to some
108 * other place (as most of the cpufreq code in via-pmu
109 */
110_GLOBAL(low_choose_750fx_pll)
111 /* Clear MSR:EE */
112 mfmsr r7
113 rlwinm r0,r7,0,17,15
114 mtmsr r0
115
116 /* If switching to PLL1, disable HID0:BTIC */
117 cmplwi cr0,r3,0
118 beq 1f
119 mfspr r5,SPRN_HID0
120 rlwinm r5,r5,0,27,25
121 sync
122 mtspr SPRN_HID0,r5
123 isync
124 sync
125
1261:
127 /* Calc new HID1 value */
128 mfspr r4,SPRN_HID1 /* Build a HID1:PS bit from parameter */
129 rlwinm r5,r3,16,15,15 /* Clear out HID1:PS from value read */
130 rlwinm r4,r4,0,16,14 /* Could have I used rlwimi here ? */
131 or r4,r4,r5
132 mtspr SPRN_HID1,r4
133
134#ifdef CONFIG_SMP
135 /* Store new HID1 image */
136 lwz r6,TASK_CPU(r2)
137 slwi r6,r6,2
138#else
139 li r6, 0
140#endif
141 addis r6,r6,nap_save_hid1@ha
142 stw r4,nap_save_hid1@l(r6)
143
144 /* If switching to PLL0, enable HID0:BTIC */
145 cmplwi cr0,r3,0
146 bne 1f
147 mfspr r5,SPRN_HID0
148 ori r5,r5,HID0_BTIC
149 sync
150 mtspr SPRN_HID0,r5
151 isync
152 sync
153
1541:
155 /* Return */
156 mtmsr r7
157 blr
158
159_GLOBAL(low_choose_7447a_dfs)
160 /* Clear MSR:EE */
161 mfmsr r7
162 rlwinm r0,r7,0,17,15
163 mtmsr r0
164
165 /* Calc new HID1 value */
166 mfspr r4,SPRN_HID1
167 insrwi r4,r3,1,9 /* insert parameter into bit 9 */
168 sync
169 mtspr SPRN_HID1,r4
170 sync
171 isync
172
173 /* Return */
174 mtmsr r7
175 blr
176
177#endif /* CONFIG_CPU_FREQ_PMAC && CONFIG_PPC_BOOK3S_32 */
178
179#ifdef CONFIG_40x
180
181/*
182 * Do an IO access in real mode
183 */
184_GLOBAL(real_readb)
185 mfmsr r7
186 rlwinm r0,r7,0,~MSR_DR
187 sync
188 mtmsr r0
189 sync
190 isync
191 lbz r3,0(r3)
192 sync
193 mtmsr r7
194 sync
195 isync
196 blr
197_ASM_NOKPROBE_SYMBOL(real_readb)
198
199 /*
200 * Do an IO access in real mode
201 */
202_GLOBAL(real_writeb)
203 mfmsr r7
204 rlwinm r0,r7,0,~MSR_DR
205 sync
206 mtmsr r0
207 sync
208 isync
209 stb r3,0(r4)
210 sync
211 mtmsr r7
212 sync
213 isync
214 blr
215_ASM_NOKPROBE_SYMBOL(real_writeb)
216
217#endif /* CONFIG_40x */
218
219/*
220 * Copy a whole page. We use the dcbz instruction on the destination
221 * to reduce memory traffic (it eliminates the unnecessary reads of
222 * the destination into cache). This requires that the destination
223 * is cacheable.
224 */
225#define COPY_16_BYTES \
226 lwz r6,4(r4); \
227 lwz r7,8(r4); \
228 lwz r8,12(r4); \
229 lwzu r9,16(r4); \
230 stw r6,4(r3); \
231 stw r7,8(r3); \
232 stw r8,12(r3); \
233 stwu r9,16(r3)
234
235_GLOBAL(copy_page)
236 rlwinm r5, r3, 0, L1_CACHE_BYTES - 1
237 addi r3,r3,-4
238
2390: twnei r5, 0 /* WARN if r3 is not cache aligned */
240 EMIT_WARN_ENTRY 0b,__FILE__,__LINE__, BUGFLAG_WARNING
241
242 addi r4,r4,-4
243
244 li r5,4
245
246#if MAX_COPY_PREFETCH > 1
247 li r0,MAX_COPY_PREFETCH
248 li r11,4
249 mtctr r0
25011: dcbt r11,r4
251 addi r11,r11,L1_CACHE_BYTES
252 bdnz 11b
253#else /* MAX_COPY_PREFETCH == 1 */
254 dcbt r5,r4
255 li r11,L1_CACHE_BYTES+4
256#endif /* MAX_COPY_PREFETCH */
257 li r0,PAGE_SIZE/L1_CACHE_BYTES - MAX_COPY_PREFETCH
258 crclr 4*cr0+eq
2592:
260 mtctr r0
2611:
262 dcbt r11,r4
263 dcbz r5,r3
264 COPY_16_BYTES
265#if L1_CACHE_BYTES >= 32
266 COPY_16_BYTES
267#if L1_CACHE_BYTES >= 64
268 COPY_16_BYTES
269 COPY_16_BYTES
270#if L1_CACHE_BYTES >= 128
271 COPY_16_BYTES
272 COPY_16_BYTES
273 COPY_16_BYTES
274 COPY_16_BYTES
275#endif
276#endif
277#endif
278 bdnz 1b
279 beqlr
280 crnot 4*cr0+eq,4*cr0+eq
281 li r0,MAX_COPY_PREFETCH
282 li r11,4
283 b 2b
284EXPORT_SYMBOL(copy_page)
285
286/*
287 * Extended precision shifts.
288 *
289 * Updated to be valid for shift counts from 0 to 63 inclusive.
290 * -- Gabriel
291 *
292 * R3/R4 has 64 bit value
293 * R5 has shift count
294 * result in R3/R4
295 *
296 * ashrdi3: arithmetic right shift (sign propagation)
297 * lshrdi3: logical right shift
298 * ashldi3: left shift
299 */
300_GLOBAL(__ashrdi3)
301 subfic r6,r5,32
302 srw r4,r4,r5 # LSW = count > 31 ? 0 : LSW >> count
303 addi r7,r5,32 # could be xori, or addi with -32
304 slw r6,r3,r6 # t1 = count > 31 ? 0 : MSW << (32-count)
305 rlwinm r8,r7,0,32 # t3 = (count < 32) ? 32 : 0
306 sraw r7,r3,r7 # t2 = MSW >> (count-32)
307 or r4,r4,r6 # LSW |= t1
308 slw r7,r7,r8 # t2 = (count < 32) ? 0 : t2
309 sraw r3,r3,r5 # MSW = MSW >> count
310 or r4,r4,r7 # LSW |= t2
311 blr
312EXPORT_SYMBOL(__ashrdi3)
313
314_GLOBAL(__ashldi3)
315 subfic r6,r5,32
316 slw r3,r3,r5 # MSW = count > 31 ? 0 : MSW << count
317 addi r7,r5,32 # could be xori, or addi with -32
318 srw r6,r4,r6 # t1 = count > 31 ? 0 : LSW >> (32-count)
319 slw r7,r4,r7 # t2 = count < 32 ? 0 : LSW << (count-32)
320 or r3,r3,r6 # MSW |= t1
321 slw r4,r4,r5 # LSW = LSW << count
322 or r3,r3,r7 # MSW |= t2
323 blr
324EXPORT_SYMBOL(__ashldi3)
325
326_GLOBAL(__lshrdi3)
327 subfic r6,r5,32
328 srw r4,r4,r5 # LSW = count > 31 ? 0 : LSW >> count
329 addi r7,r5,32 # could be xori, or addi with -32
330 slw r6,r3,r6 # t1 = count > 31 ? 0 : MSW << (32-count)
331 srw r7,r3,r7 # t2 = count < 32 ? 0 : MSW >> (count-32)
332 or r4,r4,r6 # LSW |= t1
333 srw r3,r3,r5 # MSW = MSW >> count
334 or r4,r4,r7 # LSW |= t2
335 blr
336EXPORT_SYMBOL(__lshrdi3)
337
338/*
339 * 64-bit comparison: __cmpdi2(s64 a, s64 b)
340 * Returns 0 if a < b, 1 if a == b, 2 if a > b.
341 */
342_GLOBAL(__cmpdi2)
343 cmpw r3,r5
344 li r3,1
345 bne 1f
346 cmplw r4,r6
347 beqlr
3481: li r3,0
349 bltlr
350 li r3,2
351 blr
352EXPORT_SYMBOL(__cmpdi2)
353/*
354 * 64-bit comparison: __ucmpdi2(u64 a, u64 b)
355 * Returns 0 if a < b, 1 if a == b, 2 if a > b.
356 */
357_GLOBAL(__ucmpdi2)
358 cmplw r3,r5
359 li r3,1
360 bne 1f
361 cmplw r4,r6
362 beqlr
3631: li r3,0
364 bltlr
365 li r3,2
366 blr
367EXPORT_SYMBOL(__ucmpdi2)
368
369_GLOBAL(__bswapdi2)
370 rotlwi r9,r4,8
371 rotlwi r10,r3,8
372 rlwimi r9,r4,24,0,7
373 rlwimi r10,r3,24,0,7
374 rlwimi r9,r4,24,16,23
375 rlwimi r10,r3,24,16,23
376 mr r3,r9
377 mr r4,r10
378 blr
379EXPORT_SYMBOL(__bswapdi2)
380
381#ifdef CONFIG_SMP
382_GLOBAL(start_secondary_resume)
383 /* Reset stack */
384 rlwinm r1, r1, 0, 0, 31 - THREAD_SHIFT
385 addi r1,r1,THREAD_SIZE-STACK_FRAME_MIN_SIZE
386 li r3,0
387 stw r3,0(r1) /* Zero the stack frame pointer */
388 bl start_secondary
389 b .
390#endif /* CONFIG_SMP */
1/*
2 * This file contains miscellaneous low-level functions.
3 * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
4 *
5 * Largely rewritten by Cort Dougan (cort@cs.nmt.edu)
6 * and Paul Mackerras.
7 *
8 * kexec bits:
9 * Copyright (C) 2002-2003 Eric Biederman <ebiederm@xmission.com>
10 * GameCube/ppc32 port Copyright (C) 2004 Albert Herranz
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version
15 * 2 of the License, or (at your option) any later version.
16 *
17 */
18
19#include <linux/sys.h>
20#include <asm/unistd.h>
21#include <asm/errno.h>
22#include <asm/reg.h>
23#include <asm/page.h>
24#include <asm/cache.h>
25#include <asm/cputable.h>
26#include <asm/mmu.h>
27#include <asm/ppc_asm.h>
28#include <asm/thread_info.h>
29#include <asm/asm-offsets.h>
30#include <asm/processor.h>
31#include <asm/kexec.h>
32#include <asm/bug.h>
33#include <asm/ptrace.h>
34
35 .text
36
37_GLOBAL(call_do_softirq)
38 mflr r0
39 stw r0,4(r1)
40 stwu r1,THREAD_SIZE-STACK_FRAME_OVERHEAD(r3)
41 mr r1,r3
42 bl __do_softirq
43 lwz r1,0(r1)
44 lwz r0,4(r1)
45 mtlr r0
46 blr
47
48_GLOBAL(call_handle_irq)
49 mflr r0
50 stw r0,4(r1)
51 mtctr r6
52 stwu r1,THREAD_SIZE-STACK_FRAME_OVERHEAD(r5)
53 mr r1,r5
54 bctrl
55 lwz r1,0(r1)
56 lwz r0,4(r1)
57 mtlr r0
58 blr
59
60/*
61 * This returns the high 64 bits of the product of two 64-bit numbers.
62 */
63_GLOBAL(mulhdu)
64 cmpwi r6,0
65 cmpwi cr1,r3,0
66 mr r10,r4
67 mulhwu r4,r4,r5
68 beq 1f
69 mulhwu r0,r10,r6
70 mullw r7,r10,r5
71 addc r7,r0,r7
72 addze r4,r4
731: beqlr cr1 /* all done if high part of A is 0 */
74 mr r10,r3
75 mullw r9,r3,r5
76 mulhwu r3,r3,r5
77 beq 2f
78 mullw r0,r10,r6
79 mulhwu r8,r10,r6
80 addc r7,r0,r7
81 adde r4,r4,r8
82 addze r3,r3
832: addc r4,r4,r9
84 addze r3,r3
85 blr
86
87/*
88 * sub_reloc_offset(x) returns x - reloc_offset().
89 */
90_GLOBAL(sub_reloc_offset)
91 mflr r0
92 bl 1f
931: mflr r5
94 lis r4,1b@ha
95 addi r4,r4,1b@l
96 subf r5,r4,r5
97 subf r3,r5,r3
98 mtlr r0
99 blr
100
101/*
102 * reloc_got2 runs through the .got2 section adding an offset
103 * to each entry.
104 */
105_GLOBAL(reloc_got2)
106 mflr r11
107 lis r7,__got2_start@ha
108 addi r7,r7,__got2_start@l
109 lis r8,__got2_end@ha
110 addi r8,r8,__got2_end@l
111 subf r8,r7,r8
112 srwi. r8,r8,2
113 beqlr
114 mtctr r8
115 bl 1f
1161: mflr r0
117 lis r4,1b@ha
118 addi r4,r4,1b@l
119 subf r0,r4,r0
120 add r7,r0,r7
1212: lwz r0,0(r7)
122 add r0,r0,r3
123 stw r0,0(r7)
124 addi r7,r7,4
125 bdnz 2b
126 mtlr r11
127 blr
128
129/*
130 * call_setup_cpu - call the setup_cpu function for this cpu
131 * r3 = data offset, r24 = cpu number
132 *
133 * Setup function is called with:
134 * r3 = data offset
135 * r4 = ptr to CPU spec (relocated)
136 */
137_GLOBAL(call_setup_cpu)
138 addis r4,r3,cur_cpu_spec@ha
139 addi r4,r4,cur_cpu_spec@l
140 lwz r4,0(r4)
141 add r4,r4,r3
142 lwz r5,CPU_SPEC_SETUP(r4)
143 cmpwi 0,r5,0
144 add r5,r5,r3
145 beqlr
146 mtctr r5
147 bctr
148
149#if defined(CONFIG_CPU_FREQ_PMAC) && defined(CONFIG_6xx)
150
151/* This gets called by via-pmu.c to switch the PLL selection
152 * on 750fx CPU. This function should really be moved to some
153 * other place (as most of the cpufreq code in via-pmu
154 */
155_GLOBAL(low_choose_750fx_pll)
156 /* Clear MSR:EE */
157 mfmsr r7
158 rlwinm r0,r7,0,17,15
159 mtmsr r0
160
161 /* If switching to PLL1, disable HID0:BTIC */
162 cmplwi cr0,r3,0
163 beq 1f
164 mfspr r5,SPRN_HID0
165 rlwinm r5,r5,0,27,25
166 sync
167 mtspr SPRN_HID0,r5
168 isync
169 sync
170
1711:
172 /* Calc new HID1 value */
173 mfspr r4,SPRN_HID1 /* Build a HID1:PS bit from parameter */
174 rlwinm r5,r3,16,15,15 /* Clear out HID1:PS from value read */
175 rlwinm r4,r4,0,16,14 /* Could have I used rlwimi here ? */
176 or r4,r4,r5
177 mtspr SPRN_HID1,r4
178
179 /* Store new HID1 image */
180 rlwinm r6,r1,0,0,(31-THREAD_SHIFT)
181 lwz r6,TI_CPU(r6)
182 slwi r6,r6,2
183 addis r6,r6,nap_save_hid1@ha
184 stw r4,nap_save_hid1@l(r6)
185
186 /* If switching to PLL0, enable HID0:BTIC */
187 cmplwi cr0,r3,0
188 bne 1f
189 mfspr r5,SPRN_HID0
190 ori r5,r5,HID0_BTIC
191 sync
192 mtspr SPRN_HID0,r5
193 isync
194 sync
195
1961:
197 /* Return */
198 mtmsr r7
199 blr
200
201_GLOBAL(low_choose_7447a_dfs)
202 /* Clear MSR:EE */
203 mfmsr r7
204 rlwinm r0,r7,0,17,15
205 mtmsr r0
206
207 /* Calc new HID1 value */
208 mfspr r4,SPRN_HID1
209 insrwi r4,r3,1,9 /* insert parameter into bit 9 */
210 sync
211 mtspr SPRN_HID1,r4
212 sync
213 isync
214
215 /* Return */
216 mtmsr r7
217 blr
218
219#endif /* CONFIG_CPU_FREQ_PMAC && CONFIG_6xx */
220
221/*
222 * complement mask on the msr then "or" some values on.
223 * _nmask_and_or_msr(nmask, value_to_or)
224 */
225_GLOBAL(_nmask_and_or_msr)
226 mfmsr r0 /* Get current msr */
227 andc r0,r0,r3 /* And off the bits set in r3 (first parm) */
228 or r0,r0,r4 /* Or on the bits in r4 (second parm) */
229 SYNC /* Some chip revs have problems here... */
230 mtmsr r0 /* Update machine state */
231 isync
232 blr /* Done */
233
234#ifdef CONFIG_40x
235
236/*
237 * Do an IO access in real mode
238 */
239_GLOBAL(real_readb)
240 mfmsr r7
241 ori r0,r7,MSR_DR
242 xori r0,r0,MSR_DR
243 sync
244 mtmsr r0
245 sync
246 isync
247 lbz r3,0(r3)
248 sync
249 mtmsr r7
250 sync
251 isync
252 blr
253
254 /*
255 * Do an IO access in real mode
256 */
257_GLOBAL(real_writeb)
258 mfmsr r7
259 ori r0,r7,MSR_DR
260 xori r0,r0,MSR_DR
261 sync
262 mtmsr r0
263 sync
264 isync
265 stb r3,0(r4)
266 sync
267 mtmsr r7
268 sync
269 isync
270 blr
271
272#endif /* CONFIG_40x */
273
274
275/*
276 * Flush instruction cache.
277 * This is a no-op on the 601.
278 */
279_GLOBAL(flush_instruction_cache)
280#if defined(CONFIG_8xx)
281 isync
282 lis r5, IDC_INVALL@h
283 mtspr SPRN_IC_CST, r5
284#elif defined(CONFIG_4xx)
285#ifdef CONFIG_403GCX
286 li r3, 512
287 mtctr r3
288 lis r4, KERNELBASE@h
2891: iccci 0, r4
290 addi r4, r4, 16
291 bdnz 1b
292#else
293 lis r3, KERNELBASE@h
294 iccci 0,r3
295#endif
296#elif CONFIG_FSL_BOOKE
297BEGIN_FTR_SECTION
298 mfspr r3,SPRN_L1CSR0
299 ori r3,r3,L1CSR0_CFI|L1CSR0_CLFC
300 /* msync; isync recommended here */
301 mtspr SPRN_L1CSR0,r3
302 isync
303 blr
304END_FTR_SECTION_IFSET(CPU_FTR_UNIFIED_ID_CACHE)
305 mfspr r3,SPRN_L1CSR1
306 ori r3,r3,L1CSR1_ICFI|L1CSR1_ICLFR
307 mtspr SPRN_L1CSR1,r3
308#else
309 mfspr r3,SPRN_PVR
310 rlwinm r3,r3,16,16,31
311 cmpwi 0,r3,1
312 beqlr /* for 601, do nothing */
313 /* 603/604 processor - use invalidate-all bit in HID0 */
314 mfspr r3,SPRN_HID0
315 ori r3,r3,HID0_ICFI
316 mtspr SPRN_HID0,r3
317#endif /* CONFIG_8xx/4xx */
318 isync
319 blr
320
321/*
322 * Write any modified data cache blocks out to memory
323 * and invalidate the corresponding instruction cache blocks.
324 * This is a no-op on the 601.
325 *
326 * flush_icache_range(unsigned long start, unsigned long stop)
327 */
328_KPROBE(__flush_icache_range)
329BEGIN_FTR_SECTION
330 blr /* for 601, do nothing */
331END_FTR_SECTION_IFSET(CPU_FTR_COHERENT_ICACHE)
332 li r5,L1_CACHE_BYTES-1
333 andc r3,r3,r5
334 subf r4,r3,r4
335 add r4,r4,r5
336 srwi. r4,r4,L1_CACHE_SHIFT
337 beqlr
338 mtctr r4
339 mr r6,r3
3401: dcbst 0,r3
341 addi r3,r3,L1_CACHE_BYTES
342 bdnz 1b
343 sync /* wait for dcbst's to get to ram */
344#ifndef CONFIG_44x
345 mtctr r4
3462: icbi 0,r6
347 addi r6,r6,L1_CACHE_BYTES
348 bdnz 2b
349#else
350 /* Flash invalidate on 44x because we are passed kmapped addresses and
351 this doesn't work for userspace pages due to the virtually tagged
352 icache. Sigh. */
353 iccci 0, r0
354#endif
355 sync /* additional sync needed on g4 */
356 isync
357 blr
358/*
359 * Write any modified data cache blocks out to memory.
360 * Does not invalidate the corresponding cache lines (especially for
361 * any corresponding instruction cache).
362 *
363 * clean_dcache_range(unsigned long start, unsigned long stop)
364 */
365_GLOBAL(clean_dcache_range)
366 li r5,L1_CACHE_BYTES-1
367 andc r3,r3,r5
368 subf r4,r3,r4
369 add r4,r4,r5
370 srwi. r4,r4,L1_CACHE_SHIFT
371 beqlr
372 mtctr r4
373
3741: dcbst 0,r3
375 addi r3,r3,L1_CACHE_BYTES
376 bdnz 1b
377 sync /* wait for dcbst's to get to ram */
378 blr
379
380/*
381 * Write any modified data cache blocks out to memory and invalidate them.
382 * Does not invalidate the corresponding instruction cache blocks.
383 *
384 * flush_dcache_range(unsigned long start, unsigned long stop)
385 */
386_GLOBAL(flush_dcache_range)
387 li r5,L1_CACHE_BYTES-1
388 andc r3,r3,r5
389 subf r4,r3,r4
390 add r4,r4,r5
391 srwi. r4,r4,L1_CACHE_SHIFT
392 beqlr
393 mtctr r4
394
3951: dcbf 0,r3
396 addi r3,r3,L1_CACHE_BYTES
397 bdnz 1b
398 sync /* wait for dcbst's to get to ram */
399 blr
400
401/*
402 * Like above, but invalidate the D-cache. This is used by the 8xx
403 * to invalidate the cache so the PPC core doesn't get stale data
404 * from the CPM (no cache snooping here :-).
405 *
406 * invalidate_dcache_range(unsigned long start, unsigned long stop)
407 */
408_GLOBAL(invalidate_dcache_range)
409 li r5,L1_CACHE_BYTES-1
410 andc r3,r3,r5
411 subf r4,r3,r4
412 add r4,r4,r5
413 srwi. r4,r4,L1_CACHE_SHIFT
414 beqlr
415 mtctr r4
416
4171: dcbi 0,r3
418 addi r3,r3,L1_CACHE_BYTES
419 bdnz 1b
420 sync /* wait for dcbi's to get to ram */
421 blr
422
423/*
424 * Flush a particular page from the data cache to RAM.
425 * Note: this is necessary because the instruction cache does *not*
426 * snoop from the data cache.
427 * This is a no-op on the 601 which has a unified cache.
428 *
429 * void __flush_dcache_icache(void *page)
430 */
431_GLOBAL(__flush_dcache_icache)
432BEGIN_FTR_SECTION
433 blr
434END_FTR_SECTION_IFSET(CPU_FTR_COHERENT_ICACHE)
435 rlwinm r3,r3,0,0,31-PAGE_SHIFT /* Get page base address */
436 li r4,PAGE_SIZE/L1_CACHE_BYTES /* Number of lines in a page */
437 mtctr r4
438 mr r6,r3
4390: dcbst 0,r3 /* Write line to ram */
440 addi r3,r3,L1_CACHE_BYTES
441 bdnz 0b
442 sync
443#ifdef CONFIG_44x
444 /* We don't flush the icache on 44x. Those have a virtual icache
445 * and we don't have access to the virtual address here (it's
446 * not the page vaddr but where it's mapped in user space). The
447 * flushing of the icache on these is handled elsewhere, when
448 * a change in the address space occurs, before returning to
449 * user space
450 */
451BEGIN_MMU_FTR_SECTION
452 blr
453END_MMU_FTR_SECTION_IFSET(MMU_FTR_TYPE_44x)
454#endif /* CONFIG_44x */
455 mtctr r4
4561: icbi 0,r6
457 addi r6,r6,L1_CACHE_BYTES
458 bdnz 1b
459 sync
460 isync
461 blr
462
463#ifndef CONFIG_BOOKE
464/*
465 * Flush a particular page from the data cache to RAM, identified
466 * by its physical address. We turn off the MMU so we can just use
467 * the physical address (this may be a highmem page without a kernel
468 * mapping).
469 *
470 * void __flush_dcache_icache_phys(unsigned long physaddr)
471 */
472_GLOBAL(__flush_dcache_icache_phys)
473BEGIN_FTR_SECTION
474 blr /* for 601, do nothing */
475END_FTR_SECTION_IFSET(CPU_FTR_COHERENT_ICACHE)
476 mfmsr r10
477 rlwinm r0,r10,0,28,26 /* clear DR */
478 mtmsr r0
479 isync
480 rlwinm r3,r3,0,0,31-PAGE_SHIFT /* Get page base address */
481 li r4,PAGE_SIZE/L1_CACHE_BYTES /* Number of lines in a page */
482 mtctr r4
483 mr r6,r3
4840: dcbst 0,r3 /* Write line to ram */
485 addi r3,r3,L1_CACHE_BYTES
486 bdnz 0b
487 sync
488 mtctr r4
4891: icbi 0,r6
490 addi r6,r6,L1_CACHE_BYTES
491 bdnz 1b
492 sync
493 mtmsr r10 /* restore DR */
494 isync
495 blr
496#endif /* CONFIG_BOOKE */
497
498/*
499 * Clear pages using the dcbz instruction, which doesn't cause any
500 * memory traffic (except to write out any cache lines which get
501 * displaced). This only works on cacheable memory.
502 *
503 * void clear_pages(void *page, int order) ;
504 */
505_GLOBAL(clear_pages)
506 li r0,PAGE_SIZE/L1_CACHE_BYTES
507 slw r0,r0,r4
508 mtctr r0
5091: dcbz 0,r3
510 addi r3,r3,L1_CACHE_BYTES
511 bdnz 1b
512 blr
513
514/*
515 * Copy a whole page. We use the dcbz instruction on the destination
516 * to reduce memory traffic (it eliminates the unnecessary reads of
517 * the destination into cache). This requires that the destination
518 * is cacheable.
519 */
520#define COPY_16_BYTES \
521 lwz r6,4(r4); \
522 lwz r7,8(r4); \
523 lwz r8,12(r4); \
524 lwzu r9,16(r4); \
525 stw r6,4(r3); \
526 stw r7,8(r3); \
527 stw r8,12(r3); \
528 stwu r9,16(r3)
529
530_GLOBAL(copy_page)
531 addi r3,r3,-4
532 addi r4,r4,-4
533
534 li r5,4
535
536#if MAX_COPY_PREFETCH > 1
537 li r0,MAX_COPY_PREFETCH
538 li r11,4
539 mtctr r0
54011: dcbt r11,r4
541 addi r11,r11,L1_CACHE_BYTES
542 bdnz 11b
543#else /* MAX_COPY_PREFETCH == 1 */
544 dcbt r5,r4
545 li r11,L1_CACHE_BYTES+4
546#endif /* MAX_COPY_PREFETCH */
547 li r0,PAGE_SIZE/L1_CACHE_BYTES - MAX_COPY_PREFETCH
548 crclr 4*cr0+eq
5492:
550 mtctr r0
5511:
552 dcbt r11,r4
553 dcbz r5,r3
554 COPY_16_BYTES
555#if L1_CACHE_BYTES >= 32
556 COPY_16_BYTES
557#if L1_CACHE_BYTES >= 64
558 COPY_16_BYTES
559 COPY_16_BYTES
560#if L1_CACHE_BYTES >= 128
561 COPY_16_BYTES
562 COPY_16_BYTES
563 COPY_16_BYTES
564 COPY_16_BYTES
565#endif
566#endif
567#endif
568 bdnz 1b
569 beqlr
570 crnot 4*cr0+eq,4*cr0+eq
571 li r0,MAX_COPY_PREFETCH
572 li r11,4
573 b 2b
574
575/*
576 * void atomic_clear_mask(atomic_t mask, atomic_t *addr)
577 * void atomic_set_mask(atomic_t mask, atomic_t *addr);
578 */
579_GLOBAL(atomic_clear_mask)
58010: lwarx r5,0,r4
581 andc r5,r5,r3
582 PPC405_ERR77(0,r4)
583 stwcx. r5,0,r4
584 bne- 10b
585 blr
586_GLOBAL(atomic_set_mask)
58710: lwarx r5,0,r4
588 or r5,r5,r3
589 PPC405_ERR77(0,r4)
590 stwcx. r5,0,r4
591 bne- 10b
592 blr
593
594/*
595 * Extended precision shifts.
596 *
597 * Updated to be valid for shift counts from 0 to 63 inclusive.
598 * -- Gabriel
599 *
600 * R3/R4 has 64 bit value
601 * R5 has shift count
602 * result in R3/R4
603 *
604 * ashrdi3: arithmetic right shift (sign propagation)
605 * lshrdi3: logical right shift
606 * ashldi3: left shift
607 */
608_GLOBAL(__ashrdi3)
609 subfic r6,r5,32
610 srw r4,r4,r5 # LSW = count > 31 ? 0 : LSW >> count
611 addi r7,r5,32 # could be xori, or addi with -32
612 slw r6,r3,r6 # t1 = count > 31 ? 0 : MSW << (32-count)
613 rlwinm r8,r7,0,32 # t3 = (count < 32) ? 32 : 0
614 sraw r7,r3,r7 # t2 = MSW >> (count-32)
615 or r4,r4,r6 # LSW |= t1
616 slw r7,r7,r8 # t2 = (count < 32) ? 0 : t2
617 sraw r3,r3,r5 # MSW = MSW >> count
618 or r4,r4,r7 # LSW |= t2
619 blr
620
621_GLOBAL(__ashldi3)
622 subfic r6,r5,32
623 slw r3,r3,r5 # MSW = count > 31 ? 0 : MSW << count
624 addi r7,r5,32 # could be xori, or addi with -32
625 srw r6,r4,r6 # t1 = count > 31 ? 0 : LSW >> (32-count)
626 slw r7,r4,r7 # t2 = count < 32 ? 0 : LSW << (count-32)
627 or r3,r3,r6 # MSW |= t1
628 slw r4,r4,r5 # LSW = LSW << count
629 or r3,r3,r7 # MSW |= t2
630 blr
631
632_GLOBAL(__lshrdi3)
633 subfic r6,r5,32
634 srw r4,r4,r5 # LSW = count > 31 ? 0 : LSW >> count
635 addi r7,r5,32 # could be xori, or addi with -32
636 slw r6,r3,r6 # t1 = count > 31 ? 0 : MSW << (32-count)
637 srw r7,r3,r7 # t2 = count < 32 ? 0 : MSW >> (count-32)
638 or r4,r4,r6 # LSW |= t1
639 srw r3,r3,r5 # MSW = MSW >> count
640 or r4,r4,r7 # LSW |= t2
641 blr
642
643/*
644 * 64-bit comparison: __ucmpdi2(u64 a, u64 b)
645 * Returns 0 if a < b, 1 if a == b, 2 if a > b.
646 */
647_GLOBAL(__ucmpdi2)
648 cmplw r3,r5
649 li r3,1
650 bne 1f
651 cmplw r4,r6
652 beqlr
6531: li r3,0
654 bltlr
655 li r3,2
656 blr
657
658_GLOBAL(abs)
659 srawi r4,r3,31
660 xor r3,r3,r4
661 sub r3,r3,r4
662 blr
663
664/*
665 * Create a kernel thread
666 * kernel_thread(fn, arg, flags)
667 */
668_GLOBAL(kernel_thread)
669 stwu r1,-16(r1)
670 stw r30,8(r1)
671 stw r31,12(r1)
672 mr r30,r3 /* function */
673 mr r31,r4 /* argument */
674 ori r3,r5,CLONE_VM /* flags */
675 oris r3,r3,CLONE_UNTRACED>>16
676 li r4,0 /* new sp (unused) */
677 li r0,__NR_clone
678 sc
679 bns+ 1f /* did system call indicate error? */
680 neg r3,r3 /* if so, make return code negative */
6811: cmpwi 0,r3,0 /* parent or child? */
682 bne 2f /* return if parent */
683 li r0,0 /* make top-level stack frame */
684 stwu r0,-16(r1)
685 mtlr r30 /* fn addr in lr */
686 mr r3,r31 /* load arg and call fn */
687 PPC440EP_ERR42
688 blrl
689 li r0,__NR_exit /* exit if function returns */
690 li r3,0
691 sc
6922: lwz r30,8(r1)
693 lwz r31,12(r1)
694 addi r1,r1,16
695 blr
696
697#ifdef CONFIG_SMP
698_GLOBAL(start_secondary_resume)
699 /* Reset stack */
700 rlwinm r1,r1,0,0,(31-THREAD_SHIFT) /* current_thread_info() */
701 addi r1,r1,THREAD_SIZE-STACK_FRAME_OVERHEAD
702 li r3,0
703 stw r3,0(r1) /* Zero the stack frame pointer */
704 bl start_secondary
705 b .
706#endif /* CONFIG_SMP */
707
708/*
709 * This routine is just here to keep GCC happy - sigh...
710 */
711_GLOBAL(__main)
712 blr
713
714#ifdef CONFIG_KEXEC
715 /*
716 * Must be relocatable PIC code callable as a C function.
717 */
718 .globl relocate_new_kernel
719relocate_new_kernel:
720 /* r3 = page_list */
721 /* r4 = reboot_code_buffer */
722 /* r5 = start_address */
723
724#ifdef CONFIG_FSL_BOOKE
725
726 mr r29, r3
727 mr r30, r4
728 mr r31, r5
729
730#define ENTRY_MAPPING_KEXEC_SETUP
731#include "fsl_booke_entry_mapping.S"
732#undef ENTRY_MAPPING_KEXEC_SETUP
733
734 mr r3, r29
735 mr r4, r30
736 mr r5, r31
737
738 li r0, 0
739#else
740 li r0, 0
741
742 /*
743 * Set Machine Status Register to a known status,
744 * switch the MMU off and jump to 1: in a single step.
745 */
746
747 mr r8, r0
748 ori r8, r8, MSR_RI|MSR_ME
749 mtspr SPRN_SRR1, r8
750 addi r8, r4, 1f - relocate_new_kernel
751 mtspr SPRN_SRR0, r8
752 sync
753 rfi
754
7551:
756#endif
757 /* from this point address translation is turned off */
758 /* and interrupts are disabled */
759
760 /* set a new stack at the bottom of our page... */
761 /* (not really needed now) */
762 addi r1, r4, KEXEC_CONTROL_PAGE_SIZE - 8 /* for LR Save+Back Chain */
763 stw r0, 0(r1)
764
765 /* Do the copies */
766 li r6, 0 /* checksum */
767 mr r0, r3
768 b 1f
769
7700: /* top, read another word for the indirection page */
771 lwzu r0, 4(r3)
772
7731:
774 /* is it a destination page? (r8) */
775 rlwinm. r7, r0, 0, 31, 31 /* IND_DESTINATION (1<<0) */
776 beq 2f
777
778 rlwinm r8, r0, 0, 0, 19 /* clear kexec flags, page align */
779 b 0b
780
7812: /* is it an indirection page? (r3) */
782 rlwinm. r7, r0, 0, 30, 30 /* IND_INDIRECTION (1<<1) */
783 beq 2f
784
785 rlwinm r3, r0, 0, 0, 19 /* clear kexec flags, page align */
786 subi r3, r3, 4
787 b 0b
788
7892: /* are we done? */
790 rlwinm. r7, r0, 0, 29, 29 /* IND_DONE (1<<2) */
791 beq 2f
792 b 3f
793
7942: /* is it a source page? (r9) */
795 rlwinm. r7, r0, 0, 28, 28 /* IND_SOURCE (1<<3) */
796 beq 0b
797
798 rlwinm r9, r0, 0, 0, 19 /* clear kexec flags, page align */
799
800 li r7, PAGE_SIZE / 4
801 mtctr r7
802 subi r9, r9, 4
803 subi r8, r8, 4
8049:
805 lwzu r0, 4(r9) /* do the copy */
806 xor r6, r6, r0
807 stwu r0, 4(r8)
808 dcbst 0, r8
809 sync
810 icbi 0, r8
811 bdnz 9b
812
813 addi r9, r9, 4
814 addi r8, r8, 4
815 b 0b
816
8173:
818
819 /* To be certain of avoiding problems with self-modifying code
820 * execute a serializing instruction here.
821 */
822 isync
823 sync
824
825 mfspr r3, SPRN_PIR /* current core we are running on */
826 mr r4, r5 /* load physical address of chunk called */
827
828 /* jump to the entry point, usually the setup routine */
829 mtlr r5
830 blrl
831
8321: b 1b
833
834relocate_new_kernel_end:
835
836 .globl relocate_new_kernel_size
837relocate_new_kernel_size:
838 .long relocate_new_kernel_end - relocate_new_kernel
839#endif