blob: 85cb5f974dfd87182b5f292d7fd358aa4a52e973 [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"
Ben Skeggs94580292012-07-06 12:14:00 +100036#include <nouveau_drm.h>
Ben Skeggsaa4cc5d22012-07-05 21:36:32 +100037#include "nouveau_agp.h"
Dave Airlie38651672010-03-30 05:34:13 +000038#include "nouveau_fbcon.h"
Ben Skeggs02a841d2012-07-04 23:44:54 +100039#include <core/ramht.h>
40#include <subdev/gpio.h>
Ben Skeggs330c5982010-09-16 15:39:49 +100041#include "nouveau_pm.h"
Ben Skeggs6ee73862009-12-11 19:24:15 +100042#include "nv50_display.h"
Ben Skeggs02a841d2012-07-04 23:44:54 +100043#include <engine/fifo.h>
Ben Skeggs5e120f62012-04-30 13:55:29 +100044#include "nouveau_fence.h"
Ben Skeggs20abd162012-04-30 11:33:43 -050045#include "nouveau_software.h"
Ben Skeggs6ee73862009-12-11 19:24:15 +100046
Ben Skeggs6ee73862009-12-11 19:24:15 +100047static void nouveau_stub_takedown(struct drm_device *dev) {}
Ben Skeggsee2e0132010-07-26 09:28:25 +100048static int nouveau_stub_init(struct drm_device *dev) { return 0; }
Ben Skeggs6ee73862009-12-11 19:24:15 +100049
50static int nouveau_init_engine_ptrs(struct drm_device *dev)
51{
52 struct drm_nouveau_private *dev_priv = dev->dev_private;
53 struct nouveau_engine *engine = &dev_priv->engine;
54
55 switch (dev_priv->chipset & 0xf0) {
56 case 0x00:
57 engine->instmem.init = nv04_instmem_init;
58 engine->instmem.takedown = nv04_instmem_takedown;
59 engine->instmem.suspend = nv04_instmem_suspend;
60 engine->instmem.resume = nv04_instmem_resume;
Ben Skeggse41115d2010-11-01 11:45:02 +100061 engine->instmem.get = nv04_instmem_get;
62 engine->instmem.put = nv04_instmem_put;
63 engine->instmem.map = nv04_instmem_map;
64 engine->instmem.unmap = nv04_instmem_unmap;
Ben Skeggsf56cb862010-07-08 11:29:10 +100065 engine->instmem.flush = nv04_instmem_flush;
Ben Skeggs6ee73862009-12-11 19:24:15 +100066 engine->mc.init = nv04_mc_init;
67 engine->mc.takedown = nv04_mc_takedown;
68 engine->timer.init = nv04_timer_init;
69 engine->timer.read = nv04_timer_read;
70 engine->timer.takedown = nv04_timer_takedown;
71 engine->fb.init = nv04_fb_init;
72 engine->fb.takedown = nv04_fb_takedown;
Francisco Jerezc88c2e02010-07-24 17:37:33 +020073 engine->display.early_init = nv04_display_early_init;
74 engine->display.late_takedown = nv04_display_late_takedown;
75 engine->display.create = nv04_display_create;
Francisco Jerezc88c2e02010-07-24 17:37:33 +020076 engine->display.destroy = nv04_display_destroy;
Ben Skeggs2a44e492011-11-09 11:36:33 +100077 engine->display.init = nv04_display_init;
78 engine->display.fini = nv04_display_fini;
Ben Skeggs36f13172011-10-27 10:24:12 +100079 engine->pm.clocks_get = nv04_pm_clocks_get;
80 engine->pm.clocks_pre = nv04_pm_clocks_pre;
81 engine->pm.clocks_set = nv04_pm_clocks_set;
Ben Skeggs7ad2d312011-12-11 00:30:05 +100082 engine->vram.init = nv04_fb_vram_init;
Ben Skeggs24f246a2011-06-10 13:36:08 +100083 engine->vram.takedown = nouveau_stub_takedown;
Ben Skeggs60d2a882010-12-06 15:28:54 +100084 engine->vram.flags_valid = nouveau_mem_flags_valid;
Ben Skeggs6ee73862009-12-11 19:24:15 +100085 break;
86 case 0x10:
87 engine->instmem.init = nv04_instmem_init;
88 engine->instmem.takedown = nv04_instmem_takedown;
89 engine->instmem.suspend = nv04_instmem_suspend;
90 engine->instmem.resume = nv04_instmem_resume;
Ben Skeggse41115d2010-11-01 11:45:02 +100091 engine->instmem.get = nv04_instmem_get;
92 engine->instmem.put = nv04_instmem_put;
93 engine->instmem.map = nv04_instmem_map;
94 engine->instmem.unmap = nv04_instmem_unmap;
Ben Skeggsf56cb862010-07-08 11:29:10 +100095 engine->instmem.flush = nv04_instmem_flush;
Ben Skeggs6ee73862009-12-11 19:24:15 +100096 engine->mc.init = nv04_mc_init;
97 engine->mc.takedown = nv04_mc_takedown;
98 engine->timer.init = nv04_timer_init;
99 engine->timer.read = nv04_timer_read;
100 engine->timer.takedown = nv04_timer_takedown;
101 engine->fb.init = nv10_fb_init;
102 engine->fb.takedown = nv10_fb_takedown;
Francisco Jereza5cf68b2010-10-24 16:14:41 +0200103 engine->fb.init_tile_region = nv10_fb_init_tile_region;
104 engine->fb.set_tile_region = nv10_fb_set_tile_region;
105 engine->fb.free_tile_region = nv10_fb_free_tile_region;
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200106 engine->display.early_init = nv04_display_early_init;
107 engine->display.late_takedown = nv04_display_late_takedown;
108 engine->display.create = nv04_display_create;
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200109 engine->display.destroy = nv04_display_destroy;
Ben Skeggs2a44e492011-11-09 11:36:33 +1000110 engine->display.init = nv04_display_init;
111 engine->display.fini = nv04_display_fini;
Ben Skeggsa0b25632011-11-21 16:41:48 +1000112 engine->gpio.drive = nv10_gpio_drive;
113 engine->gpio.sense = nv10_gpio_sense;
Ben Skeggs36f13172011-10-27 10:24:12 +1000114 engine->pm.clocks_get = nv04_pm_clocks_get;
115 engine->pm.clocks_pre = nv04_pm_clocks_pre;
116 engine->pm.clocks_set = nv04_pm_clocks_set;
Ben Skeggs7ad2d312011-12-11 00:30:05 +1000117 if (dev_priv->chipset == 0x1a ||
118 dev_priv->chipset == 0x1f)
119 engine->vram.init = nv1a_fb_vram_init;
120 else
121 engine->vram.init = nv10_fb_vram_init;
Ben Skeggs24f246a2011-06-10 13:36:08 +1000122 engine->vram.takedown = nouveau_stub_takedown;
Ben Skeggs60d2a882010-12-06 15:28:54 +1000123 engine->vram.flags_valid = nouveau_mem_flags_valid;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000124 break;
125 case 0x20:
126 engine->instmem.init = nv04_instmem_init;
127 engine->instmem.takedown = nv04_instmem_takedown;
128 engine->instmem.suspend = nv04_instmem_suspend;
129 engine->instmem.resume = nv04_instmem_resume;
Ben Skeggse41115d2010-11-01 11:45:02 +1000130 engine->instmem.get = nv04_instmem_get;
131 engine->instmem.put = nv04_instmem_put;
132 engine->instmem.map = nv04_instmem_map;
133 engine->instmem.unmap = nv04_instmem_unmap;
Ben Skeggsf56cb862010-07-08 11:29:10 +1000134 engine->instmem.flush = nv04_instmem_flush;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000135 engine->mc.init = nv04_mc_init;
136 engine->mc.takedown = nv04_mc_takedown;
137 engine->timer.init = nv04_timer_init;
138 engine->timer.read = nv04_timer_read;
139 engine->timer.takedown = nv04_timer_takedown;
Ben Skeggsd81c19e2011-12-12 22:51:33 +1000140 engine->fb.init = nv20_fb_init;
141 engine->fb.takedown = nv20_fb_takedown;
142 engine->fb.init_tile_region = nv20_fb_init_tile_region;
143 engine->fb.set_tile_region = nv20_fb_set_tile_region;
144 engine->fb.free_tile_region = nv20_fb_free_tile_region;
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200145 engine->display.early_init = nv04_display_early_init;
146 engine->display.late_takedown = nv04_display_late_takedown;
147 engine->display.create = nv04_display_create;
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200148 engine->display.destroy = nv04_display_destroy;
Ben Skeggs2a44e492011-11-09 11:36:33 +1000149 engine->display.init = nv04_display_init;
150 engine->display.fini = nv04_display_fini;
Ben Skeggsa0b25632011-11-21 16:41:48 +1000151 engine->gpio.drive = nv10_gpio_drive;
152 engine->gpio.sense = nv10_gpio_sense;
Ben Skeggs36f13172011-10-27 10:24:12 +1000153 engine->pm.clocks_get = nv04_pm_clocks_get;
154 engine->pm.clocks_pre = nv04_pm_clocks_pre;
155 engine->pm.clocks_set = nv04_pm_clocks_set;
Ben Skeggsd81c19e2011-12-12 22:51:33 +1000156 engine->vram.init = nv20_fb_vram_init;
Ben Skeggs24f246a2011-06-10 13:36:08 +1000157 engine->vram.takedown = nouveau_stub_takedown;
Ben Skeggs60d2a882010-12-06 15:28:54 +1000158 engine->vram.flags_valid = nouveau_mem_flags_valid;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000159 break;
160 case 0x30:
161 engine->instmem.init = nv04_instmem_init;
162 engine->instmem.takedown = nv04_instmem_takedown;
163 engine->instmem.suspend = nv04_instmem_suspend;
164 engine->instmem.resume = nv04_instmem_resume;
Ben Skeggse41115d2010-11-01 11:45:02 +1000165 engine->instmem.get = nv04_instmem_get;
166 engine->instmem.put = nv04_instmem_put;
167 engine->instmem.map = nv04_instmem_map;
168 engine->instmem.unmap = nv04_instmem_unmap;
Ben Skeggsf56cb862010-07-08 11:29:10 +1000169 engine->instmem.flush = nv04_instmem_flush;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000170 engine->mc.init = nv04_mc_init;
171 engine->mc.takedown = nv04_mc_takedown;
172 engine->timer.init = nv04_timer_init;
173 engine->timer.read = nv04_timer_read;
174 engine->timer.takedown = nv04_timer_takedown;
Francisco Jerez8bded182010-07-21 21:08:11 +0200175 engine->fb.init = nv30_fb_init;
176 engine->fb.takedown = nv30_fb_takedown;
Francisco Jereza5cf68b2010-10-24 16:14:41 +0200177 engine->fb.init_tile_region = nv30_fb_init_tile_region;
178 engine->fb.set_tile_region = nv10_fb_set_tile_region;
179 engine->fb.free_tile_region = nv30_fb_free_tile_region;
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200180 engine->display.early_init = nv04_display_early_init;
181 engine->display.late_takedown = nv04_display_late_takedown;
182 engine->display.create = nv04_display_create;
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200183 engine->display.destroy = nv04_display_destroy;
Ben Skeggs2a44e492011-11-09 11:36:33 +1000184 engine->display.init = nv04_display_init;
185 engine->display.fini = nv04_display_fini;
Ben Skeggsa0b25632011-11-21 16:41:48 +1000186 engine->gpio.drive = nv10_gpio_drive;
187 engine->gpio.sense = nv10_gpio_sense;
Ben Skeggs36f13172011-10-27 10:24:12 +1000188 engine->pm.clocks_get = nv04_pm_clocks_get;
189 engine->pm.clocks_pre = nv04_pm_clocks_pre;
190 engine->pm.clocks_set = nv04_pm_clocks_set;
Ben Skeggs442b6262010-09-16 16:25:26 +1000191 engine->pm.voltage_get = nouveau_voltage_gpio_get;
192 engine->pm.voltage_set = nouveau_voltage_gpio_set;
Ben Skeggsd81c19e2011-12-12 22:51:33 +1000193 engine->vram.init = nv20_fb_vram_init;
Ben Skeggs24f246a2011-06-10 13:36:08 +1000194 engine->vram.takedown = nouveau_stub_takedown;
Ben Skeggs60d2a882010-12-06 15:28:54 +1000195 engine->vram.flags_valid = nouveau_mem_flags_valid;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000196 break;
197 case 0x40:
198 case 0x60:
199 engine->instmem.init = nv04_instmem_init;
200 engine->instmem.takedown = nv04_instmem_takedown;
201 engine->instmem.suspend = nv04_instmem_suspend;
202 engine->instmem.resume = nv04_instmem_resume;
Ben Skeggse41115d2010-11-01 11:45:02 +1000203 engine->instmem.get = nv04_instmem_get;
204 engine->instmem.put = nv04_instmem_put;
205 engine->instmem.map = nv04_instmem_map;
206 engine->instmem.unmap = nv04_instmem_unmap;
Ben Skeggsf56cb862010-07-08 11:29:10 +1000207 engine->instmem.flush = nv04_instmem_flush;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000208 engine->mc.init = nv40_mc_init;
209 engine->mc.takedown = nv40_mc_takedown;
210 engine->timer.init = nv04_timer_init;
211 engine->timer.read = nv04_timer_read;
212 engine->timer.takedown = nv04_timer_takedown;
213 engine->fb.init = nv40_fb_init;
214 engine->fb.takedown = nv40_fb_takedown;
Francisco Jereza5cf68b2010-10-24 16:14:41 +0200215 engine->fb.init_tile_region = nv30_fb_init_tile_region;
216 engine->fb.set_tile_region = nv40_fb_set_tile_region;
217 engine->fb.free_tile_region = nv30_fb_free_tile_region;
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200218 engine->display.early_init = nv04_display_early_init;
219 engine->display.late_takedown = nv04_display_late_takedown;
220 engine->display.create = nv04_display_create;
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200221 engine->display.destroy = nv04_display_destroy;
Ben Skeggs2a44e492011-11-09 11:36:33 +1000222 engine->display.init = nv04_display_init;
223 engine->display.fini = nv04_display_fini;
Ben Skeggs47e5d5c2011-11-22 13:49:22 +1000224 engine->gpio.init = nv10_gpio_init;
225 engine->gpio.fini = nv10_gpio_fini;
Ben Skeggsa0b25632011-11-21 16:41:48 +1000226 engine->gpio.drive = nv10_gpio_drive;
227 engine->gpio.sense = nv10_gpio_sense;
Ben Skeggs47e5d5c2011-11-22 13:49:22 +1000228 engine->gpio.irq_enable = nv10_gpio_irq_enable;
Ben Skeggs1262a202011-07-18 15:15:34 +1000229 engine->pm.clocks_get = nv40_pm_clocks_get;
230 engine->pm.clocks_pre = nv40_pm_clocks_pre;
231 engine->pm.clocks_set = nv40_pm_clocks_set;
Ben Skeggs442b6262010-09-16 16:25:26 +1000232 engine->pm.voltage_get = nouveau_voltage_gpio_get;
233 engine->pm.voltage_set = nouveau_voltage_gpio_set;
Francisco Jerez8155cac2010-09-23 20:58:38 +0200234 engine->pm.temp_get = nv40_temp_get;
Ben Skeggs69346182011-09-17 02:11:39 +1000235 engine->pm.pwm_get = nv40_pm_pwm_get;
236 engine->pm.pwm_set = nv40_pm_pwm_set;
Ben Skeggsff92a6c2011-12-12 23:03:14 +1000237 engine->vram.init = nv40_fb_vram_init;
Ben Skeggs24f246a2011-06-10 13:36:08 +1000238 engine->vram.takedown = nouveau_stub_takedown;
Ben Skeggs60d2a882010-12-06 15:28:54 +1000239 engine->vram.flags_valid = nouveau_mem_flags_valid;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000240 break;
241 case 0x50:
242 case 0x80: /* gotta love NVIDIA's consistency.. */
243 case 0x90:
Ben Skeggsd9f61c22011-07-04 13:25:17 +1000244 case 0xa0:
Ben Skeggs6ee73862009-12-11 19:24:15 +1000245 engine->instmem.init = nv50_instmem_init;
246 engine->instmem.takedown = nv50_instmem_takedown;
247 engine->instmem.suspend = nv50_instmem_suspend;
248 engine->instmem.resume = nv50_instmem_resume;
Ben Skeggse41115d2010-11-01 11:45:02 +1000249 engine->instmem.get = nv50_instmem_get;
250 engine->instmem.put = nv50_instmem_put;
251 engine->instmem.map = nv50_instmem_map;
252 engine->instmem.unmap = nv50_instmem_unmap;
Ben Skeggs734ee832010-07-15 11:02:54 +1000253 if (dev_priv->chipset == 0x50)
254 engine->instmem.flush = nv50_instmem_flush;
255 else
256 engine->instmem.flush = nv84_instmem_flush;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000257 engine->mc.init = nv50_mc_init;
258 engine->mc.takedown = nv50_mc_takedown;
259 engine->timer.init = nv04_timer_init;
260 engine->timer.read = nv04_timer_read;
261 engine->timer.takedown = nv04_timer_takedown;
Marcin Kościelnicki304424e2010-03-01 00:18:39 +0000262 engine->fb.init = nv50_fb_init;
263 engine->fb.takedown = nv50_fb_takedown;
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200264 engine->display.early_init = nv50_display_early_init;
265 engine->display.late_takedown = nv50_display_late_takedown;
266 engine->display.create = nv50_display_create;
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200267 engine->display.destroy = nv50_display_destroy;
Ben Skeggs2a44e492011-11-09 11:36:33 +1000268 engine->display.init = nv50_display_init;
269 engine->display.fini = nv50_display_fini;
Ben Skeggsee2e0132010-07-26 09:28:25 +1000270 engine->gpio.init = nv50_gpio_init;
Ben Skeggsa0b25632011-11-21 16:41:48 +1000271 engine->gpio.fini = nv50_gpio_fini;
272 engine->gpio.drive = nv50_gpio_drive;
273 engine->gpio.sense = nv50_gpio_sense;
Ben Skeggsee2e0132010-07-26 09:28:25 +1000274 engine->gpio.irq_enable = nv50_gpio_irq_enable;
Ben Skeggsfade7ad2010-09-27 11:18:14 +1000275 switch (dev_priv->chipset) {
Ben Skeggsbd2e5972010-10-19 20:06:01 +1000276 case 0x84:
277 case 0x86:
278 case 0x92:
279 case 0x94:
280 case 0x96:
281 case 0x98:
282 case 0xa0:
Ben Skeggs5f801982010-10-22 08:44:09 +1000283 case 0xaa:
284 case 0xac:
Ben Skeggsbd2e5972010-10-19 20:06:01 +1000285 case 0x50:
Ben Skeggsf3fbaf32011-10-26 09:11:02 +1000286 engine->pm.clocks_get = nv50_pm_clocks_get;
287 engine->pm.clocks_pre = nv50_pm_clocks_pre;
288 engine->pm.clocks_set = nv50_pm_clocks_set;
Ben Skeggsfade7ad2010-09-27 11:18:14 +1000289 break;
Ben Skeggsbd2e5972010-10-19 20:06:01 +1000290 default:
Ben Skeggsca94a712011-06-17 15:38:48 +1000291 engine->pm.clocks_get = nva3_pm_clocks_get;
292 engine->pm.clocks_pre = nva3_pm_clocks_pre;
293 engine->pm.clocks_set = nva3_pm_clocks_set;
Ben Skeggsbd2e5972010-10-19 20:06:01 +1000294 break;
Ben Skeggsfade7ad2010-09-27 11:18:14 +1000295 }
Ben Skeggs02c30ca2010-09-16 16:17:35 +1000296 engine->pm.voltage_get = nouveau_voltage_gpio_get;
297 engine->pm.voltage_set = nouveau_voltage_gpio_set;
Francisco Jerez8155cac2010-09-23 20:58:38 +0200298 if (dev_priv->chipset >= 0x84)
299 engine->pm.temp_get = nv84_temp_get;
300 else
301 engine->pm.temp_get = nv40_temp_get;
Ben Skeggs5a4267a2011-09-17 02:01:24 +1000302 engine->pm.pwm_get = nv50_pm_pwm_get;
303 engine->pm.pwm_set = nv50_pm_pwm_set;
Ben Skeggs60d2a882010-12-06 15:28:54 +1000304 engine->vram.init = nv50_vram_init;
Ben Skeggs24f246a2011-06-10 13:36:08 +1000305 engine->vram.takedown = nv50_vram_fini;
Ben Skeggs60d2a882010-12-06 15:28:54 +1000306 engine->vram.get = nv50_vram_new;
307 engine->vram.put = nv50_vram_del;
308 engine->vram.flags_valid = nv50_vram_flags_valid;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000309 break;
Ben Skeggsd9f61c22011-07-04 13:25:17 +1000310 case 0xc0:
Ben Skeggs4b223ee2010-08-03 10:00:56 +1000311 engine->instmem.init = nvc0_instmem_init;
312 engine->instmem.takedown = nvc0_instmem_takedown;
313 engine->instmem.suspend = nvc0_instmem_suspend;
314 engine->instmem.resume = nvc0_instmem_resume;
Ben Skeggs8984e042010-11-15 11:48:33 +1000315 engine->instmem.get = nv50_instmem_get;
316 engine->instmem.put = nv50_instmem_put;
317 engine->instmem.map = nv50_instmem_map;
318 engine->instmem.unmap = nv50_instmem_unmap;
319 engine->instmem.flush = nv84_instmem_flush;
Ben Skeggs4b223ee2010-08-03 10:00:56 +1000320 engine->mc.init = nv50_mc_init;
321 engine->mc.takedown = nv50_mc_takedown;
322 engine->timer.init = nv04_timer_init;
323 engine->timer.read = nv04_timer_read;
324 engine->timer.takedown = nv04_timer_takedown;
325 engine->fb.init = nvc0_fb_init;
326 engine->fb.takedown = nvc0_fb_takedown;
Ben Skeggs4b223ee2010-08-03 10:00:56 +1000327 engine->display.early_init = nv50_display_early_init;
328 engine->display.late_takedown = nv50_display_late_takedown;
329 engine->display.create = nv50_display_create;
Ben Skeggs4b223ee2010-08-03 10:00:56 +1000330 engine->display.destroy = nv50_display_destroy;
Ben Skeggs2a44e492011-11-09 11:36:33 +1000331 engine->display.init = nv50_display_init;
332 engine->display.fini = nv50_display_fini;
Ben Skeggs4b223ee2010-08-03 10:00:56 +1000333 engine->gpio.init = nv50_gpio_init;
Ben Skeggsa0b25632011-11-21 16:41:48 +1000334 engine->gpio.fini = nv50_gpio_fini;
335 engine->gpio.drive = nv50_gpio_drive;
336 engine->gpio.sense = nv50_gpio_sense;
Ben Skeggs4b223ee2010-08-03 10:00:56 +1000337 engine->gpio.irq_enable = nv50_gpio_irq_enable;
Ben Skeggs8984e042010-11-15 11:48:33 +1000338 engine->vram.init = nvc0_vram_init;
Ben Skeggs24f246a2011-06-10 13:36:08 +1000339 engine->vram.takedown = nv50_vram_fini;
Ben Skeggs8984e042010-11-15 11:48:33 +1000340 engine->vram.get = nvc0_vram_new;
341 engine->vram.put = nv50_vram_del;
342 engine->vram.flags_valid = nvc0_vram_flags_valid;
Martin Peres74cfad12011-05-12 22:40:47 +0200343 engine->pm.temp_get = nv84_temp_get;
Ben Skeggs354d0782011-06-19 01:44:36 +1000344 engine->pm.clocks_get = nvc0_pm_clocks_get;
Ben Skeggs045da4e2011-10-29 00:22:49 +1000345 engine->pm.clocks_pre = nvc0_pm_clocks_pre;
346 engine->pm.clocks_set = nvc0_pm_clocks_set;
Ben Skeggs3c71c232011-06-09 17:34:02 +1000347 engine->pm.voltage_get = nouveau_voltage_gpio_get;
Ben Skeggsda1dc4c2011-06-10 12:07:09 +1000348 engine->pm.voltage_set = nouveau_voltage_gpio_set;
Ben Skeggs5a4267a2011-09-17 02:01:24 +1000349 engine->pm.pwm_get = nv50_pm_pwm_get;
350 engine->pm.pwm_set = nv50_pm_pwm_set;
Ben Skeggs4b223ee2010-08-03 10:00:56 +1000351 break;
Ben Skeggsd9f61c22011-07-04 13:25:17 +1000352 case 0xd0:
353 engine->instmem.init = nvc0_instmem_init;
354 engine->instmem.takedown = nvc0_instmem_takedown;
355 engine->instmem.suspend = nvc0_instmem_suspend;
356 engine->instmem.resume = nvc0_instmem_resume;
357 engine->instmem.get = nv50_instmem_get;
358 engine->instmem.put = nv50_instmem_put;
359 engine->instmem.map = nv50_instmem_map;
360 engine->instmem.unmap = nv50_instmem_unmap;
361 engine->instmem.flush = nv84_instmem_flush;
362 engine->mc.init = nv50_mc_init;
363 engine->mc.takedown = nv50_mc_takedown;
364 engine->timer.init = nv04_timer_init;
365 engine->timer.read = nv04_timer_read;
366 engine->timer.takedown = nv04_timer_takedown;
367 engine->fb.init = nvc0_fb_init;
368 engine->fb.takedown = nvc0_fb_takedown;
Ben Skeggsd9f61c22011-07-04 13:25:17 +1000369 engine->display.early_init = nouveau_stub_init;
370 engine->display.late_takedown = nouveau_stub_takedown;
Ben Skeggs26f6d882011-07-04 16:25:18 +1000371 engine->display.create = nvd0_display_create;
Ben Skeggs26f6d882011-07-04 16:25:18 +1000372 engine->display.destroy = nvd0_display_destroy;
Ben Skeggs2a44e492011-11-09 11:36:33 +1000373 engine->display.init = nvd0_display_init;
374 engine->display.fini = nvd0_display_fini;
Ben Skeggsd7f81722011-07-03 02:57:35 +1000375 engine->gpio.init = nv50_gpio_init;
Ben Skeggsa0b25632011-11-21 16:41:48 +1000376 engine->gpio.fini = nv50_gpio_fini;
377 engine->gpio.drive = nvd0_gpio_drive;
378 engine->gpio.sense = nvd0_gpio_sense;
Ben Skeggsd7f81722011-07-03 02:57:35 +1000379 engine->gpio.irq_enable = nv50_gpio_irq_enable;
Ben Skeggsd9f61c22011-07-04 13:25:17 +1000380 engine->vram.init = nvc0_vram_init;
381 engine->vram.takedown = nv50_vram_fini;
382 engine->vram.get = nvc0_vram_new;
383 engine->vram.put = nv50_vram_del;
384 engine->vram.flags_valid = nvc0_vram_flags_valid;
Martin Peres61091832011-10-22 01:40:40 +0200385 engine->pm.temp_get = nv84_temp_get;
Ben Skeggs4784e4a2011-07-04 14:06:07 +1000386 engine->pm.clocks_get = nvc0_pm_clocks_get;
Ben Skeggs045da4e2011-10-29 00:22:49 +1000387 engine->pm.clocks_pre = nvc0_pm_clocks_pre;
388 engine->pm.clocks_set = nvc0_pm_clocks_set;
Ben Skeggs4784e4a2011-07-04 14:06:07 +1000389 engine->pm.voltage_get = nouveau_voltage_gpio_get;
390 engine->pm.voltage_set = nouveau_voltage_gpio_set;
Ben Skeggsd9f61c22011-07-04 13:25:17 +1000391 break;
Ben Skeggs68455a42012-03-04 14:47:55 +1000392 case 0xe0:
393 engine->instmem.init = nvc0_instmem_init;
394 engine->instmem.takedown = nvc0_instmem_takedown;
395 engine->instmem.suspend = nvc0_instmem_suspend;
396 engine->instmem.resume = nvc0_instmem_resume;
397 engine->instmem.get = nv50_instmem_get;
398 engine->instmem.put = nv50_instmem_put;
399 engine->instmem.map = nv50_instmem_map;
400 engine->instmem.unmap = nv50_instmem_unmap;
401 engine->instmem.flush = nv84_instmem_flush;
402 engine->mc.init = nv50_mc_init;
403 engine->mc.takedown = nv50_mc_takedown;
404 engine->timer.init = nv04_timer_init;
405 engine->timer.read = nv04_timer_read;
406 engine->timer.takedown = nv04_timer_takedown;
407 engine->fb.init = nvc0_fb_init;
408 engine->fb.takedown = nvc0_fb_takedown;
Ben Skeggs68455a42012-03-04 14:47:55 +1000409 engine->display.early_init = nouveau_stub_init;
410 engine->display.late_takedown = nouveau_stub_takedown;
411 engine->display.create = nvd0_display_create;
412 engine->display.destroy = nvd0_display_destroy;
413 engine->display.init = nvd0_display_init;
414 engine->display.fini = nvd0_display_fini;
415 engine->gpio.init = nv50_gpio_init;
416 engine->gpio.fini = nv50_gpio_fini;
417 engine->gpio.drive = nvd0_gpio_drive;
418 engine->gpio.sense = nvd0_gpio_sense;
419 engine->gpio.irq_enable = nv50_gpio_irq_enable;
420 engine->vram.init = nvc0_vram_init;
421 engine->vram.takedown = nv50_vram_fini;
422 engine->vram.get = nvc0_vram_new;
423 engine->vram.put = nv50_vram_del;
424 engine->vram.flags_valid = nvc0_vram_flags_valid;
425 break;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000426 default:
427 NV_ERROR(dev, "NV%02x unsupported\n", dev_priv->chipset);
428 return 1;
429 }
430
Ben Skeggs03bc9672011-07-04 13:14:05 +1000431 /* headless mode */
432 if (nouveau_modeset == 2) {
433 engine->display.early_init = nouveau_stub_init;
434 engine->display.late_takedown = nouveau_stub_takedown;
435 engine->display.create = nouveau_stub_init;
436 engine->display.init = nouveau_stub_init;
437 engine->display.destroy = nouveau_stub_takedown;
438 }
439
Ben Skeggs6ee73862009-12-11 19:24:15 +1000440 return 0;
441}
442
443static unsigned int
444nouveau_vga_set_decode(void *priv, bool state)
445{
Marcin Kościelnicki9967b942010-02-08 00:20:17 +0000446 struct drm_device *dev = priv;
447 struct drm_nouveau_private *dev_priv = dev->dev_private;
448
449 if (dev_priv->chipset >= 0x40)
450 nv_wr32(dev, 0x88054, state);
451 else
452 nv_wr32(dev, 0x1854, state);
453
Ben Skeggs6ee73862009-12-11 19:24:15 +1000454 if (state)
455 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
456 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
457 else
458 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
459}
460
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000461static void nouveau_switcheroo_set_state(struct pci_dev *pdev,
462 enum vga_switcheroo_state state)
463{
Dave Airliefbf81762010-06-01 09:09:06 +1000464 struct drm_device *dev = pci_get_drvdata(pdev);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000465 pm_message_t pmm = { .event = PM_EVENT_SUSPEND };
466 if (state == VGA_SWITCHEROO_ON) {
467 printk(KERN_ERR "VGA switcheroo: switched nouveau on\n");
Dave Airlie5bcf7192010-12-07 09:20:40 +1000468 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000469 nouveau_pci_resume(pdev);
Dave Airliefbf81762010-06-01 09:09:06 +1000470 drm_kms_helper_poll_enable(dev);
Dave Airlie5bcf7192010-12-07 09:20:40 +1000471 dev->switch_power_state = DRM_SWITCH_POWER_ON;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000472 } else {
473 printk(KERN_ERR "VGA switcheroo: switched nouveau off\n");
Dave Airlie5bcf7192010-12-07 09:20:40 +1000474 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
Dave Airliefbf81762010-06-01 09:09:06 +1000475 drm_kms_helper_poll_disable(dev);
Peter Lekensteynd0992302011-12-17 12:54:04 +0100476 nouveau_switcheroo_optimus_dsm();
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000477 nouveau_pci_suspend(pdev, pmm);
Dave Airlie5bcf7192010-12-07 09:20:40 +1000478 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000479 }
480}
481
Dave Airlie8d608aa2010-12-07 08:57:57 +1000482static void nouveau_switcheroo_reprobe(struct pci_dev *pdev)
483{
484 struct drm_device *dev = pci_get_drvdata(pdev);
485 nouveau_fbcon_output_poll_changed(dev);
486}
487
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000488static bool nouveau_switcheroo_can_switch(struct pci_dev *pdev)
489{
490 struct drm_device *dev = pci_get_drvdata(pdev);
491 bool can_switch;
492
493 spin_lock(&dev->count_lock);
494 can_switch = (dev->open_count == 0);
495 spin_unlock(&dev->count_lock);
496 return can_switch;
497}
498
Ben Skeggs48aca132012-03-18 00:40:41 +1000499static void
500nouveau_card_channel_fini(struct drm_device *dev)
501{
502 struct drm_nouveau_private *dev_priv = dev->dev_private;
503
504 if (dev_priv->channel)
505 nouveau_channel_put_unlocked(&dev_priv->channel);
506}
507
508static int
509nouveau_card_channel_init(struct drm_device *dev)
510{
511 struct drm_nouveau_private *dev_priv = dev->dev_private;
512 struct nouveau_channel *chan;
Ben Skeggsd1b167e2012-05-04 14:01:52 +1000513 int ret;
Ben Skeggs48aca132012-03-18 00:40:41 +1000514
515 ret = nouveau_channel_alloc(dev, &chan, NULL, NvDmaFB, NvDmaTT);
516 dev_priv->channel = chan;
517 if (ret)
518 return ret;
Ben Skeggs48aca132012-03-18 00:40:41 +1000519 mutex_unlock(&dev_priv->channel->mutex);
520
Ben Skeggsd1b167e2012-05-04 14:01:52 +1000521 nouveau_bo_move_init(chan);
522 return 0;
Ben Skeggs48aca132012-03-18 00:40:41 +1000523}
524
Takashi Iwai26ec6852012-05-11 07:51:17 +0200525static const struct vga_switcheroo_client_ops nouveau_switcheroo_ops = {
526 .set_gpu_state = nouveau_switcheroo_set_state,
527 .reprobe = nouveau_switcheroo_reprobe,
528 .can_switch = nouveau_switcheroo_can_switch,
529};
530
Ben Skeggs6ee73862009-12-11 19:24:15 +1000531int
532nouveau_card_init(struct drm_device *dev)
533{
534 struct drm_nouveau_private *dev_priv = dev->dev_private;
535 struct nouveau_engine *engine;
Ben Skeggseea55c82011-04-18 08:57:51 +1000536 int ret, e = 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000537
Ben Skeggs6ee73862009-12-11 19:24:15 +1000538 vga_client_register(dev->pdev, dev, NULL, nouveau_vga_set_decode);
Takashi Iwai26ec6852012-05-11 07:51:17 +0200539 vga_switcheroo_register_client(dev->pdev, &nouveau_switcheroo_ops);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000540
541 /* Initialise internal driver API hooks */
542 ret = nouveau_init_engine_ptrs(dev);
543 if (ret)
Marcin Kościelnickic5804be2009-12-14 20:58:39 +0000544 goto out;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000545 engine = &dev_priv->engine;
Ben Skeggscff5c132010-10-06 16:16:59 +1000546 spin_lock_init(&dev_priv->channels.lock);
Francisco Jereza5cf68b2010-10-24 16:14:41 +0200547 spin_lock_init(&dev_priv->tile.lock);
Maarten Maathuisff9e5272010-02-01 20:58:27 +0100548 spin_lock_init(&dev_priv->context_switch_lock);
Ben Skeggs04eb34a2011-04-06 13:28:35 +1000549 spin_lock_init(&dev_priv->vm_lock);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000550
Ben Skeggsaa4cc5d22012-07-05 21:36:32 +1000551 /* Make sure the AGP controller is in a consistent state */
552 nouveau_agp_reset(dev);
553
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200554 /* Make the CRTCs and I2C buses accessible */
555 ret = engine->display.early_init(dev);
556 if (ret)
557 goto out;
558
Ben Skeggs6ee73862009-12-11 19:24:15 +1000559 /* Parse BIOS tables / Run init tables if card not POSTed */
Ben Skeggscd0b0722010-06-01 15:56:22 +1000560 ret = nouveau_bios_init(dev);
561 if (ret)
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200562 goto out_display_early;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000563
Ben Skeggs4c5df492011-10-28 10:59:45 +1000564 /* workaround an odd issue on nvc1 by disabling the device's
565 * nosnoop capability. hopefully won't cause issues until a
566 * better fix is found - assuming there is one...
567 */
568 if (dev_priv->chipset == 0xc1) {
569 nv_mask(dev, 0x00088080, 0x00000800, 0x00000000);
570 }
571
Ben Skeggs668b6c02011-12-15 10:43:03 +1000572 /* PMC */
573 ret = engine->mc.init(dev);
Ben Skeggsa76fb4e2010-03-18 09:45:20 +1000574 if (ret)
575 goto out_bios;
576
Ben Skeggs668b6c02011-12-15 10:43:03 +1000577 /* PTIMER */
578 ret = engine->timer.init(dev);
579 if (ret)
580 goto out_mc;
581
582 /* PFB */
583 ret = engine->fb.init(dev);
584 if (ret)
585 goto out_timer;
586
587 ret = engine->vram.init(dev);
588 if (ret)
589 goto out_fb;
590
591 /* PGPIO */
592 ret = nouveau_gpio_create(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000593 if (ret)
Ben Skeggsfbd2895e2010-09-01 15:24:34 +1000594 goto out_vram;
595
Ben Skeggs668b6c02011-12-15 10:43:03 +1000596 ret = nouveau_gpuobj_init(dev);
597 if (ret)
598 goto out_gpio;
599
Ben Skeggsfbd2895e2010-09-01 15:24:34 +1000600 ret = engine->instmem.init(dev);
601 if (ret)
602 goto out_gpuobj;
603
Ben Skeggs24f246a2011-06-10 13:36:08 +1000604 ret = nouveau_mem_vram_init(dev);
Ben Skeggsfbd2895e2010-09-01 15:24:34 +1000605 if (ret)
606 goto out_instmem;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000607
Ben Skeggs24f246a2011-06-10 13:36:08 +1000608 ret = nouveau_mem_gart_init(dev);
609 if (ret)
610 goto out_ttmvram;
611
Ben Skeggsaba99a82011-05-25 14:48:50 +1000612 if (!dev_priv->noaccel) {
Ben Skeggs18b54c42011-05-25 15:22:33 +1000613 switch (dev_priv->card_type) {
614 case NV_04:
Ben Skeggsc420b2d2012-05-01 20:48:08 +1000615 nv04_fifo_create(dev);
616 break;
617 case NV_10:
618 case NV_20:
619 case NV_30:
620 if (dev_priv->chipset < 0x17)
621 nv10_fifo_create(dev);
622 else
623 nv17_fifo_create(dev);
624 break;
625 case NV_40:
626 nv40_fifo_create(dev);
627 break;
628 case NV_50:
629 if (dev_priv->chipset == 0x50)
630 nv50_fifo_create(dev);
631 else
632 nv84_fifo_create(dev);
633 break;
634 case NV_C0:
635 case NV_D0:
636 nvc0_fifo_create(dev);
637 break;
638 case NV_E0:
639 nve0_fifo_create(dev);
640 break;
641 default:
642 break;
643 }
644
645 switch (dev_priv->card_type) {
646 case NV_04:
Ben Skeggs5e120f62012-04-30 13:55:29 +1000647 nv04_fence_create(dev);
648 break;
649 case NV_10:
650 case NV_20:
651 case NV_30:
652 case NV_40:
653 case NV_50:
654 if (dev_priv->chipset < 0x84)
655 nv10_fence_create(dev);
656 else
657 nv84_fence_create(dev);
658 break;
659 case NV_C0:
660 case NV_D0:
661 case NV_E0:
662 nvc0_fence_create(dev);
663 break;
664 default:
665 break;
666 }
667
668 switch (dev_priv->card_type) {
669 case NV_04:
Ben Skeggs20abd162012-04-30 11:33:43 -0500670 case NV_10:
671 case NV_20:
672 case NV_30:
673 case NV_40:
674 nv04_software_create(dev);
675 break;
676 case NV_50:
677 nv50_software_create(dev);
678 break;
679 case NV_C0:
680 case NV_D0:
681 case NV_E0:
682 nvc0_software_create(dev);
683 break;
684 default:
685 break;
686 }
687
688 switch (dev_priv->card_type) {
689 case NV_04:
Ben Skeggs18b54c42011-05-25 15:22:33 +1000690 nv04_graph_create(dev);
691 break;
692 case NV_10:
693 nv10_graph_create(dev);
694 break;
695 case NV_20:
696 case NV_30:
697 nv20_graph_create(dev);
698 break;
699 case NV_40:
700 nv40_graph_create(dev);
701 break;
702 case NV_50:
703 nv50_graph_create(dev);
704 break;
705 case NV_C0:
Ben Skeggs06784092011-07-11 15:57:54 +1000706 case NV_D0:
Ben Skeggs18b54c42011-05-25 15:22:33 +1000707 nvc0_graph_create(dev);
708 break;
Ben Skeggsab394542012-03-13 13:05:13 +1000709 case NV_E0:
710 nve0_graph_create(dev);
711 break;
Ben Skeggs18b54c42011-05-25 15:22:33 +1000712 default:
Ben Skeggs7ff54412011-03-18 10:25:59 +1000713 break;
714 }
Ben Skeggs7ff54412011-03-18 10:25:59 +1000715
Ben Skeggs18b54c42011-05-25 15:22:33 +1000716 switch (dev_priv->chipset) {
717 case 0x84:
718 case 0x86:
719 case 0x92:
720 case 0x94:
721 case 0x96:
722 case 0xa0:
723 nv84_crypt_create(dev);
724 break;
Ben Skeggs8f27c542011-08-11 14:58:06 +1000725 case 0x98:
726 case 0xaa:
727 case 0xac:
728 nv98_crypt_create(dev);
729 break;
Ben Skeggs18b54c42011-05-25 15:22:33 +1000730 }
Ben Skeggsa02ccc72011-04-04 16:08:24 +1000731
Ben Skeggs18b54c42011-05-25 15:22:33 +1000732 switch (dev_priv->card_type) {
733 case NV_50:
734 switch (dev_priv->chipset) {
735 case 0xa3:
736 case 0xa5:
737 case 0xa8:
Ben Skeggs18b54c42011-05-25 15:22:33 +1000738 nva3_copy_create(dev);
739 break;
740 }
741 break;
742 case NV_C0:
Ben Skeggs14f04582012-08-27 16:22:49 +1000743 if (!(nv_rd32(dev, 0x022500) & 0x00000200))
744 nvc0_copy_create(dev, 1);
Ben Skeggs0c75f332012-05-04 17:16:46 +1000745 case NV_D0:
Ben Skeggs14f04582012-08-27 16:22:49 +1000746 if (!(nv_rd32(dev, 0x022500) & 0x00000100))
747 nvc0_copy_create(dev, 0);
Ben Skeggs18b54c42011-05-25 15:22:33 +1000748 break;
749 default:
750 break;
751 }
752
Ben Skeggs8f27c542011-08-11 14:58:06 +1000753 if (dev_priv->chipset >= 0xa3 || dev_priv->chipset == 0x98) {
754 nv84_bsp_create(dev);
755 nv84_vp_create(dev);
756 nv98_ppp_create(dev);
757 } else
758 if (dev_priv->chipset >= 0x84) {
759 nv50_mpeg_create(dev);
760 nv84_bsp_create(dev);
761 nv84_vp_create(dev);
762 } else
763 if (dev_priv->chipset >= 0x50) {
764 nv50_mpeg_create(dev);
765 } else
Ben Skeggs52d07332011-06-23 16:44:05 +1000766 if (dev_priv->card_type == NV_40 ||
767 dev_priv->chipset == 0x31 ||
768 dev_priv->chipset == 0x34 ||
Ben Skeggs8f27c542011-08-11 14:58:06 +1000769 dev_priv->chipset == 0x36) {
Ben Skeggs323dcac2011-06-23 16:21:21 +1000770 nv31_mpeg_create(dev);
Ben Skeggs8f27c542011-08-11 14:58:06 +1000771 }
Ben Skeggs18b54c42011-05-25 15:22:33 +1000772
Ben Skeggs6dfdd7a2011-03-31 15:40:43 +1000773 for (e = 0; e < NVOBJ_ENGINE_NR; e++) {
774 if (dev_priv->eng[e]) {
775 ret = dev_priv->eng[e]->init(dev, e);
776 if (ret)
777 goto out_engine;
778 }
779 }
Marcin Kościelnickia32ed692010-01-26 14:00:42 +0000780 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000781
Ben Skeggs1575b362011-07-04 11:55:39 +1000782 ret = nouveau_irq_init(dev);
783 if (ret)
Ben Skeggsc420b2d2012-05-01 20:48:08 +1000784 goto out_engine;
Ben Skeggs1575b362011-07-04 11:55:39 +1000785
Ben Skeggs27d50302011-10-06 12:46:40 +1000786 ret = nouveau_display_create(dev);
Ben Skeggse88efe02010-07-09 10:56:08 +1000787 if (ret)
Ben Skeggs1575b362011-07-04 11:55:39 +1000788 goto out_irq;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000789
Ben Skeggs10b461e2011-08-02 19:29:37 +1000790 nouveau_backlight_init(dev);
Ben Skeggs7d3a7662012-02-01 15:17:07 +1000791 nouveau_pm_init(dev);
Ben Skeggs10b461e2011-08-02 19:29:37 +1000792
Ben Skeggsc61205b2012-03-23 09:10:22 +1000793 if (dev_priv->eng[NVOBJ_ENGINE_GR]) {
Ben Skeggs48aca132012-03-18 00:40:41 +1000794 ret = nouveau_card_channel_init(dev);
Francisco Jerez0c6c1c22010-09-22 00:58:54 +0200795 if (ret)
Ben Skeggs5e120f62012-04-30 13:55:29 +1000796 goto out_pm;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000797 }
798
Ben Skeggs1575b362011-07-04 11:55:39 +1000799 if (dev->mode_config.num_crtc) {
Ben Skeggsf62b27d2011-11-09 15:18:47 +1000800 ret = nouveau_display_init(dev);
Ben Skeggs1575b362011-07-04 11:55:39 +1000801 if (ret)
802 goto out_chan;
803
804 nouveau_fbcon_init(dev);
Ben Skeggs1575b362011-07-04 11:55:39 +1000805 }
806
Ben Skeggs6ee73862009-12-11 19:24:15 +1000807 return 0;
Marcin Kościelnickic5804be2009-12-14 20:58:39 +0000808
Ben Skeggs1575b362011-07-04 11:55:39 +1000809out_chan:
Ben Skeggs48aca132012-03-18 00:40:41 +1000810 nouveau_card_channel_fini(dev);
Ben Skeggs7d3a7662012-02-01 15:17:07 +1000811out_pm:
812 nouveau_pm_fini(dev);
Ben Skeggs10b461e2011-08-02 19:29:37 +1000813 nouveau_backlight_exit(dev);
Ben Skeggs27d50302011-10-06 12:46:40 +1000814 nouveau_display_destroy(dev);
Marcin Kościelnickic5804be2009-12-14 20:58:39 +0000815out_irq:
Ben Skeggs35fa2f22010-10-21 14:07:03 +1000816 nouveau_irq_fini(dev);
Ben Skeggs6dfdd7a2011-03-31 15:40:43 +1000817out_engine:
Ben Skeggsaba99a82011-05-25 14:48:50 +1000818 if (!dev_priv->noaccel) {
Ben Skeggs6dfdd7a2011-03-31 15:40:43 +1000819 for (e = e - 1; e >= 0; e--) {
Ben Skeggs2703c212011-04-01 09:50:18 +1000820 if (!dev_priv->eng[e])
821 continue;
Ben Skeggs6c320fe2011-07-20 11:22:33 +1000822 dev_priv->eng[e]->fini(dev, e, false);
Ben Skeggs2703c212011-04-01 09:50:18 +1000823 dev_priv->eng[e]->destroy(dev,e );
Ben Skeggs6dfdd7a2011-03-31 15:40:43 +1000824 }
825 }
Ben Skeggsfbd2895e2010-09-01 15:24:34 +1000826 nouveau_mem_gart_fini(dev);
Ben Skeggs24f246a2011-06-10 13:36:08 +1000827out_ttmvram:
828 nouveau_mem_vram_fini(dev);
Marcin Kościelnickic5804be2009-12-14 20:58:39 +0000829out_instmem:
830 engine->instmem.takedown(dev);
Ben Skeggsfbd2895e2010-09-01 15:24:34 +1000831out_gpuobj:
832 nouveau_gpuobj_takedown(dev);
Ben Skeggs668b6c02011-12-15 10:43:03 +1000833out_gpio:
834 nouveau_gpio_destroy(dev);
Ben Skeggsfbd2895e2010-09-01 15:24:34 +1000835out_vram:
Ben Skeggs24f246a2011-06-10 13:36:08 +1000836 engine->vram.takedown(dev);
Ben Skeggs668b6c02011-12-15 10:43:03 +1000837out_fb:
838 engine->fb.takedown(dev);
839out_timer:
840 engine->timer.takedown(dev);
841out_mc:
842 engine->mc.takedown(dev);
Marcin Kościelnickic5804be2009-12-14 20:58:39 +0000843out_bios:
844 nouveau_bios_takedown(dev);
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200845out_display_early:
846 engine->display.late_takedown(dev);
Marcin Kościelnickic5804be2009-12-14 20:58:39 +0000847out:
Andreas Heider5c5ed6e2012-05-21 00:14:51 +0100848 vga_switcheroo_unregister_client(dev->pdev);
Marcin Kościelnickic5804be2009-12-14 20:58:39 +0000849 vga_client_register(dev->pdev, NULL, NULL, NULL);
850 return ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000851}
852
853static void nouveau_card_takedown(struct drm_device *dev)
854{
855 struct drm_nouveau_private *dev_priv = dev->dev_private;
856 struct nouveau_engine *engine = &dev_priv->engine;
Ben Skeggs6dfdd7a2011-03-31 15:40:43 +1000857 int e;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000858
Ben Skeggs1575b362011-07-04 11:55:39 +1000859 if (dev->mode_config.num_crtc) {
Ben Skeggs1575b362011-07-04 11:55:39 +1000860 nouveau_fbcon_fini(dev);
Ben Skeggsf62b27d2011-11-09 15:18:47 +1000861 nouveau_display_fini(dev);
Ben Skeggs1575b362011-07-04 11:55:39 +1000862 }
Ben Skeggs06b75e32011-06-08 18:29:12 +1000863
Ben Skeggs48aca132012-03-18 00:40:41 +1000864 nouveau_card_channel_fini(dev);
Ben Skeggs7d3a7662012-02-01 15:17:07 +1000865 nouveau_pm_fini(dev);
Ben Skeggs10b461e2011-08-02 19:29:37 +1000866 nouveau_backlight_exit(dev);
Ben Skeggs27d50302011-10-06 12:46:40 +1000867 nouveau_display_destroy(dev);
Ben Skeggs06b75e32011-06-08 18:29:12 +1000868
Ben Skeggsaba99a82011-05-25 14:48:50 +1000869 if (!dev_priv->noaccel) {
Ben Skeggs6dfdd7a2011-03-31 15:40:43 +1000870 for (e = NVOBJ_ENGINE_NR - 1; e >= 0; e--) {
871 if (dev_priv->eng[e]) {
Ben Skeggs6c320fe2011-07-20 11:22:33 +1000872 dev_priv->eng[e]->fini(dev, e, false);
Ben Skeggs6dfdd7a2011-03-31 15:40:43 +1000873 dev_priv->eng[e]->destroy(dev,e );
874 }
875 }
Ben Skeggsb6d3d872010-06-07 15:38:27 +1000876 }
Ben Skeggsb6d3d872010-06-07 15:38:27 +1000877
Jimmy Rentz97666102011-04-17 16:15:09 -0400878 if (dev_priv->vga_ram) {
879 nouveau_bo_unpin(dev_priv->vga_ram);
880 nouveau_bo_ref(NULL, &dev_priv->vga_ram);
881 }
882
Ben Skeggsb6d3d872010-06-07 15:38:27 +1000883 mutex_lock(&dev->struct_mutex);
884 ttm_bo_clean_mm(&dev_priv->ttm.bdev, TTM_PL_VRAM);
885 ttm_bo_clean_mm(&dev_priv->ttm.bdev, TTM_PL_TT);
886 mutex_unlock(&dev->struct_mutex);
Ben Skeggsfbd2895e2010-09-01 15:24:34 +1000887 nouveau_mem_gart_fini(dev);
Ben Skeggs24f246a2011-06-10 13:36:08 +1000888 nouveau_mem_vram_fini(dev);
Ben Skeggsb6d3d872010-06-07 15:38:27 +1000889
Ben Skeggsb6d3d872010-06-07 15:38:27 +1000890 engine->instmem.takedown(dev);
Ben Skeggsfbd2895e2010-09-01 15:24:34 +1000891 nouveau_gpuobj_takedown(dev);
Ben Skeggsb6d3d872010-06-07 15:38:27 +1000892
Ben Skeggs668b6c02011-12-15 10:43:03 +1000893 nouveau_gpio_destroy(dev);
894 engine->vram.takedown(dev);
895 engine->fb.takedown(dev);
896 engine->timer.takedown(dev);
897 engine->mc.takedown(dev);
898
Ben Skeggsb6d3d872010-06-07 15:38:27 +1000899 nouveau_bios_takedown(dev);
Ben Skeggs668b6c02011-12-15 10:43:03 +1000900 engine->display.late_takedown(dev);
901
902 nouveau_irq_fini(dev);
Ben Skeggsb6d3d872010-06-07 15:38:27 +1000903
Andreas Heider5c5ed6e2012-05-21 00:14:51 +0100904 vga_switcheroo_unregister_client(dev->pdev);
Ben Skeggsb6d3d872010-06-07 15:38:27 +1000905 vga_client_register(dev->pdev, NULL, NULL, NULL);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000906}
907
Ben Skeggs3f0a68d2011-05-31 11:11:28 +1000908int
909nouveau_open(struct drm_device *dev, struct drm_file *file_priv)
910{
Ben Skeggsfe32b162011-06-03 10:07:08 +1000911 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs3f0a68d2011-05-31 11:11:28 +1000912 struct nouveau_fpriv *fpriv;
Ben Skeggse41f26e2011-06-07 15:35:37 +1000913 int ret;
Ben Skeggs3f0a68d2011-05-31 11:11:28 +1000914
915 fpriv = kzalloc(sizeof(*fpriv), GFP_KERNEL);
916 if (unlikely(!fpriv))
917 return -ENOMEM;
918
919 spin_lock_init(&fpriv->lock);
Ben Skeggse8a863c2011-06-01 19:18:48 +1000920 INIT_LIST_HEAD(&fpriv->channels);
921
Ben Skeggse41f26e2011-06-07 15:35:37 +1000922 if (dev_priv->card_type == NV_50) {
923 ret = nouveau_vm_new(dev, 0, (1ULL << 40), 0x0020000000ULL,
924 &fpriv->vm);
925 if (ret) {
926 kfree(fpriv);
927 return ret;
928 }
929 } else
930 if (dev_priv->card_type >= NV_C0) {
Ben Skeggs5de80372011-06-08 18:17:41 +1000931 ret = nouveau_vm_new(dev, 0, (1ULL << 40), 0x0008000000ULL,
932 &fpriv->vm);
933 if (ret) {
934 kfree(fpriv);
935 return ret;
936 }
Ben Skeggse41f26e2011-06-07 15:35:37 +1000937 }
Ben Skeggsfe32b162011-06-03 10:07:08 +1000938
Ben Skeggs3f0a68d2011-05-31 11:11:28 +1000939 file_priv->driver_priv = fpriv;
940 return 0;
941}
942
Ben Skeggs6ee73862009-12-11 19:24:15 +1000943/* here a client dies, release the stuff that was allocated for its
944 * file_priv */
945void nouveau_preclose(struct drm_device *dev, struct drm_file *file_priv)
946{
947 nouveau_channel_cleanup(dev, file_priv);
948}
949
Ben Skeggs3f0a68d2011-05-31 11:11:28 +1000950void
951nouveau_postclose(struct drm_device *dev, struct drm_file *file_priv)
952{
953 struct nouveau_fpriv *fpriv = nouveau_fpriv(file_priv);
Ben Skeggsfe32b162011-06-03 10:07:08 +1000954 nouveau_vm_ref(NULL, &fpriv->vm, NULL);
Ben Skeggs3f0a68d2011-05-31 11:11:28 +1000955 kfree(fpriv);
956}
957
Ben Skeggs6ee73862009-12-11 19:24:15 +1000958/* first module load, setup the mmio/fb mapping */
959/* KMS: we need mmio at load time, not when the first drm client opens. */
960int nouveau_firstopen(struct drm_device *dev)
961{
962 return 0;
963}
964
965/* if we have an OF card, copy vbios to RAMIN */
966static void nouveau_OF_copy_vbios_to_ramin(struct drm_device *dev)
967{
968#if defined(__powerpc__)
969 int size, i;
970 const uint32_t *bios;
971 struct device_node *dn = pci_device_to_OF_node(dev->pdev);
972 if (!dn) {
973 NV_INFO(dev, "Unable to get the OF node\n");
974 return;
975 }
976
977 bios = of_get_property(dn, "NVDA,BMP", &size);
978 if (bios) {
979 for (i = 0; i < size; i += 4)
980 nv_wi32(dev, i, bios[i/4]);
981 NV_INFO(dev, "OF bios successfully copied (%d bytes)\n", size);
982 } else {
983 NV_INFO(dev, "Unable to get the OF bios\n");
984 }
985#endif
986}
987
Marcin Slusarz06415c52010-05-16 17:29:56 +0200988static struct apertures_struct *nouveau_get_apertures(struct drm_device *dev)
989{
990 struct pci_dev *pdev = dev->pdev;
991 struct apertures_struct *aper = alloc_apertures(3);
992 if (!aper)
993 return NULL;
994
995 aper->ranges[0].base = pci_resource_start(pdev, 1);
996 aper->ranges[0].size = pci_resource_len(pdev, 1);
997 aper->count = 1;
998
999 if (pci_resource_len(pdev, 2)) {
1000 aper->ranges[aper->count].base = pci_resource_start(pdev, 2);
1001 aper->ranges[aper->count].size = pci_resource_len(pdev, 2);
1002 aper->count++;
1003 }
1004
1005 if (pci_resource_len(pdev, 3)) {
1006 aper->ranges[aper->count].base = pci_resource_start(pdev, 3);
1007 aper->ranges[aper->count].size = pci_resource_len(pdev, 3);
1008 aper->count++;
1009 }
1010
1011 return aper;
1012}
1013
1014static int nouveau_remove_conflicting_drivers(struct drm_device *dev)
1015{
1016 struct drm_nouveau_private *dev_priv = dev->dev_private;
Marcin Slusarz3b9676e2010-05-16 17:33:09 +02001017 bool primary = false;
Marcin Slusarz06415c52010-05-16 17:29:56 +02001018 dev_priv->apertures = nouveau_get_apertures(dev);
1019 if (!dev_priv->apertures)
1020 return -ENOMEM;
1021
Marcin Slusarz3b9676e2010-05-16 17:33:09 +02001022#ifdef CONFIG_X86
1023 primary = dev->pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW;
1024#endif
Emil Velikovf2129492011-03-19 23:31:52 +00001025
Marcin Slusarz3b9676e2010-05-16 17:33:09 +02001026 remove_conflicting_framebuffers(dev_priv->apertures, "nouveaufb", primary);
Marcin Slusarz06415c52010-05-16 17:29:56 +02001027 return 0;
1028}
1029
Ben Skeggs94580292012-07-06 12:14:00 +10001030void *
1031nouveau_newpriv(struct drm_device *dev)
1032{
1033 struct drm_nouveau_private *dev_priv = dev->dev_private;
1034 return dev_priv->newpriv;
1035}
1036
Ben Skeggs6ee73862009-12-11 19:24:15 +10001037int nouveau_load(struct drm_device *dev, unsigned long flags)
1038{
1039 struct drm_nouveau_private *dev_priv;
Ben Skeggs2f5394c2012-03-12 15:55:43 +10001040 uint32_t reg0 = ~0, strap;
Ben Skeggscd0b0722010-06-01 15:56:22 +10001041 int ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001042
1043 dev_priv = kzalloc(sizeof(*dev_priv), GFP_KERNEL);
Dan Carpentera0d069e2010-07-30 17:04:32 +02001044 if (!dev_priv) {
1045 ret = -ENOMEM;
1046 goto err_out;
1047 }
Ben Skeggs94580292012-07-06 12:14:00 +10001048 dev_priv->newpriv = dev->dev_private;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001049 dev->dev_private = dev_priv;
1050 dev_priv->dev = dev;
1051
1052 dev_priv->flags = flags & NOUVEAU_FLAGS;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001053
1054 NV_DEBUG(dev, "vendor: 0x%X device: 0x%X class: 0x%X\n",
1055 dev->pci_vendor, dev->pci_device, dev->pdev->class);
1056
Ben Skeggs586c55f2012-07-09 14:14:48 +10001057 /* determine chipset and derive architecture from it */
1058 reg0 = nv_rd32(dev, NV03_PMC_BOOT_0);
1059 if ((reg0 & 0x0f000000) > 0) {
1060 dev_priv->chipset = (reg0 & 0xff00000) >> 20;
1061 switch (dev_priv->chipset & 0xf0) {
1062 case 0x10:
1063 case 0x20:
1064 case 0x30:
1065 dev_priv->card_type = dev_priv->chipset & 0xf0;
1066 break;
1067 case 0x40:
1068 case 0x60:
1069 dev_priv->card_type = NV_40;
1070 break;
1071 case 0x50:
1072 case 0x80:
1073 case 0x90:
1074 case 0xa0:
1075 dev_priv->card_type = NV_50;
1076 break;
1077 case 0xc0:
1078 dev_priv->card_type = NV_C0;
1079 break;
1080 case 0xd0:
1081 dev_priv->card_type = NV_D0;
1082 break;
1083 case 0xe0:
1084 dev_priv->card_type = NV_E0;
1085 break;
1086 default:
1087 break;
Ben Skeggs2f5394c2012-03-12 15:55:43 +10001088 }
Ben Skeggs586c55f2012-07-09 14:14:48 +10001089 } else
1090 if ((reg0 & 0xff00fff0) == 0x20004000) {
1091 if (reg0 & 0x00f00000)
1092 dev_priv->chipset = 0x05;
1093 else
1094 dev_priv->chipset = 0x04;
1095 dev_priv->card_type = NV_04;
Ben Skeggs2f5394c2012-03-12 15:55:43 +10001096 }
1097
1098 if (!dev_priv->card_type) {
1099 NV_ERROR(dev, "unsupported chipset 0x%08x\n", reg0);
1100 ret = -EINVAL;
1101 goto err_priv;
1102 }
1103
Ben Skeggs42eddbd2012-05-09 20:17:07 +10001104 NV_INFO(dev, "Detected an NV%02x generation card (0x%08x)\n",
Ben Skeggs2f5394c2012-03-12 15:55:43 +10001105 dev_priv->card_type, reg0);
Ben Skeggs6ee73862009-12-11 19:24:15 +10001106
Ben Skeggsf2cbe462011-07-21 15:39:06 +10001107 /* determine frequency of timing crystal */
1108 strap = nv_rd32(dev, 0x101000);
1109 if ( dev_priv->chipset < 0x17 ||
1110 (dev_priv->chipset >= 0x20 && dev_priv->chipset <= 0x25))
1111 strap &= 0x00000040;
1112 else
1113 strap &= 0x00400040;
1114
1115 switch (strap) {
1116 case 0x00000000: dev_priv->crystal = 13500; break;
1117 case 0x00000040: dev_priv->crystal = 14318; break;
1118 case 0x00400000: dev_priv->crystal = 27000; break;
1119 case 0x00400040: dev_priv->crystal = 25000; break;
1120 }
1121
1122 NV_DEBUG(dev, "crystal freq: %dKHz\n", dev_priv->crystal);
1123
Ben Skeggsaba99a82011-05-25 14:48:50 +10001124 /* Determine whether we'll attempt acceleration or not, some
1125 * cards are disabled by default here due to them being known
1126 * non-functional, or never been tested due to lack of hw.
1127 */
1128 dev_priv->noaccel = !!nouveau_noaccel;
1129 if (nouveau_noaccel == -1) {
1130 switch (dev_priv->chipset) {
Ben Skeggs06784092011-07-11 15:57:54 +10001131 case 0xd9: /* known broken */
Ben Skeggsab394542012-03-13 13:05:13 +10001132 case 0xe4: /* needs binary driver firmware */
1133 case 0xe7: /* needs binary driver firmware */
Ben Skeggsad830d22011-05-27 16:18:10 +10001134 NV_INFO(dev, "acceleration disabled by default, pass "
1135 "noaccel=0 to force enable\n");
Ben Skeggsaba99a82011-05-25 14:48:50 +10001136 dev_priv->noaccel = true;
1137 break;
1138 default:
1139 dev_priv->noaccel = false;
1140 break;
1141 }
1142 }
1143
Ben Skeggscd0b0722010-06-01 15:56:22 +10001144 ret = nouveau_remove_conflicting_drivers(dev);
1145 if (ret)
Ben Skeggs586c55f2012-07-09 14:14:48 +10001146 goto err_priv;
Marcin Slusarz06415c52010-05-16 17:29:56 +02001147
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001148 /* Map PRAMIN BAR, or on older cards, the aperture within BAR0 */
Ben Skeggs6ee73862009-12-11 19:24:15 +10001149 if (dev_priv->card_type >= NV_40) {
1150 int ramin_bar = 2;
1151 if (pci_resource_len(dev->pdev, ramin_bar) == 0)
1152 ramin_bar = 3;
1153
1154 dev_priv->ramin_size = pci_resource_len(dev->pdev, ramin_bar);
Ben Skeggs6d696302010-06-02 10:16:24 +10001155 dev_priv->ramin =
1156 ioremap(pci_resource_start(dev->pdev, ramin_bar),
Ben Skeggs6ee73862009-12-11 19:24:15 +10001157 dev_priv->ramin_size);
1158 if (!dev_priv->ramin) {
Marcin Slusarzff920bf2011-08-22 23:28:56 +02001159 NV_ERROR(dev, "Failed to map PRAMIN BAR\n");
Dan Carpentera0d069e2010-07-30 17:04:32 +02001160 ret = -ENOMEM;
Ben Skeggs586c55f2012-07-09 14:14:48 +10001161 goto err_priv;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001162 }
Ben Skeggs6d696302010-06-02 10:16:24 +10001163 } else {
Ben Skeggs6ee73862009-12-11 19:24:15 +10001164 dev_priv->ramin_size = 1 * 1024 * 1024;
Ben Skeggs586c55f2012-07-09 14:14:48 +10001165 dev_priv->ramin = ioremap(pci_resource_start(dev->pdev, 0),
Ben Skeggs6d696302010-06-02 10:16:24 +10001166 dev_priv->ramin_size);
Ben Skeggs6ee73862009-12-11 19:24:15 +10001167 if (!dev_priv->ramin) {
1168 NV_ERROR(dev, "Failed to map BAR0 PRAMIN.\n");
Dan Carpentera0d069e2010-07-30 17:04:32 +02001169 ret = -ENOMEM;
Ben Skeggs586c55f2012-07-09 14:14:48 +10001170 goto err_priv;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001171 }
1172 }
1173
1174 nouveau_OF_copy_vbios_to_ramin(dev);
1175
1176 /* Special flags */
1177 if (dev->pci_device == 0x01a0)
1178 dev_priv->flags |= NV_NFORCE;
1179 else if (dev->pci_device == 0x01f0)
1180 dev_priv->flags |= NV_NFORCE2;
1181
1182 /* For kernel modesetting, init card now and bring up fbcon */
Ben Skeggscd0b0722010-06-01 15:56:22 +10001183 ret = nouveau_card_init(dev);
1184 if (ret)
Dan Carpentera0d069e2010-07-30 17:04:32 +02001185 goto err_ramin;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001186
1187 return 0;
Dan Carpentera0d069e2010-07-30 17:04:32 +02001188
1189err_ramin:
1190 iounmap(dev_priv->ramin);
Dan Carpentera0d069e2010-07-30 17:04:32 +02001191err_priv:
Ben Skeggs94580292012-07-06 12:14:00 +10001192 dev->dev_private = dev_priv->newpriv;
Dan Carpentera0d069e2010-07-30 17:04:32 +02001193 kfree(dev_priv);
Dan Carpentera0d069e2010-07-30 17:04:32 +02001194err_out:
1195 return ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001196}
1197
Ben Skeggs6ee73862009-12-11 19:24:15 +10001198void nouveau_lastclose(struct drm_device *dev)
1199{
Dave Airlie5ccb3772010-12-07 13:56:26 +10001200 vga_switcheroo_process_delayed_switch();
Ben Skeggs6ee73862009-12-11 19:24:15 +10001201}
1202
1203int nouveau_unload(struct drm_device *dev)
1204{
1205 struct drm_nouveau_private *dev_priv = dev->dev_private;
1206
Ben Skeggscd0b0722010-06-01 15:56:22 +10001207 nouveau_card_takedown(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +10001208
Ben Skeggs6ee73862009-12-11 19:24:15 +10001209 iounmap(dev_priv->ramin);
1210
Ben Skeggs94580292012-07-06 12:14:00 +10001211 dev->dev_private = dev_priv->newpriv;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001212 kfree(dev_priv);
Ben Skeggs6ee73862009-12-11 19:24:15 +10001213 return 0;
1214}
1215
Ben Skeggs6ee73862009-12-11 19:24:15 +10001216/* Wait until (value(reg) & mask) == val, up until timeout has hit */
Ben Skeggs12fb9522010-11-19 14:32:56 +10001217bool
1218nouveau_wait_eq(struct drm_device *dev, uint64_t timeout,
1219 uint32_t reg, uint32_t mask, uint32_t val)
Ben Skeggs6ee73862009-12-11 19:24:15 +10001220{
1221 struct drm_nouveau_private *dev_priv = dev->dev_private;
1222 struct nouveau_timer_engine *ptimer = &dev_priv->engine.timer;
1223 uint64_t start = ptimer->read(dev);
1224
1225 do {
1226 if ((nv_rd32(dev, reg) & mask) == val)
1227 return true;
1228 } while (ptimer->read(dev) - start < timeout);
1229
1230 return false;
1231}
1232
Ben Skeggs12fb9522010-11-19 14:32:56 +10001233/* Wait until (value(reg) & mask) != val, up until timeout has hit */
1234bool
1235nouveau_wait_ne(struct drm_device *dev, uint64_t timeout,
1236 uint32_t reg, uint32_t mask, uint32_t val)
1237{
1238 struct drm_nouveau_private *dev_priv = dev->dev_private;
1239 struct nouveau_timer_engine *ptimer = &dev_priv->engine.timer;
1240 uint64_t start = ptimer->read(dev);
1241
1242 do {
1243 if ((nv_rd32(dev, reg) & mask) != val)
1244 return true;
1245 } while (ptimer->read(dev) - start < timeout);
1246
1247 return false;
1248}
1249
Ben Skeggs78e29332011-06-18 16:27:24 +10001250/* Wait until cond(data) == true, up until timeout has hit */
1251bool
1252nouveau_wait_cb(struct drm_device *dev, u64 timeout,
1253 bool (*cond)(void *), void *data)
1254{
1255 struct drm_nouveau_private *dev_priv = dev->dev_private;
1256 struct nouveau_timer_engine *ptimer = &dev_priv->engine.timer;
1257 u64 start = ptimer->read(dev);
1258
1259 do {
1260 if (cond(data) == true)
1261 return true;
1262 } while (ptimer->read(dev) - start < timeout);
1263
1264 return false;
1265}
1266
Ben Skeggs6ee73862009-12-11 19:24:15 +10001267/* Waits for PGRAPH to go completely idle */
1268bool nouveau_wait_for_idle(struct drm_device *dev)
1269{
Francisco Jerez0541324a2010-10-18 16:15:15 +02001270 struct drm_nouveau_private *dev_priv = dev->dev_private;
1271 uint32_t mask = ~0;
1272
1273 if (dev_priv->card_type == NV_40)
1274 mask &= ~NV40_PGRAPH_STATUS_SYNC_STALL;
1275
1276 if (!nv_wait(dev, NV04_PGRAPH_STATUS, mask, 0)) {
Ben Skeggs6ee73862009-12-11 19:24:15 +10001277 NV_ERROR(dev, "PGRAPH idle timed out with status 0x%08x\n",
1278 nv_rd32(dev, NV04_PGRAPH_STATUS));
1279 return false;
1280 }
1281
1282 return true;
1283}
1284