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