source: rtems/c/src/exec/score/cpu/powerpc/cpu.c @ 993e1b5

4.104.114.84.95
Last change on this file since 993e1b5 was 993e1b5, checked in by Joel Sherrill <joel.sherrill@…>, on 04/14/98 at 19:54:24

Refreshing effort from Avenger.

  • Property mode set to 100644
File size: 14.9 KB
Line 
1/*
2 *  PowerPC CPU Dependent Source
3 *
4 *  Author:     Andrew Bray <andy@i-cubed.co.uk>
5 *
6 *  COPYRIGHT (c) 1995 by i-cubed ltd.
7 *
8 *  To anyone who acknowledges that this file is provided "AS IS"
9 *  without any express or implied warranty:
10 *      permission to use, copy, modify, and distribute this file
11 *      for any purpose is hereby granted without fee, provided that
12 *      the above copyright notice and this notice appears in all
13 *      copies, and that the name of i-cubed limited not be used in
14 *      advertising or publicity pertaining to distribution of the
15 *      software without specific, written prior permission.
16 *      i-cubed limited makes no representations about the suitability
17 *      of this software for any purpose.
18 *
19 *  Derived from c/src/exec/cpu/no_cpu/cpu.c:
20 *
21 *  COPYRIGHT (c) 1989-1997.
22 *  On-Line Applications Research Corporation (OAR).
23 *  Copyright assigned to U.S. Government, 1994.
24 *
25 *  The license and distribution terms for this file may be found in
26 *  the file LICENSE in this distribution or at
27 *  http://www.OARcorp.com/rtems/license.html.
28 *
29 *  $Id$
30 */
31
32#include <rtems/system.h>
33#include <rtems/score/isr.h>
34#include <rtems/score/context.h>
35#include <rtems/score/thread.h>
36
37/*
38 *  These are for testing purposes.
39 */
40
41/*  _CPU_Initialize
42 *
43 *  This routine performs processor dependent initialization.
44 *
45 *  INPUT PARAMETERS:
46 *    cpu_table       - CPU table to initialize
47 *    thread_dispatch - address of disptaching routine
48 */
49
50static void ppc_spurious(int, CPU_Interrupt_frame *);
51
52void _CPU_Initialize(
53  rtems_cpu_table  *cpu_table,
54  void      (*thread_dispatch)      /* ignored on this CPU */
55)
56{
57  proc_ptr handler = (proc_ptr)ppc_spurious;
58  int i;
59#if (PPC_ABI != PPC_ABI_POWEROPEN)
60  register unsigned32 r2 = 0;
61#if (PPC_ABI != PPC_ABI_GCC27)
62  register unsigned32 r13 = 0;
63
64  asm ("mr %0,13" : "=r" ((r13)) : "0" ((r13)));
65  _CPU_IRQ_info.Default_r13 = r13;
66#endif
67
68  asm ("mr %0,2" : "=r" ((r2)) : "0" ((r2)));
69  _CPU_IRQ_info.Default_r2 = r2;
70#endif
71
72  _CPU_IRQ_info.Nest_level = &_ISR_Nest_level;
73  _CPU_IRQ_info.Disable_level = &_Thread_Dispatch_disable_level;
74  _CPU_IRQ_info.Vector_table = _ISR_Vector_table;
75#if (PPC_ABI == PPC_ABI_POWEROPEN)
76  _CPU_IRQ_info.Dispatch_r2 = ((unsigned32 *)_Thread_Dispatch)[1];
77#endif
78  _CPU_IRQ_info.Switch_necessary = &_Context_Switch_necessary;
79  _CPU_IRQ_info.Signal = &_ISR_Signals_to_thread_executing;
80
81#if (PPC_USE_SPRG)
82  i = (int)&_CPU_IRQ_info;
83  asm volatile("mtspr 0x113, %0" : "=r" (i) : "0" (i)); /* SPRG 3 */
84#endif
85
86  /*
87   * Store Msr Value in the IRQ info structure.
88   */
89   _CPU_MSR_Value(_CPU_IRQ_info.msr_initial);
90 
91#if (PPC_USE_SPRG)
92  i = _CPU_IRQ_info.msr_initial;
93  asm volatile("mtspr 0x112, %0" : "=r" (i) : "0" (i)); /* SPRG 2 */
94#endif
95
96  if ( cpu_table->spurious_handler )
97    handler = (proc_ptr)cpu_table->spurious_handler;
98
99  for (i = 0; i < PPC_INTERRUPT_MAX;  i++)
100    _ISR_Vector_table[i] = handler;
101
102  _CPU_Table = *cpu_table;
103}
104
105/*PAGE
106 *
107 *  _CPU_ISR_Calculate_level
108 *
109 *  The PowerPC puts its interrupt enable status in the MSR register
110 *  which also contains things like endianness control.  To be more
111 *  awkward, the layout varies from processor to processor.  This
112 *  is why it was necessary to adopt a scheme which allowed the user
113 *  to specify specifically which interrupt sources were enabled.
114 */
115 
116unsigned32 _CPU_ISR_Calculate_level(
117  unsigned32 new_level
118)
119{
120  register unsigned32 new_msr = 0;
121
122  /*
123   *  Set the critical interrupt enable bit
124   */
125
126#if (PPC_HAS_RFCI)
127  if ( !(new_level & PPC_INTERRUPT_LEVEL_CE) )
128    new_msr |= PPC_MSR_CE;
129#endif
130
131  if ( !(new_level & PPC_INTERRUPT_LEVEL_ME) )
132    new_msr |= PPC_MSR_ME;
133
134  if ( !(new_level & PPC_INTERRUPT_LEVEL_EE) )
135    new_msr |= PPC_MSR_EE;
136
137  return new_msr;
138}
139
140/*PAGE
141 *
142 *  _CPU_ISR_Set_level
143 *
144 *  This routine sets the requested level in the MSR.
145 */
146
147void _CPU_ISR_Set_level(
148  unsigned32 new_level
149)
150{
151  register unsigned32 tmp = 0;
152  register unsigned32 new_msr;
153
154  new_msr = _CPU_ISR_Calculate_level( new_level );
155
156  asm volatile (
157    "mfmsr %0; andc %0,%0,%1; and %2, %2, %1; or %0, %0, %2; mtmsr %0" :
158    "=&r" ((tmp)) :
159    "r" ((PPC_MSR_DISABLE_MASK)), "r" ((new_msr)), "0" ((tmp))
160  );
161}
162
163/*PAGE
164 *
165 *  _CPU_ISR_Get_level
166 *
167 *  This routine gets the current interrupt level from the MSR and
168 *  converts it to an RTEMS interrupt level.
169 */
170
171unsigned32 _CPU_ISR_Get_level( void )
172{
173  unsigned32 level = 0;
174  unsigned32 msr;
175 
176  asm volatile("mfmsr %0" : "=r" ((msr)));
177 
178  msr &= PPC_MSR_DISABLE_MASK;
179
180  /*
181   *  Set the critical interrupt enable bit
182   */
183
184#if (PPC_HAS_RFCI)
185  if ( !(msr & PPC_MSR_CE) )
186    level |= PPC_INTERRUPT_LEVEL_CE;
187#endif
188
189  if ( !(msr & PPC_MSR_ME) )
190    level |= PPC_INTERRUPT_LEVEL_ME;
191
192  if ( !(msr & PPC_MSR_EE) )
193    level |= PPC_INTERRUPT_LEVEL_EE;
194
195  return level;
196}
197
198/*PAGE
199 *
200 *  _CPU_Context_Initialize
201 */
202
203#if (PPC_ABI == PPC_ABI_POWEROPEN)
204#define CPU_MINIMUM_STACK_FRAME_SIZE 56
205#else /* PPC_ABI_SVR4 or PPC_ABI_EABI */
206#define CPU_MINIMUM_STACK_FRAME_SIZE 8
207#endif
208
209void _CPU_Context_Initialize(
210  Context_Control  *the_context,
211  unsigned32       *stack_base,
212  unsigned32        size,
213  unsigned32        new_level,
214  void             *entry_point,
215  boolean           is_fp
216)
217{
218  unsigned32 msr_value;
219  unsigned32 sp;
220
221  sp = (unsigned32)stack_base + size - CPU_MINIMUM_STACK_FRAME_SIZE;
222  *((unsigned32 *)sp) = 0;
223  the_context->gpr1 = sp;
224   
225  the_context->msr = _CPU_ISR_Calculate_level( new_level );
226
227  /*
228   *  The FP bit of the MSR should only be enabled if this is a floating
229   *  point task.  Unfortunately, the vfprintf_r routine in newlib
230   *  ends up pushing a floating point register regardless of whether or
231   *  not a floating point number is being printed.  Serious restructuring
232   *  of vfprintf.c will be required to avoid this behavior.  At this
233   *  time (7 July 1997), this restructuring is not being done.
234   */
235
236  /*if ( is_fp ) */
237    the_context->msr |= PPC_MSR_FP;
238
239  /*
240   *  Calculate the task's MSR value:
241   *
242   *     + Set the exception prefix bit to point to the exception table
243   *     + Force the RI bit
244   *     + Use the DR and IR bits
245   */
246  _CPU_MSR_Value( msr_value );
247  the_context->msr |= (msr_value & PPC_MSR_EP);
248  the_context->msr |= PPC_MSR_RI;
249  the_context->msr |= msr_value & (PPC_MSR_DR|PPC_MSR_IR);
250
251#if (PPC_ABI == PPC_ABI_POWEROPEN)
252  { unsigned32 *desc = (unsigned32 *)entry_point;
253
254    the_context->pc = desc[0];
255    the_context->gpr2 = desc[1];
256  }
257#endif
258
259#if (PPC_ABI == PPC_ABI_SVR4)
260  { unsigned    r13 = 0;
261    asm volatile ("mr %0, 13" : "=r" ((r13)));
262   
263    the_context->pc = (unsigned32)entry_point;
264    the_context->gpr13 = r13;
265  }
266#endif
267
268#if (PPC_ABI == PPC_ABI_EABI)
269  { unsigned32  r2 = 0;
270    unsigned    r13 = 0;
271    asm volatile ("mr %0,2; mr %1,13" : "=r" ((r2)), "=r" ((r13)));
272 
273    the_context->pc = (unsigned32)entry_point;
274    the_context->gpr2 = r2;
275    the_context->gpr13 = r13;
276  }
277#endif
278}
279
280
281/*  _CPU_ISR_install_vector
282 *
283 *  This kernel routine installs the RTEMS handler for the
284 *  specified vector.
285 *
286 *  Input parameters:
287 *    vector      - interrupt vector number
288 *    old_handler - former ISR for this vector number
289 *    new_handler - replacement ISR for this vector number
290 *
291 *  Output parameters:  NONE
292 *
293 */
294
295void _CPU_ISR_install_vector(
296  unsigned32  vector,
297  proc_ptr    new_handler,
298  proc_ptr   *old_handler
299)
300{
301   proc_ptr   ignored;
302   *old_handler = _ISR_Vector_table[ vector ];
303
304   /*
305    *  If the interrupt vector table is a table of pointer to isr entry
306    *  points, then we need to install the appropriate RTEMS interrupt
307    *  handler for this vector number.
308    */
309
310   /*
311    * Install the wrapper so this ISR can be invoked properly.
312    */
313   if (_CPU_Table.exceptions_in_RAM)
314      _CPU_ISR_install_raw_handler( vector, _ISR_Handler, &ignored );
315
316   /*
317    *  We put the actual user ISR address in '_ISR_vector_table'.  This will
318    *  be used by the _ISR_Handler so the user gets control.
319    */
320
321    _ISR_Vector_table[ vector ] = new_handler ? (ISR_Handler_entry)new_handler :
322       _CPU_Table.spurious_handler ?
323          (ISR_Handler_entry)_CPU_Table.spurious_handler :
324          (ISR_Handler_entry)ppc_spurious;
325}
326
327/*PAGE
328 *
329 *  _CPU_Install_interrupt_stack
330 */
331
332void _CPU_Install_interrupt_stack( void )
333{
334#if (PPC_ABI == PPC_ABI_POWEROPEN || PPC_ABI == PPC_ABI_GCC27)
335  _CPU_IRQ_info.Stack = _CPU_Interrupt_stack_high - 56;
336#else
337  _CPU_IRQ_info.Stack = _CPU_Interrupt_stack_high - 8;
338#endif
339}
340
341/* Handle a spurious interrupt */
342static void ppc_spurious(int v, CPU_Interrupt_frame *i)
343{
344#if 0
345    printf("Spurious interrupt on vector %d from %08.8x\n",
346           v, i->pc);
347#endif
348#ifdef ppc403
349    if (v == PPC_IRQ_EXTERNAL)
350        {
351            register int r = 0;
352
353            asm volatile("mtdcr 0x42, %0" :
354                "=&r" ((r)) : "0" ((r))); /* EXIER */
355        }
356    else if (v == PPC_IRQ_PIT)
357        {
358            register int r = 0x08000000;
359
360            asm volatile("mtspr 0x3d8, %0" :
361                "=&r" ((r)) : "0" ((r))); /* TSR */
362        }
363    else if (v == PPC_IRQ_FIT)
364        {
365            register int r = 0x04000000;
366
367            asm volatile("mtspr 0x3d8, %0" :
368                "=&r" ((r)) : "0" ((r))); /* TSR */
369        }
370#endif
371}
372
373void _CPU_Fatal_error(unsigned32 _error)
374{
375  asm volatile ("mr 3, %0" : : "r" ((_error)));
376  asm volatile ("tweq 5,5");
377  asm volatile ("li 0,0; mtmsr 0");
378  while (1) ;
379}
380
381#define PPC_SYNCHRONOUS_TRAP_BIT_MASK    0x100
382#define PPC_ASYNCHRONOUS_TRAP( _trap ) (_trap)
383#define PPC_SYNCHRONOUS_TRAP ( _trap ) ((_trap)+PPC_SYNCHRONOUS_TRAP_BIT_MASK)
384#define PPC_REAL_TRAP_NUMBER ( _trap ) ((_trap)%PPC_SYNCHRONOUS_TRAP_BIT_MASK)
385
386
387const CPU_Trap_table_entry _CPU_Trap_slot_template = {
388
389#if (PPC_ABI == PPC_ABI_POWEROPEN || PPC_ABI == PPC_ABI_GCC27)
390#error " Vector install not tested."
391#if (PPC_HAS_FPU)
392#error " Vector install not tested."
393  0x9421feb0,           /* stwu r1, -(20*4 + 18*8 + IP_END)(r1) */
394#else
395#error " Vector install not tested."
396  0x9421ff40,           /* stwu    r1, -(20*4 + IP_END)(r1)     */
397#endif
398#else
399  0x9421ff90,           /* stwu    r1, -(IP_END)(r1)            */
400#endif
401
402  0x90010008,           /* stw   %r0, IP_0(%r1)                 */
403  0x38000000,           /* li    %r0, PPC_IRQ                   */
404  0x48000002            /* ba    PROC (_ISR_Handler)            */
405};
406
407unsigned32  ppc_exception_vector_addr(
408  unsigned32 vector
409);
410
411
412/*PAGE
413 *
414 *  _CPU_ISR_install_raw_handler
415 *
416 *  This routine installs the specified handler as a "raw" non-executive
417 *  supported trap handler (a.k.a. interrupt service routine).
418 *
419 *  Input Parameters:
420 *    vector      - trap table entry number plus synchronous
421 *                    vs. asynchronous information
422 *    new_handler - address of the handler to be installed
423 *    old_handler - pointer to an address of the handler previously installed
424 *
425 *  Output Parameters: NONE
426 *    *new_handler - address of the handler previously installed
427 *
428 *  NOTE:
429 *
430 *  This routine is based on the SPARC routine _CPU_ISR_install_raw_handler.
431 *  Install a software trap handler as an executive interrupt handler
432 *  (which is desirable since RTEMS takes care of window and register issues),
433 *  then the executive needs to know that the return address is to the trap
434 *  rather than the instruction following the trap.
435 *
436 */
437 
438void _CPU_ISR_install_raw_handler(
439  unsigned32  vector,
440  proc_ptr    new_handler,
441  proc_ptr   *old_handler
442)
443{
444  unsigned32             real_vector;
445  CPU_Trap_table_entry  *slot;
446  unsigned32             u32_handler=0;
447
448  /*
449   *  Get the "real" trap number for this vector ignoring the synchronous
450   *  versus asynchronous indicator included with our vector numbers.
451   */
452
453  real_vector = vector;
454
455  /*
456   *  Get the current base address of the trap table and calculate a pointer
457   *  to the slot we are interested in.
458   */
459  slot = (CPU_Trap_table_entry  *)ppc_exception_vector_addr( real_vector );
460
461  /*
462   *  Get the address of the old_handler from the trap table.
463   *
464   *  NOTE: The old_handler returned will be bogus if it does not follow
465   *        the RTEMS model.
466   */
467
468#define HIGH_BITS_MASK   0xFFFFFC00
469#define HIGH_BITS_SHIFT  10
470#define LOW_BITS_MASK    0x000003FF
471
472  if (slot->stwu_r1 == _CPU_Trap_slot_template.stwu_r1) {
473    /*
474     * Set u32_handler = to target address 
475     */
476    u32_handler = slot->b_Handler & 0x03fffffc;
477    *old_handler =  (proc_ptr) u32_handler;
478  } else
479    *old_handler = 0;
480
481  /*
482   *  Copy the template to the slot and then fix it.
483   */
484  *slot = _CPU_Trap_slot_template;
485
486  u32_handler = (unsigned32) new_handler;
487  slot->b_Handler |= u32_handler;
488
489  slot->li_r0_IRQ  |= vector;
490
491  _CPU_Data_Cache_Block_Flush( slot );
492}
493
494unsigned32  ppc_exception_vector_addr(
495  unsigned32 vector
496)
497{
498  unsigned32 Msr;
499  unsigned32 Top = 0;
500  unsigned32 Offset = 0x000;
501
502  _CPU_MSR_Value ( Msr );
503  if ( ( Msr & PPC_MSR_EP) != 0 ) /* Vectors at FFFx_xxxx */
504    Top = 0xfff00000;
505
506  switch ( vector ) {
507    case PPC_IRQ_SYSTEM_RESET:   /* on 40x aka PPC_IRQ_CRIT */
508      Offset = 0x00100;
509      break;
510    case PPC_IRQ_MCHECK:
511      Offset = 0x00200;
512      break;
513    case PPC_IRQ_PROTECT:
514      Offset = 0x00300;
515      break;
516    case PPC_IRQ_ISI:
517      Offset = 0x00400;
518      break;
519    case PPC_IRQ_EXTERNAL:
520      Offset = 0x00500;
521      break;
522    case PPC_IRQ_ALIGNMENT:
523      Offset = 0x00600;
524      break;
525    case PPC_IRQ_PROGRAM:
526      Offset = 0x00700;
527      break;
528    case PPC_IRQ_NOFP:
529      Offset = 0x00800;
530      break;
531    case PPC_IRQ_DECREMENTER:
532      Offset = 0x00900;
533      break;
534    case PPC_IRQ_RESERVED_A:
535      Offset = 0x00a00;
536      break;
537    case PPC_IRQ_RESERVED_B:
538      Offset = 0x00b00;
539      break;
540    case PPC_IRQ_SCALL:
541      Offset = 0x00c00;
542      break;
543    case PPC_IRQ_TRACE:
544      Offset = 0x00d00;
545      break;
546    case PPC_IRQ_FP_ASST:
547      Offset = 0x00e00;
548      break;
549
550#if defined(ppc403)
551                                 
552/*  PPC_IRQ_CRIT is the same vector as PPC_IRQ_RESET
553    case PPC_IRQ_CRIT:
554      Offset = 0x00100;
555      break;
556*/
557    case PPC_IRQ_PIT:
558      Offset = 0x01000;
559      break;
560    case PPC_IRQ_FIT:
561      Offset = 0x01010;
562      break;
563    case PPC_IRQ_WATCHDOG:
564      Offset = 0x01020;
565      break;
566    case PPC_IRQ_DEBUG:
567      Offset = 0x02000;
568      break;
569
570#elif defined(ppc601)
571    case PPC_IRQ_TRACE:
572      Offset = 0x02000;
573      break;
574
575#elif defined(ppc603)
576    case PPC_IRQ_TRANS_MISS:
577      Offset = 0x1000;
578      break;
579    case PPC_IRQ_DATA_LOAD:
580      Offset = 0x1100;
581      break;
582    case PPC_IRQ_DATA_STORE:
583      Offset = 0x1200;
584      break;
585    case PPC_IRQ_ADDR_BRK:
586      Offset = 0x1300;
587      break;
588    case PPC_IRQ_SYS_MGT:
589      Offset = 0x1400;
590      break;
591
592#elif defined(ppc603e)
593    case PPC_TLB_INST_MISS:
594      Offset = 0x1000;
595      break;
596    case PPC_TLB_LOAD_MISS:
597      Offset = 0x1100;
598      break;
599    case PPC_TLB_STORE_MISS:
600      Offset = 0x1200;
601      break;
602    case PPC_IRQ_ADDRBRK:
603      Offset = 0x1300;
604      break;
605    case PPC_IRQ_SYS_MGT:
606      Offset = 0x1400;
607      break;
608
609#elif defined(ppc604)
610    case PPC_IRQ_ADDR_BRK:
611      Offset = 0x1300;
612      break;
613    case PPC_IRQ_SYS_MGT:
614      Offset = 0x1400;
615      break;
616#endif
617
618  }
619  Top += Offset;
620  return Top;
621}
622
Note: See TracBrowser for help on using the repository browser.