2 * Copyright 2008 Advanced Micro Devices, Inc.
3 * Copyright 2008 Red Hat Inc.
4 * Copyright 2009 Jerome Glisse.
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
24 * Authors: Dave Airlie
28 #include <linux/console.h>
29 #include <linux/slab.h>
31 #include <drm/drm_crtc_helper.h>
32 #include <drm/radeon_drm.h>
33 #include <linux/vgaarb.h>
34 #include <linux/vga_switcheroo.h>
35 #include <linux/efi.h>
36 #include "radeon_reg.h"
40 static const char radeon_family_name
[][16] = {
96 * Clear GPU surface registers.
98 void radeon_surface_init(struct radeon_device
*rdev
)
100 /* FIXME: check this out */
101 if (rdev
->family
< CHIP_R600
) {
104 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
);
108 radeon_clear_surface_reg(rdev
, i
);
110 /* enable surfaces */
111 WREG32(RADEON_SURFACE_CNTL
, 0);
116 * GPU scratch registers helpers function.
118 void radeon_scratch_init(struct radeon_device
*rdev
)
122 /* FIXME: check this out */
123 if (rdev
->family
< CHIP_R300
) {
124 rdev
->scratch
.num_reg
= 5;
126 rdev
->scratch
.num_reg
= 7;
128 rdev
->scratch
.reg_base
= RADEON_SCRATCH_REG0
;
129 for (i
= 0; i
< rdev
->scratch
.num_reg
; i
++) {
130 rdev
->scratch
.free
[i
] = true;
131 rdev
->scratch
.reg
[i
] = rdev
->scratch
.reg_base
+ (i
* 4);
135 int radeon_scratch_get(struct radeon_device
*rdev
, uint32_t *reg
)
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
];
149 void radeon_scratch_free(struct radeon_device
*rdev
, uint32_t reg
)
153 for (i
= 0; i
< rdev
->scratch
.num_reg
; i
++) {
154 if (rdev
->scratch
.reg
[i
] == reg
) {
155 rdev
->scratch
.free
[i
] = true;
161 void radeon_wb_disable(struct radeon_device
*rdev
)
165 if (rdev
->wb
.wb_obj
) {
166 r
= radeon_bo_reserve(rdev
->wb
.wb_obj
, false);
167 if (unlikely(r
!= 0))
169 radeon_bo_kunmap(rdev
->wb
.wb_obj
);
170 radeon_bo_unpin(rdev
->wb
.wb_obj
);
171 radeon_bo_unreserve(rdev
->wb
.wb_obj
);
173 rdev
->wb
.enabled
= false;
176 void radeon_wb_fini(struct radeon_device
*rdev
)
178 radeon_wb_disable(rdev
);
179 if (rdev
->wb
.wb_obj
) {
180 radeon_bo_unref(&rdev
->wb
.wb_obj
);
182 rdev
->wb
.wb_obj
= NULL
;
186 int radeon_wb_init(struct radeon_device
*rdev
)
190 if (rdev
->wb
.wb_obj
== NULL
) {
191 r
= radeon_bo_create(rdev
, RADEON_GPU_PAGE_SIZE
, PAGE_SIZE
, true,
192 RADEON_GEM_DOMAIN_GTT
, &rdev
->wb
.wb_obj
);
194 dev_warn(rdev
->dev
, "(%d) create WB bo failed\n", r
);
198 r
= radeon_bo_reserve(rdev
->wb
.wb_obj
, false);
199 if (unlikely(r
!= 0)) {
200 radeon_wb_fini(rdev
);
203 r
= radeon_bo_pin(rdev
->wb
.wb_obj
, RADEON_GEM_DOMAIN_GTT
,
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
);
211 r
= radeon_bo_kmap(rdev
->wb
.wb_obj
, (void **)&rdev
->wb
.wb
);
212 radeon_bo_unreserve(rdev
->wb
.wb_obj
);
214 dev_warn(rdev
->dev
, "(%d) map WB bo failed\n", r
);
215 radeon_wb_fini(rdev
);
219 /* clear wb memory */
220 memset((char *)rdev
->wb
.wb
, 0, RADEON_GPU_PAGE_SIZE
);
221 /* disable event_write fences */
222 rdev
->wb
.use_event
= false;
223 /* disabled via module param */
224 if (radeon_no_wb
== 1)
225 rdev
->wb
.enabled
= false;
227 /* often unreliable on AGP */
228 if (rdev
->flags
& RADEON_IS_AGP
) {
229 rdev
->wb
.enabled
= false;
231 rdev
->wb
.enabled
= true;
232 /* event_write fences are only available on r600+ */
233 if (rdev
->family
>= CHIP_R600
)
234 rdev
->wb
.use_event
= true;
237 /* always use writeback/events on NI */
238 if (ASIC_IS_DCE5(rdev
)) {
239 rdev
->wb
.enabled
= true;
240 rdev
->wb
.use_event
= true;
243 dev_info(rdev
->dev
, "WB %sabled\n", rdev
->wb
.enabled
? "en" : "dis");
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
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).
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.
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.
265 * This function will never fails, worst case are limiting VRAM.
267 * Note: GTT start, end, size should be initialized before calling this
268 * function on AGP platform.
270 * Note: We don't explicitly enforce VRAM start to be aligned on VRAM size,
271 * 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
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
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
284 * Note: IGP TOM addr should be the same as the aperture addr, we don't
285 * explicitly check for that thought.
287 * FIXME: when reducing VRAM size align new size on power of 2.
289 void radeon_vram_location(struct radeon_device
*rdev
, struct radeon_mc
*mc
, u64 base
)
291 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
;
297 mc
->vram_end
= mc
->vram_start
+ mc
->mc_vram_size
- 1;
298 if (rdev
->flags
& RADEON_IS_AGP
&& mc
->vram_end
> mc
->gtt_start
&& mc
->vram_start
<= mc
->gtt_end
) {
299 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
;
303 mc
->vram_end
= mc
->vram_start
+ mc
->mc_vram_size
- 1;
304 if (radeon_vram_limit
&& radeon_vram_limit
< mc
->real_vram_size
)
305 mc
->real_vram_size
= radeon_vram_limit
;
306 dev_info(rdev
->dev
, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n",
307 mc
->mc_vram_size
>> 20, mc
->vram_start
,
308 mc
->vram_end
, mc
->real_vram_size
>> 20);
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
316 * Function will place try to place GTT before or after VRAM.
318 * If GTT size is bigger than space left then we ajust GTT size.
319 * Thus function will never fails.
321 * FIXME: when reducing GTT size align new size on power of 2.
323 void radeon_gtt_location(struct radeon_device
*rdev
, struct radeon_mc
*mc
)
325 u64 size_af
, size_bf
;
327 size_af
= ((0xFFFFFFFF - mc
->vram_end
) + mc
->gtt_base_align
) & ~mc
->gtt_base_align
;
328 size_bf
= mc
->vram_start
& ~mc
->gtt_base_align
;
329 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
;
334 mc
->gtt_start
= (mc
->vram_start
& ~mc
->gtt_base_align
) - mc
->gtt_size
;
336 if (mc
->gtt_size
> size_af
) {
337 dev_warn(rdev
->dev
, "limiting GTT\n");
338 mc
->gtt_size
= size_af
;
340 mc
->gtt_start
= (mc
->vram_end
+ 1 + mc
->gtt_base_align
) & ~mc
->gtt_base_align
;
342 mc
->gtt_end
= mc
->gtt_start
+ mc
->gtt_size
- 1;
343 dev_info(rdev
->dev
, "GTT: %lluM 0x%016llX - 0x%016llX\n",
344 mc
->gtt_size
>> 20, mc
->gtt_start
, mc
->gtt_end
);
348 * GPU helpers function.
350 bool radeon_card_posted(struct radeon_device
*rdev
)
354 if (efi_enabled
&& rdev
->pdev
->subsystem_vendor
== PCI_VENDOR_ID_APPLE
)
357 /* first check CRTCs */
358 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
)
363 } else if (ASIC_IS_DCE4(rdev
)) {
364 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
)
372 } else if (ASIC_IS_AVIVO(rdev
)) {
373 reg
= RREG32(AVIVO_D1CRTC_CONTROL
) |
374 RREG32(AVIVO_D2CRTC_CONTROL
);
375 if (reg
& AVIVO_CRTC_EN
) {
379 reg
= RREG32(RADEON_CRTC_GEN_CNTL
) |
380 RREG32(RADEON_CRTC2_GEN_CNTL
);
381 if (reg
& RADEON_CRTC_EN
) {
386 /* then check MEM_SIZE, in case the crtcs are off */
387 if (rdev
->family
>= CHIP_R600
)
388 reg
= RREG32(R600_CONFIG_MEMSIZE
);
390 reg
= RREG32(RADEON_CONFIG_MEMSIZE
);
399 void radeon_update_bandwidth_info(struct radeon_device
*rdev
)
402 u32 sclk
= rdev
->pm
.current_sclk
;
403 u32 mclk
= rdev
->pm
.current_mclk
;
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
);
412 if (rdev
->flags
& RADEON_IS_IGP
) {
413 a
.full
= dfixed_const(16);
414 /* core_bandwidth = sclk(Mhz) * 16 */
415 rdev
->pm
.core_bandwidth
.full
= dfixed_div(rdev
->pm
.sclk
, a
);
419 bool radeon_boot_test_post_card(struct radeon_device
*rdev
)
421 if (radeon_card_posted(rdev
))
425 DRM_INFO("GPU not posted. posting now...\n");
426 if (rdev
->is_atom_bios
)
427 atom_asic_init(rdev
->mode_info
.atom_context
);
429 radeon_combios_asic_init(rdev
->ddev
);
432 dev_err(rdev
->dev
, "Card not posted and no BIOS - ignoring\n");
437 int radeon_dummy_page_init(struct radeon_device
*rdev
)
439 if (rdev
->dummy_page
.page
)
441 rdev
->dummy_page
.page
= alloc_page(GFP_DMA32
| GFP_KERNEL
| __GFP_ZERO
);
442 if (rdev
->dummy_page
.page
== NULL
)
444 rdev
->dummy_page
.addr
= pci_map_page(rdev
->pdev
, rdev
->dummy_page
.page
,
445 0, PAGE_SIZE
, PCI_DMA_BIDIRECTIONAL
);
446 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");
448 __free_page(rdev
->dummy_page
.page
);
449 rdev
->dummy_page
.page
= NULL
;
455 void radeon_dummy_page_fini(struct radeon_device
*rdev
)
457 if (rdev
->dummy_page
.page
== NULL
)
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
;
466 /* ATOM accessor methods */
467 static uint32_t cail_pll_read(struct card_info
*info
, uint32_t reg
)
469 struct radeon_device
*rdev
= info
->dev
->dev_private
;
472 r
= rdev
->pll_rreg(rdev
, reg
);
476 static void cail_pll_write(struct card_info
*info
, uint32_t reg
, uint32_t val
)
478 struct radeon_device
*rdev
= info
->dev
->dev_private
;
480 rdev
->pll_wreg(rdev
, reg
, val
);
483 static uint32_t cail_mc_read(struct card_info
*info
, uint32_t reg
)
485 struct radeon_device
*rdev
= info
->dev
->dev_private
;
488 r
= rdev
->mc_rreg(rdev
, reg
);
492 static void cail_mc_write(struct card_info
*info
, uint32_t reg
, uint32_t val
)
494 struct radeon_device
*rdev
= info
->dev
->dev_private
;
496 rdev
->mc_wreg(rdev
, reg
, val
);
499 static void cail_reg_write(struct card_info
*info
, uint32_t reg
, uint32_t val
)
501 struct radeon_device
*rdev
= info
->dev
->dev_private
;
506 static uint32_t cail_reg_read(struct card_info
*info
, uint32_t reg
)
508 struct radeon_device
*rdev
= info
->dev
->dev_private
;
515 static void cail_ioreg_write(struct card_info
*info
, uint32_t reg
, uint32_t val
)
517 struct radeon_device
*rdev
= info
->dev
->dev_private
;
519 WREG32_IO(reg
*4, val
);
522 static uint32_t cail_ioreg_read(struct card_info
*info
, uint32_t reg
)
524 struct radeon_device
*rdev
= info
->dev
->dev_private
;
527 r
= RREG32_IO(reg
*4);
531 int radeon_atombios_init(struct radeon_device
*rdev
)
533 struct card_info
*atom_card_info
=
534 kzalloc(sizeof(struct card_info
), GFP_KERNEL
);
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
;
543 /* needed for iio ops */
545 atom_card_info
->ioreg_read
= cail_ioreg_read
;
546 atom_card_info
->ioreg_write
= cail_ioreg_write
;
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
;
552 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
;
557 rdev
->mode_info
.atom_context
= atom_parse(atom_card_info
, rdev
->bios
);
558 mutex_init(&rdev
->mode_info
.atom_context
->mutex
);
559 radeon_atom_initialize_bios_scratch_regs(rdev
->ddev
);
560 atom_allocate_fb_scratch(rdev
->mode_info
.atom_context
);
564 void radeon_atombios_fini(struct radeon_device
*rdev
)
566 if (rdev
->mode_info
.atom_context
) {
567 kfree(rdev
->mode_info
.atom_context
->scratch
);
568 kfree(rdev
->mode_info
.atom_context
);
570 kfree(rdev
->mode_info
.atom_card_info
);
573 int radeon_combios_init(struct radeon_device
*rdev
)
575 radeon_combios_initialize_bios_scratch_regs(rdev
->ddev
);
579 void radeon_combios_fini(struct radeon_device
*rdev
)
583 /* if we get transitioned to only one device, tak VGA back */
584 static unsigned int radeon_vga_set_decode(void *cookie
, bool state
)
586 struct radeon_device
*rdev
= cookie
;
587 radeon_vga_set_state(rdev
, state
);
589 return VGA_RSRC_LEGACY_IO
| VGA_RSRC_LEGACY_MEM
|
590 VGA_RSRC_NORMAL_IO
| VGA_RSRC_NORMAL_MEM
;
592 return VGA_RSRC_NORMAL_IO
| VGA_RSRC_NORMAL_MEM
;
595 void radeon_check_arguments(struct radeon_device
*rdev
)
597 /* vramlimit must be a power of two */
598 switch (radeon_vram_limit
) {
613 dev_warn(rdev
->dev
, "vram limit (%d) must be a power of 2\n",
615 radeon_vram_limit
= 0;
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
) {
624 dev_warn(rdev
->dev
, "gart size (%d) too small forcing to 512M\n",
626 radeon_gart_size
= 512;
638 dev_warn(rdev
->dev
, "gart size (%d) must be a power of 2\n",
640 radeon_gart_size
= 512;
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
) {
654 dev_warn(rdev
->dev
, "invalid AGP mode %d (valid mode: "
655 "-1, 0, 1, 2, 4, 8)\n", radeon_agpmode
);
661 static void radeon_switcheroo_set_state(struct pci_dev
*pdev
, enum vga_switcheroo_state state
)
663 struct drm_device
*dev
= pci_get_drvdata(pdev
);
664 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 */
668 dev
->switch_power_state
= DRM_SWITCH_POWER_CHANGING
;
669 radeon_resume_kms(dev
);
670 dev
->switch_power_state
= DRM_SWITCH_POWER_ON
;
671 drm_kms_helper_poll_enable(dev
);
673 printk(KERN_INFO
"radeon: switched off\n");
674 drm_kms_helper_poll_disable(dev
);
675 dev
->switch_power_state
= DRM_SWITCH_POWER_CHANGING
;
676 radeon_suspend_kms(dev
, pmm
);
677 dev
->switch_power_state
= DRM_SWITCH_POWER_OFF
;
681 static bool radeon_switcheroo_can_switch(struct pci_dev
*pdev
)
683 struct drm_device
*dev
= pci_get_drvdata(pdev
);
686 spin_lock(&dev
->count_lock
);
687 can_switch
= (dev
->open_count
== 0);
688 spin_unlock(&dev
->count_lock
);
693 int radeon_device_init(struct radeon_device
*rdev
,
694 struct drm_device
*ddev
,
695 struct pci_dev
*pdev
,
701 rdev
->shutdown
= false;
702 rdev
->dev
= &pdev
->dev
;
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;
711 rdev
->accel_working
= false;
713 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
);
717 /* mutex initialization are all done here so we
718 * can recall function without having locking issues */
719 radeon_mutex_init(&rdev
->cs_mutex
);
720 mutex_init(&rdev
->ib_pool
.mutex
);
721 mutex_init(&rdev
->cp
.mutex
);
722 mutex_init(&rdev
->dc_hw_i2c_mutex
);
723 if (rdev
->family
>= CHIP_R600
)
724 spin_lock_init(&rdev
->ih
.lock
);
725 mutex_init(&rdev
->gem
.mutex
);
726 mutex_init(&rdev
->pm
.mutex
);
727 mutex_init(&rdev
->vram_mutex
);
728 rwlock_init(&rdev
->fence_lock
);
729 INIT_LIST_HEAD(&rdev
->gem
.objects
);
730 init_waitqueue_head(&rdev
->irq
.vblank_queue
);
731 init_waitqueue_head(&rdev
->irq
.idle_queue
);
733 /* Set asic functions */
734 r
= radeon_asic_init(rdev
);
737 radeon_check_arguments(rdev
);
739 /* all of the newer IGP chips have an internal gart
740 * However some rs4xx report as AGP, so remove that here.
742 if ((rdev
->family
>= CHIP_RS400
) &&
743 (rdev
->flags
& RADEON_IS_IGP
)) {
744 rdev
->flags
&= ~RADEON_IS_AGP
;
747 if (rdev
->flags
& RADEON_IS_AGP
&& radeon_agpmode
== -1) {
748 radeon_agp_disable(rdev
);
751 /* set DMA mask + need_dma32 flags.
752 * PCIE - can handle 40-bits.
753 * IGP - can handle 40-bits
754 * AGP - generally dma32 is safest
755 * PCI - dma32 for legacy pci gart, 40 bits on newer asics
757 rdev
->need_dma32
= false;
758 if (rdev
->flags
& RADEON_IS_AGP
)
759 rdev
->need_dma32
= true;
760 if ((rdev
->flags
& RADEON_IS_PCI
) &&
761 (rdev
->family
< CHIP_RS400
))
762 rdev
->need_dma32
= true;
764 dma_bits
= rdev
->need_dma32
? 32 : 40;
765 r
= pci_set_dma_mask(rdev
->pdev
, DMA_BIT_MASK(dma_bits
));
767 rdev
->need_dma32
= true;
769 printk(KERN_WARNING
"radeon: No suitable DMA available.\n");
771 r
= pci_set_consistent_dma_mask(rdev
->pdev
, DMA_BIT_MASK(dma_bits
));
773 pci_set_consistent_dma_mask(rdev
->pdev
, DMA_BIT_MASK(32));
774 printk(KERN_WARNING
"radeon: No coherent DMA available.\n");
777 /* Registers mapping */
778 /* TODO: block userspace mapping of io register */
779 rdev
->rmmio_base
= pci_resource_start(rdev
->pdev
, 2);
780 rdev
->rmmio_size
= pci_resource_len(rdev
->pdev
, 2);
781 rdev
->rmmio
= ioremap(rdev
->rmmio_base
, rdev
->rmmio_size
);
782 if (rdev
->rmmio
== NULL
) {
785 DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)rdev
->rmmio_base
);
786 DRM_INFO("register mmio size: %u\n", (unsigned)rdev
->rmmio_size
);
788 /* io port mapping */
789 for (i
= 0; i
< DEVICE_COUNT_RESOURCE
; i
++) {
790 if (pci_resource_flags(rdev
->pdev
, i
) & IORESOURCE_IO
) {
791 rdev
->rio_mem_size
= pci_resource_len(rdev
->pdev
, i
);
792 rdev
->rio_mem
= pci_iomap(rdev
->pdev
, i
, rdev
->rio_mem_size
);
796 if (rdev
->rio_mem
== NULL
)
797 DRM_ERROR("Unable to find PCI I/O BAR\n");
799 /* if we have > 1 VGA cards, then disable the radeon VGA resources */
800 /* this will fail for cards that aren't VGA class devices, just
802 vga_client_register(rdev
->pdev
, rdev
, NULL
, radeon_vga_set_decode
);
803 vga_switcheroo_register_client(rdev
->pdev
,
804 radeon_switcheroo_set_state
,
806 radeon_switcheroo_can_switch
);
808 r
= radeon_init(rdev
);
812 if (rdev
->flags
& RADEON_IS_AGP
&& !rdev
->accel_working
) {
813 /* Acceleration not working on AGP card try again
814 * with fallback to PCI or PCIE GART
816 radeon_asic_reset(rdev
);
818 radeon_agp_disable(rdev
);
819 r
= radeon_init(rdev
);
823 if (radeon_testing
) {
824 radeon_test_moves(rdev
);
826 if (radeon_benchmarking
) {
827 radeon_benchmark(rdev
, radeon_benchmarking
);
832 static void radeon_debugfs_remove_files(struct radeon_device
*rdev
);
834 void radeon_device_fini(struct radeon_device
*rdev
)
836 DRM_INFO("radeon: finishing device.\n");
837 rdev
->shutdown
= true;
838 /* evict vram memory */
839 radeon_bo_evict_vram(rdev
);
841 vga_switcheroo_unregister_client(rdev
->pdev
);
842 vga_client_register(rdev
->pdev
, NULL
, NULL
, NULL
);
844 pci_iounmap(rdev
->pdev
, rdev
->rio_mem
);
845 rdev
->rio_mem
= NULL
;
846 iounmap(rdev
->rmmio
);
848 radeon_debugfs_remove_files(rdev
);
855 int radeon_suspend_kms(struct drm_device
*dev
, pm_message_t state
)
857 struct radeon_device
*rdev
;
858 struct drm_crtc
*crtc
;
859 struct drm_connector
*connector
;
862 if (dev
== NULL
|| dev
->dev_private
== NULL
) {
865 if (state
.event
== PM_EVENT_PRETHAW
) {
868 rdev
= dev
->dev_private
;
870 if (dev
->switch_power_state
== DRM_SWITCH_POWER_OFF
)
873 /* turn off display hw */
874 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
) {
875 drm_helper_connector_dpms(connector
, DRM_MODE_DPMS_OFF
);
878 /* unpin the front buffers */
879 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
, head
) {
880 struct radeon_framebuffer
*rfb
= to_radeon_framebuffer(crtc
->fb
);
881 struct radeon_bo
*robj
;
883 if (rfb
== NULL
|| rfb
->obj
== NULL
) {
886 robj
= gem_to_radeon_bo(rfb
->obj
);
887 /* don't unpin kernel fb objects */
888 if (!radeon_fbdev_robj_is_fb(rdev
, robj
)) {
889 r
= radeon_bo_reserve(robj
, false);
891 radeon_bo_unpin(robj
);
892 radeon_bo_unreserve(robj
);
896 /* evict vram memory */
897 radeon_bo_evict_vram(rdev
);
898 /* wait for gpu to finish processing current batch */
899 for (i
= 0; i
< RADEON_NUM_RINGS
; i
++)
900 radeon_fence_wait_last(rdev
, i
);
902 radeon_save_bios_scratch_regs(rdev
);
904 radeon_pm_suspend(rdev
);
905 radeon_suspend(rdev
);
906 radeon_hpd_fini(rdev
);
907 /* evict remaining vram memory */
908 radeon_bo_evict_vram(rdev
);
910 radeon_agp_suspend(rdev
);
912 pci_save_state(dev
->pdev
);
913 if (state
.event
== PM_EVENT_SUSPEND
) {
914 /* Shut down the device */
915 pci_disable_device(dev
->pdev
);
916 pci_set_power_state(dev
->pdev
, PCI_D3hot
);
919 radeon_fbdev_set_suspend(rdev
, 1);
924 int radeon_resume_kms(struct drm_device
*dev
)
926 struct drm_connector
*connector
;
927 struct radeon_device
*rdev
= dev
->dev_private
;
929 if (dev
->switch_power_state
== DRM_SWITCH_POWER_OFF
)
933 pci_set_power_state(dev
->pdev
, PCI_D0
);
934 pci_restore_state(dev
->pdev
);
935 if (pci_enable_device(dev
->pdev
)) {
939 pci_set_master(dev
->pdev
);
940 /* resume AGP if in use */
941 radeon_agp_resume(rdev
);
943 radeon_pm_resume(rdev
);
944 radeon_restore_bios_scratch_regs(rdev
);
946 radeon_fbdev_set_suspend(rdev
, 0);
950 if (rdev
->is_atom_bios
)
951 radeon_atom_encoder_init(rdev
);
952 /* reset hpd state */
953 radeon_hpd_init(rdev
);
954 /* blat the mode back in */
955 drm_helper_resume_force_mode(dev
);
956 /* turn on display hw */
957 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
) {
958 drm_helper_connector_dpms(connector
, DRM_MODE_DPMS_ON
);
963 int radeon_gpu_reset(struct radeon_device
*rdev
)
968 /* Prevent CS ioctl from interfering */
969 radeon_mutex_lock(&rdev
->cs_mutex
);
971 radeon_save_bios_scratch_regs(rdev
);
973 resched
= ttm_bo_lock_delayed_workqueue(&rdev
->mman
.bdev
);
974 radeon_suspend(rdev
);
976 r
= radeon_asic_reset(rdev
);
978 dev_info(rdev
->dev
, "GPU reset succeed\n");
980 radeon_restore_bios_scratch_regs(rdev
);
981 drm_helper_resume_force_mode(rdev
->ddev
);
982 ttm_bo_unlock_delayed_workqueue(&rdev
->mman
.bdev
, resched
);
985 radeon_mutex_unlock(&rdev
->cs_mutex
);
988 /* bad news, how to tell it to userspace ? */
989 dev_info(rdev
->dev
, "GPU reset failed\n");
999 int radeon_debugfs_add_files(struct radeon_device
*rdev
,
1000 struct drm_info_list
*files
,
1005 for (i
= 0; i
< rdev
->debugfs_count
; i
++) {
1006 if (rdev
->debugfs
[i
].files
== files
) {
1007 /* Already registered */
1012 i
= rdev
->debugfs_count
+ 1;
1013 if (i
> RADEON_DEBUGFS_MAX_COMPONENTS
) {
1014 DRM_ERROR("Reached maximum number of debugfs components.\n");
1015 DRM_ERROR("Report so we increase "
1016 "RADEON_DEBUGFS_MAX_COMPONENTS.\n");
1019 rdev
->debugfs
[rdev
->debugfs_count
].files
= files
;
1020 rdev
->debugfs
[rdev
->debugfs_count
].num_files
= nfiles
;
1021 rdev
->debugfs_count
= i
;
1022 #if defined(CONFIG_DEBUG_FS)
1023 drm_debugfs_create_files(files
, nfiles
,
1024 rdev
->ddev
->control
->debugfs_root
,
1025 rdev
->ddev
->control
);
1026 drm_debugfs_create_files(files
, nfiles
,
1027 rdev
->ddev
->primary
->debugfs_root
,
1028 rdev
->ddev
->primary
);
1033 static void radeon_debugfs_remove_files(struct radeon_device
*rdev
)
1035 #if defined(CONFIG_DEBUG_FS)
1038 for (i
= 0; i
< rdev
->debugfs_count
; i
++) {
1039 drm_debugfs_remove_files(rdev
->debugfs
[i
].files
,
1040 rdev
->debugfs
[i
].num_files
,
1041 rdev
->ddev
->control
);
1042 drm_debugfs_remove_files(rdev
->debugfs
[i
].files
,
1043 rdev
->debugfs
[i
].num_files
,
1044 rdev
->ddev
->primary
);
1049 #if defined(CONFIG_DEBUG_FS)
1050 int radeon_debugfs_init(struct drm_minor
*minor
)
1055 void radeon_debugfs_cleanup(struct drm_minor
*minor
)