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