drm: check that ->set_config properly updates the fb
[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
41 /**
42 * drm_modeset_lock_all - take all modeset locks
43 * @dev: drm device
44 *
45 * This function takes all modeset locks, suitable where a more fine-grained
46 * scheme isn't (yet) implemented.
47 */
48 void drm_modeset_lock_all(struct drm_device *dev)
49 {
50 struct drm_crtc *crtc;
51
52 mutex_lock(&dev->mode_config.mutex);
53
54 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
55 mutex_lock_nest_lock(&crtc->mutex, &dev->mode_config.mutex);
56 }
57 EXPORT_SYMBOL(drm_modeset_lock_all);
58
59 /**
60 * drm_modeset_unlock_all - drop all modeset locks
61 * @dev: device
62 */
63 void drm_modeset_unlock_all(struct drm_device *dev)
64 {
65 struct drm_crtc *crtc;
66
67 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
68 mutex_unlock(&crtc->mutex);
69
70 mutex_unlock(&dev->mode_config.mutex);
71 }
72 EXPORT_SYMBOL(drm_modeset_unlock_all);
73
74 /**
75 * drm_warn_on_modeset_not_all_locked - check that all modeset locks are locked
76 * @dev: device
77 */
78 void drm_warn_on_modeset_not_all_locked(struct drm_device *dev)
79 {
80 struct drm_crtc *crtc;
81
82 /* Locking is currently fubar in the panic handler. */
83 if (oops_in_progress)
84 return;
85
86 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
87 WARN_ON(!mutex_is_locked(&crtc->mutex));
88
89 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
90 }
91 EXPORT_SYMBOL(drm_warn_on_modeset_not_all_locked);
92
93 /* Avoid boilerplate. I'm tired of typing. */
94 #define DRM_ENUM_NAME_FN(fnname, list) \
95 const char *fnname(int val) \
96 { \
97 int i; \
98 for (i = 0; i < ARRAY_SIZE(list); i++) { \
99 if (list[i].type == val) \
100 return list[i].name; \
101 } \
102 return "(unknown)"; \
103 }
104
105 /*
106 * Global properties
107 */
108 static const struct drm_prop_enum_list drm_dpms_enum_list[] =
109 { { DRM_MODE_DPMS_ON, "On" },
110 { DRM_MODE_DPMS_STANDBY, "Standby" },
111 { DRM_MODE_DPMS_SUSPEND, "Suspend" },
112 { DRM_MODE_DPMS_OFF, "Off" }
113 };
114
115 DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
116
117 /*
118 * Optional properties
119 */
120 static const struct drm_prop_enum_list drm_scaling_mode_enum_list[] =
121 {
122 { DRM_MODE_SCALE_NONE, "None" },
123 { DRM_MODE_SCALE_FULLSCREEN, "Full" },
124 { DRM_MODE_SCALE_CENTER, "Center" },
125 { DRM_MODE_SCALE_ASPECT, "Full aspect" },
126 };
127
128 static const struct drm_prop_enum_list drm_dithering_mode_enum_list[] =
129 {
130 { DRM_MODE_DITHERING_OFF, "Off" },
131 { DRM_MODE_DITHERING_ON, "On" },
132 { DRM_MODE_DITHERING_AUTO, "Automatic" },
133 };
134
135 /*
136 * Non-global properties, but "required" for certain connectors.
137 */
138 static const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] =
139 {
140 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
141 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
142 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
143 };
144
145 DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
146
147 static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] =
148 {
149 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
150 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
151 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
152 };
153
154 DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
155 drm_dvi_i_subconnector_enum_list)
156
157 static const struct drm_prop_enum_list drm_tv_select_enum_list[] =
158 {
159 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
160 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
161 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
162 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
163 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
164 };
165
166 DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
167
168 static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] =
169 {
170 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
171 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
172 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
173 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
174 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
175 };
176
177 DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
178 drm_tv_subconnector_enum_list)
179
180 static const struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
181 { DRM_MODE_DIRTY_OFF, "Off" },
182 { DRM_MODE_DIRTY_ON, "On" },
183 { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
184 };
185
186 struct drm_conn_prop_enum_list {
187 int type;
188 const char *name;
189 int count;
190 };
191
192 /*
193 * Connector and encoder types.
194 */
195 static struct drm_conn_prop_enum_list drm_connector_enum_list[] =
196 { { DRM_MODE_CONNECTOR_Unknown, "Unknown", 0 },
197 { DRM_MODE_CONNECTOR_VGA, "VGA", 0 },
198 { DRM_MODE_CONNECTOR_DVII, "DVI-I", 0 },
199 { DRM_MODE_CONNECTOR_DVID, "DVI-D", 0 },
200 { DRM_MODE_CONNECTOR_DVIA, "DVI-A", 0 },
201 { DRM_MODE_CONNECTOR_Composite, "Composite", 0 },
202 { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO", 0 },
203 { DRM_MODE_CONNECTOR_LVDS, "LVDS", 0 },
204 { DRM_MODE_CONNECTOR_Component, "Component", 0 },
205 { DRM_MODE_CONNECTOR_9PinDIN, "DIN", 0 },
206 { DRM_MODE_CONNECTOR_DisplayPort, "DP", 0 },
207 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A", 0 },
208 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B", 0 },
209 { DRM_MODE_CONNECTOR_TV, "TV", 0 },
210 { DRM_MODE_CONNECTOR_eDP, "eDP", 0 },
211 { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual", 0},
212 };
213
214 static const struct drm_prop_enum_list drm_encoder_enum_list[] =
215 { { DRM_MODE_ENCODER_NONE, "None" },
216 { DRM_MODE_ENCODER_DAC, "DAC" },
217 { DRM_MODE_ENCODER_TMDS, "TMDS" },
218 { DRM_MODE_ENCODER_LVDS, "LVDS" },
219 { DRM_MODE_ENCODER_TVDAC, "TV" },
220 { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
221 };
222
223 const char *drm_get_encoder_name(const struct drm_encoder *encoder)
224 {
225 static char buf[32];
226
227 snprintf(buf, 32, "%s-%d",
228 drm_encoder_enum_list[encoder->encoder_type].name,
229 encoder->base.id);
230 return buf;
231 }
232 EXPORT_SYMBOL(drm_get_encoder_name);
233
234 const char *drm_get_connector_name(const struct drm_connector *connector)
235 {
236 static char buf[32];
237
238 snprintf(buf, 32, "%s-%d",
239 drm_connector_enum_list[connector->connector_type].name,
240 connector->connector_type_id);
241 return buf;
242 }
243 EXPORT_SYMBOL(drm_get_connector_name);
244
245 const char *drm_get_connector_status_name(enum drm_connector_status status)
246 {
247 if (status == connector_status_connected)
248 return "connected";
249 else if (status == connector_status_disconnected)
250 return "disconnected";
251 else
252 return "unknown";
253 }
254 EXPORT_SYMBOL(drm_get_connector_status_name);
255
256 static char printable_char(int c)
257 {
258 return isascii(c) && isprint(c) ? c : '?';
259 }
260
261 const char *drm_get_format_name(uint32_t format)
262 {
263 static char buf[32];
264
265 snprintf(buf, sizeof(buf),
266 "%c%c%c%c %s-endian (0x%08x)",
267 printable_char(format & 0xff),
268 printable_char((format >> 8) & 0xff),
269 printable_char((format >> 16) & 0xff),
270 printable_char((format >> 24) & 0x7f),
271 format & DRM_FORMAT_BIG_ENDIAN ? "big" : "little",
272 format);
273
274 return buf;
275 }
276 EXPORT_SYMBOL(drm_get_format_name);
277
278 /**
279 * drm_mode_object_get - allocate a new modeset identifier
280 * @dev: DRM device
281 * @obj: object pointer, used to generate unique ID
282 * @obj_type: object type
283 *
284 * Create a unique identifier based on @ptr in @dev's identifier space. Used
285 * for tracking modes, CRTCs and connectors.
286 *
287 * RETURNS:
288 * New unique (relative to other objects in @dev) integer identifier for the
289 * object.
290 */
291 static int drm_mode_object_get(struct drm_device *dev,
292 struct drm_mode_object *obj, uint32_t obj_type)
293 {
294 int ret;
295
296 mutex_lock(&dev->mode_config.idr_mutex);
297 ret = idr_alloc(&dev->mode_config.crtc_idr, obj, 1, 0, GFP_KERNEL);
298 if (ret >= 0) {
299 /*
300 * Set up the object linking under the protection of the idr
301 * lock so that other users can't see inconsistent state.
302 */
303 obj->id = ret;
304 obj->type = obj_type;
305 }
306 mutex_unlock(&dev->mode_config.idr_mutex);
307
308 return ret < 0 ? ret : 0;
309 }
310
311 /**
312 * drm_mode_object_put - free a modeset identifer
313 * @dev: DRM device
314 * @object: object to free
315 *
316 * Free @id from @dev's unique identifier pool.
317 */
318 static void drm_mode_object_put(struct drm_device *dev,
319 struct drm_mode_object *object)
320 {
321 mutex_lock(&dev->mode_config.idr_mutex);
322 idr_remove(&dev->mode_config.crtc_idr, object->id);
323 mutex_unlock(&dev->mode_config.idr_mutex);
324 }
325
326 /**
327 * drm_mode_object_find - look up a drm object with static lifetime
328 * @dev: drm device
329 * @id: id of the mode object
330 * @type: type of the mode object
331 *
332 * Note that framebuffers cannot be looked up with this functions - since those
333 * are reference counted, they need special treatment.
334 */
335 struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
336 uint32_t id, uint32_t type)
337 {
338 struct drm_mode_object *obj = NULL;
339
340 /* Framebuffers are reference counted and need their own lookup
341 * function.*/
342 WARN_ON(type == DRM_MODE_OBJECT_FB);
343
344 mutex_lock(&dev->mode_config.idr_mutex);
345 obj = idr_find(&dev->mode_config.crtc_idr, id);
346 if (!obj || (obj->type != type) || (obj->id != id))
347 obj = NULL;
348 mutex_unlock(&dev->mode_config.idr_mutex);
349
350 return obj;
351 }
352 EXPORT_SYMBOL(drm_mode_object_find);
353
354 /**
355 * drm_framebuffer_init - initialize a framebuffer
356 * @dev: DRM device
357 * @fb: framebuffer to be initialized
358 * @funcs: ... with these functions
359 *
360 * Allocates an ID for the framebuffer's parent mode object, sets its mode
361 * functions & device file and adds it to the master fd list.
362 *
363 * IMPORTANT:
364 * This functions publishes the fb and makes it available for concurrent access
365 * by other users. Which means by this point the fb _must_ be fully set up -
366 * since all the fb attributes are invariant over its lifetime, no further
367 * locking but only correct reference counting is required.
368 *
369 * RETURNS:
370 * Zero on success, error code on failure.
371 */
372 int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
373 const struct drm_framebuffer_funcs *funcs)
374 {
375 int ret;
376
377 mutex_lock(&dev->mode_config.fb_lock);
378 kref_init(&fb->refcount);
379 INIT_LIST_HEAD(&fb->filp_head);
380 fb->dev = dev;
381 fb->funcs = funcs;
382
383 ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
384 if (ret)
385 goto out;
386
387 /* Grab the idr reference. */
388 drm_framebuffer_reference(fb);
389
390 dev->mode_config.num_fb++;
391 list_add(&fb->head, &dev->mode_config.fb_list);
392 out:
393 mutex_unlock(&dev->mode_config.fb_lock);
394
395 return 0;
396 }
397 EXPORT_SYMBOL(drm_framebuffer_init);
398
399 static void drm_framebuffer_free(struct kref *kref)
400 {
401 struct drm_framebuffer *fb =
402 container_of(kref, struct drm_framebuffer, refcount);
403 fb->funcs->destroy(fb);
404 }
405
406 static struct drm_framebuffer *__drm_framebuffer_lookup(struct drm_device *dev,
407 uint32_t id)
408 {
409 struct drm_mode_object *obj = NULL;
410 struct drm_framebuffer *fb;
411
412 mutex_lock(&dev->mode_config.idr_mutex);
413 obj = idr_find(&dev->mode_config.crtc_idr, id);
414 if (!obj || (obj->type != DRM_MODE_OBJECT_FB) || (obj->id != id))
415 fb = NULL;
416 else
417 fb = obj_to_fb(obj);
418 mutex_unlock(&dev->mode_config.idr_mutex);
419
420 return fb;
421 }
422
423 /**
424 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
425 * @dev: drm device
426 * @id: id of the fb object
427 *
428 * If successful, this grabs an additional reference to the framebuffer -
429 * callers need to make sure to eventually unreference the returned framebuffer
430 * again.
431 */
432 struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
433 uint32_t id)
434 {
435 struct drm_framebuffer *fb;
436
437 mutex_lock(&dev->mode_config.fb_lock);
438 fb = __drm_framebuffer_lookup(dev, id);
439 if (fb)
440 drm_framebuffer_reference(fb);
441 mutex_unlock(&dev->mode_config.fb_lock);
442
443 return fb;
444 }
445 EXPORT_SYMBOL(drm_framebuffer_lookup);
446
447 /**
448 * drm_framebuffer_unreference - unref a framebuffer
449 * @fb: framebuffer to unref
450 *
451 * This functions decrements the fb's refcount and frees it if it drops to zero.
452 */
453 void drm_framebuffer_unreference(struct drm_framebuffer *fb)
454 {
455 DRM_DEBUG("FB ID: %d\n", fb->base.id);
456 kref_put(&fb->refcount, drm_framebuffer_free);
457 }
458 EXPORT_SYMBOL(drm_framebuffer_unreference);
459
460 /**
461 * drm_framebuffer_reference - incr the fb refcnt
462 * @fb: framebuffer
463 */
464 void drm_framebuffer_reference(struct drm_framebuffer *fb)
465 {
466 DRM_DEBUG("FB ID: %d\n", fb->base.id);
467 kref_get(&fb->refcount);
468 }
469 EXPORT_SYMBOL(drm_framebuffer_reference);
470
471 static void drm_framebuffer_free_bug(struct kref *kref)
472 {
473 BUG();
474 }
475
476 static void __drm_framebuffer_unreference(struct drm_framebuffer *fb)
477 {
478 DRM_DEBUG("FB ID: %d\n", fb->base.id);
479 kref_put(&fb->refcount, drm_framebuffer_free_bug);
480 }
481
482 /* dev->mode_config.fb_lock must be held! */
483 static void __drm_framebuffer_unregister(struct drm_device *dev,
484 struct drm_framebuffer *fb)
485 {
486 mutex_lock(&dev->mode_config.idr_mutex);
487 idr_remove(&dev->mode_config.crtc_idr, fb->base.id);
488 mutex_unlock(&dev->mode_config.idr_mutex);
489
490 fb->base.id = 0;
491
492 __drm_framebuffer_unreference(fb);
493 }
494
495 /**
496 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
497 * @fb: fb to unregister
498 *
499 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
500 * those used for fbdev. Note that the caller must hold a reference of it's own,
501 * i.e. the object may not be destroyed through this call (since it'll lead to a
502 * locking inversion).
503 */
504 void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
505 {
506 struct drm_device *dev = fb->dev;
507
508 mutex_lock(&dev->mode_config.fb_lock);
509 /* Mark fb as reaped and drop idr ref. */
510 __drm_framebuffer_unregister(dev, fb);
511 mutex_unlock(&dev->mode_config.fb_lock);
512 }
513 EXPORT_SYMBOL(drm_framebuffer_unregister_private);
514
515 /**
516 * drm_framebuffer_cleanup - remove a framebuffer object
517 * @fb: framebuffer to remove
518 *
519 * Cleanup references to a user-created framebuffer. This function is intended
520 * to be used from the drivers ->destroy callback.
521 *
522 * Note that this function does not remove the fb from active usuage - if it is
523 * still used anywhere, hilarity can ensue since userspace could call getfb on
524 * the id and get back -EINVAL. Obviously no concern at driver unload time.
525 *
526 * Also, the framebuffer will not be removed from the lookup idr - for
527 * user-created framebuffers this will happen in in the rmfb ioctl. For
528 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
529 * drm_framebuffer_unregister_private.
530 */
531 void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
532 {
533 struct drm_device *dev = fb->dev;
534
535 mutex_lock(&dev->mode_config.fb_lock);
536 list_del(&fb->head);
537 dev->mode_config.num_fb--;
538 mutex_unlock(&dev->mode_config.fb_lock);
539 }
540 EXPORT_SYMBOL(drm_framebuffer_cleanup);
541
542 /**
543 * drm_framebuffer_remove - remove and unreference a framebuffer object
544 * @fb: framebuffer to remove
545 *
546 * Scans all the CRTCs and planes in @dev's mode_config. If they're
547 * using @fb, removes it, setting it to NULL. Then drops the reference to the
548 * passed-in framebuffer. Might take the modeset locks.
549 *
550 * Note that this function optimizes the cleanup away if the caller holds the
551 * last reference to the framebuffer. It is also guaranteed to not take the
552 * modeset locks in this case.
553 */
554 void drm_framebuffer_remove(struct drm_framebuffer *fb)
555 {
556 struct drm_device *dev = fb->dev;
557 struct drm_crtc *crtc;
558 struct drm_plane *plane;
559 struct drm_mode_set set;
560 int ret;
561
562 WARN_ON(!list_empty(&fb->filp_head));
563
564 /*
565 * drm ABI mandates that we remove any deleted framebuffers from active
566 * useage. But since most sane clients only remove framebuffers they no
567 * longer need, try to optimize this away.
568 *
569 * Since we're holding a reference ourselves, observing a refcount of 1
570 * means that we're the last holder and can skip it. Also, the refcount
571 * can never increase from 1 again, so we don't need any barriers or
572 * locks.
573 *
574 * Note that userspace could try to race with use and instate a new
575 * usage _after_ we've cleared all current ones. End result will be an
576 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
577 * in this manner.
578 */
579 if (atomic_read(&fb->refcount.refcount) > 1) {
580 drm_modeset_lock_all(dev);
581 /* remove from any CRTC */
582 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
583 if (crtc->fb == fb) {
584 /* should turn off the crtc */
585 memset(&set, 0, sizeof(struct drm_mode_set));
586 set.crtc = crtc;
587 set.fb = NULL;
588 ret = drm_mode_set_config_internal(&set);
589 if (ret)
590 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
591 }
592 }
593
594 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
595 if (plane->fb == fb)
596 drm_plane_force_disable(plane);
597 }
598 drm_modeset_unlock_all(dev);
599 }
600
601 drm_framebuffer_unreference(fb);
602 }
603 EXPORT_SYMBOL(drm_framebuffer_remove);
604
605 /**
606 * drm_crtc_init - Initialise a new CRTC object
607 * @dev: DRM device
608 * @crtc: CRTC object to init
609 * @funcs: callbacks for the new CRTC
610 *
611 * Inits a new object created as base part of a driver crtc object.
612 *
613 * RETURNS:
614 * Zero on success, error code on failure.
615 */
616 int drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
617 const struct drm_crtc_funcs *funcs)
618 {
619 int ret;
620
621 crtc->dev = dev;
622 crtc->funcs = funcs;
623 crtc->invert_dimensions = false;
624
625 drm_modeset_lock_all(dev);
626 mutex_init(&crtc->mutex);
627 mutex_lock_nest_lock(&crtc->mutex, &dev->mode_config.mutex);
628
629 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
630 if (ret)
631 goto out;
632
633 crtc->base.properties = &crtc->properties;
634
635 list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
636 dev->mode_config.num_crtc++;
637
638 out:
639 drm_modeset_unlock_all(dev);
640
641 return ret;
642 }
643 EXPORT_SYMBOL(drm_crtc_init);
644
645 /**
646 * drm_crtc_cleanup - Clean up the core crtc usage
647 * @crtc: CRTC to cleanup
648 *
649 * This function cleans up @crtc and removes it from the DRM mode setting
650 * core. Note that the function does *not* free the crtc structure itself,
651 * this is the responsibility of the caller.
652 */
653 void drm_crtc_cleanup(struct drm_crtc *crtc)
654 {
655 struct drm_device *dev = crtc->dev;
656
657 kfree(crtc->gamma_store);
658 crtc->gamma_store = NULL;
659
660 drm_mode_object_put(dev, &crtc->base);
661 list_del(&crtc->head);
662 dev->mode_config.num_crtc--;
663 }
664 EXPORT_SYMBOL(drm_crtc_cleanup);
665
666 /**
667 * drm_mode_probed_add - add a mode to a connector's probed mode list
668 * @connector: connector the new mode
669 * @mode: mode data
670 *
671 * Add @mode to @connector's mode list for later use.
672 */
673 void drm_mode_probed_add(struct drm_connector *connector,
674 struct drm_display_mode *mode)
675 {
676 list_add_tail(&mode->head, &connector->probed_modes);
677 }
678 EXPORT_SYMBOL(drm_mode_probed_add);
679
680 /**
681 * drm_mode_remove - remove and free a mode
682 * @connector: connector list to modify
683 * @mode: mode to remove
684 *
685 * Remove @mode from @connector's mode list, then free it.
686 */
687 void drm_mode_remove(struct drm_connector *connector,
688 struct drm_display_mode *mode)
689 {
690 list_del(&mode->head);
691 drm_mode_destroy(connector->dev, mode);
692 }
693 EXPORT_SYMBOL(drm_mode_remove);
694
695 /**
696 * drm_connector_init - Init a preallocated connector
697 * @dev: DRM device
698 * @connector: the connector to init
699 * @funcs: callbacks for this connector
700 * @connector_type: user visible type of the connector
701 *
702 * Initialises a preallocated connector. Connectors should be
703 * subclassed as part of driver connector objects.
704 *
705 * RETURNS:
706 * Zero on success, error code on failure.
707 */
708 int drm_connector_init(struct drm_device *dev,
709 struct drm_connector *connector,
710 const struct drm_connector_funcs *funcs,
711 int connector_type)
712 {
713 int ret;
714
715 drm_modeset_lock_all(dev);
716
717 ret = drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
718 if (ret)
719 goto out;
720
721 connector->base.properties = &connector->properties;
722 connector->dev = dev;
723 connector->funcs = funcs;
724 connector->connector_type = connector_type;
725 connector->connector_type_id =
726 ++drm_connector_enum_list[connector_type].count; /* TODO */
727 INIT_LIST_HEAD(&connector->probed_modes);
728 INIT_LIST_HEAD(&connector->modes);
729 connector->edid_blob_ptr = NULL;
730 connector->status = connector_status_unknown;
731
732 list_add_tail(&connector->head, &dev->mode_config.connector_list);
733 dev->mode_config.num_connector++;
734
735 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
736 drm_object_attach_property(&connector->base,
737 dev->mode_config.edid_property,
738 0);
739
740 drm_object_attach_property(&connector->base,
741 dev->mode_config.dpms_property, 0);
742
743 out:
744 drm_modeset_unlock_all(dev);
745
746 return ret;
747 }
748 EXPORT_SYMBOL(drm_connector_init);
749
750 /**
751 * drm_connector_cleanup - cleans up an initialised connector
752 * @connector: connector to cleanup
753 *
754 * Cleans up the connector but doesn't free the object.
755 */
756 void drm_connector_cleanup(struct drm_connector *connector)
757 {
758 struct drm_device *dev = connector->dev;
759 struct drm_display_mode *mode, *t;
760
761 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
762 drm_mode_remove(connector, mode);
763
764 list_for_each_entry_safe(mode, t, &connector->modes, head)
765 drm_mode_remove(connector, mode);
766
767 drm_mode_object_put(dev, &connector->base);
768 list_del(&connector->head);
769 dev->mode_config.num_connector--;
770 }
771 EXPORT_SYMBOL(drm_connector_cleanup);
772
773 void drm_connector_unplug_all(struct drm_device *dev)
774 {
775 struct drm_connector *connector;
776
777 /* taking the mode config mutex ends up in a clash with sysfs */
778 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
779 drm_sysfs_connector_remove(connector);
780
781 }
782 EXPORT_SYMBOL(drm_connector_unplug_all);
783
784 int drm_encoder_init(struct drm_device *dev,
785 struct drm_encoder *encoder,
786 const struct drm_encoder_funcs *funcs,
787 int encoder_type)
788 {
789 int ret;
790
791 drm_modeset_lock_all(dev);
792
793 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
794 if (ret)
795 goto out;
796
797 encoder->dev = dev;
798 encoder->encoder_type = encoder_type;
799 encoder->funcs = funcs;
800
801 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
802 dev->mode_config.num_encoder++;
803
804 out:
805 drm_modeset_unlock_all(dev);
806
807 return ret;
808 }
809 EXPORT_SYMBOL(drm_encoder_init);
810
811 void drm_encoder_cleanup(struct drm_encoder *encoder)
812 {
813 struct drm_device *dev = encoder->dev;
814 drm_modeset_lock_all(dev);
815 drm_mode_object_put(dev, &encoder->base);
816 list_del(&encoder->head);
817 dev->mode_config.num_encoder--;
818 drm_modeset_unlock_all(dev);
819 }
820 EXPORT_SYMBOL(drm_encoder_cleanup);
821
822 /**
823 * drm_plane_init - Initialise a new plane object
824 * @dev: DRM device
825 * @plane: plane object to init
826 * @possible_crtcs: bitmask of possible CRTCs
827 * @funcs: callbacks for the new plane
828 * @formats: array of supported formats (%DRM_FORMAT_*)
829 * @format_count: number of elements in @formats
830 * @priv: plane is private (hidden from userspace)?
831 *
832 * Inits a new object created as base part of a driver plane object.
833 *
834 * RETURNS:
835 * Zero on success, error code on failure.
836 */
837 int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
838 unsigned long possible_crtcs,
839 const struct drm_plane_funcs *funcs,
840 const uint32_t *formats, uint32_t format_count,
841 bool priv)
842 {
843 int ret;
844
845 drm_modeset_lock_all(dev);
846
847 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
848 if (ret)
849 goto out;
850
851 plane->base.properties = &plane->properties;
852 plane->dev = dev;
853 plane->funcs = funcs;
854 plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
855 GFP_KERNEL);
856 if (!plane->format_types) {
857 DRM_DEBUG_KMS("out of memory when allocating plane\n");
858 drm_mode_object_put(dev, &plane->base);
859 ret = -ENOMEM;
860 goto out;
861 }
862
863 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
864 plane->format_count = format_count;
865 plane->possible_crtcs = possible_crtcs;
866
867 /* private planes are not exposed to userspace, but depending on
868 * display hardware, might be convenient to allow sharing programming
869 * for the scanout engine with the crtc implementation.
870 */
871 if (!priv) {
872 list_add_tail(&plane->head, &dev->mode_config.plane_list);
873 dev->mode_config.num_plane++;
874 } else {
875 INIT_LIST_HEAD(&plane->head);
876 }
877
878 out:
879 drm_modeset_unlock_all(dev);
880
881 return ret;
882 }
883 EXPORT_SYMBOL(drm_plane_init);
884
885 /**
886 * drm_plane_cleanup - Clean up the core plane usage
887 * @plane: plane to cleanup
888 *
889 * This function cleans up @plane and removes it from the DRM mode setting
890 * core. Note that the function does *not* free the plane structure itself,
891 * this is the responsibility of the caller.
892 */
893 void drm_plane_cleanup(struct drm_plane *plane)
894 {
895 struct drm_device *dev = plane->dev;
896
897 drm_modeset_lock_all(dev);
898 kfree(plane->format_types);
899 drm_mode_object_put(dev, &plane->base);
900 /* if not added to a list, it must be a private plane */
901 if (!list_empty(&plane->head)) {
902 list_del(&plane->head);
903 dev->mode_config.num_plane--;
904 }
905 drm_modeset_unlock_all(dev);
906 }
907 EXPORT_SYMBOL(drm_plane_cleanup);
908
909 /**
910 * drm_plane_force_disable - Forcibly disable a plane
911 * @plane: plane to disable
912 *
913 * Forces the plane to be disabled.
914 *
915 * Used when the plane's current framebuffer is destroyed,
916 * and when restoring fbdev mode.
917 */
918 void drm_plane_force_disable(struct drm_plane *plane)
919 {
920 int ret;
921
922 if (!plane->fb)
923 return;
924
925 ret = plane->funcs->disable_plane(plane);
926 if (ret)
927 DRM_ERROR("failed to disable plane with busy fb\n");
928 /* disconnect the plane from the fb and crtc: */
929 __drm_framebuffer_unreference(plane->fb);
930 plane->fb = NULL;
931 plane->crtc = NULL;
932 }
933 EXPORT_SYMBOL(drm_plane_force_disable);
934
935 /**
936 * drm_mode_create - create a new display mode
937 * @dev: DRM device
938 *
939 * Create a new drm_display_mode, give it an ID, and return it.
940 *
941 * RETURNS:
942 * Pointer to new mode on success, NULL on error.
943 */
944 struct drm_display_mode *drm_mode_create(struct drm_device *dev)
945 {
946 struct drm_display_mode *nmode;
947
948 nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
949 if (!nmode)
950 return NULL;
951
952 if (drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE)) {
953 kfree(nmode);
954 return NULL;
955 }
956
957 return nmode;
958 }
959 EXPORT_SYMBOL(drm_mode_create);
960
961 /**
962 * drm_mode_destroy - remove a mode
963 * @dev: DRM device
964 * @mode: mode to remove
965 *
966 * Free @mode's unique identifier, then free it.
967 */
968 void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
969 {
970 if (!mode)
971 return;
972
973 drm_mode_object_put(dev, &mode->base);
974
975 kfree(mode);
976 }
977 EXPORT_SYMBOL(drm_mode_destroy);
978
979 static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
980 {
981 struct drm_property *edid;
982 struct drm_property *dpms;
983
984 /*
985 * Standard properties (apply to all connectors)
986 */
987 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
988 DRM_MODE_PROP_IMMUTABLE,
989 "EDID", 0);
990 dev->mode_config.edid_property = edid;
991
992 dpms = drm_property_create_enum(dev, 0,
993 "DPMS", drm_dpms_enum_list,
994 ARRAY_SIZE(drm_dpms_enum_list));
995 dev->mode_config.dpms_property = dpms;
996
997 return 0;
998 }
999
1000 /**
1001 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1002 * @dev: DRM device
1003 *
1004 * Called by a driver the first time a DVI-I connector is made.
1005 */
1006 int drm_mode_create_dvi_i_properties(struct drm_device *dev)
1007 {
1008 struct drm_property *dvi_i_selector;
1009 struct drm_property *dvi_i_subconnector;
1010
1011 if (dev->mode_config.dvi_i_select_subconnector_property)
1012 return 0;
1013
1014 dvi_i_selector =
1015 drm_property_create_enum(dev, 0,
1016 "select subconnector",
1017 drm_dvi_i_select_enum_list,
1018 ARRAY_SIZE(drm_dvi_i_select_enum_list));
1019 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
1020
1021 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1022 "subconnector",
1023 drm_dvi_i_subconnector_enum_list,
1024 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
1025 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
1026
1027 return 0;
1028 }
1029 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
1030
1031 /**
1032 * drm_create_tv_properties - create TV specific connector properties
1033 * @dev: DRM device
1034 * @num_modes: number of different TV formats (modes) supported
1035 * @modes: array of pointers to strings containing name of each format
1036 *
1037 * Called by a driver's TV initialization routine, this function creates
1038 * the TV specific connector properties for a given device. Caller is
1039 * responsible for allocating a list of format names and passing them to
1040 * this routine.
1041 */
1042 int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
1043 char *modes[])
1044 {
1045 struct drm_property *tv_selector;
1046 struct drm_property *tv_subconnector;
1047 int i;
1048
1049 if (dev->mode_config.tv_select_subconnector_property)
1050 return 0;
1051
1052 /*
1053 * Basic connector properties
1054 */
1055 tv_selector = drm_property_create_enum(dev, 0,
1056 "select subconnector",
1057 drm_tv_select_enum_list,
1058 ARRAY_SIZE(drm_tv_select_enum_list));
1059 dev->mode_config.tv_select_subconnector_property = tv_selector;
1060
1061 tv_subconnector =
1062 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1063 "subconnector",
1064 drm_tv_subconnector_enum_list,
1065 ARRAY_SIZE(drm_tv_subconnector_enum_list));
1066 dev->mode_config.tv_subconnector_property = tv_subconnector;
1067
1068 /*
1069 * Other, TV specific properties: margins & TV modes.
1070 */
1071 dev->mode_config.tv_left_margin_property =
1072 drm_property_create_range(dev, 0, "left margin", 0, 100);
1073
1074 dev->mode_config.tv_right_margin_property =
1075 drm_property_create_range(dev, 0, "right margin", 0, 100);
1076
1077 dev->mode_config.tv_top_margin_property =
1078 drm_property_create_range(dev, 0, "top margin", 0, 100);
1079
1080 dev->mode_config.tv_bottom_margin_property =
1081 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
1082
1083 dev->mode_config.tv_mode_property =
1084 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1085 "mode", num_modes);
1086 for (i = 0; i < num_modes; i++)
1087 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1088 i, modes[i]);
1089
1090 dev->mode_config.tv_brightness_property =
1091 drm_property_create_range(dev, 0, "brightness", 0, 100);
1092
1093 dev->mode_config.tv_contrast_property =
1094 drm_property_create_range(dev, 0, "contrast", 0, 100);
1095
1096 dev->mode_config.tv_flicker_reduction_property =
1097 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
1098
1099 dev->mode_config.tv_overscan_property =
1100 drm_property_create_range(dev, 0, "overscan", 0, 100);
1101
1102 dev->mode_config.tv_saturation_property =
1103 drm_property_create_range(dev, 0, "saturation", 0, 100);
1104
1105 dev->mode_config.tv_hue_property =
1106 drm_property_create_range(dev, 0, "hue", 0, 100);
1107
1108 return 0;
1109 }
1110 EXPORT_SYMBOL(drm_mode_create_tv_properties);
1111
1112 /**
1113 * drm_mode_create_scaling_mode_property - create scaling mode property
1114 * @dev: DRM device
1115 *
1116 * Called by a driver the first time it's needed, must be attached to desired
1117 * connectors.
1118 */
1119 int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1120 {
1121 struct drm_property *scaling_mode;
1122
1123 if (dev->mode_config.scaling_mode_property)
1124 return 0;
1125
1126 scaling_mode =
1127 drm_property_create_enum(dev, 0, "scaling mode",
1128 drm_scaling_mode_enum_list,
1129 ARRAY_SIZE(drm_scaling_mode_enum_list));
1130
1131 dev->mode_config.scaling_mode_property = scaling_mode;
1132
1133 return 0;
1134 }
1135 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1136
1137 /**
1138 * drm_mode_create_dithering_property - create dithering property
1139 * @dev: DRM device
1140 *
1141 * Called by a driver the first time it's needed, must be attached to desired
1142 * connectors.
1143 */
1144 int drm_mode_create_dithering_property(struct drm_device *dev)
1145 {
1146 struct drm_property *dithering_mode;
1147
1148 if (dev->mode_config.dithering_mode_property)
1149 return 0;
1150
1151 dithering_mode =
1152 drm_property_create_enum(dev, 0, "dithering",
1153 drm_dithering_mode_enum_list,
1154 ARRAY_SIZE(drm_dithering_mode_enum_list));
1155 dev->mode_config.dithering_mode_property = dithering_mode;
1156
1157 return 0;
1158 }
1159 EXPORT_SYMBOL(drm_mode_create_dithering_property);
1160
1161 /**
1162 * drm_mode_create_dirty_property - create dirty property
1163 * @dev: DRM device
1164 *
1165 * Called by a driver the first time it's needed, must be attached to desired
1166 * connectors.
1167 */
1168 int drm_mode_create_dirty_info_property(struct drm_device *dev)
1169 {
1170 struct drm_property *dirty_info;
1171
1172 if (dev->mode_config.dirty_info_property)
1173 return 0;
1174
1175 dirty_info =
1176 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1177 "dirty",
1178 drm_dirty_info_enum_list,
1179 ARRAY_SIZE(drm_dirty_info_enum_list));
1180 dev->mode_config.dirty_info_property = dirty_info;
1181
1182 return 0;
1183 }
1184 EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1185
1186 static int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
1187 {
1188 uint32_t total_objects = 0;
1189
1190 total_objects += dev->mode_config.num_crtc;
1191 total_objects += dev->mode_config.num_connector;
1192 total_objects += dev->mode_config.num_encoder;
1193
1194 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
1195 if (!group->id_list)
1196 return -ENOMEM;
1197
1198 group->num_crtcs = 0;
1199 group->num_connectors = 0;
1200 group->num_encoders = 0;
1201 return 0;
1202 }
1203
1204 int drm_mode_group_init_legacy_group(struct drm_device *dev,
1205 struct drm_mode_group *group)
1206 {
1207 struct drm_crtc *crtc;
1208 struct drm_encoder *encoder;
1209 struct drm_connector *connector;
1210 int ret;
1211
1212 if ((ret = drm_mode_group_init(dev, group)))
1213 return ret;
1214
1215 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
1216 group->id_list[group->num_crtcs++] = crtc->base.id;
1217
1218 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
1219 group->id_list[group->num_crtcs + group->num_encoders++] =
1220 encoder->base.id;
1221
1222 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1223 group->id_list[group->num_crtcs + group->num_encoders +
1224 group->num_connectors++] = connector->base.id;
1225
1226 return 0;
1227 }
1228 EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
1229
1230 /**
1231 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1232 * @out: drm_mode_modeinfo struct to return to the user
1233 * @in: drm_display_mode to use
1234 *
1235 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1236 * the user.
1237 */
1238 static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1239 const struct drm_display_mode *in)
1240 {
1241 WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1242 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1243 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1244 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1245 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1246 "timing values too large for mode info\n");
1247
1248 out->clock = in->clock;
1249 out->hdisplay = in->hdisplay;
1250 out->hsync_start = in->hsync_start;
1251 out->hsync_end = in->hsync_end;
1252 out->htotal = in->htotal;
1253 out->hskew = in->hskew;
1254 out->vdisplay = in->vdisplay;
1255 out->vsync_start = in->vsync_start;
1256 out->vsync_end = in->vsync_end;
1257 out->vtotal = in->vtotal;
1258 out->vscan = in->vscan;
1259 out->vrefresh = in->vrefresh;
1260 out->flags = in->flags;
1261 out->type = in->type;
1262 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1263 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1264 }
1265
1266 /**
1267 * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
1268 * @out: drm_display_mode to return to the user
1269 * @in: drm_mode_modeinfo to use
1270 *
1271 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1272 * the caller.
1273 *
1274 * RETURNS:
1275 * Zero on success, errno on failure.
1276 */
1277 static int drm_crtc_convert_umode(struct drm_display_mode *out,
1278 const struct drm_mode_modeinfo *in)
1279 {
1280 if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1281 return -ERANGE;
1282
1283 out->clock = in->clock;
1284 out->hdisplay = in->hdisplay;
1285 out->hsync_start = in->hsync_start;
1286 out->hsync_end = in->hsync_end;
1287 out->htotal = in->htotal;
1288 out->hskew = in->hskew;
1289 out->vdisplay = in->vdisplay;
1290 out->vsync_start = in->vsync_start;
1291 out->vsync_end = in->vsync_end;
1292 out->vtotal = in->vtotal;
1293 out->vscan = in->vscan;
1294 out->vrefresh = in->vrefresh;
1295 out->flags = in->flags;
1296 out->type = in->type;
1297 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1298 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1299
1300 return 0;
1301 }
1302
1303 /**
1304 * drm_mode_getresources - get graphics configuration
1305 * @dev: drm device for the ioctl
1306 * @data: data pointer for the ioctl
1307 * @file_priv: drm file for the ioctl call
1308 *
1309 * Construct a set of configuration description structures and return
1310 * them to the user, including CRTC, connector and framebuffer configuration.
1311 *
1312 * Called by the user via ioctl.
1313 *
1314 * RETURNS:
1315 * Zero on success, errno on failure.
1316 */
1317 int drm_mode_getresources(struct drm_device *dev, void *data,
1318 struct drm_file *file_priv)
1319 {
1320 struct drm_mode_card_res *card_res = data;
1321 struct list_head *lh;
1322 struct drm_framebuffer *fb;
1323 struct drm_connector *connector;
1324 struct drm_crtc *crtc;
1325 struct drm_encoder *encoder;
1326 int ret = 0;
1327 int connector_count = 0;
1328 int crtc_count = 0;
1329 int fb_count = 0;
1330 int encoder_count = 0;
1331 int copied = 0, i;
1332 uint32_t __user *fb_id;
1333 uint32_t __user *crtc_id;
1334 uint32_t __user *connector_id;
1335 uint32_t __user *encoder_id;
1336 struct drm_mode_group *mode_group;
1337
1338 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1339 return -EINVAL;
1340
1341
1342 mutex_lock(&file_priv->fbs_lock);
1343 /*
1344 * For the non-control nodes we need to limit the list of resources
1345 * by IDs in the group list for this node
1346 */
1347 list_for_each(lh, &file_priv->fbs)
1348 fb_count++;
1349
1350 /* handle this in 4 parts */
1351 /* FBs */
1352 if (card_res->count_fbs >= fb_count) {
1353 copied = 0;
1354 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1355 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1356 if (put_user(fb->base.id, fb_id + copied)) {
1357 mutex_unlock(&file_priv->fbs_lock);
1358 return -EFAULT;
1359 }
1360 copied++;
1361 }
1362 }
1363 card_res->count_fbs = fb_count;
1364 mutex_unlock(&file_priv->fbs_lock);
1365
1366 drm_modeset_lock_all(dev);
1367 mode_group = &file_priv->master->minor->mode_group;
1368 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1369
1370 list_for_each(lh, &dev->mode_config.crtc_list)
1371 crtc_count++;
1372
1373 list_for_each(lh, &dev->mode_config.connector_list)
1374 connector_count++;
1375
1376 list_for_each(lh, &dev->mode_config.encoder_list)
1377 encoder_count++;
1378 } else {
1379
1380 crtc_count = mode_group->num_crtcs;
1381 connector_count = mode_group->num_connectors;
1382 encoder_count = mode_group->num_encoders;
1383 }
1384
1385 card_res->max_height = dev->mode_config.max_height;
1386 card_res->min_height = dev->mode_config.min_height;
1387 card_res->max_width = dev->mode_config.max_width;
1388 card_res->min_width = dev->mode_config.min_width;
1389
1390 /* CRTCs */
1391 if (card_res->count_crtcs >= crtc_count) {
1392 copied = 0;
1393 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1394 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1395 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1396 head) {
1397 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
1398 if (put_user(crtc->base.id, crtc_id + copied)) {
1399 ret = -EFAULT;
1400 goto out;
1401 }
1402 copied++;
1403 }
1404 } else {
1405 for (i = 0; i < mode_group->num_crtcs; i++) {
1406 if (put_user(mode_group->id_list[i],
1407 crtc_id + copied)) {
1408 ret = -EFAULT;
1409 goto out;
1410 }
1411 copied++;
1412 }
1413 }
1414 }
1415 card_res->count_crtcs = crtc_count;
1416
1417 /* Encoders */
1418 if (card_res->count_encoders >= encoder_count) {
1419 copied = 0;
1420 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1421 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1422 list_for_each_entry(encoder,
1423 &dev->mode_config.encoder_list,
1424 head) {
1425 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1426 drm_get_encoder_name(encoder));
1427 if (put_user(encoder->base.id, encoder_id +
1428 copied)) {
1429 ret = -EFAULT;
1430 goto out;
1431 }
1432 copied++;
1433 }
1434 } else {
1435 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1436 if (put_user(mode_group->id_list[i],
1437 encoder_id + copied)) {
1438 ret = -EFAULT;
1439 goto out;
1440 }
1441 copied++;
1442 }
1443
1444 }
1445 }
1446 card_res->count_encoders = encoder_count;
1447
1448 /* Connectors */
1449 if (card_res->count_connectors >= connector_count) {
1450 copied = 0;
1451 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1452 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1453 list_for_each_entry(connector,
1454 &dev->mode_config.connector_list,
1455 head) {
1456 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1457 connector->base.id,
1458 drm_get_connector_name(connector));
1459 if (put_user(connector->base.id,
1460 connector_id + copied)) {
1461 ret = -EFAULT;
1462 goto out;
1463 }
1464 copied++;
1465 }
1466 } else {
1467 int start = mode_group->num_crtcs +
1468 mode_group->num_encoders;
1469 for (i = start; i < start + mode_group->num_connectors; i++) {
1470 if (put_user(mode_group->id_list[i],
1471 connector_id + copied)) {
1472 ret = -EFAULT;
1473 goto out;
1474 }
1475 copied++;
1476 }
1477 }
1478 }
1479 card_res->count_connectors = connector_count;
1480
1481 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
1482 card_res->count_connectors, card_res->count_encoders);
1483
1484 out:
1485 drm_modeset_unlock_all(dev);
1486 return ret;
1487 }
1488
1489 /**
1490 * drm_mode_getcrtc - get CRTC configuration
1491 * @dev: drm device for the ioctl
1492 * @data: data pointer for the ioctl
1493 * @file_priv: drm file for the ioctl call
1494 *
1495 * Construct a CRTC configuration structure to return to the user.
1496 *
1497 * Called by the user via ioctl.
1498 *
1499 * RETURNS:
1500 * Zero on success, errno on failure.
1501 */
1502 int drm_mode_getcrtc(struct drm_device *dev,
1503 void *data, struct drm_file *file_priv)
1504 {
1505 struct drm_mode_crtc *crtc_resp = data;
1506 struct drm_crtc *crtc;
1507 struct drm_mode_object *obj;
1508 int ret = 0;
1509
1510 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1511 return -EINVAL;
1512
1513 drm_modeset_lock_all(dev);
1514
1515 obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
1516 DRM_MODE_OBJECT_CRTC);
1517 if (!obj) {
1518 ret = -EINVAL;
1519 goto out;
1520 }
1521 crtc = obj_to_crtc(obj);
1522
1523 crtc_resp->x = crtc->x;
1524 crtc_resp->y = crtc->y;
1525 crtc_resp->gamma_size = crtc->gamma_size;
1526 if (crtc->fb)
1527 crtc_resp->fb_id = crtc->fb->base.id;
1528 else
1529 crtc_resp->fb_id = 0;
1530
1531 if (crtc->enabled) {
1532
1533 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1534 crtc_resp->mode_valid = 1;
1535
1536 } else {
1537 crtc_resp->mode_valid = 0;
1538 }
1539
1540 out:
1541 drm_modeset_unlock_all(dev);
1542 return ret;
1543 }
1544
1545 /**
1546 * drm_mode_getconnector - get connector configuration
1547 * @dev: drm device for the ioctl
1548 * @data: data pointer for the ioctl
1549 * @file_priv: drm file for the ioctl call
1550 *
1551 * Construct a connector configuration structure to return to the user.
1552 *
1553 * Called by the user via ioctl.
1554 *
1555 * RETURNS:
1556 * Zero on success, errno on failure.
1557 */
1558 int drm_mode_getconnector(struct drm_device *dev, void *data,
1559 struct drm_file *file_priv)
1560 {
1561 struct drm_mode_get_connector *out_resp = data;
1562 struct drm_mode_object *obj;
1563 struct drm_connector *connector;
1564 struct drm_display_mode *mode;
1565 int mode_count = 0;
1566 int props_count = 0;
1567 int encoders_count = 0;
1568 int ret = 0;
1569 int copied = 0;
1570 int i;
1571 struct drm_mode_modeinfo u_mode;
1572 struct drm_mode_modeinfo __user *mode_ptr;
1573 uint32_t __user *prop_ptr;
1574 uint64_t __user *prop_values;
1575 uint32_t __user *encoder_ptr;
1576
1577 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1578 return -EINVAL;
1579
1580 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1581
1582 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
1583
1584 mutex_lock(&dev->mode_config.mutex);
1585
1586 obj = drm_mode_object_find(dev, out_resp->connector_id,
1587 DRM_MODE_OBJECT_CONNECTOR);
1588 if (!obj) {
1589 ret = -EINVAL;
1590 goto out;
1591 }
1592 connector = obj_to_connector(obj);
1593
1594 props_count = connector->properties.count;
1595
1596 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1597 if (connector->encoder_ids[i] != 0) {
1598 encoders_count++;
1599 }
1600 }
1601
1602 if (out_resp->count_modes == 0) {
1603 connector->funcs->fill_modes(connector,
1604 dev->mode_config.max_width,
1605 dev->mode_config.max_height);
1606 }
1607
1608 /* delayed so we get modes regardless of pre-fill_modes state */
1609 list_for_each_entry(mode, &connector->modes, head)
1610 mode_count++;
1611
1612 out_resp->connector_id = connector->base.id;
1613 out_resp->connector_type = connector->connector_type;
1614 out_resp->connector_type_id = connector->connector_type_id;
1615 out_resp->mm_width = connector->display_info.width_mm;
1616 out_resp->mm_height = connector->display_info.height_mm;
1617 out_resp->subpixel = connector->display_info.subpixel_order;
1618 out_resp->connection = connector->status;
1619 if (connector->encoder)
1620 out_resp->encoder_id = connector->encoder->base.id;
1621 else
1622 out_resp->encoder_id = 0;
1623
1624 /*
1625 * This ioctl is called twice, once to determine how much space is
1626 * needed, and the 2nd time to fill it.
1627 */
1628 if ((out_resp->count_modes >= mode_count) && mode_count) {
1629 copied = 0;
1630 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
1631 list_for_each_entry(mode, &connector->modes, head) {
1632 drm_crtc_convert_to_umode(&u_mode, mode);
1633 if (copy_to_user(mode_ptr + copied,
1634 &u_mode, sizeof(u_mode))) {
1635 ret = -EFAULT;
1636 goto out;
1637 }
1638 copied++;
1639 }
1640 }
1641 out_resp->count_modes = mode_count;
1642
1643 if ((out_resp->count_props >= props_count) && props_count) {
1644 copied = 0;
1645 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
1646 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
1647 for (i = 0; i < connector->properties.count; i++) {
1648 if (put_user(connector->properties.ids[i],
1649 prop_ptr + copied)) {
1650 ret = -EFAULT;
1651 goto out;
1652 }
1653
1654 if (put_user(connector->properties.values[i],
1655 prop_values + copied)) {
1656 ret = -EFAULT;
1657 goto out;
1658 }
1659 copied++;
1660 }
1661 }
1662 out_resp->count_props = props_count;
1663
1664 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1665 copied = 0;
1666 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
1667 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1668 if (connector->encoder_ids[i] != 0) {
1669 if (put_user(connector->encoder_ids[i],
1670 encoder_ptr + copied)) {
1671 ret = -EFAULT;
1672 goto out;
1673 }
1674 copied++;
1675 }
1676 }
1677 }
1678 out_resp->count_encoders = encoders_count;
1679
1680 out:
1681 mutex_unlock(&dev->mode_config.mutex);
1682
1683 return ret;
1684 }
1685
1686 int drm_mode_getencoder(struct drm_device *dev, void *data,
1687 struct drm_file *file_priv)
1688 {
1689 struct drm_mode_get_encoder *enc_resp = data;
1690 struct drm_mode_object *obj;
1691 struct drm_encoder *encoder;
1692 int ret = 0;
1693
1694 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1695 return -EINVAL;
1696
1697 drm_modeset_lock_all(dev);
1698 obj = drm_mode_object_find(dev, enc_resp->encoder_id,
1699 DRM_MODE_OBJECT_ENCODER);
1700 if (!obj) {
1701 ret = -EINVAL;
1702 goto out;
1703 }
1704 encoder = obj_to_encoder(obj);
1705
1706 if (encoder->crtc)
1707 enc_resp->crtc_id = encoder->crtc->base.id;
1708 else
1709 enc_resp->crtc_id = 0;
1710 enc_resp->encoder_type = encoder->encoder_type;
1711 enc_resp->encoder_id = encoder->base.id;
1712 enc_resp->possible_crtcs = encoder->possible_crtcs;
1713 enc_resp->possible_clones = encoder->possible_clones;
1714
1715 out:
1716 drm_modeset_unlock_all(dev);
1717 return ret;
1718 }
1719
1720 /**
1721 * drm_mode_getplane_res - get plane info
1722 * @dev: DRM device
1723 * @data: ioctl data
1724 * @file_priv: DRM file info
1725 *
1726 * Return an plane count and set of IDs.
1727 */
1728 int drm_mode_getplane_res(struct drm_device *dev, void *data,
1729 struct drm_file *file_priv)
1730 {
1731 struct drm_mode_get_plane_res *plane_resp = data;
1732 struct drm_mode_config *config;
1733 struct drm_plane *plane;
1734 uint32_t __user *plane_ptr;
1735 int copied = 0, ret = 0;
1736
1737 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1738 return -EINVAL;
1739
1740 drm_modeset_lock_all(dev);
1741 config = &dev->mode_config;
1742
1743 /*
1744 * This ioctl is called twice, once to determine how much space is
1745 * needed, and the 2nd time to fill it.
1746 */
1747 if (config->num_plane &&
1748 (plane_resp->count_planes >= config->num_plane)) {
1749 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
1750
1751 list_for_each_entry(plane, &config->plane_list, head) {
1752 if (put_user(plane->base.id, plane_ptr + copied)) {
1753 ret = -EFAULT;
1754 goto out;
1755 }
1756 copied++;
1757 }
1758 }
1759 plane_resp->count_planes = config->num_plane;
1760
1761 out:
1762 drm_modeset_unlock_all(dev);
1763 return ret;
1764 }
1765
1766 /**
1767 * drm_mode_getplane - get plane info
1768 * @dev: DRM device
1769 * @data: ioctl data
1770 * @file_priv: DRM file info
1771 *
1772 * Return plane info, including formats supported, gamma size, any
1773 * current fb, etc.
1774 */
1775 int drm_mode_getplane(struct drm_device *dev, void *data,
1776 struct drm_file *file_priv)
1777 {
1778 struct drm_mode_get_plane *plane_resp = data;
1779 struct drm_mode_object *obj;
1780 struct drm_plane *plane;
1781 uint32_t __user *format_ptr;
1782 int ret = 0;
1783
1784 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1785 return -EINVAL;
1786
1787 drm_modeset_lock_all(dev);
1788 obj = drm_mode_object_find(dev, plane_resp->plane_id,
1789 DRM_MODE_OBJECT_PLANE);
1790 if (!obj) {
1791 ret = -ENOENT;
1792 goto out;
1793 }
1794 plane = obj_to_plane(obj);
1795
1796 if (plane->crtc)
1797 plane_resp->crtc_id = plane->crtc->base.id;
1798 else
1799 plane_resp->crtc_id = 0;
1800
1801 if (plane->fb)
1802 plane_resp->fb_id = plane->fb->base.id;
1803 else
1804 plane_resp->fb_id = 0;
1805
1806 plane_resp->plane_id = plane->base.id;
1807 plane_resp->possible_crtcs = plane->possible_crtcs;
1808 plane_resp->gamma_size = 0;
1809
1810 /*
1811 * This ioctl is called twice, once to determine how much space is
1812 * needed, and the 2nd time to fill it.
1813 */
1814 if (plane->format_count &&
1815 (plane_resp->count_format_types >= plane->format_count)) {
1816 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
1817 if (copy_to_user(format_ptr,
1818 plane->format_types,
1819 sizeof(uint32_t) * plane->format_count)) {
1820 ret = -EFAULT;
1821 goto out;
1822 }
1823 }
1824 plane_resp->count_format_types = plane->format_count;
1825
1826 out:
1827 drm_modeset_unlock_all(dev);
1828 return ret;
1829 }
1830
1831 /**
1832 * drm_mode_setplane - set up or tear down an plane
1833 * @dev: DRM device
1834 * @data: ioctl data*
1835 * @file_priv: DRM file info
1836 *
1837 * Set plane info, including placement, fb, scaling, and other factors.
1838 * Or pass a NULL fb to disable.
1839 */
1840 int drm_mode_setplane(struct drm_device *dev, void *data,
1841 struct drm_file *file_priv)
1842 {
1843 struct drm_mode_set_plane *plane_req = data;
1844 struct drm_mode_object *obj;
1845 struct drm_plane *plane;
1846 struct drm_crtc *crtc;
1847 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
1848 int ret = 0;
1849 unsigned int fb_width, fb_height;
1850 int i;
1851
1852 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1853 return -EINVAL;
1854
1855 /*
1856 * First, find the plane, crtc, and fb objects. If not available,
1857 * we don't bother to call the driver.
1858 */
1859 obj = drm_mode_object_find(dev, plane_req->plane_id,
1860 DRM_MODE_OBJECT_PLANE);
1861 if (!obj) {
1862 DRM_DEBUG_KMS("Unknown plane ID %d\n",
1863 plane_req->plane_id);
1864 return -ENOENT;
1865 }
1866 plane = obj_to_plane(obj);
1867
1868 /* No fb means shut it down */
1869 if (!plane_req->fb_id) {
1870 drm_modeset_lock_all(dev);
1871 old_fb = plane->fb;
1872 plane->funcs->disable_plane(plane);
1873 plane->crtc = NULL;
1874 plane->fb = NULL;
1875 drm_modeset_unlock_all(dev);
1876 goto out;
1877 }
1878
1879 obj = drm_mode_object_find(dev, plane_req->crtc_id,
1880 DRM_MODE_OBJECT_CRTC);
1881 if (!obj) {
1882 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
1883 plane_req->crtc_id);
1884 ret = -ENOENT;
1885 goto out;
1886 }
1887 crtc = obj_to_crtc(obj);
1888
1889 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
1890 if (!fb) {
1891 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
1892 plane_req->fb_id);
1893 ret = -ENOENT;
1894 goto out;
1895 }
1896
1897 /* Check whether this plane supports the fb pixel format. */
1898 for (i = 0; i < plane->format_count; i++)
1899 if (fb->pixel_format == plane->format_types[i])
1900 break;
1901 if (i == plane->format_count) {
1902 DRM_DEBUG_KMS("Invalid pixel format %s\n",
1903 drm_get_format_name(fb->pixel_format));
1904 ret = -EINVAL;
1905 goto out;
1906 }
1907
1908 fb_width = fb->width << 16;
1909 fb_height = fb->height << 16;
1910
1911 /* Make sure source coordinates are inside the fb. */
1912 if (plane_req->src_w > fb_width ||
1913 plane_req->src_x > fb_width - plane_req->src_w ||
1914 plane_req->src_h > fb_height ||
1915 plane_req->src_y > fb_height - plane_req->src_h) {
1916 DRM_DEBUG_KMS("Invalid source coordinates "
1917 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
1918 plane_req->src_w >> 16,
1919 ((plane_req->src_w & 0xffff) * 15625) >> 10,
1920 plane_req->src_h >> 16,
1921 ((plane_req->src_h & 0xffff) * 15625) >> 10,
1922 plane_req->src_x >> 16,
1923 ((plane_req->src_x & 0xffff) * 15625) >> 10,
1924 plane_req->src_y >> 16,
1925 ((plane_req->src_y & 0xffff) * 15625) >> 10);
1926 ret = -ENOSPC;
1927 goto out;
1928 }
1929
1930 /* Give drivers some help against integer overflows */
1931 if (plane_req->crtc_w > INT_MAX ||
1932 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
1933 plane_req->crtc_h > INT_MAX ||
1934 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
1935 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
1936 plane_req->crtc_w, plane_req->crtc_h,
1937 plane_req->crtc_x, plane_req->crtc_y);
1938 ret = -ERANGE;
1939 goto out;
1940 }
1941
1942 drm_modeset_lock_all(dev);
1943 ret = plane->funcs->update_plane(plane, crtc, fb,
1944 plane_req->crtc_x, plane_req->crtc_y,
1945 plane_req->crtc_w, plane_req->crtc_h,
1946 plane_req->src_x, plane_req->src_y,
1947 plane_req->src_w, plane_req->src_h);
1948 if (!ret) {
1949 old_fb = plane->fb;
1950 plane->crtc = crtc;
1951 plane->fb = fb;
1952 fb = NULL;
1953 }
1954 drm_modeset_unlock_all(dev);
1955
1956 out:
1957 if (fb)
1958 drm_framebuffer_unreference(fb);
1959 if (old_fb)
1960 drm_framebuffer_unreference(old_fb);
1961
1962 return ret;
1963 }
1964
1965 /**
1966 * drm_mode_set_config_internal - helper to call ->set_config
1967 * @set: modeset config to set
1968 *
1969 * This is a little helper to wrap internal calls to the ->set_config driver
1970 * interface. The only thing it adds is correct refcounting dance.
1971 */
1972 int drm_mode_set_config_internal(struct drm_mode_set *set)
1973 {
1974 struct drm_crtc *crtc = set->crtc;
1975 struct drm_framebuffer *fb, *old_fb;
1976 int ret;
1977
1978 old_fb = crtc->fb;
1979 fb = set->fb;
1980
1981 ret = crtc->funcs->set_config(set);
1982 if (ret == 0) {
1983 /* crtc->fb must be updated by ->set_config, enforces this. */
1984 WARN_ON(fb != crtc->fb);
1985
1986 if (old_fb)
1987 drm_framebuffer_unreference(old_fb);
1988 if (fb)
1989 drm_framebuffer_reference(fb);
1990 }
1991
1992 return ret;
1993 }
1994 EXPORT_SYMBOL(drm_mode_set_config_internal);
1995
1996 /**
1997 * drm_mode_setcrtc - set CRTC configuration
1998 * @dev: drm device for the ioctl
1999 * @data: data pointer for the ioctl
2000 * @file_priv: drm file for the ioctl call
2001 *
2002 * Build a new CRTC configuration based on user request.
2003 *
2004 * Called by the user via ioctl.
2005 *
2006 * RETURNS:
2007 * Zero on success, errno on failure.
2008 */
2009 int drm_mode_setcrtc(struct drm_device *dev, void *data,
2010 struct drm_file *file_priv)
2011 {
2012 struct drm_mode_config *config = &dev->mode_config;
2013 struct drm_mode_crtc *crtc_req = data;
2014 struct drm_mode_object *obj;
2015 struct drm_crtc *crtc;
2016 struct drm_connector **connector_set = NULL, *connector;
2017 struct drm_framebuffer *fb = NULL;
2018 struct drm_display_mode *mode = NULL;
2019 struct drm_mode_set set;
2020 uint32_t __user *set_connectors_ptr;
2021 int ret;
2022 int i;
2023
2024 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2025 return -EINVAL;
2026
2027 /* For some reason crtc x/y offsets are signed internally. */
2028 if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
2029 return -ERANGE;
2030
2031 drm_modeset_lock_all(dev);
2032 obj = drm_mode_object_find(dev, crtc_req->crtc_id,
2033 DRM_MODE_OBJECT_CRTC);
2034 if (!obj) {
2035 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
2036 ret = -EINVAL;
2037 goto out;
2038 }
2039 crtc = obj_to_crtc(obj);
2040 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
2041
2042 if (crtc_req->mode_valid) {
2043 int hdisplay, vdisplay;
2044 /* If we have a mode we need a framebuffer. */
2045 /* If we pass -1, set the mode with the currently bound fb */
2046 if (crtc_req->fb_id == -1) {
2047 if (!crtc->fb) {
2048 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2049 ret = -EINVAL;
2050 goto out;
2051 }
2052 fb = crtc->fb;
2053 /* Make refcounting symmetric with the lookup path. */
2054 drm_framebuffer_reference(fb);
2055 } else {
2056 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2057 if (!fb) {
2058 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2059 crtc_req->fb_id);
2060 ret = -EINVAL;
2061 goto out;
2062 }
2063 }
2064
2065 mode = drm_mode_create(dev);
2066 if (!mode) {
2067 ret = -ENOMEM;
2068 goto out;
2069 }
2070
2071 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
2072 if (ret) {
2073 DRM_DEBUG_KMS("Invalid mode\n");
2074 goto out;
2075 }
2076
2077 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
2078
2079 hdisplay = mode->hdisplay;
2080 vdisplay = mode->vdisplay;
2081
2082 if (crtc->invert_dimensions)
2083 swap(hdisplay, vdisplay);
2084
2085 if (hdisplay > fb->width ||
2086 vdisplay > fb->height ||
2087 crtc_req->x > fb->width - hdisplay ||
2088 crtc_req->y > fb->height - vdisplay) {
2089 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
2090 fb->width, fb->height,
2091 hdisplay, vdisplay, crtc_req->x, crtc_req->y,
2092 crtc->invert_dimensions ? " (inverted)" : "");
2093 ret = -ENOSPC;
2094 goto out;
2095 }
2096 }
2097
2098 if (crtc_req->count_connectors == 0 && mode) {
2099 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
2100 ret = -EINVAL;
2101 goto out;
2102 }
2103
2104 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
2105 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
2106 crtc_req->count_connectors);
2107 ret = -EINVAL;
2108 goto out;
2109 }
2110
2111 if (crtc_req->count_connectors > 0) {
2112 u32 out_id;
2113
2114 /* Avoid unbounded kernel memory allocation */
2115 if (crtc_req->count_connectors > config->num_connector) {
2116 ret = -EINVAL;
2117 goto out;
2118 }
2119
2120 connector_set = kmalloc(crtc_req->count_connectors *
2121 sizeof(struct drm_connector *),
2122 GFP_KERNEL);
2123 if (!connector_set) {
2124 ret = -ENOMEM;
2125 goto out;
2126 }
2127
2128 for (i = 0; i < crtc_req->count_connectors; i++) {
2129 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
2130 if (get_user(out_id, &set_connectors_ptr[i])) {
2131 ret = -EFAULT;
2132 goto out;
2133 }
2134
2135 obj = drm_mode_object_find(dev, out_id,
2136 DRM_MODE_OBJECT_CONNECTOR);
2137 if (!obj) {
2138 DRM_DEBUG_KMS("Connector id %d unknown\n",
2139 out_id);
2140 ret = -EINVAL;
2141 goto out;
2142 }
2143 connector = obj_to_connector(obj);
2144 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2145 connector->base.id,
2146 drm_get_connector_name(connector));
2147
2148 connector_set[i] = connector;
2149 }
2150 }
2151
2152 set.crtc = crtc;
2153 set.x = crtc_req->x;
2154 set.y = crtc_req->y;
2155 set.mode = mode;
2156 set.connectors = connector_set;
2157 set.num_connectors = crtc_req->count_connectors;
2158 set.fb = fb;
2159 ret = drm_mode_set_config_internal(&set);
2160
2161 out:
2162 if (fb)
2163 drm_framebuffer_unreference(fb);
2164
2165 kfree(connector_set);
2166 drm_mode_destroy(dev, mode);
2167 drm_modeset_unlock_all(dev);
2168 return ret;
2169 }
2170
2171 int drm_mode_cursor_ioctl(struct drm_device *dev,
2172 void *data, struct drm_file *file_priv)
2173 {
2174 struct drm_mode_cursor *req = data;
2175 struct drm_mode_object *obj;
2176 struct drm_crtc *crtc;
2177 int ret = 0;
2178
2179 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2180 return -EINVAL;
2181
2182 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
2183 return -EINVAL;
2184
2185 obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
2186 if (!obj) {
2187 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
2188 return -EINVAL;
2189 }
2190 crtc = obj_to_crtc(obj);
2191
2192 mutex_lock(&crtc->mutex);
2193 if (req->flags & DRM_MODE_CURSOR_BO) {
2194 if (!crtc->funcs->cursor_set) {
2195 ret = -ENXIO;
2196 goto out;
2197 }
2198 /* Turns off the cursor if handle is 0 */
2199 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2200 req->width, req->height);
2201 }
2202
2203 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2204 if (crtc->funcs->cursor_move) {
2205 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2206 } else {
2207 ret = -EFAULT;
2208 goto out;
2209 }
2210 }
2211 out:
2212 mutex_unlock(&crtc->mutex);
2213
2214 return ret;
2215 }
2216
2217 /* Original addfb only supported RGB formats, so figure out which one */
2218 uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2219 {
2220 uint32_t fmt;
2221
2222 switch (bpp) {
2223 case 8:
2224 fmt = DRM_FORMAT_C8;
2225 break;
2226 case 16:
2227 if (depth == 15)
2228 fmt = DRM_FORMAT_XRGB1555;
2229 else
2230 fmt = DRM_FORMAT_RGB565;
2231 break;
2232 case 24:
2233 fmt = DRM_FORMAT_RGB888;
2234 break;
2235 case 32:
2236 if (depth == 24)
2237 fmt = DRM_FORMAT_XRGB8888;
2238 else if (depth == 30)
2239 fmt = DRM_FORMAT_XRGB2101010;
2240 else
2241 fmt = DRM_FORMAT_ARGB8888;
2242 break;
2243 default:
2244 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2245 fmt = DRM_FORMAT_XRGB8888;
2246 break;
2247 }
2248
2249 return fmt;
2250 }
2251 EXPORT_SYMBOL(drm_mode_legacy_fb_format);
2252
2253 /**
2254 * drm_mode_addfb - add an FB to the graphics configuration
2255 * @dev: drm device for the ioctl
2256 * @data: data pointer for the ioctl
2257 * @file_priv: drm file for the ioctl call
2258 *
2259 * Add a new FB to the specified CRTC, given a user request.
2260 *
2261 * Called by the user via ioctl.
2262 *
2263 * RETURNS:
2264 * Zero on success, errno on failure.
2265 */
2266 int drm_mode_addfb(struct drm_device *dev,
2267 void *data, struct drm_file *file_priv)
2268 {
2269 struct drm_mode_fb_cmd *or = data;
2270 struct drm_mode_fb_cmd2 r = {};
2271 struct drm_mode_config *config = &dev->mode_config;
2272 struct drm_framebuffer *fb;
2273 int ret = 0;
2274
2275 /* Use new struct with format internally */
2276 r.fb_id = or->fb_id;
2277 r.width = or->width;
2278 r.height = or->height;
2279 r.pitches[0] = or->pitch;
2280 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
2281 r.handles[0] = or->handle;
2282
2283 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2284 return -EINVAL;
2285
2286 if ((config->min_width > r.width) || (r.width > config->max_width))
2287 return -EINVAL;
2288
2289 if ((config->min_height > r.height) || (r.height > config->max_height))
2290 return -EINVAL;
2291
2292 fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2293 if (IS_ERR(fb)) {
2294 DRM_DEBUG_KMS("could not create framebuffer\n");
2295 return PTR_ERR(fb);
2296 }
2297
2298 mutex_lock(&file_priv->fbs_lock);
2299 or->fb_id = fb->base.id;
2300 list_add(&fb->filp_head, &file_priv->fbs);
2301 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
2302 mutex_unlock(&file_priv->fbs_lock);
2303
2304 return ret;
2305 }
2306
2307 static int format_check(const struct drm_mode_fb_cmd2 *r)
2308 {
2309 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2310
2311 switch (format) {
2312 case DRM_FORMAT_C8:
2313 case DRM_FORMAT_RGB332:
2314 case DRM_FORMAT_BGR233:
2315 case DRM_FORMAT_XRGB4444:
2316 case DRM_FORMAT_XBGR4444:
2317 case DRM_FORMAT_RGBX4444:
2318 case DRM_FORMAT_BGRX4444:
2319 case DRM_FORMAT_ARGB4444:
2320 case DRM_FORMAT_ABGR4444:
2321 case DRM_FORMAT_RGBA4444:
2322 case DRM_FORMAT_BGRA4444:
2323 case DRM_FORMAT_XRGB1555:
2324 case DRM_FORMAT_XBGR1555:
2325 case DRM_FORMAT_RGBX5551:
2326 case DRM_FORMAT_BGRX5551:
2327 case DRM_FORMAT_ARGB1555:
2328 case DRM_FORMAT_ABGR1555:
2329 case DRM_FORMAT_RGBA5551:
2330 case DRM_FORMAT_BGRA5551:
2331 case DRM_FORMAT_RGB565:
2332 case DRM_FORMAT_BGR565:
2333 case DRM_FORMAT_RGB888:
2334 case DRM_FORMAT_BGR888:
2335 case DRM_FORMAT_XRGB8888:
2336 case DRM_FORMAT_XBGR8888:
2337 case DRM_FORMAT_RGBX8888:
2338 case DRM_FORMAT_BGRX8888:
2339 case DRM_FORMAT_ARGB8888:
2340 case DRM_FORMAT_ABGR8888:
2341 case DRM_FORMAT_RGBA8888:
2342 case DRM_FORMAT_BGRA8888:
2343 case DRM_FORMAT_XRGB2101010:
2344 case DRM_FORMAT_XBGR2101010:
2345 case DRM_FORMAT_RGBX1010102:
2346 case DRM_FORMAT_BGRX1010102:
2347 case DRM_FORMAT_ARGB2101010:
2348 case DRM_FORMAT_ABGR2101010:
2349 case DRM_FORMAT_RGBA1010102:
2350 case DRM_FORMAT_BGRA1010102:
2351 case DRM_FORMAT_YUYV:
2352 case DRM_FORMAT_YVYU:
2353 case DRM_FORMAT_UYVY:
2354 case DRM_FORMAT_VYUY:
2355 case DRM_FORMAT_AYUV:
2356 case DRM_FORMAT_NV12:
2357 case DRM_FORMAT_NV21:
2358 case DRM_FORMAT_NV16:
2359 case DRM_FORMAT_NV61:
2360 case DRM_FORMAT_NV24:
2361 case DRM_FORMAT_NV42:
2362 case DRM_FORMAT_YUV410:
2363 case DRM_FORMAT_YVU410:
2364 case DRM_FORMAT_YUV411:
2365 case DRM_FORMAT_YVU411:
2366 case DRM_FORMAT_YUV420:
2367 case DRM_FORMAT_YVU420:
2368 case DRM_FORMAT_YUV422:
2369 case DRM_FORMAT_YVU422:
2370 case DRM_FORMAT_YUV444:
2371 case DRM_FORMAT_YVU444:
2372 return 0;
2373 default:
2374 return -EINVAL;
2375 }
2376 }
2377
2378 static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
2379 {
2380 int ret, hsub, vsub, num_planes, i;
2381
2382 ret = format_check(r);
2383 if (ret) {
2384 DRM_DEBUG_KMS("bad framebuffer format %s\n",
2385 drm_get_format_name(r->pixel_format));
2386 return ret;
2387 }
2388
2389 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
2390 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
2391 num_planes = drm_format_num_planes(r->pixel_format);
2392
2393 if (r->width == 0 || r->width % hsub) {
2394 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
2395 return -EINVAL;
2396 }
2397
2398 if (r->height == 0 || r->height % vsub) {
2399 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
2400 return -EINVAL;
2401 }
2402
2403 for (i = 0; i < num_planes; i++) {
2404 unsigned int width = r->width / (i != 0 ? hsub : 1);
2405 unsigned int height = r->height / (i != 0 ? vsub : 1);
2406 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
2407
2408 if (!r->handles[i]) {
2409 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
2410 return -EINVAL;
2411 }
2412
2413 if ((uint64_t) width * cpp > UINT_MAX)
2414 return -ERANGE;
2415
2416 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
2417 return -ERANGE;
2418
2419 if (r->pitches[i] < width * cpp) {
2420 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
2421 return -EINVAL;
2422 }
2423 }
2424
2425 return 0;
2426 }
2427
2428 /**
2429 * drm_mode_addfb2 - add an FB to the graphics configuration
2430 * @dev: drm device for the ioctl
2431 * @data: data pointer for the ioctl
2432 * @file_priv: drm file for the ioctl call
2433 *
2434 * Add a new FB to the specified CRTC, given a user request with format.
2435 *
2436 * Called by the user via ioctl.
2437 *
2438 * RETURNS:
2439 * Zero on success, errno on failure.
2440 */
2441 int drm_mode_addfb2(struct drm_device *dev,
2442 void *data, struct drm_file *file_priv)
2443 {
2444 struct drm_mode_fb_cmd2 *r = data;
2445 struct drm_mode_config *config = &dev->mode_config;
2446 struct drm_framebuffer *fb;
2447 int ret;
2448
2449 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2450 return -EINVAL;
2451
2452 if (r->flags & ~DRM_MODE_FB_INTERLACED) {
2453 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
2454 return -EINVAL;
2455 }
2456
2457 if ((config->min_width > r->width) || (r->width > config->max_width)) {
2458 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
2459 r->width, config->min_width, config->max_width);
2460 return -EINVAL;
2461 }
2462 if ((config->min_height > r->height) || (r->height > config->max_height)) {
2463 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
2464 r->height, config->min_height, config->max_height);
2465 return -EINVAL;
2466 }
2467
2468 ret = framebuffer_check(r);
2469 if (ret)
2470 return ret;
2471
2472 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
2473 if (IS_ERR(fb)) {
2474 DRM_DEBUG_KMS("could not create framebuffer\n");
2475 return PTR_ERR(fb);
2476 }
2477
2478 mutex_lock(&file_priv->fbs_lock);
2479 r->fb_id = fb->base.id;
2480 list_add(&fb->filp_head, &file_priv->fbs);
2481 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
2482 mutex_unlock(&file_priv->fbs_lock);
2483
2484
2485 return ret;
2486 }
2487
2488 /**
2489 * drm_mode_rmfb - remove an FB from the configuration
2490 * @dev: drm device for the ioctl
2491 * @data: data pointer for the ioctl
2492 * @file_priv: drm file for the ioctl call
2493 *
2494 * Remove the FB specified by the user.
2495 *
2496 * Called by the user via ioctl.
2497 *
2498 * RETURNS:
2499 * Zero on success, errno on failure.
2500 */
2501 int drm_mode_rmfb(struct drm_device *dev,
2502 void *data, struct drm_file *file_priv)
2503 {
2504 struct drm_framebuffer *fb = NULL;
2505 struct drm_framebuffer *fbl = NULL;
2506 uint32_t *id = data;
2507 int found = 0;
2508
2509 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2510 return -EINVAL;
2511
2512 mutex_lock(&file_priv->fbs_lock);
2513 mutex_lock(&dev->mode_config.fb_lock);
2514 fb = __drm_framebuffer_lookup(dev, *id);
2515 if (!fb)
2516 goto fail_lookup;
2517
2518 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2519 if (fb == fbl)
2520 found = 1;
2521 if (!found)
2522 goto fail_lookup;
2523
2524 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
2525 __drm_framebuffer_unregister(dev, fb);
2526
2527 list_del_init(&fb->filp_head);
2528 mutex_unlock(&dev->mode_config.fb_lock);
2529 mutex_unlock(&file_priv->fbs_lock);
2530
2531 drm_framebuffer_remove(fb);
2532
2533 return 0;
2534
2535 fail_lookup:
2536 mutex_unlock(&dev->mode_config.fb_lock);
2537 mutex_unlock(&file_priv->fbs_lock);
2538
2539 return -EINVAL;
2540 }
2541
2542 /**
2543 * drm_mode_getfb - get FB info
2544 * @dev: drm device for the ioctl
2545 * @data: data pointer for the ioctl
2546 * @file_priv: drm file for the ioctl call
2547 *
2548 * Lookup the FB given its ID and return info about it.
2549 *
2550 * Called by the user via ioctl.
2551 *
2552 * RETURNS:
2553 * Zero on success, errno on failure.
2554 */
2555 int drm_mode_getfb(struct drm_device *dev,
2556 void *data, struct drm_file *file_priv)
2557 {
2558 struct drm_mode_fb_cmd *r = data;
2559 struct drm_framebuffer *fb;
2560 int ret;
2561
2562 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2563 return -EINVAL;
2564
2565 fb = drm_framebuffer_lookup(dev, r->fb_id);
2566 if (!fb)
2567 return -EINVAL;
2568
2569 r->height = fb->height;
2570 r->width = fb->width;
2571 r->depth = fb->depth;
2572 r->bpp = fb->bits_per_pixel;
2573 r->pitch = fb->pitches[0];
2574 if (fb->funcs->create_handle)
2575 ret = fb->funcs->create_handle(fb, file_priv, &r->handle);
2576 else
2577 ret = -ENODEV;
2578
2579 drm_framebuffer_unreference(fb);
2580
2581 return ret;
2582 }
2583
2584 int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
2585 void *data, struct drm_file *file_priv)
2586 {
2587 struct drm_clip_rect __user *clips_ptr;
2588 struct drm_clip_rect *clips = NULL;
2589 struct drm_mode_fb_dirty_cmd *r = data;
2590 struct drm_framebuffer *fb;
2591 unsigned flags;
2592 int num_clips;
2593 int ret;
2594
2595 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2596 return -EINVAL;
2597
2598 fb = drm_framebuffer_lookup(dev, r->fb_id);
2599 if (!fb)
2600 return -EINVAL;
2601
2602 num_clips = r->num_clips;
2603 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
2604
2605 if (!num_clips != !clips_ptr) {
2606 ret = -EINVAL;
2607 goto out_err1;
2608 }
2609
2610 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
2611
2612 /* If userspace annotates copy, clips must come in pairs */
2613 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
2614 ret = -EINVAL;
2615 goto out_err1;
2616 }
2617
2618 if (num_clips && clips_ptr) {
2619 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
2620 ret = -EINVAL;
2621 goto out_err1;
2622 }
2623 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
2624 if (!clips) {
2625 ret = -ENOMEM;
2626 goto out_err1;
2627 }
2628
2629 ret = copy_from_user(clips, clips_ptr,
2630 num_clips * sizeof(*clips));
2631 if (ret) {
2632 ret = -EFAULT;
2633 goto out_err2;
2634 }
2635 }
2636
2637 if (fb->funcs->dirty) {
2638 drm_modeset_lock_all(dev);
2639 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
2640 clips, num_clips);
2641 drm_modeset_unlock_all(dev);
2642 } else {
2643 ret = -ENOSYS;
2644 }
2645
2646 out_err2:
2647 kfree(clips);
2648 out_err1:
2649 drm_framebuffer_unreference(fb);
2650
2651 return ret;
2652 }
2653
2654
2655 /**
2656 * drm_fb_release - remove and free the FBs on this file
2657 * @priv: drm file for the ioctl
2658 *
2659 * Destroy all the FBs associated with @filp.
2660 *
2661 * Called by the user via ioctl.
2662 *
2663 * RETURNS:
2664 * Zero on success, errno on failure.
2665 */
2666 void drm_fb_release(struct drm_file *priv)
2667 {
2668 struct drm_device *dev = priv->minor->dev;
2669 struct drm_framebuffer *fb, *tfb;
2670
2671 mutex_lock(&priv->fbs_lock);
2672 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
2673
2674 mutex_lock(&dev->mode_config.fb_lock);
2675 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
2676 __drm_framebuffer_unregister(dev, fb);
2677 mutex_unlock(&dev->mode_config.fb_lock);
2678
2679 list_del_init(&fb->filp_head);
2680
2681 /* This will also drop the fpriv->fbs reference. */
2682 drm_framebuffer_remove(fb);
2683 }
2684 mutex_unlock(&priv->fbs_lock);
2685 }
2686
2687 struct drm_property *drm_property_create(struct drm_device *dev, int flags,
2688 const char *name, int num_values)
2689 {
2690 struct drm_property *property = NULL;
2691 int ret;
2692
2693 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
2694 if (!property)
2695 return NULL;
2696
2697 if (num_values) {
2698 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
2699 if (!property->values)
2700 goto fail;
2701 }
2702
2703 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
2704 if (ret)
2705 goto fail;
2706
2707 property->flags = flags;
2708 property->num_values = num_values;
2709 INIT_LIST_HEAD(&property->enum_blob_list);
2710
2711 if (name) {
2712 strncpy(property->name, name, DRM_PROP_NAME_LEN);
2713 property->name[DRM_PROP_NAME_LEN-1] = '\0';
2714 }
2715
2716 list_add_tail(&property->head, &dev->mode_config.property_list);
2717 return property;
2718 fail:
2719 kfree(property->values);
2720 kfree(property);
2721 return NULL;
2722 }
2723 EXPORT_SYMBOL(drm_property_create);
2724
2725 struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
2726 const char *name,
2727 const struct drm_prop_enum_list *props,
2728 int num_values)
2729 {
2730 struct drm_property *property;
2731 int i, ret;
2732
2733 flags |= DRM_MODE_PROP_ENUM;
2734
2735 property = drm_property_create(dev, flags, name, num_values);
2736 if (!property)
2737 return NULL;
2738
2739 for (i = 0; i < num_values; i++) {
2740 ret = drm_property_add_enum(property, i,
2741 props[i].type,
2742 props[i].name);
2743 if (ret) {
2744 drm_property_destroy(dev, property);
2745 return NULL;
2746 }
2747 }
2748
2749 return property;
2750 }
2751 EXPORT_SYMBOL(drm_property_create_enum);
2752
2753 struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
2754 int flags, const char *name,
2755 const struct drm_prop_enum_list *props,
2756 int num_values)
2757 {
2758 struct drm_property *property;
2759 int i, ret;
2760
2761 flags |= DRM_MODE_PROP_BITMASK;
2762
2763 property = drm_property_create(dev, flags, name, num_values);
2764 if (!property)
2765 return NULL;
2766
2767 for (i = 0; i < num_values; i++) {
2768 ret = drm_property_add_enum(property, i,
2769 props[i].type,
2770 props[i].name);
2771 if (ret) {
2772 drm_property_destroy(dev, property);
2773 return NULL;
2774 }
2775 }
2776
2777 return property;
2778 }
2779 EXPORT_SYMBOL(drm_property_create_bitmask);
2780
2781 struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
2782 const char *name,
2783 uint64_t min, uint64_t max)
2784 {
2785 struct drm_property *property;
2786
2787 flags |= DRM_MODE_PROP_RANGE;
2788
2789 property = drm_property_create(dev, flags, name, 2);
2790 if (!property)
2791 return NULL;
2792
2793 property->values[0] = min;
2794 property->values[1] = max;
2795
2796 return property;
2797 }
2798 EXPORT_SYMBOL(drm_property_create_range);
2799
2800 int drm_property_add_enum(struct drm_property *property, int index,
2801 uint64_t value, const char *name)
2802 {
2803 struct drm_property_enum *prop_enum;
2804
2805 if (!(property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)))
2806 return -EINVAL;
2807
2808 /*
2809 * Bitmask enum properties have the additional constraint of values
2810 * from 0 to 63
2811 */
2812 if ((property->flags & DRM_MODE_PROP_BITMASK) && (value > 63))
2813 return -EINVAL;
2814
2815 if (!list_empty(&property->enum_blob_list)) {
2816 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2817 if (prop_enum->value == value) {
2818 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2819 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2820 return 0;
2821 }
2822 }
2823 }
2824
2825 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
2826 if (!prop_enum)
2827 return -ENOMEM;
2828
2829 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2830 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2831 prop_enum->value = value;
2832
2833 property->values[index] = value;
2834 list_add_tail(&prop_enum->head, &property->enum_blob_list);
2835 return 0;
2836 }
2837 EXPORT_SYMBOL(drm_property_add_enum);
2838
2839 void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
2840 {
2841 struct drm_property_enum *prop_enum, *pt;
2842
2843 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
2844 list_del(&prop_enum->head);
2845 kfree(prop_enum);
2846 }
2847
2848 if (property->num_values)
2849 kfree(property->values);
2850 drm_mode_object_put(dev, &property->base);
2851 list_del(&property->head);
2852 kfree(property);
2853 }
2854 EXPORT_SYMBOL(drm_property_destroy);
2855
2856 void drm_object_attach_property(struct drm_mode_object *obj,
2857 struct drm_property *property,
2858 uint64_t init_val)
2859 {
2860 int count = obj->properties->count;
2861
2862 if (count == DRM_OBJECT_MAX_PROPERTY) {
2863 WARN(1, "Failed to attach object property (type: 0x%x). Please "
2864 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
2865 "you see this message on the same object type.\n",
2866 obj->type);
2867 return;
2868 }
2869
2870 obj->properties->ids[count] = property->base.id;
2871 obj->properties->values[count] = init_val;
2872 obj->properties->count++;
2873 }
2874 EXPORT_SYMBOL(drm_object_attach_property);
2875
2876 int drm_object_property_set_value(struct drm_mode_object *obj,
2877 struct drm_property *property, uint64_t val)
2878 {
2879 int i;
2880
2881 for (i = 0; i < obj->properties->count; i++) {
2882 if (obj->properties->ids[i] == property->base.id) {
2883 obj->properties->values[i] = val;
2884 return 0;
2885 }
2886 }
2887
2888 return -EINVAL;
2889 }
2890 EXPORT_SYMBOL(drm_object_property_set_value);
2891
2892 int drm_object_property_get_value(struct drm_mode_object *obj,
2893 struct drm_property *property, uint64_t *val)
2894 {
2895 int i;
2896
2897 for (i = 0; i < obj->properties->count; i++) {
2898 if (obj->properties->ids[i] == property->base.id) {
2899 *val = obj->properties->values[i];
2900 return 0;
2901 }
2902 }
2903
2904 return -EINVAL;
2905 }
2906 EXPORT_SYMBOL(drm_object_property_get_value);
2907
2908 int drm_mode_getproperty_ioctl(struct drm_device *dev,
2909 void *data, struct drm_file *file_priv)
2910 {
2911 struct drm_mode_object *obj;
2912 struct drm_mode_get_property *out_resp = data;
2913 struct drm_property *property;
2914 int enum_count = 0;
2915 int blob_count = 0;
2916 int value_count = 0;
2917 int ret = 0, i;
2918 int copied;
2919 struct drm_property_enum *prop_enum;
2920 struct drm_mode_property_enum __user *enum_ptr;
2921 struct drm_property_blob *prop_blob;
2922 uint32_t __user *blob_id_ptr;
2923 uint64_t __user *values_ptr;
2924 uint32_t __user *blob_length_ptr;
2925
2926 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2927 return -EINVAL;
2928
2929 drm_modeset_lock_all(dev);
2930 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
2931 if (!obj) {
2932 ret = -EINVAL;
2933 goto done;
2934 }
2935 property = obj_to_property(obj);
2936
2937 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
2938 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
2939 enum_count++;
2940 } else if (property->flags & DRM_MODE_PROP_BLOB) {
2941 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
2942 blob_count++;
2943 }
2944
2945 value_count = property->num_values;
2946
2947 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
2948 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
2949 out_resp->flags = property->flags;
2950
2951 if ((out_resp->count_values >= value_count) && value_count) {
2952 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
2953 for (i = 0; i < value_count; i++) {
2954 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
2955 ret = -EFAULT;
2956 goto done;
2957 }
2958 }
2959 }
2960 out_resp->count_values = value_count;
2961
2962 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
2963 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
2964 copied = 0;
2965 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
2966 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2967
2968 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
2969 ret = -EFAULT;
2970 goto done;
2971 }
2972
2973 if (copy_to_user(&enum_ptr[copied].name,
2974 &prop_enum->name, DRM_PROP_NAME_LEN)) {
2975 ret = -EFAULT;
2976 goto done;
2977 }
2978 copied++;
2979 }
2980 }
2981 out_resp->count_enum_blobs = enum_count;
2982 }
2983
2984 if (property->flags & DRM_MODE_PROP_BLOB) {
2985 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
2986 copied = 0;
2987 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
2988 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
2989
2990 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
2991 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
2992 ret = -EFAULT;
2993 goto done;
2994 }
2995
2996 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
2997 ret = -EFAULT;
2998 goto done;
2999 }
3000
3001 copied++;
3002 }
3003 }
3004 out_resp->count_enum_blobs = blob_count;
3005 }
3006 done:
3007 drm_modeset_unlock_all(dev);
3008 return ret;
3009 }
3010
3011 static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
3012 void *data)
3013 {
3014 struct drm_property_blob *blob;
3015 int ret;
3016
3017 if (!length || !data)
3018 return NULL;
3019
3020 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
3021 if (!blob)
3022 return NULL;
3023
3024 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
3025 if (ret) {
3026 kfree(blob);
3027 return NULL;
3028 }
3029
3030 blob->length = length;
3031
3032 memcpy(blob->data, data, length);
3033
3034 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
3035 return blob;
3036 }
3037
3038 static void drm_property_destroy_blob(struct drm_device *dev,
3039 struct drm_property_blob *blob)
3040 {
3041 drm_mode_object_put(dev, &blob->base);
3042 list_del(&blob->head);
3043 kfree(blob);
3044 }
3045
3046 int drm_mode_getblob_ioctl(struct drm_device *dev,
3047 void *data, struct drm_file *file_priv)
3048 {
3049 struct drm_mode_object *obj;
3050 struct drm_mode_get_blob *out_resp = data;
3051 struct drm_property_blob *blob;
3052 int ret = 0;
3053 void __user *blob_ptr;
3054
3055 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3056 return -EINVAL;
3057
3058 drm_modeset_lock_all(dev);
3059 obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
3060 if (!obj) {
3061 ret = -EINVAL;
3062 goto done;
3063 }
3064 blob = obj_to_blob(obj);
3065
3066 if (out_resp->length == blob->length) {
3067 blob_ptr = (void __user *)(unsigned long)out_resp->data;
3068 if (copy_to_user(blob_ptr, blob->data, blob->length)){
3069 ret = -EFAULT;
3070 goto done;
3071 }
3072 }
3073 out_resp->length = blob->length;
3074
3075 done:
3076 drm_modeset_unlock_all(dev);
3077 return ret;
3078 }
3079
3080 int drm_mode_connector_update_edid_property(struct drm_connector *connector,
3081 struct edid *edid)
3082 {
3083 struct drm_device *dev = connector->dev;
3084 int ret, size;
3085
3086 if (connector->edid_blob_ptr)
3087 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
3088
3089 /* Delete edid, when there is none. */
3090 if (!edid) {
3091 connector->edid_blob_ptr = NULL;
3092 ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
3093 return ret;
3094 }
3095
3096 size = EDID_LENGTH * (1 + edid->extensions);
3097 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
3098 size, edid);
3099 if (!connector->edid_blob_ptr)
3100 return -EINVAL;
3101
3102 ret = drm_object_property_set_value(&connector->base,
3103 dev->mode_config.edid_property,
3104 connector->edid_blob_ptr->base.id);
3105
3106 return ret;
3107 }
3108 EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
3109
3110 static bool drm_property_change_is_valid(struct drm_property *property,
3111 uint64_t value)
3112 {
3113 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
3114 return false;
3115 if (property->flags & DRM_MODE_PROP_RANGE) {
3116 if (value < property->values[0] || value > property->values[1])
3117 return false;
3118 return true;
3119 } else if (property->flags & DRM_MODE_PROP_BITMASK) {
3120 int i;
3121 uint64_t valid_mask = 0;
3122 for (i = 0; i < property->num_values; i++)
3123 valid_mask |= (1ULL << property->values[i]);
3124 return !(value & ~valid_mask);
3125 } else if (property->flags & DRM_MODE_PROP_BLOB) {
3126 /* Only the driver knows */
3127 return true;
3128 } else {
3129 int i;
3130 for (i = 0; i < property->num_values; i++)
3131 if (property->values[i] == value)
3132 return true;
3133 return false;
3134 }
3135 }
3136
3137 int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
3138 void *data, struct drm_file *file_priv)
3139 {
3140 struct drm_mode_connector_set_property *conn_set_prop = data;
3141 struct drm_mode_obj_set_property obj_set_prop = {
3142 .value = conn_set_prop->value,
3143 .prop_id = conn_set_prop->prop_id,
3144 .obj_id = conn_set_prop->connector_id,
3145 .obj_type = DRM_MODE_OBJECT_CONNECTOR
3146 };
3147
3148 /* It does all the locking and checking we need */
3149 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
3150 }
3151
3152 static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
3153 struct drm_property *property,
3154 uint64_t value)
3155 {
3156 int ret = -EINVAL;
3157 struct drm_connector *connector = obj_to_connector(obj);
3158
3159 /* Do DPMS ourselves */
3160 if (property == connector->dev->mode_config.dpms_property) {
3161 if (connector->funcs->dpms)
3162 (*connector->funcs->dpms)(connector, (int)value);
3163 ret = 0;
3164 } else if (connector->funcs->set_property)
3165 ret = connector->funcs->set_property(connector, property, value);
3166
3167 /* store the property value if successful */
3168 if (!ret)
3169 drm_object_property_set_value(&connector->base, property, value);
3170 return ret;
3171 }
3172
3173 static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
3174 struct drm_property *property,
3175 uint64_t value)
3176 {
3177 int ret = -EINVAL;
3178 struct drm_crtc *crtc = obj_to_crtc(obj);
3179
3180 if (crtc->funcs->set_property)
3181 ret = crtc->funcs->set_property(crtc, property, value);
3182 if (!ret)
3183 drm_object_property_set_value(obj, property, value);
3184
3185 return ret;
3186 }
3187
3188 static int drm_mode_plane_set_obj_prop(struct drm_mode_object *obj,
3189 struct drm_property *property,
3190 uint64_t value)
3191 {
3192 int ret = -EINVAL;
3193 struct drm_plane *plane = obj_to_plane(obj);
3194
3195 if (plane->funcs->set_property)
3196 ret = plane->funcs->set_property(plane, property, value);
3197 if (!ret)
3198 drm_object_property_set_value(obj, property, value);
3199
3200 return ret;
3201 }
3202
3203 int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
3204 struct drm_file *file_priv)
3205 {
3206 struct drm_mode_obj_get_properties *arg = data;
3207 struct drm_mode_object *obj;
3208 int ret = 0;
3209 int i;
3210 int copied = 0;
3211 int props_count = 0;
3212 uint32_t __user *props_ptr;
3213 uint64_t __user *prop_values_ptr;
3214
3215 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3216 return -EINVAL;
3217
3218 drm_modeset_lock_all(dev);
3219
3220 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3221 if (!obj) {
3222 ret = -EINVAL;
3223 goto out;
3224 }
3225 if (!obj->properties) {
3226 ret = -EINVAL;
3227 goto out;
3228 }
3229
3230 props_count = obj->properties->count;
3231
3232 /* This ioctl is called twice, once to determine how much space is
3233 * needed, and the 2nd time to fill it. */
3234 if ((arg->count_props >= props_count) && props_count) {
3235 copied = 0;
3236 props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
3237 prop_values_ptr = (uint64_t __user *)(unsigned long)
3238 (arg->prop_values_ptr);
3239 for (i = 0; i < props_count; i++) {
3240 if (put_user(obj->properties->ids[i],
3241 props_ptr + copied)) {
3242 ret = -EFAULT;
3243 goto out;
3244 }
3245 if (put_user(obj->properties->values[i],
3246 prop_values_ptr + copied)) {
3247 ret = -EFAULT;
3248 goto out;
3249 }
3250 copied++;
3251 }
3252 }
3253 arg->count_props = props_count;
3254 out:
3255 drm_modeset_unlock_all(dev);
3256 return ret;
3257 }
3258
3259 int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
3260 struct drm_file *file_priv)
3261 {
3262 struct drm_mode_obj_set_property *arg = data;
3263 struct drm_mode_object *arg_obj;
3264 struct drm_mode_object *prop_obj;
3265 struct drm_property *property;
3266 int ret = -EINVAL;
3267 int i;
3268
3269 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3270 return -EINVAL;
3271
3272 drm_modeset_lock_all(dev);
3273
3274 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3275 if (!arg_obj)
3276 goto out;
3277 if (!arg_obj->properties)
3278 goto out;
3279
3280 for (i = 0; i < arg_obj->properties->count; i++)
3281 if (arg_obj->properties->ids[i] == arg->prop_id)
3282 break;
3283
3284 if (i == arg_obj->properties->count)
3285 goto out;
3286
3287 prop_obj = drm_mode_object_find(dev, arg->prop_id,
3288 DRM_MODE_OBJECT_PROPERTY);
3289 if (!prop_obj)
3290 goto out;
3291 property = obj_to_property(prop_obj);
3292
3293 if (!drm_property_change_is_valid(property, arg->value))
3294 goto out;
3295
3296 switch (arg_obj->type) {
3297 case DRM_MODE_OBJECT_CONNECTOR:
3298 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
3299 arg->value);
3300 break;
3301 case DRM_MODE_OBJECT_CRTC:
3302 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
3303 break;
3304 case DRM_MODE_OBJECT_PLANE:
3305 ret = drm_mode_plane_set_obj_prop(arg_obj, property, arg->value);
3306 break;
3307 }
3308
3309 out:
3310 drm_modeset_unlock_all(dev);
3311 return ret;
3312 }
3313
3314 int drm_mode_connector_attach_encoder(struct drm_connector *connector,
3315 struct drm_encoder *encoder)
3316 {
3317 int i;
3318
3319 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3320 if (connector->encoder_ids[i] == 0) {
3321 connector->encoder_ids[i] = encoder->base.id;
3322 return 0;
3323 }
3324 }
3325 return -ENOMEM;
3326 }
3327 EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
3328
3329 void drm_mode_connector_detach_encoder(struct drm_connector *connector,
3330 struct drm_encoder *encoder)
3331 {
3332 int i;
3333 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3334 if (connector->encoder_ids[i] == encoder->base.id) {
3335 connector->encoder_ids[i] = 0;
3336 if (connector->encoder == encoder)
3337 connector->encoder = NULL;
3338 break;
3339 }
3340 }
3341 }
3342 EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
3343
3344 int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
3345 int gamma_size)
3346 {
3347 crtc->gamma_size = gamma_size;
3348
3349 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
3350 if (!crtc->gamma_store) {
3351 crtc->gamma_size = 0;
3352 return -ENOMEM;
3353 }
3354
3355 return 0;
3356 }
3357 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
3358
3359 int drm_mode_gamma_set_ioctl(struct drm_device *dev,
3360 void *data, struct drm_file *file_priv)
3361 {
3362 struct drm_mode_crtc_lut *crtc_lut = data;
3363 struct drm_mode_object *obj;
3364 struct drm_crtc *crtc;
3365 void *r_base, *g_base, *b_base;
3366 int size;
3367 int ret = 0;
3368
3369 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3370 return -EINVAL;
3371
3372 drm_modeset_lock_all(dev);
3373 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3374 if (!obj) {
3375 ret = -EINVAL;
3376 goto out;
3377 }
3378 crtc = obj_to_crtc(obj);
3379
3380 if (crtc->funcs->gamma_set == NULL) {
3381 ret = -ENOSYS;
3382 goto out;
3383 }
3384
3385 /* memcpy into gamma store */
3386 if (crtc_lut->gamma_size != crtc->gamma_size) {
3387 ret = -EINVAL;
3388 goto out;
3389 }
3390
3391 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3392 r_base = crtc->gamma_store;
3393 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
3394 ret = -EFAULT;
3395 goto out;
3396 }
3397
3398 g_base = r_base + size;
3399 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
3400 ret = -EFAULT;
3401 goto out;
3402 }
3403
3404 b_base = g_base + size;
3405 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
3406 ret = -EFAULT;
3407 goto out;
3408 }
3409
3410 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
3411
3412 out:
3413 drm_modeset_unlock_all(dev);
3414 return ret;
3415
3416 }
3417
3418 int drm_mode_gamma_get_ioctl(struct drm_device *dev,
3419 void *data, struct drm_file *file_priv)
3420 {
3421 struct drm_mode_crtc_lut *crtc_lut = data;
3422 struct drm_mode_object *obj;
3423 struct drm_crtc *crtc;
3424 void *r_base, *g_base, *b_base;
3425 int size;
3426 int ret = 0;
3427
3428 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3429 return -EINVAL;
3430
3431 drm_modeset_lock_all(dev);
3432 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3433 if (!obj) {
3434 ret = -EINVAL;
3435 goto out;
3436 }
3437 crtc = obj_to_crtc(obj);
3438
3439 /* memcpy into gamma store */
3440 if (crtc_lut->gamma_size != crtc->gamma_size) {
3441 ret = -EINVAL;
3442 goto out;
3443 }
3444
3445 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3446 r_base = crtc->gamma_store;
3447 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
3448 ret = -EFAULT;
3449 goto out;
3450 }
3451
3452 g_base = r_base + size;
3453 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
3454 ret = -EFAULT;
3455 goto out;
3456 }
3457
3458 b_base = g_base + size;
3459 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
3460 ret = -EFAULT;
3461 goto out;
3462 }
3463 out:
3464 drm_modeset_unlock_all(dev);
3465 return ret;
3466 }
3467
3468 int drm_mode_page_flip_ioctl(struct drm_device *dev,
3469 void *data, struct drm_file *file_priv)
3470 {
3471 struct drm_mode_crtc_page_flip *page_flip = data;
3472 struct drm_mode_object *obj;
3473 struct drm_crtc *crtc;
3474 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
3475 struct drm_pending_vblank_event *e = NULL;
3476 unsigned long flags;
3477 int hdisplay, vdisplay;
3478 int ret = -EINVAL;
3479
3480 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
3481 page_flip->reserved != 0)
3482 return -EINVAL;
3483
3484 obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
3485 if (!obj)
3486 return -EINVAL;
3487 crtc = obj_to_crtc(obj);
3488
3489 mutex_lock(&crtc->mutex);
3490 if (crtc->fb == NULL) {
3491 /* The framebuffer is currently unbound, presumably
3492 * due to a hotplug event, that userspace has not
3493 * yet discovered.
3494 */
3495 ret = -EBUSY;
3496 goto out;
3497 }
3498
3499 if (crtc->funcs->page_flip == NULL)
3500 goto out;
3501
3502 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
3503 if (!fb)
3504 goto out;
3505
3506 hdisplay = crtc->mode.hdisplay;
3507 vdisplay = crtc->mode.vdisplay;
3508
3509 if (crtc->invert_dimensions)
3510 swap(hdisplay, vdisplay);
3511
3512 if (hdisplay > fb->width ||
3513 vdisplay > fb->height ||
3514 crtc->x > fb->width - hdisplay ||
3515 crtc->y > fb->height - vdisplay) {
3516 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
3517 fb->width, fb->height, hdisplay, vdisplay, crtc->x, crtc->y,
3518 crtc->invert_dimensions ? " (inverted)" : "");
3519 ret = -ENOSPC;
3520 goto out;
3521 }
3522
3523 if (crtc->fb->pixel_format != fb->pixel_format) {
3524 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
3525 ret = -EINVAL;
3526 goto out;
3527 }
3528
3529 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3530 ret = -ENOMEM;
3531 spin_lock_irqsave(&dev->event_lock, flags);
3532 if (file_priv->event_space < sizeof e->event) {
3533 spin_unlock_irqrestore(&dev->event_lock, flags);
3534 goto out;
3535 }
3536 file_priv->event_space -= sizeof e->event;
3537 spin_unlock_irqrestore(&dev->event_lock, flags);
3538
3539 e = kzalloc(sizeof *e, GFP_KERNEL);
3540 if (e == NULL) {
3541 spin_lock_irqsave(&dev->event_lock, flags);
3542 file_priv->event_space += sizeof e->event;
3543 spin_unlock_irqrestore(&dev->event_lock, flags);
3544 goto out;
3545 }
3546
3547 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
3548 e->event.base.length = sizeof e->event;
3549 e->event.user_data = page_flip->user_data;
3550 e->base.event = &e->event.base;
3551 e->base.file_priv = file_priv;
3552 e->base.destroy =
3553 (void (*) (struct drm_pending_event *)) kfree;
3554 }
3555
3556 old_fb = crtc->fb;
3557 ret = crtc->funcs->page_flip(crtc, fb, e);
3558 if (ret) {
3559 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3560 spin_lock_irqsave(&dev->event_lock, flags);
3561 file_priv->event_space += sizeof e->event;
3562 spin_unlock_irqrestore(&dev->event_lock, flags);
3563 kfree(e);
3564 }
3565 /* Keep the old fb, don't unref it. */
3566 old_fb = NULL;
3567 } else {
3568 /*
3569 * Warn if the driver hasn't properly updated the crtc->fb
3570 * field to reflect that the new framebuffer is now used.
3571 * Failing to do so will screw with the reference counting
3572 * on framebuffers.
3573 */
3574 WARN_ON(crtc->fb != fb);
3575 /* Unref only the old framebuffer. */
3576 fb = NULL;
3577 }
3578
3579 out:
3580 if (fb)
3581 drm_framebuffer_unreference(fb);
3582 if (old_fb)
3583 drm_framebuffer_unreference(old_fb);
3584 mutex_unlock(&crtc->mutex);
3585
3586 return ret;
3587 }
3588
3589 void drm_mode_config_reset(struct drm_device *dev)
3590 {
3591 struct drm_crtc *crtc;
3592 struct drm_encoder *encoder;
3593 struct drm_connector *connector;
3594
3595 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3596 if (crtc->funcs->reset)
3597 crtc->funcs->reset(crtc);
3598
3599 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
3600 if (encoder->funcs->reset)
3601 encoder->funcs->reset(encoder);
3602
3603 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
3604 connector->status = connector_status_unknown;
3605
3606 if (connector->funcs->reset)
3607 connector->funcs->reset(connector);
3608 }
3609 }
3610 EXPORT_SYMBOL(drm_mode_config_reset);
3611
3612 int drm_mode_create_dumb_ioctl(struct drm_device *dev,
3613 void *data, struct drm_file *file_priv)
3614 {
3615 struct drm_mode_create_dumb *args = data;
3616
3617 if (!dev->driver->dumb_create)
3618 return -ENOSYS;
3619 return dev->driver->dumb_create(file_priv, dev, args);
3620 }
3621
3622 int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
3623 void *data, struct drm_file *file_priv)
3624 {
3625 struct drm_mode_map_dumb *args = data;
3626
3627 /* call driver ioctl to get mmap offset */
3628 if (!dev->driver->dumb_map_offset)
3629 return -ENOSYS;
3630
3631 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
3632 }
3633
3634 int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
3635 void *data, struct drm_file *file_priv)
3636 {
3637 struct drm_mode_destroy_dumb *args = data;
3638
3639 if (!dev->driver->dumb_destroy)
3640 return -ENOSYS;
3641
3642 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
3643 }
3644
3645 /*
3646 * Just need to support RGB formats here for compat with code that doesn't
3647 * use pixel formats directly yet.
3648 */
3649 void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
3650 int *bpp)
3651 {
3652 switch (format) {
3653 case DRM_FORMAT_C8:
3654 case DRM_FORMAT_RGB332:
3655 case DRM_FORMAT_BGR233:
3656 *depth = 8;
3657 *bpp = 8;
3658 break;
3659 case DRM_FORMAT_XRGB1555:
3660 case DRM_FORMAT_XBGR1555:
3661 case DRM_FORMAT_RGBX5551:
3662 case DRM_FORMAT_BGRX5551:
3663 case DRM_FORMAT_ARGB1555:
3664 case DRM_FORMAT_ABGR1555:
3665 case DRM_FORMAT_RGBA5551:
3666 case DRM_FORMAT_BGRA5551:
3667 *depth = 15;
3668 *bpp = 16;
3669 break;
3670 case DRM_FORMAT_RGB565:
3671 case DRM_FORMAT_BGR565:
3672 *depth = 16;
3673 *bpp = 16;
3674 break;
3675 case DRM_FORMAT_RGB888:
3676 case DRM_FORMAT_BGR888:
3677 *depth = 24;
3678 *bpp = 24;
3679 break;
3680 case DRM_FORMAT_XRGB8888:
3681 case DRM_FORMAT_XBGR8888:
3682 case DRM_FORMAT_RGBX8888:
3683 case DRM_FORMAT_BGRX8888:
3684 *depth = 24;
3685 *bpp = 32;
3686 break;
3687 case DRM_FORMAT_XRGB2101010:
3688 case DRM_FORMAT_XBGR2101010:
3689 case DRM_FORMAT_RGBX1010102:
3690 case DRM_FORMAT_BGRX1010102:
3691 case DRM_FORMAT_ARGB2101010:
3692 case DRM_FORMAT_ABGR2101010:
3693 case DRM_FORMAT_RGBA1010102:
3694 case DRM_FORMAT_BGRA1010102:
3695 *depth = 30;
3696 *bpp = 32;
3697 break;
3698 case DRM_FORMAT_ARGB8888:
3699 case DRM_FORMAT_ABGR8888:
3700 case DRM_FORMAT_RGBA8888:
3701 case DRM_FORMAT_BGRA8888:
3702 *depth = 32;
3703 *bpp = 32;
3704 break;
3705 default:
3706 DRM_DEBUG_KMS("unsupported pixel format\n");
3707 *depth = 0;
3708 *bpp = 0;
3709 break;
3710 }
3711 }
3712 EXPORT_SYMBOL(drm_fb_get_bpp_depth);
3713
3714 /**
3715 * drm_format_num_planes - get the number of planes for format
3716 * @format: pixel format (DRM_FORMAT_*)
3717 *
3718 * RETURNS:
3719 * The number of planes used by the specified pixel format.
3720 */
3721 int drm_format_num_planes(uint32_t format)
3722 {
3723 switch (format) {
3724 case DRM_FORMAT_YUV410:
3725 case DRM_FORMAT_YVU410:
3726 case DRM_FORMAT_YUV411:
3727 case DRM_FORMAT_YVU411:
3728 case DRM_FORMAT_YUV420:
3729 case DRM_FORMAT_YVU420:
3730 case DRM_FORMAT_YUV422:
3731 case DRM_FORMAT_YVU422:
3732 case DRM_FORMAT_YUV444:
3733 case DRM_FORMAT_YVU444:
3734 return 3;
3735 case DRM_FORMAT_NV12:
3736 case DRM_FORMAT_NV21:
3737 case DRM_FORMAT_NV16:
3738 case DRM_FORMAT_NV61:
3739 case DRM_FORMAT_NV24:
3740 case DRM_FORMAT_NV42:
3741 return 2;
3742 default:
3743 return 1;
3744 }
3745 }
3746 EXPORT_SYMBOL(drm_format_num_planes);
3747
3748 /**
3749 * drm_format_plane_cpp - determine the bytes per pixel value
3750 * @format: pixel format (DRM_FORMAT_*)
3751 * @plane: plane index
3752 *
3753 * RETURNS:
3754 * The bytes per pixel value for the specified plane.
3755 */
3756 int drm_format_plane_cpp(uint32_t format, int plane)
3757 {
3758 unsigned int depth;
3759 int bpp;
3760
3761 if (plane >= drm_format_num_planes(format))
3762 return 0;
3763
3764 switch (format) {
3765 case DRM_FORMAT_YUYV:
3766 case DRM_FORMAT_YVYU:
3767 case DRM_FORMAT_UYVY:
3768 case DRM_FORMAT_VYUY:
3769 return 2;
3770 case DRM_FORMAT_NV12:
3771 case DRM_FORMAT_NV21:
3772 case DRM_FORMAT_NV16:
3773 case DRM_FORMAT_NV61:
3774 case DRM_FORMAT_NV24:
3775 case DRM_FORMAT_NV42:
3776 return plane ? 2 : 1;
3777 case DRM_FORMAT_YUV410:
3778 case DRM_FORMAT_YVU410:
3779 case DRM_FORMAT_YUV411:
3780 case DRM_FORMAT_YVU411:
3781 case DRM_FORMAT_YUV420:
3782 case DRM_FORMAT_YVU420:
3783 case DRM_FORMAT_YUV422:
3784 case DRM_FORMAT_YVU422:
3785 case DRM_FORMAT_YUV444:
3786 case DRM_FORMAT_YVU444:
3787 return 1;
3788 default:
3789 drm_fb_get_bpp_depth(format, &depth, &bpp);
3790 return bpp >> 3;
3791 }
3792 }
3793 EXPORT_SYMBOL(drm_format_plane_cpp);
3794
3795 /**
3796 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
3797 * @format: pixel format (DRM_FORMAT_*)
3798 *
3799 * RETURNS:
3800 * The horizontal chroma subsampling factor for the
3801 * specified pixel format.
3802 */
3803 int drm_format_horz_chroma_subsampling(uint32_t format)
3804 {
3805 switch (format) {
3806 case DRM_FORMAT_YUV411:
3807 case DRM_FORMAT_YVU411:
3808 case DRM_FORMAT_YUV410:
3809 case DRM_FORMAT_YVU410:
3810 return 4;
3811 case DRM_FORMAT_YUYV:
3812 case DRM_FORMAT_YVYU:
3813 case DRM_FORMAT_UYVY:
3814 case DRM_FORMAT_VYUY:
3815 case DRM_FORMAT_NV12:
3816 case DRM_FORMAT_NV21:
3817 case DRM_FORMAT_NV16:
3818 case DRM_FORMAT_NV61:
3819 case DRM_FORMAT_YUV422:
3820 case DRM_FORMAT_YVU422:
3821 case DRM_FORMAT_YUV420:
3822 case DRM_FORMAT_YVU420:
3823 return 2;
3824 default:
3825 return 1;
3826 }
3827 }
3828 EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
3829
3830 /**
3831 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
3832 * @format: pixel format (DRM_FORMAT_*)
3833 *
3834 * RETURNS:
3835 * The vertical chroma subsampling factor for the
3836 * specified pixel format.
3837 */
3838 int drm_format_vert_chroma_subsampling(uint32_t format)
3839 {
3840 switch (format) {
3841 case DRM_FORMAT_YUV410:
3842 case DRM_FORMAT_YVU410:
3843 return 4;
3844 case DRM_FORMAT_YUV420:
3845 case DRM_FORMAT_YVU420:
3846 case DRM_FORMAT_NV12:
3847 case DRM_FORMAT_NV21:
3848 return 2;
3849 default:
3850 return 1;
3851 }
3852 }
3853 EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
3854
3855 /**
3856 * drm_mode_config_init - initialize DRM mode_configuration structure
3857 * @dev: DRM device
3858 *
3859 * Initialize @dev's mode_config structure, used for tracking the graphics
3860 * configuration of @dev.
3861 *
3862 * Since this initializes the modeset locks, no locking is possible. Which is no
3863 * problem, since this should happen single threaded at init time. It is the
3864 * driver's problem to ensure this guarantee.
3865 *
3866 */
3867 void drm_mode_config_init(struct drm_device *dev)
3868 {
3869 mutex_init(&dev->mode_config.mutex);
3870 mutex_init(&dev->mode_config.idr_mutex);
3871 mutex_init(&dev->mode_config.fb_lock);
3872 INIT_LIST_HEAD(&dev->mode_config.fb_list);
3873 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
3874 INIT_LIST_HEAD(&dev->mode_config.connector_list);
3875 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
3876 INIT_LIST_HEAD(&dev->mode_config.property_list);
3877 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
3878 INIT_LIST_HEAD(&dev->mode_config.plane_list);
3879 idr_init(&dev->mode_config.crtc_idr);
3880
3881 drm_modeset_lock_all(dev);
3882 drm_mode_create_standard_connector_properties(dev);
3883 drm_modeset_unlock_all(dev);
3884
3885 /* Just to be sure */
3886 dev->mode_config.num_fb = 0;
3887 dev->mode_config.num_connector = 0;
3888 dev->mode_config.num_crtc = 0;
3889 dev->mode_config.num_encoder = 0;
3890 }
3891 EXPORT_SYMBOL(drm_mode_config_init);
3892
3893 /**
3894 * drm_mode_config_cleanup - free up DRM mode_config info
3895 * @dev: DRM device
3896 *
3897 * Free up all the connectors and CRTCs associated with this DRM device, then
3898 * free up the framebuffers and associated buffer objects.
3899 *
3900 * Note that since this /should/ happen single-threaded at driver/device
3901 * teardown time, no locking is required. It's the driver's job to ensure that
3902 * this guarantee actually holds true.
3903 *
3904 * FIXME: cleanup any dangling user buffer objects too
3905 */
3906 void drm_mode_config_cleanup(struct drm_device *dev)
3907 {
3908 struct drm_connector *connector, *ot;
3909 struct drm_crtc *crtc, *ct;
3910 struct drm_encoder *encoder, *enct;
3911 struct drm_framebuffer *fb, *fbt;
3912 struct drm_property *property, *pt;
3913 struct drm_property_blob *blob, *bt;
3914 struct drm_plane *plane, *plt;
3915
3916 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
3917 head) {
3918 encoder->funcs->destroy(encoder);
3919 }
3920
3921 list_for_each_entry_safe(connector, ot,
3922 &dev->mode_config.connector_list, head) {
3923 connector->funcs->destroy(connector);
3924 }
3925
3926 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
3927 head) {
3928 drm_property_destroy(dev, property);
3929 }
3930
3931 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
3932 head) {
3933 drm_property_destroy_blob(dev, blob);
3934 }
3935
3936 /*
3937 * Single-threaded teardown context, so it's not required to grab the
3938 * fb_lock to protect against concurrent fb_list access. Contrary, it
3939 * would actually deadlock with the drm_framebuffer_cleanup function.
3940 *
3941 * Also, if there are any framebuffers left, that's a driver leak now,
3942 * so politely WARN about this.
3943 */
3944 WARN_ON(!list_empty(&dev->mode_config.fb_list));
3945 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
3946 drm_framebuffer_remove(fb);
3947 }
3948
3949 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
3950 head) {
3951 plane->funcs->destroy(plane);
3952 }
3953
3954 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
3955 crtc->funcs->destroy(crtc);
3956 }
3957
3958 idr_destroy(&dev->mode_config.crtc_idr);
3959 }
3960 EXPORT_SYMBOL(drm_mode_config_cleanup);
This page took 0.139023 seconds and 6 git commands to generate.