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