NV50: These are actually errors.
[platform/upstream/libdrm.git] / linux-core / nv50_kms_wrapper.c
1 /*
2  * Copyright (C) 2008 Maarten Maathuis.
3  * All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice (including the
14  * next paragraph) shall be included in all copies or substantial
15  * portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20  * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
21  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  *
25  */
26
27 #include "nv50_kms_wrapper.h"
28 #include "drm_crtc_helper.h" /* be careful what you use from this */
29
30 /* This file serves as the interface between the common kernel modesetting code and the device dependent implementation. */
31
32 /*
33  * Get private functions.
34  */
35
36 struct nv50_kms_priv *nv50_get_kms_priv(struct drm_device *dev)
37 {
38         struct drm_nouveau_private *dev_priv = dev->dev_private;
39         return dev_priv->kms_priv;
40 }
41
42 /*
43  * Allocation functions.
44  */
45
46 static void *nv50_kms_alloc_crtc(struct drm_device *dev)
47 {
48         struct nv50_kms_priv *kms_priv = nv50_get_kms_priv(dev);
49         struct nv50_kms_crtc *crtc = kzalloc(sizeof(struct nv50_kms_crtc), GFP_KERNEL);
50
51         if (!crtc)
52                 return NULL;
53
54         list_add_tail(&crtc->item, &kms_priv->crtcs);
55
56         return &(crtc->priv);
57 }
58
59 static void *nv50_kms_alloc_output(struct drm_device *dev)
60 {
61         struct nv50_kms_priv *kms_priv = nv50_get_kms_priv(dev);
62         struct nv50_kms_encoder *encoder = kzalloc(sizeof(struct nv50_kms_encoder), GFP_KERNEL);
63
64         if (!encoder)
65                 return NULL;
66
67         list_add_tail(&encoder->item, &kms_priv->encoders);
68
69         return &(encoder->priv);
70 }
71
72 static void *nv50_kms_alloc_connector(struct drm_device *dev)
73 {
74         struct nv50_kms_priv *kms_priv = nv50_get_kms_priv(dev);
75         struct nv50_kms_connector *connector = kzalloc(sizeof(struct nv50_kms_connector), GFP_KERNEL);
76
77         if (!connector)
78                 return NULL;
79
80         list_add_tail(&connector->item, &kms_priv->connectors);
81
82         return &(connector->priv);
83 }
84
85 static void nv50_kms_free_crtc(void *crtc)
86 {
87         struct nv50_kms_crtc *kms_crtc = from_nv50_crtc(crtc);
88
89         list_del(&kms_crtc->item);
90
91         kfree(kms_crtc);
92 }
93
94 static void nv50_kms_free_output(void *output)
95 {
96         struct nv50_kms_encoder *kms_encoder = from_nv50_output(output);
97
98         list_del(&kms_encoder->item);
99
100         kfree(kms_encoder);
101 }
102
103 static void nv50_kms_free_connector(void *connector)
104 {
105         struct nv50_kms_connector *kms_connector = from_nv50_connector(connector);
106
107         list_del(&kms_connector->item);
108
109         kfree(kms_connector);
110 }
111
112 /*
113  * Mode conversion functions.
114  */
115
116 static struct nouveau_hw_mode *nv50_kms_to_hw_mode(struct drm_display_mode *mode)
117 {
118         struct nouveau_hw_mode *hw_mode = kzalloc(sizeof(struct nouveau_hw_mode), GFP_KERNEL);
119         if (!hw_mode)
120                 return NULL;
121
122         /* create hw values. */
123         hw_mode->clock = mode->clock;
124         hw_mode->flags = hw_mode->flags;
125
126         hw_mode->hdisplay = mode->hdisplay;
127         hw_mode->hsync_start = mode->hsync_start;
128         hw_mode->hsync_end = mode->hsync_end;
129         hw_mode->htotal = mode->htotal;
130
131         hw_mode->hblank_start = mode->hdisplay + 1;
132         hw_mode->hblank_end = mode->htotal;
133
134         hw_mode->vdisplay = mode->vdisplay;
135         hw_mode->vsync_start = mode->vsync_start;
136         hw_mode->vsync_end = mode->vsync_end;
137         hw_mode->vtotal = mode->vtotal;
138
139         hw_mode->vblank_start = mode->vdisplay + 1;
140         hw_mode->vblank_end = mode->vtotal;
141
142         return hw_mode;
143 }
144
145 /*
146  * State mirroring functions.
147  */
148
149 static void nv50_kms_mirror_routing(struct drm_device *dev)
150 {
151         struct nv50_display *display = nv50_get_display(dev);
152         struct nv50_crtc *crtc = NULL;
153         struct nv50_output *output = NULL;
154         struct nv50_connector *connector = NULL;
155         struct drm_connector *drm_connector = NULL;
156
157         /* Wipe all previous connections. */
158         list_for_each_entry(connector, &display->connectors, head) {
159                 connector->output = NULL;
160         }
161
162         list_for_each_entry(output, &display->outputs, head) {
163                 output->crtc = NULL;
164         }
165
166         list_for_each_entry(drm_connector, &dev->mode_config.connector_list, head) {
167                 if (drm_connector->encoder) {
168                         output = to_nv50_output(drm_connector->encoder);
169                         connector = to_nv50_connector(drm_connector);
170
171                         /* hook up output to connector. */
172                         connector->output = output;
173
174                         if (drm_connector->encoder->crtc) {
175                                 crtc = to_nv50_crtc(drm_connector->encoder->crtc);
176
177                                 /* hook up output to crtc. */
178                                 output->crtc = crtc;
179                         }
180                 }
181         }
182 }
183
184 /*
185  * FB functions.
186  */
187
188 static void nv50_kms_framebuffer_destroy(struct drm_framebuffer *drm_framebuffer)
189 {
190         drm_framebuffer_cleanup(drm_framebuffer);
191
192         kfree(drm_framebuffer);
193 }
194
195 static const struct drm_framebuffer_funcs nv50_kms_fb_funcs = {
196         .destroy = nv50_kms_framebuffer_destroy,
197 };
198
199 /*
200  * Mode config functions.
201  */
202
203 static struct drm_framebuffer *nv50_kms_framebuffer_create(struct drm_device *dev,
204                                                 struct drm_file *file_priv, struct drm_mode_fb_cmd *mode_cmd)
205 {
206         struct drm_framebuffer *drm_framebuffer = kzalloc(sizeof(struct drm_framebuffer), GFP_KERNEL);
207         if (!drm_framebuffer)
208                 return NULL;
209
210         drm_framebuffer_init(dev, drm_framebuffer, &nv50_kms_fb_funcs);
211         drm_helper_mode_fill_fb_struct(drm_framebuffer, mode_cmd);
212
213         return drm_framebuffer;
214 }
215
216 static int nv50_kms_fb_changed(struct drm_device *dev)
217 {
218         return 0; /* not needed until nouveaufb? */
219 }
220
221 static const struct drm_mode_config_funcs nv50_kms_mode_funcs = {
222         .resize_fb = NULL,
223         .fb_create = nv50_kms_framebuffer_create,
224         .fb_changed = nv50_kms_fb_changed,
225 };
226
227 /*
228  * CRTC functions.
229  */
230
231 static int nv50_kms_crtc_cursor_set(struct drm_crtc *drm_crtc, uint32_t buffer_handle,
232                           uint32_t width, uint32_t height)
233 {
234         struct nv50_crtc *crtc = to_nv50_crtc(drm_crtc);
235         struct nv50_display *display = nv50_get_display(crtc->dev);
236         int rval;
237
238         if (width != 64 || height != 64)
239                 return -EINVAL;
240
241         rval = crtc->cursor->set_bo(crtc, (drm_handle_t) buffer_handle);
242
243         if (rval != 0)
244                 return rval;
245
246         /* in case this triggers any other cursor changes */
247         display->update(display);
248
249         return 0;
250 }
251
252 static int nv50_kms_crtc_cursor_move(struct drm_crtc *drm_crtc, int x, int y)
253 {
254         struct nv50_crtc *crtc = to_nv50_crtc(drm_crtc);
255
256         return crtc->cursor->set_pos(crtc, x, y);
257 }
258
259 void nv50_kms_crtc_gamma_set(struct drm_crtc *drm_crtc, u16 *r, u16 *g, u16 *b,
260                 uint32_t size)
261 {
262         struct nv50_crtc *crtc = to_nv50_crtc(drm_crtc);
263
264         if (size != 256)
265                 return;
266
267         crtc->lut->set(crtc, (uint16_t *)r, (uint16_t *)g, (uint16_t *)b);
268 }
269
270 int nv50_kms_crtc_set_config(struct drm_mode_set *set)
271 {
272         int rval = 0, i;
273         uint32_t crtc_mask = 0;
274         struct drm_device *dev = NULL;
275         struct drm_nouveau_private *dev_priv = NULL;
276         struct nv50_display *display = NULL;
277         struct drm_connector *drm_connector = NULL;
278         struct drm_encoder *drm_encoder = NULL;
279         struct drm_crtc *drm_crtc = NULL;
280
281         struct nv50_crtc *crtc = NULL;
282         struct nv50_output *output = NULL;
283         struct nv50_connector *connector = NULL;
284         struct nouveau_hw_mode *hw_mode = NULL;
285         struct nv50_fb_info fb_info;
286
287         bool blank = false;
288         bool switch_fb = false;
289         bool modeset = false;
290
291         NV50_DEBUG("\n");
292
293         /*
294          * Supported operations:
295          * - Switch mode.
296          * - Switch framebuffer.
297          * - Blank screen.
298          */
299
300         /* Sanity checking */
301         if (!set) {
302                 DRM_ERROR("Sanity check failed\n");
303                 goto out;
304         }
305
306         if (!set->crtc) {
307                 DRM_ERROR("Sanity check failed\n");
308                 goto out;
309         }
310
311         if (set->mode) {
312                 if (set->fb) {
313                         if (!drm_mode_equal(set->mode, &set->crtc->mode))
314                                 modeset = true;
315
316                         if (set->fb != set->crtc->fb)
317                                 switch_fb = true;
318
319                         if (set->x != set->crtc->x || set->y != set->crtc->y)
320                                 switch_fb = true;
321                 }
322         } else {
323                 blank = true;
324         }
325
326         if (!set->connectors && modeset) {
327                 DRM_ERROR("Sanity check failed\n");
328                 goto out;
329         }
330
331         if (!modeset && !switch_fb && !blank) {
332                 DRM_ERROR("There is nothing to do, bad input.\n");
333                 goto out;
334         }
335
336         /* Basic variable setting */
337         dev = set->crtc->dev;
338         dev_priv = dev->dev_private;
339         display = nv50_get_display(dev);
340         crtc = to_nv50_crtc(set->crtc);
341
342         /**
343          * Wiring up the encoders and connectors.
344          */
345
346         if (modeset) {
347                 /* Mode validation */
348                 hw_mode = nv50_kms_to_hw_mode(set->mode);
349
350                 rval = crtc->validate_mode(crtc, hw_mode);
351
352                 if (rval != MODE_OK) {
353                         DRM_ERROR("Mode not ok\n");
354                         goto out;
355                 }
356
357                 for (i = 0; i < set->num_connectors; i++) {
358                         drm_connector = set->connectors[i];
359                         if (!drm_connector) {
360                                 DRM_ERROR("No connector\n");
361                                 goto out;
362                         }
363                         connector = to_nv50_connector(drm_connector);
364
365                         output = connector->to_output(connector, connector->digital);
366                         if (!output) {
367                                 DRM_ERROR("No output\n");
368                                 goto out;
369                         }
370
371                         rval = output->validate_mode(output, hw_mode);
372                         if (rval != MODE_OK) {
373                                 DRM_ERROR("Mode not ok\n");
374                                 goto out;
375                         }
376                 }
377
378                 /* Validation done, move on to cleaning of existing structures. */
379
380                 /* find encoders that use this crtc. */
381                 list_for_each_entry(drm_encoder, &dev->mode_config.encoder_list, head) {
382                         if (drm_encoder->crtc == set->crtc) {
383                                 /* find the connector that goes with it */
384                                 list_for_each_entry(drm_connector, &dev->mode_config.connector_list, head) {
385                                         if (drm_connector->encoder == drm_encoder) {
386                                                 drm_connector->encoder =  NULL;
387                                                 break;
388                                         }
389                                 }
390                                 drm_encoder->crtc = NULL;
391                         }
392                 }
393
394                 /* now find if our desired encoders or connectors are in use already. */
395                 for (i = 0; i < set->num_connectors; i++) {
396                         drm_connector = set->connectors[i];
397                         if (!drm_connector) {
398                                 DRM_ERROR("No connector\n");
399                                 goto out;
400                         }
401
402                         if (!drm_connector->encoder)
403                                 continue;
404
405                         drm_encoder = drm_connector->encoder;
406                         drm_connector->encoder = NULL;
407
408                         if (!drm_encoder->crtc)
409                                 continue;
410
411                         drm_crtc = drm_encoder->crtc;
412                         drm_encoder->crtc = NULL;
413
414                         crtc = to_nv50_crtc(drm_crtc);
415                         crtc->active = false;
416                         drm_crtc->enabled = false;
417                 }
418
419                 /* Time to wire up the public encoder, the private one will be handled later. */
420                 for (i = 0; i < set->num_connectors; i++) {
421                         drm_connector = set->connectors[i];
422                         if (!drm_connector) {
423                                 DRM_ERROR("No connector\n");
424                                 goto out;
425                         }
426
427                         output = connector->to_output(connector, connector->digital);
428                         if (!output) {
429                                 DRM_ERROR("No output\n");
430                                 goto out;
431                         }
432
433                         /* find the encoder public structure that matches out output structure. */
434                         drm_encoder = to_nv50_kms_encoder(output);
435
436                         if (!drm_encoder) {
437                                 DRM_ERROR("No encoder\n");
438                                 goto out;
439                         }
440
441                         drm_encoder->crtc = set->crtc;
442                         drm_connector->encoder = drm_encoder;
443                 }
444         }
445
446         /**
447          * Disable crtc.
448          */
449
450         if (blank) {
451                 crtc = to_nv50_crtc(set->crtc);
452
453                 /* keeping the encoders and connectors attached, so they can be tracked */
454                 crtc->active = false;
455                 set->crtc->enabled = false;
456         }
457
458         /**
459          * All state should now be updated, now onto the real work.
460          */
461
462         /* mirror everything to the private structs */
463         nv50_kms_mirror_routing(dev);
464
465         /**
466          * Bind framebuffer.
467          */
468
469         if (switch_fb) {
470                 /* set framebuffer */
471                 set->crtc->fb = set->fb;
472
473                 /* set private framebuffer */
474                 crtc = to_nv50_crtc(set->crtc);
475                 fb_info.block = find_block_by_handle(dev_priv->fb_heap, set->fb->mm_handle);
476                 fb_info.width = set->fb->width;
477                 fb_info.height = set->fb->height;
478                 fb_info.depth = set->fb->depth;
479                 fb_info.bpp = set->fb->bits_per_pixel;
480                 fb_info.pitch = set->fb->pitch;
481                 fb_info.x = set->x;
482                 fb_info.y = set->y;
483
484                 rval = crtc->fb->bind(crtc, &fb_info);
485                 if (rval != 0) {
486                         DRM_ERROR("fb_bind failed\n");
487                         goto out;
488                 }
489         }
490
491         /* this is !cursor_show */
492         if (!crtc->cursor->enabled) {
493                 rval = crtc->cursor->enable(crtc);
494                 if (rval != 0) {
495                         DRM_ERROR("cursor_enable failed\n");
496                         goto out;
497                 }
498         }
499
500         /**
501          * Blanking.
502          */
503
504         if (blank) {
505                 crtc = to_nv50_crtc(set->crtc);
506
507                 rval = crtc->blank(crtc, TRUE);
508                 if (rval != 0) {
509                         DRM_ERROR("blanking failed\n");
510                         goto out;
511                 }
512
513                 /* detach any outputs that are currently running on this crtc */
514                 list_for_each_entry(drm_encoder, &dev->mode_config.encoder_list, head) {
515                         if (drm_encoder->crtc == set->crtc) {
516                                 output = to_nv50_output(drm_encoder);
517
518                                 rval = output->execute_mode(output, TRUE);
519                                 if (rval != 0) {
520                                         DRM_ERROR("detaching output failed\n");
521                                         goto out;
522                                 }
523                         }
524                 }
525         }
526
527         /**
528          * Change framebuffer, without changing mode.
529          */
530
531         if (switch_fb && !modeset && !blank) {
532                 crtc = to_nv50_crtc(set->crtc);
533
534                 rval = crtc->blank(crtc, TRUE);
535                 if (rval != 0) {
536                         DRM_ERROR("blanking failed\n");
537                         goto out;
538                 }
539
540                 rval = crtc->set_fb(crtc);
541                 if (rval != 0) {
542                         DRM_ERROR("set_fb failed\n");
543                         goto out;
544                 }
545
546                 /* this also sets the fb offset */
547                 rval = crtc->blank(crtc, FALSE);
548                 if (rval != 0) {
549                         DRM_ERROR("unblanking failed\n");
550                         goto out;
551                 }
552         }
553
554         /**
555          * Normal modesetting.
556          */
557
558         if (modeset) {
559                 /* disconnect unused outputs */
560                 list_for_each_entry(output, &display->outputs, head) {
561                         if (output->crtc) {
562                                 crtc_mask |= 1 << output->crtc->index;
563                         } else {
564                                 rval = output->execute_mode(output, TRUE);
565                                 if (rval != 0) {
566                                         DRM_ERROR("detaching output failed\n");
567                                         goto out;
568                                 }
569                         }
570                 }
571
572                 rval = crtc->set_mode(crtc, hw_mode);
573                 if (rval != 0) {
574                         DRM_ERROR("crtc mode set failed\n");
575                         goto out;
576                 }
577
578                 /* find native mode. */
579                 list_for_each_entry(output, &display->outputs, head) {
580                         if (output->crtc != crtc)
581                                 continue;
582
583                         *crtc->native_mode = *output->native_mode;
584                         list_for_each_entry(connector, &display->connectors, head) {
585                                 if (connector->output != output)
586                                         continue;
587
588                                 crtc->scaling_mode = connector->scaling_mode;
589                                 break;
590                         }
591
592                         if (crtc->scaling_mode == SCALE_PANEL)
593                                 crtc->use_native_mode = false;
594                         else
595                                 crtc->use_native_mode = true;
596
597                         break; /* no use in finding more than one mode */
598                 }
599
600                 rval = crtc->execute_mode(crtc);
601                 if (rval != 0) {
602                         DRM_ERROR("crtc execute mode failed\n");
603                         goto out;
604                 }
605
606                 list_for_each_entry(output, &display->outputs, head) {
607                         if (output->crtc != crtc)
608                                 continue;
609
610                         rval = output->execute_mode(output, FALSE);
611                         if (rval != 0) {
612                                 DRM_ERROR("output execute mode failed\n");
613                                 goto out;
614                         }
615                 }
616
617                 rval = crtc->set_scale(crtc);
618                 if (rval != 0) {
619                         DRM_ERROR("crtc set scale failed\n");
620                         goto out;
621                 }
622
623                 /* next line changes crtc, so putting it here is important */
624                 display->last_crtc = crtc->index;
625
626                 /* blank any unused crtcs */
627                 list_for_each_entry(crtc, &display->crtcs, head) {
628                         if (!(crtc_mask & (1 << crtc->index)))
629                                 crtc->blank(crtc, TRUE);
630                 }
631         }
632
633         display->update(display);
634
635         kfree(hw_mode);
636
637         return 0;
638
639 out:
640         if (display)
641                 display->update(display);
642
643         kfree(hw_mode);
644
645         if (rval != 0)
646                 return rval;
647         else
648                 return -EINVAL;
649 }
650
651 static void nv50_kms_crtc_destroy(struct drm_crtc *drm_crtc)
652 {
653         struct nv50_crtc *crtc = to_nv50_crtc(drm_crtc);
654
655         drm_crtc_cleanup(drm_crtc);
656
657         /* this will even destroy the public structure. */
658         crtc->destroy(crtc);
659 }
660
661 static const struct drm_crtc_funcs nv50_kms_crtc_funcs = {
662         .save = NULL,
663         .restore = NULL,
664         .cursor_set = nv50_kms_crtc_cursor_set,
665         .cursor_move = nv50_kms_crtc_cursor_move,
666         .gamma_set = nv50_kms_crtc_gamma_set,
667         .set_config = nv50_kms_crtc_set_config,
668         .destroy = nv50_kms_crtc_destroy,
669 };
670
671 static int nv50_kms_crtcs_init(struct drm_device *dev)
672 {
673         struct nv50_display *display = nv50_get_display(dev);
674         struct nv50_crtc *crtc = NULL;
675
676         /*
677          * This may look a bit confusing, but:
678          * The internal structure is already allocated and so is the public one.
679          * Just a matter of getting to the memory and register it.
680          */
681         list_for_each_entry(crtc, &display->crtcs, head) {
682                 struct drm_crtc *drm_crtc = to_nv50_kms_crtc(crtc);
683
684                 drm_crtc_init(dev, drm_crtc, &nv50_kms_crtc_funcs);
685         }
686
687         return 0;
688 }
689
690 /*
691  * Encoder functions
692  */
693
694 static void nv50_kms_encoder_destroy(struct drm_encoder *drm_encoder)
695 {
696         struct nv50_output *output = to_nv50_output(drm_encoder);
697
698         drm_encoder_cleanup(drm_encoder);
699
700         /* this will even destroy the public structure. */
701         output->destroy(output);
702 }
703
704 static const struct drm_encoder_funcs nv50_kms_encoder_funcs = {
705         .destroy = nv50_kms_encoder_destroy,
706 };
707
708 static int nv50_kms_encoders_init(struct drm_device *dev)
709 {
710         struct nv50_display *display = nv50_get_display(dev);
711         struct nv50_output *output = NULL;
712
713         list_for_each_entry(output, &display->outputs, head) {
714                 struct drm_encoder *drm_encoder = to_nv50_kms_encoder(output);
715                 uint32_t type = DRM_MODE_ENCODER_NONE;
716
717                 switch (output->type) {
718                         case OUTPUT_DAC:
719                                 type = DRM_MODE_ENCODER_DAC;
720                                 break;
721                         case OUTPUT_TMDS:
722                                 type = DRM_MODE_ENCODER_TMDS;
723                                 break;
724                         case OUTPUT_LVDS:
725                                 type = DRM_MODE_ENCODER_LVDS;
726                                 break;
727                         case OUTPUT_TV:
728                                 type = DRM_MODE_ENCODER_TVDAC;
729                                 break;
730                         default:
731                                 type = DRM_MODE_ENCODER_NONE;
732                                 break;
733                 }
734
735                 if (type == DRM_MODE_ENCODER_NONE) {
736                         DRM_ERROR("DRM_MODE_ENCODER_NONE encountered\n");
737                         continue;
738                 }
739
740                 drm_encoder_init(dev, drm_encoder, &nv50_kms_encoder_funcs, type);
741
742                 /* I've never seen possible crtc's restricted. */
743                 drm_encoder->possible_crtcs = 3;
744                 drm_encoder->possible_clones = 0;
745         }
746
747         return 0;
748 }
749
750 /*
751  * Connector functions
752  */
753
754 void nv50_kms_connector_detect_all(struct drm_device *dev)
755 {
756         struct drm_connector *drm_connector = NULL;
757
758         list_for_each_entry(drm_connector, &dev->mode_config.connector_list, head) {
759                 drm_connector->funcs->detect(drm_connector);
760         }
761 }
762
763 static enum drm_connector_status nv50_kms_connector_detect(struct drm_connector *drm_connector)
764 {
765         struct nv50_connector *connector = to_nv50_connector(drm_connector);
766         struct drm_device *dev = drm_connector->dev;
767         bool connected;
768         int old_status;
769
770         connected = connector->detect(connector);
771
772         old_status = drm_connector->status;
773
774         if (connected)
775                 drm_connector->status = connector_status_connected;
776         else
777                 drm_connector->status = connector_status_disconnected;
778
779         /* update our modes whenever there is reason to */
780         if (old_status != drm_connector->status) {
781                 drm_connector->funcs->fill_modes(drm_connector, 0, 0);
782                 /* notify fb of changes */
783                 dev->mode_config.funcs->fb_changed(dev);
784         }
785
786         return drm_connector->status;
787 }
788
789 static void nv50_kms_connector_destroy(struct drm_connector *drm_connector)
790 {
791         struct nv50_connector *connector = to_nv50_connector(drm_connector);
792
793         drm_sysfs_connector_remove(drm_connector);
794         drm_connector_cleanup(drm_connector);
795
796         /* this will even destroy the public structure. */
797         connector->destroy(connector);
798 }
799
800 /*
801  * Detailed mode info for a standard 640x480@60Hz monitor
802  */
803 static struct drm_display_mode std_mode[] = {
804         /*{ DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 25200, 640, 656,
805                  752, 800, 0, 480, 490, 492, 525, 0,
806                  V_NHSYNC | V_NVSYNC) },*/ /* 640x480@60Hz */
807         { DRM_MODE("1280x1024", DRM_MODE_TYPE_DEFAULT, 135000, 1280, 1296,
808                    1440, 1688, 0, 1024, 1025, 1028, 1066, 0,
809                    V_PHSYNC | V_PVSYNC) }, /* 1280x1024@75Hz */
810 };
811
812 static void nv50_kms_connector_fill_modes(struct drm_connector *drm_connector, uint32_t maxX, uint32_t maxY)
813 {
814         struct nv50_connector *connector = to_nv50_connector(drm_connector);
815         int ret = 0;
816         bool connected;
817         struct drm_display_mode *mode, *t;
818         struct edid *edid = NULL;
819
820         NV50_DEBUG("%s\n", drm_get_connector_name(drm_connector));
821         /* set all modes to the unverified state */
822         list_for_each_entry_safe(mode, t, &drm_connector->modes, head)
823                 mode->status = MODE_UNVERIFIED;
824
825         connected = connector->detect(connector);
826
827         if (connected)
828                 drm_connector->status = connector_status_connected;
829         else
830                 drm_connector->status = connector_status_disconnected;
831
832         if (!connected) {
833                 NV50_DEBUG("%s is disconnected\n", drm_get_connector_name(drm_connector));
834                 /* TODO set EDID to NULL */
835                 return;
836         }
837
838         /* Not all connnectors have an i2c channel. */
839         if (connector->i2c_chan)
840                 edid = (struct edid *) drm_do_probe_ddc_edid(&connector->i2c_chan->adapter);
841
842         if (edid) {
843                 drm_mode_connector_update_edid_property(drm_connector, edid);
844                 ret = drm_add_edid_modes(drm_connector, edid);
845                 connector->digital = edid->digital; /* cache */
846         }
847
848         if (ret) /* number of modes  > 1 */
849                 drm_mode_connector_list_update(drm_connector);
850
851         if (maxX && maxY)
852                 drm_mode_validate_size(drm_connector->dev, &drm_connector->modes, maxX, maxY, 0);
853
854         list_for_each_entry_safe(mode, t, &drm_connector->modes, head) {
855                 if (mode->status == MODE_OK) {
856                         struct nouveau_hw_mode *hw_mode = nv50_kms_to_hw_mode(mode);
857                         struct nv50_output *output = connector->to_output(connector, connector->digital);
858
859                         mode->status = output->validate_mode(output, hw_mode);
860                         /* find native mode, TODO: also check if we actually found one */
861                         if (mode->status == MODE_OK) {
862                                 if (mode->type & DRM_MODE_TYPE_PREFERRED)
863                                         *output->native_mode = *hw_mode;
864                         }
865                         kfree(hw_mode);
866                 }
867         }
868
869         /* revalidate now that we have native mode */
870         list_for_each_entry_safe(mode, t, &drm_connector->modes, head) {
871                 if (mode->status == MODE_OK) {
872                         struct nouveau_hw_mode *hw_mode = nv50_kms_to_hw_mode(mode);
873                         struct nv50_output *output = connector->to_output(connector, connector->digital);
874
875                         mode->status = output->validate_mode(output, hw_mode);
876                         kfree(hw_mode);
877                 }
878         }
879
880         drm_mode_prune_invalid(drm_connector->dev, &drm_connector->modes, TRUE);
881
882         if (list_empty(&drm_connector->modes)) {
883                 struct drm_display_mode *stdmode;
884                 struct nouveau_hw_mode *hw_mode;
885                 struct nv50_output *output;
886
887                 NV50_DEBUG("No valid modes on %s\n", drm_get_connector_name(drm_connector));
888
889                 /* Should we do this here ???
890                  * When no valid EDID modes are available we end up
891                  * here and bailed in the past, now we add a standard
892                  * 640x480@60Hz mode and carry on.
893                  */
894                 stdmode = drm_mode_duplicate(drm_connector->dev, &std_mode[0]);
895                 drm_mode_probed_add(drm_connector, stdmode);
896                 drm_mode_list_concat(&drm_connector->probed_modes,
897                                      &drm_connector->modes);
898
899                 /* also add it as native mode */
900                 hw_mode = nv50_kms_to_hw_mode(mode);
901                 output = connector->to_output(connector, connector->digital);
902
903                 if (hw_mode)
904                         *output->native_mode = *hw_mode;
905
906                 DRM_DEBUG("Adding standard 640x480 @ 60Hz to %s\n",
907                           drm_get_connector_name(drm_connector));
908         }
909
910         drm_mode_sort(&drm_connector->modes);
911
912         NV50_DEBUG("Probed modes for %s\n", drm_get_connector_name(drm_connector));
913
914         list_for_each_entry_safe(mode, t, &drm_connector->modes, head) {
915                 mode->vrefresh = drm_mode_vrefresh(mode);
916
917                 /* is this needed, as it's unused by the driver? */
918                 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
919                 drm_mode_debug_printmodeline(mode);
920         }
921 }
922
923 static const struct drm_connector_funcs nv50_kms_connector_funcs = {
924         .save = NULL,
925         .restore = NULL,
926         .detect = nv50_kms_connector_detect,
927         .destroy = nv50_kms_connector_destroy,
928         .fill_modes = nv50_kms_connector_fill_modes,
929 };
930
931 static int nv50_kms_connectors_init(struct drm_device *dev)
932 {
933         struct nv50_display *display = nv50_get_display(dev);
934         struct nv50_connector *connector = NULL;
935         int i;
936
937         list_for_each_entry(connector, &display->connectors, head) {
938                 struct drm_connector *drm_connector = to_nv50_kms_connector(connector);
939                 uint32_t type = DRM_MODE_CONNECTOR_Unknown;
940
941                 switch (connector->type) {
942                         case CONNECTOR_VGA:
943                                 type = DRM_MODE_CONNECTOR_VGA;
944                                 break;
945                         case CONNECTOR_DVI_D:
946                                 type = DRM_MODE_CONNECTOR_DVID;
947                                 break;
948                         case CONNECTOR_DVI_I:
949                                 type = DRM_MODE_CONNECTOR_DVII;
950                                 break;
951                         case CONNECTOR_LVDS:
952                                 type = DRM_MODE_CONNECTOR_LVDS;
953                                 break;
954                         case CONNECTOR_TV:
955                                 type = DRM_MODE_CONNECTOR_SVIDEO;
956                                 break;
957                         default:
958                                 type = DRM_MODE_CONNECTOR_Unknown;
959                                 break;
960                 }
961
962                 if (type == DRM_MODE_CONNECTOR_Unknown) {
963                         DRM_ERROR("DRM_MODE_CONNECTOR_Unknown encountered\n");
964                         continue;
965                 }
966
967                 /* It should be allowed sometimes, but let's be safe for the moment. */
968                 drm_connector->interlace_allowed = false;
969                 drm_connector->doublescan_allowed = false;
970
971                 drm_connector_init(dev, drm_connector, &nv50_kms_connector_funcs, type);
972
973                 /* attach encoders, possibilities are analog + digital */
974                 for (i = 0; i < 2; i++) {
975                         struct drm_encoder *drm_encoder = NULL;
976                         struct nv50_output *output = connector->to_output(connector, i);
977                         if (!output)
978                                 continue;
979
980                         drm_encoder = to_nv50_kms_encoder(output);
981                         if (!drm_encoder) {
982                                 DRM_ERROR("No struct drm_connector to match struct nv50_output\n");
983                                 continue;
984                         }
985
986                         drm_mode_connector_attach_encoder(drm_connector, drm_encoder);
987                 }
988
989                 drm_sysfs_connector_add(drm_connector);
990         }
991
992         return 0;
993 }
994
995 /*
996  * Main functions
997  */
998
999 int nv50_kms_init(struct drm_device *dev)
1000 {
1001         struct drm_nouveau_private *dev_priv = dev->dev_private;
1002         struct nv50_kms_priv *kms_priv = kzalloc(sizeof(struct nv50_kms_priv), GFP_KERNEL);
1003         struct nv50_display *display = NULL;
1004         int rval = 0;
1005
1006         if (!kms_priv)
1007                 return -ENOMEM;
1008
1009         dev_priv->kms_priv = kms_priv;
1010
1011         /* function pointers */
1012         /* an allocation interface that deals with the outside world, without polluting the core. */
1013         dev_priv->alloc_crtc = nv50_kms_alloc_crtc;
1014         dev_priv->alloc_output = nv50_kms_alloc_output;
1015         dev_priv->alloc_connector = nv50_kms_alloc_connector;
1016
1017         dev_priv->free_crtc = nv50_kms_free_crtc;
1018         dev_priv->free_output = nv50_kms_free_output;
1019         dev_priv->free_connector = nv50_kms_free_connector;
1020
1021         /* bios is needed for tables. */
1022         rval = nouveau_parse_bios(dev);
1023         if (rval != 0)
1024                 goto out;
1025
1026         /* init basic kernel modesetting */
1027         drm_mode_config_init(dev);
1028
1029         dev->mode_config.min_width = 0;
1030         dev->mode_config.min_height = 0;
1031
1032         dev->mode_config.funcs = (void *)&nv50_kms_mode_funcs;
1033
1034         dev->mode_config.max_width = 8192;
1035         dev->mode_config.max_height = 8192;
1036
1037         dev->mode_config.fb_base = dev_priv->fb_phys;
1038
1039         /* init kms lists */
1040         INIT_LIST_HEAD(&kms_priv->crtcs);
1041         INIT_LIST_HEAD(&kms_priv->encoders);
1042         INIT_LIST_HEAD(&kms_priv->connectors);
1043
1044         /* init the internal core, must be done first. */
1045         rval = nv50_display_create(dev);
1046         if (rval != 0)
1047                 goto out;
1048
1049         display = nv50_get_display(dev);
1050         if (!display) {
1051                 rval = -EINVAL;
1052                 goto out;
1053         }
1054
1055         /* pre-init now */
1056         rval = display->pre_init(display);
1057         if (rval != 0)
1058                 goto out;
1059
1060         /* init external layer */
1061         rval = nv50_kms_crtcs_init(dev);
1062         if (rval != 0)
1063                 goto out;
1064
1065         rval = nv50_kms_encoders_init(dev);
1066         if (rval != 0)
1067                 goto out;
1068
1069         rval = nv50_kms_connectors_init(dev);
1070         if (rval != 0)
1071                 goto out;
1072
1073         /* init now, this'll kill the textmode */
1074         rval = display->init(display);
1075         if (rval != 0)
1076                 goto out;
1077
1078         /* process cmdbuffer */
1079         display->update(display);
1080
1081         return 0;
1082
1083 out:
1084         kfree(kms_priv);
1085         dev_priv->kms_priv = NULL;
1086
1087         return rval;
1088 }
1089
1090 int nv50_kms_destroy(struct drm_device *dev)
1091 {
1092         drm_mode_config_cleanup(dev);
1093
1094         return 0;
1095 }
1096