drm_crtc.c revision 1.13 1 1.13 riastrad /* $NetBSD: drm_crtc.c,v 1.13 2020/02/14 04:35:19 riastradh Exp $ */
2 1.5 riastrad
3 1.1 riastrad /*
4 1.1 riastrad * Copyright (c) 2006-2008 Intel Corporation
5 1.1 riastrad * Copyright (c) 2007 Dave Airlie <airlied (at) linux.ie>
6 1.1 riastrad * Copyright (c) 2008 Red Hat Inc.
7 1.1 riastrad *
8 1.1 riastrad * DRM core CRTC related functions
9 1.1 riastrad *
10 1.1 riastrad * Permission to use, copy, modify, distribute, and sell this software and its
11 1.1 riastrad * documentation for any purpose is hereby granted without fee, provided that
12 1.1 riastrad * the above copyright notice appear in all copies and that both that copyright
13 1.1 riastrad * notice and this permission notice appear in supporting documentation, and
14 1.1 riastrad * that the name of the copyright holders not be used in advertising or
15 1.1 riastrad * publicity pertaining to distribution of the software without specific,
16 1.1 riastrad * written prior permission. The copyright holders make no representations
17 1.1 riastrad * about the suitability of this software for any purpose. It is provided "as
18 1.1 riastrad * is" without express or implied warranty.
19 1.1 riastrad *
20 1.1 riastrad * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
21 1.1 riastrad * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
22 1.1 riastrad * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
23 1.1 riastrad * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
24 1.1 riastrad * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
25 1.1 riastrad * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
26 1.1 riastrad * OF THIS SOFTWARE.
27 1.1 riastrad *
28 1.1 riastrad * Authors:
29 1.1 riastrad * Keith Packard
30 1.1 riastrad * Eric Anholt <eric (at) anholt.net>
31 1.1 riastrad * Dave Airlie <airlied (at) linux.ie>
32 1.1 riastrad * Jesse Barnes <jesse.barnes (at) intel.com>
33 1.1 riastrad */
34 1.5 riastrad #include <sys/cdefs.h>
35 1.13 riastrad __KERNEL_RCSID(0, "$NetBSD: drm_crtc.c,v 1.13 2020/02/14 04:35:19 riastradh Exp $");
36 1.5 riastrad
37 1.2 riastrad #include <linux/err.h>
38 1.2 riastrad #include <linux/spinlock.h>
39 1.3 riastrad #include <linux/ctype.h>
40 1.1 riastrad #include <linux/list.h>
41 1.1 riastrad #include <linux/slab.h>
42 1.1 riastrad #include <linux/export.h>
43 1.4 riastrad #include <linux/errno.h>
44 1.2 riastrad #include <asm/bug.h>
45 1.1 riastrad #include <drm/drmP.h>
46 1.1 riastrad #include <drm/drm_crtc.h>
47 1.1 riastrad #include <drm/drm_edid.h>
48 1.1 riastrad #include <drm/drm_fourcc.h>
49 1.5 riastrad #include <drm/drm_modeset_lock.h>
50 1.5 riastrad #include <drm/drm_atomic.h>
51 1.1 riastrad
52 1.3 riastrad #include "drm_crtc_internal.h"
53 1.5 riastrad #include "drm_internal.h"
54 1.3 riastrad
55 1.13 riastrad #include <linux/nbsd-namespace.h>
56 1.13 riastrad
57 1.5 riastrad static struct drm_framebuffer *
58 1.5 riastrad internal_framebuffer_create(struct drm_device *dev,
59 1.5 riastrad struct drm_mode_fb_cmd2 *r,
60 1.5 riastrad struct drm_file *file_priv);
61 1.3 riastrad
62 1.1 riastrad /* Avoid boilerplate. I'm tired of typing. */
63 1.1 riastrad #define DRM_ENUM_NAME_FN(fnname, list) \
64 1.2 riastrad const char *fnname(int val) \
65 1.1 riastrad { \
66 1.1 riastrad int i; \
67 1.1 riastrad for (i = 0; i < ARRAY_SIZE(list); i++) { \
68 1.1 riastrad if (list[i].type == val) \
69 1.1 riastrad return list[i].name; \
70 1.1 riastrad } \
71 1.1 riastrad return "(unknown)"; \
72 1.1 riastrad }
73 1.1 riastrad
74 1.1 riastrad /*
75 1.1 riastrad * Global properties
76 1.1 riastrad */
77 1.5 riastrad static const struct drm_prop_enum_list drm_dpms_enum_list[] = {
78 1.5 riastrad { DRM_MODE_DPMS_ON, "On" },
79 1.1 riastrad { DRM_MODE_DPMS_STANDBY, "Standby" },
80 1.1 riastrad { DRM_MODE_DPMS_SUSPEND, "Suspend" },
81 1.1 riastrad { DRM_MODE_DPMS_OFF, "Off" }
82 1.1 riastrad };
83 1.1 riastrad
84 1.1 riastrad DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
85 1.1 riastrad
86 1.5 riastrad static const struct drm_prop_enum_list drm_plane_type_enum_list[] = {
87 1.3 riastrad { DRM_PLANE_TYPE_OVERLAY, "Overlay" },
88 1.3 riastrad { DRM_PLANE_TYPE_PRIMARY, "Primary" },
89 1.3 riastrad { DRM_PLANE_TYPE_CURSOR, "Cursor" },
90 1.3 riastrad };
91 1.3 riastrad
92 1.1 riastrad /*
93 1.1 riastrad * Optional properties
94 1.1 riastrad */
95 1.5 riastrad static const struct drm_prop_enum_list drm_scaling_mode_enum_list[] = {
96 1.1 riastrad { DRM_MODE_SCALE_NONE, "None" },
97 1.1 riastrad { DRM_MODE_SCALE_FULLSCREEN, "Full" },
98 1.1 riastrad { DRM_MODE_SCALE_CENTER, "Center" },
99 1.1 riastrad { DRM_MODE_SCALE_ASPECT, "Full aspect" },
100 1.1 riastrad };
101 1.1 riastrad
102 1.5 riastrad static const struct drm_prop_enum_list drm_aspect_ratio_enum_list[] = {
103 1.5 riastrad { DRM_MODE_PICTURE_ASPECT_NONE, "Automatic" },
104 1.5 riastrad { DRM_MODE_PICTURE_ASPECT_4_3, "4:3" },
105 1.5 riastrad { DRM_MODE_PICTURE_ASPECT_16_9, "16:9" },
106 1.5 riastrad };
107 1.5 riastrad
108 1.1 riastrad /*
109 1.1 riastrad * Non-global properties, but "required" for certain connectors.
110 1.1 riastrad */
111 1.5 riastrad static const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] = {
112 1.1 riastrad { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
113 1.1 riastrad { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
114 1.1 riastrad { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
115 1.1 riastrad };
116 1.1 riastrad
117 1.1 riastrad DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
118 1.1 riastrad
119 1.5 riastrad static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] = {
120 1.1 riastrad { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
121 1.1 riastrad { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
122 1.1 riastrad { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
123 1.1 riastrad };
124 1.1 riastrad
125 1.1 riastrad DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
126 1.1 riastrad drm_dvi_i_subconnector_enum_list)
127 1.1 riastrad
128 1.5 riastrad static const struct drm_prop_enum_list drm_tv_select_enum_list[] = {
129 1.1 riastrad { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
130 1.1 riastrad { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
131 1.1 riastrad { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
132 1.1 riastrad { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
133 1.1 riastrad { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
134 1.1 riastrad };
135 1.1 riastrad
136 1.1 riastrad DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
137 1.1 riastrad
138 1.5 riastrad static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] = {
139 1.1 riastrad { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
140 1.1 riastrad { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
141 1.1 riastrad { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
142 1.1 riastrad { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
143 1.1 riastrad { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
144 1.1 riastrad };
145 1.1 riastrad
146 1.1 riastrad DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
147 1.1 riastrad drm_tv_subconnector_enum_list)
148 1.1 riastrad
149 1.3 riastrad static const struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
150 1.1 riastrad { DRM_MODE_DIRTY_OFF, "Off" },
151 1.1 riastrad { DRM_MODE_DIRTY_ON, "On" },
152 1.1 riastrad { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
153 1.1 riastrad };
154 1.1 riastrad
155 1.1 riastrad struct drm_conn_prop_enum_list {
156 1.1 riastrad int type;
157 1.2 riastrad const char *name;
158 1.3 riastrad struct ida ida;
159 1.1 riastrad };
160 1.1 riastrad
161 1.1 riastrad /*
162 1.1 riastrad * Connector and encoder types.
163 1.1 riastrad */
164 1.5 riastrad static struct drm_conn_prop_enum_list drm_connector_enum_list[] = {
165 1.5 riastrad { DRM_MODE_CONNECTOR_Unknown, "Unknown" },
166 1.3 riastrad { DRM_MODE_CONNECTOR_VGA, "VGA" },
167 1.3 riastrad { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
168 1.3 riastrad { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
169 1.3 riastrad { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
170 1.3 riastrad { DRM_MODE_CONNECTOR_Composite, "Composite" },
171 1.3 riastrad { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO" },
172 1.3 riastrad { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
173 1.3 riastrad { DRM_MODE_CONNECTOR_Component, "Component" },
174 1.3 riastrad { DRM_MODE_CONNECTOR_9PinDIN, "DIN" },
175 1.3 riastrad { DRM_MODE_CONNECTOR_DisplayPort, "DP" },
176 1.3 riastrad { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
177 1.3 riastrad { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
178 1.3 riastrad { DRM_MODE_CONNECTOR_TV, "TV" },
179 1.3 riastrad { DRM_MODE_CONNECTOR_eDP, "eDP" },
180 1.3 riastrad { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual" },
181 1.3 riastrad { DRM_MODE_CONNECTOR_DSI, "DSI" },
182 1.1 riastrad };
183 1.1 riastrad
184 1.5 riastrad static const struct drm_prop_enum_list drm_encoder_enum_list[] = {
185 1.5 riastrad { DRM_MODE_ENCODER_NONE, "None" },
186 1.1 riastrad { DRM_MODE_ENCODER_DAC, "DAC" },
187 1.1 riastrad { DRM_MODE_ENCODER_TMDS, "TMDS" },
188 1.1 riastrad { DRM_MODE_ENCODER_LVDS, "LVDS" },
189 1.1 riastrad { DRM_MODE_ENCODER_TVDAC, "TV" },
190 1.1 riastrad { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
191 1.3 riastrad { DRM_MODE_ENCODER_DSI, "DSI" },
192 1.5 riastrad { DRM_MODE_ENCODER_DPMST, "DP MST" },
193 1.3 riastrad };
194 1.3 riastrad
195 1.5 riastrad static const struct drm_prop_enum_list drm_subpixel_enum_list[] = {
196 1.3 riastrad { SubPixelUnknown, "Unknown" },
197 1.3 riastrad { SubPixelHorizontalRGB, "Horizontal RGB" },
198 1.3 riastrad { SubPixelHorizontalBGR, "Horizontal BGR" },
199 1.3 riastrad { SubPixelVerticalRGB, "Vertical RGB" },
200 1.3 riastrad { SubPixelVerticalBGR, "Vertical BGR" },
201 1.3 riastrad { SubPixelNone, "None" },
202 1.1 riastrad };
203 1.1 riastrad
204 1.3 riastrad void drm_connector_ida_init(void)
205 1.3 riastrad {
206 1.3 riastrad int i;
207 1.3 riastrad
208 1.3 riastrad for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
209 1.3 riastrad ida_init(&drm_connector_enum_list[i].ida);
210 1.3 riastrad }
211 1.3 riastrad
212 1.3 riastrad void drm_connector_ida_destroy(void)
213 1.3 riastrad {
214 1.3 riastrad int i;
215 1.3 riastrad
216 1.3 riastrad for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
217 1.3 riastrad ida_destroy(&drm_connector_enum_list[i].ida);
218 1.3 riastrad }
219 1.3 riastrad
220 1.3 riastrad /**
221 1.3 riastrad * drm_get_connector_status_name - return a string for connector status
222 1.3 riastrad * @status: connector status to compute name of
223 1.3 riastrad *
224 1.3 riastrad * In contrast to the other drm_get_*_name functions this one here returns a
225 1.3 riastrad * const pointer and hence is threadsafe.
226 1.3 riastrad */
227 1.3 riastrad const char *drm_get_connector_status_name(enum drm_connector_status status)
228 1.1 riastrad {
229 1.1 riastrad if (status == connector_status_connected)
230 1.1 riastrad return "connected";
231 1.1 riastrad else if (status == connector_status_disconnected)
232 1.1 riastrad return "disconnected";
233 1.1 riastrad else
234 1.1 riastrad return "unknown";
235 1.1 riastrad }
236 1.3 riastrad EXPORT_SYMBOL(drm_get_connector_status_name);
237 1.3 riastrad
238 1.3 riastrad /**
239 1.3 riastrad * drm_get_subpixel_order_name - return a string for a given subpixel enum
240 1.3 riastrad * @order: enum of subpixel_order
241 1.3 riastrad *
242 1.3 riastrad * Note you could abuse this and return something out of bounds, but that
243 1.3 riastrad * would be a caller error. No unscrubbed user data should make it here.
244 1.3 riastrad */
245 1.3 riastrad const char *drm_get_subpixel_order_name(enum subpixel_order order)
246 1.3 riastrad {
247 1.3 riastrad return drm_subpixel_enum_list[order].name;
248 1.3 riastrad }
249 1.3 riastrad EXPORT_SYMBOL(drm_get_subpixel_order_name);
250 1.3 riastrad
251 1.3 riastrad static char printable_char(int c)
252 1.3 riastrad {
253 1.3 riastrad return isascii(c) && isprint(c) ? c : '?';
254 1.3 riastrad }
255 1.3 riastrad
256 1.3 riastrad /**
257 1.3 riastrad * drm_get_format_name - return a string for drm fourcc format
258 1.3 riastrad * @format: format to compute name of
259 1.3 riastrad *
260 1.3 riastrad * Note that the buffer used by this function is globally shared and owned by
261 1.3 riastrad * the function itself.
262 1.3 riastrad *
263 1.3 riastrad * FIXME: This isn't really multithreading safe.
264 1.3 riastrad */
265 1.3 riastrad const char *drm_get_format_name(uint32_t format)
266 1.3 riastrad {
267 1.3 riastrad static char buf[32];
268 1.3 riastrad
269 1.3 riastrad snprintf(buf, sizeof(buf),
270 1.3 riastrad "%c%c%c%c %s-endian (0x%08x)",
271 1.3 riastrad printable_char(format & 0xff),
272 1.3 riastrad printable_char((format >> 8) & 0xff),
273 1.3 riastrad printable_char((format >> 16) & 0xff),
274 1.3 riastrad printable_char((format >> 24) & 0x7f),
275 1.3 riastrad format & DRM_FORMAT_BIG_ENDIAN ? "big" : "little",
276 1.3 riastrad format);
277 1.3 riastrad
278 1.3 riastrad return buf;
279 1.3 riastrad }
280 1.3 riastrad EXPORT_SYMBOL(drm_get_format_name);
281 1.1 riastrad
282 1.5 riastrad /*
283 1.5 riastrad * Internal function to assign a slot in the object idr and optionally
284 1.5 riastrad * register the object into the idr.
285 1.5 riastrad */
286 1.5 riastrad static int drm_mode_object_get_reg(struct drm_device *dev,
287 1.5 riastrad struct drm_mode_object *obj,
288 1.5 riastrad uint32_t obj_type,
289 1.5 riastrad bool register_obj)
290 1.5 riastrad {
291 1.5 riastrad int ret;
292 1.5 riastrad
293 1.5 riastrad idr_preload(GFP_KERNEL);
294 1.5 riastrad mutex_lock(&dev->mode_config.idr_mutex);
295 1.5 riastrad ret = idr_alloc(&dev->mode_config.crtc_idr, register_obj ? obj : NULL, 1, 0, GFP_KERNEL);
296 1.5 riastrad if (ret >= 0) {
297 1.5 riastrad /*
298 1.5 riastrad * Set up the object linking under the protection of the idr
299 1.5 riastrad * lock so that other users can't see inconsistent state.
300 1.5 riastrad */
301 1.5 riastrad obj->id = ret;
302 1.5 riastrad obj->type = obj_type;
303 1.5 riastrad }
304 1.5 riastrad mutex_unlock(&dev->mode_config.idr_mutex);
305 1.5 riastrad idr_preload_end();
306 1.5 riastrad
307 1.5 riastrad return ret < 0 ? ret : 0;
308 1.5 riastrad }
309 1.5 riastrad
310 1.1 riastrad /**
311 1.3 riastrad * drm_mode_object_get - allocate a new modeset identifier
312 1.1 riastrad * @dev: DRM device
313 1.3 riastrad * @obj: object pointer, used to generate unique ID
314 1.3 riastrad * @obj_type: object type
315 1.1 riastrad *
316 1.1 riastrad * Create a unique identifier based on @ptr in @dev's identifier space. Used
317 1.3 riastrad * for tracking modes, CRTCs and connectors. Note that despite the _get postfix
318 1.3 riastrad * modeset identifiers are _not_ reference counted. Hence don't use this for
319 1.3 riastrad * reference counted modeset objects like framebuffers.
320 1.1 riastrad *
321 1.3 riastrad * Returns:
322 1.5 riastrad * Zero on success, error code on failure.
323 1.1 riastrad */
324 1.3 riastrad int drm_mode_object_get(struct drm_device *dev,
325 1.3 riastrad struct drm_mode_object *obj, uint32_t obj_type)
326 1.1 riastrad {
327 1.5 riastrad return drm_mode_object_get_reg(dev, obj, obj_type, true);
328 1.5 riastrad }
329 1.1 riastrad
330 1.5 riastrad static void drm_mode_object_register(struct drm_device *dev,
331 1.5 riastrad struct drm_mode_object *obj)
332 1.5 riastrad {
333 1.3 riastrad mutex_lock(&dev->mode_config.idr_mutex);
334 1.5 riastrad idr_replace(&dev->mode_config.crtc_idr, obj, obj->id);
335 1.1 riastrad mutex_unlock(&dev->mode_config.idr_mutex);
336 1.1 riastrad }
337 1.1 riastrad
338 1.1 riastrad /**
339 1.3 riastrad * drm_mode_object_put - free a modeset identifer
340 1.1 riastrad * @dev: DRM device
341 1.3 riastrad * @object: object to free
342 1.1 riastrad *
343 1.3 riastrad * Free @id from @dev's unique identifier pool. Note that despite the _get
344 1.3 riastrad * postfix modeset identifiers are _not_ reference counted. Hence don't use this
345 1.3 riastrad * for reference counted modeset objects like framebuffers.
346 1.1 riastrad */
347 1.3 riastrad void drm_mode_object_put(struct drm_device *dev,
348 1.3 riastrad struct drm_mode_object *object)
349 1.1 riastrad {
350 1.1 riastrad mutex_lock(&dev->mode_config.idr_mutex);
351 1.1 riastrad idr_remove(&dev->mode_config.crtc_idr, object->id);
352 1.1 riastrad mutex_unlock(&dev->mode_config.idr_mutex);
353 1.1 riastrad }
354 1.1 riastrad
355 1.5 riastrad static struct drm_mode_object *_object_find(struct drm_device *dev,
356 1.5 riastrad uint32_t id, uint32_t type)
357 1.5 riastrad {
358 1.5 riastrad struct drm_mode_object *obj = NULL;
359 1.5 riastrad
360 1.5 riastrad mutex_lock(&dev->mode_config.idr_mutex);
361 1.5 riastrad obj = idr_find(&dev->mode_config.crtc_idr, id);
362 1.5 riastrad if (obj && type != DRM_MODE_OBJECT_ANY && obj->type != type)
363 1.5 riastrad obj = NULL;
364 1.5 riastrad if (obj && obj->id != id)
365 1.5 riastrad obj = NULL;
366 1.5 riastrad /* don't leak out unref'd fb's */
367 1.5 riastrad if (obj &&
368 1.5 riastrad (obj->type == DRM_MODE_OBJECT_FB ||
369 1.5 riastrad obj->type == DRM_MODE_OBJECT_BLOB))
370 1.5 riastrad obj = NULL;
371 1.5 riastrad mutex_unlock(&dev->mode_config.idr_mutex);
372 1.5 riastrad
373 1.5 riastrad return obj;
374 1.5 riastrad }
375 1.5 riastrad
376 1.3 riastrad /**
377 1.3 riastrad * drm_mode_object_find - look up a drm object with static lifetime
378 1.3 riastrad * @dev: drm device
379 1.3 riastrad * @id: id of the mode object
380 1.3 riastrad * @type: type of the mode object
381 1.3 riastrad *
382 1.3 riastrad * Note that framebuffers cannot be looked up with this functions - since those
383 1.5 riastrad * are reference counted, they need special treatment. Even with
384 1.5 riastrad * DRM_MODE_OBJECT_ANY (although that will simply return NULL
385 1.5 riastrad * rather than WARN_ON()).
386 1.3 riastrad */
387 1.1 riastrad struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
388 1.1 riastrad uint32_t id, uint32_t type)
389 1.1 riastrad {
390 1.1 riastrad struct drm_mode_object *obj = NULL;
391 1.1 riastrad
392 1.3 riastrad /* Framebuffers are reference counted and need their own lookup
393 1.3 riastrad * function.*/
394 1.5 riastrad WARN_ON(type == DRM_MODE_OBJECT_FB || type == DRM_MODE_OBJECT_BLOB);
395 1.5 riastrad obj = _object_find(dev, id, type);
396 1.1 riastrad return obj;
397 1.1 riastrad }
398 1.1 riastrad EXPORT_SYMBOL(drm_mode_object_find);
399 1.1 riastrad
400 1.1 riastrad /**
401 1.1 riastrad * drm_framebuffer_init - initialize a framebuffer
402 1.1 riastrad * @dev: DRM device
403 1.3 riastrad * @fb: framebuffer to be initialized
404 1.3 riastrad * @funcs: ... with these functions
405 1.1 riastrad *
406 1.1 riastrad * Allocates an ID for the framebuffer's parent mode object, sets its mode
407 1.1 riastrad * functions & device file and adds it to the master fd list.
408 1.1 riastrad *
409 1.3 riastrad * IMPORTANT:
410 1.3 riastrad * This functions publishes the fb and makes it available for concurrent access
411 1.3 riastrad * by other users. Which means by this point the fb _must_ be fully set up -
412 1.3 riastrad * since all the fb attributes are invariant over its lifetime, no further
413 1.3 riastrad * locking but only correct reference counting is required.
414 1.3 riastrad *
415 1.3 riastrad * Returns:
416 1.1 riastrad * Zero on success, error code on failure.
417 1.1 riastrad */
418 1.1 riastrad int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
419 1.1 riastrad const struct drm_framebuffer_funcs *funcs)
420 1.1 riastrad {
421 1.1 riastrad int ret;
422 1.1 riastrad
423 1.3 riastrad mutex_lock(&dev->mode_config.fb_lock);
424 1.1 riastrad kref_init(&fb->refcount);
425 1.3 riastrad INIT_LIST_HEAD(&fb->filp_head);
426 1.3 riastrad fb->dev = dev;
427 1.3 riastrad fb->funcs = funcs;
428 1.1 riastrad
429 1.1 riastrad ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
430 1.1 riastrad if (ret)
431 1.3 riastrad goto out;
432 1.3 riastrad
433 1.1 riastrad dev->mode_config.num_fb++;
434 1.1 riastrad list_add(&fb->head, &dev->mode_config.fb_list);
435 1.3 riastrad out:
436 1.3 riastrad mutex_unlock(&dev->mode_config.fb_lock);
437 1.1 riastrad
438 1.5 riastrad return ret;
439 1.1 riastrad }
440 1.1 riastrad EXPORT_SYMBOL(drm_framebuffer_init);
441 1.1 riastrad
442 1.5 riastrad /* dev->mode_config.fb_lock must be held! */
443 1.5 riastrad static void __drm_framebuffer_unregister(struct drm_device *dev,
444 1.5 riastrad struct drm_framebuffer *fb)
445 1.5 riastrad {
446 1.5 riastrad mutex_lock(&dev->mode_config.idr_mutex);
447 1.5 riastrad idr_remove(&dev->mode_config.crtc_idr, fb->base.id);
448 1.5 riastrad mutex_unlock(&dev->mode_config.idr_mutex);
449 1.5 riastrad
450 1.5 riastrad fb->base.id = 0;
451 1.5 riastrad }
452 1.5 riastrad
453 1.1 riastrad static void drm_framebuffer_free(struct kref *kref)
454 1.1 riastrad {
455 1.1 riastrad struct drm_framebuffer *fb =
456 1.1 riastrad container_of(kref, struct drm_framebuffer, refcount);
457 1.5 riastrad struct drm_device *dev = fb->dev;
458 1.5 riastrad
459 1.5 riastrad /*
460 1.5 riastrad * The lookup idr holds a weak reference, which has not necessarily been
461 1.5 riastrad * removed at this point. Check for that.
462 1.5 riastrad */
463 1.5 riastrad mutex_lock(&dev->mode_config.fb_lock);
464 1.5 riastrad if (fb->base.id) {
465 1.5 riastrad /* Mark fb as reaped and drop idr ref. */
466 1.5 riastrad __drm_framebuffer_unregister(dev, fb);
467 1.5 riastrad }
468 1.5 riastrad mutex_unlock(&dev->mode_config.fb_lock);
469 1.5 riastrad
470 1.1 riastrad fb->funcs->destroy(fb);
471 1.1 riastrad }
472 1.1 riastrad
473 1.3 riastrad static struct drm_framebuffer *__drm_framebuffer_lookup(struct drm_device *dev,
474 1.3 riastrad uint32_t id)
475 1.3 riastrad {
476 1.3 riastrad struct drm_mode_object *obj = NULL;
477 1.3 riastrad struct drm_framebuffer *fb;
478 1.3 riastrad
479 1.3 riastrad mutex_lock(&dev->mode_config.idr_mutex);
480 1.3 riastrad obj = idr_find(&dev->mode_config.crtc_idr, id);
481 1.3 riastrad if (!obj || (obj->type != DRM_MODE_OBJECT_FB) || (obj->id != id))
482 1.3 riastrad fb = NULL;
483 1.3 riastrad else
484 1.3 riastrad fb = obj_to_fb(obj);
485 1.3 riastrad mutex_unlock(&dev->mode_config.idr_mutex);
486 1.3 riastrad
487 1.3 riastrad return fb;
488 1.3 riastrad }
489 1.3 riastrad
490 1.3 riastrad /**
491 1.3 riastrad * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
492 1.3 riastrad * @dev: drm device
493 1.3 riastrad * @id: id of the fb object
494 1.3 riastrad *
495 1.3 riastrad * If successful, this grabs an additional reference to the framebuffer -
496 1.3 riastrad * callers need to make sure to eventually unreference the returned framebuffer
497 1.3 riastrad * again, using @drm_framebuffer_unreference.
498 1.3 riastrad */
499 1.3 riastrad struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
500 1.3 riastrad uint32_t id)
501 1.3 riastrad {
502 1.3 riastrad struct drm_framebuffer *fb;
503 1.3 riastrad
504 1.3 riastrad mutex_lock(&dev->mode_config.fb_lock);
505 1.3 riastrad fb = __drm_framebuffer_lookup(dev, id);
506 1.5 riastrad if (fb) {
507 1.5 riastrad if (!kref_get_unless_zero(&fb->refcount))
508 1.5 riastrad fb = NULL;
509 1.5 riastrad }
510 1.3 riastrad mutex_unlock(&dev->mode_config.fb_lock);
511 1.3 riastrad
512 1.3 riastrad return fb;
513 1.3 riastrad }
514 1.3 riastrad EXPORT_SYMBOL(drm_framebuffer_lookup);
515 1.3 riastrad
516 1.1 riastrad /**
517 1.1 riastrad * drm_framebuffer_unreference - unref a framebuffer
518 1.3 riastrad * @fb: framebuffer to unref
519 1.1 riastrad *
520 1.3 riastrad * This functions decrements the fb's refcount and frees it if it drops to zero.
521 1.1 riastrad */
522 1.1 riastrad void drm_framebuffer_unreference(struct drm_framebuffer *fb)
523 1.1 riastrad {
524 1.7 riastrad DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, fb->refcount.kr_count);
525 1.1 riastrad kref_put(&fb->refcount, drm_framebuffer_free);
526 1.1 riastrad }
527 1.1 riastrad EXPORT_SYMBOL(drm_framebuffer_unreference);
528 1.1 riastrad
529 1.1 riastrad /**
530 1.1 riastrad * drm_framebuffer_reference - incr the fb refcnt
531 1.3 riastrad * @fb: framebuffer
532 1.3 riastrad *
533 1.3 riastrad * This functions increments the fb's refcount.
534 1.1 riastrad */
535 1.1 riastrad void drm_framebuffer_reference(struct drm_framebuffer *fb)
536 1.1 riastrad {
537 1.7 riastrad DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, fb->refcount.kr_count);
538 1.1 riastrad kref_get(&fb->refcount);
539 1.1 riastrad }
540 1.1 riastrad EXPORT_SYMBOL(drm_framebuffer_reference);
541 1.1 riastrad
542 1.3 riastrad /**
543 1.3 riastrad * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
544 1.3 riastrad * @fb: fb to unregister
545 1.3 riastrad *
546 1.3 riastrad * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
547 1.3 riastrad * those used for fbdev. Note that the caller must hold a reference of it's own,
548 1.3 riastrad * i.e. the object may not be destroyed through this call (since it'll lead to a
549 1.3 riastrad * locking inversion).
550 1.3 riastrad */
551 1.3 riastrad void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
552 1.3 riastrad {
553 1.5 riastrad struct drm_device *dev;
554 1.5 riastrad
555 1.5 riastrad if (!fb)
556 1.5 riastrad return;
557 1.5 riastrad
558 1.5 riastrad dev = fb->dev;
559 1.3 riastrad
560 1.3 riastrad mutex_lock(&dev->mode_config.fb_lock);
561 1.3 riastrad /* Mark fb as reaped and drop idr ref. */
562 1.3 riastrad __drm_framebuffer_unregister(dev, fb);
563 1.3 riastrad mutex_unlock(&dev->mode_config.fb_lock);
564 1.3 riastrad }
565 1.3 riastrad EXPORT_SYMBOL(drm_framebuffer_unregister_private);
566 1.3 riastrad
567 1.1 riastrad /**
568 1.1 riastrad * drm_framebuffer_cleanup - remove a framebuffer object
569 1.1 riastrad * @fb: framebuffer to remove
570 1.1 riastrad *
571 1.3 riastrad * Cleanup framebuffer. This function is intended to be used from the drivers
572 1.3 riastrad * ->destroy callback. It can also be used to clean up driver private
573 1.3 riastrad * framebuffers embedded into a larger structure.
574 1.3 riastrad *
575 1.3 riastrad * Note that this function does not remove the fb from active usuage - if it is
576 1.3 riastrad * still used anywhere, hilarity can ensue since userspace could call getfb on
577 1.3 riastrad * the id and get back -EINVAL. Obviously no concern at driver unload time.
578 1.3 riastrad *
579 1.3 riastrad * Also, the framebuffer will not be removed from the lookup idr - for
580 1.3 riastrad * user-created framebuffers this will happen in in the rmfb ioctl. For
581 1.3 riastrad * driver-private objects (e.g. for fbdev) drivers need to explicitly call
582 1.3 riastrad * drm_framebuffer_unregister_private.
583 1.1 riastrad */
584 1.1 riastrad void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
585 1.1 riastrad {
586 1.1 riastrad struct drm_device *dev = fb->dev;
587 1.3 riastrad
588 1.3 riastrad mutex_lock(&dev->mode_config.fb_lock);
589 1.1 riastrad list_del(&fb->head);
590 1.1 riastrad dev->mode_config.num_fb--;
591 1.3 riastrad mutex_unlock(&dev->mode_config.fb_lock);
592 1.1 riastrad }
593 1.1 riastrad EXPORT_SYMBOL(drm_framebuffer_cleanup);
594 1.1 riastrad
595 1.1 riastrad /**
596 1.1 riastrad * drm_framebuffer_remove - remove and unreference a framebuffer object
597 1.1 riastrad * @fb: framebuffer to remove
598 1.1 riastrad *
599 1.3 riastrad * Scans all the CRTCs and planes in @dev's mode_config. If they're
600 1.3 riastrad * using @fb, removes it, setting it to NULL. Then drops the reference to the
601 1.3 riastrad * passed-in framebuffer. Might take the modeset locks.
602 1.1 riastrad *
603 1.3 riastrad * Note that this function optimizes the cleanup away if the caller holds the
604 1.3 riastrad * last reference to the framebuffer. It is also guaranteed to not take the
605 1.3 riastrad * modeset locks in this case.
606 1.1 riastrad */
607 1.1 riastrad void drm_framebuffer_remove(struct drm_framebuffer *fb)
608 1.1 riastrad {
609 1.5 riastrad struct drm_device *dev;
610 1.1 riastrad struct drm_crtc *crtc;
611 1.1 riastrad struct drm_plane *plane;
612 1.1 riastrad struct drm_mode_set set;
613 1.1 riastrad int ret;
614 1.1 riastrad
615 1.5 riastrad if (!fb)
616 1.5 riastrad return;
617 1.5 riastrad
618 1.5 riastrad dev = fb->dev;
619 1.5 riastrad
620 1.3 riastrad WARN_ON(!list_empty(&fb->filp_head));
621 1.3 riastrad
622 1.3 riastrad /*
623 1.3 riastrad * drm ABI mandates that we remove any deleted framebuffers from active
624 1.3 riastrad * useage. But since most sane clients only remove framebuffers they no
625 1.3 riastrad * longer need, try to optimize this away.
626 1.3 riastrad *
627 1.3 riastrad * Since we're holding a reference ourselves, observing a refcount of 1
628 1.3 riastrad * means that we're the last holder and can skip it. Also, the refcount
629 1.3 riastrad * can never increase from 1 again, so we don't need any barriers or
630 1.3 riastrad * locks.
631 1.3 riastrad *
632 1.3 riastrad * Note that userspace could try to race with use and instate a new
633 1.3 riastrad * usage _after_ we've cleared all current ones. End result will be an
634 1.3 riastrad * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
635 1.3 riastrad * in this manner.
636 1.3 riastrad */
637 1.4 riastrad if (!kref_exclusive_p(&fb->refcount)) {
638 1.3 riastrad drm_modeset_lock_all(dev);
639 1.3 riastrad /* remove from any CRTC */
640 1.5 riastrad drm_for_each_crtc(crtc, dev) {
641 1.3 riastrad if (crtc->primary->fb == fb) {
642 1.3 riastrad /* should turn off the crtc */
643 1.3 riastrad memset(&set, 0, sizeof(struct drm_mode_set));
644 1.3 riastrad set.crtc = crtc;
645 1.3 riastrad set.fb = NULL;
646 1.3 riastrad ret = drm_mode_set_config_internal(&set);
647 1.3 riastrad if (ret)
648 1.3 riastrad DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
649 1.3 riastrad }
650 1.1 riastrad }
651 1.1 riastrad
652 1.5 riastrad drm_for_each_plane(plane, dev) {
653 1.3 riastrad if (plane->fb == fb)
654 1.3 riastrad drm_plane_force_disable(plane);
655 1.1 riastrad }
656 1.3 riastrad drm_modeset_unlock_all(dev);
657 1.1 riastrad }
658 1.1 riastrad
659 1.1 riastrad drm_framebuffer_unreference(fb);
660 1.1 riastrad }
661 1.1 riastrad EXPORT_SYMBOL(drm_framebuffer_remove);
662 1.1 riastrad
663 1.5 riastrad DEFINE_WW_CLASS(crtc_ww_class);
664 1.5 riastrad
665 1.1 riastrad /**
666 1.3 riastrad * drm_crtc_init_with_planes - Initialise a new CRTC object with
667 1.3 riastrad * specified primary and cursor planes.
668 1.1 riastrad * @dev: DRM device
669 1.1 riastrad * @crtc: CRTC object to init
670 1.3 riastrad * @primary: Primary plane for CRTC
671 1.3 riastrad * @cursor: Cursor plane for CRTC
672 1.1 riastrad * @funcs: callbacks for the new CRTC
673 1.1 riastrad *
674 1.3 riastrad * Inits a new object created as base part of a driver crtc object.
675 1.1 riastrad *
676 1.3 riastrad * Returns:
677 1.1 riastrad * Zero on success, error code on failure.
678 1.1 riastrad */
679 1.3 riastrad int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
680 1.3 riastrad struct drm_plane *primary,
681 1.5 riastrad struct drm_plane *cursor,
682 1.3 riastrad const struct drm_crtc_funcs *funcs)
683 1.1 riastrad {
684 1.5 riastrad struct drm_mode_config *config = &dev->mode_config;
685 1.1 riastrad int ret;
686 1.1 riastrad
687 1.5 riastrad WARN_ON(primary && primary->type != DRM_PLANE_TYPE_PRIMARY);
688 1.5 riastrad WARN_ON(cursor && cursor->type != DRM_PLANE_TYPE_CURSOR);
689 1.5 riastrad
690 1.1 riastrad crtc->dev = dev;
691 1.1 riastrad crtc->funcs = funcs;
692 1.1 riastrad
693 1.5 riastrad drm_modeset_lock_init(&crtc->mutex);
694 1.1 riastrad ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
695 1.1 riastrad if (ret)
696 1.5 riastrad return ret;
697 1.1 riastrad
698 1.1 riastrad crtc->base.properties = &crtc->properties;
699 1.1 riastrad
700 1.5 riastrad list_add_tail(&crtc->head, &config->crtc_list);
701 1.5 riastrad config->num_crtc++;
702 1.1 riastrad
703 1.3 riastrad crtc->primary = primary;
704 1.5 riastrad crtc->cursor = cursor;
705 1.3 riastrad if (primary)
706 1.3 riastrad primary->possible_crtcs = 1 << drm_crtc_index(crtc);
707 1.5 riastrad if (cursor)
708 1.5 riastrad cursor->possible_crtcs = 1 << drm_crtc_index(crtc);
709 1.3 riastrad
710 1.5 riastrad if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
711 1.5 riastrad drm_object_attach_property(&crtc->base, config->prop_active, 0);
712 1.5 riastrad drm_object_attach_property(&crtc->base, config->prop_mode_id, 0);
713 1.5 riastrad }
714 1.1 riastrad
715 1.5 riastrad return 0;
716 1.1 riastrad }
717 1.3 riastrad EXPORT_SYMBOL(drm_crtc_init_with_planes);
718 1.1 riastrad
719 1.1 riastrad /**
720 1.3 riastrad * drm_crtc_cleanup - Clean up the core crtc usage
721 1.1 riastrad * @crtc: CRTC to cleanup
722 1.1 riastrad *
723 1.3 riastrad * This function cleans up @crtc and removes it from the DRM mode setting
724 1.3 riastrad * core. Note that the function does *not* free the crtc structure itself,
725 1.3 riastrad * this is the responsibility of the caller.
726 1.1 riastrad */
727 1.1 riastrad void drm_crtc_cleanup(struct drm_crtc *crtc)
728 1.1 riastrad {
729 1.1 riastrad struct drm_device *dev = crtc->dev;
730 1.1 riastrad
731 1.1 riastrad kfree(crtc->gamma_store);
732 1.1 riastrad crtc->gamma_store = NULL;
733 1.1 riastrad
734 1.5 riastrad drm_modeset_lock_fini(&crtc->mutex);
735 1.5 riastrad
736 1.1 riastrad drm_mode_object_put(dev, &crtc->base);
737 1.1 riastrad list_del(&crtc->head);
738 1.1 riastrad dev->mode_config.num_crtc--;
739 1.4 riastrad
740 1.5 riastrad WARN_ON(crtc->state && !crtc->funcs->atomic_destroy_state);
741 1.5 riastrad if (crtc->state && crtc->funcs->atomic_destroy_state)
742 1.5 riastrad crtc->funcs->atomic_destroy_state(crtc, crtc->state);
743 1.5 riastrad
744 1.5 riastrad memset(crtc, 0, sizeof(*crtc));
745 1.1 riastrad }
746 1.1 riastrad EXPORT_SYMBOL(drm_crtc_cleanup);
747 1.1 riastrad
748 1.1 riastrad /**
749 1.3 riastrad * drm_crtc_index - find the index of a registered CRTC
750 1.3 riastrad * @crtc: CRTC to find index for
751 1.1 riastrad *
752 1.3 riastrad * Given a registered CRTC, return the index of that CRTC within a DRM
753 1.3 riastrad * device's list of CRTCs.
754 1.1 riastrad */
755 1.3 riastrad unsigned int drm_crtc_index(struct drm_crtc *crtc)
756 1.1 riastrad {
757 1.3 riastrad unsigned int index = 0;
758 1.3 riastrad struct drm_crtc *tmp;
759 1.3 riastrad
760 1.5 riastrad drm_for_each_crtc(tmp, crtc->dev) {
761 1.3 riastrad if (tmp == crtc)
762 1.3 riastrad return index;
763 1.3 riastrad
764 1.3 riastrad index++;
765 1.3 riastrad }
766 1.3 riastrad
767 1.3 riastrad BUG();
768 1.1 riastrad }
769 1.3 riastrad EXPORT_SYMBOL(drm_crtc_index);
770 1.1 riastrad
771 1.3 riastrad /*
772 1.1 riastrad * drm_mode_remove - remove and free a mode
773 1.1 riastrad * @connector: connector list to modify
774 1.1 riastrad * @mode: mode to remove
775 1.1 riastrad *
776 1.1 riastrad * Remove @mode from @connector's mode list, then free it.
777 1.1 riastrad */
778 1.3 riastrad static void drm_mode_remove(struct drm_connector *connector,
779 1.3 riastrad struct drm_display_mode *mode)
780 1.1 riastrad {
781 1.1 riastrad list_del(&mode->head);
782 1.1 riastrad drm_mode_destroy(connector->dev, mode);
783 1.1 riastrad }
784 1.1 riastrad
785 1.1 riastrad /**
786 1.5 riastrad * drm_display_info_set_bus_formats - set the supported bus formats
787 1.5 riastrad * @info: display info to store bus formats in
788 1.5 riastrad * @formats: array containing the supported bus formats
789 1.5 riastrad * @num_formats: the number of entries in the fmts array
790 1.5 riastrad *
791 1.5 riastrad * Store the supported bus formats in display info structure.
792 1.5 riastrad * See MEDIA_BUS_FMT_* definitions in include/uapi/linux/media-bus-format.h for
793 1.5 riastrad * a full list of available formats.
794 1.5 riastrad */
795 1.5 riastrad int drm_display_info_set_bus_formats(struct drm_display_info *info,
796 1.5 riastrad const u32 *formats,
797 1.5 riastrad unsigned int num_formats)
798 1.5 riastrad {
799 1.5 riastrad u32 *fmts = NULL;
800 1.5 riastrad
801 1.5 riastrad if (!formats && num_formats)
802 1.5 riastrad return -EINVAL;
803 1.5 riastrad
804 1.5 riastrad if (formats && num_formats) {
805 1.5 riastrad fmts = kmemdup(formats, sizeof(*formats) * num_formats,
806 1.5 riastrad GFP_KERNEL);
807 1.5 riastrad if (!fmts)
808 1.5 riastrad return -ENOMEM;
809 1.5 riastrad }
810 1.5 riastrad
811 1.5 riastrad kfree(info->bus_formats);
812 1.5 riastrad info->bus_formats = fmts;
813 1.5 riastrad info->num_bus_formats = num_formats;
814 1.5 riastrad
815 1.5 riastrad return 0;
816 1.5 riastrad }
817 1.5 riastrad EXPORT_SYMBOL(drm_display_info_set_bus_formats);
818 1.5 riastrad
819 1.5 riastrad /**
820 1.5 riastrad * drm_connector_get_cmdline_mode - reads the user's cmdline mode
821 1.5 riastrad * @connector: connector to quwery
822 1.5 riastrad *
823 1.5 riastrad * The kernel supports per-connector configration of its consoles through
824 1.5 riastrad * use of the video= parameter. This function parses that option and
825 1.5 riastrad * extracts the user's specified mode (or enable/disable status) for a
826 1.5 riastrad * particular connector. This is typically only used during the early fbdev
827 1.5 riastrad * setup.
828 1.5 riastrad */
829 1.5 riastrad static void drm_connector_get_cmdline_mode(struct drm_connector *connector)
830 1.5 riastrad {
831 1.5 riastrad struct drm_cmdline_mode *mode = &connector->cmdline_mode;
832 1.5 riastrad char *option = NULL;
833 1.5 riastrad
834 1.5 riastrad #ifdef __NetBSD__
835 1.5 riastrad prop_dictionary_t prop = device_properties(connector->dev->dev);
836 1.5 riastrad if (!prop_dictionary_get_cstring(prop, connector->name, &option))
837 1.5 riastrad return;
838 1.5 riastrad #else
839 1.5 riastrad if (fb_get_options(connector->name, &option))
840 1.5 riastrad return;
841 1.5 riastrad #endif
842 1.5 riastrad
843 1.5 riastrad if (!drm_mode_parse_command_line_for_connector(option,
844 1.5 riastrad connector,
845 1.5 riastrad mode))
846 1.5 riastrad return;
847 1.5 riastrad
848 1.5 riastrad if (mode->force) {
849 1.5 riastrad const char *s;
850 1.5 riastrad
851 1.5 riastrad switch (mode->force) {
852 1.5 riastrad case DRM_FORCE_OFF:
853 1.5 riastrad s = "OFF";
854 1.5 riastrad break;
855 1.5 riastrad case DRM_FORCE_ON_DIGITAL:
856 1.5 riastrad s = "ON - dig";
857 1.5 riastrad break;
858 1.5 riastrad default:
859 1.5 riastrad case DRM_FORCE_ON:
860 1.5 riastrad s = "ON";
861 1.5 riastrad break;
862 1.5 riastrad }
863 1.5 riastrad
864 1.5 riastrad DRM_INFO("forcing %s connector %s\n", connector->name, s);
865 1.5 riastrad connector->force = mode->force;
866 1.5 riastrad }
867 1.5 riastrad
868 1.5 riastrad DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
869 1.5 riastrad connector->name,
870 1.5 riastrad mode->xres, mode->yres,
871 1.5 riastrad mode->refresh_specified ? mode->refresh : 60,
872 1.5 riastrad mode->rb ? " reduced blanking" : "",
873 1.5 riastrad mode->margins ? " with margins" : "",
874 1.5 riastrad mode->interlace ? " interlaced" : "");
875 1.5 riastrad }
876 1.5 riastrad
877 1.5 riastrad /**
878 1.1 riastrad * drm_connector_init - Init a preallocated connector
879 1.1 riastrad * @dev: DRM device
880 1.1 riastrad * @connector: the connector to init
881 1.1 riastrad * @funcs: callbacks for this connector
882 1.3 riastrad * @connector_type: user visible type of the connector
883 1.1 riastrad *
884 1.1 riastrad * Initialises a preallocated connector. Connectors should be
885 1.1 riastrad * subclassed as part of driver connector objects.
886 1.1 riastrad *
887 1.3 riastrad * Returns:
888 1.1 riastrad * Zero on success, error code on failure.
889 1.1 riastrad */
890 1.1 riastrad int drm_connector_init(struct drm_device *dev,
891 1.1 riastrad struct drm_connector *connector,
892 1.1 riastrad const struct drm_connector_funcs *funcs,
893 1.1 riastrad int connector_type)
894 1.1 riastrad {
895 1.5 riastrad struct drm_mode_config *config = &dev->mode_config;
896 1.1 riastrad int ret;
897 1.3 riastrad struct ida *connector_ida =
898 1.3 riastrad &drm_connector_enum_list[connector_type].ida;
899 1.1 riastrad
900 1.3 riastrad drm_modeset_lock_all(dev);
901 1.1 riastrad
902 1.5 riastrad ret = drm_mode_object_get_reg(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR, false);
903 1.1 riastrad if (ret)
904 1.5 riastrad goto out_unlock;
905 1.1 riastrad
906 1.1 riastrad connector->base.properties = &connector->properties;
907 1.1 riastrad connector->dev = dev;
908 1.10 riastrad connector->kdev = dev->dev;
909 1.1 riastrad connector->funcs = funcs;
910 1.1 riastrad connector->connector_type = connector_type;
911 1.1 riastrad connector->connector_type_id =
912 1.3 riastrad ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
913 1.3 riastrad if (connector->connector_type_id < 0) {
914 1.3 riastrad ret = connector->connector_type_id;
915 1.5 riastrad goto out_put;
916 1.5 riastrad }
917 1.5 riastrad connector->name =
918 1.5 riastrad kasprintf(GFP_KERNEL, "%s-%d",
919 1.5 riastrad drm_connector_enum_list[connector_type].name,
920 1.5 riastrad connector->connector_type_id);
921 1.5 riastrad if (!connector->name) {
922 1.5 riastrad ret = -ENOMEM;
923 1.5 riastrad goto out_put;
924 1.3 riastrad }
925 1.5 riastrad
926 1.1 riastrad INIT_LIST_HEAD(&connector->probed_modes);
927 1.1 riastrad INIT_LIST_HEAD(&connector->modes);
928 1.1 riastrad connector->edid_blob_ptr = NULL;
929 1.1 riastrad connector->status = connector_status_unknown;
930 1.1 riastrad
931 1.5 riastrad drm_connector_get_cmdline_mode(connector);
932 1.5 riastrad
933 1.5 riastrad /* We should add connectors at the end to avoid upsetting the connector
934 1.5 riastrad * index too much. */
935 1.5 riastrad list_add_tail(&connector->head, &config->connector_list);
936 1.5 riastrad config->num_connector++;
937 1.1 riastrad
938 1.1 riastrad if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
939 1.1 riastrad drm_object_attach_property(&connector->base,
940 1.5 riastrad config->edid_property,
941 1.1 riastrad 0);
942 1.1 riastrad
943 1.1 riastrad drm_object_attach_property(&connector->base,
944 1.5 riastrad config->dpms_property, 0);
945 1.5 riastrad
946 1.5 riastrad if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
947 1.5 riastrad drm_object_attach_property(&connector->base, config->prop_crtc_id, 0);
948 1.5 riastrad }
949 1.5 riastrad
950 1.5 riastrad connector->debugfs_entry = NULL;
951 1.5 riastrad
952 1.5 riastrad out_put:
953 1.5 riastrad if (ret)
954 1.5 riastrad drm_mode_object_put(dev, &connector->base);
955 1.1 riastrad
956 1.5 riastrad out_unlock:
957 1.3 riastrad drm_modeset_unlock_all(dev);
958 1.1 riastrad
959 1.1 riastrad return ret;
960 1.1 riastrad }
961 1.1 riastrad EXPORT_SYMBOL(drm_connector_init);
962 1.1 riastrad
963 1.1 riastrad /**
964 1.1 riastrad * drm_connector_cleanup - cleans up an initialised connector
965 1.1 riastrad * @connector: connector to cleanup
966 1.1 riastrad *
967 1.1 riastrad * Cleans up the connector but doesn't free the object.
968 1.1 riastrad */
969 1.1 riastrad void drm_connector_cleanup(struct drm_connector *connector)
970 1.1 riastrad {
971 1.1 riastrad struct drm_device *dev = connector->dev;
972 1.1 riastrad struct drm_display_mode *mode, *t;
973 1.1 riastrad
974 1.5 riastrad if (connector->tile_group) {
975 1.5 riastrad drm_mode_put_tile_group(dev, connector->tile_group);
976 1.5 riastrad connector->tile_group = NULL;
977 1.5 riastrad }
978 1.5 riastrad
979 1.1 riastrad list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
980 1.1 riastrad drm_mode_remove(connector, mode);
981 1.1 riastrad
982 1.1 riastrad list_for_each_entry_safe(mode, t, &connector->modes, head)
983 1.1 riastrad drm_mode_remove(connector, mode);
984 1.1 riastrad
985 1.3 riastrad ida_remove(&drm_connector_enum_list[connector->connector_type].ida,
986 1.3 riastrad connector->connector_type_id);
987 1.1 riastrad
988 1.5 riastrad kfree(connector->display_info.bus_formats);
989 1.1 riastrad drm_mode_object_put(dev, &connector->base);
990 1.5 riastrad kfree(connector->name);
991 1.5 riastrad connector->name = NULL;
992 1.1 riastrad list_del(&connector->head);
993 1.1 riastrad dev->mode_config.num_connector--;
994 1.5 riastrad
995 1.5 riastrad WARN_ON(connector->state && !connector->funcs->atomic_destroy_state);
996 1.5 riastrad if (connector->state && connector->funcs->atomic_destroy_state)
997 1.5 riastrad connector->funcs->atomic_destroy_state(connector,
998 1.5 riastrad connector->state);
999 1.5 riastrad
1000 1.5 riastrad memset(connector, 0, sizeof(*connector));
1001 1.1 riastrad }
1002 1.1 riastrad EXPORT_SYMBOL(drm_connector_cleanup);
1003 1.1 riastrad
1004 1.3 riastrad /**
1005 1.5 riastrad * drm_connector_index - find the index of a registered connector
1006 1.5 riastrad * @connector: connector to find index for
1007 1.3 riastrad *
1008 1.5 riastrad * Given a registered connector, return the index of that connector within a DRM
1009 1.5 riastrad * device's list of connectors.
1010 1.3 riastrad */
1011 1.5 riastrad unsigned int drm_connector_index(struct drm_connector *connector)
1012 1.1 riastrad {
1013 1.5 riastrad unsigned int index = 0;
1014 1.5 riastrad struct drm_connector *tmp;
1015 1.5 riastrad struct drm_mode_config *config = &connector->dev->mode_config;
1016 1.5 riastrad
1017 1.5 riastrad WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
1018 1.5 riastrad
1019 1.5 riastrad drm_for_each_connector(tmp, connector->dev) {
1020 1.5 riastrad if (tmp == connector)
1021 1.5 riastrad return index;
1022 1.1 riastrad
1023 1.5 riastrad index++;
1024 1.5 riastrad }
1025 1.1 riastrad
1026 1.5 riastrad BUG();
1027 1.1 riastrad }
1028 1.5 riastrad EXPORT_SYMBOL(drm_connector_index);
1029 1.1 riastrad
1030 1.3 riastrad /**
1031 1.5 riastrad * drm_connector_register - register a connector
1032 1.5 riastrad * @connector: the connector to register
1033 1.3 riastrad *
1034 1.5 riastrad * Register userspace interfaces for a connector
1035 1.3 riastrad *
1036 1.3 riastrad * Returns:
1037 1.3 riastrad * Zero on success, error code on failure.
1038 1.3 riastrad */
1039 1.5 riastrad int drm_connector_register(struct drm_connector *connector)
1040 1.3 riastrad {
1041 1.8 riastrad #ifndef __NetBSD__ /* XXX sysfs, debugfs */
1042 1.3 riastrad int ret;
1043 1.8 riastrad #endif
1044 1.3 riastrad
1045 1.5 riastrad drm_mode_object_register(connector->dev, &connector->base);
1046 1.3 riastrad
1047 1.5 riastrad #ifndef __NetBSD__ /* XXX sysfs, debugfs */
1048 1.5 riastrad ret = drm_sysfs_connector_add(connector);
1049 1.3 riastrad if (ret)
1050 1.5 riastrad return ret;
1051 1.3 riastrad
1052 1.5 riastrad ret = drm_debugfs_connector_add(connector);
1053 1.5 riastrad if (ret) {
1054 1.5 riastrad drm_sysfs_connector_remove(connector);
1055 1.5 riastrad return ret;
1056 1.5 riastrad }
1057 1.5 riastrad #endif
1058 1.3 riastrad
1059 1.5 riastrad return 0;
1060 1.5 riastrad }
1061 1.5 riastrad EXPORT_SYMBOL(drm_connector_register);
1062 1.3 riastrad
1063 1.5 riastrad /**
1064 1.5 riastrad * drm_connector_unregister - unregister a connector
1065 1.5 riastrad * @connector: the connector to unregister
1066 1.5 riastrad *
1067 1.5 riastrad * Unregister userspace interfaces for a connector
1068 1.5 riastrad */
1069 1.5 riastrad void drm_connector_unregister(struct drm_connector *connector)
1070 1.5 riastrad {
1071 1.5 riastrad #ifndef __NetBSD__ /* XXX sysfs, debugfs */
1072 1.5 riastrad drm_sysfs_connector_remove(connector);
1073 1.5 riastrad drm_debugfs_connector_remove(connector);
1074 1.5 riastrad #endif
1075 1.3 riastrad }
1076 1.5 riastrad EXPORT_SYMBOL(drm_connector_unregister);
1077 1.5 riastrad
1078 1.3 riastrad
1079 1.3 riastrad /**
1080 1.5 riastrad * drm_connector_unplug_all - unregister connector userspace interfaces
1081 1.5 riastrad * @dev: drm device
1082 1.3 riastrad *
1083 1.5 riastrad * This function unregisters all connector userspace interfaces in sysfs. Should
1084 1.5 riastrad * be call when the device is disconnected, e.g. from an usb driver's
1085 1.5 riastrad * ->disconnect callback.
1086 1.3 riastrad */
1087 1.5 riastrad void drm_connector_unplug_all(struct drm_device *dev)
1088 1.3 riastrad {
1089 1.5 riastrad struct drm_connector *connector;
1090 1.5 riastrad
1091 1.5 riastrad /* FIXME: taking the mode config mutex ends up in a clash with sysfs */
1092 1.5 riastrad list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1093 1.5 riastrad drm_connector_unregister(connector);
1094 1.3 riastrad
1095 1.3 riastrad }
1096 1.5 riastrad EXPORT_SYMBOL(drm_connector_unplug_all);
1097 1.3 riastrad
1098 1.3 riastrad /**
1099 1.3 riastrad * drm_encoder_init - Init a preallocated encoder
1100 1.3 riastrad * @dev: drm device
1101 1.3 riastrad * @encoder: the encoder to init
1102 1.3 riastrad * @funcs: callbacks for this encoder
1103 1.3 riastrad * @encoder_type: user visible type of the encoder
1104 1.3 riastrad *
1105 1.3 riastrad * Initialises a preallocated encoder. Encoder should be
1106 1.3 riastrad * subclassed as part of driver encoder objects.
1107 1.3 riastrad *
1108 1.3 riastrad * Returns:
1109 1.3 riastrad * Zero on success, error code on failure.
1110 1.3 riastrad */
1111 1.1 riastrad int drm_encoder_init(struct drm_device *dev,
1112 1.1 riastrad struct drm_encoder *encoder,
1113 1.1 riastrad const struct drm_encoder_funcs *funcs,
1114 1.1 riastrad int encoder_type)
1115 1.1 riastrad {
1116 1.1 riastrad int ret;
1117 1.1 riastrad
1118 1.3 riastrad drm_modeset_lock_all(dev);
1119 1.1 riastrad
1120 1.1 riastrad ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
1121 1.1 riastrad if (ret)
1122 1.5 riastrad goto out_unlock;
1123 1.1 riastrad
1124 1.1 riastrad encoder->dev = dev;
1125 1.1 riastrad encoder->encoder_type = encoder_type;
1126 1.1 riastrad encoder->funcs = funcs;
1127 1.5 riastrad encoder->name = kasprintf(GFP_KERNEL, "%s-%d",
1128 1.5 riastrad drm_encoder_enum_list[encoder_type].name,
1129 1.5 riastrad encoder->base.id);
1130 1.5 riastrad if (!encoder->name) {
1131 1.5 riastrad ret = -ENOMEM;
1132 1.5 riastrad goto out_put;
1133 1.5 riastrad }
1134 1.1 riastrad
1135 1.1 riastrad list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
1136 1.1 riastrad dev->mode_config.num_encoder++;
1137 1.1 riastrad
1138 1.5 riastrad out_put:
1139 1.5 riastrad if (ret)
1140 1.5 riastrad drm_mode_object_put(dev, &encoder->base);
1141 1.5 riastrad
1142 1.5 riastrad out_unlock:
1143 1.3 riastrad drm_modeset_unlock_all(dev);
1144 1.1 riastrad
1145 1.1 riastrad return ret;
1146 1.1 riastrad }
1147 1.1 riastrad EXPORT_SYMBOL(drm_encoder_init);
1148 1.1 riastrad
1149 1.3 riastrad /**
1150 1.3 riastrad * drm_encoder_cleanup - cleans up an initialised encoder
1151 1.3 riastrad * @encoder: encoder to cleanup
1152 1.3 riastrad *
1153 1.3 riastrad * Cleans up the encoder but doesn't free the object.
1154 1.3 riastrad */
1155 1.1 riastrad void drm_encoder_cleanup(struct drm_encoder *encoder)
1156 1.1 riastrad {
1157 1.1 riastrad struct drm_device *dev = encoder->dev;
1158 1.5 riastrad
1159 1.3 riastrad drm_modeset_lock_all(dev);
1160 1.1 riastrad drm_mode_object_put(dev, &encoder->base);
1161 1.5 riastrad kfree(encoder->name);
1162 1.1 riastrad list_del(&encoder->head);
1163 1.1 riastrad dev->mode_config.num_encoder--;
1164 1.3 riastrad drm_modeset_unlock_all(dev);
1165 1.5 riastrad
1166 1.5 riastrad memset(encoder, 0, sizeof(*encoder));
1167 1.1 riastrad }
1168 1.1 riastrad EXPORT_SYMBOL(drm_encoder_cleanup);
1169 1.1 riastrad
1170 1.3 riastrad /**
1171 1.3 riastrad * drm_universal_plane_init - Initialize a new universal plane object
1172 1.3 riastrad * @dev: DRM device
1173 1.3 riastrad * @plane: plane object to init
1174 1.3 riastrad * @possible_crtcs: bitmask of possible CRTCs
1175 1.3 riastrad * @funcs: callbacks for the new plane
1176 1.3 riastrad * @formats: array of supported formats (%DRM_FORMAT_*)
1177 1.3 riastrad * @format_count: number of elements in @formats
1178 1.3 riastrad * @type: type of plane (overlay, primary, cursor)
1179 1.3 riastrad *
1180 1.3 riastrad * Initializes a plane object of type @type.
1181 1.3 riastrad *
1182 1.3 riastrad * Returns:
1183 1.3 riastrad * Zero on success, error code on failure.
1184 1.3 riastrad */
1185 1.3 riastrad int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane,
1186 1.3 riastrad unsigned long possible_crtcs,
1187 1.3 riastrad const struct drm_plane_funcs *funcs,
1188 1.5 riastrad const uint32_t *formats, unsigned int format_count,
1189 1.3 riastrad enum drm_plane_type type)
1190 1.1 riastrad {
1191 1.5 riastrad struct drm_mode_config *config = &dev->mode_config;
1192 1.1 riastrad int ret;
1193 1.1 riastrad
1194 1.1 riastrad ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
1195 1.1 riastrad if (ret)
1196 1.5 riastrad return ret;
1197 1.5 riastrad
1198 1.5 riastrad drm_modeset_lock_init(&plane->mutex);
1199 1.1 riastrad
1200 1.1 riastrad plane->base.properties = &plane->properties;
1201 1.1 riastrad plane->dev = dev;
1202 1.1 riastrad plane->funcs = funcs;
1203 1.5 riastrad plane->format_types = kmalloc_array(format_count, sizeof(uint32_t),
1204 1.5 riastrad GFP_KERNEL);
1205 1.1 riastrad if (!plane->format_types) {
1206 1.1 riastrad DRM_DEBUG_KMS("out of memory when allocating plane\n");
1207 1.1 riastrad drm_mode_object_put(dev, &plane->base);
1208 1.5 riastrad return -ENOMEM;
1209 1.1 riastrad }
1210 1.1 riastrad
1211 1.1 riastrad memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
1212 1.1 riastrad plane->format_count = format_count;
1213 1.1 riastrad plane->possible_crtcs = possible_crtcs;
1214 1.3 riastrad plane->type = type;
1215 1.1 riastrad
1216 1.5 riastrad list_add_tail(&plane->head, &config->plane_list);
1217 1.5 riastrad config->num_total_plane++;
1218 1.3 riastrad if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1219 1.5 riastrad config->num_overlay_plane++;
1220 1.3 riastrad
1221 1.3 riastrad drm_object_attach_property(&plane->base,
1222 1.5 riastrad config->plane_type_property,
1223 1.3 riastrad plane->type);
1224 1.1 riastrad
1225 1.5 riastrad if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
1226 1.5 riastrad drm_object_attach_property(&plane->base, config->prop_fb_id, 0);
1227 1.5 riastrad drm_object_attach_property(&plane->base, config->prop_crtc_id, 0);
1228 1.5 riastrad drm_object_attach_property(&plane->base, config->prop_crtc_x, 0);
1229 1.5 riastrad drm_object_attach_property(&plane->base, config->prop_crtc_y, 0);
1230 1.5 riastrad drm_object_attach_property(&plane->base, config->prop_crtc_w, 0);
1231 1.5 riastrad drm_object_attach_property(&plane->base, config->prop_crtc_h, 0);
1232 1.5 riastrad drm_object_attach_property(&plane->base, config->prop_src_x, 0);
1233 1.5 riastrad drm_object_attach_property(&plane->base, config->prop_src_y, 0);
1234 1.5 riastrad drm_object_attach_property(&plane->base, config->prop_src_w, 0);
1235 1.5 riastrad drm_object_attach_property(&plane->base, config->prop_src_h, 0);
1236 1.5 riastrad }
1237 1.1 riastrad
1238 1.5 riastrad return 0;
1239 1.1 riastrad }
1240 1.3 riastrad EXPORT_SYMBOL(drm_universal_plane_init);
1241 1.1 riastrad
1242 1.3 riastrad /**
1243 1.3 riastrad * drm_plane_init - Initialize a legacy plane
1244 1.3 riastrad * @dev: DRM device
1245 1.3 riastrad * @plane: plane object to init
1246 1.3 riastrad * @possible_crtcs: bitmask of possible CRTCs
1247 1.3 riastrad * @funcs: callbacks for the new plane
1248 1.3 riastrad * @formats: array of supported formats (%DRM_FORMAT_*)
1249 1.3 riastrad * @format_count: number of elements in @formats
1250 1.3 riastrad * @is_primary: plane type (primary vs overlay)
1251 1.3 riastrad *
1252 1.3 riastrad * Legacy API to initialize a DRM plane.
1253 1.3 riastrad *
1254 1.3 riastrad * New drivers should call drm_universal_plane_init() instead.
1255 1.3 riastrad *
1256 1.3 riastrad * Returns:
1257 1.3 riastrad * Zero on success, error code on failure.
1258 1.3 riastrad */
1259 1.3 riastrad int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
1260 1.3 riastrad unsigned long possible_crtcs,
1261 1.3 riastrad const struct drm_plane_funcs *funcs,
1262 1.5 riastrad const uint32_t *formats, unsigned int format_count,
1263 1.3 riastrad bool is_primary)
1264 1.1 riastrad {
1265 1.3 riastrad enum drm_plane_type type;
1266 1.1 riastrad
1267 1.3 riastrad type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
1268 1.3 riastrad return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
1269 1.3 riastrad formats, format_count, type);
1270 1.1 riastrad }
1271 1.3 riastrad EXPORT_SYMBOL(drm_plane_init);
1272 1.1 riastrad
1273 1.1 riastrad /**
1274 1.3 riastrad * drm_plane_cleanup - Clean up the core plane usage
1275 1.3 riastrad * @plane: plane to cleanup
1276 1.1 riastrad *
1277 1.3 riastrad * This function cleans up @plane and removes it from the DRM mode setting
1278 1.3 riastrad * core. Note that the function does *not* free the plane structure itself,
1279 1.3 riastrad * this is the responsibility of the caller.
1280 1.1 riastrad */
1281 1.3 riastrad void drm_plane_cleanup(struct drm_plane *plane)
1282 1.1 riastrad {
1283 1.3 riastrad struct drm_device *dev = plane->dev;
1284 1.1 riastrad
1285 1.3 riastrad drm_modeset_lock_all(dev);
1286 1.3 riastrad kfree(plane->format_types);
1287 1.3 riastrad drm_mode_object_put(dev, &plane->base);
1288 1.1 riastrad
1289 1.3 riastrad BUG_ON(list_empty(&plane->head));
1290 1.1 riastrad
1291 1.3 riastrad list_del(&plane->head);
1292 1.3 riastrad dev->mode_config.num_total_plane--;
1293 1.3 riastrad if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1294 1.3 riastrad dev->mode_config.num_overlay_plane--;
1295 1.3 riastrad drm_modeset_unlock_all(dev);
1296 1.5 riastrad
1297 1.5 riastrad WARN_ON(plane->state && !plane->funcs->atomic_destroy_state);
1298 1.5 riastrad if (plane->state && plane->funcs->atomic_destroy_state)
1299 1.5 riastrad plane->funcs->atomic_destroy_state(plane, plane->state);
1300 1.11 riastrad drm_modeset_lock_fini(&plane->mutex);
1301 1.5 riastrad
1302 1.5 riastrad memset(plane, 0, sizeof(*plane));
1303 1.1 riastrad }
1304 1.3 riastrad EXPORT_SYMBOL(drm_plane_cleanup);
1305 1.1 riastrad
1306 1.1 riastrad /**
1307 1.5 riastrad * drm_plane_index - find the index of a registered plane
1308 1.5 riastrad * @plane: plane to find index for
1309 1.5 riastrad *
1310 1.5 riastrad * Given a registered plane, return the index of that CRTC within a DRM
1311 1.5 riastrad * device's list of planes.
1312 1.5 riastrad */
1313 1.5 riastrad unsigned int drm_plane_index(struct drm_plane *plane)
1314 1.5 riastrad {
1315 1.5 riastrad unsigned int index = 0;
1316 1.5 riastrad struct drm_plane *tmp;
1317 1.5 riastrad
1318 1.5 riastrad drm_for_each_plane(tmp, plane->dev) {
1319 1.5 riastrad if (tmp == plane)
1320 1.5 riastrad return index;
1321 1.5 riastrad
1322 1.5 riastrad index++;
1323 1.5 riastrad }
1324 1.5 riastrad
1325 1.5 riastrad BUG();
1326 1.5 riastrad }
1327 1.5 riastrad EXPORT_SYMBOL(drm_plane_index);
1328 1.5 riastrad
1329 1.5 riastrad /**
1330 1.5 riastrad * drm_plane_from_index - find the registered plane at an index
1331 1.5 riastrad * @dev: DRM device
1332 1.5 riastrad * @idx: index of registered plane to find for
1333 1.5 riastrad *
1334 1.5 riastrad * Given a plane index, return the registered plane from DRM device's
1335 1.5 riastrad * list of planes with matching index.
1336 1.5 riastrad */
1337 1.5 riastrad struct drm_plane *
1338 1.5 riastrad drm_plane_from_index(struct drm_device *dev, int idx)
1339 1.5 riastrad {
1340 1.5 riastrad struct drm_plane *plane;
1341 1.5 riastrad unsigned int i = 0;
1342 1.5 riastrad
1343 1.5 riastrad drm_for_each_plane(plane, dev) {
1344 1.5 riastrad if (i == idx)
1345 1.5 riastrad return plane;
1346 1.5 riastrad i++;
1347 1.5 riastrad }
1348 1.5 riastrad return NULL;
1349 1.5 riastrad }
1350 1.5 riastrad EXPORT_SYMBOL(drm_plane_from_index);
1351 1.5 riastrad
1352 1.5 riastrad /**
1353 1.3 riastrad * drm_plane_force_disable - Forcibly disable a plane
1354 1.3 riastrad * @plane: plane to disable
1355 1.1 riastrad *
1356 1.3 riastrad * Forces the plane to be disabled.
1357 1.1 riastrad *
1358 1.3 riastrad * Used when the plane's current framebuffer is destroyed,
1359 1.3 riastrad * and when restoring fbdev mode.
1360 1.1 riastrad */
1361 1.3 riastrad void drm_plane_force_disable(struct drm_plane *plane)
1362 1.1 riastrad {
1363 1.3 riastrad int ret;
1364 1.3 riastrad
1365 1.3 riastrad if (!plane->fb)
1366 1.1 riastrad return;
1367 1.1 riastrad
1368 1.5 riastrad plane->old_fb = plane->fb;
1369 1.3 riastrad ret = plane->funcs->disable_plane(plane);
1370 1.5 riastrad if (ret) {
1371 1.3 riastrad DRM_ERROR("failed to disable plane with busy fb\n");
1372 1.5 riastrad plane->old_fb = NULL;
1373 1.5 riastrad return;
1374 1.5 riastrad }
1375 1.3 riastrad /* disconnect the plane from the fb and crtc: */
1376 1.5 riastrad drm_framebuffer_unreference(plane->old_fb);
1377 1.5 riastrad plane->old_fb = NULL;
1378 1.3 riastrad plane->fb = NULL;
1379 1.3 riastrad plane->crtc = NULL;
1380 1.1 riastrad }
1381 1.3 riastrad EXPORT_SYMBOL(drm_plane_force_disable);
1382 1.1 riastrad
1383 1.5 riastrad static int drm_mode_create_standard_properties(struct drm_device *dev)
1384 1.1 riastrad {
1385 1.5 riastrad struct drm_property *prop;
1386 1.1 riastrad
1387 1.1 riastrad /*
1388 1.1 riastrad * Standard properties (apply to all connectors)
1389 1.1 riastrad */
1390 1.5 riastrad prop = drm_property_create(dev, DRM_MODE_PROP_BLOB |
1391 1.1 riastrad DRM_MODE_PROP_IMMUTABLE,
1392 1.1 riastrad "EDID", 0);
1393 1.5 riastrad if (!prop)
1394 1.5 riastrad return -ENOMEM;
1395 1.5 riastrad dev->mode_config.edid_property = prop;
1396 1.1 riastrad
1397 1.5 riastrad prop = drm_property_create_enum(dev, 0,
1398 1.1 riastrad "DPMS", drm_dpms_enum_list,
1399 1.1 riastrad ARRAY_SIZE(drm_dpms_enum_list));
1400 1.5 riastrad if (!prop)
1401 1.5 riastrad return -ENOMEM;
1402 1.5 riastrad dev->mode_config.dpms_property = prop;
1403 1.1 riastrad
1404 1.5 riastrad prop = drm_property_create(dev,
1405 1.5 riastrad DRM_MODE_PROP_BLOB |
1406 1.5 riastrad DRM_MODE_PROP_IMMUTABLE,
1407 1.5 riastrad "PATH", 0);
1408 1.5 riastrad if (!prop)
1409 1.5 riastrad return -ENOMEM;
1410 1.5 riastrad dev->mode_config.path_property = prop;
1411 1.1 riastrad
1412 1.5 riastrad prop = drm_property_create(dev,
1413 1.5 riastrad DRM_MODE_PROP_BLOB |
1414 1.5 riastrad DRM_MODE_PROP_IMMUTABLE,
1415 1.5 riastrad "TILE", 0);
1416 1.5 riastrad if (!prop)
1417 1.5 riastrad return -ENOMEM;
1418 1.5 riastrad dev->mode_config.tile_property = prop;
1419 1.3 riastrad
1420 1.5 riastrad prop = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1421 1.3 riastrad "type", drm_plane_type_enum_list,
1422 1.3 riastrad ARRAY_SIZE(drm_plane_type_enum_list));
1423 1.5 riastrad if (!prop)
1424 1.5 riastrad return -ENOMEM;
1425 1.5 riastrad dev->mode_config.plane_type_property = prop;
1426 1.5 riastrad
1427 1.5 riastrad prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1428 1.5 riastrad "SRC_X", 0, UINT_MAX);
1429 1.5 riastrad if (!prop)
1430 1.5 riastrad return -ENOMEM;
1431 1.5 riastrad dev->mode_config.prop_src_x = prop;
1432 1.5 riastrad
1433 1.5 riastrad prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1434 1.5 riastrad "SRC_Y", 0, UINT_MAX);
1435 1.5 riastrad if (!prop)
1436 1.5 riastrad return -ENOMEM;
1437 1.5 riastrad dev->mode_config.prop_src_y = prop;
1438 1.5 riastrad
1439 1.5 riastrad prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1440 1.5 riastrad "SRC_W", 0, UINT_MAX);
1441 1.5 riastrad if (!prop)
1442 1.5 riastrad return -ENOMEM;
1443 1.5 riastrad dev->mode_config.prop_src_w = prop;
1444 1.5 riastrad
1445 1.5 riastrad prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1446 1.5 riastrad "SRC_H", 0, UINT_MAX);
1447 1.5 riastrad if (!prop)
1448 1.5 riastrad return -ENOMEM;
1449 1.5 riastrad dev->mode_config.prop_src_h = prop;
1450 1.5 riastrad
1451 1.5 riastrad prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
1452 1.5 riastrad "CRTC_X", INT_MIN, INT_MAX);
1453 1.5 riastrad if (!prop)
1454 1.5 riastrad return -ENOMEM;
1455 1.5 riastrad dev->mode_config.prop_crtc_x = prop;
1456 1.5 riastrad
1457 1.5 riastrad prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
1458 1.5 riastrad "CRTC_Y", INT_MIN, INT_MAX);
1459 1.5 riastrad if (!prop)
1460 1.5 riastrad return -ENOMEM;
1461 1.5 riastrad dev->mode_config.prop_crtc_y = prop;
1462 1.5 riastrad
1463 1.5 riastrad prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1464 1.5 riastrad "CRTC_W", 0, INT_MAX);
1465 1.5 riastrad if (!prop)
1466 1.5 riastrad return -ENOMEM;
1467 1.5 riastrad dev->mode_config.prop_crtc_w = prop;
1468 1.5 riastrad
1469 1.5 riastrad prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1470 1.5 riastrad "CRTC_H", 0, INT_MAX);
1471 1.5 riastrad if (!prop)
1472 1.5 riastrad return -ENOMEM;
1473 1.5 riastrad dev->mode_config.prop_crtc_h = prop;
1474 1.5 riastrad
1475 1.5 riastrad prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
1476 1.5 riastrad "FB_ID", DRM_MODE_OBJECT_FB);
1477 1.5 riastrad if (!prop)
1478 1.5 riastrad return -ENOMEM;
1479 1.5 riastrad dev->mode_config.prop_fb_id = prop;
1480 1.5 riastrad
1481 1.5 riastrad prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
1482 1.5 riastrad "CRTC_ID", DRM_MODE_OBJECT_CRTC);
1483 1.5 riastrad if (!prop)
1484 1.5 riastrad return -ENOMEM;
1485 1.5 riastrad dev->mode_config.prop_crtc_id = prop;
1486 1.5 riastrad
1487 1.5 riastrad prop = drm_property_create_bool(dev, DRM_MODE_PROP_ATOMIC,
1488 1.5 riastrad "ACTIVE");
1489 1.5 riastrad if (!prop)
1490 1.5 riastrad return -ENOMEM;
1491 1.5 riastrad dev->mode_config.prop_active = prop;
1492 1.5 riastrad
1493 1.5 riastrad prop = drm_property_create(dev,
1494 1.5 riastrad DRM_MODE_PROP_ATOMIC | DRM_MODE_PROP_BLOB,
1495 1.5 riastrad "MODE_ID", 0);
1496 1.5 riastrad if (!prop)
1497 1.5 riastrad return -ENOMEM;
1498 1.5 riastrad dev->mode_config.prop_mode_id = prop;
1499 1.3 riastrad
1500 1.3 riastrad return 0;
1501 1.3 riastrad }
1502 1.3 riastrad
1503 1.1 riastrad /**
1504 1.1 riastrad * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1505 1.1 riastrad * @dev: DRM device
1506 1.1 riastrad *
1507 1.1 riastrad * Called by a driver the first time a DVI-I connector is made.
1508 1.1 riastrad */
1509 1.1 riastrad int drm_mode_create_dvi_i_properties(struct drm_device *dev)
1510 1.1 riastrad {
1511 1.1 riastrad struct drm_property *dvi_i_selector;
1512 1.1 riastrad struct drm_property *dvi_i_subconnector;
1513 1.1 riastrad
1514 1.1 riastrad if (dev->mode_config.dvi_i_select_subconnector_property)
1515 1.1 riastrad return 0;
1516 1.1 riastrad
1517 1.1 riastrad dvi_i_selector =
1518 1.1 riastrad drm_property_create_enum(dev, 0,
1519 1.1 riastrad "select subconnector",
1520 1.1 riastrad drm_dvi_i_select_enum_list,
1521 1.1 riastrad ARRAY_SIZE(drm_dvi_i_select_enum_list));
1522 1.1 riastrad dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
1523 1.1 riastrad
1524 1.1 riastrad dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1525 1.1 riastrad "subconnector",
1526 1.1 riastrad drm_dvi_i_subconnector_enum_list,
1527 1.1 riastrad ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
1528 1.1 riastrad dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
1529 1.1 riastrad
1530 1.1 riastrad return 0;
1531 1.1 riastrad }
1532 1.1 riastrad EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
1533 1.1 riastrad
1534 1.1 riastrad /**
1535 1.1 riastrad * drm_create_tv_properties - create TV specific connector properties
1536 1.1 riastrad * @dev: DRM device
1537 1.1 riastrad * @num_modes: number of different TV formats (modes) supported
1538 1.1 riastrad * @modes: array of pointers to strings containing name of each format
1539 1.1 riastrad *
1540 1.1 riastrad * Called by a driver's TV initialization routine, this function creates
1541 1.1 riastrad * the TV specific connector properties for a given device. Caller is
1542 1.1 riastrad * responsible for allocating a list of format names and passing them to
1543 1.1 riastrad * this routine.
1544 1.1 riastrad */
1545 1.5 riastrad int drm_mode_create_tv_properties(struct drm_device *dev,
1546 1.5 riastrad unsigned int num_modes,
1547 1.5 riastrad const char * const modes[])
1548 1.1 riastrad {
1549 1.1 riastrad struct drm_property *tv_selector;
1550 1.1 riastrad struct drm_property *tv_subconnector;
1551 1.5 riastrad unsigned int i;
1552 1.1 riastrad
1553 1.1 riastrad if (dev->mode_config.tv_select_subconnector_property)
1554 1.1 riastrad return 0;
1555 1.1 riastrad
1556 1.1 riastrad /*
1557 1.1 riastrad * Basic connector properties
1558 1.1 riastrad */
1559 1.1 riastrad tv_selector = drm_property_create_enum(dev, 0,
1560 1.1 riastrad "select subconnector",
1561 1.1 riastrad drm_tv_select_enum_list,
1562 1.1 riastrad ARRAY_SIZE(drm_tv_select_enum_list));
1563 1.5 riastrad if (!tv_selector)
1564 1.5 riastrad goto nomem;
1565 1.5 riastrad
1566 1.1 riastrad dev->mode_config.tv_select_subconnector_property = tv_selector;
1567 1.1 riastrad
1568 1.1 riastrad tv_subconnector =
1569 1.1 riastrad drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1570 1.1 riastrad "subconnector",
1571 1.1 riastrad drm_tv_subconnector_enum_list,
1572 1.1 riastrad ARRAY_SIZE(drm_tv_subconnector_enum_list));
1573 1.5 riastrad if (!tv_subconnector)
1574 1.5 riastrad goto nomem;
1575 1.1 riastrad dev->mode_config.tv_subconnector_property = tv_subconnector;
1576 1.1 riastrad
1577 1.1 riastrad /*
1578 1.1 riastrad * Other, TV specific properties: margins & TV modes.
1579 1.1 riastrad */
1580 1.1 riastrad dev->mode_config.tv_left_margin_property =
1581 1.1 riastrad drm_property_create_range(dev, 0, "left margin", 0, 100);
1582 1.5 riastrad if (!dev->mode_config.tv_left_margin_property)
1583 1.5 riastrad goto nomem;
1584 1.1 riastrad
1585 1.1 riastrad dev->mode_config.tv_right_margin_property =
1586 1.1 riastrad drm_property_create_range(dev, 0, "right margin", 0, 100);
1587 1.5 riastrad if (!dev->mode_config.tv_right_margin_property)
1588 1.5 riastrad goto nomem;
1589 1.1 riastrad
1590 1.1 riastrad dev->mode_config.tv_top_margin_property =
1591 1.1 riastrad drm_property_create_range(dev, 0, "top margin", 0, 100);
1592 1.5 riastrad if (!dev->mode_config.tv_top_margin_property)
1593 1.5 riastrad goto nomem;
1594 1.1 riastrad
1595 1.1 riastrad dev->mode_config.tv_bottom_margin_property =
1596 1.1 riastrad drm_property_create_range(dev, 0, "bottom margin", 0, 100);
1597 1.5 riastrad if (!dev->mode_config.tv_bottom_margin_property)
1598 1.5 riastrad goto nomem;
1599 1.1 riastrad
1600 1.1 riastrad dev->mode_config.tv_mode_property =
1601 1.1 riastrad drm_property_create(dev, DRM_MODE_PROP_ENUM,
1602 1.1 riastrad "mode", num_modes);
1603 1.5 riastrad if (!dev->mode_config.tv_mode_property)
1604 1.5 riastrad goto nomem;
1605 1.5 riastrad
1606 1.1 riastrad for (i = 0; i < num_modes; i++)
1607 1.1 riastrad drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1608 1.1 riastrad i, modes[i]);
1609 1.1 riastrad
1610 1.1 riastrad dev->mode_config.tv_brightness_property =
1611 1.1 riastrad drm_property_create_range(dev, 0, "brightness", 0, 100);
1612 1.5 riastrad if (!dev->mode_config.tv_brightness_property)
1613 1.5 riastrad goto nomem;
1614 1.1 riastrad
1615 1.1 riastrad dev->mode_config.tv_contrast_property =
1616 1.1 riastrad drm_property_create_range(dev, 0, "contrast", 0, 100);
1617 1.5 riastrad if (!dev->mode_config.tv_contrast_property)
1618 1.5 riastrad goto nomem;
1619 1.1 riastrad
1620 1.1 riastrad dev->mode_config.tv_flicker_reduction_property =
1621 1.1 riastrad drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
1622 1.5 riastrad if (!dev->mode_config.tv_flicker_reduction_property)
1623 1.5 riastrad goto nomem;
1624 1.1 riastrad
1625 1.1 riastrad dev->mode_config.tv_overscan_property =
1626 1.1 riastrad drm_property_create_range(dev, 0, "overscan", 0, 100);
1627 1.5 riastrad if (!dev->mode_config.tv_overscan_property)
1628 1.5 riastrad goto nomem;
1629 1.1 riastrad
1630 1.1 riastrad dev->mode_config.tv_saturation_property =
1631 1.1 riastrad drm_property_create_range(dev, 0, "saturation", 0, 100);
1632 1.5 riastrad if (!dev->mode_config.tv_saturation_property)
1633 1.5 riastrad goto nomem;
1634 1.1 riastrad
1635 1.1 riastrad dev->mode_config.tv_hue_property =
1636 1.1 riastrad drm_property_create_range(dev, 0, "hue", 0, 100);
1637 1.5 riastrad if (!dev->mode_config.tv_hue_property)
1638 1.5 riastrad goto nomem;
1639 1.1 riastrad
1640 1.1 riastrad return 0;
1641 1.5 riastrad nomem:
1642 1.5 riastrad return -ENOMEM;
1643 1.1 riastrad }
1644 1.1 riastrad EXPORT_SYMBOL(drm_mode_create_tv_properties);
1645 1.1 riastrad
1646 1.1 riastrad /**
1647 1.1 riastrad * drm_mode_create_scaling_mode_property - create scaling mode property
1648 1.1 riastrad * @dev: DRM device
1649 1.1 riastrad *
1650 1.1 riastrad * Called by a driver the first time it's needed, must be attached to desired
1651 1.1 riastrad * connectors.
1652 1.1 riastrad */
1653 1.1 riastrad int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1654 1.1 riastrad {
1655 1.1 riastrad struct drm_property *scaling_mode;
1656 1.1 riastrad
1657 1.1 riastrad if (dev->mode_config.scaling_mode_property)
1658 1.1 riastrad return 0;
1659 1.1 riastrad
1660 1.1 riastrad scaling_mode =
1661 1.1 riastrad drm_property_create_enum(dev, 0, "scaling mode",
1662 1.1 riastrad drm_scaling_mode_enum_list,
1663 1.1 riastrad ARRAY_SIZE(drm_scaling_mode_enum_list));
1664 1.1 riastrad
1665 1.1 riastrad dev->mode_config.scaling_mode_property = scaling_mode;
1666 1.1 riastrad
1667 1.1 riastrad return 0;
1668 1.1 riastrad }
1669 1.1 riastrad EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1670 1.1 riastrad
1671 1.1 riastrad /**
1672 1.5 riastrad * drm_mode_create_aspect_ratio_property - create aspect ratio property
1673 1.5 riastrad * @dev: DRM device
1674 1.5 riastrad *
1675 1.5 riastrad * Called by a driver the first time it's needed, must be attached to desired
1676 1.5 riastrad * connectors.
1677 1.5 riastrad *
1678 1.5 riastrad * Returns:
1679 1.5 riastrad * Zero on success, negative errno on failure.
1680 1.5 riastrad */
1681 1.5 riastrad int drm_mode_create_aspect_ratio_property(struct drm_device *dev)
1682 1.5 riastrad {
1683 1.5 riastrad if (dev->mode_config.aspect_ratio_property)
1684 1.5 riastrad return 0;
1685 1.5 riastrad
1686 1.5 riastrad dev->mode_config.aspect_ratio_property =
1687 1.5 riastrad drm_property_create_enum(dev, 0, "aspect ratio",
1688 1.5 riastrad drm_aspect_ratio_enum_list,
1689 1.5 riastrad ARRAY_SIZE(drm_aspect_ratio_enum_list));
1690 1.5 riastrad
1691 1.5 riastrad if (dev->mode_config.aspect_ratio_property == NULL)
1692 1.5 riastrad return -ENOMEM;
1693 1.5 riastrad
1694 1.5 riastrad return 0;
1695 1.5 riastrad }
1696 1.5 riastrad EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property);
1697 1.5 riastrad
1698 1.5 riastrad /**
1699 1.1 riastrad * drm_mode_create_dirty_property - create dirty property
1700 1.1 riastrad * @dev: DRM device
1701 1.1 riastrad *
1702 1.1 riastrad * Called by a driver the first time it's needed, must be attached to desired
1703 1.1 riastrad * connectors.
1704 1.1 riastrad */
1705 1.1 riastrad int drm_mode_create_dirty_info_property(struct drm_device *dev)
1706 1.1 riastrad {
1707 1.1 riastrad struct drm_property *dirty_info;
1708 1.1 riastrad
1709 1.1 riastrad if (dev->mode_config.dirty_info_property)
1710 1.1 riastrad return 0;
1711 1.1 riastrad
1712 1.1 riastrad dirty_info =
1713 1.1 riastrad drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1714 1.1 riastrad "dirty",
1715 1.1 riastrad drm_dirty_info_enum_list,
1716 1.1 riastrad ARRAY_SIZE(drm_dirty_info_enum_list));
1717 1.1 riastrad dev->mode_config.dirty_info_property = dirty_info;
1718 1.1 riastrad
1719 1.3 riastrad return 0;
1720 1.1 riastrad }
1721 1.3 riastrad EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1722 1.1 riastrad
1723 1.1 riastrad /**
1724 1.5 riastrad * drm_mode_create_suggested_offset_properties - create suggests offset properties
1725 1.5 riastrad * @dev: DRM device
1726 1.1 riastrad *
1727 1.5 riastrad * Create the the suggested x/y offset property for connectors.
1728 1.1 riastrad */
1729 1.5 riastrad int drm_mode_create_suggested_offset_properties(struct drm_device *dev)
1730 1.1 riastrad {
1731 1.5 riastrad if (dev->mode_config.suggested_x_property && dev->mode_config.suggested_y_property)
1732 1.5 riastrad return 0;
1733 1.1 riastrad
1734 1.5 riastrad dev->mode_config.suggested_x_property =
1735 1.5 riastrad drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested X", 0, 0xffffffff);
1736 1.3 riastrad
1737 1.5 riastrad dev->mode_config.suggested_y_property =
1738 1.5 riastrad drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested Y", 0, 0xffffffff);
1739 1.1 riastrad
1740 1.5 riastrad if (dev->mode_config.suggested_x_property == NULL ||
1741 1.5 riastrad dev->mode_config.suggested_y_property == NULL)
1742 1.5 riastrad return -ENOMEM;
1743 1.1 riastrad return 0;
1744 1.1 riastrad }
1745 1.5 riastrad EXPORT_SYMBOL(drm_mode_create_suggested_offset_properties);
1746 1.1 riastrad
1747 1.1 riastrad /**
1748 1.1 riastrad * drm_mode_getresources - get graphics configuration
1749 1.3 riastrad * @dev: drm device for the ioctl
1750 1.3 riastrad * @data: data pointer for the ioctl
1751 1.3 riastrad * @file_priv: drm file for the ioctl call
1752 1.1 riastrad *
1753 1.1 riastrad * Construct a set of configuration description structures and return
1754 1.1 riastrad * them to the user, including CRTC, connector and framebuffer configuration.
1755 1.1 riastrad *
1756 1.1 riastrad * Called by the user via ioctl.
1757 1.1 riastrad *
1758 1.3 riastrad * Returns:
1759 1.5 riastrad * Zero on success, negative errno on failure.
1760 1.1 riastrad */
1761 1.1 riastrad int drm_mode_getresources(struct drm_device *dev, void *data,
1762 1.1 riastrad struct drm_file *file_priv)
1763 1.1 riastrad {
1764 1.1 riastrad struct drm_mode_card_res *card_res = data;
1765 1.1 riastrad struct list_head *lh;
1766 1.1 riastrad struct drm_framebuffer *fb;
1767 1.1 riastrad struct drm_connector *connector;
1768 1.1 riastrad struct drm_crtc *crtc;
1769 1.1 riastrad struct drm_encoder *encoder;
1770 1.1 riastrad int ret = 0;
1771 1.1 riastrad int connector_count = 0;
1772 1.1 riastrad int crtc_count = 0;
1773 1.1 riastrad int fb_count = 0;
1774 1.1 riastrad int encoder_count = 0;
1775 1.5 riastrad int copied = 0;
1776 1.1 riastrad uint32_t __user *fb_id;
1777 1.1 riastrad uint32_t __user *crtc_id;
1778 1.1 riastrad uint32_t __user *connector_id;
1779 1.1 riastrad uint32_t __user *encoder_id;
1780 1.1 riastrad
1781 1.1 riastrad if (!drm_core_check_feature(dev, DRIVER_MODESET))
1782 1.1 riastrad return -EINVAL;
1783 1.1 riastrad
1784 1.1 riastrad
1785 1.3 riastrad mutex_lock(&file_priv->fbs_lock);
1786 1.1 riastrad /*
1787 1.1 riastrad * For the non-control nodes we need to limit the list of resources
1788 1.1 riastrad * by IDs in the group list for this node
1789 1.1 riastrad */
1790 1.1 riastrad list_for_each(lh, &file_priv->fbs)
1791 1.1 riastrad fb_count++;
1792 1.1 riastrad
1793 1.3 riastrad /* handle this in 4 parts */
1794 1.3 riastrad /* FBs */
1795 1.3 riastrad if (card_res->count_fbs >= fb_count) {
1796 1.3 riastrad copied = 0;
1797 1.3 riastrad fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1798 1.3 riastrad list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1799 1.3 riastrad if (put_user(fb->base.id, fb_id + copied)) {
1800 1.3 riastrad mutex_unlock(&file_priv->fbs_lock);
1801 1.3 riastrad return -EFAULT;
1802 1.3 riastrad }
1803 1.3 riastrad copied++;
1804 1.3 riastrad }
1805 1.3 riastrad }
1806 1.3 riastrad card_res->count_fbs = fb_count;
1807 1.3 riastrad mutex_unlock(&file_priv->fbs_lock);
1808 1.3 riastrad
1809 1.5 riastrad /* mode_config.mutex protects the connector list against e.g. DP MST
1810 1.5 riastrad * connector hot-adding. CRTC/Plane lists are invariant. */
1811 1.5 riastrad mutex_lock(&dev->mode_config.mutex);
1812 1.5 riastrad drm_for_each_crtc(crtc, dev)
1813 1.5 riastrad crtc_count++;
1814 1.1 riastrad
1815 1.5 riastrad drm_for_each_connector(connector, dev)
1816 1.5 riastrad connector_count++;
1817 1.1 riastrad
1818 1.5 riastrad drm_for_each_encoder(encoder, dev)
1819 1.5 riastrad encoder_count++;
1820 1.1 riastrad
1821 1.1 riastrad card_res->max_height = dev->mode_config.max_height;
1822 1.1 riastrad card_res->min_height = dev->mode_config.min_height;
1823 1.1 riastrad card_res->max_width = dev->mode_config.max_width;
1824 1.1 riastrad card_res->min_width = dev->mode_config.min_width;
1825 1.1 riastrad
1826 1.1 riastrad /* CRTCs */
1827 1.1 riastrad if (card_res->count_crtcs >= crtc_count) {
1828 1.1 riastrad copied = 0;
1829 1.1 riastrad crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1830 1.5 riastrad drm_for_each_crtc(crtc, dev) {
1831 1.5 riastrad DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
1832 1.5 riastrad if (put_user(crtc->base.id, crtc_id + copied)) {
1833 1.5 riastrad ret = -EFAULT;
1834 1.5 riastrad goto out;
1835 1.1 riastrad }
1836 1.5 riastrad copied++;
1837 1.1 riastrad }
1838 1.1 riastrad }
1839 1.1 riastrad card_res->count_crtcs = crtc_count;
1840 1.1 riastrad
1841 1.1 riastrad /* Encoders */
1842 1.1 riastrad if (card_res->count_encoders >= encoder_count) {
1843 1.1 riastrad copied = 0;
1844 1.1 riastrad encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1845 1.5 riastrad drm_for_each_encoder(encoder, dev) {
1846 1.5 riastrad DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1847 1.5 riastrad encoder->name);
1848 1.5 riastrad if (put_user(encoder->base.id, encoder_id +
1849 1.5 riastrad copied)) {
1850 1.5 riastrad ret = -EFAULT;
1851 1.5 riastrad goto out;
1852 1.1 riastrad }
1853 1.5 riastrad copied++;
1854 1.1 riastrad }
1855 1.1 riastrad }
1856 1.1 riastrad card_res->count_encoders = encoder_count;
1857 1.1 riastrad
1858 1.1 riastrad /* Connectors */
1859 1.1 riastrad if (card_res->count_connectors >= connector_count) {
1860 1.1 riastrad copied = 0;
1861 1.1 riastrad connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1862 1.5 riastrad drm_for_each_connector(connector, dev) {
1863 1.5 riastrad DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1864 1.5 riastrad connector->base.id,
1865 1.5 riastrad connector->name);
1866 1.5 riastrad if (put_user(connector->base.id,
1867 1.5 riastrad connector_id + copied)) {
1868 1.5 riastrad ret = -EFAULT;
1869 1.5 riastrad goto out;
1870 1.1 riastrad }
1871 1.5 riastrad copied++;
1872 1.1 riastrad }
1873 1.1 riastrad }
1874 1.1 riastrad card_res->count_connectors = connector_count;
1875 1.1 riastrad
1876 1.1 riastrad DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
1877 1.1 riastrad card_res->count_connectors, card_res->count_encoders);
1878 1.1 riastrad
1879 1.1 riastrad out:
1880 1.5 riastrad mutex_unlock(&dev->mode_config.mutex);
1881 1.1 riastrad return ret;
1882 1.1 riastrad }
1883 1.1 riastrad
1884 1.1 riastrad /**
1885 1.1 riastrad * drm_mode_getcrtc - get CRTC configuration
1886 1.3 riastrad * @dev: drm device for the ioctl
1887 1.3 riastrad * @data: data pointer for the ioctl
1888 1.3 riastrad * @file_priv: drm file for the ioctl call
1889 1.1 riastrad *
1890 1.1 riastrad * Construct a CRTC configuration structure to return to the user.
1891 1.1 riastrad *
1892 1.1 riastrad * Called by the user via ioctl.
1893 1.1 riastrad *
1894 1.3 riastrad * Returns:
1895 1.5 riastrad * Zero on success, negative errno on failure.
1896 1.1 riastrad */
1897 1.1 riastrad int drm_mode_getcrtc(struct drm_device *dev,
1898 1.1 riastrad void *data, struct drm_file *file_priv)
1899 1.1 riastrad {
1900 1.1 riastrad struct drm_mode_crtc *crtc_resp = data;
1901 1.1 riastrad struct drm_crtc *crtc;
1902 1.1 riastrad
1903 1.1 riastrad if (!drm_core_check_feature(dev, DRIVER_MODESET))
1904 1.1 riastrad return -EINVAL;
1905 1.1 riastrad
1906 1.5 riastrad crtc = drm_crtc_find(dev, crtc_resp->crtc_id);
1907 1.5 riastrad if (!crtc)
1908 1.5 riastrad return -ENOENT;
1909 1.1 riastrad
1910 1.5 riastrad drm_modeset_lock_crtc(crtc, crtc->primary);
1911 1.1 riastrad crtc_resp->gamma_size = crtc->gamma_size;
1912 1.3 riastrad if (crtc->primary->fb)
1913 1.3 riastrad crtc_resp->fb_id = crtc->primary->fb->base.id;
1914 1.1 riastrad else
1915 1.1 riastrad crtc_resp->fb_id = 0;
1916 1.1 riastrad
1917 1.5 riastrad if (crtc->state) {
1918 1.5 riastrad crtc_resp->x = crtc->primary->state->src_x >> 16;
1919 1.5 riastrad crtc_resp->y = crtc->primary->state->src_y >> 16;
1920 1.5 riastrad if (crtc->state->enable) {
1921 1.5 riastrad drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->state->mode);
1922 1.5 riastrad crtc_resp->mode_valid = 1;
1923 1.1 riastrad
1924 1.5 riastrad } else {
1925 1.5 riastrad crtc_resp->mode_valid = 0;
1926 1.5 riastrad }
1927 1.5 riastrad } else {
1928 1.5 riastrad crtc_resp->x = crtc->x;
1929 1.5 riastrad crtc_resp->y = crtc->y;
1930 1.5 riastrad if (crtc->enabled) {
1931 1.5 riastrad drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1932 1.5 riastrad crtc_resp->mode_valid = 1;
1933 1.1 riastrad
1934 1.5 riastrad } else {
1935 1.5 riastrad crtc_resp->mode_valid = 0;
1936 1.5 riastrad }
1937 1.1 riastrad }
1938 1.5 riastrad drm_modeset_unlock_crtc(crtc);
1939 1.1 riastrad
1940 1.5 riastrad return 0;
1941 1.1 riastrad }
1942 1.1 riastrad
1943 1.3 riastrad static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
1944 1.3 riastrad const struct drm_file *file_priv)
1945 1.3 riastrad {
1946 1.3 riastrad /*
1947 1.3 riastrad * If user-space hasn't configured the driver to expose the stereo 3D
1948 1.3 riastrad * modes, don't expose them.
1949 1.3 riastrad */
1950 1.3 riastrad if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
1951 1.3 riastrad return false;
1952 1.3 riastrad
1953 1.3 riastrad return true;
1954 1.3 riastrad }
1955 1.3 riastrad
1956 1.5 riastrad static struct drm_encoder *drm_connector_get_encoder(struct drm_connector *connector)
1957 1.5 riastrad {
1958 1.5 riastrad /* For atomic drivers only state objects are synchronously updated and
1959 1.5 riastrad * protected by modeset locks, so check those first. */
1960 1.5 riastrad if (connector->state)
1961 1.5 riastrad return connector->state->best_encoder;
1962 1.5 riastrad return connector->encoder;
1963 1.5 riastrad }
1964 1.5 riastrad
1965 1.5 riastrad /* helper for getconnector and getproperties ioctls */
1966 1.5 riastrad static int get_properties(struct drm_mode_object *obj, bool atomic,
1967 1.5 riastrad uint32_t __user *prop_ptr, uint64_t __user *prop_values,
1968 1.5 riastrad uint32_t *arg_count_props)
1969 1.5 riastrad {
1970 1.5 riastrad int props_count;
1971 1.5 riastrad int i, ret, copied;
1972 1.5 riastrad
1973 1.5 riastrad props_count = obj->properties->count;
1974 1.5 riastrad if (!atomic)
1975 1.5 riastrad props_count -= obj->properties->atomic_count;
1976 1.5 riastrad
1977 1.5 riastrad if ((*arg_count_props >= props_count) && props_count) {
1978 1.5 riastrad for (i = 0, copied = 0; copied < props_count; i++) {
1979 1.5 riastrad struct drm_property *prop = obj->properties->properties[i];
1980 1.5 riastrad uint64_t val;
1981 1.5 riastrad
1982 1.5 riastrad if ((prop->flags & DRM_MODE_PROP_ATOMIC) && !atomic)
1983 1.5 riastrad continue;
1984 1.5 riastrad
1985 1.5 riastrad ret = drm_object_property_get_value(obj, prop, &val);
1986 1.5 riastrad if (ret)
1987 1.5 riastrad return ret;
1988 1.5 riastrad
1989 1.5 riastrad if (put_user(prop->base.id, prop_ptr + copied))
1990 1.5 riastrad return -EFAULT;
1991 1.5 riastrad
1992 1.5 riastrad if (put_user(val, prop_values + copied))
1993 1.5 riastrad return -EFAULT;
1994 1.5 riastrad
1995 1.5 riastrad copied++;
1996 1.5 riastrad }
1997 1.5 riastrad }
1998 1.5 riastrad *arg_count_props = props_count;
1999 1.5 riastrad
2000 1.5 riastrad return 0;
2001 1.5 riastrad }
2002 1.5 riastrad
2003 1.1 riastrad /**
2004 1.1 riastrad * drm_mode_getconnector - get connector configuration
2005 1.3 riastrad * @dev: drm device for the ioctl
2006 1.3 riastrad * @data: data pointer for the ioctl
2007 1.3 riastrad * @file_priv: drm file for the ioctl call
2008 1.1 riastrad *
2009 1.1 riastrad * Construct a connector configuration structure to return to the user.
2010 1.1 riastrad *
2011 1.1 riastrad * Called by the user via ioctl.
2012 1.1 riastrad *
2013 1.3 riastrad * Returns:
2014 1.5 riastrad * Zero on success, negative errno on failure.
2015 1.1 riastrad */
2016 1.1 riastrad int drm_mode_getconnector(struct drm_device *dev, void *data,
2017 1.1 riastrad struct drm_file *file_priv)
2018 1.1 riastrad {
2019 1.1 riastrad struct drm_mode_get_connector *out_resp = data;
2020 1.1 riastrad struct drm_connector *connector;
2021 1.5 riastrad struct drm_encoder *encoder;
2022 1.1 riastrad struct drm_display_mode *mode;
2023 1.1 riastrad int mode_count = 0;
2024 1.1 riastrad int encoders_count = 0;
2025 1.1 riastrad int ret = 0;
2026 1.1 riastrad int copied = 0;
2027 1.1 riastrad int i;
2028 1.1 riastrad struct drm_mode_modeinfo u_mode;
2029 1.1 riastrad struct drm_mode_modeinfo __user *mode_ptr;
2030 1.1 riastrad uint32_t __user *encoder_ptr;
2031 1.1 riastrad
2032 1.1 riastrad if (!drm_core_check_feature(dev, DRIVER_MODESET))
2033 1.1 riastrad return -EINVAL;
2034 1.1 riastrad
2035 1.1 riastrad memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
2036 1.1 riastrad
2037 1.1 riastrad DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
2038 1.1 riastrad
2039 1.1 riastrad mutex_lock(&dev->mode_config.mutex);
2040 1.1 riastrad
2041 1.5 riastrad connector = drm_connector_find(dev, out_resp->connector_id);
2042 1.5 riastrad if (!connector) {
2043 1.3 riastrad ret = -ENOENT;
2044 1.5 riastrad goto out_unlock;
2045 1.1 riastrad }
2046 1.1 riastrad
2047 1.5 riastrad for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++)
2048 1.5 riastrad if (connector->encoder_ids[i] != 0)
2049 1.1 riastrad encoders_count++;
2050 1.1 riastrad
2051 1.1 riastrad if (out_resp->count_modes == 0) {
2052 1.1 riastrad connector->funcs->fill_modes(connector,
2053 1.1 riastrad dev->mode_config.max_width,
2054 1.1 riastrad dev->mode_config.max_height);
2055 1.1 riastrad }
2056 1.1 riastrad
2057 1.1 riastrad /* delayed so we get modes regardless of pre-fill_modes state */
2058 1.1 riastrad list_for_each_entry(mode, &connector->modes, head)
2059 1.3 riastrad if (drm_mode_expose_to_userspace(mode, file_priv))
2060 1.3 riastrad mode_count++;
2061 1.1 riastrad
2062 1.1 riastrad out_resp->connector_id = connector->base.id;
2063 1.1 riastrad out_resp->connector_type = connector->connector_type;
2064 1.1 riastrad out_resp->connector_type_id = connector->connector_type_id;
2065 1.1 riastrad out_resp->mm_width = connector->display_info.width_mm;
2066 1.1 riastrad out_resp->mm_height = connector->display_info.height_mm;
2067 1.1 riastrad out_resp->subpixel = connector->display_info.subpixel_order;
2068 1.1 riastrad out_resp->connection = connector->status;
2069 1.5 riastrad
2070 1.5 riastrad drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
2071 1.5 riastrad encoder = drm_connector_get_encoder(connector);
2072 1.5 riastrad if (encoder)
2073 1.5 riastrad out_resp->encoder_id = encoder->base.id;
2074 1.1 riastrad else
2075 1.1 riastrad out_resp->encoder_id = 0;
2076 1.1 riastrad
2077 1.1 riastrad /*
2078 1.1 riastrad * This ioctl is called twice, once to determine how much space is
2079 1.1 riastrad * needed, and the 2nd time to fill it.
2080 1.1 riastrad */
2081 1.1 riastrad if ((out_resp->count_modes >= mode_count) && mode_count) {
2082 1.1 riastrad copied = 0;
2083 1.1 riastrad mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
2084 1.1 riastrad list_for_each_entry(mode, &connector->modes, head) {
2085 1.3 riastrad if (!drm_mode_expose_to_userspace(mode, file_priv))
2086 1.3 riastrad continue;
2087 1.3 riastrad
2088 1.5 riastrad drm_mode_convert_to_umode(&u_mode, mode);
2089 1.1 riastrad if (copy_to_user(mode_ptr + copied,
2090 1.1 riastrad &u_mode, sizeof(u_mode))) {
2091 1.1 riastrad ret = -EFAULT;
2092 1.1 riastrad goto out;
2093 1.1 riastrad }
2094 1.1 riastrad copied++;
2095 1.1 riastrad }
2096 1.1 riastrad }
2097 1.1 riastrad out_resp->count_modes = mode_count;
2098 1.1 riastrad
2099 1.5 riastrad ret = get_properties(&connector->base, file_priv->atomic,
2100 1.5 riastrad (uint32_t __user *)(unsigned long)(out_resp->props_ptr),
2101 1.5 riastrad (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr),
2102 1.5 riastrad &out_resp->count_props);
2103 1.5 riastrad if (ret)
2104 1.5 riastrad goto out;
2105 1.1 riastrad
2106 1.1 riastrad if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
2107 1.1 riastrad copied = 0;
2108 1.1 riastrad encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
2109 1.1 riastrad for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2110 1.1 riastrad if (connector->encoder_ids[i] != 0) {
2111 1.1 riastrad if (put_user(connector->encoder_ids[i],
2112 1.1 riastrad encoder_ptr + copied)) {
2113 1.1 riastrad ret = -EFAULT;
2114 1.1 riastrad goto out;
2115 1.1 riastrad }
2116 1.1 riastrad copied++;
2117 1.1 riastrad }
2118 1.1 riastrad }
2119 1.1 riastrad }
2120 1.1 riastrad out_resp->count_encoders = encoders_count;
2121 1.1 riastrad
2122 1.1 riastrad out:
2123 1.5 riastrad drm_modeset_unlock(&dev->mode_config.connection_mutex);
2124 1.5 riastrad
2125 1.5 riastrad out_unlock:
2126 1.1 riastrad mutex_unlock(&dev->mode_config.mutex);
2127 1.3 riastrad
2128 1.1 riastrad return ret;
2129 1.1 riastrad }
2130 1.1 riastrad
2131 1.5 riastrad static struct drm_crtc *drm_encoder_get_crtc(struct drm_encoder *encoder)
2132 1.5 riastrad {
2133 1.5 riastrad struct drm_connector *connector;
2134 1.5 riastrad struct drm_device *dev = encoder->dev;
2135 1.5 riastrad bool uses_atomic = false;
2136 1.5 riastrad
2137 1.5 riastrad /* For atomic drivers only state objects are synchronously updated and
2138 1.5 riastrad * protected by modeset locks, so check those first. */
2139 1.5 riastrad drm_for_each_connector(connector, dev) {
2140 1.5 riastrad if (!connector->state)
2141 1.5 riastrad continue;
2142 1.5 riastrad
2143 1.5 riastrad uses_atomic = true;
2144 1.5 riastrad
2145 1.5 riastrad if (connector->state->best_encoder != encoder)
2146 1.5 riastrad continue;
2147 1.5 riastrad
2148 1.5 riastrad return connector->state->crtc;
2149 1.5 riastrad }
2150 1.5 riastrad
2151 1.5 riastrad /* Don't return stale data (e.g. pending async disable). */
2152 1.5 riastrad if (uses_atomic)
2153 1.5 riastrad return NULL;
2154 1.5 riastrad
2155 1.5 riastrad return encoder->crtc;
2156 1.5 riastrad }
2157 1.5 riastrad
2158 1.3 riastrad /**
2159 1.3 riastrad * drm_mode_getencoder - get encoder configuration
2160 1.3 riastrad * @dev: drm device for the ioctl
2161 1.3 riastrad * @data: data pointer for the ioctl
2162 1.3 riastrad * @file_priv: drm file for the ioctl call
2163 1.3 riastrad *
2164 1.3 riastrad * Construct a encoder configuration structure to return to the user.
2165 1.3 riastrad *
2166 1.3 riastrad * Called by the user via ioctl.
2167 1.3 riastrad *
2168 1.3 riastrad * Returns:
2169 1.5 riastrad * Zero on success, negative errno on failure.
2170 1.3 riastrad */
2171 1.1 riastrad int drm_mode_getencoder(struct drm_device *dev, void *data,
2172 1.1 riastrad struct drm_file *file_priv)
2173 1.1 riastrad {
2174 1.1 riastrad struct drm_mode_get_encoder *enc_resp = data;
2175 1.1 riastrad struct drm_encoder *encoder;
2176 1.5 riastrad struct drm_crtc *crtc;
2177 1.1 riastrad
2178 1.1 riastrad if (!drm_core_check_feature(dev, DRIVER_MODESET))
2179 1.1 riastrad return -EINVAL;
2180 1.1 riastrad
2181 1.5 riastrad encoder = drm_encoder_find(dev, enc_resp->encoder_id);
2182 1.5 riastrad if (!encoder)
2183 1.5 riastrad return -ENOENT;
2184 1.1 riastrad
2185 1.5 riastrad drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
2186 1.5 riastrad crtc = drm_encoder_get_crtc(encoder);
2187 1.5 riastrad if (crtc)
2188 1.5 riastrad enc_resp->crtc_id = crtc->base.id;
2189 1.1 riastrad else
2190 1.1 riastrad enc_resp->crtc_id = 0;
2191 1.5 riastrad drm_modeset_unlock(&dev->mode_config.connection_mutex);
2192 1.5 riastrad
2193 1.1 riastrad enc_resp->encoder_type = encoder->encoder_type;
2194 1.1 riastrad enc_resp->encoder_id = encoder->base.id;
2195 1.1 riastrad enc_resp->possible_crtcs = encoder->possible_crtcs;
2196 1.1 riastrad enc_resp->possible_clones = encoder->possible_clones;
2197 1.1 riastrad
2198 1.5 riastrad return 0;
2199 1.1 riastrad }
2200 1.1 riastrad
2201 1.1 riastrad /**
2202 1.3 riastrad * drm_mode_getplane_res - enumerate all plane resources
2203 1.1 riastrad * @dev: DRM device
2204 1.1 riastrad * @data: ioctl data
2205 1.1 riastrad * @file_priv: DRM file info
2206 1.1 riastrad *
2207 1.3 riastrad * Construct a list of plane ids to return to the user.
2208 1.3 riastrad *
2209 1.3 riastrad * Called by the user via ioctl.
2210 1.1 riastrad *
2211 1.3 riastrad * Returns:
2212 1.5 riastrad * Zero on success, negative errno on failure.
2213 1.1 riastrad */
2214 1.1 riastrad int drm_mode_getplane_res(struct drm_device *dev, void *data,
2215 1.3 riastrad struct drm_file *file_priv)
2216 1.1 riastrad {
2217 1.1 riastrad struct drm_mode_get_plane_res *plane_resp = data;
2218 1.1 riastrad struct drm_mode_config *config;
2219 1.1 riastrad struct drm_plane *plane;
2220 1.1 riastrad uint32_t __user *plane_ptr;
2221 1.5 riastrad int copied = 0;
2222 1.3 riastrad unsigned num_planes;
2223 1.1 riastrad
2224 1.1 riastrad if (!drm_core_check_feature(dev, DRIVER_MODESET))
2225 1.1 riastrad return -EINVAL;
2226 1.1 riastrad
2227 1.1 riastrad config = &dev->mode_config;
2228 1.1 riastrad
2229 1.3 riastrad if (file_priv->universal_planes)
2230 1.3 riastrad num_planes = config->num_total_plane;
2231 1.3 riastrad else
2232 1.3 riastrad num_planes = config->num_overlay_plane;
2233 1.3 riastrad
2234 1.1 riastrad /*
2235 1.1 riastrad * This ioctl is called twice, once to determine how much space is
2236 1.1 riastrad * needed, and the 2nd time to fill it.
2237 1.1 riastrad */
2238 1.3 riastrad if (num_planes &&
2239 1.3 riastrad (plane_resp->count_planes >= num_planes)) {
2240 1.1 riastrad plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
2241 1.1 riastrad
2242 1.5 riastrad /* Plane lists are invariant, no locking needed. */
2243 1.5 riastrad drm_for_each_plane(plane, dev) {
2244 1.3 riastrad /*
2245 1.3 riastrad * Unless userspace set the 'universal planes'
2246 1.3 riastrad * capability bit, only advertise overlays.
2247 1.3 riastrad */
2248 1.3 riastrad if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
2249 1.3 riastrad !file_priv->universal_planes)
2250 1.3 riastrad continue;
2251 1.3 riastrad
2252 1.5 riastrad if (put_user(plane->base.id, plane_ptr + copied))
2253 1.5 riastrad return -EFAULT;
2254 1.1 riastrad copied++;
2255 1.1 riastrad }
2256 1.1 riastrad }
2257 1.3 riastrad plane_resp->count_planes = num_planes;
2258 1.1 riastrad
2259 1.5 riastrad return 0;
2260 1.1 riastrad }
2261 1.1 riastrad
2262 1.1 riastrad /**
2263 1.3 riastrad * drm_mode_getplane - get plane configuration
2264 1.1 riastrad * @dev: DRM device
2265 1.1 riastrad * @data: ioctl data
2266 1.1 riastrad * @file_priv: DRM file info
2267 1.1 riastrad *
2268 1.3 riastrad * Construct a plane configuration structure to return to the user.
2269 1.1 riastrad *
2270 1.3 riastrad * Called by the user via ioctl.
2271 1.3 riastrad *
2272 1.3 riastrad * Returns:
2273 1.5 riastrad * Zero on success, negative errno on failure.
2274 1.1 riastrad */
2275 1.1 riastrad int drm_mode_getplane(struct drm_device *dev, void *data,
2276 1.3 riastrad struct drm_file *file_priv)
2277 1.1 riastrad {
2278 1.1 riastrad struct drm_mode_get_plane *plane_resp = data;
2279 1.1 riastrad struct drm_plane *plane;
2280 1.1 riastrad uint32_t __user *format_ptr;
2281 1.1 riastrad
2282 1.1 riastrad if (!drm_core_check_feature(dev, DRIVER_MODESET))
2283 1.1 riastrad return -EINVAL;
2284 1.1 riastrad
2285 1.5 riastrad plane = drm_plane_find(dev, plane_resp->plane_id);
2286 1.5 riastrad if (!plane)
2287 1.5 riastrad return -ENOENT;
2288 1.1 riastrad
2289 1.5 riastrad drm_modeset_lock(&plane->mutex, NULL);
2290 1.1 riastrad if (plane->crtc)
2291 1.1 riastrad plane_resp->crtc_id = plane->crtc->base.id;
2292 1.1 riastrad else
2293 1.1 riastrad plane_resp->crtc_id = 0;
2294 1.1 riastrad
2295 1.1 riastrad if (plane->fb)
2296 1.1 riastrad plane_resp->fb_id = plane->fb->base.id;
2297 1.1 riastrad else
2298 1.1 riastrad plane_resp->fb_id = 0;
2299 1.5 riastrad drm_modeset_unlock(&plane->mutex);
2300 1.1 riastrad
2301 1.1 riastrad plane_resp->plane_id = plane->base.id;
2302 1.1 riastrad plane_resp->possible_crtcs = plane->possible_crtcs;
2303 1.3 riastrad plane_resp->gamma_size = 0;
2304 1.1 riastrad
2305 1.1 riastrad /*
2306 1.1 riastrad * This ioctl is called twice, once to determine how much space is
2307 1.1 riastrad * needed, and the 2nd time to fill it.
2308 1.1 riastrad */
2309 1.5 riastrad if (plane->format_count &&
2310 1.5 riastrad (plane_resp->count_format_types >= plane->format_count)) {
2311 1.5 riastrad format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
2312 1.5 riastrad if (copy_to_user(format_ptr,
2313 1.5 riastrad plane->format_types,
2314 1.5 riastrad sizeof(uint32_t) * plane->format_count)) {
2315 1.5 riastrad return -EFAULT;
2316 1.5 riastrad }
2317 1.5 riastrad }
2318 1.5 riastrad plane_resp->count_format_types = plane->format_count;
2319 1.5 riastrad
2320 1.5 riastrad return 0;
2321 1.5 riastrad }
2322 1.5 riastrad
2323 1.5 riastrad /**
2324 1.5 riastrad * drm_plane_check_pixel_format - Check if the plane supports the pixel format
2325 1.5 riastrad * @plane: plane to check for format support
2326 1.5 riastrad * @format: the pixel format
2327 1.5 riastrad *
2328 1.5 riastrad * Returns:
2329 1.5 riastrad * Zero of @plane has @format in its list of supported pixel formats, -EINVAL
2330 1.5 riastrad * otherwise.
2331 1.5 riastrad */
2332 1.5 riastrad int drm_plane_check_pixel_format(const struct drm_plane *plane, u32 format)
2333 1.5 riastrad {
2334 1.5 riastrad unsigned int i;
2335 1.5 riastrad
2336 1.5 riastrad for (i = 0; i < plane->format_count; i++) {
2337 1.5 riastrad if (format == plane->format_types[i])
2338 1.5 riastrad return 0;
2339 1.5 riastrad }
2340 1.5 riastrad
2341 1.5 riastrad return -EINVAL;
2342 1.5 riastrad }
2343 1.5 riastrad
2344 1.5 riastrad static int check_src_coords(uint32_t src_x, uint32_t src_y,
2345 1.5 riastrad uint32_t src_w, uint32_t src_h,
2346 1.5 riastrad const struct drm_framebuffer *fb)
2347 1.5 riastrad {
2348 1.5 riastrad unsigned int fb_width, fb_height;
2349 1.5 riastrad
2350 1.5 riastrad fb_width = fb->width << 16;
2351 1.5 riastrad fb_height = fb->height << 16;
2352 1.5 riastrad
2353 1.5 riastrad /* Make sure source coordinates are inside the fb. */
2354 1.5 riastrad if (src_w > fb_width ||
2355 1.5 riastrad src_x > fb_width - src_w ||
2356 1.5 riastrad src_h > fb_height ||
2357 1.5 riastrad src_y > fb_height - src_h) {
2358 1.5 riastrad DRM_DEBUG_KMS("Invalid source coordinates "
2359 1.5 riastrad "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
2360 1.5 riastrad src_w >> 16, ((src_w & 0xffff) * 15625) >> 10,
2361 1.5 riastrad src_h >> 16, ((src_h & 0xffff) * 15625) >> 10,
2362 1.5 riastrad src_x >> 16, ((src_x & 0xffff) * 15625) >> 10,
2363 1.5 riastrad src_y >> 16, ((src_y & 0xffff) * 15625) >> 10);
2364 1.5 riastrad return -ENOSPC;
2365 1.5 riastrad }
2366 1.5 riastrad
2367 1.5 riastrad return 0;
2368 1.5 riastrad }
2369 1.5 riastrad
2370 1.5 riastrad /*
2371 1.5 riastrad * setplane_internal - setplane handler for internal callers
2372 1.5 riastrad *
2373 1.5 riastrad * Note that we assume an extra reference has already been taken on fb. If the
2374 1.5 riastrad * update fails, this reference will be dropped before return; if it succeeds,
2375 1.5 riastrad * the previous framebuffer (if any) will be unreferenced instead.
2376 1.5 riastrad *
2377 1.5 riastrad * src_{x,y,w,h} are provided in 16.16 fixed point format
2378 1.5 riastrad */
2379 1.5 riastrad static int __setplane_internal(struct drm_plane *plane,
2380 1.5 riastrad struct drm_crtc *crtc,
2381 1.5 riastrad struct drm_framebuffer *fb,
2382 1.5 riastrad int32_t crtc_x, int32_t crtc_y,
2383 1.5 riastrad uint32_t crtc_w, uint32_t crtc_h,
2384 1.5 riastrad /* src_{x,y,w,h} values are 16.16 fixed point */
2385 1.5 riastrad uint32_t src_x, uint32_t src_y,
2386 1.5 riastrad uint32_t src_w, uint32_t src_h)
2387 1.5 riastrad {
2388 1.5 riastrad int ret = 0;
2389 1.5 riastrad
2390 1.5 riastrad /* No fb means shut it down */
2391 1.5 riastrad if (!fb) {
2392 1.5 riastrad plane->old_fb = plane->fb;
2393 1.5 riastrad ret = plane->funcs->disable_plane(plane);
2394 1.5 riastrad if (!ret) {
2395 1.5 riastrad plane->crtc = NULL;
2396 1.5 riastrad plane->fb = NULL;
2397 1.5 riastrad } else {
2398 1.5 riastrad plane->old_fb = NULL;
2399 1.5 riastrad }
2400 1.5 riastrad goto out;
2401 1.5 riastrad }
2402 1.5 riastrad
2403 1.5 riastrad /* Check whether this plane is usable on this CRTC */
2404 1.5 riastrad if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
2405 1.5 riastrad DRM_DEBUG_KMS("Invalid crtc for plane\n");
2406 1.5 riastrad ret = -EINVAL;
2407 1.5 riastrad goto out;
2408 1.5 riastrad }
2409 1.5 riastrad
2410 1.5 riastrad /* Check whether this plane supports the fb pixel format. */
2411 1.5 riastrad ret = drm_plane_check_pixel_format(plane, fb->pixel_format);
2412 1.5 riastrad if (ret) {
2413 1.5 riastrad DRM_DEBUG_KMS("Invalid pixel format %s\n",
2414 1.5 riastrad drm_get_format_name(fb->pixel_format));
2415 1.5 riastrad goto out;
2416 1.5 riastrad }
2417 1.5 riastrad
2418 1.5 riastrad /* Give drivers some help against integer overflows */
2419 1.5 riastrad if (crtc_w > INT_MAX ||
2420 1.5 riastrad crtc_x > INT_MAX - (int32_t) crtc_w ||
2421 1.5 riastrad crtc_h > INT_MAX ||
2422 1.5 riastrad crtc_y > INT_MAX - (int32_t) crtc_h) {
2423 1.5 riastrad DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
2424 1.5 riastrad crtc_w, crtc_h, crtc_x, crtc_y);
2425 1.5 riastrad ret = -ERANGE;
2426 1.5 riastrad goto out;
2427 1.5 riastrad }
2428 1.5 riastrad
2429 1.5 riastrad ret = check_src_coords(src_x, src_y, src_w, src_h, fb);
2430 1.5 riastrad if (ret)
2431 1.5 riastrad goto out;
2432 1.5 riastrad
2433 1.5 riastrad plane->old_fb = plane->fb;
2434 1.5 riastrad ret = plane->funcs->update_plane(plane, crtc, fb,
2435 1.5 riastrad crtc_x, crtc_y, crtc_w, crtc_h,
2436 1.5 riastrad src_x, src_y, src_w, src_h);
2437 1.5 riastrad if (!ret) {
2438 1.5 riastrad plane->crtc = crtc;
2439 1.5 riastrad plane->fb = fb;
2440 1.5 riastrad fb = NULL;
2441 1.5 riastrad } else {
2442 1.5 riastrad plane->old_fb = NULL;
2443 1.1 riastrad }
2444 1.1 riastrad
2445 1.1 riastrad out:
2446 1.5 riastrad if (fb)
2447 1.5 riastrad drm_framebuffer_unreference(fb);
2448 1.5 riastrad if (plane->old_fb)
2449 1.5 riastrad drm_framebuffer_unreference(plane->old_fb);
2450 1.5 riastrad plane->old_fb = NULL;
2451 1.5 riastrad
2452 1.5 riastrad return ret;
2453 1.5 riastrad }
2454 1.5 riastrad
2455 1.5 riastrad static int setplane_internal(struct drm_plane *plane,
2456 1.5 riastrad struct drm_crtc *crtc,
2457 1.5 riastrad struct drm_framebuffer *fb,
2458 1.5 riastrad int32_t crtc_x, int32_t crtc_y,
2459 1.5 riastrad uint32_t crtc_w, uint32_t crtc_h,
2460 1.5 riastrad /* src_{x,y,w,h} values are 16.16 fixed point */
2461 1.5 riastrad uint32_t src_x, uint32_t src_y,
2462 1.5 riastrad uint32_t src_w, uint32_t src_h)
2463 1.5 riastrad {
2464 1.5 riastrad int ret;
2465 1.5 riastrad
2466 1.5 riastrad drm_modeset_lock_all(plane->dev);
2467 1.5 riastrad ret = __setplane_internal(plane, crtc, fb,
2468 1.5 riastrad crtc_x, crtc_y, crtc_w, crtc_h,
2469 1.5 riastrad src_x, src_y, src_w, src_h);
2470 1.5 riastrad drm_modeset_unlock_all(plane->dev);
2471 1.5 riastrad
2472 1.1 riastrad return ret;
2473 1.1 riastrad }
2474 1.1 riastrad
2475 1.1 riastrad /**
2476 1.3 riastrad * drm_mode_setplane - configure a plane's configuration
2477 1.1 riastrad * @dev: DRM device
2478 1.1 riastrad * @data: ioctl data*
2479 1.3 riastrad * @file_priv: DRM file info
2480 1.1 riastrad *
2481 1.3 riastrad * Set plane configuration, including placement, fb, scaling, and other factors.
2482 1.5 riastrad * Or pass a NULL fb to disable (planes may be disabled without providing a
2483 1.5 riastrad * valid crtc).
2484 1.1 riastrad *
2485 1.3 riastrad * Returns:
2486 1.5 riastrad * Zero on success, negative errno on failure.
2487 1.1 riastrad */
2488 1.1 riastrad int drm_mode_setplane(struct drm_device *dev, void *data,
2489 1.3 riastrad struct drm_file *file_priv)
2490 1.1 riastrad {
2491 1.1 riastrad struct drm_mode_set_plane *plane_req = data;
2492 1.1 riastrad struct drm_plane *plane;
2493 1.5 riastrad struct drm_crtc *crtc = NULL;
2494 1.5 riastrad struct drm_framebuffer *fb = NULL;
2495 1.1 riastrad
2496 1.1 riastrad if (!drm_core_check_feature(dev, DRIVER_MODESET))
2497 1.1 riastrad return -EINVAL;
2498 1.1 riastrad
2499 1.1 riastrad /*
2500 1.1 riastrad * First, find the plane, crtc, and fb objects. If not available,
2501 1.1 riastrad * we don't bother to call the driver.
2502 1.1 riastrad */
2503 1.5 riastrad plane = drm_plane_find(dev, plane_req->plane_id);
2504 1.5 riastrad if (!plane) {
2505 1.1 riastrad DRM_DEBUG_KMS("Unknown plane ID %d\n",
2506 1.1 riastrad plane_req->plane_id);
2507 1.3 riastrad return -ENOENT;
2508 1.1 riastrad }
2509 1.1 riastrad
2510 1.5 riastrad if (plane_req->fb_id) {
2511 1.5 riastrad fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
2512 1.5 riastrad if (!fb) {
2513 1.5 riastrad DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
2514 1.5 riastrad plane_req->fb_id);
2515 1.5 riastrad return -ENOENT;
2516 1.5 riastrad }
2517 1.1 riastrad
2518 1.5 riastrad crtc = drm_crtc_find(dev, plane_req->crtc_id);
2519 1.5 riastrad if (!crtc) {
2520 1.5 riastrad DRM_DEBUG_KMS("Unknown crtc ID %d\n",
2521 1.5 riastrad plane_req->crtc_id);
2522 1.5 riastrad return -ENOENT;
2523 1.5 riastrad }
2524 1.1 riastrad }
2525 1.1 riastrad
2526 1.5 riastrad /*
2527 1.5 riastrad * setplane_internal will take care of deref'ing either the old or new
2528 1.5 riastrad * framebuffer depending on success.
2529 1.5 riastrad */
2530 1.5 riastrad return setplane_internal(plane, crtc, fb,
2531 1.5 riastrad plane_req->crtc_x, plane_req->crtc_y,
2532 1.5 riastrad plane_req->crtc_w, plane_req->crtc_h,
2533 1.5 riastrad plane_req->src_x, plane_req->src_y,
2534 1.5 riastrad plane_req->src_w, plane_req->src_h);
2535 1.3 riastrad }
2536 1.3 riastrad
2537 1.3 riastrad /**
2538 1.3 riastrad * drm_mode_set_config_internal - helper to call ->set_config
2539 1.3 riastrad * @set: modeset config to set
2540 1.3 riastrad *
2541 1.3 riastrad * This is a little helper to wrap internal calls to the ->set_config driver
2542 1.3 riastrad * interface. The only thing it adds is correct refcounting dance.
2543 1.5 riastrad *
2544 1.3 riastrad * Returns:
2545 1.5 riastrad * Zero on success, negative errno on failure.
2546 1.3 riastrad */
2547 1.3 riastrad int drm_mode_set_config_internal(struct drm_mode_set *set)
2548 1.3 riastrad {
2549 1.3 riastrad struct drm_crtc *crtc = set->crtc;
2550 1.3 riastrad struct drm_framebuffer *fb;
2551 1.3 riastrad struct drm_crtc *tmp;
2552 1.3 riastrad int ret;
2553 1.3 riastrad
2554 1.3 riastrad /*
2555 1.3 riastrad * NOTE: ->set_config can also disable other crtcs (if we steal all
2556 1.3 riastrad * connectors from it), hence we need to refcount the fbs across all
2557 1.3 riastrad * crtcs. Atomic modeset will have saner semantics ...
2558 1.3 riastrad */
2559 1.5 riastrad drm_for_each_crtc(tmp, crtc->dev)
2560 1.5 riastrad tmp->primary->old_fb = tmp->primary->fb;
2561 1.3 riastrad
2562 1.3 riastrad fb = set->fb;
2563 1.3 riastrad
2564 1.3 riastrad ret = crtc->funcs->set_config(set);
2565 1.3 riastrad if (ret == 0) {
2566 1.3 riastrad crtc->primary->crtc = crtc;
2567 1.5 riastrad crtc->primary->fb = fb;
2568 1.3 riastrad }
2569 1.3 riastrad
2570 1.5 riastrad drm_for_each_crtc(tmp, crtc->dev) {
2571 1.3 riastrad if (tmp->primary->fb)
2572 1.3 riastrad drm_framebuffer_reference(tmp->primary->fb);
2573 1.5 riastrad if (tmp->primary->old_fb)
2574 1.5 riastrad drm_framebuffer_unreference(tmp->primary->old_fb);
2575 1.5 riastrad tmp->primary->old_fb = NULL;
2576 1.3 riastrad }
2577 1.1 riastrad
2578 1.1 riastrad return ret;
2579 1.1 riastrad }
2580 1.3 riastrad EXPORT_SYMBOL(drm_mode_set_config_internal);
2581 1.3 riastrad
2582 1.3 riastrad /**
2583 1.5 riastrad * drm_crtc_get_hv_timing - Fetches hdisplay/vdisplay for given mode
2584 1.5 riastrad * @mode: mode to query
2585 1.5 riastrad * @hdisplay: hdisplay value to fill in
2586 1.5 riastrad * @vdisplay: vdisplay value to fill in
2587 1.5 riastrad *
2588 1.5 riastrad * The vdisplay value will be doubled if the specified mode is a stereo mode of
2589 1.5 riastrad * the appropriate layout.
2590 1.5 riastrad */
2591 1.5 riastrad void drm_crtc_get_hv_timing(const struct drm_display_mode *mode,
2592 1.5 riastrad int *hdisplay, int *vdisplay)
2593 1.5 riastrad {
2594 1.5 riastrad struct drm_display_mode adjusted;
2595 1.5 riastrad
2596 1.5 riastrad drm_mode_copy(&adjusted, mode);
2597 1.5 riastrad drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE_ONLY);
2598 1.5 riastrad *hdisplay = adjusted.crtc_hdisplay;
2599 1.5 riastrad *vdisplay = adjusted.crtc_vdisplay;
2600 1.5 riastrad }
2601 1.5 riastrad EXPORT_SYMBOL(drm_crtc_get_hv_timing);
2602 1.5 riastrad
2603 1.5 riastrad /**
2604 1.3 riastrad * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
2605 1.3 riastrad * CRTC viewport
2606 1.3 riastrad * @crtc: CRTC that framebuffer will be displayed on
2607 1.3 riastrad * @x: x panning
2608 1.3 riastrad * @y: y panning
2609 1.3 riastrad * @mode: mode that framebuffer will be displayed under
2610 1.3 riastrad * @fb: framebuffer to check size of
2611 1.3 riastrad */
2612 1.3 riastrad int drm_crtc_check_viewport(const struct drm_crtc *crtc,
2613 1.3 riastrad int x, int y,
2614 1.3 riastrad const struct drm_display_mode *mode,
2615 1.3 riastrad const struct drm_framebuffer *fb)
2616 1.3 riastrad
2617 1.3 riastrad {
2618 1.3 riastrad int hdisplay, vdisplay;
2619 1.3 riastrad
2620 1.5 riastrad drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
2621 1.3 riastrad
2622 1.5 riastrad if (crtc->state &&
2623 1.5 riastrad crtc->primary->state->rotation & (BIT(DRM_ROTATE_90) |
2624 1.5 riastrad BIT(DRM_ROTATE_270)))
2625 1.3 riastrad swap(hdisplay, vdisplay);
2626 1.3 riastrad
2627 1.5 riastrad return check_src_coords(x << 16, y << 16,
2628 1.5 riastrad hdisplay << 16, vdisplay << 16, fb);
2629 1.3 riastrad }
2630 1.3 riastrad EXPORT_SYMBOL(drm_crtc_check_viewport);
2631 1.1 riastrad
2632 1.1 riastrad /**
2633 1.1 riastrad * drm_mode_setcrtc - set CRTC configuration
2634 1.3 riastrad * @dev: drm device for the ioctl
2635 1.3 riastrad * @data: data pointer for the ioctl
2636 1.3 riastrad * @file_priv: drm file for the ioctl call
2637 1.1 riastrad *
2638 1.1 riastrad * Build a new CRTC configuration based on user request.
2639 1.1 riastrad *
2640 1.1 riastrad * Called by the user via ioctl.
2641 1.1 riastrad *
2642 1.3 riastrad * Returns:
2643 1.5 riastrad * Zero on success, negative errno on failure.
2644 1.1 riastrad */
2645 1.1 riastrad int drm_mode_setcrtc(struct drm_device *dev, void *data,
2646 1.1 riastrad struct drm_file *file_priv)
2647 1.1 riastrad {
2648 1.1 riastrad struct drm_mode_config *config = &dev->mode_config;
2649 1.1 riastrad struct drm_mode_crtc *crtc_req = data;
2650 1.1 riastrad struct drm_crtc *crtc;
2651 1.1 riastrad struct drm_connector **connector_set = NULL, *connector;
2652 1.1 riastrad struct drm_framebuffer *fb = NULL;
2653 1.1 riastrad struct drm_display_mode *mode = NULL;
2654 1.1 riastrad struct drm_mode_set set;
2655 1.1 riastrad uint32_t __user *set_connectors_ptr;
2656 1.1 riastrad int ret;
2657 1.1 riastrad int i;
2658 1.1 riastrad
2659 1.1 riastrad if (!drm_core_check_feature(dev, DRIVER_MODESET))
2660 1.1 riastrad return -EINVAL;
2661 1.1 riastrad
2662 1.5 riastrad /*
2663 1.5 riastrad * Universal plane src offsets are only 16.16, prevent havoc for
2664 1.5 riastrad * drivers using universal plane code internally.
2665 1.5 riastrad */
2666 1.5 riastrad if (crtc_req->x & 0xffff0000 || crtc_req->y & 0xffff0000)
2667 1.1 riastrad return -ERANGE;
2668 1.1 riastrad
2669 1.3 riastrad drm_modeset_lock_all(dev);
2670 1.5 riastrad crtc = drm_crtc_find(dev, crtc_req->crtc_id);
2671 1.5 riastrad if (!crtc) {
2672 1.1 riastrad DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
2673 1.3 riastrad ret = -ENOENT;
2674 1.1 riastrad goto out;
2675 1.1 riastrad }
2676 1.1 riastrad DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
2677 1.1 riastrad
2678 1.1 riastrad if (crtc_req->mode_valid) {
2679 1.1 riastrad /* If we have a mode we need a framebuffer. */
2680 1.1 riastrad /* If we pass -1, set the mode with the currently bound fb */
2681 1.1 riastrad if (crtc_req->fb_id == -1) {
2682 1.3 riastrad if (!crtc->primary->fb) {
2683 1.1 riastrad DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2684 1.1 riastrad ret = -EINVAL;
2685 1.1 riastrad goto out;
2686 1.1 riastrad }
2687 1.3 riastrad fb = crtc->primary->fb;
2688 1.3 riastrad /* Make refcounting symmetric with the lookup path. */
2689 1.3 riastrad drm_framebuffer_reference(fb);
2690 1.1 riastrad } else {
2691 1.3 riastrad fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2692 1.3 riastrad if (!fb) {
2693 1.1 riastrad DRM_DEBUG_KMS("Unknown FB ID%d\n",
2694 1.1 riastrad crtc_req->fb_id);
2695 1.3 riastrad ret = -ENOENT;
2696 1.1 riastrad goto out;
2697 1.1 riastrad }
2698 1.1 riastrad }
2699 1.1 riastrad
2700 1.1 riastrad mode = drm_mode_create(dev);
2701 1.1 riastrad if (!mode) {
2702 1.1 riastrad ret = -ENOMEM;
2703 1.1 riastrad goto out;
2704 1.1 riastrad }
2705 1.1 riastrad
2706 1.5 riastrad ret = drm_mode_convert_umode(mode, &crtc_req->mode);
2707 1.1 riastrad if (ret) {
2708 1.1 riastrad DRM_DEBUG_KMS("Invalid mode\n");
2709 1.1 riastrad goto out;
2710 1.1 riastrad }
2711 1.1 riastrad
2712 1.5 riastrad /*
2713 1.5 riastrad * Check whether the primary plane supports the fb pixel format.
2714 1.5 riastrad * Drivers not implementing the universal planes API use a
2715 1.5 riastrad * default formats list provided by the DRM core which doesn't
2716 1.5 riastrad * match real hardware capabilities. Skip the check in that
2717 1.5 riastrad * case.
2718 1.5 riastrad */
2719 1.5 riastrad if (!crtc->primary->format_default) {
2720 1.5 riastrad ret = drm_plane_check_pixel_format(crtc->primary,
2721 1.5 riastrad fb->pixel_format);
2722 1.5 riastrad if (ret) {
2723 1.5 riastrad DRM_DEBUG_KMS("Invalid pixel format %s\n",
2724 1.5 riastrad drm_get_format_name(fb->pixel_format));
2725 1.5 riastrad goto out;
2726 1.5 riastrad }
2727 1.5 riastrad }
2728 1.1 riastrad
2729 1.3 riastrad ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
2730 1.3 riastrad mode, fb);
2731 1.3 riastrad if (ret)
2732 1.3 riastrad goto out;
2733 1.1 riastrad
2734 1.1 riastrad }
2735 1.1 riastrad
2736 1.1 riastrad if (crtc_req->count_connectors == 0 && mode) {
2737 1.1 riastrad DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
2738 1.1 riastrad ret = -EINVAL;
2739 1.1 riastrad goto out;
2740 1.1 riastrad }
2741 1.1 riastrad
2742 1.1 riastrad if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
2743 1.1 riastrad DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
2744 1.1 riastrad crtc_req->count_connectors);
2745 1.1 riastrad ret = -EINVAL;
2746 1.1 riastrad goto out;
2747 1.1 riastrad }
2748 1.1 riastrad
2749 1.1 riastrad if (crtc_req->count_connectors > 0) {
2750 1.1 riastrad u32 out_id;
2751 1.1 riastrad
2752 1.1 riastrad /* Avoid unbounded kernel memory allocation */
2753 1.1 riastrad if (crtc_req->count_connectors > config->num_connector) {
2754 1.1 riastrad ret = -EINVAL;
2755 1.1 riastrad goto out;
2756 1.1 riastrad }
2757 1.1 riastrad
2758 1.5 riastrad connector_set = kmalloc_array(crtc_req->count_connectors,
2759 1.5 riastrad sizeof(struct drm_connector *),
2760 1.5 riastrad GFP_KERNEL);
2761 1.1 riastrad if (!connector_set) {
2762 1.1 riastrad ret = -ENOMEM;
2763 1.1 riastrad goto out;
2764 1.1 riastrad }
2765 1.1 riastrad
2766 1.1 riastrad for (i = 0; i < crtc_req->count_connectors; i++) {
2767 1.1 riastrad set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
2768 1.1 riastrad if (get_user(out_id, &set_connectors_ptr[i])) {
2769 1.1 riastrad ret = -EFAULT;
2770 1.1 riastrad goto out;
2771 1.1 riastrad }
2772 1.1 riastrad
2773 1.5 riastrad connector = drm_connector_find(dev, out_id);
2774 1.5 riastrad if (!connector) {
2775 1.1 riastrad DRM_DEBUG_KMS("Connector id %d unknown\n",
2776 1.1 riastrad out_id);
2777 1.3 riastrad ret = -ENOENT;
2778 1.1 riastrad goto out;
2779 1.1 riastrad }
2780 1.1 riastrad DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2781 1.1 riastrad connector->base.id,
2782 1.5 riastrad connector->name);
2783 1.1 riastrad
2784 1.1 riastrad connector_set[i] = connector;
2785 1.1 riastrad }
2786 1.1 riastrad }
2787 1.1 riastrad
2788 1.1 riastrad set.crtc = crtc;
2789 1.1 riastrad set.x = crtc_req->x;
2790 1.1 riastrad set.y = crtc_req->y;
2791 1.1 riastrad set.mode = mode;
2792 1.1 riastrad set.connectors = connector_set;
2793 1.1 riastrad set.num_connectors = crtc_req->count_connectors;
2794 1.1 riastrad set.fb = fb;
2795 1.3 riastrad ret = drm_mode_set_config_internal(&set);
2796 1.1 riastrad
2797 1.1 riastrad out:
2798 1.3 riastrad if (fb)
2799 1.3 riastrad drm_framebuffer_unreference(fb);
2800 1.3 riastrad
2801 1.1 riastrad kfree(connector_set);
2802 1.1 riastrad drm_mode_destroy(dev, mode);
2803 1.3 riastrad drm_modeset_unlock_all(dev);
2804 1.1 riastrad return ret;
2805 1.1 riastrad }
2806 1.1 riastrad
2807 1.5 riastrad /**
2808 1.5 riastrad * drm_mode_cursor_universal - translate legacy cursor ioctl call into a
2809 1.5 riastrad * universal plane handler call
2810 1.5 riastrad * @crtc: crtc to update cursor for
2811 1.5 riastrad * @req: data pointer for the ioctl
2812 1.5 riastrad * @file_priv: drm file for the ioctl call
2813 1.5 riastrad *
2814 1.5 riastrad * Legacy cursor ioctl's work directly with driver buffer handles. To
2815 1.5 riastrad * translate legacy ioctl calls into universal plane handler calls, we need to
2816 1.5 riastrad * wrap the native buffer handle in a drm_framebuffer.
2817 1.5 riastrad *
2818 1.5 riastrad * Note that we assume any handle passed to the legacy ioctls was a 32-bit ARGB
2819 1.5 riastrad * buffer with a pitch of 4*width; the universal plane interface should be used
2820 1.5 riastrad * directly in cases where the hardware can support other buffer settings and
2821 1.5 riastrad * userspace wants to make use of these capabilities.
2822 1.5 riastrad *
2823 1.5 riastrad * Returns:
2824 1.5 riastrad * Zero on success, negative errno on failure.
2825 1.5 riastrad */
2826 1.5 riastrad static int drm_mode_cursor_universal(struct drm_crtc *crtc,
2827 1.5 riastrad struct drm_mode_cursor2 *req,
2828 1.5 riastrad struct drm_file *file_priv)
2829 1.5 riastrad {
2830 1.5 riastrad struct drm_device *dev = crtc->dev;
2831 1.5 riastrad struct drm_framebuffer *fb = NULL;
2832 1.5 riastrad struct drm_mode_fb_cmd2 fbreq = {
2833 1.5 riastrad .width = req->width,
2834 1.5 riastrad .height = req->height,
2835 1.5 riastrad .pixel_format = DRM_FORMAT_ARGB8888,
2836 1.5 riastrad .pitches = { req->width * 4 },
2837 1.5 riastrad .handles = { req->handle },
2838 1.5 riastrad };
2839 1.5 riastrad int32_t crtc_x, crtc_y;
2840 1.5 riastrad uint32_t crtc_w = 0, crtc_h = 0;
2841 1.5 riastrad uint32_t src_w = 0, src_h = 0;
2842 1.5 riastrad int ret = 0;
2843 1.5 riastrad
2844 1.5 riastrad BUG_ON(!crtc->cursor);
2845 1.5 riastrad WARN_ON(crtc->cursor->crtc != crtc && crtc->cursor->crtc != NULL);
2846 1.5 riastrad
2847 1.5 riastrad /*
2848 1.5 riastrad * Obtain fb we'll be using (either new or existing) and take an extra
2849 1.5 riastrad * reference to it if fb != null. setplane will take care of dropping
2850 1.5 riastrad * the reference if the plane update fails.
2851 1.5 riastrad */
2852 1.5 riastrad if (req->flags & DRM_MODE_CURSOR_BO) {
2853 1.5 riastrad if (req->handle) {
2854 1.5 riastrad fb = internal_framebuffer_create(dev, &fbreq, file_priv);
2855 1.5 riastrad if (IS_ERR(fb)) {
2856 1.5 riastrad DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
2857 1.5 riastrad return PTR_ERR(fb);
2858 1.5 riastrad }
2859 1.5 riastrad } else {
2860 1.5 riastrad fb = NULL;
2861 1.5 riastrad }
2862 1.5 riastrad } else {
2863 1.5 riastrad fb = crtc->cursor->fb;
2864 1.5 riastrad if (fb)
2865 1.5 riastrad drm_framebuffer_reference(fb);
2866 1.5 riastrad }
2867 1.5 riastrad
2868 1.5 riastrad if (req->flags & DRM_MODE_CURSOR_MOVE) {
2869 1.5 riastrad crtc_x = req->x;
2870 1.5 riastrad crtc_y = req->y;
2871 1.5 riastrad } else {
2872 1.5 riastrad crtc_x = crtc->cursor_x;
2873 1.5 riastrad crtc_y = crtc->cursor_y;
2874 1.5 riastrad }
2875 1.5 riastrad
2876 1.5 riastrad if (fb) {
2877 1.5 riastrad crtc_w = fb->width;
2878 1.5 riastrad crtc_h = fb->height;
2879 1.5 riastrad src_w = fb->width << 16;
2880 1.5 riastrad src_h = fb->height << 16;
2881 1.5 riastrad }
2882 1.5 riastrad
2883 1.5 riastrad /*
2884 1.5 riastrad * setplane_internal will take care of deref'ing either the old or new
2885 1.5 riastrad * framebuffer depending on success.
2886 1.5 riastrad */
2887 1.5 riastrad ret = __setplane_internal(crtc->cursor, crtc, fb,
2888 1.5 riastrad crtc_x, crtc_y, crtc_w, crtc_h,
2889 1.5 riastrad 0, 0, src_w, src_h);
2890 1.5 riastrad
2891 1.5 riastrad /* Update successful; save new cursor position, if necessary */
2892 1.5 riastrad if (ret == 0 && req->flags & DRM_MODE_CURSOR_MOVE) {
2893 1.5 riastrad crtc->cursor_x = req->x;
2894 1.5 riastrad crtc->cursor_y = req->y;
2895 1.5 riastrad }
2896 1.5 riastrad
2897 1.5 riastrad return ret;
2898 1.5 riastrad }
2899 1.5 riastrad
2900 1.3 riastrad static int drm_mode_cursor_common(struct drm_device *dev,
2901 1.3 riastrad struct drm_mode_cursor2 *req,
2902 1.3 riastrad struct drm_file *file_priv)
2903 1.1 riastrad {
2904 1.1 riastrad struct drm_crtc *crtc;
2905 1.1 riastrad int ret = 0;
2906 1.1 riastrad
2907 1.1 riastrad if (!drm_core_check_feature(dev, DRIVER_MODESET))
2908 1.1 riastrad return -EINVAL;
2909 1.1 riastrad
2910 1.1 riastrad if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
2911 1.1 riastrad return -EINVAL;
2912 1.1 riastrad
2913 1.5 riastrad crtc = drm_crtc_find(dev, req->crtc_id);
2914 1.5 riastrad if (!crtc) {
2915 1.1 riastrad DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
2916 1.3 riastrad return -ENOENT;
2917 1.1 riastrad }
2918 1.1 riastrad
2919 1.5 riastrad /*
2920 1.5 riastrad * If this crtc has a universal cursor plane, call that plane's update
2921 1.5 riastrad * handler rather than using legacy cursor handlers.
2922 1.5 riastrad */
2923 1.5 riastrad drm_modeset_lock_crtc(crtc, crtc->cursor);
2924 1.5 riastrad if (crtc->cursor) {
2925 1.5 riastrad ret = drm_mode_cursor_universal(crtc, req, file_priv);
2926 1.5 riastrad goto out;
2927 1.5 riastrad }
2928 1.5 riastrad
2929 1.1 riastrad if (req->flags & DRM_MODE_CURSOR_BO) {
2930 1.3 riastrad if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
2931 1.1 riastrad ret = -ENXIO;
2932 1.1 riastrad goto out;
2933 1.1 riastrad }
2934 1.1 riastrad /* Turns off the cursor if handle is 0 */
2935 1.3 riastrad if (crtc->funcs->cursor_set2)
2936 1.3 riastrad ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
2937 1.3 riastrad req->width, req->height, req->hot_x, req->hot_y);
2938 1.3 riastrad else
2939 1.3 riastrad ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2940 1.3 riastrad req->width, req->height);
2941 1.1 riastrad }
2942 1.1 riastrad
2943 1.1 riastrad if (req->flags & DRM_MODE_CURSOR_MOVE) {
2944 1.1 riastrad if (crtc->funcs->cursor_move) {
2945 1.1 riastrad ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2946 1.1 riastrad } else {
2947 1.1 riastrad ret = -EFAULT;
2948 1.1 riastrad goto out;
2949 1.1 riastrad }
2950 1.1 riastrad }
2951 1.1 riastrad out:
2952 1.5 riastrad drm_modeset_unlock_crtc(crtc);
2953 1.3 riastrad
2954 1.1 riastrad return ret;
2955 1.3 riastrad
2956 1.3 riastrad }
2957 1.3 riastrad
2958 1.3 riastrad
2959 1.3 riastrad /**
2960 1.3 riastrad * drm_mode_cursor_ioctl - set CRTC's cursor configuration
2961 1.3 riastrad * @dev: drm device for the ioctl
2962 1.3 riastrad * @data: data pointer for the ioctl
2963 1.3 riastrad * @file_priv: drm file for the ioctl call
2964 1.3 riastrad *
2965 1.3 riastrad * Set the cursor configuration based on user request.
2966 1.3 riastrad *
2967 1.3 riastrad * Called by the user via ioctl.
2968 1.3 riastrad *
2969 1.3 riastrad * Returns:
2970 1.5 riastrad * Zero on success, negative errno on failure.
2971 1.3 riastrad */
2972 1.3 riastrad int drm_mode_cursor_ioctl(struct drm_device *dev,
2973 1.3 riastrad void *data, struct drm_file *file_priv)
2974 1.3 riastrad {
2975 1.3 riastrad struct drm_mode_cursor *req = data;
2976 1.3 riastrad struct drm_mode_cursor2 new_req;
2977 1.3 riastrad
2978 1.3 riastrad memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
2979 1.3 riastrad new_req.hot_x = new_req.hot_y = 0;
2980 1.3 riastrad
2981 1.3 riastrad return drm_mode_cursor_common(dev, &new_req, file_priv);
2982 1.3 riastrad }
2983 1.3 riastrad
2984 1.3 riastrad /**
2985 1.3 riastrad * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
2986 1.3 riastrad * @dev: drm device for the ioctl
2987 1.3 riastrad * @data: data pointer for the ioctl
2988 1.3 riastrad * @file_priv: drm file for the ioctl call
2989 1.3 riastrad *
2990 1.3 riastrad * Set the cursor configuration based on user request. This implements the 2nd
2991 1.3 riastrad * version of the cursor ioctl, which allows userspace to additionally specify
2992 1.3 riastrad * the hotspot of the pointer.
2993 1.3 riastrad *
2994 1.3 riastrad * Called by the user via ioctl.
2995 1.3 riastrad *
2996 1.3 riastrad * Returns:
2997 1.5 riastrad * Zero on success, negative errno on failure.
2998 1.3 riastrad */
2999 1.3 riastrad int drm_mode_cursor2_ioctl(struct drm_device *dev,
3000 1.3 riastrad void *data, struct drm_file *file_priv)
3001 1.3 riastrad {
3002 1.3 riastrad struct drm_mode_cursor2 *req = data;
3003 1.5 riastrad
3004 1.3 riastrad return drm_mode_cursor_common(dev, req, file_priv);
3005 1.1 riastrad }
3006 1.1 riastrad
3007 1.3 riastrad /**
3008 1.3 riastrad * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
3009 1.3 riastrad * @bpp: bits per pixels
3010 1.3 riastrad * @depth: bit depth per pixel
3011 1.3 riastrad *
3012 1.3 riastrad * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
3013 1.3 riastrad * Useful in fbdev emulation code, since that deals in those values.
3014 1.3 riastrad */
3015 1.1 riastrad uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
3016 1.1 riastrad {
3017 1.1 riastrad uint32_t fmt;
3018 1.1 riastrad
3019 1.1 riastrad switch (bpp) {
3020 1.1 riastrad case 8:
3021 1.3 riastrad fmt = DRM_FORMAT_C8;
3022 1.1 riastrad break;
3023 1.1 riastrad case 16:
3024 1.1 riastrad if (depth == 15)
3025 1.1 riastrad fmt = DRM_FORMAT_XRGB1555;
3026 1.1 riastrad else
3027 1.1 riastrad fmt = DRM_FORMAT_RGB565;
3028 1.1 riastrad break;
3029 1.1 riastrad case 24:
3030 1.1 riastrad fmt = DRM_FORMAT_RGB888;
3031 1.1 riastrad break;
3032 1.1 riastrad case 32:
3033 1.1 riastrad if (depth == 24)
3034 1.1 riastrad fmt = DRM_FORMAT_XRGB8888;
3035 1.1 riastrad else if (depth == 30)
3036 1.1 riastrad fmt = DRM_FORMAT_XRGB2101010;
3037 1.1 riastrad else
3038 1.1 riastrad fmt = DRM_FORMAT_ARGB8888;
3039 1.1 riastrad break;
3040 1.1 riastrad default:
3041 1.1 riastrad DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
3042 1.1 riastrad fmt = DRM_FORMAT_XRGB8888;
3043 1.1 riastrad break;
3044 1.1 riastrad }
3045 1.1 riastrad
3046 1.1 riastrad return fmt;
3047 1.1 riastrad }
3048 1.1 riastrad EXPORT_SYMBOL(drm_mode_legacy_fb_format);
3049 1.1 riastrad
3050 1.1 riastrad /**
3051 1.1 riastrad * drm_mode_addfb - add an FB to the graphics configuration
3052 1.3 riastrad * @dev: drm device for the ioctl
3053 1.3 riastrad * @data: data pointer for the ioctl
3054 1.3 riastrad * @file_priv: drm file for the ioctl call
3055 1.1 riastrad *
3056 1.3 riastrad * Add a new FB to the specified CRTC, given a user request. This is the
3057 1.5 riastrad * original addfb ioctl which only supported RGB formats.
3058 1.1 riastrad *
3059 1.1 riastrad * Called by the user via ioctl.
3060 1.1 riastrad *
3061 1.3 riastrad * Returns:
3062 1.5 riastrad * Zero on success, negative errno on failure.
3063 1.1 riastrad */
3064 1.1 riastrad int drm_mode_addfb(struct drm_device *dev,
3065 1.1 riastrad void *data, struct drm_file *file_priv)
3066 1.1 riastrad {
3067 1.1 riastrad struct drm_mode_fb_cmd *or = data;
3068 1.2 riastrad static const struct drm_mode_fb_cmd2 zero_fbcmd;
3069 1.2 riastrad struct drm_mode_fb_cmd2 r = zero_fbcmd;
3070 1.8 riastrad int ret;
3071 1.1 riastrad
3072 1.5 riastrad /* convert to new format and call new ioctl */
3073 1.1 riastrad r.fb_id = or->fb_id;
3074 1.1 riastrad r.width = or->width;
3075 1.1 riastrad r.height = or->height;
3076 1.1 riastrad r.pitches[0] = or->pitch;
3077 1.1 riastrad r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
3078 1.1 riastrad r.handles[0] = or->handle;
3079 1.1 riastrad
3080 1.5 riastrad ret = drm_mode_addfb2(dev, &r, file_priv);
3081 1.5 riastrad if (ret)
3082 1.5 riastrad return ret;
3083 1.1 riastrad
3084 1.5 riastrad or->fb_id = r.fb_id;
3085 1.1 riastrad
3086 1.5 riastrad return 0;
3087 1.1 riastrad }
3088 1.1 riastrad
3089 1.1 riastrad static int format_check(const struct drm_mode_fb_cmd2 *r)
3090 1.1 riastrad {
3091 1.1 riastrad uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
3092 1.1 riastrad
3093 1.1 riastrad switch (format) {
3094 1.1 riastrad case DRM_FORMAT_C8:
3095 1.1 riastrad case DRM_FORMAT_RGB332:
3096 1.1 riastrad case DRM_FORMAT_BGR233:
3097 1.1 riastrad case DRM_FORMAT_XRGB4444:
3098 1.1 riastrad case DRM_FORMAT_XBGR4444:
3099 1.1 riastrad case DRM_FORMAT_RGBX4444:
3100 1.1 riastrad case DRM_FORMAT_BGRX4444:
3101 1.1 riastrad case DRM_FORMAT_ARGB4444:
3102 1.1 riastrad case DRM_FORMAT_ABGR4444:
3103 1.1 riastrad case DRM_FORMAT_RGBA4444:
3104 1.1 riastrad case DRM_FORMAT_BGRA4444:
3105 1.1 riastrad case DRM_FORMAT_XRGB1555:
3106 1.1 riastrad case DRM_FORMAT_XBGR1555:
3107 1.1 riastrad case DRM_FORMAT_RGBX5551:
3108 1.1 riastrad case DRM_FORMAT_BGRX5551:
3109 1.1 riastrad case DRM_FORMAT_ARGB1555:
3110 1.1 riastrad case DRM_FORMAT_ABGR1555:
3111 1.1 riastrad case DRM_FORMAT_RGBA5551:
3112 1.1 riastrad case DRM_FORMAT_BGRA5551:
3113 1.1 riastrad case DRM_FORMAT_RGB565:
3114 1.1 riastrad case DRM_FORMAT_BGR565:
3115 1.1 riastrad case DRM_FORMAT_RGB888:
3116 1.1 riastrad case DRM_FORMAT_BGR888:
3117 1.1 riastrad case DRM_FORMAT_XRGB8888:
3118 1.1 riastrad case DRM_FORMAT_XBGR8888:
3119 1.1 riastrad case DRM_FORMAT_RGBX8888:
3120 1.1 riastrad case DRM_FORMAT_BGRX8888:
3121 1.1 riastrad case DRM_FORMAT_ARGB8888:
3122 1.1 riastrad case DRM_FORMAT_ABGR8888:
3123 1.1 riastrad case DRM_FORMAT_RGBA8888:
3124 1.1 riastrad case DRM_FORMAT_BGRA8888:
3125 1.1 riastrad case DRM_FORMAT_XRGB2101010:
3126 1.1 riastrad case DRM_FORMAT_XBGR2101010:
3127 1.1 riastrad case DRM_FORMAT_RGBX1010102:
3128 1.1 riastrad case DRM_FORMAT_BGRX1010102:
3129 1.1 riastrad case DRM_FORMAT_ARGB2101010:
3130 1.1 riastrad case DRM_FORMAT_ABGR2101010:
3131 1.1 riastrad case DRM_FORMAT_RGBA1010102:
3132 1.1 riastrad case DRM_FORMAT_BGRA1010102:
3133 1.1 riastrad case DRM_FORMAT_YUYV:
3134 1.1 riastrad case DRM_FORMAT_YVYU:
3135 1.1 riastrad case DRM_FORMAT_UYVY:
3136 1.1 riastrad case DRM_FORMAT_VYUY:
3137 1.1 riastrad case DRM_FORMAT_AYUV:
3138 1.1 riastrad case DRM_FORMAT_NV12:
3139 1.1 riastrad case DRM_FORMAT_NV21:
3140 1.1 riastrad case DRM_FORMAT_NV16:
3141 1.1 riastrad case DRM_FORMAT_NV61:
3142 1.1 riastrad case DRM_FORMAT_NV24:
3143 1.1 riastrad case DRM_FORMAT_NV42:
3144 1.1 riastrad case DRM_FORMAT_YUV410:
3145 1.1 riastrad case DRM_FORMAT_YVU410:
3146 1.1 riastrad case DRM_FORMAT_YUV411:
3147 1.1 riastrad case DRM_FORMAT_YVU411:
3148 1.1 riastrad case DRM_FORMAT_YUV420:
3149 1.1 riastrad case DRM_FORMAT_YVU420:
3150 1.1 riastrad case DRM_FORMAT_YUV422:
3151 1.1 riastrad case DRM_FORMAT_YVU422:
3152 1.1 riastrad case DRM_FORMAT_YUV444:
3153 1.1 riastrad case DRM_FORMAT_YVU444:
3154 1.1 riastrad return 0;
3155 1.1 riastrad default:
3156 1.3 riastrad DRM_DEBUG_KMS("invalid pixel format %s\n",
3157 1.3 riastrad drm_get_format_name(r->pixel_format));
3158 1.1 riastrad return -EINVAL;
3159 1.1 riastrad }
3160 1.1 riastrad }
3161 1.1 riastrad
3162 1.1 riastrad static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
3163 1.1 riastrad {
3164 1.1 riastrad int ret, hsub, vsub, num_planes, i;
3165 1.1 riastrad
3166 1.1 riastrad ret = format_check(r);
3167 1.1 riastrad if (ret) {
3168 1.3 riastrad DRM_DEBUG_KMS("bad framebuffer format %s\n",
3169 1.3 riastrad drm_get_format_name(r->pixel_format));
3170 1.1 riastrad return ret;
3171 1.1 riastrad }
3172 1.1 riastrad
3173 1.1 riastrad hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
3174 1.1 riastrad vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
3175 1.1 riastrad num_planes = drm_format_num_planes(r->pixel_format);
3176 1.1 riastrad
3177 1.1 riastrad if (r->width == 0 || r->width % hsub) {
3178 1.5 riastrad DRM_DEBUG_KMS("bad framebuffer width %u\n", r->width);
3179 1.1 riastrad return -EINVAL;
3180 1.1 riastrad }
3181 1.1 riastrad
3182 1.1 riastrad if (r->height == 0 || r->height % vsub) {
3183 1.1 riastrad DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
3184 1.1 riastrad return -EINVAL;
3185 1.1 riastrad }
3186 1.1 riastrad
3187 1.1 riastrad for (i = 0; i < num_planes; i++) {
3188 1.1 riastrad unsigned int width = r->width / (i != 0 ? hsub : 1);
3189 1.1 riastrad unsigned int height = r->height / (i != 0 ? vsub : 1);
3190 1.1 riastrad unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
3191 1.1 riastrad
3192 1.1 riastrad if (!r->handles[i]) {
3193 1.1 riastrad DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
3194 1.1 riastrad return -EINVAL;
3195 1.1 riastrad }
3196 1.1 riastrad
3197 1.1 riastrad if ((uint64_t) width * cpp > UINT_MAX)
3198 1.1 riastrad return -ERANGE;
3199 1.1 riastrad
3200 1.1 riastrad if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
3201 1.1 riastrad return -ERANGE;
3202 1.1 riastrad
3203 1.1 riastrad if (r->pitches[i] < width * cpp) {
3204 1.1 riastrad DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
3205 1.1 riastrad return -EINVAL;
3206 1.1 riastrad }
3207 1.5 riastrad
3208 1.5 riastrad if (r->modifier[i] && !(r->flags & DRM_MODE_FB_MODIFIERS)) {
3209 1.8 riastrad DRM_DEBUG_KMS("bad fb modifier %"PRIu64" for plane %d\n",
3210 1.5 riastrad r->modifier[i], i);
3211 1.5 riastrad return -EINVAL;
3212 1.5 riastrad }
3213 1.5 riastrad
3214 1.5 riastrad /* modifier specific checks: */
3215 1.5 riastrad switch (r->modifier[i]) {
3216 1.5 riastrad case DRM_FORMAT_MOD_SAMSUNG_64_32_TILE:
3217 1.5 riastrad /* NOTE: the pitch restriction may be lifted later if it turns
3218 1.5 riastrad * out that no hw has this restriction:
3219 1.5 riastrad */
3220 1.5 riastrad if (r->pixel_format != DRM_FORMAT_NV12 ||
3221 1.5 riastrad width % 128 || height % 32 ||
3222 1.5 riastrad r->pitches[i] % 128) {
3223 1.5 riastrad DRM_DEBUG_KMS("bad modifier data for plane %d\n", i);
3224 1.5 riastrad return -EINVAL;
3225 1.5 riastrad }
3226 1.5 riastrad break;
3227 1.5 riastrad
3228 1.5 riastrad default:
3229 1.5 riastrad break;
3230 1.5 riastrad }
3231 1.5 riastrad }
3232 1.5 riastrad
3233 1.5 riastrad for (i = num_planes; i < 4; i++) {
3234 1.5 riastrad if (r->modifier[i]) {
3235 1.5 riastrad DRM_DEBUG_KMS("non-zero modifier for unused plane %d\n", i);
3236 1.5 riastrad return -EINVAL;
3237 1.5 riastrad }
3238 1.5 riastrad
3239 1.5 riastrad /* Pre-FB_MODIFIERS userspace didn't clear the structs properly. */
3240 1.5 riastrad if (!(r->flags & DRM_MODE_FB_MODIFIERS))
3241 1.5 riastrad continue;
3242 1.5 riastrad
3243 1.5 riastrad if (r->handles[i]) {
3244 1.5 riastrad DRM_DEBUG_KMS("buffer object handle for unused plane %d\n", i);
3245 1.5 riastrad return -EINVAL;
3246 1.5 riastrad }
3247 1.5 riastrad
3248 1.5 riastrad if (r->pitches[i]) {
3249 1.5 riastrad DRM_DEBUG_KMS("non-zero pitch for unused plane %d\n", i);
3250 1.5 riastrad return -EINVAL;
3251 1.5 riastrad }
3252 1.5 riastrad
3253 1.5 riastrad if (r->offsets[i]) {
3254 1.5 riastrad DRM_DEBUG_KMS("non-zero offset for unused plane %d\n", i);
3255 1.5 riastrad return -EINVAL;
3256 1.5 riastrad }
3257 1.1 riastrad }
3258 1.1 riastrad
3259 1.1 riastrad return 0;
3260 1.1 riastrad }
3261 1.1 riastrad
3262 1.5 riastrad static struct drm_framebuffer *
3263 1.5 riastrad internal_framebuffer_create(struct drm_device *dev,
3264 1.5 riastrad struct drm_mode_fb_cmd2 *r,
3265 1.5 riastrad struct drm_file *file_priv)
3266 1.5 riastrad {
3267 1.5 riastrad struct drm_mode_config *config = &dev->mode_config;
3268 1.5 riastrad struct drm_framebuffer *fb;
3269 1.5 riastrad int ret;
3270 1.5 riastrad
3271 1.5 riastrad if (r->flags & ~(DRM_MODE_FB_INTERLACED | DRM_MODE_FB_MODIFIERS)) {
3272 1.5 riastrad DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
3273 1.5 riastrad return ERR_PTR(-EINVAL);
3274 1.5 riastrad }
3275 1.5 riastrad
3276 1.5 riastrad if ((config->min_width > r->width) || (r->width > config->max_width)) {
3277 1.5 riastrad DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
3278 1.5 riastrad r->width, config->min_width, config->max_width);
3279 1.5 riastrad return ERR_PTR(-EINVAL);
3280 1.5 riastrad }
3281 1.5 riastrad if ((config->min_height > r->height) || (r->height > config->max_height)) {
3282 1.5 riastrad DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
3283 1.5 riastrad r->height, config->min_height, config->max_height);
3284 1.5 riastrad return ERR_PTR(-EINVAL);
3285 1.5 riastrad }
3286 1.5 riastrad
3287 1.5 riastrad if (r->flags & DRM_MODE_FB_MODIFIERS &&
3288 1.5 riastrad !dev->mode_config.allow_fb_modifiers) {
3289 1.5 riastrad DRM_DEBUG_KMS("driver does not support fb modifiers\n");
3290 1.5 riastrad return ERR_PTR(-EINVAL);
3291 1.5 riastrad }
3292 1.5 riastrad
3293 1.5 riastrad ret = framebuffer_check(r);
3294 1.5 riastrad if (ret)
3295 1.5 riastrad return ERR_PTR(ret);
3296 1.5 riastrad
3297 1.5 riastrad fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
3298 1.5 riastrad if (IS_ERR(fb)) {
3299 1.5 riastrad DRM_DEBUG_KMS("could not create framebuffer\n");
3300 1.5 riastrad return fb;
3301 1.5 riastrad }
3302 1.5 riastrad
3303 1.5 riastrad return fb;
3304 1.5 riastrad }
3305 1.5 riastrad
3306 1.1 riastrad /**
3307 1.1 riastrad * drm_mode_addfb2 - add an FB to the graphics configuration
3308 1.3 riastrad * @dev: drm device for the ioctl
3309 1.3 riastrad * @data: data pointer for the ioctl
3310 1.3 riastrad * @file_priv: drm file for the ioctl call
3311 1.3 riastrad *
3312 1.3 riastrad * Add a new FB to the specified CRTC, given a user request with format. This is
3313 1.3 riastrad * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
3314 1.3 riastrad * and uses fourcc codes as pixel format specifiers.
3315 1.1 riastrad *
3316 1.1 riastrad * Called by the user via ioctl.
3317 1.1 riastrad *
3318 1.3 riastrad * Returns:
3319 1.5 riastrad * Zero on success, negative errno on failure.
3320 1.1 riastrad */
3321 1.1 riastrad int drm_mode_addfb2(struct drm_device *dev,
3322 1.1 riastrad void *data, struct drm_file *file_priv)
3323 1.1 riastrad {
3324 1.1 riastrad struct drm_mode_fb_cmd2 *r = data;
3325 1.1 riastrad struct drm_framebuffer *fb;
3326 1.1 riastrad
3327 1.1 riastrad if (!drm_core_check_feature(dev, DRIVER_MODESET))
3328 1.1 riastrad return -EINVAL;
3329 1.1 riastrad
3330 1.5 riastrad fb = internal_framebuffer_create(dev, r, file_priv);
3331 1.5 riastrad if (IS_ERR(fb))
3332 1.3 riastrad return PTR_ERR(fb);
3333 1.1 riastrad
3334 1.5 riastrad /* Transfer ownership to the filp for reaping on close */
3335 1.5 riastrad
3336 1.5 riastrad DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
3337 1.3 riastrad mutex_lock(&file_priv->fbs_lock);
3338 1.1 riastrad r->fb_id = fb->base.id;
3339 1.1 riastrad list_add(&fb->filp_head, &file_priv->fbs);
3340 1.3 riastrad mutex_unlock(&file_priv->fbs_lock);
3341 1.3 riastrad
3342 1.5 riastrad return 0;
3343 1.5 riastrad }
3344 1.5 riastrad
3345 1.5 riastrad struct drm_mode_rmfb_work {
3346 1.5 riastrad struct work_struct work;
3347 1.5 riastrad struct list_head fbs;
3348 1.5 riastrad };
3349 1.5 riastrad
3350 1.5 riastrad static void drm_mode_rmfb_work_fn(struct work_struct *w)
3351 1.5 riastrad {
3352 1.5 riastrad struct drm_mode_rmfb_work *arg = container_of(w, typeof(*arg), work);
3353 1.5 riastrad
3354 1.5 riastrad while (!list_empty(&arg->fbs)) {
3355 1.5 riastrad struct drm_framebuffer *fb =
3356 1.5 riastrad list_first_entry(&arg->fbs, typeof(*fb), filp_head);
3357 1.1 riastrad
3358 1.5 riastrad list_del_init(&fb->filp_head);
3359 1.5 riastrad drm_framebuffer_remove(fb);
3360 1.5 riastrad }
3361 1.1 riastrad }
3362 1.1 riastrad
3363 1.1 riastrad /**
3364 1.1 riastrad * drm_mode_rmfb - remove an FB from the configuration
3365 1.3 riastrad * @dev: drm device for the ioctl
3366 1.3 riastrad * @data: data pointer for the ioctl
3367 1.3 riastrad * @file_priv: drm file for the ioctl call
3368 1.1 riastrad *
3369 1.1 riastrad * Remove the FB specified by the user.
3370 1.1 riastrad *
3371 1.1 riastrad * Called by the user via ioctl.
3372 1.1 riastrad *
3373 1.3 riastrad * Returns:
3374 1.5 riastrad * Zero on success, negative errno on failure.
3375 1.1 riastrad */
3376 1.1 riastrad int drm_mode_rmfb(struct drm_device *dev,
3377 1.1 riastrad void *data, struct drm_file *file_priv)
3378 1.1 riastrad {
3379 1.1 riastrad struct drm_framebuffer *fb = NULL;
3380 1.1 riastrad struct drm_framebuffer *fbl = NULL;
3381 1.1 riastrad uint32_t *id = data;
3382 1.1 riastrad int found = 0;
3383 1.1 riastrad
3384 1.1 riastrad if (!drm_core_check_feature(dev, DRIVER_MODESET))
3385 1.1 riastrad return -EINVAL;
3386 1.1 riastrad
3387 1.3 riastrad mutex_lock(&file_priv->fbs_lock);
3388 1.3 riastrad mutex_lock(&dev->mode_config.fb_lock);
3389 1.3 riastrad fb = __drm_framebuffer_lookup(dev, *id);
3390 1.3 riastrad if (!fb)
3391 1.3 riastrad goto fail_lookup;
3392 1.1 riastrad
3393 1.1 riastrad list_for_each_entry(fbl, &file_priv->fbs, filp_head)
3394 1.1 riastrad if (fb == fbl)
3395 1.1 riastrad found = 1;
3396 1.3 riastrad if (!found)
3397 1.3 riastrad goto fail_lookup;
3398 1.1 riastrad
3399 1.3 riastrad list_del_init(&fb->filp_head);
3400 1.3 riastrad mutex_unlock(&dev->mode_config.fb_lock);
3401 1.3 riastrad mutex_unlock(&file_priv->fbs_lock);
3402 1.1 riastrad
3403 1.5 riastrad /*
3404 1.5 riastrad * we now own the reference that was stored in the fbs list
3405 1.5 riastrad *
3406 1.5 riastrad * drm_framebuffer_remove may fail with -EINTR on pending signals,
3407 1.5 riastrad * so run this in a separate stack as there's no way to correctly
3408 1.5 riastrad * handle this after the fb is already removed from the lookup table.
3409 1.5 riastrad */
3410 1.6 riastrad if (!kref_exclusive_p(&fb->refcount)) {
3411 1.5 riastrad struct drm_mode_rmfb_work arg;
3412 1.5 riastrad
3413 1.5 riastrad INIT_WORK_ONSTACK(&arg.work, drm_mode_rmfb_work_fn);
3414 1.5 riastrad INIT_LIST_HEAD(&arg.fbs);
3415 1.5 riastrad list_add_tail(&fb->filp_head, &arg.fbs);
3416 1.5 riastrad
3417 1.5 riastrad schedule_work(&arg.work);
3418 1.5 riastrad flush_work(&arg.work);
3419 1.5 riastrad destroy_work_on_stack(&arg.work);
3420 1.5 riastrad } else
3421 1.5 riastrad drm_framebuffer_unreference(fb);
3422 1.1 riastrad
3423 1.3 riastrad return 0;
3424 1.3 riastrad
3425 1.3 riastrad fail_lookup:
3426 1.3 riastrad mutex_unlock(&dev->mode_config.fb_lock);
3427 1.3 riastrad mutex_unlock(&file_priv->fbs_lock);
3428 1.3 riastrad
3429 1.3 riastrad return -ENOENT;
3430 1.1 riastrad }
3431 1.1 riastrad
3432 1.1 riastrad /**
3433 1.1 riastrad * drm_mode_getfb - get FB info
3434 1.3 riastrad * @dev: drm device for the ioctl
3435 1.3 riastrad * @data: data pointer for the ioctl
3436 1.3 riastrad * @file_priv: drm file for the ioctl call
3437 1.1 riastrad *
3438 1.1 riastrad * Lookup the FB given its ID and return info about it.
3439 1.1 riastrad *
3440 1.1 riastrad * Called by the user via ioctl.
3441 1.1 riastrad *
3442 1.3 riastrad * Returns:
3443 1.5 riastrad * Zero on success, negative errno on failure.
3444 1.1 riastrad */
3445 1.1 riastrad int drm_mode_getfb(struct drm_device *dev,
3446 1.1 riastrad void *data, struct drm_file *file_priv)
3447 1.1 riastrad {
3448 1.1 riastrad struct drm_mode_fb_cmd *r = data;
3449 1.1 riastrad struct drm_framebuffer *fb;
3450 1.3 riastrad int ret;
3451 1.1 riastrad
3452 1.1 riastrad if (!drm_core_check_feature(dev, DRIVER_MODESET))
3453 1.1 riastrad return -EINVAL;
3454 1.1 riastrad
3455 1.3 riastrad fb = drm_framebuffer_lookup(dev, r->fb_id);
3456 1.3 riastrad if (!fb)
3457 1.3 riastrad return -ENOENT;
3458 1.1 riastrad
3459 1.1 riastrad r->height = fb->height;
3460 1.1 riastrad r->width = fb->width;
3461 1.1 riastrad r->depth = fb->depth;
3462 1.1 riastrad r->bpp = fb->bits_per_pixel;
3463 1.1 riastrad r->pitch = fb->pitches[0];
3464 1.3 riastrad if (fb->funcs->create_handle) {
3465 1.4 riastrad if (file_priv->is_master ||
3466 1.4 riastrad #ifdef __NetBSD__
3467 1.4 riastrad DRM_SUSER() ||
3468 1.4 riastrad #else
3469 1.4 riastrad capable(CAP_SYS_ADMIN) ||
3470 1.4 riastrad #endif
3471 1.3 riastrad drm_is_control_client(file_priv)) {
3472 1.3 riastrad ret = fb->funcs->create_handle(fb, file_priv,
3473 1.3 riastrad &r->handle);
3474 1.3 riastrad } else {
3475 1.3 riastrad /* GET_FB() is an unprivileged ioctl so we must not
3476 1.3 riastrad * return a buffer-handle to non-master processes! For
3477 1.3 riastrad * backwards-compatibility reasons, we cannot make
3478 1.3 riastrad * GET_FB() privileged, so just return an invalid handle
3479 1.3 riastrad * for non-masters. */
3480 1.3 riastrad r->handle = 0;
3481 1.3 riastrad ret = 0;
3482 1.3 riastrad }
3483 1.3 riastrad } else {
3484 1.3 riastrad ret = -ENODEV;
3485 1.3 riastrad }
3486 1.3 riastrad
3487 1.3 riastrad drm_framebuffer_unreference(fb);
3488 1.1 riastrad
3489 1.1 riastrad return ret;
3490 1.1 riastrad }
3491 1.1 riastrad
3492 1.3 riastrad /**
3493 1.3 riastrad * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
3494 1.3 riastrad * @dev: drm device for the ioctl
3495 1.3 riastrad * @data: data pointer for the ioctl
3496 1.3 riastrad * @file_priv: drm file for the ioctl call
3497 1.3 riastrad *
3498 1.3 riastrad * Lookup the FB and flush out the damaged area supplied by userspace as a clip
3499 1.3 riastrad * rectangle list. Generic userspace which does frontbuffer rendering must call
3500 1.3 riastrad * this ioctl to flush out the changes on manual-update display outputs, e.g.
3501 1.3 riastrad * usb display-link, mipi manual update panels or edp panel self refresh modes.
3502 1.3 riastrad *
3503 1.3 riastrad * Modesetting drivers which always update the frontbuffer do not need to
3504 1.3 riastrad * implement the corresponding ->dirty framebuffer callback.
3505 1.3 riastrad *
3506 1.3 riastrad * Called by the user via ioctl.
3507 1.3 riastrad *
3508 1.3 riastrad * Returns:
3509 1.5 riastrad * Zero on success, negative errno on failure.
3510 1.3 riastrad */
3511 1.1 riastrad int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
3512 1.1 riastrad void *data, struct drm_file *file_priv)
3513 1.1 riastrad {
3514 1.1 riastrad struct drm_clip_rect __user *clips_ptr;
3515 1.1 riastrad struct drm_clip_rect *clips = NULL;
3516 1.1 riastrad struct drm_mode_fb_dirty_cmd *r = data;
3517 1.1 riastrad struct drm_framebuffer *fb;
3518 1.1 riastrad unsigned flags;
3519 1.1 riastrad int num_clips;
3520 1.1 riastrad int ret;
3521 1.1 riastrad
3522 1.1 riastrad if (!drm_core_check_feature(dev, DRIVER_MODESET))
3523 1.1 riastrad return -EINVAL;
3524 1.1 riastrad
3525 1.3 riastrad fb = drm_framebuffer_lookup(dev, r->fb_id);
3526 1.3 riastrad if (!fb)
3527 1.3 riastrad return -ENOENT;
3528 1.1 riastrad
3529 1.1 riastrad num_clips = r->num_clips;
3530 1.1 riastrad clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
3531 1.1 riastrad
3532 1.1 riastrad if (!num_clips != !clips_ptr) {
3533 1.1 riastrad ret = -EINVAL;
3534 1.1 riastrad goto out_err1;
3535 1.1 riastrad }
3536 1.1 riastrad
3537 1.1 riastrad flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
3538 1.1 riastrad
3539 1.1 riastrad /* If userspace annotates copy, clips must come in pairs */
3540 1.1 riastrad if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
3541 1.1 riastrad ret = -EINVAL;
3542 1.1 riastrad goto out_err1;
3543 1.1 riastrad }
3544 1.1 riastrad
3545 1.1 riastrad if (num_clips && clips_ptr) {
3546 1.1 riastrad if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
3547 1.1 riastrad ret = -EINVAL;
3548 1.1 riastrad goto out_err1;
3549 1.1 riastrad }
3550 1.5 riastrad clips = kcalloc(num_clips, sizeof(*clips), GFP_KERNEL);
3551 1.1 riastrad if (!clips) {
3552 1.1 riastrad ret = -ENOMEM;
3553 1.1 riastrad goto out_err1;
3554 1.1 riastrad }
3555 1.1 riastrad
3556 1.1 riastrad ret = copy_from_user(clips, clips_ptr,
3557 1.1 riastrad num_clips * sizeof(*clips));
3558 1.1 riastrad if (ret) {
3559 1.1 riastrad ret = -EFAULT;
3560 1.1 riastrad goto out_err2;
3561 1.1 riastrad }
3562 1.1 riastrad }
3563 1.1 riastrad
3564 1.1 riastrad if (fb->funcs->dirty) {
3565 1.1 riastrad ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
3566 1.1 riastrad clips, num_clips);
3567 1.1 riastrad } else {
3568 1.1 riastrad ret = -ENOSYS;
3569 1.1 riastrad }
3570 1.1 riastrad
3571 1.1 riastrad out_err2:
3572 1.1 riastrad kfree(clips);
3573 1.1 riastrad out_err1:
3574 1.3 riastrad drm_framebuffer_unreference(fb);
3575 1.3 riastrad
3576 1.1 riastrad return ret;
3577 1.1 riastrad }
3578 1.1 riastrad
3579 1.1 riastrad /**
3580 1.1 riastrad * drm_fb_release - remove and free the FBs on this file
3581 1.3 riastrad * @priv: drm file for the ioctl
3582 1.1 riastrad *
3583 1.1 riastrad * Destroy all the FBs associated with @filp.
3584 1.1 riastrad *
3585 1.1 riastrad * Called by the user via ioctl.
3586 1.1 riastrad *
3587 1.3 riastrad * Returns:
3588 1.5 riastrad * Zero on success, negative errno on failure.
3589 1.1 riastrad */
3590 1.1 riastrad void drm_fb_release(struct drm_file *priv)
3591 1.1 riastrad {
3592 1.1 riastrad struct drm_framebuffer *fb, *tfb;
3593 1.5 riastrad struct drm_mode_rmfb_work arg;
3594 1.1 riastrad
3595 1.5 riastrad INIT_LIST_HEAD(&arg.fbs);
3596 1.5 riastrad
3597 1.5 riastrad /*
3598 1.5 riastrad * When the file gets released that means no one else can access the fb
3599 1.5 riastrad * list any more, so no need to grab fpriv->fbs_lock. And we need to
3600 1.5 riastrad * avoid upsetting lockdep since the universal cursor code adds a
3601 1.5 riastrad * framebuffer while holding mutex locks.
3602 1.5 riastrad *
3603 1.5 riastrad * Note that a real deadlock between fpriv->fbs_lock and the modeset
3604 1.5 riastrad * locks is impossible here since no one else but this function can get
3605 1.5 riastrad * at it any more.
3606 1.5 riastrad */
3607 1.1 riastrad list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
3608 1.6 riastrad if (!kref_exclusive_p(&fb->refcount)) {
3609 1.5 riastrad list_move_tail(&fb->filp_head, &arg.fbs);
3610 1.5 riastrad } else {
3611 1.5 riastrad list_del_init(&fb->filp_head);
3612 1.1 riastrad
3613 1.5 riastrad /* This drops the fpriv->fbs reference. */
3614 1.5 riastrad drm_framebuffer_unreference(fb);
3615 1.5 riastrad }
3616 1.5 riastrad }
3617 1.1 riastrad
3618 1.5 riastrad if (!list_empty(&arg.fbs)) {
3619 1.5 riastrad INIT_WORK_ONSTACK(&arg.work, drm_mode_rmfb_work_fn);
3620 1.1 riastrad
3621 1.5 riastrad schedule_work(&arg.work);
3622 1.5 riastrad flush_work(&arg.work);
3623 1.5 riastrad destroy_work_on_stack(&arg.work);
3624 1.1 riastrad }
3625 1.1 riastrad }
3626 1.1 riastrad
3627 1.1 riastrad /**
3628 1.3 riastrad * drm_property_create - create a new property type
3629 1.3 riastrad * @dev: drm device
3630 1.3 riastrad * @flags: flags specifying the property type
3631 1.3 riastrad * @name: name of the property
3632 1.3 riastrad * @num_values: number of pre-defined values
3633 1.1 riastrad *
3634 1.3 riastrad * This creates a new generic drm property which can then be attached to a drm
3635 1.3 riastrad * object with drm_object_attach_property. The returned property object must be
3636 1.3 riastrad * freed with drm_property_destroy.
3637 1.1 riastrad *
3638 1.5 riastrad * Note that the DRM core keeps a per-device list of properties and that, if
3639 1.5 riastrad * drm_mode_config_cleanup() is called, it will destroy all properties created
3640 1.5 riastrad * by the driver.
3641 1.5 riastrad *
3642 1.3 riastrad * Returns:
3643 1.3 riastrad * A pointer to the newly created property on success, NULL on failure.
3644 1.1 riastrad */
3645 1.1 riastrad struct drm_property *drm_property_create(struct drm_device *dev, int flags,
3646 1.1 riastrad const char *name, int num_values)
3647 1.1 riastrad {
3648 1.1 riastrad struct drm_property *property = NULL;
3649 1.1 riastrad int ret;
3650 1.1 riastrad
3651 1.1 riastrad property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
3652 1.1 riastrad if (!property)
3653 1.1 riastrad return NULL;
3654 1.1 riastrad
3655 1.5 riastrad property->dev = dev;
3656 1.5 riastrad
3657 1.1 riastrad if (num_values) {
3658 1.5 riastrad property->values = kcalloc(num_values, sizeof(uint64_t),
3659 1.5 riastrad GFP_KERNEL);
3660 1.1 riastrad if (!property->values)
3661 1.1 riastrad goto fail;
3662 1.1 riastrad }
3663 1.1 riastrad
3664 1.1 riastrad ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
3665 1.1 riastrad if (ret)
3666 1.1 riastrad goto fail;
3667 1.1 riastrad
3668 1.1 riastrad property->flags = flags;
3669 1.1 riastrad property->num_values = num_values;
3670 1.5 riastrad INIT_LIST_HEAD(&property->enum_list);
3671 1.1 riastrad
3672 1.1 riastrad if (name) {
3673 1.1 riastrad strncpy(property->name, name, DRM_PROP_NAME_LEN);
3674 1.1 riastrad property->name[DRM_PROP_NAME_LEN-1] = '\0';
3675 1.1 riastrad }
3676 1.1 riastrad
3677 1.1 riastrad list_add_tail(&property->head, &dev->mode_config.property_list);
3678 1.5 riastrad
3679 1.5 riastrad WARN_ON(!drm_property_type_valid(property));
3680 1.5 riastrad
3681 1.1 riastrad return property;
3682 1.1 riastrad fail:
3683 1.1 riastrad kfree(property->values);
3684 1.1 riastrad kfree(property);
3685 1.1 riastrad return NULL;
3686 1.1 riastrad }
3687 1.1 riastrad EXPORT_SYMBOL(drm_property_create);
3688 1.1 riastrad
3689 1.3 riastrad /**
3690 1.5 riastrad * drm_property_create_enum - create a new enumeration property type
3691 1.3 riastrad * @dev: drm device
3692 1.3 riastrad * @flags: flags specifying the property type
3693 1.3 riastrad * @name: name of the property
3694 1.3 riastrad * @props: enumeration lists with property values
3695 1.3 riastrad * @num_values: number of pre-defined values
3696 1.3 riastrad *
3697 1.3 riastrad * This creates a new generic drm property which can then be attached to a drm
3698 1.3 riastrad * object with drm_object_attach_property. The returned property object must be
3699 1.3 riastrad * freed with drm_property_destroy.
3700 1.3 riastrad *
3701 1.3 riastrad * Userspace is only allowed to set one of the predefined values for enumeration
3702 1.3 riastrad * properties.
3703 1.3 riastrad *
3704 1.3 riastrad * Returns:
3705 1.3 riastrad * A pointer to the newly created property on success, NULL on failure.
3706 1.3 riastrad */
3707 1.1 riastrad struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
3708 1.1 riastrad const char *name,
3709 1.1 riastrad const struct drm_prop_enum_list *props,
3710 1.1 riastrad int num_values)
3711 1.1 riastrad {
3712 1.1 riastrad struct drm_property *property;
3713 1.1 riastrad int i, ret;
3714 1.1 riastrad
3715 1.1 riastrad flags |= DRM_MODE_PROP_ENUM;
3716 1.1 riastrad
3717 1.1 riastrad property = drm_property_create(dev, flags, name, num_values);
3718 1.1 riastrad if (!property)
3719 1.1 riastrad return NULL;
3720 1.1 riastrad
3721 1.1 riastrad for (i = 0; i < num_values; i++) {
3722 1.1 riastrad ret = drm_property_add_enum(property, i,
3723 1.1 riastrad props[i].type,
3724 1.1 riastrad props[i].name);
3725 1.1 riastrad if (ret) {
3726 1.1 riastrad drm_property_destroy(dev, property);
3727 1.1 riastrad return NULL;
3728 1.1 riastrad }
3729 1.1 riastrad }
3730 1.1 riastrad
3731 1.1 riastrad return property;
3732 1.1 riastrad }
3733 1.1 riastrad EXPORT_SYMBOL(drm_property_create_enum);
3734 1.1 riastrad
3735 1.3 riastrad /**
3736 1.5 riastrad * drm_property_create_bitmask - create a new bitmask property type
3737 1.3 riastrad * @dev: drm device
3738 1.3 riastrad * @flags: flags specifying the property type
3739 1.3 riastrad * @name: name of the property
3740 1.3 riastrad * @props: enumeration lists with property bitflags
3741 1.5 riastrad * @num_props: size of the @props array
3742 1.5 riastrad * @supported_bits: bitmask of all supported enumeration values
3743 1.3 riastrad *
3744 1.5 riastrad * This creates a new bitmask drm property which can then be attached to a drm
3745 1.3 riastrad * object with drm_object_attach_property. The returned property object must be
3746 1.3 riastrad * freed with drm_property_destroy.
3747 1.3 riastrad *
3748 1.3 riastrad * Compared to plain enumeration properties userspace is allowed to set any
3749 1.3 riastrad * or'ed together combination of the predefined property bitflag values
3750 1.3 riastrad *
3751 1.3 riastrad * Returns:
3752 1.3 riastrad * A pointer to the newly created property on success, NULL on failure.
3753 1.3 riastrad */
3754 1.1 riastrad struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
3755 1.1 riastrad int flags, const char *name,
3756 1.1 riastrad const struct drm_prop_enum_list *props,
3757 1.5 riastrad int num_props,
3758 1.5 riastrad uint64_t supported_bits)
3759 1.1 riastrad {
3760 1.1 riastrad struct drm_property *property;
3761 1.5 riastrad int i, ret, index = 0;
3762 1.5 riastrad int num_values = hweight64(supported_bits);
3763 1.1 riastrad
3764 1.1 riastrad flags |= DRM_MODE_PROP_BITMASK;
3765 1.1 riastrad
3766 1.1 riastrad property = drm_property_create(dev, flags, name, num_values);
3767 1.1 riastrad if (!property)
3768 1.1 riastrad return NULL;
3769 1.5 riastrad for (i = 0; i < num_props; i++) {
3770 1.5 riastrad if (!(supported_bits & (1ULL << props[i].type)))
3771 1.5 riastrad continue;
3772 1.1 riastrad
3773 1.5 riastrad if (WARN_ON(index >= num_values)) {
3774 1.5 riastrad drm_property_destroy(dev, property);
3775 1.5 riastrad return NULL;
3776 1.5 riastrad }
3777 1.5 riastrad
3778 1.5 riastrad ret = drm_property_add_enum(property, index++,
3779 1.1 riastrad props[i].type,
3780 1.1 riastrad props[i].name);
3781 1.1 riastrad if (ret) {
3782 1.1 riastrad drm_property_destroy(dev, property);
3783 1.1 riastrad return NULL;
3784 1.1 riastrad }
3785 1.1 riastrad }
3786 1.1 riastrad
3787 1.1 riastrad return property;
3788 1.1 riastrad }
3789 1.1 riastrad EXPORT_SYMBOL(drm_property_create_bitmask);
3790 1.1 riastrad
3791 1.5 riastrad static struct drm_property *property_create_range(struct drm_device *dev,
3792 1.5 riastrad int flags, const char *name,
3793 1.5 riastrad uint64_t min, uint64_t max)
3794 1.5 riastrad {
3795 1.5 riastrad struct drm_property *property;
3796 1.5 riastrad
3797 1.5 riastrad property = drm_property_create(dev, flags, name, 2);
3798 1.5 riastrad if (!property)
3799 1.5 riastrad return NULL;
3800 1.5 riastrad
3801 1.5 riastrad property->values[0] = min;
3802 1.5 riastrad property->values[1] = max;
3803 1.5 riastrad
3804 1.5 riastrad return property;
3805 1.5 riastrad }
3806 1.5 riastrad
3807 1.3 riastrad /**
3808 1.5 riastrad * drm_property_create_range - create a new unsigned ranged property type
3809 1.3 riastrad * @dev: drm device
3810 1.3 riastrad * @flags: flags specifying the property type
3811 1.3 riastrad * @name: name of the property
3812 1.3 riastrad * @min: minimum value of the property
3813 1.3 riastrad * @max: maximum value of the property
3814 1.3 riastrad *
3815 1.3 riastrad * This creates a new generic drm property which can then be attached to a drm
3816 1.3 riastrad * object with drm_object_attach_property. The returned property object must be
3817 1.3 riastrad * freed with drm_property_destroy.
3818 1.3 riastrad *
3819 1.5 riastrad * Userspace is allowed to set any unsigned integer value in the (min, max)
3820 1.5 riastrad * range inclusive.
3821 1.3 riastrad *
3822 1.3 riastrad * Returns:
3823 1.3 riastrad * A pointer to the newly created property on success, NULL on failure.
3824 1.3 riastrad */
3825 1.1 riastrad struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
3826 1.1 riastrad const char *name,
3827 1.1 riastrad uint64_t min, uint64_t max)
3828 1.1 riastrad {
3829 1.5 riastrad return property_create_range(dev, DRM_MODE_PROP_RANGE | flags,
3830 1.5 riastrad name, min, max);
3831 1.5 riastrad }
3832 1.5 riastrad EXPORT_SYMBOL(drm_property_create_range);
3833 1.5 riastrad
3834 1.5 riastrad /**
3835 1.5 riastrad * drm_property_create_signed_range - create a new signed ranged property type
3836 1.5 riastrad * @dev: drm device
3837 1.5 riastrad * @flags: flags specifying the property type
3838 1.5 riastrad * @name: name of the property
3839 1.5 riastrad * @min: minimum value of the property
3840 1.5 riastrad * @max: maximum value of the property
3841 1.5 riastrad *
3842 1.5 riastrad * This creates a new generic drm property which can then be attached to a drm
3843 1.5 riastrad * object with drm_object_attach_property. The returned property object must be
3844 1.5 riastrad * freed with drm_property_destroy.
3845 1.5 riastrad *
3846 1.5 riastrad * Userspace is allowed to set any signed integer value in the (min, max)
3847 1.5 riastrad * range inclusive.
3848 1.5 riastrad *
3849 1.5 riastrad * Returns:
3850 1.5 riastrad * A pointer to the newly created property on success, NULL on failure.
3851 1.5 riastrad */
3852 1.5 riastrad struct drm_property *drm_property_create_signed_range(struct drm_device *dev,
3853 1.5 riastrad int flags, const char *name,
3854 1.5 riastrad int64_t min, int64_t max)
3855 1.5 riastrad {
3856 1.5 riastrad return property_create_range(dev, DRM_MODE_PROP_SIGNED_RANGE | flags,
3857 1.5 riastrad name, I642U64(min), I642U64(max));
3858 1.5 riastrad }
3859 1.5 riastrad EXPORT_SYMBOL(drm_property_create_signed_range);
3860 1.5 riastrad
3861 1.5 riastrad /**
3862 1.5 riastrad * drm_property_create_object - create a new object property type
3863 1.5 riastrad * @dev: drm device
3864 1.5 riastrad * @flags: flags specifying the property type
3865 1.5 riastrad * @name: name of the property
3866 1.5 riastrad * @type: object type from DRM_MODE_OBJECT_* defines
3867 1.5 riastrad *
3868 1.5 riastrad * This creates a new generic drm property which can then be attached to a drm
3869 1.5 riastrad * object with drm_object_attach_property. The returned property object must be
3870 1.5 riastrad * freed with drm_property_destroy.
3871 1.5 riastrad *
3872 1.5 riastrad * Userspace is only allowed to set this to any property value of the given
3873 1.5 riastrad * @type. Only useful for atomic properties, which is enforced.
3874 1.5 riastrad *
3875 1.5 riastrad * Returns:
3876 1.5 riastrad * A pointer to the newly created property on success, NULL on failure.
3877 1.5 riastrad */
3878 1.5 riastrad struct drm_property *drm_property_create_object(struct drm_device *dev,
3879 1.5 riastrad int flags, const char *name, uint32_t type)
3880 1.5 riastrad {
3881 1.1 riastrad struct drm_property *property;
3882 1.1 riastrad
3883 1.5 riastrad flags |= DRM_MODE_PROP_OBJECT;
3884 1.5 riastrad
3885 1.5 riastrad if (WARN_ON(!(flags & DRM_MODE_PROP_ATOMIC)))
3886 1.5 riastrad return NULL;
3887 1.1 riastrad
3888 1.5 riastrad property = drm_property_create(dev, flags, name, 1);
3889 1.1 riastrad if (!property)
3890 1.1 riastrad return NULL;
3891 1.1 riastrad
3892 1.5 riastrad property->values[0] = type;
3893 1.1 riastrad
3894 1.1 riastrad return property;
3895 1.1 riastrad }
3896 1.5 riastrad EXPORT_SYMBOL(drm_property_create_object);
3897 1.5 riastrad
3898 1.5 riastrad /**
3899 1.5 riastrad * drm_property_create_bool - create a new boolean property type
3900 1.5 riastrad * @dev: drm device
3901 1.5 riastrad * @flags: flags specifying the property type
3902 1.5 riastrad * @name: name of the property
3903 1.5 riastrad *
3904 1.5 riastrad * This creates a new generic drm property which can then be attached to a drm
3905 1.5 riastrad * object with drm_object_attach_property. The returned property object must be
3906 1.5 riastrad * freed with drm_property_destroy.
3907 1.5 riastrad *
3908 1.5 riastrad * This is implemented as a ranged property with only {0, 1} as valid values.
3909 1.5 riastrad *
3910 1.5 riastrad * Returns:
3911 1.5 riastrad * A pointer to the newly created property on success, NULL on failure.
3912 1.5 riastrad */
3913 1.5 riastrad struct drm_property *drm_property_create_bool(struct drm_device *dev, int flags,
3914 1.5 riastrad const char *name)
3915 1.5 riastrad {
3916 1.5 riastrad return drm_property_create_range(dev, flags, name, 0, 1);
3917 1.5 riastrad }
3918 1.5 riastrad EXPORT_SYMBOL(drm_property_create_bool);
3919 1.1 riastrad
3920 1.3 riastrad /**
3921 1.3 riastrad * drm_property_add_enum - add a possible value to an enumeration property
3922 1.3 riastrad * @property: enumeration property to change
3923 1.3 riastrad * @index: index of the new enumeration
3924 1.3 riastrad * @value: value of the new enumeration
3925 1.3 riastrad * @name: symbolic name of the new enumeration
3926 1.3 riastrad *
3927 1.3 riastrad * This functions adds enumerations to a property.
3928 1.3 riastrad *
3929 1.3 riastrad * It's use is deprecated, drivers should use one of the more specific helpers
3930 1.3 riastrad * to directly create the property with all enumerations already attached.
3931 1.3 riastrad *
3932 1.3 riastrad * Returns:
3933 1.3 riastrad * Zero on success, error code on failure.
3934 1.3 riastrad */
3935 1.1 riastrad int drm_property_add_enum(struct drm_property *property, int index,
3936 1.1 riastrad uint64_t value, const char *name)
3937 1.1 riastrad {
3938 1.1 riastrad struct drm_property_enum *prop_enum;
3939 1.1 riastrad
3940 1.5 riastrad if (!(drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3941 1.5 riastrad drm_property_type_is(property, DRM_MODE_PROP_BITMASK)))
3942 1.1 riastrad return -EINVAL;
3943 1.1 riastrad
3944 1.1 riastrad /*
3945 1.1 riastrad * Bitmask enum properties have the additional constraint of values
3946 1.1 riastrad * from 0 to 63
3947 1.1 riastrad */
3948 1.5 riastrad if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK) &&
3949 1.5 riastrad (value > 63))
3950 1.1 riastrad return -EINVAL;
3951 1.1 riastrad
3952 1.5 riastrad if (!list_empty(&property->enum_list)) {
3953 1.5 riastrad list_for_each_entry(prop_enum, &property->enum_list, head) {
3954 1.1 riastrad if (prop_enum->value == value) {
3955 1.1 riastrad strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3956 1.1 riastrad prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3957 1.1 riastrad return 0;
3958 1.1 riastrad }
3959 1.1 riastrad }
3960 1.1 riastrad }
3961 1.1 riastrad
3962 1.1 riastrad prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
3963 1.1 riastrad if (!prop_enum)
3964 1.1 riastrad return -ENOMEM;
3965 1.1 riastrad
3966 1.1 riastrad strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3967 1.1 riastrad prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3968 1.1 riastrad prop_enum->value = value;
3969 1.1 riastrad
3970 1.1 riastrad property->values[index] = value;
3971 1.5 riastrad list_add_tail(&prop_enum->head, &property->enum_list);
3972 1.1 riastrad return 0;
3973 1.1 riastrad }
3974 1.1 riastrad EXPORT_SYMBOL(drm_property_add_enum);
3975 1.1 riastrad
3976 1.3 riastrad /**
3977 1.3 riastrad * drm_property_destroy - destroy a drm property
3978 1.3 riastrad * @dev: drm device
3979 1.3 riastrad * @property: property to destry
3980 1.3 riastrad *
3981 1.3 riastrad * This function frees a property including any attached resources like
3982 1.3 riastrad * enumeration values.
3983 1.3 riastrad */
3984 1.1 riastrad void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
3985 1.1 riastrad {
3986 1.1 riastrad struct drm_property_enum *prop_enum, *pt;
3987 1.1 riastrad
3988 1.5 riastrad list_for_each_entry_safe(prop_enum, pt, &property->enum_list, head) {
3989 1.1 riastrad list_del(&prop_enum->head);
3990 1.1 riastrad kfree(prop_enum);
3991 1.1 riastrad }
3992 1.1 riastrad
3993 1.1 riastrad if (property->num_values)
3994 1.1 riastrad kfree(property->values);
3995 1.1 riastrad drm_mode_object_put(dev, &property->base);
3996 1.1 riastrad list_del(&property->head);
3997 1.1 riastrad kfree(property);
3998 1.1 riastrad }
3999 1.1 riastrad EXPORT_SYMBOL(drm_property_destroy);
4000 1.1 riastrad
4001 1.3 riastrad /**
4002 1.3 riastrad * drm_object_attach_property - attach a property to a modeset object
4003 1.3 riastrad * @obj: drm modeset object
4004 1.3 riastrad * @property: property to attach
4005 1.3 riastrad * @init_val: initial value of the property
4006 1.3 riastrad *
4007 1.3 riastrad * This attaches the given property to the modeset object with the given initial
4008 1.3 riastrad * value. Currently this function cannot fail since the properties are stored in
4009 1.3 riastrad * a statically sized array.
4010 1.3 riastrad */
4011 1.1 riastrad void drm_object_attach_property(struct drm_mode_object *obj,
4012 1.1 riastrad struct drm_property *property,
4013 1.1 riastrad uint64_t init_val)
4014 1.1 riastrad {
4015 1.1 riastrad int count = obj->properties->count;
4016 1.1 riastrad
4017 1.1 riastrad if (count == DRM_OBJECT_MAX_PROPERTY) {
4018 1.1 riastrad WARN(1, "Failed to attach object property (type: 0x%x). Please "
4019 1.1 riastrad "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
4020 1.1 riastrad "you see this message on the same object type.\n",
4021 1.1 riastrad obj->type);
4022 1.1 riastrad return;
4023 1.1 riastrad }
4024 1.1 riastrad
4025 1.5 riastrad obj->properties->properties[count] = property;
4026 1.1 riastrad obj->properties->values[count] = init_val;
4027 1.1 riastrad obj->properties->count++;
4028 1.5 riastrad if (property->flags & DRM_MODE_PROP_ATOMIC)
4029 1.5 riastrad obj->properties->atomic_count++;
4030 1.1 riastrad }
4031 1.1 riastrad EXPORT_SYMBOL(drm_object_attach_property);
4032 1.1 riastrad
4033 1.3 riastrad /**
4034 1.3 riastrad * drm_object_property_set_value - set the value of a property
4035 1.3 riastrad * @obj: drm mode object to set property value for
4036 1.3 riastrad * @property: property to set
4037 1.3 riastrad * @val: value the property should be set to
4038 1.3 riastrad *
4039 1.3 riastrad * This functions sets a given property on a given object. This function only
4040 1.3 riastrad * changes the software state of the property, it does not call into the
4041 1.3 riastrad * driver's ->set_property callback.
4042 1.3 riastrad *
4043 1.3 riastrad * Returns:
4044 1.3 riastrad * Zero on success, error code on failure.
4045 1.3 riastrad */
4046 1.1 riastrad int drm_object_property_set_value(struct drm_mode_object *obj,
4047 1.1 riastrad struct drm_property *property, uint64_t val)
4048 1.1 riastrad {
4049 1.1 riastrad int i;
4050 1.1 riastrad
4051 1.1 riastrad for (i = 0; i < obj->properties->count; i++) {
4052 1.5 riastrad if (obj->properties->properties[i] == property) {
4053 1.1 riastrad obj->properties->values[i] = val;
4054 1.1 riastrad return 0;
4055 1.1 riastrad }
4056 1.1 riastrad }
4057 1.1 riastrad
4058 1.1 riastrad return -EINVAL;
4059 1.1 riastrad }
4060 1.1 riastrad EXPORT_SYMBOL(drm_object_property_set_value);
4061 1.1 riastrad
4062 1.3 riastrad /**
4063 1.3 riastrad * drm_object_property_get_value - retrieve the value of a property
4064 1.3 riastrad * @obj: drm mode object to get property value from
4065 1.3 riastrad * @property: property to retrieve
4066 1.3 riastrad * @val: storage for the property value
4067 1.3 riastrad *
4068 1.3 riastrad * This function retrieves the softare state of the given property for the given
4069 1.3 riastrad * property. Since there is no driver callback to retrieve the current property
4070 1.3 riastrad * value this might be out of sync with the hardware, depending upon the driver
4071 1.3 riastrad * and property.
4072 1.3 riastrad *
4073 1.3 riastrad * Returns:
4074 1.3 riastrad * Zero on success, error code on failure.
4075 1.3 riastrad */
4076 1.1 riastrad int drm_object_property_get_value(struct drm_mode_object *obj,
4077 1.1 riastrad struct drm_property *property, uint64_t *val)
4078 1.1 riastrad {
4079 1.1 riastrad int i;
4080 1.1 riastrad
4081 1.5 riastrad /* read-only properties bypass atomic mechanism and still store
4082 1.5 riastrad * their value in obj->properties->values[].. mostly to avoid
4083 1.5 riastrad * having to deal w/ EDID and similar props in atomic paths:
4084 1.5 riastrad */
4085 1.5 riastrad if (drm_core_check_feature(property->dev, DRIVER_ATOMIC) &&
4086 1.5 riastrad !(property->flags & DRM_MODE_PROP_IMMUTABLE))
4087 1.5 riastrad return drm_atomic_get_property(obj, property, val);
4088 1.5 riastrad
4089 1.1 riastrad for (i = 0; i < obj->properties->count; i++) {
4090 1.5 riastrad if (obj->properties->properties[i] == property) {
4091 1.1 riastrad *val = obj->properties->values[i];
4092 1.1 riastrad return 0;
4093 1.1 riastrad }
4094 1.1 riastrad }
4095 1.1 riastrad
4096 1.1 riastrad return -EINVAL;
4097 1.1 riastrad }
4098 1.1 riastrad EXPORT_SYMBOL(drm_object_property_get_value);
4099 1.1 riastrad
4100 1.3 riastrad /**
4101 1.5 riastrad * drm_mode_getproperty_ioctl - get the property metadata
4102 1.3 riastrad * @dev: DRM device
4103 1.3 riastrad * @data: ioctl data
4104 1.3 riastrad * @file_priv: DRM file info
4105 1.3 riastrad *
4106 1.5 riastrad * This function retrieves the metadata for a given property, like the different
4107 1.5 riastrad * possible values for an enum property or the limits for a range property.
4108 1.5 riastrad *
4109 1.5 riastrad * Blob properties are special
4110 1.3 riastrad *
4111 1.3 riastrad * Called by the user via ioctl.
4112 1.3 riastrad *
4113 1.3 riastrad * Returns:
4114 1.5 riastrad * Zero on success, negative errno on failure.
4115 1.3 riastrad */
4116 1.1 riastrad int drm_mode_getproperty_ioctl(struct drm_device *dev,
4117 1.1 riastrad void *data, struct drm_file *file_priv)
4118 1.1 riastrad {
4119 1.1 riastrad struct drm_mode_get_property *out_resp = data;
4120 1.1 riastrad struct drm_property *property;
4121 1.1 riastrad int enum_count = 0;
4122 1.1 riastrad int value_count = 0;
4123 1.1 riastrad int ret = 0, i;
4124 1.1 riastrad int copied;
4125 1.1 riastrad struct drm_property_enum *prop_enum;
4126 1.1 riastrad struct drm_mode_property_enum __user *enum_ptr;
4127 1.1 riastrad uint64_t __user *values_ptr;
4128 1.1 riastrad
4129 1.1 riastrad if (!drm_core_check_feature(dev, DRIVER_MODESET))
4130 1.1 riastrad return -EINVAL;
4131 1.1 riastrad
4132 1.3 riastrad drm_modeset_lock_all(dev);
4133 1.5 riastrad property = drm_property_find(dev, out_resp->prop_id);
4134 1.5 riastrad if (!property) {
4135 1.3 riastrad ret = -ENOENT;
4136 1.1 riastrad goto done;
4137 1.1 riastrad }
4138 1.1 riastrad
4139 1.5 riastrad if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
4140 1.5 riastrad drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
4141 1.5 riastrad list_for_each_entry(prop_enum, &property->enum_list, head)
4142 1.1 riastrad enum_count++;
4143 1.1 riastrad }
4144 1.1 riastrad
4145 1.1 riastrad value_count = property->num_values;
4146 1.1 riastrad
4147 1.1 riastrad strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
4148 1.1 riastrad out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
4149 1.1 riastrad out_resp->flags = property->flags;
4150 1.1 riastrad
4151 1.1 riastrad if ((out_resp->count_values >= value_count) && value_count) {
4152 1.1 riastrad values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
4153 1.1 riastrad for (i = 0; i < value_count; i++) {
4154 1.1 riastrad if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
4155 1.1 riastrad ret = -EFAULT;
4156 1.1 riastrad goto done;
4157 1.1 riastrad }
4158 1.1 riastrad }
4159 1.1 riastrad }
4160 1.1 riastrad out_resp->count_values = value_count;
4161 1.1 riastrad
4162 1.5 riastrad if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
4163 1.5 riastrad drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
4164 1.1 riastrad if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
4165 1.1 riastrad copied = 0;
4166 1.1 riastrad enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
4167 1.5 riastrad list_for_each_entry(prop_enum, &property->enum_list, head) {
4168 1.1 riastrad
4169 1.1 riastrad if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
4170 1.1 riastrad ret = -EFAULT;
4171 1.1 riastrad goto done;
4172 1.1 riastrad }
4173 1.1 riastrad
4174 1.1 riastrad if (copy_to_user(&enum_ptr[copied].name,
4175 1.1 riastrad &prop_enum->name, DRM_PROP_NAME_LEN)) {
4176 1.1 riastrad ret = -EFAULT;
4177 1.1 riastrad goto done;
4178 1.1 riastrad }
4179 1.1 riastrad copied++;
4180 1.1 riastrad }
4181 1.1 riastrad }
4182 1.1 riastrad out_resp->count_enum_blobs = enum_count;
4183 1.1 riastrad }
4184 1.1 riastrad
4185 1.5 riastrad /*
4186 1.5 riastrad * NOTE: The idea seems to have been to use this to read all the blob
4187 1.5 riastrad * property values. But nothing ever added them to the corresponding
4188 1.5 riastrad * list, userspace always used the special-purpose get_blob ioctl to
4189 1.5 riastrad * read the value for a blob property. It also doesn't make a lot of
4190 1.5 riastrad * sense to return values here when everything else is just metadata for
4191 1.5 riastrad * the property itself.
4192 1.5 riastrad */
4193 1.5 riastrad if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
4194 1.5 riastrad out_resp->count_enum_blobs = 0;
4195 1.1 riastrad done:
4196 1.3 riastrad drm_modeset_unlock_all(dev);
4197 1.1 riastrad return ret;
4198 1.1 riastrad }
4199 1.1 riastrad
4200 1.5 riastrad /**
4201 1.5 riastrad * drm_property_create_blob - Create new blob property
4202 1.5 riastrad *
4203 1.5 riastrad * Creates a new blob property for a specified DRM device, optionally
4204 1.5 riastrad * copying data.
4205 1.5 riastrad *
4206 1.5 riastrad * @dev: DRM device to create property for
4207 1.5 riastrad * @length: Length to allocate for blob data
4208 1.5 riastrad * @data: If specified, copies data into blob
4209 1.5 riastrad *
4210 1.5 riastrad * Returns:
4211 1.5 riastrad * New blob property with a single reference on success, or an ERR_PTR
4212 1.5 riastrad * value on failure.
4213 1.5 riastrad */
4214 1.5 riastrad struct drm_property_blob *
4215 1.5 riastrad drm_property_create_blob(struct drm_device *dev, size_t length,
4216 1.5 riastrad const void *data)
4217 1.1 riastrad {
4218 1.1 riastrad struct drm_property_blob *blob;
4219 1.1 riastrad int ret;
4220 1.1 riastrad
4221 1.5 riastrad if (!length || length > ULONG_MAX - sizeof(struct drm_property_blob))
4222 1.5 riastrad return ERR_PTR(-EINVAL);
4223 1.1 riastrad
4224 1.1 riastrad blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
4225 1.1 riastrad if (!blob)
4226 1.5 riastrad return ERR_PTR(-ENOMEM);
4227 1.5 riastrad
4228 1.5 riastrad /* This must be explicitly initialised, so we can safely call list_del
4229 1.5 riastrad * on it in the removal handler, even if it isn't in a file list. */
4230 1.5 riastrad INIT_LIST_HEAD(&blob->head_file);
4231 1.5 riastrad blob->length = length;
4232 1.5 riastrad blob->dev = dev;
4233 1.5 riastrad
4234 1.5 riastrad if (data)
4235 1.5 riastrad memcpy(blob->data, data, length);
4236 1.5 riastrad
4237 1.5 riastrad mutex_lock(&dev->mode_config.blob_lock);
4238 1.1 riastrad
4239 1.1 riastrad ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
4240 1.1 riastrad if (ret) {
4241 1.1 riastrad kfree(blob);
4242 1.5 riastrad mutex_unlock(&dev->mode_config.blob_lock);
4243 1.5 riastrad return ERR_PTR(-EINVAL);
4244 1.1 riastrad }
4245 1.1 riastrad
4246 1.5 riastrad kref_init(&blob->refcount);
4247 1.5 riastrad
4248 1.5 riastrad list_add_tail(&blob->head_global,
4249 1.5 riastrad &dev->mode_config.property_blob_list);
4250 1.1 riastrad
4251 1.5 riastrad mutex_unlock(&dev->mode_config.blob_lock);
4252 1.1 riastrad
4253 1.1 riastrad return blob;
4254 1.1 riastrad }
4255 1.5 riastrad EXPORT_SYMBOL(drm_property_create_blob);
4256 1.1 riastrad
4257 1.5 riastrad /**
4258 1.5 riastrad * drm_property_free_blob - Blob property destructor
4259 1.5 riastrad *
4260 1.5 riastrad * Internal free function for blob properties; must not be used directly.
4261 1.5 riastrad *
4262 1.5 riastrad * @kref: Reference
4263 1.5 riastrad */
4264 1.5 riastrad static void drm_property_free_blob(struct kref *kref)
4265 1.1 riastrad {
4266 1.5 riastrad struct drm_property_blob *blob =
4267 1.5 riastrad container_of(kref, struct drm_property_blob, refcount);
4268 1.5 riastrad
4269 1.5 riastrad WARN_ON(!mutex_is_locked(&blob->dev->mode_config.blob_lock));
4270 1.5 riastrad
4271 1.5 riastrad list_del(&blob->head_global);
4272 1.5 riastrad list_del(&blob->head_file);
4273 1.5 riastrad drm_mode_object_put(blob->dev, &blob->base);
4274 1.5 riastrad
4275 1.1 riastrad kfree(blob);
4276 1.1 riastrad }
4277 1.1 riastrad
4278 1.3 riastrad /**
4279 1.5 riastrad * drm_property_unreference_blob - Unreference a blob property
4280 1.5 riastrad *
4281 1.5 riastrad * Drop a reference on a blob property. May free the object.
4282 1.5 riastrad *
4283 1.5 riastrad * @blob: Pointer to blob property
4284 1.5 riastrad */
4285 1.5 riastrad void drm_property_unreference_blob(struct drm_property_blob *blob)
4286 1.5 riastrad {
4287 1.5 riastrad struct drm_device *dev;
4288 1.5 riastrad
4289 1.5 riastrad if (!blob)
4290 1.5 riastrad return;
4291 1.5 riastrad
4292 1.5 riastrad dev = blob->dev;
4293 1.5 riastrad
4294 1.7 riastrad DRM_DEBUG("%p: blob ID: %d (%d)\n", blob, blob->base.id, blob->refcount.kr_count);
4295 1.5 riastrad
4296 1.5 riastrad if (kref_put_mutex(&blob->refcount, drm_property_free_blob,
4297 1.5 riastrad &dev->mode_config.blob_lock))
4298 1.5 riastrad mutex_unlock(&dev->mode_config.blob_lock);
4299 1.5 riastrad else
4300 1.5 riastrad might_lock(&dev->mode_config.blob_lock);
4301 1.5 riastrad }
4302 1.5 riastrad EXPORT_SYMBOL(drm_property_unreference_blob);
4303 1.5 riastrad
4304 1.5 riastrad /**
4305 1.5 riastrad * drm_property_unreference_blob_locked - Unreference a blob property with blob_lock held
4306 1.5 riastrad *
4307 1.5 riastrad * Drop a reference on a blob property. May free the object. This must be
4308 1.5 riastrad * called with blob_lock held.
4309 1.5 riastrad *
4310 1.5 riastrad * @blob: Pointer to blob property
4311 1.5 riastrad */
4312 1.5 riastrad static void drm_property_unreference_blob_locked(struct drm_property_blob *blob)
4313 1.5 riastrad {
4314 1.5 riastrad if (!blob)
4315 1.5 riastrad return;
4316 1.5 riastrad
4317 1.7 riastrad DRM_DEBUG("%p: blob ID: %d (%d)\n", blob, blob->base.id, blob->refcount.kr_count);
4318 1.5 riastrad
4319 1.5 riastrad kref_put(&blob->refcount, drm_property_free_blob);
4320 1.5 riastrad }
4321 1.5 riastrad
4322 1.5 riastrad /**
4323 1.5 riastrad * drm_property_destroy_user_blobs - destroy all blobs created by this client
4324 1.5 riastrad * @dev: DRM device
4325 1.5 riastrad * @file_priv: destroy all blobs owned by this file handle
4326 1.5 riastrad */
4327 1.5 riastrad void drm_property_destroy_user_blobs(struct drm_device *dev,
4328 1.5 riastrad struct drm_file *file_priv)
4329 1.5 riastrad {
4330 1.5 riastrad struct drm_property_blob *blob, *bt;
4331 1.5 riastrad
4332 1.5 riastrad mutex_lock(&dev->mode_config.blob_lock);
4333 1.5 riastrad
4334 1.5 riastrad list_for_each_entry_safe(blob, bt, &file_priv->blobs, head_file) {
4335 1.5 riastrad list_del_init(&blob->head_file);
4336 1.5 riastrad drm_property_unreference_blob_locked(blob);
4337 1.5 riastrad }
4338 1.5 riastrad
4339 1.5 riastrad mutex_unlock(&dev->mode_config.blob_lock);
4340 1.5 riastrad }
4341 1.5 riastrad
4342 1.5 riastrad /**
4343 1.5 riastrad * drm_property_reference_blob - Take a reference on an existing property
4344 1.5 riastrad *
4345 1.5 riastrad * Take a new reference on an existing blob property.
4346 1.5 riastrad *
4347 1.5 riastrad * @blob: Pointer to blob property
4348 1.5 riastrad */
4349 1.5 riastrad struct drm_property_blob *drm_property_reference_blob(struct drm_property_blob *blob)
4350 1.5 riastrad {
4351 1.7 riastrad DRM_DEBUG("%p: blob ID: %d (%d)\n", blob, blob->base.id, blob->refcount.kr_count);
4352 1.5 riastrad kref_get(&blob->refcount);
4353 1.5 riastrad return blob;
4354 1.5 riastrad }
4355 1.5 riastrad EXPORT_SYMBOL(drm_property_reference_blob);
4356 1.5 riastrad
4357 1.5 riastrad /*
4358 1.5 riastrad * Like drm_property_lookup_blob, but does not return an additional reference.
4359 1.5 riastrad * Must be called with blob_lock held.
4360 1.5 riastrad */
4361 1.5 riastrad static struct drm_property_blob *__drm_property_lookup_blob(struct drm_device *dev,
4362 1.5 riastrad uint32_t id)
4363 1.5 riastrad {
4364 1.5 riastrad struct drm_mode_object *obj = NULL;
4365 1.5 riastrad struct drm_property_blob *blob;
4366 1.5 riastrad
4367 1.5 riastrad WARN_ON(!mutex_is_locked(&dev->mode_config.blob_lock));
4368 1.5 riastrad
4369 1.5 riastrad mutex_lock(&dev->mode_config.idr_mutex);
4370 1.5 riastrad obj = idr_find(&dev->mode_config.crtc_idr, id);
4371 1.5 riastrad if (!obj || (obj->type != DRM_MODE_OBJECT_BLOB) || (obj->id != id))
4372 1.5 riastrad blob = NULL;
4373 1.5 riastrad else
4374 1.5 riastrad blob = obj_to_blob(obj);
4375 1.5 riastrad mutex_unlock(&dev->mode_config.idr_mutex);
4376 1.5 riastrad
4377 1.5 riastrad return blob;
4378 1.5 riastrad }
4379 1.5 riastrad
4380 1.5 riastrad /**
4381 1.5 riastrad * drm_property_lookup_blob - look up a blob property and take a reference
4382 1.5 riastrad * @dev: drm device
4383 1.5 riastrad * @id: id of the blob property
4384 1.5 riastrad *
4385 1.5 riastrad * If successful, this takes an additional reference to the blob property.
4386 1.5 riastrad * callers need to make sure to eventually unreference the returned property
4387 1.5 riastrad * again, using @drm_property_unreference_blob.
4388 1.5 riastrad */
4389 1.5 riastrad struct drm_property_blob *drm_property_lookup_blob(struct drm_device *dev,
4390 1.5 riastrad uint32_t id)
4391 1.5 riastrad {
4392 1.5 riastrad struct drm_property_blob *blob;
4393 1.5 riastrad
4394 1.5 riastrad mutex_lock(&dev->mode_config.blob_lock);
4395 1.5 riastrad blob = __drm_property_lookup_blob(dev, id);
4396 1.5 riastrad if (blob) {
4397 1.5 riastrad if (!kref_get_unless_zero(&blob->refcount))
4398 1.5 riastrad blob = NULL;
4399 1.5 riastrad }
4400 1.5 riastrad mutex_unlock(&dev->mode_config.blob_lock);
4401 1.5 riastrad
4402 1.5 riastrad return blob;
4403 1.5 riastrad }
4404 1.5 riastrad EXPORT_SYMBOL(drm_property_lookup_blob);
4405 1.5 riastrad
4406 1.5 riastrad /**
4407 1.5 riastrad * drm_property_replace_global_blob - atomically replace existing blob property
4408 1.5 riastrad * @dev: drm device
4409 1.5 riastrad * @replace: location of blob property pointer to be replaced
4410 1.5 riastrad * @length: length of data for new blob, or 0 for no data
4411 1.5 riastrad * @data: content for new blob, or NULL for no data
4412 1.5 riastrad * @obj_holds_id: optional object for property holding blob ID
4413 1.5 riastrad * @prop_holds_id: optional property holding blob ID
4414 1.5 riastrad * @return 0 on success or error on failure
4415 1.5 riastrad *
4416 1.5 riastrad * This function will atomically replace a global property in the blob list,
4417 1.5 riastrad * optionally updating a property which holds the ID of that property. It is
4418 1.5 riastrad * guaranteed to be atomic: no caller will be allowed to see intermediate
4419 1.5 riastrad * results, and either the entire operation will succeed and clean up the
4420 1.5 riastrad * previous property, or it will fail and the state will be unchanged.
4421 1.5 riastrad *
4422 1.5 riastrad * If length is 0 or data is NULL, no new blob will be created, and the holding
4423 1.5 riastrad * property, if specified, will be set to 0.
4424 1.5 riastrad *
4425 1.5 riastrad * Access to the replace pointer is assumed to be protected by the caller, e.g.
4426 1.5 riastrad * by holding the relevant modesetting object lock for its parent.
4427 1.5 riastrad *
4428 1.5 riastrad * For example, a drm_connector has a 'PATH' property, which contains the ID
4429 1.5 riastrad * of a blob property with the value of the MST path information. Calling this
4430 1.5 riastrad * function with replace pointing to the connector's path_blob_ptr, length and
4431 1.5 riastrad * data set for the new path information, obj_holds_id set to the connector's
4432 1.5 riastrad * base object, and prop_holds_id set to the path property name, will perform
4433 1.5 riastrad * a completely atomic update. The access to path_blob_ptr is protected by the
4434 1.5 riastrad * caller holding a lock on the connector.
4435 1.5 riastrad */
4436 1.5 riastrad static int drm_property_replace_global_blob(struct drm_device *dev,
4437 1.5 riastrad struct drm_property_blob **replace,
4438 1.5 riastrad size_t length,
4439 1.5 riastrad const void *data,
4440 1.5 riastrad struct drm_mode_object *obj_holds_id,
4441 1.5 riastrad struct drm_property *prop_holds_id)
4442 1.5 riastrad {
4443 1.5 riastrad struct drm_property_blob *new_blob = NULL;
4444 1.5 riastrad struct drm_property_blob *old_blob = NULL;
4445 1.5 riastrad int ret;
4446 1.5 riastrad
4447 1.5 riastrad WARN_ON(replace == NULL);
4448 1.5 riastrad
4449 1.5 riastrad old_blob = *replace;
4450 1.5 riastrad
4451 1.5 riastrad if (length && data) {
4452 1.5 riastrad new_blob = drm_property_create_blob(dev, length, data);
4453 1.5 riastrad if (IS_ERR(new_blob))
4454 1.5 riastrad return PTR_ERR(new_blob);
4455 1.5 riastrad }
4456 1.5 riastrad
4457 1.5 riastrad /* This does not need to be synchronised with blob_lock, as the
4458 1.5 riastrad * get_properties ioctl locks all modesetting objects, and
4459 1.5 riastrad * obj_holds_id must be locked before calling here, so we cannot
4460 1.5 riastrad * have its value out of sync with the list membership modified
4461 1.5 riastrad * below under blob_lock. */
4462 1.5 riastrad if (obj_holds_id) {
4463 1.5 riastrad ret = drm_object_property_set_value(obj_holds_id,
4464 1.5 riastrad prop_holds_id,
4465 1.5 riastrad new_blob ?
4466 1.5 riastrad new_blob->base.id : 0);
4467 1.5 riastrad if (ret != 0)
4468 1.5 riastrad goto err_created;
4469 1.5 riastrad }
4470 1.5 riastrad
4471 1.5 riastrad drm_property_unreference_blob(old_blob);
4472 1.5 riastrad *replace = new_blob;
4473 1.5 riastrad
4474 1.5 riastrad return 0;
4475 1.5 riastrad
4476 1.5 riastrad err_created:
4477 1.5 riastrad drm_property_unreference_blob(new_blob);
4478 1.5 riastrad return ret;
4479 1.5 riastrad }
4480 1.5 riastrad
4481 1.5 riastrad /**
4482 1.3 riastrad * drm_mode_getblob_ioctl - get the contents of a blob property value
4483 1.3 riastrad * @dev: DRM device
4484 1.3 riastrad * @data: ioctl data
4485 1.3 riastrad * @file_priv: DRM file info
4486 1.3 riastrad *
4487 1.3 riastrad * This function retrieves the contents of a blob property. The value stored in
4488 1.3 riastrad * an object's blob property is just a normal modeset object id.
4489 1.3 riastrad *
4490 1.3 riastrad * Called by the user via ioctl.
4491 1.3 riastrad *
4492 1.3 riastrad * Returns:
4493 1.5 riastrad * Zero on success, negative errno on failure.
4494 1.3 riastrad */
4495 1.1 riastrad int drm_mode_getblob_ioctl(struct drm_device *dev,
4496 1.1 riastrad void *data, struct drm_file *file_priv)
4497 1.1 riastrad {
4498 1.1 riastrad struct drm_mode_get_blob *out_resp = data;
4499 1.1 riastrad struct drm_property_blob *blob;
4500 1.1 riastrad int ret = 0;
4501 1.1 riastrad void __user *blob_ptr;
4502 1.1 riastrad
4503 1.1 riastrad if (!drm_core_check_feature(dev, DRIVER_MODESET))
4504 1.1 riastrad return -EINVAL;
4505 1.1 riastrad
4506 1.3 riastrad drm_modeset_lock_all(dev);
4507 1.5 riastrad mutex_lock(&dev->mode_config.blob_lock);
4508 1.5 riastrad blob = __drm_property_lookup_blob(dev, out_resp->blob_id);
4509 1.5 riastrad if (!blob) {
4510 1.3 riastrad ret = -ENOENT;
4511 1.1 riastrad goto done;
4512 1.1 riastrad }
4513 1.1 riastrad
4514 1.1 riastrad if (out_resp->length == blob->length) {
4515 1.1 riastrad blob_ptr = (void __user *)(unsigned long)out_resp->data;
4516 1.5 riastrad if (copy_to_user(blob_ptr, blob->data, blob->length)) {
4517 1.1 riastrad ret = -EFAULT;
4518 1.1 riastrad goto done;
4519 1.1 riastrad }
4520 1.1 riastrad }
4521 1.1 riastrad out_resp->length = blob->length;
4522 1.1 riastrad
4523 1.1 riastrad done:
4524 1.5 riastrad mutex_unlock(&dev->mode_config.blob_lock);
4525 1.3 riastrad drm_modeset_unlock_all(dev);
4526 1.1 riastrad return ret;
4527 1.1 riastrad }
4528 1.1 riastrad
4529 1.3 riastrad /**
4530 1.5 riastrad * drm_mode_createblob_ioctl - create a new blob property
4531 1.5 riastrad * @dev: DRM device
4532 1.5 riastrad * @data: ioctl data
4533 1.5 riastrad * @file_priv: DRM file info
4534 1.5 riastrad *
4535 1.5 riastrad * This function creates a new blob property with user-defined values. In order
4536 1.5 riastrad * to give us sensible validation and checking when creating, rather than at
4537 1.5 riastrad * every potential use, we also require a type to be provided upfront.
4538 1.5 riastrad *
4539 1.5 riastrad * Called by the user via ioctl.
4540 1.5 riastrad *
4541 1.5 riastrad * Returns:
4542 1.5 riastrad * Zero on success, negative errno on failure.
4543 1.5 riastrad */
4544 1.5 riastrad int drm_mode_createblob_ioctl(struct drm_device *dev,
4545 1.5 riastrad void *data, struct drm_file *file_priv)
4546 1.5 riastrad {
4547 1.5 riastrad struct drm_mode_create_blob *out_resp = data;
4548 1.5 riastrad struct drm_property_blob *blob;
4549 1.5 riastrad void __user *blob_ptr;
4550 1.5 riastrad int ret = 0;
4551 1.5 riastrad
4552 1.5 riastrad if (!drm_core_check_feature(dev, DRIVER_MODESET))
4553 1.5 riastrad return -EINVAL;
4554 1.5 riastrad
4555 1.5 riastrad blob = drm_property_create_blob(dev, out_resp->length, NULL);
4556 1.5 riastrad if (IS_ERR(blob))
4557 1.5 riastrad return PTR_ERR(blob);
4558 1.5 riastrad
4559 1.5 riastrad blob_ptr = (void __user *)(unsigned long)out_resp->data;
4560 1.5 riastrad if (copy_from_user(blob->data, blob_ptr, out_resp->length)) {
4561 1.5 riastrad ret = -EFAULT;
4562 1.5 riastrad goto out_blob;
4563 1.5 riastrad }
4564 1.5 riastrad
4565 1.5 riastrad /* Dropping the lock between create_blob and our access here is safe
4566 1.5 riastrad * as only the same file_priv can remove the blob; at this point, it is
4567 1.5 riastrad * not associated with any file_priv. */
4568 1.5 riastrad mutex_lock(&dev->mode_config.blob_lock);
4569 1.5 riastrad out_resp->blob_id = blob->base.id;
4570 1.5 riastrad list_add_tail(&blob->head_file, &file_priv->blobs);
4571 1.5 riastrad mutex_unlock(&dev->mode_config.blob_lock);
4572 1.5 riastrad
4573 1.5 riastrad return 0;
4574 1.5 riastrad
4575 1.5 riastrad out_blob:
4576 1.5 riastrad drm_property_unreference_blob(blob);
4577 1.5 riastrad return ret;
4578 1.5 riastrad }
4579 1.5 riastrad
4580 1.5 riastrad /**
4581 1.5 riastrad * drm_mode_destroyblob_ioctl - destroy a user blob property
4582 1.5 riastrad * @dev: DRM device
4583 1.5 riastrad * @data: ioctl data
4584 1.5 riastrad * @file_priv: DRM file info
4585 1.5 riastrad *
4586 1.5 riastrad * Destroy an existing user-defined blob property.
4587 1.5 riastrad *
4588 1.5 riastrad * Called by the user via ioctl.
4589 1.5 riastrad *
4590 1.5 riastrad * Returns:
4591 1.5 riastrad * Zero on success, negative errno on failure.
4592 1.5 riastrad */
4593 1.5 riastrad int drm_mode_destroyblob_ioctl(struct drm_device *dev,
4594 1.5 riastrad void *data, struct drm_file *file_priv)
4595 1.5 riastrad {
4596 1.5 riastrad struct drm_mode_destroy_blob *out_resp = data;
4597 1.5 riastrad struct drm_property_blob *blob = NULL, *bt;
4598 1.5 riastrad bool found = false;
4599 1.5 riastrad int ret = 0;
4600 1.5 riastrad
4601 1.5 riastrad if (!drm_core_check_feature(dev, DRIVER_MODESET))
4602 1.5 riastrad return -EINVAL;
4603 1.5 riastrad
4604 1.5 riastrad mutex_lock(&dev->mode_config.blob_lock);
4605 1.5 riastrad blob = __drm_property_lookup_blob(dev, out_resp->blob_id);
4606 1.5 riastrad if (!blob) {
4607 1.5 riastrad ret = -ENOENT;
4608 1.5 riastrad goto err;
4609 1.5 riastrad }
4610 1.5 riastrad
4611 1.5 riastrad /* Ensure the property was actually created by this user. */
4612 1.5 riastrad list_for_each_entry(bt, &file_priv->blobs, head_file) {
4613 1.5 riastrad if (bt == blob) {
4614 1.5 riastrad found = true;
4615 1.5 riastrad break;
4616 1.5 riastrad }
4617 1.5 riastrad }
4618 1.5 riastrad
4619 1.5 riastrad if (!found) {
4620 1.5 riastrad ret = -EPERM;
4621 1.5 riastrad goto err;
4622 1.5 riastrad }
4623 1.5 riastrad
4624 1.5 riastrad /* We must drop head_file here, because we may not be the last
4625 1.5 riastrad * reference on the blob. */
4626 1.5 riastrad list_del_init(&blob->head_file);
4627 1.5 riastrad drm_property_unreference_blob_locked(blob);
4628 1.5 riastrad mutex_unlock(&dev->mode_config.blob_lock);
4629 1.5 riastrad
4630 1.5 riastrad return 0;
4631 1.5 riastrad
4632 1.5 riastrad err:
4633 1.5 riastrad mutex_unlock(&dev->mode_config.blob_lock);
4634 1.5 riastrad return ret;
4635 1.5 riastrad }
4636 1.5 riastrad
4637 1.5 riastrad /**
4638 1.5 riastrad * drm_mode_connector_set_path_property - set tile property on connector
4639 1.5 riastrad * @connector: connector to set property on.
4640 1.5 riastrad * @path: path to use for property; must not be NULL.
4641 1.5 riastrad *
4642 1.5 riastrad * This creates a property to expose to userspace to specify a
4643 1.5 riastrad * connector path. This is mainly used for DisplayPort MST where
4644 1.5 riastrad * connectors have a topology and we want to allow userspace to give
4645 1.5 riastrad * them more meaningful names.
4646 1.5 riastrad *
4647 1.5 riastrad * Returns:
4648 1.5 riastrad * Zero on success, negative errno on failure.
4649 1.5 riastrad */
4650 1.5 riastrad int drm_mode_connector_set_path_property(struct drm_connector *connector,
4651 1.5 riastrad const char *path)
4652 1.5 riastrad {
4653 1.5 riastrad struct drm_device *dev = connector->dev;
4654 1.5 riastrad int ret;
4655 1.5 riastrad
4656 1.5 riastrad ret = drm_property_replace_global_blob(dev,
4657 1.5 riastrad &connector->path_blob_ptr,
4658 1.5 riastrad strlen(path) + 1,
4659 1.5 riastrad path,
4660 1.5 riastrad &connector->base,
4661 1.5 riastrad dev->mode_config.path_property);
4662 1.5 riastrad return ret;
4663 1.5 riastrad }
4664 1.5 riastrad EXPORT_SYMBOL(drm_mode_connector_set_path_property);
4665 1.5 riastrad
4666 1.5 riastrad /**
4667 1.5 riastrad * drm_mode_connector_set_tile_property - set tile property on connector
4668 1.5 riastrad * @connector: connector to set property on.
4669 1.5 riastrad *
4670 1.5 riastrad * This looks up the tile information for a connector, and creates a
4671 1.5 riastrad * property for userspace to parse if it exists. The property is of
4672 1.5 riastrad * the form of 8 integers using ':' as a separator.
4673 1.5 riastrad *
4674 1.5 riastrad * Returns:
4675 1.5 riastrad * Zero on success, errno on failure.
4676 1.5 riastrad */
4677 1.5 riastrad int drm_mode_connector_set_tile_property(struct drm_connector *connector)
4678 1.5 riastrad {
4679 1.5 riastrad struct drm_device *dev = connector->dev;
4680 1.5 riastrad char tile[256];
4681 1.5 riastrad int ret;
4682 1.5 riastrad
4683 1.5 riastrad if (!connector->has_tile) {
4684 1.5 riastrad ret = drm_property_replace_global_blob(dev,
4685 1.5 riastrad &connector->tile_blob_ptr,
4686 1.5 riastrad 0,
4687 1.5 riastrad NULL,
4688 1.5 riastrad &connector->base,
4689 1.5 riastrad dev->mode_config.tile_property);
4690 1.5 riastrad return ret;
4691 1.5 riastrad }
4692 1.5 riastrad
4693 1.5 riastrad snprintf(tile, 256, "%d:%d:%d:%d:%d:%d:%d:%d",
4694 1.5 riastrad connector->tile_group->id, connector->tile_is_single_monitor,
4695 1.5 riastrad connector->num_h_tile, connector->num_v_tile,
4696 1.5 riastrad connector->tile_h_loc, connector->tile_v_loc,
4697 1.5 riastrad connector->tile_h_size, connector->tile_v_size);
4698 1.5 riastrad
4699 1.5 riastrad ret = drm_property_replace_global_blob(dev,
4700 1.5 riastrad &connector->tile_blob_ptr,
4701 1.5 riastrad strlen(tile) + 1,
4702 1.5 riastrad tile,
4703 1.5 riastrad &connector->base,
4704 1.5 riastrad dev->mode_config.tile_property);
4705 1.5 riastrad return ret;
4706 1.5 riastrad }
4707 1.5 riastrad EXPORT_SYMBOL(drm_mode_connector_set_tile_property);
4708 1.5 riastrad
4709 1.5 riastrad /**
4710 1.3 riastrad * drm_mode_connector_update_edid_property - update the edid property of a connector
4711 1.3 riastrad * @connector: drm connector
4712 1.3 riastrad * @edid: new value of the edid property
4713 1.3 riastrad *
4714 1.3 riastrad * This function creates a new blob modeset object and assigns its id to the
4715 1.3 riastrad * connector's edid property.
4716 1.3 riastrad *
4717 1.3 riastrad * Returns:
4718 1.5 riastrad * Zero on success, negative errno on failure.
4719 1.3 riastrad */
4720 1.1 riastrad int drm_mode_connector_update_edid_property(struct drm_connector *connector,
4721 1.5 riastrad const struct edid *edid)
4722 1.1 riastrad {
4723 1.1 riastrad struct drm_device *dev = connector->dev;
4724 1.5 riastrad size_t size = 0;
4725 1.5 riastrad int ret;
4726 1.1 riastrad
4727 1.5 riastrad /* ignore requests to set edid when overridden */
4728 1.5 riastrad if (connector->override_edid)
4729 1.5 riastrad return 0;
4730 1.1 riastrad
4731 1.5 riastrad if (edid)
4732 1.5 riastrad size = EDID_LENGTH * (1 + edid->extensions);
4733 1.1 riastrad
4734 1.5 riastrad ret = drm_property_replace_global_blob(dev,
4735 1.5 riastrad &connector->edid_blob_ptr,
4736 1.5 riastrad size,
4737 1.5 riastrad edid,
4738 1.5 riastrad &connector->base,
4739 1.5 riastrad dev->mode_config.edid_property);
4740 1.1 riastrad return ret;
4741 1.1 riastrad }
4742 1.1 riastrad EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
4743 1.1 riastrad
4744 1.5 riastrad /* Some properties could refer to dynamic refcnt'd objects, or things that
4745 1.5 riastrad * need special locking to handle lifetime issues (ie. to ensure the prop
4746 1.5 riastrad * value doesn't become invalid part way through the property update due to
4747 1.5 riastrad * race). The value returned by reference via 'obj' should be passed back
4748 1.5 riastrad * to drm_property_change_valid_put() after the property is set (and the
4749 1.5 riastrad * object to which the property is attached has a chance to take it's own
4750 1.5 riastrad * reference).
4751 1.5 riastrad */
4752 1.5 riastrad bool drm_property_change_valid_get(struct drm_property *property,
4753 1.5 riastrad uint64_t value, struct drm_mode_object **ref)
4754 1.1 riastrad {
4755 1.5 riastrad int i;
4756 1.5 riastrad
4757 1.1 riastrad if (property->flags & DRM_MODE_PROP_IMMUTABLE)
4758 1.1 riastrad return false;
4759 1.5 riastrad
4760 1.5 riastrad *ref = NULL;
4761 1.5 riastrad
4762 1.5 riastrad if (drm_property_type_is(property, DRM_MODE_PROP_RANGE)) {
4763 1.1 riastrad if (value < property->values[0] || value > property->values[1])
4764 1.1 riastrad return false;
4765 1.1 riastrad return true;
4766 1.5 riastrad } else if (drm_property_type_is(property, DRM_MODE_PROP_SIGNED_RANGE)) {
4767 1.5 riastrad int64_t svalue = U642I64(value);
4768 1.5 riastrad
4769 1.5 riastrad if (svalue < U642I64(property->values[0]) ||
4770 1.5 riastrad svalue > U642I64(property->values[1]))
4771 1.5 riastrad return false;
4772 1.5 riastrad return true;
4773 1.5 riastrad } else if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
4774 1.1 riastrad uint64_t valid_mask = 0;
4775 1.5 riastrad
4776 1.1 riastrad for (i = 0; i < property->num_values; i++)
4777 1.1 riastrad valid_mask |= (1ULL << property->values[i]);
4778 1.1 riastrad return !(value & ~valid_mask);
4779 1.5 riastrad } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
4780 1.5 riastrad struct drm_property_blob *blob;
4781 1.5 riastrad
4782 1.5 riastrad if (value == 0)
4783 1.5 riastrad return true;
4784 1.5 riastrad
4785 1.5 riastrad blob = drm_property_lookup_blob(property->dev, value);
4786 1.5 riastrad if (blob) {
4787 1.5 riastrad *ref = &blob->base;
4788 1.5 riastrad return true;
4789 1.5 riastrad } else {
4790 1.5 riastrad return false;
4791 1.5 riastrad }
4792 1.5 riastrad } else if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
4793 1.5 riastrad /* a zero value for an object property translates to null: */
4794 1.5 riastrad if (value == 0)
4795 1.5 riastrad return true;
4796 1.5 riastrad
4797 1.5 riastrad /* handle refcnt'd objects specially: */
4798 1.5 riastrad if (property->values[0] == DRM_MODE_OBJECT_FB) {
4799 1.5 riastrad struct drm_framebuffer *fb;
4800 1.5 riastrad fb = drm_framebuffer_lookup(property->dev, value);
4801 1.5 riastrad if (fb) {
4802 1.5 riastrad *ref = &fb->base;
4803 1.1 riastrad return true;
4804 1.5 riastrad } else {
4805 1.5 riastrad return false;
4806 1.5 riastrad }
4807 1.5 riastrad } else {
4808 1.5 riastrad return _object_find(property->dev, value, property->values[0]) != NULL;
4809 1.5 riastrad }
4810 1.3 riastrad }
4811 1.5 riastrad
4812 1.5 riastrad for (i = 0; i < property->num_values; i++)
4813 1.5 riastrad if (property->values[i] == value)
4814 1.5 riastrad return true;
4815 1.5 riastrad return false;
4816 1.5 riastrad }
4817 1.5 riastrad
4818 1.5 riastrad void drm_property_change_valid_put(struct drm_property *property,
4819 1.5 riastrad struct drm_mode_object *ref)
4820 1.5 riastrad {
4821 1.5 riastrad if (!ref)
4822 1.5 riastrad return;
4823 1.5 riastrad
4824 1.5 riastrad if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
4825 1.5 riastrad if (property->values[0] == DRM_MODE_OBJECT_FB)
4826 1.5 riastrad drm_framebuffer_unreference(obj_to_fb(ref));
4827 1.5 riastrad } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
4828 1.5 riastrad drm_property_unreference_blob(obj_to_blob(ref));
4829 1.3 riastrad }
4830 1.3 riastrad
4831 1.3 riastrad /**
4832 1.3 riastrad * drm_mode_connector_property_set_ioctl - set the current value of a connector property
4833 1.3 riastrad * @dev: DRM device
4834 1.3 riastrad * @data: ioctl data
4835 1.3 riastrad * @file_priv: DRM file info
4836 1.3 riastrad *
4837 1.3 riastrad * This function sets the current value for a connectors's property. It also
4838 1.3 riastrad * calls into a driver's ->set_property callback to update the hardware state
4839 1.3 riastrad *
4840 1.3 riastrad * Called by the user via ioctl.
4841 1.3 riastrad *
4842 1.3 riastrad * Returns:
4843 1.5 riastrad * Zero on success, negative errno on failure.
4844 1.3 riastrad */
4845 1.1 riastrad int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
4846 1.1 riastrad void *data, struct drm_file *file_priv)
4847 1.1 riastrad {
4848 1.1 riastrad struct drm_mode_connector_set_property *conn_set_prop = data;
4849 1.1 riastrad struct drm_mode_obj_set_property obj_set_prop = {
4850 1.1 riastrad .value = conn_set_prop->value,
4851 1.1 riastrad .prop_id = conn_set_prop->prop_id,
4852 1.1 riastrad .obj_id = conn_set_prop->connector_id,
4853 1.1 riastrad .obj_type = DRM_MODE_OBJECT_CONNECTOR
4854 1.1 riastrad };
4855 1.1 riastrad
4856 1.1 riastrad /* It does all the locking and checking we need */
4857 1.1 riastrad return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
4858 1.1 riastrad }
4859 1.1 riastrad
4860 1.1 riastrad static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
4861 1.1 riastrad struct drm_property *property,
4862 1.1 riastrad uint64_t value)
4863 1.1 riastrad {
4864 1.1 riastrad int ret = -EINVAL;
4865 1.1 riastrad struct drm_connector *connector = obj_to_connector(obj);
4866 1.1 riastrad
4867 1.1 riastrad /* Do DPMS ourselves */
4868 1.1 riastrad if (property == connector->dev->mode_config.dpms_property) {
4869 1.5 riastrad ret = 0;
4870 1.1 riastrad if (connector->funcs->dpms)
4871 1.5 riastrad ret = (*connector->funcs->dpms)(connector, (int)value);
4872 1.1 riastrad } else if (connector->funcs->set_property)
4873 1.1 riastrad ret = connector->funcs->set_property(connector, property, value);
4874 1.1 riastrad
4875 1.1 riastrad /* store the property value if successful */
4876 1.1 riastrad if (!ret)
4877 1.1 riastrad drm_object_property_set_value(&connector->base, property, value);
4878 1.1 riastrad return ret;
4879 1.1 riastrad }
4880 1.1 riastrad
4881 1.1 riastrad static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
4882 1.1 riastrad struct drm_property *property,
4883 1.1 riastrad uint64_t value)
4884 1.1 riastrad {
4885 1.1 riastrad int ret = -EINVAL;
4886 1.1 riastrad struct drm_crtc *crtc = obj_to_crtc(obj);
4887 1.1 riastrad
4888 1.1 riastrad if (crtc->funcs->set_property)
4889 1.1 riastrad ret = crtc->funcs->set_property(crtc, property, value);
4890 1.1 riastrad if (!ret)
4891 1.1 riastrad drm_object_property_set_value(obj, property, value);
4892 1.1 riastrad
4893 1.1 riastrad return ret;
4894 1.1 riastrad }
4895 1.1 riastrad
4896 1.5 riastrad /**
4897 1.5 riastrad * drm_mode_plane_set_obj_prop - set the value of a property
4898 1.5 riastrad * @plane: drm plane object to set property value for
4899 1.5 riastrad * @property: property to set
4900 1.5 riastrad * @value: value the property should be set to
4901 1.5 riastrad *
4902 1.5 riastrad * This functions sets a given property on a given plane object. This function
4903 1.5 riastrad * calls the driver's ->set_property callback and changes the software state of
4904 1.5 riastrad * the property if the callback succeeds.
4905 1.5 riastrad *
4906 1.5 riastrad * Returns:
4907 1.5 riastrad * Zero on success, error code on failure.
4908 1.5 riastrad */
4909 1.5 riastrad int drm_mode_plane_set_obj_prop(struct drm_plane *plane,
4910 1.5 riastrad struct drm_property *property,
4911 1.5 riastrad uint64_t value)
4912 1.1 riastrad {
4913 1.1 riastrad int ret = -EINVAL;
4914 1.5 riastrad struct drm_mode_object *obj = &plane->base;
4915 1.1 riastrad
4916 1.1 riastrad if (plane->funcs->set_property)
4917 1.1 riastrad ret = plane->funcs->set_property(plane, property, value);
4918 1.1 riastrad if (!ret)
4919 1.1 riastrad drm_object_property_set_value(obj, property, value);
4920 1.1 riastrad
4921 1.1 riastrad return ret;
4922 1.1 riastrad }
4923 1.5 riastrad EXPORT_SYMBOL(drm_mode_plane_set_obj_prop);
4924 1.1 riastrad
4925 1.3 riastrad /**
4926 1.5 riastrad * drm_mode_obj_get_properties_ioctl - get the current value of a object's property
4927 1.3 riastrad * @dev: DRM device
4928 1.3 riastrad * @data: ioctl data
4929 1.3 riastrad * @file_priv: DRM file info
4930 1.3 riastrad *
4931 1.3 riastrad * This function retrieves the current value for an object's property. Compared
4932 1.3 riastrad * to the connector specific ioctl this one is extended to also work on crtc and
4933 1.3 riastrad * plane objects.
4934 1.3 riastrad *
4935 1.3 riastrad * Called by the user via ioctl.
4936 1.3 riastrad *
4937 1.3 riastrad * Returns:
4938 1.5 riastrad * Zero on success, negative errno on failure.
4939 1.3 riastrad */
4940 1.1 riastrad int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
4941 1.1 riastrad struct drm_file *file_priv)
4942 1.1 riastrad {
4943 1.1 riastrad struct drm_mode_obj_get_properties *arg = data;
4944 1.1 riastrad struct drm_mode_object *obj;
4945 1.1 riastrad int ret = 0;
4946 1.1 riastrad
4947 1.1 riastrad if (!drm_core_check_feature(dev, DRIVER_MODESET))
4948 1.1 riastrad return -EINVAL;
4949 1.1 riastrad
4950 1.3 riastrad drm_modeset_lock_all(dev);
4951 1.1 riastrad
4952 1.1 riastrad obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
4953 1.1 riastrad if (!obj) {
4954 1.3 riastrad ret = -ENOENT;
4955 1.1 riastrad goto out;
4956 1.1 riastrad }
4957 1.1 riastrad if (!obj->properties) {
4958 1.1 riastrad ret = -EINVAL;
4959 1.1 riastrad goto out;
4960 1.1 riastrad }
4961 1.1 riastrad
4962 1.5 riastrad ret = get_properties(obj, file_priv->atomic,
4963 1.5 riastrad (uint32_t __user *)(unsigned long)(arg->props_ptr),
4964 1.5 riastrad (uint64_t __user *)(unsigned long)(arg->prop_values_ptr),
4965 1.5 riastrad &arg->count_props);
4966 1.1 riastrad
4967 1.1 riastrad out:
4968 1.3 riastrad drm_modeset_unlock_all(dev);
4969 1.1 riastrad return ret;
4970 1.1 riastrad }
4971 1.1 riastrad
4972 1.3 riastrad /**
4973 1.3 riastrad * drm_mode_obj_set_property_ioctl - set the current value of an object's property
4974 1.3 riastrad * @dev: DRM device
4975 1.3 riastrad * @data: ioctl data
4976 1.3 riastrad * @file_priv: DRM file info
4977 1.3 riastrad *
4978 1.3 riastrad * This function sets the current value for an object's property. It also calls
4979 1.3 riastrad * into a driver's ->set_property callback to update the hardware state.
4980 1.3 riastrad * Compared to the connector specific ioctl this one is extended to also work on
4981 1.3 riastrad * crtc and plane objects.
4982 1.3 riastrad *
4983 1.3 riastrad * Called by the user via ioctl.
4984 1.3 riastrad *
4985 1.3 riastrad * Returns:
4986 1.5 riastrad * Zero on success, negative errno on failure.
4987 1.3 riastrad */
4988 1.1 riastrad int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
4989 1.1 riastrad struct drm_file *file_priv)
4990 1.1 riastrad {
4991 1.1 riastrad struct drm_mode_obj_set_property *arg = data;
4992 1.1 riastrad struct drm_mode_object *arg_obj;
4993 1.1 riastrad struct drm_mode_object *prop_obj;
4994 1.1 riastrad struct drm_property *property;
4995 1.5 riastrad int i, ret = -EINVAL;
4996 1.5 riastrad struct drm_mode_object *ref;
4997 1.1 riastrad
4998 1.1 riastrad if (!drm_core_check_feature(dev, DRIVER_MODESET))
4999 1.1 riastrad return -EINVAL;
5000 1.1 riastrad
5001 1.3 riastrad drm_modeset_lock_all(dev);
5002 1.1 riastrad
5003 1.1 riastrad arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
5004 1.3 riastrad if (!arg_obj) {
5005 1.3 riastrad ret = -ENOENT;
5006 1.1 riastrad goto out;
5007 1.3 riastrad }
5008 1.1 riastrad if (!arg_obj->properties)
5009 1.1 riastrad goto out;
5010 1.1 riastrad
5011 1.1 riastrad for (i = 0; i < arg_obj->properties->count; i++)
5012 1.5 riastrad if (arg_obj->properties->properties[i]->base.id == arg->prop_id)
5013 1.1 riastrad break;
5014 1.1 riastrad
5015 1.1 riastrad if (i == arg_obj->properties->count)
5016 1.1 riastrad goto out;
5017 1.1 riastrad
5018 1.1 riastrad prop_obj = drm_mode_object_find(dev, arg->prop_id,
5019 1.1 riastrad DRM_MODE_OBJECT_PROPERTY);
5020 1.3 riastrad if (!prop_obj) {
5021 1.3 riastrad ret = -ENOENT;
5022 1.1 riastrad goto out;
5023 1.3 riastrad }
5024 1.1 riastrad property = obj_to_property(prop_obj);
5025 1.1 riastrad
5026 1.5 riastrad if (!drm_property_change_valid_get(property, arg->value, &ref))
5027 1.1 riastrad goto out;
5028 1.1 riastrad
5029 1.1 riastrad switch (arg_obj->type) {
5030 1.1 riastrad case DRM_MODE_OBJECT_CONNECTOR:
5031 1.1 riastrad ret = drm_mode_connector_set_obj_prop(arg_obj, property,
5032 1.1 riastrad arg->value);
5033 1.1 riastrad break;
5034 1.1 riastrad case DRM_MODE_OBJECT_CRTC:
5035 1.1 riastrad ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
5036 1.1 riastrad break;
5037 1.1 riastrad case DRM_MODE_OBJECT_PLANE:
5038 1.5 riastrad ret = drm_mode_plane_set_obj_prop(obj_to_plane(arg_obj),
5039 1.5 riastrad property, arg->value);
5040 1.1 riastrad break;
5041 1.1 riastrad }
5042 1.1 riastrad
5043 1.5 riastrad drm_property_change_valid_put(property, ref);
5044 1.5 riastrad
5045 1.1 riastrad out:
5046 1.3 riastrad drm_modeset_unlock_all(dev);
5047 1.1 riastrad return ret;
5048 1.1 riastrad }
5049 1.1 riastrad
5050 1.3 riastrad /**
5051 1.3 riastrad * drm_mode_connector_attach_encoder - attach a connector to an encoder
5052 1.3 riastrad * @connector: connector to attach
5053 1.3 riastrad * @encoder: encoder to attach @connector to
5054 1.3 riastrad *
5055 1.3 riastrad * This function links up a connector to an encoder. Note that the routing
5056 1.3 riastrad * restrictions between encoders and crtcs are exposed to userspace through the
5057 1.3 riastrad * possible_clones and possible_crtcs bitmasks.
5058 1.3 riastrad *
5059 1.3 riastrad * Returns:
5060 1.5 riastrad * Zero on success, negative errno on failure.
5061 1.3 riastrad */
5062 1.1 riastrad int drm_mode_connector_attach_encoder(struct drm_connector *connector,
5063 1.1 riastrad struct drm_encoder *encoder)
5064 1.1 riastrad {
5065 1.1 riastrad int i;
5066 1.1 riastrad
5067 1.1 riastrad for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
5068 1.1 riastrad if (connector->encoder_ids[i] == 0) {
5069 1.1 riastrad connector->encoder_ids[i] = encoder->base.id;
5070 1.1 riastrad return 0;
5071 1.1 riastrad }
5072 1.1 riastrad }
5073 1.1 riastrad return -ENOMEM;
5074 1.1 riastrad }
5075 1.1 riastrad EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
5076 1.1 riastrad
5077 1.3 riastrad /**
5078 1.3 riastrad * drm_mode_crtc_set_gamma_size - set the gamma table size
5079 1.3 riastrad * @crtc: CRTC to set the gamma table size for
5080 1.3 riastrad * @gamma_size: size of the gamma table
5081 1.3 riastrad *
5082 1.3 riastrad * Drivers which support gamma tables should set this to the supported gamma
5083 1.3 riastrad * table size when initializing the CRTC. Currently the drm core only supports a
5084 1.3 riastrad * fixed gamma table size.
5085 1.3 riastrad *
5086 1.3 riastrad * Returns:
5087 1.5 riastrad * Zero on success, negative errno on failure.
5088 1.3 riastrad */
5089 1.1 riastrad int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
5090 1.3 riastrad int gamma_size)
5091 1.1 riastrad {
5092 1.1 riastrad crtc->gamma_size = gamma_size;
5093 1.1 riastrad
5094 1.5 riastrad crtc->gamma_store = kcalloc(gamma_size, sizeof(uint16_t) * 3,
5095 1.5 riastrad GFP_KERNEL);
5096 1.1 riastrad if (!crtc->gamma_store) {
5097 1.1 riastrad crtc->gamma_size = 0;
5098 1.1 riastrad return -ENOMEM;
5099 1.1 riastrad }
5100 1.1 riastrad
5101 1.1 riastrad return 0;
5102 1.1 riastrad }
5103 1.1 riastrad EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
5104 1.1 riastrad
5105 1.3 riastrad /**
5106 1.3 riastrad * drm_mode_gamma_set_ioctl - set the gamma table
5107 1.3 riastrad * @dev: DRM device
5108 1.3 riastrad * @data: ioctl data
5109 1.3 riastrad * @file_priv: DRM file info
5110 1.3 riastrad *
5111 1.3 riastrad * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
5112 1.3 riastrad * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
5113 1.3 riastrad *
5114 1.3 riastrad * Called by the user via ioctl.
5115 1.3 riastrad *
5116 1.3 riastrad * Returns:
5117 1.5 riastrad * Zero on success, negative errno on failure.
5118 1.3 riastrad */
5119 1.1 riastrad int drm_mode_gamma_set_ioctl(struct drm_device *dev,
5120 1.1 riastrad void *data, struct drm_file *file_priv)
5121 1.1 riastrad {
5122 1.1 riastrad struct drm_mode_crtc_lut *crtc_lut = data;
5123 1.1 riastrad struct drm_crtc *crtc;
5124 1.1 riastrad void *r_base, *g_base, *b_base;
5125 1.1 riastrad int size;
5126 1.1 riastrad int ret = 0;
5127 1.1 riastrad
5128 1.1 riastrad if (!drm_core_check_feature(dev, DRIVER_MODESET))
5129 1.1 riastrad return -EINVAL;
5130 1.1 riastrad
5131 1.3 riastrad drm_modeset_lock_all(dev);
5132 1.5 riastrad crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
5133 1.5 riastrad if (!crtc) {
5134 1.3 riastrad ret = -ENOENT;
5135 1.1 riastrad goto out;
5136 1.1 riastrad }
5137 1.1 riastrad
5138 1.1 riastrad if (crtc->funcs->gamma_set == NULL) {
5139 1.1 riastrad ret = -ENOSYS;
5140 1.1 riastrad goto out;
5141 1.1 riastrad }
5142 1.1 riastrad
5143 1.1 riastrad /* memcpy into gamma store */
5144 1.1 riastrad if (crtc_lut->gamma_size != crtc->gamma_size) {
5145 1.1 riastrad ret = -EINVAL;
5146 1.1 riastrad goto out;
5147 1.1 riastrad }
5148 1.1 riastrad
5149 1.1 riastrad size = crtc_lut->gamma_size * (sizeof(uint16_t));
5150 1.1 riastrad r_base = crtc->gamma_store;
5151 1.1 riastrad if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
5152 1.1 riastrad ret = -EFAULT;
5153 1.1 riastrad goto out;
5154 1.1 riastrad }
5155 1.1 riastrad
5156 1.12 riastrad g_base = r_base + size;
5157 1.1 riastrad if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
5158 1.1 riastrad ret = -EFAULT;
5159 1.1 riastrad goto out;
5160 1.1 riastrad }
5161 1.1 riastrad
5162 1.12 riastrad b_base = g_base + size;
5163 1.1 riastrad if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
5164 1.1 riastrad ret = -EFAULT;
5165 1.1 riastrad goto out;
5166 1.1 riastrad }
5167 1.1 riastrad
5168 1.1 riastrad crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
5169 1.1 riastrad
5170 1.1 riastrad out:
5171 1.3 riastrad drm_modeset_unlock_all(dev);
5172 1.1 riastrad return ret;
5173 1.1 riastrad
5174 1.1 riastrad }
5175 1.1 riastrad
5176 1.3 riastrad /**
5177 1.3 riastrad * drm_mode_gamma_get_ioctl - get the gamma table
5178 1.3 riastrad * @dev: DRM device
5179 1.3 riastrad * @data: ioctl data
5180 1.3 riastrad * @file_priv: DRM file info
5181 1.3 riastrad *
5182 1.3 riastrad * Copy the current gamma table into the storage provided. This also provides
5183 1.3 riastrad * the gamma table size the driver expects, which can be used to size the
5184 1.3 riastrad * allocated storage.
5185 1.3 riastrad *
5186 1.3 riastrad * Called by the user via ioctl.
5187 1.3 riastrad *
5188 1.3 riastrad * Returns:
5189 1.5 riastrad * Zero on success, negative errno on failure.
5190 1.3 riastrad */
5191 1.1 riastrad int drm_mode_gamma_get_ioctl(struct drm_device *dev,
5192 1.1 riastrad void *data, struct drm_file *file_priv)
5193 1.1 riastrad {
5194 1.1 riastrad struct drm_mode_crtc_lut *crtc_lut = data;
5195 1.1 riastrad struct drm_crtc *crtc;
5196 1.1 riastrad void *r_base, *g_base, *b_base;
5197 1.1 riastrad int size;
5198 1.1 riastrad int ret = 0;
5199 1.1 riastrad
5200 1.1 riastrad if (!drm_core_check_feature(dev, DRIVER_MODESET))
5201 1.1 riastrad return -EINVAL;
5202 1.1 riastrad
5203 1.3 riastrad drm_modeset_lock_all(dev);
5204 1.5 riastrad crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
5205 1.5 riastrad if (!crtc) {
5206 1.3 riastrad ret = -ENOENT;
5207 1.1 riastrad goto out;
5208 1.1 riastrad }
5209 1.1 riastrad
5210 1.1 riastrad /* memcpy into gamma store */
5211 1.1 riastrad if (crtc_lut->gamma_size != crtc->gamma_size) {
5212 1.1 riastrad ret = -EINVAL;
5213 1.1 riastrad goto out;
5214 1.1 riastrad }
5215 1.1 riastrad
5216 1.1 riastrad size = crtc_lut->gamma_size * (sizeof(uint16_t));
5217 1.1 riastrad r_base = crtc->gamma_store;
5218 1.1 riastrad if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
5219 1.1 riastrad ret = -EFAULT;
5220 1.1 riastrad goto out;
5221 1.1 riastrad }
5222 1.1 riastrad
5223 1.12 riastrad g_base = r_base + size;
5224 1.1 riastrad if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
5225 1.1 riastrad ret = -EFAULT;
5226 1.1 riastrad goto out;
5227 1.1 riastrad }
5228 1.1 riastrad
5229 1.12 riastrad b_base = g_base + size;
5230 1.1 riastrad if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
5231 1.1 riastrad ret = -EFAULT;
5232 1.1 riastrad goto out;
5233 1.1 riastrad }
5234 1.1 riastrad out:
5235 1.3 riastrad drm_modeset_unlock_all(dev);
5236 1.1 riastrad return ret;
5237 1.1 riastrad }
5238 1.1 riastrad
5239 1.3 riastrad /**
5240 1.3 riastrad * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
5241 1.3 riastrad * @dev: DRM device
5242 1.3 riastrad * @data: ioctl data
5243 1.3 riastrad * @file_priv: DRM file info
5244 1.3 riastrad *
5245 1.3 riastrad * This schedules an asynchronous update on a given CRTC, called page flip.
5246 1.3 riastrad * Optionally a drm event is generated to signal the completion of the event.
5247 1.3 riastrad * Generic drivers cannot assume that a pageflip with changed framebuffer
5248 1.3 riastrad * properties (including driver specific metadata like tiling layout) will work,
5249 1.3 riastrad * but some drivers support e.g. pixel format changes through the pageflip
5250 1.3 riastrad * ioctl.
5251 1.3 riastrad *
5252 1.3 riastrad * Called by the user via ioctl.
5253 1.3 riastrad *
5254 1.3 riastrad * Returns:
5255 1.5 riastrad * Zero on success, negative errno on failure.
5256 1.3 riastrad */
5257 1.1 riastrad int drm_mode_page_flip_ioctl(struct drm_device *dev,
5258 1.1 riastrad void *data, struct drm_file *file_priv)
5259 1.1 riastrad {
5260 1.1 riastrad struct drm_mode_crtc_page_flip *page_flip = data;
5261 1.1 riastrad struct drm_crtc *crtc;
5262 1.5 riastrad struct drm_framebuffer *fb = NULL;
5263 1.1 riastrad struct drm_pending_vblank_event *e = NULL;
5264 1.1 riastrad unsigned long flags;
5265 1.1 riastrad int ret = -EINVAL;
5266 1.1 riastrad
5267 1.5 riastrad if (!drm_core_check_feature(dev, DRIVER_MODESET))
5268 1.5 riastrad return -EINVAL;
5269 1.5 riastrad
5270 1.1 riastrad if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
5271 1.1 riastrad page_flip->reserved != 0)
5272 1.1 riastrad return -EINVAL;
5273 1.1 riastrad
5274 1.3 riastrad if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
5275 1.3 riastrad return -EINVAL;
5276 1.3 riastrad
5277 1.5 riastrad crtc = drm_crtc_find(dev, page_flip->crtc_id);
5278 1.5 riastrad if (!crtc)
5279 1.3 riastrad return -ENOENT;
5280 1.1 riastrad
5281 1.5 riastrad drm_modeset_lock_crtc(crtc, crtc->primary);
5282 1.3 riastrad if (crtc->primary->fb == NULL) {
5283 1.1 riastrad /* The framebuffer is currently unbound, presumably
5284 1.1 riastrad * due to a hotplug event, that userspace has not
5285 1.1 riastrad * yet discovered.
5286 1.1 riastrad */
5287 1.1 riastrad ret = -EBUSY;
5288 1.1 riastrad goto out;
5289 1.1 riastrad }
5290 1.1 riastrad
5291 1.1 riastrad if (crtc->funcs->page_flip == NULL)
5292 1.1 riastrad goto out;
5293 1.1 riastrad
5294 1.3 riastrad fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
5295 1.3 riastrad if (!fb) {
5296 1.3 riastrad ret = -ENOENT;
5297 1.1 riastrad goto out;
5298 1.3 riastrad }
5299 1.1 riastrad
5300 1.5 riastrad if (crtc->state) {
5301 1.5 riastrad const struct drm_plane_state *state = crtc->primary->state;
5302 1.5 riastrad
5303 1.5 riastrad ret = check_src_coords(state->src_x, state->src_y,
5304 1.5 riastrad state->src_w, state->src_h, fb);
5305 1.5 riastrad } else {
5306 1.5 riastrad ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
5307 1.5 riastrad }
5308 1.3 riastrad if (ret)
5309 1.3 riastrad goto out;
5310 1.1 riastrad
5311 1.3 riastrad if (crtc->primary->fb->pixel_format != fb->pixel_format) {
5312 1.3 riastrad DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
5313 1.3 riastrad ret = -EINVAL;
5314 1.1 riastrad goto out;
5315 1.1 riastrad }
5316 1.1 riastrad
5317 1.1 riastrad if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
5318 1.1 riastrad ret = -ENOMEM;
5319 1.1 riastrad spin_lock_irqsave(&dev->event_lock, flags);
5320 1.5 riastrad if (file_priv->event_space < sizeof(e->event)) {
5321 1.1 riastrad spin_unlock_irqrestore(&dev->event_lock, flags);
5322 1.1 riastrad goto out;
5323 1.1 riastrad }
5324 1.5 riastrad file_priv->event_space -= sizeof(e->event);
5325 1.1 riastrad spin_unlock_irqrestore(&dev->event_lock, flags);
5326 1.1 riastrad
5327 1.5 riastrad e = kzalloc(sizeof(*e), GFP_KERNEL);
5328 1.1 riastrad if (e == NULL) {
5329 1.1 riastrad spin_lock_irqsave(&dev->event_lock, flags);
5330 1.5 riastrad file_priv->event_space += sizeof(e->event);
5331 1.1 riastrad spin_unlock_irqrestore(&dev->event_lock, flags);
5332 1.1 riastrad goto out;
5333 1.1 riastrad }
5334 1.1 riastrad
5335 1.1 riastrad e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
5336 1.5 riastrad e->event.base.length = sizeof(e->event);
5337 1.1 riastrad e->event.user_data = page_flip->user_data;
5338 1.1 riastrad e->base.event = &e->event.base;
5339 1.1 riastrad e->base.file_priv = file_priv;
5340 1.1 riastrad e->base.destroy =
5341 1.1 riastrad (void (*) (struct drm_pending_event *)) kfree;
5342 1.1 riastrad }
5343 1.1 riastrad
5344 1.5 riastrad crtc->primary->old_fb = crtc->primary->fb;
5345 1.3 riastrad ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
5346 1.1 riastrad if (ret) {
5347 1.1 riastrad if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
5348 1.1 riastrad spin_lock_irqsave(&dev->event_lock, flags);
5349 1.5 riastrad file_priv->event_space += sizeof(e->event);
5350 1.1 riastrad spin_unlock_irqrestore(&dev->event_lock, flags);
5351 1.1 riastrad kfree(e);
5352 1.1 riastrad }
5353 1.3 riastrad /* Keep the old fb, don't unref it. */
5354 1.5 riastrad crtc->primary->old_fb = NULL;
5355 1.3 riastrad } else {
5356 1.5 riastrad crtc->primary->fb = fb;
5357 1.3 riastrad /* Unref only the old framebuffer. */
5358 1.3 riastrad fb = NULL;
5359 1.1 riastrad }
5360 1.1 riastrad
5361 1.1 riastrad out:
5362 1.3 riastrad if (fb)
5363 1.3 riastrad drm_framebuffer_unreference(fb);
5364 1.5 riastrad if (crtc->primary->old_fb)
5365 1.5 riastrad drm_framebuffer_unreference(crtc->primary->old_fb);
5366 1.5 riastrad crtc->primary->old_fb = NULL;
5367 1.5 riastrad drm_modeset_unlock_crtc(crtc);
5368 1.3 riastrad
5369 1.1 riastrad return ret;
5370 1.1 riastrad }
5371 1.1 riastrad
5372 1.3 riastrad /**
5373 1.3 riastrad * drm_mode_config_reset - call ->reset callbacks
5374 1.3 riastrad * @dev: drm device
5375 1.3 riastrad *
5376 1.3 riastrad * This functions calls all the crtc's, encoder's and connector's ->reset
5377 1.3 riastrad * callback. Drivers can use this in e.g. their driver load or resume code to
5378 1.3 riastrad * reset hardware and software state.
5379 1.3 riastrad */
5380 1.1 riastrad void drm_mode_config_reset(struct drm_device *dev)
5381 1.1 riastrad {
5382 1.1 riastrad struct drm_crtc *crtc;
5383 1.5 riastrad struct drm_plane *plane;
5384 1.1 riastrad struct drm_encoder *encoder;
5385 1.1 riastrad struct drm_connector *connector;
5386 1.1 riastrad
5387 1.5 riastrad drm_for_each_plane(plane, dev)
5388 1.5 riastrad if (plane->funcs->reset)
5389 1.5 riastrad plane->funcs->reset(plane);
5390 1.5 riastrad
5391 1.5 riastrad drm_for_each_crtc(crtc, dev)
5392 1.1 riastrad if (crtc->funcs->reset)
5393 1.1 riastrad crtc->funcs->reset(crtc);
5394 1.1 riastrad
5395 1.5 riastrad drm_for_each_encoder(encoder, dev)
5396 1.1 riastrad if (encoder->funcs->reset)
5397 1.1 riastrad encoder->funcs->reset(encoder);
5398 1.1 riastrad
5399 1.5 riastrad mutex_lock(&dev->mode_config.mutex);
5400 1.5 riastrad drm_for_each_connector(connector, dev)
5401 1.1 riastrad if (connector->funcs->reset)
5402 1.1 riastrad connector->funcs->reset(connector);
5403 1.5 riastrad mutex_unlock(&dev->mode_config.mutex);
5404 1.1 riastrad }
5405 1.1 riastrad EXPORT_SYMBOL(drm_mode_config_reset);
5406 1.1 riastrad
5407 1.3 riastrad /**
5408 1.3 riastrad * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
5409 1.3 riastrad * @dev: DRM device
5410 1.3 riastrad * @data: ioctl data
5411 1.3 riastrad * @file_priv: DRM file info
5412 1.3 riastrad *
5413 1.3 riastrad * This creates a new dumb buffer in the driver's backing storage manager (GEM,
5414 1.3 riastrad * TTM or something else entirely) and returns the resulting buffer handle. This
5415 1.3 riastrad * handle can then be wrapped up into a framebuffer modeset object.
5416 1.3 riastrad *
5417 1.3 riastrad * Note that userspace is not allowed to use such objects for render
5418 1.3 riastrad * acceleration - drivers must create their own private ioctls for such a use
5419 1.3 riastrad * case.
5420 1.3 riastrad *
5421 1.3 riastrad * Called by the user via ioctl.
5422 1.3 riastrad *
5423 1.3 riastrad * Returns:
5424 1.5 riastrad * Zero on success, negative errno on failure.
5425 1.3 riastrad */
5426 1.1 riastrad int drm_mode_create_dumb_ioctl(struct drm_device *dev,
5427 1.1 riastrad void *data, struct drm_file *file_priv)
5428 1.1 riastrad {
5429 1.1 riastrad struct drm_mode_create_dumb *args = data;
5430 1.3 riastrad u32 cpp, stride, size;
5431 1.1 riastrad
5432 1.1 riastrad if (!dev->driver->dumb_create)
5433 1.1 riastrad return -ENOSYS;
5434 1.3 riastrad if (!args->width || !args->height || !args->bpp)
5435 1.3 riastrad return -EINVAL;
5436 1.3 riastrad
5437 1.3 riastrad /* overflow checks for 32bit size calculations */
5438 1.5 riastrad /* NOTE: DIV_ROUND_UP() can overflow */
5439 1.3 riastrad cpp = DIV_ROUND_UP(args->bpp, 8);
5440 1.5 riastrad if (!cpp || cpp > 0xffffffffU / args->width)
5441 1.3 riastrad return -EINVAL;
5442 1.3 riastrad stride = cpp * args->width;
5443 1.3 riastrad if (args->height > 0xffffffffU / stride)
5444 1.3 riastrad return -EINVAL;
5445 1.3 riastrad
5446 1.3 riastrad /* test for wrap-around */
5447 1.3 riastrad size = args->height * stride;
5448 1.3 riastrad if (PAGE_ALIGN(size) == 0)
5449 1.3 riastrad return -EINVAL;
5450 1.3 riastrad
5451 1.5 riastrad /*
5452 1.5 riastrad * handle, pitch and size are output parameters. Zero them out to
5453 1.5 riastrad * prevent drivers from accidentally using uninitialized data. Since
5454 1.5 riastrad * not all existing userspace is clearing these fields properly we
5455 1.5 riastrad * cannot reject IOCTL with garbage in them.
5456 1.5 riastrad */
5457 1.5 riastrad args->handle = 0;
5458 1.5 riastrad args->pitch = 0;
5459 1.5 riastrad args->size = 0;
5460 1.5 riastrad
5461 1.1 riastrad return dev->driver->dumb_create(file_priv, dev, args);
5462 1.1 riastrad }
5463 1.1 riastrad
5464 1.3 riastrad /**
5465 1.3 riastrad * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
5466 1.3 riastrad * @dev: DRM device
5467 1.3 riastrad * @data: ioctl data
5468 1.3 riastrad * @file_priv: DRM file info
5469 1.3 riastrad *
5470 1.3 riastrad * Allocate an offset in the drm device node's address space to be able to
5471 1.3 riastrad * memory map a dumb buffer.
5472 1.3 riastrad *
5473 1.3 riastrad * Called by the user via ioctl.
5474 1.3 riastrad *
5475 1.3 riastrad * Returns:
5476 1.5 riastrad * Zero on success, negative errno on failure.
5477 1.3 riastrad */
5478 1.1 riastrad int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
5479 1.1 riastrad void *data, struct drm_file *file_priv)
5480 1.1 riastrad {
5481 1.1 riastrad struct drm_mode_map_dumb *args = data;
5482 1.1 riastrad
5483 1.1 riastrad /* call driver ioctl to get mmap offset */
5484 1.1 riastrad if (!dev->driver->dumb_map_offset)
5485 1.1 riastrad return -ENOSYS;
5486 1.1 riastrad
5487 1.1 riastrad return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
5488 1.1 riastrad }
5489 1.1 riastrad
5490 1.3 riastrad /**
5491 1.3 riastrad * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
5492 1.3 riastrad * @dev: DRM device
5493 1.3 riastrad * @data: ioctl data
5494 1.3 riastrad * @file_priv: DRM file info
5495 1.3 riastrad *
5496 1.3 riastrad * This destroys the userspace handle for the given dumb backing storage buffer.
5497 1.3 riastrad * Since buffer objects must be reference counted in the kernel a buffer object
5498 1.3 riastrad * won't be immediately freed if a framebuffer modeset object still uses it.
5499 1.3 riastrad *
5500 1.3 riastrad * Called by the user via ioctl.
5501 1.3 riastrad *
5502 1.3 riastrad * Returns:
5503 1.5 riastrad * Zero on success, negative errno on failure.
5504 1.3 riastrad */
5505 1.1 riastrad int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
5506 1.1 riastrad void *data, struct drm_file *file_priv)
5507 1.1 riastrad {
5508 1.1 riastrad struct drm_mode_destroy_dumb *args = data;
5509 1.1 riastrad
5510 1.1 riastrad if (!dev->driver->dumb_destroy)
5511 1.1 riastrad return -ENOSYS;
5512 1.1 riastrad
5513 1.1 riastrad return dev->driver->dumb_destroy(file_priv, dev, args->handle);
5514 1.1 riastrad }
5515 1.1 riastrad
5516 1.3 riastrad /**
5517 1.3 riastrad * drm_fb_get_bpp_depth - get the bpp/depth values for format
5518 1.3 riastrad * @format: pixel format (DRM_FORMAT_*)
5519 1.3 riastrad * @depth: storage for the depth value
5520 1.3 riastrad * @bpp: storage for the bpp value
5521 1.3 riastrad *
5522 1.3 riastrad * This only supports RGB formats here for compat with code that doesn't use
5523 1.3 riastrad * pixel formats directly yet.
5524 1.1 riastrad */
5525 1.1 riastrad void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
5526 1.1 riastrad int *bpp)
5527 1.1 riastrad {
5528 1.1 riastrad switch (format) {
5529 1.3 riastrad case DRM_FORMAT_C8:
5530 1.1 riastrad case DRM_FORMAT_RGB332:
5531 1.1 riastrad case DRM_FORMAT_BGR233:
5532 1.1 riastrad *depth = 8;
5533 1.1 riastrad *bpp = 8;
5534 1.1 riastrad break;
5535 1.1 riastrad case DRM_FORMAT_XRGB1555:
5536 1.1 riastrad case DRM_FORMAT_XBGR1555:
5537 1.1 riastrad case DRM_FORMAT_RGBX5551:
5538 1.1 riastrad case DRM_FORMAT_BGRX5551:
5539 1.1 riastrad case DRM_FORMAT_ARGB1555:
5540 1.1 riastrad case DRM_FORMAT_ABGR1555:
5541 1.1 riastrad case DRM_FORMAT_RGBA5551:
5542 1.1 riastrad case DRM_FORMAT_BGRA5551:
5543 1.1 riastrad *depth = 15;
5544 1.1 riastrad *bpp = 16;
5545 1.1 riastrad break;
5546 1.1 riastrad case DRM_FORMAT_RGB565:
5547 1.1 riastrad case DRM_FORMAT_BGR565:
5548 1.1 riastrad *depth = 16;
5549 1.1 riastrad *bpp = 16;
5550 1.1 riastrad break;
5551 1.1 riastrad case DRM_FORMAT_RGB888:
5552 1.1 riastrad case DRM_FORMAT_BGR888:
5553 1.1 riastrad *depth = 24;
5554 1.1 riastrad *bpp = 24;
5555 1.1 riastrad break;
5556 1.1 riastrad case DRM_FORMAT_XRGB8888:
5557 1.1 riastrad case DRM_FORMAT_XBGR8888:
5558 1.1 riastrad case DRM_FORMAT_RGBX8888:
5559 1.1 riastrad case DRM_FORMAT_BGRX8888:
5560 1.1 riastrad *depth = 24;
5561 1.1 riastrad *bpp = 32;
5562 1.1 riastrad break;
5563 1.1 riastrad case DRM_FORMAT_XRGB2101010:
5564 1.1 riastrad case DRM_FORMAT_XBGR2101010:
5565 1.1 riastrad case DRM_FORMAT_RGBX1010102:
5566 1.1 riastrad case DRM_FORMAT_BGRX1010102:
5567 1.1 riastrad case DRM_FORMAT_ARGB2101010:
5568 1.1 riastrad case DRM_FORMAT_ABGR2101010:
5569 1.1 riastrad case DRM_FORMAT_RGBA1010102:
5570 1.1 riastrad case DRM_FORMAT_BGRA1010102:
5571 1.1 riastrad *depth = 30;
5572 1.1 riastrad *bpp = 32;
5573 1.1 riastrad break;
5574 1.1 riastrad case DRM_FORMAT_ARGB8888:
5575 1.1 riastrad case DRM_FORMAT_ABGR8888:
5576 1.1 riastrad case DRM_FORMAT_RGBA8888:
5577 1.1 riastrad case DRM_FORMAT_BGRA8888:
5578 1.1 riastrad *depth = 32;
5579 1.1 riastrad *bpp = 32;
5580 1.1 riastrad break;
5581 1.1 riastrad default:
5582 1.3 riastrad DRM_DEBUG_KMS("unsupported pixel format %s\n",
5583 1.3 riastrad drm_get_format_name(format));
5584 1.1 riastrad *depth = 0;
5585 1.1 riastrad *bpp = 0;
5586 1.1 riastrad break;
5587 1.1 riastrad }
5588 1.1 riastrad }
5589 1.1 riastrad EXPORT_SYMBOL(drm_fb_get_bpp_depth);
5590 1.1 riastrad
5591 1.1 riastrad /**
5592 1.1 riastrad * drm_format_num_planes - get the number of planes for format
5593 1.1 riastrad * @format: pixel format (DRM_FORMAT_*)
5594 1.1 riastrad *
5595 1.3 riastrad * Returns:
5596 1.1 riastrad * The number of planes used by the specified pixel format.
5597 1.1 riastrad */
5598 1.1 riastrad int drm_format_num_planes(uint32_t format)
5599 1.1 riastrad {
5600 1.1 riastrad switch (format) {
5601 1.1 riastrad case DRM_FORMAT_YUV410:
5602 1.1 riastrad case DRM_FORMAT_YVU410:
5603 1.1 riastrad case DRM_FORMAT_YUV411:
5604 1.1 riastrad case DRM_FORMAT_YVU411:
5605 1.1 riastrad case DRM_FORMAT_YUV420:
5606 1.1 riastrad case DRM_FORMAT_YVU420:
5607 1.1 riastrad case DRM_FORMAT_YUV422:
5608 1.1 riastrad case DRM_FORMAT_YVU422:
5609 1.1 riastrad case DRM_FORMAT_YUV444:
5610 1.1 riastrad case DRM_FORMAT_YVU444:
5611 1.1 riastrad return 3;
5612 1.1 riastrad case DRM_FORMAT_NV12:
5613 1.1 riastrad case DRM_FORMAT_NV21:
5614 1.1 riastrad case DRM_FORMAT_NV16:
5615 1.1 riastrad case DRM_FORMAT_NV61:
5616 1.1 riastrad case DRM_FORMAT_NV24:
5617 1.1 riastrad case DRM_FORMAT_NV42:
5618 1.1 riastrad return 2;
5619 1.1 riastrad default:
5620 1.1 riastrad return 1;
5621 1.1 riastrad }
5622 1.1 riastrad }
5623 1.1 riastrad EXPORT_SYMBOL(drm_format_num_planes);
5624 1.1 riastrad
5625 1.1 riastrad /**
5626 1.1 riastrad * drm_format_plane_cpp - determine the bytes per pixel value
5627 1.1 riastrad * @format: pixel format (DRM_FORMAT_*)
5628 1.1 riastrad * @plane: plane index
5629 1.1 riastrad *
5630 1.3 riastrad * Returns:
5631 1.1 riastrad * The bytes per pixel value for the specified plane.
5632 1.1 riastrad */
5633 1.1 riastrad int drm_format_plane_cpp(uint32_t format, int plane)
5634 1.1 riastrad {
5635 1.1 riastrad unsigned int depth;
5636 1.1 riastrad int bpp;
5637 1.1 riastrad
5638 1.1 riastrad if (plane >= drm_format_num_planes(format))
5639 1.1 riastrad return 0;
5640 1.1 riastrad
5641 1.1 riastrad switch (format) {
5642 1.1 riastrad case DRM_FORMAT_YUYV:
5643 1.1 riastrad case DRM_FORMAT_YVYU:
5644 1.1 riastrad case DRM_FORMAT_UYVY:
5645 1.1 riastrad case DRM_FORMAT_VYUY:
5646 1.1 riastrad return 2;
5647 1.1 riastrad case DRM_FORMAT_NV12:
5648 1.1 riastrad case DRM_FORMAT_NV21:
5649 1.1 riastrad case DRM_FORMAT_NV16:
5650 1.1 riastrad case DRM_FORMAT_NV61:
5651 1.1 riastrad case DRM_FORMAT_NV24:
5652 1.1 riastrad case DRM_FORMAT_NV42:
5653 1.1 riastrad return plane ? 2 : 1;
5654 1.1 riastrad case DRM_FORMAT_YUV410:
5655 1.1 riastrad case DRM_FORMAT_YVU410:
5656 1.1 riastrad case DRM_FORMAT_YUV411:
5657 1.1 riastrad case DRM_FORMAT_YVU411:
5658 1.1 riastrad case DRM_FORMAT_YUV420:
5659 1.1 riastrad case DRM_FORMAT_YVU420:
5660 1.1 riastrad case DRM_FORMAT_YUV422:
5661 1.1 riastrad case DRM_FORMAT_YVU422:
5662 1.1 riastrad case DRM_FORMAT_YUV444:
5663 1.1 riastrad case DRM_FORMAT_YVU444:
5664 1.1 riastrad return 1;
5665 1.1 riastrad default:
5666 1.1 riastrad drm_fb_get_bpp_depth(format, &depth, &bpp);
5667 1.1 riastrad return bpp >> 3;
5668 1.1 riastrad }
5669 1.1 riastrad }
5670 1.1 riastrad EXPORT_SYMBOL(drm_format_plane_cpp);
5671 1.1 riastrad
5672 1.1 riastrad /**
5673 1.1 riastrad * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
5674 1.1 riastrad * @format: pixel format (DRM_FORMAT_*)
5675 1.1 riastrad *
5676 1.3 riastrad * Returns:
5677 1.1 riastrad * The horizontal chroma subsampling factor for the
5678 1.1 riastrad * specified pixel format.
5679 1.1 riastrad */
5680 1.1 riastrad int drm_format_horz_chroma_subsampling(uint32_t format)
5681 1.1 riastrad {
5682 1.1 riastrad switch (format) {
5683 1.1 riastrad case DRM_FORMAT_YUV411:
5684 1.1 riastrad case DRM_FORMAT_YVU411:
5685 1.1 riastrad case DRM_FORMAT_YUV410:
5686 1.1 riastrad case DRM_FORMAT_YVU410:
5687 1.1 riastrad return 4;
5688 1.1 riastrad case DRM_FORMAT_YUYV:
5689 1.1 riastrad case DRM_FORMAT_YVYU:
5690 1.1 riastrad case DRM_FORMAT_UYVY:
5691 1.1 riastrad case DRM_FORMAT_VYUY:
5692 1.1 riastrad case DRM_FORMAT_NV12:
5693 1.1 riastrad case DRM_FORMAT_NV21:
5694 1.1 riastrad case DRM_FORMAT_NV16:
5695 1.1 riastrad case DRM_FORMAT_NV61:
5696 1.1 riastrad case DRM_FORMAT_YUV422:
5697 1.1 riastrad case DRM_FORMAT_YVU422:
5698 1.1 riastrad case DRM_FORMAT_YUV420:
5699 1.1 riastrad case DRM_FORMAT_YVU420:
5700 1.1 riastrad return 2;
5701 1.1 riastrad default:
5702 1.1 riastrad return 1;
5703 1.1 riastrad }
5704 1.1 riastrad }
5705 1.1 riastrad EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
5706 1.1 riastrad
5707 1.1 riastrad /**
5708 1.1 riastrad * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
5709 1.1 riastrad * @format: pixel format (DRM_FORMAT_*)
5710 1.1 riastrad *
5711 1.3 riastrad * Returns:
5712 1.1 riastrad * The vertical chroma subsampling factor for the
5713 1.1 riastrad * specified pixel format.
5714 1.1 riastrad */
5715 1.1 riastrad int drm_format_vert_chroma_subsampling(uint32_t format)
5716 1.1 riastrad {
5717 1.1 riastrad switch (format) {
5718 1.1 riastrad case DRM_FORMAT_YUV410:
5719 1.1 riastrad case DRM_FORMAT_YVU410:
5720 1.1 riastrad return 4;
5721 1.1 riastrad case DRM_FORMAT_YUV420:
5722 1.1 riastrad case DRM_FORMAT_YVU420:
5723 1.1 riastrad case DRM_FORMAT_NV12:
5724 1.1 riastrad case DRM_FORMAT_NV21:
5725 1.1 riastrad return 2;
5726 1.1 riastrad default:
5727 1.1 riastrad return 1;
5728 1.1 riastrad }
5729 1.1 riastrad }
5730 1.1 riastrad EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
5731 1.3 riastrad
5732 1.3 riastrad /**
5733 1.5 riastrad * drm_rotation_simplify() - Try to simplify the rotation
5734 1.5 riastrad * @rotation: Rotation to be simplified
5735 1.5 riastrad * @supported_rotations: Supported rotations
5736 1.5 riastrad *
5737 1.5 riastrad * Attempt to simplify the rotation to a form that is supported.
5738 1.5 riastrad * Eg. if the hardware supports everything except DRM_REFLECT_X
5739 1.5 riastrad * one could call this function like this:
5740 1.5 riastrad *
5741 1.5 riastrad * drm_rotation_simplify(rotation, BIT(DRM_ROTATE_0) |
5742 1.5 riastrad * BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_180) |
5743 1.5 riastrad * BIT(DRM_ROTATE_270) | BIT(DRM_REFLECT_Y));
5744 1.5 riastrad *
5745 1.5 riastrad * to eliminate the DRM_ROTATE_X flag. Depending on what kind of
5746 1.5 riastrad * transforms the hardware supports, this function may not
5747 1.5 riastrad * be able to produce a supported transform, so the caller should
5748 1.5 riastrad * check the result afterwards.
5749 1.5 riastrad */
5750 1.5 riastrad unsigned int drm_rotation_simplify(unsigned int rotation,
5751 1.5 riastrad unsigned int supported_rotations)
5752 1.5 riastrad {
5753 1.5 riastrad if (rotation & ~supported_rotations) {
5754 1.5 riastrad rotation ^= BIT(DRM_REFLECT_X) | BIT(DRM_REFLECT_Y);
5755 1.5 riastrad rotation = (rotation & DRM_REFLECT_MASK) |
5756 1.5 riastrad BIT((ffs(rotation & DRM_ROTATE_MASK) + 1) % 4);
5757 1.5 riastrad }
5758 1.5 riastrad
5759 1.5 riastrad return rotation;
5760 1.5 riastrad }
5761 1.5 riastrad EXPORT_SYMBOL(drm_rotation_simplify);
5762 1.5 riastrad
5763 1.5 riastrad /**
5764 1.3 riastrad * drm_mode_config_init - initialize DRM mode_configuration structure
5765 1.3 riastrad * @dev: DRM device
5766 1.3 riastrad *
5767 1.3 riastrad * Initialize @dev's mode_config structure, used for tracking the graphics
5768 1.3 riastrad * configuration of @dev.
5769 1.3 riastrad *
5770 1.3 riastrad * Since this initializes the modeset locks, no locking is possible. Which is no
5771 1.3 riastrad * problem, since this should happen single threaded at init time. It is the
5772 1.3 riastrad * driver's problem to ensure this guarantee.
5773 1.3 riastrad *
5774 1.3 riastrad */
5775 1.3 riastrad void drm_mode_config_init(struct drm_device *dev)
5776 1.3 riastrad {
5777 1.3 riastrad mutex_init(&dev->mode_config.mutex);
5778 1.5 riastrad drm_modeset_lock_init(&dev->mode_config.connection_mutex);
5779 1.3 riastrad mutex_init(&dev->mode_config.idr_mutex);
5780 1.3 riastrad mutex_init(&dev->mode_config.fb_lock);
5781 1.5 riastrad mutex_init(&dev->mode_config.blob_lock);
5782 1.3 riastrad INIT_LIST_HEAD(&dev->mode_config.fb_list);
5783 1.3 riastrad INIT_LIST_HEAD(&dev->mode_config.crtc_list);
5784 1.3 riastrad INIT_LIST_HEAD(&dev->mode_config.connector_list);
5785 1.3 riastrad INIT_LIST_HEAD(&dev->mode_config.encoder_list);
5786 1.3 riastrad INIT_LIST_HEAD(&dev->mode_config.property_list);
5787 1.3 riastrad INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
5788 1.3 riastrad INIT_LIST_HEAD(&dev->mode_config.plane_list);
5789 1.3 riastrad idr_init(&dev->mode_config.crtc_idr);
5790 1.5 riastrad idr_init(&dev->mode_config.tile_idr);
5791 1.3 riastrad
5792 1.3 riastrad drm_modeset_lock_all(dev);
5793 1.5 riastrad drm_mode_create_standard_properties(dev);
5794 1.3 riastrad drm_modeset_unlock_all(dev);
5795 1.3 riastrad
5796 1.3 riastrad /* Just to be sure */
5797 1.3 riastrad dev->mode_config.num_fb = 0;
5798 1.3 riastrad dev->mode_config.num_connector = 0;
5799 1.3 riastrad dev->mode_config.num_crtc = 0;
5800 1.3 riastrad dev->mode_config.num_encoder = 0;
5801 1.3 riastrad dev->mode_config.num_overlay_plane = 0;
5802 1.3 riastrad dev->mode_config.num_total_plane = 0;
5803 1.3 riastrad }
5804 1.3 riastrad EXPORT_SYMBOL(drm_mode_config_init);
5805 1.3 riastrad
5806 1.3 riastrad /**
5807 1.3 riastrad * drm_mode_config_cleanup - free up DRM mode_config info
5808 1.3 riastrad * @dev: DRM device
5809 1.3 riastrad *
5810 1.3 riastrad * Free up all the connectors and CRTCs associated with this DRM device, then
5811 1.3 riastrad * free up the framebuffers and associated buffer objects.
5812 1.3 riastrad *
5813 1.3 riastrad * Note that since this /should/ happen single-threaded at driver/device
5814 1.3 riastrad * teardown time, no locking is required. It's the driver's job to ensure that
5815 1.3 riastrad * this guarantee actually holds true.
5816 1.3 riastrad *
5817 1.3 riastrad * FIXME: cleanup any dangling user buffer objects too
5818 1.3 riastrad */
5819 1.3 riastrad void drm_mode_config_cleanup(struct drm_device *dev)
5820 1.3 riastrad {
5821 1.3 riastrad struct drm_connector *connector, *ot;
5822 1.3 riastrad struct drm_crtc *crtc, *ct;
5823 1.3 riastrad struct drm_encoder *encoder, *enct;
5824 1.3 riastrad struct drm_framebuffer *fb, *fbt;
5825 1.3 riastrad struct drm_property *property, *pt;
5826 1.3 riastrad struct drm_property_blob *blob, *bt;
5827 1.3 riastrad struct drm_plane *plane, *plt;
5828 1.3 riastrad
5829 1.3 riastrad list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
5830 1.3 riastrad head) {
5831 1.3 riastrad encoder->funcs->destroy(encoder);
5832 1.3 riastrad }
5833 1.3 riastrad
5834 1.3 riastrad list_for_each_entry_safe(connector, ot,
5835 1.3 riastrad &dev->mode_config.connector_list, head) {
5836 1.3 riastrad connector->funcs->destroy(connector);
5837 1.3 riastrad }
5838 1.3 riastrad
5839 1.3 riastrad list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
5840 1.3 riastrad head) {
5841 1.3 riastrad drm_property_destroy(dev, property);
5842 1.3 riastrad }
5843 1.3 riastrad
5844 1.3 riastrad list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
5845 1.5 riastrad head_global) {
5846 1.5 riastrad drm_property_unreference_blob(blob);
5847 1.3 riastrad }
5848 1.3 riastrad
5849 1.3 riastrad /*
5850 1.3 riastrad * Single-threaded teardown context, so it's not required to grab the
5851 1.3 riastrad * fb_lock to protect against concurrent fb_list access. Contrary, it
5852 1.3 riastrad * would actually deadlock with the drm_framebuffer_cleanup function.
5853 1.3 riastrad *
5854 1.3 riastrad * Also, if there are any framebuffers left, that's a driver leak now,
5855 1.3 riastrad * so politely WARN about this.
5856 1.3 riastrad */
5857 1.3 riastrad WARN_ON(!list_empty(&dev->mode_config.fb_list));
5858 1.3 riastrad list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
5859 1.5 riastrad drm_framebuffer_free(&fb->refcount);
5860 1.3 riastrad }
5861 1.3 riastrad
5862 1.3 riastrad list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
5863 1.3 riastrad head) {
5864 1.3 riastrad plane->funcs->destroy(plane);
5865 1.3 riastrad }
5866 1.3 riastrad
5867 1.3 riastrad list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
5868 1.3 riastrad crtc->funcs->destroy(crtc);
5869 1.3 riastrad }
5870 1.3 riastrad
5871 1.5 riastrad idr_destroy(&dev->mode_config.tile_idr);
5872 1.3 riastrad idr_destroy(&dev->mode_config.crtc_idr);
5873 1.13 riastrad mutex_destroy(&dev->mode_config.blob_lock);
5874 1.13 riastrad mutex_destroy(&dev->mode_config.fb_lock);
5875 1.13 riastrad mutex_destroy(&dev->mode_config.idr_mutex);
5876 1.13 riastrad mutex_destroy(&dev->mode_config.mutex);
5877 1.5 riastrad drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
5878 1.3 riastrad }
5879 1.3 riastrad EXPORT_SYMBOL(drm_mode_config_cleanup);
5880 1.5 riastrad
5881 1.5 riastrad struct drm_property *drm_mode_create_rotation_property(struct drm_device *dev,
5882 1.5 riastrad unsigned int supported_rotations)
5883 1.5 riastrad {
5884 1.5 riastrad static const struct drm_prop_enum_list props[] = {
5885 1.5 riastrad { DRM_ROTATE_0, "rotate-0" },
5886 1.5 riastrad { DRM_ROTATE_90, "rotate-90" },
5887 1.5 riastrad { DRM_ROTATE_180, "rotate-180" },
5888 1.5 riastrad { DRM_ROTATE_270, "rotate-270" },
5889 1.5 riastrad { DRM_REFLECT_X, "reflect-x" },
5890 1.5 riastrad { DRM_REFLECT_Y, "reflect-y" },
5891 1.5 riastrad };
5892 1.5 riastrad
5893 1.5 riastrad return drm_property_create_bitmask(dev, 0, "rotation",
5894 1.5 riastrad props, ARRAY_SIZE(props),
5895 1.5 riastrad supported_rotations);
5896 1.5 riastrad }
5897 1.5 riastrad EXPORT_SYMBOL(drm_mode_create_rotation_property);
5898 1.5 riastrad
5899 1.5 riastrad /**
5900 1.5 riastrad * DOC: Tile group
5901 1.5 riastrad *
5902 1.5 riastrad * Tile groups are used to represent tiled monitors with a unique
5903 1.5 riastrad * integer identifier. Tiled monitors using DisplayID v1.3 have
5904 1.5 riastrad * a unique 8-byte handle, we store this in a tile group, so we
5905 1.5 riastrad * have a common identifier for all tiles in a monitor group.
5906 1.5 riastrad */
5907 1.5 riastrad static void drm_tile_group_free(struct kref *kref)
5908 1.5 riastrad {
5909 1.5 riastrad struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount);
5910 1.5 riastrad struct drm_device *dev = tg->dev;
5911 1.5 riastrad mutex_lock(&dev->mode_config.idr_mutex);
5912 1.5 riastrad idr_remove(&dev->mode_config.tile_idr, tg->id);
5913 1.5 riastrad mutex_unlock(&dev->mode_config.idr_mutex);
5914 1.5 riastrad kfree(tg);
5915 1.5 riastrad }
5916 1.5 riastrad
5917 1.5 riastrad /**
5918 1.5 riastrad * drm_mode_put_tile_group - drop a reference to a tile group.
5919 1.5 riastrad * @dev: DRM device
5920 1.5 riastrad * @tg: tile group to drop reference to.
5921 1.5 riastrad *
5922 1.5 riastrad * drop reference to tile group and free if 0.
5923 1.5 riastrad */
5924 1.5 riastrad void drm_mode_put_tile_group(struct drm_device *dev,
5925 1.5 riastrad struct drm_tile_group *tg)
5926 1.5 riastrad {
5927 1.5 riastrad kref_put(&tg->refcount, drm_tile_group_free);
5928 1.5 riastrad }
5929 1.5 riastrad
5930 1.5 riastrad /**
5931 1.5 riastrad * drm_mode_get_tile_group - get a reference to an existing tile group
5932 1.5 riastrad * @dev: DRM device
5933 1.5 riastrad * @topology: 8-bytes unique per monitor.
5934 1.5 riastrad *
5935 1.5 riastrad * Use the unique bytes to get a reference to an existing tile group.
5936 1.5 riastrad *
5937 1.5 riastrad * RETURNS:
5938 1.5 riastrad * tile group or NULL if not found.
5939 1.5 riastrad */
5940 1.5 riastrad struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
5941 1.5 riastrad char topology[8])
5942 1.5 riastrad {
5943 1.5 riastrad struct drm_tile_group *tg;
5944 1.5 riastrad int id;
5945 1.5 riastrad mutex_lock(&dev->mode_config.idr_mutex);
5946 1.5 riastrad idr_for_each_entry(&dev->mode_config.tile_idr, tg, id) {
5947 1.5 riastrad if (!memcmp(tg->group_data, topology, 8)) {
5948 1.5 riastrad if (!kref_get_unless_zero(&tg->refcount))
5949 1.5 riastrad tg = NULL;
5950 1.5 riastrad mutex_unlock(&dev->mode_config.idr_mutex);
5951 1.5 riastrad return tg;
5952 1.5 riastrad }
5953 1.5 riastrad }
5954 1.5 riastrad mutex_unlock(&dev->mode_config.idr_mutex);
5955 1.5 riastrad return NULL;
5956 1.5 riastrad }
5957 1.5 riastrad EXPORT_SYMBOL(drm_mode_get_tile_group);
5958 1.5 riastrad
5959 1.5 riastrad /**
5960 1.5 riastrad * drm_mode_create_tile_group - create a tile group from a displayid description
5961 1.5 riastrad * @dev: DRM device
5962 1.5 riastrad * @topology: 8-bytes unique per monitor.
5963 1.5 riastrad *
5964 1.5 riastrad * Create a tile group for the unique monitor, and get a unique
5965 1.5 riastrad * identifier for the tile group.
5966 1.5 riastrad *
5967 1.5 riastrad * RETURNS:
5968 1.5 riastrad * new tile group or error.
5969 1.5 riastrad */
5970 1.5 riastrad struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
5971 1.5 riastrad char topology[8])
5972 1.5 riastrad {
5973 1.5 riastrad struct drm_tile_group *tg;
5974 1.5 riastrad int ret;
5975 1.5 riastrad
5976 1.5 riastrad tg = kzalloc(sizeof(*tg), GFP_KERNEL);
5977 1.5 riastrad if (!tg)
5978 1.5 riastrad return ERR_PTR(-ENOMEM);
5979 1.5 riastrad
5980 1.5 riastrad kref_init(&tg->refcount);
5981 1.5 riastrad memcpy(tg->group_data, topology, 8);
5982 1.5 riastrad tg->dev = dev;
5983 1.5 riastrad
5984 1.9 riastrad idr_preload(GFP_KERNEL);
5985 1.5 riastrad mutex_lock(&dev->mode_config.idr_mutex);
5986 1.5 riastrad ret = idr_alloc(&dev->mode_config.tile_idr, tg, 1, 0, GFP_KERNEL);
5987 1.5 riastrad if (ret >= 0) {
5988 1.5 riastrad tg->id = ret;
5989 1.5 riastrad } else {
5990 1.5 riastrad kfree(tg);
5991 1.5 riastrad tg = ERR_PTR(ret);
5992 1.5 riastrad }
5993 1.5 riastrad
5994 1.5 riastrad mutex_unlock(&dev->mode_config.idr_mutex);
5995 1.9 riastrad idr_preload_end();
5996 1.5 riastrad return tg;
5997 1.5 riastrad }
5998 1.5 riastrad EXPORT_SYMBOL(drm_mode_create_tile_group);
5999