more WIP on blobs..
[platform/upstream/libdrm.git] / linux-core / drm_crtc.c
1 /*
2  * Copyright (c) 2006-2007 Intel Corporation
3  * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
4  *
5  * DRM core CRTC related functions
6  *
7  * Permission to use, copy, modify, distribute, and sell this software and its
8  * documentation for any purpose is hereby granted without fee, provided that
9  * the above copyright notice appear in all copies and that both that copyright
10  * notice and this permission notice appear in supporting documentation, and
11  * that the name of the copyright holders not be used in advertising or
12  * publicity pertaining to distribution of the software without specific,
13  * written prior permission.  The copyright holders make no representations
14  * about the suitability of this software for any purpose.  It is provided "as
15  * is" without express or implied warranty.
16  *
17  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
18  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
19  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
20  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
21  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
22  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
23  * OF THIS SOFTWARE.
24  *
25  * Authors:
26  *      Keith Packard
27  *      Eric Anholt <eric@anholt.net>
28  *      Dave Airlie <airlied@linux.ie>
29  *      Jesse Barnes <jesse.barnes@intel.com>
30  */
31 #include <linux/list.h>
32 #include "drm.h"
33 #include "drmP.h"
34 #include "drm_crtc.h"
35
36 /**
37  * drm_idr_get - allocate a new identifier
38  * @dev: DRM device
39  * @ptr: object pointer, used to generate unique ID
40  *
41  * LOCKING:
42  * Caller must hold DRM mode_config lock.
43  *
44  * Create a unique identifier based on @ptr in @dev's identifier space.  Used
45  * for tracking modes, CRTCs and outputs.
46  *
47  * RETURNS:
48  * New unique (relative to other objects in @dev) integer identifier for the
49  * object.
50  */
51 int drm_idr_get(struct drm_device *dev, void *ptr)
52 {
53         int new_id = 0;
54         int ret;
55 again:
56         if (idr_pre_get(&dev->mode_config.crtc_idr, GFP_KERNEL) == 0) {
57                 DRM_ERROR("Ran out memory getting a mode number\n");
58                 return 0;
59         }
60
61         ret = idr_get_new_above(&dev->mode_config.crtc_idr, ptr, 1, &new_id);
62         if (ret == -EAGAIN)
63                 goto again;     
64
65         return new_id;
66 }
67
68 /**
69  * drm_idr_put - free an identifer
70  * @dev: DRM device
71  * @id: ID to free
72  *
73  * LOCKING:
74  * Caller must hold DRM mode_config lock.
75  *
76  * Free @id from @dev's unique identifier pool.
77  */
78 void drm_idr_put(struct drm_device *dev, int id)
79 {
80         idr_remove(&dev->mode_config.crtc_idr, id);
81 }
82
83 /**
84  * drm_crtc_from_fb - find the CRTC structure associated with an fb
85  * @dev: DRM device
86  * @fb: framebuffer in question
87  *
88  * LOCKING:
89  * Caller must hold mode_config lock.
90  *
91  * Find CRTC in the mode_config structure that matches @fb.
92  *
93  * RETURNS:
94  * Pointer to the CRTC or NULL if it wasn't found.
95  */
96 struct drm_crtc *drm_crtc_from_fb(struct drm_device *dev,
97                                   struct drm_framebuffer *fb)
98 {
99         struct drm_crtc *crtc;
100
101         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
102                 if (crtc->fb == fb)
103                         return crtc;
104         }
105         return NULL;
106 }
107
108 /**
109  * drm_framebuffer_create - create a new framebuffer object
110  * @dev: DRM device
111  *
112  * LOCKING:
113  * Caller must hold mode config lock.
114  *
115  * Creates a new framebuffer objects and adds it to @dev's DRM mode_config.
116  *
117  * RETURNS:
118  * Pointer to new framebuffer or NULL on error.
119  */
120 struct drm_framebuffer *drm_framebuffer_create(struct drm_device *dev)
121 {
122         struct drm_framebuffer *fb;
123
124         /* Limit to single framebuffer for now */
125         if (dev->mode_config.num_fb > 1) {
126                 mutex_unlock(&dev->mode_config.mutex);
127                 DRM_ERROR("Attempt to add multiple framebuffers failed\n");
128                 return NULL;
129         }
130
131         fb = kzalloc(sizeof(struct drm_framebuffer), GFP_KERNEL);
132         if (!fb)
133                 return NULL;
134         
135         fb->id = drm_idr_get(dev, fb);
136         fb->dev = dev;
137         dev->mode_config.num_fb++;
138         list_add(&fb->head, &dev->mode_config.fb_list);
139
140         return fb;
141 }
142 EXPORT_SYMBOL(drm_framebuffer_create);
143
144 /**
145  * drm_framebuffer_destroy - remove a framebuffer object
146  * @fb: framebuffer to remove
147  *
148  * LOCKING:
149  * Caller must hold mode config lock.
150  *
151  * Scans all the CRTCs in @dev's mode_config.  If they're using @fb, removes
152  * it, setting it to NULL.
153  */
154 void drm_framebuffer_destroy(struct drm_framebuffer *fb)
155 {
156         struct drm_device *dev = fb->dev;
157         struct drm_crtc *crtc;
158
159         /* remove from any CRTC */
160         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
161                 if (crtc->fb == fb)
162                         crtc->fb = NULL;
163         }
164
165         drm_idr_put(dev, fb->id);
166         list_del(&fb->head);
167         dev->mode_config.num_fb--;
168
169         kfree(fb);
170 }
171 EXPORT_SYMBOL(drm_framebuffer_destroy);
172
173 /**
174  * drm_crtc_create - create a new CRTC object
175  * @dev: DRM device
176  * @funcs: callbacks for the new CRTC
177  *
178  * LOCKING:
179  * Caller must hold mode config lock.
180  *
181  * Creates a new CRTC object and adds it to @dev's mode_config structure.
182  *
183  * RETURNS:
184  * Pointer to new CRTC object or NULL on error.
185  */
186 struct drm_crtc *drm_crtc_create(struct drm_device *dev,
187                                  const struct drm_crtc_funcs *funcs)
188 {
189         struct drm_crtc *crtc;
190
191         crtc = kzalloc(sizeof(struct drm_crtc), GFP_KERNEL);
192         if (!crtc)
193                 return NULL;
194
195         crtc->dev = dev;
196         crtc->funcs = funcs;
197
198         crtc->id = drm_idr_get(dev, crtc);
199
200         list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
201         dev->mode_config.num_crtc++;
202
203         return crtc;
204 }
205 EXPORT_SYMBOL(drm_crtc_create);
206
207 /**
208  * drm_crtc_destroy - remove a CRTC object
209  * @crtc: CRTC to remove
210  *
211  * LOCKING:
212  * Caller must hold mode config lock.
213  *
214  * Cleanup @crtc.  Calls @crtc's cleanup function, then removes @crtc from
215  * its associated DRM device's mode_config.  Frees it afterwards.
216  */
217 void drm_crtc_destroy(struct drm_crtc *crtc)
218 {
219         struct drm_device *dev = crtc->dev;
220
221         if (crtc->funcs->cleanup)
222                 (*crtc->funcs->cleanup)(crtc);
223
224         drm_idr_put(dev, crtc->id);
225         list_del(&crtc->head);
226         dev->mode_config.num_crtc--;
227         kfree(crtc);
228 }
229 EXPORT_SYMBOL(drm_crtc_destroy);
230
231 /**
232  * drm_crtc_in_use - check if a given CRTC is in a mode_config
233  * @crtc: CRTC to check
234  *
235  * LOCKING:
236  * Caller must hold mode config lock.
237  *
238  * Walk @crtc's DRM device's mode_config and see if it's in use.
239  *
240  * RETURNS:
241  * True if @crtc is part of the mode_config, false otherwise.
242  */
243 bool drm_crtc_in_use(struct drm_crtc *crtc)
244 {
245         struct drm_output *output;
246         struct drm_device *dev = crtc->dev;
247         /* FIXME: Locking around list access? */
248         list_for_each_entry(output, &dev->mode_config.output_list, head)
249                 if (output->crtc == crtc)
250                         return true;
251         return false;
252 }
253 EXPORT_SYMBOL(drm_crtc_in_use);
254
255 /*
256  * Detailed mode info for a standard 640x480@60Hz monitor
257  */
258 static struct drm_display_mode std_mode[] = {
259         { DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 25200, 640, 656,
260                    752, 800, 0, 480, 490, 492, 525, 0,
261                    V_NHSYNC | V_NVSYNC) }, /* 640x480@60Hz */
262 };
263
264 /**
265  * drm_crtc_probe_output_modes - get complete set of display modes
266  * @dev: DRM device
267  * @maxX: max width for modes
268  * @maxY: max height for modes
269  *
270  * LOCKING:
271  * Caller must hold mode config lock.
272  *
273  * Based on @dev's mode_config layout, scan all the outputs and try to detect
274  * modes on them.  Modes will first be added to the output's probed_modes
275  * list, then culled (based on validity and the @maxX, @maxY parameters) and
276  * put into the normal modes list.
277  *
278  * Intended to be used either at bootup time or when major configuration
279  * changes have occurred.
280  *
281  * FIXME: take into account monitor limits
282  */
283 void drm_crtc_probe_single_output_modes(struct drm_output *output, int maxX, int maxY)
284 {
285         struct drm_device *dev = output->dev;
286         struct drm_display_mode *mode, *t;
287         int ret;
288         //if (maxX == 0 || maxY == 0) 
289         // TODO
290
291         /* set all modes to the unverified state */
292         list_for_each_entry_safe(mode, t, &output->modes, head)
293                 mode->status = MODE_UNVERIFIED;
294                 
295         output->status = (*output->funcs->detect)(output);
296         
297         if (output->status == output_status_disconnected) {
298                 DRM_DEBUG("%s is disconnected\n", output->name);
299                 /* TODO set EDID to NULL */
300                 return;
301         }
302         
303         ret = (*output->funcs->get_modes)(output);
304         
305         if (ret) {
306                 drm_mode_output_list_update(output);
307         }
308         
309         if (maxX && maxY)
310                 drm_mode_validate_size(dev, &output->modes, maxX,
311                                        maxY, 0);
312         list_for_each_entry_safe(mode, t, &output->modes, head) {
313                 if (mode->status == MODE_OK)
314                         mode->status = (*output->funcs->mode_valid)(output,mode);
315         }
316         
317         
318         drm_mode_prune_invalid(dev, &output->modes, TRUE);
319         
320         if (list_empty(&output->modes)) {
321                 struct drm_display_mode *stdmode;
322                 
323                 DRM_DEBUG("No valid modes on %s\n", output->name);
324                 
325                 /* Should we do this here ???
326                  * When no valid EDID modes are available we end up
327                  * here and bailed in the past, now we add a standard
328                  * 640x480@60Hz mode and carry on.
329                  */
330                 stdmode = drm_mode_duplicate(dev, &std_mode[0]);
331                 drm_mode_probed_add(output, stdmode);
332                 drm_mode_list_concat(&output->probed_modes,
333                                      &output->modes);
334                 
335                 DRM_DEBUG("Adding standard 640x480 @ 60Hz to %s\n",
336                           output->name);
337         }
338         
339         drm_mode_sort(&output->modes);
340         
341         DRM_DEBUG("Probed modes for %s\n", output->name);
342         list_for_each_entry_safe(mode, t, &output->modes, head) {
343                 mode->vrefresh = drm_mode_vrefresh(mode);
344                 
345                 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
346                 drm_mode_debug_printmodeline(dev, mode);
347         }
348 }
349
350 void drm_crtc_probe_output_modes(struct drm_device *dev, int maxX, int maxY)
351 {
352         struct drm_output *output;
353
354         list_for_each_entry(output, &dev->mode_config.output_list, head) {
355                 drm_crtc_probe_single_output_modes(output, maxX, maxY);
356         }
357 }
358
359 /**
360  * drm_crtc_set_mode - set a mode
361  * @crtc: CRTC to program
362  * @mode: mode to use
363  * @x: width of mode
364  * @y: height of mode
365  *
366  * LOCKING:
367  * Caller must hold mode config lock.
368  *
369  * Try to set @mode on @crtc.  Give @crtc and its associated outputs a chance
370  * to fixup or reject the mode prior to trying to set it.
371  *
372  * RETURNS:
373  * True if the mode was set successfully, or false otherwise.
374  */
375 bool drm_crtc_set_mode(struct drm_crtc *crtc, struct drm_display_mode *mode,
376                        int x, int y)
377 {
378         struct drm_device *dev = crtc->dev;
379         struct drm_display_mode *adjusted_mode, saved_mode;
380         int saved_x, saved_y;
381         bool didLock = false;
382         bool ret = false;
383         struct drm_output *output;
384
385         adjusted_mode = drm_mode_duplicate(dev, mode);
386
387         crtc->enabled = drm_crtc_in_use(crtc);
388
389         if (!crtc->enabled) {
390                 return true;
391         }
392
393         didLock = crtc->funcs->lock(crtc);
394
395         saved_mode = crtc->mode;
396         saved_x = crtc->x;
397         saved_y = crtc->y;
398         
399         /* Update crtc values up front so the driver can rely on them for mode
400          * setting.
401          */
402         crtc->mode = *mode;
403         crtc->x = x;
404         crtc->y = y;
405
406         /* XXX short-circuit changes to base location only */
407         
408         /* Pass our mode to the outputs and the CRTC to give them a chance to
409          * adjust it according to limitations or output properties, and also
410          * a chance to reject the mode entirely.
411          */
412         list_for_each_entry(output, &dev->mode_config.output_list, head) {
413                 
414                 if (output->crtc != crtc)
415                         continue;
416                 
417                 if (!output->funcs->mode_fixup(output, mode, adjusted_mode)) {
418                         goto done;
419                 }
420         }
421         
422         if (!crtc->funcs->mode_fixup(crtc, mode, adjusted_mode)) {
423                 goto done;
424         }
425
426         /* Prepare the outputs and CRTCs before setting the mode. */
427         list_for_each_entry(output, &dev->mode_config.output_list, head) {
428
429                 if (output->crtc != crtc)
430                         continue;
431                 
432                 /* Disable the output as the first thing we do. */
433                 output->funcs->prepare(output);
434         }
435         
436         crtc->funcs->prepare(crtc);
437         
438         /* Set up the DPLL and any output state that needs to adjust or depend
439          * on the DPLL.
440          */
441         crtc->funcs->mode_set(crtc, mode, adjusted_mode, x, y);
442
443         list_for_each_entry(output, &dev->mode_config.output_list, head) {
444
445                 if (output->crtc != crtc)
446                         continue;
447                 
448                 DRM_INFO("%s: set mode %s %x\n", output->name, mode->name, mode->mode_id);
449
450                 output->funcs->mode_set(output, mode, adjusted_mode);
451         }
452         
453         /* Now, enable the clocks, plane, pipe, and outputs that we set up. */
454         crtc->funcs->commit(crtc);
455
456         list_for_each_entry(output, &dev->mode_config.output_list, head) {
457
458                 if (output->crtc != crtc)
459                         continue;
460                 
461                 output->funcs->commit(output);
462
463 #if 0 // TODO def RANDR_12_INTERFACE
464                 if (output->randr_output)
465                         RRPostPendingProperties (output->randr_output);
466 #endif
467         }
468         
469         /* XXX free adjustedmode */
470         drm_mode_destroy(dev, adjusted_mode);
471         ret = TRUE;
472         /* TODO */
473 //      if (scrn->pScreen)
474 //              drm_crtc_set_screen_sub_pixel_order(dev);
475
476 done:
477         if (!ret) {
478                 crtc->x = saved_x;
479                 crtc->y = saved_y;
480                 crtc->mode = saved_mode;
481         }
482         
483         if (didLock)
484                 crtc->funcs->unlock (crtc);
485         
486         return ret;
487 }
488 EXPORT_SYMBOL(drm_crtc_set_mode);
489
490 /**
491  * drm_disable_unused_functions - disable unused objects
492  * @dev: DRM device
493  *
494  * LOCKING:
495  * Caller must hold mode config lock.
496  *
497  * If an output or CRTC isn't part of @dev's mode_config, it can be disabled
498  * by calling its dpms function, which should power it off.
499  */
500 void drm_disable_unused_functions(struct drm_device *dev)
501 {
502         struct drm_output *output;
503         struct drm_crtc *crtc;
504
505         list_for_each_entry(output, &dev->mode_config.output_list, head) {
506                 if (!output->crtc)
507                         (*output->funcs->dpms)(output, DPMSModeOff);
508         }
509
510         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
511                 if (!crtc->enabled)
512                         crtc->funcs->dpms(crtc, DPMSModeOff);
513         }
514 }
515         
516 /**
517  * drm_mode_probed_add - add a mode to the specified output's probed mode list
518  * @output: output the new mode
519  * @mode: mode data
520  *
521  * LOCKING:
522  * Caller must hold mode config lock.
523  * 
524  * Add @mode to @output's mode list for later use.
525  */
526 void drm_mode_probed_add(struct drm_output *output,
527                          struct drm_display_mode *mode)
528 {
529         list_add(&mode->head, &output->probed_modes);
530 }
531 EXPORT_SYMBOL(drm_mode_probed_add);
532
533 /**
534  * drm_mode_remove - remove and free a mode
535  * @output: output list to modify
536  * @mode: mode to remove
537  *
538  * LOCKING:
539  * Caller must hold mode config lock.
540  * 
541  * Remove @mode from @output's mode list, then free it.
542  */
543 void drm_mode_remove(struct drm_output *output, struct drm_display_mode *mode)
544 {
545         list_del(&mode->head);
546         kfree(mode);
547 }
548 EXPORT_SYMBOL(drm_mode_remove);
549
550 /**
551  * drm_output_create - create a new output
552  * @dev: DRM device
553  * @funcs: callbacks for this output
554  * @name: user visible name of the output
555  *
556  * LOCKING:
557  * Caller must hold @dev's mode_config lock.
558  *
559  * Creates a new drm_output structure and adds it to @dev's mode_config
560  * structure.
561  *
562  * RETURNS:
563  * Pointer to the new output or NULL on error.
564  */
565 struct drm_output *drm_output_create(struct drm_device *dev,
566                                      const struct drm_output_funcs *funcs,
567                                      const char *name)
568 {
569         struct drm_output *output = NULL;
570
571         output = kzalloc(sizeof(struct drm_output), GFP_KERNEL);
572         if (!output)
573                 return NULL;
574                 
575         output->dev = dev;
576         output->funcs = funcs;
577         output->id = drm_idr_get(dev, output);
578         if (name)
579                 strncpy(output->name, name, DRM_OUTPUT_LEN);
580         output->name[DRM_OUTPUT_LEN - 1] = 0;
581         output->subpixel_order = SubPixelUnknown;
582         INIT_LIST_HEAD(&output->user_modes);
583         INIT_LIST_HEAD(&output->probed_modes);
584         INIT_LIST_HEAD(&output->modes);
585         /* randr_output? */
586         /* output_set_monitor(output)? */
587         /* check for output_ignored(output)? */
588
589         mutex_lock(&dev->mode_config.mutex);
590         list_add_tail(&output->head, &dev->mode_config.output_list);
591         dev->mode_config.num_output++;
592
593         mutex_unlock(&dev->mode_config.mutex);
594
595         return output;
596
597 }
598 EXPORT_SYMBOL(drm_output_create);
599
600 /**
601  * drm_output_destroy - remove an output
602  * @output: output to remove
603  *
604  * LOCKING:
605  * Caller must hold @dev's mode_config lock.
606  *
607  * Call @output's cleanup function, then remove the output from the DRM
608  * mode_config after freeing @output's modes.
609  */
610 void drm_output_destroy(struct drm_output *output)
611 {
612         struct drm_device *dev = output->dev;
613         struct drm_display_mode *mode, *t;
614
615         if (*output->funcs->cleanup)
616                 (*output->funcs->cleanup)(output);
617
618         list_for_each_entry_safe(mode, t, &output->probed_modes, head)
619                 drm_mode_remove(output, mode);
620
621         list_for_each_entry_safe(mode, t, &output->modes, head)
622                 drm_mode_remove(output, mode);
623
624         list_for_each_entry_safe(mode, t, &output->user_modes, head)
625                 drm_mode_remove(output, mode);
626
627         mutex_lock(&dev->mode_config.mutex);
628         drm_idr_put(dev, output->id);
629         list_del(&output->head);
630         mutex_unlock(&dev->mode_config.mutex);
631         kfree(output);
632 }
633 EXPORT_SYMBOL(drm_output_destroy);
634
635 /**
636  * drm_output_rename - rename an output
637  * @output: output to rename
638  * @name: new user visible name
639  *
640  * LOCKING:
641  * None.
642  *
643  * Simply stuff a new name into @output's name field, based on @name.
644  *
645  * RETURNS:
646  * True if the name was changed, false otherwise.
647  */
648 bool drm_output_rename(struct drm_output *output, const char *name)
649 {
650         if (!name)
651                 return false;
652
653         strncpy(output->name, name, DRM_OUTPUT_LEN);
654         output->name[DRM_OUTPUT_LEN - 1] = 0;
655
656         DRM_DEBUG("Changed name to %s\n", output->name);
657 //      drm_output_set_monitor(output);
658 //      if (drm_output_ignored(output))
659 //              return FALSE;
660
661         return TRUE;
662 }
663 EXPORT_SYMBOL(drm_output_rename);
664
665 /**
666  * drm_mode_create - create a new display mode
667  * @dev: DRM device
668  *
669  * LOCKING:
670  * None.
671  *
672  * Create a new drm_display_mode, give it an ID, and return it.
673  *
674  * RETURNS:
675  * Pointer to new mode on success, NULL on error.
676  */
677 struct drm_display_mode *drm_mode_create(struct drm_device *dev)
678 {
679         struct drm_display_mode *nmode;
680
681         nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
682         if (!nmode)
683                 return NULL;
684
685         nmode->mode_id = drm_idr_get(dev, nmode);
686         return nmode;
687 }
688 EXPORT_SYMBOL(drm_mode_create);
689
690 /**
691  * drm_mode_destroy - remove a mode
692  * @dev: DRM device
693  * @mode: mode to remove
694  *
695  * LOCKING:
696  * Caller must hold mode config lock.
697  *
698  * Free @mode's unique identifier, then free it.
699  */
700 void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
701 {
702         drm_idr_put(dev, mode->mode_id);
703
704         kfree(mode);
705 }
706 EXPORT_SYMBOL(drm_mode_destroy);
707
708 /**
709  * drm_mode_config_init - initialize DRM mode_configuration structure
710  * @dev: DRM device
711  *
712  * LOCKING:
713  * None, should happen single threaded at init time.
714  *
715  * Initialize @dev's mode_config structure, used for tracking the graphics
716  * configuration of @dev.
717  */
718 void drm_mode_config_init(struct drm_device *dev)
719 {
720         mutex_init(&dev->mode_config.mutex);
721         INIT_LIST_HEAD(&dev->mode_config.fb_list);
722         INIT_LIST_HEAD(&dev->mode_config.crtc_list);
723         INIT_LIST_HEAD(&dev->mode_config.output_list);
724         INIT_LIST_HEAD(&dev->mode_config.property_list);
725         INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
726         idr_init(&dev->mode_config.crtc_idr);
727         dev->mode_config.edid_property = drm_property_create(dev,
728                                                              DRM_MODE_PROP_BLOB | DRM_MODE_PROP_IMMUTABLE,
729                                                              "EDID", 0);
730 }
731 EXPORT_SYMBOL(drm_mode_config_init);
732
733 /**
734  * drm_get_buffer_object - find the buffer object for a given handle
735  * @dev: DRM device
736  * @bo: pointer to caller's buffer_object pointer
737  * @handle: handle to lookup
738  *
739  * LOCKING:
740  * Must take @dev's struct_mutex to protect buffer object lookup.
741  *
742  * Given @handle, lookup the buffer object in @dev and put it in the caller's
743  * @bo pointer.
744  *
745  * RETURNS:
746  * Zero on success, -EINVAL if the handle couldn't be found.
747  */
748 static int drm_get_buffer_object(struct drm_device *dev, struct drm_buffer_object **bo, unsigned long handle)
749 {
750         struct drm_user_object *uo;
751         struct drm_hash_item *hash;
752         int ret;
753
754         *bo = NULL;
755
756         mutex_lock(&dev->struct_mutex);
757         ret = drm_ht_find_item(&dev->object_hash, handle, &hash);
758         if (ret) {
759                 DRM_ERROR("Couldn't find handle.\n");
760                 ret = -EINVAL;
761                 goto out_err;
762         }
763
764         uo = drm_hash_entry(hash, struct drm_user_object, hash);
765         if (uo->type != drm_buffer_type) {
766                 ret = -EINVAL;
767                 goto out_err;
768         }
769         
770         *bo = drm_user_object_entry(uo, struct drm_buffer_object, base);
771         ret = 0;
772 out_err:
773         mutex_unlock(&dev->struct_mutex);
774         return ret;
775 }
776
777 /**
778  * drm_pick_crtcs - pick crtcs for output devices
779  * @dev: DRM device
780  *
781  * LOCKING:
782  * Caller must hold mode config lock.
783  */
784 static void drm_pick_crtcs (struct drm_device *dev)
785 {
786         int c, o, assigned;
787         struct drm_output *output, *output_equal;
788         struct drm_crtc   *crtc;
789         struct drm_display_mode *des_mode = NULL, *modes, *modes_equal;
790
791         list_for_each_entry(output, &dev->mode_config.output_list, head) {
792                 output->crtc = NULL;
793     
794                 /* Don't hook up outputs that are disconnected ??
795                  *
796                  * This is debateable. Do we want fixed /dev/fbX or
797                  * dynamic on hotplug (need mode code for that though) ?
798                  *
799                  * If we don't hook up outputs now, then we only create
800                  * /dev/fbX for the output that's enabled, that's good as
801                  * the users console will be on that output.
802                  *
803                  * If we do hook up outputs that are disconnected now, then
804                  * the user may end up having to muck about with the fbcon
805                  * map flags to assign his console to the enabled output. Ugh.
806                  */
807                 if (output->status != output_status_connected)
808                         continue;
809
810                 des_mode = NULL;
811                 list_for_each_entry(des_mode, &output->modes, head) {
812                         if (des_mode->type & DRM_MODE_TYPE_PREFERRED)
813                                 break;
814                 }
815
816                 /* No preferred mode, let's just select the first available */
817                 if (!des_mode || !(des_mode->type & DRM_MODE_TYPE_PREFERRED)) {
818                         list_for_each_entry(des_mode, &output->modes, head) {
819                                 if (des_mode)
820                                         break;
821                         }
822                 }
823
824                 c = -1;
825                 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
826                         assigned = 0;
827
828                         c++;
829                         if ((output->possible_crtcs & (1 << c)) == 0)
830                                 continue;
831         
832                         list_for_each_entry(output_equal, &dev->mode_config.output_list, head) {
833                                 if (output->id == output_equal->id)
834                                         continue;
835
836                                 /* Find out if crtc has been assigned before */
837                                 if (output_equal->crtc == crtc)
838                                         assigned = 1;
839                         }
840
841 #if 1 /* continue for now */
842                         if (assigned)
843                                 continue;
844 #endif
845
846                         o = -1;
847                         list_for_each_entry(output_equal, &dev->mode_config.output_list, head) {
848                                 o++;
849                                 if (output->id == output_equal->id)
850                                         continue;
851
852                                 list_for_each_entry(modes, &output->modes, head) {
853                                         list_for_each_entry(modes_equal, &output_equal->modes, head) {
854                                                 if (drm_mode_equal (modes, modes_equal)) {
855                                                         if ((output->possible_clones & output_equal->possible_clones) && (output_equal->crtc == crtc)) {
856                                                                 printk("Cloning %s (0x%lx) to %s (0x%lx)\n",output->name,output->possible_clones,output_equal->name,output_equal->possible_clones);
857                                                                 assigned = 0;
858                                                                 goto clone;
859                                                         }
860                                                 }
861                                         }
862                                 }
863                         }
864
865 clone:
866                         /* crtc has been assigned skip it */
867                         if (assigned)
868                                 continue;
869
870                         /* Found a CRTC to attach to, do it ! */
871                         output->crtc = crtc;
872                         output->crtc->desired_mode = des_mode;
873                         output->initial_x = 0;
874                         output->initial_y = 0;
875                         DRM_DEBUG("Desired mode for CRTC %d is 0x%x:%s\n",c,des_mode->mode_id, des_mode->name);
876                         break;
877                 }
878         }
879 }
880
881
882 /**
883  * drm_initial_config - setup a sane initial output configuration
884  * @dev: DRM device
885  * @can_grow: this configuration is growable
886  *
887  * LOCKING:
888  * Called at init time, must take mode config lock.
889  *
890  * Scan the CRTCs and outputs and try to put together an initial setup.
891  * At the moment, this is a cloned configuration across all heads with
892  * a new framebuffer object as the backing store.
893  *
894  * RETURNS:
895  * Zero if everything went ok, nonzero otherwise.
896  */
897 bool drm_initial_config(struct drm_device *dev, bool can_grow)
898 {
899         struct drm_output *output;
900         struct drm_crtc *crtc;
901         int ret = false;
902
903         mutex_lock(&dev->mode_config.mutex);
904
905         drm_crtc_probe_output_modes(dev, 2048, 2048);
906
907         drm_pick_crtcs(dev);
908
909         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
910
911                 /* can't setup the crtc if there's no assigned mode */
912                 if (!crtc->desired_mode)
913                         continue;
914
915                 /* Now setup the fbdev for attached crtcs */
916                 dev->driver->fb_probe(dev, crtc);
917         }
918
919         /* This is a little screwy, as we've already walked the outputs 
920          * above, but it's a little bit of magic too. There's the potential
921          * for things not to get setup above if an existing device gets
922          * re-assigned thus confusing the hardware. By walking the outputs
923          * this fixes up their crtc's.
924          */
925         list_for_each_entry(output, &dev->mode_config.output_list, head) {
926
927                 /* can't setup the output if there's no assigned mode */
928                 if (!output->crtc || !output->crtc->desired_mode)
929                         continue;
930
931                 /* and needs an attached fb */
932                 if (output->crtc->fb)
933                         drm_crtc_set_mode(output->crtc, output->crtc->desired_mode, 0, 0);
934         }
935
936         drm_disable_unused_functions(dev);
937
938         mutex_unlock(&dev->mode_config.mutex);
939         return ret;
940 }
941 EXPORT_SYMBOL(drm_initial_config);
942
943 /**
944  * drm_mode_config_cleanup - free up DRM mode_config info
945  * @dev: DRM device
946  *
947  * LOCKING:
948  * Caller must hold mode config lock.
949  *
950  * Free up all the outputs and CRTCs associated with this DRM device, then
951  * free up the framebuffers and associated buffer objects.
952  *
953  * FIXME: cleanup any dangling user buffer objects too
954  */
955 void drm_mode_config_cleanup(struct drm_device *dev)
956 {
957         struct drm_output *output, *ot;
958         struct drm_crtc *crtc, *ct;
959         struct drm_framebuffer *fb, *fbt;
960         struct drm_property *property, *pt;
961
962         list_for_each_entry_safe(output, ot, &dev->mode_config.output_list, head) {
963                 drm_output_destroy(output);
964         }
965
966         list_for_each_entry_safe(property, pt, &dev->mode_config.property_list, head) {
967                 drm_property_destroy(dev, property);
968         }
969
970         list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
971                 if (fb->bo->type != drm_bo_type_kernel)
972                         drm_framebuffer_destroy(fb);
973                 else
974                         dev->driver->fb_remove(dev, drm_crtc_from_fb(dev, fb));
975         }
976
977         list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
978                 drm_crtc_destroy(crtc);
979         }
980
981 }
982 EXPORT_SYMBOL(drm_mode_config_cleanup);
983
984 /**
985  * drm_crtc_set_config - set a new config from userspace
986  * @crtc: CRTC to setup
987  * @crtc_info: user provided configuration
988  * @new_mode: new mode to set
989  * @output_set: set of outputs for the new config
990  * @fb: new framebuffer
991  *
992  * LOCKING:
993  * Caller must hold mode config lock.
994  *
995  * Setup a new configuration, provided by the user in @crtc_info, and enable
996  * it.
997  *
998  * RETURNS:
999  * Zero. (FIXME)
1000  */
1001 int drm_crtc_set_config(struct drm_crtc *crtc, struct drm_mode_crtc *crtc_info, struct drm_display_mode *new_mode, struct drm_output **output_set, struct drm_framebuffer *fb)
1002 {
1003         struct drm_device *dev = crtc->dev;
1004         struct drm_crtc **save_crtcs, *new_crtc;
1005         bool save_enabled = crtc->enabled;
1006         bool changed;
1007         struct drm_output *output;
1008         int count = 0, ro;
1009
1010         save_crtcs = kzalloc(dev->mode_config.num_crtc * sizeof(struct drm_crtc *), GFP_KERNEL);
1011         if (!save_crtcs)
1012                 return -ENOMEM;
1013
1014         if (crtc->fb != fb)
1015                 changed = true;
1016
1017         if (crtc_info->x != crtc->x || crtc_info->y != crtc->y)
1018                 changed = true;
1019
1020         if (new_mode && !drm_mode_equal(new_mode, &crtc->mode))
1021                 changed = true;
1022
1023         list_for_each_entry(output, &dev->mode_config.output_list, head) {
1024                 save_crtcs[count++] = output->crtc;
1025
1026                 if (output->crtc == crtc)
1027                         new_crtc = NULL;
1028                 else
1029                         new_crtc = output->crtc;
1030
1031                 for (ro = 0; ro < crtc_info->count_outputs; ro++) {
1032                         if (output_set[ro] == output)
1033                                 new_crtc = crtc;
1034                 }
1035                 if (new_crtc != output->crtc) {
1036                         changed = true;
1037                         output->crtc = new_crtc;
1038                 }
1039         }
1040
1041         if (changed) {
1042                 crtc->fb = fb;
1043                 crtc->enabled = (new_mode != NULL);
1044                 if (new_mode != NULL) {
1045                         DRM_DEBUG("attempting to set mode from userspace\n");
1046                         drm_mode_debug_printmodeline(dev, new_mode);
1047                         if (!drm_crtc_set_mode(crtc, new_mode, crtc_info->x,
1048                                                crtc_info->y)) {
1049                                 crtc->enabled = save_enabled;
1050                                 count = 0;
1051                                 list_for_each_entry(output, &dev->mode_config.output_list, head)
1052                                         output->crtc = save_crtcs[count++];
1053                                 kfree(save_crtcs);
1054                                 return -EINVAL;
1055                         }
1056                         crtc->desired_x = crtc_info->x;
1057                         crtc->desired_y = crtc_info->y;
1058                         crtc->desired_mode = new_mode;
1059                 }
1060                 drm_disable_unused_functions(dev);
1061         }
1062         kfree(save_crtcs);
1063         return 0;
1064 }
1065
1066 /**
1067  * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1068  * @out: drm_mode_modeinfo struct to return to the user
1069  * @in: drm_display_mode to use
1070  *
1071  * LOCKING:
1072  * None.
1073  *
1074  * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1075  * the user.
1076  */
1077 void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out, struct drm_display_mode *in)
1078 {
1079         out->clock = in->clock;
1080         out->hdisplay = in->hdisplay;
1081         out->hsync_start = in->hsync_start;
1082         out->hsync_end = in->hsync_end;
1083         out->htotal = in->htotal;
1084         out->hskew = in->hskew;
1085         out->vdisplay = in->vdisplay;
1086         out->vsync_start = in->vsync_start;
1087         out->vsync_end = in->vsync_end;
1088         out->vtotal = in->vtotal;
1089         out->vscan = in->vscan;
1090         out->vrefresh = in->vrefresh;
1091         out->flags = in->flags;
1092         out->type = in->type;
1093         strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1094         out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1095 }
1096
1097 /**
1098  * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
1099  * @out: drm_display_mode to return to the user
1100  * @in: drm_mode_modeinfo to use
1101  *
1102  * LOCKING:
1103  * None.
1104  *
1105  * Convert a drmo_mode_modeinfo into a drm_display_mode structure to return to
1106  * the caller.
1107  */
1108 void drm_crtc_convert_umode(struct drm_display_mode *out, struct drm_mode_modeinfo *in)
1109 {
1110         out->clock = in->clock;
1111         out->hdisplay = in->hdisplay;
1112         out->hsync_start = in->hsync_start;
1113         out->hsync_end = in->hsync_end;
1114         out->htotal = in->htotal;
1115         out->hskew = in->hskew;
1116         out->vdisplay = in->vdisplay;
1117         out->vsync_start = in->vsync_start;
1118         out->vsync_end = in->vsync_end;
1119         out->vtotal = in->vtotal;
1120         out->vscan = in->vscan;
1121         out->vrefresh = in->vrefresh;
1122         out->flags = in->flags;
1123         out->type = in->type;
1124         strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1125         out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1126 }
1127         
1128 /**
1129  * drm_mode_getresources - get graphics configuration
1130  * @inode: inode from the ioctl
1131  * @filp: file * from the ioctl
1132  * @cmd: cmd from ioctl
1133  * @arg: arg from ioctl
1134  *
1135  * LOCKING:
1136  * Takes mode config lock.
1137  *
1138  * Construct a set of configuration description structures and return
1139  * them to the user, including CRTC, output and framebuffer configuration.
1140  *
1141  * Called by the user via ioctl.
1142  *
1143  * RETURNS:
1144  * Zero on success, errno on failure.
1145  */
1146 int drm_mode_getresources(struct drm_device *dev,
1147                           void *data, struct drm_file *file_priv)
1148 {
1149         struct drm_mode_card_res *card_res = data;
1150         struct list_head *lh;
1151         struct drm_framebuffer *fb;
1152         struct drm_output *output;
1153         struct drm_crtc *crtc;
1154         int ret = 0;
1155         int output_count = 0;
1156         int crtc_count = 0;
1157         int fb_count = 0;
1158         int copied = 0;
1159         uint32_t __user *fb_id;
1160         uint32_t __user *crtc_id;
1161         uint32_t __user *output_id;
1162
1163         mutex_lock(&dev->mode_config.mutex);
1164
1165         list_for_each(lh, &dev->mode_config.fb_list)
1166                 fb_count++;
1167
1168         list_for_each(lh, &dev->mode_config.crtc_list)
1169                 crtc_count++;
1170
1171         list_for_each(lh, &dev->mode_config.output_list)
1172                 output_count++;
1173
1174         card_res->max_height = dev->mode_config.max_height;
1175         card_res->min_height = dev->mode_config.min_height;
1176         card_res->max_width = dev->mode_config.max_width;
1177         card_res->min_width = dev->mode_config.min_width;
1178
1179         /* handle this in 4 parts */
1180         /* FBs */
1181         if (card_res->count_fbs >= fb_count) {
1182                 copied = 0;
1183                 fb_id = (uint32_t *)(unsigned long)card_res->fb_id_ptr;
1184                 list_for_each_entry(fb, &dev->mode_config.fb_list, head) {
1185                         if (put_user(fb->id, fb_id + copied)) {
1186                                 ret = -EFAULT;
1187                                 goto out;
1188                         }
1189                         copied++;
1190                 }
1191         }
1192         card_res->count_fbs = fb_count;
1193
1194         /* CRTCs */
1195         if (card_res->count_crtcs >= crtc_count) {
1196                 copied = 0;
1197                 crtc_id = (uint32_t *)(unsigned long)card_res->crtc_id_ptr;
1198                 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head){
1199                         DRM_DEBUG("CRTC ID is %d\n", crtc->id);
1200                         if (put_user(crtc->id, crtc_id + copied)) {
1201                                 ret = -EFAULT;
1202                                 goto out;
1203                         }
1204                         copied++;
1205                 }
1206         }
1207         card_res->count_crtcs = crtc_count;
1208
1209
1210         /* Outputs */
1211         if (card_res->count_outputs >= output_count) {
1212                 copied = 0;
1213                 output_id = (uint32_t *)(unsigned long)card_res->output_id_ptr;
1214                 list_for_each_entry(output, &dev->mode_config.output_list,
1215                                     head) {
1216                         DRM_DEBUG("OUTPUT ID is %d\n", output->id);
1217                         if (put_user(output->id, output_id + copied)) {
1218                                 ret = -EFAULT;
1219                                 goto out;
1220                         }
1221                         copied++;
1222                 }
1223         }
1224         card_res->count_outputs = output_count;
1225         
1226         DRM_DEBUG("Counted %d %d\n", card_res->count_crtcs,
1227                   card_res->count_outputs);
1228
1229 out:    
1230         mutex_unlock(&dev->mode_config.mutex);
1231         return ret;
1232 }
1233
1234 /**
1235  * drm_mode_getcrtc - get CRTC configuration
1236  * @inode: inode from the ioctl
1237  * @filp: file * from the ioctl
1238  * @cmd: cmd from ioctl
1239  * @arg: arg from ioctl
1240  *
1241  * LOCKING:
1242  * Caller? (FIXME)
1243  *
1244  * Construct a CRTC configuration structure to return to the user.
1245  *
1246  * Called by the user via ioctl.
1247  *
1248  * RETURNS:
1249  * Zero on success, errno on failure.
1250  */
1251 int drm_mode_getcrtc(struct drm_device *dev,
1252                      void *data, struct drm_file *file_priv)
1253 {
1254         struct drm_mode_crtc *crtc_resp = data;
1255         struct drm_crtc *crtc;
1256         struct drm_output *output;
1257         int ocount;
1258         int ret = 0;
1259
1260         mutex_lock(&dev->mode_config.mutex);
1261         crtc = idr_find(&dev->mode_config.crtc_idr, crtc_resp->crtc_id);
1262         if (!crtc || (crtc->id != crtc_resp->crtc_id)) {
1263                 ret = -EINVAL;
1264                 goto out;
1265         }
1266
1267         crtc_resp->x = crtc->x;
1268         crtc_resp->y = crtc->y;
1269
1270         if (crtc->fb)
1271                 crtc_resp->fb_id = crtc->fb->id;
1272         else
1273                 crtc_resp->fb_id = 0;
1274
1275         crtc_resp->outputs = 0;
1276         if (crtc->enabled) {
1277
1278                 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1279                 crtc_resp->mode_valid = 1;
1280                 ocount = 0;
1281                 list_for_each_entry(output, &dev->mode_config.output_list, head) {
1282                         if (output->crtc == crtc)
1283                                 crtc_resp->outputs |= 1 << (ocount++);
1284                 }
1285                 
1286         } else {
1287                 crtc_resp->mode_valid = 0;
1288         }
1289
1290 out:
1291         mutex_unlock(&dev->mode_config.mutex);
1292         return ret;
1293 }
1294
1295 /**
1296  * drm_mode_getoutput - get output configuration
1297  * @inode: inode from the ioctl
1298  * @filp: file * from the ioctl
1299  * @cmd: cmd from ioctl
1300  * @arg: arg from ioctl
1301  *
1302  * LOCKING:
1303  * Caller? (FIXME)
1304  *
1305  * Construct a output configuration structure to return to the user.
1306  *
1307  * Called by the user via ioctl.
1308  *
1309  * RETURNS:
1310  * Zero on success, errno on failure.
1311  */
1312 int drm_mode_getoutput(struct drm_device *dev,
1313                        void *data, struct drm_file *file_priv)
1314 {
1315         struct drm_mode_get_output *out_resp = data;
1316         struct drm_output *output;
1317         struct drm_display_mode *mode;
1318         int mode_count = 0;
1319         int props_count = 0;
1320         int ret = 0;
1321         int copied = 0;
1322         int i;
1323         struct drm_mode_modeinfo u_mode;
1324         struct drm_mode_modeinfo __user *mode_ptr;
1325         uint32_t __user *prop_ptr;
1326         uint64_t __user *prop_values;
1327
1328         memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1329
1330         DRM_DEBUG("output id %d:\n", out_resp->output);
1331
1332         mutex_lock(&dev->mode_config.mutex);
1333         output= idr_find(&dev->mode_config.crtc_idr, out_resp->output);
1334         if (!output || (output->id != out_resp->output)) {
1335                 ret = -EINVAL;
1336                 goto out;
1337         }
1338
1339         list_for_each_entry(mode, &output->modes, head)
1340                 mode_count++;
1341         
1342         for (i = 0; i < DRM_OUTPUT_MAX_PROPERTY; i++) {
1343                 if (output->property_ids[i] != 0) {
1344                         props_count++;
1345                 }
1346         }
1347
1348         if (out_resp->count_modes == 0) {
1349                 drm_crtc_probe_single_output_modes(output, dev->mode_config.max_width, dev->mode_config.max_height);
1350         }
1351
1352         strncpy(out_resp->name, output->name, DRM_OUTPUT_NAME_LEN);
1353         out_resp->name[DRM_OUTPUT_NAME_LEN-1] = 0;
1354
1355         out_resp->mm_width = output->mm_width;
1356         out_resp->mm_height = output->mm_height;
1357         out_resp->subpixel = output->subpixel_order;
1358         out_resp->connection = output->status;
1359         if (output->crtc)
1360                 out_resp->crtc = output->crtc->id;
1361         else
1362                 out_resp->crtc = 0;
1363
1364         out_resp->crtcs = output->possible_crtcs;
1365         out_resp->clones = output->possible_clones;
1366
1367         if ((out_resp->count_modes >= mode_count) && mode_count) {
1368                 copied = 0;
1369                 mode_ptr = (struct drm_mode_modeinfo *)(unsigned long)out_resp->modes_ptr;
1370                 list_for_each_entry(mode, &output->modes, head) {
1371                         drm_crtc_convert_to_umode(&u_mode, mode);
1372                         if (copy_to_user(mode_ptr + copied,
1373                                          &u_mode, sizeof(u_mode))) {
1374                                 ret = -EFAULT;
1375                                 goto out;
1376                         }
1377                         copied++;
1378                         
1379                 }
1380         }
1381         out_resp->count_modes = mode_count;
1382
1383         if ((out_resp->count_props >= props_count) && props_count) {
1384                 copied = 0;
1385                 prop_ptr = (uint32_t *)(unsigned long)(out_resp->props_ptr);
1386                 prop_values = (uint64_t *)(unsigned long)(out_resp->prop_values_ptr);
1387                 for (i = 0; i < DRM_OUTPUT_MAX_PROPERTY; i++) {
1388                         if (output->property_ids[i] != 0) {
1389                                 if (put_user(output->property_ids[i], prop_ptr + copied)) {
1390                                         ret = -EFAULT;
1391                                         goto out;
1392                                 }
1393
1394                                 if (put_user(output->property_values[i], prop_values + copied)) {
1395                                         ret = -EFAULT;
1396                                         goto out;
1397                                 }
1398                                 copied++;
1399                         }
1400                 }
1401         }
1402         out_resp->count_props = props_count;
1403
1404 out:
1405         mutex_unlock(&dev->mode_config.mutex);
1406         return ret;
1407 }
1408
1409 /**
1410  * drm_mode_setcrtc - set CRTC configuration
1411  * @inode: inode from the ioctl
1412  * @filp: file * from the ioctl
1413  * @cmd: cmd from ioctl
1414  * @arg: arg from ioctl
1415  *
1416  * LOCKING:
1417  * Caller? (FIXME)
1418  *
1419  * Build a new CRTC configuration based on user request.
1420  *
1421  * Called by the user via ioctl.
1422  *
1423  * RETURNS:
1424  * Zero on success, errno on failure.
1425  */
1426 int drm_mode_setcrtc(struct drm_device *dev,
1427                      void *data, struct drm_file *file_priv)
1428 {
1429         struct drm_mode_crtc *crtc_req = data;
1430         struct drm_crtc *crtc;
1431         struct drm_output **output_set = NULL, *output;
1432         struct drm_framebuffer *fb = NULL;
1433         struct drm_display_mode *mode = NULL;
1434         int ret = 0;
1435         int i;
1436         uint32_t __user *set_outputs_ptr;
1437
1438         mutex_lock(&dev->mode_config.mutex);
1439         crtc = idr_find(&dev->mode_config.crtc_idr, crtc_req->crtc_id);
1440         if (!crtc || (crtc->id != crtc_req->crtc_id)) {
1441                 DRM_DEBUG("Unknown CRTC ID %d\n", crtc_req->crtc_id);
1442                 ret = -EINVAL;
1443                 goto out;
1444         }
1445
1446         if (crtc_req->mode_valid) {
1447                 /* if we have a mode we need a framebuffer */
1448                 if (crtc_req->fb_id) {
1449                         fb = idr_find(&dev->mode_config.crtc_idr, crtc_req->fb_id);
1450                         if (!fb || (fb->id != crtc_req->fb_id)) {
1451                                 DRM_DEBUG("Unknown FB ID%d\n", crtc_req->fb_id);
1452                                 ret = -EINVAL;
1453                                 goto out;
1454                         }
1455                 }
1456
1457                 mode = drm_mode_create(dev);
1458                 drm_crtc_convert_umode(mode, &crtc_req->mode);
1459         }
1460
1461         if (crtc_req->count_outputs == 0 && mode) {
1462                 DRM_DEBUG("Count outputs is 0 but mode set\n");
1463                 ret = -EINVAL;
1464                 goto out;
1465         }
1466
1467         if (crtc_req->count_outputs > 0 && !mode && !fb) {
1468                 DRM_DEBUG("Count outputs is %d but no mode or fb set\n", crtc_req->count_outputs);
1469                 ret = -EINVAL;
1470                 goto out;
1471         }
1472
1473         if (crtc_req->count_outputs > 0) {
1474                 u32 out_id;
1475                 output_set = kmalloc(crtc_req->count_outputs *
1476                                      sizeof(struct drm_output *), GFP_KERNEL);
1477                 if (!output_set) {
1478                         ret = -ENOMEM;
1479                         goto out;
1480                 }
1481
1482                 for (i = 0; i < crtc_req->count_outputs; i++) {
1483                         set_outputs_ptr = (uint32_t *)(unsigned long)crtc_req->set_outputs_ptr;
1484                         if (get_user(out_id, &set_outputs_ptr[i])) {
1485                                 ret = -EFAULT;
1486                                 goto out;
1487                         }
1488
1489                         output = idr_find(&dev->mode_config.crtc_idr, out_id);
1490                         if (!output || (out_id != output->id)) {
1491                                 DRM_DEBUG("Output id %d unknown\n", out_id);
1492                                 ret = -EINVAL;
1493                                 goto out;
1494                         }
1495
1496                         output_set[i] = output;
1497                 }
1498         }
1499
1500         ret = drm_crtc_set_config(crtc, crtc_req, mode, output_set, fb);
1501
1502 out:
1503         mutex_unlock(&dev->mode_config.mutex);
1504         return ret;
1505 }
1506
1507 /**
1508  * drm_mode_addfb - add an FB to the graphics configuration
1509  * @inode: inode from the ioctl
1510  * @filp: file * from the ioctl
1511  * @cmd: cmd from ioctl
1512  * @arg: arg from ioctl
1513  *
1514  * LOCKING:
1515  * Takes mode config lock.
1516  *
1517  * Add a new FB to the specified CRTC, given a user request.
1518  *
1519  * Called by the user via ioctl.
1520  *
1521  * RETURNS:
1522  * Zero on success, errno on failure.
1523  */
1524 int drm_mode_addfb(struct drm_device *dev,
1525                    void *data, struct drm_file *file_priv)
1526 {
1527         struct drm_mode_fb_cmd *r = data;
1528         struct drm_mode_config *config = &dev->mode_config;
1529         struct drm_framebuffer *fb;
1530         struct drm_buffer_object *bo;
1531         struct drm_crtc *crtc;
1532         int ret = 0;
1533
1534         if ((config->min_width > r->width) || (r->width > config->max_width)) {
1535                 DRM_ERROR("mode new framebuffer width not within limits\n");
1536                 return -EINVAL;
1537         }
1538         if ((config->min_height > r->height) || (r->height > config->max_height)) {
1539                 DRM_ERROR("mode new framebuffer height not within limits\n");
1540                 return -EINVAL;
1541         }
1542
1543         mutex_lock(&dev->mode_config.mutex);
1544         /* TODO check limits are okay */
1545         ret = drm_get_buffer_object(dev, &bo, r->handle);
1546         if (ret || !bo) {
1547                 ret = -EINVAL;
1548                 goto out;
1549         }
1550
1551         /* TODO check buffer is sufficently large */
1552         /* TODO setup destructor callback */
1553
1554         fb = drm_framebuffer_create(dev);
1555         if (!fb) {
1556                 ret = -EINVAL;
1557                 goto out;
1558         }
1559
1560         fb->width = r->width;
1561         fb->height = r->height;
1562         fb->pitch = r->pitch;
1563         fb->bits_per_pixel = r->bpp;
1564         fb->depth = r->depth;
1565         fb->offset = bo->offset;
1566         fb->bo = bo;
1567
1568         r->buffer_id = fb->id;
1569
1570         list_add(&fb->filp_head, &file_priv->fbs);
1571
1572         /* FIXME: bind the fb to the right crtc */
1573         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1574                 crtc->fb = fb;
1575                 dev->driver->fb_probe(dev, crtc);
1576         }
1577
1578 out:
1579         mutex_unlock(&dev->mode_config.mutex);
1580         return ret;
1581 }
1582
1583 /**
1584  * drm_mode_rmfb - remove an FB from the configuration
1585  * @inode: inode from the ioctl
1586  * @filp: file * from the ioctl
1587  * @cmd: cmd from ioctl
1588  * @arg: arg from ioctl
1589  *
1590  * LOCKING:
1591  * Takes mode config lock.
1592  *
1593  * Remove the FB specified by the user.
1594  *
1595  * Called by the user via ioctl.
1596  *
1597  * RETURNS:
1598  * Zero on success, errno on failure.
1599  */
1600 int drm_mode_rmfb(struct drm_device *dev,
1601                    void *data, struct drm_file *file_priv)
1602 {
1603         struct drm_framebuffer *fb = 0;
1604         uint32_t *id = data;
1605         int ret = 0;
1606
1607         mutex_lock(&dev->mode_config.mutex);
1608         fb = idr_find(&dev->mode_config.crtc_idr, *id);
1609         /* TODO check that we realy get a framebuffer back. */
1610         if (!fb || (*id != fb->id)) {
1611                 DRM_ERROR("mode invalid framebuffer id\n");
1612                 ret = -EINVAL;
1613                 goto out;
1614         }
1615
1616         /* TODO check if we own the buffer */
1617         /* TODO release all crtc connected to the framebuffer */
1618         /* bind the fb to the crtc for now */
1619         /* TODO unhock the destructor from the buffer object */
1620
1621         if (fb->bo->type != drm_bo_type_kernel)
1622                 drm_framebuffer_destroy(fb);
1623         else
1624                 dev->driver->fb_remove(dev, drm_crtc_from_fb(dev, fb));
1625
1626 out:
1627         mutex_unlock(&dev->mode_config.mutex);
1628         return ret;
1629 }
1630
1631 /**
1632  * drm_mode_getfb - get FB info
1633  * @inode: inode from the ioctl
1634  * @filp: file * from the ioctl
1635  * @cmd: cmd from ioctl
1636  * @arg: arg from ioctl
1637  *
1638  * LOCKING:
1639  * Caller? (FIXME)
1640  *
1641  * Lookup the FB given its ID and return info about it.
1642  *
1643  * Called by the user via ioctl.
1644  *
1645  * RETURNS:
1646  * Zero on success, errno on failure.
1647  */
1648 int drm_mode_getfb(struct drm_device *dev,
1649                    void *data, struct drm_file *file_priv)
1650 {
1651         struct drm_mode_fb_cmd *r = data;
1652         struct drm_framebuffer *fb;
1653         int ret = 0;
1654
1655         mutex_lock(&dev->mode_config.mutex);
1656         fb = idr_find(&dev->mode_config.crtc_idr, r->buffer_id);
1657         if (!fb || (r->buffer_id != fb->id)) {
1658                 DRM_ERROR("invalid framebuffer id\n");
1659                 ret = -EINVAL;
1660                 goto out;
1661         }
1662
1663         r->height = fb->height;
1664         r->width = fb->width;
1665         r->depth = fb->depth;
1666         r->bpp = fb->bits_per_pixel;
1667         r->handle = fb->bo->base.hash.key;
1668         r->pitch = fb->pitch;
1669
1670 out:
1671         mutex_unlock(&dev->mode_config.mutex);
1672         return ret;
1673 }
1674
1675 /**
1676  * drm_fb_release - remove and free the FBs on this file
1677  * @filp: file * from the ioctl
1678  *
1679  * LOCKING:
1680  * Takes mode config lock.
1681  *
1682  * Destroy all the FBs associated with @filp.
1683  *
1684  * Called by the user via ioctl.
1685  *
1686  * RETURNS:
1687  * Zero on success, errno on failure.
1688  */
1689 void drm_fb_release(struct file *filp)
1690 {
1691         struct drm_file *priv = filp->private_data;
1692         struct drm_device *dev = priv->head->dev;
1693         struct drm_framebuffer *fb, *tfb;
1694
1695         mutex_lock(&dev->mode_config.mutex);
1696         list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
1697                 list_del(&fb->filp_head);
1698                 if (fb->bo->type != drm_bo_type_kernel)
1699                         drm_framebuffer_destroy(fb);
1700                 else
1701                         dev->driver->fb_remove(dev, drm_crtc_from_fb(dev, fb));
1702         }
1703         mutex_unlock(&dev->mode_config.mutex);
1704 }
1705
1706 /*
1707  *
1708  */
1709
1710 static int drm_mode_attachmode(struct drm_device *dev,
1711                                struct drm_output *output,
1712                                struct drm_display_mode *mode)
1713 {
1714         int ret = 0;
1715
1716         list_add_tail(&mode->head, &output->user_modes);
1717         return ret;
1718 }
1719
1720 int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
1721                              struct drm_display_mode *mode)
1722 {
1723         struct drm_output *output;
1724         int ret = 0;
1725         struct drm_display_mode *dup_mode;
1726         int need_dup = 0;
1727         list_for_each_entry(output, &dev->mode_config.output_list, head) {
1728                 if (output->crtc == crtc) {
1729                         if (need_dup)
1730                                 dup_mode = drm_mode_duplicate(dev, mode);
1731                         else
1732                                 dup_mode = mode;
1733                         ret = drm_mode_attachmode(dev, output, dup_mode); 
1734                         if (ret)
1735                                 return ret;
1736                         need_dup = 1;
1737                 }
1738         }
1739         return 0;
1740 }
1741 EXPORT_SYMBOL(drm_mode_attachmode_crtc);
1742
1743 static int drm_mode_detachmode(struct drm_device *dev,
1744                                struct drm_output *output,
1745                                struct drm_display_mode *mode)
1746 {
1747         int found = 0;
1748         int ret = 0;
1749         struct drm_display_mode *match_mode, *t;
1750
1751         list_for_each_entry_safe(match_mode, t, &output->user_modes, head) {
1752                 if (drm_mode_equal(match_mode, mode)) {
1753                         list_del(&match_mode->head);
1754                         drm_mode_destroy(dev, match_mode);
1755                         found = 1;
1756                         break;
1757                 }
1758         }
1759
1760         if (!found)
1761                 ret = -EINVAL;
1762
1763         return ret;
1764 }
1765
1766 int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
1767 {
1768         struct drm_output *output;
1769
1770         list_for_each_entry(output, &dev->mode_config.output_list, head) {
1771                 drm_mode_detachmode(dev, output, mode);
1772         }
1773         return 0;
1774 }
1775 EXPORT_SYMBOL(drm_mode_detachmode_crtc);
1776
1777 /**
1778  * drm_fb_attachmode - Attach a user mode to an output
1779  * @inode: inode from the ioctl
1780  * @filp: file * from the ioctl
1781  * @cmd: cmd from ioctl
1782  * @arg: arg from ioctl
1783  *
1784  * This attaches a user specified mode to an output.
1785  * Called by the user via ioctl.
1786  *
1787  * RETURNS:
1788  * Zero on success, errno on failure.
1789  */
1790 int drm_mode_attachmode_ioctl(struct drm_device *dev,
1791                               void *data, struct drm_file *file_priv)
1792 {
1793         struct drm_mode_mode_cmd *mode_cmd = data;
1794         struct drm_output *output;
1795         struct drm_display_mode *mode;
1796         struct drm_mode_modeinfo *umode = &mode_cmd->mode;
1797         int ret = 0;
1798
1799         mutex_lock(&dev->mode_config.mutex);
1800
1801         output = idr_find(&dev->mode_config.crtc_idr, mode_cmd->output_id);
1802         if (!output || (output->id != mode_cmd->output_id)) {
1803                 ret = -EINVAL;
1804                 goto out;
1805         }
1806
1807         mode = drm_mode_create(dev);
1808         if (!mode) {
1809                 ret = -ENOMEM;
1810                 goto out;
1811         }
1812         
1813         drm_crtc_convert_umode(mode, umode);
1814
1815         ret = drm_mode_attachmode(dev, output, mode);
1816 out:
1817         mutex_unlock(&dev->mode_config.mutex);
1818         return ret;
1819 }
1820
1821
1822 /**
1823  * drm_fb_detachmode - Detach a user specified mode from an output
1824  * @inode: inode from the ioctl
1825  * @filp: file * from the ioctl
1826  * @cmd: cmd from ioctl
1827  * @arg: arg from ioctl
1828  *
1829  * Called by the user via ioctl.
1830  *
1831  * RETURNS:
1832  * Zero on success, errno on failure.
1833  */
1834 int drm_mode_detachmode_ioctl(struct drm_device *dev,
1835                               void *data, struct drm_file *file_priv)
1836 {
1837         struct drm_mode_mode_cmd *mode_cmd = data;
1838         struct drm_output *output;
1839         struct drm_display_mode mode;
1840         struct drm_mode_modeinfo *umode = &mode_cmd->mode;
1841         int ret = 0;
1842
1843         mutex_lock(&dev->mode_config.mutex);
1844
1845         output = idr_find(&dev->mode_config.crtc_idr, mode_cmd->output_id);
1846         if (!output || (output->id != mode_cmd->output_id)) {
1847                 ret = -EINVAL;
1848                 goto out;
1849         }
1850         
1851         drm_crtc_convert_umode(&mode, umode);
1852         ret = drm_mode_detachmode(dev, output, &mode);
1853 out:           
1854         mutex_unlock(&dev->mode_config.mutex);
1855         return ret;
1856 }
1857
1858 struct drm_property *drm_property_create(struct drm_device *dev, int flags,
1859                                          const char *name, int num_values)
1860 {
1861         struct drm_property *property = NULL;
1862
1863         property = kzalloc(sizeof(struct drm_output), GFP_KERNEL);
1864         if (!property)
1865                 return NULL;
1866
1867         if (num_values) {
1868                 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
1869                 if (!property->values)
1870                         goto fail;
1871         }
1872
1873         property->id = drm_idr_get(dev, property);
1874         property->flags = flags;
1875         property->num_values = num_values;
1876         INIT_LIST_HEAD(&property->enum_blob_list);
1877
1878         if (name)
1879                 strncpy(property->name, name, DRM_PROP_NAME_LEN);
1880
1881         list_add_tail(&property->head, &dev->mode_config.property_list);
1882         return property;
1883 fail:
1884         kfree(property);
1885         return NULL;
1886 }
1887 EXPORT_SYMBOL(drm_property_create);
1888
1889 int drm_property_add_enum(struct drm_property *property, int index,
1890                           uint64_t value, const char *name)
1891 {
1892         struct drm_property_enum *prop_enum;
1893
1894         if (!(property->flags & DRM_MODE_PROP_ENUM))
1895                 return -EINVAL;
1896
1897         if (!list_empty(&property->enum_blob_list)) {
1898                 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
1899                         if (prop_enum->value == value) {
1900                                 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN); 
1901                                 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
1902                                 return 0;
1903                         }
1904                 }
1905         }
1906
1907         prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
1908         if (!prop_enum)
1909                 return -ENOMEM;
1910
1911         strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN); 
1912         prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
1913         prop_enum->value = value;
1914
1915         property->values[index] = value;
1916         list_add_tail(&prop_enum->head, &property->enum_blob_list);
1917         return 0;
1918 }
1919 EXPORT_SYMBOL(drm_property_add_enum);
1920
1921 void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
1922 {
1923         struct drm_property_enum *prop_enum, *pt;
1924
1925         list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
1926                 list_del(&prop_enum->head);
1927                 kfree(prop_enum);
1928         }
1929
1930         if (property->num_values)
1931                 kfree(property->values);
1932         drm_idr_put(dev, property->id);
1933         list_del(&property->head);
1934         kfree(property);        
1935 }
1936 EXPORT_SYMBOL(drm_property_destroy);
1937
1938
1939 int drm_output_attach_property(struct drm_output *output,
1940                                struct drm_property *property, uint64_t init_val)
1941 {
1942         int i;
1943
1944         for (i = 0; i < DRM_OUTPUT_MAX_PROPERTY; i++) {
1945                 if (output->property_ids[i] == 0) {
1946                         output->property_ids[i] = property->id;
1947                         output->property_values[i] = init_val;
1948                         break;
1949                 }
1950         }
1951
1952         if (i == DRM_OUTPUT_MAX_PROPERTY)
1953                 return -EINVAL;
1954         return 0;
1955 }
1956 EXPORT_SYMBOL(drm_output_attach_property);
1957
1958 int drm_mode_getproperty_ioctl(struct drm_device *dev,
1959                                void *data, struct drm_file *file_priv)
1960 {
1961         struct drm_mode_get_property *out_resp = data;
1962         struct drm_property *property;
1963         int enum_count = 0;
1964         int blob_count = 0;
1965         int value_count = 0;
1966         int ret = 0, i;
1967         int copied;
1968         struct drm_property_enum *prop_enum;
1969         struct drm_property_enum __user *enum_ptr;
1970         struct drm_property_blob *prop_blob;
1971         uint32_t *blob_id_ptr;
1972         uint64_t __user *values_ptr;
1973         uint32_t __user *blob_length_ptr;
1974
1975         mutex_lock(&dev->mode_config.mutex);
1976         property = idr_find(&dev->mode_config.crtc_idr, out_resp->prop_id);
1977         if (!property || (property->id != out_resp->prop_id)) {
1978                 ret = -EINVAL;
1979                 goto done;
1980         }
1981
1982         if (property->flags & DRM_MODE_PROP_ENUM) {
1983                 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
1984                         enum_count++;
1985         } else if (property->flags & DRM_MODE_PROP_BLOB) {
1986                 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
1987                         blob_count++;
1988         }
1989
1990         value_count = property->num_values;
1991
1992         strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
1993         out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
1994         out_resp->flags = property->flags;
1995
1996         if ((out_resp->count_values >= value_count) && value_count) {
1997                 values_ptr = (uint64_t *)(unsigned long)out_resp->values_ptr;
1998                 for (i = 0; i < value_count; i++) {
1999                         if (put_user(property->values[i], values_ptr + i)) {
2000                                 ret = -EFAULT;
2001                                 goto done;
2002                         }
2003                 }
2004         }
2005         out_resp->count_values = value_count;
2006
2007         if (property->flags & DRM_MODE_PROP_ENUM) {
2008                 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
2009                         copied = 0;
2010                         enum_ptr = (struct drm_property_enum *)(unsigned long)out_resp->enum_blob_ptr;
2011                         list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2012                                 if (put_user(prop_enum->value, &enum_ptr[copied].value)) {
2013                                         ret = -EFAULT;
2014                                         goto done;
2015                                 }
2016                                 
2017                                 if (copy_to_user(&enum_ptr[copied].name,
2018                                                  prop_enum->name, DRM_PROP_NAME_LEN)) {
2019                                 ret = -EFAULT;
2020                                 goto done;
2021                                 }
2022                                 copied++;
2023                         }
2024                 }
2025                 out_resp->count_enum_blobs = enum_count;
2026         }
2027
2028         if (property->flags & DRM_MODE_PROP_BLOB) {
2029                 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
2030                         copied = 0;
2031                         blob_id_ptr = (uint32_t *)(unsigned long)out_resp->enum_blob_ptr;
2032                         blob_length_ptr = (uint32_t *)(unsigned long)out_resp->values_ptr;
2033                         
2034                         list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
2035                                 if (put_user(prop_blob->id, blob_id_ptr + copied)) {
2036                                         ret = -EFAULT;
2037                                         goto done;
2038                                 }
2039                                 
2040                                 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
2041                                         ret = -EFAULT;
2042                                         goto done;
2043                                 }
2044                                 
2045                                 copied++;
2046                         }
2047                 }
2048                 out_resp->count_enum_blobs = enum_count;
2049         }
2050 done:
2051         mutex_unlock(&dev->mode_config.mutex);
2052         return ret;
2053 }
2054
2055 static int drm_property_create_blob(struct drm_device *dev, int length,
2056                                     void *data)
2057 {
2058         struct drm_property_blob *blob;
2059
2060         if (!length || !data)
2061                 return -EINVAL;
2062
2063         blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
2064         if (!blob)
2065                 return -EINVAL;
2066
2067         blob->data = (void *)((char *)blob + sizeof(struct drm_property_blob));
2068         blob->length = length;
2069
2070         memcpy(blob->data, data, length);
2071
2072         blob->id = drm_idr_get(dev, blob);
2073         
2074         list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
2075         return blob->id;
2076 }
2077
2078 static void drm_property_destroy_blob(struct drm_device *dev,
2079                                struct drm_property_blob *blob)
2080 {
2081         drm_idr_put(dev, blob->id);
2082         list_del(&blob->head);
2083         kfree(blob);
2084 }