summaryrefslogtreecommitdiffstats
path: root/cpukit/score/src/smp.c
blob: 08677d83c7b180700738eddcb3cb7d1de70bb389 (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
/**
 *  @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.org/license/LICENSE.
 */

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

#include <rtems/score/smpimpl.h>
#include <rtems/score/assert.h>
#include <rtems/score/threaddispatch.h>
#include <rtems/score/threadimpl.h>
#include <rtems/config.h>

void _SMP_Handler_initialize( void )
{
  uint32_t cpu_max = rtems_configuration_get_maximum_processors();
  uint32_t cpu_self;
  uint32_t cpu_count;
  uint32_t cpu_index;

  for ( cpu_index = 0 ; cpu_index < cpu_max; ++cpu_index ) {
    Per_CPU_Control *per_cpu = _Per_CPU_Get_by_index( cpu_index );

    _SMP_ticket_lock_Initialize( &per_cpu->Lock, "per-CPU" );
  }

  /*
   * Discover and initialize the secondary cores in an SMP system.
   */

  cpu_count = _CPU_SMP_Initialize();
  cpu_count = cpu_count < cpu_max ? cpu_count : cpu_max;
  _SMP_Processor_count = cpu_count;

  cpu_self = _SMP_Get_current_processor();

  for ( cpu_index = 0 ; cpu_index < cpu_count; ++cpu_index ) {
    if ( cpu_index != cpu_self ) {
      bool ok = _CPU_SMP_Start_processor( cpu_index );

      if ( !ok ) {
        _Terminate(
          RTEMS_FATAL_SOURCE_SMP,
          false,
          SMP_FATAL_START_OF_MANDATORY_PROCESSOR_FAILED
        );
      }
    }
  }

  _CPU_SMP_Finalize_initialization( cpu_count );
}

void _SMP_Request_start_multitasking( void )
{
  Per_CPU_Control *self_cpu = _Per_CPU_Get();
  uint32_t ncpus = _SMP_Get_processor_count();
  uint32_t cpu;

  _Per_CPU_State_change( self_cpu, PER_CPU_STATE_READY_TO_START_MULTITASKING );

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

    _Per_CPU_State_change( per_cpu, PER_CPU_STATE_REQUEST_START_MULTITASKING );
  }
}

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

  _Per_CPU_State_change( self_cpu, PER_CPU_STATE_READY_TO_START_MULTITASKING );

  _Thread_Start_multitasking();
}

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

  _Per_CPU_State_change( self_cpu, PER_CPU_STATE_SHUTDOWN );

  /*
   * We have to drop the Giant lock here in order to give other processors the
   * opportunity to receive the inter-processor interrupts issued previously.
   * In case the executing thread still holds SMP locks, then other processors
   * already waiting for this SMP lock will spin forever.
   */
  _Giant_Drop( self_cpu );
}

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

  _Per_CPU_ISR_disable_and_acquire( per_cpu, level );
  per_cpu->message |= message;
  _Per_CPU_Release_and_ISR_enable( 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;

  _Assert( _Debug_Is_thread_dispatching_allowed() );

  for ( cpu = 0 ; cpu < ncpus ; ++cpu ) {
    if ( cpu != self ) {
      _SMP_Send_message( cpu, message );
    }
  }
}