1 // SPDX-License-Identifier: MIT 2 /* 3 * Copyright © 2025 Intel Corporation 4 */ 5 6 #include <linux/bitops.h> 7 #include <linux/ctype.h> 8 #include <linux/configfs.h> 9 #include <linux/cleanup.h> 10 #include <linux/find.h> 11 #include <linux/init.h> 12 #include <linux/module.h> 13 #include <linux/pci.h> 14 #include <linux/string.h> 15 16 #include "instructions/xe_mi_commands.h" 17 #include "xe_configfs.h" 18 #include "xe_gt_types.h" 19 #include "xe_hw_engine_types.h" 20 #include "xe_module.h" 21 #include "xe_pci_types.h" 22 #include "xe_sriov_types.h" 23 24 /** 25 * DOC: Xe Configfs 26 * 27 * Overview 28 * ======== 29 * 30 * Configfs is a filesystem-based manager of kernel objects. XE KMD registers a 31 * configfs subsystem called ``xe`` that creates a directory in the mounted 32 * configfs directory. The user can create devices under this directory and 33 * configure them as necessary. See Documentation/filesystems/configfs.rst for 34 * more information about how configfs works. 35 * 36 * Create devices 37 * ============== 38 * 39 * To create a device, the ``xe`` module should already be loaded, but some 40 * attributes can only be set before binding the device. It can be accomplished 41 * by blocking the driver autoprobe:: 42 * 43 * # echo 0 > /sys/bus/pci/drivers_autoprobe 44 * # modprobe xe 45 * 46 * In order to create a device, the user has to create a directory inside ``xe``:: 47 * 48 * # mkdir /sys/kernel/config/xe/0000:03:00.0/ 49 * 50 * Every device created is populated by the driver with entries that can be 51 * used to configure it:: 52 * 53 * /sys/kernel/config/xe/ 54 * ├── 0000:00:02.0 55 * │ └── ... 56 * ├── 0000:00:02.1 57 * │ └── ... 58 * : 59 * └── 0000:03:00.0 60 * ├── survivability_mode 61 * ├── gt_types_allowed 62 * ├── engines_allowed 63 * └── enable_psmi 64 * 65 * After configuring the attributes as per next section, the device can be 66 * probed with:: 67 * 68 * # echo 0000:03:00.0 > /sys/bus/pci/drivers/xe/bind 69 * # # or 70 * # echo 0000:03:00.0 > /sys/bus/pci/drivers_probe 71 * 72 * Configure Attributes 73 * ==================== 74 * 75 * Survivability mode: 76 * ------------------- 77 * 78 * Enable survivability mode on supported cards. This setting only takes 79 * effect when probing the device. Example to enable it:: 80 * 81 * # echo 1 > /sys/kernel/config/xe/0000:03:00.0/survivability_mode 82 * 83 * This attribute can only be set before binding to the device. 84 * 85 * Allowed GT types: 86 * ----------------- 87 * 88 * Allow only specific types of GTs to be detected and initialized by the 89 * driver. Any combination of GT types can be enabled/disabled, although 90 * some settings will cause the device to fail to probe. 91 * 92 * Writes support both comma- and newline-separated input format. Reads 93 * will always return one GT type per line. "primary" and "media" are the 94 * GT type names supported by this interface. 95 * 96 * This attribute can only be set before binding to the device. 97 * 98 * Examples: 99 * 100 * Allow both primary and media GTs to be initialized and used. This matches 101 * the driver's default behavior:: 102 * 103 * # echo 'primary,media' > /sys/kernel/config/xe/0000:03:00.0/gt_types_allowed 104 * 105 * Allow only the primary GT of each tile to be initialized and used, 106 * effectively disabling the media GT if it exists on the platform:: 107 * 108 * # echo 'primary' > /sys/kernel/config/xe/0000:03:00.0/gt_types_allowed 109 * 110 * Allow only the media GT of each tile to be initialized and used, 111 * effectively disabling the primary GT. **This configuration will cause 112 * device probe failure on all current platforms, but may be allowed on 113 * igpu platforms in the future**:: 114 * 115 * # echo 'media' > /sys/kernel/config/xe/0000:03:00.0/gt_types_allowed 116 * 117 * Disable all GTs. Only other GPU IP (such as display) is potentially usable. 118 * **This configuration will cause device probe failure on all current 119 * platforms, but may be allowed on igpu platforms in the future**:: 120 * 121 * # echo '' > /sys/kernel/config/xe/0000:03:00.0/gt_types_allowed 122 * 123 * Allowed engines: 124 * ---------------- 125 * 126 * Allow only a set of engine(s) to be available, disabling the other engines 127 * even if they are available in hardware. This is applied after HW fuses are 128 * considered on each tile. Examples: 129 * 130 * Allow only one render and one copy engines, nothing else:: 131 * 132 * # echo 'rcs0,bcs0' > /sys/kernel/config/xe/0000:03:00.0/engines_allowed 133 * 134 * Allow only compute engines and first copy engine:: 135 * 136 * # echo 'ccs*,bcs0' > /sys/kernel/config/xe/0000:03:00.0/engines_allowed 137 * 138 * Note that the engine names are the per-GT hardware names. On multi-tile 139 * platforms, writing ``rcs0,bcs0`` to this file would allow the first render 140 * and copy engines on each tile. 141 * 142 * The requested configuration may not be supported by the platform and driver 143 * may fail to probe. For example: if at least one copy engine is expected to be 144 * available for migrations, but it's disabled. This is intended for debugging 145 * purposes only. 146 * 147 * This attribute can only be set before binding to the device. 148 * 149 * PSMI 150 * ---- 151 * 152 * Enable extra debugging capabilities to trace engine execution. Only useful 153 * during early platform enabling and requires additional hardware connected. 154 * Once it's enabled, additionals WAs are added and runtime configuration is 155 * done via debugfs. Example to enable it:: 156 * 157 * # echo 1 > /sys/kernel/config/xe/0000:03:00.0/enable_psmi 158 * 159 * This attribute can only be set before binding to the device. 160 * 161 * Context restore BB 162 * ------------------ 163 * 164 * Allow to execute a batch buffer during any context switches. When the 165 * GPU is restoring the context, it executes additional commands. It's useful 166 * for testing additional workarounds and validating certain HW behaviors: it's 167 * not intended for normal execution and will taint the kernel with TAINT_TEST 168 * when used. 169 * 170 * The syntax allows to pass straight instructions to be executed by the engine 171 * in a batch buffer or set specific registers. 172 * 173 * #. Generic instruction:: 174 * 175 * <engine-class> cmd <instr> [[dword0] [dword1] [...]] 176 * 177 * #. Simple register setting:: 178 * 179 * <engine-class> reg <address> <value> 180 * 181 * Commands are saved per engine class: all instances of that class will execute 182 * those commands during context switch. The instruction, dword arguments, 183 * addresses and values are in hex format like in the examples below. 184 * 185 * #. Execute a LRI command to write 0xDEADBEEF to register 0x4f10 after the 186 * normal context restore:: 187 * 188 * # echo 'rcs cmd 11000001 4F100 DEADBEEF' \ 189 * > /sys/kernel/config/xe/0000:03:00.0/ctx_restore_post_bb 190 * 191 * #. Execute a LRI command to write 0xDEADBEEF to register 0x4f10 at the 192 * beginning of the context restore:: 193 * 194 * # echo 'rcs cmd 11000001 4F100 DEADBEEF' \ 195 * > /sys/kernel/config/xe/0000:03:00.0/ctx_restore_mid_bb 196 197 * #. Load certain values in a couple of registers (it can be used as a simpler 198 * alternative to the `cmd`) action:: 199 * 200 * # cat > /sys/kernel/config/xe/0000:03:00.0/ctx_restore_post_bb <<EOF 201 * rcs reg 4F100 DEADBEEF 202 * rcs reg 4F104 FFFFFFFF 203 * EOF 204 * 205 * .. note:: 206 * 207 * When using multiple lines, make sure to use a command that is 208 * implemented with a single write syscall, like HEREDOC. 209 * 210 * Currently this is implemented only for post and mid context restore and 211 * these attributes can only be set before binding to the device. 212 * 213 * Max SR-IOV Virtual Functions 214 * ---------------------------- 215 * 216 * This config allows to limit number of the Virtual Functions (VFs) that can 217 * be managed by the Physical Function (PF) driver, where value 0 disables the 218 * PF mode (no VFs). 219 * 220 * The default max_vfs config value is taken from the max_vfs modparam. 221 * 222 * How to enable PF with support with unlimited (up to HW limit) number of VFs:: 223 * 224 * # echo unlimited > /sys/kernel/config/xe/0000:00:02.0/sriov/max_vfs 225 * # echo 0000:00:02.0 > /sys/bus/pci/drivers/xe/bind 226 * 227 * How to enable PF with support up to 3 VFs:: 228 * 229 * # echo 3 > /sys/kernel/config/xe/0000:00:02.0/sriov/max_vfs 230 * # echo 0000:00:02.0 > /sys/bus/pci/drivers/xe/bind 231 * 232 * How to disable PF mode and always run as native:: 233 * 234 * # echo 0 > /sys/kernel/config/xe/0000:00:02.0/sriov/max_vfs 235 * # echo 0000:00:02.0 > /sys/bus/pci/drivers/xe/bind 236 * 237 * This setting only takes effect when probing the device. 238 * 239 * Remove devices 240 * ============== 241 * 242 * The created device directories can be removed using ``rmdir``:: 243 * 244 * # rmdir /sys/kernel/config/xe/0000:03:00.0/ 245 */ 246 247 /* Similar to struct xe_bb, but not tied to HW (yet) */ 248 struct wa_bb { 249 u32 *cs; 250 u32 len; /* in dwords */ 251 }; 252 253 struct xe_config_group_device { 254 struct config_group group; 255 struct config_group sriov; 256 257 struct xe_config_device { 258 u64 gt_types_allowed; 259 u64 engines_allowed; 260 struct wa_bb ctx_restore_post_bb[XE_ENGINE_CLASS_MAX]; 261 struct wa_bb ctx_restore_mid_bb[XE_ENGINE_CLASS_MAX]; 262 bool survivability_mode; 263 bool enable_psmi; 264 struct { 265 unsigned int max_vfs; 266 } sriov; 267 } config; 268 269 /* protects attributes */ 270 struct mutex lock; 271 /* matching descriptor */ 272 const struct xe_device_desc *desc; 273 /* tentative SR-IOV mode */ 274 enum xe_sriov_mode mode; 275 }; 276 277 static const struct xe_config_device device_defaults = { 278 .gt_types_allowed = U64_MAX, 279 .engines_allowed = U64_MAX, 280 .survivability_mode = false, 281 .enable_psmi = false, 282 .sriov = { 283 .max_vfs = UINT_MAX, 284 }, 285 }; 286 287 static void set_device_defaults(struct xe_config_device *config) 288 { 289 *config = device_defaults; 290 #ifdef CONFIG_PCI_IOV 291 config->sriov.max_vfs = xe_modparam.max_vfs; 292 #endif 293 } 294 295 struct engine_info { 296 const char *cls; 297 u64 mask; 298 enum xe_engine_class engine_class; 299 }; 300 301 /* Some helpful macros to aid on the sizing of buffer allocation when parsing */ 302 #define MAX_ENGINE_CLASS_CHARS 5 303 #define MAX_ENGINE_INSTANCE_CHARS 2 304 #define MAX_GT_TYPE_CHARS 7 305 306 static const struct engine_info engine_info[] = { 307 { .cls = "rcs", .mask = XE_HW_ENGINE_RCS_MASK, .engine_class = XE_ENGINE_CLASS_RENDER }, 308 { .cls = "bcs", .mask = XE_HW_ENGINE_BCS_MASK, .engine_class = XE_ENGINE_CLASS_COPY }, 309 { .cls = "vcs", .mask = XE_HW_ENGINE_VCS_MASK, .engine_class = XE_ENGINE_CLASS_VIDEO_DECODE }, 310 { .cls = "vecs", .mask = XE_HW_ENGINE_VECS_MASK, .engine_class = XE_ENGINE_CLASS_VIDEO_ENHANCE }, 311 { .cls = "ccs", .mask = XE_HW_ENGINE_CCS_MASK, .engine_class = XE_ENGINE_CLASS_COMPUTE }, 312 { .cls = "gsccs", .mask = XE_HW_ENGINE_GSCCS_MASK, .engine_class = XE_ENGINE_CLASS_OTHER }, 313 }; 314 315 static const struct { 316 const char name[MAX_GT_TYPE_CHARS + 1]; 317 enum xe_gt_type type; 318 } gt_types[] = { 319 { .name = "primary", .type = XE_GT_TYPE_MAIN }, 320 { .name = "media", .type = XE_GT_TYPE_MEDIA }, 321 }; 322 323 static struct xe_config_group_device *to_xe_config_group_device(struct config_item *item) 324 { 325 return container_of(to_config_group(item), struct xe_config_group_device, group); 326 } 327 328 static struct xe_config_device *to_xe_config_device(struct config_item *item) 329 { 330 return &to_xe_config_group_device(item)->config; 331 } 332 333 static bool is_bound(struct xe_config_group_device *dev) 334 { 335 unsigned int domain, bus, slot, function; 336 struct pci_dev *pdev; 337 const char *name; 338 bool ret; 339 340 lockdep_assert_held(&dev->lock); 341 342 name = dev->group.cg_item.ci_name; 343 if (sscanf(name, "%x:%x:%x.%x", &domain, &bus, &slot, &function) != 4) 344 return false; 345 346 pdev = pci_get_domain_bus_and_slot(domain, bus, PCI_DEVFN(slot, function)); 347 if (!pdev) 348 return false; 349 350 ret = pci_get_drvdata(pdev); 351 pci_dev_put(pdev); 352 353 if (ret) 354 pci_dbg(pdev, "Already bound to driver\n"); 355 356 return ret; 357 } 358 359 static ssize_t survivability_mode_show(struct config_item *item, char *page) 360 { 361 struct xe_config_device *dev = to_xe_config_device(item); 362 363 return sprintf(page, "%d\n", dev->survivability_mode); 364 } 365 366 static ssize_t survivability_mode_store(struct config_item *item, const char *page, size_t len) 367 { 368 struct xe_config_group_device *dev = to_xe_config_group_device(item); 369 bool survivability_mode; 370 int ret; 371 372 ret = kstrtobool(page, &survivability_mode); 373 if (ret) 374 return ret; 375 376 guard(mutex)(&dev->lock); 377 if (is_bound(dev)) 378 return -EBUSY; 379 380 dev->config.survivability_mode = survivability_mode; 381 382 return len; 383 } 384 385 static ssize_t gt_types_allowed_show(struct config_item *item, char *page) 386 { 387 struct xe_config_device *dev = to_xe_config_device(item); 388 char *p = page; 389 390 for (size_t i = 0; i < ARRAY_SIZE(gt_types); i++) 391 if (dev->gt_types_allowed & BIT_ULL(gt_types[i].type)) 392 p += sprintf(p, "%s\n", gt_types[i].name); 393 394 return p - page; 395 } 396 397 static ssize_t gt_types_allowed_store(struct config_item *item, const char *page, 398 size_t len) 399 { 400 struct xe_config_group_device *dev = to_xe_config_group_device(item); 401 char *buf __free(kfree) = kstrdup(page, GFP_KERNEL); 402 char *p = buf; 403 u64 typemask = 0; 404 405 if (!buf) 406 return -ENOMEM; 407 408 while (p) { 409 char *typename = strsep(&p, ",\n"); 410 bool matched = false; 411 412 if (typename[0] == '\0') 413 continue; 414 415 for (size_t i = 0; i < ARRAY_SIZE(gt_types); i++) { 416 if (strcmp(typename, gt_types[i].name) == 0) { 417 typemask |= BIT(gt_types[i].type); 418 matched = true; 419 break; 420 } 421 } 422 423 if (!matched) 424 return -EINVAL; 425 } 426 427 guard(mutex)(&dev->lock); 428 if (is_bound(dev)) 429 return -EBUSY; 430 431 dev->config.gt_types_allowed = typemask; 432 433 return len; 434 } 435 436 static ssize_t engines_allowed_show(struct config_item *item, char *page) 437 { 438 struct xe_config_device *dev = to_xe_config_device(item); 439 char *p = page; 440 441 for (size_t i = 0; i < ARRAY_SIZE(engine_info); i++) { 442 u64 mask = engine_info[i].mask; 443 444 if ((dev->engines_allowed & mask) == mask) { 445 p += sprintf(p, "%s*\n", engine_info[i].cls); 446 } else if (mask & dev->engines_allowed) { 447 u16 bit0 = __ffs64(mask), bit; 448 449 mask &= dev->engines_allowed; 450 451 for_each_set_bit(bit, (const unsigned long *)&mask, 64) 452 p += sprintf(p, "%s%u\n", engine_info[i].cls, 453 bit - bit0); 454 } 455 } 456 457 return p - page; 458 } 459 460 /* 461 * Lookup engine_info. If @mask is not NULL, reduce the mask according to the 462 * instance in @pattern. 463 * 464 * Examples of inputs: 465 * - lookup_engine_info("rcs0", &mask): return "rcs" entry from @engine_info and 466 * mask == BIT_ULL(XE_HW_ENGINE_RCS0) 467 * - lookup_engine_info("rcs*", &mask): return "rcs" entry from @engine_info and 468 * mask == XE_HW_ENGINE_RCS_MASK 469 * - lookup_engine_info("rcs", NULL): return "rcs" entry from @engine_info 470 */ 471 static const struct engine_info *lookup_engine_info(const char *pattern, u64 *mask) 472 { 473 for (size_t i = 0; i < ARRAY_SIZE(engine_info); i++) { 474 u8 instance; 475 u16 bit; 476 477 if (!str_has_prefix(pattern, engine_info[i].cls)) 478 continue; 479 480 pattern += strlen(engine_info[i].cls); 481 if (!mask) 482 return *pattern ? NULL : &engine_info[i]; 483 484 if (!strcmp(pattern, "*")) { 485 *mask = engine_info[i].mask; 486 return &engine_info[i]; 487 } 488 489 if (kstrtou8(pattern, 10, &instance)) 490 return NULL; 491 492 bit = __ffs64(engine_info[i].mask) + instance; 493 if (bit >= fls64(engine_info[i].mask)) 494 return NULL; 495 496 *mask = BIT_ULL(bit); 497 return &engine_info[i]; 498 } 499 500 return NULL; 501 } 502 503 static int parse_engine(const char *s, const char *end_chars, u64 *mask, 504 const struct engine_info **pinfo) 505 { 506 char buf[MAX_ENGINE_CLASS_CHARS + MAX_ENGINE_INSTANCE_CHARS + 1]; 507 const struct engine_info *info; 508 size_t len; 509 510 len = strcspn(s, end_chars); 511 if (len >= sizeof(buf)) 512 return -EINVAL; 513 514 memcpy(buf, s, len); 515 buf[len] = '\0'; 516 517 info = lookup_engine_info(buf, mask); 518 if (!info) 519 return -ENOENT; 520 521 if (pinfo) 522 *pinfo = info; 523 524 return len; 525 } 526 527 static ssize_t engines_allowed_store(struct config_item *item, const char *page, 528 size_t len) 529 { 530 struct xe_config_group_device *dev = to_xe_config_group_device(item); 531 ssize_t patternlen, p; 532 u64 mask, val = 0; 533 534 for (p = 0; p < len; p += patternlen + 1) { 535 patternlen = parse_engine(page + p, ",\n", &mask, NULL); 536 if (patternlen < 0) 537 return -EINVAL; 538 539 val |= mask; 540 } 541 542 guard(mutex)(&dev->lock); 543 if (is_bound(dev)) 544 return -EBUSY; 545 546 dev->config.engines_allowed = val; 547 548 return len; 549 } 550 551 static ssize_t enable_psmi_show(struct config_item *item, char *page) 552 { 553 struct xe_config_device *dev = to_xe_config_device(item); 554 555 return sprintf(page, "%d\n", dev->enable_psmi); 556 } 557 558 static ssize_t enable_psmi_store(struct config_item *item, const char *page, size_t len) 559 { 560 struct xe_config_group_device *dev = to_xe_config_group_device(item); 561 bool val; 562 int ret; 563 564 ret = kstrtobool(page, &val); 565 if (ret) 566 return ret; 567 568 guard(mutex)(&dev->lock); 569 if (is_bound(dev)) 570 return -EBUSY; 571 572 dev->config.enable_psmi = val; 573 574 return len; 575 } 576 577 static bool wa_bb_read_advance(bool dereference, char **p, 578 const char *append, size_t len, 579 size_t *max_size) 580 { 581 if (dereference) { 582 if (len >= *max_size) 583 return false; 584 *max_size -= len; 585 if (append) 586 memcpy(*p, append, len); 587 } 588 589 *p += len; 590 591 return true; 592 } 593 594 static ssize_t wa_bb_show(struct xe_config_group_device *dev, 595 struct wa_bb wa_bb[static XE_ENGINE_CLASS_MAX], 596 char *data, size_t sz) 597 { 598 char *p = data; 599 600 guard(mutex)(&dev->lock); 601 602 for (size_t i = 0; i < ARRAY_SIZE(engine_info); i++) { 603 enum xe_engine_class ec = engine_info[i].engine_class; 604 size_t len; 605 606 if (!wa_bb[ec].len) 607 continue; 608 609 len = snprintf(p, sz, "%s:", engine_info[i].cls); 610 if (!wa_bb_read_advance(data, &p, NULL, len, &sz)) 611 return -ENOBUFS; 612 613 for (size_t j = 0; j < wa_bb[ec].len; j++) { 614 len = snprintf(p, sz, " %08x", wa_bb[ec].cs[j]); 615 if (!wa_bb_read_advance(data, &p, NULL, len, &sz)) 616 return -ENOBUFS; 617 } 618 619 if (!wa_bb_read_advance(data, &p, "\n", 1, &sz)) 620 return -ENOBUFS; 621 } 622 623 if (!wa_bb_read_advance(data, &p, "", 1, &sz)) 624 return -ENOBUFS; 625 626 /* Reserve one more to match check for '\0' */ 627 if (!data) 628 p++; 629 630 return p - data; 631 } 632 633 static ssize_t ctx_restore_mid_bb_show(struct config_item *item, char *page) 634 { 635 struct xe_config_group_device *dev = to_xe_config_group_device(item); 636 637 return wa_bb_show(dev, dev->config.ctx_restore_mid_bb, page, SZ_4K); 638 } 639 640 static ssize_t ctx_restore_post_bb_show(struct config_item *item, char *page) 641 { 642 struct xe_config_group_device *dev = to_xe_config_group_device(item); 643 644 return wa_bb_show(dev, dev->config.ctx_restore_post_bb, page, SZ_4K); 645 } 646 647 static void wa_bb_append(struct wa_bb *wa_bb, u32 val) 648 { 649 if (wa_bb->cs) 650 wa_bb->cs[wa_bb->len] = val; 651 652 wa_bb->len++; 653 } 654 655 static ssize_t parse_hex(const char *line, u32 *pval) 656 { 657 char numstr[12]; 658 const char *p; 659 ssize_t numlen; 660 661 p = line + strspn(line, " \t"); 662 if (!*p || *p == '\n') 663 return 0; 664 665 numlen = strcspn(p, " \t\n"); 666 if (!numlen || numlen >= sizeof(numstr) - 1) 667 return -EINVAL; 668 669 memcpy(numstr, p, numlen); 670 numstr[numlen] = '\0'; 671 p += numlen; 672 673 if (kstrtou32(numstr, 16, pval)) 674 return -EINVAL; 675 676 return p - line; 677 } 678 679 /* 680 * Parse lines with the format 681 * 682 * <engine-class> cmd <u32> <u32...> 683 * <engine-class> reg <u32_addr> <u32_val> 684 * 685 * and optionally save them in @wa_bb[i].cs is non-NULL. 686 * 687 * Return the number of dwords parsed. 688 */ 689 static ssize_t parse_wa_bb_lines(const char *lines, 690 struct wa_bb wa_bb[static XE_ENGINE_CLASS_MAX]) 691 { 692 ssize_t dwords = 0, ret; 693 const char *p; 694 695 for (p = lines; *p; p++) { 696 const struct engine_info *info = NULL; 697 u32 val, val2; 698 699 /* Also allow empty lines */ 700 p += strspn(p, " \t\n"); 701 if (!*p) 702 break; 703 704 ret = parse_engine(p, " \t\n", NULL, &info); 705 if (ret < 0) 706 return ret; 707 708 p += ret; 709 p += strspn(p, " \t"); 710 711 if (str_has_prefix(p, "cmd")) { 712 for (p += strlen("cmd"); *p;) { 713 ret = parse_hex(p, &val); 714 if (ret < 0) 715 return -EINVAL; 716 if (!ret) 717 break; 718 719 p += ret; 720 dwords++; 721 wa_bb_append(&wa_bb[info->engine_class], val); 722 } 723 } else if (str_has_prefix(p, "reg")) { 724 p += strlen("reg"); 725 ret = parse_hex(p, &val); 726 if (ret <= 0) 727 return -EINVAL; 728 729 p += ret; 730 ret = parse_hex(p, &val2); 731 if (ret <= 0) 732 return -EINVAL; 733 734 p += ret; 735 dwords += 3; 736 wa_bb_append(&wa_bb[info->engine_class], 737 MI_LOAD_REGISTER_IMM | MI_LRI_NUM_REGS(1)); 738 wa_bb_append(&wa_bb[info->engine_class], val); 739 wa_bb_append(&wa_bb[info->engine_class], val2); 740 } else { 741 return -EINVAL; 742 } 743 } 744 745 return dwords; 746 } 747 748 static ssize_t wa_bb_store(struct wa_bb wa_bb[static XE_ENGINE_CLASS_MAX], 749 struct xe_config_group_device *dev, 750 const char *page, size_t len) 751 { 752 /* tmp_wa_bb must match wa_bb's size */ 753 struct wa_bb tmp_wa_bb[XE_ENGINE_CLASS_MAX] = { }; 754 ssize_t count, class; 755 u32 *tmp; 756 757 /* 1. Count dwords - wa_bb[i].cs is NULL for all classes */ 758 count = parse_wa_bb_lines(page, tmp_wa_bb); 759 if (count < 0) 760 return count; 761 762 guard(mutex)(&dev->lock); 763 764 if (is_bound(dev)) 765 return -EBUSY; 766 767 /* 768 * 2. Allocate a u32 array and set the pointers to the right positions 769 * according to the length of each class' wa_bb 770 */ 771 tmp = krealloc(wa_bb[0].cs, count * sizeof(u32), GFP_KERNEL); 772 if (!tmp) 773 return -ENOMEM; 774 775 if (!count) { 776 memset(wa_bb, 0, sizeof(tmp_wa_bb)); 777 return len; 778 } 779 780 for (class = 0, count = 0; class < XE_ENGINE_CLASS_MAX; ++class) { 781 tmp_wa_bb[class].cs = tmp + count; 782 count += tmp_wa_bb[class].len; 783 tmp_wa_bb[class].len = 0; 784 } 785 786 /* 3. Parse wa_bb lines again, this time saving the values */ 787 count = parse_wa_bb_lines(page, tmp_wa_bb); 788 if (count < 0) 789 return count; 790 791 memcpy(wa_bb, tmp_wa_bb, sizeof(tmp_wa_bb)); 792 793 return len; 794 } 795 796 static ssize_t ctx_restore_mid_bb_store(struct config_item *item, 797 const char *data, size_t sz) 798 { 799 struct xe_config_group_device *dev = to_xe_config_group_device(item); 800 801 return wa_bb_store(dev->config.ctx_restore_mid_bb, dev, data, sz); 802 } 803 804 static ssize_t ctx_restore_post_bb_store(struct config_item *item, 805 const char *data, size_t sz) 806 { 807 struct xe_config_group_device *dev = to_xe_config_group_device(item); 808 809 return wa_bb_store(dev->config.ctx_restore_post_bb, dev, data, sz); 810 } 811 812 CONFIGFS_ATTR(, ctx_restore_mid_bb); 813 CONFIGFS_ATTR(, ctx_restore_post_bb); 814 CONFIGFS_ATTR(, enable_psmi); 815 CONFIGFS_ATTR(, engines_allowed); 816 CONFIGFS_ATTR(, gt_types_allowed); 817 CONFIGFS_ATTR(, survivability_mode); 818 819 static struct configfs_attribute *xe_config_device_attrs[] = { 820 &attr_ctx_restore_mid_bb, 821 &attr_ctx_restore_post_bb, 822 &attr_enable_psmi, 823 &attr_engines_allowed, 824 &attr_gt_types_allowed, 825 &attr_survivability_mode, 826 NULL, 827 }; 828 829 static void xe_config_device_release(struct config_item *item) 830 { 831 struct xe_config_group_device *dev = to_xe_config_group_device(item); 832 833 mutex_destroy(&dev->lock); 834 835 kfree(dev->config.ctx_restore_post_bb[0].cs); 836 kfree(dev); 837 } 838 839 static struct configfs_item_operations xe_config_device_ops = { 840 .release = xe_config_device_release, 841 }; 842 843 static bool xe_config_device_is_visible(struct config_item *item, 844 struct configfs_attribute *attr, int n) 845 { 846 struct xe_config_group_device *dev = to_xe_config_group_device(item); 847 848 if (attr == &attr_survivability_mode) { 849 if (!dev->desc->is_dgfx || dev->desc->platform < XE_BATTLEMAGE) 850 return false; 851 } 852 853 return true; 854 } 855 856 static struct configfs_group_operations xe_config_device_group_ops = { 857 .is_visible = xe_config_device_is_visible, 858 }; 859 860 static const struct config_item_type xe_config_device_type = { 861 .ct_item_ops = &xe_config_device_ops, 862 .ct_group_ops = &xe_config_device_group_ops, 863 .ct_attrs = xe_config_device_attrs, 864 .ct_owner = THIS_MODULE, 865 }; 866 867 static ssize_t sriov_max_vfs_show(struct config_item *item, char *page) 868 { 869 struct xe_config_group_device *dev = to_xe_config_group_device(item->ci_parent); 870 871 guard(mutex)(&dev->lock); 872 873 if (dev->config.sriov.max_vfs == UINT_MAX) 874 return sprintf(page, "%s\n", "unlimited"); 875 else 876 return sprintf(page, "%u\n", dev->config.sriov.max_vfs); 877 } 878 879 static ssize_t sriov_max_vfs_store(struct config_item *item, const char *page, size_t len) 880 { 881 struct xe_config_group_device *dev = to_xe_config_group_device(item->ci_parent); 882 unsigned int max_vfs; 883 int ret; 884 885 guard(mutex)(&dev->lock); 886 887 if (is_bound(dev)) 888 return -EBUSY; 889 890 ret = kstrtouint(page, 0, &max_vfs); 891 if (ret) { 892 if (!sysfs_streq(page, "unlimited")) 893 return ret; 894 max_vfs = UINT_MAX; 895 } 896 897 dev->config.sriov.max_vfs = max_vfs; 898 return len; 899 } 900 901 CONFIGFS_ATTR(sriov_, max_vfs); 902 903 static struct configfs_attribute *xe_config_sriov_attrs[] = { 904 &sriov_attr_max_vfs, 905 NULL, 906 }; 907 908 static bool xe_config_sriov_is_visible(struct config_item *item, 909 struct configfs_attribute *attr, int n) 910 { 911 struct xe_config_group_device *dev = to_xe_config_group_device(item->ci_parent); 912 913 if (attr == &sriov_attr_max_vfs && dev->mode != XE_SRIOV_MODE_PF) 914 return false; 915 916 return true; 917 } 918 919 static struct configfs_group_operations xe_config_sriov_group_ops = { 920 .is_visible = xe_config_sriov_is_visible, 921 }; 922 923 static const struct config_item_type xe_config_sriov_type = { 924 .ct_owner = THIS_MODULE, 925 .ct_group_ops = &xe_config_sriov_group_ops, 926 .ct_attrs = xe_config_sriov_attrs, 927 }; 928 929 static const struct xe_device_desc *xe_match_desc(struct pci_dev *pdev) 930 { 931 struct device_driver *driver = driver_find("xe", &pci_bus_type); 932 struct pci_driver *drv = to_pci_driver(driver); 933 const struct pci_device_id *ids = drv ? drv->id_table : NULL; 934 const struct pci_device_id *found = pci_match_id(ids, pdev); 935 936 return found ? (const void *)found->driver_data : NULL; 937 } 938 939 static struct pci_dev *get_physfn_instead(struct pci_dev *virtfn) 940 { 941 struct pci_dev *physfn = pci_physfn(virtfn); 942 943 pci_dev_get(physfn); 944 pci_dev_put(virtfn); 945 return physfn; 946 } 947 948 static struct config_group *xe_config_make_device_group(struct config_group *group, 949 const char *name) 950 { 951 unsigned int domain, bus, slot, function; 952 struct xe_config_group_device *dev; 953 const struct xe_device_desc *match; 954 enum xe_sriov_mode mode; 955 struct pci_dev *pdev; 956 char canonical[16]; 957 int vfnumber = 0; 958 int ret; 959 960 ret = sscanf(name, "%x:%x:%x.%x", &domain, &bus, &slot, &function); 961 if (ret != 4) 962 return ERR_PTR(-EINVAL); 963 964 ret = scnprintf(canonical, sizeof(canonical), "%04x:%02x:%02x.%d", domain, bus, 965 PCI_SLOT(PCI_DEVFN(slot, function)), 966 PCI_FUNC(PCI_DEVFN(slot, function))); 967 if (ret != 12 || strcmp(name, canonical)) 968 return ERR_PTR(-EINVAL); 969 970 pdev = pci_get_domain_bus_and_slot(domain, bus, PCI_DEVFN(slot, function)); 971 mode = pdev ? dev_is_pf(&pdev->dev) ? 972 XE_SRIOV_MODE_PF : XE_SRIOV_MODE_NONE : XE_SRIOV_MODE_VF; 973 974 if (!pdev && function) 975 pdev = pci_get_domain_bus_and_slot(domain, bus, PCI_DEVFN(slot, 0)); 976 if (!pdev && slot) 977 pdev = pci_get_domain_bus_and_slot(domain, bus, PCI_DEVFN(0, 0)); 978 if (!pdev) 979 return ERR_PTR(-ENODEV); 980 981 if (PCI_DEVFN(slot, function) != pdev->devfn) { 982 pdev = get_physfn_instead(pdev); 983 vfnumber = PCI_DEVFN(slot, function) - pdev->devfn; 984 if (!dev_is_pf(&pdev->dev) || vfnumber > pci_sriov_get_totalvfs(pdev)) { 985 pci_dev_put(pdev); 986 return ERR_PTR(-ENODEV); 987 } 988 } 989 990 match = xe_match_desc(pdev); 991 if (match && vfnumber && !match->has_sriov) { 992 pci_info(pdev, "xe driver does not support VFs on this device\n"); 993 match = NULL; 994 } else if (!match) { 995 pci_info(pdev, "xe driver does not support configuration of this device\n"); 996 } 997 998 pci_dev_put(pdev); 999 1000 if (!match) 1001 return ERR_PTR(-ENOENT); 1002 1003 dev = kzalloc(sizeof(*dev), GFP_KERNEL); 1004 if (!dev) 1005 return ERR_PTR(-ENOMEM); 1006 1007 dev->desc = match; 1008 dev->mode = match->has_sriov ? mode : XE_SRIOV_MODE_NONE; 1009 1010 set_device_defaults(&dev->config); 1011 1012 config_group_init_type_name(&dev->group, name, &xe_config_device_type); 1013 if (dev->mode != XE_SRIOV_MODE_NONE) { 1014 config_group_init_type_name(&dev->sriov, "sriov", &xe_config_sriov_type); 1015 configfs_add_default_group(&dev->sriov, &dev->group); 1016 } 1017 1018 mutex_init(&dev->lock); 1019 1020 return &dev->group; 1021 } 1022 1023 static struct configfs_group_operations xe_config_group_ops = { 1024 .make_group = xe_config_make_device_group, 1025 }; 1026 1027 static const struct config_item_type xe_configfs_type = { 1028 .ct_group_ops = &xe_config_group_ops, 1029 .ct_owner = THIS_MODULE, 1030 }; 1031 1032 static struct configfs_subsystem xe_configfs = { 1033 .su_group = { 1034 .cg_item = { 1035 .ci_namebuf = "xe", 1036 .ci_type = &xe_configfs_type, 1037 }, 1038 }, 1039 }; 1040 1041 static struct xe_config_group_device *find_xe_config_group_device(struct pci_dev *pdev) 1042 { 1043 struct config_item *item; 1044 1045 mutex_lock(&xe_configfs.su_mutex); 1046 item = config_group_find_item(&xe_configfs.su_group, pci_name(pdev)); 1047 mutex_unlock(&xe_configfs.su_mutex); 1048 1049 if (!item) 1050 return NULL; 1051 1052 return to_xe_config_group_device(item); 1053 } 1054 1055 static void dump_custom_dev_config(struct pci_dev *pdev, 1056 struct xe_config_group_device *dev) 1057 { 1058 #define PRI_CUSTOM_ATTR(fmt_, attr_) do { \ 1059 if (dev->config.attr_ != device_defaults.attr_) \ 1060 pci_info(pdev, "configfs: " __stringify(attr_) " = " fmt_ "\n", \ 1061 dev->config.attr_); \ 1062 } while (0) 1063 1064 PRI_CUSTOM_ATTR("%llx", gt_types_allowed); 1065 PRI_CUSTOM_ATTR("%llx", engines_allowed); 1066 PRI_CUSTOM_ATTR("%d", enable_psmi); 1067 PRI_CUSTOM_ATTR("%d", survivability_mode); 1068 1069 #undef PRI_CUSTOM_ATTR 1070 } 1071 1072 /** 1073 * xe_configfs_check_device() - Test if device was configured by configfs 1074 * @pdev: the &pci_dev device to test 1075 * 1076 * Try to find the configfs group that belongs to the specified pci device 1077 * and print a diagnostic message if different than the default value. 1078 */ 1079 void xe_configfs_check_device(struct pci_dev *pdev) 1080 { 1081 struct xe_config_group_device *dev = find_xe_config_group_device(pdev); 1082 1083 if (!dev) 1084 return; 1085 1086 /* memcmp here is safe as both are zero-initialized */ 1087 if (memcmp(&dev->config, &device_defaults, sizeof(dev->config))) { 1088 pci_info(pdev, "Found custom settings in configfs\n"); 1089 dump_custom_dev_config(pdev, dev); 1090 } 1091 1092 config_group_put(&dev->group); 1093 } 1094 1095 /** 1096 * xe_configfs_get_survivability_mode - get configfs survivability mode attribute 1097 * @pdev: pci device 1098 * 1099 * Return: survivability_mode attribute in configfs 1100 */ 1101 bool xe_configfs_get_survivability_mode(struct pci_dev *pdev) 1102 { 1103 struct xe_config_group_device *dev = find_xe_config_group_device(pdev); 1104 bool mode; 1105 1106 if (!dev) 1107 return device_defaults.survivability_mode; 1108 1109 mode = dev->config.survivability_mode; 1110 config_group_put(&dev->group); 1111 1112 return mode; 1113 } 1114 1115 static u64 get_gt_types_allowed(struct pci_dev *pdev) 1116 { 1117 struct xe_config_group_device *dev = find_xe_config_group_device(pdev); 1118 u64 mask; 1119 1120 if (!dev) 1121 return device_defaults.gt_types_allowed; 1122 1123 mask = dev->config.gt_types_allowed; 1124 config_group_put(&dev->group); 1125 1126 return mask; 1127 } 1128 1129 /** 1130 * xe_configfs_primary_gt_allowed - determine whether primary GTs are supported 1131 * @pdev: pci device 1132 * 1133 * Return: True if primary GTs are enabled, false if they have been disabled via 1134 * configfs. 1135 */ 1136 bool xe_configfs_primary_gt_allowed(struct pci_dev *pdev) 1137 { 1138 return get_gt_types_allowed(pdev) & BIT_ULL(XE_GT_TYPE_MAIN); 1139 } 1140 1141 /** 1142 * xe_configfs_media_gt_allowed - determine whether media GTs are supported 1143 * @pdev: pci device 1144 * 1145 * Return: True if the media GTs are enabled, false if they have been disabled 1146 * via configfs. 1147 */ 1148 bool xe_configfs_media_gt_allowed(struct pci_dev *pdev) 1149 { 1150 return get_gt_types_allowed(pdev) & BIT_ULL(XE_GT_TYPE_MEDIA); 1151 } 1152 1153 /** 1154 * xe_configfs_get_engines_allowed - get engine allowed mask from configfs 1155 * @pdev: pci device 1156 * 1157 * Return: engine mask with allowed engines set in configfs 1158 */ 1159 u64 xe_configfs_get_engines_allowed(struct pci_dev *pdev) 1160 { 1161 struct xe_config_group_device *dev = find_xe_config_group_device(pdev); 1162 u64 engines_allowed; 1163 1164 if (!dev) 1165 return device_defaults.engines_allowed; 1166 1167 engines_allowed = dev->config.engines_allowed; 1168 config_group_put(&dev->group); 1169 1170 return engines_allowed; 1171 } 1172 1173 /** 1174 * xe_configfs_get_psmi_enabled - get configfs enable_psmi setting 1175 * @pdev: pci device 1176 * 1177 * Return: enable_psmi setting in configfs 1178 */ 1179 bool xe_configfs_get_psmi_enabled(struct pci_dev *pdev) 1180 { 1181 struct xe_config_group_device *dev = find_xe_config_group_device(pdev); 1182 bool ret; 1183 1184 if (!dev) 1185 return false; 1186 1187 ret = dev->config.enable_psmi; 1188 config_group_put(&dev->group); 1189 1190 return ret; 1191 } 1192 1193 /** 1194 * xe_configfs_get_ctx_restore_mid_bb - get configfs ctx_restore_mid_bb setting 1195 * @pdev: pci device 1196 * @class: hw engine class 1197 * @cs: pointer to the bb to use - only valid during probe 1198 * 1199 * Return: Number of dwords used in the mid_ctx_restore setting in configfs 1200 */ 1201 u32 xe_configfs_get_ctx_restore_mid_bb(struct pci_dev *pdev, 1202 enum xe_engine_class class, 1203 const u32 **cs) 1204 { 1205 struct xe_config_group_device *dev = find_xe_config_group_device(pdev); 1206 u32 len; 1207 1208 if (!dev) 1209 return 0; 1210 1211 if (cs) 1212 *cs = dev->config.ctx_restore_mid_bb[class].cs; 1213 1214 len = dev->config.ctx_restore_mid_bb[class].len; 1215 config_group_put(&dev->group); 1216 1217 return len; 1218 } 1219 1220 /** 1221 * xe_configfs_get_ctx_restore_post_bb - get configfs ctx_restore_post_bb setting 1222 * @pdev: pci device 1223 * @class: hw engine class 1224 * @cs: pointer to the bb to use - only valid during probe 1225 * 1226 * Return: Number of dwords used in the post_ctx_restore setting in configfs 1227 */ 1228 u32 xe_configfs_get_ctx_restore_post_bb(struct pci_dev *pdev, 1229 enum xe_engine_class class, 1230 const u32 **cs) 1231 { 1232 struct xe_config_group_device *dev = find_xe_config_group_device(pdev); 1233 u32 len; 1234 1235 if (!dev) 1236 return 0; 1237 1238 *cs = dev->config.ctx_restore_post_bb[class].cs; 1239 len = dev->config.ctx_restore_post_bb[class].len; 1240 config_group_put(&dev->group); 1241 1242 return len; 1243 } 1244 1245 #ifdef CONFIG_PCI_IOV 1246 /** 1247 * xe_configfs_get_max_vfs() - Get number of VFs that could be managed 1248 * @pdev: the &pci_dev device 1249 * 1250 * Find the configfs group that belongs to the PCI device and return maximum 1251 * number of Virtual Functions (VFs) that could be managed by this device. 1252 * If configfs group is not present, use value of max_vfs module parameter. 1253 * 1254 * Return: maximum number of VFs that could be managed. 1255 */ 1256 unsigned int xe_configfs_get_max_vfs(struct pci_dev *pdev) 1257 { 1258 struct xe_config_group_device *dev = find_xe_config_group_device(pdev); 1259 unsigned int max_vfs; 1260 1261 if (!dev) 1262 return xe_modparam.max_vfs; 1263 1264 scoped_guard(mutex, &dev->lock) 1265 max_vfs = dev->config.sriov.max_vfs; 1266 1267 config_group_put(&dev->group); 1268 1269 return max_vfs; 1270 } 1271 #endif 1272 1273 int __init xe_configfs_init(void) 1274 { 1275 int ret; 1276 1277 config_group_init(&xe_configfs.su_group); 1278 mutex_init(&xe_configfs.su_mutex); 1279 ret = configfs_register_subsystem(&xe_configfs); 1280 if (ret) { 1281 mutex_destroy(&xe_configfs.su_mutex); 1282 return ret; 1283 } 1284 1285 return 0; 1286 } 1287 1288 void xe_configfs_exit(void) 1289 { 1290 configfs_unregister_subsystem(&xe_configfs); 1291 mutex_destroy(&xe_configfs.su_mutex); 1292 } 1293