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