xref: /openbmc/linux/drivers/firmware/arm_scmi/bus.c (revision 7effbd18)
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 #include <linux/of.h>
18 
19 #include "common.h"
20 
21 BLOCKING_NOTIFIER_HEAD(scmi_requested_devices_nh);
22 EXPORT_SYMBOL_GPL(scmi_requested_devices_nh);
23 
24 static DEFINE_IDA(scmi_bus_id);
25 
26 static DEFINE_IDR(scmi_requested_devices);
27 /* Protect access to scmi_requested_devices */
28 static DEFINE_MUTEX(scmi_requested_devices_mtx);
29 
30 struct scmi_requested_dev {
31 	const struct scmi_device_id *id_table;
32 	struct list_head node;
33 };
34 
35 /* Track globally the creation of SCMI SystemPower related devices */
36 static atomic_t scmi_syspower_registered = ATOMIC_INIT(0);
37 
38 /**
39  * scmi_protocol_device_request  - Helper to request a device
40  *
41  * @id_table: A protocol/name pair descriptor for the device to be created.
42  *
43  * This helper let an SCMI driver request specific devices identified by the
44  * @id_table to be created for each active SCMI instance.
45  *
46  * The requested device name MUST NOT be already existent for any protocol;
47  * at first the freshly requested @id_table is annotated in the IDR table
48  * @scmi_requested_devices and then the requested device is advertised to any
49  * registered party via the @scmi_requested_devices_nh notification chain.
50  *
51  * Return: 0 on Success
52  */
53 static int scmi_protocol_device_request(const struct scmi_device_id *id_table)
54 {
55 	int ret = 0;
56 	unsigned int id = 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 	 * Search for the matching protocol rdev list and then search
72 	 * of any existent equally named device...fails if any duplicate found.
73 	 */
74 	mutex_lock(&scmi_requested_devices_mtx);
75 	idr_for_each_entry(&scmi_requested_devices, head, id) {
76 		if (!phead) {
77 			/* A list found registered in the IDR is never empty */
78 			rdev = list_first_entry(head, struct scmi_requested_dev,
79 						node);
80 			if (rdev->id_table->protocol_id ==
81 			    id_table->protocol_id)
82 				phead = head;
83 		}
84 		list_for_each_entry(rdev, head, node) {
85 			if (!strcmp(rdev->id_table->name, id_table->name)) {
86 				pr_err("Ignoring duplicate request [%d] %s\n",
87 				       rdev->id_table->protocol_id,
88 				       rdev->id_table->name);
89 				ret = -EINVAL;
90 				goto out;
91 			}
92 		}
93 	}
94 
95 	/*
96 	 * No duplicate found for requested id_table, so let's create a new
97 	 * requested device entry for this new valid request.
98 	 */
99 	rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
100 	if (!rdev) {
101 		ret = -ENOMEM;
102 		goto out;
103 	}
104 	rdev->id_table = id_table;
105 
106 	/*
107 	 * Append the new requested device table descriptor to the head of the
108 	 * related protocol list, eventually creating such head if not already
109 	 * there.
110 	 */
111 	if (!phead) {
112 		phead = kzalloc(sizeof(*phead), GFP_KERNEL);
113 		if (!phead) {
114 			kfree(rdev);
115 			ret = -ENOMEM;
116 			goto out;
117 		}
118 		INIT_LIST_HEAD(phead);
119 
120 		ret = idr_alloc(&scmi_requested_devices, (void *)phead,
121 				id_table->protocol_id,
122 				id_table->protocol_id + 1, GFP_KERNEL);
123 		if (ret != id_table->protocol_id) {
124 			pr_err("Failed to save SCMI device - ret:%d\n", ret);
125 			kfree(rdev);
126 			kfree(phead);
127 			ret = -EINVAL;
128 			goto out;
129 		}
130 		ret = 0;
131 	}
132 	list_add(&rdev->node, phead);
133 
134 out:
135 	mutex_unlock(&scmi_requested_devices_mtx);
136 
137 	if (!ret)
138 		blocking_notifier_call_chain(&scmi_requested_devices_nh,
139 					     SCMI_BUS_NOTIFY_DEVICE_REQUEST,
140 					     (void *)rdev->id_table);
141 
142 	return ret;
143 }
144 
145 /**
146  * scmi_protocol_device_unrequest  - Helper to unrequest a device
147  *
148  * @id_table: A protocol/name pair descriptor for the device to be unrequested.
149  *
150  * The unrequested device, described by the provided id_table, is at first
151  * removed from the IDR @scmi_requested_devices and then the removal is
152  * advertised to any registered party via the @scmi_requested_devices_nh
153  * notification chain.
154  */
155 static void scmi_protocol_device_unrequest(const struct scmi_device_id *id_table)
156 {
157 	struct list_head *phead;
158 
159 	pr_debug("Unrequesting SCMI device (%s) for protocol %x\n",
160 		 id_table->name, id_table->protocol_id);
161 
162 	mutex_lock(&scmi_requested_devices_mtx);
163 	phead = idr_find(&scmi_requested_devices, id_table->protocol_id);
164 	if (phead) {
165 		struct scmi_requested_dev *victim, *tmp;
166 
167 		list_for_each_entry_safe(victim, tmp, phead, node) {
168 			if (!strcmp(victim->id_table->name, id_table->name)) {
169 				list_del(&victim->node);
170 
171 				mutex_unlock(&scmi_requested_devices_mtx);
172 				blocking_notifier_call_chain(&scmi_requested_devices_nh,
173 							     SCMI_BUS_NOTIFY_DEVICE_UNREQUEST,
174 							     (void *)victim->id_table);
175 				kfree(victim);
176 				mutex_lock(&scmi_requested_devices_mtx);
177 				break;
178 			}
179 		}
180 
181 		if (list_empty(phead)) {
182 			idr_remove(&scmi_requested_devices,
183 				   id_table->protocol_id);
184 			kfree(phead);
185 		}
186 	}
187 	mutex_unlock(&scmi_requested_devices_mtx);
188 }
189 
190 static const struct scmi_device_id *
191 scmi_dev_match_id(struct scmi_device *scmi_dev, struct scmi_driver *scmi_drv)
192 {
193 	const struct scmi_device_id *id = scmi_drv->id_table;
194 
195 	if (!id)
196 		return NULL;
197 
198 	for (; id->protocol_id; id++)
199 		if (id->protocol_id == scmi_dev->protocol_id) {
200 			if (!id->name)
201 				return id;
202 			else if (!strcmp(id->name, scmi_dev->name))
203 				return id;
204 		}
205 
206 	return NULL;
207 }
208 
209 static int scmi_dev_match(struct device *dev, struct device_driver *drv)
210 {
211 	struct scmi_driver *scmi_drv = to_scmi_driver(drv);
212 	struct scmi_device *scmi_dev = to_scmi_dev(dev);
213 	const struct scmi_device_id *id;
214 
215 	id = scmi_dev_match_id(scmi_dev, scmi_drv);
216 	if (id)
217 		return 1;
218 
219 	return 0;
220 }
221 
222 static int scmi_match_by_id_table(struct device *dev, void *data)
223 {
224 	struct scmi_device *sdev = to_scmi_dev(dev);
225 	struct scmi_device_id *id_table = data;
226 
227 	return sdev->protocol_id == id_table->protocol_id &&
228 		(id_table->name && !strcmp(sdev->name, id_table->name));
229 }
230 
231 static struct scmi_device *scmi_child_dev_find(struct device *parent,
232 					       int prot_id, const char *name)
233 {
234 	struct scmi_device_id id_table;
235 	struct device *dev;
236 
237 	id_table.protocol_id = prot_id;
238 	id_table.name = name;
239 
240 	dev = device_find_child(parent, &id_table, scmi_match_by_id_table);
241 	if (!dev)
242 		return NULL;
243 
244 	return to_scmi_dev(dev);
245 }
246 
247 static int scmi_dev_probe(struct device *dev)
248 {
249 	struct scmi_driver *scmi_drv = to_scmi_driver(dev->driver);
250 	struct scmi_device *scmi_dev = to_scmi_dev(dev);
251 
252 	if (!scmi_dev->handle)
253 		return -EPROBE_DEFER;
254 
255 	return scmi_drv->probe(scmi_dev);
256 }
257 
258 static void scmi_dev_remove(struct device *dev)
259 {
260 	struct scmi_driver *scmi_drv = to_scmi_driver(dev->driver);
261 	struct scmi_device *scmi_dev = to_scmi_dev(dev);
262 
263 	if (scmi_drv->remove)
264 		scmi_drv->remove(scmi_dev);
265 }
266 
267 struct bus_type scmi_bus_type = {
268 	.name =	"scmi_protocol",
269 	.match = scmi_dev_match,
270 	.probe = scmi_dev_probe,
271 	.remove = scmi_dev_remove,
272 };
273 EXPORT_SYMBOL_GPL(scmi_bus_type);
274 
275 int scmi_driver_register(struct scmi_driver *driver, struct module *owner,
276 			 const char *mod_name)
277 {
278 	int retval;
279 
280 	if (!driver->probe)
281 		return -EINVAL;
282 
283 	retval = scmi_protocol_device_request(driver->id_table);
284 	if (retval)
285 		return retval;
286 
287 	driver->driver.bus = &scmi_bus_type;
288 	driver->driver.name = driver->name;
289 	driver->driver.owner = owner;
290 	driver->driver.mod_name = mod_name;
291 
292 	retval = driver_register(&driver->driver);
293 	if (!retval)
294 		pr_debug("Registered new scmi driver %s\n", driver->name);
295 
296 	return retval;
297 }
298 EXPORT_SYMBOL_GPL(scmi_driver_register);
299 
300 void scmi_driver_unregister(struct scmi_driver *driver)
301 {
302 	driver_unregister(&driver->driver);
303 	scmi_protocol_device_unrequest(driver->id_table);
304 }
305 EXPORT_SYMBOL_GPL(scmi_driver_unregister);
306 
307 static void scmi_device_release(struct device *dev)
308 {
309 	kfree(to_scmi_dev(dev));
310 }
311 
312 static void __scmi_device_destroy(struct scmi_device *scmi_dev)
313 {
314 	pr_debug("(%s) Destroying SCMI device '%s' for protocol 0x%x (%s)\n",
315 		 of_node_full_name(scmi_dev->dev.parent->of_node),
316 		 dev_name(&scmi_dev->dev), scmi_dev->protocol_id,
317 		 scmi_dev->name);
318 
319 	if (scmi_dev->protocol_id == SCMI_PROTOCOL_SYSTEM)
320 		atomic_set(&scmi_syspower_registered, 0);
321 
322 	kfree_const(scmi_dev->name);
323 	ida_free(&scmi_bus_id, scmi_dev->id);
324 	device_unregister(&scmi_dev->dev);
325 }
326 
327 static struct scmi_device *
328 __scmi_device_create(struct device_node *np, struct device *parent,
329 		     int protocol, const char *name)
330 {
331 	int id, retval;
332 	struct scmi_device *scmi_dev;
333 
334 	/*
335 	 * If the same protocol/name device already exist under the same parent
336 	 * (i.e. SCMI instance) just return the existent device.
337 	 * This avoids any race between the SCMI driver, creating devices for
338 	 * each DT defined protocol at probe time, and the concurrent
339 	 * registration of SCMI drivers.
340 	 */
341 	scmi_dev = scmi_child_dev_find(parent, protocol, name);
342 	if (scmi_dev)
343 		return scmi_dev;
344 
345 	/*
346 	 * Ignore any possible subsequent failures while creating the device
347 	 * since we are doomed anyway at that point; not using a mutex which
348 	 * spans across this whole function to keep things simple and to avoid
349 	 * to serialize all the __scmi_device_create calls across possibly
350 	 * different SCMI server instances (parent)
351 	 */
352 	if (protocol == SCMI_PROTOCOL_SYSTEM &&
353 	    atomic_cmpxchg(&scmi_syspower_registered, 0, 1)) {
354 		dev_warn(parent,
355 			 "SCMI SystemPower protocol device must be unique !\n");
356 		return NULL;
357 	}
358 
359 	scmi_dev = kzalloc(sizeof(*scmi_dev), GFP_KERNEL);
360 	if (!scmi_dev)
361 		return NULL;
362 
363 	scmi_dev->name = kstrdup_const(name ?: "unknown", GFP_KERNEL);
364 	if (!scmi_dev->name) {
365 		kfree(scmi_dev);
366 		return NULL;
367 	}
368 
369 	id = ida_alloc_min(&scmi_bus_id, 1, GFP_KERNEL);
370 	if (id < 0) {
371 		kfree_const(scmi_dev->name);
372 		kfree(scmi_dev);
373 		return NULL;
374 	}
375 
376 	scmi_dev->id = id;
377 	scmi_dev->protocol_id = protocol;
378 	scmi_dev->dev.parent = parent;
379 	device_set_node(&scmi_dev->dev, of_fwnode_handle(np));
380 	scmi_dev->dev.bus = &scmi_bus_type;
381 	scmi_dev->dev.release = scmi_device_release;
382 	dev_set_name(&scmi_dev->dev, "scmi_dev.%d", id);
383 
384 	retval = device_register(&scmi_dev->dev);
385 	if (retval)
386 		goto put_dev;
387 
388 	pr_debug("(%s) Created SCMI device '%s' for protocol 0x%x (%s)\n",
389 		 of_node_full_name(parent->of_node),
390 		 dev_name(&scmi_dev->dev), protocol, name);
391 
392 	return scmi_dev;
393 put_dev:
394 	kfree_const(scmi_dev->name);
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  */
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 scmi_dev;
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 
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 
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 
482 static void scmi_devices_unregister(void)
483 {
484 	bus_for_each_dev(&scmi_bus_type, NULL, NULL, __scmi_devices_unregister);
485 }
486 
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 
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