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