cd70bd8276144507aac80142f12219ad746d3f96
[platform/adaptation/renesas_rcar/renesas_kernel.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_early_init(struct drm_device *dev)
80 {
81         /* Unlock the VGA CRTCs. */
82         NVLockVgaCrtcs(dev, false);
83
84         /* Make sure the CRTCs aren't in slaved mode. */
85         if (nv_two_heads(dev)) {
86                 nv04_display_store_initial_head_owner(dev);
87                 NVSetOwner(dev, 0);
88         }
89
90         return 0;
91 }
92
93 void
94 nv04_display_late_takedown(struct drm_device *dev)
95 {
96         struct drm_nouveau_private *dev_priv = dev->dev_private;
97
98         if (nv_two_heads(dev))
99                 NVSetOwner(dev, dev_priv->crtc_owner);
100
101         NVLockVgaCrtcs(dev, true);
102 }
103
104 int
105 nv04_display_create(struct drm_device *dev)
106 {
107         struct drm_nouveau_private *dev_priv = dev->dev_private;
108         struct dcb_table *dcb = &dev_priv->vbios.dcb;
109         struct drm_connector *connector, *ct;
110         struct drm_encoder *encoder;
111         struct drm_crtc *crtc;
112         int i, ret;
113
114         NV_DEBUG_KMS(dev, "\n");
115
116         nouveau_hw_save_vga_fonts(dev, 1);
117
118         drm_mode_config_init(dev);
119         drm_mode_create_scaling_mode_property(dev);
120         drm_mode_create_dithering_property(dev);
121
122         dev->mode_config.funcs = (void *)&nouveau_mode_config_funcs;
123
124         dev->mode_config.min_width = 0;
125         dev->mode_config.min_height = 0;
126         switch (dev_priv->card_type) {
127         case NV_04:
128                 dev->mode_config.max_width = 2048;
129                 dev->mode_config.max_height = 2048;
130                 break;
131         default:
132                 dev->mode_config.max_width = 4096;
133                 dev->mode_config.max_height = 4096;
134                 break;
135         }
136
137         dev->mode_config.fb_base = dev_priv->fb_phys;
138
139         nv04_crtc_create(dev, 0);
140         if (nv_two_heads(dev))
141                 nv04_crtc_create(dev, 1);
142
143         for (i = 0; i < dcb->entries; i++) {
144                 struct dcb_entry *dcbent = &dcb->entry[i];
145
146                 connector = nouveau_connector_create(dev, dcbent->connector);
147                 if (IS_ERR(connector))
148                         continue;
149
150                 switch (dcbent->type) {
151                 case OUTPUT_ANALOG:
152                         ret = nv04_dac_create(connector, dcbent);
153                         break;
154                 case OUTPUT_LVDS:
155                 case OUTPUT_TMDS:
156                         ret = nv04_dfp_create(connector, dcbent);
157                         break;
158                 case OUTPUT_TV:
159                         if (dcbent->location == DCB_LOC_ON_CHIP)
160                                 ret = nv17_tv_create(connector, dcbent);
161                         else
162                                 ret = nv04_tv_create(connector, dcbent);
163                         break;
164                 default:
165                         NV_WARN(dev, "DCB type %d not known\n", dcbent->type);
166                         continue;
167                 }
168
169                 if (ret)
170                         continue;
171         }
172
173         list_for_each_entry_safe(connector, ct,
174                                  &dev->mode_config.connector_list, head) {
175                 if (!connector->encoder_ids[0]) {
176                         NV_WARN(dev, "%s has no encoders, removing\n",
177                                 drm_get_connector_name(connector));
178                         connector->funcs->destroy(connector);
179                 }
180         }
181
182         /* Save previous state */
183         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
184                 crtc->funcs->save(crtc);
185
186         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
187                 struct drm_encoder_helper_funcs *func = encoder->helper_private;
188
189                 func->save(encoder);
190         }
191
192         return 0;
193 }
194
195 void
196 nv04_display_destroy(struct drm_device *dev)
197 {
198         struct drm_encoder *encoder;
199         struct drm_crtc *crtc;
200
201         NV_DEBUG_KMS(dev, "\n");
202
203         /* Turn every CRTC off. */
204         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
205                 struct drm_mode_set modeset = {
206                         .crtc = crtc,
207                 };
208
209                 crtc->funcs->set_config(&modeset);
210         }
211
212         /* Restore state */
213         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
214                 struct drm_encoder_helper_funcs *func = encoder->helper_private;
215
216                 func->restore(encoder);
217         }
218
219         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
220                 crtc->funcs->restore(crtc);
221
222         drm_mode_config_cleanup(dev);
223
224         nouveau_hw_save_vga_fonts(dev, 0);
225 }
226
227 int
228 nv04_display_init(struct drm_device *dev)
229 {
230         struct drm_encoder *encoder;
231         struct drm_crtc *crtc;
232
233         /* meh.. modeset apparently doesn't setup all the regs and depends
234          * on pre-existing state, for now load the state of the card *before*
235          * nouveau was loaded, and then do a modeset.
236          *
237          * best thing to do probably is to make save/restore routines not
238          * save/restore "pre-load" state, but more general so we can save
239          * on suspend too.
240          */
241         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
242                 struct drm_encoder_helper_funcs *func = encoder->helper_private;
243
244                 func->restore(encoder);
245         }
246
247         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
248                 crtc->funcs->restore(crtc);
249
250         return 0;
251 }
252