summaryrefslogtreecommitdiffstats
path: root/drivers/gpu/drm/nouveau/nvkm/subdev/gsp/fwsec.c
blob: 330d72b1a4af1bed66b03d6bb18b898ba2473ed8 (plain)
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*
 * Copyright 2023 Red Hat Inc.
 *
 * 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 COPYRIGHT HOLDER(S) OR AUTHOR(S) 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 "priv.h"

#include <subdev/bios.h>
#include <subdev/bios/pmu.h>

#include <nvfw/fw.h>

union nvfw_falcon_appif_hdr {
	struct nvfw_falcon_appif_hdr_v1 {
		u8 ver;
		u8 hdr;
		u8 len;
		u8 cnt;
	} v1;
};

union nvfw_falcon_appif {
	struct nvfw_falcon_appif_v1 {
#define NVFW_FALCON_APPIF_ID_DMEMMAPPER 0x00000004
		u32 id;
		u32 dmem_base;
	} v1;
};

union nvfw_falcon_appif_dmemmapper {
	struct {
		u32 signature;
		u16 version;
		u16 size;
		u32 cmd_in_buffer_offset;
		u32 cmd_in_buffer_size;
		u32 cmd_out_buffer_offset;
		u32 cmd_out_buffer_size;
		u32 nvf_img_data_buffer_offset;
		u32 nvf_img_data_buffer_size;
		u32 printf_buffer_hdr;
		u32 ucode_build_time_stamp;
		u32 ucode_signature;
#define NVFW_FALCON_APPIF_DMEMMAPPER_CMD_FRTS 0x00000015
#define NVFW_FALCON_APPIF_DMEMMAPPER_CMD_SB   0x00000019
		u32 init_cmd;
		u32 ucode_feature;
		u32 ucode_cmd_mask0;
		u32 ucode_cmd_mask1;
		u32 multi_tgt_tbl;
	} v3;
};

struct nvfw_fwsec_frts_cmd {
	struct {
	    u32 ver;
	    u32 hdr;
	    u64 addr;
	    u32 size;
	    u32 flags;
	} read_vbios;
	struct {
	    u32 ver;
	    u32 hdr;
	    u32 addr;
	    u32 size;
#define NVFW_FRTS_CMD_REGION_TYPE_FB 0x00000002
	    u32 type;
	} frts_region;
};

static int
nvkm_gsp_fwsec_patch(struct nvkm_gsp *gsp, struct nvkm_falcon_fw *fw, u32 if_offset, u32 init_cmd)
{
	union nvfw_falcon_appif_hdr *hdr = (void *)(fw->fw.img + fw->dmem_base_img + if_offset);
	const u8 *dmem = fw->fw.img + fw->dmem_base_img;
	int i;

	if (WARN_ON(hdr->v1.ver != 1))
		return -EINVAL;

	for (i = 0; i < hdr->v1.cnt; i++) {
		union nvfw_falcon_appif *app = (void *)((u8 *)hdr + hdr->v1.hdr + i * hdr->v1.len);
		union nvfw_falcon_appif_dmemmapper *dmemmap;
		struct nvfw_fwsec_frts_cmd *frtscmd;

		if (app->v1.id != NVFW_FALCON_APPIF_ID_DMEMMAPPER)
			continue;

		dmemmap = (void *)(dmem + app->v1.dmem_base);
		dmemmap->v3.init_cmd = init_cmd;

		frtscmd = (void *)(dmem + dmemmap->v3.cmd_in_buffer_offset);

		frtscmd->read_vbios.ver = 1;
		frtscmd->read_vbios.hdr = sizeof(frtscmd->read_vbios);
		frtscmd->read_vbios.addr = 0;
		frtscmd->read_vbios.size = 0;
		frtscmd->read_vbios.flags = 2;

		if (init_cmd == NVFW_FALCON_APPIF_DMEMMAPPER_CMD_FRTS) {
			frtscmd->frts_region.ver = 1;
			frtscmd->frts_region.hdr = sizeof(frtscmd->frts_region);
			frtscmd->frts_region.addr = gsp->fb.wpr2.frts.addr >> 12;
			frtscmd->frts_region.size = gsp->fb.wpr2.frts.size >> 12;
			frtscmd->frts_region.type = NVFW_FRTS_CMD_REGION_TYPE_FB;
		}

		break;
	}

	if (WARN_ON(i == hdr->v1.cnt))
		return -EINVAL;

	return 0;
}

union nvfw_falcon_ucode_desc {
	struct nvkm_falcon_ucode_desc_v2 {
		u32 Hdr;
		u32 StoredSize;
		u32 UncompressedSize;
		u32 VirtualEntry;
		u32 InterfaceOffset;
		u32 IMEMPhysBase;
		u32 IMEMLoadSize;
		u32 IMEMVirtBase;
		u32 IMEMSecBase;
		u32 IMEMSecSize;
		u32 DMEMOffset;
		u32 DMEMPhysBase;
		u32 DMEMLoadSize;
		u32 altIMEMLoadSize;
		u32 altDMEMLoadSize;
	} v2;

	struct nvkm_falcon_ucode_desc_v3 {
		u32 Hdr;
		u32 StoredSize;
		u32 PKCDataOffset;
		u32 InterfaceOffset;
		u32 IMEMPhysBase;
		u32 IMEMLoadSize;
		u32 IMEMVirtBase;
		u32 DMEMPhysBase;
		u32 DMEMLoadSize;
		u16 EngineIdMask;
		u8  UcodeId;
		u8  SignatureCount;
		u16 SignatureVersions;
		u16 Reserved;
	} v3;
};

static int
nvkm_gsp_fwsec_v2(struct nvkm_gsp *gsp, const char *name,
		  const struct nvkm_falcon_ucode_desc_v2 *desc, u32 size, u32 init_cmd,
		  struct nvkm_falcon_fw *fw)
{
	struct nvkm_subdev *subdev = &gsp->subdev;
	const struct firmware *bl;
	const struct nvfw_bin_hdr *hdr;
	const struct nvfw_bl_desc *bld;
	int ret;

	/* Build ucode. */
	ret = nvkm_falcon_fw_ctor(gsp->func->fwsec, name, subdev->device, true,
				  (u8 *)desc + size, desc->IMEMLoadSize + desc->DMEMLoadSize,
				  &gsp->falcon, fw);
	if (WARN_ON(ret))
		return ret;

	fw->nmem_base_img = 0;
	fw->nmem_base = desc->IMEMPhysBase;
	fw->nmem_size = desc->IMEMLoadSize - desc->IMEMSecSize;

	fw->imem_base_img = 0;
	fw->imem_base = desc->IMEMSecBase;
	fw->imem_size = desc->IMEMSecSize;

	fw->dmem_base_img = desc->DMEMOffset;
	fw->dmem_base = desc->DMEMPhysBase;
	fw->dmem_size = desc->DMEMLoadSize;

	/* Bootloader. */
	ret = nvkm_firmware_get(subdev, "acr/bl", 0, &bl);
	if (ret)
		return ret;

	hdr = nvfw_bin_hdr(subdev, bl->data);
	bld = nvfw_bl_desc(subdev, bl->data + hdr->header_offset);

	fw->boot_addr = bld->start_tag << 8;
	fw->boot_size = bld->code_size;
	fw->boot = kmemdup(bl->data + hdr->data_offset + bld->code_off, fw->boot_size, GFP_KERNEL);
	if (!fw->boot)
		ret = -ENOMEM;

	nvkm_firmware_put(bl);

	/* Patch in interface data. */
	return nvkm_gsp_fwsec_patch(gsp, fw, desc->InterfaceOffset, init_cmd);
}

static int
nvkm_gsp_fwsec_v3(struct nvkm_gsp *gsp, const char *name,
		  const struct nvkm_falcon_ucode_desc_v3 *desc, u32 size, u32 init_cmd,
		  struct nvkm_falcon_fw *fw)
{
	struct nvkm_device *device = gsp->subdev.device;
	struct nvkm_bios *bios = device->bios;
	int ret;

	/* Build ucode. */
	ret = nvkm_falcon_fw_ctor(gsp->func->fwsec, name, device, true,
				  (u8 *)desc + size, desc->IMEMLoadSize + desc->DMEMLoadSize,
				  &gsp->falcon, fw);
	if (WARN_ON(ret))
		return ret;

	fw->imem_base_img = 0;
	fw->imem_base = desc->IMEMPhysBase;
	fw->imem_size = desc->IMEMLoadSize;
	fw->dmem_base_img = desc->IMEMLoadSize;
	fw->dmem_base = desc->DMEMPhysBase;
	fw->dmem_size = ALIGN(desc->DMEMLoadSize, 256);
	fw->dmem_sign = desc->PKCDataOffset;
	fw->boot_addr = 0;
	fw->fuse_ver = desc->SignatureVersions;
	fw->ucode_id = desc->UcodeId;
	fw->engine_id = desc->EngineIdMask;

	/* Patch in signature. */
	ret = nvkm_falcon_fw_sign(fw, fw->dmem_base_img + desc->PKCDataOffset, 96 * 4,
				  nvbios_pointer(bios, 0), desc->SignatureCount,
				  (u8 *)desc + 0x2c - (u8 *)nvbios_pointer(bios, 0), 0, 0);
	if (WARN_ON(ret))
		return ret;

	/* Patch in interface data. */
	return nvkm_gsp_fwsec_patch(gsp, fw, desc->InterfaceOffset, init_cmd);
}

static int
nvkm_gsp_fwsec(struct nvkm_gsp *gsp, const char *name, u32 init_cmd)
{
	struct nvkm_subdev *subdev = &gsp->subdev;
	struct nvkm_device *device = subdev->device;
	struct nvkm_bios *bios = device->bios;
	const union nvfw_falcon_ucode_desc *desc;
	struct nvbios_pmuE flcn_ucode;
	u8 idx, ver, hdr;
	u32 data;
	u16 size, vers;
	struct nvkm_falcon_fw fw = {};
	u32 mbox0 = 0;
	int ret;

	/* Lookup in VBIOS. */
	for (idx = 0; (data = nvbios_pmuEp(bios, idx, &ver, &hdr, &flcn_ucode)); idx++) {
		if (flcn_ucode.type == 0x85)
			break;
	}

	if (WARN_ON(!data))
		return -EINVAL;

	/* Deteremine version. */
	desc = nvbios_pointer(bios, flcn_ucode.data);
	if (WARN_ON(!(desc->v2.Hdr & 0x00000001)))
		return -EINVAL;

	size = (desc->v2.Hdr & 0xffff0000) >> 16;
	vers = (desc->v2.Hdr & 0x0000ff00) >> 8;

	switch (vers) {
	case 2: ret = nvkm_gsp_fwsec_v2(gsp, name, &desc->v2, size, init_cmd, &fw); break;
	case 3: ret = nvkm_gsp_fwsec_v3(gsp, name, &desc->v3, size, init_cmd, &fw); break;
	default:
		nvkm_error(subdev, "%s(v%d): version unknown\n", name, vers);
		return -EINVAL;
	}

	if (ret) {
		nvkm_error(subdev, "%s(v%d): %d\n", name, vers, ret);
		return ret;
	}

	/* Boot. */
	ret = nvkm_falcon_fw_boot(&fw, subdev, true, &mbox0, NULL, 0, 0);
	nvkm_falcon_fw_dtor(&fw);
	if (ret)
		return ret;

	return 0;
}

int
nvkm_gsp_fwsec_sb(struct nvkm_gsp *gsp)
{
	struct nvkm_subdev *subdev = &gsp->subdev;
	struct nvkm_device *device = subdev->device;
	int ret;
	u32 err;

	ret = nvkm_gsp_fwsec(gsp, "fwsec-sb", NVFW_FALCON_APPIF_DMEMMAPPER_CMD_SB);
	if (ret)
		return ret;

	/* Verify. */
	err = nvkm_rd32(device, 0x001400 + (0xf * 4)) & 0x0000ffff;
	if (err) {
		nvkm_error(subdev, "fwsec-sb: 0x%04x\n", err);
		return -EIO;
	}

	return 0;
}

int
nvkm_gsp_fwsec_frts(struct nvkm_gsp *gsp)
{
	struct nvkm_subdev *subdev = &gsp->subdev;
	struct nvkm_device *device = subdev->device;
	int ret;
	u32 err, wpr2_lo, wpr2_hi;

	ret = nvkm_gsp_fwsec(gsp, "fwsec-frts", NVFW_FALCON_APPIF_DMEMMAPPER_CMD_FRTS);
	if (ret)
		return ret;

	/* Verify. */
	err = nvkm_rd32(device, 0x001400 + (0xe * 4)) >> 16;
	if (err) {
		nvkm_error(subdev, "fwsec-frts: 0x%04x\n", err);
		return -EIO;
	}

	wpr2_lo = nvkm_rd32(device, 0x1fa824);
	wpr2_hi = nvkm_rd32(device, 0x1fa828);
	nvkm_debug(subdev, "fwsec-frts: WPR2 @ %08x - %08x\n", wpr2_lo, wpr2_hi);
	return 0;
}