/*1* Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.2* Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.3* Copyright (c) 2009-2010, Code Aurora Forum.4* All rights reserved.5*6* Author: Rickard E. (Rik) Faith <[email protected]>7* Author: Gareth Hughes <[email protected]>8*9* Permission is hereby granted, free of charge, to any person obtaining a10* copy of this software and associated documentation files (the "Software"),11* to deal in the Software without restriction, including without limitation12* the rights to use, copy, modify, merge, publish, distribute, sublicense,13* and/or sell copies of the Software, and to permit persons to whom the14* Software is furnished to do so, subject to the following conditions:15*16* The above copyright notice and this permission notice (including the next17* paragraph) shall be included in all copies or substantial portions of the18* Software.19*20* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR21* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,22* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL23* VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR24* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,25* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR26* OTHER DEALINGS IN THE SOFTWARE.27*/2829#ifndef _DRM_FILE_H_30#define _DRM_FILE_H_3132#include <linux/types.h>33#include <linux/completion.h>34#include <linux/idr.h>3536#include <uapi/drm/drm.h>3738#include <drm/drm_prime.h>3940struct dma_fence;41struct drm_file;42struct drm_device;43struct drm_printer;44struct device;45struct file;4647extern struct xarray drm_minors_xa;4849/*50* FIXME: Not sure we want to have drm_minor here in the end, but to avoid51* header include loops we need it here for now.52*/5354/* Note that the values of this enum are ABI (it determines55* /dev/dri/renderD* numbers).56*57* Setting DRM_MINOR_ACCEL to 32 gives enough space for more drm minors to58* be implemented before we hit any future59*/60enum drm_minor_type {61DRM_MINOR_PRIMARY = 0,62DRM_MINOR_CONTROL = 1,63DRM_MINOR_RENDER = 2,64DRM_MINOR_ACCEL = 32,65};6667/**68* struct drm_minor - DRM device minor structure69*70* This structure represents a DRM minor number for device nodes in /dev.71* Entirely opaque to drivers and should never be inspected directly by drivers.72* Drivers instead should only interact with &struct drm_file and of course73* &struct drm_device, which is also where driver-private data and resources can74* be attached to.75*/76struct drm_minor {77/* private: */78int index; /* Minor device number */79int type; /* Control or render or accel */80struct device *kdev; /* Linux device */81struct drm_device *dev;8283struct dentry *debugfs_symlink;84struct dentry *debugfs_root;85};8687/**88* struct drm_pending_event - Event queued up for userspace to read89*90* This represents a DRM event. Drivers can use this as a generic completion91* mechanism, which supports kernel-internal &struct completion, &struct dma_fence92* and also the DRM-specific &struct drm_event delivery mechanism.93*/94struct drm_pending_event {95/**96* @completion:97*98* Optional pointer to a kernel internal completion signalled when99* drm_send_event() is called, useful to internally synchronize with100* nonblocking operations.101*/102struct completion *completion;103104/**105* @completion_release:106*107* Optional callback currently only used by the atomic modeset helpers108* to clean up the reference count for the structure @completion is109* stored in.110*/111void (*completion_release)(struct completion *completion);112113/**114* @event:115*116* Pointer to the actual event that should be sent to userspace to be117* read using drm_read(). Can be optional, since nowadays events are118* also used to signal kernel internal threads with @completion or DMA119* transactions using @fence.120*/121struct drm_event *event;122123/**124* @fence:125*126* Optional DMA fence to unblock other hardware transactions which127* depend upon the nonblocking DRM operation this event represents.128*/129struct dma_fence *fence;130131/**132* @file_priv:133*134* &struct drm_file where @event should be delivered to. Only set when135* @event is set.136*/137struct drm_file *file_priv;138139/**140* @link:141*142* Double-linked list to keep track of this event. Can be used by the143* driver up to the point when it calls drm_send_event(), after that144* this list entry is owned by the core for its own book-keeping.145*/146struct list_head link;147148/**149* @pending_link:150*151* Entry on &drm_file.pending_event_list, to keep track of all pending152* events for @file_priv, to allow correct unwinding of them when153* userspace closes the file before the event is delivered.154*/155struct list_head pending_link;156};157158/**159* struct drm_file - DRM file private data160*161* This structure tracks DRM state per open file descriptor.162*/163struct drm_file {164/**165* @authenticated:166*167* Whether the client is allowed to submit rendering, which for legacy168* nodes means it must be authenticated.169*170* See also the :ref:`section on primary nodes and authentication171* <drm_primary_node>`.172*/173bool authenticated;174175/**176* @stereo_allowed:177*178* True when the client has asked us to expose stereo 3D mode flags.179*/180bool stereo_allowed;181182/**183* @universal_planes:184*185* True if client understands CRTC primary planes and cursor planes186* in the plane list. Automatically set when @atomic is set.187*/188bool universal_planes;189190/** @atomic: True if client understands atomic properties. */191bool atomic;192193/**194* @aspect_ratio_allowed:195*196* True, if client can handle picture aspect ratios, and has requested197* to pass this information along with the mode.198*/199bool aspect_ratio_allowed;200201/**202* @writeback_connectors:203*204* True if client understands writeback connectors205*/206bool writeback_connectors;207208/**209* @was_master:210*211* This client has or had, master capability. Protected by struct212* &drm_device.master_mutex.213*214* This is used to ensure that CAP_SYS_ADMIN is not enforced, if the215* client is or was master in the past.216*/217bool was_master;218219/**220* @is_master:221*222* This client is the creator of @master. Protected by struct223* &drm_device.master_mutex.224*225* See also the :ref:`section on primary nodes and authentication226* <drm_primary_node>`.227*/228bool is_master;229230/**231* @supports_virtualized_cursor_plane:232*233* This client is capable of handling the cursor plane with the234* restrictions imposed on it by the virtualized drivers.235*236* This implies that the cursor plane has to behave like a cursor237* i.e. track cursor movement. It also requires setting of the238* hotspot properties by the client on the cursor plane.239*/240bool supports_virtualized_cursor_plane;241242/**243* @master:244*245* Master this node is currently associated with. Protected by struct246* &drm_device.master_mutex, and serialized by @master_lookup_lock.247*248* Only relevant if drm_is_primary_client() returns true. Note that249* this only matches &drm_device.master if the master is the currently250* active one.251*252* To update @master, both &drm_device.master_mutex and253* @master_lookup_lock need to be held, therefore holding either of254* them is safe and enough for the read side.255*256* When dereferencing this pointer, either hold struct257* &drm_device.master_mutex for the duration of the pointer's use, or258* use drm_file_get_master() if struct &drm_device.master_mutex is not259* currently held and there is no other need to hold it. This prevents260* @master from being freed during use.261*262* See also @authentication and @is_master and the :ref:`section on263* primary nodes and authentication <drm_primary_node>`.264*/265struct drm_master *master;266267/** @master_lookup_lock: Serializes @master. */268spinlock_t master_lookup_lock;269270/**271* @pid: Process that is using this file.272*273* Must only be dereferenced under a rcu_read_lock or equivalent.274*275* Updates are guarded with dev->filelist_mutex and reference must be276* dropped after a RCU grace period to accommodate lockless readers.277*/278struct pid __rcu *pid;279280/** @client_id: A unique id for fdinfo */281u64 client_id;282283/** @magic: Authentication magic, see @authenticated. */284drm_magic_t magic;285286/**287* @lhead:288*289* List of all open files of a DRM device, linked into290* &drm_device.filelist. Protected by &drm_device.filelist_mutex.291*/292struct list_head lhead;293294/** @minor: &struct drm_minor for this file. */295struct drm_minor *minor;296297/**298* @object_idr:299*300* Mapping of mm object handles to object pointers. Used by the GEM301* subsystem. Protected by @table_lock.302*303* Note that allocated entries might be NULL as a transient state when304* creating or deleting a handle.305*/306struct idr object_idr;307308/** @table_lock: Protects @object_idr. */309spinlock_t table_lock;310311/** @syncobj_idr: Mapping of sync object handles to object pointers. */312struct idr syncobj_idr;313/** @syncobj_table_lock: Protects @syncobj_idr. */314spinlock_t syncobj_table_lock;315316/** @filp: Pointer to the core file structure. */317struct file *filp;318319/**320* @driver_priv:321*322* Optional pointer for driver private data. Can be allocated in323* &drm_driver.open and should be freed in &drm_driver.postclose.324*/325void *driver_priv;326327/**328* @fbs:329*330* List of &struct drm_framebuffer associated with this file, using the331* &drm_framebuffer.filp_head entry.332*333* Protected by @fbs_lock. Note that the @fbs list holds a reference on334* the framebuffer object to prevent it from untimely disappearing.335*/336struct list_head fbs;337338/** @fbs_lock: Protects @fbs. */339struct mutex fbs_lock;340341/**342* @blobs:343*344* User-created blob properties; this retains a reference on the345* property.346*347* Protected by @drm_mode_config.blob_lock;348*/349struct list_head blobs;350351/** @event_wait: Waitqueue for new events added to @event_list. */352wait_queue_head_t event_wait;353354/**355* @pending_event_list:356*357* List of pending &struct drm_pending_event, used to clean up pending358* events in case this file gets closed before the event is signalled.359* Uses the &drm_pending_event.pending_link entry.360*361* Protect by &drm_device.event_lock.362*/363struct list_head pending_event_list;364365/**366* @event_list:367*368* List of &struct drm_pending_event, ready for delivery to userspace369* through drm_read(). Uses the &drm_pending_event.link entry.370*371* Protect by &drm_device.event_lock.372*/373struct list_head event_list;374375/**376* @event_space:377*378* Available event space to prevent userspace from379* exhausting kernel memory. Currently limited to the fairly arbitrary380* value of 4KB.381*/382int event_space;383384/** @event_read_lock: Serializes drm_read(). */385struct mutex event_read_lock;386387/**388* @prime:389*390* Per-file buffer caches used by the PRIME buffer sharing code.391*/392struct drm_prime_file_private prime;393394/**395* @client_name:396*397* Userspace-provided name; useful for accounting and debugging.398*/399const char *client_name;400401/**402* @client_name_lock: Protects @client_name.403*/404struct mutex client_name_lock;405406/**407* @debugfs_client:408*409* debugfs directory for each client under a drm node.410*/411struct dentry *debugfs_client;412};413414/**415* drm_is_primary_client - is this an open file of the primary node416* @file_priv: DRM file417*418* Returns true if this is an open file of the primary node, i.e.419* &drm_file.minor of @file_priv is a primary minor.420*421* See also the :ref:`section on primary nodes and authentication422* <drm_primary_node>`.423*/424static inline bool drm_is_primary_client(const struct drm_file *file_priv)425{426return file_priv->minor->type == DRM_MINOR_PRIMARY;427}428429/**430* drm_is_render_client - is this an open file of the render node431* @file_priv: DRM file432*433* Returns true if this is an open file of the render node, i.e.434* &drm_file.minor of @file_priv is a render minor.435*436* See also the :ref:`section on render nodes <drm_render_node>`.437*/438static inline bool drm_is_render_client(const struct drm_file *file_priv)439{440return file_priv->minor->type == DRM_MINOR_RENDER;441}442443/**444* drm_is_accel_client - is this an open file of the compute acceleration node445* @file_priv: DRM file446*447* Returns true if this is an open file of the compute acceleration node, i.e.448* &drm_file.minor of @file_priv is a accel minor.449*450* See also :doc:`Introduction to compute accelerators subsystem451* </accel/introduction>`.452*/453static inline bool drm_is_accel_client(const struct drm_file *file_priv)454{455return file_priv->minor->type == DRM_MINOR_ACCEL;456}457458__printf(2, 3)459void drm_file_err(struct drm_file *file_priv, const char *fmt, ...);460461void drm_file_update_pid(struct drm_file *);462463struct drm_minor *drm_minor_acquire(struct xarray *minors_xa, unsigned int minor_id);464void drm_minor_release(struct drm_minor *minor);465466int drm_open(struct inode *inode, struct file *filp);467int drm_open_helper(struct file *filp, struct drm_minor *minor);468ssize_t drm_read(struct file *filp, char __user *buffer,469size_t count, loff_t *offset);470int drm_release(struct inode *inode, struct file *filp);471int drm_release_noglobal(struct inode *inode, struct file *filp);472__poll_t drm_poll(struct file *filp, struct poll_table_struct *wait);473int drm_event_reserve_init_locked(struct drm_device *dev,474struct drm_file *file_priv,475struct drm_pending_event *p,476struct drm_event *e);477int drm_event_reserve_init(struct drm_device *dev,478struct drm_file *file_priv,479struct drm_pending_event *p,480struct drm_event *e);481void drm_event_cancel_free(struct drm_device *dev,482struct drm_pending_event *p);483void drm_send_event_locked(struct drm_device *dev, struct drm_pending_event *e);484void drm_send_event(struct drm_device *dev, struct drm_pending_event *e);485void drm_send_event_timestamp_locked(struct drm_device *dev,486struct drm_pending_event *e,487ktime_t timestamp);488489/**490* struct drm_memory_stats - GEM object stats associated491* @shared: Total size of GEM objects shared between processes492* @private: Total size of GEM objects493* @resident: Total size of GEM objects backing pages494* @purgeable: Total size of GEM objects that can be purged (resident and not active)495* @active: Total size of GEM objects active on one or more engines496*497* Used by drm_print_memory_stats()498*/499struct drm_memory_stats {500u64 shared;501u64 private;502u64 resident;503u64 purgeable;504u64 active;505};506507enum drm_gem_object_status;508509int drm_memory_stats_is_zero(const struct drm_memory_stats *stats);510void drm_fdinfo_print_size(struct drm_printer *p,511const char *prefix,512const char *stat,513const char *region,514u64 sz);515void drm_print_memory_stats(struct drm_printer *p,516const struct drm_memory_stats *stats,517enum drm_gem_object_status supported_status,518const char *region);519520void drm_show_memory_stats(struct drm_printer *p, struct drm_file *file);521void drm_show_fdinfo(struct seq_file *m, struct file *f);522523struct file *mock_drm_getfile(struct drm_minor *minor, unsigned int flags);524525#endif /* _DRM_FILE_H_ */526527528