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