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