blob: 84bff459491e06559b1cff149410ecbd8fd40bb6 [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 Jereza5cf68b2010-10-24 16:14:41 +0200121 engine->fb.init_tile_region = nv10_fb_init_tile_region;
122 engine->fb.set_tile_region = nv10_fb_set_tile_region;
123 engine->fb.free_tile_region = nv10_fb_free_tile_region;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000124 engine->graph.init = nv10_graph_init;
125 engine->graph.takedown = nv10_graph_takedown;
126 engine->graph.channel = nv10_graph_channel;
127 engine->graph.create_context = nv10_graph_create_context;
128 engine->graph.destroy_context = nv10_graph_destroy_context;
129 engine->graph.fifo_access = nv04_graph_fifo_access;
130 engine->graph.load_context = nv10_graph_load_context;
131 engine->graph.unload_context = nv10_graph_unload_context;
Francisco Jereza5cf68b2010-10-24 16:14:41 +0200132 engine->graph.set_tile_region = nv10_graph_set_tile_region;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000133 engine->fifo.channels = 32;
134 engine->fifo.init = nv10_fifo_init;
135 engine->fifo.takedown = nouveau_stub_takedown;
136 engine->fifo.disable = nv04_fifo_disable;
137 engine->fifo.enable = nv04_fifo_enable;
138 engine->fifo.reassign = nv04_fifo_reassign;
Francisco Jerez588d7d12009-12-13 20:07:42 +0100139 engine->fifo.cache_pull = nv04_fifo_cache_pull;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000140 engine->fifo.channel_id = nv10_fifo_channel_id;
141 engine->fifo.create_context = nv10_fifo_create_context;
Francisco Jerez3945e472010-10-18 03:53:39 +0200142 engine->fifo.destroy_context = nv04_fifo_destroy_context;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000143 engine->fifo.load_context = nv10_fifo_load_context;
144 engine->fifo.unload_context = nv10_fifo_unload_context;
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200145 engine->display.early_init = nv04_display_early_init;
146 engine->display.late_takedown = nv04_display_late_takedown;
147 engine->display.create = nv04_display_create;
148 engine->display.init = nv04_display_init;
149 engine->display.destroy = nv04_display_destroy;
Ben Skeggsee2e0132010-07-26 09:28:25 +1000150 engine->gpio.init = nouveau_stub_init;
151 engine->gpio.takedown = nouveau_stub_takedown;
152 engine->gpio.get = nv10_gpio_get;
153 engine->gpio.set = nv10_gpio_set;
154 engine->gpio.irq_enable = NULL;
Ben Skeggs442b6262010-09-16 16:25:26 +1000155 engine->pm.clock_get = nv04_pm_clock_get;
156 engine->pm.clock_pre = nv04_pm_clock_pre;
157 engine->pm.clock_set = nv04_pm_clock_set;
Ben Skeggsbd2e5972010-10-19 20:06:01 +1000158 engine->crypt.init = nouveau_stub_init;
159 engine->crypt.takedown = nouveau_stub_takedown;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000160 break;
161 case 0x20:
162 engine->instmem.init = nv04_instmem_init;
163 engine->instmem.takedown = nv04_instmem_takedown;
164 engine->instmem.suspend = nv04_instmem_suspend;
165 engine->instmem.resume = nv04_instmem_resume;
166 engine->instmem.populate = nv04_instmem_populate;
167 engine->instmem.clear = nv04_instmem_clear;
168 engine->instmem.bind = nv04_instmem_bind;
169 engine->instmem.unbind = nv04_instmem_unbind;
Ben Skeggsf56cb862010-07-08 11:29:10 +1000170 engine->instmem.flush = nv04_instmem_flush;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000171 engine->mc.init = nv04_mc_init;
172 engine->mc.takedown = nv04_mc_takedown;
173 engine->timer.init = nv04_timer_init;
174 engine->timer.read = nv04_timer_read;
175 engine->timer.takedown = nv04_timer_takedown;
176 engine->fb.init = nv10_fb_init;
177 engine->fb.takedown = nv10_fb_takedown;
Francisco Jereza5cf68b2010-10-24 16:14:41 +0200178 engine->fb.init_tile_region = nv10_fb_init_tile_region;
179 engine->fb.set_tile_region = nv10_fb_set_tile_region;
180 engine->fb.free_tile_region = nv10_fb_free_tile_region;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000181 engine->graph.init = nv20_graph_init;
182 engine->graph.takedown = nv20_graph_takedown;
183 engine->graph.channel = nv10_graph_channel;
184 engine->graph.create_context = nv20_graph_create_context;
185 engine->graph.destroy_context = nv20_graph_destroy_context;
186 engine->graph.fifo_access = nv04_graph_fifo_access;
187 engine->graph.load_context = nv20_graph_load_context;
188 engine->graph.unload_context = nv20_graph_unload_context;
Francisco Jereza5cf68b2010-10-24 16:14:41 +0200189 engine->graph.set_tile_region = nv20_graph_set_tile_region;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000190 engine->fifo.channels = 32;
191 engine->fifo.init = nv10_fifo_init;
192 engine->fifo.takedown = nouveau_stub_takedown;
193 engine->fifo.disable = nv04_fifo_disable;
194 engine->fifo.enable = nv04_fifo_enable;
195 engine->fifo.reassign = nv04_fifo_reassign;
Francisco Jerez588d7d12009-12-13 20:07:42 +0100196 engine->fifo.cache_pull = nv04_fifo_cache_pull;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000197 engine->fifo.channel_id = nv10_fifo_channel_id;
198 engine->fifo.create_context = nv10_fifo_create_context;
Francisco Jerez3945e472010-10-18 03:53:39 +0200199 engine->fifo.destroy_context = nv04_fifo_destroy_context;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000200 engine->fifo.load_context = nv10_fifo_load_context;
201 engine->fifo.unload_context = nv10_fifo_unload_context;
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200202 engine->display.early_init = nv04_display_early_init;
203 engine->display.late_takedown = nv04_display_late_takedown;
204 engine->display.create = nv04_display_create;
205 engine->display.init = nv04_display_init;
206 engine->display.destroy = nv04_display_destroy;
Ben Skeggsee2e0132010-07-26 09:28:25 +1000207 engine->gpio.init = nouveau_stub_init;
208 engine->gpio.takedown = nouveau_stub_takedown;
209 engine->gpio.get = nv10_gpio_get;
210 engine->gpio.set = nv10_gpio_set;
211 engine->gpio.irq_enable = NULL;
Ben Skeggs442b6262010-09-16 16:25:26 +1000212 engine->pm.clock_get = nv04_pm_clock_get;
213 engine->pm.clock_pre = nv04_pm_clock_pre;
214 engine->pm.clock_set = nv04_pm_clock_set;
Ben Skeggsbd2e5972010-10-19 20:06:01 +1000215 engine->crypt.init = nouveau_stub_init;
216 engine->crypt.takedown = nouveau_stub_takedown;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000217 break;
218 case 0x30:
219 engine->instmem.init = nv04_instmem_init;
220 engine->instmem.takedown = nv04_instmem_takedown;
221 engine->instmem.suspend = nv04_instmem_suspend;
222 engine->instmem.resume = nv04_instmem_resume;
223 engine->instmem.populate = nv04_instmem_populate;
224 engine->instmem.clear = nv04_instmem_clear;
225 engine->instmem.bind = nv04_instmem_bind;
226 engine->instmem.unbind = nv04_instmem_unbind;
Ben Skeggsf56cb862010-07-08 11:29:10 +1000227 engine->instmem.flush = nv04_instmem_flush;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000228 engine->mc.init = nv04_mc_init;
229 engine->mc.takedown = nv04_mc_takedown;
230 engine->timer.init = nv04_timer_init;
231 engine->timer.read = nv04_timer_read;
232 engine->timer.takedown = nv04_timer_takedown;
Francisco Jerez8bded182010-07-21 21:08:11 +0200233 engine->fb.init = nv30_fb_init;
234 engine->fb.takedown = nv30_fb_takedown;
Francisco Jereza5cf68b2010-10-24 16:14:41 +0200235 engine->fb.init_tile_region = nv30_fb_init_tile_region;
236 engine->fb.set_tile_region = nv10_fb_set_tile_region;
237 engine->fb.free_tile_region = nv30_fb_free_tile_region;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000238 engine->graph.init = nv30_graph_init;
239 engine->graph.takedown = nv20_graph_takedown;
240 engine->graph.fifo_access = nv04_graph_fifo_access;
241 engine->graph.channel = nv10_graph_channel;
242 engine->graph.create_context = nv20_graph_create_context;
243 engine->graph.destroy_context = nv20_graph_destroy_context;
244 engine->graph.load_context = nv20_graph_load_context;
245 engine->graph.unload_context = nv20_graph_unload_context;
Francisco Jereza5cf68b2010-10-24 16:14:41 +0200246 engine->graph.set_tile_region = nv20_graph_set_tile_region;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000247 engine->fifo.channels = 32;
248 engine->fifo.init = nv10_fifo_init;
249 engine->fifo.takedown = nouveau_stub_takedown;
250 engine->fifo.disable = nv04_fifo_disable;
251 engine->fifo.enable = nv04_fifo_enable;
252 engine->fifo.reassign = nv04_fifo_reassign;
Francisco Jerez588d7d12009-12-13 20:07:42 +0100253 engine->fifo.cache_pull = nv04_fifo_cache_pull;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000254 engine->fifo.channel_id = nv10_fifo_channel_id;
255 engine->fifo.create_context = nv10_fifo_create_context;
Francisco Jerez3945e472010-10-18 03:53:39 +0200256 engine->fifo.destroy_context = nv04_fifo_destroy_context;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000257 engine->fifo.load_context = nv10_fifo_load_context;
258 engine->fifo.unload_context = nv10_fifo_unload_context;
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200259 engine->display.early_init = nv04_display_early_init;
260 engine->display.late_takedown = nv04_display_late_takedown;
261 engine->display.create = nv04_display_create;
262 engine->display.init = nv04_display_init;
263 engine->display.destroy = nv04_display_destroy;
Ben Skeggsee2e0132010-07-26 09:28:25 +1000264 engine->gpio.init = nouveau_stub_init;
265 engine->gpio.takedown = nouveau_stub_takedown;
266 engine->gpio.get = nv10_gpio_get;
267 engine->gpio.set = nv10_gpio_set;
268 engine->gpio.irq_enable = NULL;
Ben Skeggs442b6262010-09-16 16:25:26 +1000269 engine->pm.clock_get = nv04_pm_clock_get;
270 engine->pm.clock_pre = nv04_pm_clock_pre;
271 engine->pm.clock_set = nv04_pm_clock_set;
272 engine->pm.voltage_get = nouveau_voltage_gpio_get;
273 engine->pm.voltage_set = nouveau_voltage_gpio_set;
Ben Skeggsbd2e5972010-10-19 20:06:01 +1000274 engine->crypt.init = nouveau_stub_init;
275 engine->crypt.takedown = nouveau_stub_takedown;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000276 break;
277 case 0x40:
278 case 0x60:
279 engine->instmem.init = nv04_instmem_init;
280 engine->instmem.takedown = nv04_instmem_takedown;
281 engine->instmem.suspend = nv04_instmem_suspend;
282 engine->instmem.resume = nv04_instmem_resume;
283 engine->instmem.populate = nv04_instmem_populate;
284 engine->instmem.clear = nv04_instmem_clear;
285 engine->instmem.bind = nv04_instmem_bind;
286 engine->instmem.unbind = nv04_instmem_unbind;
Ben Skeggsf56cb862010-07-08 11:29:10 +1000287 engine->instmem.flush = nv04_instmem_flush;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000288 engine->mc.init = nv40_mc_init;
289 engine->mc.takedown = nv40_mc_takedown;
290 engine->timer.init = nv04_timer_init;
291 engine->timer.read = nv04_timer_read;
292 engine->timer.takedown = nv04_timer_takedown;
293 engine->fb.init = nv40_fb_init;
294 engine->fb.takedown = nv40_fb_takedown;
Francisco Jereza5cf68b2010-10-24 16:14:41 +0200295 engine->fb.init_tile_region = nv30_fb_init_tile_region;
296 engine->fb.set_tile_region = nv40_fb_set_tile_region;
297 engine->fb.free_tile_region = nv30_fb_free_tile_region;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000298 engine->graph.init = nv40_graph_init;
299 engine->graph.takedown = nv40_graph_takedown;
300 engine->graph.fifo_access = nv04_graph_fifo_access;
301 engine->graph.channel = nv40_graph_channel;
302 engine->graph.create_context = nv40_graph_create_context;
303 engine->graph.destroy_context = nv40_graph_destroy_context;
304 engine->graph.load_context = nv40_graph_load_context;
305 engine->graph.unload_context = nv40_graph_unload_context;
Francisco Jereza5cf68b2010-10-24 16:14:41 +0200306 engine->graph.set_tile_region = nv40_graph_set_tile_region;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000307 engine->fifo.channels = 32;
308 engine->fifo.init = nv40_fifo_init;
309 engine->fifo.takedown = nouveau_stub_takedown;
310 engine->fifo.disable = nv04_fifo_disable;
311 engine->fifo.enable = nv04_fifo_enable;
312 engine->fifo.reassign = nv04_fifo_reassign;
Francisco Jerez588d7d12009-12-13 20:07:42 +0100313 engine->fifo.cache_pull = nv04_fifo_cache_pull;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000314 engine->fifo.channel_id = nv10_fifo_channel_id;
315 engine->fifo.create_context = nv40_fifo_create_context;
Francisco Jerez3945e472010-10-18 03:53:39 +0200316 engine->fifo.destroy_context = nv04_fifo_destroy_context;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000317 engine->fifo.load_context = nv40_fifo_load_context;
318 engine->fifo.unload_context = nv40_fifo_unload_context;
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200319 engine->display.early_init = nv04_display_early_init;
320 engine->display.late_takedown = nv04_display_late_takedown;
321 engine->display.create = nv04_display_create;
322 engine->display.init = nv04_display_init;
323 engine->display.destroy = nv04_display_destroy;
Ben Skeggsee2e0132010-07-26 09:28:25 +1000324 engine->gpio.init = nouveau_stub_init;
325 engine->gpio.takedown = nouveau_stub_takedown;
326 engine->gpio.get = nv10_gpio_get;
327 engine->gpio.set = nv10_gpio_set;
328 engine->gpio.irq_enable = NULL;
Ben Skeggs442b6262010-09-16 16:25:26 +1000329 engine->pm.clock_get = nv04_pm_clock_get;
330 engine->pm.clock_pre = nv04_pm_clock_pre;
331 engine->pm.clock_set = nv04_pm_clock_set;
332 engine->pm.voltage_get = nouveau_voltage_gpio_get;
333 engine->pm.voltage_set = nouveau_voltage_gpio_set;
Francisco Jerez8155cac2010-09-23 20:58:38 +0200334 engine->pm.temp_get = nv40_temp_get;
Ben Skeggsbd2e5972010-10-19 20:06:01 +1000335 engine->crypt.init = nouveau_stub_init;
336 engine->crypt.takedown = nouveau_stub_takedown;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000337 break;
338 case 0x50:
339 case 0x80: /* gotta love NVIDIA's consistency.. */
340 case 0x90:
341 case 0xA0:
342 engine->instmem.init = nv50_instmem_init;
343 engine->instmem.takedown = nv50_instmem_takedown;
344 engine->instmem.suspend = nv50_instmem_suspend;
345 engine->instmem.resume = nv50_instmem_resume;
346 engine->instmem.populate = nv50_instmem_populate;
347 engine->instmem.clear = nv50_instmem_clear;
348 engine->instmem.bind = nv50_instmem_bind;
349 engine->instmem.unbind = nv50_instmem_unbind;
Ben Skeggs734ee832010-07-15 11:02:54 +1000350 if (dev_priv->chipset == 0x50)
351 engine->instmem.flush = nv50_instmem_flush;
352 else
353 engine->instmem.flush = nv84_instmem_flush;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000354 engine->mc.init = nv50_mc_init;
355 engine->mc.takedown = nv50_mc_takedown;
356 engine->timer.init = nv04_timer_init;
357 engine->timer.read = nv04_timer_read;
358 engine->timer.takedown = nv04_timer_takedown;
Marcin Koƛcielnicki304424e2010-03-01 00:18:39 +0000359 engine->fb.init = nv50_fb_init;
360 engine->fb.takedown = nv50_fb_takedown;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000361 engine->graph.init = nv50_graph_init;
362 engine->graph.takedown = nv50_graph_takedown;
363 engine->graph.fifo_access = nv50_graph_fifo_access;
364 engine->graph.channel = nv50_graph_channel;
365 engine->graph.create_context = nv50_graph_create_context;
366 engine->graph.destroy_context = nv50_graph_destroy_context;
367 engine->graph.load_context = nv50_graph_load_context;
368 engine->graph.unload_context = nv50_graph_unload_context;
Ben Skeggs56ac7472010-10-22 10:26:24 +1000369 if (dev_priv->chipset != 0x86)
370 engine->graph.tlb_flush = nv50_graph_tlb_flush;
371 else {
372 /* from what i can see nvidia do this on every
373 * pre-NVA3 board except NVAC, but, we've only
374 * ever seen problems on NV86
375 */
376 engine->graph.tlb_flush = nv86_graph_tlb_flush;
377 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000378 engine->fifo.channels = 128;
379 engine->fifo.init = nv50_fifo_init;
380 engine->fifo.takedown = nv50_fifo_takedown;
381 engine->fifo.disable = nv04_fifo_disable;
382 engine->fifo.enable = nv04_fifo_enable;
383 engine->fifo.reassign = nv04_fifo_reassign;
384 engine->fifo.channel_id = nv50_fifo_channel_id;
385 engine->fifo.create_context = nv50_fifo_create_context;
386 engine->fifo.destroy_context = nv50_fifo_destroy_context;
387 engine->fifo.load_context = nv50_fifo_load_context;
388 engine->fifo.unload_context = nv50_fifo_unload_context;
Ben Skeggs56ac7472010-10-22 10:26:24 +1000389 engine->fifo.tlb_flush = nv50_fifo_tlb_flush;
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200390 engine->display.early_init = nv50_display_early_init;
391 engine->display.late_takedown = nv50_display_late_takedown;
392 engine->display.create = nv50_display_create;
393 engine->display.init = nv50_display_init;
394 engine->display.destroy = nv50_display_destroy;
Ben Skeggsee2e0132010-07-26 09:28:25 +1000395 engine->gpio.init = nv50_gpio_init;
Ben Skeggs2cbd4c82010-11-03 10:18:04 +1000396 engine->gpio.takedown = nv50_gpio_fini;
Ben Skeggsee2e0132010-07-26 09:28:25 +1000397 engine->gpio.get = nv50_gpio_get;
398 engine->gpio.set = nv50_gpio_set;
399 engine->gpio.irq_enable = nv50_gpio_irq_enable;
Ben Skeggsfade7ad2010-09-27 11:18:14 +1000400 switch (dev_priv->chipset) {
Ben Skeggsbd2e5972010-10-19 20:06:01 +1000401 case 0x84:
402 case 0x86:
403 case 0x92:
404 case 0x94:
405 case 0x96:
406 case 0x98:
407 case 0xa0:
Ben Skeggs5f801982010-10-22 08:44:09 +1000408 case 0xaa:
409 case 0xac:
Ben Skeggsbd2e5972010-10-19 20:06:01 +1000410 case 0x50:
Ben Skeggsfade7ad2010-09-27 11:18:14 +1000411 engine->pm.clock_get = nv50_pm_clock_get;
412 engine->pm.clock_pre = nv50_pm_clock_pre;
413 engine->pm.clock_set = nv50_pm_clock_set;
414 break;
Ben Skeggsbd2e5972010-10-19 20:06:01 +1000415 default:
416 engine->pm.clock_get = nva3_pm_clock_get;
417 engine->pm.clock_pre = nva3_pm_clock_pre;
418 engine->pm.clock_set = nva3_pm_clock_set;
419 break;
Ben Skeggsfade7ad2010-09-27 11:18:14 +1000420 }
Ben Skeggs02c30ca2010-09-16 16:17:35 +1000421 engine->pm.voltage_get = nouveau_voltage_gpio_get;
422 engine->pm.voltage_set = nouveau_voltage_gpio_set;
Francisco Jerez8155cac2010-09-23 20:58:38 +0200423 if (dev_priv->chipset >= 0x84)
424 engine->pm.temp_get = nv84_temp_get;
425 else
426 engine->pm.temp_get = nv40_temp_get;
Ben Skeggsbd2e5972010-10-19 20:06:01 +1000427 switch (dev_priv->chipset) {
428 case 0x84:
429 case 0x86:
430 case 0x92:
431 case 0x94:
432 case 0x96:
433 case 0xa0:
434 engine->crypt.init = nv84_crypt_init;
435 engine->crypt.takedown = nv84_crypt_fini;
436 engine->crypt.create_context = nv84_crypt_create_context;
437 engine->crypt.destroy_context = nv84_crypt_destroy_context;
438 break;
439 default:
440 engine->crypt.init = nouveau_stub_init;
441 engine->crypt.takedown = nouveau_stub_takedown;
442 break;
443 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000444 break;
Ben Skeggs4b223ee2010-08-03 10:00:56 +1000445 case 0xC0:
446 engine->instmem.init = nvc0_instmem_init;
447 engine->instmem.takedown = nvc0_instmem_takedown;
448 engine->instmem.suspend = nvc0_instmem_suspend;
449 engine->instmem.resume = nvc0_instmem_resume;
450 engine->instmem.populate = nvc0_instmem_populate;
451 engine->instmem.clear = nvc0_instmem_clear;
452 engine->instmem.bind = nvc0_instmem_bind;
453 engine->instmem.unbind = nvc0_instmem_unbind;
454 engine->instmem.flush = nvc0_instmem_flush;
455 engine->mc.init = nv50_mc_init;
456 engine->mc.takedown = nv50_mc_takedown;
457 engine->timer.init = nv04_timer_init;
458 engine->timer.read = nv04_timer_read;
459 engine->timer.takedown = nv04_timer_takedown;
460 engine->fb.init = nvc0_fb_init;
461 engine->fb.takedown = nvc0_fb_takedown;
Ben Skeggs4b223ee2010-08-03 10:00:56 +1000462 engine->graph.init = nvc0_graph_init;
463 engine->graph.takedown = nvc0_graph_takedown;
464 engine->graph.fifo_access = nvc0_graph_fifo_access;
465 engine->graph.channel = nvc0_graph_channel;
466 engine->graph.create_context = nvc0_graph_create_context;
467 engine->graph.destroy_context = nvc0_graph_destroy_context;
468 engine->graph.load_context = nvc0_graph_load_context;
469 engine->graph.unload_context = nvc0_graph_unload_context;
470 engine->fifo.channels = 128;
471 engine->fifo.init = nvc0_fifo_init;
472 engine->fifo.takedown = nvc0_fifo_takedown;
473 engine->fifo.disable = nvc0_fifo_disable;
474 engine->fifo.enable = nvc0_fifo_enable;
475 engine->fifo.reassign = nvc0_fifo_reassign;
476 engine->fifo.channel_id = nvc0_fifo_channel_id;
477 engine->fifo.create_context = nvc0_fifo_create_context;
478 engine->fifo.destroy_context = nvc0_fifo_destroy_context;
479 engine->fifo.load_context = nvc0_fifo_load_context;
480 engine->fifo.unload_context = nvc0_fifo_unload_context;
481 engine->display.early_init = nv50_display_early_init;
482 engine->display.late_takedown = nv50_display_late_takedown;
483 engine->display.create = nv50_display_create;
484 engine->display.init = nv50_display_init;
485 engine->display.destroy = nv50_display_destroy;
486 engine->gpio.init = nv50_gpio_init;
487 engine->gpio.takedown = nouveau_stub_takedown;
488 engine->gpio.get = nv50_gpio_get;
489 engine->gpio.set = nv50_gpio_set;
490 engine->gpio.irq_enable = nv50_gpio_irq_enable;
Ben Skeggsbd2e5972010-10-19 20:06:01 +1000491 engine->crypt.init = nouveau_stub_init;
492 engine->crypt.takedown = nouveau_stub_takedown;
Ben Skeggs4b223ee2010-08-03 10:00:56 +1000493 break;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000494 default:
495 NV_ERROR(dev, "NV%02x unsupported\n", dev_priv->chipset);
496 return 1;
497 }
498
499 return 0;
500}
501
502static unsigned int
503nouveau_vga_set_decode(void *priv, bool state)
504{
Marcin Koƛcielnicki9967b942010-02-08 00:20:17 +0000505 struct drm_device *dev = priv;
506 struct drm_nouveau_private *dev_priv = dev->dev_private;
507
508 if (dev_priv->chipset >= 0x40)
509 nv_wr32(dev, 0x88054, state);
510 else
511 nv_wr32(dev, 0x1854, state);
512
Ben Skeggs6ee73862009-12-11 19:24:15 +1000513 if (state)
514 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
515 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
516 else
517 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
518}
519
Ben Skeggs0735f622009-12-16 14:28:55 +1000520static int
521nouveau_card_init_channel(struct drm_device *dev)
522{
523 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggsa8eaebc2010-09-01 15:24:31 +1000524 struct nouveau_gpuobj *gpuobj = NULL;
Ben Skeggs0735f622009-12-16 14:28:55 +1000525 int ret;
526
527 ret = nouveau_channel_alloc(dev, &dev_priv->channel,
Ben Skeggsa8eaebc2010-09-01 15:24:31 +1000528 (struct drm_file *)-2, NvDmaFB, NvDmaTT);
Ben Skeggs0735f622009-12-16 14:28:55 +1000529 if (ret)
530 return ret;
531
Ben Skeggs0735f622009-12-16 14:28:55 +1000532 ret = nouveau_gpuobj_dma_new(dev_priv->channel, NV_CLASS_DMA_IN_MEMORY,
Ben Skeggsa76fb4e2010-03-18 09:45:20 +1000533 0, dev_priv->vram_size,
Ben Skeggs0735f622009-12-16 14:28:55 +1000534 NV_DMA_ACCESS_RW, NV_DMA_TARGET_VIDMEM,
535 &gpuobj);
536 if (ret)
537 goto out_err;
538
Ben Skeggsa8eaebc2010-09-01 15:24:31 +1000539 ret = nouveau_ramht_insert(dev_priv->channel, NvDmaVRAM, gpuobj);
540 nouveau_gpuobj_ref(NULL, &gpuobj);
Ben Skeggs0735f622009-12-16 14:28:55 +1000541 if (ret)
542 goto out_err;
543
Ben Skeggs0735f622009-12-16 14:28:55 +1000544 ret = nouveau_gpuobj_gart_dma_new(dev_priv->channel, 0,
545 dev_priv->gart_info.aper_size,
546 NV_DMA_ACCESS_RW, &gpuobj, NULL);
547 if (ret)
548 goto out_err;
549
Ben Skeggsa8eaebc2010-09-01 15:24:31 +1000550 ret = nouveau_ramht_insert(dev_priv->channel, NvDmaGART, gpuobj);
551 nouveau_gpuobj_ref(NULL, &gpuobj);
Ben Skeggs0735f622009-12-16 14:28:55 +1000552 if (ret)
553 goto out_err;
554
Ben Skeggscff5c132010-10-06 16:16:59 +1000555 mutex_unlock(&dev_priv->channel->mutex);
Ben Skeggs0735f622009-12-16 14:28:55 +1000556 return 0;
Ben Skeggsa8eaebc2010-09-01 15:24:31 +1000557
Ben Skeggs0735f622009-12-16 14:28:55 +1000558out_err:
Ben Skeggscff5c132010-10-06 16:16:59 +1000559 nouveau_channel_put(&dev_priv->channel);
Ben Skeggs0735f622009-12-16 14:28:55 +1000560 return ret;
561}
562
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000563static void nouveau_switcheroo_set_state(struct pci_dev *pdev,
564 enum vga_switcheroo_state state)
565{
Dave Airliefbf81762010-06-01 09:09:06 +1000566 struct drm_device *dev = pci_get_drvdata(pdev);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000567 pm_message_t pmm = { .event = PM_EVENT_SUSPEND };
568 if (state == VGA_SWITCHEROO_ON) {
569 printk(KERN_ERR "VGA switcheroo: switched nouveau on\n");
570 nouveau_pci_resume(pdev);
Dave Airliefbf81762010-06-01 09:09:06 +1000571 drm_kms_helper_poll_enable(dev);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000572 } else {
573 printk(KERN_ERR "VGA switcheroo: switched nouveau off\n");
Dave Airliefbf81762010-06-01 09:09:06 +1000574 drm_kms_helper_poll_disable(dev);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000575 nouveau_pci_suspend(pdev, pmm);
576 }
577}
578
579static bool nouveau_switcheroo_can_switch(struct pci_dev *pdev)
580{
581 struct drm_device *dev = pci_get_drvdata(pdev);
582 bool can_switch;
583
584 spin_lock(&dev->count_lock);
585 can_switch = (dev->open_count == 0);
586 spin_unlock(&dev->count_lock);
587 return can_switch;
588}
589
Ben Skeggs6ee73862009-12-11 19:24:15 +1000590int
591nouveau_card_init(struct drm_device *dev)
592{
593 struct drm_nouveau_private *dev_priv = dev->dev_private;
594 struct nouveau_engine *engine;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000595 int ret;
596
Ben Skeggs6ee73862009-12-11 19:24:15 +1000597 vga_client_register(dev->pdev, dev, NULL, nouveau_vga_set_decode);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000598 vga_switcheroo_register_client(dev->pdev, nouveau_switcheroo_set_state,
599 nouveau_switcheroo_can_switch);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000600
601 /* Initialise internal driver API hooks */
602 ret = nouveau_init_engine_ptrs(dev);
603 if (ret)
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000604 goto out;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000605 engine = &dev_priv->engine;
Ben Skeggscff5c132010-10-06 16:16:59 +1000606 spin_lock_init(&dev_priv->channels.lock);
Francisco Jereza5cf68b2010-10-24 16:14:41 +0200607 spin_lock_init(&dev_priv->tile.lock);
Maarten Maathuisff9e5272010-02-01 20:58:27 +0100608 spin_lock_init(&dev_priv->context_switch_lock);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000609
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200610 /* Make the CRTCs and I2C buses accessible */
611 ret = engine->display.early_init(dev);
612 if (ret)
613 goto out;
614
Ben Skeggs6ee73862009-12-11 19:24:15 +1000615 /* Parse BIOS tables / Run init tables if card not POSTed */
Ben Skeggscd0b0722010-06-01 15:56:22 +1000616 ret = nouveau_bios_init(dev);
617 if (ret)
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200618 goto out_display_early;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000619
Ben Skeggs330c5982010-09-16 15:39:49 +1000620 nouveau_pm_init(dev);
621
Ben Skeggsfbd28952010-09-01 15:24:34 +1000622 ret = nouveau_mem_vram_init(dev);
Ben Skeggsa76fb4e2010-03-18 09:45:20 +1000623 if (ret)
624 goto out_bios;
625
Ben Skeggs6ee73862009-12-11 19:24:15 +1000626 ret = nouveau_gpuobj_init(dev);
627 if (ret)
Ben Skeggsfbd28952010-09-01 15:24:34 +1000628 goto out_vram;
629
630 ret = engine->instmem.init(dev);
631 if (ret)
632 goto out_gpuobj;
633
634 ret = nouveau_mem_gart_init(dev);
635 if (ret)
636 goto out_instmem;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000637
638 /* PMC */
639 ret = engine->mc.init(dev);
640 if (ret)
Ben Skeggsfbd28952010-09-01 15:24:34 +1000641 goto out_gart;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000642
Ben Skeggsee2e0132010-07-26 09:28:25 +1000643 /* PGPIO */
644 ret = engine->gpio.init(dev);
645 if (ret)
646 goto out_mc;
647
Ben Skeggs6ee73862009-12-11 19:24:15 +1000648 /* PTIMER */
649 ret = engine->timer.init(dev);
650 if (ret)
Ben Skeggsee2e0132010-07-26 09:28:25 +1000651 goto out_gpio;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000652
653 /* PFB */
654 ret = engine->fb.init(dev);
655 if (ret)
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000656 goto out_timer;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000657
Marcin Koƛcielnickia32ed692010-01-26 14:00:42 +0000658 if (nouveau_noaccel)
659 engine->graph.accel_blocked = true;
660 else {
661 /* PGRAPH */
662 ret = engine->graph.init(dev);
663 if (ret)
664 goto out_fb;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000665
Ben Skeggsbd2e5972010-10-19 20:06:01 +1000666 /* PCRYPT */
667 ret = engine->crypt.init(dev);
668 if (ret)
669 goto out_graph;
670
Marcin Koƛcielnickia32ed692010-01-26 14:00:42 +0000671 /* PFIFO */
672 ret = engine->fifo.init(dev);
673 if (ret)
Ben Skeggsbd2e5972010-10-19 20:06:01 +1000674 goto out_crypt;
Marcin Koƛcielnickia32ed692010-01-26 14:00:42 +0000675 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000676
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200677 ret = engine->display.create(dev);
Ben Skeggse88efe02010-07-09 10:56:08 +1000678 if (ret)
679 goto out_fifo;
680
Francisco Jerez042206c2010-10-21 18:19:29 +0200681 ret = drm_vblank_init(dev, nv_two_heads(dev) ? 2 : 1);
682 if (ret)
683 goto out_vblank;
684
Ben Skeggs35fa2f22010-10-21 14:07:03 +1000685 ret = nouveau_irq_init(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000686 if (ret)
Francisco Jerez042206c2010-10-21 18:19:29 +0200687 goto out_vblank;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000688
689 /* what about PVIDEO/PCRTC/PRAMDAC etc? */
690
Ben Skeggs0735f622009-12-16 14:28:55 +1000691 if (!engine->graph.accel_blocked) {
Francisco Jerez0c6c1c22010-09-22 00:58:54 +0200692 ret = nouveau_fence_init(dev);
Ben Skeggs0735f622009-12-16 14:28:55 +1000693 if (ret)
694 goto out_irq;
Francisco Jerez0c6c1c22010-09-22 00:58:54 +0200695
696 ret = nouveau_card_init_channel(dev);
697 if (ret)
698 goto out_fence;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000699 }
700
Ben Skeggs6ee73862009-12-11 19:24:15 +1000701 ret = nouveau_backlight_init(dev);
702 if (ret)
703 NV_ERROR(dev, "Error %d registering backlight\n", ret);
704
Ben Skeggscd0b0722010-06-01 15:56:22 +1000705 nouveau_fbcon_init(dev);
706 drm_kms_helper_poll_init(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000707 return 0;
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000708
Francisco Jerez0c6c1c22010-09-22 00:58:54 +0200709out_fence:
710 nouveau_fence_fini(dev);
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000711out_irq:
Ben Skeggs35fa2f22010-10-21 14:07:03 +1000712 nouveau_irq_fini(dev);
Francisco Jerez042206c2010-10-21 18:19:29 +0200713out_vblank:
714 drm_vblank_cleanup(dev);
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200715 engine->display.destroy(dev);
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000716out_fifo:
Marcin Koƛcielnickia32ed692010-01-26 14:00:42 +0000717 if (!nouveau_noaccel)
718 engine->fifo.takedown(dev);
Ben Skeggsbd2e5972010-10-19 20:06:01 +1000719out_crypt:
720 if (!nouveau_noaccel)
721 engine->crypt.takedown(dev);
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000722out_graph:
Marcin Koƛcielnickia32ed692010-01-26 14:00:42 +0000723 if (!nouveau_noaccel)
724 engine->graph.takedown(dev);
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000725out_fb:
726 engine->fb.takedown(dev);
727out_timer:
728 engine->timer.takedown(dev);
Ben Skeggsee2e0132010-07-26 09:28:25 +1000729out_gpio:
730 engine->gpio.takedown(dev);
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000731out_mc:
732 engine->mc.takedown(dev);
Ben Skeggsfbd28952010-09-01 15:24:34 +1000733out_gart:
734 nouveau_mem_gart_fini(dev);
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000735out_instmem:
736 engine->instmem.takedown(dev);
Ben Skeggsfbd28952010-09-01 15:24:34 +1000737out_gpuobj:
738 nouveau_gpuobj_takedown(dev);
739out_vram:
740 nouveau_mem_vram_fini(dev);
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000741out_bios:
Ben Skeggs330c5982010-09-16 15:39:49 +1000742 nouveau_pm_fini(dev);
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000743 nouveau_bios_takedown(dev);
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200744out_display_early:
745 engine->display.late_takedown(dev);
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000746out:
747 vga_client_register(dev->pdev, NULL, NULL, NULL);
748 return ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000749}
750
751static void nouveau_card_takedown(struct drm_device *dev)
752{
753 struct drm_nouveau_private *dev_priv = dev->dev_private;
754 struct nouveau_engine *engine = &dev_priv->engine;
755
Ben Skeggsb6d3d872010-06-07 15:38:27 +1000756 nouveau_backlight_exit(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000757
Francisco Jerez0c6c1c22010-09-22 00:58:54 +0200758 if (!engine->graph.accel_blocked) {
759 nouveau_fence_fini(dev);
Francisco Jerez36c952e2010-10-18 03:01:34 +0200760 nouveau_channel_put_unlocked(&dev_priv->channel);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000761 }
Ben Skeggsb6d3d872010-06-07 15:38:27 +1000762
763 if (!nouveau_noaccel) {
764 engine->fifo.takedown(dev);
Ben Skeggsbd2e5972010-10-19 20:06:01 +1000765 engine->crypt.takedown(dev);
Ben Skeggsb6d3d872010-06-07 15:38:27 +1000766 engine->graph.takedown(dev);
767 }
768 engine->fb.takedown(dev);
769 engine->timer.takedown(dev);
Ben Skeggsee2e0132010-07-26 09:28:25 +1000770 engine->gpio.takedown(dev);
Ben Skeggsb6d3d872010-06-07 15:38:27 +1000771 engine->mc.takedown(dev);
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200772 engine->display.late_takedown(dev);
Ben Skeggsb6d3d872010-06-07 15:38:27 +1000773
774 mutex_lock(&dev->struct_mutex);
775 ttm_bo_clean_mm(&dev_priv->ttm.bdev, TTM_PL_VRAM);
776 ttm_bo_clean_mm(&dev_priv->ttm.bdev, TTM_PL_TT);
777 mutex_unlock(&dev->struct_mutex);
Ben Skeggsfbd28952010-09-01 15:24:34 +1000778 nouveau_mem_gart_fini(dev);
Ben Skeggsb6d3d872010-06-07 15:38:27 +1000779
Ben Skeggsb6d3d872010-06-07 15:38:27 +1000780 engine->instmem.takedown(dev);
Ben Skeggsfbd28952010-09-01 15:24:34 +1000781 nouveau_gpuobj_takedown(dev);
782 nouveau_mem_vram_fini(dev);
Ben Skeggsb6d3d872010-06-07 15:38:27 +1000783
Ben Skeggs35fa2f22010-10-21 14:07:03 +1000784 nouveau_irq_fini(dev);
Francisco Jerez042206c2010-10-21 18:19:29 +0200785 drm_vblank_cleanup(dev);
Ben Skeggsb6d3d872010-06-07 15:38:27 +1000786
Ben Skeggs330c5982010-09-16 15:39:49 +1000787 nouveau_pm_fini(dev);
Ben Skeggsb6d3d872010-06-07 15:38:27 +1000788 nouveau_bios_takedown(dev);
789
790 vga_client_register(dev->pdev, NULL, NULL, NULL);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000791}
792
793/* here a client dies, release the stuff that was allocated for its
794 * file_priv */
795void nouveau_preclose(struct drm_device *dev, struct drm_file *file_priv)
796{
797 nouveau_channel_cleanup(dev, file_priv);
798}
799
800/* first module load, setup the mmio/fb mapping */
801/* KMS: we need mmio at load time, not when the first drm client opens. */
802int nouveau_firstopen(struct drm_device *dev)
803{
804 return 0;
805}
806
807/* if we have an OF card, copy vbios to RAMIN */
808static void nouveau_OF_copy_vbios_to_ramin(struct drm_device *dev)
809{
810#if defined(__powerpc__)
811 int size, i;
812 const uint32_t *bios;
813 struct device_node *dn = pci_device_to_OF_node(dev->pdev);
814 if (!dn) {
815 NV_INFO(dev, "Unable to get the OF node\n");
816 return;
817 }
818
819 bios = of_get_property(dn, "NVDA,BMP", &size);
820 if (bios) {
821 for (i = 0; i < size; i += 4)
822 nv_wi32(dev, i, bios[i/4]);
823 NV_INFO(dev, "OF bios successfully copied (%d bytes)\n", size);
824 } else {
825 NV_INFO(dev, "Unable to get the OF bios\n");
826 }
827#endif
828}
829
Marcin Slusarz06415c52010-05-16 17:29:56 +0200830static struct apertures_struct *nouveau_get_apertures(struct drm_device *dev)
831{
832 struct pci_dev *pdev = dev->pdev;
833 struct apertures_struct *aper = alloc_apertures(3);
834 if (!aper)
835 return NULL;
836
837 aper->ranges[0].base = pci_resource_start(pdev, 1);
838 aper->ranges[0].size = pci_resource_len(pdev, 1);
839 aper->count = 1;
840
841 if (pci_resource_len(pdev, 2)) {
842 aper->ranges[aper->count].base = pci_resource_start(pdev, 2);
843 aper->ranges[aper->count].size = pci_resource_len(pdev, 2);
844 aper->count++;
845 }
846
847 if (pci_resource_len(pdev, 3)) {
848 aper->ranges[aper->count].base = pci_resource_start(pdev, 3);
849 aper->ranges[aper->count].size = pci_resource_len(pdev, 3);
850 aper->count++;
851 }
852
853 return aper;
854}
855
856static int nouveau_remove_conflicting_drivers(struct drm_device *dev)
857{
858 struct drm_nouveau_private *dev_priv = dev->dev_private;
Marcin Slusarz3b9676e2010-05-16 17:33:09 +0200859 bool primary = false;
Marcin Slusarz06415c52010-05-16 17:29:56 +0200860 dev_priv->apertures = nouveau_get_apertures(dev);
861 if (!dev_priv->apertures)
862 return -ENOMEM;
863
Marcin Slusarz3b9676e2010-05-16 17:33:09 +0200864#ifdef CONFIG_X86
865 primary = dev->pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW;
866#endif
867
868 remove_conflicting_framebuffers(dev_priv->apertures, "nouveaufb", primary);
Marcin Slusarz06415c52010-05-16 17:29:56 +0200869 return 0;
870}
871
Ben Skeggs6ee73862009-12-11 19:24:15 +1000872int nouveau_load(struct drm_device *dev, unsigned long flags)
873{
874 struct drm_nouveau_private *dev_priv;
875 uint32_t reg0;
876 resource_size_t mmio_start_offs;
Ben Skeggscd0b0722010-06-01 15:56:22 +1000877 int ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000878
879 dev_priv = kzalloc(sizeof(*dev_priv), GFP_KERNEL);
Dan Carpentera0d069e2010-07-30 17:04:32 +0200880 if (!dev_priv) {
881 ret = -ENOMEM;
882 goto err_out;
883 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000884 dev->dev_private = dev_priv;
885 dev_priv->dev = dev;
886
887 dev_priv->flags = flags & NOUVEAU_FLAGS;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000888
889 NV_DEBUG(dev, "vendor: 0x%X device: 0x%X class: 0x%X\n",
890 dev->pci_vendor, dev->pci_device, dev->pdev->class);
891
Ben Skeggs6ee73862009-12-11 19:24:15 +1000892 dev_priv->wq = create_workqueue("nouveau");
Dan Carpentera0d069e2010-07-30 17:04:32 +0200893 if (!dev_priv->wq) {
894 ret = -EINVAL;
895 goto err_priv;
896 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000897
898 /* resource 0 is mmio regs */
899 /* resource 1 is linear FB */
900 /* resource 2 is RAMIN (mmio regs + 0x1000000) */
901 /* resource 6 is bios */
902
903 /* map the mmio regs */
904 mmio_start_offs = pci_resource_start(dev->pdev, 0);
905 dev_priv->mmio = ioremap(mmio_start_offs, 0x00800000);
906 if (!dev_priv->mmio) {
907 NV_ERROR(dev, "Unable to initialize the mmio mapping. "
908 "Please report your setup to " DRIVER_EMAIL "\n");
Dan Carpentera0d069e2010-07-30 17:04:32 +0200909 ret = -EINVAL;
910 goto err_wq;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000911 }
912 NV_DEBUG(dev, "regs mapped ok at 0x%llx\n",
913 (unsigned long long)mmio_start_offs);
914
915#ifdef __BIG_ENDIAN
916 /* Put the card in BE mode if it's not */
917 if (nv_rd32(dev, NV03_PMC_BOOT_1))
918 nv_wr32(dev, NV03_PMC_BOOT_1, 0x00000001);
919
920 DRM_MEMORYBARRIER();
921#endif
922
923 /* Time to determine the card architecture */
924 reg0 = nv_rd32(dev, NV03_PMC_BOOT_0);
925
926 /* We're dealing with >=NV10 */
927 if ((reg0 & 0x0f000000) > 0) {
928 /* Bit 27-20 contain the architecture in hex */
929 dev_priv->chipset = (reg0 & 0xff00000) >> 20;
930 /* NV04 or NV05 */
931 } else if ((reg0 & 0xff00fff0) == 0x20004000) {
Ben Skeggs1dee7a92010-01-07 13:47:57 +1000932 if (reg0 & 0x00f00000)
933 dev_priv->chipset = 0x05;
934 else
935 dev_priv->chipset = 0x04;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000936 } else
937 dev_priv->chipset = 0xff;
938
939 switch (dev_priv->chipset & 0xf0) {
940 case 0x00:
941 case 0x10:
942 case 0x20:
943 case 0x30:
944 dev_priv->card_type = dev_priv->chipset & 0xf0;
945 break;
946 case 0x40:
947 case 0x60:
948 dev_priv->card_type = NV_40;
949 break;
950 case 0x50:
951 case 0x80:
952 case 0x90:
953 case 0xa0:
954 dev_priv->card_type = NV_50;
955 break;
Ben Skeggs4b223ee2010-08-03 10:00:56 +1000956 case 0xc0:
957 dev_priv->card_type = NV_C0;
958 break;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000959 default:
960 NV_INFO(dev, "Unsupported chipset 0x%08x\n", reg0);
Dan Carpentera0d069e2010-07-30 17:04:32 +0200961 ret = -EINVAL;
962 goto err_mmio;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000963 }
964
965 NV_INFO(dev, "Detected an NV%2x generation card (0x%08x)\n",
966 dev_priv->card_type, reg0);
967
Ben Skeggscd0b0722010-06-01 15:56:22 +1000968 ret = nouveau_remove_conflicting_drivers(dev);
969 if (ret)
Dan Carpentera0d069e2010-07-30 17:04:32 +0200970 goto err_mmio;
Marcin Slusarz06415c52010-05-16 17:29:56 +0200971
Ben Skeggs6d696302010-06-02 10:16:24 +1000972 /* Map PRAMIN BAR, or on older cards, the aperture withing BAR0 */
Ben Skeggs6ee73862009-12-11 19:24:15 +1000973 if (dev_priv->card_type >= NV_40) {
974 int ramin_bar = 2;
975 if (pci_resource_len(dev->pdev, ramin_bar) == 0)
976 ramin_bar = 3;
977
978 dev_priv->ramin_size = pci_resource_len(dev->pdev, ramin_bar);
Ben Skeggs6d696302010-06-02 10:16:24 +1000979 dev_priv->ramin =
980 ioremap(pci_resource_start(dev->pdev, ramin_bar),
Ben Skeggs6ee73862009-12-11 19:24:15 +1000981 dev_priv->ramin_size);
982 if (!dev_priv->ramin) {
Ben Skeggs6d696302010-06-02 10:16:24 +1000983 NV_ERROR(dev, "Failed to PRAMIN BAR");
Dan Carpentera0d069e2010-07-30 17:04:32 +0200984 ret = -ENOMEM;
985 goto err_mmio;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000986 }
Ben Skeggs6d696302010-06-02 10:16:24 +1000987 } else {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000988 dev_priv->ramin_size = 1 * 1024 * 1024;
989 dev_priv->ramin = ioremap(mmio_start_offs + NV_RAMIN,
Ben Skeggs6d696302010-06-02 10:16:24 +1000990 dev_priv->ramin_size);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000991 if (!dev_priv->ramin) {
992 NV_ERROR(dev, "Failed to map BAR0 PRAMIN.\n");
Dan Carpentera0d069e2010-07-30 17:04:32 +0200993 ret = -ENOMEM;
994 goto err_mmio;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000995 }
996 }
997
998 nouveau_OF_copy_vbios_to_ramin(dev);
999
1000 /* Special flags */
1001 if (dev->pci_device == 0x01a0)
1002 dev_priv->flags |= NV_NFORCE;
1003 else if (dev->pci_device == 0x01f0)
1004 dev_priv->flags |= NV_NFORCE2;
1005
1006 /* For kernel modesetting, init card now and bring up fbcon */
Ben Skeggscd0b0722010-06-01 15:56:22 +10001007 ret = nouveau_card_init(dev);
1008 if (ret)
Dan Carpentera0d069e2010-07-30 17:04:32 +02001009 goto err_ramin;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001010
1011 return 0;
Dan Carpentera0d069e2010-07-30 17:04:32 +02001012
1013err_ramin:
1014 iounmap(dev_priv->ramin);
1015err_mmio:
1016 iounmap(dev_priv->mmio);
1017err_wq:
1018 destroy_workqueue(dev_priv->wq);
1019err_priv:
1020 kfree(dev_priv);
1021 dev->dev_private = NULL;
1022err_out:
1023 return ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001024}
1025
Ben Skeggs6ee73862009-12-11 19:24:15 +10001026void nouveau_lastclose(struct drm_device *dev)
1027{
Ben Skeggs6ee73862009-12-11 19:24:15 +10001028}
1029
1030int nouveau_unload(struct drm_device *dev)
1031{
1032 struct drm_nouveau_private *dev_priv = dev->dev_private;
Francisco Jerezc88c2e02010-07-24 17:37:33 +02001033 struct nouveau_engine *engine = &dev_priv->engine;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001034
Ben Skeggscd0b0722010-06-01 15:56:22 +10001035 drm_kms_helper_poll_fini(dev);
1036 nouveau_fbcon_fini(dev);
Francisco Jerezc88c2e02010-07-24 17:37:33 +02001037 engine->display.destroy(dev);
Ben Skeggscd0b0722010-06-01 15:56:22 +10001038 nouveau_card_takedown(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +10001039
1040 iounmap(dev_priv->mmio);
1041 iounmap(dev_priv->ramin);
1042
1043 kfree(dev_priv);
1044 dev->dev_private = NULL;
1045 return 0;
1046}
1047
Ben Skeggs6ee73862009-12-11 19:24:15 +10001048int nouveau_ioctl_getparam(struct drm_device *dev, void *data,
1049 struct drm_file *file_priv)
1050{
1051 struct drm_nouveau_private *dev_priv = dev->dev_private;
1052 struct drm_nouveau_getparam *getparam = data;
1053
Ben Skeggs6ee73862009-12-11 19:24:15 +10001054 switch (getparam->param) {
1055 case NOUVEAU_GETPARAM_CHIPSET_ID:
1056 getparam->value = dev_priv->chipset;
1057 break;
1058 case NOUVEAU_GETPARAM_PCI_VENDOR:
1059 getparam->value = dev->pci_vendor;
1060 break;
1061 case NOUVEAU_GETPARAM_PCI_DEVICE:
1062 getparam->value = dev->pci_device;
1063 break;
1064 case NOUVEAU_GETPARAM_BUS_TYPE:
1065 if (drm_device_is_agp(dev))
1066 getparam->value = NV_AGP;
1067 else if (drm_device_is_pcie(dev))
1068 getparam->value = NV_PCIE;
1069 else
1070 getparam->value = NV_PCI;
1071 break;
1072 case NOUVEAU_GETPARAM_FB_PHYSICAL:
1073 getparam->value = dev_priv->fb_phys;
1074 break;
1075 case NOUVEAU_GETPARAM_AGP_PHYSICAL:
1076 getparam->value = dev_priv->gart_info.aper_base;
1077 break;
1078 case NOUVEAU_GETPARAM_PCI_PHYSICAL:
1079 if (dev->sg) {
1080 getparam->value = (unsigned long)dev->sg->virtual;
1081 } else {
1082 NV_ERROR(dev, "Requested PCIGART address, "
1083 "while no PCIGART was created\n");
1084 return -EINVAL;
1085 }
1086 break;
1087 case NOUVEAU_GETPARAM_FB_SIZE:
1088 getparam->value = dev_priv->fb_available_size;
1089 break;
1090 case NOUVEAU_GETPARAM_AGP_SIZE:
1091 getparam->value = dev_priv->gart_info.aper_size;
1092 break;
1093 case NOUVEAU_GETPARAM_VM_VRAM_BASE:
1094 getparam->value = dev_priv->vm_vram_base;
1095 break;
Marcin Koƛcielnicki7fc74f12010-05-23 11:36:04 +00001096 case NOUVEAU_GETPARAM_PTIMER_TIME:
1097 getparam->value = dev_priv->engine.timer.read(dev);
1098 break;
Francisco Jerezf13b3262010-10-10 06:01:08 +02001099 case NOUVEAU_GETPARAM_HAS_BO_USAGE:
1100 getparam->value = 1;
1101 break;
Francisco Jerez332b2422010-10-20 23:35:40 +02001102 case NOUVEAU_GETPARAM_HAS_PAGEFLIP:
1103 getparam->value = (dev_priv->card_type < NV_50);
1104 break;
Marcin Koƛcielnicki69c97002010-01-26 18:39:20 +00001105 case NOUVEAU_GETPARAM_GRAPH_UNITS:
1106 /* NV40 and NV50 versions are quite different, but register
1107 * address is the same. User is supposed to know the card
1108 * family anyway... */
1109 if (dev_priv->chipset >= 0x40) {
1110 getparam->value = nv_rd32(dev, NV40_PMC_GRAPH_UNITS);
1111 break;
1112 }
1113 /* FALLTHRU */
Ben Skeggs6ee73862009-12-11 19:24:15 +10001114 default:
Francisco Jerez1397b422010-10-12 03:17:43 +02001115 NV_DEBUG(dev, "unknown parameter %lld\n", getparam->param);
Ben Skeggs6ee73862009-12-11 19:24:15 +10001116 return -EINVAL;
1117 }
1118
1119 return 0;
1120}
1121
1122int
1123nouveau_ioctl_setparam(struct drm_device *dev, void *data,
1124 struct drm_file *file_priv)
1125{
1126 struct drm_nouveau_setparam *setparam = data;
1127
Ben Skeggs6ee73862009-12-11 19:24:15 +10001128 switch (setparam->param) {
1129 default:
Francisco Jerez1397b422010-10-12 03:17:43 +02001130 NV_DEBUG(dev, "unknown parameter %lld\n", setparam->param);
Ben Skeggs6ee73862009-12-11 19:24:15 +10001131 return -EINVAL;
1132 }
1133
1134 return 0;
1135}
1136
1137/* Wait until (value(reg) & mask) == val, up until timeout has hit */
1138bool nouveau_wait_until(struct drm_device *dev, uint64_t timeout,
1139 uint32_t reg, uint32_t mask, uint32_t val)
1140{
1141 struct drm_nouveau_private *dev_priv = dev->dev_private;
1142 struct nouveau_timer_engine *ptimer = &dev_priv->engine.timer;
1143 uint64_t start = ptimer->read(dev);
1144
1145 do {
1146 if ((nv_rd32(dev, reg) & mask) == val)
1147 return true;
1148 } while (ptimer->read(dev) - start < timeout);
1149
1150 return false;
1151}
1152
1153/* Waits for PGRAPH to go completely idle */
1154bool nouveau_wait_for_idle(struct drm_device *dev)
1155{
Francisco Jerez0541324a2010-10-18 16:15:15 +02001156 struct drm_nouveau_private *dev_priv = dev->dev_private;
1157 uint32_t mask = ~0;
1158
1159 if (dev_priv->card_type == NV_40)
1160 mask &= ~NV40_PGRAPH_STATUS_SYNC_STALL;
1161
1162 if (!nv_wait(dev, NV04_PGRAPH_STATUS, mask, 0)) {
Ben Skeggs6ee73862009-12-11 19:24:15 +10001163 NV_ERROR(dev, "PGRAPH idle timed out with status 0x%08x\n",
1164 nv_rd32(dev, NV04_PGRAPH_STATUS));
1165 return false;
1166 }
1167
1168 return true;
1169}
1170