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 * ======== GateHWSem.c ========
34 */
36 #include <xdc/std.h>
37 #include <xdc/runtime/Assert.h>
38 #include <xdc/runtime/IGateProvider.h>
39 #include <xdc/runtime/Startup.h>
41 #include <ti/sdo/ipc/interfaces/IGateMPSupport.h>
43 #include "package/internal/GateHWSem.xdc.h"
45 /*
46 * ======== GateHWSem_Module_startup ========
47 * release the HW semaphore if the core owns it.
48 */
49 Int GateHWSem_Module_startup(Int phase)
50 {
51 Int i;
52 volatile UInt32 *baseAddr = (volatile UInt32 *)GateHWSem_baseAddr;
53 Bits32 *reservedMaskArr = (Bits32 *)GateHWSem_reservedMaskArr;
55 /* releases the HW semaphore if the core owns it otherwise its a nop */
56 for (i = 0; i < GateHWSem_numSems; i++) {
57 if (reservedMaskArr[i >> 5] & (1 << (i % 32))) {
58 /* Don't reset reserved HW semaphores */
59 continue;
60 }
61 baseAddr[i] = 1;
62 }
64 return (Startup_DONE);
65 }
67 /*
68 *************************************************************************
69 * Instance functions
70 *************************************************************************
71 */
73 /*
74 * ======== GateHWSem_Instance_init ========
75 */
76 Void GateHWSem_Instance_init(GateHWSem_Object *obj,
77 IGateProvider_Handle localGate,
78 const GateHWSem_Params *params)
79 {
80 volatile UInt32 *baseAddr = (volatile UInt32 *)GateHWSem_baseAddr;
82 /* Assert that params->resourceId is valid */
83 Assert_isTrue(params->resourceId < GateHWSem_numSems,
84 GateHWSem_A_invSemNum);
86 /* Create the local gate */
87 obj->localGate = localGate;
88 obj->semNum = params->resourceId;
89 obj->nested = 0;
91 /* Check for dynamic open */
92 if (!params->openFlag) {
93 /* Reset the hardware semaphore */
94 baseAddr[obj->semNum] = 1;
95 }
96 }
98 /*
99 * ======== GateHWSem_enter ========
100 */
101 IArg GateHWSem_enter(GateHWSem_Object *obj)
102 {
103 IArg key;
104 volatile UInt32 *baseAddr = (volatile UInt32 *)GateHWSem_baseAddr;
106 key = IGateProvider_enter(obj->localGate);
108 /* If the gate object has already been entered, return the nested value */
109 obj->nested++;
110 if (obj->nested > 1) {
111 return (key);
112 }
114 /* Enter the hardware lock */
115 while (baseAddr[obj->semNum] != 1) {
116 obj->nested--; /* Restore state of delegate object */
117 IGateProvider_leave(obj->localGate, key);
118 key = IGateProvider_enter(obj->localGate);
119 obj->nested++; /* Re-nest the gate */
120 }
122 return (key);
123 }
125 /*
126 * ======== GateHWSem_leave ========
127 */
128 Void GateHWSem_leave(GateHWSem_Object *obj, IArg key)
129 {
130 volatile UInt32 *baseAddr = (volatile UInt32 *)GateHWSem_baseAddr;
132 obj->nested--;
134 if (obj->nested == 0) {
135 /* Leave the critical region by setting address value to 1 */
136 baseAddr[obj->semNum] = 1;
137 }
139 IGateProvider_leave(obj->localGate, key);
140 }
142 /*
143 * ======== GateHWSem_getResourceId ========
144 */
145 Bits32 GateHWSem_getResourceId(GateHWSem_Object *obj)
146 {
147 return (obj->semNum);
148 }
150 /*
151 *************************************************************************
152 * Module functions
153 *************************************************************************
154 */
156 /*
157 * ======== GateHWSem_getReservedMask ========
158 */
159 Bits32 *GateHWSem_getReservedMask()
160 {
161 return ((Bits32 *)GateHWSem_reservedMaskArr);
162 }
164 /*
165 * ======== GateHWSem_query ========
166 */
167 Bool GateHWSem_query(Int qual)
168 {
169 Bool rc;
171 switch (qual) {
172 case IGateProvider_Q_BLOCKING:
173 /* Depends on gate proxy? */
174 rc = TRUE;
175 break;
177 case IGateProvider_Q_PREEMPTING:
178 /* Depends on gate proxy? */
179 rc = TRUE;
180 break;
181 default:
182 rc = FALSE;
183 break;
184 }
185 return (rc);
186 }
188 /*
189 * ======== GateHWSem_sharedMemReq ========
190 */
191 SizeT GateHWSem_sharedMemReq(const IGateMPSupport_Params *params)
192 {
193 /* No shared memory needed */
194 return (0);
195 }