1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * CDX bus driver. 4 * 5 * Copyright (C) 2022-2023, Advanced Micro Devices, Inc. 6 */ 7 8 /* 9 * Architecture Overview 10 * ===================== 11 * CDX is a Hardware Architecture designed for AMD FPGA devices. It 12 * consists of sophisticated mechanism for interaction between FPGA, 13 * Firmware and the APUs (Application CPUs). 14 * 15 * Firmware resides on RPU (Realtime CPUs) which interacts with 16 * the FPGA program manager and the APUs. The RPU provides memory-mapped 17 * interface (RPU if) which is used to communicate with APUs. 18 * 19 * The diagram below shows an overview of the CDX architecture: 20 * 21 * +--------------------------------------+ 22 * | Application CPUs (APU) | 23 * | | 24 * | CDX device drivers| 25 * | Linux OS | | 26 * | CDX bus | 27 * | | | 28 * | CDX controller | 29 * | | | 30 * +-----------------------------|--------+ 31 * | (discover, config, 32 * | reset, rescan) 33 * | 34 * +------------------------| RPU if |----+ 35 * | | | 36 * | V | 37 * | Realtime CPUs (RPU) | 38 * | | 39 * +--------------------------------------+ 40 * | 41 * +---------------------|----------------+ 42 * | FPGA | | 43 * | +-----------------------+ | 44 * | | | | | 45 * | +-------+ +-------+ +-------+ | 46 * | | dev 1 | | dev 2 | | dev 3 | | 47 * | +-------+ +-------+ +-------+ | 48 * +--------------------------------------+ 49 * 50 * The RPU firmware extracts the device information from the loaded FPGA 51 * image and implements a mechanism that allows the APU drivers to 52 * enumerate such devices (device personality and resource details) via 53 * a dedicated communication channel. RPU mediates operations such as 54 * discover, reset and rescan of the FPGA devices for the APU. This is 55 * done using memory mapped interface provided by the RPU to APU. 56 */ 57 58 #include <linux/init.h> 59 #include <linux/irqdomain.h> 60 #include <linux/kernel.h> 61 #include <linux/of.h> 62 #include <linux/of_device.h> 63 #include <linux/of_platform.h> 64 #include <linux/platform_device.h> 65 #include <linux/slab.h> 66 #include <linux/mm.h> 67 #include <linux/idr.h> 68 #include <linux/cdx/cdx_bus.h> 69 #include <linux/iommu.h> 70 #include <linux/dma-map-ops.h> 71 #include <linux/debugfs.h> 72 #include "cdx.h" 73 74 /* Default DMA mask for devices on a CDX bus */ 75 #define CDX_DEFAULT_DMA_MASK (~0ULL) 76 #define MAX_CDX_CONTROLLERS 16 77 78 /* IDA for CDX controllers registered with the CDX bus */ 79 static DEFINE_IDA(cdx_controller_ida); 80 /* Lock to protect controller ops */ 81 static DEFINE_MUTEX(cdx_controller_lock); 82 /* Debugfs dir for cdx bus */ 83 static struct dentry *cdx_debugfs_dir; 84 85 static char *compat_node_name = "xlnx,versal-net-cdx"; 86 87 static void cdx_destroy_res_attr(struct cdx_device *cdx_dev, int num); 88 89 /** 90 * cdx_dev_reset - Reset a CDX device 91 * @dev: CDX device 92 * 93 * Return: -errno on failure, 0 on success. 94 */ 95 int cdx_dev_reset(struct device *dev) 96 { 97 struct cdx_device *cdx_dev = to_cdx_device(dev); 98 struct cdx_controller *cdx = cdx_dev->cdx; 99 struct cdx_device_config dev_config = {0}; 100 struct cdx_driver *cdx_drv; 101 int ret; 102 103 cdx_drv = to_cdx_driver(dev->driver); 104 /* Notify driver that device is being reset */ 105 if (cdx_drv && cdx_drv->reset_prepare) 106 cdx_drv->reset_prepare(cdx_dev); 107 108 dev_config.type = CDX_DEV_RESET_CONF; 109 ret = cdx->ops->dev_configure(cdx, cdx_dev->bus_num, 110 cdx_dev->dev_num, &dev_config); 111 if (ret) 112 dev_err(dev, "cdx device reset failed\n"); 113 114 /* Notify driver that device reset is complete */ 115 if (cdx_drv && cdx_drv->reset_done) 116 cdx_drv->reset_done(cdx_dev); 117 118 return ret; 119 } 120 EXPORT_SYMBOL_GPL(cdx_dev_reset); 121 122 /** 123 * reset_cdx_device - Reset a CDX device 124 * @dev: CDX device 125 * @data: This is always passed as NULL, and is not used in this API, 126 * but is required here as the device_for_each_child() API expects 127 * the passed function to have this as an argument. 128 * 129 * Return: -errno on failure, 0 on success. 130 */ 131 static int reset_cdx_device(struct device *dev, void *data) 132 { 133 return cdx_dev_reset(dev); 134 } 135 136 /** 137 * cdx_unregister_device - Unregister a CDX device 138 * @dev: CDX device 139 * @data: This is always passed as NULL, and is not used in this API, 140 * but is required here as the bus_for_each_dev() API expects 141 * the passed function (cdx_unregister_device) to have this 142 * as an argument. 143 * 144 * Return: 0 on success. 145 */ 146 static int cdx_unregister_device(struct device *dev, 147 void *data) 148 { 149 struct cdx_device *cdx_dev = to_cdx_device(dev); 150 struct cdx_controller *cdx = cdx_dev->cdx; 151 152 if (cdx_dev->is_bus) { 153 device_for_each_child(dev, NULL, cdx_unregister_device); 154 if (cdx_dev->enabled && cdx->ops->bus_disable) 155 cdx->ops->bus_disable(cdx, cdx_dev->bus_num); 156 } else { 157 cdx_destroy_res_attr(cdx_dev, MAX_CDX_DEV_RESOURCES); 158 debugfs_remove_recursive(cdx_dev->debugfs_dir); 159 kfree(cdx_dev->driver_override); 160 cdx_dev->driver_override = NULL; 161 } 162 163 /* 164 * Do not free cdx_dev here as it would be freed in 165 * cdx_device_release() called from within put_device(). 166 */ 167 device_del(&cdx_dev->dev); 168 put_device(&cdx_dev->dev); 169 170 return 0; 171 } 172 173 static void cdx_unregister_devices(struct bus_type *bus) 174 { 175 /* Reset all the devices attached to cdx bus */ 176 bus_for_each_dev(bus, NULL, NULL, cdx_unregister_device); 177 } 178 179 /** 180 * cdx_match_one_device - Tell if a CDX device structure has a matching 181 * CDX device id structure 182 * @id: single CDX device id structure to match 183 * @dev: the CDX device structure to match against 184 * 185 * Return: matching cdx_device_id structure or NULL if there is no match. 186 */ 187 static inline const struct cdx_device_id * 188 cdx_match_one_device(const struct cdx_device_id *id, 189 const struct cdx_device *dev) 190 { 191 /* Use vendor ID and device ID for matching */ 192 if ((id->vendor == CDX_ANY_ID || id->vendor == dev->vendor) && 193 (id->device == CDX_ANY_ID || id->device == dev->device) && 194 (id->subvendor == CDX_ANY_ID || id->subvendor == dev->subsystem_vendor) && 195 (id->subdevice == CDX_ANY_ID || id->subdevice == dev->subsystem_device) && 196 !((id->class ^ dev->class) & id->class_mask)) 197 return id; 198 return NULL; 199 } 200 201 /** 202 * cdx_match_id - See if a CDX device matches a given cdx_id table 203 * @ids: array of CDX device ID structures to search in 204 * @dev: the CDX device structure to match against. 205 * 206 * Used by a driver to check whether a CDX device is in its list of 207 * supported devices. Returns the matching cdx_device_id structure or 208 * NULL if there is no match. 209 * 210 * Return: matching cdx_device_id structure or NULL if there is no match. 211 */ 212 static inline const struct cdx_device_id * 213 cdx_match_id(const struct cdx_device_id *ids, struct cdx_device *dev) 214 { 215 if (ids) { 216 while (ids->vendor || ids->device) { 217 if (cdx_match_one_device(ids, dev)) 218 return ids; 219 ids++; 220 } 221 } 222 return NULL; 223 } 224 225 int cdx_set_master(struct cdx_device *cdx_dev) 226 { 227 struct cdx_controller *cdx = cdx_dev->cdx; 228 struct cdx_device_config dev_config; 229 int ret = -EOPNOTSUPP; 230 231 dev_config.type = CDX_DEV_BUS_MASTER_CONF; 232 dev_config.bus_master_enable = true; 233 if (cdx->ops->dev_configure) 234 ret = cdx->ops->dev_configure(cdx, cdx_dev->bus_num, 235 cdx_dev->dev_num, &dev_config); 236 237 return ret; 238 } 239 EXPORT_SYMBOL_GPL(cdx_set_master); 240 241 int cdx_clear_master(struct cdx_device *cdx_dev) 242 { 243 struct cdx_controller *cdx = cdx_dev->cdx; 244 struct cdx_device_config dev_config; 245 int ret = -EOPNOTSUPP; 246 247 dev_config.type = CDX_DEV_BUS_MASTER_CONF; 248 dev_config.bus_master_enable = false; 249 if (cdx->ops->dev_configure) 250 ret = cdx->ops->dev_configure(cdx, cdx_dev->bus_num, 251 cdx_dev->dev_num, &dev_config); 252 253 return ret; 254 } 255 EXPORT_SYMBOL_GPL(cdx_clear_master); 256 257 /** 258 * cdx_bus_match - device to driver matching callback 259 * @dev: the cdx device to match against 260 * @drv: the device driver to search for matching cdx device 261 * structures 262 * 263 * Return: true on success, false otherwise. 264 */ 265 static int cdx_bus_match(struct device *dev, const struct device_driver *drv) 266 { 267 struct cdx_device *cdx_dev = to_cdx_device(dev); 268 const struct cdx_driver *cdx_drv = to_cdx_driver(drv); 269 const struct cdx_device_id *found_id = NULL; 270 const struct cdx_device_id *ids; 271 272 if (cdx_dev->is_bus) 273 return false; 274 275 ids = cdx_drv->match_id_table; 276 277 /* When driver_override is set, only bind to the matching driver */ 278 if (cdx_dev->driver_override && strcmp(cdx_dev->driver_override, drv->name)) 279 return false; 280 281 found_id = cdx_match_id(ids, cdx_dev); 282 if (!found_id) 283 return false; 284 285 do { 286 /* 287 * In case override_only was set, enforce driver_override 288 * matching. 289 */ 290 if (!found_id->override_only) 291 return true; 292 if (cdx_dev->driver_override) 293 return true; 294 295 ids = found_id + 1; 296 found_id = cdx_match_id(ids, cdx_dev); 297 } while (found_id); 298 299 return false; 300 } 301 302 static int cdx_probe(struct device *dev) 303 { 304 struct cdx_driver *cdx_drv = to_cdx_driver(dev->driver); 305 struct cdx_device *cdx_dev = to_cdx_device(dev); 306 struct cdx_controller *cdx = cdx_dev->cdx; 307 int error; 308 309 /* 310 * Setup MSI device data so that generic MSI alloc/free can 311 * be used by the device driver. 312 */ 313 if (cdx->msi_domain) { 314 error = msi_setup_device_data(&cdx_dev->dev); 315 if (error) 316 return error; 317 } 318 319 error = cdx_drv->probe(cdx_dev); 320 if (error) { 321 dev_err_probe(dev, error, "%s failed\n", __func__); 322 return error; 323 } 324 325 return 0; 326 } 327 328 static void cdx_remove(struct device *dev) 329 { 330 struct cdx_driver *cdx_drv = to_cdx_driver(dev->driver); 331 struct cdx_device *cdx_dev = to_cdx_device(dev); 332 333 if (cdx_drv && cdx_drv->remove) 334 cdx_drv->remove(cdx_dev); 335 } 336 337 static void cdx_shutdown(struct device *dev) 338 { 339 struct cdx_driver *cdx_drv = to_cdx_driver(dev->driver); 340 struct cdx_device *cdx_dev = to_cdx_device(dev); 341 struct cdx_controller *cdx = cdx_dev->cdx; 342 343 if (cdx_dev->is_bus && cdx_dev->enabled && cdx->ops->bus_disable) 344 cdx->ops->bus_disable(cdx, cdx_dev->bus_num); 345 if (cdx_drv && cdx_drv->shutdown) 346 cdx_drv->shutdown(cdx_dev); 347 } 348 349 static int cdx_dma_configure(struct device *dev) 350 { 351 struct cdx_driver *cdx_drv = to_cdx_driver(dev->driver); 352 struct cdx_device *cdx_dev = to_cdx_device(dev); 353 struct cdx_controller *cdx = cdx_dev->cdx; 354 u32 input_id = cdx_dev->req_id; 355 int ret; 356 357 ret = of_dma_configure_id(dev, cdx->dev->of_node, 0, &input_id); 358 if (ret && ret != -EPROBE_DEFER) { 359 dev_err(dev, "of_dma_configure_id() failed\n"); 360 return ret; 361 } 362 363 if (!ret && !cdx_drv->driver_managed_dma) { 364 ret = iommu_device_use_default_domain(dev); 365 if (ret) 366 arch_teardown_dma_ops(dev); 367 } 368 369 return 0; 370 } 371 372 static void cdx_dma_cleanup(struct device *dev) 373 { 374 struct cdx_driver *cdx_drv = to_cdx_driver(dev->driver); 375 376 if (!cdx_drv->driver_managed_dma) 377 iommu_device_unuse_default_domain(dev); 378 } 379 380 /* show configuration fields */ 381 #define cdx_config_attr(field, format_string) \ 382 static ssize_t \ 383 field##_show(struct device *dev, struct device_attribute *attr, char *buf) \ 384 { \ 385 struct cdx_device *cdx_dev = to_cdx_device(dev); \ 386 return sysfs_emit(buf, format_string, cdx_dev->field); \ 387 } \ 388 static DEVICE_ATTR_RO(field) 389 390 cdx_config_attr(vendor, "0x%04x\n"); 391 cdx_config_attr(device, "0x%04x\n"); 392 cdx_config_attr(subsystem_vendor, "0x%04x\n"); 393 cdx_config_attr(subsystem_device, "0x%04x\n"); 394 cdx_config_attr(revision, "0x%02x\n"); 395 cdx_config_attr(class, "0x%06x\n"); 396 397 static ssize_t remove_store(struct device *dev, 398 struct device_attribute *attr, 399 const char *buf, size_t count) 400 { 401 bool val; 402 403 if (kstrtobool(buf, &val) < 0) 404 return -EINVAL; 405 406 if (!val) 407 return -EINVAL; 408 409 if (device_remove_file_self(dev, attr)) { 410 int ret; 411 412 ret = cdx_unregister_device(dev, NULL); 413 if (ret) 414 return ret; 415 } 416 417 return count; 418 } 419 static DEVICE_ATTR_WO(remove); 420 421 static ssize_t reset_store(struct device *dev, struct device_attribute *attr, 422 const char *buf, size_t count) 423 { 424 struct cdx_device *cdx_dev = to_cdx_device(dev); 425 bool val; 426 int ret; 427 428 if (kstrtobool(buf, &val) < 0) 429 return -EINVAL; 430 431 if (!val) 432 return -EINVAL; 433 434 if (cdx_dev->is_bus) 435 /* Reset all the devices attached to cdx bus */ 436 ret = device_for_each_child(dev, NULL, reset_cdx_device); 437 else 438 ret = cdx_dev_reset(dev); 439 440 return ret < 0 ? ret : count; 441 } 442 static DEVICE_ATTR_WO(reset); 443 444 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, 445 char *buf) 446 { 447 struct cdx_device *cdx_dev = to_cdx_device(dev); 448 449 return sprintf(buf, "cdx:v%04Xd%04Xsv%04Xsd%04Xc%06X\n", cdx_dev->vendor, 450 cdx_dev->device, cdx_dev->subsystem_vendor, cdx_dev->subsystem_device, 451 cdx_dev->class); 452 } 453 static DEVICE_ATTR_RO(modalias); 454 455 static ssize_t driver_override_store(struct device *dev, 456 struct device_attribute *attr, 457 const char *buf, size_t count) 458 { 459 struct cdx_device *cdx_dev = to_cdx_device(dev); 460 int ret; 461 462 if (WARN_ON(dev->bus != &cdx_bus_type)) 463 return -EINVAL; 464 465 ret = driver_set_override(dev, &cdx_dev->driver_override, buf, count); 466 if (ret) 467 return ret; 468 469 return count; 470 } 471 472 static ssize_t driver_override_show(struct device *dev, 473 struct device_attribute *attr, char *buf) 474 { 475 struct cdx_device *cdx_dev = to_cdx_device(dev); 476 ssize_t len; 477 478 device_lock(dev); 479 len = sysfs_emit(buf, "%s\n", cdx_dev->driver_override); 480 device_unlock(dev); 481 return len; 482 } 483 static DEVICE_ATTR_RW(driver_override); 484 485 static ssize_t enable_store(struct device *dev, struct device_attribute *attr, 486 const char *buf, size_t count) 487 { 488 struct cdx_device *cdx_dev = to_cdx_device(dev); 489 struct cdx_controller *cdx = cdx_dev->cdx; 490 bool enable; 491 int ret; 492 493 if (kstrtobool(buf, &enable) < 0) 494 return -EINVAL; 495 496 if (enable == cdx_dev->enabled) 497 return count; 498 499 if (enable && cdx->ops->bus_enable) 500 ret = cdx->ops->bus_enable(cdx, cdx_dev->bus_num); 501 else if (!enable && cdx->ops->bus_disable) 502 ret = cdx->ops->bus_disable(cdx, cdx_dev->bus_num); 503 else 504 ret = -EOPNOTSUPP; 505 506 if (!ret) 507 cdx_dev->enabled = enable; 508 509 return ret < 0 ? ret : count; 510 } 511 512 static ssize_t enable_show(struct device *dev, struct device_attribute *attr, char *buf) 513 { 514 struct cdx_device *cdx_dev = to_cdx_device(dev); 515 516 return sysfs_emit(buf, "%u\n", cdx_dev->enabled); 517 } 518 static DEVICE_ATTR_RW(enable); 519 520 static umode_t cdx_dev_attrs_are_visible(struct kobject *kobj, struct attribute *a, int n) 521 { 522 struct device *dev = kobj_to_dev(kobj); 523 struct cdx_device *cdx_dev; 524 525 cdx_dev = to_cdx_device(dev); 526 if (!cdx_dev->is_bus) 527 return a->mode; 528 529 return 0; 530 } 531 532 static umode_t cdx_bus_attrs_are_visible(struct kobject *kobj, struct attribute *a, int n) 533 { 534 struct device *dev = kobj_to_dev(kobj); 535 struct cdx_device *cdx_dev; 536 537 cdx_dev = to_cdx_device(dev); 538 if (cdx_dev->is_bus) 539 return a->mode; 540 541 return 0; 542 } 543 544 static struct attribute *cdx_dev_attrs[] = { 545 &dev_attr_remove.attr, 546 &dev_attr_reset.attr, 547 &dev_attr_vendor.attr, 548 &dev_attr_device.attr, 549 &dev_attr_subsystem_vendor.attr, 550 &dev_attr_subsystem_device.attr, 551 &dev_attr_class.attr, 552 &dev_attr_revision.attr, 553 &dev_attr_modalias.attr, 554 &dev_attr_driver_override.attr, 555 NULL, 556 }; 557 558 static const struct attribute_group cdx_dev_group = { 559 .attrs = cdx_dev_attrs, 560 .is_visible = cdx_dev_attrs_are_visible, 561 }; 562 563 static struct attribute *cdx_bus_dev_attrs[] = { 564 &dev_attr_enable.attr, 565 &dev_attr_reset.attr, 566 NULL, 567 }; 568 569 static const struct attribute_group cdx_bus_dev_group = { 570 .attrs = cdx_bus_dev_attrs, 571 .is_visible = cdx_bus_attrs_are_visible, 572 }; 573 574 static const struct attribute_group *cdx_dev_groups[] = { 575 &cdx_dev_group, 576 &cdx_bus_dev_group, 577 NULL, 578 }; 579 580 static int cdx_debug_resource_show(struct seq_file *s, void *data) 581 { 582 struct cdx_device *cdx_dev = s->private; 583 int i; 584 585 for (i = 0; i < MAX_CDX_DEV_RESOURCES; i++) { 586 struct resource *res = &cdx_dev->res[i]; 587 588 seq_printf(s, "%pr\n", res); 589 } 590 591 return 0; 592 } 593 DEFINE_SHOW_ATTRIBUTE(cdx_debug_resource); 594 595 static void cdx_device_debugfs_init(struct cdx_device *cdx_dev) 596 { 597 cdx_dev->debugfs_dir = debugfs_create_dir(dev_name(&cdx_dev->dev), cdx_debugfs_dir); 598 if (IS_ERR(cdx_dev->debugfs_dir)) 599 return; 600 601 debugfs_create_file("resource", 0444, cdx_dev->debugfs_dir, cdx_dev, 602 &cdx_debug_resource_fops); 603 } 604 605 static ssize_t rescan_store(const struct bus_type *bus, 606 const char *buf, size_t count) 607 { 608 struct cdx_controller *cdx; 609 struct platform_device *pd; 610 struct device_node *np; 611 bool val; 612 613 if (kstrtobool(buf, &val) < 0) 614 return -EINVAL; 615 616 if (!val) 617 return -EINVAL; 618 619 mutex_lock(&cdx_controller_lock); 620 621 /* Unregister all the devices on the bus */ 622 cdx_unregister_devices(&cdx_bus_type); 623 624 /* Rescan all the devices */ 625 for_each_compatible_node(np, NULL, compat_node_name) { 626 pd = of_find_device_by_node(np); 627 if (!pd) { 628 of_node_put(np); 629 count = -EINVAL; 630 goto unlock; 631 } 632 633 cdx = platform_get_drvdata(pd); 634 if (cdx && cdx->controller_registered && cdx->ops->scan) 635 cdx->ops->scan(cdx); 636 637 put_device(&pd->dev); 638 } 639 640 unlock: 641 mutex_unlock(&cdx_controller_lock); 642 643 return count; 644 } 645 static BUS_ATTR_WO(rescan); 646 647 static struct attribute *cdx_bus_attrs[] = { 648 &bus_attr_rescan.attr, 649 NULL, 650 }; 651 ATTRIBUTE_GROUPS(cdx_bus); 652 653 struct bus_type cdx_bus_type = { 654 .name = "cdx", 655 .match = cdx_bus_match, 656 .probe = cdx_probe, 657 .remove = cdx_remove, 658 .shutdown = cdx_shutdown, 659 .dma_configure = cdx_dma_configure, 660 .dma_cleanup = cdx_dma_cleanup, 661 .bus_groups = cdx_bus_groups, 662 .dev_groups = cdx_dev_groups, 663 }; 664 EXPORT_SYMBOL_GPL(cdx_bus_type); 665 666 int __cdx_driver_register(struct cdx_driver *cdx_driver, 667 struct module *owner) 668 { 669 int error; 670 671 cdx_driver->driver.owner = owner; 672 cdx_driver->driver.bus = &cdx_bus_type; 673 674 error = driver_register(&cdx_driver->driver); 675 if (error) { 676 pr_err("driver_register() failed for %s: %d\n", 677 cdx_driver->driver.name, error); 678 return error; 679 } 680 681 return 0; 682 } 683 EXPORT_SYMBOL_GPL(__cdx_driver_register); 684 685 void cdx_driver_unregister(struct cdx_driver *cdx_driver) 686 { 687 driver_unregister(&cdx_driver->driver); 688 } 689 EXPORT_SYMBOL_GPL(cdx_driver_unregister); 690 691 static void cdx_device_release(struct device *dev) 692 { 693 struct cdx_device *cdx_dev = to_cdx_device(dev); 694 695 kfree(cdx_dev); 696 } 697 698 static const struct vm_operations_struct cdx_phys_vm_ops = { 699 #ifdef CONFIG_HAVE_IOREMAP_PROT 700 .access = generic_access_phys, 701 #endif 702 }; 703 704 /** 705 * cdx_mmap_resource - map a CDX resource into user memory space 706 * @fp: File pointer. Not used in this function, but required where 707 * this API is registered as a callback. 708 * @kobj: kobject for mapping 709 * @attr: struct bin_attribute for the file being mapped 710 * @vma: struct vm_area_struct passed into the mmap 711 * 712 * Use the regular CDX mapping routines to map a CDX resource into userspace. 713 * 714 * Return: true on success, false otherwise. 715 */ 716 static int cdx_mmap_resource(struct file *fp, struct kobject *kobj, 717 const struct bin_attribute *attr, 718 struct vm_area_struct *vma) 719 { 720 struct cdx_device *cdx_dev = to_cdx_device(kobj_to_dev(kobj)); 721 int num = (unsigned long)attr->private; 722 struct resource *res; 723 unsigned long size; 724 725 res = &cdx_dev->res[num]; 726 if (iomem_is_exclusive(res->start)) 727 return -EINVAL; 728 729 /* Make sure the caller is mapping a valid resource for this device */ 730 size = ((cdx_resource_len(cdx_dev, num) - 1) >> PAGE_SHIFT) + 1; 731 if (vma->vm_pgoff + vma_pages(vma) > size) 732 return -EINVAL; 733 734 /* 735 * Map memory region and vm->vm_pgoff is expected to be an 736 * offset within that region. 737 */ 738 vma->vm_page_prot = pgprot_device(vma->vm_page_prot); 739 vma->vm_pgoff += (cdx_resource_start(cdx_dev, num) >> PAGE_SHIFT); 740 vma->vm_ops = &cdx_phys_vm_ops; 741 return io_remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff, 742 vma->vm_end - vma->vm_start, 743 vma->vm_page_prot); 744 } 745 746 static void cdx_destroy_res_attr(struct cdx_device *cdx_dev, int num) 747 { 748 int i; 749 750 /* removing the bin attributes */ 751 for (i = 0; i < num; i++) { 752 struct bin_attribute *res_attr; 753 754 res_attr = cdx_dev->res_attr[i]; 755 if (res_attr) { 756 sysfs_remove_bin_file(&cdx_dev->dev.kobj, res_attr); 757 kfree(res_attr); 758 } 759 } 760 } 761 762 #define CDX_RES_ATTR_NAME_LEN 10 763 static int cdx_create_res_attr(struct cdx_device *cdx_dev, int num) 764 { 765 struct bin_attribute *res_attr; 766 char *res_attr_name; 767 int ret; 768 769 res_attr = kzalloc(sizeof(*res_attr) + CDX_RES_ATTR_NAME_LEN, GFP_ATOMIC); 770 if (!res_attr) 771 return -ENOMEM; 772 773 res_attr_name = (char *)(res_attr + 1); 774 775 sysfs_bin_attr_init(res_attr); 776 777 cdx_dev->res_attr[num] = res_attr; 778 sprintf(res_attr_name, "resource%d", num); 779 780 res_attr->mmap = cdx_mmap_resource; 781 res_attr->attr.name = res_attr_name; 782 res_attr->attr.mode = 0600; 783 res_attr->size = cdx_resource_len(cdx_dev, num); 784 res_attr->private = (void *)(unsigned long)num; 785 ret = sysfs_create_bin_file(&cdx_dev->dev.kobj, res_attr); 786 if (ret) 787 kfree(res_attr); 788 789 return ret; 790 } 791 792 int cdx_device_add(struct cdx_dev_params *dev_params) 793 { 794 struct cdx_controller *cdx = dev_params->cdx; 795 struct cdx_device *cdx_dev; 796 int ret, i; 797 798 cdx_dev = kzalloc(sizeof(*cdx_dev), GFP_KERNEL); 799 if (!cdx_dev) 800 return -ENOMEM; 801 802 /* Populate resource */ 803 memcpy(cdx_dev->res, dev_params->res, sizeof(struct resource) * 804 dev_params->res_count); 805 cdx_dev->res_count = dev_params->res_count; 806 807 /* Populate CDX dev params */ 808 cdx_dev->req_id = dev_params->req_id; 809 cdx_dev->msi_dev_id = dev_params->msi_dev_id; 810 cdx_dev->vendor = dev_params->vendor; 811 cdx_dev->device = dev_params->device; 812 cdx_dev->subsystem_vendor = dev_params->subsys_vendor; 813 cdx_dev->subsystem_device = dev_params->subsys_device; 814 cdx_dev->class = dev_params->class; 815 cdx_dev->revision = dev_params->revision; 816 cdx_dev->bus_num = dev_params->bus_num; 817 cdx_dev->dev_num = dev_params->dev_num; 818 cdx_dev->cdx = dev_params->cdx; 819 cdx_dev->dma_mask = CDX_DEFAULT_DMA_MASK; 820 821 /* Initialize generic device */ 822 device_initialize(&cdx_dev->dev); 823 cdx_dev->dev.parent = dev_params->parent; 824 cdx_dev->dev.bus = &cdx_bus_type; 825 cdx_dev->dev.dma_mask = &cdx_dev->dma_mask; 826 cdx_dev->dev.release = cdx_device_release; 827 cdx_dev->msi_write_pending = false; 828 mutex_init(&cdx_dev->irqchip_lock); 829 830 /* Set Name */ 831 dev_set_name(&cdx_dev->dev, "cdx-%02x:%02x", 832 ((cdx->id << CDX_CONTROLLER_ID_SHIFT) | (cdx_dev->bus_num & CDX_BUS_NUM_MASK)), 833 cdx_dev->dev_num); 834 835 if (cdx->msi_domain) { 836 cdx_dev->num_msi = dev_params->num_msi; 837 dev_set_msi_domain(&cdx_dev->dev, cdx->msi_domain); 838 } 839 840 ret = device_add(&cdx_dev->dev); 841 if (ret) { 842 dev_err(&cdx_dev->dev, 843 "cdx device add failed: %d", ret); 844 goto fail; 845 } 846 847 /* Create resource<N> attributes */ 848 for (i = 0; i < MAX_CDX_DEV_RESOURCES; i++) { 849 if (cdx_resource_flags(cdx_dev, i) & IORESOURCE_MEM) { 850 /* skip empty resources */ 851 if (!cdx_resource_len(cdx_dev, i)) 852 continue; 853 854 ret = cdx_create_res_attr(cdx_dev, i); 855 if (ret != 0) { 856 dev_err(&cdx_dev->dev, 857 "cdx device resource<%d> file creation failed: %d", i, ret); 858 goto resource_create_fail; 859 } 860 } 861 } 862 863 cdx_device_debugfs_init(cdx_dev); 864 865 return 0; 866 resource_create_fail: 867 cdx_destroy_res_attr(cdx_dev, i); 868 device_del(&cdx_dev->dev); 869 fail: 870 /* 871 * Do not free cdx_dev here as it would be freed in 872 * cdx_device_release() called from put_device(). 873 */ 874 put_device(&cdx_dev->dev); 875 876 return ret; 877 } 878 EXPORT_SYMBOL_NS_GPL(cdx_device_add, "CDX_BUS_CONTROLLER"); 879 880 struct device *cdx_bus_add(struct cdx_controller *cdx, u8 bus_num) 881 { 882 struct cdx_device *cdx_dev; 883 int ret; 884 885 cdx_dev = kzalloc(sizeof(*cdx_dev), GFP_KERNEL); 886 if (!cdx_dev) 887 return NULL; 888 889 device_initialize(&cdx_dev->dev); 890 cdx_dev->cdx = cdx; 891 892 cdx_dev->dev.parent = cdx->dev; 893 cdx_dev->dev.bus = &cdx_bus_type; 894 cdx_dev->dev.release = cdx_device_release; 895 cdx_dev->is_bus = true; 896 cdx_dev->bus_num = bus_num; 897 898 dev_set_name(&cdx_dev->dev, "cdx-%02x", 899 ((cdx->id << CDX_CONTROLLER_ID_SHIFT) | (bus_num & CDX_BUS_NUM_MASK))); 900 901 ret = device_add(&cdx_dev->dev); 902 if (ret) { 903 dev_err(&cdx_dev->dev, "cdx bus device add failed: %d\n", ret); 904 goto device_add_fail; 905 } 906 907 if (cdx->ops->bus_enable) { 908 ret = cdx->ops->bus_enable(cdx, bus_num); 909 if (ret && ret != -EALREADY) { 910 dev_err(cdx->dev, "cdx bus enable failed: %d\n", ret); 911 goto bus_enable_fail; 912 } 913 } 914 915 cdx_dev->enabled = true; 916 return &cdx_dev->dev; 917 918 bus_enable_fail: 919 device_del(&cdx_dev->dev); 920 device_add_fail: 921 put_device(&cdx_dev->dev); 922 923 return NULL; 924 } 925 EXPORT_SYMBOL_NS_GPL(cdx_bus_add, "CDX_BUS_CONTROLLER"); 926 927 int cdx_register_controller(struct cdx_controller *cdx) 928 { 929 int ret; 930 931 ret = ida_alloc_range(&cdx_controller_ida, 0, MAX_CDX_CONTROLLERS - 1, GFP_KERNEL); 932 if (ret < 0) { 933 dev_err(cdx->dev, 934 "No free index available. Maximum controllers already registered\n"); 935 cdx->id = (u8)MAX_CDX_CONTROLLERS; 936 return ret; 937 } 938 939 mutex_lock(&cdx_controller_lock); 940 cdx->id = ret; 941 942 /* Scan all the devices */ 943 if (cdx->ops->scan) 944 cdx->ops->scan(cdx); 945 cdx->controller_registered = true; 946 mutex_unlock(&cdx_controller_lock); 947 948 return 0; 949 } 950 EXPORT_SYMBOL_NS_GPL(cdx_register_controller, "CDX_BUS_CONTROLLER"); 951 952 void cdx_unregister_controller(struct cdx_controller *cdx) 953 { 954 if (cdx->id >= MAX_CDX_CONTROLLERS) 955 return; 956 957 mutex_lock(&cdx_controller_lock); 958 959 cdx->controller_registered = false; 960 device_for_each_child(cdx->dev, NULL, cdx_unregister_device); 961 ida_free(&cdx_controller_ida, cdx->id); 962 963 mutex_unlock(&cdx_controller_lock); 964 } 965 EXPORT_SYMBOL_NS_GPL(cdx_unregister_controller, "CDX_BUS_CONTROLLER"); 966 967 static int __init cdx_bus_init(void) 968 { 969 int ret; 970 971 ret = bus_register(&cdx_bus_type); 972 if (!ret) 973 cdx_debugfs_dir = debugfs_create_dir(cdx_bus_type.name, NULL); 974 975 return ret; 976 } 977 postcore_initcall(cdx_bus_init); 978