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 */
scmi_protocol_device_request(const struct scmi_device_id * id_table)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
scmi_protocol_table_register(const struct scmi_device_id * id_table)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 */
scmi_protocol_device_unrequest(const struct scmi_device_id * id_table)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
scmi_protocol_table_unregister(const struct scmi_device_id * id_table)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 *
scmi_dev_match_id(struct scmi_device * scmi_dev,const struct scmi_driver * scmi_drv)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
scmi_dev_match(struct device * dev,const struct device_driver * drv)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
scmi_match_by_id_table(struct device * dev,const void * data)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
scmi_child_dev_find(struct device * parent,int prot_id,const char * name)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 /* Drop the refcnt bumped implicitly by device_find_child */
259 put_device(dev);
260
261 return to_scmi_dev(dev);
262 }
263
scmi_dev_probe(struct device * dev)264 static int scmi_dev_probe(struct device *dev)
265 {
266 struct scmi_driver *scmi_drv = to_scmi_driver(dev->driver);
267 struct scmi_device *scmi_dev = to_scmi_dev(dev);
268
269 if (!scmi_dev->handle)
270 return -EPROBE_DEFER;
271
272 return scmi_drv->probe(scmi_dev);
273 }
274
scmi_dev_remove(struct device * dev)275 static void scmi_dev_remove(struct device *dev)
276 {
277 struct scmi_driver *scmi_drv = to_scmi_driver(dev->driver);
278 struct scmi_device *scmi_dev = to_scmi_dev(dev);
279
280 if (scmi_drv->remove)
281 scmi_drv->remove(scmi_dev);
282 }
283
scmi_device_uevent(const struct device * dev,struct kobj_uevent_env * env)284 static int scmi_device_uevent(const struct device *dev, struct kobj_uevent_env *env)
285 {
286 const struct scmi_device *scmi_dev = to_scmi_dev(dev);
287
288 return add_uevent_var(env, "MODALIAS=" SCMI_UEVENT_MODALIAS_FMT,
289 dev_name(&scmi_dev->dev), scmi_dev->protocol_id,
290 scmi_dev->name);
291 }
292
modalias_show(struct device * dev,struct device_attribute * attr,char * buf)293 static ssize_t modalias_show(struct device *dev,
294 struct device_attribute *attr, char *buf)
295 {
296 struct scmi_device *scmi_dev = to_scmi_dev(dev);
297
298 return sysfs_emit(buf, SCMI_UEVENT_MODALIAS_FMT,
299 dev_name(&scmi_dev->dev), scmi_dev->protocol_id,
300 scmi_dev->name);
301 }
302 static DEVICE_ATTR_RO(modalias);
303
protocol_id_show(struct device * dev,struct device_attribute * attr,char * buf)304 static ssize_t protocol_id_show(struct device *dev,
305 struct device_attribute *attr, char *buf)
306 {
307 struct scmi_device *scmi_dev = to_scmi_dev(dev);
308
309 return sprintf(buf, "0x%02x\n", scmi_dev->protocol_id);
310 }
311 static DEVICE_ATTR_RO(protocol_id);
312
name_show(struct device * dev,struct device_attribute * attr,char * buf)313 static ssize_t name_show(struct device *dev, struct device_attribute *attr,
314 char *buf)
315 {
316 struct scmi_device *scmi_dev = to_scmi_dev(dev);
317
318 return sprintf(buf, "%s\n", scmi_dev->name);
319 }
320 static DEVICE_ATTR_RO(name);
321
322 static struct attribute *scmi_device_attributes_attrs[] = {
323 &dev_attr_protocol_id.attr,
324 &dev_attr_name.attr,
325 &dev_attr_modalias.attr,
326 NULL,
327 };
328 ATTRIBUTE_GROUPS(scmi_device_attributes);
329
330 const struct bus_type scmi_bus_type = {
331 .name = "scmi_protocol",
332 .match = scmi_dev_match,
333 .probe = scmi_dev_probe,
334 .remove = scmi_dev_remove,
335 .uevent = scmi_device_uevent,
336 .dev_groups = scmi_device_attributes_groups,
337 };
338 EXPORT_SYMBOL_GPL(scmi_bus_type);
339
scmi_driver_register(struct scmi_driver * driver,struct module * owner,const char * mod_name)340 int scmi_driver_register(struct scmi_driver *driver, struct module *owner,
341 const char *mod_name)
342 {
343 int retval;
344
345 if (!driver->probe)
346 return -EINVAL;
347
348 retval = scmi_protocol_table_register(driver->id_table);
349 if (retval)
350 return retval;
351
352 driver->driver.bus = &scmi_bus_type;
353 driver->driver.name = driver->name;
354 driver->driver.owner = owner;
355 driver->driver.mod_name = mod_name;
356
357 retval = driver_register(&driver->driver);
358 if (!retval)
359 pr_debug("Registered new scmi driver %s\n", driver->name);
360
361 return retval;
362 }
363 EXPORT_SYMBOL_GPL(scmi_driver_register);
364
scmi_driver_unregister(struct scmi_driver * driver)365 void scmi_driver_unregister(struct scmi_driver *driver)
366 {
367 driver_unregister(&driver->driver);
368 scmi_protocol_table_unregister(driver->id_table);
369 }
370 EXPORT_SYMBOL_GPL(scmi_driver_unregister);
371
scmi_device_release(struct device * dev)372 static void scmi_device_release(struct device *dev)
373 {
374 struct scmi_device *scmi_dev = to_scmi_dev(dev);
375
376 kfree_const(scmi_dev->name);
377 kfree(scmi_dev);
378 }
379
__scmi_device_destroy(struct scmi_device * scmi_dev)380 static void __scmi_device_destroy(struct scmi_device *scmi_dev)
381 {
382 pr_debug("(%s) Destroying SCMI device '%s' for protocol 0x%x (%s)\n",
383 of_node_full_name(scmi_dev->dev.parent->of_node),
384 dev_name(&scmi_dev->dev), scmi_dev->protocol_id,
385 scmi_dev->name);
386
387 if (scmi_dev->protocol_id == SCMI_PROTOCOL_SYSTEM)
388 atomic_set(&scmi_syspower_registered, 0);
389
390 ida_free(&scmi_bus_id, scmi_dev->id);
391 device_unregister(&scmi_dev->dev);
392 }
393
394 static struct scmi_device *
__scmi_device_create(struct device_node * np,struct device * parent,int protocol,const char * name)395 __scmi_device_create(struct device_node *np, struct device *parent,
396 int protocol, const char *name)
397 {
398 int id, retval;
399 struct scmi_device *scmi_dev;
400
401 /*
402 * If the same protocol/name device already exist under the same parent
403 * (i.e. SCMI instance) just return the existent device.
404 * This avoids any race between the SCMI driver, creating devices for
405 * each DT defined protocol at probe time, and the concurrent
406 * registration of SCMI drivers.
407 */
408 scmi_dev = scmi_child_dev_find(parent, protocol, name);
409 if (scmi_dev)
410 return scmi_dev;
411
412 /*
413 * Ignore any possible subsequent failures while creating the device
414 * since we are doomed anyway at that point; not using a mutex which
415 * spans across this whole function to keep things simple and to avoid
416 * to serialize all the __scmi_device_create calls across possibly
417 * different SCMI server instances (parent)
418 */
419 if (protocol == SCMI_PROTOCOL_SYSTEM &&
420 atomic_cmpxchg(&scmi_syspower_registered, 0, 1)) {
421 dev_warn(parent,
422 "SCMI SystemPower protocol device must be unique !\n");
423 return NULL;
424 }
425
426 scmi_dev = kzalloc(sizeof(*scmi_dev), GFP_KERNEL);
427 if (!scmi_dev)
428 return NULL;
429
430 scmi_dev->name = kstrdup_const(name ?: "unknown", GFP_KERNEL);
431 if (!scmi_dev->name) {
432 kfree(scmi_dev);
433 return NULL;
434 }
435
436 id = ida_alloc_min(&scmi_bus_id, 1, GFP_KERNEL);
437 if (id < 0) {
438 kfree_const(scmi_dev->name);
439 kfree(scmi_dev);
440 return NULL;
441 }
442
443 scmi_dev->id = id;
444 scmi_dev->protocol_id = protocol;
445 scmi_dev->dev.parent = parent;
446 device_set_node(&scmi_dev->dev, of_fwnode_handle(np));
447 scmi_dev->dev.bus = &scmi_bus_type;
448 scmi_dev->dev.release = scmi_device_release;
449 dev_set_name(&scmi_dev->dev, "scmi_dev.%d", id);
450
451 retval = device_register(&scmi_dev->dev);
452 if (retval)
453 goto put_dev;
454
455 pr_debug("(%s) Created SCMI device '%s' for protocol 0x%x (%s)\n",
456 of_node_full_name(parent->of_node),
457 dev_name(&scmi_dev->dev), protocol, name);
458
459 return scmi_dev;
460 put_dev:
461 put_device(&scmi_dev->dev);
462 ida_free(&scmi_bus_id, id);
463 return NULL;
464 }
465
466 /**
467 * scmi_device_create - A method to create one or more SCMI devices
468 *
469 * @np: A reference to the device node to use for the new device(s)
470 * @parent: The parent device to use identifying a specific SCMI instance
471 * @protocol: The SCMI protocol to be associated with this device
472 * @name: The requested-name of the device to be created; this is optional
473 * and if no @name is provided, all the devices currently known to
474 * be requested on the SCMI bus for @protocol will be created.
475 *
476 * This method can be invoked to create a single well-defined device (like
477 * a transport device or a device requested by an SCMI driver loaded after
478 * the core SCMI stack has been probed), or to create all the devices currently
479 * known to have been requested by the loaded SCMI drivers for a specific
480 * protocol (typically during SCMI core protocol enumeration at probe time).
481 *
482 * Return: The created device (or one of them if @name was NOT provided and
483 * multiple devices were created) or NULL if no device was created;
484 * note that NULL indicates an error ONLY in case a specific @name
485 * was provided: when @name param was not provided, a number of devices
486 * could have been potentially created for a whole protocol, unless no
487 * device was found to have been requested for that specific protocol.
488 */
scmi_device_create(struct device_node * np,struct device * parent,int protocol,const char * name)489 struct scmi_device *scmi_device_create(struct device_node *np,
490 struct device *parent, int protocol,
491 const char *name)
492 {
493 struct list_head *phead;
494 struct scmi_requested_dev *rdev;
495 struct scmi_device *scmi_dev = NULL;
496
497 if (name)
498 return __scmi_device_create(np, parent, protocol, name);
499
500 mutex_lock(&scmi_requested_devices_mtx);
501 phead = idr_find(&scmi_requested_devices, protocol);
502 /* Nothing to do. */
503 if (!phead) {
504 mutex_unlock(&scmi_requested_devices_mtx);
505 return NULL;
506 }
507
508 /* Walk the list of requested devices for protocol and create them */
509 list_for_each_entry(rdev, phead, node) {
510 struct scmi_device *sdev;
511
512 sdev = __scmi_device_create(np, parent,
513 rdev->id_table->protocol_id,
514 rdev->id_table->name);
515 /* Report errors and carry on... */
516 if (sdev)
517 scmi_dev = sdev;
518 else
519 pr_err("(%s) Failed to create device for protocol 0x%x (%s)\n",
520 of_node_full_name(parent->of_node),
521 rdev->id_table->protocol_id,
522 rdev->id_table->name);
523 }
524 mutex_unlock(&scmi_requested_devices_mtx);
525
526 return scmi_dev;
527 }
528 EXPORT_SYMBOL_GPL(scmi_device_create);
529
scmi_device_destroy(struct device * parent,int protocol,const char * name)530 void scmi_device_destroy(struct device *parent, int protocol, const char *name)
531 {
532 struct scmi_device *scmi_dev;
533
534 scmi_dev = scmi_child_dev_find(parent, protocol, name);
535 if (scmi_dev)
536 __scmi_device_destroy(scmi_dev);
537 }
538 EXPORT_SYMBOL_GPL(scmi_device_destroy);
539
__scmi_devices_unregister(struct device * dev,void * data)540 static int __scmi_devices_unregister(struct device *dev, void *data)
541 {
542 struct scmi_device *scmi_dev = to_scmi_dev(dev);
543
544 __scmi_device_destroy(scmi_dev);
545 return 0;
546 }
547
scmi_devices_unregister(void)548 static void scmi_devices_unregister(void)
549 {
550 bus_for_each_dev(&scmi_bus_type, NULL, NULL, __scmi_devices_unregister);
551 }
552
scmi_bus_init(void)553 static int __init scmi_bus_init(void)
554 {
555 int retval;
556
557 retval = bus_register(&scmi_bus_type);
558 if (retval)
559 pr_err("SCMI protocol bus register failed (%d)\n", retval);
560
561 pr_info("SCMI protocol bus registered\n");
562
563 return retval;
564 }
565 subsys_initcall(scmi_bus_init);
566
scmi_bus_exit(void)567 static void __exit scmi_bus_exit(void)
568 {
569 /*
570 * Destroy all remaining devices: just in case the drivers were
571 * manually unbound and at first and then the modules unloaded.
572 */
573 scmi_devices_unregister();
574 bus_unregister(&scmi_bus_type);
575 ida_destroy(&scmi_bus_id);
576 }
577 module_exit(scmi_bus_exit);
578
579 MODULE_ALIAS("scmi-core");
580 MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");
581 MODULE_DESCRIPTION("ARM SCMI protocol bus");
582 MODULE_LICENSE("GPL");
583