1 | /* SPDX-License-Identifier: BSD-2-Clause */ |
---|
2 | |
---|
3 | /** |
---|
4 | * @file |
---|
5 | * |
---|
6 | * @ingroup RTEMSImplClassicScheduler |
---|
7 | * |
---|
8 | * @brief This source file contains the implementation of |
---|
9 | * rtems_scheduler_add_processor(). |
---|
10 | */ |
---|
11 | |
---|
12 | /* |
---|
13 | * Copyright (c) 2016 embedded brains GmbH & Co. KG |
---|
14 | * |
---|
15 | * Redistribution and use in source and binary forms, with or without |
---|
16 | * modification, are permitted provided that the following conditions |
---|
17 | * are met: |
---|
18 | * 1. Redistributions of source code must retain the above copyright |
---|
19 | * notice, this list of conditions and the following disclaimer. |
---|
20 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
21 | * notice, this list of conditions and the following disclaimer in the |
---|
22 | * documentation and/or other materials provided with the distribution. |
---|
23 | * |
---|
24 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
25 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
---|
28 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
29 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
30 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
31 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
32 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
33 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
34 | * POSSIBILITY OF SUCH DAMAGE. |
---|
35 | */ |
---|
36 | |
---|
37 | #ifdef HAVE_CONFIG_H |
---|
38 | #include "config.h" |
---|
39 | #endif |
---|
40 | |
---|
41 | #include <rtems/rtems/scheduler.h> |
---|
42 | #include <rtems/score/assert.h> |
---|
43 | #include <rtems/score/schedulerimpl.h> |
---|
44 | #include <rtems/config.h> |
---|
45 | |
---|
46 | rtems_status_code rtems_scheduler_add_processor( |
---|
47 | rtems_id scheduler_id, |
---|
48 | uint32_t cpu_index |
---|
49 | ) |
---|
50 | { |
---|
51 | uint32_t scheduler_index; |
---|
52 | #if defined(RTEMS_SMP) |
---|
53 | Per_CPU_Control *cpu; |
---|
54 | rtems_status_code status; |
---|
55 | #endif |
---|
56 | |
---|
57 | scheduler_index = _Scheduler_Get_index_by_id( scheduler_id ); |
---|
58 | |
---|
59 | if ( scheduler_index >= _Scheduler_Count ) { |
---|
60 | return RTEMS_INVALID_ID; |
---|
61 | } |
---|
62 | |
---|
63 | if ( cpu_index >= rtems_configuration_get_maximum_processors() ) { |
---|
64 | return RTEMS_NOT_CONFIGURED; |
---|
65 | } |
---|
66 | |
---|
67 | #if defined(RTEMS_SMP) |
---|
68 | cpu = _Per_CPU_Get_by_index( cpu_index ); |
---|
69 | |
---|
70 | if ( _Scheduler_Initial_assignments[ cpu_index ].scheduler == NULL ) { |
---|
71 | return RTEMS_NOT_CONFIGURED; |
---|
72 | } |
---|
73 | |
---|
74 | if ( !_Per_CPU_Is_processor_online( cpu ) ) { |
---|
75 | return RTEMS_INCORRECT_STATE; |
---|
76 | } |
---|
77 | |
---|
78 | _Objects_Allocator_lock(); |
---|
79 | |
---|
80 | if ( cpu->Scheduler.control == NULL ) { |
---|
81 | const Scheduler_Control *scheduler; |
---|
82 | Scheduler_Context *scheduler_context; |
---|
83 | Priority_Control idle_priority; |
---|
84 | Thread_Control *idle; |
---|
85 | Scheduler_Node *scheduler_node; |
---|
86 | ISR_lock_Context lock_context; |
---|
87 | Per_CPU_Control *cpu_self; |
---|
88 | |
---|
89 | scheduler = &_Scheduler_Table[ scheduler_index ]; |
---|
90 | scheduler_context = _Scheduler_Get_context( scheduler ); |
---|
91 | idle_priority = |
---|
92 | _Scheduler_Map_priority( scheduler, scheduler->maximum_priority ); |
---|
93 | |
---|
94 | idle = cpu->Scheduler.idle_if_online_and_unused; |
---|
95 | _Assert( idle != NULL ); |
---|
96 | cpu->Scheduler.idle_if_online_and_unused = NULL; |
---|
97 | |
---|
98 | idle->Scheduler.home_scheduler = scheduler; |
---|
99 | idle->Start.initial_priority = idle_priority; |
---|
100 | scheduler_node = |
---|
101 | _Thread_Scheduler_get_node_by_index( idle, scheduler_index ); |
---|
102 | _Priority_Node_set_priority( &idle->Real_priority, idle_priority ); |
---|
103 | _Priority_Initialize_one( |
---|
104 | &scheduler_node->Wait.Priority, |
---|
105 | &idle->Real_priority |
---|
106 | ); |
---|
107 | _Assert( _Chain_Is_empty( &idle->Scheduler.Wait_nodes ) ); |
---|
108 | _Chain_Initialize_one( |
---|
109 | &idle->Scheduler.Wait_nodes, |
---|
110 | &scheduler_node->Thread.Wait_node |
---|
111 | ); |
---|
112 | _Assert( _Chain_Is_empty( &idle->Scheduler.Scheduler_nodes ) ); |
---|
113 | _Chain_Initialize_one( |
---|
114 | &idle->Scheduler.Scheduler_nodes, |
---|
115 | &scheduler_node->Thread.Scheduler_node.Chain |
---|
116 | ); |
---|
117 | |
---|
118 | _ISR_lock_ISR_disable( &lock_context ); |
---|
119 | _Scheduler_Acquire_critical( scheduler, &lock_context ); |
---|
120 | _Processor_mask_Set( &scheduler_context->Processors, cpu_index ); |
---|
121 | cpu->Scheduler.control = scheduler; |
---|
122 | cpu->Scheduler.context = scheduler_context; |
---|
123 | ( *scheduler->Operations.add_processor )( scheduler, idle ); |
---|
124 | cpu_self = _Thread_Dispatch_disable_critical( &lock_context ); |
---|
125 | _Scheduler_Release_critical( scheduler, &lock_context ); |
---|
126 | _ISR_lock_ISR_enable( &lock_context ); |
---|
127 | _Thread_Dispatch_direct( cpu_self ); |
---|
128 | status = RTEMS_SUCCESSFUL; |
---|
129 | } else { |
---|
130 | status = RTEMS_RESOURCE_IN_USE; |
---|
131 | } |
---|
132 | |
---|
133 | _Objects_Allocator_unlock(); |
---|
134 | return status; |
---|
135 | #else |
---|
136 | return RTEMS_RESOURCE_IN_USE; |
---|
137 | #endif |
---|
138 | } |
---|