drm_crtc.c revision 1.1.1.2 1 1.1 riastrad /*
2 1.1 riastrad * Copyright (c) 2006-2008 Intel Corporation
3 1.1 riastrad * Copyright (c) 2007 Dave Airlie <airlied (at) linux.ie>
4 1.1 riastrad * Copyright (c) 2008 Red Hat Inc.
5 1.1 riastrad *
6 1.1 riastrad * DRM core CRTC related functions
7 1.1 riastrad *
8 1.1 riastrad * Permission to use, copy, modify, distribute, and sell this software and its
9 1.1 riastrad * documentation for any purpose is hereby granted without fee, provided that
10 1.1 riastrad * the above copyright notice appear in all copies and that both that copyright
11 1.1 riastrad * notice and this permission notice appear in supporting documentation, and
12 1.1 riastrad * that the name of the copyright holders not be used in advertising or
13 1.1 riastrad * publicity pertaining to distribution of the software without specific,
14 1.1 riastrad * written prior permission. The copyright holders make no representations
15 1.1 riastrad * about the suitability of this software for any purpose. It is provided "as
16 1.1 riastrad * is" without express or implied warranty.
17 1.1 riastrad *
18 1.1 riastrad * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19 1.1 riastrad * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
20 1.1 riastrad * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
21 1.1 riastrad * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
22 1.1 riastrad * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
23 1.1 riastrad * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24 1.1 riastrad * OF THIS SOFTWARE.
25 1.1 riastrad *
26 1.1 riastrad * Authors:
27 1.1 riastrad * Keith Packard
28 1.1 riastrad * Eric Anholt <eric (at) anholt.net>
29 1.1 riastrad * Dave Airlie <airlied (at) linux.ie>
30 1.1 riastrad * Jesse Barnes <jesse.barnes (at) intel.com>
31 1.1 riastrad */
32 1.1.1.2 riastrad #include <linux/ctype.h>
33 1.1 riastrad #include <linux/list.h>
34 1.1 riastrad #include <linux/slab.h>
35 1.1 riastrad #include <linux/export.h>
36 1.1 riastrad #include <drm/drmP.h>
37 1.1 riastrad #include <drm/drm_crtc.h>
38 1.1 riastrad #include <drm/drm_edid.h>
39 1.1 riastrad #include <drm/drm_fourcc.h>
40 1.1 riastrad
41 1.1.1.2 riastrad #include "drm_crtc_internal.h"
42 1.1.1.2 riastrad
43 1.1.1.2 riastrad /**
44 1.1.1.2 riastrad * drm_modeset_lock_all - take all modeset locks
45 1.1.1.2 riastrad * @dev: drm device
46 1.1.1.2 riastrad *
47 1.1.1.2 riastrad * This function takes all modeset locks, suitable where a more fine-grained
48 1.1.1.2 riastrad * scheme isn't (yet) implemented. Locks must be dropped with
49 1.1.1.2 riastrad * drm_modeset_unlock_all.
50 1.1.1.2 riastrad */
51 1.1.1.2 riastrad void drm_modeset_lock_all(struct drm_device *dev)
52 1.1.1.2 riastrad {
53 1.1.1.2 riastrad struct drm_crtc *crtc;
54 1.1.1.2 riastrad
55 1.1.1.2 riastrad mutex_lock(&dev->mode_config.mutex);
56 1.1.1.2 riastrad
57 1.1.1.2 riastrad list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
58 1.1.1.2 riastrad mutex_lock_nest_lock(&crtc->mutex, &dev->mode_config.mutex);
59 1.1.1.2 riastrad }
60 1.1.1.2 riastrad EXPORT_SYMBOL(drm_modeset_lock_all);
61 1.1.1.2 riastrad
62 1.1.1.2 riastrad /**
63 1.1.1.2 riastrad * drm_modeset_unlock_all - drop all modeset locks
64 1.1.1.2 riastrad * @dev: device
65 1.1.1.2 riastrad *
66 1.1.1.2 riastrad * This function drop all modeset locks taken by drm_modeset_lock_all.
67 1.1.1.2 riastrad */
68 1.1.1.2 riastrad void drm_modeset_unlock_all(struct drm_device *dev)
69 1.1.1.2 riastrad {
70 1.1.1.2 riastrad struct drm_crtc *crtc;
71 1.1.1.2 riastrad
72 1.1.1.2 riastrad list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
73 1.1.1.2 riastrad mutex_unlock(&crtc->mutex);
74 1.1.1.2 riastrad
75 1.1.1.2 riastrad mutex_unlock(&dev->mode_config.mutex);
76 1.1.1.2 riastrad }
77 1.1.1.2 riastrad EXPORT_SYMBOL(drm_modeset_unlock_all);
78 1.1.1.2 riastrad
79 1.1.1.2 riastrad /**
80 1.1.1.2 riastrad * drm_warn_on_modeset_not_all_locked - check that all modeset locks are locked
81 1.1.1.2 riastrad * @dev: device
82 1.1.1.2 riastrad *
83 1.1.1.2 riastrad * Useful as a debug assert.
84 1.1.1.2 riastrad */
85 1.1.1.2 riastrad void drm_warn_on_modeset_not_all_locked(struct drm_device *dev)
86 1.1.1.2 riastrad {
87 1.1.1.2 riastrad struct drm_crtc *crtc;
88 1.1.1.2 riastrad
89 1.1.1.2 riastrad /* Locking is currently fubar in the panic handler. */
90 1.1.1.2 riastrad if (oops_in_progress)
91 1.1.1.2 riastrad return;
92 1.1.1.2 riastrad
93 1.1.1.2 riastrad list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
94 1.1.1.2 riastrad WARN_ON(!mutex_is_locked(&crtc->mutex));
95 1.1.1.2 riastrad
96 1.1.1.2 riastrad WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
97 1.1.1.2 riastrad }
98 1.1.1.2 riastrad EXPORT_SYMBOL(drm_warn_on_modeset_not_all_locked);
99 1.1.1.2 riastrad
100 1.1 riastrad /* Avoid boilerplate. I'm tired of typing. */
101 1.1 riastrad #define DRM_ENUM_NAME_FN(fnname, list) \
102 1.1.1.2 riastrad const char *fnname(int val) \
103 1.1 riastrad { \
104 1.1 riastrad int i; \
105 1.1 riastrad for (i = 0; i < ARRAY_SIZE(list); i++) { \
106 1.1 riastrad if (list[i].type == val) \
107 1.1 riastrad return list[i].name; \
108 1.1 riastrad } \
109 1.1 riastrad return "(unknown)"; \
110 1.1 riastrad }
111 1.1 riastrad
112 1.1 riastrad /*
113 1.1 riastrad * Global properties
114 1.1 riastrad */
115 1.1.1.2 riastrad static const struct drm_prop_enum_list drm_dpms_enum_list[] =
116 1.1 riastrad { { DRM_MODE_DPMS_ON, "On" },
117 1.1 riastrad { DRM_MODE_DPMS_STANDBY, "Standby" },
118 1.1 riastrad { DRM_MODE_DPMS_SUSPEND, "Suspend" },
119 1.1 riastrad { DRM_MODE_DPMS_OFF, "Off" }
120 1.1 riastrad };
121 1.1 riastrad
122 1.1 riastrad DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
123 1.1 riastrad
124 1.1.1.2 riastrad static const struct drm_prop_enum_list drm_plane_type_enum_list[] =
125 1.1.1.2 riastrad {
126 1.1.1.2 riastrad { DRM_PLANE_TYPE_OVERLAY, "Overlay" },
127 1.1.1.2 riastrad { DRM_PLANE_TYPE_PRIMARY, "Primary" },
128 1.1.1.2 riastrad { DRM_PLANE_TYPE_CURSOR, "Cursor" },
129 1.1.1.2 riastrad };
130 1.1.1.2 riastrad
131 1.1 riastrad /*
132 1.1 riastrad * Optional properties
133 1.1 riastrad */
134 1.1.1.2 riastrad static const struct drm_prop_enum_list drm_scaling_mode_enum_list[] =
135 1.1 riastrad {
136 1.1 riastrad { DRM_MODE_SCALE_NONE, "None" },
137 1.1 riastrad { DRM_MODE_SCALE_FULLSCREEN, "Full" },
138 1.1 riastrad { DRM_MODE_SCALE_CENTER, "Center" },
139 1.1 riastrad { DRM_MODE_SCALE_ASPECT, "Full aspect" },
140 1.1 riastrad };
141 1.1 riastrad
142 1.1 riastrad /*
143 1.1 riastrad * Non-global properties, but "required" for certain connectors.
144 1.1 riastrad */
145 1.1.1.2 riastrad static const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] =
146 1.1 riastrad {
147 1.1 riastrad { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
148 1.1 riastrad { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
149 1.1 riastrad { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
150 1.1 riastrad };
151 1.1 riastrad
152 1.1 riastrad DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
153 1.1 riastrad
154 1.1.1.2 riastrad static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] =
155 1.1 riastrad {
156 1.1 riastrad { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
157 1.1 riastrad { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
158 1.1 riastrad { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
159 1.1 riastrad };
160 1.1 riastrad
161 1.1 riastrad DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
162 1.1 riastrad drm_dvi_i_subconnector_enum_list)
163 1.1 riastrad
164 1.1.1.2 riastrad static const struct drm_prop_enum_list drm_tv_select_enum_list[] =
165 1.1 riastrad {
166 1.1 riastrad { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
167 1.1 riastrad { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
168 1.1 riastrad { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
169 1.1 riastrad { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
170 1.1 riastrad { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
171 1.1 riastrad };
172 1.1 riastrad
173 1.1 riastrad DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
174 1.1 riastrad
175 1.1.1.2 riastrad static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] =
176 1.1 riastrad {
177 1.1 riastrad { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
178 1.1 riastrad { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
179 1.1 riastrad { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
180 1.1 riastrad { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
181 1.1 riastrad { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
182 1.1 riastrad };
183 1.1 riastrad
184 1.1 riastrad DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
185 1.1 riastrad drm_tv_subconnector_enum_list)
186 1.1 riastrad
187 1.1.1.2 riastrad static const struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
188 1.1 riastrad { DRM_MODE_DIRTY_OFF, "Off" },
189 1.1 riastrad { DRM_MODE_DIRTY_ON, "On" },
190 1.1 riastrad { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
191 1.1 riastrad };
192 1.1 riastrad
193 1.1 riastrad struct drm_conn_prop_enum_list {
194 1.1 riastrad int type;
195 1.1.1.2 riastrad const char *name;
196 1.1.1.2 riastrad struct ida ida;
197 1.1 riastrad };
198 1.1 riastrad
199 1.1 riastrad /*
200 1.1 riastrad * Connector and encoder types.
201 1.1 riastrad */
202 1.1 riastrad static struct drm_conn_prop_enum_list drm_connector_enum_list[] =
203 1.1.1.2 riastrad { { DRM_MODE_CONNECTOR_Unknown, "Unknown" },
204 1.1.1.2 riastrad { DRM_MODE_CONNECTOR_VGA, "VGA" },
205 1.1.1.2 riastrad { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
206 1.1.1.2 riastrad { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
207 1.1.1.2 riastrad { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
208 1.1.1.2 riastrad { DRM_MODE_CONNECTOR_Composite, "Composite" },
209 1.1.1.2 riastrad { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO" },
210 1.1.1.2 riastrad { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
211 1.1.1.2 riastrad { DRM_MODE_CONNECTOR_Component, "Component" },
212 1.1.1.2 riastrad { DRM_MODE_CONNECTOR_9PinDIN, "DIN" },
213 1.1.1.2 riastrad { DRM_MODE_CONNECTOR_DisplayPort, "DP" },
214 1.1.1.2 riastrad { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
215 1.1.1.2 riastrad { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
216 1.1.1.2 riastrad { DRM_MODE_CONNECTOR_TV, "TV" },
217 1.1.1.2 riastrad { DRM_MODE_CONNECTOR_eDP, "eDP" },
218 1.1.1.2 riastrad { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual" },
219 1.1.1.2 riastrad { DRM_MODE_CONNECTOR_DSI, "DSI" },
220 1.1 riastrad };
221 1.1 riastrad
222 1.1.1.2 riastrad static const struct drm_prop_enum_list drm_encoder_enum_list[] =
223 1.1 riastrad { { DRM_MODE_ENCODER_NONE, "None" },
224 1.1 riastrad { DRM_MODE_ENCODER_DAC, "DAC" },
225 1.1 riastrad { DRM_MODE_ENCODER_TMDS, "TMDS" },
226 1.1 riastrad { DRM_MODE_ENCODER_LVDS, "LVDS" },
227 1.1 riastrad { DRM_MODE_ENCODER_TVDAC, "TV" },
228 1.1 riastrad { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
229 1.1.1.2 riastrad { DRM_MODE_ENCODER_DSI, "DSI" },
230 1.1.1.2 riastrad };
231 1.1.1.2 riastrad
232 1.1.1.2 riastrad static const struct drm_prop_enum_list drm_subpixel_enum_list[] =
233 1.1.1.2 riastrad {
234 1.1.1.2 riastrad { SubPixelUnknown, "Unknown" },
235 1.1.1.2 riastrad { SubPixelHorizontalRGB, "Horizontal RGB" },
236 1.1.1.2 riastrad { SubPixelHorizontalBGR, "Horizontal BGR" },
237 1.1.1.2 riastrad { SubPixelVerticalRGB, "Vertical RGB" },
238 1.1.1.2 riastrad { SubPixelVerticalBGR, "Vertical BGR" },
239 1.1.1.2 riastrad { SubPixelNone, "None" },
240 1.1 riastrad };
241 1.1 riastrad
242 1.1.1.2 riastrad void drm_connector_ida_init(void)
243 1.1.1.2 riastrad {
244 1.1.1.2 riastrad int i;
245 1.1.1.2 riastrad
246 1.1.1.2 riastrad for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
247 1.1.1.2 riastrad ida_init(&drm_connector_enum_list[i].ida);
248 1.1.1.2 riastrad }
249 1.1.1.2 riastrad
250 1.1.1.2 riastrad void drm_connector_ida_destroy(void)
251 1.1.1.2 riastrad {
252 1.1.1.2 riastrad int i;
253 1.1.1.2 riastrad
254 1.1.1.2 riastrad for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
255 1.1.1.2 riastrad ida_destroy(&drm_connector_enum_list[i].ida);
256 1.1.1.2 riastrad }
257 1.1.1.2 riastrad
258 1.1.1.2 riastrad /**
259 1.1.1.2 riastrad * drm_get_encoder_name - return a string for encoder
260 1.1.1.2 riastrad * @encoder: encoder to compute name of
261 1.1.1.2 riastrad *
262 1.1.1.2 riastrad * Note that the buffer used by this function is globally shared and owned by
263 1.1.1.2 riastrad * the function itself.
264 1.1.1.2 riastrad *
265 1.1.1.2 riastrad * FIXME: This isn't really multithreading safe.
266 1.1.1.2 riastrad */
267 1.1.1.2 riastrad const char *drm_get_encoder_name(const struct drm_encoder *encoder)
268 1.1 riastrad {
269 1.1 riastrad static char buf[32];
270 1.1 riastrad
271 1.1 riastrad snprintf(buf, 32, "%s-%d",
272 1.1 riastrad drm_encoder_enum_list[encoder->encoder_type].name,
273 1.1 riastrad encoder->base.id);
274 1.1 riastrad return buf;
275 1.1 riastrad }
276 1.1 riastrad EXPORT_SYMBOL(drm_get_encoder_name);
277 1.1 riastrad
278 1.1.1.2 riastrad /**
279 1.1.1.2 riastrad * drm_get_connector_name - return a string for connector
280 1.1.1.2 riastrad * @connector: connector to compute name of
281 1.1.1.2 riastrad *
282 1.1.1.2 riastrad * Note that the buffer used by this function is globally shared and owned by
283 1.1.1.2 riastrad * the function itself.
284 1.1.1.2 riastrad *
285 1.1.1.2 riastrad * FIXME: This isn't really multithreading safe.
286 1.1.1.2 riastrad */
287 1.1.1.2 riastrad const char *drm_get_connector_name(const struct drm_connector *connector)
288 1.1 riastrad {
289 1.1 riastrad static char buf[32];
290 1.1 riastrad
291 1.1 riastrad snprintf(buf, 32, "%s-%d",
292 1.1 riastrad drm_connector_enum_list[connector->connector_type].name,
293 1.1 riastrad connector->connector_type_id);
294 1.1 riastrad return buf;
295 1.1 riastrad }
296 1.1 riastrad EXPORT_SYMBOL(drm_get_connector_name);
297 1.1 riastrad
298 1.1.1.2 riastrad /**
299 1.1.1.2 riastrad * drm_get_connector_status_name - return a string for connector status
300 1.1.1.2 riastrad * @status: connector status to compute name of
301 1.1.1.2 riastrad *
302 1.1.1.2 riastrad * In contrast to the other drm_get_*_name functions this one here returns a
303 1.1.1.2 riastrad * const pointer and hence is threadsafe.
304 1.1.1.2 riastrad */
305 1.1.1.2 riastrad const char *drm_get_connector_status_name(enum drm_connector_status status)
306 1.1 riastrad {
307 1.1 riastrad if (status == connector_status_connected)
308 1.1 riastrad return "connected";
309 1.1 riastrad else if (status == connector_status_disconnected)
310 1.1 riastrad return "disconnected";
311 1.1 riastrad else
312 1.1 riastrad return "unknown";
313 1.1 riastrad }
314 1.1.1.2 riastrad EXPORT_SYMBOL(drm_get_connector_status_name);
315 1.1 riastrad
316 1.1 riastrad /**
317 1.1.1.2 riastrad * drm_get_subpixel_order_name - return a string for a given subpixel enum
318 1.1.1.2 riastrad * @order: enum of subpixel_order
319 1.1.1.2 riastrad *
320 1.1.1.2 riastrad * Note you could abuse this and return something out of bounds, but that
321 1.1.1.2 riastrad * would be a caller error. No unscrubbed user data should make it here.
322 1.1.1.2 riastrad */
323 1.1.1.2 riastrad const char *drm_get_subpixel_order_name(enum subpixel_order order)
324 1.1.1.2 riastrad {
325 1.1.1.2 riastrad return drm_subpixel_enum_list[order].name;
326 1.1.1.2 riastrad }
327 1.1.1.2 riastrad EXPORT_SYMBOL(drm_get_subpixel_order_name);
328 1.1.1.2 riastrad
329 1.1.1.2 riastrad static char printable_char(int c)
330 1.1.1.2 riastrad {
331 1.1.1.2 riastrad return isascii(c) && isprint(c) ? c : '?';
332 1.1.1.2 riastrad }
333 1.1.1.2 riastrad
334 1.1.1.2 riastrad /**
335 1.1.1.2 riastrad * drm_get_format_name - return a string for drm fourcc format
336 1.1.1.2 riastrad * @format: format to compute name of
337 1.1.1.2 riastrad *
338 1.1.1.2 riastrad * Note that the buffer used by this function is globally shared and owned by
339 1.1.1.2 riastrad * the function itself.
340 1.1 riastrad *
341 1.1.1.2 riastrad * FIXME: This isn't really multithreading safe.
342 1.1.1.2 riastrad */
343 1.1.1.2 riastrad const char *drm_get_format_name(uint32_t format)
344 1.1.1.2 riastrad {
345 1.1.1.2 riastrad static char buf[32];
346 1.1.1.2 riastrad
347 1.1.1.2 riastrad snprintf(buf, sizeof(buf),
348 1.1.1.2 riastrad "%c%c%c%c %s-endian (0x%08x)",
349 1.1.1.2 riastrad printable_char(format & 0xff),
350 1.1.1.2 riastrad printable_char((format >> 8) & 0xff),
351 1.1.1.2 riastrad printable_char((format >> 16) & 0xff),
352 1.1.1.2 riastrad printable_char((format >> 24) & 0x7f),
353 1.1.1.2 riastrad format & DRM_FORMAT_BIG_ENDIAN ? "big" : "little",
354 1.1.1.2 riastrad format);
355 1.1.1.2 riastrad
356 1.1.1.2 riastrad return buf;
357 1.1.1.2 riastrad }
358 1.1.1.2 riastrad EXPORT_SYMBOL(drm_get_format_name);
359 1.1.1.2 riastrad
360 1.1.1.2 riastrad /**
361 1.1.1.2 riastrad * drm_mode_object_get - allocate a new modeset identifier
362 1.1.1.2 riastrad * @dev: DRM device
363 1.1.1.2 riastrad * @obj: object pointer, used to generate unique ID
364 1.1.1.2 riastrad * @obj_type: object type
365 1.1 riastrad *
366 1.1 riastrad * Create a unique identifier based on @ptr in @dev's identifier space. Used
367 1.1.1.2 riastrad * for tracking modes, CRTCs and connectors. Note that despite the _get postfix
368 1.1.1.2 riastrad * modeset identifiers are _not_ reference counted. Hence don't use this for
369 1.1.1.2 riastrad * reference counted modeset objects like framebuffers.
370 1.1 riastrad *
371 1.1.1.2 riastrad * Returns:
372 1.1 riastrad * New unique (relative to other objects in @dev) integer identifier for the
373 1.1 riastrad * object.
374 1.1 riastrad */
375 1.1.1.2 riastrad int drm_mode_object_get(struct drm_device *dev,
376 1.1.1.2 riastrad struct drm_mode_object *obj, uint32_t obj_type)
377 1.1 riastrad {
378 1.1 riastrad int ret;
379 1.1 riastrad
380 1.1 riastrad mutex_lock(&dev->mode_config.idr_mutex);
381 1.1.1.2 riastrad ret = idr_alloc(&dev->mode_config.crtc_idr, obj, 1, 0, GFP_KERNEL);
382 1.1.1.2 riastrad if (ret >= 0) {
383 1.1.1.2 riastrad /*
384 1.1.1.2 riastrad * Set up the object linking under the protection of the idr
385 1.1.1.2 riastrad * lock so that other users can't see inconsistent state.
386 1.1.1.2 riastrad */
387 1.1.1.2 riastrad obj->id = ret;
388 1.1.1.2 riastrad obj->type = obj_type;
389 1.1.1.2 riastrad }
390 1.1 riastrad mutex_unlock(&dev->mode_config.idr_mutex);
391 1.1 riastrad
392 1.1.1.2 riastrad return ret < 0 ? ret : 0;
393 1.1 riastrad }
394 1.1 riastrad
395 1.1 riastrad /**
396 1.1.1.2 riastrad * drm_mode_object_put - free a modeset identifer
397 1.1 riastrad * @dev: DRM device
398 1.1.1.2 riastrad * @object: object to free
399 1.1 riastrad *
400 1.1.1.2 riastrad * Free @id from @dev's unique identifier pool. Note that despite the _get
401 1.1.1.2 riastrad * postfix modeset identifiers are _not_ reference counted. Hence don't use this
402 1.1.1.2 riastrad * for reference counted modeset objects like framebuffers.
403 1.1 riastrad */
404 1.1.1.2 riastrad void drm_mode_object_put(struct drm_device *dev,
405 1.1.1.2 riastrad struct drm_mode_object *object)
406 1.1 riastrad {
407 1.1 riastrad mutex_lock(&dev->mode_config.idr_mutex);
408 1.1 riastrad idr_remove(&dev->mode_config.crtc_idr, object->id);
409 1.1 riastrad mutex_unlock(&dev->mode_config.idr_mutex);
410 1.1 riastrad }
411 1.1 riastrad
412 1.1.1.2 riastrad /**
413 1.1.1.2 riastrad * drm_mode_object_find - look up a drm object with static lifetime
414 1.1.1.2 riastrad * @dev: drm device
415 1.1.1.2 riastrad * @id: id of the mode object
416 1.1.1.2 riastrad * @type: type of the mode object
417 1.1.1.2 riastrad *
418 1.1.1.2 riastrad * Note that framebuffers cannot be looked up with this functions - since those
419 1.1.1.2 riastrad * are reference counted, they need special treatment.
420 1.1.1.2 riastrad */
421 1.1 riastrad struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
422 1.1 riastrad uint32_t id, uint32_t type)
423 1.1 riastrad {
424 1.1 riastrad struct drm_mode_object *obj = NULL;
425 1.1 riastrad
426 1.1.1.2 riastrad /* Framebuffers are reference counted and need their own lookup
427 1.1.1.2 riastrad * function.*/
428 1.1.1.2 riastrad WARN_ON(type == DRM_MODE_OBJECT_FB);
429 1.1.1.2 riastrad
430 1.1 riastrad mutex_lock(&dev->mode_config.idr_mutex);
431 1.1 riastrad obj = idr_find(&dev->mode_config.crtc_idr, id);
432 1.1 riastrad if (!obj || (obj->type != type) || (obj->id != id))
433 1.1 riastrad obj = NULL;
434 1.1 riastrad mutex_unlock(&dev->mode_config.idr_mutex);
435 1.1 riastrad
436 1.1 riastrad return obj;
437 1.1 riastrad }
438 1.1 riastrad EXPORT_SYMBOL(drm_mode_object_find);
439 1.1 riastrad
440 1.1 riastrad /**
441 1.1 riastrad * drm_framebuffer_init - initialize a framebuffer
442 1.1 riastrad * @dev: DRM device
443 1.1.1.2 riastrad * @fb: framebuffer to be initialized
444 1.1.1.2 riastrad * @funcs: ... with these functions
445 1.1 riastrad *
446 1.1 riastrad * Allocates an ID for the framebuffer's parent mode object, sets its mode
447 1.1 riastrad * functions & device file and adds it to the master fd list.
448 1.1 riastrad *
449 1.1.1.2 riastrad * IMPORTANT:
450 1.1.1.2 riastrad * This functions publishes the fb and makes it available for concurrent access
451 1.1.1.2 riastrad * by other users. Which means by this point the fb _must_ be fully set up -
452 1.1.1.2 riastrad * since all the fb attributes are invariant over its lifetime, no further
453 1.1.1.2 riastrad * locking but only correct reference counting is required.
454 1.1.1.2 riastrad *
455 1.1.1.2 riastrad * Returns:
456 1.1 riastrad * Zero on success, error code on failure.
457 1.1 riastrad */
458 1.1 riastrad int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
459 1.1 riastrad const struct drm_framebuffer_funcs *funcs)
460 1.1 riastrad {
461 1.1 riastrad int ret;
462 1.1 riastrad
463 1.1.1.2 riastrad mutex_lock(&dev->mode_config.fb_lock);
464 1.1 riastrad kref_init(&fb->refcount);
465 1.1.1.2 riastrad INIT_LIST_HEAD(&fb->filp_head);
466 1.1.1.2 riastrad fb->dev = dev;
467 1.1.1.2 riastrad fb->funcs = funcs;
468 1.1 riastrad
469 1.1 riastrad ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
470 1.1 riastrad if (ret)
471 1.1.1.2 riastrad goto out;
472 1.1.1.2 riastrad
473 1.1.1.2 riastrad /* Grab the idr reference. */
474 1.1.1.2 riastrad drm_framebuffer_reference(fb);
475 1.1 riastrad
476 1.1 riastrad dev->mode_config.num_fb++;
477 1.1 riastrad list_add(&fb->head, &dev->mode_config.fb_list);
478 1.1.1.2 riastrad out:
479 1.1.1.2 riastrad mutex_unlock(&dev->mode_config.fb_lock);
480 1.1 riastrad
481 1.1 riastrad return 0;
482 1.1 riastrad }
483 1.1 riastrad EXPORT_SYMBOL(drm_framebuffer_init);
484 1.1 riastrad
485 1.1 riastrad static void drm_framebuffer_free(struct kref *kref)
486 1.1 riastrad {
487 1.1 riastrad struct drm_framebuffer *fb =
488 1.1 riastrad container_of(kref, struct drm_framebuffer, refcount);
489 1.1 riastrad fb->funcs->destroy(fb);
490 1.1 riastrad }
491 1.1 riastrad
492 1.1.1.2 riastrad static struct drm_framebuffer *__drm_framebuffer_lookup(struct drm_device *dev,
493 1.1.1.2 riastrad uint32_t id)
494 1.1.1.2 riastrad {
495 1.1.1.2 riastrad struct drm_mode_object *obj = NULL;
496 1.1.1.2 riastrad struct drm_framebuffer *fb;
497 1.1.1.2 riastrad
498 1.1.1.2 riastrad mutex_lock(&dev->mode_config.idr_mutex);
499 1.1.1.2 riastrad obj = idr_find(&dev->mode_config.crtc_idr, id);
500 1.1.1.2 riastrad if (!obj || (obj->type != DRM_MODE_OBJECT_FB) || (obj->id != id))
501 1.1.1.2 riastrad fb = NULL;
502 1.1.1.2 riastrad else
503 1.1.1.2 riastrad fb = obj_to_fb(obj);
504 1.1.1.2 riastrad mutex_unlock(&dev->mode_config.idr_mutex);
505 1.1.1.2 riastrad
506 1.1.1.2 riastrad return fb;
507 1.1.1.2 riastrad }
508 1.1.1.2 riastrad
509 1.1.1.2 riastrad /**
510 1.1.1.2 riastrad * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
511 1.1.1.2 riastrad * @dev: drm device
512 1.1.1.2 riastrad * @id: id of the fb object
513 1.1.1.2 riastrad *
514 1.1.1.2 riastrad * If successful, this grabs an additional reference to the framebuffer -
515 1.1.1.2 riastrad * callers need to make sure to eventually unreference the returned framebuffer
516 1.1.1.2 riastrad * again, using @drm_framebuffer_unreference.
517 1.1.1.2 riastrad */
518 1.1.1.2 riastrad struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
519 1.1.1.2 riastrad uint32_t id)
520 1.1.1.2 riastrad {
521 1.1.1.2 riastrad struct drm_framebuffer *fb;
522 1.1.1.2 riastrad
523 1.1.1.2 riastrad mutex_lock(&dev->mode_config.fb_lock);
524 1.1.1.2 riastrad fb = __drm_framebuffer_lookup(dev, id);
525 1.1.1.2 riastrad if (fb)
526 1.1.1.2 riastrad drm_framebuffer_reference(fb);
527 1.1.1.2 riastrad mutex_unlock(&dev->mode_config.fb_lock);
528 1.1.1.2 riastrad
529 1.1.1.2 riastrad return fb;
530 1.1.1.2 riastrad }
531 1.1.1.2 riastrad EXPORT_SYMBOL(drm_framebuffer_lookup);
532 1.1.1.2 riastrad
533 1.1 riastrad /**
534 1.1 riastrad * drm_framebuffer_unreference - unref a framebuffer
535 1.1.1.2 riastrad * @fb: framebuffer to unref
536 1.1 riastrad *
537 1.1.1.2 riastrad * This functions decrements the fb's refcount and frees it if it drops to zero.
538 1.1 riastrad */
539 1.1 riastrad void drm_framebuffer_unreference(struct drm_framebuffer *fb)
540 1.1 riastrad {
541 1.1 riastrad DRM_DEBUG("FB ID: %d\n", fb->base.id);
542 1.1 riastrad kref_put(&fb->refcount, drm_framebuffer_free);
543 1.1 riastrad }
544 1.1 riastrad EXPORT_SYMBOL(drm_framebuffer_unreference);
545 1.1 riastrad
546 1.1 riastrad /**
547 1.1 riastrad * drm_framebuffer_reference - incr the fb refcnt
548 1.1.1.2 riastrad * @fb: framebuffer
549 1.1.1.2 riastrad *
550 1.1.1.2 riastrad * This functions increments the fb's refcount.
551 1.1 riastrad */
552 1.1 riastrad void drm_framebuffer_reference(struct drm_framebuffer *fb)
553 1.1 riastrad {
554 1.1 riastrad DRM_DEBUG("FB ID: %d\n", fb->base.id);
555 1.1 riastrad kref_get(&fb->refcount);
556 1.1 riastrad }
557 1.1 riastrad EXPORT_SYMBOL(drm_framebuffer_reference);
558 1.1 riastrad
559 1.1.1.2 riastrad static void drm_framebuffer_free_bug(struct kref *kref)
560 1.1.1.2 riastrad {
561 1.1.1.2 riastrad BUG();
562 1.1.1.2 riastrad }
563 1.1.1.2 riastrad
564 1.1.1.2 riastrad static void __drm_framebuffer_unreference(struct drm_framebuffer *fb)
565 1.1.1.2 riastrad {
566 1.1.1.2 riastrad DRM_DEBUG("FB ID: %d\n", fb->base.id);
567 1.1.1.2 riastrad kref_put(&fb->refcount, drm_framebuffer_free_bug);
568 1.1.1.2 riastrad }
569 1.1.1.2 riastrad
570 1.1.1.2 riastrad /* dev->mode_config.fb_lock must be held! */
571 1.1.1.2 riastrad static void __drm_framebuffer_unregister(struct drm_device *dev,
572 1.1.1.2 riastrad struct drm_framebuffer *fb)
573 1.1.1.2 riastrad {
574 1.1.1.2 riastrad mutex_lock(&dev->mode_config.idr_mutex);
575 1.1.1.2 riastrad idr_remove(&dev->mode_config.crtc_idr, fb->base.id);
576 1.1.1.2 riastrad mutex_unlock(&dev->mode_config.idr_mutex);
577 1.1.1.2 riastrad
578 1.1.1.2 riastrad fb->base.id = 0;
579 1.1.1.2 riastrad
580 1.1.1.2 riastrad __drm_framebuffer_unreference(fb);
581 1.1.1.2 riastrad }
582 1.1.1.2 riastrad
583 1.1.1.2 riastrad /**
584 1.1.1.2 riastrad * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
585 1.1.1.2 riastrad * @fb: fb to unregister
586 1.1.1.2 riastrad *
587 1.1.1.2 riastrad * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
588 1.1.1.2 riastrad * those used for fbdev. Note that the caller must hold a reference of it's own,
589 1.1.1.2 riastrad * i.e. the object may not be destroyed through this call (since it'll lead to a
590 1.1.1.2 riastrad * locking inversion).
591 1.1.1.2 riastrad */
592 1.1.1.2 riastrad void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
593 1.1.1.2 riastrad {
594 1.1.1.2 riastrad struct drm_device *dev = fb->dev;
595 1.1.1.2 riastrad
596 1.1.1.2 riastrad mutex_lock(&dev->mode_config.fb_lock);
597 1.1.1.2 riastrad /* Mark fb as reaped and drop idr ref. */
598 1.1.1.2 riastrad __drm_framebuffer_unregister(dev, fb);
599 1.1.1.2 riastrad mutex_unlock(&dev->mode_config.fb_lock);
600 1.1.1.2 riastrad }
601 1.1.1.2 riastrad EXPORT_SYMBOL(drm_framebuffer_unregister_private);
602 1.1.1.2 riastrad
603 1.1 riastrad /**
604 1.1 riastrad * drm_framebuffer_cleanup - remove a framebuffer object
605 1.1 riastrad * @fb: framebuffer to remove
606 1.1 riastrad *
607 1.1.1.2 riastrad * Cleanup framebuffer. This function is intended to be used from the drivers
608 1.1.1.2 riastrad * ->destroy callback. It can also be used to clean up driver private
609 1.1.1.2 riastrad * framebuffers embedded into a larger structure.
610 1.1.1.2 riastrad *
611 1.1.1.2 riastrad * Note that this function does not remove the fb from active usuage - if it is
612 1.1.1.2 riastrad * still used anywhere, hilarity can ensue since userspace could call getfb on
613 1.1.1.2 riastrad * the id and get back -EINVAL. Obviously no concern at driver unload time.
614 1.1.1.2 riastrad *
615 1.1.1.2 riastrad * Also, the framebuffer will not be removed from the lookup idr - for
616 1.1.1.2 riastrad * user-created framebuffers this will happen in in the rmfb ioctl. For
617 1.1.1.2 riastrad * driver-private objects (e.g. for fbdev) drivers need to explicitly call
618 1.1.1.2 riastrad * drm_framebuffer_unregister_private.
619 1.1 riastrad */
620 1.1 riastrad void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
621 1.1 riastrad {
622 1.1 riastrad struct drm_device *dev = fb->dev;
623 1.1.1.2 riastrad
624 1.1.1.2 riastrad mutex_lock(&dev->mode_config.fb_lock);
625 1.1 riastrad list_del(&fb->head);
626 1.1 riastrad dev->mode_config.num_fb--;
627 1.1.1.2 riastrad mutex_unlock(&dev->mode_config.fb_lock);
628 1.1 riastrad }
629 1.1 riastrad EXPORT_SYMBOL(drm_framebuffer_cleanup);
630 1.1 riastrad
631 1.1 riastrad /**
632 1.1 riastrad * drm_framebuffer_remove - remove and unreference a framebuffer object
633 1.1 riastrad * @fb: framebuffer to remove
634 1.1 riastrad *
635 1.1 riastrad * Scans all the CRTCs and planes in @dev's mode_config. If they're
636 1.1.1.2 riastrad * using @fb, removes it, setting it to NULL. Then drops the reference to the
637 1.1.1.2 riastrad * passed-in framebuffer. Might take the modeset locks.
638 1.1.1.2 riastrad *
639 1.1.1.2 riastrad * Note that this function optimizes the cleanup away if the caller holds the
640 1.1.1.2 riastrad * last reference to the framebuffer. It is also guaranteed to not take the
641 1.1.1.2 riastrad * modeset locks in this case.
642 1.1 riastrad */
643 1.1 riastrad void drm_framebuffer_remove(struct drm_framebuffer *fb)
644 1.1 riastrad {
645 1.1 riastrad struct drm_device *dev = fb->dev;
646 1.1 riastrad struct drm_crtc *crtc;
647 1.1 riastrad struct drm_plane *plane;
648 1.1 riastrad struct drm_mode_set set;
649 1.1 riastrad int ret;
650 1.1 riastrad
651 1.1.1.2 riastrad WARN_ON(!list_empty(&fb->filp_head));
652 1.1.1.2 riastrad
653 1.1.1.2 riastrad /*
654 1.1.1.2 riastrad * drm ABI mandates that we remove any deleted framebuffers from active
655 1.1.1.2 riastrad * useage. But since most sane clients only remove framebuffers they no
656 1.1.1.2 riastrad * longer need, try to optimize this away.
657 1.1.1.2 riastrad *
658 1.1.1.2 riastrad * Since we're holding a reference ourselves, observing a refcount of 1
659 1.1.1.2 riastrad * means that we're the last holder and can skip it. Also, the refcount
660 1.1.1.2 riastrad * can never increase from 1 again, so we don't need any barriers or
661 1.1.1.2 riastrad * locks.
662 1.1.1.2 riastrad *
663 1.1.1.2 riastrad * Note that userspace could try to race with use and instate a new
664 1.1.1.2 riastrad * usage _after_ we've cleared all current ones. End result will be an
665 1.1.1.2 riastrad * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
666 1.1.1.2 riastrad * in this manner.
667 1.1.1.2 riastrad */
668 1.1.1.2 riastrad if (atomic_read(&fb->refcount.refcount) > 1) {
669 1.1.1.2 riastrad drm_modeset_lock_all(dev);
670 1.1.1.2 riastrad /* remove from any CRTC */
671 1.1.1.2 riastrad list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
672 1.1.1.2 riastrad if (crtc->primary->fb == fb) {
673 1.1.1.2 riastrad /* should turn off the crtc */
674 1.1.1.2 riastrad memset(&set, 0, sizeof(struct drm_mode_set));
675 1.1.1.2 riastrad set.crtc = crtc;
676 1.1.1.2 riastrad set.fb = NULL;
677 1.1.1.2 riastrad ret = drm_mode_set_config_internal(&set);
678 1.1.1.2 riastrad if (ret)
679 1.1.1.2 riastrad DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
680 1.1.1.2 riastrad }
681 1.1 riastrad }
682 1.1 riastrad
683 1.1.1.2 riastrad list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
684 1.1.1.2 riastrad if (plane->fb == fb)
685 1.1.1.2 riastrad drm_plane_force_disable(plane);
686 1.1 riastrad }
687 1.1.1.2 riastrad drm_modeset_unlock_all(dev);
688 1.1 riastrad }
689 1.1 riastrad
690 1.1 riastrad drm_framebuffer_unreference(fb);
691 1.1 riastrad }
692 1.1 riastrad EXPORT_SYMBOL(drm_framebuffer_remove);
693 1.1 riastrad
694 1.1 riastrad /**
695 1.1.1.2 riastrad * drm_crtc_init_with_planes - Initialise a new CRTC object with
696 1.1.1.2 riastrad * specified primary and cursor planes.
697 1.1 riastrad * @dev: DRM device
698 1.1 riastrad * @crtc: CRTC object to init
699 1.1.1.2 riastrad * @primary: Primary plane for CRTC
700 1.1.1.2 riastrad * @cursor: Cursor plane for CRTC
701 1.1 riastrad * @funcs: callbacks for the new CRTC
702 1.1 riastrad *
703 1.1.1.2 riastrad * Inits a new object created as base part of a driver crtc object.
704 1.1 riastrad *
705 1.1.1.2 riastrad * Returns:
706 1.1 riastrad * Zero on success, error code on failure.
707 1.1 riastrad */
708 1.1.1.2 riastrad int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
709 1.1.1.2 riastrad struct drm_plane *primary,
710 1.1.1.2 riastrad void *cursor,
711 1.1.1.2 riastrad const struct drm_crtc_funcs *funcs)
712 1.1 riastrad {
713 1.1 riastrad int ret;
714 1.1 riastrad
715 1.1 riastrad crtc->dev = dev;
716 1.1 riastrad crtc->funcs = funcs;
717 1.1 riastrad crtc->invert_dimensions = false;
718 1.1 riastrad
719 1.1.1.2 riastrad drm_modeset_lock_all(dev);
720 1.1.1.2 riastrad mutex_init(&crtc->mutex);
721 1.1.1.2 riastrad mutex_lock_nest_lock(&crtc->mutex, &dev->mode_config.mutex);
722 1.1 riastrad
723 1.1 riastrad ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
724 1.1 riastrad if (ret)
725 1.1 riastrad goto out;
726 1.1 riastrad
727 1.1 riastrad crtc->base.properties = &crtc->properties;
728 1.1 riastrad
729 1.1 riastrad list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
730 1.1 riastrad dev->mode_config.num_crtc++;
731 1.1 riastrad
732 1.1.1.2 riastrad crtc->primary = primary;
733 1.1.1.2 riastrad if (primary)
734 1.1.1.2 riastrad primary->possible_crtcs = 1 << drm_crtc_index(crtc);
735 1.1.1.2 riastrad
736 1.1 riastrad out:
737 1.1.1.2 riastrad drm_modeset_unlock_all(dev);
738 1.1 riastrad
739 1.1 riastrad return ret;
740 1.1 riastrad }
741 1.1.1.2 riastrad EXPORT_SYMBOL(drm_crtc_init_with_planes);
742 1.1 riastrad
743 1.1 riastrad /**
744 1.1.1.2 riastrad * drm_crtc_cleanup - Clean up the core crtc usage
745 1.1 riastrad * @crtc: CRTC to cleanup
746 1.1 riastrad *
747 1.1.1.2 riastrad * This function cleans up @crtc and removes it from the DRM mode setting
748 1.1.1.2 riastrad * core. Note that the function does *not* free the crtc structure itself,
749 1.1.1.2 riastrad * this is the responsibility of the caller.
750 1.1 riastrad */
751 1.1 riastrad void drm_crtc_cleanup(struct drm_crtc *crtc)
752 1.1 riastrad {
753 1.1 riastrad struct drm_device *dev = crtc->dev;
754 1.1 riastrad
755 1.1 riastrad kfree(crtc->gamma_store);
756 1.1 riastrad crtc->gamma_store = NULL;
757 1.1 riastrad
758 1.1 riastrad drm_mode_object_put(dev, &crtc->base);
759 1.1 riastrad list_del(&crtc->head);
760 1.1 riastrad dev->mode_config.num_crtc--;
761 1.1 riastrad }
762 1.1 riastrad EXPORT_SYMBOL(drm_crtc_cleanup);
763 1.1 riastrad
764 1.1 riastrad /**
765 1.1.1.2 riastrad * drm_crtc_index - find the index of a registered CRTC
766 1.1.1.2 riastrad * @crtc: CRTC to find index for
767 1.1 riastrad *
768 1.1.1.2 riastrad * Given a registered CRTC, return the index of that CRTC within a DRM
769 1.1.1.2 riastrad * device's list of CRTCs.
770 1.1 riastrad */
771 1.1.1.2 riastrad unsigned int drm_crtc_index(struct drm_crtc *crtc)
772 1.1 riastrad {
773 1.1.1.2 riastrad unsigned int index = 0;
774 1.1.1.2 riastrad struct drm_crtc *tmp;
775 1.1.1.2 riastrad
776 1.1.1.2 riastrad list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
777 1.1.1.2 riastrad if (tmp == crtc)
778 1.1.1.2 riastrad return index;
779 1.1.1.2 riastrad
780 1.1.1.2 riastrad index++;
781 1.1.1.2 riastrad }
782 1.1.1.2 riastrad
783 1.1.1.2 riastrad BUG();
784 1.1 riastrad }
785 1.1.1.2 riastrad EXPORT_SYMBOL(drm_crtc_index);
786 1.1 riastrad
787 1.1.1.2 riastrad /*
788 1.1 riastrad * drm_mode_remove - remove and free a mode
789 1.1 riastrad * @connector: connector list to modify
790 1.1 riastrad * @mode: mode to remove
791 1.1 riastrad *
792 1.1 riastrad * Remove @mode from @connector's mode list, then free it.
793 1.1 riastrad */
794 1.1.1.2 riastrad static void drm_mode_remove(struct drm_connector *connector,
795 1.1.1.2 riastrad struct drm_display_mode *mode)
796 1.1 riastrad {
797 1.1 riastrad list_del(&mode->head);
798 1.1 riastrad drm_mode_destroy(connector->dev, mode);
799 1.1 riastrad }
800 1.1 riastrad
801 1.1 riastrad /**
802 1.1 riastrad * drm_connector_init - Init a preallocated connector
803 1.1 riastrad * @dev: DRM device
804 1.1 riastrad * @connector: the connector to init
805 1.1 riastrad * @funcs: callbacks for this connector
806 1.1.1.2 riastrad * @connector_type: user visible type of the connector
807 1.1 riastrad *
808 1.1 riastrad * Initialises a preallocated connector. Connectors should be
809 1.1 riastrad * subclassed as part of driver connector objects.
810 1.1 riastrad *
811 1.1.1.2 riastrad * Returns:
812 1.1 riastrad * Zero on success, error code on failure.
813 1.1 riastrad */
814 1.1 riastrad int drm_connector_init(struct drm_device *dev,
815 1.1 riastrad struct drm_connector *connector,
816 1.1 riastrad const struct drm_connector_funcs *funcs,
817 1.1 riastrad int connector_type)
818 1.1 riastrad {
819 1.1 riastrad int ret;
820 1.1.1.2 riastrad struct ida *connector_ida =
821 1.1.1.2 riastrad &drm_connector_enum_list[connector_type].ida;
822 1.1 riastrad
823 1.1.1.2 riastrad drm_modeset_lock_all(dev);
824 1.1 riastrad
825 1.1 riastrad ret = drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
826 1.1 riastrad if (ret)
827 1.1 riastrad goto out;
828 1.1 riastrad
829 1.1 riastrad connector->base.properties = &connector->properties;
830 1.1 riastrad connector->dev = dev;
831 1.1 riastrad connector->funcs = funcs;
832 1.1 riastrad connector->connector_type = connector_type;
833 1.1 riastrad connector->connector_type_id =
834 1.1.1.2 riastrad ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
835 1.1.1.2 riastrad if (connector->connector_type_id < 0) {
836 1.1.1.2 riastrad ret = connector->connector_type_id;
837 1.1.1.2 riastrad drm_mode_object_put(dev, &connector->base);
838 1.1.1.2 riastrad goto out;
839 1.1.1.2 riastrad }
840 1.1 riastrad INIT_LIST_HEAD(&connector->probed_modes);
841 1.1 riastrad INIT_LIST_HEAD(&connector->modes);
842 1.1 riastrad connector->edid_blob_ptr = NULL;
843 1.1 riastrad connector->status = connector_status_unknown;
844 1.1 riastrad
845 1.1 riastrad list_add_tail(&connector->head, &dev->mode_config.connector_list);
846 1.1 riastrad dev->mode_config.num_connector++;
847 1.1 riastrad
848 1.1 riastrad if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
849 1.1 riastrad drm_object_attach_property(&connector->base,
850 1.1 riastrad dev->mode_config.edid_property,
851 1.1 riastrad 0);
852 1.1 riastrad
853 1.1 riastrad drm_object_attach_property(&connector->base,
854 1.1 riastrad dev->mode_config.dpms_property, 0);
855 1.1 riastrad
856 1.1 riastrad out:
857 1.1.1.2 riastrad drm_modeset_unlock_all(dev);
858 1.1 riastrad
859 1.1 riastrad return ret;
860 1.1 riastrad }
861 1.1 riastrad EXPORT_SYMBOL(drm_connector_init);
862 1.1 riastrad
863 1.1 riastrad /**
864 1.1 riastrad * drm_connector_cleanup - cleans up an initialised connector
865 1.1 riastrad * @connector: connector to cleanup
866 1.1 riastrad *
867 1.1 riastrad * Cleans up the connector but doesn't free the object.
868 1.1 riastrad */
869 1.1 riastrad void drm_connector_cleanup(struct drm_connector *connector)
870 1.1 riastrad {
871 1.1 riastrad struct drm_device *dev = connector->dev;
872 1.1 riastrad struct drm_display_mode *mode, *t;
873 1.1 riastrad
874 1.1 riastrad list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
875 1.1 riastrad drm_mode_remove(connector, mode);
876 1.1 riastrad
877 1.1 riastrad list_for_each_entry_safe(mode, t, &connector->modes, head)
878 1.1 riastrad drm_mode_remove(connector, mode);
879 1.1 riastrad
880 1.1.1.2 riastrad ida_remove(&drm_connector_enum_list[connector->connector_type].ida,
881 1.1.1.2 riastrad connector->connector_type_id);
882 1.1 riastrad
883 1.1 riastrad drm_mode_object_put(dev, &connector->base);
884 1.1 riastrad list_del(&connector->head);
885 1.1 riastrad dev->mode_config.num_connector--;
886 1.1 riastrad }
887 1.1 riastrad EXPORT_SYMBOL(drm_connector_cleanup);
888 1.1 riastrad
889 1.1.1.2 riastrad /**
890 1.1.1.2 riastrad * drm_connector_unplug_all - unregister connector userspace interfaces
891 1.1.1.2 riastrad * @dev: drm device
892 1.1.1.2 riastrad *
893 1.1.1.2 riastrad * This function unregisters all connector userspace interfaces in sysfs. Should
894 1.1.1.2 riastrad * be call when the device is disconnected, e.g. from an usb driver's
895 1.1.1.2 riastrad * ->disconnect callback.
896 1.1.1.2 riastrad */
897 1.1 riastrad void drm_connector_unplug_all(struct drm_device *dev)
898 1.1 riastrad {
899 1.1 riastrad struct drm_connector *connector;
900 1.1 riastrad
901 1.1 riastrad /* taking the mode config mutex ends up in a clash with sysfs */
902 1.1 riastrad list_for_each_entry(connector, &dev->mode_config.connector_list, head)
903 1.1 riastrad drm_sysfs_connector_remove(connector);
904 1.1 riastrad
905 1.1 riastrad }
906 1.1 riastrad EXPORT_SYMBOL(drm_connector_unplug_all);
907 1.1 riastrad
908 1.1.1.2 riastrad /**
909 1.1.1.2 riastrad * drm_bridge_init - initialize a drm transcoder/bridge
910 1.1.1.2 riastrad * @dev: drm device
911 1.1.1.2 riastrad * @bridge: transcoder/bridge to set up
912 1.1.1.2 riastrad * @funcs: bridge function table
913 1.1.1.2 riastrad *
914 1.1.1.2 riastrad * Initialises a preallocated bridge. Bridges should be
915 1.1.1.2 riastrad * subclassed as part of driver connector objects.
916 1.1.1.2 riastrad *
917 1.1.1.2 riastrad * Returns:
918 1.1.1.2 riastrad * Zero on success, error code on failure.
919 1.1.1.2 riastrad */
920 1.1.1.2 riastrad int drm_bridge_init(struct drm_device *dev, struct drm_bridge *bridge,
921 1.1.1.2 riastrad const struct drm_bridge_funcs *funcs)
922 1.1.1.2 riastrad {
923 1.1.1.2 riastrad int ret;
924 1.1.1.2 riastrad
925 1.1.1.2 riastrad drm_modeset_lock_all(dev);
926 1.1.1.2 riastrad
927 1.1.1.2 riastrad ret = drm_mode_object_get(dev, &bridge->base, DRM_MODE_OBJECT_BRIDGE);
928 1.1.1.2 riastrad if (ret)
929 1.1.1.2 riastrad goto out;
930 1.1.1.2 riastrad
931 1.1.1.2 riastrad bridge->dev = dev;
932 1.1.1.2 riastrad bridge->funcs = funcs;
933 1.1.1.2 riastrad
934 1.1.1.2 riastrad list_add_tail(&bridge->head, &dev->mode_config.bridge_list);
935 1.1.1.2 riastrad dev->mode_config.num_bridge++;
936 1.1.1.2 riastrad
937 1.1.1.2 riastrad out:
938 1.1.1.2 riastrad drm_modeset_unlock_all(dev);
939 1.1.1.2 riastrad return ret;
940 1.1.1.2 riastrad }
941 1.1.1.2 riastrad EXPORT_SYMBOL(drm_bridge_init);
942 1.1.1.2 riastrad
943 1.1.1.2 riastrad /**
944 1.1.1.2 riastrad * drm_bridge_cleanup - cleans up an initialised bridge
945 1.1.1.2 riastrad * @bridge: bridge to cleanup
946 1.1.1.2 riastrad *
947 1.1.1.2 riastrad * Cleans up the bridge but doesn't free the object.
948 1.1.1.2 riastrad */
949 1.1.1.2 riastrad void drm_bridge_cleanup(struct drm_bridge *bridge)
950 1.1.1.2 riastrad {
951 1.1.1.2 riastrad struct drm_device *dev = bridge->dev;
952 1.1.1.2 riastrad
953 1.1.1.2 riastrad drm_modeset_lock_all(dev);
954 1.1.1.2 riastrad drm_mode_object_put(dev, &bridge->base);
955 1.1.1.2 riastrad list_del(&bridge->head);
956 1.1.1.2 riastrad dev->mode_config.num_bridge--;
957 1.1.1.2 riastrad drm_modeset_unlock_all(dev);
958 1.1.1.2 riastrad }
959 1.1.1.2 riastrad EXPORT_SYMBOL(drm_bridge_cleanup);
960 1.1.1.2 riastrad
961 1.1.1.2 riastrad /**
962 1.1.1.2 riastrad * drm_encoder_init - Init a preallocated encoder
963 1.1.1.2 riastrad * @dev: drm device
964 1.1.1.2 riastrad * @encoder: the encoder to init
965 1.1.1.2 riastrad * @funcs: callbacks for this encoder
966 1.1.1.2 riastrad * @encoder_type: user visible type of the encoder
967 1.1.1.2 riastrad *
968 1.1.1.2 riastrad * Initialises a preallocated encoder. Encoder should be
969 1.1.1.2 riastrad * subclassed as part of driver encoder objects.
970 1.1.1.2 riastrad *
971 1.1.1.2 riastrad * Returns:
972 1.1.1.2 riastrad * Zero on success, error code on failure.
973 1.1.1.2 riastrad */
974 1.1 riastrad int drm_encoder_init(struct drm_device *dev,
975 1.1 riastrad struct drm_encoder *encoder,
976 1.1 riastrad const struct drm_encoder_funcs *funcs,
977 1.1 riastrad int encoder_type)
978 1.1 riastrad {
979 1.1 riastrad int ret;
980 1.1 riastrad
981 1.1.1.2 riastrad drm_modeset_lock_all(dev);
982 1.1 riastrad
983 1.1 riastrad ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
984 1.1 riastrad if (ret)
985 1.1 riastrad goto out;
986 1.1 riastrad
987 1.1 riastrad encoder->dev = dev;
988 1.1 riastrad encoder->encoder_type = encoder_type;
989 1.1 riastrad encoder->funcs = funcs;
990 1.1 riastrad
991 1.1 riastrad list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
992 1.1 riastrad dev->mode_config.num_encoder++;
993 1.1 riastrad
994 1.1 riastrad out:
995 1.1.1.2 riastrad drm_modeset_unlock_all(dev);
996 1.1 riastrad
997 1.1 riastrad return ret;
998 1.1 riastrad }
999 1.1 riastrad EXPORT_SYMBOL(drm_encoder_init);
1000 1.1 riastrad
1001 1.1.1.2 riastrad /**
1002 1.1.1.2 riastrad * drm_encoder_cleanup - cleans up an initialised encoder
1003 1.1.1.2 riastrad * @encoder: encoder to cleanup
1004 1.1.1.2 riastrad *
1005 1.1.1.2 riastrad * Cleans up the encoder but doesn't free the object.
1006 1.1.1.2 riastrad */
1007 1.1 riastrad void drm_encoder_cleanup(struct drm_encoder *encoder)
1008 1.1 riastrad {
1009 1.1 riastrad struct drm_device *dev = encoder->dev;
1010 1.1.1.2 riastrad drm_modeset_lock_all(dev);
1011 1.1 riastrad drm_mode_object_put(dev, &encoder->base);
1012 1.1 riastrad list_del(&encoder->head);
1013 1.1 riastrad dev->mode_config.num_encoder--;
1014 1.1.1.2 riastrad drm_modeset_unlock_all(dev);
1015 1.1 riastrad }
1016 1.1 riastrad EXPORT_SYMBOL(drm_encoder_cleanup);
1017 1.1 riastrad
1018 1.1.1.2 riastrad /**
1019 1.1.1.2 riastrad * drm_universal_plane_init - Initialize a new universal plane object
1020 1.1.1.2 riastrad * @dev: DRM device
1021 1.1.1.2 riastrad * @plane: plane object to init
1022 1.1.1.2 riastrad * @possible_crtcs: bitmask of possible CRTCs
1023 1.1.1.2 riastrad * @funcs: callbacks for the new plane
1024 1.1.1.2 riastrad * @formats: array of supported formats (%DRM_FORMAT_*)
1025 1.1.1.2 riastrad * @format_count: number of elements in @formats
1026 1.1.1.2 riastrad * @type: type of plane (overlay, primary, cursor)
1027 1.1.1.2 riastrad *
1028 1.1.1.2 riastrad * Initializes a plane object of type @type.
1029 1.1.1.2 riastrad *
1030 1.1.1.2 riastrad * Returns:
1031 1.1.1.2 riastrad * Zero on success, error code on failure.
1032 1.1.1.2 riastrad */
1033 1.1.1.2 riastrad int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane,
1034 1.1.1.2 riastrad unsigned long possible_crtcs,
1035 1.1.1.2 riastrad const struct drm_plane_funcs *funcs,
1036 1.1.1.2 riastrad const uint32_t *formats, uint32_t format_count,
1037 1.1.1.2 riastrad enum drm_plane_type type)
1038 1.1 riastrad {
1039 1.1 riastrad int ret;
1040 1.1 riastrad
1041 1.1.1.2 riastrad drm_modeset_lock_all(dev);
1042 1.1 riastrad
1043 1.1 riastrad ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
1044 1.1 riastrad if (ret)
1045 1.1 riastrad goto out;
1046 1.1 riastrad
1047 1.1 riastrad plane->base.properties = &plane->properties;
1048 1.1 riastrad plane->dev = dev;
1049 1.1 riastrad plane->funcs = funcs;
1050 1.1 riastrad plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
1051 1.1 riastrad GFP_KERNEL);
1052 1.1 riastrad if (!plane->format_types) {
1053 1.1 riastrad DRM_DEBUG_KMS("out of memory when allocating plane\n");
1054 1.1 riastrad drm_mode_object_put(dev, &plane->base);
1055 1.1 riastrad ret = -ENOMEM;
1056 1.1 riastrad goto out;
1057 1.1 riastrad }
1058 1.1 riastrad
1059 1.1 riastrad memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
1060 1.1 riastrad plane->format_count = format_count;
1061 1.1 riastrad plane->possible_crtcs = possible_crtcs;
1062 1.1.1.2 riastrad plane->type = type;
1063 1.1 riastrad
1064 1.1.1.2 riastrad list_add_tail(&plane->head, &dev->mode_config.plane_list);
1065 1.1.1.2 riastrad dev->mode_config.num_total_plane++;
1066 1.1.1.2 riastrad if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1067 1.1.1.2 riastrad dev->mode_config.num_overlay_plane++;
1068 1.1.1.2 riastrad
1069 1.1.1.2 riastrad drm_object_attach_property(&plane->base,
1070 1.1.1.2 riastrad dev->mode_config.plane_type_property,
1071 1.1.1.2 riastrad plane->type);
1072 1.1 riastrad
1073 1.1 riastrad out:
1074 1.1.1.2 riastrad drm_modeset_unlock_all(dev);
1075 1.1 riastrad
1076 1.1 riastrad return ret;
1077 1.1 riastrad }
1078 1.1.1.2 riastrad EXPORT_SYMBOL(drm_universal_plane_init);
1079 1.1 riastrad
1080 1.1.1.2 riastrad /**
1081 1.1.1.2 riastrad * drm_plane_init - Initialize a legacy plane
1082 1.1.1.2 riastrad * @dev: DRM device
1083 1.1.1.2 riastrad * @plane: plane object to init
1084 1.1.1.2 riastrad * @possible_crtcs: bitmask of possible CRTCs
1085 1.1.1.2 riastrad * @funcs: callbacks for the new plane
1086 1.1.1.2 riastrad * @formats: array of supported formats (%DRM_FORMAT_*)
1087 1.1.1.2 riastrad * @format_count: number of elements in @formats
1088 1.1.1.2 riastrad * @is_primary: plane type (primary vs overlay)
1089 1.1.1.2 riastrad *
1090 1.1.1.2 riastrad * Legacy API to initialize a DRM plane.
1091 1.1.1.2 riastrad *
1092 1.1.1.2 riastrad * New drivers should call drm_universal_plane_init() instead.
1093 1.1.1.2 riastrad *
1094 1.1.1.2 riastrad * Returns:
1095 1.1.1.2 riastrad * Zero on success, error code on failure.
1096 1.1.1.2 riastrad */
1097 1.1.1.2 riastrad int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
1098 1.1.1.2 riastrad unsigned long possible_crtcs,
1099 1.1.1.2 riastrad const struct drm_plane_funcs *funcs,
1100 1.1.1.2 riastrad const uint32_t *formats, uint32_t format_count,
1101 1.1.1.2 riastrad bool is_primary)
1102 1.1 riastrad {
1103 1.1.1.2 riastrad enum drm_plane_type type;
1104 1.1 riastrad
1105 1.1.1.2 riastrad type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
1106 1.1.1.2 riastrad return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
1107 1.1.1.2 riastrad formats, format_count, type);
1108 1.1 riastrad }
1109 1.1.1.2 riastrad EXPORT_SYMBOL(drm_plane_init);
1110 1.1 riastrad
1111 1.1 riastrad /**
1112 1.1.1.2 riastrad * drm_plane_cleanup - Clean up the core plane usage
1113 1.1.1.2 riastrad * @plane: plane to cleanup
1114 1.1 riastrad *
1115 1.1.1.2 riastrad * This function cleans up @plane and removes it from the DRM mode setting
1116 1.1.1.2 riastrad * core. Note that the function does *not* free the plane structure itself,
1117 1.1.1.2 riastrad * this is the responsibility of the caller.
1118 1.1 riastrad */
1119 1.1.1.2 riastrad void drm_plane_cleanup(struct drm_plane *plane)
1120 1.1 riastrad {
1121 1.1.1.2 riastrad struct drm_device *dev = plane->dev;
1122 1.1 riastrad
1123 1.1.1.2 riastrad drm_modeset_lock_all(dev);
1124 1.1.1.2 riastrad kfree(plane->format_types);
1125 1.1.1.2 riastrad drm_mode_object_put(dev, &plane->base);
1126 1.1 riastrad
1127 1.1.1.2 riastrad BUG_ON(list_empty(&plane->head));
1128 1.1 riastrad
1129 1.1.1.2 riastrad list_del(&plane->head);
1130 1.1.1.2 riastrad dev->mode_config.num_total_plane--;
1131 1.1.1.2 riastrad if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1132 1.1.1.2 riastrad dev->mode_config.num_overlay_plane--;
1133 1.1.1.2 riastrad drm_modeset_unlock_all(dev);
1134 1.1 riastrad }
1135 1.1.1.2 riastrad EXPORT_SYMBOL(drm_plane_cleanup);
1136 1.1 riastrad
1137 1.1 riastrad /**
1138 1.1.1.2 riastrad * drm_plane_force_disable - Forcibly disable a plane
1139 1.1.1.2 riastrad * @plane: plane to disable
1140 1.1 riastrad *
1141 1.1.1.2 riastrad * Forces the plane to be disabled.
1142 1.1 riastrad *
1143 1.1.1.2 riastrad * Used when the plane's current framebuffer is destroyed,
1144 1.1.1.2 riastrad * and when restoring fbdev mode.
1145 1.1 riastrad */
1146 1.1.1.2 riastrad void drm_plane_force_disable(struct drm_plane *plane)
1147 1.1 riastrad {
1148 1.1.1.2 riastrad int ret;
1149 1.1 riastrad
1150 1.1.1.2 riastrad if (!plane->fb)
1151 1.1.1.2 riastrad return;
1152 1.1 riastrad
1153 1.1.1.2 riastrad ret = plane->funcs->disable_plane(plane);
1154 1.1.1.2 riastrad if (ret)
1155 1.1.1.2 riastrad DRM_ERROR("failed to disable plane with busy fb\n");
1156 1.1.1.2 riastrad /* disconnect the plane from the fb and crtc: */
1157 1.1.1.2 riastrad __drm_framebuffer_unreference(plane->fb);
1158 1.1.1.2 riastrad plane->fb = NULL;
1159 1.1.1.2 riastrad plane->crtc = NULL;
1160 1.1 riastrad }
1161 1.1.1.2 riastrad EXPORT_SYMBOL(drm_plane_force_disable);
1162 1.1 riastrad
1163 1.1 riastrad static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
1164 1.1 riastrad {
1165 1.1 riastrad struct drm_property *edid;
1166 1.1 riastrad struct drm_property *dpms;
1167 1.1 riastrad
1168 1.1 riastrad /*
1169 1.1 riastrad * Standard properties (apply to all connectors)
1170 1.1 riastrad */
1171 1.1 riastrad edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
1172 1.1 riastrad DRM_MODE_PROP_IMMUTABLE,
1173 1.1 riastrad "EDID", 0);
1174 1.1 riastrad dev->mode_config.edid_property = edid;
1175 1.1 riastrad
1176 1.1 riastrad dpms = drm_property_create_enum(dev, 0,
1177 1.1 riastrad "DPMS", drm_dpms_enum_list,
1178 1.1 riastrad ARRAY_SIZE(drm_dpms_enum_list));
1179 1.1 riastrad dev->mode_config.dpms_property = dpms;
1180 1.1 riastrad
1181 1.1 riastrad return 0;
1182 1.1 riastrad }
1183 1.1 riastrad
1184 1.1.1.2 riastrad static int drm_mode_create_standard_plane_properties(struct drm_device *dev)
1185 1.1.1.2 riastrad {
1186 1.1.1.2 riastrad struct drm_property *type;
1187 1.1.1.2 riastrad
1188 1.1.1.2 riastrad /*
1189 1.1.1.2 riastrad * Standard properties (apply to all planes)
1190 1.1.1.2 riastrad */
1191 1.1.1.2 riastrad type = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1192 1.1.1.2 riastrad "type", drm_plane_type_enum_list,
1193 1.1.1.2 riastrad ARRAY_SIZE(drm_plane_type_enum_list));
1194 1.1.1.2 riastrad dev->mode_config.plane_type_property = type;
1195 1.1.1.2 riastrad
1196 1.1.1.2 riastrad return 0;
1197 1.1.1.2 riastrad }
1198 1.1.1.2 riastrad
1199 1.1 riastrad /**
1200 1.1 riastrad * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1201 1.1 riastrad * @dev: DRM device
1202 1.1 riastrad *
1203 1.1 riastrad * Called by a driver the first time a DVI-I connector is made.
1204 1.1 riastrad */
1205 1.1 riastrad int drm_mode_create_dvi_i_properties(struct drm_device *dev)
1206 1.1 riastrad {
1207 1.1 riastrad struct drm_property *dvi_i_selector;
1208 1.1 riastrad struct drm_property *dvi_i_subconnector;
1209 1.1 riastrad
1210 1.1 riastrad if (dev->mode_config.dvi_i_select_subconnector_property)
1211 1.1 riastrad return 0;
1212 1.1 riastrad
1213 1.1 riastrad dvi_i_selector =
1214 1.1 riastrad drm_property_create_enum(dev, 0,
1215 1.1 riastrad "select subconnector",
1216 1.1 riastrad drm_dvi_i_select_enum_list,
1217 1.1 riastrad ARRAY_SIZE(drm_dvi_i_select_enum_list));
1218 1.1 riastrad dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
1219 1.1 riastrad
1220 1.1 riastrad dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1221 1.1 riastrad "subconnector",
1222 1.1 riastrad drm_dvi_i_subconnector_enum_list,
1223 1.1 riastrad ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
1224 1.1 riastrad dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
1225 1.1 riastrad
1226 1.1 riastrad return 0;
1227 1.1 riastrad }
1228 1.1 riastrad EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
1229 1.1 riastrad
1230 1.1 riastrad /**
1231 1.1 riastrad * drm_create_tv_properties - create TV specific connector properties
1232 1.1 riastrad * @dev: DRM device
1233 1.1 riastrad * @num_modes: number of different TV formats (modes) supported
1234 1.1 riastrad * @modes: array of pointers to strings containing name of each format
1235 1.1 riastrad *
1236 1.1 riastrad * Called by a driver's TV initialization routine, this function creates
1237 1.1 riastrad * the TV specific connector properties for a given device. Caller is
1238 1.1 riastrad * responsible for allocating a list of format names and passing them to
1239 1.1 riastrad * this routine.
1240 1.1 riastrad */
1241 1.1 riastrad int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
1242 1.1 riastrad char *modes[])
1243 1.1 riastrad {
1244 1.1 riastrad struct drm_property *tv_selector;
1245 1.1 riastrad struct drm_property *tv_subconnector;
1246 1.1 riastrad int i;
1247 1.1 riastrad
1248 1.1 riastrad if (dev->mode_config.tv_select_subconnector_property)
1249 1.1 riastrad return 0;
1250 1.1 riastrad
1251 1.1 riastrad /*
1252 1.1 riastrad * Basic connector properties
1253 1.1 riastrad */
1254 1.1 riastrad tv_selector = drm_property_create_enum(dev, 0,
1255 1.1 riastrad "select subconnector",
1256 1.1 riastrad drm_tv_select_enum_list,
1257 1.1 riastrad ARRAY_SIZE(drm_tv_select_enum_list));
1258 1.1 riastrad dev->mode_config.tv_select_subconnector_property = tv_selector;
1259 1.1 riastrad
1260 1.1 riastrad tv_subconnector =
1261 1.1 riastrad drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1262 1.1 riastrad "subconnector",
1263 1.1 riastrad drm_tv_subconnector_enum_list,
1264 1.1 riastrad ARRAY_SIZE(drm_tv_subconnector_enum_list));
1265 1.1 riastrad dev->mode_config.tv_subconnector_property = tv_subconnector;
1266 1.1 riastrad
1267 1.1 riastrad /*
1268 1.1 riastrad * Other, TV specific properties: margins & TV modes.
1269 1.1 riastrad */
1270 1.1 riastrad dev->mode_config.tv_left_margin_property =
1271 1.1 riastrad drm_property_create_range(dev, 0, "left margin", 0, 100);
1272 1.1 riastrad
1273 1.1 riastrad dev->mode_config.tv_right_margin_property =
1274 1.1 riastrad drm_property_create_range(dev, 0, "right margin", 0, 100);
1275 1.1 riastrad
1276 1.1 riastrad dev->mode_config.tv_top_margin_property =
1277 1.1 riastrad drm_property_create_range(dev, 0, "top margin", 0, 100);
1278 1.1 riastrad
1279 1.1 riastrad dev->mode_config.tv_bottom_margin_property =
1280 1.1 riastrad drm_property_create_range(dev, 0, "bottom margin", 0, 100);
1281 1.1 riastrad
1282 1.1 riastrad dev->mode_config.tv_mode_property =
1283 1.1 riastrad drm_property_create(dev, DRM_MODE_PROP_ENUM,
1284 1.1 riastrad "mode", num_modes);
1285 1.1 riastrad for (i = 0; i < num_modes; i++)
1286 1.1 riastrad drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1287 1.1 riastrad i, modes[i]);
1288 1.1 riastrad
1289 1.1 riastrad dev->mode_config.tv_brightness_property =
1290 1.1 riastrad drm_property_create_range(dev, 0, "brightness", 0, 100);
1291 1.1 riastrad
1292 1.1 riastrad dev->mode_config.tv_contrast_property =
1293 1.1 riastrad drm_property_create_range(dev, 0, "contrast", 0, 100);
1294 1.1 riastrad
1295 1.1 riastrad dev->mode_config.tv_flicker_reduction_property =
1296 1.1 riastrad drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
1297 1.1 riastrad
1298 1.1 riastrad dev->mode_config.tv_overscan_property =
1299 1.1 riastrad drm_property_create_range(dev, 0, "overscan", 0, 100);
1300 1.1 riastrad
1301 1.1 riastrad dev->mode_config.tv_saturation_property =
1302 1.1 riastrad drm_property_create_range(dev, 0, "saturation", 0, 100);
1303 1.1 riastrad
1304 1.1 riastrad dev->mode_config.tv_hue_property =
1305 1.1 riastrad drm_property_create_range(dev, 0, "hue", 0, 100);
1306 1.1 riastrad
1307 1.1 riastrad return 0;
1308 1.1 riastrad }
1309 1.1 riastrad EXPORT_SYMBOL(drm_mode_create_tv_properties);
1310 1.1 riastrad
1311 1.1 riastrad /**
1312 1.1 riastrad * drm_mode_create_scaling_mode_property - create scaling mode property
1313 1.1 riastrad * @dev: DRM device
1314 1.1 riastrad *
1315 1.1 riastrad * Called by a driver the first time it's needed, must be attached to desired
1316 1.1 riastrad * connectors.
1317 1.1 riastrad */
1318 1.1 riastrad int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1319 1.1 riastrad {
1320 1.1 riastrad struct drm_property *scaling_mode;
1321 1.1 riastrad
1322 1.1 riastrad if (dev->mode_config.scaling_mode_property)
1323 1.1 riastrad return 0;
1324 1.1 riastrad
1325 1.1 riastrad scaling_mode =
1326 1.1 riastrad drm_property_create_enum(dev, 0, "scaling mode",
1327 1.1 riastrad drm_scaling_mode_enum_list,
1328 1.1 riastrad ARRAY_SIZE(drm_scaling_mode_enum_list));
1329 1.1 riastrad
1330 1.1 riastrad dev->mode_config.scaling_mode_property = scaling_mode;
1331 1.1 riastrad
1332 1.1 riastrad return 0;
1333 1.1 riastrad }
1334 1.1 riastrad EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1335 1.1 riastrad
1336 1.1 riastrad /**
1337 1.1 riastrad * drm_mode_create_dirty_property - create dirty property
1338 1.1 riastrad * @dev: DRM device
1339 1.1 riastrad *
1340 1.1 riastrad * Called by a driver the first time it's needed, must be attached to desired
1341 1.1 riastrad * connectors.
1342 1.1 riastrad */
1343 1.1.1.2 riastrad int drm_mode_create_dirty_info_property(struct drm_device *dev)
1344 1.1 riastrad {
1345 1.1.1.2 riastrad struct drm_property *dirty_info;
1346 1.1 riastrad
1347 1.1.1.2 riastrad if (dev->mode_config.dirty_info_property)
1348 1.1.1.2 riastrad return 0;
1349 1.1 riastrad
1350 1.1.1.2 riastrad dirty_info =
1351 1.1.1.2 riastrad drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1352 1.1.1.2 riastrad "dirty",
1353 1.1.1.2 riastrad drm_dirty_info_enum_list,
1354 1.1.1.2 riastrad ARRAY_SIZE(drm_dirty_info_enum_list));
1355 1.1.1.2 riastrad dev->mode_config.dirty_info_property = dirty_info;
1356 1.1.1.2 riastrad
1357 1.1.1.2 riastrad return 0;
1358 1.1 riastrad }
1359 1.1.1.2 riastrad EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1360 1.1 riastrad
1361 1.1.1.2 riastrad static int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
1362 1.1 riastrad {
1363 1.1 riastrad uint32_t total_objects = 0;
1364 1.1 riastrad
1365 1.1 riastrad total_objects += dev->mode_config.num_crtc;
1366 1.1 riastrad total_objects += dev->mode_config.num_connector;
1367 1.1 riastrad total_objects += dev->mode_config.num_encoder;
1368 1.1.1.2 riastrad total_objects += dev->mode_config.num_bridge;
1369 1.1 riastrad
1370 1.1 riastrad group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
1371 1.1 riastrad if (!group->id_list)
1372 1.1 riastrad return -ENOMEM;
1373 1.1 riastrad
1374 1.1 riastrad group->num_crtcs = 0;
1375 1.1 riastrad group->num_connectors = 0;
1376 1.1 riastrad group->num_encoders = 0;
1377 1.1.1.2 riastrad group->num_bridges = 0;
1378 1.1 riastrad return 0;
1379 1.1 riastrad }
1380 1.1 riastrad
1381 1.1.1.2 riastrad /*
1382 1.1.1.2 riastrad * NOTE: Driver's shouldn't ever call drm_mode_group_init_legacy_group - it is
1383 1.1.1.2 riastrad * the drm core's responsibility to set up mode control groups.
1384 1.1.1.2 riastrad */
1385 1.1 riastrad int drm_mode_group_init_legacy_group(struct drm_device *dev,
1386 1.1 riastrad struct drm_mode_group *group)
1387 1.1 riastrad {
1388 1.1 riastrad struct drm_crtc *crtc;
1389 1.1 riastrad struct drm_encoder *encoder;
1390 1.1 riastrad struct drm_connector *connector;
1391 1.1.1.2 riastrad struct drm_bridge *bridge;
1392 1.1 riastrad int ret;
1393 1.1 riastrad
1394 1.1 riastrad if ((ret = drm_mode_group_init(dev, group)))
1395 1.1 riastrad return ret;
1396 1.1 riastrad
1397 1.1 riastrad list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
1398 1.1 riastrad group->id_list[group->num_crtcs++] = crtc->base.id;
1399 1.1 riastrad
1400 1.1 riastrad list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
1401 1.1 riastrad group->id_list[group->num_crtcs + group->num_encoders++] =
1402 1.1 riastrad encoder->base.id;
1403 1.1 riastrad
1404 1.1 riastrad list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1405 1.1 riastrad group->id_list[group->num_crtcs + group->num_encoders +
1406 1.1 riastrad group->num_connectors++] = connector->base.id;
1407 1.1 riastrad
1408 1.1.1.2 riastrad list_for_each_entry(bridge, &dev->mode_config.bridge_list, head)
1409 1.1.1.2 riastrad group->id_list[group->num_crtcs + group->num_encoders +
1410 1.1.1.2 riastrad group->num_connectors + group->num_bridges++] =
1411 1.1.1.2 riastrad bridge->base.id;
1412 1.1.1.2 riastrad
1413 1.1 riastrad return 0;
1414 1.1 riastrad }
1415 1.1 riastrad EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
1416 1.1 riastrad
1417 1.1 riastrad /**
1418 1.1 riastrad * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1419 1.1 riastrad * @out: drm_mode_modeinfo struct to return to the user
1420 1.1 riastrad * @in: drm_display_mode to use
1421 1.1 riastrad *
1422 1.1 riastrad * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1423 1.1 riastrad * the user.
1424 1.1 riastrad */
1425 1.1 riastrad static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1426 1.1 riastrad const struct drm_display_mode *in)
1427 1.1 riastrad {
1428 1.1 riastrad WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1429 1.1 riastrad in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1430 1.1 riastrad in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1431 1.1 riastrad in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1432 1.1 riastrad in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1433 1.1 riastrad "timing values too large for mode info\n");
1434 1.1 riastrad
1435 1.1 riastrad out->clock = in->clock;
1436 1.1 riastrad out->hdisplay = in->hdisplay;
1437 1.1 riastrad out->hsync_start = in->hsync_start;
1438 1.1 riastrad out->hsync_end = in->hsync_end;
1439 1.1 riastrad out->htotal = in->htotal;
1440 1.1 riastrad out->hskew = in->hskew;
1441 1.1 riastrad out->vdisplay = in->vdisplay;
1442 1.1 riastrad out->vsync_start = in->vsync_start;
1443 1.1 riastrad out->vsync_end = in->vsync_end;
1444 1.1 riastrad out->vtotal = in->vtotal;
1445 1.1 riastrad out->vscan = in->vscan;
1446 1.1 riastrad out->vrefresh = in->vrefresh;
1447 1.1 riastrad out->flags = in->flags;
1448 1.1 riastrad out->type = in->type;
1449 1.1 riastrad strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1450 1.1 riastrad out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1451 1.1 riastrad }
1452 1.1 riastrad
1453 1.1 riastrad /**
1454 1.1.1.2 riastrad * drm_crtc_convert_umode - convert a modeinfo into a drm_display_mode
1455 1.1 riastrad * @out: drm_display_mode to return to the user
1456 1.1 riastrad * @in: drm_mode_modeinfo to use
1457 1.1 riastrad *
1458 1.1 riastrad * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1459 1.1 riastrad * the caller.
1460 1.1 riastrad *
1461 1.1.1.2 riastrad * Returns:
1462 1.1 riastrad * Zero on success, errno on failure.
1463 1.1 riastrad */
1464 1.1 riastrad static int drm_crtc_convert_umode(struct drm_display_mode *out,
1465 1.1 riastrad const struct drm_mode_modeinfo *in)
1466 1.1 riastrad {
1467 1.1 riastrad if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1468 1.1 riastrad return -ERANGE;
1469 1.1 riastrad
1470 1.1.1.2 riastrad if ((in->flags & DRM_MODE_FLAG_3D_MASK) > DRM_MODE_FLAG_3D_MAX)
1471 1.1.1.2 riastrad return -EINVAL;
1472 1.1.1.2 riastrad
1473 1.1 riastrad out->clock = in->clock;
1474 1.1 riastrad out->hdisplay = in->hdisplay;
1475 1.1 riastrad out->hsync_start = in->hsync_start;
1476 1.1 riastrad out->hsync_end = in->hsync_end;
1477 1.1 riastrad out->htotal = in->htotal;
1478 1.1 riastrad out->hskew = in->hskew;
1479 1.1 riastrad out->vdisplay = in->vdisplay;
1480 1.1 riastrad out->vsync_start = in->vsync_start;
1481 1.1 riastrad out->vsync_end = in->vsync_end;
1482 1.1 riastrad out->vtotal = in->vtotal;
1483 1.1 riastrad out->vscan = in->vscan;
1484 1.1 riastrad out->vrefresh = in->vrefresh;
1485 1.1 riastrad out->flags = in->flags;
1486 1.1 riastrad out->type = in->type;
1487 1.1 riastrad strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1488 1.1 riastrad out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1489 1.1 riastrad
1490 1.1 riastrad return 0;
1491 1.1 riastrad }
1492 1.1 riastrad
1493 1.1 riastrad /**
1494 1.1 riastrad * drm_mode_getresources - get graphics configuration
1495 1.1.1.2 riastrad * @dev: drm device for the ioctl
1496 1.1.1.2 riastrad * @data: data pointer for the ioctl
1497 1.1.1.2 riastrad * @file_priv: drm file for the ioctl call
1498 1.1 riastrad *
1499 1.1 riastrad * Construct a set of configuration description structures and return
1500 1.1 riastrad * them to the user, including CRTC, connector and framebuffer configuration.
1501 1.1 riastrad *
1502 1.1 riastrad * Called by the user via ioctl.
1503 1.1 riastrad *
1504 1.1.1.2 riastrad * Returns:
1505 1.1 riastrad * Zero on success, errno on failure.
1506 1.1 riastrad */
1507 1.1 riastrad int drm_mode_getresources(struct drm_device *dev, void *data,
1508 1.1 riastrad struct drm_file *file_priv)
1509 1.1 riastrad {
1510 1.1 riastrad struct drm_mode_card_res *card_res = data;
1511 1.1 riastrad struct list_head *lh;
1512 1.1 riastrad struct drm_framebuffer *fb;
1513 1.1 riastrad struct drm_connector *connector;
1514 1.1 riastrad struct drm_crtc *crtc;
1515 1.1 riastrad struct drm_encoder *encoder;
1516 1.1 riastrad int ret = 0;
1517 1.1 riastrad int connector_count = 0;
1518 1.1 riastrad int crtc_count = 0;
1519 1.1 riastrad int fb_count = 0;
1520 1.1 riastrad int encoder_count = 0;
1521 1.1 riastrad int copied = 0, i;
1522 1.1 riastrad uint32_t __user *fb_id;
1523 1.1 riastrad uint32_t __user *crtc_id;
1524 1.1 riastrad uint32_t __user *connector_id;
1525 1.1 riastrad uint32_t __user *encoder_id;
1526 1.1 riastrad struct drm_mode_group *mode_group;
1527 1.1 riastrad
1528 1.1 riastrad if (!drm_core_check_feature(dev, DRIVER_MODESET))
1529 1.1 riastrad return -EINVAL;
1530 1.1 riastrad
1531 1.1 riastrad
1532 1.1.1.2 riastrad mutex_lock(&file_priv->fbs_lock);
1533 1.1 riastrad /*
1534 1.1 riastrad * For the non-control nodes we need to limit the list of resources
1535 1.1 riastrad * by IDs in the group list for this node
1536 1.1 riastrad */
1537 1.1 riastrad list_for_each(lh, &file_priv->fbs)
1538 1.1 riastrad fb_count++;
1539 1.1 riastrad
1540 1.1.1.2 riastrad /* handle this in 4 parts */
1541 1.1.1.2 riastrad /* FBs */
1542 1.1.1.2 riastrad if (card_res->count_fbs >= fb_count) {
1543 1.1.1.2 riastrad copied = 0;
1544 1.1.1.2 riastrad fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1545 1.1.1.2 riastrad list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1546 1.1.1.2 riastrad if (put_user(fb->base.id, fb_id + copied)) {
1547 1.1.1.2 riastrad mutex_unlock(&file_priv->fbs_lock);
1548 1.1.1.2 riastrad return -EFAULT;
1549 1.1.1.2 riastrad }
1550 1.1.1.2 riastrad copied++;
1551 1.1.1.2 riastrad }
1552 1.1.1.2 riastrad }
1553 1.1.1.2 riastrad card_res->count_fbs = fb_count;
1554 1.1.1.2 riastrad mutex_unlock(&file_priv->fbs_lock);
1555 1.1.1.2 riastrad
1556 1.1.1.2 riastrad drm_modeset_lock_all(dev);
1557 1.1.1.2 riastrad if (!drm_is_primary_client(file_priv)) {
1558 1.1 riastrad
1559 1.1.1.2 riastrad mode_group = NULL;
1560 1.1 riastrad list_for_each(lh, &dev->mode_config.crtc_list)
1561 1.1 riastrad crtc_count++;
1562 1.1 riastrad
1563 1.1 riastrad list_for_each(lh, &dev->mode_config.connector_list)
1564 1.1 riastrad connector_count++;
1565 1.1 riastrad
1566 1.1 riastrad list_for_each(lh, &dev->mode_config.encoder_list)
1567 1.1 riastrad encoder_count++;
1568 1.1 riastrad } else {
1569 1.1 riastrad
1570 1.1.1.2 riastrad mode_group = &file_priv->master->minor->mode_group;
1571 1.1 riastrad crtc_count = mode_group->num_crtcs;
1572 1.1 riastrad connector_count = mode_group->num_connectors;
1573 1.1 riastrad encoder_count = mode_group->num_encoders;
1574 1.1 riastrad }
1575 1.1 riastrad
1576 1.1 riastrad card_res->max_height = dev->mode_config.max_height;
1577 1.1 riastrad card_res->min_height = dev->mode_config.min_height;
1578 1.1 riastrad card_res->max_width = dev->mode_config.max_width;
1579 1.1 riastrad card_res->min_width = dev->mode_config.min_width;
1580 1.1 riastrad
1581 1.1 riastrad /* CRTCs */
1582 1.1 riastrad if (card_res->count_crtcs >= crtc_count) {
1583 1.1 riastrad copied = 0;
1584 1.1 riastrad crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1585 1.1.1.2 riastrad if (!mode_group) {
1586 1.1 riastrad list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1587 1.1 riastrad head) {
1588 1.1 riastrad DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
1589 1.1 riastrad if (put_user(crtc->base.id, crtc_id + copied)) {
1590 1.1 riastrad ret = -EFAULT;
1591 1.1 riastrad goto out;
1592 1.1 riastrad }
1593 1.1 riastrad copied++;
1594 1.1 riastrad }
1595 1.1 riastrad } else {
1596 1.1 riastrad for (i = 0; i < mode_group->num_crtcs; i++) {
1597 1.1 riastrad if (put_user(mode_group->id_list[i],
1598 1.1 riastrad crtc_id + copied)) {
1599 1.1 riastrad ret = -EFAULT;
1600 1.1 riastrad goto out;
1601 1.1 riastrad }
1602 1.1 riastrad copied++;
1603 1.1 riastrad }
1604 1.1 riastrad }
1605 1.1 riastrad }
1606 1.1 riastrad card_res->count_crtcs = crtc_count;
1607 1.1 riastrad
1608 1.1 riastrad /* Encoders */
1609 1.1 riastrad if (card_res->count_encoders >= encoder_count) {
1610 1.1 riastrad copied = 0;
1611 1.1 riastrad encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1612 1.1.1.2 riastrad if (!mode_group) {
1613 1.1 riastrad list_for_each_entry(encoder,
1614 1.1 riastrad &dev->mode_config.encoder_list,
1615 1.1 riastrad head) {
1616 1.1 riastrad DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1617 1.1 riastrad drm_get_encoder_name(encoder));
1618 1.1 riastrad if (put_user(encoder->base.id, encoder_id +
1619 1.1 riastrad copied)) {
1620 1.1 riastrad ret = -EFAULT;
1621 1.1 riastrad goto out;
1622 1.1 riastrad }
1623 1.1 riastrad copied++;
1624 1.1 riastrad }
1625 1.1 riastrad } else {
1626 1.1 riastrad for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1627 1.1 riastrad if (put_user(mode_group->id_list[i],
1628 1.1 riastrad encoder_id + copied)) {
1629 1.1 riastrad ret = -EFAULT;
1630 1.1 riastrad goto out;
1631 1.1 riastrad }
1632 1.1 riastrad copied++;
1633 1.1 riastrad }
1634 1.1 riastrad
1635 1.1 riastrad }
1636 1.1 riastrad }
1637 1.1 riastrad card_res->count_encoders = encoder_count;
1638 1.1 riastrad
1639 1.1 riastrad /* Connectors */
1640 1.1 riastrad if (card_res->count_connectors >= connector_count) {
1641 1.1 riastrad copied = 0;
1642 1.1 riastrad connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1643 1.1.1.2 riastrad if (!mode_group) {
1644 1.1 riastrad list_for_each_entry(connector,
1645 1.1 riastrad &dev->mode_config.connector_list,
1646 1.1 riastrad head) {
1647 1.1 riastrad DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1648 1.1 riastrad connector->base.id,
1649 1.1 riastrad drm_get_connector_name(connector));
1650 1.1 riastrad if (put_user(connector->base.id,
1651 1.1 riastrad connector_id + copied)) {
1652 1.1 riastrad ret = -EFAULT;
1653 1.1 riastrad goto out;
1654 1.1 riastrad }
1655 1.1 riastrad copied++;
1656 1.1 riastrad }
1657 1.1 riastrad } else {
1658 1.1 riastrad int start = mode_group->num_crtcs +
1659 1.1 riastrad mode_group->num_encoders;
1660 1.1 riastrad for (i = start; i < start + mode_group->num_connectors; i++) {
1661 1.1 riastrad if (put_user(mode_group->id_list[i],
1662 1.1 riastrad connector_id + copied)) {
1663 1.1 riastrad ret = -EFAULT;
1664 1.1 riastrad goto out;
1665 1.1 riastrad }
1666 1.1 riastrad copied++;
1667 1.1 riastrad }
1668 1.1 riastrad }
1669 1.1 riastrad }
1670 1.1 riastrad card_res->count_connectors = connector_count;
1671 1.1 riastrad
1672 1.1 riastrad DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
1673 1.1 riastrad card_res->count_connectors, card_res->count_encoders);
1674 1.1 riastrad
1675 1.1 riastrad out:
1676 1.1.1.2 riastrad drm_modeset_unlock_all(dev);
1677 1.1 riastrad return ret;
1678 1.1 riastrad }
1679 1.1 riastrad
1680 1.1 riastrad /**
1681 1.1 riastrad * drm_mode_getcrtc - get CRTC configuration
1682 1.1.1.2 riastrad * @dev: drm device for the ioctl
1683 1.1.1.2 riastrad * @data: data pointer for the ioctl
1684 1.1.1.2 riastrad * @file_priv: drm file for the ioctl call
1685 1.1 riastrad *
1686 1.1 riastrad * Construct a CRTC configuration structure to return to the user.
1687 1.1 riastrad *
1688 1.1 riastrad * Called by the user via ioctl.
1689 1.1 riastrad *
1690 1.1.1.2 riastrad * Returns:
1691 1.1 riastrad * Zero on success, errno on failure.
1692 1.1 riastrad */
1693 1.1 riastrad int drm_mode_getcrtc(struct drm_device *dev,
1694 1.1 riastrad void *data, struct drm_file *file_priv)
1695 1.1 riastrad {
1696 1.1 riastrad struct drm_mode_crtc *crtc_resp = data;
1697 1.1 riastrad struct drm_crtc *crtc;
1698 1.1 riastrad struct drm_mode_object *obj;
1699 1.1 riastrad int ret = 0;
1700 1.1 riastrad
1701 1.1 riastrad if (!drm_core_check_feature(dev, DRIVER_MODESET))
1702 1.1 riastrad return -EINVAL;
1703 1.1 riastrad
1704 1.1.1.2 riastrad drm_modeset_lock_all(dev);
1705 1.1 riastrad
1706 1.1 riastrad obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
1707 1.1 riastrad DRM_MODE_OBJECT_CRTC);
1708 1.1 riastrad if (!obj) {
1709 1.1.1.2 riastrad ret = -ENOENT;
1710 1.1 riastrad goto out;
1711 1.1 riastrad }
1712 1.1 riastrad crtc = obj_to_crtc(obj);
1713 1.1 riastrad
1714 1.1 riastrad crtc_resp->x = crtc->x;
1715 1.1 riastrad crtc_resp->y = crtc->y;
1716 1.1 riastrad crtc_resp->gamma_size = crtc->gamma_size;
1717 1.1.1.2 riastrad if (crtc->primary->fb)
1718 1.1.1.2 riastrad crtc_resp->fb_id = crtc->primary->fb->base.id;
1719 1.1 riastrad else
1720 1.1 riastrad crtc_resp->fb_id = 0;
1721 1.1 riastrad
1722 1.1 riastrad if (crtc->enabled) {
1723 1.1 riastrad
1724 1.1 riastrad drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1725 1.1 riastrad crtc_resp->mode_valid = 1;
1726 1.1 riastrad
1727 1.1 riastrad } else {
1728 1.1 riastrad crtc_resp->mode_valid = 0;
1729 1.1 riastrad }
1730 1.1 riastrad
1731 1.1 riastrad out:
1732 1.1.1.2 riastrad drm_modeset_unlock_all(dev);
1733 1.1 riastrad return ret;
1734 1.1 riastrad }
1735 1.1 riastrad
1736 1.1.1.2 riastrad static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
1737 1.1.1.2 riastrad const struct drm_file *file_priv)
1738 1.1.1.2 riastrad {
1739 1.1.1.2 riastrad /*
1740 1.1.1.2 riastrad * If user-space hasn't configured the driver to expose the stereo 3D
1741 1.1.1.2 riastrad * modes, don't expose them.
1742 1.1.1.2 riastrad */
1743 1.1.1.2 riastrad if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
1744 1.1.1.2 riastrad return false;
1745 1.1.1.2 riastrad
1746 1.1.1.2 riastrad return true;
1747 1.1.1.2 riastrad }
1748 1.1.1.2 riastrad
1749 1.1 riastrad /**
1750 1.1 riastrad * drm_mode_getconnector - get connector configuration
1751 1.1.1.2 riastrad * @dev: drm device for the ioctl
1752 1.1.1.2 riastrad * @data: data pointer for the ioctl
1753 1.1.1.2 riastrad * @file_priv: drm file for the ioctl call
1754 1.1 riastrad *
1755 1.1 riastrad * Construct a connector configuration structure to return to the user.
1756 1.1 riastrad *
1757 1.1 riastrad * Called by the user via ioctl.
1758 1.1 riastrad *
1759 1.1.1.2 riastrad * Returns:
1760 1.1 riastrad * Zero on success, errno on failure.
1761 1.1 riastrad */
1762 1.1 riastrad int drm_mode_getconnector(struct drm_device *dev, void *data,
1763 1.1 riastrad struct drm_file *file_priv)
1764 1.1 riastrad {
1765 1.1 riastrad struct drm_mode_get_connector *out_resp = data;
1766 1.1 riastrad struct drm_mode_object *obj;
1767 1.1 riastrad struct drm_connector *connector;
1768 1.1 riastrad struct drm_display_mode *mode;
1769 1.1 riastrad int mode_count = 0;
1770 1.1 riastrad int props_count = 0;
1771 1.1 riastrad int encoders_count = 0;
1772 1.1 riastrad int ret = 0;
1773 1.1 riastrad int copied = 0;
1774 1.1 riastrad int i;
1775 1.1 riastrad struct drm_mode_modeinfo u_mode;
1776 1.1 riastrad struct drm_mode_modeinfo __user *mode_ptr;
1777 1.1 riastrad uint32_t __user *prop_ptr;
1778 1.1 riastrad uint64_t __user *prop_values;
1779 1.1 riastrad uint32_t __user *encoder_ptr;
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 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1785 1.1 riastrad
1786 1.1 riastrad DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
1787 1.1 riastrad
1788 1.1 riastrad mutex_lock(&dev->mode_config.mutex);
1789 1.1 riastrad
1790 1.1 riastrad obj = drm_mode_object_find(dev, out_resp->connector_id,
1791 1.1 riastrad DRM_MODE_OBJECT_CONNECTOR);
1792 1.1 riastrad if (!obj) {
1793 1.1.1.2 riastrad ret = -ENOENT;
1794 1.1 riastrad goto out;
1795 1.1 riastrad }
1796 1.1 riastrad connector = obj_to_connector(obj);
1797 1.1 riastrad
1798 1.1 riastrad props_count = connector->properties.count;
1799 1.1 riastrad
1800 1.1 riastrad for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1801 1.1 riastrad if (connector->encoder_ids[i] != 0) {
1802 1.1 riastrad encoders_count++;
1803 1.1 riastrad }
1804 1.1 riastrad }
1805 1.1 riastrad
1806 1.1 riastrad if (out_resp->count_modes == 0) {
1807 1.1 riastrad connector->funcs->fill_modes(connector,
1808 1.1 riastrad dev->mode_config.max_width,
1809 1.1 riastrad dev->mode_config.max_height);
1810 1.1 riastrad }
1811 1.1 riastrad
1812 1.1 riastrad /* delayed so we get modes regardless of pre-fill_modes state */
1813 1.1 riastrad list_for_each_entry(mode, &connector->modes, head)
1814 1.1.1.2 riastrad if (drm_mode_expose_to_userspace(mode, file_priv))
1815 1.1.1.2 riastrad mode_count++;
1816 1.1 riastrad
1817 1.1 riastrad out_resp->connector_id = connector->base.id;
1818 1.1 riastrad out_resp->connector_type = connector->connector_type;
1819 1.1 riastrad out_resp->connector_type_id = connector->connector_type_id;
1820 1.1 riastrad out_resp->mm_width = connector->display_info.width_mm;
1821 1.1 riastrad out_resp->mm_height = connector->display_info.height_mm;
1822 1.1 riastrad out_resp->subpixel = connector->display_info.subpixel_order;
1823 1.1 riastrad out_resp->connection = connector->status;
1824 1.1 riastrad if (connector->encoder)
1825 1.1 riastrad out_resp->encoder_id = connector->encoder->base.id;
1826 1.1 riastrad else
1827 1.1 riastrad out_resp->encoder_id = 0;
1828 1.1 riastrad
1829 1.1 riastrad /*
1830 1.1 riastrad * This ioctl is called twice, once to determine how much space is
1831 1.1 riastrad * needed, and the 2nd time to fill it.
1832 1.1 riastrad */
1833 1.1 riastrad if ((out_resp->count_modes >= mode_count) && mode_count) {
1834 1.1 riastrad copied = 0;
1835 1.1 riastrad mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
1836 1.1 riastrad list_for_each_entry(mode, &connector->modes, head) {
1837 1.1.1.2 riastrad if (!drm_mode_expose_to_userspace(mode, file_priv))
1838 1.1.1.2 riastrad continue;
1839 1.1.1.2 riastrad
1840 1.1 riastrad drm_crtc_convert_to_umode(&u_mode, mode);
1841 1.1 riastrad if (copy_to_user(mode_ptr + copied,
1842 1.1 riastrad &u_mode, sizeof(u_mode))) {
1843 1.1 riastrad ret = -EFAULT;
1844 1.1 riastrad goto out;
1845 1.1 riastrad }
1846 1.1 riastrad copied++;
1847 1.1 riastrad }
1848 1.1 riastrad }
1849 1.1 riastrad out_resp->count_modes = mode_count;
1850 1.1 riastrad
1851 1.1 riastrad if ((out_resp->count_props >= props_count) && props_count) {
1852 1.1 riastrad copied = 0;
1853 1.1 riastrad prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
1854 1.1 riastrad prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
1855 1.1 riastrad for (i = 0; i < connector->properties.count; i++) {
1856 1.1 riastrad if (put_user(connector->properties.ids[i],
1857 1.1 riastrad prop_ptr + copied)) {
1858 1.1 riastrad ret = -EFAULT;
1859 1.1 riastrad goto out;
1860 1.1 riastrad }
1861 1.1 riastrad
1862 1.1 riastrad if (put_user(connector->properties.values[i],
1863 1.1 riastrad prop_values + copied)) {
1864 1.1 riastrad ret = -EFAULT;
1865 1.1 riastrad goto out;
1866 1.1 riastrad }
1867 1.1 riastrad copied++;
1868 1.1 riastrad }
1869 1.1 riastrad }
1870 1.1 riastrad out_resp->count_props = props_count;
1871 1.1 riastrad
1872 1.1 riastrad if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1873 1.1 riastrad copied = 0;
1874 1.1 riastrad encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
1875 1.1 riastrad for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1876 1.1 riastrad if (connector->encoder_ids[i] != 0) {
1877 1.1 riastrad if (put_user(connector->encoder_ids[i],
1878 1.1 riastrad encoder_ptr + copied)) {
1879 1.1 riastrad ret = -EFAULT;
1880 1.1 riastrad goto out;
1881 1.1 riastrad }
1882 1.1 riastrad copied++;
1883 1.1 riastrad }
1884 1.1 riastrad }
1885 1.1 riastrad }
1886 1.1 riastrad out_resp->count_encoders = encoders_count;
1887 1.1 riastrad
1888 1.1 riastrad out:
1889 1.1 riastrad mutex_unlock(&dev->mode_config.mutex);
1890 1.1.1.2 riastrad
1891 1.1 riastrad return ret;
1892 1.1 riastrad }
1893 1.1 riastrad
1894 1.1.1.2 riastrad /**
1895 1.1.1.2 riastrad * drm_mode_getencoder - get encoder configuration
1896 1.1.1.2 riastrad * @dev: drm device for the ioctl
1897 1.1.1.2 riastrad * @data: data pointer for the ioctl
1898 1.1.1.2 riastrad * @file_priv: drm file for the ioctl call
1899 1.1.1.2 riastrad *
1900 1.1.1.2 riastrad * Construct a encoder configuration structure to return to the user.
1901 1.1.1.2 riastrad *
1902 1.1.1.2 riastrad * Called by the user via ioctl.
1903 1.1.1.2 riastrad *
1904 1.1.1.2 riastrad * Returns:
1905 1.1.1.2 riastrad * Zero on success, errno on failure.
1906 1.1.1.2 riastrad */
1907 1.1 riastrad int drm_mode_getencoder(struct drm_device *dev, void *data,
1908 1.1 riastrad struct drm_file *file_priv)
1909 1.1 riastrad {
1910 1.1 riastrad struct drm_mode_get_encoder *enc_resp = data;
1911 1.1 riastrad struct drm_mode_object *obj;
1912 1.1 riastrad struct drm_encoder *encoder;
1913 1.1 riastrad int ret = 0;
1914 1.1 riastrad
1915 1.1 riastrad if (!drm_core_check_feature(dev, DRIVER_MODESET))
1916 1.1 riastrad return -EINVAL;
1917 1.1 riastrad
1918 1.1.1.2 riastrad drm_modeset_lock_all(dev);
1919 1.1 riastrad obj = drm_mode_object_find(dev, enc_resp->encoder_id,
1920 1.1 riastrad DRM_MODE_OBJECT_ENCODER);
1921 1.1 riastrad if (!obj) {
1922 1.1.1.2 riastrad ret = -ENOENT;
1923 1.1 riastrad goto out;
1924 1.1 riastrad }
1925 1.1 riastrad encoder = obj_to_encoder(obj);
1926 1.1 riastrad
1927 1.1 riastrad if (encoder->crtc)
1928 1.1 riastrad enc_resp->crtc_id = encoder->crtc->base.id;
1929 1.1 riastrad else
1930 1.1 riastrad enc_resp->crtc_id = 0;
1931 1.1 riastrad enc_resp->encoder_type = encoder->encoder_type;
1932 1.1 riastrad enc_resp->encoder_id = encoder->base.id;
1933 1.1 riastrad enc_resp->possible_crtcs = encoder->possible_crtcs;
1934 1.1 riastrad enc_resp->possible_clones = encoder->possible_clones;
1935 1.1 riastrad
1936 1.1 riastrad out:
1937 1.1.1.2 riastrad drm_modeset_unlock_all(dev);
1938 1.1 riastrad return ret;
1939 1.1 riastrad }
1940 1.1 riastrad
1941 1.1 riastrad /**
1942 1.1.1.2 riastrad * drm_mode_getplane_res - enumerate all plane resources
1943 1.1 riastrad * @dev: DRM device
1944 1.1 riastrad * @data: ioctl data
1945 1.1 riastrad * @file_priv: DRM file info
1946 1.1 riastrad *
1947 1.1.1.2 riastrad * Construct a list of plane ids to return to the user.
1948 1.1.1.2 riastrad *
1949 1.1.1.2 riastrad * Called by the user via ioctl.
1950 1.1 riastrad *
1951 1.1.1.2 riastrad * Returns:
1952 1.1.1.2 riastrad * Zero on success, errno on failure.
1953 1.1 riastrad */
1954 1.1 riastrad int drm_mode_getplane_res(struct drm_device *dev, void *data,
1955 1.1.1.2 riastrad struct drm_file *file_priv)
1956 1.1 riastrad {
1957 1.1 riastrad struct drm_mode_get_plane_res *plane_resp = data;
1958 1.1 riastrad struct drm_mode_config *config;
1959 1.1 riastrad struct drm_plane *plane;
1960 1.1 riastrad uint32_t __user *plane_ptr;
1961 1.1 riastrad int copied = 0, ret = 0;
1962 1.1.1.2 riastrad unsigned num_planes;
1963 1.1 riastrad
1964 1.1 riastrad if (!drm_core_check_feature(dev, DRIVER_MODESET))
1965 1.1 riastrad return -EINVAL;
1966 1.1 riastrad
1967 1.1.1.2 riastrad drm_modeset_lock_all(dev);
1968 1.1 riastrad config = &dev->mode_config;
1969 1.1 riastrad
1970 1.1.1.2 riastrad if (file_priv->universal_planes)
1971 1.1.1.2 riastrad num_planes = config->num_total_plane;
1972 1.1.1.2 riastrad else
1973 1.1.1.2 riastrad num_planes = config->num_overlay_plane;
1974 1.1.1.2 riastrad
1975 1.1 riastrad /*
1976 1.1 riastrad * This ioctl is called twice, once to determine how much space is
1977 1.1 riastrad * needed, and the 2nd time to fill it.
1978 1.1 riastrad */
1979 1.1.1.2 riastrad if (num_planes &&
1980 1.1.1.2 riastrad (plane_resp->count_planes >= num_planes)) {
1981 1.1 riastrad plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
1982 1.1 riastrad
1983 1.1 riastrad list_for_each_entry(plane, &config->plane_list, head) {
1984 1.1.1.2 riastrad /*
1985 1.1.1.2 riastrad * Unless userspace set the 'universal planes'
1986 1.1.1.2 riastrad * capability bit, only advertise overlays.
1987 1.1.1.2 riastrad */
1988 1.1.1.2 riastrad if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
1989 1.1.1.2 riastrad !file_priv->universal_planes)
1990 1.1.1.2 riastrad continue;
1991 1.1.1.2 riastrad
1992 1.1 riastrad if (put_user(plane->base.id, plane_ptr + copied)) {
1993 1.1 riastrad ret = -EFAULT;
1994 1.1 riastrad goto out;
1995 1.1 riastrad }
1996 1.1 riastrad copied++;
1997 1.1 riastrad }
1998 1.1 riastrad }
1999 1.1.1.2 riastrad plane_resp->count_planes = num_planes;
2000 1.1 riastrad
2001 1.1 riastrad out:
2002 1.1.1.2 riastrad drm_modeset_unlock_all(dev);
2003 1.1 riastrad return ret;
2004 1.1 riastrad }
2005 1.1 riastrad
2006 1.1 riastrad /**
2007 1.1.1.2 riastrad * drm_mode_getplane - get plane configuration
2008 1.1 riastrad * @dev: DRM device
2009 1.1 riastrad * @data: ioctl data
2010 1.1 riastrad * @file_priv: DRM file info
2011 1.1 riastrad *
2012 1.1.1.2 riastrad * Construct a plane configuration structure to return to the user.
2013 1.1.1.2 riastrad *
2014 1.1.1.2 riastrad * Called by the user via ioctl.
2015 1.1 riastrad *
2016 1.1.1.2 riastrad * Returns:
2017 1.1.1.2 riastrad * Zero on success, errno on failure.
2018 1.1 riastrad */
2019 1.1 riastrad int drm_mode_getplane(struct drm_device *dev, void *data,
2020 1.1.1.2 riastrad struct drm_file *file_priv)
2021 1.1 riastrad {
2022 1.1 riastrad struct drm_mode_get_plane *plane_resp = data;
2023 1.1 riastrad struct drm_mode_object *obj;
2024 1.1 riastrad struct drm_plane *plane;
2025 1.1 riastrad uint32_t __user *format_ptr;
2026 1.1 riastrad int ret = 0;
2027 1.1 riastrad
2028 1.1 riastrad if (!drm_core_check_feature(dev, DRIVER_MODESET))
2029 1.1 riastrad return -EINVAL;
2030 1.1 riastrad
2031 1.1.1.2 riastrad drm_modeset_lock_all(dev);
2032 1.1 riastrad obj = drm_mode_object_find(dev, plane_resp->plane_id,
2033 1.1 riastrad DRM_MODE_OBJECT_PLANE);
2034 1.1 riastrad if (!obj) {
2035 1.1 riastrad ret = -ENOENT;
2036 1.1 riastrad goto out;
2037 1.1 riastrad }
2038 1.1 riastrad plane = obj_to_plane(obj);
2039 1.1 riastrad
2040 1.1 riastrad if (plane->crtc)
2041 1.1 riastrad plane_resp->crtc_id = plane->crtc->base.id;
2042 1.1 riastrad else
2043 1.1 riastrad plane_resp->crtc_id = 0;
2044 1.1 riastrad
2045 1.1 riastrad if (plane->fb)
2046 1.1 riastrad plane_resp->fb_id = plane->fb->base.id;
2047 1.1 riastrad else
2048 1.1 riastrad plane_resp->fb_id = 0;
2049 1.1 riastrad
2050 1.1 riastrad plane_resp->plane_id = plane->base.id;
2051 1.1 riastrad plane_resp->possible_crtcs = plane->possible_crtcs;
2052 1.1.1.2 riastrad plane_resp->gamma_size = 0;
2053 1.1 riastrad
2054 1.1 riastrad /*
2055 1.1 riastrad * This ioctl is called twice, once to determine how much space is
2056 1.1 riastrad * needed, and the 2nd time to fill it.
2057 1.1 riastrad */
2058 1.1 riastrad if (plane->format_count &&
2059 1.1 riastrad (plane_resp->count_format_types >= plane->format_count)) {
2060 1.1 riastrad format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
2061 1.1 riastrad if (copy_to_user(format_ptr,
2062 1.1 riastrad plane->format_types,
2063 1.1 riastrad sizeof(uint32_t) * plane->format_count)) {
2064 1.1 riastrad ret = -EFAULT;
2065 1.1 riastrad goto out;
2066 1.1 riastrad }
2067 1.1 riastrad }
2068 1.1 riastrad plane_resp->count_format_types = plane->format_count;
2069 1.1 riastrad
2070 1.1 riastrad out:
2071 1.1.1.2 riastrad drm_modeset_unlock_all(dev);
2072 1.1 riastrad return ret;
2073 1.1 riastrad }
2074 1.1 riastrad
2075 1.1 riastrad /**
2076 1.1.1.2 riastrad * drm_mode_setplane - configure a plane's configuration
2077 1.1 riastrad * @dev: DRM device
2078 1.1 riastrad * @data: ioctl data*
2079 1.1.1.2 riastrad * @file_priv: DRM file info
2080 1.1 riastrad *
2081 1.1.1.2 riastrad * Set plane configuration, including placement, fb, scaling, and other factors.
2082 1.1 riastrad * Or pass a NULL fb to disable.
2083 1.1.1.2 riastrad *
2084 1.1.1.2 riastrad * Returns:
2085 1.1.1.2 riastrad * Zero on success, errno on failure.
2086 1.1 riastrad */
2087 1.1 riastrad int drm_mode_setplane(struct drm_device *dev, void *data,
2088 1.1.1.2 riastrad struct drm_file *file_priv)
2089 1.1 riastrad {
2090 1.1 riastrad struct drm_mode_set_plane *plane_req = data;
2091 1.1 riastrad struct drm_mode_object *obj;
2092 1.1 riastrad struct drm_plane *plane;
2093 1.1 riastrad struct drm_crtc *crtc;
2094 1.1.1.2 riastrad struct drm_framebuffer *fb = NULL, *old_fb = NULL;
2095 1.1 riastrad int ret = 0;
2096 1.1 riastrad unsigned int fb_width, fb_height;
2097 1.1 riastrad int i;
2098 1.1 riastrad
2099 1.1 riastrad if (!drm_core_check_feature(dev, DRIVER_MODESET))
2100 1.1 riastrad return -EINVAL;
2101 1.1 riastrad
2102 1.1 riastrad /*
2103 1.1 riastrad * First, find the plane, crtc, and fb objects. If not available,
2104 1.1 riastrad * we don't bother to call the driver.
2105 1.1 riastrad */
2106 1.1 riastrad obj = drm_mode_object_find(dev, plane_req->plane_id,
2107 1.1 riastrad DRM_MODE_OBJECT_PLANE);
2108 1.1 riastrad if (!obj) {
2109 1.1 riastrad DRM_DEBUG_KMS("Unknown plane ID %d\n",
2110 1.1 riastrad plane_req->plane_id);
2111 1.1.1.2 riastrad return -ENOENT;
2112 1.1 riastrad }
2113 1.1 riastrad plane = obj_to_plane(obj);
2114 1.1 riastrad
2115 1.1 riastrad /* No fb means shut it down */
2116 1.1 riastrad if (!plane_req->fb_id) {
2117 1.1.1.2 riastrad drm_modeset_lock_all(dev);
2118 1.1.1.2 riastrad old_fb = plane->fb;
2119 1.1 riastrad plane->funcs->disable_plane(plane);
2120 1.1 riastrad plane->crtc = NULL;
2121 1.1 riastrad plane->fb = NULL;
2122 1.1.1.2 riastrad drm_modeset_unlock_all(dev);
2123 1.1 riastrad goto out;
2124 1.1 riastrad }
2125 1.1 riastrad
2126 1.1 riastrad obj = drm_mode_object_find(dev, plane_req->crtc_id,
2127 1.1 riastrad DRM_MODE_OBJECT_CRTC);
2128 1.1 riastrad if (!obj) {
2129 1.1 riastrad DRM_DEBUG_KMS("Unknown crtc ID %d\n",
2130 1.1 riastrad plane_req->crtc_id);
2131 1.1 riastrad ret = -ENOENT;
2132 1.1 riastrad goto out;
2133 1.1 riastrad }
2134 1.1 riastrad crtc = obj_to_crtc(obj);
2135 1.1 riastrad
2136 1.1.1.2 riastrad fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
2137 1.1.1.2 riastrad if (!fb) {
2138 1.1 riastrad DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
2139 1.1 riastrad plane_req->fb_id);
2140 1.1 riastrad ret = -ENOENT;
2141 1.1 riastrad goto out;
2142 1.1 riastrad }
2143 1.1 riastrad
2144 1.1 riastrad /* Check whether this plane supports the fb pixel format. */
2145 1.1 riastrad for (i = 0; i < plane->format_count; i++)
2146 1.1 riastrad if (fb->pixel_format == plane->format_types[i])
2147 1.1 riastrad break;
2148 1.1 riastrad if (i == plane->format_count) {
2149 1.1.1.2 riastrad DRM_DEBUG_KMS("Invalid pixel format %s\n",
2150 1.1.1.2 riastrad drm_get_format_name(fb->pixel_format));
2151 1.1 riastrad ret = -EINVAL;
2152 1.1 riastrad goto out;
2153 1.1 riastrad }
2154 1.1 riastrad
2155 1.1 riastrad fb_width = fb->width << 16;
2156 1.1 riastrad fb_height = fb->height << 16;
2157 1.1 riastrad
2158 1.1 riastrad /* Make sure source coordinates are inside the fb. */
2159 1.1 riastrad if (plane_req->src_w > fb_width ||
2160 1.1 riastrad plane_req->src_x > fb_width - plane_req->src_w ||
2161 1.1 riastrad plane_req->src_h > fb_height ||
2162 1.1 riastrad plane_req->src_y > fb_height - plane_req->src_h) {
2163 1.1 riastrad DRM_DEBUG_KMS("Invalid source coordinates "
2164 1.1 riastrad "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
2165 1.1 riastrad plane_req->src_w >> 16,
2166 1.1 riastrad ((plane_req->src_w & 0xffff) * 15625) >> 10,
2167 1.1 riastrad plane_req->src_h >> 16,
2168 1.1 riastrad ((plane_req->src_h & 0xffff) * 15625) >> 10,
2169 1.1 riastrad plane_req->src_x >> 16,
2170 1.1 riastrad ((plane_req->src_x & 0xffff) * 15625) >> 10,
2171 1.1 riastrad plane_req->src_y >> 16,
2172 1.1 riastrad ((plane_req->src_y & 0xffff) * 15625) >> 10);
2173 1.1 riastrad ret = -ENOSPC;
2174 1.1 riastrad goto out;
2175 1.1 riastrad }
2176 1.1 riastrad
2177 1.1 riastrad /* Give drivers some help against integer overflows */
2178 1.1 riastrad if (plane_req->crtc_w > INT_MAX ||
2179 1.1 riastrad plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
2180 1.1 riastrad plane_req->crtc_h > INT_MAX ||
2181 1.1 riastrad plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
2182 1.1 riastrad DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
2183 1.1 riastrad plane_req->crtc_w, plane_req->crtc_h,
2184 1.1 riastrad plane_req->crtc_x, plane_req->crtc_y);
2185 1.1 riastrad ret = -ERANGE;
2186 1.1 riastrad goto out;
2187 1.1 riastrad }
2188 1.1 riastrad
2189 1.1.1.2 riastrad drm_modeset_lock_all(dev);
2190 1.1 riastrad ret = plane->funcs->update_plane(plane, crtc, fb,
2191 1.1 riastrad plane_req->crtc_x, plane_req->crtc_y,
2192 1.1 riastrad plane_req->crtc_w, plane_req->crtc_h,
2193 1.1 riastrad plane_req->src_x, plane_req->src_y,
2194 1.1 riastrad plane_req->src_w, plane_req->src_h);
2195 1.1 riastrad if (!ret) {
2196 1.1.1.2 riastrad old_fb = plane->fb;
2197 1.1 riastrad plane->crtc = crtc;
2198 1.1 riastrad plane->fb = fb;
2199 1.1.1.2 riastrad fb = NULL;
2200 1.1 riastrad }
2201 1.1.1.2 riastrad drm_modeset_unlock_all(dev);
2202 1.1 riastrad
2203 1.1 riastrad out:
2204 1.1.1.2 riastrad if (fb)
2205 1.1.1.2 riastrad drm_framebuffer_unreference(fb);
2206 1.1.1.2 riastrad if (old_fb)
2207 1.1.1.2 riastrad drm_framebuffer_unreference(old_fb);
2208 1.1 riastrad
2209 1.1 riastrad return ret;
2210 1.1 riastrad }
2211 1.1 riastrad
2212 1.1 riastrad /**
2213 1.1.1.2 riastrad * drm_mode_set_config_internal - helper to call ->set_config
2214 1.1.1.2 riastrad * @set: modeset config to set
2215 1.1 riastrad *
2216 1.1.1.2 riastrad * This is a little helper to wrap internal calls to the ->set_config driver
2217 1.1.1.2 riastrad * interface. The only thing it adds is correct refcounting dance.
2218 1.1.1.2 riastrad *
2219 1.1.1.2 riastrad * Returns:
2220 1.1.1.2 riastrad * Zero on success, errno on failure.
2221 1.1.1.2 riastrad */
2222 1.1.1.2 riastrad int drm_mode_set_config_internal(struct drm_mode_set *set)
2223 1.1.1.2 riastrad {
2224 1.1.1.2 riastrad struct drm_crtc *crtc = set->crtc;
2225 1.1.1.2 riastrad struct drm_framebuffer *fb;
2226 1.1.1.2 riastrad struct drm_crtc *tmp;
2227 1.1.1.2 riastrad int ret;
2228 1.1.1.2 riastrad
2229 1.1.1.2 riastrad /*
2230 1.1.1.2 riastrad * NOTE: ->set_config can also disable other crtcs (if we steal all
2231 1.1.1.2 riastrad * connectors from it), hence we need to refcount the fbs across all
2232 1.1.1.2 riastrad * crtcs. Atomic modeset will have saner semantics ...
2233 1.1.1.2 riastrad */
2234 1.1.1.2 riastrad list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head)
2235 1.1.1.2 riastrad tmp->old_fb = tmp->primary->fb;
2236 1.1.1.2 riastrad
2237 1.1.1.2 riastrad fb = set->fb;
2238 1.1.1.2 riastrad
2239 1.1.1.2 riastrad ret = crtc->funcs->set_config(set);
2240 1.1.1.2 riastrad if (ret == 0) {
2241 1.1.1.2 riastrad crtc->primary->crtc = crtc;
2242 1.1.1.2 riastrad
2243 1.1.1.2 riastrad /* crtc->fb must be updated by ->set_config, enforces this. */
2244 1.1.1.2 riastrad WARN_ON(fb != crtc->primary->fb);
2245 1.1.1.2 riastrad }
2246 1.1.1.2 riastrad
2247 1.1.1.2 riastrad list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
2248 1.1.1.2 riastrad if (tmp->primary->fb)
2249 1.1.1.2 riastrad drm_framebuffer_reference(tmp->primary->fb);
2250 1.1.1.2 riastrad if (tmp->old_fb)
2251 1.1.1.2 riastrad drm_framebuffer_unreference(tmp->old_fb);
2252 1.1.1.2 riastrad }
2253 1.1.1.2 riastrad
2254 1.1.1.2 riastrad return ret;
2255 1.1.1.2 riastrad }
2256 1.1.1.2 riastrad EXPORT_SYMBOL(drm_mode_set_config_internal);
2257 1.1.1.2 riastrad
2258 1.1.1.2 riastrad /**
2259 1.1.1.2 riastrad * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
2260 1.1.1.2 riastrad * CRTC viewport
2261 1.1.1.2 riastrad * @crtc: CRTC that framebuffer will be displayed on
2262 1.1.1.2 riastrad * @x: x panning
2263 1.1.1.2 riastrad * @y: y panning
2264 1.1.1.2 riastrad * @mode: mode that framebuffer will be displayed under
2265 1.1.1.2 riastrad * @fb: framebuffer to check size of
2266 1.1.1.2 riastrad */
2267 1.1.1.2 riastrad int drm_crtc_check_viewport(const struct drm_crtc *crtc,
2268 1.1.1.2 riastrad int x, int y,
2269 1.1.1.2 riastrad const struct drm_display_mode *mode,
2270 1.1.1.2 riastrad const struct drm_framebuffer *fb)
2271 1.1.1.2 riastrad
2272 1.1.1.2 riastrad {
2273 1.1.1.2 riastrad int hdisplay, vdisplay;
2274 1.1.1.2 riastrad
2275 1.1.1.2 riastrad hdisplay = mode->hdisplay;
2276 1.1.1.2 riastrad vdisplay = mode->vdisplay;
2277 1.1.1.2 riastrad
2278 1.1.1.2 riastrad if (drm_mode_is_stereo(mode)) {
2279 1.1.1.2 riastrad struct drm_display_mode adjusted = *mode;
2280 1.1.1.2 riastrad
2281 1.1.1.2 riastrad drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE);
2282 1.1.1.2 riastrad hdisplay = adjusted.crtc_hdisplay;
2283 1.1.1.2 riastrad vdisplay = adjusted.crtc_vdisplay;
2284 1.1.1.2 riastrad }
2285 1.1.1.2 riastrad
2286 1.1.1.2 riastrad if (crtc->invert_dimensions)
2287 1.1.1.2 riastrad swap(hdisplay, vdisplay);
2288 1.1.1.2 riastrad
2289 1.1.1.2 riastrad if (hdisplay > fb->width ||
2290 1.1.1.2 riastrad vdisplay > fb->height ||
2291 1.1.1.2 riastrad x > fb->width - hdisplay ||
2292 1.1.1.2 riastrad y > fb->height - vdisplay) {
2293 1.1.1.2 riastrad DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
2294 1.1.1.2 riastrad fb->width, fb->height, hdisplay, vdisplay, x, y,
2295 1.1.1.2 riastrad crtc->invert_dimensions ? " (inverted)" : "");
2296 1.1.1.2 riastrad return -ENOSPC;
2297 1.1.1.2 riastrad }
2298 1.1.1.2 riastrad
2299 1.1.1.2 riastrad return 0;
2300 1.1.1.2 riastrad }
2301 1.1.1.2 riastrad EXPORT_SYMBOL(drm_crtc_check_viewport);
2302 1.1.1.2 riastrad
2303 1.1.1.2 riastrad /**
2304 1.1.1.2 riastrad * drm_mode_setcrtc - set CRTC configuration
2305 1.1.1.2 riastrad * @dev: drm device for the ioctl
2306 1.1.1.2 riastrad * @data: data pointer for the ioctl
2307 1.1.1.2 riastrad * @file_priv: drm file for the ioctl call
2308 1.1 riastrad *
2309 1.1 riastrad * Build a new CRTC configuration based on user request.
2310 1.1 riastrad *
2311 1.1 riastrad * Called by the user via ioctl.
2312 1.1 riastrad *
2313 1.1.1.2 riastrad * Returns:
2314 1.1 riastrad * Zero on success, errno on failure.
2315 1.1 riastrad */
2316 1.1 riastrad int drm_mode_setcrtc(struct drm_device *dev, void *data,
2317 1.1 riastrad struct drm_file *file_priv)
2318 1.1 riastrad {
2319 1.1 riastrad struct drm_mode_config *config = &dev->mode_config;
2320 1.1 riastrad struct drm_mode_crtc *crtc_req = data;
2321 1.1 riastrad struct drm_mode_object *obj;
2322 1.1 riastrad struct drm_crtc *crtc;
2323 1.1 riastrad struct drm_connector **connector_set = NULL, *connector;
2324 1.1 riastrad struct drm_framebuffer *fb = NULL;
2325 1.1 riastrad struct drm_display_mode *mode = NULL;
2326 1.1 riastrad struct drm_mode_set set;
2327 1.1 riastrad uint32_t __user *set_connectors_ptr;
2328 1.1 riastrad int ret;
2329 1.1 riastrad int i;
2330 1.1 riastrad
2331 1.1 riastrad if (!drm_core_check_feature(dev, DRIVER_MODESET))
2332 1.1 riastrad return -EINVAL;
2333 1.1 riastrad
2334 1.1 riastrad /* For some reason crtc x/y offsets are signed internally. */
2335 1.1 riastrad if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
2336 1.1 riastrad return -ERANGE;
2337 1.1 riastrad
2338 1.1.1.2 riastrad drm_modeset_lock_all(dev);
2339 1.1 riastrad obj = drm_mode_object_find(dev, crtc_req->crtc_id,
2340 1.1 riastrad DRM_MODE_OBJECT_CRTC);
2341 1.1 riastrad if (!obj) {
2342 1.1 riastrad DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
2343 1.1.1.2 riastrad ret = -ENOENT;
2344 1.1 riastrad goto out;
2345 1.1 riastrad }
2346 1.1 riastrad crtc = obj_to_crtc(obj);
2347 1.1 riastrad DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
2348 1.1 riastrad
2349 1.1 riastrad if (crtc_req->mode_valid) {
2350 1.1 riastrad /* If we have a mode we need a framebuffer. */
2351 1.1 riastrad /* If we pass -1, set the mode with the currently bound fb */
2352 1.1 riastrad if (crtc_req->fb_id == -1) {
2353 1.1.1.2 riastrad if (!crtc->primary->fb) {
2354 1.1 riastrad DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2355 1.1 riastrad ret = -EINVAL;
2356 1.1 riastrad goto out;
2357 1.1 riastrad }
2358 1.1.1.2 riastrad fb = crtc->primary->fb;
2359 1.1.1.2 riastrad /* Make refcounting symmetric with the lookup path. */
2360 1.1.1.2 riastrad drm_framebuffer_reference(fb);
2361 1.1 riastrad } else {
2362 1.1.1.2 riastrad fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2363 1.1.1.2 riastrad if (!fb) {
2364 1.1 riastrad DRM_DEBUG_KMS("Unknown FB ID%d\n",
2365 1.1 riastrad crtc_req->fb_id);
2366 1.1.1.2 riastrad ret = -ENOENT;
2367 1.1 riastrad goto out;
2368 1.1 riastrad }
2369 1.1 riastrad }
2370 1.1 riastrad
2371 1.1 riastrad mode = drm_mode_create(dev);
2372 1.1 riastrad if (!mode) {
2373 1.1 riastrad ret = -ENOMEM;
2374 1.1 riastrad goto out;
2375 1.1 riastrad }
2376 1.1 riastrad
2377 1.1 riastrad ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
2378 1.1 riastrad if (ret) {
2379 1.1 riastrad DRM_DEBUG_KMS("Invalid mode\n");
2380 1.1 riastrad goto out;
2381 1.1 riastrad }
2382 1.1 riastrad
2383 1.1 riastrad drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
2384 1.1 riastrad
2385 1.1.1.2 riastrad ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
2386 1.1.1.2 riastrad mode, fb);
2387 1.1.1.2 riastrad if (ret)
2388 1.1 riastrad goto out;
2389 1.1.1.2 riastrad
2390 1.1 riastrad }
2391 1.1 riastrad
2392 1.1 riastrad if (crtc_req->count_connectors == 0 && mode) {
2393 1.1 riastrad DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
2394 1.1 riastrad ret = -EINVAL;
2395 1.1 riastrad goto out;
2396 1.1 riastrad }
2397 1.1 riastrad
2398 1.1 riastrad if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
2399 1.1 riastrad DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
2400 1.1 riastrad crtc_req->count_connectors);
2401 1.1 riastrad ret = -EINVAL;
2402 1.1 riastrad goto out;
2403 1.1 riastrad }
2404 1.1 riastrad
2405 1.1 riastrad if (crtc_req->count_connectors > 0) {
2406 1.1 riastrad u32 out_id;
2407 1.1 riastrad
2408 1.1 riastrad /* Avoid unbounded kernel memory allocation */
2409 1.1 riastrad if (crtc_req->count_connectors > config->num_connector) {
2410 1.1 riastrad ret = -EINVAL;
2411 1.1 riastrad goto out;
2412 1.1 riastrad }
2413 1.1 riastrad
2414 1.1 riastrad connector_set = kmalloc(crtc_req->count_connectors *
2415 1.1 riastrad sizeof(struct drm_connector *),
2416 1.1 riastrad GFP_KERNEL);
2417 1.1 riastrad if (!connector_set) {
2418 1.1 riastrad ret = -ENOMEM;
2419 1.1 riastrad goto out;
2420 1.1 riastrad }
2421 1.1 riastrad
2422 1.1 riastrad for (i = 0; i < crtc_req->count_connectors; i++) {
2423 1.1 riastrad set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
2424 1.1 riastrad if (get_user(out_id, &set_connectors_ptr[i])) {
2425 1.1 riastrad ret = -EFAULT;
2426 1.1 riastrad goto out;
2427 1.1 riastrad }
2428 1.1 riastrad
2429 1.1 riastrad obj = drm_mode_object_find(dev, out_id,
2430 1.1 riastrad DRM_MODE_OBJECT_CONNECTOR);
2431 1.1 riastrad if (!obj) {
2432 1.1 riastrad DRM_DEBUG_KMS("Connector id %d unknown\n",
2433 1.1 riastrad out_id);
2434 1.1.1.2 riastrad ret = -ENOENT;
2435 1.1 riastrad goto out;
2436 1.1 riastrad }
2437 1.1 riastrad connector = obj_to_connector(obj);
2438 1.1 riastrad DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2439 1.1 riastrad connector->base.id,
2440 1.1 riastrad drm_get_connector_name(connector));
2441 1.1 riastrad
2442 1.1 riastrad connector_set[i] = connector;
2443 1.1 riastrad }
2444 1.1 riastrad }
2445 1.1 riastrad
2446 1.1 riastrad set.crtc = crtc;
2447 1.1 riastrad set.x = crtc_req->x;
2448 1.1 riastrad set.y = crtc_req->y;
2449 1.1 riastrad set.mode = mode;
2450 1.1 riastrad set.connectors = connector_set;
2451 1.1 riastrad set.num_connectors = crtc_req->count_connectors;
2452 1.1 riastrad set.fb = fb;
2453 1.1.1.2 riastrad ret = drm_mode_set_config_internal(&set);
2454 1.1 riastrad
2455 1.1 riastrad out:
2456 1.1.1.2 riastrad if (fb)
2457 1.1.1.2 riastrad drm_framebuffer_unreference(fb);
2458 1.1.1.2 riastrad
2459 1.1 riastrad kfree(connector_set);
2460 1.1 riastrad drm_mode_destroy(dev, mode);
2461 1.1.1.2 riastrad drm_modeset_unlock_all(dev);
2462 1.1 riastrad return ret;
2463 1.1 riastrad }
2464 1.1 riastrad
2465 1.1.1.2 riastrad static int drm_mode_cursor_common(struct drm_device *dev,
2466 1.1.1.2 riastrad struct drm_mode_cursor2 *req,
2467 1.1.1.2 riastrad struct drm_file *file_priv)
2468 1.1 riastrad {
2469 1.1 riastrad struct drm_mode_object *obj;
2470 1.1 riastrad struct drm_crtc *crtc;
2471 1.1 riastrad int ret = 0;
2472 1.1 riastrad
2473 1.1 riastrad if (!drm_core_check_feature(dev, DRIVER_MODESET))
2474 1.1 riastrad return -EINVAL;
2475 1.1 riastrad
2476 1.1 riastrad if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
2477 1.1 riastrad return -EINVAL;
2478 1.1 riastrad
2479 1.1 riastrad obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
2480 1.1 riastrad if (!obj) {
2481 1.1 riastrad DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
2482 1.1.1.2 riastrad return -ENOENT;
2483 1.1 riastrad }
2484 1.1 riastrad crtc = obj_to_crtc(obj);
2485 1.1 riastrad
2486 1.1.1.2 riastrad mutex_lock(&crtc->mutex);
2487 1.1 riastrad if (req->flags & DRM_MODE_CURSOR_BO) {
2488 1.1.1.2 riastrad if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
2489 1.1 riastrad ret = -ENXIO;
2490 1.1 riastrad goto out;
2491 1.1 riastrad }
2492 1.1 riastrad /* Turns off the cursor if handle is 0 */
2493 1.1.1.2 riastrad if (crtc->funcs->cursor_set2)
2494 1.1.1.2 riastrad ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
2495 1.1.1.2 riastrad req->width, req->height, req->hot_x, req->hot_y);
2496 1.1.1.2 riastrad else
2497 1.1.1.2 riastrad ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2498 1.1.1.2 riastrad req->width, req->height);
2499 1.1 riastrad }
2500 1.1 riastrad
2501 1.1 riastrad if (req->flags & DRM_MODE_CURSOR_MOVE) {
2502 1.1 riastrad if (crtc->funcs->cursor_move) {
2503 1.1 riastrad ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2504 1.1 riastrad } else {
2505 1.1 riastrad ret = -EFAULT;
2506 1.1 riastrad goto out;
2507 1.1 riastrad }
2508 1.1 riastrad }
2509 1.1 riastrad out:
2510 1.1.1.2 riastrad mutex_unlock(&crtc->mutex);
2511 1.1.1.2 riastrad
2512 1.1 riastrad return ret;
2513 1.1.1.2 riastrad
2514 1.1.1.2 riastrad }
2515 1.1.1.2 riastrad
2516 1.1.1.2 riastrad
2517 1.1.1.2 riastrad /**
2518 1.1.1.2 riastrad * drm_mode_cursor_ioctl - set CRTC's cursor configuration
2519 1.1.1.2 riastrad * @dev: drm device for the ioctl
2520 1.1.1.2 riastrad * @data: data pointer for the ioctl
2521 1.1.1.2 riastrad * @file_priv: drm file for the ioctl call
2522 1.1.1.2 riastrad *
2523 1.1.1.2 riastrad * Set the cursor configuration based on user request.
2524 1.1.1.2 riastrad *
2525 1.1.1.2 riastrad * Called by the user via ioctl.
2526 1.1.1.2 riastrad *
2527 1.1.1.2 riastrad * Returns:
2528 1.1.1.2 riastrad * Zero on success, errno on failure.
2529 1.1.1.2 riastrad */
2530 1.1.1.2 riastrad int drm_mode_cursor_ioctl(struct drm_device *dev,
2531 1.1.1.2 riastrad void *data, struct drm_file *file_priv)
2532 1.1.1.2 riastrad {
2533 1.1.1.2 riastrad struct drm_mode_cursor *req = data;
2534 1.1.1.2 riastrad struct drm_mode_cursor2 new_req;
2535 1.1.1.2 riastrad
2536 1.1.1.2 riastrad memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
2537 1.1.1.2 riastrad new_req.hot_x = new_req.hot_y = 0;
2538 1.1.1.2 riastrad
2539 1.1.1.2 riastrad return drm_mode_cursor_common(dev, &new_req, file_priv);
2540 1.1.1.2 riastrad }
2541 1.1.1.2 riastrad
2542 1.1.1.2 riastrad /**
2543 1.1.1.2 riastrad * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
2544 1.1.1.2 riastrad * @dev: drm device for the ioctl
2545 1.1.1.2 riastrad * @data: data pointer for the ioctl
2546 1.1.1.2 riastrad * @file_priv: drm file for the ioctl call
2547 1.1.1.2 riastrad *
2548 1.1.1.2 riastrad * Set the cursor configuration based on user request. This implements the 2nd
2549 1.1.1.2 riastrad * version of the cursor ioctl, which allows userspace to additionally specify
2550 1.1.1.2 riastrad * the hotspot of the pointer.
2551 1.1.1.2 riastrad *
2552 1.1.1.2 riastrad * Called by the user via ioctl.
2553 1.1.1.2 riastrad *
2554 1.1.1.2 riastrad * Returns:
2555 1.1.1.2 riastrad * Zero on success, errno on failure.
2556 1.1.1.2 riastrad */
2557 1.1.1.2 riastrad int drm_mode_cursor2_ioctl(struct drm_device *dev,
2558 1.1.1.2 riastrad void *data, struct drm_file *file_priv)
2559 1.1.1.2 riastrad {
2560 1.1.1.2 riastrad struct drm_mode_cursor2 *req = data;
2561 1.1.1.2 riastrad return drm_mode_cursor_common(dev, req, file_priv);
2562 1.1 riastrad }
2563 1.1 riastrad
2564 1.1.1.2 riastrad /**
2565 1.1.1.2 riastrad * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
2566 1.1.1.2 riastrad * @bpp: bits per pixels
2567 1.1.1.2 riastrad * @depth: bit depth per pixel
2568 1.1.1.2 riastrad *
2569 1.1.1.2 riastrad * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
2570 1.1.1.2 riastrad * Useful in fbdev emulation code, since that deals in those values.
2571 1.1.1.2 riastrad */
2572 1.1 riastrad uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2573 1.1 riastrad {
2574 1.1 riastrad uint32_t fmt;
2575 1.1 riastrad
2576 1.1 riastrad switch (bpp) {
2577 1.1 riastrad case 8:
2578 1.1.1.2 riastrad fmt = DRM_FORMAT_C8;
2579 1.1 riastrad break;
2580 1.1 riastrad case 16:
2581 1.1 riastrad if (depth == 15)
2582 1.1 riastrad fmt = DRM_FORMAT_XRGB1555;
2583 1.1 riastrad else
2584 1.1 riastrad fmt = DRM_FORMAT_RGB565;
2585 1.1 riastrad break;
2586 1.1 riastrad case 24:
2587 1.1 riastrad fmt = DRM_FORMAT_RGB888;
2588 1.1 riastrad break;
2589 1.1 riastrad case 32:
2590 1.1 riastrad if (depth == 24)
2591 1.1 riastrad fmt = DRM_FORMAT_XRGB8888;
2592 1.1 riastrad else if (depth == 30)
2593 1.1 riastrad fmt = DRM_FORMAT_XRGB2101010;
2594 1.1 riastrad else
2595 1.1 riastrad fmt = DRM_FORMAT_ARGB8888;
2596 1.1 riastrad break;
2597 1.1 riastrad default:
2598 1.1 riastrad DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2599 1.1 riastrad fmt = DRM_FORMAT_XRGB8888;
2600 1.1 riastrad break;
2601 1.1 riastrad }
2602 1.1 riastrad
2603 1.1 riastrad return fmt;
2604 1.1 riastrad }
2605 1.1 riastrad EXPORT_SYMBOL(drm_mode_legacy_fb_format);
2606 1.1 riastrad
2607 1.1 riastrad /**
2608 1.1 riastrad * drm_mode_addfb - add an FB to the graphics configuration
2609 1.1.1.2 riastrad * @dev: drm device for the ioctl
2610 1.1.1.2 riastrad * @data: data pointer for the ioctl
2611 1.1.1.2 riastrad * @file_priv: drm file for the ioctl call
2612 1.1 riastrad *
2613 1.1.1.2 riastrad * Add a new FB to the specified CRTC, given a user request. This is the
2614 1.1.1.2 riastrad * original addfb ioclt which only supported RGB formats.
2615 1.1 riastrad *
2616 1.1 riastrad * Called by the user via ioctl.
2617 1.1 riastrad *
2618 1.1.1.2 riastrad * Returns:
2619 1.1 riastrad * Zero on success, errno on failure.
2620 1.1 riastrad */
2621 1.1 riastrad int drm_mode_addfb(struct drm_device *dev,
2622 1.1 riastrad void *data, struct drm_file *file_priv)
2623 1.1 riastrad {
2624 1.1 riastrad struct drm_mode_fb_cmd *or = data;
2625 1.1 riastrad struct drm_mode_fb_cmd2 r = {};
2626 1.1 riastrad struct drm_mode_config *config = &dev->mode_config;
2627 1.1 riastrad struct drm_framebuffer *fb;
2628 1.1 riastrad int ret = 0;
2629 1.1 riastrad
2630 1.1 riastrad /* Use new struct with format internally */
2631 1.1 riastrad r.fb_id = or->fb_id;
2632 1.1 riastrad r.width = or->width;
2633 1.1 riastrad r.height = or->height;
2634 1.1 riastrad r.pitches[0] = or->pitch;
2635 1.1 riastrad r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
2636 1.1 riastrad r.handles[0] = or->handle;
2637 1.1 riastrad
2638 1.1 riastrad if (!drm_core_check_feature(dev, DRIVER_MODESET))
2639 1.1 riastrad return -EINVAL;
2640 1.1 riastrad
2641 1.1 riastrad if ((config->min_width > r.width) || (r.width > config->max_width))
2642 1.1 riastrad return -EINVAL;
2643 1.1 riastrad
2644 1.1 riastrad if ((config->min_height > r.height) || (r.height > config->max_height))
2645 1.1 riastrad return -EINVAL;
2646 1.1 riastrad
2647 1.1 riastrad fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2648 1.1 riastrad if (IS_ERR(fb)) {
2649 1.1 riastrad DRM_DEBUG_KMS("could not create framebuffer\n");
2650 1.1.1.2 riastrad return PTR_ERR(fb);
2651 1.1 riastrad }
2652 1.1 riastrad
2653 1.1.1.2 riastrad mutex_lock(&file_priv->fbs_lock);
2654 1.1 riastrad or->fb_id = fb->base.id;
2655 1.1 riastrad list_add(&fb->filp_head, &file_priv->fbs);
2656 1.1 riastrad DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
2657 1.1.1.2 riastrad mutex_unlock(&file_priv->fbs_lock);
2658 1.1 riastrad
2659 1.1 riastrad return ret;
2660 1.1 riastrad }
2661 1.1 riastrad
2662 1.1 riastrad static int format_check(const struct drm_mode_fb_cmd2 *r)
2663 1.1 riastrad {
2664 1.1 riastrad uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2665 1.1 riastrad
2666 1.1 riastrad switch (format) {
2667 1.1 riastrad case DRM_FORMAT_C8:
2668 1.1 riastrad case DRM_FORMAT_RGB332:
2669 1.1 riastrad case DRM_FORMAT_BGR233:
2670 1.1 riastrad case DRM_FORMAT_XRGB4444:
2671 1.1 riastrad case DRM_FORMAT_XBGR4444:
2672 1.1 riastrad case DRM_FORMAT_RGBX4444:
2673 1.1 riastrad case DRM_FORMAT_BGRX4444:
2674 1.1 riastrad case DRM_FORMAT_ARGB4444:
2675 1.1 riastrad case DRM_FORMAT_ABGR4444:
2676 1.1 riastrad case DRM_FORMAT_RGBA4444:
2677 1.1 riastrad case DRM_FORMAT_BGRA4444:
2678 1.1 riastrad case DRM_FORMAT_XRGB1555:
2679 1.1 riastrad case DRM_FORMAT_XBGR1555:
2680 1.1 riastrad case DRM_FORMAT_RGBX5551:
2681 1.1 riastrad case DRM_FORMAT_BGRX5551:
2682 1.1 riastrad case DRM_FORMAT_ARGB1555:
2683 1.1 riastrad case DRM_FORMAT_ABGR1555:
2684 1.1 riastrad case DRM_FORMAT_RGBA5551:
2685 1.1 riastrad case DRM_FORMAT_BGRA5551:
2686 1.1 riastrad case DRM_FORMAT_RGB565:
2687 1.1 riastrad case DRM_FORMAT_BGR565:
2688 1.1 riastrad case DRM_FORMAT_RGB888:
2689 1.1 riastrad case DRM_FORMAT_BGR888:
2690 1.1 riastrad case DRM_FORMAT_XRGB8888:
2691 1.1 riastrad case DRM_FORMAT_XBGR8888:
2692 1.1 riastrad case DRM_FORMAT_RGBX8888:
2693 1.1 riastrad case DRM_FORMAT_BGRX8888:
2694 1.1 riastrad case DRM_FORMAT_ARGB8888:
2695 1.1 riastrad case DRM_FORMAT_ABGR8888:
2696 1.1 riastrad case DRM_FORMAT_RGBA8888:
2697 1.1 riastrad case DRM_FORMAT_BGRA8888:
2698 1.1 riastrad case DRM_FORMAT_XRGB2101010:
2699 1.1 riastrad case DRM_FORMAT_XBGR2101010:
2700 1.1 riastrad case DRM_FORMAT_RGBX1010102:
2701 1.1 riastrad case DRM_FORMAT_BGRX1010102:
2702 1.1 riastrad case DRM_FORMAT_ARGB2101010:
2703 1.1 riastrad case DRM_FORMAT_ABGR2101010:
2704 1.1 riastrad case DRM_FORMAT_RGBA1010102:
2705 1.1 riastrad case DRM_FORMAT_BGRA1010102:
2706 1.1 riastrad case DRM_FORMAT_YUYV:
2707 1.1 riastrad case DRM_FORMAT_YVYU:
2708 1.1 riastrad case DRM_FORMAT_UYVY:
2709 1.1 riastrad case DRM_FORMAT_VYUY:
2710 1.1 riastrad case DRM_FORMAT_AYUV:
2711 1.1 riastrad case DRM_FORMAT_NV12:
2712 1.1 riastrad case DRM_FORMAT_NV21:
2713 1.1 riastrad case DRM_FORMAT_NV16:
2714 1.1 riastrad case DRM_FORMAT_NV61:
2715 1.1 riastrad case DRM_FORMAT_NV24:
2716 1.1 riastrad case DRM_FORMAT_NV42:
2717 1.1 riastrad case DRM_FORMAT_YUV410:
2718 1.1 riastrad case DRM_FORMAT_YVU410:
2719 1.1 riastrad case DRM_FORMAT_YUV411:
2720 1.1 riastrad case DRM_FORMAT_YVU411:
2721 1.1 riastrad case DRM_FORMAT_YUV420:
2722 1.1 riastrad case DRM_FORMAT_YVU420:
2723 1.1 riastrad case DRM_FORMAT_YUV422:
2724 1.1 riastrad case DRM_FORMAT_YVU422:
2725 1.1 riastrad case DRM_FORMAT_YUV444:
2726 1.1 riastrad case DRM_FORMAT_YVU444:
2727 1.1 riastrad return 0;
2728 1.1 riastrad default:
2729 1.1.1.2 riastrad DRM_DEBUG_KMS("invalid pixel format %s\n",
2730 1.1.1.2 riastrad drm_get_format_name(r->pixel_format));
2731 1.1 riastrad return -EINVAL;
2732 1.1 riastrad }
2733 1.1 riastrad }
2734 1.1 riastrad
2735 1.1 riastrad static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
2736 1.1 riastrad {
2737 1.1 riastrad int ret, hsub, vsub, num_planes, i;
2738 1.1 riastrad
2739 1.1 riastrad ret = format_check(r);
2740 1.1 riastrad if (ret) {
2741 1.1.1.2 riastrad DRM_DEBUG_KMS("bad framebuffer format %s\n",
2742 1.1.1.2 riastrad drm_get_format_name(r->pixel_format));
2743 1.1 riastrad return ret;
2744 1.1 riastrad }
2745 1.1 riastrad
2746 1.1 riastrad hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
2747 1.1 riastrad vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
2748 1.1 riastrad num_planes = drm_format_num_planes(r->pixel_format);
2749 1.1 riastrad
2750 1.1 riastrad if (r->width == 0 || r->width % hsub) {
2751 1.1 riastrad DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
2752 1.1 riastrad return -EINVAL;
2753 1.1 riastrad }
2754 1.1 riastrad
2755 1.1 riastrad if (r->height == 0 || r->height % vsub) {
2756 1.1 riastrad DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
2757 1.1 riastrad return -EINVAL;
2758 1.1 riastrad }
2759 1.1 riastrad
2760 1.1 riastrad for (i = 0; i < num_planes; i++) {
2761 1.1 riastrad unsigned int width = r->width / (i != 0 ? hsub : 1);
2762 1.1 riastrad unsigned int height = r->height / (i != 0 ? vsub : 1);
2763 1.1 riastrad unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
2764 1.1 riastrad
2765 1.1 riastrad if (!r->handles[i]) {
2766 1.1 riastrad DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
2767 1.1 riastrad return -EINVAL;
2768 1.1 riastrad }
2769 1.1 riastrad
2770 1.1 riastrad if ((uint64_t) width * cpp > UINT_MAX)
2771 1.1 riastrad return -ERANGE;
2772 1.1 riastrad
2773 1.1 riastrad if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
2774 1.1 riastrad return -ERANGE;
2775 1.1 riastrad
2776 1.1 riastrad if (r->pitches[i] < width * cpp) {
2777 1.1 riastrad DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
2778 1.1 riastrad return -EINVAL;
2779 1.1 riastrad }
2780 1.1 riastrad }
2781 1.1 riastrad
2782 1.1 riastrad return 0;
2783 1.1 riastrad }
2784 1.1 riastrad
2785 1.1 riastrad /**
2786 1.1 riastrad * drm_mode_addfb2 - add an FB to the graphics configuration
2787 1.1.1.2 riastrad * @dev: drm device for the ioctl
2788 1.1.1.2 riastrad * @data: data pointer for the ioctl
2789 1.1.1.2 riastrad * @file_priv: drm file for the ioctl call
2790 1.1.1.2 riastrad *
2791 1.1.1.2 riastrad * Add a new FB to the specified CRTC, given a user request with format. This is
2792 1.1.1.2 riastrad * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
2793 1.1.1.2 riastrad * and uses fourcc codes as pixel format specifiers.
2794 1.1 riastrad *
2795 1.1 riastrad * Called by the user via ioctl.
2796 1.1 riastrad *
2797 1.1.1.2 riastrad * Returns:
2798 1.1 riastrad * Zero on success, errno on failure.
2799 1.1 riastrad */
2800 1.1 riastrad int drm_mode_addfb2(struct drm_device *dev,
2801 1.1 riastrad void *data, struct drm_file *file_priv)
2802 1.1 riastrad {
2803 1.1 riastrad struct drm_mode_fb_cmd2 *r = data;
2804 1.1 riastrad struct drm_mode_config *config = &dev->mode_config;
2805 1.1 riastrad struct drm_framebuffer *fb;
2806 1.1 riastrad int ret;
2807 1.1 riastrad
2808 1.1 riastrad if (!drm_core_check_feature(dev, DRIVER_MODESET))
2809 1.1 riastrad return -EINVAL;
2810 1.1 riastrad
2811 1.1 riastrad if (r->flags & ~DRM_MODE_FB_INTERLACED) {
2812 1.1 riastrad DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
2813 1.1 riastrad return -EINVAL;
2814 1.1 riastrad }
2815 1.1 riastrad
2816 1.1 riastrad if ((config->min_width > r->width) || (r->width > config->max_width)) {
2817 1.1 riastrad DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
2818 1.1 riastrad r->width, config->min_width, config->max_width);
2819 1.1 riastrad return -EINVAL;
2820 1.1 riastrad }
2821 1.1 riastrad if ((config->min_height > r->height) || (r->height > config->max_height)) {
2822 1.1 riastrad DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
2823 1.1 riastrad r->height, config->min_height, config->max_height);
2824 1.1 riastrad return -EINVAL;
2825 1.1 riastrad }
2826 1.1 riastrad
2827 1.1 riastrad ret = framebuffer_check(r);
2828 1.1 riastrad if (ret)
2829 1.1 riastrad return ret;
2830 1.1 riastrad
2831 1.1 riastrad fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
2832 1.1 riastrad if (IS_ERR(fb)) {
2833 1.1 riastrad DRM_DEBUG_KMS("could not create framebuffer\n");
2834 1.1.1.2 riastrad return PTR_ERR(fb);
2835 1.1 riastrad }
2836 1.1 riastrad
2837 1.1.1.2 riastrad mutex_lock(&file_priv->fbs_lock);
2838 1.1 riastrad r->fb_id = fb->base.id;
2839 1.1 riastrad list_add(&fb->filp_head, &file_priv->fbs);
2840 1.1 riastrad DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
2841 1.1.1.2 riastrad mutex_unlock(&file_priv->fbs_lock);
2842 1.1.1.2 riastrad
2843 1.1 riastrad
2844 1.1 riastrad return ret;
2845 1.1 riastrad }
2846 1.1 riastrad
2847 1.1 riastrad /**
2848 1.1 riastrad * drm_mode_rmfb - remove an FB from the configuration
2849 1.1.1.2 riastrad * @dev: drm device for the ioctl
2850 1.1.1.2 riastrad * @data: data pointer for the ioctl
2851 1.1.1.2 riastrad * @file_priv: drm file for the ioctl call
2852 1.1 riastrad *
2853 1.1 riastrad * Remove the FB specified by the user.
2854 1.1 riastrad *
2855 1.1 riastrad * Called by the user via ioctl.
2856 1.1 riastrad *
2857 1.1.1.2 riastrad * Returns:
2858 1.1 riastrad * Zero on success, errno on failure.
2859 1.1 riastrad */
2860 1.1 riastrad int drm_mode_rmfb(struct drm_device *dev,
2861 1.1 riastrad void *data, struct drm_file *file_priv)
2862 1.1 riastrad {
2863 1.1 riastrad struct drm_framebuffer *fb = NULL;
2864 1.1 riastrad struct drm_framebuffer *fbl = NULL;
2865 1.1 riastrad uint32_t *id = data;
2866 1.1 riastrad int found = 0;
2867 1.1 riastrad
2868 1.1 riastrad if (!drm_core_check_feature(dev, DRIVER_MODESET))
2869 1.1 riastrad return -EINVAL;
2870 1.1 riastrad
2871 1.1.1.2 riastrad mutex_lock(&file_priv->fbs_lock);
2872 1.1.1.2 riastrad mutex_lock(&dev->mode_config.fb_lock);
2873 1.1.1.2 riastrad fb = __drm_framebuffer_lookup(dev, *id);
2874 1.1.1.2 riastrad if (!fb)
2875 1.1.1.2 riastrad goto fail_lookup;
2876 1.1 riastrad
2877 1.1 riastrad list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2878 1.1 riastrad if (fb == fbl)
2879 1.1 riastrad found = 1;
2880 1.1.1.2 riastrad if (!found)
2881 1.1.1.2 riastrad goto fail_lookup;
2882 1.1 riastrad
2883 1.1.1.2 riastrad /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
2884 1.1.1.2 riastrad __drm_framebuffer_unregister(dev, fb);
2885 1.1.1.2 riastrad
2886 1.1.1.2 riastrad list_del_init(&fb->filp_head);
2887 1.1.1.2 riastrad mutex_unlock(&dev->mode_config.fb_lock);
2888 1.1.1.2 riastrad mutex_unlock(&file_priv->fbs_lock);
2889 1.1 riastrad
2890 1.1 riastrad drm_framebuffer_remove(fb);
2891 1.1 riastrad
2892 1.1.1.2 riastrad return 0;
2893 1.1.1.2 riastrad
2894 1.1.1.2 riastrad fail_lookup:
2895 1.1.1.2 riastrad mutex_unlock(&dev->mode_config.fb_lock);
2896 1.1.1.2 riastrad mutex_unlock(&file_priv->fbs_lock);
2897 1.1.1.2 riastrad
2898 1.1.1.2 riastrad return -ENOENT;
2899 1.1 riastrad }
2900 1.1 riastrad
2901 1.1 riastrad /**
2902 1.1 riastrad * drm_mode_getfb - get FB info
2903 1.1.1.2 riastrad * @dev: drm device for the ioctl
2904 1.1.1.2 riastrad * @data: data pointer for the ioctl
2905 1.1.1.2 riastrad * @file_priv: drm file for the ioctl call
2906 1.1 riastrad *
2907 1.1 riastrad * Lookup the FB given its ID and return info about it.
2908 1.1 riastrad *
2909 1.1 riastrad * Called by the user via ioctl.
2910 1.1 riastrad *
2911 1.1.1.2 riastrad * Returns:
2912 1.1 riastrad * Zero on success, errno on failure.
2913 1.1 riastrad */
2914 1.1 riastrad int drm_mode_getfb(struct drm_device *dev,
2915 1.1 riastrad void *data, struct drm_file *file_priv)
2916 1.1 riastrad {
2917 1.1 riastrad struct drm_mode_fb_cmd *r = data;
2918 1.1 riastrad struct drm_framebuffer *fb;
2919 1.1.1.2 riastrad int ret;
2920 1.1 riastrad
2921 1.1 riastrad if (!drm_core_check_feature(dev, DRIVER_MODESET))
2922 1.1 riastrad return -EINVAL;
2923 1.1 riastrad
2924 1.1.1.2 riastrad fb = drm_framebuffer_lookup(dev, r->fb_id);
2925 1.1.1.2 riastrad if (!fb)
2926 1.1.1.2 riastrad return -ENOENT;
2927 1.1 riastrad
2928 1.1 riastrad r->height = fb->height;
2929 1.1 riastrad r->width = fb->width;
2930 1.1 riastrad r->depth = fb->depth;
2931 1.1 riastrad r->bpp = fb->bits_per_pixel;
2932 1.1 riastrad r->pitch = fb->pitches[0];
2933 1.1.1.2 riastrad if (fb->funcs->create_handle) {
2934 1.1.1.2 riastrad if (file_priv->is_master || capable(CAP_SYS_ADMIN) ||
2935 1.1.1.2 riastrad drm_is_control_client(file_priv)) {
2936 1.1.1.2 riastrad ret = fb->funcs->create_handle(fb, file_priv,
2937 1.1.1.2 riastrad &r->handle);
2938 1.1.1.2 riastrad } else {
2939 1.1.1.2 riastrad /* GET_FB() is an unprivileged ioctl so we must not
2940 1.1.1.2 riastrad * return a buffer-handle to non-master processes! For
2941 1.1.1.2 riastrad * backwards-compatibility reasons, we cannot make
2942 1.1.1.2 riastrad * GET_FB() privileged, so just return an invalid handle
2943 1.1.1.2 riastrad * for non-masters. */
2944 1.1.1.2 riastrad r->handle = 0;
2945 1.1.1.2 riastrad ret = 0;
2946 1.1.1.2 riastrad }
2947 1.1.1.2 riastrad } else {
2948 1.1.1.2 riastrad ret = -ENODEV;
2949 1.1.1.2 riastrad }
2950 1.1.1.2 riastrad
2951 1.1.1.2 riastrad drm_framebuffer_unreference(fb);
2952 1.1 riastrad
2953 1.1 riastrad return ret;
2954 1.1 riastrad }
2955 1.1 riastrad
2956 1.1.1.2 riastrad /**
2957 1.1.1.2 riastrad * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
2958 1.1.1.2 riastrad * @dev: drm device for the ioctl
2959 1.1.1.2 riastrad * @data: data pointer for the ioctl
2960 1.1.1.2 riastrad * @file_priv: drm file for the ioctl call
2961 1.1.1.2 riastrad *
2962 1.1.1.2 riastrad * Lookup the FB and flush out the damaged area supplied by userspace as a clip
2963 1.1.1.2 riastrad * rectangle list. Generic userspace which does frontbuffer rendering must call
2964 1.1.1.2 riastrad * this ioctl to flush out the changes on manual-update display outputs, e.g.
2965 1.1.1.2 riastrad * usb display-link, mipi manual update panels or edp panel self refresh modes.
2966 1.1.1.2 riastrad *
2967 1.1.1.2 riastrad * Modesetting drivers which always update the frontbuffer do not need to
2968 1.1.1.2 riastrad * implement the corresponding ->dirty framebuffer callback.
2969 1.1.1.2 riastrad *
2970 1.1.1.2 riastrad * Called by the user via ioctl.
2971 1.1.1.2 riastrad *
2972 1.1.1.2 riastrad * Returns:
2973 1.1.1.2 riastrad * Zero on success, errno on failure.
2974 1.1.1.2 riastrad */
2975 1.1 riastrad int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
2976 1.1 riastrad void *data, struct drm_file *file_priv)
2977 1.1 riastrad {
2978 1.1 riastrad struct drm_clip_rect __user *clips_ptr;
2979 1.1 riastrad struct drm_clip_rect *clips = NULL;
2980 1.1 riastrad struct drm_mode_fb_dirty_cmd *r = data;
2981 1.1 riastrad struct drm_framebuffer *fb;
2982 1.1 riastrad unsigned flags;
2983 1.1 riastrad int num_clips;
2984 1.1 riastrad int ret;
2985 1.1 riastrad
2986 1.1 riastrad if (!drm_core_check_feature(dev, DRIVER_MODESET))
2987 1.1 riastrad return -EINVAL;
2988 1.1 riastrad
2989 1.1.1.2 riastrad fb = drm_framebuffer_lookup(dev, r->fb_id);
2990 1.1.1.2 riastrad if (!fb)
2991 1.1.1.2 riastrad return -ENOENT;
2992 1.1 riastrad
2993 1.1 riastrad num_clips = r->num_clips;
2994 1.1 riastrad clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
2995 1.1 riastrad
2996 1.1 riastrad if (!num_clips != !clips_ptr) {
2997 1.1 riastrad ret = -EINVAL;
2998 1.1 riastrad goto out_err1;
2999 1.1 riastrad }
3000 1.1 riastrad
3001 1.1 riastrad flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
3002 1.1 riastrad
3003 1.1 riastrad /* If userspace annotates copy, clips must come in pairs */
3004 1.1 riastrad if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
3005 1.1 riastrad ret = -EINVAL;
3006 1.1 riastrad goto out_err1;
3007 1.1 riastrad }
3008 1.1 riastrad
3009 1.1 riastrad if (num_clips && clips_ptr) {
3010 1.1 riastrad if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
3011 1.1 riastrad ret = -EINVAL;
3012 1.1 riastrad goto out_err1;
3013 1.1 riastrad }
3014 1.1 riastrad clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
3015 1.1 riastrad if (!clips) {
3016 1.1 riastrad ret = -ENOMEM;
3017 1.1 riastrad goto out_err1;
3018 1.1 riastrad }
3019 1.1 riastrad
3020 1.1 riastrad ret = copy_from_user(clips, clips_ptr,
3021 1.1 riastrad num_clips * sizeof(*clips));
3022 1.1 riastrad if (ret) {
3023 1.1 riastrad ret = -EFAULT;
3024 1.1 riastrad goto out_err2;
3025 1.1 riastrad }
3026 1.1 riastrad }
3027 1.1 riastrad
3028 1.1 riastrad if (fb->funcs->dirty) {
3029 1.1 riastrad ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
3030 1.1 riastrad clips, num_clips);
3031 1.1 riastrad } else {
3032 1.1 riastrad ret = -ENOSYS;
3033 1.1 riastrad }
3034 1.1 riastrad
3035 1.1 riastrad out_err2:
3036 1.1 riastrad kfree(clips);
3037 1.1 riastrad out_err1:
3038 1.1.1.2 riastrad drm_framebuffer_unreference(fb);
3039 1.1.1.2 riastrad
3040 1.1 riastrad return ret;
3041 1.1 riastrad }
3042 1.1 riastrad
3043 1.1 riastrad
3044 1.1 riastrad /**
3045 1.1 riastrad * drm_fb_release - remove and free the FBs on this file
3046 1.1.1.2 riastrad * @priv: drm file for the ioctl
3047 1.1 riastrad *
3048 1.1 riastrad * Destroy all the FBs associated with @filp.
3049 1.1 riastrad *
3050 1.1 riastrad * Called by the user via ioctl.
3051 1.1 riastrad *
3052 1.1.1.2 riastrad * Returns:
3053 1.1 riastrad * Zero on success, errno on failure.
3054 1.1 riastrad */
3055 1.1 riastrad void drm_fb_release(struct drm_file *priv)
3056 1.1 riastrad {
3057 1.1 riastrad struct drm_device *dev = priv->minor->dev;
3058 1.1 riastrad struct drm_framebuffer *fb, *tfb;
3059 1.1 riastrad
3060 1.1.1.2 riastrad mutex_lock(&priv->fbs_lock);
3061 1.1 riastrad list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
3062 1.1 riastrad
3063 1.1.1.2 riastrad mutex_lock(&dev->mode_config.fb_lock);
3064 1.1.1.2 riastrad /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
3065 1.1.1.2 riastrad __drm_framebuffer_unregister(dev, fb);
3066 1.1.1.2 riastrad mutex_unlock(&dev->mode_config.fb_lock);
3067 1.1 riastrad
3068 1.1.1.2 riastrad list_del_init(&fb->filp_head);
3069 1.1 riastrad
3070 1.1.1.2 riastrad /* This will also drop the fpriv->fbs reference. */
3071 1.1.1.2 riastrad drm_framebuffer_remove(fb);
3072 1.1 riastrad }
3073 1.1.1.2 riastrad mutex_unlock(&priv->fbs_lock);
3074 1.1 riastrad }
3075 1.1 riastrad
3076 1.1 riastrad /**
3077 1.1.1.2 riastrad * drm_property_create - create a new property type
3078 1.1.1.2 riastrad * @dev: drm device
3079 1.1.1.2 riastrad * @flags: flags specifying the property type
3080 1.1.1.2 riastrad * @name: name of the property
3081 1.1.1.2 riastrad * @num_values: number of pre-defined values
3082 1.1 riastrad *
3083 1.1.1.2 riastrad * This creates a new generic drm property which can then be attached to a drm
3084 1.1.1.2 riastrad * object with drm_object_attach_property. The returned property object must be
3085 1.1.1.2 riastrad * freed with drm_property_destroy.
3086 1.1 riastrad *
3087 1.1.1.2 riastrad * Returns:
3088 1.1.1.2 riastrad * A pointer to the newly created property on success, NULL on failure.
3089 1.1 riastrad */
3090 1.1 riastrad struct drm_property *drm_property_create(struct drm_device *dev, int flags,
3091 1.1 riastrad const char *name, int num_values)
3092 1.1 riastrad {
3093 1.1 riastrad struct drm_property *property = NULL;
3094 1.1 riastrad int ret;
3095 1.1 riastrad
3096 1.1 riastrad property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
3097 1.1 riastrad if (!property)
3098 1.1 riastrad return NULL;
3099 1.1 riastrad
3100 1.1 riastrad if (num_values) {
3101 1.1 riastrad property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
3102 1.1 riastrad if (!property->values)
3103 1.1 riastrad goto fail;
3104 1.1 riastrad }
3105 1.1 riastrad
3106 1.1 riastrad ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
3107 1.1 riastrad if (ret)
3108 1.1 riastrad goto fail;
3109 1.1 riastrad
3110 1.1 riastrad property->flags = flags;
3111 1.1 riastrad property->num_values = num_values;
3112 1.1 riastrad INIT_LIST_HEAD(&property->enum_blob_list);
3113 1.1 riastrad
3114 1.1 riastrad if (name) {
3115 1.1 riastrad strncpy(property->name, name, DRM_PROP_NAME_LEN);
3116 1.1 riastrad property->name[DRM_PROP_NAME_LEN-1] = '\0';
3117 1.1 riastrad }
3118 1.1 riastrad
3119 1.1 riastrad list_add_tail(&property->head, &dev->mode_config.property_list);
3120 1.1 riastrad return property;
3121 1.1 riastrad fail:
3122 1.1 riastrad kfree(property->values);
3123 1.1 riastrad kfree(property);
3124 1.1 riastrad return NULL;
3125 1.1 riastrad }
3126 1.1 riastrad EXPORT_SYMBOL(drm_property_create);
3127 1.1 riastrad
3128 1.1.1.2 riastrad /**
3129 1.1.1.2 riastrad * drm_property_create - create a new enumeration property type
3130 1.1.1.2 riastrad * @dev: drm device
3131 1.1.1.2 riastrad * @flags: flags specifying the property type
3132 1.1.1.2 riastrad * @name: name of the property
3133 1.1.1.2 riastrad * @props: enumeration lists with property values
3134 1.1.1.2 riastrad * @num_values: number of pre-defined values
3135 1.1.1.2 riastrad *
3136 1.1.1.2 riastrad * This creates a new generic drm property which can then be attached to a drm
3137 1.1.1.2 riastrad * object with drm_object_attach_property. The returned property object must be
3138 1.1.1.2 riastrad * freed with drm_property_destroy.
3139 1.1.1.2 riastrad *
3140 1.1.1.2 riastrad * Userspace is only allowed to set one of the predefined values for enumeration
3141 1.1.1.2 riastrad * properties.
3142 1.1.1.2 riastrad *
3143 1.1.1.2 riastrad * Returns:
3144 1.1.1.2 riastrad * A pointer to the newly created property on success, NULL on failure.
3145 1.1.1.2 riastrad */
3146 1.1 riastrad struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
3147 1.1 riastrad const char *name,
3148 1.1 riastrad const struct drm_prop_enum_list *props,
3149 1.1 riastrad int num_values)
3150 1.1 riastrad {
3151 1.1 riastrad struct drm_property *property;
3152 1.1 riastrad int i, ret;
3153 1.1 riastrad
3154 1.1 riastrad flags |= DRM_MODE_PROP_ENUM;
3155 1.1 riastrad
3156 1.1 riastrad property = drm_property_create(dev, flags, name, num_values);
3157 1.1 riastrad if (!property)
3158 1.1 riastrad return NULL;
3159 1.1 riastrad
3160 1.1 riastrad for (i = 0; i < num_values; i++) {
3161 1.1 riastrad ret = drm_property_add_enum(property, i,
3162 1.1 riastrad props[i].type,
3163 1.1 riastrad props[i].name);
3164 1.1 riastrad if (ret) {
3165 1.1 riastrad drm_property_destroy(dev, property);
3166 1.1 riastrad return NULL;
3167 1.1 riastrad }
3168 1.1 riastrad }
3169 1.1 riastrad
3170 1.1 riastrad return property;
3171 1.1 riastrad }
3172 1.1 riastrad EXPORT_SYMBOL(drm_property_create_enum);
3173 1.1 riastrad
3174 1.1.1.2 riastrad /**
3175 1.1.1.2 riastrad * drm_property_create - create a new bitmask property type
3176 1.1.1.2 riastrad * @dev: drm device
3177 1.1.1.2 riastrad * @flags: flags specifying the property type
3178 1.1.1.2 riastrad * @name: name of the property
3179 1.1.1.2 riastrad * @props: enumeration lists with property bitflags
3180 1.1.1.2 riastrad * @num_values: number of pre-defined values
3181 1.1.1.2 riastrad *
3182 1.1.1.2 riastrad * This creates a new generic drm property which can then be attached to a drm
3183 1.1.1.2 riastrad * object with drm_object_attach_property. The returned property object must be
3184 1.1.1.2 riastrad * freed with drm_property_destroy.
3185 1.1.1.2 riastrad *
3186 1.1.1.2 riastrad * Compared to plain enumeration properties userspace is allowed to set any
3187 1.1.1.2 riastrad * or'ed together combination of the predefined property bitflag values
3188 1.1.1.2 riastrad *
3189 1.1.1.2 riastrad * Returns:
3190 1.1.1.2 riastrad * A pointer to the newly created property on success, NULL on failure.
3191 1.1.1.2 riastrad */
3192 1.1 riastrad struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
3193 1.1 riastrad int flags, const char *name,
3194 1.1 riastrad const struct drm_prop_enum_list *props,
3195 1.1 riastrad int num_values)
3196 1.1 riastrad {
3197 1.1 riastrad struct drm_property *property;
3198 1.1 riastrad int i, ret;
3199 1.1 riastrad
3200 1.1 riastrad flags |= DRM_MODE_PROP_BITMASK;
3201 1.1 riastrad
3202 1.1 riastrad property = drm_property_create(dev, flags, name, num_values);
3203 1.1 riastrad if (!property)
3204 1.1 riastrad return NULL;
3205 1.1 riastrad
3206 1.1 riastrad for (i = 0; i < num_values; i++) {
3207 1.1 riastrad ret = drm_property_add_enum(property, i,
3208 1.1 riastrad props[i].type,
3209 1.1 riastrad props[i].name);
3210 1.1 riastrad if (ret) {
3211 1.1 riastrad drm_property_destroy(dev, property);
3212 1.1 riastrad return NULL;
3213 1.1 riastrad }
3214 1.1 riastrad }
3215 1.1 riastrad
3216 1.1 riastrad return property;
3217 1.1 riastrad }
3218 1.1 riastrad EXPORT_SYMBOL(drm_property_create_bitmask);
3219 1.1 riastrad
3220 1.1.1.2 riastrad /**
3221 1.1.1.2 riastrad * drm_property_create - create a new ranged property type
3222 1.1.1.2 riastrad * @dev: drm device
3223 1.1.1.2 riastrad * @flags: flags specifying the property type
3224 1.1.1.2 riastrad * @name: name of the property
3225 1.1.1.2 riastrad * @min: minimum value of the property
3226 1.1.1.2 riastrad * @max: maximum value of the property
3227 1.1.1.2 riastrad *
3228 1.1.1.2 riastrad * This creates a new generic drm property which can then be attached to a drm
3229 1.1.1.2 riastrad * object with drm_object_attach_property. The returned property object must be
3230 1.1.1.2 riastrad * freed with drm_property_destroy.
3231 1.1.1.2 riastrad *
3232 1.1.1.2 riastrad * Userspace is allowed to set any interger value in the (min, max) range
3233 1.1.1.2 riastrad * inclusive.
3234 1.1.1.2 riastrad *
3235 1.1.1.2 riastrad * Returns:
3236 1.1.1.2 riastrad * A pointer to the newly created property on success, NULL on failure.
3237 1.1.1.2 riastrad */
3238 1.1 riastrad struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
3239 1.1 riastrad const char *name,
3240 1.1 riastrad uint64_t min, uint64_t max)
3241 1.1 riastrad {
3242 1.1 riastrad struct drm_property *property;
3243 1.1 riastrad
3244 1.1 riastrad flags |= DRM_MODE_PROP_RANGE;
3245 1.1 riastrad
3246 1.1 riastrad property = drm_property_create(dev, flags, name, 2);
3247 1.1 riastrad if (!property)
3248 1.1 riastrad return NULL;
3249 1.1 riastrad
3250 1.1 riastrad property->values[0] = min;
3251 1.1 riastrad property->values[1] = max;
3252 1.1 riastrad
3253 1.1 riastrad return property;
3254 1.1 riastrad }
3255 1.1 riastrad EXPORT_SYMBOL(drm_property_create_range);
3256 1.1 riastrad
3257 1.1.1.2 riastrad /**
3258 1.1.1.2 riastrad * drm_property_add_enum - add a possible value to an enumeration property
3259 1.1.1.2 riastrad * @property: enumeration property to change
3260 1.1.1.2 riastrad * @index: index of the new enumeration
3261 1.1.1.2 riastrad * @value: value of the new enumeration
3262 1.1.1.2 riastrad * @name: symbolic name of the new enumeration
3263 1.1.1.2 riastrad *
3264 1.1.1.2 riastrad * This functions adds enumerations to a property.
3265 1.1.1.2 riastrad *
3266 1.1.1.2 riastrad * It's use is deprecated, drivers should use one of the more specific helpers
3267 1.1.1.2 riastrad * to directly create the property with all enumerations already attached.
3268 1.1.1.2 riastrad *
3269 1.1.1.2 riastrad * Returns:
3270 1.1.1.2 riastrad * Zero on success, error code on failure.
3271 1.1.1.2 riastrad */
3272 1.1 riastrad int drm_property_add_enum(struct drm_property *property, int index,
3273 1.1 riastrad uint64_t value, const char *name)
3274 1.1 riastrad {
3275 1.1 riastrad struct drm_property_enum *prop_enum;
3276 1.1 riastrad
3277 1.1 riastrad if (!(property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)))
3278 1.1 riastrad return -EINVAL;
3279 1.1 riastrad
3280 1.1 riastrad /*
3281 1.1 riastrad * Bitmask enum properties have the additional constraint of values
3282 1.1 riastrad * from 0 to 63
3283 1.1 riastrad */
3284 1.1 riastrad if ((property->flags & DRM_MODE_PROP_BITMASK) && (value > 63))
3285 1.1 riastrad return -EINVAL;
3286 1.1 riastrad
3287 1.1 riastrad if (!list_empty(&property->enum_blob_list)) {
3288 1.1 riastrad list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3289 1.1 riastrad if (prop_enum->value == value) {
3290 1.1 riastrad strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3291 1.1 riastrad prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3292 1.1 riastrad return 0;
3293 1.1 riastrad }
3294 1.1 riastrad }
3295 1.1 riastrad }
3296 1.1 riastrad
3297 1.1 riastrad prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
3298 1.1 riastrad if (!prop_enum)
3299 1.1 riastrad return -ENOMEM;
3300 1.1 riastrad
3301 1.1 riastrad strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3302 1.1 riastrad prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3303 1.1 riastrad prop_enum->value = value;
3304 1.1 riastrad
3305 1.1 riastrad property->values[index] = value;
3306 1.1 riastrad list_add_tail(&prop_enum->head, &property->enum_blob_list);
3307 1.1 riastrad return 0;
3308 1.1 riastrad }
3309 1.1 riastrad EXPORT_SYMBOL(drm_property_add_enum);
3310 1.1 riastrad
3311 1.1.1.2 riastrad /**
3312 1.1.1.2 riastrad * drm_property_destroy - destroy a drm property
3313 1.1.1.2 riastrad * @dev: drm device
3314 1.1.1.2 riastrad * @property: property to destry
3315 1.1.1.2 riastrad *
3316 1.1.1.2 riastrad * This function frees a property including any attached resources like
3317 1.1.1.2 riastrad * enumeration values.
3318 1.1.1.2 riastrad */
3319 1.1 riastrad void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
3320 1.1 riastrad {
3321 1.1 riastrad struct drm_property_enum *prop_enum, *pt;
3322 1.1 riastrad
3323 1.1 riastrad list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
3324 1.1 riastrad list_del(&prop_enum->head);
3325 1.1 riastrad kfree(prop_enum);
3326 1.1 riastrad }
3327 1.1 riastrad
3328 1.1 riastrad if (property->num_values)
3329 1.1 riastrad kfree(property->values);
3330 1.1 riastrad drm_mode_object_put(dev, &property->base);
3331 1.1 riastrad list_del(&property->head);
3332 1.1 riastrad kfree(property);
3333 1.1 riastrad }
3334 1.1 riastrad EXPORT_SYMBOL(drm_property_destroy);
3335 1.1 riastrad
3336 1.1.1.2 riastrad /**
3337 1.1.1.2 riastrad * drm_object_attach_property - attach a property to a modeset object
3338 1.1.1.2 riastrad * @obj: drm modeset object
3339 1.1.1.2 riastrad * @property: property to attach
3340 1.1.1.2 riastrad * @init_val: initial value of the property
3341 1.1.1.2 riastrad *
3342 1.1.1.2 riastrad * This attaches the given property to the modeset object with the given initial
3343 1.1.1.2 riastrad * value. Currently this function cannot fail since the properties are stored in
3344 1.1.1.2 riastrad * a statically sized array.
3345 1.1.1.2 riastrad */
3346 1.1 riastrad void drm_object_attach_property(struct drm_mode_object *obj,
3347 1.1 riastrad struct drm_property *property,
3348 1.1 riastrad uint64_t init_val)
3349 1.1 riastrad {
3350 1.1 riastrad int count = obj->properties->count;
3351 1.1 riastrad
3352 1.1 riastrad if (count == DRM_OBJECT_MAX_PROPERTY) {
3353 1.1 riastrad WARN(1, "Failed to attach object property (type: 0x%x). Please "
3354 1.1 riastrad "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
3355 1.1 riastrad "you see this message on the same object type.\n",
3356 1.1 riastrad obj->type);
3357 1.1 riastrad return;
3358 1.1 riastrad }
3359 1.1 riastrad
3360 1.1 riastrad obj->properties->ids[count] = property->base.id;
3361 1.1 riastrad obj->properties->values[count] = init_val;
3362 1.1 riastrad obj->properties->count++;
3363 1.1 riastrad }
3364 1.1 riastrad EXPORT_SYMBOL(drm_object_attach_property);
3365 1.1 riastrad
3366 1.1.1.2 riastrad /**
3367 1.1.1.2 riastrad * drm_object_property_set_value - set the value of a property
3368 1.1.1.2 riastrad * @obj: drm mode object to set property value for
3369 1.1.1.2 riastrad * @property: property to set
3370 1.1.1.2 riastrad * @val: value the property should be set to
3371 1.1.1.2 riastrad *
3372 1.1.1.2 riastrad * This functions sets a given property on a given object. This function only
3373 1.1.1.2 riastrad * changes the software state of the property, it does not call into the
3374 1.1.1.2 riastrad * driver's ->set_property callback.
3375 1.1.1.2 riastrad *
3376 1.1.1.2 riastrad * Returns:
3377 1.1.1.2 riastrad * Zero on success, error code on failure.
3378 1.1.1.2 riastrad */
3379 1.1 riastrad int drm_object_property_set_value(struct drm_mode_object *obj,
3380 1.1 riastrad struct drm_property *property, uint64_t val)
3381 1.1 riastrad {
3382 1.1 riastrad int i;
3383 1.1 riastrad
3384 1.1 riastrad for (i = 0; i < obj->properties->count; i++) {
3385 1.1 riastrad if (obj->properties->ids[i] == property->base.id) {
3386 1.1 riastrad obj->properties->values[i] = val;
3387 1.1 riastrad return 0;
3388 1.1 riastrad }
3389 1.1 riastrad }
3390 1.1 riastrad
3391 1.1 riastrad return -EINVAL;
3392 1.1 riastrad }
3393 1.1 riastrad EXPORT_SYMBOL(drm_object_property_set_value);
3394 1.1 riastrad
3395 1.1.1.2 riastrad /**
3396 1.1.1.2 riastrad * drm_object_property_get_value - retrieve the value of a property
3397 1.1.1.2 riastrad * @obj: drm mode object to get property value from
3398 1.1.1.2 riastrad * @property: property to retrieve
3399 1.1.1.2 riastrad * @val: storage for the property value
3400 1.1.1.2 riastrad *
3401 1.1.1.2 riastrad * This function retrieves the softare state of the given property for the given
3402 1.1.1.2 riastrad * property. Since there is no driver callback to retrieve the current property
3403 1.1.1.2 riastrad * value this might be out of sync with the hardware, depending upon the driver
3404 1.1.1.2 riastrad * and property.
3405 1.1.1.2 riastrad *
3406 1.1.1.2 riastrad * Returns:
3407 1.1.1.2 riastrad * Zero on success, error code on failure.
3408 1.1.1.2 riastrad */
3409 1.1 riastrad int drm_object_property_get_value(struct drm_mode_object *obj,
3410 1.1 riastrad struct drm_property *property, uint64_t *val)
3411 1.1 riastrad {
3412 1.1 riastrad int i;
3413 1.1 riastrad
3414 1.1 riastrad for (i = 0; i < obj->properties->count; i++) {
3415 1.1 riastrad if (obj->properties->ids[i] == property->base.id) {
3416 1.1 riastrad *val = obj->properties->values[i];
3417 1.1 riastrad return 0;
3418 1.1 riastrad }
3419 1.1 riastrad }
3420 1.1 riastrad
3421 1.1 riastrad return -EINVAL;
3422 1.1 riastrad }
3423 1.1 riastrad EXPORT_SYMBOL(drm_object_property_get_value);
3424 1.1 riastrad
3425 1.1.1.2 riastrad /**
3426 1.1.1.2 riastrad * drm_mode_getproperty_ioctl - get the current value of a connector's property
3427 1.1.1.2 riastrad * @dev: DRM device
3428 1.1.1.2 riastrad * @data: ioctl data
3429 1.1.1.2 riastrad * @file_priv: DRM file info
3430 1.1.1.2 riastrad *
3431 1.1.1.2 riastrad * This function retrieves the current value for an connectors's property.
3432 1.1.1.2 riastrad *
3433 1.1.1.2 riastrad * Called by the user via ioctl.
3434 1.1.1.2 riastrad *
3435 1.1.1.2 riastrad * Returns:
3436 1.1.1.2 riastrad * Zero on success, errno on failure.
3437 1.1.1.2 riastrad */
3438 1.1 riastrad int drm_mode_getproperty_ioctl(struct drm_device *dev,
3439 1.1 riastrad void *data, struct drm_file *file_priv)
3440 1.1 riastrad {
3441 1.1 riastrad struct drm_mode_object *obj;
3442 1.1 riastrad struct drm_mode_get_property *out_resp = data;
3443 1.1 riastrad struct drm_property *property;
3444 1.1 riastrad int enum_count = 0;
3445 1.1 riastrad int blob_count = 0;
3446 1.1 riastrad int value_count = 0;
3447 1.1 riastrad int ret = 0, i;
3448 1.1 riastrad int copied;
3449 1.1 riastrad struct drm_property_enum *prop_enum;
3450 1.1 riastrad struct drm_mode_property_enum __user *enum_ptr;
3451 1.1 riastrad struct drm_property_blob *prop_blob;
3452 1.1 riastrad uint32_t __user *blob_id_ptr;
3453 1.1 riastrad uint64_t __user *values_ptr;
3454 1.1 riastrad uint32_t __user *blob_length_ptr;
3455 1.1 riastrad
3456 1.1 riastrad if (!drm_core_check_feature(dev, DRIVER_MODESET))
3457 1.1 riastrad return -EINVAL;
3458 1.1 riastrad
3459 1.1.1.2 riastrad drm_modeset_lock_all(dev);
3460 1.1 riastrad obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
3461 1.1 riastrad if (!obj) {
3462 1.1.1.2 riastrad ret = -ENOENT;
3463 1.1 riastrad goto done;
3464 1.1 riastrad }
3465 1.1 riastrad property = obj_to_property(obj);
3466 1.1 riastrad
3467 1.1 riastrad if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
3468 1.1 riastrad list_for_each_entry(prop_enum, &property->enum_blob_list, head)
3469 1.1 riastrad enum_count++;
3470 1.1 riastrad } else if (property->flags & DRM_MODE_PROP_BLOB) {
3471 1.1 riastrad list_for_each_entry(prop_blob, &property->enum_blob_list, head)
3472 1.1 riastrad blob_count++;
3473 1.1 riastrad }
3474 1.1 riastrad
3475 1.1 riastrad value_count = property->num_values;
3476 1.1 riastrad
3477 1.1 riastrad strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
3478 1.1 riastrad out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
3479 1.1 riastrad out_resp->flags = property->flags;
3480 1.1 riastrad
3481 1.1 riastrad if ((out_resp->count_values >= value_count) && value_count) {
3482 1.1 riastrad values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
3483 1.1 riastrad for (i = 0; i < value_count; i++) {
3484 1.1 riastrad if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
3485 1.1 riastrad ret = -EFAULT;
3486 1.1 riastrad goto done;
3487 1.1 riastrad }
3488 1.1 riastrad }
3489 1.1 riastrad }
3490 1.1 riastrad out_resp->count_values = value_count;
3491 1.1 riastrad
3492 1.1 riastrad if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
3493 1.1 riastrad if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
3494 1.1 riastrad copied = 0;
3495 1.1 riastrad enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
3496 1.1 riastrad list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3497 1.1 riastrad
3498 1.1 riastrad if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
3499 1.1 riastrad ret = -EFAULT;
3500 1.1 riastrad goto done;
3501 1.1 riastrad }
3502 1.1 riastrad
3503 1.1 riastrad if (copy_to_user(&enum_ptr[copied].name,
3504 1.1 riastrad &prop_enum->name, DRM_PROP_NAME_LEN)) {
3505 1.1 riastrad ret = -EFAULT;
3506 1.1 riastrad goto done;
3507 1.1 riastrad }
3508 1.1 riastrad copied++;
3509 1.1 riastrad }
3510 1.1 riastrad }
3511 1.1 riastrad out_resp->count_enum_blobs = enum_count;
3512 1.1 riastrad }
3513 1.1 riastrad
3514 1.1 riastrad if (property->flags & DRM_MODE_PROP_BLOB) {
3515 1.1 riastrad if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
3516 1.1 riastrad copied = 0;
3517 1.1 riastrad blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
3518 1.1 riastrad blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
3519 1.1 riastrad
3520 1.1 riastrad list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
3521 1.1 riastrad if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
3522 1.1 riastrad ret = -EFAULT;
3523 1.1 riastrad goto done;
3524 1.1 riastrad }
3525 1.1 riastrad
3526 1.1 riastrad if (put_user(prop_blob->length, blob_length_ptr + copied)) {
3527 1.1 riastrad ret = -EFAULT;
3528 1.1 riastrad goto done;
3529 1.1 riastrad }
3530 1.1 riastrad
3531 1.1 riastrad copied++;
3532 1.1 riastrad }
3533 1.1 riastrad }
3534 1.1 riastrad out_resp->count_enum_blobs = blob_count;
3535 1.1 riastrad }
3536 1.1 riastrad done:
3537 1.1.1.2 riastrad drm_modeset_unlock_all(dev);
3538 1.1 riastrad return ret;
3539 1.1 riastrad }
3540 1.1 riastrad
3541 1.1 riastrad static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
3542 1.1 riastrad void *data)
3543 1.1 riastrad {
3544 1.1 riastrad struct drm_property_blob *blob;
3545 1.1 riastrad int ret;
3546 1.1 riastrad
3547 1.1 riastrad if (!length || !data)
3548 1.1 riastrad return NULL;
3549 1.1 riastrad
3550 1.1 riastrad blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
3551 1.1 riastrad if (!blob)
3552 1.1 riastrad return NULL;
3553 1.1 riastrad
3554 1.1 riastrad ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
3555 1.1 riastrad if (ret) {
3556 1.1 riastrad kfree(blob);
3557 1.1 riastrad return NULL;
3558 1.1 riastrad }
3559 1.1 riastrad
3560 1.1 riastrad blob->length = length;
3561 1.1 riastrad
3562 1.1 riastrad memcpy(blob->data, data, length);
3563 1.1 riastrad
3564 1.1 riastrad list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
3565 1.1 riastrad return blob;
3566 1.1 riastrad }
3567 1.1 riastrad
3568 1.1 riastrad static void drm_property_destroy_blob(struct drm_device *dev,
3569 1.1 riastrad struct drm_property_blob *blob)
3570 1.1 riastrad {
3571 1.1 riastrad drm_mode_object_put(dev, &blob->base);
3572 1.1 riastrad list_del(&blob->head);
3573 1.1 riastrad kfree(blob);
3574 1.1 riastrad }
3575 1.1 riastrad
3576 1.1.1.2 riastrad /**
3577 1.1.1.2 riastrad * drm_mode_getblob_ioctl - get the contents of a blob property value
3578 1.1.1.2 riastrad * @dev: DRM device
3579 1.1.1.2 riastrad * @data: ioctl data
3580 1.1.1.2 riastrad * @file_priv: DRM file info
3581 1.1.1.2 riastrad *
3582 1.1.1.2 riastrad * This function retrieves the contents of a blob property. The value stored in
3583 1.1.1.2 riastrad * an object's blob property is just a normal modeset object id.
3584 1.1.1.2 riastrad *
3585 1.1.1.2 riastrad * Called by the user via ioctl.
3586 1.1.1.2 riastrad *
3587 1.1.1.2 riastrad * Returns:
3588 1.1.1.2 riastrad * Zero on success, errno on failure.
3589 1.1.1.2 riastrad */
3590 1.1 riastrad int drm_mode_getblob_ioctl(struct drm_device *dev,
3591 1.1 riastrad void *data, struct drm_file *file_priv)
3592 1.1 riastrad {
3593 1.1 riastrad struct drm_mode_object *obj;
3594 1.1 riastrad struct drm_mode_get_blob *out_resp = data;
3595 1.1 riastrad struct drm_property_blob *blob;
3596 1.1 riastrad int ret = 0;
3597 1.1 riastrad void __user *blob_ptr;
3598 1.1 riastrad
3599 1.1 riastrad if (!drm_core_check_feature(dev, DRIVER_MODESET))
3600 1.1 riastrad return -EINVAL;
3601 1.1 riastrad
3602 1.1.1.2 riastrad drm_modeset_lock_all(dev);
3603 1.1 riastrad obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
3604 1.1 riastrad if (!obj) {
3605 1.1.1.2 riastrad ret = -ENOENT;
3606 1.1 riastrad goto done;
3607 1.1 riastrad }
3608 1.1 riastrad blob = obj_to_blob(obj);
3609 1.1 riastrad
3610 1.1 riastrad if (out_resp->length == blob->length) {
3611 1.1 riastrad blob_ptr = (void __user *)(unsigned long)out_resp->data;
3612 1.1 riastrad if (copy_to_user(blob_ptr, blob->data, blob->length)){
3613 1.1 riastrad ret = -EFAULT;
3614 1.1 riastrad goto done;
3615 1.1 riastrad }
3616 1.1 riastrad }
3617 1.1 riastrad out_resp->length = blob->length;
3618 1.1 riastrad
3619 1.1 riastrad done:
3620 1.1.1.2 riastrad drm_modeset_unlock_all(dev);
3621 1.1 riastrad return ret;
3622 1.1 riastrad }
3623 1.1 riastrad
3624 1.1.1.2 riastrad /**
3625 1.1.1.2 riastrad * drm_mode_connector_update_edid_property - update the edid property of a connector
3626 1.1.1.2 riastrad * @connector: drm connector
3627 1.1.1.2 riastrad * @edid: new value of the edid property
3628 1.1.1.2 riastrad *
3629 1.1.1.2 riastrad * This function creates a new blob modeset object and assigns its id to the
3630 1.1.1.2 riastrad * connector's edid property.
3631 1.1.1.2 riastrad *
3632 1.1.1.2 riastrad * Returns:
3633 1.1.1.2 riastrad * Zero on success, errno on failure.
3634 1.1.1.2 riastrad */
3635 1.1 riastrad int drm_mode_connector_update_edid_property(struct drm_connector *connector,
3636 1.1 riastrad struct edid *edid)
3637 1.1 riastrad {
3638 1.1 riastrad struct drm_device *dev = connector->dev;
3639 1.1 riastrad int ret, size;
3640 1.1 riastrad
3641 1.1 riastrad if (connector->edid_blob_ptr)
3642 1.1 riastrad drm_property_destroy_blob(dev, connector->edid_blob_ptr);
3643 1.1 riastrad
3644 1.1 riastrad /* Delete edid, when there is none. */
3645 1.1 riastrad if (!edid) {
3646 1.1 riastrad connector->edid_blob_ptr = NULL;
3647 1.1 riastrad ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
3648 1.1 riastrad return ret;
3649 1.1 riastrad }
3650 1.1 riastrad
3651 1.1 riastrad size = EDID_LENGTH * (1 + edid->extensions);
3652 1.1 riastrad connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
3653 1.1 riastrad size, edid);
3654 1.1 riastrad if (!connector->edid_blob_ptr)
3655 1.1 riastrad return -EINVAL;
3656 1.1 riastrad
3657 1.1 riastrad ret = drm_object_property_set_value(&connector->base,
3658 1.1 riastrad dev->mode_config.edid_property,
3659 1.1 riastrad connector->edid_blob_ptr->base.id);
3660 1.1 riastrad
3661 1.1 riastrad return ret;
3662 1.1 riastrad }
3663 1.1 riastrad EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
3664 1.1 riastrad
3665 1.1 riastrad static bool drm_property_change_is_valid(struct drm_property *property,
3666 1.1 riastrad uint64_t value)
3667 1.1 riastrad {
3668 1.1 riastrad if (property->flags & DRM_MODE_PROP_IMMUTABLE)
3669 1.1 riastrad return false;
3670 1.1 riastrad if (property->flags & DRM_MODE_PROP_RANGE) {
3671 1.1 riastrad if (value < property->values[0] || value > property->values[1])
3672 1.1 riastrad return false;
3673 1.1 riastrad return true;
3674 1.1 riastrad } else if (property->flags & DRM_MODE_PROP_BITMASK) {
3675 1.1 riastrad int i;
3676 1.1 riastrad uint64_t valid_mask = 0;
3677 1.1 riastrad for (i = 0; i < property->num_values; i++)
3678 1.1 riastrad valid_mask |= (1ULL << property->values[i]);
3679 1.1 riastrad return !(value & ~valid_mask);
3680 1.1 riastrad } else if (property->flags & DRM_MODE_PROP_BLOB) {
3681 1.1 riastrad /* Only the driver knows */
3682 1.1 riastrad return true;
3683 1.1 riastrad } else {
3684 1.1 riastrad int i;
3685 1.1 riastrad for (i = 0; i < property->num_values; i++)
3686 1.1 riastrad if (property->values[i] == value)
3687 1.1 riastrad return true;
3688 1.1 riastrad return false;
3689 1.1 riastrad }
3690 1.1 riastrad }
3691 1.1 riastrad
3692 1.1.1.2 riastrad /**
3693 1.1.1.2 riastrad * drm_mode_connector_property_set_ioctl - set the current value of a connector property
3694 1.1.1.2 riastrad * @dev: DRM device
3695 1.1.1.2 riastrad * @data: ioctl data
3696 1.1.1.2 riastrad * @file_priv: DRM file info
3697 1.1.1.2 riastrad *
3698 1.1.1.2 riastrad * This function sets the current value for a connectors's property. It also
3699 1.1.1.2 riastrad * calls into a driver's ->set_property callback to update the hardware state
3700 1.1.1.2 riastrad *
3701 1.1.1.2 riastrad * Called by the user via ioctl.
3702 1.1.1.2 riastrad *
3703 1.1.1.2 riastrad * Returns:
3704 1.1.1.2 riastrad * Zero on success, errno on failure.
3705 1.1.1.2 riastrad */
3706 1.1 riastrad int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
3707 1.1 riastrad void *data, struct drm_file *file_priv)
3708 1.1 riastrad {
3709 1.1 riastrad struct drm_mode_connector_set_property *conn_set_prop = data;
3710 1.1 riastrad struct drm_mode_obj_set_property obj_set_prop = {
3711 1.1 riastrad .value = conn_set_prop->value,
3712 1.1 riastrad .prop_id = conn_set_prop->prop_id,
3713 1.1 riastrad .obj_id = conn_set_prop->connector_id,
3714 1.1 riastrad .obj_type = DRM_MODE_OBJECT_CONNECTOR
3715 1.1 riastrad };
3716 1.1 riastrad
3717 1.1 riastrad /* It does all the locking and checking we need */
3718 1.1 riastrad return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
3719 1.1 riastrad }
3720 1.1 riastrad
3721 1.1 riastrad static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
3722 1.1 riastrad struct drm_property *property,
3723 1.1 riastrad uint64_t value)
3724 1.1 riastrad {
3725 1.1 riastrad int ret = -EINVAL;
3726 1.1 riastrad struct drm_connector *connector = obj_to_connector(obj);
3727 1.1 riastrad
3728 1.1 riastrad /* Do DPMS ourselves */
3729 1.1 riastrad if (property == connector->dev->mode_config.dpms_property) {
3730 1.1 riastrad if (connector->funcs->dpms)
3731 1.1 riastrad (*connector->funcs->dpms)(connector, (int)value);
3732 1.1 riastrad ret = 0;
3733 1.1 riastrad } else if (connector->funcs->set_property)
3734 1.1 riastrad ret = connector->funcs->set_property(connector, property, value);
3735 1.1 riastrad
3736 1.1 riastrad /* store the property value if successful */
3737 1.1 riastrad if (!ret)
3738 1.1 riastrad drm_object_property_set_value(&connector->base, property, value);
3739 1.1 riastrad return ret;
3740 1.1 riastrad }
3741 1.1 riastrad
3742 1.1 riastrad static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
3743 1.1 riastrad struct drm_property *property,
3744 1.1 riastrad uint64_t value)
3745 1.1 riastrad {
3746 1.1 riastrad int ret = -EINVAL;
3747 1.1 riastrad struct drm_crtc *crtc = obj_to_crtc(obj);
3748 1.1 riastrad
3749 1.1 riastrad if (crtc->funcs->set_property)
3750 1.1 riastrad ret = crtc->funcs->set_property(crtc, property, value);
3751 1.1 riastrad if (!ret)
3752 1.1 riastrad drm_object_property_set_value(obj, property, value);
3753 1.1 riastrad
3754 1.1 riastrad return ret;
3755 1.1 riastrad }
3756 1.1 riastrad
3757 1.1 riastrad static int drm_mode_plane_set_obj_prop(struct drm_mode_object *obj,
3758 1.1 riastrad struct drm_property *property,
3759 1.1 riastrad uint64_t value)
3760 1.1 riastrad {
3761 1.1 riastrad int ret = -EINVAL;
3762 1.1 riastrad struct drm_plane *plane = obj_to_plane(obj);
3763 1.1 riastrad
3764 1.1 riastrad if (plane->funcs->set_property)
3765 1.1 riastrad ret = plane->funcs->set_property(plane, property, value);
3766 1.1 riastrad if (!ret)
3767 1.1 riastrad drm_object_property_set_value(obj, property, value);
3768 1.1 riastrad
3769 1.1 riastrad return ret;
3770 1.1 riastrad }
3771 1.1 riastrad
3772 1.1.1.2 riastrad /**
3773 1.1.1.2 riastrad * drm_mode_getproperty_ioctl - get the current value of a object's property
3774 1.1.1.2 riastrad * @dev: DRM device
3775 1.1.1.2 riastrad * @data: ioctl data
3776 1.1.1.2 riastrad * @file_priv: DRM file info
3777 1.1.1.2 riastrad *
3778 1.1.1.2 riastrad * This function retrieves the current value for an object's property. Compared
3779 1.1.1.2 riastrad * to the connector specific ioctl this one is extended to also work on crtc and
3780 1.1.1.2 riastrad * plane objects.
3781 1.1.1.2 riastrad *
3782 1.1.1.2 riastrad * Called by the user via ioctl.
3783 1.1.1.2 riastrad *
3784 1.1.1.2 riastrad * Returns:
3785 1.1.1.2 riastrad * Zero on success, errno on failure.
3786 1.1.1.2 riastrad */
3787 1.1 riastrad int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
3788 1.1 riastrad struct drm_file *file_priv)
3789 1.1 riastrad {
3790 1.1 riastrad struct drm_mode_obj_get_properties *arg = data;
3791 1.1 riastrad struct drm_mode_object *obj;
3792 1.1 riastrad int ret = 0;
3793 1.1 riastrad int i;
3794 1.1 riastrad int copied = 0;
3795 1.1 riastrad int props_count = 0;
3796 1.1 riastrad uint32_t __user *props_ptr;
3797 1.1 riastrad uint64_t __user *prop_values_ptr;
3798 1.1 riastrad
3799 1.1 riastrad if (!drm_core_check_feature(dev, DRIVER_MODESET))
3800 1.1 riastrad return -EINVAL;
3801 1.1 riastrad
3802 1.1.1.2 riastrad drm_modeset_lock_all(dev);
3803 1.1 riastrad
3804 1.1 riastrad obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3805 1.1 riastrad if (!obj) {
3806 1.1.1.2 riastrad ret = -ENOENT;
3807 1.1 riastrad goto out;
3808 1.1 riastrad }
3809 1.1 riastrad if (!obj->properties) {
3810 1.1 riastrad ret = -EINVAL;
3811 1.1 riastrad goto out;
3812 1.1 riastrad }
3813 1.1 riastrad
3814 1.1 riastrad props_count = obj->properties->count;
3815 1.1 riastrad
3816 1.1 riastrad /* This ioctl is called twice, once to determine how much space is
3817 1.1 riastrad * needed, and the 2nd time to fill it. */
3818 1.1 riastrad if ((arg->count_props >= props_count) && props_count) {
3819 1.1 riastrad copied = 0;
3820 1.1 riastrad props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
3821 1.1 riastrad prop_values_ptr = (uint64_t __user *)(unsigned long)
3822 1.1 riastrad (arg->prop_values_ptr);
3823 1.1 riastrad for (i = 0; i < props_count; i++) {
3824 1.1 riastrad if (put_user(obj->properties->ids[i],
3825 1.1 riastrad props_ptr + copied)) {
3826 1.1 riastrad ret = -EFAULT;
3827 1.1 riastrad goto out;
3828 1.1 riastrad }
3829 1.1 riastrad if (put_user(obj->properties->values[i],
3830 1.1 riastrad prop_values_ptr + copied)) {
3831 1.1 riastrad ret = -EFAULT;
3832 1.1 riastrad goto out;
3833 1.1 riastrad }
3834 1.1 riastrad copied++;
3835 1.1 riastrad }
3836 1.1 riastrad }
3837 1.1 riastrad arg->count_props = props_count;
3838 1.1 riastrad out:
3839 1.1.1.2 riastrad drm_modeset_unlock_all(dev);
3840 1.1 riastrad return ret;
3841 1.1 riastrad }
3842 1.1 riastrad
3843 1.1.1.2 riastrad /**
3844 1.1.1.2 riastrad * drm_mode_obj_set_property_ioctl - set the current value of an object's property
3845 1.1.1.2 riastrad * @dev: DRM device
3846 1.1.1.2 riastrad * @data: ioctl data
3847 1.1.1.2 riastrad * @file_priv: DRM file info
3848 1.1.1.2 riastrad *
3849 1.1.1.2 riastrad * This function sets the current value for an object's property. It also calls
3850 1.1.1.2 riastrad * into a driver's ->set_property callback to update the hardware state.
3851 1.1.1.2 riastrad * Compared to the connector specific ioctl this one is extended to also work on
3852 1.1.1.2 riastrad * crtc and plane objects.
3853 1.1.1.2 riastrad *
3854 1.1.1.2 riastrad * Called by the user via ioctl.
3855 1.1.1.2 riastrad *
3856 1.1.1.2 riastrad * Returns:
3857 1.1.1.2 riastrad * Zero on success, errno on failure.
3858 1.1.1.2 riastrad */
3859 1.1 riastrad int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
3860 1.1 riastrad struct drm_file *file_priv)
3861 1.1 riastrad {
3862 1.1 riastrad struct drm_mode_obj_set_property *arg = data;
3863 1.1 riastrad struct drm_mode_object *arg_obj;
3864 1.1 riastrad struct drm_mode_object *prop_obj;
3865 1.1 riastrad struct drm_property *property;
3866 1.1 riastrad int ret = -EINVAL;
3867 1.1 riastrad int i;
3868 1.1 riastrad
3869 1.1 riastrad if (!drm_core_check_feature(dev, DRIVER_MODESET))
3870 1.1 riastrad return -EINVAL;
3871 1.1 riastrad
3872 1.1.1.2 riastrad drm_modeset_lock_all(dev);
3873 1.1 riastrad
3874 1.1 riastrad arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3875 1.1.1.2 riastrad if (!arg_obj) {
3876 1.1.1.2 riastrad ret = -ENOENT;
3877 1.1 riastrad goto out;
3878 1.1.1.2 riastrad }
3879 1.1 riastrad if (!arg_obj->properties)
3880 1.1 riastrad goto out;
3881 1.1 riastrad
3882 1.1 riastrad for (i = 0; i < arg_obj->properties->count; i++)
3883 1.1 riastrad if (arg_obj->properties->ids[i] == arg->prop_id)
3884 1.1 riastrad break;
3885 1.1 riastrad
3886 1.1 riastrad if (i == arg_obj->properties->count)
3887 1.1 riastrad goto out;
3888 1.1 riastrad
3889 1.1 riastrad prop_obj = drm_mode_object_find(dev, arg->prop_id,
3890 1.1 riastrad DRM_MODE_OBJECT_PROPERTY);
3891 1.1.1.2 riastrad if (!prop_obj) {
3892 1.1.1.2 riastrad ret = -ENOENT;
3893 1.1 riastrad goto out;
3894 1.1.1.2 riastrad }
3895 1.1 riastrad property = obj_to_property(prop_obj);
3896 1.1 riastrad
3897 1.1 riastrad if (!drm_property_change_is_valid(property, arg->value))
3898 1.1 riastrad goto out;
3899 1.1 riastrad
3900 1.1 riastrad switch (arg_obj->type) {
3901 1.1 riastrad case DRM_MODE_OBJECT_CONNECTOR:
3902 1.1 riastrad ret = drm_mode_connector_set_obj_prop(arg_obj, property,
3903 1.1 riastrad arg->value);
3904 1.1 riastrad break;
3905 1.1 riastrad case DRM_MODE_OBJECT_CRTC:
3906 1.1 riastrad ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
3907 1.1 riastrad break;
3908 1.1 riastrad case DRM_MODE_OBJECT_PLANE:
3909 1.1 riastrad ret = drm_mode_plane_set_obj_prop(arg_obj, property, arg->value);
3910 1.1 riastrad break;
3911 1.1 riastrad }
3912 1.1 riastrad
3913 1.1 riastrad out:
3914 1.1.1.2 riastrad drm_modeset_unlock_all(dev);
3915 1.1 riastrad return ret;
3916 1.1 riastrad }
3917 1.1 riastrad
3918 1.1.1.2 riastrad /**
3919 1.1.1.2 riastrad * drm_mode_connector_attach_encoder - attach a connector to an encoder
3920 1.1.1.2 riastrad * @connector: connector to attach
3921 1.1.1.2 riastrad * @encoder: encoder to attach @connector to
3922 1.1.1.2 riastrad *
3923 1.1.1.2 riastrad * This function links up a connector to an encoder. Note that the routing
3924 1.1.1.2 riastrad * restrictions between encoders and crtcs are exposed to userspace through the
3925 1.1.1.2 riastrad * possible_clones and possible_crtcs bitmasks.
3926 1.1.1.2 riastrad *
3927 1.1.1.2 riastrad * Returns:
3928 1.1.1.2 riastrad * Zero on success, errno on failure.
3929 1.1.1.2 riastrad */
3930 1.1 riastrad int drm_mode_connector_attach_encoder(struct drm_connector *connector,
3931 1.1 riastrad struct drm_encoder *encoder)
3932 1.1 riastrad {
3933 1.1 riastrad int i;
3934 1.1 riastrad
3935 1.1 riastrad for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3936 1.1 riastrad if (connector->encoder_ids[i] == 0) {
3937 1.1 riastrad connector->encoder_ids[i] = encoder->base.id;
3938 1.1 riastrad return 0;
3939 1.1 riastrad }
3940 1.1 riastrad }
3941 1.1 riastrad return -ENOMEM;
3942 1.1 riastrad }
3943 1.1 riastrad EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
3944 1.1 riastrad
3945 1.1.1.2 riastrad /**
3946 1.1.1.2 riastrad * drm_mode_crtc_set_gamma_size - set the gamma table size
3947 1.1.1.2 riastrad * @crtc: CRTC to set the gamma table size for
3948 1.1.1.2 riastrad * @gamma_size: size of the gamma table
3949 1.1.1.2 riastrad *
3950 1.1.1.2 riastrad * Drivers which support gamma tables should set this to the supported gamma
3951 1.1.1.2 riastrad * table size when initializing the CRTC. Currently the drm core only supports a
3952 1.1.1.2 riastrad * fixed gamma table size.
3953 1.1.1.2 riastrad *
3954 1.1.1.2 riastrad * Returns:
3955 1.1.1.2 riastrad * Zero on success, errno on failure.
3956 1.1.1.2 riastrad */
3957 1.1 riastrad int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
3958 1.1.1.2 riastrad int gamma_size)
3959 1.1 riastrad {
3960 1.1 riastrad crtc->gamma_size = gamma_size;
3961 1.1 riastrad
3962 1.1 riastrad crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
3963 1.1 riastrad if (!crtc->gamma_store) {
3964 1.1 riastrad crtc->gamma_size = 0;
3965 1.1 riastrad return -ENOMEM;
3966 1.1 riastrad }
3967 1.1 riastrad
3968 1.1 riastrad return 0;
3969 1.1 riastrad }
3970 1.1 riastrad EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
3971 1.1 riastrad
3972 1.1.1.2 riastrad /**
3973 1.1.1.2 riastrad * drm_mode_gamma_set_ioctl - set the gamma table
3974 1.1.1.2 riastrad * @dev: DRM device
3975 1.1.1.2 riastrad * @data: ioctl data
3976 1.1.1.2 riastrad * @file_priv: DRM file info
3977 1.1.1.2 riastrad *
3978 1.1.1.2 riastrad * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
3979 1.1.1.2 riastrad * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
3980 1.1.1.2 riastrad *
3981 1.1.1.2 riastrad * Called by the user via ioctl.
3982 1.1.1.2 riastrad *
3983 1.1.1.2 riastrad * Returns:
3984 1.1.1.2 riastrad * Zero on success, errno on failure.
3985 1.1.1.2 riastrad */
3986 1.1 riastrad int drm_mode_gamma_set_ioctl(struct drm_device *dev,
3987 1.1 riastrad void *data, struct drm_file *file_priv)
3988 1.1 riastrad {
3989 1.1 riastrad struct drm_mode_crtc_lut *crtc_lut = data;
3990 1.1 riastrad struct drm_mode_object *obj;
3991 1.1 riastrad struct drm_crtc *crtc;
3992 1.1 riastrad void *r_base, *g_base, *b_base;
3993 1.1 riastrad int size;
3994 1.1 riastrad int ret = 0;
3995 1.1 riastrad
3996 1.1 riastrad if (!drm_core_check_feature(dev, DRIVER_MODESET))
3997 1.1 riastrad return -EINVAL;
3998 1.1 riastrad
3999 1.1.1.2 riastrad drm_modeset_lock_all(dev);
4000 1.1 riastrad obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
4001 1.1 riastrad if (!obj) {
4002 1.1.1.2 riastrad ret = -ENOENT;
4003 1.1 riastrad goto out;
4004 1.1 riastrad }
4005 1.1 riastrad crtc = obj_to_crtc(obj);
4006 1.1 riastrad
4007 1.1 riastrad if (crtc->funcs->gamma_set == NULL) {
4008 1.1 riastrad ret = -ENOSYS;
4009 1.1 riastrad goto out;
4010 1.1 riastrad }
4011 1.1 riastrad
4012 1.1 riastrad /* memcpy into gamma store */
4013 1.1 riastrad if (crtc_lut->gamma_size != crtc->gamma_size) {
4014 1.1 riastrad ret = -EINVAL;
4015 1.1 riastrad goto out;
4016 1.1 riastrad }
4017 1.1 riastrad
4018 1.1 riastrad size = crtc_lut->gamma_size * (sizeof(uint16_t));
4019 1.1 riastrad r_base = crtc->gamma_store;
4020 1.1 riastrad if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
4021 1.1 riastrad ret = -EFAULT;
4022 1.1 riastrad goto out;
4023 1.1 riastrad }
4024 1.1 riastrad
4025 1.1 riastrad g_base = r_base + size;
4026 1.1 riastrad if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
4027 1.1 riastrad ret = -EFAULT;
4028 1.1 riastrad goto out;
4029 1.1 riastrad }
4030 1.1 riastrad
4031 1.1 riastrad b_base = g_base + size;
4032 1.1 riastrad if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
4033 1.1 riastrad ret = -EFAULT;
4034 1.1 riastrad goto out;
4035 1.1 riastrad }
4036 1.1 riastrad
4037 1.1 riastrad crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
4038 1.1 riastrad
4039 1.1 riastrad out:
4040 1.1.1.2 riastrad drm_modeset_unlock_all(dev);
4041 1.1 riastrad return ret;
4042 1.1 riastrad
4043 1.1 riastrad }
4044 1.1 riastrad
4045 1.1.1.2 riastrad /**
4046 1.1.1.2 riastrad * drm_mode_gamma_get_ioctl - get the gamma table
4047 1.1.1.2 riastrad * @dev: DRM device
4048 1.1.1.2 riastrad * @data: ioctl data
4049 1.1.1.2 riastrad * @file_priv: DRM file info
4050 1.1.1.2 riastrad *
4051 1.1.1.2 riastrad * Copy the current gamma table into the storage provided. This also provides
4052 1.1.1.2 riastrad * the gamma table size the driver expects, which can be used to size the
4053 1.1.1.2 riastrad * allocated storage.
4054 1.1.1.2 riastrad *
4055 1.1.1.2 riastrad * Called by the user via ioctl.
4056 1.1.1.2 riastrad *
4057 1.1.1.2 riastrad * Returns:
4058 1.1.1.2 riastrad * Zero on success, errno on failure.
4059 1.1.1.2 riastrad */
4060 1.1 riastrad int drm_mode_gamma_get_ioctl(struct drm_device *dev,
4061 1.1 riastrad void *data, struct drm_file *file_priv)
4062 1.1 riastrad {
4063 1.1 riastrad struct drm_mode_crtc_lut *crtc_lut = data;
4064 1.1 riastrad struct drm_mode_object *obj;
4065 1.1 riastrad struct drm_crtc *crtc;
4066 1.1 riastrad void *r_base, *g_base, *b_base;
4067 1.1 riastrad int size;
4068 1.1 riastrad int ret = 0;
4069 1.1 riastrad
4070 1.1 riastrad if (!drm_core_check_feature(dev, DRIVER_MODESET))
4071 1.1 riastrad return -EINVAL;
4072 1.1 riastrad
4073 1.1.1.2 riastrad drm_modeset_lock_all(dev);
4074 1.1 riastrad obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
4075 1.1 riastrad if (!obj) {
4076 1.1.1.2 riastrad ret = -ENOENT;
4077 1.1 riastrad goto out;
4078 1.1 riastrad }
4079 1.1 riastrad crtc = obj_to_crtc(obj);
4080 1.1 riastrad
4081 1.1 riastrad /* memcpy into gamma store */
4082 1.1 riastrad if (crtc_lut->gamma_size != crtc->gamma_size) {
4083 1.1 riastrad ret = -EINVAL;
4084 1.1 riastrad goto out;
4085 1.1 riastrad }
4086 1.1 riastrad
4087 1.1 riastrad size = crtc_lut->gamma_size * (sizeof(uint16_t));
4088 1.1 riastrad r_base = crtc->gamma_store;
4089 1.1 riastrad if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
4090 1.1 riastrad ret = -EFAULT;
4091 1.1 riastrad goto out;
4092 1.1 riastrad }
4093 1.1 riastrad
4094 1.1 riastrad g_base = r_base + size;
4095 1.1 riastrad if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
4096 1.1 riastrad ret = -EFAULT;
4097 1.1 riastrad goto out;
4098 1.1 riastrad }
4099 1.1 riastrad
4100 1.1 riastrad b_base = g_base + size;
4101 1.1 riastrad if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
4102 1.1 riastrad ret = -EFAULT;
4103 1.1 riastrad goto out;
4104 1.1 riastrad }
4105 1.1 riastrad out:
4106 1.1.1.2 riastrad drm_modeset_unlock_all(dev);
4107 1.1 riastrad return ret;
4108 1.1 riastrad }
4109 1.1 riastrad
4110 1.1.1.2 riastrad /**
4111 1.1.1.2 riastrad * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
4112 1.1.1.2 riastrad * @dev: DRM device
4113 1.1.1.2 riastrad * @data: ioctl data
4114 1.1.1.2 riastrad * @file_priv: DRM file info
4115 1.1.1.2 riastrad *
4116 1.1.1.2 riastrad * This schedules an asynchronous update on a given CRTC, called page flip.
4117 1.1.1.2 riastrad * Optionally a drm event is generated to signal the completion of the event.
4118 1.1.1.2 riastrad * Generic drivers cannot assume that a pageflip with changed framebuffer
4119 1.1.1.2 riastrad * properties (including driver specific metadata like tiling layout) will work,
4120 1.1.1.2 riastrad * but some drivers support e.g. pixel format changes through the pageflip
4121 1.1.1.2 riastrad * ioctl.
4122 1.1.1.2 riastrad *
4123 1.1.1.2 riastrad * Called by the user via ioctl.
4124 1.1.1.2 riastrad *
4125 1.1.1.2 riastrad * Returns:
4126 1.1.1.2 riastrad * Zero on success, errno on failure.
4127 1.1.1.2 riastrad */
4128 1.1 riastrad int drm_mode_page_flip_ioctl(struct drm_device *dev,
4129 1.1 riastrad void *data, struct drm_file *file_priv)
4130 1.1 riastrad {
4131 1.1 riastrad struct drm_mode_crtc_page_flip *page_flip = data;
4132 1.1 riastrad struct drm_mode_object *obj;
4133 1.1 riastrad struct drm_crtc *crtc;
4134 1.1.1.2 riastrad struct drm_framebuffer *fb = NULL, *old_fb = NULL;
4135 1.1 riastrad struct drm_pending_vblank_event *e = NULL;
4136 1.1 riastrad unsigned long flags;
4137 1.1 riastrad int ret = -EINVAL;
4138 1.1 riastrad
4139 1.1 riastrad if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
4140 1.1 riastrad page_flip->reserved != 0)
4141 1.1 riastrad return -EINVAL;
4142 1.1 riastrad
4143 1.1.1.2 riastrad if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
4144 1.1.1.2 riastrad return -EINVAL;
4145 1.1.1.2 riastrad
4146 1.1 riastrad obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
4147 1.1 riastrad if (!obj)
4148 1.1.1.2 riastrad return -ENOENT;
4149 1.1 riastrad crtc = obj_to_crtc(obj);
4150 1.1 riastrad
4151 1.1.1.2 riastrad mutex_lock(&crtc->mutex);
4152 1.1.1.2 riastrad if (crtc->primary->fb == NULL) {
4153 1.1 riastrad /* The framebuffer is currently unbound, presumably
4154 1.1 riastrad * due to a hotplug event, that userspace has not
4155 1.1 riastrad * yet discovered.
4156 1.1 riastrad */
4157 1.1 riastrad ret = -EBUSY;
4158 1.1 riastrad goto out;
4159 1.1 riastrad }
4160 1.1 riastrad
4161 1.1 riastrad if (crtc->funcs->page_flip == NULL)
4162 1.1 riastrad goto out;
4163 1.1 riastrad
4164 1.1.1.2 riastrad fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
4165 1.1.1.2 riastrad if (!fb) {
4166 1.1.1.2 riastrad ret = -ENOENT;
4167 1.1 riastrad goto out;
4168 1.1.1.2 riastrad }
4169 1.1 riastrad
4170 1.1.1.2 riastrad ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
4171 1.1.1.2 riastrad if (ret)
4172 1.1.1.2 riastrad goto out;
4173 1.1 riastrad
4174 1.1.1.2 riastrad if (crtc->primary->fb->pixel_format != fb->pixel_format) {
4175 1.1.1.2 riastrad DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
4176 1.1.1.2 riastrad ret = -EINVAL;
4177 1.1 riastrad goto out;
4178 1.1 riastrad }
4179 1.1 riastrad
4180 1.1 riastrad if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
4181 1.1 riastrad ret = -ENOMEM;
4182 1.1 riastrad spin_lock_irqsave(&dev->event_lock, flags);
4183 1.1 riastrad if (file_priv->event_space < sizeof e->event) {
4184 1.1 riastrad spin_unlock_irqrestore(&dev->event_lock, flags);
4185 1.1 riastrad goto out;
4186 1.1 riastrad }
4187 1.1 riastrad file_priv->event_space -= sizeof e->event;
4188 1.1 riastrad spin_unlock_irqrestore(&dev->event_lock, flags);
4189 1.1 riastrad
4190 1.1 riastrad e = kzalloc(sizeof *e, GFP_KERNEL);
4191 1.1 riastrad if (e == NULL) {
4192 1.1 riastrad spin_lock_irqsave(&dev->event_lock, flags);
4193 1.1 riastrad file_priv->event_space += sizeof e->event;
4194 1.1 riastrad spin_unlock_irqrestore(&dev->event_lock, flags);
4195 1.1 riastrad goto out;
4196 1.1 riastrad }
4197 1.1 riastrad
4198 1.1 riastrad e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
4199 1.1 riastrad e->event.base.length = sizeof e->event;
4200 1.1 riastrad e->event.user_data = page_flip->user_data;
4201 1.1 riastrad e->base.event = &e->event.base;
4202 1.1 riastrad e->base.file_priv = file_priv;
4203 1.1 riastrad e->base.destroy =
4204 1.1 riastrad (void (*) (struct drm_pending_event *)) kfree;
4205 1.1 riastrad }
4206 1.1 riastrad
4207 1.1.1.2 riastrad old_fb = crtc->primary->fb;
4208 1.1.1.2 riastrad ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
4209 1.1 riastrad if (ret) {
4210 1.1 riastrad if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
4211 1.1 riastrad spin_lock_irqsave(&dev->event_lock, flags);
4212 1.1 riastrad file_priv->event_space += sizeof e->event;
4213 1.1 riastrad spin_unlock_irqrestore(&dev->event_lock, flags);
4214 1.1 riastrad kfree(e);
4215 1.1 riastrad }
4216 1.1.1.2 riastrad /* Keep the old fb, don't unref it. */
4217 1.1.1.2 riastrad old_fb = NULL;
4218 1.1.1.2 riastrad } else {
4219 1.1.1.2 riastrad /*
4220 1.1.1.2 riastrad * Warn if the driver hasn't properly updated the crtc->fb
4221 1.1.1.2 riastrad * field to reflect that the new framebuffer is now used.
4222 1.1.1.2 riastrad * Failing to do so will screw with the reference counting
4223 1.1.1.2 riastrad * on framebuffers.
4224 1.1.1.2 riastrad */
4225 1.1.1.2 riastrad WARN_ON(crtc->primary->fb != fb);
4226 1.1.1.2 riastrad /* Unref only the old framebuffer. */
4227 1.1.1.2 riastrad fb = NULL;
4228 1.1 riastrad }
4229 1.1 riastrad
4230 1.1 riastrad out:
4231 1.1.1.2 riastrad if (fb)
4232 1.1.1.2 riastrad drm_framebuffer_unreference(fb);
4233 1.1.1.2 riastrad if (old_fb)
4234 1.1.1.2 riastrad drm_framebuffer_unreference(old_fb);
4235 1.1.1.2 riastrad mutex_unlock(&crtc->mutex);
4236 1.1.1.2 riastrad
4237 1.1 riastrad return ret;
4238 1.1 riastrad }
4239 1.1 riastrad
4240 1.1.1.2 riastrad /**
4241 1.1.1.2 riastrad * drm_mode_config_reset - call ->reset callbacks
4242 1.1.1.2 riastrad * @dev: drm device
4243 1.1.1.2 riastrad *
4244 1.1.1.2 riastrad * This functions calls all the crtc's, encoder's and connector's ->reset
4245 1.1.1.2 riastrad * callback. Drivers can use this in e.g. their driver load or resume code to
4246 1.1.1.2 riastrad * reset hardware and software state.
4247 1.1.1.2 riastrad */
4248 1.1 riastrad void drm_mode_config_reset(struct drm_device *dev)
4249 1.1 riastrad {
4250 1.1 riastrad struct drm_crtc *crtc;
4251 1.1 riastrad struct drm_encoder *encoder;
4252 1.1 riastrad struct drm_connector *connector;
4253 1.1 riastrad
4254 1.1 riastrad list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
4255 1.1 riastrad if (crtc->funcs->reset)
4256 1.1 riastrad crtc->funcs->reset(crtc);
4257 1.1 riastrad
4258 1.1 riastrad list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
4259 1.1 riastrad if (encoder->funcs->reset)
4260 1.1 riastrad encoder->funcs->reset(encoder);
4261 1.1 riastrad
4262 1.1 riastrad list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4263 1.1 riastrad connector->status = connector_status_unknown;
4264 1.1 riastrad
4265 1.1 riastrad if (connector->funcs->reset)
4266 1.1 riastrad connector->funcs->reset(connector);
4267 1.1 riastrad }
4268 1.1 riastrad }
4269 1.1 riastrad EXPORT_SYMBOL(drm_mode_config_reset);
4270 1.1 riastrad
4271 1.1.1.2 riastrad /**
4272 1.1.1.2 riastrad * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
4273 1.1.1.2 riastrad * @dev: DRM device
4274 1.1.1.2 riastrad * @data: ioctl data
4275 1.1.1.2 riastrad * @file_priv: DRM file info
4276 1.1.1.2 riastrad *
4277 1.1.1.2 riastrad * This creates a new dumb buffer in the driver's backing storage manager (GEM,
4278 1.1.1.2 riastrad * TTM or something else entirely) and returns the resulting buffer handle. This
4279 1.1.1.2 riastrad * handle can then be wrapped up into a framebuffer modeset object.
4280 1.1.1.2 riastrad *
4281 1.1.1.2 riastrad * Note that userspace is not allowed to use such objects for render
4282 1.1.1.2 riastrad * acceleration - drivers must create their own private ioctls for such a use
4283 1.1.1.2 riastrad * case.
4284 1.1.1.2 riastrad *
4285 1.1.1.2 riastrad * Called by the user via ioctl.
4286 1.1.1.2 riastrad *
4287 1.1.1.2 riastrad * Returns:
4288 1.1.1.2 riastrad * Zero on success, errno on failure.
4289 1.1.1.2 riastrad */
4290 1.1 riastrad int drm_mode_create_dumb_ioctl(struct drm_device *dev,
4291 1.1 riastrad void *data, struct drm_file *file_priv)
4292 1.1 riastrad {
4293 1.1 riastrad struct drm_mode_create_dumb *args = data;
4294 1.1.1.2 riastrad u32 cpp, stride, size;
4295 1.1 riastrad
4296 1.1 riastrad if (!dev->driver->dumb_create)
4297 1.1 riastrad return -ENOSYS;
4298 1.1.1.2 riastrad if (!args->width || !args->height || !args->bpp)
4299 1.1.1.2 riastrad return -EINVAL;
4300 1.1.1.2 riastrad
4301 1.1.1.2 riastrad /* overflow checks for 32bit size calculations */
4302 1.1.1.2 riastrad cpp = DIV_ROUND_UP(args->bpp, 8);
4303 1.1.1.2 riastrad if (cpp > 0xffffffffU / args->width)
4304 1.1.1.2 riastrad return -EINVAL;
4305 1.1.1.2 riastrad stride = cpp * args->width;
4306 1.1.1.2 riastrad if (args->height > 0xffffffffU / stride)
4307 1.1.1.2 riastrad return -EINVAL;
4308 1.1.1.2 riastrad
4309 1.1.1.2 riastrad /* test for wrap-around */
4310 1.1.1.2 riastrad size = args->height * stride;
4311 1.1.1.2 riastrad if (PAGE_ALIGN(size) == 0)
4312 1.1.1.2 riastrad return -EINVAL;
4313 1.1.1.2 riastrad
4314 1.1 riastrad return dev->driver->dumb_create(file_priv, dev, args);
4315 1.1 riastrad }
4316 1.1 riastrad
4317 1.1.1.2 riastrad /**
4318 1.1.1.2 riastrad * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
4319 1.1.1.2 riastrad * @dev: DRM device
4320 1.1.1.2 riastrad * @data: ioctl data
4321 1.1.1.2 riastrad * @file_priv: DRM file info
4322 1.1.1.2 riastrad *
4323 1.1.1.2 riastrad * Allocate an offset in the drm device node's address space to be able to
4324 1.1.1.2 riastrad * memory map a dumb buffer.
4325 1.1.1.2 riastrad *
4326 1.1.1.2 riastrad * Called by the user via ioctl.
4327 1.1.1.2 riastrad *
4328 1.1.1.2 riastrad * Returns:
4329 1.1.1.2 riastrad * Zero on success, errno on failure.
4330 1.1.1.2 riastrad */
4331 1.1 riastrad int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
4332 1.1 riastrad void *data, struct drm_file *file_priv)
4333 1.1 riastrad {
4334 1.1 riastrad struct drm_mode_map_dumb *args = data;
4335 1.1 riastrad
4336 1.1 riastrad /* call driver ioctl to get mmap offset */
4337 1.1 riastrad if (!dev->driver->dumb_map_offset)
4338 1.1 riastrad return -ENOSYS;
4339 1.1 riastrad
4340 1.1 riastrad return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
4341 1.1 riastrad }
4342 1.1 riastrad
4343 1.1.1.2 riastrad /**
4344 1.1.1.2 riastrad * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
4345 1.1.1.2 riastrad * @dev: DRM device
4346 1.1.1.2 riastrad * @data: ioctl data
4347 1.1.1.2 riastrad * @file_priv: DRM file info
4348 1.1.1.2 riastrad *
4349 1.1.1.2 riastrad * This destroys the userspace handle for the given dumb backing storage buffer.
4350 1.1.1.2 riastrad * Since buffer objects must be reference counted in the kernel a buffer object
4351 1.1.1.2 riastrad * won't be immediately freed if a framebuffer modeset object still uses it.
4352 1.1.1.2 riastrad *
4353 1.1.1.2 riastrad * Called by the user via ioctl.
4354 1.1.1.2 riastrad *
4355 1.1.1.2 riastrad * Returns:
4356 1.1.1.2 riastrad * Zero on success, errno on failure.
4357 1.1.1.2 riastrad */
4358 1.1 riastrad int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
4359 1.1 riastrad void *data, struct drm_file *file_priv)
4360 1.1 riastrad {
4361 1.1 riastrad struct drm_mode_destroy_dumb *args = data;
4362 1.1 riastrad
4363 1.1 riastrad if (!dev->driver->dumb_destroy)
4364 1.1 riastrad return -ENOSYS;
4365 1.1 riastrad
4366 1.1 riastrad return dev->driver->dumb_destroy(file_priv, dev, args->handle);
4367 1.1 riastrad }
4368 1.1 riastrad
4369 1.1.1.2 riastrad /**
4370 1.1.1.2 riastrad * drm_fb_get_bpp_depth - get the bpp/depth values for format
4371 1.1.1.2 riastrad * @format: pixel format (DRM_FORMAT_*)
4372 1.1.1.2 riastrad * @depth: storage for the depth value
4373 1.1.1.2 riastrad * @bpp: storage for the bpp value
4374 1.1.1.2 riastrad *
4375 1.1.1.2 riastrad * This only supports RGB formats here for compat with code that doesn't use
4376 1.1.1.2 riastrad * pixel formats directly yet.
4377 1.1 riastrad */
4378 1.1 riastrad void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
4379 1.1 riastrad int *bpp)
4380 1.1 riastrad {
4381 1.1 riastrad switch (format) {
4382 1.1.1.2 riastrad case DRM_FORMAT_C8:
4383 1.1 riastrad case DRM_FORMAT_RGB332:
4384 1.1 riastrad case DRM_FORMAT_BGR233:
4385 1.1 riastrad *depth = 8;
4386 1.1 riastrad *bpp = 8;
4387 1.1 riastrad break;
4388 1.1 riastrad case DRM_FORMAT_XRGB1555:
4389 1.1 riastrad case DRM_FORMAT_XBGR1555:
4390 1.1 riastrad case DRM_FORMAT_RGBX5551:
4391 1.1 riastrad case DRM_FORMAT_BGRX5551:
4392 1.1 riastrad case DRM_FORMAT_ARGB1555:
4393 1.1 riastrad case DRM_FORMAT_ABGR1555:
4394 1.1 riastrad case DRM_FORMAT_RGBA5551:
4395 1.1 riastrad case DRM_FORMAT_BGRA5551:
4396 1.1 riastrad *depth = 15;
4397 1.1 riastrad *bpp = 16;
4398 1.1 riastrad break;
4399 1.1 riastrad case DRM_FORMAT_RGB565:
4400 1.1 riastrad case DRM_FORMAT_BGR565:
4401 1.1 riastrad *depth = 16;
4402 1.1 riastrad *bpp = 16;
4403 1.1 riastrad break;
4404 1.1 riastrad case DRM_FORMAT_RGB888:
4405 1.1 riastrad case DRM_FORMAT_BGR888:
4406 1.1 riastrad *depth = 24;
4407 1.1 riastrad *bpp = 24;
4408 1.1 riastrad break;
4409 1.1 riastrad case DRM_FORMAT_XRGB8888:
4410 1.1 riastrad case DRM_FORMAT_XBGR8888:
4411 1.1 riastrad case DRM_FORMAT_RGBX8888:
4412 1.1 riastrad case DRM_FORMAT_BGRX8888:
4413 1.1 riastrad *depth = 24;
4414 1.1 riastrad *bpp = 32;
4415 1.1 riastrad break;
4416 1.1 riastrad case DRM_FORMAT_XRGB2101010:
4417 1.1 riastrad case DRM_FORMAT_XBGR2101010:
4418 1.1 riastrad case DRM_FORMAT_RGBX1010102:
4419 1.1 riastrad case DRM_FORMAT_BGRX1010102:
4420 1.1 riastrad case DRM_FORMAT_ARGB2101010:
4421 1.1 riastrad case DRM_FORMAT_ABGR2101010:
4422 1.1 riastrad case DRM_FORMAT_RGBA1010102:
4423 1.1 riastrad case DRM_FORMAT_BGRA1010102:
4424 1.1 riastrad *depth = 30;
4425 1.1 riastrad *bpp = 32;
4426 1.1 riastrad break;
4427 1.1 riastrad case DRM_FORMAT_ARGB8888:
4428 1.1 riastrad case DRM_FORMAT_ABGR8888:
4429 1.1 riastrad case DRM_FORMAT_RGBA8888:
4430 1.1 riastrad case DRM_FORMAT_BGRA8888:
4431 1.1 riastrad *depth = 32;
4432 1.1 riastrad *bpp = 32;
4433 1.1 riastrad break;
4434 1.1 riastrad default:
4435 1.1.1.2 riastrad DRM_DEBUG_KMS("unsupported pixel format %s\n",
4436 1.1.1.2 riastrad drm_get_format_name(format));
4437 1.1 riastrad *depth = 0;
4438 1.1 riastrad *bpp = 0;
4439 1.1 riastrad break;
4440 1.1 riastrad }
4441 1.1 riastrad }
4442 1.1 riastrad EXPORT_SYMBOL(drm_fb_get_bpp_depth);
4443 1.1 riastrad
4444 1.1 riastrad /**
4445 1.1 riastrad * drm_format_num_planes - get the number of planes for format
4446 1.1 riastrad * @format: pixel format (DRM_FORMAT_*)
4447 1.1 riastrad *
4448 1.1.1.2 riastrad * Returns:
4449 1.1 riastrad * The number of planes used by the specified pixel format.
4450 1.1 riastrad */
4451 1.1 riastrad int drm_format_num_planes(uint32_t format)
4452 1.1 riastrad {
4453 1.1 riastrad switch (format) {
4454 1.1 riastrad case DRM_FORMAT_YUV410:
4455 1.1 riastrad case DRM_FORMAT_YVU410:
4456 1.1 riastrad case DRM_FORMAT_YUV411:
4457 1.1 riastrad case DRM_FORMAT_YVU411:
4458 1.1 riastrad case DRM_FORMAT_YUV420:
4459 1.1 riastrad case DRM_FORMAT_YVU420:
4460 1.1 riastrad case DRM_FORMAT_YUV422:
4461 1.1 riastrad case DRM_FORMAT_YVU422:
4462 1.1 riastrad case DRM_FORMAT_YUV444:
4463 1.1 riastrad case DRM_FORMAT_YVU444:
4464 1.1 riastrad return 3;
4465 1.1 riastrad case DRM_FORMAT_NV12:
4466 1.1 riastrad case DRM_FORMAT_NV21:
4467 1.1 riastrad case DRM_FORMAT_NV16:
4468 1.1 riastrad case DRM_FORMAT_NV61:
4469 1.1 riastrad case DRM_FORMAT_NV24:
4470 1.1 riastrad case DRM_FORMAT_NV42:
4471 1.1 riastrad return 2;
4472 1.1 riastrad default:
4473 1.1 riastrad return 1;
4474 1.1 riastrad }
4475 1.1 riastrad }
4476 1.1 riastrad EXPORT_SYMBOL(drm_format_num_planes);
4477 1.1 riastrad
4478 1.1 riastrad /**
4479 1.1 riastrad * drm_format_plane_cpp - determine the bytes per pixel value
4480 1.1 riastrad * @format: pixel format (DRM_FORMAT_*)
4481 1.1 riastrad * @plane: plane index
4482 1.1 riastrad *
4483 1.1.1.2 riastrad * Returns:
4484 1.1 riastrad * The bytes per pixel value for the specified plane.
4485 1.1 riastrad */
4486 1.1 riastrad int drm_format_plane_cpp(uint32_t format, int plane)
4487 1.1 riastrad {
4488 1.1 riastrad unsigned int depth;
4489 1.1 riastrad int bpp;
4490 1.1 riastrad
4491 1.1 riastrad if (plane >= drm_format_num_planes(format))
4492 1.1 riastrad return 0;
4493 1.1 riastrad
4494 1.1 riastrad switch (format) {
4495 1.1 riastrad case DRM_FORMAT_YUYV:
4496 1.1 riastrad case DRM_FORMAT_YVYU:
4497 1.1 riastrad case DRM_FORMAT_UYVY:
4498 1.1 riastrad case DRM_FORMAT_VYUY:
4499 1.1 riastrad return 2;
4500 1.1 riastrad case DRM_FORMAT_NV12:
4501 1.1 riastrad case DRM_FORMAT_NV21:
4502 1.1 riastrad case DRM_FORMAT_NV16:
4503 1.1 riastrad case DRM_FORMAT_NV61:
4504 1.1 riastrad case DRM_FORMAT_NV24:
4505 1.1 riastrad case DRM_FORMAT_NV42:
4506 1.1 riastrad return plane ? 2 : 1;
4507 1.1 riastrad case DRM_FORMAT_YUV410:
4508 1.1 riastrad case DRM_FORMAT_YVU410:
4509 1.1 riastrad case DRM_FORMAT_YUV411:
4510 1.1 riastrad case DRM_FORMAT_YVU411:
4511 1.1 riastrad case DRM_FORMAT_YUV420:
4512 1.1 riastrad case DRM_FORMAT_YVU420:
4513 1.1 riastrad case DRM_FORMAT_YUV422:
4514 1.1 riastrad case DRM_FORMAT_YVU422:
4515 1.1 riastrad case DRM_FORMAT_YUV444:
4516 1.1 riastrad case DRM_FORMAT_YVU444:
4517 1.1 riastrad return 1;
4518 1.1 riastrad default:
4519 1.1 riastrad drm_fb_get_bpp_depth(format, &depth, &bpp);
4520 1.1 riastrad return bpp >> 3;
4521 1.1 riastrad }
4522 1.1 riastrad }
4523 1.1 riastrad EXPORT_SYMBOL(drm_format_plane_cpp);
4524 1.1 riastrad
4525 1.1 riastrad /**
4526 1.1 riastrad * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
4527 1.1 riastrad * @format: pixel format (DRM_FORMAT_*)
4528 1.1 riastrad *
4529 1.1.1.2 riastrad * Returns:
4530 1.1 riastrad * The horizontal chroma subsampling factor for the
4531 1.1 riastrad * specified pixel format.
4532 1.1 riastrad */
4533 1.1 riastrad int drm_format_horz_chroma_subsampling(uint32_t format)
4534 1.1 riastrad {
4535 1.1 riastrad switch (format) {
4536 1.1 riastrad case DRM_FORMAT_YUV411:
4537 1.1 riastrad case DRM_FORMAT_YVU411:
4538 1.1 riastrad case DRM_FORMAT_YUV410:
4539 1.1 riastrad case DRM_FORMAT_YVU410:
4540 1.1 riastrad return 4;
4541 1.1 riastrad case DRM_FORMAT_YUYV:
4542 1.1 riastrad case DRM_FORMAT_YVYU:
4543 1.1 riastrad case DRM_FORMAT_UYVY:
4544 1.1 riastrad case DRM_FORMAT_VYUY:
4545 1.1 riastrad case DRM_FORMAT_NV12:
4546 1.1 riastrad case DRM_FORMAT_NV21:
4547 1.1 riastrad case DRM_FORMAT_NV16:
4548 1.1 riastrad case DRM_FORMAT_NV61:
4549 1.1 riastrad case DRM_FORMAT_YUV422:
4550 1.1 riastrad case DRM_FORMAT_YVU422:
4551 1.1 riastrad case DRM_FORMAT_YUV420:
4552 1.1 riastrad case DRM_FORMAT_YVU420:
4553 1.1 riastrad return 2;
4554 1.1 riastrad default:
4555 1.1 riastrad return 1;
4556 1.1 riastrad }
4557 1.1 riastrad }
4558 1.1 riastrad EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
4559 1.1 riastrad
4560 1.1 riastrad /**
4561 1.1 riastrad * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
4562 1.1 riastrad * @format: pixel format (DRM_FORMAT_*)
4563 1.1 riastrad *
4564 1.1.1.2 riastrad * Returns:
4565 1.1 riastrad * The vertical chroma subsampling factor for the
4566 1.1 riastrad * specified pixel format.
4567 1.1 riastrad */
4568 1.1 riastrad int drm_format_vert_chroma_subsampling(uint32_t format)
4569 1.1 riastrad {
4570 1.1 riastrad switch (format) {
4571 1.1 riastrad case DRM_FORMAT_YUV410:
4572 1.1 riastrad case DRM_FORMAT_YVU410:
4573 1.1 riastrad return 4;
4574 1.1 riastrad case DRM_FORMAT_YUV420:
4575 1.1 riastrad case DRM_FORMAT_YVU420:
4576 1.1 riastrad case DRM_FORMAT_NV12:
4577 1.1 riastrad case DRM_FORMAT_NV21:
4578 1.1 riastrad return 2;
4579 1.1 riastrad default:
4580 1.1 riastrad return 1;
4581 1.1 riastrad }
4582 1.1 riastrad }
4583 1.1 riastrad EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
4584 1.1.1.2 riastrad
4585 1.1.1.2 riastrad /**
4586 1.1.1.2 riastrad * drm_mode_config_init - initialize DRM mode_configuration structure
4587 1.1.1.2 riastrad * @dev: DRM device
4588 1.1.1.2 riastrad *
4589 1.1.1.2 riastrad * Initialize @dev's mode_config structure, used for tracking the graphics
4590 1.1.1.2 riastrad * configuration of @dev.
4591 1.1.1.2 riastrad *
4592 1.1.1.2 riastrad * Since this initializes the modeset locks, no locking is possible. Which is no
4593 1.1.1.2 riastrad * problem, since this should happen single threaded at init time. It is the
4594 1.1.1.2 riastrad * driver's problem to ensure this guarantee.
4595 1.1.1.2 riastrad *
4596 1.1.1.2 riastrad */
4597 1.1.1.2 riastrad void drm_mode_config_init(struct drm_device *dev)
4598 1.1.1.2 riastrad {
4599 1.1.1.2 riastrad mutex_init(&dev->mode_config.mutex);
4600 1.1.1.2 riastrad mutex_init(&dev->mode_config.idr_mutex);
4601 1.1.1.2 riastrad mutex_init(&dev->mode_config.fb_lock);
4602 1.1.1.2 riastrad INIT_LIST_HEAD(&dev->mode_config.fb_list);
4603 1.1.1.2 riastrad INIT_LIST_HEAD(&dev->mode_config.crtc_list);
4604 1.1.1.2 riastrad INIT_LIST_HEAD(&dev->mode_config.connector_list);
4605 1.1.1.2 riastrad INIT_LIST_HEAD(&dev->mode_config.bridge_list);
4606 1.1.1.2 riastrad INIT_LIST_HEAD(&dev->mode_config.encoder_list);
4607 1.1.1.2 riastrad INIT_LIST_HEAD(&dev->mode_config.property_list);
4608 1.1.1.2 riastrad INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
4609 1.1.1.2 riastrad INIT_LIST_HEAD(&dev->mode_config.plane_list);
4610 1.1.1.2 riastrad idr_init(&dev->mode_config.crtc_idr);
4611 1.1.1.2 riastrad
4612 1.1.1.2 riastrad drm_modeset_lock_all(dev);
4613 1.1.1.2 riastrad drm_mode_create_standard_connector_properties(dev);
4614 1.1.1.2 riastrad drm_mode_create_standard_plane_properties(dev);
4615 1.1.1.2 riastrad drm_modeset_unlock_all(dev);
4616 1.1.1.2 riastrad
4617 1.1.1.2 riastrad /* Just to be sure */
4618 1.1.1.2 riastrad dev->mode_config.num_fb = 0;
4619 1.1.1.2 riastrad dev->mode_config.num_connector = 0;
4620 1.1.1.2 riastrad dev->mode_config.num_crtc = 0;
4621 1.1.1.2 riastrad dev->mode_config.num_encoder = 0;
4622 1.1.1.2 riastrad dev->mode_config.num_overlay_plane = 0;
4623 1.1.1.2 riastrad dev->mode_config.num_total_plane = 0;
4624 1.1.1.2 riastrad }
4625 1.1.1.2 riastrad EXPORT_SYMBOL(drm_mode_config_init);
4626 1.1.1.2 riastrad
4627 1.1.1.2 riastrad /**
4628 1.1.1.2 riastrad * drm_mode_config_cleanup - free up DRM mode_config info
4629 1.1.1.2 riastrad * @dev: DRM device
4630 1.1.1.2 riastrad *
4631 1.1.1.2 riastrad * Free up all the connectors and CRTCs associated with this DRM device, then
4632 1.1.1.2 riastrad * free up the framebuffers and associated buffer objects.
4633 1.1.1.2 riastrad *
4634 1.1.1.2 riastrad * Note that since this /should/ happen single-threaded at driver/device
4635 1.1.1.2 riastrad * teardown time, no locking is required. It's the driver's job to ensure that
4636 1.1.1.2 riastrad * this guarantee actually holds true.
4637 1.1.1.2 riastrad *
4638 1.1.1.2 riastrad * FIXME: cleanup any dangling user buffer objects too
4639 1.1.1.2 riastrad */
4640 1.1.1.2 riastrad void drm_mode_config_cleanup(struct drm_device *dev)
4641 1.1.1.2 riastrad {
4642 1.1.1.2 riastrad struct drm_connector *connector, *ot;
4643 1.1.1.2 riastrad struct drm_crtc *crtc, *ct;
4644 1.1.1.2 riastrad struct drm_encoder *encoder, *enct;
4645 1.1.1.2 riastrad struct drm_bridge *bridge, *brt;
4646 1.1.1.2 riastrad struct drm_framebuffer *fb, *fbt;
4647 1.1.1.2 riastrad struct drm_property *property, *pt;
4648 1.1.1.2 riastrad struct drm_property_blob *blob, *bt;
4649 1.1.1.2 riastrad struct drm_plane *plane, *plt;
4650 1.1.1.2 riastrad
4651 1.1.1.2 riastrad list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
4652 1.1.1.2 riastrad head) {
4653 1.1.1.2 riastrad encoder->funcs->destroy(encoder);
4654 1.1.1.2 riastrad }
4655 1.1.1.2 riastrad
4656 1.1.1.2 riastrad list_for_each_entry_safe(bridge, brt,
4657 1.1.1.2 riastrad &dev->mode_config.bridge_list, head) {
4658 1.1.1.2 riastrad bridge->funcs->destroy(bridge);
4659 1.1.1.2 riastrad }
4660 1.1.1.2 riastrad
4661 1.1.1.2 riastrad list_for_each_entry_safe(connector, ot,
4662 1.1.1.2 riastrad &dev->mode_config.connector_list, head) {
4663 1.1.1.2 riastrad connector->funcs->destroy(connector);
4664 1.1.1.2 riastrad }
4665 1.1.1.2 riastrad
4666 1.1.1.2 riastrad list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
4667 1.1.1.2 riastrad head) {
4668 1.1.1.2 riastrad drm_property_destroy(dev, property);
4669 1.1.1.2 riastrad }
4670 1.1.1.2 riastrad
4671 1.1.1.2 riastrad list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
4672 1.1.1.2 riastrad head) {
4673 1.1.1.2 riastrad drm_property_destroy_blob(dev, blob);
4674 1.1.1.2 riastrad }
4675 1.1.1.2 riastrad
4676 1.1.1.2 riastrad /*
4677 1.1.1.2 riastrad * Single-threaded teardown context, so it's not required to grab the
4678 1.1.1.2 riastrad * fb_lock to protect against concurrent fb_list access. Contrary, it
4679 1.1.1.2 riastrad * would actually deadlock with the drm_framebuffer_cleanup function.
4680 1.1.1.2 riastrad *
4681 1.1.1.2 riastrad * Also, if there are any framebuffers left, that's a driver leak now,
4682 1.1.1.2 riastrad * so politely WARN about this.
4683 1.1.1.2 riastrad */
4684 1.1.1.2 riastrad WARN_ON(!list_empty(&dev->mode_config.fb_list));
4685 1.1.1.2 riastrad list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
4686 1.1.1.2 riastrad drm_framebuffer_remove(fb);
4687 1.1.1.2 riastrad }
4688 1.1.1.2 riastrad
4689 1.1.1.2 riastrad list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
4690 1.1.1.2 riastrad head) {
4691 1.1.1.2 riastrad plane->funcs->destroy(plane);
4692 1.1.1.2 riastrad }
4693 1.1.1.2 riastrad
4694 1.1.1.2 riastrad list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
4695 1.1.1.2 riastrad crtc->funcs->destroy(crtc);
4696 1.1.1.2 riastrad }
4697 1.1.1.2 riastrad
4698 1.1.1.2 riastrad idr_destroy(&dev->mode_config.crtc_idr);
4699 1.1.1.2 riastrad }
4700 1.1.1.2 riastrad EXPORT_SYMBOL(drm_mode_config_cleanup);
4701