1 /*
2  * Copyright 2005 Stephane Marchesin
3  * Copyright 2008 Stuart Bennett
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice (including the next
14  * paragraph) shall be included in all copies or substantial portions of the
15  * Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20  * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23  * DEALINGS IN THE SOFTWARE.
24  */
25 
26 #include <linux/swab.h>
27 #include <linux/slab.h>
28 #include "drmP.h"
29 #include "drm.h"
30 #include "drm_sarea.h"
31 #include "drm_crtc_helper.h"
32 #include <linux/vgaarb.h>
33 #include <linux/vga_switcheroo.h>
34 
35 #include "nouveau_drv.h"
36 #include "nouveau_drm.h"
37 #include "nouveau_fbcon.h"
38 #include "nouveau_ramht.h"
39 #include "nouveau_gpio.h"
40 #include "nouveau_pm.h"
41 #include "nv50_display.h"
42 
nouveau_stub_takedown(struct drm_device * dev)43 static void nouveau_stub_takedown(struct drm_device *dev) {}
nouveau_stub_init(struct drm_device * dev)44 static int nouveau_stub_init(struct drm_device *dev) { return 0; }
45 
nouveau_init_engine_ptrs(struct drm_device * dev)46 static int nouveau_init_engine_ptrs(struct drm_device *dev)
47 {
48 	struct drm_nouveau_private *dev_priv = dev->dev_private;
49 	struct nouveau_engine *engine = &dev_priv->engine;
50 	u32 pclass = dev->pdev->class >> 8;
51 
52 	switch (dev_priv->chipset & 0xf0) {
53 	case 0x00:
54 		engine->instmem.init		= nv04_instmem_init;
55 		engine->instmem.takedown	= nv04_instmem_takedown;
56 		engine->instmem.suspend		= nv04_instmem_suspend;
57 		engine->instmem.resume		= nv04_instmem_resume;
58 		engine->instmem.get		= nv04_instmem_get;
59 		engine->instmem.put		= nv04_instmem_put;
60 		engine->instmem.map		= nv04_instmem_map;
61 		engine->instmem.unmap		= nv04_instmem_unmap;
62 		engine->instmem.flush		= nv04_instmem_flush;
63 		engine->mc.init			= nv04_mc_init;
64 		engine->mc.takedown		= nv04_mc_takedown;
65 		engine->timer.init		= nv04_timer_init;
66 		engine->timer.read		= nv04_timer_read;
67 		engine->timer.takedown		= nv04_timer_takedown;
68 		engine->fb.init			= nv04_fb_init;
69 		engine->fb.takedown		= nv04_fb_takedown;
70 		engine->fifo.channels		= 16;
71 		engine->fifo.init		= nv04_fifo_init;
72 		engine->fifo.takedown		= nv04_fifo_fini;
73 		engine->fifo.disable		= nv04_fifo_disable;
74 		engine->fifo.enable		= nv04_fifo_enable;
75 		engine->fifo.reassign		= nv04_fifo_reassign;
76 		engine->fifo.cache_pull		= nv04_fifo_cache_pull;
77 		engine->fifo.channel_id		= nv04_fifo_channel_id;
78 		engine->fifo.create_context	= nv04_fifo_create_context;
79 		engine->fifo.destroy_context	= nv04_fifo_destroy_context;
80 		engine->fifo.load_context	= nv04_fifo_load_context;
81 		engine->fifo.unload_context	= nv04_fifo_unload_context;
82 		engine->display.early_init	= nv04_display_early_init;
83 		engine->display.late_takedown	= nv04_display_late_takedown;
84 		engine->display.create		= nv04_display_create;
85 		engine->display.destroy		= nv04_display_destroy;
86 		engine->display.init		= nv04_display_init;
87 		engine->display.fini		= nv04_display_fini;
88 		engine->pm.clocks_get		= nv04_pm_clocks_get;
89 		engine->pm.clocks_pre		= nv04_pm_clocks_pre;
90 		engine->pm.clocks_set		= nv04_pm_clocks_set;
91 		engine->vram.init		= nv04_fb_vram_init;
92 		engine->vram.takedown		= nouveau_stub_takedown;
93 		engine->vram.flags_valid	= nouveau_mem_flags_valid;
94 		break;
95 	case 0x10:
96 		engine->instmem.init		= nv04_instmem_init;
97 		engine->instmem.takedown	= nv04_instmem_takedown;
98 		engine->instmem.suspend		= nv04_instmem_suspend;
99 		engine->instmem.resume		= nv04_instmem_resume;
100 		engine->instmem.get		= nv04_instmem_get;
101 		engine->instmem.put		= nv04_instmem_put;
102 		engine->instmem.map		= nv04_instmem_map;
103 		engine->instmem.unmap		= nv04_instmem_unmap;
104 		engine->instmem.flush		= nv04_instmem_flush;
105 		engine->mc.init			= nv04_mc_init;
106 		engine->mc.takedown		= nv04_mc_takedown;
107 		engine->timer.init		= nv04_timer_init;
108 		engine->timer.read		= nv04_timer_read;
109 		engine->timer.takedown		= nv04_timer_takedown;
110 		engine->fb.init			= nv10_fb_init;
111 		engine->fb.takedown		= nv10_fb_takedown;
112 		engine->fb.init_tile_region	= nv10_fb_init_tile_region;
113 		engine->fb.set_tile_region	= nv10_fb_set_tile_region;
114 		engine->fb.free_tile_region	= nv10_fb_free_tile_region;
115 		engine->fifo.channels		= 32;
116 		engine->fifo.init		= nv10_fifo_init;
117 		engine->fifo.takedown		= nv04_fifo_fini;
118 		engine->fifo.disable		= nv04_fifo_disable;
119 		engine->fifo.enable		= nv04_fifo_enable;
120 		engine->fifo.reassign		= nv04_fifo_reassign;
121 		engine->fifo.cache_pull		= nv04_fifo_cache_pull;
122 		engine->fifo.channel_id		= nv10_fifo_channel_id;
123 		engine->fifo.create_context	= nv10_fifo_create_context;
124 		engine->fifo.destroy_context	= nv04_fifo_destroy_context;
125 		engine->fifo.load_context	= nv10_fifo_load_context;
126 		engine->fifo.unload_context	= nv10_fifo_unload_context;
127 		engine->display.early_init	= nv04_display_early_init;
128 		engine->display.late_takedown	= nv04_display_late_takedown;
129 		engine->display.create		= nv04_display_create;
130 		engine->display.destroy		= nv04_display_destroy;
131 		engine->display.init		= nv04_display_init;
132 		engine->display.fini		= nv04_display_fini;
133 		engine->gpio.drive		= nv10_gpio_drive;
134 		engine->gpio.sense		= nv10_gpio_sense;
135 		engine->pm.clocks_get		= nv04_pm_clocks_get;
136 		engine->pm.clocks_pre		= nv04_pm_clocks_pre;
137 		engine->pm.clocks_set		= nv04_pm_clocks_set;
138 		if (dev_priv->chipset == 0x1a ||
139 		    dev_priv->chipset == 0x1f)
140 			engine->vram.init	= nv1a_fb_vram_init;
141 		else
142 			engine->vram.init	= nv10_fb_vram_init;
143 		engine->vram.takedown		= nouveau_stub_takedown;
144 		engine->vram.flags_valid	= nouveau_mem_flags_valid;
145 		break;
146 	case 0x20:
147 		engine->instmem.init		= nv04_instmem_init;
148 		engine->instmem.takedown	= nv04_instmem_takedown;
149 		engine->instmem.suspend		= nv04_instmem_suspend;
150 		engine->instmem.resume		= nv04_instmem_resume;
151 		engine->instmem.get		= nv04_instmem_get;
152 		engine->instmem.put		= nv04_instmem_put;
153 		engine->instmem.map		= nv04_instmem_map;
154 		engine->instmem.unmap		= nv04_instmem_unmap;
155 		engine->instmem.flush		= nv04_instmem_flush;
156 		engine->mc.init			= nv04_mc_init;
157 		engine->mc.takedown		= nv04_mc_takedown;
158 		engine->timer.init		= nv04_timer_init;
159 		engine->timer.read		= nv04_timer_read;
160 		engine->timer.takedown		= nv04_timer_takedown;
161 		engine->fb.init			= nv20_fb_init;
162 		engine->fb.takedown		= nv20_fb_takedown;
163 		engine->fb.init_tile_region	= nv20_fb_init_tile_region;
164 		engine->fb.set_tile_region	= nv20_fb_set_tile_region;
165 		engine->fb.free_tile_region	= nv20_fb_free_tile_region;
166 		engine->fifo.channels		= 32;
167 		engine->fifo.init		= nv10_fifo_init;
168 		engine->fifo.takedown		= nv04_fifo_fini;
169 		engine->fifo.disable		= nv04_fifo_disable;
170 		engine->fifo.enable		= nv04_fifo_enable;
171 		engine->fifo.reassign		= nv04_fifo_reassign;
172 		engine->fifo.cache_pull		= nv04_fifo_cache_pull;
173 		engine->fifo.channel_id		= nv10_fifo_channel_id;
174 		engine->fifo.create_context	= nv10_fifo_create_context;
175 		engine->fifo.destroy_context	= nv04_fifo_destroy_context;
176 		engine->fifo.load_context	= nv10_fifo_load_context;
177 		engine->fifo.unload_context	= nv10_fifo_unload_context;
178 		engine->display.early_init	= nv04_display_early_init;
179 		engine->display.late_takedown	= nv04_display_late_takedown;
180 		engine->display.create		= nv04_display_create;
181 		engine->display.destroy		= nv04_display_destroy;
182 		engine->display.init		= nv04_display_init;
183 		engine->display.fini		= nv04_display_fini;
184 		engine->gpio.drive		= nv10_gpio_drive;
185 		engine->gpio.sense		= nv10_gpio_sense;
186 		engine->pm.clocks_get		= nv04_pm_clocks_get;
187 		engine->pm.clocks_pre		= nv04_pm_clocks_pre;
188 		engine->pm.clocks_set		= nv04_pm_clocks_set;
189 		engine->vram.init		= nv20_fb_vram_init;
190 		engine->vram.takedown		= nouveau_stub_takedown;
191 		engine->vram.flags_valid	= nouveau_mem_flags_valid;
192 		break;
193 	case 0x30:
194 		engine->instmem.init		= nv04_instmem_init;
195 		engine->instmem.takedown	= nv04_instmem_takedown;
196 		engine->instmem.suspend		= nv04_instmem_suspend;
197 		engine->instmem.resume		= nv04_instmem_resume;
198 		engine->instmem.get		= nv04_instmem_get;
199 		engine->instmem.put		= nv04_instmem_put;
200 		engine->instmem.map		= nv04_instmem_map;
201 		engine->instmem.unmap		= nv04_instmem_unmap;
202 		engine->instmem.flush		= nv04_instmem_flush;
203 		engine->mc.init			= nv04_mc_init;
204 		engine->mc.takedown		= nv04_mc_takedown;
205 		engine->timer.init		= nv04_timer_init;
206 		engine->timer.read		= nv04_timer_read;
207 		engine->timer.takedown		= nv04_timer_takedown;
208 		engine->fb.init			= nv30_fb_init;
209 		engine->fb.takedown		= nv30_fb_takedown;
210 		engine->fb.init_tile_region	= nv30_fb_init_tile_region;
211 		engine->fb.set_tile_region	= nv10_fb_set_tile_region;
212 		engine->fb.free_tile_region	= nv30_fb_free_tile_region;
213 		engine->fifo.channels		= 32;
214 		engine->fifo.init		= nv10_fifo_init;
215 		engine->fifo.takedown		= nv04_fifo_fini;
216 		engine->fifo.disable		= nv04_fifo_disable;
217 		engine->fifo.enable		= nv04_fifo_enable;
218 		engine->fifo.reassign		= nv04_fifo_reassign;
219 		engine->fifo.cache_pull		= nv04_fifo_cache_pull;
220 		engine->fifo.channel_id		= nv10_fifo_channel_id;
221 		engine->fifo.create_context	= nv10_fifo_create_context;
222 		engine->fifo.destroy_context	= nv04_fifo_destroy_context;
223 		engine->fifo.load_context	= nv10_fifo_load_context;
224 		engine->fifo.unload_context	= nv10_fifo_unload_context;
225 		engine->display.early_init	= nv04_display_early_init;
226 		engine->display.late_takedown	= nv04_display_late_takedown;
227 		engine->display.create		= nv04_display_create;
228 		engine->display.destroy		= nv04_display_destroy;
229 		engine->display.init		= nv04_display_init;
230 		engine->display.fini		= nv04_display_fini;
231 		engine->gpio.drive		= nv10_gpio_drive;
232 		engine->gpio.sense		= nv10_gpio_sense;
233 		engine->pm.clocks_get		= nv04_pm_clocks_get;
234 		engine->pm.clocks_pre		= nv04_pm_clocks_pre;
235 		engine->pm.clocks_set		= nv04_pm_clocks_set;
236 		engine->pm.voltage_get		= nouveau_voltage_gpio_get;
237 		engine->pm.voltage_set		= nouveau_voltage_gpio_set;
238 		engine->vram.init		= nv20_fb_vram_init;
239 		engine->vram.takedown		= nouveau_stub_takedown;
240 		engine->vram.flags_valid	= nouveau_mem_flags_valid;
241 		break;
242 	case 0x40:
243 	case 0x60:
244 		engine->instmem.init		= nv04_instmem_init;
245 		engine->instmem.takedown	= nv04_instmem_takedown;
246 		engine->instmem.suspend		= nv04_instmem_suspend;
247 		engine->instmem.resume		= nv04_instmem_resume;
248 		engine->instmem.get		= nv04_instmem_get;
249 		engine->instmem.put		= nv04_instmem_put;
250 		engine->instmem.map		= nv04_instmem_map;
251 		engine->instmem.unmap		= nv04_instmem_unmap;
252 		engine->instmem.flush		= nv04_instmem_flush;
253 		engine->mc.init			= nv40_mc_init;
254 		engine->mc.takedown		= nv40_mc_takedown;
255 		engine->timer.init		= nv04_timer_init;
256 		engine->timer.read		= nv04_timer_read;
257 		engine->timer.takedown		= nv04_timer_takedown;
258 		engine->fb.init			= nv40_fb_init;
259 		engine->fb.takedown		= nv40_fb_takedown;
260 		engine->fb.init_tile_region	= nv30_fb_init_tile_region;
261 		engine->fb.set_tile_region	= nv40_fb_set_tile_region;
262 		engine->fb.free_tile_region	= nv30_fb_free_tile_region;
263 		engine->fifo.channels		= 32;
264 		engine->fifo.init		= nv40_fifo_init;
265 		engine->fifo.takedown		= nv04_fifo_fini;
266 		engine->fifo.disable		= nv04_fifo_disable;
267 		engine->fifo.enable		= nv04_fifo_enable;
268 		engine->fifo.reassign		= nv04_fifo_reassign;
269 		engine->fifo.cache_pull		= nv04_fifo_cache_pull;
270 		engine->fifo.channel_id		= nv10_fifo_channel_id;
271 		engine->fifo.create_context	= nv40_fifo_create_context;
272 		engine->fifo.destroy_context	= nv04_fifo_destroy_context;
273 		engine->fifo.load_context	= nv40_fifo_load_context;
274 		engine->fifo.unload_context	= nv40_fifo_unload_context;
275 		engine->display.early_init	= nv04_display_early_init;
276 		engine->display.late_takedown	= nv04_display_late_takedown;
277 		engine->display.create		= nv04_display_create;
278 		engine->display.destroy		= nv04_display_destroy;
279 		engine->display.init		= nv04_display_init;
280 		engine->display.fini		= nv04_display_fini;
281 		engine->gpio.init		= nv10_gpio_init;
282 		engine->gpio.fini		= nv10_gpio_fini;
283 		engine->gpio.drive		= nv10_gpio_drive;
284 		engine->gpio.sense		= nv10_gpio_sense;
285 		engine->gpio.irq_enable		= nv10_gpio_irq_enable;
286 		engine->pm.clocks_get		= nv40_pm_clocks_get;
287 		engine->pm.clocks_pre		= nv40_pm_clocks_pre;
288 		engine->pm.clocks_set		= nv40_pm_clocks_set;
289 		engine->pm.voltage_get		= nouveau_voltage_gpio_get;
290 		engine->pm.voltage_set		= nouveau_voltage_gpio_set;
291 		engine->pm.temp_get		= nv40_temp_get;
292 		engine->pm.pwm_get		= nv40_pm_pwm_get;
293 		engine->pm.pwm_set		= nv40_pm_pwm_set;
294 		engine->vram.init		= nv40_fb_vram_init;
295 		engine->vram.takedown		= nouveau_stub_takedown;
296 		engine->vram.flags_valid	= nouveau_mem_flags_valid;
297 		break;
298 	case 0x50:
299 	case 0x80: /* gotta love NVIDIA's consistency.. */
300 	case 0x90:
301 	case 0xa0:
302 		engine->instmem.init		= nv50_instmem_init;
303 		engine->instmem.takedown	= nv50_instmem_takedown;
304 		engine->instmem.suspend		= nv50_instmem_suspend;
305 		engine->instmem.resume		= nv50_instmem_resume;
306 		engine->instmem.get		= nv50_instmem_get;
307 		engine->instmem.put		= nv50_instmem_put;
308 		engine->instmem.map		= nv50_instmem_map;
309 		engine->instmem.unmap		= nv50_instmem_unmap;
310 		if (dev_priv->chipset == 0x50)
311 			engine->instmem.flush	= nv50_instmem_flush;
312 		else
313 			engine->instmem.flush	= nv84_instmem_flush;
314 		engine->mc.init			= nv50_mc_init;
315 		engine->mc.takedown		= nv50_mc_takedown;
316 		engine->timer.init		= nv04_timer_init;
317 		engine->timer.read		= nv04_timer_read;
318 		engine->timer.takedown		= nv04_timer_takedown;
319 		engine->fb.init			= nv50_fb_init;
320 		engine->fb.takedown		= nv50_fb_takedown;
321 		engine->fifo.channels		= 128;
322 		engine->fifo.init		= nv50_fifo_init;
323 		engine->fifo.takedown		= nv50_fifo_takedown;
324 		engine->fifo.disable		= nv04_fifo_disable;
325 		engine->fifo.enable		= nv04_fifo_enable;
326 		engine->fifo.reassign		= nv04_fifo_reassign;
327 		engine->fifo.channel_id		= nv50_fifo_channel_id;
328 		engine->fifo.create_context	= nv50_fifo_create_context;
329 		engine->fifo.destroy_context	= nv50_fifo_destroy_context;
330 		engine->fifo.load_context	= nv50_fifo_load_context;
331 		engine->fifo.unload_context	= nv50_fifo_unload_context;
332 		engine->fifo.tlb_flush		= nv50_fifo_tlb_flush;
333 		engine->display.early_init	= nv50_display_early_init;
334 		engine->display.late_takedown	= nv50_display_late_takedown;
335 		engine->display.create		= nv50_display_create;
336 		engine->display.destroy		= nv50_display_destroy;
337 		engine->display.init		= nv50_display_init;
338 		engine->display.fini		= nv50_display_fini;
339 		engine->gpio.init		= nv50_gpio_init;
340 		engine->gpio.fini		= nv50_gpio_fini;
341 		engine->gpio.drive		= nv50_gpio_drive;
342 		engine->gpio.sense		= nv50_gpio_sense;
343 		engine->gpio.irq_enable		= nv50_gpio_irq_enable;
344 		switch (dev_priv->chipset) {
345 		case 0x84:
346 		case 0x86:
347 		case 0x92:
348 		case 0x94:
349 		case 0x96:
350 		case 0x98:
351 		case 0xa0:
352 		case 0xaa:
353 		case 0xac:
354 		case 0x50:
355 			engine->pm.clocks_get	= nv50_pm_clocks_get;
356 			engine->pm.clocks_pre	= nv50_pm_clocks_pre;
357 			engine->pm.clocks_set	= nv50_pm_clocks_set;
358 			break;
359 		default:
360 			engine->pm.clocks_get	= nva3_pm_clocks_get;
361 			engine->pm.clocks_pre	= nva3_pm_clocks_pre;
362 			engine->pm.clocks_set	= nva3_pm_clocks_set;
363 			break;
364 		}
365 		engine->pm.voltage_get		= nouveau_voltage_gpio_get;
366 		engine->pm.voltage_set		= nouveau_voltage_gpio_set;
367 		if (dev_priv->chipset >= 0x84)
368 			engine->pm.temp_get	= nv84_temp_get;
369 		else
370 			engine->pm.temp_get	= nv40_temp_get;
371 		engine->pm.pwm_get		= nv50_pm_pwm_get;
372 		engine->pm.pwm_set		= nv50_pm_pwm_set;
373 		engine->vram.init		= nv50_vram_init;
374 		engine->vram.takedown		= nv50_vram_fini;
375 		engine->vram.get		= nv50_vram_new;
376 		engine->vram.put		= nv50_vram_del;
377 		engine->vram.flags_valid	= nv50_vram_flags_valid;
378 		break;
379 	case 0xc0:
380 		engine->instmem.init		= nvc0_instmem_init;
381 		engine->instmem.takedown	= nvc0_instmem_takedown;
382 		engine->instmem.suspend		= nvc0_instmem_suspend;
383 		engine->instmem.resume		= nvc0_instmem_resume;
384 		engine->instmem.get		= nv50_instmem_get;
385 		engine->instmem.put		= nv50_instmem_put;
386 		engine->instmem.map		= nv50_instmem_map;
387 		engine->instmem.unmap		= nv50_instmem_unmap;
388 		engine->instmem.flush		= nv84_instmem_flush;
389 		engine->mc.init			= nv50_mc_init;
390 		engine->mc.takedown		= nv50_mc_takedown;
391 		engine->timer.init		= nv04_timer_init;
392 		engine->timer.read		= nv04_timer_read;
393 		engine->timer.takedown		= nv04_timer_takedown;
394 		engine->fb.init			= nvc0_fb_init;
395 		engine->fb.takedown		= nvc0_fb_takedown;
396 		engine->fifo.channels		= 128;
397 		engine->fifo.init		= nvc0_fifo_init;
398 		engine->fifo.takedown		= nvc0_fifo_takedown;
399 		engine->fifo.disable		= nvc0_fifo_disable;
400 		engine->fifo.enable		= nvc0_fifo_enable;
401 		engine->fifo.reassign		= nvc0_fifo_reassign;
402 		engine->fifo.channel_id		= nvc0_fifo_channel_id;
403 		engine->fifo.create_context	= nvc0_fifo_create_context;
404 		engine->fifo.destroy_context	= nvc0_fifo_destroy_context;
405 		engine->fifo.load_context	= nvc0_fifo_load_context;
406 		engine->fifo.unload_context	= nvc0_fifo_unload_context;
407 		engine->display.early_init	= nv50_display_early_init;
408 		engine->display.late_takedown	= nv50_display_late_takedown;
409 		engine->display.create		= nv50_display_create;
410 		engine->display.destroy		= nv50_display_destroy;
411 		engine->display.init		= nv50_display_init;
412 		engine->display.fini		= nv50_display_fini;
413 		engine->gpio.init		= nv50_gpio_init;
414 		engine->gpio.fini		= nv50_gpio_fini;
415 		engine->gpio.drive		= nv50_gpio_drive;
416 		engine->gpio.sense		= nv50_gpio_sense;
417 		engine->gpio.irq_enable		= nv50_gpio_irq_enable;
418 		engine->vram.init		= nvc0_vram_init;
419 		engine->vram.takedown		= nv50_vram_fini;
420 		engine->vram.get		= nvc0_vram_new;
421 		engine->vram.put		= nv50_vram_del;
422 		engine->vram.flags_valid	= nvc0_vram_flags_valid;
423 		engine->pm.temp_get		= nv84_temp_get;
424 		engine->pm.clocks_get		= nvc0_pm_clocks_get;
425 		engine->pm.clocks_pre		= nvc0_pm_clocks_pre;
426 		engine->pm.clocks_set		= nvc0_pm_clocks_set;
427 		engine->pm.voltage_get		= nouveau_voltage_gpio_get;
428 		engine->pm.voltage_set		= nouveau_voltage_gpio_set;
429 		engine->pm.pwm_get		= nv50_pm_pwm_get;
430 		engine->pm.pwm_set		= nv50_pm_pwm_set;
431 		break;
432 	case 0xd0:
433 		engine->instmem.init		= nvc0_instmem_init;
434 		engine->instmem.takedown	= nvc0_instmem_takedown;
435 		engine->instmem.suspend		= nvc0_instmem_suspend;
436 		engine->instmem.resume		= nvc0_instmem_resume;
437 		engine->instmem.get		= nv50_instmem_get;
438 		engine->instmem.put		= nv50_instmem_put;
439 		engine->instmem.map		= nv50_instmem_map;
440 		engine->instmem.unmap		= nv50_instmem_unmap;
441 		engine->instmem.flush		= nv84_instmem_flush;
442 		engine->mc.init			= nv50_mc_init;
443 		engine->mc.takedown		= nv50_mc_takedown;
444 		engine->timer.init		= nv04_timer_init;
445 		engine->timer.read		= nv04_timer_read;
446 		engine->timer.takedown		= nv04_timer_takedown;
447 		engine->fb.init			= nvc0_fb_init;
448 		engine->fb.takedown		= nvc0_fb_takedown;
449 		engine->fifo.channels		= 128;
450 		engine->fifo.init		= nvc0_fifo_init;
451 		engine->fifo.takedown		= nvc0_fifo_takedown;
452 		engine->fifo.disable		= nvc0_fifo_disable;
453 		engine->fifo.enable		= nvc0_fifo_enable;
454 		engine->fifo.reassign		= nvc0_fifo_reassign;
455 		engine->fifo.channel_id		= nvc0_fifo_channel_id;
456 		engine->fifo.create_context	= nvc0_fifo_create_context;
457 		engine->fifo.destroy_context	= nvc0_fifo_destroy_context;
458 		engine->fifo.load_context	= nvc0_fifo_load_context;
459 		engine->fifo.unload_context	= nvc0_fifo_unload_context;
460 		engine->display.early_init	= nouveau_stub_init;
461 		engine->display.late_takedown	= nouveau_stub_takedown;
462 		engine->display.create		= nvd0_display_create;
463 		engine->display.destroy		= nvd0_display_destroy;
464 		engine->display.init		= nvd0_display_init;
465 		engine->display.fini		= nvd0_display_fini;
466 		engine->gpio.init		= nv50_gpio_init;
467 		engine->gpio.fini		= nv50_gpio_fini;
468 		engine->gpio.drive		= nvd0_gpio_drive;
469 		engine->gpio.sense		= nvd0_gpio_sense;
470 		engine->gpio.irq_enable		= nv50_gpio_irq_enable;
471 		engine->vram.init		= nvc0_vram_init;
472 		engine->vram.takedown		= nv50_vram_fini;
473 		engine->vram.get		= nvc0_vram_new;
474 		engine->vram.put		= nv50_vram_del;
475 		engine->vram.flags_valid	= nvc0_vram_flags_valid;
476 		engine->pm.temp_get		= nv84_temp_get;
477 		engine->pm.clocks_get		= nvc0_pm_clocks_get;
478 		engine->pm.clocks_pre		= nvc0_pm_clocks_pre;
479 		engine->pm.clocks_set		= nvc0_pm_clocks_set;
480 		engine->pm.voltage_get		= nouveau_voltage_gpio_get;
481 		engine->pm.voltage_set		= nouveau_voltage_gpio_set;
482 		break;
483 	case 0xe0:
484 		engine->instmem.init		= nvc0_instmem_init;
485 		engine->instmem.takedown	= nvc0_instmem_takedown;
486 		engine->instmem.suspend		= nvc0_instmem_suspend;
487 		engine->instmem.resume		= nvc0_instmem_resume;
488 		engine->instmem.get		= nv50_instmem_get;
489 		engine->instmem.put		= nv50_instmem_put;
490 		engine->instmem.map		= nv50_instmem_map;
491 		engine->instmem.unmap		= nv50_instmem_unmap;
492 		engine->instmem.flush		= nv84_instmem_flush;
493 		engine->mc.init			= nv50_mc_init;
494 		engine->mc.takedown		= nv50_mc_takedown;
495 		engine->timer.init		= nv04_timer_init;
496 		engine->timer.read		= nv04_timer_read;
497 		engine->timer.takedown		= nv04_timer_takedown;
498 		engine->fb.init			= nvc0_fb_init;
499 		engine->fb.takedown		= nvc0_fb_takedown;
500 		engine->fifo.channels		= 0;
501 		engine->fifo.init		= nouveau_stub_init;
502 		engine->fifo.takedown		= nouveau_stub_takedown;
503 		engine->fifo.disable		= nvc0_fifo_disable;
504 		engine->fifo.enable		= nvc0_fifo_enable;
505 		engine->fifo.reassign		= nvc0_fifo_reassign;
506 		engine->fifo.unload_context	= nouveau_stub_init;
507 		engine->display.early_init	= nouveau_stub_init;
508 		engine->display.late_takedown	= nouveau_stub_takedown;
509 		engine->display.create		= nvd0_display_create;
510 		engine->display.destroy		= nvd0_display_destroy;
511 		engine->display.init		= nvd0_display_init;
512 		engine->display.fini		= nvd0_display_fini;
513 		engine->gpio.init		= nv50_gpio_init;
514 		engine->gpio.fini		= nv50_gpio_fini;
515 		engine->gpio.drive		= nvd0_gpio_drive;
516 		engine->gpio.sense		= nvd0_gpio_sense;
517 		engine->gpio.irq_enable		= nv50_gpio_irq_enable;
518 		engine->vram.init		= nvc0_vram_init;
519 		engine->vram.takedown		= nv50_vram_fini;
520 		engine->vram.get		= nvc0_vram_new;
521 		engine->vram.put		= nv50_vram_del;
522 		engine->vram.flags_valid	= nvc0_vram_flags_valid;
523 		break;
524 	default:
525 		NV_ERROR(dev, "NV%02x unsupported\n", dev_priv->chipset);
526 		return 1;
527 	}
528 
529 	/* headless mode */
530 	if (nouveau_modeset == 2 ||
531 	    (nouveau_modeset < 0 && pclass != PCI_CLASS_DISPLAY_VGA)) {
532 		engine->display.early_init = nouveau_stub_init;
533 		engine->display.late_takedown = nouveau_stub_takedown;
534 		engine->display.create = nouveau_stub_init;
535 		engine->display.init = nouveau_stub_init;
536 		engine->display.destroy = nouveau_stub_takedown;
537 	}
538 
539 	return 0;
540 }
541 
542 static unsigned int
nouveau_vga_set_decode(void * priv,bool state)543 nouveau_vga_set_decode(void *priv, bool state)
544 {
545 	struct drm_device *dev = priv;
546 	struct drm_nouveau_private *dev_priv = dev->dev_private;
547 
548 	if (dev_priv->chipset >= 0x40)
549 		nv_wr32(dev, 0x88054, state);
550 	else
551 		nv_wr32(dev, 0x1854, state);
552 
553 	if (state)
554 		return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
555 		       VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
556 	else
557 		return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
558 }
559 
nouveau_switcheroo_set_state(struct pci_dev * pdev,enum vga_switcheroo_state state)560 static void nouveau_switcheroo_set_state(struct pci_dev *pdev,
561 					 enum vga_switcheroo_state state)
562 {
563 	struct drm_device *dev = pci_get_drvdata(pdev);
564 	pm_message_t pmm = { .event = PM_EVENT_SUSPEND };
565 	if (state == VGA_SWITCHEROO_ON) {
566 		printk(KERN_ERR "VGA switcheroo: switched nouveau on\n");
567 		dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
568 		nouveau_pci_resume(pdev);
569 		drm_kms_helper_poll_enable(dev);
570 		dev->switch_power_state = DRM_SWITCH_POWER_ON;
571 	} else {
572 		printk(KERN_ERR "VGA switcheroo: switched nouveau off\n");
573 		dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
574 		drm_kms_helper_poll_disable(dev);
575 		nouveau_switcheroo_optimus_dsm();
576 		nouveau_pci_suspend(pdev, pmm);
577 		dev->switch_power_state = DRM_SWITCH_POWER_OFF;
578 	}
579 }
580 
nouveau_switcheroo_reprobe(struct pci_dev * pdev)581 static void nouveau_switcheroo_reprobe(struct pci_dev *pdev)
582 {
583 	struct drm_device *dev = pci_get_drvdata(pdev);
584 	nouveau_fbcon_output_poll_changed(dev);
585 }
586 
nouveau_switcheroo_can_switch(struct pci_dev * pdev)587 static bool nouveau_switcheroo_can_switch(struct pci_dev *pdev)
588 {
589 	struct drm_device *dev = pci_get_drvdata(pdev);
590 	bool can_switch;
591 
592 	spin_lock(&dev->count_lock);
593 	can_switch = (dev->open_count == 0);
594 	spin_unlock(&dev->count_lock);
595 	return can_switch;
596 }
597 
598 static void
nouveau_card_channel_fini(struct drm_device * dev)599 nouveau_card_channel_fini(struct drm_device *dev)
600 {
601 	struct drm_nouveau_private *dev_priv = dev->dev_private;
602 
603 	if (dev_priv->channel)
604 		nouveau_channel_put_unlocked(&dev_priv->channel);
605 }
606 
607 static int
nouveau_card_channel_init(struct drm_device * dev)608 nouveau_card_channel_init(struct drm_device *dev)
609 {
610 	struct drm_nouveau_private *dev_priv = dev->dev_private;
611 	struct nouveau_channel *chan;
612 	int ret, oclass;
613 
614 	ret = nouveau_channel_alloc(dev, &chan, NULL, NvDmaFB, NvDmaTT);
615 	dev_priv->channel = chan;
616 	if (ret)
617 		return ret;
618 
619 	mutex_unlock(&dev_priv->channel->mutex);
620 
621 	if (dev_priv->card_type <= NV_50) {
622 		if (dev_priv->card_type < NV_50)
623 			oclass = 0x0039;
624 		else
625 			oclass = 0x5039;
626 
627 		ret = nouveau_gpuobj_gr_new(chan, NvM2MF, oclass);
628 		if (ret)
629 			goto error;
630 
631 		ret = nouveau_notifier_alloc(chan, NvNotify0, 32, 0xfe0, 0x1000,
632 					     &chan->m2mf_ntfy);
633 		if (ret)
634 			goto error;
635 
636 		ret = RING_SPACE(chan, 6);
637 		if (ret)
638 			goto error;
639 
640 		BEGIN_RING(chan, NvSubM2MF, NV_MEMORY_TO_MEMORY_FORMAT_NAME, 1);
641 		OUT_RING  (chan, NvM2MF);
642 		BEGIN_RING(chan, NvSubM2MF, NV_MEMORY_TO_MEMORY_FORMAT_DMA_NOTIFY, 3);
643 		OUT_RING  (chan, NvNotify0);
644 		OUT_RING  (chan, chan->vram_handle);
645 		OUT_RING  (chan, chan->gart_handle);
646 	} else
647 	if (dev_priv->card_type <= NV_D0) {
648 		ret = nouveau_gpuobj_gr_new(chan, 0x9039, 0x9039);
649 		if (ret)
650 			goto error;
651 
652 		ret = RING_SPACE(chan, 2);
653 		if (ret)
654 			goto error;
655 
656 		BEGIN_NVC0(chan, 2, NvSubM2MF, 0x0000, 1);
657 		OUT_RING  (chan, 0x00009039);
658 	}
659 
660 	FIRE_RING (chan);
661 error:
662 	if (ret)
663 		nouveau_card_channel_fini(dev);
664 	return ret;
665 }
666 
667 int
nouveau_card_init(struct drm_device * dev)668 nouveau_card_init(struct drm_device *dev)
669 {
670 	struct drm_nouveau_private *dev_priv = dev->dev_private;
671 	struct nouveau_engine *engine;
672 	int ret, e = 0;
673 
674 	vga_client_register(dev->pdev, dev, NULL, nouveau_vga_set_decode);
675 	vga_switcheroo_register_client(dev->pdev, nouveau_switcheroo_set_state,
676 				       nouveau_switcheroo_reprobe,
677 				       nouveau_switcheroo_can_switch);
678 
679 	/* Initialise internal driver API hooks */
680 	ret = nouveau_init_engine_ptrs(dev);
681 	if (ret)
682 		goto out;
683 	engine = &dev_priv->engine;
684 	spin_lock_init(&dev_priv->channels.lock);
685 	spin_lock_init(&dev_priv->tile.lock);
686 	spin_lock_init(&dev_priv->context_switch_lock);
687 	spin_lock_init(&dev_priv->vm_lock);
688 
689 	/* Make the CRTCs and I2C buses accessible */
690 	ret = engine->display.early_init(dev);
691 	if (ret)
692 		goto out;
693 
694 	/* Parse BIOS tables / Run init tables if card not POSTed */
695 	ret = nouveau_bios_init(dev);
696 	if (ret)
697 		goto out_display_early;
698 
699 	/* workaround an odd issue on nvc1 by disabling the device's
700 	 * nosnoop capability.  hopefully won't cause issues until a
701 	 * better fix is found - assuming there is one...
702 	 */
703 	if (dev_priv->chipset == 0xc1) {
704 		nv_mask(dev, 0x00088080, 0x00000800, 0x00000000);
705 	}
706 
707 	/* PMC */
708 	ret = engine->mc.init(dev);
709 	if (ret)
710 		goto out_bios;
711 
712 	/* PTIMER */
713 	ret = engine->timer.init(dev);
714 	if (ret)
715 		goto out_mc;
716 
717 	/* PFB */
718 	ret = engine->fb.init(dev);
719 	if (ret)
720 		goto out_timer;
721 
722 	ret = engine->vram.init(dev);
723 	if (ret)
724 		goto out_fb;
725 
726 	/* PGPIO */
727 	ret = nouveau_gpio_create(dev);
728 	if (ret)
729 		goto out_vram;
730 
731 	ret = nouveau_gpuobj_init(dev);
732 	if (ret)
733 		goto out_gpio;
734 
735 	ret = engine->instmem.init(dev);
736 	if (ret)
737 		goto out_gpuobj;
738 
739 	ret = nouveau_mem_vram_init(dev);
740 	if (ret)
741 		goto out_instmem;
742 
743 	ret = nouveau_mem_gart_init(dev);
744 	if (ret)
745 		goto out_ttmvram;
746 
747 	if (!dev_priv->noaccel) {
748 		switch (dev_priv->card_type) {
749 		case NV_04:
750 			nv04_graph_create(dev);
751 			break;
752 		case NV_10:
753 			nv10_graph_create(dev);
754 			break;
755 		case NV_20:
756 		case NV_30:
757 			nv20_graph_create(dev);
758 			break;
759 		case NV_40:
760 			nv40_graph_create(dev);
761 			break;
762 		case NV_50:
763 			nv50_graph_create(dev);
764 			break;
765 		case NV_C0:
766 		case NV_D0:
767 			nvc0_graph_create(dev);
768 			break;
769 		default:
770 			break;
771 		}
772 
773 		switch (dev_priv->chipset) {
774 		case 0x84:
775 		case 0x86:
776 		case 0x92:
777 		case 0x94:
778 		case 0x96:
779 		case 0xa0:
780 			nv84_crypt_create(dev);
781 			break;
782 		case 0x98:
783 		case 0xaa:
784 		case 0xac:
785 			nv98_crypt_create(dev);
786 			break;
787 		}
788 
789 		switch (dev_priv->card_type) {
790 		case NV_50:
791 			switch (dev_priv->chipset) {
792 			case 0xa3:
793 			case 0xa5:
794 			case 0xa8:
795 			case 0xaf:
796 				nva3_copy_create(dev);
797 				break;
798 			}
799 			break;
800 		case NV_C0:
801 			nvc0_copy_create(dev, 0);
802 			nvc0_copy_create(dev, 1);
803 			break;
804 		default:
805 			break;
806 		}
807 
808 		if (dev_priv->chipset >= 0xa3 || dev_priv->chipset == 0x98) {
809 			nv84_bsp_create(dev);
810 			nv84_vp_create(dev);
811 			nv98_ppp_create(dev);
812 		} else
813 		if (dev_priv->chipset >= 0x84) {
814 			nv50_mpeg_create(dev);
815 			nv84_bsp_create(dev);
816 			nv84_vp_create(dev);
817 		} else
818 		if (dev_priv->chipset >= 0x50) {
819 			nv50_mpeg_create(dev);
820 		} else
821 		if (dev_priv->card_type == NV_40 ||
822 		    dev_priv->chipset == 0x31 ||
823 		    dev_priv->chipset == 0x34 ||
824 		    dev_priv->chipset == 0x36) {
825 			nv31_mpeg_create(dev);
826 		}
827 
828 		for (e = 0; e < NVOBJ_ENGINE_NR; e++) {
829 			if (dev_priv->eng[e]) {
830 				ret = dev_priv->eng[e]->init(dev, e);
831 				if (ret)
832 					goto out_engine;
833 			}
834 		}
835 
836 		/* PFIFO */
837 		ret = engine->fifo.init(dev);
838 		if (ret)
839 			goto out_engine;
840 	}
841 
842 	ret = nouveau_irq_init(dev);
843 	if (ret)
844 		goto out_fifo;
845 
846 	ret = nouveau_display_create(dev);
847 	if (ret)
848 		goto out_irq;
849 
850 	nouveau_backlight_init(dev);
851 	nouveau_pm_init(dev);
852 
853 	ret = nouveau_fence_init(dev);
854 	if (ret)
855 		goto out_pm;
856 
857 	if (dev_priv->eng[NVOBJ_ENGINE_GR]) {
858 		ret = nouveau_card_channel_init(dev);
859 		if (ret)
860 			goto out_fence;
861 	}
862 
863 	if (dev->mode_config.num_crtc) {
864 		ret = nouveau_display_init(dev);
865 		if (ret)
866 			goto out_chan;
867 
868 		nouveau_fbcon_init(dev);
869 	}
870 
871 	return 0;
872 
873 out_chan:
874 	nouveau_card_channel_fini(dev);
875 out_fence:
876 	nouveau_fence_fini(dev);
877 out_pm:
878 	nouveau_pm_fini(dev);
879 	nouveau_backlight_exit(dev);
880 	nouveau_display_destroy(dev);
881 out_irq:
882 	nouveau_irq_fini(dev);
883 out_fifo:
884 	if (!dev_priv->noaccel)
885 		engine->fifo.takedown(dev);
886 out_engine:
887 	if (!dev_priv->noaccel) {
888 		for (e = e - 1; e >= 0; e--) {
889 			if (!dev_priv->eng[e])
890 				continue;
891 			dev_priv->eng[e]->fini(dev, e, false);
892 			dev_priv->eng[e]->destroy(dev,e );
893 		}
894 	}
895 	nouveau_mem_gart_fini(dev);
896 out_ttmvram:
897 	nouveau_mem_vram_fini(dev);
898 out_instmem:
899 	engine->instmem.takedown(dev);
900 out_gpuobj:
901 	nouveau_gpuobj_takedown(dev);
902 out_gpio:
903 	nouveau_gpio_destroy(dev);
904 out_vram:
905 	engine->vram.takedown(dev);
906 out_fb:
907 	engine->fb.takedown(dev);
908 out_timer:
909 	engine->timer.takedown(dev);
910 out_mc:
911 	engine->mc.takedown(dev);
912 out_bios:
913 	nouveau_bios_takedown(dev);
914 out_display_early:
915 	engine->display.late_takedown(dev);
916 out:
917 	vga_client_register(dev->pdev, NULL, NULL, NULL);
918 	return ret;
919 }
920 
nouveau_card_takedown(struct drm_device * dev)921 static void nouveau_card_takedown(struct drm_device *dev)
922 {
923 	struct drm_nouveau_private *dev_priv = dev->dev_private;
924 	struct nouveau_engine *engine = &dev_priv->engine;
925 	int e;
926 
927 	if (dev->mode_config.num_crtc) {
928 		nouveau_fbcon_fini(dev);
929 		nouveau_display_fini(dev);
930 	}
931 
932 	nouveau_card_channel_fini(dev);
933 	nouveau_fence_fini(dev);
934 	nouveau_pm_fini(dev);
935 	nouveau_backlight_exit(dev);
936 	nouveau_display_destroy(dev);
937 
938 	if (!dev_priv->noaccel) {
939 		engine->fifo.takedown(dev);
940 		for (e = NVOBJ_ENGINE_NR - 1; e >= 0; e--) {
941 			if (dev_priv->eng[e]) {
942 				dev_priv->eng[e]->fini(dev, e, false);
943 				dev_priv->eng[e]->destroy(dev,e );
944 			}
945 		}
946 	}
947 
948 	if (dev_priv->vga_ram) {
949 		nouveau_bo_unpin(dev_priv->vga_ram);
950 		nouveau_bo_ref(NULL, &dev_priv->vga_ram);
951 	}
952 
953 	mutex_lock(&dev->struct_mutex);
954 	ttm_bo_clean_mm(&dev_priv->ttm.bdev, TTM_PL_VRAM);
955 	ttm_bo_clean_mm(&dev_priv->ttm.bdev, TTM_PL_TT);
956 	mutex_unlock(&dev->struct_mutex);
957 	nouveau_mem_gart_fini(dev);
958 	nouveau_mem_vram_fini(dev);
959 
960 	engine->instmem.takedown(dev);
961 	nouveau_gpuobj_takedown(dev);
962 
963 	nouveau_gpio_destroy(dev);
964 	engine->vram.takedown(dev);
965 	engine->fb.takedown(dev);
966 	engine->timer.takedown(dev);
967 	engine->mc.takedown(dev);
968 
969 	nouveau_bios_takedown(dev);
970 	engine->display.late_takedown(dev);
971 
972 	nouveau_irq_fini(dev);
973 
974 	vga_client_register(dev->pdev, NULL, NULL, NULL);
975 }
976 
977 int
nouveau_open(struct drm_device * dev,struct drm_file * file_priv)978 nouveau_open(struct drm_device *dev, struct drm_file *file_priv)
979 {
980 	struct drm_nouveau_private *dev_priv = dev->dev_private;
981 	struct nouveau_fpriv *fpriv;
982 	int ret;
983 
984 	fpriv = kzalloc(sizeof(*fpriv), GFP_KERNEL);
985 	if (unlikely(!fpriv))
986 		return -ENOMEM;
987 
988 	spin_lock_init(&fpriv->lock);
989 	INIT_LIST_HEAD(&fpriv->channels);
990 
991 	if (dev_priv->card_type == NV_50) {
992 		ret = nouveau_vm_new(dev, 0, (1ULL << 40), 0x0020000000ULL,
993 				     &fpriv->vm);
994 		if (ret) {
995 			kfree(fpriv);
996 			return ret;
997 		}
998 	} else
999 	if (dev_priv->card_type >= NV_C0) {
1000 		ret = nouveau_vm_new(dev, 0, (1ULL << 40), 0x0008000000ULL,
1001 				     &fpriv->vm);
1002 		if (ret) {
1003 			kfree(fpriv);
1004 			return ret;
1005 		}
1006 	}
1007 
1008 	file_priv->driver_priv = fpriv;
1009 	return 0;
1010 }
1011 
1012 /* here a client dies, release the stuff that was allocated for its
1013  * file_priv */
nouveau_preclose(struct drm_device * dev,struct drm_file * file_priv)1014 void nouveau_preclose(struct drm_device *dev, struct drm_file *file_priv)
1015 {
1016 	nouveau_channel_cleanup(dev, file_priv);
1017 }
1018 
1019 void
nouveau_postclose(struct drm_device * dev,struct drm_file * file_priv)1020 nouveau_postclose(struct drm_device *dev, struct drm_file *file_priv)
1021 {
1022 	struct nouveau_fpriv *fpriv = nouveau_fpriv(file_priv);
1023 	nouveau_vm_ref(NULL, &fpriv->vm, NULL);
1024 	kfree(fpriv);
1025 }
1026 
1027 /* first module load, setup the mmio/fb mapping */
1028 /* KMS: we need mmio at load time, not when the first drm client opens. */
nouveau_firstopen(struct drm_device * dev)1029 int nouveau_firstopen(struct drm_device *dev)
1030 {
1031 	return 0;
1032 }
1033 
1034 /* if we have an OF card, copy vbios to RAMIN */
nouveau_OF_copy_vbios_to_ramin(struct drm_device * dev)1035 static void nouveau_OF_copy_vbios_to_ramin(struct drm_device *dev)
1036 {
1037 #if defined(__powerpc__)
1038 	int size, i;
1039 	const uint32_t *bios;
1040 	struct device_node *dn = pci_device_to_OF_node(dev->pdev);
1041 	if (!dn) {
1042 		NV_INFO(dev, "Unable to get the OF node\n");
1043 		return;
1044 	}
1045 
1046 	bios = of_get_property(dn, "NVDA,BMP", &size);
1047 	if (bios) {
1048 		for (i = 0; i < size; i += 4)
1049 			nv_wi32(dev, i, bios[i/4]);
1050 		NV_INFO(dev, "OF bios successfully copied (%d bytes)\n", size);
1051 	} else {
1052 		NV_INFO(dev, "Unable to get the OF bios\n");
1053 	}
1054 #endif
1055 }
1056 
nouveau_get_apertures(struct drm_device * dev)1057 static struct apertures_struct *nouveau_get_apertures(struct drm_device *dev)
1058 {
1059 	struct pci_dev *pdev = dev->pdev;
1060 	struct apertures_struct *aper = alloc_apertures(3);
1061 	if (!aper)
1062 		return NULL;
1063 
1064 	aper->ranges[0].base = pci_resource_start(pdev, 1);
1065 	aper->ranges[0].size = pci_resource_len(pdev, 1);
1066 	aper->count = 1;
1067 
1068 	if (pci_resource_len(pdev, 2)) {
1069 		aper->ranges[aper->count].base = pci_resource_start(pdev, 2);
1070 		aper->ranges[aper->count].size = pci_resource_len(pdev, 2);
1071 		aper->count++;
1072 	}
1073 
1074 	if (pci_resource_len(pdev, 3)) {
1075 		aper->ranges[aper->count].base = pci_resource_start(pdev, 3);
1076 		aper->ranges[aper->count].size = pci_resource_len(pdev, 3);
1077 		aper->count++;
1078 	}
1079 
1080 	return aper;
1081 }
1082 
nouveau_remove_conflicting_drivers(struct drm_device * dev)1083 static int nouveau_remove_conflicting_drivers(struct drm_device *dev)
1084 {
1085 	struct drm_nouveau_private *dev_priv = dev->dev_private;
1086 	bool primary = false;
1087 	dev_priv->apertures = nouveau_get_apertures(dev);
1088 	if (!dev_priv->apertures)
1089 		return -ENOMEM;
1090 
1091 #ifdef CONFIG_X86
1092 	primary = dev->pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW;
1093 #endif
1094 
1095 	remove_conflicting_framebuffers(dev_priv->apertures, "nouveaufb", primary);
1096 	return 0;
1097 }
1098 
nouveau_load(struct drm_device * dev,unsigned long flags)1099 int nouveau_load(struct drm_device *dev, unsigned long flags)
1100 {
1101 	struct drm_nouveau_private *dev_priv;
1102 	unsigned long long offset, length;
1103 	uint32_t reg0 = ~0, strap;
1104 	int ret;
1105 
1106 	dev_priv = kzalloc(sizeof(*dev_priv), GFP_KERNEL);
1107 	if (!dev_priv) {
1108 		ret = -ENOMEM;
1109 		goto err_out;
1110 	}
1111 	dev->dev_private = dev_priv;
1112 	dev_priv->dev = dev;
1113 
1114 	pci_set_master(dev->pdev);
1115 
1116 	dev_priv->flags = flags & NOUVEAU_FLAGS;
1117 
1118 	NV_DEBUG(dev, "vendor: 0x%X device: 0x%X class: 0x%X\n",
1119 		 dev->pci_vendor, dev->pci_device, dev->pdev->class);
1120 
1121 	/* first up, map the start of mmio and determine the chipset */
1122 	dev_priv->mmio = ioremap(pci_resource_start(dev->pdev, 0), PAGE_SIZE);
1123 	if (dev_priv->mmio) {
1124 #ifdef __BIG_ENDIAN
1125 		/* put the card into big-endian mode if it's not */
1126 		if (nv_rd32(dev, NV03_PMC_BOOT_1) != 0x01000001)
1127 			nv_wr32(dev, NV03_PMC_BOOT_1, 0x01000001);
1128 		DRM_MEMORYBARRIER();
1129 #endif
1130 
1131 		/* determine chipset and derive architecture from it */
1132 		reg0 = nv_rd32(dev, NV03_PMC_BOOT_0);
1133 		if ((reg0 & 0x0f000000) > 0) {
1134 			dev_priv->chipset = (reg0 & 0xff00000) >> 20;
1135 			switch (dev_priv->chipset & 0xf0) {
1136 			case 0x10:
1137 			case 0x20:
1138 			case 0x30:
1139 				dev_priv->card_type = dev_priv->chipset & 0xf0;
1140 				break;
1141 			case 0x40:
1142 			case 0x60:
1143 				dev_priv->card_type = NV_40;
1144 				break;
1145 			case 0x50:
1146 			case 0x80:
1147 			case 0x90:
1148 			case 0xa0:
1149 				dev_priv->card_type = NV_50;
1150 				break;
1151 			case 0xc0:
1152 				dev_priv->card_type = NV_C0;
1153 				break;
1154 			case 0xd0:
1155 				dev_priv->card_type = NV_D0;
1156 				break;
1157 			case 0xe0:
1158 				dev_priv->card_type = NV_E0;
1159 				break;
1160 			default:
1161 				break;
1162 			}
1163 		} else
1164 		if ((reg0 & 0xff00fff0) == 0x20004000) {
1165 			if (reg0 & 0x00f00000)
1166 				dev_priv->chipset = 0x05;
1167 			else
1168 				dev_priv->chipset = 0x04;
1169 			dev_priv->card_type = NV_04;
1170 		}
1171 
1172 		iounmap(dev_priv->mmio);
1173 	}
1174 
1175 	if (!dev_priv->card_type) {
1176 		NV_ERROR(dev, "unsupported chipset 0x%08x\n", reg0);
1177 		ret = -EINVAL;
1178 		goto err_priv;
1179 	}
1180 
1181 	NV_INFO(dev, "Detected an NV%2x generation card (0x%08x)\n",
1182 		     dev_priv->card_type, reg0);
1183 
1184 	/* map the mmio regs, limiting the amount to preserve vmap space */
1185 	offset = pci_resource_start(dev->pdev, 0);
1186 	length = pci_resource_len(dev->pdev, 0);
1187 	if (dev_priv->card_type < NV_E0)
1188 		length = min(length, (unsigned long long)0x00800000);
1189 
1190 	dev_priv->mmio = ioremap(offset, length);
1191 	if (!dev_priv->mmio) {
1192 		NV_ERROR(dev, "Unable to initialize the mmio mapping. "
1193 			 "Please report your setup to " DRIVER_EMAIL "\n");
1194 		ret = -EINVAL;
1195 		goto err_priv;
1196 	}
1197 	NV_DEBUG(dev, "regs mapped ok at 0x%llx\n", offset);
1198 
1199 	/* determine frequency of timing crystal */
1200 	strap = nv_rd32(dev, 0x101000);
1201 	if ( dev_priv->chipset < 0x17 ||
1202 	    (dev_priv->chipset >= 0x20 && dev_priv->chipset <= 0x25))
1203 		strap &= 0x00000040;
1204 	else
1205 		strap &= 0x00400040;
1206 
1207 	switch (strap) {
1208 	case 0x00000000: dev_priv->crystal = 13500; break;
1209 	case 0x00000040: dev_priv->crystal = 14318; break;
1210 	case 0x00400000: dev_priv->crystal = 27000; break;
1211 	case 0x00400040: dev_priv->crystal = 25000; break;
1212 	}
1213 
1214 	NV_DEBUG(dev, "crystal freq: %dKHz\n", dev_priv->crystal);
1215 
1216 	/* Determine whether we'll attempt acceleration or not, some
1217 	 * cards are disabled by default here due to them being known
1218 	 * non-functional, or never been tested due to lack of hw.
1219 	 */
1220 	dev_priv->noaccel = !!nouveau_noaccel;
1221 	if (nouveau_noaccel == -1) {
1222 		switch (dev_priv->chipset) {
1223 		case 0xd9: /* known broken */
1224 			NV_INFO(dev, "acceleration disabled by default, pass "
1225 				     "noaccel=0 to force enable\n");
1226 			dev_priv->noaccel = true;
1227 			break;
1228 		default:
1229 			dev_priv->noaccel = false;
1230 			break;
1231 		}
1232 	}
1233 
1234 	ret = nouveau_remove_conflicting_drivers(dev);
1235 	if (ret)
1236 		goto err_mmio;
1237 
1238 	/* Map PRAMIN BAR, or on older cards, the aperture within BAR0 */
1239 	if (dev_priv->card_type >= NV_40) {
1240 		int ramin_bar = 2;
1241 		if (pci_resource_len(dev->pdev, ramin_bar) == 0)
1242 			ramin_bar = 3;
1243 
1244 		dev_priv->ramin_size = pci_resource_len(dev->pdev, ramin_bar);
1245 		dev_priv->ramin =
1246 			ioremap(pci_resource_start(dev->pdev, ramin_bar),
1247 				dev_priv->ramin_size);
1248 		if (!dev_priv->ramin) {
1249 			NV_ERROR(dev, "Failed to map PRAMIN BAR\n");
1250 			ret = -ENOMEM;
1251 			goto err_mmio;
1252 		}
1253 	} else {
1254 		dev_priv->ramin_size = 1 * 1024 * 1024;
1255 		dev_priv->ramin = ioremap(offset + NV_RAMIN,
1256 					  dev_priv->ramin_size);
1257 		if (!dev_priv->ramin) {
1258 			NV_ERROR(dev, "Failed to map BAR0 PRAMIN.\n");
1259 			ret = -ENOMEM;
1260 			goto err_mmio;
1261 		}
1262 	}
1263 
1264 	nouveau_OF_copy_vbios_to_ramin(dev);
1265 
1266 	/* Special flags */
1267 	if (dev->pci_device == 0x01a0)
1268 		dev_priv->flags |= NV_NFORCE;
1269 	else if (dev->pci_device == 0x01f0)
1270 		dev_priv->flags |= NV_NFORCE2;
1271 
1272 	/* For kernel modesetting, init card now and bring up fbcon */
1273 	ret = nouveau_card_init(dev);
1274 	if (ret)
1275 		goto err_ramin;
1276 
1277 	return 0;
1278 
1279 err_ramin:
1280 	iounmap(dev_priv->ramin);
1281 err_mmio:
1282 	iounmap(dev_priv->mmio);
1283 err_priv:
1284 	kfree(dev_priv);
1285 	dev->dev_private = NULL;
1286 err_out:
1287 	return ret;
1288 }
1289 
nouveau_lastclose(struct drm_device * dev)1290 void nouveau_lastclose(struct drm_device *dev)
1291 {
1292 	vga_switcheroo_process_delayed_switch();
1293 }
1294 
nouveau_unload(struct drm_device * dev)1295 int nouveau_unload(struct drm_device *dev)
1296 {
1297 	struct drm_nouveau_private *dev_priv = dev->dev_private;
1298 
1299 	nouveau_card_takedown(dev);
1300 
1301 	iounmap(dev_priv->mmio);
1302 	iounmap(dev_priv->ramin);
1303 
1304 	kfree(dev_priv);
1305 	dev->dev_private = NULL;
1306 	return 0;
1307 }
1308 
nouveau_ioctl_getparam(struct drm_device * dev,void * data,struct drm_file * file_priv)1309 int nouveau_ioctl_getparam(struct drm_device *dev, void *data,
1310 						struct drm_file *file_priv)
1311 {
1312 	struct drm_nouveau_private *dev_priv = dev->dev_private;
1313 	struct drm_nouveau_getparam *getparam = data;
1314 
1315 	switch (getparam->param) {
1316 	case NOUVEAU_GETPARAM_CHIPSET_ID:
1317 		getparam->value = dev_priv->chipset;
1318 		break;
1319 	case NOUVEAU_GETPARAM_PCI_VENDOR:
1320 		getparam->value = dev->pci_vendor;
1321 		break;
1322 	case NOUVEAU_GETPARAM_PCI_DEVICE:
1323 		getparam->value = dev->pci_device;
1324 		break;
1325 	case NOUVEAU_GETPARAM_BUS_TYPE:
1326 		if (drm_pci_device_is_agp(dev))
1327 			getparam->value = NV_AGP;
1328 		else if (pci_is_pcie(dev->pdev))
1329 			getparam->value = NV_PCIE;
1330 		else
1331 			getparam->value = NV_PCI;
1332 		break;
1333 	case NOUVEAU_GETPARAM_FB_SIZE:
1334 		getparam->value = dev_priv->fb_available_size;
1335 		break;
1336 	case NOUVEAU_GETPARAM_AGP_SIZE:
1337 		getparam->value = dev_priv->gart_info.aper_size;
1338 		break;
1339 	case NOUVEAU_GETPARAM_VM_VRAM_BASE:
1340 		getparam->value = 0; /* deprecated */
1341 		break;
1342 	case NOUVEAU_GETPARAM_PTIMER_TIME:
1343 		getparam->value = dev_priv->engine.timer.read(dev);
1344 		break;
1345 	case NOUVEAU_GETPARAM_HAS_BO_USAGE:
1346 		getparam->value = 1;
1347 		break;
1348 	case NOUVEAU_GETPARAM_HAS_PAGEFLIP:
1349 		getparam->value = 1;
1350 		break;
1351 	case NOUVEAU_GETPARAM_GRAPH_UNITS:
1352 		/* NV40 and NV50 versions are quite different, but register
1353 		 * address is the same. User is supposed to know the card
1354 		 * family anyway... */
1355 		if (dev_priv->chipset >= 0x40) {
1356 			getparam->value = nv_rd32(dev, NV40_PMC_GRAPH_UNITS);
1357 			break;
1358 		}
1359 		/* FALLTHRU */
1360 	default:
1361 		NV_DEBUG(dev, "unknown parameter %lld\n", getparam->param);
1362 		return -EINVAL;
1363 	}
1364 
1365 	return 0;
1366 }
1367 
1368 int
nouveau_ioctl_setparam(struct drm_device * dev,void * data,struct drm_file * file_priv)1369 nouveau_ioctl_setparam(struct drm_device *dev, void *data,
1370 		       struct drm_file *file_priv)
1371 {
1372 	struct drm_nouveau_setparam *setparam = data;
1373 
1374 	switch (setparam->param) {
1375 	default:
1376 		NV_DEBUG(dev, "unknown parameter %lld\n", setparam->param);
1377 		return -EINVAL;
1378 	}
1379 
1380 	return 0;
1381 }
1382 
1383 /* Wait until (value(reg) & mask) == val, up until timeout has hit */
1384 bool
nouveau_wait_eq(struct drm_device * dev,uint64_t timeout,uint32_t reg,uint32_t mask,uint32_t val)1385 nouveau_wait_eq(struct drm_device *dev, uint64_t timeout,
1386 		uint32_t reg, uint32_t mask, uint32_t val)
1387 {
1388 	struct drm_nouveau_private *dev_priv = dev->dev_private;
1389 	struct nouveau_timer_engine *ptimer = &dev_priv->engine.timer;
1390 	uint64_t start = ptimer->read(dev);
1391 
1392 	do {
1393 		if ((nv_rd32(dev, reg) & mask) == val)
1394 			return true;
1395 	} while (ptimer->read(dev) - start < timeout);
1396 
1397 	return false;
1398 }
1399 
1400 /* Wait until (value(reg) & mask) != val, up until timeout has hit */
1401 bool
nouveau_wait_ne(struct drm_device * dev,uint64_t timeout,uint32_t reg,uint32_t mask,uint32_t val)1402 nouveau_wait_ne(struct drm_device *dev, uint64_t timeout,
1403 		uint32_t reg, uint32_t mask, uint32_t val)
1404 {
1405 	struct drm_nouveau_private *dev_priv = dev->dev_private;
1406 	struct nouveau_timer_engine *ptimer = &dev_priv->engine.timer;
1407 	uint64_t start = ptimer->read(dev);
1408 
1409 	do {
1410 		if ((nv_rd32(dev, reg) & mask) != val)
1411 			return true;
1412 	} while (ptimer->read(dev) - start < timeout);
1413 
1414 	return false;
1415 }
1416 
1417 /* Wait until cond(data) == true, up until timeout has hit */
1418 bool
nouveau_wait_cb(struct drm_device * dev,u64 timeout,bool (* cond)(void *),void * data)1419 nouveau_wait_cb(struct drm_device *dev, u64 timeout,
1420 		bool (*cond)(void *), void *data)
1421 {
1422 	struct drm_nouveau_private *dev_priv = dev->dev_private;
1423 	struct nouveau_timer_engine *ptimer = &dev_priv->engine.timer;
1424 	u64 start = ptimer->read(dev);
1425 
1426 	do {
1427 		if (cond(data) == true)
1428 			return true;
1429 	} while (ptimer->read(dev) - start < timeout);
1430 
1431 	return false;
1432 }
1433 
1434 /* Waits for PGRAPH to go completely idle */
nouveau_wait_for_idle(struct drm_device * dev)1435 bool nouveau_wait_for_idle(struct drm_device *dev)
1436 {
1437 	struct drm_nouveau_private *dev_priv = dev->dev_private;
1438 	uint32_t mask = ~0;
1439 
1440 	if (dev_priv->card_type == NV_40)
1441 		mask &= ~NV40_PGRAPH_STATUS_SYNC_STALL;
1442 
1443 	if (!nv_wait(dev, NV04_PGRAPH_STATUS, mask, 0)) {
1444 		NV_ERROR(dev, "PGRAPH idle timed out with status 0x%08x\n",
1445 			 nv_rd32(dev, NV04_PGRAPH_STATUS));
1446 		return false;
1447 	}
1448 
1449 	return true;
1450 }
1451 
1452