1 /* $NetBSD: dmub_reg.h,v 1.2 2021/12/18 23:45:07 riastradh Exp $ */ 2 3 /* 4 * Copyright 2019 Advanced Micro Devices, Inc. 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the "Software"), 8 * to deal in the Software without restriction, including without limitation 9 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 * and/or sell copies of the Software, and to permit persons to whom the 11 * Software is furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice shall be included in 14 * all copies or substantial portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 * OTHER DEALINGS IN THE SOFTWARE. 23 * 24 * Authors: AMD 25 * 26 */ 27 28 #ifndef _DMUB_REG_H_ 29 #define _DMUB_REG_H_ 30 31 #include "../inc/dmub_types.h" 32 33 struct dmub_srv; 34 35 /* Register offset and field lookup. */ 36 37 #define BASE(seg) BASE_INNER(seg) 38 39 #define REG_OFFSET(reg_name) (BASE(mm##reg_name##_BASE_IDX) + mm##reg_name) 40 41 #define FD_SHIFT(reg_name, field) reg_name##__##field##__SHIFT 42 43 #define FD_MASK(reg_name, field) reg_name##__##field##_MASK 44 45 #define REG(reg) (REGS)->offset.reg 46 47 #define FD(reg_field) (REGS)->shift.reg_field, (REGS)->mask.reg_field 48 49 #define FN(reg_name, field) FD(reg_name##__##field) 50 51 /* Register reads and writes. */ 52 53 #define REG_READ(reg) ((CTX)->funcs.reg_read((CTX)->user_ctx, REG(reg))) 54 55 #define REG_WRITE(reg, val) \ 56 ((CTX)->funcs.reg_write((CTX)->user_ctx, REG(reg), (val))) 57 58 /* Register field setting. */ 59 60 #define REG_SET_N(reg_name, n, initial_val, ...) \ 61 dmub_reg_set(CTX, REG(reg_name), initial_val, n, __VA_ARGS__) 62 63 #define REG_SET(reg_name, initial_val, field, val) \ 64 REG_SET_N(reg_name, 1, initial_val, \ 65 FN(reg_name, field), val) 66 67 #define REG_SET_2(reg, init_value, f1, v1, f2, v2) \ 68 REG_SET_N(reg, 2, init_value, \ 69 FN(reg, f1), v1, \ 70 FN(reg, f2), v2) 71 72 #define REG_SET_3(reg, init_value, f1, v1, f2, v2, f3, v3) \ 73 REG_SET_N(reg, 3, init_value, \ 74 FN(reg, f1), v1, \ 75 FN(reg, f2), v2, \ 76 FN(reg, f3), v3) 77 78 #define REG_SET_4(reg, init_value, f1, v1, f2, v2, f3, v3, f4, v4) \ 79 REG_SET_N(reg, 4, init_value, \ 80 FN(reg, f1), v1, \ 81 FN(reg, f2), v2, \ 82 FN(reg, f3), v3, \ 83 FN(reg, f4), v4) 84 85 /* Register field updating. */ 86 87 #define REG_UPDATE_N(reg_name, n, ...)\ 88 dmub_reg_update(CTX, REG(reg_name), n, __VA_ARGS__) 89 90 #define REG_UPDATE(reg_name, field, val) \ 91 REG_UPDATE_N(reg_name, 1, \ 92 FN(reg_name, field), val) 93 94 #define REG_UPDATE_2(reg, f1, v1, f2, v2) \ 95 REG_UPDATE_N(reg, 2,\ 96 FN(reg, f1), v1,\ 97 FN(reg, f2), v2) 98 99 #define REG_UPDATE_3(reg, f1, v1, f2, v2, f3, v3) \ 100 REG_UPDATE_N(reg, 3, \ 101 FN(reg, f1), v1, \ 102 FN(reg, f2), v2, \ 103 FN(reg, f3), v3) 104 105 #define REG_UPDATE_4(reg, f1, v1, f2, v2, f3, v3, f4, v4) \ 106 REG_UPDATE_N(reg, 4, \ 107 FN(reg, f1), v1, \ 108 FN(reg, f2), v2, \ 109 FN(reg, f3), v3, \ 110 FN(reg, f4), v4) 111 112 /* Register field getting. */ 113 114 #define REG_GET(reg_name, field, val) \ 115 dmub_reg_get(CTX, REG(reg_name), FN(reg_name, field), val) 116 117 void dmub_reg_set(struct dmub_srv *srv, uint32_t addr, uint32_t reg_val, int n, 118 uint8_t shift1, uint32_t mask1, uint32_t field_value1, ...); 119 120 void dmub_reg_update(struct dmub_srv *srv, uint32_t addr, int n, uint8_t shift1, 121 uint32_t mask1, uint32_t field_value1, ...); 122 123 void dmub_reg_get(struct dmub_srv *srv, uint32_t addr, uint8_t shift, 124 uint32_t mask, uint32_t *field_value); 125 126 #endif /* _DMUB_REG_H_ */ 127