xref: /openbmc/linux/drivers/firmware/arm_scmi/bus.c (revision ca90578000afb0d8f177ea36f7259a9c3640cf49)
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 			return id;
33 
34 	return NULL;
35 }
36 
37 static int scmi_dev_match(struct device *dev, struct device_driver *drv)
38 {
39 	struct scmi_driver *scmi_drv = to_scmi_driver(drv);
40 	struct scmi_device *scmi_dev = to_scmi_dev(dev);
41 	const struct scmi_device_id *id;
42 
43 	id = scmi_dev_match_id(scmi_dev, scmi_drv);
44 	if (id)
45 		return 1;
46 
47 	return 0;
48 }
49 
50 static int scmi_protocol_init(int protocol_id, struct scmi_handle *handle)
51 {
52 	scmi_prot_init_fn_t fn = idr_find(&scmi_protocols, protocol_id);
53 
54 	if (unlikely(!fn))
55 		return -EINVAL;
56 	return fn(handle);
57 }
58 
59 static int scmi_dev_probe(struct device *dev)
60 {
61 	struct scmi_driver *scmi_drv = to_scmi_driver(dev->driver);
62 	struct scmi_device *scmi_dev = to_scmi_dev(dev);
63 	const struct scmi_device_id *id;
64 	int ret;
65 
66 	id = scmi_dev_match_id(scmi_dev, scmi_drv);
67 	if (!id)
68 		return -ENODEV;
69 
70 	if (!scmi_dev->handle)
71 		return -EPROBE_DEFER;
72 
73 	ret = scmi_protocol_init(scmi_dev->protocol_id, scmi_dev->handle);
74 	if (ret)
75 		return ret;
76 
77 	return scmi_drv->probe(scmi_dev);
78 }
79 
80 static int scmi_dev_remove(struct device *dev)
81 {
82 	struct scmi_driver *scmi_drv = to_scmi_driver(dev->driver);
83 	struct scmi_device *scmi_dev = to_scmi_dev(dev);
84 
85 	if (scmi_drv->remove)
86 		scmi_drv->remove(scmi_dev);
87 
88 	return 0;
89 }
90 
91 static struct bus_type scmi_bus_type = {
92 	.name =	"scmi_protocol",
93 	.match = scmi_dev_match,
94 	.probe = scmi_dev_probe,
95 	.remove = scmi_dev_remove,
96 };
97 
98 int scmi_driver_register(struct scmi_driver *driver, struct module *owner,
99 			 const char *mod_name)
100 {
101 	int retval;
102 
103 	driver->driver.bus = &scmi_bus_type;
104 	driver->driver.name = driver->name;
105 	driver->driver.owner = owner;
106 	driver->driver.mod_name = mod_name;
107 
108 	retval = driver_register(&driver->driver);
109 	if (!retval)
110 		pr_debug("registered new scmi driver %s\n", driver->name);
111 
112 	return retval;
113 }
114 EXPORT_SYMBOL_GPL(scmi_driver_register);
115 
116 void scmi_driver_unregister(struct scmi_driver *driver)
117 {
118 	driver_unregister(&driver->driver);
119 }
120 EXPORT_SYMBOL_GPL(scmi_driver_unregister);
121 
122 struct scmi_device *
123 scmi_device_create(struct device_node *np, struct device *parent, int protocol)
124 {
125 	int id, retval;
126 	struct scmi_device *scmi_dev;
127 
128 	id = ida_simple_get(&scmi_bus_id, 1, 0, GFP_KERNEL);
129 	if (id < 0)
130 		return NULL;
131 
132 	scmi_dev = kzalloc(sizeof(*scmi_dev), GFP_KERNEL);
133 	if (!scmi_dev)
134 		goto no_mem;
135 
136 	scmi_dev->id = id;
137 	scmi_dev->protocol_id = protocol;
138 	scmi_dev->dev.parent = parent;
139 	scmi_dev->dev.of_node = np;
140 	scmi_dev->dev.bus = &scmi_bus_type;
141 	dev_set_name(&scmi_dev->dev, "scmi_dev.%d", id);
142 
143 	retval = device_register(&scmi_dev->dev);
144 	if (!retval)
145 		return scmi_dev;
146 
147 	put_device(&scmi_dev->dev);
148 	kfree(scmi_dev);
149 no_mem:
150 	ida_simple_remove(&scmi_bus_id, id);
151 	return NULL;
152 }
153 
154 void scmi_device_destroy(struct scmi_device *scmi_dev)
155 {
156 	scmi_handle_put(scmi_dev->handle);
157 	device_unregister(&scmi_dev->dev);
158 	ida_simple_remove(&scmi_bus_id, scmi_dev->id);
159 	kfree(scmi_dev);
160 }
161 
162 void scmi_set_handle(struct scmi_device *scmi_dev)
163 {
164 	scmi_dev->handle = scmi_handle_get(&scmi_dev->dev);
165 }
166 
167 int scmi_protocol_register(int protocol_id, scmi_prot_init_fn_t fn)
168 {
169 	int ret;
170 
171 	spin_lock(&protocol_lock);
172 	ret = idr_alloc(&scmi_protocols, fn, protocol_id, protocol_id + 1,
173 			GFP_ATOMIC);
174 	if (ret != protocol_id)
175 		pr_err("unable to allocate SCMI idr slot, err %d\n", ret);
176 	spin_unlock(&protocol_lock);
177 
178 	return ret;
179 }
180 EXPORT_SYMBOL_GPL(scmi_protocol_register);
181 
182 void scmi_protocol_unregister(int protocol_id)
183 {
184 	spin_lock(&protocol_lock);
185 	idr_remove(&scmi_protocols, protocol_id);
186 	spin_unlock(&protocol_lock);
187 }
188 EXPORT_SYMBOL_GPL(scmi_protocol_unregister);
189 
190 static int __scmi_devices_unregister(struct device *dev, void *data)
191 {
192 	struct scmi_device *scmi_dev = to_scmi_dev(dev);
193 
194 	scmi_device_destroy(scmi_dev);
195 	return 0;
196 }
197 
198 static void scmi_devices_unregister(void)
199 {
200 	bus_for_each_dev(&scmi_bus_type, NULL, NULL, __scmi_devices_unregister);
201 }
202 
203 static int __init scmi_bus_init(void)
204 {
205 	int retval;
206 
207 	retval = bus_register(&scmi_bus_type);
208 	if (retval)
209 		pr_err("scmi protocol bus register failed (%d)\n", retval);
210 
211 	return retval;
212 }
213 subsys_initcall(scmi_bus_init);
214 
215 static void __exit scmi_bus_exit(void)
216 {
217 	scmi_devices_unregister();
218 	bus_unregister(&scmi_bus_type);
219 	ida_destroy(&scmi_bus_id);
220 }
221 module_exit(scmi_bus_exit);
222