nouveau_state.c 23.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*
 * Copyright 2005 Stephane Marchesin
 * Copyright 2008 Stuart Bennett
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

#include <linux/swab.h>
27
#include <linux/slab.h>
28 29 30 31 32
#include "drmP.h"
#include "drm.h"
#include "drm_sarea.h"
#include "drm_crtc_helper.h"
#include <linux/vgaarb.h>
33
#include <linux/vga_switcheroo.h>
34 35

#include "nouveau_drv.h"
36
#include <nouveau_drm.h>
37
#include "nouveau_fbcon.h"
38
#include <core/ramht.h>
39
#include "nouveau_pm.h"
40
#include "nv04_display.h"
41
#include "nv50_display.h"
42
#include <engine/fifo.h>
43
#include "nouveau_fence.h"
44
#include "nouveau_software.h"
45 46

static void nouveau_stub_takedown(struct drm_device *dev) {}
B
Ben Skeggs 已提交
47
static int nouveau_stub_init(struct drm_device *dev) { return 0; }
48 49 50 51 52 53 54 55

static int nouveau_init_engine_ptrs(struct drm_device *dev)
{
	struct drm_nouveau_private *dev_priv = dev->dev_private;
	struct nouveau_engine *engine = &dev_priv->engine;

	switch (dev_priv->chipset & 0xf0) {
	case 0x00:
56 57 58 59
		engine->display.early_init	= nv04_display_early_init;
		engine->display.late_takedown	= nv04_display_late_takedown;
		engine->display.create		= nv04_display_create;
		engine->display.destroy		= nv04_display_destroy;
60 61
		engine->display.init		= nv04_display_init;
		engine->display.fini		= nv04_display_fini;
62 63 64
		engine->pm.clocks_get		= nv04_pm_clocks_get;
		engine->pm.clocks_pre		= nv04_pm_clocks_pre;
		engine->pm.clocks_set		= nv04_pm_clocks_set;
65 66
		break;
	case 0x10:
67 68 69 70
		engine->display.early_init	= nv04_display_early_init;
		engine->display.late_takedown	= nv04_display_late_takedown;
		engine->display.create		= nv04_display_create;
		engine->display.destroy		= nv04_display_destroy;
71 72
		engine->display.init		= nv04_display_init;
		engine->display.fini		= nv04_display_fini;
73 74 75
		engine->pm.clocks_get		= nv04_pm_clocks_get;
		engine->pm.clocks_pre		= nv04_pm_clocks_pre;
		engine->pm.clocks_set		= nv04_pm_clocks_set;
76 77
		break;
	case 0x20:
78 79 80 81
		engine->display.early_init	= nv04_display_early_init;
		engine->display.late_takedown	= nv04_display_late_takedown;
		engine->display.create		= nv04_display_create;
		engine->display.destroy		= nv04_display_destroy;
82 83
		engine->display.init		= nv04_display_init;
		engine->display.fini		= nv04_display_fini;
84 85 86
		engine->pm.clocks_get		= nv04_pm_clocks_get;
		engine->pm.clocks_pre		= nv04_pm_clocks_pre;
		engine->pm.clocks_set		= nv04_pm_clocks_set;
87 88
		break;
	case 0x30:
89 90 91 92
		engine->display.early_init	= nv04_display_early_init;
		engine->display.late_takedown	= nv04_display_late_takedown;
		engine->display.create		= nv04_display_create;
		engine->display.destroy		= nv04_display_destroy;
93 94
		engine->display.init		= nv04_display_init;
		engine->display.fini		= nv04_display_fini;
95 96 97
		engine->pm.clocks_get		= nv04_pm_clocks_get;
		engine->pm.clocks_pre		= nv04_pm_clocks_pre;
		engine->pm.clocks_set		= nv04_pm_clocks_set;
98 99
		engine->pm.voltage_get		= nouveau_voltage_gpio_get;
		engine->pm.voltage_set		= nouveau_voltage_gpio_set;
100 101 102
		break;
	case 0x40:
	case 0x60:
103 104 105 106
		engine->display.early_init	= nv04_display_early_init;
		engine->display.late_takedown	= nv04_display_late_takedown;
		engine->display.create		= nv04_display_create;
		engine->display.destroy		= nv04_display_destroy;
107 108
		engine->display.init		= nv04_display_init;
		engine->display.fini		= nv04_display_fini;
109 110 111
		engine->pm.clocks_get		= nv40_pm_clocks_get;
		engine->pm.clocks_pre		= nv40_pm_clocks_pre;
		engine->pm.clocks_set		= nv40_pm_clocks_set;
112 113
		engine->pm.voltage_get		= nouveau_voltage_gpio_get;
		engine->pm.voltage_set		= nouveau_voltage_gpio_set;
114
		engine->pm.temp_get		= nv40_temp_get;
115 116
		engine->pm.pwm_get		= nv40_pm_pwm_get;
		engine->pm.pwm_set		= nv40_pm_pwm_set;
117 118 119 120
		break;
	case 0x50:
	case 0x80: /* gotta love NVIDIA's consistency.. */
	case 0x90:
121
	case 0xa0:
122 123 124 125
		engine->display.early_init	= nv50_display_early_init;
		engine->display.late_takedown	= nv50_display_late_takedown;
		engine->display.create		= nv50_display_create;
		engine->display.destroy		= nv50_display_destroy;
126 127
		engine->display.init		= nv50_display_init;
		engine->display.fini		= nv50_display_fini;
128
		switch (dev_priv->chipset) {
129 130 131 132 133 134 135
		case 0x84:
		case 0x86:
		case 0x92:
		case 0x94:
		case 0x96:
		case 0x98:
		case 0xa0:
136 137
		case 0xaa:
		case 0xac:
138
		case 0x50:
139 140 141
			engine->pm.clocks_get	= nv50_pm_clocks_get;
			engine->pm.clocks_pre	= nv50_pm_clocks_pre;
			engine->pm.clocks_set	= nv50_pm_clocks_set;
142
			break;
143
		default:
144 145 146
			engine->pm.clocks_get	= nva3_pm_clocks_get;
			engine->pm.clocks_pre	= nva3_pm_clocks_pre;
			engine->pm.clocks_set	= nva3_pm_clocks_set;
147
			break;
148
		}
149 150
		engine->pm.voltage_get		= nouveau_voltage_gpio_get;
		engine->pm.voltage_set		= nouveau_voltage_gpio_set;
151 152 153 154
		if (dev_priv->chipset >= 0x84)
			engine->pm.temp_get	= nv84_temp_get;
		else
			engine->pm.temp_get	= nv40_temp_get;
155 156
		engine->pm.pwm_get		= nv50_pm_pwm_get;
		engine->pm.pwm_set		= nv50_pm_pwm_set;
157
		break;
158
	case 0xc0:
159 160 161 162
		engine->display.early_init	= nv50_display_early_init;
		engine->display.late_takedown	= nv50_display_late_takedown;
		engine->display.create		= nv50_display_create;
		engine->display.destroy		= nv50_display_destroy;
163 164
		engine->display.init		= nv50_display_init;
		engine->display.fini		= nv50_display_fini;
165
		engine->pm.temp_get		= nv84_temp_get;
166
		engine->pm.clocks_get		= nvc0_pm_clocks_get;
167 168
		engine->pm.clocks_pre		= nvc0_pm_clocks_pre;
		engine->pm.clocks_set		= nvc0_pm_clocks_set;
B
Ben Skeggs 已提交
169
		engine->pm.voltage_get		= nouveau_voltage_gpio_get;
170
		engine->pm.voltage_set		= nouveau_voltage_gpio_set;
171 172
		engine->pm.pwm_get		= nv50_pm_pwm_get;
		engine->pm.pwm_set		= nv50_pm_pwm_set;
173
		break;
174 175 176
	case 0xd0:
		engine->display.early_init	= nouveau_stub_init;
		engine->display.late_takedown	= nouveau_stub_takedown;
177 178
		engine->display.create		= nvd0_display_create;
		engine->display.destroy		= nvd0_display_destroy;
179 180
		engine->display.init		= nvd0_display_init;
		engine->display.fini		= nvd0_display_fini;
181
		engine->pm.temp_get		= nv84_temp_get;
182
		engine->pm.clocks_get		= nvc0_pm_clocks_get;
183 184
		engine->pm.clocks_pre		= nvc0_pm_clocks_pre;
		engine->pm.clocks_set		= nvc0_pm_clocks_set;
185 186
		engine->pm.voltage_get		= nouveau_voltage_gpio_get;
		engine->pm.voltage_set		= nouveau_voltage_gpio_set;
187
		break;
188 189 190 191 192 193 194 195
	case 0xe0:
		engine->display.early_init	= nouveau_stub_init;
		engine->display.late_takedown	= nouveau_stub_takedown;
		engine->display.create		= nvd0_display_create;
		engine->display.destroy		= nvd0_display_destroy;
		engine->display.init		= nvd0_display_init;
		engine->display.fini		= nvd0_display_fini;
		break;
196 197 198 199 200
	default:
		NV_ERROR(dev, "NV%02x unsupported\n", dev_priv->chipset);
		return 1;
	}

201 202 203 204 205 206 207 208 209
	/* headless mode */
	if (nouveau_modeset == 2) {
		engine->display.early_init = nouveau_stub_init;
		engine->display.late_takedown = nouveau_stub_takedown;
		engine->display.create = nouveau_stub_init;
		engine->display.init = nouveau_stub_init;
		engine->display.destroy = nouveau_stub_takedown;
	}

210 211 212 213 214 215
	return 0;
}

static unsigned int
nouveau_vga_set_decode(void *priv, bool state)
{
216 217 218 219 220 221 222 223
	struct drm_device *dev = priv;
	struct drm_nouveau_private *dev_priv = dev->dev_private;

	if (dev_priv->chipset >= 0x40)
		nv_wr32(dev, 0x88054, state);
	else
		nv_wr32(dev, 0x1854, state);

224 225 226 227 228 229 230
	if (state)
		return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
		       VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
	else
		return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
}

231 232 233
static void nouveau_switcheroo_set_state(struct pci_dev *pdev,
					 enum vga_switcheroo_state state)
{
234
	struct drm_device *dev = pci_get_drvdata(pdev);
235 236 237
	pm_message_t pmm = { .event = PM_EVENT_SUSPEND };
	if (state == VGA_SWITCHEROO_ON) {
		printk(KERN_ERR "VGA switcheroo: switched nouveau on\n");
238
		dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
239
		nouveau_pci_resume(pdev);
240
		drm_kms_helper_poll_enable(dev);
241
		dev->switch_power_state = DRM_SWITCH_POWER_ON;
242 243
	} else {
		printk(KERN_ERR "VGA switcheroo: switched nouveau off\n");
244
		dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
245
		drm_kms_helper_poll_disable(dev);
246
		nouveau_switcheroo_optimus_dsm();
247
		nouveau_pci_suspend(pdev, pmm);
248
		dev->switch_power_state = DRM_SWITCH_POWER_OFF;
249 250 251
	}
}

252 253 254 255 256 257
static void nouveau_switcheroo_reprobe(struct pci_dev *pdev)
{
	struct drm_device *dev = pci_get_drvdata(pdev);
	nouveau_fbcon_output_poll_changed(dev);
}

258 259 260 261 262 263 264 265 266 267 268
static bool nouveau_switcheroo_can_switch(struct pci_dev *pdev)
{
	struct drm_device *dev = pci_get_drvdata(pdev);
	bool can_switch;

	spin_lock(&dev->count_lock);
	can_switch = (dev->open_count == 0);
	spin_unlock(&dev->count_lock);
	return can_switch;
}

269 270 271 272 273
static void
nouveau_card_channel_fini(struct drm_device *dev)
{
	struct drm_nouveau_private *dev_priv = dev->dev_private;

274
	if (dev_priv->channel) {
275
		nouveau_channel_put_unlocked(&dev_priv->channel);
276 277
		nouveau_vm_ref(NULL, &dev_priv->chan_vm, NULL);
	}
278 279 280 281 282 283 284
}

static int
nouveau_card_channel_init(struct drm_device *dev)
{
	struct drm_nouveau_private *dev_priv = dev->dev_private;
	struct nouveau_channel *chan;
285
	int ret;
286

287 288 289 290
	ret = nouveau_vm_new(dev, 0, (1ULL << 40), 0x1000, &dev_priv->chan_vm);
	if (ret)
		return ret;

291 292 293 294 295 296
	ret = nouveau_channel_alloc(dev, &chan, NULL, NvDmaFB, NvDmaTT);
	dev_priv->channel = chan;
	if (ret)
		return ret;
	mutex_unlock(&dev_priv->channel->mutex);

297 298
	nouveau_bo_move_init(chan);
	return 0;
299 300
}

301 302 303 304 305 306
static const struct vga_switcheroo_client_ops nouveau_switcheroo_ops = {
	.set_gpu_state = nouveau_switcheroo_set_state,
	.reprobe = nouveau_switcheroo_reprobe,
	.can_switch = nouveau_switcheroo_can_switch,
};

307 308 309 310 311
int
nouveau_card_init(struct drm_device *dev)
{
	struct drm_nouveau_private *dev_priv = dev->dev_private;
	struct nouveau_engine *engine;
312
	int ret, e = 0;
313 314

	vga_client_register(dev->pdev, dev, NULL, nouveau_vga_set_decode);
315
	vga_switcheroo_register_client(dev->pdev, &nouveau_switcheroo_ops);
316 317 318 319

	/* Initialise internal driver API hooks */
	ret = nouveau_init_engine_ptrs(dev);
	if (ret)
320
		goto out;
321
	engine = &dev_priv->engine;
322
	spin_lock_init(&dev_priv->channels.lock);
323
	spin_lock_init(&dev_priv->tile.lock);
324
	spin_lock_init(&dev_priv->context_switch_lock);
325
	spin_lock_init(&dev_priv->vm_lock);
326
	INIT_LIST_HEAD(&dev_priv->classes);
327

328 329 330 331 332
	/* Make the CRTCs and I2C buses accessible */
	ret = engine->display.early_init(dev);
	if (ret)
		goto out;

333
	/* Parse BIOS tables / Run init tables if card not POSTed */
334 335
	ret = nouveau_bios_init(dev);
	if (ret)
336
		goto out_display_early;
337

338 339 340 341 342 343 344 345
	/* workaround an odd issue on nvc1 by disabling the device's
	 * nosnoop capability.  hopefully won't cause issues until a
	 * better fix is found - assuming there is one...
	 */
	if (dev_priv->chipset == 0xc1) {
		nv_mask(dev, 0x00088080, 0x00000800, 0x00000000);
	}

346
	ret = nouveau_mem_vram_init(dev);
347
	if (ret)
348
		goto out_bios;
349

350 351 352 353
	ret = nouveau_mem_gart_init(dev);
	if (ret)
		goto out_ttmvram;

354
	if (!dev_priv->noaccel) {
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
		switch (dev_priv->card_type) {
		case NV_04:
			nv04_fifo_create(dev);
			break;
		case NV_10:
		case NV_20:
		case NV_30:
			if (dev_priv->chipset < 0x17)
				nv10_fifo_create(dev);
			else
				nv17_fifo_create(dev);
			break;
		case NV_40:
			nv40_fifo_create(dev);
			break;
		case NV_50:
			if (dev_priv->chipset == 0x50)
				nv50_fifo_create(dev);
			else
				nv84_fifo_create(dev);
			break;
		case NV_C0:
		case NV_D0:
			nvc0_fifo_create(dev);
			break;
		case NV_E0:
			nve0_fifo_create(dev);
			break;
		default:
			break;
		}

387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
		switch (dev_priv->card_type) {
		case NV_04:
			nv04_fence_create(dev);
			break;
		case NV_10:
		case NV_20:
		case NV_30:
		case NV_40:
		case NV_50:
			if (dev_priv->chipset < 0x84)
				nv10_fence_create(dev);
			else
				nv84_fence_create(dev);
			break;
		case NV_C0:
		case NV_D0:
		case NV_E0:
			nvc0_fence_create(dev);
			break;
		default:
			break;
		}

410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
		switch (dev_priv->card_type) {
		case NV_04:
		case NV_10:
		case NV_20:
		case NV_30:
		case NV_40:
			nv04_software_create(dev);
			break;
		case NV_50:
			nv50_software_create(dev);
			break;
		case NV_C0:
		case NV_D0:
		case NV_E0:
			nvc0_software_create(dev);
			break;
		default:
			break;
		}

430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
		switch (dev_priv->card_type) {
		case NV_04:
			nv04_graph_create(dev);
			break;
		case NV_10:
			nv10_graph_create(dev);
			break;
		case NV_20:
		case NV_30:
			nv20_graph_create(dev);
			break;
		case NV_40:
			nv40_graph_create(dev);
			break;
		case NV_50:
			nv50_graph_create(dev);
			break;
		case NV_C0:
448
		case NV_D0:
449 450
			nvc0_graph_create(dev);
			break;
B
Ben Skeggs 已提交
451 452 453
		case NV_E0:
			nve0_graph_create(dev);
			break;
454 455 456
		default:
			break;
		}
457

458
		switch (dev_priv->chipset) {
459 460 461 462 463 464 465
		case 0x84:
		case 0x86:
		case 0x92:
		case 0x94:
		case 0x96:
		case 0xa0:
			nv84_crypt_create(dev);
466
			break;
467 468 469 470 471
		case 0x98:
		case 0xaa:
		case 0xac:
			nv98_crypt_create(dev);
			break;
472 473
		}

474 475 476 477 478 479 480 481 482 483 484
		switch (dev_priv->card_type) {
		case NV_50:
			switch (dev_priv->chipset) {
			case 0xa3:
			case 0xa5:
			case 0xa8:
				nva3_copy_create(dev);
				break;
			}
			break;
		case NV_C0:
485 486
			if (!(nv_rd32(dev, 0x022500) & 0x00000200))
				nvc0_copy_create(dev, 1);
487
		case NV_D0:
488 489
			if (!(nv_rd32(dev, 0x022500) & 0x00000100))
				nvc0_copy_create(dev, 0);
490 491 492 493 494
			break;
		default:
			break;
		}

495 496 497 498 499 500 501 502 503 504 505 506 507
		if (dev_priv->chipset >= 0xa3 || dev_priv->chipset == 0x98) {
			nv84_bsp_create(dev);
			nv84_vp_create(dev);
			nv98_ppp_create(dev);
		} else
		if (dev_priv->chipset >= 0x84) {
			nv50_mpeg_create(dev);
			nv84_bsp_create(dev);
			nv84_vp_create(dev);
		} else
		if (dev_priv->chipset >= 0x50) {
			nv50_mpeg_create(dev);
		} else
508 509 510
		if (dev_priv->card_type == NV_40 ||
		    dev_priv->chipset == 0x31 ||
		    dev_priv->chipset == 0x34 ||
511
		    dev_priv->chipset == 0x36) {
512
			nv31_mpeg_create(dev);
513
		}
B
Ben Skeggs 已提交
514

515 516 517 518 519 520 521
		for (e = 0; e < NVOBJ_ENGINE_NR; e++) {
			if (dev_priv->eng[e]) {
				ret = dev_priv->eng[e]->init(dev, e);
				if (ret)
					goto out_engine;
			}
		}
522
	}
523

524 525
	ret = nouveau_irq_init(dev);
	if (ret)
526
		goto out_engine;
527

528
	ret = nouveau_display_create(dev);
529
	if (ret)
530
		goto out_irq;
531

532
	nouveau_backlight_init(dev);
533
	nouveau_pm_init(dev);
534

535
	if (dev_priv->eng[NVOBJ_ENGINE_GR]) {
536
		ret = nouveau_card_channel_init(dev);
537
		if (ret)
538
			goto out_pm;
539 540 541
	}

	if (dev->mode_config.num_crtc) {
542
		ret = nouveau_display_init(dev);
543 544 545 546
		if (ret)
			goto out_chan;

		nouveau_fbcon_init(dev);
547 548 549
	}

	return 0;
550

551
out_chan:
552
	nouveau_card_channel_fini(dev);
553 554
out_pm:
	nouveau_pm_fini(dev);
555
	nouveau_backlight_exit(dev);
556
	nouveau_display_destroy(dev);
557
out_irq:
B
Ben Skeggs 已提交
558
	nouveau_irq_fini(dev);
559
out_engine:
560
	if (!dev_priv->noaccel) {
561
		for (e = e - 1; e >= 0; e--) {
562 563
			if (!dev_priv->eng[e])
				continue;
564
			dev_priv->eng[e]->fini(dev, e, false);
565
			dev_priv->eng[e]->destroy(dev,e );
566 567
		}
	}
568
	nouveau_mem_gart_fini(dev);
569 570
out_ttmvram:
	nouveau_mem_vram_fini(dev);
571 572
out_bios:
	nouveau_bios_takedown(dev);
573 574
out_display_early:
	engine->display.late_takedown(dev);
575
out:
576
	vga_switcheroo_unregister_client(dev->pdev);
577 578
	vga_client_register(dev->pdev, NULL, NULL, NULL);
	return ret;
579 580 581 582 583 584
}

static void nouveau_card_takedown(struct drm_device *dev)
{
	struct drm_nouveau_private *dev_priv = dev->dev_private;
	struct nouveau_engine *engine = &dev_priv->engine;
585
	int e;
586

587 588
	if (dev->mode_config.num_crtc) {
		nouveau_fbcon_fini(dev);
589
		nouveau_display_fini(dev);
590
	}
591

592
	nouveau_card_channel_fini(dev);
593
	nouveau_pm_fini(dev);
594
	nouveau_backlight_exit(dev);
595
	nouveau_display_destroy(dev);
596

597
	if (!dev_priv->noaccel) {
598 599
		for (e = NVOBJ_ENGINE_NR - 1; e >= 0; e--) {
			if (dev_priv->eng[e]) {
600
				dev_priv->eng[e]->fini(dev, e, false);
601 602 603
				dev_priv->eng[e]->destroy(dev,e );
			}
		}
604
	}
605

606 607 608 609 610
	if (dev_priv->vga_ram) {
		nouveau_bo_unpin(dev_priv->vga_ram);
		nouveau_bo_ref(NULL, &dev_priv->vga_ram);
	}

611 612 613 614
	mutex_lock(&dev->struct_mutex);
	ttm_bo_clean_mm(&dev_priv->ttm.bdev, TTM_PL_VRAM);
	ttm_bo_clean_mm(&dev_priv->ttm.bdev, TTM_PL_TT);
	mutex_unlock(&dev->struct_mutex);
615
	nouveau_mem_gart_fini(dev);
616
	nouveau_mem_vram_fini(dev);
617

618
	nouveau_bios_takedown(dev);
619 620 621
	engine->display.late_takedown(dev);

	nouveau_irq_fini(dev);
622

623
	vga_switcheroo_unregister_client(dev->pdev);
624
	vga_client_register(dev->pdev, NULL, NULL, NULL);
625 626
}

627 628 629
int
nouveau_open(struct drm_device *dev, struct drm_file *file_priv)
{
630
	struct drm_nouveau_private *dev_priv = dev->dev_private;
631
	struct nouveau_fpriv *fpriv;
632
	int ret;
633 634 635 636 637 638

	fpriv = kzalloc(sizeof(*fpriv), GFP_KERNEL);
	if (unlikely(!fpriv))
		return -ENOMEM;

	spin_lock_init(&fpriv->lock);
639 640
	INIT_LIST_HEAD(&fpriv->channels);

641 642 643 644 645 646 647 648 649
	if (dev_priv->card_type == NV_50) {
		ret = nouveau_vm_new(dev, 0, (1ULL << 40), 0x0020000000ULL,
				     &fpriv->vm);
		if (ret) {
			kfree(fpriv);
			return ret;
		}
	} else
	if (dev_priv->card_type >= NV_C0) {
650 651 652 653 654 655
		ret = nouveau_vm_new(dev, 0, (1ULL << 40), 0x0008000000ULL,
				     &fpriv->vm);
		if (ret) {
			kfree(fpriv);
			return ret;
		}
656
	}
657

658 659 660 661
	file_priv->driver_priv = fpriv;
	return 0;
}

662 663 664 665 666 667 668
/* here a client dies, release the stuff that was allocated for its
 * file_priv */
void nouveau_preclose(struct drm_device *dev, struct drm_file *file_priv)
{
	nouveau_channel_cleanup(dev, file_priv);
}

669 670 671 672
void
nouveau_postclose(struct drm_device *dev, struct drm_file *file_priv)
{
	struct nouveau_fpriv *fpriv = nouveau_fpriv(file_priv);
673
	nouveau_vm_ref(NULL, &fpriv->vm, NULL);
674 675 676
	kfree(fpriv);
}

677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706
/* first module load, setup the mmio/fb mapping */
/* KMS: we need mmio at load time, not when the first drm client opens. */
int nouveau_firstopen(struct drm_device *dev)
{
	return 0;
}

/* if we have an OF card, copy vbios to RAMIN */
static void nouveau_OF_copy_vbios_to_ramin(struct drm_device *dev)
{
#if defined(__powerpc__)
	int size, i;
	const uint32_t *bios;
	struct device_node *dn = pci_device_to_OF_node(dev->pdev);
	if (!dn) {
		NV_INFO(dev, "Unable to get the OF node\n");
		return;
	}

	bios = of_get_property(dn, "NVDA,BMP", &size);
	if (bios) {
		for (i = 0; i < size; i += 4)
			nv_wi32(dev, i, bios[i/4]);
		NV_INFO(dev, "OF bios successfully copied (%d bytes)\n", size);
	} else {
		NV_INFO(dev, "Unable to get the OF bios\n");
	}
#endif
}

707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735
static struct apertures_struct *nouveau_get_apertures(struct drm_device *dev)
{
	struct pci_dev *pdev = dev->pdev;
	struct apertures_struct *aper = alloc_apertures(3);
	if (!aper)
		return NULL;

	aper->ranges[0].base = pci_resource_start(pdev, 1);
	aper->ranges[0].size = pci_resource_len(pdev, 1);
	aper->count = 1;

	if (pci_resource_len(pdev, 2)) {
		aper->ranges[aper->count].base = pci_resource_start(pdev, 2);
		aper->ranges[aper->count].size = pci_resource_len(pdev, 2);
		aper->count++;
	}

	if (pci_resource_len(pdev, 3)) {
		aper->ranges[aper->count].base = pci_resource_start(pdev, 3);
		aper->ranges[aper->count].size = pci_resource_len(pdev, 3);
		aper->count++;
	}

	return aper;
}

static int nouveau_remove_conflicting_drivers(struct drm_device *dev)
{
	struct drm_nouveau_private *dev_priv = dev->dev_private;
736
	bool primary = false;
737 738 739 740
	dev_priv->apertures = nouveau_get_apertures(dev);
	if (!dev_priv->apertures)
		return -ENOMEM;

741 742 743
#ifdef CONFIG_X86
	primary = dev->pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW;
#endif
744

745
	remove_conflicting_framebuffers(dev_priv->apertures, "nouveaufb", primary);
746 747 748
	return 0;
}

749 750 751 752 753 754 755
void *
nouveau_newpriv(struct drm_device *dev)
{
	struct drm_nouveau_private *dev_priv = dev->dev_private;
	return dev_priv->newpriv;
}

756 757 758
int nouveau_load(struct drm_device *dev, unsigned long flags)
{
	struct drm_nouveau_private *dev_priv;
759
	uint32_t reg0 = ~0, strap;
760
	int ret;
761 762

	dev_priv = kzalloc(sizeof(*dev_priv), GFP_KERNEL);
763 764 765 766
	if (!dev_priv) {
		ret = -ENOMEM;
		goto err_out;
	}
767
	dev_priv->newpriv = dev->dev_private;
768 769 770 771 772 773 774 775
	dev->dev_private = dev_priv;
	dev_priv->dev = dev;

	dev_priv->flags = flags & NOUVEAU_FLAGS;

	NV_DEBUG(dev, "vendor: 0x%X device: 0x%X class: 0x%X\n",
		 dev->pci_vendor, dev->pci_device, dev->pdev->class);

776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806
	/* determine chipset and derive architecture from it */
	reg0 = nv_rd32(dev, NV03_PMC_BOOT_0);
	if ((reg0 & 0x0f000000) > 0) {
		dev_priv->chipset = (reg0 & 0xff00000) >> 20;
		switch (dev_priv->chipset & 0xf0) {
		case 0x10:
		case 0x20:
		case 0x30:
			dev_priv->card_type = dev_priv->chipset & 0xf0;
			break;
		case 0x40:
		case 0x60:
			dev_priv->card_type = NV_40;
			break;
		case 0x50:
		case 0x80:
		case 0x90:
		case 0xa0:
			dev_priv->card_type = NV_50;
			break;
		case 0xc0:
			dev_priv->card_type = NV_C0;
			break;
		case 0xd0:
			dev_priv->card_type = NV_D0;
			break;
		case 0xe0:
			dev_priv->card_type = NV_E0;
			break;
		default:
			break;
807
		}
808 809 810 811 812 813 814
	} else
	if ((reg0 & 0xff00fff0) == 0x20004000) {
		if (reg0 & 0x00f00000)
			dev_priv->chipset = 0x05;
		else
			dev_priv->chipset = 0x04;
		dev_priv->card_type = NV_04;
815 816 817 818 819 820 821 822
	}

	if (!dev_priv->card_type) {
		NV_ERROR(dev, "unsupported chipset 0x%08x\n", reg0);
		ret = -EINVAL;
		goto err_priv;
	}

823
	NV_INFO(dev, "Detected an NV%02x generation card (0x%08x)\n",
824
		     dev_priv->card_type, reg0);
825

826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842
	/* determine frequency of timing crystal */
	strap = nv_rd32(dev, 0x101000);
	if ( dev_priv->chipset < 0x17 ||
	    (dev_priv->chipset >= 0x20 && dev_priv->chipset <= 0x25))
		strap &= 0x00000040;
	else
		strap &= 0x00400040;

	switch (strap) {
	case 0x00000000: dev_priv->crystal = 13500; break;
	case 0x00000040: dev_priv->crystal = 14318; break;
	case 0x00400000: dev_priv->crystal = 27000; break;
	case 0x00400040: dev_priv->crystal = 25000; break;
	}

	NV_DEBUG(dev, "crystal freq: %dKHz\n", dev_priv->crystal);

843 844 845 846 847 848 849
	/* Determine whether we'll attempt acceleration or not, some
	 * cards are disabled by default here due to them being known
	 * non-functional, or never been tested due to lack of hw.
	 */
	dev_priv->noaccel = !!nouveau_noaccel;
	if (nouveau_noaccel == -1) {
		switch (dev_priv->chipset) {
850
		case 0xd9: /* known broken */
B
Ben Skeggs 已提交
851 852
		case 0xe4: /* needs binary driver firmware */
		case 0xe7: /* needs binary driver firmware */
853 854
			NV_INFO(dev, "acceleration disabled by default, pass "
				     "noaccel=0 to force enable\n");
855 856 857 858 859 860 861 862
			dev_priv->noaccel = true;
			break;
		default:
			dev_priv->noaccel = false;
			break;
		}
	}

863 864
	ret = nouveau_remove_conflicting_drivers(dev);
	if (ret)
865
		goto err_priv;
866

867 868 869 870 871 872 873 874 875
	nouveau_OF_copy_vbios_to_ramin(dev);

	/* Special flags */
	if (dev->pci_device == 0x01a0)
		dev_priv->flags |= NV_NFORCE;
	else if (dev->pci_device == 0x01f0)
		dev_priv->flags |= NV_NFORCE2;

	/* For kernel modesetting, init card now and bring up fbcon */
876 877
	ret = nouveau_card_init(dev);
	if (ret)
878
		goto err_priv;
879 880

	return 0;
881 882

err_priv:
883
	dev->dev_private = dev_priv->newpriv;
884 885 886
	kfree(dev_priv);
err_out:
	return ret;
887 888 889 890
}

void nouveau_lastclose(struct drm_device *dev)
{
891
	vga_switcheroo_process_delayed_switch();
892 893 894 895 896 897
}

int nouveau_unload(struct drm_device *dev)
{
	struct drm_nouveau_private *dev_priv = dev->dev_private;

898
	nouveau_card_takedown(dev);
899

900
	dev->dev_private = dev_priv->newpriv;
901 902 903 904 905 906 907
	kfree(dev_priv);
	return 0;
}

/* Waits for PGRAPH to go completely idle */
bool nouveau_wait_for_idle(struct drm_device *dev)
{
908 909 910 911 912 913 914
	struct drm_nouveau_private *dev_priv = dev->dev_private;
	uint32_t mask = ~0;

	if (dev_priv->card_type == NV_40)
		mask &= ~NV40_PGRAPH_STATUS_SYNC_STALL;

	if (!nv_wait(dev, NV04_PGRAPH_STATUS, mask, 0)) {
915 916 917 918 919 920 921 922
		NV_ERROR(dev, "PGRAPH idle timed out with status 0x%08x\n",
			 nv_rd32(dev, NV04_PGRAPH_STATUS));
		return false;
	}

	return true;
}