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*22779149SSudeep Holla #define SCMI_UEVENT_MODALIAS_FMT "arm_ffa:%04x:%pUb" 19*22779149SSudeep Holla 2019b87664SSudeep Holla static DEFINE_IDA(ffa_bus_id); 2119b87664SSudeep Holla 22e7818584SSudeep Holla static int ffa_device_match(struct device *dev, struct device_driver *drv) 23e7818584SSudeep Holla { 24e7818584SSudeep Holla const struct ffa_device_id *id_table; 25e7818584SSudeep Holla struct ffa_device *ffa_dev; 26e7818584SSudeep Holla 27e7818584SSudeep Holla id_table = to_ffa_driver(drv)->id_table; 28e7818584SSudeep Holla ffa_dev = to_ffa_dev(dev); 29e7818584SSudeep Holla 30e7818584SSudeep Holla while (!uuid_is_null(&id_table->uuid)) { 31d0c0bce8SSudeep Holla /* 32d0c0bce8SSudeep Holla * FF-A v1.0 doesn't provide discovery of UUIDs, just the 33d0c0bce8SSudeep Holla * partition IDs, so fetch the partitions IDs for this 34d0c0bce8SSudeep Holla * id_table UUID and assign the UUID to the device if the 35d0c0bce8SSudeep Holla * partition ID matches 36d0c0bce8SSudeep Holla */ 37d0c0bce8SSudeep Holla if (uuid_is_null(&ffa_dev->uuid)) 38d0c0bce8SSudeep Holla ffa_device_match_uuid(ffa_dev, &id_table->uuid); 39d0c0bce8SSudeep Holla 40e7818584SSudeep Holla if (uuid_equal(&ffa_dev->uuid, &id_table->uuid)) 41e7818584SSudeep Holla return 1; 42e7818584SSudeep Holla id_table++; 43e7818584SSudeep Holla } 44e7818584SSudeep Holla 45e7818584SSudeep Holla return 0; 46e7818584SSudeep Holla } 47e7818584SSudeep Holla 48e7818584SSudeep Holla static int ffa_device_probe(struct device *dev) 49e7818584SSudeep Holla { 50e7818584SSudeep Holla struct ffa_driver *ffa_drv = to_ffa_driver(dev->driver); 51e7818584SSudeep Holla struct ffa_device *ffa_dev = to_ffa_dev(dev); 52e7818584SSudeep Holla 53e7818584SSudeep Holla return ffa_drv->probe(ffa_dev); 54e7818584SSudeep Holla } 55e7818584SSudeep Holla 56244f5d59SSudeep Holla static void ffa_device_remove(struct device *dev) 57244f5d59SSudeep Holla { 58244f5d59SSudeep Holla struct ffa_driver *ffa_drv = to_ffa_driver(dev->driver); 59244f5d59SSudeep Holla 60b71b5524SSudeep Holla if (ffa_drv->remove) 61244f5d59SSudeep Holla ffa_drv->remove(to_ffa_dev(dev)); 62244f5d59SSudeep Holla } 63244f5d59SSudeep Holla 642a81ada3SGreg Kroah-Hartman static int ffa_device_uevent(const struct device *dev, struct kobj_uevent_env *env) 65e7818584SSudeep Holla { 662a81ada3SGreg Kroah-Hartman const struct ffa_device *ffa_dev = to_ffa_dev(dev); 67e7818584SSudeep Holla 68*22779149SSudeep Holla return add_uevent_var(env, "MODALIAS=" SCMI_UEVENT_MODALIAS_FMT, 69e7818584SSudeep Holla ffa_dev->vm_id, &ffa_dev->uuid); 70e7818584SSudeep Holla } 71e7818584SSudeep Holla 72*22779149SSudeep Holla static ssize_t modalias_show(struct device *dev, 73*22779149SSudeep Holla struct device_attribute *attr, char *buf) 74*22779149SSudeep Holla { 75*22779149SSudeep Holla struct ffa_device *ffa_dev = to_ffa_dev(dev); 76*22779149SSudeep Holla 77*22779149SSudeep Holla return sysfs_emit(buf, SCMI_UEVENT_MODALIAS_FMT, ffa_dev->vm_id, 78*22779149SSudeep Holla &ffa_dev->uuid); 79*22779149SSudeep Holla } 80*22779149SSudeep Holla static DEVICE_ATTR_RO(modalias); 81*22779149SSudeep Holla 82e7818584SSudeep Holla static ssize_t partition_id_show(struct device *dev, 83e7818584SSudeep Holla struct device_attribute *attr, char *buf) 84e7818584SSudeep Holla { 85e7818584SSudeep Holla struct ffa_device *ffa_dev = to_ffa_dev(dev); 86e7818584SSudeep Holla 87e7818584SSudeep Holla return sprintf(buf, "0x%04x\n", ffa_dev->vm_id); 88e7818584SSudeep Holla } 89e7818584SSudeep Holla static DEVICE_ATTR_RO(partition_id); 90e7818584SSudeep Holla 91e7818584SSudeep Holla static ssize_t uuid_show(struct device *dev, struct device_attribute *attr, 92e7818584SSudeep Holla char *buf) 93e7818584SSudeep Holla { 94e7818584SSudeep Holla struct ffa_device *ffa_dev = to_ffa_dev(dev); 95e7818584SSudeep Holla 96e7818584SSudeep Holla return sprintf(buf, "%pUb\n", &ffa_dev->uuid); 97e7818584SSudeep Holla } 98e7818584SSudeep Holla static DEVICE_ATTR_RO(uuid); 99e7818584SSudeep Holla 100e7818584SSudeep Holla static struct attribute *ffa_device_attributes_attrs[] = { 101e7818584SSudeep Holla &dev_attr_partition_id.attr, 102e7818584SSudeep Holla &dev_attr_uuid.attr, 103*22779149SSudeep Holla &dev_attr_modalias.attr, 104e7818584SSudeep Holla NULL, 105e7818584SSudeep Holla }; 106e7818584SSudeep Holla ATTRIBUTE_GROUPS(ffa_device_attributes); 107e7818584SSudeep Holla 108e7818584SSudeep Holla struct bus_type ffa_bus_type = { 109e7818584SSudeep Holla .name = "arm_ffa", 110e7818584SSudeep Holla .match = ffa_device_match, 111e7818584SSudeep Holla .probe = ffa_device_probe, 112244f5d59SSudeep Holla .remove = ffa_device_remove, 113e7818584SSudeep Holla .uevent = ffa_device_uevent, 114e7818584SSudeep Holla .dev_groups = ffa_device_attributes_groups, 115e7818584SSudeep Holla }; 116e7818584SSudeep Holla EXPORT_SYMBOL_GPL(ffa_bus_type); 117e7818584SSudeep Holla 118e7818584SSudeep Holla int ffa_driver_register(struct ffa_driver *driver, struct module *owner, 119e7818584SSudeep Holla const char *mod_name) 120e7818584SSudeep Holla { 121e7818584SSudeep Holla int ret; 122e7818584SSudeep Holla 12392743071SUwe Kleine-König if (!driver->probe) 12492743071SUwe Kleine-König return -EINVAL; 12592743071SUwe Kleine-König 126e7818584SSudeep Holla driver->driver.bus = &ffa_bus_type; 127e7818584SSudeep Holla driver->driver.name = driver->name; 128e7818584SSudeep Holla driver->driver.owner = owner; 129e7818584SSudeep Holla driver->driver.mod_name = mod_name; 130e7818584SSudeep Holla 131e7818584SSudeep Holla ret = driver_register(&driver->driver); 132e7818584SSudeep Holla if (!ret) 133e7818584SSudeep Holla pr_debug("registered new ffa driver %s\n", driver->name); 134e7818584SSudeep Holla 135e7818584SSudeep Holla return ret; 136e7818584SSudeep Holla } 137e7818584SSudeep Holla EXPORT_SYMBOL_GPL(ffa_driver_register); 138e7818584SSudeep Holla 139e7818584SSudeep Holla void ffa_driver_unregister(struct ffa_driver *driver) 140e7818584SSudeep Holla { 141e7818584SSudeep Holla driver_unregister(&driver->driver); 142e7818584SSudeep Holla } 143e7818584SSudeep Holla EXPORT_SYMBOL_GPL(ffa_driver_unregister); 144e7818584SSudeep Holla 145e7818584SSudeep Holla static void ffa_release_device(struct device *dev) 146e7818584SSudeep Holla { 147e7818584SSudeep Holla struct ffa_device *ffa_dev = to_ffa_dev(dev); 148e7818584SSudeep Holla 14919b87664SSudeep Holla ida_free(&ffa_bus_id, ffa_dev->id); 150e7818584SSudeep Holla kfree(ffa_dev); 151e7818584SSudeep Holla } 152e7818584SSudeep Holla 153e7818584SSudeep Holla static int __ffa_devices_unregister(struct device *dev, void *data) 154e7818584SSudeep Holla { 155eb7b52e6SSudeep Holla device_unregister(dev); 156e7818584SSudeep Holla 157e7818584SSudeep Holla return 0; 158e7818584SSudeep Holla } 159e7818584SSudeep Holla 160e7818584SSudeep Holla static void ffa_devices_unregister(void) 161e7818584SSudeep Holla { 162e7818584SSudeep Holla bus_for_each_dev(&ffa_bus_type, NULL, NULL, 163e7818584SSudeep Holla __ffa_devices_unregister); 164e7818584SSudeep Holla } 165e7818584SSudeep Holla 166e7818584SSudeep Holla bool ffa_device_is_valid(struct ffa_device *ffa_dev) 167e7818584SSudeep Holla { 168e7818584SSudeep Holla bool valid = false; 169e7818584SSudeep Holla struct device *dev = NULL; 170e7818584SSudeep Holla struct ffa_device *tmp_dev; 171e7818584SSudeep Holla 172e7818584SSudeep Holla do { 173e7818584SSudeep Holla dev = bus_find_next_device(&ffa_bus_type, dev); 174e7818584SSudeep Holla tmp_dev = to_ffa_dev(dev); 175e7818584SSudeep Holla if (tmp_dev == ffa_dev) { 176e7818584SSudeep Holla valid = true; 177e7818584SSudeep Holla break; 178e7818584SSudeep Holla } 179e7818584SSudeep Holla put_device(dev); 180e7818584SSudeep Holla } while (dev); 181e7818584SSudeep Holla 182e7818584SSudeep Holla put_device(dev); 183e7818584SSudeep Holla 184e7818584SSudeep Holla return valid; 185e7818584SSudeep Holla } 186e7818584SSudeep Holla 187d01387fcSSudeep Holla struct ffa_device *ffa_device_register(const uuid_t *uuid, int vm_id, 1887aa7a979SSudeep Holla const struct ffa_ops *ops) 189e7818584SSudeep Holla { 19019b87664SSudeep Holla int id, ret; 191e7818584SSudeep Holla struct device *dev; 192e7818584SSudeep Holla struct ffa_device *ffa_dev; 193e7818584SSudeep Holla 19419b87664SSudeep Holla id = ida_alloc_min(&ffa_bus_id, 1, GFP_KERNEL); 19519b87664SSudeep Holla if (id < 0) 196e7818584SSudeep Holla return NULL; 197e7818584SSudeep Holla 19819b87664SSudeep Holla ffa_dev = kzalloc(sizeof(*ffa_dev), GFP_KERNEL); 19919b87664SSudeep Holla if (!ffa_dev) { 20019b87664SSudeep Holla ida_free(&ffa_bus_id, id); 20119b87664SSudeep Holla return NULL; 20219b87664SSudeep Holla } 20319b87664SSudeep Holla 204e7818584SSudeep Holla dev = &ffa_dev->dev; 205e7818584SSudeep Holla dev->bus = &ffa_bus_type; 206e7818584SSudeep Holla dev->release = ffa_release_device; 20719b87664SSudeep Holla dev_set_name(&ffa_dev->dev, "arm-ffa-%d", id); 208e7818584SSudeep Holla 2097d0bc636SSudeep Holla ffa_dev->id = id; 210e7818584SSudeep Holla ffa_dev->vm_id = vm_id; 211d01387fcSSudeep Holla ffa_dev->ops = ops; 212e7818584SSudeep Holla uuid_copy(&ffa_dev->uuid, uuid); 213e7818584SSudeep Holla 214e7818584SSudeep Holla ret = device_register(&ffa_dev->dev); 215e7818584SSudeep Holla if (ret) { 216e7818584SSudeep Holla dev_err(dev, "unable to register device %s err=%d\n", 217e7818584SSudeep Holla dev_name(dev), ret); 218e7818584SSudeep Holla put_device(dev); 219e7818584SSudeep Holla return NULL; 220e7818584SSudeep Holla } 221e7818584SSudeep Holla 222e7818584SSudeep Holla return ffa_dev; 223e7818584SSudeep Holla } 224e7818584SSudeep Holla EXPORT_SYMBOL_GPL(ffa_device_register); 225e7818584SSudeep Holla 226e7818584SSudeep Holla void ffa_device_unregister(struct ffa_device *ffa_dev) 227e7818584SSudeep Holla { 228e7818584SSudeep Holla if (!ffa_dev) 229e7818584SSudeep Holla return; 230e7818584SSudeep Holla 231e7818584SSudeep Holla device_unregister(&ffa_dev->dev); 232e7818584SSudeep Holla } 233e7818584SSudeep Holla EXPORT_SYMBOL_GPL(ffa_device_unregister); 234e7818584SSudeep Holla 2353bbfe987SSudeep Holla int arm_ffa_bus_init(void) 236e7818584SSudeep Holla { 237e7818584SSudeep Holla return bus_register(&ffa_bus_type); 238e7818584SSudeep Holla } 239e7818584SSudeep Holla 2403bbfe987SSudeep Holla void arm_ffa_bus_exit(void) 241e7818584SSudeep Holla { 242e7818584SSudeep Holla ffa_devices_unregister(); 243e7818584SSudeep Holla bus_unregister(&ffa_bus_type); 24419b87664SSudeep Holla ida_destroy(&ffa_bus_id); 245e7818584SSudeep Holla } 246