blob: fa36b5368d9550d4d3d4100b7a671b0fcd5c59d2 [file] [log] [blame]
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001/*
2 * Copyright 2008 Advanced Micro Devices, Inc.
3 * Copyright 2008 Red Hat Inc.
4 * Copyright 2009 Jerome Glisse.
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 shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors: Dave Airlie
25 * Alex Deucher
26 * Jerome Glisse
27 */
28#include <linux/console.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Jerome Glisse771fe6b2009-06-05 14:42:42 +020030#include <drm/drmP.h>
31#include <drm/drm_crtc_helper.h>
32#include <drm/radeon_drm.h>
Dave Airlie28d52042009-09-21 14:33:58 +100033#include <linux/vgaarb.h>
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100034#include <linux/vga_switcheroo.h>
Matthew Garrettbcc65fd2011-08-08 16:21:16 +000035#include <linux/efi.h>
Jerome Glisse771fe6b2009-06-05 14:42:42 +020036#include "radeon_reg.h"
37#include "radeon.h"
Jerome Glisse771fe6b2009-06-05 14:42:42 +020038#include "atom.h"
39
Jerome Glisse1b5331d2010-04-12 20:21:53 +000040static const char radeon_family_name[][16] = {
41 "R100",
42 "RV100",
43 "RS100",
44 "RV200",
45 "RS200",
46 "R200",
47 "RV250",
48 "RS300",
49 "RV280",
50 "R300",
51 "R350",
52 "RV350",
53 "RV380",
54 "R420",
55 "R423",
56 "RV410",
57 "RS400",
58 "RS480",
59 "RS600",
60 "RS690",
61 "RS740",
62 "RV515",
63 "R520",
64 "RV530",
65 "RV560",
66 "RV570",
67 "R580",
68 "R600",
69 "RV610",
70 "RV630",
71 "RV670",
72 "RV620",
73 "RV635",
74 "RS780",
75 "RS880",
76 "RV770",
77 "RV730",
78 "RV710",
79 "RV740",
80 "CEDAR",
81 "REDWOOD",
82 "JUNIPER",
83 "CYPRESS",
84 "HEMLOCK",
Alex Deucherb08ebe72010-12-03 15:34:16 -050085 "PALM",
Alex Deucher4df64e62011-05-31 15:42:46 -040086 "SUMO",
87 "SUMO2",
Alex Deucher1fe18302011-01-06 21:19:12 -050088 "BARTS",
89 "TURKS",
90 "CAICOS",
Alex Deucherb7cfc9f2011-03-02 20:07:27 -050091 "CAYMAN",
Jerome Glisse1b5331d2010-04-12 20:21:53 +000092 "LAST",
93};
94
Jerome Glisse771fe6b2009-06-05 14:42:42 +020095/*
Michel Dänzerb1e3a6d2009-06-23 16:12:54 +020096 * Clear GPU surface registers.
97 */
Jerome Glisse3ce0a232009-09-08 10:10:24 +100098void radeon_surface_init(struct radeon_device *rdev)
Michel Dänzerb1e3a6d2009-06-23 16:12:54 +020099{
100 /* FIXME: check this out */
101 if (rdev->family < CHIP_R600) {
102 int i;
103
Dave Airlie550e2d92009-12-09 14:15:38 +1000104 for (i = 0; i < RADEON_GEM_MAX_SURFACES; i++) {
105 if (rdev->surface_regs[i].bo)
106 radeon_bo_get_surface_reg(rdev->surface_regs[i].bo);
107 else
108 radeon_clear_surface_reg(rdev, i);
Michel Dänzerb1e3a6d2009-06-23 16:12:54 +0200109 }
Dave Airliee024e112009-06-24 09:48:08 +1000110 /* enable surfaces */
111 WREG32(RADEON_SURFACE_CNTL, 0);
Michel Dänzerb1e3a6d2009-06-23 16:12:54 +0200112 }
113}
114
115/*
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200116 * GPU scratch registers helpers function.
117 */
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000118void radeon_scratch_init(struct radeon_device *rdev)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200119{
120 int i;
121
122 /* FIXME: check this out */
123 if (rdev->family < CHIP_R300) {
124 rdev->scratch.num_reg = 5;
125 } else {
126 rdev->scratch.num_reg = 7;
127 }
Alex Deucher724c80e2010-08-27 18:25:25 -0400128 rdev->scratch.reg_base = RADEON_SCRATCH_REG0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200129 for (i = 0; i < rdev->scratch.num_reg; i++) {
130 rdev->scratch.free[i] = true;
Alex Deucher724c80e2010-08-27 18:25:25 -0400131 rdev->scratch.reg[i] = rdev->scratch.reg_base + (i * 4);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200132 }
133}
134
135int radeon_scratch_get(struct radeon_device *rdev, uint32_t *reg)
136{
137 int i;
138
139 for (i = 0; i < rdev->scratch.num_reg; i++) {
140 if (rdev->scratch.free[i]) {
141 rdev->scratch.free[i] = false;
142 *reg = rdev->scratch.reg[i];
143 return 0;
144 }
145 }
146 return -EINVAL;
147}
148
149void radeon_scratch_free(struct radeon_device *rdev, uint32_t reg)
150{
151 int i;
152
153 for (i = 0; i < rdev->scratch.num_reg; i++) {
154 if (rdev->scratch.reg[i] == reg) {
155 rdev->scratch.free[i] = true;
156 return;
157 }
158 }
159}
160
Alex Deucher724c80e2010-08-27 18:25:25 -0400161void radeon_wb_disable(struct radeon_device *rdev)
162{
163 int r;
164
165 if (rdev->wb.wb_obj) {
166 r = radeon_bo_reserve(rdev->wb.wb_obj, false);
167 if (unlikely(r != 0))
168 return;
169 radeon_bo_kunmap(rdev->wb.wb_obj);
170 radeon_bo_unpin(rdev->wb.wb_obj);
171 radeon_bo_unreserve(rdev->wb.wb_obj);
172 }
173 rdev->wb.enabled = false;
174}
175
176void radeon_wb_fini(struct radeon_device *rdev)
177{
178 radeon_wb_disable(rdev);
179 if (rdev->wb.wb_obj) {
180 radeon_bo_unref(&rdev->wb.wb_obj);
181 rdev->wb.wb = NULL;
182 rdev->wb.wb_obj = NULL;
183 }
184}
185
186int radeon_wb_init(struct radeon_device *rdev)
187{
188 int r;
189
190 if (rdev->wb.wb_obj == NULL) {
Daniel Vetter441921d2011-02-18 17:59:16 +0100191 r = radeon_bo_create(rdev, RADEON_GPU_PAGE_SIZE, PAGE_SIZE, true,
Alex Deucher724c80e2010-08-27 18:25:25 -0400192 RADEON_GEM_DOMAIN_GTT, &rdev->wb.wb_obj);
193 if (r) {
194 dev_warn(rdev->dev, "(%d) create WB bo failed\n", r);
195 return r;
196 }
197 }
198 r = radeon_bo_reserve(rdev->wb.wb_obj, false);
199 if (unlikely(r != 0)) {
200 radeon_wb_fini(rdev);
201 return r;
202 }
203 r = radeon_bo_pin(rdev->wb.wb_obj, RADEON_GEM_DOMAIN_GTT,
204 &rdev->wb.gpu_addr);
205 if (r) {
206 radeon_bo_unreserve(rdev->wb.wb_obj);
207 dev_warn(rdev->dev, "(%d) pin WB bo failed\n", r);
208 radeon_wb_fini(rdev);
209 return r;
210 }
211 r = radeon_bo_kmap(rdev->wb.wb_obj, (void **)&rdev->wb.wb);
212 radeon_bo_unreserve(rdev->wb.wb_obj);
213 if (r) {
214 dev_warn(rdev->dev, "(%d) map WB bo failed\n", r);
215 radeon_wb_fini(rdev);
216 return r;
217 }
218
Alex Deuchere6ba7592011-06-13 22:02:51 +0000219 /* clear wb memory */
220 memset((char *)rdev->wb.wb, 0, RADEON_GPU_PAGE_SIZE);
Alex Deucherd0f8a852010-09-04 05:04:34 -0400221 /* disable event_write fences */
222 rdev->wb.use_event = false;
Alex Deucher724c80e2010-08-27 18:25:25 -0400223 /* disabled via module param */
224 if (radeon_no_wb == 1)
225 rdev->wb.enabled = false;
226 else {
227 /* often unreliable on AGP */
228 if (rdev->flags & RADEON_IS_AGP) {
229 rdev->wb.enabled = false;
Alex Deucherd0f8a852010-09-04 05:04:34 -0400230 } else {
Alex Deucher724c80e2010-08-27 18:25:25 -0400231 rdev->wb.enabled = true;
Alex Deucherd0f8a852010-09-04 05:04:34 -0400232 /* event_write fences are only available on r600+ */
233 if (rdev->family >= CHIP_R600)
234 rdev->wb.use_event = true;
235 }
Alex Deucher724c80e2010-08-27 18:25:25 -0400236 }
Alex Deucher7d527852011-01-06 21:19:27 -0500237 /* always use writeback/events on NI */
238 if (ASIC_IS_DCE5(rdev)) {
239 rdev->wb.enabled = true;
240 rdev->wb.use_event = true;
241 }
Alex Deucher724c80e2010-08-27 18:25:25 -0400242
243 dev_info(rdev->dev, "WB %sabled\n", rdev->wb.enabled ? "en" : "dis");
244
245 return 0;
246}
247
Jerome Glissed594e462010-02-17 21:54:29 +0000248/**
249 * radeon_vram_location - try to find VRAM location
250 * @rdev: radeon device structure holding all necessary informations
251 * @mc: memory controller structure holding memory informations
252 * @base: base address at which to put VRAM
253 *
254 * Function will place try to place VRAM at base address provided
255 * as parameter (which is so far either PCI aperture address or
256 * for IGP TOM base address).
257 *
258 * If there is not enough space to fit the unvisible VRAM in the 32bits
259 * address space then we limit the VRAM size to the aperture.
260 *
261 * If we are using AGP and if the AGP aperture doesn't allow us to have
262 * room for all the VRAM than we restrict the VRAM to the PCI aperture
263 * size and print a warning.
264 *
265 * This function will never fails, worst case are limiting VRAM.
266 *
267 * Note: GTT start, end, size should be initialized before calling this
268 * function on AGP platform.
269 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300270 * Note: We don't explicitly enforce VRAM start to be aligned on VRAM size,
Jerome Glissed594e462010-02-17 21:54:29 +0000271 * this shouldn't be a problem as we are using the PCI aperture as a reference.
272 * Otherwise this would be needed for rv280, all r3xx, and all r4xx, but
273 * not IGP.
274 *
275 * Note: we use mc_vram_size as on some board we need to program the mc to
276 * cover the whole aperture even if VRAM size is inferior to aperture size
277 * Novell bug 204882 + along with lots of ubuntu ones
278 *
279 * Note: when limiting vram it's safe to overwritte real_vram_size because
280 * we are not in case where real_vram_size is inferior to mc_vram_size (ie
281 * note afected by bogus hw of Novell bug 204882 + along with lots of ubuntu
282 * ones)
283 *
284 * Note: IGP TOM addr should be the same as the aperture addr, we don't
285 * explicitly check for that thought.
286 *
287 * FIXME: when reducing VRAM size align new size on power of 2.
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200288 */
Jerome Glissed594e462010-02-17 21:54:29 +0000289void radeon_vram_location(struct radeon_device *rdev, struct radeon_mc *mc, u64 base)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200290{
Jerome Glissed594e462010-02-17 21:54:29 +0000291 mc->vram_start = base;
292 if (mc->mc_vram_size > (0xFFFFFFFF - base + 1)) {
293 dev_warn(rdev->dev, "limiting VRAM to PCI aperture size\n");
294 mc->real_vram_size = mc->aper_size;
295 mc->mc_vram_size = mc->aper_size;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200296 }
Jerome Glissed594e462010-02-17 21:54:29 +0000297 mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
Jerome Glisse2cbeb4e2010-08-16 11:54:36 -0400298 if (rdev->flags & RADEON_IS_AGP && mc->vram_end > mc->gtt_start && mc->vram_start <= mc->gtt_end) {
Jerome Glissed594e462010-02-17 21:54:29 +0000299 dev_warn(rdev->dev, "limiting VRAM to PCI aperture size\n");
300 mc->real_vram_size = mc->aper_size;
301 mc->mc_vram_size = mc->aper_size;
302 }
303 mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
Michel Dänzerba95c452011-08-19 15:24:18 +0000304 if (radeon_vram_limit && radeon_vram_limit < mc->real_vram_size)
305 mc->real_vram_size = radeon_vram_limit;
Alex Deucherdd7cc552010-12-03 14:37:21 -0500306 dev_info(rdev->dev, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n",
Jerome Glissed594e462010-02-17 21:54:29 +0000307 mc->mc_vram_size >> 20, mc->vram_start,
308 mc->vram_end, mc->real_vram_size >> 20);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200309}
310
Jerome Glissed594e462010-02-17 21:54:29 +0000311/**
312 * radeon_gtt_location - try to find GTT location
313 * @rdev: radeon device structure holding all necessary informations
314 * @mc: memory controller structure holding memory informations
315 *
316 * Function will place try to place GTT before or after VRAM.
317 *
318 * If GTT size is bigger than space left then we ajust GTT size.
319 * Thus function will never fails.
320 *
321 * FIXME: when reducing GTT size align new size on power of 2.
322 */
323void radeon_gtt_location(struct radeon_device *rdev, struct radeon_mc *mc)
324{
325 u64 size_af, size_bf;
326
Alex Deucher8d369bb2010-07-15 10:51:10 -0400327 size_af = ((0xFFFFFFFF - mc->vram_end) + mc->gtt_base_align) & ~mc->gtt_base_align;
328 size_bf = mc->vram_start & ~mc->gtt_base_align;
Jerome Glissed594e462010-02-17 21:54:29 +0000329 if (size_bf > size_af) {
330 if (mc->gtt_size > size_bf) {
331 dev_warn(rdev->dev, "limiting GTT\n");
332 mc->gtt_size = size_bf;
333 }
Alex Deucher8d369bb2010-07-15 10:51:10 -0400334 mc->gtt_start = (mc->vram_start & ~mc->gtt_base_align) - mc->gtt_size;
Jerome Glissed594e462010-02-17 21:54:29 +0000335 } else {
336 if (mc->gtt_size > size_af) {
337 dev_warn(rdev->dev, "limiting GTT\n");
338 mc->gtt_size = size_af;
339 }
Alex Deucher8d369bb2010-07-15 10:51:10 -0400340 mc->gtt_start = (mc->vram_end + 1 + mc->gtt_base_align) & ~mc->gtt_base_align;
Jerome Glissed594e462010-02-17 21:54:29 +0000341 }
342 mc->gtt_end = mc->gtt_start + mc->gtt_size - 1;
Alex Deucherdd7cc552010-12-03 14:37:21 -0500343 dev_info(rdev->dev, "GTT: %lluM 0x%016llX - 0x%016llX\n",
Jerome Glissed594e462010-02-17 21:54:29 +0000344 mc->gtt_size >> 20, mc->gtt_start, mc->gtt_end);
345}
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200346
347/*
348 * GPU helpers function.
349 */
Jerome Glisse9f022dd2009-09-11 15:35:22 +0200350bool radeon_card_posted(struct radeon_device *rdev)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200351{
352 uint32_t reg;
353
Matthew Garrettbcc65fd2011-08-08 16:21:16 +0000354 if (efi_enabled && rdev->pdev->subsystem_vendor == PCI_VENDOR_ID_APPLE)
355 return false;
356
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200357 /* first check CRTCs */
Alex Deucher18007402010-11-22 17:56:28 -0500358 if (ASIC_IS_DCE41(rdev)) {
359 reg = RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC0_REGISTER_OFFSET) |
360 RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC1_REGISTER_OFFSET);
361 if (reg & EVERGREEN_CRTC_MASTER_EN)
362 return true;
363 } else if (ASIC_IS_DCE4(rdev)) {
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500364 reg = RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC0_REGISTER_OFFSET) |
365 RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC1_REGISTER_OFFSET) |
366 RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC2_REGISTER_OFFSET) |
367 RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC3_REGISTER_OFFSET) |
368 RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC4_REGISTER_OFFSET) |
369 RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC5_REGISTER_OFFSET);
370 if (reg & EVERGREEN_CRTC_MASTER_EN)
371 return true;
372 } else if (ASIC_IS_AVIVO(rdev)) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200373 reg = RREG32(AVIVO_D1CRTC_CONTROL) |
374 RREG32(AVIVO_D2CRTC_CONTROL);
375 if (reg & AVIVO_CRTC_EN) {
376 return true;
377 }
378 } else {
379 reg = RREG32(RADEON_CRTC_GEN_CNTL) |
380 RREG32(RADEON_CRTC2_GEN_CNTL);
381 if (reg & RADEON_CRTC_EN) {
382 return true;
383 }
384 }
385
386 /* then check MEM_SIZE, in case the crtcs are off */
387 if (rdev->family >= CHIP_R600)
388 reg = RREG32(R600_CONFIG_MEMSIZE);
389 else
390 reg = RREG32(RADEON_CONFIG_MEMSIZE);
391
392 if (reg)
393 return true;
394
395 return false;
396
397}
398
Alex Deucherf47299c2010-03-16 20:54:38 -0400399void radeon_update_bandwidth_info(struct radeon_device *rdev)
400{
401 fixed20_12 a;
Alex Deucher88072862010-08-10 12:33:20 -0400402 u32 sclk = rdev->pm.current_sclk;
403 u32 mclk = rdev->pm.current_mclk;
404
405 /* sclk/mclk in Mhz */
406 a.full = dfixed_const(100);
407 rdev->pm.sclk.full = dfixed_const(sclk);
408 rdev->pm.sclk.full = dfixed_div(rdev->pm.sclk, a);
409 rdev->pm.mclk.full = dfixed_const(mclk);
410 rdev->pm.mclk.full = dfixed_div(rdev->pm.mclk, a);
Alex Deucherf47299c2010-03-16 20:54:38 -0400411
412 if (rdev->flags & RADEON_IS_IGP) {
Ben Skeggs68adac52010-04-28 11:46:42 +1000413 a.full = dfixed_const(16);
Alex Deucherf47299c2010-03-16 20:54:38 -0400414 /* core_bandwidth = sclk(Mhz) * 16 */
Ben Skeggs68adac52010-04-28 11:46:42 +1000415 rdev->pm.core_bandwidth.full = dfixed_div(rdev->pm.sclk, a);
Alex Deucherf47299c2010-03-16 20:54:38 -0400416 }
417}
418
Dave Airlie72542d72009-12-01 14:06:31 +1000419bool radeon_boot_test_post_card(struct radeon_device *rdev)
420{
421 if (radeon_card_posted(rdev))
422 return true;
423
424 if (rdev->bios) {
425 DRM_INFO("GPU not posted. posting now...\n");
426 if (rdev->is_atom_bios)
427 atom_asic_init(rdev->mode_info.atom_context);
428 else
429 radeon_combios_asic_init(rdev->ddev);
430 return true;
431 } else {
432 dev_err(rdev->dev, "Card not posted and no BIOS - ignoring\n");
433 return false;
434 }
435}
436
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000437int radeon_dummy_page_init(struct radeon_device *rdev)
438{
Dave Airlie82568562010-02-05 16:00:07 +1000439 if (rdev->dummy_page.page)
440 return 0;
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000441 rdev->dummy_page.page = alloc_page(GFP_DMA32 | GFP_KERNEL | __GFP_ZERO);
442 if (rdev->dummy_page.page == NULL)
443 return -ENOMEM;
444 rdev->dummy_page.addr = pci_map_page(rdev->pdev, rdev->dummy_page.page,
445 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
Benjamin Herrenschmidta30f6fb72010-08-10 14:48:58 +1000446 if (pci_dma_mapping_error(rdev->pdev, rdev->dummy_page.addr)) {
447 dev_err(&rdev->pdev->dev, "Failed to DMA MAP the dummy page\n");
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000448 __free_page(rdev->dummy_page.page);
449 rdev->dummy_page.page = NULL;
450 return -ENOMEM;
451 }
452 return 0;
453}
454
455void radeon_dummy_page_fini(struct radeon_device *rdev)
456{
457 if (rdev->dummy_page.page == NULL)
458 return;
459 pci_unmap_page(rdev->pdev, rdev->dummy_page.addr,
460 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
461 __free_page(rdev->dummy_page.page);
462 rdev->dummy_page.page = NULL;
463}
464
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200465
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200466/* ATOM accessor methods */
467static uint32_t cail_pll_read(struct card_info *info, uint32_t reg)
468{
469 struct radeon_device *rdev = info->dev->dev_private;
470 uint32_t r;
471
472 r = rdev->pll_rreg(rdev, reg);
473 return r;
474}
475
476static void cail_pll_write(struct card_info *info, uint32_t reg, uint32_t val)
477{
478 struct radeon_device *rdev = info->dev->dev_private;
479
480 rdev->pll_wreg(rdev, reg, val);
481}
482
483static uint32_t cail_mc_read(struct card_info *info, uint32_t reg)
484{
485 struct radeon_device *rdev = info->dev->dev_private;
486 uint32_t r;
487
488 r = rdev->mc_rreg(rdev, reg);
489 return r;
490}
491
492static void cail_mc_write(struct card_info *info, uint32_t reg, uint32_t val)
493{
494 struct radeon_device *rdev = info->dev->dev_private;
495
496 rdev->mc_wreg(rdev, reg, val);
497}
498
499static void cail_reg_write(struct card_info *info, uint32_t reg, uint32_t val)
500{
501 struct radeon_device *rdev = info->dev->dev_private;
502
503 WREG32(reg*4, val);
504}
505
506static uint32_t cail_reg_read(struct card_info *info, uint32_t reg)
507{
508 struct radeon_device *rdev = info->dev->dev_private;
509 uint32_t r;
510
511 r = RREG32(reg*4);
512 return r;
513}
514
Alex Deucher351a52a2010-06-30 11:52:50 -0400515static void cail_ioreg_write(struct card_info *info, uint32_t reg, uint32_t val)
516{
517 struct radeon_device *rdev = info->dev->dev_private;
518
519 WREG32_IO(reg*4, val);
520}
521
522static uint32_t cail_ioreg_read(struct card_info *info, uint32_t reg)
523{
524 struct radeon_device *rdev = info->dev->dev_private;
525 uint32_t r;
526
527 r = RREG32_IO(reg*4);
528 return r;
529}
530
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200531int radeon_atombios_init(struct radeon_device *rdev)
532{
Mathias Fröhlich61c4b242009-10-27 15:08:01 -0400533 struct card_info *atom_card_info =
534 kzalloc(sizeof(struct card_info), GFP_KERNEL);
535
536 if (!atom_card_info)
537 return -ENOMEM;
538
539 rdev->mode_info.atom_card_info = atom_card_info;
540 atom_card_info->dev = rdev->ddev;
541 atom_card_info->reg_read = cail_reg_read;
542 atom_card_info->reg_write = cail_reg_write;
Alex Deucher351a52a2010-06-30 11:52:50 -0400543 /* needed for iio ops */
544 if (rdev->rio_mem) {
545 atom_card_info->ioreg_read = cail_ioreg_read;
546 atom_card_info->ioreg_write = cail_ioreg_write;
547 } else {
548 DRM_ERROR("Unable to find PCI I/O BAR; using MMIO for ATOM IIO\n");
549 atom_card_info->ioreg_read = cail_reg_read;
550 atom_card_info->ioreg_write = cail_reg_write;
551 }
Mathias Fröhlich61c4b242009-10-27 15:08:01 -0400552 atom_card_info->mc_read = cail_mc_read;
553 atom_card_info->mc_write = cail_mc_write;
554 atom_card_info->pll_read = cail_pll_read;
555 atom_card_info->pll_write = cail_pll_write;
556
557 rdev->mode_info.atom_context = atom_parse(atom_card_info, rdev->bios);
Rafał Miłeckic31ad972009-12-17 00:00:46 +0100558 mutex_init(&rdev->mode_info.atom_context->mutex);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200559 radeon_atom_initialize_bios_scratch_regs(rdev->ddev);
Dave Airlied904ef92009-11-17 06:29:46 +1000560 atom_allocate_fb_scratch(rdev->mode_info.atom_context);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200561 return 0;
562}
563
564void radeon_atombios_fini(struct radeon_device *rdev)
565{
Jerome Glisse4a04a842009-12-09 17:39:16 +0100566 if (rdev->mode_info.atom_context) {
567 kfree(rdev->mode_info.atom_context->scratch);
568 kfree(rdev->mode_info.atom_context);
569 }
Mathias Fröhlich61c4b242009-10-27 15:08:01 -0400570 kfree(rdev->mode_info.atom_card_info);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200571}
572
573int radeon_combios_init(struct radeon_device *rdev)
574{
575 radeon_combios_initialize_bios_scratch_regs(rdev->ddev);
576 return 0;
577}
578
579void radeon_combios_fini(struct radeon_device *rdev)
580{
581}
582
Dave Airlie28d52042009-09-21 14:33:58 +1000583/* if we get transitioned to only one device, tak VGA back */
584static unsigned int radeon_vga_set_decode(void *cookie, bool state)
585{
586 struct radeon_device *rdev = cookie;
Dave Airlie28d52042009-09-21 14:33:58 +1000587 radeon_vga_set_state(rdev, state);
588 if (state)
589 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
590 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
591 else
592 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
593}
Dave Airliec1176d62009-10-08 14:03:05 +1000594
Jerome Glisse36421332009-12-11 21:18:34 +0100595void radeon_check_arguments(struct radeon_device *rdev)
596{
597 /* vramlimit must be a power of two */
598 switch (radeon_vram_limit) {
599 case 0:
600 case 4:
601 case 8:
602 case 16:
603 case 32:
604 case 64:
605 case 128:
606 case 256:
607 case 512:
608 case 1024:
609 case 2048:
610 case 4096:
611 break;
612 default:
613 dev_warn(rdev->dev, "vram limit (%d) must be a power of 2\n",
614 radeon_vram_limit);
615 radeon_vram_limit = 0;
616 break;
617 }
618 radeon_vram_limit = radeon_vram_limit << 20;
619 /* gtt size must be power of two and greater or equal to 32M */
620 switch (radeon_gart_size) {
621 case 4:
622 case 8:
623 case 16:
624 dev_warn(rdev->dev, "gart size (%d) too small forcing to 512M\n",
625 radeon_gart_size);
626 radeon_gart_size = 512;
627 break;
628 case 32:
629 case 64:
630 case 128:
631 case 256:
632 case 512:
633 case 1024:
634 case 2048:
635 case 4096:
636 break;
637 default:
638 dev_warn(rdev->dev, "gart size (%d) must be a power of 2\n",
639 radeon_gart_size);
640 radeon_gart_size = 512;
641 break;
642 }
643 rdev->mc.gtt_size = radeon_gart_size * 1024 * 1024;
644 /* AGP mode can only be -1, 1, 2, 4, 8 */
645 switch (radeon_agpmode) {
646 case -1:
647 case 0:
648 case 1:
649 case 2:
650 case 4:
651 case 8:
652 break;
653 default:
654 dev_warn(rdev->dev, "invalid AGP mode %d (valid mode: "
655 "-1, 0, 1, 2, 4, 8)\n", radeon_agpmode);
656 radeon_agpmode = 0;
657 break;
658 }
659}
660
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000661static void radeon_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
662{
663 struct drm_device *dev = pci_get_drvdata(pdev);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000664 pm_message_t pmm = { .event = PM_EVENT_SUSPEND };
665 if (state == VGA_SWITCHEROO_ON) {
666 printk(KERN_INFO "radeon: switched on\n");
667 /* don't suspend or resume card normally */
Dave Airlie5bcf7192010-12-07 09:20:40 +1000668 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000669 radeon_resume_kms(dev);
Dave Airlie5bcf7192010-12-07 09:20:40 +1000670 dev->switch_power_state = DRM_SWITCH_POWER_ON;
Dave Airliefbf81762010-06-01 09:09:06 +1000671 drm_kms_helper_poll_enable(dev);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000672 } else {
673 printk(KERN_INFO "radeon: switched off\n");
Dave Airliefbf81762010-06-01 09:09:06 +1000674 drm_kms_helper_poll_disable(dev);
Dave Airlie5bcf7192010-12-07 09:20:40 +1000675 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000676 radeon_suspend_kms(dev, pmm);
Dave Airlie5bcf7192010-12-07 09:20:40 +1000677 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000678 }
679}
680
681static bool radeon_switcheroo_can_switch(struct pci_dev *pdev)
682{
683 struct drm_device *dev = pci_get_drvdata(pdev);
684 bool can_switch;
685
686 spin_lock(&dev->count_lock);
687 can_switch = (dev->open_count == 0);
688 spin_unlock(&dev->count_lock);
689 return can_switch;
690}
691
692
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200693int radeon_device_init(struct radeon_device *rdev,
694 struct drm_device *ddev,
695 struct pci_dev *pdev,
696 uint32_t flags)
697{
Alex Deucher351a52a2010-06-30 11:52:50 -0400698 int r, i;
Dave Airliead49f502009-07-10 22:36:26 +1000699 int dma_bits;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200700
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200701 rdev->shutdown = false;
Jerome Glisse9f022dd2009-09-11 15:35:22 +0200702 rdev->dev = &pdev->dev;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200703 rdev->ddev = ddev;
704 rdev->pdev = pdev;
705 rdev->flags = flags;
706 rdev->family = flags & RADEON_FAMILY_MASK;
707 rdev->is_atom_bios = false;
708 rdev->usec_timeout = RADEON_MAX_USEC_TIMEOUT;
709 rdev->mc.gtt_size = radeon_gart_size * 1024 * 1024;
710 rdev->gpu_lockup = false;
Jerome Glisse733289c2009-09-16 15:24:21 +0200711 rdev->accel_working = false;
Jerome Glisse1b5331d2010-04-12 20:21:53 +0000712
Thomas Reimd522d9c2011-07-29 14:28:59 +0000713 DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X).\n",
714 radeon_family_name[rdev->family], pdev->vendor, pdev->device,
715 pdev->subsystem_vendor, pdev->subsystem_device);
Jerome Glisse1b5331d2010-04-12 20:21:53 +0000716
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200717 /* mutex initialization are all done here so we
718 * can recall function without having locking issues */
Michel Dänzer7a1619b2011-11-10 18:57:26 +0100719 radeon_mutex_init(&rdev->cs_mutex);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200720 mutex_init(&rdev->ib_pool.mutex);
Christian Königbf852792011-10-13 13:19:22 +0200721 for (i = 0; i < RADEON_NUM_RINGS; ++i)
722 mutex_init(&rdev->cp[i].mutex);
Alex Deucher40bacf12009-12-23 03:23:21 -0500723 mutex_init(&rdev->dc_hw_i2c_mutex);
Alex Deucherd8f60cf2009-12-01 13:43:46 -0500724 if (rdev->family >= CHIP_R600)
725 spin_lock_init(&rdev->ih.lock);
Jerome Glisse4c788672009-11-20 14:29:23 +0100726 mutex_init(&rdev->gem.mutex);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100727 mutex_init(&rdev->pm.mutex);
Matthew Garrett5876dd22010-04-26 15:52:20 -0400728 mutex_init(&rdev->vram_mutex);
Alex Deucher74652802011-08-25 13:39:48 -0400729 rwlock_init(&rdev->fence_lock);
Christian König15d33322011-09-15 19:02:22 +0200730 rwlock_init(&rdev->semaphore_drv.lock);
Jerome Glisse9f022dd2009-09-11 15:35:22 +0200731 INIT_LIST_HEAD(&rdev->gem.objects);
Rafał Miłecki73a6d3f2010-01-08 00:22:47 +0100732 init_waitqueue_head(&rdev->irq.vblank_queue);
Alex Deucher2031f772010-04-22 12:52:11 -0400733 init_waitqueue_head(&rdev->irq.idle_queue);
Christian König15d33322011-09-15 19:02:22 +0200734 INIT_LIST_HEAD(&rdev->semaphore_drv.free);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200735
Jerome Glisse4aac0472009-09-14 18:29:49 +0200736 /* Set asic functions */
737 r = radeon_asic_init(rdev);
Jerome Glisse36421332009-12-11 21:18:34 +0100738 if (r)
Jerome Glisse4aac0472009-09-14 18:29:49 +0200739 return r;
Jerome Glisse36421332009-12-11 21:18:34 +0100740 radeon_check_arguments(rdev);
Jerome Glisse4aac0472009-09-14 18:29:49 +0200741
Alex Deucherf95df9c2010-03-21 14:02:25 -0400742 /* all of the newer IGP chips have an internal gart
743 * However some rs4xx report as AGP, so remove that here.
744 */
745 if ((rdev->family >= CHIP_RS400) &&
746 (rdev->flags & RADEON_IS_IGP)) {
747 rdev->flags &= ~RADEON_IS_AGP;
748 }
749
Jerome Glisse30256a32009-11-30 17:47:59 +0100750 if (rdev->flags & RADEON_IS_AGP && radeon_agpmode == -1) {
Jerome Glisseb574f252009-10-06 19:04:29 +0200751 radeon_agp_disable(rdev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200752 }
753
Dave Airliead49f502009-07-10 22:36:26 +1000754 /* set DMA mask + need_dma32 flags.
755 * PCIE - can handle 40-bits.
Alex Deucher005a83f2011-10-05 10:02:57 -0400756 * IGP - can handle 40-bits
Dave Airliead49f502009-07-10 22:36:26 +1000757 * AGP - generally dma32 is safest
Alex Deucher005a83f2011-10-05 10:02:57 -0400758 * PCI - dma32 for legacy pci gart, 40 bits on newer asics
Dave Airliead49f502009-07-10 22:36:26 +1000759 */
760 rdev->need_dma32 = false;
761 if (rdev->flags & RADEON_IS_AGP)
762 rdev->need_dma32 = true;
Alex Deucher005a83f2011-10-05 10:02:57 -0400763 if ((rdev->flags & RADEON_IS_PCI) &&
764 (rdev->family < CHIP_RS400))
Dave Airliead49f502009-07-10 22:36:26 +1000765 rdev->need_dma32 = true;
766
767 dma_bits = rdev->need_dma32 ? 32 : 40;
768 r = pci_set_dma_mask(rdev->pdev, DMA_BIT_MASK(dma_bits));
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200769 if (r) {
Daniel Haid62fff812011-06-08 20:04:45 +1000770 rdev->need_dma32 = true;
Konrad Rzeszutek Wilkc52494f2011-10-17 17:15:08 -0400771 dma_bits = 32;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200772 printk(KERN_WARNING "radeon: No suitable DMA available.\n");
773 }
Konrad Rzeszutek Wilkc52494f2011-10-17 17:15:08 -0400774 r = pci_set_consistent_dma_mask(rdev->pdev, DMA_BIT_MASK(dma_bits));
775 if (r) {
776 pci_set_consistent_dma_mask(rdev->pdev, DMA_BIT_MASK(32));
777 printk(KERN_WARNING "radeon: No coherent DMA available.\n");
778 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200779
780 /* Registers mapping */
781 /* TODO: block userspace mapping of io register */
Jordan Crouse01d73a62010-05-27 13:40:24 -0600782 rdev->rmmio_base = pci_resource_start(rdev->pdev, 2);
783 rdev->rmmio_size = pci_resource_len(rdev->pdev, 2);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200784 rdev->rmmio = ioremap(rdev->rmmio_base, rdev->rmmio_size);
785 if (rdev->rmmio == NULL) {
786 return -ENOMEM;
787 }
788 DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)rdev->rmmio_base);
789 DRM_INFO("register mmio size: %u\n", (unsigned)rdev->rmmio_size);
790
Alex Deucher351a52a2010-06-30 11:52:50 -0400791 /* io port mapping */
792 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
793 if (pci_resource_flags(rdev->pdev, i) & IORESOURCE_IO) {
794 rdev->rio_mem_size = pci_resource_len(rdev->pdev, i);
795 rdev->rio_mem = pci_iomap(rdev->pdev, i, rdev->rio_mem_size);
796 break;
797 }
798 }
799 if (rdev->rio_mem == NULL)
800 DRM_ERROR("Unable to find PCI I/O BAR\n");
801
Dave Airlie28d52042009-09-21 14:33:58 +1000802 /* if we have > 1 VGA cards, then disable the radeon VGA resources */
Dave Airlie93239ea2009-10-28 11:09:58 +1000803 /* this will fail for cards that aren't VGA class devices, just
804 * ignore it */
805 vga_client_register(rdev->pdev, rdev, NULL, radeon_vga_set_decode);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000806 vga_switcheroo_register_client(rdev->pdev,
807 radeon_switcheroo_set_state,
Dave Airlie8d608aa2010-12-07 08:57:57 +1000808 NULL,
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000809 radeon_switcheroo_can_switch);
Dave Airlie28d52042009-09-21 14:33:58 +1000810
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000811 r = radeon_init(rdev);
Jerome Glisseb574f252009-10-06 19:04:29 +0200812 if (r)
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000813 return r;
Michel Dänzerb1e3a6d2009-06-23 16:12:54 +0200814
Jerome Glisseb574f252009-10-06 19:04:29 +0200815 if (rdev->flags & RADEON_IS_AGP && !rdev->accel_working) {
816 /* Acceleration not working on AGP card try again
817 * with fallback to PCI or PCIE GART
818 */
Jerome Glissea2d07b72010-03-09 14:45:11 +0000819 radeon_asic_reset(rdev);
Jerome Glisseb574f252009-10-06 19:04:29 +0200820 radeon_fini(rdev);
821 radeon_agp_disable(rdev);
822 r = radeon_init(rdev);
Jerome Glisse4aac0472009-09-14 18:29:49 +0200823 if (r)
824 return r;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200825 }
Christian König60a7e392011-09-27 12:31:00 +0200826 if ((radeon_testing & 1)) {
Michel Dänzerecc0b322009-07-21 11:23:57 +0200827 radeon_test_moves(rdev);
828 }
Christian König60a7e392011-09-27 12:31:00 +0200829 if ((radeon_testing & 2)) {
830 radeon_test_syncing(rdev);
831 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200832 if (radeon_benchmarking) {
Ilija Hadzic638dd7d2011-10-12 23:29:39 -0400833 radeon_benchmark(rdev, radeon_benchmarking);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200834 }
Jerome Glisse6cf8a3f2009-09-10 21:46:48 +0200835 return 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200836}
837
Christian König4d8bf9a2011-10-24 14:54:54 +0200838static void radeon_debugfs_remove_files(struct radeon_device *rdev);
839
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200840void radeon_device_fini(struct radeon_device *rdev)
841{
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200842 DRM_INFO("radeon: finishing device.\n");
843 rdev->shutdown = true;
Jerome Glisse90aca4d2010-03-09 14:45:12 +0000844 /* evict vram memory */
845 radeon_bo_evict_vram(rdev);
Jerome Glisse62a8ea32009-10-01 18:02:11 +0200846 radeon_fini(rdev);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000847 vga_switcheroo_unregister_client(rdev->pdev);
Dave Airliec1176d62009-10-08 14:03:05 +1000848 vga_client_register(rdev->pdev, NULL, NULL, NULL);
Alex Deuchere0a2ca72010-07-08 12:24:52 -0400849 if (rdev->rio_mem)
850 pci_iounmap(rdev->pdev, rdev->rio_mem);
Alex Deucher351a52a2010-06-30 11:52:50 -0400851 rdev->rio_mem = NULL;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200852 iounmap(rdev->rmmio);
853 rdev->rmmio = NULL;
Christian König4d8bf9a2011-10-24 14:54:54 +0200854 radeon_debugfs_remove_files(rdev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200855}
856
857
858/*
859 * Suspend & resume.
860 */
861int radeon_suspend_kms(struct drm_device *dev, pm_message_t state)
862{
Darren Jenkins875c1862009-12-30 12:18:30 +1100863 struct radeon_device *rdev;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200864 struct drm_crtc *crtc;
Alex Deucherd8dcaa12010-06-02 12:08:41 -0400865 struct drm_connector *connector;
Alex Deucher74652802011-08-25 13:39:48 -0400866 int i, r;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200867
Darren Jenkins875c1862009-12-30 12:18:30 +1100868 if (dev == NULL || dev->dev_private == NULL) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200869 return -ENODEV;
870 }
871 if (state.event == PM_EVENT_PRETHAW) {
872 return 0;
873 }
Darren Jenkins875c1862009-12-30 12:18:30 +1100874 rdev = dev->dev_private;
875
Dave Airlie5bcf7192010-12-07 09:20:40 +1000876 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000877 return 0;
Alex Deucherd8dcaa12010-06-02 12:08:41 -0400878
879 /* turn off display hw */
880 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
881 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
882 }
883
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200884 /* unpin the front buffers */
885 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
886 struct radeon_framebuffer *rfb = to_radeon_framebuffer(crtc->fb);
Jerome Glisse4c788672009-11-20 14:29:23 +0100887 struct radeon_bo *robj;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200888
889 if (rfb == NULL || rfb->obj == NULL) {
890 continue;
891 }
Daniel Vetter7e4d15d2011-02-18 17:59:17 +0100892 robj = gem_to_radeon_bo(rfb->obj);
Dave Airlie38651672010-03-30 05:34:13 +0000893 /* don't unpin kernel fb objects */
894 if (!radeon_fbdev_robj_is_fb(rdev, robj)) {
Jerome Glisse4c788672009-11-20 14:29:23 +0100895 r = radeon_bo_reserve(robj, false);
Dave Airlie38651672010-03-30 05:34:13 +0000896 if (r == 0) {
Jerome Glisse4c788672009-11-20 14:29:23 +0100897 radeon_bo_unpin(robj);
898 radeon_bo_unreserve(robj);
899 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200900 }
901 }
902 /* evict vram memory */
Jerome Glisse4c788672009-11-20 14:29:23 +0100903 radeon_bo_evict_vram(rdev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200904 /* wait for gpu to finish processing current batch */
Alex Deucher74652802011-08-25 13:39:48 -0400905 for (i = 0; i < RADEON_NUM_RINGS; i++)
906 radeon_fence_wait_last(rdev, i);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200907
Yang Zhaof657c2a2009-09-15 12:21:01 +1000908 radeon_save_bios_scratch_regs(rdev);
909
Alex Deucherce8f5372010-05-07 15:10:16 -0400910 radeon_pm_suspend(rdev);
Jerome Glisse62a8ea32009-10-01 18:02:11 +0200911 radeon_suspend(rdev);
Alex Deucherd4877cf2009-12-04 16:56:37 -0500912 radeon_hpd_fini(rdev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200913 /* evict remaining vram memory */
Jerome Glisse4c788672009-11-20 14:29:23 +0100914 radeon_bo_evict_vram(rdev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200915
Jerome Glisse10b06122010-05-21 18:48:54 +0200916 radeon_agp_suspend(rdev);
917
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200918 pci_save_state(dev->pdev);
919 if (state.event == PM_EVENT_SUSPEND) {
920 /* Shut down the device */
921 pci_disable_device(dev->pdev);
922 pci_set_power_state(dev->pdev, PCI_D3hot);
923 }
Torben Hohnac751ef2011-01-25 15:07:35 -0800924 console_lock();
Dave Airlie38651672010-03-30 05:34:13 +0000925 radeon_fbdev_set_suspend(rdev, 1);
Torben Hohnac751ef2011-01-25 15:07:35 -0800926 console_unlock();
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200927 return 0;
928}
929
930int radeon_resume_kms(struct drm_device *dev)
931{
Cedric Godin09bdf592010-06-11 14:40:56 -0400932 struct drm_connector *connector;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200933 struct radeon_device *rdev = dev->dev_private;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200934
Dave Airlie5bcf7192010-12-07 09:20:40 +1000935 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000936 return 0;
937
Torben Hohnac751ef2011-01-25 15:07:35 -0800938 console_lock();
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200939 pci_set_power_state(dev->pdev, PCI_D0);
940 pci_restore_state(dev->pdev);
941 if (pci_enable_device(dev->pdev)) {
Torben Hohnac751ef2011-01-25 15:07:35 -0800942 console_unlock();
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200943 return -1;
944 }
945 pci_set_master(dev->pdev);
Dave Airlie0ebf1712009-11-05 15:39:10 +1000946 /* resume AGP if in use */
947 radeon_agp_resume(rdev);
Jerome Glisse62a8ea32009-10-01 18:02:11 +0200948 radeon_resume(rdev);
Alex Deucherce8f5372010-05-07 15:10:16 -0400949 radeon_pm_resume(rdev);
Yang Zhaof657c2a2009-09-15 12:21:01 +1000950 radeon_restore_bios_scratch_regs(rdev);
Cedric Godin09bdf592010-06-11 14:40:56 -0400951
Dave Airlie38651672010-03-30 05:34:13 +0000952 radeon_fbdev_set_suspend(rdev, 0);
Torben Hohnac751ef2011-01-25 15:07:35 -0800953 console_unlock();
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200954
Alex Deucherac89af12011-05-22 13:20:36 -0400955 /* init dig PHYs */
956 if (rdev->is_atom_bios)
957 radeon_atom_encoder_init(rdev);
Alex Deucherd4877cf2009-12-04 16:56:37 -0500958 /* reset hpd state */
959 radeon_hpd_init(rdev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200960 /* blat the mode back in */
961 drm_helper_resume_force_mode(dev);
Alex Deuchera93f3442010-12-20 11:22:29 -0500962 /* turn on display hw */
963 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
964 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
965 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200966 return 0;
967}
968
Jerome Glisse90aca4d2010-03-09 14:45:12 +0000969int radeon_gpu_reset(struct radeon_device *rdev)
970{
971 int r;
Dave Airlie8fd1b842011-02-10 14:46:06 +1000972 int resched;
Jerome Glisse90aca4d2010-03-09 14:45:12 +0000973
Michel Dänzer7a1619b2011-11-10 18:57:26 +0100974 /* Prevent CS ioctl from interfering */
975 radeon_mutex_lock(&rdev->cs_mutex);
976
Jerome Glisse90aca4d2010-03-09 14:45:12 +0000977 radeon_save_bios_scratch_regs(rdev);
Dave Airlie8fd1b842011-02-10 14:46:06 +1000978 /* block TTM */
979 resched = ttm_bo_lock_delayed_workqueue(&rdev->mman.bdev);
Jerome Glisse90aca4d2010-03-09 14:45:12 +0000980 radeon_suspend(rdev);
981
982 r = radeon_asic_reset(rdev);
983 if (!r) {
984 dev_info(rdev->dev, "GPU reset succeed\n");
985 radeon_resume(rdev);
986 radeon_restore_bios_scratch_regs(rdev);
987 drm_helper_resume_force_mode(rdev->ddev);
Dave Airlie8fd1b842011-02-10 14:46:06 +1000988 ttm_bo_unlock_delayed_workqueue(&rdev->mman.bdev, resched);
Jerome Glisse90aca4d2010-03-09 14:45:12 +0000989 }
Michel Dänzer7a1619b2011-11-10 18:57:26 +0100990
991 radeon_mutex_unlock(&rdev->cs_mutex);
992
993 if (r) {
994 /* bad news, how to tell it to userspace ? */
995 dev_info(rdev->dev, "GPU reset failed\n");
996 }
997
Jerome Glisse90aca4d2010-03-09 14:45:12 +0000998 return r;
999}
1000
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001001
1002/*
1003 * Debugfs
1004 */
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001005int radeon_debugfs_add_files(struct radeon_device *rdev,
1006 struct drm_info_list *files,
1007 unsigned nfiles)
1008{
1009 unsigned i;
1010
Christian König4d8bf9a2011-10-24 14:54:54 +02001011 for (i = 0; i < rdev->debugfs_count; i++) {
1012 if (rdev->debugfs[i].files == files) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001013 /* Already registered */
1014 return 0;
1015 }
1016 }
Michael Wittenc245cb92011-09-16 20:45:30 +00001017
Christian König4d8bf9a2011-10-24 14:54:54 +02001018 i = rdev->debugfs_count + 1;
Michael Wittenc245cb92011-09-16 20:45:30 +00001019 if (i > RADEON_DEBUGFS_MAX_COMPONENTS) {
1020 DRM_ERROR("Reached maximum number of debugfs components.\n");
1021 DRM_ERROR("Report so we increase "
1022 "RADEON_DEBUGFS_MAX_COMPONENTS.\n");
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001023 return -EINVAL;
1024 }
Christian König4d8bf9a2011-10-24 14:54:54 +02001025 rdev->debugfs[rdev->debugfs_count].files = files;
1026 rdev->debugfs[rdev->debugfs_count].num_files = nfiles;
1027 rdev->debugfs_count = i;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001028#if defined(CONFIG_DEBUG_FS)
1029 drm_debugfs_create_files(files, nfiles,
1030 rdev->ddev->control->debugfs_root,
1031 rdev->ddev->control);
1032 drm_debugfs_create_files(files, nfiles,
1033 rdev->ddev->primary->debugfs_root,
1034 rdev->ddev->primary);
1035#endif
1036 return 0;
1037}
1038
Christian König4d8bf9a2011-10-24 14:54:54 +02001039static void radeon_debugfs_remove_files(struct radeon_device *rdev)
1040{
1041#if defined(CONFIG_DEBUG_FS)
1042 unsigned i;
1043
1044 for (i = 0; i < rdev->debugfs_count; i++) {
1045 drm_debugfs_remove_files(rdev->debugfs[i].files,
1046 rdev->debugfs[i].num_files,
1047 rdev->ddev->control);
1048 drm_debugfs_remove_files(rdev->debugfs[i].files,
1049 rdev->debugfs[i].num_files,
1050 rdev->ddev->primary);
1051 }
1052#endif
1053}
1054
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001055#if defined(CONFIG_DEBUG_FS)
1056int radeon_debugfs_init(struct drm_minor *minor)
1057{
1058 return 0;
1059}
1060
1061void radeon_debugfs_cleanup(struct drm_minor *minor)
1062{
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001063}
1064#endif