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