27eae03bd38eea96bb46c5e39bd36159a6efeb1e
[deliverable/linux.git] / drivers / gpu / drm / drm_crtc.c
1 /*
2 * Copyright (c) 2006-2008 Intel Corporation
3 * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
4 * Copyright (c) 2008 Red Hat Inc.
5 *
6 * DRM core CRTC related functions
7 *
8 * Permission to use, copy, modify, distribute, and sell this software and its
9 * documentation for any purpose is hereby granted without fee, provided that
10 * the above copyright notice appear in all copies and that both that copyright
11 * notice and this permission notice appear in supporting documentation, and
12 * that the name of the copyright holders not be used in advertising or
13 * publicity pertaining to distribution of the software without specific,
14 * written prior permission. The copyright holders make no representations
15 * about the suitability of this software for any purpose. It is provided "as
16 * is" without express or implied warranty.
17 *
18 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
20 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
21 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
22 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
23 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24 * OF THIS SOFTWARE.
25 *
26 * Authors:
27 * Keith Packard
28 * Eric Anholt <eric@anholt.net>
29 * Dave Airlie <airlied@linux.ie>
30 * Jesse Barnes <jesse.barnes@intel.com>
31 */
32 #include <linux/ctype.h>
33 #include <linux/list.h>
34 #include <linux/slab.h>
35 #include <linux/export.h>
36 #include <drm/drmP.h>
37 #include <drm/drm_crtc.h>
38 #include <drm/drm_edid.h>
39 #include <drm/drm_fourcc.h>
40 #include <drm/drm_modeset_lock.h>
41
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 void *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 if (primary)
756 primary->possible_crtcs = 1 << drm_crtc_index(crtc);
757
758 out:
759 drm_modeset_unlock_all(dev);
760
761 return ret;
762 }
763 EXPORT_SYMBOL(drm_crtc_init_with_planes);
764
765 /**
766 * drm_crtc_cleanup - Clean up the core crtc usage
767 * @crtc: CRTC to cleanup
768 *
769 * This function cleans up @crtc and removes it from the DRM mode setting
770 * core. Note that the function does *not* free the crtc structure itself,
771 * this is the responsibility of the caller.
772 */
773 void drm_crtc_cleanup(struct drm_crtc *crtc)
774 {
775 struct drm_device *dev = crtc->dev;
776
777 kfree(crtc->gamma_store);
778 crtc->gamma_store = NULL;
779
780 drm_modeset_lock_fini(&crtc->mutex);
781
782 drm_mode_object_put(dev, &crtc->base);
783 list_del(&crtc->head);
784 dev->mode_config.num_crtc--;
785 }
786 EXPORT_SYMBOL(drm_crtc_cleanup);
787
788 /**
789 * drm_crtc_index - find the index of a registered CRTC
790 * @crtc: CRTC to find index for
791 *
792 * Given a registered CRTC, return the index of that CRTC within a DRM
793 * device's list of CRTCs.
794 */
795 unsigned int drm_crtc_index(struct drm_crtc *crtc)
796 {
797 unsigned int index = 0;
798 struct drm_crtc *tmp;
799
800 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
801 if (tmp == crtc)
802 return index;
803
804 index++;
805 }
806
807 BUG();
808 }
809 EXPORT_SYMBOL(drm_crtc_index);
810
811 /*
812 * drm_mode_remove - remove and free a mode
813 * @connector: connector list to modify
814 * @mode: mode to remove
815 *
816 * Remove @mode from @connector's mode list, then free it.
817 */
818 static void drm_mode_remove(struct drm_connector *connector,
819 struct drm_display_mode *mode)
820 {
821 list_del(&mode->head);
822 drm_mode_destroy(connector->dev, mode);
823 }
824
825 /**
826 * drm_connector_init - Init a preallocated connector
827 * @dev: DRM device
828 * @connector: the connector to init
829 * @funcs: callbacks for this connector
830 * @connector_type: user visible type of the connector
831 *
832 * Initialises a preallocated connector. Connectors should be
833 * subclassed as part of driver connector objects.
834 *
835 * Returns:
836 * Zero on success, error code on failure.
837 */
838 int drm_connector_init(struct drm_device *dev,
839 struct drm_connector *connector,
840 const struct drm_connector_funcs *funcs,
841 int connector_type)
842 {
843 int ret;
844 struct ida *connector_ida =
845 &drm_connector_enum_list[connector_type].ida;
846
847 drm_modeset_lock_all(dev);
848
849 ret = drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
850 if (ret)
851 goto out_unlock;
852
853 connector->base.properties = &connector->properties;
854 connector->dev = dev;
855 connector->funcs = funcs;
856 connector->connector_type = connector_type;
857 connector->connector_type_id =
858 ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
859 if (connector->connector_type_id < 0) {
860 ret = connector->connector_type_id;
861 goto out_put;
862 }
863 connector->name =
864 kasprintf(GFP_KERNEL, "%s-%d",
865 drm_connector_enum_list[connector_type].name,
866 connector->connector_type_id);
867 if (!connector->name) {
868 ret = -ENOMEM;
869 goto out_put;
870 }
871
872 INIT_LIST_HEAD(&connector->probed_modes);
873 INIT_LIST_HEAD(&connector->modes);
874 connector->edid_blob_ptr = NULL;
875 connector->status = connector_status_unknown;
876
877 list_add_tail(&connector->head, &dev->mode_config.connector_list);
878 dev->mode_config.num_connector++;
879
880 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
881 drm_object_attach_property(&connector->base,
882 dev->mode_config.edid_property,
883 0);
884
885 drm_object_attach_property(&connector->base,
886 dev->mode_config.dpms_property, 0);
887
888 out_put:
889 if (ret)
890 drm_mode_object_put(dev, &connector->base);
891
892 out_unlock:
893 drm_modeset_unlock_all(dev);
894
895 return ret;
896 }
897 EXPORT_SYMBOL(drm_connector_init);
898
899 /**
900 * drm_connector_cleanup - cleans up an initialised connector
901 * @connector: connector to cleanup
902 *
903 * Cleans up the connector but doesn't free the object.
904 */
905 void drm_connector_cleanup(struct drm_connector *connector)
906 {
907 struct drm_device *dev = connector->dev;
908 struct drm_display_mode *mode, *t;
909
910 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
911 drm_mode_remove(connector, mode);
912
913 list_for_each_entry_safe(mode, t, &connector->modes, head)
914 drm_mode_remove(connector, mode);
915
916 ida_remove(&drm_connector_enum_list[connector->connector_type].ida,
917 connector->connector_type_id);
918
919 drm_mode_object_put(dev, &connector->base);
920 kfree(connector->name);
921 connector->name = NULL;
922 list_del(&connector->head);
923 dev->mode_config.num_connector--;
924 }
925 EXPORT_SYMBOL(drm_connector_cleanup);
926
927 /**
928 * drm_connector_unplug_all - unregister connector userspace interfaces
929 * @dev: drm device
930 *
931 * This function unregisters all connector userspace interfaces in sysfs. Should
932 * be call when the device is disconnected, e.g. from an usb driver's
933 * ->disconnect callback.
934 */
935 void drm_connector_unplug_all(struct drm_device *dev)
936 {
937 struct drm_connector *connector;
938
939 /* taking the mode config mutex ends up in a clash with sysfs */
940 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
941 drm_sysfs_connector_remove(connector);
942
943 }
944 EXPORT_SYMBOL(drm_connector_unplug_all);
945
946 /**
947 * drm_bridge_init - initialize a drm transcoder/bridge
948 * @dev: drm device
949 * @bridge: transcoder/bridge to set up
950 * @funcs: bridge function table
951 *
952 * Initialises a preallocated bridge. Bridges should be
953 * subclassed as part of driver connector objects.
954 *
955 * Returns:
956 * Zero on success, error code on failure.
957 */
958 int drm_bridge_init(struct drm_device *dev, struct drm_bridge *bridge,
959 const struct drm_bridge_funcs *funcs)
960 {
961 int ret;
962
963 drm_modeset_lock_all(dev);
964
965 ret = drm_mode_object_get(dev, &bridge->base, DRM_MODE_OBJECT_BRIDGE);
966 if (ret)
967 goto out;
968
969 bridge->dev = dev;
970 bridge->funcs = funcs;
971
972 list_add_tail(&bridge->head, &dev->mode_config.bridge_list);
973 dev->mode_config.num_bridge++;
974
975 out:
976 drm_modeset_unlock_all(dev);
977 return ret;
978 }
979 EXPORT_SYMBOL(drm_bridge_init);
980
981 /**
982 * drm_bridge_cleanup - cleans up an initialised bridge
983 * @bridge: bridge to cleanup
984 *
985 * Cleans up the bridge but doesn't free the object.
986 */
987 void drm_bridge_cleanup(struct drm_bridge *bridge)
988 {
989 struct drm_device *dev = bridge->dev;
990
991 drm_modeset_lock_all(dev);
992 drm_mode_object_put(dev, &bridge->base);
993 list_del(&bridge->head);
994 dev->mode_config.num_bridge--;
995 drm_modeset_unlock_all(dev);
996 }
997 EXPORT_SYMBOL(drm_bridge_cleanup);
998
999 /**
1000 * drm_encoder_init - Init a preallocated encoder
1001 * @dev: drm device
1002 * @encoder: the encoder to init
1003 * @funcs: callbacks for this encoder
1004 * @encoder_type: user visible type of the encoder
1005 *
1006 * Initialises a preallocated encoder. Encoder should be
1007 * subclassed as part of driver encoder objects.
1008 *
1009 * Returns:
1010 * Zero on success, error code on failure.
1011 */
1012 int drm_encoder_init(struct drm_device *dev,
1013 struct drm_encoder *encoder,
1014 const struct drm_encoder_funcs *funcs,
1015 int encoder_type)
1016 {
1017 int ret;
1018
1019 drm_modeset_lock_all(dev);
1020
1021 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
1022 if (ret)
1023 goto out_unlock;
1024
1025 encoder->dev = dev;
1026 encoder->encoder_type = encoder_type;
1027 encoder->funcs = funcs;
1028 encoder->name = kasprintf(GFP_KERNEL, "%s-%d",
1029 drm_encoder_enum_list[encoder_type].name,
1030 encoder->base.id);
1031 if (!encoder->name) {
1032 ret = -ENOMEM;
1033 goto out_put;
1034 }
1035
1036 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
1037 dev->mode_config.num_encoder++;
1038
1039 out_put:
1040 if (ret)
1041 drm_mode_object_put(dev, &encoder->base);
1042
1043 out_unlock:
1044 drm_modeset_unlock_all(dev);
1045
1046 return ret;
1047 }
1048 EXPORT_SYMBOL(drm_encoder_init);
1049
1050 /**
1051 * drm_encoder_cleanup - cleans up an initialised encoder
1052 * @encoder: encoder to cleanup
1053 *
1054 * Cleans up the encoder but doesn't free the object.
1055 */
1056 void drm_encoder_cleanup(struct drm_encoder *encoder)
1057 {
1058 struct drm_device *dev = encoder->dev;
1059 drm_modeset_lock_all(dev);
1060 drm_mode_object_put(dev, &encoder->base);
1061 kfree(encoder->name);
1062 encoder->name = NULL;
1063 list_del(&encoder->head);
1064 dev->mode_config.num_encoder--;
1065 drm_modeset_unlock_all(dev);
1066 }
1067 EXPORT_SYMBOL(drm_encoder_cleanup);
1068
1069 /**
1070 * drm_universal_plane_init - Initialize a new universal plane object
1071 * @dev: DRM device
1072 * @plane: plane object to init
1073 * @possible_crtcs: bitmask of possible CRTCs
1074 * @funcs: callbacks for the new plane
1075 * @formats: array of supported formats (%DRM_FORMAT_*)
1076 * @format_count: number of elements in @formats
1077 * @type: type of plane (overlay, primary, cursor)
1078 *
1079 * Initializes a plane object of type @type.
1080 *
1081 * Returns:
1082 * Zero on success, error code on failure.
1083 */
1084 int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane,
1085 unsigned long possible_crtcs,
1086 const struct drm_plane_funcs *funcs,
1087 const uint32_t *formats, uint32_t format_count,
1088 enum drm_plane_type type)
1089 {
1090 int ret;
1091
1092 drm_modeset_lock_all(dev);
1093
1094 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
1095 if (ret)
1096 goto out;
1097
1098 plane->base.properties = &plane->properties;
1099 plane->dev = dev;
1100 plane->funcs = funcs;
1101 plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
1102 GFP_KERNEL);
1103 if (!plane->format_types) {
1104 DRM_DEBUG_KMS("out of memory when allocating plane\n");
1105 drm_mode_object_put(dev, &plane->base);
1106 ret = -ENOMEM;
1107 goto out;
1108 }
1109
1110 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
1111 plane->format_count = format_count;
1112 plane->possible_crtcs = possible_crtcs;
1113 plane->type = type;
1114
1115 list_add_tail(&plane->head, &dev->mode_config.plane_list);
1116 dev->mode_config.num_total_plane++;
1117 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1118 dev->mode_config.num_overlay_plane++;
1119
1120 drm_object_attach_property(&plane->base,
1121 dev->mode_config.plane_type_property,
1122 plane->type);
1123
1124 out:
1125 drm_modeset_unlock_all(dev);
1126
1127 return ret;
1128 }
1129 EXPORT_SYMBOL(drm_universal_plane_init);
1130
1131 /**
1132 * drm_plane_init - Initialize a legacy plane
1133 * @dev: DRM device
1134 * @plane: plane object to init
1135 * @possible_crtcs: bitmask of possible CRTCs
1136 * @funcs: callbacks for the new plane
1137 * @formats: array of supported formats (%DRM_FORMAT_*)
1138 * @format_count: number of elements in @formats
1139 * @is_primary: plane type (primary vs overlay)
1140 *
1141 * Legacy API to initialize a DRM plane.
1142 *
1143 * New drivers should call drm_universal_plane_init() instead.
1144 *
1145 * Returns:
1146 * Zero on success, error code on failure.
1147 */
1148 int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
1149 unsigned long possible_crtcs,
1150 const struct drm_plane_funcs *funcs,
1151 const uint32_t *formats, uint32_t format_count,
1152 bool is_primary)
1153 {
1154 enum drm_plane_type type;
1155
1156 type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
1157 return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
1158 formats, format_count, type);
1159 }
1160 EXPORT_SYMBOL(drm_plane_init);
1161
1162 /**
1163 * drm_plane_cleanup - Clean up the core plane usage
1164 * @plane: plane to cleanup
1165 *
1166 * This function cleans up @plane and removes it from the DRM mode setting
1167 * core. Note that the function does *not* free the plane structure itself,
1168 * this is the responsibility of the caller.
1169 */
1170 void drm_plane_cleanup(struct drm_plane *plane)
1171 {
1172 struct drm_device *dev = plane->dev;
1173
1174 drm_modeset_lock_all(dev);
1175 kfree(plane->format_types);
1176 drm_mode_object_put(dev, &plane->base);
1177
1178 BUG_ON(list_empty(&plane->head));
1179
1180 list_del(&plane->head);
1181 dev->mode_config.num_total_plane--;
1182 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1183 dev->mode_config.num_overlay_plane--;
1184 drm_modeset_unlock_all(dev);
1185 }
1186 EXPORT_SYMBOL(drm_plane_cleanup);
1187
1188 /**
1189 * drm_plane_force_disable - Forcibly disable a plane
1190 * @plane: plane to disable
1191 *
1192 * Forces the plane to be disabled.
1193 *
1194 * Used when the plane's current framebuffer is destroyed,
1195 * and when restoring fbdev mode.
1196 */
1197 void drm_plane_force_disable(struct drm_plane *plane)
1198 {
1199 struct drm_framebuffer *old_fb = plane->fb;
1200 int ret;
1201
1202 if (!old_fb)
1203 return;
1204
1205 ret = plane->funcs->disable_plane(plane);
1206 if (ret) {
1207 DRM_ERROR("failed to disable plane with busy fb\n");
1208 return;
1209 }
1210 /* disconnect the plane from the fb and crtc: */
1211 __drm_framebuffer_unreference(old_fb);
1212 plane->fb = NULL;
1213 plane->crtc = NULL;
1214 }
1215 EXPORT_SYMBOL(drm_plane_force_disable);
1216
1217 static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
1218 {
1219 struct drm_property *edid;
1220 struct drm_property *dpms;
1221
1222 /*
1223 * Standard properties (apply to all connectors)
1224 */
1225 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
1226 DRM_MODE_PROP_IMMUTABLE,
1227 "EDID", 0);
1228 dev->mode_config.edid_property = edid;
1229
1230 dpms = drm_property_create_enum(dev, 0,
1231 "DPMS", drm_dpms_enum_list,
1232 ARRAY_SIZE(drm_dpms_enum_list));
1233 dev->mode_config.dpms_property = dpms;
1234
1235 return 0;
1236 }
1237
1238 static int drm_mode_create_standard_plane_properties(struct drm_device *dev)
1239 {
1240 struct drm_property *type;
1241
1242 /*
1243 * Standard properties (apply to all planes)
1244 */
1245 type = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1246 "type", drm_plane_type_enum_list,
1247 ARRAY_SIZE(drm_plane_type_enum_list));
1248 dev->mode_config.plane_type_property = type;
1249
1250 return 0;
1251 }
1252
1253 /**
1254 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1255 * @dev: DRM device
1256 *
1257 * Called by a driver the first time a DVI-I connector is made.
1258 */
1259 int drm_mode_create_dvi_i_properties(struct drm_device *dev)
1260 {
1261 struct drm_property *dvi_i_selector;
1262 struct drm_property *dvi_i_subconnector;
1263
1264 if (dev->mode_config.dvi_i_select_subconnector_property)
1265 return 0;
1266
1267 dvi_i_selector =
1268 drm_property_create_enum(dev, 0,
1269 "select subconnector",
1270 drm_dvi_i_select_enum_list,
1271 ARRAY_SIZE(drm_dvi_i_select_enum_list));
1272 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
1273
1274 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1275 "subconnector",
1276 drm_dvi_i_subconnector_enum_list,
1277 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
1278 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
1279
1280 return 0;
1281 }
1282 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
1283
1284 /**
1285 * drm_create_tv_properties - create TV specific connector properties
1286 * @dev: DRM device
1287 * @num_modes: number of different TV formats (modes) supported
1288 * @modes: array of pointers to strings containing name of each format
1289 *
1290 * Called by a driver's TV initialization routine, this function creates
1291 * the TV specific connector properties for a given device. Caller is
1292 * responsible for allocating a list of format names and passing them to
1293 * this routine.
1294 */
1295 int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
1296 char *modes[])
1297 {
1298 struct drm_property *tv_selector;
1299 struct drm_property *tv_subconnector;
1300 int i;
1301
1302 if (dev->mode_config.tv_select_subconnector_property)
1303 return 0;
1304
1305 /*
1306 * Basic connector properties
1307 */
1308 tv_selector = drm_property_create_enum(dev, 0,
1309 "select subconnector",
1310 drm_tv_select_enum_list,
1311 ARRAY_SIZE(drm_tv_select_enum_list));
1312 dev->mode_config.tv_select_subconnector_property = tv_selector;
1313
1314 tv_subconnector =
1315 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1316 "subconnector",
1317 drm_tv_subconnector_enum_list,
1318 ARRAY_SIZE(drm_tv_subconnector_enum_list));
1319 dev->mode_config.tv_subconnector_property = tv_subconnector;
1320
1321 /*
1322 * Other, TV specific properties: margins & TV modes.
1323 */
1324 dev->mode_config.tv_left_margin_property =
1325 drm_property_create_range(dev, 0, "left margin", 0, 100);
1326
1327 dev->mode_config.tv_right_margin_property =
1328 drm_property_create_range(dev, 0, "right margin", 0, 100);
1329
1330 dev->mode_config.tv_top_margin_property =
1331 drm_property_create_range(dev, 0, "top margin", 0, 100);
1332
1333 dev->mode_config.tv_bottom_margin_property =
1334 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
1335
1336 dev->mode_config.tv_mode_property =
1337 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1338 "mode", num_modes);
1339 for (i = 0; i < num_modes; i++)
1340 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1341 i, modes[i]);
1342
1343 dev->mode_config.tv_brightness_property =
1344 drm_property_create_range(dev, 0, "brightness", 0, 100);
1345
1346 dev->mode_config.tv_contrast_property =
1347 drm_property_create_range(dev, 0, "contrast", 0, 100);
1348
1349 dev->mode_config.tv_flicker_reduction_property =
1350 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
1351
1352 dev->mode_config.tv_overscan_property =
1353 drm_property_create_range(dev, 0, "overscan", 0, 100);
1354
1355 dev->mode_config.tv_saturation_property =
1356 drm_property_create_range(dev, 0, "saturation", 0, 100);
1357
1358 dev->mode_config.tv_hue_property =
1359 drm_property_create_range(dev, 0, "hue", 0, 100);
1360
1361 return 0;
1362 }
1363 EXPORT_SYMBOL(drm_mode_create_tv_properties);
1364
1365 /**
1366 * drm_mode_create_scaling_mode_property - create scaling mode property
1367 * @dev: DRM device
1368 *
1369 * Called by a driver the first time it's needed, must be attached to desired
1370 * connectors.
1371 */
1372 int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1373 {
1374 struct drm_property *scaling_mode;
1375
1376 if (dev->mode_config.scaling_mode_property)
1377 return 0;
1378
1379 scaling_mode =
1380 drm_property_create_enum(dev, 0, "scaling mode",
1381 drm_scaling_mode_enum_list,
1382 ARRAY_SIZE(drm_scaling_mode_enum_list));
1383
1384 dev->mode_config.scaling_mode_property = scaling_mode;
1385
1386 return 0;
1387 }
1388 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1389
1390 /**
1391 * drm_mode_create_dirty_property - create dirty property
1392 * @dev: DRM device
1393 *
1394 * Called by a driver the first time it's needed, must be attached to desired
1395 * connectors.
1396 */
1397 int drm_mode_create_dirty_info_property(struct drm_device *dev)
1398 {
1399 struct drm_property *dirty_info;
1400
1401 if (dev->mode_config.dirty_info_property)
1402 return 0;
1403
1404 dirty_info =
1405 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1406 "dirty",
1407 drm_dirty_info_enum_list,
1408 ARRAY_SIZE(drm_dirty_info_enum_list));
1409 dev->mode_config.dirty_info_property = dirty_info;
1410
1411 return 0;
1412 }
1413 EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1414
1415 static int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
1416 {
1417 uint32_t total_objects = 0;
1418
1419 total_objects += dev->mode_config.num_crtc;
1420 total_objects += dev->mode_config.num_connector;
1421 total_objects += dev->mode_config.num_encoder;
1422 total_objects += dev->mode_config.num_bridge;
1423
1424 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
1425 if (!group->id_list)
1426 return -ENOMEM;
1427
1428 group->num_crtcs = 0;
1429 group->num_connectors = 0;
1430 group->num_encoders = 0;
1431 group->num_bridges = 0;
1432 return 0;
1433 }
1434
1435 void drm_mode_group_destroy(struct drm_mode_group *group)
1436 {
1437 kfree(group->id_list);
1438 group->id_list = NULL;
1439 }
1440
1441 /*
1442 * NOTE: Driver's shouldn't ever call drm_mode_group_init_legacy_group - it is
1443 * the drm core's responsibility to set up mode control groups.
1444 */
1445 int drm_mode_group_init_legacy_group(struct drm_device *dev,
1446 struct drm_mode_group *group)
1447 {
1448 struct drm_crtc *crtc;
1449 struct drm_encoder *encoder;
1450 struct drm_connector *connector;
1451 struct drm_bridge *bridge;
1452 int ret;
1453
1454 if ((ret = drm_mode_group_init(dev, group)))
1455 return ret;
1456
1457 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
1458 group->id_list[group->num_crtcs++] = crtc->base.id;
1459
1460 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
1461 group->id_list[group->num_crtcs + group->num_encoders++] =
1462 encoder->base.id;
1463
1464 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1465 group->id_list[group->num_crtcs + group->num_encoders +
1466 group->num_connectors++] = connector->base.id;
1467
1468 list_for_each_entry(bridge, &dev->mode_config.bridge_list, head)
1469 group->id_list[group->num_crtcs + group->num_encoders +
1470 group->num_connectors + group->num_bridges++] =
1471 bridge->base.id;
1472
1473 return 0;
1474 }
1475 EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
1476
1477 /**
1478 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1479 * @out: drm_mode_modeinfo struct to return to the user
1480 * @in: drm_display_mode to use
1481 *
1482 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1483 * the user.
1484 */
1485 static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1486 const struct drm_display_mode *in)
1487 {
1488 WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1489 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1490 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1491 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1492 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1493 "timing values too large for mode info\n");
1494
1495 out->clock = in->clock;
1496 out->hdisplay = in->hdisplay;
1497 out->hsync_start = in->hsync_start;
1498 out->hsync_end = in->hsync_end;
1499 out->htotal = in->htotal;
1500 out->hskew = in->hskew;
1501 out->vdisplay = in->vdisplay;
1502 out->vsync_start = in->vsync_start;
1503 out->vsync_end = in->vsync_end;
1504 out->vtotal = in->vtotal;
1505 out->vscan = in->vscan;
1506 out->vrefresh = in->vrefresh;
1507 out->flags = in->flags;
1508 out->type = in->type;
1509 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1510 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1511 }
1512
1513 /**
1514 * drm_crtc_convert_umode - convert a modeinfo into a drm_display_mode
1515 * @out: drm_display_mode to return to the user
1516 * @in: drm_mode_modeinfo to use
1517 *
1518 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1519 * the caller.
1520 *
1521 * Returns:
1522 * Zero on success, errno on failure.
1523 */
1524 static int drm_crtc_convert_umode(struct drm_display_mode *out,
1525 const struct drm_mode_modeinfo *in)
1526 {
1527 if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1528 return -ERANGE;
1529
1530 if ((in->flags & DRM_MODE_FLAG_3D_MASK) > DRM_MODE_FLAG_3D_MAX)
1531 return -EINVAL;
1532
1533 out->clock = in->clock;
1534 out->hdisplay = in->hdisplay;
1535 out->hsync_start = in->hsync_start;
1536 out->hsync_end = in->hsync_end;
1537 out->htotal = in->htotal;
1538 out->hskew = in->hskew;
1539 out->vdisplay = in->vdisplay;
1540 out->vsync_start = in->vsync_start;
1541 out->vsync_end = in->vsync_end;
1542 out->vtotal = in->vtotal;
1543 out->vscan = in->vscan;
1544 out->vrefresh = in->vrefresh;
1545 out->flags = in->flags;
1546 out->type = in->type;
1547 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1548 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1549
1550 return 0;
1551 }
1552
1553 /**
1554 * drm_mode_getresources - get graphics configuration
1555 * @dev: drm device for the ioctl
1556 * @data: data pointer for the ioctl
1557 * @file_priv: drm file for the ioctl call
1558 *
1559 * Construct a set of configuration description structures and return
1560 * them to the user, including CRTC, connector and framebuffer configuration.
1561 *
1562 * Called by the user via ioctl.
1563 *
1564 * Returns:
1565 * Zero on success, errno on failure.
1566 */
1567 int drm_mode_getresources(struct drm_device *dev, void *data,
1568 struct drm_file *file_priv)
1569 {
1570 struct drm_mode_card_res *card_res = data;
1571 struct list_head *lh;
1572 struct drm_framebuffer *fb;
1573 struct drm_connector *connector;
1574 struct drm_crtc *crtc;
1575 struct drm_encoder *encoder;
1576 int ret = 0;
1577 int connector_count = 0;
1578 int crtc_count = 0;
1579 int fb_count = 0;
1580 int encoder_count = 0;
1581 int copied = 0, i;
1582 uint32_t __user *fb_id;
1583 uint32_t __user *crtc_id;
1584 uint32_t __user *connector_id;
1585 uint32_t __user *encoder_id;
1586 struct drm_mode_group *mode_group;
1587
1588 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1589 return -EINVAL;
1590
1591
1592 mutex_lock(&file_priv->fbs_lock);
1593 /*
1594 * For the non-control nodes we need to limit the list of resources
1595 * by IDs in the group list for this node
1596 */
1597 list_for_each(lh, &file_priv->fbs)
1598 fb_count++;
1599
1600 /* handle this in 4 parts */
1601 /* FBs */
1602 if (card_res->count_fbs >= fb_count) {
1603 copied = 0;
1604 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1605 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1606 if (put_user(fb->base.id, fb_id + copied)) {
1607 mutex_unlock(&file_priv->fbs_lock);
1608 return -EFAULT;
1609 }
1610 copied++;
1611 }
1612 }
1613 card_res->count_fbs = fb_count;
1614 mutex_unlock(&file_priv->fbs_lock);
1615
1616 drm_modeset_lock_all(dev);
1617 if (!drm_is_primary_client(file_priv)) {
1618
1619 mode_group = NULL;
1620 list_for_each(lh, &dev->mode_config.crtc_list)
1621 crtc_count++;
1622
1623 list_for_each(lh, &dev->mode_config.connector_list)
1624 connector_count++;
1625
1626 list_for_each(lh, &dev->mode_config.encoder_list)
1627 encoder_count++;
1628 } else {
1629
1630 mode_group = &file_priv->master->minor->mode_group;
1631 crtc_count = mode_group->num_crtcs;
1632 connector_count = mode_group->num_connectors;
1633 encoder_count = mode_group->num_encoders;
1634 }
1635
1636 card_res->max_height = dev->mode_config.max_height;
1637 card_res->min_height = dev->mode_config.min_height;
1638 card_res->max_width = dev->mode_config.max_width;
1639 card_res->min_width = dev->mode_config.min_width;
1640
1641 /* CRTCs */
1642 if (card_res->count_crtcs >= crtc_count) {
1643 copied = 0;
1644 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1645 if (!mode_group) {
1646 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1647 head) {
1648 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
1649 if (put_user(crtc->base.id, crtc_id + copied)) {
1650 ret = -EFAULT;
1651 goto out;
1652 }
1653 copied++;
1654 }
1655 } else {
1656 for (i = 0; i < mode_group->num_crtcs; i++) {
1657 if (put_user(mode_group->id_list[i],
1658 crtc_id + copied)) {
1659 ret = -EFAULT;
1660 goto out;
1661 }
1662 copied++;
1663 }
1664 }
1665 }
1666 card_res->count_crtcs = crtc_count;
1667
1668 /* Encoders */
1669 if (card_res->count_encoders >= encoder_count) {
1670 copied = 0;
1671 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1672 if (!mode_group) {
1673 list_for_each_entry(encoder,
1674 &dev->mode_config.encoder_list,
1675 head) {
1676 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1677 encoder->name);
1678 if (put_user(encoder->base.id, encoder_id +
1679 copied)) {
1680 ret = -EFAULT;
1681 goto out;
1682 }
1683 copied++;
1684 }
1685 } else {
1686 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1687 if (put_user(mode_group->id_list[i],
1688 encoder_id + copied)) {
1689 ret = -EFAULT;
1690 goto out;
1691 }
1692 copied++;
1693 }
1694
1695 }
1696 }
1697 card_res->count_encoders = encoder_count;
1698
1699 /* Connectors */
1700 if (card_res->count_connectors >= connector_count) {
1701 copied = 0;
1702 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1703 if (!mode_group) {
1704 list_for_each_entry(connector,
1705 &dev->mode_config.connector_list,
1706 head) {
1707 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1708 connector->base.id,
1709 connector->name);
1710 if (put_user(connector->base.id,
1711 connector_id + copied)) {
1712 ret = -EFAULT;
1713 goto out;
1714 }
1715 copied++;
1716 }
1717 } else {
1718 int start = mode_group->num_crtcs +
1719 mode_group->num_encoders;
1720 for (i = start; i < start + mode_group->num_connectors; i++) {
1721 if (put_user(mode_group->id_list[i],
1722 connector_id + copied)) {
1723 ret = -EFAULT;
1724 goto out;
1725 }
1726 copied++;
1727 }
1728 }
1729 }
1730 card_res->count_connectors = connector_count;
1731
1732 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
1733 card_res->count_connectors, card_res->count_encoders);
1734
1735 out:
1736 drm_modeset_unlock_all(dev);
1737 return ret;
1738 }
1739
1740 /**
1741 * drm_mode_getcrtc - get CRTC configuration
1742 * @dev: drm device for the ioctl
1743 * @data: data pointer for the ioctl
1744 * @file_priv: drm file for the ioctl call
1745 *
1746 * Construct a CRTC configuration structure to return to the user.
1747 *
1748 * Called by the user via ioctl.
1749 *
1750 * Returns:
1751 * Zero on success, errno on failure.
1752 */
1753 int drm_mode_getcrtc(struct drm_device *dev,
1754 void *data, struct drm_file *file_priv)
1755 {
1756 struct drm_mode_crtc *crtc_resp = data;
1757 struct drm_crtc *crtc;
1758 int ret = 0;
1759
1760 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1761 return -EINVAL;
1762
1763 drm_modeset_lock_all(dev);
1764
1765 crtc = drm_crtc_find(dev, crtc_resp->crtc_id);
1766 if (!crtc) {
1767 ret = -ENOENT;
1768 goto out;
1769 }
1770
1771 crtc_resp->x = crtc->x;
1772 crtc_resp->y = crtc->y;
1773 crtc_resp->gamma_size = crtc->gamma_size;
1774 if (crtc->primary->fb)
1775 crtc_resp->fb_id = crtc->primary->fb->base.id;
1776 else
1777 crtc_resp->fb_id = 0;
1778
1779 if (crtc->enabled) {
1780
1781 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1782 crtc_resp->mode_valid = 1;
1783
1784 } else {
1785 crtc_resp->mode_valid = 0;
1786 }
1787
1788 out:
1789 drm_modeset_unlock_all(dev);
1790 return ret;
1791 }
1792
1793 static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
1794 const struct drm_file *file_priv)
1795 {
1796 /*
1797 * If user-space hasn't configured the driver to expose the stereo 3D
1798 * modes, don't expose them.
1799 */
1800 if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
1801 return false;
1802
1803 return true;
1804 }
1805
1806 /**
1807 * drm_mode_getconnector - get connector configuration
1808 * @dev: drm device for the ioctl
1809 * @data: data pointer for the ioctl
1810 * @file_priv: drm file for the ioctl call
1811 *
1812 * Construct a connector configuration structure to return to the user.
1813 *
1814 * Called by the user via ioctl.
1815 *
1816 * Returns:
1817 * Zero on success, errno on failure.
1818 */
1819 int drm_mode_getconnector(struct drm_device *dev, void *data,
1820 struct drm_file *file_priv)
1821 {
1822 struct drm_mode_get_connector *out_resp = data;
1823 struct drm_connector *connector;
1824 struct drm_display_mode *mode;
1825 int mode_count = 0;
1826 int props_count = 0;
1827 int encoders_count = 0;
1828 int ret = 0;
1829 int copied = 0;
1830 int i;
1831 struct drm_mode_modeinfo u_mode;
1832 struct drm_mode_modeinfo __user *mode_ptr;
1833 uint32_t __user *prop_ptr;
1834 uint64_t __user *prop_values;
1835 uint32_t __user *encoder_ptr;
1836
1837 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1838 return -EINVAL;
1839
1840 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1841
1842 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
1843
1844 mutex_lock(&dev->mode_config.mutex);
1845
1846 connector = drm_connector_find(dev, out_resp->connector_id);
1847 if (!connector) {
1848 ret = -ENOENT;
1849 goto out;
1850 }
1851
1852 props_count = connector->properties.count;
1853
1854 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1855 if (connector->encoder_ids[i] != 0) {
1856 encoders_count++;
1857 }
1858 }
1859
1860 if (out_resp->count_modes == 0) {
1861 connector->funcs->fill_modes(connector,
1862 dev->mode_config.max_width,
1863 dev->mode_config.max_height);
1864 }
1865
1866 /* delayed so we get modes regardless of pre-fill_modes state */
1867 list_for_each_entry(mode, &connector->modes, head)
1868 if (drm_mode_expose_to_userspace(mode, file_priv))
1869 mode_count++;
1870
1871 out_resp->connector_id = connector->base.id;
1872 out_resp->connector_type = connector->connector_type;
1873 out_resp->connector_type_id = connector->connector_type_id;
1874 out_resp->mm_width = connector->display_info.width_mm;
1875 out_resp->mm_height = connector->display_info.height_mm;
1876 out_resp->subpixel = connector->display_info.subpixel_order;
1877 out_resp->connection = connector->status;
1878 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
1879 if (connector->encoder)
1880 out_resp->encoder_id = connector->encoder->base.id;
1881 else
1882 out_resp->encoder_id = 0;
1883 drm_modeset_unlock(&dev->mode_config.connection_mutex);
1884
1885 /*
1886 * This ioctl is called twice, once to determine how much space is
1887 * needed, and the 2nd time to fill it.
1888 */
1889 if ((out_resp->count_modes >= mode_count) && mode_count) {
1890 copied = 0;
1891 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
1892 list_for_each_entry(mode, &connector->modes, head) {
1893 if (!drm_mode_expose_to_userspace(mode, file_priv))
1894 continue;
1895
1896 drm_crtc_convert_to_umode(&u_mode, mode);
1897 if (copy_to_user(mode_ptr + copied,
1898 &u_mode, sizeof(u_mode))) {
1899 ret = -EFAULT;
1900 goto out;
1901 }
1902 copied++;
1903 }
1904 }
1905 out_resp->count_modes = mode_count;
1906
1907 if ((out_resp->count_props >= props_count) && props_count) {
1908 copied = 0;
1909 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
1910 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
1911 for (i = 0; i < connector->properties.count; i++) {
1912 if (put_user(connector->properties.ids[i],
1913 prop_ptr + copied)) {
1914 ret = -EFAULT;
1915 goto out;
1916 }
1917
1918 if (put_user(connector->properties.values[i],
1919 prop_values + copied)) {
1920 ret = -EFAULT;
1921 goto out;
1922 }
1923 copied++;
1924 }
1925 }
1926 out_resp->count_props = props_count;
1927
1928 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1929 copied = 0;
1930 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
1931 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1932 if (connector->encoder_ids[i] != 0) {
1933 if (put_user(connector->encoder_ids[i],
1934 encoder_ptr + copied)) {
1935 ret = -EFAULT;
1936 goto out;
1937 }
1938 copied++;
1939 }
1940 }
1941 }
1942 out_resp->count_encoders = encoders_count;
1943
1944 out:
1945 mutex_unlock(&dev->mode_config.mutex);
1946
1947 return ret;
1948 }
1949
1950 /**
1951 * drm_mode_getencoder - get encoder configuration
1952 * @dev: drm device for the ioctl
1953 * @data: data pointer for the ioctl
1954 * @file_priv: drm file for the ioctl call
1955 *
1956 * Construct a encoder configuration structure to return to the user.
1957 *
1958 * Called by the user via ioctl.
1959 *
1960 * Returns:
1961 * Zero on success, errno on failure.
1962 */
1963 int drm_mode_getencoder(struct drm_device *dev, void *data,
1964 struct drm_file *file_priv)
1965 {
1966 struct drm_mode_get_encoder *enc_resp = data;
1967 struct drm_encoder *encoder;
1968 int ret = 0;
1969
1970 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1971 return -EINVAL;
1972
1973 drm_modeset_lock_all(dev);
1974 encoder = drm_encoder_find(dev, enc_resp->encoder_id);
1975 if (!encoder) {
1976 ret = -ENOENT;
1977 goto out;
1978 }
1979
1980 if (encoder->crtc)
1981 enc_resp->crtc_id = encoder->crtc->base.id;
1982 else
1983 enc_resp->crtc_id = 0;
1984 enc_resp->encoder_type = encoder->encoder_type;
1985 enc_resp->encoder_id = encoder->base.id;
1986 enc_resp->possible_crtcs = encoder->possible_crtcs;
1987 enc_resp->possible_clones = encoder->possible_clones;
1988
1989 out:
1990 drm_modeset_unlock_all(dev);
1991 return ret;
1992 }
1993
1994 /**
1995 * drm_mode_getplane_res - enumerate all plane resources
1996 * @dev: DRM device
1997 * @data: ioctl data
1998 * @file_priv: DRM file info
1999 *
2000 * Construct a list of plane ids to return to the user.
2001 *
2002 * Called by the user via ioctl.
2003 *
2004 * Returns:
2005 * Zero on success, errno on failure.
2006 */
2007 int drm_mode_getplane_res(struct drm_device *dev, void *data,
2008 struct drm_file *file_priv)
2009 {
2010 struct drm_mode_get_plane_res *plane_resp = data;
2011 struct drm_mode_config *config;
2012 struct drm_plane *plane;
2013 uint32_t __user *plane_ptr;
2014 int copied = 0, ret = 0;
2015 unsigned num_planes;
2016
2017 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2018 return -EINVAL;
2019
2020 drm_modeset_lock_all(dev);
2021 config = &dev->mode_config;
2022
2023 if (file_priv->universal_planes)
2024 num_planes = config->num_total_plane;
2025 else
2026 num_planes = config->num_overlay_plane;
2027
2028 /*
2029 * This ioctl is called twice, once to determine how much space is
2030 * needed, and the 2nd time to fill it.
2031 */
2032 if (num_planes &&
2033 (plane_resp->count_planes >= num_planes)) {
2034 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
2035
2036 list_for_each_entry(plane, &config->plane_list, head) {
2037 /*
2038 * Unless userspace set the 'universal planes'
2039 * capability bit, only advertise overlays.
2040 */
2041 if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
2042 !file_priv->universal_planes)
2043 continue;
2044
2045 if (put_user(plane->base.id, plane_ptr + copied)) {
2046 ret = -EFAULT;
2047 goto out;
2048 }
2049 copied++;
2050 }
2051 }
2052 plane_resp->count_planes = num_planes;
2053
2054 out:
2055 drm_modeset_unlock_all(dev);
2056 return ret;
2057 }
2058
2059 /**
2060 * drm_mode_getplane - get plane configuration
2061 * @dev: DRM device
2062 * @data: ioctl data
2063 * @file_priv: DRM file info
2064 *
2065 * Construct a plane configuration structure to return to the user.
2066 *
2067 * Called by the user via ioctl.
2068 *
2069 * Returns:
2070 * Zero on success, errno on failure.
2071 */
2072 int drm_mode_getplane(struct drm_device *dev, void *data,
2073 struct drm_file *file_priv)
2074 {
2075 struct drm_mode_get_plane *plane_resp = data;
2076 struct drm_plane *plane;
2077 uint32_t __user *format_ptr;
2078 int ret = 0;
2079
2080 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2081 return -EINVAL;
2082
2083 drm_modeset_lock_all(dev);
2084 plane = drm_plane_find(dev, plane_resp->plane_id);
2085 if (!plane) {
2086 ret = -ENOENT;
2087 goto out;
2088 }
2089
2090 if (plane->crtc)
2091 plane_resp->crtc_id = plane->crtc->base.id;
2092 else
2093 plane_resp->crtc_id = 0;
2094
2095 if (plane->fb)
2096 plane_resp->fb_id = plane->fb->base.id;
2097 else
2098 plane_resp->fb_id = 0;
2099
2100 plane_resp->plane_id = plane->base.id;
2101 plane_resp->possible_crtcs = plane->possible_crtcs;
2102 plane_resp->gamma_size = 0;
2103
2104 /*
2105 * This ioctl is called twice, once to determine how much space is
2106 * needed, and the 2nd time to fill it.
2107 */
2108 if (plane->format_count &&
2109 (plane_resp->count_format_types >= plane->format_count)) {
2110 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
2111 if (copy_to_user(format_ptr,
2112 plane->format_types,
2113 sizeof(uint32_t) * plane->format_count)) {
2114 ret = -EFAULT;
2115 goto out;
2116 }
2117 }
2118 plane_resp->count_format_types = plane->format_count;
2119
2120 out:
2121 drm_modeset_unlock_all(dev);
2122 return ret;
2123 }
2124
2125 /*
2126 * setplane_internal - setplane handler for internal callers
2127 *
2128 * Note that we assume an extra reference has already been taken on fb. If the
2129 * update fails, this reference will be dropped before return; if it succeeds,
2130 * the previous framebuffer (if any) will be unreferenced instead.
2131 *
2132 * src_{x,y,w,h} are provided in 16.16 fixed point format
2133 */
2134 static int setplane_internal(struct drm_crtc *crtc,
2135 struct drm_plane *plane,
2136 struct drm_framebuffer *fb,
2137 int32_t crtc_x, int32_t crtc_y,
2138 uint32_t crtc_w, uint32_t crtc_h,
2139 /* src_{x,y,w,h} values are 16.16 fixed point */
2140 uint32_t src_x, uint32_t src_y,
2141 uint32_t src_w, uint32_t src_h)
2142 {
2143 struct drm_device *dev = crtc->dev;
2144 struct drm_framebuffer *old_fb = NULL;
2145 int ret = 0;
2146 unsigned int fb_width, fb_height;
2147 int i;
2148
2149 /* No fb means shut it down */
2150 if (!fb) {
2151 drm_modeset_lock_all(dev);
2152 old_fb = plane->fb;
2153 ret = plane->funcs->disable_plane(plane);
2154 if (!ret) {
2155 plane->crtc = NULL;
2156 plane->fb = NULL;
2157 } else {
2158 old_fb = NULL;
2159 }
2160 drm_modeset_unlock_all(dev);
2161 goto out;
2162 }
2163
2164 /* Check whether this plane is usable on this CRTC */
2165 if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
2166 DRM_DEBUG_KMS("Invalid crtc for plane\n");
2167 ret = -EINVAL;
2168 goto out;
2169 }
2170
2171 /* Check whether this plane supports the fb pixel format. */
2172 for (i = 0; i < plane->format_count; i++)
2173 if (fb->pixel_format == plane->format_types[i])
2174 break;
2175 if (i == plane->format_count) {
2176 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2177 drm_get_format_name(fb->pixel_format));
2178 ret = -EINVAL;
2179 goto out;
2180 }
2181
2182 fb_width = fb->width << 16;
2183 fb_height = fb->height << 16;
2184
2185 /* Make sure source coordinates are inside the fb. */
2186 if (src_w > fb_width ||
2187 src_x > fb_width - src_w ||
2188 src_h > fb_height ||
2189 src_y > fb_height - src_h) {
2190 DRM_DEBUG_KMS("Invalid source coordinates "
2191 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
2192 src_w >> 16, ((src_w & 0xffff) * 15625) >> 10,
2193 src_h >> 16, ((src_h & 0xffff) * 15625) >> 10,
2194 src_x >> 16, ((src_x & 0xffff) * 15625) >> 10,
2195 src_y >> 16, ((src_y & 0xffff) * 15625) >> 10);
2196 ret = -ENOSPC;
2197 goto out;
2198 }
2199
2200 drm_modeset_lock_all(dev);
2201 old_fb = plane->fb;
2202 ret = plane->funcs->update_plane(plane, crtc, fb,
2203 crtc_x, crtc_y, crtc_w, crtc_h,
2204 src_x, src_y, src_w, src_h);
2205 if (!ret) {
2206 plane->crtc = crtc;
2207 plane->fb = fb;
2208 fb = NULL;
2209 } else {
2210 old_fb = NULL;
2211 }
2212 drm_modeset_unlock_all(dev);
2213
2214 out:
2215 if (fb)
2216 drm_framebuffer_unreference(fb);
2217 if (old_fb)
2218 drm_framebuffer_unreference(old_fb);
2219
2220 return ret;
2221
2222 }
2223
2224 /**
2225 * drm_mode_setplane - configure a plane's configuration
2226 * @dev: DRM device
2227 * @data: ioctl data*
2228 * @file_priv: DRM file info
2229 *
2230 * Set plane configuration, including placement, fb, scaling, and other factors.
2231 * Or pass a NULL fb to disable (planes may be disabled without providing a
2232 * valid crtc).
2233 *
2234 * Returns:
2235 * Zero on success, errno on failure.
2236 */
2237 int drm_mode_setplane(struct drm_device *dev, void *data,
2238 struct drm_file *file_priv)
2239 {
2240 struct drm_mode_set_plane *plane_req = data;
2241 struct drm_mode_object *obj;
2242 struct drm_plane *plane;
2243 struct drm_crtc *crtc = NULL;
2244 struct drm_framebuffer *fb = NULL;
2245
2246 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2247 return -EINVAL;
2248
2249 /* Give drivers some help against integer overflows */
2250 if (plane_req->crtc_w > INT_MAX ||
2251 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
2252 plane_req->crtc_h > INT_MAX ||
2253 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
2254 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
2255 plane_req->crtc_w, plane_req->crtc_h,
2256 plane_req->crtc_x, plane_req->crtc_y);
2257 return -ERANGE;
2258 }
2259
2260 /*
2261 * First, find the plane, crtc, and fb objects. If not available,
2262 * we don't bother to call the driver.
2263 */
2264 obj = drm_mode_object_find(dev, plane_req->plane_id,
2265 DRM_MODE_OBJECT_PLANE);
2266 if (!obj) {
2267 DRM_DEBUG_KMS("Unknown plane ID %d\n",
2268 plane_req->plane_id);
2269 return -ENOENT;
2270 }
2271 plane = obj_to_plane(obj);
2272
2273 if (plane_req->fb_id) {
2274 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
2275 if (!fb) {
2276 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
2277 plane_req->fb_id);
2278 return -ENOENT;
2279 }
2280
2281 obj = drm_mode_object_find(dev, plane_req->crtc_id,
2282 DRM_MODE_OBJECT_CRTC);
2283 if (!obj) {
2284 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
2285 plane_req->crtc_id);
2286 return -ENOENT;
2287 }
2288 crtc = obj_to_crtc(obj);
2289 }
2290
2291 return setplane_internal(crtc, plane, fb,
2292 plane_req->crtc_x, plane_req->crtc_y,
2293 plane_req->crtc_w, plane_req->crtc_h,
2294 plane_req->src_x, plane_req->src_y,
2295 plane_req->src_w, plane_req->src_h);
2296 }
2297
2298 /**
2299 * drm_mode_set_config_internal - helper to call ->set_config
2300 * @set: modeset config to set
2301 *
2302 * This is a little helper to wrap internal calls to the ->set_config driver
2303 * interface. The only thing it adds is correct refcounting dance.
2304 *
2305 * Returns:
2306 * Zero on success, errno on failure.
2307 */
2308 int drm_mode_set_config_internal(struct drm_mode_set *set)
2309 {
2310 struct drm_crtc *crtc = set->crtc;
2311 struct drm_framebuffer *fb;
2312 struct drm_crtc *tmp;
2313 int ret;
2314
2315 /*
2316 * NOTE: ->set_config can also disable other crtcs (if we steal all
2317 * connectors from it), hence we need to refcount the fbs across all
2318 * crtcs. Atomic modeset will have saner semantics ...
2319 */
2320 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head)
2321 tmp->old_fb = tmp->primary->fb;
2322
2323 fb = set->fb;
2324
2325 ret = crtc->funcs->set_config(set);
2326 if (ret == 0) {
2327 crtc->primary->crtc = crtc;
2328 crtc->primary->fb = fb;
2329 }
2330
2331 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
2332 if (tmp->primary->fb)
2333 drm_framebuffer_reference(tmp->primary->fb);
2334 if (tmp->old_fb)
2335 drm_framebuffer_unreference(tmp->old_fb);
2336 }
2337
2338 return ret;
2339 }
2340 EXPORT_SYMBOL(drm_mode_set_config_internal);
2341
2342 /**
2343 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
2344 * CRTC viewport
2345 * @crtc: CRTC that framebuffer will be displayed on
2346 * @x: x panning
2347 * @y: y panning
2348 * @mode: mode that framebuffer will be displayed under
2349 * @fb: framebuffer to check size of
2350 */
2351 int drm_crtc_check_viewport(const struct drm_crtc *crtc,
2352 int x, int y,
2353 const struct drm_display_mode *mode,
2354 const struct drm_framebuffer *fb)
2355
2356 {
2357 int hdisplay, vdisplay;
2358
2359 hdisplay = mode->hdisplay;
2360 vdisplay = mode->vdisplay;
2361
2362 if (drm_mode_is_stereo(mode)) {
2363 struct drm_display_mode adjusted = *mode;
2364
2365 drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE);
2366 hdisplay = adjusted.crtc_hdisplay;
2367 vdisplay = adjusted.crtc_vdisplay;
2368 }
2369
2370 if (crtc->invert_dimensions)
2371 swap(hdisplay, vdisplay);
2372
2373 if (hdisplay > fb->width ||
2374 vdisplay > fb->height ||
2375 x > fb->width - hdisplay ||
2376 y > fb->height - vdisplay) {
2377 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
2378 fb->width, fb->height, hdisplay, vdisplay, x, y,
2379 crtc->invert_dimensions ? " (inverted)" : "");
2380 return -ENOSPC;
2381 }
2382
2383 return 0;
2384 }
2385 EXPORT_SYMBOL(drm_crtc_check_viewport);
2386
2387 /**
2388 * drm_mode_setcrtc - set CRTC configuration
2389 * @dev: drm device for the ioctl
2390 * @data: data pointer for the ioctl
2391 * @file_priv: drm file for the ioctl call
2392 *
2393 * Build a new CRTC configuration based on user request.
2394 *
2395 * Called by the user via ioctl.
2396 *
2397 * Returns:
2398 * Zero on success, errno on failure.
2399 */
2400 int drm_mode_setcrtc(struct drm_device *dev, void *data,
2401 struct drm_file *file_priv)
2402 {
2403 struct drm_mode_config *config = &dev->mode_config;
2404 struct drm_mode_crtc *crtc_req = data;
2405 struct drm_crtc *crtc;
2406 struct drm_connector **connector_set = NULL, *connector;
2407 struct drm_framebuffer *fb = NULL;
2408 struct drm_display_mode *mode = NULL;
2409 struct drm_mode_set set;
2410 uint32_t __user *set_connectors_ptr;
2411 int ret;
2412 int i;
2413
2414 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2415 return -EINVAL;
2416
2417 /* For some reason crtc x/y offsets are signed internally. */
2418 if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
2419 return -ERANGE;
2420
2421 drm_modeset_lock_all(dev);
2422 crtc = drm_crtc_find(dev, crtc_req->crtc_id);
2423 if (!crtc) {
2424 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
2425 ret = -ENOENT;
2426 goto out;
2427 }
2428 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
2429
2430 if (crtc_req->mode_valid) {
2431 /* If we have a mode we need a framebuffer. */
2432 /* If we pass -1, set the mode with the currently bound fb */
2433 if (crtc_req->fb_id == -1) {
2434 if (!crtc->primary->fb) {
2435 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2436 ret = -EINVAL;
2437 goto out;
2438 }
2439 fb = crtc->primary->fb;
2440 /* Make refcounting symmetric with the lookup path. */
2441 drm_framebuffer_reference(fb);
2442 } else {
2443 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2444 if (!fb) {
2445 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2446 crtc_req->fb_id);
2447 ret = -ENOENT;
2448 goto out;
2449 }
2450 }
2451
2452 mode = drm_mode_create(dev);
2453 if (!mode) {
2454 ret = -ENOMEM;
2455 goto out;
2456 }
2457
2458 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
2459 if (ret) {
2460 DRM_DEBUG_KMS("Invalid mode\n");
2461 goto out;
2462 }
2463
2464 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
2465
2466 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
2467 mode, fb);
2468 if (ret)
2469 goto out;
2470
2471 }
2472
2473 if (crtc_req->count_connectors == 0 && mode) {
2474 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
2475 ret = -EINVAL;
2476 goto out;
2477 }
2478
2479 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
2480 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
2481 crtc_req->count_connectors);
2482 ret = -EINVAL;
2483 goto out;
2484 }
2485
2486 if (crtc_req->count_connectors > 0) {
2487 u32 out_id;
2488
2489 /* Avoid unbounded kernel memory allocation */
2490 if (crtc_req->count_connectors > config->num_connector) {
2491 ret = -EINVAL;
2492 goto out;
2493 }
2494
2495 connector_set = kmalloc(crtc_req->count_connectors *
2496 sizeof(struct drm_connector *),
2497 GFP_KERNEL);
2498 if (!connector_set) {
2499 ret = -ENOMEM;
2500 goto out;
2501 }
2502
2503 for (i = 0; i < crtc_req->count_connectors; i++) {
2504 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
2505 if (get_user(out_id, &set_connectors_ptr[i])) {
2506 ret = -EFAULT;
2507 goto out;
2508 }
2509
2510 connector = drm_connector_find(dev, out_id);
2511 if (!connector) {
2512 DRM_DEBUG_KMS("Connector id %d unknown\n",
2513 out_id);
2514 ret = -ENOENT;
2515 goto out;
2516 }
2517 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2518 connector->base.id,
2519 connector->name);
2520
2521 connector_set[i] = connector;
2522 }
2523 }
2524
2525 set.crtc = crtc;
2526 set.x = crtc_req->x;
2527 set.y = crtc_req->y;
2528 set.mode = mode;
2529 set.connectors = connector_set;
2530 set.num_connectors = crtc_req->count_connectors;
2531 set.fb = fb;
2532 ret = drm_mode_set_config_internal(&set);
2533
2534 out:
2535 if (fb)
2536 drm_framebuffer_unreference(fb);
2537
2538 kfree(connector_set);
2539 drm_mode_destroy(dev, mode);
2540 drm_modeset_unlock_all(dev);
2541 return ret;
2542 }
2543
2544 static int drm_mode_cursor_common(struct drm_device *dev,
2545 struct drm_mode_cursor2 *req,
2546 struct drm_file *file_priv)
2547 {
2548 struct drm_crtc *crtc;
2549 int ret = 0;
2550
2551 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2552 return -EINVAL;
2553
2554 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
2555 return -EINVAL;
2556
2557 crtc = drm_crtc_find(dev, req->crtc_id);
2558 if (!crtc) {
2559 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
2560 return -ENOENT;
2561 }
2562
2563 drm_modeset_lock(&crtc->mutex, NULL);
2564 if (req->flags & DRM_MODE_CURSOR_BO) {
2565 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
2566 ret = -ENXIO;
2567 goto out;
2568 }
2569 /* Turns off the cursor if handle is 0 */
2570 if (crtc->funcs->cursor_set2)
2571 ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
2572 req->width, req->height, req->hot_x, req->hot_y);
2573 else
2574 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2575 req->width, req->height);
2576 }
2577
2578 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2579 if (crtc->funcs->cursor_move) {
2580 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2581 } else {
2582 ret = -EFAULT;
2583 goto out;
2584 }
2585 }
2586 out:
2587 drm_modeset_unlock(&crtc->mutex);
2588
2589 return ret;
2590
2591 }
2592
2593
2594 /**
2595 * drm_mode_cursor_ioctl - set CRTC's cursor configuration
2596 * @dev: drm device for the ioctl
2597 * @data: data pointer for the ioctl
2598 * @file_priv: drm file for the ioctl call
2599 *
2600 * Set the cursor configuration based on user request.
2601 *
2602 * Called by the user via ioctl.
2603 *
2604 * Returns:
2605 * Zero on success, errno on failure.
2606 */
2607 int drm_mode_cursor_ioctl(struct drm_device *dev,
2608 void *data, struct drm_file *file_priv)
2609 {
2610 struct drm_mode_cursor *req = data;
2611 struct drm_mode_cursor2 new_req;
2612
2613 memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
2614 new_req.hot_x = new_req.hot_y = 0;
2615
2616 return drm_mode_cursor_common(dev, &new_req, file_priv);
2617 }
2618
2619 /**
2620 * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
2621 * @dev: drm device for the ioctl
2622 * @data: data pointer for the ioctl
2623 * @file_priv: drm file for the ioctl call
2624 *
2625 * Set the cursor configuration based on user request. This implements the 2nd
2626 * version of the cursor ioctl, which allows userspace to additionally specify
2627 * the hotspot of the pointer.
2628 *
2629 * Called by the user via ioctl.
2630 *
2631 * Returns:
2632 * Zero on success, errno on failure.
2633 */
2634 int drm_mode_cursor2_ioctl(struct drm_device *dev,
2635 void *data, struct drm_file *file_priv)
2636 {
2637 struct drm_mode_cursor2 *req = data;
2638 return drm_mode_cursor_common(dev, req, file_priv);
2639 }
2640
2641 /**
2642 * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
2643 * @bpp: bits per pixels
2644 * @depth: bit depth per pixel
2645 *
2646 * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
2647 * Useful in fbdev emulation code, since that deals in those values.
2648 */
2649 uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2650 {
2651 uint32_t fmt;
2652
2653 switch (bpp) {
2654 case 8:
2655 fmt = DRM_FORMAT_C8;
2656 break;
2657 case 16:
2658 if (depth == 15)
2659 fmt = DRM_FORMAT_XRGB1555;
2660 else
2661 fmt = DRM_FORMAT_RGB565;
2662 break;
2663 case 24:
2664 fmt = DRM_FORMAT_RGB888;
2665 break;
2666 case 32:
2667 if (depth == 24)
2668 fmt = DRM_FORMAT_XRGB8888;
2669 else if (depth == 30)
2670 fmt = DRM_FORMAT_XRGB2101010;
2671 else
2672 fmt = DRM_FORMAT_ARGB8888;
2673 break;
2674 default:
2675 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2676 fmt = DRM_FORMAT_XRGB8888;
2677 break;
2678 }
2679
2680 return fmt;
2681 }
2682 EXPORT_SYMBOL(drm_mode_legacy_fb_format);
2683
2684 /**
2685 * drm_mode_addfb - add an FB to the graphics configuration
2686 * @dev: drm device for the ioctl
2687 * @data: data pointer for the ioctl
2688 * @file_priv: drm file for the ioctl call
2689 *
2690 * Add a new FB to the specified CRTC, given a user request. This is the
2691 * original addfb ioclt which only supported RGB formats.
2692 *
2693 * Called by the user via ioctl.
2694 *
2695 * Returns:
2696 * Zero on success, errno on failure.
2697 */
2698 int drm_mode_addfb(struct drm_device *dev,
2699 void *data, struct drm_file *file_priv)
2700 {
2701 struct drm_mode_fb_cmd *or = data;
2702 struct drm_mode_fb_cmd2 r = {};
2703 struct drm_mode_config *config = &dev->mode_config;
2704 struct drm_framebuffer *fb;
2705 int ret = 0;
2706
2707 /* Use new struct with format internally */
2708 r.fb_id = or->fb_id;
2709 r.width = or->width;
2710 r.height = or->height;
2711 r.pitches[0] = or->pitch;
2712 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
2713 r.handles[0] = or->handle;
2714
2715 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2716 return -EINVAL;
2717
2718 if ((config->min_width > r.width) || (r.width > config->max_width))
2719 return -EINVAL;
2720
2721 if ((config->min_height > r.height) || (r.height > config->max_height))
2722 return -EINVAL;
2723
2724 fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2725 if (IS_ERR(fb)) {
2726 DRM_DEBUG_KMS("could not create framebuffer\n");
2727 return PTR_ERR(fb);
2728 }
2729
2730 mutex_lock(&file_priv->fbs_lock);
2731 or->fb_id = fb->base.id;
2732 list_add(&fb->filp_head, &file_priv->fbs);
2733 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
2734 mutex_unlock(&file_priv->fbs_lock);
2735
2736 return ret;
2737 }
2738
2739 static int format_check(const struct drm_mode_fb_cmd2 *r)
2740 {
2741 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2742
2743 switch (format) {
2744 case DRM_FORMAT_C8:
2745 case DRM_FORMAT_RGB332:
2746 case DRM_FORMAT_BGR233:
2747 case DRM_FORMAT_XRGB4444:
2748 case DRM_FORMAT_XBGR4444:
2749 case DRM_FORMAT_RGBX4444:
2750 case DRM_FORMAT_BGRX4444:
2751 case DRM_FORMAT_ARGB4444:
2752 case DRM_FORMAT_ABGR4444:
2753 case DRM_FORMAT_RGBA4444:
2754 case DRM_FORMAT_BGRA4444:
2755 case DRM_FORMAT_XRGB1555:
2756 case DRM_FORMAT_XBGR1555:
2757 case DRM_FORMAT_RGBX5551:
2758 case DRM_FORMAT_BGRX5551:
2759 case DRM_FORMAT_ARGB1555:
2760 case DRM_FORMAT_ABGR1555:
2761 case DRM_FORMAT_RGBA5551:
2762 case DRM_FORMAT_BGRA5551:
2763 case DRM_FORMAT_RGB565:
2764 case DRM_FORMAT_BGR565:
2765 case DRM_FORMAT_RGB888:
2766 case DRM_FORMAT_BGR888:
2767 case DRM_FORMAT_XRGB8888:
2768 case DRM_FORMAT_XBGR8888:
2769 case DRM_FORMAT_RGBX8888:
2770 case DRM_FORMAT_BGRX8888:
2771 case DRM_FORMAT_ARGB8888:
2772 case DRM_FORMAT_ABGR8888:
2773 case DRM_FORMAT_RGBA8888:
2774 case DRM_FORMAT_BGRA8888:
2775 case DRM_FORMAT_XRGB2101010:
2776 case DRM_FORMAT_XBGR2101010:
2777 case DRM_FORMAT_RGBX1010102:
2778 case DRM_FORMAT_BGRX1010102:
2779 case DRM_FORMAT_ARGB2101010:
2780 case DRM_FORMAT_ABGR2101010:
2781 case DRM_FORMAT_RGBA1010102:
2782 case DRM_FORMAT_BGRA1010102:
2783 case DRM_FORMAT_YUYV:
2784 case DRM_FORMAT_YVYU:
2785 case DRM_FORMAT_UYVY:
2786 case DRM_FORMAT_VYUY:
2787 case DRM_FORMAT_AYUV:
2788 case DRM_FORMAT_NV12:
2789 case DRM_FORMAT_NV21:
2790 case DRM_FORMAT_NV16:
2791 case DRM_FORMAT_NV61:
2792 case DRM_FORMAT_NV24:
2793 case DRM_FORMAT_NV42:
2794 case DRM_FORMAT_YUV410:
2795 case DRM_FORMAT_YVU410:
2796 case DRM_FORMAT_YUV411:
2797 case DRM_FORMAT_YVU411:
2798 case DRM_FORMAT_YUV420:
2799 case DRM_FORMAT_YVU420:
2800 case DRM_FORMAT_YUV422:
2801 case DRM_FORMAT_YVU422:
2802 case DRM_FORMAT_YUV444:
2803 case DRM_FORMAT_YVU444:
2804 return 0;
2805 default:
2806 DRM_DEBUG_KMS("invalid pixel format %s\n",
2807 drm_get_format_name(r->pixel_format));
2808 return -EINVAL;
2809 }
2810 }
2811
2812 static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
2813 {
2814 int ret, hsub, vsub, num_planes, i;
2815
2816 ret = format_check(r);
2817 if (ret) {
2818 DRM_DEBUG_KMS("bad framebuffer format %s\n",
2819 drm_get_format_name(r->pixel_format));
2820 return ret;
2821 }
2822
2823 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
2824 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
2825 num_planes = drm_format_num_planes(r->pixel_format);
2826
2827 if (r->width == 0 || r->width % hsub) {
2828 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
2829 return -EINVAL;
2830 }
2831
2832 if (r->height == 0 || r->height % vsub) {
2833 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
2834 return -EINVAL;
2835 }
2836
2837 for (i = 0; i < num_planes; i++) {
2838 unsigned int width = r->width / (i != 0 ? hsub : 1);
2839 unsigned int height = r->height / (i != 0 ? vsub : 1);
2840 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
2841
2842 if (!r->handles[i]) {
2843 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
2844 return -EINVAL;
2845 }
2846
2847 if ((uint64_t) width * cpp > UINT_MAX)
2848 return -ERANGE;
2849
2850 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
2851 return -ERANGE;
2852
2853 if (r->pitches[i] < width * cpp) {
2854 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
2855 return -EINVAL;
2856 }
2857 }
2858
2859 return 0;
2860 }
2861
2862 static struct drm_framebuffer *add_framebuffer_internal(struct drm_device *dev,
2863 struct drm_mode_fb_cmd2 *r,
2864 struct drm_file *file_priv)
2865 {
2866 struct drm_mode_config *config = &dev->mode_config;
2867 struct drm_framebuffer *fb;
2868 int ret;
2869
2870 if (r->flags & ~DRM_MODE_FB_INTERLACED) {
2871 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
2872 return ERR_PTR(-EINVAL);
2873 }
2874
2875 if ((config->min_width > r->width) || (r->width > config->max_width)) {
2876 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
2877 r->width, config->min_width, config->max_width);
2878 return ERR_PTR(-EINVAL);
2879 }
2880 if ((config->min_height > r->height) || (r->height > config->max_height)) {
2881 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
2882 r->height, config->min_height, config->max_height);
2883 return ERR_PTR(-EINVAL);
2884 }
2885
2886 ret = framebuffer_check(r);
2887 if (ret)
2888 return ERR_PTR(ret);
2889
2890 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
2891 if (IS_ERR(fb)) {
2892 DRM_DEBUG_KMS("could not create framebuffer\n");
2893 return fb;
2894 }
2895
2896 mutex_lock(&file_priv->fbs_lock);
2897 r->fb_id = fb->base.id;
2898 list_add(&fb->filp_head, &file_priv->fbs);
2899 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
2900 mutex_unlock(&file_priv->fbs_lock);
2901
2902 return fb;
2903 }
2904
2905 /**
2906 * drm_mode_addfb2 - add an FB to the graphics configuration
2907 * @dev: drm device for the ioctl
2908 * @data: data pointer for the ioctl
2909 * @file_priv: drm file for the ioctl call
2910 *
2911 * Add a new FB to the specified CRTC, given a user request with format. This is
2912 * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
2913 * and uses fourcc codes as pixel format specifiers.
2914 *
2915 * Called by the user via ioctl.
2916 *
2917 * Returns:
2918 * Zero on success, errno on failure.
2919 */
2920 int drm_mode_addfb2(struct drm_device *dev,
2921 void *data, struct drm_file *file_priv)
2922 {
2923 struct drm_framebuffer *fb;
2924
2925 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2926 return -EINVAL;
2927
2928 fb = add_framebuffer_internal(dev, data, file_priv);
2929 if (IS_ERR(fb))
2930 return PTR_ERR(fb);
2931
2932 return 0;
2933 }
2934
2935 /**
2936 * drm_mode_rmfb - remove an FB from the configuration
2937 * @dev: drm device for the ioctl
2938 * @data: data pointer for the ioctl
2939 * @file_priv: drm file for the ioctl call
2940 *
2941 * Remove the FB specified by the user.
2942 *
2943 * Called by the user via ioctl.
2944 *
2945 * Returns:
2946 * Zero on success, errno on failure.
2947 */
2948 int drm_mode_rmfb(struct drm_device *dev,
2949 void *data, struct drm_file *file_priv)
2950 {
2951 struct drm_framebuffer *fb = NULL;
2952 struct drm_framebuffer *fbl = NULL;
2953 uint32_t *id = data;
2954 int found = 0;
2955
2956 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2957 return -EINVAL;
2958
2959 mutex_lock(&file_priv->fbs_lock);
2960 mutex_lock(&dev->mode_config.fb_lock);
2961 fb = __drm_framebuffer_lookup(dev, *id);
2962 if (!fb)
2963 goto fail_lookup;
2964
2965 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2966 if (fb == fbl)
2967 found = 1;
2968 if (!found)
2969 goto fail_lookup;
2970
2971 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
2972 __drm_framebuffer_unregister(dev, fb);
2973
2974 list_del_init(&fb->filp_head);
2975 mutex_unlock(&dev->mode_config.fb_lock);
2976 mutex_unlock(&file_priv->fbs_lock);
2977
2978 drm_framebuffer_remove(fb);
2979
2980 return 0;
2981
2982 fail_lookup:
2983 mutex_unlock(&dev->mode_config.fb_lock);
2984 mutex_unlock(&file_priv->fbs_lock);
2985
2986 return -ENOENT;
2987 }
2988
2989 /**
2990 * drm_mode_getfb - get FB info
2991 * @dev: drm device for the ioctl
2992 * @data: data pointer for the ioctl
2993 * @file_priv: drm file for the ioctl call
2994 *
2995 * Lookup the FB given its ID and return info about it.
2996 *
2997 * Called by the user via ioctl.
2998 *
2999 * Returns:
3000 * Zero on success, errno on failure.
3001 */
3002 int drm_mode_getfb(struct drm_device *dev,
3003 void *data, struct drm_file *file_priv)
3004 {
3005 struct drm_mode_fb_cmd *r = data;
3006 struct drm_framebuffer *fb;
3007 int ret;
3008
3009 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3010 return -EINVAL;
3011
3012 fb = drm_framebuffer_lookup(dev, r->fb_id);
3013 if (!fb)
3014 return -ENOENT;
3015
3016 r->height = fb->height;
3017 r->width = fb->width;
3018 r->depth = fb->depth;
3019 r->bpp = fb->bits_per_pixel;
3020 r->pitch = fb->pitches[0];
3021 if (fb->funcs->create_handle) {
3022 if (file_priv->is_master || capable(CAP_SYS_ADMIN) ||
3023 drm_is_control_client(file_priv)) {
3024 ret = fb->funcs->create_handle(fb, file_priv,
3025 &r->handle);
3026 } else {
3027 /* GET_FB() is an unprivileged ioctl so we must not
3028 * return a buffer-handle to non-master processes! For
3029 * backwards-compatibility reasons, we cannot make
3030 * GET_FB() privileged, so just return an invalid handle
3031 * for non-masters. */
3032 r->handle = 0;
3033 ret = 0;
3034 }
3035 } else {
3036 ret = -ENODEV;
3037 }
3038
3039 drm_framebuffer_unreference(fb);
3040
3041 return ret;
3042 }
3043
3044 /**
3045 * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
3046 * @dev: drm device for the ioctl
3047 * @data: data pointer for the ioctl
3048 * @file_priv: drm file for the ioctl call
3049 *
3050 * Lookup the FB and flush out the damaged area supplied by userspace as a clip
3051 * rectangle list. Generic userspace which does frontbuffer rendering must call
3052 * this ioctl to flush out the changes on manual-update display outputs, e.g.
3053 * usb display-link, mipi manual update panels or edp panel self refresh modes.
3054 *
3055 * Modesetting drivers which always update the frontbuffer do not need to
3056 * implement the corresponding ->dirty framebuffer callback.
3057 *
3058 * Called by the user via ioctl.
3059 *
3060 * Returns:
3061 * Zero on success, errno on failure.
3062 */
3063 int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
3064 void *data, struct drm_file *file_priv)
3065 {
3066 struct drm_clip_rect __user *clips_ptr;
3067 struct drm_clip_rect *clips = NULL;
3068 struct drm_mode_fb_dirty_cmd *r = data;
3069 struct drm_framebuffer *fb;
3070 unsigned flags;
3071 int num_clips;
3072 int ret;
3073
3074 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3075 return -EINVAL;
3076
3077 fb = drm_framebuffer_lookup(dev, r->fb_id);
3078 if (!fb)
3079 return -ENOENT;
3080
3081 num_clips = r->num_clips;
3082 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
3083
3084 if (!num_clips != !clips_ptr) {
3085 ret = -EINVAL;
3086 goto out_err1;
3087 }
3088
3089 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
3090
3091 /* If userspace annotates copy, clips must come in pairs */
3092 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
3093 ret = -EINVAL;
3094 goto out_err1;
3095 }
3096
3097 if (num_clips && clips_ptr) {
3098 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
3099 ret = -EINVAL;
3100 goto out_err1;
3101 }
3102 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
3103 if (!clips) {
3104 ret = -ENOMEM;
3105 goto out_err1;
3106 }
3107
3108 ret = copy_from_user(clips, clips_ptr,
3109 num_clips * sizeof(*clips));
3110 if (ret) {
3111 ret = -EFAULT;
3112 goto out_err2;
3113 }
3114 }
3115
3116 if (fb->funcs->dirty) {
3117 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
3118 clips, num_clips);
3119 } else {
3120 ret = -ENOSYS;
3121 }
3122
3123 out_err2:
3124 kfree(clips);
3125 out_err1:
3126 drm_framebuffer_unreference(fb);
3127
3128 return ret;
3129 }
3130
3131
3132 /**
3133 * drm_fb_release - remove and free the FBs on this file
3134 * @priv: drm file for the ioctl
3135 *
3136 * Destroy all the FBs associated with @filp.
3137 *
3138 * Called by the user via ioctl.
3139 *
3140 * Returns:
3141 * Zero on success, errno on failure.
3142 */
3143 void drm_fb_release(struct drm_file *priv)
3144 {
3145 struct drm_device *dev = priv->minor->dev;
3146 struct drm_framebuffer *fb, *tfb;
3147
3148 mutex_lock(&priv->fbs_lock);
3149 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
3150
3151 mutex_lock(&dev->mode_config.fb_lock);
3152 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
3153 __drm_framebuffer_unregister(dev, fb);
3154 mutex_unlock(&dev->mode_config.fb_lock);
3155
3156 list_del_init(&fb->filp_head);
3157
3158 /* This will also drop the fpriv->fbs reference. */
3159 drm_framebuffer_remove(fb);
3160 }
3161 mutex_unlock(&priv->fbs_lock);
3162 }
3163
3164 /**
3165 * drm_property_create - create a new property type
3166 * @dev: drm device
3167 * @flags: flags specifying the property type
3168 * @name: name of the property
3169 * @num_values: number of pre-defined values
3170 *
3171 * This creates a new generic drm property which can then be attached to a drm
3172 * object with drm_object_attach_property. The returned property object must be
3173 * freed with drm_property_destroy.
3174 *
3175 * Returns:
3176 * A pointer to the newly created property on success, NULL on failure.
3177 */
3178 struct drm_property *drm_property_create(struct drm_device *dev, int flags,
3179 const char *name, int num_values)
3180 {
3181 struct drm_property *property = NULL;
3182 int ret;
3183
3184 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
3185 if (!property)
3186 return NULL;
3187
3188 property->dev = dev;
3189
3190 if (num_values) {
3191 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
3192 if (!property->values)
3193 goto fail;
3194 }
3195
3196 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
3197 if (ret)
3198 goto fail;
3199
3200 property->flags = flags;
3201 property->num_values = num_values;
3202 INIT_LIST_HEAD(&property->enum_blob_list);
3203
3204 if (name) {
3205 strncpy(property->name, name, DRM_PROP_NAME_LEN);
3206 property->name[DRM_PROP_NAME_LEN-1] = '\0';
3207 }
3208
3209 list_add_tail(&property->head, &dev->mode_config.property_list);
3210
3211 WARN_ON(!drm_property_type_valid(property));
3212
3213 return property;
3214 fail:
3215 kfree(property->values);
3216 kfree(property);
3217 return NULL;
3218 }
3219 EXPORT_SYMBOL(drm_property_create);
3220
3221 /**
3222 * drm_property_create - create a new enumeration property type
3223 * @dev: drm device
3224 * @flags: flags specifying the property type
3225 * @name: name of the property
3226 * @props: enumeration lists with property values
3227 * @num_values: number of pre-defined values
3228 *
3229 * This creates a new generic drm property which can then be attached to a drm
3230 * object with drm_object_attach_property. The returned property object must be
3231 * freed with drm_property_destroy.
3232 *
3233 * Userspace is only allowed to set one of the predefined values for enumeration
3234 * properties.
3235 *
3236 * Returns:
3237 * A pointer to the newly created property on success, NULL on failure.
3238 */
3239 struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
3240 const char *name,
3241 const struct drm_prop_enum_list *props,
3242 int num_values)
3243 {
3244 struct drm_property *property;
3245 int i, ret;
3246
3247 flags |= DRM_MODE_PROP_ENUM;
3248
3249 property = drm_property_create(dev, flags, name, num_values);
3250 if (!property)
3251 return NULL;
3252
3253 for (i = 0; i < num_values; i++) {
3254 ret = drm_property_add_enum(property, i,
3255 props[i].type,
3256 props[i].name);
3257 if (ret) {
3258 drm_property_destroy(dev, property);
3259 return NULL;
3260 }
3261 }
3262
3263 return property;
3264 }
3265 EXPORT_SYMBOL(drm_property_create_enum);
3266
3267 /**
3268 * drm_property_create - create a new bitmask property type
3269 * @dev: drm device
3270 * @flags: flags specifying the property type
3271 * @name: name of the property
3272 * @props: enumeration lists with property bitflags
3273 * @num_values: number of pre-defined values
3274 *
3275 * This creates a new generic drm property which can then be attached to a drm
3276 * object with drm_object_attach_property. The returned property object must be
3277 * freed with drm_property_destroy.
3278 *
3279 * Compared to plain enumeration properties userspace is allowed to set any
3280 * or'ed together combination of the predefined property bitflag values
3281 *
3282 * Returns:
3283 * A pointer to the newly created property on success, NULL on failure.
3284 */
3285 struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
3286 int flags, const char *name,
3287 const struct drm_prop_enum_list *props,
3288 int num_values)
3289 {
3290 struct drm_property *property;
3291 int i, ret;
3292
3293 flags |= DRM_MODE_PROP_BITMASK;
3294
3295 property = drm_property_create(dev, flags, name, num_values);
3296 if (!property)
3297 return NULL;
3298
3299 for (i = 0; i < num_values; i++) {
3300 ret = drm_property_add_enum(property, i,
3301 props[i].type,
3302 props[i].name);
3303 if (ret) {
3304 drm_property_destroy(dev, property);
3305 return NULL;
3306 }
3307 }
3308
3309 return property;
3310 }
3311 EXPORT_SYMBOL(drm_property_create_bitmask);
3312
3313 static struct drm_property *property_create_range(struct drm_device *dev,
3314 int flags, const char *name,
3315 uint64_t min, uint64_t max)
3316 {
3317 struct drm_property *property;
3318
3319 property = drm_property_create(dev, flags, name, 2);
3320 if (!property)
3321 return NULL;
3322
3323 property->values[0] = min;
3324 property->values[1] = max;
3325
3326 return property;
3327 }
3328
3329 /**
3330 * drm_property_create - create a new ranged property type
3331 * @dev: drm device
3332 * @flags: flags specifying the property type
3333 * @name: name of the property
3334 * @min: minimum value of the property
3335 * @max: maximum value of the property
3336 *
3337 * This creates a new generic drm property which can then be attached to a drm
3338 * object with drm_object_attach_property. The returned property object must be
3339 * freed with drm_property_destroy.
3340 *
3341 * Userspace is allowed to set any interger value in the (min, max) range
3342 * inclusive.
3343 *
3344 * Returns:
3345 * A pointer to the newly created property on success, NULL on failure.
3346 */
3347 struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
3348 const char *name,
3349 uint64_t min, uint64_t max)
3350 {
3351 return property_create_range(dev, DRM_MODE_PROP_RANGE | flags,
3352 name, min, max);
3353 }
3354 EXPORT_SYMBOL(drm_property_create_range);
3355
3356 struct drm_property *drm_property_create_signed_range(struct drm_device *dev,
3357 int flags, const char *name,
3358 int64_t min, int64_t max)
3359 {
3360 return property_create_range(dev, DRM_MODE_PROP_SIGNED_RANGE | flags,
3361 name, I642U64(min), I642U64(max));
3362 }
3363 EXPORT_SYMBOL(drm_property_create_signed_range);
3364
3365 struct drm_property *drm_property_create_object(struct drm_device *dev,
3366 int flags, const char *name, uint32_t type)
3367 {
3368 struct drm_property *property;
3369
3370 flags |= DRM_MODE_PROP_OBJECT;
3371
3372 property = drm_property_create(dev, flags, name, 1);
3373 if (!property)
3374 return NULL;
3375
3376 property->values[0] = type;
3377
3378 return property;
3379 }
3380 EXPORT_SYMBOL(drm_property_create_object);
3381
3382 /**
3383 * drm_property_add_enum - add a possible value to an enumeration property
3384 * @property: enumeration property to change
3385 * @index: index of the new enumeration
3386 * @value: value of the new enumeration
3387 * @name: symbolic name of the new enumeration
3388 *
3389 * This functions adds enumerations to a property.
3390 *
3391 * It's use is deprecated, drivers should use one of the more specific helpers
3392 * to directly create the property with all enumerations already attached.
3393 *
3394 * Returns:
3395 * Zero on success, error code on failure.
3396 */
3397 int drm_property_add_enum(struct drm_property *property, int index,
3398 uint64_t value, const char *name)
3399 {
3400 struct drm_property_enum *prop_enum;
3401
3402 if (!(drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3403 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)))
3404 return -EINVAL;
3405
3406 /*
3407 * Bitmask enum properties have the additional constraint of values
3408 * from 0 to 63
3409 */
3410 if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK) &&
3411 (value > 63))
3412 return -EINVAL;
3413
3414 if (!list_empty(&property->enum_blob_list)) {
3415 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3416 if (prop_enum->value == value) {
3417 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3418 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3419 return 0;
3420 }
3421 }
3422 }
3423
3424 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
3425 if (!prop_enum)
3426 return -ENOMEM;
3427
3428 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3429 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3430 prop_enum->value = value;
3431
3432 property->values[index] = value;
3433 list_add_tail(&prop_enum->head, &property->enum_blob_list);
3434 return 0;
3435 }
3436 EXPORT_SYMBOL(drm_property_add_enum);
3437
3438 /**
3439 * drm_property_destroy - destroy a drm property
3440 * @dev: drm device
3441 * @property: property to destry
3442 *
3443 * This function frees a property including any attached resources like
3444 * enumeration values.
3445 */
3446 void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
3447 {
3448 struct drm_property_enum *prop_enum, *pt;
3449
3450 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
3451 list_del(&prop_enum->head);
3452 kfree(prop_enum);
3453 }
3454
3455 if (property->num_values)
3456 kfree(property->values);
3457 drm_mode_object_put(dev, &property->base);
3458 list_del(&property->head);
3459 kfree(property);
3460 }
3461 EXPORT_SYMBOL(drm_property_destroy);
3462
3463 /**
3464 * drm_object_attach_property - attach a property to a modeset object
3465 * @obj: drm modeset object
3466 * @property: property to attach
3467 * @init_val: initial value of the property
3468 *
3469 * This attaches the given property to the modeset object with the given initial
3470 * value. Currently this function cannot fail since the properties are stored in
3471 * a statically sized array.
3472 */
3473 void drm_object_attach_property(struct drm_mode_object *obj,
3474 struct drm_property *property,
3475 uint64_t init_val)
3476 {
3477 int count = obj->properties->count;
3478
3479 if (count == DRM_OBJECT_MAX_PROPERTY) {
3480 WARN(1, "Failed to attach object property (type: 0x%x). Please "
3481 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
3482 "you see this message on the same object type.\n",
3483 obj->type);
3484 return;
3485 }
3486
3487 obj->properties->ids[count] = property->base.id;
3488 obj->properties->values[count] = init_val;
3489 obj->properties->count++;
3490 }
3491 EXPORT_SYMBOL(drm_object_attach_property);
3492
3493 /**
3494 * drm_object_property_set_value - set the value of a property
3495 * @obj: drm mode object to set property value for
3496 * @property: property to set
3497 * @val: value the property should be set to
3498 *
3499 * This functions sets a given property on a given object. This function only
3500 * changes the software state of the property, it does not call into the
3501 * driver's ->set_property callback.
3502 *
3503 * Returns:
3504 * Zero on success, error code on failure.
3505 */
3506 int drm_object_property_set_value(struct drm_mode_object *obj,
3507 struct drm_property *property, uint64_t val)
3508 {
3509 int i;
3510
3511 for (i = 0; i < obj->properties->count; i++) {
3512 if (obj->properties->ids[i] == property->base.id) {
3513 obj->properties->values[i] = val;
3514 return 0;
3515 }
3516 }
3517
3518 return -EINVAL;
3519 }
3520 EXPORT_SYMBOL(drm_object_property_set_value);
3521
3522 /**
3523 * drm_object_property_get_value - retrieve the value of a property
3524 * @obj: drm mode object to get property value from
3525 * @property: property to retrieve
3526 * @val: storage for the property value
3527 *
3528 * This function retrieves the softare state of the given property for the given
3529 * property. Since there is no driver callback to retrieve the current property
3530 * value this might be out of sync with the hardware, depending upon the driver
3531 * and property.
3532 *
3533 * Returns:
3534 * Zero on success, error code on failure.
3535 */
3536 int drm_object_property_get_value(struct drm_mode_object *obj,
3537 struct drm_property *property, uint64_t *val)
3538 {
3539 int i;
3540
3541 for (i = 0; i < obj->properties->count; i++) {
3542 if (obj->properties->ids[i] == property->base.id) {
3543 *val = obj->properties->values[i];
3544 return 0;
3545 }
3546 }
3547
3548 return -EINVAL;
3549 }
3550 EXPORT_SYMBOL(drm_object_property_get_value);
3551
3552 /**
3553 * drm_mode_getproperty_ioctl - get the current value of a connector's property
3554 * @dev: DRM device
3555 * @data: ioctl data
3556 * @file_priv: DRM file info
3557 *
3558 * This function retrieves the current value for an connectors's property.
3559 *
3560 * Called by the user via ioctl.
3561 *
3562 * Returns:
3563 * Zero on success, errno on failure.
3564 */
3565 int drm_mode_getproperty_ioctl(struct drm_device *dev,
3566 void *data, struct drm_file *file_priv)
3567 {
3568 struct drm_mode_get_property *out_resp = data;
3569 struct drm_property *property;
3570 int enum_count = 0;
3571 int blob_count = 0;
3572 int value_count = 0;
3573 int ret = 0, i;
3574 int copied;
3575 struct drm_property_enum *prop_enum;
3576 struct drm_mode_property_enum __user *enum_ptr;
3577 struct drm_property_blob *prop_blob;
3578 uint32_t __user *blob_id_ptr;
3579 uint64_t __user *values_ptr;
3580 uint32_t __user *blob_length_ptr;
3581
3582 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3583 return -EINVAL;
3584
3585 drm_modeset_lock_all(dev);
3586 property = drm_property_find(dev, out_resp->prop_id);
3587 if (!property) {
3588 ret = -ENOENT;
3589 goto done;
3590 }
3591
3592 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3593 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
3594 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
3595 enum_count++;
3596 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
3597 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
3598 blob_count++;
3599 }
3600
3601 value_count = property->num_values;
3602
3603 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
3604 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
3605 out_resp->flags = property->flags;
3606
3607 if ((out_resp->count_values >= value_count) && value_count) {
3608 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
3609 for (i = 0; i < value_count; i++) {
3610 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
3611 ret = -EFAULT;
3612 goto done;
3613 }
3614 }
3615 }
3616 out_resp->count_values = value_count;
3617
3618 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3619 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
3620 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
3621 copied = 0;
3622 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
3623 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3624
3625 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
3626 ret = -EFAULT;
3627 goto done;
3628 }
3629
3630 if (copy_to_user(&enum_ptr[copied].name,
3631 &prop_enum->name, DRM_PROP_NAME_LEN)) {
3632 ret = -EFAULT;
3633 goto done;
3634 }
3635 copied++;
3636 }
3637 }
3638 out_resp->count_enum_blobs = enum_count;
3639 }
3640
3641 if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
3642 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
3643 copied = 0;
3644 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
3645 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
3646
3647 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
3648 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
3649 ret = -EFAULT;
3650 goto done;
3651 }
3652
3653 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
3654 ret = -EFAULT;
3655 goto done;
3656 }
3657
3658 copied++;
3659 }
3660 }
3661 out_resp->count_enum_blobs = blob_count;
3662 }
3663 done:
3664 drm_modeset_unlock_all(dev);
3665 return ret;
3666 }
3667
3668 static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
3669 void *data)
3670 {
3671 struct drm_property_blob *blob;
3672 int ret;
3673
3674 if (!length || !data)
3675 return NULL;
3676
3677 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
3678 if (!blob)
3679 return NULL;
3680
3681 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
3682 if (ret) {
3683 kfree(blob);
3684 return NULL;
3685 }
3686
3687 blob->length = length;
3688
3689 memcpy(blob->data, data, length);
3690
3691 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
3692 return blob;
3693 }
3694
3695 static void drm_property_destroy_blob(struct drm_device *dev,
3696 struct drm_property_blob *blob)
3697 {
3698 drm_mode_object_put(dev, &blob->base);
3699 list_del(&blob->head);
3700 kfree(blob);
3701 }
3702
3703 /**
3704 * drm_mode_getblob_ioctl - get the contents of a blob property value
3705 * @dev: DRM device
3706 * @data: ioctl data
3707 * @file_priv: DRM file info
3708 *
3709 * This function retrieves the contents of a blob property. The value stored in
3710 * an object's blob property is just a normal modeset object id.
3711 *
3712 * Called by the user via ioctl.
3713 *
3714 * Returns:
3715 * Zero on success, errno on failure.
3716 */
3717 int drm_mode_getblob_ioctl(struct drm_device *dev,
3718 void *data, struct drm_file *file_priv)
3719 {
3720 struct drm_mode_get_blob *out_resp = data;
3721 struct drm_property_blob *blob;
3722 int ret = 0;
3723 void __user *blob_ptr;
3724
3725 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3726 return -EINVAL;
3727
3728 drm_modeset_lock_all(dev);
3729 blob = drm_property_blob_find(dev, out_resp->blob_id);
3730 if (!blob) {
3731 ret = -ENOENT;
3732 goto done;
3733 }
3734
3735 if (out_resp->length == blob->length) {
3736 blob_ptr = (void __user *)(unsigned long)out_resp->data;
3737 if (copy_to_user(blob_ptr, blob->data, blob->length)){
3738 ret = -EFAULT;
3739 goto done;
3740 }
3741 }
3742 out_resp->length = blob->length;
3743
3744 done:
3745 drm_modeset_unlock_all(dev);
3746 return ret;
3747 }
3748
3749 /**
3750 * drm_mode_connector_update_edid_property - update the edid property of a connector
3751 * @connector: drm connector
3752 * @edid: new value of the edid property
3753 *
3754 * This function creates a new blob modeset object and assigns its id to the
3755 * connector's edid property.
3756 *
3757 * Returns:
3758 * Zero on success, errno on failure.
3759 */
3760 int drm_mode_connector_update_edid_property(struct drm_connector *connector,
3761 struct edid *edid)
3762 {
3763 struct drm_device *dev = connector->dev;
3764 int ret, size;
3765
3766 if (connector->edid_blob_ptr)
3767 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
3768
3769 /* Delete edid, when there is none. */
3770 if (!edid) {
3771 connector->edid_blob_ptr = NULL;
3772 ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
3773 return ret;
3774 }
3775
3776 size = EDID_LENGTH * (1 + edid->extensions);
3777 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
3778 size, edid);
3779 if (!connector->edid_blob_ptr)
3780 return -EINVAL;
3781
3782 ret = drm_object_property_set_value(&connector->base,
3783 dev->mode_config.edid_property,
3784 connector->edid_blob_ptr->base.id);
3785
3786 return ret;
3787 }
3788 EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
3789
3790 static bool drm_property_change_is_valid(struct drm_property *property,
3791 uint64_t value)
3792 {
3793 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
3794 return false;
3795
3796 if (drm_property_type_is(property, DRM_MODE_PROP_RANGE)) {
3797 if (value < property->values[0] || value > property->values[1])
3798 return false;
3799 return true;
3800 } else if (drm_property_type_is(property, DRM_MODE_PROP_SIGNED_RANGE)) {
3801 int64_t svalue = U642I64(value);
3802 if (svalue < U642I64(property->values[0]) ||
3803 svalue > U642I64(property->values[1]))
3804 return false;
3805 return true;
3806 } else if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
3807 int i;
3808 uint64_t valid_mask = 0;
3809 for (i = 0; i < property->num_values; i++)
3810 valid_mask |= (1ULL << property->values[i]);
3811 return !(value & ~valid_mask);
3812 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
3813 /* Only the driver knows */
3814 return true;
3815 } else if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
3816 struct drm_mode_object *obj;
3817 /* a zero value for an object property translates to null: */
3818 if (value == 0)
3819 return true;
3820 /*
3821 * NOTE: use _object_find() directly to bypass restriction on
3822 * looking up refcnt'd objects (ie. fb's). For a refcnt'd
3823 * object this could race against object finalization, so it
3824 * simply tells us that the object *was* valid. Which is good
3825 * enough.
3826 */
3827 obj = _object_find(property->dev, value, property->values[0]);
3828 return obj != NULL;
3829 } else {
3830 int i;
3831 for (i = 0; i < property->num_values; i++)
3832 if (property->values[i] == value)
3833 return true;
3834 return false;
3835 }
3836 }
3837
3838 /**
3839 * drm_mode_connector_property_set_ioctl - set the current value of a connector property
3840 * @dev: DRM device
3841 * @data: ioctl data
3842 * @file_priv: DRM file info
3843 *
3844 * This function sets the current value for a connectors's property. It also
3845 * calls into a driver's ->set_property callback to update the hardware state
3846 *
3847 * Called by the user via ioctl.
3848 *
3849 * Returns:
3850 * Zero on success, errno on failure.
3851 */
3852 int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
3853 void *data, struct drm_file *file_priv)
3854 {
3855 struct drm_mode_connector_set_property *conn_set_prop = data;
3856 struct drm_mode_obj_set_property obj_set_prop = {
3857 .value = conn_set_prop->value,
3858 .prop_id = conn_set_prop->prop_id,
3859 .obj_id = conn_set_prop->connector_id,
3860 .obj_type = DRM_MODE_OBJECT_CONNECTOR
3861 };
3862
3863 /* It does all the locking and checking we need */
3864 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
3865 }
3866
3867 static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
3868 struct drm_property *property,
3869 uint64_t value)
3870 {
3871 int ret = -EINVAL;
3872 struct drm_connector *connector = obj_to_connector(obj);
3873
3874 /* Do DPMS ourselves */
3875 if (property == connector->dev->mode_config.dpms_property) {
3876 if (connector->funcs->dpms)
3877 (*connector->funcs->dpms)(connector, (int)value);
3878 ret = 0;
3879 } else if (connector->funcs->set_property)
3880 ret = connector->funcs->set_property(connector, property, value);
3881
3882 /* store the property value if successful */
3883 if (!ret)
3884 drm_object_property_set_value(&connector->base, property, value);
3885 return ret;
3886 }
3887
3888 static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
3889 struct drm_property *property,
3890 uint64_t value)
3891 {
3892 int ret = -EINVAL;
3893 struct drm_crtc *crtc = obj_to_crtc(obj);
3894
3895 if (crtc->funcs->set_property)
3896 ret = crtc->funcs->set_property(crtc, property, value);
3897 if (!ret)
3898 drm_object_property_set_value(obj, property, value);
3899
3900 return ret;
3901 }
3902
3903 static int drm_mode_plane_set_obj_prop(struct drm_mode_object *obj,
3904 struct drm_property *property,
3905 uint64_t value)
3906 {
3907 int ret = -EINVAL;
3908 struct drm_plane *plane = obj_to_plane(obj);
3909
3910 if (plane->funcs->set_property)
3911 ret = plane->funcs->set_property(plane, property, value);
3912 if (!ret)
3913 drm_object_property_set_value(obj, property, value);
3914
3915 return ret;
3916 }
3917
3918 /**
3919 * drm_mode_getproperty_ioctl - get the current value of a object's property
3920 * @dev: DRM device
3921 * @data: ioctl data
3922 * @file_priv: DRM file info
3923 *
3924 * This function retrieves the current value for an object's property. Compared
3925 * to the connector specific ioctl this one is extended to also work on crtc and
3926 * plane objects.
3927 *
3928 * Called by the user via ioctl.
3929 *
3930 * Returns:
3931 * Zero on success, errno on failure.
3932 */
3933 int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
3934 struct drm_file *file_priv)
3935 {
3936 struct drm_mode_obj_get_properties *arg = data;
3937 struct drm_mode_object *obj;
3938 int ret = 0;
3939 int i;
3940 int copied = 0;
3941 int props_count = 0;
3942 uint32_t __user *props_ptr;
3943 uint64_t __user *prop_values_ptr;
3944
3945 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3946 return -EINVAL;
3947
3948 drm_modeset_lock_all(dev);
3949
3950 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3951 if (!obj) {
3952 ret = -ENOENT;
3953 goto out;
3954 }
3955 if (!obj->properties) {
3956 ret = -EINVAL;
3957 goto out;
3958 }
3959
3960 props_count = obj->properties->count;
3961
3962 /* This ioctl is called twice, once to determine how much space is
3963 * needed, and the 2nd time to fill it. */
3964 if ((arg->count_props >= props_count) && props_count) {
3965 copied = 0;
3966 props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
3967 prop_values_ptr = (uint64_t __user *)(unsigned long)
3968 (arg->prop_values_ptr);
3969 for (i = 0; i < props_count; i++) {
3970 if (put_user(obj->properties->ids[i],
3971 props_ptr + copied)) {
3972 ret = -EFAULT;
3973 goto out;
3974 }
3975 if (put_user(obj->properties->values[i],
3976 prop_values_ptr + copied)) {
3977 ret = -EFAULT;
3978 goto out;
3979 }
3980 copied++;
3981 }
3982 }
3983 arg->count_props = props_count;
3984 out:
3985 drm_modeset_unlock_all(dev);
3986 return ret;
3987 }
3988
3989 /**
3990 * drm_mode_obj_set_property_ioctl - set the current value of an object's property
3991 * @dev: DRM device
3992 * @data: ioctl data
3993 * @file_priv: DRM file info
3994 *
3995 * This function sets the current value for an object's property. It also calls
3996 * into a driver's ->set_property callback to update the hardware state.
3997 * Compared to the connector specific ioctl this one is extended to also work on
3998 * crtc and plane objects.
3999 *
4000 * Called by the user via ioctl.
4001 *
4002 * Returns:
4003 * Zero on success, errno on failure.
4004 */
4005 int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
4006 struct drm_file *file_priv)
4007 {
4008 struct drm_mode_obj_set_property *arg = data;
4009 struct drm_mode_object *arg_obj;
4010 struct drm_mode_object *prop_obj;
4011 struct drm_property *property;
4012 int ret = -EINVAL;
4013 int i;
4014
4015 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4016 return -EINVAL;
4017
4018 drm_modeset_lock_all(dev);
4019
4020 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
4021 if (!arg_obj) {
4022 ret = -ENOENT;
4023 goto out;
4024 }
4025 if (!arg_obj->properties)
4026 goto out;
4027
4028 for (i = 0; i < arg_obj->properties->count; i++)
4029 if (arg_obj->properties->ids[i] == arg->prop_id)
4030 break;
4031
4032 if (i == arg_obj->properties->count)
4033 goto out;
4034
4035 prop_obj = drm_mode_object_find(dev, arg->prop_id,
4036 DRM_MODE_OBJECT_PROPERTY);
4037 if (!prop_obj) {
4038 ret = -ENOENT;
4039 goto out;
4040 }
4041 property = obj_to_property(prop_obj);
4042
4043 if (!drm_property_change_is_valid(property, arg->value))
4044 goto out;
4045
4046 switch (arg_obj->type) {
4047 case DRM_MODE_OBJECT_CONNECTOR:
4048 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
4049 arg->value);
4050 break;
4051 case DRM_MODE_OBJECT_CRTC:
4052 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
4053 break;
4054 case DRM_MODE_OBJECT_PLANE:
4055 ret = drm_mode_plane_set_obj_prop(arg_obj, property, arg->value);
4056 break;
4057 }
4058
4059 out:
4060 drm_modeset_unlock_all(dev);
4061 return ret;
4062 }
4063
4064 /**
4065 * drm_mode_connector_attach_encoder - attach a connector to an encoder
4066 * @connector: connector to attach
4067 * @encoder: encoder to attach @connector to
4068 *
4069 * This function links up a connector to an encoder. Note that the routing
4070 * restrictions between encoders and crtcs are exposed to userspace through the
4071 * possible_clones and possible_crtcs bitmasks.
4072 *
4073 * Returns:
4074 * Zero on success, errno on failure.
4075 */
4076 int drm_mode_connector_attach_encoder(struct drm_connector *connector,
4077 struct drm_encoder *encoder)
4078 {
4079 int i;
4080
4081 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
4082 if (connector->encoder_ids[i] == 0) {
4083 connector->encoder_ids[i] = encoder->base.id;
4084 return 0;
4085 }
4086 }
4087 return -ENOMEM;
4088 }
4089 EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
4090
4091 /**
4092 * drm_mode_crtc_set_gamma_size - set the gamma table size
4093 * @crtc: CRTC to set the gamma table size for
4094 * @gamma_size: size of the gamma table
4095 *
4096 * Drivers which support gamma tables should set this to the supported gamma
4097 * table size when initializing the CRTC. Currently the drm core only supports a
4098 * fixed gamma table size.
4099 *
4100 * Returns:
4101 * Zero on success, errno on failure.
4102 */
4103 int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
4104 int gamma_size)
4105 {
4106 crtc->gamma_size = gamma_size;
4107
4108 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
4109 if (!crtc->gamma_store) {
4110 crtc->gamma_size = 0;
4111 return -ENOMEM;
4112 }
4113
4114 return 0;
4115 }
4116 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
4117
4118 /**
4119 * drm_mode_gamma_set_ioctl - set the gamma table
4120 * @dev: DRM device
4121 * @data: ioctl data
4122 * @file_priv: DRM file info
4123 *
4124 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
4125 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
4126 *
4127 * Called by the user via ioctl.
4128 *
4129 * Returns:
4130 * Zero on success, errno on failure.
4131 */
4132 int drm_mode_gamma_set_ioctl(struct drm_device *dev,
4133 void *data, struct drm_file *file_priv)
4134 {
4135 struct drm_mode_crtc_lut *crtc_lut = data;
4136 struct drm_crtc *crtc;
4137 void *r_base, *g_base, *b_base;
4138 int size;
4139 int ret = 0;
4140
4141 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4142 return -EINVAL;
4143
4144 drm_modeset_lock_all(dev);
4145 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
4146 if (!crtc) {
4147 ret = -ENOENT;
4148 goto out;
4149 }
4150
4151 if (crtc->funcs->gamma_set == NULL) {
4152 ret = -ENOSYS;
4153 goto out;
4154 }
4155
4156 /* memcpy into gamma store */
4157 if (crtc_lut->gamma_size != crtc->gamma_size) {
4158 ret = -EINVAL;
4159 goto out;
4160 }
4161
4162 size = crtc_lut->gamma_size * (sizeof(uint16_t));
4163 r_base = crtc->gamma_store;
4164 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
4165 ret = -EFAULT;
4166 goto out;
4167 }
4168
4169 g_base = r_base + size;
4170 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
4171 ret = -EFAULT;
4172 goto out;
4173 }
4174
4175 b_base = g_base + size;
4176 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
4177 ret = -EFAULT;
4178 goto out;
4179 }
4180
4181 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
4182
4183 out:
4184 drm_modeset_unlock_all(dev);
4185 return ret;
4186
4187 }
4188
4189 /**
4190 * drm_mode_gamma_get_ioctl - get the gamma table
4191 * @dev: DRM device
4192 * @data: ioctl data
4193 * @file_priv: DRM file info
4194 *
4195 * Copy the current gamma table into the storage provided. This also provides
4196 * the gamma table size the driver expects, which can be used to size the
4197 * allocated storage.
4198 *
4199 * Called by the user via ioctl.
4200 *
4201 * Returns:
4202 * Zero on success, errno on failure.
4203 */
4204 int drm_mode_gamma_get_ioctl(struct drm_device *dev,
4205 void *data, struct drm_file *file_priv)
4206 {
4207 struct drm_mode_crtc_lut *crtc_lut = data;
4208 struct drm_crtc *crtc;
4209 void *r_base, *g_base, *b_base;
4210 int size;
4211 int ret = 0;
4212
4213 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4214 return -EINVAL;
4215
4216 drm_modeset_lock_all(dev);
4217 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
4218 if (!crtc) {
4219 ret = -ENOENT;
4220 goto out;
4221 }
4222
4223 /* memcpy into gamma store */
4224 if (crtc_lut->gamma_size != crtc->gamma_size) {
4225 ret = -EINVAL;
4226 goto out;
4227 }
4228
4229 size = crtc_lut->gamma_size * (sizeof(uint16_t));
4230 r_base = crtc->gamma_store;
4231 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
4232 ret = -EFAULT;
4233 goto out;
4234 }
4235
4236 g_base = r_base + size;
4237 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
4238 ret = -EFAULT;
4239 goto out;
4240 }
4241
4242 b_base = g_base + size;
4243 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
4244 ret = -EFAULT;
4245 goto out;
4246 }
4247 out:
4248 drm_modeset_unlock_all(dev);
4249 return ret;
4250 }
4251
4252 /**
4253 * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
4254 * @dev: DRM device
4255 * @data: ioctl data
4256 * @file_priv: DRM file info
4257 *
4258 * This schedules an asynchronous update on a given CRTC, called page flip.
4259 * Optionally a drm event is generated to signal the completion of the event.
4260 * Generic drivers cannot assume that a pageflip with changed framebuffer
4261 * properties (including driver specific metadata like tiling layout) will work,
4262 * but some drivers support e.g. pixel format changes through the pageflip
4263 * ioctl.
4264 *
4265 * Called by the user via ioctl.
4266 *
4267 * Returns:
4268 * Zero on success, errno on failure.
4269 */
4270 int drm_mode_page_flip_ioctl(struct drm_device *dev,
4271 void *data, struct drm_file *file_priv)
4272 {
4273 struct drm_mode_crtc_page_flip *page_flip = data;
4274 struct drm_crtc *crtc;
4275 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
4276 struct drm_pending_vblank_event *e = NULL;
4277 unsigned long flags;
4278 int ret = -EINVAL;
4279
4280 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
4281 page_flip->reserved != 0)
4282 return -EINVAL;
4283
4284 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
4285 return -EINVAL;
4286
4287 crtc = drm_crtc_find(dev, page_flip->crtc_id);
4288 if (!crtc)
4289 return -ENOENT;
4290
4291 drm_modeset_lock(&crtc->mutex, NULL);
4292 if (crtc->primary->fb == NULL) {
4293 /* The framebuffer is currently unbound, presumably
4294 * due to a hotplug event, that userspace has not
4295 * yet discovered.
4296 */
4297 ret = -EBUSY;
4298 goto out;
4299 }
4300
4301 if (crtc->funcs->page_flip == NULL)
4302 goto out;
4303
4304 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
4305 if (!fb) {
4306 ret = -ENOENT;
4307 goto out;
4308 }
4309
4310 ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
4311 if (ret)
4312 goto out;
4313
4314 if (crtc->primary->fb->pixel_format != fb->pixel_format) {
4315 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
4316 ret = -EINVAL;
4317 goto out;
4318 }
4319
4320 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
4321 ret = -ENOMEM;
4322 spin_lock_irqsave(&dev->event_lock, flags);
4323 if (file_priv->event_space < sizeof e->event) {
4324 spin_unlock_irqrestore(&dev->event_lock, flags);
4325 goto out;
4326 }
4327 file_priv->event_space -= sizeof e->event;
4328 spin_unlock_irqrestore(&dev->event_lock, flags);
4329
4330 e = kzalloc(sizeof *e, GFP_KERNEL);
4331 if (e == NULL) {
4332 spin_lock_irqsave(&dev->event_lock, flags);
4333 file_priv->event_space += sizeof e->event;
4334 spin_unlock_irqrestore(&dev->event_lock, flags);
4335 goto out;
4336 }
4337
4338 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
4339 e->event.base.length = sizeof e->event;
4340 e->event.user_data = page_flip->user_data;
4341 e->base.event = &e->event.base;
4342 e->base.file_priv = file_priv;
4343 e->base.destroy =
4344 (void (*) (struct drm_pending_event *)) kfree;
4345 }
4346
4347 old_fb = crtc->primary->fb;
4348 ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
4349 if (ret) {
4350 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
4351 spin_lock_irqsave(&dev->event_lock, flags);
4352 file_priv->event_space += sizeof e->event;
4353 spin_unlock_irqrestore(&dev->event_lock, flags);
4354 kfree(e);
4355 }
4356 /* Keep the old fb, don't unref it. */
4357 old_fb = NULL;
4358 } else {
4359 /*
4360 * Warn if the driver hasn't properly updated the crtc->fb
4361 * field to reflect that the new framebuffer is now used.
4362 * Failing to do so will screw with the reference counting
4363 * on framebuffers.
4364 */
4365 WARN_ON(crtc->primary->fb != fb);
4366 /* Unref only the old framebuffer. */
4367 fb = NULL;
4368 }
4369
4370 out:
4371 if (fb)
4372 drm_framebuffer_unreference(fb);
4373 if (old_fb)
4374 drm_framebuffer_unreference(old_fb);
4375 drm_modeset_unlock(&crtc->mutex);
4376
4377 return ret;
4378 }
4379
4380 /**
4381 * drm_mode_config_reset - call ->reset callbacks
4382 * @dev: drm device
4383 *
4384 * This functions calls all the crtc's, encoder's and connector's ->reset
4385 * callback. Drivers can use this in e.g. their driver load or resume code to
4386 * reset hardware and software state.
4387 */
4388 void drm_mode_config_reset(struct drm_device *dev)
4389 {
4390 struct drm_crtc *crtc;
4391 struct drm_encoder *encoder;
4392 struct drm_connector *connector;
4393
4394 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
4395 if (crtc->funcs->reset)
4396 crtc->funcs->reset(crtc);
4397
4398 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
4399 if (encoder->funcs->reset)
4400 encoder->funcs->reset(encoder);
4401
4402 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4403 connector->status = connector_status_unknown;
4404
4405 if (connector->funcs->reset)
4406 connector->funcs->reset(connector);
4407 }
4408 }
4409 EXPORT_SYMBOL(drm_mode_config_reset);
4410
4411 /**
4412 * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
4413 * @dev: DRM device
4414 * @data: ioctl data
4415 * @file_priv: DRM file info
4416 *
4417 * This creates a new dumb buffer in the driver's backing storage manager (GEM,
4418 * TTM or something else entirely) and returns the resulting buffer handle. This
4419 * handle can then be wrapped up into a framebuffer modeset object.
4420 *
4421 * Note that userspace is not allowed to use such objects for render
4422 * acceleration - drivers must create their own private ioctls for such a use
4423 * case.
4424 *
4425 * Called by the user via ioctl.
4426 *
4427 * Returns:
4428 * Zero on success, errno on failure.
4429 */
4430 int drm_mode_create_dumb_ioctl(struct drm_device *dev,
4431 void *data, struct drm_file *file_priv)
4432 {
4433 struct drm_mode_create_dumb *args = data;
4434 u32 cpp, stride, size;
4435
4436 if (!dev->driver->dumb_create)
4437 return -ENOSYS;
4438 if (!args->width || !args->height || !args->bpp)
4439 return -EINVAL;
4440
4441 /* overflow checks for 32bit size calculations */
4442 cpp = DIV_ROUND_UP(args->bpp, 8);
4443 if (cpp > 0xffffffffU / args->width)
4444 return -EINVAL;
4445 stride = cpp * args->width;
4446 if (args->height > 0xffffffffU / stride)
4447 return -EINVAL;
4448
4449 /* test for wrap-around */
4450 size = args->height * stride;
4451 if (PAGE_ALIGN(size) == 0)
4452 return -EINVAL;
4453
4454 return dev->driver->dumb_create(file_priv, dev, args);
4455 }
4456
4457 /**
4458 * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
4459 * @dev: DRM device
4460 * @data: ioctl data
4461 * @file_priv: DRM file info
4462 *
4463 * Allocate an offset in the drm device node's address space to be able to
4464 * memory map a dumb buffer.
4465 *
4466 * Called by the user via ioctl.
4467 *
4468 * Returns:
4469 * Zero on success, errno on failure.
4470 */
4471 int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
4472 void *data, struct drm_file *file_priv)
4473 {
4474 struct drm_mode_map_dumb *args = data;
4475
4476 /* call driver ioctl to get mmap offset */
4477 if (!dev->driver->dumb_map_offset)
4478 return -ENOSYS;
4479
4480 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
4481 }
4482
4483 /**
4484 * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
4485 * @dev: DRM device
4486 * @data: ioctl data
4487 * @file_priv: DRM file info
4488 *
4489 * This destroys the userspace handle for the given dumb backing storage buffer.
4490 * Since buffer objects must be reference counted in the kernel a buffer object
4491 * won't be immediately freed if a framebuffer modeset object still uses it.
4492 *
4493 * Called by the user via ioctl.
4494 *
4495 * Returns:
4496 * Zero on success, errno on failure.
4497 */
4498 int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
4499 void *data, struct drm_file *file_priv)
4500 {
4501 struct drm_mode_destroy_dumb *args = data;
4502
4503 if (!dev->driver->dumb_destroy)
4504 return -ENOSYS;
4505
4506 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
4507 }
4508
4509 /**
4510 * drm_fb_get_bpp_depth - get the bpp/depth values for format
4511 * @format: pixel format (DRM_FORMAT_*)
4512 * @depth: storage for the depth value
4513 * @bpp: storage for the bpp value
4514 *
4515 * This only supports RGB formats here for compat with code that doesn't use
4516 * pixel formats directly yet.
4517 */
4518 void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
4519 int *bpp)
4520 {
4521 switch (format) {
4522 case DRM_FORMAT_C8:
4523 case DRM_FORMAT_RGB332:
4524 case DRM_FORMAT_BGR233:
4525 *depth = 8;
4526 *bpp = 8;
4527 break;
4528 case DRM_FORMAT_XRGB1555:
4529 case DRM_FORMAT_XBGR1555:
4530 case DRM_FORMAT_RGBX5551:
4531 case DRM_FORMAT_BGRX5551:
4532 case DRM_FORMAT_ARGB1555:
4533 case DRM_FORMAT_ABGR1555:
4534 case DRM_FORMAT_RGBA5551:
4535 case DRM_FORMAT_BGRA5551:
4536 *depth = 15;
4537 *bpp = 16;
4538 break;
4539 case DRM_FORMAT_RGB565:
4540 case DRM_FORMAT_BGR565:
4541 *depth = 16;
4542 *bpp = 16;
4543 break;
4544 case DRM_FORMAT_RGB888:
4545 case DRM_FORMAT_BGR888:
4546 *depth = 24;
4547 *bpp = 24;
4548 break;
4549 case DRM_FORMAT_XRGB8888:
4550 case DRM_FORMAT_XBGR8888:
4551 case DRM_FORMAT_RGBX8888:
4552 case DRM_FORMAT_BGRX8888:
4553 *depth = 24;
4554 *bpp = 32;
4555 break;
4556 case DRM_FORMAT_XRGB2101010:
4557 case DRM_FORMAT_XBGR2101010:
4558 case DRM_FORMAT_RGBX1010102:
4559 case DRM_FORMAT_BGRX1010102:
4560 case DRM_FORMAT_ARGB2101010:
4561 case DRM_FORMAT_ABGR2101010:
4562 case DRM_FORMAT_RGBA1010102:
4563 case DRM_FORMAT_BGRA1010102:
4564 *depth = 30;
4565 *bpp = 32;
4566 break;
4567 case DRM_FORMAT_ARGB8888:
4568 case DRM_FORMAT_ABGR8888:
4569 case DRM_FORMAT_RGBA8888:
4570 case DRM_FORMAT_BGRA8888:
4571 *depth = 32;
4572 *bpp = 32;
4573 break;
4574 default:
4575 DRM_DEBUG_KMS("unsupported pixel format %s\n",
4576 drm_get_format_name(format));
4577 *depth = 0;
4578 *bpp = 0;
4579 break;
4580 }
4581 }
4582 EXPORT_SYMBOL(drm_fb_get_bpp_depth);
4583
4584 /**
4585 * drm_format_num_planes - get the number of planes for format
4586 * @format: pixel format (DRM_FORMAT_*)
4587 *
4588 * Returns:
4589 * The number of planes used by the specified pixel format.
4590 */
4591 int drm_format_num_planes(uint32_t format)
4592 {
4593 switch (format) {
4594 case DRM_FORMAT_YUV410:
4595 case DRM_FORMAT_YVU410:
4596 case DRM_FORMAT_YUV411:
4597 case DRM_FORMAT_YVU411:
4598 case DRM_FORMAT_YUV420:
4599 case DRM_FORMAT_YVU420:
4600 case DRM_FORMAT_YUV422:
4601 case DRM_FORMAT_YVU422:
4602 case DRM_FORMAT_YUV444:
4603 case DRM_FORMAT_YVU444:
4604 return 3;
4605 case DRM_FORMAT_NV12:
4606 case DRM_FORMAT_NV21:
4607 case DRM_FORMAT_NV16:
4608 case DRM_FORMAT_NV61:
4609 case DRM_FORMAT_NV24:
4610 case DRM_FORMAT_NV42:
4611 return 2;
4612 default:
4613 return 1;
4614 }
4615 }
4616 EXPORT_SYMBOL(drm_format_num_planes);
4617
4618 /**
4619 * drm_format_plane_cpp - determine the bytes per pixel value
4620 * @format: pixel format (DRM_FORMAT_*)
4621 * @plane: plane index
4622 *
4623 * Returns:
4624 * The bytes per pixel value for the specified plane.
4625 */
4626 int drm_format_plane_cpp(uint32_t format, int plane)
4627 {
4628 unsigned int depth;
4629 int bpp;
4630
4631 if (plane >= drm_format_num_planes(format))
4632 return 0;
4633
4634 switch (format) {
4635 case DRM_FORMAT_YUYV:
4636 case DRM_FORMAT_YVYU:
4637 case DRM_FORMAT_UYVY:
4638 case DRM_FORMAT_VYUY:
4639 return 2;
4640 case DRM_FORMAT_NV12:
4641 case DRM_FORMAT_NV21:
4642 case DRM_FORMAT_NV16:
4643 case DRM_FORMAT_NV61:
4644 case DRM_FORMAT_NV24:
4645 case DRM_FORMAT_NV42:
4646 return plane ? 2 : 1;
4647 case DRM_FORMAT_YUV410:
4648 case DRM_FORMAT_YVU410:
4649 case DRM_FORMAT_YUV411:
4650 case DRM_FORMAT_YVU411:
4651 case DRM_FORMAT_YUV420:
4652 case DRM_FORMAT_YVU420:
4653 case DRM_FORMAT_YUV422:
4654 case DRM_FORMAT_YVU422:
4655 case DRM_FORMAT_YUV444:
4656 case DRM_FORMAT_YVU444:
4657 return 1;
4658 default:
4659 drm_fb_get_bpp_depth(format, &depth, &bpp);
4660 return bpp >> 3;
4661 }
4662 }
4663 EXPORT_SYMBOL(drm_format_plane_cpp);
4664
4665 /**
4666 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
4667 * @format: pixel format (DRM_FORMAT_*)
4668 *
4669 * Returns:
4670 * The horizontal chroma subsampling factor for the
4671 * specified pixel format.
4672 */
4673 int drm_format_horz_chroma_subsampling(uint32_t format)
4674 {
4675 switch (format) {
4676 case DRM_FORMAT_YUV411:
4677 case DRM_FORMAT_YVU411:
4678 case DRM_FORMAT_YUV410:
4679 case DRM_FORMAT_YVU410:
4680 return 4;
4681 case DRM_FORMAT_YUYV:
4682 case DRM_FORMAT_YVYU:
4683 case DRM_FORMAT_UYVY:
4684 case DRM_FORMAT_VYUY:
4685 case DRM_FORMAT_NV12:
4686 case DRM_FORMAT_NV21:
4687 case DRM_FORMAT_NV16:
4688 case DRM_FORMAT_NV61:
4689 case DRM_FORMAT_YUV422:
4690 case DRM_FORMAT_YVU422:
4691 case DRM_FORMAT_YUV420:
4692 case DRM_FORMAT_YVU420:
4693 return 2;
4694 default:
4695 return 1;
4696 }
4697 }
4698 EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
4699
4700 /**
4701 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
4702 * @format: pixel format (DRM_FORMAT_*)
4703 *
4704 * Returns:
4705 * The vertical chroma subsampling factor for the
4706 * specified pixel format.
4707 */
4708 int drm_format_vert_chroma_subsampling(uint32_t format)
4709 {
4710 switch (format) {
4711 case DRM_FORMAT_YUV410:
4712 case DRM_FORMAT_YVU410:
4713 return 4;
4714 case DRM_FORMAT_YUV420:
4715 case DRM_FORMAT_YVU420:
4716 case DRM_FORMAT_NV12:
4717 case DRM_FORMAT_NV21:
4718 return 2;
4719 default:
4720 return 1;
4721 }
4722 }
4723 EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
4724
4725 /**
4726 * drm_mode_config_init - initialize DRM mode_configuration structure
4727 * @dev: DRM device
4728 *
4729 * Initialize @dev's mode_config structure, used for tracking the graphics
4730 * configuration of @dev.
4731 *
4732 * Since this initializes the modeset locks, no locking is possible. Which is no
4733 * problem, since this should happen single threaded at init time. It is the
4734 * driver's problem to ensure this guarantee.
4735 *
4736 */
4737 void drm_mode_config_init(struct drm_device *dev)
4738 {
4739 mutex_init(&dev->mode_config.mutex);
4740 drm_modeset_lock_init(&dev->mode_config.connection_mutex);
4741 mutex_init(&dev->mode_config.idr_mutex);
4742 mutex_init(&dev->mode_config.fb_lock);
4743 INIT_LIST_HEAD(&dev->mode_config.fb_list);
4744 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
4745 INIT_LIST_HEAD(&dev->mode_config.connector_list);
4746 INIT_LIST_HEAD(&dev->mode_config.bridge_list);
4747 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
4748 INIT_LIST_HEAD(&dev->mode_config.property_list);
4749 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
4750 INIT_LIST_HEAD(&dev->mode_config.plane_list);
4751 idr_init(&dev->mode_config.crtc_idr);
4752
4753 drm_modeset_lock_all(dev);
4754 drm_mode_create_standard_connector_properties(dev);
4755 drm_mode_create_standard_plane_properties(dev);
4756 drm_modeset_unlock_all(dev);
4757
4758 /* Just to be sure */
4759 dev->mode_config.num_fb = 0;
4760 dev->mode_config.num_connector = 0;
4761 dev->mode_config.num_crtc = 0;
4762 dev->mode_config.num_encoder = 0;
4763 dev->mode_config.num_overlay_plane = 0;
4764 dev->mode_config.num_total_plane = 0;
4765 }
4766 EXPORT_SYMBOL(drm_mode_config_init);
4767
4768 /**
4769 * drm_mode_config_cleanup - free up DRM mode_config info
4770 * @dev: DRM device
4771 *
4772 * Free up all the connectors and CRTCs associated with this DRM device, then
4773 * free up the framebuffers and associated buffer objects.
4774 *
4775 * Note that since this /should/ happen single-threaded at driver/device
4776 * teardown time, no locking is required. It's the driver's job to ensure that
4777 * this guarantee actually holds true.
4778 *
4779 * FIXME: cleanup any dangling user buffer objects too
4780 */
4781 void drm_mode_config_cleanup(struct drm_device *dev)
4782 {
4783 struct drm_connector *connector, *ot;
4784 struct drm_crtc *crtc, *ct;
4785 struct drm_encoder *encoder, *enct;
4786 struct drm_bridge *bridge, *brt;
4787 struct drm_framebuffer *fb, *fbt;
4788 struct drm_property *property, *pt;
4789 struct drm_property_blob *blob, *bt;
4790 struct drm_plane *plane, *plt;
4791
4792 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
4793 head) {
4794 encoder->funcs->destroy(encoder);
4795 }
4796
4797 list_for_each_entry_safe(bridge, brt,
4798 &dev->mode_config.bridge_list, head) {
4799 bridge->funcs->destroy(bridge);
4800 }
4801
4802 list_for_each_entry_safe(connector, ot,
4803 &dev->mode_config.connector_list, head) {
4804 connector->funcs->destroy(connector);
4805 }
4806
4807 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
4808 head) {
4809 drm_property_destroy(dev, property);
4810 }
4811
4812 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
4813 head) {
4814 drm_property_destroy_blob(dev, blob);
4815 }
4816
4817 /*
4818 * Single-threaded teardown context, so it's not required to grab the
4819 * fb_lock to protect against concurrent fb_list access. Contrary, it
4820 * would actually deadlock with the drm_framebuffer_cleanup function.
4821 *
4822 * Also, if there are any framebuffers left, that's a driver leak now,
4823 * so politely WARN about this.
4824 */
4825 WARN_ON(!list_empty(&dev->mode_config.fb_list));
4826 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
4827 drm_framebuffer_remove(fb);
4828 }
4829
4830 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
4831 head) {
4832 plane->funcs->destroy(plane);
4833 }
4834
4835 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
4836 crtc->funcs->destroy(crtc);
4837 }
4838
4839 idr_destroy(&dev->mode_config.crtc_idr);
4840 drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
4841 }
4842 EXPORT_SYMBOL(drm_mode_config_cleanup);
This page took 0.129503 seconds and 4 git commands to generate.