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