summaryrefslogtreecommitdiff
path: root/usr/src/cmd/amdzen/usmn.c
blob: ee49fe301d0b21f04046b7c4854e874b3a9d5be0 (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
/*
 * 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 2022 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_parse_uint32(const char *str, uint32_t *valp)
{
	long long l;
	char *eptr;

	errno = 0;
	l = strtoll(str, &eptr, 16);
	if (errno != 0 || *eptr != '\0') {
		warnx("failed to parse string '%s'", str);
		return (B_FALSE);
	}

	if (l < 0 || l > UINT32_MAX) {
		warnx("value %s is outside the valid range [0, UINT32_MAX]",
		    str);
		return (B_FALSE);
	}

	*valp = (uint32_t)l;
	return (B_TRUE);
}

static boolean_t
usmn_op(boolean_t do_write, int fd, const char *addr, uint32_t length,
    uint32_t value)
{
	usmn_reg_t usr;

	usr.usr_data = value;
	usr.usr_size = length;
	if (!usmn_parse_uint32(addr, &usr.usr_addr)) {
		return (B_FALSE);
	}

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

	if (!do_write) {
		(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;
	boolean_t do_write = B_FALSE;
	uint32_t wval = 0;
	uint32_t length = 4;

	while ((c = getopt(argc, argv, "d:L:w:")) != -1) {
		switch (c) {
		case 'd':
			device = optarg;
			break;
		case 'L':
			if (!usmn_parse_uint32(optarg, &length)) {
				return (EXIT_FAILURE);
			}
			if (length != 1 && length != 2 && length != 4) {
				warnx("length %u is out of range {1,2,4}",
				    length);
				return (EXIT_FAILURE);
			}
			break;
		case 'w':
			do_write = B_TRUE;
			if (!usmn_parse_uint32(optarg, &wval)) {
				return (EXIT_FAILURE);
			}
			break;
		default:
			(void) fprintf(stderr, "Usage: usmn -d device "
			    "[-L length] [-w value] 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, "at least one register must be specified");
	}

	if (do_write && argc != 1) {
		errx(EXIT_FAILURE, "can only write to a single register");
	}

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

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

	(void) close(fd);
	return (ret);
}