1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @brief Allocate and Initialize Per CPU Structures |
---|
5 | * @ingroup PerCPU |
---|
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.com/license/LICENSE. |
---|
15 | */ |
---|
16 | |
---|
17 | #if HAVE_CONFIG_H |
---|
18 | #include "config.h" |
---|
19 | #endif |
---|
20 | |
---|
21 | #include <rtems/system.h> |
---|
22 | #include <rtems/score/address.h> |
---|
23 | #include <rtems/score/thread.h> |
---|
24 | #include <rtems/score/percpu.h> |
---|
25 | #include <rtems/score/wkspace.h> |
---|
26 | #include <rtems/config.h> |
---|
27 | #include <string.h> |
---|
28 | |
---|
29 | #if defined(RTEMS_SMP) |
---|
30 | |
---|
31 | #include <rtems/score/smp.h> |
---|
32 | #include <rtems/bspsmp.h> |
---|
33 | |
---|
34 | void _SMP_Handler_initialize(void) |
---|
35 | { |
---|
36 | uint32_t max_cpus = rtems_configuration_get_maximum_processors(); |
---|
37 | uint32_t cpu; |
---|
38 | |
---|
39 | /* |
---|
40 | * Initialize per cpu pointer table |
---|
41 | */ |
---|
42 | _Per_CPU_Information_p[0] = _Per_CPU_Get_by_index( 0 ); |
---|
43 | for ( cpu = 1 ; cpu < max_cpus; ++cpu ) { |
---|
44 | |
---|
45 | Per_CPU_Control *p = _Per_CPU_Get_by_index( cpu ); |
---|
46 | |
---|
47 | _Per_CPU_Information_p[cpu] = p; |
---|
48 | |
---|
49 | #if CPU_ALLOCATE_INTERRUPT_STACK == TRUE |
---|
50 | { |
---|
51 | size_t size = rtems_configuration_get_interrupt_stack_size(); |
---|
52 | uintptr_t ptr; |
---|
53 | |
---|
54 | p->interrupt_stack_low = _Workspace_Allocate_or_fatal_error( size ); |
---|
55 | |
---|
56 | ptr = (uintptr_t) _Addresses_Add_offset( p->interrupt_stack_low, size ); |
---|
57 | ptr &= ~(CPU_STACK_ALIGNMENT - 1); |
---|
58 | p->interrupt_stack_high = (void *)ptr; |
---|
59 | } |
---|
60 | #endif |
---|
61 | } |
---|
62 | |
---|
63 | /* |
---|
64 | * Discover and initialize the secondary cores in an SMP system. |
---|
65 | */ |
---|
66 | max_cpus = bsp_smp_initialize( max_cpus ); |
---|
67 | |
---|
68 | _SMP_Processor_count = max_cpus; |
---|
69 | |
---|
70 | for ( cpu = 1 ; cpu < max_cpus; ++cpu ) { |
---|
71 | const Per_CPU_Control *per_cpu = _Per_CPU_Get_by_index( cpu ); |
---|
72 | |
---|
73 | _Per_CPU_Wait_for_state( |
---|
74 | per_cpu, |
---|
75 | PER_CPU_STATE_READY_TO_BEGIN_MULTITASKING |
---|
76 | ); |
---|
77 | } |
---|
78 | } |
---|
79 | |
---|
80 | void _Per_CPU_Change_state( |
---|
81 | Per_CPU_Control *per_cpu, |
---|
82 | Per_CPU_State new_state |
---|
83 | ) |
---|
84 | { |
---|
85 | per_cpu->state = new_state; |
---|
86 | _CPU_SMP_Processor_event_broadcast(); |
---|
87 | } |
---|
88 | |
---|
89 | void _Per_CPU_Wait_for_state( |
---|
90 | const Per_CPU_Control *per_cpu, |
---|
91 | Per_CPU_State desired_state |
---|
92 | ) |
---|
93 | { |
---|
94 | while ( per_cpu->state != desired_state ) { |
---|
95 | _CPU_SMP_Processor_event_receive(); |
---|
96 | } |
---|
97 | } |
---|
98 | #else |
---|
99 | /* |
---|
100 | * On single core systems, we can efficiently directly access a single |
---|
101 | * statically allocated per cpu structure. And the fields are initialized |
---|
102 | * as individual elements just like it has always been done. |
---|
103 | */ |
---|
104 | Per_CPU_Control _Per_CPU_Information[1]; |
---|
105 | #endif |
---|