blob: a64811a94519a1ce5bd245d7f2c864e0d68ecf3d [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>
Jerome Glisse771fe6b2009-06-05 14:42:42 +020035#include "radeon_reg.h"
36#include "radeon.h"
Jerome Glisse771fe6b2009-06-05 14:42:42 +020037#include "atom.h"
38
Jerome Glisse1b5331d2010-04-12 20:21:53 +000039static const char radeon_family_name[][16] = {
40 "R100",
41 "RV100",
42 "RS100",
43 "RV200",
44 "RS200",
45 "R200",
46 "RV250",
47 "RS300",
48 "RV280",
49 "R300",
50 "R350",
51 "RV350",
52 "RV380",
53 "R420",
54 "R423",
55 "RV410",
56 "RS400",
57 "RS480",
58 "RS600",
59 "RS690",
60 "RS740",
61 "RV515",
62 "R520",
63 "RV530",
64 "RV560",
65 "RV570",
66 "R580",
67 "R600",
68 "RV610",
69 "RV630",
70 "RV670",
71 "RV620",
72 "RV635",
73 "RS780",
74 "RS880",
75 "RV770",
76 "RV730",
77 "RV710",
78 "RV740",
79 "CEDAR",
80 "REDWOOD",
81 "JUNIPER",
82 "CYPRESS",
83 "HEMLOCK",
84 "LAST",
85};
86
Jerome Glisse771fe6b2009-06-05 14:42:42 +020087/*
Michel Dänzerb1e3a6d2009-06-23 16:12:54 +020088 * Clear GPU surface registers.
89 */
Jerome Glisse3ce0a232009-09-08 10:10:24 +100090void radeon_surface_init(struct radeon_device *rdev)
Michel Dänzerb1e3a6d2009-06-23 16:12:54 +020091{
92 /* FIXME: check this out */
93 if (rdev->family < CHIP_R600) {
94 int i;
95
Dave Airlie550e2d92009-12-09 14:15:38 +100096 for (i = 0; i < RADEON_GEM_MAX_SURFACES; i++) {
97 if (rdev->surface_regs[i].bo)
98 radeon_bo_get_surface_reg(rdev->surface_regs[i].bo);
99 else
100 radeon_clear_surface_reg(rdev, i);
Michel Dänzerb1e3a6d2009-06-23 16:12:54 +0200101 }
Dave Airliee024e112009-06-24 09:48:08 +1000102 /* enable surfaces */
103 WREG32(RADEON_SURFACE_CNTL, 0);
Michel Dänzerb1e3a6d2009-06-23 16:12:54 +0200104 }
105}
106
107/*
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200108 * GPU scratch registers helpers function.
109 */
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000110void radeon_scratch_init(struct radeon_device *rdev)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200111{
112 int i;
113
114 /* FIXME: check this out */
115 if (rdev->family < CHIP_R300) {
116 rdev->scratch.num_reg = 5;
117 } else {
118 rdev->scratch.num_reg = 7;
119 }
120 for (i = 0; i < rdev->scratch.num_reg; i++) {
121 rdev->scratch.free[i] = true;
122 rdev->scratch.reg[i] = RADEON_SCRATCH_REG0 + (i * 4);
123 }
124}
125
126int radeon_scratch_get(struct radeon_device *rdev, uint32_t *reg)
127{
128 int i;
129
130 for (i = 0; i < rdev->scratch.num_reg; i++) {
131 if (rdev->scratch.free[i]) {
132 rdev->scratch.free[i] = false;
133 *reg = rdev->scratch.reg[i];
134 return 0;
135 }
136 }
137 return -EINVAL;
138}
139
140void radeon_scratch_free(struct radeon_device *rdev, uint32_t reg)
141{
142 int i;
143
144 for (i = 0; i < rdev->scratch.num_reg; i++) {
145 if (rdev->scratch.reg[i] == reg) {
146 rdev->scratch.free[i] = true;
147 return;
148 }
149 }
150}
151
Jerome Glissed594e462010-02-17 21:54:29 +0000152/**
153 * radeon_vram_location - try to find VRAM location
154 * @rdev: radeon device structure holding all necessary informations
155 * @mc: memory controller structure holding memory informations
156 * @base: base address at which to put VRAM
157 *
158 * Function will place try to place VRAM at base address provided
159 * as parameter (which is so far either PCI aperture address or
160 * for IGP TOM base address).
161 *
162 * If there is not enough space to fit the unvisible VRAM in the 32bits
163 * address space then we limit the VRAM size to the aperture.
164 *
165 * If we are using AGP and if the AGP aperture doesn't allow us to have
166 * room for all the VRAM than we restrict the VRAM to the PCI aperture
167 * size and print a warning.
168 *
169 * This function will never fails, worst case are limiting VRAM.
170 *
171 * Note: GTT start, end, size should be initialized before calling this
172 * function on AGP platform.
173 *
174 * Note: We don't explictly enforce VRAM start to be aligned on VRAM size,
175 * this shouldn't be a problem as we are using the PCI aperture as a reference.
176 * Otherwise this would be needed for rv280, all r3xx, and all r4xx, but
177 * not IGP.
178 *
179 * Note: we use mc_vram_size as on some board we need to program the mc to
180 * cover the whole aperture even if VRAM size is inferior to aperture size
181 * Novell bug 204882 + along with lots of ubuntu ones
182 *
183 * Note: when limiting vram it's safe to overwritte real_vram_size because
184 * we are not in case where real_vram_size is inferior to mc_vram_size (ie
185 * note afected by bogus hw of Novell bug 204882 + along with lots of ubuntu
186 * ones)
187 *
188 * Note: IGP TOM addr should be the same as the aperture addr, we don't
189 * explicitly check for that thought.
190 *
191 * FIXME: when reducing VRAM size align new size on power of 2.
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200192 */
Jerome Glissed594e462010-02-17 21:54:29 +0000193void radeon_vram_location(struct radeon_device *rdev, struct radeon_mc *mc, u64 base)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200194{
Jerome Glissed594e462010-02-17 21:54:29 +0000195 mc->vram_start = base;
196 if (mc->mc_vram_size > (0xFFFFFFFF - base + 1)) {
197 dev_warn(rdev->dev, "limiting VRAM to PCI aperture size\n");
198 mc->real_vram_size = mc->aper_size;
199 mc->mc_vram_size = mc->aper_size;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200200 }
Jerome Glissed594e462010-02-17 21:54:29 +0000201 mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
202 if (rdev->flags & RADEON_IS_AGP && mc->vram_end > mc->gtt_start && mc->vram_end <= mc->gtt_end) {
203 dev_warn(rdev->dev, "limiting VRAM to PCI aperture size\n");
204 mc->real_vram_size = mc->aper_size;
205 mc->mc_vram_size = mc->aper_size;
206 }
207 mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
208 dev_info(rdev->dev, "VRAM: %lluM 0x%08llX - 0x%08llX (%lluM used)\n",
209 mc->mc_vram_size >> 20, mc->vram_start,
210 mc->vram_end, mc->real_vram_size >> 20);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200211}
212
Jerome Glissed594e462010-02-17 21:54:29 +0000213/**
214 * radeon_gtt_location - try to find GTT location
215 * @rdev: radeon device structure holding all necessary informations
216 * @mc: memory controller structure holding memory informations
217 *
218 * Function will place try to place GTT before or after VRAM.
219 *
220 * If GTT size is bigger than space left then we ajust GTT size.
221 * Thus function will never fails.
222 *
223 * FIXME: when reducing GTT size align new size on power of 2.
224 */
225void radeon_gtt_location(struct radeon_device *rdev, struct radeon_mc *mc)
226{
227 u64 size_af, size_bf;
228
Alex Deucher8d369bb2010-07-15 10:51:10 -0400229 size_af = ((0xFFFFFFFF - mc->vram_end) + mc->gtt_base_align) & ~mc->gtt_base_align;
230 size_bf = mc->vram_start & ~mc->gtt_base_align;
Jerome Glissed594e462010-02-17 21:54:29 +0000231 if (size_bf > size_af) {
232 if (mc->gtt_size > size_bf) {
233 dev_warn(rdev->dev, "limiting GTT\n");
234 mc->gtt_size = size_bf;
235 }
Alex Deucher8d369bb2010-07-15 10:51:10 -0400236 mc->gtt_start = (mc->vram_start & ~mc->gtt_base_align) - mc->gtt_size;
Jerome Glissed594e462010-02-17 21:54:29 +0000237 } else {
238 if (mc->gtt_size > size_af) {
239 dev_warn(rdev->dev, "limiting GTT\n");
240 mc->gtt_size = size_af;
241 }
Alex Deucher8d369bb2010-07-15 10:51:10 -0400242 mc->gtt_start = (mc->vram_end + 1 + mc->gtt_base_align) & ~mc->gtt_base_align;
Jerome Glissed594e462010-02-17 21:54:29 +0000243 }
244 mc->gtt_end = mc->gtt_start + mc->gtt_size - 1;
245 dev_info(rdev->dev, "GTT: %lluM 0x%08llX - 0x%08llX\n",
246 mc->gtt_size >> 20, mc->gtt_start, mc->gtt_end);
247}
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200248
249/*
250 * GPU helpers function.
251 */
Jerome Glisse9f022dd2009-09-11 15:35:22 +0200252bool radeon_card_posted(struct radeon_device *rdev)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200253{
254 uint32_t reg;
255
256 /* first check CRTCs */
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500257 if (ASIC_IS_DCE4(rdev)) {
258 reg = RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC0_REGISTER_OFFSET) |
259 RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC1_REGISTER_OFFSET) |
260 RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC2_REGISTER_OFFSET) |
261 RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC3_REGISTER_OFFSET) |
262 RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC4_REGISTER_OFFSET) |
263 RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC5_REGISTER_OFFSET);
264 if (reg & EVERGREEN_CRTC_MASTER_EN)
265 return true;
266 } else if (ASIC_IS_AVIVO(rdev)) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200267 reg = RREG32(AVIVO_D1CRTC_CONTROL) |
268 RREG32(AVIVO_D2CRTC_CONTROL);
269 if (reg & AVIVO_CRTC_EN) {
270 return true;
271 }
272 } else {
273 reg = RREG32(RADEON_CRTC_GEN_CNTL) |
274 RREG32(RADEON_CRTC2_GEN_CNTL);
275 if (reg & RADEON_CRTC_EN) {
276 return true;
277 }
278 }
279
280 /* then check MEM_SIZE, in case the crtcs are off */
281 if (rdev->family >= CHIP_R600)
282 reg = RREG32(R600_CONFIG_MEMSIZE);
283 else
284 reg = RREG32(RADEON_CONFIG_MEMSIZE);
285
286 if (reg)
287 return true;
288
289 return false;
290
291}
292
Alex Deucherf47299c2010-03-16 20:54:38 -0400293void radeon_update_bandwidth_info(struct radeon_device *rdev)
294{
295 fixed20_12 a;
296 u32 sclk, mclk;
297
298 if (rdev->flags & RADEON_IS_IGP) {
299 sclk = radeon_get_engine_clock(rdev);
300 mclk = rdev->clock.default_mclk;
301
Ben Skeggs68adac52010-04-28 11:46:42 +1000302 a.full = dfixed_const(100);
303 rdev->pm.sclk.full = dfixed_const(sclk);
304 rdev->pm.sclk.full = dfixed_div(rdev->pm.sclk, a);
305 rdev->pm.mclk.full = dfixed_const(mclk);
306 rdev->pm.mclk.full = dfixed_div(rdev->pm.mclk, a);
Alex Deucherf47299c2010-03-16 20:54:38 -0400307
Ben Skeggs68adac52010-04-28 11:46:42 +1000308 a.full = dfixed_const(16);
Alex Deucherf47299c2010-03-16 20:54:38 -0400309 /* core_bandwidth = sclk(Mhz) * 16 */
Ben Skeggs68adac52010-04-28 11:46:42 +1000310 rdev->pm.core_bandwidth.full = dfixed_div(rdev->pm.sclk, a);
Alex Deucherf47299c2010-03-16 20:54:38 -0400311 } else {
312 sclk = radeon_get_engine_clock(rdev);
313 mclk = radeon_get_memory_clock(rdev);
314
Ben Skeggs68adac52010-04-28 11:46:42 +1000315 a.full = dfixed_const(100);
316 rdev->pm.sclk.full = dfixed_const(sclk);
317 rdev->pm.sclk.full = dfixed_div(rdev->pm.sclk, a);
318 rdev->pm.mclk.full = dfixed_const(mclk);
319 rdev->pm.mclk.full = dfixed_div(rdev->pm.mclk, a);
Alex Deucherf47299c2010-03-16 20:54:38 -0400320 }
321}
322
Dave Airlie72542d72009-12-01 14:06:31 +1000323bool radeon_boot_test_post_card(struct radeon_device *rdev)
324{
325 if (radeon_card_posted(rdev))
326 return true;
327
328 if (rdev->bios) {
329 DRM_INFO("GPU not posted. posting now...\n");
330 if (rdev->is_atom_bios)
331 atom_asic_init(rdev->mode_info.atom_context);
332 else
333 radeon_combios_asic_init(rdev->ddev);
334 return true;
335 } else {
336 dev_err(rdev->dev, "Card not posted and no BIOS - ignoring\n");
337 return false;
338 }
339}
340
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000341int radeon_dummy_page_init(struct radeon_device *rdev)
342{
Dave Airlie82568562010-02-05 16:00:07 +1000343 if (rdev->dummy_page.page)
344 return 0;
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000345 rdev->dummy_page.page = alloc_page(GFP_DMA32 | GFP_KERNEL | __GFP_ZERO);
346 if (rdev->dummy_page.page == NULL)
347 return -ENOMEM;
348 rdev->dummy_page.addr = pci_map_page(rdev->pdev, rdev->dummy_page.page,
349 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
350 if (!rdev->dummy_page.addr) {
351 __free_page(rdev->dummy_page.page);
352 rdev->dummy_page.page = NULL;
353 return -ENOMEM;
354 }
355 return 0;
356}
357
358void radeon_dummy_page_fini(struct radeon_device *rdev)
359{
360 if (rdev->dummy_page.page == NULL)
361 return;
362 pci_unmap_page(rdev->pdev, rdev->dummy_page.addr,
363 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
364 __free_page(rdev->dummy_page.page);
365 rdev->dummy_page.page = NULL;
366}
367
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200368
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200369/* ATOM accessor methods */
370static uint32_t cail_pll_read(struct card_info *info, uint32_t reg)
371{
372 struct radeon_device *rdev = info->dev->dev_private;
373 uint32_t r;
374
375 r = rdev->pll_rreg(rdev, reg);
376 return r;
377}
378
379static void cail_pll_write(struct card_info *info, uint32_t reg, uint32_t val)
380{
381 struct radeon_device *rdev = info->dev->dev_private;
382
383 rdev->pll_wreg(rdev, reg, val);
384}
385
386static uint32_t cail_mc_read(struct card_info *info, uint32_t reg)
387{
388 struct radeon_device *rdev = info->dev->dev_private;
389 uint32_t r;
390
391 r = rdev->mc_rreg(rdev, reg);
392 return r;
393}
394
395static void cail_mc_write(struct card_info *info, uint32_t reg, uint32_t val)
396{
397 struct radeon_device *rdev = info->dev->dev_private;
398
399 rdev->mc_wreg(rdev, reg, val);
400}
401
402static void cail_reg_write(struct card_info *info, uint32_t reg, uint32_t val)
403{
404 struct radeon_device *rdev = info->dev->dev_private;
405
406 WREG32(reg*4, val);
407}
408
409static uint32_t cail_reg_read(struct card_info *info, uint32_t reg)
410{
411 struct radeon_device *rdev = info->dev->dev_private;
412 uint32_t r;
413
414 r = RREG32(reg*4);
415 return r;
416}
417
Alex Deucher351a52a2010-06-30 11:52:50 -0400418static void cail_ioreg_write(struct card_info *info, uint32_t reg, uint32_t val)
419{
420 struct radeon_device *rdev = info->dev->dev_private;
421
422 WREG32_IO(reg*4, val);
423}
424
425static uint32_t cail_ioreg_read(struct card_info *info, uint32_t reg)
426{
427 struct radeon_device *rdev = info->dev->dev_private;
428 uint32_t r;
429
430 r = RREG32_IO(reg*4);
431 return r;
432}
433
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200434int radeon_atombios_init(struct radeon_device *rdev)
435{
Mathias Fröhlich61c4b242009-10-27 15:08:01 -0400436 struct card_info *atom_card_info =
437 kzalloc(sizeof(struct card_info), GFP_KERNEL);
438
439 if (!atom_card_info)
440 return -ENOMEM;
441
442 rdev->mode_info.atom_card_info = atom_card_info;
443 atom_card_info->dev = rdev->ddev;
444 atom_card_info->reg_read = cail_reg_read;
445 atom_card_info->reg_write = cail_reg_write;
Alex Deucher351a52a2010-06-30 11:52:50 -0400446 /* needed for iio ops */
447 if (rdev->rio_mem) {
448 atom_card_info->ioreg_read = cail_ioreg_read;
449 atom_card_info->ioreg_write = cail_ioreg_write;
450 } else {
451 DRM_ERROR("Unable to find PCI I/O BAR; using MMIO for ATOM IIO\n");
452 atom_card_info->ioreg_read = cail_reg_read;
453 atom_card_info->ioreg_write = cail_reg_write;
454 }
Mathias Fröhlich61c4b242009-10-27 15:08:01 -0400455 atom_card_info->mc_read = cail_mc_read;
456 atom_card_info->mc_write = cail_mc_write;
457 atom_card_info->pll_read = cail_pll_read;
458 atom_card_info->pll_write = cail_pll_write;
459
460 rdev->mode_info.atom_context = atom_parse(atom_card_info, rdev->bios);
Rafał Miłeckic31ad972009-12-17 00:00:46 +0100461 mutex_init(&rdev->mode_info.atom_context->mutex);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200462 radeon_atom_initialize_bios_scratch_regs(rdev->ddev);
Dave Airlied904ef92009-11-17 06:29:46 +1000463 atom_allocate_fb_scratch(rdev->mode_info.atom_context);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200464 return 0;
465}
466
467void radeon_atombios_fini(struct radeon_device *rdev)
468{
Jerome Glisse4a04a842009-12-09 17:39:16 +0100469 if (rdev->mode_info.atom_context) {
470 kfree(rdev->mode_info.atom_context->scratch);
471 kfree(rdev->mode_info.atom_context);
472 }
Mathias Fröhlich61c4b242009-10-27 15:08:01 -0400473 kfree(rdev->mode_info.atom_card_info);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200474}
475
476int radeon_combios_init(struct radeon_device *rdev)
477{
478 radeon_combios_initialize_bios_scratch_regs(rdev->ddev);
479 return 0;
480}
481
482void radeon_combios_fini(struct radeon_device *rdev)
483{
484}
485
Dave Airlie28d52042009-09-21 14:33:58 +1000486/* if we get transitioned to only one device, tak VGA back */
487static unsigned int radeon_vga_set_decode(void *cookie, bool state)
488{
489 struct radeon_device *rdev = cookie;
Dave Airlie28d52042009-09-21 14:33:58 +1000490 radeon_vga_set_state(rdev, state);
491 if (state)
492 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
493 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
494 else
495 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
496}
Dave Airliec1176d62009-10-08 14:03:05 +1000497
Jerome Glisse36421332009-12-11 21:18:34 +0100498void radeon_check_arguments(struct radeon_device *rdev)
499{
500 /* vramlimit must be a power of two */
501 switch (radeon_vram_limit) {
502 case 0:
503 case 4:
504 case 8:
505 case 16:
506 case 32:
507 case 64:
508 case 128:
509 case 256:
510 case 512:
511 case 1024:
512 case 2048:
513 case 4096:
514 break;
515 default:
516 dev_warn(rdev->dev, "vram limit (%d) must be a power of 2\n",
517 radeon_vram_limit);
518 radeon_vram_limit = 0;
519 break;
520 }
521 radeon_vram_limit = radeon_vram_limit << 20;
522 /* gtt size must be power of two and greater or equal to 32M */
523 switch (radeon_gart_size) {
524 case 4:
525 case 8:
526 case 16:
527 dev_warn(rdev->dev, "gart size (%d) too small forcing to 512M\n",
528 radeon_gart_size);
529 radeon_gart_size = 512;
530 break;
531 case 32:
532 case 64:
533 case 128:
534 case 256:
535 case 512:
536 case 1024:
537 case 2048:
538 case 4096:
539 break;
540 default:
541 dev_warn(rdev->dev, "gart size (%d) must be a power of 2\n",
542 radeon_gart_size);
543 radeon_gart_size = 512;
544 break;
545 }
546 rdev->mc.gtt_size = radeon_gart_size * 1024 * 1024;
547 /* AGP mode can only be -1, 1, 2, 4, 8 */
548 switch (radeon_agpmode) {
549 case -1:
550 case 0:
551 case 1:
552 case 2:
553 case 4:
554 case 8:
555 break;
556 default:
557 dev_warn(rdev->dev, "invalid AGP mode %d (valid mode: "
558 "-1, 0, 1, 2, 4, 8)\n", radeon_agpmode);
559 radeon_agpmode = 0;
560 break;
561 }
562}
563
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000564static void radeon_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
565{
566 struct drm_device *dev = pci_get_drvdata(pdev);
567 struct radeon_device *rdev = dev->dev_private;
568 pm_message_t pmm = { .event = PM_EVENT_SUSPEND };
569 if (state == VGA_SWITCHEROO_ON) {
570 printk(KERN_INFO "radeon: switched on\n");
571 /* don't suspend or resume card normally */
572 rdev->powered_down = false;
573 radeon_resume_kms(dev);
Dave Airliefbf81762010-06-01 09:09:06 +1000574 drm_kms_helper_poll_enable(dev);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000575 } else {
576 printk(KERN_INFO "radeon: switched off\n");
Dave Airliefbf81762010-06-01 09:09:06 +1000577 drm_kms_helper_poll_disable(dev);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000578 radeon_suspend_kms(dev, pmm);
579 /* don't suspend or resume card normally */
580 rdev->powered_down = true;
581 }
582}
583
584static bool radeon_switcheroo_can_switch(struct pci_dev *pdev)
585{
586 struct drm_device *dev = pci_get_drvdata(pdev);
587 bool can_switch;
588
589 spin_lock(&dev->count_lock);
590 can_switch = (dev->open_count == 0);
591 spin_unlock(&dev->count_lock);
592 return can_switch;
593}
594
595
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200596int radeon_device_init(struct radeon_device *rdev,
597 struct drm_device *ddev,
598 struct pci_dev *pdev,
599 uint32_t flags)
600{
Alex Deucher351a52a2010-06-30 11:52:50 -0400601 int r, i;
Dave Airliead49f502009-07-10 22:36:26 +1000602 int dma_bits;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200603
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200604 rdev->shutdown = false;
Jerome Glisse9f022dd2009-09-11 15:35:22 +0200605 rdev->dev = &pdev->dev;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200606 rdev->ddev = ddev;
607 rdev->pdev = pdev;
608 rdev->flags = flags;
609 rdev->family = flags & RADEON_FAMILY_MASK;
610 rdev->is_atom_bios = false;
611 rdev->usec_timeout = RADEON_MAX_USEC_TIMEOUT;
612 rdev->mc.gtt_size = radeon_gart_size * 1024 * 1024;
613 rdev->gpu_lockup = false;
Jerome Glisse733289c2009-09-16 15:24:21 +0200614 rdev->accel_working = false;
Jerome Glisse1b5331d2010-04-12 20:21:53 +0000615
616 DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X).\n",
617 radeon_family_name[rdev->family], pdev->vendor, pdev->device);
618
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200619 /* mutex initialization are all done here so we
620 * can recall function without having locking issues */
621 mutex_init(&rdev->cs_mutex);
622 mutex_init(&rdev->ib_pool.mutex);
623 mutex_init(&rdev->cp.mutex);
Alex Deucher40bacf12009-12-23 03:23:21 -0500624 mutex_init(&rdev->dc_hw_i2c_mutex);
Alex Deucherd8f60cf2009-12-01 13:43:46 -0500625 if (rdev->family >= CHIP_R600)
626 spin_lock_init(&rdev->ih.lock);
Jerome Glisse4c788672009-11-20 14:29:23 +0100627 mutex_init(&rdev->gem.mutex);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100628 mutex_init(&rdev->pm.mutex);
Matthew Garrett5876dd22010-04-26 15:52:20 -0400629 mutex_init(&rdev->vram_mutex);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200630 rwlock_init(&rdev->fence_drv.lock);
Jerome Glisse9f022dd2009-09-11 15:35:22 +0200631 INIT_LIST_HEAD(&rdev->gem.objects);
Rafał Miłecki73a6d3f2010-01-08 00:22:47 +0100632 init_waitqueue_head(&rdev->irq.vblank_queue);
Alex Deucher2031f772010-04-22 12:52:11 -0400633 init_waitqueue_head(&rdev->irq.idle_queue);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200634
Alex Deucherd4877cf2009-12-04 16:56:37 -0500635 /* setup workqueue */
636 rdev->wq = create_workqueue("radeon");
637 if (rdev->wq == NULL)
638 return -ENOMEM;
639
Jerome Glisse4aac0472009-09-14 18:29:49 +0200640 /* Set asic functions */
641 r = radeon_asic_init(rdev);
Jerome Glisse36421332009-12-11 21:18:34 +0100642 if (r)
Jerome Glisse4aac0472009-09-14 18:29:49 +0200643 return r;
Jerome Glisse36421332009-12-11 21:18:34 +0100644 radeon_check_arguments(rdev);
Jerome Glisse4aac0472009-09-14 18:29:49 +0200645
Alex Deucherf95df9c2010-03-21 14:02:25 -0400646 /* all of the newer IGP chips have an internal gart
647 * However some rs4xx report as AGP, so remove that here.
648 */
649 if ((rdev->family >= CHIP_RS400) &&
650 (rdev->flags & RADEON_IS_IGP)) {
651 rdev->flags &= ~RADEON_IS_AGP;
652 }
653
Jerome Glisse30256a32009-11-30 17:47:59 +0100654 if (rdev->flags & RADEON_IS_AGP && radeon_agpmode == -1) {
Jerome Glisseb574f252009-10-06 19:04:29 +0200655 radeon_agp_disable(rdev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200656 }
657
Dave Airliead49f502009-07-10 22:36:26 +1000658 /* set DMA mask + need_dma32 flags.
659 * PCIE - can handle 40-bits.
660 * IGP - can handle 40-bits (in theory)
661 * AGP - generally dma32 is safest
662 * PCI - only dma32
663 */
664 rdev->need_dma32 = false;
665 if (rdev->flags & RADEON_IS_AGP)
666 rdev->need_dma32 = true;
667 if (rdev->flags & RADEON_IS_PCI)
668 rdev->need_dma32 = true;
669
670 dma_bits = rdev->need_dma32 ? 32 : 40;
671 r = pci_set_dma_mask(rdev->pdev, DMA_BIT_MASK(dma_bits));
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200672 if (r) {
673 printk(KERN_WARNING "radeon: No suitable DMA available.\n");
674 }
675
676 /* Registers mapping */
677 /* TODO: block userspace mapping of io register */
Jordan Crouse01d73a62010-05-27 13:40:24 -0600678 rdev->rmmio_base = pci_resource_start(rdev->pdev, 2);
679 rdev->rmmio_size = pci_resource_len(rdev->pdev, 2);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200680 rdev->rmmio = ioremap(rdev->rmmio_base, rdev->rmmio_size);
681 if (rdev->rmmio == NULL) {
682 return -ENOMEM;
683 }
684 DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)rdev->rmmio_base);
685 DRM_INFO("register mmio size: %u\n", (unsigned)rdev->rmmio_size);
686
Alex Deucher351a52a2010-06-30 11:52:50 -0400687 /* io port mapping */
688 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
689 if (pci_resource_flags(rdev->pdev, i) & IORESOURCE_IO) {
690 rdev->rio_mem_size = pci_resource_len(rdev->pdev, i);
691 rdev->rio_mem = pci_iomap(rdev->pdev, i, rdev->rio_mem_size);
692 break;
693 }
694 }
695 if (rdev->rio_mem == NULL)
696 DRM_ERROR("Unable to find PCI I/O BAR\n");
697
Dave Airlie28d52042009-09-21 14:33:58 +1000698 /* if we have > 1 VGA cards, then disable the radeon VGA resources */
Dave Airlie93239ea2009-10-28 11:09:58 +1000699 /* this will fail for cards that aren't VGA class devices, just
700 * ignore it */
701 vga_client_register(rdev->pdev, rdev, NULL, radeon_vga_set_decode);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000702 vga_switcheroo_register_client(rdev->pdev,
703 radeon_switcheroo_set_state,
704 radeon_switcheroo_can_switch);
Dave Airlie28d52042009-09-21 14:33:58 +1000705
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000706 r = radeon_init(rdev);
Jerome Glisseb574f252009-10-06 19:04:29 +0200707 if (r)
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000708 return r;
Michel Dänzerb1e3a6d2009-06-23 16:12:54 +0200709
Jerome Glisseb574f252009-10-06 19:04:29 +0200710 if (rdev->flags & RADEON_IS_AGP && !rdev->accel_working) {
711 /* Acceleration not working on AGP card try again
712 * with fallback to PCI or PCIE GART
713 */
Jerome Glissea2d07b72010-03-09 14:45:11 +0000714 radeon_asic_reset(rdev);
Jerome Glisseb574f252009-10-06 19:04:29 +0200715 radeon_fini(rdev);
716 radeon_agp_disable(rdev);
717 r = radeon_init(rdev);
Jerome Glisse4aac0472009-09-14 18:29:49 +0200718 if (r)
719 return r;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200720 }
Michel Dänzerecc0b322009-07-21 11:23:57 +0200721 if (radeon_testing) {
722 radeon_test_moves(rdev);
723 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200724 if (radeon_benchmarking) {
725 radeon_benchmark(rdev);
726 }
Jerome Glisse6cf8a3f52009-09-10 21:46:48 +0200727 return 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200728}
729
730void radeon_device_fini(struct radeon_device *rdev)
731{
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200732 DRM_INFO("radeon: finishing device.\n");
733 rdev->shutdown = true;
Jerome Glisse90aca4d2010-03-09 14:45:12 +0000734 /* evict vram memory */
735 radeon_bo_evict_vram(rdev);
Jerome Glisse62a8ea32009-10-01 18:02:11 +0200736 radeon_fini(rdev);
Alex Deucherd4877cf2009-12-04 16:56:37 -0500737 destroy_workqueue(rdev->wq);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000738 vga_switcheroo_unregister_client(rdev->pdev);
Dave Airliec1176d62009-10-08 14:03:05 +1000739 vga_client_register(rdev->pdev, NULL, NULL, NULL);
Alex Deuchere0a2ca72010-07-08 12:24:52 -0400740 if (rdev->rio_mem)
741 pci_iounmap(rdev->pdev, rdev->rio_mem);
Alex Deucher351a52a2010-06-30 11:52:50 -0400742 rdev->rio_mem = NULL;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200743 iounmap(rdev->rmmio);
744 rdev->rmmio = NULL;
745}
746
747
748/*
749 * Suspend & resume.
750 */
751int radeon_suspend_kms(struct drm_device *dev, pm_message_t state)
752{
Darren Jenkins875c1862009-12-30 12:18:30 +1100753 struct radeon_device *rdev;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200754 struct drm_crtc *crtc;
Alex Deucherd8dcaa12010-06-02 12:08:41 -0400755 struct drm_connector *connector;
Jerome Glisse4c788672009-11-20 14:29:23 +0100756 int r;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200757
Darren Jenkins875c1862009-12-30 12:18:30 +1100758 if (dev == NULL || dev->dev_private == NULL) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200759 return -ENODEV;
760 }
761 if (state.event == PM_EVENT_PRETHAW) {
762 return 0;
763 }
Darren Jenkins875c1862009-12-30 12:18:30 +1100764 rdev = dev->dev_private;
765
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000766 if (rdev->powered_down)
767 return 0;
Alex Deucherd8dcaa12010-06-02 12:08:41 -0400768
769 /* turn off display hw */
770 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
771 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
772 }
773
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200774 /* unpin the front buffers */
775 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
776 struct radeon_framebuffer *rfb = to_radeon_framebuffer(crtc->fb);
Jerome Glisse4c788672009-11-20 14:29:23 +0100777 struct radeon_bo *robj;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200778
779 if (rfb == NULL || rfb->obj == NULL) {
780 continue;
781 }
782 robj = rfb->obj->driver_private;
Dave Airlie38651672010-03-30 05:34:13 +0000783 /* don't unpin kernel fb objects */
784 if (!radeon_fbdev_robj_is_fb(rdev, robj)) {
Jerome Glisse4c788672009-11-20 14:29:23 +0100785 r = radeon_bo_reserve(robj, false);
Dave Airlie38651672010-03-30 05:34:13 +0000786 if (r == 0) {
Jerome Glisse4c788672009-11-20 14:29:23 +0100787 radeon_bo_unpin(robj);
788 radeon_bo_unreserve(robj);
789 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200790 }
791 }
792 /* evict vram memory */
Jerome Glisse4c788672009-11-20 14:29:23 +0100793 radeon_bo_evict_vram(rdev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200794 /* wait for gpu to finish processing current batch */
795 radeon_fence_wait_last(rdev);
796
Yang Zhaof657c2a2009-09-15 12:21:01 +1000797 radeon_save_bios_scratch_regs(rdev);
798
Alex Deucherce8f5372010-05-07 15:10:16 -0400799 radeon_pm_suspend(rdev);
Jerome Glisse62a8ea32009-10-01 18:02:11 +0200800 radeon_suspend(rdev);
Alex Deucherd4877cf2009-12-04 16:56:37 -0500801 radeon_hpd_fini(rdev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200802 /* evict remaining vram memory */
Jerome Glisse4c788672009-11-20 14:29:23 +0100803 radeon_bo_evict_vram(rdev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200804
Jerome Glisse10b06122010-05-21 18:48:54 +0200805 radeon_agp_suspend(rdev);
806
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200807 pci_save_state(dev->pdev);
808 if (state.event == PM_EVENT_SUSPEND) {
809 /* Shut down the device */
810 pci_disable_device(dev->pdev);
811 pci_set_power_state(dev->pdev, PCI_D3hot);
812 }
813 acquire_console_sem();
Dave Airlie38651672010-03-30 05:34:13 +0000814 radeon_fbdev_set_suspend(rdev, 1);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200815 release_console_sem();
816 return 0;
817}
818
819int radeon_resume_kms(struct drm_device *dev)
820{
Cedric Godin09bdf592010-06-11 14:40:56 -0400821 struct drm_connector *connector;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200822 struct radeon_device *rdev = dev->dev_private;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200823
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000824 if (rdev->powered_down)
825 return 0;
826
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200827 acquire_console_sem();
828 pci_set_power_state(dev->pdev, PCI_D0);
829 pci_restore_state(dev->pdev);
830 if (pci_enable_device(dev->pdev)) {
831 release_console_sem();
832 return -1;
833 }
834 pci_set_master(dev->pdev);
Dave Airlie0ebf1712009-11-05 15:39:10 +1000835 /* resume AGP if in use */
836 radeon_agp_resume(rdev);
Jerome Glisse62a8ea32009-10-01 18:02:11 +0200837 radeon_resume(rdev);
Alex Deucherce8f5372010-05-07 15:10:16 -0400838 radeon_pm_resume(rdev);
Yang Zhaof657c2a2009-09-15 12:21:01 +1000839 radeon_restore_bios_scratch_regs(rdev);
Cedric Godin09bdf592010-06-11 14:40:56 -0400840
841 /* turn on display hw */
842 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
843 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
844 }
845
Dave Airlie38651672010-03-30 05:34:13 +0000846 radeon_fbdev_set_suspend(rdev, 0);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200847 release_console_sem();
848
Alex Deucherd4877cf2009-12-04 16:56:37 -0500849 /* reset hpd state */
850 radeon_hpd_init(rdev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200851 /* blat the mode back in */
852 drm_helper_resume_force_mode(dev);
853 return 0;
854}
855
Jerome Glisse90aca4d2010-03-09 14:45:12 +0000856int radeon_gpu_reset(struct radeon_device *rdev)
857{
858 int r;
859
860 radeon_save_bios_scratch_regs(rdev);
861 radeon_suspend(rdev);
862
863 r = radeon_asic_reset(rdev);
864 if (!r) {
865 dev_info(rdev->dev, "GPU reset succeed\n");
866 radeon_resume(rdev);
867 radeon_restore_bios_scratch_regs(rdev);
868 drm_helper_resume_force_mode(rdev->ddev);
869 return 0;
870 }
871 /* bad news, how to tell it to userspace ? */
872 dev_info(rdev->dev, "GPU reset failed\n");
873 return r;
874}
875
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200876
877/*
878 * Debugfs
879 */
880struct radeon_debugfs {
881 struct drm_info_list *files;
882 unsigned num_files;
883};
884static struct radeon_debugfs _radeon_debugfs[RADEON_DEBUGFS_MAX_NUM_FILES];
885static unsigned _radeon_debugfs_count = 0;
886
887int radeon_debugfs_add_files(struct radeon_device *rdev,
888 struct drm_info_list *files,
889 unsigned nfiles)
890{
891 unsigned i;
892
893 for (i = 0; i < _radeon_debugfs_count; i++) {
894 if (_radeon_debugfs[i].files == files) {
895 /* Already registered */
896 return 0;
897 }
898 }
899 if ((_radeon_debugfs_count + nfiles) > RADEON_DEBUGFS_MAX_NUM_FILES) {
900 DRM_ERROR("Reached maximum number of debugfs files.\n");
901 DRM_ERROR("Report so we increase RADEON_DEBUGFS_MAX_NUM_FILES.\n");
902 return -EINVAL;
903 }
904 _radeon_debugfs[_radeon_debugfs_count].files = files;
905 _radeon_debugfs[_radeon_debugfs_count].num_files = nfiles;
906 _radeon_debugfs_count++;
907#if defined(CONFIG_DEBUG_FS)
908 drm_debugfs_create_files(files, nfiles,
909 rdev->ddev->control->debugfs_root,
910 rdev->ddev->control);
911 drm_debugfs_create_files(files, nfiles,
912 rdev->ddev->primary->debugfs_root,
913 rdev->ddev->primary);
914#endif
915 return 0;
916}
917
918#if defined(CONFIG_DEBUG_FS)
919int radeon_debugfs_init(struct drm_minor *minor)
920{
921 return 0;
922}
923
924void radeon_debugfs_cleanup(struct drm_minor *minor)
925{
926 unsigned i;
927
928 for (i = 0; i < _radeon_debugfs_count; i++) {
929 drm_debugfs_remove_files(_radeon_debugfs[i].files,
930 _radeon_debugfs[i].num_files, minor);
931 }
932}
933#endif