source: rtems/cpukit/score/src/mpci.c @ ab02824

Last change on this file since ab02824 was ab02824, checked in by Joel Sherrill <joel@…>, on 02/16/22 at 21:09:06

score/src/[a-m]*.c: Change license to BSD-2

Updates #3053.

  • Property mode set to 100644
File size: 13.7 KB
Line 
1/* SPDX-License-Identifier: BSD-2-Clause */
2
3/**
4 * @file
5 *
6 * @ingroup RTEMSScoreMPCI
7 *
8 * @brief This source file contains the definition of
9 *   ::_System_state_Is_multiprocessing, ::_MPCI_Semaphore,
10 *   ::_MPCI_Remote_blocked_threads, ::_MPCI_table, ::_MPCI_Receive_server_tcb,
11 *   ::_MPCI_Packet_processors and the implementation of
12 *   _MPCI_Register_packet_processor(), _MPCI_Get_packet(),
13 *   _MPCI_Send_process_packet(), _MPCI_Send_request_packet(),
14 *   _MPCI_Send_response_packet(), _MPCI_Receive_packet(),
15 *   _MPCI_Process_response(), _MPCI_Receive_server(), _MPCI_Announce(),
16 *   _MPCI_Internal_packets_Send_process_packet(),
17 *   _MPCI_Internal_packets_Process_packet(),
18 *   _MPCI_Internal_packets_Get_packet(), and the MPCI Handler system
19 *   initialization.
20 */
21
22/*
23 *  COPYRIGHT (c) 1989-2014.
24 *  On-Line Applications Research Corporation (OAR).
25 *
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions
28 * are met:
29 * 1. Redistributions of source code must retain the above copyright
30 *    notice, this list of conditions and the following disclaimer.
31 * 2. Redistributions in binary form must reproduce the above copyright
32 *    notice, this list of conditions and the following disclaimer in the
33 *    documentation and/or other materials provided with the distribution.
34 *
35 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
36 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
38 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
39 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
40 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
41 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
42 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
43 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
44 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
45 * POSSIBILITY OF SUCH DAMAGE.
46 */
47
48#ifdef HAVE_CONFIG_H
49#include "config.h"
50#endif
51
52#include <rtems/score/mpciimpl.h>
53#include <rtems/score/coresemimpl.h>
54#include <rtems/score/interr.h>
55#include <rtems/score/objectmp.h>
56#include <rtems/score/stackimpl.h>
57#include <rtems/score/sysstate.h>
58#include <rtems/score/schedulerimpl.h>
59#include <rtems/score/threadimpl.h>
60#include <rtems/score/threadqimpl.h>
61#include <rtems/sysinit.h>
62
63#include <string.h>
64
65RTEMS_STATIC_ASSERT(
66  sizeof(MPCI_Internal_packet) <= MP_PACKET_MINIMUM_PACKET_SIZE,
67  MPCI_Internal_packet
68);
69
70#define MPCI_SEMAPHORE_TQ_OPERATIONS &_Thread_queue_Operations_FIFO
71
72bool _System_state_Is_multiprocessing;
73
74/**
75 *  This is the core semaphore which the MPCI Receive Server blocks on.
76 */
77CORE_semaphore_Control _MPCI_Semaphore;
78
79Thread_queue_Control _MPCI_Remote_blocked_threads =
80  THREAD_QUEUE_INITIALIZER( "MPCI Remote Blocked Threads" );
81
82MPCI_Control *_MPCI_table;
83
84Thread_Control *_MPCI_Receive_server_tcb;
85
86MPCI_Packet_processor _MPCI_Packet_processors[ MP_PACKET_CLASSES_LAST + 1 ];
87
88static void _MPCI_Handler_early_initialization( void )
89{
90  /*
91   *  Initialize the system state based on whether this is an MP system.
92   *  In an MP configuration, internally we view single processor
93   *  systems as a very restricted multiprocessor system.
94   */
95  if ( _MPCI_Configuration.maximum_nodes > 1 ) {
96    _System_state_Is_multiprocessing = true;
97  }
98
99  _Objects_MP_Handler_early_initialization();
100}
101
102static void _MPCI_Handler_initialization( void )
103{
104  MPCI_Control               *users_mpci_table;
105
106  _Objects_MP_Handler_initialization();
107
108  users_mpci_table = _MPCI_Configuration.User_mpci_table;
109
110  if ( _System_state_Is_multiprocessing && !users_mpci_table )
111    _Internal_error( INTERNAL_ERROR_NO_MPCI );
112
113  _MPCI_table = users_mpci_table;
114
115  if ( !_System_state_Is_multiprocessing )
116    return;
117
118  /*
119   *  Register the MP Process Packet routine.
120   */
121
122  _MPCI_Register_packet_processor(
123    MP_PACKET_MPCI_INTERNAL,
124    _MPCI_Internal_packets_Process_packet
125  );
126
127  /*
128   *  Create the counting semaphore used by the MPCI Receive Server.
129   */
130
131  _CORE_semaphore_Initialize(
132    &_MPCI_Semaphore,
133    0                         /* initial_value */
134  );
135}
136
137static void _MPCI_Create_server( void )
138{
139  Thread_Entry_information entry = {
140    .adaptor = _Thread_Entry_adaptor_numeric,
141    .Kinds = {
142      .Numeric = {
143        .entry = _MPCI_Receive_server
144      }
145    }
146  };
147  Thread_Configuration config;
148  Status_Control       status;
149  ISR_lock_Context     lock_context;
150
151
152  if ( !_System_state_Is_multiprocessing )
153    return;
154
155  /*
156   *  Initialize the MPCI Receive Server
157   */
158
159  _MPCI_Receive_server_tcb = _Thread_Internal_allocate();
160  _Assert( _MPCI_Receive_server_tcb != NULL );
161
162  memset( &config, 0, sizeof( config ) );
163  config.scheduler = &_Scheduler_Table[ 0 ];
164  config.name = _Objects_Build_name( 'M', 'P', 'C', 'I' );
165  config.priority = PRIORITY_PSEUDO_ISR;
166  config.is_fp = CPU_ALL_TASKS_ARE_FP;
167  config.stack_size = _Stack_Minimum()
168    + _MPCI_Configuration.extra_mpci_receive_server_stack
169    + CPU_MPCI_RECEIVE_SERVER_EXTRA_STACK
170    + CPU_ALL_TASKS_ARE_FP * CONTEXT_FP_SIZE;
171  config.stack_area = _MPCI_Receive_server_stack;
172
173  status = _Thread_Initialize(
174    &_Thread_Information,
175    _MPCI_Receive_server_tcb,
176    &config
177  );
178  _Assert_Unused_variable_equals( status, STATUS_SUCCESSFUL );
179
180  _ISR_lock_ISR_disable( &lock_context );
181  _Thread_Start( _MPCI_Receive_server_tcb, &entry, &lock_context );
182}
183
184static void _MPCI_Initialization( void )
185{
186  (*_MPCI_table->initialization)();
187}
188
189void _MPCI_Register_packet_processor(
190  MP_packet_Classes      the_class,
191  MPCI_Packet_processor  the_packet_processor
192
193)
194{
195  _MPCI_Packet_processors[ the_class ] = the_packet_processor;
196}
197
198MP_packet_Prefix *_MPCI_Get_packet ( void )
199{
200  MP_packet_Prefix  *the_packet;
201
202  (*_MPCI_table->get_packet)( &the_packet );
203
204  if ( the_packet == NULL )
205    _Internal_error( INTERNAL_ERROR_OUT_OF_PACKETS );
206
207  /*
208   *  Put in a default timeout that will be used for
209   *  all packets that do not otherwise have a timeout.
210   */
211
212  the_packet->timeout = MPCI_DEFAULT_TIMEOUT;
213
214  return the_packet;
215}
216
217void _MPCI_Return_packet (
218  MP_packet_Prefix   *the_packet
219)
220{
221  (*_MPCI_table->return_packet)( the_packet );
222}
223
224void _MPCI_Send_process_packet (
225  uint32_t            destination,
226  MP_packet_Prefix   *the_packet
227)
228{
229  the_packet->source_tid = _Thread_Executing->Object.id;
230  the_packet->to_convert =
231     ( the_packet->to_convert - sizeof(MP_packet_Prefix) ) / sizeof(uint32_t);
232
233  (*_MPCI_table->send_packet)( destination, the_packet );
234}
235
236static void _MPCI_Enqueue_callout(
237  Thread_queue_Queue     *queue,
238  Thread_Control         *the_thread,
239  struct Per_CPU_Control *cpu_self,
240  Thread_queue_Context   *queue_context
241)
242{
243  _Thread_queue_Add_timeout_ticks( queue, the_thread, cpu_self, queue_context );
244  _Thread_Dispatch_unnest( cpu_self );
245}
246
247Status_Control _MPCI_Send_request_packet(
248  uint32_t          destination,
249  MP_packet_Prefix *the_packet,
250  States_Control    extra_state
251)
252{
253  Per_CPU_Control      *cpu_self;
254  Thread_queue_Context  queue_context;
255  Thread_Control       *executing;
256
257  /*
258   *  See if we need a default timeout
259   */
260
261  if (the_packet->timeout == MPCI_DEFAULT_TIMEOUT)
262      the_packet->timeout = _MPCI_table->default_timeout;
263
264  _Thread_queue_Context_initialize( &queue_context );
265  _Thread_queue_Context_set_thread_state(
266    &queue_context,
267    STATES_WAITING_FOR_RPC_REPLY | extra_state
268  );
269  _Thread_queue_Context_set_timeout_ticks( &queue_context, the_packet->timeout );
270  _Thread_queue_Context_set_enqueue_callout(
271    &queue_context,
272    _MPCI_Enqueue_callout
273  );
274
275  cpu_self = _Thread_Dispatch_disable();
276
277  executing = _Per_CPU_Get_executing( cpu_self );
278  executing->Wait.remote_id = the_packet->id;
279
280  the_packet->source_tid      = executing->Object.id;
281  the_packet->source_priority = _Thread_Get_priority( executing );
282  the_packet->to_convert =
283     ( the_packet->to_convert - sizeof(MP_packet_Prefix) ) / sizeof(uint32_t);
284
285  (*_MPCI_table->send_packet)( destination, the_packet );
286
287  _Thread_queue_Acquire( &_MPCI_Remote_blocked_threads, &queue_context );
288  _Thread_queue_Enqueue(
289    &_MPCI_Remote_blocked_threads.Queue,
290    &_Thread_queue_Operations_FIFO,
291    executing,
292    &queue_context
293  );
294  return _Thread_Wait_get_status( executing );
295}
296
297void _MPCI_Send_response_packet (
298  uint32_t            destination,
299  MP_packet_Prefix   *the_packet
300)
301{
302  the_packet->source_tid = _Thread_Executing->Object.id;
303
304  (*_MPCI_table->send_packet)( destination, the_packet );
305}
306
307MP_packet_Prefix  *_MPCI_Receive_packet ( void )
308{
309  MP_packet_Prefix  *the_packet;
310
311  (*_MPCI_table->receive_packet)( &the_packet );
312
313  return the_packet;
314}
315
316Thread_Control *_MPCI_Process_response (
317  MP_packet_Prefix  *the_packet
318)
319{
320  ISR_lock_Context  lock_context;
321  Thread_Control   *the_thread;
322
323  the_thread = _Thread_Get( the_packet->id, &lock_context );
324  _Assert( the_thread != NULL );
325
326  /*
327   * FIXME: This is broken on SMP, see https://devel.rtems.org/ticket/2703.
328   *
329   * Should use _Thread_queue_Extract_critical() instead with a handler
330   * function provided by the caller of _MPCI_Process_response().  Similar to
331   * the filter function in _Thread_queue_Flush_critical().
332   */
333  _ISR_lock_ISR_enable( &lock_context );
334  _Thread_queue_Extract( the_thread );
335  the_thread->Wait.return_code = the_packet->return_code;
336  return the_thread;
337}
338
339/*
340 *  _MPCI_Receive_server
341 *
342 */
343
344void _MPCI_Receive_server(
345  Thread_Entry_numeric_type ignored
346)
347{
348
349  MP_packet_Prefix      *the_packet;
350  MPCI_Packet_processor  the_function;
351  Thread_Control        *executing;
352  Thread_queue_Context   queue_context;
353
354  executing = _Thread_Get_executing();
355  _Thread_queue_Context_initialize( &queue_context );
356  _Thread_queue_Context_set_enqueue_do_nothing_extra( &queue_context );
357
358  for ( ; ; ) {
359
360    executing->receive_packet = NULL;
361
362    _ISR_lock_ISR_disable( &queue_context.Lock_context.Lock_context );
363    _CORE_semaphore_Seize(
364      &_MPCI_Semaphore,
365      MPCI_SEMAPHORE_TQ_OPERATIONS,
366      executing,
367      true,
368      &queue_context
369    );
370
371    for ( ; ; ) {
372      executing->receive_packet = NULL;
373
374      the_packet = _MPCI_Receive_packet();
375
376      if ( !the_packet )
377        break;
378
379      executing->receive_packet = the_packet;
380
381      if ( !_Mp_packet_Is_valid_packet_class ( the_packet->the_class ) )
382        break;
383
384      the_function = _MPCI_Packet_processors[ the_packet->the_class ];
385
386      if ( !the_function )
387        _Internal_error( INTERNAL_ERROR_BAD_PACKET );
388
389       (*the_function)( the_packet );
390    }
391  }
392}
393
394void _MPCI_Announce ( void )
395{
396  Thread_queue_Context queue_context;
397
398  _ISR_lock_ISR_disable( &queue_context.Lock_context.Lock_context );
399  (void) _CORE_semaphore_Surrender(
400    &_MPCI_Semaphore,
401    MPCI_SEMAPHORE_TQ_OPERATIONS,
402    UINT32_MAX,
403    &queue_context
404  );
405}
406
407void _MPCI_Internal_packets_Send_process_packet (
408   MPCI_Internal_Remote_operations operation
409)
410{
411  MPCI_Internal_packet *the_packet;
412
413  switch ( operation ) {
414
415    case MPCI_PACKETS_SYSTEM_VERIFY:
416
417      the_packet                    = _MPCI_Internal_packets_Get_packet();
418      the_packet->Prefix.the_class  = MP_PACKET_MPCI_INTERNAL;
419      the_packet->Prefix.length     = sizeof ( MPCI_Internal_packet );
420      the_packet->Prefix.to_convert = sizeof ( MPCI_Internal_packet );
421      the_packet->operation         = operation;
422
423      the_packet->maximum_nodes = _Objects_Maximum_nodes;
424
425      the_packet->maximum_global_objects = _Objects_MP_Maximum_global_objects;
426
427      _MPCI_Send_process_packet( MPCI_ALL_NODES, &the_packet->Prefix );
428      break;
429  }
430}
431
432/*
433 *  _MPCI_Internal_packets_Send_request_packet
434 *
435 *  This subprogram is not needed since there are no request
436 *  packets to be sent by this manager.
437 *
438 */
439
440/*
441 *  _MPCI_Internal_packets_Send_response_packet
442 *
443 *  This subprogram is not needed since there are no response
444 *  packets to be sent by this manager.
445 *
446 */
447
448void _MPCI_Internal_packets_Process_packet (
449  MP_packet_Prefix  *the_packet_prefix
450)
451{
452  MPCI_Internal_packet *the_packet;
453  uint32_t                    maximum_nodes;
454  uint32_t                    maximum_global_objects;
455
456  the_packet = (MPCI_Internal_packet *) the_packet_prefix;
457
458  switch ( the_packet->operation ) {
459
460    case MPCI_PACKETS_SYSTEM_VERIFY:
461
462      maximum_nodes          = the_packet->maximum_nodes;
463      maximum_global_objects = the_packet->maximum_global_objects;
464      if ( maximum_nodes != _Objects_Maximum_nodes ||
465           maximum_global_objects != _Objects_MP_Maximum_global_objects ) {
466
467        _MPCI_Return_packet( the_packet_prefix );
468
469        _Internal_error( INTERNAL_ERROR_INCONSISTENT_MP_INFORMATION );
470      }
471
472      _MPCI_Return_packet( the_packet_prefix );
473
474      break;
475  }
476}
477
478/*
479 *  _MPCI_Internal_packets_Send_object_was_deleted
480 *
481 *  This subprogram is not needed since there are no objects
482 *  deleted by this manager.
483 *
484 */
485
486/*
487 *  _MPCI_Internal_packets_Send_extract_proxy
488 *
489 *  This subprogram is not needed since there are no objects
490 *  deleted by this manager.
491 *
492 */
493
494MPCI_Internal_packet *_MPCI_Internal_packets_Get_packet ( void )
495{
496  return ( (MPCI_Internal_packet *) _MPCI_Get_packet() );
497}
498
499static void _MPCI_Finalize( void )
500{
501  if ( _System_state_Is_multiprocessing ) {
502    _MPCI_Initialization();
503    _MPCI_Internal_packets_Send_process_packet( MPCI_PACKETS_SYSTEM_VERIFY );
504  }
505}
506
507RTEMS_SYSINIT_ITEM(
508  _MPCI_Handler_early_initialization,
509  RTEMS_SYSINIT_MP_EARLY,
510  RTEMS_SYSINIT_ORDER_MIDDLE
511);
512
513RTEMS_SYSINIT_ITEM(
514  _MPCI_Handler_initialization,
515  RTEMS_SYSINIT_MP,
516  RTEMS_SYSINIT_ORDER_MIDDLE
517);
518
519RTEMS_SYSINIT_ITEM(
520  _MPCI_Create_server,
521  RTEMS_SYSINIT_MP_SERVER,
522  RTEMS_SYSINIT_ORDER_MIDDLE
523);
524
525RTEMS_SYSINIT_ITEM(
526  _MPCI_Finalize,
527  RTEMS_SYSINIT_MP_FINALIZE,
528  RTEMS_SYSINIT_ORDER_MIDDLE
529);
530
531/* end of file */
Note: See TracBrowser for help on using the repository browser.