Files
linux-nvgpu/drivers/gpu/nvgpu/os/posix/firmware.c
srajum b2345cd01a gpu: nvgpu: fixing unit tests for ga10b
- Add support for unit tests to run on orin platform.

JIRA NVGPU-9909 

Change-Id: If4ca69b77d0d8483c0e9f6a6a5a64c3c3e050d65
Signed-off-by: srajum <srajum@nvidia.com>
Reviewed-on: https://git-master.nvidia.com/r/c/linux-nvgpu/+/2737876
Reviewed-by: Dinesh T <dt@nvidia.com>
Reviewed-by: svc-mobile-coverity <svc-mobile-coverity@nvidia.com>
Reviewed-by: svc-mobile-misra <svc-mobile-misra@nvidia.com>
Reviewed-by: Ankur Kishore <ankkishore@nvidia.com>
GVS: Gerrit_Virtual_Submit <buildbot_gerritrpt@nvidia.com>
2023-04-28 02:08:09 -07:00

179 lines
4.0 KiB
C

/*
* Copyright (c) 2018-2023, NVIDIA CORPORATION. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <nvgpu/types.h>
#include <nvgpu/kmem.h>
#include <nvgpu/firmware.h>
#include <nvgpu/gk20a.h>
#define FW_MAX_PATH_SIZE 2048U
#if defined(__QNX__)
#define NVGPU_UNITTEST_UCODE_PATH_GV11B "/gv11b/"
#define NVGPU_UNITTEST_UCODE_PATH_GA10B "/ga10b/"
#else
#define NVGPU_UNITTEST_UCODE_PATH_GV11B "/firmware/gv11b/"
#define NVGPU_UNITTEST_UCODE_PATH_GA10B "/firmware/ga10b/"
#endif
static int nvgpu_ucode_load(struct gk20a *g, const char *path,
struct nvgpu_firmware *ucode)
{
struct stat buf;
size_t bufsize;
ssize_t len;
int fd = -1;
int ret = -1;
u8 *data, *data1;
fd = open(path, O_RDONLY);
if (fd < 0) {
nvgpu_err(g, "fw: %s open failed", path);
goto done;
}
if (fstat(fd, &buf) == -1) {
nvgpu_err(g, "fw: fstat failed");
goto done;
}
if (buf.st_size <= 0) {
nvgpu_err(g, "fw: invalid buf.st_size");
goto done;
}
bufsize = (size_t)buf.st_size;
data = (u8 *)nvgpu_kmalloc(g, bufsize);
if (data == NULL) {
nvgpu_err(g, "fw: malloc failed");
goto done;
}
data1 = data;
while (bufsize > 0UL) {
len = read(fd, data, bufsize);
if (len < 0) {
ret = errno;
nvgpu_err(g, "fw: read failed");
goto err;
}
if (len == 0) {
nvgpu_err(g, "fw load failed: read size mismatch");
ret = -1;
goto err;
}
data += len;
bufsize -= (size_t)len;
}
ucode->data = data1;
ucode->size = (size_t)buf.st_size;
ret = 0;
goto done;
err:
nvgpu_kfree(g, data);
done:
if (fd >= 0) {
if (close(fd) != 0) {
nvgpu_err(g, "close() failed");
}
}
return ret;
}
struct nvgpu_firmware *nvgpu_request_firmware(struct gk20a *g,
const char *fw_name,
u32 flags)
{
struct nvgpu_firmware *fw;
char full_path[FW_MAX_PATH_SIZE] = "";
int ret;
size_t full_path_len;
(void)flags;
if (fw_name == NULL) {
return NULL;
}
#if defined(__QNX__)
strcpy(full_path, "/proc/boot/");
#else
getcwd(full_path, FW_MAX_PATH_SIZE);
#endif
full_path_len = strlen(full_path);
full_path_len += strlen(fw_name);
if (strcmp(g->name, "ga10b") == 0) {
full_path_len += strlen(NVGPU_UNITTEST_UCODE_PATH_GA10B);
} else {
full_path_len += strlen(NVGPU_UNITTEST_UCODE_PATH_GV11B);
}
if (full_path_len >= FW_MAX_PATH_SIZE) {
nvgpu_err(g, "Invalid MAX_PATH_SIZE %lu %u", full_path_len,
FW_MAX_PATH_SIZE);
goto err;
}
if (strcmp(g->name, "ga10b") == 0) {
strcat(full_path, NVGPU_UNITTEST_UCODE_PATH_GA10B);
} else {
strcat(full_path, NVGPU_UNITTEST_UCODE_PATH_GV11B);
}
strcat(full_path, fw_name);
fw = nvgpu_kzalloc(g, sizeof(*fw));
if (fw == NULL) {
nvgpu_err(g, "malloc failed");
goto err;
}
ret = nvgpu_ucode_load(g, full_path, fw);
if (ret != 0) {
nvgpu_err(g, "failed to load %s ucode", fw_name);
goto err_fw;
}
return fw;
err_fw:
nvgpu_kfree(g, fw);
err:
return NULL;
}
void nvgpu_release_firmware(struct gk20a *g, struct nvgpu_firmware *fw)
{
nvgpu_kfree(g, fw->data);
nvgpu_kfree(g, fw);
}