blob: c5f29f0d18da637653feb6015b1aba02bfd1cc52 [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 Skeggs330c5982010-09-16 15:39:49 +100039#include "nouveau_pm.h"
Ben Skeggs6ee73862009-12-11 19:24:15 +100040#include "nv50_display.h"
41
Ben Skeggs6ee73862009-12-11 19:24:15 +100042static void nouveau_stub_takedown(struct drm_device *dev) {}
Ben Skeggsee2e0132010-07-26 09:28:25 +100043static int nouveau_stub_init(struct drm_device *dev) { return 0; }
Ben Skeggs6ee73862009-12-11 19:24:15 +100044
45static int nouveau_init_engine_ptrs(struct drm_device *dev)
46{
47 struct drm_nouveau_private *dev_priv = dev->dev_private;
48 struct nouveau_engine *engine = &dev_priv->engine;
49
50 switch (dev_priv->chipset & 0xf0) {
51 case 0x00:
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.populate = nv04_instmem_populate;
57 engine->instmem.clear = nv04_instmem_clear;
58 engine->instmem.bind = nv04_instmem_bind;
59 engine->instmem.unbind = nv04_instmem_unbind;
Ben Skeggsf56cb862010-07-08 11:29:10 +100060 engine->instmem.flush = nv04_instmem_flush;
Ben Skeggs6ee73862009-12-11 19:24:15 +100061 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;
Ben Skeggs6ee73862009-12-11 19:24:15 +100068 engine->graph.init = nv04_graph_init;
69 engine->graph.takedown = nv04_graph_takedown;
70 engine->graph.fifo_access = nv04_graph_fifo_access;
71 engine->graph.channel = nv04_graph_channel;
72 engine->graph.create_context = nv04_graph_create_context;
73 engine->graph.destroy_context = nv04_graph_destroy_context;
74 engine->graph.load_context = nv04_graph_load_context;
75 engine->graph.unload_context = nv04_graph_unload_context;
76 engine->fifo.channels = 16;
77 engine->fifo.init = nv04_fifo_init;
78 engine->fifo.takedown = nouveau_stub_takedown;
79 engine->fifo.disable = nv04_fifo_disable;
80 engine->fifo.enable = nv04_fifo_enable;
81 engine->fifo.reassign = nv04_fifo_reassign;
Francisco Jerez588d7d12009-12-13 20:07:42 +010082 engine->fifo.cache_pull = nv04_fifo_cache_pull;
Ben Skeggs6ee73862009-12-11 19:24:15 +100083 engine->fifo.channel_id = nv04_fifo_channel_id;
84 engine->fifo.create_context = nv04_fifo_create_context;
85 engine->fifo.destroy_context = nv04_fifo_destroy_context;
86 engine->fifo.load_context = nv04_fifo_load_context;
87 engine->fifo.unload_context = nv04_fifo_unload_context;
Francisco Jerezc88c2e02010-07-24 17:37:33 +020088 engine->display.early_init = nv04_display_early_init;
89 engine->display.late_takedown = nv04_display_late_takedown;
90 engine->display.create = nv04_display_create;
91 engine->display.init = nv04_display_init;
92 engine->display.destroy = nv04_display_destroy;
Ben Skeggsee2e0132010-07-26 09:28:25 +100093 engine->gpio.init = nouveau_stub_init;
94 engine->gpio.takedown = nouveau_stub_takedown;
95 engine->gpio.get = NULL;
96 engine->gpio.set = NULL;
97 engine->gpio.irq_enable = NULL;
Ben Skeggs442b6262010-09-16 16:25:26 +100098 engine->pm.clock_get = nv04_pm_clock_get;
99 engine->pm.clock_pre = nv04_pm_clock_pre;
100 engine->pm.clock_set = nv04_pm_clock_set;
Ben Skeggsbd2e5972010-10-19 20:06:01 +1000101 engine->crypt.init = nouveau_stub_init;
102 engine->crypt.takedown = nouveau_stub_takedown;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000103 break;
104 case 0x10:
105 engine->instmem.init = nv04_instmem_init;
106 engine->instmem.takedown = nv04_instmem_takedown;
107 engine->instmem.suspend = nv04_instmem_suspend;
108 engine->instmem.resume = nv04_instmem_resume;
109 engine->instmem.populate = nv04_instmem_populate;
110 engine->instmem.clear = nv04_instmem_clear;
111 engine->instmem.bind = nv04_instmem_bind;
112 engine->instmem.unbind = nv04_instmem_unbind;
Ben Skeggsf56cb862010-07-08 11:29:10 +1000113 engine->instmem.flush = nv04_instmem_flush;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000114 engine->mc.init = nv04_mc_init;
115 engine->mc.takedown = nv04_mc_takedown;
116 engine->timer.init = nv04_timer_init;
117 engine->timer.read = nv04_timer_read;
118 engine->timer.takedown = nv04_timer_takedown;
119 engine->fb.init = nv10_fb_init;
120 engine->fb.takedown = nv10_fb_takedown;
Francisco Jerezcb00f7c2009-12-16 12:12:27 +0100121 engine->fb.set_region_tiling = nv10_fb_set_region_tiling;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000122 engine->graph.init = nv10_graph_init;
123 engine->graph.takedown = nv10_graph_takedown;
124 engine->graph.channel = nv10_graph_channel;
125 engine->graph.create_context = nv10_graph_create_context;
126 engine->graph.destroy_context = nv10_graph_destroy_context;
127 engine->graph.fifo_access = nv04_graph_fifo_access;
128 engine->graph.load_context = nv10_graph_load_context;
129 engine->graph.unload_context = nv10_graph_unload_context;
Francisco Jerezcb00f7c2009-12-16 12:12:27 +0100130 engine->graph.set_region_tiling = nv10_graph_set_region_tiling;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000131 engine->fifo.channels = 32;
132 engine->fifo.init = nv10_fifo_init;
133 engine->fifo.takedown = nouveau_stub_takedown;
134 engine->fifo.disable = nv04_fifo_disable;
135 engine->fifo.enable = nv04_fifo_enable;
136 engine->fifo.reassign = nv04_fifo_reassign;
Francisco Jerez588d7d12009-12-13 20:07:42 +0100137 engine->fifo.cache_pull = nv04_fifo_cache_pull;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000138 engine->fifo.channel_id = nv10_fifo_channel_id;
139 engine->fifo.create_context = nv10_fifo_create_context;
Francisco Jerez3945e472010-10-18 03:53:39 +0200140 engine->fifo.destroy_context = nv04_fifo_destroy_context;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000141 engine->fifo.load_context = nv10_fifo_load_context;
142 engine->fifo.unload_context = nv10_fifo_unload_context;
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200143 engine->display.early_init = nv04_display_early_init;
144 engine->display.late_takedown = nv04_display_late_takedown;
145 engine->display.create = nv04_display_create;
146 engine->display.init = nv04_display_init;
147 engine->display.destroy = nv04_display_destroy;
Ben Skeggsee2e0132010-07-26 09:28:25 +1000148 engine->gpio.init = nouveau_stub_init;
149 engine->gpio.takedown = nouveau_stub_takedown;
150 engine->gpio.get = nv10_gpio_get;
151 engine->gpio.set = nv10_gpio_set;
152 engine->gpio.irq_enable = NULL;
Ben Skeggs442b6262010-09-16 16:25:26 +1000153 engine->pm.clock_get = nv04_pm_clock_get;
154 engine->pm.clock_pre = nv04_pm_clock_pre;
155 engine->pm.clock_set = nv04_pm_clock_set;
Ben Skeggsbd2e5972010-10-19 20:06:01 +1000156 engine->crypt.init = nouveau_stub_init;
157 engine->crypt.takedown = nouveau_stub_takedown;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000158 break;
159 case 0x20:
160 engine->instmem.init = nv04_instmem_init;
161 engine->instmem.takedown = nv04_instmem_takedown;
162 engine->instmem.suspend = nv04_instmem_suspend;
163 engine->instmem.resume = nv04_instmem_resume;
164 engine->instmem.populate = nv04_instmem_populate;
165 engine->instmem.clear = nv04_instmem_clear;
166 engine->instmem.bind = nv04_instmem_bind;
167 engine->instmem.unbind = nv04_instmem_unbind;
Ben Skeggsf56cb862010-07-08 11:29:10 +1000168 engine->instmem.flush = nv04_instmem_flush;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000169 engine->mc.init = nv04_mc_init;
170 engine->mc.takedown = nv04_mc_takedown;
171 engine->timer.init = nv04_timer_init;
172 engine->timer.read = nv04_timer_read;
173 engine->timer.takedown = nv04_timer_takedown;
174 engine->fb.init = nv10_fb_init;
175 engine->fb.takedown = nv10_fb_takedown;
Francisco Jerezcb00f7c2009-12-16 12:12:27 +0100176 engine->fb.set_region_tiling = nv10_fb_set_region_tiling;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000177 engine->graph.init = nv20_graph_init;
178 engine->graph.takedown = nv20_graph_takedown;
179 engine->graph.channel = nv10_graph_channel;
180 engine->graph.create_context = nv20_graph_create_context;
181 engine->graph.destroy_context = nv20_graph_destroy_context;
182 engine->graph.fifo_access = nv04_graph_fifo_access;
183 engine->graph.load_context = nv20_graph_load_context;
184 engine->graph.unload_context = nv20_graph_unload_context;
Francisco Jerezcb00f7c2009-12-16 12:12:27 +0100185 engine->graph.set_region_tiling = nv20_graph_set_region_tiling;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000186 engine->fifo.channels = 32;
187 engine->fifo.init = nv10_fifo_init;
188 engine->fifo.takedown = nouveau_stub_takedown;
189 engine->fifo.disable = nv04_fifo_disable;
190 engine->fifo.enable = nv04_fifo_enable;
191 engine->fifo.reassign = nv04_fifo_reassign;
Francisco Jerez588d7d12009-12-13 20:07:42 +0100192 engine->fifo.cache_pull = nv04_fifo_cache_pull;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000193 engine->fifo.channel_id = nv10_fifo_channel_id;
194 engine->fifo.create_context = nv10_fifo_create_context;
Francisco Jerez3945e472010-10-18 03:53:39 +0200195 engine->fifo.destroy_context = nv04_fifo_destroy_context;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000196 engine->fifo.load_context = nv10_fifo_load_context;
197 engine->fifo.unload_context = nv10_fifo_unload_context;
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200198 engine->display.early_init = nv04_display_early_init;
199 engine->display.late_takedown = nv04_display_late_takedown;
200 engine->display.create = nv04_display_create;
201 engine->display.init = nv04_display_init;
202 engine->display.destroy = nv04_display_destroy;
Ben Skeggsee2e0132010-07-26 09:28:25 +1000203 engine->gpio.init = nouveau_stub_init;
204 engine->gpio.takedown = nouveau_stub_takedown;
205 engine->gpio.get = nv10_gpio_get;
206 engine->gpio.set = nv10_gpio_set;
207 engine->gpio.irq_enable = NULL;
Ben Skeggs442b6262010-09-16 16:25:26 +1000208 engine->pm.clock_get = nv04_pm_clock_get;
209 engine->pm.clock_pre = nv04_pm_clock_pre;
210 engine->pm.clock_set = nv04_pm_clock_set;
Ben Skeggsbd2e5972010-10-19 20:06:01 +1000211 engine->crypt.init = nouveau_stub_init;
212 engine->crypt.takedown = nouveau_stub_takedown;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000213 break;
214 case 0x30:
215 engine->instmem.init = nv04_instmem_init;
216 engine->instmem.takedown = nv04_instmem_takedown;
217 engine->instmem.suspend = nv04_instmem_suspend;
218 engine->instmem.resume = nv04_instmem_resume;
219 engine->instmem.populate = nv04_instmem_populate;
220 engine->instmem.clear = nv04_instmem_clear;
221 engine->instmem.bind = nv04_instmem_bind;
222 engine->instmem.unbind = nv04_instmem_unbind;
Ben Skeggsf56cb862010-07-08 11:29:10 +1000223 engine->instmem.flush = nv04_instmem_flush;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000224 engine->mc.init = nv04_mc_init;
225 engine->mc.takedown = nv04_mc_takedown;
226 engine->timer.init = nv04_timer_init;
227 engine->timer.read = nv04_timer_read;
228 engine->timer.takedown = nv04_timer_takedown;
Francisco Jerez8bded182010-07-21 21:08:11 +0200229 engine->fb.init = nv30_fb_init;
230 engine->fb.takedown = nv30_fb_takedown;
Francisco Jerezcb00f7c2009-12-16 12:12:27 +0100231 engine->fb.set_region_tiling = nv10_fb_set_region_tiling;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000232 engine->graph.init = nv30_graph_init;
233 engine->graph.takedown = nv20_graph_takedown;
234 engine->graph.fifo_access = nv04_graph_fifo_access;
235 engine->graph.channel = nv10_graph_channel;
236 engine->graph.create_context = nv20_graph_create_context;
237 engine->graph.destroy_context = nv20_graph_destroy_context;
238 engine->graph.load_context = nv20_graph_load_context;
239 engine->graph.unload_context = nv20_graph_unload_context;
Francisco Jerezcb00f7c2009-12-16 12:12:27 +0100240 engine->graph.set_region_tiling = nv20_graph_set_region_tiling;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000241 engine->fifo.channels = 32;
242 engine->fifo.init = nv10_fifo_init;
243 engine->fifo.takedown = nouveau_stub_takedown;
244 engine->fifo.disable = nv04_fifo_disable;
245 engine->fifo.enable = nv04_fifo_enable;
246 engine->fifo.reassign = nv04_fifo_reassign;
Francisco Jerez588d7d12009-12-13 20:07:42 +0100247 engine->fifo.cache_pull = nv04_fifo_cache_pull;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000248 engine->fifo.channel_id = nv10_fifo_channel_id;
249 engine->fifo.create_context = nv10_fifo_create_context;
Francisco Jerez3945e472010-10-18 03:53:39 +0200250 engine->fifo.destroy_context = nv04_fifo_destroy_context;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000251 engine->fifo.load_context = nv10_fifo_load_context;
252 engine->fifo.unload_context = nv10_fifo_unload_context;
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200253 engine->display.early_init = nv04_display_early_init;
254 engine->display.late_takedown = nv04_display_late_takedown;
255 engine->display.create = nv04_display_create;
256 engine->display.init = nv04_display_init;
257 engine->display.destroy = nv04_display_destroy;
Ben Skeggsee2e0132010-07-26 09:28:25 +1000258 engine->gpio.init = nouveau_stub_init;
259 engine->gpio.takedown = nouveau_stub_takedown;
260 engine->gpio.get = nv10_gpio_get;
261 engine->gpio.set = nv10_gpio_set;
262 engine->gpio.irq_enable = NULL;
Ben Skeggs442b6262010-09-16 16:25:26 +1000263 engine->pm.clock_get = nv04_pm_clock_get;
264 engine->pm.clock_pre = nv04_pm_clock_pre;
265 engine->pm.clock_set = nv04_pm_clock_set;
266 engine->pm.voltage_get = nouveau_voltage_gpio_get;
267 engine->pm.voltage_set = nouveau_voltage_gpio_set;
Ben Skeggsbd2e5972010-10-19 20:06:01 +1000268 engine->crypt.init = nouveau_stub_init;
269 engine->crypt.takedown = nouveau_stub_takedown;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000270 break;
271 case 0x40:
272 case 0x60:
273 engine->instmem.init = nv04_instmem_init;
274 engine->instmem.takedown = nv04_instmem_takedown;
275 engine->instmem.suspend = nv04_instmem_suspend;
276 engine->instmem.resume = nv04_instmem_resume;
277 engine->instmem.populate = nv04_instmem_populate;
278 engine->instmem.clear = nv04_instmem_clear;
279 engine->instmem.bind = nv04_instmem_bind;
280 engine->instmem.unbind = nv04_instmem_unbind;
Ben Skeggsf56cb862010-07-08 11:29:10 +1000281 engine->instmem.flush = nv04_instmem_flush;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000282 engine->mc.init = nv40_mc_init;
283 engine->mc.takedown = nv40_mc_takedown;
284 engine->timer.init = nv04_timer_init;
285 engine->timer.read = nv04_timer_read;
286 engine->timer.takedown = nv04_timer_takedown;
287 engine->fb.init = nv40_fb_init;
288 engine->fb.takedown = nv40_fb_takedown;
Francisco Jerezcb00f7c2009-12-16 12:12:27 +0100289 engine->fb.set_region_tiling = nv40_fb_set_region_tiling;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000290 engine->graph.init = nv40_graph_init;
291 engine->graph.takedown = nv40_graph_takedown;
292 engine->graph.fifo_access = nv04_graph_fifo_access;
293 engine->graph.channel = nv40_graph_channel;
294 engine->graph.create_context = nv40_graph_create_context;
295 engine->graph.destroy_context = nv40_graph_destroy_context;
296 engine->graph.load_context = nv40_graph_load_context;
297 engine->graph.unload_context = nv40_graph_unload_context;
Francisco Jerezcb00f7c2009-12-16 12:12:27 +0100298 engine->graph.set_region_tiling = nv40_graph_set_region_tiling;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000299 engine->fifo.channels = 32;
300 engine->fifo.init = nv40_fifo_init;
301 engine->fifo.takedown = nouveau_stub_takedown;
302 engine->fifo.disable = nv04_fifo_disable;
303 engine->fifo.enable = nv04_fifo_enable;
304 engine->fifo.reassign = nv04_fifo_reassign;
Francisco Jerez588d7d12009-12-13 20:07:42 +0100305 engine->fifo.cache_pull = nv04_fifo_cache_pull;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000306 engine->fifo.channel_id = nv10_fifo_channel_id;
307 engine->fifo.create_context = nv40_fifo_create_context;
Francisco Jerez3945e472010-10-18 03:53:39 +0200308 engine->fifo.destroy_context = nv04_fifo_destroy_context;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000309 engine->fifo.load_context = nv40_fifo_load_context;
310 engine->fifo.unload_context = nv40_fifo_unload_context;
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200311 engine->display.early_init = nv04_display_early_init;
312 engine->display.late_takedown = nv04_display_late_takedown;
313 engine->display.create = nv04_display_create;
314 engine->display.init = nv04_display_init;
315 engine->display.destroy = nv04_display_destroy;
Ben Skeggsee2e0132010-07-26 09:28:25 +1000316 engine->gpio.init = nouveau_stub_init;
317 engine->gpio.takedown = nouveau_stub_takedown;
318 engine->gpio.get = nv10_gpio_get;
319 engine->gpio.set = nv10_gpio_set;
320 engine->gpio.irq_enable = NULL;
Ben Skeggs442b6262010-09-16 16:25:26 +1000321 engine->pm.clock_get = nv04_pm_clock_get;
322 engine->pm.clock_pre = nv04_pm_clock_pre;
323 engine->pm.clock_set = nv04_pm_clock_set;
324 engine->pm.voltage_get = nouveau_voltage_gpio_get;
325 engine->pm.voltage_set = nouveau_voltage_gpio_set;
Francisco Jerez8155cac2010-09-23 20:58:38 +0200326 engine->pm.temp_get = nv40_temp_get;
Ben Skeggsbd2e5972010-10-19 20:06:01 +1000327 engine->crypt.init = nouveau_stub_init;
328 engine->crypt.takedown = nouveau_stub_takedown;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000329 break;
330 case 0x50:
331 case 0x80: /* gotta love NVIDIA's consistency.. */
332 case 0x90:
333 case 0xA0:
334 engine->instmem.init = nv50_instmem_init;
335 engine->instmem.takedown = nv50_instmem_takedown;
336 engine->instmem.suspend = nv50_instmem_suspend;
337 engine->instmem.resume = nv50_instmem_resume;
338 engine->instmem.populate = nv50_instmem_populate;
339 engine->instmem.clear = nv50_instmem_clear;
340 engine->instmem.bind = nv50_instmem_bind;
341 engine->instmem.unbind = nv50_instmem_unbind;
Ben Skeggs734ee832010-07-15 11:02:54 +1000342 if (dev_priv->chipset == 0x50)
343 engine->instmem.flush = nv50_instmem_flush;
344 else
345 engine->instmem.flush = nv84_instmem_flush;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000346 engine->mc.init = nv50_mc_init;
347 engine->mc.takedown = nv50_mc_takedown;
348 engine->timer.init = nv04_timer_init;
349 engine->timer.read = nv04_timer_read;
350 engine->timer.takedown = nv04_timer_takedown;
Marcin Koƛcielnicki304424e2010-03-01 00:18:39 +0000351 engine->fb.init = nv50_fb_init;
352 engine->fb.takedown = nv50_fb_takedown;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000353 engine->graph.init = nv50_graph_init;
354 engine->graph.takedown = nv50_graph_takedown;
355 engine->graph.fifo_access = nv50_graph_fifo_access;
356 engine->graph.channel = nv50_graph_channel;
357 engine->graph.create_context = nv50_graph_create_context;
358 engine->graph.destroy_context = nv50_graph_destroy_context;
359 engine->graph.load_context = nv50_graph_load_context;
360 engine->graph.unload_context = nv50_graph_unload_context;
Ben Skeggs56ac7472010-10-22 10:26:24 +1000361 if (dev_priv->chipset != 0x86)
362 engine->graph.tlb_flush = nv50_graph_tlb_flush;
363 else {
364 /* from what i can see nvidia do this on every
365 * pre-NVA3 board except NVAC, but, we've only
366 * ever seen problems on NV86
367 */
368 engine->graph.tlb_flush = nv86_graph_tlb_flush;
369 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000370 engine->fifo.channels = 128;
371 engine->fifo.init = nv50_fifo_init;
372 engine->fifo.takedown = nv50_fifo_takedown;
373 engine->fifo.disable = nv04_fifo_disable;
374 engine->fifo.enable = nv04_fifo_enable;
375 engine->fifo.reassign = nv04_fifo_reassign;
376 engine->fifo.channel_id = nv50_fifo_channel_id;
377 engine->fifo.create_context = nv50_fifo_create_context;
378 engine->fifo.destroy_context = nv50_fifo_destroy_context;
379 engine->fifo.load_context = nv50_fifo_load_context;
380 engine->fifo.unload_context = nv50_fifo_unload_context;
Ben Skeggs56ac7472010-10-22 10:26:24 +1000381 engine->fifo.tlb_flush = nv50_fifo_tlb_flush;
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200382 engine->display.early_init = nv50_display_early_init;
383 engine->display.late_takedown = nv50_display_late_takedown;
384 engine->display.create = nv50_display_create;
385 engine->display.init = nv50_display_init;
386 engine->display.destroy = nv50_display_destroy;
Ben Skeggsee2e0132010-07-26 09:28:25 +1000387 engine->gpio.init = nv50_gpio_init;
388 engine->gpio.takedown = nouveau_stub_takedown;
389 engine->gpio.get = nv50_gpio_get;
390 engine->gpio.set = nv50_gpio_set;
391 engine->gpio.irq_enable = nv50_gpio_irq_enable;
Ben Skeggsfade7ad2010-09-27 11:18:14 +1000392 switch (dev_priv->chipset) {
Ben Skeggsbd2e5972010-10-19 20:06:01 +1000393 case 0x84:
394 case 0x86:
395 case 0x92:
396 case 0x94:
397 case 0x96:
398 case 0x98:
399 case 0xa0:
Ben Skeggs5f801982010-10-22 08:44:09 +1000400 case 0xaa:
401 case 0xac:
Ben Skeggsbd2e5972010-10-19 20:06:01 +1000402 case 0x50:
Ben Skeggsfade7ad2010-09-27 11:18:14 +1000403 engine->pm.clock_get = nv50_pm_clock_get;
404 engine->pm.clock_pre = nv50_pm_clock_pre;
405 engine->pm.clock_set = nv50_pm_clock_set;
406 break;
Ben Skeggsbd2e5972010-10-19 20:06:01 +1000407 default:
408 engine->pm.clock_get = nva3_pm_clock_get;
409 engine->pm.clock_pre = nva3_pm_clock_pre;
410 engine->pm.clock_set = nva3_pm_clock_set;
411 break;
Ben Skeggsfade7ad2010-09-27 11:18:14 +1000412 }
Ben Skeggs02c30ca2010-09-16 16:17:35 +1000413 engine->pm.voltage_get = nouveau_voltage_gpio_get;
414 engine->pm.voltage_set = nouveau_voltage_gpio_set;
Francisco Jerez8155cac2010-09-23 20:58:38 +0200415 if (dev_priv->chipset >= 0x84)
416 engine->pm.temp_get = nv84_temp_get;
417 else
418 engine->pm.temp_get = nv40_temp_get;
Ben Skeggsbd2e5972010-10-19 20:06:01 +1000419 switch (dev_priv->chipset) {
420 case 0x84:
421 case 0x86:
422 case 0x92:
423 case 0x94:
424 case 0x96:
425 case 0xa0:
426 engine->crypt.init = nv84_crypt_init;
427 engine->crypt.takedown = nv84_crypt_fini;
428 engine->crypt.create_context = nv84_crypt_create_context;
429 engine->crypt.destroy_context = nv84_crypt_destroy_context;
430 break;
431 default:
432 engine->crypt.init = nouveau_stub_init;
433 engine->crypt.takedown = nouveau_stub_takedown;
434 break;
435 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000436 break;
Ben Skeggs4b223ee2010-08-03 10:00:56 +1000437 case 0xC0:
438 engine->instmem.init = nvc0_instmem_init;
439 engine->instmem.takedown = nvc0_instmem_takedown;
440 engine->instmem.suspend = nvc0_instmem_suspend;
441 engine->instmem.resume = nvc0_instmem_resume;
442 engine->instmem.populate = nvc0_instmem_populate;
443 engine->instmem.clear = nvc0_instmem_clear;
444 engine->instmem.bind = nvc0_instmem_bind;
445 engine->instmem.unbind = nvc0_instmem_unbind;
446 engine->instmem.flush = nvc0_instmem_flush;
447 engine->mc.init = nv50_mc_init;
448 engine->mc.takedown = nv50_mc_takedown;
449 engine->timer.init = nv04_timer_init;
450 engine->timer.read = nv04_timer_read;
451 engine->timer.takedown = nv04_timer_takedown;
452 engine->fb.init = nvc0_fb_init;
453 engine->fb.takedown = nvc0_fb_takedown;
Ben Skeggs4b223ee2010-08-03 10:00:56 +1000454 engine->graph.init = nvc0_graph_init;
455 engine->graph.takedown = nvc0_graph_takedown;
456 engine->graph.fifo_access = nvc0_graph_fifo_access;
457 engine->graph.channel = nvc0_graph_channel;
458 engine->graph.create_context = nvc0_graph_create_context;
459 engine->graph.destroy_context = nvc0_graph_destroy_context;
460 engine->graph.load_context = nvc0_graph_load_context;
461 engine->graph.unload_context = nvc0_graph_unload_context;
462 engine->fifo.channels = 128;
463 engine->fifo.init = nvc0_fifo_init;
464 engine->fifo.takedown = nvc0_fifo_takedown;
465 engine->fifo.disable = nvc0_fifo_disable;
466 engine->fifo.enable = nvc0_fifo_enable;
467 engine->fifo.reassign = nvc0_fifo_reassign;
468 engine->fifo.channel_id = nvc0_fifo_channel_id;
469 engine->fifo.create_context = nvc0_fifo_create_context;
470 engine->fifo.destroy_context = nvc0_fifo_destroy_context;
471 engine->fifo.load_context = nvc0_fifo_load_context;
472 engine->fifo.unload_context = nvc0_fifo_unload_context;
473 engine->display.early_init = nv50_display_early_init;
474 engine->display.late_takedown = nv50_display_late_takedown;
475 engine->display.create = nv50_display_create;
476 engine->display.init = nv50_display_init;
477 engine->display.destroy = nv50_display_destroy;
478 engine->gpio.init = nv50_gpio_init;
479 engine->gpio.takedown = nouveau_stub_takedown;
480 engine->gpio.get = nv50_gpio_get;
481 engine->gpio.set = nv50_gpio_set;
482 engine->gpio.irq_enable = nv50_gpio_irq_enable;
Ben Skeggsbd2e5972010-10-19 20:06:01 +1000483 engine->crypt.init = nouveau_stub_init;
484 engine->crypt.takedown = nouveau_stub_takedown;
Ben Skeggs4b223ee2010-08-03 10:00:56 +1000485 break;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000486 default:
487 NV_ERROR(dev, "NV%02x unsupported\n", dev_priv->chipset);
488 return 1;
489 }
490
491 return 0;
492}
493
494static unsigned int
495nouveau_vga_set_decode(void *priv, bool state)
496{
Marcin Koƛcielnicki9967b942010-02-08 00:20:17 +0000497 struct drm_device *dev = priv;
498 struct drm_nouveau_private *dev_priv = dev->dev_private;
499
500 if (dev_priv->chipset >= 0x40)
501 nv_wr32(dev, 0x88054, state);
502 else
503 nv_wr32(dev, 0x1854, state);
504
Ben Skeggs6ee73862009-12-11 19:24:15 +1000505 if (state)
506 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
507 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
508 else
509 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
510}
511
Ben Skeggs0735f622009-12-16 14:28:55 +1000512static int
513nouveau_card_init_channel(struct drm_device *dev)
514{
515 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggsa8eaebc2010-09-01 15:24:31 +1000516 struct nouveau_gpuobj *gpuobj = NULL;
Ben Skeggs0735f622009-12-16 14:28:55 +1000517 int ret;
518
519 ret = nouveau_channel_alloc(dev, &dev_priv->channel,
Ben Skeggsa8eaebc2010-09-01 15:24:31 +1000520 (struct drm_file *)-2, NvDmaFB, NvDmaTT);
Ben Skeggs0735f622009-12-16 14:28:55 +1000521 if (ret)
522 return ret;
523
Ben Skeggs0735f622009-12-16 14:28:55 +1000524 ret = nouveau_gpuobj_dma_new(dev_priv->channel, NV_CLASS_DMA_IN_MEMORY,
Ben Skeggsa76fb4e2010-03-18 09:45:20 +1000525 0, dev_priv->vram_size,
Ben Skeggs0735f622009-12-16 14:28:55 +1000526 NV_DMA_ACCESS_RW, NV_DMA_TARGET_VIDMEM,
527 &gpuobj);
528 if (ret)
529 goto out_err;
530
Ben Skeggsa8eaebc2010-09-01 15:24:31 +1000531 ret = nouveau_ramht_insert(dev_priv->channel, NvDmaVRAM, gpuobj);
532 nouveau_gpuobj_ref(NULL, &gpuobj);
Ben Skeggs0735f622009-12-16 14:28:55 +1000533 if (ret)
534 goto out_err;
535
Ben Skeggs0735f622009-12-16 14:28:55 +1000536 ret = nouveau_gpuobj_gart_dma_new(dev_priv->channel, 0,
537 dev_priv->gart_info.aper_size,
538 NV_DMA_ACCESS_RW, &gpuobj, NULL);
539 if (ret)
540 goto out_err;
541
Ben Skeggsa8eaebc2010-09-01 15:24:31 +1000542 ret = nouveau_ramht_insert(dev_priv->channel, NvDmaGART, gpuobj);
543 nouveau_gpuobj_ref(NULL, &gpuobj);
Ben Skeggs0735f622009-12-16 14:28:55 +1000544 if (ret)
545 goto out_err;
546
Ben Skeggscff5c132010-10-06 16:16:59 +1000547 mutex_unlock(&dev_priv->channel->mutex);
Ben Skeggs0735f622009-12-16 14:28:55 +1000548 return 0;
Ben Skeggsa8eaebc2010-09-01 15:24:31 +1000549
Ben Skeggs0735f622009-12-16 14:28:55 +1000550out_err:
Ben Skeggscff5c132010-10-06 16:16:59 +1000551 nouveau_channel_put(&dev_priv->channel);
Ben Skeggs0735f622009-12-16 14:28:55 +1000552 return ret;
553}
554
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000555static void nouveau_switcheroo_set_state(struct pci_dev *pdev,
556 enum vga_switcheroo_state state)
557{
Dave Airliefbf81762010-06-01 09:09:06 +1000558 struct drm_device *dev = pci_get_drvdata(pdev);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000559 pm_message_t pmm = { .event = PM_EVENT_SUSPEND };
560 if (state == VGA_SWITCHEROO_ON) {
561 printk(KERN_ERR "VGA switcheroo: switched nouveau on\n");
562 nouveau_pci_resume(pdev);
Dave Airliefbf81762010-06-01 09:09:06 +1000563 drm_kms_helper_poll_enable(dev);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000564 } else {
565 printk(KERN_ERR "VGA switcheroo: switched nouveau off\n");
Dave Airliefbf81762010-06-01 09:09:06 +1000566 drm_kms_helper_poll_disable(dev);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000567 nouveau_pci_suspend(pdev, pmm);
568 }
569}
570
571static bool nouveau_switcheroo_can_switch(struct pci_dev *pdev)
572{
573 struct drm_device *dev = pci_get_drvdata(pdev);
574 bool can_switch;
575
576 spin_lock(&dev->count_lock);
577 can_switch = (dev->open_count == 0);
578 spin_unlock(&dev->count_lock);
579 return can_switch;
580}
581
Ben Skeggs6ee73862009-12-11 19:24:15 +1000582int
583nouveau_card_init(struct drm_device *dev)
584{
585 struct drm_nouveau_private *dev_priv = dev->dev_private;
586 struct nouveau_engine *engine;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000587 int ret;
588
Ben Skeggs6ee73862009-12-11 19:24:15 +1000589 vga_client_register(dev->pdev, dev, NULL, nouveau_vga_set_decode);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000590 vga_switcheroo_register_client(dev->pdev, nouveau_switcheroo_set_state,
591 nouveau_switcheroo_can_switch);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000592
593 /* Initialise internal driver API hooks */
594 ret = nouveau_init_engine_ptrs(dev);
595 if (ret)
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000596 goto out;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000597 engine = &dev_priv->engine;
Ben Skeggscff5c132010-10-06 16:16:59 +1000598 spin_lock_init(&dev_priv->channels.lock);
Maarten Maathuisff9e5272010-02-01 20:58:27 +0100599 spin_lock_init(&dev_priv->context_switch_lock);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000600
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200601 /* Make the CRTCs and I2C buses accessible */
602 ret = engine->display.early_init(dev);
603 if (ret)
604 goto out;
605
Ben Skeggs6ee73862009-12-11 19:24:15 +1000606 /* Parse BIOS tables / Run init tables if card not POSTed */
Ben Skeggscd0b0722010-06-01 15:56:22 +1000607 ret = nouveau_bios_init(dev);
608 if (ret)
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200609 goto out_display_early;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000610
Ben Skeggs330c5982010-09-16 15:39:49 +1000611 nouveau_pm_init(dev);
612
Ben Skeggsfbd28952010-09-01 15:24:34 +1000613 ret = nouveau_mem_vram_init(dev);
Ben Skeggsa76fb4e2010-03-18 09:45:20 +1000614 if (ret)
615 goto out_bios;
616
Ben Skeggs6ee73862009-12-11 19:24:15 +1000617 ret = nouveau_gpuobj_init(dev);
618 if (ret)
Ben Skeggsfbd28952010-09-01 15:24:34 +1000619 goto out_vram;
620
621 ret = engine->instmem.init(dev);
622 if (ret)
623 goto out_gpuobj;
624
625 ret = nouveau_mem_gart_init(dev);
626 if (ret)
627 goto out_instmem;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000628
629 /* PMC */
630 ret = engine->mc.init(dev);
631 if (ret)
Ben Skeggsfbd28952010-09-01 15:24:34 +1000632 goto out_gart;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000633
Ben Skeggsee2e0132010-07-26 09:28:25 +1000634 /* PGPIO */
635 ret = engine->gpio.init(dev);
636 if (ret)
637 goto out_mc;
638
Ben Skeggs6ee73862009-12-11 19:24:15 +1000639 /* PTIMER */
640 ret = engine->timer.init(dev);
641 if (ret)
Ben Skeggsee2e0132010-07-26 09:28:25 +1000642 goto out_gpio;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000643
644 /* PFB */
645 ret = engine->fb.init(dev);
646 if (ret)
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000647 goto out_timer;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000648
Marcin Koƛcielnickia32ed692010-01-26 14:00:42 +0000649 if (nouveau_noaccel)
650 engine->graph.accel_blocked = true;
651 else {
652 /* PGRAPH */
653 ret = engine->graph.init(dev);
654 if (ret)
655 goto out_fb;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000656
Ben Skeggsbd2e5972010-10-19 20:06:01 +1000657 /* PCRYPT */
658 ret = engine->crypt.init(dev);
659 if (ret)
660 goto out_graph;
661
Marcin Koƛcielnickia32ed692010-01-26 14:00:42 +0000662 /* PFIFO */
663 ret = engine->fifo.init(dev);
664 if (ret)
Ben Skeggsbd2e5972010-10-19 20:06:01 +1000665 goto out_crypt;
Marcin Koƛcielnickia32ed692010-01-26 14:00:42 +0000666 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000667
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200668 ret = engine->display.create(dev);
Ben Skeggse88efe02010-07-09 10:56:08 +1000669 if (ret)
670 goto out_fifo;
671
Ben Skeggs35fa2f22010-10-21 14:07:03 +1000672 ret = nouveau_irq_init(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000673 if (ret)
Ben Skeggse88efe02010-07-09 10:56:08 +1000674 goto out_display;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000675
676 ret = drm_vblank_init(dev, 0);
677 if (ret)
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000678 goto out_irq;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000679
680 /* what about PVIDEO/PCRTC/PRAMDAC etc? */
681
Ben Skeggs0735f622009-12-16 14:28:55 +1000682 if (!engine->graph.accel_blocked) {
Francisco Jerez0c6c1c22010-09-22 00:58:54 +0200683 ret = nouveau_fence_init(dev);
Ben Skeggs0735f622009-12-16 14:28:55 +1000684 if (ret)
685 goto out_irq;
Francisco Jerez0c6c1c22010-09-22 00:58:54 +0200686
687 ret = nouveau_card_init_channel(dev);
688 if (ret)
689 goto out_fence;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000690 }
691
Ben Skeggs6ee73862009-12-11 19:24:15 +1000692 ret = nouveau_backlight_init(dev);
693 if (ret)
694 NV_ERROR(dev, "Error %d registering backlight\n", ret);
695
Ben Skeggscd0b0722010-06-01 15:56:22 +1000696 nouveau_fbcon_init(dev);
697 drm_kms_helper_poll_init(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000698 return 0;
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000699
Francisco Jerez0c6c1c22010-09-22 00:58:54 +0200700out_fence:
701 nouveau_fence_fini(dev);
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000702out_irq:
Ben Skeggs35fa2f22010-10-21 14:07:03 +1000703 nouveau_irq_fini(dev);
Ben Skeggse88efe02010-07-09 10:56:08 +1000704out_display:
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200705 engine->display.destroy(dev);
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000706out_fifo:
Marcin Koƛcielnickia32ed692010-01-26 14:00:42 +0000707 if (!nouveau_noaccel)
708 engine->fifo.takedown(dev);
Ben Skeggsbd2e5972010-10-19 20:06:01 +1000709out_crypt:
710 if (!nouveau_noaccel)
711 engine->crypt.takedown(dev);
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000712out_graph:
Marcin Koƛcielnickia32ed692010-01-26 14:00:42 +0000713 if (!nouveau_noaccel)
714 engine->graph.takedown(dev);
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000715out_fb:
716 engine->fb.takedown(dev);
717out_timer:
718 engine->timer.takedown(dev);
Ben Skeggsee2e0132010-07-26 09:28:25 +1000719out_gpio:
720 engine->gpio.takedown(dev);
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000721out_mc:
722 engine->mc.takedown(dev);
Ben Skeggsfbd28952010-09-01 15:24:34 +1000723out_gart:
724 nouveau_mem_gart_fini(dev);
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000725out_instmem:
726 engine->instmem.takedown(dev);
Ben Skeggsfbd28952010-09-01 15:24:34 +1000727out_gpuobj:
728 nouveau_gpuobj_takedown(dev);
729out_vram:
730 nouveau_mem_vram_fini(dev);
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000731out_bios:
Ben Skeggs330c5982010-09-16 15:39:49 +1000732 nouveau_pm_fini(dev);
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000733 nouveau_bios_takedown(dev);
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200734out_display_early:
735 engine->display.late_takedown(dev);
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000736out:
737 vga_client_register(dev->pdev, NULL, NULL, NULL);
738 return ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000739}
740
741static void nouveau_card_takedown(struct drm_device *dev)
742{
743 struct drm_nouveau_private *dev_priv = dev->dev_private;
744 struct nouveau_engine *engine = &dev_priv->engine;
745
Ben Skeggsb6d3d872010-06-07 15:38:27 +1000746 nouveau_backlight_exit(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000747
Francisco Jerez0c6c1c22010-09-22 00:58:54 +0200748 if (!engine->graph.accel_blocked) {
749 nouveau_fence_fini(dev);
Francisco Jerez36c952e2010-10-18 03:01:34 +0200750 nouveau_channel_put_unlocked(&dev_priv->channel);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000751 }
Ben Skeggsb6d3d872010-06-07 15:38:27 +1000752
753 if (!nouveau_noaccel) {
754 engine->fifo.takedown(dev);
Ben Skeggsbd2e5972010-10-19 20:06:01 +1000755 engine->crypt.takedown(dev);
Ben Skeggsb6d3d872010-06-07 15:38:27 +1000756 engine->graph.takedown(dev);
757 }
758 engine->fb.takedown(dev);
759 engine->timer.takedown(dev);
Ben Skeggsee2e0132010-07-26 09:28:25 +1000760 engine->gpio.takedown(dev);
Ben Skeggsb6d3d872010-06-07 15:38:27 +1000761 engine->mc.takedown(dev);
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200762 engine->display.late_takedown(dev);
Ben Skeggsb6d3d872010-06-07 15:38:27 +1000763
764 mutex_lock(&dev->struct_mutex);
765 ttm_bo_clean_mm(&dev_priv->ttm.bdev, TTM_PL_VRAM);
766 ttm_bo_clean_mm(&dev_priv->ttm.bdev, TTM_PL_TT);
767 mutex_unlock(&dev->struct_mutex);
Ben Skeggsfbd28952010-09-01 15:24:34 +1000768 nouveau_mem_gart_fini(dev);
Ben Skeggsb6d3d872010-06-07 15:38:27 +1000769
Ben Skeggsb6d3d872010-06-07 15:38:27 +1000770 engine->instmem.takedown(dev);
Ben Skeggsfbd28952010-09-01 15:24:34 +1000771 nouveau_gpuobj_takedown(dev);
772 nouveau_mem_vram_fini(dev);
Ben Skeggsb6d3d872010-06-07 15:38:27 +1000773
Ben Skeggs35fa2f22010-10-21 14:07:03 +1000774 nouveau_irq_fini(dev);
Ben Skeggsb6d3d872010-06-07 15:38:27 +1000775
Ben Skeggs330c5982010-09-16 15:39:49 +1000776 nouveau_pm_fini(dev);
Ben Skeggsb6d3d872010-06-07 15:38:27 +1000777 nouveau_bios_takedown(dev);
778
779 vga_client_register(dev->pdev, NULL, NULL, NULL);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000780}
781
782/* here a client dies, release the stuff that was allocated for its
783 * file_priv */
784void nouveau_preclose(struct drm_device *dev, struct drm_file *file_priv)
785{
786 nouveau_channel_cleanup(dev, file_priv);
787}
788
789/* first module load, setup the mmio/fb mapping */
790/* KMS: we need mmio at load time, not when the first drm client opens. */
791int nouveau_firstopen(struct drm_device *dev)
792{
793 return 0;
794}
795
796/* if we have an OF card, copy vbios to RAMIN */
797static void nouveau_OF_copy_vbios_to_ramin(struct drm_device *dev)
798{
799#if defined(__powerpc__)
800 int size, i;
801 const uint32_t *bios;
802 struct device_node *dn = pci_device_to_OF_node(dev->pdev);
803 if (!dn) {
804 NV_INFO(dev, "Unable to get the OF node\n");
805 return;
806 }
807
808 bios = of_get_property(dn, "NVDA,BMP", &size);
809 if (bios) {
810 for (i = 0; i < size; i += 4)
811 nv_wi32(dev, i, bios[i/4]);
812 NV_INFO(dev, "OF bios successfully copied (%d bytes)\n", size);
813 } else {
814 NV_INFO(dev, "Unable to get the OF bios\n");
815 }
816#endif
817}
818
Marcin Slusarz06415c52010-05-16 17:29:56 +0200819static struct apertures_struct *nouveau_get_apertures(struct drm_device *dev)
820{
821 struct pci_dev *pdev = dev->pdev;
822 struct apertures_struct *aper = alloc_apertures(3);
823 if (!aper)
824 return NULL;
825
826 aper->ranges[0].base = pci_resource_start(pdev, 1);
827 aper->ranges[0].size = pci_resource_len(pdev, 1);
828 aper->count = 1;
829
830 if (pci_resource_len(pdev, 2)) {
831 aper->ranges[aper->count].base = pci_resource_start(pdev, 2);
832 aper->ranges[aper->count].size = pci_resource_len(pdev, 2);
833 aper->count++;
834 }
835
836 if (pci_resource_len(pdev, 3)) {
837 aper->ranges[aper->count].base = pci_resource_start(pdev, 3);
838 aper->ranges[aper->count].size = pci_resource_len(pdev, 3);
839 aper->count++;
840 }
841
842 return aper;
843}
844
845static int nouveau_remove_conflicting_drivers(struct drm_device *dev)
846{
847 struct drm_nouveau_private *dev_priv = dev->dev_private;
Marcin Slusarz3b9676e2010-05-16 17:33:09 +0200848 bool primary = false;
Marcin Slusarz06415c52010-05-16 17:29:56 +0200849 dev_priv->apertures = nouveau_get_apertures(dev);
850 if (!dev_priv->apertures)
851 return -ENOMEM;
852
Marcin Slusarz3b9676e2010-05-16 17:33:09 +0200853#ifdef CONFIG_X86
854 primary = dev->pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW;
855#endif
856
857 remove_conflicting_framebuffers(dev_priv->apertures, "nouveaufb", primary);
Marcin Slusarz06415c52010-05-16 17:29:56 +0200858 return 0;
859}
860
Ben Skeggs6ee73862009-12-11 19:24:15 +1000861int nouveau_load(struct drm_device *dev, unsigned long flags)
862{
863 struct drm_nouveau_private *dev_priv;
864 uint32_t reg0;
865 resource_size_t mmio_start_offs;
Ben Skeggscd0b0722010-06-01 15:56:22 +1000866 int ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000867
868 dev_priv = kzalloc(sizeof(*dev_priv), GFP_KERNEL);
Dan Carpentera0d069e2010-07-30 17:04:32 +0200869 if (!dev_priv) {
870 ret = -ENOMEM;
871 goto err_out;
872 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000873 dev->dev_private = dev_priv;
874 dev_priv->dev = dev;
875
876 dev_priv->flags = flags & NOUVEAU_FLAGS;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000877
878 NV_DEBUG(dev, "vendor: 0x%X device: 0x%X class: 0x%X\n",
879 dev->pci_vendor, dev->pci_device, dev->pdev->class);
880
Ben Skeggs6ee73862009-12-11 19:24:15 +1000881 dev_priv->wq = create_workqueue("nouveau");
Dan Carpentera0d069e2010-07-30 17:04:32 +0200882 if (!dev_priv->wq) {
883 ret = -EINVAL;
884 goto err_priv;
885 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000886
887 /* resource 0 is mmio regs */
888 /* resource 1 is linear FB */
889 /* resource 2 is RAMIN (mmio regs + 0x1000000) */
890 /* resource 6 is bios */
891
892 /* map the mmio regs */
893 mmio_start_offs = pci_resource_start(dev->pdev, 0);
894 dev_priv->mmio = ioremap(mmio_start_offs, 0x00800000);
895 if (!dev_priv->mmio) {
896 NV_ERROR(dev, "Unable to initialize the mmio mapping. "
897 "Please report your setup to " DRIVER_EMAIL "\n");
Dan Carpentera0d069e2010-07-30 17:04:32 +0200898 ret = -EINVAL;
899 goto err_wq;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000900 }
901 NV_DEBUG(dev, "regs mapped ok at 0x%llx\n",
902 (unsigned long long)mmio_start_offs);
903
904#ifdef __BIG_ENDIAN
905 /* Put the card in BE mode if it's not */
906 if (nv_rd32(dev, NV03_PMC_BOOT_1))
907 nv_wr32(dev, NV03_PMC_BOOT_1, 0x00000001);
908
909 DRM_MEMORYBARRIER();
910#endif
911
912 /* Time to determine the card architecture */
913 reg0 = nv_rd32(dev, NV03_PMC_BOOT_0);
914
915 /* We're dealing with >=NV10 */
916 if ((reg0 & 0x0f000000) > 0) {
917 /* Bit 27-20 contain the architecture in hex */
918 dev_priv->chipset = (reg0 & 0xff00000) >> 20;
919 /* NV04 or NV05 */
920 } else if ((reg0 & 0xff00fff0) == 0x20004000) {
Ben Skeggs1dee7a92010-01-07 13:47:57 +1000921 if (reg0 & 0x00f00000)
922 dev_priv->chipset = 0x05;
923 else
924 dev_priv->chipset = 0x04;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000925 } else
926 dev_priv->chipset = 0xff;
927
928 switch (dev_priv->chipset & 0xf0) {
929 case 0x00:
930 case 0x10:
931 case 0x20:
932 case 0x30:
933 dev_priv->card_type = dev_priv->chipset & 0xf0;
934 break;
935 case 0x40:
936 case 0x60:
937 dev_priv->card_type = NV_40;
938 break;
939 case 0x50:
940 case 0x80:
941 case 0x90:
942 case 0xa0:
943 dev_priv->card_type = NV_50;
944 break;
Ben Skeggs4b223ee2010-08-03 10:00:56 +1000945 case 0xc0:
946 dev_priv->card_type = NV_C0;
947 break;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000948 default:
949 NV_INFO(dev, "Unsupported chipset 0x%08x\n", reg0);
Dan Carpentera0d069e2010-07-30 17:04:32 +0200950 ret = -EINVAL;
951 goto err_mmio;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000952 }
953
954 NV_INFO(dev, "Detected an NV%2x generation card (0x%08x)\n",
955 dev_priv->card_type, reg0);
956
Ben Skeggscd0b0722010-06-01 15:56:22 +1000957 ret = nouveau_remove_conflicting_drivers(dev);
958 if (ret)
Dan Carpentera0d069e2010-07-30 17:04:32 +0200959 goto err_mmio;
Marcin Slusarz06415c52010-05-16 17:29:56 +0200960
Ben Skeggs6d696302010-06-02 10:16:24 +1000961 /* Map PRAMIN BAR, or on older cards, the aperture withing BAR0 */
Ben Skeggs6ee73862009-12-11 19:24:15 +1000962 if (dev_priv->card_type >= NV_40) {
963 int ramin_bar = 2;
964 if (pci_resource_len(dev->pdev, ramin_bar) == 0)
965 ramin_bar = 3;
966
967 dev_priv->ramin_size = pci_resource_len(dev->pdev, ramin_bar);
Ben Skeggs6d696302010-06-02 10:16:24 +1000968 dev_priv->ramin =
969 ioremap(pci_resource_start(dev->pdev, ramin_bar),
Ben Skeggs6ee73862009-12-11 19:24:15 +1000970 dev_priv->ramin_size);
971 if (!dev_priv->ramin) {
Ben Skeggs6d696302010-06-02 10:16:24 +1000972 NV_ERROR(dev, "Failed to PRAMIN BAR");
Dan Carpentera0d069e2010-07-30 17:04:32 +0200973 ret = -ENOMEM;
974 goto err_mmio;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000975 }
Ben Skeggs6d696302010-06-02 10:16:24 +1000976 } else {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000977 dev_priv->ramin_size = 1 * 1024 * 1024;
978 dev_priv->ramin = ioremap(mmio_start_offs + NV_RAMIN,
Ben Skeggs6d696302010-06-02 10:16:24 +1000979 dev_priv->ramin_size);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000980 if (!dev_priv->ramin) {
981 NV_ERROR(dev, "Failed to map BAR0 PRAMIN.\n");
Dan Carpentera0d069e2010-07-30 17:04:32 +0200982 ret = -ENOMEM;
983 goto err_mmio;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000984 }
985 }
986
987 nouveau_OF_copy_vbios_to_ramin(dev);
988
989 /* Special flags */
990 if (dev->pci_device == 0x01a0)
991 dev_priv->flags |= NV_NFORCE;
992 else if (dev->pci_device == 0x01f0)
993 dev_priv->flags |= NV_NFORCE2;
994
995 /* For kernel modesetting, init card now and bring up fbcon */
Ben Skeggscd0b0722010-06-01 15:56:22 +1000996 ret = nouveau_card_init(dev);
997 if (ret)
Dan Carpentera0d069e2010-07-30 17:04:32 +0200998 goto err_ramin;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000999
1000 return 0;
Dan Carpentera0d069e2010-07-30 17:04:32 +02001001
1002err_ramin:
1003 iounmap(dev_priv->ramin);
1004err_mmio:
1005 iounmap(dev_priv->mmio);
1006err_wq:
1007 destroy_workqueue(dev_priv->wq);
1008err_priv:
1009 kfree(dev_priv);
1010 dev->dev_private = NULL;
1011err_out:
1012 return ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001013}
1014
Ben Skeggs6ee73862009-12-11 19:24:15 +10001015void nouveau_lastclose(struct drm_device *dev)
1016{
Ben Skeggs6ee73862009-12-11 19:24:15 +10001017}
1018
1019int nouveau_unload(struct drm_device *dev)
1020{
1021 struct drm_nouveau_private *dev_priv = dev->dev_private;
Francisco Jerezc88c2e02010-07-24 17:37:33 +02001022 struct nouveau_engine *engine = &dev_priv->engine;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001023
Ben Skeggscd0b0722010-06-01 15:56:22 +10001024 drm_kms_helper_poll_fini(dev);
1025 nouveau_fbcon_fini(dev);
Francisco Jerezc88c2e02010-07-24 17:37:33 +02001026 engine->display.destroy(dev);
Ben Skeggscd0b0722010-06-01 15:56:22 +10001027 nouveau_card_takedown(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +10001028
1029 iounmap(dev_priv->mmio);
1030 iounmap(dev_priv->ramin);
1031
1032 kfree(dev_priv);
1033 dev->dev_private = NULL;
1034 return 0;
1035}
1036
Ben Skeggs6ee73862009-12-11 19:24:15 +10001037int nouveau_ioctl_getparam(struct drm_device *dev, void *data,
1038 struct drm_file *file_priv)
1039{
1040 struct drm_nouveau_private *dev_priv = dev->dev_private;
1041 struct drm_nouveau_getparam *getparam = data;
1042
Ben Skeggs6ee73862009-12-11 19:24:15 +10001043 switch (getparam->param) {
1044 case NOUVEAU_GETPARAM_CHIPSET_ID:
1045 getparam->value = dev_priv->chipset;
1046 break;
1047 case NOUVEAU_GETPARAM_PCI_VENDOR:
1048 getparam->value = dev->pci_vendor;
1049 break;
1050 case NOUVEAU_GETPARAM_PCI_DEVICE:
1051 getparam->value = dev->pci_device;
1052 break;
1053 case NOUVEAU_GETPARAM_BUS_TYPE:
1054 if (drm_device_is_agp(dev))
1055 getparam->value = NV_AGP;
1056 else if (drm_device_is_pcie(dev))
1057 getparam->value = NV_PCIE;
1058 else
1059 getparam->value = NV_PCI;
1060 break;
1061 case NOUVEAU_GETPARAM_FB_PHYSICAL:
1062 getparam->value = dev_priv->fb_phys;
1063 break;
1064 case NOUVEAU_GETPARAM_AGP_PHYSICAL:
1065 getparam->value = dev_priv->gart_info.aper_base;
1066 break;
1067 case NOUVEAU_GETPARAM_PCI_PHYSICAL:
1068 if (dev->sg) {
1069 getparam->value = (unsigned long)dev->sg->virtual;
1070 } else {
1071 NV_ERROR(dev, "Requested PCIGART address, "
1072 "while no PCIGART was created\n");
1073 return -EINVAL;
1074 }
1075 break;
1076 case NOUVEAU_GETPARAM_FB_SIZE:
1077 getparam->value = dev_priv->fb_available_size;
1078 break;
1079 case NOUVEAU_GETPARAM_AGP_SIZE:
1080 getparam->value = dev_priv->gart_info.aper_size;
1081 break;
1082 case NOUVEAU_GETPARAM_VM_VRAM_BASE:
1083 getparam->value = dev_priv->vm_vram_base;
1084 break;
Marcin Koƛcielnicki7fc74f12010-05-23 11:36:04 +00001085 case NOUVEAU_GETPARAM_PTIMER_TIME:
1086 getparam->value = dev_priv->engine.timer.read(dev);
1087 break;
Francisco Jerezf13b3262010-10-10 06:01:08 +02001088 case NOUVEAU_GETPARAM_HAS_BO_USAGE:
1089 getparam->value = 1;
1090 break;
Marcin Koƛcielnicki69c97002010-01-26 18:39:20 +00001091 case NOUVEAU_GETPARAM_GRAPH_UNITS:
1092 /* NV40 and NV50 versions are quite different, but register
1093 * address is the same. User is supposed to know the card
1094 * family anyway... */
1095 if (dev_priv->chipset >= 0x40) {
1096 getparam->value = nv_rd32(dev, NV40_PMC_GRAPH_UNITS);
1097 break;
1098 }
1099 /* FALLTHRU */
Ben Skeggs6ee73862009-12-11 19:24:15 +10001100 default:
Francisco Jerez1397b422010-10-12 03:17:43 +02001101 NV_DEBUG(dev, "unknown parameter %lld\n", getparam->param);
Ben Skeggs6ee73862009-12-11 19:24:15 +10001102 return -EINVAL;
1103 }
1104
1105 return 0;
1106}
1107
1108int
1109nouveau_ioctl_setparam(struct drm_device *dev, void *data,
1110 struct drm_file *file_priv)
1111{
1112 struct drm_nouveau_setparam *setparam = data;
1113
Ben Skeggs6ee73862009-12-11 19:24:15 +10001114 switch (setparam->param) {
1115 default:
Francisco Jerez1397b422010-10-12 03:17:43 +02001116 NV_DEBUG(dev, "unknown parameter %lld\n", setparam->param);
Ben Skeggs6ee73862009-12-11 19:24:15 +10001117 return -EINVAL;
1118 }
1119
1120 return 0;
1121}
1122
1123/* Wait until (value(reg) & mask) == val, up until timeout has hit */
1124bool nouveau_wait_until(struct drm_device *dev, uint64_t timeout,
1125 uint32_t reg, uint32_t mask, uint32_t val)
1126{
1127 struct drm_nouveau_private *dev_priv = dev->dev_private;
1128 struct nouveau_timer_engine *ptimer = &dev_priv->engine.timer;
1129 uint64_t start = ptimer->read(dev);
1130
1131 do {
1132 if ((nv_rd32(dev, reg) & mask) == val)
1133 return true;
1134 } while (ptimer->read(dev) - start < timeout);
1135
1136 return false;
1137}
1138
1139/* Waits for PGRAPH to go completely idle */
1140bool nouveau_wait_for_idle(struct drm_device *dev)
1141{
Francisco Jerez0541324a2010-10-18 16:15:15 +02001142 struct drm_nouveau_private *dev_priv = dev->dev_private;
1143 uint32_t mask = ~0;
1144
1145 if (dev_priv->card_type == NV_40)
1146 mask &= ~NV40_PGRAPH_STATUS_SYNC_STALL;
1147
1148 if (!nv_wait(dev, NV04_PGRAPH_STATUS, mask, 0)) {
Ben Skeggs6ee73862009-12-11 19:24:15 +10001149 NV_ERROR(dev, "PGRAPH idle timed out with status 0x%08x\n",
1150 nv_rd32(dev, NV04_PGRAPH_STATUS));
1151 return false;
1152 }
1153
1154 return true;
1155}
1156