source: rtems/cpukit/score/src/smp.c @ 6c36946f

4.115
Last change on this file since 6c36946f was 6c36946f, checked in by Sebastian Huber <sebastian.huber@…>, on 05/07/14 at 12:22:56

score: Fix SMP startup

  • Property mode set to 100644
File size: 4.6 KB
Line 
1/**
2 *  @file
3 *
4 *  @brief SMP Support
5 *  @ingroup Score
6 */
7
8/*
9 *  COPYRIGHT (c) 1989-2011.
10 *  On-Line Applications Research Corporation (OAR).
11 *
12 *  The license and distribution terms for this file may be
13 *  found in the file LICENSE in this distribution or at
14 *  http://www.rtems.org/license/LICENSE.
15 */
16
17#if HAVE_CONFIG_H
18#include "config.h"
19#endif
20
21#include <rtems/score/smpimpl.h>
22#include <rtems/score/assert.h>
23#include <rtems/score/schedulerimpl.h>
24#include <rtems/score/threaddispatch.h>
25#include <rtems/score/threadimpl.h>
26#include <rtems/config.h>
27
28static void _SMP_Start_processors( uint32_t cpu_count )
29{
30  uint32_t cpu_index_self = _SMP_Get_current_processor();
31  uint32_t cpu_index;
32
33
34  for ( cpu_index = 0 ; cpu_index < cpu_count; ++cpu_index ) {
35    const Scheduler_Assignment *assignment =
36      _Scheduler_Get_assignment( cpu_index );
37    Per_CPU_Control *cpu = _Per_CPU_Get_by_index( cpu_index );
38    bool started;
39
40    if ( cpu_index != cpu_index_self ) {
41      if ( _Scheduler_Should_start_processor( assignment ) ) {
42        started = _CPU_SMP_Start_processor( cpu_index );
43
44        if ( !started && _Scheduler_Is_mandatory_processor( assignment ) ) {
45          _SMP_Fatal( SMP_FATAL_START_OF_MANDATORY_PROCESSOR_FAILED );
46        }
47      } else {
48        started = false;
49      }
50    } else {
51      started = true;
52
53      if ( !_Scheduler_Should_start_processor( assignment ) ) {
54        _SMP_Fatal( SMP_FATAL_BOOT_PROCESSOR_NOT_ASSIGNED_TO_SCHEDULER );
55      }
56    }
57
58    cpu->started = started;
59
60    if ( started ) {
61      Scheduler_Context *scheduler_context = assignment->scheduler->context;
62
63      ++scheduler_context->processor_count;
64      cpu->scheduler_context = scheduler_context;
65    }
66  }
67}
68
69void _SMP_Handler_initialize( void )
70{
71  uint32_t cpu_max = rtems_configuration_get_maximum_processors();
72  uint32_t cpu_count;
73  uint32_t cpu_index;
74
75  for ( cpu_index = 0 ; cpu_index < cpu_max; ++cpu_index ) {
76    Per_CPU_Control *cpu = _Per_CPU_Get_by_index( cpu_index );
77
78    _SMP_ticket_lock_Initialize( &cpu->Lock, "per-CPU" );
79  }
80
81  /*
82   * Discover and initialize the secondary cores in an SMP system.
83   */
84
85  cpu_count = _CPU_SMP_Initialize();
86  cpu_count = cpu_count < cpu_max ? cpu_count : cpu_max;
87  _SMP_Processor_count = cpu_count;
88
89  for ( cpu_index = cpu_count ; cpu_index < cpu_max; ++cpu_index ) {
90    const Scheduler_Assignment *assignment =
91      _Scheduler_Get_assignment( cpu_index );
92
93    if ( _Scheduler_Is_mandatory_processor( assignment ) ) {
94      _SMP_Fatal( SMP_FATAL_MANDATORY_PROCESSOR_NOT_PRESENT );
95    }
96  }
97
98  _SMP_Start_processors( cpu_count );
99
100  _CPU_SMP_Finalize_initialization( cpu_count );
101}
102
103void _SMP_Request_start_multitasking( void )
104{
105  Per_CPU_Control *self_cpu = _Per_CPU_Get();
106  uint32_t cpu_count = _SMP_Get_processor_count();
107  uint32_t cpu_index;
108
109  _Per_CPU_State_change( self_cpu, PER_CPU_STATE_READY_TO_START_MULTITASKING );
110
111  for ( cpu_index = 0 ; cpu_index < cpu_count ; ++cpu_index ) {
112    Per_CPU_Control *cpu = _Per_CPU_Get_by_index( cpu_index );
113
114    if ( _Per_CPU_Is_processor_started( cpu ) ) {
115      _Per_CPU_State_change( cpu, PER_CPU_STATE_REQUEST_START_MULTITASKING );
116    }
117  }
118}
119
120void _SMP_Start_multitasking_on_secondary_processor( void )
121{
122  Per_CPU_Control *self_cpu = _Per_CPU_Get();
123
124  if ( !_Per_CPU_Is_processor_started( self_cpu ) ) {
125    _SMP_Fatal( SMP_FATAL_MULTITASKING_START_ON_UNASSIGNED_PROCESSOR );
126  }
127
128  _Per_CPU_State_change( self_cpu, PER_CPU_STATE_READY_TO_START_MULTITASKING );
129
130  _Thread_Start_multitasking();
131}
132
133void _SMP_Request_shutdown( void )
134{
135  Per_CPU_Control *self_cpu = _Per_CPU_Get();
136
137  _Per_CPU_State_change( self_cpu, PER_CPU_STATE_SHUTDOWN );
138
139  /*
140   * We have to drop the Giant lock here in order to give other processors the
141   * opportunity to receive the inter-processor interrupts issued previously.
142   * In case the executing thread still holds SMP locks, then other processors
143   * already waiting for this SMP lock will spin forever.
144   */
145  _Giant_Drop( self_cpu );
146}
147
148void _SMP_Send_message( uint32_t cpu_index, unsigned long message )
149{
150  Per_CPU_Control *cpu = _Per_CPU_Get_by_index( cpu_index );
151
152  _Atomic_Fetch_or_ulong( &cpu->message, message, ATOMIC_ORDER_RELAXED );
153
154  _CPU_SMP_Send_interrupt( cpu_index );
155}
156
157void _SMP_Broadcast_message( uint32_t message )
158{
159  uint32_t cpu_count = _SMP_Get_processor_count();
160  uint32_t cpu_index_self = _SMP_Get_current_processor();
161  uint32_t cpu_index;
162
163  _Assert( _Debug_Is_thread_dispatching_allowed() );
164
165  for ( cpu_index = 0 ; cpu_index < cpu_count ; ++cpu_index ) {
166    if ( cpu_index != cpu_index_self ) {
167      _SMP_Send_message( cpu_index, message );
168    }
169  }
170}
171
172SMP_Test_message_handler _SMP_Test_message_handler;
Note: See TracBrowser for help on using the repository browser.