1 /*
2 * Copyright (c) 2011-2014, 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 */
33 /*
34 * ======== Resource.c ========
35 *
36 */
38 #include <xdc/runtime/System.h>
39 #include <xdc/runtime/Startup.h>
40 #include <ti/sysbios/hal/Cache.h>
42 #include "rsc_types.h"
43 #include "package/internal/Resource.xdc.h"
46 /*
47 * ======== Resource_getTraceBufPtr ========
48 */
49 Ptr Resource_getTraceBufPtr()
50 {
51 UInt32 i;
52 UInt32 offset;
53 UInt32 type;
54 struct fw_rsc_trace *entry = NULL;
55 Resource_RscTable *table = (Resource_RscTable *)
56 (Resource_module->pTable);
58 for (i = 0; i < module->pTable->num; i++) {
59 offset = (UInt32)((Char *)table + table->offset[i]);
60 type = *(UInt32 *)offset;
61 if (type == TYPE_TRACE) {
62 entry = (struct fw_rsc_trace *)offset;
63 return ((Ptr)entry->da);
64 }
65 }
67 return (NULL);
68 }
70 /*
71 * ======== Resource_getTraceBufPtr ========
72 */
73 SizeT Resource_getTraceBufSize()
74 {
75 UInt32 i;
76 UInt32 offset;
77 UInt32 type;
78 struct fw_rsc_trace *entry = NULL;
79 Resource_RscTable *table = (Resource_RscTable *)
80 (Resource_module->pTable);
82 for (i = 0; i < module->pTable->num; i++) {
83 offset = (UInt32)((Char *)table + table->offset[i]);
84 type = *(UInt32 *)offset;
85 if (type == TYPE_TRACE) {
86 entry = (struct fw_rsc_trace *)offset;
87 return ((SizeT)entry->len);
88 }
89 }
91 return (NULL);
92 }
94 /*
95 * ======== getVdevStatus ========
96 */
97 Char Resource_getVdevStatus(UInt32 id)
98 {
99 UInt32 i;
100 UInt32 offset;
101 UInt32 type;
102 Char status = 0;
103 struct fw_rsc_vdev *vdev= NULL;
104 Resource_RscTable *table = (Resource_RscTable *)
105 (Resource_module->pTable);
107 for (i = 0; i < module->pTable->num; i++) {
108 offset = (UInt32)((Char *)table + table->offset[i]);
109 type = *(UInt32 *)offset;
110 if (type == TYPE_VDEV) {
111 vdev = (struct fw_rsc_vdev *)offset;
112 if (vdev->id == id) {
113 /* invalidate memory as host will update the status field */
114 Cache_inv(vdev, sizeof(*vdev), Cache_Type_ALL, TRUE);
115 status = vdev->status;
116 break;
117 }
118 }
119 }
121 return (status);
122 }
124 /*
125 * ======== Resource_getVringDA ========
126 */
127 Ptr Resource_getVringDA(UInt32 vqId)
128 {
129 UInt32 i;
130 UInt32 offset;
131 UInt32 type;
132 Ptr da = NULL;
133 struct fw_rsc_vdev *vdev= NULL;
134 Resource_RscTable *table = (Resource_RscTable *)
135 (Resource_module->pTable);
137 for (i = 0; i < module->pTable->num; i++) {
138 offset = (UInt32)((Char *)table + table->offset[i]);
139 type = *(UInt32 *)offset;
140 if (type == TYPE_VDEV) {
141 vdev = (struct fw_rsc_vdev *)offset;
142 /* Ensure vqID is within expected number of vrings: */
143 if (vqId < vdev->num_of_vrings) {
144 da = (Ptr)(((struct fw_rsc_vdev_vring *)
145 (offset + sizeof(*vdev) +
146 vqId * sizeof(struct fw_rsc_vdev_vring)))->da);
147 }
148 else {
149 break; /* Not found. da is NULL */
150 }
151 }
152 }
154 return (da);
155 }
157 /*
158 * ======== Resource_getMemEntry ========
159 */
160 Resource_MemEntry *Resource_getMemEntry(UInt index)
161 {
162 UInt32 offset;
163 UInt32 *type;
164 Resource_MemEntry *entry = NULL;
165 Resource_RscTable *table = (Resource_RscTable *)
166 (Resource_module->pTable);
168 if (index >= table->num) {
169 return (NULL);
170 }
172 offset = (UInt32)((Char *)table + table->offset[index]);
173 type = (UInt32 *)offset;
174 if (*type == TYPE_CARVEOUT || *type == TYPE_DEVMEM) {
175 entry = (Resource_MemEntry *) ((Char *)offset);
176 }
178 return (entry);
179 }
181 /*
182 *************************************************************************
183 * Module wide functions
184 *************************************************************************
185 */
187 /*
188 * ======== Resource_Module_startup ========
189 */
190 Int Resource_Module_startup(Int phase)
191 {
192 Resource_init();
193 return (Startup_DONE);
194 }
196 /*
197 * ======== Resource_virtToPhys ========
198 */
199 Int Resource_virtToPhys(UInt32 va, UInt32 *pa)
200 {
201 UInt32 i;
202 UInt32 offset;
203 Resource_MemEntry *entry;
205 *pa = 0;
207 for (i = 0; i < module->pTable->num; i++) {
208 entry = Resource_getMemEntry(i);
209 if (entry && va >= entry->da && va < (entry->da + entry->len)) {
210 offset = va - entry->da;
211 *pa = entry->pa + offset;
212 return (Resource_S_SUCCESS);
213 }
214 }
216 return (Resource_E_NOTFOUND);
217 }
219 /*
220 * ======== Resource_physToVirt ========
221 */
222 Int Resource_physToVirt(UInt32 pa, UInt32 *va)
223 {
224 UInt32 i;
225 UInt32 offset;
226 Resource_MemEntry *entry;
228 *va = 0;
230 for (i = 0; i < module->pTable->num; i++) {
231 entry = Resource_getMemEntry(i);
232 if (entry && pa >= entry->pa && pa < (entry->pa + entry->len)) {
233 offset = pa - entry->pa;
234 *va = entry->da + offset;
235 return (Resource_S_SUCCESS);
236 }
237 }
239 return (Resource_E_NOTFOUND);
240 }