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/types.h> 11 #include <linux/module.h> 12 #include <linux/kernel.h> 13 #include <linux/slab.h> 14 #include <linux/device.h> 15 #include <linux/of.h> 16 17 #include "common.h" 18 19 static DEFINE_IDA(scmi_bus_id); 20 static DEFINE_IDR(scmi_protocols); 21 static DEFINE_SPINLOCK(protocol_lock); 22 23 static const struct scmi_device_id * 24 scmi_dev_match_id(struct scmi_device *scmi_dev, struct scmi_driver *scmi_drv) 25 { 26 const struct scmi_device_id *id = scmi_drv->id_table; 27 28 if (!id) 29 return NULL; 30 31 for (; id->protocol_id; id++) 32 if (id->protocol_id == scmi_dev->protocol_id) { 33 if (!id->name) 34 return id; 35 else if (!strcmp(id->name, scmi_dev->name)) 36 return id; 37 } 38 39 return NULL; 40 } 41 42 static int scmi_dev_match(struct device *dev, struct device_driver *drv) 43 { 44 struct scmi_driver *scmi_drv = to_scmi_driver(drv); 45 struct scmi_device *scmi_dev = to_scmi_dev(dev); 46 const struct scmi_device_id *id; 47 48 id = scmi_dev_match_id(scmi_dev, scmi_drv); 49 if (id) 50 return 1; 51 52 return 0; 53 } 54 55 static int scmi_match_by_id_table(struct device *dev, void *data) 56 { 57 struct scmi_device *sdev = to_scmi_dev(dev); 58 struct scmi_device_id *id_table = data; 59 60 return sdev->protocol_id == id_table->protocol_id && 61 !strcmp(sdev->name, id_table->name); 62 } 63 64 struct scmi_device *scmi_child_dev_find(struct device *parent, 65 int prot_id, const char *name) 66 { 67 struct scmi_device_id id_table; 68 struct device *dev; 69 70 id_table.protocol_id = prot_id; 71 id_table.name = name; 72 73 dev = device_find_child(parent, &id_table, scmi_match_by_id_table); 74 if (!dev) 75 return NULL; 76 77 return to_scmi_dev(dev); 78 } 79 80 const struct scmi_protocol *scmi_protocol_get(int protocol_id) 81 { 82 const struct scmi_protocol *proto; 83 84 proto = idr_find(&scmi_protocols, protocol_id); 85 if (!proto || !try_module_get(proto->owner)) { 86 pr_warn("SCMI Protocol 0x%x not found!\n", protocol_id); 87 return NULL; 88 } 89 90 pr_debug("Found SCMI Protocol 0x%x\n", protocol_id); 91 92 return proto; 93 } 94 95 void scmi_protocol_put(int protocol_id) 96 { 97 const struct scmi_protocol *proto; 98 99 proto = idr_find(&scmi_protocols, protocol_id); 100 if (proto) 101 module_put(proto->owner); 102 } 103 104 static int scmi_dev_probe(struct device *dev) 105 { 106 struct scmi_driver *scmi_drv = to_scmi_driver(dev->driver); 107 struct scmi_device *scmi_dev = to_scmi_dev(dev); 108 109 if (!scmi_dev->handle) 110 return -EPROBE_DEFER; 111 112 return scmi_drv->probe(scmi_dev); 113 } 114 115 static void scmi_dev_remove(struct device *dev) 116 { 117 struct scmi_driver *scmi_drv = to_scmi_driver(dev->driver); 118 struct scmi_device *scmi_dev = to_scmi_dev(dev); 119 120 if (scmi_drv->remove) 121 scmi_drv->remove(scmi_dev); 122 } 123 124 static struct bus_type scmi_bus_type = { 125 .name = "scmi_protocol", 126 .match = scmi_dev_match, 127 .probe = scmi_dev_probe, 128 .remove = scmi_dev_remove, 129 }; 130 131 int scmi_driver_register(struct scmi_driver *driver, struct module *owner, 132 const char *mod_name) 133 { 134 int retval; 135 136 if (!driver->probe) 137 return -EINVAL; 138 139 retval = scmi_protocol_device_request(driver->id_table); 140 if (retval) 141 return retval; 142 143 driver->driver.bus = &scmi_bus_type; 144 driver->driver.name = driver->name; 145 driver->driver.owner = owner; 146 driver->driver.mod_name = mod_name; 147 148 retval = driver_register(&driver->driver); 149 if (!retval) 150 pr_debug("registered new scmi driver %s\n", driver->name); 151 152 return retval; 153 } 154 EXPORT_SYMBOL_GPL(scmi_driver_register); 155 156 void scmi_driver_unregister(struct scmi_driver *driver) 157 { 158 driver_unregister(&driver->driver); 159 scmi_protocol_device_unrequest(driver->id_table); 160 } 161 EXPORT_SYMBOL_GPL(scmi_driver_unregister); 162 163 static void scmi_device_release(struct device *dev) 164 { 165 kfree(to_scmi_dev(dev)); 166 } 167 168 struct scmi_device * 169 scmi_device_create(struct device_node *np, struct device *parent, int protocol, 170 const char *name) 171 { 172 int id, retval; 173 struct scmi_device *scmi_dev; 174 175 scmi_dev = kzalloc(sizeof(*scmi_dev), GFP_KERNEL); 176 if (!scmi_dev) 177 return NULL; 178 179 scmi_dev->name = kstrdup_const(name ?: "unknown", GFP_KERNEL); 180 if (!scmi_dev->name) { 181 kfree(scmi_dev); 182 return NULL; 183 } 184 185 id = ida_alloc_min(&scmi_bus_id, 1, GFP_KERNEL); 186 if (id < 0) { 187 kfree_const(scmi_dev->name); 188 kfree(scmi_dev); 189 return NULL; 190 } 191 192 scmi_dev->id = id; 193 scmi_dev->protocol_id = protocol; 194 scmi_dev->dev.parent = parent; 195 device_set_node(&scmi_dev->dev, of_fwnode_handle(np)); 196 scmi_dev->dev.bus = &scmi_bus_type; 197 scmi_dev->dev.release = scmi_device_release; 198 dev_set_name(&scmi_dev->dev, "scmi_dev.%d", id); 199 200 retval = device_register(&scmi_dev->dev); 201 if (retval) 202 goto put_dev; 203 204 return scmi_dev; 205 put_dev: 206 kfree_const(scmi_dev->name); 207 put_device(&scmi_dev->dev); 208 ida_free(&scmi_bus_id, id); 209 return NULL; 210 } 211 212 void scmi_device_destroy(struct scmi_device *scmi_dev) 213 { 214 kfree_const(scmi_dev->name); 215 scmi_handle_put(scmi_dev->handle); 216 ida_free(&scmi_bus_id, scmi_dev->id); 217 device_unregister(&scmi_dev->dev); 218 } 219 220 void scmi_device_link_add(struct device *consumer, struct device *supplier) 221 { 222 struct device_link *link; 223 224 link = device_link_add(consumer, supplier, DL_FLAG_AUTOREMOVE_CONSUMER); 225 226 WARN_ON(!link); 227 } 228 229 void scmi_set_handle(struct scmi_device *scmi_dev) 230 { 231 scmi_dev->handle = scmi_handle_get(&scmi_dev->dev); 232 if (scmi_dev->handle) 233 scmi_device_link_add(&scmi_dev->dev, scmi_dev->handle->dev); 234 } 235 236 int scmi_protocol_register(const struct scmi_protocol *proto) 237 { 238 int ret; 239 240 if (!proto) { 241 pr_err("invalid protocol\n"); 242 return -EINVAL; 243 } 244 245 if (!proto->instance_init) { 246 pr_err("missing init for protocol 0x%x\n", proto->id); 247 return -EINVAL; 248 } 249 250 spin_lock(&protocol_lock); 251 ret = idr_alloc(&scmi_protocols, (void *)proto, 252 proto->id, proto->id + 1, GFP_ATOMIC); 253 spin_unlock(&protocol_lock); 254 if (ret != proto->id) { 255 pr_err("unable to allocate SCMI idr slot for 0x%x - err %d\n", 256 proto->id, ret); 257 return ret; 258 } 259 260 pr_debug("Registered SCMI Protocol 0x%x\n", proto->id); 261 262 return 0; 263 } 264 EXPORT_SYMBOL_GPL(scmi_protocol_register); 265 266 void scmi_protocol_unregister(const struct scmi_protocol *proto) 267 { 268 spin_lock(&protocol_lock); 269 idr_remove(&scmi_protocols, proto->id); 270 spin_unlock(&protocol_lock); 271 272 pr_debug("Unregistered SCMI Protocol 0x%x\n", proto->id); 273 274 return; 275 } 276 EXPORT_SYMBOL_GPL(scmi_protocol_unregister); 277 278 static int __scmi_devices_unregister(struct device *dev, void *data) 279 { 280 struct scmi_device *scmi_dev = to_scmi_dev(dev); 281 282 scmi_device_destroy(scmi_dev); 283 return 0; 284 } 285 286 static void scmi_devices_unregister(void) 287 { 288 bus_for_each_dev(&scmi_bus_type, NULL, NULL, __scmi_devices_unregister); 289 } 290 291 int __init scmi_bus_init(void) 292 { 293 int retval; 294 295 retval = bus_register(&scmi_bus_type); 296 if (retval) 297 pr_err("scmi protocol bus register failed (%d)\n", retval); 298 299 return retval; 300 } 301 302 void __exit scmi_bus_exit(void) 303 { 304 scmi_devices_unregister(); 305 bus_unregister(&scmi_bus_type); 306 ida_destroy(&scmi_bus_id); 307 } 308