nouveau_state.c 40.4 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

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;
56 57 58 59
		engine->instmem.get		= nv04_instmem_get;
		engine->instmem.put		= nv04_instmem_put;
		engine->instmem.map		= nv04_instmem_map;
		engine->instmem.unmap		= nv04_instmem_unmap;
60
		engine->instmem.flush		= nv04_instmem_flush;
61 62 63 64 65 66 67 68 69
		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->fifo.channels		= 16;
		engine->fifo.init		= nv04_fifo_init;
70
		engine->fifo.takedown		= nv04_fifo_fini;
71 72 73
		engine->fifo.disable		= nv04_fifo_disable;
		engine->fifo.enable		= nv04_fifo_enable;
		engine->fifo.reassign		= nv04_fifo_reassign;
74
		engine->fifo.cache_pull		= nv04_fifo_cache_pull;
75 76 77 78 79
		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;
80 81 82 83
		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;
84 85
		engine->display.init		= nv04_display_init;
		engine->display.fini		= nv04_display_fini;
B
Ben Skeggs 已提交
86 87 88 89 90
		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;
91 92 93
		engine->pm.clocks_get		= nv04_pm_clocks_get;
		engine->pm.clocks_pre		= nv04_pm_clocks_pre;
		engine->pm.clocks_set		= nv04_pm_clocks_set;
94
		engine->vram.init		= nouveau_mem_detect;
95
		engine->vram.takedown		= nouveau_stub_takedown;
96
		engine->vram.flags_valid	= nouveau_mem_flags_valid;
97 98 99 100 101 102
		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;
103 104 105 106
		engine->instmem.get		= nv04_instmem_get;
		engine->instmem.put		= nv04_instmem_put;
		engine->instmem.map		= nv04_instmem_map;
		engine->instmem.unmap		= nv04_instmem_unmap;
107
		engine->instmem.flush		= nv04_instmem_flush;
108 109 110 111 112 113 114
		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;
115 116 117
		engine->fb.init_tile_region	= nv10_fb_init_tile_region;
		engine->fb.set_tile_region	= nv10_fb_set_tile_region;
		engine->fb.free_tile_region	= nv10_fb_free_tile_region;
118 119
		engine->fifo.channels		= 32;
		engine->fifo.init		= nv10_fifo_init;
120
		engine->fifo.takedown		= nv04_fifo_fini;
121 122 123
		engine->fifo.disable		= nv04_fifo_disable;
		engine->fifo.enable		= nv04_fifo_enable;
		engine->fifo.reassign		= nv04_fifo_reassign;
124
		engine->fifo.cache_pull		= nv04_fifo_cache_pull;
125 126
		engine->fifo.channel_id		= nv10_fifo_channel_id;
		engine->fifo.create_context	= nv10_fifo_create_context;
127
		engine->fifo.destroy_context	= nv04_fifo_destroy_context;
128 129
		engine->fifo.load_context	= nv10_fifo_load_context;
		engine->fifo.unload_context	= nv10_fifo_unload_context;
130 131 132 133
		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;
134 135
		engine->display.init		= nv04_display_init;
		engine->display.fini		= nv04_display_fini;
B
Ben Skeggs 已提交
136 137 138 139 140
		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;
141 142 143
		engine->pm.clocks_get		= nv04_pm_clocks_get;
		engine->pm.clocks_pre		= nv04_pm_clocks_pre;
		engine->pm.clocks_set		= nv04_pm_clocks_set;
144
		engine->vram.init		= nouveau_mem_detect;
145
		engine->vram.takedown		= nouveau_stub_takedown;
146
		engine->vram.flags_valid	= nouveau_mem_flags_valid;
147 148 149 150 151 152
		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;
153 154 155 156
		engine->instmem.get		= nv04_instmem_get;
		engine->instmem.put		= nv04_instmem_put;
		engine->instmem.map		= nv04_instmem_map;
		engine->instmem.unmap		= nv04_instmem_unmap;
157
		engine->instmem.flush		= nv04_instmem_flush;
158 159 160 161 162 163 164
		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;
165 166 167
		engine->fb.init_tile_region	= nv10_fb_init_tile_region;
		engine->fb.set_tile_region	= nv10_fb_set_tile_region;
		engine->fb.free_tile_region	= nv10_fb_free_tile_region;
168 169
		engine->fifo.channels		= 32;
		engine->fifo.init		= nv10_fifo_init;
170
		engine->fifo.takedown		= nv04_fifo_fini;
171 172 173
		engine->fifo.disable		= nv04_fifo_disable;
		engine->fifo.enable		= nv04_fifo_enable;
		engine->fifo.reassign		= nv04_fifo_reassign;
174
		engine->fifo.cache_pull		= nv04_fifo_cache_pull;
175 176
		engine->fifo.channel_id		= nv10_fifo_channel_id;
		engine->fifo.create_context	= nv10_fifo_create_context;
177
		engine->fifo.destroy_context	= nv04_fifo_destroy_context;
178 179
		engine->fifo.load_context	= nv10_fifo_load_context;
		engine->fifo.unload_context	= nv10_fifo_unload_context;
180 181 182 183
		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;
184 185
		engine->display.init		= nv04_display_init;
		engine->display.fini		= nv04_display_fini;
B
Ben Skeggs 已提交
186 187 188 189 190
		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;
191 192 193
		engine->pm.clocks_get		= nv04_pm_clocks_get;
		engine->pm.clocks_pre		= nv04_pm_clocks_pre;
		engine->pm.clocks_set		= nv04_pm_clocks_set;
194
		engine->vram.init		= nouveau_mem_detect;
195
		engine->vram.takedown		= nouveau_stub_takedown;
196
		engine->vram.flags_valid	= nouveau_mem_flags_valid;
197 198 199 200 201 202
		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;
203 204 205 206
		engine->instmem.get		= nv04_instmem_get;
		engine->instmem.put		= nv04_instmem_put;
		engine->instmem.map		= nv04_instmem_map;
		engine->instmem.unmap		= nv04_instmem_unmap;
207
		engine->instmem.flush		= nv04_instmem_flush;
208 209 210 211 212
		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;
213 214
		engine->fb.init			= nv30_fb_init;
		engine->fb.takedown		= nv30_fb_takedown;
215 216 217
		engine->fb.init_tile_region	= nv30_fb_init_tile_region;
		engine->fb.set_tile_region	= nv10_fb_set_tile_region;
		engine->fb.free_tile_region	= nv30_fb_free_tile_region;
218 219
		engine->fifo.channels		= 32;
		engine->fifo.init		= nv10_fifo_init;
220
		engine->fifo.takedown		= nv04_fifo_fini;
221 222 223
		engine->fifo.disable		= nv04_fifo_disable;
		engine->fifo.enable		= nv04_fifo_enable;
		engine->fifo.reassign		= nv04_fifo_reassign;
224
		engine->fifo.cache_pull		= nv04_fifo_cache_pull;
225 226
		engine->fifo.channel_id		= nv10_fifo_channel_id;
		engine->fifo.create_context	= nv10_fifo_create_context;
227
		engine->fifo.destroy_context	= nv04_fifo_destroy_context;
228 229
		engine->fifo.load_context	= nv10_fifo_load_context;
		engine->fifo.unload_context	= nv10_fifo_unload_context;
230 231 232 233
		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;
234 235
		engine->display.init		= nv04_display_init;
		engine->display.fini		= nv04_display_fini;
B
Ben Skeggs 已提交
236 237 238 239 240
		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;
241 242 243
		engine->pm.clocks_get		= nv04_pm_clocks_get;
		engine->pm.clocks_pre		= nv04_pm_clocks_pre;
		engine->pm.clocks_set		= nv04_pm_clocks_set;
244 245
		engine->pm.voltage_get		= nouveau_voltage_gpio_get;
		engine->pm.voltage_set		= nouveau_voltage_gpio_set;
246
		engine->vram.init		= nouveau_mem_detect;
247
		engine->vram.takedown		= nouveau_stub_takedown;
248
		engine->vram.flags_valid	= nouveau_mem_flags_valid;
249 250 251 252 253 254 255
		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;
256 257 258 259
		engine->instmem.get		= nv04_instmem_get;
		engine->instmem.put		= nv04_instmem_put;
		engine->instmem.map		= nv04_instmem_map;
		engine->instmem.unmap		= nv04_instmem_unmap;
260
		engine->instmem.flush		= nv04_instmem_flush;
261 262 263 264 265 266 267
		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;
268 269 270
		engine->fb.init_tile_region	= nv30_fb_init_tile_region;
		engine->fb.set_tile_region	= nv40_fb_set_tile_region;
		engine->fb.free_tile_region	= nv30_fb_free_tile_region;
271 272
		engine->fifo.channels		= 32;
		engine->fifo.init		= nv40_fifo_init;
273
		engine->fifo.takedown		= nv04_fifo_fini;
274 275 276
		engine->fifo.disable		= nv04_fifo_disable;
		engine->fifo.enable		= nv04_fifo_enable;
		engine->fifo.reassign		= nv04_fifo_reassign;
277
		engine->fifo.cache_pull		= nv04_fifo_cache_pull;
278 279
		engine->fifo.channel_id		= nv10_fifo_channel_id;
		engine->fifo.create_context	= nv40_fifo_create_context;
280
		engine->fifo.destroy_context	= nv04_fifo_destroy_context;
281 282
		engine->fifo.load_context	= nv40_fifo_load_context;
		engine->fifo.unload_context	= nv40_fifo_unload_context;
283 284 285 286
		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;
287 288
		engine->display.init		= nv04_display_init;
		engine->display.fini		= nv04_display_fini;
B
Ben Skeggs 已提交
289 290 291 292 293
		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;
294 295 296
		engine->pm.clocks_get		= nv40_pm_clocks_get;
		engine->pm.clocks_pre		= nv40_pm_clocks_pre;
		engine->pm.clocks_set		= nv40_pm_clocks_set;
297 298
		engine->pm.voltage_get		= nouveau_voltage_gpio_get;
		engine->pm.voltage_set		= nouveau_voltage_gpio_set;
299
		engine->pm.temp_get		= nv40_temp_get;
300 301
		engine->pm.pwm_get		= nv40_pm_pwm_get;
		engine->pm.pwm_set		= nv40_pm_pwm_set;
302
		engine->vram.init		= nouveau_mem_detect;
303
		engine->vram.takedown		= nouveau_stub_takedown;
304
		engine->vram.flags_valid	= nouveau_mem_flags_valid;
305 306 307 308
		break;
	case 0x50:
	case 0x80: /* gotta love NVIDIA's consistency.. */
	case 0x90:
309
	case 0xa0:
310 311 312 313
		engine->instmem.init		= nv50_instmem_init;
		engine->instmem.takedown	= nv50_instmem_takedown;
		engine->instmem.suspend		= nv50_instmem_suspend;
		engine->instmem.resume		= nv50_instmem_resume;
314 315 316 317
		engine->instmem.get		= nv50_instmem_get;
		engine->instmem.put		= nv50_instmem_put;
		engine->instmem.map		= nv50_instmem_map;
		engine->instmem.unmap		= nv50_instmem_unmap;
318 319 320 321
		if (dev_priv->chipset == 0x50)
			engine->instmem.flush	= nv50_instmem_flush;
		else
			engine->instmem.flush	= nv84_instmem_flush;
322 323 324 325 326
		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;
327 328
		engine->fb.init			= nv50_fb_init;
		engine->fb.takedown		= nv50_fb_takedown;
329 330 331 332 333 334 335 336 337 338 339
		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;
340
		engine->fifo.tlb_flush		= nv50_fifo_tlb_flush;
341 342 343 344
		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;
345 346
		engine->display.init		= nv50_display_init;
		engine->display.fini		= nv50_display_fini;
B
Ben Skeggs 已提交
347
		engine->gpio.init		= nv50_gpio_init;
348
		engine->gpio.takedown		= nv50_gpio_fini;
B
Ben Skeggs 已提交
349 350
		engine->gpio.get		= nv50_gpio_get;
		engine->gpio.set		= nv50_gpio_set;
351 352
		engine->gpio.irq_register	= nv50_gpio_irq_register;
		engine->gpio.irq_unregister	= nv50_gpio_irq_unregister;
B
Ben Skeggs 已提交
353
		engine->gpio.irq_enable		= nv50_gpio_irq_enable;
354
		switch (dev_priv->chipset) {
355 356 357 358 359 360 361
		case 0x84:
		case 0x86:
		case 0x92:
		case 0x94:
		case 0x96:
		case 0x98:
		case 0xa0:
362 363
		case 0xaa:
		case 0xac:
364
		case 0x50:
365 366 367
			engine->pm.clocks_get	= nv50_pm_clocks_get;
			engine->pm.clocks_pre	= nv50_pm_clocks_pre;
			engine->pm.clocks_set	= nv50_pm_clocks_set;
368
			break;
369
		default:
370 371 372
			engine->pm.clocks_get	= nva3_pm_clocks_get;
			engine->pm.clocks_pre	= nva3_pm_clocks_pre;
			engine->pm.clocks_set	= nva3_pm_clocks_set;
373
			break;
374
		}
375 376
		engine->pm.voltage_get		= nouveau_voltage_gpio_get;
		engine->pm.voltage_set		= nouveau_voltage_gpio_set;
377 378 379 380
		if (dev_priv->chipset >= 0x84)
			engine->pm.temp_get	= nv84_temp_get;
		else
			engine->pm.temp_get	= nv40_temp_get;
381 382
		engine->pm.pwm_get		= nv50_pm_pwm_get;
		engine->pm.pwm_set		= nv50_pm_pwm_set;
383
		engine->vram.init		= nv50_vram_init;
384
		engine->vram.takedown		= nv50_vram_fini;
385 386 387
		engine->vram.get		= nv50_vram_new;
		engine->vram.put		= nv50_vram_del;
		engine->vram.flags_valid	= nv50_vram_flags_valid;
388
		break;
389
	case 0xc0:
390 391 392 393
		engine->instmem.init		= nvc0_instmem_init;
		engine->instmem.takedown	= nvc0_instmem_takedown;
		engine->instmem.suspend		= nvc0_instmem_suspend;
		engine->instmem.resume		= nvc0_instmem_resume;
394 395 396 397 398
		engine->instmem.get		= nv50_instmem_get;
		engine->instmem.put		= nv50_instmem_put;
		engine->instmem.map		= nv50_instmem_map;
		engine->instmem.unmap		= nv50_instmem_unmap;
		engine->instmem.flush		= nv84_instmem_flush;
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
		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->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.destroy		= nv50_display_destroy;
421 422
		engine->display.init		= nv50_display_init;
		engine->display.fini		= nv50_display_fini;
423 424 425 426
		engine->gpio.init		= nv50_gpio_init;
		engine->gpio.takedown		= nouveau_stub_takedown;
		engine->gpio.get		= nv50_gpio_get;
		engine->gpio.set		= nv50_gpio_set;
427 428
		engine->gpio.irq_register	= nv50_gpio_irq_register;
		engine->gpio.irq_unregister	= nv50_gpio_irq_unregister;
429
		engine->gpio.irq_enable		= nv50_gpio_irq_enable;
430
		engine->vram.init		= nvc0_vram_init;
431
		engine->vram.takedown		= nv50_vram_fini;
432 433 434
		engine->vram.get		= nvc0_vram_new;
		engine->vram.put		= nv50_vram_del;
		engine->vram.flags_valid	= nvc0_vram_flags_valid;
435
		engine->pm.temp_get		= nv84_temp_get;
436
		engine->pm.clocks_get		= nvc0_pm_clocks_get;
B
Ben Skeggs 已提交
437
		engine->pm.voltage_get		= nouveau_voltage_gpio_get;
438
		engine->pm.voltage_set		= nouveau_voltage_gpio_set;
439 440
		engine->pm.pwm_get		= nv50_pm_pwm_get;
		engine->pm.pwm_set		= nv50_pm_pwm_set;
441
		break;
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
	case 0xd0:
		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.get		= nv50_instmem_get;
		engine->instmem.put		= nv50_instmem_put;
		engine->instmem.map		= nv50_instmem_map;
		engine->instmem.unmap		= nv50_instmem_unmap;
		engine->instmem.flush		= nv84_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->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	= nouveau_stub_init;
		engine->display.late_takedown	= nouveau_stub_takedown;
472 473
		engine->display.create		= nvd0_display_create;
		engine->display.destroy		= nvd0_display_destroy;
474 475
		engine->display.init		= nvd0_display_init;
		engine->display.fini		= nvd0_display_fini;
476
		engine->gpio.init		= nv50_gpio_init;
477
		engine->gpio.takedown		= nouveau_stub_takedown;
478 479 480 481 482
		engine->gpio.get		= nvd0_gpio_get;
		engine->gpio.set		= nvd0_gpio_set;
		engine->gpio.irq_register	= nv50_gpio_irq_register;
		engine->gpio.irq_unregister	= nv50_gpio_irq_unregister;
		engine->gpio.irq_enable		= nv50_gpio_irq_enable;
483 484 485 486 487
		engine->vram.init		= nvc0_vram_init;
		engine->vram.takedown		= nv50_vram_fini;
		engine->vram.get		= nvc0_vram_new;
		engine->vram.put		= nv50_vram_del;
		engine->vram.flags_valid	= nvc0_vram_flags_valid;
488
		engine->pm.temp_get		= nv84_temp_get;
489 490 491
		engine->pm.clocks_get		= nvc0_pm_clocks_get;
		engine->pm.voltage_get		= nouveau_voltage_gpio_get;
		engine->pm.voltage_set		= nouveau_voltage_gpio_set;
492
		break;
493 494 495 496 497
	default:
		NV_ERROR(dev, "NV%02x unsupported\n", dev_priv->chipset);
		return 1;
	}

498 499 500 501 502 503 504 505 506
	/* 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;
	}

507 508 509 510 511 512
	return 0;
}

static unsigned int
nouveau_vga_set_decode(void *priv, bool state)
{
513 514 515 516 517 518 519 520
	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);

521 522 523 524 525 526 527
	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;
}

528 529 530
static void nouveau_switcheroo_set_state(struct pci_dev *pdev,
					 enum vga_switcheroo_state state)
{
531
	struct drm_device *dev = pci_get_drvdata(pdev);
532 533 534
	pm_message_t pmm = { .event = PM_EVENT_SUSPEND };
	if (state == VGA_SWITCHEROO_ON) {
		printk(KERN_ERR "VGA switcheroo: switched nouveau on\n");
535
		dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
536
		nouveau_pci_resume(pdev);
537
		drm_kms_helper_poll_enable(dev);
538
		dev->switch_power_state = DRM_SWITCH_POWER_ON;
539 540
	} else {
		printk(KERN_ERR "VGA switcheroo: switched nouveau off\n");
541
		dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
542
		drm_kms_helper_poll_disable(dev);
543
		nouveau_pci_suspend(pdev, pmm);
544
		dev->switch_power_state = DRM_SWITCH_POWER_OFF;
545 546 547
	}
}

548 549 550 551 552 553
static void nouveau_switcheroo_reprobe(struct pci_dev *pdev)
{
	struct drm_device *dev = pci_get_drvdata(pdev);
	nouveau_fbcon_output_poll_changed(dev);
}

554 555 556 557 558 559 560 561 562 563 564
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;
}

565 566 567 568 569
int
nouveau_card_init(struct drm_device *dev)
{
	struct drm_nouveau_private *dev_priv = dev->dev_private;
	struct nouveau_engine *engine;
570
	int ret, e = 0;
571 572

	vga_client_register(dev->pdev, dev, NULL, nouveau_vga_set_decode);
573
	vga_switcheroo_register_client(dev->pdev, nouveau_switcheroo_set_state,
574
				       nouveau_switcheroo_reprobe,
575
				       nouveau_switcheroo_can_switch);
576 577 578 579

	/* Initialise internal driver API hooks */
	ret = nouveau_init_engine_ptrs(dev);
	if (ret)
580
		goto out;
581
	engine = &dev_priv->engine;
582
	spin_lock_init(&dev_priv->channels.lock);
583
	spin_lock_init(&dev_priv->tile.lock);
584
	spin_lock_init(&dev_priv->context_switch_lock);
585
	spin_lock_init(&dev_priv->vm_lock);
586

587 588 589 590 591
	/* Make the CRTCs and I2C buses accessible */
	ret = engine->display.early_init(dev);
	if (ret)
		goto out;

592
	/* Parse BIOS tables / Run init tables if card not POSTed */
593 594
	ret = nouveau_bios_init(dev);
	if (ret)
595
		goto out_display_early;
596

597 598 599 600 601 602 603 604
	/* 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);
	}

605 606
	nouveau_pm_init(dev);

607
	ret = engine->vram.init(dev);
608 609 610
	if (ret)
		goto out_bios;

611
	ret = nouveau_gpuobj_init(dev);
612
	if (ret)
613
		goto out_vram;
614 615 616

	ret = engine->instmem.init(dev);
	if (ret)
617
		goto out_gpuobj;
618

619
	ret = nouveau_mem_vram_init(dev);
620
	if (ret)
621
		goto out_instmem;
622

623 624 625 626
	ret = nouveau_mem_gart_init(dev);
	if (ret)
		goto out_ttmvram;

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
	if (!dev_priv->noaccel) {
648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665
		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:
666
		case NV_D0:
667 668 669 670 671
			nvc0_graph_create(dev);
			break;
		default:
			break;
		}
672

673
		switch (dev_priv->chipset) {
674 675 676 677 678 679 680
		case 0x84:
		case 0x86:
		case 0x92:
		case 0x94:
		case 0x96:
		case 0xa0:
			nv84_crypt_create(dev);
681
			break;
682 683 684 685 686
		case 0x98:
		case 0xaa:
		case 0xac:
			nv98_crypt_create(dev);
			break;
687 688
		}

689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707
		switch (dev_priv->card_type) {
		case NV_50:
			switch (dev_priv->chipset) {
			case 0xa3:
			case 0xa5:
			case 0xa8:
			case 0xaf:
				nva3_copy_create(dev);
				break;
			}
			break;
		case NV_C0:
			nvc0_copy_create(dev, 0);
			nvc0_copy_create(dev, 1);
			break;
		default:
			break;
		}

708 709 710 711 712 713 714 715 716 717 718 719 720
		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
721 722 723
		if (dev_priv->card_type == NV_40 ||
		    dev_priv->chipset == 0x31 ||
		    dev_priv->chipset == 0x34 ||
724
		    dev_priv->chipset == 0x36) {
725
			nv31_mpeg_create(dev);
726
		}
B
Ben Skeggs 已提交
727

728 729 730 731 732 733 734 735
		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;
			}
		}

736 737 738
		/* PFIFO */
		ret = engine->fifo.init(dev);
		if (ret)
739
			goto out_engine;
740
	}
741

742 743 744 745
	ret = nouveau_irq_init(dev);
	if (ret)
		goto out_fifo;

746
	ret = nouveau_display_create(dev);
747
	if (ret)
748
		goto out_irq;
749

750 751
	nouveau_backlight_init(dev);

752
	if (dev_priv->eng[NVOBJ_ENGINE_GR]) {
753
		ret = nouveau_fence_init(dev);
754
		if (ret)
755
			goto out_disp;
756

757 758
		ret = nouveau_channel_alloc(dev, &dev_priv->channel, NULL,
					    NvDmaFB, NvDmaTT);
759 760
		if (ret)
			goto out_fence;
761 762 763 764 765

		mutex_unlock(&dev_priv->channel->mutex);
	}

	if (dev->mode_config.num_crtc) {
766
		ret = nouveau_display_init(dev);
767 768 769 770
		if (ret)
			goto out_chan;

		nouveau_fbcon_init(dev);
771 772 773
	}

	return 0;
774

775 776
out_chan:
	nouveau_channel_put_unlocked(&dev_priv->channel);
777 778
out_fence:
	nouveau_fence_fini(dev);
779
out_disp:
780
	nouveau_backlight_exit(dev);
781
	nouveau_display_destroy(dev);
782
out_irq:
B
Ben Skeggs 已提交
783
	nouveau_irq_fini(dev);
784
out_fifo:
785
	if (!dev_priv->noaccel)
786
		engine->fifo.takedown(dev);
787
out_engine:
788
	if (!dev_priv->noaccel) {
789
		for (e = e - 1; e >= 0; e--) {
790 791
			if (!dev_priv->eng[e])
				continue;
792
			dev_priv->eng[e]->fini(dev, e, false);
793
			dev_priv->eng[e]->destroy(dev,e );
794 795 796
		}
	}

797 798 799
	engine->fb.takedown(dev);
out_timer:
	engine->timer.takedown(dev);
B
Ben Skeggs 已提交
800 801
out_gpio:
	engine->gpio.takedown(dev);
802 803
out_mc:
	engine->mc.takedown(dev);
804 805
out_gart:
	nouveau_mem_gart_fini(dev);
806 807
out_ttmvram:
	nouveau_mem_vram_fini(dev);
808 809
out_instmem:
	engine->instmem.takedown(dev);
810 811 812
out_gpuobj:
	nouveau_gpuobj_takedown(dev);
out_vram:
813
	engine->vram.takedown(dev);
814
out_bios:
815
	nouveau_pm_fini(dev);
816
	nouveau_bios_takedown(dev);
817 818
out_display_early:
	engine->display.late_takedown(dev);
819 820 821
out:
	vga_client_register(dev->pdev, NULL, NULL, NULL);
	return ret;
822 823 824 825 826 827
}

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;
828
	int e;
829

830 831
	if (dev->mode_config.num_crtc) {
		nouveau_fbcon_fini(dev);
832
		nouveau_display_fini(dev);
833
	}
834

835
	if (dev_priv->channel) {
836
		nouveau_channel_put_unlocked(&dev_priv->channel);
837
		nouveau_fence_fini(dev);
838
	}
839

840
	nouveau_backlight_exit(dev);
841
	nouveau_display_destroy(dev);
842

843
	if (!dev_priv->noaccel) {
844
		engine->fifo.takedown(dev);
845 846
		for (e = NVOBJ_ENGINE_NR - 1; e >= 0; e--) {
			if (dev_priv->eng[e]) {
847
				dev_priv->eng[e]->fini(dev, e, false);
848 849 850
				dev_priv->eng[e]->destroy(dev,e );
			}
		}
851 852 853
	}
	engine->fb.takedown(dev);
	engine->timer.takedown(dev);
B
Ben Skeggs 已提交
854
	engine->gpio.takedown(dev);
855
	engine->mc.takedown(dev);
856
	engine->display.late_takedown(dev);
857

858 859 860 861 862
	if (dev_priv->vga_ram) {
		nouveau_bo_unpin(dev_priv->vga_ram);
		nouveau_bo_ref(NULL, &dev_priv->vga_ram);
	}

863 864 865 866
	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);
867
	nouveau_mem_gart_fini(dev);
868
	nouveau_mem_vram_fini(dev);
869

870
	engine->instmem.takedown(dev);
871
	nouveau_gpuobj_takedown(dev);
872
	engine->vram.takedown(dev);
873

B
Ben Skeggs 已提交
874
	nouveau_irq_fini(dev);
875

876
	nouveau_pm_fini(dev);
877
	nouveau_bios_takedown(dev);
878

879
	vga_client_register(dev->pdev, NULL, NULL, NULL);
880 881
}

882 883 884
int
nouveau_open(struct drm_device *dev, struct drm_file *file_priv)
{
885
	struct drm_nouveau_private *dev_priv = dev->dev_private;
886
	struct nouveau_fpriv *fpriv;
887
	int ret;
888 889 890 891 892 893

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

	spin_lock_init(&fpriv->lock);
894 895
	INIT_LIST_HEAD(&fpriv->channels);

896 897 898 899 900 901 902 903 904
	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) {
905 906 907 908 909 910
		ret = nouveau_vm_new(dev, 0, (1ULL << 40), 0x0008000000ULL,
				     &fpriv->vm);
		if (ret) {
			kfree(fpriv);
			return ret;
		}
911
	}
912

913 914 915 916
	file_priv->driver_priv = fpriv;
	return 0;
}

917 918 919 920 921 922 923
/* 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);
}

924 925 926 927
void
nouveau_postclose(struct drm_device *dev, struct drm_file *file_priv)
{
	struct nouveau_fpriv *fpriv = nouveau_fpriv(file_priv);
928
	nouveau_vm_ref(NULL, &fpriv->vm, NULL);
929 930 931
	kfree(fpriv);
}

932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961
/* 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
}

962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990
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;
991
	bool primary = false;
992 993 994 995
	dev_priv->apertures = nouveau_get_apertures(dev);
	if (!dev_priv->apertures)
		return -ENOMEM;

996 997 998
#ifdef CONFIG_X86
	primary = dev->pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW;
#endif
999

1000
	remove_conflicting_framebuffers(dev_priv->apertures, "nouveaufb", primary);
1001 1002 1003
	return 0;
}

1004 1005 1006
int nouveau_load(struct drm_device *dev, unsigned long flags)
{
	struct drm_nouveau_private *dev_priv;
1007
	uint32_t reg0, strap;
1008
	resource_size_t mmio_start_offs;
1009
	int ret;
1010 1011

	dev_priv = kzalloc(sizeof(*dev_priv), GFP_KERNEL);
1012 1013 1014 1015
	if (!dev_priv) {
		ret = -ENOMEM;
		goto err_out;
	}
1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
	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);

	/* 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");
1035
		ret = -EINVAL;
1036
		goto err_priv;
1037 1038 1039 1040 1041 1042
	}
	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 */
B
Ben Skeggs 已提交
1043 1044
	if (nv_rd32(dev, NV03_PMC_BOOT_1) != 0x01000001)
		nv_wr32(dev, NV03_PMC_BOOT_1, 0x01000001);
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057

	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) {
1058 1059 1060 1061
		if (reg0 & 0x00f00000)
			dev_priv->chipset = 0x05;
		else
			dev_priv->chipset = 0x04;
1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081
	} 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;
1082 1083 1084
	case 0xc0:
		dev_priv->card_type = NV_C0;
		break;
1085 1086 1087
	case 0xd0:
		dev_priv->card_type = NV_D0;
		break;
1088 1089
	default:
		NV_INFO(dev, "Unsupported chipset 0x%08x\n", reg0);
1090 1091
		ret = -EINVAL;
		goto err_mmio;
1092 1093 1094 1095 1096
	}

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

1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113
	/* 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);

1114 1115 1116 1117 1118 1119 1120
	/* 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) {
1121
		case 0xd9: /* known broken */
1122 1123
			NV_INFO(dev, "acceleration disabled by default, pass "
				     "noaccel=0 to force enable\n");
1124 1125 1126 1127 1128 1129 1130 1131
			dev_priv->noaccel = true;
			break;
		default:
			dev_priv->noaccel = false;
			break;
		}
	}

1132 1133
	ret = nouveau_remove_conflicting_drivers(dev);
	if (ret)
1134
		goto err_mmio;
1135

L
Lucas De Marchi 已提交
1136
	/* Map PRAMIN BAR, or on older cards, the aperture within BAR0 */
1137 1138 1139 1140 1141 1142
	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);
1143 1144
		dev_priv->ramin =
			ioremap(pci_resource_start(dev->pdev, ramin_bar),
1145 1146
				dev_priv->ramin_size);
		if (!dev_priv->ramin) {
1147
			NV_ERROR(dev, "Failed to map PRAMIN BAR\n");
1148 1149
			ret = -ENOMEM;
			goto err_mmio;
1150
		}
1151
	} else {
1152 1153
		dev_priv->ramin_size = 1 * 1024 * 1024;
		dev_priv->ramin = ioremap(mmio_start_offs + NV_RAMIN,
1154
					  dev_priv->ramin_size);
1155 1156
		if (!dev_priv->ramin) {
			NV_ERROR(dev, "Failed to map BAR0 PRAMIN.\n");
1157 1158
			ret = -ENOMEM;
			goto err_mmio;
1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170
		}
	}

	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 */
1171 1172
	ret = nouveau_card_init(dev);
	if (ret)
1173
		goto err_ramin;
1174 1175

	return 0;
1176 1177 1178 1179 1180 1181 1182 1183 1184 1185

err_ramin:
	iounmap(dev_priv->ramin);
err_mmio:
	iounmap(dev_priv->mmio);
err_priv:
	kfree(dev_priv);
	dev->dev_private = NULL;
err_out:
	return ret;
1186 1187 1188 1189
}

void nouveau_lastclose(struct drm_device *dev)
{
1190
	vga_switcheroo_process_delayed_switch();
1191 1192 1193 1194 1195 1196
}

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

1197
	nouveau_card_takedown(dev);
1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223

	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:
1224
		if (drm_pci_device_is_agp(dev))
1225
			getparam->value = NV_AGP;
J
Jon Mason 已提交
1226
		else if (pci_is_pcie(dev->pdev))
1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237
			getparam->value = NV_PCIE;
		else
			getparam->value = NV_PCI;
		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:
1238
		getparam->value = 0; /* deprecated */
1239
		break;
1240 1241 1242
	case NOUVEAU_GETPARAM_PTIMER_TIME:
		getparam->value = dev_priv->engine.timer.read(dev);
		break;
1243 1244 1245
	case NOUVEAU_GETPARAM_HAS_BO_USAGE:
		getparam->value = 1;
		break;
1246
	case NOUVEAU_GETPARAM_HAS_PAGEFLIP:
1247
		getparam->value = dev_priv->card_type < NV_D0;
1248
		break;
1249 1250 1251 1252 1253 1254 1255 1256 1257
	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 */
1258
	default:
1259
		NV_DEBUG(dev, "unknown parameter %lld\n", getparam->param);
1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273
		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:
1274
		NV_DEBUG(dev, "unknown parameter %lld\n", setparam->param);
1275 1276 1277 1278 1279 1280 1281
		return -EINVAL;
	}

	return 0;
}

/* Wait until (value(reg) & mask) == val, up until timeout has hit */
1282 1283 1284
bool
nouveau_wait_eq(struct drm_device *dev, uint64_t timeout,
		uint32_t reg, uint32_t mask, uint32_t val)
1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297
{
	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;
}

1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314
/* Wait until (value(reg) & mask) != val, up until timeout has hit */
bool
nouveau_wait_ne(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;
}

1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331
/* Wait until cond(data) == true, up until timeout has hit */
bool
nouveau_wait_cb(struct drm_device *dev, u64 timeout,
		bool (*cond)(void *), void *data)
{
	struct drm_nouveau_private *dev_priv = dev->dev_private;
	struct nouveau_timer_engine *ptimer = &dev_priv->engine.timer;
	u64 start = ptimer->read(dev);

	do {
		if (cond(data) == true)
			return true;
	} while (ptimer->read(dev) - start < timeout);

	return false;
}

1332 1333 1334
/* Waits for PGRAPH to go completely idle */
bool nouveau_wait_for_idle(struct drm_device *dev)
{
1335 1336 1337 1338 1339 1340 1341
	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)) {
1342 1343 1344 1345 1346 1347 1348 1349
		NV_ERROR(dev, "PGRAPH idle timed out with status 0x%08x\n",
			 nv_rd32(dev, NV04_PGRAPH_STATUS));
		return false;
	}

	return true;
}