blob: 1039e57d0aef74dac6112fdfe919583c11cd3dcb [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"
Ben Skeggs5e120f62012-04-30 13:55:29 +100042#include "nouveau_fence.h"
Ben Skeggs20abd162012-04-30 11:33:43 -050043#include "nouveau_software.h"
Ben Skeggs6ee73862009-12-11 19:24:15 +100044
Ben Skeggs6ee73862009-12-11 19:24:15 +100045static void nouveau_stub_takedown(struct drm_device *dev) {}
Ben Skeggsee2e0132010-07-26 09:28:25 +100046static int nouveau_stub_init(struct drm_device *dev) { return 0; }
Ben Skeggs6ee73862009-12-11 19:24:15 +100047
48static int nouveau_init_engine_ptrs(struct drm_device *dev)
49{
50 struct drm_nouveau_private *dev_priv = dev->dev_private;
51 struct nouveau_engine *engine = &dev_priv->engine;
52
53 switch (dev_priv->chipset & 0xf0) {
54 case 0x00:
55 engine->instmem.init = nv04_instmem_init;
56 engine->instmem.takedown = nv04_instmem_takedown;
57 engine->instmem.suspend = nv04_instmem_suspend;
58 engine->instmem.resume = nv04_instmem_resume;
Ben Skeggse41115d2010-11-01 11:45:02 +100059 engine->instmem.get = nv04_instmem_get;
60 engine->instmem.put = nv04_instmem_put;
61 engine->instmem.map = nv04_instmem_map;
62 engine->instmem.unmap = nv04_instmem_unmap;
Ben Skeggsf56cb862010-07-08 11:29:10 +100063 engine->instmem.flush = nv04_instmem_flush;
Ben Skeggs6ee73862009-12-11 19:24:15 +100064 engine->mc.init = nv04_mc_init;
65 engine->mc.takedown = nv04_mc_takedown;
66 engine->timer.init = nv04_timer_init;
67 engine->timer.read = nv04_timer_read;
68 engine->timer.takedown = nv04_timer_takedown;
69 engine->fb.init = nv04_fb_init;
70 engine->fb.takedown = nv04_fb_takedown;
Ben Skeggs6ee73862009-12-11 19:24:15 +100071 engine->fifo.channels = 16;
72 engine->fifo.init = nv04_fifo_init;
Ben Skeggs5178d402010-11-03 10:56:05 +100073 engine->fifo.takedown = nv04_fifo_fini;
Ben Skeggs6ee73862009-12-11 19:24:15 +100074 engine->fifo.disable = nv04_fifo_disable;
75 engine->fifo.enable = nv04_fifo_enable;
76 engine->fifo.reassign = nv04_fifo_reassign;
Francisco Jerez588d7d12009-12-13 20:07:42 +010077 engine->fifo.cache_pull = nv04_fifo_cache_pull;
Ben Skeggs6ee73862009-12-11 19:24:15 +100078 engine->fifo.channel_id = nv04_fifo_channel_id;
79 engine->fifo.create_context = nv04_fifo_create_context;
80 engine->fifo.destroy_context = nv04_fifo_destroy_context;
81 engine->fifo.load_context = nv04_fifo_load_context;
82 engine->fifo.unload_context = nv04_fifo_unload_context;
Francisco Jerezc88c2e02010-07-24 17:37:33 +020083 engine->display.early_init = nv04_display_early_init;
84 engine->display.late_takedown = nv04_display_late_takedown;
85 engine->display.create = nv04_display_create;
Francisco Jerezc88c2e02010-07-24 17:37:33 +020086 engine->display.destroy = nv04_display_destroy;
Ben Skeggs2a44e492011-11-09 11:36:33 +100087 engine->display.init = nv04_display_init;
88 engine->display.fini = nv04_display_fini;
Ben Skeggs36f13172011-10-27 10:24:12 +100089 engine->pm.clocks_get = nv04_pm_clocks_get;
90 engine->pm.clocks_pre = nv04_pm_clocks_pre;
91 engine->pm.clocks_set = nv04_pm_clocks_set;
Ben Skeggs7ad2d312011-12-11 00:30:05 +100092 engine->vram.init = nv04_fb_vram_init;
Ben Skeggs24f246a2011-06-10 13:36:08 +100093 engine->vram.takedown = nouveau_stub_takedown;
Ben Skeggs60d2a882010-12-06 15:28:54 +100094 engine->vram.flags_valid = nouveau_mem_flags_valid;
Ben Skeggs6ee73862009-12-11 19:24:15 +100095 break;
96 case 0x10:
97 engine->instmem.init = nv04_instmem_init;
98 engine->instmem.takedown = nv04_instmem_takedown;
99 engine->instmem.suspend = nv04_instmem_suspend;
100 engine->instmem.resume = nv04_instmem_resume;
Ben Skeggse41115d2010-11-01 11:45:02 +1000101 engine->instmem.get = nv04_instmem_get;
102 engine->instmem.put = nv04_instmem_put;
103 engine->instmem.map = nv04_instmem_map;
104 engine->instmem.unmap = nv04_instmem_unmap;
Ben Skeggsf56cb862010-07-08 11:29:10 +1000105 engine->instmem.flush = nv04_instmem_flush;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000106 engine->mc.init = nv04_mc_init;
107 engine->mc.takedown = nv04_mc_takedown;
108 engine->timer.init = nv04_timer_init;
109 engine->timer.read = nv04_timer_read;
110 engine->timer.takedown = nv04_timer_takedown;
111 engine->fb.init = nv10_fb_init;
112 engine->fb.takedown = nv10_fb_takedown;
Francisco Jereza5cf68b2010-10-24 16:14:41 +0200113 engine->fb.init_tile_region = nv10_fb_init_tile_region;
114 engine->fb.set_tile_region = nv10_fb_set_tile_region;
115 engine->fb.free_tile_region = nv10_fb_free_tile_region;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000116 engine->fifo.channels = 32;
117 engine->fifo.init = nv10_fifo_init;
Ben Skeggs5178d402010-11-03 10:56:05 +1000118 engine->fifo.takedown = nv04_fifo_fini;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000119 engine->fifo.disable = nv04_fifo_disable;
120 engine->fifo.enable = nv04_fifo_enable;
121 engine->fifo.reassign = nv04_fifo_reassign;
Francisco Jerez588d7d12009-12-13 20:07:42 +0100122 engine->fifo.cache_pull = nv04_fifo_cache_pull;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000123 engine->fifo.channel_id = nv10_fifo_channel_id;
124 engine->fifo.create_context = nv10_fifo_create_context;
Francisco Jerez3945e472010-10-18 03:53:39 +0200125 engine->fifo.destroy_context = nv04_fifo_destroy_context;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000126 engine->fifo.load_context = nv10_fifo_load_context;
127 engine->fifo.unload_context = nv10_fifo_unload_context;
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200128 engine->display.early_init = nv04_display_early_init;
129 engine->display.late_takedown = nv04_display_late_takedown;
130 engine->display.create = nv04_display_create;
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200131 engine->display.destroy = nv04_display_destroy;
Ben Skeggs2a44e492011-11-09 11:36:33 +1000132 engine->display.init = nv04_display_init;
133 engine->display.fini = nv04_display_fini;
Ben Skeggsa0b25632011-11-21 16:41:48 +1000134 engine->gpio.drive = nv10_gpio_drive;
135 engine->gpio.sense = nv10_gpio_sense;
Ben Skeggs36f13172011-10-27 10:24:12 +1000136 engine->pm.clocks_get = nv04_pm_clocks_get;
137 engine->pm.clocks_pre = nv04_pm_clocks_pre;
138 engine->pm.clocks_set = nv04_pm_clocks_set;
Ben Skeggs7ad2d312011-12-11 00:30:05 +1000139 if (dev_priv->chipset == 0x1a ||
140 dev_priv->chipset == 0x1f)
141 engine->vram.init = nv1a_fb_vram_init;
142 else
143 engine->vram.init = nv10_fb_vram_init;
Ben Skeggs24f246a2011-06-10 13:36:08 +1000144 engine->vram.takedown = nouveau_stub_takedown;
Ben Skeggs60d2a882010-12-06 15:28:54 +1000145 engine->vram.flags_valid = nouveau_mem_flags_valid;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000146 break;
147 case 0x20:
148 engine->instmem.init = nv04_instmem_init;
149 engine->instmem.takedown = nv04_instmem_takedown;
150 engine->instmem.suspend = nv04_instmem_suspend;
151 engine->instmem.resume = nv04_instmem_resume;
Ben Skeggse41115d2010-11-01 11:45:02 +1000152 engine->instmem.get = nv04_instmem_get;
153 engine->instmem.put = nv04_instmem_put;
154 engine->instmem.map = nv04_instmem_map;
155 engine->instmem.unmap = nv04_instmem_unmap;
Ben Skeggsf56cb862010-07-08 11:29:10 +1000156 engine->instmem.flush = nv04_instmem_flush;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000157 engine->mc.init = nv04_mc_init;
158 engine->mc.takedown = nv04_mc_takedown;
159 engine->timer.init = nv04_timer_init;
160 engine->timer.read = nv04_timer_read;
161 engine->timer.takedown = nv04_timer_takedown;
Ben Skeggsd81c19e2011-12-12 22:51:33 +1000162 engine->fb.init = nv20_fb_init;
163 engine->fb.takedown = nv20_fb_takedown;
164 engine->fb.init_tile_region = nv20_fb_init_tile_region;
165 engine->fb.set_tile_region = nv20_fb_set_tile_region;
166 engine->fb.free_tile_region = nv20_fb_free_tile_region;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000167 engine->fifo.channels = 32;
168 engine->fifo.init = nv10_fifo_init;
Ben Skeggs5178d402010-11-03 10:56:05 +1000169 engine->fifo.takedown = nv04_fifo_fini;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000170 engine->fifo.disable = nv04_fifo_disable;
171 engine->fifo.enable = nv04_fifo_enable;
172 engine->fifo.reassign = nv04_fifo_reassign;
Francisco Jerez588d7d12009-12-13 20:07:42 +0100173 engine->fifo.cache_pull = nv04_fifo_cache_pull;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000174 engine->fifo.channel_id = nv10_fifo_channel_id;
175 engine->fifo.create_context = nv10_fifo_create_context;
Francisco Jerez3945e472010-10-18 03:53:39 +0200176 engine->fifo.destroy_context = nv04_fifo_destroy_context;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000177 engine->fifo.load_context = nv10_fifo_load_context;
178 engine->fifo.unload_context = nv10_fifo_unload_context;
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200179 engine->display.early_init = nv04_display_early_init;
180 engine->display.late_takedown = nv04_display_late_takedown;
181 engine->display.create = nv04_display_create;
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200182 engine->display.destroy = nv04_display_destroy;
Ben Skeggs2a44e492011-11-09 11:36:33 +1000183 engine->display.init = nv04_display_init;
184 engine->display.fini = nv04_display_fini;
Ben Skeggsa0b25632011-11-21 16:41:48 +1000185 engine->gpio.drive = nv10_gpio_drive;
186 engine->gpio.sense = nv10_gpio_sense;
Ben Skeggs36f13172011-10-27 10:24:12 +1000187 engine->pm.clocks_get = nv04_pm_clocks_get;
188 engine->pm.clocks_pre = nv04_pm_clocks_pre;
189 engine->pm.clocks_set = nv04_pm_clocks_set;
Ben Skeggsd81c19e2011-12-12 22:51:33 +1000190 engine->vram.init = nv20_fb_vram_init;
Ben Skeggs24f246a2011-06-10 13:36:08 +1000191 engine->vram.takedown = nouveau_stub_takedown;
Ben Skeggs60d2a882010-12-06 15:28:54 +1000192 engine->vram.flags_valid = nouveau_mem_flags_valid;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000193 break;
194 case 0x30:
195 engine->instmem.init = nv04_instmem_init;
196 engine->instmem.takedown = nv04_instmem_takedown;
197 engine->instmem.suspend = nv04_instmem_suspend;
198 engine->instmem.resume = nv04_instmem_resume;
Ben Skeggse41115d2010-11-01 11:45:02 +1000199 engine->instmem.get = nv04_instmem_get;
200 engine->instmem.put = nv04_instmem_put;
201 engine->instmem.map = nv04_instmem_map;
202 engine->instmem.unmap = nv04_instmem_unmap;
Ben Skeggsf56cb862010-07-08 11:29:10 +1000203 engine->instmem.flush = nv04_instmem_flush;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000204 engine->mc.init = nv04_mc_init;
205 engine->mc.takedown = nv04_mc_takedown;
206 engine->timer.init = nv04_timer_init;
207 engine->timer.read = nv04_timer_read;
208 engine->timer.takedown = nv04_timer_takedown;
Francisco Jerez8bded182010-07-21 21:08:11 +0200209 engine->fb.init = nv30_fb_init;
210 engine->fb.takedown = nv30_fb_takedown;
Francisco Jereza5cf68b2010-10-24 16:14:41 +0200211 engine->fb.init_tile_region = nv30_fb_init_tile_region;
212 engine->fb.set_tile_region = nv10_fb_set_tile_region;
213 engine->fb.free_tile_region = nv30_fb_free_tile_region;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000214 engine->fifo.channels = 32;
215 engine->fifo.init = nv10_fifo_init;
Ben Skeggs5178d402010-11-03 10:56:05 +1000216 engine->fifo.takedown = nv04_fifo_fini;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000217 engine->fifo.disable = nv04_fifo_disable;
218 engine->fifo.enable = nv04_fifo_enable;
219 engine->fifo.reassign = nv04_fifo_reassign;
Francisco Jerez588d7d12009-12-13 20:07:42 +0100220 engine->fifo.cache_pull = nv04_fifo_cache_pull;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000221 engine->fifo.channel_id = nv10_fifo_channel_id;
222 engine->fifo.create_context = nv10_fifo_create_context;
Francisco Jerez3945e472010-10-18 03:53:39 +0200223 engine->fifo.destroy_context = nv04_fifo_destroy_context;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000224 engine->fifo.load_context = nv10_fifo_load_context;
225 engine->fifo.unload_context = nv10_fifo_unload_context;
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200226 engine->display.early_init = nv04_display_early_init;
227 engine->display.late_takedown = nv04_display_late_takedown;
228 engine->display.create = nv04_display_create;
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200229 engine->display.destroy = nv04_display_destroy;
Ben Skeggs2a44e492011-11-09 11:36:33 +1000230 engine->display.init = nv04_display_init;
231 engine->display.fini = nv04_display_fini;
Ben Skeggsa0b25632011-11-21 16:41:48 +1000232 engine->gpio.drive = nv10_gpio_drive;
233 engine->gpio.sense = nv10_gpio_sense;
Ben Skeggs36f13172011-10-27 10:24:12 +1000234 engine->pm.clocks_get = nv04_pm_clocks_get;
235 engine->pm.clocks_pre = nv04_pm_clocks_pre;
236 engine->pm.clocks_set = nv04_pm_clocks_set;
Ben Skeggs442b6262010-09-16 16:25:26 +1000237 engine->pm.voltage_get = nouveau_voltage_gpio_get;
238 engine->pm.voltage_set = nouveau_voltage_gpio_set;
Ben Skeggsd81c19e2011-12-12 22:51:33 +1000239 engine->vram.init = nv20_fb_vram_init;
Ben Skeggs24f246a2011-06-10 13:36:08 +1000240 engine->vram.takedown = nouveau_stub_takedown;
Ben Skeggs60d2a882010-12-06 15:28:54 +1000241 engine->vram.flags_valid = nouveau_mem_flags_valid;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000242 break;
243 case 0x40:
244 case 0x60:
245 engine->instmem.init = nv04_instmem_init;
246 engine->instmem.takedown = nv04_instmem_takedown;
247 engine->instmem.suspend = nv04_instmem_suspend;
248 engine->instmem.resume = nv04_instmem_resume;
Ben Skeggse41115d2010-11-01 11:45:02 +1000249 engine->instmem.get = nv04_instmem_get;
250 engine->instmem.put = nv04_instmem_put;
251 engine->instmem.map = nv04_instmem_map;
252 engine->instmem.unmap = nv04_instmem_unmap;
Ben Skeggsf56cb862010-07-08 11:29:10 +1000253 engine->instmem.flush = nv04_instmem_flush;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000254 engine->mc.init = nv40_mc_init;
255 engine->mc.takedown = nv40_mc_takedown;
256 engine->timer.init = nv04_timer_init;
257 engine->timer.read = nv04_timer_read;
258 engine->timer.takedown = nv04_timer_takedown;
259 engine->fb.init = nv40_fb_init;
260 engine->fb.takedown = nv40_fb_takedown;
Francisco Jereza5cf68b2010-10-24 16:14:41 +0200261 engine->fb.init_tile_region = nv30_fb_init_tile_region;
262 engine->fb.set_tile_region = nv40_fb_set_tile_region;
263 engine->fb.free_tile_region = nv30_fb_free_tile_region;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000264 engine->fifo.channels = 32;
265 engine->fifo.init = nv40_fifo_init;
Ben Skeggs5178d402010-11-03 10:56:05 +1000266 engine->fifo.takedown = nv04_fifo_fini;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000267 engine->fifo.disable = nv04_fifo_disable;
268 engine->fifo.enable = nv04_fifo_enable;
269 engine->fifo.reassign = nv04_fifo_reassign;
Francisco Jerez588d7d12009-12-13 20:07:42 +0100270 engine->fifo.cache_pull = nv04_fifo_cache_pull;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000271 engine->fifo.channel_id = nv10_fifo_channel_id;
272 engine->fifo.create_context = nv40_fifo_create_context;
Francisco Jerez3945e472010-10-18 03:53:39 +0200273 engine->fifo.destroy_context = nv04_fifo_destroy_context;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000274 engine->fifo.load_context = nv40_fifo_load_context;
275 engine->fifo.unload_context = nv40_fifo_unload_context;
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200276 engine->display.early_init = nv04_display_early_init;
277 engine->display.late_takedown = nv04_display_late_takedown;
278 engine->display.create = nv04_display_create;
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200279 engine->display.destroy = nv04_display_destroy;
Ben Skeggs2a44e492011-11-09 11:36:33 +1000280 engine->display.init = nv04_display_init;
281 engine->display.fini = nv04_display_fini;
Ben Skeggs47e5d5c2011-11-22 13:49:22 +1000282 engine->gpio.init = nv10_gpio_init;
283 engine->gpio.fini = nv10_gpio_fini;
Ben Skeggsa0b25632011-11-21 16:41:48 +1000284 engine->gpio.drive = nv10_gpio_drive;
285 engine->gpio.sense = nv10_gpio_sense;
Ben Skeggs47e5d5c2011-11-22 13:49:22 +1000286 engine->gpio.irq_enable = nv10_gpio_irq_enable;
Ben Skeggs1262a202011-07-18 15:15:34 +1000287 engine->pm.clocks_get = nv40_pm_clocks_get;
288 engine->pm.clocks_pre = nv40_pm_clocks_pre;
289 engine->pm.clocks_set = nv40_pm_clocks_set;
Ben Skeggs442b6262010-09-16 16:25:26 +1000290 engine->pm.voltage_get = nouveau_voltage_gpio_get;
291 engine->pm.voltage_set = nouveau_voltage_gpio_set;
Francisco Jerez8155cac2010-09-23 20:58:38 +0200292 engine->pm.temp_get = nv40_temp_get;
Ben Skeggs69346182011-09-17 02:11:39 +1000293 engine->pm.pwm_get = nv40_pm_pwm_get;
294 engine->pm.pwm_set = nv40_pm_pwm_set;
Ben Skeggsff92a6c2011-12-12 23:03:14 +1000295 engine->vram.init = nv40_fb_vram_init;
Ben Skeggs24f246a2011-06-10 13:36:08 +1000296 engine->vram.takedown = nouveau_stub_takedown;
Ben Skeggs60d2a882010-12-06 15:28:54 +1000297 engine->vram.flags_valid = nouveau_mem_flags_valid;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000298 break;
299 case 0x50:
300 case 0x80: /* gotta love NVIDIA's consistency.. */
301 case 0x90:
Ben Skeggsd9f61c22011-07-04 13:25:17 +1000302 case 0xa0:
Ben Skeggs6ee73862009-12-11 19:24:15 +1000303 engine->instmem.init = nv50_instmem_init;
304 engine->instmem.takedown = nv50_instmem_takedown;
305 engine->instmem.suspend = nv50_instmem_suspend;
306 engine->instmem.resume = nv50_instmem_resume;
Ben Skeggse41115d2010-11-01 11:45:02 +1000307 engine->instmem.get = nv50_instmem_get;
308 engine->instmem.put = nv50_instmem_put;
309 engine->instmem.map = nv50_instmem_map;
310 engine->instmem.unmap = nv50_instmem_unmap;
Ben Skeggs734ee832010-07-15 11:02:54 +1000311 if (dev_priv->chipset == 0x50)
312 engine->instmem.flush = nv50_instmem_flush;
313 else
314 engine->instmem.flush = nv84_instmem_flush;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000315 engine->mc.init = nv50_mc_init;
316 engine->mc.takedown = nv50_mc_takedown;
317 engine->timer.init = nv04_timer_init;
318 engine->timer.read = nv04_timer_read;
319 engine->timer.takedown = nv04_timer_takedown;
Marcin Koƛcielnicki304424e2010-03-01 00:18:39 +0000320 engine->fb.init = nv50_fb_init;
321 engine->fb.takedown = nv50_fb_takedown;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000322 engine->fifo.channels = 128;
323 engine->fifo.init = nv50_fifo_init;
324 engine->fifo.takedown = nv50_fifo_takedown;
325 engine->fifo.disable = nv04_fifo_disable;
326 engine->fifo.enable = nv04_fifo_enable;
327 engine->fifo.reassign = nv04_fifo_reassign;
328 engine->fifo.channel_id = nv50_fifo_channel_id;
329 engine->fifo.create_context = nv50_fifo_create_context;
330 engine->fifo.destroy_context = nv50_fifo_destroy_context;
331 engine->fifo.load_context = nv50_fifo_load_context;
332 engine->fifo.unload_context = nv50_fifo_unload_context;
Ben Skeggs56ac7472010-10-22 10:26:24 +1000333 engine->fifo.tlb_flush = nv50_fifo_tlb_flush;
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200334 engine->display.early_init = nv50_display_early_init;
335 engine->display.late_takedown = nv50_display_late_takedown;
336 engine->display.create = nv50_display_create;
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200337 engine->display.destroy = nv50_display_destroy;
Ben Skeggs2a44e492011-11-09 11:36:33 +1000338 engine->display.init = nv50_display_init;
339 engine->display.fini = nv50_display_fini;
Ben Skeggsee2e0132010-07-26 09:28:25 +1000340 engine->gpio.init = nv50_gpio_init;
Ben Skeggsa0b25632011-11-21 16:41:48 +1000341 engine->gpio.fini = nv50_gpio_fini;
342 engine->gpio.drive = nv50_gpio_drive;
343 engine->gpio.sense = nv50_gpio_sense;
Ben Skeggsee2e0132010-07-26 09:28:25 +1000344 engine->gpio.irq_enable = nv50_gpio_irq_enable;
Ben Skeggsfade7ad2010-09-27 11:18:14 +1000345 switch (dev_priv->chipset) {
Ben Skeggsbd2e5972010-10-19 20:06:01 +1000346 case 0x84:
347 case 0x86:
348 case 0x92:
349 case 0x94:
350 case 0x96:
351 case 0x98:
352 case 0xa0:
Ben Skeggs5f801982010-10-22 08:44:09 +1000353 case 0xaa:
354 case 0xac:
Ben Skeggsbd2e5972010-10-19 20:06:01 +1000355 case 0x50:
Ben Skeggsf3fbaf32011-10-26 09:11:02 +1000356 engine->pm.clocks_get = nv50_pm_clocks_get;
357 engine->pm.clocks_pre = nv50_pm_clocks_pre;
358 engine->pm.clocks_set = nv50_pm_clocks_set;
Ben Skeggsfade7ad2010-09-27 11:18:14 +1000359 break;
Ben Skeggsbd2e5972010-10-19 20:06:01 +1000360 default:
Ben Skeggsca94a712011-06-17 15:38:48 +1000361 engine->pm.clocks_get = nva3_pm_clocks_get;
362 engine->pm.clocks_pre = nva3_pm_clocks_pre;
363 engine->pm.clocks_set = nva3_pm_clocks_set;
Ben Skeggsbd2e5972010-10-19 20:06:01 +1000364 break;
Ben Skeggsfade7ad2010-09-27 11:18:14 +1000365 }
Ben Skeggs02c30ca2010-09-16 16:17:35 +1000366 engine->pm.voltage_get = nouveau_voltage_gpio_get;
367 engine->pm.voltage_set = nouveau_voltage_gpio_set;
Francisco Jerez8155cac2010-09-23 20:58:38 +0200368 if (dev_priv->chipset >= 0x84)
369 engine->pm.temp_get = nv84_temp_get;
370 else
371 engine->pm.temp_get = nv40_temp_get;
Ben Skeggs5a4267a2011-09-17 02:01:24 +1000372 engine->pm.pwm_get = nv50_pm_pwm_get;
373 engine->pm.pwm_set = nv50_pm_pwm_set;
Ben Skeggs60d2a882010-12-06 15:28:54 +1000374 engine->vram.init = nv50_vram_init;
Ben Skeggs24f246a2011-06-10 13:36:08 +1000375 engine->vram.takedown = nv50_vram_fini;
Ben Skeggs60d2a882010-12-06 15:28:54 +1000376 engine->vram.get = nv50_vram_new;
377 engine->vram.put = nv50_vram_del;
378 engine->vram.flags_valid = nv50_vram_flags_valid;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000379 break;
Ben Skeggsd9f61c22011-07-04 13:25:17 +1000380 case 0xc0:
Ben Skeggs4b223ee2010-08-03 10:00:56 +1000381 engine->instmem.init = nvc0_instmem_init;
382 engine->instmem.takedown = nvc0_instmem_takedown;
383 engine->instmem.suspend = nvc0_instmem_suspend;
384 engine->instmem.resume = nvc0_instmem_resume;
Ben Skeggs8984e042010-11-15 11:48:33 +1000385 engine->instmem.get = nv50_instmem_get;
386 engine->instmem.put = nv50_instmem_put;
387 engine->instmem.map = nv50_instmem_map;
388 engine->instmem.unmap = nv50_instmem_unmap;
389 engine->instmem.flush = nv84_instmem_flush;
Ben Skeggs4b223ee2010-08-03 10:00:56 +1000390 engine->mc.init = nv50_mc_init;
391 engine->mc.takedown = nv50_mc_takedown;
392 engine->timer.init = nv04_timer_init;
393 engine->timer.read = nv04_timer_read;
394 engine->timer.takedown = nv04_timer_takedown;
395 engine->fb.init = nvc0_fb_init;
396 engine->fb.takedown = nvc0_fb_takedown;
Ben Skeggs4b223ee2010-08-03 10:00:56 +1000397 engine->fifo.channels = 128;
398 engine->fifo.init = nvc0_fifo_init;
399 engine->fifo.takedown = nvc0_fifo_takedown;
400 engine->fifo.disable = nvc0_fifo_disable;
401 engine->fifo.enable = nvc0_fifo_enable;
402 engine->fifo.reassign = nvc0_fifo_reassign;
403 engine->fifo.channel_id = nvc0_fifo_channel_id;
404 engine->fifo.create_context = nvc0_fifo_create_context;
405 engine->fifo.destroy_context = nvc0_fifo_destroy_context;
406 engine->fifo.load_context = nvc0_fifo_load_context;
407 engine->fifo.unload_context = nvc0_fifo_unload_context;
408 engine->display.early_init = nv50_display_early_init;
409 engine->display.late_takedown = nv50_display_late_takedown;
410 engine->display.create = nv50_display_create;
Ben Skeggs4b223ee2010-08-03 10:00:56 +1000411 engine->display.destroy = nv50_display_destroy;
Ben Skeggs2a44e492011-11-09 11:36:33 +1000412 engine->display.init = nv50_display_init;
413 engine->display.fini = nv50_display_fini;
Ben Skeggs4b223ee2010-08-03 10:00:56 +1000414 engine->gpio.init = nv50_gpio_init;
Ben Skeggsa0b25632011-11-21 16:41:48 +1000415 engine->gpio.fini = nv50_gpio_fini;
416 engine->gpio.drive = nv50_gpio_drive;
417 engine->gpio.sense = nv50_gpio_sense;
Ben Skeggs4b223ee2010-08-03 10:00:56 +1000418 engine->gpio.irq_enable = nv50_gpio_irq_enable;
Ben Skeggs8984e042010-11-15 11:48:33 +1000419 engine->vram.init = nvc0_vram_init;
Ben Skeggs24f246a2011-06-10 13:36:08 +1000420 engine->vram.takedown = nv50_vram_fini;
Ben Skeggs8984e042010-11-15 11:48:33 +1000421 engine->vram.get = nvc0_vram_new;
422 engine->vram.put = nv50_vram_del;
423 engine->vram.flags_valid = nvc0_vram_flags_valid;
Martin Peres74cfad12011-05-12 22:40:47 +0200424 engine->pm.temp_get = nv84_temp_get;
Ben Skeggs354d0782011-06-19 01:44:36 +1000425 engine->pm.clocks_get = nvc0_pm_clocks_get;
Ben Skeggs045da4e2011-10-29 00:22:49 +1000426 engine->pm.clocks_pre = nvc0_pm_clocks_pre;
427 engine->pm.clocks_set = nvc0_pm_clocks_set;
Ben Skeggs3c71c232011-06-09 17:34:02 +1000428 engine->pm.voltage_get = nouveau_voltage_gpio_get;
Ben Skeggsda1dc4c2011-06-10 12:07:09 +1000429 engine->pm.voltage_set = nouveau_voltage_gpio_set;
Ben Skeggs5a4267a2011-09-17 02:01:24 +1000430 engine->pm.pwm_get = nv50_pm_pwm_get;
431 engine->pm.pwm_set = nv50_pm_pwm_set;
Ben Skeggs4b223ee2010-08-03 10:00:56 +1000432 break;
Ben Skeggsd9f61c22011-07-04 13:25:17 +1000433 case 0xd0:
434 engine->instmem.init = nvc0_instmem_init;
435 engine->instmem.takedown = nvc0_instmem_takedown;
436 engine->instmem.suspend = nvc0_instmem_suspend;
437 engine->instmem.resume = nvc0_instmem_resume;
438 engine->instmem.get = nv50_instmem_get;
439 engine->instmem.put = nv50_instmem_put;
440 engine->instmem.map = nv50_instmem_map;
441 engine->instmem.unmap = nv50_instmem_unmap;
442 engine->instmem.flush = nv84_instmem_flush;
443 engine->mc.init = nv50_mc_init;
444 engine->mc.takedown = nv50_mc_takedown;
445 engine->timer.init = nv04_timer_init;
446 engine->timer.read = nv04_timer_read;
447 engine->timer.takedown = nv04_timer_takedown;
448 engine->fb.init = nvc0_fb_init;
449 engine->fb.takedown = nvc0_fb_takedown;
450 engine->fifo.channels = 128;
451 engine->fifo.init = nvc0_fifo_init;
452 engine->fifo.takedown = nvc0_fifo_takedown;
453 engine->fifo.disable = nvc0_fifo_disable;
454 engine->fifo.enable = nvc0_fifo_enable;
455 engine->fifo.reassign = nvc0_fifo_reassign;
456 engine->fifo.channel_id = nvc0_fifo_channel_id;
457 engine->fifo.create_context = nvc0_fifo_create_context;
458 engine->fifo.destroy_context = nvc0_fifo_destroy_context;
459 engine->fifo.load_context = nvc0_fifo_load_context;
460 engine->fifo.unload_context = nvc0_fifo_unload_context;
461 engine->display.early_init = nouveau_stub_init;
462 engine->display.late_takedown = nouveau_stub_takedown;
Ben Skeggs26f6d882011-07-04 16:25:18 +1000463 engine->display.create = nvd0_display_create;
Ben Skeggs26f6d882011-07-04 16:25:18 +1000464 engine->display.destroy = nvd0_display_destroy;
Ben Skeggs2a44e492011-11-09 11:36:33 +1000465 engine->display.init = nvd0_display_init;
466 engine->display.fini = nvd0_display_fini;
Ben Skeggsd7f81722011-07-03 02:57:35 +1000467 engine->gpio.init = nv50_gpio_init;
Ben Skeggsa0b25632011-11-21 16:41:48 +1000468 engine->gpio.fini = nv50_gpio_fini;
469 engine->gpio.drive = nvd0_gpio_drive;
470 engine->gpio.sense = nvd0_gpio_sense;
Ben Skeggsd7f81722011-07-03 02:57:35 +1000471 engine->gpio.irq_enable = nv50_gpio_irq_enable;
Ben Skeggsd9f61c22011-07-04 13:25:17 +1000472 engine->vram.init = nvc0_vram_init;
473 engine->vram.takedown = nv50_vram_fini;
474 engine->vram.get = nvc0_vram_new;
475 engine->vram.put = nv50_vram_del;
476 engine->vram.flags_valid = nvc0_vram_flags_valid;
Martin Peres61091832011-10-22 01:40:40 +0200477 engine->pm.temp_get = nv84_temp_get;
Ben Skeggs4784e4a2011-07-04 14:06:07 +1000478 engine->pm.clocks_get = nvc0_pm_clocks_get;
Ben Skeggs045da4e2011-10-29 00:22:49 +1000479 engine->pm.clocks_pre = nvc0_pm_clocks_pre;
480 engine->pm.clocks_set = nvc0_pm_clocks_set;
Ben Skeggs4784e4a2011-07-04 14:06:07 +1000481 engine->pm.voltage_get = nouveau_voltage_gpio_get;
482 engine->pm.voltage_set = nouveau_voltage_gpio_set;
Ben Skeggsd9f61c22011-07-04 13:25:17 +1000483 break;
Ben Skeggs68455a42012-03-04 14:47:55 +1000484 case 0xe0:
485 engine->instmem.init = nvc0_instmem_init;
486 engine->instmem.takedown = nvc0_instmem_takedown;
487 engine->instmem.suspend = nvc0_instmem_suspend;
488 engine->instmem.resume = nvc0_instmem_resume;
489 engine->instmem.get = nv50_instmem_get;
490 engine->instmem.put = nv50_instmem_put;
491 engine->instmem.map = nv50_instmem_map;
492 engine->instmem.unmap = nv50_instmem_unmap;
493 engine->instmem.flush = nv84_instmem_flush;
494 engine->mc.init = nv50_mc_init;
495 engine->mc.takedown = nv50_mc_takedown;
496 engine->timer.init = nv04_timer_init;
497 engine->timer.read = nv04_timer_read;
498 engine->timer.takedown = nv04_timer_takedown;
499 engine->fb.init = nvc0_fb_init;
500 engine->fb.takedown = nvc0_fb_takedown;
Ben Skeggs5132f372012-03-08 10:41:45 +1000501 engine->fifo.channels = 4096;
502 engine->fifo.init = nve0_fifo_init;
503 engine->fifo.takedown = nve0_fifo_takedown;
Ben Skeggs68455a42012-03-04 14:47:55 +1000504 engine->fifo.disable = nvc0_fifo_disable;
505 engine->fifo.enable = nvc0_fifo_enable;
506 engine->fifo.reassign = nvc0_fifo_reassign;
Ben Skeggs5132f372012-03-08 10:41:45 +1000507 engine->fifo.channel_id = nve0_fifo_channel_id;
508 engine->fifo.create_context = nve0_fifo_create_context;
509 engine->fifo.destroy_context = nve0_fifo_destroy_context;
510 engine->fifo.load_context = nvc0_fifo_load_context;
511 engine->fifo.unload_context = nve0_fifo_unload_context;
Ben Skeggs68455a42012-03-04 14:47:55 +1000512 engine->display.early_init = nouveau_stub_init;
513 engine->display.late_takedown = nouveau_stub_takedown;
514 engine->display.create = nvd0_display_create;
515 engine->display.destroy = nvd0_display_destroy;
516 engine->display.init = nvd0_display_init;
517 engine->display.fini = nvd0_display_fini;
518 engine->gpio.init = nv50_gpio_init;
519 engine->gpio.fini = nv50_gpio_fini;
520 engine->gpio.drive = nvd0_gpio_drive;
521 engine->gpio.sense = nvd0_gpio_sense;
522 engine->gpio.irq_enable = nv50_gpio_irq_enable;
523 engine->vram.init = nvc0_vram_init;
524 engine->vram.takedown = nv50_vram_fini;
525 engine->vram.get = nvc0_vram_new;
526 engine->vram.put = nv50_vram_del;
527 engine->vram.flags_valid = nvc0_vram_flags_valid;
528 break;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000529 default:
530 NV_ERROR(dev, "NV%02x unsupported\n", dev_priv->chipset);
531 return 1;
532 }
533
Ben Skeggs03bc9672011-07-04 13:14:05 +1000534 /* headless mode */
535 if (nouveau_modeset == 2) {
536 engine->display.early_init = nouveau_stub_init;
537 engine->display.late_takedown = nouveau_stub_takedown;
538 engine->display.create = nouveau_stub_init;
539 engine->display.init = nouveau_stub_init;
540 engine->display.destroy = nouveau_stub_takedown;
541 }
542
Ben Skeggs6ee73862009-12-11 19:24:15 +1000543 return 0;
544}
545
546static unsigned int
547nouveau_vga_set_decode(void *priv, bool state)
548{
Marcin Koƛcielnicki9967b942010-02-08 00:20:17 +0000549 struct drm_device *dev = priv;
550 struct drm_nouveau_private *dev_priv = dev->dev_private;
551
552 if (dev_priv->chipset >= 0x40)
553 nv_wr32(dev, 0x88054, state);
554 else
555 nv_wr32(dev, 0x1854, state);
556
Ben Skeggs6ee73862009-12-11 19:24:15 +1000557 if (state)
558 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
559 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
560 else
561 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
562}
563
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000564static void nouveau_switcheroo_set_state(struct pci_dev *pdev,
565 enum vga_switcheroo_state state)
566{
Dave Airliefbf81762010-06-01 09:09:06 +1000567 struct drm_device *dev = pci_get_drvdata(pdev);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000568 pm_message_t pmm = { .event = PM_EVENT_SUSPEND };
569 if (state == VGA_SWITCHEROO_ON) {
570 printk(KERN_ERR "VGA switcheroo: switched nouveau on\n");
Dave Airlie5bcf7192010-12-07 09:20:40 +1000571 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000572 nouveau_pci_resume(pdev);
Dave Airliefbf81762010-06-01 09:09:06 +1000573 drm_kms_helper_poll_enable(dev);
Dave Airlie5bcf7192010-12-07 09:20:40 +1000574 dev->switch_power_state = DRM_SWITCH_POWER_ON;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000575 } else {
576 printk(KERN_ERR "VGA switcheroo: switched nouveau off\n");
Dave Airlie5bcf7192010-12-07 09:20:40 +1000577 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
Dave Airliefbf81762010-06-01 09:09:06 +1000578 drm_kms_helper_poll_disable(dev);
Peter Lekensteynd0992302011-12-17 12:54:04 +0100579 nouveau_switcheroo_optimus_dsm();
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000580 nouveau_pci_suspend(pdev, pmm);
Dave Airlie5bcf7192010-12-07 09:20:40 +1000581 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000582 }
583}
584
Dave Airlie8d608aa2010-12-07 08:57:57 +1000585static void nouveau_switcheroo_reprobe(struct pci_dev *pdev)
586{
587 struct drm_device *dev = pci_get_drvdata(pdev);
588 nouveau_fbcon_output_poll_changed(dev);
589}
590
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000591static bool nouveau_switcheroo_can_switch(struct pci_dev *pdev)
592{
593 struct drm_device *dev = pci_get_drvdata(pdev);
594 bool can_switch;
595
596 spin_lock(&dev->count_lock);
597 can_switch = (dev->open_count == 0);
598 spin_unlock(&dev->count_lock);
599 return can_switch;
600}
601
Ben Skeggs48aca132012-03-18 00:40:41 +1000602static void
603nouveau_card_channel_fini(struct drm_device *dev)
604{
605 struct drm_nouveau_private *dev_priv = dev->dev_private;
606
607 if (dev_priv->channel)
608 nouveau_channel_put_unlocked(&dev_priv->channel);
609}
610
611static int
612nouveau_card_channel_init(struct drm_device *dev)
613{
614 struct drm_nouveau_private *dev_priv = dev->dev_private;
615 struct nouveau_channel *chan;
616 int ret, oclass;
617
618 ret = nouveau_channel_alloc(dev, &chan, NULL, NvDmaFB, NvDmaTT);
619 dev_priv->channel = chan;
620 if (ret)
621 return ret;
622
623 mutex_unlock(&dev_priv->channel->mutex);
624
625 if (dev_priv->card_type <= NV_50) {
626 if (dev_priv->card_type < NV_50)
627 oclass = 0x0039;
628 else
629 oclass = 0x5039;
630
631 ret = nouveau_gpuobj_gr_new(chan, NvM2MF, oclass);
632 if (ret)
633 goto error;
634
635 ret = nouveau_notifier_alloc(chan, NvNotify0, 32, 0xfe0, 0x1000,
636 &chan->m2mf_ntfy);
637 if (ret)
638 goto error;
639
640 ret = RING_SPACE(chan, 6);
641 if (ret)
642 goto error;
643
Ben Skeggs6d597022012-04-01 21:09:13 +1000644 BEGIN_NV04(chan, NvSubM2MF, NV_MEMORY_TO_MEMORY_FORMAT_NAME, 1);
Ben Skeggs48aca132012-03-18 00:40:41 +1000645 OUT_RING (chan, NvM2MF);
Ben Skeggs6d597022012-04-01 21:09:13 +1000646 BEGIN_NV04(chan, NvSubM2MF, NV_MEMORY_TO_MEMORY_FORMAT_DMA_NOTIFY, 3);
Ben Skeggs48aca132012-03-18 00:40:41 +1000647 OUT_RING (chan, NvNotify0);
648 OUT_RING (chan, chan->vram_handle);
649 OUT_RING (chan, chan->gart_handle);
650 } else
Ben Skeggs4a206ff2012-03-27 14:41:04 +1000651 if (dev_priv->card_type <= NV_D0) {
Ben Skeggs48aca132012-03-18 00:40:41 +1000652 ret = nouveau_gpuobj_gr_new(chan, 0x9039, 0x9039);
653 if (ret)
654 goto error;
655
656 ret = RING_SPACE(chan, 2);
657 if (ret)
658 goto error;
659
Ben Skeggs6d597022012-04-01 21:09:13 +1000660 BEGIN_NVC0(chan, NvSubM2MF, 0x0000, 1);
Ben Skeggs48aca132012-03-18 00:40:41 +1000661 OUT_RING (chan, 0x00009039);
Ben Skeggsc6b7e892012-03-20 14:36:04 +1000662 } else
663 if (dev_priv->card_type <= NV_E0) {
664 /* not used, but created to get a graph context */
665 ret = nouveau_gpuobj_gr_new(chan, 0xa040, 0xa040);
666 if (ret)
667 goto error;
668
669 /* bind strange copy engine to subchannel 4 (fixed...) */
670 ret = RING_SPACE(chan, 2);
671 if (ret)
672 goto error;
673
Ben Skeggs6d597022012-04-01 21:09:13 +1000674 BEGIN_NVC0(chan, NvSubCopy, 0x0000, 1);
Ben Skeggsc6b7e892012-03-20 14:36:04 +1000675 OUT_RING (chan, 0x0000a0b5);
Ben Skeggs48aca132012-03-18 00:40:41 +1000676 }
677
678 FIRE_RING (chan);
679error:
680 if (ret)
681 nouveau_card_channel_fini(dev);
682 return ret;
683}
684
Takashi Iwai26ec6852012-05-11 07:51:17 +0200685static const struct vga_switcheroo_client_ops nouveau_switcheroo_ops = {
686 .set_gpu_state = nouveau_switcheroo_set_state,
687 .reprobe = nouveau_switcheroo_reprobe,
688 .can_switch = nouveau_switcheroo_can_switch,
689};
690
Ben Skeggs6ee73862009-12-11 19:24:15 +1000691int
692nouveau_card_init(struct drm_device *dev)
693{
694 struct drm_nouveau_private *dev_priv = dev->dev_private;
695 struct nouveau_engine *engine;
Ben Skeggseea55c82011-04-18 08:57:51 +1000696 int ret, e = 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000697
Ben Skeggs6ee73862009-12-11 19:24:15 +1000698 vga_client_register(dev->pdev, dev, NULL, nouveau_vga_set_decode);
Takashi Iwai26ec6852012-05-11 07:51:17 +0200699 vga_switcheroo_register_client(dev->pdev, &nouveau_switcheroo_ops);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000700
701 /* Initialise internal driver API hooks */
702 ret = nouveau_init_engine_ptrs(dev);
703 if (ret)
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000704 goto out;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000705 engine = &dev_priv->engine;
Ben Skeggscff5c132010-10-06 16:16:59 +1000706 spin_lock_init(&dev_priv->channels.lock);
Francisco Jereza5cf68b2010-10-24 16:14:41 +0200707 spin_lock_init(&dev_priv->tile.lock);
Maarten Maathuisff9e5272010-02-01 20:58:27 +0100708 spin_lock_init(&dev_priv->context_switch_lock);
Ben Skeggs04eb34a2011-04-06 13:28:35 +1000709 spin_lock_init(&dev_priv->vm_lock);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000710
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200711 /* Make the CRTCs and I2C buses accessible */
712 ret = engine->display.early_init(dev);
713 if (ret)
714 goto out;
715
Ben Skeggs6ee73862009-12-11 19:24:15 +1000716 /* Parse BIOS tables / Run init tables if card not POSTed */
Ben Skeggscd0b0722010-06-01 15:56:22 +1000717 ret = nouveau_bios_init(dev);
718 if (ret)
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200719 goto out_display_early;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000720
Ben Skeggs4c5df492011-10-28 10:59:45 +1000721 /* workaround an odd issue on nvc1 by disabling the device's
722 * nosnoop capability. hopefully won't cause issues until a
723 * better fix is found - assuming there is one...
724 */
725 if (dev_priv->chipset == 0xc1) {
726 nv_mask(dev, 0x00088080, 0x00000800, 0x00000000);
727 }
728
Ben Skeggs668b6c02011-12-15 10:43:03 +1000729 /* PMC */
730 ret = engine->mc.init(dev);
Ben Skeggsa76fb4e2010-03-18 09:45:20 +1000731 if (ret)
732 goto out_bios;
733
Ben Skeggs668b6c02011-12-15 10:43:03 +1000734 /* PTIMER */
735 ret = engine->timer.init(dev);
736 if (ret)
737 goto out_mc;
738
739 /* PFB */
740 ret = engine->fb.init(dev);
741 if (ret)
742 goto out_timer;
743
744 ret = engine->vram.init(dev);
745 if (ret)
746 goto out_fb;
747
748 /* PGPIO */
749 ret = nouveau_gpio_create(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000750 if (ret)
Ben Skeggsfbd2895e2010-09-01 15:24:34 +1000751 goto out_vram;
752
Ben Skeggs668b6c02011-12-15 10:43:03 +1000753 ret = nouveau_gpuobj_init(dev);
754 if (ret)
755 goto out_gpio;
756
Ben Skeggsfbd2895e2010-09-01 15:24:34 +1000757 ret = engine->instmem.init(dev);
758 if (ret)
759 goto out_gpuobj;
760
Ben Skeggs24f246a2011-06-10 13:36:08 +1000761 ret = nouveau_mem_vram_init(dev);
Ben Skeggsfbd2895e2010-09-01 15:24:34 +1000762 if (ret)
763 goto out_instmem;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000764
Ben Skeggs24f246a2011-06-10 13:36:08 +1000765 ret = nouveau_mem_gart_init(dev);
766 if (ret)
767 goto out_ttmvram;
768
Ben Skeggsaba99a82011-05-25 14:48:50 +1000769 if (!dev_priv->noaccel) {
Ben Skeggs18b54c42011-05-25 15:22:33 +1000770 switch (dev_priv->card_type) {
771 case NV_04:
Ben Skeggs5e120f62012-04-30 13:55:29 +1000772 nv04_fence_create(dev);
773 break;
774 case NV_10:
775 case NV_20:
776 case NV_30:
777 case NV_40:
778 case NV_50:
779 if (dev_priv->chipset < 0x84)
780 nv10_fence_create(dev);
781 else
782 nv84_fence_create(dev);
783 break;
784 case NV_C0:
785 case NV_D0:
786 case NV_E0:
787 nvc0_fence_create(dev);
788 break;
789 default:
790 break;
791 }
792
793 switch (dev_priv->card_type) {
794 case NV_04:
Ben Skeggs20abd162012-04-30 11:33:43 -0500795 case NV_10:
796 case NV_20:
797 case NV_30:
798 case NV_40:
799 nv04_software_create(dev);
800 break;
801 case NV_50:
802 nv50_software_create(dev);
803 break;
804 case NV_C0:
805 case NV_D0:
806 case NV_E0:
807 nvc0_software_create(dev);
808 break;
809 default:
810 break;
811 }
812
813 switch (dev_priv->card_type) {
814 case NV_04:
Ben Skeggs18b54c42011-05-25 15:22:33 +1000815 nv04_graph_create(dev);
816 break;
817 case NV_10:
818 nv10_graph_create(dev);
819 break;
820 case NV_20:
821 case NV_30:
822 nv20_graph_create(dev);
823 break;
824 case NV_40:
825 nv40_graph_create(dev);
826 break;
827 case NV_50:
828 nv50_graph_create(dev);
829 break;
830 case NV_C0:
Ben Skeggs06784092011-07-11 15:57:54 +1000831 case NV_D0:
Ben Skeggs18b54c42011-05-25 15:22:33 +1000832 nvc0_graph_create(dev);
833 break;
Ben Skeggsab394542012-03-13 13:05:13 +1000834 case NV_E0:
835 nve0_graph_create(dev);
836 break;
Ben Skeggs18b54c42011-05-25 15:22:33 +1000837 default:
Ben Skeggs7ff54412011-03-18 10:25:59 +1000838 break;
839 }
Ben Skeggs7ff54412011-03-18 10:25:59 +1000840
Ben Skeggs18b54c42011-05-25 15:22:33 +1000841 switch (dev_priv->chipset) {
842 case 0x84:
843 case 0x86:
844 case 0x92:
845 case 0x94:
846 case 0x96:
847 case 0xa0:
848 nv84_crypt_create(dev);
849 break;
Ben Skeggs8f27c542011-08-11 14:58:06 +1000850 case 0x98:
851 case 0xaa:
852 case 0xac:
853 nv98_crypt_create(dev);
854 break;
Ben Skeggs18b54c42011-05-25 15:22:33 +1000855 }
Ben Skeggsa02ccc72011-04-04 16:08:24 +1000856
Ben Skeggs18b54c42011-05-25 15:22:33 +1000857 switch (dev_priv->card_type) {
858 case NV_50:
859 switch (dev_priv->chipset) {
860 case 0xa3:
861 case 0xa5:
862 case 0xa8:
863 case 0xaf:
864 nva3_copy_create(dev);
865 break;
866 }
867 break;
868 case NV_C0:
869 nvc0_copy_create(dev, 0);
870 nvc0_copy_create(dev, 1);
871 break;
872 default:
873 break;
874 }
875
Ben Skeggs8f27c542011-08-11 14:58:06 +1000876 if (dev_priv->chipset >= 0xa3 || dev_priv->chipset == 0x98) {
877 nv84_bsp_create(dev);
878 nv84_vp_create(dev);
879 nv98_ppp_create(dev);
880 } else
881 if (dev_priv->chipset >= 0x84) {
882 nv50_mpeg_create(dev);
883 nv84_bsp_create(dev);
884 nv84_vp_create(dev);
885 } else
886 if (dev_priv->chipset >= 0x50) {
887 nv50_mpeg_create(dev);
888 } else
Ben Skeggs52d07332011-06-23 16:44:05 +1000889 if (dev_priv->card_type == NV_40 ||
890 dev_priv->chipset == 0x31 ||
891 dev_priv->chipset == 0x34 ||
Ben Skeggs8f27c542011-08-11 14:58:06 +1000892 dev_priv->chipset == 0x36) {
Ben Skeggs323dcac2011-06-23 16:21:21 +1000893 nv31_mpeg_create(dev);
Ben Skeggs8f27c542011-08-11 14:58:06 +1000894 }
Ben Skeggs18b54c42011-05-25 15:22:33 +1000895
Ben Skeggs6dfdd7a2011-03-31 15:40:43 +1000896 for (e = 0; e < NVOBJ_ENGINE_NR; e++) {
897 if (dev_priv->eng[e]) {
898 ret = dev_priv->eng[e]->init(dev, e);
899 if (ret)
900 goto out_engine;
901 }
902 }
903
Marcin Koƛcielnickia32ed692010-01-26 14:00:42 +0000904 /* PFIFO */
905 ret = engine->fifo.init(dev);
906 if (ret)
Ben Skeggsa82dd492011-04-01 13:56:05 +1000907 goto out_engine;
Marcin Koƛcielnickia32ed692010-01-26 14:00:42 +0000908 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000909
Ben Skeggs1575b362011-07-04 11:55:39 +1000910 ret = nouveau_irq_init(dev);
911 if (ret)
912 goto out_fifo;
913
Ben Skeggs27d50302011-10-06 12:46:40 +1000914 ret = nouveau_display_create(dev);
Ben Skeggse88efe02010-07-09 10:56:08 +1000915 if (ret)
Ben Skeggs1575b362011-07-04 11:55:39 +1000916 goto out_irq;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000917
Ben Skeggs10b461e2011-08-02 19:29:37 +1000918 nouveau_backlight_init(dev);
Ben Skeggs7d3a7662012-02-01 15:17:07 +1000919 nouveau_pm_init(dev);
Ben Skeggs10b461e2011-08-02 19:29:37 +1000920
Ben Skeggsc61205b2012-03-23 09:10:22 +1000921 if (dev_priv->eng[NVOBJ_ENGINE_GR]) {
Ben Skeggs48aca132012-03-18 00:40:41 +1000922 ret = nouveau_card_channel_init(dev);
Francisco Jerez0c6c1c22010-09-22 00:58:54 +0200923 if (ret)
Ben Skeggs5e120f62012-04-30 13:55:29 +1000924 goto out_pm;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000925 }
926
Ben Skeggs1575b362011-07-04 11:55:39 +1000927 if (dev->mode_config.num_crtc) {
Ben Skeggsf62b27d2011-11-09 15:18:47 +1000928 ret = nouveau_display_init(dev);
Ben Skeggs1575b362011-07-04 11:55:39 +1000929 if (ret)
930 goto out_chan;
931
932 nouveau_fbcon_init(dev);
Ben Skeggs1575b362011-07-04 11:55:39 +1000933 }
934
Ben Skeggs6ee73862009-12-11 19:24:15 +1000935 return 0;
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000936
Ben Skeggs1575b362011-07-04 11:55:39 +1000937out_chan:
Ben Skeggs48aca132012-03-18 00:40:41 +1000938 nouveau_card_channel_fini(dev);
Ben Skeggs7d3a7662012-02-01 15:17:07 +1000939out_pm:
940 nouveau_pm_fini(dev);
Ben Skeggs10b461e2011-08-02 19:29:37 +1000941 nouveau_backlight_exit(dev);
Ben Skeggs27d50302011-10-06 12:46:40 +1000942 nouveau_display_destroy(dev);
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000943out_irq:
Ben Skeggs35fa2f22010-10-21 14:07:03 +1000944 nouveau_irq_fini(dev);
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000945out_fifo:
Ben Skeggsaba99a82011-05-25 14:48:50 +1000946 if (!dev_priv->noaccel)
Marcin Koƛcielnickia32ed692010-01-26 14:00:42 +0000947 engine->fifo.takedown(dev);
Ben Skeggs6dfdd7a2011-03-31 15:40:43 +1000948out_engine:
Ben Skeggsaba99a82011-05-25 14:48:50 +1000949 if (!dev_priv->noaccel) {
Ben Skeggs6dfdd7a2011-03-31 15:40:43 +1000950 for (e = e - 1; e >= 0; e--) {
Ben Skeggs2703c212011-04-01 09:50:18 +1000951 if (!dev_priv->eng[e])
952 continue;
Ben Skeggs6c320fe2011-07-20 11:22:33 +1000953 dev_priv->eng[e]->fini(dev, e, false);
Ben Skeggs2703c212011-04-01 09:50:18 +1000954 dev_priv->eng[e]->destroy(dev,e );
Ben Skeggs6dfdd7a2011-03-31 15:40:43 +1000955 }
956 }
Ben Skeggsfbd2895e2010-09-01 15:24:34 +1000957 nouveau_mem_gart_fini(dev);
Ben Skeggs24f246a2011-06-10 13:36:08 +1000958out_ttmvram:
959 nouveau_mem_vram_fini(dev);
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000960out_instmem:
961 engine->instmem.takedown(dev);
Ben Skeggsfbd2895e2010-09-01 15:24:34 +1000962out_gpuobj:
963 nouveau_gpuobj_takedown(dev);
Ben Skeggs668b6c02011-12-15 10:43:03 +1000964out_gpio:
965 nouveau_gpio_destroy(dev);
Ben Skeggsfbd2895e2010-09-01 15:24:34 +1000966out_vram:
Ben Skeggs24f246a2011-06-10 13:36:08 +1000967 engine->vram.takedown(dev);
Ben Skeggs668b6c02011-12-15 10:43:03 +1000968out_fb:
969 engine->fb.takedown(dev);
970out_timer:
971 engine->timer.takedown(dev);
972out_mc:
973 engine->mc.takedown(dev);
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000974out_bios:
975 nouveau_bios_takedown(dev);
Francisco Jerezc88c2e02010-07-24 17:37:33 +0200976out_display_early:
977 engine->display.late_takedown(dev);
Marcin Koƛcielnickic5804be2009-12-14 20:58:39 +0000978out:
979 vga_client_register(dev->pdev, NULL, NULL, NULL);
980 return ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000981}
982
983static void nouveau_card_takedown(struct drm_device *dev)
984{
985 struct drm_nouveau_private *dev_priv = dev->dev_private;
986 struct nouveau_engine *engine = &dev_priv->engine;
Ben Skeggs6dfdd7a2011-03-31 15:40:43 +1000987 int e;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000988
Ben Skeggs1575b362011-07-04 11:55:39 +1000989 if (dev->mode_config.num_crtc) {
Ben Skeggs1575b362011-07-04 11:55:39 +1000990 nouveau_fbcon_fini(dev);
Ben Skeggsf62b27d2011-11-09 15:18:47 +1000991 nouveau_display_fini(dev);
Ben Skeggs1575b362011-07-04 11:55:39 +1000992 }
Ben Skeggs06b75e32011-06-08 18:29:12 +1000993
Ben Skeggs48aca132012-03-18 00:40:41 +1000994 nouveau_card_channel_fini(dev);
Ben Skeggs7d3a7662012-02-01 15:17:07 +1000995 nouveau_pm_fini(dev);
Ben Skeggs10b461e2011-08-02 19:29:37 +1000996 nouveau_backlight_exit(dev);
Ben Skeggs27d50302011-10-06 12:46:40 +1000997 nouveau_display_destroy(dev);
Ben Skeggs06b75e32011-06-08 18:29:12 +1000998
Ben Skeggsaba99a82011-05-25 14:48:50 +1000999 if (!dev_priv->noaccel) {
Ben Skeggsb6d3d872010-06-07 15:38:27 +10001000 engine->fifo.takedown(dev);
Ben Skeggs6dfdd7a2011-03-31 15:40:43 +10001001 for (e = NVOBJ_ENGINE_NR - 1; e >= 0; e--) {
1002 if (dev_priv->eng[e]) {
Ben Skeggs6c320fe2011-07-20 11:22:33 +10001003 dev_priv->eng[e]->fini(dev, e, false);
Ben Skeggs6dfdd7a2011-03-31 15:40:43 +10001004 dev_priv->eng[e]->destroy(dev,e );
1005 }
1006 }
Ben Skeggsb6d3d872010-06-07 15:38:27 +10001007 }
Ben Skeggsb6d3d872010-06-07 15:38:27 +10001008
Jimmy Rentz97666102011-04-17 16:15:09 -04001009 if (dev_priv->vga_ram) {
1010 nouveau_bo_unpin(dev_priv->vga_ram);
1011 nouveau_bo_ref(NULL, &dev_priv->vga_ram);
1012 }
1013
Ben Skeggsb6d3d872010-06-07 15:38:27 +10001014 mutex_lock(&dev->struct_mutex);
1015 ttm_bo_clean_mm(&dev_priv->ttm.bdev, TTM_PL_VRAM);
1016 ttm_bo_clean_mm(&dev_priv->ttm.bdev, TTM_PL_TT);
1017 mutex_unlock(&dev->struct_mutex);
Ben Skeggsfbd2895e2010-09-01 15:24:34 +10001018 nouveau_mem_gart_fini(dev);
Ben Skeggs24f246a2011-06-10 13:36:08 +10001019 nouveau_mem_vram_fini(dev);
Ben Skeggsb6d3d872010-06-07 15:38:27 +10001020
Ben Skeggsb6d3d872010-06-07 15:38:27 +10001021 engine->instmem.takedown(dev);
Ben Skeggsfbd2895e2010-09-01 15:24:34 +10001022 nouveau_gpuobj_takedown(dev);
Ben Skeggsb6d3d872010-06-07 15:38:27 +10001023
Ben Skeggs668b6c02011-12-15 10:43:03 +10001024 nouveau_gpio_destroy(dev);
1025 engine->vram.takedown(dev);
1026 engine->fb.takedown(dev);
1027 engine->timer.takedown(dev);
1028 engine->mc.takedown(dev);
1029
Ben Skeggsb6d3d872010-06-07 15:38:27 +10001030 nouveau_bios_takedown(dev);
Ben Skeggs668b6c02011-12-15 10:43:03 +10001031 engine->display.late_takedown(dev);
1032
1033 nouveau_irq_fini(dev);
Ben Skeggsb6d3d872010-06-07 15:38:27 +10001034
1035 vga_client_register(dev->pdev, NULL, NULL, NULL);
Ben Skeggs6ee73862009-12-11 19:24:15 +10001036}
1037
Ben Skeggs3f0a68d2011-05-31 11:11:28 +10001038int
1039nouveau_open(struct drm_device *dev, struct drm_file *file_priv)
1040{
Ben Skeggsfe32b162011-06-03 10:07:08 +10001041 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs3f0a68d2011-05-31 11:11:28 +10001042 struct nouveau_fpriv *fpriv;
Ben Skeggse41f26e2011-06-07 15:35:37 +10001043 int ret;
Ben Skeggs3f0a68d2011-05-31 11:11:28 +10001044
1045 fpriv = kzalloc(sizeof(*fpriv), GFP_KERNEL);
1046 if (unlikely(!fpriv))
1047 return -ENOMEM;
1048
1049 spin_lock_init(&fpriv->lock);
Ben Skeggse8a863c2011-06-01 19:18:48 +10001050 INIT_LIST_HEAD(&fpriv->channels);
1051
Ben Skeggse41f26e2011-06-07 15:35:37 +10001052 if (dev_priv->card_type == NV_50) {
1053 ret = nouveau_vm_new(dev, 0, (1ULL << 40), 0x0020000000ULL,
1054 &fpriv->vm);
1055 if (ret) {
1056 kfree(fpriv);
1057 return ret;
1058 }
1059 } else
1060 if (dev_priv->card_type >= NV_C0) {
Ben Skeggs5de80372011-06-08 18:17:41 +10001061 ret = nouveau_vm_new(dev, 0, (1ULL << 40), 0x0008000000ULL,
1062 &fpriv->vm);
1063 if (ret) {
1064 kfree(fpriv);
1065 return ret;
1066 }
Ben Skeggse41f26e2011-06-07 15:35:37 +10001067 }
Ben Skeggsfe32b162011-06-03 10:07:08 +10001068
Ben Skeggs3f0a68d2011-05-31 11:11:28 +10001069 file_priv->driver_priv = fpriv;
1070 return 0;
1071}
1072
Ben Skeggs6ee73862009-12-11 19:24:15 +10001073/* here a client dies, release the stuff that was allocated for its
1074 * file_priv */
1075void nouveau_preclose(struct drm_device *dev, struct drm_file *file_priv)
1076{
1077 nouveau_channel_cleanup(dev, file_priv);
1078}
1079
Ben Skeggs3f0a68d2011-05-31 11:11:28 +10001080void
1081nouveau_postclose(struct drm_device *dev, struct drm_file *file_priv)
1082{
1083 struct nouveau_fpriv *fpriv = nouveau_fpriv(file_priv);
Ben Skeggsfe32b162011-06-03 10:07:08 +10001084 nouveau_vm_ref(NULL, &fpriv->vm, NULL);
Ben Skeggs3f0a68d2011-05-31 11:11:28 +10001085 kfree(fpriv);
1086}
1087
Ben Skeggs6ee73862009-12-11 19:24:15 +10001088/* first module load, setup the mmio/fb mapping */
1089/* KMS: we need mmio at load time, not when the first drm client opens. */
1090int nouveau_firstopen(struct drm_device *dev)
1091{
1092 return 0;
1093}
1094
1095/* if we have an OF card, copy vbios to RAMIN */
1096static void nouveau_OF_copy_vbios_to_ramin(struct drm_device *dev)
1097{
1098#if defined(__powerpc__)
1099 int size, i;
1100 const uint32_t *bios;
1101 struct device_node *dn = pci_device_to_OF_node(dev->pdev);
1102 if (!dn) {
1103 NV_INFO(dev, "Unable to get the OF node\n");
1104 return;
1105 }
1106
1107 bios = of_get_property(dn, "NVDA,BMP", &size);
1108 if (bios) {
1109 for (i = 0; i < size; i += 4)
1110 nv_wi32(dev, i, bios[i/4]);
1111 NV_INFO(dev, "OF bios successfully copied (%d bytes)\n", size);
1112 } else {
1113 NV_INFO(dev, "Unable to get the OF bios\n");
1114 }
1115#endif
1116}
1117
Marcin Slusarz06415c52010-05-16 17:29:56 +02001118static struct apertures_struct *nouveau_get_apertures(struct drm_device *dev)
1119{
1120 struct pci_dev *pdev = dev->pdev;
1121 struct apertures_struct *aper = alloc_apertures(3);
1122 if (!aper)
1123 return NULL;
1124
1125 aper->ranges[0].base = pci_resource_start(pdev, 1);
1126 aper->ranges[0].size = pci_resource_len(pdev, 1);
1127 aper->count = 1;
1128
1129 if (pci_resource_len(pdev, 2)) {
1130 aper->ranges[aper->count].base = pci_resource_start(pdev, 2);
1131 aper->ranges[aper->count].size = pci_resource_len(pdev, 2);
1132 aper->count++;
1133 }
1134
1135 if (pci_resource_len(pdev, 3)) {
1136 aper->ranges[aper->count].base = pci_resource_start(pdev, 3);
1137 aper->ranges[aper->count].size = pci_resource_len(pdev, 3);
1138 aper->count++;
1139 }
1140
1141 return aper;
1142}
1143
1144static int nouveau_remove_conflicting_drivers(struct drm_device *dev)
1145{
1146 struct drm_nouveau_private *dev_priv = dev->dev_private;
Marcin Slusarz3b9676e2010-05-16 17:33:09 +02001147 bool primary = false;
Marcin Slusarz06415c52010-05-16 17:29:56 +02001148 dev_priv->apertures = nouveau_get_apertures(dev);
1149 if (!dev_priv->apertures)
1150 return -ENOMEM;
1151
Marcin Slusarz3b9676e2010-05-16 17:33:09 +02001152#ifdef CONFIG_X86
1153 primary = dev->pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW;
1154#endif
Emil Velikovf2129492011-03-19 23:31:52 +00001155
Marcin Slusarz3b9676e2010-05-16 17:33:09 +02001156 remove_conflicting_framebuffers(dev_priv->apertures, "nouveaufb", primary);
Marcin Slusarz06415c52010-05-16 17:29:56 +02001157 return 0;
1158}
1159
Ben Skeggs6ee73862009-12-11 19:24:15 +10001160int nouveau_load(struct drm_device *dev, unsigned long flags)
1161{
1162 struct drm_nouveau_private *dev_priv;
Ben Skeggs68455a42012-03-04 14:47:55 +10001163 unsigned long long offset, length;
Ben Skeggs2f5394c2012-03-12 15:55:43 +10001164 uint32_t reg0 = ~0, strap;
Ben Skeggscd0b0722010-06-01 15:56:22 +10001165 int ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001166
1167 dev_priv = kzalloc(sizeof(*dev_priv), GFP_KERNEL);
Dan Carpentera0d069e2010-07-30 17:04:32 +02001168 if (!dev_priv) {
1169 ret = -ENOMEM;
1170 goto err_out;
1171 }
Ben Skeggs6ee73862009-12-11 19:24:15 +10001172 dev->dev_private = dev_priv;
1173 dev_priv->dev = dev;
1174
Dave Airlie466e69b2011-12-19 11:15:29 +00001175 pci_set_master(dev->pdev);
1176
Ben Skeggs6ee73862009-12-11 19:24:15 +10001177 dev_priv->flags = flags & NOUVEAU_FLAGS;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001178
1179 NV_DEBUG(dev, "vendor: 0x%X device: 0x%X class: 0x%X\n",
1180 dev->pci_vendor, dev->pci_device, dev->pdev->class);
1181
Ben Skeggs2f5394c2012-03-12 15:55:43 +10001182 /* first up, map the start of mmio and determine the chipset */
1183 dev_priv->mmio = ioremap(pci_resource_start(dev->pdev, 0), PAGE_SIZE);
1184 if (dev_priv->mmio) {
1185#ifdef __BIG_ENDIAN
1186 /* put the card into big-endian mode if it's not */
1187 if (nv_rd32(dev, NV03_PMC_BOOT_1) != 0x01000001)
1188 nv_wr32(dev, NV03_PMC_BOOT_1, 0x01000001);
1189 DRM_MEMORYBARRIER();
1190#endif
1191
1192 /* determine chipset and derive architecture from it */
1193 reg0 = nv_rd32(dev, NV03_PMC_BOOT_0);
1194 if ((reg0 & 0x0f000000) > 0) {
1195 dev_priv->chipset = (reg0 & 0xff00000) >> 20;
1196 switch (dev_priv->chipset & 0xf0) {
1197 case 0x10:
1198 case 0x20:
1199 case 0x30:
1200 dev_priv->card_type = dev_priv->chipset & 0xf0;
1201 break;
1202 case 0x40:
1203 case 0x60:
1204 dev_priv->card_type = NV_40;
1205 break;
1206 case 0x50:
1207 case 0x80:
1208 case 0x90:
1209 case 0xa0:
1210 dev_priv->card_type = NV_50;
1211 break;
1212 case 0xc0:
1213 dev_priv->card_type = NV_C0;
1214 break;
1215 case 0xd0:
1216 dev_priv->card_type = NV_D0;
1217 break;
Ben Skeggs68455a42012-03-04 14:47:55 +10001218 case 0xe0:
1219 dev_priv->card_type = NV_E0;
1220 break;
Ben Skeggs2f5394c2012-03-12 15:55:43 +10001221 default:
1222 break;
1223 }
1224 } else
1225 if ((reg0 & 0xff00fff0) == 0x20004000) {
1226 if (reg0 & 0x00f00000)
1227 dev_priv->chipset = 0x05;
1228 else
1229 dev_priv->chipset = 0x04;
1230 dev_priv->card_type = NV_04;
1231 }
1232
1233 iounmap(dev_priv->mmio);
1234 }
1235
1236 if (!dev_priv->card_type) {
1237 NV_ERROR(dev, "unsupported chipset 0x%08x\n", reg0);
1238 ret = -EINVAL;
1239 goto err_priv;
1240 }
1241
1242 NV_INFO(dev, "Detected an NV%2x generation card (0x%08x)\n",
1243 dev_priv->card_type, reg0);
Ben Skeggs6ee73862009-12-11 19:24:15 +10001244
Ben Skeggs68455a42012-03-04 14:47:55 +10001245 /* map the mmio regs, limiting the amount to preserve vmap space */
1246 offset = pci_resource_start(dev->pdev, 0);
1247 length = pci_resource_len(dev->pdev, 0);
1248 if (dev_priv->card_type < NV_E0)
1249 length = min(length, (unsigned long long)0x00800000);
1250
1251 dev_priv->mmio = ioremap(offset, length);
Ben Skeggs6ee73862009-12-11 19:24:15 +10001252 if (!dev_priv->mmio) {
1253 NV_ERROR(dev, "Unable to initialize the mmio mapping. "
1254 "Please report your setup to " DRIVER_EMAIL "\n");
Dan Carpentera0d069e2010-07-30 17:04:32 +02001255 ret = -EINVAL;
Tejun Heod82f8e62011-01-26 17:49:18 +01001256 goto err_priv;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001257 }
Ben Skeggs68455a42012-03-04 14:47:55 +10001258 NV_DEBUG(dev, "regs mapped ok at 0x%llx\n", offset);
Ben Skeggs6ee73862009-12-11 19:24:15 +10001259
Ben Skeggsf2cbe462011-07-21 15:39:06 +10001260 /* determine frequency of timing crystal */
1261 strap = nv_rd32(dev, 0x101000);
1262 if ( dev_priv->chipset < 0x17 ||
1263 (dev_priv->chipset >= 0x20 && dev_priv->chipset <= 0x25))
1264 strap &= 0x00000040;
1265 else
1266 strap &= 0x00400040;
1267
1268 switch (strap) {
1269 case 0x00000000: dev_priv->crystal = 13500; break;
1270 case 0x00000040: dev_priv->crystal = 14318; break;
1271 case 0x00400000: dev_priv->crystal = 27000; break;
1272 case 0x00400040: dev_priv->crystal = 25000; break;
1273 }
1274
1275 NV_DEBUG(dev, "crystal freq: %dKHz\n", dev_priv->crystal);
1276
Ben Skeggsaba99a82011-05-25 14:48:50 +10001277 /* Determine whether we'll attempt acceleration or not, some
1278 * cards are disabled by default here due to them being known
1279 * non-functional, or never been tested due to lack of hw.
1280 */
1281 dev_priv->noaccel = !!nouveau_noaccel;
1282 if (nouveau_noaccel == -1) {
1283 switch (dev_priv->chipset) {
Ben Skeggs06784092011-07-11 15:57:54 +10001284 case 0xd9: /* known broken */
Ben Skeggsab394542012-03-13 13:05:13 +10001285 case 0xe4: /* needs binary driver firmware */
1286 case 0xe7: /* needs binary driver firmware */
Ben Skeggsad830d22011-05-27 16:18:10 +10001287 NV_INFO(dev, "acceleration disabled by default, pass "
1288 "noaccel=0 to force enable\n");
Ben Skeggsaba99a82011-05-25 14:48:50 +10001289 dev_priv->noaccel = true;
1290 break;
1291 default:
1292 dev_priv->noaccel = false;
1293 break;
1294 }
1295 }
1296
Ben Skeggscd0b0722010-06-01 15:56:22 +10001297 ret = nouveau_remove_conflicting_drivers(dev);
1298 if (ret)
Dan Carpentera0d069e2010-07-30 17:04:32 +02001299 goto err_mmio;
Marcin Slusarz06415c52010-05-16 17:29:56 +02001300
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001301 /* Map PRAMIN BAR, or on older cards, the aperture within BAR0 */
Ben Skeggs6ee73862009-12-11 19:24:15 +10001302 if (dev_priv->card_type >= NV_40) {
1303 int ramin_bar = 2;
1304 if (pci_resource_len(dev->pdev, ramin_bar) == 0)
1305 ramin_bar = 3;
1306
1307 dev_priv->ramin_size = pci_resource_len(dev->pdev, ramin_bar);
Ben Skeggs6d696302010-06-02 10:16:24 +10001308 dev_priv->ramin =
1309 ioremap(pci_resource_start(dev->pdev, ramin_bar),
Ben Skeggs6ee73862009-12-11 19:24:15 +10001310 dev_priv->ramin_size);
1311 if (!dev_priv->ramin) {
Marcin Slusarzff920bf2011-08-22 23:28:56 +02001312 NV_ERROR(dev, "Failed to map PRAMIN BAR\n");
Dan Carpentera0d069e2010-07-30 17:04:32 +02001313 ret = -ENOMEM;
1314 goto err_mmio;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001315 }
Ben Skeggs6d696302010-06-02 10:16:24 +10001316 } else {
Ben Skeggs6ee73862009-12-11 19:24:15 +10001317 dev_priv->ramin_size = 1 * 1024 * 1024;
Ben Skeggs68455a42012-03-04 14:47:55 +10001318 dev_priv->ramin = ioremap(offset + NV_RAMIN,
Ben Skeggs6d696302010-06-02 10:16:24 +10001319 dev_priv->ramin_size);
Ben Skeggs6ee73862009-12-11 19:24:15 +10001320 if (!dev_priv->ramin) {
1321 NV_ERROR(dev, "Failed to map BAR0 PRAMIN.\n");
Dan Carpentera0d069e2010-07-30 17:04:32 +02001322 ret = -ENOMEM;
1323 goto err_mmio;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001324 }
1325 }
1326
1327 nouveau_OF_copy_vbios_to_ramin(dev);
1328
1329 /* Special flags */
1330 if (dev->pci_device == 0x01a0)
1331 dev_priv->flags |= NV_NFORCE;
1332 else if (dev->pci_device == 0x01f0)
1333 dev_priv->flags |= NV_NFORCE2;
1334
1335 /* For kernel modesetting, init card now and bring up fbcon */
Ben Skeggscd0b0722010-06-01 15:56:22 +10001336 ret = nouveau_card_init(dev);
1337 if (ret)
Dan Carpentera0d069e2010-07-30 17:04:32 +02001338 goto err_ramin;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001339
1340 return 0;
Dan Carpentera0d069e2010-07-30 17:04:32 +02001341
1342err_ramin:
1343 iounmap(dev_priv->ramin);
1344err_mmio:
1345 iounmap(dev_priv->mmio);
Dan Carpentera0d069e2010-07-30 17:04:32 +02001346err_priv:
1347 kfree(dev_priv);
1348 dev->dev_private = NULL;
1349err_out:
1350 return ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001351}
1352
Ben Skeggs6ee73862009-12-11 19:24:15 +10001353void nouveau_lastclose(struct drm_device *dev)
1354{
Dave Airlie5ccb3772010-12-07 13:56:26 +10001355 vga_switcheroo_process_delayed_switch();
Ben Skeggs6ee73862009-12-11 19:24:15 +10001356}
1357
1358int nouveau_unload(struct drm_device *dev)
1359{
1360 struct drm_nouveau_private *dev_priv = dev->dev_private;
1361
Ben Skeggscd0b0722010-06-01 15:56:22 +10001362 nouveau_card_takedown(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +10001363
1364 iounmap(dev_priv->mmio);
1365 iounmap(dev_priv->ramin);
1366
1367 kfree(dev_priv);
1368 dev->dev_private = NULL;
1369 return 0;
1370}
1371
Ben Skeggs6ee73862009-12-11 19:24:15 +10001372int nouveau_ioctl_getparam(struct drm_device *dev, void *data,
1373 struct drm_file *file_priv)
1374{
1375 struct drm_nouveau_private *dev_priv = dev->dev_private;
1376 struct drm_nouveau_getparam *getparam = data;
1377
Ben Skeggs6ee73862009-12-11 19:24:15 +10001378 switch (getparam->param) {
1379 case NOUVEAU_GETPARAM_CHIPSET_ID:
1380 getparam->value = dev_priv->chipset;
1381 break;
1382 case NOUVEAU_GETPARAM_PCI_VENDOR:
1383 getparam->value = dev->pci_vendor;
1384 break;
1385 case NOUVEAU_GETPARAM_PCI_DEVICE:
1386 getparam->value = dev->pci_device;
1387 break;
1388 case NOUVEAU_GETPARAM_BUS_TYPE:
Dave Airlie8410ea32010-12-15 03:16:38 +10001389 if (drm_pci_device_is_agp(dev))
Ben Skeggs6ee73862009-12-11 19:24:15 +10001390 getparam->value = NV_AGP;
Jon Mason58b65422011-06-27 16:07:50 +00001391 else if (pci_is_pcie(dev->pdev))
Ben Skeggs6ee73862009-12-11 19:24:15 +10001392 getparam->value = NV_PCIE;
1393 else
1394 getparam->value = NV_PCI;
1395 break;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001396 case NOUVEAU_GETPARAM_FB_SIZE:
1397 getparam->value = dev_priv->fb_available_size;
1398 break;
1399 case NOUVEAU_GETPARAM_AGP_SIZE:
1400 getparam->value = dev_priv->gart_info.aper_size;
1401 break;
1402 case NOUVEAU_GETPARAM_VM_VRAM_BASE:
Ben Skeggs6d6c5a12010-11-16 10:17:53 +10001403 getparam->value = 0; /* deprecated */
Ben Skeggs6ee73862009-12-11 19:24:15 +10001404 break;
Marcin Koƛcielnicki7fc74f12010-05-23 11:36:04 +00001405 case NOUVEAU_GETPARAM_PTIMER_TIME:
1406 getparam->value = dev_priv->engine.timer.read(dev);
1407 break;
Francisco Jerezf13b3262010-10-10 06:01:08 +02001408 case NOUVEAU_GETPARAM_HAS_BO_USAGE:
1409 getparam->value = 1;
1410 break;
Francisco Jerez332b2422010-10-20 23:35:40 +02001411 case NOUVEAU_GETPARAM_HAS_PAGEFLIP:
Ben Skeggs3376ee32011-11-12 14:28:12 +10001412 getparam->value = 1;
Francisco Jerez332b2422010-10-20 23:35:40 +02001413 break;
Marcin Koƛcielnicki69c97002010-01-26 18:39:20 +00001414 case NOUVEAU_GETPARAM_GRAPH_UNITS:
1415 /* NV40 and NV50 versions are quite different, but register
1416 * address is the same. User is supposed to know the card
1417 * family anyway... */
1418 if (dev_priv->chipset >= 0x40) {
1419 getparam->value = nv_rd32(dev, NV40_PMC_GRAPH_UNITS);
1420 break;
1421 }
1422 /* FALLTHRU */
Ben Skeggs6ee73862009-12-11 19:24:15 +10001423 default:
Francisco Jerez1397b422010-10-12 03:17:43 +02001424 NV_DEBUG(dev, "unknown parameter %lld\n", getparam->param);
Ben Skeggs6ee73862009-12-11 19:24:15 +10001425 return -EINVAL;
1426 }
1427
1428 return 0;
1429}
1430
1431int
1432nouveau_ioctl_setparam(struct drm_device *dev, void *data,
1433 struct drm_file *file_priv)
1434{
1435 struct drm_nouveau_setparam *setparam = data;
1436
Ben Skeggs6ee73862009-12-11 19:24:15 +10001437 switch (setparam->param) {
1438 default:
Francisco Jerez1397b422010-10-12 03:17:43 +02001439 NV_DEBUG(dev, "unknown parameter %lld\n", setparam->param);
Ben Skeggs6ee73862009-12-11 19:24:15 +10001440 return -EINVAL;
1441 }
1442
1443 return 0;
1444}
1445
1446/* Wait until (value(reg) & mask) == val, up until timeout has hit */
Ben Skeggs12fb9522010-11-19 14:32:56 +10001447bool
1448nouveau_wait_eq(struct drm_device *dev, uint64_t timeout,
1449 uint32_t reg, uint32_t mask, uint32_t val)
Ben Skeggs6ee73862009-12-11 19:24:15 +10001450{
1451 struct drm_nouveau_private *dev_priv = dev->dev_private;
1452 struct nouveau_timer_engine *ptimer = &dev_priv->engine.timer;
1453 uint64_t start = ptimer->read(dev);
1454
1455 do {
1456 if ((nv_rd32(dev, reg) & mask) == val)
1457 return true;
1458 } while (ptimer->read(dev) - start < timeout);
1459
1460 return false;
1461}
1462
Ben Skeggs12fb9522010-11-19 14:32:56 +10001463/* Wait until (value(reg) & mask) != val, up until timeout has hit */
1464bool
1465nouveau_wait_ne(struct drm_device *dev, uint64_t timeout,
1466 uint32_t reg, uint32_t mask, uint32_t val)
1467{
1468 struct drm_nouveau_private *dev_priv = dev->dev_private;
1469 struct nouveau_timer_engine *ptimer = &dev_priv->engine.timer;
1470 uint64_t start = ptimer->read(dev);
1471
1472 do {
1473 if ((nv_rd32(dev, reg) & mask) != val)
1474 return true;
1475 } while (ptimer->read(dev) - start < timeout);
1476
1477 return false;
1478}
1479
Ben Skeggs78e29332011-06-18 16:27:24 +10001480/* Wait until cond(data) == true, up until timeout has hit */
1481bool
1482nouveau_wait_cb(struct drm_device *dev, u64 timeout,
1483 bool (*cond)(void *), void *data)
1484{
1485 struct drm_nouveau_private *dev_priv = dev->dev_private;
1486 struct nouveau_timer_engine *ptimer = &dev_priv->engine.timer;
1487 u64 start = ptimer->read(dev);
1488
1489 do {
1490 if (cond(data) == true)
1491 return true;
1492 } while (ptimer->read(dev) - start < timeout);
1493
1494 return false;
1495}
1496
Ben Skeggs6ee73862009-12-11 19:24:15 +10001497/* Waits for PGRAPH to go completely idle */
1498bool nouveau_wait_for_idle(struct drm_device *dev)
1499{
Francisco Jerez0541324a2010-10-18 16:15:15 +02001500 struct drm_nouveau_private *dev_priv = dev->dev_private;
1501 uint32_t mask = ~0;
1502
1503 if (dev_priv->card_type == NV_40)
1504 mask &= ~NV40_PGRAPH_STATUS_SYNC_STALL;
1505
1506 if (!nv_wait(dev, NV04_PGRAPH_STATUS, mask, 0)) {
Ben Skeggs6ee73862009-12-11 19:24:15 +10001507 NV_ERROR(dev, "PGRAPH idle timed out with status 0x%08x\n",
1508 nv_rd32(dev, NV04_PGRAPH_STATUS));
1509 return false;
1510 }
1511
1512 return true;
1513}
1514