1 1.5 riastrad /* $NetBSD: drm_file.c,v 1.5 2021/12/19 11:21:03 riastradh Exp $ */ 2 1.1 riastrad 3 1.1 riastrad /* 4 1.1 riastrad * \author Rickard E. (Rik) Faith <faith (at) valinux.com> 5 1.1 riastrad * \author Daryll Strauss <daryll (at) valinux.com> 6 1.1 riastrad * \author Gareth Hughes <gareth (at) valinux.com> 7 1.1 riastrad */ 8 1.1 riastrad 9 1.1 riastrad /* 10 1.1 riastrad * Created: Mon Jan 4 08:58:31 1999 by faith (at) valinux.com 11 1.1 riastrad * 12 1.1 riastrad * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas. 13 1.1 riastrad * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California. 14 1.1 riastrad * All Rights Reserved. 15 1.1 riastrad * 16 1.1 riastrad * Permission is hereby granted, free of charge, to any person obtaining a 17 1.1 riastrad * copy of this software and associated documentation files (the "Software"), 18 1.1 riastrad * to deal in the Software without restriction, including without limitation 19 1.1 riastrad * the rights to use, copy, modify, merge, publish, distribute, sublicense, 20 1.1 riastrad * and/or sell copies of the Software, and to permit persons to whom the 21 1.1 riastrad * Software is furnished to do so, subject to the following conditions: 22 1.1 riastrad * 23 1.1 riastrad * The above copyright notice and this permission notice (including the next 24 1.1 riastrad * paragraph) shall be included in all copies or substantial portions of the 25 1.1 riastrad * Software. 26 1.1 riastrad * 27 1.1 riastrad * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 28 1.1 riastrad * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 29 1.1 riastrad * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 30 1.1 riastrad * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 31 1.1 riastrad * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 32 1.1 riastrad * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 33 1.1 riastrad * OTHER DEALINGS IN THE SOFTWARE. 34 1.1 riastrad */ 35 1.1 riastrad 36 1.1 riastrad #include <sys/cdefs.h> 37 1.5 riastrad __KERNEL_RCSID(0, "$NetBSD: drm_file.c,v 1.5 2021/12/19 11:21:03 riastradh Exp $"); 38 1.1 riastrad 39 1.1 riastrad #include <linux/anon_inodes.h> 40 1.1 riastrad #include <linux/dma-fence.h> 41 1.1 riastrad #include <linux/file.h> 42 1.1 riastrad #include <linux/module.h> 43 1.1 riastrad #include <linux/pci.h> 44 1.1 riastrad #include <linux/poll.h> 45 1.1 riastrad #include <linux/slab.h> 46 1.1 riastrad 47 1.1 riastrad #include <drm/drm_client.h> 48 1.1 riastrad #include <drm/drm_drv.h> 49 1.1 riastrad #include <drm/drm_file.h> 50 1.1 riastrad #include <drm/drm_print.h> 51 1.1 riastrad 52 1.1 riastrad #include "drm_crtc_internal.h" 53 1.1 riastrad #include "drm_internal.h" 54 1.1 riastrad #include "drm_legacy.h" 55 1.1 riastrad 56 1.3 riastrad #ifdef __NetBSD__ 57 1.3 riastrad #include <sys/poll.h> 58 1.3 riastrad #include <sys/select.h> 59 1.3 riastrad #endif 60 1.3 riastrad 61 1.3 riastrad #include <linux/nbsd-namespace.h> 62 1.3 riastrad 63 1.1 riastrad /* from BKL pushdown */ 64 1.3 riastrad #ifndef __NetBSD__ 65 1.1 riastrad DEFINE_MUTEX(drm_global_mutex); 66 1.3 riastrad #endif 67 1.1 riastrad 68 1.1 riastrad /** 69 1.1 riastrad * DOC: file operations 70 1.1 riastrad * 71 1.1 riastrad * Drivers must define the file operations structure that forms the DRM 72 1.1 riastrad * userspace API entry point, even though most of those operations are 73 1.1 riastrad * implemented in the DRM core. The resulting &struct file_operations must be 74 1.1 riastrad * stored in the &drm_driver.fops field. The mandatory functions are drm_open(), 75 1.1 riastrad * drm_read(), drm_ioctl() and drm_compat_ioctl() if CONFIG_COMPAT is enabled 76 1.1 riastrad * Note that drm_compat_ioctl will be NULL if CONFIG_COMPAT=n, so there's no 77 1.1 riastrad * need to sprinkle #ifdef into the code. Drivers which implement private ioctls 78 1.1 riastrad * that require 32/64 bit compatibility support must provide their own 79 1.1 riastrad * &file_operations.compat_ioctl handler that processes private ioctls and calls 80 1.1 riastrad * drm_compat_ioctl() for core ioctls. 81 1.1 riastrad * 82 1.1 riastrad * In addition drm_read() and drm_poll() provide support for DRM events. DRM 83 1.1 riastrad * events are a generic and extensible means to send asynchronous events to 84 1.1 riastrad * userspace through the file descriptor. They are used to send vblank event and 85 1.1 riastrad * page flip completions by the KMS API. But drivers can also use it for their 86 1.1 riastrad * own needs, e.g. to signal completion of rendering. 87 1.1 riastrad * 88 1.1 riastrad * For the driver-side event interface see drm_event_reserve_init() and 89 1.1 riastrad * drm_send_event() as the main starting points. 90 1.1 riastrad * 91 1.1 riastrad * The memory mapping implementation will vary depending on how the driver 92 1.1 riastrad * manages memory. Legacy drivers will use the deprecated drm_legacy_mmap() 93 1.1 riastrad * function, modern drivers should use one of the provided memory-manager 94 1.1 riastrad * specific implementations. For GEM-based drivers this is drm_gem_mmap(), and 95 1.1 riastrad * for drivers which use the CMA GEM helpers it's drm_gem_cma_mmap(). 96 1.1 riastrad * 97 1.1 riastrad * No other file operations are supported by the DRM userspace API. Overall the 98 1.1 riastrad * following is an example &file_operations structure:: 99 1.1 riastrad * 100 1.1 riastrad * static const example_drm_fops = { 101 1.1 riastrad * .owner = THIS_MODULE, 102 1.1 riastrad * .open = drm_open, 103 1.1 riastrad * .release = drm_release, 104 1.1 riastrad * .unlocked_ioctl = drm_ioctl, 105 1.1 riastrad * .compat_ioctl = drm_compat_ioctl, // NULL if CONFIG_COMPAT=n 106 1.1 riastrad * .poll = drm_poll, 107 1.1 riastrad * .read = drm_read, 108 1.1 riastrad * .llseek = no_llseek, 109 1.1 riastrad * .mmap = drm_gem_mmap, 110 1.1 riastrad * }; 111 1.1 riastrad * 112 1.1 riastrad * For plain GEM based drivers there is the DEFINE_DRM_GEM_FOPS() macro, and for 113 1.1 riastrad * CMA based drivers there is the DEFINE_DRM_GEM_CMA_FOPS() macro to make this 114 1.1 riastrad * simpler. 115 1.1 riastrad * 116 1.1 riastrad * The driver's &file_operations must be stored in &drm_driver.fops. 117 1.1 riastrad * 118 1.1 riastrad * For driver-private IOCTL handling see the more detailed discussion in 119 1.1 riastrad * :ref:`IOCTL support in the userland interfaces chapter<drm_driver_ioctl>`. 120 1.1 riastrad */ 121 1.1 riastrad 122 1.1 riastrad /** 123 1.1 riastrad * drm_file_alloc - allocate file context 124 1.1 riastrad * @minor: minor to allocate on 125 1.1 riastrad * 126 1.1 riastrad * This allocates a new DRM file context. It is not linked into any context and 127 1.1 riastrad * can be used by the caller freely. Note that the context keeps a pointer to 128 1.1 riastrad * @minor, so it must be freed before @minor is. 129 1.1 riastrad * 130 1.1 riastrad * RETURNS: 131 1.1 riastrad * Pointer to newly allocated context, ERR_PTR on failure. 132 1.1 riastrad */ 133 1.1 riastrad struct drm_file *drm_file_alloc(struct drm_minor *minor) 134 1.1 riastrad { 135 1.1 riastrad struct drm_device *dev = minor->dev; 136 1.1 riastrad struct drm_file *file; 137 1.1 riastrad int ret; 138 1.1 riastrad 139 1.1 riastrad file = kzalloc(sizeof(*file), GFP_KERNEL); 140 1.1 riastrad if (!file) 141 1.1 riastrad return ERR_PTR(-ENOMEM); 142 1.1 riastrad 143 1.3 riastrad #ifndef __NetBSD__ 144 1.1 riastrad file->pid = get_pid(task_pid(current)); 145 1.3 riastrad #endif 146 1.1 riastrad file->minor = minor; 147 1.1 riastrad 148 1.1 riastrad /* for compatibility root is always authenticated */ 149 1.1 riastrad file->authenticated = capable(CAP_SYS_ADMIN); 150 1.1 riastrad 151 1.1 riastrad INIT_LIST_HEAD(&file->lhead); 152 1.1 riastrad INIT_LIST_HEAD(&file->fbs); 153 1.1 riastrad mutex_init(&file->fbs_lock); 154 1.1 riastrad INIT_LIST_HEAD(&file->blobs); 155 1.1 riastrad INIT_LIST_HEAD(&file->pending_event_list); 156 1.1 riastrad INIT_LIST_HEAD(&file->event_list); 157 1.3 riastrad #ifdef __NetBSD__ 158 1.3 riastrad DRM_INIT_WAITQUEUE(&file->event_wait, "drmevent"); 159 1.4 riastrad DRM_INIT_WAITQUEUE(&file->event_read_wq, "drmevtrd"); 160 1.3 riastrad selinit(&file->event_selq); 161 1.3 riastrad #else 162 1.1 riastrad init_waitqueue_head(&file->event_wait); 163 1.3 riastrad #endif 164 1.1 riastrad file->event_space = 4096; /* set aside 4k for event buffer */ 165 1.1 riastrad 166 1.3 riastrad #ifdef __NetBSD__ 167 1.3 riastrad file->event_read_lock = NULL; 168 1.3 riastrad #else 169 1.1 riastrad mutex_init(&file->event_read_lock); 170 1.3 riastrad #endif 171 1.1 riastrad 172 1.1 riastrad if (drm_core_check_feature(dev, DRIVER_GEM)) 173 1.1 riastrad drm_gem_open(dev, file); 174 1.1 riastrad 175 1.1 riastrad if (drm_core_check_feature(dev, DRIVER_SYNCOBJ)) 176 1.1 riastrad drm_syncobj_open(file); 177 1.1 riastrad 178 1.1 riastrad drm_prime_init_file_private(&file->prime); 179 1.1 riastrad 180 1.1 riastrad if (dev->driver->open) { 181 1.1 riastrad ret = dev->driver->open(dev, file); 182 1.1 riastrad if (ret < 0) 183 1.1 riastrad goto out_prime_destroy; 184 1.1 riastrad } 185 1.1 riastrad 186 1.1 riastrad return file; 187 1.1 riastrad 188 1.1 riastrad out_prime_destroy: 189 1.1 riastrad drm_prime_destroy_file_private(&file->prime); 190 1.1 riastrad if (drm_core_check_feature(dev, DRIVER_SYNCOBJ)) 191 1.1 riastrad drm_syncobj_release(file); 192 1.1 riastrad if (drm_core_check_feature(dev, DRIVER_GEM)) 193 1.1 riastrad drm_gem_release(dev, file); 194 1.3 riastrad #ifdef __NetBSD__ 195 1.3 riastrad KASSERT(file->event_read_lock == NULL); 196 1.3 riastrad #else 197 1.3 riastrad mutex_destroy(&file->event_read_lock); 198 1.3 riastrad #endif 199 1.3 riastrad mutex_destroy(&file->fbs_lock); 200 1.3 riastrad #ifdef __NetBSD__ 201 1.3 riastrad DRM_DESTROY_WAITQUEUE(&file->event_wait); 202 1.4 riastrad DRM_DESTROY_WAITQUEUE(&file->event_read_wq); 203 1.3 riastrad seldestroy(&file->event_selq); 204 1.3 riastrad #else 205 1.1 riastrad put_pid(file->pid); 206 1.3 riastrad #endif 207 1.1 riastrad kfree(file); 208 1.1 riastrad 209 1.1 riastrad return ERR_PTR(ret); 210 1.1 riastrad } 211 1.1 riastrad 212 1.1 riastrad static void drm_events_release(struct drm_file *file_priv) 213 1.1 riastrad { 214 1.1 riastrad struct drm_device *dev = file_priv->minor->dev; 215 1.1 riastrad struct drm_pending_event *e, *et; 216 1.1 riastrad unsigned long flags; 217 1.1 riastrad 218 1.1 riastrad spin_lock_irqsave(&dev->event_lock, flags); 219 1.1 riastrad 220 1.1 riastrad /* Unlink pending events */ 221 1.1 riastrad list_for_each_entry_safe(e, et, &file_priv->pending_event_list, 222 1.1 riastrad pending_link) { 223 1.1 riastrad list_del(&e->pending_link); 224 1.1 riastrad e->file_priv = NULL; 225 1.1 riastrad } 226 1.1 riastrad 227 1.1 riastrad /* Remove unconsumed events */ 228 1.1 riastrad list_for_each_entry_safe(e, et, &file_priv->event_list, link) { 229 1.1 riastrad list_del(&e->link); 230 1.1 riastrad kfree(e); 231 1.1 riastrad } 232 1.1 riastrad 233 1.1 riastrad spin_unlock_irqrestore(&dev->event_lock, flags); 234 1.1 riastrad } 235 1.1 riastrad 236 1.1 riastrad /** 237 1.1 riastrad * drm_file_free - free file context 238 1.1 riastrad * @file: context to free, or NULL 239 1.1 riastrad * 240 1.1 riastrad * This destroys and deallocates a DRM file context previously allocated via 241 1.1 riastrad * drm_file_alloc(). The caller must make sure to unlink it from any contexts 242 1.1 riastrad * before calling this. 243 1.1 riastrad * 244 1.1 riastrad * If NULL is passed, this is a no-op. 245 1.1 riastrad * 246 1.1 riastrad * RETURNS: 247 1.1 riastrad * 0 on success, or error code on failure. 248 1.1 riastrad */ 249 1.1 riastrad void drm_file_free(struct drm_file *file) 250 1.1 riastrad { 251 1.1 riastrad struct drm_device *dev; 252 1.1 riastrad 253 1.1 riastrad if (!file) 254 1.1 riastrad return; 255 1.1 riastrad 256 1.1 riastrad dev = file->minor->dev; 257 1.1 riastrad 258 1.1 riastrad DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n", 259 1.1 riastrad task_pid_nr(current), 260 1.3 riastrad #ifdef __NetBSD__ 261 1.3 riastrad (unsigned long)device_unit(file->minor->dev->dev), 262 1.3 riastrad #else 263 1.1 riastrad (long)old_encode_dev(file->minor->kdev->devt), 264 1.3 riastrad #endif 265 1.1 riastrad dev->open_count); 266 1.1 riastrad 267 1.1 riastrad if (drm_core_check_feature(dev, DRIVER_LEGACY) && 268 1.1 riastrad dev->driver->preclose) 269 1.1 riastrad dev->driver->preclose(dev, file); 270 1.1 riastrad 271 1.1 riastrad if (drm_core_check_feature(dev, DRIVER_LEGACY)) 272 1.1 riastrad drm_legacy_lock_release(dev, file->filp); 273 1.1 riastrad 274 1.1 riastrad if (drm_core_check_feature(dev, DRIVER_HAVE_DMA)) 275 1.1 riastrad drm_legacy_reclaim_buffers(dev, file); 276 1.1 riastrad 277 1.1 riastrad drm_events_release(file); 278 1.1 riastrad 279 1.1 riastrad if (drm_core_check_feature(dev, DRIVER_MODESET)) { 280 1.1 riastrad drm_fb_release(file); 281 1.1 riastrad drm_property_destroy_user_blobs(dev, file); 282 1.1 riastrad } 283 1.1 riastrad 284 1.1 riastrad if (drm_core_check_feature(dev, DRIVER_SYNCOBJ)) 285 1.1 riastrad drm_syncobj_release(file); 286 1.1 riastrad 287 1.1 riastrad if (drm_core_check_feature(dev, DRIVER_GEM)) 288 1.1 riastrad drm_gem_release(dev, file); 289 1.1 riastrad 290 1.1 riastrad drm_legacy_ctxbitmap_flush(dev, file); 291 1.1 riastrad 292 1.1 riastrad if (drm_is_primary_client(file)) 293 1.1 riastrad drm_master_release(file); 294 1.1 riastrad 295 1.1 riastrad if (dev->driver->postclose) 296 1.1 riastrad dev->driver->postclose(dev, file); 297 1.1 riastrad 298 1.1 riastrad drm_prime_destroy_file_private(&file->prime); 299 1.1 riastrad 300 1.1 riastrad WARN_ON(!list_empty(&file->event_list)); 301 1.1 riastrad 302 1.3 riastrad #ifdef __NetBSD__ 303 1.3 riastrad DRM_DESTROY_WAITQUEUE(&file->event_wait); 304 1.5 riastrad DRM_DESTROY_WAITQUEUE(&file->event_read_wq); 305 1.3 riastrad seldestroy(&file->event_selq); 306 1.3 riastrad #else 307 1.1 riastrad put_pid(file->pid); 308 1.3 riastrad mutex_destroy(&file->event_read_lock); 309 1.3 riastrad #endif 310 1.3 riastrad mutex_destroy(&file->fbs_lock); 311 1.1 riastrad kfree(file); 312 1.1 riastrad } 313 1.1 riastrad 314 1.3 riastrad #ifndef __NetBSD__ 315 1.1 riastrad static void drm_close_helper(struct file *filp) 316 1.1 riastrad { 317 1.1 riastrad struct drm_file *file_priv = filp->private_data; 318 1.1 riastrad struct drm_device *dev = file_priv->minor->dev; 319 1.1 riastrad 320 1.1 riastrad mutex_lock(&dev->filelist_mutex); 321 1.1 riastrad list_del(&file_priv->lhead); 322 1.1 riastrad mutex_unlock(&dev->filelist_mutex); 323 1.1 riastrad 324 1.1 riastrad drm_file_free(file_priv); 325 1.1 riastrad } 326 1.3 riastrad #endif 327 1.1 riastrad 328 1.1 riastrad /* 329 1.1 riastrad * Check whether DRI will run on this CPU. 330 1.1 riastrad * 331 1.1 riastrad * \return non-zero if the DRI will run on this CPU, or zero otherwise. 332 1.1 riastrad */ 333 1.3 riastrad __unused 334 1.1 riastrad static int drm_cpu_valid(void) 335 1.1 riastrad { 336 1.1 riastrad #if defined(__sparc__) && !defined(__sparc_v9__) 337 1.1 riastrad return 0; /* No cmpxchg before v9 sparc. */ 338 1.1 riastrad #endif 339 1.1 riastrad return 1; 340 1.1 riastrad } 341 1.1 riastrad 342 1.1 riastrad /* 343 1.1 riastrad * Called whenever a process opens a drm node 344 1.1 riastrad * 345 1.1 riastrad * \param filp file pointer. 346 1.1 riastrad * \param minor acquired minor-object. 347 1.1 riastrad * \return zero on success or a negative number on failure. 348 1.1 riastrad * 349 1.1 riastrad * Creates and initializes a drm_file structure for the file private data in \p 350 1.1 riastrad * filp and add it into the double linked list in \p dev. 351 1.1 riastrad */ 352 1.3 riastrad #ifndef __NetBSD__ 353 1.1 riastrad static int drm_open_helper(struct file *filp, struct drm_minor *minor) 354 1.1 riastrad { 355 1.1 riastrad struct drm_device *dev = minor->dev; 356 1.1 riastrad struct drm_file *priv; 357 1.1 riastrad int ret; 358 1.1 riastrad 359 1.1 riastrad if (filp->f_flags & O_EXCL) 360 1.1 riastrad return -EBUSY; /* No exclusive opens */ 361 1.1 riastrad if (!drm_cpu_valid()) 362 1.1 riastrad return -EINVAL; 363 1.1 riastrad if (dev->switch_power_state != DRM_SWITCH_POWER_ON && dev->switch_power_state != DRM_SWITCH_POWER_DYNAMIC_OFF) 364 1.1 riastrad return -EINVAL; 365 1.1 riastrad 366 1.1 riastrad DRM_DEBUG("pid = %d, minor = %d\n", task_pid_nr(current), minor->index); 367 1.1 riastrad 368 1.1 riastrad priv = drm_file_alloc(minor); 369 1.1 riastrad if (IS_ERR(priv)) 370 1.1 riastrad return PTR_ERR(priv); 371 1.1 riastrad 372 1.1 riastrad if (drm_is_primary_client(priv)) { 373 1.1 riastrad ret = drm_master_open(priv); 374 1.1 riastrad if (ret) { 375 1.1 riastrad drm_file_free(priv); 376 1.1 riastrad return ret; 377 1.1 riastrad } 378 1.1 riastrad } 379 1.1 riastrad 380 1.1 riastrad filp->private_data = priv; 381 1.1 riastrad filp->f_mode |= FMODE_UNSIGNED_OFFSET; 382 1.1 riastrad priv->filp = filp; 383 1.1 riastrad 384 1.1 riastrad mutex_lock(&dev->filelist_mutex); 385 1.1 riastrad list_add(&priv->lhead, &dev->filelist); 386 1.1 riastrad mutex_unlock(&dev->filelist_mutex); 387 1.1 riastrad 388 1.1 riastrad #ifdef __alpha__ 389 1.1 riastrad /* 390 1.1 riastrad * Default the hose 391 1.1 riastrad */ 392 1.1 riastrad if (!dev->hose) { 393 1.1 riastrad struct pci_dev *pci_dev; 394 1.1 riastrad pci_dev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL); 395 1.1 riastrad if (pci_dev) { 396 1.1 riastrad dev->hose = pci_dev->sysdata; 397 1.1 riastrad pci_dev_put(pci_dev); 398 1.1 riastrad } 399 1.1 riastrad if (!dev->hose) { 400 1.1 riastrad struct pci_bus *b = list_entry(pci_root_buses.next, 401 1.1 riastrad struct pci_bus, node); 402 1.1 riastrad if (b) 403 1.1 riastrad dev->hose = b->sysdata; 404 1.1 riastrad } 405 1.1 riastrad } 406 1.1 riastrad #endif 407 1.1 riastrad 408 1.1 riastrad return 0; 409 1.1 riastrad } 410 1.3 riastrad #endif 411 1.1 riastrad 412 1.1 riastrad /** 413 1.1 riastrad * drm_open - open method for DRM file 414 1.1 riastrad * @inode: device inode 415 1.1 riastrad * @filp: file pointer. 416 1.1 riastrad * 417 1.1 riastrad * This function must be used by drivers as their &file_operations.open method. 418 1.1 riastrad * It looks up the correct DRM device and instantiates all the per-file 419 1.1 riastrad * resources for it. It also calls the &drm_driver.open driver callback. 420 1.1 riastrad * 421 1.1 riastrad * RETURNS: 422 1.1 riastrad * 423 1.1 riastrad * 0 on success or negative errno value on falure. 424 1.1 riastrad */ 425 1.3 riastrad #ifndef __NetBSD__ 426 1.1 riastrad int drm_open(struct inode *inode, struct file *filp) 427 1.1 riastrad { 428 1.1 riastrad struct drm_device *dev; 429 1.1 riastrad struct drm_minor *minor; 430 1.1 riastrad int retcode; 431 1.1 riastrad int need_setup = 0; 432 1.1 riastrad 433 1.1 riastrad minor = drm_minor_acquire(iminor(inode)); 434 1.1 riastrad if (IS_ERR(minor)) 435 1.1 riastrad return PTR_ERR(minor); 436 1.1 riastrad 437 1.1 riastrad dev = minor->dev; 438 1.1 riastrad if (!dev->open_count++) 439 1.1 riastrad need_setup = 1; 440 1.1 riastrad 441 1.1 riastrad /* share address_space across all char-devs of a single device */ 442 1.1 riastrad filp->f_mapping = dev->anon_inode->i_mapping; 443 1.1 riastrad 444 1.1 riastrad retcode = drm_open_helper(filp, minor); 445 1.1 riastrad if (retcode) 446 1.1 riastrad goto err_undo; 447 1.1 riastrad if (need_setup) { 448 1.1 riastrad retcode = drm_legacy_setup(dev); 449 1.1 riastrad if (retcode) { 450 1.1 riastrad drm_close_helper(filp); 451 1.1 riastrad goto err_undo; 452 1.1 riastrad } 453 1.1 riastrad } 454 1.1 riastrad return 0; 455 1.1 riastrad 456 1.1 riastrad err_undo: 457 1.1 riastrad dev->open_count--; 458 1.1 riastrad drm_minor_release(minor); 459 1.1 riastrad return retcode; 460 1.1 riastrad } 461 1.1 riastrad EXPORT_SYMBOL(drm_open); 462 1.3 riastrad #endif 463 1.1 riastrad 464 1.1 riastrad void drm_lastclose(struct drm_device * dev) 465 1.1 riastrad { 466 1.1 riastrad DRM_DEBUG("\n"); 467 1.1 riastrad 468 1.1 riastrad if (dev->driver->lastclose) 469 1.1 riastrad dev->driver->lastclose(dev); 470 1.1 riastrad DRM_DEBUG("driver lastclose completed\n"); 471 1.1 riastrad 472 1.1 riastrad if (drm_core_check_feature(dev, DRIVER_LEGACY)) 473 1.1 riastrad drm_legacy_dev_reinit(dev); 474 1.1 riastrad 475 1.1 riastrad drm_client_dev_restore(dev); 476 1.1 riastrad } 477 1.1 riastrad 478 1.1 riastrad /** 479 1.1 riastrad * drm_release - release method for DRM file 480 1.1 riastrad * @inode: device inode 481 1.1 riastrad * @filp: file pointer. 482 1.1 riastrad * 483 1.1 riastrad * This function must be used by drivers as their &file_operations.release 484 1.1 riastrad * method. It frees any resources associated with the open file, and calls the 485 1.1 riastrad * &drm_driver.postclose driver callback. If this is the last open file for the 486 1.1 riastrad * DRM device also proceeds to call the &drm_driver.lastclose driver callback. 487 1.1 riastrad * 488 1.1 riastrad * RETURNS: 489 1.1 riastrad * 490 1.1 riastrad * Always succeeds and returns 0. 491 1.1 riastrad */ 492 1.3 riastrad #ifndef __NetBSD__ 493 1.1 riastrad int drm_release(struct inode *inode, struct file *filp) 494 1.1 riastrad { 495 1.1 riastrad struct drm_file *file_priv = filp->private_data; 496 1.1 riastrad struct drm_minor *minor = file_priv->minor; 497 1.1 riastrad struct drm_device *dev = minor->dev; 498 1.1 riastrad 499 1.1 riastrad mutex_lock(&drm_global_mutex); 500 1.1 riastrad 501 1.1 riastrad DRM_DEBUG("open_count = %d\n", dev->open_count); 502 1.1 riastrad 503 1.1 riastrad drm_close_helper(filp); 504 1.1 riastrad 505 1.1 riastrad if (!--dev->open_count) 506 1.1 riastrad drm_lastclose(dev); 507 1.1 riastrad 508 1.1 riastrad mutex_unlock(&drm_global_mutex); 509 1.1 riastrad 510 1.1 riastrad drm_minor_release(minor); 511 1.1 riastrad 512 1.1 riastrad return 0; 513 1.1 riastrad } 514 1.1 riastrad EXPORT_SYMBOL(drm_release); 515 1.3 riastrad #endif 516 1.1 riastrad 517 1.1 riastrad /** 518 1.1 riastrad * drm_read - read method for DRM file 519 1.1 riastrad * @filp: file pointer 520 1.1 riastrad * @buffer: userspace destination pointer for the read 521 1.1 riastrad * @count: count in bytes to read 522 1.1 riastrad * @offset: offset to read 523 1.1 riastrad * 524 1.1 riastrad * This function must be used by drivers as their &file_operations.read 525 1.1 riastrad * method iff they use DRM events for asynchronous signalling to userspace. 526 1.1 riastrad * Since events are used by the KMS API for vblank and page flip completion this 527 1.1 riastrad * means all modern display drivers must use it. 528 1.1 riastrad * 529 1.1 riastrad * @offset is ignored, DRM events are read like a pipe. Therefore drivers also 530 1.1 riastrad * must set the &file_operation.llseek to no_llseek(). Polling support is 531 1.1 riastrad * provided by drm_poll(). 532 1.1 riastrad * 533 1.1 riastrad * This function will only ever read a full event. Therefore userspace must 534 1.1 riastrad * supply a big enough buffer to fit any event to ensure forward progress. Since 535 1.1 riastrad * the maximum event space is currently 4K it's recommended to just use that for 536 1.1 riastrad * safety. 537 1.1 riastrad * 538 1.1 riastrad * RETURNS: 539 1.1 riastrad * 540 1.1 riastrad * Number of bytes read (always aligned to full events, and can be 0) or a 541 1.1 riastrad * negative error code on failure. 542 1.1 riastrad */ 543 1.3 riastrad #ifndef __NetBSD__ 544 1.1 riastrad ssize_t drm_read(struct file *filp, char __user *buffer, 545 1.1 riastrad size_t count, loff_t *offset) 546 1.1 riastrad { 547 1.1 riastrad struct drm_file *file_priv = filp->private_data; 548 1.1 riastrad struct drm_device *dev = file_priv->minor->dev; 549 1.1 riastrad ssize_t ret; 550 1.1 riastrad 551 1.1 riastrad if (!access_ok(buffer, count)) 552 1.1 riastrad return -EFAULT; 553 1.1 riastrad 554 1.1 riastrad ret = mutex_lock_interruptible(&file_priv->event_read_lock); 555 1.1 riastrad if (ret) 556 1.1 riastrad return ret; 557 1.1 riastrad 558 1.1 riastrad for (;;) { 559 1.1 riastrad struct drm_pending_event *e = NULL; 560 1.1 riastrad 561 1.1 riastrad spin_lock_irq(&dev->event_lock); 562 1.1 riastrad if (!list_empty(&file_priv->event_list)) { 563 1.1 riastrad e = list_first_entry(&file_priv->event_list, 564 1.1 riastrad struct drm_pending_event, link); 565 1.1 riastrad file_priv->event_space += e->event->length; 566 1.1 riastrad list_del(&e->link); 567 1.1 riastrad } 568 1.1 riastrad spin_unlock_irq(&dev->event_lock); 569 1.1 riastrad 570 1.1 riastrad if (e == NULL) { 571 1.1 riastrad if (ret) 572 1.1 riastrad break; 573 1.1 riastrad 574 1.1 riastrad if (filp->f_flags & O_NONBLOCK) { 575 1.1 riastrad ret = -EAGAIN; 576 1.1 riastrad break; 577 1.1 riastrad } 578 1.1 riastrad 579 1.1 riastrad mutex_unlock(&file_priv->event_read_lock); 580 1.1 riastrad ret = wait_event_interruptible(file_priv->event_wait, 581 1.1 riastrad !list_empty(&file_priv->event_list)); 582 1.1 riastrad if (ret >= 0) 583 1.1 riastrad ret = mutex_lock_interruptible(&file_priv->event_read_lock); 584 1.1 riastrad if (ret) 585 1.1 riastrad return ret; 586 1.1 riastrad } else { 587 1.1 riastrad unsigned length = e->event->length; 588 1.1 riastrad 589 1.1 riastrad if (length > count - ret) { 590 1.1 riastrad put_back_event: 591 1.1 riastrad spin_lock_irq(&dev->event_lock); 592 1.1 riastrad file_priv->event_space -= length; 593 1.1 riastrad list_add(&e->link, &file_priv->event_list); 594 1.1 riastrad spin_unlock_irq(&dev->event_lock); 595 1.1 riastrad wake_up_interruptible(&file_priv->event_wait); 596 1.1 riastrad break; 597 1.1 riastrad } 598 1.1 riastrad 599 1.1 riastrad if (copy_to_user(buffer + ret, e->event, length)) { 600 1.1 riastrad if (ret == 0) 601 1.1 riastrad ret = -EFAULT; 602 1.1 riastrad goto put_back_event; 603 1.1 riastrad } 604 1.1 riastrad 605 1.1 riastrad ret += length; 606 1.1 riastrad kfree(e); 607 1.1 riastrad } 608 1.1 riastrad } 609 1.1 riastrad mutex_unlock(&file_priv->event_read_lock); 610 1.1 riastrad 611 1.1 riastrad return ret; 612 1.1 riastrad } 613 1.1 riastrad EXPORT_SYMBOL(drm_read); 614 1.3 riastrad #endif 615 1.1 riastrad 616 1.1 riastrad /** 617 1.1 riastrad * drm_poll - poll method for DRM file 618 1.1 riastrad * @filp: file pointer 619 1.1 riastrad * @wait: poll waiter table 620 1.1 riastrad * 621 1.1 riastrad * This function must be used by drivers as their &file_operations.read method 622 1.1 riastrad * iff they use DRM events for asynchronous signalling to userspace. Since 623 1.1 riastrad * events are used by the KMS API for vblank and page flip completion this means 624 1.1 riastrad * all modern display drivers must use it. 625 1.1 riastrad * 626 1.1 riastrad * See also drm_read(). 627 1.1 riastrad * 628 1.1 riastrad * RETURNS: 629 1.1 riastrad * 630 1.1 riastrad * Mask of POLL flags indicating the current status of the file. 631 1.1 riastrad */ 632 1.3 riastrad #ifndef __NetBSD__ 633 1.1 riastrad __poll_t drm_poll(struct file *filp, struct poll_table_struct *wait) 634 1.1 riastrad { 635 1.1 riastrad struct drm_file *file_priv = filp->private_data; 636 1.1 riastrad __poll_t mask = 0; 637 1.1 riastrad 638 1.1 riastrad poll_wait(filp, &file_priv->event_wait, wait); 639 1.1 riastrad 640 1.1 riastrad if (!list_empty(&file_priv->event_list)) 641 1.1 riastrad mask |= EPOLLIN | EPOLLRDNORM; 642 1.1 riastrad 643 1.1 riastrad return mask; 644 1.1 riastrad } 645 1.1 riastrad EXPORT_SYMBOL(drm_poll); 646 1.3 riastrad #endif 647 1.1 riastrad 648 1.1 riastrad /** 649 1.1 riastrad * drm_event_reserve_init_locked - init a DRM event and reserve space for it 650 1.1 riastrad * @dev: DRM device 651 1.1 riastrad * @file_priv: DRM file private data 652 1.1 riastrad * @p: tracking structure for the pending event 653 1.1 riastrad * @e: actual event data to deliver to userspace 654 1.1 riastrad * 655 1.1 riastrad * This function prepares the passed in event for eventual delivery. If the event 656 1.1 riastrad * doesn't get delivered (because the IOCTL fails later on, before queuing up 657 1.1 riastrad * anything) then the even must be cancelled and freed using 658 1.1 riastrad * drm_event_cancel_free(). Successfully initialized events should be sent out 659 1.1 riastrad * using drm_send_event() or drm_send_event_locked() to signal completion of the 660 1.1 riastrad * asynchronous event to userspace. 661 1.1 riastrad * 662 1.1 riastrad * If callers embedded @p into a larger structure it must be allocated with 663 1.1 riastrad * kmalloc and @p must be the first member element. 664 1.1 riastrad * 665 1.1 riastrad * This is the locked version of drm_event_reserve_init() for callers which 666 1.1 riastrad * already hold &drm_device.event_lock. 667 1.1 riastrad * 668 1.1 riastrad * RETURNS: 669 1.1 riastrad * 670 1.1 riastrad * 0 on success or a negative error code on failure. 671 1.1 riastrad */ 672 1.1 riastrad int drm_event_reserve_init_locked(struct drm_device *dev, 673 1.1 riastrad struct drm_file *file_priv, 674 1.1 riastrad struct drm_pending_event *p, 675 1.1 riastrad struct drm_event *e) 676 1.1 riastrad { 677 1.1 riastrad if (file_priv->event_space < e->length) 678 1.1 riastrad return -ENOMEM; 679 1.1 riastrad 680 1.1 riastrad file_priv->event_space -= e->length; 681 1.1 riastrad 682 1.1 riastrad p->event = e; 683 1.1 riastrad list_add(&p->pending_link, &file_priv->pending_event_list); 684 1.1 riastrad p->file_priv = file_priv; 685 1.1 riastrad 686 1.1 riastrad return 0; 687 1.1 riastrad } 688 1.1 riastrad EXPORT_SYMBOL(drm_event_reserve_init_locked); 689 1.1 riastrad 690 1.1 riastrad /** 691 1.1 riastrad * drm_event_reserve_init - init a DRM event and reserve space for it 692 1.1 riastrad * @dev: DRM device 693 1.1 riastrad * @file_priv: DRM file private data 694 1.1 riastrad * @p: tracking structure for the pending event 695 1.1 riastrad * @e: actual event data to deliver to userspace 696 1.1 riastrad * 697 1.1 riastrad * This function prepares the passed in event for eventual delivery. If the event 698 1.1 riastrad * doesn't get delivered (because the IOCTL fails later on, before queuing up 699 1.1 riastrad * anything) then the even must be cancelled and freed using 700 1.1 riastrad * drm_event_cancel_free(). Successfully initialized events should be sent out 701 1.1 riastrad * using drm_send_event() or drm_send_event_locked() to signal completion of the 702 1.1 riastrad * asynchronous event to userspace. 703 1.1 riastrad * 704 1.1 riastrad * If callers embedded @p into a larger structure it must be allocated with 705 1.1 riastrad * kmalloc and @p must be the first member element. 706 1.1 riastrad * 707 1.1 riastrad * Callers which already hold &drm_device.event_lock should use 708 1.1 riastrad * drm_event_reserve_init_locked() instead. 709 1.1 riastrad * 710 1.1 riastrad * RETURNS: 711 1.1 riastrad * 712 1.1 riastrad * 0 on success or a negative error code on failure. 713 1.1 riastrad */ 714 1.1 riastrad int drm_event_reserve_init(struct drm_device *dev, 715 1.1 riastrad struct drm_file *file_priv, 716 1.1 riastrad struct drm_pending_event *p, 717 1.1 riastrad struct drm_event *e) 718 1.1 riastrad { 719 1.1 riastrad unsigned long flags; 720 1.1 riastrad int ret; 721 1.1 riastrad 722 1.1 riastrad spin_lock_irqsave(&dev->event_lock, flags); 723 1.1 riastrad ret = drm_event_reserve_init_locked(dev, file_priv, p, e); 724 1.1 riastrad spin_unlock_irqrestore(&dev->event_lock, flags); 725 1.1 riastrad 726 1.1 riastrad return ret; 727 1.1 riastrad } 728 1.1 riastrad EXPORT_SYMBOL(drm_event_reserve_init); 729 1.1 riastrad 730 1.1 riastrad /** 731 1.1 riastrad * drm_event_cancel_free - free a DRM event and release its space 732 1.1 riastrad * @dev: DRM device 733 1.1 riastrad * @p: tracking structure for the pending event 734 1.1 riastrad * 735 1.1 riastrad * This function frees the event @p initialized with drm_event_reserve_init() 736 1.1 riastrad * and releases any allocated space. It is used to cancel an event when the 737 1.1 riastrad * nonblocking operation could not be submitted and needed to be aborted. 738 1.1 riastrad */ 739 1.1 riastrad void drm_event_cancel_free(struct drm_device *dev, 740 1.1 riastrad struct drm_pending_event *p) 741 1.1 riastrad { 742 1.1 riastrad unsigned long flags; 743 1.1 riastrad spin_lock_irqsave(&dev->event_lock, flags); 744 1.1 riastrad if (p->file_priv) { 745 1.1 riastrad p->file_priv->event_space += p->event->length; 746 1.1 riastrad list_del(&p->pending_link); 747 1.1 riastrad } 748 1.1 riastrad spin_unlock_irqrestore(&dev->event_lock, flags); 749 1.1 riastrad 750 1.1 riastrad if (p->fence) 751 1.1 riastrad dma_fence_put(p->fence); 752 1.1 riastrad 753 1.1 riastrad kfree(p); 754 1.1 riastrad } 755 1.1 riastrad EXPORT_SYMBOL(drm_event_cancel_free); 756 1.1 riastrad 757 1.1 riastrad /** 758 1.1 riastrad * drm_send_event_locked - send DRM event to file descriptor 759 1.1 riastrad * @dev: DRM device 760 1.1 riastrad * @e: DRM event to deliver 761 1.1 riastrad * 762 1.1 riastrad * This function sends the event @e, initialized with drm_event_reserve_init(), 763 1.1 riastrad * to its associated userspace DRM file. Callers must already hold 764 1.1 riastrad * &drm_device.event_lock, see drm_send_event() for the unlocked version. 765 1.1 riastrad * 766 1.1 riastrad * Note that the core will take care of unlinking and disarming events when the 767 1.1 riastrad * corresponding DRM file is closed. Drivers need not worry about whether the 768 1.1 riastrad * DRM file for this event still exists and can call this function upon 769 1.1 riastrad * completion of the asynchronous work unconditionally. 770 1.1 riastrad */ 771 1.1 riastrad void drm_send_event_locked(struct drm_device *dev, struct drm_pending_event *e) 772 1.1 riastrad { 773 1.1 riastrad assert_spin_locked(&dev->event_lock); 774 1.1 riastrad 775 1.1 riastrad if (e->completion) { 776 1.1 riastrad complete_all(e->completion); 777 1.1 riastrad e->completion_release(e->completion); 778 1.1 riastrad e->completion = NULL; 779 1.1 riastrad } 780 1.1 riastrad 781 1.1 riastrad if (e->fence) { 782 1.1 riastrad dma_fence_signal(e->fence); 783 1.1 riastrad dma_fence_put(e->fence); 784 1.1 riastrad } 785 1.1 riastrad 786 1.1 riastrad if (!e->file_priv) { 787 1.1 riastrad kfree(e); 788 1.1 riastrad return; 789 1.1 riastrad } 790 1.1 riastrad 791 1.1 riastrad list_del(&e->pending_link); 792 1.1 riastrad list_add_tail(&e->link, 793 1.1 riastrad &e->file_priv->event_list); 794 1.3 riastrad #ifdef __NetBSD__ 795 1.3 riastrad DRM_SPIN_WAKEUP_ONE(&e->file_priv->event_wait, &dev->event_lock); 796 1.3 riastrad selnotify(&e->file_priv->event_selq, POLLIN|POLLRDNORM, NOTE_SUBMIT); 797 1.3 riastrad #else 798 1.1 riastrad wake_up_interruptible(&e->file_priv->event_wait); 799 1.3 riastrad #endif 800 1.1 riastrad } 801 1.1 riastrad EXPORT_SYMBOL(drm_send_event_locked); 802 1.1 riastrad 803 1.1 riastrad /** 804 1.1 riastrad * drm_send_event - send DRM event to file descriptor 805 1.1 riastrad * @dev: DRM device 806 1.1 riastrad * @e: DRM event to deliver 807 1.1 riastrad * 808 1.1 riastrad * This function sends the event @e, initialized with drm_event_reserve_init(), 809 1.1 riastrad * to its associated userspace DRM file. This function acquires 810 1.1 riastrad * &drm_device.event_lock, see drm_send_event_locked() for callers which already 811 1.1 riastrad * hold this lock. 812 1.1 riastrad * 813 1.1 riastrad * Note that the core will take care of unlinking and disarming events when the 814 1.1 riastrad * corresponding DRM file is closed. Drivers need not worry about whether the 815 1.1 riastrad * DRM file for this event still exists and can call this function upon 816 1.1 riastrad * completion of the asynchronous work unconditionally. 817 1.1 riastrad */ 818 1.1 riastrad void drm_send_event(struct drm_device *dev, struct drm_pending_event *e) 819 1.1 riastrad { 820 1.1 riastrad unsigned long irqflags; 821 1.1 riastrad 822 1.1 riastrad spin_lock_irqsave(&dev->event_lock, irqflags); 823 1.1 riastrad drm_send_event_locked(dev, e); 824 1.1 riastrad spin_unlock_irqrestore(&dev->event_lock, irqflags); 825 1.1 riastrad } 826 1.1 riastrad EXPORT_SYMBOL(drm_send_event); 827 1.1 riastrad 828 1.1 riastrad /** 829 1.1 riastrad * mock_drm_getfile - Create a new struct file for the drm device 830 1.1 riastrad * @minor: drm minor to wrap (e.g. #drm_device.primary) 831 1.1 riastrad * @flags: file creation mode (O_RDWR etc) 832 1.1 riastrad * 833 1.1 riastrad * This create a new struct file that wraps a DRM file context around a 834 1.1 riastrad * DRM minor. This mimicks userspace opening e.g. /dev/dri/card0, but without 835 1.1 riastrad * invoking userspace. The struct file may be operated on using its f_op 836 1.1 riastrad * (the drm_device.driver.fops) to mimick userspace operations, or be supplied 837 1.1 riastrad * to userspace facing functions as an internal/anonymous client. 838 1.1 riastrad * 839 1.1 riastrad * RETURNS: 840 1.1 riastrad * Pointer to newly created struct file, ERR_PTR on failure. 841 1.1 riastrad */ 842 1.3 riastrad #ifndef __NetBSD__ 843 1.1 riastrad struct file *mock_drm_getfile(struct drm_minor *minor, unsigned int flags) 844 1.1 riastrad { 845 1.1 riastrad struct drm_device *dev = minor->dev; 846 1.1 riastrad struct drm_file *priv; 847 1.1 riastrad struct file *file; 848 1.1 riastrad 849 1.1 riastrad priv = drm_file_alloc(minor); 850 1.1 riastrad if (IS_ERR(priv)) 851 1.1 riastrad return ERR_CAST(priv); 852 1.1 riastrad 853 1.1 riastrad file = anon_inode_getfile("drm", dev->driver->fops, priv, flags); 854 1.1 riastrad if (IS_ERR(file)) { 855 1.1 riastrad drm_file_free(priv); 856 1.1 riastrad return file; 857 1.1 riastrad } 858 1.1 riastrad 859 1.1 riastrad /* Everyone shares a single global address space */ 860 1.1 riastrad file->f_mapping = dev->anon_inode->i_mapping; 861 1.1 riastrad 862 1.1 riastrad drm_dev_get(dev); 863 1.1 riastrad priv->filp = file; 864 1.1 riastrad 865 1.1 riastrad return file; 866 1.1 riastrad } 867 1.1 riastrad EXPORT_SYMBOL_FOR_TESTS_ONLY(mock_drm_getfile); 868 1.3 riastrad #endif 869