source: rtems/cpukit/score/cpu/mips/cpu.c

Last change on this file was bf10d5b, checked in by Joel Sherrill <joel@…>, on 02/16/22 at 22:53:36

score/cpu/mips: Change license to BSD-2

Updates #3053.

  • Property mode set to 100644
File size: 8.1 KB
RevLine 
[bf10d5b]1/* SPDX-License-Identifier: BSD-2-Clause */
2
[0c0181d]3/**
4 *  @file
[e0f91da]5 *
6 *  @brief MIPS CPU Dependent Source
7 */
8
9/*
10 * MIPS CPU Dependent Source
[f198c63]11 *
[bd1ecb0]12 *  2002:       Greg Menke (gregory.menke@gsfc.nasa.gov)
13 *      Overhauled interrupt level and interrupt enable/disable code
14 *      to more exactly support MIPS.  Our mods were for MIPS1 processors
15 *      MIPS3 ports are affected, though apps written to the old behavior
16 *      should still work OK.
17 *
[fda47cd]18 *  Conversion to MIPS port by Alan Cudmore <alanc@linuxstart.com> and
[5bb38e15]19 *           Joel Sherrill <joel@OARcorp.com>.
[aa7f8a1f]20 *
21 *    These changes made the code conditional on standard cpp predefines,
22 *    merged the mips1 and mips3 code sequences as much as possible,
23 *    and moved some of the assembly code to C.  Alan did much of the
24 *    initial analysis and rework.  Joel took over from there and
25 *    wrote the JMR3904 BSP so this could be tested.  Joel also
26 *    added the new interrupt vectoring support in libcpu and
27 *    tried to better support the various interrupt controllers.
[e0f91da]28 *
[fda47cd]29 *  Original MIP64ORION port by Craig Lebakken <craigl@transition.com>
30 *           COPYRIGHT (c) 1996 by Transition Networks Inc.
[f198c63]31 *
[fda47cd]32 *         To anyone who acknowledges that this file is provided "AS IS"
33 *         without any express or implied warranty:
34 *             permission to use, copy, modify, and distribute this file
35 *             for any purpose is hereby granted without fee, provided that
36 *             the above copyright notice and this notice appears in all
37 *             copies, and that the name of Transition Networks not be used in
38 *             advertising or publicity pertaining to distribution of the
39 *             software without specific, written prior permission.
[5bb38e15]40 *             Transition Networks makes no representations about the
[fda47cd]41 *             suitability of this software for any purpose.
[f198c63]42 *
[0c0181d]43 *  COPYRIGHT (c) 1989-2012.
[f198c63]44 *  On-Line Applications Research Corporation (OAR).
45 *
[bf10d5b]46 * Redistribution and use in source and binary forms, with or without
47 * modification, are permitted provided that the following conditions
48 * are met:
49 * 1. Redistributions of source code must retain the above copyright
50 *    notice, this list of conditions and the following disclaimer.
51 * 2. Redistributions in binary form must reproduce the above copyright
52 *    notice, this list of conditions and the following disclaimer in the
53 *    documentation and/or other materials provided with the distribution.
54 *
55 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
56 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
59 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
60 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
61 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
62 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
63 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
64 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
65 * POSSIBILITY OF SUCH DAMAGE.
[f198c63]66 */
67
[febaa8a]68#ifdef HAVE_CONFIG_H
69#include "config.h"
70#endif
71
[8b65b574]72#include <rtems/score/cpuimpl.h>
[f198c63]73#include <rtems/score/isr.h>
74
[d638aca]75#if CPU_HARDWARE_FP
76Context_Control_fp _CPU_Null_fp_context;
77#endif
78
[5bb38e15]79/*
80** Exception stack frame pointer used in cpu_asm to pass the exception stack frame
[7c99007]81** address to the context switch code.
[8264d23]82*/
[25571ae]83#if (__mips == 1) || (__mips == 32)
[7c99007]84typedef uint32_t ESF_PTR_TYPE;
[5bb38e15]85#elif (__mips == 3)
[7c99007]86typedef uint64_t ESF_PTR_TYPE;
87#else
88#error "unknown MIPS ISA"
89#endif
[8264d23]90
[5bb38e15]91ESF_PTR_TYPE __exceptionStackFrame = 0;
[f198c63]92/*  _CPU_Initialize
93 *
94 *  This routine performs processor dependent initialization.
95 *
[383871ac]96 *    thread_dispatch - address of dispatching routine
[f198c63]97 */
[c03e2bc]98
99void _CPU_Initialize(void)
[f198c63]100{
101  /*
102   *  If there is not an easy way to initialize the FP context
103   *  during Context_Initialize, then it is usually easier to
104   *  save an "uninitialized" FP context here and copy it to
105   *  the task's during Context_Initialize.
106   */
107
[a3de1b48]108#if CPU_HARDWARE_FP
[f198c63]109  /* FP context initialization support goes here */
[a3de1b48]110  _CPU_Null_fp_context.fpcs = 0x1000000;        /* Set FS flag in floating point coprocessor
111                                                   control register to prevent underflow and
112                                                   inexact exceptions */
113#endif
[f198c63]114}
115
[8b65b574]116void _CPU_Fatal_halt( uint32_t source, CPU_Uint32ptr error )
117{
118  ISR_Level level;
119
120  _CPU_ISR_Disable( level );
121  (void) level;
122
123  while ( true ) {
124    /* Do nothing */
125  }
126}
127
[c346f33d]128uint32_t   _CPU_ISR_Get_level( void )
[f198c63]129{
[32f415d]130  unsigned int sr;
131
132  mips_get_sr(sr);
133
[5194a28]134  /* printf("current sr=%08X, ",sr); */
135
136#if (__mips == 3) || (__mips == 32)
137/* IE bit and shift down hardware ints into bits 1 thru 6 */
[7c99007]138  sr = (sr & SR_IE) | ((sr & mips_interrupt_mask()) >> 9);
[2e549dad]139
140#elif __mips == 1
[e6dec71c]141/* IEC bit and shift down hardware ints into bits 1 thru 6 */
[7c99007]142  sr = (sr & SR_IEC) | ((sr & mips_interrupt_mask()) >> 9);
[2e549dad]143
144#else
145#error "CPU ISR level: unknown MIPS level for SR handling"
146#endif
[bd1ecb0]147  return sr;
[f198c63]148}
[c346f33d]149void _CPU_ISR_Set_level( uint32_t   new_level )
[2e549dad]150{
[e6dec71c]151  unsigned int sr, srbits;
152
[5bb38e15]153  /*
154  ** mask off the int level bits only so we can
[e6dec71c]155  ** preserve software int settings and FP enable
156  ** for this thread.  Note we don't force software ints
157  ** enabled when changing level, they were turned on
[5bb38e15]158  ** when this task was created, but may have been turned
[e6dec71c]159  ** off since, so we'll just leave them alone.
160  */
161
[bd1ecb0]162  new_level &= 0xff;
[2e549dad]163
164  mips_get_sr(sr);
165
[5194a28]166#if (__mips == 3) || (__mips == 32)
[bd1ecb0]167  mips_set_sr( (sr & ~SR_IE) );                 /* first disable ie bit (recommended) */
[e6dec71c]168
[5194a28]169   srbits = sr & ~(0xfc00 | SR_IE);
[e6dec71c]170
[7c99007]171   sr = srbits | ((new_level==0)? (mips_interrupt_mask() | SR_IE): \
172                 (((new_level<<9) & mips_interrupt_mask()) | \
[5194a28]173                   ((new_level & 1)?SR_IE:0)));
[e6dec71c]174/*
[2e549dad]175  if ( (new_level & SR_EXL) == (sr & SR_EXL) )
176    return;
177
178  if ( (new_level & SR_EXL) == 0 ) {
[e6dec71c]179    sr &= ~SR_EXL;                    * clear the EXL bit *
[2e549dad]180    mips_set_sr(sr);
181  } else {
182
[e6dec71c]183    sr |= SR_EXL|SR_IE;              * enable exception level *
184    mips_set_sr(sr);                 * first disable ie bit (recommended) *
[2e549dad]185  }
[e6dec71c]186*/
[5bb38e15]187
[2e549dad]188#elif __mips == 1
[bd1ecb0]189  mips_set_sr( (sr & ~SR_IEC) );
[e6dec71c]190  srbits = sr & ~(0xfc00 | SR_IEC);
[bd1ecb0]191  sr = srbits | ((new_level==0)?0xfc01:( ((new_level<<9) & 0xfc00) | \
192                                         (new_level & SR_IEC)));
[32f415d]193#else
194#error "CPU ISR level: unknown MIPS level for SR handling"
[f198c63]195#endif
[e6dec71c]196  mips_set_sr( sr );
[2e549dad]197}
198
[a0cb87c]199void _CPU_Context_Initialize(
200  Context_Control  *the_context,
201  uintptr_t        *stack_base,
202  uint32_t          size,
203  uint32_t          new_level,
204  void             *entry_point,
[022851a]205  bool              is_fp,
206  void             *tls_area
[a0cb87c]207)
208{
209  uintptr_t             stack_tmp;
210  __MIPS_REGISTER_TYPE  intlvl = new_level & 0xff;
[335e5ca]211  __MIPS_REGISTER_TYPE  c0_sr;
[a0cb87c]212
213  stack_tmp  = (uintptr_t)stack_base;
214  stack_tmp += ((size) - CPU_STACK_ALIGNMENT);
215  stack_tmp &= (__MIPS_REGISTER_TYPE) ~(CPU_STACK_ALIGNMENT - 1);
216
217  the_context->sp = (__MIPS_REGISTER_TYPE) stack_tmp;
218  the_context->fp = (__MIPS_REGISTER_TYPE) stack_tmp;
219  the_context->ra = (__MIPS_REGISTER_TYPE) (uintptr_t)entry_point;
[335e5ca]220
221  c0_sr =
[a0cb87c]222    ((intlvl==0)? (mips_interrupt_mask() | 0x300 | _INTON):
223      ( ((intlvl<<9) & mips_interrupt_mask()) | 0x300 |
224      ((intlvl & 1)?_INTON:0)) ) |
[335e5ca]225      SR_CU0 | _EXTRABITS;
226#if MIPS_HAS_FPU == 1
227  if ( is_fp ) {
228    c0_sr |= SR_CU1;
229  }
230#endif
231  the_context->c0_sr = c0_sr;
[a0cb87c]232}
[7c2f2448]233/*
[f198c63]234 *  _CPU_Internal_threads_Idle_thread_body
235 *
236 *  NOTES:
237 *
238 *  1. This is the same as the regular CPU independent algorithm.
239 *
240 *  2. If you implement this using a "halt", "idle", or "shutdown"
241 *     instruction, then don't forget to put it in an infinite loop.
242 *
243 *  3. Be warned. Some processors with onboard DMA have been known
244 *     to stop the DMA if the CPU were put in IDLE mode.  This might
245 *     also be a problem with other on-chip peripherals.  So use this
246 *     hook with caution.
247 */
248
[cca8379]249void *_CPU_Thread_Idle_body( uintptr_t ignored )
[f198c63]250{
[5194a28]251#if (__mips == 3) || (__mips == 32)
[32f415d]252   for( ; ; )
[f8958d9]253     __asm__ volatile("wait"); /* use wait to enter low power mode */
[32f415d]254#elif __mips == 1
255   for( ; ; )
256     ;
257#else
258#error "IDLE: __mips not set to 1 or 3"
[f198c63]259#endif
[32f415d]260}
Note: See TracBrowser for help on using the repository browser.