Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:08

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /*  Shm_Initialization
0004  *
0005  *  This routine is the shared memory communications initerface
0006  *  driver initialization routine.
0007  *
0008  *  Input parameters:  NONE
0009  *
0010  *  Output parameters: NONE
0011  *
0012  *  COPYRIGHT (c) 1989-1999.
0013  *  On-Line Applications Research Corporation (OAR).
0014  *
0015  * Redistribution and use in source and binary forms, with or without
0016  * modification, are permitted provided that the following conditions
0017  * are met:
0018  * 1. Redistributions of source code must retain the above copyright
0019  *    notice, this list of conditions and the following disclaimer.
0020  * 2. Redistributions in binary form must reproduce the above copyright
0021  *    notice, this list of conditions and the following disclaimer in the
0022  *    documentation and/or other materials provided with the distribution.
0023  *
0024  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0025  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0026  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0027  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0028  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0029  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0030  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0031  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0032  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0033  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0034  * POSSIBILITY OF SUCH DAMAGE.
0035  */
0036 
0037 #define _SHM_INIT
0038 
0039 #include <rtems.h>
0040 #include <shm_driver.h>
0041 
0042 #include <string.h>    /* memset() */
0043 #include <stdlib.h>    /* malloc() */
0044 #include <assert.h>
0045 
0046 /*
0047  * User extension to install MPCI_Fatal as a fatal error
0048  * handler extension
0049  */
0050 
0051 rtems_extensions_table MPCI_Shm_extensions;
0052 
0053 /*
0054  *  MP configuration table from confdefs.h
0055  */
0056 
0057 rtems_mpci_entry Shm_Initialization( void )
0058 
0059 {
0060   uint32_t                 i, all_initialized;
0061   uint32_t                 interrupt_cause, interrupt_value;
0062   void                    *interrupt_address;
0063   Shm_Node_status_control *nscb;
0064   uint32_t                 extension_id;    /* for installation of MPCI_Fatal */
0065   uint32_t                 remaining_memory;
0066   uint32_t                 local_node;
0067 
0068   local_node = rtems_object_get_local_node();
0069 
0070   Shm_Get_configuration( local_node, &Shm_Configuration );
0071 
0072   Shm_Interrupt_table = (Shm_Interrupt_information *) malloc(
0073     sizeof(Shm_Interrupt_information) * (SHM_MAXIMUM_NODES + 1)
0074   );
0075 
0076   assert( Shm_Interrupt_table );
0077 
0078   Shm_Receive_message_count = 0;
0079   Shm_Null_message_count    = 0;
0080   Shm_Interrupt_count       = 0;
0081 
0082   /*
0083    *  Set the Node Status indicators
0084    */
0085 
0086   Shm_Pending_initialization =
0087     Shm_Convert(rtems_build_name( 'P', 'E', 'N', 'D' ));
0088   Shm_Initialization_complete =
0089     Shm_Convert(rtems_build_name( 'C', 'O', 'M', 'P' ));
0090   Shm_Active_node =
0091     Shm_Convert(rtems_build_name( 'A', 'C', 'T', 'V' ));
0092 
0093   /*
0094    *  Initialize the constants used by the Locked Queue code.
0095    */
0096 
0097   Shm_Locked_queue_End_of_list = Shm_Convert( 0xffffffff );
0098   Shm_Locked_queue_Not_on_list = Shm_Convert( 0xfffffffe );
0099 
0100   /*
0101    *  Set the base addresses for the:
0102    *     + Node Status Table
0103    *     + Free Pool and Receive Queues
0104    *     + Envelopes
0105    */
0106 
0107   Shm_Node_statuses  = (Shm_Node_status_control *) START_NS_CBS;
0108   Shm_Locked_queues  = (Shm_Locked_queue_Control *) START_LQ_CBS;
0109   Shm_Envelopes      = (Shm_Envelope_control *) START_ENVELOPES;
0110 
0111   /*
0112    *  Calculate the maximum number of envelopes which can be
0113    *  placed the remaining shared memory.
0114    */
0115 
0116   remaining_memory =
0117      ((void *)Shm_Configuration->base + Shm_Configuration->length) -
0118      ((void *)Shm_Envelopes);
0119 
0120   Shm_Maximum_envelopes = remaining_memory / sizeof( Shm_Envelope_control );
0121   Shm_Maximum_envelopes -= 1;
0122 
0123   /*
0124    *  Set the pointer to the receive queue for the local node.
0125    *  When we receive a node, we will get it from here before
0126    *  processing it.
0127    */
0128 
0129   Shm_Local_receive_queue  = &Shm_Locked_queues[ local_node ];
0130   Shm_Local_node_status    = &Shm_Node_statuses[ local_node ];
0131 
0132   /*
0133    *  Convert local interrupt cause information into the
0134    *  neutral format so other nodes will be able to
0135    *  understand it.
0136    */
0137 
0138   interrupt_address =
0139     (void *) Shm_Convert( (uint32_t)Shm_Configuration->Intr.address );
0140   interrupt_value   = Shm_Convert( Shm_Configuration->Intr.value );
0141   interrupt_cause   = Shm_Convert( Shm_Configuration->Intr.length );
0142 
0143   if ( Shm_Configuration->poll_intr == POLLED_MODE ) Shm_install_timer();
0144   else                                               Shm_setvec();
0145 
0146   if ( Shm_Is_master_node() ) {
0147 
0148     /*
0149      *  Zero out the shared memory area.
0150      */
0151 
0152     (void) memset(
0153       (void *) Shm_Configuration->base,
0154       0,
0155       Shm_Configuration->length
0156     );
0157 
0158     /*
0159      *  Initialize all of the locked queues (the free envelope
0160      *  pool and a receive queue per node) and set all of the
0161      *  node's status so they will be waiting to initialization
0162      *  to complete.
0163      */
0164 
0165     Shm_Locked_queue_Initialize( FREE_ENV_CB, FREE_ENV_POOL );
0166 
0167     for ( i=SHM_FIRST_NODE ; i<=SHM_MAXIMUM_NODES ; i++ ) {
0168       Shm_Initialize_receive_queue( i );
0169 
0170       Shm_Node_statuses[ i ].status = Shm_Pending_initialization;
0171       Shm_Node_statuses[ i ].error  = 0;
0172     }
0173 
0174     /*
0175      *  Initialize all of the envelopes and place them in the
0176      *  free pool.
0177      */
0178 
0179     for ( i=0 ; i<Shm_Maximum_envelopes ; i++ ) {
0180       Shm_Envelopes[ i ].index = Shm_Convert(i);
0181       Shm_Free_envelope( &Shm_Envelopes[ i ] );
0182     }
0183 
0184     /*
0185      *  Initialize this node's interrupt information in the
0186      *  shared area so other nodes can interrupt us.
0187      */
0188 
0189     Shm_Local_node_status->int_address = (uint32_t) interrupt_address;
0190     Shm_Local_node_status->int_value   = interrupt_value;
0191     Shm_Local_node_status->int_length  = interrupt_cause;
0192 
0193     Shm_Local_node_status->status      = Shm_Initialization_complete;
0194 
0195     /*
0196      *  Loop until all nodes have completed initialization.
0197      */
0198 
0199     do {
0200       all_initialized = 1;
0201 
0202       for ( i = SHM_FIRST_NODE ; i <= SHM_MAXIMUM_NODES ; i++ )
0203         if ( Shm_Node_statuses[ i ].status != Shm_Initialization_complete )
0204           all_initialized = 0;
0205 
0206     } while ( all_initialized == 0 );
0207 
0208     /*
0209      *  Tell the other nodes we think that the system is up.
0210      */
0211 
0212     for ( i = SHM_FIRST_NODE ; i <= SHM_MAXIMUM_NODES ; i++ )
0213       Shm_Node_statuses[ i ].status = Shm_Active_node;
0214 
0215   } else {   /* is not MASTER node */
0216 
0217     /*
0218      *  Initialize the node status for the non-master nodes.
0219      *  Because the master node zeroes out memory, it is
0220      *  necessary for them to keep putting their values in
0221      *  the node status area until the master says they
0222      *  should become active.
0223      */
0224 
0225     Shm_Local_node_status->status = Shm_Pending_initialization;
0226 
0227     do {
0228 
0229       if ( Shm_Local_node_status->status == Shm_Pending_initialization ) {
0230 
0231         /*
0232          *  Initialize this node's interrupt information in the
0233          *  shared area so other nodes can interrupt us.
0234          */
0235 
0236         Shm_Local_node_status->int_address =
0237           (uint32_t) interrupt_address;
0238         Shm_Local_node_status->int_value   = interrupt_value;
0239         Shm_Local_node_status->int_length  = interrupt_cause;
0240 
0241         Shm_Local_node_status->status      = Shm_Initialization_complete;
0242       }
0243     } while ( Shm_Local_node_status->status != Shm_Active_node ) ;
0244   }
0245 
0246   /*
0247    *  Initialize the Interrupt Information Table
0248    */
0249 
0250   for ( i = SHM_FIRST_NODE ; i <= SHM_MAXIMUM_NODES ; i++ ) {
0251     nscb = &Shm_Node_statuses[ i ];
0252 
0253     Shm_Interrupt_table[i].address = Shm_Convert_address(
0254       (void *)Shm_Convert(((vol_u32) nscb->int_address))
0255     );
0256     Shm_Interrupt_table[i].value = Shm_Convert( nscb->int_value );
0257     Shm_Interrupt_table[i].length = Shm_Convert( nscb->int_length );
0258   }
0259 
0260   MPCI_Shm_extensions.fatal = MPCI_Fatal;
0261 
0262   (void) rtems_extension_create(
0263     rtems_build_name( 'M', 'P', 'E', 'X' ),
0264     &MPCI_Shm_extensions,
0265     &extension_id
0266   );
0267 }