/
xlnx_xrm_utils.c
285 lines (259 loc) · 10.4 KB
/
xlnx_xrm_utils.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
* Copyright (C) 2021, Xilinx Inc - All rights reserved
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may
* not use this file except in compliance with the License. A copy of the
* License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
#include "xlnx_xrm_utils.h"
XlnxIPMapping xlnx_ip_mapping[XRM_MAX_NUM_IP] = {
{"DECODER", "xrmU30DecPlugin", "decoder", "DECODER_MPSOC",
"kernel_vcu_decoder", 1},
{"SCALER", "xrmU30ScalPlugin", "scaler", "SCALER_MPSOC", "", 0},
{"LOOKAHEAD", "xrmU30EncPlugin", "lookahead", "LOOKAHEAD_MPSOC", "", 0},
{"ENCODER", "xrmU30EncPlugin", "encoder", "ENCODER_MPSOC",
"kernel_vcu_encoder", 1}};
/**
* xlnx_xrm_init: Create XRM context
*
* @return XRM context or NULL
*/
xrmContext xlnx_xrm_create_context()
{
/* Create XRM context */
xrmContext xrm_ctx = xrmCreateContext(XRM_API_VERSION_1);
if(xrm_ctx == NULL) {
xma_logmsg(XMA_ERROR_LOG, XLNX_XMA_APP_MODULE,
"creation of XRM context failed\n");
return NULL;
}
return xrm_ctx;
}
/**
* xlnx_xrm_deinit: Deinit XRM
*
* @param xrm_ctx: XRM context
* @param cu_reserve_id: CU reservation index
*/
void xlnx_xrm_deinit(xrmContext xrm_ctx, int32_t reserve_idx)
{
if(reserve_idx > 0) {
xrmCuPoolRelinquish(xrm_ctx, reserve_idx);
}
xrmDestroyContext(xrm_ctx);
return;
}
/**
* xlnx_xma_props_to_json: Converts the xma props to json for the given
* function name
*
* @param props: The xma props, be they encoder, decoder, or scaler props
* @param func_name: The name of the function - ENCODER, DECODER, LOOKAHEAD,
* SCALER
* @param json_job: json props
* @return XMA_APP_SUCCESS on success
*/
static int32_t xlnx_xma_props_to_json(void* props, char* func_name,
char* json_job)
{
void* handle;
/* Loading propstojson library to convert xma properties to json */
void (*convert_xma_props_to_json)(void* props, char* func_name,
char* json_job);
handle = dlopen(XMA_PROPS_TO_JSON_SO, RTLD_NOW);
convert_xma_props_to_json = dlsym(handle, "convertXmaPropsToJson");
(*convert_xma_props_to_json)(props, func_name, json_job);
dlclose(handle);
return XMA_APP_SUCCESS;
}
/**
* xlnx_get_mapping_id: Returns the mapping index for the IP
*
* @param ip_name: Name of the IP module
* @return Mapping ID or -1 on failure
*/
static int32_t xlnx_get_mapping_id(char* ip_name)
{
for(int32_t i = 0; i < XRM_MAX_NUM_IP; i++) {
if(!strcmp(xlnx_ip_mapping[i].ip_name, ip_name)) {
return i;
}
}
return XMA_APP_ERROR;
}
/**
* xlnx_xrm_load_calc: Calculates load for the given module
*
* @param xrm_ctx: XRM context
* @param xma_props: XMA properties
* @param ip_name: Name of the IP module
* @return CU load or XMA_APP_ERROR on error
*/
int32_t xlnx_xrm_load_calc(xrmContext xrm_ctx, void* xma_props, char* ip_name)
{
int32_t ret;
int32_t skip_value;
int32_t cu_load = 0;
int32_t func_id = 0;
int32_t mapping_id = -1;
xrmPluginFuncParam plg_param;
mapping_id = xlnx_get_mapping_id(ip_name);
memset(&plg_param, 0, sizeof(xrmPluginFuncParam));
xlnx_xma_props_to_json(xma_props, ip_name, plg_param.input);
ret = xrmExecPluginFunc(xrm_ctx, xlnx_ip_mapping[mapping_id].plugin_name,
func_id, &plg_param);
if(ret != XRM_SUCCESS) {
xma_logmsg(XMA_ERROR_LOG, XLNX_XMA_APP_MODULE,
"XRM plugin failed to calculate load for %s: %d\n", ip_name,
ret);
return XMA_APP_ERROR;
}
cu_load = atoi((char*)(strtok(plg_param.output, " ")));
skip_value = atoi((char*)(strtok(NULL, " ")));
(void)skip_value; /* To avoid warning */
if(!strcmp(ip_name, "LOOKAHEAD")) {
cu_load = atoi((char*)(strtok(NULL, " ")));
}
return cu_load;
}
/**
* xlnx_update_pool_props: Updates CU pool properties
*
* @param ip_name: Name of the IP module
* @param cu_load: CU load for the module
* @param num_soft_kernel: Number of soft kernels
* @param cu_pool_prop: CU pool properties
* @return XMA_APP_SUCCESS or XMA_APP_ERROR
*/
int32_t xlnx_update_pool_props(char* ip_name, int32_t cu_load,
int8_t num_soft_kernel,
xrmCuPoolPropertyV2* cu_pool_prop)
{
int32_t cu_num = cu_pool_prop->cuListProp.cuNum;
int32_t mapping_id = -1;
cu_pool_prop->cuListNum = 1;
mapping_id = xlnx_get_mapping_id(ip_name);
if(cu_load > 0) {
strcpy(cu_pool_prop->cuListProp.cuProps[cu_num].kernelName,
xlnx_ip_mapping[mapping_id].kernel_name);
strcpy(cu_pool_prop->cuListProp.cuProps[cu_num].kernelAlias,
xlnx_ip_mapping[mapping_id].kernel_alias);
cu_pool_prop->cuListProp.cuProps[cu_num].devExcl = false;
cu_pool_prop->cuListProp.cuProps[cu_num].requestLoad =
XRM_PRECISION_1000000_BIT_MASK(cu_load);
cu_num++;
for(int32_t i = 0; i < num_soft_kernel; i++) {
strcpy(cu_pool_prop->cuListProp.cuProps[cu_num].kernelName,
xlnx_ip_mapping[mapping_id].soft_kernel_name);
strcpy(cu_pool_prop->cuListProp.cuProps[cu_num].kernelAlias, "");
cu_pool_prop->cuListProp.cuProps[cu_num].devExcl = false;
cu_pool_prop->cuListProp.cuProps[cu_num].requestLoad =
XRM_PRECISION_1000000_BIT_MASK(
XRM_MAX_CU_LOAD_GRANULARITY_1000000);
cu_num++;
}
}
cu_pool_prop->cuListProp.cuNum = cu_num;
return XMA_APP_SUCCESS;
}
/**
* Allocates resources and initializes device(s)
* @param xrm_ctx The xrm context
* @param device_ids An array of unique device ids to be initialized
* @param num_devices The size of the array
* @param cu_pool_prop The CU pool properties
* @return XMA_APP_SUCCESS or XMA_APP_ERROR
*/
int32_t xlnx_xrm_device_init(xrmContext xrm_ctx, int32_t* device_ids,
int32_t num_devices,
xrmCuPoolPropertyV2* cu_pool_prop)
{
/* xclbin configuration */
XmaXclbinParameter xclbin_param[num_devices];
int32_t ret = XMA_APP_ERROR;
int32_t num_cu_pool = 0;
/* Check the number of pools available for the given encoder load */
num_cu_pool = xrmCheckCuPoolAvailableNumV2(xrm_ctx, cu_pool_prop);
if(num_cu_pool <= 0) {
xma_logmsg(XMA_ERROR_LOG, XLNX_XMA_APP_MODULE,
"No resources available for allocation\n");
return XMA_APP_ERROR;
}
xma_logmsg(XMA_INFO_LOG, XLNX_XMA_APP_MODULE, "Num CU pools available %d\n",
num_cu_pool);
for(int i = 0; i < num_devices; i++) {
xclbin_param[i].device_id = device_ids[i];
xclbin_param[i].xclbin_name = XCLBIN_PARAM_NAME;
}
if((ret = xma_initialize(xclbin_param, num_devices)) != XMA_SUCCESS) {
xma_logmsg(XMA_ERROR_LOG, XLNX_XMA_APP_MODULE,
"XMA Initialization failed\n");
return XMA_APP_ERROR;
}
xma_logmsg(XMA_INFO_LOG, XLNX_XMA_APP_MODULE,
"XMA initialization success\n");
return XMA_APP_SUCCESS;
}
/**
* xlnx_xrm_cu_alloc: Allocates the xrm cu list based on the requested load for
* a user specified device id or reservation index.
*
* @param xrm_ctx: XRM context
* @param ip_name: Name of the IP module
* @param cu_load: CU load for the module
* @param device_id: Device index
* @param cu_reserve_id: CU reservation index
* @param xrm_cu_list_res: XRM CU list resource structure
* @return XMA_APP_SUCCESS or XMA_APP_ERROR
*/
int32_t xlnx_xrm_cu_alloc(xrmContext xrm_ctx, char* ip_name, int32_t cu_load,
int32_t device_id, int32_t cu_reserve_id,
xrmCuListResourceV2* xrm_cu_list_res)
{
int32_t ret = XMA_APP_ERROR;
int32_t mapping_id = xlnx_get_mapping_id(ip_name);
xrmCuListPropertyV2 xrm_cu_list_prop;
memset(&xrm_cu_list_prop, 0, sizeof(xrmCuListPropertyV2));
memset(xrm_cu_list_res, 0, sizeof(xrmCuListResourceV2));
uint64_t dev_info;
dev_info = (device_id << XRM_DEVICE_INFO_DEVICE_INDEX_SHIFT) |
((uint64_t)XRM_DEVICE_INFO_CONSTRAINT_TYPE_HARDWARE_DEVICE_INDEX
<< XRM_DEVICE_INFO_CONSTRAINT_TYPE_SHIFT);
xrm_cu_list_prop.cuNum = 1;
strcpy(xrm_cu_list_prop.cuProps[0].kernelName,
xlnx_ip_mapping[mapping_id].kernel_name);
strcpy(xrm_cu_list_prop.cuProps[0].kernelAlias,
xlnx_ip_mapping[mapping_id].kernel_alias);
xrm_cu_list_prop.cuProps[0].devExcl = false;
xrm_cu_list_prop.cuProps[0].requestLoad =
XRM_PRECISION_1000000_BIT_MASK(cu_load);
xrm_cu_list_prop.cuProps[0].deviceInfo = dev_info;
xrm_cu_list_prop.cuProps[0].poolId = cu_reserve_id;
if(xlnx_ip_mapping[mapping_id].soft_kernel) {
xrm_cu_list_prop.cuNum++;
strcpy(xrm_cu_list_prop.cuProps[1].kernelName,
xlnx_ip_mapping[mapping_id].soft_kernel_name);
xrm_cu_list_prop.cuProps[1].devExcl = false;
xrm_cu_list_prop.cuProps[1].requestLoad =
XRM_PRECISION_1000000_BIT_MASK(XRM_MAX_CU_LOAD_GRANULARITY_1000000);
xrm_cu_list_prop.cuProps[1].deviceInfo = dev_info;
xrm_cu_list_prop.cuProps[1].poolId = cu_reserve_id;
}
if((ret = xrmCuListAllocV2(xrm_ctx, &xrm_cu_list_prop, xrm_cu_list_res)) !=
0) {
xma_logmsg(XMA_ERROR_LOG, XLNX_XMA_APP_MODULE,
"xrm_allocation: fail to allocate cu list from reserve "
"id %d or device id %d, error %d\n",
cu_reserve_id, device_id, ret);
return XMA_APP_ERROR;
}
return XMA_APP_SUCCESS;
}