2 * Copyright (c) 2006-2008 Intel Corporation
3 * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
4 * Copyright (c) 2008 Red Hat Inc.
6 * DRM core CRTC related functions
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.
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
28 * Eric Anholt <eric@anholt.net>
29 * Dave Airlie <airlied@linux.ie>
30 * Jesse Barnes <jesse.barnes@intel.com>
32 #include <linux/ctype.h>
33 #include <linux/list.h>
34 #include <linux/slab.h>
35 #include <linux/export.h>
37 #include <drm/drm_crtc.h>
38 #include <drm/drm_edid.h>
39 #include <drm/drm_fourcc.h>
42 * drm_modeset_lock_all - take all modeset locks
45 * This function takes all modeset locks, suitable where a more fine-grained
46 * scheme isn't (yet) implemented.
48 void drm_modeset_lock_all(struct drm_device *dev)
50 struct drm_crtc *crtc;
52 mutex_lock(&dev->mode_config.mutex);
54 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
55 mutex_lock_nest_lock(&crtc->mutex, &dev->mode_config.mutex);
57 EXPORT_SYMBOL(drm_modeset_lock_all);
60 * drm_modeset_unlock_all - drop all modeset locks
63 void drm_modeset_unlock_all(struct drm_device *dev)
65 struct drm_crtc *crtc;
67 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
68 mutex_unlock(&crtc->mutex);
70 mutex_unlock(&dev->mode_config.mutex);
72 EXPORT_SYMBOL(drm_modeset_unlock_all);
75 * drm_warn_on_modeset_not_all_locked - check that all modeset locks are locked
78 void drm_warn_on_modeset_not_all_locked(struct drm_device *dev)
80 struct drm_crtc *crtc;
82 /* Locking is currently fubar in the panic handler. */
86 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
87 WARN_ON(!mutex_is_locked(&crtc->mutex));
89 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
91 EXPORT_SYMBOL(drm_warn_on_modeset_not_all_locked);
93 /* Avoid boilerplate. I'm tired of typing. */
94 #define DRM_ENUM_NAME_FN(fnname, list) \
95 const char *fnname(int val) \
98 for (i = 0; i < ARRAY_SIZE(list); i++) { \
99 if (list[i].type == val) \
100 return list[i].name; \
102 return "(unknown)"; \
108 static const struct drm_prop_enum_list drm_dpms_enum_list[] =
109 { { DRM_MODE_DPMS_ON, "On" },
110 { DRM_MODE_DPMS_STANDBY, "Standby" },
111 { DRM_MODE_DPMS_SUSPEND, "Suspend" },
112 { DRM_MODE_DPMS_OFF, "Off" }
115 DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
118 * Optional properties
120 static const struct drm_prop_enum_list drm_scaling_mode_enum_list[] =
122 { DRM_MODE_SCALE_NONE, "None" },
123 { DRM_MODE_SCALE_FULLSCREEN, "Full" },
124 { DRM_MODE_SCALE_CENTER, "Center" },
125 { DRM_MODE_SCALE_ASPECT, "Full aspect" },
128 static const struct drm_prop_enum_list drm_dithering_mode_enum_list[] =
130 { DRM_MODE_DITHERING_OFF, "Off" },
131 { DRM_MODE_DITHERING_ON, "On" },
132 { DRM_MODE_DITHERING_AUTO, "Automatic" },
136 * Non-global properties, but "required" for certain connectors.
138 static const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] =
140 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
141 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
142 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
145 DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
147 static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] =
149 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
150 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
151 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
154 DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
155 drm_dvi_i_subconnector_enum_list)
157 static const struct drm_prop_enum_list drm_tv_select_enum_list[] =
159 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
160 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
161 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
162 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
163 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
166 DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
168 static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] =
170 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
171 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
172 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
173 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
174 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
177 DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
178 drm_tv_subconnector_enum_list)
180 static const struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
181 { DRM_MODE_DIRTY_OFF, "Off" },
182 { DRM_MODE_DIRTY_ON, "On" },
183 { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
186 struct drm_conn_prop_enum_list {
193 * Connector and encoder types.
195 static struct drm_conn_prop_enum_list drm_connector_enum_list[] =
196 { { DRM_MODE_CONNECTOR_Unknown, "Unknown", 0 },
197 { DRM_MODE_CONNECTOR_VGA, "VGA", 0 },
198 { DRM_MODE_CONNECTOR_DVII, "DVI-I", 0 },
199 { DRM_MODE_CONNECTOR_DVID, "DVI-D", 0 },
200 { DRM_MODE_CONNECTOR_DVIA, "DVI-A", 0 },
201 { DRM_MODE_CONNECTOR_Composite, "Composite", 0 },
202 { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO", 0 },
203 { DRM_MODE_CONNECTOR_LVDS, "LVDS", 0 },
204 { DRM_MODE_CONNECTOR_Component, "Component", 0 },
205 { DRM_MODE_CONNECTOR_9PinDIN, "DIN", 0 },
206 { DRM_MODE_CONNECTOR_DisplayPort, "DP", 0 },
207 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A", 0 },
208 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B", 0 },
209 { DRM_MODE_CONNECTOR_TV, "TV", 0 },
210 { DRM_MODE_CONNECTOR_eDP, "eDP", 0 },
211 { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual", 0},
214 static const struct drm_prop_enum_list drm_encoder_enum_list[] =
215 { { DRM_MODE_ENCODER_NONE, "None" },
216 { DRM_MODE_ENCODER_DAC, "DAC" },
217 { DRM_MODE_ENCODER_TMDS, "TMDS" },
218 { DRM_MODE_ENCODER_LVDS, "LVDS" },
219 { DRM_MODE_ENCODER_TVDAC, "TV" },
220 { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
223 const char *drm_get_encoder_name(const struct drm_encoder *encoder)
227 snprintf(buf, 32, "%s-%d",
228 drm_encoder_enum_list[encoder->encoder_type].name,
232 EXPORT_SYMBOL(drm_get_encoder_name);
234 const char *drm_get_connector_name(const struct drm_connector *connector)
238 snprintf(buf, 32, "%s-%d",
239 drm_connector_enum_list[connector->connector_type].name,
240 connector->connector_type_id);
243 EXPORT_SYMBOL(drm_get_connector_name);
245 const char *drm_get_connector_status_name(enum drm_connector_status status)
247 if (status == connector_status_connected)
249 else if (status == connector_status_disconnected)
250 return "disconnected";
254 EXPORT_SYMBOL(drm_get_connector_status_name);
256 static char printable_char(int c)
258 return isascii(c) && isprint(c) ? c : '?';
261 const char *drm_get_format_name(uint32_t format)
265 snprintf(buf, sizeof(buf),
266 "%c%c%c%c %s-endian (0x%08x)",
267 printable_char(format & 0xff),
268 printable_char((format >> 8) & 0xff),
269 printable_char((format >> 16) & 0xff),
270 printable_char((format >> 24) & 0x7f),
271 format & DRM_FORMAT_BIG_ENDIAN ? "big" : "little",
276 EXPORT_SYMBOL(drm_get_format_name);
279 * drm_mode_object_get - allocate a new modeset identifier
281 * @obj: object pointer, used to generate unique ID
282 * @obj_type: object type
284 * Create a unique identifier based on @ptr in @dev's identifier space. Used
285 * for tracking modes, CRTCs and connectors.
288 * New unique (relative to other objects in @dev) integer identifier for the
291 static int drm_mode_object_get(struct drm_device *dev,
292 struct drm_mode_object *obj, uint32_t obj_type)
296 mutex_lock(&dev->mode_config.idr_mutex);
297 ret = idr_alloc(&dev->mode_config.crtc_idr, obj, 1, 0, GFP_KERNEL);
300 * Set up the object linking under the protection of the idr
301 * lock so that other users can't see inconsistent state.
304 obj->type = obj_type;
306 mutex_unlock(&dev->mode_config.idr_mutex);
308 return ret < 0 ? ret : 0;
312 * drm_mode_object_put - free a modeset identifer
314 * @object: object to free
316 * Free @id from @dev's unique identifier pool.
318 static void drm_mode_object_put(struct drm_device *dev,
319 struct drm_mode_object *object)
321 mutex_lock(&dev->mode_config.idr_mutex);
322 idr_remove(&dev->mode_config.crtc_idr, object->id);
323 mutex_unlock(&dev->mode_config.idr_mutex);
327 * drm_mode_object_find - look up a drm object with static lifetime
329 * @id: id of the mode object
330 * @type: type of the mode object
332 * Note that framebuffers cannot be looked up with this functions - since those
333 * are reference counted, they need special treatment.
335 struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
336 uint32_t id, uint32_t type)
338 struct drm_mode_object *obj = NULL;
340 /* Framebuffers are reference counted and need their own lookup
342 WARN_ON(type == DRM_MODE_OBJECT_FB);
344 mutex_lock(&dev->mode_config.idr_mutex);
345 obj = idr_find(&dev->mode_config.crtc_idr, id);
346 if (!obj || (obj->type != type) || (obj->id != id))
348 mutex_unlock(&dev->mode_config.idr_mutex);
352 EXPORT_SYMBOL(drm_mode_object_find);
355 * drm_framebuffer_init - initialize a framebuffer
357 * @fb: framebuffer to be initialized
358 * @funcs: ... with these functions
360 * Allocates an ID for the framebuffer's parent mode object, sets its mode
361 * functions & device file and adds it to the master fd list.
364 * This functions publishes the fb and makes it available for concurrent access
365 * by other users. Which means by this point the fb _must_ be fully set up -
366 * since all the fb attributes are invariant over its lifetime, no further
367 * locking but only correct reference counting is required.
370 * Zero on success, error code on failure.
372 int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
373 const struct drm_framebuffer_funcs *funcs)
377 mutex_lock(&dev->mode_config.fb_lock);
378 kref_init(&fb->refcount);
379 INIT_LIST_HEAD(&fb->filp_head);
383 ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
387 /* Grab the idr reference. */
388 drm_framebuffer_reference(fb);
390 dev->mode_config.num_fb++;
391 list_add(&fb->head, &dev->mode_config.fb_list);
393 mutex_unlock(&dev->mode_config.fb_lock);
397 EXPORT_SYMBOL(drm_framebuffer_init);
399 static void drm_framebuffer_free(struct kref *kref)
401 struct drm_framebuffer *fb =
402 container_of(kref, struct drm_framebuffer, refcount);
403 fb->funcs->destroy(fb);
406 static struct drm_framebuffer *__drm_framebuffer_lookup(struct drm_device *dev,
409 struct drm_mode_object *obj = NULL;
410 struct drm_framebuffer *fb;
412 mutex_lock(&dev->mode_config.idr_mutex);
413 obj = idr_find(&dev->mode_config.crtc_idr, id);
414 if (!obj || (obj->type != DRM_MODE_OBJECT_FB) || (obj->id != id))
418 mutex_unlock(&dev->mode_config.idr_mutex);
424 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
426 * @id: id of the fb object
428 * If successful, this grabs an additional reference to the framebuffer -
429 * callers need to make sure to eventually unreference the returned framebuffer
432 struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
435 struct drm_framebuffer *fb;
437 mutex_lock(&dev->mode_config.fb_lock);
438 fb = __drm_framebuffer_lookup(dev, id);
440 drm_framebuffer_reference(fb);
441 mutex_unlock(&dev->mode_config.fb_lock);
445 EXPORT_SYMBOL(drm_framebuffer_lookup);
448 * drm_framebuffer_unreference - unref a framebuffer
449 * @fb: framebuffer to unref
451 * This functions decrements the fb's refcount and frees it if it drops to zero.
453 void drm_framebuffer_unreference(struct drm_framebuffer *fb)
455 DRM_DEBUG("FB ID: %d\n", fb->base.id);
456 kref_put(&fb->refcount, drm_framebuffer_free);
458 EXPORT_SYMBOL(drm_framebuffer_unreference);
461 * drm_framebuffer_reference - incr the fb refcnt
464 void drm_framebuffer_reference(struct drm_framebuffer *fb)
466 DRM_DEBUG("FB ID: %d\n", fb->base.id);
467 kref_get(&fb->refcount);
469 EXPORT_SYMBOL(drm_framebuffer_reference);
471 static void drm_framebuffer_free_bug(struct kref *kref)
476 static void __drm_framebuffer_unreference(struct drm_framebuffer *fb)
478 DRM_DEBUG("FB ID: %d\n", fb->base.id);
479 kref_put(&fb->refcount, drm_framebuffer_free_bug);
482 /* dev->mode_config.fb_lock must be held! */
483 static void __drm_framebuffer_unregister(struct drm_device *dev,
484 struct drm_framebuffer *fb)
486 mutex_lock(&dev->mode_config.idr_mutex);
487 idr_remove(&dev->mode_config.crtc_idr, fb->base.id);
488 mutex_unlock(&dev->mode_config.idr_mutex);
492 __drm_framebuffer_unreference(fb);
496 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
497 * @fb: fb to unregister
499 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
500 * those used for fbdev. Note that the caller must hold a reference of it's own,
501 * i.e. the object may not be destroyed through this call (since it'll lead to a
502 * locking inversion).
504 void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
506 struct drm_device *dev = fb->dev;
508 mutex_lock(&dev->mode_config.fb_lock);
509 /* Mark fb as reaped and drop idr ref. */
510 __drm_framebuffer_unregister(dev, fb);
511 mutex_unlock(&dev->mode_config.fb_lock);
513 EXPORT_SYMBOL(drm_framebuffer_unregister_private);
516 * drm_framebuffer_cleanup - remove a framebuffer object
517 * @fb: framebuffer to remove
519 * Cleanup references to a user-created framebuffer. This function is intended
520 * to be used from the drivers ->destroy callback.
522 * Note that this function does not remove the fb from active usuage - if it is
523 * still used anywhere, hilarity can ensue since userspace could call getfb on
524 * the id and get back -EINVAL. Obviously no concern at driver unload time.
526 * Also, the framebuffer will not be removed from the lookup idr - for
527 * user-created framebuffers this will happen in in the rmfb ioctl. For
528 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
529 * drm_framebuffer_unregister_private.
531 void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
533 struct drm_device *dev = fb->dev;
535 mutex_lock(&dev->mode_config.fb_lock);
537 dev->mode_config.num_fb--;
538 mutex_unlock(&dev->mode_config.fb_lock);
540 EXPORT_SYMBOL(drm_framebuffer_cleanup);
543 * drm_framebuffer_remove - remove and unreference a framebuffer object
544 * @fb: framebuffer to remove
546 * Scans all the CRTCs and planes in @dev's mode_config. If they're
547 * using @fb, removes it, setting it to NULL. Then drops the reference to the
548 * passed-in framebuffer. Might take the modeset locks.
550 * Note that this function optimizes the cleanup away if the caller holds the
551 * last reference to the framebuffer. It is also guaranteed to not take the
552 * modeset locks in this case.
554 void drm_framebuffer_remove(struct drm_framebuffer *fb)
556 struct drm_device *dev = fb->dev;
557 struct drm_crtc *crtc;
558 struct drm_plane *plane;
559 struct drm_mode_set set;
562 WARN_ON(!list_empty(&fb->filp_head));
565 * drm ABI mandates that we remove any deleted framebuffers from active
566 * useage. But since most sane clients only remove framebuffers they no
567 * longer need, try to optimize this away.
569 * Since we're holding a reference ourselves, observing a refcount of 1
570 * means that we're the last holder and can skip it. Also, the refcount
571 * can never increase from 1 again, so we don't need any barriers or
574 * Note that userspace could try to race with use and instate a new
575 * usage _after_ we've cleared all current ones. End result will be an
576 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
579 if (atomic_read(&fb->refcount.refcount) > 1) {
580 drm_modeset_lock_all(dev);
581 /* remove from any CRTC */
582 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
583 if (crtc->fb == fb) {
584 /* should turn off the crtc */
585 memset(&set, 0, sizeof(struct drm_mode_set));
588 ret = drm_mode_set_config_internal(&set);
590 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
594 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
595 if (plane->fb == fb) {
596 /* should turn off the crtc */
597 ret = plane->funcs->disable_plane(plane);
599 DRM_ERROR("failed to disable plane with busy fb\n");
600 /* disconnect the plane from the fb and crtc: */
601 __drm_framebuffer_unreference(plane->fb);
606 drm_modeset_unlock_all(dev);
609 drm_framebuffer_unreference(fb);
611 EXPORT_SYMBOL(drm_framebuffer_remove);
614 * drm_crtc_init - Initialise a new CRTC object
616 * @crtc: CRTC object to init
617 * @funcs: callbacks for the new CRTC
619 * Inits a new object created as base part of a driver crtc object.
622 * Zero on success, error code on failure.
624 int drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
625 const struct drm_crtc_funcs *funcs)
631 crtc->invert_dimensions = false;
633 drm_modeset_lock_all(dev);
634 mutex_init(&crtc->mutex);
635 mutex_lock_nest_lock(&crtc->mutex, &dev->mode_config.mutex);
637 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
641 crtc->base.properties = &crtc->properties;
643 list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
644 dev->mode_config.num_crtc++;
647 drm_modeset_unlock_all(dev);
651 EXPORT_SYMBOL(drm_crtc_init);
654 * drm_crtc_cleanup - Clean up the core crtc usage
655 * @crtc: CRTC to cleanup
657 * This function cleans up @crtc and removes it from the DRM mode setting
658 * core. Note that the function does *not* free the crtc structure itself,
659 * this is the responsibility of the caller.
661 void drm_crtc_cleanup(struct drm_crtc *crtc)
663 struct drm_device *dev = crtc->dev;
665 kfree(crtc->gamma_store);
666 crtc->gamma_store = NULL;
668 drm_mode_object_put(dev, &crtc->base);
669 list_del(&crtc->head);
670 dev->mode_config.num_crtc--;
672 EXPORT_SYMBOL(drm_crtc_cleanup);
675 * drm_mode_probed_add - add a mode to a connector's probed mode list
676 * @connector: connector the new mode
679 * Add @mode to @connector's mode list for later use.
681 void drm_mode_probed_add(struct drm_connector *connector,
682 struct drm_display_mode *mode)
684 list_add_tail(&mode->head, &connector->probed_modes);
686 EXPORT_SYMBOL(drm_mode_probed_add);
689 * drm_mode_remove - remove and free a mode
690 * @connector: connector list to modify
691 * @mode: mode to remove
693 * Remove @mode from @connector's mode list, then free it.
695 void drm_mode_remove(struct drm_connector *connector,
696 struct drm_display_mode *mode)
698 list_del(&mode->head);
699 drm_mode_destroy(connector->dev, mode);
701 EXPORT_SYMBOL(drm_mode_remove);
704 * drm_connector_init - Init a preallocated connector
706 * @connector: the connector to init
707 * @funcs: callbacks for this connector
708 * @connector_type: user visible type of the connector
710 * Initialises a preallocated connector. Connectors should be
711 * subclassed as part of driver connector objects.
714 * Zero on success, error code on failure.
716 int drm_connector_init(struct drm_device *dev,
717 struct drm_connector *connector,
718 const struct drm_connector_funcs *funcs,
723 drm_modeset_lock_all(dev);
725 ret = drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
729 connector->base.properties = &connector->properties;
730 connector->dev = dev;
731 connector->funcs = funcs;
732 connector->connector_type = connector_type;
733 connector->connector_type_id =
734 ++drm_connector_enum_list[connector_type].count; /* TODO */
735 INIT_LIST_HEAD(&connector->probed_modes);
736 INIT_LIST_HEAD(&connector->modes);
737 connector->edid_blob_ptr = NULL;
738 connector->status = connector_status_unknown;
740 list_add_tail(&connector->head, &dev->mode_config.connector_list);
741 dev->mode_config.num_connector++;
743 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
744 drm_object_attach_property(&connector->base,
745 dev->mode_config.edid_property,
748 drm_object_attach_property(&connector->base,
749 dev->mode_config.dpms_property, 0);
752 drm_modeset_unlock_all(dev);
756 EXPORT_SYMBOL(drm_connector_init);
759 * drm_connector_cleanup - cleans up an initialised connector
760 * @connector: connector to cleanup
762 * Cleans up the connector but doesn't free the object.
764 void drm_connector_cleanup(struct drm_connector *connector)
766 struct drm_device *dev = connector->dev;
767 struct drm_display_mode *mode, *t;
769 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
770 drm_mode_remove(connector, mode);
772 list_for_each_entry_safe(mode, t, &connector->modes, head)
773 drm_mode_remove(connector, mode);
775 drm_mode_object_put(dev, &connector->base);
776 list_del(&connector->head);
777 dev->mode_config.num_connector--;
779 EXPORT_SYMBOL(drm_connector_cleanup);
781 void drm_connector_unplug_all(struct drm_device *dev)
783 struct drm_connector *connector;
785 /* taking the mode config mutex ends up in a clash with sysfs */
786 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
787 drm_sysfs_connector_remove(connector);
790 EXPORT_SYMBOL(drm_connector_unplug_all);
792 int drm_encoder_init(struct drm_device *dev,
793 struct drm_encoder *encoder,
794 const struct drm_encoder_funcs *funcs,
799 drm_modeset_lock_all(dev);
801 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
806 encoder->encoder_type = encoder_type;
807 encoder->funcs = funcs;
809 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
810 dev->mode_config.num_encoder++;
813 drm_modeset_unlock_all(dev);
817 EXPORT_SYMBOL(drm_encoder_init);
819 void drm_encoder_cleanup(struct drm_encoder *encoder)
821 struct drm_device *dev = encoder->dev;
822 drm_modeset_lock_all(dev);
823 drm_mode_object_put(dev, &encoder->base);
824 list_del(&encoder->head);
825 dev->mode_config.num_encoder--;
826 drm_modeset_unlock_all(dev);
828 EXPORT_SYMBOL(drm_encoder_cleanup);
830 int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
831 unsigned long possible_crtcs,
832 const struct drm_plane_funcs *funcs,
833 const uint32_t *formats, uint32_t format_count,
838 drm_modeset_lock_all(dev);
840 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
844 plane->base.properties = &plane->properties;
846 plane->funcs = funcs;
847 plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
849 if (!plane->format_types) {
850 DRM_DEBUG_KMS("out of memory when allocating plane\n");
851 drm_mode_object_put(dev, &plane->base);
856 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
857 plane->format_count = format_count;
858 plane->possible_crtcs = possible_crtcs;
860 /* private planes are not exposed to userspace, but depending on
861 * display hardware, might be convenient to allow sharing programming
862 * for the scanout engine with the crtc implementation.
865 list_add_tail(&plane->head, &dev->mode_config.plane_list);
866 dev->mode_config.num_plane++;
868 INIT_LIST_HEAD(&plane->head);
872 drm_modeset_unlock_all(dev);
876 EXPORT_SYMBOL(drm_plane_init);
878 void drm_plane_cleanup(struct drm_plane *plane)
880 struct drm_device *dev = plane->dev;
882 drm_modeset_lock_all(dev);
883 kfree(plane->format_types);
884 drm_mode_object_put(dev, &plane->base);
885 /* if not added to a list, it must be a private plane */
886 if (!list_empty(&plane->head)) {
887 list_del(&plane->head);
888 dev->mode_config.num_plane--;
890 drm_modeset_unlock_all(dev);
892 EXPORT_SYMBOL(drm_plane_cleanup);
895 * drm_mode_create - create a new display mode
898 * Create a new drm_display_mode, give it an ID, and return it.
901 * Pointer to new mode on success, NULL on error.
903 struct drm_display_mode *drm_mode_create(struct drm_device *dev)
905 struct drm_display_mode *nmode;
907 nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
911 if (drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE)) {
918 EXPORT_SYMBOL(drm_mode_create);
921 * drm_mode_destroy - remove a mode
923 * @mode: mode to remove
925 * Free @mode's unique identifier, then free it.
927 void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
932 drm_mode_object_put(dev, &mode->base);
936 EXPORT_SYMBOL(drm_mode_destroy);
938 static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
940 struct drm_property *edid;
941 struct drm_property *dpms;
944 * Standard properties (apply to all connectors)
946 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
947 DRM_MODE_PROP_IMMUTABLE,
949 dev->mode_config.edid_property = edid;
951 dpms = drm_property_create_enum(dev, 0,
952 "DPMS", drm_dpms_enum_list,
953 ARRAY_SIZE(drm_dpms_enum_list));
954 dev->mode_config.dpms_property = dpms;
960 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
963 * Called by a driver the first time a DVI-I connector is made.
965 int drm_mode_create_dvi_i_properties(struct drm_device *dev)
967 struct drm_property *dvi_i_selector;
968 struct drm_property *dvi_i_subconnector;
970 if (dev->mode_config.dvi_i_select_subconnector_property)
974 drm_property_create_enum(dev, 0,
975 "select subconnector",
976 drm_dvi_i_select_enum_list,
977 ARRAY_SIZE(drm_dvi_i_select_enum_list));
978 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
980 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
982 drm_dvi_i_subconnector_enum_list,
983 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
984 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
988 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
991 * drm_create_tv_properties - create TV specific connector properties
993 * @num_modes: number of different TV formats (modes) supported
994 * @modes: array of pointers to strings containing name of each format
996 * Called by a driver's TV initialization routine, this function creates
997 * the TV specific connector properties for a given device. Caller is
998 * responsible for allocating a list of format names and passing them to
1001 int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
1004 struct drm_property *tv_selector;
1005 struct drm_property *tv_subconnector;
1008 if (dev->mode_config.tv_select_subconnector_property)
1012 * Basic connector properties
1014 tv_selector = drm_property_create_enum(dev, 0,
1015 "select subconnector",
1016 drm_tv_select_enum_list,
1017 ARRAY_SIZE(drm_tv_select_enum_list));
1018 dev->mode_config.tv_select_subconnector_property = tv_selector;
1021 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1023 drm_tv_subconnector_enum_list,
1024 ARRAY_SIZE(drm_tv_subconnector_enum_list));
1025 dev->mode_config.tv_subconnector_property = tv_subconnector;
1028 * Other, TV specific properties: margins & TV modes.
1030 dev->mode_config.tv_left_margin_property =
1031 drm_property_create_range(dev, 0, "left margin", 0, 100);
1033 dev->mode_config.tv_right_margin_property =
1034 drm_property_create_range(dev, 0, "right margin", 0, 100);
1036 dev->mode_config.tv_top_margin_property =
1037 drm_property_create_range(dev, 0, "top margin", 0, 100);
1039 dev->mode_config.tv_bottom_margin_property =
1040 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
1042 dev->mode_config.tv_mode_property =
1043 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1045 for (i = 0; i < num_modes; i++)
1046 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1049 dev->mode_config.tv_brightness_property =
1050 drm_property_create_range(dev, 0, "brightness", 0, 100);
1052 dev->mode_config.tv_contrast_property =
1053 drm_property_create_range(dev, 0, "contrast", 0, 100);
1055 dev->mode_config.tv_flicker_reduction_property =
1056 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
1058 dev->mode_config.tv_overscan_property =
1059 drm_property_create_range(dev, 0, "overscan", 0, 100);
1061 dev->mode_config.tv_saturation_property =
1062 drm_property_create_range(dev, 0, "saturation", 0, 100);
1064 dev->mode_config.tv_hue_property =
1065 drm_property_create_range(dev, 0, "hue", 0, 100);
1069 EXPORT_SYMBOL(drm_mode_create_tv_properties);
1072 * drm_mode_create_scaling_mode_property - create scaling mode property
1075 * Called by a driver the first time it's needed, must be attached to desired
1078 int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1080 struct drm_property *scaling_mode;
1082 if (dev->mode_config.scaling_mode_property)
1086 drm_property_create_enum(dev, 0, "scaling mode",
1087 drm_scaling_mode_enum_list,
1088 ARRAY_SIZE(drm_scaling_mode_enum_list));
1090 dev->mode_config.scaling_mode_property = scaling_mode;
1094 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1097 * drm_mode_create_dithering_property - create dithering property
1100 * Called by a driver the first time it's needed, must be attached to desired
1103 int drm_mode_create_dithering_property(struct drm_device *dev)
1105 struct drm_property *dithering_mode;
1107 if (dev->mode_config.dithering_mode_property)
1111 drm_property_create_enum(dev, 0, "dithering",
1112 drm_dithering_mode_enum_list,
1113 ARRAY_SIZE(drm_dithering_mode_enum_list));
1114 dev->mode_config.dithering_mode_property = dithering_mode;
1118 EXPORT_SYMBOL(drm_mode_create_dithering_property);
1121 * drm_mode_create_dirty_property - create dirty property
1124 * Called by a driver the first time it's needed, must be attached to desired
1127 int drm_mode_create_dirty_info_property(struct drm_device *dev)
1129 struct drm_property *dirty_info;
1131 if (dev->mode_config.dirty_info_property)
1135 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1137 drm_dirty_info_enum_list,
1138 ARRAY_SIZE(drm_dirty_info_enum_list));
1139 dev->mode_config.dirty_info_property = dirty_info;
1143 EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1145 static int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
1147 uint32_t total_objects = 0;
1149 total_objects += dev->mode_config.num_crtc;
1150 total_objects += dev->mode_config.num_connector;
1151 total_objects += dev->mode_config.num_encoder;
1153 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
1154 if (!group->id_list)
1157 group->num_crtcs = 0;
1158 group->num_connectors = 0;
1159 group->num_encoders = 0;
1163 int drm_mode_group_init_legacy_group(struct drm_device *dev,
1164 struct drm_mode_group *group)
1166 struct drm_crtc *crtc;
1167 struct drm_encoder *encoder;
1168 struct drm_connector *connector;
1171 if ((ret = drm_mode_group_init(dev, group)))
1174 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
1175 group->id_list[group->num_crtcs++] = crtc->base.id;
1177 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
1178 group->id_list[group->num_crtcs + group->num_encoders++] =
1181 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1182 group->id_list[group->num_crtcs + group->num_encoders +
1183 group->num_connectors++] = connector->base.id;
1187 EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
1190 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1191 * @out: drm_mode_modeinfo struct to return to the user
1192 * @in: drm_display_mode to use
1194 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1197 static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1198 const struct drm_display_mode *in)
1200 WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1201 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1202 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1203 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1204 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1205 "timing values too large for mode info\n");
1207 out->clock = in->clock;
1208 out->hdisplay = in->hdisplay;
1209 out->hsync_start = in->hsync_start;
1210 out->hsync_end = in->hsync_end;
1211 out->htotal = in->htotal;
1212 out->hskew = in->hskew;
1213 out->vdisplay = in->vdisplay;
1214 out->vsync_start = in->vsync_start;
1215 out->vsync_end = in->vsync_end;
1216 out->vtotal = in->vtotal;
1217 out->vscan = in->vscan;
1218 out->vrefresh = in->vrefresh;
1219 out->flags = in->flags;
1220 out->type = in->type;
1221 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1222 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1226 * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
1227 * @out: drm_display_mode to return to the user
1228 * @in: drm_mode_modeinfo to use
1230 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1234 * Zero on success, errno on failure.
1236 static int drm_crtc_convert_umode(struct drm_display_mode *out,
1237 const struct drm_mode_modeinfo *in)
1239 if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1242 out->clock = in->clock;
1243 out->hdisplay = in->hdisplay;
1244 out->hsync_start = in->hsync_start;
1245 out->hsync_end = in->hsync_end;
1246 out->htotal = in->htotal;
1247 out->hskew = in->hskew;
1248 out->vdisplay = in->vdisplay;
1249 out->vsync_start = in->vsync_start;
1250 out->vsync_end = in->vsync_end;
1251 out->vtotal = in->vtotal;
1252 out->vscan = in->vscan;
1253 out->vrefresh = in->vrefresh;
1254 out->flags = in->flags;
1255 out->type = in->type;
1256 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1257 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1263 * drm_mode_getresources - get graphics configuration
1264 * @dev: drm device for the ioctl
1265 * @data: data pointer for the ioctl
1266 * @file_priv: drm file for the ioctl call
1268 * Construct a set of configuration description structures and return
1269 * them to the user, including CRTC, connector and framebuffer configuration.
1271 * Called by the user via ioctl.
1274 * Zero on success, errno on failure.
1276 int drm_mode_getresources(struct drm_device *dev, void *data,
1277 struct drm_file *file_priv)
1279 struct drm_mode_card_res *card_res = data;
1280 struct list_head *lh;
1281 struct drm_framebuffer *fb;
1282 struct drm_connector *connector;
1283 struct drm_crtc *crtc;
1284 struct drm_encoder *encoder;
1286 int connector_count = 0;
1289 int encoder_count = 0;
1291 uint32_t __user *fb_id;
1292 uint32_t __user *crtc_id;
1293 uint32_t __user *connector_id;
1294 uint32_t __user *encoder_id;
1295 struct drm_mode_group *mode_group;
1297 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1301 mutex_lock(&file_priv->fbs_lock);
1303 * For the non-control nodes we need to limit the list of resources
1304 * by IDs in the group list for this node
1306 list_for_each(lh, &file_priv->fbs)
1309 /* handle this in 4 parts */
1311 if (card_res->count_fbs >= fb_count) {
1313 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1314 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1315 if (put_user(fb->base.id, fb_id + copied)) {
1316 mutex_unlock(&file_priv->fbs_lock);
1322 card_res->count_fbs = fb_count;
1323 mutex_unlock(&file_priv->fbs_lock);
1325 drm_modeset_lock_all(dev);
1326 mode_group = &file_priv->master->minor->mode_group;
1327 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1329 list_for_each(lh, &dev->mode_config.crtc_list)
1332 list_for_each(lh, &dev->mode_config.connector_list)
1335 list_for_each(lh, &dev->mode_config.encoder_list)
1339 crtc_count = mode_group->num_crtcs;
1340 connector_count = mode_group->num_connectors;
1341 encoder_count = mode_group->num_encoders;
1344 card_res->max_height = dev->mode_config.max_height;
1345 card_res->min_height = dev->mode_config.min_height;
1346 card_res->max_width = dev->mode_config.max_width;
1347 card_res->min_width = dev->mode_config.min_width;
1350 if (card_res->count_crtcs >= crtc_count) {
1352 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1353 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1354 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1356 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
1357 if (put_user(crtc->base.id, crtc_id + copied)) {
1364 for (i = 0; i < mode_group->num_crtcs; i++) {
1365 if (put_user(mode_group->id_list[i],
1366 crtc_id + copied)) {
1374 card_res->count_crtcs = crtc_count;
1377 if (card_res->count_encoders >= encoder_count) {
1379 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1380 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1381 list_for_each_entry(encoder,
1382 &dev->mode_config.encoder_list,
1384 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1385 drm_get_encoder_name(encoder));
1386 if (put_user(encoder->base.id, encoder_id +
1394 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1395 if (put_user(mode_group->id_list[i],
1396 encoder_id + copied)) {
1405 card_res->count_encoders = encoder_count;
1408 if (card_res->count_connectors >= connector_count) {
1410 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1411 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1412 list_for_each_entry(connector,
1413 &dev->mode_config.connector_list,
1415 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1417 drm_get_connector_name(connector));
1418 if (put_user(connector->base.id,
1419 connector_id + copied)) {
1426 int start = mode_group->num_crtcs +
1427 mode_group->num_encoders;
1428 for (i = start; i < start + mode_group->num_connectors; i++) {
1429 if (put_user(mode_group->id_list[i],
1430 connector_id + copied)) {
1438 card_res->count_connectors = connector_count;
1440 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
1441 card_res->count_connectors, card_res->count_encoders);
1444 drm_modeset_unlock_all(dev);
1449 * drm_mode_getcrtc - get CRTC configuration
1450 * @dev: drm device for the ioctl
1451 * @data: data pointer for the ioctl
1452 * @file_priv: drm file for the ioctl call
1454 * Construct a CRTC configuration structure to return to the user.
1456 * Called by the user via ioctl.
1459 * Zero on success, errno on failure.
1461 int drm_mode_getcrtc(struct drm_device *dev,
1462 void *data, struct drm_file *file_priv)
1464 struct drm_mode_crtc *crtc_resp = data;
1465 struct drm_crtc *crtc;
1466 struct drm_mode_object *obj;
1469 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1472 drm_modeset_lock_all(dev);
1474 obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
1475 DRM_MODE_OBJECT_CRTC);
1480 crtc = obj_to_crtc(obj);
1482 crtc_resp->x = crtc->x;
1483 crtc_resp->y = crtc->y;
1484 crtc_resp->gamma_size = crtc->gamma_size;
1486 crtc_resp->fb_id = crtc->fb->base.id;
1488 crtc_resp->fb_id = 0;
1490 if (crtc->enabled) {
1492 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1493 crtc_resp->mode_valid = 1;
1496 crtc_resp->mode_valid = 0;
1500 drm_modeset_unlock_all(dev);
1505 * drm_mode_getconnector - get connector configuration
1506 * @dev: drm device for the ioctl
1507 * @data: data pointer for the ioctl
1508 * @file_priv: drm file for the ioctl call
1510 * Construct a connector configuration structure to return to the user.
1512 * Called by the user via ioctl.
1515 * Zero on success, errno on failure.
1517 int drm_mode_getconnector(struct drm_device *dev, void *data,
1518 struct drm_file *file_priv)
1520 struct drm_mode_get_connector *out_resp = data;
1521 struct drm_mode_object *obj;
1522 struct drm_connector *connector;
1523 struct drm_display_mode *mode;
1525 int props_count = 0;
1526 int encoders_count = 0;
1530 struct drm_mode_modeinfo u_mode;
1531 struct drm_mode_modeinfo __user *mode_ptr;
1532 uint32_t __user *prop_ptr;
1533 uint64_t __user *prop_values;
1534 uint32_t __user *encoder_ptr;
1536 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1539 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1541 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
1543 mutex_lock(&dev->mode_config.mutex);
1545 obj = drm_mode_object_find(dev, out_resp->connector_id,
1546 DRM_MODE_OBJECT_CONNECTOR);
1551 connector = obj_to_connector(obj);
1553 props_count = connector->properties.count;
1555 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1556 if (connector->encoder_ids[i] != 0) {
1561 if (out_resp->count_modes == 0) {
1562 connector->funcs->fill_modes(connector,
1563 dev->mode_config.max_width,
1564 dev->mode_config.max_height);
1567 /* delayed so we get modes regardless of pre-fill_modes state */
1568 list_for_each_entry(mode, &connector->modes, head)
1571 out_resp->connector_id = connector->base.id;
1572 out_resp->connector_type = connector->connector_type;
1573 out_resp->connector_type_id = connector->connector_type_id;
1574 out_resp->mm_width = connector->display_info.width_mm;
1575 out_resp->mm_height = connector->display_info.height_mm;
1576 out_resp->subpixel = connector->display_info.subpixel_order;
1577 out_resp->connection = connector->status;
1578 if (connector->encoder)
1579 out_resp->encoder_id = connector->encoder->base.id;
1581 out_resp->encoder_id = 0;
1584 * This ioctl is called twice, once to determine how much space is
1585 * needed, and the 2nd time to fill it.
1587 if ((out_resp->count_modes >= mode_count) && mode_count) {
1589 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
1590 list_for_each_entry(mode, &connector->modes, head) {
1591 drm_crtc_convert_to_umode(&u_mode, mode);
1592 if (copy_to_user(mode_ptr + copied,
1593 &u_mode, sizeof(u_mode))) {
1600 out_resp->count_modes = mode_count;
1602 if ((out_resp->count_props >= props_count) && props_count) {
1604 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
1605 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
1606 for (i = 0; i < connector->properties.count; i++) {
1607 if (put_user(connector->properties.ids[i],
1608 prop_ptr + copied)) {
1613 if (put_user(connector->properties.values[i],
1614 prop_values + copied)) {
1621 out_resp->count_props = props_count;
1623 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1625 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
1626 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1627 if (connector->encoder_ids[i] != 0) {
1628 if (put_user(connector->encoder_ids[i],
1629 encoder_ptr + copied)) {
1637 out_resp->count_encoders = encoders_count;
1640 mutex_unlock(&dev->mode_config.mutex);
1645 int drm_mode_getencoder(struct drm_device *dev, void *data,
1646 struct drm_file *file_priv)
1648 struct drm_mode_get_encoder *enc_resp = data;
1649 struct drm_mode_object *obj;
1650 struct drm_encoder *encoder;
1653 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1656 drm_modeset_lock_all(dev);
1657 obj = drm_mode_object_find(dev, enc_resp->encoder_id,
1658 DRM_MODE_OBJECT_ENCODER);
1663 encoder = obj_to_encoder(obj);
1666 enc_resp->crtc_id = encoder->crtc->base.id;
1668 enc_resp->crtc_id = 0;
1669 enc_resp->encoder_type = encoder->encoder_type;
1670 enc_resp->encoder_id = encoder->base.id;
1671 enc_resp->possible_crtcs = encoder->possible_crtcs;
1672 enc_resp->possible_clones = encoder->possible_clones;
1675 drm_modeset_unlock_all(dev);
1680 * drm_mode_getplane_res - get plane info
1683 * @file_priv: DRM file info
1685 * Return an plane count and set of IDs.
1687 int drm_mode_getplane_res(struct drm_device *dev, void *data,
1688 struct drm_file *file_priv)
1690 struct drm_mode_get_plane_res *plane_resp = data;
1691 struct drm_mode_config *config;
1692 struct drm_plane *plane;
1693 uint32_t __user *plane_ptr;
1694 int copied = 0, ret = 0;
1696 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1699 drm_modeset_lock_all(dev);
1700 config = &dev->mode_config;
1703 * This ioctl is called twice, once to determine how much space is
1704 * needed, and the 2nd time to fill it.
1706 if (config->num_plane &&
1707 (plane_resp->count_planes >= config->num_plane)) {
1708 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
1710 list_for_each_entry(plane, &config->plane_list, head) {
1711 if (put_user(plane->base.id, plane_ptr + copied)) {
1718 plane_resp->count_planes = config->num_plane;
1721 drm_modeset_unlock_all(dev);
1726 * drm_mode_getplane - get plane info
1729 * @file_priv: DRM file info
1731 * Return plane info, including formats supported, gamma size, any
1734 int drm_mode_getplane(struct drm_device *dev, void *data,
1735 struct drm_file *file_priv)
1737 struct drm_mode_get_plane *plane_resp = data;
1738 struct drm_mode_object *obj;
1739 struct drm_plane *plane;
1740 uint32_t __user *format_ptr;
1743 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1746 drm_modeset_lock_all(dev);
1747 obj = drm_mode_object_find(dev, plane_resp->plane_id,
1748 DRM_MODE_OBJECT_PLANE);
1753 plane = obj_to_plane(obj);
1756 plane_resp->crtc_id = plane->crtc->base.id;
1758 plane_resp->crtc_id = 0;
1761 plane_resp->fb_id = plane->fb->base.id;
1763 plane_resp->fb_id = 0;
1765 plane_resp->plane_id = plane->base.id;
1766 plane_resp->possible_crtcs = plane->possible_crtcs;
1767 plane_resp->gamma_size = plane->gamma_size;
1770 * This ioctl is called twice, once to determine how much space is
1771 * needed, and the 2nd time to fill it.
1773 if (plane->format_count &&
1774 (plane_resp->count_format_types >= plane->format_count)) {
1775 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
1776 if (copy_to_user(format_ptr,
1777 plane->format_types,
1778 sizeof(uint32_t) * plane->format_count)) {
1783 plane_resp->count_format_types = plane->format_count;
1786 drm_modeset_unlock_all(dev);
1791 * drm_mode_setplane - set up or tear down an plane
1793 * @data: ioctl data*
1794 * @file_priv: DRM file info
1796 * Set plane info, including placement, fb, scaling, and other factors.
1797 * Or pass a NULL fb to disable.
1799 int drm_mode_setplane(struct drm_device *dev, void *data,
1800 struct drm_file *file_priv)
1802 struct drm_mode_set_plane *plane_req = data;
1803 struct drm_mode_object *obj;
1804 struct drm_plane *plane;
1805 struct drm_crtc *crtc;
1806 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
1808 unsigned int fb_width, fb_height;
1811 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1815 * First, find the plane, crtc, and fb objects. If not available,
1816 * we don't bother to call the driver.
1818 obj = drm_mode_object_find(dev, plane_req->plane_id,
1819 DRM_MODE_OBJECT_PLANE);
1821 DRM_DEBUG_KMS("Unknown plane ID %d\n",
1822 plane_req->plane_id);
1825 plane = obj_to_plane(obj);
1827 /* No fb means shut it down */
1828 if (!plane_req->fb_id) {
1829 drm_modeset_lock_all(dev);
1831 plane->funcs->disable_plane(plane);
1834 drm_modeset_unlock_all(dev);
1838 obj = drm_mode_object_find(dev, plane_req->crtc_id,
1839 DRM_MODE_OBJECT_CRTC);
1841 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
1842 plane_req->crtc_id);
1846 crtc = obj_to_crtc(obj);
1848 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
1850 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
1856 /* Check whether this plane supports the fb pixel format. */
1857 for (i = 0; i < plane->format_count; i++)
1858 if (fb->pixel_format == plane->format_types[i])
1860 if (i == plane->format_count) {
1861 DRM_DEBUG_KMS("Invalid pixel format %s\n",
1862 drm_get_format_name(fb->pixel_format));
1867 fb_width = fb->width << 16;
1868 fb_height = fb->height << 16;
1870 /* Make sure source coordinates are inside the fb. */
1871 if (plane_req->src_w > fb_width ||
1872 plane_req->src_x > fb_width - plane_req->src_w ||
1873 plane_req->src_h > fb_height ||
1874 plane_req->src_y > fb_height - plane_req->src_h) {
1875 DRM_DEBUG_KMS("Invalid source coordinates "
1876 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
1877 plane_req->src_w >> 16,
1878 ((plane_req->src_w & 0xffff) * 15625) >> 10,
1879 plane_req->src_h >> 16,
1880 ((plane_req->src_h & 0xffff) * 15625) >> 10,
1881 plane_req->src_x >> 16,
1882 ((plane_req->src_x & 0xffff) * 15625) >> 10,
1883 plane_req->src_y >> 16,
1884 ((plane_req->src_y & 0xffff) * 15625) >> 10);
1889 /* Give drivers some help against integer overflows */
1890 if (plane_req->crtc_w > INT_MAX ||
1891 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
1892 plane_req->crtc_h > INT_MAX ||
1893 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
1894 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
1895 plane_req->crtc_w, plane_req->crtc_h,
1896 plane_req->crtc_x, plane_req->crtc_y);
1901 drm_modeset_lock_all(dev);
1902 ret = plane->funcs->update_plane(plane, crtc, fb,
1903 plane_req->crtc_x, plane_req->crtc_y,
1904 plane_req->crtc_w, plane_req->crtc_h,
1905 plane_req->src_x, plane_req->src_y,
1906 plane_req->src_w, plane_req->src_h);
1913 drm_modeset_unlock_all(dev);
1917 drm_framebuffer_unreference(fb);
1919 drm_framebuffer_unreference(old_fb);
1925 * drm_mode_set_config_internal - helper to call ->set_config
1926 * @set: modeset config to set
1928 * This is a little helper to wrap internal calls to the ->set_config driver
1929 * interface. The only thing it adds is correct refcounting dance.
1931 int drm_mode_set_config_internal(struct drm_mode_set *set)
1933 struct drm_crtc *crtc = set->crtc;
1934 struct drm_framebuffer *fb, *old_fb;
1940 ret = crtc->funcs->set_config(set);
1943 drm_framebuffer_unreference(old_fb);
1945 drm_framebuffer_reference(fb);
1950 EXPORT_SYMBOL(drm_mode_set_config_internal);
1953 * drm_mode_setcrtc - set CRTC configuration
1954 * @dev: drm device for the ioctl
1955 * @data: data pointer for the ioctl
1956 * @file_priv: drm file for the ioctl call
1958 * Build a new CRTC configuration based on user request.
1960 * Called by the user via ioctl.
1963 * Zero on success, errno on failure.
1965 int drm_mode_setcrtc(struct drm_device *dev, void *data,
1966 struct drm_file *file_priv)
1968 struct drm_mode_config *config = &dev->mode_config;
1969 struct drm_mode_crtc *crtc_req = data;
1970 struct drm_mode_object *obj;
1971 struct drm_crtc *crtc;
1972 struct drm_connector **connector_set = NULL, *connector;
1973 struct drm_framebuffer *fb = NULL;
1974 struct drm_display_mode *mode = NULL;
1975 struct drm_mode_set set;
1976 uint32_t __user *set_connectors_ptr;
1980 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1983 /* For some reason crtc x/y offsets are signed internally. */
1984 if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
1987 drm_modeset_lock_all(dev);
1988 obj = drm_mode_object_find(dev, crtc_req->crtc_id,
1989 DRM_MODE_OBJECT_CRTC);
1991 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
1995 crtc = obj_to_crtc(obj);
1996 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
1998 if (crtc_req->mode_valid) {
1999 int hdisplay, vdisplay;
2000 /* If we have a mode we need a framebuffer. */
2001 /* If we pass -1, set the mode with the currently bound fb */
2002 if (crtc_req->fb_id == -1) {
2004 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2009 /* Make refcounting symmetric with the lookup path. */
2010 drm_framebuffer_reference(fb);
2012 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2014 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2021 mode = drm_mode_create(dev);
2027 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
2029 DRM_DEBUG_KMS("Invalid mode\n");
2033 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
2035 hdisplay = mode->hdisplay;
2036 vdisplay = mode->vdisplay;
2038 if (crtc->invert_dimensions)
2039 swap(hdisplay, vdisplay);
2041 if (hdisplay > fb->width ||
2042 vdisplay > fb->height ||
2043 crtc_req->x > fb->width - hdisplay ||
2044 crtc_req->y > fb->height - vdisplay) {
2045 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
2046 fb->width, fb->height,
2047 hdisplay, vdisplay, crtc_req->x, crtc_req->y,
2048 crtc->invert_dimensions ? " (inverted)" : "");
2054 if (crtc_req->count_connectors == 0 && mode) {
2055 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
2060 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
2061 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
2062 crtc_req->count_connectors);
2067 if (crtc_req->count_connectors > 0) {
2070 /* Avoid unbounded kernel memory allocation */
2071 if (crtc_req->count_connectors > config->num_connector) {
2076 connector_set = kmalloc(crtc_req->count_connectors *
2077 sizeof(struct drm_connector *),
2079 if (!connector_set) {
2084 for (i = 0; i < crtc_req->count_connectors; i++) {
2085 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
2086 if (get_user(out_id, &set_connectors_ptr[i])) {
2091 obj = drm_mode_object_find(dev, out_id,
2092 DRM_MODE_OBJECT_CONNECTOR);
2094 DRM_DEBUG_KMS("Connector id %d unknown\n",
2099 connector = obj_to_connector(obj);
2100 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2102 drm_get_connector_name(connector));
2104 connector_set[i] = connector;
2109 set.x = crtc_req->x;
2110 set.y = crtc_req->y;
2112 set.connectors = connector_set;
2113 set.num_connectors = crtc_req->count_connectors;
2115 ret = drm_mode_set_config_internal(&set);
2119 drm_framebuffer_unreference(fb);
2121 kfree(connector_set);
2122 drm_mode_destroy(dev, mode);
2123 drm_modeset_unlock_all(dev);
2127 int drm_mode_cursor_ioctl(struct drm_device *dev,
2128 void *data, struct drm_file *file_priv)
2130 struct drm_mode_cursor *req = data;
2131 struct drm_mode_object *obj;
2132 struct drm_crtc *crtc;
2135 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2138 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
2141 obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
2143 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
2146 crtc = obj_to_crtc(obj);
2148 mutex_lock(&crtc->mutex);
2149 if (req->flags & DRM_MODE_CURSOR_BO) {
2150 if (!crtc->funcs->cursor_set) {
2154 /* Turns off the cursor if handle is 0 */
2155 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2156 req->width, req->height);
2159 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2160 if (crtc->funcs->cursor_move) {
2161 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2168 mutex_unlock(&crtc->mutex);
2173 /* Original addfb only supported RGB formats, so figure out which one */
2174 uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2180 fmt = DRM_FORMAT_C8;
2184 fmt = DRM_FORMAT_XRGB1555;
2186 fmt = DRM_FORMAT_RGB565;
2189 fmt = DRM_FORMAT_RGB888;
2193 fmt = DRM_FORMAT_XRGB8888;
2194 else if (depth == 30)
2195 fmt = DRM_FORMAT_XRGB2101010;
2197 fmt = DRM_FORMAT_ARGB8888;
2200 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2201 fmt = DRM_FORMAT_XRGB8888;
2207 EXPORT_SYMBOL(drm_mode_legacy_fb_format);
2210 * drm_mode_addfb - add an FB to the graphics configuration
2211 * @dev: drm device for the ioctl
2212 * @data: data pointer for the ioctl
2213 * @file_priv: drm file for the ioctl call
2215 * Add a new FB to the specified CRTC, given a user request.
2217 * Called by the user via ioctl.
2220 * Zero on success, errno on failure.
2222 int drm_mode_addfb(struct drm_device *dev,
2223 void *data, struct drm_file *file_priv)
2225 struct drm_mode_fb_cmd *or = data;
2226 struct drm_mode_fb_cmd2 r = {};
2227 struct drm_mode_config *config = &dev->mode_config;
2228 struct drm_framebuffer *fb;
2231 /* Use new struct with format internally */
2232 r.fb_id = or->fb_id;
2233 r.width = or->width;
2234 r.height = or->height;
2235 r.pitches[0] = or->pitch;
2236 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
2237 r.handles[0] = or->handle;
2239 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2242 if ((config->min_width > r.width) || (r.width > config->max_width))
2245 if ((config->min_height > r.height) || (r.height > config->max_height))
2248 fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2250 DRM_DEBUG_KMS("could not create framebuffer\n");
2254 mutex_lock(&file_priv->fbs_lock);
2255 or->fb_id = fb->base.id;
2256 list_add(&fb->filp_head, &file_priv->fbs);
2257 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
2258 mutex_unlock(&file_priv->fbs_lock);
2263 static int format_check(const struct drm_mode_fb_cmd2 *r)
2265 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2269 case DRM_FORMAT_RGB332:
2270 case DRM_FORMAT_BGR233:
2271 case DRM_FORMAT_XRGB4444:
2272 case DRM_FORMAT_XBGR4444:
2273 case DRM_FORMAT_RGBX4444:
2274 case DRM_FORMAT_BGRX4444:
2275 case DRM_FORMAT_ARGB4444:
2276 case DRM_FORMAT_ABGR4444:
2277 case DRM_FORMAT_RGBA4444:
2278 case DRM_FORMAT_BGRA4444:
2279 case DRM_FORMAT_XRGB1555:
2280 case DRM_FORMAT_XBGR1555:
2281 case DRM_FORMAT_RGBX5551:
2282 case DRM_FORMAT_BGRX5551:
2283 case DRM_FORMAT_ARGB1555:
2284 case DRM_FORMAT_ABGR1555:
2285 case DRM_FORMAT_RGBA5551:
2286 case DRM_FORMAT_BGRA5551:
2287 case DRM_FORMAT_RGB565:
2288 case DRM_FORMAT_BGR565:
2289 case DRM_FORMAT_RGB888:
2290 case DRM_FORMAT_BGR888:
2291 case DRM_FORMAT_XRGB8888:
2292 case DRM_FORMAT_XBGR8888:
2293 case DRM_FORMAT_RGBX8888:
2294 case DRM_FORMAT_BGRX8888:
2295 case DRM_FORMAT_ARGB8888:
2296 case DRM_FORMAT_ABGR8888:
2297 case DRM_FORMAT_RGBA8888:
2298 case DRM_FORMAT_BGRA8888:
2299 case DRM_FORMAT_XRGB2101010:
2300 case DRM_FORMAT_XBGR2101010:
2301 case DRM_FORMAT_RGBX1010102:
2302 case DRM_FORMAT_BGRX1010102:
2303 case DRM_FORMAT_ARGB2101010:
2304 case DRM_FORMAT_ABGR2101010:
2305 case DRM_FORMAT_RGBA1010102:
2306 case DRM_FORMAT_BGRA1010102:
2307 case DRM_FORMAT_YUYV:
2308 case DRM_FORMAT_YVYU:
2309 case DRM_FORMAT_UYVY:
2310 case DRM_FORMAT_VYUY:
2311 case DRM_FORMAT_AYUV:
2312 case DRM_FORMAT_NV12:
2313 case DRM_FORMAT_NV21:
2314 case DRM_FORMAT_NV16:
2315 case DRM_FORMAT_NV61:
2316 case DRM_FORMAT_NV24:
2317 case DRM_FORMAT_NV42:
2318 case DRM_FORMAT_YUV410:
2319 case DRM_FORMAT_YVU410:
2320 case DRM_FORMAT_YUV411:
2321 case DRM_FORMAT_YVU411:
2322 case DRM_FORMAT_YUV420:
2323 case DRM_FORMAT_YVU420:
2324 case DRM_FORMAT_YUV422:
2325 case DRM_FORMAT_YVU422:
2326 case DRM_FORMAT_YUV444:
2327 case DRM_FORMAT_YVU444:
2334 static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
2336 int ret, hsub, vsub, num_planes, i;
2338 ret = format_check(r);
2340 DRM_DEBUG_KMS("bad framebuffer format %s\n",
2341 drm_get_format_name(r->pixel_format));
2345 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
2346 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
2347 num_planes = drm_format_num_planes(r->pixel_format);
2349 if (r->width == 0 || r->width % hsub) {
2350 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
2354 if (r->height == 0 || r->height % vsub) {
2355 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
2359 for (i = 0; i < num_planes; i++) {
2360 unsigned int width = r->width / (i != 0 ? hsub : 1);
2361 unsigned int height = r->height / (i != 0 ? vsub : 1);
2362 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
2364 if (!r->handles[i]) {
2365 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
2369 if ((uint64_t) width * cpp > UINT_MAX)
2372 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
2375 if (r->pitches[i] < width * cpp) {
2376 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
2385 * drm_mode_addfb2 - add an FB to the graphics configuration
2386 * @dev: drm device for the ioctl
2387 * @data: data pointer for the ioctl
2388 * @file_priv: drm file for the ioctl call
2390 * Add a new FB to the specified CRTC, given a user request with format.
2392 * Called by the user via ioctl.
2395 * Zero on success, errno on failure.
2397 int drm_mode_addfb2(struct drm_device *dev,
2398 void *data, struct drm_file *file_priv)
2400 struct drm_mode_fb_cmd2 *r = data;
2401 struct drm_mode_config *config = &dev->mode_config;
2402 struct drm_framebuffer *fb;
2405 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2408 if (r->flags & ~DRM_MODE_FB_INTERLACED) {
2409 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
2413 if ((config->min_width > r->width) || (r->width > config->max_width)) {
2414 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
2415 r->width, config->min_width, config->max_width);
2418 if ((config->min_height > r->height) || (r->height > config->max_height)) {
2419 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
2420 r->height, config->min_height, config->max_height);
2424 ret = framebuffer_check(r);
2428 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
2430 DRM_DEBUG_KMS("could not create framebuffer\n");
2434 mutex_lock(&file_priv->fbs_lock);
2435 r->fb_id = fb->base.id;
2436 list_add(&fb->filp_head, &file_priv->fbs);
2437 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
2438 mutex_unlock(&file_priv->fbs_lock);
2445 * drm_mode_rmfb - remove an FB from the configuration
2446 * @dev: drm device for the ioctl
2447 * @data: data pointer for the ioctl
2448 * @file_priv: drm file for the ioctl call
2450 * Remove the FB specified by the user.
2452 * Called by the user via ioctl.
2455 * Zero on success, errno on failure.
2457 int drm_mode_rmfb(struct drm_device *dev,
2458 void *data, struct drm_file *file_priv)
2460 struct drm_framebuffer *fb = NULL;
2461 struct drm_framebuffer *fbl = NULL;
2462 uint32_t *id = data;
2465 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2468 mutex_lock(&file_priv->fbs_lock);
2469 mutex_lock(&dev->mode_config.fb_lock);
2470 fb = __drm_framebuffer_lookup(dev, *id);
2474 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2480 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
2481 __drm_framebuffer_unregister(dev, fb);
2483 list_del_init(&fb->filp_head);
2484 mutex_unlock(&dev->mode_config.fb_lock);
2485 mutex_unlock(&file_priv->fbs_lock);
2487 drm_framebuffer_remove(fb);
2492 mutex_unlock(&dev->mode_config.fb_lock);
2493 mutex_unlock(&file_priv->fbs_lock);
2499 * drm_mode_getfb - get FB info
2500 * @dev: drm device for the ioctl
2501 * @data: data pointer for the ioctl
2502 * @file_priv: drm file for the ioctl call
2504 * Lookup the FB given its ID and return info about it.
2506 * Called by the user via ioctl.
2509 * Zero on success, errno on failure.
2511 int drm_mode_getfb(struct drm_device *dev,
2512 void *data, struct drm_file *file_priv)
2514 struct drm_mode_fb_cmd *r = data;
2515 struct drm_framebuffer *fb;
2518 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2521 fb = drm_framebuffer_lookup(dev, r->fb_id);
2525 r->height = fb->height;
2526 r->width = fb->width;
2527 r->depth = fb->depth;
2528 r->bpp = fb->bits_per_pixel;
2529 r->pitch = fb->pitches[0];
2530 if (fb->funcs->create_handle)
2531 ret = fb->funcs->create_handle(fb, file_priv, &r->handle);
2535 drm_framebuffer_unreference(fb);
2540 int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
2541 void *data, struct drm_file *file_priv)
2543 struct drm_clip_rect __user *clips_ptr;
2544 struct drm_clip_rect *clips = NULL;
2545 struct drm_mode_fb_dirty_cmd *r = data;
2546 struct drm_framebuffer *fb;
2551 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2554 fb = drm_framebuffer_lookup(dev, r->fb_id);
2558 num_clips = r->num_clips;
2559 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
2561 if (!num_clips != !clips_ptr) {
2566 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
2568 /* If userspace annotates copy, clips must come in pairs */
2569 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
2574 if (num_clips && clips_ptr) {
2575 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
2579 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
2585 ret = copy_from_user(clips, clips_ptr,
2586 num_clips * sizeof(*clips));
2593 if (fb->funcs->dirty) {
2594 drm_modeset_lock_all(dev);
2595 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
2597 drm_modeset_unlock_all(dev);
2605 drm_framebuffer_unreference(fb);
2612 * drm_fb_release - remove and free the FBs on this file
2613 * @priv: drm file for the ioctl
2615 * Destroy all the FBs associated with @filp.
2617 * Called by the user via ioctl.
2620 * Zero on success, errno on failure.
2622 void drm_fb_release(struct drm_file *priv)
2624 struct drm_device *dev = priv->minor->dev;
2625 struct drm_framebuffer *fb, *tfb;
2627 mutex_lock(&priv->fbs_lock);
2628 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
2630 mutex_lock(&dev->mode_config.fb_lock);
2631 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
2632 __drm_framebuffer_unregister(dev, fb);
2633 mutex_unlock(&dev->mode_config.fb_lock);
2635 list_del_init(&fb->filp_head);
2637 /* This will also drop the fpriv->fbs reference. */
2638 drm_framebuffer_remove(fb);
2640 mutex_unlock(&priv->fbs_lock);
2643 struct drm_property *drm_property_create(struct drm_device *dev, int flags,
2644 const char *name, int num_values)
2646 struct drm_property *property = NULL;
2649 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
2654 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
2655 if (!property->values)
2659 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
2663 property->flags = flags;
2664 property->num_values = num_values;
2665 INIT_LIST_HEAD(&property->enum_blob_list);
2668 strncpy(property->name, name, DRM_PROP_NAME_LEN);
2669 property->name[DRM_PROP_NAME_LEN-1] = '\0';
2672 list_add_tail(&property->head, &dev->mode_config.property_list);
2675 kfree(property->values);
2679 EXPORT_SYMBOL(drm_property_create);
2681 struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
2683 const struct drm_prop_enum_list *props,
2686 struct drm_property *property;
2689 flags |= DRM_MODE_PROP_ENUM;
2691 property = drm_property_create(dev, flags, name, num_values);
2695 for (i = 0; i < num_values; i++) {
2696 ret = drm_property_add_enum(property, i,
2700 drm_property_destroy(dev, property);
2707 EXPORT_SYMBOL(drm_property_create_enum);
2709 struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
2710 int flags, const char *name,
2711 const struct drm_prop_enum_list *props,
2714 struct drm_property *property;
2717 flags |= DRM_MODE_PROP_BITMASK;
2719 property = drm_property_create(dev, flags, name, num_values);
2723 for (i = 0; i < num_values; i++) {
2724 ret = drm_property_add_enum(property, i,
2728 drm_property_destroy(dev, property);
2735 EXPORT_SYMBOL(drm_property_create_bitmask);
2737 struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
2739 uint64_t min, uint64_t max)
2741 struct drm_property *property;
2743 flags |= DRM_MODE_PROP_RANGE;
2745 property = drm_property_create(dev, flags, name, 2);
2749 property->values[0] = min;
2750 property->values[1] = max;
2754 EXPORT_SYMBOL(drm_property_create_range);
2756 int drm_property_add_enum(struct drm_property *property, int index,
2757 uint64_t value, const char *name)
2759 struct drm_property_enum *prop_enum;
2761 if (!(property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)))
2765 * Bitmask enum properties have the additional constraint of values
2768 if ((property->flags & DRM_MODE_PROP_BITMASK) && (value > 63))
2771 if (!list_empty(&property->enum_blob_list)) {
2772 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2773 if (prop_enum->value == value) {
2774 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2775 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2781 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
2785 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2786 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2787 prop_enum->value = value;
2789 property->values[index] = value;
2790 list_add_tail(&prop_enum->head, &property->enum_blob_list);
2793 EXPORT_SYMBOL(drm_property_add_enum);
2795 void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
2797 struct drm_property_enum *prop_enum, *pt;
2799 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
2800 list_del(&prop_enum->head);
2804 if (property->num_values)
2805 kfree(property->values);
2806 drm_mode_object_put(dev, &property->base);
2807 list_del(&property->head);
2810 EXPORT_SYMBOL(drm_property_destroy);
2812 void drm_object_attach_property(struct drm_mode_object *obj,
2813 struct drm_property *property,
2816 int count = obj->properties->count;
2818 if (count == DRM_OBJECT_MAX_PROPERTY) {
2819 WARN(1, "Failed to attach object property (type: 0x%x). Please "
2820 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
2821 "you see this message on the same object type.\n",
2826 obj->properties->ids[count] = property->base.id;
2827 obj->properties->values[count] = init_val;
2828 obj->properties->count++;
2830 EXPORT_SYMBOL(drm_object_attach_property);
2832 int drm_object_property_set_value(struct drm_mode_object *obj,
2833 struct drm_property *property, uint64_t val)
2837 for (i = 0; i < obj->properties->count; i++) {
2838 if (obj->properties->ids[i] == property->base.id) {
2839 obj->properties->values[i] = val;
2846 EXPORT_SYMBOL(drm_object_property_set_value);
2848 int drm_object_property_get_value(struct drm_mode_object *obj,
2849 struct drm_property *property, uint64_t *val)
2853 for (i = 0; i < obj->properties->count; i++) {
2854 if (obj->properties->ids[i] == property->base.id) {
2855 *val = obj->properties->values[i];
2862 EXPORT_SYMBOL(drm_object_property_get_value);
2864 int drm_mode_getproperty_ioctl(struct drm_device *dev,
2865 void *data, struct drm_file *file_priv)
2867 struct drm_mode_object *obj;
2868 struct drm_mode_get_property *out_resp = data;
2869 struct drm_property *property;
2872 int value_count = 0;
2875 struct drm_property_enum *prop_enum;
2876 struct drm_mode_property_enum __user *enum_ptr;
2877 struct drm_property_blob *prop_blob;
2878 uint32_t __user *blob_id_ptr;
2879 uint64_t __user *values_ptr;
2880 uint32_t __user *blob_length_ptr;
2882 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2885 drm_modeset_lock_all(dev);
2886 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
2891 property = obj_to_property(obj);
2893 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
2894 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
2896 } else if (property->flags & DRM_MODE_PROP_BLOB) {
2897 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
2901 value_count = property->num_values;
2903 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
2904 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
2905 out_resp->flags = property->flags;
2907 if ((out_resp->count_values >= value_count) && value_count) {
2908 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
2909 for (i = 0; i < value_count; i++) {
2910 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
2916 out_resp->count_values = value_count;
2918 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
2919 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
2921 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
2922 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2924 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
2929 if (copy_to_user(&enum_ptr[copied].name,
2930 &prop_enum->name, DRM_PROP_NAME_LEN)) {
2937 out_resp->count_enum_blobs = enum_count;
2940 if (property->flags & DRM_MODE_PROP_BLOB) {
2941 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
2943 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
2944 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
2946 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
2947 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
2952 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
2960 out_resp->count_enum_blobs = blob_count;
2963 drm_modeset_unlock_all(dev);
2967 static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
2970 struct drm_property_blob *blob;
2973 if (!length || !data)
2976 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
2980 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
2986 blob->length = length;
2988 memcpy(blob->data, data, length);
2990 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
2994 static void drm_property_destroy_blob(struct drm_device *dev,
2995 struct drm_property_blob *blob)
2997 drm_mode_object_put(dev, &blob->base);
2998 list_del(&blob->head);
3002 int drm_mode_getblob_ioctl(struct drm_device *dev,
3003 void *data, struct drm_file *file_priv)
3005 struct drm_mode_object *obj;
3006 struct drm_mode_get_blob *out_resp = data;
3007 struct drm_property_blob *blob;
3009 void __user *blob_ptr;
3011 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3014 drm_modeset_lock_all(dev);
3015 obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
3020 blob = obj_to_blob(obj);
3022 if (out_resp->length == blob->length) {
3023 blob_ptr = (void __user *)(unsigned long)out_resp->data;
3024 if (copy_to_user(blob_ptr, blob->data, blob->length)){
3029 out_resp->length = blob->length;
3032 drm_modeset_unlock_all(dev);
3036 int drm_mode_connector_update_edid_property(struct drm_connector *connector,
3039 struct drm_device *dev = connector->dev;
3042 if (connector->edid_blob_ptr)
3043 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
3045 /* Delete edid, when there is none. */
3047 connector->edid_blob_ptr = NULL;
3048 ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
3052 size = EDID_LENGTH * (1 + edid->extensions);
3053 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
3055 if (!connector->edid_blob_ptr)
3058 ret = drm_object_property_set_value(&connector->base,
3059 dev->mode_config.edid_property,
3060 connector->edid_blob_ptr->base.id);
3064 EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
3066 static bool drm_property_change_is_valid(struct drm_property *property,
3069 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
3071 if (property->flags & DRM_MODE_PROP_RANGE) {
3072 if (value < property->values[0] || value > property->values[1])
3075 } else if (property->flags & DRM_MODE_PROP_BITMASK) {
3077 uint64_t valid_mask = 0;
3078 for (i = 0; i < property->num_values; i++)
3079 valid_mask |= (1ULL << property->values[i]);
3080 return !(value & ~valid_mask);
3081 } else if (property->flags & DRM_MODE_PROP_BLOB) {
3082 /* Only the driver knows */
3086 for (i = 0; i < property->num_values; i++)
3087 if (property->values[i] == value)
3093 int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
3094 void *data, struct drm_file *file_priv)
3096 struct drm_mode_connector_set_property *conn_set_prop = data;
3097 struct drm_mode_obj_set_property obj_set_prop = {
3098 .value = conn_set_prop->value,
3099 .prop_id = conn_set_prop->prop_id,
3100 .obj_id = conn_set_prop->connector_id,
3101 .obj_type = DRM_MODE_OBJECT_CONNECTOR
3104 /* It does all the locking and checking we need */
3105 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
3108 static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
3109 struct drm_property *property,
3113 struct drm_connector *connector = obj_to_connector(obj);
3115 /* Do DPMS ourselves */
3116 if (property == connector->dev->mode_config.dpms_property) {
3117 if (connector->funcs->dpms)
3118 (*connector->funcs->dpms)(connector, (int)value);
3120 } else if (connector->funcs->set_property)
3121 ret = connector->funcs->set_property(connector, property, value);
3123 /* store the property value if successful */
3125 drm_object_property_set_value(&connector->base, property, value);
3129 static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
3130 struct drm_property *property,
3134 struct drm_crtc *crtc = obj_to_crtc(obj);
3136 if (crtc->funcs->set_property)
3137 ret = crtc->funcs->set_property(crtc, property, value);
3139 drm_object_property_set_value(obj, property, value);
3144 static int drm_mode_plane_set_obj_prop(struct drm_mode_object *obj,
3145 struct drm_property *property,
3149 struct drm_plane *plane = obj_to_plane(obj);
3151 if (plane->funcs->set_property)
3152 ret = plane->funcs->set_property(plane, property, value);
3154 drm_object_property_set_value(obj, property, value);
3159 int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
3160 struct drm_file *file_priv)
3162 struct drm_mode_obj_get_properties *arg = data;
3163 struct drm_mode_object *obj;
3167 int props_count = 0;
3168 uint32_t __user *props_ptr;
3169 uint64_t __user *prop_values_ptr;
3171 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3174 drm_modeset_lock_all(dev);
3176 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3181 if (!obj->properties) {
3186 props_count = obj->properties->count;
3188 /* This ioctl is called twice, once to determine how much space is
3189 * needed, and the 2nd time to fill it. */
3190 if ((arg->count_props >= props_count) && props_count) {
3192 props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
3193 prop_values_ptr = (uint64_t __user *)(unsigned long)
3194 (arg->prop_values_ptr);
3195 for (i = 0; i < props_count; i++) {
3196 if (put_user(obj->properties->ids[i],
3197 props_ptr + copied)) {
3201 if (put_user(obj->properties->values[i],
3202 prop_values_ptr + copied)) {
3209 arg->count_props = props_count;
3211 drm_modeset_unlock_all(dev);
3215 int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
3216 struct drm_file *file_priv)
3218 struct drm_mode_obj_set_property *arg = data;
3219 struct drm_mode_object *arg_obj;
3220 struct drm_mode_object *prop_obj;
3221 struct drm_property *property;
3225 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3228 drm_modeset_lock_all(dev);
3230 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3233 if (!arg_obj->properties)
3236 for (i = 0; i < arg_obj->properties->count; i++)
3237 if (arg_obj->properties->ids[i] == arg->prop_id)
3240 if (i == arg_obj->properties->count)
3243 prop_obj = drm_mode_object_find(dev, arg->prop_id,
3244 DRM_MODE_OBJECT_PROPERTY);
3247 property = obj_to_property(prop_obj);
3249 if (!drm_property_change_is_valid(property, arg->value))
3252 switch (arg_obj->type) {
3253 case DRM_MODE_OBJECT_CONNECTOR:
3254 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
3257 case DRM_MODE_OBJECT_CRTC:
3258 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
3260 case DRM_MODE_OBJECT_PLANE:
3261 ret = drm_mode_plane_set_obj_prop(arg_obj, property, arg->value);
3266 drm_modeset_unlock_all(dev);
3270 int drm_mode_connector_attach_encoder(struct drm_connector *connector,
3271 struct drm_encoder *encoder)
3275 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3276 if (connector->encoder_ids[i] == 0) {
3277 connector->encoder_ids[i] = encoder->base.id;
3283 EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
3285 void drm_mode_connector_detach_encoder(struct drm_connector *connector,
3286 struct drm_encoder *encoder)
3289 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3290 if (connector->encoder_ids[i] == encoder->base.id) {
3291 connector->encoder_ids[i] = 0;
3292 if (connector->encoder == encoder)
3293 connector->encoder = NULL;
3298 EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
3300 int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
3303 crtc->gamma_size = gamma_size;
3305 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
3306 if (!crtc->gamma_store) {
3307 crtc->gamma_size = 0;
3313 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
3315 int drm_mode_gamma_set_ioctl(struct drm_device *dev,
3316 void *data, struct drm_file *file_priv)
3318 struct drm_mode_crtc_lut *crtc_lut = data;
3319 struct drm_mode_object *obj;
3320 struct drm_crtc *crtc;
3321 void *r_base, *g_base, *b_base;
3325 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3328 drm_modeset_lock_all(dev);
3329 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3334 crtc = obj_to_crtc(obj);
3336 if (crtc->funcs->gamma_set == NULL) {
3341 /* memcpy into gamma store */
3342 if (crtc_lut->gamma_size != crtc->gamma_size) {
3347 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3348 r_base = crtc->gamma_store;
3349 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
3354 g_base = r_base + size;
3355 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
3360 b_base = g_base + size;
3361 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
3366 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
3369 drm_modeset_unlock_all(dev);
3374 int drm_mode_gamma_get_ioctl(struct drm_device *dev,
3375 void *data, struct drm_file *file_priv)
3377 struct drm_mode_crtc_lut *crtc_lut = data;
3378 struct drm_mode_object *obj;
3379 struct drm_crtc *crtc;
3380 void *r_base, *g_base, *b_base;
3384 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3387 drm_modeset_lock_all(dev);
3388 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3393 crtc = obj_to_crtc(obj);
3395 /* memcpy into gamma store */
3396 if (crtc_lut->gamma_size != crtc->gamma_size) {
3401 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3402 r_base = crtc->gamma_store;
3403 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
3408 g_base = r_base + size;
3409 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
3414 b_base = g_base + size;
3415 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
3420 drm_modeset_unlock_all(dev);
3424 int drm_mode_page_flip_ioctl(struct drm_device *dev,
3425 void *data, struct drm_file *file_priv)
3427 struct drm_mode_crtc_page_flip *page_flip = data;
3428 struct drm_mode_object *obj;
3429 struct drm_crtc *crtc;
3430 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
3431 struct drm_pending_vblank_event *e = NULL;
3432 unsigned long flags;
3433 int hdisplay, vdisplay;
3436 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
3437 page_flip->reserved != 0)
3440 obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
3443 crtc = obj_to_crtc(obj);
3445 mutex_lock(&crtc->mutex);
3446 if (crtc->fb == NULL) {
3447 /* The framebuffer is currently unbound, presumably
3448 * due to a hotplug event, that userspace has not
3455 if (crtc->funcs->page_flip == NULL)
3458 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
3462 hdisplay = crtc->mode.hdisplay;
3463 vdisplay = crtc->mode.vdisplay;
3465 if (crtc->invert_dimensions)
3466 swap(hdisplay, vdisplay);
3468 if (hdisplay > fb->width ||
3469 vdisplay > fb->height ||
3470 crtc->x > fb->width - hdisplay ||
3471 crtc->y > fb->height - vdisplay) {
3472 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
3473 fb->width, fb->height, hdisplay, vdisplay, crtc->x, crtc->y,
3474 crtc->invert_dimensions ? " (inverted)" : "");
3479 if (crtc->fb->pixel_format != fb->pixel_format) {
3480 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
3485 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3487 spin_lock_irqsave(&dev->event_lock, flags);
3488 if (file_priv->event_space < sizeof e->event) {
3489 spin_unlock_irqrestore(&dev->event_lock, flags);
3492 file_priv->event_space -= sizeof e->event;
3493 spin_unlock_irqrestore(&dev->event_lock, flags);
3495 e = kzalloc(sizeof *e, GFP_KERNEL);
3497 spin_lock_irqsave(&dev->event_lock, flags);
3498 file_priv->event_space += sizeof e->event;
3499 spin_unlock_irqrestore(&dev->event_lock, flags);
3503 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
3504 e->event.base.length = sizeof e->event;
3505 e->event.user_data = page_flip->user_data;
3506 e->base.event = &e->event.base;
3507 e->base.file_priv = file_priv;
3509 (void (*) (struct drm_pending_event *)) kfree;
3513 ret = crtc->funcs->page_flip(crtc, fb, e);
3515 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3516 spin_lock_irqsave(&dev->event_lock, flags);
3517 file_priv->event_space += sizeof e->event;
3518 spin_unlock_irqrestore(&dev->event_lock, flags);
3521 /* Keep the old fb, don't unref it. */
3525 * Warn if the driver hasn't properly updated the crtc->fb
3526 * field to reflect that the new framebuffer is now used.
3527 * Failing to do so will screw with the reference counting
3530 WARN_ON(crtc->fb != fb);
3531 /* Unref only the old framebuffer. */
3537 drm_framebuffer_unreference(fb);
3539 drm_framebuffer_unreference(old_fb);
3540 mutex_unlock(&crtc->mutex);
3545 void drm_mode_config_reset(struct drm_device *dev)
3547 struct drm_crtc *crtc;
3548 struct drm_encoder *encoder;
3549 struct drm_connector *connector;
3551 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3552 if (crtc->funcs->reset)
3553 crtc->funcs->reset(crtc);
3555 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
3556 if (encoder->funcs->reset)
3557 encoder->funcs->reset(encoder);
3559 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
3560 connector->status = connector_status_unknown;
3562 if (connector->funcs->reset)
3563 connector->funcs->reset(connector);
3566 EXPORT_SYMBOL(drm_mode_config_reset);
3568 int drm_mode_create_dumb_ioctl(struct drm_device *dev,
3569 void *data, struct drm_file *file_priv)
3571 struct drm_mode_create_dumb *args = data;
3573 if (!dev->driver->dumb_create)
3575 return dev->driver->dumb_create(file_priv, dev, args);
3578 int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
3579 void *data, struct drm_file *file_priv)
3581 struct drm_mode_map_dumb *args = data;
3583 /* call driver ioctl to get mmap offset */
3584 if (!dev->driver->dumb_map_offset)
3587 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
3590 int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
3591 void *data, struct drm_file *file_priv)
3593 struct drm_mode_destroy_dumb *args = data;
3595 if (!dev->driver->dumb_destroy)
3598 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
3602 * Just need to support RGB formats here for compat with code that doesn't
3603 * use pixel formats directly yet.
3605 void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
3610 case DRM_FORMAT_RGB332:
3611 case DRM_FORMAT_BGR233:
3615 case DRM_FORMAT_XRGB1555:
3616 case DRM_FORMAT_XBGR1555:
3617 case DRM_FORMAT_RGBX5551:
3618 case DRM_FORMAT_BGRX5551:
3619 case DRM_FORMAT_ARGB1555:
3620 case DRM_FORMAT_ABGR1555:
3621 case DRM_FORMAT_RGBA5551:
3622 case DRM_FORMAT_BGRA5551:
3626 case DRM_FORMAT_RGB565:
3627 case DRM_FORMAT_BGR565:
3631 case DRM_FORMAT_RGB888:
3632 case DRM_FORMAT_BGR888:
3636 case DRM_FORMAT_XRGB8888:
3637 case DRM_FORMAT_XBGR8888:
3638 case DRM_FORMAT_RGBX8888:
3639 case DRM_FORMAT_BGRX8888:
3643 case DRM_FORMAT_XRGB2101010:
3644 case DRM_FORMAT_XBGR2101010:
3645 case DRM_FORMAT_RGBX1010102:
3646 case DRM_FORMAT_BGRX1010102:
3647 case DRM_FORMAT_ARGB2101010:
3648 case DRM_FORMAT_ABGR2101010:
3649 case DRM_FORMAT_RGBA1010102:
3650 case DRM_FORMAT_BGRA1010102:
3654 case DRM_FORMAT_ARGB8888:
3655 case DRM_FORMAT_ABGR8888:
3656 case DRM_FORMAT_RGBA8888:
3657 case DRM_FORMAT_BGRA8888:
3662 DRM_DEBUG_KMS("unsupported pixel format\n");
3668 EXPORT_SYMBOL(drm_fb_get_bpp_depth);
3671 * drm_format_num_planes - get the number of planes for format
3672 * @format: pixel format (DRM_FORMAT_*)
3675 * The number of planes used by the specified pixel format.
3677 int drm_format_num_planes(uint32_t format)
3680 case DRM_FORMAT_YUV410:
3681 case DRM_FORMAT_YVU410:
3682 case DRM_FORMAT_YUV411:
3683 case DRM_FORMAT_YVU411:
3684 case DRM_FORMAT_YUV420:
3685 case DRM_FORMAT_YVU420:
3686 case DRM_FORMAT_YUV422:
3687 case DRM_FORMAT_YVU422:
3688 case DRM_FORMAT_YUV444:
3689 case DRM_FORMAT_YVU444:
3691 case DRM_FORMAT_NV12:
3692 case DRM_FORMAT_NV21:
3693 case DRM_FORMAT_NV16:
3694 case DRM_FORMAT_NV61:
3695 case DRM_FORMAT_NV24:
3696 case DRM_FORMAT_NV42:
3702 EXPORT_SYMBOL(drm_format_num_planes);
3705 * drm_format_plane_cpp - determine the bytes per pixel value
3706 * @format: pixel format (DRM_FORMAT_*)
3707 * @plane: plane index
3710 * The bytes per pixel value for the specified plane.
3712 int drm_format_plane_cpp(uint32_t format, int plane)
3717 if (plane >= drm_format_num_planes(format))
3721 case DRM_FORMAT_YUYV:
3722 case DRM_FORMAT_YVYU:
3723 case DRM_FORMAT_UYVY:
3724 case DRM_FORMAT_VYUY:
3726 case DRM_FORMAT_NV12:
3727 case DRM_FORMAT_NV21:
3728 case DRM_FORMAT_NV16:
3729 case DRM_FORMAT_NV61:
3730 case DRM_FORMAT_NV24:
3731 case DRM_FORMAT_NV42:
3732 return plane ? 2 : 1;
3733 case DRM_FORMAT_YUV410:
3734 case DRM_FORMAT_YVU410:
3735 case DRM_FORMAT_YUV411:
3736 case DRM_FORMAT_YVU411:
3737 case DRM_FORMAT_YUV420:
3738 case DRM_FORMAT_YVU420:
3739 case DRM_FORMAT_YUV422:
3740 case DRM_FORMAT_YVU422:
3741 case DRM_FORMAT_YUV444:
3742 case DRM_FORMAT_YVU444:
3745 drm_fb_get_bpp_depth(format, &depth, &bpp);
3749 EXPORT_SYMBOL(drm_format_plane_cpp);
3752 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
3753 * @format: pixel format (DRM_FORMAT_*)
3756 * The horizontal chroma subsampling factor for the
3757 * specified pixel format.
3759 int drm_format_horz_chroma_subsampling(uint32_t format)
3762 case DRM_FORMAT_YUV411:
3763 case DRM_FORMAT_YVU411:
3764 case DRM_FORMAT_YUV410:
3765 case DRM_FORMAT_YVU410:
3767 case DRM_FORMAT_YUYV:
3768 case DRM_FORMAT_YVYU:
3769 case DRM_FORMAT_UYVY:
3770 case DRM_FORMAT_VYUY:
3771 case DRM_FORMAT_NV12:
3772 case DRM_FORMAT_NV21:
3773 case DRM_FORMAT_NV16:
3774 case DRM_FORMAT_NV61:
3775 case DRM_FORMAT_YUV422:
3776 case DRM_FORMAT_YVU422:
3777 case DRM_FORMAT_YUV420:
3778 case DRM_FORMAT_YVU420:
3784 EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
3787 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
3788 * @format: pixel format (DRM_FORMAT_*)
3791 * The vertical chroma subsampling factor for the
3792 * specified pixel format.
3794 int drm_format_vert_chroma_subsampling(uint32_t format)
3797 case DRM_FORMAT_YUV410:
3798 case DRM_FORMAT_YVU410:
3800 case DRM_FORMAT_YUV420:
3801 case DRM_FORMAT_YVU420:
3802 case DRM_FORMAT_NV12:
3803 case DRM_FORMAT_NV21:
3809 EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
3812 * drm_mode_config_init - initialize DRM mode_configuration structure
3815 * Initialize @dev's mode_config structure, used for tracking the graphics
3816 * configuration of @dev.
3818 * Since this initializes the modeset locks, no locking is possible. Which is no
3819 * problem, since this should happen single threaded at init time. It is the
3820 * driver's problem to ensure this guarantee.
3823 void drm_mode_config_init(struct drm_device *dev)
3825 mutex_init(&dev->mode_config.mutex);
3826 mutex_init(&dev->mode_config.idr_mutex);
3827 mutex_init(&dev->mode_config.fb_lock);
3828 INIT_LIST_HEAD(&dev->mode_config.fb_list);
3829 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
3830 INIT_LIST_HEAD(&dev->mode_config.connector_list);
3831 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
3832 INIT_LIST_HEAD(&dev->mode_config.property_list);
3833 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
3834 INIT_LIST_HEAD(&dev->mode_config.plane_list);
3835 idr_init(&dev->mode_config.crtc_idr);
3837 drm_modeset_lock_all(dev);
3838 drm_mode_create_standard_connector_properties(dev);
3839 drm_modeset_unlock_all(dev);
3841 /* Just to be sure */
3842 dev->mode_config.num_fb = 0;
3843 dev->mode_config.num_connector = 0;
3844 dev->mode_config.num_crtc = 0;
3845 dev->mode_config.num_encoder = 0;
3847 EXPORT_SYMBOL(drm_mode_config_init);
3850 * drm_mode_config_cleanup - free up DRM mode_config info
3853 * Free up all the connectors and CRTCs associated with this DRM device, then
3854 * free up the framebuffers and associated buffer objects.
3856 * Note that since this /should/ happen single-threaded at driver/device
3857 * teardown time, no locking is required. It's the driver's job to ensure that
3858 * this guarantee actually holds true.
3860 * FIXME: cleanup any dangling user buffer objects too
3862 void drm_mode_config_cleanup(struct drm_device *dev)
3864 struct drm_connector *connector, *ot;
3865 struct drm_crtc *crtc, *ct;
3866 struct drm_encoder *encoder, *enct;
3867 struct drm_framebuffer *fb, *fbt;
3868 struct drm_property *property, *pt;
3869 struct drm_property_blob *blob, *bt;
3870 struct drm_plane *plane, *plt;
3872 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
3874 encoder->funcs->destroy(encoder);
3877 list_for_each_entry_safe(connector, ot,
3878 &dev->mode_config.connector_list, head) {
3879 connector->funcs->destroy(connector);
3882 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
3884 drm_property_destroy(dev, property);
3887 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
3889 drm_property_destroy_blob(dev, blob);
3893 * Single-threaded teardown context, so it's not required to grab the
3894 * fb_lock to protect against concurrent fb_list access. Contrary, it
3895 * would actually deadlock with the drm_framebuffer_cleanup function.
3897 * Also, if there are any framebuffers left, that's a driver leak now,
3898 * so politely WARN about this.
3900 WARN_ON(!list_empty(&dev->mode_config.fb_list));
3901 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
3902 drm_framebuffer_remove(fb);
3905 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
3907 plane->funcs->destroy(plane);
3910 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
3911 crtc->funcs->destroy(crtc);
3914 idr_destroy(&dev->mode_config.crtc_idr);
3916 EXPORT_SYMBOL(drm_mode_config_cleanup);