1/*
2 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
3 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
4 * Copyright (c) 2009-2010, Code Aurora Forum.
5 * Copyright 2016 Intel Corp.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the next
15 * paragraph) shall be included in all copies or substantial portions of the
16 * Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
21 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
22 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 * OTHER DEALINGS IN THE SOFTWARE.
25 */
26
27#ifndef _DRM_DRV_H_
28#define _DRM_DRV_H_
29
30#include <linux/list.h>
31#include <linux/irqreturn.h>
32
33#include <video/nomodeset.h>
34
35#include <drm/drm_device.h>
36
37struct drm_file;
38struct drm_gem_object;
39struct drm_master;
40struct drm_minor;
41struct dma_buf;
42struct dma_buf_attachment;
43struct drm_display_mode;
44struct drm_mode_create_dumb;
45struct drm_printer;
46struct sg_table;
47
48/**
49 * enum drm_driver_feature - feature flags
50 *
51 * See &drm_driver.driver_features, drm_device.driver_features and
52 * drm_core_check_feature().
53 */
54enum drm_driver_feature {
55	/**
56	 * @DRIVER_GEM:
57	 *
58	 * Driver use the GEM memory manager. This should be set for all modern
59	 * drivers.
60	 */
61	DRIVER_GEM			= BIT(0),
62	/**
63	 * @DRIVER_MODESET:
64	 *
65	 * Driver supports mode setting interfaces (KMS).
66	 */
67	DRIVER_MODESET			= BIT(1),
68	/**
69	 * @DRIVER_RENDER:
70	 *
71	 * Driver supports dedicated render nodes. See also the :ref:`section on
72	 * render nodes <drm_render_node>` for details.
73	 */
74	DRIVER_RENDER			= BIT(3),
75	/**
76	 * @DRIVER_ATOMIC:
77	 *
78	 * Driver supports the full atomic modesetting userspace API. Drivers
79	 * which only use atomic internally, but do not support the full
80	 * userspace API (e.g. not all properties converted to atomic, or
81	 * multi-plane updates are not guaranteed to be tear-free) should not
82	 * set this flag.
83	 */
84	DRIVER_ATOMIC			= BIT(4),
85	/**
86	 * @DRIVER_SYNCOBJ:
87	 *
88	 * Driver supports &drm_syncobj for explicit synchronization of command
89	 * submission.
90	 */
91	DRIVER_SYNCOBJ                  = BIT(5),
92	/**
93	 * @DRIVER_SYNCOBJ_TIMELINE:
94	 *
95	 * Driver supports the timeline flavor of &drm_syncobj for explicit
96	 * synchronization of command submission.
97	 */
98	DRIVER_SYNCOBJ_TIMELINE         = BIT(6),
99	/**
100	 * @DRIVER_COMPUTE_ACCEL:
101	 *
102	 * Driver supports compute acceleration devices. This flag is mutually exclusive with
103	 * @DRIVER_RENDER and @DRIVER_MODESET. Devices that support both graphics and compute
104	 * acceleration should be handled by two drivers that are connected using auxiliary bus.
105	 */
106	DRIVER_COMPUTE_ACCEL            = BIT(7),
107	/**
108	 * @DRIVER_GEM_GPUVA:
109	 *
110	 * Driver supports user defined GPU VA bindings for GEM objects.
111	 */
112	DRIVER_GEM_GPUVA		= BIT(8),
113	/**
114	 * @DRIVER_CURSOR_HOTSPOT:
115	 *
116	 * Driver supports and requires cursor hotspot information in the
117	 * cursor plane (e.g. cursor plane has to actually track the mouse
118	 * cursor and the clients are required to set hotspot in order for
119	 * the cursor planes to work correctly).
120	 */
121	DRIVER_CURSOR_HOTSPOT           = BIT(9),
122
123	/* IMPORTANT: Below are all the legacy flags, add new ones above. */
124
125	/**
126	 * @DRIVER_USE_AGP:
127	 *
128	 * Set up DRM AGP support, see drm_agp_init(), the DRM core will manage
129	 * AGP resources. New drivers don't need this.
130	 */
131	DRIVER_USE_AGP			= BIT(25),
132	/**
133	 * @DRIVER_LEGACY:
134	 *
135	 * Denote a legacy driver using shadow attach. Do not use.
136	 */
137	DRIVER_LEGACY			= BIT(26),
138	/**
139	 * @DRIVER_PCI_DMA:
140	 *
141	 * Driver is capable of PCI DMA, mapping of PCI DMA buffers to userspace
142	 * will be enabled. Only for legacy drivers. Do not use.
143	 */
144	DRIVER_PCI_DMA			= BIT(27),
145	/**
146	 * @DRIVER_SG:
147	 *
148	 * Driver can perform scatter/gather DMA, allocation and mapping of
149	 * scatter/gather buffers will be enabled. Only for legacy drivers. Do
150	 * not use.
151	 */
152	DRIVER_SG			= BIT(28),
153
154	/**
155	 * @DRIVER_HAVE_DMA:
156	 *
157	 * Driver supports DMA, the userspace DMA API will be supported. Only
158	 * for legacy drivers. Do not use.
159	 */
160	DRIVER_HAVE_DMA			= BIT(29),
161	/**
162	 * @DRIVER_HAVE_IRQ:
163	 *
164	 * Legacy irq support. Only for legacy drivers. Do not use.
165	 */
166	DRIVER_HAVE_IRQ			= BIT(30),
167};
168
169/**
170 * struct drm_driver - DRM driver structure
171 *
172 * This structure represent the common code for a family of cards. There will be
173 * one &struct drm_device for each card present in this family. It contains lots
174 * of vfunc entries, and a pile of those probably should be moved to more
175 * appropriate places like &drm_mode_config_funcs or into a new operations
176 * structure for GEM drivers.
177 */
178struct drm_driver {
179	/**
180	 * @load:
181	 *
182	 * Backward-compatible driver callback to complete initialization steps
183	 * after the driver is registered.  For this reason, may suffer from
184	 * race conditions and its use is deprecated for new drivers.  It is
185	 * therefore only supported for existing drivers not yet converted to
186	 * the new scheme.  See devm_drm_dev_alloc() and drm_dev_register() for
187	 * proper and race-free way to set up a &struct drm_device.
188	 *
189	 * This is deprecated, do not use!
190	 *
191	 * Returns:
192	 *
193	 * Zero on success, non-zero value on failure.
194	 */
195	int (*load) (struct drm_device *, unsigned long flags);
196
197	/**
198	 * @open:
199	 *
200	 * Driver callback when a new &struct drm_file is opened. Useful for
201	 * setting up driver-private data structures like buffer allocators,
202	 * execution contexts or similar things. Such driver-private resources
203	 * must be released again in @postclose.
204	 *
205	 * Since the display/modeset side of DRM can only be owned by exactly
206	 * one &struct drm_file (see &drm_file.is_master and &drm_device.master)
207	 * there should never be a need to set up any modeset related resources
208	 * in this callback. Doing so would be a driver design bug.
209	 *
210	 * Returns:
211	 *
212	 * 0 on success, a negative error code on failure, which will be
213	 * promoted to userspace as the result of the open() system call.
214	 */
215	int (*open) (struct drm_device *, struct drm_file *);
216
217	/**
218	 * @postclose:
219	 *
220	 * One of the driver callbacks when a new &struct drm_file is closed.
221	 * Useful for tearing down driver-private data structures allocated in
222	 * @open like buffer allocators, execution contexts or similar things.
223	 *
224	 * Since the display/modeset side of DRM can only be owned by exactly
225	 * one &struct drm_file (see &drm_file.is_master and &drm_device.master)
226	 * there should never be a need to tear down any modeset related
227	 * resources in this callback. Doing so would be a driver design bug.
228	 */
229	void (*postclose) (struct drm_device *, struct drm_file *);
230
231	/**
232	 * @lastclose:
233	 *
234	 * Called when the last &struct drm_file has been closed and there's
235	 * currently no userspace client for the &struct drm_device.
236	 *
237	 * Modern drivers should only use this to force-restore the fbdev
238	 * framebuffer using drm_fb_helper_restore_fbdev_mode_unlocked().
239	 * Anything else would indicate there's something seriously wrong.
240	 * Modern drivers can also use this to execute delayed power switching
241	 * state changes, e.g. in conjunction with the :ref:`vga_switcheroo`
242	 * infrastructure.
243	 *
244	 * This is called after @postclose hook has been called.
245	 *
246	 * NOTE:
247	 *
248	 * All legacy drivers use this callback to de-initialize the hardware.
249	 * This is purely because of the shadow-attach model, where the DRM
250	 * kernel driver does not really own the hardware. Instead ownershipe is
251	 * handled with the help of userspace through an inheritedly racy dance
252	 * to set/unset the VT into raw mode.
253	 *
254	 * Legacy drivers initialize the hardware in the @firstopen callback,
255	 * which isn't even called for modern drivers.
256	 */
257	void (*lastclose) (struct drm_device *);
258
259	/**
260	 * @unload:
261	 *
262	 * Reverse the effects of the driver load callback.  Ideally,
263	 * the clean up performed by the driver should happen in the
264	 * reverse order of the initialization.  Similarly to the load
265	 * hook, this handler is deprecated and its usage should be
266	 * dropped in favor of an open-coded teardown function at the
267	 * driver layer.  See drm_dev_unregister() and drm_dev_put()
268	 * for the proper way to remove a &struct drm_device.
269	 *
270	 * The unload() hook is called right after unregistering
271	 * the device.
272	 *
273	 */
274	void (*unload) (struct drm_device *);
275
276	/**
277	 * @release:
278	 *
279	 * Optional callback for destroying device data after the final
280	 * reference is released, i.e. the device is being destroyed.
281	 *
282	 * This is deprecated, clean up all memory allocations associated with a
283	 * &drm_device using drmm_add_action(), drmm_kmalloc() and related
284	 * managed resources functions.
285	 */
286	void (*release) (struct drm_device *);
287
288	/**
289	 * @master_set:
290	 *
291	 * Called whenever the minor master is set. Only used by vmwgfx.
292	 */
293	void (*master_set)(struct drm_device *dev, struct drm_file *file_priv,
294			   bool from_open);
295	/**
296	 * @master_drop:
297	 *
298	 * Called whenever the minor master is dropped. Only used by vmwgfx.
299	 */
300	void (*master_drop)(struct drm_device *dev, struct drm_file *file_priv);
301
302	/**
303	 * @debugfs_init:
304	 *
305	 * Allows drivers to create driver-specific debugfs files.
306	 */
307	void (*debugfs_init)(struct drm_minor *minor);
308
309	/**
310	 * @gem_create_object: constructor for gem objects
311	 *
312	 * Hook for allocating the GEM object struct, for use by the CMA
313	 * and SHMEM GEM helpers. Returns a GEM object on success, or an
314	 * ERR_PTR()-encoded error code otherwise.
315	 */
316	struct drm_gem_object *(*gem_create_object)(struct drm_device *dev,
317						    size_t size);
318
319	/**
320	 * @prime_handle_to_fd:
321	 *
322	 * PRIME export function. Only used by vmwgfx.
323	 */
324	int (*prime_handle_to_fd)(struct drm_device *dev, struct drm_file *file_priv,
325				uint32_t handle, uint32_t flags, int *prime_fd);
326	/**
327	 * @prime_fd_to_handle:
328	 *
329	 * PRIME import function. Only used by vmwgfx.
330	 */
331	int (*prime_fd_to_handle)(struct drm_device *dev, struct drm_file *file_priv,
332				int prime_fd, uint32_t *handle);
333
334	/**
335	 * @gem_prime_import:
336	 *
337	 * Import hook for GEM drivers.
338	 *
339	 * This defaults to drm_gem_prime_import() if not set.
340	 */
341	struct drm_gem_object * (*gem_prime_import)(struct drm_device *dev,
342				struct dma_buf *dma_buf);
343	/**
344	 * @gem_prime_import_sg_table:
345	 *
346	 * Optional hook used by the PRIME helper functions
347	 * drm_gem_prime_import() respectively drm_gem_prime_import_dev().
348	 */
349	struct drm_gem_object *(*gem_prime_import_sg_table)(
350				struct drm_device *dev,
351				struct dma_buf_attachment *attach,
352				struct sg_table *sgt);
353
354	/**
355	 * @dumb_create:
356	 *
357	 * This creates a new dumb buffer in the driver's backing storage manager (GEM,
358	 * TTM or something else entirely) and returns the resulting buffer handle. This
359	 * handle can then be wrapped up into a framebuffer modeset object.
360	 *
361	 * Note that userspace is not allowed to use such objects for render
362	 * acceleration - drivers must create their own private ioctls for such a use
363	 * case.
364	 *
365	 * Width, height and depth are specified in the &drm_mode_create_dumb
366	 * argument. The callback needs to fill the handle, pitch and size for
367	 * the created buffer.
368	 *
369	 * Called by the user via ioctl.
370	 *
371	 * Returns:
372	 *
373	 * Zero on success, negative errno on failure.
374	 */
375	int (*dumb_create)(struct drm_file *file_priv,
376			   struct drm_device *dev,
377			   struct drm_mode_create_dumb *args);
378	/**
379	 * @dumb_map_offset:
380	 *
381	 * Allocate an offset in the drm device node's address space to be able to
382	 * memory map a dumb buffer.
383	 *
384	 * The default implementation is drm_gem_create_mmap_offset(). GEM based
385	 * drivers must not overwrite this.
386	 *
387	 * Called by the user via ioctl.
388	 *
389	 * Returns:
390	 *
391	 * Zero on success, negative errno on failure.
392	 */
393	int (*dumb_map_offset)(struct drm_file *file_priv,
394			       struct drm_device *dev, uint32_t handle,
395			       uint64_t *offset);
396
397	/**
398	 * @show_fdinfo:
399	 *
400	 * Print device specific fdinfo.  See Documentation/gpu/drm-usage-stats.rst.
401	 */
402	void (*show_fdinfo)(struct drm_printer *p, struct drm_file *f);
403
404	/** @major: driver major number */
405	int major;
406	/** @minor: driver minor number */
407	int minor;
408	/** @patchlevel: driver patch level */
409	int patchlevel;
410	/** @name: driver name */
411	char *name;
412	/** @desc: driver description */
413	char *desc;
414	/** @date: driver date */
415	char *date;
416
417	/**
418	 * @driver_features:
419	 * Driver features, see &enum drm_driver_feature. Drivers can disable
420	 * some features on a per-instance basis using
421	 * &drm_device.driver_features.
422	 */
423	u32 driver_features;
424
425	/**
426	 * @ioctls:
427	 *
428	 * Array of driver-private IOCTL description entries. See the chapter on
429	 * :ref:`IOCTL support in the userland interfaces
430	 * chapter<drm_driver_ioctl>` for the full details.
431	 */
432
433	const struct drm_ioctl_desc *ioctls;
434	/** @num_ioctls: Number of entries in @ioctls. */
435	int num_ioctls;
436
437	/**
438	 * @fops:
439	 *
440	 * File operations for the DRM device node. See the discussion in
441	 * :ref:`file operations<drm_driver_fops>` for in-depth coverage and
442	 * some examples.
443	 */
444	const struct file_operations *fops;
445};
446
447void *__devm_drm_dev_alloc(struct device *parent,
448			   const struct drm_driver *driver,
449			   size_t size, size_t offset);
450
451/**
452 * devm_drm_dev_alloc - Resource managed allocation of a &drm_device instance
453 * @parent: Parent device object
454 * @driver: DRM driver
455 * @type: the type of the struct which contains struct &drm_device
456 * @member: the name of the &drm_device within @type.
457 *
458 * This allocates and initialize a new DRM device. No device registration is done.
459 * Call drm_dev_register() to advertice the device to user space and register it
460 * with other core subsystems. This should be done last in the device
461 * initialization sequence to make sure userspace can't access an inconsistent
462 * state.
463 *
464 * The initial ref-count of the object is 1. Use drm_dev_get() and
465 * drm_dev_put() to take and drop further ref-counts.
466 *
467 * It is recommended that drivers embed &struct drm_device into their own device
468 * structure.
469 *
470 * Note that this manages the lifetime of the resulting &drm_device
471 * automatically using devres. The DRM device initialized with this function is
472 * automatically put on driver detach using drm_dev_put().
473 *
474 * RETURNS:
475 * Pointer to new DRM device, or ERR_PTR on failure.
476 */
477#define devm_drm_dev_alloc(parent, driver, type, member) \
478	((type *) __devm_drm_dev_alloc(parent, driver, sizeof(type), \
479				       offsetof(type, member)))
480
481struct drm_device *drm_dev_alloc(const struct drm_driver *driver,
482				 struct device *parent);
483int drm_dev_register(struct drm_device *dev, unsigned long flags);
484void drm_dev_unregister(struct drm_device *dev);
485
486void drm_dev_get(struct drm_device *dev);
487void drm_dev_put(struct drm_device *dev);
488void drm_put_dev(struct drm_device *dev);
489bool drm_dev_enter(struct drm_device *dev, int *idx);
490void drm_dev_exit(int idx);
491void drm_dev_unplug(struct drm_device *dev);
492
493/**
494 * drm_dev_is_unplugged - is a DRM device unplugged
495 * @dev: DRM device
496 *
497 * This function can be called to check whether a hotpluggable is unplugged.
498 * Unplugging itself is singalled through drm_dev_unplug(). If a device is
499 * unplugged, these two functions guarantee that any store before calling
500 * drm_dev_unplug() is visible to callers of this function after it completes
501 *
502 * WARNING: This function fundamentally races against drm_dev_unplug(). It is
503 * recommended that drivers instead use the underlying drm_dev_enter() and
504 * drm_dev_exit() function pairs.
505 */
506static inline bool drm_dev_is_unplugged(struct drm_device *dev)
507{
508	int idx;
509
510	if (drm_dev_enter(dev, &idx)) {
511		drm_dev_exit(idx);
512		return false;
513	}
514
515	return true;
516}
517
518/**
519 * drm_core_check_all_features - check driver feature flags mask
520 * @dev: DRM device to check
521 * @features: feature flag(s) mask
522 *
523 * This checks @dev for driver features, see &drm_driver.driver_features,
524 * &drm_device.driver_features, and the various &enum drm_driver_feature flags.
525 *
526 * Returns true if all features in the @features mask are supported, false
527 * otherwise.
528 */
529static inline bool drm_core_check_all_features(const struct drm_device *dev,
530					       u32 features)
531{
532	u32 supported = dev->driver->driver_features & dev->driver_features;
533
534	return features && (supported & features) == features;
535}
536
537/**
538 * drm_core_check_feature - check driver feature flags
539 * @dev: DRM device to check
540 * @feature: feature flag
541 *
542 * This checks @dev for driver features, see &drm_driver.driver_features,
543 * &drm_device.driver_features, and the various &enum drm_driver_feature flags.
544 *
545 * Returns true if the @feature is supported, false otherwise.
546 */
547static inline bool drm_core_check_feature(const struct drm_device *dev,
548					  enum drm_driver_feature feature)
549{
550	return drm_core_check_all_features(dev, feature);
551}
552
553/**
554 * drm_drv_uses_atomic_modeset - check if the driver implements
555 * atomic_commit()
556 * @dev: DRM device
557 *
558 * This check is useful if drivers do not have DRIVER_ATOMIC set but
559 * have atomic modesetting internally implemented.
560 */
561static inline bool drm_drv_uses_atomic_modeset(struct drm_device *dev)
562{
563	return drm_core_check_feature(dev, DRIVER_ATOMIC) ||
564		(dev->mode_config.funcs && dev->mode_config.funcs->atomic_commit != NULL);
565}
566
567
568/* TODO: Inline drm_firmware_drivers_only() in all its callers. */
569static inline bool drm_firmware_drivers_only(void)
570{
571	return video_firmware_drivers_only();
572}
573
574#if defined(CONFIG_DEBUG_FS)
575void drm_debugfs_dev_init(struct drm_device *dev, struct dentry *root);
576#else
577static inline void drm_debugfs_dev_init(struct drm_device *dev, struct dentry *root)
578{
579}
580#endif
581
582#endif
583