xref: /illumos-gate/usr/src/cmd/amdzen/usmn.c (revision 7a6d80f1660abd4755c68cbd094d4a914681d26e)
1 /*
2  * This file and its contents are supplied under the terms of the
3  * Common Development and Distribution License ("CDDL"), version 1.0.
4  * You may only use this file in accordance with the terms of version
5  * 1.0 of the CDDL.
6  *
7  * A full copy of the text of the CDDL should have accompanied this
8  * source.  A copy of the CDDL is also available via the Internet at
9  * http://www.illumos.org/license/CDDL.
10  */
11 
12 /*
13  * Copyright 2022 Oxide Computer Company
14  */
15 
16 /*
17  * Read and write to the AMD SMN.
18  */
19 
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <unistd.h>
23 #include <err.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27 #include <errno.h>
28 #include <usmn.h>
29 
30 static boolean_t
31 usmn_parse_uint32(const char *str, uint32_t *valp)
32 {
33 	long long l;
34 	char *eptr;
35 
36 	errno = 0;
37 	l = strtoll(str, &eptr, 16);
38 	if (errno != 0 || *eptr != '\0') {
39 		warnx("failed to parse string '%s'", str);
40 		return (B_FALSE);
41 	}
42 
43 	if (l < 0 || l > UINT32_MAX) {
44 		warnx("value %s is outside the valid range [0, UINT32_MAX]",
45 		    str);
46 		return (B_FALSE);
47 	}
48 
49 	*valp = (uint32_t)l;
50 	return (B_TRUE);
51 }
52 
53 static boolean_t
54 usmn_op(boolean_t do_write, int fd, const char *addr, uint32_t length,
55     uint32_t value)
56 {
57 	usmn_reg_t usr;
58 
59 	usr.usr_data = value;
60 	usr.usr_size = length;
61 	if (!usmn_parse_uint32(addr, &usr.usr_addr)) {
62 		return (B_FALSE);
63 	}
64 
65 	if (ioctl(fd, do_write ? USMN_WRITE : USMN_READ, &usr) != 0) {
66 		warn("SMN ioctl failed at 0x%x", usr.usr_addr);
67 		return (B_FALSE);
68 	}
69 
70 	if (!do_write) {
71 		(void) printf("0x%x: 0x%x\n", usr.usr_addr, usr.usr_data);
72 	}
73 	return (B_TRUE);
74 }
75 
76 int
77 main(int argc, char *argv[])
78 {
79 	int i, c, fd, ret;
80 	const char *device = NULL;
81 	boolean_t do_write = B_FALSE;
82 	uint32_t wval = 0;
83 	uint32_t length = 4;
84 
85 	while ((c = getopt(argc, argv, "d:L:w:")) != -1) {
86 		switch (c) {
87 		case 'd':
88 			device = optarg;
89 			break;
90 		case 'L':
91 			if (!usmn_parse_uint32(optarg, &length)) {
92 				return (EXIT_FAILURE);
93 			}
94 			if (length != 1 && length != 2 && length != 4) {
95 				warnx("length %u is out of range {1,2,4}",
96 				    length);
97 				return (EXIT_FAILURE);
98 			}
99 			break;
100 		case 'w':
101 			do_write = B_TRUE;
102 			if (!usmn_parse_uint32(optarg, &wval)) {
103 				return (EXIT_FAILURE);
104 			}
105 			break;
106 		default:
107 			(void) fprintf(stderr, "Usage: usmn -d device "
108 			    "[-L length] [-w value] addr [addr]...\n"
109 			    "Note: All addresses are interpreted as hex\n");
110 			return (2);
111 		}
112 	}
113 
114 	if (device == NULL) {
115 		errx(EXIT_FAILURE, "missing required device");
116 	}
117 
118 	argc -= optind;
119 	argv += optind;
120 
121 	if (argc == 0) {
122 		errx(EXIT_FAILURE, "at least one register must be specified");
123 	}
124 
125 	if (do_write && argc != 1) {
126 		errx(EXIT_FAILURE, "can only write to a single register");
127 	}
128 
129 	if ((fd = open(device, do_write ? O_RDWR : O_RDONLY)) < 0) {
130 		err(EXIT_FAILURE, "failed to open %s", device);
131 	}
132 
133 	ret = EXIT_SUCCESS;
134 	for (i = 0; i < argc; i++) {
135 		if (!usmn_op(do_write, fd, argv[i], length, wval)) {
136 			ret = EXIT_FAILURE;
137 		}
138 	}
139 
140 	(void) close(fd);
141 	return (ret);
142 }
143