blob: af203cc5d7add62416617800f2d82833041e25cb [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;
Francisco Jerez3945e472010-10-18 03:53:39 +0200140 engine->fifo.destroy_context = nv04_fifo_destroy_context;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000141 engine->fifo.load_context = nv10_fifo_load_context;
142 engine->fifo.unload_context = nv10_fifo_unload_context;
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200143 engine->display.early_init = nv04_display_early_init;
144 engine->display.late_takedown = nv04_display_late_takedown;
145 engine->display.create = nv04_display_create;
146 engine->display.init = nv04_display_init;
147 engine->display.destroy = nv04_display_destroy;
Ben Skeggsee2e0132010-07-26 09:28:25 +1000148 engine->gpio.init = nouveau_stub_init;
149 engine->gpio.takedown = nouveau_stub_takedown;
150 engine->gpio.get = nv10_gpio_get;
151 engine->gpio.set = nv10_gpio_set;
152 engine->gpio.irq_enable = NULL;
Ben Skeggs442b6262010-09-16 16:25:26 +1000153 engine->pm.clock_get = nv04_pm_clock_get;
154 engine->pm.clock_pre = nv04_pm_clock_pre;
155 engine->pm.clock_set = nv04_pm_clock_set;
Ben 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;
Francisco Jerez3945e472010-10-18 03:53:39 +0200194 engine->fifo.destroy_context = nv04_fifo_destroy_context;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000195 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;
Francisco Jerez3945e472010-10-18 03:53:39 +0200248 engine->fifo.destroy_context = nv04_fifo_destroy_context;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000249 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;
Francisco Jerez3945e472010-10-18 03:53:39 +0200305 engine->fifo.destroy_context = nv04_fifo_destroy_context;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000306 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;
Ben Skeggs56ac7472010-10-22 10:26:24 +1000357 if (dev_priv->chipset != 0x86)
358 engine->graph.tlb_flush = nv50_graph_tlb_flush;
359 else {
360 /* from what i can see nvidia do this on every
361 * pre-NVA3 board except NVAC, but, we've only
362 * ever seen problems on NV86
363 */
364 engine->graph.tlb_flush = nv86_graph_tlb_flush;
365 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000366 engine->fifo.channels = 128;
367 engine->fifo.init = nv50_fifo_init;
368 engine->fifo.takedown = nv50_fifo_takedown;
369 engine->fifo.disable = nv04_fifo_disable;
370 engine->fifo.enable = nv04_fifo_enable;
371 engine->fifo.reassign = nv04_fifo_reassign;
372 engine->fifo.channel_id = nv50_fifo_channel_id;
373 engine->fifo.create_context = nv50_fifo_create_context;
374 engine->fifo.destroy_context = nv50_fifo_destroy_context;
375 engine->fifo.load_context = nv50_fifo_load_context;
376 engine->fifo.unload_context = nv50_fifo_unload_context;
Ben Skeggs56ac7472010-10-22 10:26:24 +1000377 engine->fifo.tlb_flush = nv50_fifo_tlb_flush;
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200378 engine->display.early_init = nv50_display_early_init;
379 engine->display.late_takedown = nv50_display_late_takedown;
380 engine->display.create = nv50_display_create;
381 engine->display.init = nv50_display_init;
382 engine->display.destroy = nv50_display_destroy;
Ben Skeggsee2e0132010-07-26 09:28:25 +1000383 engine->gpio.init = nv50_gpio_init;
384 engine->gpio.takedown = nouveau_stub_takedown;
385 engine->gpio.get = nv50_gpio_get;
386 engine->gpio.set = nv50_gpio_set;
387 engine->gpio.irq_enable = nv50_gpio_irq_enable;
Ben Skeggsfade7ad2010-09-27 11:18:14 +1000388 switch (dev_priv->chipset) {
389 case 0xa3:
390 case 0xa5:
391 case 0xa8:
392 case 0xaf:
393 engine->pm.clock_get = nva3_pm_clock_get;
394 engine->pm.clock_pre = nva3_pm_clock_pre;
395 engine->pm.clock_set = nva3_pm_clock_set;
396 break;
397 default:
398 engine->pm.clock_get = nv50_pm_clock_get;
399 engine->pm.clock_pre = nv50_pm_clock_pre;
400 engine->pm.clock_set = nv50_pm_clock_set;
401 break;
402 }
Ben Skeggs02c30ca2010-09-16 16:17:35 +1000403 engine->pm.voltage_get = nouveau_voltage_gpio_get;
404 engine->pm.voltage_set = nouveau_voltage_gpio_set;
Francisco Jerez8155cac2010-09-23 20:58:38 +0200405 if (dev_priv->chipset >= 0x84)
406 engine->pm.temp_get = nv84_temp_get;
407 else
408 engine->pm.temp_get = nv40_temp_get;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000409 break;
Ben Skeggs4b223ee2010-08-03 10:00:56 +1000410 case 0xC0:
411 engine->instmem.init = nvc0_instmem_init;
412 engine->instmem.takedown = nvc0_instmem_takedown;
413 engine->instmem.suspend = nvc0_instmem_suspend;
414 engine->instmem.resume = nvc0_instmem_resume;
415 engine->instmem.populate = nvc0_instmem_populate;
416 engine->instmem.clear = nvc0_instmem_clear;
417 engine->instmem.bind = nvc0_instmem_bind;
418 engine->instmem.unbind = nvc0_instmem_unbind;
419 engine->instmem.flush = nvc0_instmem_flush;
420 engine->mc.init = nv50_mc_init;
421 engine->mc.takedown = nv50_mc_takedown;
422 engine->timer.init = nv04_timer_init;
423 engine->timer.read = nv04_timer_read;
424 engine->timer.takedown = nv04_timer_takedown;
425 engine->fb.init = nvc0_fb_init;
426 engine->fb.takedown = nvc0_fb_takedown;
427 engine->graph.grclass = NULL; //nvc0_graph_grclass;
428 engine->graph.init = nvc0_graph_init;
429 engine->graph.takedown = nvc0_graph_takedown;
430 engine->graph.fifo_access = nvc0_graph_fifo_access;
431 engine->graph.channel = nvc0_graph_channel;
432 engine->graph.create_context = nvc0_graph_create_context;
433 engine->graph.destroy_context = nvc0_graph_destroy_context;
434 engine->graph.load_context = nvc0_graph_load_context;
435 engine->graph.unload_context = nvc0_graph_unload_context;
436 engine->fifo.channels = 128;
437 engine->fifo.init = nvc0_fifo_init;
438 engine->fifo.takedown = nvc0_fifo_takedown;
439 engine->fifo.disable = nvc0_fifo_disable;
440 engine->fifo.enable = nvc0_fifo_enable;
441 engine->fifo.reassign = nvc0_fifo_reassign;
442 engine->fifo.channel_id = nvc0_fifo_channel_id;
443 engine->fifo.create_context = nvc0_fifo_create_context;
444 engine->fifo.destroy_context = nvc0_fifo_destroy_context;
445 engine->fifo.load_context = nvc0_fifo_load_context;
446 engine->fifo.unload_context = nvc0_fifo_unload_context;
447 engine->display.early_init = nv50_display_early_init;
448 engine->display.late_takedown = nv50_display_late_takedown;
449 engine->display.create = nv50_display_create;
450 engine->display.init = nv50_display_init;
451 engine->display.destroy = nv50_display_destroy;
452 engine->gpio.init = nv50_gpio_init;
453 engine->gpio.takedown = nouveau_stub_takedown;
454 engine->gpio.get = nv50_gpio_get;
455 engine->gpio.set = nv50_gpio_set;
456 engine->gpio.irq_enable = nv50_gpio_irq_enable;
457 break;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000458 default:
459 NV_ERROR(dev, "NV%02x unsupported\n", dev_priv->chipset);
460 return 1;
461 }
462
463 return 0;
464}
465
466static unsigned int
467nouveau_vga_set_decode(void *priv, bool state)
468{
Marcin Koƛcielnicki9967b942010-02-08 00:20:17 +0000469 struct drm_device *dev = priv;
470 struct drm_nouveau_private *dev_priv = dev->dev_private;
471
472 if (dev_priv->chipset >= 0x40)
473 nv_wr32(dev, 0x88054, state);
474 else
475 nv_wr32(dev, 0x1854, state);
476
Ben Skeggs6ee73862009-12-11 19:24:15 +1000477 if (state)
478 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
479 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
480 else
481 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
482}
483
Ben Skeggs0735f622009-12-16 14:28:55 +1000484static int
485nouveau_card_init_channel(struct drm_device *dev)
486{
487 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggsa8eaebc2010-09-01 15:24:31 +1000488 struct nouveau_gpuobj *gpuobj = NULL;
Ben Skeggs0735f622009-12-16 14:28:55 +1000489 int ret;
490
491 ret = nouveau_channel_alloc(dev, &dev_priv->channel,
Ben Skeggsa8eaebc2010-09-01 15:24:31 +1000492 (struct drm_file *)-2, NvDmaFB, NvDmaTT);
Ben Skeggs0735f622009-12-16 14:28:55 +1000493 if (ret)
494 return ret;
495
Ben Skeggs0735f622009-12-16 14:28:55 +1000496 ret = nouveau_gpuobj_dma_new(dev_priv->channel, NV_CLASS_DMA_IN_MEMORY,
Ben Skeggsa76fb4e2010-03-18 09:45:20 +1000497 0, dev_priv->vram_size,
Ben Skeggs0735f622009-12-16 14:28:55 +1000498 NV_DMA_ACCESS_RW, NV_DMA_TARGET_VIDMEM,
499 &gpuobj);
500 if (ret)
501 goto out_err;
502
Ben Skeggsa8eaebc2010-09-01 15:24:31 +1000503 ret = nouveau_ramht_insert(dev_priv->channel, NvDmaVRAM, gpuobj);
504 nouveau_gpuobj_ref(NULL, &gpuobj);
Ben Skeggs0735f622009-12-16 14:28:55 +1000505 if (ret)
506 goto out_err;
507
Ben Skeggs0735f622009-12-16 14:28:55 +1000508 ret = nouveau_gpuobj_gart_dma_new(dev_priv->channel, 0,
509 dev_priv->gart_info.aper_size,
510 NV_DMA_ACCESS_RW, &gpuobj, NULL);
511 if (ret)
512 goto out_err;
513
Ben Skeggsa8eaebc2010-09-01 15:24:31 +1000514 ret = nouveau_ramht_insert(dev_priv->channel, NvDmaGART, gpuobj);
515 nouveau_gpuobj_ref(NULL, &gpuobj);
Ben Skeggs0735f622009-12-16 14:28:55 +1000516 if (ret)
517 goto out_err;
518
Ben Skeggscff5c132010-10-06 16:16:59 +1000519 mutex_unlock(&dev_priv->channel->mutex);
Ben Skeggs0735f622009-12-16 14:28:55 +1000520 return 0;
Ben Skeggsa8eaebc2010-09-01 15:24:31 +1000521
Ben Skeggs0735f622009-12-16 14:28:55 +1000522out_err:
Ben Skeggscff5c132010-10-06 16:16:59 +1000523 nouveau_channel_put(&dev_priv->channel);
Ben Skeggs0735f622009-12-16 14:28:55 +1000524 return ret;
525}
526
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000527static void nouveau_switcheroo_set_state(struct pci_dev *pdev,
528 enum vga_switcheroo_state state)
529{
Dave Airliefbf81762010-06-01 09:09:06 +1000530 struct drm_device *dev = pci_get_drvdata(pdev);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000531 pm_message_t pmm = { .event = PM_EVENT_SUSPEND };
532 if (state == VGA_SWITCHEROO_ON) {
533 printk(KERN_ERR "VGA switcheroo: switched nouveau on\n");
534 nouveau_pci_resume(pdev);
Dave Airliefbf81762010-06-01 09:09:06 +1000535 drm_kms_helper_poll_enable(dev);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000536 } else {
537 printk(KERN_ERR "VGA switcheroo: switched nouveau off\n");
Dave Airliefbf81762010-06-01 09:09:06 +1000538 drm_kms_helper_poll_disable(dev);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000539 nouveau_pci_suspend(pdev, pmm);
540 }
541}
542
543static bool nouveau_switcheroo_can_switch(struct pci_dev *pdev)
544{
545 struct drm_device *dev = pci_get_drvdata(pdev);
546 bool can_switch;
547
548 spin_lock(&dev->count_lock);
549 can_switch = (dev->open_count == 0);
550 spin_unlock(&dev->count_lock);
551 return can_switch;
552}
553
Ben Skeggs6ee73862009-12-11 19:24:15 +1000554int
555nouveau_card_init(struct drm_device *dev)
556{
557 struct drm_nouveau_private *dev_priv = dev->dev_private;
558 struct nouveau_engine *engine;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000559 int ret;
560
Ben Skeggs6ee73862009-12-11 19:24:15 +1000561 vga_client_register(dev->pdev, dev, NULL, nouveau_vga_set_decode);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000562 vga_switcheroo_register_client(dev->pdev, nouveau_switcheroo_set_state,
563 nouveau_switcheroo_can_switch);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000564
565 /* Initialise internal driver API hooks */
566 ret = nouveau_init_engine_ptrs(dev);
567 if (ret)
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000568 goto out;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000569 engine = &dev_priv->engine;
Ben Skeggscff5c132010-10-06 16:16:59 +1000570 spin_lock_init(&dev_priv->channels.lock);
Maarten Maathuisff9e5272010-02-01 20:58:27 +0100571 spin_lock_init(&dev_priv->context_switch_lock);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000572
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200573 /* Make the CRTCs and I2C buses accessible */
574 ret = engine->display.early_init(dev);
575 if (ret)
576 goto out;
577
Ben Skeggs6ee73862009-12-11 19:24:15 +1000578 /* Parse BIOS tables / Run init tables if card not POSTed */
Ben Skeggscd0b0722010-06-01 15:56:22 +1000579 ret = nouveau_bios_init(dev);
580 if (ret)
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200581 goto out_display_early;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000582
Ben Skeggs330c5982010-09-16 15:39:49 +1000583 nouveau_pm_init(dev);
584
Ben Skeggsfbd28952010-09-01 15:24:34 +1000585 ret = nouveau_mem_vram_init(dev);
Ben Skeggsa76fb4e2010-03-18 09:45:20 +1000586 if (ret)
587 goto out_bios;
588
Ben Skeggs6ee73862009-12-11 19:24:15 +1000589 ret = nouveau_gpuobj_init(dev);
590 if (ret)
Ben Skeggsfbd28952010-09-01 15:24:34 +1000591 goto out_vram;
592
593 ret = engine->instmem.init(dev);
594 if (ret)
595 goto out_gpuobj;
596
597 ret = nouveau_mem_gart_init(dev);
598 if (ret)
599 goto out_instmem;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000600
601 /* PMC */
602 ret = engine->mc.init(dev);
603 if (ret)
Ben Skeggsfbd28952010-09-01 15:24:34 +1000604 goto out_gart;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000605
Ben Skeggsee2e0132010-07-26 09:28:25 +1000606 /* PGPIO */
607 ret = engine->gpio.init(dev);
608 if (ret)
609 goto out_mc;
610
Ben Skeggs6ee73862009-12-11 19:24:15 +1000611 /* PTIMER */
612 ret = engine->timer.init(dev);
613 if (ret)
Ben Skeggsee2e0132010-07-26 09:28:25 +1000614 goto out_gpio;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000615
616 /* PFB */
617 ret = engine->fb.init(dev);
618 if (ret)
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000619 goto out_timer;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000620
Marcin Koƛcielnickia32ed692010-01-26 14:00:42 +0000621 if (nouveau_noaccel)
622 engine->graph.accel_blocked = true;
623 else {
624 /* PGRAPH */
625 ret = engine->graph.init(dev);
626 if (ret)
627 goto out_fb;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000628
Marcin Koƛcielnickia32ed692010-01-26 14:00:42 +0000629 /* PFIFO */
630 ret = engine->fifo.init(dev);
631 if (ret)
632 goto out_graph;
633 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000634
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200635 ret = engine->display.create(dev);
Ben Skeggse88efe02010-07-09 10:56:08 +1000636 if (ret)
637 goto out_fifo;
638
Ben Skeggs6ee73862009-12-11 19:24:15 +1000639 /* this call irq_preinstall, register irq handler and
640 * call irq_postinstall
641 */
642 ret = drm_irq_install(dev);
643 if (ret)
Ben Skeggse88efe02010-07-09 10:56:08 +1000644 goto out_display;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000645
646 ret = drm_vblank_init(dev, 0);
647 if (ret)
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000648 goto out_irq;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000649
650 /* what about PVIDEO/PCRTC/PRAMDAC etc? */
651
Ben Skeggs0735f622009-12-16 14:28:55 +1000652 if (!engine->graph.accel_blocked) {
Francisco Jerez0c6c1c22010-09-22 00:58:54 +0200653 ret = nouveau_fence_init(dev);
Ben Skeggs0735f622009-12-16 14:28:55 +1000654 if (ret)
655 goto out_irq;
Francisco Jerez0c6c1c22010-09-22 00:58:54 +0200656
657 ret = nouveau_card_init_channel(dev);
658 if (ret)
659 goto out_fence;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000660 }
661
Ben Skeggs6ee73862009-12-11 19:24:15 +1000662 ret = nouveau_backlight_init(dev);
663 if (ret)
664 NV_ERROR(dev, "Error %d registering backlight\n", ret);
665
Ben Skeggscd0b0722010-06-01 15:56:22 +1000666 nouveau_fbcon_init(dev);
667 drm_kms_helper_poll_init(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000668 return 0;
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000669
Francisco Jerez0c6c1c22010-09-22 00:58:54 +0200670out_fence:
671 nouveau_fence_fini(dev);
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000672out_irq:
673 drm_irq_uninstall(dev);
Ben Skeggse88efe02010-07-09 10:56:08 +1000674out_display:
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200675 engine->display.destroy(dev);
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000676out_fifo:
Marcin Koƛcielnickia32ed692010-01-26 14:00:42 +0000677 if (!nouveau_noaccel)
678 engine->fifo.takedown(dev);
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000679out_graph:
Marcin Koƛcielnickia32ed692010-01-26 14:00:42 +0000680 if (!nouveau_noaccel)
681 engine->graph.takedown(dev);
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000682out_fb:
683 engine->fb.takedown(dev);
684out_timer:
685 engine->timer.takedown(dev);
Ben Skeggsee2e0132010-07-26 09:28:25 +1000686out_gpio:
687 engine->gpio.takedown(dev);
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000688out_mc:
689 engine->mc.takedown(dev);
Ben Skeggsfbd28952010-09-01 15:24:34 +1000690out_gart:
691 nouveau_mem_gart_fini(dev);
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000692out_instmem:
693 engine->instmem.takedown(dev);
Ben Skeggsfbd28952010-09-01 15:24:34 +1000694out_gpuobj:
695 nouveau_gpuobj_takedown(dev);
696out_vram:
697 nouveau_mem_vram_fini(dev);
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000698out_bios:
Ben Skeggs330c5982010-09-16 15:39:49 +1000699 nouveau_pm_fini(dev);
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000700 nouveau_bios_takedown(dev);
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200701out_display_early:
702 engine->display.late_takedown(dev);
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000703out:
704 vga_client_register(dev->pdev, NULL, NULL, NULL);
705 return ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000706}
707
708static void nouveau_card_takedown(struct drm_device *dev)
709{
710 struct drm_nouveau_private *dev_priv = dev->dev_private;
711 struct nouveau_engine *engine = &dev_priv->engine;
712
Ben Skeggsb6d3d872010-06-07 15:38:27 +1000713 nouveau_backlight_exit(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000714
Francisco Jerez0c6c1c22010-09-22 00:58:54 +0200715 if (!engine->graph.accel_blocked) {
716 nouveau_fence_fini(dev);
Francisco Jerez36c952e2010-10-18 03:01:34 +0200717 nouveau_channel_put_unlocked(&dev_priv->channel);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000718 }
Ben Skeggsb6d3d872010-06-07 15:38:27 +1000719
720 if (!nouveau_noaccel) {
721 engine->fifo.takedown(dev);
722 engine->graph.takedown(dev);
723 }
724 engine->fb.takedown(dev);
725 engine->timer.takedown(dev);
Ben Skeggsee2e0132010-07-26 09:28:25 +1000726 engine->gpio.takedown(dev);
Ben Skeggsb6d3d872010-06-07 15:38:27 +1000727 engine->mc.takedown(dev);
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200728 engine->display.late_takedown(dev);
Ben Skeggsb6d3d872010-06-07 15:38:27 +1000729
730 mutex_lock(&dev->struct_mutex);
731 ttm_bo_clean_mm(&dev_priv->ttm.bdev, TTM_PL_VRAM);
732 ttm_bo_clean_mm(&dev_priv->ttm.bdev, TTM_PL_TT);
733 mutex_unlock(&dev->struct_mutex);
Ben Skeggsfbd28952010-09-01 15:24:34 +1000734 nouveau_mem_gart_fini(dev);
Ben Skeggsb6d3d872010-06-07 15:38:27 +1000735
Ben Skeggsb6d3d872010-06-07 15:38:27 +1000736 engine->instmem.takedown(dev);
Ben Skeggsfbd28952010-09-01 15:24:34 +1000737 nouveau_gpuobj_takedown(dev);
738 nouveau_mem_vram_fini(dev);
Ben Skeggsb6d3d872010-06-07 15:38:27 +1000739
740 drm_irq_uninstall(dev);
741
Ben Skeggs330c5982010-09-16 15:39:49 +1000742 nouveau_pm_fini(dev);
Ben Skeggsb6d3d872010-06-07 15:38:27 +1000743 nouveau_bios_takedown(dev);
744
745 vga_client_register(dev->pdev, NULL, NULL, NULL);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000746}
747
748/* here a client dies, release the stuff that was allocated for its
749 * file_priv */
750void nouveau_preclose(struct drm_device *dev, struct drm_file *file_priv)
751{
752 nouveau_channel_cleanup(dev, file_priv);
753}
754
755/* first module load, setup the mmio/fb mapping */
756/* KMS: we need mmio at load time, not when the first drm client opens. */
757int nouveau_firstopen(struct drm_device *dev)
758{
759 return 0;
760}
761
762/* if we have an OF card, copy vbios to RAMIN */
763static void nouveau_OF_copy_vbios_to_ramin(struct drm_device *dev)
764{
765#if defined(__powerpc__)
766 int size, i;
767 const uint32_t *bios;
768 struct device_node *dn = pci_device_to_OF_node(dev->pdev);
769 if (!dn) {
770 NV_INFO(dev, "Unable to get the OF node\n");
771 return;
772 }
773
774 bios = of_get_property(dn, "NVDA,BMP", &size);
775 if (bios) {
776 for (i = 0; i < size; i += 4)
777 nv_wi32(dev, i, bios[i/4]);
778 NV_INFO(dev, "OF bios successfully copied (%d bytes)\n", size);
779 } else {
780 NV_INFO(dev, "Unable to get the OF bios\n");
781 }
782#endif
783}
784
Marcin Slusarz06415c52010-05-16 17:29:56 +0200785static struct apertures_struct *nouveau_get_apertures(struct drm_device *dev)
786{
787 struct pci_dev *pdev = dev->pdev;
788 struct apertures_struct *aper = alloc_apertures(3);
789 if (!aper)
790 return NULL;
791
792 aper->ranges[0].base = pci_resource_start(pdev, 1);
793 aper->ranges[0].size = pci_resource_len(pdev, 1);
794 aper->count = 1;
795
796 if (pci_resource_len(pdev, 2)) {
797 aper->ranges[aper->count].base = pci_resource_start(pdev, 2);
798 aper->ranges[aper->count].size = pci_resource_len(pdev, 2);
799 aper->count++;
800 }
801
802 if (pci_resource_len(pdev, 3)) {
803 aper->ranges[aper->count].base = pci_resource_start(pdev, 3);
804 aper->ranges[aper->count].size = pci_resource_len(pdev, 3);
805 aper->count++;
806 }
807
808 return aper;
809}
810
811static int nouveau_remove_conflicting_drivers(struct drm_device *dev)
812{
813 struct drm_nouveau_private *dev_priv = dev->dev_private;
Marcin Slusarz3b9676e2010-05-16 17:33:09 +0200814 bool primary = false;
Marcin Slusarz06415c52010-05-16 17:29:56 +0200815 dev_priv->apertures = nouveau_get_apertures(dev);
816 if (!dev_priv->apertures)
817 return -ENOMEM;
818
Marcin Slusarz3b9676e2010-05-16 17:33:09 +0200819#ifdef CONFIG_X86
820 primary = dev->pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW;
821#endif
822
823 remove_conflicting_framebuffers(dev_priv->apertures, "nouveaufb", primary);
Marcin Slusarz06415c52010-05-16 17:29:56 +0200824 return 0;
825}
826
Ben Skeggs6ee73862009-12-11 19:24:15 +1000827int nouveau_load(struct drm_device *dev, unsigned long flags)
828{
829 struct drm_nouveau_private *dev_priv;
830 uint32_t reg0;
831 resource_size_t mmio_start_offs;
Ben Skeggscd0b0722010-06-01 15:56:22 +1000832 int ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000833
834 dev_priv = kzalloc(sizeof(*dev_priv), GFP_KERNEL);
Dan Carpentera0d069e2010-07-30 17:04:32 +0200835 if (!dev_priv) {
836 ret = -ENOMEM;
837 goto err_out;
838 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000839 dev->dev_private = dev_priv;
840 dev_priv->dev = dev;
841
842 dev_priv->flags = flags & NOUVEAU_FLAGS;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000843
844 NV_DEBUG(dev, "vendor: 0x%X device: 0x%X class: 0x%X\n",
845 dev->pci_vendor, dev->pci_device, dev->pdev->class);
846
Ben Skeggs6ee73862009-12-11 19:24:15 +1000847 dev_priv->wq = create_workqueue("nouveau");
Dan Carpentera0d069e2010-07-30 17:04:32 +0200848 if (!dev_priv->wq) {
849 ret = -EINVAL;
850 goto err_priv;
851 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000852
853 /* resource 0 is mmio regs */
854 /* resource 1 is linear FB */
855 /* resource 2 is RAMIN (mmio regs + 0x1000000) */
856 /* resource 6 is bios */
857
858 /* map the mmio regs */
859 mmio_start_offs = pci_resource_start(dev->pdev, 0);
860 dev_priv->mmio = ioremap(mmio_start_offs, 0x00800000);
861 if (!dev_priv->mmio) {
862 NV_ERROR(dev, "Unable to initialize the mmio mapping. "
863 "Please report your setup to " DRIVER_EMAIL "\n");
Dan Carpentera0d069e2010-07-30 17:04:32 +0200864 ret = -EINVAL;
865 goto err_wq;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000866 }
867 NV_DEBUG(dev, "regs mapped ok at 0x%llx\n",
868 (unsigned long long)mmio_start_offs);
869
870#ifdef __BIG_ENDIAN
871 /* Put the card in BE mode if it's not */
872 if (nv_rd32(dev, NV03_PMC_BOOT_1))
873 nv_wr32(dev, NV03_PMC_BOOT_1, 0x00000001);
874
875 DRM_MEMORYBARRIER();
876#endif
877
878 /* Time to determine the card architecture */
879 reg0 = nv_rd32(dev, NV03_PMC_BOOT_0);
880
881 /* We're dealing with >=NV10 */
882 if ((reg0 & 0x0f000000) > 0) {
883 /* Bit 27-20 contain the architecture in hex */
884 dev_priv->chipset = (reg0 & 0xff00000) >> 20;
885 /* NV04 or NV05 */
886 } else if ((reg0 & 0xff00fff0) == 0x20004000) {
Ben Skeggs1dee7a92010-01-07 13:47:57 +1000887 if (reg0 & 0x00f00000)
888 dev_priv->chipset = 0x05;
889 else
890 dev_priv->chipset = 0x04;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000891 } else
892 dev_priv->chipset = 0xff;
893
894 switch (dev_priv->chipset & 0xf0) {
895 case 0x00:
896 case 0x10:
897 case 0x20:
898 case 0x30:
899 dev_priv->card_type = dev_priv->chipset & 0xf0;
900 break;
901 case 0x40:
902 case 0x60:
903 dev_priv->card_type = NV_40;
904 break;
905 case 0x50:
906 case 0x80:
907 case 0x90:
908 case 0xa0:
909 dev_priv->card_type = NV_50;
910 break;
Ben Skeggs4b223ee2010-08-03 10:00:56 +1000911 case 0xc0:
912 dev_priv->card_type = NV_C0;
913 break;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000914 default:
915 NV_INFO(dev, "Unsupported chipset 0x%08x\n", reg0);
Dan Carpentera0d069e2010-07-30 17:04:32 +0200916 ret = -EINVAL;
917 goto err_mmio;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000918 }
919
920 NV_INFO(dev, "Detected an NV%2x generation card (0x%08x)\n",
921 dev_priv->card_type, reg0);
922
Ben Skeggscd0b0722010-06-01 15:56:22 +1000923 ret = nouveau_remove_conflicting_drivers(dev);
924 if (ret)
Dan Carpentera0d069e2010-07-30 17:04:32 +0200925 goto err_mmio;
Marcin Slusarz06415c52010-05-16 17:29:56 +0200926
Ben Skeggs6d696302010-06-02 10:16:24 +1000927 /* Map PRAMIN BAR, or on older cards, the aperture withing BAR0 */
Ben Skeggs6ee73862009-12-11 19:24:15 +1000928 if (dev_priv->card_type >= NV_40) {
929 int ramin_bar = 2;
930 if (pci_resource_len(dev->pdev, ramin_bar) == 0)
931 ramin_bar = 3;
932
933 dev_priv->ramin_size = pci_resource_len(dev->pdev, ramin_bar);
Ben Skeggs6d696302010-06-02 10:16:24 +1000934 dev_priv->ramin =
935 ioremap(pci_resource_start(dev->pdev, ramin_bar),
Ben Skeggs6ee73862009-12-11 19:24:15 +1000936 dev_priv->ramin_size);
937 if (!dev_priv->ramin) {
Ben Skeggs6d696302010-06-02 10:16:24 +1000938 NV_ERROR(dev, "Failed to PRAMIN BAR");
Dan Carpentera0d069e2010-07-30 17:04:32 +0200939 ret = -ENOMEM;
940 goto err_mmio;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000941 }
Ben Skeggs6d696302010-06-02 10:16:24 +1000942 } else {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000943 dev_priv->ramin_size = 1 * 1024 * 1024;
944 dev_priv->ramin = ioremap(mmio_start_offs + NV_RAMIN,
Ben Skeggs6d696302010-06-02 10:16:24 +1000945 dev_priv->ramin_size);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000946 if (!dev_priv->ramin) {
947 NV_ERROR(dev, "Failed to map BAR0 PRAMIN.\n");
Dan Carpentera0d069e2010-07-30 17:04:32 +0200948 ret = -ENOMEM;
949 goto err_mmio;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000950 }
951 }
952
953 nouveau_OF_copy_vbios_to_ramin(dev);
954
955 /* Special flags */
956 if (dev->pci_device == 0x01a0)
957 dev_priv->flags |= NV_NFORCE;
958 else if (dev->pci_device == 0x01f0)
959 dev_priv->flags |= NV_NFORCE2;
960
961 /* For kernel modesetting, init card now and bring up fbcon */
Ben Skeggscd0b0722010-06-01 15:56:22 +1000962 ret = nouveau_card_init(dev);
963 if (ret)
Dan Carpentera0d069e2010-07-30 17:04:32 +0200964 goto err_ramin;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000965
966 return 0;
Dan Carpentera0d069e2010-07-30 17:04:32 +0200967
968err_ramin:
969 iounmap(dev_priv->ramin);
970err_mmio:
971 iounmap(dev_priv->mmio);
972err_wq:
973 destroy_workqueue(dev_priv->wq);
974err_priv:
975 kfree(dev_priv);
976 dev->dev_private = NULL;
977err_out:
978 return ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000979}
980
Ben Skeggs6ee73862009-12-11 19:24:15 +1000981void nouveau_lastclose(struct drm_device *dev)
982{
Ben Skeggs6ee73862009-12-11 19:24:15 +1000983}
984
985int nouveau_unload(struct drm_device *dev)
986{
987 struct drm_nouveau_private *dev_priv = dev->dev_private;
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200988 struct nouveau_engine *engine = &dev_priv->engine;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000989
Ben Skeggscd0b0722010-06-01 15:56:22 +1000990 drm_kms_helper_poll_fini(dev);
991 nouveau_fbcon_fini(dev);
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200992 engine->display.destroy(dev);
Ben Skeggscd0b0722010-06-01 15:56:22 +1000993 nouveau_card_takedown(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000994
995 iounmap(dev_priv->mmio);
996 iounmap(dev_priv->ramin);
997
998 kfree(dev_priv);
999 dev->dev_private = NULL;
1000 return 0;
1001}
1002
Ben Skeggs6ee73862009-12-11 19:24:15 +10001003int nouveau_ioctl_getparam(struct drm_device *dev, void *data,
1004 struct drm_file *file_priv)
1005{
1006 struct drm_nouveau_private *dev_priv = dev->dev_private;
1007 struct drm_nouveau_getparam *getparam = data;
1008
Ben Skeggs6ee73862009-12-11 19:24:15 +10001009 switch (getparam->param) {
1010 case NOUVEAU_GETPARAM_CHIPSET_ID:
1011 getparam->value = dev_priv->chipset;
1012 break;
1013 case NOUVEAU_GETPARAM_PCI_VENDOR:
1014 getparam->value = dev->pci_vendor;
1015 break;
1016 case NOUVEAU_GETPARAM_PCI_DEVICE:
1017 getparam->value = dev->pci_device;
1018 break;
1019 case NOUVEAU_GETPARAM_BUS_TYPE:
1020 if (drm_device_is_agp(dev))
1021 getparam->value = NV_AGP;
1022 else if (drm_device_is_pcie(dev))
1023 getparam->value = NV_PCIE;
1024 else
1025 getparam->value = NV_PCI;
1026 break;
1027 case NOUVEAU_GETPARAM_FB_PHYSICAL:
1028 getparam->value = dev_priv->fb_phys;
1029 break;
1030 case NOUVEAU_GETPARAM_AGP_PHYSICAL:
1031 getparam->value = dev_priv->gart_info.aper_base;
1032 break;
1033 case NOUVEAU_GETPARAM_PCI_PHYSICAL:
1034 if (dev->sg) {
1035 getparam->value = (unsigned long)dev->sg->virtual;
1036 } else {
1037 NV_ERROR(dev, "Requested PCIGART address, "
1038 "while no PCIGART was created\n");
1039 return -EINVAL;
1040 }
1041 break;
1042 case NOUVEAU_GETPARAM_FB_SIZE:
1043 getparam->value = dev_priv->fb_available_size;
1044 break;
1045 case NOUVEAU_GETPARAM_AGP_SIZE:
1046 getparam->value = dev_priv->gart_info.aper_size;
1047 break;
1048 case NOUVEAU_GETPARAM_VM_VRAM_BASE:
1049 getparam->value = dev_priv->vm_vram_base;
1050 break;
Marcin Koƛcielnicki7fc74f12010-05-23 11:36:04 +00001051 case NOUVEAU_GETPARAM_PTIMER_TIME:
1052 getparam->value = dev_priv->engine.timer.read(dev);
1053 break;
Francisco Jerezf13b3262010-10-10 06:01:08 +02001054 case NOUVEAU_GETPARAM_HAS_BO_USAGE:
1055 getparam->value = 1;
1056 break;
Marcin Koƛcielnicki69c97002010-01-26 18:39:20 +00001057 case NOUVEAU_GETPARAM_GRAPH_UNITS:
1058 /* NV40 and NV50 versions are quite different, but register
1059 * address is the same. User is supposed to know the card
1060 * family anyway... */
1061 if (dev_priv->chipset >= 0x40) {
1062 getparam->value = nv_rd32(dev, NV40_PMC_GRAPH_UNITS);
1063 break;
1064 }
1065 /* FALLTHRU */
Ben Skeggs6ee73862009-12-11 19:24:15 +10001066 default:
Francisco Jerez1397b422010-10-12 03:17:43 +02001067 NV_DEBUG(dev, "unknown parameter %lld\n", getparam->param);
Ben Skeggs6ee73862009-12-11 19:24:15 +10001068 return -EINVAL;
1069 }
1070
1071 return 0;
1072}
1073
1074int
1075nouveau_ioctl_setparam(struct drm_device *dev, void *data,
1076 struct drm_file *file_priv)
1077{
1078 struct drm_nouveau_setparam *setparam = data;
1079
Ben Skeggs6ee73862009-12-11 19:24:15 +10001080 switch (setparam->param) {
1081 default:
Francisco Jerez1397b422010-10-12 03:17:43 +02001082 NV_DEBUG(dev, "unknown parameter %lld\n", setparam->param);
Ben Skeggs6ee73862009-12-11 19:24:15 +10001083 return -EINVAL;
1084 }
1085
1086 return 0;
1087}
1088
1089/* Wait until (value(reg) & mask) == val, up until timeout has hit */
1090bool nouveau_wait_until(struct drm_device *dev, uint64_t timeout,
1091 uint32_t reg, uint32_t mask, uint32_t val)
1092{
1093 struct drm_nouveau_private *dev_priv = dev->dev_private;
1094 struct nouveau_timer_engine *ptimer = &dev_priv->engine.timer;
1095 uint64_t start = ptimer->read(dev);
1096
1097 do {
1098 if ((nv_rd32(dev, reg) & mask) == val)
1099 return true;
1100 } while (ptimer->read(dev) - start < timeout);
1101
1102 return false;
1103}
1104
1105/* Waits for PGRAPH to go completely idle */
1106bool nouveau_wait_for_idle(struct drm_device *dev)
1107{
Francisco Jerez4b5c1522010-09-07 17:34:44 +02001108 if (!nv_wait(dev, NV04_PGRAPH_STATUS, 0xffffffff, 0x00000000)) {
Ben Skeggs6ee73862009-12-11 19:24:15 +10001109 NV_ERROR(dev, "PGRAPH idle timed out with status 0x%08x\n",
1110 nv_rd32(dev, NV04_PGRAPH_STATUS));
1111 return false;
1112 }
1113
1114 return true;
1115}
1116