1 #ifndef _DRM_DEVICE_H_
2 #define _DRM_DEVICE_H_
3 
4 #include <sys/types.h>
5 #include <sys/event.h>
6 
7 #include <linux/list.h>
8 #include <linux/kref.h>
9 #include <linux/mutex.h>
10 #include <linux/idr.h>
11 #include <linux/pci.h>
12 
13 #include <drm/drm_mode_config.h>
14 
15 #include <sys/pool.h>
16 
17 struct drm_driver;
18 struct drm_minor;
19 struct drm_master;
20 struct drm_vblank_crtc;
21 struct drm_vma_offset_manager;
22 struct drm_vram_mm;
23 struct drm_fb_helper;
24 
25 struct inode;
26 
27 struct pci_dev;
28 struct pci_controller;
29 
30 
31 /**
32  * enum switch_power_state - power state of drm device
33  */
34 
35 enum switch_power_state {
36 	/** @DRM_SWITCH_POWER_ON: Power state is ON */
37 	DRM_SWITCH_POWER_ON = 0,
38 
39 	/** @DRM_SWITCH_POWER_OFF: Power state is OFF */
40 	DRM_SWITCH_POWER_OFF = 1,
41 
42 	/** @DRM_SWITCH_POWER_CHANGING: Power state is changing */
43 	DRM_SWITCH_POWER_CHANGING = 2,
44 
45 	/** @DRM_SWITCH_POWER_DYNAMIC_OFF: Suspended */
46 	DRM_SWITCH_POWER_DYNAMIC_OFF = 3,
47 };
48 
49 #ifdef __OpenBSD__
50 #include <dev/pci/pcivar.h>
51 #include <dev/pci/agpvar.h>
52 
53 struct drm_agp_head {
54 	struct agp_softc			*agpdev;
55 	const char				*chipset;
56 	TAILQ_HEAD(agp_memlist, drm_agp_mem)	 memory;
57 	struct agp_info				 info;
58 	unsigned long				 base;
59 	unsigned long				 mode;
60 	unsigned long				 page_mask;
61 	int					 acquired;
62 	int					 cant_use_aperture;
63 	int					 enabled;
64    	int					 mtrr;
65 };
66 #if IS_ENABLED(CONFIG_AGP)
67 struct drm_agp_head *drm_legacy_agp_init(struct drm_device *dev);
68 #endif
69 #endif /* __OpenBSD__ */
70 
71 /**
72  * struct drm_device - DRM device structure
73  *
74  * This structure represent a complete card that
75  * may contain multiple heads.
76  */
77 struct drm_device {
78 	struct device *dev;
79 
80 	/** @if_version: Highest interface version set */
81 	int if_version;
82 
83 	/** @ref: Object ref-count */
84 	struct kref ref;
85 
86 #ifdef __linux__
87 	/** @dev: Device structure of bus-device */
88 	struct device *dev;
89 #endif
90 
91 	/**
92 	 * @managed:
93 	 *
94 	 * Managed resources linked to the lifetime of this &drm_device as
95 	 * tracked by @ref.
96 	 */
97 	struct {
98 		/** @managed.resources: managed resources list */
99 		struct list_head resources;
100 		/** @managed.final_kfree: pointer for final kfree() call */
101 		void *final_kfree;
102 		/** @managed.lock: protects @managed.resources */
103 		spinlock_t lock;
104 	} managed;
105 
106 	/** @driver: DRM driver managing the device */
107 	const struct drm_driver *driver;
108 
109 	bus_dma_tag_t		dmat;
110 	bus_space_tag_t		bst;
111 
112 	struct klist note;
113 	struct pci_dev  _pdev;
114 	struct pci_dev *pdev;
115 
116 	struct mutex	quiesce_mtx;
117 	int		quiesce;
118 	int		quiesce_count;
119 
120 	/**
121 	 * @dev_private:
122 	 *
123 	 * DRM driver private data. This is deprecated and should be left set to
124 	 * NULL.
125 	 *
126 	 * Instead of using this pointer it is recommended that drivers use
127 	 * devm_drm_dev_alloc() and embed struct &drm_device in their larger
128 	 * per-device structure.
129 	 */
130 	void *dev_private;
131 
132 	/**
133 	 * @primary:
134 	 *
135 	 * Primary node. Drivers should not interact with this
136 	 * directly. debugfs interfaces can be registered with
137 	 * drm_debugfs_add_file(), and sysfs should be directly added on the
138 	 * hardware (and not character device node) struct device @dev.
139 	 */
140 	struct drm_minor *primary;
141 
142 	/**
143 	 * @render:
144 	 *
145 	 * Render node. Drivers should not interact with this directly ever.
146 	 * Drivers should not expose any additional interfaces in debugfs or
147 	 * sysfs on this node.
148 	 */
149 	struct drm_minor *render;
150 
151 	/** @accel: Compute Acceleration node */
152 	struct drm_minor *accel;
153 
154 	/**
155 	 * @registered:
156 	 *
157 	 * Internally used by drm_dev_register() and drm_connector_register().
158 	 */
159 	bool registered;
160 
161 	/**
162 	 * @master:
163 	 *
164 	 * Currently active master for this device.
165 	 * Protected by &master_mutex
166 	 */
167 	struct drm_master *master;
168 
169 	/**
170 	 * @driver_features: per-device driver features
171 	 *
172 	 * Drivers can clear specific flags here to disallow
173 	 * certain features on a per-device basis while still
174 	 * sharing a single &struct drm_driver instance across
175 	 * all devices.
176 	 */
177 	u32 driver_features;
178 
179 	/**
180 	 * @unplugged:
181 	 *
182 	 * Flag to tell if the device has been unplugged.
183 	 * See drm_dev_enter() and drm_dev_is_unplugged().
184 	 */
185 	bool unplugged;
186 
187 	/** @anon_inode: inode for private address-space */
188 	struct inode *anon_inode;
189 
190 	/** @unique: Unique name of the device */
191 	char *unique;
192 
193 	/**
194 	 * @struct_mutex:
195 	 *
196 	 * Lock for others (not &drm_minor.master and &drm_file.is_master)
197 	 *
198 	 * TODO: This lock used to be the BKL of the DRM subsystem. Move the
199 	 *       lock into i915, which is the only remaining user.
200 	 */
201 	struct rwlock struct_mutex;
202 
203 	/**
204 	 * @master_mutex:
205 	 *
206 	 * Lock for &drm_minor.master and &drm_file.is_master
207 	 */
208 	struct rwlock master_mutex;
209 
210 	/**
211 	 * @open_count:
212 	 *
213 	 * Usage counter for outstanding files open,
214 	 * protected by drm_global_mutex
215 	 */
216 	atomic_t open_count;
217 
218 	/** @filelist_mutex: Protects @filelist. */
219 	struct rwlock filelist_mutex;
220 	/**
221 	 * @filelist:
222 	 *
223 	 * List of userspace clients, linked through &drm_file.lhead.
224 	 */
225 #ifdef __linux__
226 	struct list_head filelist;
227 #else
228 	SPLAY_HEAD(drm_file_tree, drm_file)	files;
229 #endif
230 
231 	/**
232 	 * @filelist_internal:
233 	 *
234 	 * List of open DRM files for in-kernel clients.
235 	 * Protected by &filelist_mutex.
236 	 */
237 	struct list_head filelist_internal;
238 
239 	/**
240 	 * @clientlist_mutex:
241 	 *
242 	 * Protects &clientlist access.
243 	 */
244 	struct rwlock clientlist_mutex;
245 
246 	/**
247 	 * @clientlist:
248 	 *
249 	 * List of in-kernel clients. Protected by &clientlist_mutex.
250 	 */
251 	struct list_head clientlist;
252 
253 	/**
254 	 * @vblank_disable_immediate:
255 	 *
256 	 * If true, vblank interrupt will be disabled immediately when the
257 	 * refcount drops to zero, as opposed to via the vblank disable
258 	 * timer.
259 	 *
260 	 * This can be set to true it the hardware has a working vblank counter
261 	 * with high-precision timestamping (otherwise there are races) and the
262 	 * driver uses drm_crtc_vblank_on() and drm_crtc_vblank_off()
263 	 * appropriately. Also, see @max_vblank_count,
264 	 * &drm_crtc_funcs.get_vblank_counter and
265 	 * &drm_vblank_crtc_config.disable_immediate.
266 	 */
267 	bool vblank_disable_immediate;
268 
269 	/**
270 	 * @vblank:
271 	 *
272 	 * Array of vblank tracking structures, one per &struct drm_crtc. For
273 	 * historical reasons (vblank support predates kernel modesetting) this
274 	 * is free-standing and not part of &struct drm_crtc itself. It must be
275 	 * initialized explicitly by calling drm_vblank_init().
276 	 */
277 	struct drm_vblank_crtc *vblank;
278 
279 	/**
280 	 * @vblank_time_lock:
281 	 *
282 	 *  Protects vblank count and time updates during vblank enable/disable
283 	 */
284 	spinlock_t vblank_time_lock;
285 	/**
286 	 * @vbl_lock: Top-level vblank references lock, wraps the low-level
287 	 * @vblank_time_lock.
288 	 */
289 	spinlock_t vbl_lock;
290 
291 	/**
292 	 * @max_vblank_count:
293 	 *
294 	 * Maximum value of the vblank registers. This value +1 will result in a
295 	 * wrap-around of the vblank register. It is used by the vblank core to
296 	 * handle wrap-arounds.
297 	 *
298 	 * If set to zero the vblank core will try to guess the elapsed vblanks
299 	 * between times when the vblank interrupt is disabled through
300 	 * high-precision timestamps. That approach is suffering from small
301 	 * races and imprecision over longer time periods, hence exposing a
302 	 * hardware vblank counter is always recommended.
303 	 *
304 	 * This is the statically configured device wide maximum. The driver
305 	 * can instead choose to use a runtime configurable per-crtc value
306 	 * &drm_vblank_crtc.max_vblank_count, in which case @max_vblank_count
307 	 * must be left at zero. See drm_crtc_set_max_vblank_count() on how
308 	 * to use the per-crtc value.
309 	 *
310 	 * If non-zero, &drm_crtc_funcs.get_vblank_counter must be set.
311 	 */
312 	u32 max_vblank_count;
313 
314 	/** @vblank_event_list: List of vblank events */
315 	struct list_head vblank_event_list;
316 
317 	/**
318 	 * @event_lock:
319 	 *
320 	 * Protects @vblank_event_list and event delivery in
321 	 * general. See drm_send_event() and drm_send_event_locked().
322 	 */
323 	spinlock_t event_lock;
324 
325 	/** @num_crtcs: Number of CRTCs on this device */
326 	unsigned int num_crtcs;
327 
328 	/** @mode_config: Current mode config */
329 	struct drm_mode_config mode_config;
330 
331 	struct pool objpl;
332 
333 	/** @object_name_lock: GEM information */
334 	struct rwlock object_name_lock;
335 
336 	/** @object_name_idr: GEM information */
337 	struct idr object_name_idr;
338 
339 	/** @vma_offset_manager: GEM information */
340 	struct drm_vma_offset_manager *vma_offset_manager;
341 
342 	/** @vram_mm: VRAM MM memory manager */
343 	struct drm_vram_mm *vram_mm;
344 
345 	/**
346 	 * @switch_power_state:
347 	 *
348 	 * Power state of the client.
349 	 * Used by drivers supporting the switcheroo driver.
350 	 * The state is maintained in the
351 	 * &vga_switcheroo_client_ops.set_gpu_state callback
352 	 */
353 	enum switch_power_state switch_power_state;
354 
355 	/**
356 	 * @fb_helper:
357 	 *
358 	 * Pointer to the fbdev emulation structure.
359 	 * Set by drm_fb_helper_init() and cleared by drm_fb_helper_fini().
360 	 */
361 	struct drm_fb_helper *fb_helper;
362 
363 	/**
364 	 * @debugfs_root:
365 	 *
366 	 * Root directory for debugfs files.
367 	 */
368 	struct dentry *debugfs_root;
369 
370 	struct drm_agp_head *agp;
371 };
372 
373 #endif
374