1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * AMD Address Translation Library 4 * 5 * system.c : Functions to read and save system-wide data 6 * 7 * Copyright (c) 2023, Advanced Micro Devices, Inc. 8 * All Rights Reserved. 9 * 10 * Author: Yazen Ghannam <Yazen.Ghannam@amd.com> 11 */ 12 13 #include "internal.h" 14 15 int determine_node_id(struct addr_ctx *ctx, u8 socket_id, u8 die_id) 16 { 17 u16 socket_id_bits, die_id_bits; 18 19 if (socket_id > 0 && df_cfg.socket_id_mask == 0) { 20 atl_debug(ctx, "Invalid socket inputs: socket_id=%u socket_id_mask=0x%x", 21 socket_id, df_cfg.socket_id_mask); 22 return -EINVAL; 23 } 24 25 /* Do each step independently to avoid shift out-of-bounds issues. */ 26 socket_id_bits = socket_id; 27 socket_id_bits <<= df_cfg.socket_id_shift; 28 socket_id_bits &= df_cfg.socket_id_mask; 29 30 if (die_id > 0 && df_cfg.die_id_mask == 0) { 31 atl_debug(ctx, "Invalid die inputs: die_id=%u die_id_mask=0x%x", 32 die_id, df_cfg.die_id_mask); 33 return -EINVAL; 34 } 35 36 /* Do each step independently to avoid shift out-of-bounds issues. */ 37 die_id_bits = die_id; 38 die_id_bits <<= df_cfg.die_id_shift; 39 die_id_bits &= df_cfg.die_id_mask; 40 41 ctx->node_id = (socket_id_bits | die_id_bits) >> df_cfg.node_id_shift; 42 return 0; 43 } 44 45 static void df2_get_masks_shifts(u32 mask0) 46 { 47 df_cfg.socket_id_shift = FIELD_GET(DF2_SOCKET_ID_SHIFT, mask0); 48 df_cfg.socket_id_mask = FIELD_GET(DF2_SOCKET_ID_MASK, mask0); 49 df_cfg.die_id_shift = FIELD_GET(DF2_DIE_ID_SHIFT, mask0); 50 df_cfg.die_id_mask = FIELD_GET(DF2_DIE_ID_MASK, mask0); 51 df_cfg.node_id_shift = df_cfg.die_id_shift; 52 df_cfg.node_id_mask = df_cfg.socket_id_mask | df_cfg.die_id_mask; 53 df_cfg.component_id_mask = ~df_cfg.node_id_mask; 54 } 55 56 static void df3_get_masks_shifts(u32 mask0, u32 mask1) 57 { 58 df_cfg.component_id_mask = FIELD_GET(DF3_COMPONENT_ID_MASK, mask0); 59 df_cfg.node_id_mask = FIELD_GET(DF3_NODE_ID_MASK, mask0); 60 61 df_cfg.node_id_shift = FIELD_GET(DF3_NODE_ID_SHIFT, mask1); 62 df_cfg.socket_id_shift = FIELD_GET(DF3_SOCKET_ID_SHIFT, mask1); 63 df_cfg.socket_id_mask = FIELD_GET(DF3_SOCKET_ID_MASK, mask1); 64 df_cfg.die_id_mask = FIELD_GET(DF3_DIE_ID_MASK, mask1); 65 } 66 67 static void df3p5_get_masks_shifts(u32 mask0, u32 mask1, u32 mask2) 68 { 69 df_cfg.component_id_mask = FIELD_GET(DF4_COMPONENT_ID_MASK, mask0); 70 df_cfg.node_id_mask = FIELD_GET(DF4_NODE_ID_MASK, mask0); 71 72 df_cfg.node_id_shift = FIELD_GET(DF3_NODE_ID_SHIFT, mask1); 73 df_cfg.socket_id_shift = FIELD_GET(DF4_SOCKET_ID_SHIFT, mask1); 74 75 df_cfg.socket_id_mask = FIELD_GET(DF4_SOCKET_ID_MASK, mask2); 76 df_cfg.die_id_mask = FIELD_GET(DF4_DIE_ID_MASK, mask2); 77 } 78 79 static void df4_get_masks_shifts(u32 mask0, u32 mask1, u32 mask2) 80 { 81 df3p5_get_masks_shifts(mask0, mask1, mask2); 82 83 if (!(df_cfg.flags.socket_id_shift_quirk && df_cfg.socket_id_shift == 1)) 84 return; 85 86 df_cfg.socket_id_shift = 0; 87 df_cfg.socket_id_mask = 1; 88 df_cfg.die_id_shift = 0; 89 df_cfg.die_id_mask = 0; 90 df_cfg.node_id_shift = 8; 91 df_cfg.node_id_mask = 0x100; 92 } 93 94 static int df4_get_fabric_id_mask_registers(void) 95 { 96 u32 mask0, mask1, mask2; 97 98 /* Read D18F4x1B0 (SystemFabricIdMask0) */ 99 if (df_indirect_read_broadcast(0, 4, 0x1B0, &mask0)) 100 return -EINVAL; 101 102 /* Read D18F4x1B4 (SystemFabricIdMask1) */ 103 if (df_indirect_read_broadcast(0, 4, 0x1B4, &mask1)) 104 return -EINVAL; 105 106 /* Read D18F4x1B8 (SystemFabricIdMask2) */ 107 if (df_indirect_read_broadcast(0, 4, 0x1B8, &mask2)) 108 return -EINVAL; 109 110 df4_get_masks_shifts(mask0, mask1, mask2); 111 return 0; 112 } 113 114 static int df4_determine_df_rev(u32 reg) 115 { 116 df_cfg.rev = FIELD_GET(DF_MINOR_REVISION, reg) < 5 ? DF4 : DF4p5; 117 118 /* Check for special cases or quirks based on Device/Vendor IDs.*/ 119 120 /* Read D18F0x000 (DeviceVendorId0) */ 121 if (df_indirect_read_broadcast(0, 0, 0, ®)) 122 return -EINVAL; 123 124 if (reg == DF_FUNC0_ID_ZEN4_SERVER) 125 df_cfg.flags.socket_id_shift_quirk = 1; 126 127 return df4_get_fabric_id_mask_registers(); 128 } 129 130 static int determine_df_rev_legacy(void) 131 { 132 u32 fabric_id_mask0, fabric_id_mask1, fabric_id_mask2; 133 134 /* 135 * Check for DF3.5. 136 * 137 * Component ID Mask must be non-zero. Register D18F1x150 is 138 * reserved pre-DF3.5, so value will be Read-as-Zero. 139 */ 140 141 /* Read D18F1x150 (SystemFabricIdMask0). */ 142 if (df_indirect_read_broadcast(0, 1, 0x150, &fabric_id_mask0)) 143 return -EINVAL; 144 145 if (FIELD_GET(DF4_COMPONENT_ID_MASK, fabric_id_mask0)) { 146 df_cfg.rev = DF3p5; 147 148 /* Read D18F1x154 (SystemFabricIdMask1) */ 149 if (df_indirect_read_broadcast(0, 1, 0x154, &fabric_id_mask1)) 150 return -EINVAL; 151 152 /* Read D18F1x158 (SystemFabricIdMask2) */ 153 if (df_indirect_read_broadcast(0, 1, 0x158, &fabric_id_mask2)) 154 return -EINVAL; 155 156 df3p5_get_masks_shifts(fabric_id_mask0, fabric_id_mask1, fabric_id_mask2); 157 return 0; 158 } 159 160 /* 161 * Check for DF3. 162 * 163 * Component ID Mask must be non-zero. Field is Read-as-Zero on DF2. 164 */ 165 166 /* Read D18F1x208 (SystemFabricIdMask). */ 167 if (df_indirect_read_broadcast(0, 1, 0x208, &fabric_id_mask0)) 168 return -EINVAL; 169 170 if (FIELD_GET(DF3_COMPONENT_ID_MASK, fabric_id_mask0)) { 171 df_cfg.rev = DF3; 172 173 /* Read D18F1x20C (SystemFabricIdMask1) */ 174 if (df_indirect_read_broadcast(0, 1, 0x20C, &fabric_id_mask1)) 175 return -EINVAL; 176 177 df3_get_masks_shifts(fabric_id_mask0, fabric_id_mask1); 178 return 0; 179 } 180 181 /* Default to DF2. */ 182 df_cfg.rev = DF2; 183 df2_get_masks_shifts(fabric_id_mask0); 184 return 0; 185 } 186 187 static int determine_df_rev(void) 188 { 189 u32 reg; 190 u8 rev; 191 192 if (df_cfg.rev != UNKNOWN) 193 return 0; 194 195 /* Read D18F0x40 (FabricBlockInstanceCount). */ 196 if (df_indirect_read_broadcast(0, 0, 0x40, ®)) 197 return -EINVAL; 198 199 /* 200 * Revision fields added for DF4 and later. 201 * 202 * Major revision of '0' is found pre-DF4. Field is Read-as-Zero. 203 */ 204 rev = FIELD_GET(DF_MAJOR_REVISION, reg); 205 if (!rev) 206 return determine_df_rev_legacy(); 207 208 /* 209 * Fail out for major revisions other than '4'. 210 * 211 * Explicit support should be added for newer systems to avoid issues. 212 */ 213 if (rev == 4) 214 return df4_determine_df_rev(reg); 215 216 return -EINVAL; 217 } 218 219 static void get_num_maps(void) 220 { 221 switch (df_cfg.rev) { 222 case DF2: 223 case DF3: 224 case DF3p5: 225 df_cfg.num_coh_st_maps = 2; 226 break; 227 case DF4: 228 case DF4p5: 229 df_cfg.num_coh_st_maps = 4; 230 break; 231 default: 232 atl_debug_on_bad_df_rev(); 233 } 234 } 235 236 static void apply_node_id_shift(void) 237 { 238 if (df_cfg.rev == DF2) 239 return; 240 241 df_cfg.die_id_shift = df_cfg.node_id_shift; 242 df_cfg.die_id_mask <<= df_cfg.node_id_shift; 243 df_cfg.socket_id_mask <<= df_cfg.node_id_shift; 244 df_cfg.socket_id_shift += df_cfg.node_id_shift; 245 } 246 247 static void dump_df_cfg(void) 248 { 249 pr_debug("rev=0x%x", df_cfg.rev); 250 251 pr_debug("component_id_mask=0x%x", df_cfg.component_id_mask); 252 pr_debug("die_id_mask=0x%x", df_cfg.die_id_mask); 253 pr_debug("node_id_mask=0x%x", df_cfg.node_id_mask); 254 pr_debug("socket_id_mask=0x%x", df_cfg.socket_id_mask); 255 256 pr_debug("die_id_shift=0x%x", df_cfg.die_id_shift); 257 pr_debug("node_id_shift=0x%x", df_cfg.node_id_shift); 258 pr_debug("socket_id_shift=0x%x", df_cfg.socket_id_shift); 259 260 pr_debug("num_coh_st_maps=%u", df_cfg.num_coh_st_maps); 261 262 pr_debug("flags.legacy_ficaa=%u", df_cfg.flags.legacy_ficaa); 263 pr_debug("flags.socket_id_shift_quirk=%u", df_cfg.flags.socket_id_shift_quirk); 264 } 265 266 int get_df_system_info(void) 267 { 268 if (determine_df_rev()) { 269 pr_warn("amd_atl: Failed to determine DF Revision"); 270 df_cfg.rev = UNKNOWN; 271 return -EINVAL; 272 } 273 274 apply_node_id_shift(); 275 276 get_num_maps(); 277 278 dump_df_cfg(); 279 280 return 0; 281 } 282