amdgpu_xgmi.c revision 1.1
1/* $NetBSD: amdgpu_xgmi.c,v 1.1 2021/12/18 20:11:13 riastradh Exp $ */ 2 3/* 4 * Copyright 2018 Advanced Micro Devices, Inc. 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the "Software"), 8 * to deal in the Software without restriction, including without limitation 9 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 * and/or sell copies of the Software, and to permit persons to whom the 11 * Software is furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice shall be included in 14 * all copies or substantial portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 * OTHER DEALINGS IN THE SOFTWARE. 23 * 24 * 25 */ 26#include <sys/cdefs.h> 27__KERNEL_RCSID(0, "$NetBSD: amdgpu_xgmi.c,v 1.1 2021/12/18 20:11:13 riastradh Exp $"); 28 29#include <linux/list.h> 30#include "amdgpu.h" 31#include "amdgpu_xgmi.h" 32#include "amdgpu_smu.h" 33#include "amdgpu_ras.h" 34#include "df/df_3_6_offset.h" 35 36static DEFINE_MUTEX(xgmi_mutex); 37 38#define AMDGPU_MAX_XGMI_HIVE 8 39#define AMDGPU_MAX_XGMI_DEVICE_PER_HIVE 4 40 41static struct amdgpu_hive_info xgmi_hives[AMDGPU_MAX_XGMI_HIVE]; 42static unsigned hive_count = 0; 43 44void *amdgpu_xgmi_hive_try_lock(struct amdgpu_hive_info *hive) 45{ 46 return &hive->device_list; 47} 48 49/** 50 * DOC: AMDGPU XGMI Support 51 * 52 * XGMI is a high speed interconnect that joins multiple GPU cards 53 * into a homogeneous memory space that is organized by a collective 54 * hive ID and individual node IDs, both of which are 64-bit numbers. 55 * 56 * The file xgmi_device_id contains the unique per GPU device ID and 57 * is stored in the /sys/class/drm/card${cardno}/device/ directory. 58 * 59 * Inside the device directory a sub-directory 'xgmi_hive_info' is 60 * created which contains the hive ID and the list of nodes. 61 * 62 * The hive ID is stored in: 63 * /sys/class/drm/card${cardno}/device/xgmi_hive_info/xgmi_hive_id 64 * 65 * The node information is stored in numbered directories: 66 * /sys/class/drm/card${cardno}/device/xgmi_hive_info/node${nodeno}/xgmi_device_id 67 * 68 * Each device has their own xgmi_hive_info direction with a mirror 69 * set of node sub-directories. 70 * 71 * The XGMI memory space is built by contiguously adding the power of 72 * two padded VRAM space from each node to each other. 73 * 74 */ 75 76 77static ssize_t amdgpu_xgmi_show_hive_id(struct device *dev, 78 struct device_attribute *attr, char *buf) 79{ 80 struct amdgpu_hive_info *hive = 81 container_of(attr, struct amdgpu_hive_info, dev_attr); 82 83 return snprintf(buf, PAGE_SIZE, "%llu\n", hive->hive_id); 84} 85 86static int amdgpu_xgmi_sysfs_create(struct amdgpu_device *adev, 87 struct amdgpu_hive_info *hive) 88{ 89 int ret = 0; 90 91 if (WARN_ON(hive->kobj)) 92 return -EINVAL; 93 94 hive->kobj = kobject_create_and_add("xgmi_hive_info", &adev->dev->kobj); 95 if (!hive->kobj) { 96 dev_err(adev->dev, "XGMI: Failed to allocate sysfs entry!\n"); 97 return -EINVAL; 98 } 99 100 hive->dev_attr = (struct device_attribute) { 101 .attr = { 102 .name = "xgmi_hive_id", 103 .mode = S_IRUGO, 104 105 }, 106 .show = amdgpu_xgmi_show_hive_id, 107 }; 108 109 ret = sysfs_create_file(hive->kobj, &hive->dev_attr.attr); 110 if (ret) { 111 dev_err(adev->dev, "XGMI: Failed to create device file xgmi_hive_id\n"); 112 kobject_del(hive->kobj); 113 kobject_put(hive->kobj); 114 hive->kobj = NULL; 115 } 116 117 return ret; 118} 119 120static void amdgpu_xgmi_sysfs_destroy(struct amdgpu_device *adev, 121 struct amdgpu_hive_info *hive) 122{ 123 sysfs_remove_file(hive->kobj, &hive->dev_attr.attr); 124 kobject_del(hive->kobj); 125 kobject_put(hive->kobj); 126 hive->kobj = NULL; 127} 128 129static ssize_t amdgpu_xgmi_show_device_id(struct device *dev, 130 struct device_attribute *attr, 131 char *buf) 132{ 133 struct drm_device *ddev = dev_get_drvdata(dev); 134 struct amdgpu_device *adev = ddev->dev_private; 135 136 return snprintf(buf, PAGE_SIZE, "%llu\n", adev->gmc.xgmi.node_id); 137 138} 139 140#define AMDGPU_XGMI_SET_FICAA(o) ((o) | 0x456801) 141static ssize_t amdgpu_xgmi_show_error(struct device *dev, 142 struct device_attribute *attr, 143 char *buf) 144{ 145 struct drm_device *ddev = dev_get_drvdata(dev); 146 struct amdgpu_device *adev = ddev->dev_private; 147 uint32_t ficaa_pie_ctl_in, ficaa_pie_status_in; 148 uint64_t fica_out; 149 unsigned int error_count = 0; 150 151 ficaa_pie_ctl_in = AMDGPU_XGMI_SET_FICAA(0x200); 152 ficaa_pie_status_in = AMDGPU_XGMI_SET_FICAA(0x208); 153 154 fica_out = adev->df.funcs->get_fica(adev, ficaa_pie_ctl_in); 155 if (fica_out != 0x1f) 156 pr_err("xGMI error counters not enabled!\n"); 157 158 fica_out = adev->df.funcs->get_fica(adev, ficaa_pie_status_in); 159 160 if ((fica_out & 0xffff) == 2) 161 error_count = ((fica_out >> 62) & 0x1) + (fica_out >> 63); 162 163 adev->df.funcs->set_fica(adev, ficaa_pie_status_in, 0, 0); 164 165 return snprintf(buf, PAGE_SIZE, "%d\n", error_count); 166} 167 168 169static DEVICE_ATTR(xgmi_device_id, S_IRUGO, amdgpu_xgmi_show_device_id, NULL); 170static DEVICE_ATTR(xgmi_error, S_IRUGO, amdgpu_xgmi_show_error, NULL); 171 172static int amdgpu_xgmi_sysfs_add_dev_info(struct amdgpu_device *adev, 173 struct amdgpu_hive_info *hive) 174{ 175 int ret = 0; 176 char node[10] = { 0 }; 177 178 /* Create xgmi device id file */ 179 ret = device_create_file(adev->dev, &dev_attr_xgmi_device_id); 180 if (ret) { 181 dev_err(adev->dev, "XGMI: Failed to create device file xgmi_device_id\n"); 182 return ret; 183 } 184 185 /* Create xgmi error file */ 186 ret = device_create_file(adev->dev, &dev_attr_xgmi_error); 187 if (ret) 188 pr_err("failed to create xgmi_error\n"); 189 190 191 /* Create sysfs link to hive info folder on the first device */ 192 if (adev != hive->adev) { 193 ret = sysfs_create_link(&adev->dev->kobj, hive->kobj, 194 "xgmi_hive_info"); 195 if (ret) { 196 dev_err(adev->dev, "XGMI: Failed to create link to hive info"); 197 goto remove_file; 198 } 199 } 200 201 sprintf(node, "node%d", hive->number_devices); 202 /* Create sysfs link form the hive folder to yourself */ 203 ret = sysfs_create_link(hive->kobj, &adev->dev->kobj, node); 204 if (ret) { 205 dev_err(adev->dev, "XGMI: Failed to create link from hive info"); 206 goto remove_link; 207 } 208 209 goto success; 210 211 212remove_link: 213 sysfs_remove_link(&adev->dev->kobj, adev->ddev->unique); 214 215remove_file: 216 device_remove_file(adev->dev, &dev_attr_xgmi_device_id); 217 218success: 219 return ret; 220} 221 222static void amdgpu_xgmi_sysfs_rem_dev_info(struct amdgpu_device *adev, 223 struct amdgpu_hive_info *hive) 224{ 225 device_remove_file(adev->dev, &dev_attr_xgmi_device_id); 226 sysfs_remove_link(&adev->dev->kobj, adev->ddev->unique); 227 sysfs_remove_link(hive->kobj, adev->ddev->unique); 228} 229 230 231 232struct amdgpu_hive_info *amdgpu_get_xgmi_hive(struct amdgpu_device *adev, int lock) 233{ 234 int i; 235 struct amdgpu_hive_info *tmp; 236 237 if (!adev->gmc.xgmi.hive_id) 238 return NULL; 239 240 mutex_lock(&xgmi_mutex); 241 242 for (i = 0 ; i < hive_count; ++i) { 243 tmp = &xgmi_hives[i]; 244 if (tmp->hive_id == adev->gmc.xgmi.hive_id) { 245 if (lock) 246 mutex_lock(&tmp->hive_lock); 247 mutex_unlock(&xgmi_mutex); 248 return tmp; 249 } 250 } 251 if (i >= AMDGPU_MAX_XGMI_HIVE) { 252 mutex_unlock(&xgmi_mutex); 253 return NULL; 254 } 255 256 /* initialize new hive if not exist */ 257 tmp = &xgmi_hives[hive_count++]; 258 259 if (amdgpu_xgmi_sysfs_create(adev, tmp)) { 260 mutex_unlock(&xgmi_mutex); 261 return NULL; 262 } 263 264 tmp->adev = adev; 265 tmp->hive_id = adev->gmc.xgmi.hive_id; 266 INIT_LIST_HEAD(&tmp->device_list); 267 mutex_init(&tmp->hive_lock); 268 mutex_init(&tmp->reset_lock); 269 task_barrier_init(&tmp->tb); 270 271 if (lock) 272 mutex_lock(&tmp->hive_lock); 273 tmp->pstate = -1; 274 mutex_unlock(&xgmi_mutex); 275 276 return tmp; 277} 278 279int amdgpu_xgmi_set_pstate(struct amdgpu_device *adev, int pstate) 280{ 281 int ret = 0; 282 struct amdgpu_hive_info *hive = amdgpu_get_xgmi_hive(adev, 0); 283 struct amdgpu_device *tmp_adev; 284 bool update_hive_pstate = true; 285 bool is_high_pstate = pstate && adev->asic_type == CHIP_VEGA20; 286 287 if (!hive) 288 return 0; 289 290 mutex_lock(&hive->hive_lock); 291 292 if (hive->pstate == pstate) { 293 adev->pstate = is_high_pstate ? pstate : adev->pstate; 294 goto out; 295 } 296 297 dev_dbg(adev->dev, "Set xgmi pstate %d.\n", pstate); 298 299 ret = amdgpu_dpm_set_xgmi_pstate(adev, pstate); 300 if (ret) { 301 dev_err(adev->dev, 302 "XGMI: Set pstate failure on device %llx, hive %llx, ret %d", 303 adev->gmc.xgmi.node_id, 304 adev->gmc.xgmi.hive_id, ret); 305 goto out; 306 } 307 308 /* Update device pstate */ 309 adev->pstate = pstate; 310 311 /* 312 * Update the hive pstate only all devices of the hive 313 * are in the same pstate 314 */ 315 list_for_each_entry(tmp_adev, &hive->device_list, gmc.xgmi.head) { 316 if (tmp_adev->pstate != adev->pstate) { 317 update_hive_pstate = false; 318 break; 319 } 320 } 321 if (update_hive_pstate || is_high_pstate) 322 hive->pstate = pstate; 323 324out: 325 mutex_unlock(&hive->hive_lock); 326 327 return ret; 328} 329 330int amdgpu_xgmi_update_topology(struct amdgpu_hive_info *hive, struct amdgpu_device *adev) 331{ 332 int ret = -EINVAL; 333 334 /* Each psp need to set the latest topology */ 335 ret = psp_xgmi_set_topology_info(&adev->psp, 336 hive->number_devices, 337 &adev->psp.xgmi_context.top_info); 338 if (ret) 339 dev_err(adev->dev, 340 "XGMI: Set topology failure on device %llx, hive %llx, ret %d", 341 adev->gmc.xgmi.node_id, 342 adev->gmc.xgmi.hive_id, ret); 343 344 return ret; 345} 346 347 348int amdgpu_xgmi_get_hops_count(struct amdgpu_device *adev, 349 struct amdgpu_device *peer_adev) 350{ 351 struct psp_xgmi_topology_info *top = &adev->psp.xgmi_context.top_info; 352 int i; 353 354 for (i = 0 ; i < top->num_nodes; ++i) 355 if (top->nodes[i].node_id == peer_adev->gmc.xgmi.node_id) 356 return top->nodes[i].num_hops; 357 return -EINVAL; 358} 359 360int amdgpu_xgmi_add_device(struct amdgpu_device *adev) 361{ 362 struct psp_xgmi_topology_info *top_info; 363 struct amdgpu_hive_info *hive; 364 struct amdgpu_xgmi *entry; 365 struct amdgpu_device *tmp_adev = NULL; 366 367 int count = 0, ret = 0; 368 369 if (!adev->gmc.xgmi.supported) 370 return 0; 371 372 if (amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_PSP)) { 373 ret = psp_xgmi_get_hive_id(&adev->psp, &adev->gmc.xgmi.hive_id); 374 if (ret) { 375 dev_err(adev->dev, 376 "XGMI: Failed to get hive id\n"); 377 return ret; 378 } 379 380 ret = psp_xgmi_get_node_id(&adev->psp, &adev->gmc.xgmi.node_id); 381 if (ret) { 382 dev_err(adev->dev, 383 "XGMI: Failed to get node id\n"); 384 return ret; 385 } 386 } else { 387 adev->gmc.xgmi.hive_id = 16; 388 adev->gmc.xgmi.node_id = adev->gmc.xgmi.physical_node_id + 16; 389 } 390 391 hive = amdgpu_get_xgmi_hive(adev, 1); 392 if (!hive) { 393 ret = -EINVAL; 394 dev_err(adev->dev, 395 "XGMI: node 0x%llx, can not match hive 0x%llx in the hive list.\n", 396 adev->gmc.xgmi.node_id, adev->gmc.xgmi.hive_id); 397 goto exit; 398 } 399 400 /* Set default device pstate */ 401 adev->pstate = -1; 402 403 top_info = &adev->psp.xgmi_context.top_info; 404 405 list_add_tail(&adev->gmc.xgmi.head, &hive->device_list); 406 list_for_each_entry(entry, &hive->device_list, head) 407 top_info->nodes[count++].node_id = entry->node_id; 408 top_info->num_nodes = count; 409 hive->number_devices = count; 410 411 task_barrier_add_task(&hive->tb); 412 413 if (amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_PSP)) { 414 list_for_each_entry(tmp_adev, &hive->device_list, gmc.xgmi.head) { 415 /* update node list for other device in the hive */ 416 if (tmp_adev != adev) { 417 top_info = &tmp_adev->psp.xgmi_context.top_info; 418 top_info->nodes[count - 1].node_id = 419 adev->gmc.xgmi.node_id; 420 top_info->num_nodes = count; 421 } 422 ret = amdgpu_xgmi_update_topology(hive, tmp_adev); 423 if (ret) 424 goto exit; 425 } 426 427 /* get latest topology info for each device from psp */ 428 list_for_each_entry(tmp_adev, &hive->device_list, gmc.xgmi.head) { 429 ret = psp_xgmi_get_topology_info(&tmp_adev->psp, count, 430 &tmp_adev->psp.xgmi_context.top_info); 431 if (ret) { 432 dev_err(tmp_adev->dev, 433 "XGMI: Get topology failure on device %llx, hive %llx, ret %d", 434 tmp_adev->gmc.xgmi.node_id, 435 tmp_adev->gmc.xgmi.hive_id, ret); 436 /* To do : continue with some node failed or disable the whole hive */ 437 goto exit; 438 } 439 } 440 } 441 442 if (!ret) 443 ret = amdgpu_xgmi_sysfs_add_dev_info(adev, hive); 444 445 446 mutex_unlock(&hive->hive_lock); 447exit: 448 if (!ret) 449 dev_info(adev->dev, "XGMI: Add node %d, hive 0x%llx.\n", 450 adev->gmc.xgmi.physical_node_id, adev->gmc.xgmi.hive_id); 451 else 452 dev_err(adev->dev, "XGMI: Failed to add node %d, hive 0x%llx ret: %d\n", 453 adev->gmc.xgmi.physical_node_id, adev->gmc.xgmi.hive_id, 454 ret); 455 456 return ret; 457} 458 459void amdgpu_xgmi_remove_device(struct amdgpu_device *adev) 460{ 461 struct amdgpu_hive_info *hive; 462 463 if (!adev->gmc.xgmi.supported) 464 return; 465 466 hive = amdgpu_get_xgmi_hive(adev, 1); 467 if (!hive) 468 return; 469 470 if (!(hive->number_devices--)) { 471 amdgpu_xgmi_sysfs_destroy(adev, hive); 472 mutex_destroy(&hive->hive_lock); 473 mutex_destroy(&hive->reset_lock); 474 } else { 475 task_barrier_rem_task(&hive->tb); 476 amdgpu_xgmi_sysfs_rem_dev_info(adev, hive); 477 mutex_unlock(&hive->hive_lock); 478 } 479} 480 481int amdgpu_xgmi_ras_late_init(struct amdgpu_device *adev) 482{ 483 int r; 484 struct ras_ih_if ih_info = { 485 .cb = NULL, 486 }; 487 struct ras_fs_if fs_info = { 488 .sysfs_name = "xgmi_wafl_err_count", 489 .debugfs_name = "xgmi_wafl_err_inject", 490 }; 491 492 if (!adev->gmc.xgmi.supported || 493 adev->gmc.xgmi.num_physical_nodes == 0) 494 return 0; 495 496 if (!adev->gmc.xgmi.ras_if) { 497 adev->gmc.xgmi.ras_if = kmalloc(sizeof(struct ras_common_if), GFP_KERNEL); 498 if (!adev->gmc.xgmi.ras_if) 499 return -ENOMEM; 500 adev->gmc.xgmi.ras_if->block = AMDGPU_RAS_BLOCK__XGMI_WAFL; 501 adev->gmc.xgmi.ras_if->type = AMDGPU_RAS_ERROR__MULTI_UNCORRECTABLE; 502 adev->gmc.xgmi.ras_if->sub_block_index = 0; 503 strcpy(adev->gmc.xgmi.ras_if->name, "xgmi_wafl"); 504 } 505 ih_info.head = fs_info.head = *adev->gmc.xgmi.ras_if; 506 r = amdgpu_ras_late_init(adev, adev->gmc.xgmi.ras_if, 507 &fs_info, &ih_info); 508 if (r || !amdgpu_ras_is_supported(adev, adev->gmc.xgmi.ras_if->block)) { 509 kfree(adev->gmc.xgmi.ras_if); 510 adev->gmc.xgmi.ras_if = NULL; 511 } 512 513 return r; 514} 515 516void amdgpu_xgmi_ras_fini(struct amdgpu_device *adev) 517{ 518 if (amdgpu_ras_is_supported(adev, AMDGPU_RAS_BLOCK__XGMI_WAFL) && 519 adev->gmc.xgmi.ras_if) { 520 struct ras_common_if *ras_if = adev->gmc.xgmi.ras_if; 521 struct ras_ih_if ih_info = { 522 .cb = NULL, 523 }; 524 525 amdgpu_ras_late_fini(adev, ras_if, &ih_info); 526 kfree(ras_if); 527 } 528} 529