d2aab65fd9efebf0405ef1d8f9c6017859786170
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / gpu / drm / nouveau / nv50_sor.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 <drm/drmP.h>
28 #include <drm/drm_crtc_helper.h>
29
30 #define NOUVEAU_DMA_DEBUG (nouveau_reg_debug & NOUVEAU_REG_DEBUG_EVO)
31 #include "nouveau_reg.h"
32 #include "nouveau_drm.h"
33 #include "nouveau_dma.h"
34 #include "nouveau_encoder.h"
35 #include "nouveau_connector.h"
36 #include "nouveau_crtc.h"
37 #include "nv50_display.h"
38
39 #include <core/class.h>
40
41 #include <subdev/timer.h>
42
43 static void
44 nv50_sor_dp_train_set(struct drm_device *dev, struct dcb_output *dcb, u8 pattern)
45 {
46         struct nv50_display *disp = nv50_display(dev);
47         const u32 or = ffs(dcb->or) - 1, link = !(dcb->sorconf.link & 1);
48         const u32 moff = (link << 2) | or;
49         nv_call(disp->core, NV94_DISP_SOR_DP_TRAIN + moff, pattern);
50 }
51
52 static void
53 nv50_sor_dp_train_adj(struct drm_device *dev, struct dcb_output *dcb,
54                       u8 lane, u8 swing, u8 preem)
55 {
56         struct nv50_display *disp = nv50_display(dev);
57         const u32 or = ffs(dcb->or) - 1, link = !(dcb->sorconf.link & 1);
58         const u32 moff = (link << 2) | or;
59         const u32 data = (swing << 8) | preem;
60         nv_call(disp->core, NV94_DISP_SOR_DP_DRVCTL(lane) + moff, data);
61 }
62
63 static void
64 nv50_sor_dp_link_set(struct drm_device *dev, struct dcb_output *dcb, int crtc,
65                      int link_nr, u32 link_bw, bool enhframe)
66 {
67         struct nv50_display *disp = nv50_display(dev);
68         const u32 or = ffs(dcb->or) - 1, link = !(dcb->sorconf.link & 1);
69         const u32 moff = (crtc << 3) | (link << 2) | or;
70         u32 data = ((link_bw / 27000) << 8) | link_nr;
71         if (enhframe)
72                 data |= NV94_DISP_SOR_DP_LNKCTL_FRAME_ENH;
73         nv_call(disp->core, NV94_DISP_SOR_DP_LNKCTL + moff, data);
74 }
75
76 static void
77 nv50_sor_dp_link_get(struct drm_device *dev, u32 or, u32 link, u32 *nr, u32 *bw)
78 {
79         struct nouveau_device *device = nouveau_dev(dev);
80         u32 dpctrl = nv_rd32(device, NV50_SOR_DP_CTRL(or, link)) & 0x000f0000;
81         u32 clksor = nv_rd32(device, 0x614300 + (or * 0x800));
82         if (clksor & 0x000c0000)
83                 *bw = 270000;
84         else
85                 *bw = 162000;
86
87         if      (dpctrl > 0x00030000) *nr = 4;
88         else if (dpctrl > 0x00010000) *nr = 2;
89         else                          *nr = 1;
90 }
91
92 void
93 nv50_sor_dp_calc_tu(struct drm_device *dev, int or, int link, u32 clk, u32 bpp)
94 {
95         struct nouveau_device *device = nouveau_dev(dev);
96         struct nouveau_drm *drm = nouveau_drm(dev);
97         const u32 symbol = 100000;
98         int bestTU = 0, bestVTUi = 0, bestVTUf = 0, bestVTUa = 0;
99         int TU, VTUi, VTUf, VTUa;
100         u64 link_data_rate, link_ratio, unk;
101         u32 best_diff = 64 * symbol;
102         u32 link_nr, link_bw, r;
103
104         /* calculate packed data rate for each lane */
105         nv50_sor_dp_link_get(dev, or, link, &link_nr, &link_bw);
106         link_data_rate = (clk * bpp / 8) / link_nr;
107
108         /* calculate ratio of packed data rate to link symbol rate */
109         link_ratio = link_data_rate * symbol;
110         r = do_div(link_ratio, link_bw);
111
112         for (TU = 64; TU >= 32; TU--) {
113                 /* calculate average number of valid symbols in each TU */
114                 u32 tu_valid = link_ratio * TU;
115                 u32 calc, diff;
116
117                 /* find a hw representation for the fraction.. */
118                 VTUi = tu_valid / symbol;
119                 calc = VTUi * symbol;
120                 diff = tu_valid - calc;
121                 if (diff) {
122                         if (diff >= (symbol / 2)) {
123                                 VTUf = symbol / (symbol - diff);
124                                 if (symbol - (VTUf * diff))
125                                         VTUf++;
126
127                                 if (VTUf <= 15) {
128                                         VTUa  = 1;
129                                         calc += symbol - (symbol / VTUf);
130                                 } else {
131                                         VTUa  = 0;
132                                         VTUf  = 1;
133                                         calc += symbol;
134                                 }
135                         } else {
136                                 VTUa  = 0;
137                                 VTUf  = min((int)(symbol / diff), 15);
138                                 calc += symbol / VTUf;
139                         }
140
141                         diff = calc - tu_valid;
142                 } else {
143                         /* no remainder, but the hw doesn't like the fractional
144                          * part to be zero.  decrement the integer part and
145                          * have the fraction add a whole symbol back
146                          */
147                         VTUa = 0;
148                         VTUf = 1;
149                         VTUi--;
150                 }
151
152                 if (diff < best_diff) {
153                         best_diff = diff;
154                         bestTU = TU;
155                         bestVTUa = VTUa;
156                         bestVTUf = VTUf;
157                         bestVTUi = VTUi;
158                         if (diff == 0)
159                                 break;
160                 }
161         }
162
163         if (!bestTU) {
164                 NV_ERROR(drm, "DP: unable to find suitable config\n");
165                 return;
166         }
167
168         /* XXX close to vbios numbers, but not right */
169         unk  = (symbol - link_ratio) * bestTU;
170         unk *= link_ratio;
171         r = do_div(unk, symbol);
172         r = do_div(unk, symbol);
173         unk += 6;
174
175         nv_mask(device, NV50_SOR_DP_CTRL(or, link), 0x000001fc, bestTU << 2);
176         nv_mask(device, NV50_SOR_DP_SCFG(or, link), 0x010f7f3f, bestVTUa << 24 |
177                                                              bestVTUf << 16 |
178                                                              bestVTUi << 8 |
179                                                              unk);
180 }
181 static void
182 nv50_sor_disconnect(struct drm_encoder *encoder)
183 {
184         struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
185         struct nouveau_drm *drm = nouveau_drm(encoder->dev);
186         struct drm_device *dev = encoder->dev;
187         struct nouveau_channel *evo = nv50_display(dev)->master;
188         int ret;
189
190         if (!nv_encoder->crtc)
191                 return;
192         nv50_crtc_blank(nouveau_crtc(nv_encoder->crtc), true);
193
194         NV_DEBUG(drm, "Disconnecting SOR %d\n", nv_encoder->or);
195
196         ret = RING_SPACE(evo, 4);
197         if (ret) {
198                 NV_ERROR(drm, "no space while disconnecting SOR\n");
199                 return;
200         }
201         BEGIN_NV04(evo, 0, NV50_EVO_SOR(nv_encoder->or, MODE_CTRL), 1);
202         OUT_RING  (evo, 0);
203         BEGIN_NV04(evo, 0, NV50_EVO_UPDATE, 1);
204         OUT_RING  (evo, 0);
205
206         nouveau_hdmi_mode_set(encoder, NULL);
207
208         nv_encoder->crtc = NULL;
209         nv_encoder->last_dpms = DRM_MODE_DPMS_OFF;
210 }
211
212 static void
213 nv50_sor_dpms(struct drm_encoder *encoder, int mode)
214 {
215         struct nv50_display *priv = nv50_display(encoder->dev);
216         struct nouveau_drm *drm = nouveau_drm(encoder->dev);
217         struct drm_device *dev = encoder->dev;
218         struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
219         struct drm_encoder *enc;
220         int or = nv_encoder->or;
221
222         NV_DEBUG(drm, "or %d type %d mode %d\n", or, nv_encoder->dcb->type, mode);
223
224         nv_encoder->last_dpms = mode;
225         list_for_each_entry(enc, &dev->mode_config.encoder_list, head) {
226                 struct nouveau_encoder *nvenc = nouveau_encoder(enc);
227
228                 if (nvenc == nv_encoder ||
229                     (nvenc->dcb->type != DCB_OUTPUT_TMDS &&
230                      nvenc->dcb->type != DCB_OUTPUT_LVDS &&
231                      nvenc->dcb->type != DCB_OUTPUT_DP) ||
232                     nvenc->dcb->or != nv_encoder->dcb->or)
233                         continue;
234
235                 if (nvenc->last_dpms == DRM_MODE_DPMS_ON)
236                         return;
237         }
238
239         nv_call(priv->core, NV50_DISP_SOR_PWR + or, (mode == DRM_MODE_DPMS_ON));
240
241         if (nv_encoder->dcb->type == DCB_OUTPUT_DP) {
242                 struct dp_train_func func = {
243                         .link_set = nv50_sor_dp_link_set,
244                         .train_set = nv50_sor_dp_train_set,
245                         .train_adj = nv50_sor_dp_train_adj
246                 };
247
248                 nouveau_dp_dpms(encoder, mode, nv_encoder->dp.datarate, &func);
249         }
250 }
251
252 static void
253 nv50_sor_save(struct drm_encoder *encoder)
254 {
255         struct nouveau_drm *drm = nouveau_drm(encoder->dev);
256         NV_ERROR(drm, "!!\n");
257 }
258
259 static void
260 nv50_sor_restore(struct drm_encoder *encoder)
261 {
262         struct nouveau_drm *drm = nouveau_drm(encoder->dev);
263         NV_ERROR(drm, "!!\n");
264 }
265
266 static bool
267 nv50_sor_mode_fixup(struct drm_encoder *encoder,
268                     const struct drm_display_mode *mode,
269                     struct drm_display_mode *adjusted_mode)
270 {
271         struct nouveau_drm *drm = nouveau_drm(encoder->dev);
272         struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
273         struct nouveau_connector *connector;
274
275         NV_DEBUG(drm, "or %d\n", nv_encoder->or);
276
277         connector = nouveau_encoder_connector_get(nv_encoder);
278         if (!connector) {
279                 NV_ERROR(drm, "Encoder has no connector\n");
280                 return false;
281         }
282
283         if (connector->scaling_mode != DRM_MODE_SCALE_NONE &&
284              connector->native_mode)
285                 drm_mode_copy(adjusted_mode, connector->native_mode);
286
287         return true;
288 }
289
290 static void
291 nv50_sor_prepare(struct drm_encoder *encoder)
292 {
293         struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
294         nv50_sor_disconnect(encoder);
295         if (nv_encoder->dcb->type == DCB_OUTPUT_DP) {
296                 /* avoid race between link training and supervisor intr */
297                 nv50_display_sync(encoder->dev);
298         }
299 }
300
301 static void
302 nv50_sor_commit(struct drm_encoder *encoder)
303 {
304 }
305
306 static void
307 nv50_sor_mode_set(struct drm_encoder *encoder, struct drm_display_mode *umode,
308                   struct drm_display_mode *mode)
309 {
310         struct nouveau_channel *evo = nv50_display(encoder->dev)->master;
311         struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
312         struct nouveau_drm *drm = nouveau_drm(encoder->dev);
313         struct nouveau_crtc *crtc = nouveau_crtc(encoder->crtc);
314         struct nouveau_connector *nv_connector;
315         uint32_t mode_ctl = 0;
316         int ret;
317
318         NV_DEBUG(drm, "or %d type %d -> crtc %d\n",
319                      nv_encoder->or, nv_encoder->dcb->type, crtc->index);
320         nv_encoder->crtc = encoder->crtc;
321
322         switch (nv_encoder->dcb->type) {
323         case DCB_OUTPUT_TMDS:
324                 if (nv_encoder->dcb->sorconf.link & 1) {
325                         if (mode->clock < 165000)
326                                 mode_ctl = 0x0100;
327                         else
328                                 mode_ctl = 0x0500;
329                 } else
330                         mode_ctl = 0x0200;
331
332                 nouveau_hdmi_mode_set(encoder, mode);
333                 break;
334         case DCB_OUTPUT_DP:
335                 nv_connector = nouveau_encoder_connector_get(nv_encoder);
336                 if (nv_connector && nv_connector->base.display_info.bpc == 6) {
337                         nv_encoder->dp.datarate = mode->clock * 18 / 8;
338                         mode_ctl |= 0x00020000;
339                 } else {
340                         nv_encoder->dp.datarate = mode->clock * 24 / 8;
341                         mode_ctl |= 0x00050000;
342                 }
343
344                 if (nv_encoder->dcb->sorconf.link & 1)
345                         mode_ctl |= 0x00000800;
346                 else
347                         mode_ctl |= 0x00000900;
348                 break;
349         default:
350                 break;
351         }
352
353         if (crtc->index == 1)
354                 mode_ctl |= NV50_EVO_SOR_MODE_CTRL_CRTC1;
355         else
356                 mode_ctl |= NV50_EVO_SOR_MODE_CTRL_CRTC0;
357
358         if (mode->flags & DRM_MODE_FLAG_NHSYNC)
359                 mode_ctl |= NV50_EVO_SOR_MODE_CTRL_NHSYNC;
360
361         if (mode->flags & DRM_MODE_FLAG_NVSYNC)
362                 mode_ctl |= NV50_EVO_SOR_MODE_CTRL_NVSYNC;
363
364         nv50_sor_dpms(encoder, DRM_MODE_DPMS_ON);
365
366         ret = RING_SPACE(evo, 2);
367         if (ret) {
368                 NV_ERROR(drm, "no space while connecting SOR\n");
369                 nv_encoder->crtc = NULL;
370                 return;
371         }
372         BEGIN_NV04(evo, 0, NV50_EVO_SOR(nv_encoder->or, MODE_CTRL), 1);
373         OUT_RING(evo, mode_ctl);
374 }
375
376 static struct drm_crtc *
377 nv50_sor_crtc_get(struct drm_encoder *encoder)
378 {
379         return nouveau_encoder(encoder)->crtc;
380 }
381
382 static const struct drm_encoder_helper_funcs nv50_sor_helper_funcs = {
383         .dpms = nv50_sor_dpms,
384         .save = nv50_sor_save,
385         .restore = nv50_sor_restore,
386         .mode_fixup = nv50_sor_mode_fixup,
387         .prepare = nv50_sor_prepare,
388         .commit = nv50_sor_commit,
389         .mode_set = nv50_sor_mode_set,
390         .get_crtc = nv50_sor_crtc_get,
391         .detect = NULL,
392         .disable = nv50_sor_disconnect
393 };
394
395 static void
396 nv50_sor_destroy(struct drm_encoder *encoder)
397 {
398         struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
399
400         drm_encoder_cleanup(encoder);
401         kfree(nv_encoder);
402 }
403
404 static const struct drm_encoder_funcs nv50_sor_encoder_funcs = {
405         .destroy = nv50_sor_destroy,
406 };
407
408 int
409 nv50_sor_create(struct drm_connector *connector, struct dcb_output *entry)
410 {
411         struct nouveau_encoder *nv_encoder = NULL;
412         struct drm_device *dev = connector->dev;
413         struct drm_encoder *encoder;
414         int type;
415
416         switch (entry->type) {
417         case DCB_OUTPUT_TMDS:
418         case DCB_OUTPUT_DP:
419                 type = DRM_MODE_ENCODER_TMDS;
420                 break;
421         case DCB_OUTPUT_LVDS:
422                 type = DRM_MODE_ENCODER_LVDS;
423                 break;
424         default:
425                 return -EINVAL;
426         }
427
428         nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
429         if (!nv_encoder)
430                 return -ENOMEM;
431         encoder = to_drm_encoder(nv_encoder);
432
433         nv_encoder->dcb = entry;
434         nv_encoder->or = ffs(entry->or) - 1;
435         nv_encoder->last_dpms = DRM_MODE_DPMS_OFF;
436
437         drm_encoder_init(dev, encoder, &nv50_sor_encoder_funcs, type);
438         drm_encoder_helper_add(encoder, &nv50_sor_helper_funcs);
439
440         encoder->possible_crtcs = entry->heads;
441         encoder->possible_clones = 0;
442
443         drm_mode_connector_attach_encoder(connector, encoder);
444         return 0;
445 }