nv_driver.c revision 6e60514d
1/*
2 * Copyright 1996-1997 David J. McKay
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 AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
19 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23#include <stdio.h>
24#include <fcntl.h>
25
26#include "nv_include.h"
27
28#include "xorg-server.h"
29#include "xf86drm.h"
30#include "xf86drmMode.h"
31#include "nouveau_drm.h"
32#ifdef DRI2
33#include "dri2.h"
34#endif
35
36#include "nouveau_copy.h"
37#include "nouveau_present.h"
38#include "nouveau_sync.h"
39
40#if !HAVE_XORG_LIST
41#define xorg_list_is_empty              list_is_empty
42#define xorg_list_for_each_entry        list_for_each_entry
43#endif
44
45/*
46 * Forward definitions for the functions that make up the driver.
47 */
48/* Mandatory functions */
49static const OptionInfoRec * NVAvailableOptions(int chipid, int busid);
50static void    NVIdentify(int flags);
51static Bool    NVPreInit(ScrnInfoPtr pScrn, int flags);
52static Bool    NVScreenInit(SCREEN_INIT_ARGS_DECL);
53static Bool    NVEnterVT(VT_FUNC_ARGS_DECL);
54static void    NVLeaveVT(VT_FUNC_ARGS_DECL);
55static Bool    NVCloseScreen(CLOSE_SCREEN_ARGS_DECL);
56static Bool    NVSaveScreen(ScreenPtr pScreen, int mode);
57static void    NVCloseDRM(ScrnInfoPtr);
58
59/* Optional functions */
60static Bool    NVDriverFunc(ScrnInfoPtr scrn, xorgDriverFuncOp op,
61			    void *data);
62static Bool    NVSwitchMode(SWITCH_MODE_ARGS_DECL);
63static void    NVAdjustFrame(ADJUST_FRAME_ARGS_DECL);
64static void    NVFreeScreen(FREE_SCREEN_ARGS_DECL);
65
66/* Internally used functions */
67
68static Bool	NVMapMem(ScrnInfoPtr pScrn);
69static Bool	NVUnmapMem(ScrnInfoPtr pScrn);
70
71#define NOUVEAU_PCI_DEVICE(_vendor_id, _device_id)                             \
72	{ (_vendor_id), (_device_id), PCI_MATCH_ANY, PCI_MATCH_ANY,            \
73	  0x00030000, 0x00ff0000, 0 }
74
75static const struct pci_id_match nouveau_device_match[] = {
76	NOUVEAU_PCI_DEVICE(0x12d2, PCI_MATCH_ANY),
77	NOUVEAU_PCI_DEVICE(0x10de, PCI_MATCH_ANY),
78	{ 0, 0, 0 },
79};
80
81static Bool NVPciProbe (	DriverPtr 		drv,
82				int 			entity_num,
83				struct pci_device	*dev,
84				intptr_t		match_data	);
85
86#ifdef XSERVER_PLATFORM_BUS
87static Bool NVPlatformProbe(DriverPtr driver,
88				int entity_num, int flags,
89				struct xf86_platform_device *dev,
90				intptr_t dev_match_data);
91#endif
92
93_X_EXPORT int NVEntityIndex = -1;
94
95static int getNVEntityIndex(void)
96{
97	return NVEntityIndex;
98}
99
100/*
101 * This contains the functions needed by the server after loading the
102 * driver module.  It must be supplied, and gets added the driver list by
103 * the Module Setup funtion in the dynamic case.  In the static case a
104 * reference to this is compiled in, and this requires that the name of
105 * this DriverRec be an upper-case version of the driver name.
106 */
107
108_X_EXPORT DriverRec NV = {
109	NV_VERSION,
110	NV_DRIVER_NAME,
111	NVIdentify,
112	NULL,
113	NVAvailableOptions,
114	NULL,
115	0,
116	NVDriverFunc,
117	nouveau_device_match,
118	NVPciProbe,
119#ifdef XSERVER_PLATFORM_BUS
120	NVPlatformProbe,
121#endif
122};
123
124struct NvFamily
125{
126  char *name;
127  char *chipset;
128};
129
130static struct NvFamily NVKnownFamilies[] =
131{
132  { "RIVA TNT",    "NV04" },
133  { "RIVA TNT2",   "NV05" },
134  { "GeForce 256", "NV10" },
135  { "GeForce 2",   "NV11, NV15" },
136  { "GeForce 4MX", "NV17, NV18" },
137  { "GeForce 3",   "NV20" },
138  { "GeForce 4Ti", "NV25, NV28" },
139  { "GeForce FX",  "NV3x" },
140  { "GeForce 6",   "NV4x" },
141  { "GeForce 7",   "G7x" },
142  { "GeForce 8",   "G8x" },
143  { "GeForce 9",   "G9x" },
144  { "GeForce GTX 2xx/3xx", "GT2xx" },
145  { "GeForce GTX 4xx/5xx", "GFxxx" },
146  { "GeForce GTX 6xx/7xx", "GKxxx" },
147  { "GeForce GTX 9xx", "GMxxx" },
148  { "GeForce GTX 10xx", "GPxxx" },
149  { NULL, NULL}
150};
151
152static MODULESETUPPROTO(nouveauSetup);
153
154static XF86ModuleVersionInfo nouveauVersRec =
155{
156    "nouveau",
157    MODULEVENDORSTRING,
158    MODINFOSTRING1,
159    MODINFOSTRING2,
160    XORG_VERSION_CURRENT,
161    PACKAGE_VERSION_MAJOR, PACKAGE_VERSION_MINOR, PACKAGE_VERSION_PATCHLEVEL,
162    ABI_CLASS_VIDEODRV,                     /* This is a video driver */
163    ABI_VIDEODRV_VERSION,
164    MOD_CLASS_VIDEODRV,
165    {0,0,0,0}
166};
167
168_X_EXPORT XF86ModuleData nouveauModuleData = { &nouveauVersRec, nouveauSetup, NULL };
169
170static pointer
171nouveauSetup(pointer module, pointer opts, int *errmaj, int *errmin)
172{
173	static Bool setupDone = FALSE;
174
175	/* This module should be loaded only once, but check to be sure. */
176
177	if (!setupDone) {
178		setupDone = TRUE;
179		/* The 1 here is needed to turn off a backwards compatibility mode */
180		/* Otherwise NVPciProbe() is not called */
181		xf86AddDriver(&NV, module, 1);
182
183		/*
184		 * The return value must be non-NULL on success even though there
185		 * is no TearDownProc.
186		 */
187		return (pointer)1;
188	} else {
189		if (errmaj) *errmaj = LDR_ONCEONLY;
190		return NULL;
191	}
192}
193
194static const OptionInfoRec *
195NVAvailableOptions(int chipid, int busid)
196{
197    return NVOptions;
198}
199
200/* Mandatory */
201static void
202NVIdentify(int flags)
203{
204    struct NvFamily *family;
205    size_t maxLen=0;
206
207    xf86DrvMsg(0, X_INFO, NV_NAME " driver " NV_DRIVER_DATE "\n");
208    xf86DrvMsg(0, X_INFO, NV_NAME " driver for NVIDIA chipset families :\n");
209
210    /* maximum length for alignment */
211    family = NVKnownFamilies;
212    while(family->name && family->chipset)
213    {
214        maxLen = max(maxLen, strlen(family->name));
215        family++;
216    }
217
218    /* display */
219    family = NVKnownFamilies;
220    while(family->name && family->chipset)
221    {
222        size_t len = strlen(family->name);
223        xf86ErrorF("\t%s", family->name);
224        while(len<maxLen+1)
225        {
226            xf86ErrorF(" ");
227            len++;
228        }
229        xf86ErrorF("(%s)\n", family->chipset);
230        family++;
231    }
232}
233
234static Bool
235NVDriverFunc(ScrnInfoPtr scrn, xorgDriverFuncOp op, void *data)
236{
237    xorgHWFlags *flag;
238
239    switch (op) {
240	case GET_REQUIRED_HW_INTERFACES:
241	    flag = (CARD32 *)data;
242	    (*flag) = 0;
243	    return TRUE;
244#if XORG_VERSION_CURRENT > XORG_VERSION_NUMERIC(1,15,99,0,0)
245	case SUPPORTS_SERVER_FDS:
246	    return TRUE;
247#endif
248	default:
249	    return FALSE;
250    }
251}
252
253static void
254NVInitScrn(ScrnInfoPtr pScrn, struct xf86_platform_device *platform_dev,
255	   int entity_num)
256{
257	DevUnion *pPriv;
258	NVEntPtr pNVEnt;
259
260	pScrn->driverVersion    = NV_VERSION;
261	pScrn->driverName       = NV_DRIVER_NAME;
262	pScrn->name             = NV_NAME;
263
264	pScrn->Probe            = NULL;
265	pScrn->PreInit          = NVPreInit;
266	pScrn->ScreenInit       = NVScreenInit;
267	pScrn->SwitchMode       = NVSwitchMode;
268	pScrn->AdjustFrame      = NVAdjustFrame;
269	pScrn->EnterVT          = NVEnterVT;
270	pScrn->LeaveVT          = NVLeaveVT;
271	pScrn->FreeScreen       = NVFreeScreen;
272
273	xf86SetEntitySharable(entity_num);
274	if (NVEntityIndex == -1)
275	    NVEntityIndex = xf86AllocateEntityPrivateIndex();
276
277	pPriv = xf86GetEntityPrivate(entity_num,
278				     NVEntityIndex);
279	if (!pPriv->ptr) {
280		pPriv->ptr = xnfcalloc(sizeof(NVEntRec), 1);
281		pNVEnt = pPriv->ptr;
282		pNVEnt->platform_dev = platform_dev;
283	}
284	else
285		pNVEnt = pPriv->ptr;
286
287	/* Reset settings which must not persist across server regeneration */
288	if (pNVEnt->reinitGeneration != serverGeneration) {
289		pNVEnt->reinitGeneration = serverGeneration;
290		/* Clear mask of assigned crtc's in this generation to "none" */
291		pNVEnt->assigned_crtcs = 0;
292	}
293
294	xf86SetEntityInstanceForScreen(pScrn, entity_num,
295					xf86GetNumEntityInstances(entity_num) - 1);
296}
297
298static struct nouveau_device *
299NVOpenNouveauDevice(struct pci_device *pci_dev,
300	struct xf86_platform_device *platform_dev, int scrnIndex, Bool probe)
301{
302	struct nouveau_device *dev = NULL;
303	char *busid;
304	int ret, fd = -1;
305
306#ifdef ODEV_ATTRIB_PATH
307	if (platform_dev)
308		busid = NULL;
309	else
310#endif
311	{
312#if XORG_VERSION_CURRENT >= XORG_VERSION_NUMERIC(1,9,99,901,0)
313		XNFasprintf(&busid, "pci:%04x:%02x:%02x.%d",
314			    pci_dev->domain, pci_dev->bus,
315			    pci_dev->dev, pci_dev->func);
316#else
317		busid = XNFprintf("pci:%04x:%02x:%02x.%d",
318				  pci_dev->domain, pci_dev->bus,
319				  pci_dev->dev, pci_dev->func);
320#endif
321	}
322
323#if defined(ODEV_ATTRIB_FD)
324	if (platform_dev)
325		fd = xf86_get_platform_device_int_attrib(platform_dev,
326							 ODEV_ATTRIB_FD, -1);
327#endif
328	if (fd != -1)
329		ret = nouveau_device_wrap(fd, 0, &dev);
330#ifdef ODEV_ATTRIB_PATH
331	else if (platform_dev) {
332		const char *path;
333
334		path = xf86_get_platform_device_attrib(platform_dev,
335						       ODEV_ATTRIB_PATH);
336
337		fd = open(path, O_RDWR | O_CLOEXEC);
338		ret = nouveau_device_wrap(fd, 1, &dev);
339		if (ret)
340			close(fd);
341	}
342#endif
343	else
344		ret = nouveau_device_open(busid, &dev);
345	if (ret)
346		xf86DrvMsg(scrnIndex, X_ERROR,
347			   "[drm] Failed to open DRM device for %s: %d\n",
348			   busid, ret);
349
350	free(busid);
351	return dev;
352}
353
354static Bool
355NVHasKMS(struct pci_device *pci_dev, struct xf86_platform_device *platform_dev)
356{
357	struct nouveau_device *dev = NULL;
358	drmVersion *version;
359	int chipset;
360
361	dev = NVOpenNouveauDevice(pci_dev, platform_dev, -1, TRUE);
362	if (!dev)
363		return FALSE;
364
365	/* Check the version reported by the kernel module.  In theory we
366	 * shouldn't have to do this, as libdrm_nouveau will do its own checks.
367	 * But, we're currently using the kernel patchlevel to also version
368	 * the DRI interface.
369	 */
370	version = drmGetVersion(dev->fd);
371	xf86DrvMsg(-1, X_INFO, "[drm] nouveau interface version: %d.%d.%d\n",
372		   version->version_major, version->version_minor,
373		   version->version_patchlevel);
374	drmFree(version);
375
376	chipset = dev->chipset;
377	nouveau_device_del(&dev);
378
379
380	switch (chipset & ~0xf) {
381	case 0x00:
382	case 0x10:
383	case 0x20:
384	case 0x30:
385	case 0x40:
386	case 0x60:
387	case 0x50:
388	case 0x80:
389	case 0x90:
390	case 0xa0:
391	case 0xc0:
392	case 0xd0:
393	case 0xe0:
394	case 0xf0:
395	case 0x100:
396	case 0x110:
397	case 0x120:
398	case 0x130:
399		break;
400	default:
401		xf86DrvMsg(-1, X_ERROR, "Unknown chipset: NV%02X\n", chipset);
402		return FALSE;
403	}
404	return TRUE;
405}
406
407static Bool
408NVPciProbe(DriverPtr drv, int entity_num, struct pci_device *pci_dev,
409	   intptr_t match_data)
410{
411	PciChipsets NVChipsets[] = {
412		{ pci_dev->device_id,
413		  (pci_dev->vendor_id << 16) | pci_dev->device_id, NULL },
414		{ -1, -1, NULL }
415	};
416	ScrnInfoPtr pScrn = NULL;
417
418	if (!NVHasKMS(pci_dev, NULL))
419		return FALSE;
420
421	pScrn = xf86ConfigPciEntity(pScrn, 0, entity_num, NVChipsets,
422				    NULL, NULL, NULL, NULL, NULL);
423	if (!pScrn)
424		return FALSE;
425
426	NVInitScrn(pScrn, NULL, entity_num);
427
428	return TRUE;
429}
430
431#ifdef XSERVER_PLATFORM_BUS
432static Bool
433NVPlatformProbe(DriverPtr driver,
434            int entity_num, int flags, struct xf86_platform_device *dev, intptr_t dev_match_data)
435{
436	ScrnInfoPtr scrn = NULL;
437	uint32_t scr_flags = 0;
438
439	if (!NVHasKMS(dev->pdev, dev))
440		return FALSE;
441
442	if (flags & PLATFORM_PROBE_GPU_SCREEN)
443		scr_flags = XF86_ALLOCATE_GPU_SCREEN;
444
445	scrn = xf86AllocateScreen(driver, scr_flags);
446	if (!scrn)
447		return FALSE;
448
449	if (xf86IsEntitySharable(entity_num))
450		xf86SetEntityShared(entity_num);
451	xf86AddEntityToScreen(scrn, entity_num);
452
453	NVInitScrn(scrn, dev, entity_num);
454
455	return TRUE;
456}
457#endif
458
459#define MAX_CHIPS MAXSCREENS
460
461Bool
462NVSwitchMode(SWITCH_MODE_ARGS_DECL)
463{
464	SCRN_INFO_PTR(arg);
465
466	return xf86SetSingleMode(pScrn, mode, RR_Rotate_0);
467}
468
469/*
470 * This function is used to initialize the Start Address - the first
471 * displayed location in the video memory.
472 */
473/* Usually mandatory */
474void
475NVAdjustFrame(ADJUST_FRAME_ARGS_DECL)
476{
477	SCRN_INFO_PTR(arg);
478	drmmode_adjust_frame(pScrn, x, y);
479}
480
481/*
482 * This is called when VT switching back to the X server.  Its job is
483 * to reinitialise the video mode.
484 */
485
486/* Mandatory */
487static Bool
488NVEnterVT(VT_FUNC_ARGS_DECL)
489{
490	SCRN_INFO_PTR(arg);
491	NVPtr pNv = NVPTR(pScrn);
492#ifdef XF86_PDEV_SERVER_FD
493	NVEntPtr pNVEnt = NVEntPriv(pScrn);
494#endif
495	int ret;
496
497	xf86DrvMsg(pScrn->scrnIndex, X_INFO, "NVEnterVT is called.\n");
498
499#ifdef XF86_PDEV_SERVER_FD
500	if (!(pNVEnt->platform_dev &&
501	      (pNVEnt->platform_dev->flags & XF86_PDEV_SERVER_FD)))
502#endif
503	{
504		ret = drmSetMaster(pNv->dev->fd);
505		if (ret)
506			ErrorF("Unable to get master: %s\n", strerror(errno));
507	}
508
509	if (XF86_CRTC_CONFIG_PTR(pScrn)->num_crtc && !xf86SetDesiredModes(pScrn))
510		return FALSE;
511
512	if (pNv->overlayAdaptor && pNv->Architecture != NV_ARCH_04)
513		NV10WriteOverlayParameters(pScrn);
514
515	return TRUE;
516}
517
518/*
519 * This is called when VT switching away from the X server.  Its job is
520 * to restore the previous (text) mode.
521 */
522
523/* Mandatory */
524static void
525NVLeaveVT(VT_FUNC_ARGS_DECL)
526{
527	SCRN_INFO_PTR(arg);
528	NVPtr pNv = NVPTR(pScrn);
529#ifdef XF86_PDEV_SERVER_FD
530	NVEntPtr pNVEnt = NVEntPriv(pScrn);
531#endif
532	int ret;
533
534	xf86DrvMsg(pScrn->scrnIndex, X_INFO, "NVLeaveVT is called.\n");
535
536#ifdef XF86_PDEV_SERVER_FD
537	if (pNVEnt->platform_dev &&
538	    (pNVEnt->platform_dev->flags & XF86_PDEV_SERVER_FD))
539		return;
540#endif
541
542	ret = drmDropMaster(pNv->dev->fd);
543	if (ret && errno != EIO && errno != ENODEV)
544		ErrorF("Error dropping master: %i(%m)\n", -errno);
545}
546
547static void
548NVFlushCallback(CallbackListPtr *list, pointer user_data, pointer call_data)
549{
550	ScrnInfoPtr pScrn = user_data;
551	NVPtr pNv = NVPTR(pScrn);
552	if (pScrn->vtSema && pNv->Flush)
553		pNv->Flush(pScrn);
554}
555
556#ifdef NOUVEAU_PIXMAP_SHARING
557static void
558redisplay_dirty(ScreenPtr screen, PixmapDirtyUpdatePtr dirty)
559{
560	RegionRec pixregion;
561
562	PixmapRegionInit(&pixregion, dirty->slave_dst);
563
564	DamageRegionAppend(&dirty->slave_dst->drawable, &pixregion);
565#ifdef HAS_DIRTYTRACKING_ROTATION
566	PixmapSyncDirtyHelper(dirty);
567#else
568	PixmapSyncDirtyHelper(dirty, &pixregion);
569#endif
570
571	DamageRegionProcessPending(&dirty->slave_dst->drawable);
572	RegionUninit(&pixregion);
573}
574
575static void
576nouveau_dirty_update(ScreenPtr screen)
577{
578	RegionPtr region;
579	PixmapDirtyUpdatePtr ent;
580
581	if (xorg_list_is_empty(&screen->pixmap_dirty_list))
582		return;
583
584	xorg_list_for_each_entry(ent, &screen->pixmap_dirty_list, ent) {
585		region = DamageRegion(ent->damage);
586		if (RegionNotEmpty(region)) {
587			redisplay_dirty(screen, ent);
588			DamageEmpty(ent->damage);
589		}
590	}
591}
592#endif
593
594static void
595NVBlockHandler (BLOCKHANDLER_ARGS_DECL)
596{
597	SCREEN_PTR(arg);
598	ScrnInfoPtr pScrn   = xf86ScreenToScrn(pScreen);
599	NVPtr pNv = NVPTR(pScrn);
600
601	pScreen->BlockHandler = pNv->BlockHandler;
602	(*pScreen->BlockHandler) (BLOCKHANDLER_ARGS);
603	pScreen->BlockHandler = NVBlockHandler;
604
605#ifdef NOUVEAU_PIXMAP_SHARING
606	nouveau_dirty_update(pScreen);
607#endif
608
609	NVFlushCallback(NULL, pScrn, NULL);
610
611	if (pNv->VideoTimerCallback)
612		(*pNv->VideoTimerCallback)(pScrn, currentTime.milliseconds);
613}
614
615static Bool
616NVCreateScreenResources(ScreenPtr pScreen)
617{
618	ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
619	NVPtr pNv = NVPTR(pScrn);
620
621	pScreen->CreateScreenResources = pNv->CreateScreenResources;
622	if (!(*pScreen->CreateScreenResources)(pScreen))
623		return FALSE;
624	pScreen->CreateScreenResources = NVCreateScreenResources;
625
626	drmmode_fbcon_copy(pScreen);
627	if (!NVEnterVT(VT_FUNC_ARGS(0)))
628		return FALSE;
629
630	if (pNv->AccelMethod == EXA) {
631		PixmapPtr ppix = pScreen->GetScreenPixmap(pScreen);
632		nouveau_bo_ref(pNv->scanout, &nouveau_pixmap(ppix)->bo);
633	}
634
635	return TRUE;
636}
637
638/*
639 * This is called at the end of each server generation.  It restores the
640 * original (text) mode.  It should also unmap the video memory, and free
641 * any per-generation data allocated by the driver.  It should finish
642 * by unwrapping and calling the saved CloseScreen function.
643 */
644
645/* Mandatory */
646static Bool
647NVCloseScreen(CLOSE_SCREEN_ARGS_DECL)
648{
649	ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
650	NVPtr pNv = NVPTR(pScrn);
651
652	if (XF86_CRTC_CONFIG_PTR(pScrn)->num_crtc)
653		drmmode_screen_fini(pScreen);
654
655	nouveau_present_fini(pScreen);
656	nouveau_dri2_fini(pScreen);
657	nouveau_sync_fini(pScreen);
658	nouveau_copy_fini(pScreen);
659
660	if (pScrn->vtSema) {
661		NVLeaveVT(VT_FUNC_ARGS(0));
662		pScrn->vtSema = FALSE;
663	}
664
665	NVTakedownVideo(pScrn);
666	NVAccelCommonFini(pScrn);
667	NVUnmapMem(pScrn);
668
669	xf86_cursors_fini(pScreen);
670
671	DeleteCallback(&FlushCallback, NVFlushCallback, pScrn);
672
673	if (pNv->ShadowPtr) {
674		free(pNv->ShadowPtr);
675		pNv->ShadowPtr = NULL;
676	}
677	if (pNv->overlayAdaptor) {
678		free(pNv->overlayAdaptor);
679		pNv->overlayAdaptor = NULL;
680	}
681	if (pNv->blitAdaptor) {
682		free(pNv->blitAdaptor);
683		pNv->blitAdaptor = NULL;
684	}
685	if (pNv->textureAdaptor[0]) {
686		free(pNv->textureAdaptor[0]);
687		pNv->textureAdaptor[0] = NULL;
688	}
689	if (pNv->textureAdaptor[1]) {
690		free(pNv->textureAdaptor[1]);
691		pNv->textureAdaptor[1] = NULL;
692	}
693	if (pNv->EXADriverPtr) {
694		exaDriverFini(pScreen);
695		free(pNv->EXADriverPtr);
696		pNv->EXADriverPtr = NULL;
697	}
698
699	pScrn->vtSema = FALSE;
700	pScreen->CloseScreen = pNv->CloseScreen;
701	pScreen->BlockHandler = pNv->BlockHandler;
702	return (*pScreen->CloseScreen)(CLOSE_SCREEN_ARGS);
703}
704
705/* Free up any persistent data structures */
706
707/* Optional */
708static void
709NVFreeScreen(FREE_SCREEN_ARGS_DECL)
710{
711	/*
712	 * This only gets called when a screen is being deleted.  It does not
713	 * get called routinely at the end of a server generation.
714	 */
715	SCRN_INFO_PTR(arg);
716	NVPtr pNv = NVPTR(pScrn);
717
718	if (!pNv)
719		return;
720
721	NVCloseDRM(pScrn);
722
723	free(pScrn->driverPrivate);
724	pScrn->driverPrivate = NULL;
725}
726
727#define NVPreInitFail(fmt, args...) do {                                    \
728	xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "%d: "fmt, __LINE__, ##args); \
729	NVFreeScreen(FREE_SCREEN_ARGS(pScrn));			\
730	return FALSE;                                                       \
731} while(0)
732
733static void
734NVCloseDRM(ScrnInfoPtr pScrn)
735{
736	NVPtr pNv = NVPTR(pScrn);
737
738	drmFree(pNv->drm_device_name);
739	nouveau_client_del(&pNv->client);
740	nouveau_device_del(&pNv->dev);
741	free(pNv->render_node);
742}
743
744static void
745nouveau_setup_capabilities(ScrnInfoPtr pScrn)
746{
747#ifdef NOUVEAU_PIXMAP_SHARING
748	NVPtr pNv = NVPTR(pScrn);
749	xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
750	uint64_t value;
751	int ret;
752
753	pScrn->capabilities = 0;
754	ret = drmGetCap(pNv->dev->fd, DRM_CAP_PRIME, &value);
755	if (ret == 0) {
756		if (value & DRM_PRIME_CAP_EXPORT)
757			pScrn->capabilities |= RR_Capability_SourceOutput;
758		if (value & DRM_PRIME_CAP_IMPORT) {
759			pScrn->capabilities |= RR_Capability_SourceOffload;
760			if (xf86_config->num_crtc)
761				pScrn->capabilities |= RR_Capability_SinkOutput;
762		}
763	}
764#endif
765}
766
767NVEntPtr NVEntPriv(ScrnInfoPtr pScrn)
768{
769	DevUnion     *pPriv;
770	NVPtr  pNv   = NVPTR(pScrn);
771	pPriv = xf86GetEntityPrivate(pNv->pEnt->index,
772				     getNVEntityIndex());
773	return pPriv->ptr;
774}
775
776static Bool NVOpenDRMMaster(ScrnInfoPtr pScrn)
777{
778	NVPtr pNv = NVPTR(pScrn);
779	NVEntPtr pNVEnt = NVEntPriv(pScrn);
780	drmSetVersion sv;
781	int err;
782	int ret;
783
784	if (pNVEnt->fd) {
785		xf86DrvMsg(pScrn->scrnIndex, X_INFO,
786			   " reusing fd for second head\n");
787		ret = nouveau_device_wrap(pNVEnt->fd, 0, &pNv->dev);
788		if (ret) {
789			xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
790				"[drm] error creating device\n");
791			return FALSE;
792		}
793		return TRUE;
794	}
795
796	pNv->dev = NVOpenNouveauDevice(pNv->PciInfo, pNVEnt->platform_dev,
797				       pScrn->scrnIndex, FALSE);
798	if (!pNv->dev)
799		return FALSE;
800
801	sv.drm_di_major = 1;
802	sv.drm_di_minor = 1;
803	sv.drm_dd_major = -1;
804	sv.drm_dd_minor = -1;
805	err = drmSetInterfaceVersion(pNv->dev->fd, &sv);
806	if (err != 0) {
807		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
808			   "[drm] failed to set drm interface version.\n");
809		nouveau_device_del(&pNv->dev);
810		return FALSE;
811	}
812	pNVEnt->fd = pNv->dev->fd;
813	return TRUE;
814}
815
816static Bool
817NVPreInitDRM(ScrnInfoPtr pScrn)
818{
819	NVPtr pNv = NVPTR(pScrn);
820	int ret;
821
822	if (!xf86LoadSubModule(pScrn, "dri2"))
823		return FALSE;
824
825	/* Load the kernel module, and open the DRM */
826	ret = NVOpenDRMMaster(pScrn);
827	if (!ret) {
828		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
829			   "[drm] error opening the drm\n");
830		return FALSE;
831	}
832
833	ret = nouveau_client_new(pNv->dev, &pNv->client);
834	if (ret)
835		return FALSE;
836
837	pNv->drm_device_name = drmGetDeviceNameFromFd(pNv->dev->fd);
838
839	return TRUE;
840}
841
842/* Mandatory */
843Bool
844NVPreInit(ScrnInfoPtr pScrn, int flags)
845{
846	struct nouveau_device *dev;
847	NVPtr pNv;
848	MessageType from;
849	const char *reason, *string;
850	uint64_t v;
851	int ret;
852	int defaultDepth = 0;
853
854	if (flags & PROBE_DETECT) {
855		EntityInfoPtr pEnt = xf86GetEntityInfo(pScrn->entityList[0]);
856
857		if (!pEnt)
858			return FALSE;
859
860		free(pEnt);
861
862		return TRUE;
863	}
864
865	/*
866	 * Note: This function is only called once at server startup, and
867	 * not at the start of each server generation.  This means that
868	 * only things that are persistent across server generations can
869	 * be initialised here.  xf86Screens[] is (pScrn is a pointer to one
870	 * of these).  Privates allocated using xf86AllocateScrnInfoPrivateIndex()
871	 * are too, and should be used for data that must persist across
872	 * server generations.
873	 *
874	 * Per-generation data should be allocated with
875	 * AllocateScreenPrivateIndex() from the ScreenInit() function.
876	 */
877
878	/* Check the number of entities, and fail if it isn't one. */
879	if (pScrn->numEntities != 1)
880		return FALSE;
881
882	/* Allocate the NVRec driverPrivate */
883	if (!(pScrn->driverPrivate = xnfcalloc(1, sizeof(NVRec))))
884		return FALSE;
885	pNv = NVPTR(pScrn);
886
887	/* Get the entity, and make sure it is PCI. */
888	pNv->pEnt = xf86GetEntityInfo(pScrn->entityList[0]);
889	if (pNv->pEnt->location.type != BUS_PCI
890#ifdef XSERVER_PLATFORM_BUS
891		&& pNv->pEnt->location.type != BUS_PLATFORM
892#endif
893		)
894		return FALSE;
895
896	if (xf86IsEntityShared(pScrn->entityList[0])) {
897		if(!xf86IsPrimInitDone(pScrn->entityList[0])) {
898			pNv->Primary = TRUE;
899			xf86SetPrimInitDone(pScrn->entityList[0]);
900		} else {
901			pNv->Secondary = TRUE;
902		}
903        }
904
905	/* Find the PCI info for this screen */
906	pNv->PciInfo = xf86GetPciInfoForEntity(pNv->pEnt->index);
907
908	/* Initialise the kernel module */
909	if (!NVPreInitDRM(pScrn))
910		NVPreInitFail("\n");
911	dev = pNv->dev;
912
913	pScrn->chipset = malloc(sizeof(char) * 25);
914	sprintf((char *)pScrn->chipset, "NVIDIA NV%02X", dev->chipset);
915	xf86DrvMsg(pScrn->scrnIndex, X_PROBED, "Chipset: \"%s\"\n", pScrn->chipset);
916
917	switch (dev->chipset & ~0xf) {
918	case 0x00:
919		pNv->Architecture = NV_ARCH_04;
920		break;
921	case 0x10:
922		pNv->Architecture = NV_ARCH_10;
923		break;
924	case 0x20:
925		pNv->Architecture = NV_ARCH_20;
926		break;
927	case 0x30:
928		pNv->Architecture = NV_ARCH_30;
929		break;
930	case 0x40:
931	case 0x60:
932		pNv->Architecture = NV_ARCH_40;
933		break;
934	case 0x50:
935	case 0x80:
936	case 0x90:
937	case 0xa0:
938		pNv->Architecture = NV_TESLA;
939		break;
940	case 0xc0:
941	case 0xd0:
942		pNv->Architecture = NV_FERMI;
943		break;
944	case 0xe0:
945	case 0xf0:
946	case 0x100:
947		pNv->Architecture = NV_KEPLER;
948		break;
949	case 0x110:
950	case 0x120:
951		pNv->Architecture = NV_MAXWELL;
952		break;
953	case 0x130:
954		pNv->Architecture = NV_PASCAL;
955		break;
956	default:
957		return FALSE;
958	}
959
960	/* Set pScrn->monitor */
961	pScrn->monitor = pScrn->confScreen->monitor;
962
963	/*
964	 * The first thing we should figure out is the depth, bpp, etc.
965	 */
966
967	if (dev->vram_size <= 16 * 1024 * 1024)
968		defaultDepth = 16;
969	if (!xf86SetDepthBpp(pScrn, defaultDepth, 0, 0, Support32bppFb)) {
970		NVPreInitFail("\n");
971	} else {
972		/* Check that the returned depth is one we support */
973		switch (pScrn->depth) {
974		case 16:
975		case 24:
976			/* OK */
977			break;
978		case 30:
979			/* OK on NV50 KMS */
980			if (pNv->Architecture < NV_TESLA)
981				NVPreInitFail("Depth 30 supported on G80+ only\n");
982			break;
983		case 15: /* 15 may get done one day, so leave any code for it in place */
984		default:
985			NVPreInitFail("Given depth (%d) is not supported by this driver\n",
986				pScrn->depth);
987		}
988	}
989	xf86PrintDepthBpp(pScrn);
990
991	/*
992	 * This must happen after pScrn->display has been set because
993	 * xf86SetWeight references it.
994	 */
995	rgb rgbzeros = {0, 0, 0};
996
997	if (pScrn->depth == 30) {
998		rgb rgbmask;
999
1000		rgbmask.red   = 0x000003ff;
1001		rgbmask.green = 0x000ffc00;
1002		rgbmask.blue  = 0x3ff00000;
1003		if (!xf86SetWeight(pScrn, rgbzeros, rgbmask))
1004			NVPreInitFail("\n");
1005
1006		/* xf86SetWeight() seems to think ffs(1) == 0... */
1007		pScrn->offset.red--;
1008		pScrn->offset.green--;
1009		pScrn->offset.blue--;
1010	} else {
1011		if (!xf86SetWeight(pScrn, rgbzeros, rgbzeros))
1012			NVPreInitFail("\n");
1013	}
1014
1015	if (!xf86SetDefaultVisual(pScrn, -1))
1016		NVPreInitFail("\n");
1017
1018	/* We don't support DirectColor */
1019	if (pScrn->defaultVisual != TrueColor) {
1020		NVPreInitFail("Given default visual (%s) is not supported at depth %d\n",
1021			      xf86GetVisualName(pScrn->defaultVisual), pScrn->depth);
1022	}
1023
1024	/* We use a programmable clock */
1025	pScrn->progClock = TRUE;
1026
1027	/* Collect all of the relevant option flags (fill in pScrn->options) */
1028	xf86CollectOptions(pScrn, NULL);
1029
1030	/* Process the options */
1031	if (!(pNv->Options = malloc(sizeof(NVOptions))))
1032		return FALSE;
1033	memcpy(pNv->Options, NVOptions, sizeof(NVOptions));
1034	xf86ProcessOptions(pScrn->scrnIndex, pScrn->options, pNv->Options);
1035
1036	from = X_DEFAULT;
1037
1038	pNv->HWCursor = TRUE;
1039	/*
1040	 * The preferred method is to use the "hw cursor" option as a tri-state
1041	 * option, with the default set above.
1042	 */
1043	if (xf86GetOptValBool(pNv->Options, OPTION_HW_CURSOR, &pNv->HWCursor)) {
1044		from = X_CONFIG;
1045	}
1046	/* For compatibility, accept this too (as an override) */
1047	if (xf86ReturnOptValBool(pNv->Options, OPTION_SW_CURSOR, FALSE)) {
1048		from = X_CONFIG;
1049		pNv->HWCursor = FALSE;
1050	}
1051	xf86DrvMsg(pScrn->scrnIndex, from, "Using %s cursor\n",
1052		pNv->HWCursor ? "HW" : "SW");
1053
1054	string = xf86GetOptValString(pNv->Options, OPTION_ACCELMETHOD);
1055	if (string) {
1056		if      (!strcmp(string,   "none")) pNv->AccelMethod = NONE;
1057		else if (!strcmp(string,    "exa")) pNv->AccelMethod = EXA;
1058		else {
1059			xf86DrvMsg(pScrn->scrnIndex, X_CONFIG,
1060				   "Invalid AccelMethod specified\n");
1061		}
1062	}
1063
1064	if (pNv->AccelMethod == UNKNOWN) {
1065		pNv->AccelMethod = EXA;
1066	}
1067
1068	if (xf86ReturnOptValBool(pNv->Options, OPTION_NOACCEL, FALSE)) {
1069		pNv->AccelMethod = NONE;
1070		xf86DrvMsg(pScrn->scrnIndex, X_CONFIG, "Acceleration disabled\n");
1071	}
1072
1073	if (xf86ReturnOptValBool(pNv->Options, OPTION_SHADOW_FB, FALSE)) {
1074		pNv->ShadowFB = TRUE;
1075		pNv->AccelMethod = NONE;
1076		xf86DrvMsg(pScrn->scrnIndex, X_CONFIG,
1077			"Using \"Shadow Framebuffer\" - acceleration disabled\n");
1078	}
1079
1080	if (pNv->AccelMethod > NONE) {
1081#if 0
1082		if (pNv->Architecture >= NV_TESLA)
1083			pNv->wfb_enabled = xf86ReturnOptValBool(
1084				pNv->Options, OPTION_WFB, FALSE);
1085#endif
1086
1087		if (pNv->Architecture >= NV_ARCH_10)
1088			pNv->tiled_scanout = TRUE;
1089	}
1090
1091	pNv->ce_enabled =
1092		xf86ReturnOptValBool(pNv->Options, OPTION_ASYNC_COPY, FALSE);
1093
1094	/* Define maximum allowed level of DRI implementation to use.
1095	 * We default to DRI2 on EXA for now, as DRI3 still has some
1096	 * problems.
1097	 */
1098	pNv->max_dri_level = 2;
1099	from = X_DEFAULT;
1100
1101	if (xf86GetOptValInteger(pNv->Options, OPTION_DRI,
1102				 &pNv->max_dri_level)) {
1103		from = X_CONFIG;
1104		if (pNv->max_dri_level < 2)
1105			pNv->max_dri_level = 2;
1106		if (pNv->max_dri_level > 3)
1107			pNv->max_dri_level = 3;
1108	}
1109	xf86DrvMsg(pScrn->scrnIndex, from, "Allowed maximum DRI level %i.\n",
1110		   pNv->max_dri_level);
1111
1112	if (pNv->AccelMethod > NONE && pNv->dev->chipset >= 0x11) {
1113		from = X_DEFAULT;
1114		pNv->glx_vblank = TRUE;
1115		if (xf86GetOptValBool(pNv->Options, OPTION_GLX_VBLANK,
1116				      &pNv->glx_vblank))
1117			from = X_CONFIG;
1118
1119		xf86DrvMsg(pScrn->scrnIndex, from, "GLX sync to VBlank %s.\n",
1120			   pNv->glx_vblank ? "enabled" : "disabled");
1121	}
1122
1123#ifdef NOUVEAU_GETPARAM_HAS_PAGEFLIP
1124	reason = ": no kernel support";
1125	from = X_DEFAULT;
1126
1127	ret = nouveau_getparam(pNv->dev, NOUVEAU_GETPARAM_HAS_PAGEFLIP, &v);
1128	if (ret == 0 && v == 1) {
1129		pNv->has_pageflip = TRUE;
1130		if (xf86GetOptValBool(pNv->Options, OPTION_PAGE_FLIP, &pNv->has_pageflip))
1131			from = X_CONFIG;
1132		reason = "";
1133	}
1134#else
1135	reason = ": not available at build time";
1136#endif
1137
1138	xf86DrvMsg(pScrn->scrnIndex, from, "Page flipping %sabled%s\n",
1139		   pNv->has_pageflip ? "en" : "dis", reason);
1140
1141	if(xf86GetOptValInteger(pNv->Options, OPTION_VIDEO_KEY, &(pNv->videoKey))) {
1142		xf86DrvMsg(pScrn->scrnIndex, X_CONFIG, "video key set to 0x%x\n",
1143					pNv->videoKey);
1144	} else {
1145		pNv->videoKey =  (1 << pScrn->offset.red) |
1146					(1 << pScrn->offset.green) |
1147		(((pScrn->mask.blue >> pScrn->offset.blue) - 1) << pScrn->offset.blue);
1148	}
1149
1150	/* Limit to max 2 pending swaps - we can't handle more than triple-buffering: */
1151	pNv->max_swap_limit = 2;
1152
1153	if(xf86GetOptValInteger(pNv->Options, OPTION_SWAP_LIMIT, &(pNv->swap_limit))) {
1154		if (pNv->swap_limit < 1)
1155			pNv->swap_limit = 1;
1156
1157		if (pNv->swap_limit > pNv->max_swap_limit)
1158			pNv->swap_limit = pNv->max_swap_limit;
1159
1160		reason = "";
1161		from = X_CONFIG;
1162
1163		if ((DRI2INFOREC_VERSION < 6) && (pNv->swap_limit > 1)) {
1164			/* No swap limit api in server. A value > 1 requires use
1165			 * of problematic hacks.
1166			 */
1167			from = X_WARNING;
1168			reason = ": Caution: Use of this swap limit > 1 violates OML_sync_control spec on this X-Server!\n";
1169		}
1170	} else {
1171		/* Always default to double-buffering, because it avoids artifacts like
1172		 * unthrottled rendering of non-fullscreen clients under desktop composition.
1173		 */
1174		pNv->swap_limit = 1;
1175		reason = "";
1176		from = X_DEFAULT;
1177	}
1178
1179	xf86DrvMsg(pScrn->scrnIndex, from, "Swap limit set to %d [Max allowed %d]%s\n",
1180		   pNv->swap_limit, pNv->max_swap_limit, reason);
1181
1182	/* Does kernel do the sync of pageflips to vblank? */
1183	pNv->has_async_pageflip = FALSE;
1184#ifdef DRM_CAP_ASYNC_PAGE_FLIP
1185	ret = drmGetCap(pNv->dev->fd, DRM_CAP_ASYNC_PAGE_FLIP, &v);
1186	if (ret == 0 && v == 1) {
1187		pNv->has_async_pageflip = TRUE;
1188	}
1189	xf86DrvMsg(pScrn->scrnIndex, X_DEFAULT, "Page flipping synced to vblank by %s.\n",
1190			   pNv->has_async_pageflip ? "kernel" : "ddx");
1191#endif
1192
1193	ret = drmmode_pre_init(pScrn, pNv->dev->fd, pScrn->bitsPerPixel >> 3);
1194	if (ret == FALSE)
1195		NVPreInitFail("Kernel modesetting failed to initialize\n");
1196
1197	/*
1198	 * If the driver can do gamma correction, it should call xf86SetGamma()
1199	 * here.
1200	 */
1201	Gamma gammazeros = {0.0, 0.0, 0.0};
1202
1203	if (!xf86SetGamma(pScrn, gammazeros))
1204		NVPreInitFail("\n");
1205
1206#ifdef NOUVEAU_PIXMAP_SHARING
1207	/*
1208	 * The driver will not work as gpu screen without acceleration enabled.
1209	 * To support this usecase modesetting ddx can be used instead.
1210	 */
1211	if (pNv->AccelMethod <= NONE || pNv->ShadowFB) {
1212		/*
1213		 * Optimus mode requires acceleration enabled.
1214		 * So if no mode is found, or the screen is created
1215		 * as a gpu screen the pre init should fail.
1216		 */
1217		if (pScrn->is_gpu || !pScrn->modes)
1218			return FALSE;
1219	}
1220
1221#else
1222	/* No usable mode, no optimus config possible */
1223	if (!pScrn->modes)
1224		return FALSE;
1225#endif
1226
1227	nouveau_setup_capabilities(pScrn);
1228
1229	if (!pScrn->modes) {
1230		DisplayModePtr m;
1231
1232		m = xf86CVTMode(pScrn->display->virtualX,
1233				    pScrn->display->virtualY,
1234				    60, 0, 0);
1235		xf86SetModeDefaultName(m);
1236		pScrn->modes = xf86ModesAdd(pScrn->modes, m);
1237	}
1238
1239	/* Set the current mode to the first in the list */
1240	pScrn->currentMode = pScrn->modes;
1241
1242	/* Print the list of modes being used */
1243	xf86PrintModes(pScrn);
1244
1245	/* Set display resolution */
1246	xf86SetDpi(pScrn, 0, 0);
1247
1248#if 0
1249	if (pNv->wfb_enabled) {
1250		if (xf86LoadSubModule(pScrn, "wfb") == NULL)
1251			NVPreInitFail("\n");
1252	}
1253#endif
1254
1255	if (xf86LoadSubModule(pScrn, "fb") == NULL)
1256		NVPreInitFail("\n");
1257
1258	/* Load shadowfb */
1259	if (!xf86LoadSubModule(pScrn, "shadowfb"))
1260		NVPreInitFail("\n");
1261
1262	return TRUE;
1263}
1264
1265
1266static Bool
1267NVMapMem(ScrnInfoPtr pScrn)
1268{
1269	NVPtr pNv = NVPTR(pScrn);
1270	int ret, pitch;
1271
1272	ret = nouveau_allocate_surface(pScrn, pScrn->virtualX, pScrn->virtualY,
1273				       pScrn->bitsPerPixel,
1274				       NOUVEAU_CREATE_PIXMAP_SCANOUT,
1275				       &pitch, &pNv->scanout);
1276	if (!ret) {
1277		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1278			   "Error allocating scanout buffer: %d\n", ret);
1279		return FALSE;
1280	}
1281
1282	pScrn->displayWidth = pitch / (pScrn->bitsPerPixel / 8);
1283	return TRUE;
1284}
1285
1286/*
1287 * Unmap the framebuffer and offscreen memory.
1288 */
1289
1290static Bool
1291NVUnmapMem(ScrnInfoPtr pScrn)
1292{
1293	NVPtr pNv = NVPTR(pScrn);
1294
1295	drmmode_remove_fb(pScrn);
1296
1297	nouveau_bo_ref(NULL, &pNv->transfer);
1298	nouveau_bo_ref(NULL, &pNv->scanout);
1299	return TRUE;
1300}
1301
1302static void
1303NVLoadPalette(ScrnInfoPtr pScrn, int numColors, int *indices,
1304	      LOCO * colors, VisualPtr pVisual)
1305{
1306	xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
1307	int c;
1308	int i, j, index;
1309	CARD16 lut_r[256], lut_g[256], lut_b[256];
1310
1311	for (c = 0; c < xf86_config->num_crtc; c++) {
1312		xf86CrtcPtr crtc = xf86_config->crtc[c];
1313
1314		/* code borrowed from intel driver */
1315		switch (pScrn->depth) {
1316		case 15:
1317			for (i = 0; i < numColors; i++) {
1318				index = indices[i];
1319				for (j = 0; j < 8; j++) {
1320					lut_r[index * 8 + j] = colors[index].red << 8;
1321					lut_g[index * 8 + j] = colors[index].green << 8;
1322					lut_b[index * 8 + j] = colors[index].blue << 8;
1323				}
1324			}
1325			break;
1326
1327		case 16:
1328			for (i = 0; i < numColors; i++) {
1329				index = indices[i];
1330
1331				if (i <= 31) {
1332					for (j = 0; j < 8; j++) {
1333						lut_r[index * 8 + j] = colors[index].red << 8;
1334						lut_b[index * 8 + j] = colors[index].blue << 8;
1335					}
1336				}
1337
1338				for (j = 0; j < 4; j++) {
1339					lut_g[index * 4 + j] = colors[index].green << 8;
1340				}
1341			}
1342			break;
1343
1344		default:
1345			for (i = 0; i < numColors; i++) {
1346				index = indices[i];
1347				lut_r[index] = colors[index].red << 8;
1348				lut_g[index] = colors[index].green << 8;
1349				lut_b[index] = colors[index].blue << 8;
1350			}
1351			break;
1352		}
1353
1354		if (crtc->randr_crtc)
1355			/* Make the change through RandR */
1356			RRCrtcGammaSet(crtc->randr_crtc, lut_r, lut_g, lut_b);
1357	}
1358}
1359
1360/* Mandatory */
1361
1362/* This gets called at the start of each server generation */
1363static Bool
1364NVScreenInit(SCREEN_INIT_ARGS_DECL)
1365{
1366	ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
1367	NVPtr pNv = NVPTR(pScrn);
1368	int ret;
1369	VisualPtr visual;
1370	unsigned char *FBStart;
1371	int displayWidth;
1372
1373	if (pNv->AccelMethod == EXA) {
1374		if (!NVAccelCommonInit(pScrn)) {
1375			xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1376				   "Error initialising acceleration.  "
1377				   "Falling back to NoAccel\n");
1378			pNv->AccelMethod = NONE;
1379			pNv->ShadowFB = TRUE;
1380#if 0
1381			pNv->wfb_enabled = FALSE;
1382#endif
1383			pNv->tiled_scanout = FALSE;
1384			pScrn->capabilities &= ~(RR_Capability_SourceOutput |
1385						 RR_Capability_SourceOffload |
1386						 RR_Capability_SinkOutput);
1387			pScrn->displayWidth = nv_pitch_align(pNv,
1388							     pScrn->virtualX,
1389							     pScrn->depth);
1390		}
1391	}
1392
1393	nouveau_copy_init(pScreen);
1394
1395	/* Allocate and map memory areas we need */
1396	if (!NVMapMem(pScrn))
1397		return FALSE;
1398
1399	xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
1400	int i;
1401
1402	/* need to point to new screen on server regeneration */
1403	for (i = 0; i < xf86_config->num_crtc; i++)
1404		xf86_config->crtc[i]->scrn = pScrn;
1405	for (i = 0; i < xf86_config->num_output; i++)
1406		xf86_config->output[i]->scrn = pScrn;
1407
1408	/*
1409	 * The next step is to setup the screen's visuals, and initialise the
1410	 * framebuffer code.  In cases where the framebuffer's default
1411	 * choices for things like visual layouts and bits per RGB are OK,
1412	 * this may be as simple as calling the framebuffer's ScreenInit()
1413	 * function.  If not, the visuals will need to be setup before calling
1414	 * a fb ScreenInit() function and fixed up after.
1415	 *
1416	 * For most PC hardware at depths >= 8, the defaults that fb uses
1417	 * are not appropriate.  In this driver, we fixup the visuals after.
1418	 */
1419
1420	/*
1421	 * Reset the visual list.
1422	 */
1423	miClearVisualTypes();
1424
1425	/* Setup the visuals we support. */
1426	if (!miSetVisualTypes(pScrn->depth,
1427			      miGetDefaultVisualMask(pScrn->depth),
1428			      pScrn->rgbBits, pScrn->defaultVisual))
1429		return FALSE;
1430
1431	if (!miSetPixmapDepths ())
1432		return FALSE;
1433
1434	/*
1435	 * Call the framebuffer layer's ScreenInit function, and fill in other
1436	 * pScreen fields.
1437	 */
1438
1439	if (pNv->ShadowFB) {
1440		pNv->ShadowPitch = BitmapBytePad(pScrn->bitsPerPixel * pScrn->virtualX);
1441		pNv->ShadowPtr = malloc(pNv->ShadowPitch * pScrn->virtualY);
1442		displayWidth = pNv->ShadowPitch / (pScrn->bitsPerPixel >> 3);
1443		FBStart = pNv->ShadowPtr;
1444	} else
1445	if (pNv->AccelMethod <= NONE) {
1446		pNv->ShadowPtr = NULL;
1447		displayWidth = pScrn->displayWidth;
1448		nouveau_bo_map(pNv->scanout, NOUVEAU_BO_RDWR, pNv->client);
1449		FBStart = pNv->scanout->map;
1450	} else {
1451		pNv->ShadowPtr = NULL;
1452		displayWidth = pScrn->displayWidth;
1453		FBStart = NULL;
1454	}
1455
1456	switch (pScrn->bitsPerPixel) {
1457	case 16:
1458	case 32:
1459#if 0
1460	if (pNv->wfb_enabled) {
1461		ret = wfbScreenInit(pScreen, FBStart, pScrn->virtualX,
1462				    pScrn->virtualY, pScrn->xDpi, pScrn->yDpi,
1463				    displayWidth, pScrn->bitsPerPixel,
1464				    nouveau_wfb_setup_wrap,
1465				    nouveau_wfb_finish_wrap);
1466	} else {
1467#endif
1468		ret = fbScreenInit(pScreen, FBStart, pScrn->virtualX,
1469				   pScrn->virtualY, pScrn->xDpi, pScrn->yDpi,
1470				   displayWidth, pScrn->bitsPerPixel);
1471#if 0
1472	}
1473#endif
1474		break;
1475	default:
1476		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1477			   "Internal error: invalid bpp (%d) in NVScreenInit\n",
1478			   pScrn->bitsPerPixel);
1479		ret = FALSE;
1480		break;
1481	}
1482	if (!ret)
1483		return FALSE;
1484
1485	/* Fixup RGB ordering */
1486	visual = pScreen->visuals + pScreen->numVisuals;
1487	while (--visual >= pScreen->visuals) {
1488		if ((visual->class | DynamicClass) == DirectColor) {
1489			visual->offsetRed = pScrn->offset.red;
1490			visual->offsetGreen = pScrn->offset.green;
1491			visual->offsetBlue = pScrn->offset.blue;
1492			visual->redMask = pScrn->mask.red;
1493			visual->greenMask = pScrn->mask.green;
1494			visual->blueMask = pScrn->mask.blue;
1495		}
1496	}
1497
1498#if 0
1499	if (pNv->wfb_enabled)
1500		wfbPictureInit (pScreen, 0, 0);
1501	else
1502#endif
1503		fbPictureInit (pScreen, 0, 0);
1504
1505	xf86SetBlackWhitePixels(pScreen);
1506
1507	if (pNv->AccelMethod == EXA && nouveau_present_init(pScreen))
1508		xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1509			   "Hardware support for Present enabled\n");
1510	else
1511		xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1512			   "Hardware support for Present disabled\n");
1513
1514	nouveau_sync_init(pScreen);
1515	nouveau_dri2_init(pScreen);
1516	if (pNv->AccelMethod == EXA) {
1517		if (pNv->max_dri_level >= 3 &&
1518		    !nouveau_dri3_screen_init(pScreen))
1519			return FALSE;
1520
1521		if (!nouveau_exa_init(pScreen))
1522			return FALSE;
1523	}
1524
1525	xf86SetBackingStore(pScreen);
1526	xf86SetSilkenMouse(pScreen);
1527
1528	/*
1529	 * Initialize software cursor.
1530	 * Must precede creation of the default colormap.
1531	 */
1532	miDCInitialize(pScreen, xf86GetPointerScreenFuncs());
1533
1534	/*
1535	 * Initialize HW cursor layer.
1536	 * Must follow software cursor initialization.
1537	 */
1538	if (xf86_config->num_crtc && pNv->HWCursor) {
1539		ret = drmmode_cursor_init(pScreen);
1540		if (ret != TRUE) {
1541			xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1542				   "Hardware cursor initialization failed\n");
1543			pNv->HWCursor = FALSE;
1544		}
1545	}
1546
1547	if (pNv->ShadowFB)
1548		ShadowFBInit(pScreen, NVRefreshArea);
1549
1550	pScrn->fbOffset = 0;
1551
1552	NVInitVideo(pScreen);
1553
1554	/* Wrap the block handler here, if we do it after the EnterVT we
1555	 * can end up in the unfortunate case where we've wrapped the
1556	 * xf86RotateBlockHandler which sometimes is not expecting to
1557	 * be in the wrap chain and calls a NULL pointer...
1558	 */
1559	pNv->BlockHandler = pScreen->BlockHandler;
1560	pScreen->BlockHandler = NVBlockHandler;
1561
1562	if (!AddCallback(&FlushCallback, NVFlushCallback, pScrn))
1563		return FALSE;
1564
1565	pScrn->vtSema = TRUE;
1566	pScrn->pScreen = pScreen;
1567
1568	xf86DPMSInit(pScreen, xf86DPMSSet, 0);
1569
1570	/* Wrap the current CloseScreen function */
1571	pScreen->SaveScreen = NVSaveScreen;
1572	pNv->CloseScreen = pScreen->CloseScreen;
1573	pScreen->CloseScreen = NVCloseScreen;
1574	pNv->CreateScreenResources = pScreen->CreateScreenResources;
1575	pScreen->CreateScreenResources = NVCreateScreenResources;
1576
1577#ifdef NOUVEAU_PIXMAP_SHARING
1578	pScreen->StartPixmapTracking = PixmapStartDirtyTracking;
1579	pScreen->StopPixmapTracking = PixmapStopDirtyTracking;
1580#endif
1581
1582	if (!xf86CrtcScreenInit(pScreen))
1583		return FALSE;
1584
1585	/* Initialise default colourmap */
1586	if (!miCreateDefColormap(pScreen))
1587		return FALSE;
1588
1589	/*
1590	 * Initialize colormap layer.
1591	 * Must follow initialization of the default colormap.
1592	 * X-Server < 1.20 mishandles > 256 slots / > 8 bpc color maps, so skip
1593	 * color map setup on old servers at > 8 bpc. Gamma luts still work.
1594	 */
1595	if (xf86_config->num_crtc && (pScrn->rgbBits <= 8 ||
1596	    XORG_VERSION_CURRENT >= XORG_VERSION_NUMERIC(1,20,0,0,0)) &&
1597	    !xf86HandleColormaps(pScreen, 1 << pScrn->rgbBits, pScrn->rgbBits,
1598				 NVLoadPalette, NULL, CMAP_PALETTED_TRUECOLOR))
1599		return FALSE;
1600
1601	/* Report any unused options (only for the first generation) */
1602	if (serverGeneration == 1)
1603		xf86ShowUnusedOptions(pScrn->scrnIndex, pScrn->options);
1604
1605	if (xf86_config->num_crtc)
1606		drmmode_screen_init(pScreen);
1607	else
1608		pNv->glx_vblank = FALSE;
1609	return TRUE;
1610}
1611
1612static Bool
1613NVSaveScreen(ScreenPtr pScreen, int mode)
1614{
1615	return TRUE;
1616}
1617
1618