gpu: ipu-v3: ipu-dc: don't bug out on invalid bus_format
[deliverable/linux.git] / drivers / gpu / drm / drm_crtc.c
1 /*
2 * Copyright (c) 2006-2008 Intel Corporation
3 * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
4 * Copyright (c) 2008 Red Hat Inc.
5 *
6 * DRM core CRTC related functions
7 *
8 * Permission to use, copy, modify, distribute, and sell this software and its
9 * documentation for any purpose is hereby granted without fee, provided that
10 * the above copyright notice appear in all copies and that both that copyright
11 * notice and this permission notice appear in supporting documentation, and
12 * that the name of the copyright holders not be used in advertising or
13 * publicity pertaining to distribution of the software without specific,
14 * written prior permission. The copyright holders make no representations
15 * about the suitability of this software for any purpose. It is provided "as
16 * is" without express or implied warranty.
17 *
18 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
20 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
21 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
22 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
23 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24 * OF THIS SOFTWARE.
25 *
26 * Authors:
27 * Keith Packard
28 * Eric Anholt <eric@anholt.net>
29 * Dave Airlie <airlied@linux.ie>
30 * Jesse Barnes <jesse.barnes@intel.com>
31 */
32 #include <linux/ctype.h>
33 #include <linux/list.h>
34 #include <linux/slab.h>
35 #include <linux/export.h>
36 #include <drm/drmP.h>
37 #include <drm/drm_crtc.h>
38 #include <drm/drm_edid.h>
39 #include <drm/drm_fourcc.h>
40 #include <drm/drm_modeset_lock.h>
41 #include <drm/drm_atomic.h>
42 #include <drm/drm_auth.h>
43
44 #include "drm_crtc_internal.h"
45 #include "drm_internal.h"
46
47 static struct drm_framebuffer *
48 internal_framebuffer_create(struct drm_device *dev,
49 const struct drm_mode_fb_cmd2 *r,
50 struct drm_file *file_priv);
51
52 /* Avoid boilerplate. I'm tired of typing. */
53 #define DRM_ENUM_NAME_FN(fnname, list) \
54 const char *fnname(int val) \
55 { \
56 int i; \
57 for (i = 0; i < ARRAY_SIZE(list); i++) { \
58 if (list[i].type == val) \
59 return list[i].name; \
60 } \
61 return "(unknown)"; \
62 }
63
64 /*
65 * Global properties
66 */
67 static const struct drm_prop_enum_list drm_dpms_enum_list[] = {
68 { DRM_MODE_DPMS_ON, "On" },
69 { DRM_MODE_DPMS_STANDBY, "Standby" },
70 { DRM_MODE_DPMS_SUSPEND, "Suspend" },
71 { DRM_MODE_DPMS_OFF, "Off" }
72 };
73
74 DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
75
76 static const struct drm_prop_enum_list drm_plane_type_enum_list[] = {
77 { DRM_PLANE_TYPE_OVERLAY, "Overlay" },
78 { DRM_PLANE_TYPE_PRIMARY, "Primary" },
79 { DRM_PLANE_TYPE_CURSOR, "Cursor" },
80 };
81
82 /*
83 * Optional properties
84 */
85 static const struct drm_prop_enum_list drm_scaling_mode_enum_list[] = {
86 { DRM_MODE_SCALE_NONE, "None" },
87 { DRM_MODE_SCALE_FULLSCREEN, "Full" },
88 { DRM_MODE_SCALE_CENTER, "Center" },
89 { DRM_MODE_SCALE_ASPECT, "Full aspect" },
90 };
91
92 static const struct drm_prop_enum_list drm_aspect_ratio_enum_list[] = {
93 { DRM_MODE_PICTURE_ASPECT_NONE, "Automatic" },
94 { DRM_MODE_PICTURE_ASPECT_4_3, "4:3" },
95 { DRM_MODE_PICTURE_ASPECT_16_9, "16:9" },
96 };
97
98 /*
99 * Non-global properties, but "required" for certain connectors.
100 */
101 static const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] = {
102 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
103 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
104 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
105 };
106
107 DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
108
109 static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] = {
110 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
111 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
112 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
113 };
114
115 DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
116 drm_dvi_i_subconnector_enum_list)
117
118 static const struct drm_prop_enum_list drm_tv_select_enum_list[] = {
119 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
120 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
121 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
122 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
123 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
124 };
125
126 DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
127
128 static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] = {
129 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
130 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
131 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
132 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
133 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
134 };
135
136 DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
137 drm_tv_subconnector_enum_list)
138
139 static const struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
140 { DRM_MODE_DIRTY_OFF, "Off" },
141 { DRM_MODE_DIRTY_ON, "On" },
142 { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
143 };
144
145 struct drm_conn_prop_enum_list {
146 int type;
147 const char *name;
148 struct ida ida;
149 };
150
151 /*
152 * Connector and encoder types.
153 */
154 static struct drm_conn_prop_enum_list drm_connector_enum_list[] = {
155 { DRM_MODE_CONNECTOR_Unknown, "Unknown" },
156 { DRM_MODE_CONNECTOR_VGA, "VGA" },
157 { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
158 { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
159 { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
160 { DRM_MODE_CONNECTOR_Composite, "Composite" },
161 { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO" },
162 { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
163 { DRM_MODE_CONNECTOR_Component, "Component" },
164 { DRM_MODE_CONNECTOR_9PinDIN, "DIN" },
165 { DRM_MODE_CONNECTOR_DisplayPort, "DP" },
166 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
167 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
168 { DRM_MODE_CONNECTOR_TV, "TV" },
169 { DRM_MODE_CONNECTOR_eDP, "eDP" },
170 { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual" },
171 { DRM_MODE_CONNECTOR_DSI, "DSI" },
172 { DRM_MODE_CONNECTOR_DPI, "DPI" },
173 };
174
175 static const struct drm_prop_enum_list drm_encoder_enum_list[] = {
176 { DRM_MODE_ENCODER_NONE, "None" },
177 { DRM_MODE_ENCODER_DAC, "DAC" },
178 { DRM_MODE_ENCODER_TMDS, "TMDS" },
179 { DRM_MODE_ENCODER_LVDS, "LVDS" },
180 { DRM_MODE_ENCODER_TVDAC, "TV" },
181 { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
182 { DRM_MODE_ENCODER_DSI, "DSI" },
183 { DRM_MODE_ENCODER_DPMST, "DP MST" },
184 { DRM_MODE_ENCODER_DPI, "DPI" },
185 };
186
187 static const struct drm_prop_enum_list drm_subpixel_enum_list[] = {
188 { SubPixelUnknown, "Unknown" },
189 { SubPixelHorizontalRGB, "Horizontal RGB" },
190 { SubPixelHorizontalBGR, "Horizontal BGR" },
191 { SubPixelVerticalRGB, "Vertical RGB" },
192 { SubPixelVerticalBGR, "Vertical BGR" },
193 { SubPixelNone, "None" },
194 };
195
196 void drm_connector_ida_init(void)
197 {
198 int i;
199
200 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
201 ida_init(&drm_connector_enum_list[i].ida);
202 }
203
204 void drm_connector_ida_destroy(void)
205 {
206 int i;
207
208 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
209 ida_destroy(&drm_connector_enum_list[i].ida);
210 }
211
212 /**
213 * drm_get_connector_status_name - return a string for connector status
214 * @status: connector status to compute name of
215 *
216 * In contrast to the other drm_get_*_name functions this one here returns a
217 * const pointer and hence is threadsafe.
218 */
219 const char *drm_get_connector_status_name(enum drm_connector_status status)
220 {
221 if (status == connector_status_connected)
222 return "connected";
223 else if (status == connector_status_disconnected)
224 return "disconnected";
225 else
226 return "unknown";
227 }
228 EXPORT_SYMBOL(drm_get_connector_status_name);
229
230 /**
231 * drm_get_subpixel_order_name - return a string for a given subpixel enum
232 * @order: enum of subpixel_order
233 *
234 * Note you could abuse this and return something out of bounds, but that
235 * would be a caller error. No unscrubbed user data should make it here.
236 */
237 const char *drm_get_subpixel_order_name(enum subpixel_order order)
238 {
239 return drm_subpixel_enum_list[order].name;
240 }
241 EXPORT_SYMBOL(drm_get_subpixel_order_name);
242
243 /*
244 * Internal function to assign a slot in the object idr and optionally
245 * register the object into the idr.
246 */
247 static int drm_mode_object_get_reg(struct drm_device *dev,
248 struct drm_mode_object *obj,
249 uint32_t obj_type,
250 bool register_obj,
251 void (*obj_free_cb)(struct kref *kref))
252 {
253 int ret;
254
255 mutex_lock(&dev->mode_config.idr_mutex);
256 ret = idr_alloc(&dev->mode_config.crtc_idr, register_obj ? obj : NULL, 1, 0, GFP_KERNEL);
257 if (ret >= 0) {
258 /*
259 * Set up the object linking under the protection of the idr
260 * lock so that other users can't see inconsistent state.
261 */
262 obj->id = ret;
263 obj->type = obj_type;
264 if (obj_free_cb) {
265 obj->free_cb = obj_free_cb;
266 kref_init(&obj->refcount);
267 }
268 }
269 mutex_unlock(&dev->mode_config.idr_mutex);
270
271 return ret < 0 ? ret : 0;
272 }
273
274 /**
275 * drm_mode_object_get - allocate a new modeset identifier
276 * @dev: DRM device
277 * @obj: object pointer, used to generate unique ID
278 * @obj_type: object type
279 *
280 * Create a unique identifier based on @ptr in @dev's identifier space. Used
281 * for tracking modes, CRTCs and connectors. Note that despite the _get postfix
282 * modeset identifiers are _not_ reference counted. Hence don't use this for
283 * reference counted modeset objects like framebuffers.
284 *
285 * Returns:
286 * Zero on success, error code on failure.
287 */
288 int drm_mode_object_get(struct drm_device *dev,
289 struct drm_mode_object *obj, uint32_t obj_type)
290 {
291 return drm_mode_object_get_reg(dev, obj, obj_type, true, NULL);
292 }
293
294 static void drm_mode_object_register(struct drm_device *dev,
295 struct drm_mode_object *obj)
296 {
297 mutex_lock(&dev->mode_config.idr_mutex);
298 idr_replace(&dev->mode_config.crtc_idr, obj, obj->id);
299 mutex_unlock(&dev->mode_config.idr_mutex);
300 }
301
302 /**
303 * drm_mode_object_unregister - free a modeset identifer
304 * @dev: DRM device
305 * @object: object to free
306 *
307 * Free @id from @dev's unique identifier pool.
308 * This function can be called multiple times, and guards against
309 * multiple removals.
310 * These modeset identifiers are _not_ reference counted. Hence don't use this
311 * for reference counted modeset objects like framebuffers.
312 */
313 void drm_mode_object_unregister(struct drm_device *dev,
314 struct drm_mode_object *object)
315 {
316 mutex_lock(&dev->mode_config.idr_mutex);
317 if (object->id) {
318 idr_remove(&dev->mode_config.crtc_idr, object->id);
319 object->id = 0;
320 }
321 mutex_unlock(&dev->mode_config.idr_mutex);
322 }
323
324 static struct drm_mode_object *_object_find(struct drm_device *dev,
325 uint32_t id, uint32_t type)
326 {
327 struct drm_mode_object *obj = NULL;
328
329 mutex_lock(&dev->mode_config.idr_mutex);
330 obj = idr_find(&dev->mode_config.crtc_idr, id);
331 if (obj && type != DRM_MODE_OBJECT_ANY && obj->type != type)
332 obj = NULL;
333 if (obj && obj->id != id)
334 obj = NULL;
335
336 if (obj && obj->free_cb) {
337 if (!kref_get_unless_zero(&obj->refcount))
338 obj = NULL;
339 }
340 mutex_unlock(&dev->mode_config.idr_mutex);
341
342 return obj;
343 }
344
345 /**
346 * drm_mode_object_find - look up a drm object with static lifetime
347 * @dev: drm device
348 * @id: id of the mode object
349 * @type: type of the mode object
350 *
351 * This function is used to look up a modeset object. It will acquire a
352 * reference for reference counted objects. This reference must be dropped again
353 * by callind drm_mode_object_unreference().
354 */
355 struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
356 uint32_t id, uint32_t type)
357 {
358 struct drm_mode_object *obj = NULL;
359
360 obj = _object_find(dev, id, type);
361 return obj;
362 }
363 EXPORT_SYMBOL(drm_mode_object_find);
364
365 /**
366 * drm_mode_object_unreference - decr the object refcnt
367 * @obj: mode_object
368 *
369 * This functions decrements the object's refcount if it is a refcounted modeset
370 * object. It is a no-op on any other object. This is used to drop references
371 * acquired with drm_mode_object_reference().
372 */
373 void drm_mode_object_unreference(struct drm_mode_object *obj)
374 {
375 if (obj->free_cb) {
376 DRM_DEBUG("OBJ ID: %d (%d)\n", obj->id, atomic_read(&obj->refcount.refcount));
377 kref_put(&obj->refcount, obj->free_cb);
378 }
379 }
380 EXPORT_SYMBOL(drm_mode_object_unreference);
381
382 /**
383 * drm_mode_object_reference - incr the object refcnt
384 * @obj: mode_object
385 *
386 * This functions increments the object's refcount if it is a refcounted modeset
387 * object. It is a no-op on any other object. References should be dropped again
388 * by calling drm_mode_object_unreference().
389 */
390 void drm_mode_object_reference(struct drm_mode_object *obj)
391 {
392 if (obj->free_cb) {
393 DRM_DEBUG("OBJ ID: %d (%d)\n", obj->id, atomic_read(&obj->refcount.refcount));
394 kref_get(&obj->refcount);
395 }
396 }
397 EXPORT_SYMBOL(drm_mode_object_reference);
398
399 static void drm_framebuffer_free(struct kref *kref)
400 {
401 struct drm_framebuffer *fb =
402 container_of(kref, struct drm_framebuffer, base.refcount);
403 struct drm_device *dev = fb->dev;
404
405 /*
406 * The lookup idr holds a weak reference, which has not necessarily been
407 * removed at this point. Check for that.
408 */
409 drm_mode_object_unregister(dev, &fb->base);
410
411 fb->funcs->destroy(fb);
412 }
413
414 /**
415 * drm_framebuffer_init - initialize a framebuffer
416 * @dev: DRM device
417 * @fb: framebuffer to be initialized
418 * @funcs: ... with these functions
419 *
420 * Allocates an ID for the framebuffer's parent mode object, sets its mode
421 * functions & device file and adds it to the master fd list.
422 *
423 * IMPORTANT:
424 * This functions publishes the fb and makes it available for concurrent access
425 * by other users. Which means by this point the fb _must_ be fully set up -
426 * since all the fb attributes are invariant over its lifetime, no further
427 * locking but only correct reference counting is required.
428 *
429 * Returns:
430 * Zero on success, error code on failure.
431 */
432 int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
433 const struct drm_framebuffer_funcs *funcs)
434 {
435 int ret;
436
437 INIT_LIST_HEAD(&fb->filp_head);
438 fb->dev = dev;
439 fb->funcs = funcs;
440
441 ret = drm_mode_object_get_reg(dev, &fb->base, DRM_MODE_OBJECT_FB,
442 false, drm_framebuffer_free);
443 if (ret)
444 goto out;
445
446 mutex_lock(&dev->mode_config.fb_lock);
447 dev->mode_config.num_fb++;
448 list_add(&fb->head, &dev->mode_config.fb_list);
449 mutex_unlock(&dev->mode_config.fb_lock);
450
451 drm_mode_object_register(dev, &fb->base);
452 out:
453 return ret;
454 }
455 EXPORT_SYMBOL(drm_framebuffer_init);
456
457 /**
458 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
459 * @dev: drm device
460 * @id: id of the fb object
461 *
462 * If successful, this grabs an additional reference to the framebuffer -
463 * callers need to make sure to eventually unreference the returned framebuffer
464 * again, using @drm_framebuffer_unreference.
465 */
466 struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
467 uint32_t id)
468 {
469 struct drm_mode_object *obj;
470 struct drm_framebuffer *fb = NULL;
471
472 obj = _object_find(dev, id, DRM_MODE_OBJECT_FB);
473 if (obj)
474 fb = obj_to_fb(obj);
475 return fb;
476 }
477 EXPORT_SYMBOL(drm_framebuffer_lookup);
478
479 /**
480 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
481 * @fb: fb to unregister
482 *
483 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
484 * those used for fbdev. Note that the caller must hold a reference of it's own,
485 * i.e. the object may not be destroyed through this call (since it'll lead to a
486 * locking inversion).
487 */
488 void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
489 {
490 struct drm_device *dev;
491
492 if (!fb)
493 return;
494
495 dev = fb->dev;
496
497 /* Mark fb as reaped and drop idr ref. */
498 drm_mode_object_unregister(dev, &fb->base);
499 }
500 EXPORT_SYMBOL(drm_framebuffer_unregister_private);
501
502 /**
503 * drm_framebuffer_cleanup - remove a framebuffer object
504 * @fb: framebuffer to remove
505 *
506 * Cleanup framebuffer. This function is intended to be used from the drivers
507 * ->destroy callback. It can also be used to clean up driver private
508 * framebuffers embedded into a larger structure.
509 *
510 * Note that this function does not remove the fb from active usuage - if it is
511 * still used anywhere, hilarity can ensue since userspace could call getfb on
512 * the id and get back -EINVAL. Obviously no concern at driver unload time.
513 *
514 * Also, the framebuffer will not be removed from the lookup idr - for
515 * user-created framebuffers this will happen in in the rmfb ioctl. For
516 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
517 * drm_framebuffer_unregister_private.
518 */
519 void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
520 {
521 struct drm_device *dev = fb->dev;
522
523 mutex_lock(&dev->mode_config.fb_lock);
524 list_del(&fb->head);
525 dev->mode_config.num_fb--;
526 mutex_unlock(&dev->mode_config.fb_lock);
527 }
528 EXPORT_SYMBOL(drm_framebuffer_cleanup);
529
530 /**
531 * drm_framebuffer_remove - remove and unreference a framebuffer object
532 * @fb: framebuffer to remove
533 *
534 * Scans all the CRTCs and planes in @dev's mode_config. If they're
535 * using @fb, removes it, setting it to NULL. Then drops the reference to the
536 * passed-in framebuffer. Might take the modeset locks.
537 *
538 * Note that this function optimizes the cleanup away if the caller holds the
539 * last reference to the framebuffer. It is also guaranteed to not take the
540 * modeset locks in this case.
541 */
542 void drm_framebuffer_remove(struct drm_framebuffer *fb)
543 {
544 struct drm_device *dev;
545 struct drm_crtc *crtc;
546 struct drm_plane *plane;
547 struct drm_mode_set set;
548 int ret;
549
550 if (!fb)
551 return;
552
553 dev = fb->dev;
554
555 WARN_ON(!list_empty(&fb->filp_head));
556
557 /*
558 * drm ABI mandates that we remove any deleted framebuffers from active
559 * useage. But since most sane clients only remove framebuffers they no
560 * longer need, try to optimize this away.
561 *
562 * Since we're holding a reference ourselves, observing a refcount of 1
563 * means that we're the last holder and can skip it. Also, the refcount
564 * can never increase from 1 again, so we don't need any barriers or
565 * locks.
566 *
567 * Note that userspace could try to race with use and instate a new
568 * usage _after_ we've cleared all current ones. End result will be an
569 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
570 * in this manner.
571 */
572 if (drm_framebuffer_read_refcount(fb) > 1) {
573 drm_modeset_lock_all(dev);
574 /* remove from any CRTC */
575 drm_for_each_crtc(crtc, dev) {
576 if (crtc->primary->fb == fb) {
577 /* should turn off the crtc */
578 memset(&set, 0, sizeof(struct drm_mode_set));
579 set.crtc = crtc;
580 set.fb = NULL;
581 ret = drm_mode_set_config_internal(&set);
582 if (ret)
583 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
584 }
585 }
586
587 drm_for_each_plane(plane, dev) {
588 if (plane->fb == fb)
589 drm_plane_force_disable(plane);
590 }
591 drm_modeset_unlock_all(dev);
592 }
593
594 drm_framebuffer_unreference(fb);
595 }
596 EXPORT_SYMBOL(drm_framebuffer_remove);
597
598 DEFINE_WW_CLASS(crtc_ww_class);
599
600 static unsigned int drm_num_crtcs(struct drm_device *dev)
601 {
602 unsigned int num = 0;
603 struct drm_crtc *tmp;
604
605 drm_for_each_crtc(tmp, dev) {
606 num++;
607 }
608
609 return num;
610 }
611
612 static int drm_crtc_register_all(struct drm_device *dev)
613 {
614 struct drm_crtc *crtc;
615 int ret = 0;
616
617 drm_for_each_crtc(crtc, dev) {
618 if (crtc->funcs->late_register)
619 ret = crtc->funcs->late_register(crtc);
620 if (ret)
621 return ret;
622 }
623
624 return 0;
625 }
626
627 static void drm_crtc_unregister_all(struct drm_device *dev)
628 {
629 struct drm_crtc *crtc;
630
631 drm_for_each_crtc(crtc, dev) {
632 if (crtc->funcs->early_unregister)
633 crtc->funcs->early_unregister(crtc);
634 }
635 }
636
637 /**
638 * drm_crtc_init_with_planes - Initialise a new CRTC object with
639 * specified primary and cursor planes.
640 * @dev: DRM device
641 * @crtc: CRTC object to init
642 * @primary: Primary plane for CRTC
643 * @cursor: Cursor plane for CRTC
644 * @funcs: callbacks for the new CRTC
645 * @name: printf style format string for the CRTC name, or NULL for default name
646 *
647 * Inits a new object created as base part of a driver crtc object.
648 *
649 * Returns:
650 * Zero on success, error code on failure.
651 */
652 int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
653 struct drm_plane *primary,
654 struct drm_plane *cursor,
655 const struct drm_crtc_funcs *funcs,
656 const char *name, ...)
657 {
658 struct drm_mode_config *config = &dev->mode_config;
659 int ret;
660
661 WARN_ON(primary && primary->type != DRM_PLANE_TYPE_PRIMARY);
662 WARN_ON(cursor && cursor->type != DRM_PLANE_TYPE_CURSOR);
663
664 crtc->dev = dev;
665 crtc->funcs = funcs;
666
667 INIT_LIST_HEAD(&crtc->commit_list);
668 spin_lock_init(&crtc->commit_lock);
669
670 drm_modeset_lock_init(&crtc->mutex);
671 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
672 if (ret)
673 return ret;
674
675 if (name) {
676 va_list ap;
677
678 va_start(ap, name);
679 crtc->name = kvasprintf(GFP_KERNEL, name, ap);
680 va_end(ap);
681 } else {
682 crtc->name = kasprintf(GFP_KERNEL, "crtc-%d",
683 drm_num_crtcs(dev));
684 }
685 if (!crtc->name) {
686 drm_mode_object_unregister(dev, &crtc->base);
687 return -ENOMEM;
688 }
689
690 crtc->base.properties = &crtc->properties;
691
692 list_add_tail(&crtc->head, &config->crtc_list);
693 crtc->index = config->num_crtc++;
694
695 crtc->primary = primary;
696 crtc->cursor = cursor;
697 if (primary)
698 primary->possible_crtcs = 1 << drm_crtc_index(crtc);
699 if (cursor)
700 cursor->possible_crtcs = 1 << drm_crtc_index(crtc);
701
702 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
703 drm_object_attach_property(&crtc->base, config->prop_active, 0);
704 drm_object_attach_property(&crtc->base, config->prop_mode_id, 0);
705 }
706
707 return 0;
708 }
709 EXPORT_SYMBOL(drm_crtc_init_with_planes);
710
711 /**
712 * drm_crtc_cleanup - Clean up the core crtc usage
713 * @crtc: CRTC to cleanup
714 *
715 * This function cleans up @crtc and removes it from the DRM mode setting
716 * core. Note that the function does *not* free the crtc structure itself,
717 * this is the responsibility of the caller.
718 */
719 void drm_crtc_cleanup(struct drm_crtc *crtc)
720 {
721 struct drm_device *dev = crtc->dev;
722
723 /* Note that the crtc_list is considered to be static; should we
724 * remove the drm_crtc at runtime we would have to decrement all
725 * the indices on the drm_crtc after us in the crtc_list.
726 */
727
728 kfree(crtc->gamma_store);
729 crtc->gamma_store = NULL;
730
731 drm_modeset_lock_fini(&crtc->mutex);
732
733 drm_mode_object_unregister(dev, &crtc->base);
734 list_del(&crtc->head);
735 dev->mode_config.num_crtc--;
736
737 WARN_ON(crtc->state && !crtc->funcs->atomic_destroy_state);
738 if (crtc->state && crtc->funcs->atomic_destroy_state)
739 crtc->funcs->atomic_destroy_state(crtc, crtc->state);
740
741 kfree(crtc->name);
742
743 memset(crtc, 0, sizeof(*crtc));
744 }
745 EXPORT_SYMBOL(drm_crtc_cleanup);
746
747 /*
748 * drm_mode_remove - remove and free a mode
749 * @connector: connector list to modify
750 * @mode: mode to remove
751 *
752 * Remove @mode from @connector's mode list, then free it.
753 */
754 static void drm_mode_remove(struct drm_connector *connector,
755 struct drm_display_mode *mode)
756 {
757 list_del(&mode->head);
758 drm_mode_destroy(connector->dev, mode);
759 }
760
761 /**
762 * drm_display_info_set_bus_formats - set the supported bus formats
763 * @info: display info to store bus formats in
764 * @formats: array containing the supported bus formats
765 * @num_formats: the number of entries in the fmts array
766 *
767 * Store the supported bus formats in display info structure.
768 * See MEDIA_BUS_FMT_* definitions in include/uapi/linux/media-bus-format.h for
769 * a full list of available formats.
770 */
771 int drm_display_info_set_bus_formats(struct drm_display_info *info,
772 const u32 *formats,
773 unsigned int num_formats)
774 {
775 u32 *fmts = NULL;
776
777 if (!formats && num_formats)
778 return -EINVAL;
779
780 if (formats && num_formats) {
781 fmts = kmemdup(formats, sizeof(*formats) * num_formats,
782 GFP_KERNEL);
783 if (!fmts)
784 return -ENOMEM;
785 }
786
787 kfree(info->bus_formats);
788 info->bus_formats = fmts;
789 info->num_bus_formats = num_formats;
790
791 return 0;
792 }
793 EXPORT_SYMBOL(drm_display_info_set_bus_formats);
794
795 /**
796 * drm_connector_get_cmdline_mode - reads the user's cmdline mode
797 * @connector: connector to quwery
798 *
799 * The kernel supports per-connector configration of its consoles through
800 * use of the video= parameter. This function parses that option and
801 * extracts the user's specified mode (or enable/disable status) for a
802 * particular connector. This is typically only used during the early fbdev
803 * setup.
804 */
805 static void drm_connector_get_cmdline_mode(struct drm_connector *connector)
806 {
807 struct drm_cmdline_mode *mode = &connector->cmdline_mode;
808 char *option = NULL;
809
810 if (fb_get_options(connector->name, &option))
811 return;
812
813 if (!drm_mode_parse_command_line_for_connector(option,
814 connector,
815 mode))
816 return;
817
818 if (mode->force) {
819 const char *s;
820
821 switch (mode->force) {
822 case DRM_FORCE_OFF:
823 s = "OFF";
824 break;
825 case DRM_FORCE_ON_DIGITAL:
826 s = "ON - dig";
827 break;
828 default:
829 case DRM_FORCE_ON:
830 s = "ON";
831 break;
832 }
833
834 DRM_INFO("forcing %s connector %s\n", connector->name, s);
835 connector->force = mode->force;
836 }
837
838 DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
839 connector->name,
840 mode->xres, mode->yres,
841 mode->refresh_specified ? mode->refresh : 60,
842 mode->rb ? " reduced blanking" : "",
843 mode->margins ? " with margins" : "",
844 mode->interlace ? " interlaced" : "");
845 }
846
847 static void drm_connector_free(struct kref *kref)
848 {
849 struct drm_connector *connector =
850 container_of(kref, struct drm_connector, base.refcount);
851 struct drm_device *dev = connector->dev;
852
853 drm_mode_object_unregister(dev, &connector->base);
854 connector->funcs->destroy(connector);
855 }
856
857 /**
858 * drm_connector_init - Init a preallocated connector
859 * @dev: DRM device
860 * @connector: the connector to init
861 * @funcs: callbacks for this connector
862 * @connector_type: user visible type of the connector
863 *
864 * Initialises a preallocated connector. Connectors should be
865 * subclassed as part of driver connector objects.
866 *
867 * Returns:
868 * Zero on success, error code on failure.
869 */
870 int drm_connector_init(struct drm_device *dev,
871 struct drm_connector *connector,
872 const struct drm_connector_funcs *funcs,
873 int connector_type)
874 {
875 struct drm_mode_config *config = &dev->mode_config;
876 int ret;
877 struct ida *connector_ida =
878 &drm_connector_enum_list[connector_type].ida;
879
880 drm_modeset_lock_all(dev);
881
882 ret = drm_mode_object_get_reg(dev, &connector->base,
883 DRM_MODE_OBJECT_CONNECTOR,
884 false, drm_connector_free);
885 if (ret)
886 goto out_unlock;
887
888 connector->base.properties = &connector->properties;
889 connector->dev = dev;
890 connector->funcs = funcs;
891
892 connector->connector_id = ida_simple_get(&config->connector_ida, 0, 0, GFP_KERNEL);
893 if (connector->connector_id < 0) {
894 ret = connector->connector_id;
895 goto out_put;
896 }
897
898 connector->connector_type = connector_type;
899 connector->connector_type_id =
900 ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
901 if (connector->connector_type_id < 0) {
902 ret = connector->connector_type_id;
903 goto out_put_id;
904 }
905 connector->name =
906 kasprintf(GFP_KERNEL, "%s-%d",
907 drm_connector_enum_list[connector_type].name,
908 connector->connector_type_id);
909 if (!connector->name) {
910 ret = -ENOMEM;
911 goto out_put_type_id;
912 }
913
914 INIT_LIST_HEAD(&connector->probed_modes);
915 INIT_LIST_HEAD(&connector->modes);
916 connector->edid_blob_ptr = NULL;
917 connector->status = connector_status_unknown;
918
919 drm_connector_get_cmdline_mode(connector);
920
921 /* We should add connectors at the end to avoid upsetting the connector
922 * index too much. */
923 list_add_tail(&connector->head, &config->connector_list);
924 config->num_connector++;
925
926 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
927 drm_object_attach_property(&connector->base,
928 config->edid_property,
929 0);
930
931 drm_object_attach_property(&connector->base,
932 config->dpms_property, 0);
933
934 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
935 drm_object_attach_property(&connector->base, config->prop_crtc_id, 0);
936 }
937
938 connector->debugfs_entry = NULL;
939 out_put_type_id:
940 if (ret)
941 ida_remove(connector_ida, connector->connector_type_id);
942 out_put_id:
943 if (ret)
944 ida_remove(&config->connector_ida, connector->connector_id);
945 out_put:
946 if (ret)
947 drm_mode_object_unregister(dev, &connector->base);
948
949 out_unlock:
950 drm_modeset_unlock_all(dev);
951
952 return ret;
953 }
954 EXPORT_SYMBOL(drm_connector_init);
955
956 /**
957 * drm_connector_cleanup - cleans up an initialised connector
958 * @connector: connector to cleanup
959 *
960 * Cleans up the connector but doesn't free the object.
961 */
962 void drm_connector_cleanup(struct drm_connector *connector)
963 {
964 struct drm_device *dev = connector->dev;
965 struct drm_display_mode *mode, *t;
966
967 /* The connector should have been removed from userspace long before
968 * it is finally destroyed.
969 */
970 if (WARN_ON(connector->registered))
971 drm_connector_unregister(connector);
972
973 if (connector->tile_group) {
974 drm_mode_put_tile_group(dev, connector->tile_group);
975 connector->tile_group = NULL;
976 }
977
978 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
979 drm_mode_remove(connector, mode);
980
981 list_for_each_entry_safe(mode, t, &connector->modes, head)
982 drm_mode_remove(connector, mode);
983
984 ida_remove(&drm_connector_enum_list[connector->connector_type].ida,
985 connector->connector_type_id);
986
987 ida_remove(&dev->mode_config.connector_ida,
988 connector->connector_id);
989
990 kfree(connector->display_info.bus_formats);
991 drm_mode_object_unregister(dev, &connector->base);
992 kfree(connector->name);
993 connector->name = NULL;
994 list_del(&connector->head);
995 dev->mode_config.num_connector--;
996
997 WARN_ON(connector->state && !connector->funcs->atomic_destroy_state);
998 if (connector->state && connector->funcs->atomic_destroy_state)
999 connector->funcs->atomic_destroy_state(connector,
1000 connector->state);
1001
1002 memset(connector, 0, sizeof(*connector));
1003 }
1004 EXPORT_SYMBOL(drm_connector_cleanup);
1005
1006 /**
1007 * drm_connector_register - register a connector
1008 * @connector: the connector to register
1009 *
1010 * Register userspace interfaces for a connector
1011 *
1012 * Returns:
1013 * Zero on success, error code on failure.
1014 */
1015 int drm_connector_register(struct drm_connector *connector)
1016 {
1017 int ret;
1018
1019 if (connector->registered)
1020 return 0;
1021
1022 ret = drm_sysfs_connector_add(connector);
1023 if (ret)
1024 return ret;
1025
1026 ret = drm_debugfs_connector_add(connector);
1027 if (ret) {
1028 goto err_sysfs;
1029 }
1030
1031 if (connector->funcs->late_register) {
1032 ret = connector->funcs->late_register(connector);
1033 if (ret)
1034 goto err_debugfs;
1035 }
1036
1037 drm_mode_object_register(connector->dev, &connector->base);
1038
1039 connector->registered = true;
1040 return 0;
1041
1042 err_debugfs:
1043 drm_debugfs_connector_remove(connector);
1044 err_sysfs:
1045 drm_sysfs_connector_remove(connector);
1046 return ret;
1047 }
1048 EXPORT_SYMBOL(drm_connector_register);
1049
1050 /**
1051 * drm_connector_unregister - unregister a connector
1052 * @connector: the connector to unregister
1053 *
1054 * Unregister userspace interfaces for a connector
1055 */
1056 void drm_connector_unregister(struct drm_connector *connector)
1057 {
1058 if (!connector->registered)
1059 return;
1060
1061 if (connector->funcs->early_unregister)
1062 connector->funcs->early_unregister(connector);
1063
1064 drm_sysfs_connector_remove(connector);
1065 drm_debugfs_connector_remove(connector);
1066
1067 connector->registered = false;
1068 }
1069 EXPORT_SYMBOL(drm_connector_unregister);
1070
1071 /**
1072 * drm_connector_register_all - register all connectors
1073 * @dev: drm device
1074 *
1075 * This function registers all connectors in sysfs and other places so that
1076 * userspace can start to access them. drm_connector_register_all() is called
1077 * automatically from drm_dev_register() to complete the device registration,
1078 * if they don't call drm_connector_register() on each connector individually.
1079 *
1080 * When a device is unplugged and should be removed from userspace access,
1081 * call drm_connector_unregister_all(), which is the inverse of this
1082 * function.
1083 *
1084 * Returns:
1085 * Zero on success, error code on failure.
1086 */
1087 int drm_connector_register_all(struct drm_device *dev)
1088 {
1089 struct drm_connector *connector;
1090 int ret;
1091
1092 mutex_lock(&dev->mode_config.mutex);
1093
1094 drm_for_each_connector(connector, dev) {
1095 ret = drm_connector_register(connector);
1096 if (ret)
1097 goto err;
1098 }
1099
1100 mutex_unlock(&dev->mode_config.mutex);
1101
1102 return 0;
1103
1104 err:
1105 mutex_unlock(&dev->mode_config.mutex);
1106 drm_connector_unregister_all(dev);
1107 return ret;
1108 }
1109 EXPORT_SYMBOL(drm_connector_register_all);
1110
1111 /**
1112 * drm_connector_unregister_all - unregister connector userspace interfaces
1113 * @dev: drm device
1114 *
1115 * This functions unregisters all connectors from sysfs and other places so
1116 * that userspace can no longer access them. Drivers should call this as the
1117 * first step tearing down the device instace, or when the underlying
1118 * physical device disappeared (e.g. USB unplug), right before calling
1119 * drm_dev_unregister().
1120 */
1121 void drm_connector_unregister_all(struct drm_device *dev)
1122 {
1123 struct drm_connector *connector;
1124
1125 /* FIXME: taking the mode config mutex ends up in a clash with sysfs */
1126 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1127 drm_connector_unregister(connector);
1128 }
1129 EXPORT_SYMBOL(drm_connector_unregister_all);
1130
1131 static int drm_encoder_register_all(struct drm_device *dev)
1132 {
1133 struct drm_encoder *encoder;
1134 int ret = 0;
1135
1136 drm_for_each_encoder(encoder, dev) {
1137 if (encoder->funcs->late_register)
1138 ret = encoder->funcs->late_register(encoder);
1139 if (ret)
1140 return ret;
1141 }
1142
1143 return 0;
1144 }
1145
1146 static void drm_encoder_unregister_all(struct drm_device *dev)
1147 {
1148 struct drm_encoder *encoder;
1149
1150 drm_for_each_encoder(encoder, dev) {
1151 if (encoder->funcs->early_unregister)
1152 encoder->funcs->early_unregister(encoder);
1153 }
1154 }
1155
1156 /**
1157 * drm_encoder_init - Init a preallocated encoder
1158 * @dev: drm device
1159 * @encoder: the encoder to init
1160 * @funcs: callbacks for this encoder
1161 * @encoder_type: user visible type of the encoder
1162 * @name: printf style format string for the encoder name, or NULL for default name
1163 *
1164 * Initialises a preallocated encoder. Encoder should be
1165 * subclassed as part of driver encoder objects.
1166 *
1167 * Returns:
1168 * Zero on success, error code on failure.
1169 */
1170 int drm_encoder_init(struct drm_device *dev,
1171 struct drm_encoder *encoder,
1172 const struct drm_encoder_funcs *funcs,
1173 int encoder_type, const char *name, ...)
1174 {
1175 int ret;
1176
1177 drm_modeset_lock_all(dev);
1178
1179 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
1180 if (ret)
1181 goto out_unlock;
1182
1183 encoder->dev = dev;
1184 encoder->encoder_type = encoder_type;
1185 encoder->funcs = funcs;
1186 if (name) {
1187 va_list ap;
1188
1189 va_start(ap, name);
1190 encoder->name = kvasprintf(GFP_KERNEL, name, ap);
1191 va_end(ap);
1192 } else {
1193 encoder->name = kasprintf(GFP_KERNEL, "%s-%d",
1194 drm_encoder_enum_list[encoder_type].name,
1195 encoder->base.id);
1196 }
1197 if (!encoder->name) {
1198 ret = -ENOMEM;
1199 goto out_put;
1200 }
1201
1202 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
1203 encoder->index = dev->mode_config.num_encoder++;
1204
1205 out_put:
1206 if (ret)
1207 drm_mode_object_unregister(dev, &encoder->base);
1208
1209 out_unlock:
1210 drm_modeset_unlock_all(dev);
1211
1212 return ret;
1213 }
1214 EXPORT_SYMBOL(drm_encoder_init);
1215
1216 /**
1217 * drm_encoder_cleanup - cleans up an initialised encoder
1218 * @encoder: encoder to cleanup
1219 *
1220 * Cleans up the encoder but doesn't free the object.
1221 */
1222 void drm_encoder_cleanup(struct drm_encoder *encoder)
1223 {
1224 struct drm_device *dev = encoder->dev;
1225
1226 /* Note that the encoder_list is considered to be static; should we
1227 * remove the drm_encoder at runtime we would have to decrement all
1228 * the indices on the drm_encoder after us in the encoder_list.
1229 */
1230
1231 drm_modeset_lock_all(dev);
1232 drm_mode_object_unregister(dev, &encoder->base);
1233 kfree(encoder->name);
1234 list_del(&encoder->head);
1235 dev->mode_config.num_encoder--;
1236 drm_modeset_unlock_all(dev);
1237
1238 memset(encoder, 0, sizeof(*encoder));
1239 }
1240 EXPORT_SYMBOL(drm_encoder_cleanup);
1241
1242 static unsigned int drm_num_planes(struct drm_device *dev)
1243 {
1244 unsigned int num = 0;
1245 struct drm_plane *tmp;
1246
1247 drm_for_each_plane(tmp, dev) {
1248 num++;
1249 }
1250
1251 return num;
1252 }
1253
1254 /**
1255 * drm_universal_plane_init - Initialize a new universal plane object
1256 * @dev: DRM device
1257 * @plane: plane object to init
1258 * @possible_crtcs: bitmask of possible CRTCs
1259 * @funcs: callbacks for the new plane
1260 * @formats: array of supported formats (%DRM_FORMAT_*)
1261 * @format_count: number of elements in @formats
1262 * @type: type of plane (overlay, primary, cursor)
1263 * @name: printf style format string for the plane name, or NULL for default name
1264 *
1265 * Initializes a plane object of type @type.
1266 *
1267 * Returns:
1268 * Zero on success, error code on failure.
1269 */
1270 int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane,
1271 unsigned long possible_crtcs,
1272 const struct drm_plane_funcs *funcs,
1273 const uint32_t *formats, unsigned int format_count,
1274 enum drm_plane_type type,
1275 const char *name, ...)
1276 {
1277 struct drm_mode_config *config = &dev->mode_config;
1278 int ret;
1279
1280 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
1281 if (ret)
1282 return ret;
1283
1284 drm_modeset_lock_init(&plane->mutex);
1285
1286 plane->base.properties = &plane->properties;
1287 plane->dev = dev;
1288 plane->funcs = funcs;
1289 plane->format_types = kmalloc_array(format_count, sizeof(uint32_t),
1290 GFP_KERNEL);
1291 if (!plane->format_types) {
1292 DRM_DEBUG_KMS("out of memory when allocating plane\n");
1293 drm_mode_object_unregister(dev, &plane->base);
1294 return -ENOMEM;
1295 }
1296
1297 if (name) {
1298 va_list ap;
1299
1300 va_start(ap, name);
1301 plane->name = kvasprintf(GFP_KERNEL, name, ap);
1302 va_end(ap);
1303 } else {
1304 plane->name = kasprintf(GFP_KERNEL, "plane-%d",
1305 drm_num_planes(dev));
1306 }
1307 if (!plane->name) {
1308 kfree(plane->format_types);
1309 drm_mode_object_unregister(dev, &plane->base);
1310 return -ENOMEM;
1311 }
1312
1313 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
1314 plane->format_count = format_count;
1315 plane->possible_crtcs = possible_crtcs;
1316 plane->type = type;
1317
1318 list_add_tail(&plane->head, &config->plane_list);
1319 plane->index = config->num_total_plane++;
1320 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1321 config->num_overlay_plane++;
1322
1323 drm_object_attach_property(&plane->base,
1324 config->plane_type_property,
1325 plane->type);
1326
1327 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
1328 drm_object_attach_property(&plane->base, config->prop_fb_id, 0);
1329 drm_object_attach_property(&plane->base, config->prop_crtc_id, 0);
1330 drm_object_attach_property(&plane->base, config->prop_crtc_x, 0);
1331 drm_object_attach_property(&plane->base, config->prop_crtc_y, 0);
1332 drm_object_attach_property(&plane->base, config->prop_crtc_w, 0);
1333 drm_object_attach_property(&plane->base, config->prop_crtc_h, 0);
1334 drm_object_attach_property(&plane->base, config->prop_src_x, 0);
1335 drm_object_attach_property(&plane->base, config->prop_src_y, 0);
1336 drm_object_attach_property(&plane->base, config->prop_src_w, 0);
1337 drm_object_attach_property(&plane->base, config->prop_src_h, 0);
1338 }
1339
1340 return 0;
1341 }
1342 EXPORT_SYMBOL(drm_universal_plane_init);
1343
1344 static int drm_plane_register_all(struct drm_device *dev)
1345 {
1346 struct drm_plane *plane;
1347 int ret = 0;
1348
1349 drm_for_each_plane(plane, dev) {
1350 if (plane->funcs->late_register)
1351 ret = plane->funcs->late_register(plane);
1352 if (ret)
1353 return ret;
1354 }
1355
1356 return 0;
1357 }
1358
1359 static void drm_plane_unregister_all(struct drm_device *dev)
1360 {
1361 struct drm_plane *plane;
1362
1363 drm_for_each_plane(plane, dev) {
1364 if (plane->funcs->early_unregister)
1365 plane->funcs->early_unregister(plane);
1366 }
1367 }
1368
1369 /**
1370 * drm_plane_init - Initialize a legacy plane
1371 * @dev: DRM device
1372 * @plane: plane object to init
1373 * @possible_crtcs: bitmask of possible CRTCs
1374 * @funcs: callbacks for the new plane
1375 * @formats: array of supported formats (%DRM_FORMAT_*)
1376 * @format_count: number of elements in @formats
1377 * @is_primary: plane type (primary vs overlay)
1378 *
1379 * Legacy API to initialize a DRM plane.
1380 *
1381 * New drivers should call drm_universal_plane_init() instead.
1382 *
1383 * Returns:
1384 * Zero on success, error code on failure.
1385 */
1386 int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
1387 unsigned long possible_crtcs,
1388 const struct drm_plane_funcs *funcs,
1389 const uint32_t *formats, unsigned int format_count,
1390 bool is_primary)
1391 {
1392 enum drm_plane_type type;
1393
1394 type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
1395 return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
1396 formats, format_count, type, NULL);
1397 }
1398 EXPORT_SYMBOL(drm_plane_init);
1399
1400 /**
1401 * drm_plane_cleanup - Clean up the core plane usage
1402 * @plane: plane to cleanup
1403 *
1404 * This function cleans up @plane and removes it from the DRM mode setting
1405 * core. Note that the function does *not* free the plane structure itself,
1406 * this is the responsibility of the caller.
1407 */
1408 void drm_plane_cleanup(struct drm_plane *plane)
1409 {
1410 struct drm_device *dev = plane->dev;
1411
1412 drm_modeset_lock_all(dev);
1413 kfree(plane->format_types);
1414 drm_mode_object_unregister(dev, &plane->base);
1415
1416 BUG_ON(list_empty(&plane->head));
1417
1418 /* Note that the plane_list is considered to be static; should we
1419 * remove the drm_plane at runtime we would have to decrement all
1420 * the indices on the drm_plane after us in the plane_list.
1421 */
1422
1423 list_del(&plane->head);
1424 dev->mode_config.num_total_plane--;
1425 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1426 dev->mode_config.num_overlay_plane--;
1427 drm_modeset_unlock_all(dev);
1428
1429 WARN_ON(plane->state && !plane->funcs->atomic_destroy_state);
1430 if (plane->state && plane->funcs->atomic_destroy_state)
1431 plane->funcs->atomic_destroy_state(plane, plane->state);
1432
1433 kfree(plane->name);
1434
1435 memset(plane, 0, sizeof(*plane));
1436 }
1437 EXPORT_SYMBOL(drm_plane_cleanup);
1438
1439 /**
1440 * drm_plane_from_index - find the registered plane at an index
1441 * @dev: DRM device
1442 * @idx: index of registered plane to find for
1443 *
1444 * Given a plane index, return the registered plane from DRM device's
1445 * list of planes with matching index.
1446 */
1447 struct drm_plane *
1448 drm_plane_from_index(struct drm_device *dev, int idx)
1449 {
1450 struct drm_plane *plane;
1451
1452 drm_for_each_plane(plane, dev)
1453 if (idx == plane->index)
1454 return plane;
1455
1456 return NULL;
1457 }
1458 EXPORT_SYMBOL(drm_plane_from_index);
1459
1460 /**
1461 * drm_plane_force_disable - Forcibly disable a plane
1462 * @plane: plane to disable
1463 *
1464 * Forces the plane to be disabled.
1465 *
1466 * Used when the plane's current framebuffer is destroyed,
1467 * and when restoring fbdev mode.
1468 */
1469 void drm_plane_force_disable(struct drm_plane *plane)
1470 {
1471 int ret;
1472
1473 if (!plane->fb)
1474 return;
1475
1476 plane->old_fb = plane->fb;
1477 ret = plane->funcs->disable_plane(plane);
1478 if (ret) {
1479 DRM_ERROR("failed to disable plane with busy fb\n");
1480 plane->old_fb = NULL;
1481 return;
1482 }
1483 /* disconnect the plane from the fb and crtc: */
1484 drm_framebuffer_unreference(plane->old_fb);
1485 plane->old_fb = NULL;
1486 plane->fb = NULL;
1487 plane->crtc = NULL;
1488 }
1489 EXPORT_SYMBOL(drm_plane_force_disable);
1490
1491 int drm_modeset_register_all(struct drm_device *dev)
1492 {
1493 int ret;
1494
1495 ret = drm_plane_register_all(dev);
1496 if (ret)
1497 goto err_plane;
1498
1499 ret = drm_crtc_register_all(dev);
1500 if (ret)
1501 goto err_crtc;
1502
1503 ret = drm_encoder_register_all(dev);
1504 if (ret)
1505 goto err_encoder;
1506
1507 ret = drm_connector_register_all(dev);
1508 if (ret)
1509 goto err_connector;
1510
1511 return 0;
1512
1513 err_connector:
1514 drm_encoder_unregister_all(dev);
1515 err_encoder:
1516 drm_crtc_unregister_all(dev);
1517 err_crtc:
1518 drm_plane_unregister_all(dev);
1519 err_plane:
1520 return ret;
1521 }
1522
1523 void drm_modeset_unregister_all(struct drm_device *dev)
1524 {
1525 drm_connector_unregister_all(dev);
1526 drm_encoder_unregister_all(dev);
1527 drm_crtc_unregister_all(dev);
1528 drm_plane_unregister_all(dev);
1529 }
1530
1531 static int drm_mode_create_standard_properties(struct drm_device *dev)
1532 {
1533 struct drm_property *prop;
1534
1535 /*
1536 * Standard properties (apply to all connectors)
1537 */
1538 prop = drm_property_create(dev, DRM_MODE_PROP_BLOB |
1539 DRM_MODE_PROP_IMMUTABLE,
1540 "EDID", 0);
1541 if (!prop)
1542 return -ENOMEM;
1543 dev->mode_config.edid_property = prop;
1544
1545 prop = drm_property_create_enum(dev, 0,
1546 "DPMS", drm_dpms_enum_list,
1547 ARRAY_SIZE(drm_dpms_enum_list));
1548 if (!prop)
1549 return -ENOMEM;
1550 dev->mode_config.dpms_property = prop;
1551
1552 prop = drm_property_create(dev,
1553 DRM_MODE_PROP_BLOB |
1554 DRM_MODE_PROP_IMMUTABLE,
1555 "PATH", 0);
1556 if (!prop)
1557 return -ENOMEM;
1558 dev->mode_config.path_property = prop;
1559
1560 prop = drm_property_create(dev,
1561 DRM_MODE_PROP_BLOB |
1562 DRM_MODE_PROP_IMMUTABLE,
1563 "TILE", 0);
1564 if (!prop)
1565 return -ENOMEM;
1566 dev->mode_config.tile_property = prop;
1567
1568 prop = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1569 "type", drm_plane_type_enum_list,
1570 ARRAY_SIZE(drm_plane_type_enum_list));
1571 if (!prop)
1572 return -ENOMEM;
1573 dev->mode_config.plane_type_property = prop;
1574
1575 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1576 "SRC_X", 0, UINT_MAX);
1577 if (!prop)
1578 return -ENOMEM;
1579 dev->mode_config.prop_src_x = prop;
1580
1581 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1582 "SRC_Y", 0, UINT_MAX);
1583 if (!prop)
1584 return -ENOMEM;
1585 dev->mode_config.prop_src_y = prop;
1586
1587 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1588 "SRC_W", 0, UINT_MAX);
1589 if (!prop)
1590 return -ENOMEM;
1591 dev->mode_config.prop_src_w = prop;
1592
1593 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1594 "SRC_H", 0, UINT_MAX);
1595 if (!prop)
1596 return -ENOMEM;
1597 dev->mode_config.prop_src_h = prop;
1598
1599 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
1600 "CRTC_X", INT_MIN, INT_MAX);
1601 if (!prop)
1602 return -ENOMEM;
1603 dev->mode_config.prop_crtc_x = prop;
1604
1605 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
1606 "CRTC_Y", INT_MIN, INT_MAX);
1607 if (!prop)
1608 return -ENOMEM;
1609 dev->mode_config.prop_crtc_y = prop;
1610
1611 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1612 "CRTC_W", 0, INT_MAX);
1613 if (!prop)
1614 return -ENOMEM;
1615 dev->mode_config.prop_crtc_w = prop;
1616
1617 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1618 "CRTC_H", 0, INT_MAX);
1619 if (!prop)
1620 return -ENOMEM;
1621 dev->mode_config.prop_crtc_h = prop;
1622
1623 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
1624 "FB_ID", DRM_MODE_OBJECT_FB);
1625 if (!prop)
1626 return -ENOMEM;
1627 dev->mode_config.prop_fb_id = prop;
1628
1629 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
1630 "CRTC_ID", DRM_MODE_OBJECT_CRTC);
1631 if (!prop)
1632 return -ENOMEM;
1633 dev->mode_config.prop_crtc_id = prop;
1634
1635 prop = drm_property_create_bool(dev, DRM_MODE_PROP_ATOMIC,
1636 "ACTIVE");
1637 if (!prop)
1638 return -ENOMEM;
1639 dev->mode_config.prop_active = prop;
1640
1641 prop = drm_property_create(dev,
1642 DRM_MODE_PROP_ATOMIC | DRM_MODE_PROP_BLOB,
1643 "MODE_ID", 0);
1644 if (!prop)
1645 return -ENOMEM;
1646 dev->mode_config.prop_mode_id = prop;
1647
1648 prop = drm_property_create(dev,
1649 DRM_MODE_PROP_BLOB,
1650 "DEGAMMA_LUT", 0);
1651 if (!prop)
1652 return -ENOMEM;
1653 dev->mode_config.degamma_lut_property = prop;
1654
1655 prop = drm_property_create_range(dev,
1656 DRM_MODE_PROP_IMMUTABLE,
1657 "DEGAMMA_LUT_SIZE", 0, UINT_MAX);
1658 if (!prop)
1659 return -ENOMEM;
1660 dev->mode_config.degamma_lut_size_property = prop;
1661
1662 prop = drm_property_create(dev,
1663 DRM_MODE_PROP_BLOB,
1664 "CTM", 0);
1665 if (!prop)
1666 return -ENOMEM;
1667 dev->mode_config.ctm_property = prop;
1668
1669 prop = drm_property_create(dev,
1670 DRM_MODE_PROP_BLOB,
1671 "GAMMA_LUT", 0);
1672 if (!prop)
1673 return -ENOMEM;
1674 dev->mode_config.gamma_lut_property = prop;
1675
1676 prop = drm_property_create_range(dev,
1677 DRM_MODE_PROP_IMMUTABLE,
1678 "GAMMA_LUT_SIZE", 0, UINT_MAX);
1679 if (!prop)
1680 return -ENOMEM;
1681 dev->mode_config.gamma_lut_size_property = prop;
1682
1683 return 0;
1684 }
1685
1686 /**
1687 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1688 * @dev: DRM device
1689 *
1690 * Called by a driver the first time a DVI-I connector is made.
1691 */
1692 int drm_mode_create_dvi_i_properties(struct drm_device *dev)
1693 {
1694 struct drm_property *dvi_i_selector;
1695 struct drm_property *dvi_i_subconnector;
1696
1697 if (dev->mode_config.dvi_i_select_subconnector_property)
1698 return 0;
1699
1700 dvi_i_selector =
1701 drm_property_create_enum(dev, 0,
1702 "select subconnector",
1703 drm_dvi_i_select_enum_list,
1704 ARRAY_SIZE(drm_dvi_i_select_enum_list));
1705 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
1706
1707 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1708 "subconnector",
1709 drm_dvi_i_subconnector_enum_list,
1710 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
1711 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
1712
1713 return 0;
1714 }
1715 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
1716
1717 /**
1718 * drm_create_tv_properties - create TV specific connector properties
1719 * @dev: DRM device
1720 * @num_modes: number of different TV formats (modes) supported
1721 * @modes: array of pointers to strings containing name of each format
1722 *
1723 * Called by a driver's TV initialization routine, this function creates
1724 * the TV specific connector properties for a given device. Caller is
1725 * responsible for allocating a list of format names and passing them to
1726 * this routine.
1727 */
1728 int drm_mode_create_tv_properties(struct drm_device *dev,
1729 unsigned int num_modes,
1730 const char * const modes[])
1731 {
1732 struct drm_property *tv_selector;
1733 struct drm_property *tv_subconnector;
1734 unsigned int i;
1735
1736 if (dev->mode_config.tv_select_subconnector_property)
1737 return 0;
1738
1739 /*
1740 * Basic connector properties
1741 */
1742 tv_selector = drm_property_create_enum(dev, 0,
1743 "select subconnector",
1744 drm_tv_select_enum_list,
1745 ARRAY_SIZE(drm_tv_select_enum_list));
1746 if (!tv_selector)
1747 goto nomem;
1748
1749 dev->mode_config.tv_select_subconnector_property = tv_selector;
1750
1751 tv_subconnector =
1752 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1753 "subconnector",
1754 drm_tv_subconnector_enum_list,
1755 ARRAY_SIZE(drm_tv_subconnector_enum_list));
1756 if (!tv_subconnector)
1757 goto nomem;
1758 dev->mode_config.tv_subconnector_property = tv_subconnector;
1759
1760 /*
1761 * Other, TV specific properties: margins & TV modes.
1762 */
1763 dev->mode_config.tv_left_margin_property =
1764 drm_property_create_range(dev, 0, "left margin", 0, 100);
1765 if (!dev->mode_config.tv_left_margin_property)
1766 goto nomem;
1767
1768 dev->mode_config.tv_right_margin_property =
1769 drm_property_create_range(dev, 0, "right margin", 0, 100);
1770 if (!dev->mode_config.tv_right_margin_property)
1771 goto nomem;
1772
1773 dev->mode_config.tv_top_margin_property =
1774 drm_property_create_range(dev, 0, "top margin", 0, 100);
1775 if (!dev->mode_config.tv_top_margin_property)
1776 goto nomem;
1777
1778 dev->mode_config.tv_bottom_margin_property =
1779 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
1780 if (!dev->mode_config.tv_bottom_margin_property)
1781 goto nomem;
1782
1783 dev->mode_config.tv_mode_property =
1784 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1785 "mode", num_modes);
1786 if (!dev->mode_config.tv_mode_property)
1787 goto nomem;
1788
1789 for (i = 0; i < num_modes; i++)
1790 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1791 i, modes[i]);
1792
1793 dev->mode_config.tv_brightness_property =
1794 drm_property_create_range(dev, 0, "brightness", 0, 100);
1795 if (!dev->mode_config.tv_brightness_property)
1796 goto nomem;
1797
1798 dev->mode_config.tv_contrast_property =
1799 drm_property_create_range(dev, 0, "contrast", 0, 100);
1800 if (!dev->mode_config.tv_contrast_property)
1801 goto nomem;
1802
1803 dev->mode_config.tv_flicker_reduction_property =
1804 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
1805 if (!dev->mode_config.tv_flicker_reduction_property)
1806 goto nomem;
1807
1808 dev->mode_config.tv_overscan_property =
1809 drm_property_create_range(dev, 0, "overscan", 0, 100);
1810 if (!dev->mode_config.tv_overscan_property)
1811 goto nomem;
1812
1813 dev->mode_config.tv_saturation_property =
1814 drm_property_create_range(dev, 0, "saturation", 0, 100);
1815 if (!dev->mode_config.tv_saturation_property)
1816 goto nomem;
1817
1818 dev->mode_config.tv_hue_property =
1819 drm_property_create_range(dev, 0, "hue", 0, 100);
1820 if (!dev->mode_config.tv_hue_property)
1821 goto nomem;
1822
1823 return 0;
1824 nomem:
1825 return -ENOMEM;
1826 }
1827 EXPORT_SYMBOL(drm_mode_create_tv_properties);
1828
1829 /**
1830 * drm_mode_create_scaling_mode_property - create scaling mode property
1831 * @dev: DRM device
1832 *
1833 * Called by a driver the first time it's needed, must be attached to desired
1834 * connectors.
1835 */
1836 int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1837 {
1838 struct drm_property *scaling_mode;
1839
1840 if (dev->mode_config.scaling_mode_property)
1841 return 0;
1842
1843 scaling_mode =
1844 drm_property_create_enum(dev, 0, "scaling mode",
1845 drm_scaling_mode_enum_list,
1846 ARRAY_SIZE(drm_scaling_mode_enum_list));
1847
1848 dev->mode_config.scaling_mode_property = scaling_mode;
1849
1850 return 0;
1851 }
1852 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1853
1854 /**
1855 * drm_mode_create_aspect_ratio_property - create aspect ratio property
1856 * @dev: DRM device
1857 *
1858 * Called by a driver the first time it's needed, must be attached to desired
1859 * connectors.
1860 *
1861 * Returns:
1862 * Zero on success, negative errno on failure.
1863 */
1864 int drm_mode_create_aspect_ratio_property(struct drm_device *dev)
1865 {
1866 if (dev->mode_config.aspect_ratio_property)
1867 return 0;
1868
1869 dev->mode_config.aspect_ratio_property =
1870 drm_property_create_enum(dev, 0, "aspect ratio",
1871 drm_aspect_ratio_enum_list,
1872 ARRAY_SIZE(drm_aspect_ratio_enum_list));
1873
1874 if (dev->mode_config.aspect_ratio_property == NULL)
1875 return -ENOMEM;
1876
1877 return 0;
1878 }
1879 EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property);
1880
1881 /**
1882 * drm_mode_create_dirty_property - create dirty property
1883 * @dev: DRM device
1884 *
1885 * Called by a driver the first time it's needed, must be attached to desired
1886 * connectors.
1887 */
1888 int drm_mode_create_dirty_info_property(struct drm_device *dev)
1889 {
1890 struct drm_property *dirty_info;
1891
1892 if (dev->mode_config.dirty_info_property)
1893 return 0;
1894
1895 dirty_info =
1896 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1897 "dirty",
1898 drm_dirty_info_enum_list,
1899 ARRAY_SIZE(drm_dirty_info_enum_list));
1900 dev->mode_config.dirty_info_property = dirty_info;
1901
1902 return 0;
1903 }
1904 EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1905
1906 /**
1907 * drm_mode_create_suggested_offset_properties - create suggests offset properties
1908 * @dev: DRM device
1909 *
1910 * Create the the suggested x/y offset property for connectors.
1911 */
1912 int drm_mode_create_suggested_offset_properties(struct drm_device *dev)
1913 {
1914 if (dev->mode_config.suggested_x_property && dev->mode_config.suggested_y_property)
1915 return 0;
1916
1917 dev->mode_config.suggested_x_property =
1918 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested X", 0, 0xffffffff);
1919
1920 dev->mode_config.suggested_y_property =
1921 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested Y", 0, 0xffffffff);
1922
1923 if (dev->mode_config.suggested_x_property == NULL ||
1924 dev->mode_config.suggested_y_property == NULL)
1925 return -ENOMEM;
1926 return 0;
1927 }
1928 EXPORT_SYMBOL(drm_mode_create_suggested_offset_properties);
1929
1930 /**
1931 * drm_mode_getresources - get graphics configuration
1932 * @dev: drm device for the ioctl
1933 * @data: data pointer for the ioctl
1934 * @file_priv: drm file for the ioctl call
1935 *
1936 * Construct a set of configuration description structures and return
1937 * them to the user, including CRTC, connector and framebuffer configuration.
1938 *
1939 * Called by the user via ioctl.
1940 *
1941 * Returns:
1942 * Zero on success, negative errno on failure.
1943 */
1944 int drm_mode_getresources(struct drm_device *dev, void *data,
1945 struct drm_file *file_priv)
1946 {
1947 struct drm_mode_card_res *card_res = data;
1948 struct list_head *lh;
1949 struct drm_framebuffer *fb;
1950 struct drm_connector *connector;
1951 struct drm_crtc *crtc;
1952 struct drm_encoder *encoder;
1953 int ret = 0;
1954 int connector_count = 0;
1955 int crtc_count = 0;
1956 int fb_count = 0;
1957 int encoder_count = 0;
1958 int copied = 0;
1959 uint32_t __user *fb_id;
1960 uint32_t __user *crtc_id;
1961 uint32_t __user *connector_id;
1962 uint32_t __user *encoder_id;
1963
1964 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1965 return -EINVAL;
1966
1967
1968 mutex_lock(&file_priv->fbs_lock);
1969 /*
1970 * For the non-control nodes we need to limit the list of resources
1971 * by IDs in the group list for this node
1972 */
1973 list_for_each(lh, &file_priv->fbs)
1974 fb_count++;
1975
1976 /* handle this in 4 parts */
1977 /* FBs */
1978 if (card_res->count_fbs >= fb_count) {
1979 copied = 0;
1980 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1981 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1982 if (put_user(fb->base.id, fb_id + copied)) {
1983 mutex_unlock(&file_priv->fbs_lock);
1984 return -EFAULT;
1985 }
1986 copied++;
1987 }
1988 }
1989 card_res->count_fbs = fb_count;
1990 mutex_unlock(&file_priv->fbs_lock);
1991
1992 /* mode_config.mutex protects the connector list against e.g. DP MST
1993 * connector hot-adding. CRTC/Plane lists are invariant. */
1994 mutex_lock(&dev->mode_config.mutex);
1995 drm_for_each_crtc(crtc, dev)
1996 crtc_count++;
1997
1998 drm_for_each_connector(connector, dev)
1999 connector_count++;
2000
2001 drm_for_each_encoder(encoder, dev)
2002 encoder_count++;
2003
2004 card_res->max_height = dev->mode_config.max_height;
2005 card_res->min_height = dev->mode_config.min_height;
2006 card_res->max_width = dev->mode_config.max_width;
2007 card_res->min_width = dev->mode_config.min_width;
2008
2009 /* CRTCs */
2010 if (card_res->count_crtcs >= crtc_count) {
2011 copied = 0;
2012 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
2013 drm_for_each_crtc(crtc, dev) {
2014 if (put_user(crtc->base.id, crtc_id + copied)) {
2015 ret = -EFAULT;
2016 goto out;
2017 }
2018 copied++;
2019 }
2020 }
2021 card_res->count_crtcs = crtc_count;
2022
2023 /* Encoders */
2024 if (card_res->count_encoders >= encoder_count) {
2025 copied = 0;
2026 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
2027 drm_for_each_encoder(encoder, dev) {
2028 if (put_user(encoder->base.id, encoder_id +
2029 copied)) {
2030 ret = -EFAULT;
2031 goto out;
2032 }
2033 copied++;
2034 }
2035 }
2036 card_res->count_encoders = encoder_count;
2037
2038 /* Connectors */
2039 if (card_res->count_connectors >= connector_count) {
2040 copied = 0;
2041 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
2042 drm_for_each_connector(connector, dev) {
2043 if (put_user(connector->base.id,
2044 connector_id + copied)) {
2045 ret = -EFAULT;
2046 goto out;
2047 }
2048 copied++;
2049 }
2050 }
2051 card_res->count_connectors = connector_count;
2052
2053 out:
2054 mutex_unlock(&dev->mode_config.mutex);
2055 return ret;
2056 }
2057
2058 /**
2059 * drm_mode_getcrtc - get CRTC configuration
2060 * @dev: drm device for the ioctl
2061 * @data: data pointer for the ioctl
2062 * @file_priv: drm file for the ioctl call
2063 *
2064 * Construct a CRTC configuration structure to return to the user.
2065 *
2066 * Called by the user via ioctl.
2067 *
2068 * Returns:
2069 * Zero on success, negative errno on failure.
2070 */
2071 int drm_mode_getcrtc(struct drm_device *dev,
2072 void *data, struct drm_file *file_priv)
2073 {
2074 struct drm_mode_crtc *crtc_resp = data;
2075 struct drm_crtc *crtc;
2076
2077 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2078 return -EINVAL;
2079
2080 crtc = drm_crtc_find(dev, crtc_resp->crtc_id);
2081 if (!crtc)
2082 return -ENOENT;
2083
2084 drm_modeset_lock_crtc(crtc, crtc->primary);
2085 crtc_resp->gamma_size = crtc->gamma_size;
2086 if (crtc->primary->fb)
2087 crtc_resp->fb_id = crtc->primary->fb->base.id;
2088 else
2089 crtc_resp->fb_id = 0;
2090
2091 if (crtc->state) {
2092 crtc_resp->x = crtc->primary->state->src_x >> 16;
2093 crtc_resp->y = crtc->primary->state->src_y >> 16;
2094 if (crtc->state->enable) {
2095 drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->state->mode);
2096 crtc_resp->mode_valid = 1;
2097
2098 } else {
2099 crtc_resp->mode_valid = 0;
2100 }
2101 } else {
2102 crtc_resp->x = crtc->x;
2103 crtc_resp->y = crtc->y;
2104 if (crtc->enabled) {
2105 drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->mode);
2106 crtc_resp->mode_valid = 1;
2107
2108 } else {
2109 crtc_resp->mode_valid = 0;
2110 }
2111 }
2112 drm_modeset_unlock_crtc(crtc);
2113
2114 return 0;
2115 }
2116
2117 static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
2118 const struct drm_file *file_priv)
2119 {
2120 /*
2121 * If user-space hasn't configured the driver to expose the stereo 3D
2122 * modes, don't expose them.
2123 */
2124 if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
2125 return false;
2126
2127 return true;
2128 }
2129
2130 static struct drm_encoder *drm_connector_get_encoder(struct drm_connector *connector)
2131 {
2132 /* For atomic drivers only state objects are synchronously updated and
2133 * protected by modeset locks, so check those first. */
2134 if (connector->state)
2135 return connector->state->best_encoder;
2136 return connector->encoder;
2137 }
2138
2139 /* helper for getconnector and getproperties ioctls */
2140 static int get_properties(struct drm_mode_object *obj, bool atomic,
2141 uint32_t __user *prop_ptr, uint64_t __user *prop_values,
2142 uint32_t *arg_count_props)
2143 {
2144 int props_count;
2145 int i, ret, copied;
2146
2147 props_count = obj->properties->count;
2148 if (!atomic)
2149 props_count -= obj->properties->atomic_count;
2150
2151 if ((*arg_count_props >= props_count) && props_count) {
2152 for (i = 0, copied = 0; copied < props_count; i++) {
2153 struct drm_property *prop = obj->properties->properties[i];
2154 uint64_t val;
2155
2156 if ((prop->flags & DRM_MODE_PROP_ATOMIC) && !atomic)
2157 continue;
2158
2159 ret = drm_object_property_get_value(obj, prop, &val);
2160 if (ret)
2161 return ret;
2162
2163 if (put_user(prop->base.id, prop_ptr + copied))
2164 return -EFAULT;
2165
2166 if (put_user(val, prop_values + copied))
2167 return -EFAULT;
2168
2169 copied++;
2170 }
2171 }
2172 *arg_count_props = props_count;
2173
2174 return 0;
2175 }
2176
2177 /**
2178 * drm_mode_getconnector - get connector configuration
2179 * @dev: drm device for the ioctl
2180 * @data: data pointer for the ioctl
2181 * @file_priv: drm file for the ioctl call
2182 *
2183 * Construct a connector configuration structure to return to the user.
2184 *
2185 * Called by the user via ioctl.
2186 *
2187 * Returns:
2188 * Zero on success, negative errno on failure.
2189 */
2190 int drm_mode_getconnector(struct drm_device *dev, void *data,
2191 struct drm_file *file_priv)
2192 {
2193 struct drm_mode_get_connector *out_resp = data;
2194 struct drm_connector *connector;
2195 struct drm_encoder *encoder;
2196 struct drm_display_mode *mode;
2197 int mode_count = 0;
2198 int encoders_count = 0;
2199 int ret = 0;
2200 int copied = 0;
2201 int i;
2202 struct drm_mode_modeinfo u_mode;
2203 struct drm_mode_modeinfo __user *mode_ptr;
2204 uint32_t __user *encoder_ptr;
2205
2206 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2207 return -EINVAL;
2208
2209 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
2210
2211 mutex_lock(&dev->mode_config.mutex);
2212
2213 connector = drm_connector_lookup(dev, out_resp->connector_id);
2214 if (!connector) {
2215 ret = -ENOENT;
2216 goto out_unlock;
2217 }
2218
2219 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++)
2220 if (connector->encoder_ids[i] != 0)
2221 encoders_count++;
2222
2223 if (out_resp->count_modes == 0) {
2224 connector->funcs->fill_modes(connector,
2225 dev->mode_config.max_width,
2226 dev->mode_config.max_height);
2227 }
2228
2229 /* delayed so we get modes regardless of pre-fill_modes state */
2230 list_for_each_entry(mode, &connector->modes, head)
2231 if (drm_mode_expose_to_userspace(mode, file_priv))
2232 mode_count++;
2233
2234 out_resp->connector_id = connector->base.id;
2235 out_resp->connector_type = connector->connector_type;
2236 out_resp->connector_type_id = connector->connector_type_id;
2237 out_resp->mm_width = connector->display_info.width_mm;
2238 out_resp->mm_height = connector->display_info.height_mm;
2239 out_resp->subpixel = connector->display_info.subpixel_order;
2240 out_resp->connection = connector->status;
2241
2242 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
2243 encoder = drm_connector_get_encoder(connector);
2244 if (encoder)
2245 out_resp->encoder_id = encoder->base.id;
2246 else
2247 out_resp->encoder_id = 0;
2248
2249 /*
2250 * This ioctl is called twice, once to determine how much space is
2251 * needed, and the 2nd time to fill it.
2252 */
2253 if ((out_resp->count_modes >= mode_count) && mode_count) {
2254 copied = 0;
2255 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
2256 list_for_each_entry(mode, &connector->modes, head) {
2257 if (!drm_mode_expose_to_userspace(mode, file_priv))
2258 continue;
2259
2260 drm_mode_convert_to_umode(&u_mode, mode);
2261 if (copy_to_user(mode_ptr + copied,
2262 &u_mode, sizeof(u_mode))) {
2263 ret = -EFAULT;
2264 goto out;
2265 }
2266 copied++;
2267 }
2268 }
2269 out_resp->count_modes = mode_count;
2270
2271 ret = get_properties(&connector->base, file_priv->atomic,
2272 (uint32_t __user *)(unsigned long)(out_resp->props_ptr),
2273 (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr),
2274 &out_resp->count_props);
2275 if (ret)
2276 goto out;
2277
2278 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
2279 copied = 0;
2280 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
2281 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2282 if (connector->encoder_ids[i] != 0) {
2283 if (put_user(connector->encoder_ids[i],
2284 encoder_ptr + copied)) {
2285 ret = -EFAULT;
2286 goto out;
2287 }
2288 copied++;
2289 }
2290 }
2291 }
2292 out_resp->count_encoders = encoders_count;
2293
2294 out:
2295 drm_modeset_unlock(&dev->mode_config.connection_mutex);
2296
2297 drm_connector_unreference(connector);
2298 out_unlock:
2299 mutex_unlock(&dev->mode_config.mutex);
2300
2301 return ret;
2302 }
2303
2304 static struct drm_crtc *drm_encoder_get_crtc(struct drm_encoder *encoder)
2305 {
2306 struct drm_connector *connector;
2307 struct drm_device *dev = encoder->dev;
2308 bool uses_atomic = false;
2309
2310 /* For atomic drivers only state objects are synchronously updated and
2311 * protected by modeset locks, so check those first. */
2312 drm_for_each_connector(connector, dev) {
2313 if (!connector->state)
2314 continue;
2315
2316 uses_atomic = true;
2317
2318 if (connector->state->best_encoder != encoder)
2319 continue;
2320
2321 return connector->state->crtc;
2322 }
2323
2324 /* Don't return stale data (e.g. pending async disable). */
2325 if (uses_atomic)
2326 return NULL;
2327
2328 return encoder->crtc;
2329 }
2330
2331 /**
2332 * drm_mode_getencoder - get encoder configuration
2333 * @dev: drm device for the ioctl
2334 * @data: data pointer for the ioctl
2335 * @file_priv: drm file for the ioctl call
2336 *
2337 * Construct a encoder configuration structure to return to the user.
2338 *
2339 * Called by the user via ioctl.
2340 *
2341 * Returns:
2342 * Zero on success, negative errno on failure.
2343 */
2344 int drm_mode_getencoder(struct drm_device *dev, void *data,
2345 struct drm_file *file_priv)
2346 {
2347 struct drm_mode_get_encoder *enc_resp = data;
2348 struct drm_encoder *encoder;
2349 struct drm_crtc *crtc;
2350
2351 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2352 return -EINVAL;
2353
2354 encoder = drm_encoder_find(dev, enc_resp->encoder_id);
2355 if (!encoder)
2356 return -ENOENT;
2357
2358 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
2359 crtc = drm_encoder_get_crtc(encoder);
2360 if (crtc)
2361 enc_resp->crtc_id = crtc->base.id;
2362 else
2363 enc_resp->crtc_id = 0;
2364 drm_modeset_unlock(&dev->mode_config.connection_mutex);
2365
2366 enc_resp->encoder_type = encoder->encoder_type;
2367 enc_resp->encoder_id = encoder->base.id;
2368 enc_resp->possible_crtcs = encoder->possible_crtcs;
2369 enc_resp->possible_clones = encoder->possible_clones;
2370
2371 return 0;
2372 }
2373
2374 /**
2375 * drm_mode_getplane_res - enumerate all plane resources
2376 * @dev: DRM device
2377 * @data: ioctl data
2378 * @file_priv: DRM file info
2379 *
2380 * Construct a list of plane ids to return to the user.
2381 *
2382 * Called by the user via ioctl.
2383 *
2384 * Returns:
2385 * Zero on success, negative errno on failure.
2386 */
2387 int drm_mode_getplane_res(struct drm_device *dev, void *data,
2388 struct drm_file *file_priv)
2389 {
2390 struct drm_mode_get_plane_res *plane_resp = data;
2391 struct drm_mode_config *config;
2392 struct drm_plane *plane;
2393 uint32_t __user *plane_ptr;
2394 int copied = 0;
2395 unsigned num_planes;
2396
2397 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2398 return -EINVAL;
2399
2400 config = &dev->mode_config;
2401
2402 if (file_priv->universal_planes)
2403 num_planes = config->num_total_plane;
2404 else
2405 num_planes = config->num_overlay_plane;
2406
2407 /*
2408 * This ioctl is called twice, once to determine how much space is
2409 * needed, and the 2nd time to fill it.
2410 */
2411 if (num_planes &&
2412 (plane_resp->count_planes >= num_planes)) {
2413 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
2414
2415 /* Plane lists are invariant, no locking needed. */
2416 drm_for_each_plane(plane, dev) {
2417 /*
2418 * Unless userspace set the 'universal planes'
2419 * capability bit, only advertise overlays.
2420 */
2421 if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
2422 !file_priv->universal_planes)
2423 continue;
2424
2425 if (put_user(plane->base.id, plane_ptr + copied))
2426 return -EFAULT;
2427 copied++;
2428 }
2429 }
2430 plane_resp->count_planes = num_planes;
2431
2432 return 0;
2433 }
2434
2435 /**
2436 * drm_mode_getplane - get plane configuration
2437 * @dev: DRM device
2438 * @data: ioctl data
2439 * @file_priv: DRM file info
2440 *
2441 * Construct a plane configuration structure to return to the user.
2442 *
2443 * Called by the user via ioctl.
2444 *
2445 * Returns:
2446 * Zero on success, negative errno on failure.
2447 */
2448 int drm_mode_getplane(struct drm_device *dev, void *data,
2449 struct drm_file *file_priv)
2450 {
2451 struct drm_mode_get_plane *plane_resp = data;
2452 struct drm_plane *plane;
2453 uint32_t __user *format_ptr;
2454
2455 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2456 return -EINVAL;
2457
2458 plane = drm_plane_find(dev, plane_resp->plane_id);
2459 if (!plane)
2460 return -ENOENT;
2461
2462 drm_modeset_lock(&plane->mutex, NULL);
2463 if (plane->crtc)
2464 plane_resp->crtc_id = plane->crtc->base.id;
2465 else
2466 plane_resp->crtc_id = 0;
2467
2468 if (plane->fb)
2469 plane_resp->fb_id = plane->fb->base.id;
2470 else
2471 plane_resp->fb_id = 0;
2472 drm_modeset_unlock(&plane->mutex);
2473
2474 plane_resp->plane_id = plane->base.id;
2475 plane_resp->possible_crtcs = plane->possible_crtcs;
2476 plane_resp->gamma_size = 0;
2477
2478 /*
2479 * This ioctl is called twice, once to determine how much space is
2480 * needed, and the 2nd time to fill it.
2481 */
2482 if (plane->format_count &&
2483 (plane_resp->count_format_types >= plane->format_count)) {
2484 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
2485 if (copy_to_user(format_ptr,
2486 plane->format_types,
2487 sizeof(uint32_t) * plane->format_count)) {
2488 return -EFAULT;
2489 }
2490 }
2491 plane_resp->count_format_types = plane->format_count;
2492
2493 return 0;
2494 }
2495
2496 /**
2497 * drm_plane_check_pixel_format - Check if the plane supports the pixel format
2498 * @plane: plane to check for format support
2499 * @format: the pixel format
2500 *
2501 * Returns:
2502 * Zero of @plane has @format in its list of supported pixel formats, -EINVAL
2503 * otherwise.
2504 */
2505 int drm_plane_check_pixel_format(const struct drm_plane *plane, u32 format)
2506 {
2507 unsigned int i;
2508
2509 for (i = 0; i < plane->format_count; i++) {
2510 if (format == plane->format_types[i])
2511 return 0;
2512 }
2513
2514 return -EINVAL;
2515 }
2516
2517 static int check_src_coords(uint32_t src_x, uint32_t src_y,
2518 uint32_t src_w, uint32_t src_h,
2519 const struct drm_framebuffer *fb)
2520 {
2521 unsigned int fb_width, fb_height;
2522
2523 fb_width = fb->width << 16;
2524 fb_height = fb->height << 16;
2525
2526 /* Make sure source coordinates are inside the fb. */
2527 if (src_w > fb_width ||
2528 src_x > fb_width - src_w ||
2529 src_h > fb_height ||
2530 src_y > fb_height - src_h) {
2531 DRM_DEBUG_KMS("Invalid source coordinates "
2532 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
2533 src_w >> 16, ((src_w & 0xffff) * 15625) >> 10,
2534 src_h >> 16, ((src_h & 0xffff) * 15625) >> 10,
2535 src_x >> 16, ((src_x & 0xffff) * 15625) >> 10,
2536 src_y >> 16, ((src_y & 0xffff) * 15625) >> 10);
2537 return -ENOSPC;
2538 }
2539
2540 return 0;
2541 }
2542
2543 /*
2544 * setplane_internal - setplane handler for internal callers
2545 *
2546 * Note that we assume an extra reference has already been taken on fb. If the
2547 * update fails, this reference will be dropped before return; if it succeeds,
2548 * the previous framebuffer (if any) will be unreferenced instead.
2549 *
2550 * src_{x,y,w,h} are provided in 16.16 fixed point format
2551 */
2552 static int __setplane_internal(struct drm_plane *plane,
2553 struct drm_crtc *crtc,
2554 struct drm_framebuffer *fb,
2555 int32_t crtc_x, int32_t crtc_y,
2556 uint32_t crtc_w, uint32_t crtc_h,
2557 /* src_{x,y,w,h} values are 16.16 fixed point */
2558 uint32_t src_x, uint32_t src_y,
2559 uint32_t src_w, uint32_t src_h)
2560 {
2561 int ret = 0;
2562
2563 /* No fb means shut it down */
2564 if (!fb) {
2565 plane->old_fb = plane->fb;
2566 ret = plane->funcs->disable_plane(plane);
2567 if (!ret) {
2568 plane->crtc = NULL;
2569 plane->fb = NULL;
2570 } else {
2571 plane->old_fb = NULL;
2572 }
2573 goto out;
2574 }
2575
2576 /* Check whether this plane is usable on this CRTC */
2577 if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
2578 DRM_DEBUG_KMS("Invalid crtc for plane\n");
2579 ret = -EINVAL;
2580 goto out;
2581 }
2582
2583 /* Check whether this plane supports the fb pixel format. */
2584 ret = drm_plane_check_pixel_format(plane, fb->pixel_format);
2585 if (ret) {
2586 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2587 drm_get_format_name(fb->pixel_format));
2588 goto out;
2589 }
2590
2591 /* Give drivers some help against integer overflows */
2592 if (crtc_w > INT_MAX ||
2593 crtc_x > INT_MAX - (int32_t) crtc_w ||
2594 crtc_h > INT_MAX ||
2595 crtc_y > INT_MAX - (int32_t) crtc_h) {
2596 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
2597 crtc_w, crtc_h, crtc_x, crtc_y);
2598 ret = -ERANGE;
2599 goto out;
2600 }
2601
2602 ret = check_src_coords(src_x, src_y, src_w, src_h, fb);
2603 if (ret)
2604 goto out;
2605
2606 plane->old_fb = plane->fb;
2607 ret = plane->funcs->update_plane(plane, crtc, fb,
2608 crtc_x, crtc_y, crtc_w, crtc_h,
2609 src_x, src_y, src_w, src_h);
2610 if (!ret) {
2611 plane->crtc = crtc;
2612 plane->fb = fb;
2613 fb = NULL;
2614 } else {
2615 plane->old_fb = NULL;
2616 }
2617
2618 out:
2619 if (fb)
2620 drm_framebuffer_unreference(fb);
2621 if (plane->old_fb)
2622 drm_framebuffer_unreference(plane->old_fb);
2623 plane->old_fb = NULL;
2624
2625 return ret;
2626 }
2627
2628 static int setplane_internal(struct drm_plane *plane,
2629 struct drm_crtc *crtc,
2630 struct drm_framebuffer *fb,
2631 int32_t crtc_x, int32_t crtc_y,
2632 uint32_t crtc_w, uint32_t crtc_h,
2633 /* src_{x,y,w,h} values are 16.16 fixed point */
2634 uint32_t src_x, uint32_t src_y,
2635 uint32_t src_w, uint32_t src_h)
2636 {
2637 int ret;
2638
2639 drm_modeset_lock_all(plane->dev);
2640 ret = __setplane_internal(plane, crtc, fb,
2641 crtc_x, crtc_y, crtc_w, crtc_h,
2642 src_x, src_y, src_w, src_h);
2643 drm_modeset_unlock_all(plane->dev);
2644
2645 return ret;
2646 }
2647
2648 /**
2649 * drm_mode_setplane - configure a plane's configuration
2650 * @dev: DRM device
2651 * @data: ioctl data*
2652 * @file_priv: DRM file info
2653 *
2654 * Set plane configuration, including placement, fb, scaling, and other factors.
2655 * Or pass a NULL fb to disable (planes may be disabled without providing a
2656 * valid crtc).
2657 *
2658 * Returns:
2659 * Zero on success, negative errno on failure.
2660 */
2661 int drm_mode_setplane(struct drm_device *dev, void *data,
2662 struct drm_file *file_priv)
2663 {
2664 struct drm_mode_set_plane *plane_req = data;
2665 struct drm_plane *plane;
2666 struct drm_crtc *crtc = NULL;
2667 struct drm_framebuffer *fb = NULL;
2668
2669 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2670 return -EINVAL;
2671
2672 /*
2673 * First, find the plane, crtc, and fb objects. If not available,
2674 * we don't bother to call the driver.
2675 */
2676 plane = drm_plane_find(dev, plane_req->plane_id);
2677 if (!plane) {
2678 DRM_DEBUG_KMS("Unknown plane ID %d\n",
2679 plane_req->plane_id);
2680 return -ENOENT;
2681 }
2682
2683 if (plane_req->fb_id) {
2684 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
2685 if (!fb) {
2686 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
2687 plane_req->fb_id);
2688 return -ENOENT;
2689 }
2690
2691 crtc = drm_crtc_find(dev, plane_req->crtc_id);
2692 if (!crtc) {
2693 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
2694 plane_req->crtc_id);
2695 return -ENOENT;
2696 }
2697 }
2698
2699 /*
2700 * setplane_internal will take care of deref'ing either the old or new
2701 * framebuffer depending on success.
2702 */
2703 return setplane_internal(plane, crtc, fb,
2704 plane_req->crtc_x, plane_req->crtc_y,
2705 plane_req->crtc_w, plane_req->crtc_h,
2706 plane_req->src_x, plane_req->src_y,
2707 plane_req->src_w, plane_req->src_h);
2708 }
2709
2710 /**
2711 * drm_mode_set_config_internal - helper to call ->set_config
2712 * @set: modeset config to set
2713 *
2714 * This is a little helper to wrap internal calls to the ->set_config driver
2715 * interface. The only thing it adds is correct refcounting dance.
2716 *
2717 * Returns:
2718 * Zero on success, negative errno on failure.
2719 */
2720 int drm_mode_set_config_internal(struct drm_mode_set *set)
2721 {
2722 struct drm_crtc *crtc = set->crtc;
2723 struct drm_framebuffer *fb;
2724 struct drm_crtc *tmp;
2725 int ret;
2726
2727 /*
2728 * NOTE: ->set_config can also disable other crtcs (if we steal all
2729 * connectors from it), hence we need to refcount the fbs across all
2730 * crtcs. Atomic modeset will have saner semantics ...
2731 */
2732 drm_for_each_crtc(tmp, crtc->dev)
2733 tmp->primary->old_fb = tmp->primary->fb;
2734
2735 fb = set->fb;
2736
2737 ret = crtc->funcs->set_config(set);
2738 if (ret == 0) {
2739 crtc->primary->crtc = crtc;
2740 crtc->primary->fb = fb;
2741 }
2742
2743 drm_for_each_crtc(tmp, crtc->dev) {
2744 if (tmp->primary->fb)
2745 drm_framebuffer_reference(tmp->primary->fb);
2746 if (tmp->primary->old_fb)
2747 drm_framebuffer_unreference(tmp->primary->old_fb);
2748 tmp->primary->old_fb = NULL;
2749 }
2750
2751 return ret;
2752 }
2753 EXPORT_SYMBOL(drm_mode_set_config_internal);
2754
2755 /**
2756 * drm_crtc_get_hv_timing - Fetches hdisplay/vdisplay for given mode
2757 * @mode: mode to query
2758 * @hdisplay: hdisplay value to fill in
2759 * @vdisplay: vdisplay value to fill in
2760 *
2761 * The vdisplay value will be doubled if the specified mode is a stereo mode of
2762 * the appropriate layout.
2763 */
2764 void drm_crtc_get_hv_timing(const struct drm_display_mode *mode,
2765 int *hdisplay, int *vdisplay)
2766 {
2767 struct drm_display_mode adjusted;
2768
2769 drm_mode_copy(&adjusted, mode);
2770 drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE_ONLY);
2771 *hdisplay = adjusted.crtc_hdisplay;
2772 *vdisplay = adjusted.crtc_vdisplay;
2773 }
2774 EXPORT_SYMBOL(drm_crtc_get_hv_timing);
2775
2776 /**
2777 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
2778 * CRTC viewport
2779 * @crtc: CRTC that framebuffer will be displayed on
2780 * @x: x panning
2781 * @y: y panning
2782 * @mode: mode that framebuffer will be displayed under
2783 * @fb: framebuffer to check size of
2784 */
2785 int drm_crtc_check_viewport(const struct drm_crtc *crtc,
2786 int x, int y,
2787 const struct drm_display_mode *mode,
2788 const struct drm_framebuffer *fb)
2789
2790 {
2791 int hdisplay, vdisplay;
2792
2793 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
2794
2795 if (crtc->state &&
2796 crtc->primary->state->rotation & (BIT(DRM_ROTATE_90) |
2797 BIT(DRM_ROTATE_270)))
2798 swap(hdisplay, vdisplay);
2799
2800 return check_src_coords(x << 16, y << 16,
2801 hdisplay << 16, vdisplay << 16, fb);
2802 }
2803 EXPORT_SYMBOL(drm_crtc_check_viewport);
2804
2805 /**
2806 * drm_mode_setcrtc - set CRTC configuration
2807 * @dev: drm device for the ioctl
2808 * @data: data pointer for the ioctl
2809 * @file_priv: drm file for the ioctl call
2810 *
2811 * Build a new CRTC configuration based on user request.
2812 *
2813 * Called by the user via ioctl.
2814 *
2815 * Returns:
2816 * Zero on success, negative errno on failure.
2817 */
2818 int drm_mode_setcrtc(struct drm_device *dev, void *data,
2819 struct drm_file *file_priv)
2820 {
2821 struct drm_mode_config *config = &dev->mode_config;
2822 struct drm_mode_crtc *crtc_req = data;
2823 struct drm_crtc *crtc;
2824 struct drm_connector **connector_set = NULL, *connector;
2825 struct drm_framebuffer *fb = NULL;
2826 struct drm_display_mode *mode = NULL;
2827 struct drm_mode_set set;
2828 uint32_t __user *set_connectors_ptr;
2829 int ret;
2830 int i;
2831
2832 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2833 return -EINVAL;
2834
2835 /*
2836 * Universal plane src offsets are only 16.16, prevent havoc for
2837 * drivers using universal plane code internally.
2838 */
2839 if (crtc_req->x & 0xffff0000 || crtc_req->y & 0xffff0000)
2840 return -ERANGE;
2841
2842 drm_modeset_lock_all(dev);
2843 crtc = drm_crtc_find(dev, crtc_req->crtc_id);
2844 if (!crtc) {
2845 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
2846 ret = -ENOENT;
2847 goto out;
2848 }
2849 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
2850
2851 if (crtc_req->mode_valid) {
2852 /* If we have a mode we need a framebuffer. */
2853 /* If we pass -1, set the mode with the currently bound fb */
2854 if (crtc_req->fb_id == -1) {
2855 if (!crtc->primary->fb) {
2856 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2857 ret = -EINVAL;
2858 goto out;
2859 }
2860 fb = crtc->primary->fb;
2861 /* Make refcounting symmetric with the lookup path. */
2862 drm_framebuffer_reference(fb);
2863 } else {
2864 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2865 if (!fb) {
2866 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2867 crtc_req->fb_id);
2868 ret = -ENOENT;
2869 goto out;
2870 }
2871 }
2872
2873 mode = drm_mode_create(dev);
2874 if (!mode) {
2875 ret = -ENOMEM;
2876 goto out;
2877 }
2878
2879 ret = drm_mode_convert_umode(mode, &crtc_req->mode);
2880 if (ret) {
2881 DRM_DEBUG_KMS("Invalid mode\n");
2882 goto out;
2883 }
2884
2885 /*
2886 * Check whether the primary plane supports the fb pixel format.
2887 * Drivers not implementing the universal planes API use a
2888 * default formats list provided by the DRM core which doesn't
2889 * match real hardware capabilities. Skip the check in that
2890 * case.
2891 */
2892 if (!crtc->primary->format_default) {
2893 ret = drm_plane_check_pixel_format(crtc->primary,
2894 fb->pixel_format);
2895 if (ret) {
2896 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2897 drm_get_format_name(fb->pixel_format));
2898 goto out;
2899 }
2900 }
2901
2902 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
2903 mode, fb);
2904 if (ret)
2905 goto out;
2906
2907 }
2908
2909 if (crtc_req->count_connectors == 0 && mode) {
2910 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
2911 ret = -EINVAL;
2912 goto out;
2913 }
2914
2915 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
2916 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
2917 crtc_req->count_connectors);
2918 ret = -EINVAL;
2919 goto out;
2920 }
2921
2922 if (crtc_req->count_connectors > 0) {
2923 u32 out_id;
2924
2925 /* Avoid unbounded kernel memory allocation */
2926 if (crtc_req->count_connectors > config->num_connector) {
2927 ret = -EINVAL;
2928 goto out;
2929 }
2930
2931 connector_set = kmalloc_array(crtc_req->count_connectors,
2932 sizeof(struct drm_connector *),
2933 GFP_KERNEL);
2934 if (!connector_set) {
2935 ret = -ENOMEM;
2936 goto out;
2937 }
2938
2939 for (i = 0; i < crtc_req->count_connectors; i++) {
2940 connector_set[i] = NULL;
2941 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
2942 if (get_user(out_id, &set_connectors_ptr[i])) {
2943 ret = -EFAULT;
2944 goto out;
2945 }
2946
2947 connector = drm_connector_lookup(dev, out_id);
2948 if (!connector) {
2949 DRM_DEBUG_KMS("Connector id %d unknown\n",
2950 out_id);
2951 ret = -ENOENT;
2952 goto out;
2953 }
2954 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2955 connector->base.id,
2956 connector->name);
2957
2958 connector_set[i] = connector;
2959 }
2960 }
2961
2962 set.crtc = crtc;
2963 set.x = crtc_req->x;
2964 set.y = crtc_req->y;
2965 set.mode = mode;
2966 set.connectors = connector_set;
2967 set.num_connectors = crtc_req->count_connectors;
2968 set.fb = fb;
2969 ret = drm_mode_set_config_internal(&set);
2970
2971 out:
2972 if (fb)
2973 drm_framebuffer_unreference(fb);
2974
2975 if (connector_set) {
2976 for (i = 0; i < crtc_req->count_connectors; i++) {
2977 if (connector_set[i])
2978 drm_connector_unreference(connector_set[i]);
2979 }
2980 }
2981 kfree(connector_set);
2982 drm_mode_destroy(dev, mode);
2983 drm_modeset_unlock_all(dev);
2984 return ret;
2985 }
2986
2987 /**
2988 * drm_mode_cursor_universal - translate legacy cursor ioctl call into a
2989 * universal plane handler call
2990 * @crtc: crtc to update cursor for
2991 * @req: data pointer for the ioctl
2992 * @file_priv: drm file for the ioctl call
2993 *
2994 * Legacy cursor ioctl's work directly with driver buffer handles. To
2995 * translate legacy ioctl calls into universal plane handler calls, we need to
2996 * wrap the native buffer handle in a drm_framebuffer.
2997 *
2998 * Note that we assume any handle passed to the legacy ioctls was a 32-bit ARGB
2999 * buffer with a pitch of 4*width; the universal plane interface should be used
3000 * directly in cases where the hardware can support other buffer settings and
3001 * userspace wants to make use of these capabilities.
3002 *
3003 * Returns:
3004 * Zero on success, negative errno on failure.
3005 */
3006 static int drm_mode_cursor_universal(struct drm_crtc *crtc,
3007 struct drm_mode_cursor2 *req,
3008 struct drm_file *file_priv)
3009 {
3010 struct drm_device *dev = crtc->dev;
3011 struct drm_framebuffer *fb = NULL;
3012 struct drm_mode_fb_cmd2 fbreq = {
3013 .width = req->width,
3014 .height = req->height,
3015 .pixel_format = DRM_FORMAT_ARGB8888,
3016 .pitches = { req->width * 4 },
3017 .handles = { req->handle },
3018 };
3019 int32_t crtc_x, crtc_y;
3020 uint32_t crtc_w = 0, crtc_h = 0;
3021 uint32_t src_w = 0, src_h = 0;
3022 int ret = 0;
3023
3024 BUG_ON(!crtc->cursor);
3025 WARN_ON(crtc->cursor->crtc != crtc && crtc->cursor->crtc != NULL);
3026
3027 /*
3028 * Obtain fb we'll be using (either new or existing) and take an extra
3029 * reference to it if fb != null. setplane will take care of dropping
3030 * the reference if the plane update fails.
3031 */
3032 if (req->flags & DRM_MODE_CURSOR_BO) {
3033 if (req->handle) {
3034 fb = internal_framebuffer_create(dev, &fbreq, file_priv);
3035 if (IS_ERR(fb)) {
3036 DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
3037 return PTR_ERR(fb);
3038 }
3039 fb->hot_x = req->hot_x;
3040 fb->hot_y = req->hot_y;
3041 } else {
3042 fb = NULL;
3043 }
3044 } else {
3045 fb = crtc->cursor->fb;
3046 if (fb)
3047 drm_framebuffer_reference(fb);
3048 }
3049
3050 if (req->flags & DRM_MODE_CURSOR_MOVE) {
3051 crtc_x = req->x;
3052 crtc_y = req->y;
3053 } else {
3054 crtc_x = crtc->cursor_x;
3055 crtc_y = crtc->cursor_y;
3056 }
3057
3058 if (fb) {
3059 crtc_w = fb->width;
3060 crtc_h = fb->height;
3061 src_w = fb->width << 16;
3062 src_h = fb->height << 16;
3063 }
3064
3065 /*
3066 * setplane_internal will take care of deref'ing either the old or new
3067 * framebuffer depending on success.
3068 */
3069 ret = __setplane_internal(crtc->cursor, crtc, fb,
3070 crtc_x, crtc_y, crtc_w, crtc_h,
3071 0, 0, src_w, src_h);
3072
3073 /* Update successful; save new cursor position, if necessary */
3074 if (ret == 0 && req->flags & DRM_MODE_CURSOR_MOVE) {
3075 crtc->cursor_x = req->x;
3076 crtc->cursor_y = req->y;
3077 }
3078
3079 return ret;
3080 }
3081
3082 static int drm_mode_cursor_common(struct drm_device *dev,
3083 struct drm_mode_cursor2 *req,
3084 struct drm_file *file_priv)
3085 {
3086 struct drm_crtc *crtc;
3087 int ret = 0;
3088
3089 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3090 return -EINVAL;
3091
3092 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
3093 return -EINVAL;
3094
3095 crtc = drm_crtc_find(dev, req->crtc_id);
3096 if (!crtc) {
3097 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
3098 return -ENOENT;
3099 }
3100
3101 /*
3102 * If this crtc has a universal cursor plane, call that plane's update
3103 * handler rather than using legacy cursor handlers.
3104 */
3105 drm_modeset_lock_crtc(crtc, crtc->cursor);
3106 if (crtc->cursor) {
3107 ret = drm_mode_cursor_universal(crtc, req, file_priv);
3108 goto out;
3109 }
3110
3111 if (req->flags & DRM_MODE_CURSOR_BO) {
3112 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
3113 ret = -ENXIO;
3114 goto out;
3115 }
3116 /* Turns off the cursor if handle is 0 */
3117 if (crtc->funcs->cursor_set2)
3118 ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
3119 req->width, req->height, req->hot_x, req->hot_y);
3120 else
3121 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
3122 req->width, req->height);
3123 }
3124
3125 if (req->flags & DRM_MODE_CURSOR_MOVE) {
3126 if (crtc->funcs->cursor_move) {
3127 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
3128 } else {
3129 ret = -EFAULT;
3130 goto out;
3131 }
3132 }
3133 out:
3134 drm_modeset_unlock_crtc(crtc);
3135
3136 return ret;
3137
3138 }
3139
3140
3141 /**
3142 * drm_mode_cursor_ioctl - set CRTC's cursor configuration
3143 * @dev: drm device for the ioctl
3144 * @data: data pointer for the ioctl
3145 * @file_priv: drm file for the ioctl call
3146 *
3147 * Set the cursor configuration based on user request.
3148 *
3149 * Called by the user via ioctl.
3150 *
3151 * Returns:
3152 * Zero on success, negative errno on failure.
3153 */
3154 int drm_mode_cursor_ioctl(struct drm_device *dev,
3155 void *data, struct drm_file *file_priv)
3156 {
3157 struct drm_mode_cursor *req = data;
3158 struct drm_mode_cursor2 new_req;
3159
3160 memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
3161 new_req.hot_x = new_req.hot_y = 0;
3162
3163 return drm_mode_cursor_common(dev, &new_req, file_priv);
3164 }
3165
3166 /**
3167 * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
3168 * @dev: drm device for the ioctl
3169 * @data: data pointer for the ioctl
3170 * @file_priv: drm file for the ioctl call
3171 *
3172 * Set the cursor configuration based on user request. This implements the 2nd
3173 * version of the cursor ioctl, which allows userspace to additionally specify
3174 * the hotspot of the pointer.
3175 *
3176 * Called by the user via ioctl.
3177 *
3178 * Returns:
3179 * Zero on success, negative errno on failure.
3180 */
3181 int drm_mode_cursor2_ioctl(struct drm_device *dev,
3182 void *data, struct drm_file *file_priv)
3183 {
3184 struct drm_mode_cursor2 *req = data;
3185
3186 return drm_mode_cursor_common(dev, req, file_priv);
3187 }
3188
3189 /**
3190 * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
3191 * @bpp: bits per pixels
3192 * @depth: bit depth per pixel
3193 *
3194 * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
3195 * Useful in fbdev emulation code, since that deals in those values.
3196 */
3197 uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
3198 {
3199 uint32_t fmt;
3200
3201 switch (bpp) {
3202 case 8:
3203 fmt = DRM_FORMAT_C8;
3204 break;
3205 case 16:
3206 if (depth == 15)
3207 fmt = DRM_FORMAT_XRGB1555;
3208 else
3209 fmt = DRM_FORMAT_RGB565;
3210 break;
3211 case 24:
3212 fmt = DRM_FORMAT_RGB888;
3213 break;
3214 case 32:
3215 if (depth == 24)
3216 fmt = DRM_FORMAT_XRGB8888;
3217 else if (depth == 30)
3218 fmt = DRM_FORMAT_XRGB2101010;
3219 else
3220 fmt = DRM_FORMAT_ARGB8888;
3221 break;
3222 default:
3223 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
3224 fmt = DRM_FORMAT_XRGB8888;
3225 break;
3226 }
3227
3228 return fmt;
3229 }
3230 EXPORT_SYMBOL(drm_mode_legacy_fb_format);
3231
3232 /**
3233 * drm_mode_addfb - add an FB to the graphics configuration
3234 * @dev: drm device for the ioctl
3235 * @data: data pointer for the ioctl
3236 * @file_priv: drm file for the ioctl call
3237 *
3238 * Add a new FB to the specified CRTC, given a user request. This is the
3239 * original addfb ioctl which only supported RGB formats.
3240 *
3241 * Called by the user via ioctl.
3242 *
3243 * Returns:
3244 * Zero on success, negative errno on failure.
3245 */
3246 int drm_mode_addfb(struct drm_device *dev,
3247 void *data, struct drm_file *file_priv)
3248 {
3249 struct drm_mode_fb_cmd *or = data;
3250 struct drm_mode_fb_cmd2 r = {};
3251 int ret;
3252
3253 /* convert to new format and call new ioctl */
3254 r.fb_id = or->fb_id;
3255 r.width = or->width;
3256 r.height = or->height;
3257 r.pitches[0] = or->pitch;
3258 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
3259 r.handles[0] = or->handle;
3260
3261 ret = drm_mode_addfb2(dev, &r, file_priv);
3262 if (ret)
3263 return ret;
3264
3265 or->fb_id = r.fb_id;
3266
3267 return 0;
3268 }
3269
3270 static int format_check(const struct drm_mode_fb_cmd2 *r)
3271 {
3272 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
3273
3274 switch (format) {
3275 case DRM_FORMAT_C8:
3276 case DRM_FORMAT_RGB332:
3277 case DRM_FORMAT_BGR233:
3278 case DRM_FORMAT_XRGB4444:
3279 case DRM_FORMAT_XBGR4444:
3280 case DRM_FORMAT_RGBX4444:
3281 case DRM_FORMAT_BGRX4444:
3282 case DRM_FORMAT_ARGB4444:
3283 case DRM_FORMAT_ABGR4444:
3284 case DRM_FORMAT_RGBA4444:
3285 case DRM_FORMAT_BGRA4444:
3286 case DRM_FORMAT_XRGB1555:
3287 case DRM_FORMAT_XBGR1555:
3288 case DRM_FORMAT_RGBX5551:
3289 case DRM_FORMAT_BGRX5551:
3290 case DRM_FORMAT_ARGB1555:
3291 case DRM_FORMAT_ABGR1555:
3292 case DRM_FORMAT_RGBA5551:
3293 case DRM_FORMAT_BGRA5551:
3294 case DRM_FORMAT_RGB565:
3295 case DRM_FORMAT_BGR565:
3296 case DRM_FORMAT_RGB888:
3297 case DRM_FORMAT_BGR888:
3298 case DRM_FORMAT_XRGB8888:
3299 case DRM_FORMAT_XBGR8888:
3300 case DRM_FORMAT_RGBX8888:
3301 case DRM_FORMAT_BGRX8888:
3302 case DRM_FORMAT_ARGB8888:
3303 case DRM_FORMAT_ABGR8888:
3304 case DRM_FORMAT_RGBA8888:
3305 case DRM_FORMAT_BGRA8888:
3306 case DRM_FORMAT_XRGB2101010:
3307 case DRM_FORMAT_XBGR2101010:
3308 case DRM_FORMAT_RGBX1010102:
3309 case DRM_FORMAT_BGRX1010102:
3310 case DRM_FORMAT_ARGB2101010:
3311 case DRM_FORMAT_ABGR2101010:
3312 case DRM_FORMAT_RGBA1010102:
3313 case DRM_FORMAT_BGRA1010102:
3314 case DRM_FORMAT_YUYV:
3315 case DRM_FORMAT_YVYU:
3316 case DRM_FORMAT_UYVY:
3317 case DRM_FORMAT_VYUY:
3318 case DRM_FORMAT_AYUV:
3319 case DRM_FORMAT_NV12:
3320 case DRM_FORMAT_NV21:
3321 case DRM_FORMAT_NV16:
3322 case DRM_FORMAT_NV61:
3323 case DRM_FORMAT_NV24:
3324 case DRM_FORMAT_NV42:
3325 case DRM_FORMAT_YUV410:
3326 case DRM_FORMAT_YVU410:
3327 case DRM_FORMAT_YUV411:
3328 case DRM_FORMAT_YVU411:
3329 case DRM_FORMAT_YUV420:
3330 case DRM_FORMAT_YVU420:
3331 case DRM_FORMAT_YUV422:
3332 case DRM_FORMAT_YVU422:
3333 case DRM_FORMAT_YUV444:
3334 case DRM_FORMAT_YVU444:
3335 return 0;
3336 default:
3337 DRM_DEBUG_KMS("invalid pixel format %s\n",
3338 drm_get_format_name(r->pixel_format));
3339 return -EINVAL;
3340 }
3341 }
3342
3343 static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
3344 {
3345 int ret, hsub, vsub, num_planes, i;
3346
3347 ret = format_check(r);
3348 if (ret) {
3349 DRM_DEBUG_KMS("bad framebuffer format %s\n",
3350 drm_get_format_name(r->pixel_format));
3351 return ret;
3352 }
3353
3354 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
3355 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
3356 num_planes = drm_format_num_planes(r->pixel_format);
3357
3358 if (r->width == 0 || r->width % hsub) {
3359 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->width);
3360 return -EINVAL;
3361 }
3362
3363 if (r->height == 0 || r->height % vsub) {
3364 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
3365 return -EINVAL;
3366 }
3367
3368 for (i = 0; i < num_planes; i++) {
3369 unsigned int width = r->width / (i != 0 ? hsub : 1);
3370 unsigned int height = r->height / (i != 0 ? vsub : 1);
3371 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
3372
3373 if (!r->handles[i]) {
3374 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
3375 return -EINVAL;
3376 }
3377
3378 if ((uint64_t) width * cpp > UINT_MAX)
3379 return -ERANGE;
3380
3381 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
3382 return -ERANGE;
3383
3384 if (r->pitches[i] < width * cpp) {
3385 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
3386 return -EINVAL;
3387 }
3388
3389 if (r->modifier[i] && !(r->flags & DRM_MODE_FB_MODIFIERS)) {
3390 DRM_DEBUG_KMS("bad fb modifier %llu for plane %d\n",
3391 r->modifier[i], i);
3392 return -EINVAL;
3393 }
3394
3395 /* modifier specific checks: */
3396 switch (r->modifier[i]) {
3397 case DRM_FORMAT_MOD_SAMSUNG_64_32_TILE:
3398 /* NOTE: the pitch restriction may be lifted later if it turns
3399 * out that no hw has this restriction:
3400 */
3401 if (r->pixel_format != DRM_FORMAT_NV12 ||
3402 width % 128 || height % 32 ||
3403 r->pitches[i] % 128) {
3404 DRM_DEBUG_KMS("bad modifier data for plane %d\n", i);
3405 return -EINVAL;
3406 }
3407 break;
3408
3409 default:
3410 break;
3411 }
3412 }
3413
3414 for (i = num_planes; i < 4; i++) {
3415 if (r->modifier[i]) {
3416 DRM_DEBUG_KMS("non-zero modifier for unused plane %d\n", i);
3417 return -EINVAL;
3418 }
3419
3420 /* Pre-FB_MODIFIERS userspace didn't clear the structs properly. */
3421 if (!(r->flags & DRM_MODE_FB_MODIFIERS))
3422 continue;
3423
3424 if (r->handles[i]) {
3425 DRM_DEBUG_KMS("buffer object handle for unused plane %d\n", i);
3426 return -EINVAL;
3427 }
3428
3429 if (r->pitches[i]) {
3430 DRM_DEBUG_KMS("non-zero pitch for unused plane %d\n", i);
3431 return -EINVAL;
3432 }
3433
3434 if (r->offsets[i]) {
3435 DRM_DEBUG_KMS("non-zero offset for unused plane %d\n", i);
3436 return -EINVAL;
3437 }
3438 }
3439
3440 return 0;
3441 }
3442
3443 static struct drm_framebuffer *
3444 internal_framebuffer_create(struct drm_device *dev,
3445 const struct drm_mode_fb_cmd2 *r,
3446 struct drm_file *file_priv)
3447 {
3448 struct drm_mode_config *config = &dev->mode_config;
3449 struct drm_framebuffer *fb;
3450 int ret;
3451
3452 if (r->flags & ~(DRM_MODE_FB_INTERLACED | DRM_MODE_FB_MODIFIERS)) {
3453 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
3454 return ERR_PTR(-EINVAL);
3455 }
3456
3457 if ((config->min_width > r->width) || (r->width > config->max_width)) {
3458 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
3459 r->width, config->min_width, config->max_width);
3460 return ERR_PTR(-EINVAL);
3461 }
3462 if ((config->min_height > r->height) || (r->height > config->max_height)) {
3463 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
3464 r->height, config->min_height, config->max_height);
3465 return ERR_PTR(-EINVAL);
3466 }
3467
3468 if (r->flags & DRM_MODE_FB_MODIFIERS &&
3469 !dev->mode_config.allow_fb_modifiers) {
3470 DRM_DEBUG_KMS("driver does not support fb modifiers\n");
3471 return ERR_PTR(-EINVAL);
3472 }
3473
3474 ret = framebuffer_check(r);
3475 if (ret)
3476 return ERR_PTR(ret);
3477
3478 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
3479 if (IS_ERR(fb)) {
3480 DRM_DEBUG_KMS("could not create framebuffer\n");
3481 return fb;
3482 }
3483
3484 return fb;
3485 }
3486
3487 /**
3488 * drm_mode_addfb2 - add an FB to the graphics configuration
3489 * @dev: drm device for the ioctl
3490 * @data: data pointer for the ioctl
3491 * @file_priv: drm file for the ioctl call
3492 *
3493 * Add a new FB to the specified CRTC, given a user request with format. This is
3494 * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
3495 * and uses fourcc codes as pixel format specifiers.
3496 *
3497 * Called by the user via ioctl.
3498 *
3499 * Returns:
3500 * Zero on success, negative errno on failure.
3501 */
3502 int drm_mode_addfb2(struct drm_device *dev,
3503 void *data, struct drm_file *file_priv)
3504 {
3505 struct drm_mode_fb_cmd2 *r = data;
3506 struct drm_framebuffer *fb;
3507
3508 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3509 return -EINVAL;
3510
3511 fb = internal_framebuffer_create(dev, r, file_priv);
3512 if (IS_ERR(fb))
3513 return PTR_ERR(fb);
3514
3515 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
3516 r->fb_id = fb->base.id;
3517
3518 /* Transfer ownership to the filp for reaping on close */
3519 mutex_lock(&file_priv->fbs_lock);
3520 list_add(&fb->filp_head, &file_priv->fbs);
3521 mutex_unlock(&file_priv->fbs_lock);
3522
3523 return 0;
3524 }
3525
3526 struct drm_mode_rmfb_work {
3527 struct work_struct work;
3528 struct list_head fbs;
3529 };
3530
3531 static void drm_mode_rmfb_work_fn(struct work_struct *w)
3532 {
3533 struct drm_mode_rmfb_work *arg = container_of(w, typeof(*arg), work);
3534
3535 while (!list_empty(&arg->fbs)) {
3536 struct drm_framebuffer *fb =
3537 list_first_entry(&arg->fbs, typeof(*fb), filp_head);
3538
3539 list_del_init(&fb->filp_head);
3540 drm_framebuffer_remove(fb);
3541 }
3542 }
3543
3544 /**
3545 * drm_mode_rmfb - remove an FB from the configuration
3546 * @dev: drm device for the ioctl
3547 * @data: data pointer for the ioctl
3548 * @file_priv: drm file for the ioctl call
3549 *
3550 * Remove the FB specified by the user.
3551 *
3552 * Called by the user via ioctl.
3553 *
3554 * Returns:
3555 * Zero on success, negative errno on failure.
3556 */
3557 int drm_mode_rmfb(struct drm_device *dev,
3558 void *data, struct drm_file *file_priv)
3559 {
3560 struct drm_framebuffer *fb = NULL;
3561 struct drm_framebuffer *fbl = NULL;
3562 uint32_t *id = data;
3563 int found = 0;
3564
3565 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3566 return -EINVAL;
3567
3568 fb = drm_framebuffer_lookup(dev, *id);
3569 if (!fb)
3570 return -ENOENT;
3571
3572 mutex_lock(&file_priv->fbs_lock);
3573 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
3574 if (fb == fbl)
3575 found = 1;
3576 if (!found) {
3577 mutex_unlock(&file_priv->fbs_lock);
3578 goto fail_unref;
3579 }
3580
3581 list_del_init(&fb->filp_head);
3582 mutex_unlock(&file_priv->fbs_lock);
3583
3584 /* drop the reference we picked up in framebuffer lookup */
3585 drm_framebuffer_unreference(fb);
3586
3587 /*
3588 * we now own the reference that was stored in the fbs list
3589 *
3590 * drm_framebuffer_remove may fail with -EINTR on pending signals,
3591 * so run this in a separate stack as there's no way to correctly
3592 * handle this after the fb is already removed from the lookup table.
3593 */
3594 if (drm_framebuffer_read_refcount(fb) > 1) {
3595 struct drm_mode_rmfb_work arg;
3596
3597 INIT_WORK_ONSTACK(&arg.work, drm_mode_rmfb_work_fn);
3598 INIT_LIST_HEAD(&arg.fbs);
3599 list_add_tail(&fb->filp_head, &arg.fbs);
3600
3601 schedule_work(&arg.work);
3602 flush_work(&arg.work);
3603 destroy_work_on_stack(&arg.work);
3604 } else
3605 drm_framebuffer_unreference(fb);
3606
3607 return 0;
3608
3609 fail_unref:
3610 drm_framebuffer_unreference(fb);
3611 return -ENOENT;
3612 }
3613
3614 /**
3615 * drm_mode_getfb - get FB info
3616 * @dev: drm device for the ioctl
3617 * @data: data pointer for the ioctl
3618 * @file_priv: drm file for the ioctl call
3619 *
3620 * Lookup the FB given its ID and return info about it.
3621 *
3622 * Called by the user via ioctl.
3623 *
3624 * Returns:
3625 * Zero on success, negative errno on failure.
3626 */
3627 int drm_mode_getfb(struct drm_device *dev,
3628 void *data, struct drm_file *file_priv)
3629 {
3630 struct drm_mode_fb_cmd *r = data;
3631 struct drm_framebuffer *fb;
3632 int ret;
3633
3634 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3635 return -EINVAL;
3636
3637 fb = drm_framebuffer_lookup(dev, r->fb_id);
3638 if (!fb)
3639 return -ENOENT;
3640
3641 r->height = fb->height;
3642 r->width = fb->width;
3643 r->depth = fb->depth;
3644 r->bpp = fb->bits_per_pixel;
3645 r->pitch = fb->pitches[0];
3646 if (fb->funcs->create_handle) {
3647 if (drm_is_current_master(file_priv) || capable(CAP_SYS_ADMIN) ||
3648 drm_is_control_client(file_priv)) {
3649 ret = fb->funcs->create_handle(fb, file_priv,
3650 &r->handle);
3651 } else {
3652 /* GET_FB() is an unprivileged ioctl so we must not
3653 * return a buffer-handle to non-master processes! For
3654 * backwards-compatibility reasons, we cannot make
3655 * GET_FB() privileged, so just return an invalid handle
3656 * for non-masters. */
3657 r->handle = 0;
3658 ret = 0;
3659 }
3660 } else {
3661 ret = -ENODEV;
3662 }
3663
3664 drm_framebuffer_unreference(fb);
3665
3666 return ret;
3667 }
3668
3669 /**
3670 * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
3671 * @dev: drm device for the ioctl
3672 * @data: data pointer for the ioctl
3673 * @file_priv: drm file for the ioctl call
3674 *
3675 * Lookup the FB and flush out the damaged area supplied by userspace as a clip
3676 * rectangle list. Generic userspace which does frontbuffer rendering must call
3677 * this ioctl to flush out the changes on manual-update display outputs, e.g.
3678 * usb display-link, mipi manual update panels or edp panel self refresh modes.
3679 *
3680 * Modesetting drivers which always update the frontbuffer do not need to
3681 * implement the corresponding ->dirty framebuffer callback.
3682 *
3683 * Called by the user via ioctl.
3684 *
3685 * Returns:
3686 * Zero on success, negative errno on failure.
3687 */
3688 int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
3689 void *data, struct drm_file *file_priv)
3690 {
3691 struct drm_clip_rect __user *clips_ptr;
3692 struct drm_clip_rect *clips = NULL;
3693 struct drm_mode_fb_dirty_cmd *r = data;
3694 struct drm_framebuffer *fb;
3695 unsigned flags;
3696 int num_clips;
3697 int ret;
3698
3699 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3700 return -EINVAL;
3701
3702 fb = drm_framebuffer_lookup(dev, r->fb_id);
3703 if (!fb)
3704 return -ENOENT;
3705
3706 num_clips = r->num_clips;
3707 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
3708
3709 if (!num_clips != !clips_ptr) {
3710 ret = -EINVAL;
3711 goto out_err1;
3712 }
3713
3714 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
3715
3716 /* If userspace annotates copy, clips must come in pairs */
3717 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
3718 ret = -EINVAL;
3719 goto out_err1;
3720 }
3721
3722 if (num_clips && clips_ptr) {
3723 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
3724 ret = -EINVAL;
3725 goto out_err1;
3726 }
3727 clips = kcalloc(num_clips, sizeof(*clips), GFP_KERNEL);
3728 if (!clips) {
3729 ret = -ENOMEM;
3730 goto out_err1;
3731 }
3732
3733 ret = copy_from_user(clips, clips_ptr,
3734 num_clips * sizeof(*clips));
3735 if (ret) {
3736 ret = -EFAULT;
3737 goto out_err2;
3738 }
3739 }
3740
3741 if (fb->funcs->dirty) {
3742 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
3743 clips, num_clips);
3744 } else {
3745 ret = -ENOSYS;
3746 }
3747
3748 out_err2:
3749 kfree(clips);
3750 out_err1:
3751 drm_framebuffer_unreference(fb);
3752
3753 return ret;
3754 }
3755
3756 /**
3757 * drm_fb_release - remove and free the FBs on this file
3758 * @priv: drm file for the ioctl
3759 *
3760 * Destroy all the FBs associated with @filp.
3761 *
3762 * Called by the user via ioctl.
3763 *
3764 * Returns:
3765 * Zero on success, negative errno on failure.
3766 */
3767 void drm_fb_release(struct drm_file *priv)
3768 {
3769 struct drm_framebuffer *fb, *tfb;
3770 struct drm_mode_rmfb_work arg;
3771
3772 INIT_LIST_HEAD(&arg.fbs);
3773
3774 /*
3775 * When the file gets released that means no one else can access the fb
3776 * list any more, so no need to grab fpriv->fbs_lock. And we need to
3777 * avoid upsetting lockdep since the universal cursor code adds a
3778 * framebuffer while holding mutex locks.
3779 *
3780 * Note that a real deadlock between fpriv->fbs_lock and the modeset
3781 * locks is impossible here since no one else but this function can get
3782 * at it any more.
3783 */
3784 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
3785 if (drm_framebuffer_read_refcount(fb) > 1) {
3786 list_move_tail(&fb->filp_head, &arg.fbs);
3787 } else {
3788 list_del_init(&fb->filp_head);
3789
3790 /* This drops the fpriv->fbs reference. */
3791 drm_framebuffer_unreference(fb);
3792 }
3793 }
3794
3795 if (!list_empty(&arg.fbs)) {
3796 INIT_WORK_ONSTACK(&arg.work, drm_mode_rmfb_work_fn);
3797
3798 schedule_work(&arg.work);
3799 flush_work(&arg.work);
3800 destroy_work_on_stack(&arg.work);
3801 }
3802 }
3803
3804 static bool drm_property_type_valid(struct drm_property *property)
3805 {
3806 if (property->flags & DRM_MODE_PROP_EXTENDED_TYPE)
3807 return !(property->flags & DRM_MODE_PROP_LEGACY_TYPE);
3808 return !!(property->flags & DRM_MODE_PROP_LEGACY_TYPE);
3809 }
3810
3811 /**
3812 * drm_property_create - create a new property type
3813 * @dev: drm device
3814 * @flags: flags specifying the property type
3815 * @name: name of the property
3816 * @num_values: number of pre-defined values
3817 *
3818 * This creates a new generic drm property which can then be attached to a drm
3819 * object with drm_object_attach_property. The returned property object must be
3820 * freed with drm_property_destroy.
3821 *
3822 * Note that the DRM core keeps a per-device list of properties and that, if
3823 * drm_mode_config_cleanup() is called, it will destroy all properties created
3824 * by the driver.
3825 *
3826 * Returns:
3827 * A pointer to the newly created property on success, NULL on failure.
3828 */
3829 struct drm_property *drm_property_create(struct drm_device *dev, int flags,
3830 const char *name, int num_values)
3831 {
3832 struct drm_property *property = NULL;
3833 int ret;
3834
3835 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
3836 if (!property)
3837 return NULL;
3838
3839 property->dev = dev;
3840
3841 if (num_values) {
3842 property->values = kcalloc(num_values, sizeof(uint64_t),
3843 GFP_KERNEL);
3844 if (!property->values)
3845 goto fail;
3846 }
3847
3848 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
3849 if (ret)
3850 goto fail;
3851
3852 property->flags = flags;
3853 property->num_values = num_values;
3854 INIT_LIST_HEAD(&property->enum_list);
3855
3856 if (name) {
3857 strncpy(property->name, name, DRM_PROP_NAME_LEN);
3858 property->name[DRM_PROP_NAME_LEN-1] = '\0';
3859 }
3860
3861 list_add_tail(&property->head, &dev->mode_config.property_list);
3862
3863 WARN_ON(!drm_property_type_valid(property));
3864
3865 return property;
3866 fail:
3867 kfree(property->values);
3868 kfree(property);
3869 return NULL;
3870 }
3871 EXPORT_SYMBOL(drm_property_create);
3872
3873 /**
3874 * drm_property_create_enum - create a new enumeration property type
3875 * @dev: drm device
3876 * @flags: flags specifying the property type
3877 * @name: name of the property
3878 * @props: enumeration lists with property values
3879 * @num_values: number of pre-defined values
3880 *
3881 * This creates a new generic drm property which can then be attached to a drm
3882 * object with drm_object_attach_property. The returned property object must be
3883 * freed with drm_property_destroy.
3884 *
3885 * Userspace is only allowed to set one of the predefined values for enumeration
3886 * properties.
3887 *
3888 * Returns:
3889 * A pointer to the newly created property on success, NULL on failure.
3890 */
3891 struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
3892 const char *name,
3893 const struct drm_prop_enum_list *props,
3894 int num_values)
3895 {
3896 struct drm_property *property;
3897 int i, ret;
3898
3899 flags |= DRM_MODE_PROP_ENUM;
3900
3901 property = drm_property_create(dev, flags, name, num_values);
3902 if (!property)
3903 return NULL;
3904
3905 for (i = 0; i < num_values; i++) {
3906 ret = drm_property_add_enum(property, i,
3907 props[i].type,
3908 props[i].name);
3909 if (ret) {
3910 drm_property_destroy(dev, property);
3911 return NULL;
3912 }
3913 }
3914
3915 return property;
3916 }
3917 EXPORT_SYMBOL(drm_property_create_enum);
3918
3919 /**
3920 * drm_property_create_bitmask - create a new bitmask property type
3921 * @dev: drm device
3922 * @flags: flags specifying the property type
3923 * @name: name of the property
3924 * @props: enumeration lists with property bitflags
3925 * @num_props: size of the @props array
3926 * @supported_bits: bitmask of all supported enumeration values
3927 *
3928 * This creates a new bitmask drm property which can then be attached to a drm
3929 * object with drm_object_attach_property. The returned property object must be
3930 * freed with drm_property_destroy.
3931 *
3932 * Compared to plain enumeration properties userspace is allowed to set any
3933 * or'ed together combination of the predefined property bitflag values
3934 *
3935 * Returns:
3936 * A pointer to the newly created property on success, NULL on failure.
3937 */
3938 struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
3939 int flags, const char *name,
3940 const struct drm_prop_enum_list *props,
3941 int num_props,
3942 uint64_t supported_bits)
3943 {
3944 struct drm_property *property;
3945 int i, ret, index = 0;
3946 int num_values = hweight64(supported_bits);
3947
3948 flags |= DRM_MODE_PROP_BITMASK;
3949
3950 property = drm_property_create(dev, flags, name, num_values);
3951 if (!property)
3952 return NULL;
3953 for (i = 0; i < num_props; i++) {
3954 if (!(supported_bits & (1ULL << props[i].type)))
3955 continue;
3956
3957 if (WARN_ON(index >= num_values)) {
3958 drm_property_destroy(dev, property);
3959 return NULL;
3960 }
3961
3962 ret = drm_property_add_enum(property, index++,
3963 props[i].type,
3964 props[i].name);
3965 if (ret) {
3966 drm_property_destroy(dev, property);
3967 return NULL;
3968 }
3969 }
3970
3971 return property;
3972 }
3973 EXPORT_SYMBOL(drm_property_create_bitmask);
3974
3975 static struct drm_property *property_create_range(struct drm_device *dev,
3976 int flags, const char *name,
3977 uint64_t min, uint64_t max)
3978 {
3979 struct drm_property *property;
3980
3981 property = drm_property_create(dev, flags, name, 2);
3982 if (!property)
3983 return NULL;
3984
3985 property->values[0] = min;
3986 property->values[1] = max;
3987
3988 return property;
3989 }
3990
3991 /**
3992 * drm_property_create_range - create a new unsigned ranged property type
3993 * @dev: drm device
3994 * @flags: flags specifying the property type
3995 * @name: name of the property
3996 * @min: minimum value of the property
3997 * @max: maximum value of the property
3998 *
3999 * This creates a new generic drm property which can then be attached to a drm
4000 * object with drm_object_attach_property. The returned property object must be
4001 * freed with drm_property_destroy.
4002 *
4003 * Userspace is allowed to set any unsigned integer value in the (min, max)
4004 * range inclusive.
4005 *
4006 * Returns:
4007 * A pointer to the newly created property on success, NULL on failure.
4008 */
4009 struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
4010 const char *name,
4011 uint64_t min, uint64_t max)
4012 {
4013 return property_create_range(dev, DRM_MODE_PROP_RANGE | flags,
4014 name, min, max);
4015 }
4016 EXPORT_SYMBOL(drm_property_create_range);
4017
4018 /**
4019 * drm_property_create_signed_range - create a new signed ranged property type
4020 * @dev: drm device
4021 * @flags: flags specifying the property type
4022 * @name: name of the property
4023 * @min: minimum value of the property
4024 * @max: maximum value of the property
4025 *
4026 * This creates a new generic drm property which can then be attached to a drm
4027 * object with drm_object_attach_property. The returned property object must be
4028 * freed with drm_property_destroy.
4029 *
4030 * Userspace is allowed to set any signed integer value in the (min, max)
4031 * range inclusive.
4032 *
4033 * Returns:
4034 * A pointer to the newly created property on success, NULL on failure.
4035 */
4036 struct drm_property *drm_property_create_signed_range(struct drm_device *dev,
4037 int flags, const char *name,
4038 int64_t min, int64_t max)
4039 {
4040 return property_create_range(dev, DRM_MODE_PROP_SIGNED_RANGE | flags,
4041 name, I642U64(min), I642U64(max));
4042 }
4043 EXPORT_SYMBOL(drm_property_create_signed_range);
4044
4045 /**
4046 * drm_property_create_object - create a new object property type
4047 * @dev: drm device
4048 * @flags: flags specifying the property type
4049 * @name: name of the property
4050 * @type: object type from DRM_MODE_OBJECT_* defines
4051 *
4052 * This creates a new generic drm property which can then be attached to a drm
4053 * object with drm_object_attach_property. The returned property object must be
4054 * freed with drm_property_destroy.
4055 *
4056 * Userspace is only allowed to set this to any property value of the given
4057 * @type. Only useful for atomic properties, which is enforced.
4058 *
4059 * Returns:
4060 * A pointer to the newly created property on success, NULL on failure.
4061 */
4062 struct drm_property *drm_property_create_object(struct drm_device *dev,
4063 int flags, const char *name, uint32_t type)
4064 {
4065 struct drm_property *property;
4066
4067 flags |= DRM_MODE_PROP_OBJECT;
4068
4069 if (WARN_ON(!(flags & DRM_MODE_PROP_ATOMIC)))
4070 return NULL;
4071
4072 property = drm_property_create(dev, flags, name, 1);
4073 if (!property)
4074 return NULL;
4075
4076 property->values[0] = type;
4077
4078 return property;
4079 }
4080 EXPORT_SYMBOL(drm_property_create_object);
4081
4082 /**
4083 * drm_property_create_bool - create a new boolean property type
4084 * @dev: drm device
4085 * @flags: flags specifying the property type
4086 * @name: name of the property
4087 *
4088 * This creates a new generic drm property which can then be attached to a drm
4089 * object with drm_object_attach_property. The returned property object must be
4090 * freed with drm_property_destroy.
4091 *
4092 * This is implemented as a ranged property with only {0, 1} as valid values.
4093 *
4094 * Returns:
4095 * A pointer to the newly created property on success, NULL on failure.
4096 */
4097 struct drm_property *drm_property_create_bool(struct drm_device *dev, int flags,
4098 const char *name)
4099 {
4100 return drm_property_create_range(dev, flags, name, 0, 1);
4101 }
4102 EXPORT_SYMBOL(drm_property_create_bool);
4103
4104 /**
4105 * drm_property_add_enum - add a possible value to an enumeration property
4106 * @property: enumeration property to change
4107 * @index: index of the new enumeration
4108 * @value: value of the new enumeration
4109 * @name: symbolic name of the new enumeration
4110 *
4111 * This functions adds enumerations to a property.
4112 *
4113 * It's use is deprecated, drivers should use one of the more specific helpers
4114 * to directly create the property with all enumerations already attached.
4115 *
4116 * Returns:
4117 * Zero on success, error code on failure.
4118 */
4119 int drm_property_add_enum(struct drm_property *property, int index,
4120 uint64_t value, const char *name)
4121 {
4122 struct drm_property_enum *prop_enum;
4123
4124 if (!(drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
4125 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)))
4126 return -EINVAL;
4127
4128 /*
4129 * Bitmask enum properties have the additional constraint of values
4130 * from 0 to 63
4131 */
4132 if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK) &&
4133 (value > 63))
4134 return -EINVAL;
4135
4136 if (!list_empty(&property->enum_list)) {
4137 list_for_each_entry(prop_enum, &property->enum_list, head) {
4138 if (prop_enum->value == value) {
4139 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
4140 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
4141 return 0;
4142 }
4143 }
4144 }
4145
4146 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
4147 if (!prop_enum)
4148 return -ENOMEM;
4149
4150 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
4151 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
4152 prop_enum->value = value;
4153
4154 property->values[index] = value;
4155 list_add_tail(&prop_enum->head, &property->enum_list);
4156 return 0;
4157 }
4158 EXPORT_SYMBOL(drm_property_add_enum);
4159
4160 /**
4161 * drm_property_destroy - destroy a drm property
4162 * @dev: drm device
4163 * @property: property to destry
4164 *
4165 * This function frees a property including any attached resources like
4166 * enumeration values.
4167 */
4168 void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
4169 {
4170 struct drm_property_enum *prop_enum, *pt;
4171
4172 list_for_each_entry_safe(prop_enum, pt, &property->enum_list, head) {
4173 list_del(&prop_enum->head);
4174 kfree(prop_enum);
4175 }
4176
4177 if (property->num_values)
4178 kfree(property->values);
4179 drm_mode_object_unregister(dev, &property->base);
4180 list_del(&property->head);
4181 kfree(property);
4182 }
4183 EXPORT_SYMBOL(drm_property_destroy);
4184
4185 /**
4186 * drm_object_attach_property - attach a property to a modeset object
4187 * @obj: drm modeset object
4188 * @property: property to attach
4189 * @init_val: initial value of the property
4190 *
4191 * This attaches the given property to the modeset object with the given initial
4192 * value. Currently this function cannot fail since the properties are stored in
4193 * a statically sized array.
4194 */
4195 void drm_object_attach_property(struct drm_mode_object *obj,
4196 struct drm_property *property,
4197 uint64_t init_val)
4198 {
4199 int count = obj->properties->count;
4200
4201 if (count == DRM_OBJECT_MAX_PROPERTY) {
4202 WARN(1, "Failed to attach object property (type: 0x%x). Please "
4203 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
4204 "you see this message on the same object type.\n",
4205 obj->type);
4206 return;
4207 }
4208
4209 obj->properties->properties[count] = property;
4210 obj->properties->values[count] = init_val;
4211 obj->properties->count++;
4212 if (property->flags & DRM_MODE_PROP_ATOMIC)
4213 obj->properties->atomic_count++;
4214 }
4215 EXPORT_SYMBOL(drm_object_attach_property);
4216
4217 /**
4218 * drm_object_property_set_value - set the value of a property
4219 * @obj: drm mode object to set property value for
4220 * @property: property to set
4221 * @val: value the property should be set to
4222 *
4223 * This functions sets a given property on a given object. This function only
4224 * changes the software state of the property, it does not call into the
4225 * driver's ->set_property callback.
4226 *
4227 * Returns:
4228 * Zero on success, error code on failure.
4229 */
4230 int drm_object_property_set_value(struct drm_mode_object *obj,
4231 struct drm_property *property, uint64_t val)
4232 {
4233 int i;
4234
4235 for (i = 0; i < obj->properties->count; i++) {
4236 if (obj->properties->properties[i] == property) {
4237 obj->properties->values[i] = val;
4238 return 0;
4239 }
4240 }
4241
4242 return -EINVAL;
4243 }
4244 EXPORT_SYMBOL(drm_object_property_set_value);
4245
4246 /**
4247 * drm_object_property_get_value - retrieve the value of a property
4248 * @obj: drm mode object to get property value from
4249 * @property: property to retrieve
4250 * @val: storage for the property value
4251 *
4252 * This function retrieves the softare state of the given property for the given
4253 * property. Since there is no driver callback to retrieve the current property
4254 * value this might be out of sync with the hardware, depending upon the driver
4255 * and property.
4256 *
4257 * Returns:
4258 * Zero on success, error code on failure.
4259 */
4260 int drm_object_property_get_value(struct drm_mode_object *obj,
4261 struct drm_property *property, uint64_t *val)
4262 {
4263 int i;
4264
4265 /* read-only properties bypass atomic mechanism and still store
4266 * their value in obj->properties->values[].. mostly to avoid
4267 * having to deal w/ EDID and similar props in atomic paths:
4268 */
4269 if (drm_core_check_feature(property->dev, DRIVER_ATOMIC) &&
4270 !(property->flags & DRM_MODE_PROP_IMMUTABLE))
4271 return drm_atomic_get_property(obj, property, val);
4272
4273 for (i = 0; i < obj->properties->count; i++) {
4274 if (obj->properties->properties[i] == property) {
4275 *val = obj->properties->values[i];
4276 return 0;
4277 }
4278 }
4279
4280 return -EINVAL;
4281 }
4282 EXPORT_SYMBOL(drm_object_property_get_value);
4283
4284 /**
4285 * drm_mode_getproperty_ioctl - get the property metadata
4286 * @dev: DRM device
4287 * @data: ioctl data
4288 * @file_priv: DRM file info
4289 *
4290 * This function retrieves the metadata for a given property, like the different
4291 * possible values for an enum property or the limits for a range property.
4292 *
4293 * Blob properties are special
4294 *
4295 * Called by the user via ioctl.
4296 *
4297 * Returns:
4298 * Zero on success, negative errno on failure.
4299 */
4300 int drm_mode_getproperty_ioctl(struct drm_device *dev,
4301 void *data, struct drm_file *file_priv)
4302 {
4303 struct drm_mode_get_property *out_resp = data;
4304 struct drm_property *property;
4305 int enum_count = 0;
4306 int value_count = 0;
4307 int ret = 0, i;
4308 int copied;
4309 struct drm_property_enum *prop_enum;
4310 struct drm_mode_property_enum __user *enum_ptr;
4311 uint64_t __user *values_ptr;
4312
4313 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4314 return -EINVAL;
4315
4316 drm_modeset_lock_all(dev);
4317 property = drm_property_find(dev, out_resp->prop_id);
4318 if (!property) {
4319 ret = -ENOENT;
4320 goto done;
4321 }
4322
4323 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
4324 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
4325 list_for_each_entry(prop_enum, &property->enum_list, head)
4326 enum_count++;
4327 }
4328
4329 value_count = property->num_values;
4330
4331 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
4332 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
4333 out_resp->flags = property->flags;
4334
4335 if ((out_resp->count_values >= value_count) && value_count) {
4336 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
4337 for (i = 0; i < value_count; i++) {
4338 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
4339 ret = -EFAULT;
4340 goto done;
4341 }
4342 }
4343 }
4344 out_resp->count_values = value_count;
4345
4346 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
4347 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
4348 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
4349 copied = 0;
4350 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
4351 list_for_each_entry(prop_enum, &property->enum_list, head) {
4352
4353 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
4354 ret = -EFAULT;
4355 goto done;
4356 }
4357
4358 if (copy_to_user(&enum_ptr[copied].name,
4359 &prop_enum->name, DRM_PROP_NAME_LEN)) {
4360 ret = -EFAULT;
4361 goto done;
4362 }
4363 copied++;
4364 }
4365 }
4366 out_resp->count_enum_blobs = enum_count;
4367 }
4368
4369 /*
4370 * NOTE: The idea seems to have been to use this to read all the blob
4371 * property values. But nothing ever added them to the corresponding
4372 * list, userspace always used the special-purpose get_blob ioctl to
4373 * read the value for a blob property. It also doesn't make a lot of
4374 * sense to return values here when everything else is just metadata for
4375 * the property itself.
4376 */
4377 if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
4378 out_resp->count_enum_blobs = 0;
4379 done:
4380 drm_modeset_unlock_all(dev);
4381 return ret;
4382 }
4383
4384 static void drm_property_free_blob(struct kref *kref)
4385 {
4386 struct drm_property_blob *blob =
4387 container_of(kref, struct drm_property_blob, base.refcount);
4388
4389 mutex_lock(&blob->dev->mode_config.blob_lock);
4390 list_del(&blob->head_global);
4391 mutex_unlock(&blob->dev->mode_config.blob_lock);
4392
4393 drm_mode_object_unregister(blob->dev, &blob->base);
4394
4395 kfree(blob);
4396 }
4397
4398 /**
4399 * drm_property_create_blob - Create new blob property
4400 *
4401 * Creates a new blob property for a specified DRM device, optionally
4402 * copying data.
4403 *
4404 * @dev: DRM device to create property for
4405 * @length: Length to allocate for blob data
4406 * @data: If specified, copies data into blob
4407 *
4408 * Returns:
4409 * New blob property with a single reference on success, or an ERR_PTR
4410 * value on failure.
4411 */
4412 struct drm_property_blob *
4413 drm_property_create_blob(struct drm_device *dev, size_t length,
4414 const void *data)
4415 {
4416 struct drm_property_blob *blob;
4417 int ret;
4418
4419 if (!length || length > ULONG_MAX - sizeof(struct drm_property_blob))
4420 return ERR_PTR(-EINVAL);
4421
4422 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
4423 if (!blob)
4424 return ERR_PTR(-ENOMEM);
4425
4426 /* This must be explicitly initialised, so we can safely call list_del
4427 * on it in the removal handler, even if it isn't in a file list. */
4428 INIT_LIST_HEAD(&blob->head_file);
4429 blob->length = length;
4430 blob->dev = dev;
4431
4432 if (data)
4433 memcpy(blob->data, data, length);
4434
4435 ret = drm_mode_object_get_reg(dev, &blob->base, DRM_MODE_OBJECT_BLOB,
4436 true, drm_property_free_blob);
4437 if (ret) {
4438 kfree(blob);
4439 return ERR_PTR(-EINVAL);
4440 }
4441
4442 mutex_lock(&dev->mode_config.blob_lock);
4443 list_add_tail(&blob->head_global,
4444 &dev->mode_config.property_blob_list);
4445 mutex_unlock(&dev->mode_config.blob_lock);
4446
4447 return blob;
4448 }
4449 EXPORT_SYMBOL(drm_property_create_blob);
4450
4451 /**
4452 * drm_property_unreference_blob - Unreference a blob property
4453 *
4454 * Drop a reference on a blob property. May free the object.
4455 *
4456 * @blob: Pointer to blob property
4457 */
4458 void drm_property_unreference_blob(struct drm_property_blob *blob)
4459 {
4460 if (!blob)
4461 return;
4462
4463 drm_mode_object_unreference(&blob->base);
4464 }
4465 EXPORT_SYMBOL(drm_property_unreference_blob);
4466
4467 /**
4468 * drm_property_destroy_user_blobs - destroy all blobs created by this client
4469 * @dev: DRM device
4470 * @file_priv: destroy all blobs owned by this file handle
4471 */
4472 void drm_property_destroy_user_blobs(struct drm_device *dev,
4473 struct drm_file *file_priv)
4474 {
4475 struct drm_property_blob *blob, *bt;
4476
4477 /*
4478 * When the file gets released that means no one else can access the
4479 * blob list any more, so no need to grab dev->blob_lock.
4480 */
4481 list_for_each_entry_safe(blob, bt, &file_priv->blobs, head_file) {
4482 list_del_init(&blob->head_file);
4483 drm_property_unreference_blob(blob);
4484 }
4485 }
4486
4487 /**
4488 * drm_property_reference_blob - Take a reference on an existing property
4489 *
4490 * Take a new reference on an existing blob property.
4491 *
4492 * @blob: Pointer to blob property
4493 */
4494 struct drm_property_blob *drm_property_reference_blob(struct drm_property_blob *blob)
4495 {
4496 drm_mode_object_reference(&blob->base);
4497 return blob;
4498 }
4499 EXPORT_SYMBOL(drm_property_reference_blob);
4500
4501 /**
4502 * drm_property_lookup_blob - look up a blob property and take a reference
4503 * @dev: drm device
4504 * @id: id of the blob property
4505 *
4506 * If successful, this takes an additional reference to the blob property.
4507 * callers need to make sure to eventually unreference the returned property
4508 * again, using @drm_property_unreference_blob.
4509 */
4510 struct drm_property_blob *drm_property_lookup_blob(struct drm_device *dev,
4511 uint32_t id)
4512 {
4513 struct drm_mode_object *obj;
4514 struct drm_property_blob *blob = NULL;
4515
4516 obj = _object_find(dev, id, DRM_MODE_OBJECT_BLOB);
4517 if (obj)
4518 blob = obj_to_blob(obj);
4519 return blob;
4520 }
4521 EXPORT_SYMBOL(drm_property_lookup_blob);
4522
4523 /**
4524 * drm_property_replace_global_blob - atomically replace existing blob property
4525 * @dev: drm device
4526 * @replace: location of blob property pointer to be replaced
4527 * @length: length of data for new blob, or 0 for no data
4528 * @data: content for new blob, or NULL for no data
4529 * @obj_holds_id: optional object for property holding blob ID
4530 * @prop_holds_id: optional property holding blob ID
4531 * @return 0 on success or error on failure
4532 *
4533 * This function will atomically replace a global property in the blob list,
4534 * optionally updating a property which holds the ID of that property. It is
4535 * guaranteed to be atomic: no caller will be allowed to see intermediate
4536 * results, and either the entire operation will succeed and clean up the
4537 * previous property, or it will fail and the state will be unchanged.
4538 *
4539 * If length is 0 or data is NULL, no new blob will be created, and the holding
4540 * property, if specified, will be set to 0.
4541 *
4542 * Access to the replace pointer is assumed to be protected by the caller, e.g.
4543 * by holding the relevant modesetting object lock for its parent.
4544 *
4545 * For example, a drm_connector has a 'PATH' property, which contains the ID
4546 * of a blob property with the value of the MST path information. Calling this
4547 * function with replace pointing to the connector's path_blob_ptr, length and
4548 * data set for the new path information, obj_holds_id set to the connector's
4549 * base object, and prop_holds_id set to the path property name, will perform
4550 * a completely atomic update. The access to path_blob_ptr is protected by the
4551 * caller holding a lock on the connector.
4552 */
4553 static int drm_property_replace_global_blob(struct drm_device *dev,
4554 struct drm_property_blob **replace,
4555 size_t length,
4556 const void *data,
4557 struct drm_mode_object *obj_holds_id,
4558 struct drm_property *prop_holds_id)
4559 {
4560 struct drm_property_blob *new_blob = NULL;
4561 struct drm_property_blob *old_blob = NULL;
4562 int ret;
4563
4564 WARN_ON(replace == NULL);
4565
4566 old_blob = *replace;
4567
4568 if (length && data) {
4569 new_blob = drm_property_create_blob(dev, length, data);
4570 if (IS_ERR(new_blob))
4571 return PTR_ERR(new_blob);
4572 }
4573
4574 /* This does not need to be synchronised with blob_lock, as the
4575 * get_properties ioctl locks all modesetting objects, and
4576 * obj_holds_id must be locked before calling here, so we cannot
4577 * have its value out of sync with the list membership modified
4578 * below under blob_lock. */
4579 if (obj_holds_id) {
4580 ret = drm_object_property_set_value(obj_holds_id,
4581 prop_holds_id,
4582 new_blob ?
4583 new_blob->base.id : 0);
4584 if (ret != 0)
4585 goto err_created;
4586 }
4587
4588 drm_property_unreference_blob(old_blob);
4589 *replace = new_blob;
4590
4591 return 0;
4592
4593 err_created:
4594 drm_property_unreference_blob(new_blob);
4595 return ret;
4596 }
4597
4598 /**
4599 * drm_mode_getblob_ioctl - get the contents of a blob property value
4600 * @dev: DRM device
4601 * @data: ioctl data
4602 * @file_priv: DRM file info
4603 *
4604 * This function retrieves the contents of a blob property. The value stored in
4605 * an object's blob property is just a normal modeset object id.
4606 *
4607 * Called by the user via ioctl.
4608 *
4609 * Returns:
4610 * Zero on success, negative errno on failure.
4611 */
4612 int drm_mode_getblob_ioctl(struct drm_device *dev,
4613 void *data, struct drm_file *file_priv)
4614 {
4615 struct drm_mode_get_blob *out_resp = data;
4616 struct drm_property_blob *blob;
4617 int ret = 0;
4618 void __user *blob_ptr;
4619
4620 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4621 return -EINVAL;
4622
4623 blob = drm_property_lookup_blob(dev, out_resp->blob_id);
4624 if (!blob)
4625 return -ENOENT;
4626
4627 if (out_resp->length == blob->length) {
4628 blob_ptr = (void __user *)(unsigned long)out_resp->data;
4629 if (copy_to_user(blob_ptr, blob->data, blob->length)) {
4630 ret = -EFAULT;
4631 goto unref;
4632 }
4633 }
4634 out_resp->length = blob->length;
4635 unref:
4636 drm_property_unreference_blob(blob);
4637
4638 return ret;
4639 }
4640
4641 /**
4642 * drm_mode_createblob_ioctl - create a new blob property
4643 * @dev: DRM device
4644 * @data: ioctl data
4645 * @file_priv: DRM file info
4646 *
4647 * This function creates a new blob property with user-defined values. In order
4648 * to give us sensible validation and checking when creating, rather than at
4649 * every potential use, we also require a type to be provided upfront.
4650 *
4651 * Called by the user via ioctl.
4652 *
4653 * Returns:
4654 * Zero on success, negative errno on failure.
4655 */
4656 int drm_mode_createblob_ioctl(struct drm_device *dev,
4657 void *data, struct drm_file *file_priv)
4658 {
4659 struct drm_mode_create_blob *out_resp = data;
4660 struct drm_property_blob *blob;
4661 void __user *blob_ptr;
4662 int ret = 0;
4663
4664 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4665 return -EINVAL;
4666
4667 blob = drm_property_create_blob(dev, out_resp->length, NULL);
4668 if (IS_ERR(blob))
4669 return PTR_ERR(blob);
4670
4671 blob_ptr = (void __user *)(unsigned long)out_resp->data;
4672 if (copy_from_user(blob->data, blob_ptr, out_resp->length)) {
4673 ret = -EFAULT;
4674 goto out_blob;
4675 }
4676
4677 /* Dropping the lock between create_blob and our access here is safe
4678 * as only the same file_priv can remove the blob; at this point, it is
4679 * not associated with any file_priv. */
4680 mutex_lock(&dev->mode_config.blob_lock);
4681 out_resp->blob_id = blob->base.id;
4682 list_add_tail(&blob->head_file, &file_priv->blobs);
4683 mutex_unlock(&dev->mode_config.blob_lock);
4684
4685 return 0;
4686
4687 out_blob:
4688 drm_property_unreference_blob(blob);
4689 return ret;
4690 }
4691
4692 /**
4693 * drm_mode_destroyblob_ioctl - destroy a user blob property
4694 * @dev: DRM device
4695 * @data: ioctl data
4696 * @file_priv: DRM file info
4697 *
4698 * Destroy an existing user-defined blob property.
4699 *
4700 * Called by the user via ioctl.
4701 *
4702 * Returns:
4703 * Zero on success, negative errno on failure.
4704 */
4705 int drm_mode_destroyblob_ioctl(struct drm_device *dev,
4706 void *data, struct drm_file *file_priv)
4707 {
4708 struct drm_mode_destroy_blob *out_resp = data;
4709 struct drm_property_blob *blob = NULL, *bt;
4710 bool found = false;
4711 int ret = 0;
4712
4713 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4714 return -EINVAL;
4715
4716 blob = drm_property_lookup_blob(dev, out_resp->blob_id);
4717 if (!blob)
4718 return -ENOENT;
4719
4720 mutex_lock(&dev->mode_config.blob_lock);
4721 /* Ensure the property was actually created by this user. */
4722 list_for_each_entry(bt, &file_priv->blobs, head_file) {
4723 if (bt == blob) {
4724 found = true;
4725 break;
4726 }
4727 }
4728
4729 if (!found) {
4730 ret = -EPERM;
4731 goto err;
4732 }
4733
4734 /* We must drop head_file here, because we may not be the last
4735 * reference on the blob. */
4736 list_del_init(&blob->head_file);
4737 mutex_unlock(&dev->mode_config.blob_lock);
4738
4739 /* One reference from lookup, and one from the filp. */
4740 drm_property_unreference_blob(blob);
4741 drm_property_unreference_blob(blob);
4742
4743 return 0;
4744
4745 err:
4746 mutex_unlock(&dev->mode_config.blob_lock);
4747 drm_property_unreference_blob(blob);
4748
4749 return ret;
4750 }
4751
4752 /**
4753 * drm_mode_connector_set_path_property - set tile property on connector
4754 * @connector: connector to set property on.
4755 * @path: path to use for property; must not be NULL.
4756 *
4757 * This creates a property to expose to userspace to specify a
4758 * connector path. This is mainly used for DisplayPort MST where
4759 * connectors have a topology and we want to allow userspace to give
4760 * them more meaningful names.
4761 *
4762 * Returns:
4763 * Zero on success, negative errno on failure.
4764 */
4765 int drm_mode_connector_set_path_property(struct drm_connector *connector,
4766 const char *path)
4767 {
4768 struct drm_device *dev = connector->dev;
4769 int ret;
4770
4771 ret = drm_property_replace_global_blob(dev,
4772 &connector->path_blob_ptr,
4773 strlen(path) + 1,
4774 path,
4775 &connector->base,
4776 dev->mode_config.path_property);
4777 return ret;
4778 }
4779 EXPORT_SYMBOL(drm_mode_connector_set_path_property);
4780
4781 /**
4782 * drm_mode_connector_set_tile_property - set tile property on connector
4783 * @connector: connector to set property on.
4784 *
4785 * This looks up the tile information for a connector, and creates a
4786 * property for userspace to parse if it exists. The property is of
4787 * the form of 8 integers using ':' as a separator.
4788 *
4789 * Returns:
4790 * Zero on success, errno on failure.
4791 */
4792 int drm_mode_connector_set_tile_property(struct drm_connector *connector)
4793 {
4794 struct drm_device *dev = connector->dev;
4795 char tile[256];
4796 int ret;
4797
4798 if (!connector->has_tile) {
4799 ret = drm_property_replace_global_blob(dev,
4800 &connector->tile_blob_ptr,
4801 0,
4802 NULL,
4803 &connector->base,
4804 dev->mode_config.tile_property);
4805 return ret;
4806 }
4807
4808 snprintf(tile, 256, "%d:%d:%d:%d:%d:%d:%d:%d",
4809 connector->tile_group->id, connector->tile_is_single_monitor,
4810 connector->num_h_tile, connector->num_v_tile,
4811 connector->tile_h_loc, connector->tile_v_loc,
4812 connector->tile_h_size, connector->tile_v_size);
4813
4814 ret = drm_property_replace_global_blob(dev,
4815 &connector->tile_blob_ptr,
4816 strlen(tile) + 1,
4817 tile,
4818 &connector->base,
4819 dev->mode_config.tile_property);
4820 return ret;
4821 }
4822 EXPORT_SYMBOL(drm_mode_connector_set_tile_property);
4823
4824 /**
4825 * drm_mode_connector_update_edid_property - update the edid property of a connector
4826 * @connector: drm connector
4827 * @edid: new value of the edid property
4828 *
4829 * This function creates a new blob modeset object and assigns its id to the
4830 * connector's edid property.
4831 *
4832 * Returns:
4833 * Zero on success, negative errno on failure.
4834 */
4835 int drm_mode_connector_update_edid_property(struct drm_connector *connector,
4836 const struct edid *edid)
4837 {
4838 struct drm_device *dev = connector->dev;
4839 size_t size = 0;
4840 int ret;
4841
4842 /* ignore requests to set edid when overridden */
4843 if (connector->override_edid)
4844 return 0;
4845
4846 if (edid)
4847 size = EDID_LENGTH * (1 + edid->extensions);
4848
4849 ret = drm_property_replace_global_blob(dev,
4850 &connector->edid_blob_ptr,
4851 size,
4852 edid,
4853 &connector->base,
4854 dev->mode_config.edid_property);
4855 return ret;
4856 }
4857 EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
4858
4859 /* Some properties could refer to dynamic refcnt'd objects, or things that
4860 * need special locking to handle lifetime issues (ie. to ensure the prop
4861 * value doesn't become invalid part way through the property update due to
4862 * race). The value returned by reference via 'obj' should be passed back
4863 * to drm_property_change_valid_put() after the property is set (and the
4864 * object to which the property is attached has a chance to take it's own
4865 * reference).
4866 */
4867 bool drm_property_change_valid_get(struct drm_property *property,
4868 uint64_t value, struct drm_mode_object **ref)
4869 {
4870 int i;
4871
4872 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
4873 return false;
4874
4875 *ref = NULL;
4876
4877 if (drm_property_type_is(property, DRM_MODE_PROP_RANGE)) {
4878 if (value < property->values[0] || value > property->values[1])
4879 return false;
4880 return true;
4881 } else if (drm_property_type_is(property, DRM_MODE_PROP_SIGNED_RANGE)) {
4882 int64_t svalue = U642I64(value);
4883
4884 if (svalue < U642I64(property->values[0]) ||
4885 svalue > U642I64(property->values[1]))
4886 return false;
4887 return true;
4888 } else if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
4889 uint64_t valid_mask = 0;
4890
4891 for (i = 0; i < property->num_values; i++)
4892 valid_mask |= (1ULL << property->values[i]);
4893 return !(value & ~valid_mask);
4894 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
4895 struct drm_property_blob *blob;
4896
4897 if (value == 0)
4898 return true;
4899
4900 blob = drm_property_lookup_blob(property->dev, value);
4901 if (blob) {
4902 *ref = &blob->base;
4903 return true;
4904 } else {
4905 return false;
4906 }
4907 } else if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
4908 /* a zero value for an object property translates to null: */
4909 if (value == 0)
4910 return true;
4911
4912 *ref = _object_find(property->dev, value, property->values[0]);
4913 return *ref != NULL;
4914 }
4915
4916 for (i = 0; i < property->num_values; i++)
4917 if (property->values[i] == value)
4918 return true;
4919 return false;
4920 }
4921
4922 void drm_property_change_valid_put(struct drm_property *property,
4923 struct drm_mode_object *ref)
4924 {
4925 if (!ref)
4926 return;
4927
4928 if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
4929 drm_mode_object_unreference(ref);
4930 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
4931 drm_property_unreference_blob(obj_to_blob(ref));
4932 }
4933
4934 /**
4935 * drm_mode_connector_property_set_ioctl - set the current value of a connector property
4936 * @dev: DRM device
4937 * @data: ioctl data
4938 * @file_priv: DRM file info
4939 *
4940 * This function sets the current value for a connectors's property. It also
4941 * calls into a driver's ->set_property callback to update the hardware state
4942 *
4943 * Called by the user via ioctl.
4944 *
4945 * Returns:
4946 * Zero on success, negative errno on failure.
4947 */
4948 int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
4949 void *data, struct drm_file *file_priv)
4950 {
4951 struct drm_mode_connector_set_property *conn_set_prop = data;
4952 struct drm_mode_obj_set_property obj_set_prop = {
4953 .value = conn_set_prop->value,
4954 .prop_id = conn_set_prop->prop_id,
4955 .obj_id = conn_set_prop->connector_id,
4956 .obj_type = DRM_MODE_OBJECT_CONNECTOR
4957 };
4958
4959 /* It does all the locking and checking we need */
4960 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
4961 }
4962
4963 static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
4964 struct drm_property *property,
4965 uint64_t value)
4966 {
4967 int ret = -EINVAL;
4968 struct drm_connector *connector = obj_to_connector(obj);
4969
4970 /* Do DPMS ourselves */
4971 if (property == connector->dev->mode_config.dpms_property) {
4972 ret = (*connector->funcs->dpms)(connector, (int)value);
4973 } else if (connector->funcs->set_property)
4974 ret = connector->funcs->set_property(connector, property, value);
4975
4976 /* store the property value if successful */
4977 if (!ret)
4978 drm_object_property_set_value(&connector->base, property, value);
4979 return ret;
4980 }
4981
4982 static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
4983 struct drm_property *property,
4984 uint64_t value)
4985 {
4986 int ret = -EINVAL;
4987 struct drm_crtc *crtc = obj_to_crtc(obj);
4988
4989 if (crtc->funcs->set_property)
4990 ret = crtc->funcs->set_property(crtc, property, value);
4991 if (!ret)
4992 drm_object_property_set_value(obj, property, value);
4993
4994 return ret;
4995 }
4996
4997 /**
4998 * drm_mode_plane_set_obj_prop - set the value of a property
4999 * @plane: drm plane object to set property value for
5000 * @property: property to set
5001 * @value: value the property should be set to
5002 *
5003 * This functions sets a given property on a given plane object. This function
5004 * calls the driver's ->set_property callback and changes the software state of
5005 * the property if the callback succeeds.
5006 *
5007 * Returns:
5008 * Zero on success, error code on failure.
5009 */
5010 int drm_mode_plane_set_obj_prop(struct drm_plane *plane,
5011 struct drm_property *property,
5012 uint64_t value)
5013 {
5014 int ret = -EINVAL;
5015 struct drm_mode_object *obj = &plane->base;
5016
5017 if (plane->funcs->set_property)
5018 ret = plane->funcs->set_property(plane, property, value);
5019 if (!ret)
5020 drm_object_property_set_value(obj, property, value);
5021
5022 return ret;
5023 }
5024 EXPORT_SYMBOL(drm_mode_plane_set_obj_prop);
5025
5026 /**
5027 * drm_mode_obj_get_properties_ioctl - get the current value of a object's property
5028 * @dev: DRM device
5029 * @data: ioctl data
5030 * @file_priv: DRM file info
5031 *
5032 * This function retrieves the current value for an object's property. Compared
5033 * to the connector specific ioctl this one is extended to also work on crtc and
5034 * plane objects.
5035 *
5036 * Called by the user via ioctl.
5037 *
5038 * Returns:
5039 * Zero on success, negative errno on failure.
5040 */
5041 int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
5042 struct drm_file *file_priv)
5043 {
5044 struct drm_mode_obj_get_properties *arg = data;
5045 struct drm_mode_object *obj;
5046 int ret = 0;
5047
5048 if (!drm_core_check_feature(dev, DRIVER_MODESET))
5049 return -EINVAL;
5050
5051 drm_modeset_lock_all(dev);
5052
5053 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
5054 if (!obj) {
5055 ret = -ENOENT;
5056 goto out;
5057 }
5058 if (!obj->properties) {
5059 ret = -EINVAL;
5060 goto out_unref;
5061 }
5062
5063 ret = get_properties(obj, file_priv->atomic,
5064 (uint32_t __user *)(unsigned long)(arg->props_ptr),
5065 (uint64_t __user *)(unsigned long)(arg->prop_values_ptr),
5066 &arg->count_props);
5067
5068 out_unref:
5069 drm_mode_object_unreference(obj);
5070 out:
5071 drm_modeset_unlock_all(dev);
5072 return ret;
5073 }
5074
5075 /**
5076 * drm_mode_obj_set_property_ioctl - set the current value of an object's property
5077 * @dev: DRM device
5078 * @data: ioctl data
5079 * @file_priv: DRM file info
5080 *
5081 * This function sets the current value for an object's property. It also calls
5082 * into a driver's ->set_property callback to update the hardware state.
5083 * Compared to the connector specific ioctl this one is extended to also work on
5084 * crtc and plane objects.
5085 *
5086 * Called by the user via ioctl.
5087 *
5088 * Returns:
5089 * Zero on success, negative errno on failure.
5090 */
5091 int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
5092 struct drm_file *file_priv)
5093 {
5094 struct drm_mode_obj_set_property *arg = data;
5095 struct drm_mode_object *arg_obj;
5096 struct drm_mode_object *prop_obj;
5097 struct drm_property *property;
5098 int i, ret = -EINVAL;
5099 struct drm_mode_object *ref;
5100
5101 if (!drm_core_check_feature(dev, DRIVER_MODESET))
5102 return -EINVAL;
5103
5104 drm_modeset_lock_all(dev);
5105
5106 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
5107 if (!arg_obj) {
5108 ret = -ENOENT;
5109 goto out;
5110 }
5111 if (!arg_obj->properties)
5112 goto out_unref;
5113
5114 for (i = 0; i < arg_obj->properties->count; i++)
5115 if (arg_obj->properties->properties[i]->base.id == arg->prop_id)
5116 break;
5117
5118 if (i == arg_obj->properties->count)
5119 goto out_unref;
5120
5121 prop_obj = drm_mode_object_find(dev, arg->prop_id,
5122 DRM_MODE_OBJECT_PROPERTY);
5123 if (!prop_obj) {
5124 ret = -ENOENT;
5125 goto out_unref;
5126 }
5127 property = obj_to_property(prop_obj);
5128
5129 if (!drm_property_change_valid_get(property, arg->value, &ref))
5130 goto out_unref;
5131
5132 switch (arg_obj->type) {
5133 case DRM_MODE_OBJECT_CONNECTOR:
5134 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
5135 arg->value);
5136 break;
5137 case DRM_MODE_OBJECT_CRTC:
5138 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
5139 break;
5140 case DRM_MODE_OBJECT_PLANE:
5141 ret = drm_mode_plane_set_obj_prop(obj_to_plane(arg_obj),
5142 property, arg->value);
5143 break;
5144 }
5145
5146 drm_property_change_valid_put(property, ref);
5147
5148 out_unref:
5149 drm_mode_object_unreference(arg_obj);
5150 out:
5151 drm_modeset_unlock_all(dev);
5152 return ret;
5153 }
5154
5155 /**
5156 * drm_mode_connector_attach_encoder - attach a connector to an encoder
5157 * @connector: connector to attach
5158 * @encoder: encoder to attach @connector to
5159 *
5160 * This function links up a connector to an encoder. Note that the routing
5161 * restrictions between encoders and crtcs are exposed to userspace through the
5162 * possible_clones and possible_crtcs bitmasks.
5163 *
5164 * Returns:
5165 * Zero on success, negative errno on failure.
5166 */
5167 int drm_mode_connector_attach_encoder(struct drm_connector *connector,
5168 struct drm_encoder *encoder)
5169 {
5170 int i;
5171
5172 /*
5173 * In the past, drivers have attempted to model the static association
5174 * of connector to encoder in simple connector/encoder devices using a
5175 * direct assignment of connector->encoder = encoder. This connection
5176 * is a logical one and the responsibility of the core, so drivers are
5177 * expected not to mess with this.
5178 *
5179 * Note that the error return should've been enough here, but a large
5180 * majority of drivers ignores the return value, so add in a big WARN
5181 * to get people's attention.
5182 */
5183 if (WARN_ON(connector->encoder))
5184 return -EINVAL;
5185
5186 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
5187 if (connector->encoder_ids[i] == 0) {
5188 connector->encoder_ids[i] = encoder->base.id;
5189 return 0;
5190 }
5191 }
5192 return -ENOMEM;
5193 }
5194 EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
5195
5196 /**
5197 * drm_mode_crtc_set_gamma_size - set the gamma table size
5198 * @crtc: CRTC to set the gamma table size for
5199 * @gamma_size: size of the gamma table
5200 *
5201 * Drivers which support gamma tables should set this to the supported gamma
5202 * table size when initializing the CRTC. Currently the drm core only supports a
5203 * fixed gamma table size.
5204 *
5205 * Returns:
5206 * Zero on success, negative errno on failure.
5207 */
5208 int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
5209 int gamma_size)
5210 {
5211 uint16_t *r_base, *g_base, *b_base;
5212 int i;
5213
5214 crtc->gamma_size = gamma_size;
5215
5216 crtc->gamma_store = kcalloc(gamma_size, sizeof(uint16_t) * 3,
5217 GFP_KERNEL);
5218 if (!crtc->gamma_store) {
5219 crtc->gamma_size = 0;
5220 return -ENOMEM;
5221 }
5222
5223 r_base = crtc->gamma_store;
5224 g_base = r_base + gamma_size;
5225 b_base = g_base + gamma_size;
5226 for (i = 0; i < gamma_size; i++) {
5227 r_base[i] = i << 8;
5228 g_base[i] = i << 8;
5229 b_base[i] = i << 8;
5230 }
5231
5232
5233 return 0;
5234 }
5235 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
5236
5237 /**
5238 * drm_mode_gamma_set_ioctl - set the gamma table
5239 * @dev: DRM device
5240 * @data: ioctl data
5241 * @file_priv: DRM file info
5242 *
5243 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
5244 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
5245 *
5246 * Called by the user via ioctl.
5247 *
5248 * Returns:
5249 * Zero on success, negative errno on failure.
5250 */
5251 int drm_mode_gamma_set_ioctl(struct drm_device *dev,
5252 void *data, struct drm_file *file_priv)
5253 {
5254 struct drm_mode_crtc_lut *crtc_lut = data;
5255 struct drm_crtc *crtc;
5256 void *r_base, *g_base, *b_base;
5257 int size;
5258 int ret = 0;
5259
5260 if (!drm_core_check_feature(dev, DRIVER_MODESET))
5261 return -EINVAL;
5262
5263 drm_modeset_lock_all(dev);
5264 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
5265 if (!crtc) {
5266 ret = -ENOENT;
5267 goto out;
5268 }
5269
5270 if (crtc->funcs->gamma_set == NULL) {
5271 ret = -ENOSYS;
5272 goto out;
5273 }
5274
5275 /* memcpy into gamma store */
5276 if (crtc_lut->gamma_size != crtc->gamma_size) {
5277 ret = -EINVAL;
5278 goto out;
5279 }
5280
5281 size = crtc_lut->gamma_size * (sizeof(uint16_t));
5282 r_base = crtc->gamma_store;
5283 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
5284 ret = -EFAULT;
5285 goto out;
5286 }
5287
5288 g_base = r_base + size;
5289 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
5290 ret = -EFAULT;
5291 goto out;
5292 }
5293
5294 b_base = g_base + size;
5295 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
5296 ret = -EFAULT;
5297 goto out;
5298 }
5299
5300 ret = crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, crtc->gamma_size);
5301
5302 out:
5303 drm_modeset_unlock_all(dev);
5304 return ret;
5305
5306 }
5307
5308 /**
5309 * drm_mode_gamma_get_ioctl - get the gamma table
5310 * @dev: DRM device
5311 * @data: ioctl data
5312 * @file_priv: DRM file info
5313 *
5314 * Copy the current gamma table into the storage provided. This also provides
5315 * the gamma table size the driver expects, which can be used to size the
5316 * allocated storage.
5317 *
5318 * Called by the user via ioctl.
5319 *
5320 * Returns:
5321 * Zero on success, negative errno on failure.
5322 */
5323 int drm_mode_gamma_get_ioctl(struct drm_device *dev,
5324 void *data, struct drm_file *file_priv)
5325 {
5326 struct drm_mode_crtc_lut *crtc_lut = data;
5327 struct drm_crtc *crtc;
5328 void *r_base, *g_base, *b_base;
5329 int size;
5330 int ret = 0;
5331
5332 if (!drm_core_check_feature(dev, DRIVER_MODESET))
5333 return -EINVAL;
5334
5335 drm_modeset_lock_all(dev);
5336 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
5337 if (!crtc) {
5338 ret = -ENOENT;
5339 goto out;
5340 }
5341
5342 /* memcpy into gamma store */
5343 if (crtc_lut->gamma_size != crtc->gamma_size) {
5344 ret = -EINVAL;
5345 goto out;
5346 }
5347
5348 size = crtc_lut->gamma_size * (sizeof(uint16_t));
5349 r_base = crtc->gamma_store;
5350 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
5351 ret = -EFAULT;
5352 goto out;
5353 }
5354
5355 g_base = r_base + size;
5356 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
5357 ret = -EFAULT;
5358 goto out;
5359 }
5360
5361 b_base = g_base + size;
5362 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
5363 ret = -EFAULT;
5364 goto out;
5365 }
5366 out:
5367 drm_modeset_unlock_all(dev);
5368 return ret;
5369 }
5370
5371 /**
5372 * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
5373 * @dev: DRM device
5374 * @data: ioctl data
5375 * @file_priv: DRM file info
5376 *
5377 * This schedules an asynchronous update on a given CRTC, called page flip.
5378 * Optionally a drm event is generated to signal the completion of the event.
5379 * Generic drivers cannot assume that a pageflip with changed framebuffer
5380 * properties (including driver specific metadata like tiling layout) will work,
5381 * but some drivers support e.g. pixel format changes through the pageflip
5382 * ioctl.
5383 *
5384 * Called by the user via ioctl.
5385 *
5386 * Returns:
5387 * Zero on success, negative errno on failure.
5388 */
5389 int drm_mode_page_flip_ioctl(struct drm_device *dev,
5390 void *data, struct drm_file *file_priv)
5391 {
5392 struct drm_mode_crtc_page_flip *page_flip = data;
5393 struct drm_crtc *crtc;
5394 struct drm_framebuffer *fb = NULL;
5395 struct drm_pending_vblank_event *e = NULL;
5396 int ret = -EINVAL;
5397
5398 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
5399 page_flip->reserved != 0)
5400 return -EINVAL;
5401
5402 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
5403 return -EINVAL;
5404
5405 crtc = drm_crtc_find(dev, page_flip->crtc_id);
5406 if (!crtc)
5407 return -ENOENT;
5408
5409 drm_modeset_lock_crtc(crtc, crtc->primary);
5410 if (crtc->primary->fb == NULL) {
5411 /* The framebuffer is currently unbound, presumably
5412 * due to a hotplug event, that userspace has not
5413 * yet discovered.
5414 */
5415 ret = -EBUSY;
5416 goto out;
5417 }
5418
5419 if (crtc->funcs->page_flip == NULL)
5420 goto out;
5421
5422 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
5423 if (!fb) {
5424 ret = -ENOENT;
5425 goto out;
5426 }
5427
5428 if (crtc->state) {
5429 const struct drm_plane_state *state = crtc->primary->state;
5430
5431 ret = check_src_coords(state->src_x, state->src_y,
5432 state->src_w, state->src_h, fb);
5433 } else {
5434 ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
5435 }
5436 if (ret)
5437 goto out;
5438
5439 if (crtc->primary->fb->pixel_format != fb->pixel_format) {
5440 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
5441 ret = -EINVAL;
5442 goto out;
5443 }
5444
5445 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
5446 e = kzalloc(sizeof *e, GFP_KERNEL);
5447 if (!e) {
5448 ret = -ENOMEM;
5449 goto out;
5450 }
5451 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
5452 e->event.base.length = sizeof(e->event);
5453 e->event.user_data = page_flip->user_data;
5454 ret = drm_event_reserve_init(dev, file_priv, &e->base, &e->event.base);
5455 if (ret) {
5456 kfree(e);
5457 goto out;
5458 }
5459 }
5460
5461 crtc->primary->old_fb = crtc->primary->fb;
5462 ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
5463 if (ret) {
5464 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT)
5465 drm_event_cancel_free(dev, &e->base);
5466 /* Keep the old fb, don't unref it. */
5467 crtc->primary->old_fb = NULL;
5468 } else {
5469 crtc->primary->fb = fb;
5470 /* Unref only the old framebuffer. */
5471 fb = NULL;
5472 }
5473
5474 out:
5475 if (fb)
5476 drm_framebuffer_unreference(fb);
5477 if (crtc->primary->old_fb)
5478 drm_framebuffer_unreference(crtc->primary->old_fb);
5479 crtc->primary->old_fb = NULL;
5480 drm_modeset_unlock_crtc(crtc);
5481
5482 return ret;
5483 }
5484
5485 /**
5486 * drm_mode_config_reset - call ->reset callbacks
5487 * @dev: drm device
5488 *
5489 * This functions calls all the crtc's, encoder's and connector's ->reset
5490 * callback. Drivers can use this in e.g. their driver load or resume code to
5491 * reset hardware and software state.
5492 */
5493 void drm_mode_config_reset(struct drm_device *dev)
5494 {
5495 struct drm_crtc *crtc;
5496 struct drm_plane *plane;
5497 struct drm_encoder *encoder;
5498 struct drm_connector *connector;
5499
5500 drm_for_each_plane(plane, dev)
5501 if (plane->funcs->reset)
5502 plane->funcs->reset(plane);
5503
5504 drm_for_each_crtc(crtc, dev)
5505 if (crtc->funcs->reset)
5506 crtc->funcs->reset(crtc);
5507
5508 drm_for_each_encoder(encoder, dev)
5509 if (encoder->funcs->reset)
5510 encoder->funcs->reset(encoder);
5511
5512 mutex_lock(&dev->mode_config.mutex);
5513 drm_for_each_connector(connector, dev)
5514 if (connector->funcs->reset)
5515 connector->funcs->reset(connector);
5516 mutex_unlock(&dev->mode_config.mutex);
5517 }
5518 EXPORT_SYMBOL(drm_mode_config_reset);
5519
5520 /**
5521 * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
5522 * @dev: DRM device
5523 * @data: ioctl data
5524 * @file_priv: DRM file info
5525 *
5526 * This creates a new dumb buffer in the driver's backing storage manager (GEM,
5527 * TTM or something else entirely) and returns the resulting buffer handle. This
5528 * handle can then be wrapped up into a framebuffer modeset object.
5529 *
5530 * Note that userspace is not allowed to use such objects for render
5531 * acceleration - drivers must create their own private ioctls for such a use
5532 * case.
5533 *
5534 * Called by the user via ioctl.
5535 *
5536 * Returns:
5537 * Zero on success, negative errno on failure.
5538 */
5539 int drm_mode_create_dumb_ioctl(struct drm_device *dev,
5540 void *data, struct drm_file *file_priv)
5541 {
5542 struct drm_mode_create_dumb *args = data;
5543 u32 cpp, stride, size;
5544
5545 if (!dev->driver->dumb_create)
5546 return -ENOSYS;
5547 if (!args->width || !args->height || !args->bpp)
5548 return -EINVAL;
5549
5550 /* overflow checks for 32bit size calculations */
5551 /* NOTE: DIV_ROUND_UP() can overflow */
5552 cpp = DIV_ROUND_UP(args->bpp, 8);
5553 if (!cpp || cpp > 0xffffffffU / args->width)
5554 return -EINVAL;
5555 stride = cpp * args->width;
5556 if (args->height > 0xffffffffU / stride)
5557 return -EINVAL;
5558
5559 /* test for wrap-around */
5560 size = args->height * stride;
5561 if (PAGE_ALIGN(size) == 0)
5562 return -EINVAL;
5563
5564 /*
5565 * handle, pitch and size are output parameters. Zero them out to
5566 * prevent drivers from accidentally using uninitialized data. Since
5567 * not all existing userspace is clearing these fields properly we
5568 * cannot reject IOCTL with garbage in them.
5569 */
5570 args->handle = 0;
5571 args->pitch = 0;
5572 args->size = 0;
5573
5574 return dev->driver->dumb_create(file_priv, dev, args);
5575 }
5576
5577 /**
5578 * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
5579 * @dev: DRM device
5580 * @data: ioctl data
5581 * @file_priv: DRM file info
5582 *
5583 * Allocate an offset in the drm device node's address space to be able to
5584 * memory map a dumb buffer.
5585 *
5586 * Called by the user via ioctl.
5587 *
5588 * Returns:
5589 * Zero on success, negative errno on failure.
5590 */
5591 int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
5592 void *data, struct drm_file *file_priv)
5593 {
5594 struct drm_mode_map_dumb *args = data;
5595
5596 /* call driver ioctl to get mmap offset */
5597 if (!dev->driver->dumb_map_offset)
5598 return -ENOSYS;
5599
5600 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
5601 }
5602
5603 /**
5604 * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
5605 * @dev: DRM device
5606 * @data: ioctl data
5607 * @file_priv: DRM file info
5608 *
5609 * This destroys the userspace handle for the given dumb backing storage buffer.
5610 * Since buffer objects must be reference counted in the kernel a buffer object
5611 * won't be immediately freed if a framebuffer modeset object still uses it.
5612 *
5613 * Called by the user via ioctl.
5614 *
5615 * Returns:
5616 * Zero on success, negative errno on failure.
5617 */
5618 int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
5619 void *data, struct drm_file *file_priv)
5620 {
5621 struct drm_mode_destroy_dumb *args = data;
5622
5623 if (!dev->driver->dumb_destroy)
5624 return -ENOSYS;
5625
5626 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
5627 }
5628
5629 /**
5630 * drm_rotation_simplify() - Try to simplify the rotation
5631 * @rotation: Rotation to be simplified
5632 * @supported_rotations: Supported rotations
5633 *
5634 * Attempt to simplify the rotation to a form that is supported.
5635 * Eg. if the hardware supports everything except DRM_REFLECT_X
5636 * one could call this function like this:
5637 *
5638 * drm_rotation_simplify(rotation, BIT(DRM_ROTATE_0) |
5639 * BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_180) |
5640 * BIT(DRM_ROTATE_270) | BIT(DRM_REFLECT_Y));
5641 *
5642 * to eliminate the DRM_ROTATE_X flag. Depending on what kind of
5643 * transforms the hardware supports, this function may not
5644 * be able to produce a supported transform, so the caller should
5645 * check the result afterwards.
5646 */
5647 unsigned int drm_rotation_simplify(unsigned int rotation,
5648 unsigned int supported_rotations)
5649 {
5650 if (rotation & ~supported_rotations) {
5651 rotation ^= BIT(DRM_REFLECT_X) | BIT(DRM_REFLECT_Y);
5652 rotation = (rotation & DRM_REFLECT_MASK) |
5653 BIT((ffs(rotation & DRM_ROTATE_MASK) + 1) % 4);
5654 }
5655
5656 return rotation;
5657 }
5658 EXPORT_SYMBOL(drm_rotation_simplify);
5659
5660 /**
5661 * drm_mode_config_init - initialize DRM mode_configuration structure
5662 * @dev: DRM device
5663 *
5664 * Initialize @dev's mode_config structure, used for tracking the graphics
5665 * configuration of @dev.
5666 *
5667 * Since this initializes the modeset locks, no locking is possible. Which is no
5668 * problem, since this should happen single threaded at init time. It is the
5669 * driver's problem to ensure this guarantee.
5670 *
5671 */
5672 void drm_mode_config_init(struct drm_device *dev)
5673 {
5674 mutex_init(&dev->mode_config.mutex);
5675 drm_modeset_lock_init(&dev->mode_config.connection_mutex);
5676 mutex_init(&dev->mode_config.idr_mutex);
5677 mutex_init(&dev->mode_config.fb_lock);
5678 mutex_init(&dev->mode_config.blob_lock);
5679 INIT_LIST_HEAD(&dev->mode_config.fb_list);
5680 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
5681 INIT_LIST_HEAD(&dev->mode_config.connector_list);
5682 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
5683 INIT_LIST_HEAD(&dev->mode_config.property_list);
5684 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
5685 INIT_LIST_HEAD(&dev->mode_config.plane_list);
5686 idr_init(&dev->mode_config.crtc_idr);
5687 idr_init(&dev->mode_config.tile_idr);
5688 ida_init(&dev->mode_config.connector_ida);
5689
5690 drm_modeset_lock_all(dev);
5691 drm_mode_create_standard_properties(dev);
5692 drm_modeset_unlock_all(dev);
5693
5694 /* Just to be sure */
5695 dev->mode_config.num_fb = 0;
5696 dev->mode_config.num_connector = 0;
5697 dev->mode_config.num_crtc = 0;
5698 dev->mode_config.num_encoder = 0;
5699 dev->mode_config.num_overlay_plane = 0;
5700 dev->mode_config.num_total_plane = 0;
5701 }
5702 EXPORT_SYMBOL(drm_mode_config_init);
5703
5704 /**
5705 * drm_mode_config_cleanup - free up DRM mode_config info
5706 * @dev: DRM device
5707 *
5708 * Free up all the connectors and CRTCs associated with this DRM device, then
5709 * free up the framebuffers and associated buffer objects.
5710 *
5711 * Note that since this /should/ happen single-threaded at driver/device
5712 * teardown time, no locking is required. It's the driver's job to ensure that
5713 * this guarantee actually holds true.
5714 *
5715 * FIXME: cleanup any dangling user buffer objects too
5716 */
5717 void drm_mode_config_cleanup(struct drm_device *dev)
5718 {
5719 struct drm_connector *connector, *ot;
5720 struct drm_crtc *crtc, *ct;
5721 struct drm_encoder *encoder, *enct;
5722 struct drm_framebuffer *fb, *fbt;
5723 struct drm_property *property, *pt;
5724 struct drm_property_blob *blob, *bt;
5725 struct drm_plane *plane, *plt;
5726
5727 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
5728 head) {
5729 encoder->funcs->destroy(encoder);
5730 }
5731
5732 list_for_each_entry_safe(connector, ot,
5733 &dev->mode_config.connector_list, head) {
5734 connector->funcs->destroy(connector);
5735 }
5736
5737 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
5738 head) {
5739 drm_property_destroy(dev, property);
5740 }
5741
5742 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
5743 head) {
5744 plane->funcs->destroy(plane);
5745 }
5746
5747 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
5748 crtc->funcs->destroy(crtc);
5749 }
5750
5751 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
5752 head_global) {
5753 drm_property_unreference_blob(blob);
5754 }
5755
5756 /*
5757 * Single-threaded teardown context, so it's not required to grab the
5758 * fb_lock to protect against concurrent fb_list access. Contrary, it
5759 * would actually deadlock with the drm_framebuffer_cleanup function.
5760 *
5761 * Also, if there are any framebuffers left, that's a driver leak now,
5762 * so politely WARN about this.
5763 */
5764 WARN_ON(!list_empty(&dev->mode_config.fb_list));
5765 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
5766 drm_framebuffer_free(&fb->base.refcount);
5767 }
5768
5769 ida_destroy(&dev->mode_config.connector_ida);
5770 idr_destroy(&dev->mode_config.tile_idr);
5771 idr_destroy(&dev->mode_config.crtc_idr);
5772 drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
5773 }
5774 EXPORT_SYMBOL(drm_mode_config_cleanup);
5775
5776 struct drm_property *drm_mode_create_rotation_property(struct drm_device *dev,
5777 unsigned int supported_rotations)
5778 {
5779 static const struct drm_prop_enum_list props[] = {
5780 { DRM_ROTATE_0, "rotate-0" },
5781 { DRM_ROTATE_90, "rotate-90" },
5782 { DRM_ROTATE_180, "rotate-180" },
5783 { DRM_ROTATE_270, "rotate-270" },
5784 { DRM_REFLECT_X, "reflect-x" },
5785 { DRM_REFLECT_Y, "reflect-y" },
5786 };
5787
5788 return drm_property_create_bitmask(dev, 0, "rotation",
5789 props, ARRAY_SIZE(props),
5790 supported_rotations);
5791 }
5792 EXPORT_SYMBOL(drm_mode_create_rotation_property);
5793
5794 /**
5795 * DOC: Tile group
5796 *
5797 * Tile groups are used to represent tiled monitors with a unique
5798 * integer identifier. Tiled monitors using DisplayID v1.3 have
5799 * a unique 8-byte handle, we store this in a tile group, so we
5800 * have a common identifier for all tiles in a monitor group.
5801 */
5802 static void drm_tile_group_free(struct kref *kref)
5803 {
5804 struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount);
5805 struct drm_device *dev = tg->dev;
5806 mutex_lock(&dev->mode_config.idr_mutex);
5807 idr_remove(&dev->mode_config.tile_idr, tg->id);
5808 mutex_unlock(&dev->mode_config.idr_mutex);
5809 kfree(tg);
5810 }
5811
5812 /**
5813 * drm_mode_put_tile_group - drop a reference to a tile group.
5814 * @dev: DRM device
5815 * @tg: tile group to drop reference to.
5816 *
5817 * drop reference to tile group and free if 0.
5818 */
5819 void drm_mode_put_tile_group(struct drm_device *dev,
5820 struct drm_tile_group *tg)
5821 {
5822 kref_put(&tg->refcount, drm_tile_group_free);
5823 }
5824
5825 /**
5826 * drm_mode_get_tile_group - get a reference to an existing tile group
5827 * @dev: DRM device
5828 * @topology: 8-bytes unique per monitor.
5829 *
5830 * Use the unique bytes to get a reference to an existing tile group.
5831 *
5832 * RETURNS:
5833 * tile group or NULL if not found.
5834 */
5835 struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
5836 char topology[8])
5837 {
5838 struct drm_tile_group *tg;
5839 int id;
5840 mutex_lock(&dev->mode_config.idr_mutex);
5841 idr_for_each_entry(&dev->mode_config.tile_idr, tg, id) {
5842 if (!memcmp(tg->group_data, topology, 8)) {
5843 if (!kref_get_unless_zero(&tg->refcount))
5844 tg = NULL;
5845 mutex_unlock(&dev->mode_config.idr_mutex);
5846 return tg;
5847 }
5848 }
5849 mutex_unlock(&dev->mode_config.idr_mutex);
5850 return NULL;
5851 }
5852 EXPORT_SYMBOL(drm_mode_get_tile_group);
5853
5854 /**
5855 * drm_mode_create_tile_group - create a tile group from a displayid description
5856 * @dev: DRM device
5857 * @topology: 8-bytes unique per monitor.
5858 *
5859 * Create a tile group for the unique monitor, and get a unique
5860 * identifier for the tile group.
5861 *
5862 * RETURNS:
5863 * new tile group or error.
5864 */
5865 struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
5866 char topology[8])
5867 {
5868 struct drm_tile_group *tg;
5869 int ret;
5870
5871 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
5872 if (!tg)
5873 return ERR_PTR(-ENOMEM);
5874
5875 kref_init(&tg->refcount);
5876 memcpy(tg->group_data, topology, 8);
5877 tg->dev = dev;
5878
5879 mutex_lock(&dev->mode_config.idr_mutex);
5880 ret = idr_alloc(&dev->mode_config.tile_idr, tg, 1, 0, GFP_KERNEL);
5881 if (ret >= 0) {
5882 tg->id = ret;
5883 } else {
5884 kfree(tg);
5885 tg = ERR_PTR(ret);
5886 }
5887
5888 mutex_unlock(&dev->mode_config.idr_mutex);
5889 return tg;
5890 }
5891 EXPORT_SYMBOL(drm_mode_create_tile_group);
5892
5893 /**
5894 * drm_crtc_enable_color_mgmt - enable color management properties
5895 * @crtc: DRM CRTC
5896 * @degamma_lut_size: the size of the degamma lut (before CSC)
5897 * @has_ctm: whether to attach ctm_property for CSC matrix
5898 * @gamma_lut_size: the size of the gamma lut (after CSC)
5899 *
5900 * This function lets the driver enable the color correction
5901 * properties on a CRTC. This includes 3 degamma, csc and gamma
5902 * properties that userspace can set and 2 size properties to inform
5903 * the userspace of the lut sizes. Each of the properties are
5904 * optional. The gamma and degamma properties are only attached if
5905 * their size is not 0 and ctm_property is only attached if has_ctm is
5906 * true.
5907 */
5908 void drm_crtc_enable_color_mgmt(struct drm_crtc *crtc,
5909 uint degamma_lut_size,
5910 bool has_ctm,
5911 uint gamma_lut_size)
5912 {
5913 struct drm_device *dev = crtc->dev;
5914 struct drm_mode_config *config = &dev->mode_config;
5915
5916 if (degamma_lut_size) {
5917 drm_object_attach_property(&crtc->base,
5918 config->degamma_lut_property, 0);
5919 drm_object_attach_property(&crtc->base,
5920 config->degamma_lut_size_property,
5921 degamma_lut_size);
5922 }
5923
5924 if (has_ctm)
5925 drm_object_attach_property(&crtc->base,
5926 config->ctm_property, 0);
5927
5928 if (gamma_lut_size) {
5929 drm_object_attach_property(&crtc->base,
5930 config->gamma_lut_property, 0);
5931 drm_object_attach_property(&crtc->base,
5932 config->gamma_lut_size_property,
5933 gamma_lut_size);
5934 }
5935 }
5936 EXPORT_SYMBOL(drm_crtc_enable_color_mgmt);
This page took 0.253513 seconds and 5 git commands to generate.