summaryrefslogtreecommitdiffstats
path: root/cpukit/sapi/src/exinit.c
blob: 794ccb4bfad77d66f5a61b4c323c44a1fac49ab5 (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/**
 * @file
 *
 * @brief Initialization Manager
 *
 * @ingroup ClassicRTEMS
 */

/*
 *  COPYRIGHT (c) 1989-2014.
 *  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

/*
 *  SCORE_INIT and SAPI_INIT are defined so all of the super core and
 *  super API data will be included in this object file.
 */

#define SAPI_INIT
#define SCORE_INIT

#include <rtems/system.h>
#include <rtems/config.h>
#include <rtems/extensionimpl.h>
#include <rtems/init.h>
#include <rtems/score/sysstate.h>

#include <rtems/score/apiext.h>
#include <rtems/score/apimutex.h>
#include <rtems/score/copyrt.h>
#include <rtems/score/cpusetimpl.h>
#include <rtems/score/heap.h>
#include <rtems/score/interr.h>
#include <rtems/score/isr.h>
#include <rtems/score/priority.h>
#include <rtems/score/schedulerimpl.h>
#include <rtems/score/smpimpl.h>
#include <rtems/score/timecounter.h>
#include <rtems/score/threadimpl.h>
#include <rtems/score/todimpl.h>
#include <rtems/score/userextimpl.h>
#include <rtems/score/watchdogimpl.h>
#include <rtems/score/wkspace.h>

#include <rtems/sptables.h>


#include <rtems/rtems/rtemsapi.h>
#include <rtems/posix/posixapi.h>

#ifdef RTEMS_DRVMGR_STARTUP
  #include <drvmgr/drvmgr.h>
#endif

Objects_Information *_Internal_Objects[ OBJECTS_INTERNAL_CLASSES_LAST + 1 ];

void rtems_initialize_data_structures(void)
{
  /*
   *  Dispatching and interrupts are disabled until the end of the
   *  initialization sequence.  This prevents an inadvertent context
   *  switch before the executive is initialized.
   *
   *  WARNING: Interrupts should have been disabled by the BSP and
   *           are disabled by boot_card().
   */

  #if defined(RTEMS_MULTIPROCESSING)
    /*
     *  Initialize the system state based on whether this is an MP system.
     *  In an MP configuration, internally we view single processor
     *  systems as a very restricted multiprocessor system.
     */
    _Configuration_MP_table = rtems_configuration_get_user_multiprocessing_table();

    if ( _Configuration_MP_table == NULL ) {
      _Configuration_MP_table =
	(void *)&_Initialization_Default_multiprocessing_table;
      _System_state_Handler_initialization( FALSE );
    } else {
      _System_state_Handler_initialization( TRUE );
    }
  #else
    _System_state_Handler_initialization( FALSE );
  #endif

  /*
   * Initialize any target architecture specific support as early as possible
   */
  _CPU_Initialize();

  #if defined(RTEMS_MULTIPROCESSING)
    _Objects_MP_Handler_early_initialization();
  #endif

  _Thread_Dispatch_initialization();

  _User_extensions_Handler_initialization();
  _ISR_Handler_initialization();

  /*
   * Initialize the internal support API and allocator Mutex
   */
  _Objects_Information_table[OBJECTS_INTERNAL_API] = _Internal_Objects;

  _API_Mutex_Initialization( 2 );
  _API_Mutex_Allocate( &_RTEMS_Allocator_Mutex );
  _API_Mutex_Allocate( &_Once_Mutex );

  _Watchdog_Handler_initialization();
  _TOD_Handler_initialization();

  _Thread_Handler_initialization();

  _Scheduler_Handler_initialization();

  #if defined(RTEMS_MULTIPROCESSING)
    _Objects_MP_Handler_initialization();
    _MPCI_Handler_initialization( RTEMS_TIMEOUT );
  #endif

  _SMP_Handler_initialize();

  _CPU_set_Handler_initialization();

/* MANAGERS */

  _RTEMS_API_Initialize();

  _Extension_Manager_initialization();

  _POSIX_API_Initialize();

  _System_state_Set( SYSTEM_STATE_BEFORE_MULTITASKING );

  /*
   *  No threads should be created before this point!!!
   *  _Thread_Executing and _Thread_Heir are not set.
   *
   *  At this point all API extensions are in place.  After the call to
   *  _Thread_Create_idle() _Thread_Executing and _Thread_Heir will be set.
   */
  _Thread_Create_idle();

  /*
   *  Scheduling can properly occur now as long as we avoid dispatching.
   */
}

void rtems_initialize_before_drivers(void)
{
  #ifdef RTEMS_DRVMGR_STARTUP
  _DRV_Manager_initialization();
  #endif

  #if defined(RTEMS_MULTIPROCESSING)
    _MPCI_Create_server();
  #endif
}

void rtems_initialize_device_drivers(void)
{
  /*
   *  Initialize all the device drivers and initialize the MPCI layer.
   *
   *  NOTE:  The MPCI may be build upon a device driver.
   */

  #ifdef RTEMS_DRVMGR_STARTUP
  /* BSPs has already registered their "root bus" driver in the
   * bsp_predriver hook or so.
   *
   * Init Drivers to Level 1, constraints:
   *   - Interrupts and system clock timer does not work.
   *   - malloc() work, however other memory services may not
   *     have been initialized yet.
   *   - initializes most basic stuff
   *
   * Typical setup in Level 1:
   *   - Find most devices in system, do PCI scan and configuration.
   *   - Reset hardware if needed.
   *   - Install IRQ driver
   *   - Install Timer driver
   *   - Install console driver and debug printk()
   *   - Install extra memory.
   */
  _DRV_Manager_init_level(1);
  bsp_driver_level_hook(1);
  #endif

  /* Initialize I/O drivers. 
   *
   * Driver Manager note:
   * All drivers may not be registered yet. Drivers will dynamically
   * be initialized when registered in level 2,3 and 4.
   */
  _IO_Initialize_all_drivers();

  #ifdef RTEMS_DRVMGR_STARTUP
  /* Init Drivers to Level 2, constraints:
   *  - Interrupts can be registered and enabled.
   *  - System Clock is running
   *  - Console may be used.
   *
   * This is typically where drivers are initialized
   * for the first time.
   */
  _DRV_Manager_init_level(2);
  bsp_driver_level_hook(2);

  /* Init Drivers to Level 3 
   * 
   * This is typically where normal drivers are initialized
   * for the second time, they may depend on other drivers
   * API inited in level 2
   */
  _DRV_Manager_init_level(3);
  bsp_driver_level_hook(3);

  /* Init Drivers to Level 4,
   * Init drivers that depend on services initialized in Level 3
   */
  _DRV_Manager_init_level(4);
  bsp_driver_level_hook(4);
  #endif

  #if defined(RTEMS_MULTIPROCESSING)
    if ( _System_state_Is_multiprocessing ) {
      _MPCI_Initialization();
      _MPCI_Internal_packets_Send_process_packet(
	MPCI_PACKETS_SYSTEM_VERIFY
      );
    }
  #endif

  /*
   *  Run the APIs and BSPs postdriver hooks.
   *
   *  The API extensions are supposed to create user initialization tasks.
   */
  _API_extensions_Run_postdriver();
}

void rtems_initialize_start_multitasking(void)
{
  _System_state_Set( SYSTEM_STATE_UP );

  _SMP_Request_start_multitasking();

  _Thread_Start_multitasking();

  /*******************************************************************
   *******************************************************************
   *******************************************************************
   ******                 APPLICATION RUNS HERE                 ******
   ******              THE FUNCTION NEVER RETURNS               ******
   *******************************************************************
   *******************************************************************
   *******************************************************************/
}