NV50: kill set_pclk
[nouveau] / src / nv50_sor.c
1 /*
2  * Copyright (c) 2007 NVIDIA, Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sublicense, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included
13  * in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  */
23
24 #define DPMS_SERVER
25 #include <X11/extensions/dpms.h>
26 #include <X11/Xatom.h>
27
28 #include "nv_include.h"
29 #include "nv50_type.h"
30 #include "nv50_display.h"
31 #include "nv50_output.h"
32
33 void
34 NV50SorSetPClk(xf86OutputPtr output, int pclk)
35 {
36         NV50OutputPrivPtr nv_output = output->driver_private;
37         ScrnInfoPtr pScrn = output->scrn;
38         NVPtr pNv = NVPTR(pScrn);
39         const int limit = 165000;
40
41         NVWrite(pNv, 0x00614300 + nv_output->or * 0x800, (pclk > limit) ? 0x101 : 0);
42 }
43
44 static void
45 NV50SorDPMSSet(xf86OutputPtr output, int mode)
46 {
47         NV50OutputPrivPtr nv_output = output->driver_private;
48         ScrnInfoPtr pScrn = output->scrn;
49         NVPtr pNv = NVPTR(pScrn);
50         CARD32 tmp;
51
52         while((NVRead(pNv, 0x0061c004 + nv_output->or * 0x800) & 0x80000000));
53
54         tmp = NVRead(pNv, 0x0061c004 + nv_output->or * 0x800);
55         tmp |= 0x80000000;
56
57         if(mode == DPMSModeOn)
58                 tmp |= 1;
59         else
60                 tmp &= ~1;
61
62         NVWrite(pNv, 0x0061c004 + nv_output->or * 0x800, tmp);
63         while((NVRead(pNv, 0x0061c030 + nv_output->or * 0x800) & 0x10000000));
64 }
65
66 static int
67 NV50TMDSModeValid(xf86OutputPtr output, DisplayModePtr mode)
68 {
69         // Disable dual-link modes until I can find a way to make them work
70         // reliably.
71         if (mode->Clock > 165000)
72                 return MODE_CLOCK_HIGH;
73
74         return NV50OutputModeValid(output, mode);
75 }
76
77 static int
78 NV50LVDSModeValid(xf86OutputPtr output, DisplayModePtr mode)
79 {
80         NV50OutputPrivPtr nv_output = output->driver_private;
81         DisplayModePtr native = nv_output->nativeMode;
82
83         // Ignore modes larger than the native res.
84         if (mode->HDisplay > native->HDisplay || mode->VDisplay > native->VDisplay)
85                 return MODE_PANEL;
86
87         return NV50OutputModeValid(output, mode);
88 }
89
90 static void
91 NV50SorModeSet(xf86OutputPtr output, DisplayModePtr mode,
92                 DisplayModePtr adjusted_mode)
93 {
94         ScrnInfoPtr pScrn = output->scrn;
95         NV50OutputPrivPtr nv_output = output->driver_private;
96         const int sorOff = 0x40 * nv_output->or;
97         CARD32 type;
98
99         if(!adjusted_mode) {
100                 /* Disconnect the SOR */
101                 NV50DisplayCommand(pScrn, 0x600 + sorOff, 0);
102                 return;
103         }
104
105         if (nv_output->type == OUTPUT_LVDS) {
106                 type = 0;
107         } else
108         if (adjusted_mode->Clock > 165000) {
109                 type = 0x500;
110         } else {
111                 type = 0x100;
112         }
113
114         // This wouldn't be necessary, but the server is stupid and calls
115         // NV50SorDPMSSet after the output is disconnected, even though the hardware
116         // turns it off automatically.
117         NV50SorDPMSSet(output, DPMSModeOn);
118
119         NV50DisplayCommand(pScrn, 0x600 + sorOff,
120                 (NV50CrtcGetHead(output->crtc) == HEAD0 ? 1 : 2) | type |
121                 ((adjusted_mode->Flags & V_NHSYNC) ? 0x1000 : 0) |
122                 ((adjusted_mode->Flags & V_NVSYNC) ? 0x2000 : 0));
123
124         NV50CrtcSetScale(output->crtc, adjusted_mode, nv_output->scale);
125 }
126
127 static xf86OutputStatus
128 NV50SorDetect(xf86OutputPtr output)
129 {
130         NV50OutputPrivPtr nv_output = output->driver_private;
131         xf86MonPtr ddc_mon;
132
133         if (nv_output->pDDCBus == NULL)
134                 return XF86OutputStatusDisconnected;
135
136         ddc_mon = xf86OutputGetEDID(output, nv_output->pDDCBus);
137         if (!ddc_mon)
138                 return XF86OutputStatusDisconnected;
139
140         if (!ddc_mon->features.input_type) /* Analog? */
141                 return XF86OutputStatusDisconnected;
142
143         return XF86OutputStatusConnected;
144 }
145
146 static xf86OutputStatus
147 NV50SorLVDSDetect(xf86OutputPtr output)
148 {
149         /* Assume LVDS is always connected */
150         return XF86OutputStatusConnected;
151 }
152
153 static void
154 NV50SorDestroy(xf86OutputPtr output)
155 {
156         NV50OutputPrivPtr nv_output = output->driver_private;
157
158         NV50OutputDestroy(output);
159
160         xf86DeleteMode(&nv_output->nativeMode, nv_output->nativeMode);
161
162         xfree(output->driver_private);
163         output->driver_private = NULL;
164 }
165
166 static void
167 NV50SorSetModeBackend(DisplayModePtr dst, const DisplayModePtr src)
168 {
169         // Stash the backend mode timings from src into dst
170         dst->Clock           = src->Clock;
171         dst->Flags           = src->Flags;
172         dst->CrtcHDisplay    = src->CrtcHDisplay;
173         dst->CrtcHBlankStart = src->CrtcHBlankStart;
174         dst->CrtcHSyncStart  = src->CrtcHSyncStart;
175         dst->CrtcHSyncEnd    = src->CrtcHSyncEnd;
176         dst->CrtcHBlankEnd   = src->CrtcHBlankEnd;
177         dst->CrtcHTotal      = src->CrtcHTotal;
178         dst->CrtcHSkew       = src->CrtcHSkew;
179         dst->CrtcVDisplay    = src->CrtcVDisplay;
180         dst->CrtcVBlankStart = src->CrtcVBlankStart;
181         dst->CrtcVSyncStart  = src->CrtcVSyncStart;
182         dst->CrtcVSyncEnd    = src->CrtcVSyncEnd;
183         dst->CrtcVBlankEnd   = src->CrtcVBlankEnd;
184         dst->CrtcVTotal      = src->CrtcVTotal;
185         dst->CrtcHAdjusted   = src->CrtcHAdjusted;
186         dst->CrtcVAdjusted   = src->CrtcVAdjusted;
187 }
188
189 static Bool
190 NV50SorModeFixup(xf86OutputPtr output, DisplayModePtr mode,
191                  DisplayModePtr adjusted_mode)
192 {
193         NV50OutputPrivPtr nv_output = output->driver_private;
194         DisplayModePtr native = nv_output->nativeMode;
195
196         if(native && nv_output->scale != SCALE_PANEL) {
197                 NV50SorSetModeBackend(adjusted_mode, native);
198                 // This mode is already "fixed"
199                 NV50CrtcSkipModeFixup(output->crtc);
200         }
201
202         return TRUE;
203 }
204
205 static Bool
206 NV50SorTMDSModeFixup(xf86OutputPtr output, DisplayModePtr mode,
207                         DisplayModePtr adjusted_mode)
208 {
209         int scrnIndex = output->scrn->scrnIndex;
210         NV50OutputPrivPtr nv_output = output->driver_private;
211         DisplayModePtr modes = output->probed_modes;
212
213         xf86DeleteMode(&nv_output->nativeMode, nv_output->nativeMode);
214
215         if(modes) {
216                 // Find the preferred mode and use that as the "native" mode.
217                 // If no preferred mode is available, use the first one.
218                 DisplayModePtr mode;
219
220                 // Find the preferred mode.
221                 for(mode = modes; mode; mode = mode->next) {
222                         if(mode->type & M_T_PREFERRED) {
223                                 xf86DrvMsgVerb(scrnIndex, X_INFO, 5,
224                                                 "%s: preferred mode is %s\n",
225                                                 output->name, mode->name);
226                                 break;
227                         }
228                 }
229
230                 // XXX: May not want to allow scaling if no preferred mode is found.
231                 if(!mode) {
232                         mode = modes;
233                         xf86DrvMsgVerb(scrnIndex, X_INFO, 5,
234                                 "%s: no preferred mode found, using %s\n",
235                                 output->name, mode->name);
236                 }
237
238                 nv_output->nativeMode = xf86DuplicateMode(mode);
239                 NV50CrtcDoModeFixup(nv_output->nativeMode, mode);
240         }
241
242         return NV50SorModeFixup(output, mode, adjusted_mode);
243 }
244
245 static DisplayModePtr
246 NV50SorGetLVDSModes(xf86OutputPtr output)
247 {
248         NV50OutputPrivPtr nv_output = output->driver_private;
249         return xf86DuplicateMode(nv_output->nativeMode);
250 }
251
252 #define MAKE_ATOM(a) MakeAtom((a), sizeof(a) - 1, TRUE);
253
254 struct property {
255         Atom atom;
256         INT32 range[2];
257 };
258
259 static struct {
260         struct property dither;
261         struct property scale;
262 } properties;
263
264 static void
265 NV50SorCreateResources(xf86OutputPtr output)
266 {
267         ScrnInfoPtr pScrn = output->scrn;
268         NVPtr pNv = NVPTR(pScrn);
269         int data, err;
270         const char *s;
271
272         /******** dithering ********/
273         properties.dither.atom = MAKE_ATOM("dither");
274         properties.dither.range[0] = 0;
275         properties.dither.range[1] = 1;
276         err = RRConfigureOutputProperty(output->randr_output,
277                                         properties.dither.atom, FALSE, TRUE, FALSE,
278                                         2, properties.dither.range);
279         if(err)
280                 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
281                         "Failed to configure dithering property for %s: error %d\n",
282                         output->name, err);
283
284         // Set the default value
285         data = pNv->FPDither;
286         err = RRChangeOutputProperty(output->randr_output, properties.dither.atom,
287                                         XA_INTEGER, 32, PropModeReplace, 1, &data,
288                                         FALSE, FALSE);
289         if(err)
290                 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
291                         "Failed to set dithering property for %s: error %d\n",
292                         output->name, err);
293
294         /******** scaling ********/
295         properties.scale.atom = MAKE_ATOM("scale");
296         err = RRConfigureOutputProperty(output->randr_output,
297                                         properties.scale.atom, FALSE, FALSE,
298                                         FALSE, 0, NULL);
299         if(err)
300                 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
301                         "Failed to configure scaling property for %s: error %d\n",
302                         output->name, err);
303
304         // Set the default value
305         s = "aspect";
306         err = RRChangeOutputProperty(output->randr_output, properties.scale.atom,
307                                         XA_STRING, 8, PropModeReplace, strlen(s),
308                                         (pointer)s, FALSE, FALSE);
309         if(err)
310                 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
311                         "Failed to set scaling property for %s: error %d\n",
312                         output->name, err);
313 }
314
315 static Bool
316 NV50SorSetProperty(xf86OutputPtr output, Atom prop, RRPropertyValuePtr val)
317 {
318         NV50OutputPrivPtr nv_output = output->driver_private;
319
320         if(prop == properties.dither.atom) {
321                 INT32 i;
322
323                 if (val->type != XA_INTEGER || val->format != 32 || val->size != 1)
324                         return FALSE;
325
326                 i = *(INT32*)val->data;
327                 if (i < properties.dither.range[0] || i > properties.dither.range[1])
328                         return FALSE;
329
330                 NV50CrtcSetDither(output->crtc, i, TRUE);
331                 return TRUE;
332         } else if (prop == properties.scale.atom) {
333                 const char *s;
334                 enum scaling_modes oldScale, scale;
335                 int i;
336                 const struct {
337                         const char *name;
338                         enum scaling_modes scale;
339                 } modes[] = {
340                         { "panel", SCALE_PANEL },
341                         { "aspect", SCALE_ASPECT },
342                         { "fullscreen",   SCALE_FULLSCREEN },
343                         { "noscale", SCALE_NOSCALE },
344                         { NULL,     0 },
345                 };
346
347                 if (val->type != XA_STRING || val->format != 8)
348                         return FALSE;
349                 s = (char*)val->data;
350
351                 for (i = 0; modes[i].name; i++) {
352                         const char *name = modes[i].name;
353                         const int len = strlen(name);
354
355                         if(val->size == len && !strncmp(name, s, len)) {
356                                 scale = modes[i].scale;
357                                 break;
358                         }
359                 }
360                 if (!modes[i].name)
361                         return FALSE;
362                 if (scale == SCALE_PANEL && nv_output->type == OUTPUT_LVDS)
363                         // LVDS requires scaling
364                         return FALSE;
365
366                 oldScale = nv_output->scale;
367                 nv_output->scale = scale;
368                 if (output->crtc) {
369                         xf86CrtcPtr crtc = output->crtc;
370
371                         if (!xf86CrtcSetMode(crtc, &crtc->desiredMode, crtc->desiredRotation,
372                                         crtc->desiredX, crtc->desiredY)) {
373                                 xf86DrvMsg(crtc->scrn->scrnIndex, X_ERROR,
374                                                 "Failed to set scaling to %s for output %s\n",
375                                                 modes[i].name, output->name);
376
377                                 // Restore old scale and try again.
378                                 nv_output->scale = oldScale;
379                                 if (!xf86CrtcSetMode(crtc, &crtc->desiredMode,
380                                                         crtc->desiredRotation, crtc->desiredX,
381                                                         crtc->desiredY)) {
382                                         xf86DrvMsg(crtc->scrn->scrnIndex, X_ERROR,
383                                                 "Failed to restore old scaling for output %s\n",
384                                                 output->name);
385                                 }
386
387                                 return FALSE;
388                         }
389                 }
390                 return TRUE;
391         }
392
393         return FALSE;
394 }
395
396 static const xf86OutputFuncsRec NV50SorTMDSOutputFuncs = {
397         .dpms = NV50SorDPMSSet,
398         .save = NULL,
399         .restore = NULL,
400         .mode_valid = NV50TMDSModeValid,
401         .mode_fixup = NV50SorTMDSModeFixup,
402         .prepare = NV50OutputPrepare,
403         .commit = NV50OutputCommit,
404         .mode_set = NV50SorModeSet,
405         .detect = NV50SorDetect,
406         .get_modes = NV50OutputGetDDCModes,
407         .create_resources = NV50SorCreateResources,
408         .set_property = NV50SorSetProperty,
409         .destroy = NV50SorDestroy,
410 };
411
412 static const xf86OutputFuncsRec NV50SorLVDSOutputFuncs = {
413         .dpms = NV50SorDPMSSet,
414         .save = NULL,
415         .restore = NULL,
416         .mode_valid = NV50LVDSModeValid,
417         .mode_fixup = NV50SorModeFixup,
418         .prepare = NV50OutputPrepare,
419         .commit = NV50OutputCommit,
420         .mode_set = NV50SorModeSet,
421         .detect = NV50SorLVDSDetect,
422         .get_modes = NV50SorGetLVDSModes,
423         .create_resources = NV50SorCreateResources,
424         .set_property = NV50SorSetProperty,
425         .destroy = NV50SorDestroy,
426 };
427
428 static DisplayModePtr
429 ReadLVDSNativeMode(ScrnInfoPtr pScrn, const int off)
430 {
431         NVPtr pNv = NVPTR(pScrn);
432         DisplayModePtr mode = xnfcalloc(1, sizeof(DisplayModeRec));
433         const CARD32 size = NVRead(pNv, 0x00610b4c + off);
434         const int width = size & 0x3fff;
435         const int height = (size >> 16) & 0x3fff;
436
437         mode->HDisplay = mode->CrtcHDisplay = width;
438         mode->VDisplay = mode->CrtcVDisplay = height;
439         mode->Clock = NVRead(pNv, 0x00610ad4 + off) & 0x3fffff;
440         mode->CrtcHBlankStart = NVRead(pNv, 0x00610afc + off);
441         mode->CrtcHSyncEnd = NVRead(pNv, 0x00610b04 + off);
442         mode->CrtcHBlankEnd = NVRead(pNv, 0x00610ae8 + off);
443         mode->CrtcHTotal = NVRead(pNv, 0x00610af4 + off);
444
445         mode->next = mode->prev = NULL;
446         mode->status = MODE_OK;
447         mode->type = M_T_DRIVER | M_T_PREFERRED;
448
449         xf86SetModeDefaultName(mode);
450
451         return mode;
452 }
453
454 static DisplayModePtr
455 GetLVDSNativeMode(ScrnInfoPtr pScrn)
456 {
457         NVPtr pNv = NVPTR(pScrn);
458         CARD32 val = NVRead(pNv, 0x00610050);
459
460         if ((val & 0x3) == 0x2) {
461                 return ReadLVDSNativeMode(pScrn, 0);
462         } else if ((val & 0x300) == 0x200) {
463                 return ReadLVDSNativeMode(pScrn, 0x540);
464         }
465
466         return NULL;
467 }
468
469 xf86OutputPtr
470 NV50CreateSor(ScrnInfoPtr pScrn, ORNum or, NVOutputType type)
471 {
472         NV50OutputPrivPtr nv_output = xnfcalloc(sizeof(*nv_output), 1);
473         NVPtr pNv = NVPTR(pScrn);
474         xf86OutputPtr output;
475         char orName[5];
476         const xf86OutputFuncsRec *funcs;
477
478         if(!nv_output)
479                 return NULL;
480
481         if(type == OUTPUT_LVDS) {
482                 strcpy(orName, "LVDS");
483                 funcs = &NV50SorLVDSOutputFuncs;
484
485                 nv_output->nativeMode = GetLVDSNativeMode(pScrn);
486
487                 if(!nv_output->nativeMode) {
488                         xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
489                                 "Failed to find LVDS native mode\n");
490                         xfree(nv_output);
491                         return NULL;
492                 }
493
494                 xf86DrvMsg(pScrn->scrnIndex, X_INFO, "%s native size %dx%d\n",
495                         orName, nv_output->nativeMode->HDisplay,
496                         nv_output->nativeMode->VDisplay);
497         } else {
498                 snprintf(orName, 5, "DVI%d", or);
499                 funcs = &NV50SorTMDSOutputFuncs;
500         }
501
502         output = xf86OutputCreate(pScrn, funcs, orName);
503
504         nv_output->or = or;
505         nv_output->type = type;
506         output->driver_private = nv_output;
507         output->interlaceAllowed = TRUE;
508         output->doubleScanAllowed = TRUE;
509
510         if (type != OUTPUT_LVDS) {
511                 NVWrite(pNv, 0x0061c00c + nv_output->or * 0x800, 0x03010700);
512                 NVWrite(pNv, 0x0061c010 + nv_output->or * 0x800, 0x0000152f);
513                 NVWrite(pNv, 0x0061c014 + nv_output->or * 0x800, 0x00000000);
514                 NVWrite(pNv, 0x0061c018 + nv_output->or * 0x800, 0x00245af8);
515         }
516
517         return output;
518 }
519