blob: f80c5e0762ff90b4c29ec186ee1c5c4017f2fc7d [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 Skeggsa0b25632011-11-21 16:41:48 +100039#include "nouveau_gpio.h"
Ben Skeggs330c5982010-09-16 15:39:49 +100040#include "nouveau_pm.h"
Ben Skeggs6ee73862009-12-11 19:24:15 +100041#include "nv50_display.h"
42
Ben Skeggs6ee73862009-12-11 19:24:15 +100043static void nouveau_stub_takedown(struct drm_device *dev) {}
Ben Skeggsee2e0132010-07-26 09:28:25 +100044static int nouveau_stub_init(struct drm_device *dev) { return 0; }
Ben Skeggs6ee73862009-12-11 19:24:15 +100045
46static int nouveau_init_engine_ptrs(struct drm_device *dev)
47{
48 struct drm_nouveau_private *dev_priv = dev->dev_private;
49 struct nouveau_engine *engine = &dev_priv->engine;
50
51 switch (dev_priv->chipset & 0xf0) {
52 case 0x00:
53 engine->instmem.init = nv04_instmem_init;
54 engine->instmem.takedown = nv04_instmem_takedown;
55 engine->instmem.suspend = nv04_instmem_suspend;
56 engine->instmem.resume = nv04_instmem_resume;
Ben Skeggse41115d2010-11-01 11:45:02 +100057 engine->instmem.get = nv04_instmem_get;
58 engine->instmem.put = nv04_instmem_put;
59 engine->instmem.map = nv04_instmem_map;
60 engine->instmem.unmap = nv04_instmem_unmap;
Ben Skeggsf56cb862010-07-08 11:29:10 +100061 engine->instmem.flush = nv04_instmem_flush;
Ben Skeggs6ee73862009-12-11 19:24:15 +100062 engine->mc.init = nv04_mc_init;
63 engine->mc.takedown = nv04_mc_takedown;
64 engine->timer.init = nv04_timer_init;
65 engine->timer.read = nv04_timer_read;
66 engine->timer.takedown = nv04_timer_takedown;
67 engine->fb.init = nv04_fb_init;
68 engine->fb.takedown = nv04_fb_takedown;
Ben Skeggs6ee73862009-12-11 19:24:15 +100069 engine->fifo.channels = 16;
70 engine->fifo.init = nv04_fifo_init;
Ben Skeggs5178d402010-11-03 10:56:05 +100071 engine->fifo.takedown = nv04_fifo_fini;
Ben Skeggs6ee73862009-12-11 19:24:15 +100072 engine->fifo.disable = nv04_fifo_disable;
73 engine->fifo.enable = nv04_fifo_enable;
74 engine->fifo.reassign = nv04_fifo_reassign;
Francisco Jerez588d7d12009-12-13 20:07:42 +010075 engine->fifo.cache_pull = nv04_fifo_cache_pull;
Ben Skeggs6ee73862009-12-11 19:24:15 +100076 engine->fifo.channel_id = nv04_fifo_channel_id;
77 engine->fifo.create_context = nv04_fifo_create_context;
78 engine->fifo.destroy_context = nv04_fifo_destroy_context;
79 engine->fifo.load_context = nv04_fifo_load_context;
80 engine->fifo.unload_context = nv04_fifo_unload_context;
Francisco Jerezc88c2e02010-07-24 17:37:33 +020081 engine->display.early_init = nv04_display_early_init;
82 engine->display.late_takedown = nv04_display_late_takedown;
83 engine->display.create = nv04_display_create;
Francisco Jerezc88c2e02010-07-24 17:37:33 +020084 engine->display.destroy = nv04_display_destroy;
Ben Skeggs2a44e492011-11-09 11:36:33 +100085 engine->display.init = nv04_display_init;
86 engine->display.fini = nv04_display_fini;
Ben Skeggs36f13172011-10-27 10:24:12 +100087 engine->pm.clocks_get = nv04_pm_clocks_get;
88 engine->pm.clocks_pre = nv04_pm_clocks_pre;
89 engine->pm.clocks_set = nv04_pm_clocks_set;
Ben Skeggs60d2a882010-12-06 15:28:54 +100090 engine->vram.init = nouveau_mem_detect;
Ben Skeggs24f246a2011-06-10 13:36:08 +100091 engine->vram.takedown = nouveau_stub_takedown;
Ben Skeggs60d2a882010-12-06 15:28:54 +100092 engine->vram.flags_valid = nouveau_mem_flags_valid;
Ben Skeggs6ee73862009-12-11 19:24:15 +100093 break;
94 case 0x10:
95 engine->instmem.init = nv04_instmem_init;
96 engine->instmem.takedown = nv04_instmem_takedown;
97 engine->instmem.suspend = nv04_instmem_suspend;
98 engine->instmem.resume = nv04_instmem_resume;
Ben Skeggse41115d2010-11-01 11:45:02 +100099 engine->instmem.get = nv04_instmem_get;
100 engine->instmem.put = nv04_instmem_put;
101 engine->instmem.map = nv04_instmem_map;
102 engine->instmem.unmap = nv04_instmem_unmap;
Ben Skeggsf56cb862010-07-08 11:29:10 +1000103 engine->instmem.flush = nv04_instmem_flush;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000104 engine->mc.init = nv04_mc_init;
105 engine->mc.takedown = nv04_mc_takedown;
106 engine->timer.init = nv04_timer_init;
107 engine->timer.read = nv04_timer_read;
108 engine->timer.takedown = nv04_timer_takedown;
109 engine->fb.init = nv10_fb_init;
110 engine->fb.takedown = nv10_fb_takedown;
Francisco Jereza5cf68b2010-10-24 16:14:41 +0200111 engine->fb.init_tile_region = nv10_fb_init_tile_region;
112 engine->fb.set_tile_region = nv10_fb_set_tile_region;
113 engine->fb.free_tile_region = nv10_fb_free_tile_region;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000114 engine->fifo.channels = 32;
115 engine->fifo.init = nv10_fifo_init;
Ben Skeggs5178d402010-11-03 10:56:05 +1000116 engine->fifo.takedown = nv04_fifo_fini;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000117 engine->fifo.disable = nv04_fifo_disable;
118 engine->fifo.enable = nv04_fifo_enable;
119 engine->fifo.reassign = nv04_fifo_reassign;
Francisco Jerez588d7d12009-12-13 20:07:42 +0100120 engine->fifo.cache_pull = nv04_fifo_cache_pull;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000121 engine->fifo.channel_id = nv10_fifo_channel_id;
122 engine->fifo.create_context = nv10_fifo_create_context;
Francisco Jerez3945e472010-10-18 03:53:39 +0200123 engine->fifo.destroy_context = nv04_fifo_destroy_context;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000124 engine->fifo.load_context = nv10_fifo_load_context;
125 engine->fifo.unload_context = nv10_fifo_unload_context;
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200126 engine->display.early_init = nv04_display_early_init;
127 engine->display.late_takedown = nv04_display_late_takedown;
128 engine->display.create = nv04_display_create;
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200129 engine->display.destroy = nv04_display_destroy;
Ben Skeggs2a44e492011-11-09 11:36:33 +1000130 engine->display.init = nv04_display_init;
131 engine->display.fini = nv04_display_fini;
Ben Skeggsa0b25632011-11-21 16:41:48 +1000132 engine->gpio.drive = nv10_gpio_drive;
133 engine->gpio.sense = nv10_gpio_sense;
Ben Skeggs36f13172011-10-27 10:24:12 +1000134 engine->pm.clocks_get = nv04_pm_clocks_get;
135 engine->pm.clocks_pre = nv04_pm_clocks_pre;
136 engine->pm.clocks_set = nv04_pm_clocks_set;
Ben Skeggs60d2a882010-12-06 15:28:54 +1000137 engine->vram.init = nouveau_mem_detect;
Ben Skeggs24f246a2011-06-10 13:36:08 +1000138 engine->vram.takedown = nouveau_stub_takedown;
Ben Skeggs60d2a882010-12-06 15:28:54 +1000139 engine->vram.flags_valid = nouveau_mem_flags_valid;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000140 break;
141 case 0x20:
142 engine->instmem.init = nv04_instmem_init;
143 engine->instmem.takedown = nv04_instmem_takedown;
144 engine->instmem.suspend = nv04_instmem_suspend;
145 engine->instmem.resume = nv04_instmem_resume;
Ben Skeggse41115d2010-11-01 11:45:02 +1000146 engine->instmem.get = nv04_instmem_get;
147 engine->instmem.put = nv04_instmem_put;
148 engine->instmem.map = nv04_instmem_map;
149 engine->instmem.unmap = nv04_instmem_unmap;
Ben Skeggsf56cb862010-07-08 11:29:10 +1000150 engine->instmem.flush = nv04_instmem_flush;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000151 engine->mc.init = nv04_mc_init;
152 engine->mc.takedown = nv04_mc_takedown;
153 engine->timer.init = nv04_timer_init;
154 engine->timer.read = nv04_timer_read;
155 engine->timer.takedown = nv04_timer_takedown;
156 engine->fb.init = nv10_fb_init;
157 engine->fb.takedown = nv10_fb_takedown;
Francisco Jereza5cf68b2010-10-24 16:14:41 +0200158 engine->fb.init_tile_region = nv10_fb_init_tile_region;
159 engine->fb.set_tile_region = nv10_fb_set_tile_region;
160 engine->fb.free_tile_region = nv10_fb_free_tile_region;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000161 engine->fifo.channels = 32;
162 engine->fifo.init = nv10_fifo_init;
Ben Skeggs5178d402010-11-03 10:56:05 +1000163 engine->fifo.takedown = nv04_fifo_fini;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000164 engine->fifo.disable = nv04_fifo_disable;
165 engine->fifo.enable = nv04_fifo_enable;
166 engine->fifo.reassign = nv04_fifo_reassign;
Francisco Jerez588d7d12009-12-13 20:07:42 +0100167 engine->fifo.cache_pull = nv04_fifo_cache_pull;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000168 engine->fifo.channel_id = nv10_fifo_channel_id;
169 engine->fifo.create_context = nv10_fifo_create_context;
Francisco Jerez3945e472010-10-18 03:53:39 +0200170 engine->fifo.destroy_context = nv04_fifo_destroy_context;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000171 engine->fifo.load_context = nv10_fifo_load_context;
172 engine->fifo.unload_context = nv10_fifo_unload_context;
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200173 engine->display.early_init = nv04_display_early_init;
174 engine->display.late_takedown = nv04_display_late_takedown;
175 engine->display.create = nv04_display_create;
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200176 engine->display.destroy = nv04_display_destroy;
Ben Skeggs2a44e492011-11-09 11:36:33 +1000177 engine->display.init = nv04_display_init;
178 engine->display.fini = nv04_display_fini;
Ben Skeggsa0b25632011-11-21 16:41:48 +1000179 engine->gpio.drive = nv10_gpio_drive;
180 engine->gpio.sense = nv10_gpio_sense;
Ben Skeggs36f13172011-10-27 10:24:12 +1000181 engine->pm.clocks_get = nv04_pm_clocks_get;
182 engine->pm.clocks_pre = nv04_pm_clocks_pre;
183 engine->pm.clocks_set = nv04_pm_clocks_set;
Ben Skeggs60d2a882010-12-06 15:28:54 +1000184 engine->vram.init = nouveau_mem_detect;
Ben Skeggs24f246a2011-06-10 13:36:08 +1000185 engine->vram.takedown = nouveau_stub_takedown;
Ben Skeggs60d2a882010-12-06 15:28:54 +1000186 engine->vram.flags_valid = nouveau_mem_flags_valid;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000187 break;
188 case 0x30:
189 engine->instmem.init = nv04_instmem_init;
190 engine->instmem.takedown = nv04_instmem_takedown;
191 engine->instmem.suspend = nv04_instmem_suspend;
192 engine->instmem.resume = nv04_instmem_resume;
Ben Skeggse41115d2010-11-01 11:45:02 +1000193 engine->instmem.get = nv04_instmem_get;
194 engine->instmem.put = nv04_instmem_put;
195 engine->instmem.map = nv04_instmem_map;
196 engine->instmem.unmap = nv04_instmem_unmap;
Ben Skeggsf56cb862010-07-08 11:29:10 +1000197 engine->instmem.flush = nv04_instmem_flush;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000198 engine->mc.init = nv04_mc_init;
199 engine->mc.takedown = nv04_mc_takedown;
200 engine->timer.init = nv04_timer_init;
201 engine->timer.read = nv04_timer_read;
202 engine->timer.takedown = nv04_timer_takedown;
Francisco Jerez8bded182010-07-21 21:08:11 +0200203 engine->fb.init = nv30_fb_init;
204 engine->fb.takedown = nv30_fb_takedown;
Francisco Jereza5cf68b2010-10-24 16:14:41 +0200205 engine->fb.init_tile_region = nv30_fb_init_tile_region;
206 engine->fb.set_tile_region = nv10_fb_set_tile_region;
207 engine->fb.free_tile_region = nv30_fb_free_tile_region;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000208 engine->fifo.channels = 32;
209 engine->fifo.init = nv10_fifo_init;
Ben Skeggs5178d402010-11-03 10:56:05 +1000210 engine->fifo.takedown = nv04_fifo_fini;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000211 engine->fifo.disable = nv04_fifo_disable;
212 engine->fifo.enable = nv04_fifo_enable;
213 engine->fifo.reassign = nv04_fifo_reassign;
Francisco Jerez588d7d12009-12-13 20:07:42 +0100214 engine->fifo.cache_pull = nv04_fifo_cache_pull;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000215 engine->fifo.channel_id = nv10_fifo_channel_id;
216 engine->fifo.create_context = nv10_fifo_create_context;
Francisco Jerez3945e472010-10-18 03:53:39 +0200217 engine->fifo.destroy_context = nv04_fifo_destroy_context;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000218 engine->fifo.load_context = nv10_fifo_load_context;
219 engine->fifo.unload_context = nv10_fifo_unload_context;
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200220 engine->display.early_init = nv04_display_early_init;
221 engine->display.late_takedown = nv04_display_late_takedown;
222 engine->display.create = nv04_display_create;
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200223 engine->display.destroy = nv04_display_destroy;
Ben Skeggs2a44e492011-11-09 11:36:33 +1000224 engine->display.init = nv04_display_init;
225 engine->display.fini = nv04_display_fini;
Ben Skeggsa0b25632011-11-21 16:41:48 +1000226 engine->gpio.drive = nv10_gpio_drive;
227 engine->gpio.sense = nv10_gpio_sense;
Ben Skeggs36f13172011-10-27 10:24:12 +1000228 engine->pm.clocks_get = nv04_pm_clocks_get;
229 engine->pm.clocks_pre = nv04_pm_clocks_pre;
230 engine->pm.clocks_set = nv04_pm_clocks_set;
Ben Skeggs442b6262010-09-16 16:25:26 +1000231 engine->pm.voltage_get = nouveau_voltage_gpio_get;
232 engine->pm.voltage_set = nouveau_voltage_gpio_set;
Ben Skeggs60d2a882010-12-06 15:28:54 +1000233 engine->vram.init = nouveau_mem_detect;
Ben Skeggs24f246a2011-06-10 13:36:08 +1000234 engine->vram.takedown = nouveau_stub_takedown;
Ben Skeggs60d2a882010-12-06 15:28:54 +1000235 engine->vram.flags_valid = nouveau_mem_flags_valid;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000236 break;
237 case 0x40:
238 case 0x60:
239 engine->instmem.init = nv04_instmem_init;
240 engine->instmem.takedown = nv04_instmem_takedown;
241 engine->instmem.suspend = nv04_instmem_suspend;
242 engine->instmem.resume = nv04_instmem_resume;
Ben Skeggse41115d2010-11-01 11:45:02 +1000243 engine->instmem.get = nv04_instmem_get;
244 engine->instmem.put = nv04_instmem_put;
245 engine->instmem.map = nv04_instmem_map;
246 engine->instmem.unmap = nv04_instmem_unmap;
Ben Skeggsf56cb862010-07-08 11:29:10 +1000247 engine->instmem.flush = nv04_instmem_flush;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000248 engine->mc.init = nv40_mc_init;
249 engine->mc.takedown = nv40_mc_takedown;
250 engine->timer.init = nv04_timer_init;
251 engine->timer.read = nv04_timer_read;
252 engine->timer.takedown = nv04_timer_takedown;
253 engine->fb.init = nv40_fb_init;
254 engine->fb.takedown = nv40_fb_takedown;
Francisco Jereza5cf68b2010-10-24 16:14:41 +0200255 engine->fb.init_tile_region = nv30_fb_init_tile_region;
256 engine->fb.set_tile_region = nv40_fb_set_tile_region;
257 engine->fb.free_tile_region = nv30_fb_free_tile_region;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000258 engine->fifo.channels = 32;
259 engine->fifo.init = nv40_fifo_init;
Ben Skeggs5178d402010-11-03 10:56:05 +1000260 engine->fifo.takedown = nv04_fifo_fini;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000261 engine->fifo.disable = nv04_fifo_disable;
262 engine->fifo.enable = nv04_fifo_enable;
263 engine->fifo.reassign = nv04_fifo_reassign;
Francisco Jerez588d7d12009-12-13 20:07:42 +0100264 engine->fifo.cache_pull = nv04_fifo_cache_pull;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000265 engine->fifo.channel_id = nv10_fifo_channel_id;
266 engine->fifo.create_context = nv40_fifo_create_context;
Francisco Jerez3945e472010-10-18 03:53:39 +0200267 engine->fifo.destroy_context = nv04_fifo_destroy_context;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000268 engine->fifo.load_context = nv40_fifo_load_context;
269 engine->fifo.unload_context = nv40_fifo_unload_context;
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200270 engine->display.early_init = nv04_display_early_init;
271 engine->display.late_takedown = nv04_display_late_takedown;
272 engine->display.create = nv04_display_create;
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200273 engine->display.destroy = nv04_display_destroy;
Ben Skeggs2a44e492011-11-09 11:36:33 +1000274 engine->display.init = nv04_display_init;
275 engine->display.fini = nv04_display_fini;
Ben Skeggs47e5d5c2011-11-22 13:49:22 +1000276 engine->gpio.init = nv10_gpio_init;
277 engine->gpio.fini = nv10_gpio_fini;
Ben Skeggsa0b25632011-11-21 16:41:48 +1000278 engine->gpio.drive = nv10_gpio_drive;
279 engine->gpio.sense = nv10_gpio_sense;
Ben Skeggs47e5d5c2011-11-22 13:49:22 +1000280 engine->gpio.irq_enable = nv10_gpio_irq_enable;
Ben Skeggs1262a202011-07-18 15:15:34 +1000281 engine->pm.clocks_get = nv40_pm_clocks_get;
282 engine->pm.clocks_pre = nv40_pm_clocks_pre;
283 engine->pm.clocks_set = nv40_pm_clocks_set;
Ben Skeggs442b6262010-09-16 16:25:26 +1000284 engine->pm.voltage_get = nouveau_voltage_gpio_get;
285 engine->pm.voltage_set = nouveau_voltage_gpio_set;
Francisco Jerez8155cac2010-09-23 20:58:38 +0200286 engine->pm.temp_get = nv40_temp_get;
Ben Skeggs69346182011-09-17 02:11:39 +1000287 engine->pm.pwm_get = nv40_pm_pwm_get;
288 engine->pm.pwm_set = nv40_pm_pwm_set;
Ben Skeggs60d2a882010-12-06 15:28:54 +1000289 engine->vram.init = nouveau_mem_detect;
Ben Skeggs24f246a2011-06-10 13:36:08 +1000290 engine->vram.takedown = nouveau_stub_takedown;
Ben Skeggs60d2a882010-12-06 15:28:54 +1000291 engine->vram.flags_valid = nouveau_mem_flags_valid;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000292 break;
293 case 0x50:
294 case 0x80: /* gotta love NVIDIA's consistency.. */
295 case 0x90:
Ben Skeggsd9f61c22011-07-04 13:25:17 +1000296 case 0xa0:
Ben Skeggs6ee73862009-12-11 19:24:15 +1000297 engine->instmem.init = nv50_instmem_init;
298 engine->instmem.takedown = nv50_instmem_takedown;
299 engine->instmem.suspend = nv50_instmem_suspend;
300 engine->instmem.resume = nv50_instmem_resume;
Ben Skeggse41115d2010-11-01 11:45:02 +1000301 engine->instmem.get = nv50_instmem_get;
302 engine->instmem.put = nv50_instmem_put;
303 engine->instmem.map = nv50_instmem_map;
304 engine->instmem.unmap = nv50_instmem_unmap;
Ben Skeggs734ee832010-07-15 11:02:54 +1000305 if (dev_priv->chipset == 0x50)
306 engine->instmem.flush = nv50_instmem_flush;
307 else
308 engine->instmem.flush = nv84_instmem_flush;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000309 engine->mc.init = nv50_mc_init;
310 engine->mc.takedown = nv50_mc_takedown;
311 engine->timer.init = nv04_timer_init;
312 engine->timer.read = nv04_timer_read;
313 engine->timer.takedown = nv04_timer_takedown;
Marcin Koƛcielnicki304424e2010-03-01 00:18:39 +0000314 engine->fb.init = nv50_fb_init;
315 engine->fb.takedown = nv50_fb_takedown;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000316 engine->fifo.channels = 128;
317 engine->fifo.init = nv50_fifo_init;
318 engine->fifo.takedown = nv50_fifo_takedown;
319 engine->fifo.disable = nv04_fifo_disable;
320 engine->fifo.enable = nv04_fifo_enable;
321 engine->fifo.reassign = nv04_fifo_reassign;
322 engine->fifo.channel_id = nv50_fifo_channel_id;
323 engine->fifo.create_context = nv50_fifo_create_context;
324 engine->fifo.destroy_context = nv50_fifo_destroy_context;
325 engine->fifo.load_context = nv50_fifo_load_context;
326 engine->fifo.unload_context = nv50_fifo_unload_context;
Ben Skeggs56ac7472010-10-22 10:26:24 +1000327 engine->fifo.tlb_flush = nv50_fifo_tlb_flush;
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200328 engine->display.early_init = nv50_display_early_init;
329 engine->display.late_takedown = nv50_display_late_takedown;
330 engine->display.create = nv50_display_create;
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200331 engine->display.destroy = nv50_display_destroy;
Ben Skeggs2a44e492011-11-09 11:36:33 +1000332 engine->display.init = nv50_display_init;
333 engine->display.fini = nv50_display_fini;
Ben Skeggsee2e0132010-07-26 09:28:25 +1000334 engine->gpio.init = nv50_gpio_init;
Ben Skeggsa0b25632011-11-21 16:41:48 +1000335 engine->gpio.fini = nv50_gpio_fini;
336 engine->gpio.drive = nv50_gpio_drive;
337 engine->gpio.sense = nv50_gpio_sense;
Ben Skeggsee2e0132010-07-26 09:28:25 +1000338 engine->gpio.irq_enable = nv50_gpio_irq_enable;
Ben Skeggsfade7ad2010-09-27 11:18:14 +1000339 switch (dev_priv->chipset) {
Ben Skeggsbd2e5972010-10-19 20:06:01 +1000340 case 0x84:
341 case 0x86:
342 case 0x92:
343 case 0x94:
344 case 0x96:
345 case 0x98:
346 case 0xa0:
Ben Skeggs5f801982010-10-22 08:44:09 +1000347 case 0xaa:
348 case 0xac:
Ben Skeggsbd2e5972010-10-19 20:06:01 +1000349 case 0x50:
Ben Skeggsf3fbaf32011-10-26 09:11:02 +1000350 engine->pm.clocks_get = nv50_pm_clocks_get;
351 engine->pm.clocks_pre = nv50_pm_clocks_pre;
352 engine->pm.clocks_set = nv50_pm_clocks_set;
Ben Skeggsfade7ad2010-09-27 11:18:14 +1000353 break;
Ben Skeggsbd2e5972010-10-19 20:06:01 +1000354 default:
Ben Skeggsca94a712011-06-17 15:38:48 +1000355 engine->pm.clocks_get = nva3_pm_clocks_get;
356 engine->pm.clocks_pre = nva3_pm_clocks_pre;
357 engine->pm.clocks_set = nva3_pm_clocks_set;
Ben Skeggsbd2e5972010-10-19 20:06:01 +1000358 break;
Ben Skeggsfade7ad2010-09-27 11:18:14 +1000359 }
Ben Skeggs02c30ca2010-09-16 16:17:35 +1000360 engine->pm.voltage_get = nouveau_voltage_gpio_get;
361 engine->pm.voltage_set = nouveau_voltage_gpio_set;
Francisco Jerez8155cac2010-09-23 20:58:38 +0200362 if (dev_priv->chipset >= 0x84)
363 engine->pm.temp_get = nv84_temp_get;
364 else
365 engine->pm.temp_get = nv40_temp_get;
Ben Skeggs5a4267a2011-09-17 02:01:24 +1000366 engine->pm.pwm_get = nv50_pm_pwm_get;
367 engine->pm.pwm_set = nv50_pm_pwm_set;
Ben Skeggs60d2a882010-12-06 15:28:54 +1000368 engine->vram.init = nv50_vram_init;
Ben Skeggs24f246a2011-06-10 13:36:08 +1000369 engine->vram.takedown = nv50_vram_fini;
Ben Skeggs60d2a882010-12-06 15:28:54 +1000370 engine->vram.get = nv50_vram_new;
371 engine->vram.put = nv50_vram_del;
372 engine->vram.flags_valid = nv50_vram_flags_valid;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000373 break;
Ben Skeggsd9f61c22011-07-04 13:25:17 +1000374 case 0xc0:
Ben Skeggs4b223ee2010-08-03 10:00:56 +1000375 engine->instmem.init = nvc0_instmem_init;
376 engine->instmem.takedown = nvc0_instmem_takedown;
377 engine->instmem.suspend = nvc0_instmem_suspend;
378 engine->instmem.resume = nvc0_instmem_resume;
Ben Skeggs8984e042010-11-15 11:48:33 +1000379 engine->instmem.get = nv50_instmem_get;
380 engine->instmem.put = nv50_instmem_put;
381 engine->instmem.map = nv50_instmem_map;
382 engine->instmem.unmap = nv50_instmem_unmap;
383 engine->instmem.flush = nv84_instmem_flush;
Ben Skeggs4b223ee2010-08-03 10:00:56 +1000384 engine->mc.init = nv50_mc_init;
385 engine->mc.takedown = nv50_mc_takedown;
386 engine->timer.init = nv04_timer_init;
387 engine->timer.read = nv04_timer_read;
388 engine->timer.takedown = nv04_timer_takedown;
389 engine->fb.init = nvc0_fb_init;
390 engine->fb.takedown = nvc0_fb_takedown;
Ben Skeggs4b223ee2010-08-03 10:00:56 +1000391 engine->fifo.channels = 128;
392 engine->fifo.init = nvc0_fifo_init;
393 engine->fifo.takedown = nvc0_fifo_takedown;
394 engine->fifo.disable = nvc0_fifo_disable;
395 engine->fifo.enable = nvc0_fifo_enable;
396 engine->fifo.reassign = nvc0_fifo_reassign;
397 engine->fifo.channel_id = nvc0_fifo_channel_id;
398 engine->fifo.create_context = nvc0_fifo_create_context;
399 engine->fifo.destroy_context = nvc0_fifo_destroy_context;
400 engine->fifo.load_context = nvc0_fifo_load_context;
401 engine->fifo.unload_context = nvc0_fifo_unload_context;
402 engine->display.early_init = nv50_display_early_init;
403 engine->display.late_takedown = nv50_display_late_takedown;
404 engine->display.create = nv50_display_create;
Ben Skeggs4b223ee2010-08-03 10:00:56 +1000405 engine->display.destroy = nv50_display_destroy;
Ben Skeggs2a44e492011-11-09 11:36:33 +1000406 engine->display.init = nv50_display_init;
407 engine->display.fini = nv50_display_fini;
Ben Skeggs4b223ee2010-08-03 10:00:56 +1000408 engine->gpio.init = nv50_gpio_init;
Ben Skeggsa0b25632011-11-21 16:41:48 +1000409 engine->gpio.fini = nv50_gpio_fini;
410 engine->gpio.drive = nv50_gpio_drive;
411 engine->gpio.sense = nv50_gpio_sense;
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;
Ben Skeggs24f246a2011-06-10 13:36:08 +1000414 engine->vram.takedown = nv50_vram_fini;
Ben Skeggs8984e042010-11-15 11:48:33 +1000415 engine->vram.get = nvc0_vram_new;
416 engine->vram.put = nv50_vram_del;
417 engine->vram.flags_valid = nvc0_vram_flags_valid;
Martin Peres74cfad12011-05-12 22:40:47 +0200418 engine->pm.temp_get = nv84_temp_get;
Ben Skeggs354d0782011-06-19 01:44:36 +1000419 engine->pm.clocks_get = nvc0_pm_clocks_get;
Ben Skeggs045da4e2011-10-29 00:22:49 +1000420 engine->pm.clocks_pre = nvc0_pm_clocks_pre;
421 engine->pm.clocks_set = nvc0_pm_clocks_set;
Ben Skeggs3c71c232011-06-09 17:34:02 +1000422 engine->pm.voltage_get = nouveau_voltage_gpio_get;
Ben Skeggsda1dc4c2011-06-10 12:07:09 +1000423 engine->pm.voltage_set = nouveau_voltage_gpio_set;
Ben Skeggs5a4267a2011-09-17 02:01:24 +1000424 engine->pm.pwm_get = nv50_pm_pwm_get;
425 engine->pm.pwm_set = nv50_pm_pwm_set;
Ben Skeggs4b223ee2010-08-03 10:00:56 +1000426 break;
Ben Skeggsd9f61c22011-07-04 13:25:17 +1000427 case 0xd0:
428 engine->instmem.init = nvc0_instmem_init;
429 engine->instmem.takedown = nvc0_instmem_takedown;
430 engine->instmem.suspend = nvc0_instmem_suspend;
431 engine->instmem.resume = nvc0_instmem_resume;
432 engine->instmem.get = nv50_instmem_get;
433 engine->instmem.put = nv50_instmem_put;
434 engine->instmem.map = nv50_instmem_map;
435 engine->instmem.unmap = nv50_instmem_unmap;
436 engine->instmem.flush = nv84_instmem_flush;
437 engine->mc.init = nv50_mc_init;
438 engine->mc.takedown = nv50_mc_takedown;
439 engine->timer.init = nv04_timer_init;
440 engine->timer.read = nv04_timer_read;
441 engine->timer.takedown = nv04_timer_takedown;
442 engine->fb.init = nvc0_fb_init;
443 engine->fb.takedown = nvc0_fb_takedown;
444 engine->fifo.channels = 128;
445 engine->fifo.init = nvc0_fifo_init;
446 engine->fifo.takedown = nvc0_fifo_takedown;
447 engine->fifo.disable = nvc0_fifo_disable;
448 engine->fifo.enable = nvc0_fifo_enable;
449 engine->fifo.reassign = nvc0_fifo_reassign;
450 engine->fifo.channel_id = nvc0_fifo_channel_id;
451 engine->fifo.create_context = nvc0_fifo_create_context;
452 engine->fifo.destroy_context = nvc0_fifo_destroy_context;
453 engine->fifo.load_context = nvc0_fifo_load_context;
454 engine->fifo.unload_context = nvc0_fifo_unload_context;
455 engine->display.early_init = nouveau_stub_init;
456 engine->display.late_takedown = nouveau_stub_takedown;
Ben Skeggs26f6d882011-07-04 16:25:18 +1000457 engine->display.create = nvd0_display_create;
Ben Skeggs26f6d882011-07-04 16:25:18 +1000458 engine->display.destroy = nvd0_display_destroy;
Ben Skeggs2a44e492011-11-09 11:36:33 +1000459 engine->display.init = nvd0_display_init;
460 engine->display.fini = nvd0_display_fini;
Ben Skeggsd7f81722011-07-03 02:57:35 +1000461 engine->gpio.init = nv50_gpio_init;
Ben Skeggsa0b25632011-11-21 16:41:48 +1000462 engine->gpio.fini = nv50_gpio_fini;
463 engine->gpio.drive = nvd0_gpio_drive;
464 engine->gpio.sense = nvd0_gpio_sense;
Ben Skeggsd7f81722011-07-03 02:57:35 +1000465 engine->gpio.irq_enable = nv50_gpio_irq_enable;
Ben Skeggsd9f61c22011-07-04 13:25:17 +1000466 engine->vram.init = nvc0_vram_init;
467 engine->vram.takedown = nv50_vram_fini;
468 engine->vram.get = nvc0_vram_new;
469 engine->vram.put = nv50_vram_del;
470 engine->vram.flags_valid = nvc0_vram_flags_valid;
Martin Peres61091832011-10-22 01:40:40 +0200471 engine->pm.temp_get = nv84_temp_get;
Ben Skeggs4784e4a2011-07-04 14:06:07 +1000472 engine->pm.clocks_get = nvc0_pm_clocks_get;
Ben Skeggs045da4e2011-10-29 00:22:49 +1000473 engine->pm.clocks_pre = nvc0_pm_clocks_pre;
474 engine->pm.clocks_set = nvc0_pm_clocks_set;
Ben Skeggs4784e4a2011-07-04 14:06:07 +1000475 engine->pm.voltage_get = nouveau_voltage_gpio_get;
476 engine->pm.voltage_set = nouveau_voltage_gpio_set;
Ben Skeggsd9f61c22011-07-04 13:25:17 +1000477 break;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000478 default:
479 NV_ERROR(dev, "NV%02x unsupported\n", dev_priv->chipset);
480 return 1;
481 }
482
Ben Skeggs03bc9672011-07-04 13:14:05 +1000483 /* headless mode */
484 if (nouveau_modeset == 2) {
485 engine->display.early_init = nouveau_stub_init;
486 engine->display.late_takedown = nouveau_stub_takedown;
487 engine->display.create = nouveau_stub_init;
488 engine->display.init = nouveau_stub_init;
489 engine->display.destroy = nouveau_stub_takedown;
490 }
491
Ben Skeggs6ee73862009-12-11 19:24:15 +1000492 return 0;
493}
494
495static unsigned int
496nouveau_vga_set_decode(void *priv, bool state)
497{
Marcin Koƛcielnicki9967b942010-02-08 00:20:17 +0000498 struct drm_device *dev = priv;
499 struct drm_nouveau_private *dev_priv = dev->dev_private;
500
501 if (dev_priv->chipset >= 0x40)
502 nv_wr32(dev, 0x88054, state);
503 else
504 nv_wr32(dev, 0x1854, state);
505
Ben Skeggs6ee73862009-12-11 19:24:15 +1000506 if (state)
507 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
508 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
509 else
510 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
511}
512
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000513static void nouveau_switcheroo_set_state(struct pci_dev *pdev,
514 enum vga_switcheroo_state state)
515{
Dave Airliefbf81762010-06-01 09:09:06 +1000516 struct drm_device *dev = pci_get_drvdata(pdev);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000517 pm_message_t pmm = { .event = PM_EVENT_SUSPEND };
518 if (state == VGA_SWITCHEROO_ON) {
519 printk(KERN_ERR "VGA switcheroo: switched nouveau on\n");
Dave Airlie5bcf7192010-12-07 09:20:40 +1000520 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000521 nouveau_pci_resume(pdev);
Dave Airliefbf81762010-06-01 09:09:06 +1000522 drm_kms_helper_poll_enable(dev);
Dave Airlie5bcf7192010-12-07 09:20:40 +1000523 dev->switch_power_state = DRM_SWITCH_POWER_ON;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000524 } else {
525 printk(KERN_ERR "VGA switcheroo: switched nouveau off\n");
Dave Airlie5bcf7192010-12-07 09:20:40 +1000526 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
Dave Airliefbf81762010-06-01 09:09:06 +1000527 drm_kms_helper_poll_disable(dev);
Peter Lekensteynd0992302011-12-17 12:54:04 +0100528 nouveau_switcheroo_optimus_dsm();
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000529 nouveau_pci_suspend(pdev, pmm);
Dave Airlie5bcf7192010-12-07 09:20:40 +1000530 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000531 }
532}
533
Dave Airlie8d608aa2010-12-07 08:57:57 +1000534static void nouveau_switcheroo_reprobe(struct pci_dev *pdev)
535{
536 struct drm_device *dev = pci_get_drvdata(pdev);
537 nouveau_fbcon_output_poll_changed(dev);
538}
539
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000540static bool nouveau_switcheroo_can_switch(struct pci_dev *pdev)
541{
542 struct drm_device *dev = pci_get_drvdata(pdev);
543 bool can_switch;
544
545 spin_lock(&dev->count_lock);
546 can_switch = (dev->open_count == 0);
547 spin_unlock(&dev->count_lock);
548 return can_switch;
549}
550
Ben Skeggs6ee73862009-12-11 19:24:15 +1000551int
552nouveau_card_init(struct drm_device *dev)
553{
554 struct drm_nouveau_private *dev_priv = dev->dev_private;
555 struct nouveau_engine *engine;
Ben Skeggseea55c82011-04-18 08:57:51 +1000556 int ret, e = 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000557
Ben Skeggs6ee73862009-12-11 19:24:15 +1000558 vga_client_register(dev->pdev, dev, NULL, nouveau_vga_set_decode);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000559 vga_switcheroo_register_client(dev->pdev, nouveau_switcheroo_set_state,
Dave Airlie8d608aa2010-12-07 08:57:57 +1000560 nouveau_switcheroo_reprobe,
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000561 nouveau_switcheroo_can_switch);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000562
563 /* Initialise internal driver API hooks */
564 ret = nouveau_init_engine_ptrs(dev);
565 if (ret)
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000566 goto out;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000567 engine = &dev_priv->engine;
Ben Skeggscff5c132010-10-06 16:16:59 +1000568 spin_lock_init(&dev_priv->channels.lock);
Francisco Jereza5cf68b2010-10-24 16:14:41 +0200569 spin_lock_init(&dev_priv->tile.lock);
Maarten Maathuisff9e5272010-02-01 20:58:27 +0100570 spin_lock_init(&dev_priv->context_switch_lock);
Ben Skeggs04eb34a2011-04-06 13:28:35 +1000571 spin_lock_init(&dev_priv->vm_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 Skeggs4c5df492011-10-28 10:59:45 +1000583 /* workaround an odd issue on nvc1 by disabling the device's
584 * nosnoop capability. hopefully won't cause issues until a
585 * better fix is found - assuming there is one...
586 */
587 if (dev_priv->chipset == 0xc1) {
588 nv_mask(dev, 0x00088080, 0x00000800, 0x00000000);
589 }
590
Ben Skeggs330c5982010-09-16 15:39:49 +1000591 nouveau_pm_init(dev);
592
Ben Skeggs24f246a2011-06-10 13:36:08 +1000593 ret = engine->vram.init(dev);
Ben Skeggsa76fb4e2010-03-18 09:45:20 +1000594 if (ret)
595 goto out_bios;
596
Ben Skeggs6ee73862009-12-11 19:24:15 +1000597 ret = nouveau_gpuobj_init(dev);
598 if (ret)
Ben Skeggsfbd28952010-09-01 15:24:34 +1000599 goto out_vram;
600
601 ret = engine->instmem.init(dev);
602 if (ret)
603 goto out_gpuobj;
604
Ben Skeggs24f246a2011-06-10 13:36:08 +1000605 ret = nouveau_mem_vram_init(dev);
Ben Skeggsfbd28952010-09-01 15:24:34 +1000606 if (ret)
607 goto out_instmem;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000608
Ben Skeggs24f246a2011-06-10 13:36:08 +1000609 ret = nouveau_mem_gart_init(dev);
610 if (ret)
611 goto out_ttmvram;
612
Ben Skeggs6ee73862009-12-11 19:24:15 +1000613 /* PMC */
614 ret = engine->mc.init(dev);
615 if (ret)
Ben Skeggsfbd28952010-09-01 15:24:34 +1000616 goto out_gart;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000617
Ben Skeggsee2e0132010-07-26 09:28:25 +1000618 /* PGPIO */
Ben Skeggsa0b25632011-11-21 16:41:48 +1000619 ret = nouveau_gpio_create(dev);
Ben Skeggsee2e0132010-07-26 09:28:25 +1000620 if (ret)
621 goto out_mc;
622
Ben Skeggs6ee73862009-12-11 19:24:15 +1000623 /* PTIMER */
624 ret = engine->timer.init(dev);
625 if (ret)
Ben Skeggsee2e0132010-07-26 09:28:25 +1000626 goto out_gpio;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000627
628 /* PFB */
629 ret = engine->fb.init(dev);
630 if (ret)
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000631 goto out_timer;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000632
Ben Skeggsaba99a82011-05-25 14:48:50 +1000633 if (!dev_priv->noaccel) {
Ben Skeggs18b54c42011-05-25 15:22:33 +1000634 switch (dev_priv->card_type) {
635 case NV_04:
636 nv04_graph_create(dev);
637 break;
638 case NV_10:
639 nv10_graph_create(dev);
640 break;
641 case NV_20:
642 case NV_30:
643 nv20_graph_create(dev);
644 break;
645 case NV_40:
646 nv40_graph_create(dev);
647 break;
648 case NV_50:
649 nv50_graph_create(dev);
650 break;
651 case NV_C0:
Ben Skeggs06784092011-07-11 15:57:54 +1000652 case NV_D0:
Ben Skeggs18b54c42011-05-25 15:22:33 +1000653 nvc0_graph_create(dev);
654 break;
655 default:
Ben Skeggs7ff54412011-03-18 10:25:59 +1000656 break;
657 }
Ben Skeggs7ff54412011-03-18 10:25:59 +1000658
Ben Skeggs18b54c42011-05-25 15:22:33 +1000659 switch (dev_priv->chipset) {
660 case 0x84:
661 case 0x86:
662 case 0x92:
663 case 0x94:
664 case 0x96:
665 case 0xa0:
666 nv84_crypt_create(dev);
667 break;
Ben Skeggs8f27c542011-08-11 14:58:06 +1000668 case 0x98:
669 case 0xaa:
670 case 0xac:
671 nv98_crypt_create(dev);
672 break;
Ben Skeggs18b54c42011-05-25 15:22:33 +1000673 }
Ben Skeggsa02ccc72011-04-04 16:08:24 +1000674
Ben Skeggs18b54c42011-05-25 15:22:33 +1000675 switch (dev_priv->card_type) {
676 case NV_50:
677 switch (dev_priv->chipset) {
678 case 0xa3:
679 case 0xa5:
680 case 0xa8:
681 case 0xaf:
682 nva3_copy_create(dev);
683 break;
684 }
685 break;
686 case NV_C0:
687 nvc0_copy_create(dev, 0);
688 nvc0_copy_create(dev, 1);
689 break;
690 default:
691 break;
692 }
693
Ben Skeggs8f27c542011-08-11 14:58:06 +1000694 if (dev_priv->chipset >= 0xa3 || dev_priv->chipset == 0x98) {
695 nv84_bsp_create(dev);
696 nv84_vp_create(dev);
697 nv98_ppp_create(dev);
698 } else
699 if (dev_priv->chipset >= 0x84) {
700 nv50_mpeg_create(dev);
701 nv84_bsp_create(dev);
702 nv84_vp_create(dev);
703 } else
704 if (dev_priv->chipset >= 0x50) {
705 nv50_mpeg_create(dev);
706 } else
Ben Skeggs52d07332011-06-23 16:44:05 +1000707 if (dev_priv->card_type == NV_40 ||
708 dev_priv->chipset == 0x31 ||
709 dev_priv->chipset == 0x34 ||
Ben Skeggs8f27c542011-08-11 14:58:06 +1000710 dev_priv->chipset == 0x36) {
Ben Skeggs323dcac2011-06-23 16:21:21 +1000711 nv31_mpeg_create(dev);
Ben Skeggs8f27c542011-08-11 14:58:06 +1000712 }
Ben Skeggs18b54c42011-05-25 15:22:33 +1000713
Ben Skeggs6dfdd7a2011-03-31 15:40:43 +1000714 for (e = 0; e < NVOBJ_ENGINE_NR; e++) {
715 if (dev_priv->eng[e]) {
716 ret = dev_priv->eng[e]->init(dev, e);
717 if (ret)
718 goto out_engine;
719 }
720 }
721
Marcin Koƛcielnickia32ed692010-01-26 14:00:42 +0000722 /* PFIFO */
723 ret = engine->fifo.init(dev);
724 if (ret)
Ben Skeggsa82dd492011-04-01 13:56:05 +1000725 goto out_engine;
Marcin Koƛcielnickia32ed692010-01-26 14:00:42 +0000726 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000727
Ben Skeggs1575b362011-07-04 11:55:39 +1000728 ret = nouveau_irq_init(dev);
729 if (ret)
730 goto out_fifo;
731
Ben Skeggs27d50302011-10-06 12:46:40 +1000732 ret = nouveau_display_create(dev);
Ben Skeggse88efe02010-07-09 10:56:08 +1000733 if (ret)
Ben Skeggs1575b362011-07-04 11:55:39 +1000734 goto out_irq;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000735
Ben Skeggs10b461e2011-08-02 19:29:37 +1000736 nouveau_backlight_init(dev);
737
Ben Skeggsa82dd492011-04-01 13:56:05 +1000738 if (dev_priv->eng[NVOBJ_ENGINE_GR]) {
Francisco Jerez0c6c1c22010-09-22 00:58:54 +0200739 ret = nouveau_fence_init(dev);
Ben Skeggs0735f622009-12-16 14:28:55 +1000740 if (ret)
Ben Skeggs1575b362011-07-04 11:55:39 +1000741 goto out_disp;
Francisco Jerez0c6c1c22010-09-22 00:58:54 +0200742
Ben Skeggs1575b362011-07-04 11:55:39 +1000743 ret = nouveau_channel_alloc(dev, &dev_priv->channel, NULL,
744 NvDmaFB, NvDmaTT);
Francisco Jerez0c6c1c22010-09-22 00:58:54 +0200745 if (ret)
746 goto out_fence;
Ben Skeggs1575b362011-07-04 11:55:39 +1000747
748 mutex_unlock(&dev_priv->channel->mutex);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000749 }
750
Ben Skeggs1575b362011-07-04 11:55:39 +1000751 if (dev->mode_config.num_crtc) {
Ben Skeggsf62b27d2011-11-09 15:18:47 +1000752 ret = nouveau_display_init(dev);
Ben Skeggs1575b362011-07-04 11:55:39 +1000753 if (ret)
754 goto out_chan;
755
756 nouveau_fbcon_init(dev);
Ben Skeggs1575b362011-07-04 11:55:39 +1000757 }
758
Ben Skeggs6ee73862009-12-11 19:24:15 +1000759 return 0;
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000760
Ben Skeggs1575b362011-07-04 11:55:39 +1000761out_chan:
762 nouveau_channel_put_unlocked(&dev_priv->channel);
Francisco Jerez0c6c1c22010-09-22 00:58:54 +0200763out_fence:
764 nouveau_fence_fini(dev);
Ben Skeggs1575b362011-07-04 11:55:39 +1000765out_disp:
Ben Skeggs10b461e2011-08-02 19:29:37 +1000766 nouveau_backlight_exit(dev);
Ben Skeggs27d50302011-10-06 12:46:40 +1000767 nouveau_display_destroy(dev);
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000768out_irq:
Ben Skeggs35fa2f22010-10-21 14:07:03 +1000769 nouveau_irq_fini(dev);
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000770out_fifo:
Ben Skeggsaba99a82011-05-25 14:48:50 +1000771 if (!dev_priv->noaccel)
Marcin Koƛcielnickia32ed692010-01-26 14:00:42 +0000772 engine->fifo.takedown(dev);
Ben Skeggs6dfdd7a2011-03-31 15:40:43 +1000773out_engine:
Ben Skeggsaba99a82011-05-25 14:48:50 +1000774 if (!dev_priv->noaccel) {
Ben Skeggs6dfdd7a2011-03-31 15:40:43 +1000775 for (e = e - 1; e >= 0; e--) {
Ben Skeggs2703c212011-04-01 09:50:18 +1000776 if (!dev_priv->eng[e])
777 continue;
Ben Skeggs6c320fe2011-07-20 11:22:33 +1000778 dev_priv->eng[e]->fini(dev, e, false);
Ben Skeggs2703c212011-04-01 09:50:18 +1000779 dev_priv->eng[e]->destroy(dev,e );
Ben Skeggs6dfdd7a2011-03-31 15:40:43 +1000780 }
781 }
782
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000783 engine->fb.takedown(dev);
784out_timer:
785 engine->timer.takedown(dev);
Ben Skeggsee2e0132010-07-26 09:28:25 +1000786out_gpio:
Ben Skeggsa0b25632011-11-21 16:41:48 +1000787 nouveau_gpio_destroy(dev);
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000788out_mc:
789 engine->mc.takedown(dev);
Ben Skeggsfbd28952010-09-01 15:24:34 +1000790out_gart:
791 nouveau_mem_gart_fini(dev);
Ben Skeggs24f246a2011-06-10 13:36:08 +1000792out_ttmvram:
793 nouveau_mem_vram_fini(dev);
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000794out_instmem:
795 engine->instmem.takedown(dev);
Ben Skeggsfbd28952010-09-01 15:24:34 +1000796out_gpuobj:
797 nouveau_gpuobj_takedown(dev);
798out_vram:
Ben Skeggs24f246a2011-06-10 13:36:08 +1000799 engine->vram.takedown(dev);
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000800out_bios:
Ben Skeggs330c5982010-09-16 15:39:49 +1000801 nouveau_pm_fini(dev);
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000802 nouveau_bios_takedown(dev);
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200803out_display_early:
804 engine->display.late_takedown(dev);
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000805out:
806 vga_client_register(dev->pdev, NULL, NULL, NULL);
807 return ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000808}
809
810static void nouveau_card_takedown(struct drm_device *dev)
811{
812 struct drm_nouveau_private *dev_priv = dev->dev_private;
813 struct nouveau_engine *engine = &dev_priv->engine;
Ben Skeggs6dfdd7a2011-03-31 15:40:43 +1000814 int e;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000815
Ben Skeggs1575b362011-07-04 11:55:39 +1000816 if (dev->mode_config.num_crtc) {
Ben Skeggs1575b362011-07-04 11:55:39 +1000817 nouveau_fbcon_fini(dev);
Ben Skeggsf62b27d2011-11-09 15:18:47 +1000818 nouveau_display_fini(dev);
Ben Skeggs1575b362011-07-04 11:55:39 +1000819 }
Ben Skeggs06b75e32011-06-08 18:29:12 +1000820
Ben Skeggsa82dd492011-04-01 13:56:05 +1000821 if (dev_priv->channel) {
Francisco Jerez36c952e2010-10-18 03:01:34 +0200822 nouveau_channel_put_unlocked(&dev_priv->channel);
Ben Skeggs06b75e32011-06-08 18:29:12 +1000823 nouveau_fence_fini(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000824 }
Ben Skeggsb6d3d872010-06-07 15:38:27 +1000825
Ben Skeggs10b461e2011-08-02 19:29:37 +1000826 nouveau_backlight_exit(dev);
Ben Skeggs27d50302011-10-06 12:46:40 +1000827 nouveau_display_destroy(dev);
Ben Skeggs06b75e32011-06-08 18:29:12 +1000828
Ben Skeggsaba99a82011-05-25 14:48:50 +1000829 if (!dev_priv->noaccel) {
Ben Skeggsb6d3d872010-06-07 15:38:27 +1000830 engine->fifo.takedown(dev);
Ben Skeggs6dfdd7a2011-03-31 15:40:43 +1000831 for (e = NVOBJ_ENGINE_NR - 1; e >= 0; e--) {
832 if (dev_priv->eng[e]) {
Ben Skeggs6c320fe2011-07-20 11:22:33 +1000833 dev_priv->eng[e]->fini(dev, e, false);
Ben Skeggs6dfdd7a2011-03-31 15:40:43 +1000834 dev_priv->eng[e]->destroy(dev,e );
835 }
836 }
Ben Skeggsb6d3d872010-06-07 15:38:27 +1000837 }
838 engine->fb.takedown(dev);
839 engine->timer.takedown(dev);
Ben Skeggsa0b25632011-11-21 16:41:48 +1000840 nouveau_gpio_destroy(dev);
Ben Skeggsb6d3d872010-06-07 15:38:27 +1000841 engine->mc.takedown(dev);
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200842 engine->display.late_takedown(dev);
Ben Skeggsb6d3d872010-06-07 15:38:27 +1000843
Jimmy Rentz97666102011-04-17 16:15:09 -0400844 if (dev_priv->vga_ram) {
845 nouveau_bo_unpin(dev_priv->vga_ram);
846 nouveau_bo_ref(NULL, &dev_priv->vga_ram);
847 }
848
Ben Skeggsb6d3d872010-06-07 15:38:27 +1000849 mutex_lock(&dev->struct_mutex);
850 ttm_bo_clean_mm(&dev_priv->ttm.bdev, TTM_PL_VRAM);
851 ttm_bo_clean_mm(&dev_priv->ttm.bdev, TTM_PL_TT);
852 mutex_unlock(&dev->struct_mutex);
Ben Skeggsfbd28952010-09-01 15:24:34 +1000853 nouveau_mem_gart_fini(dev);
Ben Skeggs24f246a2011-06-10 13:36:08 +1000854 nouveau_mem_vram_fini(dev);
Ben Skeggsb6d3d872010-06-07 15:38:27 +1000855
Ben Skeggsb6d3d872010-06-07 15:38:27 +1000856 engine->instmem.takedown(dev);
Ben Skeggsfbd28952010-09-01 15:24:34 +1000857 nouveau_gpuobj_takedown(dev);
Ben Skeggs24f246a2011-06-10 13:36:08 +1000858 engine->vram.takedown(dev);
Ben Skeggsb6d3d872010-06-07 15:38:27 +1000859
Ben Skeggs35fa2f22010-10-21 14:07:03 +1000860 nouveau_irq_fini(dev);
Ben Skeggsb6d3d872010-06-07 15:38:27 +1000861
Ben Skeggs330c5982010-09-16 15:39:49 +1000862 nouveau_pm_fini(dev);
Ben Skeggsb6d3d872010-06-07 15:38:27 +1000863 nouveau_bios_takedown(dev);
864
865 vga_client_register(dev->pdev, NULL, NULL, NULL);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000866}
867
Ben Skeggs3f0a68d2011-05-31 11:11:28 +1000868int
869nouveau_open(struct drm_device *dev, struct drm_file *file_priv)
870{
Ben Skeggsfe32b162011-06-03 10:07:08 +1000871 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs3f0a68d2011-05-31 11:11:28 +1000872 struct nouveau_fpriv *fpriv;
Ben Skeggse41f26e2011-06-07 15:35:37 +1000873 int ret;
Ben Skeggs3f0a68d2011-05-31 11:11:28 +1000874
875 fpriv = kzalloc(sizeof(*fpriv), GFP_KERNEL);
876 if (unlikely(!fpriv))
877 return -ENOMEM;
878
879 spin_lock_init(&fpriv->lock);
Ben Skeggse8a863c2011-06-01 19:18:48 +1000880 INIT_LIST_HEAD(&fpriv->channels);
881
Ben Skeggse41f26e2011-06-07 15:35:37 +1000882 if (dev_priv->card_type == NV_50) {
883 ret = nouveau_vm_new(dev, 0, (1ULL << 40), 0x0020000000ULL,
884 &fpriv->vm);
885 if (ret) {
886 kfree(fpriv);
887 return ret;
888 }
889 } else
890 if (dev_priv->card_type >= NV_C0) {
Ben Skeggs5de80372011-06-08 18:17:41 +1000891 ret = nouveau_vm_new(dev, 0, (1ULL << 40), 0x0008000000ULL,
892 &fpriv->vm);
893 if (ret) {
894 kfree(fpriv);
895 return ret;
896 }
Ben Skeggse41f26e2011-06-07 15:35:37 +1000897 }
Ben Skeggsfe32b162011-06-03 10:07:08 +1000898
Ben Skeggs3f0a68d2011-05-31 11:11:28 +1000899 file_priv->driver_priv = fpriv;
900 return 0;
901}
902
Ben Skeggs6ee73862009-12-11 19:24:15 +1000903/* here a client dies, release the stuff that was allocated for its
904 * file_priv */
905void nouveau_preclose(struct drm_device *dev, struct drm_file *file_priv)
906{
907 nouveau_channel_cleanup(dev, file_priv);
908}
909
Ben Skeggs3f0a68d2011-05-31 11:11:28 +1000910void
911nouveau_postclose(struct drm_device *dev, struct drm_file *file_priv)
912{
913 struct nouveau_fpriv *fpriv = nouveau_fpriv(file_priv);
Ben Skeggsfe32b162011-06-03 10:07:08 +1000914 nouveau_vm_ref(NULL, &fpriv->vm, NULL);
Ben Skeggs3f0a68d2011-05-31 11:11:28 +1000915 kfree(fpriv);
916}
917
Ben Skeggs6ee73862009-12-11 19:24:15 +1000918/* first module load, setup the mmio/fb mapping */
919/* KMS: we need mmio at load time, not when the first drm client opens. */
920int nouveau_firstopen(struct drm_device *dev)
921{
922 return 0;
923}
924
925/* if we have an OF card, copy vbios to RAMIN */
926static void nouveau_OF_copy_vbios_to_ramin(struct drm_device *dev)
927{
928#if defined(__powerpc__)
929 int size, i;
930 const uint32_t *bios;
931 struct device_node *dn = pci_device_to_OF_node(dev->pdev);
932 if (!dn) {
933 NV_INFO(dev, "Unable to get the OF node\n");
934 return;
935 }
936
937 bios = of_get_property(dn, "NVDA,BMP", &size);
938 if (bios) {
939 for (i = 0; i < size; i += 4)
940 nv_wi32(dev, i, bios[i/4]);
941 NV_INFO(dev, "OF bios successfully copied (%d bytes)\n", size);
942 } else {
943 NV_INFO(dev, "Unable to get the OF bios\n");
944 }
945#endif
946}
947
Marcin Slusarz06415c52010-05-16 17:29:56 +0200948static struct apertures_struct *nouveau_get_apertures(struct drm_device *dev)
949{
950 struct pci_dev *pdev = dev->pdev;
951 struct apertures_struct *aper = alloc_apertures(3);
952 if (!aper)
953 return NULL;
954
955 aper->ranges[0].base = pci_resource_start(pdev, 1);
956 aper->ranges[0].size = pci_resource_len(pdev, 1);
957 aper->count = 1;
958
959 if (pci_resource_len(pdev, 2)) {
960 aper->ranges[aper->count].base = pci_resource_start(pdev, 2);
961 aper->ranges[aper->count].size = pci_resource_len(pdev, 2);
962 aper->count++;
963 }
964
965 if (pci_resource_len(pdev, 3)) {
966 aper->ranges[aper->count].base = pci_resource_start(pdev, 3);
967 aper->ranges[aper->count].size = pci_resource_len(pdev, 3);
968 aper->count++;
969 }
970
971 return aper;
972}
973
974static int nouveau_remove_conflicting_drivers(struct drm_device *dev)
975{
976 struct drm_nouveau_private *dev_priv = dev->dev_private;
Marcin Slusarz3b9676e2010-05-16 17:33:09 +0200977 bool primary = false;
Marcin Slusarz06415c52010-05-16 17:29:56 +0200978 dev_priv->apertures = nouveau_get_apertures(dev);
979 if (!dev_priv->apertures)
980 return -ENOMEM;
981
Marcin Slusarz3b9676e2010-05-16 17:33:09 +0200982#ifdef CONFIG_X86
983 primary = dev->pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW;
984#endif
Emil Velikovf2129492011-03-19 23:31:52 +0000985
Marcin Slusarz3b9676e2010-05-16 17:33:09 +0200986 remove_conflicting_framebuffers(dev_priv->apertures, "nouveaufb", primary);
Marcin Slusarz06415c52010-05-16 17:29:56 +0200987 return 0;
988}
989
Ben Skeggs6ee73862009-12-11 19:24:15 +1000990int nouveau_load(struct drm_device *dev, unsigned long flags)
991{
992 struct drm_nouveau_private *dev_priv;
Ben Skeggsf2cbe462011-07-21 15:39:06 +1000993 uint32_t reg0, strap;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000994 resource_size_t mmio_start_offs;
Ben Skeggscd0b0722010-06-01 15:56:22 +1000995 int ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000996
997 dev_priv = kzalloc(sizeof(*dev_priv), GFP_KERNEL);
Dan Carpentera0d069e2010-07-30 17:04:32 +0200998 if (!dev_priv) {
999 ret = -ENOMEM;
1000 goto err_out;
1001 }
Ben Skeggs6ee73862009-12-11 19:24:15 +10001002 dev->dev_private = dev_priv;
1003 dev_priv->dev = dev;
1004
1005 dev_priv->flags = flags & NOUVEAU_FLAGS;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001006
1007 NV_DEBUG(dev, "vendor: 0x%X device: 0x%X class: 0x%X\n",
1008 dev->pci_vendor, dev->pci_device, dev->pdev->class);
1009
Ben Skeggs6ee73862009-12-11 19:24:15 +10001010 /* resource 0 is mmio regs */
1011 /* resource 1 is linear FB */
1012 /* resource 2 is RAMIN (mmio regs + 0x1000000) */
1013 /* resource 6 is bios */
1014
1015 /* map the mmio regs */
1016 mmio_start_offs = pci_resource_start(dev->pdev, 0);
1017 dev_priv->mmio = ioremap(mmio_start_offs, 0x00800000);
1018 if (!dev_priv->mmio) {
1019 NV_ERROR(dev, "Unable to initialize the mmio mapping. "
1020 "Please report your setup to " DRIVER_EMAIL "\n");
Dan Carpentera0d069e2010-07-30 17:04:32 +02001021 ret = -EINVAL;
Tejun Heod82f8e62011-01-26 17:49:18 +01001022 goto err_priv;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001023 }
1024 NV_DEBUG(dev, "regs mapped ok at 0x%llx\n",
1025 (unsigned long long)mmio_start_offs);
1026
1027#ifdef __BIG_ENDIAN
1028 /* Put the card in BE mode if it's not */
Ben Skeggs08975542011-06-14 10:16:17 +10001029 if (nv_rd32(dev, NV03_PMC_BOOT_1) != 0x01000001)
1030 nv_wr32(dev, NV03_PMC_BOOT_1, 0x01000001);
Ben Skeggs6ee73862009-12-11 19:24:15 +10001031
1032 DRM_MEMORYBARRIER();
1033#endif
1034
1035 /* Time to determine the card architecture */
1036 reg0 = nv_rd32(dev, NV03_PMC_BOOT_0);
1037
1038 /* We're dealing with >=NV10 */
1039 if ((reg0 & 0x0f000000) > 0) {
1040 /* Bit 27-20 contain the architecture in hex */
1041 dev_priv->chipset = (reg0 & 0xff00000) >> 20;
1042 /* NV04 or NV05 */
1043 } else if ((reg0 & 0xff00fff0) == 0x20004000) {
Ben Skeggs1dee7a92010-01-07 13:47:57 +10001044 if (reg0 & 0x00f00000)
1045 dev_priv->chipset = 0x05;
1046 else
1047 dev_priv->chipset = 0x04;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001048 } else
1049 dev_priv->chipset = 0xff;
1050
1051 switch (dev_priv->chipset & 0xf0) {
1052 case 0x00:
1053 case 0x10:
1054 case 0x20:
1055 case 0x30:
1056 dev_priv->card_type = dev_priv->chipset & 0xf0;
1057 break;
1058 case 0x40:
1059 case 0x60:
1060 dev_priv->card_type = NV_40;
1061 break;
1062 case 0x50:
1063 case 0x80:
1064 case 0x90:
1065 case 0xa0:
1066 dev_priv->card_type = NV_50;
1067 break;
Ben Skeggs4b223ee2010-08-03 10:00:56 +10001068 case 0xc0:
1069 dev_priv->card_type = NV_C0;
1070 break;
Ben Skeggsd9f61c22011-07-04 13:25:17 +10001071 case 0xd0:
1072 dev_priv->card_type = NV_D0;
1073 break;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001074 default:
1075 NV_INFO(dev, "Unsupported chipset 0x%08x\n", reg0);
Dan Carpentera0d069e2010-07-30 17:04:32 +02001076 ret = -EINVAL;
1077 goto err_mmio;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001078 }
1079
1080 NV_INFO(dev, "Detected an NV%2x generation card (0x%08x)\n",
1081 dev_priv->card_type, reg0);
1082
Ben Skeggsf2cbe462011-07-21 15:39:06 +10001083 /* determine frequency of timing crystal */
1084 strap = nv_rd32(dev, 0x101000);
1085 if ( dev_priv->chipset < 0x17 ||
1086 (dev_priv->chipset >= 0x20 && dev_priv->chipset <= 0x25))
1087 strap &= 0x00000040;
1088 else
1089 strap &= 0x00400040;
1090
1091 switch (strap) {
1092 case 0x00000000: dev_priv->crystal = 13500; break;
1093 case 0x00000040: dev_priv->crystal = 14318; break;
1094 case 0x00400000: dev_priv->crystal = 27000; break;
1095 case 0x00400040: dev_priv->crystal = 25000; break;
1096 }
1097
1098 NV_DEBUG(dev, "crystal freq: %dKHz\n", dev_priv->crystal);
1099
Ben Skeggsaba99a82011-05-25 14:48:50 +10001100 /* Determine whether we'll attempt acceleration or not, some
1101 * cards are disabled by default here due to them being known
1102 * non-functional, or never been tested due to lack of hw.
1103 */
1104 dev_priv->noaccel = !!nouveau_noaccel;
1105 if (nouveau_noaccel == -1) {
1106 switch (dev_priv->chipset) {
Ben Skeggs06784092011-07-11 15:57:54 +10001107 case 0xd9: /* known broken */
Ben Skeggsad830d22011-05-27 16:18:10 +10001108 NV_INFO(dev, "acceleration disabled by default, pass "
1109 "noaccel=0 to force enable\n");
Ben Skeggsaba99a82011-05-25 14:48:50 +10001110 dev_priv->noaccel = true;
1111 break;
1112 default:
1113 dev_priv->noaccel = false;
1114 break;
1115 }
1116 }
1117
Ben Skeggscd0b0722010-06-01 15:56:22 +10001118 ret = nouveau_remove_conflicting_drivers(dev);
1119 if (ret)
Dan Carpentera0d069e2010-07-30 17:04:32 +02001120 goto err_mmio;
Marcin Slusarz06415c52010-05-16 17:29:56 +02001121
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001122 /* Map PRAMIN BAR, or on older cards, the aperture within BAR0 */
Ben Skeggs6ee73862009-12-11 19:24:15 +10001123 if (dev_priv->card_type >= NV_40) {
1124 int ramin_bar = 2;
1125 if (pci_resource_len(dev->pdev, ramin_bar) == 0)
1126 ramin_bar = 3;
1127
1128 dev_priv->ramin_size = pci_resource_len(dev->pdev, ramin_bar);
Ben Skeggs6d696302010-06-02 10:16:24 +10001129 dev_priv->ramin =
1130 ioremap(pci_resource_start(dev->pdev, ramin_bar),
Ben Skeggs6ee73862009-12-11 19:24:15 +10001131 dev_priv->ramin_size);
1132 if (!dev_priv->ramin) {
Marcin Slusarzff920bf2011-08-22 23:28:56 +02001133 NV_ERROR(dev, "Failed to map PRAMIN BAR\n");
Dan Carpentera0d069e2010-07-30 17:04:32 +02001134 ret = -ENOMEM;
1135 goto err_mmio;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001136 }
Ben Skeggs6d696302010-06-02 10:16:24 +10001137 } else {
Ben Skeggs6ee73862009-12-11 19:24:15 +10001138 dev_priv->ramin_size = 1 * 1024 * 1024;
1139 dev_priv->ramin = ioremap(mmio_start_offs + NV_RAMIN,
Ben Skeggs6d696302010-06-02 10:16:24 +10001140 dev_priv->ramin_size);
Ben Skeggs6ee73862009-12-11 19:24:15 +10001141 if (!dev_priv->ramin) {
1142 NV_ERROR(dev, "Failed to map BAR0 PRAMIN.\n");
Dan Carpentera0d069e2010-07-30 17:04:32 +02001143 ret = -ENOMEM;
1144 goto err_mmio;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001145 }
1146 }
1147
1148 nouveau_OF_copy_vbios_to_ramin(dev);
1149
1150 /* Special flags */
1151 if (dev->pci_device == 0x01a0)
1152 dev_priv->flags |= NV_NFORCE;
1153 else if (dev->pci_device == 0x01f0)
1154 dev_priv->flags |= NV_NFORCE2;
1155
1156 /* For kernel modesetting, init card now and bring up fbcon */
Ben Skeggscd0b0722010-06-01 15:56:22 +10001157 ret = nouveau_card_init(dev);
1158 if (ret)
Dan Carpentera0d069e2010-07-30 17:04:32 +02001159 goto err_ramin;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001160
1161 return 0;
Dan Carpentera0d069e2010-07-30 17:04:32 +02001162
1163err_ramin:
1164 iounmap(dev_priv->ramin);
1165err_mmio:
1166 iounmap(dev_priv->mmio);
Dan Carpentera0d069e2010-07-30 17:04:32 +02001167err_priv:
1168 kfree(dev_priv);
1169 dev->dev_private = NULL;
1170err_out:
1171 return ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001172}
1173
Ben Skeggs6ee73862009-12-11 19:24:15 +10001174void nouveau_lastclose(struct drm_device *dev)
1175{
Dave Airlie5ccb3772010-12-07 13:56:26 +10001176 vga_switcheroo_process_delayed_switch();
Ben Skeggs6ee73862009-12-11 19:24:15 +10001177}
1178
1179int nouveau_unload(struct drm_device *dev)
1180{
1181 struct drm_nouveau_private *dev_priv = dev->dev_private;
1182
Ben Skeggscd0b0722010-06-01 15:56:22 +10001183 nouveau_card_takedown(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +10001184
1185 iounmap(dev_priv->mmio);
1186 iounmap(dev_priv->ramin);
1187
1188 kfree(dev_priv);
1189 dev->dev_private = NULL;
1190 return 0;
1191}
1192
Ben Skeggs6ee73862009-12-11 19:24:15 +10001193int nouveau_ioctl_getparam(struct drm_device *dev, void *data,
1194 struct drm_file *file_priv)
1195{
1196 struct drm_nouveau_private *dev_priv = dev->dev_private;
1197 struct drm_nouveau_getparam *getparam = data;
1198
Ben Skeggs6ee73862009-12-11 19:24:15 +10001199 switch (getparam->param) {
1200 case NOUVEAU_GETPARAM_CHIPSET_ID:
1201 getparam->value = dev_priv->chipset;
1202 break;
1203 case NOUVEAU_GETPARAM_PCI_VENDOR:
1204 getparam->value = dev->pci_vendor;
1205 break;
1206 case NOUVEAU_GETPARAM_PCI_DEVICE:
1207 getparam->value = dev->pci_device;
1208 break;
1209 case NOUVEAU_GETPARAM_BUS_TYPE:
Dave Airlie8410ea32010-12-15 03:16:38 +10001210 if (drm_pci_device_is_agp(dev))
Ben Skeggs6ee73862009-12-11 19:24:15 +10001211 getparam->value = NV_AGP;
Jon Mason58b65422011-06-27 16:07:50 +00001212 else if (pci_is_pcie(dev->pdev))
Ben Skeggs6ee73862009-12-11 19:24:15 +10001213 getparam->value = NV_PCIE;
1214 else
1215 getparam->value = NV_PCI;
1216 break;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001217 case NOUVEAU_GETPARAM_FB_SIZE:
1218 getparam->value = dev_priv->fb_available_size;
1219 break;
1220 case NOUVEAU_GETPARAM_AGP_SIZE:
1221 getparam->value = dev_priv->gart_info.aper_size;
1222 break;
1223 case NOUVEAU_GETPARAM_VM_VRAM_BASE:
Ben Skeggs6d6c5a12010-11-16 10:17:53 +10001224 getparam->value = 0; /* deprecated */
Ben Skeggs6ee73862009-12-11 19:24:15 +10001225 break;
Marcin Koƛcielnicki7fc74f12010-05-23 11:36:04 +00001226 case NOUVEAU_GETPARAM_PTIMER_TIME:
1227 getparam->value = dev_priv->engine.timer.read(dev);
1228 break;
Francisco Jerezf13b3262010-10-10 06:01:08 +02001229 case NOUVEAU_GETPARAM_HAS_BO_USAGE:
1230 getparam->value = 1;
1231 break;
Francisco Jerez332b2422010-10-20 23:35:40 +02001232 case NOUVEAU_GETPARAM_HAS_PAGEFLIP:
Ben Skeggs3376ee32011-11-12 14:28:12 +10001233 getparam->value = 1;
Francisco Jerez332b2422010-10-20 23:35:40 +02001234 break;
Marcin Koƛcielnicki69c97002010-01-26 18:39:20 +00001235 case NOUVEAU_GETPARAM_GRAPH_UNITS:
1236 /* NV40 and NV50 versions are quite different, but register
1237 * address is the same. User is supposed to know the card
1238 * family anyway... */
1239 if (dev_priv->chipset >= 0x40) {
1240 getparam->value = nv_rd32(dev, NV40_PMC_GRAPH_UNITS);
1241 break;
1242 }
1243 /* FALLTHRU */
Ben Skeggs6ee73862009-12-11 19:24:15 +10001244 default:
Francisco Jerez1397b422010-10-12 03:17:43 +02001245 NV_DEBUG(dev, "unknown parameter %lld\n", getparam->param);
Ben Skeggs6ee73862009-12-11 19:24:15 +10001246 return -EINVAL;
1247 }
1248
1249 return 0;
1250}
1251
1252int
1253nouveau_ioctl_setparam(struct drm_device *dev, void *data,
1254 struct drm_file *file_priv)
1255{
1256 struct drm_nouveau_setparam *setparam = data;
1257
Ben Skeggs6ee73862009-12-11 19:24:15 +10001258 switch (setparam->param) {
1259 default:
Francisco Jerez1397b422010-10-12 03:17:43 +02001260 NV_DEBUG(dev, "unknown parameter %lld\n", setparam->param);
Ben Skeggs6ee73862009-12-11 19:24:15 +10001261 return -EINVAL;
1262 }
1263
1264 return 0;
1265}
1266
1267/* Wait until (value(reg) & mask) == val, up until timeout has hit */
Ben Skeggs12fb9522010-11-19 14:32:56 +10001268bool
1269nouveau_wait_eq(struct drm_device *dev, uint64_t timeout,
1270 uint32_t reg, uint32_t mask, uint32_t val)
Ben Skeggs6ee73862009-12-11 19:24:15 +10001271{
1272 struct drm_nouveau_private *dev_priv = dev->dev_private;
1273 struct nouveau_timer_engine *ptimer = &dev_priv->engine.timer;
1274 uint64_t start = ptimer->read(dev);
1275
1276 do {
1277 if ((nv_rd32(dev, reg) & mask) == val)
1278 return true;
1279 } while (ptimer->read(dev) - start < timeout);
1280
1281 return false;
1282}
1283
Ben Skeggs12fb9522010-11-19 14:32:56 +10001284/* Wait until (value(reg) & mask) != val, up until timeout has hit */
1285bool
1286nouveau_wait_ne(struct drm_device *dev, uint64_t timeout,
1287 uint32_t reg, uint32_t mask, uint32_t val)
1288{
1289 struct drm_nouveau_private *dev_priv = dev->dev_private;
1290 struct nouveau_timer_engine *ptimer = &dev_priv->engine.timer;
1291 uint64_t start = ptimer->read(dev);
1292
1293 do {
1294 if ((nv_rd32(dev, reg) & mask) != val)
1295 return true;
1296 } while (ptimer->read(dev) - start < timeout);
1297
1298 return false;
1299}
1300
Ben Skeggs78e29332011-06-18 16:27:24 +10001301/* Wait until cond(data) == true, up until timeout has hit */
1302bool
1303nouveau_wait_cb(struct drm_device *dev, u64 timeout,
1304 bool (*cond)(void *), void *data)
1305{
1306 struct drm_nouveau_private *dev_priv = dev->dev_private;
1307 struct nouveau_timer_engine *ptimer = &dev_priv->engine.timer;
1308 u64 start = ptimer->read(dev);
1309
1310 do {
1311 if (cond(data) == true)
1312 return true;
1313 } while (ptimer->read(dev) - start < timeout);
1314
1315 return false;
1316}
1317
Ben Skeggs6ee73862009-12-11 19:24:15 +10001318/* Waits for PGRAPH to go completely idle */
1319bool nouveau_wait_for_idle(struct drm_device *dev)
1320{
Francisco Jerez0541324a2010-10-18 16:15:15 +02001321 struct drm_nouveau_private *dev_priv = dev->dev_private;
1322 uint32_t mask = ~0;
1323
1324 if (dev_priv->card_type == NV_40)
1325 mask &= ~NV40_PGRAPH_STATUS_SYNC_STALL;
1326
1327 if (!nv_wait(dev, NV04_PGRAPH_STATUS, mask, 0)) {
Ben Skeggs6ee73862009-12-11 19:24:15 +10001328 NV_ERROR(dev, "PGRAPH idle timed out with status 0x%08x\n",
1329 nv_rd32(dev, NV04_PGRAPH_STATUS));
1330 return false;
1331 }
1332
1333 return true;
1334}
1335