1 /*        $NetBSD: drm_device.h,v 1.11 2022/10/15 15:19:28 riastradh Exp $      */
2 
3 #ifndef _DRM_DEVICE_H_
4 #define _DRM_DEVICE_H_
5 
6 #include <linux/list.h>
7 #include <linux/kref.h>
8 #include <linux/mutex.h>
9 #include <linux/idr.h>
10 
11 #include <drm/drm_hashtab.h>
12 #include <drm/drm_mode_config.h>
13 
14 #ifdef __NetBSD__
15 #include <drm/drm_wait_netbsd.h>
16 #include <dev/sysmon/sysmonvar.h>
17 #endif
18 
19 struct drm_driver;
20 struct drm_minor;
21 struct drm_master;
22 struct drm_device_dma;
23 struct drm_vblank_crtc;
24 struct drm_sg_mem;
25 struct drm_local_map;
26 struct drm_vma_offset_manager;
27 struct drm_vram_mm;
28 struct drm_fb_helper;
29 
30 struct pci_dev;
31 struct pci_controller;
32 
33 /**
34  * enum drm_switch_power - power state of drm device
35  */
36 
37 enum switch_power_state {
38           /** @DRM_SWITCH_POWER_ON: Power state is ON */
39           DRM_SWITCH_POWER_ON = 0,
40 
41           /** @DRM_SWITCH_POWER_OFF: Power state is OFF */
42           DRM_SWITCH_POWER_OFF = 1,
43 
44           /** @DRM_SWITCH_POWER_CHANGING: Power state is changing */
45           DRM_SWITCH_POWER_CHANGING = 2,
46 
47           /** @DRM_SWITCH_POWER_DYNAMIC_OFF: Suspended */
48           DRM_SWITCH_POWER_DYNAMIC_OFF = 3,
49 };
50 
51 /**
52  * struct drm_device - DRM device structure
53  *
54  * This structure represent a complete card that
55  * may contain multiple heads.
56  */
57 struct drm_device {
58           /**
59            * @legacy_dev_list:
60            *
61            * List of devices per driver for stealth attach cleanup
62            */
63           struct list_head legacy_dev_list;
64 
65           /** @if_version: Highest interface version set */
66           int if_version;
67 
68           /** @ref: Object ref-count */
69           struct kref ref;
70 
71           /** @dev: Device structure of bus-device */
72           struct device *dev;
73 
74           /** @driver: DRM driver managing the device */
75           struct drm_driver *driver;
76 
77           /**
78            * @dev_private:
79            *
80            * DRM driver private data. Instead of using this pointer it is
81            * recommended that drivers use drm_dev_init() and embed struct
82            * &drm_device in their larger per-device structure.
83            */
84           void *dev_private;
85 
86           /** @primary: Primary node */
87           struct drm_minor *primary;
88 
89           /** @render: Render node */
90           struct drm_minor *render;
91 
92           /**
93            * @registered:
94            *
95            * Internally used by drm_dev_register() and drm_connector_register().
96            */
97           bool registered;
98 
99           /**
100            * @master:
101            *
102            * Currently active master for this device.
103            * Protected by &master_mutex
104            */
105           struct drm_master *master;
106 
107           /**
108            * @driver_features: per-device driver features
109            *
110            * Drivers can clear specific flags here to disallow
111            * certain features on a per-device basis while still
112            * sharing a single &struct drm_driver instance across
113            * all devices.
114            */
115           u32 driver_features;
116 
117           /**
118            * @unplugged:
119            *
120            * Flag to tell if the device has been unplugged.
121            * See drm_dev_enter() and drm_dev_is_unplugged().
122            */
123           bool unplugged;
124 
125           /** @anon_inode: inode for private address-space */
126           void *anon_inode;
127 
128           /** @unique: Unique name of the device */
129           char *unique;
130 
131           /**
132            * @struct_mutex:
133            *
134            * Lock for others (not &drm_minor.master and &drm_file.is_master)
135            */
136           struct mutex struct_mutex;
137 
138           /**
139            * @master_mutex:
140            *
141            * Lock for &drm_minor.master and &drm_file.is_master
142            */
143           struct mutex master_mutex;
144 
145           /**
146            * @open_count:
147            *
148            * Usage counter for outstanding files open,
149            * protected by drm_global_mutex
150            */
151           int open_count;
152 
153           /** @filelist_mutex: Protects @filelist. */
154           struct mutex filelist_mutex;
155           /**
156            * @filelist:
157            *
158            * List of userspace clients, linked through &drm_file.lhead.
159            */
160           struct list_head filelist;
161 
162           /**
163            * @filelist_internal:
164            *
165            * List of open DRM files for in-kernel clients.
166            * Protected by &filelist_mutex.
167            */
168           struct list_head filelist_internal;
169 
170           /**
171            * @clientlist_mutex:
172            *
173            * Protects &clientlist access.
174            */
175           struct mutex clientlist_mutex;
176 
177           /**
178            * @clientlist:
179            *
180            * List of in-kernel clients. Protected by &clientlist_mutex.
181            */
182           struct list_head clientlist;
183 
184           /**
185            * @irq_enabled:
186            *
187            * Indicates that interrupt handling is enabled, specifically vblank
188            * handling. Drivers which don't use drm_irq_install() need to set this
189            * to true manually.
190            */
191           bool irq_enabled;
192 
193           /**
194            * @irq: Used by the drm_irq_install() and drm_irq_unistall() helpers.
195            */
196           int irq;
197 #ifdef __NetBSD__
198           struct drm_bus_irq_cookie *irq_cookie;
199 #endif
200 
201           /**
202            * @vblank_disable_immediate:
203            *
204            * If true, vblank interrupt will be disabled immediately when the
205            * refcount drops to zero, as opposed to via the vblank disable
206            * timer.
207            *
208            * This can be set to true it the hardware has a working vblank counter
209            * with high-precision timestamping (otherwise there are races) and the
210            * driver uses drm_crtc_vblank_on() and drm_crtc_vblank_off()
211            * appropriately. See also @max_vblank_count and
212            * &drm_crtc_funcs.get_vblank_counter.
213            */
214           bool vblank_disable_immediate;
215 
216           /**
217            * @vblank:
218            *
219            * Array of vblank tracking structures, one per &struct drm_crtc. For
220            * historical reasons (vblank support predates kernel modesetting) this
221            * is free-standing and not part of &struct drm_crtc itself. It must be
222            * initialized explicitly by calling drm_vblank_init().
223            */
224           struct drm_vblank_crtc *vblank;
225 
226           /**
227            * @vblank_time_lock:
228            *
229            *  Protects vblank count and time updates during vblank enable/disable
230            */
231           spinlock_t vblank_time_lock;
232 #ifndef __NetBSD__            /* merged into event_lock */
233           /**
234            * @vbl_lock: Top-level vblank references lock, wraps the low-level
235            * @vblank_time_lock.
236            */
237           spinlock_t vbl_lock;
238 #endif
239 
240           /**
241            * @max_vblank_count:
242            *
243            * Maximum value of the vblank registers. This value +1 will result in a
244            * wrap-around of the vblank register. It is used by the vblank core to
245            * handle wrap-arounds.
246            *
247            * If set to zero the vblank core will try to guess the elapsed vblanks
248            * between times when the vblank interrupt is disabled through
249            * high-precision timestamps. That approach is suffering from small
250            * races and imprecision over longer time periods, hence exposing a
251            * hardware vblank counter is always recommended.
252            *
253            * This is the statically configured device wide maximum. The driver
254            * can instead choose to use a runtime configurable per-crtc value
255            * &drm_vblank_crtc.max_vblank_count, in which case @max_vblank_count
256            * must be left at zero. See drm_crtc_set_max_vblank_count() on how
257            * to use the per-crtc value.
258            *
259            * If non-zero, &drm_crtc_funcs.get_vblank_counter must be set.
260            */
261           u32 max_vblank_count;
262 
263           /** @vblank_event_list: List of vblank events */
264           struct list_head vblank_event_list;
265 
266           /**
267            * @event_lock:
268            *
269            * Protects @vblank_event_list and event delivery in
270            * general. See drm_send_event() and drm_send_event_locked().
271            */
272           spinlock_t event_lock;
273 
274           /** @agp: AGP data */
275           struct drm_agp_head *agp;
276 
277           /** @pdev: PCI device structure */
278           struct pci_dev *pdev;
279 
280 #ifdef __alpha__
281           /** @hose: PCI hose, only used on ALPHA platforms. */
282           struct pci_controller *hose;
283 #endif
284 
285 #ifdef __NetBSD__
286           bus_space_tag_t bst;
287           struct drm_bus_map *bus_maps;
288           unsigned bus_nmaps;
289           bus_dma_tag_t bus_dmat;       /* bus's full DMA tag, for internal use */
290           bus_dma_tag_t bus_dmat32;     /* bus's 32-bit DMA tag */
291           bus_dma_tag_t dmat; /* DMA tag for driver, may be subregion */
292           bool dmat_subregion_p;
293           bus_addr_t dmat_subregion_min;
294           bus_addr_t dmat_subregion_max;
295           struct vmem *cma_pool;
296 #endif
297 
298           /** @num_crtcs: Number of CRTCs on this device */
299           unsigned int num_crtcs;
300 
301           /** @mode_config: Current mode config */
302           struct drm_mode_config mode_config;
303 
304           /** @object_name_lock: GEM information */
305           struct mutex object_name_lock;
306 
307           /** @object_name_idr: GEM information */
308           struct idr object_name_idr;
309 
310           /** @vma_offset_manager: GEM information */
311           struct drm_vma_offset_manager *vma_offset_manager;
312 
313           /** @vram_mm: VRAM MM memory manager */
314           struct drm_vram_mm *vram_mm;
315 
316           /**
317            * @switch_power_state:
318            *
319            * Power state of the client.
320            * Used by drivers supporting the switcheroo driver.
321            * The state is maintained in the
322            * &vga_switcheroo_client_ops.set_gpu_state callback
323            */
324           enum switch_power_state switch_power_state;
325 
326           /**
327            * @fb_helper:
328            *
329            * Pointer to the fbdev emulation structure.
330            * Set by drm_fb_helper_init() and cleared by drm_fb_helper_fini().
331            */
332           struct drm_fb_helper *fb_helper;
333 
334 #ifdef __NetBSD__
335           struct sysmon_pswitch sc_monitor_hotplug;
336           struct mutex suspend_lock;
337           drm_waitqueue_t suspend_cv;
338           uint64_t active_ioctls;
339           struct lwp *suspender;
340 #endif
341 
342           /* Everything below here is for legacy driver, never use! */
343           /* private: */
344 #if IS_ENABLED(CONFIG_DRM_LEGACY) || \
345     defined(__NetBSD__) /* XXX drm_vm.c / drm_cdevsw.c use this */
346           /* Context handle management - linked list of context handles */
347           struct list_head ctxlist;
348 
349           /* Context handle management - mutex for &ctxlist */
350           struct mutex ctxlist_mutex;
351 
352           /* Context handle management */
353           struct idr ctx_idr;
354 
355           /* Memory management - linked list of regions */
356           struct list_head maplist;
357 
358           /* Memory management - user token hash table for maps */
359           struct drm_open_hash map_hash;
360 
361           /* Context handle management - list of vmas (for debugging) */
362           struct list_head vmalist;
363 
364           /* Optional pointer for DMA support */
365           struct drm_device_dma *dma;
366 
367           /* Context swapping flag */
368           __volatile__ long context_flag;
369 
370           /* Last current context */
371           int last_context;
372 
373           /* Lock for &buf_use and a few other things. */
374           spinlock_t buf_lock;
375 
376           /* Usage counter for buffers in use -- cannot alloc */
377           int buf_use;
378 
379           /* Buffer allocation in progress */
380           atomic_t buf_alloc;
381 
382           struct {
383                     int context;
384                     struct drm_hw_lock *lock;
385           } sigdata;
386 
387           struct drm_local_map *agp_buffer_map;
388           unsigned int agp_buffer_token;
389 
390           /* Scatter gather memory */
391           struct drm_sg_mem *sg;
392 #endif
393 };
394 
395 #ifdef __NetBSD__
396 extern const struct cdevsw drm_cdevsw;
397 int drm_limit_dma_space(struct drm_device *, resource_size_t, resource_size_t);
398 int drm_guarantee_initialized(void);
399 #endif
400 
401 #endif
402