1 // SPDX-License-Identifier: GPL-2.0 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/idr.h> 12 13 #include <drm/drm_accel.h> 14 #include <drm/drm_debugfs.h> 15 #include <drm/drm_drv.h> 16 #include <drm/drm_file.h> 17 #include <drm/drm_ioctl.h> 18 #include <drm/drm_print.h> 19 20 static DEFINE_SPINLOCK(accel_minor_lock); 21 static struct idr accel_minors_idr; 22 23 static struct dentry *accel_debugfs_root; 24 static struct class *accel_class; 25 26 static struct device_type accel_sysfs_device_minor = { 27 .name = "accel_minor" 28 }; 29 30 static char *accel_devnode(const struct device *dev, umode_t *mode) 31 { 32 return kasprintf(GFP_KERNEL, "accel/%s", dev_name(dev)); 33 } 34 35 static int accel_sysfs_init(void) 36 { 37 accel_class = class_create("accel"); 38 if (IS_ERR(accel_class)) 39 return PTR_ERR(accel_class); 40 41 accel_class->devnode = accel_devnode; 42 43 return 0; 44 } 45 46 static void accel_sysfs_destroy(void) 47 { 48 if (IS_ERR_OR_NULL(accel_class)) 49 return; 50 class_destroy(accel_class); 51 accel_class = NULL; 52 } 53 54 static int accel_name_info(struct seq_file *m, void *data) 55 { 56 struct drm_info_node *node = (struct drm_info_node *) m->private; 57 struct drm_minor *minor = node->minor; 58 struct drm_device *dev = minor->dev; 59 struct drm_master *master; 60 61 mutex_lock(&dev->master_mutex); 62 master = dev->master; 63 seq_printf(m, "%s", dev->driver->name); 64 if (dev->dev) 65 seq_printf(m, " dev=%s", dev_name(dev->dev)); 66 if (master && master->unique) 67 seq_printf(m, " master=%s", master->unique); 68 if (dev->unique) 69 seq_printf(m, " unique=%s", dev->unique); 70 seq_puts(m, "\n"); 71 mutex_unlock(&dev->master_mutex); 72 73 return 0; 74 } 75 76 static const struct drm_info_list accel_debugfs_list[] = { 77 {"name", accel_name_info, 0} 78 }; 79 #define ACCEL_DEBUGFS_ENTRIES ARRAY_SIZE(accel_debugfs_list) 80 81 /** 82 * accel_debugfs_init() - Initialize debugfs for device 83 * @dev: Pointer to the device instance. 84 * 85 * This function creates a root directory for the device in debugfs. 86 */ 87 void accel_debugfs_init(struct drm_device *dev) 88 { 89 drm_debugfs_dev_init(dev, accel_debugfs_root); 90 } 91 92 /** 93 * accel_debugfs_register() - Register debugfs for device 94 * @dev: Pointer to the device instance. 95 * 96 * Creates common files for accelerators. 97 */ 98 void accel_debugfs_register(struct drm_device *dev) 99 { 100 struct drm_minor *minor = dev->accel; 101 102 INIT_LIST_HEAD(&minor->debugfs_list); 103 mutex_init(&minor->debugfs_lock); 104 minor->debugfs_root = dev->debugfs_root; 105 106 drm_debugfs_create_files(accel_debugfs_list, ACCEL_DEBUGFS_ENTRIES, 107 dev->debugfs_root, minor); 108 } 109 110 /** 111 * accel_set_device_instance_params() - Set some device parameters for accel device 112 * @kdev: Pointer to the device instance. 113 * @index: The minor's index 114 * 115 * This function creates the dev_t of the device using the accel major and 116 * the device's minor number. In addition, it sets the class and type of the 117 * device instance to the accel sysfs class and device type, respectively. 118 */ 119 void accel_set_device_instance_params(struct device *kdev, int index) 120 { 121 kdev->devt = MKDEV(ACCEL_MAJOR, index); 122 kdev->class = accel_class; 123 kdev->type = &accel_sysfs_device_minor; 124 } 125 126 /** 127 * accel_minor_alloc() - Allocates a new accel minor 128 * 129 * This function access the accel minors idr and allocates from it 130 * a new id to represent a new accel minor 131 * 132 * Return: A new id on success or error code in case idr_alloc failed 133 */ 134 int accel_minor_alloc(void) 135 { 136 unsigned long flags; 137 int r; 138 139 spin_lock_irqsave(&accel_minor_lock, flags); 140 r = idr_alloc(&accel_minors_idr, NULL, 0, ACCEL_MAX_MINORS, GFP_NOWAIT); 141 spin_unlock_irqrestore(&accel_minor_lock, flags); 142 143 return r; 144 } 145 146 /** 147 * accel_minor_remove() - Remove an accel minor 148 * @index: The minor id to remove. 149 * 150 * This function access the accel minors idr and removes from 151 * it the member with the id that is passed to this function. 152 */ 153 void accel_minor_remove(int index) 154 { 155 unsigned long flags; 156 157 spin_lock_irqsave(&accel_minor_lock, flags); 158 idr_remove(&accel_minors_idr, index); 159 spin_unlock_irqrestore(&accel_minor_lock, flags); 160 } 161 162 /** 163 * accel_minor_replace() - Replace minor pointer in accel minors idr. 164 * @minor: Pointer to the new minor. 165 * @index: The minor id to replace. 166 * 167 * This function access the accel minors idr structure and replaces the pointer 168 * that is associated with an existing id. Because the minor pointer can be 169 * NULL, we need to explicitly pass the index. 170 * 171 * Return: 0 for success, negative value for error 172 */ 173 void accel_minor_replace(struct drm_minor *minor, int index) 174 { 175 unsigned long flags; 176 177 spin_lock_irqsave(&accel_minor_lock, flags); 178 idr_replace(&accel_minors_idr, minor, index); 179 spin_unlock_irqrestore(&accel_minor_lock, flags); 180 } 181 182 /* 183 * Looks up the given minor-ID and returns the respective DRM-minor object. The 184 * refence-count of the underlying device is increased so you must release this 185 * object with accel_minor_release(). 186 * 187 * The object can be only a drm_minor that represents an accel device. 188 * 189 * As long as you hold this minor, it is guaranteed that the object and the 190 * minor->dev pointer will stay valid! However, the device may get unplugged and 191 * unregistered while you hold the minor. 192 */ 193 static struct drm_minor *accel_minor_acquire(unsigned int minor_id) 194 { 195 struct drm_minor *minor; 196 unsigned long flags; 197 198 spin_lock_irqsave(&accel_minor_lock, flags); 199 minor = idr_find(&accel_minors_idr, minor_id); 200 if (minor) 201 drm_dev_get(minor->dev); 202 spin_unlock_irqrestore(&accel_minor_lock, flags); 203 204 if (!minor) { 205 return ERR_PTR(-ENODEV); 206 } else if (drm_dev_is_unplugged(minor->dev)) { 207 drm_dev_put(minor->dev); 208 return ERR_PTR(-ENODEV); 209 } 210 211 return minor; 212 } 213 214 static void accel_minor_release(struct drm_minor *minor) 215 { 216 drm_dev_put(minor->dev); 217 } 218 219 /** 220 * accel_open - open method for ACCEL file 221 * @inode: device inode 222 * @filp: file pointer. 223 * 224 * This function must be used by drivers as their &file_operations.open method. 225 * It looks up the correct ACCEL device and instantiates all the per-file 226 * resources for it. It also calls the &drm_driver.open driver callback. 227 * 228 * Return: 0 on success or negative errno value on failure. 229 */ 230 int accel_open(struct inode *inode, struct file *filp) 231 { 232 struct drm_device *dev; 233 struct drm_minor *minor; 234 int retcode; 235 236 minor = accel_minor_acquire(iminor(inode)); 237 if (IS_ERR(minor)) 238 return PTR_ERR(minor); 239 240 dev = minor->dev; 241 242 atomic_fetch_inc(&dev->open_count); 243 244 /* share address_space across all char-devs of a single device */ 245 filp->f_mapping = dev->anon_inode->i_mapping; 246 247 retcode = drm_open_helper(filp, minor); 248 if (retcode) 249 goto err_undo; 250 251 return 0; 252 253 err_undo: 254 atomic_dec(&dev->open_count); 255 accel_minor_release(minor); 256 return retcode; 257 } 258 EXPORT_SYMBOL_GPL(accel_open); 259 260 static int accel_stub_open(struct inode *inode, struct file *filp) 261 { 262 const struct file_operations *new_fops; 263 struct drm_minor *minor; 264 int err; 265 266 minor = accel_minor_acquire(iminor(inode)); 267 if (IS_ERR(minor)) 268 return PTR_ERR(minor); 269 270 new_fops = fops_get(minor->dev->driver->fops); 271 if (!new_fops) { 272 err = -ENODEV; 273 goto out; 274 } 275 276 replace_fops(filp, new_fops); 277 if (filp->f_op->open) 278 err = filp->f_op->open(inode, filp); 279 else 280 err = 0; 281 282 out: 283 accel_minor_release(minor); 284 285 return err; 286 } 287 288 static const struct file_operations accel_stub_fops = { 289 .owner = THIS_MODULE, 290 .open = accel_stub_open, 291 .llseek = noop_llseek, 292 }; 293 294 void accel_core_exit(void) 295 { 296 unregister_chrdev(ACCEL_MAJOR, "accel"); 297 debugfs_remove(accel_debugfs_root); 298 accel_sysfs_destroy(); 299 idr_destroy(&accel_minors_idr); 300 } 301 302 int __init accel_core_init(void) 303 { 304 int ret; 305 306 idr_init(&accel_minors_idr); 307 308 ret = accel_sysfs_init(); 309 if (ret < 0) { 310 DRM_ERROR("Cannot create ACCEL class: %d\n", ret); 311 goto error; 312 } 313 314 accel_debugfs_root = debugfs_create_dir("accel", NULL); 315 316 ret = register_chrdev(ACCEL_MAJOR, "accel", &accel_stub_fops); 317 if (ret < 0) 318 DRM_ERROR("Cannot register ACCEL major: %d\n", ret); 319 320 error: 321 /* 322 * Any cleanup due to errors will be done in drm_core_exit() that 323 * will call accel_core_exit() 324 */ 325 return ret; 326 } 327