1    /*
     2     * Copyright (c) 2018, 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     *  ======== SAU.xdc ========
    34     */
    35    
    36    package ti.sysbios.family.arm.v8m;
    37    
    38    import xdc.rov.ViewInfo;
    39    
    40    /*!
    41     *  ======== SAU ========
    42     *  Security Attribution Unit (SAU) Manager.
    43     *
    44     *  This module manages the Security Attribution Unit (SAU) present in ARMv8
    45     *  Cortex-V8M devices. It enables the application to partition the memory
    46     *  into different regions and set security attributes for each region.
    47     *
    48     *  The number of memory regions supported is device specific and may vary
    49     *  on different devices.
    50     *
    51     *  @a(Examples)
    52     *  Example showing how to set attributes for a given memory region using
    53     *  *.cfg script:
    54     *
    55     *  @p(code)
    56     *  var SAU = xdc.useModule('ti.sysbios.family.arm.v8m.SAU');
    57     *  SAU.enableSAU = true;
    58     *
    59     *  // define NSC region
    60     *  SAU.configureRegionMeta(0, 0x2C410000, 0x00010000, SAU.RegionType_NSC);
    61     *  // define NS code/data region
    62     *  SAU.configureRegionMeta(1, 0x2C420000, 0x00020000, SAU.RegionType_NS);
    63     *  // allow NS access to SCB
    64     *  SAU.configureRegionMeta(2, 0xE0000000, 0x10000000, SAU.RegionType_NS);
    65     *  @p
    66     *
    67     *  @p(html)
    68     *  <h3> Calling Context </h3>
    69     *  <table border="1" cellpadding="3">
    70     *    <colgroup span="1"></colgroup> <colgroup span="5" align="center">
    71     *    </colgroup>
    72     *
    73     *    <tr>        <th> Function </th>
    74     *    <th> Hwi </th><th> Swi </th><th> Task </th><th> Main </th><th> Startup </th></tr>
    75     *    <tr><td> {@link #configureRegion} </td>
    76     *    <td>  Y  </td><td>  Y  </td><td>  Y   </td><td>  Y   </td><td>   Y   </td></tr>
    77     *    <tr><td> {@link #disable} </td>
    78     *    <td>  Y  </td><td>  Y  </td><td>  Y   </td><td>  Y   </td><td>   Y   </td></tr>
    79     *    <tr><td> {@link #disableRegion} </td>
    80     *    <td>  Y  </td><td>  Y  </td><td>  Y   </td><td>  Y   </td><td>   Y   </td></tr>
    81     *    <tr><td> {@link #enable} </td>
    82     *    <td>  Y  </td><td>  Y  </td><td>  Y   </td><td>  Y   </td><td>   Y   </td></tr>
    83     *    <tr><td> {@link #enableRegion} </td>
    84     *    <td>  Y  </td><td>  Y  </td><td>  Y   </td><td>  Y   </td><td>   Y   </td></tr>
    85     *    <tr><td> {@link #isEnabled} </td>
    86     *    <td>  Y  </td><td>  Y  </td><td>  Y   </td><td>  Y   </td><td>   Y   </td></tr>
    87     *    <tr><td colspan="6"> Definitions: <br />
    88     *       <ul>
    89     *         <li> <b>Hwi</b>: API is callable from a Hwi thread. </li>
    90     *         <li> <b>Swi</b>: API is callable from a Swi thread. </li>
    91     *         <li> <b>Task</b>: API is callable from a Task thread. </li>
    92     *         <li> <b>Main</b>: API is callable during any of these phases: </li>
    93     *           <ul>
    94     *             <li> In your module startup. </li>
    95     *             <li> During xdc.runtime.Startup.lastFxns. </li>
    96     *             <li> During main().</li>
    97     *             <li> During BIOS.startupFxns.</li>
    98     *           </ul>
    99     *         <li> <b>Startup</b>: API is callable during any of these phases:</li>
   100     *           <ul>
   101     *             <li> During xdc.runtime.Startup.firstFxns.</li>
   102     *             <li> In your module startup.</li>
   103     *           </ul>
   104     *       </ul>
   105     *    </td></tr>
   106     *
   107     *  </table>
   108     *  @p
   109     */
   110    
   111    @DirectCall
   112    module SAU
   113    {
   114        // -------- ROV views --------
   115    
   116        /*! @_nodoc */
   117        metaonly struct RegionsView {
   118            UInt8       RegionIdx;
   119            Bool        Enabled;
   120            String      BaseAddress;
   121            String      EndAddress;
   122            String      RegionType;
   123        };
   124    
   125        /*! @_nodoc */
   126        metaonly struct RegsView {
   127            UInt8       SREGION;
   128            Bool        ENABLE;
   129            String      ALLNS;
   130            Ptr         SFSR;
   131            Ptr         SFAR;
   132        };
   133    
   134        @Facet
   135        metaonly config ViewInfo.Instance rovViewInfo =
   136            ViewInfo.create({
   137                viewMap: [
   138                    [
   139                        'SauRegions', {
   140                            type: ViewInfo.MODULE_DATA,
   141                            viewInitFxn: 'viewSauRegions',
   142                            structName: 'RegionsView'
   143                        }
   144                    ],
   145                    [
   146                        'SauRegisters', {
   147                            type: ViewInfo.MODULE_DATA,
   148                            viewInitFxn: 'viewSauRegisters',
   149                            structName: 'RegsView'
   150                        }
   151                    ],
   152               ]
   153           });
   154    
   155        /*!
   156         *  Security Attribution Unit (SAU) registers. Symbol "SAU_deviceRegs" is
   157         *  the physical device
   158         */
   159        struct DeviceRegs {
   160            UInt32 CTRL;            /*! 0xE000EDD0 Control Register                   */
   161            UInt32 TYPE;            /*! 0xE000EDD4 Type Register                      */
   162            UInt32 RNR;             /*! 0xE000EDD8 Region Number Register             */
   163            UInt32 RBAR;            /*! 0xE000EDDC Region Base Address Register       */
   164            UInt32 RLAR;            /*! 0xE000EDE0 Region Base Limit Register         */
   165            UInt32 SFSR;            /*! 0xE000EDE4 Secure Fault Status Register       */
   166            UInt32 SFAR;            /*! 0xE000EDE8 Secure Fault Address Register      */
   167        };
   168    
   169        /*! @_nodoc global SAU_deviceRegs struct */
   170        extern volatile DeviceRegs deviceRegs;
   171    
   172        /*!
   173         *  ======== RegionType ========
   174         */
   175        enum RegionType {
   176            RegionType_NS,              /*! Region is Non-Secure */
   177            RegionType_NSC              /*! Region is Secure, Non-Secure Callable */
   178        };
   179    
   180        /*!
   181         *  ======== A_nullPointer ========
   182         *  Assert raised when a pointer is null
   183         */
   184        config xdc.runtime.Assert.Id A_nullPointer  = {
   185            msg: "A_nullPointer: Pointer is null"
   186        };
   187    
   188        /*!
   189         *  ======== A_invalidRegionId ========
   190         *  Assert raised when an invalid region number is passed to
   191         *  SAU_configureRegion(), SAU_enableRegion(), SAU_disableRegion()
   192         */
   193        config xdc.runtime.Assert.Id A_invalidRegionId  = {
   194            msg: "A_invalidRegionId: SAU Region number passed is invalid."
   195        };
   196    
   197        /*!
   198         *  ======== A_unalignedBaseAddr ========
   199         *  Assert raised when region's base address is not aligned.
   200         */
   201        config xdc.runtime.Assert.Id A_unalignedBaseAddr  = {
   202            msg: "A_unalignedBaseAddr: SAU region base address not aligned."
   203        };
   204    
   205        /*!
   206         *  ======== A_unalignedEndAddr ========
   207         *  Assert raised when region's end address is not aligned.
   208         */
   209        config xdc.runtime.Assert.Id A_unalignedEndAddr  = {
   210            msg: "A_unalignedEndAddr: SAU region end address not aligned."
   211        };
   212    
   213        /*!
   214         *  ======== enableSAU ========
   215         *  Configuration parameter to enable SAU. Disabled by default.
   216         */
   217        config Bool enableSAU = false;
   218    
   219        /*!
   220         *  ======== allSecureWhenSauDisabled ========
   221         *  Configuration parameter to set all memory type when SAU is disabled
   222         *
   223         *  If the SAU is disabled, this parameter determines if all of
   224         *  memory is configured as Secure or Non Secure.
   225         *
   226         *  Default is all memory is marked as Secure if the SAU is disabled.
   227         */
   228        config Bool allSecureWhenSauDisabled = true;
   229    
   230        /*!
   231         *  @_nodoc
   232         *  ======== numRegions ========
   233         *  Number of SAU regions. Default is determined based on device type.
   234         */
   235        config UInt8 numRegions;
   236    
   237        /*!
   238         *  ======== configureRegionMeta ========
   239         *  Statically sets the SAU region attributes
   240         *
   241         *  @see ti.sysbios.family.arm.r5.SAU
   242         *
   243         *  By default, the region is enabled.
   244         *  Call disableRegionMeta(regionId) to disable the region.
   245         *
   246         *  @param(regionId)        SAU region number
   247         *  @param(regionBase)      SAU region base address
   248         *  @param(regionSize)      SAU region size in bytes
   249         *  @param(regionType)      RegionType
   250         */
   251        metaonly Void configureRegionMeta(UInt8 regionId, Ptr regionBase,
   252            SizeT regionSize, RegionType regionType);
   253    
   254        /*!
   255         *  ======== disableRegionMeta ========
   256         *  disable a SAU region
   257         *
   258         *  @param(regionId)        SAU region number
   259         */
   260        metaonly Void disableRegionMeta(UInt8 regionId);
   261    
   262        /*!
   263         *  ======== disable ========
   264         *  Disables the SAU.
   265         *
   266         *  If the SAU is already disabled, then simply return.
   267         */
   268        Void disable();
   269    
   270        /*!
   271         *  ======== enable ========
   272         *  Enables the SAU.
   273         *
   274         *  If the SAU is already enabled, then simply return.
   275         */
   276        Void enable();
   277    
   278        /*!
   279         *  ======== isEnabled ========
   280         *  Determines if the SAU is enabled
   281         */
   282        Bool isEnabled();
   283    
   284        /*!
   285         *  ======== configureRegion ========
   286         *  Configure a SAU region
   287         *
   288         *  @see ti.sysbios.family.arm.v8.SAU
   289         *
   290         *  By default, the region is enabled.
   291         *  Call disableRegion(regionId) to disable the region.
   292         *
   293         *  @param(regionId)        SAU region number
   294         *  @param(regionBase)      SAU region base address
   295         *  @param(regionSize)      SAU region size in bytes
   296         *  @param(regionType)      RegionType (NS/NSC)
   297         */
   298        Void configureRegion(UInt8 regionId, Ptr regionBase,
   299            SizeT regionSize, RegionType regionTupe);
   300    
   301        /*!
   302         *  ======== enableRegion ========
   303         *  Enable a SAU region
   304         *
   305         *  @param(regionId)        SAU region number
   306         */
   307        Void enableRegion(UInt8 regionId);
   308    
   309        /*!
   310         *  ======== disableRegion ========
   311         *  Disable a SAU region
   312         *
   313         *  @param(regionId)        SAU region number
   314         */
   315        Void disableRegion(UInt8 regionId);
   316    
   317    internal:
   318    
   319        /*
   320         *  ======== RegionEntry ========
   321         */
   322        struct RegionEntry {
   323            UInt32 rbar;
   324            UInt32 rlar;
   325        };
   326    
   327        /*
   328         *  ======== regionEntry ========
   329         *  Array to hold statically configured SAU region entries
   330         */
   331        metaonly config RegionEntry regionEntry[];
   332    
   333        /*
   334         *  ======== startup ========
   335         *  startup function to initialize SAU module
   336         */
   337        Void startup();
   338    
   339        /*! Module state */
   340        struct Module_State {
   341            volatile RegionEntry regionEntry[];  // Array to hold a copy of SAU region
   342                                                 // settings
   343        }
   344    }