nouveau_state.c 33.7 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) {}
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
84
		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;
85
86
87
88
89
		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;
90
91
92
		engine->pm.clock_get		= nv04_pm_clock_get;
		engine->pm.clock_pre		= nv04_pm_clock_pre;
		engine->pm.clock_set		= nv04_pm_clock_set;
93
94
		engine->vram.init		= nouveau_mem_detect;
		engine->vram.flags_valid	= nouveau_mem_flags_valid;
95
96
97
98
99
100
		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;
101
102
103
104
		engine->instmem.get		= nv04_instmem_get;
		engine->instmem.put		= nv04_instmem_put;
		engine->instmem.map		= nv04_instmem_map;
		engine->instmem.unmap		= nv04_instmem_unmap;
105
		engine->instmem.flush		= nv04_instmem_flush;
106
107
108
109
110
111
112
		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;
113
114
115
		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;
116
117
		engine->fifo.channels		= 32;
		engine->fifo.init		= nv10_fifo_init;
118
		engine->fifo.takedown		= nv04_fifo_fini;
119
120
121
		engine->fifo.disable		= nv04_fifo_disable;
		engine->fifo.enable		= nv04_fifo_enable;
		engine->fifo.reassign		= nv04_fifo_reassign;
122
		engine->fifo.cache_pull		= nv04_fifo_cache_pull;
123
124
		engine->fifo.channel_id		= nv10_fifo_channel_id;
		engine->fifo.create_context	= nv10_fifo_create_context;
125
		engine->fifo.destroy_context	= nv04_fifo_destroy_context;
126
127
		engine->fifo.load_context	= nv10_fifo_load_context;
		engine->fifo.unload_context	= nv10_fifo_unload_context;
128
129
130
131
132
		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;
133
134
135
136
137
		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;
138
139
140
		engine->pm.clock_get		= nv04_pm_clock_get;
		engine->pm.clock_pre		= nv04_pm_clock_pre;
		engine->pm.clock_set		= nv04_pm_clock_set;
141
142
		engine->vram.init		= nouveau_mem_detect;
		engine->vram.flags_valid	= nouveau_mem_flags_valid;
143
144
145
146
147
148
		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;
149
150
151
152
		engine->instmem.get		= nv04_instmem_get;
		engine->instmem.put		= nv04_instmem_put;
		engine->instmem.map		= nv04_instmem_map;
		engine->instmem.unmap		= nv04_instmem_unmap;
153
		engine->instmem.flush		= nv04_instmem_flush;
154
155
156
157
158
159
160
		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;
161
162
163
		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;
164
165
		engine->fifo.channels		= 32;
		engine->fifo.init		= nv10_fifo_init;
166
		engine->fifo.takedown		= nv04_fifo_fini;
167
168
169
		engine->fifo.disable		= nv04_fifo_disable;
		engine->fifo.enable		= nv04_fifo_enable;
		engine->fifo.reassign		= nv04_fifo_reassign;
170
		engine->fifo.cache_pull		= nv04_fifo_cache_pull;
171
172
		engine->fifo.channel_id		= nv10_fifo_channel_id;
		engine->fifo.create_context	= nv10_fifo_create_context;
173
		engine->fifo.destroy_context	= nv04_fifo_destroy_context;
174
175
		engine->fifo.load_context	= nv10_fifo_load_context;
		engine->fifo.unload_context	= nv10_fifo_unload_context;
176
177
178
179
180
		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;
181
182
183
184
185
		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;
186
187
188
		engine->pm.clock_get		= nv04_pm_clock_get;
		engine->pm.clock_pre		= nv04_pm_clock_pre;
		engine->pm.clock_set		= nv04_pm_clock_set;
189
190
		engine->vram.init		= nouveau_mem_detect;
		engine->vram.flags_valid	= nouveau_mem_flags_valid;
191
192
193
194
195
196
		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;
197
198
199
200
		engine->instmem.get		= nv04_instmem_get;
		engine->instmem.put		= nv04_instmem_put;
		engine->instmem.map		= nv04_instmem_map;
		engine->instmem.unmap		= nv04_instmem_unmap;
201
		engine->instmem.flush		= nv04_instmem_flush;
202
203
204
205
206
		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;
207
208
		engine->fb.init			= nv30_fb_init;
		engine->fb.takedown		= nv30_fb_takedown;
209
210
211
		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;
212
213
		engine->fifo.channels		= 32;
		engine->fifo.init		= nv10_fifo_init;
214
		engine->fifo.takedown		= nv04_fifo_fini;
215
216
217
		engine->fifo.disable		= nv04_fifo_disable;
		engine->fifo.enable		= nv04_fifo_enable;
		engine->fifo.reassign		= nv04_fifo_reassign;
218
		engine->fifo.cache_pull		= nv04_fifo_cache_pull;
219
220
		engine->fifo.channel_id		= nv10_fifo_channel_id;
		engine->fifo.create_context	= nv10_fifo_create_context;
221
		engine->fifo.destroy_context	= nv04_fifo_destroy_context;
222
223
		engine->fifo.load_context	= nv10_fifo_load_context;
		engine->fifo.unload_context	= nv10_fifo_unload_context;
224
225
226
227
228
		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;
229
230
231
232
233
		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;
234
235
236
237
238
		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;
239
240
		engine->vram.init		= nouveau_mem_detect;
		engine->vram.flags_valid	= nouveau_mem_flags_valid;
241
242
243
244
245
246
247
		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;
248
249
250
251
		engine->instmem.get		= nv04_instmem_get;
		engine->instmem.put		= nv04_instmem_put;
		engine->instmem.map		= nv04_instmem_map;
		engine->instmem.unmap		= nv04_instmem_unmap;
252
		engine->instmem.flush		= nv04_instmem_flush;
253
254
255
256
257
258
259
		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;
260
261
262
		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;
263
264
		engine->fifo.channels		= 32;
		engine->fifo.init		= nv40_fifo_init;
265
		engine->fifo.takedown		= nv04_fifo_fini;
266
267
268
		engine->fifo.disable		= nv04_fifo_disable;
		engine->fifo.enable		= nv04_fifo_enable;
		engine->fifo.reassign		= nv04_fifo_reassign;
269
		engine->fifo.cache_pull		= nv04_fifo_cache_pull;
270
271
		engine->fifo.channel_id		= nv10_fifo_channel_id;
		engine->fifo.create_context	= nv40_fifo_create_context;
272
		engine->fifo.destroy_context	= nv04_fifo_destroy_context;
273
274
		engine->fifo.load_context	= nv40_fifo_load_context;
		engine->fifo.unload_context	= nv40_fifo_unload_context;
275
276
277
278
279
		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;
280
281
282
283
284
		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;
285
286
287
288
289
		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;
290
		engine->pm.temp_get		= nv40_temp_get;
291
292
		engine->vram.init		= nouveau_mem_detect;
		engine->vram.flags_valid	= nouveau_mem_flags_valid;
293
294
295
296
297
298
299
300
301
		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;
302
303
304
305
		engine->instmem.get		= nv50_instmem_get;
		engine->instmem.put		= nv50_instmem_put;
		engine->instmem.map		= nv50_instmem_map;
		engine->instmem.unmap		= nv50_instmem_unmap;
306
307
308
309
		if (dev_priv->chipset == 0x50)
			engine->instmem.flush	= nv50_instmem_flush;
		else
			engine->instmem.flush	= nv84_instmem_flush;
310
311
312
313
314
		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;
315
316
		engine->fb.init			= nv50_fb_init;
		engine->fb.takedown		= nv50_fb_takedown;
317
318
319
320
321
322
323
324
325
326
327
		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;
328
		engine->fifo.tlb_flush		= nv50_fifo_tlb_flush;
329
330
331
332
333
		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;
334
		engine->gpio.init		= nv50_gpio_init;
335
		engine->gpio.takedown		= nv50_gpio_fini;
336
337
		engine->gpio.get		= nv50_gpio_get;
		engine->gpio.set		= nv50_gpio_set;
338
339
		engine->gpio.irq_register	= nv50_gpio_irq_register;
		engine->gpio.irq_unregister	= nv50_gpio_irq_unregister;
340
		engine->gpio.irq_enable		= nv50_gpio_irq_enable;
341
		switch (dev_priv->chipset) {
342
343
344
345
346
347
348
		case 0x84:
		case 0x86:
		case 0x92:
		case 0x94:
		case 0x96:
		case 0x98:
		case 0xa0:
349
350
		case 0xaa:
		case 0xac:
351
		case 0x50:
352
353
354
355
			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;
356
357
358
359
360
		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;
361
		}
362
363
		engine->pm.voltage_get		= nouveau_voltage_gpio_get;
		engine->pm.voltage_set		= nouveau_voltage_gpio_set;
364
365
366
367
		if (dev_priv->chipset >= 0x84)
			engine->pm.temp_get	= nv84_temp_get;
		else
			engine->pm.temp_get	= nv40_temp_get;
368
369
370
371
		engine->vram.init		= nv50_vram_init;
		engine->vram.get		= nv50_vram_new;
		engine->vram.put		= nv50_vram_del;
		engine->vram.flags_valid	= nv50_vram_flags_valid;
372
		break;
373
374
375
376
377
	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;
378
379
380
381
382
		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;
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
		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.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;
410
411
		engine->gpio.irq_register	= nv50_gpio_irq_register;
		engine->gpio.irq_unregister	= nv50_gpio_irq_unregister;
412
		engine->gpio.irq_enable		= nv50_gpio_irq_enable;
413
414
415
416
		engine->vram.init		= nvc0_vram_init;
		engine->vram.get		= nvc0_vram_new;
		engine->vram.put		= nv50_vram_del;
		engine->vram.flags_valid	= nvc0_vram_flags_valid;
417
		break;
418
419
420
421
422
423
424
425
426
427
428
	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)
{
429
430
431
432
433
434
435
436
	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);

437
438
439
440
441
442
443
	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;
}

444
445
446
447
448
449
450
static int
nouveau_card_init_channel(struct drm_device *dev)
{
	struct drm_nouveau_private *dev_priv = dev->dev_private;
	int ret;

	ret = nouveau_channel_alloc(dev, &dev_priv->channel,
451
				    (struct drm_file *)-2, NvDmaFB, NvDmaTT);
452
453
454
	if (ret)
		return ret;

455
	mutex_unlock(&dev_priv->channel->mutex);
456
457
458
	return 0;
}

459
460
461
static void nouveau_switcheroo_set_state(struct pci_dev *pdev,
					 enum vga_switcheroo_state state)
{
462
	struct drm_device *dev = pci_get_drvdata(pdev);
463
464
465
	pm_message_t pmm = { .event = PM_EVENT_SUSPEND };
	if (state == VGA_SWITCHEROO_ON) {
		printk(KERN_ERR "VGA switcheroo: switched nouveau on\n");
466
		dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
467
		nouveau_pci_resume(pdev);
468
		drm_kms_helper_poll_enable(dev);
469
		dev->switch_power_state = DRM_SWITCH_POWER_ON;
470
471
	} else {
		printk(KERN_ERR "VGA switcheroo: switched nouveau off\n");
472
		dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
473
		drm_kms_helper_poll_disable(dev);
474
		nouveau_pci_suspend(pdev, pmm);
475
		dev->switch_power_state = DRM_SWITCH_POWER_OFF;
476
477
478
	}
}

479
480
481
482
483
484
static void nouveau_switcheroo_reprobe(struct pci_dev *pdev)
{
	struct drm_device *dev = pci_get_drvdata(pdev);
	nouveau_fbcon_output_poll_changed(dev);
}

485
486
487
488
489
490
491
492
493
494
495
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;
}

496
497
498
499
500
int
nouveau_card_init(struct drm_device *dev)
{
	struct drm_nouveau_private *dev_priv = dev->dev_private;
	struct nouveau_engine *engine;
501
	int ret, e;
502
503

	vga_client_register(dev->pdev, dev, NULL, nouveau_vga_set_decode);
504
	vga_switcheroo_register_client(dev->pdev, nouveau_switcheroo_set_state,
505
				       nouveau_switcheroo_reprobe,
506
				       nouveau_switcheroo_can_switch);
507
508
509
510

	/* Initialise internal driver API hooks */
	ret = nouveau_init_engine_ptrs(dev);
	if (ret)
511
		goto out;
512
	engine = &dev_priv->engine;
513
	spin_lock_init(&dev_priv->channels.lock);
514
	spin_lock_init(&dev_priv->tile.lock);
515
	spin_lock_init(&dev_priv->context_switch_lock);
516
	spin_lock_init(&dev_priv->vm_lock);
517

518
519
520
521
522
	/* Make the CRTCs and I2C buses accessible */
	ret = engine->display.early_init(dev);
	if (ret)
		goto out;

523
	/* Parse BIOS tables / Run init tables if card not POSTed */
524
525
	ret = nouveau_bios_init(dev);
	if (ret)
526
		goto out_display_early;
527

528
529
	nouveau_pm_init(dev);

530
	ret = nouveau_mem_vram_init(dev);
531
532
533
	if (ret)
		goto out_bios;

534
	ret = nouveau_gpuobj_init(dev);
535
	if (ret)
536
		goto out_vram;
537
538
539

	ret = engine->instmem.init(dev);
	if (ret)
540
		goto out_gpuobj;
541

542
	ret = nouveau_mem_gart_init(dev);
543
	if (ret)
544
		goto out_instmem;
545
546
547
548

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

551
552
553
554
555
	/* PGPIO */
	ret = engine->gpio.init(dev);
	if (ret)
		goto out_mc;

556
557
558
	/* PTIMER */
	ret = engine->timer.init(dev);
	if (ret)
559
		goto out_gpio;
560
561
562
563

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

566
	switch (dev_priv->card_type) {
567
568
569
	case NV_04:
		nv04_graph_create(dev);
		break;
570
571
572
	case NV_10:
		nv10_graph_create(dev);
		break;
573
574
575
576
	case NV_20:
	case NV_30:
		nv20_graph_create(dev);
		break;
577
578
579
580
	case NV_40:
		nv40_graph_create(dev);
		break;
	case NV_50:
581
		nv50_graph_create(dev);
582
583
		break;
	case NV_C0:
584
		nvc0_graph_create(dev);
585
		break;
586
587
	default:
		break;
588
	}
589

590
591
592
593
594
595
596
597
598
599
600
	switch (dev_priv->chipset) {
	case 0x84:
	case 0x86:
	case 0x92:
	case 0x94:
	case 0x96:
	case 0xa0:
		nv84_crypt_create(dev);
		break;
	}

601
602
603
604
605
606
607
608
609
610
611
	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;
612
613
614
615
	case NV_C0:
		nvc0_copy_create(dev, 0);
		nvc0_copy_create(dev, 1);
		break;
616
617
618
619
	default:
		break;
	}

620
621
	if (dev_priv->card_type == NV_40)
		nv40_mpeg_create(dev);
Ben Skeggs's avatar
Ben Skeggs committed
622
	else
623
	if (dev_priv->card_type == NV_50 &&
Ben Skeggs's avatar
Ben Skeggs committed
624
	    (dev_priv->chipset < 0x98 || dev_priv->chipset == 0xa0))
625
		nv50_mpeg_create(dev);
626

627
	if (!nouveau_noaccel) {
628
629
630
631
632
633
634
635
		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;
			}
		}

636
637
638
		/* PFIFO */
		ret = engine->fifo.init(dev);
		if (ret)
639
			goto out_engine;
640
	}
641

642
	ret = engine->display.create(dev);
643
644
645
	if (ret)
		goto out_fifo;

646
	ret = drm_vblank_init(dev, nv_two_heads(dev) ? 2 : 1);
647
	if (ret)
648
		goto out_vblank;
649

650
	ret = nouveau_irq_init(dev);
651
	if (ret)
652
		goto out_vblank;
653
654
655

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

656
	if (dev_priv->eng[NVOBJ_ENGINE_GR]) {
657
		ret = nouveau_fence_init(dev);
658
659
		if (ret)
			goto out_irq;
660
661
662
663

		ret = nouveau_card_init_channel(dev);
		if (ret)
			goto out_fence;
664
665
	}

666
667
	nouveau_fbcon_init(dev);
	drm_kms_helper_poll_init(dev);
668
	return 0;
669

670
671
out_fence:
	nouveau_fence_fini(dev);
672
out_irq:
Ben Skeggs's avatar
Ben Skeggs committed
673
	nouveau_irq_fini(dev);
674
675
out_vblank:
	drm_vblank_cleanup(dev);
676
	engine->display.destroy(dev);
677
out_fifo:
678
679
	if (!nouveau_noaccel)
		engine->fifo.takedown(dev);
680
681
682
out_engine:
	if (!nouveau_noaccel) {
		for (e = e - 1; e >= 0; e--) {
683
684
			if (!dev_priv->eng[e])
				continue;
685
			dev_priv->eng[e]->fini(dev, e);
686
			dev_priv->eng[e]->destroy(dev,e );
687
688
689
		}
	}

690
691
692
	engine->fb.takedown(dev);
out_timer:
	engine->timer.takedown(dev);
693
694
out_gpio:
	engine->gpio.takedown(dev);
695
696
out_mc:
	engine->mc.takedown(dev);
697
698
out_gart:
	nouveau_mem_gart_fini(dev);
699
700
out_instmem:
	engine->instmem.takedown(dev);
701
702
703
704
out_gpuobj:
	nouveau_gpuobj_takedown(dev);
out_vram:
	nouveau_mem_vram_fini(dev);
705
out_bios:
706
	nouveau_pm_fini(dev);
707
	nouveau_bios_takedown(dev);
708
709
out_display_early:
	engine->display.late_takedown(dev);
710
711
712
out:
	vga_client_register(dev->pdev, NULL, NULL, NULL);
	return ret;
713
714
715
716
717
718
}

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;
719
	int e;
720

721
	if (dev_priv->channel) {
722
		nouveau_fence_fini(dev);
723
		nouveau_channel_put_unlocked(&dev_priv->channel);
724
	}
725

726
727
	if (!nouveau_noaccel) {
		engine->fifo.takedown(dev);
728
729
730
731
732
733
		for (e = NVOBJ_ENGINE_NR - 1; e >= 0; e--) {
			if (dev_priv->eng[e]) {
				dev_priv->eng[e]->fini(dev, e);
				dev_priv->eng[e]->destroy(dev,e );
			}
		}
734
735
736
	}
	engine->fb.takedown(dev);
	engine->timer.takedown(dev);
737
	engine->gpio.takedown(dev);
738
	engine->mc.takedown(dev);
739
	engine->display.late_takedown(dev);
740

741
742
743
744
745
	if (dev_priv->vga_ram) {
		nouveau_bo_unpin(dev_priv->vga_ram);
		nouveau_bo_ref(NULL, &dev_priv->vga_ram);
	}

746
747
748
749
	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);
750
	nouveau_mem_gart_fini(dev);
751

752
	engine->instmem.takedown(dev);
753
754
	nouveau_gpuobj_takedown(dev);
	nouveau_mem_vram_fini(dev);
755

Ben Skeggs's avatar
Ben Skeggs committed
756
	nouveau_irq_fini(dev);
757
	drm_vblank_cleanup(dev);
758

759
	nouveau_pm_fini(dev);
760
	nouveau_bios_takedown(dev);
761

762
	vga_client_register(dev->pdev, NULL, NULL, NULL);
763
764
765
766
767
768
769
770
771
772
773
774
775
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
}

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

802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
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;
831
	bool primary = false;
832
833
834
835
	dev_priv->apertures = nouveau_get_apertures(dev);
	if (!dev_priv->apertures)
		return -ENOMEM;

836
837
838
#ifdef CONFIG_X86
	primary = dev->pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW;
#endif
839

840
	remove_conflicting_framebuffers(dev_priv->apertures, "nouveaufb", primary);
841
842
843
	return 0;
}

844
845
846
847
848
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;
849
	int ret;
850
851

	dev_priv = kzalloc(sizeof(*dev_priv), GFP_KERNEL);
852
853
854
855
	if (!dev_priv) {
		ret = -ENOMEM;
		goto err_out;
	}
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
	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");
875
		ret = -EINVAL;
876
		goto err_priv;
877
878
879
880
881
882
883
884
885
886
887
888
889
890
	}
	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);
891
	dev_priv->stepping = 0; /* XXX: add stepping for pre-NV10? */
892
893
894
895
896

	/* We're dealing with >=NV10 */
	if ((reg0 & 0x0f000000) > 0) {
		/* Bit 27-20 contain the architecture in hex */
		dev_priv->chipset = (reg0 & 0xff00000) >> 20;
897
		dev_priv->stepping = (reg0 & 0xff);
898
899
	/* NV04 or NV05 */
	} else if ((reg0 & 0xff00fff0) == 0x20004000) {
900
901
902
903
		if (reg0 & 0x00f00000)
			dev_priv->chipset = 0x05;
		else
			dev_priv->chipset = 0x04;
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
	} 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;
924
925
926
	case 0xc0:
		dev_priv->card_type = NV_C0;
		break;
927
928
	default:
		NV_INFO(dev, "Unsupported chipset 0x%08x\n", reg0);
929
930
		ret = -EINVAL;
		goto err_mmio;
931
932
933
934
935
	}

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

936
937
	ret = nouveau_remove_conflicting_drivers(dev);
	if (ret)
938
		goto err_mmio;
939

Lucas De Marchi's avatar
Lucas De Marchi committed
940
	/* Map PRAMIN BAR, or on older cards, the aperture within BAR0 */
941
942
943
944
945
946
	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);
947
948
		dev_priv->ramin =
			ioremap(pci_resource_start(dev->pdev, ramin_bar),
949
950
				dev_priv->ramin_size);
		if (!dev_priv->ramin) {
951
			NV_ERROR(dev, "Failed to PRAMIN BAR");
952
953
			ret = -ENOMEM;
			goto err_mmio;
954
		}
955
	} else {
956
957
		dev_priv->ramin_size = 1 * 1024 * 1024;
		dev_priv->ramin = ioremap(mmio_start_offs + NV_RAMIN,
958
					  dev_priv->ramin_size);
959
960
		if (!dev_priv->ramin) {
			NV_ERROR(dev, "Failed to map BAR0 PRAMIN.\n");
961
962
			ret = -ENOMEM;
			goto err_mmio;
963
964
965
966
967
968
969
970
971
972
973
974
		}
	}

	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 */
975
976
	ret = nouveau_card_init(dev);
	if (ret)
977
		goto err_ramin;
978
979

	return 0;
980
981
982
983
984
985
986
987
988
989

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;
990
991
992
993
}

void nouveau_lastclose(struct drm_device *dev)
{
994
	vga_switcheroo_process_delayed_switch();
995
996
997
998
999
}

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