diff options
Diffstat (limited to 'drivers/gpu/nvgpu/common/linux/firmware.c')
-rw-r--r-- | drivers/gpu/nvgpu/common/linux/firmware.c | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/drivers/gpu/nvgpu/common/linux/firmware.c b/drivers/gpu/nvgpu/common/linux/firmware.c new file mode 100644 index 00000000..32b1e61f --- /dev/null +++ b/drivers/gpu/nvgpu/common/linux/firmware.c | |||
@@ -0,0 +1,114 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. | ||
3 | * | ||
4 | * This program is free software; you can redistribute it and/or modify it | ||
5 | * under the terms and conditions of the GNU General Public License, | ||
6 | * version 2, as published by the Free Software Foundation. | ||
7 | * | ||
8 | * This program is distributed in the hope it will be useful, but WITHOUT | ||
9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | ||
11 | * more details. | ||
12 | * | ||
13 | * You should have received a copy of the GNU General Public License | ||
14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
15 | */ | ||
16 | |||
17 | #include <linux/firmware.h> | ||
18 | |||
19 | #include <nvgpu/kmem.h> | ||
20 | #include <nvgpu/bug.h> | ||
21 | #include <nvgpu/firmware.h> | ||
22 | |||
23 | #include "gk20a/gk20a.h" | ||
24 | |||
25 | static const struct firmware *do_request_firmware(struct device *dev, | ||
26 | const char *prefix, const char *fw_name, int flags) | ||
27 | { | ||
28 | const struct firmware *fw; | ||
29 | char *fw_path = NULL; | ||
30 | int path_len, err; | ||
31 | |||
32 | if (prefix) { | ||
33 | path_len = strlen(prefix) + strlen(fw_name); | ||
34 | path_len += 2; /* for the path separator and zero terminator*/ | ||
35 | |||
36 | fw_path = nvgpu_kzalloc(get_gk20a(dev), | ||
37 | sizeof(*fw_path) * path_len); | ||
38 | if (!fw_path) | ||
39 | return NULL; | ||
40 | |||
41 | sprintf(fw_path, "%s/%s", prefix, fw_name); | ||
42 | fw_name = fw_path; | ||
43 | } | ||
44 | |||
45 | #if LINUX_VERSION_CODE < KERNEL_VERSION(3,18,0) | ||
46 | err = request_firmware(&fw, fw_name, dev); | ||
47 | #else | ||
48 | if (flags & NVGPU_REQUEST_FIRMWARE_NO_WARN) | ||
49 | err = request_firmware_direct(&fw, fw_name, dev); | ||
50 | else | ||
51 | err = request_firmware(&fw, fw_name, dev); | ||
52 | #endif | ||
53 | |||
54 | nvgpu_kfree(get_gk20a(dev), fw_path); | ||
55 | if (err) | ||
56 | return NULL; | ||
57 | return fw; | ||
58 | } | ||
59 | |||
60 | /* This is a simple wrapper around request_firmware that takes 'fw_name' and | ||
61 | * applies an IP specific relative path prefix to it. The caller is | ||
62 | * responsible for calling nvgpu_release_firmware later. */ | ||
63 | struct nvgpu_firmware *nvgpu_request_firmware(struct gk20a *g, | ||
64 | const char *fw_name, | ||
65 | int flags) | ||
66 | { | ||
67 | struct device *dev = g->dev; | ||
68 | struct nvgpu_firmware *fw; | ||
69 | const struct firmware *linux_fw; | ||
70 | |||
71 | /* current->fs is NULL when calling from SYS_EXIT. | ||
72 | Add a check here to prevent crash in request_firmware */ | ||
73 | if (!current->fs || !fw_name) | ||
74 | return NULL; | ||
75 | |||
76 | fw = nvgpu_kzalloc(g, sizeof(*fw)); | ||
77 | if (!fw) | ||
78 | return NULL; | ||
79 | |||
80 | linux_fw = do_request_firmware(dev, g->name, fw_name, flags); | ||
81 | |||
82 | #ifdef CONFIG_TEGRA_GK20A | ||
83 | /* TO BE REMOVED - Support loading from legacy SOC specific path. */ | ||
84 | if (!linux_fw && !(flags & NVGPU_REQUEST_FIRMWARE_NO_SOC)) { | ||
85 | struct gk20a_platform *platform = gk20a_get_platform(dev); | ||
86 | linux_fw = do_request_firmware(dev, | ||
87 | platform->soc_name, fw_name, flags); | ||
88 | } | ||
89 | #endif | ||
90 | |||
91 | if (!linux_fw) | ||
92 | goto err; | ||
93 | |||
94 | fw->data = nvgpu_kmalloc(g, linux_fw->size); | ||
95 | if (!fw->data) | ||
96 | goto err; | ||
97 | |||
98 | memcpy(fw->data, linux_fw->data, linux_fw->size); | ||
99 | fw->size = linux_fw->size; | ||
100 | |||
101 | release_firmware(linux_fw); | ||
102 | |||
103 | return fw; | ||
104 | |||
105 | err: | ||
106 | nvgpu_kfree(g, fw); | ||
107 | return NULL; | ||
108 | } | ||
109 | |||
110 | void nvgpu_release_firmware(struct gk20a *g, struct nvgpu_firmware *fw) | ||
111 | { | ||
112 | nvgpu_kfree(g, fw->data); | ||
113 | nvgpu_kfree(g, fw); | ||
114 | } | ||