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