# # 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 2020 Oxide Computer Company # PROG= usmn udf include ../Makefile.cmd ROOTCMDDIR = $(ROOTLIB) CPPFLAGS += -I$(SRC)/uts/intel/io/amdzen CFLAGS += $(CCVERBOSE) .KEEP_STATE: all: $(PROG) install: all $(ROOTCMD) clean: include ../Makefile.targ /* * 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 2025 Oxide Computer Company */ /* * Facilitate access to the AMD Zen data fabric */ #include #include #include #include #include #include #include #include #include #include static void udf_readone(int fd, uint8_t inst, uint8_t func, uint16_t reg, zen_udf_flags_t flags) { int ret; zen_udf_io_t zui; bzero(&zui, sizeof (zui)); zui.zui_flags = flags; zui.zui_inst = inst; zui.zui_func = func; zui.zui_reg = reg; ret = ioctl(fd, ZEN_UDF_READ, &zui); if (ret != 0) { err(EXIT_FAILURE, "failed to issue read ioctl"); } if ((flags & ZEN_UDF_F_BCAST) == 0) { (void) printf("ifr %x/%x/%x: 0x%" PRIx64 "\n", inst, func, reg, zui.zui_data); } else { (void) printf("ifr bcast/%x/%x: 0x%" PRIx64 "\n", func, reg, zui.zui_data); } } int main(int argc, char *argv[]) { int c, fd; const char *device = NULL; const char *funcstr = NULL; const char *inststr = NULL; const char *regstr = NULL; zen_udf_flags_t flags = 0; uint8_t func, inst = UINT8_MAX; uint16_t reg; unsigned long lval; char *eptr; while ((c = getopt(argc, argv, "d:f:bi:r:l")) != -1) { switch (c) { case 'd': device = optarg; break; case 'f': funcstr = optarg; break; case 'b': flags |= ZEN_UDF_F_BCAST; break; case 'i': inststr = optarg; break; case 'l': flags |= ZEN_UDF_F_64; break; case 'r': regstr = optarg; break; } } if (device == NULL || funcstr == NULL || regstr == NULL || (inststr == NULL && (flags & ZEN_UDF_F_BCAST) == 0)) { warnx("missing required arguments"); (void) fprintf(stderr, "Usage: " "\tudf \t[-l] -d device -f func -b -r reg\n" "\t\t[-l] -d device -f func -i inst -r reg\n"); exit(2); } errno = 0; lval = strtoul(funcstr, &eptr, 0); if (errno != 0 || lval > UINT8_MAX || *eptr != '\0') { errx(EXIT_FAILURE, "failed to parse -f: %s", funcstr); } func = (uint8_t)lval; if ((flags & ZEN_UDF_F_BCAST) == 0) { lval = strtoul(inststr, &eptr, 0); if (errno != 0 || lval > UINT8_MAX || *eptr != '\0') { errx(EXIT_FAILURE, "failed to parse -i: %s", inststr); } inst = (uint8_t)lval; } else if (inststr != NULL) { errx(EXIT_FAILURE, "pass just one of -b or -i inst"); } lval = strtoul(regstr, &eptr, 0); if (errno != 0 || lval > UINT16_MAX || *eptr != '\0') { errx(EXIT_FAILURE, "failed to parse -r: %s", regstr); } reg = (uint16_t)lval; if ((fd = open(device, O_RDONLY)) < 0) { err(EXIT_FAILURE, "failed to open %s", device); } udf_readone(fd, inst, func, reg, flags); (void) close(fd); return (0); } /* * 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 #include #include #include #include #include #include #include #include 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); }