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 "radeon_reg.h"
39 static const char radeon_family_name
[][16] = {
95 * Clear GPU surface registers.
97 void radeon_surface_init(struct radeon_device
*rdev
)
99 /* FIXME: check this out */
100 if (rdev
->family
< CHIP_R600
) {
103 for (i
= 0; i
< RADEON_GEM_MAX_SURFACES
; i
++) {
104 if (rdev
->surface_regs
[i
].bo
)
105 radeon_bo_get_surface_reg(rdev
->surface_regs
[i
].bo
);
107 radeon_clear_surface_reg(rdev
, i
);
109 /* enable surfaces */
110 WREG32(RADEON_SURFACE_CNTL
, 0);
115 * GPU scratch registers helpers function.
117 void radeon_scratch_init(struct radeon_device
*rdev
)
121 /* FIXME: check this out */
122 if (rdev
->family
< CHIP_R300
) {
123 rdev
->scratch
.num_reg
= 5;
125 rdev
->scratch
.num_reg
= 7;
127 rdev
->scratch
.reg_base
= RADEON_SCRATCH_REG0
;
128 for (i
= 0; i
< rdev
->scratch
.num_reg
; i
++) {
129 rdev
->scratch
.free
[i
] = true;
130 rdev
->scratch
.reg
[i
] = rdev
->scratch
.reg_base
+ (i
* 4);
134 int radeon_scratch_get(struct radeon_device
*rdev
, uint32_t *reg
)
138 for (i
= 0; i
< rdev
->scratch
.num_reg
; i
++) {
139 if (rdev
->scratch
.free
[i
]) {
140 rdev
->scratch
.free
[i
] = false;
141 *reg
= rdev
->scratch
.reg
[i
];
148 void radeon_scratch_free(struct radeon_device
*rdev
, uint32_t reg
)
152 for (i
= 0; i
< rdev
->scratch
.num_reg
; i
++) {
153 if (rdev
->scratch
.reg
[i
] == reg
) {
154 rdev
->scratch
.free
[i
] = true;
160 void radeon_wb_disable(struct radeon_device
*rdev
)
164 if (rdev
->wb
.wb_obj
) {
165 r
= radeon_bo_reserve(rdev
->wb
.wb_obj
, false);
166 if (unlikely(r
!= 0))
168 radeon_bo_kunmap(rdev
->wb
.wb_obj
);
169 radeon_bo_unpin(rdev
->wb
.wb_obj
);
170 radeon_bo_unreserve(rdev
->wb
.wb_obj
);
172 rdev
->wb
.enabled
= false;
175 void radeon_wb_fini(struct radeon_device
*rdev
)
177 radeon_wb_disable(rdev
);
178 if (rdev
->wb
.wb_obj
) {
179 radeon_bo_unref(&rdev
->wb
.wb_obj
);
181 rdev
->wb
.wb_obj
= NULL
;
185 int radeon_wb_init(struct radeon_device
*rdev
)
189 if (rdev
->wb
.wb_obj
== NULL
) {
190 r
= radeon_bo_create(rdev
, RADEON_GPU_PAGE_SIZE
, PAGE_SIZE
, true,
191 RADEON_GEM_DOMAIN_GTT
, &rdev
->wb
.wb_obj
);
193 dev_warn(rdev
->dev
, "(%d) create WB bo failed\n", r
);
197 r
= radeon_bo_reserve(rdev
->wb
.wb_obj
, false);
198 if (unlikely(r
!= 0)) {
199 radeon_wb_fini(rdev
);
202 r
= radeon_bo_pin(rdev
->wb
.wb_obj
, RADEON_GEM_DOMAIN_GTT
,
205 radeon_bo_unreserve(rdev
->wb
.wb_obj
);
206 dev_warn(rdev
->dev
, "(%d) pin WB bo failed\n", r
);
207 radeon_wb_fini(rdev
);
210 r
= radeon_bo_kmap(rdev
->wb
.wb_obj
, (void **)&rdev
->wb
.wb
);
211 radeon_bo_unreserve(rdev
->wb
.wb_obj
);
213 dev_warn(rdev
->dev
, "(%d) map WB bo failed\n", r
);
214 radeon_wb_fini(rdev
);
218 /* clear wb memory */
219 memset((char *)rdev
->wb
.wb
, 0, RADEON_GPU_PAGE_SIZE
);
220 /* disable event_write fences */
221 rdev
->wb
.use_event
= false;
222 /* disabled via module param */
223 if (radeon_no_wb
== 1)
224 rdev
->wb
.enabled
= false;
226 /* often unreliable on AGP */
227 if (rdev
->flags
& RADEON_IS_AGP
) {
228 rdev
->wb
.enabled
= false;
230 rdev
->wb
.enabled
= true;
231 /* event_write fences are only available on r600+ */
232 if (rdev
->family
>= CHIP_R600
)
233 rdev
->wb
.use_event
= true;
236 /* always use writeback/events on NI */
237 if (ASIC_IS_DCE5(rdev
)) {
238 rdev
->wb
.enabled
= true;
239 rdev
->wb
.use_event
= true;
242 dev_info(rdev
->dev
, "WB %sabled\n", rdev
->wb
.enabled
? "en" : "dis");
248 * radeon_vram_location - try to find VRAM location
249 * @rdev: radeon device structure holding all necessary informations
250 * @mc: memory controller structure holding memory informations
251 * @base: base address at which to put VRAM
253 * Function will place try to place VRAM at base address provided
254 * as parameter (which is so far either PCI aperture address or
255 * for IGP TOM base address).
257 * If there is not enough space to fit the unvisible VRAM in the 32bits
258 * address space then we limit the VRAM size to the aperture.
260 * If we are using AGP and if the AGP aperture doesn't allow us to have
261 * room for all the VRAM than we restrict the VRAM to the PCI aperture
262 * size and print a warning.
264 * This function will never fails, worst case are limiting VRAM.
266 * Note: GTT start, end, size should be initialized before calling this
267 * function on AGP platform.
269 * Note: We don't explicitly enforce VRAM start to be aligned on VRAM size,
270 * this shouldn't be a problem as we are using the PCI aperture as a reference.
271 * Otherwise this would be needed for rv280, all r3xx, and all r4xx, but
274 * Note: we use mc_vram_size as on some board we need to program the mc to
275 * cover the whole aperture even if VRAM size is inferior to aperture size
276 * Novell bug 204882 + along with lots of ubuntu ones
278 * Note: when limiting vram it's safe to overwritte real_vram_size because
279 * we are not in case where real_vram_size is inferior to mc_vram_size (ie
280 * note afected by bogus hw of Novell bug 204882 + along with lots of ubuntu
283 * Note: IGP TOM addr should be the same as the aperture addr, we don't
284 * explicitly check for that thought.
286 * FIXME: when reducing VRAM size align new size on power of 2.
288 void radeon_vram_location(struct radeon_device
*rdev
, struct radeon_mc
*mc
, u64 base
)
290 mc
->vram_start
= base
;
291 if (mc
->mc_vram_size
> (0xFFFFFFFF - base
+ 1)) {
292 dev_warn(rdev
->dev
, "limiting VRAM to PCI aperture size\n");
293 mc
->real_vram_size
= mc
->aper_size
;
294 mc
->mc_vram_size
= mc
->aper_size
;
296 mc
->vram_end
= mc
->vram_start
+ mc
->mc_vram_size
- 1;
297 if (rdev
->flags
& RADEON_IS_AGP
&& mc
->vram_end
> mc
->gtt_start
&& mc
->vram_start
<= mc
->gtt_end
) {
298 dev_warn(rdev
->dev
, "limiting VRAM to PCI aperture size\n");
299 mc
->real_vram_size
= mc
->aper_size
;
300 mc
->mc_vram_size
= mc
->aper_size
;
302 mc
->vram_end
= mc
->vram_start
+ mc
->mc_vram_size
- 1;
303 dev_info(rdev
->dev
, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n",
304 mc
->mc_vram_size
>> 20, mc
->vram_start
,
305 mc
->vram_end
, mc
->real_vram_size
>> 20);
309 * radeon_gtt_location - try to find GTT location
310 * @rdev: radeon device structure holding all necessary informations
311 * @mc: memory controller structure holding memory informations
313 * Function will place try to place GTT before or after VRAM.
315 * If GTT size is bigger than space left then we ajust GTT size.
316 * Thus function will never fails.
318 * FIXME: when reducing GTT size align new size on power of 2.
320 void radeon_gtt_location(struct radeon_device
*rdev
, struct radeon_mc
*mc
)
322 u64 size_af
, size_bf
;
324 size_af
= ((0xFFFFFFFF - mc
->vram_end
) + mc
->gtt_base_align
) & ~mc
->gtt_base_align
;
325 size_bf
= mc
->vram_start
& ~mc
->gtt_base_align
;
326 if (size_bf
> size_af
) {
327 if (mc
->gtt_size
> size_bf
) {
328 dev_warn(rdev
->dev
, "limiting GTT\n");
329 mc
->gtt_size
= size_bf
;
331 mc
->gtt_start
= (mc
->vram_start
& ~mc
->gtt_base_align
) - mc
->gtt_size
;
333 if (mc
->gtt_size
> size_af
) {
334 dev_warn(rdev
->dev
, "limiting GTT\n");
335 mc
->gtt_size
= size_af
;
337 mc
->gtt_start
= (mc
->vram_end
+ 1 + mc
->gtt_base_align
) & ~mc
->gtt_base_align
;
339 mc
->gtt_end
= mc
->gtt_start
+ mc
->gtt_size
- 1;
340 dev_info(rdev
->dev
, "GTT: %lluM 0x%016llX - 0x%016llX\n",
341 mc
->gtt_size
>> 20, mc
->gtt_start
, mc
->gtt_end
);
345 * GPU helpers function.
347 bool radeon_card_posted(struct radeon_device
*rdev
)
351 /* first check CRTCs */
352 if (ASIC_IS_DCE41(rdev
)) {
353 reg
= RREG32(EVERGREEN_CRTC_CONTROL
+ EVERGREEN_CRTC0_REGISTER_OFFSET
) |
354 RREG32(EVERGREEN_CRTC_CONTROL
+ EVERGREEN_CRTC1_REGISTER_OFFSET
);
355 if (reg
& EVERGREEN_CRTC_MASTER_EN
)
357 } else if (ASIC_IS_DCE4(rdev
)) {
358 reg
= RREG32(EVERGREEN_CRTC_CONTROL
+ EVERGREEN_CRTC0_REGISTER_OFFSET
) |
359 RREG32(EVERGREEN_CRTC_CONTROL
+ EVERGREEN_CRTC1_REGISTER_OFFSET
) |
360 RREG32(EVERGREEN_CRTC_CONTROL
+ EVERGREEN_CRTC2_REGISTER_OFFSET
) |
361 RREG32(EVERGREEN_CRTC_CONTROL
+ EVERGREEN_CRTC3_REGISTER_OFFSET
) |
362 RREG32(EVERGREEN_CRTC_CONTROL
+ EVERGREEN_CRTC4_REGISTER_OFFSET
) |
363 RREG32(EVERGREEN_CRTC_CONTROL
+ EVERGREEN_CRTC5_REGISTER_OFFSET
);
364 if (reg
& EVERGREEN_CRTC_MASTER_EN
)
366 } else if (ASIC_IS_AVIVO(rdev
)) {
367 reg
= RREG32(AVIVO_D1CRTC_CONTROL
) |
368 RREG32(AVIVO_D2CRTC_CONTROL
);
369 if (reg
& AVIVO_CRTC_EN
) {
373 reg
= RREG32(RADEON_CRTC_GEN_CNTL
) |
374 RREG32(RADEON_CRTC2_GEN_CNTL
);
375 if (reg
& RADEON_CRTC_EN
) {
380 /* then check MEM_SIZE, in case the crtcs are off */
381 if (rdev
->family
>= CHIP_R600
)
382 reg
= RREG32(R600_CONFIG_MEMSIZE
);
384 reg
= RREG32(RADEON_CONFIG_MEMSIZE
);
393 void radeon_update_bandwidth_info(struct radeon_device
*rdev
)
396 u32 sclk
= rdev
->pm
.current_sclk
;
397 u32 mclk
= rdev
->pm
.current_mclk
;
399 /* sclk/mclk in Mhz */
400 a
.full
= dfixed_const(100);
401 rdev
->pm
.sclk
.full
= dfixed_const(sclk
);
402 rdev
->pm
.sclk
.full
= dfixed_div(rdev
->pm
.sclk
, a
);
403 rdev
->pm
.mclk
.full
= dfixed_const(mclk
);
404 rdev
->pm
.mclk
.full
= dfixed_div(rdev
->pm
.mclk
, a
);
406 if (rdev
->flags
& RADEON_IS_IGP
) {
407 a
.full
= dfixed_const(16);
408 /* core_bandwidth = sclk(Mhz) * 16 */
409 rdev
->pm
.core_bandwidth
.full
= dfixed_div(rdev
->pm
.sclk
, a
);
413 bool radeon_boot_test_post_card(struct radeon_device
*rdev
)
415 if (radeon_card_posted(rdev
))
419 DRM_INFO("GPU not posted. posting now...\n");
420 if (rdev
->is_atom_bios
)
421 atom_asic_init(rdev
->mode_info
.atom_context
);
423 radeon_combios_asic_init(rdev
->ddev
);
426 dev_err(rdev
->dev
, "Card not posted and no BIOS - ignoring\n");
431 int radeon_dummy_page_init(struct radeon_device
*rdev
)
433 if (rdev
->dummy_page
.page
)
435 rdev
->dummy_page
.page
= alloc_page(GFP_DMA32
| GFP_KERNEL
| __GFP_ZERO
);
436 if (rdev
->dummy_page
.page
== NULL
)
438 rdev
->dummy_page
.addr
= pci_map_page(rdev
->pdev
, rdev
->dummy_page
.page
,
439 0, PAGE_SIZE
, PCI_DMA_BIDIRECTIONAL
);
440 if (pci_dma_mapping_error(rdev
->pdev
, rdev
->dummy_page
.addr
)) {
441 dev_err(&rdev
->pdev
->dev
, "Failed to DMA MAP the dummy page\n");
442 __free_page(rdev
->dummy_page
.page
);
443 rdev
->dummy_page
.page
= NULL
;
449 void radeon_dummy_page_fini(struct radeon_device
*rdev
)
451 if (rdev
->dummy_page
.page
== NULL
)
453 pci_unmap_page(rdev
->pdev
, rdev
->dummy_page
.addr
,
454 PAGE_SIZE
, PCI_DMA_BIDIRECTIONAL
);
455 __free_page(rdev
->dummy_page
.page
);
456 rdev
->dummy_page
.page
= NULL
;
460 /* ATOM accessor methods */
461 static uint32_t cail_pll_read(struct card_info
*info
, uint32_t reg
)
463 struct radeon_device
*rdev
= info
->dev
->dev_private
;
466 r
= rdev
->pll_rreg(rdev
, reg
);
470 static void cail_pll_write(struct card_info
*info
, uint32_t reg
, uint32_t val
)
472 struct radeon_device
*rdev
= info
->dev
->dev_private
;
474 rdev
->pll_wreg(rdev
, reg
, val
);
477 static uint32_t cail_mc_read(struct card_info
*info
, uint32_t reg
)
479 struct radeon_device
*rdev
= info
->dev
->dev_private
;
482 r
= rdev
->mc_rreg(rdev
, reg
);
486 static void cail_mc_write(struct card_info
*info
, uint32_t reg
, uint32_t val
)
488 struct radeon_device
*rdev
= info
->dev
->dev_private
;
490 rdev
->mc_wreg(rdev
, reg
, val
);
493 static void cail_reg_write(struct card_info
*info
, uint32_t reg
, uint32_t val
)
495 struct radeon_device
*rdev
= info
->dev
->dev_private
;
500 static uint32_t cail_reg_read(struct card_info
*info
, uint32_t reg
)
502 struct radeon_device
*rdev
= info
->dev
->dev_private
;
509 static void cail_ioreg_write(struct card_info
*info
, uint32_t reg
, uint32_t val
)
511 struct radeon_device
*rdev
= info
->dev
->dev_private
;
513 WREG32_IO(reg
*4, val
);
516 static uint32_t cail_ioreg_read(struct card_info
*info
, uint32_t reg
)
518 struct radeon_device
*rdev
= info
->dev
->dev_private
;
521 r
= RREG32_IO(reg
*4);
525 int radeon_atombios_init(struct radeon_device
*rdev
)
527 struct card_info
*atom_card_info
=
528 kzalloc(sizeof(struct card_info
), GFP_KERNEL
);
533 rdev
->mode_info
.atom_card_info
= atom_card_info
;
534 atom_card_info
->dev
= rdev
->ddev
;
535 atom_card_info
->reg_read
= cail_reg_read
;
536 atom_card_info
->reg_write
= cail_reg_write
;
537 /* needed for iio ops */
539 atom_card_info
->ioreg_read
= cail_ioreg_read
;
540 atom_card_info
->ioreg_write
= cail_ioreg_write
;
542 DRM_ERROR("Unable to find PCI I/O BAR; using MMIO for ATOM IIO\n");
543 atom_card_info
->ioreg_read
= cail_reg_read
;
544 atom_card_info
->ioreg_write
= cail_reg_write
;
546 atom_card_info
->mc_read
= cail_mc_read
;
547 atom_card_info
->mc_write
= cail_mc_write
;
548 atom_card_info
->pll_read
= cail_pll_read
;
549 atom_card_info
->pll_write
= cail_pll_write
;
551 rdev
->mode_info
.atom_context
= atom_parse(atom_card_info
, rdev
->bios
);
552 mutex_init(&rdev
->mode_info
.atom_context
->mutex
);
553 radeon_atom_initialize_bios_scratch_regs(rdev
->ddev
);
554 atom_allocate_fb_scratch(rdev
->mode_info
.atom_context
);
558 void radeon_atombios_fini(struct radeon_device
*rdev
)
560 if (rdev
->mode_info
.atom_context
) {
561 kfree(rdev
->mode_info
.atom_context
->scratch
);
562 kfree(rdev
->mode_info
.atom_context
);
564 kfree(rdev
->mode_info
.atom_card_info
);
567 int radeon_combios_init(struct radeon_device
*rdev
)
569 radeon_combios_initialize_bios_scratch_regs(rdev
->ddev
);
573 void radeon_combios_fini(struct radeon_device
*rdev
)
577 /* if we get transitioned to only one device, tak VGA back */
578 static unsigned int radeon_vga_set_decode(void *cookie
, bool state
)
580 struct radeon_device
*rdev
= cookie
;
581 radeon_vga_set_state(rdev
, state
);
583 return VGA_RSRC_LEGACY_IO
| VGA_RSRC_LEGACY_MEM
|
584 VGA_RSRC_NORMAL_IO
| VGA_RSRC_NORMAL_MEM
;
586 return VGA_RSRC_NORMAL_IO
| VGA_RSRC_NORMAL_MEM
;
589 void radeon_check_arguments(struct radeon_device
*rdev
)
591 /* vramlimit must be a power of two */
592 switch (radeon_vram_limit
) {
607 dev_warn(rdev
->dev
, "vram limit (%d) must be a power of 2\n",
609 radeon_vram_limit
= 0;
612 radeon_vram_limit
= radeon_vram_limit
<< 20;
613 /* gtt size must be power of two and greater or equal to 32M */
614 switch (radeon_gart_size
) {
618 dev_warn(rdev
->dev
, "gart size (%d) too small forcing to 512M\n",
620 radeon_gart_size
= 512;
632 dev_warn(rdev
->dev
, "gart size (%d) must be a power of 2\n",
634 radeon_gart_size
= 512;
637 rdev
->mc
.gtt_size
= radeon_gart_size
* 1024 * 1024;
638 /* AGP mode can only be -1, 1, 2, 4, 8 */
639 switch (radeon_agpmode
) {
648 dev_warn(rdev
->dev
, "invalid AGP mode %d (valid mode: "
649 "-1, 0, 1, 2, 4, 8)\n", radeon_agpmode
);
655 static void radeon_switcheroo_set_state(struct pci_dev
*pdev
, enum vga_switcheroo_state state
)
657 struct drm_device
*dev
= pci_get_drvdata(pdev
);
658 pm_message_t pmm
= { .event
= PM_EVENT_SUSPEND
};
659 if (state
== VGA_SWITCHEROO_ON
) {
660 printk(KERN_INFO
"radeon: switched on\n");
661 /* don't suspend or resume card normally */
662 dev
->switch_power_state
= DRM_SWITCH_POWER_CHANGING
;
663 radeon_resume_kms(dev
);
664 dev
->switch_power_state
= DRM_SWITCH_POWER_ON
;
665 drm_kms_helper_poll_enable(dev
);
667 printk(KERN_INFO
"radeon: switched off\n");
668 drm_kms_helper_poll_disable(dev
);
669 dev
->switch_power_state
= DRM_SWITCH_POWER_CHANGING
;
670 radeon_suspend_kms(dev
, pmm
);
671 dev
->switch_power_state
= DRM_SWITCH_POWER_OFF
;
675 static bool radeon_switcheroo_can_switch(struct pci_dev
*pdev
)
677 struct drm_device
*dev
= pci_get_drvdata(pdev
);
680 spin_lock(&dev
->count_lock
);
681 can_switch
= (dev
->open_count
== 0);
682 spin_unlock(&dev
->count_lock
);
687 int radeon_device_init(struct radeon_device
*rdev
,
688 struct drm_device
*ddev
,
689 struct pci_dev
*pdev
,
695 rdev
->shutdown
= false;
696 rdev
->dev
= &pdev
->dev
;
700 rdev
->family
= flags
& RADEON_FAMILY_MASK
;
701 rdev
->is_atom_bios
= false;
702 rdev
->usec_timeout
= RADEON_MAX_USEC_TIMEOUT
;
703 rdev
->mc
.gtt_size
= radeon_gart_size
* 1024 * 1024;
704 rdev
->gpu_lockup
= false;
705 rdev
->accel_working
= false;
707 DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X).\n",
708 radeon_family_name
[rdev
->family
], pdev
->vendor
, pdev
->device
,
709 pdev
->subsystem_vendor
, pdev
->subsystem_device
);
711 /* mutex initialization are all done here so we
712 * can recall function without having locking issues */
713 mutex_init(&rdev
->cs_mutex
);
714 mutex_init(&rdev
->ib_pool
.mutex
);
715 mutex_init(&rdev
->cp
.mutex
);
716 mutex_init(&rdev
->dc_hw_i2c_mutex
);
717 if (rdev
->family
>= CHIP_R600
)
718 spin_lock_init(&rdev
->ih
.lock
);
719 mutex_init(&rdev
->gem
.mutex
);
720 mutex_init(&rdev
->pm
.mutex
);
721 mutex_init(&rdev
->vram_mutex
);
722 rwlock_init(&rdev
->fence_drv
.lock
);
723 INIT_LIST_HEAD(&rdev
->gem
.objects
);
724 init_waitqueue_head(&rdev
->irq
.vblank_queue
);
725 init_waitqueue_head(&rdev
->irq
.idle_queue
);
727 /* Set asic functions */
728 r
= radeon_asic_init(rdev
);
731 radeon_check_arguments(rdev
);
733 /* all of the newer IGP chips have an internal gart
734 * However some rs4xx report as AGP, so remove that here.
736 if ((rdev
->family
>= CHIP_RS400
) &&
737 (rdev
->flags
& RADEON_IS_IGP
)) {
738 rdev
->flags
&= ~RADEON_IS_AGP
;
741 if (rdev
->flags
& RADEON_IS_AGP
&& radeon_agpmode
== -1) {
742 radeon_agp_disable(rdev
);
745 /* set DMA mask + need_dma32 flags.
746 * PCIE - can handle 40-bits.
747 * IGP - can handle 40-bits (in theory)
748 * AGP - generally dma32 is safest
751 rdev
->need_dma32
= false;
752 if (rdev
->flags
& RADEON_IS_AGP
)
753 rdev
->need_dma32
= true;
754 if (rdev
->flags
& RADEON_IS_PCI
)
755 rdev
->need_dma32
= true;
757 dma_bits
= rdev
->need_dma32
? 32 : 40;
758 r
= pci_set_dma_mask(rdev
->pdev
, DMA_BIT_MASK(dma_bits
));
760 rdev
->need_dma32
= true;
761 printk(KERN_WARNING
"radeon: No suitable DMA available.\n");
764 /* Registers mapping */
765 /* TODO: block userspace mapping of io register */
766 rdev
->rmmio_base
= pci_resource_start(rdev
->pdev
, 2);
767 rdev
->rmmio_size
= pci_resource_len(rdev
->pdev
, 2);
768 rdev
->rmmio
= ioremap(rdev
->rmmio_base
, rdev
->rmmio_size
);
769 if (rdev
->rmmio
== NULL
) {
772 DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)rdev
->rmmio_base
);
773 DRM_INFO("register mmio size: %u\n", (unsigned)rdev
->rmmio_size
);
775 /* io port mapping */
776 for (i
= 0; i
< DEVICE_COUNT_RESOURCE
; i
++) {
777 if (pci_resource_flags(rdev
->pdev
, i
) & IORESOURCE_IO
) {
778 rdev
->rio_mem_size
= pci_resource_len(rdev
->pdev
, i
);
779 rdev
->rio_mem
= pci_iomap(rdev
->pdev
, i
, rdev
->rio_mem_size
);
783 if (rdev
->rio_mem
== NULL
)
784 DRM_ERROR("Unable to find PCI I/O BAR\n");
786 /* if we have > 1 VGA cards, then disable the radeon VGA resources */
787 /* this will fail for cards that aren't VGA class devices, just
789 vga_client_register(rdev
->pdev
, rdev
, NULL
, radeon_vga_set_decode
);
790 vga_switcheroo_register_client(rdev
->pdev
,
791 radeon_switcheroo_set_state
,
793 radeon_switcheroo_can_switch
);
795 r
= radeon_init(rdev
);
799 if (rdev
->flags
& RADEON_IS_AGP
&& !rdev
->accel_working
) {
800 /* Acceleration not working on AGP card try again
801 * with fallback to PCI or PCIE GART
803 radeon_asic_reset(rdev
);
805 radeon_agp_disable(rdev
);
806 r
= radeon_init(rdev
);
810 if (radeon_testing
) {
811 radeon_test_moves(rdev
);
813 if (radeon_benchmarking
) {
814 radeon_benchmark(rdev
);
819 void radeon_device_fini(struct radeon_device
*rdev
)
821 DRM_INFO("radeon: finishing device.\n");
822 rdev
->shutdown
= true;
823 /* evict vram memory */
824 radeon_bo_evict_vram(rdev
);
826 vga_switcheroo_unregister_client(rdev
->pdev
);
827 vga_client_register(rdev
->pdev
, NULL
, NULL
, NULL
);
829 pci_iounmap(rdev
->pdev
, rdev
->rio_mem
);
830 rdev
->rio_mem
= NULL
;
831 iounmap(rdev
->rmmio
);
839 int radeon_suspend_kms(struct drm_device
*dev
, pm_message_t state
)
841 struct radeon_device
*rdev
;
842 struct drm_crtc
*crtc
;
843 struct drm_connector
*connector
;
846 if (dev
== NULL
|| dev
->dev_private
== NULL
) {
849 if (state
.event
== PM_EVENT_PRETHAW
) {
852 rdev
= dev
->dev_private
;
854 if (dev
->switch_power_state
== DRM_SWITCH_POWER_OFF
)
857 /* turn off display hw */
858 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
) {
859 drm_helper_connector_dpms(connector
, DRM_MODE_DPMS_OFF
);
862 /* unpin the front buffers */
863 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
, head
) {
864 struct radeon_framebuffer
*rfb
= to_radeon_framebuffer(crtc
->fb
);
865 struct radeon_bo
*robj
;
867 if (rfb
== NULL
|| rfb
->obj
== NULL
) {
870 robj
= gem_to_radeon_bo(rfb
->obj
);
871 /* don't unpin kernel fb objects */
872 if (!radeon_fbdev_robj_is_fb(rdev
, robj
)) {
873 r
= radeon_bo_reserve(robj
, false);
875 radeon_bo_unpin(robj
);
876 radeon_bo_unreserve(robj
);
880 /* evict vram memory */
881 radeon_bo_evict_vram(rdev
);
882 /* wait for gpu to finish processing current batch */
883 radeon_fence_wait_last(rdev
);
885 radeon_save_bios_scratch_regs(rdev
);
887 radeon_pm_suspend(rdev
);
888 radeon_suspend(rdev
);
889 radeon_hpd_fini(rdev
);
890 /* evict remaining vram memory */
891 radeon_bo_evict_vram(rdev
);
893 radeon_agp_suspend(rdev
);
895 pci_save_state(dev
->pdev
);
896 if (state
.event
== PM_EVENT_SUSPEND
) {
897 /* Shut down the device */
898 pci_disable_device(dev
->pdev
);
899 pci_set_power_state(dev
->pdev
, PCI_D3hot
);
902 radeon_fbdev_set_suspend(rdev
, 1);
907 int radeon_resume_kms(struct drm_device
*dev
)
909 struct drm_connector
*connector
;
910 struct radeon_device
*rdev
= dev
->dev_private
;
912 if (dev
->switch_power_state
== DRM_SWITCH_POWER_OFF
)
916 pci_set_power_state(dev
->pdev
, PCI_D0
);
917 pci_restore_state(dev
->pdev
);
918 if (pci_enable_device(dev
->pdev
)) {
922 pci_set_master(dev
->pdev
);
923 /* resume AGP if in use */
924 radeon_agp_resume(rdev
);
926 radeon_pm_resume(rdev
);
927 radeon_restore_bios_scratch_regs(rdev
);
929 radeon_fbdev_set_suspend(rdev
, 0);
933 if (rdev
->is_atom_bios
)
934 radeon_atom_encoder_init(rdev
);
935 /* reset hpd state */
936 radeon_hpd_init(rdev
);
937 /* blat the mode back in */
938 drm_helper_resume_force_mode(dev
);
939 /* turn on display hw */
940 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
) {
941 drm_helper_connector_dpms(connector
, DRM_MODE_DPMS_ON
);
946 int radeon_gpu_reset(struct radeon_device
*rdev
)
951 radeon_save_bios_scratch_regs(rdev
);
953 resched
= ttm_bo_lock_delayed_workqueue(&rdev
->mman
.bdev
);
954 radeon_suspend(rdev
);
956 r
= radeon_asic_reset(rdev
);
958 dev_info(rdev
->dev
, "GPU reset succeed\n");
960 radeon_restore_bios_scratch_regs(rdev
);
961 drm_helper_resume_force_mode(rdev
->ddev
);
962 ttm_bo_unlock_delayed_workqueue(&rdev
->mman
.bdev
, resched
);
965 /* bad news, how to tell it to userspace ? */
966 dev_info(rdev
->dev
, "GPU reset failed\n");
974 struct radeon_debugfs
{
975 struct drm_info_list
*files
;
978 static struct radeon_debugfs _radeon_debugfs
[RADEON_DEBUGFS_MAX_NUM_FILES
];
979 static unsigned _radeon_debugfs_count
= 0;
981 int radeon_debugfs_add_files(struct radeon_device
*rdev
,
982 struct drm_info_list
*files
,
987 for (i
= 0; i
< _radeon_debugfs_count
; i
++) {
988 if (_radeon_debugfs
[i
].files
== files
) {
989 /* Already registered */
993 if ((_radeon_debugfs_count
+ nfiles
) > RADEON_DEBUGFS_MAX_NUM_FILES
) {
994 DRM_ERROR("Reached maximum number of debugfs files.\n");
995 DRM_ERROR("Report so we increase RADEON_DEBUGFS_MAX_NUM_FILES.\n");
998 _radeon_debugfs
[_radeon_debugfs_count
].files
= files
;
999 _radeon_debugfs
[_radeon_debugfs_count
].num_files
= nfiles
;
1000 _radeon_debugfs_count
++;
1001 #if defined(CONFIG_DEBUG_FS)
1002 drm_debugfs_create_files(files
, nfiles
,
1003 rdev
->ddev
->control
->debugfs_root
,
1004 rdev
->ddev
->control
);
1005 drm_debugfs_create_files(files
, nfiles
,
1006 rdev
->ddev
->primary
->debugfs_root
,
1007 rdev
->ddev
->primary
);
1012 #if defined(CONFIG_DEBUG_FS)
1013 int radeon_debugfs_init(struct drm_minor
*minor
)
1018 void radeon_debugfs_cleanup(struct drm_minor
*minor
)
1022 for (i
= 0; i
< _radeon_debugfs_count
; i
++) {
1023 drm_debugfs_remove_files(_radeon_debugfs
[i
].files
,
1024 _radeon_debugfs
[i
].num_files
, minor
);