source: rtems/cpukit/score/src/smp.c @ 2369b10

4.115
Last change on this file since 2369b10 was 2369b10, checked in by Sebastian Huber <sebastian.huber@…>, on 05/14/14 at 14:04:10

score: Add and use _Scheduler_Get_context()

  • 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 *context =
62        _Scheduler_Get_context( assignment->scheduler );
63
64      ++context->processor_count;
65      cpu->scheduler_context = context;
66    }
67  }
68}
69
70void _SMP_Handler_initialize( void )
71{
72  uint32_t cpu_max = rtems_configuration_get_maximum_processors();
73  uint32_t cpu_count;
74  uint32_t cpu_index;
75
76  for ( cpu_index = 0 ; cpu_index < cpu_max; ++cpu_index ) {
77    Per_CPU_Control *cpu = _Per_CPU_Get_by_index( cpu_index );
78
79    _SMP_ticket_lock_Initialize( &cpu->Lock, "per-CPU" );
80  }
81
82  /*
83   * Discover and initialize the secondary cores in an SMP system.
84   */
85
86  cpu_count = _CPU_SMP_Initialize();
87  cpu_count = cpu_count < cpu_max ? cpu_count : cpu_max;
88  _SMP_Processor_count = cpu_count;
89
90  for ( cpu_index = cpu_count ; cpu_index < cpu_max; ++cpu_index ) {
91    const Scheduler_Assignment *assignment =
92      _Scheduler_Get_assignment( cpu_index );
93
94    if ( _Scheduler_Is_mandatory_processor( assignment ) ) {
95      _SMP_Fatal( SMP_FATAL_MANDATORY_PROCESSOR_NOT_PRESENT );
96    }
97  }
98
99  _SMP_Start_processors( cpu_count );
100
101  _CPU_SMP_Finalize_initialization( cpu_count );
102}
103
104void _SMP_Request_start_multitasking( void )
105{
106  Per_CPU_Control *self_cpu = _Per_CPU_Get();
107  uint32_t cpu_count = _SMP_Get_processor_count();
108  uint32_t cpu_index;
109
110  _Per_CPU_State_change( self_cpu, PER_CPU_STATE_READY_TO_START_MULTITASKING );
111
112  for ( cpu_index = 0 ; cpu_index < cpu_count ; ++cpu_index ) {
113    Per_CPU_Control *cpu = _Per_CPU_Get_by_index( cpu_index );
114
115    if ( _Per_CPU_Is_processor_started( cpu ) ) {
116      _Per_CPU_State_change( cpu, PER_CPU_STATE_REQUEST_START_MULTITASKING );
117    }
118  }
119}
120
121void _SMP_Start_multitasking_on_secondary_processor( void )
122{
123  Per_CPU_Control *self_cpu = _Per_CPU_Get();
124
125  if ( !_Per_CPU_Is_processor_started( self_cpu ) ) {
126    _SMP_Fatal( SMP_FATAL_MULTITASKING_START_ON_UNASSIGNED_PROCESSOR );
127  }
128
129  _Per_CPU_State_change( self_cpu, PER_CPU_STATE_READY_TO_START_MULTITASKING );
130
131  _Thread_Start_multitasking();
132}
133
134void _SMP_Request_shutdown( void )
135{
136  Per_CPU_Control *self_cpu = _Per_CPU_Get();
137
138  _Per_CPU_State_change( self_cpu, PER_CPU_STATE_SHUTDOWN );
139
140  /*
141   * We have to drop the Giant lock here in order to give other processors the
142   * opportunity to receive the inter-processor interrupts issued previously.
143   * In case the executing thread still holds SMP locks, then other processors
144   * already waiting for this SMP lock will spin forever.
145   */
146  _Giant_Drop( self_cpu );
147}
148
149void _SMP_Send_message( uint32_t cpu_index, unsigned long message )
150{
151  Per_CPU_Control *cpu = _Per_CPU_Get_by_index( cpu_index );
152
153  _Atomic_Fetch_or_ulong( &cpu->message, message, ATOMIC_ORDER_RELAXED );
154
155  _CPU_SMP_Send_interrupt( cpu_index );
156}
157
158void _SMP_Broadcast_message( uint32_t message )
159{
160  uint32_t cpu_count = _SMP_Get_processor_count();
161  uint32_t cpu_index_self = _SMP_Get_current_processor();
162  uint32_t cpu_index;
163
164  _Assert( _Debug_Is_thread_dispatching_allowed() );
165
166  for ( cpu_index = 0 ; cpu_index < cpu_count ; ++cpu_index ) {
167    if ( cpu_index != cpu_index_self ) {
168      _SMP_Send_message( cpu_index, message );
169    }
170  }
171}
172
173SMP_Test_message_handler _SMP_Test_message_handler;
Note: See TracBrowser for help on using the repository browser.