blob: ee49fe301d0b21f04046b7c4854e874b3a9d5be0 [file] [log] [blame]
Robert Mustacchi047043c2020-04-08 21:35:09 -07001/*
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/*
Keith M Wesolowski4adf43b2022-11-09 07:00:30 +000013 * Copyright 2022 Oxide Computer Company
Robert Mustacchi047043c2020-04-08 21:35:09 -070014 */
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
30static boolean_t
Robert Mustacchif1986072021-07-29 22:27:35 -070031usmn_parse_uint32(const char *str, uint32_t *valp)
Robert Mustacchi047043c2020-04-08 21:35:09 -070032{
Robert Mustacchif1986072021-07-29 22:27:35 -070033 long long l;
Robert Mustacchi047043c2020-04-08 21:35:09 -070034 char *eptr;
Robert Mustacchi047043c2020-04-08 21:35:09 -070035
36 errno = 0;
Robert Mustacchif1986072021-07-29 22:27:35 -070037 l = strtoll(str, &eptr, 16);
38 if (errno != 0 || *eptr != '\0') {
39 warnx("failed to parse string '%s'", str);
Robert Mustacchi047043c2020-04-08 21:35:09 -070040 return (B_FALSE);
41 }
42
Robert Mustacchif1986072021-07-29 22:27:35 -070043 if (l < 0 || l > UINT32_MAX) {
44 warnx("value %s is outside the valid range [0, UINT32_MAX]",
45 str);
Robert Mustacchi047043c2020-04-08 21:35:09 -070046 return (B_FALSE);
47 }
48
Robert Mustacchif1986072021-07-29 22:27:35 -070049 *valp = (uint32_t)l;
50 return (B_TRUE);
51}
52
53static boolean_t
Keith M Wesolowski4adf43b2022-11-09 07:00:30 +000054usmn_op(boolean_t do_write, int fd, const char *addr, uint32_t length,
55 uint32_t value)
Robert Mustacchif1986072021-07-29 22:27:35 -070056{
57 usmn_reg_t usr;
58
59 usr.usr_data = value;
Keith M Wesolowski4adf43b2022-11-09 07:00:30 +000060 usr.usr_size = length;
Robert Mustacchif1986072021-07-29 22:27:35 -070061 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 }
Robert Mustacchi047043c2020-04-08 21:35:09 -070073 return (B_TRUE);
74}
75
76int
77main(int argc, char *argv[])
78{
79 int i, c, fd, ret;
80 const char *device = NULL;
Robert Mustacchif1986072021-07-29 22:27:35 -070081 boolean_t do_write = B_FALSE;
82 uint32_t wval = 0;
Keith M Wesolowski4adf43b2022-11-09 07:00:30 +000083 uint32_t length = 4;
Robert Mustacchi047043c2020-04-08 21:35:09 -070084
Keith M Wesolowski4adf43b2022-11-09 07:00:30 +000085 while ((c = getopt(argc, argv, "d:L:w:")) != -1) {
Robert Mustacchi047043c2020-04-08 21:35:09 -070086 switch (c) {
87 case 'd':
88 device = optarg;
89 break;
Keith M Wesolowski4adf43b2022-11-09 07:00:30 +000090 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;
Robert Mustacchif1986072021-07-29 22:27:35 -0700100 case 'w':
101 do_write = B_TRUE;
102 if (!usmn_parse_uint32(optarg, &wval)) {
103 return (EXIT_FAILURE);
104 }
105 break;
Robert Mustacchi047043c2020-04-08 21:35:09 -0700106 default:
Robert Mustacchif1986072021-07-29 22:27:35 -0700107 (void) fprintf(stderr, "Usage: usmn -d device "
Keith M Wesolowski4adf43b2022-11-09 07:00:30 +0000108 "[-L length] [-w value] addr [addr]...\n"
Robert Mustacchi047043c2020-04-08 21:35:09 -0700109 "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) {
Robert Mustacchif1986072021-07-29 22:27:35 -0700122 errx(EXIT_FAILURE, "at least one register must be specified");
Robert Mustacchi047043c2020-04-08 21:35:09 -0700123 }
124
Robert Mustacchif1986072021-07-29 22:27:35 -0700125 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) {
Robert Mustacchi047043c2020-04-08 21:35:09 -0700130 err(EXIT_FAILURE, "failed to open %s", device);
131 }
132
133 ret = EXIT_SUCCESS;
134 for (i = 0; i < argc; i++) {
Keith M Wesolowski4adf43b2022-11-09 07:00:30 +0000135 if (!usmn_op(do_write, fd, argv[i], length, wval)) {
Robert Mustacchi047043c2020-04-08 21:35:09 -0700136 ret = EXIT_FAILURE;
137 }
138 }
139
140 (void) close(fd);
Robert Mustacchi047043c2020-04-08 21:35:09 -0700141 return (ret);
142}