1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2021 ARM Ltd.
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8 #include <linux/arm_ffa.h>
9 #include <linux/device.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <linux/types.h>
18 static DEFINE_IDA(ffa_bus_id);
20 static int ffa_device_match(struct device *dev, struct device_driver *drv)
22 const struct ffa_device_id *id_table;
23 struct ffa_device *ffa_dev;
25 id_table = to_ffa_driver(drv)->id_table;
26 ffa_dev = to_ffa_dev(dev);
28 while (!uuid_is_null(&id_table->uuid)) {
30 * FF-A v1.0 doesn't provide discovery of UUIDs, just the
31 * partition IDs, so fetch the partitions IDs for this
32 * id_table UUID and assign the UUID to the device if the
33 * partition ID matches
35 if (uuid_is_null(&ffa_dev->uuid))
36 ffa_device_match_uuid(ffa_dev, &id_table->uuid);
38 if (uuid_equal(&ffa_dev->uuid, &id_table->uuid))
46 static int ffa_device_probe(struct device *dev)
48 struct ffa_driver *ffa_drv = to_ffa_driver(dev->driver);
49 struct ffa_device *ffa_dev = to_ffa_dev(dev);
51 return ffa_drv->probe(ffa_dev);
54 static void ffa_device_remove(struct device *dev)
56 struct ffa_driver *ffa_drv = to_ffa_driver(dev->driver);
59 ffa_drv->remove(to_ffa_dev(dev));
62 static int ffa_device_uevent(const struct device *dev, struct kobj_uevent_env *env)
64 const struct ffa_device *ffa_dev = to_ffa_dev(dev);
66 return add_uevent_var(env, "MODALIAS=arm_ffa:%04x:%pUb",
67 ffa_dev->vm_id, &ffa_dev->uuid);
70 static ssize_t partition_id_show(struct device *dev,
71 struct device_attribute *attr, char *buf)
73 struct ffa_device *ffa_dev = to_ffa_dev(dev);
75 return sprintf(buf, "0x%04x\n", ffa_dev->vm_id);
77 static DEVICE_ATTR_RO(partition_id);
79 static ssize_t uuid_show(struct device *dev, struct device_attribute *attr,
82 struct ffa_device *ffa_dev = to_ffa_dev(dev);
84 return sprintf(buf, "%pUb\n", &ffa_dev->uuid);
86 static DEVICE_ATTR_RO(uuid);
88 static struct attribute *ffa_device_attributes_attrs[] = {
89 &dev_attr_partition_id.attr,
93 ATTRIBUTE_GROUPS(ffa_device_attributes);
95 struct bus_type ffa_bus_type = {
97 .match = ffa_device_match,
98 .probe = ffa_device_probe,
99 .remove = ffa_device_remove,
100 .uevent = ffa_device_uevent,
101 .dev_groups = ffa_device_attributes_groups,
103 EXPORT_SYMBOL_GPL(ffa_bus_type);
105 int ffa_driver_register(struct ffa_driver *driver, struct module *owner,
106 const char *mod_name)
113 driver->driver.bus = &ffa_bus_type;
114 driver->driver.name = driver->name;
115 driver->driver.owner = owner;
116 driver->driver.mod_name = mod_name;
118 ret = driver_register(&driver->driver);
120 pr_debug("registered new ffa driver %s\n", driver->name);
124 EXPORT_SYMBOL_GPL(ffa_driver_register);
126 void ffa_driver_unregister(struct ffa_driver *driver)
128 driver_unregister(&driver->driver);
130 EXPORT_SYMBOL_GPL(ffa_driver_unregister);
132 static void ffa_release_device(struct device *dev)
134 struct ffa_device *ffa_dev = to_ffa_dev(dev);
136 ida_free(&ffa_bus_id, ffa_dev->id);
140 static int __ffa_devices_unregister(struct device *dev, void *data)
142 device_unregister(dev);
147 static void ffa_devices_unregister(void)
149 bus_for_each_dev(&ffa_bus_type, NULL, NULL,
150 __ffa_devices_unregister);
153 bool ffa_device_is_valid(struct ffa_device *ffa_dev)
156 struct device *dev = NULL;
157 struct ffa_device *tmp_dev;
160 dev = bus_find_next_device(&ffa_bus_type, dev);
161 tmp_dev = to_ffa_dev(dev);
162 if (tmp_dev == ffa_dev) {
174 struct ffa_device *ffa_device_register(const uuid_t *uuid, int vm_id,
175 const struct ffa_ops *ops)
179 struct ffa_device *ffa_dev;
181 id = ida_alloc_min(&ffa_bus_id, 1, GFP_KERNEL);
185 ffa_dev = kzalloc(sizeof(*ffa_dev), GFP_KERNEL);
187 ida_free(&ffa_bus_id, id);
192 dev->bus = &ffa_bus_type;
193 dev->release = ffa_release_device;
194 dev_set_name(&ffa_dev->dev, "arm-ffa-%d", id);
196 ffa_dev->vm_id = vm_id;
198 uuid_copy(&ffa_dev->uuid, uuid);
200 ret = device_register(&ffa_dev->dev);
202 dev_err(dev, "unable to register device %s err=%d\n",
210 EXPORT_SYMBOL_GPL(ffa_device_register);
212 void ffa_device_unregister(struct ffa_device *ffa_dev)
217 device_unregister(&ffa_dev->dev);
219 EXPORT_SYMBOL_GPL(ffa_device_unregister);
221 int arm_ffa_bus_init(void)
223 return bus_register(&ffa_bus_type);
226 void arm_ffa_bus_exit(void)
228 ffa_devices_unregister();
229 bus_unregister(&ffa_bus_type);
230 ida_destroy(&ffa_bus_id);