blob: ed7757f14083a17e3122250ca83aa8ee2959ddec [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;
68 engine->graph.grclass = nv04_graph_grclass;
69 engine->graph.init = nv04_graph_init;
70 engine->graph.takedown = nv04_graph_takedown;
71 engine->graph.fifo_access = nv04_graph_fifo_access;
72 engine->graph.channel = nv04_graph_channel;
73 engine->graph.create_context = nv04_graph_create_context;
74 engine->graph.destroy_context = nv04_graph_destroy_context;
75 engine->graph.load_context = nv04_graph_load_context;
76 engine->graph.unload_context = nv04_graph_unload_context;
77 engine->fifo.channels = 16;
78 engine->fifo.init = nv04_fifo_init;
79 engine->fifo.takedown = nouveau_stub_takedown;
80 engine->fifo.disable = nv04_fifo_disable;
81 engine->fifo.enable = nv04_fifo_enable;
82 engine->fifo.reassign = nv04_fifo_reassign;
Francisco Jerez588d7d12009-12-13 20:07:42 +010083 engine->fifo.cache_pull = nv04_fifo_cache_pull;
Ben Skeggs6ee73862009-12-11 19:24:15 +100084 engine->fifo.channel_id = nv04_fifo_channel_id;
85 engine->fifo.create_context = nv04_fifo_create_context;
86 engine->fifo.destroy_context = nv04_fifo_destroy_context;
87 engine->fifo.load_context = nv04_fifo_load_context;
88 engine->fifo.unload_context = nv04_fifo_unload_context;
Francisco Jerezc88c2e02010-07-24 17:37:33 +020089 engine->display.early_init = nv04_display_early_init;
90 engine->display.late_takedown = nv04_display_late_takedown;
91 engine->display.create = nv04_display_create;
92 engine->display.init = nv04_display_init;
93 engine->display.destroy = nv04_display_destroy;
Ben Skeggsee2e0132010-07-26 09:28:25 +100094 engine->gpio.init = nouveau_stub_init;
95 engine->gpio.takedown = nouveau_stub_takedown;
96 engine->gpio.get = NULL;
97 engine->gpio.set = NULL;
98 engine->gpio.irq_enable = NULL;
Ben Skeggs442b6262010-09-16 16:25:26 +100099 engine->pm.clock_get = nv04_pm_clock_get;
100 engine->pm.clock_pre = nv04_pm_clock_pre;
101 engine->pm.clock_set = nv04_pm_clock_set;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000102 break;
103 case 0x10:
104 engine->instmem.init = nv04_instmem_init;
105 engine->instmem.takedown = nv04_instmem_takedown;
106 engine->instmem.suspend = nv04_instmem_suspend;
107 engine->instmem.resume = nv04_instmem_resume;
108 engine->instmem.populate = nv04_instmem_populate;
109 engine->instmem.clear = nv04_instmem_clear;
110 engine->instmem.bind = nv04_instmem_bind;
111 engine->instmem.unbind = nv04_instmem_unbind;
Ben Skeggsf56cb862010-07-08 11:29:10 +1000112 engine->instmem.flush = nv04_instmem_flush;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000113 engine->mc.init = nv04_mc_init;
114 engine->mc.takedown = nv04_mc_takedown;
115 engine->timer.init = nv04_timer_init;
116 engine->timer.read = nv04_timer_read;
117 engine->timer.takedown = nv04_timer_takedown;
118 engine->fb.init = nv10_fb_init;
119 engine->fb.takedown = nv10_fb_takedown;
Francisco Jerezcb00f7c2009-12-16 12:12:27 +0100120 engine->fb.set_region_tiling = nv10_fb_set_region_tiling;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000121 engine->graph.grclass = nv10_graph_grclass;
122 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;
140 engine->fifo.destroy_context = nv10_fifo_destroy_context;
141 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 Skeggs6ee73862009-12-11 19:24:15 +1000156 break;
157 case 0x20:
158 engine->instmem.init = nv04_instmem_init;
159 engine->instmem.takedown = nv04_instmem_takedown;
160 engine->instmem.suspend = nv04_instmem_suspend;
161 engine->instmem.resume = nv04_instmem_resume;
162 engine->instmem.populate = nv04_instmem_populate;
163 engine->instmem.clear = nv04_instmem_clear;
164 engine->instmem.bind = nv04_instmem_bind;
165 engine->instmem.unbind = nv04_instmem_unbind;
Ben Skeggsf56cb862010-07-08 11:29:10 +1000166 engine->instmem.flush = nv04_instmem_flush;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000167 engine->mc.init = nv04_mc_init;
168 engine->mc.takedown = nv04_mc_takedown;
169 engine->timer.init = nv04_timer_init;
170 engine->timer.read = nv04_timer_read;
171 engine->timer.takedown = nv04_timer_takedown;
172 engine->fb.init = nv10_fb_init;
173 engine->fb.takedown = nv10_fb_takedown;
Francisco Jerezcb00f7c2009-12-16 12:12:27 +0100174 engine->fb.set_region_tiling = nv10_fb_set_region_tiling;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000175 engine->graph.grclass = nv20_graph_grclass;
176 engine->graph.init = nv20_graph_init;
177 engine->graph.takedown = nv20_graph_takedown;
178 engine->graph.channel = nv10_graph_channel;
179 engine->graph.create_context = nv20_graph_create_context;
180 engine->graph.destroy_context = nv20_graph_destroy_context;
181 engine->graph.fifo_access = nv04_graph_fifo_access;
182 engine->graph.load_context = nv20_graph_load_context;
183 engine->graph.unload_context = nv20_graph_unload_context;
Francisco Jerezcb00f7c2009-12-16 12:12:27 +0100184 engine->graph.set_region_tiling = nv20_graph_set_region_tiling;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000185 engine->fifo.channels = 32;
186 engine->fifo.init = nv10_fifo_init;
187 engine->fifo.takedown = nouveau_stub_takedown;
188 engine->fifo.disable = nv04_fifo_disable;
189 engine->fifo.enable = nv04_fifo_enable;
190 engine->fifo.reassign = nv04_fifo_reassign;
Francisco Jerez588d7d12009-12-13 20:07:42 +0100191 engine->fifo.cache_pull = nv04_fifo_cache_pull;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000192 engine->fifo.channel_id = nv10_fifo_channel_id;
193 engine->fifo.create_context = nv10_fifo_create_context;
194 engine->fifo.destroy_context = nv10_fifo_destroy_context;
195 engine->fifo.load_context = nv10_fifo_load_context;
196 engine->fifo.unload_context = nv10_fifo_unload_context;
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200197 engine->display.early_init = nv04_display_early_init;
198 engine->display.late_takedown = nv04_display_late_takedown;
199 engine->display.create = nv04_display_create;
200 engine->display.init = nv04_display_init;
201 engine->display.destroy = nv04_display_destroy;
Ben Skeggsee2e0132010-07-26 09:28:25 +1000202 engine->gpio.init = nouveau_stub_init;
203 engine->gpio.takedown = nouveau_stub_takedown;
204 engine->gpio.get = nv10_gpio_get;
205 engine->gpio.set = nv10_gpio_set;
206 engine->gpio.irq_enable = NULL;
Ben Skeggs442b6262010-09-16 16:25:26 +1000207 engine->pm.clock_get = nv04_pm_clock_get;
208 engine->pm.clock_pre = nv04_pm_clock_pre;
209 engine->pm.clock_set = nv04_pm_clock_set;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000210 break;
211 case 0x30:
212 engine->instmem.init = nv04_instmem_init;
213 engine->instmem.takedown = nv04_instmem_takedown;
214 engine->instmem.suspend = nv04_instmem_suspend;
215 engine->instmem.resume = nv04_instmem_resume;
216 engine->instmem.populate = nv04_instmem_populate;
217 engine->instmem.clear = nv04_instmem_clear;
218 engine->instmem.bind = nv04_instmem_bind;
219 engine->instmem.unbind = nv04_instmem_unbind;
Ben Skeggsf56cb862010-07-08 11:29:10 +1000220 engine->instmem.flush = nv04_instmem_flush;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000221 engine->mc.init = nv04_mc_init;
222 engine->mc.takedown = nv04_mc_takedown;
223 engine->timer.init = nv04_timer_init;
224 engine->timer.read = nv04_timer_read;
225 engine->timer.takedown = nv04_timer_takedown;
Francisco Jerez8bded182010-07-21 21:08:11 +0200226 engine->fb.init = nv30_fb_init;
227 engine->fb.takedown = nv30_fb_takedown;
Francisco Jerezcb00f7c2009-12-16 12:12:27 +0100228 engine->fb.set_region_tiling = nv10_fb_set_region_tiling;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000229 engine->graph.grclass = nv30_graph_grclass;
230 engine->graph.init = nv30_graph_init;
231 engine->graph.takedown = nv20_graph_takedown;
232 engine->graph.fifo_access = nv04_graph_fifo_access;
233 engine->graph.channel = nv10_graph_channel;
234 engine->graph.create_context = nv20_graph_create_context;
235 engine->graph.destroy_context = nv20_graph_destroy_context;
236 engine->graph.load_context = nv20_graph_load_context;
237 engine->graph.unload_context = nv20_graph_unload_context;
Francisco Jerezcb00f7c2009-12-16 12:12:27 +0100238 engine->graph.set_region_tiling = nv20_graph_set_region_tiling;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000239 engine->fifo.channels = 32;
240 engine->fifo.init = nv10_fifo_init;
241 engine->fifo.takedown = nouveau_stub_takedown;
242 engine->fifo.disable = nv04_fifo_disable;
243 engine->fifo.enable = nv04_fifo_enable;
244 engine->fifo.reassign = nv04_fifo_reassign;
Francisco Jerez588d7d12009-12-13 20:07:42 +0100245 engine->fifo.cache_pull = nv04_fifo_cache_pull;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000246 engine->fifo.channel_id = nv10_fifo_channel_id;
247 engine->fifo.create_context = nv10_fifo_create_context;
248 engine->fifo.destroy_context = nv10_fifo_destroy_context;
249 engine->fifo.load_context = nv10_fifo_load_context;
250 engine->fifo.unload_context = nv10_fifo_unload_context;
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200251 engine->display.early_init = nv04_display_early_init;
252 engine->display.late_takedown = nv04_display_late_takedown;
253 engine->display.create = nv04_display_create;
254 engine->display.init = nv04_display_init;
255 engine->display.destroy = nv04_display_destroy;
Ben Skeggsee2e0132010-07-26 09:28:25 +1000256 engine->gpio.init = nouveau_stub_init;
257 engine->gpio.takedown = nouveau_stub_takedown;
258 engine->gpio.get = nv10_gpio_get;
259 engine->gpio.set = nv10_gpio_set;
260 engine->gpio.irq_enable = NULL;
Ben Skeggs442b6262010-09-16 16:25:26 +1000261 engine->pm.clock_get = nv04_pm_clock_get;
262 engine->pm.clock_pre = nv04_pm_clock_pre;
263 engine->pm.clock_set = nv04_pm_clock_set;
264 engine->pm.voltage_get = nouveau_voltage_gpio_get;
265 engine->pm.voltage_set = nouveau_voltage_gpio_set;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000266 break;
267 case 0x40:
268 case 0x60:
269 engine->instmem.init = nv04_instmem_init;
270 engine->instmem.takedown = nv04_instmem_takedown;
271 engine->instmem.suspend = nv04_instmem_suspend;
272 engine->instmem.resume = nv04_instmem_resume;
273 engine->instmem.populate = nv04_instmem_populate;
274 engine->instmem.clear = nv04_instmem_clear;
275 engine->instmem.bind = nv04_instmem_bind;
276 engine->instmem.unbind = nv04_instmem_unbind;
Ben Skeggsf56cb862010-07-08 11:29:10 +1000277 engine->instmem.flush = nv04_instmem_flush;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000278 engine->mc.init = nv40_mc_init;
279 engine->mc.takedown = nv40_mc_takedown;
280 engine->timer.init = nv04_timer_init;
281 engine->timer.read = nv04_timer_read;
282 engine->timer.takedown = nv04_timer_takedown;
283 engine->fb.init = nv40_fb_init;
284 engine->fb.takedown = nv40_fb_takedown;
Francisco Jerezcb00f7c2009-12-16 12:12:27 +0100285 engine->fb.set_region_tiling = nv40_fb_set_region_tiling;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000286 engine->graph.grclass = nv40_graph_grclass;
287 engine->graph.init = nv40_graph_init;
288 engine->graph.takedown = nv40_graph_takedown;
289 engine->graph.fifo_access = nv04_graph_fifo_access;
290 engine->graph.channel = nv40_graph_channel;
291 engine->graph.create_context = nv40_graph_create_context;
292 engine->graph.destroy_context = nv40_graph_destroy_context;
293 engine->graph.load_context = nv40_graph_load_context;
294 engine->graph.unload_context = nv40_graph_unload_context;
Francisco Jerezcb00f7c2009-12-16 12:12:27 +0100295 engine->graph.set_region_tiling = nv40_graph_set_region_tiling;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000296 engine->fifo.channels = 32;
297 engine->fifo.init = nv40_fifo_init;
298 engine->fifo.takedown = nouveau_stub_takedown;
299 engine->fifo.disable = nv04_fifo_disable;
300 engine->fifo.enable = nv04_fifo_enable;
301 engine->fifo.reassign = nv04_fifo_reassign;
Francisco Jerez588d7d12009-12-13 20:07:42 +0100302 engine->fifo.cache_pull = nv04_fifo_cache_pull;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000303 engine->fifo.channel_id = nv10_fifo_channel_id;
304 engine->fifo.create_context = nv40_fifo_create_context;
305 engine->fifo.destroy_context = nv40_fifo_destroy_context;
306 engine->fifo.load_context = nv40_fifo_load_context;
307 engine->fifo.unload_context = nv40_fifo_unload_context;
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200308 engine->display.early_init = nv04_display_early_init;
309 engine->display.late_takedown = nv04_display_late_takedown;
310 engine->display.create = nv04_display_create;
311 engine->display.init = nv04_display_init;
312 engine->display.destroy = nv04_display_destroy;
Ben Skeggsee2e0132010-07-26 09:28:25 +1000313 engine->gpio.init = nouveau_stub_init;
314 engine->gpio.takedown = nouveau_stub_takedown;
315 engine->gpio.get = nv10_gpio_get;
316 engine->gpio.set = nv10_gpio_set;
317 engine->gpio.irq_enable = NULL;
Ben Skeggs442b6262010-09-16 16:25:26 +1000318 engine->pm.clock_get = nv04_pm_clock_get;
319 engine->pm.clock_pre = nv04_pm_clock_pre;
320 engine->pm.clock_set = nv04_pm_clock_set;
321 engine->pm.voltage_get = nouveau_voltage_gpio_get;
322 engine->pm.voltage_set = nouveau_voltage_gpio_set;
Francisco Jerez8155cac2010-09-23 20:58:38 +0200323 engine->pm.temp_get = nv40_temp_get;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000324 break;
325 case 0x50:
326 case 0x80: /* gotta love NVIDIA's consistency.. */
327 case 0x90:
328 case 0xA0:
329 engine->instmem.init = nv50_instmem_init;
330 engine->instmem.takedown = nv50_instmem_takedown;
331 engine->instmem.suspend = nv50_instmem_suspend;
332 engine->instmem.resume = nv50_instmem_resume;
333 engine->instmem.populate = nv50_instmem_populate;
334 engine->instmem.clear = nv50_instmem_clear;
335 engine->instmem.bind = nv50_instmem_bind;
336 engine->instmem.unbind = nv50_instmem_unbind;
Ben Skeggs734ee832010-07-15 11:02:54 +1000337 if (dev_priv->chipset == 0x50)
338 engine->instmem.flush = nv50_instmem_flush;
339 else
340 engine->instmem.flush = nv84_instmem_flush;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000341 engine->mc.init = nv50_mc_init;
342 engine->mc.takedown = nv50_mc_takedown;
343 engine->timer.init = nv04_timer_init;
344 engine->timer.read = nv04_timer_read;
345 engine->timer.takedown = nv04_timer_takedown;
Marcin Koƛcielnicki304424e2010-03-01 00:18:39 +0000346 engine->fb.init = nv50_fb_init;
347 engine->fb.takedown = nv50_fb_takedown;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000348 engine->graph.grclass = nv50_graph_grclass;
349 engine->graph.init = nv50_graph_init;
350 engine->graph.takedown = nv50_graph_takedown;
351 engine->graph.fifo_access = nv50_graph_fifo_access;
352 engine->graph.channel = nv50_graph_channel;
353 engine->graph.create_context = nv50_graph_create_context;
354 engine->graph.destroy_context = nv50_graph_destroy_context;
355 engine->graph.load_context = nv50_graph_load_context;
356 engine->graph.unload_context = nv50_graph_unload_context;
357 engine->fifo.channels = 128;
358 engine->fifo.init = nv50_fifo_init;
359 engine->fifo.takedown = nv50_fifo_takedown;
360 engine->fifo.disable = nv04_fifo_disable;
361 engine->fifo.enable = nv04_fifo_enable;
362 engine->fifo.reassign = nv04_fifo_reassign;
363 engine->fifo.channel_id = nv50_fifo_channel_id;
364 engine->fifo.create_context = nv50_fifo_create_context;
365 engine->fifo.destroy_context = nv50_fifo_destroy_context;
366 engine->fifo.load_context = nv50_fifo_load_context;
367 engine->fifo.unload_context = nv50_fifo_unload_context;
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200368 engine->display.early_init = nv50_display_early_init;
369 engine->display.late_takedown = nv50_display_late_takedown;
370 engine->display.create = nv50_display_create;
371 engine->display.init = nv50_display_init;
372 engine->display.destroy = nv50_display_destroy;
Ben Skeggsee2e0132010-07-26 09:28:25 +1000373 engine->gpio.init = nv50_gpio_init;
374 engine->gpio.takedown = nouveau_stub_takedown;
375 engine->gpio.get = nv50_gpio_get;
376 engine->gpio.set = nv50_gpio_set;
377 engine->gpio.irq_enable = nv50_gpio_irq_enable;
Ben Skeggsfade7ad2010-09-27 11:18:14 +1000378 switch (dev_priv->chipset) {
379 case 0xa3:
380 case 0xa5:
381 case 0xa8:
382 case 0xaf:
383 engine->pm.clock_get = nva3_pm_clock_get;
384 engine->pm.clock_pre = nva3_pm_clock_pre;
385 engine->pm.clock_set = nva3_pm_clock_set;
386 break;
387 default:
388 engine->pm.clock_get = nv50_pm_clock_get;
389 engine->pm.clock_pre = nv50_pm_clock_pre;
390 engine->pm.clock_set = nv50_pm_clock_set;
391 break;
392 }
Ben Skeggs02c30ca2010-09-16 16:17:35 +1000393 engine->pm.voltage_get = nouveau_voltage_gpio_get;
394 engine->pm.voltage_set = nouveau_voltage_gpio_set;
Francisco Jerez8155cac2010-09-23 20:58:38 +0200395 if (dev_priv->chipset >= 0x84)
396 engine->pm.temp_get = nv84_temp_get;
397 else
398 engine->pm.temp_get = nv40_temp_get;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000399 break;
Ben Skeggs4b223ee2010-08-03 10:00:56 +1000400 case 0xC0:
401 engine->instmem.init = nvc0_instmem_init;
402 engine->instmem.takedown = nvc0_instmem_takedown;
403 engine->instmem.suspend = nvc0_instmem_suspend;
404 engine->instmem.resume = nvc0_instmem_resume;
405 engine->instmem.populate = nvc0_instmem_populate;
406 engine->instmem.clear = nvc0_instmem_clear;
407 engine->instmem.bind = nvc0_instmem_bind;
408 engine->instmem.unbind = nvc0_instmem_unbind;
409 engine->instmem.flush = nvc0_instmem_flush;
410 engine->mc.init = nv50_mc_init;
411 engine->mc.takedown = nv50_mc_takedown;
412 engine->timer.init = nv04_timer_init;
413 engine->timer.read = nv04_timer_read;
414 engine->timer.takedown = nv04_timer_takedown;
415 engine->fb.init = nvc0_fb_init;
416 engine->fb.takedown = nvc0_fb_takedown;
417 engine->graph.grclass = NULL; //nvc0_graph_grclass;
418 engine->graph.init = nvc0_graph_init;
419 engine->graph.takedown = nvc0_graph_takedown;
420 engine->graph.fifo_access = nvc0_graph_fifo_access;
421 engine->graph.channel = nvc0_graph_channel;
422 engine->graph.create_context = nvc0_graph_create_context;
423 engine->graph.destroy_context = nvc0_graph_destroy_context;
424 engine->graph.load_context = nvc0_graph_load_context;
425 engine->graph.unload_context = nvc0_graph_unload_context;
426 engine->fifo.channels = 128;
427 engine->fifo.init = nvc0_fifo_init;
428 engine->fifo.takedown = nvc0_fifo_takedown;
429 engine->fifo.disable = nvc0_fifo_disable;
430 engine->fifo.enable = nvc0_fifo_enable;
431 engine->fifo.reassign = nvc0_fifo_reassign;
432 engine->fifo.channel_id = nvc0_fifo_channel_id;
433 engine->fifo.create_context = nvc0_fifo_create_context;
434 engine->fifo.destroy_context = nvc0_fifo_destroy_context;
435 engine->fifo.load_context = nvc0_fifo_load_context;
436 engine->fifo.unload_context = nvc0_fifo_unload_context;
437 engine->display.early_init = nv50_display_early_init;
438 engine->display.late_takedown = nv50_display_late_takedown;
439 engine->display.create = nv50_display_create;
440 engine->display.init = nv50_display_init;
441 engine->display.destroy = nv50_display_destroy;
442 engine->gpio.init = nv50_gpio_init;
443 engine->gpio.takedown = nouveau_stub_takedown;
444 engine->gpio.get = nv50_gpio_get;
445 engine->gpio.set = nv50_gpio_set;
446 engine->gpio.irq_enable = nv50_gpio_irq_enable;
447 break;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000448 default:
449 NV_ERROR(dev, "NV%02x unsupported\n", dev_priv->chipset);
450 return 1;
451 }
452
453 return 0;
454}
455
456static unsigned int
457nouveau_vga_set_decode(void *priv, bool state)
458{
Marcin Koƛcielnicki9967b942010-02-08 00:20:17 +0000459 struct drm_device *dev = priv;
460 struct drm_nouveau_private *dev_priv = dev->dev_private;
461
462 if (dev_priv->chipset >= 0x40)
463 nv_wr32(dev, 0x88054, state);
464 else
465 nv_wr32(dev, 0x1854, state);
466
Ben Skeggs6ee73862009-12-11 19:24:15 +1000467 if (state)
468 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
469 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
470 else
471 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
472}
473
Ben Skeggs0735f622009-12-16 14:28:55 +1000474static int
475nouveau_card_init_channel(struct drm_device *dev)
476{
477 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggsa8eaebc2010-09-01 15:24:31 +1000478 struct nouveau_gpuobj *gpuobj = NULL;
Ben Skeggs0735f622009-12-16 14:28:55 +1000479 int ret;
480
481 ret = nouveau_channel_alloc(dev, &dev_priv->channel,
Ben Skeggsa8eaebc2010-09-01 15:24:31 +1000482 (struct drm_file *)-2, NvDmaFB, NvDmaTT);
Ben Skeggs0735f622009-12-16 14:28:55 +1000483 if (ret)
484 return ret;
485
Ben Skeggs0735f622009-12-16 14:28:55 +1000486 ret = nouveau_gpuobj_dma_new(dev_priv->channel, NV_CLASS_DMA_IN_MEMORY,
Ben Skeggsa76fb4e2010-03-18 09:45:20 +1000487 0, dev_priv->vram_size,
Ben Skeggs0735f622009-12-16 14:28:55 +1000488 NV_DMA_ACCESS_RW, NV_DMA_TARGET_VIDMEM,
489 &gpuobj);
490 if (ret)
491 goto out_err;
492
Ben Skeggsa8eaebc2010-09-01 15:24:31 +1000493 ret = nouveau_ramht_insert(dev_priv->channel, NvDmaVRAM, gpuobj);
494 nouveau_gpuobj_ref(NULL, &gpuobj);
Ben Skeggs0735f622009-12-16 14:28:55 +1000495 if (ret)
496 goto out_err;
497
Ben Skeggs0735f622009-12-16 14:28:55 +1000498 ret = nouveau_gpuobj_gart_dma_new(dev_priv->channel, 0,
499 dev_priv->gart_info.aper_size,
500 NV_DMA_ACCESS_RW, &gpuobj, NULL);
501 if (ret)
502 goto out_err;
503
Ben Skeggsa8eaebc2010-09-01 15:24:31 +1000504 ret = nouveau_ramht_insert(dev_priv->channel, NvDmaGART, gpuobj);
505 nouveau_gpuobj_ref(NULL, &gpuobj);
Ben Skeggs0735f622009-12-16 14:28:55 +1000506 if (ret)
507 goto out_err;
508
509 return 0;
Ben Skeggsa8eaebc2010-09-01 15:24:31 +1000510
Ben Skeggs0735f622009-12-16 14:28:55 +1000511out_err:
Ben Skeggs0735f622009-12-16 14:28:55 +1000512 nouveau_channel_free(dev_priv->channel);
513 dev_priv->channel = NULL;
514 return ret;
515}
516
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000517static void nouveau_switcheroo_set_state(struct pci_dev *pdev,
518 enum vga_switcheroo_state state)
519{
Dave Airliefbf81762010-06-01 09:09:06 +1000520 struct drm_device *dev = pci_get_drvdata(pdev);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000521 pm_message_t pmm = { .event = PM_EVENT_SUSPEND };
522 if (state == VGA_SWITCHEROO_ON) {
523 printk(KERN_ERR "VGA switcheroo: switched nouveau on\n");
524 nouveau_pci_resume(pdev);
Dave Airliefbf81762010-06-01 09:09:06 +1000525 drm_kms_helper_poll_enable(dev);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000526 } else {
527 printk(KERN_ERR "VGA switcheroo: switched nouveau off\n");
Dave Airliefbf81762010-06-01 09:09:06 +1000528 drm_kms_helper_poll_disable(dev);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000529 nouveau_pci_suspend(pdev, pmm);
530 }
531}
532
533static bool nouveau_switcheroo_can_switch(struct pci_dev *pdev)
534{
535 struct drm_device *dev = pci_get_drvdata(pdev);
536 bool can_switch;
537
538 spin_lock(&dev->count_lock);
539 can_switch = (dev->open_count == 0);
540 spin_unlock(&dev->count_lock);
541 return can_switch;
542}
543
Ben Skeggs6ee73862009-12-11 19:24:15 +1000544int
545nouveau_card_init(struct drm_device *dev)
546{
547 struct drm_nouveau_private *dev_priv = dev->dev_private;
548 struct nouveau_engine *engine;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000549 int ret;
550
Ben Skeggs6ee73862009-12-11 19:24:15 +1000551 vga_client_register(dev->pdev, dev, NULL, nouveau_vga_set_decode);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000552 vga_switcheroo_register_client(dev->pdev, nouveau_switcheroo_set_state,
553 nouveau_switcheroo_can_switch);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000554
555 /* Initialise internal driver API hooks */
556 ret = nouveau_init_engine_ptrs(dev);
557 if (ret)
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000558 goto out;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000559 engine = &dev_priv->engine;
Maarten Maathuisff9e5272010-02-01 20:58:27 +0100560 spin_lock_init(&dev_priv->context_switch_lock);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000561
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200562 /* Make the CRTCs and I2C buses accessible */
563 ret = engine->display.early_init(dev);
564 if (ret)
565 goto out;
566
Ben Skeggs6ee73862009-12-11 19:24:15 +1000567 /* Parse BIOS tables / Run init tables if card not POSTed */
Ben Skeggscd0b0722010-06-01 15:56:22 +1000568 ret = nouveau_bios_init(dev);
569 if (ret)
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200570 goto out_display_early;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000571
Ben Skeggs330c5982010-09-16 15:39:49 +1000572 nouveau_pm_init(dev);
573
Ben Skeggsfbd2895e2010-09-01 15:24:34 +1000574 ret = nouveau_mem_vram_init(dev);
Ben Skeggsa76fb4e2010-03-18 09:45:20 +1000575 if (ret)
576 goto out_bios;
577
Ben Skeggs6ee73862009-12-11 19:24:15 +1000578 ret = nouveau_gpuobj_init(dev);
579 if (ret)
Ben Skeggsfbd2895e2010-09-01 15:24:34 +1000580 goto out_vram;
581
582 ret = engine->instmem.init(dev);
583 if (ret)
584 goto out_gpuobj;
585
586 ret = nouveau_mem_gart_init(dev);
587 if (ret)
588 goto out_instmem;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000589
590 /* PMC */
591 ret = engine->mc.init(dev);
592 if (ret)
Ben Skeggsfbd2895e2010-09-01 15:24:34 +1000593 goto out_gart;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000594
Ben Skeggsee2e0132010-07-26 09:28:25 +1000595 /* PGPIO */
596 ret = engine->gpio.init(dev);
597 if (ret)
598 goto out_mc;
599
Ben Skeggs6ee73862009-12-11 19:24:15 +1000600 /* PTIMER */
601 ret = engine->timer.init(dev);
602 if (ret)
Ben Skeggsee2e0132010-07-26 09:28:25 +1000603 goto out_gpio;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000604
605 /* PFB */
606 ret = engine->fb.init(dev);
607 if (ret)
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000608 goto out_timer;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000609
Marcin Koƛcielnickia32ed692010-01-26 14:00:42 +0000610 if (nouveau_noaccel)
611 engine->graph.accel_blocked = true;
612 else {
613 /* PGRAPH */
614 ret = engine->graph.init(dev);
615 if (ret)
616 goto out_fb;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000617
Marcin Koƛcielnickia32ed692010-01-26 14:00:42 +0000618 /* PFIFO */
619 ret = engine->fifo.init(dev);
620 if (ret)
621 goto out_graph;
622 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000623
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200624 ret = engine->display.create(dev);
Ben Skeggse88efe02010-07-09 10:56:08 +1000625 if (ret)
626 goto out_fifo;
627
Ben Skeggs6ee73862009-12-11 19:24:15 +1000628 /* this call irq_preinstall, register irq handler and
629 * call irq_postinstall
630 */
631 ret = drm_irq_install(dev);
632 if (ret)
Ben Skeggse88efe02010-07-09 10:56:08 +1000633 goto out_display;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000634
635 ret = drm_vblank_init(dev, 0);
636 if (ret)
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000637 goto out_irq;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000638
639 /* what about PVIDEO/PCRTC/PRAMDAC etc? */
640
Ben Skeggs0735f622009-12-16 14:28:55 +1000641 if (!engine->graph.accel_blocked) {
Francisco Jerez0c6c1c22010-09-22 00:58:54 +0200642 ret = nouveau_fence_init(dev);
Ben Skeggs0735f622009-12-16 14:28:55 +1000643 if (ret)
644 goto out_irq;
Francisco Jerez0c6c1c22010-09-22 00:58:54 +0200645
646 ret = nouveau_card_init_channel(dev);
647 if (ret)
648 goto out_fence;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000649 }
650
Ben Skeggs6ee73862009-12-11 19:24:15 +1000651 ret = nouveau_backlight_init(dev);
652 if (ret)
653 NV_ERROR(dev, "Error %d registering backlight\n", ret);
654
Ben Skeggscd0b0722010-06-01 15:56:22 +1000655 nouveau_fbcon_init(dev);
656 drm_kms_helper_poll_init(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000657 return 0;
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000658
Francisco Jerez0c6c1c22010-09-22 00:58:54 +0200659out_fence:
660 nouveau_fence_fini(dev);
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000661out_irq:
662 drm_irq_uninstall(dev);
Ben Skeggse88efe02010-07-09 10:56:08 +1000663out_display:
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200664 engine->display.destroy(dev);
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000665out_fifo:
Marcin Koƛcielnickia32ed692010-01-26 14:00:42 +0000666 if (!nouveau_noaccel)
667 engine->fifo.takedown(dev);
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000668out_graph:
Marcin Koƛcielnickia32ed692010-01-26 14:00:42 +0000669 if (!nouveau_noaccel)
670 engine->graph.takedown(dev);
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000671out_fb:
672 engine->fb.takedown(dev);
673out_timer:
674 engine->timer.takedown(dev);
Ben Skeggsee2e0132010-07-26 09:28:25 +1000675out_gpio:
676 engine->gpio.takedown(dev);
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000677out_mc:
678 engine->mc.takedown(dev);
Ben Skeggsfbd2895e2010-09-01 15:24:34 +1000679out_gart:
680 nouveau_mem_gart_fini(dev);
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000681out_instmem:
682 engine->instmem.takedown(dev);
Ben Skeggsfbd2895e2010-09-01 15:24:34 +1000683out_gpuobj:
684 nouveau_gpuobj_takedown(dev);
685out_vram:
686 nouveau_mem_vram_fini(dev);
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000687out_bios:
Ben Skeggs330c5982010-09-16 15:39:49 +1000688 nouveau_pm_fini(dev);
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000689 nouveau_bios_takedown(dev);
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200690out_display_early:
691 engine->display.late_takedown(dev);
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000692out:
693 vga_client_register(dev->pdev, NULL, NULL, NULL);
694 return ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000695}
696
697static void nouveau_card_takedown(struct drm_device *dev)
698{
699 struct drm_nouveau_private *dev_priv = dev->dev_private;
700 struct nouveau_engine *engine = &dev_priv->engine;
701
Ben Skeggsb6d3d872010-06-07 15:38:27 +1000702 nouveau_backlight_exit(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000703
Francisco Jerez0c6c1c22010-09-22 00:58:54 +0200704 if (!engine->graph.accel_blocked) {
705 nouveau_fence_fini(dev);
Ben Skeggsb6d3d872010-06-07 15:38:27 +1000706 nouveau_channel_free(dev_priv->channel);
707 dev_priv->channel = NULL;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000708 }
Ben Skeggsb6d3d872010-06-07 15:38:27 +1000709
710 if (!nouveau_noaccel) {
711 engine->fifo.takedown(dev);
712 engine->graph.takedown(dev);
713 }
714 engine->fb.takedown(dev);
715 engine->timer.takedown(dev);
Ben Skeggsee2e0132010-07-26 09:28:25 +1000716 engine->gpio.takedown(dev);
Ben Skeggsb6d3d872010-06-07 15:38:27 +1000717 engine->mc.takedown(dev);
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200718 engine->display.late_takedown(dev);
Ben Skeggsb6d3d872010-06-07 15:38:27 +1000719
720 mutex_lock(&dev->struct_mutex);
721 ttm_bo_clean_mm(&dev_priv->ttm.bdev, TTM_PL_VRAM);
722 ttm_bo_clean_mm(&dev_priv->ttm.bdev, TTM_PL_TT);
723 mutex_unlock(&dev->struct_mutex);
Ben Skeggsfbd2895e2010-09-01 15:24:34 +1000724 nouveau_mem_gart_fini(dev);
Ben Skeggsb6d3d872010-06-07 15:38:27 +1000725
Ben Skeggsb6d3d872010-06-07 15:38:27 +1000726 engine->instmem.takedown(dev);
Ben Skeggsfbd2895e2010-09-01 15:24:34 +1000727 nouveau_gpuobj_takedown(dev);
728 nouveau_mem_vram_fini(dev);
Ben Skeggsb6d3d872010-06-07 15:38:27 +1000729
730 drm_irq_uninstall(dev);
731
Ben Skeggs330c5982010-09-16 15:39:49 +1000732 nouveau_pm_fini(dev);
Ben Skeggsb6d3d872010-06-07 15:38:27 +1000733 nouveau_bios_takedown(dev);
734
735 vga_client_register(dev->pdev, NULL, NULL, NULL);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000736}
737
738/* here a client dies, release the stuff that was allocated for its
739 * file_priv */
740void nouveau_preclose(struct drm_device *dev, struct drm_file *file_priv)
741{
742 nouveau_channel_cleanup(dev, file_priv);
743}
744
745/* first module load, setup the mmio/fb mapping */
746/* KMS: we need mmio at load time, not when the first drm client opens. */
747int nouveau_firstopen(struct drm_device *dev)
748{
749 return 0;
750}
751
752/* if we have an OF card, copy vbios to RAMIN */
753static void nouveau_OF_copy_vbios_to_ramin(struct drm_device *dev)
754{
755#if defined(__powerpc__)
756 int size, i;
757 const uint32_t *bios;
758 struct device_node *dn = pci_device_to_OF_node(dev->pdev);
759 if (!dn) {
760 NV_INFO(dev, "Unable to get the OF node\n");
761 return;
762 }
763
764 bios = of_get_property(dn, "NVDA,BMP", &size);
765 if (bios) {
766 for (i = 0; i < size; i += 4)
767 nv_wi32(dev, i, bios[i/4]);
768 NV_INFO(dev, "OF bios successfully copied (%d bytes)\n", size);
769 } else {
770 NV_INFO(dev, "Unable to get the OF bios\n");
771 }
772#endif
773}
774
Marcin Slusarz06415c52010-05-16 17:29:56 +0200775static struct apertures_struct *nouveau_get_apertures(struct drm_device *dev)
776{
777 struct pci_dev *pdev = dev->pdev;
778 struct apertures_struct *aper = alloc_apertures(3);
779 if (!aper)
780 return NULL;
781
782 aper->ranges[0].base = pci_resource_start(pdev, 1);
783 aper->ranges[0].size = pci_resource_len(pdev, 1);
784 aper->count = 1;
785
786 if (pci_resource_len(pdev, 2)) {
787 aper->ranges[aper->count].base = pci_resource_start(pdev, 2);
788 aper->ranges[aper->count].size = pci_resource_len(pdev, 2);
789 aper->count++;
790 }
791
792 if (pci_resource_len(pdev, 3)) {
793 aper->ranges[aper->count].base = pci_resource_start(pdev, 3);
794 aper->ranges[aper->count].size = pci_resource_len(pdev, 3);
795 aper->count++;
796 }
797
798 return aper;
799}
800
801static int nouveau_remove_conflicting_drivers(struct drm_device *dev)
802{
803 struct drm_nouveau_private *dev_priv = dev->dev_private;
Marcin Slusarz3b9676e2010-05-16 17:33:09 +0200804 bool primary = false;
Marcin Slusarz06415c52010-05-16 17:29:56 +0200805 dev_priv->apertures = nouveau_get_apertures(dev);
806 if (!dev_priv->apertures)
807 return -ENOMEM;
808
Marcin Slusarz3b9676e2010-05-16 17:33:09 +0200809#ifdef CONFIG_X86
810 primary = dev->pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW;
811#endif
812
813 remove_conflicting_framebuffers(dev_priv->apertures, "nouveaufb", primary);
Marcin Slusarz06415c52010-05-16 17:29:56 +0200814 return 0;
815}
816
Ben Skeggs6ee73862009-12-11 19:24:15 +1000817int nouveau_load(struct drm_device *dev, unsigned long flags)
818{
819 struct drm_nouveau_private *dev_priv;
820 uint32_t reg0;
821 resource_size_t mmio_start_offs;
Ben Skeggscd0b0722010-06-01 15:56:22 +1000822 int ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000823
824 dev_priv = kzalloc(sizeof(*dev_priv), GFP_KERNEL);
Dan Carpentera0d069e2010-07-30 17:04:32 +0200825 if (!dev_priv) {
826 ret = -ENOMEM;
827 goto err_out;
828 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000829 dev->dev_private = dev_priv;
830 dev_priv->dev = dev;
831
832 dev_priv->flags = flags & NOUVEAU_FLAGS;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000833
834 NV_DEBUG(dev, "vendor: 0x%X device: 0x%X class: 0x%X\n",
835 dev->pci_vendor, dev->pci_device, dev->pdev->class);
836
Ben Skeggs6ee73862009-12-11 19:24:15 +1000837 dev_priv->wq = create_workqueue("nouveau");
Dan Carpentera0d069e2010-07-30 17:04:32 +0200838 if (!dev_priv->wq) {
839 ret = -EINVAL;
840 goto err_priv;
841 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000842
843 /* resource 0 is mmio regs */
844 /* resource 1 is linear FB */
845 /* resource 2 is RAMIN (mmio regs + 0x1000000) */
846 /* resource 6 is bios */
847
848 /* map the mmio regs */
849 mmio_start_offs = pci_resource_start(dev->pdev, 0);
850 dev_priv->mmio = ioremap(mmio_start_offs, 0x00800000);
851 if (!dev_priv->mmio) {
852 NV_ERROR(dev, "Unable to initialize the mmio mapping. "
853 "Please report your setup to " DRIVER_EMAIL "\n");
Dan Carpentera0d069e2010-07-30 17:04:32 +0200854 ret = -EINVAL;
855 goto err_wq;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000856 }
857 NV_DEBUG(dev, "regs mapped ok at 0x%llx\n",
858 (unsigned long long)mmio_start_offs);
859
860#ifdef __BIG_ENDIAN
861 /* Put the card in BE mode if it's not */
862 if (nv_rd32(dev, NV03_PMC_BOOT_1))
863 nv_wr32(dev, NV03_PMC_BOOT_1, 0x00000001);
864
865 DRM_MEMORYBARRIER();
866#endif
867
868 /* Time to determine the card architecture */
869 reg0 = nv_rd32(dev, NV03_PMC_BOOT_0);
870
871 /* We're dealing with >=NV10 */
872 if ((reg0 & 0x0f000000) > 0) {
873 /* Bit 27-20 contain the architecture in hex */
874 dev_priv->chipset = (reg0 & 0xff00000) >> 20;
875 /* NV04 or NV05 */
876 } else if ((reg0 & 0xff00fff0) == 0x20004000) {
Ben Skeggs1dee7a92010-01-07 13:47:57 +1000877 if (reg0 & 0x00f00000)
878 dev_priv->chipset = 0x05;
879 else
880 dev_priv->chipset = 0x04;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000881 } else
882 dev_priv->chipset = 0xff;
883
884 switch (dev_priv->chipset & 0xf0) {
885 case 0x00:
886 case 0x10:
887 case 0x20:
888 case 0x30:
889 dev_priv->card_type = dev_priv->chipset & 0xf0;
890 break;
891 case 0x40:
892 case 0x60:
893 dev_priv->card_type = NV_40;
894 break;
895 case 0x50:
896 case 0x80:
897 case 0x90:
898 case 0xa0:
899 dev_priv->card_type = NV_50;
900 break;
Ben Skeggs4b223ee2010-08-03 10:00:56 +1000901 case 0xc0:
902 dev_priv->card_type = NV_C0;
903 break;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000904 default:
905 NV_INFO(dev, "Unsupported chipset 0x%08x\n", reg0);
Dan Carpentera0d069e2010-07-30 17:04:32 +0200906 ret = -EINVAL;
907 goto err_mmio;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000908 }
909
910 NV_INFO(dev, "Detected an NV%2x generation card (0x%08x)\n",
911 dev_priv->card_type, reg0);
912
Ben Skeggscd0b0722010-06-01 15:56:22 +1000913 ret = nouveau_remove_conflicting_drivers(dev);
914 if (ret)
Dan Carpentera0d069e2010-07-30 17:04:32 +0200915 goto err_mmio;
Marcin Slusarz06415c52010-05-16 17:29:56 +0200916
Ben Skeggs6d696302010-06-02 10:16:24 +1000917 /* Map PRAMIN BAR, or on older cards, the aperture withing BAR0 */
Ben Skeggs6ee73862009-12-11 19:24:15 +1000918 if (dev_priv->card_type >= NV_40) {
919 int ramin_bar = 2;
920 if (pci_resource_len(dev->pdev, ramin_bar) == 0)
921 ramin_bar = 3;
922
923 dev_priv->ramin_size = pci_resource_len(dev->pdev, ramin_bar);
Ben Skeggs6d696302010-06-02 10:16:24 +1000924 dev_priv->ramin =
925 ioremap(pci_resource_start(dev->pdev, ramin_bar),
Ben Skeggs6ee73862009-12-11 19:24:15 +1000926 dev_priv->ramin_size);
927 if (!dev_priv->ramin) {
Ben Skeggs6d696302010-06-02 10:16:24 +1000928 NV_ERROR(dev, "Failed to PRAMIN BAR");
Dan Carpentera0d069e2010-07-30 17:04:32 +0200929 ret = -ENOMEM;
930 goto err_mmio;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000931 }
Ben Skeggs6d696302010-06-02 10:16:24 +1000932 } else {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000933 dev_priv->ramin_size = 1 * 1024 * 1024;
934 dev_priv->ramin = ioremap(mmio_start_offs + NV_RAMIN,
Ben Skeggs6d696302010-06-02 10:16:24 +1000935 dev_priv->ramin_size);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000936 if (!dev_priv->ramin) {
937 NV_ERROR(dev, "Failed to map BAR0 PRAMIN.\n");
Dan Carpentera0d069e2010-07-30 17:04:32 +0200938 ret = -ENOMEM;
939 goto err_mmio;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000940 }
941 }
942
943 nouveau_OF_copy_vbios_to_ramin(dev);
944
945 /* Special flags */
946 if (dev->pci_device == 0x01a0)
947 dev_priv->flags |= NV_NFORCE;
948 else if (dev->pci_device == 0x01f0)
949 dev_priv->flags |= NV_NFORCE2;
950
951 /* For kernel modesetting, init card now and bring up fbcon */
Ben Skeggscd0b0722010-06-01 15:56:22 +1000952 ret = nouveau_card_init(dev);
953 if (ret)
Dan Carpentera0d069e2010-07-30 17:04:32 +0200954 goto err_ramin;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000955
956 return 0;
Dan Carpentera0d069e2010-07-30 17:04:32 +0200957
958err_ramin:
959 iounmap(dev_priv->ramin);
960err_mmio:
961 iounmap(dev_priv->mmio);
962err_wq:
963 destroy_workqueue(dev_priv->wq);
964err_priv:
965 kfree(dev_priv);
966 dev->dev_private = NULL;
967err_out:
968 return ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000969}
970
Ben Skeggs6ee73862009-12-11 19:24:15 +1000971void nouveau_lastclose(struct drm_device *dev)
972{
Ben Skeggs6ee73862009-12-11 19:24:15 +1000973}
974
975int nouveau_unload(struct drm_device *dev)
976{
977 struct drm_nouveau_private *dev_priv = dev->dev_private;
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200978 struct nouveau_engine *engine = &dev_priv->engine;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000979
Ben Skeggscd0b0722010-06-01 15:56:22 +1000980 drm_kms_helper_poll_fini(dev);
981 nouveau_fbcon_fini(dev);
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200982 engine->display.destroy(dev);
Ben Skeggscd0b0722010-06-01 15:56:22 +1000983 nouveau_card_takedown(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000984
985 iounmap(dev_priv->mmio);
986 iounmap(dev_priv->ramin);
987
988 kfree(dev_priv);
989 dev->dev_private = NULL;
990 return 0;
991}
992
Ben Skeggs6ee73862009-12-11 19:24:15 +1000993int nouveau_ioctl_getparam(struct drm_device *dev, void *data,
994 struct drm_file *file_priv)
995{
996 struct drm_nouveau_private *dev_priv = dev->dev_private;
997 struct drm_nouveau_getparam *getparam = data;
998
Ben Skeggs6ee73862009-12-11 19:24:15 +1000999 switch (getparam->param) {
1000 case NOUVEAU_GETPARAM_CHIPSET_ID:
1001 getparam->value = dev_priv->chipset;
1002 break;
1003 case NOUVEAU_GETPARAM_PCI_VENDOR:
1004 getparam->value = dev->pci_vendor;
1005 break;
1006 case NOUVEAU_GETPARAM_PCI_DEVICE:
1007 getparam->value = dev->pci_device;
1008 break;
1009 case NOUVEAU_GETPARAM_BUS_TYPE:
1010 if (drm_device_is_agp(dev))
1011 getparam->value = NV_AGP;
1012 else if (drm_device_is_pcie(dev))
1013 getparam->value = NV_PCIE;
1014 else
1015 getparam->value = NV_PCI;
1016 break;
1017 case NOUVEAU_GETPARAM_FB_PHYSICAL:
1018 getparam->value = dev_priv->fb_phys;
1019 break;
1020 case NOUVEAU_GETPARAM_AGP_PHYSICAL:
1021 getparam->value = dev_priv->gart_info.aper_base;
1022 break;
1023 case NOUVEAU_GETPARAM_PCI_PHYSICAL:
1024 if (dev->sg) {
1025 getparam->value = (unsigned long)dev->sg->virtual;
1026 } else {
1027 NV_ERROR(dev, "Requested PCIGART address, "
1028 "while no PCIGART was created\n");
1029 return -EINVAL;
1030 }
1031 break;
1032 case NOUVEAU_GETPARAM_FB_SIZE:
1033 getparam->value = dev_priv->fb_available_size;
1034 break;
1035 case NOUVEAU_GETPARAM_AGP_SIZE:
1036 getparam->value = dev_priv->gart_info.aper_size;
1037 break;
1038 case NOUVEAU_GETPARAM_VM_VRAM_BASE:
1039 getparam->value = dev_priv->vm_vram_base;
1040 break;
Marcin Koƛcielnicki7fc74f12010-05-23 11:36:04 +00001041 case NOUVEAU_GETPARAM_PTIMER_TIME:
1042 getparam->value = dev_priv->engine.timer.read(dev);
1043 break;
Marcin Koƛcielnicki69c97002010-01-26 18:39:20 +00001044 case NOUVEAU_GETPARAM_GRAPH_UNITS:
1045 /* NV40 and NV50 versions are quite different, but register
1046 * address is the same. User is supposed to know the card
1047 * family anyway... */
1048 if (dev_priv->chipset >= 0x40) {
1049 getparam->value = nv_rd32(dev, NV40_PMC_GRAPH_UNITS);
1050 break;
1051 }
1052 /* FALLTHRU */
Ben Skeggs6ee73862009-12-11 19:24:15 +10001053 default:
1054 NV_ERROR(dev, "unknown parameter %lld\n", getparam->param);
1055 return -EINVAL;
1056 }
1057
1058 return 0;
1059}
1060
1061int
1062nouveau_ioctl_setparam(struct drm_device *dev, void *data,
1063 struct drm_file *file_priv)
1064{
1065 struct drm_nouveau_setparam *setparam = data;
1066
Ben Skeggs6ee73862009-12-11 19:24:15 +10001067 switch (setparam->param) {
1068 default:
1069 NV_ERROR(dev, "unknown parameter %lld\n", setparam->param);
1070 return -EINVAL;
1071 }
1072
1073 return 0;
1074}
1075
1076/* Wait until (value(reg) & mask) == val, up until timeout has hit */
1077bool nouveau_wait_until(struct drm_device *dev, uint64_t timeout,
1078 uint32_t reg, uint32_t mask, uint32_t val)
1079{
1080 struct drm_nouveau_private *dev_priv = dev->dev_private;
1081 struct nouveau_timer_engine *ptimer = &dev_priv->engine.timer;
1082 uint64_t start = ptimer->read(dev);
1083
1084 do {
1085 if ((nv_rd32(dev, reg) & mask) == val)
1086 return true;
1087 } while (ptimer->read(dev) - start < timeout);
1088
1089 return false;
1090}
1091
1092/* Waits for PGRAPH to go completely idle */
1093bool nouveau_wait_for_idle(struct drm_device *dev)
1094{
Francisco Jerez4b5c1522010-09-07 17:34:44 +02001095 if (!nv_wait(dev, NV04_PGRAPH_STATUS, 0xffffffff, 0x00000000)) {
Ben Skeggs6ee73862009-12-11 19:24:15 +10001096 NV_ERROR(dev, "PGRAPH idle timed out with status 0x%08x\n",
1097 nv_rd32(dev, NV04_PGRAPH_STATUS));
1098 return false;
1099 }
1100
1101 return true;
1102}
1103