2 * Copyright 2005 Stephane Marchesin
3 * Copyright 2008 Stuart Bennett
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:
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
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.
26 #include <linux/swab.h>
27 #include <linux/slab.h>
30 #include "drm_sarea.h"
31 #include "drm_crtc_helper.h"
32 #include <linux/vgaarb.h>
33 #include <linux/vga_switcheroo.h>
35 #include "nouveau_drv.h"
36 #include "nouveau_drm.h"
37 #include "nouveau_fbcon.h"
38 #include "nouveau_ramht.h"
39 #include "nouveau_pm.h"
40 #include "nv50_display.h"
42 static void nouveau_stub_takedown(struct drm_device *dev) {}
43 static int nouveau_stub_init(struct drm_device *dev) { return 0; }
45 static int nouveau_init_engine_ptrs(struct drm_device *dev)
47 struct drm_nouveau_private *dev_priv = dev->dev_private;
48 struct nouveau_engine *engine = &dev_priv->engine;
50 switch (dev_priv->chipset & 0xf0) {
52 engine->instmem.init = nv04_instmem_init;
53 engine->instmem.takedown = nv04_instmem_takedown;
54 engine->instmem.suspend = nv04_instmem_suspend;
55 engine->instmem.resume = nv04_instmem_resume;
56 engine->instmem.get = nv04_instmem_get;
57 engine->instmem.put = nv04_instmem_put;
58 engine->instmem.map = nv04_instmem_map;
59 engine->instmem.unmap = nv04_instmem_unmap;
60 engine->instmem.flush = nv04_instmem_flush;
61 engine->mc.init = nv04_mc_init;
62 engine->mc.takedown = nv04_mc_takedown;
63 engine->timer.init = nv04_timer_init;
64 engine->timer.read = nv04_timer_read;
65 engine->timer.takedown = nv04_timer_takedown;
66 engine->fb.init = nv04_fb_init;
67 engine->fb.takedown = nv04_fb_takedown;
68 engine->fifo.channels = 16;
69 engine->fifo.init = nv04_fifo_init;
70 engine->fifo.takedown = nv04_fifo_fini;
71 engine->fifo.disable = nv04_fifo_disable;
72 engine->fifo.enable = nv04_fifo_enable;
73 engine->fifo.reassign = nv04_fifo_reassign;
74 engine->fifo.cache_pull = nv04_fifo_cache_pull;
75 engine->fifo.channel_id = nv04_fifo_channel_id;
76 engine->fifo.create_context = nv04_fifo_create_context;
77 engine->fifo.destroy_context = nv04_fifo_destroy_context;
78 engine->fifo.load_context = nv04_fifo_load_context;
79 engine->fifo.unload_context = nv04_fifo_unload_context;
80 engine->display.early_init = nv04_display_early_init;
81 engine->display.late_takedown = nv04_display_late_takedown;
82 engine->display.create = nv04_display_create;
83 engine->display.init = nv04_display_init;
84 engine->display.destroy = nv04_display_destroy;
85 engine->gpio.init = nouveau_stub_init;
86 engine->gpio.takedown = nouveau_stub_takedown;
87 engine->gpio.get = NULL;
88 engine->gpio.set = NULL;
89 engine->gpio.irq_enable = NULL;
90 engine->pm.clock_get = nv04_pm_clock_get;
91 engine->pm.clock_pre = nv04_pm_clock_pre;
92 engine->pm.clock_set = nv04_pm_clock_set;
93 engine->vram.init = nouveau_mem_detect;
94 engine->vram.takedown = nouveau_stub_takedown;
95 engine->vram.flags_valid = nouveau_mem_flags_valid;
98 engine->instmem.init = nv04_instmem_init;
99 engine->instmem.takedown = nv04_instmem_takedown;
100 engine->instmem.suspend = nv04_instmem_suspend;
101 engine->instmem.resume = nv04_instmem_resume;
102 engine->instmem.get = nv04_instmem_get;
103 engine->instmem.put = nv04_instmem_put;
104 engine->instmem.map = nv04_instmem_map;
105 engine->instmem.unmap = nv04_instmem_unmap;
106 engine->instmem.flush = nv04_instmem_flush;
107 engine->mc.init = nv04_mc_init;
108 engine->mc.takedown = nv04_mc_takedown;
109 engine->timer.init = nv04_timer_init;
110 engine->timer.read = nv04_timer_read;
111 engine->timer.takedown = nv04_timer_takedown;
112 engine->fb.init = nv10_fb_init;
113 engine->fb.takedown = nv10_fb_takedown;
114 engine->fb.init_tile_region = nv10_fb_init_tile_region;
115 engine->fb.set_tile_region = nv10_fb_set_tile_region;
116 engine->fb.free_tile_region = nv10_fb_free_tile_region;
117 engine->fifo.channels = 32;
118 engine->fifo.init = nv10_fifo_init;
119 engine->fifo.takedown = nv04_fifo_fini;
120 engine->fifo.disable = nv04_fifo_disable;
121 engine->fifo.enable = nv04_fifo_enable;
122 engine->fifo.reassign = nv04_fifo_reassign;
123 engine->fifo.cache_pull = nv04_fifo_cache_pull;
124 engine->fifo.channel_id = nv10_fifo_channel_id;
125 engine->fifo.create_context = nv10_fifo_create_context;
126 engine->fifo.destroy_context = nv04_fifo_destroy_context;
127 engine->fifo.load_context = nv10_fifo_load_context;
128 engine->fifo.unload_context = nv10_fifo_unload_context;
129 engine->display.early_init = nv04_display_early_init;
130 engine->display.late_takedown = nv04_display_late_takedown;
131 engine->display.create = nv04_display_create;
132 engine->display.init = nv04_display_init;
133 engine->display.destroy = nv04_display_destroy;
134 engine->gpio.init = nouveau_stub_init;
135 engine->gpio.takedown = nouveau_stub_takedown;
136 engine->gpio.get = nv10_gpio_get;
137 engine->gpio.set = nv10_gpio_set;
138 engine->gpio.irq_enable = NULL;
139 engine->pm.clock_get = nv04_pm_clock_get;
140 engine->pm.clock_pre = nv04_pm_clock_pre;
141 engine->pm.clock_set = nv04_pm_clock_set;
142 engine->vram.init = nouveau_mem_detect;
143 engine->vram.takedown = nouveau_stub_takedown;
144 engine->vram.flags_valid = nouveau_mem_flags_valid;
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 = nv10_fb_init;
162 engine->fb.takedown = nv10_fb_takedown;
163 engine->fb.init_tile_region = nv10_fb_init_tile_region;
164 engine->fb.set_tile_region = nv10_fb_set_tile_region;
165 engine->fb.free_tile_region = nv10_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.init = nv04_display_init;
182 engine->display.destroy = nv04_display_destroy;
183 engine->gpio.init = nouveau_stub_init;
184 engine->gpio.takedown = nouveau_stub_takedown;
185 engine->gpio.get = nv10_gpio_get;
186 engine->gpio.set = nv10_gpio_set;
187 engine->gpio.irq_enable = NULL;
188 engine->pm.clock_get = nv04_pm_clock_get;
189 engine->pm.clock_pre = nv04_pm_clock_pre;
190 engine->pm.clock_set = nv04_pm_clock_set;
191 engine->vram.init = nouveau_mem_detect;
192 engine->vram.takedown = nouveau_stub_takedown;
193 engine->vram.flags_valid = nouveau_mem_flags_valid;
196 engine->instmem.init = nv04_instmem_init;
197 engine->instmem.takedown = nv04_instmem_takedown;
198 engine->instmem.suspend = nv04_instmem_suspend;
199 engine->instmem.resume = nv04_instmem_resume;
200 engine->instmem.get = nv04_instmem_get;
201 engine->instmem.put = nv04_instmem_put;
202 engine->instmem.map = nv04_instmem_map;
203 engine->instmem.unmap = nv04_instmem_unmap;
204 engine->instmem.flush = nv04_instmem_flush;
205 engine->mc.init = nv04_mc_init;
206 engine->mc.takedown = nv04_mc_takedown;
207 engine->timer.init = nv04_timer_init;
208 engine->timer.read = nv04_timer_read;
209 engine->timer.takedown = nv04_timer_takedown;
210 engine->fb.init = nv30_fb_init;
211 engine->fb.takedown = nv30_fb_takedown;
212 engine->fb.init_tile_region = nv30_fb_init_tile_region;
213 engine->fb.set_tile_region = nv10_fb_set_tile_region;
214 engine->fb.free_tile_region = nv30_fb_free_tile_region;
215 engine->fifo.channels = 32;
216 engine->fifo.init = nv10_fifo_init;
217 engine->fifo.takedown = nv04_fifo_fini;
218 engine->fifo.disable = nv04_fifo_disable;
219 engine->fifo.enable = nv04_fifo_enable;
220 engine->fifo.reassign = nv04_fifo_reassign;
221 engine->fifo.cache_pull = nv04_fifo_cache_pull;
222 engine->fifo.channel_id = nv10_fifo_channel_id;
223 engine->fifo.create_context = nv10_fifo_create_context;
224 engine->fifo.destroy_context = nv04_fifo_destroy_context;
225 engine->fifo.load_context = nv10_fifo_load_context;
226 engine->fifo.unload_context = nv10_fifo_unload_context;
227 engine->display.early_init = nv04_display_early_init;
228 engine->display.late_takedown = nv04_display_late_takedown;
229 engine->display.create = nv04_display_create;
230 engine->display.init = nv04_display_init;
231 engine->display.destroy = nv04_display_destroy;
232 engine->gpio.init = nouveau_stub_init;
233 engine->gpio.takedown = nouveau_stub_takedown;
234 engine->gpio.get = nv10_gpio_get;
235 engine->gpio.set = nv10_gpio_set;
236 engine->gpio.irq_enable = NULL;
237 engine->pm.clock_get = nv04_pm_clock_get;
238 engine->pm.clock_pre = nv04_pm_clock_pre;
239 engine->pm.clock_set = nv04_pm_clock_set;
240 engine->pm.voltage_get = nouveau_voltage_gpio_get;
241 engine->pm.voltage_set = nouveau_voltage_gpio_set;
242 engine->vram.init = nouveau_mem_detect;
243 engine->vram.takedown = nouveau_stub_takedown;
244 engine->vram.flags_valid = nouveau_mem_flags_valid;
248 engine->instmem.init = nv04_instmem_init;
249 engine->instmem.takedown = nv04_instmem_takedown;
250 engine->instmem.suspend = nv04_instmem_suspend;
251 engine->instmem.resume = nv04_instmem_resume;
252 engine->instmem.get = nv04_instmem_get;
253 engine->instmem.put = nv04_instmem_put;
254 engine->instmem.map = nv04_instmem_map;
255 engine->instmem.unmap = nv04_instmem_unmap;
256 engine->instmem.flush = nv04_instmem_flush;
257 engine->mc.init = nv40_mc_init;
258 engine->mc.takedown = nv40_mc_takedown;
259 engine->timer.init = nv04_timer_init;
260 engine->timer.read = nv04_timer_read;
261 engine->timer.takedown = nv04_timer_takedown;
262 engine->fb.init = nv40_fb_init;
263 engine->fb.takedown = nv40_fb_takedown;
264 engine->fb.init_tile_region = nv30_fb_init_tile_region;
265 engine->fb.set_tile_region = nv40_fb_set_tile_region;
266 engine->fb.free_tile_region = nv30_fb_free_tile_region;
267 engine->fifo.channels = 32;
268 engine->fifo.init = nv40_fifo_init;
269 engine->fifo.takedown = nv04_fifo_fini;
270 engine->fifo.disable = nv04_fifo_disable;
271 engine->fifo.enable = nv04_fifo_enable;
272 engine->fifo.reassign = nv04_fifo_reassign;
273 engine->fifo.cache_pull = nv04_fifo_cache_pull;
274 engine->fifo.channel_id = nv10_fifo_channel_id;
275 engine->fifo.create_context = nv40_fifo_create_context;
276 engine->fifo.destroy_context = nv04_fifo_destroy_context;
277 engine->fifo.load_context = nv40_fifo_load_context;
278 engine->fifo.unload_context = nv40_fifo_unload_context;
279 engine->display.early_init = nv04_display_early_init;
280 engine->display.late_takedown = nv04_display_late_takedown;
281 engine->display.create = nv04_display_create;
282 engine->display.init = nv04_display_init;
283 engine->display.destroy = nv04_display_destroy;
284 engine->gpio.init = nouveau_stub_init;
285 engine->gpio.takedown = nouveau_stub_takedown;
286 engine->gpio.get = nv10_gpio_get;
287 engine->gpio.set = nv10_gpio_set;
288 engine->gpio.irq_enable = NULL;
289 engine->pm.clock_get = nv04_pm_clock_get;
290 engine->pm.clock_pre = nv04_pm_clock_pre;
291 engine->pm.clock_set = nv04_pm_clock_set;
292 engine->pm.voltage_get = nouveau_voltage_gpio_get;
293 engine->pm.voltage_set = nouveau_voltage_gpio_set;
294 engine->pm.temp_get = nv40_temp_get;
295 engine->vram.init = nouveau_mem_detect;
296 engine->vram.takedown = nouveau_stub_takedown;
297 engine->vram.flags_valid = nouveau_mem_flags_valid;
300 case 0x80: /* gotta love NVIDIA's consistency.. */
303 engine->instmem.init = nv50_instmem_init;
304 engine->instmem.takedown = nv50_instmem_takedown;
305 engine->instmem.suspend = nv50_instmem_suspend;
306 engine->instmem.resume = nv50_instmem_resume;
307 engine->instmem.get = nv50_instmem_get;
308 engine->instmem.put = nv50_instmem_put;
309 engine->instmem.map = nv50_instmem_map;
310 engine->instmem.unmap = nv50_instmem_unmap;
311 if (dev_priv->chipset == 0x50)
312 engine->instmem.flush = nv50_instmem_flush;
314 engine->instmem.flush = nv84_instmem_flush;
315 engine->mc.init = nv50_mc_init;
316 engine->mc.takedown = nv50_mc_takedown;
317 engine->timer.init = nv04_timer_init;
318 engine->timer.read = nv04_timer_read;
319 engine->timer.takedown = nv04_timer_takedown;
320 engine->fb.init = nv50_fb_init;
321 engine->fb.takedown = nv50_fb_takedown;
322 engine->fifo.channels = 128;
323 engine->fifo.init = nv50_fifo_init;
324 engine->fifo.takedown = nv50_fifo_takedown;
325 engine->fifo.disable = nv04_fifo_disable;
326 engine->fifo.enable = nv04_fifo_enable;
327 engine->fifo.reassign = nv04_fifo_reassign;
328 engine->fifo.channel_id = nv50_fifo_channel_id;
329 engine->fifo.create_context = nv50_fifo_create_context;
330 engine->fifo.destroy_context = nv50_fifo_destroy_context;
331 engine->fifo.load_context = nv50_fifo_load_context;
332 engine->fifo.unload_context = nv50_fifo_unload_context;
333 engine->fifo.tlb_flush = nv50_fifo_tlb_flush;
334 engine->display.early_init = nv50_display_early_init;
335 engine->display.late_takedown = nv50_display_late_takedown;
336 engine->display.create = nv50_display_create;
337 engine->display.init = nv50_display_init;
338 engine->display.destroy = nv50_display_destroy;
339 engine->gpio.init = nv50_gpio_init;
340 engine->gpio.takedown = nv50_gpio_fini;
341 engine->gpio.get = nv50_gpio_get;
342 engine->gpio.set = nv50_gpio_set;
343 engine->gpio.irq_register = nv50_gpio_irq_register;
344 engine->gpio.irq_unregister = nv50_gpio_irq_unregister;
345 engine->gpio.irq_enable = nv50_gpio_irq_enable;
346 switch (dev_priv->chipset) {
357 engine->pm.clock_get = nv50_pm_clock_get;
358 engine->pm.clock_pre = nv50_pm_clock_pre;
359 engine->pm.clock_set = nv50_pm_clock_set;
362 engine->pm.clocks_get = nva3_pm_clocks_get;
363 engine->pm.clocks_pre = nva3_pm_clocks_pre;
364 engine->pm.clocks_set = nva3_pm_clocks_set;
367 engine->pm.voltage_get = nouveau_voltage_gpio_get;
368 engine->pm.voltage_set = nouveau_voltage_gpio_set;
369 if (dev_priv->chipset >= 0x84)
370 engine->pm.temp_get = nv84_temp_get;
372 engine->pm.temp_get = nv40_temp_get;
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;
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.init = nv50_display_init;
411 engine->display.destroy = nv50_display_destroy;
412 engine->gpio.init = nv50_gpio_init;
413 engine->gpio.takedown = nouveau_stub_takedown;
414 engine->gpio.get = nv50_gpio_get;
415 engine->gpio.set = nv50_gpio_set;
416 engine->gpio.irq_register = nv50_gpio_irq_register;
417 engine->gpio.irq_unregister = nv50_gpio_irq_unregister;
418 engine->gpio.irq_enable = nv50_gpio_irq_enable;
419 engine->vram.init = nvc0_vram_init;
420 engine->vram.takedown = nv50_vram_fini;
421 engine->vram.get = nvc0_vram_new;
422 engine->vram.put = nv50_vram_del;
423 engine->vram.flags_valid = nvc0_vram_flags_valid;
424 engine->pm.temp_get = nv84_temp_get;
425 engine->pm.clocks_get = nvc0_pm_clocks_get;
426 engine->pm.voltage_get = nouveau_voltage_gpio_get;
427 engine->pm.voltage_set = nouveau_voltage_gpio_set;
430 NV_ERROR(dev, "NV%02x unsupported\n", dev_priv->chipset);
438 nouveau_vga_set_decode(void *priv, bool state)
440 struct drm_device *dev = priv;
441 struct drm_nouveau_private *dev_priv = dev->dev_private;
443 if (dev_priv->chipset >= 0x40)
444 nv_wr32(dev, 0x88054, state);
446 nv_wr32(dev, 0x1854, state);
449 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
450 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
452 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
456 nouveau_card_init_channel(struct drm_device *dev)
458 struct drm_nouveau_private *dev_priv = dev->dev_private;
461 ret = nouveau_channel_alloc(dev, &dev_priv->channel, NULL,
466 mutex_unlock(&dev_priv->channel->mutex);
470 static void nouveau_switcheroo_set_state(struct pci_dev *pdev,
471 enum vga_switcheroo_state state)
473 struct drm_device *dev = pci_get_drvdata(pdev);
474 pm_message_t pmm = { .event = PM_EVENT_SUSPEND };
475 if (state == VGA_SWITCHEROO_ON) {
476 printk(KERN_ERR "VGA switcheroo: switched nouveau on\n");
477 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
478 nouveau_pci_resume(pdev);
479 drm_kms_helper_poll_enable(dev);
480 dev->switch_power_state = DRM_SWITCH_POWER_ON;
482 printk(KERN_ERR "VGA switcheroo: switched nouveau off\n");
483 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
484 drm_kms_helper_poll_disable(dev);
485 nouveau_pci_suspend(pdev, pmm);
486 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
490 static void nouveau_switcheroo_reprobe(struct pci_dev *pdev)
492 struct drm_device *dev = pci_get_drvdata(pdev);
493 nouveau_fbcon_output_poll_changed(dev);
496 static bool nouveau_switcheroo_can_switch(struct pci_dev *pdev)
498 struct drm_device *dev = pci_get_drvdata(pdev);
501 spin_lock(&dev->count_lock);
502 can_switch = (dev->open_count == 0);
503 spin_unlock(&dev->count_lock);
508 nouveau_card_init(struct drm_device *dev)
510 struct drm_nouveau_private *dev_priv = dev->dev_private;
511 struct nouveau_engine *engine;
514 vga_client_register(dev->pdev, dev, NULL, nouveau_vga_set_decode);
515 vga_switcheroo_register_client(dev->pdev, nouveau_switcheroo_set_state,
516 nouveau_switcheroo_reprobe,
517 nouveau_switcheroo_can_switch);
519 /* Initialise internal driver API hooks */
520 ret = nouveau_init_engine_ptrs(dev);
523 engine = &dev_priv->engine;
524 spin_lock_init(&dev_priv->channels.lock);
525 spin_lock_init(&dev_priv->tile.lock);
526 spin_lock_init(&dev_priv->context_switch_lock);
527 spin_lock_init(&dev_priv->vm_lock);
529 /* Make the CRTCs and I2C buses accessible */
530 ret = engine->display.early_init(dev);
534 /* Parse BIOS tables / Run init tables if card not POSTed */
535 ret = nouveau_bios_init(dev);
537 goto out_display_early;
539 nouveau_pm_init(dev);
541 ret = engine->vram.init(dev);
545 ret = nouveau_gpuobj_init(dev);
549 ret = engine->instmem.init(dev);
553 ret = nouveau_mem_vram_init(dev);
557 ret = nouveau_mem_gart_init(dev);
562 ret = engine->mc.init(dev);
567 ret = engine->gpio.init(dev);
572 ret = engine->timer.init(dev);
577 ret = engine->fb.init(dev);
581 if (!dev_priv->noaccel) {
582 switch (dev_priv->card_type) {
584 nv04_graph_create(dev);
587 nv10_graph_create(dev);
591 nv20_graph_create(dev);
594 nv40_graph_create(dev);
597 nv50_graph_create(dev);
600 nvc0_graph_create(dev);
606 switch (dev_priv->chipset) {
613 nv84_crypt_create(dev);
617 switch (dev_priv->card_type) {
619 switch (dev_priv->chipset) {
624 nva3_copy_create(dev);
629 nvc0_copy_create(dev, 0);
630 nvc0_copy_create(dev, 1);
636 if (dev_priv->card_type == NV_40)
637 nv40_mpeg_create(dev);
639 if (dev_priv->card_type == NV_50 &&
640 (dev_priv->chipset < 0x98 || dev_priv->chipset == 0xa0))
641 nv50_mpeg_create(dev);
643 for (e = 0; e < NVOBJ_ENGINE_NR; e++) {
644 if (dev_priv->eng[e]) {
645 ret = dev_priv->eng[e]->init(dev, e);
652 ret = engine->fifo.init(dev);
657 ret = engine->display.create(dev);
661 ret = drm_vblank_init(dev, nv_two_heads(dev) ? 2 : 1);
665 ret = nouveau_irq_init(dev);
669 /* what about PVIDEO/PCRTC/PRAMDAC etc? */
671 if (dev_priv->eng[NVOBJ_ENGINE_GR]) {
672 ret = nouveau_fence_init(dev);
676 ret = nouveau_card_init_channel(dev);
681 nouveau_fbcon_init(dev);
682 drm_kms_helper_poll_init(dev);
686 nouveau_fence_fini(dev);
688 nouveau_irq_fini(dev);
690 drm_vblank_cleanup(dev);
691 engine->display.destroy(dev);
693 if (!dev_priv->noaccel)
694 engine->fifo.takedown(dev);
696 if (!dev_priv->noaccel) {
697 for (e = e - 1; e >= 0; e--) {
698 if (!dev_priv->eng[e])
700 dev_priv->eng[e]->fini(dev, e, false);
701 dev_priv->eng[e]->destroy(dev,e );
705 engine->fb.takedown(dev);
707 engine->timer.takedown(dev);
709 engine->gpio.takedown(dev);
711 engine->mc.takedown(dev);
713 nouveau_mem_gart_fini(dev);
715 nouveau_mem_vram_fini(dev);
717 engine->instmem.takedown(dev);
719 nouveau_gpuobj_takedown(dev);
721 engine->vram.takedown(dev);
723 nouveau_pm_fini(dev);
724 nouveau_bios_takedown(dev);
726 engine->display.late_takedown(dev);
728 vga_client_register(dev->pdev, NULL, NULL, NULL);
732 static void nouveau_card_takedown(struct drm_device *dev)
734 struct drm_nouveau_private *dev_priv = dev->dev_private;
735 struct nouveau_engine *engine = &dev_priv->engine;
738 drm_kms_helper_poll_fini(dev);
739 nouveau_fbcon_fini(dev);
741 if (dev_priv->channel) {
742 nouveau_channel_put_unlocked(&dev_priv->channel);
743 nouveau_fence_fini(dev);
746 engine->display.destroy(dev);
748 if (!dev_priv->noaccel) {
749 engine->fifo.takedown(dev);
750 for (e = NVOBJ_ENGINE_NR - 1; e >= 0; e--) {
751 if (dev_priv->eng[e]) {
752 dev_priv->eng[e]->fini(dev, e, false);
753 dev_priv->eng[e]->destroy(dev,e );
757 engine->fb.takedown(dev);
758 engine->timer.takedown(dev);
759 engine->gpio.takedown(dev);
760 engine->mc.takedown(dev);
761 engine->display.late_takedown(dev);
763 if (dev_priv->vga_ram) {
764 nouveau_bo_unpin(dev_priv->vga_ram);
765 nouveau_bo_ref(NULL, &dev_priv->vga_ram);
768 mutex_lock(&dev->struct_mutex);
769 ttm_bo_clean_mm(&dev_priv->ttm.bdev, TTM_PL_VRAM);
770 ttm_bo_clean_mm(&dev_priv->ttm.bdev, TTM_PL_TT);
771 mutex_unlock(&dev->struct_mutex);
772 nouveau_mem_gart_fini(dev);
773 nouveau_mem_vram_fini(dev);
775 engine->instmem.takedown(dev);
776 nouveau_gpuobj_takedown(dev);
777 engine->vram.takedown(dev);
779 nouveau_irq_fini(dev);
780 drm_vblank_cleanup(dev);
782 nouveau_pm_fini(dev);
783 nouveau_bios_takedown(dev);
785 vga_client_register(dev->pdev, NULL, NULL, NULL);
789 nouveau_open(struct drm_device *dev, struct drm_file *file_priv)
791 struct drm_nouveau_private *dev_priv = dev->dev_private;
792 struct nouveau_fpriv *fpriv;
795 fpriv = kzalloc(sizeof(*fpriv), GFP_KERNEL);
796 if (unlikely(!fpriv))
799 spin_lock_init(&fpriv->lock);
800 INIT_LIST_HEAD(&fpriv->channels);
802 if (dev_priv->card_type == NV_50) {
803 ret = nouveau_vm_new(dev, 0, (1ULL << 40), 0x0020000000ULL,
810 if (dev_priv->card_type >= NV_C0) {
811 ret = nouveau_vm_new(dev, 0, (1ULL << 40), 0x0008000000ULL,
819 file_priv->driver_priv = fpriv;
823 /* here a client dies, release the stuff that was allocated for its
825 void nouveau_preclose(struct drm_device *dev, struct drm_file *file_priv)
827 nouveau_channel_cleanup(dev, file_priv);
831 nouveau_postclose(struct drm_device *dev, struct drm_file *file_priv)
833 struct nouveau_fpriv *fpriv = nouveau_fpriv(file_priv);
834 nouveau_vm_ref(NULL, &fpriv->vm, NULL);
838 /* first module load, setup the mmio/fb mapping */
839 /* KMS: we need mmio at load time, not when the first drm client opens. */
840 int nouveau_firstopen(struct drm_device *dev)
845 /* if we have an OF card, copy vbios to RAMIN */
846 static void nouveau_OF_copy_vbios_to_ramin(struct drm_device *dev)
848 #if defined(__powerpc__)
850 const uint32_t *bios;
851 struct device_node *dn = pci_device_to_OF_node(dev->pdev);
853 NV_INFO(dev, "Unable to get the OF node\n");
857 bios = of_get_property(dn, "NVDA,BMP", &size);
859 for (i = 0; i < size; i += 4)
860 nv_wi32(dev, i, bios[i/4]);
861 NV_INFO(dev, "OF bios successfully copied (%d bytes)\n", size);
863 NV_INFO(dev, "Unable to get the OF bios\n");
868 static struct apertures_struct *nouveau_get_apertures(struct drm_device *dev)
870 struct pci_dev *pdev = dev->pdev;
871 struct apertures_struct *aper = alloc_apertures(3);
875 aper->ranges[0].base = pci_resource_start(pdev, 1);
876 aper->ranges[0].size = pci_resource_len(pdev, 1);
879 if (pci_resource_len(pdev, 2)) {
880 aper->ranges[aper->count].base = pci_resource_start(pdev, 2);
881 aper->ranges[aper->count].size = pci_resource_len(pdev, 2);
885 if (pci_resource_len(pdev, 3)) {
886 aper->ranges[aper->count].base = pci_resource_start(pdev, 3);
887 aper->ranges[aper->count].size = pci_resource_len(pdev, 3);
894 static int nouveau_remove_conflicting_drivers(struct drm_device *dev)
896 struct drm_nouveau_private *dev_priv = dev->dev_private;
897 bool primary = false;
898 dev_priv->apertures = nouveau_get_apertures(dev);
899 if (!dev_priv->apertures)
903 primary = dev->pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW;
906 remove_conflicting_framebuffers(dev_priv->apertures, "nouveaufb", primary);
910 int nouveau_load(struct drm_device *dev, unsigned long flags)
912 struct drm_nouveau_private *dev_priv;
914 resource_size_t mmio_start_offs;
917 dev_priv = kzalloc(sizeof(*dev_priv), GFP_KERNEL);
922 dev->dev_private = dev_priv;
925 dev_priv->flags = flags & NOUVEAU_FLAGS;
927 NV_DEBUG(dev, "vendor: 0x%X device: 0x%X class: 0x%X\n",
928 dev->pci_vendor, dev->pci_device, dev->pdev->class);
930 /* resource 0 is mmio regs */
931 /* resource 1 is linear FB */
932 /* resource 2 is RAMIN (mmio regs + 0x1000000) */
933 /* resource 6 is bios */
935 /* map the mmio regs */
936 mmio_start_offs = pci_resource_start(dev->pdev, 0);
937 dev_priv->mmio = ioremap(mmio_start_offs, 0x00800000);
938 if (!dev_priv->mmio) {
939 NV_ERROR(dev, "Unable to initialize the mmio mapping. "
940 "Please report your setup to " DRIVER_EMAIL "\n");
944 NV_DEBUG(dev, "regs mapped ok at 0x%llx\n",
945 (unsigned long long)mmio_start_offs);
948 /* Put the card in BE mode if it's not */
949 if (nv_rd32(dev, NV03_PMC_BOOT_1) != 0x01000001)
950 nv_wr32(dev, NV03_PMC_BOOT_1, 0x01000001);
955 /* Time to determine the card architecture */
956 reg0 = nv_rd32(dev, NV03_PMC_BOOT_0);
957 dev_priv->stepping = 0; /* XXX: add stepping for pre-NV10? */
959 /* We're dealing with >=NV10 */
960 if ((reg0 & 0x0f000000) > 0) {
961 /* Bit 27-20 contain the architecture in hex */
962 dev_priv->chipset = (reg0 & 0xff00000) >> 20;
963 dev_priv->stepping = (reg0 & 0xff);
965 } else if ((reg0 & 0xff00fff0) == 0x20004000) {
966 if (reg0 & 0x00f00000)
967 dev_priv->chipset = 0x05;
969 dev_priv->chipset = 0x04;
971 dev_priv->chipset = 0xff;
973 switch (dev_priv->chipset & 0xf0) {
978 dev_priv->card_type = dev_priv->chipset & 0xf0;
982 dev_priv->card_type = NV_40;
988 dev_priv->card_type = NV_50;
991 dev_priv->card_type = NV_C0;
994 NV_INFO(dev, "Unsupported chipset 0x%08x\n", reg0);
999 NV_INFO(dev, "Detected an NV%2x generation card (0x%08x)\n",
1000 dev_priv->card_type, reg0);
1002 /* Determine whether we'll attempt acceleration or not, some
1003 * cards are disabled by default here due to them being known
1004 * non-functional, or never been tested due to lack of hw.
1006 dev_priv->noaccel = !!nouveau_noaccel;
1007 if (nouveau_noaccel == -1) {
1008 switch (dev_priv->chipset) {
1009 case 0xc1: /* known broken */
1010 case 0xc8: /* never tested */
1011 NV_INFO(dev, "acceleration disabled by default, pass "
1012 "noaccel=0 to force enable\n");
1013 dev_priv->noaccel = true;
1016 dev_priv->noaccel = false;
1021 ret = nouveau_remove_conflicting_drivers(dev);
1025 /* Map PRAMIN BAR, or on older cards, the aperture within BAR0 */
1026 if (dev_priv->card_type >= NV_40) {
1028 if (pci_resource_len(dev->pdev, ramin_bar) == 0)
1031 dev_priv->ramin_size = pci_resource_len(dev->pdev, ramin_bar);
1033 ioremap(pci_resource_start(dev->pdev, ramin_bar),
1034 dev_priv->ramin_size);
1035 if (!dev_priv->ramin) {
1036 NV_ERROR(dev, "Failed to PRAMIN BAR");
1041 dev_priv->ramin_size = 1 * 1024 * 1024;
1042 dev_priv->ramin = ioremap(mmio_start_offs + NV_RAMIN,
1043 dev_priv->ramin_size);
1044 if (!dev_priv->ramin) {
1045 NV_ERROR(dev, "Failed to map BAR0 PRAMIN.\n");
1051 nouveau_OF_copy_vbios_to_ramin(dev);
1054 if (dev->pci_device == 0x01a0)
1055 dev_priv->flags |= NV_NFORCE;
1056 else if (dev->pci_device == 0x01f0)
1057 dev_priv->flags |= NV_NFORCE2;
1059 /* For kernel modesetting, init card now and bring up fbcon */
1060 ret = nouveau_card_init(dev);
1067 iounmap(dev_priv->ramin);
1069 iounmap(dev_priv->mmio);
1072 dev->dev_private = NULL;
1077 void nouveau_lastclose(struct drm_device *dev)
1079 vga_switcheroo_process_delayed_switch();
1082 int nouveau_unload(struct drm_device *dev)
1084 struct drm_nouveau_private *dev_priv = dev->dev_private;
1086 nouveau_card_takedown(dev);
1088 iounmap(dev_priv->mmio);
1089 iounmap(dev_priv->ramin);
1092 dev->dev_private = NULL;
1096 int nouveau_ioctl_getparam(struct drm_device *dev, void *data,
1097 struct drm_file *file_priv)
1099 struct drm_nouveau_private *dev_priv = dev->dev_private;
1100 struct drm_nouveau_getparam *getparam = data;
1102 switch (getparam->param) {
1103 case NOUVEAU_GETPARAM_CHIPSET_ID:
1104 getparam->value = dev_priv->chipset;
1106 case NOUVEAU_GETPARAM_PCI_VENDOR:
1107 getparam->value = dev->pci_vendor;
1109 case NOUVEAU_GETPARAM_PCI_DEVICE:
1110 getparam->value = dev->pci_device;
1112 case NOUVEAU_GETPARAM_BUS_TYPE:
1113 if (drm_pci_device_is_agp(dev))
1114 getparam->value = NV_AGP;
1115 else if (pci_is_pcie(dev->pdev))
1116 getparam->value = NV_PCIE;
1118 getparam->value = NV_PCI;
1120 case NOUVEAU_GETPARAM_FB_SIZE:
1121 getparam->value = dev_priv->fb_available_size;
1123 case NOUVEAU_GETPARAM_AGP_SIZE:
1124 getparam->value = dev_priv->gart_info.aper_size;
1126 case NOUVEAU_GETPARAM_VM_VRAM_BASE:
1127 getparam->value = 0; /* deprecated */
1129 case NOUVEAU_GETPARAM_PTIMER_TIME:
1130 getparam->value = dev_priv->engine.timer.read(dev);
1132 case NOUVEAU_GETPARAM_HAS_BO_USAGE:
1133 getparam->value = 1;
1135 case NOUVEAU_GETPARAM_HAS_PAGEFLIP:
1136 getparam->value = 1;
1138 case NOUVEAU_GETPARAM_GRAPH_UNITS:
1139 /* NV40 and NV50 versions are quite different, but register
1140 * address is the same. User is supposed to know the card
1141 * family anyway... */
1142 if (dev_priv->chipset >= 0x40) {
1143 getparam->value = nv_rd32(dev, NV40_PMC_GRAPH_UNITS);
1148 NV_DEBUG(dev, "unknown parameter %lld\n", getparam->param);
1156 nouveau_ioctl_setparam(struct drm_device *dev, void *data,
1157 struct drm_file *file_priv)
1159 struct drm_nouveau_setparam *setparam = data;
1161 switch (setparam->param) {
1163 NV_DEBUG(dev, "unknown parameter %lld\n", setparam->param);
1170 /* Wait until (value(reg) & mask) == val, up until timeout has hit */
1172 nouveau_wait_eq(struct drm_device *dev, uint64_t timeout,
1173 uint32_t reg, uint32_t mask, uint32_t val)
1175 struct drm_nouveau_private *dev_priv = dev->dev_private;
1176 struct nouveau_timer_engine *ptimer = &dev_priv->engine.timer;
1177 uint64_t start = ptimer->read(dev);
1180 if ((nv_rd32(dev, reg) & mask) == val)
1182 } while (ptimer->read(dev) - start < timeout);
1187 /* Wait until (value(reg) & mask) != val, up until timeout has hit */
1189 nouveau_wait_ne(struct drm_device *dev, uint64_t timeout,
1190 uint32_t reg, uint32_t mask, uint32_t val)
1192 struct drm_nouveau_private *dev_priv = dev->dev_private;
1193 struct nouveau_timer_engine *ptimer = &dev_priv->engine.timer;
1194 uint64_t start = ptimer->read(dev);
1197 if ((nv_rd32(dev, reg) & mask) != val)
1199 } while (ptimer->read(dev) - start < timeout);
1204 /* Wait until cond(data) == true, up until timeout has hit */
1206 nouveau_wait_cb(struct drm_device *dev, u64 timeout,
1207 bool (*cond)(void *), void *data)
1209 struct drm_nouveau_private *dev_priv = dev->dev_private;
1210 struct nouveau_timer_engine *ptimer = &dev_priv->engine.timer;
1211 u64 start = ptimer->read(dev);
1214 if (cond(data) == true)
1216 } while (ptimer->read(dev) - start < timeout);
1221 /* Waits for PGRAPH to go completely idle */
1222 bool nouveau_wait_for_idle(struct drm_device *dev)
1224 struct drm_nouveau_private *dev_priv = dev->dev_private;
1227 if (dev_priv->card_type == NV_40)
1228 mask &= ~NV40_PGRAPH_STATUS_SYNC_STALL;
1230 if (!nv_wait(dev, NV04_PGRAPH_STATUS, mask, 0)) {
1231 NV_ERROR(dev, "PGRAPH idle timed out with status 0x%08x\n",
1232 nv_rd32(dev, NV04_PGRAPH_STATUS));