CUDApp
CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit.
Brought by drakestapleton
CUDApp / tests / amd-kfd-launch / reference / topology-rocm-7.2.2.c
| 1 | /* |
| 2 | * Copyright © 2014 Advanced Micro Devices, Inc. |
| 3 | * Copyright 2016-2018 Raptor Engineering, LLC. All Rights Reserved. |
| 4 | * |
| 5 | * Permission is hereby granted, free of charge, to any person |
| 6 | * obtaining a copy of this software and associated documentation |
| 7 | * files (the "Software"), to deal in the Software without |
| 8 | * restriction, including without limitation the rights to use, copy, |
| 9 | * modify, merge, publish, distribute, sublicense, and/or sell copies |
| 10 | * of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice (including |
| 14 | * the next paragraph) shall be included in all copies or substantial |
| 15 | * portions of the Software. |
| 16 | * |
| 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 21 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 22 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 24 | * DEALINGS IN THE SOFTWARE. |
| 25 | */ |
| 26 | |
| 27 | #include <assert.h> |
| 28 | #include <stdio.h> |
| 29 | #include <stdlib.h> |
| 30 | #include <dirent.h> |
| 31 | #include <malloc.h> |
| 32 | #include <string.h> |
| 33 | #include <unistd.h> |
| 34 | #include <ctype.h> |
| 35 | #include <limits.h> |
| 36 | |
| 37 | #include <errno.h> |
| 38 | #include <sys/sysinfo.h> |
| 39 | #include <xf86drm.h> |
| 40 | #include <amdgpu.h> |
| 41 | #include <amdgpu_drm.h> |
| 42 | |
| 43 | #include "libhsakmt.h" |
| 44 | #include "hsakmt/hsakmtmodel.h" |
| 45 | #include "fmm.h" |
| 46 | |
| 47 | /* Number of memory banks added by thunk on top of topology |
| 48 | * This only includes static heaps like LDS, scratch and SVM, |
| 49 | * not for MMIO_REMAP heap. MMIO_REMAP memory bank is reported |
| 50 | * dynamically based on whether mmio aperture was mapped |
| 51 | * successfully on this node. |
| 52 | */ |
| 53 | #define NUM_OF_IGPU_HEAPS 3 |
| 54 | #define NUM_OF_DGPU_HEAPS 3 |
| 55 | /* SYSFS related */ |
| 56 | #define KFD_SYSFS_PATH "/sys/devices/virtual/kfd/kfd/topology" |
| 57 | #define KFD_SYSFS_PATH_GENERATION_ID "%s/generation_id" |
| 58 | #define KFD_SYSFS_PATH_SYSTEM_PROPERTIES "%s/system_properties" |
| 59 | #define KFD_SYSFS_PATH_NODES "%s/nodes" |
| 60 | |
| 61 | static const char *get_topology_dir(void) |
| 62 | { |
| 63 | if (hsakmt_use_model) |
| 64 | return hsakmt_model_topology; |
| 65 | return KFD_SYSFS_PATH; |
| 66 | } |
| 67 | |
| 68 | typedef struct { |
| 69 | HsaNodeProperties node; |
| 70 | HsaMemoryProperties *mem; /* node->NumBanks elements */ |
| 71 | HsaCacheProperties *cache; |
| 72 | HsaIoLinkProperties *link; |
| 73 | } node_props_t; |
| 74 | |
| 75 | static HsaSystemProperties *g_system; |
| 76 | static node_props_t *g_props; |
| 77 | |
| 78 | /* This array caches sysfs based node IDs of CPU nodes + all supported GPU nodes. |
| 79 | * It will be used to map user-node IDs to sysfs-node IDs. |
| 80 | */ |
| 81 | static uint32_t *map_user_to_sysfs_node_id; |
| 82 | static uint32_t map_user_to_sysfs_node_id_size; |
| 83 | static uint32_t num_sysfs_nodes; |
| 84 | |
| 85 | static int processor_vendor = -1; |
| 86 | /* Supported System Vendors */ |
| 87 | enum SUPPORTED_PROCESSOR_VENDORS { |
| 88 | GENUINE_INTEL = 0, |
| 89 | AUTHENTIC_AMD, |
| 90 | IBM_POWER |
| 91 | }; |
| 92 | /* Adding newline to make the search easier */ |
| 93 | static const char *supported_processor_vendor_name[] = { |
| 94 | "GenuineIntel\n", |
| 95 | "AuthenticAMD\n", |
| 96 | "\n" // POWER requires a different search method |
| 97 | }; |
| 98 | |
| 99 | static HSAKMT_STATUS topology_take_snapshot(HsaKFDContext *ctx); |
| 100 | static void topology_drop_snapshot(void); |
| 101 | |
| 102 | static const struct hsa_gfxip_table gfxip_lookup_table[] = { |
| 103 | /* Kaveri Family */ |
| 104 | { 0x1304, 7, 0, 0, "Spectre" }, |
| 105 | { 0x1305, 7, 0, 0, "Spectre" }, |
| 106 | { 0x1306, 7, 0, 0, "Spectre" }, |
| 107 | { 0x1307, 7, 0, 0, "Spectre" }, |
| 108 | { 0x1309, 7, 0, 0, "Spectre" }, |
| 109 | { 0x130A, 7, 0, 0, "Spectre" }, |
| 110 | { 0x130B, 7, 0, 0, "Spectre" }, |
| 111 | { 0x130C, 7, 0, 0, "Spectre" }, |
| 112 | { 0x130D, 7, 0, 0, "Spectre" }, |
| 113 | { 0x130E, 7, 0, 0, "Spectre" }, |
| 114 | { 0x130F, 7, 0, 0, "Spectre" }, |
| 115 | { 0x1310, 7, 0, 0, "Spectre" }, |
| 116 | { 0x1311, 7, 0, 0, "Spectre" }, |
| 117 | { 0x1312, 7, 0, 0, "Spooky" }, |
| 118 | { 0x1313, 7, 0, 0, "Spectre" }, |
| 119 | { 0x1315, 7, 0, 0, "Spectre" }, |
| 120 | { 0x1316, 7, 0, 0, "Spooky" }, |
| 121 | { 0x1317, 7, 0, 0, "Spooky" }, |
| 122 | { 0x1318, 7, 0, 0, "Spectre" }, |
| 123 | { 0x131B, 7, 0, 0, "Spectre" }, |
| 124 | { 0x131C, 7, 0, 0, "Spectre" }, |
| 125 | { 0x131D, 7, 0, 0, "Spectre" }, |
| 126 | /* Hawaii Family */ |
| 127 | { 0x67A0, 7, 0, 1, "Hawaii" }, |
| 128 | { 0x67A1, 7, 0, 1, "Hawaii" }, |
| 129 | { 0x67A2, 7, 0, 1, "Hawaii" }, |
| 130 | { 0x67A8, 7, 0, 1, "Hawaii" }, |
| 131 | { 0x67A9, 7, 0, 1, "Hawaii" }, |
| 132 | { 0x67AA, 7, 0, 1, "Hawaii" }, |
| 133 | { 0x67B0, 7, 0, 1, "Hawaii" }, |
| 134 | { 0x67B1, 7, 0, 1, "Hawaii" }, |
| 135 | { 0x67B8, 7, 0, 1, "Hawaii" }, |
| 136 | { 0x67B9, 7, 0, 1, "Hawaii" }, |
| 137 | { 0x67BA, 7, 0, 1, "Hawaii" }, |
| 138 | { 0x67BE, 7, 0, 1, "Hawaii" }, |
| 139 | /* Carrizo Family */ |
| 140 | { 0x9870, 8, 0, 1, "Carrizo" }, |
| 141 | { 0x9874, 8, 0, 1, "Carrizo" }, |
| 142 | { 0x9875, 8, 0, 1, "Carrizo" }, |
| 143 | { 0x9876, 8, 0, 1, "Carrizo" }, |
| 144 | { 0x9877, 8, 0, 1, "Carrizo" }, |
| 145 | /* Tonga Family */ |
| 146 | { 0x6920, 8, 0, 2, "Tonga" }, |
| 147 | { 0x6921, 8, 0, 2, "Tonga" }, |
| 148 | { 0x6928, 8, 0, 2, "Tonga" }, |
| 149 | { 0x6929, 8, 0, 2, "Tonga" }, |
| 150 | { 0x692B, 8, 0, 2, "Tonga" }, |
| 151 | { 0x692F, 8, 0, 2, "Tonga" }, |
| 152 | { 0x6930, 8, 0, 2, "Tonga" }, |
| 153 | { 0x6938, 8, 0, 2, "Tonga" }, |
| 154 | { 0x6939, 8, 0, 2, "Tonga" }, |
| 155 | /* Fiji */ |
| 156 | { 0x7300, 8, 0, 3, "Fiji" }, |
| 157 | { 0x730F, 8, 0, 3, "Fiji" }, |
| 158 | /* Polaris10 */ |
| 159 | { 0x67C0, 8, 0, 3, "Polaris10" }, |
| 160 | { 0x67C1, 8, 0, 3, "Polaris10" }, |
| 161 | { 0x67C2, 8, 0, 3, "Polaris10" }, |
| 162 | { 0x67C4, 8, 0, 3, "Polaris10" }, |
| 163 | { 0x67C7, 8, 0, 3, "Polaris10" }, |
| 164 | { 0x67C8, 8, 0, 3, "Polaris10" }, |
| 165 | { 0x67C9, 8, 0, 3, "Polaris10" }, |
| 166 | { 0x67CA, 8, 0, 3, "Polaris10" }, |
| 167 | { 0x67CC, 8, 0, 3, "Polaris10" }, |
| 168 | { 0x67CF, 8, 0, 3, "Polaris10" }, |
| 169 | { 0x67D0, 8, 0, 3, "Polaris10" }, |
| 170 | { 0x67DF, 8, 0, 3, "Polaris10" }, |
| 171 | { 0x6FDF, 8, 0, 3, "Polaris10" }, |
| 172 | /* Polaris11 */ |
| 173 | { 0x67E0, 8, 0, 3, "Polaris11" }, |
| 174 | { 0x67E1, 8, 0, 3, "Polaris11" }, |
| 175 | { 0x67E3, 8, 0, 3, "Polaris11" }, |
| 176 | { 0x67E7, 8, 0, 3, "Polaris11" }, |
| 177 | { 0x67E8, 8, 0, 3, "Polaris11" }, |
| 178 | { 0x67E9, 8, 0, 3, "Polaris11" }, |
| 179 | { 0x67EB, 8, 0, 3, "Polaris11" }, |
| 180 | { 0x67EF, 8, 0, 3, "Polaris11" }, |
| 181 | { 0x67FF, 8, 0, 3, "Polaris11" }, |
| 182 | /* Polaris12 */ |
| 183 | { 0x6980, 8, 0, 3, "Polaris12" }, |
| 184 | { 0x6981, 8, 0, 3, "Polaris12" }, |
| 185 | { 0x6985, 8, 0, 3, "Polaris12" }, |
| 186 | { 0x6986, 8, 0, 3, "Polaris12" }, |
| 187 | { 0x6987, 8, 0, 3, "Polaris12" }, |
| 188 | { 0x6995, 8, 0, 3, "Polaris12" }, |
| 189 | { 0x6997, 8, 0, 3, "Polaris12" }, |
| 190 | { 0x699F, 8, 0, 3, "Polaris12" }, |
| 191 | /* VegaM */ |
| 192 | { 0x694C, 8, 0, 3, "VegaM" }, |
| 193 | { 0x694E, 8, 0, 3, "VegaM" }, |
| 194 | { 0x694F, 8, 0, 3, "VegaM" }, |
| 195 | /* Vega10 */ |
| 196 | { 0x6860, 9, 0, 0, "Vega10" }, |
| 197 | { 0x6861, 9, 0, 0, "Vega10" }, |
| 198 | { 0x6862, 9, 0, 0, "Vega10" }, |
| 199 | { 0x6863, 9, 0, 0, "Vega10" }, |
| 200 | { 0x6864, 9, 0, 0, "Vega10" }, |
| 201 | { 0x6867, 9, 0, 0, "Vega10" }, |
| 202 | { 0x6868, 9, 0, 0, "Vega10" }, |
| 203 | { 0x6869, 9, 0, 0, "Vega10" }, |
| 204 | { 0x686A, 9, 0, 0, "Vega10" }, |
| 205 | { 0x686B, 9, 0, 0, "Vega10" }, |
| 206 | { 0x686C, 9, 0, 0, "Vega10" }, |
| 207 | { 0x686D, 9, 0, 0, "Vega10" }, |
| 208 | { 0x686E, 9, 0, 0, "Vega10" }, |
| 209 | { 0x687F, 9, 0, 0, "Vega10" }, |
| 210 | /* Vega12 */ |
| 211 | { 0x69A0, 9, 0, 4, "Vega12" }, |
| 212 | { 0x69A1, 9, 0, 4, "Vega12" }, |
| 213 | { 0x69A2, 9, 0, 4, "Vega12" }, |
| 214 | { 0x69A3, 9, 0, 4, "Vega12" }, |
| 215 | { 0x69Af, 9, 0, 4, "Vega12" }, |
| 216 | /* Raven */ |
| 217 | { 0x15DD, 9, 0, 2, "Raven" }, |
| 218 | { 0x15D8, 9, 0, 2, "Raven" }, |
| 219 | /* Vega20 */ |
| 220 | { 0x66A0, 9, 0, 6, "Vega20" }, |
| 221 | { 0x66A1, 9, 0, 6, "Vega20" }, |
| 222 | { 0x66A2, 9, 0, 6, "Vega20" }, |
| 223 | { 0x66A3, 9, 0, 6, "Vega20" }, |
| 224 | { 0x66A4, 9, 0, 6, "Vega20" }, |
| 225 | { 0x66A7, 9, 0, 6, "Vega20" }, |
| 226 | { 0x66AF, 9, 0, 6, "Vega20" }, |
| 227 | /* Arcturus */ |
| 228 | { 0x7388, 9, 0, 8, "Arcturus" }, |
| 229 | { 0x738C, 9, 0, 8, "Arcturus" }, |
| 230 | { 0x738E, 9, 0, 8, "Arcturus" }, |
| 231 | { 0x7390, 9, 0, 8, "Arcturus" }, |
| 232 | /* Aldebaran */ |
| 233 | { 0x7408, 9, 0, 10, "Aldebaran" }, |
| 234 | { 0x740C, 9, 0, 10, "Aldebaran" }, |
| 235 | { 0x740F, 9, 0, 10, "Aldebaran" }, |
| 236 | { 0x7410, 9, 0, 10, "Aldebaran" }, |
| 237 | /* Renoir */ |
| 238 | { 0x15E7, 9, 0, 12, "Renoir" }, |
| 239 | { 0x1636, 9, 0, 12, "Renoir" }, |
| 240 | { 0x1638, 9, 0, 12, "Renoir" }, |
| 241 | { 0x164C, 9, 0, 12, "Renoir" }, |
| 242 | /* Navi10 */ |
| 243 | { 0x7310, 10, 1, 0, "Navi10" }, |
| 244 | { 0x7312, 10, 1, 0, "Navi10" }, |
| 245 | { 0x7318, 10, 1, 0, "Navi10" }, |
| 246 | { 0x731A, 10, 1, 0, "Navi10" }, |
| 247 | { 0x731E, 10, 1, 0, "Navi10" }, |
| 248 | { 0x731F, 10, 1, 0, "Navi10" }, |
| 249 | /* cyan_skillfish */ |
| 250 | { 0x13F9, 10, 1, 3, "cyan_skillfish" }, |
| 251 | { 0x13FA, 10, 1, 3, "cyan_skillfish" }, |
| 252 | { 0x13FB, 10, 1, 3, "cyan_skillfish" }, |
| 253 | { 0x13FC, 10, 1, 3, "cyan_skillfish" }, |
| 254 | { 0x13FE, 10, 1, 3, "cyan_skillfish" }, |
| 255 | { 0x143F, 10, 1, 3, "cyan_skillfish" }, |
| 256 | /* Navi14 */ |
| 257 | { 0x7340, 10, 1, 2, "Navi14" }, |
| 258 | { 0x7341, 10, 1, 2, "Navi14" }, |
| 259 | { 0x7347, 10, 1, 2, "Navi14" }, |
| 260 | /* Navi12 */ |
| 261 | { 0x7360, 10, 1, 1, "Navi12" }, |
| 262 | { 0x7362, 10, 1, 1, "Navi12" }, |
| 263 | /* SIENNA_CICHLID */ |
| 264 | { 0x73A0, 10, 3, 0, "SIENNA_CICHLID" }, |
| 265 | { 0x73A1, 10, 3, 0, "SIENNA_CICHLID" }, |
| 266 | { 0x73A2, 10, 3, 0, "SIENNA_CICHLID" }, |
| 267 | { 0x73A3, 10, 3, 0, "SIENNA_CICHLID" }, |
| 268 | { 0x73A5, 10, 3, 0, "SIENNA_CICHLID" }, |
| 269 | { 0x73A8, 10, 3, 0, "SIENNA_CICHLID" }, |
| 270 | { 0x73A9, 10, 3, 0, "SIENNA_CICHLID" }, |
| 271 | { 0x73AC, 10, 3, 0, "SIENNA_CICHLID" }, |
| 272 | { 0x73AD, 10, 3, 0, "SIENNA_CICHLID" }, |
| 273 | { 0x73AB, 10, 3, 0, "SIENNA_CICHLID" }, |
| 274 | { 0x73AE, 10, 3, 0, "SIENNA_CICHLID" }, |
| 275 | { 0x73BF, 10, 3, 0, "SIENNA_CICHLID" }, |
| 276 | /* NAVY_FLOUNDER */ |
| 277 | { 0x73C0, 10, 3, 1, "NAVY_FLOUNDER" }, |
| 278 | { 0x73C1, 10, 3, 1, "NAVY_FLOUNDER" }, |
| 279 | { 0x73C3, 10, 3, 1, "NAVY_FLOUNDER" }, |
| 280 | { 0x73DA, 10, 3, 1, "NAVY_FLOUNDER" }, |
| 281 | { 0x73DB, 10, 3, 1, "NAVY_FLOUNDER" }, |
| 282 | { 0x73DC, 10, 3, 1, "NAVY_FLOUNDER" }, |
| 283 | { 0x73DD, 10, 3, 1, "NAVY_FLOUNDER" }, |
| 284 | { 0x73DE, 10, 3, 1, "NAVY_FLOUNDER" }, |
| 285 | { 0x73DF, 10, 3, 1, "NAVY_FLOUNDER" }, |
| 286 | /* DIMGREY_CAVEFISH */ |
| 287 | { 0x73E0, 10, 3, 2, "DIMGREY_CAVEFISH" }, |
| 288 | { 0x73E1, 10, 3, 2, "DIMGREY_CAVEFISH" }, |
| 289 | { 0x73E2, 10, 3, 2, "DIMGREY_CAVEFISH" }, |
| 290 | { 0x73E8, 10, 3, 2, "DIMGREY_CAVEFISH" }, |
| 291 | { 0x73E9, 10, 3, 2, "DIMGREY_CAVEFISH" }, |
| 292 | { 0x73EA, 10, 3, 2, "DIMGREY_CAVEFISH" }, |
| 293 | { 0x73EB, 10, 3, 2, "DIMGREY_CAVEFISH" }, |
| 294 | { 0x73EC, 10, 3, 2, "DIMGREY_CAVEFISH" }, |
| 295 | { 0x73ED, 10, 3, 2, "DIMGREY_CAVEFISH" }, |
| 296 | { 0x73EF, 10, 3, 2, "DIMGREY_CAVEFISH" }, |
| 297 | { 0x73FF, 10, 3, 2, "DIMGREY_CAVEFISH" }, |
| 298 | /* VanGogh */ |
| 299 | { 0x163F, 10, 3, 3, "VanGogh" }, |
| 300 | /* BEIGE_GOBY */ |
| 301 | { 0x7420, 10, 3, 4, "BEIGE_GOBY" }, |
| 302 | { 0x7421, 10, 3, 4, "BEIGE_GOBY" }, |
| 303 | { 0x7422, 10, 3, 4, "BEIGE_GOBY" }, |
| 304 | { 0x7423, 10, 3, 4, "BEIGE_GOBY" }, |
| 305 | { 0x743F, 10, 3, 4, "BEIGE_GOBY" }, |
| 306 | /* Yellow_Carp */ |
| 307 | { 0x164D, 10, 3, 5, "YELLOW_CARP" }, |
| 308 | { 0x1681, 10, 3, 5, "YELLOW_CARP" }, |
| 309 | }; |
| 310 | |
| 311 | /* information from /proc/cpuinfo */ |
| 312 | struct proc_cpuinfo { |
| 313 | uint32_t proc_num; /* processor */ |
| 314 | uint32_t apicid; /* apicid */ |
| 315 | char model_name[HSA_PUBLIC_NAME_SIZE]; /* model name */ |
| 316 | }; |
| 317 | |
| 318 | /* CPU cache table for all CPUs on the system. Each entry has the relative CPU |
| 319 | * info and caches connected to that CPU. |
| 320 | */ |
| 321 | typedef struct cpu_cacheinfo { |
| 322 | uint32_t len; /* length of the table = number of online procs */ |
| 323 | int32_t proc_num; /* this cpu's processor number */ |
| 324 | uint32_t num_caches; /* number of caches reported by this cpu */ |
| 325 | HsaCacheProperties *cache_prop; /* a list of cache properties */ |
| 326 | } cpu_cacheinfo_t; |
| 327 | |
| 328 | static void free_properties(node_props_t *props, int size) |
| 329 | { |
| 330 | if (props) { |
| 331 | int i; |
| 332 | for (i = 0; i < size; i++) { |
| 333 | free(props[i].mem); |
| 334 | free(props[i].cache); |
| 335 | free(props[i].link); |
| 336 | } |
| 337 | |
| 338 | free(props); |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | /* num_subdirs - find the number of sub-directories in the specified path |
| 343 | * @dirpath - directory path to find sub-directories underneath |
| 344 | * @prefix - only count sub-directory names starting with prefix. |
| 345 | * Use blank string, "", to count all. |
| 346 | * Return - number of sub-directories |
| 347 | */ |
| 348 | static int num_subdirs(char *dirpath, char *prefix) |
| 349 | { |
| 350 | int count = 0; |
| 351 | DIR *dirp; |
| 352 | struct dirent *dir; |
| 353 | int prefix_len = strlen(prefix); |
| 354 | |
| 355 | dirp = opendir(dirpath); |
| 356 | if (dirp) { |
| 357 | while ((dir = readdir(dirp)) != 0) { |
| 358 | if ((strcmp(dir->d_name, ".") == 0) || |
| 359 | (strcmp(dir->d_name, "..") == 0)) |
| 360 | continue; |
| 361 | if (prefix_len && |
| 362 | strncmp(dir->d_name, prefix, prefix_len)) |
| 363 | continue; |
| 364 | count++; |
| 365 | } |
| 366 | closedir(dirp); |
| 367 | } |
| 368 | |
| 369 | return count; |
| 370 | } |
| 371 | |
| 372 | /* fscanf_dec - read a file whose content is a decimal number |
| 373 | * @file [IN ] file to read |
| 374 | * @num [OUT] number in the file |
| 375 | */ |
| 376 | static HSAKMT_STATUS fscanf_dec(char *file, uint32_t *num) |
| 377 | { |
| 378 | FILE *fd; |
| 379 | HSAKMT_STATUS ret = HSAKMT_STATUS_SUCCESS; |
| 380 | |
| 381 | fd = fopen(file, "r"); |
| 382 | if (!fd) { |
| 383 | pr_err("Failed to open %s\n", file); |
| 384 | return HSAKMT_STATUS_INVALID_PARAMETER; |
| 385 | } |
| 386 | if (fscanf(fd, "%u", num) != 1) { |
| 387 | pr_err("Failed to parse %s as a decimal.\n", file); |
| 388 | ret = HSAKMT_STATUS_ERROR; |
| 389 | } |
| 390 | |
| 391 | fclose(fd); |
| 392 | return ret; |
| 393 | } |
| 394 | |
| 395 | /* fscanf_str - read a file whose content is a string |
| 396 | * @file [IN ] file to read |
| 397 | * @str [OUT] string in the file |
| 398 | */ |
| 399 | static HSAKMT_STATUS fscanf_str(const char *file, char *str, size_t str_size) |
| 400 | { |
| 401 | FILE *fd; |
| 402 | HSAKMT_STATUS ret = HSAKMT_STATUS_SUCCESS; |
| 403 | |
| 404 | fd = fopen(file, "r"); |
| 405 | if (!fd) { |
| 406 | pr_err("Failed to open %s\n", file); |
| 407 | return HSAKMT_STATUS_INVALID_PARAMETER; |
| 408 | } |
| 409 | |
| 410 | if (!fgets(str, (int)str_size, fd)) { |
| 411 | pr_err("Failed to read from %s.\n", file); |
| 412 | ret = HSAKMT_STATUS_ERROR; |
| 413 | } else { |
| 414 | // Remove possible newline characters at the end, due to using fgets function |
| 415 | str[strcspn(str, "\r\n")] = '\0'; |
| 416 | } |
| 417 | |
| 418 | fclose(fd); |
| 419 | return ret; |
| 420 | } |
| 421 | |
| 422 | /* fscanf_size - read a file whose content represents size as a string |
| 423 | * @file [IN ] file to read |
| 424 | * @bytes [OUT] sizes in bytes |
| 425 | */ |
| 426 | static HSAKMT_STATUS fscanf_size(char *file, uint32_t *bytes) |
| 427 | { |
| 428 | FILE *fd; |
| 429 | HSAKMT_STATUS ret = HSAKMT_STATUS_SUCCESS; |
| 430 | char unit; |
| 431 | int n; |
| 432 | |
| 433 | fd = fopen(file, "r"); |
| 434 | if (!fd) { |
| 435 | pr_err("Failed to open %s\n", file); |
| 436 | return HSAKMT_STATUS_INVALID_PARAMETER; |
| 437 | } |
| 438 | |
| 439 | n = fscanf(fd, "%u%c", bytes, &unit); |
| 440 | if (n < 1) { |
| 441 | pr_err("Failed to parse %s\n", file); |
| 442 | ret = HSAKMT_STATUS_ERROR; |
| 443 | } |
| 444 | |
| 445 | if (n == 2) { |
| 446 | switch (unit) { |
| 447 | case 'K': |
| 448 | *bytes <<= 10; break; |
| 449 | case 'M': |
| 450 | *bytes <<= 20; break; |
| 451 | case 'G': |
| 452 | *bytes <<= 30; break; |
| 453 | default: |
| 454 | ret = HSAKMT_STATUS_ERROR; break; |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | fclose(fd); |
| 459 | return ret; |
| 460 | } |
| 461 | |
| 462 | /* cpumap_to_cpu_ci - translate shared_cpu_map string + cpuinfo->apicid into |
| 463 | * SiblingMap in cache |
| 464 | * @shared_cpu_map [IN ] shared_cpu_map string |
| 465 | * @cpuinfo [IN ] cpuinfo to get apicid |
| 466 | * @this_cache [OUT] CPU cache to fill in SiblingMap |
| 467 | */ |
| 468 | static void cpumap_to_cpu_ci(char *shared_cpu_map, |
| 469 | struct proc_cpuinfo *cpuinfo, |
| 470 | HsaCacheProperties *this_cache) |
| 471 | { |
| 472 | uint32_t num_hexs, bit; |
| 473 | uint32_t proc, apicid, mask; |
| 474 | char *ch_ptr; |
| 475 | |
| 476 | /* shared_cpu_map is shown as ...X3,X2,X1 Each X is a hex without 0x |
| 477 | * and it's up to 8 characters(32 bits). For the first 32 CPUs(actually |
| 478 | * procs), it's presented in X1. The next 32 is in X2, and so on. |
| 479 | */ |
| 480 | num_hexs = (strlen(shared_cpu_map) + 8) / 9; /* 8 characters + "," */ |
| 481 | ch_ptr = strtok(shared_cpu_map, ","); |
| 482 | while (num_hexs-- > 0) { |
| 483 | mask = strtol(ch_ptr, NULL, 16); /* each X */ |
| 484 | for (bit = 0; bit < 32; bit++) { |
| 485 | if (!((1U << bit) & mask)) |
| 486 | continue; |
| 487 | proc = num_hexs * 32 + bit; |
| 488 | apicid = cpuinfo[proc].apicid; |
| 489 | if (apicid >= HSA_CPU_SIBLINGS) { |
| 490 | pr_warn("SiblingMap buffer %d is too small\n", |
| 491 | HSA_CPU_SIBLINGS); |
| 492 | continue; |
| 493 | } |
| 494 | this_cache->SiblingMap[apicid] = 1; |
| 495 | } |
| 496 | ch_ptr = strtok(NULL, ","); |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | /* get_cpu_cache_info - get specified CPU's cache information from sysfs |
| 501 | * @prefix [IN] sysfs path for target cpu cache, |
| 502 | * /sys/devices/system/node/nodeX/cpuY/cache |
| 503 | * @cpuinfo [IN] /proc/cpuinfo data to get apicid |
| 504 | * @cpu_ci: CPU specified. This parameter is an input and also an output. |
| 505 | * [IN] cpu_ci->num_caches: number of index dirs |
| 506 | * [OUT] cpu_ci->cache_info: to store cache info collected |
| 507 | * [OUT] cpu_ci->num_caches: reduces when shared with other cpu(s) |
| 508 | * Return: number of cache reported from this cpu |
| 509 | */ |
| 510 | static int get_cpu_cache_info(const char *prefix, struct proc_cpuinfo *cpuinfo, |
| 511 | cpu_cacheinfo_t *cpu_ci) |
| 512 | { |
| 513 | int idx, num_idx, n; |
| 514 | HsaCacheProperties *this_cache; |
| 515 | char path[256], str[256]; |
| 516 | bool is_power9 = false; |
| 517 | |
| 518 | if (processor_vendor == IBM_POWER) { |
| 519 | if (strcmp(cpuinfo[0].model_name, "POWER9") == 0) { |
| 520 | is_power9 = true; |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | this_cache = cpu_ci->cache_prop; |
| 525 | num_idx = cpu_ci->num_caches; |
| 526 | for (idx = 0; idx < num_idx; idx++) { |
| 527 | /* If this cache is shared by multiple CPUs, we only need |
| 528 | * to list it in the first CPU. |
| 529 | */ |
| 530 | if (is_power9) { |
| 531 | // POWER9 has SMT4 |
| 532 | if (cpu_ci->proc_num & 0x3) { |
| 533 | /* proc is not 0,4,8,etc. Skip and reduce the cache count. */ |
| 534 | --cpu_ci->num_caches; |
| 535 | continue; |
| 536 | } |
| 537 | } else { |
| 538 | snprintf(path, 256, "%s/index%d/shared_cpu_list", prefix, idx); |
| 539 | /* shared_cpu_list is shown as n1,n2... or n1-n2,n3-n4... |
| 540 | * For both cases, this cache is listed to proc n1 only. |
| 541 | */ |
| 542 | fscanf_dec(path, (uint32_t *)&n); |
| 543 | if (cpu_ci->proc_num != n) { |
| 544 | /* proc is not n1. Skip and reduce the cache count. */ |
| 545 | --cpu_ci->num_caches; |
| 546 | continue; |
| 547 | } |
| 548 | this_cache->ProcessorIdLow = cpuinfo[cpu_ci->proc_num].apicid; |
| 549 | } |
| 550 | |
| 551 | /* CacheLevel */ |
| 552 | snprintf(path, 256, "%s/index%d/level", prefix, idx); |
| 553 | fscanf_dec(path, &this_cache->CacheLevel); |
| 554 | /* CacheType */ |
| 555 | snprintf(path, 256, "%s/index%d/type", prefix, idx); |
| 556 | |
| 557 | memset(str, 0, sizeof(str)); |
| 558 | fscanf_str(path, str, sizeof(str)); |
| 559 | if (!strcmp(str, "Data")) |
| 560 | this_cache->CacheType.ui32.Data = 1; |
| 561 | if (!strcmp(str, "Instruction")) |
| 562 | this_cache->CacheType.ui32.Instruction = 1; |
| 563 | if (!strcmp(str, "Unified")) { |
| 564 | this_cache->CacheType.ui32.Data = 1; |
| 565 | this_cache->CacheType.ui32.Instruction = 1; |
| 566 | } |
| 567 | this_cache->CacheType.ui32.CPU = 1; |
| 568 | /* CacheSize */ |
| 569 | snprintf(path, 256, "%s/index%d/size", prefix, idx); |
| 570 | fscanf_size(path, &this_cache->CacheSize); |
| 571 | /* CacheLineSize */ |
| 572 | snprintf(path, 256, "%s/index%d/coherency_line_size", prefix, idx); |
| 573 | fscanf_dec(path, &this_cache->CacheLineSize); |
| 574 | /* CacheAssociativity */ |
| 575 | snprintf(path, 256, "%s/index%d/ways_of_associativity", prefix, idx); |
| 576 | fscanf_dec(path, &this_cache->CacheAssociativity); |
| 577 | /* CacheLinesPerTag */ |
| 578 | snprintf(path, 256, "%s/index%d/physical_line_partition", prefix, idx); |
| 579 | fscanf_dec(path, &this_cache->CacheLinesPerTag); |
| 580 | /* CacheSiblings */ |
| 581 | snprintf(path, 256, "%s/index%d/shared_cpu_map", prefix, idx); |
| 582 | fscanf_str(path, str, sizeof(str)); |
| 583 | cpumap_to_cpu_ci(str, cpuinfo, this_cache); |
| 584 | |
| 585 | ++this_cache; |
| 586 | } |
| 587 | |
| 588 | return cpu_ci->num_caches; |
| 589 | } |
| 590 | |
| 591 | static HSAKMT_STATUS topology_sysfs_get_generation(uint32_t *gen) |
| 592 | { |
| 593 | FILE *fd; |
| 594 | HSAKMT_STATUS ret = HSAKMT_STATUS_SUCCESS; |
| 595 | |
| 596 | char path[256]; |
| 597 | snprintf(path, sizeof(path), KFD_SYSFS_PATH_GENERATION_ID, get_topology_dir()); |
| 598 | |
| 599 | assert(gen); |
| 600 | fd = fopen(path, "r"); |
| 601 | if (!fd) |
| 602 | return HSAKMT_STATUS_ERROR; |
| 603 | if (fscanf(fd, "%ul", gen) != 1) { |
| 604 | ret = HSAKMT_STATUS_ERROR; |
| 605 | goto err; |
| 606 | } |
| 607 | |
| 608 | err: |
| 609 | fclose(fd); |
| 610 | return ret; |
| 611 | } |
| 612 | |
| 613 | static HSAKMT_STATUS topology_sysfs_map_node_id(uint32_t node_id, uint32_t *sys_node_id) |
| 614 | { |
| 615 | if ((!map_user_to_sysfs_node_id) || (node_id >= map_user_to_sysfs_node_id_size)) |
| 616 | return HSAKMT_STATUS_NOT_SUPPORTED; |
| 617 | |
| 618 | *sys_node_id = map_user_to_sysfs_node_id[node_id]; |
| 619 | return HSAKMT_STATUS_SUCCESS; |
| 620 | } |
| 621 | |
| 622 | static HSAKMT_STATUS topology_sysfs_get_gpu_id(uint32_t sysfs_node_id, uint32_t *gpu_id) |
| 623 | { |
| 624 | FILE *fd; |
| 625 | char path[256]; |
| 626 | HSAKMT_STATUS ret = HSAKMT_STATUS_SUCCESS; |
| 627 | |
| 628 | assert(gpu_id); |
| 629 | snprintf(path, sizeof(path), KFD_SYSFS_PATH_NODES "/%d/gpu_id", get_topology_dir(), sysfs_node_id); |
| 630 | fd = fopen(path, "r"); |
| 631 | if (!fd) |
| 632 | return HSAKMT_STATUS_ERROR; |
| 633 | if (fscanf(fd, "%ul", gpu_id) != 1) |
| 634 | ret = (errno == EPERM) ? HSAKMT_STATUS_NOT_SUPPORTED : |
| 635 | HSAKMT_STATUS_ERROR; |
| 636 | fclose(fd); |
| 637 | |
| 638 | return ret; |
| 639 | } |
| 640 | |
| 641 | /* Check if the @sysfs_node_id is supported. This function will be passed with sysfs node id. |
| 642 | * This function can not use topology_* help functions, because those functions are |
| 643 | * using user node id. |
| 644 | * A sysfs node is not supported |
| 645 | * - if corresponding drm render node is not available. |
| 646 | * - if node information is not accessible (EPERM) |
| 647 | */ |
| 648 | static HSAKMT_STATUS topology_sysfs_check_node_supported(HsaKFDContext *ctx, |
| 649 | uint32_t sysfs_node_id, bool *is_node_supported) |
| 650 | { |
| 651 | uint32_t gpu_id; |
| 652 | FILE *fd; |
| 653 | char *read_buf, *p; |
| 654 | int read_size; |
| 655 | char prop_name[256]; |
| 656 | char path[256]; |
| 657 | unsigned long long prop_val; |
| 658 | uint32_t prog; |
| 659 | uint32_t drm_render_minor = 0; |
| 660 | int ret_value; |
| 661 | HSAKMT_STATUS ret = HSAKMT_STATUS_SUCCESS; |
| 662 | |
| 663 | *is_node_supported = false; |
| 664 | |
| 665 | /* Retrieve the GPU ID */ |
| 666 | ret = topology_sysfs_get_gpu_id(sysfs_node_id, &gpu_id); |
| 667 | if (ret == HSAKMT_STATUS_NOT_SUPPORTED) |
| 668 | return HSAKMT_STATUS_SUCCESS; |
| 669 | if (ret != HSAKMT_STATUS_SUCCESS) |
| 670 | return ret; |
| 671 | |
| 672 | if (gpu_id == 0) { |
| 673 | *is_node_supported = true; |
| 674 | return HSAKMT_STATUS_SUCCESS; |
| 675 | } |
| 676 | |
| 677 | read_buf = malloc(PAGE_SIZE); |
| 678 | if (!read_buf) |
| 679 | return HSAKMT_STATUS_NO_MEMORY; |
| 680 | |
| 681 | /* Retrieve the node properties */ |
| 682 | snprintf(path, 256, KFD_SYSFS_PATH_NODES "/%d/properties", get_topology_dir(), sysfs_node_id); |
| 683 | fd = fopen(path, "r"); |
| 684 | if (!fd) { |
| 685 | free(read_buf); |
| 686 | return HSAKMT_STATUS_ERROR; |
| 687 | } |
| 688 | |
| 689 | read_size = fread(read_buf, 1, PAGE_SIZE, fd); |
| 690 | if (read_size <= 0) { |
| 691 | ret = HSAKMT_STATUS_ERROR; |
| 692 | goto err; |
| 693 | } |
| 694 | |
| 695 | /* Since we're using the buffer as a string, we make sure the string terminates */ |
| 696 | if (read_size >= PAGE_SIZE) |
| 697 | read_size = PAGE_SIZE - 1; |
| 698 | read_buf[read_size] = 0; |
| 699 | |
| 700 | /* Read the node properties */ |
| 701 | prog = 0; |
| 702 | p = read_buf; |
| 703 | while (sscanf(p += prog, "%s %llu\n%n", prop_name, &prop_val, &prog) == 2) { |
| 704 | if (strcmp(prop_name, "drm_render_minor") == 0) { |
| 705 | drm_render_minor = (int32_t)prop_val; |
| 706 | break; |
| 707 | } |
| 708 | } |
| 709 | if (!drm_render_minor) { |
| 710 | ret = HSAKMT_STATUS_ERROR; |
| 711 | goto err; |
| 712 | } |
| 713 | |
| 714 | /* Open DRM Render device */ |
| 715 | ret_value = hsakmt_open_drm_render_device(ctx, drm_render_minor); |
| 716 | if (ret_value > 0) |
| 717 | *is_node_supported = true; |
| 718 | else if (ret_value != -ENOENT && ret_value != -EPERM) |
| 719 | ret = HSAKMT_STATUS_ERROR; |
| 720 | |
| 721 | err: |
| 722 | free(read_buf); |
| 723 | fclose(fd); |
| 724 | return ret; |
| 725 | } |
| 726 | |
| 727 | HSAKMT_STATUS hsakmt_topology_sysfs_get_system_props(HsaKFDContext *ctx, |
| 728 | HsaSystemProperties *props) |
| 729 | { |
| 730 | FILE *fd; |
| 731 | char *read_buf, *p; |
| 732 | char path[256]; |
| 733 | char prop_name[256]; |
| 734 | unsigned long long prop_val; |
| 735 | uint32_t prog; |
| 736 | int read_size; |
| 737 | HSAKMT_STATUS ret = HSAKMT_STATUS_SUCCESS; |
| 738 | bool is_node_supported = true; |
| 739 | uint32_t num_supported_nodes = 0; |
| 740 | |
| 741 | assert(props); |
| 742 | snprintf(path, sizeof(path), KFD_SYSFS_PATH_SYSTEM_PROPERTIES, get_topology_dir()); |
| 743 | fd = fopen(path, "r"); |
| 744 | if (!fd) |
| 745 | return HSAKMT_STATUS_ERROR; |
| 746 | |
| 747 | read_buf = malloc(PAGE_SIZE); |
| 748 | if (!read_buf) { |
| 749 | ret = HSAKMT_STATUS_NO_MEMORY; |
| 750 | goto err1; |
| 751 | } |
| 752 | |
| 753 | read_size = fread(read_buf, 1, PAGE_SIZE, fd); |
| 754 | if (read_size <= 0) { |
| 755 | ret = HSAKMT_STATUS_ERROR; |
| 756 | goto err2; |
| 757 | } |
| 758 | |
| 759 | /* Since we're using the buffer as a string, we make sure the string terminates */ |
| 760 | if (read_size >= PAGE_SIZE) |
| 761 | read_size = PAGE_SIZE - 1; |
| 762 | read_buf[read_size] = 0; |
| 763 | |
| 764 | /* Read the system properties */ |
| 765 | prog = 0; |
| 766 | p = read_buf; |
| 767 | while (sscanf(p += prog, "%s %llu\n%n", prop_name, &prop_val, &prog) == 2) { |
| 768 | if (strcmp(prop_name, "platform_oem") == 0) |
| 769 | props->PlatformOem = (uint32_t)prop_val; |
| 770 | else if (strcmp(prop_name, "platform_id") == 0) |
| 771 | props->PlatformId = (uint32_t)prop_val; |
| 772 | else if (strcmp(prop_name, "platform_rev") == 0) |
| 773 | props->PlatformRev = (uint32_t)prop_val; |
| 774 | } |
| 775 | |
| 776 | /* |
| 777 | * Discover the number of sysfs nodes: |
| 778 | * Assuming that inside nodes folder there are only folders |
| 779 | * which represent the node numbers |
| 780 | */ |
| 781 | snprintf(path, sizeof(path), KFD_SYSFS_PATH_NODES, get_topology_dir()); |
| 782 | num_sysfs_nodes = num_subdirs(path, ""); |
| 783 | |
| 784 | if (map_user_to_sysfs_node_id == NULL) { |
| 785 | /* Trade off - num_sysfs_nodes includes all CPU and GPU nodes. |
| 786 | * Slightly more memory is allocated than necessary. |
| 787 | */ |
| 788 | map_user_to_sysfs_node_id = calloc(num_sysfs_nodes, sizeof(uint32_t)); |
| 789 | if (map_user_to_sysfs_node_id == NULL) { |
| 790 | ret = HSAKMT_STATUS_NO_MEMORY; |
| 791 | goto err2; |
| 792 | } |
| 793 | map_user_to_sysfs_node_id_size = num_sysfs_nodes; |
| 794 | } else if (num_sysfs_nodes > map_user_to_sysfs_node_id_size) { |
| 795 | free(map_user_to_sysfs_node_id); |
| 796 | map_user_to_sysfs_node_id = calloc(num_sysfs_nodes, sizeof(uint32_t)); |
| 797 | if (map_user_to_sysfs_node_id == NULL) { |
| 798 | ret = HSAKMT_STATUS_NO_MEMORY; |
| 799 | goto err2; |
| 800 | } |
| 801 | map_user_to_sysfs_node_id_size = num_sysfs_nodes; |
| 802 | } |
| 803 | |
| 804 | for (uint32_t i = 0; i < num_sysfs_nodes; i++) { |
| 805 | ret = topology_sysfs_check_node_supported(ctx, i, &is_node_supported); |
| 806 | if (ret != HSAKMT_STATUS_SUCCESS) |
| 807 | goto sysfs_parse_failed; |
| 808 | if (is_node_supported) |
| 809 | map_user_to_sysfs_node_id[num_supported_nodes++] = i; |
| 810 | } |
| 811 | props->NumNodes = num_supported_nodes; |
| 812 | |
| 813 | free(read_buf); |
| 814 | fclose(fd); |
| 815 | return ret; |
| 816 | |
| 817 | sysfs_parse_failed: |
| 818 | free(map_user_to_sysfs_node_id); |
| 819 | map_user_to_sysfs_node_id = NULL; |
| 820 | err2: |
| 821 | free(read_buf); |
| 822 | err1: |
| 823 | fclose(fd); |
| 824 | return ret; |
| 825 | } |
| 826 | |
| 827 | static const struct hsa_gfxip_table *find_hsa_gfxip_device(uint16_t device_id, uint8_t gfxv_major) |
| 828 | { |
| 829 | if (gfxv_major > 10) |
| 830 | return NULL; |
| 831 | |
| 832 | uint32_t i, table_size; |
| 833 | |
| 834 | table_size = sizeof(gfxip_lookup_table)/sizeof(struct hsa_gfxip_table); |
| 835 | for (i = 0; i < table_size; i++) { |
| 836 | if (gfxip_lookup_table[i].device_id == device_id) |
| 837 | return &gfxip_lookup_table[i]; |
| 838 | } |
| 839 | return NULL; |
| 840 | } |
| 841 | |
| 842 | void hsakmt_topology_setup_is_dgpu_param(HsaNodeProperties *props) |
| 843 | { |
| 844 | /* if we found a dGPU node, then treat the whole system as dGPU */ |
| 845 | if (!props->NumCPUCores && props->NumFComputeCores) |
| 846 | hsakmt_is_dgpu = true; |
| 847 | } |
| 848 | |
| 849 | bool hsakmt_topology_is_svm_needed(HSA_ENGINE_ID EngineId) |
| 850 | { |
| 851 | if (hsakmt_is_dgpu) |
| 852 | return true; |
| 853 | |
| 854 | if (HSA_GET_GFX_VERSION_FULL(EngineId.ui32) >= GFX_VERSION_VEGA10) |
| 855 | return true; |
| 856 | |
| 857 | return false; |
| 858 | } |
| 859 | |
| 860 | static HSAKMT_STATUS topology_get_cpu_model_name(HsaNodeProperties *props, |
| 861 | struct proc_cpuinfo *cpuinfo, int num_procs) |
| 862 | { |
| 863 | int i, j; |
| 864 | |
| 865 | if (!props) { |
| 866 | pr_err("Invalid props to get cpu model name\n"); |
| 867 | return HSAKMT_STATUS_INVALID_PARAMETER; |
| 868 | } |
| 869 | |
| 870 | for (i = 0; i < num_procs; i++, cpuinfo++) { |
| 871 | if (props->CComputeIdLo == cpuinfo->apicid) { |
| 872 | if (!props->DeviceId) /* CPU-only node */ |
| 873 | strncpy((char *)props->AMDName, cpuinfo->model_name, sizeof(props->AMDName)); |
| 874 | /* Convert from UTF8 to UTF16 */ |
| 875 | for (j = 0; cpuinfo->model_name[j] != '\0' && j < HSA_PUBLIC_NAME_SIZE - 1; j++) |
| 876 | props->MarketingName[j] = cpuinfo->model_name[j]; |
| 877 | props->MarketingName[j] = '\0'; |
| 878 | return HSAKMT_STATUS_SUCCESS; |
| 879 | } |
| 880 | } |
| 881 | |
| 882 | return HSAKMT_STATUS_ERROR; |
| 883 | } |
| 884 | |
| 885 | static int topology_search_processor_vendor(const char *processor_name) |
| 886 | { |
| 887 | unsigned int i; |
| 888 | |
| 889 | for (i = 0; i < ARRAY_LEN(supported_processor_vendor_name); i++) { |
| 890 | if (!strcmp(processor_name, supported_processor_vendor_name[i])) |
| 891 | return i; |
| 892 | if (!strcmp(processor_name, "POWER9, altivec supported\n")) |
| 893 | return IBM_POWER; |
| 894 | } |
| 895 | return -1; |
| 896 | } |
| 897 | |
| 898 | /* topology_parse_cpuinfo - Parse /proc/cpuinfo and fill up required |
| 899 | * topology information |
| 900 | * cpuinfo [OUT]: output buffer to hold cpu information |
| 901 | * num_procs: number of processors the output buffer can hold |
| 902 | */ |
| 903 | static HSAKMT_STATUS topology_parse_cpuinfo(struct proc_cpuinfo *cpuinfo, |
| 904 | uint32_t num_procs) |
| 905 | { |
| 906 | HSAKMT_STATUS ret = HSAKMT_STATUS_SUCCESS; |
| 907 | FILE *fd; |
| 908 | char read_buf[256]; |
| 909 | char *p; |
| 910 | uint32_t proc = 0; |
| 911 | size_t p_len; |
| 912 | const char *proc_cpuinfo_path = "/proc/cpuinfo"; |
| 913 | |
| 914 | if (!cpuinfo) { |
| 915 | pr_err("CPU information will be missing\n"); |
| 916 | return HSAKMT_STATUS_INVALID_PARAMETER; |
| 917 | } |
| 918 | |
| 919 | fd = fopen(proc_cpuinfo_path, "r"); |
| 920 | if (!fd) { |
| 921 | pr_err("Failed to open [%s]. Unable to get CPU information", |
| 922 | proc_cpuinfo_path); |
| 923 | return HSAKMT_STATUS_ERROR; |
| 924 | } |
| 925 | |
| 926 | #ifdef __PPC64__ |
| 927 | char *p2; |
| 928 | |
| 929 | /* Each line in /proc/cpuinfo that read_buf is constructed, the format |
| 930 | * is like this: |
| 931 | * "token : value\n" |
| 932 | * where token is our target like vendor_id, model name, apicid ... |
| 933 | * and value is the answer |
| 934 | */ |
| 935 | while (fgets(read_buf, sizeof(read_buf), fd)) { |
| 936 | /* processor number */ |
| 937 | if (!strncmp("processor ", read_buf, sizeof("processor ") - 1)) { |
| 938 | p = strchr(read_buf, ':'); |
| 939 | p += 2; /* remove ": " */ |
| 940 | proc = atoi(p); |
| 941 | if (proc >= num_procs) { |
| 942 | pr_warn("cpuinfo contains processor %d larger than %u\n", |
| 943 | proc, num_procs); |
| 944 | ret = HSAKMT_STATUS_NO_MEMORY; |
| 945 | goto exit; |
| 946 | } |
| 947 | continue; |
| 948 | } |
| 949 | |
| 950 | /* vendor name / model name */ |
| 951 | if (!strncmp("cpu ", read_buf, sizeof("cpu ") - 1) && |
| 952 | (processor_vendor == -1)) { |
| 953 | p = strchr(read_buf, ':'); |
| 954 | p += 2; /* remove ": " */ |
| 955 | processor_vendor = topology_search_processor_vendor(p); |
| 956 | |
| 957 | p2 = strchr(p, ','); |
| 958 | if (p2 != NULL) { |
| 959 | p2++; |
| 960 | *p2 = 0; |
| 961 | } |
| 962 | if (strlen(p) < HSA_PUBLIC_NAME_SIZE) { |
| 963 | /* -1 to remove \n from p */ |
| 964 | strncpy(cpuinfo[proc].model_name, p, strlen(p) - 1); |
| 965 | cpuinfo[proc].model_name[strlen(p) - 1] = '\0'; |
| 966 | } else |
| 967 | strncpy(cpuinfo[proc].model_name, p, HSA_PUBLIC_NAME_SIZE); |
| 968 | continue; |
| 969 | } |
| 970 | } |
| 971 | #else |
| 972 | /* Each line in /proc/cpuinfo that read_buf is constructed, the format |
| 973 | * is like this: |
| 974 | * "token : value\n" |
| 975 | * where token is our target like vendor_id, model name, apicid ... |
| 976 | * and value is the answer |
| 977 | */ |
| 978 | while (fgets(read_buf, sizeof(read_buf), fd)) { |
| 979 | /* processor number */ |
| 980 | if (!strncmp("processor", read_buf, sizeof("processor") - 1)) { |
| 981 | p = strchr(read_buf, ':'); |
| 982 | p += 2; /* remove ": " */ |
| 983 | proc = atoi(p); |
| 984 | if (proc >= num_procs) { |
| 985 | pr_warn("cpuinfo contains processor %d larger than %u\n", |
| 986 | proc, num_procs); |
| 987 | ret = HSAKMT_STATUS_NO_MEMORY; |
| 988 | goto exit; |
| 989 | } |
| 990 | continue; |
| 991 | } |
| 992 | |
| 993 | /* vendor name */ |
| 994 | if (!strncmp("vendor_id", read_buf, sizeof("vendor_id") - 1) && |
| 995 | (processor_vendor == -1)) { |
| 996 | p = strchr(read_buf, ':'); |
| 997 | p += 2; /* remove ": " */ |
| 998 | processor_vendor = topology_search_processor_vendor(p); |
| 999 | continue; |
| 1000 | } |
| 1001 | |
| 1002 | /* model name */ |
| 1003 | if (!strncmp("model name", read_buf, sizeof("model name") - 1)) { |
| 1004 | p = strchr(read_buf, ':'); |
| 1005 | p += 2; /* remove ": " */ |
| 1006 | p_len = strlen(p); |
| 1007 | if (p_len > HSA_PUBLIC_NAME_SIZE) |
| 1008 | p_len = HSA_PUBLIC_NAME_SIZE; |
| 1009 | memcpy(cpuinfo[proc].model_name, p, p_len); |
| 1010 | cpuinfo[proc].model_name[p_len - 1] = '\0'; |
| 1011 | continue; |
| 1012 | } |
| 1013 | |
| 1014 | /* apicid */ |
| 1015 | if (!strncmp("apicid", read_buf, sizeof("apicid") - 1)) { |
| 1016 | p = strchr(read_buf, ':'); |
| 1017 | p += 2; /* remove ": " */ |
| 1018 | cpuinfo[proc].apicid = atoi(p); |
| 1019 | } |
| 1020 | } |
| 1021 | #endif |
| 1022 | |
| 1023 | if (processor_vendor < 0) { |
| 1024 | pr_err("Failed to get Processor Vendor. Setting to %s", |
| 1025 | supported_processor_vendor_name[GENUINE_INTEL]); |
| 1026 | processor_vendor = GENUINE_INTEL; |
| 1027 | } |
| 1028 | |
| 1029 | exit: |
| 1030 | fclose(fd); |
| 1031 | return ret; |
| 1032 | } |
| 1033 | |
| 1034 | static int topology_get_node_props_from_drm(HsaNodeProperties *props) |
| 1035 | { |
| 1036 | int drm_fd; |
| 1037 | uint32_t major_version; |
| 1038 | uint32_t minor_version; |
| 1039 | amdgpu_device_handle device_handle; |
| 1040 | struct amdgpu_gpu_info gpu_info; |
| 1041 | const char *name; |
| 1042 | int i, ret = 0; |
| 1043 | |
| 1044 | if (props == NULL) |
| 1045 | return -1; |
| 1046 | |
| 1047 | drm_fd = drmOpenRender(props->DrmRenderMinor); |
| 1048 | if (drm_fd < 0) |
| 1049 | return -1; |
| 1050 | |
| 1051 | if (amdgpu_device_initialize(drm_fd, |
| 1052 | &major_version, &minor_version, &device_handle) < 0) { |
| 1053 | ret = -1; |
| 1054 | goto err_device_initialize; |
| 1055 | } |
| 1056 | |
| 1057 | name = amdgpu_get_marketing_name(device_handle); |
| 1058 | if (name != NULL) { |
| 1059 | for (i = 0; name[i] != 0 && i < HSA_PUBLIC_NAME_SIZE - 1; i++) |
| 1060 | props->MarketingName[i] = name[i]; |
| 1061 | props->MarketingName[i] = '\0'; |
| 1062 | } |
| 1063 | |
| 1064 | if (amdgpu_query_gpu_info(device_handle, &gpu_info)) { |
| 1065 | ret = -1; |
| 1066 | goto err_query_gpu_info; |
| 1067 | } |
| 1068 | |
| 1069 | props->FamilyID = gpu_info.family_id; |
| 1070 | props->Integrated = !!(gpu_info.ids_flags & AMDGPU_IDS_FLAGS_FUSION); |
| 1071 | |
| 1072 | err_query_gpu_info: |
| 1073 | amdgpu_device_deinitialize(device_handle); |
| 1074 | err_device_initialize: |
| 1075 | drmClose(drm_fd); |
| 1076 | return ret; |
| 1077 | } |
| 1078 | |
| 1079 | static HSAKMT_STATUS topology_sysfs_get_node_props(uint32_t node_id, |
| 1080 | HsaNodeProperties *props, |
| 1081 | bool *p2p_links, |
| 1082 | uint32_t *num_p2pLinks) |
| 1083 | { |
| 1084 | FILE *fd; |
| 1085 | char *read_buf, *p, *envvar, dummy = '\0'; |
| 1086 | char prop_name[256]; |
| 1087 | char path[256]; |
| 1088 | char per_node_override[32]; |
| 1089 | unsigned long long prop_val = 0; |
| 1090 | uint32_t prog, major = 0, minor = 0, step = 0; |
| 1091 | int read_size; |
| 1092 | const struct hsa_gfxip_table *hsa_gfxip; |
| 1093 | uint32_t sys_node_id; |
| 1094 | uint32_t gfxv = 0; |
| 1095 | uint8_t gfxv_major, gfxv_minor, gfxv_stepping; |
| 1096 | uint32_t simd_arrays_count = 0; |
| 1097 | |
| 1098 | HSAKMT_STATUS ret = HSAKMT_STATUS_SUCCESS; |
| 1099 | |
| 1100 | assert(props); |
| 1101 | ret = topology_sysfs_map_node_id(node_id, &sys_node_id); |
| 1102 | if (ret != HSAKMT_STATUS_SUCCESS) |
| 1103 | return ret; |
| 1104 | |
| 1105 | /* Retrieve the GPU ID */ |
| 1106 | ret = topology_sysfs_get_gpu_id(sys_node_id, &props->KFDGpuID); |
| 1107 | if (ret != HSAKMT_STATUS_SUCCESS) |
| 1108 | return ret; |
| 1109 | |
| 1110 | read_buf = malloc(PAGE_SIZE); |
| 1111 | if (!read_buf) |
| 1112 | return HSAKMT_STATUS_NO_MEMORY; |
| 1113 | |
| 1114 | /* Retrieve the node properties */ |
| 1115 | snprintf(path, 256, KFD_SYSFS_PATH_NODES "/%d/properties", get_topology_dir(), sys_node_id); |
| 1116 | fd = fopen(path, "r"); |
| 1117 | if (!fd) { |
| 1118 | free(read_buf); |
| 1119 | return HSAKMT_STATUS_ERROR; |
| 1120 | } |
| 1121 | |
| 1122 | read_size = fread(read_buf, 1, PAGE_SIZE, fd); |
| 1123 | if (read_size <= 0) { |
| 1124 | ret = HSAKMT_STATUS_ERROR; |
| 1125 | goto out; |
| 1126 | } |
| 1127 | |
| 1128 | /* Since we're using the buffer as a string, we make sure the string terminates */ |
| 1129 | if (read_size >= PAGE_SIZE) |
| 1130 | read_size = PAGE_SIZE - 1; |
| 1131 | read_buf[read_size] = 0; |
| 1132 | |
| 1133 | /* Read the node properties */ |
| 1134 | prog = 0; |
| 1135 | p = read_buf; |
| 1136 | while (sscanf(p += prog, "%s %llu\n%n", prop_name, &prop_val, &prog) == 2) { |
| 1137 | if (strcmp(prop_name, "cpu_cores_count") == 0) |
| 1138 | props->NumCPUCores = (uint32_t)prop_val; |
| 1139 | else if (strcmp(prop_name, "simd_count") == 0) |
| 1140 | props->NumFComputeCores = (uint32_t)prop_val; |
| 1141 | else if (strcmp(prop_name, "mem_banks_count") == 0) |
| 1142 | props->NumMemoryBanks = (uint32_t)prop_val; |
| 1143 | else if (strcmp(prop_name, "caches_count") == 0) |
| 1144 | props->NumCaches = (uint32_t)prop_val; |
| 1145 | else if (strcmp(prop_name, "io_links_count") == 0) |
| 1146 | props->NumIOLinks = (uint32_t)prop_val; |
| 1147 | else if (strcmp(prop_name, "p2p_links_count") == 0) { |
| 1148 | props->NumIOLinks += (uint32_t)prop_val; |
| 1149 | if (num_p2pLinks) |
| 1150 | *num_p2pLinks = (uint32_t)prop_val; |
| 1151 | if (p2p_links) |
| 1152 | *p2p_links = true; |
| 1153 | } else if (strcmp(prop_name, "cpu_core_id_base") == 0) |
| 1154 | props->CComputeIdLo = (uint32_t)prop_val; |
| 1155 | else if (strcmp(prop_name, "simd_id_base") == 0) |
| 1156 | props->FComputeIdLo = (uint32_t)prop_val; |
| 1157 | else if (strcmp(prop_name, "capability") == 0) |
| 1158 | props->Capability.Value = (uint32_t)prop_val; |
| 1159 | else if (strcmp(prop_name, "capability2") == 0) |
| 1160 | props->Capability2.Value = (uint32_t)prop_val; |
| 1161 | else if (strcmp(prop_name, "debug_prop") == 0) |
| 1162 | props->DebugProperties.Value = (uint64_t)prop_val; |
| 1163 | else if (strcmp(prop_name, "max_waves_per_simd") == 0) |
| 1164 | props->MaxWavesPerSIMD = (uint32_t)prop_val; |
| 1165 | else if (strcmp(prop_name, "lds_size_in_kb") == 0) |
| 1166 | props->LDSSizeInKB = (uint32_t)prop_val; |
| 1167 | else if (strcmp(prop_name, "gds_size_in_kb") == 0) |
| 1168 | props->GDSSizeInKB = (uint32_t)prop_val; |
| 1169 | else if (strcmp(prop_name, "wave_front_size") == 0) |
| 1170 | props->WaveFrontSize = (uint32_t)prop_val; |
| 1171 | else if (strcmp(prop_name, "array_count") == 0) |
| 1172 | simd_arrays_count = (uint32_t)prop_val; |
| 1173 | else if (strcmp(prop_name, "simd_arrays_per_engine") == 0) |
| 1174 | props->NumArrays = (uint32_t)prop_val; |
| 1175 | else if (strcmp(prop_name, "cu_per_simd_array") == 0) |
| 1176 | props->NumCUPerArray = (uint32_t)prop_val; |
| 1177 | else if (strcmp(prop_name, "simd_per_cu") == 0) |
| 1178 | props->NumSIMDPerCU = (uint32_t)prop_val; |
| 1179 | else if (strcmp(prop_name, "max_slots_scratch_cu") == 0) |
| 1180 | props->MaxSlotsScratchCU = (uint32_t)prop_val; |
| 1181 | else if (strcmp(prop_name, "fw_version") == 0) |
| 1182 | props->EngineId.Value = (uint32_t)prop_val & 0x3ff; |
| 1183 | else if (strcmp(prop_name, "vendor_id") == 0) |
| 1184 | props->VendorId = (uint32_t)prop_val; |
| 1185 | else if (strcmp(prop_name, "device_id") == 0) |
| 1186 | props->DeviceId = (uint32_t)prop_val; |
| 1187 | else if (strcmp(prop_name, "location_id") == 0) |
| 1188 | props->LocationId = (uint32_t)prop_val; |
| 1189 | else if (strcmp(prop_name, "domain") == 0) |
| 1190 | props->Domain = (uint32_t)prop_val; |
| 1191 | else if (strcmp(prop_name, "max_engine_clk_fcompute") == 0) |
| 1192 | props->MaxEngineClockMhzFCompute = (uint32_t)prop_val; |
| 1193 | else if (strcmp(prop_name, "max_engine_clk_ccompute") == 0) |
| 1194 | props->MaxEngineClockMhzCCompute = (uint32_t)prop_val; |
| 1195 | else if (strcmp(prop_name, "local_mem_size") == 0) |
| 1196 | props->LocalMemSize = prop_val; |
| 1197 | else if (strcmp(prop_name, "drm_render_minor") == 0) |
| 1198 | props->DrmRenderMinor = (int32_t)prop_val; |
| 1199 | else if (strcmp(prop_name, "sdma_fw_version") == 0) |
| 1200 | props->uCodeEngineVersions.Value = (uint32_t)prop_val & 0x3ff; |
| 1201 | else if (strcmp(prop_name, "hive_id") == 0) |
| 1202 | props->HiveID = prop_val; |
| 1203 | else if (strcmp(prop_name, "unique_id") == 0) |
| 1204 | props->UniqueID = prop_val; |
| 1205 | else if (strcmp(prop_name, "num_sdma_engines") == 0) |
| 1206 | props->NumSdmaEngines = prop_val; |
| 1207 | else if (strcmp(prop_name, "num_sdma_xgmi_engines") == 0) |
| 1208 | props->NumSdmaXgmiEngines = prop_val; |
| 1209 | else if (strcmp(prop_name, "num_gws") == 0) |
| 1210 | props->NumGws = prop_val; |
| 1211 | else if (strcmp(prop_name, "num_sdma_queues_per_engine") == 0) |
| 1212 | props->NumSdmaQueuesPerEngine = prop_val; |
| 1213 | else if (strcmp(prop_name, "num_cp_queues") == 0) |
| 1214 | props->NumCpQueues = prop_val; |
| 1215 | else if (strcmp(prop_name, "cwsr_size") == 0) |
| 1216 | props->CwsrSize = prop_val; |
| 1217 | else if (strcmp(prop_name, "ctl_stack_size") == 0) |
| 1218 | props->CtlStackSize = prop_val; |
| 1219 | else if (strcmp(prop_name, "num_xcc") == 0) |
| 1220 | props->NumXcc = prop_val; |
| 1221 | else if (strcmp(prop_name, "family_id") == 0) |
| 1222 | props->FamilyID = prop_val; |
| 1223 | else if (strcmp(prop_name, "gfx_target_version") == 0) |
| 1224 | gfxv = (uint32_t)prop_val; |
| 1225 | } |
| 1226 | |
| 1227 | if (!hsakmt_is_svm_api_supported) |
| 1228 | props->Capability.ui32.SVMAPISupported = 0; |
| 1229 | |
| 1230 | /* Bail out early, if a CPU node */ |
| 1231 | if (!props->NumFComputeCores) |
| 1232 | goto out; |
| 1233 | |
| 1234 | if (props->NumArrays != 0) |
| 1235 | props->NumShaderBanks = simd_arrays_count/props->NumArrays; |
| 1236 | |
| 1237 | gfxv_major = HSA_GET_GFX_VERSION_MAJOR(gfxv); |
| 1238 | gfxv_minor = HSA_GET_GFX_VERSION_MINOR(gfxv); |
| 1239 | gfxv_stepping = HSA_GET_GFX_VERSION_STEP(gfxv); |
| 1240 | |
| 1241 | hsa_gfxip = find_hsa_gfxip_device(props->DeviceId, gfxv_major); |
| 1242 | if (hsa_gfxip || gfxv) { |
| 1243 | snprintf(per_node_override, sizeof(per_node_override), "HSA_OVERRIDE_GFX_VERSION_%d", node_id); |
| 1244 | if ((envvar = getenv(per_node_override)) || (envvar = getenv("HSA_OVERRIDE_GFX_VERSION"))) { |
| 1245 | /* HSA_OVERRIDE_GFX_VERSION=major.minor.stepping */ |
| 1246 | if ((sscanf(envvar, "%u.%u.%u%c", |
| 1247 | &major, &minor, &step, &dummy) != 3) || |
| 1248 | (major > 63 || minor > 255 || step > 255)) { |
| 1249 | pr_err("HSA_OVERRIDE_GFX_VERSION %s is invalid\n", |
| 1250 | envvar); |
| 1251 | ret = HSAKMT_STATUS_ERROR; |
| 1252 | goto out; |
| 1253 | } |
| 1254 | props->OverrideEngineId.ui32.Major = major & 0x3f; |
| 1255 | props->OverrideEngineId.ui32.Minor = minor & 0xff; |
| 1256 | props->OverrideEngineId.ui32.Stepping = step & 0xff; |
| 1257 | } |
| 1258 | |
| 1259 | if (hsa_gfxip) { |
| 1260 | props->EngineId.ui32.Major = hsa_gfxip->major & 0x3f; |
| 1261 | props->EngineId.ui32.Minor = hsa_gfxip->minor & 0xff; |
| 1262 | props->EngineId.ui32.Stepping = hsa_gfxip->stepping & 0xff; |
| 1263 | } else { |
| 1264 | props->EngineId.ui32.Major = gfxv_major & 0x3f; |
| 1265 | props->EngineId.ui32.Minor = gfxv_minor & 0xff; |
| 1266 | props->EngineId.ui32.Stepping = gfxv_stepping & 0xff; |
| 1267 | } |
| 1268 | |
| 1269 | /* Set the CAL name of the node. If DID-based hsa_gfxip lookup was |
| 1270 | * successful, use that name. Otherwise, set to GFX<GFX_VERSION>. |
| 1271 | */ |
| 1272 | if (hsa_gfxip && hsa_gfxip->amd_name) |
| 1273 | strncpy((char *)props->AMDName, hsa_gfxip->amd_name, |
| 1274 | sizeof(props->AMDName)-1); |
| 1275 | else |
| 1276 | snprintf((char *)props->AMDName, sizeof(props->AMDName)-1, "GFX%06x", |
| 1277 | HSA_GET_GFX_VERSION_FULL(props->EngineId.ui32)); |
| 1278 | |
| 1279 | /* Is dGPU Node, not APU |
| 1280 | * Retrieve the marketing name of the node. |
| 1281 | */ |
| 1282 | if (topology_get_node_props_from_drm(props)) |
| 1283 | pr_info("failed to get marketing name for device ID 0x%x\n", props->DeviceId); |
| 1284 | |
| 1285 | /* Get VGPR/SGPR size in byte per CU */ |
| 1286 | props->SGPRSizePerCU = SGPR_SIZE_PER_CU; |
| 1287 | props->VGPRSizePerCU = hsakmt_get_vgpr_size_per_cu(HSA_GET_GFX_VERSION_FULL(props->EngineId.ui32)); |
| 1288 | |
| 1289 | } else if (props->DeviceId) |
| 1290 | /* still return success */ |
| 1291 | pr_err("device ID 0x%x is not supported in libhsakmt\n", |
| 1292 | props->DeviceId); |
| 1293 | |
| 1294 | if (props->NumFComputeCores) |
| 1295 | assert(props->EngineId.ui32.Major && "HSA_OVERRIDE_GFX_VERSION may be needed"); |
| 1296 | |
| 1297 | /* On Older kernels, num_xcc may not be present in system properties. |
| 1298 | * Set it to 1 if system properties do not report num_xcc. |
| 1299 | */ |
| 1300 | if (!props->NumXcc) |
| 1301 | props->NumXcc = 1; |
| 1302 | |
| 1303 | out: |
| 1304 | free(read_buf); |
| 1305 | fclose(fd); |
| 1306 | return ret; |
| 1307 | } |
| 1308 | |
| 1309 | static HSAKMT_STATUS topology_sysfs_get_mem_props(uint32_t node_id, |
| 1310 | uint32_t mem_id, |
| 1311 | HsaMemoryProperties *props) |
| 1312 | { |
| 1313 | FILE *fd; |
| 1314 | char *read_buf, *p; |
| 1315 | char prop_name[256]; |
| 1316 | char path[256]; |
| 1317 | unsigned long long prop_val; |
| 1318 | uint32_t prog; |
| 1319 | int read_size; |
| 1320 | HSAKMT_STATUS ret = HSAKMT_STATUS_SUCCESS; |
| 1321 | uint32_t sys_node_id; |
| 1322 | |
| 1323 | assert(props); |
| 1324 | ret = topology_sysfs_map_node_id(node_id, &sys_node_id); |
| 1325 | if (ret != HSAKMT_STATUS_SUCCESS) |
| 1326 | return ret; |
| 1327 | |
| 1328 | snprintf(path, 256, KFD_SYSFS_PATH_NODES "/%d/mem_banks/%d/properties", get_topology_dir(), sys_node_id, mem_id); |
| 1329 | fd = fopen(path, "r"); |
| 1330 | if (!fd) |
| 1331 | return HSAKMT_STATUS_ERROR; |
| 1332 | read_buf = malloc(PAGE_SIZE); |
| 1333 | if (!read_buf) { |
| 1334 | ret = HSAKMT_STATUS_NO_MEMORY; |
| 1335 | goto err1; |
| 1336 | } |
| 1337 | |
| 1338 | read_size = fread(read_buf, 1, PAGE_SIZE, fd); |
| 1339 | if (read_size <= 0) { |
| 1340 | ret = HSAKMT_STATUS_ERROR; |
| 1341 | goto err2; |
| 1342 | } |
| 1343 | |
| 1344 | /* Since we're using the buffer as a string, we make sure the string terminates */ |
| 1345 | if (read_size >= PAGE_SIZE) |
| 1346 | read_size = PAGE_SIZE - 1; |
| 1347 | read_buf[read_size] = 0; |
| 1348 | |
| 1349 | prog = 0; |
| 1350 | p = read_buf; |
| 1351 | while (sscanf(p += prog, "%s %llu\n%n", prop_name, &prop_val, &prog) == 2) { |
| 1352 | if (strcmp(prop_name, "heap_type") == 0) |
| 1353 | props->HeapType = (uint32_t)prop_val; |
| 1354 | else if (strcmp(prop_name, "size_in_bytes") == 0) |
| 1355 | props->SizeInBytes = (uint64_t)prop_val; |
| 1356 | else if (strcmp(prop_name, "flags") == 0) |
| 1357 | props->Flags.MemoryProperty = (uint32_t)prop_val; |
| 1358 | else if (strcmp(prop_name, "width") == 0) |
| 1359 | props->Width = (uint32_t)prop_val; |
| 1360 | else if (strcmp(prop_name, "mem_clk_max") == 0) |
| 1361 | props->MemoryClockMax = (uint32_t)prop_val; |
| 1362 | } |
| 1363 | |
| 1364 | err2: |
| 1365 | free(read_buf); |
| 1366 | err1: |
| 1367 | fclose(fd); |
| 1368 | return ret; |
| 1369 | } |
| 1370 | |
| 1371 | /* topology_destroy_temp_cpu_cache_list - |
| 1372 | * Free the memory allocated in topology_create_temp_cpu_cache_list(). |
| 1373 | */ |
| 1374 | static void topology_destroy_temp_cpu_cache_list( |
| 1375 | cpu_cacheinfo_t *temp_cpu_ci_list) |
| 1376 | { |
| 1377 | uint32_t n; |
| 1378 | cpu_cacheinfo_t *p_temp_cpu_ci_list = temp_cpu_ci_list; |
| 1379 | cpu_cacheinfo_t *cpu_ci = p_temp_cpu_ci_list; |
| 1380 | |
| 1381 | if (p_temp_cpu_ci_list) { |
| 1382 | for (n = 0; n < p_temp_cpu_ci_list->len; n++, cpu_ci++) |
| 1383 | free(cpu_ci->cache_prop); |
| 1384 | free(p_temp_cpu_ci_list); |
| 1385 | } |
| 1386 | |
| 1387 | } |
| 1388 | |
| 1389 | /* topology_create_temp_cpu_cache_list - Create a temporary cpu-cache list to |
| 1390 | * store cpu cache information. This list will be used to copy |
| 1391 | * HsaCacheProperties in the CPU node. Two buffers are allocated |
| 1392 | * inside this function: cpu_ci list and cache_prop under each |
| 1393 | * cpu_ci. Must call topology_destroy_temp_cpu_cache_list to free |
| 1394 | * the memory after the information is copied. |
| 1395 | * @node [IN] CPU node number |
| 1396 | * @cpuinfo [IN] /proc/cpuinfo data |
| 1397 | * @temp_cpu_ci_list [OUT] cpu-cache-info list with data filled |
| 1398 | * Return: total number of caches under this CPU node |
| 1399 | */ |
| 1400 | static int topology_create_temp_cpu_cache_list(int node, |
| 1401 | struct proc_cpuinfo *cpuinfo, cpu_cacheinfo_t **temp_cpu_ci_list) |
| 1402 | { |
| 1403 | /* Get max path size from /sys/devices/system/node/node%d/%s/cache |
| 1404 | * below, which will max out according to the largest filename, |
| 1405 | * which can be present twice in the string above. 29 is for the prefix |
| 1406 | * and the +6 is for the cache suffix |
| 1407 | */ |
| 1408 | #ifndef MAXNAMLEN |
| 1409 | /* MAXNAMLEN is the BSD name for NAME_MAX. glibc aliases this as NAME_MAX, but not musl */ |
| 1410 | #define MAXNAMLEN NAME_MAX |
| 1411 | #endif |
| 1412 | const uint32_t MAXPATHSIZE = 29 + MAXNAMLEN + (MAXNAMLEN + 6); |
| 1413 | cpu_cacheinfo_t *p_temp_cpu_ci_list; /* a list of cpu_ci */ |
| 1414 | char path[MAXPATHSIZE], node_dir[MAXPATHSIZE]; |
| 1415 | int max_cpus; |
| 1416 | cpu_cacheinfo_t *this_cpu; /* one cpu_ci in cpu_ci_list */ |
| 1417 | int cache_cnt = 0; |
| 1418 | DIR *dirp = NULL; |
| 1419 | struct dirent *dir; |
| 1420 | char *p; |
| 1421 | |
| 1422 | if (!temp_cpu_ci_list) { |
| 1423 | pr_err("Invalid temp_cpu_ci_list\n"); |
| 1424 | goto exit; |
| 1425 | } |
| 1426 | *temp_cpu_ci_list = NULL; |
| 1427 | |
| 1428 | /* Get info from /sys/devices/system/node/nodeX/cpuY/cache */ |
| 1429 | int node_real = node; |
| 1430 | if (processor_vendor == IBM_POWER) { |
| 1431 | if (!strcmp(cpuinfo[0].model_name, "POWER9")) { |
| 1432 | node_real = node * 8; |
| 1433 | } |
| 1434 | } |
| 1435 | snprintf(node_dir, MAXPATHSIZE, "/sys/devices/system/node/node%d", node_real); |
| 1436 | /* Other than cpuY folders, this dir also has cpulist and cpumap */ |
| 1437 | max_cpus = num_subdirs(node_dir, "cpu"); |
| 1438 | if (max_cpus <= 0) { |
| 1439 | /* If CONFIG_NUMA is not enabled in the kernel, |
| 1440 | * /sys/devices/system/node doesn't exist. |
| 1441 | */ |
| 1442 | if (node) { /* CPU node must be 0 or something is wrong */ |
| 1443 | pr_err("Fail to get cpu* dirs under %s.", node_dir); |
| 1444 | goto exit; |
| 1445 | } |
| 1446 | /* Fall back to use /sys/devices/system/cpu */ |
| 1447 | snprintf(node_dir, MAXPATHSIZE, "/sys/devices/system/cpu"); |
| 1448 | max_cpus = num_subdirs(node_dir, "cpu"); |
| 1449 | if (max_cpus <= 0) { |
| 1450 | pr_err("Fail to get cpu* dirs under %s\n", node_dir); |
| 1451 | goto exit; |
| 1452 | } |
| 1453 | } |
| 1454 | |
| 1455 | p_temp_cpu_ci_list = calloc(max_cpus, sizeof(cpu_cacheinfo_t)); |
| 1456 | if (!p_temp_cpu_ci_list) { |
| 1457 | pr_err("Fail to allocate p_temp_cpu_ci_list\n"); |
| 1458 | goto exit; |
| 1459 | } |
| 1460 | p_temp_cpu_ci_list->len = 0; |
| 1461 | |
| 1462 | this_cpu = p_temp_cpu_ci_list; |
| 1463 | dirp = opendir(node_dir); |
| 1464 | while ((dir = readdir(dirp)) != 0) { |
| 1465 | if (strncmp(dir->d_name, "cpu", 3)) |
| 1466 | continue; |
| 1467 | if (!isdigit(dir->d_name[3])) /* ignore files like cpulist */ |
| 1468 | continue; |
| 1469 | snprintf(path, MAXPATHSIZE, "%s/%s/cache", node_dir, dir->d_name); |
| 1470 | this_cpu->num_caches = num_subdirs(path, "index"); |
| 1471 | this_cpu->cache_prop = calloc(this_cpu->num_caches, |
| 1472 | sizeof(HsaCacheProperties)); |
| 1473 | if (!this_cpu->cache_prop) { |
| 1474 | pr_err("Fail to allocate cache_info\n"); |
| 1475 | goto exit; |
| 1476 | } |
| 1477 | p = &dir->d_name[3]; |
| 1478 | this_cpu->proc_num = atoi(p); |
| 1479 | cache_cnt += get_cpu_cache_info(path, cpuinfo, this_cpu); |
| 1480 | ++p_temp_cpu_ci_list->len; |
| 1481 | ++this_cpu; |
| 1482 | } |
| 1483 | *temp_cpu_ci_list = p_temp_cpu_ci_list; |
| 1484 | |
| 1485 | exit: |
| 1486 | if (dirp) |
| 1487 | closedir(dirp); |
| 1488 | return cache_cnt; |
| 1489 | } |
| 1490 | |
| 1491 | /* topology_get_cpu_cache_props - Read CPU cache information from sysfs |
| 1492 | * @node [IN] CPU node number |
| 1493 | * @cpuinfo [IN] /proc/cpuinfo data |
| 1494 | * @tbl [OUT] the node table to fill up |
| 1495 | * Return: HSAKMT_STATUS_SUCCESS in success or error number in failure |
| 1496 | */ |
| 1497 | static HSAKMT_STATUS topology_get_cpu_cache_props(int node, |
| 1498 | struct proc_cpuinfo *cpuinfo, node_props_t *tbl) |
| 1499 | { |
| 1500 | HSAKMT_STATUS ret = HSAKMT_STATUS_SUCCESS; |
| 1501 | cpu_cacheinfo_t *cpu_ci_list = NULL; |
| 1502 | uint32_t n, cache_cnt, i; |
| 1503 | cpu_cacheinfo_t *cpu_ci; |
| 1504 | HsaCacheProperties *this_cache; |
| 1505 | |
| 1506 | tbl->node.NumCaches = topology_create_temp_cpu_cache_list( |
| 1507 | node, cpuinfo, &cpu_ci_list); |
| 1508 | if (!tbl->node.NumCaches) { |
| 1509 | /* For "Intel Meteor lake Mobile", the cache info is not in sysfs, |
| 1510 | * That means /sys/devices/system/node/node%d/%s/cache is not exist. |
| 1511 | * here AMD will not black this issue. |
| 1512 | */ |
| 1513 | pr_debug("CPU cache info is not available for node %d \n", node); |
| 1514 | goto exit; |
| 1515 | } |
| 1516 | |
| 1517 | tbl->cache = calloc(tbl->node.NumCaches, sizeof(HsaCacheProperties)); |
| 1518 | if (!tbl->cache) { |
| 1519 | ret = HSAKMT_STATUS_NO_MEMORY; |
| 1520 | goto exit; |
| 1521 | } |
| 1522 | |
| 1523 | /* Now fill in the information to cache properties. */ |
| 1524 | cache_cnt = 0; |
| 1525 | cpu_ci = cpu_ci_list; |
| 1526 | for (n = 0; n < cpu_ci_list->len; n++, cpu_ci++) { |
| 1527 | this_cache = cpu_ci->cache_prop; |
| 1528 | for (i = 0; i < cpu_ci->num_caches; i++, this_cache++) { |
| 1529 | memcpy(&tbl->cache[cache_cnt++], |
| 1530 | this_cache, |
| 1531 | sizeof(HsaCacheProperties)); |
| 1532 | if (cache_cnt >= tbl->node.NumCaches) |
| 1533 | goto exit; |
| 1534 | } |
| 1535 | } |
| 1536 | |
| 1537 | exit: |
| 1538 | topology_destroy_temp_cpu_cache_list(cpu_ci_list); |
| 1539 | |
| 1540 | return ret; |
| 1541 | } |
| 1542 | |
| 1543 | static HSAKMT_STATUS topology_sysfs_get_cache_props(uint32_t node_id, |
| 1544 | uint32_t cache_id, |
| 1545 | HsaCacheProperties *props) |
| 1546 | { |
| 1547 | FILE *fd; |
| 1548 | char *read_buf, *p; |
| 1549 | char prop_name[256]; |
| 1550 | char path[256]; |
| 1551 | unsigned long long prop_val; |
| 1552 | uint32_t i, prog; |
| 1553 | int read_size; |
| 1554 | HSAKMT_STATUS ret = HSAKMT_STATUS_SUCCESS; |
| 1555 | uint32_t sys_node_id; |
| 1556 | |
| 1557 | assert(props); |
| 1558 | ret = topology_sysfs_map_node_id(node_id, &sys_node_id); |
| 1559 | if (ret != HSAKMT_STATUS_SUCCESS) |
| 1560 | return ret; |
| 1561 | |
| 1562 | snprintf(path, 256, KFD_SYSFS_PATH_NODES "/%d/caches/%d/properties", get_topology_dir(), sys_node_id, cache_id); |
| 1563 | fd = fopen(path, "r"); |
| 1564 | if (!fd) |
| 1565 | return HSAKMT_STATUS_ERROR; |
| 1566 | read_buf = malloc(PAGE_SIZE); |
| 1567 | if (!read_buf) { |
| 1568 | ret = HSAKMT_STATUS_NO_MEMORY; |
| 1569 | goto err1; |
| 1570 | } |
| 1571 | |
| 1572 | read_size = fread(read_buf, 1, PAGE_SIZE, fd); |
| 1573 | if (read_size <= 0) { |
| 1574 | ret = HSAKMT_STATUS_ERROR; |
| 1575 | goto err2; |
| 1576 | } |
| 1577 | |
| 1578 | /* Since we're using the buffer as a string, we make sure the string terminates */ |
| 1579 | if (read_size >= PAGE_SIZE) |
| 1580 | read_size = PAGE_SIZE - 1; |
| 1581 | read_buf[read_size] = 0; |
| 1582 | |
| 1583 | prog = 0; |
| 1584 | p = read_buf; |
| 1585 | while (sscanf(p += prog, "%s %llu\n%n", prop_name, &prop_val, &prog) == 2) { |
| 1586 | if (strcmp(prop_name, "processor_id_low") == 0) |
| 1587 | props->ProcessorIdLow = (uint32_t)prop_val; |
| 1588 | else if (strcmp(prop_name, "level") == 0) |
| 1589 | props->CacheLevel = (uint32_t)prop_val; |
| 1590 | else if (strcmp(prop_name, "size") == 0) |
| 1591 | props->CacheSize = (uint32_t)prop_val; |
| 1592 | else if (strcmp(prop_name, "cache_line_size") == 0) |
| 1593 | props->CacheLineSize = (uint32_t)prop_val; |
| 1594 | else if (strcmp(prop_name, "cache_lines_per_tag") == 0) |
| 1595 | props->CacheLinesPerTag = (uint32_t)prop_val; |
| 1596 | else if (strcmp(prop_name, "association") == 0) |
| 1597 | props->CacheAssociativity = (uint32_t)prop_val; |
| 1598 | else if (strcmp(prop_name, "latency") == 0) |
| 1599 | props->CacheLatency = (uint32_t)prop_val; |
| 1600 | else if (strcmp(prop_name, "type") == 0) |
| 1601 | props->CacheType.Value = (uint32_t)prop_val; |
| 1602 | else if (strcmp(prop_name, "sibling_map") == 0) |
| 1603 | break; |
| 1604 | } |
| 1605 | |
| 1606 | prog = 0; |
| 1607 | if ((sscanf(p, "sibling_map %n", &prog)) == 0 && prog) { |
| 1608 | i = 0; |
| 1609 | while ((i < HSA_CPU_SIBLINGS) && |
| 1610 | (sscanf(p += prog, "%u%*[,\n]%n", &props->SiblingMap[i++], &prog) == 1)) |
| 1611 | continue; |
| 1612 | } |
| 1613 | |
| 1614 | err2: |
| 1615 | free(read_buf); |
| 1616 | err1: |
| 1617 | fclose(fd); |
| 1618 | return ret; |
| 1619 | } |
| 1620 | |
| 1621 | static HSAKMT_STATUS topology_map_sysfs_to_user_node_id(uint32_t sys_node_id, uint32_t *user_node_id) |
| 1622 | { |
| 1623 | uint32_t node_id; |
| 1624 | |
| 1625 | for (node_id = 0; node_id < map_user_to_sysfs_node_id_size; node_id++) |
| 1626 | if (map_user_to_sysfs_node_id[node_id] == sys_node_id) { |
| 1627 | *user_node_id = node_id; |
| 1628 | return HSAKMT_STATUS_SUCCESS; |
| 1629 | } |
| 1630 | return HSAKMT_STATUS_INVALID_NODE_UNIT; |
| 1631 | } |
| 1632 | |
| 1633 | |
| 1634 | /* For a give Node @node_id the function gets @iolink_id information i.e. parses sysfs the following sysfs entry |
| 1635 | * ./nodes/@node_id/io_links/@iolink_id/properties. @node_id has to be valid accessible node. |
| 1636 | * |
| 1637 | * If node_to specified by the @iolink_id is not accessible the function returns HSAKMT_STATUS_NOT_SUPPORTED. |
| 1638 | * If node_to is accessible, then node_to is mapped from sysfs_node to user_node and returns HSAKMT_STATUS_SUCCESS. |
| 1639 | */ |
| 1640 | static HSAKMT_STATUS topology_sysfs_get_iolink_props(HsaKFDContext *ctx, |
| 1641 | uint32_t node_id, |
| 1642 | uint32_t iolink_id, |
| 1643 | HsaIoLinkProperties *props, bool p2pLink) |
| 1644 | { |
| 1645 | FILE *fd; |
| 1646 | char *read_buf, *p; |
| 1647 | char prop_name[256]; |
| 1648 | char path[256]; |
| 1649 | unsigned long long prop_val; |
| 1650 | uint32_t prog; |
| 1651 | int read_size; |
| 1652 | HSAKMT_STATUS ret = HSAKMT_STATUS_SUCCESS; |
| 1653 | uint32_t sys_node_id; |
| 1654 | |
| 1655 | assert(props); |
| 1656 | ret = topology_sysfs_map_node_id(node_id, &sys_node_id); |
| 1657 | if (ret != HSAKMT_STATUS_SUCCESS) |
| 1658 | return ret; |
| 1659 | |
| 1660 | snprintf(path, 256, KFD_SYSFS_PATH_NODES "/%d/%s/%d/properties", get_topology_dir(), sys_node_id, p2pLink ? "p2p_links" : "io_links", iolink_id); |
| 1661 | |
| 1662 | fd = fopen(path, "r"); |
| 1663 | if (!fd) |
| 1664 | return HSAKMT_STATUS_ERROR; |
| 1665 | read_buf = malloc(PAGE_SIZE); |
| 1666 | if (!read_buf) { |
| 1667 | ret = HSAKMT_STATUS_NO_MEMORY; |
| 1668 | goto err1; |
| 1669 | } |
| 1670 | |
| 1671 | read_size = fread(read_buf, 1, PAGE_SIZE, fd); |
| 1672 | if (read_size <= 0) { |
| 1673 | ret = (errno == EPERM) ? HSAKMT_STATUS_NOT_SUPPORTED : |
| 1674 | HSAKMT_STATUS_ERROR; |
| 1675 | goto err2; |
| 1676 | } |
| 1677 | |
| 1678 | /* Since we're using the buffer as a string, we make sure the string terminates */ |
| 1679 | if (read_size >= PAGE_SIZE) |
| 1680 | read_size = PAGE_SIZE - 1; |
| 1681 | read_buf[read_size] = 0; |
| 1682 | |
| 1683 | prog = 0; |
| 1684 | p = read_buf; |
| 1685 | while (sscanf(p += prog, "%s %llu\n%n", prop_name, &prop_val, &prog) == 2) { |
| 1686 | if (strcmp(prop_name, "type") == 0) |
| 1687 | props->IoLinkType = (uint32_t)prop_val; |
| 1688 | else if (strcmp(prop_name, "version_major") == 0) |
| 1689 | props->VersionMajor = (uint32_t)prop_val; |
| 1690 | else if (strcmp(prop_name, "version_minor") == 0) |
| 1691 | props->VersionMinor = (uint32_t)prop_val; |
| 1692 | else if (strcmp(prop_name, "node_from") == 0) { |
| 1693 | if (sys_node_id != (uint32_t)prop_val) { |
| 1694 | ret = HSAKMT_STATUS_INVALID_NODE_UNIT; |
| 1695 | goto err2; |
| 1696 | } |
| 1697 | props->NodeFrom = node_id; |
| 1698 | } else if (strcmp(prop_name, "node_to") == 0) { |
| 1699 | bool is_node_supported; |
| 1700 | uint32_t sysfs_node_id; |
| 1701 | |
| 1702 | sysfs_node_id = (uint32_t)prop_val; |
| 1703 | ret = topology_sysfs_check_node_supported(ctx, sysfs_node_id, &is_node_supported); |
| 1704 | if (!is_node_supported) { |
| 1705 | ret = HSAKMT_STATUS_NOT_SUPPORTED; |
| 1706 | memset(props, 0, sizeof(*props)); |
| 1707 | goto err2; |
| 1708 | } |
| 1709 | ret = topology_map_sysfs_to_user_node_id(sysfs_node_id, &props->NodeTo); |
| 1710 | if (ret != HSAKMT_STATUS_SUCCESS) |
| 1711 | goto err2; |
| 1712 | } else if (strcmp(prop_name, "weight") == 0) |
| 1713 | props->Weight = (uint32_t)prop_val; |
| 1714 | else if (strcmp(prop_name, "min_latency") == 0) |
| 1715 | props->MinimumLatency = (uint32_t)prop_val; |
| 1716 | else if (strcmp(prop_name, "max_latency") == 0) |
| 1717 | props->MaximumLatency = (uint32_t)prop_val; |
| 1718 | else if (strcmp(prop_name, "min_bandwidth") == 0) |
| 1719 | props->MinimumBandwidth = (uint32_t)prop_val; |
| 1720 | else if (strcmp(prop_name, "max_bandwidth") == 0) |
| 1721 | props->MaximumBandwidth = (uint32_t)prop_val; |
| 1722 | else if (strcmp(prop_name, "recommended_transfer_size") == 0) |
| 1723 | props->RecTransferSize = (uint32_t)prop_val; |
| 1724 | else if (strcmp(prop_name, "recommended_sdma_engine_id_mask") == 0) |
| 1725 | props->RecSdmaEngIdMask = (uint32_t)prop_val; |
| 1726 | else if (strcmp(prop_name, "flags") == 0) |
| 1727 | props->Flags.LinkProperty = (uint32_t)prop_val; |
| 1728 | } |
| 1729 | |
| 1730 | |
| 1731 | err2: |
| 1732 | free(read_buf); |
| 1733 | err1: |
| 1734 | fclose(fd); |
| 1735 | return ret; |
| 1736 | } |
| 1737 | |
| 1738 | /* topology_get_free_io_link_slot_for_node - For the given node_id, find the |
| 1739 | * next available free slot to add an io_link |
| 1740 | */ |
| 1741 | static HsaIoLinkProperties *topology_get_free_io_link_slot_for_node(uint32_t node_id, |
| 1742 | const HsaSystemProperties *sys_props, |
| 1743 | node_props_t *node_props) |
| 1744 | { |
| 1745 | HsaIoLinkProperties *props; |
| 1746 | |
| 1747 | if (node_id >= sys_props->NumNodes) { |
| 1748 | pr_err("Invalid node [%d]\n", node_id); |
| 1749 | return NULL; |
| 1750 | } |
| 1751 | |
| 1752 | props = node_props[node_id].link; |
| 1753 | if (!props) { |
| 1754 | pr_err("No io_link reported for Node [%d]\n", node_id); |
| 1755 | return NULL; |
| 1756 | } |
| 1757 | |
| 1758 | if (node_props[node_id].node.NumIOLinks >= sys_props->NumNodes - 1) { |
| 1759 | pr_err("No more space for io_link for Node [%d]\n", node_id); |
| 1760 | return NULL; |
| 1761 | } |
| 1762 | |
| 1763 | return &props[node_props[node_id].node.NumIOLinks]; |
| 1764 | } |
| 1765 | |
| 1766 | /* topology_add_io_link_for_node - If a free slot is available, |
| 1767 | * add io_link for the given Node. |
| 1768 | * TODO: Add other members of HsaIoLinkProperties |
| 1769 | */ |
| 1770 | static HSAKMT_STATUS topology_add_io_link_for_node(uint32_t node_from, |
| 1771 | const HsaSystemProperties *sys_props, |
| 1772 | node_props_t *node_props, |
| 1773 | HSA_IOLINKTYPE IoLinkType, |
| 1774 | uint32_t node_to, |
| 1775 | uint32_t Weight) |
| 1776 | { |
| 1777 | HsaIoLinkProperties *props; |
| 1778 | |
| 1779 | props = topology_get_free_io_link_slot_for_node(node_from, |
| 1780 | sys_props, node_props); |
| 1781 | if (!props) |
| 1782 | return HSAKMT_STATUS_NO_MEMORY; |
| 1783 | |
| 1784 | props->IoLinkType = IoLinkType; |
| 1785 | props->NodeFrom = node_from; |
| 1786 | props->NodeTo = node_to; |
| 1787 | props->Weight = Weight; |
| 1788 | node_props[node_from].node.NumIOLinks++; |
| 1789 | |
| 1790 | return HSAKMT_STATUS_SUCCESS; |
| 1791 | } |
| 1792 | |
| 1793 | /* Find the CPU that this GPU (gpu_node) directly connects to */ |
| 1794 | static int32_t gpu_get_direct_link_cpu(uint32_t gpu_node, node_props_t *node_props) |
| 1795 | { |
| 1796 | HsaIoLinkProperties *props = node_props[gpu_node].link; |
| 1797 | uint32_t i; |
| 1798 | |
| 1799 | if (!node_props[gpu_node].node.KFDGpuID || !props || |
| 1800 | node_props[gpu_node].node.NumIOLinks == 0) |
| 1801 | return -1; |
| 1802 | |
| 1803 | for (i = 0; i < node_props[gpu_node].node.NumIOLinks; i++) |
| 1804 | if ((props[i].IoLinkType == HSA_IOLINKTYPE_PCIEXPRESS || props[i].IoLinkType == HSA_IOLINK_TYPE_XGMI) && |
| 1805 | props[i].Weight <= 20) /* >20 is GPU->CPU->GPU */{ |
| 1806 | if (!node_props[props[i].NodeTo].node.KFDGpuID) |
| 1807 | return props[i].NodeTo; |
| 1808 | } |
| 1809 | |
| 1810 | return -1; |
| 1811 | } |
| 1812 | |
| 1813 | /* Get node1->node2 IO link information. This should be a direct link that has |
| 1814 | * been created in the kernel. |
| 1815 | */ |
| 1816 | static HSAKMT_STATUS get_direct_iolink_info(uint32_t node1, uint32_t node2, |
| 1817 | node_props_t *node_props, HSAuint32 *weight, |
| 1818 | HSA_IOLINKTYPE *type) |
| 1819 | { |
| 1820 | HsaIoLinkProperties *props = node_props[node1].link; |
| 1821 | uint32_t i; |
| 1822 | |
| 1823 | if (!props) |
| 1824 | return HSAKMT_STATUS_INVALID_NODE_UNIT; |
| 1825 | |
| 1826 | for (i = 0; i < node_props[node1].node.NumIOLinks; i++) |
| 1827 | if (props[i].NodeTo == node2) { |
| 1828 | if (weight) |
| 1829 | *weight = props[i].Weight; |
| 1830 | if (type) |
| 1831 | *type = props[i].IoLinkType; |
| 1832 | return HSAKMT_STATUS_SUCCESS; |
| 1833 | } |
| 1834 | |
| 1835 | return HSAKMT_STATUS_INVALID_PARAMETER; |
| 1836 | } |
| 1837 | |
| 1838 | static HSAKMT_STATUS get_indirect_iolink_info(uint32_t node1, uint32_t node2, |
| 1839 | node_props_t *node_props, HSAuint32 *weight, |
| 1840 | HSA_IOLINKTYPE *type) |
| 1841 | { |
| 1842 | int32_t dir_cpu1 = -1, dir_cpu2 = -1; |
| 1843 | HSAuint32 weight1 = 0, weight2 = 0, weight3 = 0; |
| 1844 | HSAKMT_STATUS ret; |
| 1845 | uint32_t i; |
| 1846 | |
| 1847 | *weight = 0; |
| 1848 | *type = HSA_IOLINKTYPE_UNDEFINED; |
| 1849 | |
| 1850 | if (node1 == node2) |
| 1851 | return HSAKMT_STATUS_INVALID_PARAMETER; |
| 1852 | |
| 1853 | /* CPU->CPU is not an indirect link */ |
| 1854 | if (!node_props[node1].node.KFDGpuID && !node_props[node2].node.KFDGpuID) |
| 1855 | return HSAKMT_STATUS_INVALID_NODE_UNIT; |
| 1856 | |
| 1857 | if (node_props[node1].node.HiveID && |
| 1858 | node_props[node2].node.HiveID && |
| 1859 | node_props[node1].node.HiveID == node_props[node2].node.HiveID) |
| 1860 | return HSAKMT_STATUS_INVALID_PARAMETER; |
| 1861 | |
| 1862 | if (node_props[node1].node.KFDGpuID) |
| 1863 | dir_cpu1 = gpu_get_direct_link_cpu(node1, node_props); |
| 1864 | if (node_props[node2].node.KFDGpuID) |
| 1865 | dir_cpu2 = gpu_get_direct_link_cpu(node2, node_props); |
| 1866 | |
| 1867 | if (dir_cpu1 < 0 && dir_cpu2 < 0) |
| 1868 | return HSAKMT_STATUS_ERROR; |
| 1869 | |
| 1870 | /* if the node2(dst) is GPU , it need to be large bar for host access*/ |
| 1871 | if (node_props[node2].node.KFDGpuID) { |
| 1872 | for (i = 0; i < node_props[node2].node.NumMemoryBanks; ++i) |
| 1873 | if (node_props[node2].mem[i].HeapType == |
| 1874 | HSA_HEAPTYPE_FRAME_BUFFER_PUBLIC) |
| 1875 | break; |
| 1876 | if (i >= node_props[node2].node.NumMemoryBanks) |
| 1877 | return HSAKMT_STATUS_ERROR; |
| 1878 | } |
| 1879 | /* Possible topology: |
| 1880 | * GPU --(weight1) -- CPU -- (weight2) -- GPU |
| 1881 | * GPU --(weight1) -- CPU -- (weight2) -- CPU -- (weight3) -- GPU |
| 1882 | * GPU --(weight1) -- CPU -- (weight2) -- CPU |
| 1883 | * CPU -- (weight2) -- CPU -- (weight3) -- GPU |
| 1884 | */ |
| 1885 | if (dir_cpu1 >= 0) { /* GPU->CPU ... */ |
| 1886 | if (dir_cpu2 >= 0) { |
| 1887 | if (dir_cpu1 == dir_cpu2) /* GPU->CPU->GPU*/ { |
| 1888 | ret = get_direct_iolink_info(node1, dir_cpu1, |
| 1889 | node_props, &weight1, NULL); |
| 1890 | if (ret != HSAKMT_STATUS_SUCCESS) |
| 1891 | return ret; |
| 1892 | ret = get_direct_iolink_info(dir_cpu1, node2, |
| 1893 | node_props, &weight2, type); |
| 1894 | } else /* GPU->CPU->CPU->GPU*/ { |
| 1895 | ret = get_direct_iolink_info(node1, dir_cpu1, |
| 1896 | node_props, &weight1, NULL); |
| 1897 | if (ret != HSAKMT_STATUS_SUCCESS) |
| 1898 | return ret; |
| 1899 | ret = get_direct_iolink_info(dir_cpu1, dir_cpu2, |
| 1900 | node_props, &weight2, type); |
| 1901 | if (ret != HSAKMT_STATUS_SUCCESS) |
| 1902 | return ret; |
| 1903 | /* On QPI interconnection, GPUs can't access |
| 1904 | * each other if they are attached to different |
| 1905 | * CPU sockets. CPU<->CPU weight larger than 20 |
| 1906 | * means the two CPUs are in different sockets. |
| 1907 | */ |
| 1908 | if (*type == HSA_IOLINK_TYPE_QPI_1_1 |
| 1909 | && weight2 > 20) |
| 1910 | return HSAKMT_STATUS_NOT_SUPPORTED; |
| 1911 | ret = get_direct_iolink_info(dir_cpu2, node2, |
| 1912 | node_props, &weight3, NULL); |
| 1913 | } |
| 1914 | } else /* GPU->CPU->CPU */ { |
| 1915 | ret = get_direct_iolink_info(node1, dir_cpu1, node_props, |
| 1916 | &weight1, NULL); |
| 1917 | if (ret != HSAKMT_STATUS_SUCCESS) |
| 1918 | return ret; |
| 1919 | ret = get_direct_iolink_info(dir_cpu1, node2, node_props, |
| 1920 | &weight2, type); |
| 1921 | } |
| 1922 | } else { /* CPU->CPU->GPU */ |
| 1923 | ret = get_direct_iolink_info(node1, dir_cpu2, node_props, &weight2, |
| 1924 | type); |
| 1925 | if (ret != HSAKMT_STATUS_SUCCESS) |
| 1926 | return ret; |
| 1927 | ret = get_direct_iolink_info(dir_cpu2, node2, node_props, &weight3, |
| 1928 | NULL); |
| 1929 | } |
| 1930 | |
| 1931 | if (ret != HSAKMT_STATUS_SUCCESS) |
| 1932 | return ret; |
| 1933 | |
| 1934 | *weight = weight1 + weight2 + weight3; |
| 1935 | return HSAKMT_STATUS_SUCCESS; |
| 1936 | } |
| 1937 | |
| 1938 | static void topology_create_indirect_gpu_links(const HsaSystemProperties *sys_props, |
| 1939 | node_props_t *node_props) |
| 1940 | { |
| 1941 | |
| 1942 | uint32_t i, j; |
| 1943 | HSAuint32 weight; |
| 1944 | HSA_IOLINKTYPE type; |
| 1945 | |
| 1946 | for (i = 0; i < sys_props->NumNodes - 1; i++) { |
| 1947 | for (j = i + 1; j < sys_props->NumNodes; j++) { |
| 1948 | get_indirect_iolink_info(i, j, node_props, &weight, &type); |
| 1949 | if (!weight) |
| 1950 | goto try_alt_dir; |
| 1951 | if (topology_add_io_link_for_node(i, sys_props, node_props, |
| 1952 | type, j, weight) != HSAKMT_STATUS_SUCCESS) |
| 1953 | pr_err("Fail to add IO link %d->%d\n", i, j); |
| 1954 | try_alt_dir: |
| 1955 | get_indirect_iolink_info(j, i, node_props, &weight, &type); |
| 1956 | if (!weight) |
| 1957 | continue; |
| 1958 | if (topology_add_io_link_for_node(j, sys_props, node_props, |
| 1959 | type, i, weight) != HSAKMT_STATUS_SUCCESS) |
| 1960 | pr_err("Fail to add IO link %d->%d\n", j, i); |
| 1961 | } |
| 1962 | } |
| 1963 | } |
| 1964 | |
| 1965 | HSAKMT_STATUS topology_take_snapshot(HsaKFDContext *ctx) |
| 1966 | { |
| 1967 | uint32_t gen_start, gen_end, i, mem_id, cache_id; |
| 1968 | HsaSystemProperties sys_props; |
| 1969 | node_props_t *temp_props = 0; |
| 1970 | HSAKMT_STATUS ret = HSAKMT_STATUS_SUCCESS; |
| 1971 | struct proc_cpuinfo *cpuinfo; |
| 1972 | const uint32_t num_procs = get_nprocs(); |
| 1973 | uint32_t num_ioLinks; |
| 1974 | bool p2p_links = false; |
| 1975 | uint32_t num_p2pLinks = 0; |
| 1976 | |
| 1977 | cpuinfo = calloc(num_procs, sizeof(struct proc_cpuinfo)); |
| 1978 | if (!cpuinfo) { |
| 1979 | pr_err("Fail to allocate memory for CPU info\n"); |
| 1980 | return HSAKMT_STATUS_NO_MEMORY; |
| 1981 | } |
| 1982 | topology_parse_cpuinfo(cpuinfo, num_procs); |
| 1983 | |
| 1984 | retry: |
| 1985 | ret = topology_sysfs_get_generation(&gen_start); |
| 1986 | if (ret != HSAKMT_STATUS_SUCCESS) |
| 1987 | goto err; |
| 1988 | ret = hsakmt_topology_sysfs_get_system_props(ctx, &sys_props); |
| 1989 | if (ret != HSAKMT_STATUS_SUCCESS) |
| 1990 | goto err; |
| 1991 | if (sys_props.NumNodes > 0) { |
| 1992 | temp_props = calloc(sys_props.NumNodes * sizeof(node_props_t), 1); |
| 1993 | if (!temp_props) { |
| 1994 | ret = HSAKMT_STATUS_NO_MEMORY; |
| 1995 | goto err; |
| 1996 | } |
| 1997 | for (i = 0; i < sys_props.NumNodes; i++) { |
| 1998 | ret = topology_sysfs_get_node_props(i, |
| 1999 | &temp_props[i].node, |
| 2000 | &p2p_links, &num_p2pLinks); |
| 2001 | if (ret != HSAKMT_STATUS_SUCCESS) { |
| 2002 | free_properties(temp_props, i); |
| 2003 | goto err; |
| 2004 | } |
| 2005 | |
| 2006 | if (temp_props[i].node.NumCPUCores) |
| 2007 | topology_get_cpu_model_name(&temp_props[i].node, |
| 2008 | cpuinfo, num_procs); |
| 2009 | |
| 2010 | if (temp_props[i].node.NumMemoryBanks) { |
| 2011 | temp_props[i].mem = calloc(temp_props[i].node.NumMemoryBanks * sizeof(HsaMemoryProperties), 1); |
| 2012 | if (!temp_props[i].mem) { |
| 2013 | ret = HSAKMT_STATUS_NO_MEMORY; |
| 2014 | free_properties(temp_props, i + 1); |
| 2015 | goto err; |
| 2016 | } |
| 2017 | for (mem_id = 0; mem_id < temp_props[i].node.NumMemoryBanks; mem_id++) { |
| 2018 | ret = topology_sysfs_get_mem_props(i, mem_id, &temp_props[i].mem[mem_id]); |
| 2019 | if (ret != HSAKMT_STATUS_SUCCESS) { |
| 2020 | free_properties(temp_props, i + 1); |
| 2021 | goto err; |
| 2022 | } |
| 2023 | } |
| 2024 | } |
| 2025 | |
| 2026 | if (temp_props[i].node.NumCaches) { |
| 2027 | temp_props[i].cache = calloc(temp_props[i].node.NumCaches * sizeof(HsaCacheProperties), 1); |
| 2028 | if (!temp_props[i].cache) { |
| 2029 | ret = HSAKMT_STATUS_NO_MEMORY; |
| 2030 | free_properties(temp_props, i + 1); |
| 2031 | goto err; |
| 2032 | } |
| 2033 | for (cache_id = 0; cache_id < temp_props[i].node.NumCaches; cache_id++) { |
| 2034 | ret = topology_sysfs_get_cache_props(i, cache_id, &temp_props[i].cache[cache_id]); |
| 2035 | if (ret != HSAKMT_STATUS_SUCCESS) { |
| 2036 | free_properties(temp_props, i + 1); |
| 2037 | goto err; |
| 2038 | } |
| 2039 | } |
| 2040 | } else if (!temp_props[i].node.KFDGpuID) { /* a CPU node */ |
| 2041 | ret = topology_get_cpu_cache_props( |
| 2042 | i, cpuinfo, &temp_props[i]); |
| 2043 | if (ret != HSAKMT_STATUS_SUCCESS) { |
| 2044 | free_properties(temp_props, i + 1); |
| 2045 | goto err; |
| 2046 | } |
| 2047 | } |
| 2048 | |
| 2049 | /* To simplify, allocate maximum needed memory for io_links for each node. This |
| 2050 | * removes the need for realloc when indirect and QPI links are added later |
| 2051 | */ |
| 2052 | temp_props[i].link = calloc(sys_props.NumNodes - 1, sizeof(HsaIoLinkProperties)); |
| 2053 | if (!temp_props[i].link) { |
| 2054 | ret = HSAKMT_STATUS_NO_MEMORY; |
| 2055 | free_properties(temp_props, i + 1); |
| 2056 | goto err; |
| 2057 | } |
| 2058 | num_ioLinks = temp_props[i].node.NumIOLinks - num_p2pLinks; |
| 2059 | uint32_t link_id = 0; |
| 2060 | |
| 2061 | if (num_ioLinks) { |
| 2062 | uint32_t sys_link_id = 0; |
| 2063 | |
| 2064 | /* Parse all the sysfs specified io links. Skip the ones where the |
| 2065 | * remote node (node_to) is not accessible |
| 2066 | */ |
| 2067 | while (sys_link_id < num_ioLinks && |
| 2068 | link_id < sys_props.NumNodes - 1) { |
| 2069 | ret = topology_sysfs_get_iolink_props(ctx, i, sys_link_id++, |
| 2070 | &temp_props[i].link[link_id], false); |
| 2071 | if (ret == HSAKMT_STATUS_NOT_SUPPORTED) { |
| 2072 | continue; |
| 2073 | } else if (ret != HSAKMT_STATUS_SUCCESS) { |
| 2074 | free_properties(temp_props, i + 1); |
| 2075 | goto err; |
| 2076 | } |
| 2077 | link_id++; |
| 2078 | } |
| 2079 | /* sysfs specifies all the io links. Limit the number to valid ones */ |
| 2080 | temp_props[i].node.NumIOLinks = link_id; |
| 2081 | } |
| 2082 | |
| 2083 | if (num_p2pLinks) { |
| 2084 | uint32_t sys_link_id = 0; |
| 2085 | |
| 2086 | /* Parse all the sysfs specified p2p links. |
| 2087 | */ |
| 2088 | while (sys_link_id < num_p2pLinks && |
| 2089 | link_id < sys_props.NumNodes - 1) { |
| 2090 | ret = topology_sysfs_get_iolink_props(ctx, i, sys_link_id++, |
| 2091 | &temp_props[i].link[link_id], true); |
| 2092 | if (ret == HSAKMT_STATUS_NOT_SUPPORTED) { |
| 2093 | continue; |
| 2094 | } else if (ret != HSAKMT_STATUS_SUCCESS) { |
| 2095 | free_properties(temp_props, i + 1); |
| 2096 | goto err; |
| 2097 | } |
| 2098 | link_id++; |
| 2099 | } |
| 2100 | temp_props[i].node.NumIOLinks = link_id; |
| 2101 | } |
| 2102 | } |
| 2103 | } |
| 2104 | |
| 2105 | if (!p2p_links) { |
| 2106 | /* All direct IO links are created in the kernel. Here we need to |
| 2107 | * connect GPU<->GPU or GPU<->CPU indirect IO links. |
| 2108 | */ |
| 2109 | topology_create_indirect_gpu_links(&sys_props, temp_props); |
| 2110 | } |
| 2111 | |
| 2112 | ret = topology_sysfs_get_generation(&gen_end); |
| 2113 | if (ret != HSAKMT_STATUS_SUCCESS) { |
| 2114 | free_properties(temp_props, sys_props.NumNodes); |
| 2115 | goto err; |
| 2116 | } |
| 2117 | |
| 2118 | if (gen_start != gen_end) { |
| 2119 | free_properties(temp_props, sys_props.NumNodes); |
| 2120 | temp_props = 0; |
| 2121 | goto retry; |
| 2122 | } |
| 2123 | |
| 2124 | if (!g_system) { |
| 2125 | g_system = malloc(sizeof(HsaSystemProperties)); |
| 2126 | if (!g_system) { |
| 2127 | free_properties(temp_props, sys_props.NumNodes); |
| 2128 | ret = HSAKMT_STATUS_NO_MEMORY; |
| 2129 | goto err; |
| 2130 | } |
| 2131 | } |
| 2132 | |
| 2133 | *g_system = sys_props; |
| 2134 | if (g_props) |
| 2135 | free(g_props); |
| 2136 | g_props = temp_props; |
| 2137 | err: |
| 2138 | free(cpuinfo); |
| 2139 | return ret; |
| 2140 | } |
| 2141 | |
| 2142 | /* Drop the Snapshot of the HSA topology information. Assume lock is held. */ |
| 2143 | void topology_drop_snapshot(void) |
| 2144 | { |
| 2145 | if (!!g_system != !!g_props) |
| 2146 | pr_warn("Probably inconsistency?\n"); |
| 2147 | |
| 2148 | if (g_props) { |
| 2149 | /* Remove state */ |
| 2150 | free_properties(g_props, g_system->NumNodes); |
| 2151 | g_props = NULL; |
| 2152 | } |
| 2153 | |
| 2154 | free(g_system); |
| 2155 | g_system = NULL; |
| 2156 | |
| 2157 | if (map_user_to_sysfs_node_id) { |
| 2158 | free(map_user_to_sysfs_node_id); |
| 2159 | map_user_to_sysfs_node_id = NULL; |
| 2160 | map_user_to_sysfs_node_id_size = 0; |
| 2161 | } |
| 2162 | } |
| 2163 | |
| 2164 | HSAKMT_STATUS hsakmt_validate_nodeid(uint32_t nodeid, uint32_t *gpu_id) |
| 2165 | { |
| 2166 | if (!g_props || !g_system || g_system->NumNodes <= nodeid) |
| 2167 | return HSAKMT_STATUS_INVALID_NODE_UNIT; |
| 2168 | if (gpu_id) |
| 2169 | *gpu_id = g_props[nodeid].node.KFDGpuID; |
| 2170 | |
| 2171 | return HSAKMT_STATUS_SUCCESS; |
| 2172 | } |
| 2173 | |
| 2174 | HSAKMT_STATUS hsakmt_gpuid_to_nodeid(uint32_t gpu_id, uint32_t *node_id) |
| 2175 | { |
| 2176 | uint64_t node_idx; |
| 2177 | |
| 2178 | for (node_idx = 0; node_idx < g_system->NumNodes; node_idx++) { |
| 2179 | if (g_props[node_idx].node.KFDGpuID == gpu_id) { |
| 2180 | *node_id = node_idx; |
| 2181 | return HSAKMT_STATUS_SUCCESS; |
| 2182 | } |
| 2183 | } |
| 2184 | |
| 2185 | return HSAKMT_STATUS_INVALID_NODE_UNIT; |
| 2186 | |
| 2187 | } |
| 2188 | |
| 2189 | HSAKMT_STATUS HSAKMTAPI hsaKmtAcquireSystemPropertiesCtx(HsaKFDContext *ctx, |
| 2190 | HsaSystemProperties *SystemProperties) |
| 2191 | { |
| 2192 | HSAKMT_STATUS err = HSAKMT_STATUS_SUCCESS; |
| 2193 | |
| 2194 | CHECK_KFD_OPEN(); |
| 2195 | |
| 2196 | if (!SystemProperties) |
| 2197 | return HSAKMT_STATUS_INVALID_PARAMETER; |
| 2198 | |
| 2199 | pthread_mutex_lock(&hsakmt_mutex); |
| 2200 | |
| 2201 | /* We already have a valid snapshot. Avoid double initialization that |
| 2202 | * would leak memory. |
| 2203 | */ |
| 2204 | if (g_system) { |
| 2205 | *SystemProperties = *g_system; |
| 2206 | goto out; |
| 2207 | } |
| 2208 | |
| 2209 | err = topology_take_snapshot(ctx); |
| 2210 | if (err != HSAKMT_STATUS_SUCCESS) |
| 2211 | goto out; |
| 2212 | |
| 2213 | assert(g_system); |
| 2214 | |
| 2215 | if (hsakmt_use_model) |
| 2216 | model_init(); |
| 2217 | |
| 2218 | err = hsakmt_fmm_init_process_apertures(ctx, g_system->NumNodes); |
| 2219 | if (err != HSAKMT_STATUS_SUCCESS) |
| 2220 | goto init_process_apertures_failed; |
| 2221 | |
| 2222 | err = hsakmt_init_process_doorbells(ctx, g_system->NumNodes); |
| 2223 | if (err != HSAKMT_STATUS_SUCCESS) |
| 2224 | goto init_doorbells_failed; |
| 2225 | |
| 2226 | *SystemProperties = *g_system; |
| 2227 | |
| 2228 | for (int node = 0; node < g_system->NumNodes; node++) { |
| 2229 | if (hsakmt_get_gfxv_by_node_id(node) == GFX_VERSION_GFX1151 && |
| 2230 | hsakmt_kfd_version_info.KernelInterfaceMajorVersion == 1 && |
| 2231 | hsakmt_kfd_version_info.KernelInterfaceMinorVersion < 20) |
| 2232 | pr_err_once("WARNING: KFD ABI 1.20+ is recommended for gfx1151. Current KFD ABI is %i.%i. This may result in faults, crashes and other application instability\n", hsakmt_kfd_version_info.KernelInterfaceMajorVersion, hsakmt_kfd_version_info.KernelInterfaceMinorVersion); |
| 2233 | } |
| 2234 | |
| 2235 | goto out; |
| 2236 | |
| 2237 | init_doorbells_failed: |
| 2238 | hsakmt_fmm_destroy_process_apertures(ctx); |
| 2239 | init_process_apertures_failed: |
| 2240 | topology_drop_snapshot(); |
| 2241 | |
| 2242 | out: |
| 2243 | pthread_mutex_unlock(&hsakmt_mutex); |
| 2244 | return err; |
| 2245 | } |
| 2246 | |
| 2247 | HSAKMT_STATUS HSAKMTAPI hsaKmtReleaseSystemPropertiesCtx(HsaKFDContext *ctx) |
| 2248 | { |
| 2249 | pthread_mutex_lock(&hsakmt_mutex); |
| 2250 | |
| 2251 | hsakmt_destroy_process_doorbells(ctx); |
| 2252 | hsakmt_fmm_destroy_process_apertures(ctx); |
| 2253 | topology_drop_snapshot(); |
| 2254 | |
| 2255 | pthread_mutex_unlock(&hsakmt_mutex); |
| 2256 | |
| 2257 | return HSAKMT_STATUS_SUCCESS; |
| 2258 | } |
| 2259 | |
| 2260 | HSAKMT_STATUS hsakmt_topology_get_node_props(HSAuint32 NodeId, |
| 2261 | HsaNodeProperties *NodeProperties) |
| 2262 | { |
| 2263 | if (!g_system || !g_props || NodeId >= g_system->NumNodes) |
| 2264 | return HSAKMT_STATUS_ERROR; |
| 2265 | |
| 2266 | *NodeProperties = g_props[NodeId].node; |
| 2267 | return HSAKMT_STATUS_SUCCESS; |
| 2268 | } |
| 2269 | |
| 2270 | |
| 2271 | HSAKMT_STATUS HSAKMTAPI hsaKmtGetNodePropertiesCtx(HsaKFDContext *ctx, |
| 2272 | HSAuint32 NodeId, |
| 2273 | HsaNodeProperties *NodeProperties) |
| 2274 | { |
| 2275 | HSAKMT_STATUS err; |
| 2276 | uint32_t gpu_id; |
| 2277 | |
| 2278 | if (!NodeProperties) |
| 2279 | return HSAKMT_STATUS_INVALID_PARAMETER; |
| 2280 | |
| 2281 | CHECK_KFD_OPEN(); |
| 2282 | pthread_mutex_lock(&hsakmt_mutex); |
| 2283 | |
| 2284 | err = hsakmt_validate_nodeid(NodeId, &gpu_id); |
| 2285 | if (err != HSAKMT_STATUS_SUCCESS) |
| 2286 | goto out; |
| 2287 | |
| 2288 | err = hsakmt_topology_get_node_props(NodeId, NodeProperties); |
| 2289 | if (err != HSAKMT_STATUS_SUCCESS) |
| 2290 | goto out; |
| 2291 | /* For CPU only node don't add any additional GPU memory banks. */ |
| 2292 | if (gpu_id) { |
| 2293 | uint64_t base, limit; |
| 2294 | if (hsakmt_is_dgpu) |
| 2295 | NodeProperties->NumMemoryBanks += NUM_OF_DGPU_HEAPS; |
| 2296 | else |
| 2297 | NodeProperties->NumMemoryBanks += NUM_OF_IGPU_HEAPS; |
| 2298 | if (hsakmt_fmm_get_aperture_base_and_limit(ctx, FMM_MMIO, gpu_id, &base, |
| 2299 | &limit) == HSAKMT_STATUS_SUCCESS) |
| 2300 | NodeProperties->NumMemoryBanks += 1; |
| 2301 | } |
| 2302 | |
| 2303 | out: |
| 2304 | pthread_mutex_unlock(&hsakmt_mutex); |
| 2305 | return err; |
| 2306 | } |
| 2307 | |
| 2308 | HSAKMT_STATUS HSAKMTAPI hsaKmtGetNodeMemoryPropertiesCtx(HsaKFDContext *ctx, |
| 2309 | HSAuint32 NodeId, |
| 2310 | HSAuint32 NumBanks, |
| 2311 | HsaMemoryProperties *MemoryProperties) |
| 2312 | { |
| 2313 | HSAKMT_STATUS err = HSAKMT_STATUS_SUCCESS; |
| 2314 | uint32_t i, gpu_id; |
| 2315 | HSAuint64 aperture_limit; |
| 2316 | |
| 2317 | if (!MemoryProperties) |
| 2318 | return HSAKMT_STATUS_INVALID_PARAMETER; |
| 2319 | |
| 2320 | CHECK_KFD_OPEN(); |
| 2321 | pthread_mutex_lock(&hsakmt_mutex); |
| 2322 | |
| 2323 | err = hsakmt_validate_nodeid(NodeId, &gpu_id); |
| 2324 | if (err != HSAKMT_STATUS_SUCCESS) |
| 2325 | goto out; |
| 2326 | |
| 2327 | memset(MemoryProperties, 0, NumBanks * sizeof(HsaMemoryProperties)); |
| 2328 | |
| 2329 | for (i = 0; i < MIN(g_props[NodeId].node.NumMemoryBanks, NumBanks); i++) { |
| 2330 | assert(g_props[NodeId].mem); |
| 2331 | MemoryProperties[i] = g_props[NodeId].mem[i]; |
| 2332 | } |
| 2333 | |
| 2334 | /* The following memory banks does not apply to CPU only node */ |
| 2335 | if (gpu_id == 0) |
| 2336 | goto out; |
| 2337 | |
| 2338 | /*Add LDS*/ |
| 2339 | if (i < NumBanks && |
| 2340 | hsakmt_fmm_get_aperture_base_and_limit(ctx, FMM_LDS, gpu_id, |
| 2341 | &MemoryProperties[i].VirtualBaseAddress, &aperture_limit) == HSAKMT_STATUS_SUCCESS) { |
| 2342 | MemoryProperties[i].HeapType = HSA_HEAPTYPE_GPU_LDS; |
| 2343 | MemoryProperties[i].SizeInBytes = g_props[NodeId].node.LDSSizeInKB * 1024; |
| 2344 | i++; |
| 2345 | } |
| 2346 | |
| 2347 | /* Add Local memory - HSA_HEAPTYPE_FRAME_BUFFER_PRIVATE. |
| 2348 | * For dGPU the topology node contains Local Memory and it is added by |
| 2349 | * the for loop above |
| 2350 | */ |
| 2351 | if (hsakmt_get_gfxv_by_node_id(NodeId) == GFX_VERSION_KAVERI && i < NumBanks && |
| 2352 | g_props[NodeId].node.LocalMemSize > 0 && |
| 2353 | hsakmt_fmm_get_aperture_base_and_limit(ctx, FMM_GPUVM, gpu_id, |
| 2354 | &MemoryProperties[i].VirtualBaseAddress, &aperture_limit) == HSAKMT_STATUS_SUCCESS) { |
| 2355 | MemoryProperties[i].HeapType = HSA_HEAPTYPE_FRAME_BUFFER_PRIVATE; |
| 2356 | MemoryProperties[i].SizeInBytes = g_props[NodeId].node.LocalMemSize; |
| 2357 | i++; |
| 2358 | } |
| 2359 | |
| 2360 | /* Add SCRATCH */ |
| 2361 | if (i < NumBanks && |
| 2362 | hsakmt_fmm_get_aperture_base_and_limit(ctx, FMM_SCRATCH, gpu_id, |
| 2363 | &MemoryProperties[i].VirtualBaseAddress, &aperture_limit) == HSAKMT_STATUS_SUCCESS) { |
| 2364 | MemoryProperties[i].HeapType = HSA_HEAPTYPE_GPU_SCRATCH; |
| 2365 | MemoryProperties[i].SizeInBytes = (aperture_limit - MemoryProperties[i].VirtualBaseAddress) + 1; |
| 2366 | i++; |
| 2367 | } |
| 2368 | |
| 2369 | /* Add SVM aperture */ |
| 2370 | if (hsakmt_topology_is_svm_needed(g_props[NodeId].node.EngineId) && i < NumBanks && |
| 2371 | hsakmt_fmm_get_aperture_base_and_limit(ctx, |
| 2372 | FMM_SVM, gpu_id, &MemoryProperties[i].VirtualBaseAddress, |
| 2373 | &aperture_limit) == HSAKMT_STATUS_SUCCESS) { |
| 2374 | MemoryProperties[i].HeapType = HSA_HEAPTYPE_DEVICE_SVM; |
| 2375 | MemoryProperties[i].SizeInBytes = (aperture_limit - MemoryProperties[i].VirtualBaseAddress) + 1; |
| 2376 | i++; |
| 2377 | } |
| 2378 | |
| 2379 | /* Add mmio aperture */ |
| 2380 | if (i < NumBanks && |
| 2381 | hsakmt_fmm_get_aperture_base_and_limit(ctx, FMM_MMIO, gpu_id, |
| 2382 | &MemoryProperties[i].VirtualBaseAddress, &aperture_limit) == HSAKMT_STATUS_SUCCESS) { |
| 2383 | MemoryProperties[i].HeapType = HSA_HEAPTYPE_MMIO_REMAP; |
| 2384 | MemoryProperties[i].SizeInBytes = (aperture_limit - MemoryProperties[i].VirtualBaseAddress) + 1; |
| 2385 | i++; |
| 2386 | } |
| 2387 | |
| 2388 | out: |
| 2389 | pthread_mutex_unlock(&hsakmt_mutex); |
| 2390 | return err; |
| 2391 | } |
| 2392 | |
| 2393 | HSAKMT_STATUS HSAKMTAPI hsaKmtGetNodeCachePropertiesCtx(HsaKFDContext *ctx, |
| 2394 | HSAuint32 NodeId, |
| 2395 | HSAuint32 ProcessorId, |
| 2396 | HSAuint32 NumCaches, |
| 2397 | HsaCacheProperties *CacheProperties) |
| 2398 | { |
| 2399 | HSAKMT_STATUS err; |
| 2400 | uint32_t i; |
| 2401 | |
| 2402 | if (!CacheProperties) |
| 2403 | return HSAKMT_STATUS_INVALID_PARAMETER; |
| 2404 | |
| 2405 | CHECK_KFD_OPEN(); |
| 2406 | pthread_mutex_lock(&hsakmt_mutex); |
| 2407 | |
| 2408 | /* KFD ADD page 18, snapshot protocol violation */ |
| 2409 | if (!g_system || NodeId >= g_system->NumNodes) { |
| 2410 | err = HSAKMT_STATUS_INVALID_NODE_UNIT; |
| 2411 | goto out; |
| 2412 | } |
| 2413 | |
| 2414 | if (NumCaches > g_props[NodeId].node.NumCaches) { |
| 2415 | err = HSAKMT_STATUS_INVALID_PARAMETER; |
| 2416 | goto out; |
| 2417 | } |
| 2418 | |
| 2419 | for (i = 0; i < MIN(g_props[NodeId].node.NumCaches, NumCaches); i++) { |
| 2420 | assert(g_props[NodeId].cache); |
| 2421 | CacheProperties[i] = g_props[NodeId].cache[i]; |
| 2422 | } |
| 2423 | |
| 2424 | err = HSAKMT_STATUS_SUCCESS; |
| 2425 | |
| 2426 | out: |
| 2427 | pthread_mutex_unlock(&hsakmt_mutex); |
| 2428 | return err; |
| 2429 | } |
| 2430 | |
| 2431 | HSAKMT_STATUS hsakmt_topology_get_iolink_props(HSAuint32 NodeId, |
| 2432 | HSAuint32 NumIoLinks, |
| 2433 | HsaIoLinkProperties *IoLinkProperties) |
| 2434 | { |
| 2435 | if (!g_system || !g_props || NodeId >= g_system->NumNodes) |
| 2436 | return HSAKMT_STATUS_ERROR; |
| 2437 | |
| 2438 | memcpy(IoLinkProperties, g_props[NodeId].link, |
| 2439 | NumIoLinks * sizeof(*IoLinkProperties)); |
| 2440 | |
| 2441 | return HSAKMT_STATUS_SUCCESS; |
| 2442 | } |
| 2443 | |
| 2444 | HSAKMT_STATUS HSAKMTAPI hsaKmtGetNodeIoLinkPropertiesCtx(HsaKFDContext *ctx, |
| 2445 | HSAuint32 NodeId, |
| 2446 | HSAuint32 NumIoLinks, |
| 2447 | HsaIoLinkProperties *IoLinkProperties) |
| 2448 | { |
| 2449 | HSAKMT_STATUS err; |
| 2450 | |
| 2451 | if (!IoLinkProperties) |
| 2452 | return HSAKMT_STATUS_INVALID_PARAMETER; |
| 2453 | |
| 2454 | CHECK_KFD_OPEN(); |
| 2455 | |
| 2456 | pthread_mutex_lock(&hsakmt_mutex); |
| 2457 | |
| 2458 | /* KFD ADD page 18, snapshot protocol violation */ |
| 2459 | if (!g_system || NodeId >= g_system->NumNodes ) { |
| 2460 | err = HSAKMT_STATUS_INVALID_NODE_UNIT; |
| 2461 | goto out; |
| 2462 | } |
| 2463 | |
| 2464 | if (NumIoLinks > g_props[NodeId].node.NumIOLinks) { |
| 2465 | err = HSAKMT_STATUS_INVALID_PARAMETER; |
| 2466 | goto out; |
| 2467 | } |
| 2468 | |
| 2469 | assert(g_props[NodeId].link); |
| 2470 | err = hsakmt_topology_get_iolink_props(NodeId, NumIoLinks, IoLinkProperties); |
| 2471 | |
| 2472 | out: |
| 2473 | pthread_mutex_unlock(&hsakmt_mutex); |
| 2474 | return err; |
| 2475 | } |
| 2476 | |
| 2477 | uint32_t hsakmt_get_gfxv_by_node_id(HSAuint32 node_id) |
| 2478 | { |
| 2479 | return HSA_GET_GFX_VERSION_FULL(g_props[node_id].node.EngineId.ui32); |
| 2480 | } |
| 2481 | |
| 2482 | uint16_t hsakmt_get_device_id_by_node_id(HSAuint32 node_id) |
| 2483 | { |
| 2484 | if (!g_props || !g_system || g_system->NumNodes <= node_id) |
| 2485 | return 0; |
| 2486 | |
| 2487 | return g_props[node_id].node.DeviceId; |
| 2488 | } |
| 2489 | |
| 2490 | bool hsakmt_prefer_ats(HSAuint32 node_id) |
| 2491 | { |
| 2492 | return g_props[node_id].node.Capability.ui32.HSAMMUPresent |
| 2493 | && g_props[node_id].node.NumCPUCores |
| 2494 | && g_props[node_id].node.NumFComputeCores; |
| 2495 | } |
| 2496 | |
| 2497 | uint16_t hsakmt_get_device_id_by_gpu_id(HSAuint32 gpu_id) |
| 2498 | { |
| 2499 | unsigned int i; |
| 2500 | |
| 2501 | if (!g_props || !g_system) |
| 2502 | return 0; |
| 2503 | |
| 2504 | for (i = 0; i < g_system->NumNodes; i++) { |
| 2505 | if (g_props[i].node.KFDGpuID == gpu_id) |
| 2506 | return g_props[i].node.DeviceId; |
| 2507 | } |
| 2508 | |
| 2509 | return 0; |
| 2510 | } |
| 2511 | |
| 2512 | uint32_t hsakmt_get_direct_link_cpu(uint32_t gpu_node) |
| 2513 | { |
| 2514 | HSAuint64 size = 0; |
| 2515 | int32_t cpu_id; |
| 2516 | HSAuint32 i; |
| 2517 | |
| 2518 | cpu_id = gpu_get_direct_link_cpu(gpu_node, g_props); |
| 2519 | if (cpu_id == -1) |
| 2520 | return INVALID_NODEID; |
| 2521 | |
| 2522 | assert(g_props[cpu_id].mem); |
| 2523 | |
| 2524 | for (i = 0; i < g_props[cpu_id].node.NumMemoryBanks; i++) |
| 2525 | size += g_props[cpu_id].mem[i].SizeInBytes; |
| 2526 | |
| 2527 | return size ? (uint32_t)cpu_id : INVALID_NODEID; |
| 2528 | } |
| 2529 | |
| 2530 | |
| 2531 | HSAKMT_STATUS hsakmt_validate_nodeid_array(uint32_t **gpu_id_array, |
| 2532 | uint32_t NumberOfNodes, uint32_t *NodeArray) |
| 2533 | { |
| 2534 | HSAKMT_STATUS ret; |
| 2535 | unsigned int i; |
| 2536 | |
| 2537 | if (NumberOfNodes == 0 || !NodeArray || !gpu_id_array) |
| 2538 | return HSAKMT_STATUS_INVALID_PARAMETER; |
| 2539 | |
| 2540 | /* Translate Node IDs to gpu_ids */ |
| 2541 | *gpu_id_array = malloc(NumberOfNodes * sizeof(uint32_t)); |
| 2542 | if (!(*gpu_id_array)) |
| 2543 | return HSAKMT_STATUS_NO_MEMORY; |
| 2544 | for (i = 0; i < NumberOfNodes; i++) { |
| 2545 | ret = hsakmt_validate_nodeid(NodeArray[i], *gpu_id_array + i); |
| 2546 | if (ret != HSAKMT_STATUS_SUCCESS) { |
| 2547 | free(*gpu_id_array); |
| 2548 | break; |
| 2549 | } |
| 2550 | } |
| 2551 | |
| 2552 | return ret; |
| 2553 | } |
| 2554 | |
| 2555 | inline uint32_t hsakmt_get_num_sysfs_nodes(void) |
| 2556 | { |
| 2557 | return num_sysfs_nodes; |
| 2558 | } |
| 2559 | |
| 2560 | |
| 2561 | |
| 2562 | HSAKMT_STATUS HSAKMTAPI hsaKmtAcquireSystemProperties(HsaSystemProperties *SystemProperties) |
| 2563 | { |
| 2564 | return hsaKmtAcquireSystemPropertiesCtx(&hsakmt_primary_kfd_ctx, SystemProperties); |
| 2565 | } |
| 2566 | |
| 2567 | HSAKMT_STATUS HSAKMTAPI hsaKmtReleaseSystemProperties(void) |
| 2568 | { |
| 2569 | return hsaKmtReleaseSystemPropertiesCtx(&hsakmt_primary_kfd_ctx); |
| 2570 | } |
| 2571 | |
| 2572 | HSAKMT_STATUS HSAKMTAPI hsaKmtGetNodeProperties(HSAuint32 NodeId, |
| 2573 | HsaNodeProperties *NodeProperties) |
| 2574 | { |
| 2575 | return hsaKmtGetNodePropertiesCtx(&hsakmt_primary_kfd_ctx, NodeId, NodeProperties); |
| 2576 | } |
| 2577 | |
| 2578 | HSAKMT_STATUS HSAKMTAPI hsaKmtGetNodeMemoryProperties(HSAuint32 NodeId, |
| 2579 | HSAuint32 NumBanks, |
| 2580 | HsaMemoryProperties *MemoryProperties) |
| 2581 | { |
| 2582 | return hsaKmtGetNodeMemoryPropertiesCtx(&hsakmt_primary_kfd_ctx, NodeId, NumBanks, MemoryProperties); |
| 2583 | } |
| 2584 | |
| 2585 | HSAKMT_STATUS HSAKMTAPI hsaKmtGetNodeCacheProperties(HSAuint32 NodeId, |
| 2586 | HSAuint32 ProcessorId, |
| 2587 | HSAuint32 NumCaches, |
| 2588 | HsaCacheProperties *CacheProperties) |
| 2589 | { |
| 2590 | return hsaKmtGetNodeCachePropertiesCtx(&hsakmt_primary_kfd_ctx, NodeId, ProcessorId, NumCaches, CacheProperties); |
| 2591 | } |
| 2592 | |
| 2593 | HSAKMT_STATUS HSAKMTAPI hsaKmtGetNodeIoLinkProperties(HSAuint32 NodeId, |
| 2594 | HSAuint32 NumIoLinks, |
| 2595 | HsaIoLinkProperties *IoLinkProperties) |
| 2596 | { |
| 2597 | return hsaKmtGetNodeIoLinkPropertiesCtx(&hsakmt_primary_kfd_ctx, NodeId, NumIoLinks, IoLinkProperties); |
| 2598 | } |