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