summaryrefslogtreecommitdiff
path: root/usr/src/cmd/amdzen/usmn.c
blob: 3b9998f021759146e09fc91895e20cb15288c8b7 (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
/*
 * This file and its contents are supplied under the terms of the
 * Common Development and Distribution License ("CDDL"), version 1.0.
 * You may only use this file in accordance with the terms of version
 * 1.0 of the CDDL.
 *
 * A full copy of the text of the CDDL should have accompanied this
 * source.  A copy of the CDDL is also available via the Internet at
 * http://www.illumos.org/license/CDDL.
 */

/*
 * Copyright 2020 Oxide Computer Company
 */

/*
 * Read and write to the AMD SMN.
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <err.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <usmn.h>

static boolean_t
usmn_read(int fd, const char *addr)
{
	unsigned long long l;
	char *eptr;
	usmn_reg_t usr;

	errno = 0;
	l = strtoull(addr, &eptr, 16);
	if (errno != 0 || *eptr != '\0' || l > UINT32_MAX) {
		warnx("failed to parse %s: invalid string or address", addr);
		return (B_FALSE);
	}

	usr.usr_addr = (uint32_t)l;
	usr.usr_data = 0;

	if (ioctl(fd, USMN_READ, &usr) != 0) {
		warn("failed to read SMN at 0x%x", usr.usr_addr);
		return (B_FALSE);
	}

	(void) printf("0x%x: 0x%x\n", usr.usr_addr, usr.usr_data);
	return (B_TRUE);
}

int
main(int argc, char *argv[])
{
	int i, c, fd, ret;
	const char *device = NULL;

	while ((c = getopt(argc, argv, "d:")) != -1) {
		switch (c) {
		case 'd':
			device = optarg;
			break;
		default:
			(void) fprintf(stderr, "Usage: usmn -d device addr "
			    "[addr]...\n"
			    "Note: All addresses are interpreted as hex\n");
			return (2);
		}
	}

	if (device == NULL) {
		errx(EXIT_FAILURE, "missing required device");
	}

	argc -= optind;
	argv += optind;

	if (argc == 0) {
		errx(EXIT_FAILURE, "missing registers to read");
	}

	if ((fd = open(device, O_RDONLY)) < 0) {
		err(EXIT_FAILURE, "failed to open %s", device);
	}

	ret = EXIT_SUCCESS;
	for (i = 0; i < argc; i++) {
		if (!usmn_read(fd, argv[i])) {
			ret = EXIT_FAILURE;
		}
	}

	(void) close(fd);

	return (ret);
}