1 /*
2 * Copyright 2019 Red Hat Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 */
22 #include "priv.h"
23
24 #include <core/firmware.h>
25
26 #include <nvfw/fw.h>
27 #include <nvfw/hs.h>
28
29 static void
nvkm_acr_hsfw_del(struct nvkm_acr_hsfw * hsfw)30 nvkm_acr_hsfw_del(struct nvkm_acr_hsfw *hsfw)
31 {
32 list_del(&hsfw->head);
33 kfree(hsfw->imem);
34 kfree(hsfw->image);
35 kfree(hsfw->sig.prod.data);
36 kfree(hsfw->sig.dbg.data);
37 kfree(hsfw);
38 }
39
40 void
nvkm_acr_hsfw_del_all(struct nvkm_acr * acr)41 nvkm_acr_hsfw_del_all(struct nvkm_acr *acr)
42 {
43 struct nvkm_acr_hsfw *hsfw, *hsft;
44 list_for_each_entry_safe(hsfw, hsft, &acr->hsfw, head) {
45 nvkm_acr_hsfw_del(hsfw);
46 }
47 }
48
49 static int
nvkm_acr_hsfw_load_image(struct nvkm_acr * acr,const char * name,int ver,struct nvkm_acr_hsfw * hsfw)50 nvkm_acr_hsfw_load_image(struct nvkm_acr *acr, const char *name, int ver,
51 struct nvkm_acr_hsfw *hsfw)
52 {
53 struct nvkm_subdev *subdev = &acr->subdev;
54 const struct firmware *fw;
55 const struct nvfw_bin_hdr *hdr;
56 const struct nvfw_hs_header *fwhdr;
57 const struct nvfw_hs_load_header *lhdr;
58 u32 loc, sig;
59 int ret;
60
61 ret = nvkm_firmware_get(subdev, name, ver, &fw);
62 if (ret < 0)
63 return ret;
64
65 hdr = nvfw_bin_hdr(subdev, fw->data);
66 fwhdr = nvfw_hs_header(subdev, fw->data + hdr->header_offset);
67
68 /* Earlier FW releases by NVIDIA for Nouveau's use aren't in NVIDIA's
69 * standard format, and don't have the indirection seen in the 0x10de
70 * case.
71 */
72 switch (hdr->bin_magic) {
73 case 0x000010de:
74 loc = *(u32 *)(fw->data + fwhdr->patch_loc);
75 sig = *(u32 *)(fw->data + fwhdr->patch_sig);
76 break;
77 case 0x3b1d14f0:
78 loc = fwhdr->patch_loc;
79 sig = fwhdr->patch_sig;
80 break;
81 default:
82 ret = -EINVAL;
83 goto done;
84 }
85
86 lhdr = nvfw_hs_load_header(subdev, fw->data + fwhdr->hdr_offset);
87
88 if (!(hsfw->image = kmalloc(hdr->data_size, GFP_KERNEL))) {
89 ret = -ENOMEM;
90 goto done;
91 }
92
93 memcpy(hsfw->image, fw->data + hdr->data_offset, hdr->data_size);
94 hsfw->image_size = hdr->data_size;
95 hsfw->non_sec_addr = lhdr->non_sec_code_off;
96 hsfw->non_sec_size = lhdr->non_sec_code_size;
97 hsfw->sec_addr = lhdr->apps[0];
98 hsfw->sec_size = lhdr->apps[lhdr->num_apps];
99 hsfw->data_addr = lhdr->data_dma_base;
100 hsfw->data_size = lhdr->data_size;
101
102 hsfw->sig.prod.size = fwhdr->sig_prod_size;
103 hsfw->sig.prod.data = kmemdup(fw->data + fwhdr->sig_prod_offset + sig,
104 hsfw->sig.prod.size, GFP_KERNEL);
105 if (!hsfw->sig.prod.data) {
106 ret = -ENOMEM;
107 goto done;
108 }
109
110 hsfw->sig.dbg.size = fwhdr->sig_dbg_size;
111 hsfw->sig.dbg.data = kmemdup(fw->data + fwhdr->sig_dbg_offset + sig,
112 hsfw->sig.dbg.size, GFP_KERNEL);
113 if (!hsfw->sig.dbg.data) {
114 ret = -ENOMEM;
115 goto done;
116 }
117
118 hsfw->sig.patch_loc = loc;
119 done:
120 nvkm_firmware_put(fw);
121 return ret;
122 }
123
124 static int
nvkm_acr_hsfw_load_bl(struct nvkm_acr * acr,const char * name,int ver,struct nvkm_acr_hsfw * hsfw)125 nvkm_acr_hsfw_load_bl(struct nvkm_acr *acr, const char *name, int ver,
126 struct nvkm_acr_hsfw *hsfw)
127 {
128 struct nvkm_subdev *subdev = &acr->subdev;
129 const struct nvfw_bin_hdr *hdr;
130 const struct nvfw_bl_desc *desc;
131 const struct firmware *fw;
132 u8 *data;
133 int ret;
134
135 ret = nvkm_firmware_get(subdev, name, ver, &fw);
136 if (ret)
137 return ret;
138
139 hdr = nvfw_bin_hdr(subdev, fw->data);
140 desc = nvfw_bl_desc(subdev, fw->data + hdr->header_offset);
141 data = (void *)fw->data + hdr->data_offset;
142
143 hsfw->imem_size = desc->code_size;
144 hsfw->imem_tag = desc->start_tag;
145 hsfw->imem = kmalloc(desc->code_size, GFP_KERNEL);
146 memcpy(hsfw->imem, data + desc->code_off, desc->code_size);
147
148 nvkm_firmware_put(fw);
149 return 0;
150 }
151
152 int
nvkm_acr_hsfw_load(struct nvkm_acr * acr,const char * bl,const char * fw,const char * name,int version,const struct nvkm_acr_hsf_fwif * fwif)153 nvkm_acr_hsfw_load(struct nvkm_acr *acr, const char *bl, const char *fw,
154 const char *name, int version,
155 const struct nvkm_acr_hsf_fwif *fwif)
156 {
157 struct nvkm_acr_hsfw *hsfw;
158 int ret;
159
160 if (!(hsfw = kzalloc(sizeof(*hsfw), GFP_KERNEL)))
161 return -ENOMEM;
162
163 hsfw->func = fwif->func;
164 hsfw->name = name;
165 list_add_tail(&hsfw->head, &acr->hsfw);
166
167 ret = nvkm_acr_hsfw_load_bl(acr, bl, version, hsfw);
168 if (ret)
169 goto done;
170
171 ret = nvkm_acr_hsfw_load_image(acr, fw, version, hsfw);
172 done:
173 if (ret)
174 nvkm_acr_hsfw_del(hsfw);
175 return ret;
176 }
177