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