xref: /netbsd-src/sys/external/bsd/drm2/dist/include/drm/drm_mode_object.h (revision 41ec02673d281bbb3d38e6c78504ce6e30c228c1)
1 /*	$NetBSD: drm_mode_object.h,v 1.2 2021/12/18 23:45:46 riastradh Exp $	*/
2 
3 /*
4  * Copyright (c) 2016 Intel Corporation
5  *
6  * Permission to use, copy, modify, distribute, and sell this software and its
7  * documentation for any purpose is hereby granted without fee, provided that
8  * the above copyright notice appear in all copies and that both that copyright
9  * notice and this permission notice appear in supporting documentation, and
10  * that the name of the copyright holders not be used in advertising or
11  * publicity pertaining to distribution of the software without specific,
12  * written prior permission.  The copyright holders make no representations
13  * about the suitability of this software for any purpose.  It is provided "as
14  * is" without express or implied warranty.
15  *
16  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
19  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
20  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
21  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
22  * OF THIS SOFTWARE.
23  */
24 
25 #ifndef __DRM_MODESET_H__
26 #define __DRM_MODESET_H__
27 
28 #include <linux/kref.h>
29 #include <drm/drm_lease.h>
30 struct drm_object_properties;
31 struct drm_property;
32 struct drm_device;
33 struct drm_file;
34 
35 /**
36  * struct drm_mode_object - base structure for modeset objects
37  * @id: userspace visible identifier
38  * @type: type of the object, one of DRM_MODE_OBJECT\_\*
39  * @properties: properties attached to this object, including values
40  * @refcount: reference count for objects which with dynamic lifetime
41  * @free_cb: free function callback, only set for objects with dynamic lifetime
42  *
43  * Base structure for modeset objects visible to userspace. Objects can be
44  * looked up using drm_mode_object_find(). Besides basic uapi interface
45  * properties like @id and @type it provides two services:
46  *
47  * - It tracks attached properties and their values. This is used by &drm_crtc,
48  *   &drm_plane and &drm_connector. Properties are attached by calling
49  *   drm_object_attach_property() before the object is visible to userspace.
50  *
51  * - For objects with dynamic lifetimes (as indicated by a non-NULL @free_cb) it
52  *   provides reference counting through drm_mode_object_get() and
53  *   drm_mode_object_put(). This is used by &drm_framebuffer, &drm_connector
54  *   and &drm_property_blob. These objects provide specialized reference
55  *   counting wrappers.
56  */
57 struct drm_mode_object {
58 	uint32_t id;
59 	uint32_t type;
60 	struct drm_object_properties *properties;
61 	struct kref refcount;
62 	void (*free_cb)(struct kref *kref);
63 };
64 
65 #define DRM_OBJECT_MAX_PROPERTY 24
66 /**
67  * struct drm_object_properties - property tracking for &drm_mode_object
68  */
69 struct drm_object_properties {
70 	/**
71 	 * @count: number of valid properties, must be less than or equal to
72 	 * DRM_OBJECT_MAX_PROPERTY.
73 	 */
74 
75 	int count;
76 	/**
77 	 * @properties: Array of pointers to &drm_property.
78 	 *
79 	 * NOTE: if we ever start dynamically destroying properties (ie.
80 	 * not at drm_mode_config_cleanup() time), then we'd have to do
81 	 * a better job of detaching property from mode objects to avoid
82 	 * dangling property pointers:
83 	 */
84 	struct drm_property *properties[DRM_OBJECT_MAX_PROPERTY];
85 
86 	/**
87 	 * @values: Array to store the property values, matching @properties. Do
88 	 * not read/write values directly, but use
89 	 * drm_object_property_get_value() and drm_object_property_set_value().
90 	 *
91 	 * Note that atomic drivers do not store mutable properties in this
92 	 * array, but only the decoded values in the corresponding state
93 	 * structure. The decoding is done using the &drm_crtc.atomic_get_property and
94 	 * &drm_crtc.atomic_set_property hooks for &struct drm_crtc. For
95 	 * &struct drm_plane the hooks are &drm_plane_funcs.atomic_get_property and
96 	 * &drm_plane_funcs.atomic_set_property. And for &struct drm_connector
97 	 * the hooks are &drm_connector_funcs.atomic_get_property and
98 	 * &drm_connector_funcs.atomic_set_property .
99 	 *
100 	 * Hence atomic drivers should not use drm_object_property_set_value()
101 	 * and drm_object_property_get_value() on mutable objects, i.e. those
102 	 * without the DRM_MODE_PROP_IMMUTABLE flag set.
103 	 */
104 	uint64_t values[DRM_OBJECT_MAX_PROPERTY];
105 };
106 
107 /* Avoid boilerplate.  I'm tired of typing. */
108 #define DRM_ENUM_NAME_FN(fnname, list)				\
109 	const char *fnname(int val)				\
110 	{							\
111 		int i;						\
112 		for (i = 0; i < ARRAY_SIZE(list); i++) {	\
113 			if (list[i].type == val)		\
114 				return list[i].name;		\
115 		}						\
116 		return "(unknown)";				\
117 	}
118 
119 struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
120 					     struct drm_file *file_priv,
121 					     uint32_t id, uint32_t type);
122 void drm_mode_object_get(struct drm_mode_object *obj);
123 void drm_mode_object_put(struct drm_mode_object *obj);
124 
125 int drm_object_property_set_value(struct drm_mode_object *obj,
126 				  struct drm_property *property,
127 				  uint64_t val);
128 int drm_object_property_get_value(struct drm_mode_object *obj,
129 				  struct drm_property *property,
130 				  uint64_t *value);
131 
132 void drm_object_attach_property(struct drm_mode_object *obj,
133 				struct drm_property *property,
134 				uint64_t init_val);
135 
136 bool drm_mode_object_lease_required(uint32_t type);
137 #endif
138