Drop the NV*VGA defines and adjust users
[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 static 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->panelType == 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
132         /* Assume physical status isn't going to change before the BlockHandler */
133         if(nv_output->cached_status != XF86OutputStatusUnknown)
134                 return nv_output->cached_status;
135
136         NV50OutputPartnersDetect(nv_output->partner, output, nv_output->i2c);
137         return nv_output->cached_status;
138 }
139
140 static xf86OutputStatus
141 NV50SorLVDSDetect(xf86OutputPtr output)
142 {
143         /* Assume LVDS is always connected */
144         return XF86OutputStatusConnected;
145 }
146
147 static void
148 NV50SorDestroy(xf86OutputPtr output)
149 {
150         NV50OutputPrivPtr nv_output = output->driver_private;
151
152         NV50OutputDestroy(output);
153
154         xf86DeleteMode(&nv_output->nativeMode, nv_output->nativeMode);
155
156         xfree(output->driver_private);
157         output->driver_private = NULL;
158 }
159
160 static void
161 NV50SorSetModeBackend(DisplayModePtr dst, const DisplayModePtr src)
162 {
163         // Stash the backend mode timings from src into dst
164         dst->Clock           = src->Clock;
165         dst->Flags           = src->Flags;
166         dst->CrtcHDisplay    = src->CrtcHDisplay;
167         dst->CrtcHBlankStart = src->CrtcHBlankStart;
168         dst->CrtcHSyncStart  = src->CrtcHSyncStart;
169         dst->CrtcHSyncEnd    = src->CrtcHSyncEnd;
170         dst->CrtcHBlankEnd   = src->CrtcHBlankEnd;
171         dst->CrtcHTotal      = src->CrtcHTotal;
172         dst->CrtcHSkew       = src->CrtcHSkew;
173         dst->CrtcVDisplay    = src->CrtcVDisplay;
174         dst->CrtcVBlankStart = src->CrtcVBlankStart;
175         dst->CrtcVSyncStart  = src->CrtcVSyncStart;
176         dst->CrtcVSyncEnd    = src->CrtcVSyncEnd;
177         dst->CrtcVBlankEnd   = src->CrtcVBlankEnd;
178         dst->CrtcVTotal      = src->CrtcVTotal;
179         dst->CrtcHAdjusted   = src->CrtcHAdjusted;
180         dst->CrtcVAdjusted   = src->CrtcVAdjusted;
181 }
182
183 static Bool
184 NV50SorModeFixup(xf86OutputPtr output, DisplayModePtr mode,
185                  DisplayModePtr adjusted_mode)
186 {
187         NV50OutputPrivPtr nv_output = output->driver_private;
188         DisplayModePtr native = nv_output->nativeMode;
189
190         if(native && nv_output->scale != NV50_SCALE_OFF) {
191                 NV50SorSetModeBackend(adjusted_mode, native);
192                 // This mode is already "fixed"
193                 NV50CrtcSkipModeFixup(output->crtc);
194         }
195
196         return TRUE;
197 }
198
199 static Bool
200 NV50SorTMDSModeFixup(xf86OutputPtr output, DisplayModePtr mode,
201                         DisplayModePtr adjusted_mode)
202 {
203         int scrnIndex = output->scrn->scrnIndex;
204         NV50OutputPrivPtr nv_output = output->driver_private;
205         DisplayModePtr modes = output->probed_modes;
206
207         xf86DeleteMode(&nv_output->nativeMode, nv_output->nativeMode);
208
209         if(modes) {
210                 // Find the preferred mode and use that as the "native" mode.
211                 // If no preferred mode is available, use the first one.
212                 DisplayModePtr mode;
213
214                 // Find the preferred mode.
215                 for(mode = modes; mode; mode = mode->next) {
216                         if(mode->type & M_T_PREFERRED) {
217                                 xf86DrvMsgVerb(scrnIndex, X_INFO, 5,
218                                                 "%s: preferred mode is %s\n",
219                                                 output->name, mode->name);
220                                 break;
221                         }
222                 }
223
224                 // XXX: May not want to allow scaling if no preferred mode is found.
225                 if(!mode) {
226                         mode = modes;
227                         xf86DrvMsgVerb(scrnIndex, X_INFO, 5,
228                                 "%s: no preferred mode found, using %s\n",
229                                 output->name, mode->name);
230                 }
231
232                 nv_output->nativeMode = xf86DuplicateMode(mode);
233                 NV50CrtcDoModeFixup(nv_output->nativeMode, mode);
234         }
235
236         return NV50SorModeFixup(output, mode, adjusted_mode);
237 }
238
239 static DisplayModePtr
240 NV50SorGetLVDSModes(xf86OutputPtr output)
241 {
242         NV50OutputPrivPtr nv_output = output->driver_private;
243         return xf86DuplicateMode(nv_output->nativeMode);
244 }
245
246 #define MAKE_ATOM(a) MakeAtom((a), sizeof(a) - 1, TRUE);
247
248 struct property {
249         Atom atom;
250         INT32 range[2];
251 };
252
253 static struct {
254         struct property dither;
255         struct property scale;
256 } properties;
257
258 static void
259 NV50SorCreateResources(xf86OutputPtr output)
260 {
261         ScrnInfoPtr pScrn = output->scrn;
262         NVPtr pNv = NVPTR(pScrn);
263         int data, err;
264         const char *s;
265
266         /******** dithering ********/
267         properties.dither.atom = MAKE_ATOM("dither");
268         properties.dither.range[0] = 0;
269         properties.dither.range[1] = 1;
270         err = RRConfigureOutputProperty(output->randr_output,
271                                         properties.dither.atom, FALSE, TRUE, FALSE,
272                                         2, properties.dither.range);
273         if(err)
274                 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
275                         "Failed to configure dithering property for %s: error %d\n",
276                         output->name, err);
277
278         // Set the default value
279         data = pNv->FPDither;
280         err = RRChangeOutputProperty(output->randr_output, properties.dither.atom,
281                                         XA_INTEGER, 32, PropModeReplace, 1, &data,
282                                         FALSE, FALSE);
283         if(err)
284                 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
285                         "Failed to set dithering property for %s: error %d\n",
286                         output->name, err);
287
288         /******** scaling ********/
289         properties.scale.atom = MAKE_ATOM("scale");
290         err = RRConfigureOutputProperty(output->randr_output,
291                                         properties.scale.atom, FALSE, FALSE,
292                                         FALSE, 0, NULL);
293         if(err)
294                 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
295                         "Failed to configure scaling property for %s: error %d\n",
296                         output->name, err);
297
298         // Set the default value
299         s = "aspect";
300         err = RRChangeOutputProperty(output->randr_output, properties.scale.atom,
301                                         XA_STRING, 8, PropModeReplace, strlen(s),
302                                         (pointer)s, FALSE, FALSE);
303         if(err)
304                 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
305                         "Failed to set scaling property for %s: error %d\n",
306                         output->name, err);
307 }
308
309 static Bool
310 NV50SorSetProperty(xf86OutputPtr output, Atom prop, RRPropertyValuePtr val)
311 {
312         NV50OutputPrivPtr nv_output = output->driver_private;
313
314         if(prop == properties.dither.atom) {
315                 INT32 i;
316
317                 if (val->type != XA_INTEGER || val->format != 32 || val->size != 1)
318                         return FALSE;
319
320                 i = *(INT32*)val->data;
321                 if (i < properties.dither.range[0] || i > properties.dither.range[1])
322                         return FALSE;
323
324                 NV50CrtcSetDither(output->crtc, i, TRUE);
325                 return TRUE;
326         } else if (prop == properties.scale.atom) {
327                 const char *s;
328                 enum NV50ScaleMode oldScale, scale;
329                 int i;
330                 const struct {
331                         const char *name;
332                         enum NV50ScaleMode scale;
333                 } modes[] = {
334                         { "off",    NV50_SCALE_OFF },
335                         { "aspect", NV50_SCALE_ASPECT },
336                         { "fill",   NV50_SCALE_FILL },
337                         { "center", NV50_SCALE_CENTER },
338                         { NULL,     0 },
339                 };
340
341                 if (val->type != XA_STRING || val->format != 8)
342                         return FALSE;
343                 s = (char*)val->data;
344
345                 for (i = 0; modes[i].name; i++) {
346                         const char *name = modes[i].name;
347                         const int len = strlen(name);
348
349                         if(val->size == len && !strncmp(name, s, len)) {
350                                 scale = modes[i].scale;
351                                 break;
352                         }
353                 }
354                 if (!modes[i].name)
355                         return FALSE;
356                 if (scale == NV50_SCALE_OFF && nv_output->panelType == LVDS)
357                         // LVDS requires scaling
358                         return FALSE;
359
360                 oldScale = nv_output->scale;
361                 nv_output->scale = scale;
362                 if (output->crtc) {
363                         xf86CrtcPtr crtc = output->crtc;
364
365                         if (!xf86CrtcSetMode(crtc, &crtc->desiredMode, crtc->desiredRotation,
366                                         crtc->desiredX, crtc->desiredY)) {
367                                 xf86DrvMsg(crtc->scrn->scrnIndex, X_ERROR,
368                                                 "Failed to set scaling to %s for output %s\n",
369                                                 modes[i].name, output->name);
370
371                                 // Restore old scale and try again.
372                                 nv_output->scale = oldScale;
373                                 if (!xf86CrtcSetMode(crtc, &crtc->desiredMode,
374                                                         crtc->desiredRotation, crtc->desiredX,
375                                                         crtc->desiredY)) {
376                                         xf86DrvMsg(crtc->scrn->scrnIndex, X_ERROR,
377                                                 "Failed to restore old scaling for output %s\n",
378                                                 output->name);
379                                 }
380
381                                 return FALSE;
382                         }
383                 }
384                 return TRUE;
385         }
386
387         return FALSE;
388 }
389
390 static const xf86OutputFuncsRec NV50SorTMDSOutputFuncs = {
391         .dpms = NV50SorDPMSSet,
392         .save = NULL,
393         .restore = NULL,
394         .mode_valid = NV50TMDSModeValid,
395         .mode_fixup = NV50SorTMDSModeFixup,
396         .prepare = NV50OutputPrepare,
397         .commit = NV50OutputCommit,
398         .mode_set = NV50SorModeSet,
399         .detect = NV50SorDetect,
400         .get_modes = NV50OutputGetDDCModes,
401         .create_resources = NV50SorCreateResources,
402         .set_property = NV50SorSetProperty,
403         .destroy = NV50SorDestroy,
404 };
405
406 static const xf86OutputFuncsRec NV50SorLVDSOutputFuncs = {
407         .dpms = NV50SorDPMSSet,
408         .save = NULL,
409         .restore = NULL,
410         .mode_valid = NV50LVDSModeValid,
411         .mode_fixup = NV50SorModeFixup,
412         .prepare = NV50OutputPrepare,
413         .commit = NV50OutputCommit,
414         .mode_set = NV50SorModeSet,
415         .detect = NV50SorLVDSDetect,
416         .get_modes = NV50SorGetLVDSModes,
417         .create_resources = NV50SorCreateResources,
418         .set_property = NV50SorSetProperty,
419         .destroy = NV50SorDestroy,
420 };
421
422 static DisplayModePtr
423 ReadLVDSNativeMode(ScrnInfoPtr pScrn, const int off)
424 {
425         NVPtr pNv = NVPTR(pScrn);
426         DisplayModePtr mode = xnfcalloc(1, sizeof(DisplayModeRec));
427         const CARD32 size = NVRead(pNv, 0x00610b4c + off);
428         const int width = size & 0x3fff;
429         const int height = (size >> 16) & 0x3fff;
430
431         mode->HDisplay = mode->CrtcHDisplay = width;
432         mode->VDisplay = mode->CrtcVDisplay = height;
433         mode->Clock = NVRead(pNv, 0x00610ad4 + off) & 0x3fffff;
434         mode->CrtcHBlankStart = NVRead(pNv, 0x00610afc + off);
435         mode->CrtcHSyncEnd = NVRead(pNv, 0x00610b04 + off);
436         mode->CrtcHBlankEnd = NVRead(pNv, 0x00610ae8 + off);
437         mode->CrtcHTotal = NVRead(pNv, 0x00610af4 + off);
438
439         mode->next = mode->prev = NULL;
440         mode->status = MODE_OK;
441         mode->type = M_T_DRIVER | M_T_PREFERRED;
442
443         xf86SetModeDefaultName(mode);
444
445         return mode;
446 }
447
448 static DisplayModePtr
449 GetLVDSNativeMode(ScrnInfoPtr pScrn)
450 {
451         NVPtr pNv = NVPTR(pScrn);
452         CARD32 val = NVRead(pNv, 0x00610050);
453
454         if ((val & 0x3) == 0x2) {
455                 return ReadLVDSNativeMode(pScrn, 0);
456         } else if ((val & 0x300) == 0x200) {
457                 return ReadLVDSNativeMode(pScrn, 0x540);
458         }
459
460         return NULL;
461 }
462
463 xf86OutputPtr
464 NV50CreateSor(ScrnInfoPtr pScrn, ORNum or, PanelType panelType)
465 {
466         NV50OutputPrivPtr nv_output = xnfcalloc(sizeof(*nv_output), 1);
467         NVPtr pNv = NVPTR(pScrn);
468         xf86OutputPtr output;
469         char orName[5];
470         const xf86OutputFuncsRec *funcs;
471
472         if(!nv_output)
473                 return NULL;
474
475         if(panelType == LVDS) {
476                 strcpy(orName, "LVDS");
477                 funcs = &NV50SorLVDSOutputFuncs;
478
479                 nv_output->nativeMode = GetLVDSNativeMode(pScrn);
480
481                 if(!nv_output->nativeMode) {
482                         xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
483                                 "Failed to find LVDS native mode\n");
484                         xfree(nv_output);
485                         return NULL;
486                 }
487
488                 xf86DrvMsg(pScrn->scrnIndex, X_INFO, "%s native size %dx%d\n",
489                         orName, nv_output->nativeMode->HDisplay,
490                         nv_output->nativeMode->VDisplay);
491         } else {
492                 snprintf(orName, 5, "DVI%d", or);
493                 funcs = &NV50SorTMDSOutputFuncs;
494         }
495
496         output = xf86OutputCreate(pScrn, funcs, orName);
497
498         nv_output->type = SOR;
499         nv_output->or = or;
500         nv_output->panelType = panelType;
501         nv_output->cached_status = XF86OutputStatusUnknown;
502         if (panelType == TMDS)
503                 nv_output->set_pclk = NV50SorSetPClk;
504         output->driver_private = nv_output;
505         output->interlaceAllowed = TRUE;
506         output->doubleScanAllowed = TRUE;
507
508         if (panelType != LVDS) {
509                 NVWrite(pNv, 0x0061c00c + nv_output->or * 0x800, 0x03010700);
510                 NVWrite(pNv, 0x0061c010 + nv_output->or * 0x800, 0x0000152f);
511                 NVWrite(pNv, 0x0061c014 + nv_output->or * 0x800, 0x00000000);
512                 NVWrite(pNv, 0x0061c018 + nv_output->or * 0x800, 0x00245af8);
513         }
514
515         return output;
516 }
517