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