1    /*
     2     * Copyright (c) 2012-2013, Texas Instruments Incorporated
     3     * All rights reserved.
     4     *
     5     * Redistribution and use in source and binary forms, with or without
     6     * modification, are permitted provided that the following conditions
     7     * are met:
     8     *
     9     * *  Redistributions of source code must retain the above copyright
    10     *    notice, this list of conditions and the following disclaimer.
    11     *
    12     * *  Redistributions in binary form must reproduce the above copyright
    13     *    notice, this list of conditions and the following disclaimer in the
    14     *    documentation and/or other materials provided with the distribution.
    15     *
    16     * *  Neither the name of Texas Instruments Incorporated nor the names of
    17     *    its contributors may be used to endorse or promote products derived
    18     *    from this software without specific prior written permission.
    19     *
    20     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    21     * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
    22     * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
    23     * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
    24     * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
    25     * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    26     * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
    27     * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
    28     * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
    29     * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
    30     * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    31     */
    32    /*
    33     *  ======== GatePetersonN.xdc ========
    34     *
    35     */
    36    
    37    package ti.sdo.ipc.gates;
    38    
    39    import xdc.runtime.Error;
    40    import xdc.runtime.Assert;
    41    import xdc.runtime.IGateProvider;
    42    import xdc.runtime.Diags;
    43    import xdc.runtime.Log;
    44    import xdc.rov.ViewInfo;
    45    
    46    import ti.sdo.utils.MultiProc;
    47    import ti.sdo.ipc.Ipc;
    48    
    49    import ti.sdo.ipc.interfaces.IGateMPSupport;
    50    
    51    /*!
    52     *  ======== GatePetersonN (for N processors) ========
    53     *  IGateMPSupport gate based on the Peterson's algorithm
    54     *
    55     *  This module implements the {@link ti.sdo.ipc.interfaces.IGateMPSupport}
    56     *  interface using the Peterson Algorithm in shared memory. This
    57     *  implementation works for N processors.
    58     *
    59     *  Each GatePetersonN instance requires a small piece of
    60     *  shared memory.  The base address of this shared memory is specified as
    61     *  the 'sharedAddr' argument to the create. The amount of shared memory
    62     *  consumed by a single instance can be obtained using the
    63     *  {@link #sharedMemReq} call.
    64     *
    65     *  Shared memory has to conform to the following specification.  Padding is
    66     *  added between certain elements in shared memory if cache alignment is
    67     *  required for the region in which the instance is placed.
    68     *
    69     *  @p(code)
    70     *
    71     *              shmBaseAddr -> ------------------------------ bytes
    72     *                             |  enteredStage[0]           | 4
    73     *                             |  (PADDING if aligned)      |
    74     *                             |----------------------------|
    75     *                             |  enteredStage[1]           | 4
    76     *                             |  (PADDING if aligned)      |
    77     *                             |----------------------------|
    78     *                                      . . . 
    79     *                             |----------------------------|
    80     *                             |  enteredStage[N-1]         | 4
    81     *                             |  (PADDING if aligned)      |
    82     *                             |----------------------------|
    83     *                             |  lastProcEnteringStage[1]  | 4
    84     *                             |  (PADDING if aligned)      |
    85     *                             |----------------------------|
    86     *                                      . . . 
    87     *                             |----------------------------|
    88     *                             |  lastProcEnteringStage[N-1]| 4
    89     *                             |  (PADDING if aligned)      |
    90     *                             |----------------------------|
    91     *  @p
    92     */
    93    @InstanceInitError
    94    @InstanceFinalize
    95    
    96    module GatePetersonN inherits IGateMPSupport
    97    {
    98        /*! @_nodoc */
    99        metaonly struct BasicView {
   100            String  objType;
   101            Ptr     localGate;
   102            UInt    nested;
   103            String  gateOwner;
   104        }
   105    
   106        /*! @_nodoc */
   107        @Facet
   108        metaonly config ViewInfo.Instance rovViewInfo =
   109            ViewInfo.create({
   110                viewMap: [
   111                    ['Basic',
   112                        {
   113                            type: ViewInfo.INSTANCE,
   114                            viewInitFxn: 'viewInitBasic',
   115                            structName: 'BasicView'
   116                        }
   117                    ],
   118                ]
   119            });
   120    
   121        /*!
   122         *  ======== numInstances ========
   123         *  Maximum number of instances supported by the GatePetersonN module
   124         */
   125        config UInt numInstances = 512;
   126        config UInt MAX_NUM_PROCS  =  8;
   127    
   128    instance:
   129    
   130        /*!
   131         *  @_nodoc
   132         *  ======== enter ========
   133         *  Enter this gate
   134         */
   135        @DirectCall
   136        override IArg enter();
   137    
   138        /*!
   139         *  @_nodoc
   140         *  ======== leave ========
   141         *  Leave this gate
   142         */
   143        @DirectCall
   144        override Void leave(IArg key);
   145    
   146    internal:
   147    
   148        const Int32 NOT_INTERESTED = -1;
   149    
   150        /* initializes shared memory */
   151        Void postInit(Object *obj);
   152    
   153        struct Instance_State {
   154            volatile Int32 *enteredStage[MAX_NUM_PROCS];
   155            volatile Int32 *lastProcEnteringStage[MAX_NUM_PROCS-1];
   156            UInt16          selfId;
   157            UInt16          numProcessors;
   158            UInt            nested;    /* For nesting */
   159            IGateProvider.Handle localGate;
   160            Ipc.ObjType     objType;
   161            SizeT           cacheLineSize;
   162            Bool            cacheEnabled;
   163        };
   164    }