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