xref: /openbmc/linux/drivers/firmware/arm_scmi/bus.c (revision 55fd7e02)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * System Control and Management Interface (SCMI) Message Protocol bus layer
4  *
5  * Copyright (C) 2018 ARM Ltd.
6  */
7 
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9 
10 #include <linux/types.h>
11 #include <linux/module.h>
12 #include <linux/kernel.h>
13 #include <linux/slab.h>
14 #include <linux/device.h>
15 
16 #include "common.h"
17 
18 static DEFINE_IDA(scmi_bus_id);
19 static DEFINE_IDR(scmi_protocols);
20 static DEFINE_SPINLOCK(protocol_lock);
21 
22 static const struct scmi_device_id *
23 scmi_dev_match_id(struct scmi_device *scmi_dev, struct scmi_driver *scmi_drv)
24 {
25 	const struct scmi_device_id *id = scmi_drv->id_table;
26 
27 	if (!id)
28 		return NULL;
29 
30 	for (; id->protocol_id; id++)
31 		if (id->protocol_id == scmi_dev->protocol_id) {
32 			if (!id->name)
33 				return id;
34 			else if (!strcmp(id->name, scmi_dev->name))
35 				return id;
36 		}
37 
38 	return NULL;
39 }
40 
41 static int scmi_dev_match(struct device *dev, struct device_driver *drv)
42 {
43 	struct scmi_driver *scmi_drv = to_scmi_driver(drv);
44 	struct scmi_device *scmi_dev = to_scmi_dev(dev);
45 	const struct scmi_device_id *id;
46 
47 	id = scmi_dev_match_id(scmi_dev, scmi_drv);
48 	if (id)
49 		return 1;
50 
51 	return 0;
52 }
53 
54 static int scmi_protocol_init(int protocol_id, struct scmi_handle *handle)
55 {
56 	scmi_prot_init_fn_t fn = idr_find(&scmi_protocols, protocol_id);
57 
58 	if (unlikely(!fn))
59 		return -EINVAL;
60 	return fn(handle);
61 }
62 
63 static int scmi_protocol_dummy_init(struct scmi_handle *handle)
64 {
65 	return 0;
66 }
67 
68 static int scmi_dev_probe(struct device *dev)
69 {
70 	struct scmi_driver *scmi_drv = to_scmi_driver(dev->driver);
71 	struct scmi_device *scmi_dev = to_scmi_dev(dev);
72 	const struct scmi_device_id *id;
73 	int ret;
74 
75 	id = scmi_dev_match_id(scmi_dev, scmi_drv);
76 	if (!id)
77 		return -ENODEV;
78 
79 	if (!scmi_dev->handle)
80 		return -EPROBE_DEFER;
81 
82 	ret = scmi_protocol_init(scmi_dev->protocol_id, scmi_dev->handle);
83 	if (ret)
84 		return ret;
85 
86 	/* Skip protocol initialisation for additional devices */
87 	idr_replace(&scmi_protocols, &scmi_protocol_dummy_init,
88 		    scmi_dev->protocol_id);
89 
90 	return scmi_drv->probe(scmi_dev);
91 }
92 
93 static int scmi_dev_remove(struct device *dev)
94 {
95 	struct scmi_driver *scmi_drv = to_scmi_driver(dev->driver);
96 	struct scmi_device *scmi_dev = to_scmi_dev(dev);
97 
98 	if (scmi_drv->remove)
99 		scmi_drv->remove(scmi_dev);
100 
101 	return 0;
102 }
103 
104 static struct bus_type scmi_bus_type = {
105 	.name =	"scmi_protocol",
106 	.match = scmi_dev_match,
107 	.probe = scmi_dev_probe,
108 	.remove = scmi_dev_remove,
109 };
110 
111 int scmi_driver_register(struct scmi_driver *driver, struct module *owner,
112 			 const char *mod_name)
113 {
114 	int retval;
115 
116 	driver->driver.bus = &scmi_bus_type;
117 	driver->driver.name = driver->name;
118 	driver->driver.owner = owner;
119 	driver->driver.mod_name = mod_name;
120 
121 	retval = driver_register(&driver->driver);
122 	if (!retval)
123 		pr_debug("registered new scmi driver %s\n", driver->name);
124 
125 	return retval;
126 }
127 EXPORT_SYMBOL_GPL(scmi_driver_register);
128 
129 void scmi_driver_unregister(struct scmi_driver *driver)
130 {
131 	driver_unregister(&driver->driver);
132 }
133 EXPORT_SYMBOL_GPL(scmi_driver_unregister);
134 
135 static void scmi_device_release(struct device *dev)
136 {
137 	kfree(to_scmi_dev(dev));
138 }
139 
140 struct scmi_device *
141 scmi_device_create(struct device_node *np, struct device *parent, int protocol,
142 		   const char *name)
143 {
144 	int id, retval;
145 	struct scmi_device *scmi_dev;
146 
147 	scmi_dev = kzalloc(sizeof(*scmi_dev), GFP_KERNEL);
148 	if (!scmi_dev)
149 		return NULL;
150 
151 	scmi_dev->name = kstrdup_const(name ?: "unknown", GFP_KERNEL);
152 	if (!scmi_dev->name) {
153 		kfree(scmi_dev);
154 		return NULL;
155 	}
156 
157 	id = ida_simple_get(&scmi_bus_id, 1, 0, GFP_KERNEL);
158 	if (id < 0) {
159 		kfree_const(scmi_dev->name);
160 		kfree(scmi_dev);
161 		return NULL;
162 	}
163 
164 	scmi_dev->id = id;
165 	scmi_dev->protocol_id = protocol;
166 	scmi_dev->dev.parent = parent;
167 	scmi_dev->dev.of_node = np;
168 	scmi_dev->dev.bus = &scmi_bus_type;
169 	scmi_dev->dev.release = scmi_device_release;
170 	dev_set_name(&scmi_dev->dev, "scmi_dev.%d", id);
171 
172 	retval = device_register(&scmi_dev->dev);
173 	if (retval)
174 		goto put_dev;
175 
176 	return scmi_dev;
177 put_dev:
178 	kfree_const(scmi_dev->name);
179 	put_device(&scmi_dev->dev);
180 	ida_simple_remove(&scmi_bus_id, id);
181 	return NULL;
182 }
183 
184 void scmi_device_destroy(struct scmi_device *scmi_dev)
185 {
186 	kfree_const(scmi_dev->name);
187 	scmi_handle_put(scmi_dev->handle);
188 	ida_simple_remove(&scmi_bus_id, scmi_dev->id);
189 	device_unregister(&scmi_dev->dev);
190 }
191 
192 void scmi_set_handle(struct scmi_device *scmi_dev)
193 {
194 	scmi_dev->handle = scmi_handle_get(&scmi_dev->dev);
195 }
196 
197 int scmi_protocol_register(int protocol_id, scmi_prot_init_fn_t fn)
198 {
199 	int ret;
200 
201 	spin_lock(&protocol_lock);
202 	ret = idr_alloc(&scmi_protocols, fn, protocol_id, protocol_id + 1,
203 			GFP_ATOMIC);
204 	spin_unlock(&protocol_lock);
205 	if (ret != protocol_id)
206 		pr_err("unable to allocate SCMI idr slot, err %d\n", ret);
207 
208 	return ret;
209 }
210 EXPORT_SYMBOL_GPL(scmi_protocol_register);
211 
212 void scmi_protocol_unregister(int protocol_id)
213 {
214 	spin_lock(&protocol_lock);
215 	idr_remove(&scmi_protocols, protocol_id);
216 	spin_unlock(&protocol_lock);
217 }
218 EXPORT_SYMBOL_GPL(scmi_protocol_unregister);
219 
220 static int __scmi_devices_unregister(struct device *dev, void *data)
221 {
222 	struct scmi_device *scmi_dev = to_scmi_dev(dev);
223 
224 	scmi_device_destroy(scmi_dev);
225 	return 0;
226 }
227 
228 static void scmi_devices_unregister(void)
229 {
230 	bus_for_each_dev(&scmi_bus_type, NULL, NULL, __scmi_devices_unregister);
231 }
232 
233 static int __init scmi_bus_init(void)
234 {
235 	int retval;
236 
237 	retval = bus_register(&scmi_bus_type);
238 	if (retval)
239 		pr_err("scmi protocol bus register failed (%d)\n", retval);
240 
241 	return retval;
242 }
243 subsys_initcall(scmi_bus_init);
244 
245 static void __exit scmi_bus_exit(void)
246 {
247 	scmi_devices_unregister();
248 	bus_unregister(&scmi_bus_type);
249 	ida_destroy(&scmi_bus_id);
250 }
251 module_exit(scmi_bus_exit);
252