xref: /illumos-gate/usr/src/cmd/amdzen/usmn.c (revision 0153d828c132fdb1a17c11b99386a3d1b87994cf)
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 2021 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 value)
55 {
56 	usmn_reg_t usr;
57 
58 	usr.usr_data = value;
59 	if (!usmn_parse_uint32(addr, &usr.usr_addr)) {
60 		return (B_FALSE);
61 	}
62 
63 	if (ioctl(fd, do_write ? USMN_WRITE : USMN_READ, &usr) != 0) {
64 		warn("SMN ioctl failed at 0x%x", usr.usr_addr);
65 		return (B_FALSE);
66 	}
67 
68 	if (!do_write) {
69 		(void) printf("0x%x: 0x%x\n", usr.usr_addr, usr.usr_data);
70 	}
71 	return (B_TRUE);
72 }
73 
74 int
75 main(int argc, char *argv[])
76 {
77 	int i, c, fd, ret;
78 	const char *device = NULL;
79 	boolean_t do_write = B_FALSE;
80 	uint32_t wval = 0;
81 
82 	while ((c = getopt(argc, argv, "d:w:")) != -1) {
83 		switch (c) {
84 		case 'd':
85 			device = optarg;
86 			break;
87 		case 'w':
88 			do_write = B_TRUE;
89 			if (!usmn_parse_uint32(optarg, &wval)) {
90 				return (EXIT_FAILURE);
91 			}
92 			break;
93 		default:
94 			(void) fprintf(stderr, "Usage: usmn -d device "
95 			    "[-w value] addr [addr]...\n"
96 			    "Note: All addresses are interpreted as hex\n");
97 			return (2);
98 		}
99 	}
100 
101 	if (device == NULL) {
102 		errx(EXIT_FAILURE, "missing required device");
103 	}
104 
105 	argc -= optind;
106 	argv += optind;
107 
108 	if (argc == 0) {
109 		errx(EXIT_FAILURE, "at least one register must be specified");
110 	}
111 
112 	if (do_write && argc != 1) {
113 		errx(EXIT_FAILURE, "can only write to a single register");
114 	}
115 
116 	if ((fd = open(device, do_write ? O_RDWR : O_RDONLY)) < 0) {
117 		err(EXIT_FAILURE, "failed to open %s", device);
118 	}
119 
120 	ret = EXIT_SUCCESS;
121 	for (i = 0; i < argc; i++) {
122 		if (!usmn_op(do_write, fd, argv[i], wval)) {
123 			ret = EXIT_FAILURE;
124 		}
125 	}
126 
127 	(void) close(fd);
128 	return (ret);
129 }
130