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