Robert Mustacchi | 047043c | 2020-04-08 21:35:09 -0700 | [diff] [blame] | 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 | /* |
Keith M Wesolowski | 4adf43b | 2022-11-09 07:00:30 +0000 | [diff] [blame^] | 13 | * Copyright 2022 Oxide Computer Company |
Robert Mustacchi | 047043c | 2020-04-08 21:35:09 -0700 | [diff] [blame] | 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 |
Robert Mustacchi | f198607 | 2021-07-29 22:27:35 -0700 | [diff] [blame] | 31 | usmn_parse_uint32(const char *str, uint32_t *valp) |
Robert Mustacchi | 047043c | 2020-04-08 21:35:09 -0700 | [diff] [blame] | 32 | { |
Robert Mustacchi | f198607 | 2021-07-29 22:27:35 -0700 | [diff] [blame] | 33 | long long l; |
Robert Mustacchi | 047043c | 2020-04-08 21:35:09 -0700 | [diff] [blame] | 34 | char *eptr; |
Robert Mustacchi | 047043c | 2020-04-08 21:35:09 -0700 | [diff] [blame] | 35 | |
| 36 | errno = 0; |
Robert Mustacchi | f198607 | 2021-07-29 22:27:35 -0700 | [diff] [blame] | 37 | l = strtoll(str, &eptr, 16); |
| 38 | if (errno != 0 || *eptr != '\0') { |
| 39 | warnx("failed to parse string '%s'", str); |
Robert Mustacchi | 047043c | 2020-04-08 21:35:09 -0700 | [diff] [blame] | 40 | return (B_FALSE); |
| 41 | } |
| 42 | |
Robert Mustacchi | f198607 | 2021-07-29 22:27:35 -0700 | [diff] [blame] | 43 | if (l < 0 || l > UINT32_MAX) { |
| 44 | warnx("value %s is outside the valid range [0, UINT32_MAX]", |
| 45 | str); |
Robert Mustacchi | 047043c | 2020-04-08 21:35:09 -0700 | [diff] [blame] | 46 | return (B_FALSE); |
| 47 | } |
| 48 | |
Robert Mustacchi | f198607 | 2021-07-29 22:27:35 -0700 | [diff] [blame] | 49 | *valp = (uint32_t)l; |
| 50 | return (B_TRUE); |
| 51 | } |
| 52 | |
| 53 | static boolean_t |
Keith M Wesolowski | 4adf43b | 2022-11-09 07:00:30 +0000 | [diff] [blame^] | 54 | usmn_op(boolean_t do_write, int fd, const char *addr, uint32_t length, |
| 55 | uint32_t value) |
Robert Mustacchi | f198607 | 2021-07-29 22:27:35 -0700 | [diff] [blame] | 56 | { |
| 57 | usmn_reg_t usr; |
| 58 | |
| 59 | usr.usr_data = value; |
Keith M Wesolowski | 4adf43b | 2022-11-09 07:00:30 +0000 | [diff] [blame^] | 60 | usr.usr_size = length; |
Robert Mustacchi | f198607 | 2021-07-29 22:27:35 -0700 | [diff] [blame] | 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 | } |
Robert Mustacchi | 047043c | 2020-04-08 21:35:09 -0700 | [diff] [blame] | 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; |
Robert Mustacchi | f198607 | 2021-07-29 22:27:35 -0700 | [diff] [blame] | 81 | boolean_t do_write = B_FALSE; |
| 82 | uint32_t wval = 0; |
Keith M Wesolowski | 4adf43b | 2022-11-09 07:00:30 +0000 | [diff] [blame^] | 83 | uint32_t length = 4; |
Robert Mustacchi | 047043c | 2020-04-08 21:35:09 -0700 | [diff] [blame] | 84 | |
Keith M Wesolowski | 4adf43b | 2022-11-09 07:00:30 +0000 | [diff] [blame^] | 85 | while ((c = getopt(argc, argv, "d:L:w:")) != -1) { |
Robert Mustacchi | 047043c | 2020-04-08 21:35:09 -0700 | [diff] [blame] | 86 | switch (c) { |
| 87 | case 'd': |
| 88 | device = optarg; |
| 89 | break; |
Keith M Wesolowski | 4adf43b | 2022-11-09 07:00:30 +0000 | [diff] [blame^] | 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; |
Robert Mustacchi | f198607 | 2021-07-29 22:27:35 -0700 | [diff] [blame] | 100 | case 'w': |
| 101 | do_write = B_TRUE; |
| 102 | if (!usmn_parse_uint32(optarg, &wval)) { |
| 103 | return (EXIT_FAILURE); |
| 104 | } |
| 105 | break; |
Robert Mustacchi | 047043c | 2020-04-08 21:35:09 -0700 | [diff] [blame] | 106 | default: |
Robert Mustacchi | f198607 | 2021-07-29 22:27:35 -0700 | [diff] [blame] | 107 | (void) fprintf(stderr, "Usage: usmn -d device " |
Keith M Wesolowski | 4adf43b | 2022-11-09 07:00:30 +0000 | [diff] [blame^] | 108 | "[-L length] [-w value] addr [addr]...\n" |
Robert Mustacchi | 047043c | 2020-04-08 21:35:09 -0700 | [diff] [blame] | 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) { |
Robert Mustacchi | f198607 | 2021-07-29 22:27:35 -0700 | [diff] [blame] | 122 | errx(EXIT_FAILURE, "at least one register must be specified"); |
Robert Mustacchi | 047043c | 2020-04-08 21:35:09 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Robert Mustacchi | f198607 | 2021-07-29 22:27:35 -0700 | [diff] [blame] | 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) { |
Robert Mustacchi | 047043c | 2020-04-08 21:35:09 -0700 | [diff] [blame] | 130 | err(EXIT_FAILURE, "failed to open %s", device); |
| 131 | } |
| 132 | |
| 133 | ret = EXIT_SUCCESS; |
| 134 | for (i = 0; i < argc; i++) { |
Keith M Wesolowski | 4adf43b | 2022-11-09 07:00:30 +0000 | [diff] [blame^] | 135 | if (!usmn_op(do_write, fd, argv[i], length, wval)) { |
Robert Mustacchi | 047043c | 2020-04-08 21:35:09 -0700 | [diff] [blame] | 136 | ret = EXIT_FAILURE; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | (void) close(fd); |
Robert Mustacchi | 047043c | 2020-04-08 21:35:09 -0700 | [diff] [blame] | 141 | return (ret); |
| 142 | } |