blob: a36386c00d9b57c6b33351aa27194ee79d6df258 [file] [log] [blame]
Ben Skeggs6ee73862009-12-11 19:24:15 +10001/*
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Ben Skeggs6ee73862009-12-11 19:24:15 +100028#include "drmP.h"
29#include "drm.h"
30#include "drm_sarea.h"
31#include "drm_crtc_helper.h"
32#include <linux/vgaarb.h>
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100033#include <linux/vga_switcheroo.h>
Ben Skeggs6ee73862009-12-11 19:24:15 +100034
35#include "nouveau_drv.h"
36#include "nouveau_drm.h"
Dave Airlie38651672010-03-30 05:34:13 +000037#include "nouveau_fbcon.h"
Ben Skeggsa8eaebc2010-09-01 15:24:31 +100038#include "nouveau_ramht.h"
Ben Skeggsa0b25632011-11-21 16:41:48 +100039#include "nouveau_gpio.h"
Ben Skeggs330c5982010-09-16 15:39:49 +100040#include "nouveau_pm.h"
Ben Skeggs6ee73862009-12-11 19:24:15 +100041#include "nv50_display.h"
42
Ben Skeggs6ee73862009-12-11 19:24:15 +100043static void nouveau_stub_takedown(struct drm_device *dev) {}
Ben Skeggsee2e0132010-07-26 09:28:25 +100044static int nouveau_stub_init(struct drm_device *dev) { return 0; }
Ben Skeggs6ee73862009-12-11 19:24:15 +100045
46static 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
51 switch (dev_priv->chipset & 0xf0) {
52 case 0x00:
53 engine->instmem.init = nv04_instmem_init;
54 engine->instmem.takedown = nv04_instmem_takedown;
55 engine->instmem.suspend = nv04_instmem_suspend;
56 engine->instmem.resume = nv04_instmem_resume;
Ben Skeggse41115d2010-11-01 11:45:02 +100057 engine->instmem.get = nv04_instmem_get;
58 engine->instmem.put = nv04_instmem_put;
59 engine->instmem.map = nv04_instmem_map;
60 engine->instmem.unmap = nv04_instmem_unmap;
Ben Skeggsf56cb862010-07-08 11:29:10 +100061 engine->instmem.flush = nv04_instmem_flush;
Ben Skeggs6ee73862009-12-11 19:24:15 +100062 engine->mc.init = nv04_mc_init;
63 engine->mc.takedown = nv04_mc_takedown;
64 engine->timer.init = nv04_timer_init;
65 engine->timer.read = nv04_timer_read;
66 engine->timer.takedown = nv04_timer_takedown;
67 engine->fb.init = nv04_fb_init;
68 engine->fb.takedown = nv04_fb_takedown;
Ben Skeggs6ee73862009-12-11 19:24:15 +100069 engine->fifo.channels = 16;
70 engine->fifo.init = nv04_fifo_init;
Ben Skeggs5178d402010-11-03 10:56:05 +100071 engine->fifo.takedown = nv04_fifo_fini;
Ben Skeggs6ee73862009-12-11 19:24:15 +100072 engine->fifo.disable = nv04_fifo_disable;
73 engine->fifo.enable = nv04_fifo_enable;
74 engine->fifo.reassign = nv04_fifo_reassign;
Francisco Jerez588d7d12009-12-13 20:07:42 +010075 engine->fifo.cache_pull = nv04_fifo_cache_pull;
Ben Skeggs6ee73862009-12-11 19:24:15 +100076 engine->fifo.channel_id = nv04_fifo_channel_id;
77 engine->fifo.create_context = nv04_fifo_create_context;
78 engine->fifo.destroy_context = nv04_fifo_destroy_context;
79 engine->fifo.load_context = nv04_fifo_load_context;
80 engine->fifo.unload_context = nv04_fifo_unload_context;
Francisco Jerezc88c2e02010-07-24 17:37:33 +020081 engine->display.early_init = nv04_display_early_init;
82 engine->display.late_takedown = nv04_display_late_takedown;
83 engine->display.create = nv04_display_create;
Francisco Jerezc88c2e02010-07-24 17:37:33 +020084 engine->display.destroy = nv04_display_destroy;
Ben Skeggs2a44e492011-11-09 11:36:33 +100085 engine->display.init = nv04_display_init;
86 engine->display.fini = nv04_display_fini;
Ben Skeggs36f13172011-10-27 10:24:12 +100087 engine->pm.clocks_get = nv04_pm_clocks_get;
88 engine->pm.clocks_pre = nv04_pm_clocks_pre;
89 engine->pm.clocks_set = nv04_pm_clocks_set;
Ben Skeggs7ad2d312011-12-11 00:30:05 +100090 engine->vram.init = nv04_fb_vram_init;
Ben Skeggs24f246a2011-06-10 13:36:08 +100091 engine->vram.takedown = nouveau_stub_takedown;
Ben Skeggs60d2a882010-12-06 15:28:54 +100092 engine->vram.flags_valid = nouveau_mem_flags_valid;
Ben Skeggs6ee73862009-12-11 19:24:15 +100093 break;
94 case 0x10:
95 engine->instmem.init = nv04_instmem_init;
96 engine->instmem.takedown = nv04_instmem_takedown;
97 engine->instmem.suspend = nv04_instmem_suspend;
98 engine->instmem.resume = nv04_instmem_resume;
Ben Skeggse41115d2010-11-01 11:45:02 +100099 engine->instmem.get = nv04_instmem_get;
100 engine->instmem.put = nv04_instmem_put;
101 engine->instmem.map = nv04_instmem_map;
102 engine->instmem.unmap = nv04_instmem_unmap;
Ben Skeggsf56cb862010-07-08 11:29:10 +1000103 engine->instmem.flush = nv04_instmem_flush;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000104 engine->mc.init = nv04_mc_init;
105 engine->mc.takedown = nv04_mc_takedown;
106 engine->timer.init = nv04_timer_init;
107 engine->timer.read = nv04_timer_read;
108 engine->timer.takedown = nv04_timer_takedown;
109 engine->fb.init = nv10_fb_init;
110 engine->fb.takedown = nv10_fb_takedown;
Francisco Jereza5cf68b2010-10-24 16:14:41 +0200111 engine->fb.init_tile_region = nv10_fb_init_tile_region;
112 engine->fb.set_tile_region = nv10_fb_set_tile_region;
113 engine->fb.free_tile_region = nv10_fb_free_tile_region;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000114 engine->fifo.channels = 32;
115 engine->fifo.init = nv10_fifo_init;
Ben Skeggs5178d402010-11-03 10:56:05 +1000116 engine->fifo.takedown = nv04_fifo_fini;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000117 engine->fifo.disable = nv04_fifo_disable;
118 engine->fifo.enable = nv04_fifo_enable;
119 engine->fifo.reassign = nv04_fifo_reassign;
Francisco Jerez588d7d12009-12-13 20:07:42 +0100120 engine->fifo.cache_pull = nv04_fifo_cache_pull;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000121 engine->fifo.channel_id = nv10_fifo_channel_id;
122 engine->fifo.create_context = nv10_fifo_create_context;
Francisco Jerez3945e472010-10-18 03:53:39 +0200123 engine->fifo.destroy_context = nv04_fifo_destroy_context;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000124 engine->fifo.load_context = nv10_fifo_load_context;
125 engine->fifo.unload_context = nv10_fifo_unload_context;
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200126 engine->display.early_init = nv04_display_early_init;
127 engine->display.late_takedown = nv04_display_late_takedown;
128 engine->display.create = nv04_display_create;
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200129 engine->display.destroy = nv04_display_destroy;
Ben Skeggs2a44e492011-11-09 11:36:33 +1000130 engine->display.init = nv04_display_init;
131 engine->display.fini = nv04_display_fini;
Ben Skeggsa0b25632011-11-21 16:41:48 +1000132 engine->gpio.drive = nv10_gpio_drive;
133 engine->gpio.sense = nv10_gpio_sense;
Ben Skeggs36f13172011-10-27 10:24:12 +1000134 engine->pm.clocks_get = nv04_pm_clocks_get;
135 engine->pm.clocks_pre = nv04_pm_clocks_pre;
136 engine->pm.clocks_set = nv04_pm_clocks_set;
Ben Skeggs7ad2d312011-12-11 00:30:05 +1000137 if (dev_priv->chipset == 0x1a ||
138 dev_priv->chipset == 0x1f)
139 engine->vram.init = nv1a_fb_vram_init;
140 else
141 engine->vram.init = nv10_fb_vram_init;
Ben Skeggs24f246a2011-06-10 13:36:08 +1000142 engine->vram.takedown = nouveau_stub_takedown;
Ben Skeggs60d2a882010-12-06 15:28:54 +1000143 engine->vram.flags_valid = nouveau_mem_flags_valid;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000144 break;
145 case 0x20:
146 engine->instmem.init = nv04_instmem_init;
147 engine->instmem.takedown = nv04_instmem_takedown;
148 engine->instmem.suspend = nv04_instmem_suspend;
149 engine->instmem.resume = nv04_instmem_resume;
Ben Skeggse41115d2010-11-01 11:45:02 +1000150 engine->instmem.get = nv04_instmem_get;
151 engine->instmem.put = nv04_instmem_put;
152 engine->instmem.map = nv04_instmem_map;
153 engine->instmem.unmap = nv04_instmem_unmap;
Ben Skeggsf56cb862010-07-08 11:29:10 +1000154 engine->instmem.flush = nv04_instmem_flush;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000155 engine->mc.init = nv04_mc_init;
156 engine->mc.takedown = nv04_mc_takedown;
157 engine->timer.init = nv04_timer_init;
158 engine->timer.read = nv04_timer_read;
159 engine->timer.takedown = nv04_timer_takedown;
Ben Skeggsd81c19e2011-12-12 22:51:33 +1000160 engine->fb.init = nv20_fb_init;
161 engine->fb.takedown = nv20_fb_takedown;
162 engine->fb.init_tile_region = nv20_fb_init_tile_region;
163 engine->fb.set_tile_region = nv20_fb_set_tile_region;
164 engine->fb.free_tile_region = nv20_fb_free_tile_region;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000165 engine->fifo.channels = 32;
166 engine->fifo.init = nv10_fifo_init;
Ben Skeggs5178d402010-11-03 10:56:05 +1000167 engine->fifo.takedown = nv04_fifo_fini;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000168 engine->fifo.disable = nv04_fifo_disable;
169 engine->fifo.enable = nv04_fifo_enable;
170 engine->fifo.reassign = nv04_fifo_reassign;
Francisco Jerez588d7d12009-12-13 20:07:42 +0100171 engine->fifo.cache_pull = nv04_fifo_cache_pull;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000172 engine->fifo.channel_id = nv10_fifo_channel_id;
173 engine->fifo.create_context = nv10_fifo_create_context;
Francisco Jerez3945e472010-10-18 03:53:39 +0200174 engine->fifo.destroy_context = nv04_fifo_destroy_context;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000175 engine->fifo.load_context = nv10_fifo_load_context;
176 engine->fifo.unload_context = nv10_fifo_unload_context;
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200177 engine->display.early_init = nv04_display_early_init;
178 engine->display.late_takedown = nv04_display_late_takedown;
179 engine->display.create = nv04_display_create;
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200180 engine->display.destroy = nv04_display_destroy;
Ben Skeggs2a44e492011-11-09 11:36:33 +1000181 engine->display.init = nv04_display_init;
182 engine->display.fini = nv04_display_fini;
Ben Skeggsa0b25632011-11-21 16:41:48 +1000183 engine->gpio.drive = nv10_gpio_drive;
184 engine->gpio.sense = nv10_gpio_sense;
Ben Skeggs36f13172011-10-27 10:24:12 +1000185 engine->pm.clocks_get = nv04_pm_clocks_get;
186 engine->pm.clocks_pre = nv04_pm_clocks_pre;
187 engine->pm.clocks_set = nv04_pm_clocks_set;
Ben Skeggsd81c19e2011-12-12 22:51:33 +1000188 engine->vram.init = nv20_fb_vram_init;
Ben Skeggs24f246a2011-06-10 13:36:08 +1000189 engine->vram.takedown = nouveau_stub_takedown;
Ben Skeggs60d2a882010-12-06 15:28:54 +1000190 engine->vram.flags_valid = nouveau_mem_flags_valid;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000191 break;
192 case 0x30:
193 engine->instmem.init = nv04_instmem_init;
194 engine->instmem.takedown = nv04_instmem_takedown;
195 engine->instmem.suspend = nv04_instmem_suspend;
196 engine->instmem.resume = nv04_instmem_resume;
Ben Skeggse41115d2010-11-01 11:45:02 +1000197 engine->instmem.get = nv04_instmem_get;
198 engine->instmem.put = nv04_instmem_put;
199 engine->instmem.map = nv04_instmem_map;
200 engine->instmem.unmap = nv04_instmem_unmap;
Ben Skeggsf56cb862010-07-08 11:29:10 +1000201 engine->instmem.flush = nv04_instmem_flush;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000202 engine->mc.init = nv04_mc_init;
203 engine->mc.takedown = nv04_mc_takedown;
204 engine->timer.init = nv04_timer_init;
205 engine->timer.read = nv04_timer_read;
206 engine->timer.takedown = nv04_timer_takedown;
Francisco Jerez8bded182010-07-21 21:08:11 +0200207 engine->fb.init = nv30_fb_init;
208 engine->fb.takedown = nv30_fb_takedown;
Francisco Jereza5cf68b2010-10-24 16:14:41 +0200209 engine->fb.init_tile_region = nv30_fb_init_tile_region;
210 engine->fb.set_tile_region = nv10_fb_set_tile_region;
211 engine->fb.free_tile_region = nv30_fb_free_tile_region;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000212 engine->fifo.channels = 32;
213 engine->fifo.init = nv10_fifo_init;
Ben Skeggs5178d402010-11-03 10:56:05 +1000214 engine->fifo.takedown = nv04_fifo_fini;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000215 engine->fifo.disable = nv04_fifo_disable;
216 engine->fifo.enable = nv04_fifo_enable;
217 engine->fifo.reassign = nv04_fifo_reassign;
Francisco Jerez588d7d12009-12-13 20:07:42 +0100218 engine->fifo.cache_pull = nv04_fifo_cache_pull;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000219 engine->fifo.channel_id = nv10_fifo_channel_id;
220 engine->fifo.create_context = nv10_fifo_create_context;
Francisco Jerez3945e472010-10-18 03:53:39 +0200221 engine->fifo.destroy_context = nv04_fifo_destroy_context;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000222 engine->fifo.load_context = nv10_fifo_load_context;
223 engine->fifo.unload_context = nv10_fifo_unload_context;
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200224 engine->display.early_init = nv04_display_early_init;
225 engine->display.late_takedown = nv04_display_late_takedown;
226 engine->display.create = nv04_display_create;
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200227 engine->display.destroy = nv04_display_destroy;
Ben Skeggs2a44e492011-11-09 11:36:33 +1000228 engine->display.init = nv04_display_init;
229 engine->display.fini = nv04_display_fini;
Ben Skeggsa0b25632011-11-21 16:41:48 +1000230 engine->gpio.drive = nv10_gpio_drive;
231 engine->gpio.sense = nv10_gpio_sense;
Ben Skeggs36f13172011-10-27 10:24:12 +1000232 engine->pm.clocks_get = nv04_pm_clocks_get;
233 engine->pm.clocks_pre = nv04_pm_clocks_pre;
234 engine->pm.clocks_set = nv04_pm_clocks_set;
Ben Skeggs442b6262010-09-16 16:25:26 +1000235 engine->pm.voltage_get = nouveau_voltage_gpio_get;
236 engine->pm.voltage_set = nouveau_voltage_gpio_set;
Ben Skeggsd81c19e2011-12-12 22:51:33 +1000237 engine->vram.init = nv20_fb_vram_init;
Ben Skeggs24f246a2011-06-10 13:36:08 +1000238 engine->vram.takedown = nouveau_stub_takedown;
Ben Skeggs60d2a882010-12-06 15:28:54 +1000239 engine->vram.flags_valid = nouveau_mem_flags_valid;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000240 break;
241 case 0x40:
242 case 0x60:
243 engine->instmem.init = nv04_instmem_init;
244 engine->instmem.takedown = nv04_instmem_takedown;
245 engine->instmem.suspend = nv04_instmem_suspend;
246 engine->instmem.resume = nv04_instmem_resume;
Ben Skeggse41115d2010-11-01 11:45:02 +1000247 engine->instmem.get = nv04_instmem_get;
248 engine->instmem.put = nv04_instmem_put;
249 engine->instmem.map = nv04_instmem_map;
250 engine->instmem.unmap = nv04_instmem_unmap;
Ben Skeggsf56cb862010-07-08 11:29:10 +1000251 engine->instmem.flush = nv04_instmem_flush;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000252 engine->mc.init = nv40_mc_init;
253 engine->mc.takedown = nv40_mc_takedown;
254 engine->timer.init = nv04_timer_init;
255 engine->timer.read = nv04_timer_read;
256 engine->timer.takedown = nv04_timer_takedown;
257 engine->fb.init = nv40_fb_init;
258 engine->fb.takedown = nv40_fb_takedown;
Francisco Jereza5cf68b2010-10-24 16:14:41 +0200259 engine->fb.init_tile_region = nv30_fb_init_tile_region;
260 engine->fb.set_tile_region = nv40_fb_set_tile_region;
261 engine->fb.free_tile_region = nv30_fb_free_tile_region;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000262 engine->fifo.channels = 32;
263 engine->fifo.init = nv40_fifo_init;
Ben Skeggs5178d402010-11-03 10:56:05 +1000264 engine->fifo.takedown = nv04_fifo_fini;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000265 engine->fifo.disable = nv04_fifo_disable;
266 engine->fifo.enable = nv04_fifo_enable;
267 engine->fifo.reassign = nv04_fifo_reassign;
Francisco Jerez588d7d12009-12-13 20:07:42 +0100268 engine->fifo.cache_pull = nv04_fifo_cache_pull;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000269 engine->fifo.channel_id = nv10_fifo_channel_id;
270 engine->fifo.create_context = nv40_fifo_create_context;
Francisco Jerez3945e472010-10-18 03:53:39 +0200271 engine->fifo.destroy_context = nv04_fifo_destroy_context;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000272 engine->fifo.load_context = nv40_fifo_load_context;
273 engine->fifo.unload_context = nv40_fifo_unload_context;
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200274 engine->display.early_init = nv04_display_early_init;
275 engine->display.late_takedown = nv04_display_late_takedown;
276 engine->display.create = nv04_display_create;
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200277 engine->display.destroy = nv04_display_destroy;
Ben Skeggs2a44e492011-11-09 11:36:33 +1000278 engine->display.init = nv04_display_init;
279 engine->display.fini = nv04_display_fini;
Ben Skeggs47e5d5c2011-11-22 13:49:22 +1000280 engine->gpio.init = nv10_gpio_init;
281 engine->gpio.fini = nv10_gpio_fini;
Ben Skeggsa0b25632011-11-21 16:41:48 +1000282 engine->gpio.drive = nv10_gpio_drive;
283 engine->gpio.sense = nv10_gpio_sense;
Ben Skeggs47e5d5c2011-11-22 13:49:22 +1000284 engine->gpio.irq_enable = nv10_gpio_irq_enable;
Ben Skeggs1262a202011-07-18 15:15:34 +1000285 engine->pm.clocks_get = nv40_pm_clocks_get;
286 engine->pm.clocks_pre = nv40_pm_clocks_pre;
287 engine->pm.clocks_set = nv40_pm_clocks_set;
Ben Skeggs442b6262010-09-16 16:25:26 +1000288 engine->pm.voltage_get = nouveau_voltage_gpio_get;
289 engine->pm.voltage_set = nouveau_voltage_gpio_set;
Francisco Jerez8155cac2010-09-23 20:58:38 +0200290 engine->pm.temp_get = nv40_temp_get;
Ben Skeggs69346182011-09-17 02:11:39 +1000291 engine->pm.pwm_get = nv40_pm_pwm_get;
292 engine->pm.pwm_set = nv40_pm_pwm_set;
Ben Skeggsff92a6c2011-12-12 23:03:14 +1000293 engine->vram.init = nv40_fb_vram_init;
Ben Skeggs24f246a2011-06-10 13:36:08 +1000294 engine->vram.takedown = nouveau_stub_takedown;
Ben Skeggs60d2a882010-12-06 15:28:54 +1000295 engine->vram.flags_valid = nouveau_mem_flags_valid;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000296 break;
297 case 0x50:
298 case 0x80: /* gotta love NVIDIA's consistency.. */
299 case 0x90:
Ben Skeggsd9f61c22011-07-04 13:25:17 +1000300 case 0xa0:
Ben Skeggs6ee73862009-12-11 19:24:15 +1000301 engine->instmem.init = nv50_instmem_init;
302 engine->instmem.takedown = nv50_instmem_takedown;
303 engine->instmem.suspend = nv50_instmem_suspend;
304 engine->instmem.resume = nv50_instmem_resume;
Ben Skeggse41115d2010-11-01 11:45:02 +1000305 engine->instmem.get = nv50_instmem_get;
306 engine->instmem.put = nv50_instmem_put;
307 engine->instmem.map = nv50_instmem_map;
308 engine->instmem.unmap = nv50_instmem_unmap;
Ben Skeggs734ee832010-07-15 11:02:54 +1000309 if (dev_priv->chipset == 0x50)
310 engine->instmem.flush = nv50_instmem_flush;
311 else
312 engine->instmem.flush = nv84_instmem_flush;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000313 engine->mc.init = nv50_mc_init;
314 engine->mc.takedown = nv50_mc_takedown;
315 engine->timer.init = nv04_timer_init;
316 engine->timer.read = nv04_timer_read;
317 engine->timer.takedown = nv04_timer_takedown;
Marcin Koƛcielnicki304424e2010-03-01 00:18:39 +0000318 engine->fb.init = nv50_fb_init;
319 engine->fb.takedown = nv50_fb_takedown;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000320 engine->fifo.channels = 128;
321 engine->fifo.init = nv50_fifo_init;
322 engine->fifo.takedown = nv50_fifo_takedown;
323 engine->fifo.disable = nv04_fifo_disable;
324 engine->fifo.enable = nv04_fifo_enable;
325 engine->fifo.reassign = nv04_fifo_reassign;
326 engine->fifo.channel_id = nv50_fifo_channel_id;
327 engine->fifo.create_context = nv50_fifo_create_context;
328 engine->fifo.destroy_context = nv50_fifo_destroy_context;
329 engine->fifo.load_context = nv50_fifo_load_context;
330 engine->fifo.unload_context = nv50_fifo_unload_context;
Ben Skeggs56ac7472010-10-22 10:26:24 +1000331 engine->fifo.tlb_flush = nv50_fifo_tlb_flush;
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200332 engine->display.early_init = nv50_display_early_init;
333 engine->display.late_takedown = nv50_display_late_takedown;
334 engine->display.create = nv50_display_create;
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200335 engine->display.destroy = nv50_display_destroy;
Ben Skeggs2a44e492011-11-09 11:36:33 +1000336 engine->display.init = nv50_display_init;
337 engine->display.fini = nv50_display_fini;
Ben Skeggsee2e0132010-07-26 09:28:25 +1000338 engine->gpio.init = nv50_gpio_init;
Ben Skeggsa0b25632011-11-21 16:41:48 +1000339 engine->gpio.fini = nv50_gpio_fini;
340 engine->gpio.drive = nv50_gpio_drive;
341 engine->gpio.sense = nv50_gpio_sense;
Ben Skeggsee2e0132010-07-26 09:28:25 +1000342 engine->gpio.irq_enable = nv50_gpio_irq_enable;
Ben Skeggsfade7ad2010-09-27 11:18:14 +1000343 switch (dev_priv->chipset) {
Ben Skeggsbd2e5972010-10-19 20:06:01 +1000344 case 0x84:
345 case 0x86:
346 case 0x92:
347 case 0x94:
348 case 0x96:
349 case 0x98:
350 case 0xa0:
Ben Skeggs5f801982010-10-22 08:44:09 +1000351 case 0xaa:
352 case 0xac:
Ben Skeggsbd2e5972010-10-19 20:06:01 +1000353 case 0x50:
Ben Skeggsf3fbaf32011-10-26 09:11:02 +1000354 engine->pm.clocks_get = nv50_pm_clocks_get;
355 engine->pm.clocks_pre = nv50_pm_clocks_pre;
356 engine->pm.clocks_set = nv50_pm_clocks_set;
Ben Skeggsfade7ad2010-09-27 11:18:14 +1000357 break;
Ben Skeggsbd2e5972010-10-19 20:06:01 +1000358 default:
Ben Skeggsca94a712011-06-17 15:38:48 +1000359 engine->pm.clocks_get = nva3_pm_clocks_get;
360 engine->pm.clocks_pre = nva3_pm_clocks_pre;
361 engine->pm.clocks_set = nva3_pm_clocks_set;
Ben Skeggsbd2e5972010-10-19 20:06:01 +1000362 break;
Ben Skeggsfade7ad2010-09-27 11:18:14 +1000363 }
Ben Skeggs02c30ca2010-09-16 16:17:35 +1000364 engine->pm.voltage_get = nouveau_voltage_gpio_get;
365 engine->pm.voltage_set = nouveau_voltage_gpio_set;
Francisco Jerez8155cac2010-09-23 20:58:38 +0200366 if (dev_priv->chipset >= 0x84)
367 engine->pm.temp_get = nv84_temp_get;
368 else
369 engine->pm.temp_get = nv40_temp_get;
Ben Skeggs5a4267a2011-09-17 02:01:24 +1000370 engine->pm.pwm_get = nv50_pm_pwm_get;
371 engine->pm.pwm_set = nv50_pm_pwm_set;
Ben Skeggs60d2a882010-12-06 15:28:54 +1000372 engine->vram.init = nv50_vram_init;
Ben Skeggs24f246a2011-06-10 13:36:08 +1000373 engine->vram.takedown = nv50_vram_fini;
Ben Skeggs60d2a882010-12-06 15:28:54 +1000374 engine->vram.get = nv50_vram_new;
375 engine->vram.put = nv50_vram_del;
376 engine->vram.flags_valid = nv50_vram_flags_valid;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000377 break;
Ben Skeggsd9f61c22011-07-04 13:25:17 +1000378 case 0xc0:
Ben Skeggs4b223ee2010-08-03 10:00:56 +1000379 engine->instmem.init = nvc0_instmem_init;
380 engine->instmem.takedown = nvc0_instmem_takedown;
381 engine->instmem.suspend = nvc0_instmem_suspend;
382 engine->instmem.resume = nvc0_instmem_resume;
Ben Skeggs8984e042010-11-15 11:48:33 +1000383 engine->instmem.get = nv50_instmem_get;
384 engine->instmem.put = nv50_instmem_put;
385 engine->instmem.map = nv50_instmem_map;
386 engine->instmem.unmap = nv50_instmem_unmap;
387 engine->instmem.flush = nv84_instmem_flush;
Ben Skeggs4b223ee2010-08-03 10:00:56 +1000388 engine->mc.init = nv50_mc_init;
389 engine->mc.takedown = nv50_mc_takedown;
390 engine->timer.init = nv04_timer_init;
391 engine->timer.read = nv04_timer_read;
392 engine->timer.takedown = nv04_timer_takedown;
393 engine->fb.init = nvc0_fb_init;
394 engine->fb.takedown = nvc0_fb_takedown;
Ben Skeggs4b223ee2010-08-03 10:00:56 +1000395 engine->fifo.channels = 128;
396 engine->fifo.init = nvc0_fifo_init;
397 engine->fifo.takedown = nvc0_fifo_takedown;
398 engine->fifo.disable = nvc0_fifo_disable;
399 engine->fifo.enable = nvc0_fifo_enable;
400 engine->fifo.reassign = nvc0_fifo_reassign;
401 engine->fifo.channel_id = nvc0_fifo_channel_id;
402 engine->fifo.create_context = nvc0_fifo_create_context;
403 engine->fifo.destroy_context = nvc0_fifo_destroy_context;
404 engine->fifo.load_context = nvc0_fifo_load_context;
405 engine->fifo.unload_context = nvc0_fifo_unload_context;
406 engine->display.early_init = nv50_display_early_init;
407 engine->display.late_takedown = nv50_display_late_takedown;
408 engine->display.create = nv50_display_create;
Ben Skeggs4b223ee2010-08-03 10:00:56 +1000409 engine->display.destroy = nv50_display_destroy;
Ben Skeggs2a44e492011-11-09 11:36:33 +1000410 engine->display.init = nv50_display_init;
411 engine->display.fini = nv50_display_fini;
Ben Skeggs4b223ee2010-08-03 10:00:56 +1000412 engine->gpio.init = nv50_gpio_init;
Ben Skeggsa0b25632011-11-21 16:41:48 +1000413 engine->gpio.fini = nv50_gpio_fini;
414 engine->gpio.drive = nv50_gpio_drive;
415 engine->gpio.sense = nv50_gpio_sense;
Ben Skeggs4b223ee2010-08-03 10:00:56 +1000416 engine->gpio.irq_enable = nv50_gpio_irq_enable;
Ben Skeggs8984e042010-11-15 11:48:33 +1000417 engine->vram.init = nvc0_vram_init;
Ben Skeggs24f246a2011-06-10 13:36:08 +1000418 engine->vram.takedown = nv50_vram_fini;
Ben Skeggs8984e042010-11-15 11:48:33 +1000419 engine->vram.get = nvc0_vram_new;
420 engine->vram.put = nv50_vram_del;
421 engine->vram.flags_valid = nvc0_vram_flags_valid;
Martin Peres74cfad12011-05-12 22:40:47 +0200422 engine->pm.temp_get = nv84_temp_get;
Ben Skeggs354d0782011-06-19 01:44:36 +1000423 engine->pm.clocks_get = nvc0_pm_clocks_get;
Ben Skeggs045da4e2011-10-29 00:22:49 +1000424 engine->pm.clocks_pre = nvc0_pm_clocks_pre;
425 engine->pm.clocks_set = nvc0_pm_clocks_set;
Ben Skeggs3c71c232011-06-09 17:34:02 +1000426 engine->pm.voltage_get = nouveau_voltage_gpio_get;
Ben Skeggsda1dc4c2011-06-10 12:07:09 +1000427 engine->pm.voltage_set = nouveau_voltage_gpio_set;
Ben Skeggs5a4267a2011-09-17 02:01:24 +1000428 engine->pm.pwm_get = nv50_pm_pwm_get;
429 engine->pm.pwm_set = nv50_pm_pwm_set;
Ben Skeggs4b223ee2010-08-03 10:00:56 +1000430 break;
Ben Skeggsd9f61c22011-07-04 13:25:17 +1000431 case 0xd0:
432 engine->instmem.init = nvc0_instmem_init;
433 engine->instmem.takedown = nvc0_instmem_takedown;
434 engine->instmem.suspend = nvc0_instmem_suspend;
435 engine->instmem.resume = nvc0_instmem_resume;
436 engine->instmem.get = nv50_instmem_get;
437 engine->instmem.put = nv50_instmem_put;
438 engine->instmem.map = nv50_instmem_map;
439 engine->instmem.unmap = nv50_instmem_unmap;
440 engine->instmem.flush = nv84_instmem_flush;
441 engine->mc.init = nv50_mc_init;
442 engine->mc.takedown = nv50_mc_takedown;
443 engine->timer.init = nv04_timer_init;
444 engine->timer.read = nv04_timer_read;
445 engine->timer.takedown = nv04_timer_takedown;
446 engine->fb.init = nvc0_fb_init;
447 engine->fb.takedown = nvc0_fb_takedown;
448 engine->fifo.channels = 128;
449 engine->fifo.init = nvc0_fifo_init;
450 engine->fifo.takedown = nvc0_fifo_takedown;
451 engine->fifo.disable = nvc0_fifo_disable;
452 engine->fifo.enable = nvc0_fifo_enable;
453 engine->fifo.reassign = nvc0_fifo_reassign;
454 engine->fifo.channel_id = nvc0_fifo_channel_id;
455 engine->fifo.create_context = nvc0_fifo_create_context;
456 engine->fifo.destroy_context = nvc0_fifo_destroy_context;
457 engine->fifo.load_context = nvc0_fifo_load_context;
458 engine->fifo.unload_context = nvc0_fifo_unload_context;
459 engine->display.early_init = nouveau_stub_init;
460 engine->display.late_takedown = nouveau_stub_takedown;
Ben Skeggs26f6d882011-07-04 16:25:18 +1000461 engine->display.create = nvd0_display_create;
Ben Skeggs26f6d882011-07-04 16:25:18 +1000462 engine->display.destroy = nvd0_display_destroy;
Ben Skeggs2a44e492011-11-09 11:36:33 +1000463 engine->display.init = nvd0_display_init;
464 engine->display.fini = nvd0_display_fini;
Ben Skeggsd7f81722011-07-03 02:57:35 +1000465 engine->gpio.init = nv50_gpio_init;
Ben Skeggsa0b25632011-11-21 16:41:48 +1000466 engine->gpio.fini = nv50_gpio_fini;
467 engine->gpio.drive = nvd0_gpio_drive;
468 engine->gpio.sense = nvd0_gpio_sense;
Ben Skeggsd7f81722011-07-03 02:57:35 +1000469 engine->gpio.irq_enable = nv50_gpio_irq_enable;
Ben Skeggsd9f61c22011-07-04 13:25:17 +1000470 engine->vram.init = nvc0_vram_init;
471 engine->vram.takedown = nv50_vram_fini;
472 engine->vram.get = nvc0_vram_new;
473 engine->vram.put = nv50_vram_del;
474 engine->vram.flags_valid = nvc0_vram_flags_valid;
Martin Peres61091832011-10-22 01:40:40 +0200475 engine->pm.temp_get = nv84_temp_get;
Ben Skeggs4784e4a2011-07-04 14:06:07 +1000476 engine->pm.clocks_get = nvc0_pm_clocks_get;
Ben Skeggs045da4e2011-10-29 00:22:49 +1000477 engine->pm.clocks_pre = nvc0_pm_clocks_pre;
478 engine->pm.clocks_set = nvc0_pm_clocks_set;
Ben Skeggs4784e4a2011-07-04 14:06:07 +1000479 engine->pm.voltage_get = nouveau_voltage_gpio_get;
480 engine->pm.voltage_set = nouveau_voltage_gpio_set;
Ben Skeggsd9f61c22011-07-04 13:25:17 +1000481 break;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000482 default:
483 NV_ERROR(dev, "NV%02x unsupported\n", dev_priv->chipset);
484 return 1;
485 }
486
Ben Skeggs03bc9672011-07-04 13:14:05 +1000487 /* headless mode */
488 if (nouveau_modeset == 2) {
489 engine->display.early_init = nouveau_stub_init;
490 engine->display.late_takedown = nouveau_stub_takedown;
491 engine->display.create = nouveau_stub_init;
492 engine->display.init = nouveau_stub_init;
493 engine->display.destroy = nouveau_stub_takedown;
494 }
495
Ben Skeggs6ee73862009-12-11 19:24:15 +1000496 return 0;
497}
498
499static unsigned int
500nouveau_vga_set_decode(void *priv, bool state)
501{
Marcin Koƛcielnicki9967b942010-02-08 00:20:17 +0000502 struct drm_device *dev = priv;
503 struct drm_nouveau_private *dev_priv = dev->dev_private;
504
505 if (dev_priv->chipset >= 0x40)
506 nv_wr32(dev, 0x88054, state);
507 else
508 nv_wr32(dev, 0x1854, state);
509
Ben Skeggs6ee73862009-12-11 19:24:15 +1000510 if (state)
511 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
512 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
513 else
514 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
515}
516
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000517static void nouveau_switcheroo_set_state(struct pci_dev *pdev,
518 enum vga_switcheroo_state state)
519{
Dave Airliefbf81762010-06-01 09:09:06 +1000520 struct drm_device *dev = pci_get_drvdata(pdev);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000521 pm_message_t pmm = { .event = PM_EVENT_SUSPEND };
522 if (state == VGA_SWITCHEROO_ON) {
523 printk(KERN_ERR "VGA switcheroo: switched nouveau on\n");
Dave Airlie5bcf7192010-12-07 09:20:40 +1000524 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000525 nouveau_pci_resume(pdev);
Dave Airliefbf81762010-06-01 09:09:06 +1000526 drm_kms_helper_poll_enable(dev);
Dave Airlie5bcf7192010-12-07 09:20:40 +1000527 dev->switch_power_state = DRM_SWITCH_POWER_ON;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000528 } else {
529 printk(KERN_ERR "VGA switcheroo: switched nouveau off\n");
Dave Airlie5bcf7192010-12-07 09:20:40 +1000530 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
Dave Airliefbf81762010-06-01 09:09:06 +1000531 drm_kms_helper_poll_disable(dev);
Peter Lekensteynd0992302011-12-17 12:54:04 +0100532 nouveau_switcheroo_optimus_dsm();
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000533 nouveau_pci_suspend(pdev, pmm);
Dave Airlie5bcf7192010-12-07 09:20:40 +1000534 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000535 }
536}
537
Dave Airlie8d608aa2010-12-07 08:57:57 +1000538static void nouveau_switcheroo_reprobe(struct pci_dev *pdev)
539{
540 struct drm_device *dev = pci_get_drvdata(pdev);
541 nouveau_fbcon_output_poll_changed(dev);
542}
543
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000544static bool nouveau_switcheroo_can_switch(struct pci_dev *pdev)
545{
546 struct drm_device *dev = pci_get_drvdata(pdev);
547 bool can_switch;
548
549 spin_lock(&dev->count_lock);
550 can_switch = (dev->open_count == 0);
551 spin_unlock(&dev->count_lock);
552 return can_switch;
553}
554
Ben Skeggs6ee73862009-12-11 19:24:15 +1000555int
556nouveau_card_init(struct drm_device *dev)
557{
558 struct drm_nouveau_private *dev_priv = dev->dev_private;
559 struct nouveau_engine *engine;
Ben Skeggseea55c82011-04-18 08:57:51 +1000560 int ret, e = 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000561
Ben Skeggs6ee73862009-12-11 19:24:15 +1000562 vga_client_register(dev->pdev, dev, NULL, nouveau_vga_set_decode);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000563 vga_switcheroo_register_client(dev->pdev, nouveau_switcheroo_set_state,
Dave Airlie8d608aa2010-12-07 08:57:57 +1000564 nouveau_switcheroo_reprobe,
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000565 nouveau_switcheroo_can_switch);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000566
567 /* Initialise internal driver API hooks */
568 ret = nouveau_init_engine_ptrs(dev);
569 if (ret)
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000570 goto out;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000571 engine = &dev_priv->engine;
Ben Skeggscff5c132010-10-06 16:16:59 +1000572 spin_lock_init(&dev_priv->channels.lock);
Francisco Jereza5cf68b2010-10-24 16:14:41 +0200573 spin_lock_init(&dev_priv->tile.lock);
Maarten Maathuisff9e5272010-02-01 20:58:27 +0100574 spin_lock_init(&dev_priv->context_switch_lock);
Ben Skeggs04eb34a2011-04-06 13:28:35 +1000575 spin_lock_init(&dev_priv->vm_lock);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000576
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200577 /* Make the CRTCs and I2C buses accessible */
578 ret = engine->display.early_init(dev);
579 if (ret)
580 goto out;
581
Ben Skeggs6ee73862009-12-11 19:24:15 +1000582 /* Parse BIOS tables / Run init tables if card not POSTed */
Ben Skeggscd0b0722010-06-01 15:56:22 +1000583 ret = nouveau_bios_init(dev);
584 if (ret)
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200585 goto out_display_early;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000586
Ben Skeggs4c5df492011-10-28 10:59:45 +1000587 /* workaround an odd issue on nvc1 by disabling the device's
588 * nosnoop capability. hopefully won't cause issues until a
589 * better fix is found - assuming there is one...
590 */
591 if (dev_priv->chipset == 0xc1) {
592 nv_mask(dev, 0x00088080, 0x00000800, 0x00000000);
593 }
594
Ben Skeggs668b6c02011-12-15 10:43:03 +1000595 /* PMC */
596 ret = engine->mc.init(dev);
Ben Skeggsa76fb4e2010-03-18 09:45:20 +1000597 if (ret)
598 goto out_bios;
599
Ben Skeggs668b6c02011-12-15 10:43:03 +1000600 /* PTIMER */
601 ret = engine->timer.init(dev);
602 if (ret)
603 goto out_mc;
604
605 /* PFB */
606 ret = engine->fb.init(dev);
607 if (ret)
608 goto out_timer;
609
610 ret = engine->vram.init(dev);
611 if (ret)
612 goto out_fb;
613
614 /* PGPIO */
615 ret = nouveau_gpio_create(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000616 if (ret)
Ben Skeggsfbd2895e2010-09-01 15:24:34 +1000617 goto out_vram;
618
Ben Skeggs668b6c02011-12-15 10:43:03 +1000619 ret = nouveau_gpuobj_init(dev);
620 if (ret)
621 goto out_gpio;
622
Ben Skeggsfbd2895e2010-09-01 15:24:34 +1000623 ret = engine->instmem.init(dev);
624 if (ret)
625 goto out_gpuobj;
626
Ben Skeggs24f246a2011-06-10 13:36:08 +1000627 ret = nouveau_mem_vram_init(dev);
Ben Skeggsfbd2895e2010-09-01 15:24:34 +1000628 if (ret)
629 goto out_instmem;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000630
Ben Skeggs24f246a2011-06-10 13:36:08 +1000631 ret = nouveau_mem_gart_init(dev);
632 if (ret)
633 goto out_ttmvram;
634
Ben Skeggs668b6c02011-12-15 10:43:03 +1000635 nouveau_pm_init(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000636
Ben Skeggsaba99a82011-05-25 14:48:50 +1000637 if (!dev_priv->noaccel) {
Ben Skeggs18b54c42011-05-25 15:22:33 +1000638 switch (dev_priv->card_type) {
639 case NV_04:
640 nv04_graph_create(dev);
641 break;
642 case NV_10:
643 nv10_graph_create(dev);
644 break;
645 case NV_20:
646 case NV_30:
647 nv20_graph_create(dev);
648 break;
649 case NV_40:
650 nv40_graph_create(dev);
651 break;
652 case NV_50:
653 nv50_graph_create(dev);
654 break;
655 case NV_C0:
Ben Skeggs06784092011-07-11 15:57:54 +1000656 case NV_D0:
Ben Skeggs18b54c42011-05-25 15:22:33 +1000657 nvc0_graph_create(dev);
658 break;
659 default:
Ben Skeggs7ff54412011-03-18 10:25:59 +1000660 break;
661 }
Ben Skeggs7ff54412011-03-18 10:25:59 +1000662
Ben Skeggs18b54c42011-05-25 15:22:33 +1000663 switch (dev_priv->chipset) {
664 case 0x84:
665 case 0x86:
666 case 0x92:
667 case 0x94:
668 case 0x96:
669 case 0xa0:
670 nv84_crypt_create(dev);
671 break;
Ben Skeggs8f27c542011-08-11 14:58:06 +1000672 case 0x98:
673 case 0xaa:
674 case 0xac:
675 nv98_crypt_create(dev);
676 break;
Ben Skeggs18b54c42011-05-25 15:22:33 +1000677 }
Ben Skeggsa02ccc72011-04-04 16:08:24 +1000678
Ben Skeggs18b54c42011-05-25 15:22:33 +1000679 switch (dev_priv->card_type) {
680 case NV_50:
681 switch (dev_priv->chipset) {
682 case 0xa3:
683 case 0xa5:
684 case 0xa8:
685 case 0xaf:
686 nva3_copy_create(dev);
687 break;
688 }
689 break;
690 case NV_C0:
691 nvc0_copy_create(dev, 0);
692 nvc0_copy_create(dev, 1);
693 break;
694 default:
695 break;
696 }
697
Ben Skeggs8f27c542011-08-11 14:58:06 +1000698 if (dev_priv->chipset >= 0xa3 || dev_priv->chipset == 0x98) {
699 nv84_bsp_create(dev);
700 nv84_vp_create(dev);
701 nv98_ppp_create(dev);
702 } else
703 if (dev_priv->chipset >= 0x84) {
704 nv50_mpeg_create(dev);
705 nv84_bsp_create(dev);
706 nv84_vp_create(dev);
707 } else
708 if (dev_priv->chipset >= 0x50) {
709 nv50_mpeg_create(dev);
710 } else
Ben Skeggs52d07332011-06-23 16:44:05 +1000711 if (dev_priv->card_type == NV_40 ||
712 dev_priv->chipset == 0x31 ||
713 dev_priv->chipset == 0x34 ||
Ben Skeggs8f27c542011-08-11 14:58:06 +1000714 dev_priv->chipset == 0x36) {
Ben Skeggs323dcac2011-06-23 16:21:21 +1000715 nv31_mpeg_create(dev);
Ben Skeggs8f27c542011-08-11 14:58:06 +1000716 }
Ben Skeggs18b54c42011-05-25 15:22:33 +1000717
Ben Skeggs6dfdd7a2011-03-31 15:40:43 +1000718 for (e = 0; e < NVOBJ_ENGINE_NR; e++) {
719 if (dev_priv->eng[e]) {
720 ret = dev_priv->eng[e]->init(dev, e);
721 if (ret)
722 goto out_engine;
723 }
724 }
725
Marcin Koƛcielnickia32ed692010-01-26 14:00:42 +0000726 /* PFIFO */
727 ret = engine->fifo.init(dev);
728 if (ret)
Ben Skeggsa82dd492011-04-01 13:56:05 +1000729 goto out_engine;
Marcin Koƛcielnickia32ed692010-01-26 14:00:42 +0000730 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000731
Ben Skeggs1575b362011-07-04 11:55:39 +1000732 ret = nouveau_irq_init(dev);
733 if (ret)
734 goto out_fifo;
735
Ben Skeggs27d50302011-10-06 12:46:40 +1000736 ret = nouveau_display_create(dev);
Ben Skeggse88efe02010-07-09 10:56:08 +1000737 if (ret)
Ben Skeggs1575b362011-07-04 11:55:39 +1000738 goto out_irq;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000739
Ben Skeggs10b461e2011-08-02 19:29:37 +1000740 nouveau_backlight_init(dev);
741
Ben Skeggsa82dd492011-04-01 13:56:05 +1000742 if (dev_priv->eng[NVOBJ_ENGINE_GR]) {
Francisco Jerez0c6c1c22010-09-22 00:58:54 +0200743 ret = nouveau_fence_init(dev);
Ben Skeggs0735f622009-12-16 14:28:55 +1000744 if (ret)
Ben Skeggs1575b362011-07-04 11:55:39 +1000745 goto out_disp;
Francisco Jerez0c6c1c22010-09-22 00:58:54 +0200746
Ben Skeggs1575b362011-07-04 11:55:39 +1000747 ret = nouveau_channel_alloc(dev, &dev_priv->channel, NULL,
748 NvDmaFB, NvDmaTT);
Francisco Jerez0c6c1c22010-09-22 00:58:54 +0200749 if (ret)
750 goto out_fence;
Ben Skeggs1575b362011-07-04 11:55:39 +1000751
752 mutex_unlock(&dev_priv->channel->mutex);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000753 }
754
Ben Skeggs1575b362011-07-04 11:55:39 +1000755 if (dev->mode_config.num_crtc) {
Ben Skeggsf62b27d2011-11-09 15:18:47 +1000756 ret = nouveau_display_init(dev);
Ben Skeggs1575b362011-07-04 11:55:39 +1000757 if (ret)
758 goto out_chan;
759
760 nouveau_fbcon_init(dev);
Ben Skeggs1575b362011-07-04 11:55:39 +1000761 }
762
Ben Skeggs6ee73862009-12-11 19:24:15 +1000763 return 0;
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000764
Ben Skeggs1575b362011-07-04 11:55:39 +1000765out_chan:
766 nouveau_channel_put_unlocked(&dev_priv->channel);
Francisco Jerez0c6c1c22010-09-22 00:58:54 +0200767out_fence:
768 nouveau_fence_fini(dev);
Ben Skeggs1575b362011-07-04 11:55:39 +1000769out_disp:
Ben Skeggs10b461e2011-08-02 19:29:37 +1000770 nouveau_backlight_exit(dev);
Ben Skeggs27d50302011-10-06 12:46:40 +1000771 nouveau_display_destroy(dev);
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000772out_irq:
Ben Skeggs35fa2f22010-10-21 14:07:03 +1000773 nouveau_irq_fini(dev);
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000774out_fifo:
Ben Skeggsaba99a82011-05-25 14:48:50 +1000775 if (!dev_priv->noaccel)
Marcin Koƛcielnickia32ed692010-01-26 14:00:42 +0000776 engine->fifo.takedown(dev);
Ben Skeggs6dfdd7a2011-03-31 15:40:43 +1000777out_engine:
Ben Skeggsaba99a82011-05-25 14:48:50 +1000778 if (!dev_priv->noaccel) {
Ben Skeggs6dfdd7a2011-03-31 15:40:43 +1000779 for (e = e - 1; e >= 0; e--) {
Ben Skeggs2703c212011-04-01 09:50:18 +1000780 if (!dev_priv->eng[e])
781 continue;
Ben Skeggs6c320fe2011-07-20 11:22:33 +1000782 dev_priv->eng[e]->fini(dev, e, false);
Ben Skeggs2703c212011-04-01 09:50:18 +1000783 dev_priv->eng[e]->destroy(dev,e );
Ben Skeggs6dfdd7a2011-03-31 15:40:43 +1000784 }
785 }
Ben Skeggs668b6c02011-12-15 10:43:03 +1000786 nouveau_pm_fini(dev);
Ben Skeggsfbd2895e2010-09-01 15:24:34 +1000787 nouveau_mem_gart_fini(dev);
Ben Skeggs24f246a2011-06-10 13:36:08 +1000788out_ttmvram:
789 nouveau_mem_vram_fini(dev);
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000790out_instmem:
791 engine->instmem.takedown(dev);
Ben Skeggsfbd2895e2010-09-01 15:24:34 +1000792out_gpuobj:
793 nouveau_gpuobj_takedown(dev);
Ben Skeggs668b6c02011-12-15 10:43:03 +1000794out_gpio:
795 nouveau_gpio_destroy(dev);
Ben Skeggsfbd2895e2010-09-01 15:24:34 +1000796out_vram:
Ben Skeggs24f246a2011-06-10 13:36:08 +1000797 engine->vram.takedown(dev);
Ben Skeggs668b6c02011-12-15 10:43:03 +1000798out_fb:
799 engine->fb.takedown(dev);
800out_timer:
801 engine->timer.takedown(dev);
802out_mc:
803 engine->mc.takedown(dev);
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000804out_bios:
805 nouveau_bios_takedown(dev);
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200806out_display_early:
807 engine->display.late_takedown(dev);
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000808out:
809 vga_client_register(dev->pdev, NULL, NULL, NULL);
810 return ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000811}
812
813static void nouveau_card_takedown(struct drm_device *dev)
814{
815 struct drm_nouveau_private *dev_priv = dev->dev_private;
816 struct nouveau_engine *engine = &dev_priv->engine;
Ben Skeggs6dfdd7a2011-03-31 15:40:43 +1000817 int e;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000818
Ben Skeggs1575b362011-07-04 11:55:39 +1000819 if (dev->mode_config.num_crtc) {
Ben Skeggs1575b362011-07-04 11:55:39 +1000820 nouveau_fbcon_fini(dev);
Ben Skeggsf62b27d2011-11-09 15:18:47 +1000821 nouveau_display_fini(dev);
Ben Skeggs1575b362011-07-04 11:55:39 +1000822 }
Ben Skeggs06b75e32011-06-08 18:29:12 +1000823
Ben Skeggsa82dd492011-04-01 13:56:05 +1000824 if (dev_priv->channel) {
Francisco Jerez36c952e2010-10-18 03:01:34 +0200825 nouveau_channel_put_unlocked(&dev_priv->channel);
Ben Skeggs06b75e32011-06-08 18:29:12 +1000826 nouveau_fence_fini(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000827 }
Ben Skeggsb6d3d872010-06-07 15:38:27 +1000828
Ben Skeggs10b461e2011-08-02 19:29:37 +1000829 nouveau_backlight_exit(dev);
Ben Skeggs27d50302011-10-06 12:46:40 +1000830 nouveau_display_destroy(dev);
Ben Skeggs06b75e32011-06-08 18:29:12 +1000831
Ben Skeggsaba99a82011-05-25 14:48:50 +1000832 if (!dev_priv->noaccel) {
Ben Skeggsb6d3d872010-06-07 15:38:27 +1000833 engine->fifo.takedown(dev);
Ben Skeggs6dfdd7a2011-03-31 15:40:43 +1000834 for (e = NVOBJ_ENGINE_NR - 1; e >= 0; e--) {
835 if (dev_priv->eng[e]) {
Ben Skeggs6c320fe2011-07-20 11:22:33 +1000836 dev_priv->eng[e]->fini(dev, e, false);
Ben Skeggs6dfdd7a2011-03-31 15:40:43 +1000837 dev_priv->eng[e]->destroy(dev,e );
838 }
839 }
Ben Skeggsb6d3d872010-06-07 15:38:27 +1000840 }
Ben Skeggsb6d3d872010-06-07 15:38:27 +1000841
Jimmy Rentz97666102011-04-17 16:15:09 -0400842 if (dev_priv->vga_ram) {
843 nouveau_bo_unpin(dev_priv->vga_ram);
844 nouveau_bo_ref(NULL, &dev_priv->vga_ram);
845 }
846
Ben Skeggsb6d3d872010-06-07 15:38:27 +1000847 mutex_lock(&dev->struct_mutex);
848 ttm_bo_clean_mm(&dev_priv->ttm.bdev, TTM_PL_VRAM);
849 ttm_bo_clean_mm(&dev_priv->ttm.bdev, TTM_PL_TT);
850 mutex_unlock(&dev->struct_mutex);
Ben Skeggsfbd2895e2010-09-01 15:24:34 +1000851 nouveau_mem_gart_fini(dev);
Ben Skeggs24f246a2011-06-10 13:36:08 +1000852 nouveau_mem_vram_fini(dev);
Ben Skeggsb6d3d872010-06-07 15:38:27 +1000853
Ben Skeggsb6d3d872010-06-07 15:38:27 +1000854 engine->instmem.takedown(dev);
Ben Skeggsfbd2895e2010-09-01 15:24:34 +1000855 nouveau_gpuobj_takedown(dev);
Ben Skeggsb6d3d872010-06-07 15:38:27 +1000856
Ben Skeggs330c5982010-09-16 15:39:49 +1000857 nouveau_pm_fini(dev);
Ben Skeggs668b6c02011-12-15 10:43:03 +1000858
859 nouveau_gpio_destroy(dev);
860 engine->vram.takedown(dev);
861 engine->fb.takedown(dev);
862 engine->timer.takedown(dev);
863 engine->mc.takedown(dev);
864
Ben Skeggsb6d3d872010-06-07 15:38:27 +1000865 nouveau_bios_takedown(dev);
Ben Skeggs668b6c02011-12-15 10:43:03 +1000866 engine->display.late_takedown(dev);
867
868 nouveau_irq_fini(dev);
Ben Skeggsb6d3d872010-06-07 15:38:27 +1000869
870 vga_client_register(dev->pdev, NULL, NULL, NULL);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000871}
872
Ben Skeggs3f0a68d2011-05-31 11:11:28 +1000873int
874nouveau_open(struct drm_device *dev, struct drm_file *file_priv)
875{
Ben Skeggsfe32b162011-06-03 10:07:08 +1000876 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs3f0a68d2011-05-31 11:11:28 +1000877 struct nouveau_fpriv *fpriv;
Ben Skeggse41f26e2011-06-07 15:35:37 +1000878 int ret;
Ben Skeggs3f0a68d2011-05-31 11:11:28 +1000879
880 fpriv = kzalloc(sizeof(*fpriv), GFP_KERNEL);
881 if (unlikely(!fpriv))
882 return -ENOMEM;
883
884 spin_lock_init(&fpriv->lock);
Ben Skeggse8a863c2011-06-01 19:18:48 +1000885 INIT_LIST_HEAD(&fpriv->channels);
886
Ben Skeggse41f26e2011-06-07 15:35:37 +1000887 if (dev_priv->card_type == NV_50) {
888 ret = nouveau_vm_new(dev, 0, (1ULL << 40), 0x0020000000ULL,
889 &fpriv->vm);
890 if (ret) {
891 kfree(fpriv);
892 return ret;
893 }
894 } else
895 if (dev_priv->card_type >= NV_C0) {
Ben Skeggs5de80372011-06-08 18:17:41 +1000896 ret = nouveau_vm_new(dev, 0, (1ULL << 40), 0x0008000000ULL,
897 &fpriv->vm);
898 if (ret) {
899 kfree(fpriv);
900 return ret;
901 }
Ben Skeggse41f26e2011-06-07 15:35:37 +1000902 }
Ben Skeggsfe32b162011-06-03 10:07:08 +1000903
Ben Skeggs3f0a68d2011-05-31 11:11:28 +1000904 file_priv->driver_priv = fpriv;
905 return 0;
906}
907
Ben Skeggs6ee73862009-12-11 19:24:15 +1000908/* here a client dies, release the stuff that was allocated for its
909 * file_priv */
910void nouveau_preclose(struct drm_device *dev, struct drm_file *file_priv)
911{
912 nouveau_channel_cleanup(dev, file_priv);
913}
914
Ben Skeggs3f0a68d2011-05-31 11:11:28 +1000915void
916nouveau_postclose(struct drm_device *dev, struct drm_file *file_priv)
917{
918 struct nouveau_fpriv *fpriv = nouveau_fpriv(file_priv);
Ben Skeggsfe32b162011-06-03 10:07:08 +1000919 nouveau_vm_ref(NULL, &fpriv->vm, NULL);
Ben Skeggs3f0a68d2011-05-31 11:11:28 +1000920 kfree(fpriv);
921}
922
Ben Skeggs6ee73862009-12-11 19:24:15 +1000923/* first module load, setup the mmio/fb mapping */
924/* KMS: we need mmio at load time, not when the first drm client opens. */
925int nouveau_firstopen(struct drm_device *dev)
926{
927 return 0;
928}
929
930/* if we have an OF card, copy vbios to RAMIN */
931static void nouveau_OF_copy_vbios_to_ramin(struct drm_device *dev)
932{
933#if defined(__powerpc__)
934 int size, i;
935 const uint32_t *bios;
936 struct device_node *dn = pci_device_to_OF_node(dev->pdev);
937 if (!dn) {
938 NV_INFO(dev, "Unable to get the OF node\n");
939 return;
940 }
941
942 bios = of_get_property(dn, "NVDA,BMP", &size);
943 if (bios) {
944 for (i = 0; i < size; i += 4)
945 nv_wi32(dev, i, bios[i/4]);
946 NV_INFO(dev, "OF bios successfully copied (%d bytes)\n", size);
947 } else {
948 NV_INFO(dev, "Unable to get the OF bios\n");
949 }
950#endif
951}
952
Marcin Slusarz06415c52010-05-16 17:29:56 +0200953static struct apertures_struct *nouveau_get_apertures(struct drm_device *dev)
954{
955 struct pci_dev *pdev = dev->pdev;
956 struct apertures_struct *aper = alloc_apertures(3);
957 if (!aper)
958 return NULL;
959
960 aper->ranges[0].base = pci_resource_start(pdev, 1);
961 aper->ranges[0].size = pci_resource_len(pdev, 1);
962 aper->count = 1;
963
964 if (pci_resource_len(pdev, 2)) {
965 aper->ranges[aper->count].base = pci_resource_start(pdev, 2);
966 aper->ranges[aper->count].size = pci_resource_len(pdev, 2);
967 aper->count++;
968 }
969
970 if (pci_resource_len(pdev, 3)) {
971 aper->ranges[aper->count].base = pci_resource_start(pdev, 3);
972 aper->ranges[aper->count].size = pci_resource_len(pdev, 3);
973 aper->count++;
974 }
975
976 return aper;
977}
978
979static int nouveau_remove_conflicting_drivers(struct drm_device *dev)
980{
981 struct drm_nouveau_private *dev_priv = dev->dev_private;
Marcin Slusarz3b9676e2010-05-16 17:33:09 +0200982 bool primary = false;
Marcin Slusarz06415c52010-05-16 17:29:56 +0200983 dev_priv->apertures = nouveau_get_apertures(dev);
984 if (!dev_priv->apertures)
985 return -ENOMEM;
986
Marcin Slusarz3b9676e2010-05-16 17:33:09 +0200987#ifdef CONFIG_X86
988 primary = dev->pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW;
989#endif
Emil Velikovf2129492011-03-19 23:31:52 +0000990
Marcin Slusarz3b9676e2010-05-16 17:33:09 +0200991 remove_conflicting_framebuffers(dev_priv->apertures, "nouveaufb", primary);
Marcin Slusarz06415c52010-05-16 17:29:56 +0200992 return 0;
993}
994
Ben Skeggs6ee73862009-12-11 19:24:15 +1000995int nouveau_load(struct drm_device *dev, unsigned long flags)
996{
997 struct drm_nouveau_private *dev_priv;
Ben Skeggsf2cbe462011-07-21 15:39:06 +1000998 uint32_t reg0, strap;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000999 resource_size_t mmio_start_offs;
Ben Skeggscd0b0722010-06-01 15:56:22 +10001000 int ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001001
1002 dev_priv = kzalloc(sizeof(*dev_priv), GFP_KERNEL);
Dan Carpentera0d069e2010-07-30 17:04:32 +02001003 if (!dev_priv) {
1004 ret = -ENOMEM;
1005 goto err_out;
1006 }
Ben Skeggs6ee73862009-12-11 19:24:15 +10001007 dev->dev_private = dev_priv;
1008 dev_priv->dev = dev;
1009
Dave Airlie466e69b2011-12-19 11:15:29 +00001010 pci_set_master(dev->pdev);
1011
Ben Skeggs6ee73862009-12-11 19:24:15 +10001012 dev_priv->flags = flags & NOUVEAU_FLAGS;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001013
1014 NV_DEBUG(dev, "vendor: 0x%X device: 0x%X class: 0x%X\n",
1015 dev->pci_vendor, dev->pci_device, dev->pdev->class);
1016
Ben Skeggs6ee73862009-12-11 19:24:15 +10001017 /* resource 0 is mmio regs */
1018 /* resource 1 is linear FB */
1019 /* resource 2 is RAMIN (mmio regs + 0x1000000) */
1020 /* resource 6 is bios */
1021
1022 /* map the mmio regs */
1023 mmio_start_offs = pci_resource_start(dev->pdev, 0);
1024 dev_priv->mmio = ioremap(mmio_start_offs, 0x00800000);
1025 if (!dev_priv->mmio) {
1026 NV_ERROR(dev, "Unable to initialize the mmio mapping. "
1027 "Please report your setup to " DRIVER_EMAIL "\n");
Dan Carpentera0d069e2010-07-30 17:04:32 +02001028 ret = -EINVAL;
Tejun Heod82f8e62011-01-26 17:49:18 +01001029 goto err_priv;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001030 }
1031 NV_DEBUG(dev, "regs mapped ok at 0x%llx\n",
1032 (unsigned long long)mmio_start_offs);
1033
1034#ifdef __BIG_ENDIAN
1035 /* Put the card in BE mode if it's not */
Ben Skeggs08975542011-06-14 10:16:17 +10001036 if (nv_rd32(dev, NV03_PMC_BOOT_1) != 0x01000001)
1037 nv_wr32(dev, NV03_PMC_BOOT_1, 0x01000001);
Ben Skeggs6ee73862009-12-11 19:24:15 +10001038
1039 DRM_MEMORYBARRIER();
1040#endif
1041
1042 /* Time to determine the card architecture */
1043 reg0 = nv_rd32(dev, NV03_PMC_BOOT_0);
1044
1045 /* We're dealing with >=NV10 */
1046 if ((reg0 & 0x0f000000) > 0) {
1047 /* Bit 27-20 contain the architecture in hex */
1048 dev_priv->chipset = (reg0 & 0xff00000) >> 20;
1049 /* NV04 or NV05 */
1050 } else if ((reg0 & 0xff00fff0) == 0x20004000) {
Ben Skeggs1dee7a92010-01-07 13:47:57 +10001051 if (reg0 & 0x00f00000)
1052 dev_priv->chipset = 0x05;
1053 else
1054 dev_priv->chipset = 0x04;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001055 } else
1056 dev_priv->chipset = 0xff;
1057
1058 switch (dev_priv->chipset & 0xf0) {
1059 case 0x00:
1060 case 0x10:
1061 case 0x20:
1062 case 0x30:
1063 dev_priv->card_type = dev_priv->chipset & 0xf0;
1064 break;
1065 case 0x40:
1066 case 0x60:
1067 dev_priv->card_type = NV_40;
1068 break;
1069 case 0x50:
1070 case 0x80:
1071 case 0x90:
1072 case 0xa0:
1073 dev_priv->card_type = NV_50;
1074 break;
Ben Skeggs4b223ee2010-08-03 10:00:56 +10001075 case 0xc0:
1076 dev_priv->card_type = NV_C0;
1077 break;
Ben Skeggsd9f61c22011-07-04 13:25:17 +10001078 case 0xd0:
1079 dev_priv->card_type = NV_D0;
1080 break;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001081 default:
1082 NV_INFO(dev, "Unsupported chipset 0x%08x\n", reg0);
Dan Carpentera0d069e2010-07-30 17:04:32 +02001083 ret = -EINVAL;
1084 goto err_mmio;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001085 }
1086
1087 NV_INFO(dev, "Detected an NV%2x generation card (0x%08x)\n",
1088 dev_priv->card_type, reg0);
1089
Ben Skeggsf2cbe462011-07-21 15:39:06 +10001090 /* determine frequency of timing crystal */
1091 strap = nv_rd32(dev, 0x101000);
1092 if ( dev_priv->chipset < 0x17 ||
1093 (dev_priv->chipset >= 0x20 && dev_priv->chipset <= 0x25))
1094 strap &= 0x00000040;
1095 else
1096 strap &= 0x00400040;
1097
1098 switch (strap) {
1099 case 0x00000000: dev_priv->crystal = 13500; break;
1100 case 0x00000040: dev_priv->crystal = 14318; break;
1101 case 0x00400000: dev_priv->crystal = 27000; break;
1102 case 0x00400040: dev_priv->crystal = 25000; break;
1103 }
1104
1105 NV_DEBUG(dev, "crystal freq: %dKHz\n", dev_priv->crystal);
1106
Ben Skeggsaba99a82011-05-25 14:48:50 +10001107 /* Determine whether we'll attempt acceleration or not, some
1108 * cards are disabled by default here due to them being known
1109 * non-functional, or never been tested due to lack of hw.
1110 */
1111 dev_priv->noaccel = !!nouveau_noaccel;
1112 if (nouveau_noaccel == -1) {
1113 switch (dev_priv->chipset) {
Ben Skeggs06784092011-07-11 15:57:54 +10001114 case 0xd9: /* known broken */
Ben Skeggsad830d22011-05-27 16:18:10 +10001115 NV_INFO(dev, "acceleration disabled by default, pass "
1116 "noaccel=0 to force enable\n");
Ben Skeggsaba99a82011-05-25 14:48:50 +10001117 dev_priv->noaccel = true;
1118 break;
1119 default:
1120 dev_priv->noaccel = false;
1121 break;
1122 }
1123 }
1124
Ben Skeggscd0b0722010-06-01 15:56:22 +10001125 ret = nouveau_remove_conflicting_drivers(dev);
1126 if (ret)
Dan Carpentera0d069e2010-07-30 17:04:32 +02001127 goto err_mmio;
Marcin Slusarz06415c52010-05-16 17:29:56 +02001128
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001129 /* Map PRAMIN BAR, or on older cards, the aperture within BAR0 */
Ben Skeggs6ee73862009-12-11 19:24:15 +10001130 if (dev_priv->card_type >= NV_40) {
1131 int ramin_bar = 2;
1132 if (pci_resource_len(dev->pdev, ramin_bar) == 0)
1133 ramin_bar = 3;
1134
1135 dev_priv->ramin_size = pci_resource_len(dev->pdev, ramin_bar);
Ben Skeggs6d696302010-06-02 10:16:24 +10001136 dev_priv->ramin =
1137 ioremap(pci_resource_start(dev->pdev, ramin_bar),
Ben Skeggs6ee73862009-12-11 19:24:15 +10001138 dev_priv->ramin_size);
1139 if (!dev_priv->ramin) {
Marcin Slusarzff920bf2011-08-22 23:28:56 +02001140 NV_ERROR(dev, "Failed to map PRAMIN BAR\n");
Dan Carpentera0d069e2010-07-30 17:04:32 +02001141 ret = -ENOMEM;
1142 goto err_mmio;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001143 }
Ben Skeggs6d696302010-06-02 10:16:24 +10001144 } else {
Ben Skeggs6ee73862009-12-11 19:24:15 +10001145 dev_priv->ramin_size = 1 * 1024 * 1024;
1146 dev_priv->ramin = ioremap(mmio_start_offs + NV_RAMIN,
Ben Skeggs6d696302010-06-02 10:16:24 +10001147 dev_priv->ramin_size);
Ben Skeggs6ee73862009-12-11 19:24:15 +10001148 if (!dev_priv->ramin) {
1149 NV_ERROR(dev, "Failed to map BAR0 PRAMIN.\n");
Dan Carpentera0d069e2010-07-30 17:04:32 +02001150 ret = -ENOMEM;
1151 goto err_mmio;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001152 }
1153 }
1154
1155 nouveau_OF_copy_vbios_to_ramin(dev);
1156
1157 /* Special flags */
1158 if (dev->pci_device == 0x01a0)
1159 dev_priv->flags |= NV_NFORCE;
1160 else if (dev->pci_device == 0x01f0)
1161 dev_priv->flags |= NV_NFORCE2;
1162
1163 /* For kernel modesetting, init card now and bring up fbcon */
Ben Skeggscd0b0722010-06-01 15:56:22 +10001164 ret = nouveau_card_init(dev);
1165 if (ret)
Dan Carpentera0d069e2010-07-30 17:04:32 +02001166 goto err_ramin;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001167
1168 return 0;
Dan Carpentera0d069e2010-07-30 17:04:32 +02001169
1170err_ramin:
1171 iounmap(dev_priv->ramin);
1172err_mmio:
1173 iounmap(dev_priv->mmio);
Dan Carpentera0d069e2010-07-30 17:04:32 +02001174err_priv:
1175 kfree(dev_priv);
1176 dev->dev_private = NULL;
1177err_out:
1178 return ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001179}
1180
Ben Skeggs6ee73862009-12-11 19:24:15 +10001181void nouveau_lastclose(struct drm_device *dev)
1182{
Dave Airlie5ccb3772010-12-07 13:56:26 +10001183 vga_switcheroo_process_delayed_switch();
Ben Skeggs6ee73862009-12-11 19:24:15 +10001184}
1185
1186int nouveau_unload(struct drm_device *dev)
1187{
1188 struct drm_nouveau_private *dev_priv = dev->dev_private;
1189
Ben Skeggscd0b0722010-06-01 15:56:22 +10001190 nouveau_card_takedown(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +10001191
1192 iounmap(dev_priv->mmio);
1193 iounmap(dev_priv->ramin);
1194
1195 kfree(dev_priv);
1196 dev->dev_private = NULL;
1197 return 0;
1198}
1199
Ben Skeggs6ee73862009-12-11 19:24:15 +10001200int nouveau_ioctl_getparam(struct drm_device *dev, void *data,
1201 struct drm_file *file_priv)
1202{
1203 struct drm_nouveau_private *dev_priv = dev->dev_private;
1204 struct drm_nouveau_getparam *getparam = data;
1205
Ben Skeggs6ee73862009-12-11 19:24:15 +10001206 switch (getparam->param) {
1207 case NOUVEAU_GETPARAM_CHIPSET_ID:
1208 getparam->value = dev_priv->chipset;
1209 break;
1210 case NOUVEAU_GETPARAM_PCI_VENDOR:
1211 getparam->value = dev->pci_vendor;
1212 break;
1213 case NOUVEAU_GETPARAM_PCI_DEVICE:
1214 getparam->value = dev->pci_device;
1215 break;
1216 case NOUVEAU_GETPARAM_BUS_TYPE:
Dave Airlie8410ea32010-12-15 03:16:38 +10001217 if (drm_pci_device_is_agp(dev))
Ben Skeggs6ee73862009-12-11 19:24:15 +10001218 getparam->value = NV_AGP;
Jon Mason58b65422011-06-27 16:07:50 +00001219 else if (pci_is_pcie(dev->pdev))
Ben Skeggs6ee73862009-12-11 19:24:15 +10001220 getparam->value = NV_PCIE;
1221 else
1222 getparam->value = NV_PCI;
1223 break;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001224 case NOUVEAU_GETPARAM_FB_SIZE:
1225 getparam->value = dev_priv->fb_available_size;
1226 break;
1227 case NOUVEAU_GETPARAM_AGP_SIZE:
1228 getparam->value = dev_priv->gart_info.aper_size;
1229 break;
1230 case NOUVEAU_GETPARAM_VM_VRAM_BASE:
Ben Skeggs6d6c5a12010-11-16 10:17:53 +10001231 getparam->value = 0; /* deprecated */
Ben Skeggs6ee73862009-12-11 19:24:15 +10001232 break;
Marcin Koƛcielnicki7fc74f12010-05-23 11:36:04 +00001233 case NOUVEAU_GETPARAM_PTIMER_TIME:
1234 getparam->value = dev_priv->engine.timer.read(dev);
1235 break;
Francisco Jerezf13b3262010-10-10 06:01:08 +02001236 case NOUVEAU_GETPARAM_HAS_BO_USAGE:
1237 getparam->value = 1;
1238 break;
Francisco Jerez332b2422010-10-20 23:35:40 +02001239 case NOUVEAU_GETPARAM_HAS_PAGEFLIP:
Ben Skeggs3376ee32011-11-12 14:28:12 +10001240 getparam->value = 1;
Francisco Jerez332b2422010-10-20 23:35:40 +02001241 break;
Marcin Koƛcielnicki69c97002010-01-26 18:39:20 +00001242 case NOUVEAU_GETPARAM_GRAPH_UNITS:
1243 /* NV40 and NV50 versions are quite different, but register
1244 * address is the same. User is supposed to know the card
1245 * family anyway... */
1246 if (dev_priv->chipset >= 0x40) {
1247 getparam->value = nv_rd32(dev, NV40_PMC_GRAPH_UNITS);
1248 break;
1249 }
1250 /* FALLTHRU */
Ben Skeggs6ee73862009-12-11 19:24:15 +10001251 default:
Francisco Jerez1397b422010-10-12 03:17:43 +02001252 NV_DEBUG(dev, "unknown parameter %lld\n", getparam->param);
Ben Skeggs6ee73862009-12-11 19:24:15 +10001253 return -EINVAL;
1254 }
1255
1256 return 0;
1257}
1258
1259int
1260nouveau_ioctl_setparam(struct drm_device *dev, void *data,
1261 struct drm_file *file_priv)
1262{
1263 struct drm_nouveau_setparam *setparam = data;
1264
Ben Skeggs6ee73862009-12-11 19:24:15 +10001265 switch (setparam->param) {
1266 default:
Francisco Jerez1397b422010-10-12 03:17:43 +02001267 NV_DEBUG(dev, "unknown parameter %lld\n", setparam->param);
Ben Skeggs6ee73862009-12-11 19:24:15 +10001268 return -EINVAL;
1269 }
1270
1271 return 0;
1272}
1273
1274/* Wait until (value(reg) & mask) == val, up until timeout has hit */
Ben Skeggs12fb9522010-11-19 14:32:56 +10001275bool
1276nouveau_wait_eq(struct drm_device *dev, uint64_t timeout,
1277 uint32_t reg, uint32_t mask, uint32_t val)
Ben Skeggs6ee73862009-12-11 19:24:15 +10001278{
1279 struct drm_nouveau_private *dev_priv = dev->dev_private;
1280 struct nouveau_timer_engine *ptimer = &dev_priv->engine.timer;
1281 uint64_t start = ptimer->read(dev);
1282
1283 do {
1284 if ((nv_rd32(dev, reg) & mask) == val)
1285 return true;
1286 } while (ptimer->read(dev) - start < timeout);
1287
1288 return false;
1289}
1290
Ben Skeggs12fb9522010-11-19 14:32:56 +10001291/* Wait until (value(reg) & mask) != val, up until timeout has hit */
1292bool
1293nouveau_wait_ne(struct drm_device *dev, uint64_t timeout,
1294 uint32_t reg, uint32_t mask, uint32_t val)
1295{
1296 struct drm_nouveau_private *dev_priv = dev->dev_private;
1297 struct nouveau_timer_engine *ptimer = &dev_priv->engine.timer;
1298 uint64_t start = ptimer->read(dev);
1299
1300 do {
1301 if ((nv_rd32(dev, reg) & mask) != val)
1302 return true;
1303 } while (ptimer->read(dev) - start < timeout);
1304
1305 return false;
1306}
1307
Ben Skeggs78e29332011-06-18 16:27:24 +10001308/* Wait until cond(data) == true, up until timeout has hit */
1309bool
1310nouveau_wait_cb(struct drm_device *dev, u64 timeout,
1311 bool (*cond)(void *), void *data)
1312{
1313 struct drm_nouveau_private *dev_priv = dev->dev_private;
1314 struct nouveau_timer_engine *ptimer = &dev_priv->engine.timer;
1315 u64 start = ptimer->read(dev);
1316
1317 do {
1318 if (cond(data) == true)
1319 return true;
1320 } while (ptimer->read(dev) - start < timeout);
1321
1322 return false;
1323}
1324
Ben Skeggs6ee73862009-12-11 19:24:15 +10001325/* Waits for PGRAPH to go completely idle */
1326bool nouveau_wait_for_idle(struct drm_device *dev)
1327{
Francisco Jerez0541324a2010-10-18 16:15:15 +02001328 struct drm_nouveau_private *dev_priv = dev->dev_private;
1329 uint32_t mask = ~0;
1330
1331 if (dev_priv->card_type == NV_40)
1332 mask &= ~NV40_PGRAPH_STATUS_SYNC_STALL;
1333
1334 if (!nv_wait(dev, NV04_PGRAPH_STATUS, mask, 0)) {
Ben Skeggs6ee73862009-12-11 19:24:15 +10001335 NV_ERROR(dev, "PGRAPH idle timed out with status 0x%08x\n",
1336 nv_rd32(dev, NV04_PGRAPH_STATUS));
1337 return false;
1338 }
1339
1340 return true;
1341}
1342