Fix my nv10 cursor.
[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         const int limit = 165000;
37
38         NV50OutputWrite(output, 0x4300, (pclk > limit) ? 0x101 : 0);
39 }
40
41 static void
42 NV50SorDPMSSet(xf86OutputPtr output, int mode)
43 {
44         CARD32 tmp;
45
46         while((NV50OutputRead(output, 0xc004) & 0x80000000));
47
48         tmp = NV50OutputRead(output, 0xc004);
49         tmp |= 0x80000000;
50
51         if(mode == DPMSModeOn)
52                 tmp |= 1;
53         else
54                 tmp &= ~1;
55
56         NV50OutputWrite(output, 0xc004, tmp);
57         while((NV50OutputRead(output, 0xc030) & 0x10000000));
58 }
59
60 static int
61 NV50TMDSModeValid(xf86OutputPtr output, DisplayModePtr mode)
62 {
63         // Disable dual-link modes until I can find a way to make them work
64         // reliably.
65         if (mode->Clock > 165000)
66                 return MODE_CLOCK_HIGH;
67
68         return NV50OutputModeValid(output, mode);
69 }
70
71 static int
72 NV50LVDSModeValid(xf86OutputPtr output, DisplayModePtr mode)
73 {
74         NV50OutputPrivPtr nv_output = output->driver_private;
75         DisplayModePtr native = nv_output->nativeMode;
76
77         // Ignore modes larger than the native res.
78         if (mode->HDisplay > native->HDisplay || mode->VDisplay > native->VDisplay)
79                 return MODE_PANEL;
80
81         return NV50OutputModeValid(output, mode);
82 }
83
84 static void
85 NV50SorModeSet(xf86OutputPtr output, DisplayModePtr mode,
86                 DisplayModePtr adjusted_mode)
87 {
88         ScrnInfoPtr pScrn = output->scrn;
89         NV50OutputPrivPtr nv_output = output->driver_private;
90         const int sorOff = 0x40 * nv_output->or;
91         CARD32 type;
92
93         if(!adjusted_mode) {
94                 /* Disconnect the SOR */
95                 NV50DisplayCommand(pScrn, 0x600 + sorOff, 0);
96                 return;
97         }
98
99         if (nv_output->panelType == LVDS) {
100                 type = 0;
101         } else
102         if (adjusted_mode->Clock > 165000) {
103                 type = 0x500;
104         } else {
105                 type = 0x100;
106         }
107
108         // This wouldn't be necessary, but the server is stupid and calls
109         // NV50SorDPMSSet after the output is disconnected, even though the hardware
110         // turns it off automatically.
111         NV50SorDPMSSet(output, DPMSModeOn);
112
113         NV50DisplayCommand(pScrn, 0x600 + sorOff,
114                 (NV50CrtcGetHead(output->crtc) == HEAD0 ? 1 : 2) | type |
115                 ((adjusted_mode->Flags & V_NHSYNC) ? 0x1000 : 0) |
116                 ((adjusted_mode->Flags & V_NVSYNC) ? 0x2000 : 0));
117
118         NV50CrtcSetScale(output->crtc, adjusted_mode, nv_output->scale);
119 }
120
121 static xf86OutputStatus
122 NV50SorDetect(xf86OutputPtr output)
123 {
124         NV50OutputPrivPtr nv_output = output->driver_private;
125
126         /* Assume physical status isn't going to change before the BlockHandler */
127         if(nv_output->cached_status != XF86OutputStatusUnknown)
128                 return nv_output->cached_status;
129
130         NV50OutputPartnersDetect(nv_output->partner, output, nv_output->i2c);
131         return nv_output->cached_status;
132 }
133
134 static xf86OutputStatus
135 NV50SorLVDSDetect(xf86OutputPtr output)
136 {
137         /* Assume LVDS is always connected */
138         return XF86OutputStatusConnected;
139 }
140
141 static void
142 NV50SorDestroy(xf86OutputPtr output)
143 {
144         NV50OutputPrivPtr nv_output = output->driver_private;
145
146         NV50OutputDestroy(output);
147
148         xf86DeleteMode(&nv_output->nativeMode, nv_output->nativeMode);
149
150         xfree(output->driver_private);
151         output->driver_private = NULL;
152 }
153
154 static void
155 NV50SorSetModeBackend(DisplayModePtr dst, const DisplayModePtr src)
156 {
157         // Stash the backend mode timings from src into dst
158         dst->Clock           = src->Clock;
159         dst->Flags           = src->Flags;
160         dst->CrtcHDisplay    = src->CrtcHDisplay;
161         dst->CrtcHBlankStart = src->CrtcHBlankStart;
162         dst->CrtcHSyncStart  = src->CrtcHSyncStart;
163         dst->CrtcHSyncEnd    = src->CrtcHSyncEnd;
164         dst->CrtcHBlankEnd   = src->CrtcHBlankEnd;
165         dst->CrtcHTotal      = src->CrtcHTotal;
166         dst->CrtcHSkew       = src->CrtcHSkew;
167         dst->CrtcVDisplay    = src->CrtcVDisplay;
168         dst->CrtcVBlankStart = src->CrtcVBlankStart;
169         dst->CrtcVSyncStart  = src->CrtcVSyncStart;
170         dst->CrtcVSyncEnd    = src->CrtcVSyncEnd;
171         dst->CrtcVBlankEnd   = src->CrtcVBlankEnd;
172         dst->CrtcVTotal      = src->CrtcVTotal;
173         dst->CrtcHAdjusted   = src->CrtcHAdjusted;
174         dst->CrtcVAdjusted   = src->CrtcVAdjusted;
175 }
176
177 static Bool
178 NV50SorModeFixup(xf86OutputPtr output, DisplayModePtr mode,
179                  DisplayModePtr adjusted_mode)
180 {
181         NV50OutputPrivPtr nv_output = output->driver_private;
182         DisplayModePtr native = nv_output->nativeMode;
183
184         if(native && nv_output->scale != NV50_SCALE_OFF) {
185                 NV50SorSetModeBackend(adjusted_mode, native);
186                 // This mode is already "fixed"
187                 NV50CrtcSkipModeFixup(output->crtc);
188         }
189
190         return TRUE;
191 }
192
193 static Bool
194 NV50SorTMDSModeFixup(xf86OutputPtr output, DisplayModePtr mode,
195                         DisplayModePtr adjusted_mode)
196 {
197         int scrnIndex = output->scrn->scrnIndex;
198         NV50OutputPrivPtr nv_output = output->driver_private;
199         DisplayModePtr modes = output->probed_modes;
200
201         xf86DeleteMode(&nv_output->nativeMode, nv_output->nativeMode);
202
203         if(modes) {
204                 // Find the preferred mode and use that as the "native" mode.
205                 // If no preferred mode is available, use the first one.
206                 DisplayModePtr mode;
207
208                 // Find the preferred mode.
209                 for(mode = modes; mode; mode = mode->next) {
210                         if(mode->type & M_T_PREFERRED) {
211                                 xf86DrvMsgVerb(scrnIndex, X_INFO, 5,
212                                                 "%s: preferred mode is %s\n",
213                                                 output->name, mode->name);
214                                 break;
215                         }
216                 }
217
218                 // XXX: May not want to allow scaling if no preferred mode is found.
219                 if(!mode) {
220                         mode = modes;
221                         xf86DrvMsgVerb(scrnIndex, X_INFO, 5,
222                                 "%s: no preferred mode found, using %s\n",
223                                 output->name, mode->name);
224                 }
225
226                 nv_output->nativeMode = xf86DuplicateMode(mode);
227                 NV50CrtcDoModeFixup(nv_output->nativeMode, mode);
228         }
229
230         return NV50SorModeFixup(output, mode, adjusted_mode);
231 }
232
233 static DisplayModePtr
234 NV50SorGetLVDSModes(xf86OutputPtr output)
235 {
236         NV50OutputPrivPtr nv_output = output->driver_private;
237         return xf86DuplicateMode(nv_output->nativeMode);
238 }
239
240 #define MAKE_ATOM(a) MakeAtom((a), sizeof(a) - 1, TRUE);
241
242 struct property {
243         Atom atom;
244         INT32 range[2];
245 };
246
247 static struct {
248         struct property dither;
249         struct property scale;
250 } properties;
251
252 static void
253 NV50SorCreateResources(xf86OutputPtr output)
254 {
255         ScrnInfoPtr pScrn = output->scrn;
256         NVPtr pNv = NVPTR(pScrn);
257         int data, err;
258         const char *s;
259
260         /******** dithering ********/
261         properties.dither.atom = MAKE_ATOM("dither");
262         properties.dither.range[0] = 0;
263         properties.dither.range[1] = 1;
264         err = RRConfigureOutputProperty(output->randr_output,
265                                         properties.dither.atom, FALSE, TRUE, FALSE,
266                                         2, properties.dither.range);
267         if(err)
268                 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
269                         "Failed to configure dithering property for %s: error %d\n",
270                         output->name, err);
271
272         // Set the default value
273         data = pNv->FPDither;
274         err = RRChangeOutputProperty(output->randr_output, properties.dither.atom,
275                                         XA_INTEGER, 32, PropModeReplace, 1, &data,
276                                         FALSE, FALSE);
277         if(err)
278                 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
279                         "Failed to set dithering property for %s: error %d\n",
280                         output->name, err);
281
282         /******** scaling ********/
283         properties.scale.atom = MAKE_ATOM("scale");
284         err = RRConfigureOutputProperty(output->randr_output,
285                                         properties.scale.atom, FALSE, FALSE,
286                                         FALSE, 0, NULL);
287         if(err)
288                 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
289                         "Failed to configure scaling property for %s: error %d\n",
290                         output->name, err);
291
292         // Set the default value
293         s = "aspect";
294         err = RRChangeOutputProperty(output->randr_output, properties.scale.atom,
295                                         XA_STRING, 8, PropModeReplace, strlen(s),
296                                         (pointer)s, FALSE, FALSE);
297         if(err)
298                 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
299                         "Failed to set scaling property for %s: error %d\n",
300                         output->name, err);
301 }
302
303 static Bool
304 NV50SorSetProperty(xf86OutputPtr output, Atom prop, RRPropertyValuePtr val)
305 {
306         NV50OutputPrivPtr nv_output = output->driver_private;
307
308         if(prop == properties.dither.atom) {
309                 INT32 i;
310
311                 if (val->type != XA_INTEGER || val->format != 32 || val->size != 1)
312                         return FALSE;
313
314                 i = *(INT32*)val->data;
315                 if (i < properties.dither.range[0] || i > properties.dither.range[1])
316                         return FALSE;
317
318                 NV50CrtcSetDither(output->crtc, i, TRUE);
319                 return TRUE;
320         } else if (prop == properties.scale.atom) {
321                 const char *s;
322                 enum NV50ScaleMode oldScale, scale;
323                 int i;
324                 const struct {
325                         const char *name;
326                         enum NV50ScaleMode scale;
327                 } modes[] = {
328                         { "off",    NV50_SCALE_OFF },
329                         { "aspect", NV50_SCALE_ASPECT },
330                         { "fill",   NV50_SCALE_FILL },
331                         { "center", NV50_SCALE_CENTER },
332                         { NULL,     0 },
333                 };
334
335                 if (val->type != XA_STRING || val->format != 8)
336                         return FALSE;
337                 s = (char*)val->data;
338
339                 for (i = 0; modes[i].name; i++) {
340                         const char *name = modes[i].name;
341                         const int len = strlen(name);
342
343                         if(val->size == len && !strncmp(name, s, len)) {
344                                 scale = modes[i].scale;
345                                 break;
346                         }
347                 }
348                 if (!modes[i].name)
349                         return FALSE;
350                 if (scale == NV50_SCALE_OFF && nv_output->panelType == LVDS)
351                         // LVDS requires scaling
352                         return FALSE;
353
354                 oldScale = nv_output->scale;
355                 nv_output->scale = scale;
356                 if (output->crtc) {
357                         xf86CrtcPtr crtc = output->crtc;
358
359                         if (!xf86CrtcSetMode(crtc, &crtc->desiredMode, crtc->desiredRotation,
360                                         crtc->desiredX, crtc->desiredY)) {
361                                 xf86DrvMsg(crtc->scrn->scrnIndex, X_ERROR,
362                                                 "Failed to set scaling to %s for output %s\n",
363                                                 modes[i].name, output->name);
364
365                                 // Restore old scale and try again.
366                                 nv_output->scale = oldScale;
367                                 if (!xf86CrtcSetMode(crtc, &crtc->desiredMode,
368                                                         crtc->desiredRotation, crtc->desiredX,
369                                                         crtc->desiredY)) {
370                                         xf86DrvMsg(crtc->scrn->scrnIndex, X_ERROR,
371                                                 "Failed to restore old scaling for output %s\n",
372                                                 output->name);
373                                 }
374
375                                 return FALSE;
376                         }
377                 }
378                 return TRUE;
379         }
380
381         return FALSE;
382 }
383
384 static const xf86OutputFuncsRec NV50SorTMDSOutputFuncs = {
385         .dpms = NV50SorDPMSSet,
386         .save = NULL,
387         .restore = NULL,
388         .mode_valid = NV50TMDSModeValid,
389         .mode_fixup = NV50SorTMDSModeFixup,
390         .prepare = NV50OutputPrepare,
391         .commit = NV50OutputCommit,
392         .mode_set = NV50SorModeSet,
393         .detect = NV50SorDetect,
394         .get_modes = NV50OutputGetDDCModes,
395         .create_resources = NV50SorCreateResources,
396         .set_property = NV50SorSetProperty,
397         .destroy = NV50SorDestroy,
398 };
399
400 static const xf86OutputFuncsRec NV50SorLVDSOutputFuncs = {
401         .dpms = NV50SorDPMSSet,
402         .save = NULL,
403         .restore = NULL,
404         .mode_valid = NV50LVDSModeValid,
405         .mode_fixup = NV50SorModeFixup,
406         .prepare = NV50OutputPrepare,
407         .commit = NV50OutputCommit,
408         .mode_set = NV50SorModeSet,
409         .detect = NV50SorLVDSDetect,
410         .get_modes = NV50SorGetLVDSModes,
411         .create_resources = NV50SorCreateResources,
412         .set_property = NV50SorSetProperty,
413         .destroy = NV50SorDestroy,
414 };
415
416 static DisplayModePtr
417 ReadLVDSNativeMode(ScrnInfoPtr pScrn, const int off)
418 {
419         DisplayModePtr mode = xnfcalloc(1, sizeof(DisplayModeRec));
420         const CARD32 size = NV50DisplayRead(pScrn, 0xb4c + off);
421         const int width = size & 0x3fff;
422         const int height = (size >> 16) & 0x3fff;
423
424         mode->HDisplay = mode->CrtcHDisplay = width;
425         mode->VDisplay = mode->CrtcVDisplay = height;
426         mode->Clock = NV50DisplayRead(pScrn, 0xad4 + off) & 0x3fffff;
427         mode->CrtcHBlankStart = NV50DisplayRead(pScrn, 0xafc + off);
428         mode->CrtcHSyncEnd = NV50DisplayRead(pScrn, 0xb04 + off);
429         mode->CrtcHBlankEnd = NV50DisplayRead(pScrn, 0xae8 + off);
430         mode->CrtcHTotal = NV50DisplayRead(pScrn, 0xaf4 + off);
431
432         mode->next = mode->prev = NULL;
433         mode->status = MODE_OK;
434         mode->type = M_T_DRIVER | M_T_PREFERRED;
435
436         xf86SetModeDefaultName(mode);
437
438         return mode;
439 }
440
441 static DisplayModePtr
442 GetLVDSNativeMode(ScrnInfoPtr pScrn)
443 {
444         CARD32 val = NV50DisplayRead(pScrn, 0x50);
445
446         if ((val & 0x3) == 0x2) {
447                 return ReadLVDSNativeMode(pScrn, 0);
448         } else if ((val & 0x300) == 0x200) {
449                 return ReadLVDSNativeMode(pScrn, 0x540);
450         }
451
452         return NULL;
453 }
454
455 xf86OutputPtr
456 NV50CreateSor(ScrnInfoPtr pScrn, ORNum or, PanelType panelType)
457 {
458         NV50OutputPrivPtr nv_output = xnfcalloc(sizeof(*nv_output), 1);
459         xf86OutputPtr output;
460         char orName[5];
461         const xf86OutputFuncsRec *funcs;
462
463         if(!nv_output)
464                 return NULL;
465
466         if(panelType == LVDS) {
467                 strcpy(orName, "LVDS");
468                 funcs = &NV50SorLVDSOutputFuncs;
469
470                 nv_output->nativeMode = GetLVDSNativeMode(pScrn);
471
472                 if(!nv_output->nativeMode) {
473                         xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
474                                 "Failed to find LVDS native mode\n");
475                         xfree(nv_output);
476                         return NULL;
477                 }
478
479                 xf86DrvMsg(pScrn->scrnIndex, X_INFO, "%s native size %dx%d\n",
480                         orName, nv_output->nativeMode->HDisplay,
481                         nv_output->nativeMode->VDisplay);
482         } else {
483                 snprintf(orName, 5, "DVI%d", or);
484                 funcs = &NV50SorTMDSOutputFuncs;
485         }
486
487         output = xf86OutputCreate(pScrn, funcs, orName);
488
489         nv_output->type = SOR;
490         nv_output->or = or;
491         nv_output->panelType = panelType;
492         nv_output->cached_status = XF86OutputStatusUnknown;
493         if (panelType == TMDS)
494                 nv_output->set_pclk = NV50SorSetPClk;
495         output->driver_private = nv_output;
496         output->interlaceAllowed = TRUE;
497         output->doubleScanAllowed = TRUE;
498
499         if (panelType != LVDS) {
500                 NV50OutputWrite(output, 0xc00c, 0x03010700);
501                 NV50OutputWrite(output, 0xc010, 0x0000152f);
502                 NV50OutputWrite(output, 0xc014, 0x00000000);
503                 NV50OutputWrite(output, 0xc018, 0x00245af8);
504         }
505
506         return output;
507 }
508