summaryrefslogtreecommitdiffstats
path: root/cpukit/score/src/smp.c
blob: 35e8a01b90e6995ed2b6b2a0d3d9cb4471ccc936 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/**
 *  @file
 *
 *  @brief SMP Support
 *  @ingroup Score
 */

/*
 *  COPYRIGHT (c) 1989-2011.
 *  On-Line Applications Research Corporation (OAR).
 *
 *  The license and distribution terms for this file may be
 *  found in the file LICENSE in this distribution or at
 *  http://www.rtems.com/license/LICENSE.
 */

#if HAVE_CONFIG_H
#include "config.h"
#endif

#include <rtems/bspsmp.h>
#include <rtems/score/thread.h>
#include <rtems/score/threaddispatch.h>
#include <rtems/score/smp.h>
#include <rtems/score/sysstate.h>

#if defined(RTEMS_DEBUG)
  #include <rtems/bspIo.h>
#endif

void rtems_smp_secondary_cpu_initialize( void )
{
  Per_CPU_Control *self_cpu = _Per_CPU_Get();
  Thread_Control  *heir;

  #if defined(RTEMS_DEBUG)
    printk( "Made it to %d -- ", _Per_CPU_Get_index( self_cpu ) );
  #endif

  _Per_CPU_Change_state( self_cpu, PER_CPU_STATE_READY_TO_BEGIN_MULTITASKING );

  _Per_CPU_Wait_for_state( self_cpu, PER_CPU_STATE_BEGIN_MULTITASKING );

  _Per_CPU_Change_state( self_cpu, PER_CPU_STATE_UP );

  /*
   *  The Scheduler will have selected the heir thread for each CPU core.
   *  Now we have been requested to perform the first context switch.  So
   *  force a switch to the designated heir and make it executing on
   *  THIS core.
   */
  heir = self_cpu->heir;
  heir->is_executing = true;
  self_cpu->executing->is_executing = false;
  self_cpu->executing = heir;
  self_cpu->dispatch_necessary = false;

  /*
   * Threads begin execution in the _Thread_Handler() function.   This function
   * will call _Thread_Enable_dispatch().
   */
  _Thread_Disable_dispatch();

  _CPU_Context_switch_to_first_task_smp( &heir->Registers );
}

void rtems_smp_process_interrupt( void )
{
  Per_CPU_Control *self_cpu = _Per_CPU_Get();


  if ( self_cpu->message != 0 ) {
    uint32_t  message;
    ISR_Level level;

    _Per_CPU_Lock_acquire( self_cpu, level );
    message = self_cpu->message;
    self_cpu->message = 0;
    _Per_CPU_Lock_release( self_cpu, level );

    #if defined(RTEMS_DEBUG)
      {
        void *sp = __builtin_frame_address(0);
        if ( !(message & RTEMS_BSP_SMP_SHUTDOWN) ) {
          printk(
            "ISR on CPU %d -- (0x%02x) (0x%p)\n",
            _Per_CPU_Get_index( self_cpu ),
            message,
            sp
          );
          if ( message & RTEMS_BSP_SMP_SIGNAL_TO_SELF )
            printk( "signal to self\n" );
          if ( message & RTEMS_BSP_SMP_SHUTDOWN )
            printk( "shutdown\n" );
        }
        printk( "Dispatch level %d\n", _Thread_Dispatch_get_disable_level() );
      }
    #endif

    if ( ( message & RTEMS_BSP_SMP_SHUTDOWN ) != 0 ) {
      _ISR_Disable( level );

      _Thread_Dispatch_set_disable_level( 0 );

      _Per_CPU_Change_state( self_cpu, PER_CPU_STATE_SHUTDOWN );

      _CPU_Fatal_halt( _Per_CPU_Get_index( self_cpu ) );
      /* does not continue past here */
    }
  }
}

void _SMP_Send_message( uint32_t cpu, uint32_t message )
{
  Per_CPU_Control *per_cpu = _Per_CPU_Get_by_index( cpu );
  ISR_Level level;

  #if defined(RTEMS_DEBUG)
    if ( message & RTEMS_BSP_SMP_SIGNAL_TO_SELF )
      printk( "Send 0x%x to %d\n", message, cpu );
  #endif

  _Per_CPU_Lock_acquire( per_cpu, level );
  per_cpu->message |= message;
  _Per_CPU_Lock_release( per_cpu, level );

  _CPU_SMP_Send_interrupt( cpu );
}

void _SMP_Broadcast_message( uint32_t message )
{
  uint32_t self = _SMP_Get_current_processor();
  uint32_t ncpus = _SMP_Get_processor_count();
  uint32_t cpu;

  for ( cpu = 0 ; cpu < ncpus ; ++cpu ) {
    if ( cpu != self ) {
      Per_CPU_Control *per_cpu = _Per_CPU_Get_by_index( cpu );
      ISR_Level level;

      _Per_CPU_Lock_acquire( per_cpu, level );
      per_cpu->message |= message;
      _Per_CPU_Lock_release( per_cpu, level );
    }
  }

  bsp_smp_broadcast_interrupt();
}

void _SMP_Request_other_cores_to_perform_first_context_switch( void )
{
  uint32_t self = _SMP_Get_current_processor();
  uint32_t ncpus = _SMP_Get_processor_count();
  uint32_t cpu;

  for ( cpu = 0 ; cpu < ncpus ; ++cpu ) {
    Per_CPU_Control *per_cpu = _Per_CPU_Get_by_index( cpu );

    if ( cpu != self ) {
      _Per_CPU_Change_state( per_cpu, PER_CPU_STATE_BEGIN_MULTITASKING );
    } else {

      _Per_CPU_Change_state( per_cpu, PER_CPU_STATE_UP );
    }
  }
}

void _SMP_Request_other_cores_to_dispatch( void )
{
  if ( _System_state_Is_up( _System_state_Get() ) ) {
    uint32_t self = _SMP_Get_current_processor();
    uint32_t ncpus = _SMP_Get_processor_count();
    uint32_t cpu;

    for ( cpu = 0 ; cpu < ncpus ; ++cpu ) {
      const Per_CPU_Control *per_cpu = &_Per_CPU_Information[ cpu ];

      if (
        cpu != self
          && per_cpu->state == PER_CPU_STATE_UP
          && per_cpu->dispatch_necessary
      ) {
        _SMP_Send_message( cpu, 0 );
      }
    }
  }
}

void _SMP_Request_other_cores_to_shutdown( void )
{
  uint32_t self = _SMP_Get_current_processor();
  uint32_t ncpus = _SMP_Get_processor_count();
  uint32_t cpu;

  _SMP_Broadcast_message( RTEMS_BSP_SMP_SHUTDOWN );

  for ( cpu = 0 ; cpu < ncpus ; ++cpu ) {
    if ( cpu != self ) {
      _Per_CPU_Wait_for_state(
        _Per_CPU_Get_by_index( cpu ),
        PER_CPU_STATE_SHUTDOWN
      );
    }
  }
}