drm: add an fb creation ioctl that takes a pixel format v5
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / drm / drm_crtc.c
1 /*
2  * Copyright (c) 2006-2008 Intel Corporation
3  * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
4  * Copyright (c) 2008 Red Hat Inc.
5  *
6  * DRM core CRTC related functions
7  *
8  * Permission to use, copy, modify, distribute, and sell this software and its
9  * documentation for any purpose is hereby granted without fee, provided that
10  * the above copyright notice appear in all copies and that both that copyright
11  * notice and this permission notice appear in supporting documentation, and
12  * that the name of the copyright holders not be used in advertising or
13  * publicity pertaining to distribution of the software without specific,
14  * written prior permission.  The copyright holders make no representations
15  * about the suitability of this software for any purpose.  It is provided "as
16  * is" without express or implied warranty.
17  *
18  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
20  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
21  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
22  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
23  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24  * OF THIS SOFTWARE.
25  *
26  * Authors:
27  *      Keith Packard
28  *      Eric Anholt <eric@anholt.net>
29  *      Dave Airlie <airlied@linux.ie>
30  *      Jesse Barnes <jesse.barnes@intel.com>
31  */
32 #include <linux/list.h>
33 #include <linux/slab.h>
34 #include <linux/export.h>
35 #include "drm.h"
36 #include "drmP.h"
37 #include "drm_crtc.h"
38 #include "drm_edid.h"
39 #include "drm_fourcc.h"
40
41 struct drm_prop_enum_list {
42         int type;
43         char *name;
44 };
45
46 /* Avoid boilerplate.  I'm tired of typing. */
47 #define DRM_ENUM_NAME_FN(fnname, list)                          \
48         char *fnname(int val)                                   \
49         {                                                       \
50                 int i;                                          \
51                 for (i = 0; i < ARRAY_SIZE(list); i++) {        \
52                         if (list[i].type == val)                \
53                                 return list[i].name;            \
54                 }                                               \
55                 return "(unknown)";                             \
56         }
57
58 /*
59  * Global properties
60  */
61 static struct drm_prop_enum_list drm_dpms_enum_list[] =
62 {       { DRM_MODE_DPMS_ON, "On" },
63         { DRM_MODE_DPMS_STANDBY, "Standby" },
64         { DRM_MODE_DPMS_SUSPEND, "Suspend" },
65         { DRM_MODE_DPMS_OFF, "Off" }
66 };
67
68 DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
69
70 /*
71  * Optional properties
72  */
73 static struct drm_prop_enum_list drm_scaling_mode_enum_list[] =
74 {
75         { DRM_MODE_SCALE_NONE, "None" },
76         { DRM_MODE_SCALE_FULLSCREEN, "Full" },
77         { DRM_MODE_SCALE_CENTER, "Center" },
78         { DRM_MODE_SCALE_ASPECT, "Full aspect" },
79 };
80
81 static struct drm_prop_enum_list drm_dithering_mode_enum_list[] =
82 {
83         { DRM_MODE_DITHERING_OFF, "Off" },
84         { DRM_MODE_DITHERING_ON, "On" },
85         { DRM_MODE_DITHERING_AUTO, "Automatic" },
86 };
87
88 /*
89  * Non-global properties, but "required" for certain connectors.
90  */
91 static struct drm_prop_enum_list drm_dvi_i_select_enum_list[] =
92 {
93         { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
94         { DRM_MODE_SUBCONNECTOR_DVID,      "DVI-D"     }, /* DVI-I  */
95         { DRM_MODE_SUBCONNECTOR_DVIA,      "DVI-A"     }, /* DVI-I  */
96 };
97
98 DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
99
100 static struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] =
101 {
102         { DRM_MODE_SUBCONNECTOR_Unknown,   "Unknown"   }, /* DVI-I and TV-out */
103         { DRM_MODE_SUBCONNECTOR_DVID,      "DVI-D"     }, /* DVI-I  */
104         { DRM_MODE_SUBCONNECTOR_DVIA,      "DVI-A"     }, /* DVI-I  */
105 };
106
107 DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
108                  drm_dvi_i_subconnector_enum_list)
109
110 static struct drm_prop_enum_list drm_tv_select_enum_list[] =
111 {
112         { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
113         { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
114         { DRM_MODE_SUBCONNECTOR_SVIDEO,    "SVIDEO"    }, /* TV-out */
115         { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
116         { DRM_MODE_SUBCONNECTOR_SCART,     "SCART"     }, /* TV-out */
117 };
118
119 DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
120
121 static struct drm_prop_enum_list drm_tv_subconnector_enum_list[] =
122 {
123         { DRM_MODE_SUBCONNECTOR_Unknown,   "Unknown"   }, /* 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_subconnector_name,
131                  drm_tv_subconnector_enum_list)
132
133 static struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
134         { DRM_MODE_DIRTY_OFF,      "Off"      },
135         { DRM_MODE_DIRTY_ON,       "On"       },
136         { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
137 };
138
139 DRM_ENUM_NAME_FN(drm_get_dirty_info_name,
140                  drm_dirty_info_enum_list)
141
142 struct drm_conn_prop_enum_list {
143         int type;
144         char *name;
145         int count;
146 };
147
148 /*
149  * Connector and encoder types.
150  */
151 static struct drm_conn_prop_enum_list drm_connector_enum_list[] =
152 {       { DRM_MODE_CONNECTOR_Unknown, "Unknown", 0 },
153         { DRM_MODE_CONNECTOR_VGA, "VGA", 0 },
154         { DRM_MODE_CONNECTOR_DVII, "DVI-I", 0 },
155         { DRM_MODE_CONNECTOR_DVID, "DVI-D", 0 },
156         { DRM_MODE_CONNECTOR_DVIA, "DVI-A", 0 },
157         { DRM_MODE_CONNECTOR_Composite, "Composite", 0 },
158         { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO", 0 },
159         { DRM_MODE_CONNECTOR_LVDS, "LVDS", 0 },
160         { DRM_MODE_CONNECTOR_Component, "Component", 0 },
161         { DRM_MODE_CONNECTOR_9PinDIN, "DIN", 0 },
162         { DRM_MODE_CONNECTOR_DisplayPort, "DP", 0 },
163         { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A", 0 },
164         { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B", 0 },
165         { DRM_MODE_CONNECTOR_TV, "TV", 0 },
166         { DRM_MODE_CONNECTOR_eDP, "eDP", 0 },
167         { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual", 0},
168 };
169
170 static struct drm_prop_enum_list drm_encoder_enum_list[] =
171 {       { DRM_MODE_ENCODER_NONE, "None" },
172         { DRM_MODE_ENCODER_DAC, "DAC" },
173         { DRM_MODE_ENCODER_TMDS, "TMDS" },
174         { DRM_MODE_ENCODER_LVDS, "LVDS" },
175         { DRM_MODE_ENCODER_TVDAC, "TV" },
176         { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
177 };
178
179 char *drm_get_encoder_name(struct drm_encoder *encoder)
180 {
181         static char buf[32];
182
183         snprintf(buf, 32, "%s-%d",
184                  drm_encoder_enum_list[encoder->encoder_type].name,
185                  encoder->base.id);
186         return buf;
187 }
188 EXPORT_SYMBOL(drm_get_encoder_name);
189
190 char *drm_get_connector_name(struct drm_connector *connector)
191 {
192         static char buf[32];
193
194         snprintf(buf, 32, "%s-%d",
195                  drm_connector_enum_list[connector->connector_type].name,
196                  connector->connector_type_id);
197         return buf;
198 }
199 EXPORT_SYMBOL(drm_get_connector_name);
200
201 char *drm_get_connector_status_name(enum drm_connector_status status)
202 {
203         if (status == connector_status_connected)
204                 return "connected";
205         else if (status == connector_status_disconnected)
206                 return "disconnected";
207         else
208                 return "unknown";
209 }
210
211 /**
212  * drm_mode_object_get - allocate a new identifier
213  * @dev: DRM device
214  * @ptr: object pointer, used to generate unique ID
215  * @type: object type
216  *
217  * LOCKING:
218  *
219  * Create a unique identifier based on @ptr in @dev's identifier space.  Used
220  * for tracking modes, CRTCs and connectors.
221  *
222  * RETURNS:
223  * New unique (relative to other objects in @dev) integer identifier for the
224  * object.
225  */
226 static int drm_mode_object_get(struct drm_device *dev,
227                                struct drm_mode_object *obj, uint32_t obj_type)
228 {
229         int new_id = 0;
230         int ret;
231
232 again:
233         if (idr_pre_get(&dev->mode_config.crtc_idr, GFP_KERNEL) == 0) {
234                 DRM_ERROR("Ran out memory getting a mode number\n");
235                 return -EINVAL;
236         }
237
238         mutex_lock(&dev->mode_config.idr_mutex);
239         ret = idr_get_new_above(&dev->mode_config.crtc_idr, obj, 1, &new_id);
240         mutex_unlock(&dev->mode_config.idr_mutex);
241         if (ret == -EAGAIN)
242                 goto again;
243
244         obj->id = new_id;
245         obj->type = obj_type;
246         return 0;
247 }
248
249 /**
250  * drm_mode_object_put - free an identifer
251  * @dev: DRM device
252  * @id: ID to free
253  *
254  * LOCKING:
255  * Caller must hold DRM mode_config lock.
256  *
257  * Free @id from @dev's unique identifier pool.
258  */
259 static void drm_mode_object_put(struct drm_device *dev,
260                                 struct drm_mode_object *object)
261 {
262         mutex_lock(&dev->mode_config.idr_mutex);
263         idr_remove(&dev->mode_config.crtc_idr, object->id);
264         mutex_unlock(&dev->mode_config.idr_mutex);
265 }
266
267 struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
268                 uint32_t id, uint32_t type)
269 {
270         struct drm_mode_object *obj = NULL;
271
272         mutex_lock(&dev->mode_config.idr_mutex);
273         obj = idr_find(&dev->mode_config.crtc_idr, id);
274         if (!obj || (obj->type != type) || (obj->id != id))
275                 obj = NULL;
276         mutex_unlock(&dev->mode_config.idr_mutex);
277
278         return obj;
279 }
280 EXPORT_SYMBOL(drm_mode_object_find);
281
282 /**
283  * drm_framebuffer_init - initialize a framebuffer
284  * @dev: DRM device
285  *
286  * LOCKING:
287  * Caller must hold mode config lock.
288  *
289  * Allocates an ID for the framebuffer's parent mode object, sets its mode
290  * functions & device file and adds it to the master fd list.
291  *
292  * RETURNS:
293  * Zero on success, error code on failure.
294  */
295 int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
296                          const struct drm_framebuffer_funcs *funcs)
297 {
298         int ret;
299
300         ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
301         if (ret) {
302                 return ret;
303         }
304
305         fb->dev = dev;
306         fb->funcs = funcs;
307         dev->mode_config.num_fb++;
308         list_add(&fb->head, &dev->mode_config.fb_list);
309
310         return 0;
311 }
312 EXPORT_SYMBOL(drm_framebuffer_init);
313
314 /**
315  * drm_framebuffer_cleanup - remove a framebuffer object
316  * @fb: framebuffer to remove
317  *
318  * LOCKING:
319  * Caller must hold mode config lock.
320  *
321  * Scans all the CRTCs in @dev's mode_config.  If they're using @fb, removes
322  * it, setting it to NULL.
323  */
324 void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
325 {
326         struct drm_device *dev = fb->dev;
327         struct drm_crtc *crtc;
328         struct drm_plane *plane;
329         struct drm_mode_set set;
330         int ret;
331
332         /* remove from any CRTC */
333         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
334                 if (crtc->fb == fb) {
335                         /* should turn off the crtc */
336                         memset(&set, 0, sizeof(struct drm_mode_set));
337                         set.crtc = crtc;
338                         set.fb = NULL;
339                         ret = crtc->funcs->set_config(&set);
340                         if (ret)
341                                 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
342                 }
343         }
344
345         list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
346                 if (plane->fb == fb) {
347                         /* should turn off the crtc */
348                         ret = plane->funcs->disable_plane(plane);
349                         if (ret)
350                                 DRM_ERROR("failed to disable plane with busy fb\n");
351                 }
352         }
353
354         drm_mode_object_put(dev, &fb->base);
355         list_del(&fb->head);
356         dev->mode_config.num_fb--;
357 }
358 EXPORT_SYMBOL(drm_framebuffer_cleanup);
359
360 /**
361  * drm_crtc_init - Initialise a new CRTC object
362  * @dev: DRM device
363  * @crtc: CRTC object to init
364  * @funcs: callbacks for the new CRTC
365  *
366  * LOCKING:
367  * Caller must hold mode config lock.
368  *
369  * Inits a new object created as base part of an driver crtc object.
370  */
371 void drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
372                    const struct drm_crtc_funcs *funcs)
373 {
374         crtc->dev = dev;
375         crtc->funcs = funcs;
376
377         mutex_lock(&dev->mode_config.mutex);
378         drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
379
380         list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
381         dev->mode_config.num_crtc++;
382         mutex_unlock(&dev->mode_config.mutex);
383 }
384 EXPORT_SYMBOL(drm_crtc_init);
385
386 /**
387  * drm_crtc_cleanup - Cleans up the core crtc usage.
388  * @crtc: CRTC to cleanup
389  *
390  * LOCKING:
391  * Caller must hold mode config lock.
392  *
393  * Cleanup @crtc. Removes from drm modesetting space
394  * does NOT free object, caller does that.
395  */
396 void drm_crtc_cleanup(struct drm_crtc *crtc)
397 {
398         struct drm_device *dev = crtc->dev;
399
400         if (crtc->gamma_store) {
401                 kfree(crtc->gamma_store);
402                 crtc->gamma_store = NULL;
403         }
404
405         drm_mode_object_put(dev, &crtc->base);
406         list_del(&crtc->head);
407         dev->mode_config.num_crtc--;
408 }
409 EXPORT_SYMBOL(drm_crtc_cleanup);
410
411 /**
412  * drm_mode_probed_add - add a mode to a connector's probed mode list
413  * @connector: connector the new mode
414  * @mode: mode data
415  *
416  * LOCKING:
417  * Caller must hold mode config lock.
418  *
419  * Add @mode to @connector's mode list for later use.
420  */
421 void drm_mode_probed_add(struct drm_connector *connector,
422                          struct drm_display_mode *mode)
423 {
424         list_add(&mode->head, &connector->probed_modes);
425 }
426 EXPORT_SYMBOL(drm_mode_probed_add);
427
428 /**
429  * drm_mode_remove - remove and free a mode
430  * @connector: connector list to modify
431  * @mode: mode to remove
432  *
433  * LOCKING:
434  * Caller must hold mode config lock.
435  *
436  * Remove @mode from @connector's mode list, then free it.
437  */
438 void drm_mode_remove(struct drm_connector *connector,
439                      struct drm_display_mode *mode)
440 {
441         list_del(&mode->head);
442         kfree(mode);
443 }
444 EXPORT_SYMBOL(drm_mode_remove);
445
446 /**
447  * drm_connector_init - Init a preallocated connector
448  * @dev: DRM device
449  * @connector: the connector to init
450  * @funcs: callbacks for this connector
451  * @name: user visible name of the connector
452  *
453  * LOCKING:
454  * Caller must hold @dev's mode_config lock.
455  *
456  * Initialises a preallocated connector. Connectors should be
457  * subclassed as part of driver connector objects.
458  */
459 void drm_connector_init(struct drm_device *dev,
460                      struct drm_connector *connector,
461                      const struct drm_connector_funcs *funcs,
462                      int connector_type)
463 {
464         mutex_lock(&dev->mode_config.mutex);
465
466         connector->dev = dev;
467         connector->funcs = funcs;
468         drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
469         connector->connector_type = connector_type;
470         connector->connector_type_id =
471                 ++drm_connector_enum_list[connector_type].count; /* TODO */
472         INIT_LIST_HEAD(&connector->user_modes);
473         INIT_LIST_HEAD(&connector->probed_modes);
474         INIT_LIST_HEAD(&connector->modes);
475         connector->edid_blob_ptr = NULL;
476
477         list_add_tail(&connector->head, &dev->mode_config.connector_list);
478         dev->mode_config.num_connector++;
479
480         if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
481                 drm_connector_attach_property(connector,
482                                               dev->mode_config.edid_property,
483                                               0);
484
485         drm_connector_attach_property(connector,
486                                       dev->mode_config.dpms_property, 0);
487
488         mutex_unlock(&dev->mode_config.mutex);
489 }
490 EXPORT_SYMBOL(drm_connector_init);
491
492 /**
493  * drm_connector_cleanup - cleans up an initialised connector
494  * @connector: connector to cleanup
495  *
496  * LOCKING:
497  * Caller must hold @dev's mode_config lock.
498  *
499  * Cleans up the connector but doesn't free the object.
500  */
501 void drm_connector_cleanup(struct drm_connector *connector)
502 {
503         struct drm_device *dev = connector->dev;
504         struct drm_display_mode *mode, *t;
505
506         list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
507                 drm_mode_remove(connector, mode);
508
509         list_for_each_entry_safe(mode, t, &connector->modes, head)
510                 drm_mode_remove(connector, mode);
511
512         list_for_each_entry_safe(mode, t, &connector->user_modes, head)
513                 drm_mode_remove(connector, mode);
514
515         mutex_lock(&dev->mode_config.mutex);
516         drm_mode_object_put(dev, &connector->base);
517         list_del(&connector->head);
518         dev->mode_config.num_connector--;
519         mutex_unlock(&dev->mode_config.mutex);
520 }
521 EXPORT_SYMBOL(drm_connector_cleanup);
522
523 void drm_encoder_init(struct drm_device *dev,
524                       struct drm_encoder *encoder,
525                       const struct drm_encoder_funcs *funcs,
526                       int encoder_type)
527 {
528         mutex_lock(&dev->mode_config.mutex);
529
530         encoder->dev = dev;
531
532         drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
533         encoder->encoder_type = encoder_type;
534         encoder->funcs = funcs;
535
536         list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
537         dev->mode_config.num_encoder++;
538
539         mutex_unlock(&dev->mode_config.mutex);
540 }
541 EXPORT_SYMBOL(drm_encoder_init);
542
543 void drm_encoder_cleanup(struct drm_encoder *encoder)
544 {
545         struct drm_device *dev = encoder->dev;
546         mutex_lock(&dev->mode_config.mutex);
547         drm_mode_object_put(dev, &encoder->base);
548         list_del(&encoder->head);
549         dev->mode_config.num_encoder--;
550         mutex_unlock(&dev->mode_config.mutex);
551 }
552 EXPORT_SYMBOL(drm_encoder_cleanup);
553
554 int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
555                    unsigned long possible_crtcs,
556                    const struct drm_plane_funcs *funcs,
557                    uint32_t *formats, uint32_t format_count)
558 {
559         mutex_lock(&dev->mode_config.mutex);
560
561         plane->dev = dev;
562         drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
563         plane->funcs = funcs;
564         plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
565                                       GFP_KERNEL);
566         if (!plane->format_types) {
567                 DRM_DEBUG_KMS("out of memory when allocating plane\n");
568                 drm_mode_object_put(dev, &plane->base);
569                 return -ENOMEM;
570         }
571
572         memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
573         plane->format_count = format_count;
574         plane->possible_crtcs = possible_crtcs;
575
576         list_add_tail(&plane->head, &dev->mode_config.plane_list);
577         dev->mode_config.num_plane++;
578
579         mutex_unlock(&dev->mode_config.mutex);
580
581         return 0;
582 }
583 EXPORT_SYMBOL(drm_plane_init);
584
585 void drm_plane_cleanup(struct drm_plane *plane)
586 {
587         struct drm_device *dev = plane->dev;
588
589         mutex_lock(&dev->mode_config.mutex);
590         kfree(plane->format_types);
591         drm_mode_object_put(dev, &plane->base);
592         list_del(&plane->head);
593         dev->mode_config.num_plane--;
594         mutex_unlock(&dev->mode_config.mutex);
595 }
596 EXPORT_SYMBOL(drm_plane_cleanup);
597
598 /**
599  * drm_mode_create - create a new display mode
600  * @dev: DRM device
601  *
602  * LOCKING:
603  * Caller must hold DRM mode_config lock.
604  *
605  * Create a new drm_display_mode, give it an ID, and return it.
606  *
607  * RETURNS:
608  * Pointer to new mode on success, NULL on error.
609  */
610 struct drm_display_mode *drm_mode_create(struct drm_device *dev)
611 {
612         struct drm_display_mode *nmode;
613
614         nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
615         if (!nmode)
616                 return NULL;
617
618         drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE);
619         return nmode;
620 }
621 EXPORT_SYMBOL(drm_mode_create);
622
623 /**
624  * drm_mode_destroy - remove a mode
625  * @dev: DRM device
626  * @mode: mode to remove
627  *
628  * LOCKING:
629  * Caller must hold mode config lock.
630  *
631  * Free @mode's unique identifier, then free it.
632  */
633 void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
634 {
635         drm_mode_object_put(dev, &mode->base);
636
637         kfree(mode);
638 }
639 EXPORT_SYMBOL(drm_mode_destroy);
640
641 static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
642 {
643         struct drm_property *edid;
644         struct drm_property *dpms;
645         int i;
646
647         /*
648          * Standard properties (apply to all connectors)
649          */
650         edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
651                                    DRM_MODE_PROP_IMMUTABLE,
652                                    "EDID", 0);
653         dev->mode_config.edid_property = edid;
654
655         dpms = drm_property_create(dev, DRM_MODE_PROP_ENUM,
656                                    "DPMS", ARRAY_SIZE(drm_dpms_enum_list));
657         for (i = 0; i < ARRAY_SIZE(drm_dpms_enum_list); i++)
658                 drm_property_add_enum(dpms, i, drm_dpms_enum_list[i].type,
659                                       drm_dpms_enum_list[i].name);
660         dev->mode_config.dpms_property = dpms;
661
662         return 0;
663 }
664
665 /**
666  * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
667  * @dev: DRM device
668  *
669  * Called by a driver the first time a DVI-I connector is made.
670  */
671 int drm_mode_create_dvi_i_properties(struct drm_device *dev)
672 {
673         struct drm_property *dvi_i_selector;
674         struct drm_property *dvi_i_subconnector;
675         int i;
676
677         if (dev->mode_config.dvi_i_select_subconnector_property)
678                 return 0;
679
680         dvi_i_selector =
681                 drm_property_create(dev, DRM_MODE_PROP_ENUM,
682                                     "select subconnector",
683                                     ARRAY_SIZE(drm_dvi_i_select_enum_list));
684         for (i = 0; i < ARRAY_SIZE(drm_dvi_i_select_enum_list); i++)
685                 drm_property_add_enum(dvi_i_selector, i,
686                                       drm_dvi_i_select_enum_list[i].type,
687                                       drm_dvi_i_select_enum_list[i].name);
688         dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
689
690         dvi_i_subconnector =
691                 drm_property_create(dev, DRM_MODE_PROP_ENUM |
692                                     DRM_MODE_PROP_IMMUTABLE,
693                                     "subconnector",
694                                     ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
695         for (i = 0; i < ARRAY_SIZE(drm_dvi_i_subconnector_enum_list); i++)
696                 drm_property_add_enum(dvi_i_subconnector, i,
697                                       drm_dvi_i_subconnector_enum_list[i].type,
698                                       drm_dvi_i_subconnector_enum_list[i].name);
699         dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
700
701         return 0;
702 }
703 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
704
705 /**
706  * drm_create_tv_properties - create TV specific connector properties
707  * @dev: DRM device
708  * @num_modes: number of different TV formats (modes) supported
709  * @modes: array of pointers to strings containing name of each format
710  *
711  * Called by a driver's TV initialization routine, this function creates
712  * the TV specific connector properties for a given device.  Caller is
713  * responsible for allocating a list of format names and passing them to
714  * this routine.
715  */
716 int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
717                                   char *modes[])
718 {
719         struct drm_property *tv_selector;
720         struct drm_property *tv_subconnector;
721         int i;
722
723         if (dev->mode_config.tv_select_subconnector_property)
724                 return 0;
725
726         /*
727          * Basic connector properties
728          */
729         tv_selector = drm_property_create(dev, DRM_MODE_PROP_ENUM,
730                                           "select subconnector",
731                                           ARRAY_SIZE(drm_tv_select_enum_list));
732         for (i = 0; i < ARRAY_SIZE(drm_tv_select_enum_list); i++)
733                 drm_property_add_enum(tv_selector, i,
734                                       drm_tv_select_enum_list[i].type,
735                                       drm_tv_select_enum_list[i].name);
736         dev->mode_config.tv_select_subconnector_property = tv_selector;
737
738         tv_subconnector =
739                 drm_property_create(dev, DRM_MODE_PROP_ENUM |
740                                     DRM_MODE_PROP_IMMUTABLE, "subconnector",
741                                     ARRAY_SIZE(drm_tv_subconnector_enum_list));
742         for (i = 0; i < ARRAY_SIZE(drm_tv_subconnector_enum_list); i++)
743                 drm_property_add_enum(tv_subconnector, i,
744                                       drm_tv_subconnector_enum_list[i].type,
745                                       drm_tv_subconnector_enum_list[i].name);
746         dev->mode_config.tv_subconnector_property = tv_subconnector;
747
748         /*
749          * Other, TV specific properties: margins & TV modes.
750          */
751         dev->mode_config.tv_left_margin_property =
752                 drm_property_create(dev, DRM_MODE_PROP_RANGE,
753                                     "left margin", 2);
754         dev->mode_config.tv_left_margin_property->values[0] = 0;
755         dev->mode_config.tv_left_margin_property->values[1] = 100;
756
757         dev->mode_config.tv_right_margin_property =
758                 drm_property_create(dev, DRM_MODE_PROP_RANGE,
759                                     "right margin", 2);
760         dev->mode_config.tv_right_margin_property->values[0] = 0;
761         dev->mode_config.tv_right_margin_property->values[1] = 100;
762
763         dev->mode_config.tv_top_margin_property =
764                 drm_property_create(dev, DRM_MODE_PROP_RANGE,
765                                     "top margin", 2);
766         dev->mode_config.tv_top_margin_property->values[0] = 0;
767         dev->mode_config.tv_top_margin_property->values[1] = 100;
768
769         dev->mode_config.tv_bottom_margin_property =
770                 drm_property_create(dev, DRM_MODE_PROP_RANGE,
771                                     "bottom margin", 2);
772         dev->mode_config.tv_bottom_margin_property->values[0] = 0;
773         dev->mode_config.tv_bottom_margin_property->values[1] = 100;
774
775         dev->mode_config.tv_mode_property =
776                 drm_property_create(dev, DRM_MODE_PROP_ENUM,
777                                     "mode", num_modes);
778         for (i = 0; i < num_modes; i++)
779                 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
780                                       i, modes[i]);
781
782         dev->mode_config.tv_brightness_property =
783                 drm_property_create(dev, DRM_MODE_PROP_RANGE,
784                                     "brightness", 2);
785         dev->mode_config.tv_brightness_property->values[0] = 0;
786         dev->mode_config.tv_brightness_property->values[1] = 100;
787
788         dev->mode_config.tv_contrast_property =
789                 drm_property_create(dev, DRM_MODE_PROP_RANGE,
790                                     "contrast", 2);
791         dev->mode_config.tv_contrast_property->values[0] = 0;
792         dev->mode_config.tv_contrast_property->values[1] = 100;
793
794         dev->mode_config.tv_flicker_reduction_property =
795                 drm_property_create(dev, DRM_MODE_PROP_RANGE,
796                                     "flicker reduction", 2);
797         dev->mode_config.tv_flicker_reduction_property->values[0] = 0;
798         dev->mode_config.tv_flicker_reduction_property->values[1] = 100;
799
800         dev->mode_config.tv_overscan_property =
801                 drm_property_create(dev, DRM_MODE_PROP_RANGE,
802                                     "overscan", 2);
803         dev->mode_config.tv_overscan_property->values[0] = 0;
804         dev->mode_config.tv_overscan_property->values[1] = 100;
805
806         dev->mode_config.tv_saturation_property =
807                 drm_property_create(dev, DRM_MODE_PROP_RANGE,
808                                     "saturation", 2);
809         dev->mode_config.tv_saturation_property->values[0] = 0;
810         dev->mode_config.tv_saturation_property->values[1] = 100;
811
812         dev->mode_config.tv_hue_property =
813                 drm_property_create(dev, DRM_MODE_PROP_RANGE,
814                                     "hue", 2);
815         dev->mode_config.tv_hue_property->values[0] = 0;
816         dev->mode_config.tv_hue_property->values[1] = 100;
817
818         return 0;
819 }
820 EXPORT_SYMBOL(drm_mode_create_tv_properties);
821
822 /**
823  * drm_mode_create_scaling_mode_property - create scaling mode property
824  * @dev: DRM device
825  *
826  * Called by a driver the first time it's needed, must be attached to desired
827  * connectors.
828  */
829 int drm_mode_create_scaling_mode_property(struct drm_device *dev)
830 {
831         struct drm_property *scaling_mode;
832         int i;
833
834         if (dev->mode_config.scaling_mode_property)
835                 return 0;
836
837         scaling_mode =
838                 drm_property_create(dev, DRM_MODE_PROP_ENUM, "scaling mode",
839                                     ARRAY_SIZE(drm_scaling_mode_enum_list));
840         for (i = 0; i < ARRAY_SIZE(drm_scaling_mode_enum_list); i++)
841                 drm_property_add_enum(scaling_mode, i,
842                                       drm_scaling_mode_enum_list[i].type,
843                                       drm_scaling_mode_enum_list[i].name);
844
845         dev->mode_config.scaling_mode_property = scaling_mode;
846
847         return 0;
848 }
849 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
850
851 /**
852  * drm_mode_create_dithering_property - create dithering property
853  * @dev: DRM device
854  *
855  * Called by a driver the first time it's needed, must be attached to desired
856  * connectors.
857  */
858 int drm_mode_create_dithering_property(struct drm_device *dev)
859 {
860         struct drm_property *dithering_mode;
861         int i;
862
863         if (dev->mode_config.dithering_mode_property)
864                 return 0;
865
866         dithering_mode =
867                 drm_property_create(dev, DRM_MODE_PROP_ENUM, "dithering",
868                                     ARRAY_SIZE(drm_dithering_mode_enum_list));
869         for (i = 0; i < ARRAY_SIZE(drm_dithering_mode_enum_list); i++)
870                 drm_property_add_enum(dithering_mode, i,
871                                       drm_dithering_mode_enum_list[i].type,
872                                       drm_dithering_mode_enum_list[i].name);
873         dev->mode_config.dithering_mode_property = dithering_mode;
874
875         return 0;
876 }
877 EXPORT_SYMBOL(drm_mode_create_dithering_property);
878
879 /**
880  * drm_mode_create_dirty_property - create dirty property
881  * @dev: DRM device
882  *
883  * Called by a driver the first time it's needed, must be attached to desired
884  * connectors.
885  */
886 int drm_mode_create_dirty_info_property(struct drm_device *dev)
887 {
888         struct drm_property *dirty_info;
889         int i;
890
891         if (dev->mode_config.dirty_info_property)
892                 return 0;
893
894         dirty_info =
895                 drm_property_create(dev, DRM_MODE_PROP_ENUM |
896                                     DRM_MODE_PROP_IMMUTABLE,
897                                     "dirty",
898                                     ARRAY_SIZE(drm_dirty_info_enum_list));
899         for (i = 0; i < ARRAY_SIZE(drm_dirty_info_enum_list); i++)
900                 drm_property_add_enum(dirty_info, i,
901                                       drm_dirty_info_enum_list[i].type,
902                                       drm_dirty_info_enum_list[i].name);
903         dev->mode_config.dirty_info_property = dirty_info;
904
905         return 0;
906 }
907 EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
908
909 /**
910  * drm_mode_config_init - initialize DRM mode_configuration structure
911  * @dev: DRM device
912  *
913  * LOCKING:
914  * None, should happen single threaded at init time.
915  *
916  * Initialize @dev's mode_config structure, used for tracking the graphics
917  * configuration of @dev.
918  */
919 void drm_mode_config_init(struct drm_device *dev)
920 {
921         mutex_init(&dev->mode_config.mutex);
922         mutex_init(&dev->mode_config.idr_mutex);
923         INIT_LIST_HEAD(&dev->mode_config.fb_list);
924         INIT_LIST_HEAD(&dev->mode_config.crtc_list);
925         INIT_LIST_HEAD(&dev->mode_config.connector_list);
926         INIT_LIST_HEAD(&dev->mode_config.encoder_list);
927         INIT_LIST_HEAD(&dev->mode_config.property_list);
928         INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
929         INIT_LIST_HEAD(&dev->mode_config.plane_list);
930         idr_init(&dev->mode_config.crtc_idr);
931
932         mutex_lock(&dev->mode_config.mutex);
933         drm_mode_create_standard_connector_properties(dev);
934         mutex_unlock(&dev->mode_config.mutex);
935
936         /* Just to be sure */
937         dev->mode_config.num_fb = 0;
938         dev->mode_config.num_connector = 0;
939         dev->mode_config.num_crtc = 0;
940         dev->mode_config.num_encoder = 0;
941 }
942 EXPORT_SYMBOL(drm_mode_config_init);
943
944 int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
945 {
946         uint32_t total_objects = 0;
947
948         total_objects += dev->mode_config.num_crtc;
949         total_objects += dev->mode_config.num_connector;
950         total_objects += dev->mode_config.num_encoder;
951
952         group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
953         if (!group->id_list)
954                 return -ENOMEM;
955
956         group->num_crtcs = 0;
957         group->num_connectors = 0;
958         group->num_encoders = 0;
959         return 0;
960 }
961
962 int drm_mode_group_init_legacy_group(struct drm_device *dev,
963                                      struct drm_mode_group *group)
964 {
965         struct drm_crtc *crtc;
966         struct drm_encoder *encoder;
967         struct drm_connector *connector;
968         int ret;
969
970         if ((ret = drm_mode_group_init(dev, group)))
971                 return ret;
972
973         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
974                 group->id_list[group->num_crtcs++] = crtc->base.id;
975
976         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
977                 group->id_list[group->num_crtcs + group->num_encoders++] =
978                 encoder->base.id;
979
980         list_for_each_entry(connector, &dev->mode_config.connector_list, head)
981                 group->id_list[group->num_crtcs + group->num_encoders +
982                                group->num_connectors++] = connector->base.id;
983
984         return 0;
985 }
986
987 /**
988  * drm_mode_config_cleanup - free up DRM mode_config info
989  * @dev: DRM device
990  *
991  * LOCKING:
992  * Caller must hold mode config lock.
993  *
994  * Free up all the connectors and CRTCs associated with this DRM device, then
995  * free up the framebuffers and associated buffer objects.
996  *
997  * FIXME: cleanup any dangling user buffer objects too
998  */
999 void drm_mode_config_cleanup(struct drm_device *dev)
1000 {
1001         struct drm_connector *connector, *ot;
1002         struct drm_crtc *crtc, *ct;
1003         struct drm_encoder *encoder, *enct;
1004         struct drm_framebuffer *fb, *fbt;
1005         struct drm_property *property, *pt;
1006         struct drm_plane *plane, *plt;
1007
1008         list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
1009                                  head) {
1010                 encoder->funcs->destroy(encoder);
1011         }
1012
1013         list_for_each_entry_safe(connector, ot,
1014                                  &dev->mode_config.connector_list, head) {
1015                 connector->funcs->destroy(connector);
1016         }
1017
1018         list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
1019                                  head) {
1020                 drm_property_destroy(dev, property);
1021         }
1022
1023         list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
1024                 fb->funcs->destroy(fb);
1025         }
1026
1027         list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
1028                 crtc->funcs->destroy(crtc);
1029         }
1030
1031         list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
1032                                  head) {
1033                 plane->funcs->destroy(plane);
1034         }
1035 }
1036 EXPORT_SYMBOL(drm_mode_config_cleanup);
1037
1038 /**
1039  * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1040  * @out: drm_mode_modeinfo struct to return to the user
1041  * @in: drm_display_mode to use
1042  *
1043  * LOCKING:
1044  * None.
1045  *
1046  * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1047  * the user.
1048  */
1049 void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1050                                struct drm_display_mode *in)
1051 {
1052         out->clock = in->clock;
1053         out->hdisplay = in->hdisplay;
1054         out->hsync_start = in->hsync_start;
1055         out->hsync_end = in->hsync_end;
1056         out->htotal = in->htotal;
1057         out->hskew = in->hskew;
1058         out->vdisplay = in->vdisplay;
1059         out->vsync_start = in->vsync_start;
1060         out->vsync_end = in->vsync_end;
1061         out->vtotal = in->vtotal;
1062         out->vscan = in->vscan;
1063         out->vrefresh = in->vrefresh;
1064         out->flags = in->flags;
1065         out->type = in->type;
1066         strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1067         out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1068 }
1069
1070 /**
1071  * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
1072  * @out: drm_display_mode to return to the user
1073  * @in: drm_mode_modeinfo to use
1074  *
1075  * LOCKING:
1076  * None.
1077  *
1078  * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1079  * the caller.
1080  */
1081 void drm_crtc_convert_umode(struct drm_display_mode *out,
1082                             struct drm_mode_modeinfo *in)
1083 {
1084         out->clock = in->clock;
1085         out->hdisplay = in->hdisplay;
1086         out->hsync_start = in->hsync_start;
1087         out->hsync_end = in->hsync_end;
1088         out->htotal = in->htotal;
1089         out->hskew = in->hskew;
1090         out->vdisplay = in->vdisplay;
1091         out->vsync_start = in->vsync_start;
1092         out->vsync_end = in->vsync_end;
1093         out->vtotal = in->vtotal;
1094         out->vscan = in->vscan;
1095         out->vrefresh = in->vrefresh;
1096         out->flags = in->flags;
1097         out->type = in->type;
1098         strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1099         out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1100 }
1101
1102 /**
1103  * drm_mode_getresources - get graphics configuration
1104  * @inode: inode from the ioctl
1105  * @filp: file * from the ioctl
1106  * @cmd: cmd from ioctl
1107  * @arg: arg from ioctl
1108  *
1109  * LOCKING:
1110  * Takes mode config lock.
1111  *
1112  * Construct a set of configuration description structures and return
1113  * them to the user, including CRTC, connector and framebuffer configuration.
1114  *
1115  * Called by the user via ioctl.
1116  *
1117  * RETURNS:
1118  * Zero on success, errno on failure.
1119  */
1120 int drm_mode_getresources(struct drm_device *dev, void *data,
1121                           struct drm_file *file_priv)
1122 {
1123         struct drm_mode_card_res *card_res = data;
1124         struct list_head *lh;
1125         struct drm_framebuffer *fb;
1126         struct drm_connector *connector;
1127         struct drm_crtc *crtc;
1128         struct drm_encoder *encoder;
1129         int ret = 0;
1130         int connector_count = 0;
1131         int crtc_count = 0;
1132         int fb_count = 0;
1133         int encoder_count = 0;
1134         int copied = 0, i;
1135         uint32_t __user *fb_id;
1136         uint32_t __user *crtc_id;
1137         uint32_t __user *connector_id;
1138         uint32_t __user *encoder_id;
1139         struct drm_mode_group *mode_group;
1140
1141         if (!drm_core_check_feature(dev, DRIVER_MODESET))
1142                 return -EINVAL;
1143
1144         mutex_lock(&dev->mode_config.mutex);
1145
1146         /*
1147          * For the non-control nodes we need to limit the list of resources
1148          * by IDs in the group list for this node
1149          */
1150         list_for_each(lh, &file_priv->fbs)
1151                 fb_count++;
1152
1153         mode_group = &file_priv->master->minor->mode_group;
1154         if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1155
1156                 list_for_each(lh, &dev->mode_config.crtc_list)
1157                         crtc_count++;
1158
1159                 list_for_each(lh, &dev->mode_config.connector_list)
1160                         connector_count++;
1161
1162                 list_for_each(lh, &dev->mode_config.encoder_list)
1163                         encoder_count++;
1164         } else {
1165
1166                 crtc_count = mode_group->num_crtcs;
1167                 connector_count = mode_group->num_connectors;
1168                 encoder_count = mode_group->num_encoders;
1169         }
1170
1171         card_res->max_height = dev->mode_config.max_height;
1172         card_res->min_height = dev->mode_config.min_height;
1173         card_res->max_width = dev->mode_config.max_width;
1174         card_res->min_width = dev->mode_config.min_width;
1175
1176         /* handle this in 4 parts */
1177         /* FBs */
1178         if (card_res->count_fbs >= fb_count) {
1179                 copied = 0;
1180                 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1181                 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1182                         if (put_user(fb->base.id, fb_id + copied)) {
1183                                 ret = -EFAULT;
1184                                 goto out;
1185                         }
1186                         copied++;
1187                 }
1188         }
1189         card_res->count_fbs = fb_count;
1190
1191         /* CRTCs */
1192         if (card_res->count_crtcs >= crtc_count) {
1193                 copied = 0;
1194                 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1195                 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1196                         list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1197                                             head) {
1198                                 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
1199                                 if (put_user(crtc->base.id, crtc_id + copied)) {
1200                                         ret = -EFAULT;
1201                                         goto out;
1202                                 }
1203                                 copied++;
1204                         }
1205                 } else {
1206                         for (i = 0; i < mode_group->num_crtcs; i++) {
1207                                 if (put_user(mode_group->id_list[i],
1208                                              crtc_id + copied)) {
1209                                         ret = -EFAULT;
1210                                         goto out;
1211                                 }
1212                                 copied++;
1213                         }
1214                 }
1215         }
1216         card_res->count_crtcs = crtc_count;
1217
1218         /* Encoders */
1219         if (card_res->count_encoders >= encoder_count) {
1220                 copied = 0;
1221                 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1222                 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1223                         list_for_each_entry(encoder,
1224                                             &dev->mode_config.encoder_list,
1225                                             head) {
1226                                 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1227                                                 drm_get_encoder_name(encoder));
1228                                 if (put_user(encoder->base.id, encoder_id +
1229                                              copied)) {
1230                                         ret = -EFAULT;
1231                                         goto out;
1232                                 }
1233                                 copied++;
1234                         }
1235                 } else {
1236                         for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1237                                 if (put_user(mode_group->id_list[i],
1238                                              encoder_id + copied)) {
1239                                         ret = -EFAULT;
1240                                         goto out;
1241                                 }
1242                                 copied++;
1243                         }
1244
1245                 }
1246         }
1247         card_res->count_encoders = encoder_count;
1248
1249         /* Connectors */
1250         if (card_res->count_connectors >= connector_count) {
1251                 copied = 0;
1252                 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1253                 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1254                         list_for_each_entry(connector,
1255                                             &dev->mode_config.connector_list,
1256                                             head) {
1257                                 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1258                                         connector->base.id,
1259                                         drm_get_connector_name(connector));
1260                                 if (put_user(connector->base.id,
1261                                              connector_id + copied)) {
1262                                         ret = -EFAULT;
1263                                         goto out;
1264                                 }
1265                                 copied++;
1266                         }
1267                 } else {
1268                         int start = mode_group->num_crtcs +
1269                                 mode_group->num_encoders;
1270                         for (i = start; i < start + mode_group->num_connectors; i++) {
1271                                 if (put_user(mode_group->id_list[i],
1272                                              connector_id + copied)) {
1273                                         ret = -EFAULT;
1274                                         goto out;
1275                                 }
1276                                 copied++;
1277                         }
1278                 }
1279         }
1280         card_res->count_connectors = connector_count;
1281
1282         DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
1283                   card_res->count_connectors, card_res->count_encoders);
1284
1285 out:
1286         mutex_unlock(&dev->mode_config.mutex);
1287         return ret;
1288 }
1289
1290 /**
1291  * drm_mode_getcrtc - get CRTC configuration
1292  * @inode: inode from the ioctl
1293  * @filp: file * from the ioctl
1294  * @cmd: cmd from ioctl
1295  * @arg: arg from ioctl
1296  *
1297  * LOCKING:
1298  * Caller? (FIXME)
1299  *
1300  * Construct a CRTC configuration structure to return to the user.
1301  *
1302  * Called by the user via ioctl.
1303  *
1304  * RETURNS:
1305  * Zero on success, errno on failure.
1306  */
1307 int drm_mode_getcrtc(struct drm_device *dev,
1308                      void *data, struct drm_file *file_priv)
1309 {
1310         struct drm_mode_crtc *crtc_resp = data;
1311         struct drm_crtc *crtc;
1312         struct drm_mode_object *obj;
1313         int ret = 0;
1314
1315         if (!drm_core_check_feature(dev, DRIVER_MODESET))
1316                 return -EINVAL;
1317
1318         mutex_lock(&dev->mode_config.mutex);
1319
1320         obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
1321                                    DRM_MODE_OBJECT_CRTC);
1322         if (!obj) {
1323                 ret = -EINVAL;
1324                 goto out;
1325         }
1326         crtc = obj_to_crtc(obj);
1327
1328         crtc_resp->x = crtc->x;
1329         crtc_resp->y = crtc->y;
1330         crtc_resp->gamma_size = crtc->gamma_size;
1331         if (crtc->fb)
1332                 crtc_resp->fb_id = crtc->fb->base.id;
1333         else
1334                 crtc_resp->fb_id = 0;
1335
1336         if (crtc->enabled) {
1337
1338                 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1339                 crtc_resp->mode_valid = 1;
1340
1341         } else {
1342                 crtc_resp->mode_valid = 0;
1343         }
1344
1345 out:
1346         mutex_unlock(&dev->mode_config.mutex);
1347         return ret;
1348 }
1349
1350 /**
1351  * drm_mode_getconnector - get connector configuration
1352  * @inode: inode from the ioctl
1353  * @filp: file * from the ioctl
1354  * @cmd: cmd from ioctl
1355  * @arg: arg from ioctl
1356  *
1357  * LOCKING:
1358  * Caller? (FIXME)
1359  *
1360  * Construct a connector configuration structure to return to the user.
1361  *
1362  * Called by the user via ioctl.
1363  *
1364  * RETURNS:
1365  * Zero on success, errno on failure.
1366  */
1367 int drm_mode_getconnector(struct drm_device *dev, void *data,
1368                           struct drm_file *file_priv)
1369 {
1370         struct drm_mode_get_connector *out_resp = data;
1371         struct drm_mode_object *obj;
1372         struct drm_connector *connector;
1373         struct drm_display_mode *mode;
1374         int mode_count = 0;
1375         int props_count = 0;
1376         int encoders_count = 0;
1377         int ret = 0;
1378         int copied = 0;
1379         int i;
1380         struct drm_mode_modeinfo u_mode;
1381         struct drm_mode_modeinfo __user *mode_ptr;
1382         uint32_t __user *prop_ptr;
1383         uint64_t __user *prop_values;
1384         uint32_t __user *encoder_ptr;
1385
1386         if (!drm_core_check_feature(dev, DRIVER_MODESET))
1387                 return -EINVAL;
1388
1389         memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1390
1391         DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
1392
1393         mutex_lock(&dev->mode_config.mutex);
1394
1395         obj = drm_mode_object_find(dev, out_resp->connector_id,
1396                                    DRM_MODE_OBJECT_CONNECTOR);
1397         if (!obj) {
1398                 ret = -EINVAL;
1399                 goto out;
1400         }
1401         connector = obj_to_connector(obj);
1402
1403         for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
1404                 if (connector->property_ids[i] != 0) {
1405                         props_count++;
1406                 }
1407         }
1408
1409         for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1410                 if (connector->encoder_ids[i] != 0) {
1411                         encoders_count++;
1412                 }
1413         }
1414
1415         if (out_resp->count_modes == 0) {
1416                 connector->funcs->fill_modes(connector,
1417                                              dev->mode_config.max_width,
1418                                              dev->mode_config.max_height);
1419         }
1420
1421         /* delayed so we get modes regardless of pre-fill_modes state */
1422         list_for_each_entry(mode, &connector->modes, head)
1423                 mode_count++;
1424
1425         out_resp->connector_id = connector->base.id;
1426         out_resp->connector_type = connector->connector_type;
1427         out_resp->connector_type_id = connector->connector_type_id;
1428         out_resp->mm_width = connector->display_info.width_mm;
1429         out_resp->mm_height = connector->display_info.height_mm;
1430         out_resp->subpixel = connector->display_info.subpixel_order;
1431         out_resp->connection = connector->status;
1432         if (connector->encoder)
1433                 out_resp->encoder_id = connector->encoder->base.id;
1434         else
1435                 out_resp->encoder_id = 0;
1436
1437         /*
1438          * This ioctl is called twice, once to determine how much space is
1439          * needed, and the 2nd time to fill it.
1440          */
1441         if ((out_resp->count_modes >= mode_count) && mode_count) {
1442                 copied = 0;
1443                 mode_ptr = (struct drm_mode_modeinfo *)(unsigned long)out_resp->modes_ptr;
1444                 list_for_each_entry(mode, &connector->modes, head) {
1445                         drm_crtc_convert_to_umode(&u_mode, mode);
1446                         if (copy_to_user(mode_ptr + copied,
1447                                          &u_mode, sizeof(u_mode))) {
1448                                 ret = -EFAULT;
1449                                 goto out;
1450                         }
1451                         copied++;
1452                 }
1453         }
1454         out_resp->count_modes = mode_count;
1455
1456         if ((out_resp->count_props >= props_count) && props_count) {
1457                 copied = 0;
1458                 prop_ptr = (uint32_t *)(unsigned long)(out_resp->props_ptr);
1459                 prop_values = (uint64_t *)(unsigned long)(out_resp->prop_values_ptr);
1460                 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
1461                         if (connector->property_ids[i] != 0) {
1462                                 if (put_user(connector->property_ids[i],
1463                                              prop_ptr + copied)) {
1464                                         ret = -EFAULT;
1465                                         goto out;
1466                                 }
1467
1468                                 if (put_user(connector->property_values[i],
1469                                              prop_values + copied)) {
1470                                         ret = -EFAULT;
1471                                         goto out;
1472                                 }
1473                                 copied++;
1474                         }
1475                 }
1476         }
1477         out_resp->count_props = props_count;
1478
1479         if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1480                 copied = 0;
1481                 encoder_ptr = (uint32_t *)(unsigned long)(out_resp->encoders_ptr);
1482                 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1483                         if (connector->encoder_ids[i] != 0) {
1484                                 if (put_user(connector->encoder_ids[i],
1485                                              encoder_ptr + copied)) {
1486                                         ret = -EFAULT;
1487                                         goto out;
1488                                 }
1489                                 copied++;
1490                         }
1491                 }
1492         }
1493         out_resp->count_encoders = encoders_count;
1494
1495 out:
1496         mutex_unlock(&dev->mode_config.mutex);
1497         return ret;
1498 }
1499
1500 int drm_mode_getencoder(struct drm_device *dev, void *data,
1501                         struct drm_file *file_priv)
1502 {
1503         struct drm_mode_get_encoder *enc_resp = data;
1504         struct drm_mode_object *obj;
1505         struct drm_encoder *encoder;
1506         int ret = 0;
1507
1508         if (!drm_core_check_feature(dev, DRIVER_MODESET))
1509                 return -EINVAL;
1510
1511         mutex_lock(&dev->mode_config.mutex);
1512         obj = drm_mode_object_find(dev, enc_resp->encoder_id,
1513                                    DRM_MODE_OBJECT_ENCODER);
1514         if (!obj) {
1515                 ret = -EINVAL;
1516                 goto out;
1517         }
1518         encoder = obj_to_encoder(obj);
1519
1520         if (encoder->crtc)
1521                 enc_resp->crtc_id = encoder->crtc->base.id;
1522         else
1523                 enc_resp->crtc_id = 0;
1524         enc_resp->encoder_type = encoder->encoder_type;
1525         enc_resp->encoder_id = encoder->base.id;
1526         enc_resp->possible_crtcs = encoder->possible_crtcs;
1527         enc_resp->possible_clones = encoder->possible_clones;
1528
1529 out:
1530         mutex_unlock(&dev->mode_config.mutex);
1531         return ret;
1532 }
1533
1534 /**
1535  * drm_mode_getplane_res - get plane info
1536  * @dev: DRM device
1537  * @data: ioctl data
1538  * @file_priv: DRM file info
1539  *
1540  * Return an plane count and set of IDs.
1541  */
1542 int drm_mode_getplane_res(struct drm_device *dev, void *data,
1543                             struct drm_file *file_priv)
1544 {
1545         struct drm_mode_get_plane_res *plane_resp = data;
1546         struct drm_mode_config *config;
1547         struct drm_plane *plane;
1548         uint32_t __user *plane_ptr;
1549         int copied = 0, ret = 0;
1550
1551         if (!drm_core_check_feature(dev, DRIVER_MODESET))
1552                 return -EINVAL;
1553
1554         mutex_lock(&dev->mode_config.mutex);
1555         config = &dev->mode_config;
1556
1557         /*
1558          * This ioctl is called twice, once to determine how much space is
1559          * needed, and the 2nd time to fill it.
1560          */
1561         if (config->num_plane &&
1562             (plane_resp->count_planes >= config->num_plane)) {
1563                 plane_ptr = (uint32_t *)(unsigned long)plane_resp->plane_id_ptr;
1564
1565                 list_for_each_entry(plane, &config->plane_list, head) {
1566                         if (put_user(plane->base.id, plane_ptr + copied)) {
1567                                 ret = -EFAULT;
1568                                 goto out;
1569                         }
1570                         copied++;
1571                 }
1572         }
1573         plane_resp->count_planes = config->num_plane;
1574
1575 out:
1576         mutex_unlock(&dev->mode_config.mutex);
1577         return ret;
1578 }
1579
1580 /**
1581  * drm_mode_getplane - get plane info
1582  * @dev: DRM device
1583  * @data: ioctl data
1584  * @file_priv: DRM file info
1585  *
1586  * Return plane info, including formats supported, gamma size, any
1587  * current fb, etc.
1588  */
1589 int drm_mode_getplane(struct drm_device *dev, void *data,
1590                         struct drm_file *file_priv)
1591 {
1592         struct drm_mode_get_plane *plane_resp = data;
1593         struct drm_mode_object *obj;
1594         struct drm_plane *plane;
1595         uint32_t __user *format_ptr;
1596         int ret = 0;
1597
1598         if (!drm_core_check_feature(dev, DRIVER_MODESET))
1599                 return -EINVAL;
1600
1601         mutex_lock(&dev->mode_config.mutex);
1602         obj = drm_mode_object_find(dev, plane_resp->plane_id,
1603                                    DRM_MODE_OBJECT_PLANE);
1604         if (!obj) {
1605                 ret = -ENOENT;
1606                 goto out;
1607         }
1608         plane = obj_to_plane(obj);
1609
1610         if (plane->crtc)
1611                 plane_resp->crtc_id = plane->crtc->base.id;
1612         else
1613                 plane_resp->crtc_id = 0;
1614
1615         if (plane->fb)
1616                 plane_resp->fb_id = plane->fb->base.id;
1617         else
1618                 plane_resp->fb_id = 0;
1619
1620         plane_resp->plane_id = plane->base.id;
1621         plane_resp->possible_crtcs = plane->possible_crtcs;
1622         plane_resp->gamma_size = plane->gamma_size;
1623
1624         /*
1625          * This ioctl is called twice, once to determine how much space is
1626          * needed, and the 2nd time to fill it.
1627          */
1628         if (plane->format_count &&
1629             (plane_resp->count_format_types >= plane->format_count)) {
1630                 format_ptr = (uint32_t *)(unsigned long)plane_resp->format_type_ptr;
1631                 if (copy_to_user(format_ptr,
1632                                  plane->format_types,
1633                                  sizeof(uint32_t) * plane->format_count)) {
1634                         ret = -EFAULT;
1635                         goto out;
1636                 }
1637         }
1638         plane_resp->count_format_types = plane->format_count;
1639
1640 out:
1641         mutex_unlock(&dev->mode_config.mutex);
1642         return ret;
1643 }
1644
1645 /**
1646  * drm_mode_setplane - set up or tear down an plane
1647  * @dev: DRM device
1648  * @data: ioctl data*
1649  * @file_prive: DRM file info
1650  *
1651  * Set plane info, including placement, fb, scaling, and other factors.
1652  * Or pass a NULL fb to disable.
1653  */
1654 int drm_mode_setplane(struct drm_device *dev, void *data,
1655                         struct drm_file *file_priv)
1656 {
1657         struct drm_mode_set_plane *plane_req = data;
1658         struct drm_mode_object *obj;
1659         struct drm_plane *plane;
1660         struct drm_crtc *crtc;
1661         struct drm_framebuffer *fb;
1662         int ret = 0;
1663
1664         if (!drm_core_check_feature(dev, DRIVER_MODESET))
1665                 return -EINVAL;
1666
1667         mutex_lock(&dev->mode_config.mutex);
1668
1669         /*
1670          * First, find the plane, crtc, and fb objects.  If not available,
1671          * we don't bother to call the driver.
1672          */
1673         obj = drm_mode_object_find(dev, plane_req->plane_id,
1674                                    DRM_MODE_OBJECT_PLANE);
1675         if (!obj) {
1676                 DRM_DEBUG_KMS("Unknown plane ID %d\n",
1677                               plane_req->plane_id);
1678                 ret = -ENOENT;
1679                 goto out;
1680         }
1681         plane = obj_to_plane(obj);
1682
1683         /* No fb means shut it down */
1684         if (!plane_req->fb_id) {
1685                 plane->funcs->disable_plane(plane);
1686                 goto out;
1687         }
1688
1689         obj = drm_mode_object_find(dev, plane_req->crtc_id,
1690                                    DRM_MODE_OBJECT_CRTC);
1691         if (!obj) {
1692                 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
1693                               plane_req->crtc_id);
1694                 ret = -ENOENT;
1695                 goto out;
1696         }
1697         crtc = obj_to_crtc(obj);
1698
1699         obj = drm_mode_object_find(dev, plane_req->fb_id,
1700                                    DRM_MODE_OBJECT_FB);
1701         if (!obj) {
1702                 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
1703                               plane_req->fb_id);
1704                 ret = -ENOENT;
1705                 goto out;
1706         }
1707         fb = obj_to_fb(obj);
1708
1709         ret = plane->funcs->update_plane(plane, crtc, fb,
1710                                          plane_req->crtc_x, plane_req->crtc_y,
1711                                          plane_req->crtc_w, plane_req->crtc_h,
1712                                          plane_req->src_x, plane_req->src_y,
1713                                          plane_req->src_w, plane_req->src_h);
1714         if (!ret) {
1715                 plane->crtc = crtc;
1716                 plane->fb = fb;
1717         }
1718
1719 out:
1720         mutex_unlock(&dev->mode_config.mutex);
1721
1722         return ret;
1723 }
1724
1725 /**
1726  * drm_mode_setcrtc - set CRTC configuration
1727  * @inode: inode from the ioctl
1728  * @filp: file * from the ioctl
1729  * @cmd: cmd from ioctl
1730  * @arg: arg from ioctl
1731  *
1732  * LOCKING:
1733  * Caller? (FIXME)
1734  *
1735  * Build a new CRTC configuration based on user request.
1736  *
1737  * Called by the user via ioctl.
1738  *
1739  * RETURNS:
1740  * Zero on success, errno on failure.
1741  */
1742 int drm_mode_setcrtc(struct drm_device *dev, void *data,
1743                      struct drm_file *file_priv)
1744 {
1745         struct drm_mode_config *config = &dev->mode_config;
1746         struct drm_mode_crtc *crtc_req = data;
1747         struct drm_mode_object *obj;
1748         struct drm_crtc *crtc, *crtcfb;
1749         struct drm_connector **connector_set = NULL, *connector;
1750         struct drm_framebuffer *fb = NULL;
1751         struct drm_display_mode *mode = NULL;
1752         struct drm_mode_set set;
1753         uint32_t __user *set_connectors_ptr;
1754         int ret = 0;
1755         int i;
1756
1757         if (!drm_core_check_feature(dev, DRIVER_MODESET))
1758                 return -EINVAL;
1759
1760         mutex_lock(&dev->mode_config.mutex);
1761         obj = drm_mode_object_find(dev, crtc_req->crtc_id,
1762                                    DRM_MODE_OBJECT_CRTC);
1763         if (!obj) {
1764                 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
1765                 ret = -EINVAL;
1766                 goto out;
1767         }
1768         crtc = obj_to_crtc(obj);
1769         DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
1770
1771         if (crtc_req->mode_valid) {
1772                 /* If we have a mode we need a framebuffer. */
1773                 /* If we pass -1, set the mode with the currently bound fb */
1774                 if (crtc_req->fb_id == -1) {
1775                         list_for_each_entry(crtcfb,
1776                                             &dev->mode_config.crtc_list, head) {
1777                                 if (crtcfb == crtc) {
1778                                         DRM_DEBUG_KMS("Using current fb for "
1779                                                         "setmode\n");
1780                                         fb = crtc->fb;
1781                                 }
1782                         }
1783                 } else {
1784                         obj = drm_mode_object_find(dev, crtc_req->fb_id,
1785                                                    DRM_MODE_OBJECT_FB);
1786                         if (!obj) {
1787                                 DRM_DEBUG_KMS("Unknown FB ID%d\n",
1788                                                 crtc_req->fb_id);
1789                                 ret = -EINVAL;
1790                                 goto out;
1791                         }
1792                         fb = obj_to_fb(obj);
1793                 }
1794
1795                 mode = drm_mode_create(dev);
1796                 drm_crtc_convert_umode(mode, &crtc_req->mode);
1797                 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
1798         }
1799
1800         if (crtc_req->count_connectors == 0 && mode) {
1801                 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
1802                 ret = -EINVAL;
1803                 goto out;
1804         }
1805
1806         if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
1807                 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
1808                           crtc_req->count_connectors);
1809                 ret = -EINVAL;
1810                 goto out;
1811         }
1812
1813         if (crtc_req->count_connectors > 0) {
1814                 u32 out_id;
1815
1816                 /* Avoid unbounded kernel memory allocation */
1817                 if (crtc_req->count_connectors > config->num_connector) {
1818                         ret = -EINVAL;
1819                         goto out;
1820                 }
1821
1822                 connector_set = kmalloc(crtc_req->count_connectors *
1823                                         sizeof(struct drm_connector *),
1824                                         GFP_KERNEL);
1825                 if (!connector_set) {
1826                         ret = -ENOMEM;
1827                         goto out;
1828                 }
1829
1830                 for (i = 0; i < crtc_req->count_connectors; i++) {
1831                         set_connectors_ptr = (uint32_t *)(unsigned long)crtc_req->set_connectors_ptr;
1832                         if (get_user(out_id, &set_connectors_ptr[i])) {
1833                                 ret = -EFAULT;
1834                                 goto out;
1835                         }
1836
1837                         obj = drm_mode_object_find(dev, out_id,
1838                                                    DRM_MODE_OBJECT_CONNECTOR);
1839                         if (!obj) {
1840                                 DRM_DEBUG_KMS("Connector id %d unknown\n",
1841                                                 out_id);
1842                                 ret = -EINVAL;
1843                                 goto out;
1844                         }
1845                         connector = obj_to_connector(obj);
1846                         DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1847                                         connector->base.id,
1848                                         drm_get_connector_name(connector));
1849
1850                         connector_set[i] = connector;
1851                 }
1852         }
1853
1854         set.crtc = crtc;
1855         set.x = crtc_req->x;
1856         set.y = crtc_req->y;
1857         set.mode = mode;
1858         set.connectors = connector_set;
1859         set.num_connectors = crtc_req->count_connectors;
1860         set.fb = fb;
1861         ret = crtc->funcs->set_config(&set);
1862
1863 out:
1864         kfree(connector_set);
1865         mutex_unlock(&dev->mode_config.mutex);
1866         return ret;
1867 }
1868
1869 int drm_mode_cursor_ioctl(struct drm_device *dev,
1870                         void *data, struct drm_file *file_priv)
1871 {
1872         struct drm_mode_cursor *req = data;
1873         struct drm_mode_object *obj;
1874         struct drm_crtc *crtc;
1875         int ret = 0;
1876
1877         if (!drm_core_check_feature(dev, DRIVER_MODESET))
1878                 return -EINVAL;
1879
1880         if (!req->flags) {
1881                 DRM_ERROR("no operation set\n");
1882                 return -EINVAL;
1883         }
1884
1885         mutex_lock(&dev->mode_config.mutex);
1886         obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
1887         if (!obj) {
1888                 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
1889                 ret = -EINVAL;
1890                 goto out;
1891         }
1892         crtc = obj_to_crtc(obj);
1893
1894         if (req->flags & DRM_MODE_CURSOR_BO) {
1895                 if (!crtc->funcs->cursor_set) {
1896                         DRM_ERROR("crtc does not support cursor\n");
1897                         ret = -ENXIO;
1898                         goto out;
1899                 }
1900                 /* Turns off the cursor if handle is 0 */
1901                 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
1902                                               req->width, req->height);
1903         }
1904
1905         if (req->flags & DRM_MODE_CURSOR_MOVE) {
1906                 if (crtc->funcs->cursor_move) {
1907                         ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
1908                 } else {
1909                         DRM_ERROR("crtc does not support cursor\n");
1910                         ret = -EFAULT;
1911                         goto out;
1912                 }
1913         }
1914 out:
1915         mutex_unlock(&dev->mode_config.mutex);
1916         return ret;
1917 }
1918
1919 /* Original addfb only supported RGB formats, so figure out which one */
1920 uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
1921 {
1922         uint32_t fmt;
1923
1924         switch (bpp) {
1925         case 8:
1926                 fmt = DRM_FOURCC_RGB332;
1927                 break;
1928         case 16:
1929                 if (depth == 15)
1930                         fmt = DRM_FOURCC_RGB555;
1931                 else
1932                         fmt = DRM_FOURCC_RGB565;
1933                 break;
1934         case 24:
1935                 fmt = DRM_FOURCC_RGB24;
1936                 break;
1937         case 32:
1938                 if (depth == 24)
1939                         fmt = DRM_FOURCC_RGB24;
1940                 else if (depth == 30)
1941                         fmt = DRM_INTEL_RGB30;
1942                 else
1943                         fmt = DRM_FOURCC_RGB32;
1944                 break;
1945         default:
1946                 DRM_ERROR("bad bpp, assuming RGB24 pixel format\n");
1947                 fmt = DRM_FOURCC_RGB24;
1948                 break;
1949         }
1950
1951         return fmt;
1952 }
1953 EXPORT_SYMBOL(drm_mode_legacy_fb_format);
1954
1955 /**
1956  * drm_mode_addfb - add an FB to the graphics configuration
1957  * @inode: inode from the ioctl
1958  * @filp: file * from the ioctl
1959  * @cmd: cmd from ioctl
1960  * @arg: arg from ioctl
1961  *
1962  * LOCKING:
1963  * Takes mode config lock.
1964  *
1965  * Add a new FB to the specified CRTC, given a user request.
1966  *
1967  * Called by the user via ioctl.
1968  *
1969  * RETURNS:
1970  * Zero on success, errno on failure.
1971  */
1972 int drm_mode_addfb(struct drm_device *dev,
1973                    void *data, struct drm_file *file_priv)
1974 {
1975         struct drm_mode_fb_cmd *or = data;
1976         struct drm_mode_fb_cmd2 r = {};
1977         struct drm_mode_config *config = &dev->mode_config;
1978         struct drm_framebuffer *fb;
1979         int ret = 0;
1980
1981         /* Use new struct with format internally */
1982         r.fb_id = or->fb_id;
1983         r.width = or->width;
1984         r.height = or->height;
1985         r.pitches[0] = or->pitch;
1986         r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
1987         r.handles[0] = or->handle;
1988
1989         if (!drm_core_check_feature(dev, DRIVER_MODESET))
1990                 return -EINVAL;
1991
1992         if ((config->min_width > r.width) || (r.width > config->max_width)) {
1993                 DRM_ERROR("mode new framebuffer width not within limits\n");
1994                 return -EINVAL;
1995         }
1996         if ((config->min_height > r.height) || (r.height > config->max_height)) {
1997                 DRM_ERROR("mode new framebuffer height not within limits\n");
1998                 return -EINVAL;
1999         }
2000
2001         mutex_lock(&dev->mode_config.mutex);
2002
2003         /* TODO check buffer is sufficiently large */
2004         /* TODO setup destructor callback */
2005
2006         fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2007         if (IS_ERR(fb)) {
2008                 DRM_ERROR("could not create framebuffer\n");
2009                 ret = PTR_ERR(fb);
2010                 goto out;
2011         }
2012
2013         or->fb_id = fb->base.id;
2014         list_add(&fb->filp_head, &file_priv->fbs);
2015         DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
2016
2017 out:
2018         mutex_unlock(&dev->mode_config.mutex);
2019         return ret;
2020 }
2021
2022 /**
2023  * drm_mode_addfb2 - add an FB to the graphics configuration
2024  * @inode: inode from the ioctl
2025  * @filp: file * from the ioctl
2026  * @cmd: cmd from ioctl
2027  * @arg: arg from ioctl
2028  *
2029  * LOCKING:
2030  * Takes mode config lock.
2031  *
2032  * Add a new FB to the specified CRTC, given a user request with format.
2033  *
2034  * Called by the user via ioctl.
2035  *
2036  * RETURNS:
2037  * Zero on success, errno on failure.
2038  */
2039 int drm_mode_addfb2(struct drm_device *dev,
2040                     void *data, struct drm_file *file_priv)
2041 {
2042         struct drm_mode_fb_cmd2 *r = data;
2043         struct drm_mode_config *config = &dev->mode_config;
2044         struct drm_framebuffer *fb;
2045         int ret = 0;
2046
2047         if (!drm_core_check_feature(dev, DRIVER_MODESET))
2048                 return -EINVAL;
2049
2050         if ((config->min_width > r->width) || (r->width > config->max_width)) {
2051                 DRM_ERROR("bad framebuffer width %d, should be >= %d && <= %d\n",
2052                           r->width, config->min_width, config->max_width);
2053                 return -EINVAL;
2054         }
2055         if ((config->min_height > r->height) || (r->height > config->max_height)) {
2056                 DRM_ERROR("bad framebuffer height %d, should be >= %d && <= %d\n",
2057                           r->height, config->min_height, config->max_height);
2058                 return -EINVAL;
2059         }
2060
2061         mutex_lock(&dev->mode_config.mutex);
2062
2063         fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
2064         if (IS_ERR(fb)) {
2065                 DRM_ERROR("could not create framebuffer\n");
2066                 ret = PTR_ERR(fb);
2067                 goto out;
2068         }
2069
2070         r->fb_id = fb->base.id;
2071         list_add(&fb->filp_head, &file_priv->fbs);
2072         DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
2073
2074 out:
2075         mutex_unlock(&dev->mode_config.mutex);
2076         return ret;
2077 }
2078
2079 /**
2080  * drm_mode_rmfb - remove an FB from the configuration
2081  * @inode: inode from the ioctl
2082  * @filp: file * from the ioctl
2083  * @cmd: cmd from ioctl
2084  * @arg: arg from ioctl
2085  *
2086  * LOCKING:
2087  * Takes mode config lock.
2088  *
2089  * Remove the FB specified by the user.
2090  *
2091  * Called by the user via ioctl.
2092  *
2093  * RETURNS:
2094  * Zero on success, errno on failure.
2095  */
2096 int drm_mode_rmfb(struct drm_device *dev,
2097                    void *data, struct drm_file *file_priv)
2098 {
2099         struct drm_mode_object *obj;
2100         struct drm_framebuffer *fb = NULL;
2101         struct drm_framebuffer *fbl = NULL;
2102         uint32_t *id = data;
2103         int ret = 0;
2104         int found = 0;
2105
2106         if (!drm_core_check_feature(dev, DRIVER_MODESET))
2107                 return -EINVAL;
2108
2109         mutex_lock(&dev->mode_config.mutex);
2110         obj = drm_mode_object_find(dev, *id, DRM_MODE_OBJECT_FB);
2111         /* TODO check that we really get a framebuffer back. */
2112         if (!obj) {
2113                 DRM_ERROR("mode invalid framebuffer id\n");
2114                 ret = -EINVAL;
2115                 goto out;
2116         }
2117         fb = obj_to_fb(obj);
2118
2119         list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2120                 if (fb == fbl)
2121                         found = 1;
2122
2123         if (!found) {
2124                 DRM_ERROR("tried to remove a fb that we didn't own\n");
2125                 ret = -EINVAL;
2126                 goto out;
2127         }
2128
2129         /* TODO release all crtc connected to the framebuffer */
2130         /* TODO unhock the destructor from the buffer object */
2131
2132         list_del(&fb->filp_head);
2133         fb->funcs->destroy(fb);
2134
2135 out:
2136         mutex_unlock(&dev->mode_config.mutex);
2137         return ret;
2138 }
2139
2140 /**
2141  * drm_mode_getfb - get FB info
2142  * @inode: inode from the ioctl
2143  * @filp: file * from the ioctl
2144  * @cmd: cmd from ioctl
2145  * @arg: arg from ioctl
2146  *
2147  * LOCKING:
2148  * Caller? (FIXME)
2149  *
2150  * Lookup the FB given its ID and return info about it.
2151  *
2152  * Called by the user via ioctl.
2153  *
2154  * RETURNS:
2155  * Zero on success, errno on failure.
2156  */
2157 int drm_mode_getfb(struct drm_device *dev,
2158                    void *data, struct drm_file *file_priv)
2159 {
2160         struct drm_mode_fb_cmd *r = data;
2161         struct drm_mode_object *obj;
2162         struct drm_framebuffer *fb;
2163         int ret = 0;
2164
2165         if (!drm_core_check_feature(dev, DRIVER_MODESET))
2166                 return -EINVAL;
2167
2168         mutex_lock(&dev->mode_config.mutex);
2169         obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
2170         if (!obj) {
2171                 DRM_ERROR("invalid framebuffer id\n");
2172                 ret = -EINVAL;
2173                 goto out;
2174         }
2175         fb = obj_to_fb(obj);
2176
2177         r->height = fb->height;
2178         r->width = fb->width;
2179         r->depth = fb->depth;
2180         r->bpp = fb->bits_per_pixel;
2181         r->pitch = fb->pitch;
2182         fb->funcs->create_handle(fb, file_priv, &r->handle);
2183
2184 out:
2185         mutex_unlock(&dev->mode_config.mutex);
2186         return ret;
2187 }
2188
2189 int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
2190                            void *data, struct drm_file *file_priv)
2191 {
2192         struct drm_clip_rect __user *clips_ptr;
2193         struct drm_clip_rect *clips = NULL;
2194         struct drm_mode_fb_dirty_cmd *r = data;
2195         struct drm_mode_object *obj;
2196         struct drm_framebuffer *fb;
2197         unsigned flags;
2198         int num_clips;
2199         int ret = 0;
2200
2201         if (!drm_core_check_feature(dev, DRIVER_MODESET))
2202                 return -EINVAL;
2203
2204         mutex_lock(&dev->mode_config.mutex);
2205         obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
2206         if (!obj) {
2207                 DRM_ERROR("invalid framebuffer id\n");
2208                 ret = -EINVAL;
2209                 goto out_err1;
2210         }
2211         fb = obj_to_fb(obj);
2212
2213         num_clips = r->num_clips;
2214         clips_ptr = (struct drm_clip_rect *)(unsigned long)r->clips_ptr;
2215
2216         if (!num_clips != !clips_ptr) {
2217                 ret = -EINVAL;
2218                 goto out_err1;
2219         }
2220
2221         flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
2222
2223         /* If userspace annotates copy, clips must come in pairs */
2224         if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
2225                 ret = -EINVAL;
2226                 goto out_err1;
2227         }
2228
2229         if (num_clips && clips_ptr) {
2230                 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
2231                 if (!clips) {
2232                         ret = -ENOMEM;
2233                         goto out_err1;
2234                 }
2235
2236                 ret = copy_from_user(clips, clips_ptr,
2237                                      num_clips * sizeof(*clips));
2238                 if (ret) {
2239                         ret = -EFAULT;
2240                         goto out_err2;
2241                 }
2242         }
2243
2244         if (fb->funcs->dirty) {
2245                 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
2246                                        clips, num_clips);
2247         } else {
2248                 ret = -ENOSYS;
2249                 goto out_err2;
2250         }
2251
2252 out_err2:
2253         kfree(clips);
2254 out_err1:
2255         mutex_unlock(&dev->mode_config.mutex);
2256         return ret;
2257 }
2258
2259
2260 /**
2261  * drm_fb_release - remove and free the FBs on this file
2262  * @filp: file * from the ioctl
2263  *
2264  * LOCKING:
2265  * Takes mode config lock.
2266  *
2267  * Destroy all the FBs associated with @filp.
2268  *
2269  * Called by the user via ioctl.
2270  *
2271  * RETURNS:
2272  * Zero on success, errno on failure.
2273  */
2274 void drm_fb_release(struct drm_file *priv)
2275 {
2276         struct drm_device *dev = priv->minor->dev;
2277         struct drm_framebuffer *fb, *tfb;
2278
2279         mutex_lock(&dev->mode_config.mutex);
2280         list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
2281                 list_del(&fb->filp_head);
2282                 fb->funcs->destroy(fb);
2283         }
2284         mutex_unlock(&dev->mode_config.mutex);
2285 }
2286
2287 /**
2288  * drm_mode_attachmode - add a mode to the user mode list
2289  * @dev: DRM device
2290  * @connector: connector to add the mode to
2291  * @mode: mode to add
2292  *
2293  * Add @mode to @connector's user mode list.
2294  */
2295 static int drm_mode_attachmode(struct drm_device *dev,
2296                                struct drm_connector *connector,
2297                                struct drm_display_mode *mode)
2298 {
2299         int ret = 0;
2300
2301         list_add_tail(&mode->head, &connector->user_modes);
2302         return ret;
2303 }
2304
2305 int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
2306                              struct drm_display_mode *mode)
2307 {
2308         struct drm_connector *connector;
2309         int ret = 0;
2310         struct drm_display_mode *dup_mode;
2311         int need_dup = 0;
2312         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2313                 if (!connector->encoder)
2314                         break;
2315                 if (connector->encoder->crtc == crtc) {
2316                         if (need_dup)
2317                                 dup_mode = drm_mode_duplicate(dev, mode);
2318                         else
2319                                 dup_mode = mode;
2320                         ret = drm_mode_attachmode(dev, connector, dup_mode);
2321                         if (ret)
2322                                 return ret;
2323                         need_dup = 1;
2324                 }
2325         }
2326         return 0;
2327 }
2328 EXPORT_SYMBOL(drm_mode_attachmode_crtc);
2329
2330 static int drm_mode_detachmode(struct drm_device *dev,
2331                                struct drm_connector *connector,
2332                                struct drm_display_mode *mode)
2333 {
2334         int found = 0;
2335         int ret = 0;
2336         struct drm_display_mode *match_mode, *t;
2337
2338         list_for_each_entry_safe(match_mode, t, &connector->user_modes, head) {
2339                 if (drm_mode_equal(match_mode, mode)) {
2340                         list_del(&match_mode->head);
2341                         drm_mode_destroy(dev, match_mode);
2342                         found = 1;
2343                         break;
2344                 }
2345         }
2346
2347         if (!found)
2348                 ret = -EINVAL;
2349
2350         return ret;
2351 }
2352
2353 int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
2354 {
2355         struct drm_connector *connector;
2356
2357         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2358                 drm_mode_detachmode(dev, connector, mode);
2359         }
2360         return 0;
2361 }
2362 EXPORT_SYMBOL(drm_mode_detachmode_crtc);
2363
2364 /**
2365  * drm_fb_attachmode - Attach a user mode to an connector
2366  * @inode: inode from the ioctl
2367  * @filp: file * from the ioctl
2368  * @cmd: cmd from ioctl
2369  * @arg: arg from ioctl
2370  *
2371  * This attaches a user specified mode to an connector.
2372  * Called by the user via ioctl.
2373  *
2374  * RETURNS:
2375  * Zero on success, errno on failure.
2376  */
2377 int drm_mode_attachmode_ioctl(struct drm_device *dev,
2378                               void *data, struct drm_file *file_priv)
2379 {
2380         struct drm_mode_mode_cmd *mode_cmd = data;
2381         struct drm_connector *connector;
2382         struct drm_display_mode *mode;
2383         struct drm_mode_object *obj;
2384         struct drm_mode_modeinfo *umode = &mode_cmd->mode;
2385         int ret = 0;
2386
2387         if (!drm_core_check_feature(dev, DRIVER_MODESET))
2388                 return -EINVAL;
2389
2390         mutex_lock(&dev->mode_config.mutex);
2391
2392         obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2393         if (!obj) {
2394                 ret = -EINVAL;
2395                 goto out;
2396         }
2397         connector = obj_to_connector(obj);
2398
2399         mode = drm_mode_create(dev);
2400         if (!mode) {
2401                 ret = -ENOMEM;
2402                 goto out;
2403         }
2404
2405         drm_crtc_convert_umode(mode, umode);
2406
2407         ret = drm_mode_attachmode(dev, connector, mode);
2408 out:
2409         mutex_unlock(&dev->mode_config.mutex);
2410         return ret;
2411 }
2412
2413
2414 /**
2415  * drm_fb_detachmode - Detach a user specified mode from an connector
2416  * @inode: inode from the ioctl
2417  * @filp: file * from the ioctl
2418  * @cmd: cmd from ioctl
2419  * @arg: arg from ioctl
2420  *
2421  * Called by the user via ioctl.
2422  *
2423  * RETURNS:
2424  * Zero on success, errno on failure.
2425  */
2426 int drm_mode_detachmode_ioctl(struct drm_device *dev,
2427                               void *data, struct drm_file *file_priv)
2428 {
2429         struct drm_mode_object *obj;
2430         struct drm_mode_mode_cmd *mode_cmd = data;
2431         struct drm_connector *connector;
2432         struct drm_display_mode mode;
2433         struct drm_mode_modeinfo *umode = &mode_cmd->mode;
2434         int ret = 0;
2435
2436         if (!drm_core_check_feature(dev, DRIVER_MODESET))
2437                 return -EINVAL;
2438
2439         mutex_lock(&dev->mode_config.mutex);
2440
2441         obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2442         if (!obj) {
2443                 ret = -EINVAL;
2444                 goto out;
2445         }
2446         connector = obj_to_connector(obj);
2447
2448         drm_crtc_convert_umode(&mode, umode);
2449         ret = drm_mode_detachmode(dev, connector, &mode);
2450 out:
2451         mutex_unlock(&dev->mode_config.mutex);
2452         return ret;
2453 }
2454
2455 struct drm_property *drm_property_create(struct drm_device *dev, int flags,
2456                                          const char *name, int num_values)
2457 {
2458         struct drm_property *property = NULL;
2459
2460         property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
2461         if (!property)
2462                 return NULL;
2463
2464         if (num_values) {
2465                 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
2466                 if (!property->values)
2467                         goto fail;
2468         }
2469
2470         drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
2471         property->flags = flags;
2472         property->num_values = num_values;
2473         INIT_LIST_HEAD(&property->enum_blob_list);
2474
2475         if (name)
2476                 strncpy(property->name, name, DRM_PROP_NAME_LEN);
2477
2478         list_add_tail(&property->head, &dev->mode_config.property_list);
2479         return property;
2480 fail:
2481         kfree(property);
2482         return NULL;
2483 }
2484 EXPORT_SYMBOL(drm_property_create);
2485
2486 int drm_property_add_enum(struct drm_property *property, int index,
2487                           uint64_t value, const char *name)
2488 {
2489         struct drm_property_enum *prop_enum;
2490
2491         if (!(property->flags & DRM_MODE_PROP_ENUM))
2492                 return -EINVAL;
2493
2494         if (!list_empty(&property->enum_blob_list)) {
2495                 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2496                         if (prop_enum->value == value) {
2497                                 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2498                                 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2499                                 return 0;
2500                         }
2501                 }
2502         }
2503
2504         prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
2505         if (!prop_enum)
2506                 return -ENOMEM;
2507
2508         strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2509         prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2510         prop_enum->value = value;
2511
2512         property->values[index] = value;
2513         list_add_tail(&prop_enum->head, &property->enum_blob_list);
2514         return 0;
2515 }
2516 EXPORT_SYMBOL(drm_property_add_enum);
2517
2518 void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
2519 {
2520         struct drm_property_enum *prop_enum, *pt;
2521
2522         list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
2523                 list_del(&prop_enum->head);
2524                 kfree(prop_enum);
2525         }
2526
2527         if (property->num_values)
2528                 kfree(property->values);
2529         drm_mode_object_put(dev, &property->base);
2530         list_del(&property->head);
2531         kfree(property);
2532 }
2533 EXPORT_SYMBOL(drm_property_destroy);
2534
2535 int drm_connector_attach_property(struct drm_connector *connector,
2536                                struct drm_property *property, uint64_t init_val)
2537 {
2538         int i;
2539
2540         for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2541                 if (connector->property_ids[i] == 0) {
2542                         connector->property_ids[i] = property->base.id;
2543                         connector->property_values[i] = init_val;
2544                         break;
2545                 }
2546         }
2547
2548         if (i == DRM_CONNECTOR_MAX_PROPERTY)
2549                 return -EINVAL;
2550         return 0;
2551 }
2552 EXPORT_SYMBOL(drm_connector_attach_property);
2553
2554 int drm_connector_property_set_value(struct drm_connector *connector,
2555                                   struct drm_property *property, uint64_t value)
2556 {
2557         int i;
2558
2559         for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2560                 if (connector->property_ids[i] == property->base.id) {
2561                         connector->property_values[i] = value;
2562                         break;
2563                 }
2564         }
2565
2566         if (i == DRM_CONNECTOR_MAX_PROPERTY)
2567                 return -EINVAL;
2568         return 0;
2569 }
2570 EXPORT_SYMBOL(drm_connector_property_set_value);
2571
2572 int drm_connector_property_get_value(struct drm_connector *connector,
2573                                   struct drm_property *property, uint64_t *val)
2574 {
2575         int i;
2576
2577         for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2578                 if (connector->property_ids[i] == property->base.id) {
2579                         *val = connector->property_values[i];
2580                         break;
2581                 }
2582         }
2583
2584         if (i == DRM_CONNECTOR_MAX_PROPERTY)
2585                 return -EINVAL;
2586         return 0;
2587 }
2588 EXPORT_SYMBOL(drm_connector_property_get_value);
2589
2590 int drm_mode_getproperty_ioctl(struct drm_device *dev,
2591                                void *data, struct drm_file *file_priv)
2592 {
2593         struct drm_mode_object *obj;
2594         struct drm_mode_get_property *out_resp = data;
2595         struct drm_property *property;
2596         int enum_count = 0;
2597         int blob_count = 0;
2598         int value_count = 0;
2599         int ret = 0, i;
2600         int copied;
2601         struct drm_property_enum *prop_enum;
2602         struct drm_mode_property_enum __user *enum_ptr;
2603         struct drm_property_blob *prop_blob;
2604         uint32_t *blob_id_ptr;
2605         uint64_t __user *values_ptr;
2606         uint32_t __user *blob_length_ptr;
2607
2608         if (!drm_core_check_feature(dev, DRIVER_MODESET))
2609                 return -EINVAL;
2610
2611         mutex_lock(&dev->mode_config.mutex);
2612         obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
2613         if (!obj) {
2614                 ret = -EINVAL;
2615                 goto done;
2616         }
2617         property = obj_to_property(obj);
2618
2619         if (property->flags & DRM_MODE_PROP_ENUM) {
2620                 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
2621                         enum_count++;
2622         } else if (property->flags & DRM_MODE_PROP_BLOB) {
2623                 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
2624                         blob_count++;
2625         }
2626
2627         value_count = property->num_values;
2628
2629         strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
2630         out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
2631         out_resp->flags = property->flags;
2632
2633         if ((out_resp->count_values >= value_count) && value_count) {
2634                 values_ptr = (uint64_t *)(unsigned long)out_resp->values_ptr;
2635                 for (i = 0; i < value_count; i++) {
2636                         if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
2637                                 ret = -EFAULT;
2638                                 goto done;
2639                         }
2640                 }
2641         }
2642         out_resp->count_values = value_count;
2643
2644         if (property->flags & DRM_MODE_PROP_ENUM) {
2645                 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
2646                         copied = 0;
2647                         enum_ptr = (struct drm_mode_property_enum *)(unsigned long)out_resp->enum_blob_ptr;
2648                         list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2649
2650                                 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
2651                                         ret = -EFAULT;
2652                                         goto done;
2653                                 }
2654
2655                                 if (copy_to_user(&enum_ptr[copied].name,
2656                                                  &prop_enum->name, DRM_PROP_NAME_LEN)) {
2657                                         ret = -EFAULT;
2658                                         goto done;
2659                                 }
2660                                 copied++;
2661                         }
2662                 }
2663                 out_resp->count_enum_blobs = enum_count;
2664         }
2665
2666         if (property->flags & DRM_MODE_PROP_BLOB) {
2667                 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
2668                         copied = 0;
2669                         blob_id_ptr = (uint32_t *)(unsigned long)out_resp->enum_blob_ptr;
2670                         blob_length_ptr = (uint32_t *)(unsigned long)out_resp->values_ptr;
2671
2672                         list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
2673                                 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
2674                                         ret = -EFAULT;
2675                                         goto done;
2676                                 }
2677
2678                                 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
2679                                         ret = -EFAULT;
2680                                         goto done;
2681                                 }
2682
2683                                 copied++;
2684                         }
2685                 }
2686                 out_resp->count_enum_blobs = blob_count;
2687         }
2688 done:
2689         mutex_unlock(&dev->mode_config.mutex);
2690         return ret;
2691 }
2692
2693 static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
2694                                                           void *data)
2695 {
2696         struct drm_property_blob *blob;
2697
2698         if (!length || !data)
2699                 return NULL;
2700
2701         blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
2702         if (!blob)
2703                 return NULL;
2704
2705         blob->data = (void *)((char *)blob + sizeof(struct drm_property_blob));
2706         blob->length = length;
2707
2708         memcpy(blob->data, data, length);
2709
2710         drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
2711
2712         list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
2713         return blob;
2714 }
2715
2716 static void drm_property_destroy_blob(struct drm_device *dev,
2717                                struct drm_property_blob *blob)
2718 {
2719         drm_mode_object_put(dev, &blob->base);
2720         list_del(&blob->head);
2721         kfree(blob);
2722 }
2723
2724 int drm_mode_getblob_ioctl(struct drm_device *dev,
2725                            void *data, struct drm_file *file_priv)
2726 {
2727         struct drm_mode_object *obj;
2728         struct drm_mode_get_blob *out_resp = data;
2729         struct drm_property_blob *blob;
2730         int ret = 0;
2731         void *blob_ptr;
2732
2733         if (!drm_core_check_feature(dev, DRIVER_MODESET))
2734                 return -EINVAL;
2735
2736         mutex_lock(&dev->mode_config.mutex);
2737         obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
2738         if (!obj) {
2739                 ret = -EINVAL;
2740                 goto done;
2741         }
2742         blob = obj_to_blob(obj);
2743
2744         if (out_resp->length == blob->length) {
2745                 blob_ptr = (void *)(unsigned long)out_resp->data;
2746                 if (copy_to_user(blob_ptr, blob->data, blob->length)){
2747                         ret = -EFAULT;
2748                         goto done;
2749                 }
2750         }
2751         out_resp->length = blob->length;
2752
2753 done:
2754         mutex_unlock(&dev->mode_config.mutex);
2755         return ret;
2756 }
2757
2758 int drm_mode_connector_update_edid_property(struct drm_connector *connector,
2759                                             struct edid *edid)
2760 {
2761         struct drm_device *dev = connector->dev;
2762         int ret = 0, size;
2763
2764         if (connector->edid_blob_ptr)
2765                 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
2766
2767         /* Delete edid, when there is none. */
2768         if (!edid) {
2769                 connector->edid_blob_ptr = NULL;
2770                 ret = drm_connector_property_set_value(connector, dev->mode_config.edid_property, 0);
2771                 return ret;
2772         }
2773
2774         size = EDID_LENGTH * (1 + edid->extensions);
2775         connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
2776                                                             size, edid);
2777
2778         ret = drm_connector_property_set_value(connector,
2779                                                dev->mode_config.edid_property,
2780                                                connector->edid_blob_ptr->base.id);
2781
2782         return ret;
2783 }
2784 EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
2785
2786 int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
2787                                        void *data, struct drm_file *file_priv)
2788 {
2789         struct drm_mode_connector_set_property *out_resp = data;
2790         struct drm_mode_object *obj;
2791         struct drm_property *property;
2792         struct drm_connector *connector;
2793         int ret = -EINVAL;
2794         int i;
2795
2796         if (!drm_core_check_feature(dev, DRIVER_MODESET))
2797                 return -EINVAL;
2798
2799         mutex_lock(&dev->mode_config.mutex);
2800
2801         obj = drm_mode_object_find(dev, out_resp->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2802         if (!obj) {
2803                 goto out;
2804         }
2805         connector = obj_to_connector(obj);
2806
2807         for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2808                 if (connector->property_ids[i] == out_resp->prop_id)
2809                         break;
2810         }
2811
2812         if (i == DRM_CONNECTOR_MAX_PROPERTY) {
2813                 goto out;
2814         }
2815
2816         obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
2817         if (!obj) {
2818                 goto out;
2819         }
2820         property = obj_to_property(obj);
2821
2822         if (property->flags & DRM_MODE_PROP_IMMUTABLE)
2823                 goto out;
2824
2825         if (property->flags & DRM_MODE_PROP_RANGE) {
2826                 if (out_resp->value < property->values[0])
2827                         goto out;
2828
2829                 if (out_resp->value > property->values[1])
2830                         goto out;
2831         } else {
2832                 int found = 0;
2833                 for (i = 0; i < property->num_values; i++) {
2834                         if (property->values[i] == out_resp->value) {
2835                                 found = 1;
2836                                 break;
2837                         }
2838                 }
2839                 if (!found) {
2840                         goto out;
2841                 }
2842         }
2843
2844         /* Do DPMS ourselves */
2845         if (property == connector->dev->mode_config.dpms_property) {
2846                 if (connector->funcs->dpms)
2847                         (*connector->funcs->dpms)(connector, (int) out_resp->value);
2848                 ret = 0;
2849         } else if (connector->funcs->set_property)
2850                 ret = connector->funcs->set_property(connector, property, out_resp->value);
2851
2852         /* store the property value if successful */
2853         if (!ret)
2854                 drm_connector_property_set_value(connector, property, out_resp->value);
2855 out:
2856         mutex_unlock(&dev->mode_config.mutex);
2857         return ret;
2858 }
2859
2860 int drm_mode_connector_attach_encoder(struct drm_connector *connector,
2861                                       struct drm_encoder *encoder)
2862 {
2863         int i;
2864
2865         for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2866                 if (connector->encoder_ids[i] == 0) {
2867                         connector->encoder_ids[i] = encoder->base.id;
2868                         return 0;
2869                 }
2870         }
2871         return -ENOMEM;
2872 }
2873 EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
2874
2875 void drm_mode_connector_detach_encoder(struct drm_connector *connector,
2876                                     struct drm_encoder *encoder)
2877 {
2878         int i;
2879         for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2880                 if (connector->encoder_ids[i] == encoder->base.id) {
2881                         connector->encoder_ids[i] = 0;
2882                         if (connector->encoder == encoder)
2883                                 connector->encoder = NULL;
2884                         break;
2885                 }
2886         }
2887 }
2888 EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
2889
2890 bool drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
2891                                   int gamma_size)
2892 {
2893         crtc->gamma_size = gamma_size;
2894
2895         crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
2896         if (!crtc->gamma_store) {
2897                 crtc->gamma_size = 0;
2898                 return false;
2899         }
2900
2901         return true;
2902 }
2903 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
2904
2905 int drm_mode_gamma_set_ioctl(struct drm_device *dev,
2906                              void *data, struct drm_file *file_priv)
2907 {
2908         struct drm_mode_crtc_lut *crtc_lut = data;
2909         struct drm_mode_object *obj;
2910         struct drm_crtc *crtc;
2911         void *r_base, *g_base, *b_base;
2912         int size;
2913         int ret = 0;
2914
2915         if (!drm_core_check_feature(dev, DRIVER_MODESET))
2916                 return -EINVAL;
2917
2918         mutex_lock(&dev->mode_config.mutex);
2919         obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
2920         if (!obj) {
2921                 ret = -EINVAL;
2922                 goto out;
2923         }
2924         crtc = obj_to_crtc(obj);
2925
2926         /* memcpy into gamma store */
2927         if (crtc_lut->gamma_size != crtc->gamma_size) {
2928                 ret = -EINVAL;
2929                 goto out;
2930         }
2931
2932         size = crtc_lut->gamma_size * (sizeof(uint16_t));
2933         r_base = crtc->gamma_store;
2934         if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
2935                 ret = -EFAULT;
2936                 goto out;
2937         }
2938
2939         g_base = r_base + size;
2940         if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
2941                 ret = -EFAULT;
2942                 goto out;
2943         }
2944
2945         b_base = g_base + size;
2946         if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
2947                 ret = -EFAULT;
2948                 goto out;
2949         }
2950
2951         crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
2952
2953 out:
2954         mutex_unlock(&dev->mode_config.mutex);
2955         return ret;
2956
2957 }
2958
2959 int drm_mode_gamma_get_ioctl(struct drm_device *dev,
2960                              void *data, struct drm_file *file_priv)
2961 {
2962         struct drm_mode_crtc_lut *crtc_lut = data;
2963         struct drm_mode_object *obj;
2964         struct drm_crtc *crtc;
2965         void *r_base, *g_base, *b_base;
2966         int size;
2967         int ret = 0;
2968
2969         if (!drm_core_check_feature(dev, DRIVER_MODESET))
2970                 return -EINVAL;
2971
2972         mutex_lock(&dev->mode_config.mutex);
2973         obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
2974         if (!obj) {
2975                 ret = -EINVAL;
2976                 goto out;
2977         }
2978         crtc = obj_to_crtc(obj);
2979
2980         /* memcpy into gamma store */
2981         if (crtc_lut->gamma_size != crtc->gamma_size) {
2982                 ret = -EINVAL;
2983                 goto out;
2984         }
2985
2986         size = crtc_lut->gamma_size * (sizeof(uint16_t));
2987         r_base = crtc->gamma_store;
2988         if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
2989                 ret = -EFAULT;
2990                 goto out;
2991         }
2992
2993         g_base = r_base + size;
2994         if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
2995                 ret = -EFAULT;
2996                 goto out;
2997         }
2998
2999         b_base = g_base + size;
3000         if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
3001                 ret = -EFAULT;
3002                 goto out;
3003         }
3004 out:
3005         mutex_unlock(&dev->mode_config.mutex);
3006         return ret;
3007 }
3008
3009 int drm_mode_page_flip_ioctl(struct drm_device *dev,
3010                              void *data, struct drm_file *file_priv)
3011 {
3012         struct drm_mode_crtc_page_flip *page_flip = data;
3013         struct drm_mode_object *obj;
3014         struct drm_crtc *crtc;
3015         struct drm_framebuffer *fb;
3016         struct drm_pending_vblank_event *e = NULL;
3017         unsigned long flags;
3018         int ret = -EINVAL;
3019
3020         if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
3021             page_flip->reserved != 0)
3022                 return -EINVAL;
3023
3024         mutex_lock(&dev->mode_config.mutex);
3025         obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
3026         if (!obj)
3027                 goto out;
3028         crtc = obj_to_crtc(obj);
3029
3030         if (crtc->fb == NULL) {
3031                 /* The framebuffer is currently unbound, presumably
3032                  * due to a hotplug event, that userspace has not
3033                  * yet discovered.
3034                  */
3035                 ret = -EBUSY;
3036                 goto out;
3037         }
3038
3039         if (crtc->funcs->page_flip == NULL)
3040                 goto out;
3041
3042         obj = drm_mode_object_find(dev, page_flip->fb_id, DRM_MODE_OBJECT_FB);
3043         if (!obj)
3044                 goto out;
3045         fb = obj_to_fb(obj);
3046
3047         if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3048                 ret = -ENOMEM;
3049                 spin_lock_irqsave(&dev->event_lock, flags);
3050                 if (file_priv->event_space < sizeof e->event) {
3051                         spin_unlock_irqrestore(&dev->event_lock, flags);
3052                         goto out;
3053                 }
3054                 file_priv->event_space -= sizeof e->event;
3055                 spin_unlock_irqrestore(&dev->event_lock, flags);
3056
3057                 e = kzalloc(sizeof *e, GFP_KERNEL);
3058                 if (e == NULL) {
3059                         spin_lock_irqsave(&dev->event_lock, flags);
3060                         file_priv->event_space += sizeof e->event;
3061                         spin_unlock_irqrestore(&dev->event_lock, flags);
3062                         goto out;
3063                 }
3064
3065                 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
3066                 e->event.base.length = sizeof e->event;
3067                 e->event.user_data = page_flip->user_data;
3068                 e->base.event = &e->event.base;
3069                 e->base.file_priv = file_priv;
3070                 e->base.destroy =
3071                         (void (*) (struct drm_pending_event *)) kfree;
3072         }
3073
3074         ret = crtc->funcs->page_flip(crtc, fb, e);
3075         if (ret) {
3076                 spin_lock_irqsave(&dev->event_lock, flags);
3077                 file_priv->event_space += sizeof e->event;
3078                 spin_unlock_irqrestore(&dev->event_lock, flags);
3079                 kfree(e);
3080         }
3081
3082 out:
3083         mutex_unlock(&dev->mode_config.mutex);
3084         return ret;
3085 }
3086
3087 void drm_mode_config_reset(struct drm_device *dev)
3088 {
3089         struct drm_crtc *crtc;
3090         struct drm_encoder *encoder;
3091         struct drm_connector *connector;
3092
3093         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3094                 if (crtc->funcs->reset)
3095                         crtc->funcs->reset(crtc);
3096
3097         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
3098                 if (encoder->funcs->reset)
3099                         encoder->funcs->reset(encoder);
3100
3101         list_for_each_entry(connector, &dev->mode_config.connector_list, head)
3102                 if (connector->funcs->reset)
3103                         connector->funcs->reset(connector);
3104 }
3105 EXPORT_SYMBOL(drm_mode_config_reset);
3106
3107 int drm_mode_create_dumb_ioctl(struct drm_device *dev,
3108                                void *data, struct drm_file *file_priv)
3109 {
3110         struct drm_mode_create_dumb *args = data;
3111
3112         if (!dev->driver->dumb_create)
3113                 return -ENOSYS;
3114         return dev->driver->dumb_create(file_priv, dev, args);
3115 }
3116
3117 int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
3118                              void *data, struct drm_file *file_priv)
3119 {
3120         struct drm_mode_map_dumb *args = data;
3121
3122         /* call driver ioctl to get mmap offset */
3123         if (!dev->driver->dumb_map_offset)
3124                 return -ENOSYS;
3125
3126         return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
3127 }
3128
3129 int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
3130                                 void *data, struct drm_file *file_priv)
3131 {
3132         struct drm_mode_destroy_dumb *args = data;
3133
3134         if (!dev->driver->dumb_destroy)
3135                 return -ENOSYS;
3136
3137         return dev->driver->dumb_destroy(file_priv, dev, args->handle);
3138 }