blob: f6c2535e766e24c3309ebd8ccb35ef4b07a1eb57 [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;
Ben Skeggse41115d2010-11-01 11:45:02 +100056 engine->instmem.get = nv04_instmem_get;
57 engine->instmem.put = nv04_instmem_put;
58 engine->instmem.map = nv04_instmem_map;
59 engine->instmem.unmap = nv04_instmem_unmap;
Ben Skeggsf56cb862010-07-08 11:29:10 +100060 engine->instmem.flush = nv04_instmem_flush;
Ben Skeggs6ee73862009-12-11 19:24:15 +100061 engine->mc.init = nv04_mc_init;
62 engine->mc.takedown = nv04_mc_takedown;
63 engine->timer.init = nv04_timer_init;
64 engine->timer.read = nv04_timer_read;
65 engine->timer.takedown = nv04_timer_takedown;
66 engine->fb.init = nv04_fb_init;
67 engine->fb.takedown = nv04_fb_takedown;
Ben Skeggs6ee73862009-12-11 19:24:15 +100068 engine->fifo.channels = 16;
69 engine->fifo.init = nv04_fifo_init;
Ben Skeggs5178d402010-11-03 10:56:05 +100070 engine->fifo.takedown = nv04_fifo_fini;
Ben Skeggs6ee73862009-12-11 19:24:15 +100071 engine->fifo.disable = nv04_fifo_disable;
72 engine->fifo.enable = nv04_fifo_enable;
73 engine->fifo.reassign = nv04_fifo_reassign;
Francisco Jerez588d7d12009-12-13 20:07:42 +010074 engine->fifo.cache_pull = nv04_fifo_cache_pull;
Ben Skeggs6ee73862009-12-11 19:24:15 +100075 engine->fifo.channel_id = nv04_fifo_channel_id;
76 engine->fifo.create_context = nv04_fifo_create_context;
77 engine->fifo.destroy_context = nv04_fifo_destroy_context;
78 engine->fifo.load_context = nv04_fifo_load_context;
79 engine->fifo.unload_context = nv04_fifo_unload_context;
Francisco Jerezc88c2e02010-07-24 17:37:33 +020080 engine->display.early_init = nv04_display_early_init;
81 engine->display.late_takedown = nv04_display_late_takedown;
82 engine->display.create = nv04_display_create;
83 engine->display.init = nv04_display_init;
84 engine->display.destroy = nv04_display_destroy;
Ben Skeggsee2e0132010-07-26 09:28:25 +100085 engine->gpio.init = nouveau_stub_init;
86 engine->gpio.takedown = nouveau_stub_takedown;
87 engine->gpio.get = NULL;
88 engine->gpio.set = NULL;
89 engine->gpio.irq_enable = NULL;
Ben Skeggs442b6262010-09-16 16:25:26 +100090 engine->pm.clock_get = nv04_pm_clock_get;
91 engine->pm.clock_pre = nv04_pm_clock_pre;
92 engine->pm.clock_set = nv04_pm_clock_set;
Ben Skeggs60d2a882010-12-06 15:28:54 +100093 engine->vram.init = nouveau_mem_detect;
94 engine->vram.flags_valid = nouveau_mem_flags_valid;
Ben Skeggs6ee73862009-12-11 19:24:15 +100095 break;
96 case 0x10:
97 engine->instmem.init = nv04_instmem_init;
98 engine->instmem.takedown = nv04_instmem_takedown;
99 engine->instmem.suspend = nv04_instmem_suspend;
100 engine->instmem.resume = nv04_instmem_resume;
Ben Skeggse41115d2010-11-01 11:45:02 +1000101 engine->instmem.get = nv04_instmem_get;
102 engine->instmem.put = nv04_instmem_put;
103 engine->instmem.map = nv04_instmem_map;
104 engine->instmem.unmap = nv04_instmem_unmap;
Ben Skeggsf56cb862010-07-08 11:29:10 +1000105 engine->instmem.flush = nv04_instmem_flush;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000106 engine->mc.init = nv04_mc_init;
107 engine->mc.takedown = nv04_mc_takedown;
108 engine->timer.init = nv04_timer_init;
109 engine->timer.read = nv04_timer_read;
110 engine->timer.takedown = nv04_timer_takedown;
111 engine->fb.init = nv10_fb_init;
112 engine->fb.takedown = nv10_fb_takedown;
Francisco Jereza5cf68b2010-10-24 16:14:41 +0200113 engine->fb.init_tile_region = nv10_fb_init_tile_region;
114 engine->fb.set_tile_region = nv10_fb_set_tile_region;
115 engine->fb.free_tile_region = nv10_fb_free_tile_region;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000116 engine->fifo.channels = 32;
117 engine->fifo.init = nv10_fifo_init;
Ben Skeggs5178d402010-11-03 10:56:05 +1000118 engine->fifo.takedown = nv04_fifo_fini;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000119 engine->fifo.disable = nv04_fifo_disable;
120 engine->fifo.enable = nv04_fifo_enable;
121 engine->fifo.reassign = nv04_fifo_reassign;
Francisco Jerez588d7d12009-12-13 20:07:42 +0100122 engine->fifo.cache_pull = nv04_fifo_cache_pull;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000123 engine->fifo.channel_id = nv10_fifo_channel_id;
124 engine->fifo.create_context = nv10_fifo_create_context;
Francisco Jerez3945e472010-10-18 03:53:39 +0200125 engine->fifo.destroy_context = nv04_fifo_destroy_context;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000126 engine->fifo.load_context = nv10_fifo_load_context;
127 engine->fifo.unload_context = nv10_fifo_unload_context;
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200128 engine->display.early_init = nv04_display_early_init;
129 engine->display.late_takedown = nv04_display_late_takedown;
130 engine->display.create = nv04_display_create;
131 engine->display.init = nv04_display_init;
132 engine->display.destroy = nv04_display_destroy;
Ben Skeggsee2e0132010-07-26 09:28:25 +1000133 engine->gpio.init = nouveau_stub_init;
134 engine->gpio.takedown = nouveau_stub_takedown;
135 engine->gpio.get = nv10_gpio_get;
136 engine->gpio.set = nv10_gpio_set;
137 engine->gpio.irq_enable = NULL;
Ben Skeggs442b6262010-09-16 16:25:26 +1000138 engine->pm.clock_get = nv04_pm_clock_get;
139 engine->pm.clock_pre = nv04_pm_clock_pre;
140 engine->pm.clock_set = nv04_pm_clock_set;
Ben Skeggs60d2a882010-12-06 15:28:54 +1000141 engine->vram.init = nouveau_mem_detect;
142 engine->vram.flags_valid = nouveau_mem_flags_valid;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000143 break;
144 case 0x20:
145 engine->instmem.init = nv04_instmem_init;
146 engine->instmem.takedown = nv04_instmem_takedown;
147 engine->instmem.suspend = nv04_instmem_suspend;
148 engine->instmem.resume = nv04_instmem_resume;
Ben Skeggse41115d2010-11-01 11:45:02 +1000149 engine->instmem.get = nv04_instmem_get;
150 engine->instmem.put = nv04_instmem_put;
151 engine->instmem.map = nv04_instmem_map;
152 engine->instmem.unmap = nv04_instmem_unmap;
Ben Skeggsf56cb862010-07-08 11:29:10 +1000153 engine->instmem.flush = nv04_instmem_flush;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000154 engine->mc.init = nv04_mc_init;
155 engine->mc.takedown = nv04_mc_takedown;
156 engine->timer.init = nv04_timer_init;
157 engine->timer.read = nv04_timer_read;
158 engine->timer.takedown = nv04_timer_takedown;
159 engine->fb.init = nv10_fb_init;
160 engine->fb.takedown = nv10_fb_takedown;
Francisco Jereza5cf68b2010-10-24 16:14:41 +0200161 engine->fb.init_tile_region = nv10_fb_init_tile_region;
162 engine->fb.set_tile_region = nv10_fb_set_tile_region;
163 engine->fb.free_tile_region = nv10_fb_free_tile_region;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000164 engine->fifo.channels = 32;
165 engine->fifo.init = nv10_fifo_init;
Ben Skeggs5178d402010-11-03 10:56:05 +1000166 engine->fifo.takedown = nv04_fifo_fini;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000167 engine->fifo.disable = nv04_fifo_disable;
168 engine->fifo.enable = nv04_fifo_enable;
169 engine->fifo.reassign = nv04_fifo_reassign;
Francisco Jerez588d7d12009-12-13 20:07:42 +0100170 engine->fifo.cache_pull = nv04_fifo_cache_pull;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000171 engine->fifo.channel_id = nv10_fifo_channel_id;
172 engine->fifo.create_context = nv10_fifo_create_context;
Francisco Jerez3945e472010-10-18 03:53:39 +0200173 engine->fifo.destroy_context = nv04_fifo_destroy_context;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000174 engine->fifo.load_context = nv10_fifo_load_context;
175 engine->fifo.unload_context = nv10_fifo_unload_context;
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200176 engine->display.early_init = nv04_display_early_init;
177 engine->display.late_takedown = nv04_display_late_takedown;
178 engine->display.create = nv04_display_create;
179 engine->display.init = nv04_display_init;
180 engine->display.destroy = nv04_display_destroy;
Ben Skeggsee2e0132010-07-26 09:28:25 +1000181 engine->gpio.init = nouveau_stub_init;
182 engine->gpio.takedown = nouveau_stub_takedown;
183 engine->gpio.get = nv10_gpio_get;
184 engine->gpio.set = nv10_gpio_set;
185 engine->gpio.irq_enable = NULL;
Ben Skeggs442b6262010-09-16 16:25:26 +1000186 engine->pm.clock_get = nv04_pm_clock_get;
187 engine->pm.clock_pre = nv04_pm_clock_pre;
188 engine->pm.clock_set = nv04_pm_clock_set;
Ben Skeggs60d2a882010-12-06 15:28:54 +1000189 engine->vram.init = nouveau_mem_detect;
190 engine->vram.flags_valid = nouveau_mem_flags_valid;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000191 break;
192 case 0x30:
193 engine->instmem.init = nv04_instmem_init;
194 engine->instmem.takedown = nv04_instmem_takedown;
195 engine->instmem.suspend = nv04_instmem_suspend;
196 engine->instmem.resume = nv04_instmem_resume;
Ben Skeggse41115d2010-11-01 11:45:02 +1000197 engine->instmem.get = nv04_instmem_get;
198 engine->instmem.put = nv04_instmem_put;
199 engine->instmem.map = nv04_instmem_map;
200 engine->instmem.unmap = nv04_instmem_unmap;
Ben Skeggsf56cb862010-07-08 11:29:10 +1000201 engine->instmem.flush = nv04_instmem_flush;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000202 engine->mc.init = nv04_mc_init;
203 engine->mc.takedown = nv04_mc_takedown;
204 engine->timer.init = nv04_timer_init;
205 engine->timer.read = nv04_timer_read;
206 engine->timer.takedown = nv04_timer_takedown;
Francisco Jerez8bded182010-07-21 21:08:11 +0200207 engine->fb.init = nv30_fb_init;
208 engine->fb.takedown = nv30_fb_takedown;
Francisco Jereza5cf68b2010-10-24 16:14:41 +0200209 engine->fb.init_tile_region = nv30_fb_init_tile_region;
210 engine->fb.set_tile_region = nv10_fb_set_tile_region;
211 engine->fb.free_tile_region = nv30_fb_free_tile_region;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000212 engine->fifo.channels = 32;
213 engine->fifo.init = nv10_fifo_init;
Ben Skeggs5178d402010-11-03 10:56:05 +1000214 engine->fifo.takedown = nv04_fifo_fini;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000215 engine->fifo.disable = nv04_fifo_disable;
216 engine->fifo.enable = nv04_fifo_enable;
217 engine->fifo.reassign = nv04_fifo_reassign;
Francisco Jerez588d7d12009-12-13 20:07:42 +0100218 engine->fifo.cache_pull = nv04_fifo_cache_pull;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000219 engine->fifo.channel_id = nv10_fifo_channel_id;
220 engine->fifo.create_context = nv10_fifo_create_context;
Francisco Jerez3945e472010-10-18 03:53:39 +0200221 engine->fifo.destroy_context = nv04_fifo_destroy_context;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000222 engine->fifo.load_context = nv10_fifo_load_context;
223 engine->fifo.unload_context = nv10_fifo_unload_context;
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200224 engine->display.early_init = nv04_display_early_init;
225 engine->display.late_takedown = nv04_display_late_takedown;
226 engine->display.create = nv04_display_create;
227 engine->display.init = nv04_display_init;
228 engine->display.destroy = nv04_display_destroy;
Ben Skeggsee2e0132010-07-26 09:28:25 +1000229 engine->gpio.init = nouveau_stub_init;
230 engine->gpio.takedown = nouveau_stub_takedown;
231 engine->gpio.get = nv10_gpio_get;
232 engine->gpio.set = nv10_gpio_set;
233 engine->gpio.irq_enable = NULL;
Ben Skeggs442b6262010-09-16 16:25:26 +1000234 engine->pm.clock_get = nv04_pm_clock_get;
235 engine->pm.clock_pre = nv04_pm_clock_pre;
236 engine->pm.clock_set = nv04_pm_clock_set;
237 engine->pm.voltage_get = nouveau_voltage_gpio_get;
238 engine->pm.voltage_set = nouveau_voltage_gpio_set;
Ben Skeggs60d2a882010-12-06 15:28:54 +1000239 engine->vram.init = nouveau_mem_detect;
240 engine->vram.flags_valid = nouveau_mem_flags_valid;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000241 break;
242 case 0x40:
243 case 0x60:
244 engine->instmem.init = nv04_instmem_init;
245 engine->instmem.takedown = nv04_instmem_takedown;
246 engine->instmem.suspend = nv04_instmem_suspend;
247 engine->instmem.resume = nv04_instmem_resume;
Ben Skeggse41115d2010-11-01 11:45:02 +1000248 engine->instmem.get = nv04_instmem_get;
249 engine->instmem.put = nv04_instmem_put;
250 engine->instmem.map = nv04_instmem_map;
251 engine->instmem.unmap = nv04_instmem_unmap;
Ben Skeggsf56cb862010-07-08 11:29:10 +1000252 engine->instmem.flush = nv04_instmem_flush;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000253 engine->mc.init = nv40_mc_init;
254 engine->mc.takedown = nv40_mc_takedown;
255 engine->timer.init = nv04_timer_init;
256 engine->timer.read = nv04_timer_read;
257 engine->timer.takedown = nv04_timer_takedown;
258 engine->fb.init = nv40_fb_init;
259 engine->fb.takedown = nv40_fb_takedown;
Francisco Jereza5cf68b2010-10-24 16:14:41 +0200260 engine->fb.init_tile_region = nv30_fb_init_tile_region;
261 engine->fb.set_tile_region = nv40_fb_set_tile_region;
262 engine->fb.free_tile_region = nv30_fb_free_tile_region;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000263 engine->fifo.channels = 32;
264 engine->fifo.init = nv40_fifo_init;
Ben Skeggs5178d402010-11-03 10:56:05 +1000265 engine->fifo.takedown = nv04_fifo_fini;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000266 engine->fifo.disable = nv04_fifo_disable;
267 engine->fifo.enable = nv04_fifo_enable;
268 engine->fifo.reassign = nv04_fifo_reassign;
Francisco Jerez588d7d12009-12-13 20:07:42 +0100269 engine->fifo.cache_pull = nv04_fifo_cache_pull;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000270 engine->fifo.channel_id = nv10_fifo_channel_id;
271 engine->fifo.create_context = nv40_fifo_create_context;
Francisco Jerez3945e472010-10-18 03:53:39 +0200272 engine->fifo.destroy_context = nv04_fifo_destroy_context;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000273 engine->fifo.load_context = nv40_fifo_load_context;
274 engine->fifo.unload_context = nv40_fifo_unload_context;
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200275 engine->display.early_init = nv04_display_early_init;
276 engine->display.late_takedown = nv04_display_late_takedown;
277 engine->display.create = nv04_display_create;
278 engine->display.init = nv04_display_init;
279 engine->display.destroy = nv04_display_destroy;
Ben Skeggsee2e0132010-07-26 09:28:25 +1000280 engine->gpio.init = nouveau_stub_init;
281 engine->gpio.takedown = nouveau_stub_takedown;
282 engine->gpio.get = nv10_gpio_get;
283 engine->gpio.set = nv10_gpio_set;
284 engine->gpio.irq_enable = NULL;
Ben Skeggs442b6262010-09-16 16:25:26 +1000285 engine->pm.clock_get = nv04_pm_clock_get;
286 engine->pm.clock_pre = nv04_pm_clock_pre;
287 engine->pm.clock_set = nv04_pm_clock_set;
288 engine->pm.voltage_get = nouveau_voltage_gpio_get;
289 engine->pm.voltage_set = nouveau_voltage_gpio_set;
Francisco Jerez8155cac2010-09-23 20:58:38 +0200290 engine->pm.temp_get = nv40_temp_get;
Ben Skeggs60d2a882010-12-06 15:28:54 +1000291 engine->vram.init = nouveau_mem_detect;
292 engine->vram.flags_valid = nouveau_mem_flags_valid;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000293 break;
294 case 0x50:
295 case 0x80: /* gotta love NVIDIA's consistency.. */
296 case 0x90:
297 case 0xA0:
298 engine->instmem.init = nv50_instmem_init;
299 engine->instmem.takedown = nv50_instmem_takedown;
300 engine->instmem.suspend = nv50_instmem_suspend;
301 engine->instmem.resume = nv50_instmem_resume;
Ben Skeggse41115d2010-11-01 11:45:02 +1000302 engine->instmem.get = nv50_instmem_get;
303 engine->instmem.put = nv50_instmem_put;
304 engine->instmem.map = nv50_instmem_map;
305 engine->instmem.unmap = nv50_instmem_unmap;
Ben Skeggs734ee832010-07-15 11:02:54 +1000306 if (dev_priv->chipset == 0x50)
307 engine->instmem.flush = nv50_instmem_flush;
308 else
309 engine->instmem.flush = nv84_instmem_flush;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000310 engine->mc.init = nv50_mc_init;
311 engine->mc.takedown = nv50_mc_takedown;
312 engine->timer.init = nv04_timer_init;
313 engine->timer.read = nv04_timer_read;
314 engine->timer.takedown = nv04_timer_takedown;
Marcin Koƛcielnicki304424e2010-03-01 00:18:39 +0000315 engine->fb.init = nv50_fb_init;
316 engine->fb.takedown = nv50_fb_takedown;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000317 engine->fifo.channels = 128;
318 engine->fifo.init = nv50_fifo_init;
319 engine->fifo.takedown = nv50_fifo_takedown;
320 engine->fifo.disable = nv04_fifo_disable;
321 engine->fifo.enable = nv04_fifo_enable;
322 engine->fifo.reassign = nv04_fifo_reassign;
323 engine->fifo.channel_id = nv50_fifo_channel_id;
324 engine->fifo.create_context = nv50_fifo_create_context;
325 engine->fifo.destroy_context = nv50_fifo_destroy_context;
326 engine->fifo.load_context = nv50_fifo_load_context;
327 engine->fifo.unload_context = nv50_fifo_unload_context;
Ben Skeggs56ac7472010-10-22 10:26:24 +1000328 engine->fifo.tlb_flush = nv50_fifo_tlb_flush;
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200329 engine->display.early_init = nv50_display_early_init;
330 engine->display.late_takedown = nv50_display_late_takedown;
331 engine->display.create = nv50_display_create;
332 engine->display.init = nv50_display_init;
333 engine->display.destroy = nv50_display_destroy;
Ben Skeggsee2e0132010-07-26 09:28:25 +1000334 engine->gpio.init = nv50_gpio_init;
Ben Skeggs2cbd4c82010-11-03 10:18:04 +1000335 engine->gpio.takedown = nv50_gpio_fini;
Ben Skeggsee2e0132010-07-26 09:28:25 +1000336 engine->gpio.get = nv50_gpio_get;
337 engine->gpio.set = nv50_gpio_set;
Ben Skeggsfce2bad2010-11-11 16:14:56 +1000338 engine->gpio.irq_register = nv50_gpio_irq_register;
339 engine->gpio.irq_unregister = nv50_gpio_irq_unregister;
Ben Skeggsee2e0132010-07-26 09:28:25 +1000340 engine->gpio.irq_enable = nv50_gpio_irq_enable;
Ben Skeggsfade7ad2010-09-27 11:18:14 +1000341 switch (dev_priv->chipset) {
Ben Skeggsbd2e5972010-10-19 20:06:01 +1000342 case 0x84:
343 case 0x86:
344 case 0x92:
345 case 0x94:
346 case 0x96:
347 case 0x98:
348 case 0xa0:
Ben Skeggs5f801982010-10-22 08:44:09 +1000349 case 0xaa:
350 case 0xac:
Ben Skeggsbd2e5972010-10-19 20:06:01 +1000351 case 0x50:
Ben Skeggsfade7ad2010-09-27 11:18:14 +1000352 engine->pm.clock_get = nv50_pm_clock_get;
353 engine->pm.clock_pre = nv50_pm_clock_pre;
354 engine->pm.clock_set = nv50_pm_clock_set;
355 break;
Ben Skeggsbd2e5972010-10-19 20:06:01 +1000356 default:
357 engine->pm.clock_get = nva3_pm_clock_get;
358 engine->pm.clock_pre = nva3_pm_clock_pre;
359 engine->pm.clock_set = nva3_pm_clock_set;
360 break;
Ben Skeggsfade7ad2010-09-27 11:18:14 +1000361 }
Ben Skeggs02c30ca2010-09-16 16:17:35 +1000362 engine->pm.voltage_get = nouveau_voltage_gpio_get;
363 engine->pm.voltage_set = nouveau_voltage_gpio_set;
Francisco Jerez8155cac2010-09-23 20:58:38 +0200364 if (dev_priv->chipset >= 0x84)
365 engine->pm.temp_get = nv84_temp_get;
366 else
367 engine->pm.temp_get = nv40_temp_get;
Ben Skeggs60d2a882010-12-06 15:28:54 +1000368 engine->vram.init = nv50_vram_init;
369 engine->vram.get = nv50_vram_new;
370 engine->vram.put = nv50_vram_del;
371 engine->vram.flags_valid = nv50_vram_flags_valid;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000372 break;
Ben Skeggs4b223ee2010-08-03 10:00:56 +1000373 case 0xC0:
374 engine->instmem.init = nvc0_instmem_init;
375 engine->instmem.takedown = nvc0_instmem_takedown;
376 engine->instmem.suspend = nvc0_instmem_suspend;
377 engine->instmem.resume = nvc0_instmem_resume;
Ben Skeggs8984e042010-11-15 11:48:33 +1000378 engine->instmem.get = nv50_instmem_get;
379 engine->instmem.put = nv50_instmem_put;
380 engine->instmem.map = nv50_instmem_map;
381 engine->instmem.unmap = nv50_instmem_unmap;
382 engine->instmem.flush = nv84_instmem_flush;
Ben Skeggs4b223ee2010-08-03 10:00:56 +1000383 engine->mc.init = nv50_mc_init;
384 engine->mc.takedown = nv50_mc_takedown;
385 engine->timer.init = nv04_timer_init;
386 engine->timer.read = nv04_timer_read;
387 engine->timer.takedown = nv04_timer_takedown;
388 engine->fb.init = nvc0_fb_init;
389 engine->fb.takedown = nvc0_fb_takedown;
Ben Skeggs4b223ee2010-08-03 10:00:56 +1000390 engine->fifo.channels = 128;
391 engine->fifo.init = nvc0_fifo_init;
392 engine->fifo.takedown = nvc0_fifo_takedown;
393 engine->fifo.disable = nvc0_fifo_disable;
394 engine->fifo.enable = nvc0_fifo_enable;
395 engine->fifo.reassign = nvc0_fifo_reassign;
396 engine->fifo.channel_id = nvc0_fifo_channel_id;
397 engine->fifo.create_context = nvc0_fifo_create_context;
398 engine->fifo.destroy_context = nvc0_fifo_destroy_context;
399 engine->fifo.load_context = nvc0_fifo_load_context;
400 engine->fifo.unload_context = nvc0_fifo_unload_context;
401 engine->display.early_init = nv50_display_early_init;
402 engine->display.late_takedown = nv50_display_late_takedown;
403 engine->display.create = nv50_display_create;
404 engine->display.init = nv50_display_init;
405 engine->display.destroy = nv50_display_destroy;
406 engine->gpio.init = nv50_gpio_init;
407 engine->gpio.takedown = nouveau_stub_takedown;
408 engine->gpio.get = nv50_gpio_get;
409 engine->gpio.set = nv50_gpio_set;
Ben Skeggsfce2bad2010-11-11 16:14:56 +1000410 engine->gpio.irq_register = nv50_gpio_irq_register;
411 engine->gpio.irq_unregister = nv50_gpio_irq_unregister;
Ben Skeggs4b223ee2010-08-03 10:00:56 +1000412 engine->gpio.irq_enable = nv50_gpio_irq_enable;
Ben Skeggs8984e042010-11-15 11:48:33 +1000413 engine->vram.init = nvc0_vram_init;
414 engine->vram.get = nvc0_vram_new;
415 engine->vram.put = nv50_vram_del;
416 engine->vram.flags_valid = nvc0_vram_flags_valid;
Ben Skeggs4b223ee2010-08-03 10:00:56 +1000417 break;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000418 default:
419 NV_ERROR(dev, "NV%02x unsupported\n", dev_priv->chipset);
420 return 1;
421 }
422
423 return 0;
424}
425
426static unsigned int
427nouveau_vga_set_decode(void *priv, bool state)
428{
Marcin Koƛcielnicki9967b942010-02-08 00:20:17 +0000429 struct drm_device *dev = priv;
430 struct drm_nouveau_private *dev_priv = dev->dev_private;
431
432 if (dev_priv->chipset >= 0x40)
433 nv_wr32(dev, 0x88054, state);
434 else
435 nv_wr32(dev, 0x1854, state);
436
Ben Skeggs6ee73862009-12-11 19:24:15 +1000437 if (state)
438 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
439 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
440 else
441 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
442}
443
Ben Skeggs0735f622009-12-16 14:28:55 +1000444static int
445nouveau_card_init_channel(struct drm_device *dev)
446{
447 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs0735f622009-12-16 14:28:55 +1000448 int ret;
449
450 ret = nouveau_channel_alloc(dev, &dev_priv->channel,
Ben Skeggsa8eaebc2010-09-01 15:24:31 +1000451 (struct drm_file *)-2, NvDmaFB, NvDmaTT);
Ben Skeggs0735f622009-12-16 14:28:55 +1000452 if (ret)
453 return ret;
454
Ben Skeggscff5c132010-10-06 16:16:59 +1000455 mutex_unlock(&dev_priv->channel->mutex);
Ben Skeggs0735f622009-12-16 14:28:55 +1000456 return 0;
Ben Skeggs0735f622009-12-16 14:28:55 +1000457}
458
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000459static void nouveau_switcheroo_set_state(struct pci_dev *pdev,
460 enum vga_switcheroo_state state)
461{
Dave Airliefbf81762010-06-01 09:09:06 +1000462 struct drm_device *dev = pci_get_drvdata(pdev);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000463 pm_message_t pmm = { .event = PM_EVENT_SUSPEND };
464 if (state == VGA_SWITCHEROO_ON) {
465 printk(KERN_ERR "VGA switcheroo: switched nouveau on\n");
Dave Airlie5bcf7192010-12-07 09:20:40 +1000466 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000467 nouveau_pci_resume(pdev);
Dave Airliefbf81762010-06-01 09:09:06 +1000468 drm_kms_helper_poll_enable(dev);
Dave Airlie5bcf7192010-12-07 09:20:40 +1000469 dev->switch_power_state = DRM_SWITCH_POWER_ON;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000470 } else {
471 printk(KERN_ERR "VGA switcheroo: switched nouveau off\n");
Dave Airlie5bcf7192010-12-07 09:20:40 +1000472 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
Dave Airliefbf81762010-06-01 09:09:06 +1000473 drm_kms_helper_poll_disable(dev);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000474 nouveau_pci_suspend(pdev, pmm);
Dave Airlie5bcf7192010-12-07 09:20:40 +1000475 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000476 }
477}
478
Dave Airlie8d608aa2010-12-07 08:57:57 +1000479static void nouveau_switcheroo_reprobe(struct pci_dev *pdev)
480{
481 struct drm_device *dev = pci_get_drvdata(pdev);
482 nouveau_fbcon_output_poll_changed(dev);
483}
484
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000485static bool nouveau_switcheroo_can_switch(struct pci_dev *pdev)
486{
487 struct drm_device *dev = pci_get_drvdata(pdev);
488 bool can_switch;
489
490 spin_lock(&dev->count_lock);
491 can_switch = (dev->open_count == 0);
492 spin_unlock(&dev->count_lock);
493 return can_switch;
494}
495
Ben Skeggs6ee73862009-12-11 19:24:15 +1000496int
497nouveau_card_init(struct drm_device *dev)
498{
499 struct drm_nouveau_private *dev_priv = dev->dev_private;
500 struct nouveau_engine *engine;
Ben Skeggs6dfdd7a2011-03-31 15:40:43 +1000501 int ret, e;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000502
Ben Skeggs6ee73862009-12-11 19:24:15 +1000503 vga_client_register(dev->pdev, dev, NULL, nouveau_vga_set_decode);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000504 vga_switcheroo_register_client(dev->pdev, nouveau_switcheroo_set_state,
Dave Airlie8d608aa2010-12-07 08:57:57 +1000505 nouveau_switcheroo_reprobe,
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000506 nouveau_switcheroo_can_switch);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000507
508 /* Initialise internal driver API hooks */
509 ret = nouveau_init_engine_ptrs(dev);
510 if (ret)
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000511 goto out;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000512 engine = &dev_priv->engine;
Ben Skeggscff5c132010-10-06 16:16:59 +1000513 spin_lock_init(&dev_priv->channels.lock);
Francisco Jereza5cf68b2010-10-24 16:14:41 +0200514 spin_lock_init(&dev_priv->tile.lock);
Maarten Maathuisff9e5272010-02-01 20:58:27 +0100515 spin_lock_init(&dev_priv->context_switch_lock);
Ben Skeggs04eb34a2011-04-06 13:28:35 +1000516 spin_lock_init(&dev_priv->vm_lock);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000517
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200518 /* Make the CRTCs and I2C buses accessible */
519 ret = engine->display.early_init(dev);
520 if (ret)
521 goto out;
522
Ben Skeggs6ee73862009-12-11 19:24:15 +1000523 /* Parse BIOS tables / Run init tables if card not POSTed */
Ben Skeggscd0b0722010-06-01 15:56:22 +1000524 ret = nouveau_bios_init(dev);
525 if (ret)
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200526 goto out_display_early;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000527
Ben Skeggs330c5982010-09-16 15:39:49 +1000528 nouveau_pm_init(dev);
529
Ben Skeggsfbd2895e2010-09-01 15:24:34 +1000530 ret = nouveau_mem_vram_init(dev);
Ben Skeggsa76fb4e2010-03-18 09:45:20 +1000531 if (ret)
532 goto out_bios;
533
Ben Skeggs6ee73862009-12-11 19:24:15 +1000534 ret = nouveau_gpuobj_init(dev);
535 if (ret)
Ben Skeggsfbd2895e2010-09-01 15:24:34 +1000536 goto out_vram;
537
538 ret = engine->instmem.init(dev);
539 if (ret)
540 goto out_gpuobj;
541
542 ret = nouveau_mem_gart_init(dev);
543 if (ret)
544 goto out_instmem;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000545
546 /* PMC */
547 ret = engine->mc.init(dev);
548 if (ret)
Ben Skeggsfbd2895e2010-09-01 15:24:34 +1000549 goto out_gart;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000550
Ben Skeggsee2e0132010-07-26 09:28:25 +1000551 /* PGPIO */
552 ret = engine->gpio.init(dev);
553 if (ret)
554 goto out_mc;
555
Ben Skeggs6ee73862009-12-11 19:24:15 +1000556 /* PTIMER */
557 ret = engine->timer.init(dev);
558 if (ret)
Ben Skeggsee2e0132010-07-26 09:28:25 +1000559 goto out_gpio;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000560
561 /* PFB */
562 ret = engine->fb.init(dev);
563 if (ret)
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000564 goto out_timer;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000565
Ben Skeggs39c8d362011-04-01 11:33:21 +1000566 switch (dev_priv->card_type) {
Ben Skeggs49769862011-04-01 13:03:56 +1000567 case NV_04:
568 nv04_graph_create(dev);
569 break;
Ben Skeggsd11db272011-04-01 12:50:55 +1000570 case NV_10:
571 nv10_graph_create(dev);
572 break;
Ben Skeggsa0b1de82011-04-01 12:32:03 +1000573 case NV_20:
574 case NV_30:
575 nv20_graph_create(dev);
576 break;
Ben Skeggs39c8d362011-04-01 11:33:21 +1000577 case NV_40:
578 nv40_graph_create(dev);
579 break;
580 case NV_50:
Ben Skeggs2703c212011-04-01 09:50:18 +1000581 nv50_graph_create(dev);
Ben Skeggs39c8d362011-04-01 11:33:21 +1000582 break;
583 case NV_C0:
Ben Skeggs7a45cd12011-04-01 10:59:53 +1000584 nvc0_graph_create(dev);
Ben Skeggs39c8d362011-04-01 11:33:21 +1000585 break;
Ben Skeggsa0b1de82011-04-01 12:32:03 +1000586 default:
587 break;
Ben Skeggs39c8d362011-04-01 11:33:21 +1000588 }
Ben Skeggs2703c212011-04-01 09:50:18 +1000589
Ben Skeggs6dfdd7a2011-03-31 15:40:43 +1000590 switch (dev_priv->chipset) {
591 case 0x84:
592 case 0x86:
593 case 0x92:
594 case 0x94:
595 case 0x96:
596 case 0xa0:
597 nv84_crypt_create(dev);
598 break;
599 }
600
Ben Skeggs7ff54412011-03-18 10:25:59 +1000601 switch (dev_priv->card_type) {
602 case NV_50:
603 switch (dev_priv->chipset) {
604 case 0xa3:
605 case 0xa5:
606 case 0xa8:
607 case 0xaf:
608 nva3_copy_create(dev);
609 break;
610 }
611 break;
Ben Skeggsd5a27372011-04-01 16:10:08 +1000612 case NV_C0:
613 nvc0_copy_create(dev, 0);
614 nvc0_copy_create(dev, 1);
615 break;
Ben Skeggs7ff54412011-03-18 10:25:59 +1000616 default:
617 break;
618 }
619
Ben Skeggsa02ccc72011-04-04 16:08:24 +1000620 if (dev_priv->card_type == NV_40)
621 nv40_mpeg_create(dev);
622
Ben Skeggsa82dd492011-04-01 13:56:05 +1000623 if (!nouveau_noaccel) {
Ben Skeggs6dfdd7a2011-03-31 15:40:43 +1000624 for (e = 0; e < NVOBJ_ENGINE_NR; e++) {
625 if (dev_priv->eng[e]) {
626 ret = dev_priv->eng[e]->init(dev, e);
627 if (ret)
628 goto out_engine;
629 }
630 }
631
Marcin Koƛcielnickia32ed692010-01-26 14:00:42 +0000632 /* PFIFO */
633 ret = engine->fifo.init(dev);
634 if (ret)
Ben Skeggsa82dd492011-04-01 13:56:05 +1000635 goto out_engine;
Marcin Koƛcielnickia32ed692010-01-26 14:00:42 +0000636 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000637
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200638 ret = engine->display.create(dev);
Ben Skeggse88efe02010-07-09 10:56:08 +1000639 if (ret)
640 goto out_fifo;
641
Francisco Jerez042206c2010-10-21 18:19:29 +0200642 ret = drm_vblank_init(dev, nv_two_heads(dev) ? 2 : 1);
643 if (ret)
644 goto out_vblank;
645
Ben Skeggs35fa2f22010-10-21 14:07:03 +1000646 ret = nouveau_irq_init(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000647 if (ret)
Francisco Jerez042206c2010-10-21 18:19:29 +0200648 goto out_vblank;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000649
650 /* what about PVIDEO/PCRTC/PRAMDAC etc? */
651
Ben Skeggsa82dd492011-04-01 13:56:05 +1000652 if (dev_priv->eng[NVOBJ_ENGINE_GR]) {
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 Skeggscd0b0722010-06-01 15:56:22 +1000662 nouveau_fbcon_init(dev);
663 drm_kms_helper_poll_init(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000664 return 0;
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000665
Francisco Jerez0c6c1c22010-09-22 00:58:54 +0200666out_fence:
667 nouveau_fence_fini(dev);
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000668out_irq:
Ben Skeggs35fa2f22010-10-21 14:07:03 +1000669 nouveau_irq_fini(dev);
Francisco Jerez042206c2010-10-21 18:19:29 +0200670out_vblank:
671 drm_vblank_cleanup(dev);
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200672 engine->display.destroy(dev);
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000673out_fifo:
Marcin Koƛcielnickia32ed692010-01-26 14:00:42 +0000674 if (!nouveau_noaccel)
675 engine->fifo.takedown(dev);
Ben Skeggs6dfdd7a2011-03-31 15:40:43 +1000676out_engine:
677 if (!nouveau_noaccel) {
678 for (e = e - 1; e >= 0; e--) {
Ben Skeggs2703c212011-04-01 09:50:18 +1000679 if (!dev_priv->eng[e])
680 continue;
Ben Skeggs6dfdd7a2011-03-31 15:40:43 +1000681 dev_priv->eng[e]->fini(dev, e);
Ben Skeggs2703c212011-04-01 09:50:18 +1000682 dev_priv->eng[e]->destroy(dev,e );
Ben Skeggs6dfdd7a2011-03-31 15:40:43 +1000683 }
684 }
685
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000686 engine->fb.takedown(dev);
687out_timer:
688 engine->timer.takedown(dev);
Ben Skeggsee2e0132010-07-26 09:28:25 +1000689out_gpio:
690 engine->gpio.takedown(dev);
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000691out_mc:
692 engine->mc.takedown(dev);
Ben Skeggsfbd2895e2010-09-01 15:24:34 +1000693out_gart:
694 nouveau_mem_gart_fini(dev);
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000695out_instmem:
696 engine->instmem.takedown(dev);
Ben Skeggsfbd2895e2010-09-01 15:24:34 +1000697out_gpuobj:
698 nouveau_gpuobj_takedown(dev);
699out_vram:
700 nouveau_mem_vram_fini(dev);
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000701out_bios:
Ben Skeggs330c5982010-09-16 15:39:49 +1000702 nouveau_pm_fini(dev);
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000703 nouveau_bios_takedown(dev);
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200704out_display_early:
705 engine->display.late_takedown(dev);
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000706out:
707 vga_client_register(dev->pdev, NULL, NULL, NULL);
708 return ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000709}
710
711static void nouveau_card_takedown(struct drm_device *dev)
712{
713 struct drm_nouveau_private *dev_priv = dev->dev_private;
714 struct nouveau_engine *engine = &dev_priv->engine;
Ben Skeggs6dfdd7a2011-03-31 15:40:43 +1000715 int e;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000716
Ben Skeggsa82dd492011-04-01 13:56:05 +1000717 if (dev_priv->channel) {
Francisco Jerez0c6c1c22010-09-22 00:58:54 +0200718 nouveau_fence_fini(dev);
Francisco Jerez36c952e2010-10-18 03:01:34 +0200719 nouveau_channel_put_unlocked(&dev_priv->channel);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000720 }
Ben Skeggsb6d3d872010-06-07 15:38:27 +1000721
722 if (!nouveau_noaccel) {
723 engine->fifo.takedown(dev);
Ben Skeggs6dfdd7a2011-03-31 15:40:43 +1000724 for (e = NVOBJ_ENGINE_NR - 1; e >= 0; e--) {
725 if (dev_priv->eng[e]) {
726 dev_priv->eng[e]->fini(dev, e);
727 dev_priv->eng[e]->destroy(dev,e );
728 }
729 }
Ben Skeggsb6d3d872010-06-07 15:38:27 +1000730 }
731 engine->fb.takedown(dev);
732 engine->timer.takedown(dev);
Ben Skeggsee2e0132010-07-26 09:28:25 +1000733 engine->gpio.takedown(dev);
Ben Skeggsb6d3d872010-06-07 15:38:27 +1000734 engine->mc.takedown(dev);
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200735 engine->display.late_takedown(dev);
Ben Skeggsb6d3d872010-06-07 15:38:27 +1000736
737 mutex_lock(&dev->struct_mutex);
738 ttm_bo_clean_mm(&dev_priv->ttm.bdev, TTM_PL_VRAM);
739 ttm_bo_clean_mm(&dev_priv->ttm.bdev, TTM_PL_TT);
740 mutex_unlock(&dev->struct_mutex);
Ben Skeggsfbd2895e2010-09-01 15:24:34 +1000741 nouveau_mem_gart_fini(dev);
Ben Skeggsb6d3d872010-06-07 15:38:27 +1000742
Ben Skeggsb6d3d872010-06-07 15:38:27 +1000743 engine->instmem.takedown(dev);
Ben Skeggsfbd2895e2010-09-01 15:24:34 +1000744 nouveau_gpuobj_takedown(dev);
745 nouveau_mem_vram_fini(dev);
Ben Skeggsb6d3d872010-06-07 15:38:27 +1000746
Ben Skeggs35fa2f22010-10-21 14:07:03 +1000747 nouveau_irq_fini(dev);
Francisco Jerez042206c2010-10-21 18:19:29 +0200748 drm_vblank_cleanup(dev);
Ben Skeggsb6d3d872010-06-07 15:38:27 +1000749
Ben Skeggs330c5982010-09-16 15:39:49 +1000750 nouveau_pm_fini(dev);
Ben Skeggsb6d3d872010-06-07 15:38:27 +1000751 nouveau_bios_takedown(dev);
752
753 vga_client_register(dev->pdev, NULL, NULL, NULL);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000754}
755
756/* here a client dies, release the stuff that was allocated for its
757 * file_priv */
758void nouveau_preclose(struct drm_device *dev, struct drm_file *file_priv)
759{
760 nouveau_channel_cleanup(dev, file_priv);
761}
762
763/* first module load, setup the mmio/fb mapping */
764/* KMS: we need mmio at load time, not when the first drm client opens. */
765int nouveau_firstopen(struct drm_device *dev)
766{
767 return 0;
768}
769
770/* if we have an OF card, copy vbios to RAMIN */
771static void nouveau_OF_copy_vbios_to_ramin(struct drm_device *dev)
772{
773#if defined(__powerpc__)
774 int size, i;
775 const uint32_t *bios;
776 struct device_node *dn = pci_device_to_OF_node(dev->pdev);
777 if (!dn) {
778 NV_INFO(dev, "Unable to get the OF node\n");
779 return;
780 }
781
782 bios = of_get_property(dn, "NVDA,BMP", &size);
783 if (bios) {
784 for (i = 0; i < size; i += 4)
785 nv_wi32(dev, i, bios[i/4]);
786 NV_INFO(dev, "OF bios successfully copied (%d bytes)\n", size);
787 } else {
788 NV_INFO(dev, "Unable to get the OF bios\n");
789 }
790#endif
791}
792
Marcin Slusarz06415c52010-05-16 17:29:56 +0200793static struct apertures_struct *nouveau_get_apertures(struct drm_device *dev)
794{
795 struct pci_dev *pdev = dev->pdev;
796 struct apertures_struct *aper = alloc_apertures(3);
797 if (!aper)
798 return NULL;
799
800 aper->ranges[0].base = pci_resource_start(pdev, 1);
801 aper->ranges[0].size = pci_resource_len(pdev, 1);
802 aper->count = 1;
803
804 if (pci_resource_len(pdev, 2)) {
805 aper->ranges[aper->count].base = pci_resource_start(pdev, 2);
806 aper->ranges[aper->count].size = pci_resource_len(pdev, 2);
807 aper->count++;
808 }
809
810 if (pci_resource_len(pdev, 3)) {
811 aper->ranges[aper->count].base = pci_resource_start(pdev, 3);
812 aper->ranges[aper->count].size = pci_resource_len(pdev, 3);
813 aper->count++;
814 }
815
816 return aper;
817}
818
819static int nouveau_remove_conflicting_drivers(struct drm_device *dev)
820{
821 struct drm_nouveau_private *dev_priv = dev->dev_private;
Marcin Slusarz3b9676e2010-05-16 17:33:09 +0200822 bool primary = false;
Marcin Slusarz06415c52010-05-16 17:29:56 +0200823 dev_priv->apertures = nouveau_get_apertures(dev);
824 if (!dev_priv->apertures)
825 return -ENOMEM;
826
Marcin Slusarz3b9676e2010-05-16 17:33:09 +0200827#ifdef CONFIG_X86
828 primary = dev->pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW;
829#endif
Emil Velikovf2129492011-03-19 23:31:52 +0000830
Marcin Slusarz3b9676e2010-05-16 17:33:09 +0200831 remove_conflicting_framebuffers(dev_priv->apertures, "nouveaufb", primary);
Marcin Slusarz06415c52010-05-16 17:29:56 +0200832 return 0;
833}
834
Ben Skeggs6ee73862009-12-11 19:24:15 +1000835int nouveau_load(struct drm_device *dev, unsigned long flags)
836{
837 struct drm_nouveau_private *dev_priv;
838 uint32_t reg0;
839 resource_size_t mmio_start_offs;
Ben Skeggscd0b0722010-06-01 15:56:22 +1000840 int ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000841
842 dev_priv = kzalloc(sizeof(*dev_priv), GFP_KERNEL);
Dan Carpentera0d069e2010-07-30 17:04:32 +0200843 if (!dev_priv) {
844 ret = -ENOMEM;
845 goto err_out;
846 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000847 dev->dev_private = dev_priv;
848 dev_priv->dev = dev;
849
850 dev_priv->flags = flags & NOUVEAU_FLAGS;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000851
852 NV_DEBUG(dev, "vendor: 0x%X device: 0x%X class: 0x%X\n",
853 dev->pci_vendor, dev->pci_device, dev->pdev->class);
854
Ben Skeggs6ee73862009-12-11 19:24:15 +1000855 /* resource 0 is mmio regs */
856 /* resource 1 is linear FB */
857 /* resource 2 is RAMIN (mmio regs + 0x1000000) */
858 /* resource 6 is bios */
859
860 /* map the mmio regs */
861 mmio_start_offs = pci_resource_start(dev->pdev, 0);
862 dev_priv->mmio = ioremap(mmio_start_offs, 0x00800000);
863 if (!dev_priv->mmio) {
864 NV_ERROR(dev, "Unable to initialize the mmio mapping. "
865 "Please report your setup to " DRIVER_EMAIL "\n");
Dan Carpentera0d069e2010-07-30 17:04:32 +0200866 ret = -EINVAL;
Tejun Heod82f8e62011-01-26 17:49:18 +0100867 goto err_priv;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000868 }
869 NV_DEBUG(dev, "regs mapped ok at 0x%llx\n",
870 (unsigned long long)mmio_start_offs);
871
872#ifdef __BIG_ENDIAN
873 /* Put the card in BE mode if it's not */
874 if (nv_rd32(dev, NV03_PMC_BOOT_1))
875 nv_wr32(dev, NV03_PMC_BOOT_1, 0x00000001);
876
877 DRM_MEMORYBARRIER();
878#endif
879
880 /* Time to determine the card architecture */
881 reg0 = nv_rd32(dev, NV03_PMC_BOOT_0);
Roy Spliet50066f82011-03-27 18:13:11 +0200882 dev_priv->stepping = 0; /* XXX: add stepping for pre-NV10? */
Ben Skeggs6ee73862009-12-11 19:24:15 +1000883
884 /* We're dealing with >=NV10 */
885 if ((reg0 & 0x0f000000) > 0) {
886 /* Bit 27-20 contain the architecture in hex */
887 dev_priv->chipset = (reg0 & 0xff00000) >> 20;
Roy Spliet50066f82011-03-27 18:13:11 +0200888 dev_priv->stepping = (reg0 & 0xff);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000889 /* NV04 or NV05 */
890 } else if ((reg0 & 0xff00fff0) == 0x20004000) {
Ben Skeggs1dee7a92010-01-07 13:47:57 +1000891 if (reg0 & 0x00f00000)
892 dev_priv->chipset = 0x05;
893 else
894 dev_priv->chipset = 0x04;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000895 } else
896 dev_priv->chipset = 0xff;
897
898 switch (dev_priv->chipset & 0xf0) {
899 case 0x00:
900 case 0x10:
901 case 0x20:
902 case 0x30:
903 dev_priv->card_type = dev_priv->chipset & 0xf0;
904 break;
905 case 0x40:
906 case 0x60:
907 dev_priv->card_type = NV_40;
908 break;
909 case 0x50:
910 case 0x80:
911 case 0x90:
912 case 0xa0:
913 dev_priv->card_type = NV_50;
914 break;
Ben Skeggs4b223ee2010-08-03 10:00:56 +1000915 case 0xc0:
916 dev_priv->card_type = NV_C0;
917 break;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000918 default:
919 NV_INFO(dev, "Unsupported chipset 0x%08x\n", reg0);
Dan Carpentera0d069e2010-07-30 17:04:32 +0200920 ret = -EINVAL;
921 goto err_mmio;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000922 }
923
924 NV_INFO(dev, "Detected an NV%2x generation card (0x%08x)\n",
925 dev_priv->card_type, reg0);
926
Ben Skeggscd0b0722010-06-01 15:56:22 +1000927 ret = nouveau_remove_conflicting_drivers(dev);
928 if (ret)
Dan Carpentera0d069e2010-07-30 17:04:32 +0200929 goto err_mmio;
Marcin Slusarz06415c52010-05-16 17:29:56 +0200930
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300931 /* Map PRAMIN BAR, or on older cards, the aperture within BAR0 */
Ben Skeggs6ee73862009-12-11 19:24:15 +1000932 if (dev_priv->card_type >= NV_40) {
933 int ramin_bar = 2;
934 if (pci_resource_len(dev->pdev, ramin_bar) == 0)
935 ramin_bar = 3;
936
937 dev_priv->ramin_size = pci_resource_len(dev->pdev, ramin_bar);
Ben Skeggs6d696302010-06-02 10:16:24 +1000938 dev_priv->ramin =
939 ioremap(pci_resource_start(dev->pdev, ramin_bar),
Ben Skeggs6ee73862009-12-11 19:24:15 +1000940 dev_priv->ramin_size);
941 if (!dev_priv->ramin) {
Ben Skeggs6d696302010-06-02 10:16:24 +1000942 NV_ERROR(dev, "Failed to PRAMIN BAR");
Dan Carpentera0d069e2010-07-30 17:04:32 +0200943 ret = -ENOMEM;
944 goto err_mmio;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000945 }
Ben Skeggs6d696302010-06-02 10:16:24 +1000946 } else {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000947 dev_priv->ramin_size = 1 * 1024 * 1024;
948 dev_priv->ramin = ioremap(mmio_start_offs + NV_RAMIN,
Ben Skeggs6d696302010-06-02 10:16:24 +1000949 dev_priv->ramin_size);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000950 if (!dev_priv->ramin) {
951 NV_ERROR(dev, "Failed to map BAR0 PRAMIN.\n");
Dan Carpentera0d069e2010-07-30 17:04:32 +0200952 ret = -ENOMEM;
953 goto err_mmio;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000954 }
955 }
956
957 nouveau_OF_copy_vbios_to_ramin(dev);
958
959 /* Special flags */
960 if (dev->pci_device == 0x01a0)
961 dev_priv->flags |= NV_NFORCE;
962 else if (dev->pci_device == 0x01f0)
963 dev_priv->flags |= NV_NFORCE2;
964
965 /* For kernel modesetting, init card now and bring up fbcon */
Ben Skeggscd0b0722010-06-01 15:56:22 +1000966 ret = nouveau_card_init(dev);
967 if (ret)
Dan Carpentera0d069e2010-07-30 17:04:32 +0200968 goto err_ramin;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000969
970 return 0;
Dan Carpentera0d069e2010-07-30 17:04:32 +0200971
972err_ramin:
973 iounmap(dev_priv->ramin);
974err_mmio:
975 iounmap(dev_priv->mmio);
Dan Carpentera0d069e2010-07-30 17:04:32 +0200976err_priv:
977 kfree(dev_priv);
978 dev->dev_private = NULL;
979err_out:
980 return ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000981}
982
Ben Skeggs6ee73862009-12-11 19:24:15 +1000983void nouveau_lastclose(struct drm_device *dev)
984{
Dave Airlie5ccb3772010-12-07 13:56:26 +1000985 vga_switcheroo_process_delayed_switch();
Ben Skeggs6ee73862009-12-11 19:24:15 +1000986}
987
988int nouveau_unload(struct drm_device *dev)
989{
990 struct drm_nouveau_private *dev_priv = dev->dev_private;
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200991 struct nouveau_engine *engine = &dev_priv->engine;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000992
Ben Skeggscd0b0722010-06-01 15:56:22 +1000993 drm_kms_helper_poll_fini(dev);
994 nouveau_fbcon_fini(dev);
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200995 engine->display.destroy(dev);
Ben Skeggscd0b0722010-06-01 15:56:22 +1000996 nouveau_card_takedown(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000997
998 iounmap(dev_priv->mmio);
999 iounmap(dev_priv->ramin);
1000
1001 kfree(dev_priv);
1002 dev->dev_private = NULL;
1003 return 0;
1004}
1005
Ben Skeggs6ee73862009-12-11 19:24:15 +10001006int nouveau_ioctl_getparam(struct drm_device *dev, void *data,
1007 struct drm_file *file_priv)
1008{
1009 struct drm_nouveau_private *dev_priv = dev->dev_private;
1010 struct drm_nouveau_getparam *getparam = data;
1011
Ben Skeggs6ee73862009-12-11 19:24:15 +10001012 switch (getparam->param) {
1013 case NOUVEAU_GETPARAM_CHIPSET_ID:
1014 getparam->value = dev_priv->chipset;
1015 break;
1016 case NOUVEAU_GETPARAM_PCI_VENDOR:
1017 getparam->value = dev->pci_vendor;
1018 break;
1019 case NOUVEAU_GETPARAM_PCI_DEVICE:
1020 getparam->value = dev->pci_device;
1021 break;
1022 case NOUVEAU_GETPARAM_BUS_TYPE:
Dave Airlie8410ea32010-12-15 03:16:38 +10001023 if (drm_pci_device_is_agp(dev))
Ben Skeggs6ee73862009-12-11 19:24:15 +10001024 getparam->value = NV_AGP;
Dave Airlie8410ea32010-12-15 03:16:38 +10001025 else if (drm_pci_device_is_pcie(dev))
Ben Skeggs6ee73862009-12-11 19:24:15 +10001026 getparam->value = NV_PCIE;
1027 else
1028 getparam->value = NV_PCI;
1029 break;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001030 case NOUVEAU_GETPARAM_FB_SIZE:
1031 getparam->value = dev_priv->fb_available_size;
1032 break;
1033 case NOUVEAU_GETPARAM_AGP_SIZE:
1034 getparam->value = dev_priv->gart_info.aper_size;
1035 break;
1036 case NOUVEAU_GETPARAM_VM_VRAM_BASE:
Ben Skeggs6d6c5a12010-11-16 10:17:53 +10001037 getparam->value = 0; /* deprecated */
Ben Skeggs6ee73862009-12-11 19:24:15 +10001038 break;
Marcin Koƛcielnicki7fc74f12010-05-23 11:36:04 +00001039 case NOUVEAU_GETPARAM_PTIMER_TIME:
1040 getparam->value = dev_priv->engine.timer.read(dev);
1041 break;
Francisco Jerezf13b3262010-10-10 06:01:08 +02001042 case NOUVEAU_GETPARAM_HAS_BO_USAGE:
1043 getparam->value = 1;
1044 break;
Francisco Jerez332b2422010-10-20 23:35:40 +02001045 case NOUVEAU_GETPARAM_HAS_PAGEFLIP:
Ben Skeggsbd2f2032011-02-08 15:16:23 +10001046 getparam->value = 1;
Francisco Jerez332b2422010-10-20 23:35:40 +02001047 break;
Marcin Koƛcielnicki69c97002010-01-26 18:39:20 +00001048 case NOUVEAU_GETPARAM_GRAPH_UNITS:
1049 /* NV40 and NV50 versions are quite different, but register
1050 * address is the same. User is supposed to know the card
1051 * family anyway... */
1052 if (dev_priv->chipset >= 0x40) {
1053 getparam->value = nv_rd32(dev, NV40_PMC_GRAPH_UNITS);
1054 break;
1055 }
1056 /* FALLTHRU */
Ben Skeggs6ee73862009-12-11 19:24:15 +10001057 default:
Francisco Jerez1397b422010-10-12 03:17:43 +02001058 NV_DEBUG(dev, "unknown parameter %lld\n", getparam->param);
Ben Skeggs6ee73862009-12-11 19:24:15 +10001059 return -EINVAL;
1060 }
1061
1062 return 0;
1063}
1064
1065int
1066nouveau_ioctl_setparam(struct drm_device *dev, void *data,
1067 struct drm_file *file_priv)
1068{
1069 struct drm_nouveau_setparam *setparam = data;
1070
Ben Skeggs6ee73862009-12-11 19:24:15 +10001071 switch (setparam->param) {
1072 default:
Francisco Jerez1397b422010-10-12 03:17:43 +02001073 NV_DEBUG(dev, "unknown parameter %lld\n", setparam->param);
Ben Skeggs6ee73862009-12-11 19:24:15 +10001074 return -EINVAL;
1075 }
1076
1077 return 0;
1078}
1079
1080/* Wait until (value(reg) & mask) == val, up until timeout has hit */
Ben Skeggs12fb9522010-11-19 14:32:56 +10001081bool
1082nouveau_wait_eq(struct drm_device *dev, uint64_t timeout,
1083 uint32_t reg, uint32_t mask, uint32_t val)
Ben Skeggs6ee73862009-12-11 19:24:15 +10001084{
1085 struct drm_nouveau_private *dev_priv = dev->dev_private;
1086 struct nouveau_timer_engine *ptimer = &dev_priv->engine.timer;
1087 uint64_t start = ptimer->read(dev);
1088
1089 do {
1090 if ((nv_rd32(dev, reg) & mask) == val)
1091 return true;
1092 } while (ptimer->read(dev) - start < timeout);
1093
1094 return false;
1095}
1096
Ben Skeggs12fb9522010-11-19 14:32:56 +10001097/* Wait until (value(reg) & mask) != val, up until timeout has hit */
1098bool
1099nouveau_wait_ne(struct drm_device *dev, uint64_t timeout,
1100 uint32_t reg, uint32_t mask, uint32_t val)
1101{
1102 struct drm_nouveau_private *dev_priv = dev->dev_private;
1103 struct nouveau_timer_engine *ptimer = &dev_priv->engine.timer;
1104 uint64_t start = ptimer->read(dev);
1105
1106 do {
1107 if ((nv_rd32(dev, reg) & mask) != val)
1108 return true;
1109 } while (ptimer->read(dev) - start < timeout);
1110
1111 return false;
1112}
1113
Ben Skeggs6ee73862009-12-11 19:24:15 +10001114/* Waits for PGRAPH to go completely idle */
1115bool nouveau_wait_for_idle(struct drm_device *dev)
1116{
Francisco Jerez0541324a2010-10-18 16:15:15 +02001117 struct drm_nouveau_private *dev_priv = dev->dev_private;
1118 uint32_t mask = ~0;
1119
1120 if (dev_priv->card_type == NV_40)
1121 mask &= ~NV40_PGRAPH_STATUS_SYNC_STALL;
1122
1123 if (!nv_wait(dev, NV04_PGRAPH_STATUS, mask, 0)) {
Ben Skeggs6ee73862009-12-11 19:24:15 +10001124 NV_ERROR(dev, "PGRAPH idle timed out with status 0x%08x\n",
1125 nv_rd32(dev, NV04_PGRAPH_STATUS));
1126 return false;
1127 }
1128
1129 return true;
1130}
1131