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