]> bbs.cooldavid.org Git - net-next-2.6.git/blob - drivers/gpu/drm/nouveau/nv04_display.c
c6df391ebb2e58008a5037f2ad3976f3cc9ae192
[net-next-2.6.git] / drivers / gpu / drm / nouveau / nv04_display.c
1 /*
2  * Copyright 2009 Red Hat Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * Author: Ben Skeggs
23  */
24
25 #include "drmP.h"
26 #include "drm.h"
27 #include "drm_crtc_helper.h"
28
29 #include "nouveau_drv.h"
30 #include "nouveau_fb.h"
31 #include "nouveau_hw.h"
32 #include "nouveau_encoder.h"
33 #include "nouveau_connector.h"
34
35 static void
36 nv04_display_store_initial_head_owner(struct drm_device *dev)
37 {
38         struct drm_nouveau_private *dev_priv = dev->dev_private;
39
40         if (dev_priv->chipset != 0x11) {
41                 dev_priv->crtc_owner = NVReadVgaCrtc(dev, 0, NV_CIO_CRE_44);
42                 return;
43         }
44
45         /* reading CR44 is broken on nv11, so we attempt to infer it */
46         if (nvReadMC(dev, NV_PBUS_DEBUG_1) & (1 << 28)) /* heads tied, restore both */
47                 dev_priv->crtc_owner = 0x4;
48         else {
49                 uint8_t slaved_on_A, slaved_on_B;
50                 bool tvA = false;
51                 bool tvB = false;
52
53                 slaved_on_B = NVReadVgaCrtc(dev, 1, NV_CIO_CRE_PIXEL_INDEX) &
54                                                                         0x80;
55                 if (slaved_on_B)
56                         tvB = !(NVReadVgaCrtc(dev, 1, NV_CIO_CRE_LCD__INDEX) &
57                                         MASK(NV_CIO_CRE_LCD_LCD_SELECT));
58
59                 slaved_on_A = NVReadVgaCrtc(dev, 0, NV_CIO_CRE_PIXEL_INDEX) &
60                                                                         0x80;
61                 if (slaved_on_A)
62                         tvA = !(NVReadVgaCrtc(dev, 0, NV_CIO_CRE_LCD__INDEX) &
63                                         MASK(NV_CIO_CRE_LCD_LCD_SELECT));
64
65                 if (slaved_on_A && !tvA)
66                         dev_priv->crtc_owner = 0x0;
67                 else if (slaved_on_B && !tvB)
68                         dev_priv->crtc_owner = 0x3;
69                 else if (slaved_on_A)
70                         dev_priv->crtc_owner = 0x0;
71                 else if (slaved_on_B)
72                         dev_priv->crtc_owner = 0x3;
73                 else
74                         dev_priv->crtc_owner = 0x0;
75         }
76 }
77
78 int
79 nv04_display_create(struct drm_device *dev)
80 {
81         struct drm_nouveau_private *dev_priv = dev->dev_private;
82         struct dcb_table *dcb = &dev_priv->vbios.dcb;
83         struct drm_connector *connector, *ct;
84         struct drm_encoder *encoder;
85         struct drm_crtc *crtc;
86         int i, ret;
87
88         NV_DEBUG_KMS(dev, "\n");
89
90         NVLockVgaCrtcs(dev, false);
91
92         if (nv_two_heads(dev)) {
93                 nv04_display_store_initial_head_owner(dev);
94                 NVSetOwner(dev, 0);
95         }
96
97         nouveau_hw_save_vga_fonts(dev, 1);
98
99         drm_mode_config_init(dev);
100         drm_mode_create_scaling_mode_property(dev);
101         drm_mode_create_dithering_property(dev);
102
103         dev->mode_config.funcs = (void *)&nouveau_mode_config_funcs;
104
105         dev->mode_config.min_width = 0;
106         dev->mode_config.min_height = 0;
107         switch (dev_priv->card_type) {
108         case NV_04:
109                 dev->mode_config.max_width = 2048;
110                 dev->mode_config.max_height = 2048;
111                 break;
112         default:
113                 dev->mode_config.max_width = 4096;
114                 dev->mode_config.max_height = 4096;
115                 break;
116         }
117
118         dev->mode_config.fb_base = dev_priv->fb_phys;
119
120         nv04_crtc_create(dev, 0);
121         if (nv_two_heads(dev))
122                 nv04_crtc_create(dev, 1);
123
124         for (i = 0; i < dcb->entries; i++) {
125                 struct dcb_entry *dcbent = &dcb->entry[i];
126
127                 connector = nouveau_connector_create(dev, dcbent->connector);
128                 if (IS_ERR(connector))
129                         continue;
130
131                 switch (dcbent->type) {
132                 case OUTPUT_ANALOG:
133                         ret = nv04_dac_create(connector, dcbent);
134                         break;
135                 case OUTPUT_LVDS:
136                 case OUTPUT_TMDS:
137                         ret = nv04_dfp_create(connector, dcbent);
138                         break;
139                 case OUTPUT_TV:
140                         if (dcbent->location == DCB_LOC_ON_CHIP)
141                                 ret = nv17_tv_create(connector, dcbent);
142                         else
143                                 ret = nv04_tv_create(connector, dcbent);
144                         break;
145                 default:
146                         NV_WARN(dev, "DCB type %d not known\n", dcbent->type);
147                         continue;
148                 }
149
150                 if (ret)
151                         continue;
152         }
153
154         list_for_each_entry_safe(connector, ct,
155                                  &dev->mode_config.connector_list, head) {
156                 if (!connector->encoder_ids[0]) {
157                         NV_WARN(dev, "%s has no encoders, removing\n",
158                                 drm_get_connector_name(connector));
159                         connector->funcs->destroy(connector);
160                 }
161         }
162
163         /* Save previous state */
164         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
165                 crtc->funcs->save(crtc);
166
167         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
168                 struct drm_encoder_helper_funcs *func = encoder->helper_private;
169
170                 func->save(encoder);
171         }
172
173         return 0;
174 }
175
176 void
177 nv04_display_destroy(struct drm_device *dev)
178 {
179         struct drm_nouveau_private *dev_priv = dev->dev_private;
180         struct drm_encoder *encoder;
181         struct drm_crtc *crtc;
182
183         NV_DEBUG_KMS(dev, "\n");
184
185         /* Turn every CRTC off. */
186         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
187                 struct drm_mode_set modeset = {
188                         .crtc = crtc,
189                 };
190
191                 crtc->funcs->set_config(&modeset);
192         }
193
194         /* Restore state */
195         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
196                 struct drm_encoder_helper_funcs *func = encoder->helper_private;
197
198                 func->restore(encoder);
199         }
200
201         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
202                 crtc->funcs->restore(crtc);
203
204         drm_mode_config_cleanup(dev);
205
206         nouveau_hw_save_vga_fonts(dev, 0);
207
208         if (nv_two_heads(dev))
209                 NVSetOwner(dev, dev_priv->crtc_owner);
210         NVLockVgaCrtcs(dev, true);
211 }
212
213 void
214 nv04_display_restore(struct drm_device *dev)
215 {
216         struct drm_encoder *encoder;
217         struct drm_crtc *crtc;
218
219         NVLockVgaCrtcs(dev, false);
220
221         /* meh.. modeset apparently doesn't setup all the regs and depends
222          * on pre-existing state, for now load the state of the card *before*
223          * nouveau was loaded, and then do a modeset.
224          *
225          * best thing to do probably is to make save/restore routines not
226          * save/restore "pre-load" state, but more general so we can save
227          * on suspend too.
228          */
229         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
230                 struct drm_encoder_helper_funcs *func = encoder->helper_private;
231
232                 func->restore(encoder);
233         }
234
235         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
236                 crtc->funcs->restore(crtc);
237 }
238