2 * Copyright (c) 2014-2016 ARM Limited
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * Authors: Andreas Sandberg
20 #include "libnomali/nomali.h"
24 #include "mali_t6xx.hh"
25 #include "mali_t7xx.hh"
27 #define EXPORT __attribute__ ((visibility ("default")))
29 static const char *errstrs
[] = {
32 "Memory allocation failed",
33 "Invalid model handle",
37 static_assert(sizeof(errstrs
) / sizeof(*errstrs
) == NOMALI_E_NUM_ERRORS
,
38 "NoMali API error descriptions out of sync!");
46 void setGpu(NoMali::GPU
*gpu
) { _gpu
= gpu
; }
49 nomali_error_t
setCallback(const nomali_callback_t
*callback
);
51 nomali_error_t
getInfo(nomali_info_t
*info
);
53 nomali_error_t
reset();
54 nomali_error_t
regRead(uint32_t *value
, nomali_addr_t addr
);
55 nomali_error_t
regWrite(nomali_addr_t addr
, uint32_t value
);
56 nomali_error_t
regReadRaw(uint32_t *value
, nomali_addr_t addr
);
57 nomali_error_t
regWriteRaw(nomali_addr_t addr
, uint32_t value
);
58 nomali_error_t
intState(int *state
, nomali_int_t intno
) const;
61 void callbackInt(nomali_int_t intno
, int set
);
65 nomali_callback_t callbacks
[NOMALI_CALLBACK_NUM_CALLBACKS
];
70 template<class BaseGpu
>
75 template<typename
... Args
>
76 NoMaliApiGpu(NoMaliApi
&_api
, Args
&&... args
)
77 : BaseGpu(std::forward
<Args
>(args
)...),
83 void reset() override
{
89 void intJob(int set
) override
{ api
.callbackInt(NOMALI_INT_JOB
, set
); }
90 void intMMU(int set
) override
{ api
.callbackInt(NOMALI_INT_MMU
, set
); }
91 void intGPU(int set
) override
{ api
.callbackInt(NOMALI_INT_GPU
, set
); }
98 NoMaliApi::NoMaliApi()
101 memset(callbacks
, 0, sizeof(callbacks
));
105 NoMaliApi::~NoMaliApi()
110 NoMaliApi::setCallback(const nomali_callback_t
*callback
)
113 callback
->type
>= NOMALI_CALLBACK_NUM_CALLBACKS
)
114 return NOMALI_E_INVALID
;
116 callbacks
[callback
->type
] = *callback
;
122 NoMaliApi::getInfo(nomali_info_t
*info
)
125 return NOMALI_E_INVALID
;
127 info
->reg_size
= 0x4000;
140 NoMaliApi::regRead(uint32_t *value
, nomali_addr_t addr
)
143 return NOMALI_E_INVALID
;
145 *value
= _gpu
->readReg(NoMali::RegAddr(addr
));
151 NoMaliApi::regWrite(nomali_addr_t addr
, uint32_t value
)
153 _gpu
->writeReg(NoMali::RegAddr(addr
), value
);
160 NoMaliApi::regReadRaw(uint32_t *value
, nomali_addr_t addr
)
163 return NOMALI_E_INVALID
;
165 *value
= _gpu
->readRegRaw(NoMali::RegAddr(addr
));
171 NoMaliApi::regWriteRaw(nomali_addr_t addr
, uint32_t value
)
173 _gpu
->writeRegRaw(NoMali::RegAddr(addr
), value
);
179 NoMaliApi::intState(int *state
, nomali_int_t intno
) const
182 return NOMALI_E_INVALID
;
186 *state
= _gpu
->intGPUAsserted();
190 *state
= _gpu
->intJobAsserted();
194 *state
= _gpu
->intMMUAsserted();
198 return NOMALI_E_INVALID
;
206 NoMaliApi::callbackInt(nomali_int_t intno
, int set
)
208 const nomali_callback_t
&c(callbacks
[NOMALI_CALLBACK_INT
]);
210 if (c
.func
.interrupt
)
211 c
.func
.interrupt(static_cast<nomali_handle_t
>(this), c
.usr
, intno
, set
);
215 NoMaliApi::callbackReset()
217 const nomali_callback_t
&c(callbacks
[NOMALI_CALLBACK_RESET
]);
220 c
.func
.reset(static_cast<nomali_handle_t
>(this), c
.usr
);
226 get_gpu(nomali_handle_t h
)
228 return h
? static_cast<NoMaliApi
*>(h
) : nullptr;
232 extern "C" EXPORT nomali_api_version_t
235 return NOMALI_API_VERSION
;
238 extern "C" EXPORT nomali_error_t
239 nomali_create(nomali_handle_t
*h
, const nomali_config_t
*cfg
)
242 NoMaliApi
*api(new NoMaliApi());
245 return NOMALI_E_MEMORY
;
249 case NOMALI_GPU_T60X
:
250 gpu
= new NoMaliApiGpu
<NoMali::MaliT60x
>(
252 cfg
->ver_maj
, cfg
->ver_min
, cfg
->ver_status
);
255 case NOMALI_GPU_T62X
:
256 gpu
= new NoMaliApiGpu
<NoMali::MaliT62x
>(
258 cfg
->ver_maj
, cfg
->ver_min
, cfg
->ver_status
);
262 case NOMALI_GPU_T76X
:
263 gpu
= new NoMaliApiGpu
<NoMali::MaliT76x
>(
265 cfg
->ver_maj
, cfg
->ver_min
, cfg
->ver_status
);
270 return NOMALI_E_INVALID
;
275 return NOMALI_E_MEMORY
;
282 return NOMALI_E_INVALID
;
286 extern "C" EXPORT nomali_error_t
287 nomali_destroy(nomali_handle_t h
)
289 NoMaliApi
*gpu(get_gpu(h
));
295 return NOMALI_E_HANDLE
;
299 extern "C" EXPORT
const char *
300 nomali_errstr(nomali_error_t error
)
302 if (error
< NOMALI_E_NUM_ERRORS
)
303 return errstrs
[error
];
305 return "Invalid error number";
308 extern "C" EXPORT nomali_error_t
309 nomali_set_callback(nomali_handle_t h
,
310 const nomali_callback_t
*callback
)
312 NoMaliApi
*gpu(get_gpu(h
));
313 return gpu
? gpu
->setCallback(callback
) : NOMALI_E_HANDLE
;
316 extern "C" EXPORT nomali_error_t
317 nomali_get_info(nomali_handle_t h
, nomali_info_t
*info
)
319 NoMaliApi
*gpu(get_gpu(h
));
320 return gpu
? gpu
->getInfo(info
) : NOMALI_E_HANDLE
;
323 extern "C" EXPORT nomali_error_t
324 nomali_reset(nomali_handle_t h
)
326 NoMaliApi
*gpu(get_gpu(h
));
327 return gpu
? gpu
->reset() : NOMALI_E_HANDLE
;
330 extern "C" EXPORT nomali_error_t
331 nomali_reg_read(nomali_handle_t h
, uint32_t *value
,
334 NoMaliApi
*gpu(get_gpu(h
));
335 return gpu
? gpu
->regRead(value
, addr
) : NOMALI_E_HANDLE
;
338 extern "C" EXPORT nomali_error_t
339 nomali_reg_write(nomali_handle_t h
,
340 nomali_addr_t addr
, uint32_t value
)
342 NoMaliApi
*gpu(get_gpu(h
));
343 return gpu
? gpu
->regWrite(addr
, value
) : NOMALI_E_HANDLE
;
347 extern "C" EXPORT nomali_error_t
348 nomali_reg_read_raw(nomali_handle_t h
, uint32_t *value
,
351 NoMaliApi
*gpu(get_gpu(h
));
352 return gpu
? gpu
->regReadRaw(value
, addr
) : NOMALI_E_HANDLE
;
355 extern "C" EXPORT nomali_error_t
356 nomali_reg_write_raw(nomali_handle_t h
,
357 nomali_addr_t addr
, uint32_t value
)
359 NoMaliApi
*gpu(get_gpu(h
));
360 return gpu
? gpu
->regWriteRaw(addr
, value
) : NOMALI_E_HANDLE
;
363 extern "C" EXPORT nomali_error_t
364 nomali_int_state(nomali_handle_t h
, int *state
,
367 NoMaliApi
*gpu(get_gpu(h
));
368 return gpu
? gpu
->intState(state
, intno
) : NOMALI_E_HANDLE
;