| 1 | |
| 2 |
|
| 3 | |
| 4 | * Copyright 2022 HabanaLabs, Ltd.
|
| 5 | * All Rights Reserved.
|
| 6 | *
|
| 7 |
|
| 8 |
|
| 9 | #include <linux/debugfs.h>
|
| 10 | #include <linux/device.h>
|
| 11 | #include <linux/xarray.h>
|
| 12 |
|
| 13 | #include <drm/drm_accel.h>
|
| 14 | #include <drm/drm_auth.h>
|
| 15 | #include <drm/drm_debugfs.h>
|
| 16 | #include <drm/drm_drv.h>
|
| 17 | #include <drm/drm_file.h>
|
| 18 | #include <drm/drm_ioctl.h>
|
| 19 | #include <drm/drm_print.h>
|
| 20 |
|
| 21 | DEFINE_XARRAY_ALLOC(accel_minors_xa);
|
| 22 |
|
| 23 | static const struct device_type accel_sysfs_device_minor = {
|
| 24 | .name = "accel_minor"
|
| 25 | };
|
| 26 |
|
| 27 | static char *accel_devnode(const struct device *dev, umode_t *mode)
|
| 28 | {
|
| 29 | return kasprintf(GFP_KERNEL, "accel/%s", dev_name(dev));
|
| 30 | }
|
| 31 |
|
| 32 | static const struct class accel_class = {
|
| 33 | .name = "accel",
|
| 34 | .devnode = accel_devnode,
|
| 35 | };
|
| 36 |
|
| 37 | static int accel_sysfs_init(void)
|
| 38 | {
|
| 39 | return class_register(&accel_class);
|
| 40 | }
|
| 41 |
|
| 42 | static void accel_sysfs_destroy(void)
|
| 43 | {
|
| 44 | class_unregister(&accel_class);
|
| 45 | }
|
| 46 |
|
| 47 | static int accel_name_info(struct seq_file *m, void *data)
|
| 48 | {
|
| 49 | struct drm_info_node *node = (struct drm_info_node *) m->private;
|
| 50 | struct drm_minor *minor = node->minor;
|
| 51 | struct drm_device *dev = minor->dev;
|
| 52 | struct drm_master *master;
|
| 53 |
|
| 54 | mutex_lock(&dev->master_mutex);
|
| 55 | master = dev->master;
|
| 56 | seq_printf(m, "%s", dev->driver->name);
|
| 57 | if (dev->dev)
|
| 58 | seq_printf(m, " dev=%s", dev_name(dev->dev));
|
| 59 | if (master && master->unique)
|
| 60 | seq_printf(m, " master=%s", master->unique);
|
| 61 | if (dev->unique)
|
| 62 | seq_printf(m, " unique=%s", dev->unique);
|
| 63 | seq_puts(m, "\n");
|
| 64 | mutex_unlock(&dev->master_mutex);
|
| 65 |
|
| 66 | return 0;
|
| 67 | }
|
| 68 |
|
| 69 | static const struct drm_info_list accel_debugfs_list[] = {
|
| 70 | {"name", accel_name_info, 0}
|
| 71 | };
|
| 72 | #define ACCEL_DEBUGFS_ENTRIES ARRAY_SIZE(accel_debugfs_list)
|
| 73 |
|
| 74 | |
| 75 | * accel_debugfs_register() - Register debugfs for device
|
| 76 | * @dev: Pointer to the device instance.
|
| 77 | *
|
| 78 | * Creates common files for accelerators.
|
| 79 |
|
| 80 | void accel_debugfs_register(struct drm_device *dev)
|
| 81 | {
|
| 82 | struct drm_minor *minor = dev->accel;
|
| 83 |
|
| 84 | minor->debugfs_root = dev->debugfs_root;
|
| 85 |
|
| 86 | drm_debugfs_create_files(accel_debugfs_list, ACCEL_DEBUGFS_ENTRIES,
|
| 87 | dev->debugfs_root, minor);
|
| 88 | }
|
| 89 |
|
| 90 | |
| 91 | * accel_set_device_instance_params() - Set some device parameters for accel device
|
| 92 | * @kdev: Pointer to the device instance.
|
| 93 | * @index: The minor's index
|
| 94 | *
|
| 95 | * This function creates the dev_t of the device using the accel major and
|
| 96 | * the device's minor number. In addition, it sets the class and type of the
|
| 97 | * device instance to the accel sysfs class and device type, respectively.
|
| 98 |
|
| 99 | void accel_set_device_instance_params(struct device *kdev, int index)
|
| 100 | {
|
| 101 | kdev->devt = MKDEV(ACCEL_MAJOR, index);
|
| 102 | kdev->class = &accel_class;
|
| 103 | kdev->type = &accel_sysfs_device_minor;
|
| 104 | }
|
| 105 |
|
| 106 | |
| 107 | * accel_open - open method for ACCEL file
|
| 108 | * @inode: device inode
|
| 109 | * @filp: file pointer.
|
| 110 | *
|
| 111 | * This function must be used by drivers as their &file_operations.open method.
|
| 112 | * It looks up the correct ACCEL device and instantiates all the per-file
|
| 113 | * resources for it. It also calls the &drm_driver.open driver callback.
|
| 114 | *
|
| 115 | * Return: 0 on success or negative errno value on failure.
|
| 116 |
|
| 117 | int accel_open(struct inode *inode, struct file *filp)
|
| 118 | {
|
| 119 | struct drm_device *dev;
|
| 120 | struct drm_minor *minor;
|
| 121 | int retcode;
|
| 122 |
|
| 123 | minor = drm_minor_acquire(&accel_minors_xa, iminor(inode));
|
| 124 | if (IS_ERR(minor))
|
| 125 | return PTR_ERR(minor);
|
| 126 |
|
| 127 | dev = minor->dev;
|
| 128 |
|
| 129 | atomic_fetch_inc(&dev->open_count);
|
| 130 |
|
| 131 |
|
| 132 | filp->f_mapping = dev->anon_inode->i_mapping;
|
| 133 |
|
| 134 | retcode = drm_open_helper(filp, minor);
|
| 135 | if (retcode)
|
| 136 | goto err_undo;
|
| 137 |
|
| 138 | return 0;
|
| 139 |
|
| 140 | err_undo:
|
| 141 | atomic_dec(&dev->open_count);
|
| 142 | drm_minor_release(minor);
|
| 143 | return retcode;
|
| 144 | }
|
| 145 | EXPORT_SYMBOL_GPL(accel_open);
|
| 146 |
|
| 147 | static int accel_stub_open(struct inode *inode, struct file *filp)
|
| 148 | {
|
| 149 | const struct file_operations *new_fops;
|
| 150 | struct drm_minor *minor;
|
| 151 | int err;
|
| 152 |
|
| 153 | minor = drm_minor_acquire(&accel_minors_xa, iminor(inode));
|
| 154 | if (IS_ERR(minor))
|
| 155 | return PTR_ERR(minor);
|
| 156 |
|
| 157 | new_fops = fops_get(minor->dev->driver->fops);
|
| 158 | if (!new_fops) {
|
| 159 | err = -ENODEV;
|
| 160 | goto out;
|
| 161 | }
|
| 162 |
|
| 163 | replace_fops(filp, new_fops);
|
| 164 | if (filp->f_op->open)
|
| 165 | err = filp->f_op->open(inode, filp);
|
| 166 | else
|
| 167 | err = 0;
|
| 168 |
|
| 169 | out:
|
| 170 | drm_minor_release(minor);
|
| 171 |
|
| 172 | return err;
|
| 173 | }
|
| 174 |
|
| 175 | static const struct file_operations accel_stub_fops = {
|
| 176 | .owner = THIS_MODULE,
|
| 177 | .open = accel_stub_open,
|
| 178 | .llseek = noop_llseek,
|
| 179 | };
|
| 180 |
|
| 181 | void accel_core_exit(void)
|
| 182 | {
|
| 183 | unregister_chrdev(ACCEL_MAJOR, "accel");
|
| 184 | accel_sysfs_destroy();
|
| 185 | WARN_ON(!xa_empty(&accel_minors_xa));
|
| 186 | }
|
| 187 |
|
| 188 | int __init accel_core_init(void)
|
| 189 | {
|
| 190 | int ret;
|
| 191 |
|
| 192 | ret = accel_sysfs_init();
|
| 193 | if (ret < 0) {
|
| 194 | DRM_ERROR("Cannot create ACCEL class: %d\n", ret);
|
| 195 | goto error;
|
| 196 | }
|
| 197 |
|
| 198 | ret = register_chrdev(ACCEL_MAJOR, "accel", &accel_stub_fops);
|
| 199 | if (ret < 0)
|
| 200 | DRM_ERROR("Cannot register ACCEL major: %d\n", ret);
|
| 201 |
|
| 202 | error:
|
| 203 | |
| 204 | * Any cleanup due to errors will be done in drm_core_exit() that
|
| 205 | * will call accel_core_exit()
|
| 206 |
|
| 207 | return ret;
|
| 208 | }
|