1e7818584SSudeep Holla // SPDX-License-Identifier: GPL-2.0 2e7818584SSudeep Holla /* 3e7818584SSudeep Holla * Copyright (C) 2021 ARM Ltd. 4e7818584SSudeep Holla */ 5e7818584SSudeep Holla 6e7818584SSudeep Holla #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 7e7818584SSudeep Holla 8e7818584SSudeep Holla #include <linux/arm_ffa.h> 9e7818584SSudeep Holla #include <linux/device.h> 10e7818584SSudeep Holla #include <linux/fs.h> 11e7818584SSudeep Holla #include <linux/kernel.h> 12e7818584SSudeep Holla #include <linux/module.h> 13e7818584SSudeep Holla #include <linux/slab.h> 14e7818584SSudeep Holla #include <linux/types.h> 15e7818584SSudeep Holla 163bbfe987SSudeep Holla #include "common.h" 173bbfe987SSudeep Holla 18*19b87664SSudeep Holla static DEFINE_IDA(ffa_bus_id); 19*19b87664SSudeep Holla 20e7818584SSudeep Holla static int ffa_device_match(struct device *dev, struct device_driver *drv) 21e7818584SSudeep Holla { 22e7818584SSudeep Holla const struct ffa_device_id *id_table; 23e7818584SSudeep Holla struct ffa_device *ffa_dev; 24e7818584SSudeep Holla 25e7818584SSudeep Holla id_table = to_ffa_driver(drv)->id_table; 26e7818584SSudeep Holla ffa_dev = to_ffa_dev(dev); 27e7818584SSudeep Holla 28e7818584SSudeep Holla while (!uuid_is_null(&id_table->uuid)) { 29d0c0bce8SSudeep Holla /* 30d0c0bce8SSudeep Holla * FF-A v1.0 doesn't provide discovery of UUIDs, just the 31d0c0bce8SSudeep Holla * partition IDs, so fetch the partitions IDs for this 32d0c0bce8SSudeep Holla * id_table UUID and assign the UUID to the device if the 33d0c0bce8SSudeep Holla * partition ID matches 34d0c0bce8SSudeep Holla */ 35d0c0bce8SSudeep Holla if (uuid_is_null(&ffa_dev->uuid)) 36d0c0bce8SSudeep Holla ffa_device_match_uuid(ffa_dev, &id_table->uuid); 37d0c0bce8SSudeep Holla 38e7818584SSudeep Holla if (uuid_equal(&ffa_dev->uuid, &id_table->uuid)) 39e7818584SSudeep Holla return 1; 40e7818584SSudeep Holla id_table++; 41e7818584SSudeep Holla } 42e7818584SSudeep Holla 43e7818584SSudeep Holla return 0; 44e7818584SSudeep Holla } 45e7818584SSudeep Holla 46e7818584SSudeep Holla static int ffa_device_probe(struct device *dev) 47e7818584SSudeep Holla { 48e7818584SSudeep Holla struct ffa_driver *ffa_drv = to_ffa_driver(dev->driver); 49e7818584SSudeep Holla struct ffa_device *ffa_dev = to_ffa_dev(dev); 50e7818584SSudeep Holla 51e7818584SSudeep Holla return ffa_drv->probe(ffa_dev); 52e7818584SSudeep Holla } 53e7818584SSudeep Holla 54244f5d59SSudeep Holla static void ffa_device_remove(struct device *dev) 55244f5d59SSudeep Holla { 56244f5d59SSudeep Holla struct ffa_driver *ffa_drv = to_ffa_driver(dev->driver); 57244f5d59SSudeep Holla 58b71b5524SSudeep Holla if (ffa_drv->remove) 59244f5d59SSudeep Holla ffa_drv->remove(to_ffa_dev(dev)); 60244f5d59SSudeep Holla } 61244f5d59SSudeep Holla 622a81ada3SGreg Kroah-Hartman static int ffa_device_uevent(const struct device *dev, struct kobj_uevent_env *env) 63e7818584SSudeep Holla { 642a81ada3SGreg Kroah-Hartman const struct ffa_device *ffa_dev = to_ffa_dev(dev); 65e7818584SSudeep Holla 66e7818584SSudeep Holla return add_uevent_var(env, "MODALIAS=arm_ffa:%04x:%pUb", 67e7818584SSudeep Holla ffa_dev->vm_id, &ffa_dev->uuid); 68e7818584SSudeep Holla } 69e7818584SSudeep Holla 70e7818584SSudeep Holla static ssize_t partition_id_show(struct device *dev, 71e7818584SSudeep Holla struct device_attribute *attr, char *buf) 72e7818584SSudeep Holla { 73e7818584SSudeep Holla struct ffa_device *ffa_dev = to_ffa_dev(dev); 74e7818584SSudeep Holla 75e7818584SSudeep Holla return sprintf(buf, "0x%04x\n", ffa_dev->vm_id); 76e7818584SSudeep Holla } 77e7818584SSudeep Holla static DEVICE_ATTR_RO(partition_id); 78e7818584SSudeep Holla 79e7818584SSudeep Holla static ssize_t uuid_show(struct device *dev, struct device_attribute *attr, 80e7818584SSudeep Holla char *buf) 81e7818584SSudeep Holla { 82e7818584SSudeep Holla struct ffa_device *ffa_dev = to_ffa_dev(dev); 83e7818584SSudeep Holla 84e7818584SSudeep Holla return sprintf(buf, "%pUb\n", &ffa_dev->uuid); 85e7818584SSudeep Holla } 86e7818584SSudeep Holla static DEVICE_ATTR_RO(uuid); 87e7818584SSudeep Holla 88e7818584SSudeep Holla static struct attribute *ffa_device_attributes_attrs[] = { 89e7818584SSudeep Holla &dev_attr_partition_id.attr, 90e7818584SSudeep Holla &dev_attr_uuid.attr, 91e7818584SSudeep Holla NULL, 92e7818584SSudeep Holla }; 93e7818584SSudeep Holla ATTRIBUTE_GROUPS(ffa_device_attributes); 94e7818584SSudeep Holla 95e7818584SSudeep Holla struct bus_type ffa_bus_type = { 96e7818584SSudeep Holla .name = "arm_ffa", 97e7818584SSudeep Holla .match = ffa_device_match, 98e7818584SSudeep Holla .probe = ffa_device_probe, 99244f5d59SSudeep Holla .remove = ffa_device_remove, 100e7818584SSudeep Holla .uevent = ffa_device_uevent, 101e7818584SSudeep Holla .dev_groups = ffa_device_attributes_groups, 102e7818584SSudeep Holla }; 103e7818584SSudeep Holla EXPORT_SYMBOL_GPL(ffa_bus_type); 104e7818584SSudeep Holla 105e7818584SSudeep Holla int ffa_driver_register(struct ffa_driver *driver, struct module *owner, 106e7818584SSudeep Holla const char *mod_name) 107e7818584SSudeep Holla { 108e7818584SSudeep Holla int ret; 109e7818584SSudeep Holla 11092743071SUwe Kleine-König if (!driver->probe) 11192743071SUwe Kleine-König return -EINVAL; 11292743071SUwe Kleine-König 113e7818584SSudeep Holla driver->driver.bus = &ffa_bus_type; 114e7818584SSudeep Holla driver->driver.name = driver->name; 115e7818584SSudeep Holla driver->driver.owner = owner; 116e7818584SSudeep Holla driver->driver.mod_name = mod_name; 117e7818584SSudeep Holla 118e7818584SSudeep Holla ret = driver_register(&driver->driver); 119e7818584SSudeep Holla if (!ret) 120e7818584SSudeep Holla pr_debug("registered new ffa driver %s\n", driver->name); 121e7818584SSudeep Holla 122e7818584SSudeep Holla return ret; 123e7818584SSudeep Holla } 124e7818584SSudeep Holla EXPORT_SYMBOL_GPL(ffa_driver_register); 125e7818584SSudeep Holla 126e7818584SSudeep Holla void ffa_driver_unregister(struct ffa_driver *driver) 127e7818584SSudeep Holla { 128e7818584SSudeep Holla driver_unregister(&driver->driver); 129e7818584SSudeep Holla } 130e7818584SSudeep Holla EXPORT_SYMBOL_GPL(ffa_driver_unregister); 131e7818584SSudeep Holla 132e7818584SSudeep Holla static void ffa_release_device(struct device *dev) 133e7818584SSudeep Holla { 134e7818584SSudeep Holla struct ffa_device *ffa_dev = to_ffa_dev(dev); 135e7818584SSudeep Holla 136*19b87664SSudeep Holla ida_free(&ffa_bus_id, ffa_dev->id); 137e7818584SSudeep Holla kfree(ffa_dev); 138e7818584SSudeep Holla } 139e7818584SSudeep Holla 140e7818584SSudeep Holla static int __ffa_devices_unregister(struct device *dev, void *data) 141e7818584SSudeep Holla { 142eb7b52e6SSudeep Holla device_unregister(dev); 143e7818584SSudeep Holla 144e7818584SSudeep Holla return 0; 145e7818584SSudeep Holla } 146e7818584SSudeep Holla 147e7818584SSudeep Holla static void ffa_devices_unregister(void) 148e7818584SSudeep Holla { 149e7818584SSudeep Holla bus_for_each_dev(&ffa_bus_type, NULL, NULL, 150e7818584SSudeep Holla __ffa_devices_unregister); 151e7818584SSudeep Holla } 152e7818584SSudeep Holla 153e7818584SSudeep Holla bool ffa_device_is_valid(struct ffa_device *ffa_dev) 154e7818584SSudeep Holla { 155e7818584SSudeep Holla bool valid = false; 156e7818584SSudeep Holla struct device *dev = NULL; 157e7818584SSudeep Holla struct ffa_device *tmp_dev; 158e7818584SSudeep Holla 159e7818584SSudeep Holla do { 160e7818584SSudeep Holla dev = bus_find_next_device(&ffa_bus_type, dev); 161e7818584SSudeep Holla tmp_dev = to_ffa_dev(dev); 162e7818584SSudeep Holla if (tmp_dev == ffa_dev) { 163e7818584SSudeep Holla valid = true; 164e7818584SSudeep Holla break; 165e7818584SSudeep Holla } 166e7818584SSudeep Holla put_device(dev); 167e7818584SSudeep Holla } while (dev); 168e7818584SSudeep Holla 169e7818584SSudeep Holla put_device(dev); 170e7818584SSudeep Holla 171e7818584SSudeep Holla return valid; 172e7818584SSudeep Holla } 173e7818584SSudeep Holla 174d01387fcSSudeep Holla struct ffa_device *ffa_device_register(const uuid_t *uuid, int vm_id, 1757aa7a979SSudeep Holla const struct ffa_ops *ops) 176e7818584SSudeep Holla { 177*19b87664SSudeep Holla int id, ret; 178e7818584SSudeep Holla struct device *dev; 179e7818584SSudeep Holla struct ffa_device *ffa_dev; 180e7818584SSudeep Holla 181*19b87664SSudeep Holla id = ida_alloc_min(&ffa_bus_id, 1, GFP_KERNEL); 182*19b87664SSudeep Holla if (id < 0) 183e7818584SSudeep Holla return NULL; 184e7818584SSudeep Holla 185*19b87664SSudeep Holla ffa_dev = kzalloc(sizeof(*ffa_dev), GFP_KERNEL); 186*19b87664SSudeep Holla if (!ffa_dev) { 187*19b87664SSudeep Holla ida_free(&ffa_bus_id, id); 188*19b87664SSudeep Holla return NULL; 189*19b87664SSudeep Holla } 190*19b87664SSudeep Holla 191e7818584SSudeep Holla dev = &ffa_dev->dev; 192e7818584SSudeep Holla dev->bus = &ffa_bus_type; 193e7818584SSudeep Holla dev->release = ffa_release_device; 194*19b87664SSudeep Holla dev_set_name(&ffa_dev->dev, "arm-ffa-%d", id); 195e7818584SSudeep Holla 196e7818584SSudeep Holla ffa_dev->vm_id = vm_id; 197d01387fcSSudeep Holla ffa_dev->ops = ops; 198e7818584SSudeep Holla uuid_copy(&ffa_dev->uuid, uuid); 199e7818584SSudeep Holla 200e7818584SSudeep Holla ret = device_register(&ffa_dev->dev); 201e7818584SSudeep Holla if (ret) { 202e7818584SSudeep Holla dev_err(dev, "unable to register device %s err=%d\n", 203e7818584SSudeep Holla dev_name(dev), ret); 204e7818584SSudeep Holla put_device(dev); 205e7818584SSudeep Holla return NULL; 206e7818584SSudeep Holla } 207e7818584SSudeep Holla 208e7818584SSudeep Holla return ffa_dev; 209e7818584SSudeep Holla } 210e7818584SSudeep Holla EXPORT_SYMBOL_GPL(ffa_device_register); 211e7818584SSudeep Holla 212e7818584SSudeep Holla void ffa_device_unregister(struct ffa_device *ffa_dev) 213e7818584SSudeep Holla { 214e7818584SSudeep Holla if (!ffa_dev) 215e7818584SSudeep Holla return; 216e7818584SSudeep Holla 217e7818584SSudeep Holla device_unregister(&ffa_dev->dev); 218e7818584SSudeep Holla } 219e7818584SSudeep Holla EXPORT_SYMBOL_GPL(ffa_device_unregister); 220e7818584SSudeep Holla 2213bbfe987SSudeep Holla int arm_ffa_bus_init(void) 222e7818584SSudeep Holla { 223e7818584SSudeep Holla return bus_register(&ffa_bus_type); 224e7818584SSudeep Holla } 225e7818584SSudeep Holla 2263bbfe987SSudeep Holla void arm_ffa_bus_exit(void) 227e7818584SSudeep Holla { 228e7818584SSudeep Holla ffa_devices_unregister(); 229e7818584SSudeep Holla bus_unregister(&ffa_bus_type); 230*19b87664SSudeep Holla ida_destroy(&ffa_bus_id); 231e7818584SSudeep Holla } 232