source: rtems/c/src/lib/libcpu/powerpc/new-exceptions/cpu_asm.S @ 2e19bfd

4.115
Last change on this file since 2e19bfd was 2e19bfd, checked in by Sebastian Huber <sebastian.huber@…>, on 12/23/14 at 11:27:53

powerpc: Use PPC_HAS_FPU

Provide floating point context support only if PPC_HAS_FPU == 1.

  • Property mode set to 100644
File size: 11.3 KB
Line 
1/*  cpu_asm.s   1.1 - 95/12/04
2 *
3 *  This file contains the assembly code for the PowerPC implementation
4 *  of RTEMS.
5 *
6 *  Author:     Andrew Bray <andy@i-cubed.co.uk>
7 *
8 *  COPYRIGHT (c) 1995 by i-cubed ltd.
9 *
10 *  To anyone who acknowledges that this file is provided "AS IS"
11 *  without any express or implied warranty:
12 *      permission to use, copy, modify, and distribute this file
13 *      for any purpose is hereby granted without fee, provided that
14 *      the above copyright notice and this notice appears in all
15 *      copies, and that the name of i-cubed limited not be used in
16 *      advertising or publicity pertaining to distribution of the
17 *      software without specific, written prior permission.
18 *      i-cubed limited makes no representations about the suitability
19 *      of this software for any purpose.
20 *
21 *  Derived from c/src/exec/cpu/no_cpu/cpu_asm.c:
22 *
23 *  COPYRIGHT (c) 1989-1997.
24 *  On-Line Applications Research Corporation (OAR).
25 *
26 *  Copyright (c) 2011-2014 embedded brains GmbH
27 *
28 *  The license and distribution terms for this file may in
29 *  the file LICENSE in this distribution or at
30 *  http://www.rtems.org/license/LICENSE.
31 */
32
33#include <rtems/asm.h>
34#include <rtems/powerpc/powerpc.h>
35#include <rtems/score/percpu.h>
36#include <libcpu/powerpc-utility.h>
37#include <bspopts.h>
38
39#ifdef BSP_USE_DATA_CACHE_BLOCK_TOUCH
40  #define DATA_CACHE_TOUCH(rega, regb) \
41        dcbt rega, regb
42#else
43  #define DATA_CACHE_TOUCH(rega, regb)
44#endif
45
46#if BSP_DATA_CACHE_ENABLED && PPC_DEFAULT_CACHE_LINE_SIZE == 32
47  #define DATA_CACHE_ZERO_AND_TOUCH(reg, offset) \
48        li reg, offset; dcbz reg, r3; DATA_CACHE_TOUCH(reg, r4)
49#else
50  #define DATA_CACHE_ZERO_AND_TOUCH(reg, offset)
51#endif
52
53#define PPC_CONTEXT_CACHE_LINE_0 (1 * PPC_DEFAULT_CACHE_LINE_SIZE)
54#define PPC_CONTEXT_CACHE_LINE_1 (2 * PPC_DEFAULT_CACHE_LINE_SIZE)
55#define PPC_CONTEXT_CACHE_LINE_2 (3 * PPC_DEFAULT_CACHE_LINE_SIZE)
56#define PPC_CONTEXT_CACHE_LINE_3 (4 * PPC_DEFAULT_CACHE_LINE_SIZE)
57#define PPC_CONTEXT_CACHE_LINE_4 (5 * PPC_DEFAULT_CACHE_LINE_SIZE)
58
59        BEGIN_CODE
60
61#if PPC_HAS_FPU == 1
62
63/*
64 * Offsets for Context_Control_fp
65 */
66
67#if (PPC_HAS_DOUBLE==1)
68        .set    FP_SIZE,        8
69#define LDF     lfd
70#define STF     stfd
71#else
72        .set    FP_SIZE,        4
73#define LDF     lfs
74#define STF     stfs
75#endif
76
77        .set    FP_0, 0
78        .set    FP_1, (FP_0 + FP_SIZE)
79        .set    FP_2, (FP_1 + FP_SIZE)
80        .set    FP_3, (FP_2 + FP_SIZE)
81        .set    FP_4, (FP_3 + FP_SIZE)
82        .set    FP_5, (FP_4 + FP_SIZE)
83        .set    FP_6, (FP_5 + FP_SIZE)
84        .set    FP_7, (FP_6 + FP_SIZE)
85        .set    FP_8, (FP_7 + FP_SIZE)
86        .set    FP_9, (FP_8 + FP_SIZE)
87        .set    FP_10, (FP_9 + FP_SIZE)
88        .set    FP_11, (FP_10 + FP_SIZE)
89        .set    FP_12, (FP_11 + FP_SIZE)
90        .set    FP_13, (FP_12 + FP_SIZE)
91        .set    FP_14, (FP_13 + FP_SIZE)
92        .set    FP_15, (FP_14 + FP_SIZE)
93        .set    FP_16, (FP_15 + FP_SIZE)
94        .set    FP_17, (FP_16 + FP_SIZE)
95        .set    FP_18, (FP_17 + FP_SIZE)
96        .set    FP_19, (FP_18 + FP_SIZE)
97        .set    FP_20, (FP_19 + FP_SIZE)
98        .set    FP_21, (FP_20 + FP_SIZE)
99        .set    FP_22, (FP_21 + FP_SIZE)
100        .set    FP_23, (FP_22 + FP_SIZE)
101        .set    FP_24, (FP_23 + FP_SIZE)
102        .set    FP_25, (FP_24 + FP_SIZE)
103        .set    FP_26, (FP_25 + FP_SIZE)
104        .set    FP_27, (FP_26 + FP_SIZE)
105        .set    FP_28, (FP_27 + FP_SIZE)
106        .set    FP_29, (FP_28 + FP_SIZE)
107        .set    FP_30, (FP_29 + FP_SIZE)
108        .set    FP_31, (FP_30 + FP_SIZE)
109        .set    FP_FPSCR, (FP_31 + FP_SIZE)
110
111/*
112 *  _CPU_Context_save_fp_context
113 *
114 *  This routine is responsible for saving the FP context
115 *  at *fp_context_ptr.  If the point to load the FP context
116 *  from is changed then the pointer is modified by this routine.
117 *
118 *  Sometimes a macro implementation of this is in cpu.h which dereferences
119 *  the ** and a similarly named routine in this file is passed something
120 *  like a (Context_Control_fp *).  The general rule on making this decision
121 *  is to avoid writing assembly language.
122 */
123
124        ALIGN (PPC_CACHE_ALIGNMENT, PPC_CACHE_ALIGN_POWER)
125        PUBLIC_PROC (_CPU_Context_save_fp)
126PROC (_CPU_Context_save_fp):
127/* A FP context switch may occur in an ISR or exception handler when the FPU is not
128 * available. Therefore, we must explicitely enable it here!
129 */
130        mfmsr   r4
131        andi.   r5,r4,MSR_FP
132        bne     1f
133        ori     r5,r4,MSR_FP
134        mtmsr   r5
135        isync
1361:
137        lwz     r3, 0(r3)
138        STF     f0, FP_0(r3)
139        STF     f1, FP_1(r3)
140        STF     f2, FP_2(r3)
141        STF     f3, FP_3(r3)
142        STF     f4, FP_4(r3)
143        STF     f5, FP_5(r3)
144        STF     f6, FP_6(r3)
145        STF     f7, FP_7(r3)
146        STF     f8, FP_8(r3)
147        STF     f9, FP_9(r3)
148        STF     f10, FP_10(r3)
149        STF     f11, FP_11(r3)
150        STF     f12, FP_12(r3)
151        STF     f13, FP_13(r3)
152        STF     f14, FP_14(r3)
153        STF     f15, FP_15(r3)
154        STF     f16, FP_16(r3)
155        STF     f17, FP_17(r3)
156        STF     f18, FP_18(r3)
157        STF     f19, FP_19(r3)
158        STF     f20, FP_20(r3)
159        STF     f21, FP_21(r3)
160        STF     f22, FP_22(r3)
161        STF     f23, FP_23(r3)
162        STF     f24, FP_24(r3)
163        STF     f25, FP_25(r3)
164        STF     f26, FP_26(r3)
165        STF     f27, FP_27(r3)
166        STF     f28, FP_28(r3)
167        STF     f29, FP_29(r3)
168        STF     f30, FP_30(r3)
169        STF     f31, FP_31(r3)
170        mffs    f2
171        STF     f2, FP_FPSCR(r3)
172        bne     1f
173        mtmsr   r4
174        isync
1751:
176        blr
177
178/*
179 *  _CPU_Context_restore_fp_context
180 *
181 *  This routine is responsible for restoring the FP context
182 *  at *fp_context_ptr.  If the point to load the FP context
183 *  from is changed then the pointer is modified by this routine.
184 *
185 *  Sometimes a macro implementation of this is in cpu.h which dereferences
186 *  the ** and a similarly named routine in this file is passed something
187 *  like a (Context_Control_fp *).  The general rule on making this decision
188 *  is to avoid writing assembly language.
189 */
190
191        ALIGN (PPC_CACHE_ALIGNMENT, PPC_CACHE_ALIGN_POWER)
192        PUBLIC_PROC (_CPU_Context_restore_fp)
193PROC (_CPU_Context_restore_fp):
194        lwz     r3, 0(r3)
195/* A FP context switch may occur in an ISR or exception handler when the FPU is not
196 * available. Therefore, we must explicitely enable it here!
197 */
198        mfmsr   r4
199        andi.   r5,r4,MSR_FP
200        bne     1f
201        ori     r5,r4,MSR_FP
202        mtmsr   r5
203        isync
2041:
205        LDF     f2, FP_FPSCR(r3)
206        mtfsf   255, f2
207        LDF     f0, FP_0(r3)
208        LDF     f1, FP_1(r3)
209        LDF     f2, FP_2(r3)
210        LDF     f3, FP_3(r3)
211        LDF     f4, FP_4(r3)
212        LDF     f5, FP_5(r3)
213        LDF     f6, FP_6(r3)
214        LDF     f7, FP_7(r3)
215        LDF     f8, FP_8(r3)
216        LDF     f9, FP_9(r3)
217        LDF     f10, FP_10(r3)
218        LDF     f11, FP_11(r3)
219        LDF     f12, FP_12(r3)
220        LDF     f13, FP_13(r3)
221        LDF     f14, FP_14(r3)
222        LDF     f15, FP_15(r3)
223        LDF     f16, FP_16(r3)
224        LDF     f17, FP_17(r3)
225        LDF     f18, FP_18(r3)
226        LDF     f19, FP_19(r3)
227        LDF     f20, FP_20(r3)
228        LDF     f21, FP_21(r3)
229        LDF     f22, FP_22(r3)
230        LDF     f23, FP_23(r3)
231        LDF     f24, FP_24(r3)
232        LDF     f25, FP_25(r3)
233        LDF     f26, FP_26(r3)
234        LDF     f27, FP_27(r3)
235        LDF     f28, FP_28(r3)
236        LDF     f29, FP_29(r3)
237        LDF     f30, FP_30(r3)
238        LDF     f31, FP_31(r3)
239        bne     1f
240        mtmsr   r4
241        isync
2421:
243        blr
244#endif /* PPC_HAS_FPU == 1 */
245
246        ALIGN (PPC_CACHE_ALIGNMENT, PPC_CACHE_ALIGN_POWER)
247        PUBLIC_PROC (_CPU_Context_switch)
248PROC (_CPU_Context_switch):
249
250#ifdef BSP_USE_SYNC_IN_CONTEXT_SWITCH
251        sync
252        isync
253#endif
254
255        /* Align to a cache line */
256        clrrwi  r3, r3, PPC_DEFAULT_CACHE_LINE_POWER
257        clrrwi  r5, r4, PPC_DEFAULT_CACHE_LINE_POWER
258
259        DATA_CACHE_ZERO_AND_TOUCH(r10, PPC_CONTEXT_CACHE_LINE_0)
260        DATA_CACHE_ZERO_AND_TOUCH(r11, PPC_CONTEXT_CACHE_LINE_1)
261
262        /* Save context to r3 */
263
264        mfmsr   r6
265        mflr    r7
266        mfcr    r8
267
268        /*
269         * We have to clear the reservation of the executing thread.  See also
270         * Book E section 6.1.6.2 "Atomic Update Primitives".  Recent GCC
271         * versions use atomic operations in the C++ library for example.  On
272         * SMP configurations the reservation is cleared later during the
273         * context switch.
274         */
275#if PPC_CONTEXT_OFFSET_GPR1 != PPC_CONTEXT_CACHE_LINE_0 \
276  || !BSP_DATA_CACHE_ENABLED \
277  || PPC_DEFAULT_CACHE_LINE_SIZE != 32
278        li      r10, PPC_CONTEXT_OFFSET_GPR1
279#endif
280#ifndef RTEMS_SMP
281        stwcx.  r1, r3, r10
282#endif
283
284        stw     r1, PPC_CONTEXT_OFFSET_GPR1(r3)
285        stw     r6, PPC_CONTEXT_OFFSET_MSR(r3)
286        stw     r7, PPC_CONTEXT_OFFSET_LR(r3)
287        stw     r8, PPC_CONTEXT_OFFSET_CR(r3)
288        PPC_GPR_STORE   r14, PPC_CONTEXT_OFFSET_GPR14(r3)
289        PPC_GPR_STORE   r15, PPC_CONTEXT_OFFSET_GPR15(r3)
290
291#if PPC_CONTEXT_OFFSET_GPR20 == PPC_CONTEXT_CACHE_LINE_2
292        DATA_CACHE_ZERO_AND_TOUCH(r10, PPC_CONTEXT_CACHE_LINE_2)
293#endif
294
295        PPC_GPR_STORE   r16, PPC_CONTEXT_OFFSET_GPR16(r3)
296        PPC_GPR_STORE   r17, PPC_CONTEXT_OFFSET_GPR17(r3)
297
298#if PPC_CONTEXT_OFFSET_GPR26 == PPC_CONTEXT_CACHE_LINE_2
299        DATA_CACHE_ZERO_AND_TOUCH(r10, PPC_CONTEXT_CACHE_LINE_2)
300#endif
301
302        PPC_GPR_STORE   r18, PPC_CONTEXT_OFFSET_GPR18(r3)
303        PPC_GPR_STORE   r19, PPC_CONTEXT_OFFSET_GPR19(r3)
304
305#if PPC_CONTEXT_OFFSET_GPR24 == PPC_CONTEXT_CACHE_LINE_3
306        DATA_CACHE_ZERO_AND_TOUCH(r10, PPC_CONTEXT_CACHE_LINE_3)
307#endif
308
309        PPC_GPR_STORE   r20, PPC_CONTEXT_OFFSET_GPR20(r3)
310        PPC_GPR_STORE   r21, PPC_CONTEXT_OFFSET_GPR21(r3)
311        PPC_GPR_STORE   r22, PPC_CONTEXT_OFFSET_GPR22(r3)
312        PPC_GPR_STORE   r23, PPC_CONTEXT_OFFSET_GPR23(r3)
313
314#if PPC_CONTEXT_OFFSET_GPR28 == PPC_CONTEXT_CACHE_LINE_4
315        DATA_CACHE_ZERO_AND_TOUCH(r10, PPC_CONTEXT_CACHE_LINE_4)
316#endif
317
318        PPC_GPR_STORE   r24, PPC_CONTEXT_OFFSET_GPR24(r3)
319        PPC_GPR_STORE   r25, PPC_CONTEXT_OFFSET_GPR25(r3)
320        PPC_GPR_STORE   r26, PPC_CONTEXT_OFFSET_GPR26(r3)
321        PPC_GPR_STORE   r27, PPC_CONTEXT_OFFSET_GPR27(r3)
322
323        PPC_GPR_STORE   r28, PPC_CONTEXT_OFFSET_GPR28(r3)
324        PPC_GPR_STORE   r29, PPC_CONTEXT_OFFSET_GPR29(r3)
325        PPC_GPR_STORE   r30, PPC_CONTEXT_OFFSET_GPR30(r3)
326        PPC_GPR_STORE   r31, PPC_CONTEXT_OFFSET_GPR31(r3)
327
328        stw     r2, PPC_CONTEXT_OFFSET_GPR2(r3)
329
330#ifdef RTEMS_SMP
331        /* The executing context no longer executes on this processor */
332        msync
333        li      r6, 0
334        stw     r6, PPC_CONTEXT_OFFSET_IS_EXECUTING(r3)
335
336check_is_executing:
337
338        /* Check the is executing indicator of the heir context */
339        addi    r6, r5, PPC_CONTEXT_OFFSET_IS_EXECUTING
340        lwarx   r7, r0, r6
341        cmpwi   r7, 0
342        bne     check_thread_dispatch_necessary
343
344        /* Try to update the is executing indicator of the heir context */
345        li      r7, 1
346        stwcx.  r7, r0, r6
347        bne     check_thread_dispatch_necessary
348        isync
349#endif
350
351        /* Restore context from r5 */
352restore_context:
353
354#ifdef __ALTIVEC__
355        mr      r14, r5
356        .extern _CPU_Context_switch_altivec
357        bl      _CPU_Context_switch_altivec
358        mr      r5, r14
359#endif
360
361        lwz     r1, PPC_CONTEXT_OFFSET_GPR1(r5)
362        lwz     r6, PPC_CONTEXT_OFFSET_MSR(r5)
363        lwz     r7, PPC_CONTEXT_OFFSET_LR(r5)
364        lwz     r8, PPC_CONTEXT_OFFSET_CR(r5)
365
366        PPC_GPR_LOAD    r14, PPC_CONTEXT_OFFSET_GPR14(r5)
367        PPC_GPR_LOAD    r15, PPC_CONTEXT_OFFSET_GPR15(r5)
368
369        DATA_CACHE_TOUCH(r0, r1)
370
371        PPC_GPR_LOAD    r16, PPC_CONTEXT_OFFSET_GPR16(r5)
372        PPC_GPR_LOAD    r17, PPC_CONTEXT_OFFSET_GPR17(r5)
373        PPC_GPR_LOAD    r18, PPC_CONTEXT_OFFSET_GPR18(r5)
374        PPC_GPR_LOAD    r19, PPC_CONTEXT_OFFSET_GPR19(r5)
375
376        PPC_GPR_LOAD    r20, PPC_CONTEXT_OFFSET_GPR20(r5)
377        PPC_GPR_LOAD    r21, PPC_CONTEXT_OFFSET_GPR21(r5)
378        PPC_GPR_LOAD    r22, PPC_CONTEXT_OFFSET_GPR22(r5)
379        PPC_GPR_LOAD    r23, PPC_CONTEXT_OFFSET_GPR23(r5)
380
381        PPC_GPR_LOAD    r24, PPC_CONTEXT_OFFSET_GPR24(r5)
382        PPC_GPR_LOAD    r25, PPC_CONTEXT_OFFSET_GPR25(r5)
383        PPC_GPR_LOAD    r26, PPC_CONTEXT_OFFSET_GPR26(r5)
384        PPC_GPR_LOAD    r27, PPC_CONTEXT_OFFSET_GPR27(r5)
385
386        PPC_GPR_LOAD    r28, PPC_CONTEXT_OFFSET_GPR28(r5)
387        PPC_GPR_LOAD    r29, PPC_CONTEXT_OFFSET_GPR29(r5)
388        PPC_GPR_LOAD    r30, PPC_CONTEXT_OFFSET_GPR30(r5)
389        PPC_GPR_LOAD    r31, PPC_CONTEXT_OFFSET_GPR31(r5)
390
391        lwz     r2, PPC_CONTEXT_OFFSET_GPR2(r5)
392
393        mtcr    r8
394        mtlr    r7
395        mtmsr   r6
396
397#ifdef BSP_USE_SYNC_IN_CONTEXT_SWITCH
398        isync
399#endif
400
401        blr
402
403        PUBLIC_PROC (_CPU_Context_restore)
404PROC (_CPU_Context_restore):
405        /* Align to a cache line */
406        clrrwi  r5, r3, PPC_DEFAULT_CACHE_LINE_POWER
407
408#ifdef __ALTIVEC__
409        li      r3, 0
410#endif
411
412        b       restore_context
413
414#ifdef RTEMS_SMP
415check_thread_dispatch_necessary:
416
417        GET_SELF_CPU_CONTROL    r6
418
419        /* Check if a thread dispatch is necessary */
420        lbz     r7, PER_CPU_DISPATCH_NEEDED(r6)
421        cmpwi   r7, 0
422        beq     check_is_executing
423
424        /* We have a new heir */
425
426        /* Clear the thread dispatch necessary flag */
427        li      r7, 0
428        stb     r7, PER_CPU_DISPATCH_NEEDED(r6)
429        msync
430
431        /* Read the executing and heir */
432        lwz     r7, PER_CPU_OFFSET_EXECUTING(r6)
433        lwz     r8, PER_CPU_OFFSET_HEIR(r6)
434
435        /* Calculate the heir context pointer */
436        sub     r7, r4, r7
437        add     r4, r8, r7
438        clrrwi  r5, r4, PPC_DEFAULT_CACHE_LINE_POWER
439
440        /* Update the executing */
441        stw     r8, PER_CPU_OFFSET_EXECUTING(r6)
442
443        b       check_is_executing
444#endif
Note: See TracBrowser for help on using the repository browser.