1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * System Control and Management Interface (SCMI) Message Protocol bus layer 4 * 5 * Copyright (C) 2018-2021 ARM Ltd. 6 */ 7 8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 9 10 #include <linux/types.h> 11 #include <linux/module.h> 12 #include <linux/of.h> 13 #include <linux/kernel.h> 14 #include <linux/slab.h> 15 #include <linux/device.h> 16 17 #include "common.h" 18 19 #define SCMI_UEVENT_MODALIAS_FMT "%s:%02x:%s" 20 21 BLOCKING_NOTIFIER_HEAD(scmi_requested_devices_nh); 22 EXPORT_SYMBOL_GPL(scmi_requested_devices_nh); 23 24 static DEFINE_IDA(scmi_bus_id); 25 26 static DEFINE_IDR(scmi_requested_devices); 27 /* Protect access to scmi_requested_devices */ 28 static DEFINE_MUTEX(scmi_requested_devices_mtx); 29 30 struct scmi_requested_dev { 31 const struct scmi_device_id *id_table; 32 struct list_head node; 33 }; 34 35 /* Track globally the SCMI SystemPower protocol device. */ 36 static struct scmi_device *scmi_syspower_registered; 37 38 /** 39 * scmi_protocol_device_request - Helper to request a device 40 * 41 * @id_table: A protocol/name pair descriptor for the device to be created. 42 * 43 * This helper let an SCMI driver request specific devices identified by the 44 * @id_table to be created for each active SCMI instance. 45 * 46 * The requested device name MUST NOT be already existent for this protocol; 47 * at first the freshly requested @id_table is annotated in the IDR table 48 * @scmi_requested_devices and then the requested device is advertised to any 49 * registered party via the @scmi_requested_devices_nh notification chain. 50 * 51 * Return: 0 on Success 52 */ 53 static int scmi_protocol_device_request(const struct scmi_device_id *id_table) 54 { 55 int ret = 0; 56 struct list_head *head, *phead = NULL; 57 struct scmi_requested_dev *rdev; 58 59 pr_debug("Requesting SCMI device (%s) for protocol %x\n", 60 id_table->name, id_table->protocol_id); 61 62 if (IS_ENABLED(CONFIG_ARM_SCMI_RAW_MODE_SUPPORT) && 63 !IS_ENABLED(CONFIG_ARM_SCMI_RAW_MODE_SUPPORT_COEX)) { 64 pr_warn("SCMI Raw mode active. Rejecting '%s'/0x%02X\n", 65 id_table->name, id_table->protocol_id); 66 return -EINVAL; 67 } 68 69 /* 70 * Find the matching protocol rdev list and then search of any 71 * existent equally named device...fails if any duplicate found. 72 */ 73 mutex_lock(&scmi_requested_devices_mtx); 74 phead = idr_find(&scmi_requested_devices, id_table->protocol_id); 75 if (phead) { 76 head = phead; 77 list_for_each_entry(rdev, head, node) { 78 if (!strcmp(rdev->id_table->name, id_table->name)) { 79 pr_err("Ignoring duplicate request [%d] %s\n", 80 rdev->id_table->protocol_id, 81 rdev->id_table->name); 82 ret = -EINVAL; 83 goto out; 84 } 85 } 86 } 87 88 /* 89 * No duplicate found for requested id_table, so let's create a new 90 * requested device entry for this new valid request. 91 */ 92 rdev = kzalloc_obj(*rdev); 93 if (!rdev) { 94 ret = -ENOMEM; 95 goto out; 96 } 97 rdev->id_table = id_table; 98 99 /* 100 * Append the new requested device table descriptor to the head of the 101 * related protocol list, eventually creating such head if not already 102 * there. 103 */ 104 if (!phead) { 105 phead = kzalloc_obj(*phead); 106 if (!phead) { 107 kfree(rdev); 108 ret = -ENOMEM; 109 goto out; 110 } 111 INIT_LIST_HEAD(phead); 112 113 ret = idr_alloc(&scmi_requested_devices, (void *)phead, 114 id_table->protocol_id, 115 id_table->protocol_id + 1, GFP_KERNEL); 116 if (ret != id_table->protocol_id) { 117 pr_err("Failed to save SCMI device - ret:%d\n", ret); 118 kfree(rdev); 119 kfree(phead); 120 ret = -EINVAL; 121 goto out; 122 } 123 ret = 0; 124 } 125 list_add(&rdev->node, phead); 126 127 out: 128 mutex_unlock(&scmi_requested_devices_mtx); 129 130 if (!ret) 131 blocking_notifier_call_chain(&scmi_requested_devices_nh, 132 SCMI_BUS_NOTIFY_DEVICE_REQUEST, 133 (void *)rdev->id_table); 134 135 return ret; 136 } 137 138 /** 139 * scmi_protocol_device_unrequest - Helper to unrequest a device 140 * 141 * @id_table: A protocol/name pair descriptor for the device to be unrequested. 142 * 143 * The unrequested device, described by the provided id_table, is at first 144 * removed from the IDR @scmi_requested_devices and then the removal is 145 * advertised to any registered party via the @scmi_requested_devices_nh 146 * notification chain. 147 */ 148 static void scmi_protocol_device_unrequest(const struct scmi_device_id *id_table) 149 { 150 struct scmi_requested_dev *rdev, *victim = NULL; 151 struct list_head *phead; 152 153 pr_debug("Unrequesting SCMI device (%s) for protocol %x\n", 154 id_table->name, id_table->protocol_id); 155 156 mutex_lock(&scmi_requested_devices_mtx); 157 phead = idr_find(&scmi_requested_devices, id_table->protocol_id); 158 if (phead) { 159 list_for_each_entry(rdev, phead, node) { 160 if (!strcmp(rdev->id_table->name, id_table->name)) { 161 victim = rdev; 162 list_del(&rdev->node); 163 break; 164 } 165 } 166 167 if (victim && list_empty(phead)) { 168 idr_remove(&scmi_requested_devices, 169 id_table->protocol_id); 170 kfree(phead); 171 } 172 } 173 mutex_unlock(&scmi_requested_devices_mtx); 174 175 if (victim) { 176 blocking_notifier_call_chain(&scmi_requested_devices_nh, 177 SCMI_BUS_NOTIFY_DEVICE_UNREQUEST, 178 (void *)victim->id_table); 179 kfree(victim); 180 } 181 } 182 183 static int scmi_protocol_table_register(const struct scmi_device_id *id_table) 184 { 185 const struct scmi_device_id *entry; 186 int ret; 187 188 for (entry = id_table; entry->name; entry++) { 189 ret = scmi_protocol_device_request(entry); 190 if (ret) 191 goto err_unrequest; 192 } 193 194 return 0; 195 196 err_unrequest: 197 while (entry != id_table) 198 scmi_protocol_device_unrequest(--entry); 199 200 return ret; 201 } 202 203 static void 204 scmi_protocol_table_unregister(const struct scmi_device_id *id_table) 205 { 206 const struct scmi_device_id *entry; 207 208 for (entry = id_table; entry->name; entry++) 209 scmi_protocol_device_unrequest(entry); 210 } 211 212 static bool scmi_device_is_transport(const struct scmi_device *scmi_dev) 213 { 214 return !strncmp(scmi_dev->name, SCMI_TRANSPORT_DEVNAME_PREFIX, 215 strlen(SCMI_TRANSPORT_DEVNAME_PREFIX)); 216 } 217 218 static int __scmi_dev_match_by_id_table(struct scmi_device *scmi_dev, 219 const struct scmi_device_id *id_table, 220 bool skip_transport) 221 { 222 if (!id_table || !id_table->name) 223 return 0; 224 225 for (; id_table->protocol_id && id_table->name; id_table++) 226 if (id_table->protocol_id == scmi_dev->protocol_id && 227 !(skip_transport && scmi_device_is_transport(scmi_dev)) && 228 !strcmp(id_table->name, scmi_dev->name)) 229 return 1; 230 return 0; 231 } 232 233 static int scmi_dev_match_by_id_table(struct scmi_device *scmi_dev, 234 const struct scmi_device_id *id_table) 235 { 236 return __scmi_dev_match_by_id_table(scmi_dev, id_table, true); 237 } 238 239 static int scmi_dev_match_id(struct scmi_device *scmi_dev, 240 const struct scmi_driver *scmi_drv) 241 { 242 return scmi_dev_match_by_id_table(scmi_dev, scmi_drv->id_table); 243 } 244 245 static int scmi_dev_match(struct device *dev, const struct device_driver *drv) 246 { 247 const struct scmi_driver *scmi_drv = to_scmi_driver(drv); 248 struct scmi_device *scmi_dev = to_scmi_dev(dev); 249 250 return scmi_dev_match_id(scmi_dev, scmi_drv); 251 } 252 253 static int scmi_match_by_id_table(struct device *dev, const void *data) 254 { 255 struct scmi_device *scmi_dev = to_scmi_dev(dev); 256 const struct scmi_device_id *id_table = data; 257 258 return __scmi_dev_match_by_id_table(scmi_dev, id_table, false); 259 } 260 261 /* Returns a device_find_child() reference which must be dropped by caller. */ 262 static struct scmi_device * 263 scmi_child_dev_find_get(struct device *parent, int prot_id, const char *name) 264 { 265 struct scmi_device_id id_table[2] = { 0 }; 266 struct device *dev; 267 268 id_table[0].protocol_id = prot_id; 269 id_table[0].name = name; 270 271 dev = device_find_child(parent, &id_table, scmi_match_by_id_table); 272 if (!dev) 273 return NULL; 274 275 return to_scmi_dev(dev); 276 } 277 278 static int scmi_dev_probe(struct device *dev) 279 { 280 struct scmi_driver *scmi_drv = to_scmi_driver(dev->driver); 281 struct scmi_device *scmi_dev = to_scmi_dev(dev); 282 283 if (!scmi_dev->handle) 284 return -EPROBE_DEFER; 285 286 return scmi_drv->probe(scmi_dev); 287 } 288 289 static void scmi_dev_remove(struct device *dev) 290 { 291 struct scmi_driver *scmi_drv = to_scmi_driver(dev->driver); 292 struct scmi_device *scmi_dev = to_scmi_dev(dev); 293 294 if (scmi_drv->remove) 295 scmi_drv->remove(scmi_dev); 296 } 297 298 static int scmi_device_uevent(const struct device *dev, struct kobj_uevent_env *env) 299 { 300 const struct scmi_device *scmi_dev = to_scmi_dev(dev); 301 302 return add_uevent_var(env, "MODALIAS=" SCMI_UEVENT_MODALIAS_FMT, 303 dev_name(&scmi_dev->dev), scmi_dev->protocol_id, 304 scmi_dev->name); 305 } 306 307 static ssize_t modalias_show(struct device *dev, 308 struct device_attribute *attr, char *buf) 309 { 310 struct scmi_device *scmi_dev = to_scmi_dev(dev); 311 312 return sysfs_emit(buf, SCMI_UEVENT_MODALIAS_FMT, 313 dev_name(&scmi_dev->dev), scmi_dev->protocol_id, 314 scmi_dev->name); 315 } 316 static DEVICE_ATTR_RO(modalias); 317 318 static ssize_t protocol_id_show(struct device *dev, 319 struct device_attribute *attr, char *buf) 320 { 321 struct scmi_device *scmi_dev = to_scmi_dev(dev); 322 323 return sprintf(buf, "0x%02x\n", scmi_dev->protocol_id); 324 } 325 static DEVICE_ATTR_RO(protocol_id); 326 327 static ssize_t name_show(struct device *dev, struct device_attribute *attr, 328 char *buf) 329 { 330 struct scmi_device *scmi_dev = to_scmi_dev(dev); 331 332 return sprintf(buf, "%s\n", scmi_dev->name); 333 } 334 static DEVICE_ATTR_RO(name); 335 336 static struct attribute *scmi_device_attributes_attrs[] = { 337 &dev_attr_protocol_id.attr, 338 &dev_attr_name.attr, 339 &dev_attr_modalias.attr, 340 NULL, 341 }; 342 ATTRIBUTE_GROUPS(scmi_device_attributes); 343 344 static int scmi_pm_suspend(struct device *dev) 345 { 346 const struct device_driver *drv = dev->driver; 347 348 if (drv && drv->pm && drv->pm->suspend) 349 return drv->pm->suspend(dev); 350 351 return 0; 352 } 353 354 static int scmi_pm_resume(struct device *dev) 355 { 356 const struct device_driver *drv = dev->driver; 357 358 if (drv && drv->pm && drv->pm->resume) 359 return drv->pm->resume(dev); 360 361 return 0; 362 } 363 364 static const struct dev_pm_ops scmi_dev_pm_ops = { 365 .suspend = pm_sleep_ptr(scmi_pm_suspend), 366 .resume = pm_sleep_ptr(scmi_pm_resume), 367 }; 368 369 const struct bus_type scmi_bus_type = { 370 .name = "scmi_protocol", 371 .match = scmi_dev_match, 372 .probe = scmi_dev_probe, 373 .remove = scmi_dev_remove, 374 .uevent = scmi_device_uevent, 375 .dev_groups = scmi_device_attributes_groups, 376 .pm = &scmi_dev_pm_ops, 377 }; 378 EXPORT_SYMBOL_GPL(scmi_bus_type); 379 380 int scmi_driver_register(struct scmi_driver *driver, struct module *owner, 381 const char *mod_name) 382 { 383 int retval; 384 385 if (!driver->probe) 386 return -EINVAL; 387 388 retval = scmi_protocol_table_register(driver->id_table); 389 if (retval) 390 return retval; 391 392 driver->driver.bus = &scmi_bus_type; 393 driver->driver.name = driver->name; 394 driver->driver.owner = owner; 395 driver->driver.mod_name = mod_name; 396 397 retval = driver_register(&driver->driver); 398 if (retval) { 399 scmi_protocol_table_unregister(driver->id_table); 400 return retval; 401 } 402 403 pr_debug("Registered new scmi driver %s\n", driver->name); 404 405 return 0; 406 } 407 EXPORT_SYMBOL_GPL(scmi_driver_register); 408 409 void scmi_driver_unregister(struct scmi_driver *driver) 410 { 411 driver_unregister(&driver->driver); 412 scmi_protocol_table_unregister(driver->id_table); 413 } 414 EXPORT_SYMBOL_GPL(scmi_driver_unregister); 415 416 static void scmi_device_release_resources(struct scmi_device *scmi_dev) 417 { 418 if (scmi_dev->protocol_id == SCMI_PROTOCOL_SYSTEM) 419 cmpxchg(&scmi_syspower_registered, scmi_dev, NULL); 420 421 if (scmi_dev->id) { 422 ida_free(&scmi_bus_id, scmi_dev->id); 423 scmi_dev->id = 0; 424 } 425 } 426 427 static void scmi_device_release(struct device *dev) 428 { 429 struct scmi_device *scmi_dev = to_scmi_dev(dev); 430 431 scmi_device_release_resources(scmi_dev); 432 of_node_put(dev->of_node); 433 kfree_const(scmi_dev->name); 434 kfree(scmi_dev); 435 } 436 437 static void __scmi_device_destroy(struct scmi_device *scmi_dev) 438 { 439 pr_debug("(%pOF) Destroying SCMI device '%s' for protocol 0x%x (%s)\n", 440 scmi_dev->dev.parent->of_node, 441 dev_name(&scmi_dev->dev), scmi_dev->protocol_id, 442 scmi_dev->name); 443 444 device_del(&scmi_dev->dev); 445 scmi_device_release_resources(scmi_dev); 446 put_device(&scmi_dev->dev); 447 } 448 449 static struct scmi_device * 450 __scmi_device_create(struct device_node *np, struct device *parent, 451 int protocol, const char *name) 452 { 453 int id, retval; 454 struct scmi_device *scmi_dev; 455 bool syspower = (protocol == SCMI_PROTOCOL_SYSTEM); 456 457 /* 458 * If the same protocol/name device already exist under the same parent 459 * (i.e. SCMI instance) just return the existent device. 460 * This avoids any race between the SCMI driver, creating devices for 461 * each DT defined protocol at probe time, and the concurrent 462 * registration of SCMI drivers. 463 */ 464 scmi_dev = scmi_child_dev_find_get(parent, protocol, name); 465 if (scmi_dev) { 466 put_device(&scmi_dev->dev); 467 return scmi_dev; 468 } 469 470 scmi_dev = kzalloc_obj(*scmi_dev); 471 if (!scmi_dev) 472 return NULL; 473 474 scmi_dev->protocol_id = protocol; 475 476 /* 477 * Reserve the singleton SystemPower protocol device using the device 478 * pointer itself, so delayed release of an older device cannot clear 479 * a reservation owned by a newer device. 480 */ 481 if (syspower && cmpxchg(&scmi_syspower_registered, NULL, scmi_dev)) { 482 dev_warn(parent, 483 "SCMI SystemPower protocol device must be unique !\n"); 484 kfree(scmi_dev); 485 return NULL; 486 } 487 488 scmi_dev->name = kstrdup_const(name ?: "unknown", GFP_KERNEL); 489 if (!scmi_dev->name) 490 goto free_dev; 491 492 id = ida_alloc_min(&scmi_bus_id, 1, GFP_KERNEL); 493 if (id < 0) 494 goto free_name; 495 496 scmi_dev->id = id; 497 scmi_dev->dev.parent = parent; 498 device_set_node(&scmi_dev->dev, of_fwnode_handle(of_node_get(np))); 499 scmi_dev->dev.bus = &scmi_bus_type; 500 scmi_dev->dev.release = scmi_device_release; 501 dev_set_name(&scmi_dev->dev, "scmi_dev.%d", id); 502 503 retval = device_register(&scmi_dev->dev); 504 if (retval) 505 goto put_dev; 506 507 pr_debug("(%pOF) Created SCMI device '%s' for protocol 0x%x (%s)\n", 508 parent->of_node, dev_name(&scmi_dev->dev), protocol, name); 509 510 return scmi_dev; 511 put_dev: 512 scmi_device_release_resources(scmi_dev); 513 put_device(&scmi_dev->dev); 514 return NULL; 515 free_name: 516 kfree_const(scmi_dev->name); 517 free_dev: 518 scmi_device_release_resources(scmi_dev); 519 kfree(scmi_dev); 520 return NULL; 521 } 522 523 static struct scmi_device * 524 _scmi_device_create(struct device_node *np, struct device *parent, 525 int protocol, const char *name) 526 { 527 struct scmi_device *sdev; 528 529 sdev = __scmi_device_create(np, parent, protocol, name); 530 if (!sdev) 531 pr_err("(%pOF) Failed to create device for protocol 0x%x (%s)\n", 532 parent->of_node, protocol, name); 533 534 return sdev; 535 } 536 537 /** 538 * scmi_device_create - A method to create one or more SCMI devices 539 * 540 * @np: A reference to the device node to use for the new device(s) 541 * @parent: The parent device to use identifying a specific SCMI instance 542 * @protocol: The SCMI protocol to be associated with this device 543 * @name: The requested-name of the device to be created; this is optional 544 * and if no @name is provided, all the devices currently known to 545 * be requested on the SCMI bus for @protocol will be created. 546 * 547 * This method can be invoked to create a single well-defined device (like 548 * a transport device or a device requested by an SCMI driver loaded after 549 * the core SCMI stack has been probed), or to create all the devices currently 550 * known to have been requested by the loaded SCMI drivers for a specific 551 * protocol (typically during SCMI core protocol enumeration at probe time). 552 * 553 * Return: The created device (or one of them if @name was NOT provided and 554 * multiple devices were created) or NULL if no device was created; 555 * note that NULL indicates an error ONLY in case a specific @name 556 * was provided: when @name param was not provided, a number of devices 557 * could have been potentially created for a whole protocol, unless no 558 * device was found to have been requested for that specific protocol. 559 */ 560 struct scmi_device *scmi_device_create(struct device_node *np, 561 struct device *parent, int protocol, 562 const char *name) 563 { 564 struct list_head *phead; 565 struct scmi_requested_dev *rdev; 566 struct scmi_device *scmi_dev = NULL; 567 568 if (name) 569 return _scmi_device_create(np, parent, protocol, name); 570 571 mutex_lock(&scmi_requested_devices_mtx); 572 phead = idr_find(&scmi_requested_devices, protocol); 573 /* Nothing to do. */ 574 if (!phead) { 575 mutex_unlock(&scmi_requested_devices_mtx); 576 return NULL; 577 } 578 579 /* Walk the list of requested devices for protocol and create them */ 580 list_for_each_entry(rdev, phead, node) { 581 struct scmi_device *sdev; 582 583 sdev = _scmi_device_create(np, parent, 584 rdev->id_table->protocol_id, 585 rdev->id_table->name); 586 if (sdev) 587 scmi_dev = sdev; 588 } 589 590 mutex_unlock(&scmi_requested_devices_mtx); 591 592 return scmi_dev; 593 } 594 EXPORT_SYMBOL_GPL(scmi_device_create); 595 596 void scmi_device_destroy(struct device *parent, int protocol, const char *name) 597 { 598 struct scmi_device *scmi_dev; 599 600 scmi_dev = scmi_child_dev_find_get(parent, protocol, name); 601 if (scmi_dev) { 602 __scmi_device_destroy(scmi_dev); 603 put_device(&scmi_dev->dev); 604 } 605 } 606 EXPORT_SYMBOL_GPL(scmi_device_destroy); 607 608 static int __scmi_devices_unregister(struct device *dev, void *data) 609 { 610 struct scmi_device *scmi_dev = to_scmi_dev(dev); 611 612 __scmi_device_destroy(scmi_dev); 613 return 0; 614 } 615 616 static void scmi_devices_unregister(void) 617 { 618 bus_for_each_dev(&scmi_bus_type, NULL, NULL, __scmi_devices_unregister); 619 } 620 621 static int __init scmi_bus_init(void) 622 { 623 int retval; 624 625 retval = bus_register(&scmi_bus_type); 626 if (retval) 627 pr_err("SCMI protocol bus register failed (%d)\n", retval); 628 629 pr_info("SCMI protocol bus registered\n"); 630 631 return retval; 632 } 633 subsys_initcall(scmi_bus_init); 634 635 static void __exit scmi_bus_exit(void) 636 { 637 /* 638 * Destroy all remaining devices: just in case the drivers were 639 * manually unbound and at first and then the modules unloaded. 640 */ 641 scmi_devices_unregister(); 642 bus_unregister(&scmi_bus_type); 643 ida_destroy(&scmi_bus_id); 644 } 645 module_exit(scmi_bus_exit); 646 647 MODULE_ALIAS("scmi-core"); 648 MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>"); 649 MODULE_DESCRIPTION("ARM SCMI protocol bus"); 650 MODULE_LICENSE("GPL"); 651