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