nouveau_state.c 35.9 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 36

#include "nouveau_drv.h"
#include "nouveau_drm.h"
37
#include "nouveau_fbcon.h"
38
#include "nouveau_ramht.h"
39
#include "nouveau_pm.h"
40 41 42
#include "nv50_display.h"

static void nouveau_stub_takedown(struct drm_device *dev) {}
B
Ben Skeggs 已提交
43
static int nouveau_stub_init(struct drm_device *dev) { return 0; }
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59

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:
		engine->instmem.init		= nv04_instmem_init;
		engine->instmem.takedown	= nv04_instmem_takedown;
		engine->instmem.suspend		= nv04_instmem_suspend;
		engine->instmem.resume		= nv04_instmem_resume;
		engine->instmem.populate	= nv04_instmem_populate;
		engine->instmem.clear		= nv04_instmem_clear;
		engine->instmem.bind		= nv04_instmem_bind;
		engine->instmem.unbind		= nv04_instmem_unbind;
60
		engine->instmem.flush		= nv04_instmem_flush;
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
		engine->mc.init			= nv04_mc_init;
		engine->mc.takedown		= nv04_mc_takedown;
		engine->timer.init		= nv04_timer_init;
		engine->timer.read		= nv04_timer_read;
		engine->timer.takedown		= nv04_timer_takedown;
		engine->fb.init			= nv04_fb_init;
		engine->fb.takedown		= nv04_fb_takedown;
		engine->graph.init		= nv04_graph_init;
		engine->graph.takedown		= nv04_graph_takedown;
		engine->graph.fifo_access	= nv04_graph_fifo_access;
		engine->graph.channel		= nv04_graph_channel;
		engine->graph.create_context	= nv04_graph_create_context;
		engine->graph.destroy_context	= nv04_graph_destroy_context;
		engine->graph.load_context	= nv04_graph_load_context;
		engine->graph.unload_context	= nv04_graph_unload_context;
		engine->fifo.channels		= 16;
		engine->fifo.init		= nv04_fifo_init;
		engine->fifo.takedown		= nouveau_stub_takedown;
		engine->fifo.disable		= nv04_fifo_disable;
		engine->fifo.enable		= nv04_fifo_enable;
		engine->fifo.reassign		= nv04_fifo_reassign;
82
		engine->fifo.cache_pull		= nv04_fifo_cache_pull;
83 84 85 86 87
		engine->fifo.channel_id		= nv04_fifo_channel_id;
		engine->fifo.create_context	= nv04_fifo_create_context;
		engine->fifo.destroy_context	= nv04_fifo_destroy_context;
		engine->fifo.load_context	= nv04_fifo_load_context;
		engine->fifo.unload_context	= nv04_fifo_unload_context;
88 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.init		= nv04_display_init;
		engine->display.destroy		= nv04_display_destroy;
B
Ben Skeggs 已提交
93 94 95 96 97
		engine->gpio.init		= nouveau_stub_init;
		engine->gpio.takedown		= nouveau_stub_takedown;
		engine->gpio.get		= NULL;
		engine->gpio.set		= NULL;
		engine->gpio.irq_enable		= NULL;
98 99 100
		engine->pm.clock_get		= nv04_pm_clock_get;
		engine->pm.clock_pre		= nv04_pm_clock_pre;
		engine->pm.clock_set		= nv04_pm_clock_set;
101 102
		engine->crypt.init		= nouveau_stub_init;
		engine->crypt.takedown		= nouveau_stub_takedown;
103 104 105 106 107 108 109 110 111 112
		break;
	case 0x10:
		engine->instmem.init		= nv04_instmem_init;
		engine->instmem.takedown	= nv04_instmem_takedown;
		engine->instmem.suspend		= nv04_instmem_suspend;
		engine->instmem.resume		= nv04_instmem_resume;
		engine->instmem.populate	= nv04_instmem_populate;
		engine->instmem.clear		= nv04_instmem_clear;
		engine->instmem.bind		= nv04_instmem_bind;
		engine->instmem.unbind		= nv04_instmem_unbind;
113
		engine->instmem.flush		= nv04_instmem_flush;
114 115 116 117 118 119 120
		engine->mc.init			= nv04_mc_init;
		engine->mc.takedown		= nv04_mc_takedown;
		engine->timer.init		= nv04_timer_init;
		engine->timer.read		= nv04_timer_read;
		engine->timer.takedown		= nv04_timer_takedown;
		engine->fb.init			= nv10_fb_init;
		engine->fb.takedown		= nv10_fb_takedown;
121
		engine->fb.set_region_tiling	= nv10_fb_set_region_tiling;
122 123 124 125 126 127 128 129
		engine->graph.init		= nv10_graph_init;
		engine->graph.takedown		= nv10_graph_takedown;
		engine->graph.channel		= nv10_graph_channel;
		engine->graph.create_context	= nv10_graph_create_context;
		engine->graph.destroy_context	= nv10_graph_destroy_context;
		engine->graph.fifo_access	= nv04_graph_fifo_access;
		engine->graph.load_context	= nv10_graph_load_context;
		engine->graph.unload_context	= nv10_graph_unload_context;
130
		engine->graph.set_region_tiling	= nv10_graph_set_region_tiling;
131 132 133 134 135 136
		engine->fifo.channels		= 32;
		engine->fifo.init		= nv10_fifo_init;
		engine->fifo.takedown		= nouveau_stub_takedown;
		engine->fifo.disable		= nv04_fifo_disable;
		engine->fifo.enable		= nv04_fifo_enable;
		engine->fifo.reassign		= nv04_fifo_reassign;
137
		engine->fifo.cache_pull		= nv04_fifo_cache_pull;
138 139
		engine->fifo.channel_id		= nv10_fifo_channel_id;
		engine->fifo.create_context	= nv10_fifo_create_context;
140
		engine->fifo.destroy_context	= nv04_fifo_destroy_context;
141 142
		engine->fifo.load_context	= nv10_fifo_load_context;
		engine->fifo.unload_context	= nv10_fifo_unload_context;
143 144 145 146 147
		engine->display.early_init	= nv04_display_early_init;
		engine->display.late_takedown	= nv04_display_late_takedown;
		engine->display.create		= nv04_display_create;
		engine->display.init		= nv04_display_init;
		engine->display.destroy		= nv04_display_destroy;
B
Ben Skeggs 已提交
148 149 150 151 152
		engine->gpio.init		= nouveau_stub_init;
		engine->gpio.takedown		= nouveau_stub_takedown;
		engine->gpio.get		= nv10_gpio_get;
		engine->gpio.set		= nv10_gpio_set;
		engine->gpio.irq_enable		= NULL;
153 154 155
		engine->pm.clock_get		= nv04_pm_clock_get;
		engine->pm.clock_pre		= nv04_pm_clock_pre;
		engine->pm.clock_set		= nv04_pm_clock_set;
156 157
		engine->crypt.init		= nouveau_stub_init;
		engine->crypt.takedown		= nouveau_stub_takedown;
158 159 160 161 162 163 164 165 166 167
		break;
	case 0x20:
		engine->instmem.init		= nv04_instmem_init;
		engine->instmem.takedown	= nv04_instmem_takedown;
		engine->instmem.suspend		= nv04_instmem_suspend;
		engine->instmem.resume		= nv04_instmem_resume;
		engine->instmem.populate	= nv04_instmem_populate;
		engine->instmem.clear		= nv04_instmem_clear;
		engine->instmem.bind		= nv04_instmem_bind;
		engine->instmem.unbind		= nv04_instmem_unbind;
168
		engine->instmem.flush		= nv04_instmem_flush;
169 170 171 172 173 174 175
		engine->mc.init			= nv04_mc_init;
		engine->mc.takedown		= nv04_mc_takedown;
		engine->timer.init		= nv04_timer_init;
		engine->timer.read		= nv04_timer_read;
		engine->timer.takedown		= nv04_timer_takedown;
		engine->fb.init			= nv10_fb_init;
		engine->fb.takedown		= nv10_fb_takedown;
176
		engine->fb.set_region_tiling	= nv10_fb_set_region_tiling;
177 178 179 180 181 182 183 184
		engine->graph.init		= nv20_graph_init;
		engine->graph.takedown		= nv20_graph_takedown;
		engine->graph.channel		= nv10_graph_channel;
		engine->graph.create_context	= nv20_graph_create_context;
		engine->graph.destroy_context	= nv20_graph_destroy_context;
		engine->graph.fifo_access	= nv04_graph_fifo_access;
		engine->graph.load_context	= nv20_graph_load_context;
		engine->graph.unload_context	= nv20_graph_unload_context;
185
		engine->graph.set_region_tiling	= nv20_graph_set_region_tiling;
186 187 188 189 190 191
		engine->fifo.channels		= 32;
		engine->fifo.init		= nv10_fifo_init;
		engine->fifo.takedown		= nouveau_stub_takedown;
		engine->fifo.disable		= nv04_fifo_disable;
		engine->fifo.enable		= nv04_fifo_enable;
		engine->fifo.reassign		= nv04_fifo_reassign;
192
		engine->fifo.cache_pull		= nv04_fifo_cache_pull;
193 194
		engine->fifo.channel_id		= nv10_fifo_channel_id;
		engine->fifo.create_context	= nv10_fifo_create_context;
195
		engine->fifo.destroy_context	= nv04_fifo_destroy_context;
196 197
		engine->fifo.load_context	= nv10_fifo_load_context;
		engine->fifo.unload_context	= nv10_fifo_unload_context;
198 199 200 201 202
		engine->display.early_init	= nv04_display_early_init;
		engine->display.late_takedown	= nv04_display_late_takedown;
		engine->display.create		= nv04_display_create;
		engine->display.init		= nv04_display_init;
		engine->display.destroy		= nv04_display_destroy;
B
Ben Skeggs 已提交
203 204 205 206 207
		engine->gpio.init		= nouveau_stub_init;
		engine->gpio.takedown		= nouveau_stub_takedown;
		engine->gpio.get		= nv10_gpio_get;
		engine->gpio.set		= nv10_gpio_set;
		engine->gpio.irq_enable		= NULL;
208 209 210
		engine->pm.clock_get		= nv04_pm_clock_get;
		engine->pm.clock_pre		= nv04_pm_clock_pre;
		engine->pm.clock_set		= nv04_pm_clock_set;
211 212
		engine->crypt.init		= nouveau_stub_init;
		engine->crypt.takedown		= nouveau_stub_takedown;
213 214 215 216 217 218 219 220 221 222
		break;
	case 0x30:
		engine->instmem.init		= nv04_instmem_init;
		engine->instmem.takedown	= nv04_instmem_takedown;
		engine->instmem.suspend		= nv04_instmem_suspend;
		engine->instmem.resume		= nv04_instmem_resume;
		engine->instmem.populate	= nv04_instmem_populate;
		engine->instmem.clear		= nv04_instmem_clear;
		engine->instmem.bind		= nv04_instmem_bind;
		engine->instmem.unbind		= nv04_instmem_unbind;
223
		engine->instmem.flush		= nv04_instmem_flush;
224 225 226 227 228
		engine->mc.init			= nv04_mc_init;
		engine->mc.takedown		= nv04_mc_takedown;
		engine->timer.init		= nv04_timer_init;
		engine->timer.read		= nv04_timer_read;
		engine->timer.takedown		= nv04_timer_takedown;
229 230
		engine->fb.init			= nv30_fb_init;
		engine->fb.takedown		= nv30_fb_takedown;
231
		engine->fb.set_region_tiling	= nv10_fb_set_region_tiling;
232 233 234 235 236 237 238 239
		engine->graph.init		= nv30_graph_init;
		engine->graph.takedown		= nv20_graph_takedown;
		engine->graph.fifo_access	= nv04_graph_fifo_access;
		engine->graph.channel		= nv10_graph_channel;
		engine->graph.create_context	= nv20_graph_create_context;
		engine->graph.destroy_context	= nv20_graph_destroy_context;
		engine->graph.load_context	= nv20_graph_load_context;
		engine->graph.unload_context	= nv20_graph_unload_context;
240
		engine->graph.set_region_tiling	= nv20_graph_set_region_tiling;
241 242 243 244 245 246
		engine->fifo.channels		= 32;
		engine->fifo.init		= nv10_fifo_init;
		engine->fifo.takedown		= nouveau_stub_takedown;
		engine->fifo.disable		= nv04_fifo_disable;
		engine->fifo.enable		= nv04_fifo_enable;
		engine->fifo.reassign		= nv04_fifo_reassign;
247
		engine->fifo.cache_pull		= nv04_fifo_cache_pull;
248 249
		engine->fifo.channel_id		= nv10_fifo_channel_id;
		engine->fifo.create_context	= nv10_fifo_create_context;
250
		engine->fifo.destroy_context	= nv04_fifo_destroy_context;
251 252
		engine->fifo.load_context	= nv10_fifo_load_context;
		engine->fifo.unload_context	= nv10_fifo_unload_context;
253 254 255 256 257
		engine->display.early_init	= nv04_display_early_init;
		engine->display.late_takedown	= nv04_display_late_takedown;
		engine->display.create		= nv04_display_create;
		engine->display.init		= nv04_display_init;
		engine->display.destroy		= nv04_display_destroy;
B
Ben Skeggs 已提交
258 259 260 261 262
		engine->gpio.init		= nouveau_stub_init;
		engine->gpio.takedown		= nouveau_stub_takedown;
		engine->gpio.get		= nv10_gpio_get;
		engine->gpio.set		= nv10_gpio_set;
		engine->gpio.irq_enable		= NULL;
263 264 265 266 267
		engine->pm.clock_get		= nv04_pm_clock_get;
		engine->pm.clock_pre		= nv04_pm_clock_pre;
		engine->pm.clock_set		= nv04_pm_clock_set;
		engine->pm.voltage_get		= nouveau_voltage_gpio_get;
		engine->pm.voltage_set		= nouveau_voltage_gpio_set;
268 269
		engine->crypt.init		= nouveau_stub_init;
		engine->crypt.takedown		= nouveau_stub_takedown;
270 271 272 273 274 275 276 277 278 279 280
		break;
	case 0x40:
	case 0x60:
		engine->instmem.init		= nv04_instmem_init;
		engine->instmem.takedown	= nv04_instmem_takedown;
		engine->instmem.suspend		= nv04_instmem_suspend;
		engine->instmem.resume		= nv04_instmem_resume;
		engine->instmem.populate	= nv04_instmem_populate;
		engine->instmem.clear		= nv04_instmem_clear;
		engine->instmem.bind		= nv04_instmem_bind;
		engine->instmem.unbind		= nv04_instmem_unbind;
281
		engine->instmem.flush		= nv04_instmem_flush;
282 283 284 285 286 287 288
		engine->mc.init			= nv40_mc_init;
		engine->mc.takedown		= nv40_mc_takedown;
		engine->timer.init		= nv04_timer_init;
		engine->timer.read		= nv04_timer_read;
		engine->timer.takedown		= nv04_timer_takedown;
		engine->fb.init			= nv40_fb_init;
		engine->fb.takedown		= nv40_fb_takedown;
289
		engine->fb.set_region_tiling	= nv40_fb_set_region_tiling;
290 291 292 293 294 295 296 297
		engine->graph.init		= nv40_graph_init;
		engine->graph.takedown		= nv40_graph_takedown;
		engine->graph.fifo_access	= nv04_graph_fifo_access;
		engine->graph.channel		= nv40_graph_channel;
		engine->graph.create_context	= nv40_graph_create_context;
		engine->graph.destroy_context	= nv40_graph_destroy_context;
		engine->graph.load_context	= nv40_graph_load_context;
		engine->graph.unload_context	= nv40_graph_unload_context;
298
		engine->graph.set_region_tiling	= nv40_graph_set_region_tiling;
299 300 301 302 303 304
		engine->fifo.channels		= 32;
		engine->fifo.init		= nv40_fifo_init;
		engine->fifo.takedown		= nouveau_stub_takedown;
		engine->fifo.disable		= nv04_fifo_disable;
		engine->fifo.enable		= nv04_fifo_enable;
		engine->fifo.reassign		= nv04_fifo_reassign;
305
		engine->fifo.cache_pull		= nv04_fifo_cache_pull;
306 307
		engine->fifo.channel_id		= nv10_fifo_channel_id;
		engine->fifo.create_context	= nv40_fifo_create_context;
308
		engine->fifo.destroy_context	= nv04_fifo_destroy_context;
309 310
		engine->fifo.load_context	= nv40_fifo_load_context;
		engine->fifo.unload_context	= nv40_fifo_unload_context;
311 312 313 314 315
		engine->display.early_init	= nv04_display_early_init;
		engine->display.late_takedown	= nv04_display_late_takedown;
		engine->display.create		= nv04_display_create;
		engine->display.init		= nv04_display_init;
		engine->display.destroy		= nv04_display_destroy;
B
Ben Skeggs 已提交
316 317 318 319 320
		engine->gpio.init		= nouveau_stub_init;
		engine->gpio.takedown		= nouveau_stub_takedown;
		engine->gpio.get		= nv10_gpio_get;
		engine->gpio.set		= nv10_gpio_set;
		engine->gpio.irq_enable		= NULL;
321 322 323 324 325
		engine->pm.clock_get		= nv04_pm_clock_get;
		engine->pm.clock_pre		= nv04_pm_clock_pre;
		engine->pm.clock_set		= nv04_pm_clock_set;
		engine->pm.voltage_get		= nouveau_voltage_gpio_get;
		engine->pm.voltage_set		= nouveau_voltage_gpio_set;
326
		engine->pm.temp_get		= nv40_temp_get;
327 328
		engine->crypt.init		= nouveau_stub_init;
		engine->crypt.takedown		= nouveau_stub_takedown;
329 330 331 332 333 334 335 336 337 338 339 340 341
		break;
	case 0x50:
	case 0x80: /* gotta love NVIDIA's consistency.. */
	case 0x90:
	case 0xA0:
		engine->instmem.init		= nv50_instmem_init;
		engine->instmem.takedown	= nv50_instmem_takedown;
		engine->instmem.suspend		= nv50_instmem_suspend;
		engine->instmem.resume		= nv50_instmem_resume;
		engine->instmem.populate	= nv50_instmem_populate;
		engine->instmem.clear		= nv50_instmem_clear;
		engine->instmem.bind		= nv50_instmem_bind;
		engine->instmem.unbind		= nv50_instmem_unbind;
342 343 344 345
		if (dev_priv->chipset == 0x50)
			engine->instmem.flush	= nv50_instmem_flush;
		else
			engine->instmem.flush	= nv84_instmem_flush;
346 347 348 349 350
		engine->mc.init			= nv50_mc_init;
		engine->mc.takedown		= nv50_mc_takedown;
		engine->timer.init		= nv04_timer_init;
		engine->timer.read		= nv04_timer_read;
		engine->timer.takedown		= nv04_timer_takedown;
351 352
		engine->fb.init			= nv50_fb_init;
		engine->fb.takedown		= nv50_fb_takedown;
353 354 355 356 357 358 359 360
		engine->graph.init		= nv50_graph_init;
		engine->graph.takedown		= nv50_graph_takedown;
		engine->graph.fifo_access	= nv50_graph_fifo_access;
		engine->graph.channel		= nv50_graph_channel;
		engine->graph.create_context	= nv50_graph_create_context;
		engine->graph.destroy_context	= nv50_graph_destroy_context;
		engine->graph.load_context	= nv50_graph_load_context;
		engine->graph.unload_context	= nv50_graph_unload_context;
361 362 363 364 365 366 367 368 369
		if (dev_priv->chipset != 0x86)
			engine->graph.tlb_flush	= nv50_graph_tlb_flush;
		else {
			/* from what i can see nvidia do this on every
			 * pre-NVA3 board except NVAC, but, we've only
			 * ever seen problems on NV86
			 */
			engine->graph.tlb_flush	= nv86_graph_tlb_flush;
		}
370 371 372 373 374 375 376 377 378 379 380
		engine->fifo.channels		= 128;
		engine->fifo.init		= nv50_fifo_init;
		engine->fifo.takedown		= nv50_fifo_takedown;
		engine->fifo.disable		= nv04_fifo_disable;
		engine->fifo.enable		= nv04_fifo_enable;
		engine->fifo.reassign		= nv04_fifo_reassign;
		engine->fifo.channel_id		= nv50_fifo_channel_id;
		engine->fifo.create_context	= nv50_fifo_create_context;
		engine->fifo.destroy_context	= nv50_fifo_destroy_context;
		engine->fifo.load_context	= nv50_fifo_load_context;
		engine->fifo.unload_context	= nv50_fifo_unload_context;
381
		engine->fifo.tlb_flush		= nv50_fifo_tlb_flush;
382 383 384 385 386
		engine->display.early_init	= nv50_display_early_init;
		engine->display.late_takedown	= nv50_display_late_takedown;
		engine->display.create		= nv50_display_create;
		engine->display.init		= nv50_display_init;
		engine->display.destroy		= nv50_display_destroy;
B
Ben Skeggs 已提交
387 388 389 390 391
		engine->gpio.init		= nv50_gpio_init;
		engine->gpio.takedown		= nouveau_stub_takedown;
		engine->gpio.get		= nv50_gpio_get;
		engine->gpio.set		= nv50_gpio_set;
		engine->gpio.irq_enable		= nv50_gpio_irq_enable;
392
		switch (dev_priv->chipset) {
393 394 395 396 397 398 399 400
		case 0x84:
		case 0x86:
		case 0x92:
		case 0x94:
		case 0x96:
		case 0x98:
		case 0xa0:
		case 0x50:
401 402 403 404
			engine->pm.clock_get	= nv50_pm_clock_get;
			engine->pm.clock_pre	= nv50_pm_clock_pre;
			engine->pm.clock_set	= nv50_pm_clock_set;
			break;
405 406 407 408 409
		default:
			engine->pm.clock_get	= nva3_pm_clock_get;
			engine->pm.clock_pre	= nva3_pm_clock_pre;
			engine->pm.clock_set	= nva3_pm_clock_set;
			break;
410
		}
411 412
		engine->pm.voltage_get		= nouveau_voltage_gpio_get;
		engine->pm.voltage_set		= nouveau_voltage_gpio_set;
413 414 415 416
		if (dev_priv->chipset >= 0x84)
			engine->pm.temp_get	= nv84_temp_get;
		else
			engine->pm.temp_get	= nv40_temp_get;
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
		switch (dev_priv->chipset) {
		case 0x84:
		case 0x86:
		case 0x92:
		case 0x94:
		case 0x96:
		case 0xa0:
			engine->crypt.init	= nv84_crypt_init;
			engine->crypt.takedown	= nv84_crypt_fini;
			engine->crypt.create_context = nv84_crypt_create_context;
			engine->crypt.destroy_context = nv84_crypt_destroy_context;
			break;
		default:
			engine->crypt.init	= nouveau_stub_init;
			engine->crypt.takedown	= nouveau_stub_takedown;
			break;
		}
434
		break;
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
	case 0xC0:
		engine->instmem.init		= nvc0_instmem_init;
		engine->instmem.takedown	= nvc0_instmem_takedown;
		engine->instmem.suspend		= nvc0_instmem_suspend;
		engine->instmem.resume		= nvc0_instmem_resume;
		engine->instmem.populate	= nvc0_instmem_populate;
		engine->instmem.clear		= nvc0_instmem_clear;
		engine->instmem.bind		= nvc0_instmem_bind;
		engine->instmem.unbind		= nvc0_instmem_unbind;
		engine->instmem.flush		= nvc0_instmem_flush;
		engine->mc.init			= nv50_mc_init;
		engine->mc.takedown		= nv50_mc_takedown;
		engine->timer.init		= nv04_timer_init;
		engine->timer.read		= nv04_timer_read;
		engine->timer.takedown		= nv04_timer_takedown;
		engine->fb.init			= nvc0_fb_init;
		engine->fb.takedown		= nvc0_fb_takedown;
		engine->graph.init		= nvc0_graph_init;
		engine->graph.takedown		= nvc0_graph_takedown;
		engine->graph.fifo_access	= nvc0_graph_fifo_access;
		engine->graph.channel		= nvc0_graph_channel;
		engine->graph.create_context	= nvc0_graph_create_context;
		engine->graph.destroy_context	= nvc0_graph_destroy_context;
		engine->graph.load_context	= nvc0_graph_load_context;
		engine->graph.unload_context	= nvc0_graph_unload_context;
		engine->fifo.channels		= 128;
		engine->fifo.init		= nvc0_fifo_init;
		engine->fifo.takedown		= nvc0_fifo_takedown;
		engine->fifo.disable		= nvc0_fifo_disable;
		engine->fifo.enable		= nvc0_fifo_enable;
		engine->fifo.reassign		= nvc0_fifo_reassign;
		engine->fifo.channel_id		= nvc0_fifo_channel_id;
		engine->fifo.create_context	= nvc0_fifo_create_context;
		engine->fifo.destroy_context	= nvc0_fifo_destroy_context;
		engine->fifo.load_context	= nvc0_fifo_load_context;
		engine->fifo.unload_context	= nvc0_fifo_unload_context;
		engine->display.early_init	= nv50_display_early_init;
		engine->display.late_takedown	= nv50_display_late_takedown;
		engine->display.create		= nv50_display_create;
		engine->display.init		= nv50_display_init;
		engine->display.destroy		= nv50_display_destroy;
		engine->gpio.init		= nv50_gpio_init;
		engine->gpio.takedown		= nouveau_stub_takedown;
		engine->gpio.get		= nv50_gpio_get;
		engine->gpio.set		= nv50_gpio_set;
		engine->gpio.irq_enable		= nv50_gpio_irq_enable;
481 482
		engine->crypt.init		= nouveau_stub_init;
		engine->crypt.takedown		= nouveau_stub_takedown;
483
		break;
484 485 486 487 488 489 490 491 492 493 494
	default:
		NV_ERROR(dev, "NV%02x unsupported\n", dev_priv->chipset);
		return 1;
	}

	return 0;
}

static unsigned int
nouveau_vga_set_decode(void *priv, bool state)
{
495 496 497 498 499 500 501 502
	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);

503 504 505 506 507 508 509
	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;
}

510 511 512 513
static int
nouveau_card_init_channel(struct drm_device *dev)
{
	struct drm_nouveau_private *dev_priv = dev->dev_private;
514
	struct nouveau_gpuobj *gpuobj = NULL;
515 516 517
	int ret;

	ret = nouveau_channel_alloc(dev, &dev_priv->channel,
518
				    (struct drm_file *)-2, NvDmaFB, NvDmaTT);
519 520 521 522
	if (ret)
		return ret;

	ret = nouveau_gpuobj_dma_new(dev_priv->channel, NV_CLASS_DMA_IN_MEMORY,
523
				     0, dev_priv->vram_size,
524 525 526 527 528
				     NV_DMA_ACCESS_RW, NV_DMA_TARGET_VIDMEM,
				     &gpuobj);
	if (ret)
		goto out_err;

529 530
	ret = nouveau_ramht_insert(dev_priv->channel, NvDmaVRAM, gpuobj);
	nouveau_gpuobj_ref(NULL, &gpuobj);
531 532 533 534 535 536 537 538 539
	if (ret)
		goto out_err;

	ret = nouveau_gpuobj_gart_dma_new(dev_priv->channel, 0,
					  dev_priv->gart_info.aper_size,
					  NV_DMA_ACCESS_RW, &gpuobj, NULL);
	if (ret)
		goto out_err;

540 541
	ret = nouveau_ramht_insert(dev_priv->channel, NvDmaGART, gpuobj);
	nouveau_gpuobj_ref(NULL, &gpuobj);
542 543 544
	if (ret)
		goto out_err;

545
	mutex_unlock(&dev_priv->channel->mutex);
546
	return 0;
547

548
out_err:
549
	nouveau_channel_put(&dev_priv->channel);
550 551 552
	return ret;
}

553 554 555
static void nouveau_switcheroo_set_state(struct pci_dev *pdev,
					 enum vga_switcheroo_state state)
{
556
	struct drm_device *dev = pci_get_drvdata(pdev);
557 558 559 560
	pm_message_t pmm = { .event = PM_EVENT_SUSPEND };
	if (state == VGA_SWITCHEROO_ON) {
		printk(KERN_ERR "VGA switcheroo: switched nouveau on\n");
		nouveau_pci_resume(pdev);
561
		drm_kms_helper_poll_enable(dev);
562 563
	} else {
		printk(KERN_ERR "VGA switcheroo: switched nouveau off\n");
564
		drm_kms_helper_poll_disable(dev);
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
		nouveau_pci_suspend(pdev, pmm);
	}
}

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;
}

580 581 582 583 584 585 586 587
int
nouveau_card_init(struct drm_device *dev)
{
	struct drm_nouveau_private *dev_priv = dev->dev_private;
	struct nouveau_engine *engine;
	int ret;

	vga_client_register(dev->pdev, dev, NULL, nouveau_vga_set_decode);
588 589
	vga_switcheroo_register_client(dev->pdev, nouveau_switcheroo_set_state,
				       nouveau_switcheroo_can_switch);
590 591 592 593

	/* Initialise internal driver API hooks */
	ret = nouveau_init_engine_ptrs(dev);
	if (ret)
594
		goto out;
595
	engine = &dev_priv->engine;
596
	spin_lock_init(&dev_priv->channels.lock);
597
	spin_lock_init(&dev_priv->context_switch_lock);
598

599 600 601 602 603
	/* Make the CRTCs and I2C buses accessible */
	ret = engine->display.early_init(dev);
	if (ret)
		goto out;

604
	/* Parse BIOS tables / Run init tables if card not POSTed */
605 606
	ret = nouveau_bios_init(dev);
	if (ret)
607
		goto out_display_early;
608

609 610
	nouveau_pm_init(dev);

611
	ret = nouveau_mem_vram_init(dev);
612 613 614
	if (ret)
		goto out_bios;

615
	ret = nouveau_gpuobj_init(dev);
616
	if (ret)
617
		goto out_vram;
618 619 620

	ret = engine->instmem.init(dev);
	if (ret)
621
		goto out_gpuobj;
622

623
	ret = nouveau_mem_gart_init(dev);
624
	if (ret)
625
		goto out_instmem;
626 627 628 629

	/* PMC */
	ret = engine->mc.init(dev);
	if (ret)
630
		goto out_gart;
631

B
Ben Skeggs 已提交
632 633 634 635 636
	/* PGPIO */
	ret = engine->gpio.init(dev);
	if (ret)
		goto out_mc;

637 638 639
	/* PTIMER */
	ret = engine->timer.init(dev);
	if (ret)
B
Ben Skeggs 已提交
640
		goto out_gpio;
641 642 643 644

	/* PFB */
	ret = engine->fb.init(dev);
	if (ret)
645
		goto out_timer;
646

647 648 649 650 651 652 653
	if (nouveau_noaccel)
		engine->graph.accel_blocked = true;
	else {
		/* PGRAPH */
		ret = engine->graph.init(dev);
		if (ret)
			goto out_fb;
654

655 656 657 658 659
		/* PCRYPT */
		ret = engine->crypt.init(dev);
		if (ret)
			goto out_graph;

660 661 662
		/* PFIFO */
		ret = engine->fifo.init(dev);
		if (ret)
663
			goto out_crypt;
664
	}
665

666
	ret = engine->display.create(dev);
667 668 669
	if (ret)
		goto out_fifo;

B
Ben Skeggs 已提交
670
	ret = nouveau_irq_init(dev);
671
	if (ret)
672
		goto out_display;
673 674 675

	ret = drm_vblank_init(dev, 0);
	if (ret)
676
		goto out_irq;
677 678 679

	/* what about PVIDEO/PCRTC/PRAMDAC etc? */

680
	if (!engine->graph.accel_blocked) {
681
		ret = nouveau_fence_init(dev);
682 683
		if (ret)
			goto out_irq;
684 685 686 687

		ret = nouveau_card_init_channel(dev);
		if (ret)
			goto out_fence;
688 689 690 691 692 693
	}

	ret = nouveau_backlight_init(dev);
	if (ret)
		NV_ERROR(dev, "Error %d registering backlight\n", ret);

694 695
	nouveau_fbcon_init(dev);
	drm_kms_helper_poll_init(dev);
696
	return 0;
697

698 699
out_fence:
	nouveau_fence_fini(dev);
700
out_irq:
B
Ben Skeggs 已提交
701
	nouveau_irq_fini(dev);
702
out_display:
703
	engine->display.destroy(dev);
704
out_fifo:
705 706
	if (!nouveau_noaccel)
		engine->fifo.takedown(dev);
707 708 709
out_crypt:
	if (!nouveau_noaccel)
		engine->crypt.takedown(dev);
710
out_graph:
711 712
	if (!nouveau_noaccel)
		engine->graph.takedown(dev);
713 714 715 716
out_fb:
	engine->fb.takedown(dev);
out_timer:
	engine->timer.takedown(dev);
B
Ben Skeggs 已提交
717 718
out_gpio:
	engine->gpio.takedown(dev);
719 720
out_mc:
	engine->mc.takedown(dev);
721 722
out_gart:
	nouveau_mem_gart_fini(dev);
723 724
out_instmem:
	engine->instmem.takedown(dev);
725 726 727 728
out_gpuobj:
	nouveau_gpuobj_takedown(dev);
out_vram:
	nouveau_mem_vram_fini(dev);
729
out_bios:
730
	nouveau_pm_fini(dev);
731
	nouveau_bios_takedown(dev);
732 733
out_display_early:
	engine->display.late_takedown(dev);
734 735 736
out:
	vga_client_register(dev->pdev, NULL, NULL, NULL);
	return ret;
737 738 739 740 741 742 743
}

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;

744
	nouveau_backlight_exit(dev);
745

746 747
	if (!engine->graph.accel_blocked) {
		nouveau_fence_fini(dev);
748
		nouveau_channel_put_unlocked(&dev_priv->channel);
749
	}
750

751 752
	if (!nouveau_noaccel) {
		engine->fifo.takedown(dev);
753
		engine->crypt.takedown(dev);
754 755 756 757
		engine->graph.takedown(dev);
	}
	engine->fb.takedown(dev);
	engine->timer.takedown(dev);
B
Ben Skeggs 已提交
758
	engine->gpio.takedown(dev);
759
	engine->mc.takedown(dev);
760
	engine->display.late_takedown(dev);
761

762 763 764 765
	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);
766
	nouveau_mem_gart_fini(dev);
767

768
	engine->instmem.takedown(dev);
769 770
	nouveau_gpuobj_takedown(dev);
	nouveau_mem_vram_fini(dev);
771

B
Ben Skeggs 已提交
772
	nouveau_irq_fini(dev);
773

774
	nouveau_pm_fini(dev);
775
	nouveau_bios_takedown(dev);
776

777
	vga_client_register(dev->pdev, NULL, NULL, NULL);
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 807 808 809 810 811 812 813 814 815 816
}

/* 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);
}

/* 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
}

817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845
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;
846
	bool primary = false;
847 848 849 850
	dev_priv->apertures = nouveau_get_apertures(dev);
	if (!dev_priv->apertures)
		return -ENOMEM;

851 852 853 854 855
#ifdef CONFIG_X86
	primary = dev->pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW;
#endif
	
	remove_conflicting_framebuffers(dev_priv->apertures, "nouveaufb", primary);
856 857 858
	return 0;
}

859 860 861 862 863
int nouveau_load(struct drm_device *dev, unsigned long flags)
{
	struct drm_nouveau_private *dev_priv;
	uint32_t reg0;
	resource_size_t mmio_start_offs;
864
	int ret;
865 866

	dev_priv = kzalloc(sizeof(*dev_priv), GFP_KERNEL);
867 868 869 870
	if (!dev_priv) {
		ret = -ENOMEM;
		goto err_out;
	}
871 872 873 874 875 876 877 878 879
	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);

	dev_priv->wq = create_workqueue("nouveau");
880 881 882 883
	if (!dev_priv->wq) {
		ret = -EINVAL;
		goto err_priv;
	}
884 885 886 887 888 889 890 891 892 893 894 895

	/* resource 0 is mmio regs */
	/* resource 1 is linear FB */
	/* resource 2 is RAMIN (mmio regs + 0x1000000) */
	/* resource 6 is bios */

	/* map the mmio regs */
	mmio_start_offs = pci_resource_start(dev->pdev, 0);
	dev_priv->mmio = ioremap(mmio_start_offs, 0x00800000);
	if (!dev_priv->mmio) {
		NV_ERROR(dev, "Unable to initialize the mmio mapping. "
			 "Please report your setup to " DRIVER_EMAIL "\n");
896 897
		ret = -EINVAL;
		goto err_wq;
898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918
	}
	NV_DEBUG(dev, "regs mapped ok at 0x%llx\n",
					(unsigned long long)mmio_start_offs);

#ifdef __BIG_ENDIAN
	/* Put the card in BE mode if it's not */
	if (nv_rd32(dev, NV03_PMC_BOOT_1))
		nv_wr32(dev, NV03_PMC_BOOT_1, 0x00000001);

	DRM_MEMORYBARRIER();
#endif

	/* Time to determine the card architecture */
	reg0 = nv_rd32(dev, NV03_PMC_BOOT_0);

	/* We're dealing with >=NV10 */
	if ((reg0 & 0x0f000000) > 0) {
		/* Bit 27-20 contain the architecture in hex */
		dev_priv->chipset = (reg0 & 0xff00000) >> 20;
	/* NV04 or NV05 */
	} else if ((reg0 & 0xff00fff0) == 0x20004000) {
919 920 921 922
		if (reg0 & 0x00f00000)
			dev_priv->chipset = 0x05;
		else
			dev_priv->chipset = 0x04;
923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942
	} else
		dev_priv->chipset = 0xff;

	switch (dev_priv->chipset & 0xf0) {
	case 0x00:
	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;
943 944 945
	case 0xc0:
		dev_priv->card_type = NV_C0;
		break;
946 947
	default:
		NV_INFO(dev, "Unsupported chipset 0x%08x\n", reg0);
948 949
		ret = -EINVAL;
		goto err_mmio;
950 951 952 953 954
	}

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

955 956
	ret = nouveau_remove_conflicting_drivers(dev);
	if (ret)
957
		goto err_mmio;
958

959
	/* Map PRAMIN BAR, or on older cards, the aperture withing BAR0 */
960 961 962 963 964 965
	if (dev_priv->card_type >= NV_40) {
		int ramin_bar = 2;
		if (pci_resource_len(dev->pdev, ramin_bar) == 0)
			ramin_bar = 3;

		dev_priv->ramin_size = pci_resource_len(dev->pdev, ramin_bar);
966 967
		dev_priv->ramin =
			ioremap(pci_resource_start(dev->pdev, ramin_bar),
968 969
				dev_priv->ramin_size);
		if (!dev_priv->ramin) {
970
			NV_ERROR(dev, "Failed to PRAMIN BAR");
971 972
			ret = -ENOMEM;
			goto err_mmio;
973
		}
974
	} else {
975 976
		dev_priv->ramin_size = 1 * 1024 * 1024;
		dev_priv->ramin = ioremap(mmio_start_offs + NV_RAMIN,
977
					  dev_priv->ramin_size);
978 979
		if (!dev_priv->ramin) {
			NV_ERROR(dev, "Failed to map BAR0 PRAMIN.\n");
980 981
			ret = -ENOMEM;
			goto err_mmio;
982 983 984 985 986 987 988 989 990 991 992 993
		}
	}

	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 */
994 995
	ret = nouveau_card_init(dev);
	if (ret)
996
		goto err_ramin;
997 998

	return 0;
999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010

err_ramin:
	iounmap(dev_priv->ramin);
err_mmio:
	iounmap(dev_priv->mmio);
err_wq:
	destroy_workqueue(dev_priv->wq);
err_priv:
	kfree(dev_priv);
	dev->dev_private = NULL;
err_out:
	return ret;
1011 1012 1013 1014 1015 1016 1017 1018 1019
}

void nouveau_lastclose(struct drm_device *dev)
{
}

int nouveau_unload(struct drm_device *dev)
{
	struct drm_nouveau_private *dev_priv = dev->dev_private;
1020
	struct nouveau_engine *engine = &dev_priv->engine;
1021

1022 1023
	drm_kms_helper_poll_fini(dev);
	nouveau_fbcon_fini(dev);
1024
	engine->display.destroy(dev);
1025
	nouveau_card_takedown(dev);
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082

	iounmap(dev_priv->mmio);
	iounmap(dev_priv->ramin);

	kfree(dev_priv);
	dev->dev_private = NULL;
	return 0;
}

int nouveau_ioctl_getparam(struct drm_device *dev, void *data,
						struct drm_file *file_priv)
{
	struct drm_nouveau_private *dev_priv = dev->dev_private;
	struct drm_nouveau_getparam *getparam = data;

	switch (getparam->param) {
	case NOUVEAU_GETPARAM_CHIPSET_ID:
		getparam->value = dev_priv->chipset;
		break;
	case NOUVEAU_GETPARAM_PCI_VENDOR:
		getparam->value = dev->pci_vendor;
		break;
	case NOUVEAU_GETPARAM_PCI_DEVICE:
		getparam->value = dev->pci_device;
		break;
	case NOUVEAU_GETPARAM_BUS_TYPE:
		if (drm_device_is_agp(dev))
			getparam->value = NV_AGP;
		else if (drm_device_is_pcie(dev))
			getparam->value = NV_PCIE;
		else
			getparam->value = NV_PCI;
		break;
	case NOUVEAU_GETPARAM_FB_PHYSICAL:
		getparam->value = dev_priv->fb_phys;
		break;
	case NOUVEAU_GETPARAM_AGP_PHYSICAL:
		getparam->value = dev_priv->gart_info.aper_base;
		break;
	case NOUVEAU_GETPARAM_PCI_PHYSICAL:
		if (dev->sg) {
			getparam->value = (unsigned long)dev->sg->virtual;
		} else {
			NV_ERROR(dev, "Requested PCIGART address, "
					"while no PCIGART was created\n");
			return -EINVAL;
		}
		break;
	case NOUVEAU_GETPARAM_FB_SIZE:
		getparam->value = dev_priv->fb_available_size;
		break;
	case NOUVEAU_GETPARAM_AGP_SIZE:
		getparam->value = dev_priv->gart_info.aper_size;
		break;
	case NOUVEAU_GETPARAM_VM_VRAM_BASE:
		getparam->value = dev_priv->vm_vram_base;
		break;
1083 1084 1085
	case NOUVEAU_GETPARAM_PTIMER_TIME:
		getparam->value = dev_priv->engine.timer.read(dev);
		break;
1086 1087 1088
	case NOUVEAU_GETPARAM_HAS_BO_USAGE:
		getparam->value = 1;
		break;
1089 1090 1091 1092 1093 1094 1095 1096 1097
	case NOUVEAU_GETPARAM_GRAPH_UNITS:
		/* NV40 and NV50 versions are quite different, but register
		 * address is the same. User is supposed to know the card
		 * family anyway... */
		if (dev_priv->chipset >= 0x40) {
			getparam->value = nv_rd32(dev, NV40_PMC_GRAPH_UNITS);
			break;
		}
		/* FALLTHRU */
1098
	default:
1099
		NV_DEBUG(dev, "unknown parameter %lld\n", getparam->param);
1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113
		return -EINVAL;
	}

	return 0;
}

int
nouveau_ioctl_setparam(struct drm_device *dev, void *data,
		       struct drm_file *file_priv)
{
	struct drm_nouveau_setparam *setparam = data;

	switch (setparam->param) {
	default:
1114
		NV_DEBUG(dev, "unknown parameter %lld\n", setparam->param);
1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139
		return -EINVAL;
	}

	return 0;
}

/* Wait until (value(reg) & mask) == val, up until timeout has hit */
bool nouveau_wait_until(struct drm_device *dev, uint64_t timeout,
			uint32_t reg, uint32_t mask, uint32_t val)
{
	struct drm_nouveau_private *dev_priv = dev->dev_private;
	struct nouveau_timer_engine *ptimer = &dev_priv->engine.timer;
	uint64_t start = ptimer->read(dev);

	do {
		if ((nv_rd32(dev, reg) & mask) == val)
			return true;
	} while (ptimer->read(dev) - start < timeout);

	return false;
}

/* Waits for PGRAPH to go completely idle */
bool nouveau_wait_for_idle(struct drm_device *dev)
{
1140 1141 1142 1143 1144 1145 1146
	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)) {
1147 1148 1149 1150 1151 1152 1153 1154
		NV_ERROR(dev, "PGRAPH idle timed out with status 0x%08x\n",
			 nv_rd32(dev, NV04_PGRAPH_STATUS));
		return false;
	}

	return true;
}