File: fu-amd-gpu-device.c

package info (click to toggle)
fwupd 2.0.19-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 32,340 kB
  • sloc: ansic: 274,440; python: 11,468; xml: 9,432; sh: 1,625; makefile: 167; cpp: 19; asm: 11; javascript: 9
file content (459 lines) | stat: -rw-r--r-- 14,352 bytes parent folder | download
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/*
 * Copyright 2023 Advanced Micro Devices Inc.
 * All rights reserved.
 *
 * This file is provided under a dual MIT/LGPLv2 license.  When using or
 * redistributing this file, you may do so under either license.
 * AMD Chooses the MIT license part of Dual MIT/LGPLv2 license agreement.
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later OR MIT
 */

#include "config.h"

#include <fcntl.h>
#include <glib/gstdio.h>
#include <libdrm/amdgpu.h>
#include <libdrm/amdgpu_drm.h>

#include "fu-amd-gpu-atom-firmware.h"
#include "fu-amd-gpu-device.h"
#include "fu-amd-gpu-psp-firmware.h"

struct _FuAmdGpuDevice {
	FuOpromDevice parent_instance;
	gchar *vbios_pn;
	guint32 drm_major;
	guint32 drm_minor;
};

#define PSPVBFLASH_MAX_POLL    1500
#define PSPVBFLASH_NOT_STARTED 0x0
#define PSPVBFLASH_IN_PROGRESS 0x1
#define PSPVBFLASH_SUCCESS     0x80000000

#define PART_NUM_STR_SIZE 10

G_DEFINE_TYPE(FuAmdGpuDevice, fu_amd_gpu_device, FU_TYPE_OPROM_DEVICE)

static void
fu_amd_gpu_device_to_string(FuDevice *device, guint idt, GString *str)
{
	FuAmdGpuDevice *self = FU_AMDGPU_DEVICE(device);

	fwupd_codec_string_append_int(str, idt, "DrmMajor", self->drm_major);
	fwupd_codec_string_append_int(str, idt, "DrmMinor", self->drm_minor);
}

static gboolean
fu_amd_gpu_device_set_device_file(FuAmdGpuDevice *self, const gchar *base, GError **error)
{
	FuDeviceEvent *event = NULL;
	const gchar *f;
	g_autofree gchar *ddir = NULL;
	g_autofree gchar *device_file = NULL;
	g_autofree gchar *event_id = NULL;
	g_autoptr(GDir) dir = NULL;

	/* emulated */
	if (fu_device_has_flag(FU_DEVICE(self), FWUPD_DEVICE_FLAG_EMULATED) ||
	    fu_context_has_flag(fu_device_get_context(FU_DEVICE(self)),
				FU_CONTEXT_FLAG_SAVE_EVENTS)) {
		event_id = g_strdup_printf("DrmAmdgpuSetDeviceFile:Base=%s", base);
	}

	/* emulated */
	if (fu_device_has_flag(FU_DEVICE(self), FWUPD_DEVICE_FLAG_EMULATED)) {
		event = fu_device_load_event(FU_DEVICE(self), event_id, error);
		if (event == NULL)
			return FALSE;
		f = fu_device_event_get_str(event, "Filename", error);
		if (f == NULL)
			return FALSE;
		fu_udev_device_set_device_file(FU_UDEV_DEVICE(self), f);
		return TRUE;
	}

	/* save */
	if (fu_context_has_flag(fu_device_get_context(FU_DEVICE(self)),
				FU_CONTEXT_FLAG_SAVE_EVENTS)) {
		event = fu_device_save_event(FU_DEVICE(self), event_id);
	}

	/* find card path */
	ddir = g_build_filename(base, "drm", NULL);
	dir = g_dir_open(ddir, 0, error);
	if (dir == NULL)
		return FALSE;
	while ((f = g_dir_read_name(dir))) {
		if (g_str_has_prefix(f, "card")) {
			device_file = fu_path_build(FU_PATH_KIND_DEVFS, "dri", f, NULL);
			break;
		}
	}

	/* nothing found */
	if (device_file == NULL) {
		g_set_error_literal(error,
				    FWUPD_ERROR,
				    FWUPD_ERROR_NOT_SUPPORTED,
				    "no DRM device file found");
		return FALSE;
	}

	if (event != NULL)
		fu_device_event_set_str(event, "Filename", device_file);

	/* success */
	fu_udev_device_set_device_file(FU_UDEV_DEVICE(self), device_file);
	return TRUE;
}

static gboolean
fu_amd_gpu_device_probe(FuDevice *device, GError **error)
{
	FuAmdGpuDevice *self = FU_AMDGPU_DEVICE(device);
	const gchar *base;
	gboolean exists_rom = FALSE;
	gboolean exists_vbflash = FALSE;
	gboolean exists_vbflash_status = FALSE;
	g_autofree gchar *rom = NULL;
	g_autofree gchar *psp_vbflash = NULL;
	g_autofree gchar *psp_vbflash_status = NULL;

	base = fu_udev_device_get_sysfs_path(FU_UDEV_DEVICE(device));
	if (!fu_amd_gpu_device_set_device_file(self, base, error))
		return FALSE;

	/* APUs don't have 'rom' sysfs file */
	rom = g_build_filename(base, "rom", NULL);
	if (!fu_device_query_file_exists(FU_DEVICE(device), rom, &exists_rom, error))
		return FALSE;
	if (!exists_rom) {
		fu_device_add_private_flag(device, FU_DEVICE_PRIVATE_FLAG_HOST_CPU_CHILD);
		fu_udev_device_add_open_flag(FU_UDEV_DEVICE(device), FU_IO_CHANNEL_OPEN_FLAG_READ);
		fu_device_add_flag(device, FWUPD_DEVICE_FLAG_INTERNAL);
	} else {
		fu_device_set_logical_id(device, "rom");
		fu_device_add_flag(device, FWUPD_DEVICE_FLAG_CAN_VERIFY_IMAGE);
		fu_udev_device_add_open_flag(FU_UDEV_DEVICE(device), FU_IO_CHANNEL_OPEN_FLAG_READ);
	}

	/* firmware upgrade support */
	psp_vbflash = g_build_filename(base, "psp_vbflash", NULL);
	if (!fu_device_query_file_exists(device, psp_vbflash, &exists_vbflash, error))
		return FALSE;
	psp_vbflash_status = g_build_filename(base, "psp_vbflash_status", NULL);
	if (!fu_device_query_file_exists(device, psp_vbflash_status, &exists_vbflash_status, error))
		return FALSE;
	if (exists_vbflash && exists_vbflash_status) {
		fu_device_add_flag(device, FWUPD_DEVICE_FLAG_UPDATABLE);
		fu_device_add_flag(device, FWUPD_DEVICE_FLAG_DUAL_IMAGE);
		fu_device_add_flag(device, FWUPD_DEVICE_FLAG_SIGNED_PAYLOAD);
		fu_device_add_flag(device, FWUPD_DEVICE_FLAG_USABLE_DURING_UPDATE);
		fu_device_add_flag(device, FWUPD_DEVICE_FLAG_SELF_RECOVERY);
		fu_device_add_flag(device, FWUPD_DEVICE_FLAG_NEEDS_REBOOT);
		fu_device_set_install_duration(device, 70);
		fu_device_add_protocol(device, "com.amd.pspvbflash");
	}

	return TRUE;
}

static void
fu_amd_gpu_device_set_marketing_name(FuAmdGpuDevice *self)
{
	FuIOChannel *io_channel = fu_udev_device_get_io_channel(FU_UDEV_DEVICE(self));
	amdgpu_device_handle device_handle = {0};
	gint r;

	/* ignore */
	if (fu_device_has_flag(FU_DEVICE(self), FWUPD_DEVICE_FLAG_EMULATED))
		return;

	r = amdgpu_device_initialize(fu_io_channel_unix_get_fd(io_channel),
				     &self->drm_major,
				     &self->drm_minor,
				     &device_handle);
	if (r == 0) {
		const gchar *marketing_name = amdgpu_get_marketing_name(device_handle);
		if (marketing_name != NULL)
			fu_device_set_name(FU_DEVICE(self), marketing_name);
		amdgpu_device_deinitialize(device_handle);
	} else
		g_warning("unable to set marketing name: %s", fwupd_strerror(r));
}

static gboolean
fu_amd_gpu_device_ioctl_buffer_cb(FuIoctl *self,
				  gpointer ptr,
				  guint8 *buf,
				  gsize bufsz,
				  GError **error)
{
	struct drm_amdgpu_info *request = (struct drm_amdgpu_info *)ptr;
	request->return_pointer = GPOINTER_TO_SIZE(buf);
	request->return_size = bufsz;
	return TRUE;
}

static gboolean
fu_amd_gpu_device_ioctl_drm_info(FuAmdGpuDevice *self, guint8 *buf, gsize bufsz, GError **error)
{
	g_autoptr(FuIoctl) ioctl = fu_udev_device_ioctl_new(FU_UDEV_DEVICE(self));
	struct drm_amdgpu_info request = {
	    .query = AMDGPU_INFO_VBIOS,
	    .vbios_info.type = AMDGPU_INFO_VBIOS_INFO,
	};

	/* include these when generating the emulation event */
	fu_ioctl_add_key_as_u16(ioctl, "Request", DRM_IOCTL_AMDGPU_INFO);
	fu_ioctl_add_key_as_u8(ioctl, "Query", request.query);
	fu_ioctl_add_mutable_buffer(ioctl, NULL, buf, bufsz, fu_amd_gpu_device_ioctl_buffer_cb);
	if (!fu_ioctl_execute(ioctl,
			      DRM_IOCTL_AMDGPU_INFO,
			      &request,
			      sizeof(request),
			      NULL,
			      1000, /* ms */
			      FU_IOCTL_FLAG_NONE,
			      error)) {
		g_prefix_error_literal(error, "failed to DRM_IOCTL_AMDGPU_INFO: ");
		return FALSE;
	}

	/* success */
	return TRUE;
}

static gboolean
fu_amd_gpu_device_parse_version_string(FuAmdGpuDevice *self, const gchar *str, GError **error)
{
	guint64 ver;
	g_autoptr(GError) error_parse = NULL;

	if (!fu_strtoull(str, &ver, 0, G_MAXUINT64, FU_INTEGER_BASE_AUTO, &error_parse)) {
		if (fu_device_has_flag(FU_DEVICE(self), FWUPD_DEVICE_FLAG_UPDATABLE)) {
			g_propagate_error(error, g_steal_pointer(&error_parse));
			return FALSE;
		}
		g_info("unable to parse version from '%s': %s", str, error_parse->message);
		fu_device_set_version_format(FU_DEVICE(self), FWUPD_VERSION_FORMAT_PLAIN);
		fu_device_set_version(FU_DEVICE(self), str); /* nocheck:set-version */
	} else {
		fu_device_set_version_raw(FU_DEVICE(self), ver);
	}

	return TRUE;
}

static gboolean
fu_amd_gpu_device_setup(FuDevice *device, GError **error)
{
	FuAmdGpuDevice *self = FU_AMDGPU_DEVICE(device);
	struct drm_amdgpu_info_vbios vbios_info = {0};
	g_autofree gchar *part = NULL;
	g_autofree gchar *model = NULL;
	g_auto(GStrv) tokens = NULL;

	fu_amd_gpu_device_set_marketing_name(self);

	if (!fu_amd_gpu_device_ioctl_drm_info(self,
					      (guint8 *)&vbios_info,
					      sizeof(vbios_info),
					      error))
		return FALSE;
	self->vbios_pn = fu_strsafe((const gchar *)vbios_info.vbios_pn, PART_NUM_STR_SIZE);
	part = g_strdup_printf("AMD\\%s", self->vbios_pn);
	fu_device_add_instance_id(device, part);

	tokens =
	    fu_strsplit((const gchar *)vbios_info.vbios_pn, sizeof(vbios_info.vbios_pn), "-", -1);
	if (g_strv_length(tokens) >= 3) {
		if (!fu_amd_gpu_device_parse_version_string(self, tokens[2], error))
			return FALSE;
	}

	model = fu_strsafe((const gchar *)vbios_info.name, sizeof(vbios_info.name));
	fu_device_set_summary(device, model);

	return TRUE;
}

static gchar *
fu_amd_gpu_device_convert_version(FuDevice *device, guint64 version_raw)
{
	return fu_version_from_uint32(version_raw, fu_device_get_version_format(device));
}

static FuFirmware *
fu_amd_gpu_device_prepare_firmware(FuDevice *device,
				   GInputStream *stream,
				   FuProgress *progress,
				   FuFirmwareParseFlags flags,
				   GError **error)
{
	FuAmdGpuDevice *self = FU_AMDGPU_DEVICE(device);
	g_autoptr(FuFirmware) firmware = fu_amd_gpu_psp_firmware_new();
	g_autoptr(FuFirmware) ish_a = NULL;
	g_autoptr(FuFirmware) partition_a = NULL;
	g_autoptr(FuFirmware) csm = NULL;
	g_autofree gchar *fw_pn = NULL;

	if (!fu_firmware_parse_stream(firmware, stream, 0x0, flags, error))
		return NULL;

	/* we will always flash the contents of partition A */
	ish_a = fu_firmware_get_image_by_id(firmware, "ISH_A", error);
	if (ish_a == NULL)
		return NULL;
	partition_a = fu_firmware_get_image_by_id(ish_a, "PARTITION_A", error);
	if (partition_a == NULL)
		return NULL;
	csm = fu_firmware_get_image_by_id(partition_a, "ATOM_CSM_A", error);
	if (csm == NULL)
		return NULL;

	fw_pn = fu_strsafe(fu_amd_gpu_atom_firmware_get_vbios_pn(FU_AMD_GPU_ATOM_FIRMWARE(csm)),
			   PART_NUM_STR_SIZE);
	if (g_strcmp0(fw_pn, self->vbios_pn) != 0) {
		if ((flags & FWUPD_INSTALL_FLAG_FORCE) == 0) {
			g_set_error(error,
				    FWUPD_ERROR,
				    FWUPD_ERROR_NOT_SUPPORTED,
				    "firmware for %s does not match %s",
				    fw_pn,
				    self->vbios_pn);
			return NULL;
		}
		g_warning("firmware for %s does not match %s but is being force installed anyway",
			  fw_pn,
			  self->vbios_pn);
	}

	return g_steal_pointer(&firmware);
}

static gboolean
fu_amd_gpu_device_wait_for_completion_cb(FuDevice *device, gpointer user_data, GError **error)
{
	const gchar *base = fu_udev_device_get_sysfs_path(FU_UDEV_DEVICE(device));
	gsize sz = 0;
	guint64 status = 0;
	g_autofree gchar *buf = NULL;
	g_autofree gchar *psp_vbflash_status = NULL;

	psp_vbflash_status = g_build_filename(base, "psp_vbflash_status", NULL);
	if (!g_file_get_contents(psp_vbflash_status, &buf, &sz, error))
		return FALSE;
	if (!fu_strtoull(buf, &status, 0, G_MAXUINT64, FU_INTEGER_BASE_AUTO, error))
		return FALSE;
	if (status != PSPVBFLASH_SUCCESS) {
		g_set_error(error,
			    FWUPD_ERROR,
			    FWUPD_ERROR_INTERNAL,
			    "status was %" G_GUINT64_FORMAT,
			    status);
		return FALSE;
	}

	/* success */
	return TRUE;
}

static gboolean
fu_amd_gpu_device_write_firmware(FuDevice *device,
				 FuFirmware *firmware,
				 FuProgress *progress,
				 FwupdInstallFlags flags,
				 GError **error)
{
	g_autofree gchar *psp_vbflash = NULL;
	g_autoptr(FuIOChannel) image_io = NULL;
	g_autoptr(GBytes) fw = NULL;
	g_autoptr(GError) error_read = NULL;
	const gchar *base;

	/* emulation doesn't currently cover IO channel use */
	if (fu_device_has_flag(device, FWUPD_DEVICE_FLAG_EMULATED))
		return TRUE;

	base = fu_udev_device_get_sysfs_path(FU_UDEV_DEVICE(device));
	psp_vbflash = g_build_filename(base, "psp_vbflash", NULL);

	image_io =
	    fu_io_channel_new_file(psp_vbflash,
				   FU_IO_CHANNEL_OPEN_FLAG_READ | FU_IO_CHANNEL_OPEN_FLAG_WRITE,
				   error);
	if (image_io == NULL)
		return FALSE;

	fu_progress_set_id(progress, G_STRLOC);

	/* stage the image */
	fw = fu_firmware_get_bytes(firmware, error);
	if (fw == NULL)
		return FALSE;
	if (!fu_io_channel_write_bytes(image_io, fw, 100, FU_IO_CHANNEL_FLAG_NONE, error))
		return FALSE;

	/* trigger the update (this looks funny but amdgpu returns 0 bytes) */
	if (!fu_io_channel_read_raw(image_io,
				    NULL,
				    1,
				    NULL,
				    100,
				    FU_IO_CHANNEL_FLAG_NONE,
				    &error_read))
		g_debug("triggered update: %s", error_read->message);

	/* poll for completion */
	return fu_device_retry_full(device,
				    fu_amd_gpu_device_wait_for_completion_cb,
				    PSPVBFLASH_MAX_POLL,
				    100, /* ms */
				    NULL,
				    error);
}

static void
fu_amd_gpu_device_set_progress(FuDevice *device, FuProgress *progress)
{
	fu_progress_set_id(progress, G_STRLOC);
	fu_progress_add_step(progress, FWUPD_STATUS_DECOMPRESSING, 0, "prepare-fw");
	fu_progress_add_step(progress, FWUPD_STATUS_DEVICE_RESTART, 0, NULL); /* detach */
	fu_progress_add_step(progress, FWUPD_STATUS_DEVICE_WRITE, 100, NULL); /* write */
	fu_progress_add_step(progress, FWUPD_STATUS_DEVICE_RESTART, 0, NULL); /* attach */
	fu_progress_add_step(progress, FWUPD_STATUS_DEVICE_BUSY, 0, NULL);    /* reload */
}

static void
fu_amd_gpu_device_init(FuAmdGpuDevice *self)
{
	fu_device_add_private_flag(FU_DEVICE(self), FU_DEVICE_PRIVATE_FLAG_AUTO_PARENT_CHILDREN);
	fu_device_add_private_flag(FU_DEVICE(self), FU_DEVICE_PRIVATE_FLAG_NO_GENERIC_GUIDS);
	fu_device_set_version_format(FU_DEVICE(self), FWUPD_VERSION_FORMAT_NUMBER);
}

static void
fu_amd_gpu_device_finalize(GObject *object)
{
	FuAmdGpuDevice *self = FU_AMDGPU_DEVICE(object);
	g_free(self->vbios_pn);
	G_OBJECT_CLASS(fu_amd_gpu_device_parent_class)->finalize(object);
}

static void
fu_amd_gpu_device_class_init(FuAmdGpuDeviceClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS(klass);
	FuDeviceClass *device_class = FU_DEVICE_CLASS(klass);
	object_class->finalize = fu_amd_gpu_device_finalize;
	device_class->probe = fu_amd_gpu_device_probe;
	device_class->setup = fu_amd_gpu_device_setup;
	device_class->set_progress = fu_amd_gpu_device_set_progress;
	device_class->write_firmware = fu_amd_gpu_device_write_firmware;
	device_class->prepare_firmware = fu_amd_gpu_device_prepare_firmware;
	device_class->to_string = fu_amd_gpu_device_to_string;
	device_class->convert_version = fu_amd_gpu_device_convert_version;
}