Home | History | Annotate | Line # | Download | only in radeon
      1  1.1  riastrad /*	$NetBSD: radeon_rs600.c,v 1.2 2021/12/18 23:45:43 riastradh Exp $	*/
      2  1.1  riastrad 
      3  1.1  riastrad /*
      4  1.1  riastrad  * Copyright 2008 Advanced Micro Devices, Inc.
      5  1.1  riastrad  * Copyright 2008 Red Hat Inc.
      6  1.1  riastrad  * Copyright 2009 Jerome Glisse.
      7  1.1  riastrad  *
      8  1.1  riastrad  * Permission is hereby granted, free of charge, to any person obtaining a
      9  1.1  riastrad  * copy of this software and associated documentation files (the "Software"),
     10  1.1  riastrad  * to deal in the Software without restriction, including without limitation
     11  1.1  riastrad  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     12  1.1  riastrad  * and/or sell copies of the Software, and to permit persons to whom the
     13  1.1  riastrad  * Software is furnished to do so, subject to the following conditions:
     14  1.1  riastrad  *
     15  1.1  riastrad  * The above copyright notice and this permission notice shall be included in
     16  1.1  riastrad  * all copies or substantial portions of the Software.
     17  1.1  riastrad  *
     18  1.1  riastrad  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     19  1.1  riastrad  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     20  1.1  riastrad  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     21  1.1  riastrad  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
     22  1.1  riastrad  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
     23  1.1  riastrad  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
     24  1.1  riastrad  * OTHER DEALINGS IN THE SOFTWARE.
     25  1.1  riastrad  *
     26  1.1  riastrad  * Authors: Dave Airlie
     27  1.1  riastrad  *          Alex Deucher
     28  1.1  riastrad  *          Jerome Glisse
     29  1.1  riastrad  */
     30  1.1  riastrad /* RS600 / Radeon X1250/X1270 integrated GPU
     31  1.1  riastrad  *
     32  1.1  riastrad  * This file gather function specific to RS600 which is the IGP of
     33  1.1  riastrad  * the X1250/X1270 family supporting intel CPU (while RS690/RS740
     34  1.1  riastrad  * is the X1250/X1270 supporting AMD CPU). The display engine are
     35  1.1  riastrad  * the avivo one, bios is an atombios, 3D block are the one of the
     36  1.1  riastrad  * R4XX family. The GART is different from the RS400 one and is very
     37  1.1  riastrad  * close to the one of the R600 family (R600 likely being an evolution
     38  1.1  riastrad  * of the RS600 GART block).
     39  1.1  riastrad  */
     40  1.2  riastrad 
     41  1.1  riastrad #include <sys/cdefs.h>
     42  1.1  riastrad __KERNEL_RCSID(0, "$NetBSD: radeon_rs600.c,v 1.2 2021/12/18 23:45:43 riastradh Exp $");
     43  1.1  riastrad 
     44  1.2  riastrad #include <linux/io-64-nonatomic-lo-hi.h>
     45  1.2  riastrad #include <linux/pci.h>
     46  1.2  riastrad 
     47  1.2  riastrad #include <drm/drm_device.h>
     48  1.2  riastrad #include <drm/drm_vblank.h>
     49  1.2  riastrad 
     50  1.2  riastrad #include "atom.h"
     51  1.1  riastrad #include "radeon.h"
     52  1.1  riastrad #include "radeon_asic.h"
     53  1.1  riastrad #include "radeon_audio.h"
     54  1.2  riastrad #include "rs600_reg_safe.h"
     55  1.1  riastrad #include "rs600d.h"
     56  1.1  riastrad 
     57  1.1  riastrad static void rs600_gpu_init(struct radeon_device *rdev);
     58  1.1  riastrad int rs600_mc_wait_for_idle(struct radeon_device *rdev);
     59  1.1  riastrad 
     60  1.1  riastrad static const u32 crtc_offsets[2] =
     61  1.1  riastrad {
     62  1.1  riastrad 	0,
     63  1.1  riastrad 	AVIVO_D2CRTC_H_TOTAL - AVIVO_D1CRTC_H_TOTAL
     64  1.1  riastrad };
     65  1.1  riastrad 
     66  1.1  riastrad static bool avivo_is_in_vblank(struct radeon_device *rdev, int crtc)
     67  1.1  riastrad {
     68  1.1  riastrad 	if (RREG32(AVIVO_D1CRTC_STATUS + crtc_offsets[crtc]) & AVIVO_D1CRTC_V_BLANK)
     69  1.1  riastrad 		return true;
     70  1.1  riastrad 	else
     71  1.1  riastrad 		return false;
     72  1.1  riastrad }
     73  1.1  riastrad 
     74  1.1  riastrad static bool avivo_is_counter_moving(struct radeon_device *rdev, int crtc)
     75  1.1  riastrad {
     76  1.1  riastrad 	u32 pos1, pos2;
     77  1.1  riastrad 
     78  1.1  riastrad 	pos1 = RREG32(AVIVO_D1CRTC_STATUS_POSITION + crtc_offsets[crtc]);
     79  1.1  riastrad 	pos2 = RREG32(AVIVO_D1CRTC_STATUS_POSITION + crtc_offsets[crtc]);
     80  1.1  riastrad 
     81  1.1  riastrad 	if (pos1 != pos2)
     82  1.1  riastrad 		return true;
     83  1.1  riastrad 	else
     84  1.1  riastrad 		return false;
     85  1.1  riastrad }
     86  1.1  riastrad 
     87  1.1  riastrad /**
     88  1.1  riastrad  * avivo_wait_for_vblank - vblank wait asic callback.
     89  1.1  riastrad  *
     90  1.1  riastrad  * @rdev: radeon_device pointer
     91  1.1  riastrad  * @crtc: crtc to wait for vblank on
     92  1.1  riastrad  *
     93  1.1  riastrad  * Wait for vblank on the requested crtc (r5xx-r7xx).
     94  1.1  riastrad  */
     95  1.1  riastrad void avivo_wait_for_vblank(struct radeon_device *rdev, int crtc)
     96  1.1  riastrad {
     97  1.1  riastrad 	unsigned i = 0;
     98  1.1  riastrad 
     99  1.1  riastrad 	if (crtc >= rdev->num_crtc)
    100  1.1  riastrad 		return;
    101  1.1  riastrad 
    102  1.1  riastrad 	if (!(RREG32(AVIVO_D1CRTC_CONTROL + crtc_offsets[crtc]) & AVIVO_CRTC_EN))
    103  1.1  riastrad 		return;
    104  1.1  riastrad 
    105  1.1  riastrad 	/* depending on when we hit vblank, we may be close to active; if so,
    106  1.1  riastrad 	 * wait for another frame.
    107  1.1  riastrad 	 */
    108  1.1  riastrad 	while (avivo_is_in_vblank(rdev, crtc)) {
    109  1.1  riastrad 		if (i++ % 100 == 0) {
    110  1.1  riastrad 			if (!avivo_is_counter_moving(rdev, crtc))
    111  1.1  riastrad 				break;
    112  1.1  riastrad 		}
    113  1.1  riastrad 	}
    114  1.1  riastrad 
    115  1.1  riastrad 	while (!avivo_is_in_vblank(rdev, crtc)) {
    116  1.1  riastrad 		if (i++ % 100 == 0) {
    117  1.1  riastrad 			if (!avivo_is_counter_moving(rdev, crtc))
    118  1.1  riastrad 				break;
    119  1.1  riastrad 		}
    120  1.1  riastrad 	}
    121  1.1  riastrad }
    122  1.1  riastrad 
    123  1.2  riastrad void rs600_page_flip(struct radeon_device *rdev, int crtc_id, u64 crtc_base, bool async)
    124  1.1  riastrad {
    125  1.1  riastrad 	struct radeon_crtc *radeon_crtc = rdev->mode_info.crtcs[crtc_id];
    126  1.1  riastrad 	u32 tmp = RREG32(AVIVO_D1GRPH_UPDATE + radeon_crtc->crtc_offset);
    127  1.1  riastrad 	int i;
    128  1.1  riastrad 
    129  1.1  riastrad 	/* Lock the graphics update lock */
    130  1.1  riastrad 	tmp |= AVIVO_D1GRPH_UPDATE_LOCK;
    131  1.1  riastrad 	WREG32(AVIVO_D1GRPH_UPDATE + radeon_crtc->crtc_offset, tmp);
    132  1.1  riastrad 
    133  1.1  riastrad 	/* update the scanout addresses */
    134  1.2  riastrad 	WREG32(AVIVO_D1GRPH_FLIP_CONTROL + radeon_crtc->crtc_offset,
    135  1.2  riastrad 	       async ? AVIVO_D1GRPH_SURFACE_UPDATE_H_RETRACE_EN : 0);
    136  1.1  riastrad 	WREG32(AVIVO_D1GRPH_SECONDARY_SURFACE_ADDRESS + radeon_crtc->crtc_offset,
    137  1.1  riastrad 	       (u32)crtc_base);
    138  1.1  riastrad 	WREG32(AVIVO_D1GRPH_PRIMARY_SURFACE_ADDRESS + radeon_crtc->crtc_offset,
    139  1.1  riastrad 	       (u32)crtc_base);
    140  1.1  riastrad 
    141  1.1  riastrad 	/* Wait for update_pending to go high. */
    142  1.1  riastrad 	for (i = 0; i < rdev->usec_timeout; i++) {
    143  1.1  riastrad 		if (RREG32(AVIVO_D1GRPH_UPDATE + radeon_crtc->crtc_offset) & AVIVO_D1GRPH_SURFACE_UPDATE_PENDING)
    144  1.1  riastrad 			break;
    145  1.1  riastrad 		udelay(1);
    146  1.1  riastrad 	}
    147  1.1  riastrad 	DRM_DEBUG("Update pending now high. Unlocking vupdate_lock.\n");
    148  1.1  riastrad 
    149  1.1  riastrad 	/* Unlock the lock, so double-buffering can take place inside vblank */
    150  1.1  riastrad 	tmp &= ~AVIVO_D1GRPH_UPDATE_LOCK;
    151  1.1  riastrad 	WREG32(AVIVO_D1GRPH_UPDATE + radeon_crtc->crtc_offset, tmp);
    152  1.1  riastrad }
    153  1.1  riastrad 
    154  1.1  riastrad bool rs600_page_flip_pending(struct radeon_device *rdev, int crtc_id)
    155  1.1  riastrad {
    156  1.1  riastrad 	struct radeon_crtc *radeon_crtc = rdev->mode_info.crtcs[crtc_id];
    157  1.1  riastrad 
    158  1.1  riastrad 	/* Return current update_pending status: */
    159  1.1  riastrad 	return !!(RREG32(AVIVO_D1GRPH_UPDATE + radeon_crtc->crtc_offset) &
    160  1.1  riastrad 		AVIVO_D1GRPH_SURFACE_UPDATE_PENDING);
    161  1.1  riastrad }
    162  1.1  riastrad 
    163  1.1  riastrad void avivo_program_fmt(struct drm_encoder *encoder)
    164  1.1  riastrad {
    165  1.1  riastrad 	struct drm_device *dev = encoder->dev;
    166  1.1  riastrad 	struct radeon_device *rdev = dev->dev_private;
    167  1.1  riastrad 	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
    168  1.1  riastrad 	struct drm_connector *connector = radeon_get_connector_for_encoder(encoder);
    169  1.1  riastrad 	int bpc = 0;
    170  1.1  riastrad 	u32 tmp = 0;
    171  1.1  riastrad 	enum radeon_connector_dither dither = RADEON_FMT_DITHER_DISABLE;
    172  1.1  riastrad 
    173  1.1  riastrad 	if (connector) {
    174  1.1  riastrad 		struct radeon_connector *radeon_connector = to_radeon_connector(connector);
    175  1.1  riastrad 		bpc = radeon_get_monitor_bpc(connector);
    176  1.1  riastrad 		dither = radeon_connector->dither;
    177  1.1  riastrad 	}
    178  1.1  riastrad 
    179  1.1  riastrad 	/* LVDS FMT is set up by atom */
    180  1.1  riastrad 	if (radeon_encoder->devices & ATOM_DEVICE_LCD_SUPPORT)
    181  1.1  riastrad 		return;
    182  1.1  riastrad 
    183  1.1  riastrad 	if (bpc == 0)
    184  1.1  riastrad 		return;
    185  1.1  riastrad 
    186  1.1  riastrad 	switch (bpc) {
    187  1.1  riastrad 	case 6:
    188  1.1  riastrad 		if (dither == RADEON_FMT_DITHER_ENABLE)
    189  1.1  riastrad 			/* XXX sort out optimal dither settings */
    190  1.1  riastrad 			tmp |= AVIVO_TMDS_BIT_DEPTH_CONTROL_SPATIAL_DITHER_EN;
    191  1.1  riastrad 		else
    192  1.1  riastrad 			tmp |= AVIVO_TMDS_BIT_DEPTH_CONTROL_TRUNCATE_EN;
    193  1.1  riastrad 		break;
    194  1.1  riastrad 	case 8:
    195  1.1  riastrad 		if (dither == RADEON_FMT_DITHER_ENABLE)
    196  1.1  riastrad 			/* XXX sort out optimal dither settings */
    197  1.1  riastrad 			tmp |= (AVIVO_TMDS_BIT_DEPTH_CONTROL_SPATIAL_DITHER_EN |
    198  1.1  riastrad 				AVIVO_TMDS_BIT_DEPTH_CONTROL_SPATIAL_DITHER_DEPTH);
    199  1.1  riastrad 		else
    200  1.1  riastrad 			tmp |= (AVIVO_TMDS_BIT_DEPTH_CONTROL_TRUNCATE_EN |
    201  1.1  riastrad 				AVIVO_TMDS_BIT_DEPTH_CONTROL_TRUNCATE_DEPTH);
    202  1.1  riastrad 		break;
    203  1.1  riastrad 	case 10:
    204  1.1  riastrad 	default:
    205  1.1  riastrad 		/* not needed */
    206  1.1  riastrad 		break;
    207  1.1  riastrad 	}
    208  1.1  riastrad 
    209  1.1  riastrad 	switch (radeon_encoder->encoder_id) {
    210  1.1  riastrad 	case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_TMDS1:
    211  1.1  riastrad 		WREG32(AVIVO_TMDSA_BIT_DEPTH_CONTROL, tmp);
    212  1.1  riastrad 		break;
    213  1.1  riastrad 	case ENCODER_OBJECT_ID_INTERNAL_LVTM1:
    214  1.1  riastrad 		WREG32(AVIVO_LVTMA_BIT_DEPTH_CONTROL, tmp);
    215  1.1  riastrad 		break;
    216  1.1  riastrad 	case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DVO1:
    217  1.1  riastrad 		WREG32(AVIVO_DVOA_BIT_DEPTH_CONTROL, tmp);
    218  1.1  riastrad 		break;
    219  1.1  riastrad 	case ENCODER_OBJECT_ID_INTERNAL_DDI:
    220  1.1  riastrad 		WREG32(AVIVO_DDIA_BIT_DEPTH_CONTROL, tmp);
    221  1.1  riastrad 		break;
    222  1.1  riastrad 	default:
    223  1.1  riastrad 		break;
    224  1.1  riastrad 	}
    225  1.1  riastrad }
    226  1.1  riastrad 
    227  1.1  riastrad void rs600_pm_misc(struct radeon_device *rdev)
    228  1.1  riastrad {
    229  1.1  riastrad 	int requested_index = rdev->pm.requested_power_state_index;
    230  1.1  riastrad 	struct radeon_power_state *ps = &rdev->pm.power_state[requested_index];
    231  1.1  riastrad 	struct radeon_voltage *voltage = &ps->clock_info[0].voltage;
    232  1.1  riastrad 	u32 tmp, dyn_pwrmgt_sclk_length, dyn_sclk_vol_cntl;
    233  1.1  riastrad 	u32 hdp_dyn_cntl, /*mc_host_dyn_cntl,*/ dyn_backbias_cntl;
    234  1.1  riastrad 
    235  1.1  riastrad 	if ((voltage->type == VOLTAGE_GPIO) && (voltage->gpio.valid)) {
    236  1.1  riastrad 		if (ps->misc & ATOM_PM_MISCINFO_VOLTAGE_DROP_SUPPORT) {
    237  1.1  riastrad 			tmp = RREG32(voltage->gpio.reg);
    238  1.1  riastrad 			if (voltage->active_high)
    239  1.1  riastrad 				tmp |= voltage->gpio.mask;
    240  1.1  riastrad 			else
    241  1.1  riastrad 				tmp &= ~(voltage->gpio.mask);
    242  1.1  riastrad 			WREG32(voltage->gpio.reg, tmp);
    243  1.1  riastrad 			if (voltage->delay)
    244  1.1  riastrad 				udelay(voltage->delay);
    245  1.1  riastrad 		} else {
    246  1.1  riastrad 			tmp = RREG32(voltage->gpio.reg);
    247  1.1  riastrad 			if (voltage->active_high)
    248  1.1  riastrad 				tmp &= ~voltage->gpio.mask;
    249  1.1  riastrad 			else
    250  1.1  riastrad 				tmp |= voltage->gpio.mask;
    251  1.1  riastrad 			WREG32(voltage->gpio.reg, tmp);
    252  1.1  riastrad 			if (voltage->delay)
    253  1.1  riastrad 				udelay(voltage->delay);
    254  1.1  riastrad 		}
    255  1.1  riastrad 	} else if (voltage->type == VOLTAGE_VDDC)
    256  1.1  riastrad 		radeon_atom_set_voltage(rdev, voltage->vddc_id, SET_VOLTAGE_TYPE_ASIC_VDDC);
    257  1.1  riastrad 
    258  1.1  riastrad 	dyn_pwrmgt_sclk_length = RREG32_PLL(DYN_PWRMGT_SCLK_LENGTH);
    259  1.1  riastrad 	dyn_pwrmgt_sclk_length &= ~REDUCED_POWER_SCLK_HILEN(0xf);
    260  1.1  riastrad 	dyn_pwrmgt_sclk_length &= ~REDUCED_POWER_SCLK_LOLEN(0xf);
    261  1.1  riastrad 	if (ps->misc & ATOM_PM_MISCINFO_ASIC_REDUCED_SPEED_SCLK_EN) {
    262  1.1  riastrad 		if (ps->misc & ATOM_PM_MISCINFO_DYNAMIC_CLOCK_DIVIDER_BY_2) {
    263  1.1  riastrad 			dyn_pwrmgt_sclk_length |= REDUCED_POWER_SCLK_HILEN(2);
    264  1.1  riastrad 			dyn_pwrmgt_sclk_length |= REDUCED_POWER_SCLK_LOLEN(2);
    265  1.1  riastrad 		} else if (ps->misc & ATOM_PM_MISCINFO_DYNAMIC_CLOCK_DIVIDER_BY_4) {
    266  1.1  riastrad 			dyn_pwrmgt_sclk_length |= REDUCED_POWER_SCLK_HILEN(4);
    267  1.1  riastrad 			dyn_pwrmgt_sclk_length |= REDUCED_POWER_SCLK_LOLEN(4);
    268  1.1  riastrad 		}
    269  1.1  riastrad 	} else {
    270  1.1  riastrad 		dyn_pwrmgt_sclk_length |= REDUCED_POWER_SCLK_HILEN(1);
    271  1.1  riastrad 		dyn_pwrmgt_sclk_length |= REDUCED_POWER_SCLK_LOLEN(1);
    272  1.1  riastrad 	}
    273  1.1  riastrad 	WREG32_PLL(DYN_PWRMGT_SCLK_LENGTH, dyn_pwrmgt_sclk_length);
    274  1.1  riastrad 
    275  1.1  riastrad 	dyn_sclk_vol_cntl = RREG32_PLL(DYN_SCLK_VOL_CNTL);
    276  1.1  riastrad 	if (ps->misc & ATOM_PM_MISCINFO_ASIC_DYNAMIC_VOLTAGE_EN) {
    277  1.1  riastrad 		dyn_sclk_vol_cntl |= IO_CG_VOLTAGE_DROP;
    278  1.1  riastrad 		if (voltage->delay) {
    279  1.1  riastrad 			dyn_sclk_vol_cntl |= VOLTAGE_DROP_SYNC;
    280  1.1  riastrad 			dyn_sclk_vol_cntl |= VOLTAGE_DELAY_SEL(voltage->delay);
    281  1.1  riastrad 		} else
    282  1.1  riastrad 			dyn_sclk_vol_cntl &= ~VOLTAGE_DROP_SYNC;
    283  1.1  riastrad 	} else
    284  1.1  riastrad 		dyn_sclk_vol_cntl &= ~IO_CG_VOLTAGE_DROP;
    285  1.1  riastrad 	WREG32_PLL(DYN_SCLK_VOL_CNTL, dyn_sclk_vol_cntl);
    286  1.1  riastrad 
    287  1.1  riastrad 	hdp_dyn_cntl = RREG32_PLL(HDP_DYN_CNTL);
    288  1.1  riastrad 	if (ps->misc & ATOM_PM_MISCINFO_DYNAMIC_HDP_BLOCK_EN)
    289  1.1  riastrad 		hdp_dyn_cntl &= ~HDP_FORCEON;
    290  1.1  riastrad 	else
    291  1.1  riastrad 		hdp_dyn_cntl |= HDP_FORCEON;
    292  1.1  riastrad 	WREG32_PLL(HDP_DYN_CNTL, hdp_dyn_cntl);
    293  1.1  riastrad #if 0
    294  1.1  riastrad 	/* mc_host_dyn seems to cause hangs from time to time */
    295  1.1  riastrad 	mc_host_dyn_cntl = RREG32_PLL(MC_HOST_DYN_CNTL);
    296  1.1  riastrad 	if (ps->misc & ATOM_PM_MISCINFO_DYNAMIC_MC_HOST_BLOCK_EN)
    297  1.1  riastrad 		mc_host_dyn_cntl &= ~MC_HOST_FORCEON;
    298  1.1  riastrad 	else
    299  1.1  riastrad 		mc_host_dyn_cntl |= MC_HOST_FORCEON;
    300  1.1  riastrad 	WREG32_PLL(MC_HOST_DYN_CNTL, mc_host_dyn_cntl);
    301  1.1  riastrad #endif
    302  1.1  riastrad 	dyn_backbias_cntl = RREG32_PLL(DYN_BACKBIAS_CNTL);
    303  1.1  riastrad 	if (ps->misc & ATOM_PM_MISCINFO2_DYNAMIC_BACK_BIAS_EN)
    304  1.1  riastrad 		dyn_backbias_cntl |= IO_CG_BACKBIAS_EN;
    305  1.1  riastrad 	else
    306  1.1  riastrad 		dyn_backbias_cntl &= ~IO_CG_BACKBIAS_EN;
    307  1.1  riastrad 	WREG32_PLL(DYN_BACKBIAS_CNTL, dyn_backbias_cntl);
    308  1.1  riastrad 
    309  1.1  riastrad 	/* set pcie lanes */
    310  1.1  riastrad 	if ((rdev->flags & RADEON_IS_PCIE) &&
    311  1.1  riastrad 	    !(rdev->flags & RADEON_IS_IGP) &&
    312  1.1  riastrad 	    rdev->asic->pm.set_pcie_lanes &&
    313  1.1  riastrad 	    (ps->pcie_lanes !=
    314  1.1  riastrad 	     rdev->pm.power_state[rdev->pm.current_power_state_index].pcie_lanes)) {
    315  1.1  riastrad 		radeon_set_pcie_lanes(rdev,
    316  1.1  riastrad 				      ps->pcie_lanes);
    317  1.1  riastrad 		DRM_DEBUG("Setting: p: %d\n", ps->pcie_lanes);
    318  1.1  riastrad 	}
    319  1.1  riastrad }
    320  1.1  riastrad 
    321  1.1  riastrad void rs600_pm_prepare(struct radeon_device *rdev)
    322  1.1  riastrad {
    323  1.1  riastrad 	struct drm_device *ddev = rdev->ddev;
    324  1.1  riastrad 	struct drm_crtc *crtc;
    325  1.1  riastrad 	struct radeon_crtc *radeon_crtc;
    326  1.1  riastrad 	u32 tmp;
    327  1.1  riastrad 
    328  1.1  riastrad 	/* disable any active CRTCs */
    329  1.1  riastrad 	list_for_each_entry(crtc, &ddev->mode_config.crtc_list, head) {
    330  1.1  riastrad 		radeon_crtc = to_radeon_crtc(crtc);
    331  1.1  riastrad 		if (radeon_crtc->enabled) {
    332  1.1  riastrad 			tmp = RREG32(AVIVO_D1CRTC_CONTROL + radeon_crtc->crtc_offset);
    333  1.1  riastrad 			tmp |= AVIVO_CRTC_DISP_READ_REQUEST_DISABLE;
    334  1.1  riastrad 			WREG32(AVIVO_D1CRTC_CONTROL + radeon_crtc->crtc_offset, tmp);
    335  1.1  riastrad 		}
    336  1.1  riastrad 	}
    337  1.1  riastrad }
    338  1.1  riastrad 
    339  1.1  riastrad void rs600_pm_finish(struct radeon_device *rdev)
    340  1.1  riastrad {
    341  1.1  riastrad 	struct drm_device *ddev = rdev->ddev;
    342  1.1  riastrad 	struct drm_crtc *crtc;
    343  1.1  riastrad 	struct radeon_crtc *radeon_crtc;
    344  1.1  riastrad 	u32 tmp;
    345  1.1  riastrad 
    346  1.1  riastrad 	/* enable any active CRTCs */
    347  1.1  riastrad 	list_for_each_entry(crtc, &ddev->mode_config.crtc_list, head) {
    348  1.1  riastrad 		radeon_crtc = to_radeon_crtc(crtc);
    349  1.1  riastrad 		if (radeon_crtc->enabled) {
    350  1.1  riastrad 			tmp = RREG32(AVIVO_D1CRTC_CONTROL + radeon_crtc->crtc_offset);
    351  1.1  riastrad 			tmp &= ~AVIVO_CRTC_DISP_READ_REQUEST_DISABLE;
    352  1.1  riastrad 			WREG32(AVIVO_D1CRTC_CONTROL + radeon_crtc->crtc_offset, tmp);
    353  1.1  riastrad 		}
    354  1.1  riastrad 	}
    355  1.1  riastrad }
    356  1.1  riastrad 
    357  1.1  riastrad /* hpd for digital panel detect/disconnect */
    358  1.1  riastrad bool rs600_hpd_sense(struct radeon_device *rdev, enum radeon_hpd_id hpd)
    359  1.1  riastrad {
    360  1.1  riastrad 	u32 tmp;
    361  1.1  riastrad 	bool connected = false;
    362  1.1  riastrad 
    363  1.1  riastrad 	switch (hpd) {
    364  1.1  riastrad 	case RADEON_HPD_1:
    365  1.1  riastrad 		tmp = RREG32(R_007D04_DC_HOT_PLUG_DETECT1_INT_STATUS);
    366  1.1  riastrad 		if (G_007D04_DC_HOT_PLUG_DETECT1_SENSE(tmp))
    367  1.1  riastrad 			connected = true;
    368  1.1  riastrad 		break;
    369  1.1  riastrad 	case RADEON_HPD_2:
    370  1.1  riastrad 		tmp = RREG32(R_007D14_DC_HOT_PLUG_DETECT2_INT_STATUS);
    371  1.1  riastrad 		if (G_007D14_DC_HOT_PLUG_DETECT2_SENSE(tmp))
    372  1.1  riastrad 			connected = true;
    373  1.1  riastrad 		break;
    374  1.1  riastrad 	default:
    375  1.1  riastrad 		break;
    376  1.1  riastrad 	}
    377  1.1  riastrad 	return connected;
    378  1.1  riastrad }
    379  1.1  riastrad 
    380  1.1  riastrad void rs600_hpd_set_polarity(struct radeon_device *rdev,
    381  1.1  riastrad 			    enum radeon_hpd_id hpd)
    382  1.1  riastrad {
    383  1.1  riastrad 	u32 tmp;
    384  1.1  riastrad 	bool connected = rs600_hpd_sense(rdev, hpd);
    385  1.1  riastrad 
    386  1.1  riastrad 	switch (hpd) {
    387  1.1  riastrad 	case RADEON_HPD_1:
    388  1.1  riastrad 		tmp = RREG32(R_007D08_DC_HOT_PLUG_DETECT1_INT_CONTROL);
    389  1.1  riastrad 		if (connected)
    390  1.1  riastrad 			tmp &= ~S_007D08_DC_HOT_PLUG_DETECT1_INT_POLARITY(1);
    391  1.1  riastrad 		else
    392  1.1  riastrad 			tmp |= S_007D08_DC_HOT_PLUG_DETECT1_INT_POLARITY(1);
    393  1.1  riastrad 		WREG32(R_007D08_DC_HOT_PLUG_DETECT1_INT_CONTROL, tmp);
    394  1.1  riastrad 		break;
    395  1.1  riastrad 	case RADEON_HPD_2:
    396  1.1  riastrad 		tmp = RREG32(R_007D18_DC_HOT_PLUG_DETECT2_INT_CONTROL);
    397  1.1  riastrad 		if (connected)
    398  1.1  riastrad 			tmp &= ~S_007D18_DC_HOT_PLUG_DETECT2_INT_POLARITY(1);
    399  1.1  riastrad 		else
    400  1.1  riastrad 			tmp |= S_007D18_DC_HOT_PLUG_DETECT2_INT_POLARITY(1);
    401  1.1  riastrad 		WREG32(R_007D18_DC_HOT_PLUG_DETECT2_INT_CONTROL, tmp);
    402  1.1  riastrad 		break;
    403  1.1  riastrad 	default:
    404  1.1  riastrad 		break;
    405  1.1  riastrad 	}
    406  1.1  riastrad }
    407  1.1  riastrad 
    408  1.1  riastrad void rs600_hpd_init(struct radeon_device *rdev)
    409  1.1  riastrad {
    410  1.1  riastrad 	struct drm_device *dev = rdev->ddev;
    411  1.1  riastrad 	struct drm_connector *connector;
    412  1.1  riastrad 	unsigned enable = 0;
    413  1.1  riastrad 
    414  1.1  riastrad 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
    415  1.1  riastrad 		struct radeon_connector *radeon_connector = to_radeon_connector(connector);
    416  1.1  riastrad 		switch (radeon_connector->hpd.hpd) {
    417  1.1  riastrad 		case RADEON_HPD_1:
    418  1.1  riastrad 			WREG32(R_007D00_DC_HOT_PLUG_DETECT1_CONTROL,
    419  1.1  riastrad 			       S_007D00_DC_HOT_PLUG_DETECT1_EN(1));
    420  1.1  riastrad 			break;
    421  1.1  riastrad 		case RADEON_HPD_2:
    422  1.1  riastrad 			WREG32(R_007D10_DC_HOT_PLUG_DETECT2_CONTROL,
    423  1.1  riastrad 			       S_007D10_DC_HOT_PLUG_DETECT2_EN(1));
    424  1.1  riastrad 			break;
    425  1.1  riastrad 		default:
    426  1.1  riastrad 			break;
    427  1.1  riastrad 		}
    428  1.2  riastrad 		if (radeon_connector->hpd.hpd != RADEON_HPD_NONE)
    429  1.2  riastrad 			enable |= 1 << radeon_connector->hpd.hpd;
    430  1.1  riastrad 		radeon_hpd_set_polarity(rdev, radeon_connector->hpd.hpd);
    431  1.1  riastrad 	}
    432  1.1  riastrad 	radeon_irq_kms_enable_hpd(rdev, enable);
    433  1.1  riastrad }
    434  1.1  riastrad 
    435  1.1  riastrad void rs600_hpd_fini(struct radeon_device *rdev)
    436  1.1  riastrad {
    437  1.1  riastrad 	struct drm_device *dev = rdev->ddev;
    438  1.1  riastrad 	struct drm_connector *connector;
    439  1.1  riastrad 	unsigned disable = 0;
    440  1.1  riastrad 
    441  1.1  riastrad 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
    442  1.1  riastrad 		struct radeon_connector *radeon_connector = to_radeon_connector(connector);
    443  1.1  riastrad 		switch (radeon_connector->hpd.hpd) {
    444  1.1  riastrad 		case RADEON_HPD_1:
    445  1.1  riastrad 			WREG32(R_007D00_DC_HOT_PLUG_DETECT1_CONTROL,
    446  1.1  riastrad 			       S_007D00_DC_HOT_PLUG_DETECT1_EN(0));
    447  1.1  riastrad 			break;
    448  1.1  riastrad 		case RADEON_HPD_2:
    449  1.1  riastrad 			WREG32(R_007D10_DC_HOT_PLUG_DETECT2_CONTROL,
    450  1.1  riastrad 			       S_007D10_DC_HOT_PLUG_DETECT2_EN(0));
    451  1.1  riastrad 			break;
    452  1.1  riastrad 		default:
    453  1.1  riastrad 			break;
    454  1.1  riastrad 		}
    455  1.2  riastrad 		if (radeon_connector->hpd.hpd != RADEON_HPD_NONE)
    456  1.2  riastrad 			disable |= 1 << radeon_connector->hpd.hpd;
    457  1.1  riastrad 	}
    458  1.1  riastrad 	radeon_irq_kms_disable_hpd(rdev, disable);
    459  1.1  riastrad }
    460  1.1  riastrad 
    461  1.2  riastrad int rs600_asic_reset(struct radeon_device *rdev, bool hard)
    462  1.1  riastrad {
    463  1.1  riastrad 	struct rv515_mc_save save;
    464  1.1  riastrad 	u32 status, tmp;
    465  1.1  riastrad 	int ret = 0;
    466  1.1  riastrad 
    467  1.1  riastrad 	status = RREG32(R_000E40_RBBM_STATUS);
    468  1.1  riastrad 	if (!G_000E40_GUI_ACTIVE(status)) {
    469  1.1  riastrad 		return 0;
    470  1.1  riastrad 	}
    471  1.1  riastrad 	/* Stops all mc clients */
    472  1.1  riastrad 	rv515_mc_stop(rdev, &save);
    473  1.1  riastrad 	status = RREG32(R_000E40_RBBM_STATUS);
    474  1.1  riastrad 	dev_info(rdev->dev, "(%s:%d) RBBM_STATUS=0x%08X\n", __func__, __LINE__, status);
    475  1.1  riastrad 	/* stop CP */
    476  1.1  riastrad 	WREG32(RADEON_CP_CSQ_CNTL, 0);
    477  1.1  riastrad 	tmp = RREG32(RADEON_CP_RB_CNTL);
    478  1.1  riastrad 	WREG32(RADEON_CP_RB_CNTL, tmp | RADEON_RB_RPTR_WR_ENA);
    479  1.1  riastrad 	WREG32(RADEON_CP_RB_RPTR_WR, 0);
    480  1.1  riastrad 	WREG32(RADEON_CP_RB_WPTR, 0);
    481  1.1  riastrad 	WREG32(RADEON_CP_RB_CNTL, tmp);
    482  1.1  riastrad 	pci_save_state(rdev->pdev);
    483  1.1  riastrad 	/* disable bus mastering */
    484  1.1  riastrad 	pci_clear_master(rdev->pdev);
    485  1.1  riastrad 	mdelay(1);
    486  1.1  riastrad 	/* reset GA+VAP */
    487  1.1  riastrad 	WREG32(R_0000F0_RBBM_SOFT_RESET, S_0000F0_SOFT_RESET_VAP(1) |
    488  1.1  riastrad 					S_0000F0_SOFT_RESET_GA(1));
    489  1.1  riastrad 	RREG32(R_0000F0_RBBM_SOFT_RESET);
    490  1.1  riastrad 	mdelay(500);
    491  1.1  riastrad 	WREG32(R_0000F0_RBBM_SOFT_RESET, 0);
    492  1.1  riastrad 	mdelay(1);
    493  1.1  riastrad 	status = RREG32(R_000E40_RBBM_STATUS);
    494  1.1  riastrad 	dev_info(rdev->dev, "(%s:%d) RBBM_STATUS=0x%08X\n", __func__, __LINE__, status);
    495  1.1  riastrad 	/* reset CP */
    496  1.1  riastrad 	WREG32(R_0000F0_RBBM_SOFT_RESET, S_0000F0_SOFT_RESET_CP(1));
    497  1.1  riastrad 	RREG32(R_0000F0_RBBM_SOFT_RESET);
    498  1.1  riastrad 	mdelay(500);
    499  1.1  riastrad 	WREG32(R_0000F0_RBBM_SOFT_RESET, 0);
    500  1.1  riastrad 	mdelay(1);
    501  1.1  riastrad 	status = RREG32(R_000E40_RBBM_STATUS);
    502  1.1  riastrad 	dev_info(rdev->dev, "(%s:%d) RBBM_STATUS=0x%08X\n", __func__, __LINE__, status);
    503  1.1  riastrad 	/* reset MC */
    504  1.1  riastrad 	WREG32(R_0000F0_RBBM_SOFT_RESET, S_0000F0_SOFT_RESET_MC(1));
    505  1.1  riastrad 	RREG32(R_0000F0_RBBM_SOFT_RESET);
    506  1.1  riastrad 	mdelay(500);
    507  1.1  riastrad 	WREG32(R_0000F0_RBBM_SOFT_RESET, 0);
    508  1.1  riastrad 	mdelay(1);
    509  1.1  riastrad 	status = RREG32(R_000E40_RBBM_STATUS);
    510  1.1  riastrad 	dev_info(rdev->dev, "(%s:%d) RBBM_STATUS=0x%08X\n", __func__, __LINE__, status);
    511  1.1  riastrad 	/* restore PCI & busmastering */
    512  1.1  riastrad 	pci_restore_state(rdev->pdev);
    513  1.1  riastrad 	/* Check if GPU is idle */
    514  1.1  riastrad 	if (G_000E40_GA_BUSY(status) || G_000E40_VAP_BUSY(status)) {
    515  1.1  riastrad 		dev_err(rdev->dev, "failed to reset GPU\n");
    516  1.1  riastrad 		ret = -1;
    517  1.1  riastrad 	} else
    518  1.1  riastrad 		dev_info(rdev->dev, "GPU reset succeed\n");
    519  1.1  riastrad 	rv515_mc_resume(rdev, &save);
    520  1.1  riastrad 	return ret;
    521  1.1  riastrad }
    522  1.1  riastrad 
    523  1.1  riastrad /*
    524  1.1  riastrad  * GART.
    525  1.1  riastrad  */
    526  1.1  riastrad void rs600_gart_tlb_flush(struct radeon_device *rdev)
    527  1.1  riastrad {
    528  1.1  riastrad 	uint32_t tmp;
    529  1.1  riastrad 
    530  1.1  riastrad 	tmp = RREG32_MC(R_000100_MC_PT0_CNTL);
    531  1.1  riastrad 	tmp &= C_000100_INVALIDATE_ALL_L1_TLBS & C_000100_INVALIDATE_L2_CACHE;
    532  1.1  riastrad 	WREG32_MC(R_000100_MC_PT0_CNTL, tmp);
    533  1.1  riastrad 
    534  1.1  riastrad 	tmp = RREG32_MC(R_000100_MC_PT0_CNTL);
    535  1.1  riastrad 	tmp |= S_000100_INVALIDATE_ALL_L1_TLBS(1) | S_000100_INVALIDATE_L2_CACHE(1);
    536  1.1  riastrad 	WREG32_MC(R_000100_MC_PT0_CNTL, tmp);
    537  1.1  riastrad 
    538  1.1  riastrad 	tmp = RREG32_MC(R_000100_MC_PT0_CNTL);
    539  1.1  riastrad 	tmp &= C_000100_INVALIDATE_ALL_L1_TLBS & C_000100_INVALIDATE_L2_CACHE;
    540  1.1  riastrad 	WREG32_MC(R_000100_MC_PT0_CNTL, tmp);
    541  1.1  riastrad 	tmp = RREG32_MC(R_000100_MC_PT0_CNTL);
    542  1.1  riastrad }
    543  1.1  riastrad 
    544  1.1  riastrad static int rs600_gart_init(struct radeon_device *rdev)
    545  1.1  riastrad {
    546  1.1  riastrad 	int r;
    547  1.1  riastrad 
    548  1.1  riastrad 	if (rdev->gart.robj) {
    549  1.1  riastrad 		WARN(1, "RS600 GART already initialized\n");
    550  1.1  riastrad 		return 0;
    551  1.1  riastrad 	}
    552  1.1  riastrad 	/* Initialize common gart structure */
    553  1.1  riastrad 	r = radeon_gart_init(rdev);
    554  1.1  riastrad 	if (r) {
    555  1.1  riastrad 		return r;
    556  1.1  riastrad 	}
    557  1.1  riastrad 	rdev->gart.table_size = rdev->gart.num_gpu_pages * 8;
    558  1.1  riastrad 	return radeon_gart_table_vram_alloc(rdev);
    559  1.1  riastrad }
    560  1.1  riastrad 
    561  1.1  riastrad static int rs600_gart_enable(struct radeon_device *rdev)
    562  1.1  riastrad {
    563  1.1  riastrad 	u32 tmp;
    564  1.1  riastrad 	int r, i;
    565  1.1  riastrad 
    566  1.1  riastrad 	if (rdev->gart.robj == NULL) {
    567  1.1  riastrad 		dev_err(rdev->dev, "No VRAM object for PCIE GART.\n");
    568  1.1  riastrad 		return -EINVAL;
    569  1.1  riastrad 	}
    570  1.1  riastrad 	r = radeon_gart_table_vram_pin(rdev);
    571  1.1  riastrad 	if (r)
    572  1.1  riastrad 		return r;
    573  1.1  riastrad 	/* Enable bus master */
    574  1.1  riastrad 	tmp = RREG32(RADEON_BUS_CNTL) & ~RS600_BUS_MASTER_DIS;
    575  1.1  riastrad 	WREG32(RADEON_BUS_CNTL, tmp);
    576  1.1  riastrad 	/* FIXME: setup default page */
    577  1.1  riastrad 	WREG32_MC(R_000100_MC_PT0_CNTL,
    578  1.1  riastrad 		  (S_000100_EFFECTIVE_L2_CACHE_SIZE(6) |
    579  1.1  riastrad 		   S_000100_EFFECTIVE_L2_QUEUE_SIZE(6)));
    580  1.1  riastrad 
    581  1.1  riastrad 	for (i = 0; i < 19; i++) {
    582  1.1  riastrad 		WREG32_MC(R_00016C_MC_PT0_CLIENT0_CNTL + i,
    583  1.1  riastrad 			  S_00016C_ENABLE_TRANSLATION_MODE_OVERRIDE(1) |
    584  1.1  riastrad 			  S_00016C_SYSTEM_ACCESS_MODE_MASK(
    585  1.1  riastrad 				  V_00016C_SYSTEM_ACCESS_MODE_NOT_IN_SYS) |
    586  1.1  riastrad 			  S_00016C_SYSTEM_APERTURE_UNMAPPED_ACCESS(
    587  1.1  riastrad 				  V_00016C_SYSTEM_APERTURE_UNMAPPED_PASSTHROUGH) |
    588  1.1  riastrad 			  S_00016C_EFFECTIVE_L1_CACHE_SIZE(3) |
    589  1.1  riastrad 			  S_00016C_ENABLE_FRAGMENT_PROCESSING(1) |
    590  1.1  riastrad 			  S_00016C_EFFECTIVE_L1_QUEUE_SIZE(3));
    591  1.1  riastrad 	}
    592  1.1  riastrad 	/* enable first context */
    593  1.1  riastrad 	WREG32_MC(R_000102_MC_PT0_CONTEXT0_CNTL,
    594  1.1  riastrad 		  S_000102_ENABLE_PAGE_TABLE(1) |
    595  1.1  riastrad 		  S_000102_PAGE_TABLE_DEPTH(V_000102_PAGE_TABLE_FLAT));
    596  1.1  riastrad 
    597  1.1  riastrad 	/* disable all other contexts */
    598  1.1  riastrad 	for (i = 1; i < 8; i++)
    599  1.1  riastrad 		WREG32_MC(R_000102_MC_PT0_CONTEXT0_CNTL + i, 0);
    600  1.1  riastrad 
    601  1.1  riastrad 	/* setup the page table */
    602  1.1  riastrad 	WREG32_MC(R_00012C_MC_PT0_CONTEXT0_FLAT_BASE_ADDR,
    603  1.1  riastrad 		  rdev->gart.table_addr);
    604  1.1  riastrad 	WREG32_MC(R_00013C_MC_PT0_CONTEXT0_FLAT_START_ADDR, rdev->mc.gtt_start);
    605  1.1  riastrad 	WREG32_MC(R_00014C_MC_PT0_CONTEXT0_FLAT_END_ADDR, rdev->mc.gtt_end);
    606  1.1  riastrad 	WREG32_MC(R_00011C_MC_PT0_CONTEXT0_DEFAULT_READ_ADDR, 0);
    607  1.1  riastrad 
    608  1.1  riastrad 	/* System context maps to VRAM space */
    609  1.1  riastrad 	WREG32_MC(R_000112_MC_PT0_SYSTEM_APERTURE_LOW_ADDR, rdev->mc.vram_start);
    610  1.1  riastrad 	WREG32_MC(R_000114_MC_PT0_SYSTEM_APERTURE_HIGH_ADDR, rdev->mc.vram_end);
    611  1.1  riastrad 
    612  1.1  riastrad 	/* enable page tables */
    613  1.1  riastrad 	tmp = RREG32_MC(R_000100_MC_PT0_CNTL);
    614  1.1  riastrad 	WREG32_MC(R_000100_MC_PT0_CNTL, (tmp | S_000100_ENABLE_PT(1)));
    615  1.1  riastrad 	tmp = RREG32_MC(R_000009_MC_CNTL1);
    616  1.1  riastrad 	WREG32_MC(R_000009_MC_CNTL1, (tmp | S_000009_ENABLE_PAGE_TABLES(1)));
    617  1.1  riastrad 	rs600_gart_tlb_flush(rdev);
    618  1.1  riastrad 	DRM_INFO("PCIE GART of %uM enabled (table at 0x%016llX).\n",
    619  1.1  riastrad 		 (unsigned)(rdev->mc.gtt_size >> 20),
    620  1.1  riastrad 		 (unsigned long long)rdev->gart.table_addr);
    621  1.1  riastrad 	rdev->gart.ready = true;
    622  1.1  riastrad 	return 0;
    623  1.1  riastrad }
    624  1.1  riastrad 
    625  1.1  riastrad static void rs600_gart_disable(struct radeon_device *rdev)
    626  1.1  riastrad {
    627  1.1  riastrad 	u32 tmp;
    628  1.1  riastrad 
    629  1.1  riastrad 	/* FIXME: disable out of gart access */
    630  1.1  riastrad 	WREG32_MC(R_000100_MC_PT0_CNTL, 0);
    631  1.1  riastrad 	tmp = RREG32_MC(R_000009_MC_CNTL1);
    632  1.1  riastrad 	WREG32_MC(R_000009_MC_CNTL1, tmp & C_000009_ENABLE_PAGE_TABLES);
    633  1.1  riastrad 	radeon_gart_table_vram_unpin(rdev);
    634  1.1  riastrad }
    635  1.1  riastrad 
    636  1.1  riastrad static void rs600_gart_fini(struct radeon_device *rdev)
    637  1.1  riastrad {
    638  1.1  riastrad 	radeon_gart_fini(rdev);
    639  1.1  riastrad 	rs600_gart_disable(rdev);
    640  1.1  riastrad 	radeon_gart_table_vram_free(rdev);
    641  1.1  riastrad }
    642  1.1  riastrad 
    643  1.1  riastrad uint64_t rs600_gart_get_page_entry(uint64_t addr, uint32_t flags)
    644  1.1  riastrad {
    645  1.1  riastrad 	addr = addr & 0xFFFFFFFFFFFFF000ULL;
    646  1.1  riastrad 	addr |= R600_PTE_SYSTEM;
    647  1.1  riastrad 	if (flags & RADEON_GART_PAGE_VALID)
    648  1.1  riastrad 		addr |= R600_PTE_VALID;
    649  1.1  riastrad 	if (flags & RADEON_GART_PAGE_READ)
    650  1.1  riastrad 		addr |= R600_PTE_READABLE;
    651  1.1  riastrad 	if (flags & RADEON_GART_PAGE_WRITE)
    652  1.1  riastrad 		addr |= R600_PTE_WRITEABLE;
    653  1.1  riastrad 	if (flags & RADEON_GART_PAGE_SNOOP)
    654  1.1  riastrad 		addr |= R600_PTE_SNOOPED;
    655  1.1  riastrad 	return addr;
    656  1.1  riastrad }
    657  1.1  riastrad 
    658  1.1  riastrad #ifdef __NetBSD__
    659  1.1  riastrad #  define	__iomem	volatile
    660  1.1  riastrad #  define	writeq	fake_writeq
    661  1.1  riastrad 
    662  1.1  riastrad static inline void
    663  1.1  riastrad fake_writeq(uint64_t v, void __iomem *ptr)
    664  1.1  riastrad {
    665  1.1  riastrad 
    666  1.1  riastrad 	membar_producer();
    667  1.1  riastrad 	*(uint64_t __iomem *)ptr = v;
    668  1.1  riastrad }
    669  1.1  riastrad #endif
    670  1.1  riastrad 
    671  1.1  riastrad void rs600_gart_set_page(struct radeon_device *rdev, unsigned i,
    672  1.1  riastrad 			 uint64_t entry)
    673  1.1  riastrad {
    674  1.1  riastrad 	void __iomem *ptr = (void *)rdev->gart.ptr;
    675  1.1  riastrad 	writeq(entry, (char __iomem *)ptr + (i * 8));
    676  1.1  riastrad }
    677  1.1  riastrad 
    678  1.1  riastrad #ifdef __NetBSD__
    679  1.1  riastrad #  undef	writeq
    680  1.1  riastrad #  undef	__iomem
    681  1.1  riastrad #endif
    682  1.1  riastrad 
    683  1.1  riastrad int rs600_irq_set(struct radeon_device *rdev)
    684  1.1  riastrad {
    685  1.1  riastrad 	uint32_t tmp = 0;
    686  1.1  riastrad 	uint32_t mode_int = 0;
    687  1.1  riastrad 	u32 hpd1 = RREG32(R_007D08_DC_HOT_PLUG_DETECT1_INT_CONTROL) &
    688  1.1  riastrad 		~S_007D08_DC_HOT_PLUG_DETECT1_INT_EN(1);
    689  1.1  riastrad 	u32 hpd2 = RREG32(R_007D18_DC_HOT_PLUG_DETECT2_INT_CONTROL) &
    690  1.1  riastrad 		~S_007D18_DC_HOT_PLUG_DETECT2_INT_EN(1);
    691  1.1  riastrad 	u32 hdmi0;
    692  1.1  riastrad 	if (ASIC_IS_DCE2(rdev))
    693  1.1  riastrad 		hdmi0 = RREG32(R_007408_HDMI0_AUDIO_PACKET_CONTROL) &
    694  1.1  riastrad 			~S_007408_HDMI0_AZ_FORMAT_WTRIG_MASK(1);
    695  1.1  riastrad 	else
    696  1.1  riastrad 		hdmi0 = 0;
    697  1.1  riastrad 
    698  1.1  riastrad 	if (!rdev->irq.installed) {
    699  1.1  riastrad 		WARN(1, "Can't enable IRQ/MSI because no handler is installed\n");
    700  1.1  riastrad 		WREG32(R_000040_GEN_INT_CNTL, 0);
    701  1.1  riastrad 		return -EINVAL;
    702  1.1  riastrad 	}
    703  1.1  riastrad 	if (atomic_read(&rdev->irq.ring_int[RADEON_RING_TYPE_GFX_INDEX])) {
    704  1.1  riastrad 		tmp |= S_000040_SW_INT_EN(1);
    705  1.1  riastrad 	}
    706  1.1  riastrad 	if (rdev->irq.crtc_vblank_int[0] ||
    707  1.1  riastrad 	    atomic_read(&rdev->irq.pflip[0])) {
    708  1.1  riastrad 		mode_int |= S_006540_D1MODE_VBLANK_INT_MASK(1);
    709  1.1  riastrad 	}
    710  1.1  riastrad 	if (rdev->irq.crtc_vblank_int[1] ||
    711  1.1  riastrad 	    atomic_read(&rdev->irq.pflip[1])) {
    712  1.1  riastrad 		mode_int |= S_006540_D2MODE_VBLANK_INT_MASK(1);
    713  1.1  riastrad 	}
    714  1.1  riastrad 	if (rdev->irq.hpd[0]) {
    715  1.1  riastrad 		hpd1 |= S_007D08_DC_HOT_PLUG_DETECT1_INT_EN(1);
    716  1.1  riastrad 	}
    717  1.1  riastrad 	if (rdev->irq.hpd[1]) {
    718  1.1  riastrad 		hpd2 |= S_007D18_DC_HOT_PLUG_DETECT2_INT_EN(1);
    719  1.1  riastrad 	}
    720  1.1  riastrad 	if (rdev->irq.afmt[0]) {
    721  1.1  riastrad 		hdmi0 |= S_007408_HDMI0_AZ_FORMAT_WTRIG_MASK(1);
    722  1.1  riastrad 	}
    723  1.1  riastrad 	WREG32(R_000040_GEN_INT_CNTL, tmp);
    724  1.1  riastrad 	WREG32(R_006540_DxMODE_INT_MASK, mode_int);
    725  1.1  riastrad 	WREG32(R_007D08_DC_HOT_PLUG_DETECT1_INT_CONTROL, hpd1);
    726  1.1  riastrad 	WREG32(R_007D18_DC_HOT_PLUG_DETECT2_INT_CONTROL, hpd2);
    727  1.1  riastrad 	if (ASIC_IS_DCE2(rdev))
    728  1.1  riastrad 		WREG32(R_007408_HDMI0_AUDIO_PACKET_CONTROL, hdmi0);
    729  1.1  riastrad 
    730  1.1  riastrad 	/* posting read */
    731  1.1  riastrad 	RREG32(R_000040_GEN_INT_CNTL);
    732  1.1  riastrad 
    733  1.1  riastrad 	return 0;
    734  1.1  riastrad }
    735  1.1  riastrad 
    736  1.1  riastrad static inline u32 rs600_irq_ack(struct radeon_device *rdev)
    737  1.1  riastrad {
    738  1.1  riastrad 	uint32_t irqs = RREG32(R_000044_GEN_INT_STATUS);
    739  1.1  riastrad 	uint32_t irq_mask = S_000044_SW_INT(1);
    740  1.1  riastrad 	u32 tmp;
    741  1.1  riastrad 
    742  1.1  riastrad 	if (G_000044_DISPLAY_INT_STAT(irqs)) {
    743  1.1  riastrad 		rdev->irq.stat_regs.r500.disp_int = RREG32(R_007EDC_DISP_INTERRUPT_STATUS);
    744  1.1  riastrad 		if (G_007EDC_LB_D1_VBLANK_INTERRUPT(rdev->irq.stat_regs.r500.disp_int)) {
    745  1.1  riastrad 			WREG32(R_006534_D1MODE_VBLANK_STATUS,
    746  1.1  riastrad 				S_006534_D1MODE_VBLANK_ACK(1));
    747  1.1  riastrad 		}
    748  1.1  riastrad 		if (G_007EDC_LB_D2_VBLANK_INTERRUPT(rdev->irq.stat_regs.r500.disp_int)) {
    749  1.1  riastrad 			WREG32(R_006D34_D2MODE_VBLANK_STATUS,
    750  1.1  riastrad 				S_006D34_D2MODE_VBLANK_ACK(1));
    751  1.1  riastrad 		}
    752  1.1  riastrad 		if (G_007EDC_DC_HOT_PLUG_DETECT1_INTERRUPT(rdev->irq.stat_regs.r500.disp_int)) {
    753  1.1  riastrad 			tmp = RREG32(R_007D08_DC_HOT_PLUG_DETECT1_INT_CONTROL);
    754  1.1  riastrad 			tmp |= S_007D08_DC_HOT_PLUG_DETECT1_INT_ACK(1);
    755  1.1  riastrad 			WREG32(R_007D08_DC_HOT_PLUG_DETECT1_INT_CONTROL, tmp);
    756  1.1  riastrad 		}
    757  1.1  riastrad 		if (G_007EDC_DC_HOT_PLUG_DETECT2_INTERRUPT(rdev->irq.stat_regs.r500.disp_int)) {
    758  1.1  riastrad 			tmp = RREG32(R_007D18_DC_HOT_PLUG_DETECT2_INT_CONTROL);
    759  1.1  riastrad 			tmp |= S_007D18_DC_HOT_PLUG_DETECT2_INT_ACK(1);
    760  1.1  riastrad 			WREG32(R_007D18_DC_HOT_PLUG_DETECT2_INT_CONTROL, tmp);
    761  1.1  riastrad 		}
    762  1.1  riastrad 	} else {
    763  1.1  riastrad 		rdev->irq.stat_regs.r500.disp_int = 0;
    764  1.1  riastrad 	}
    765  1.1  riastrad 
    766  1.1  riastrad 	if (ASIC_IS_DCE2(rdev)) {
    767  1.1  riastrad 		rdev->irq.stat_regs.r500.hdmi0_status = RREG32(R_007404_HDMI0_STATUS) &
    768  1.1  riastrad 			S_007404_HDMI0_AZ_FORMAT_WTRIG(1);
    769  1.1  riastrad 		if (G_007404_HDMI0_AZ_FORMAT_WTRIG(rdev->irq.stat_regs.r500.hdmi0_status)) {
    770  1.1  riastrad 			tmp = RREG32(R_007408_HDMI0_AUDIO_PACKET_CONTROL);
    771  1.1  riastrad 			tmp |= S_007408_HDMI0_AZ_FORMAT_WTRIG_ACK(1);
    772  1.1  riastrad 			WREG32(R_007408_HDMI0_AUDIO_PACKET_CONTROL, tmp);
    773  1.1  riastrad 		}
    774  1.1  riastrad 	} else
    775  1.1  riastrad 		rdev->irq.stat_regs.r500.hdmi0_status = 0;
    776  1.1  riastrad 
    777  1.1  riastrad 	if (irqs) {
    778  1.1  riastrad 		WREG32(R_000044_GEN_INT_STATUS, irqs);
    779  1.1  riastrad 	}
    780  1.1  riastrad 	return irqs & irq_mask;
    781  1.1  riastrad }
    782  1.1  riastrad 
    783  1.1  riastrad void rs600_irq_disable(struct radeon_device *rdev)
    784  1.1  riastrad {
    785  1.1  riastrad 	u32 hdmi0 = RREG32(R_007408_HDMI0_AUDIO_PACKET_CONTROL) &
    786  1.1  riastrad 		~S_007408_HDMI0_AZ_FORMAT_WTRIG_MASK(1);
    787  1.1  riastrad 	WREG32(R_007408_HDMI0_AUDIO_PACKET_CONTROL, hdmi0);
    788  1.1  riastrad 	WREG32(R_000040_GEN_INT_CNTL, 0);
    789  1.1  riastrad 	WREG32(R_006540_DxMODE_INT_MASK, 0);
    790  1.1  riastrad 	/* Wait and acknowledge irq */
    791  1.1  riastrad 	mdelay(1);
    792  1.1  riastrad 	rs600_irq_ack(rdev);
    793  1.1  riastrad }
    794  1.1  riastrad 
    795  1.1  riastrad int rs600_irq_process(struct radeon_device *rdev)
    796  1.1  riastrad {
    797  1.1  riastrad 	u32 status, msi_rearm;
    798  1.1  riastrad 	bool queue_hotplug = false;
    799  1.1  riastrad 	bool queue_hdmi = false;
    800  1.1  riastrad 
    801  1.1  riastrad 	status = rs600_irq_ack(rdev);
    802  1.1  riastrad 	if (!status &&
    803  1.1  riastrad 	    !rdev->irq.stat_regs.r500.disp_int &&
    804  1.1  riastrad 	    !rdev->irq.stat_regs.r500.hdmi0_status) {
    805  1.1  riastrad 		return IRQ_NONE;
    806  1.1  riastrad 	}
    807  1.1  riastrad 	while (status ||
    808  1.1  riastrad 	       rdev->irq.stat_regs.r500.disp_int ||
    809  1.1  riastrad 	       rdev->irq.stat_regs.r500.hdmi0_status) {
    810  1.1  riastrad 		/* SW interrupt */
    811  1.1  riastrad 		if (G_000044_SW_INT(status)) {
    812  1.1  riastrad 			radeon_fence_process(rdev, RADEON_RING_TYPE_GFX_INDEX);
    813  1.1  riastrad 		}
    814  1.1  riastrad 		/* Vertical blank interrupts */
    815  1.1  riastrad 		if (G_007EDC_LB_D1_VBLANK_INTERRUPT(rdev->irq.stat_regs.r500.disp_int)) {
    816  1.1  riastrad 			if (rdev->irq.crtc_vblank_int[0]) {
    817  1.1  riastrad 				drm_handle_vblank(rdev->ddev, 0);
    818  1.1  riastrad #ifdef __NetBSD__
    819  1.1  riastrad 				spin_lock(&rdev->irq.vblank_lock);
    820  1.1  riastrad 				rdev->pm.vblank_sync = true;
    821  1.1  riastrad 				DRM_SPIN_WAKEUP_ONE(&rdev->irq.vblank_queue, &rdev->irq.vblank_lock);
    822  1.1  riastrad 				spin_unlock(&rdev->irq.vblank_lock);
    823  1.1  riastrad #else
    824  1.1  riastrad 				rdev->pm.vblank_sync = true;
    825  1.1  riastrad 				wake_up(&rdev->irq.vblank_queue);
    826  1.1  riastrad #endif
    827  1.1  riastrad 			}
    828  1.1  riastrad 			if (atomic_read(&rdev->irq.pflip[0]))
    829  1.1  riastrad 				radeon_crtc_handle_vblank(rdev, 0);
    830  1.1  riastrad 		}
    831  1.1  riastrad 		if (G_007EDC_LB_D2_VBLANK_INTERRUPT(rdev->irq.stat_regs.r500.disp_int)) {
    832  1.1  riastrad 			if (rdev->irq.crtc_vblank_int[1]) {
    833  1.1  riastrad 				drm_handle_vblank(rdev->ddev, 1);
    834  1.1  riastrad #ifdef __NetBSD__
    835  1.1  riastrad 				spin_lock(&rdev->irq.vblank_lock);
    836  1.1  riastrad 				rdev->pm.vblank_sync = true;
    837  1.1  riastrad 				DRM_SPIN_WAKEUP_ONE(&rdev->irq.vblank_queue, &rdev->irq.vblank_lock);
    838  1.1  riastrad 				spin_unlock(&rdev->irq.vblank_lock);
    839  1.1  riastrad #else
    840  1.1  riastrad 				rdev->pm.vblank_sync = true;
    841  1.1  riastrad 				wake_up(&rdev->irq.vblank_queue);
    842  1.1  riastrad #endif
    843  1.1  riastrad 			}
    844  1.1  riastrad 			if (atomic_read(&rdev->irq.pflip[1]))
    845  1.1  riastrad 				radeon_crtc_handle_vblank(rdev, 1);
    846  1.1  riastrad 		}
    847  1.1  riastrad 		if (G_007EDC_DC_HOT_PLUG_DETECT1_INTERRUPT(rdev->irq.stat_regs.r500.disp_int)) {
    848  1.1  riastrad 			queue_hotplug = true;
    849  1.1  riastrad 			DRM_DEBUG("HPD1\n");
    850  1.1  riastrad 		}
    851  1.1  riastrad 		if (G_007EDC_DC_HOT_PLUG_DETECT2_INTERRUPT(rdev->irq.stat_regs.r500.disp_int)) {
    852  1.1  riastrad 			queue_hotplug = true;
    853  1.1  riastrad 			DRM_DEBUG("HPD2\n");
    854  1.1  riastrad 		}
    855  1.1  riastrad 		if (G_007404_HDMI0_AZ_FORMAT_WTRIG(rdev->irq.stat_regs.r500.hdmi0_status)) {
    856  1.1  riastrad 			queue_hdmi = true;
    857  1.1  riastrad 			DRM_DEBUG("HDMI0\n");
    858  1.1  riastrad 		}
    859  1.1  riastrad 		status = rs600_irq_ack(rdev);
    860  1.1  riastrad 	}
    861  1.1  riastrad 	if (queue_hotplug)
    862  1.1  riastrad 		schedule_delayed_work(&rdev->hotplug_work, 0);
    863  1.1  riastrad 	if (queue_hdmi)
    864  1.1  riastrad 		schedule_work(&rdev->audio_work);
    865  1.1  riastrad 	if (rdev->msi_enabled) {
    866  1.1  riastrad 		switch (rdev->family) {
    867  1.1  riastrad 		case CHIP_RS600:
    868  1.1  riastrad 		case CHIP_RS690:
    869  1.1  riastrad 		case CHIP_RS740:
    870  1.1  riastrad 			msi_rearm = RREG32(RADEON_BUS_CNTL) & ~RS600_MSI_REARM;
    871  1.1  riastrad 			WREG32(RADEON_BUS_CNTL, msi_rearm);
    872  1.1  riastrad 			WREG32(RADEON_BUS_CNTL, msi_rearm | RS600_MSI_REARM);
    873  1.1  riastrad 			break;
    874  1.1  riastrad 		default:
    875  1.1  riastrad 			WREG32(RADEON_MSI_REARM_EN, RV370_MSI_REARM_EN);
    876  1.1  riastrad 			break;
    877  1.1  riastrad 		}
    878  1.1  riastrad 	}
    879  1.1  riastrad 	return IRQ_HANDLED;
    880  1.1  riastrad }
    881  1.1  riastrad 
    882  1.1  riastrad u32 rs600_get_vblank_counter(struct radeon_device *rdev, int crtc)
    883  1.1  riastrad {
    884  1.1  riastrad 	if (crtc == 0)
    885  1.1  riastrad 		return RREG32(R_0060A4_D1CRTC_STATUS_FRAME_COUNT);
    886  1.1  riastrad 	else
    887  1.1  riastrad 		return RREG32(R_0068A4_D2CRTC_STATUS_FRAME_COUNT);
    888  1.1  riastrad }
    889  1.1  riastrad 
    890  1.1  riastrad int rs600_mc_wait_for_idle(struct radeon_device *rdev)
    891  1.1  riastrad {
    892  1.1  riastrad 	unsigned i;
    893  1.1  riastrad 
    894  1.1  riastrad 	for (i = 0; i < rdev->usec_timeout; i++) {
    895  1.1  riastrad 		if (G_000000_MC_IDLE(RREG32_MC(R_000000_MC_STATUS)))
    896  1.1  riastrad 			return 0;
    897  1.1  riastrad 		udelay(1);
    898  1.1  riastrad 	}
    899  1.1  riastrad 	return -1;
    900  1.1  riastrad }
    901  1.1  riastrad 
    902  1.1  riastrad static void rs600_gpu_init(struct radeon_device *rdev)
    903  1.1  riastrad {
    904  1.1  riastrad 	r420_pipes_init(rdev);
    905  1.1  riastrad 	/* Wait for mc idle */
    906  1.1  riastrad 	if (rs600_mc_wait_for_idle(rdev))
    907  1.1  riastrad 		dev_warn(rdev->dev, "Wait MC idle timeout before updating MC.\n");
    908  1.1  riastrad }
    909  1.1  riastrad 
    910  1.1  riastrad static void rs600_mc_init(struct radeon_device *rdev)
    911  1.1  riastrad {
    912  1.1  riastrad 	u64 base;
    913  1.1  riastrad 
    914  1.1  riastrad 	rdev->mc.aper_base = pci_resource_start(rdev->pdev, 0);
    915  1.1  riastrad 	rdev->mc.aper_size = pci_resource_len(rdev->pdev, 0);
    916  1.1  riastrad 	rdev->mc.vram_is_ddr = true;
    917  1.1  riastrad 	rdev->mc.vram_width = 128;
    918  1.1  riastrad 	rdev->mc.real_vram_size = RREG32(RADEON_CONFIG_MEMSIZE);
    919  1.1  riastrad 	rdev->mc.mc_vram_size = rdev->mc.real_vram_size;
    920  1.1  riastrad 	rdev->mc.visible_vram_size = rdev->mc.aper_size;
    921  1.1  riastrad 	rdev->mc.igp_sideport_enabled = radeon_atombios_sideport_present(rdev);
    922  1.1  riastrad 	base = RREG32_MC(R_000004_MC_FB_LOCATION);
    923  1.1  riastrad 	base = G_000004_MC_FB_START(base) << 16;
    924  1.1  riastrad 	radeon_vram_location(rdev, &rdev->mc, base);
    925  1.1  riastrad 	rdev->mc.gtt_base_align = 0;
    926  1.1  riastrad 	radeon_gtt_location(rdev, &rdev->mc);
    927  1.1  riastrad 	radeon_update_bandwidth_info(rdev);
    928  1.1  riastrad }
    929  1.1  riastrad 
    930  1.1  riastrad void rs600_bandwidth_update(struct radeon_device *rdev)
    931  1.1  riastrad {
    932  1.1  riastrad 	struct drm_display_mode *mode0 = NULL;
    933  1.1  riastrad 	struct drm_display_mode *mode1 = NULL;
    934  1.1  riastrad 	u32 d1mode_priority_a_cnt, d2mode_priority_a_cnt;
    935  1.1  riastrad 	/* FIXME: implement full support */
    936  1.1  riastrad 
    937  1.1  riastrad 	if (!rdev->mode_info.mode_config_initialized)
    938  1.1  riastrad 		return;
    939  1.1  riastrad 
    940  1.1  riastrad 	radeon_update_display_priority(rdev);
    941  1.1  riastrad 
    942  1.1  riastrad 	if (rdev->mode_info.crtcs[0]->base.enabled)
    943  1.1  riastrad 		mode0 = &rdev->mode_info.crtcs[0]->base.mode;
    944  1.1  riastrad 	if (rdev->mode_info.crtcs[1]->base.enabled)
    945  1.1  riastrad 		mode1 = &rdev->mode_info.crtcs[1]->base.mode;
    946  1.1  riastrad 
    947  1.1  riastrad 	rs690_line_buffer_adjust(rdev, mode0, mode1);
    948  1.1  riastrad 
    949  1.1  riastrad 	if (rdev->disp_priority == 2) {
    950  1.1  riastrad 		d1mode_priority_a_cnt = RREG32(R_006548_D1MODE_PRIORITY_A_CNT);
    951  1.1  riastrad 		d2mode_priority_a_cnt = RREG32(R_006D48_D2MODE_PRIORITY_A_CNT);
    952  1.1  riastrad 		d1mode_priority_a_cnt |= S_006548_D1MODE_PRIORITY_A_ALWAYS_ON(1);
    953  1.1  riastrad 		d2mode_priority_a_cnt |= S_006D48_D2MODE_PRIORITY_A_ALWAYS_ON(1);
    954  1.1  riastrad 		WREG32(R_006548_D1MODE_PRIORITY_A_CNT, d1mode_priority_a_cnt);
    955  1.1  riastrad 		WREG32(R_00654C_D1MODE_PRIORITY_B_CNT, d1mode_priority_a_cnt);
    956  1.1  riastrad 		WREG32(R_006D48_D2MODE_PRIORITY_A_CNT, d2mode_priority_a_cnt);
    957  1.1  riastrad 		WREG32(R_006D4C_D2MODE_PRIORITY_B_CNT, d2mode_priority_a_cnt);
    958  1.1  riastrad 	}
    959  1.1  riastrad }
    960  1.1  riastrad 
    961  1.1  riastrad uint32_t rs600_mc_rreg(struct radeon_device *rdev, uint32_t reg)
    962  1.1  riastrad {
    963  1.1  riastrad 	unsigned long flags;
    964  1.1  riastrad 	u32 r;
    965  1.1  riastrad 
    966  1.1  riastrad 	spin_lock_irqsave(&rdev->mc_idx_lock, flags);
    967  1.1  riastrad 	WREG32(R_000070_MC_IND_INDEX, S_000070_MC_IND_ADDR(reg) |
    968  1.1  riastrad 		S_000070_MC_IND_CITF_ARB0(1));
    969  1.1  riastrad 	r = RREG32(R_000074_MC_IND_DATA);
    970  1.1  riastrad 	spin_unlock_irqrestore(&rdev->mc_idx_lock, flags);
    971  1.1  riastrad 	return r;
    972  1.1  riastrad }
    973  1.1  riastrad 
    974  1.1  riastrad void rs600_mc_wreg(struct radeon_device *rdev, uint32_t reg, uint32_t v)
    975  1.1  riastrad {
    976  1.1  riastrad 	unsigned long flags;
    977  1.1  riastrad 
    978  1.1  riastrad 	spin_lock_irqsave(&rdev->mc_idx_lock, flags);
    979  1.1  riastrad 	WREG32(R_000070_MC_IND_INDEX, S_000070_MC_IND_ADDR(reg) |
    980  1.1  riastrad 		S_000070_MC_IND_CITF_ARB0(1) | S_000070_MC_IND_WR_EN(1));
    981  1.1  riastrad 	WREG32(R_000074_MC_IND_DATA, v);
    982  1.1  riastrad 	spin_unlock_irqrestore(&rdev->mc_idx_lock, flags);
    983  1.1  riastrad }
    984  1.1  riastrad 
    985  1.1  riastrad static void rs600_debugfs(struct radeon_device *rdev)
    986  1.1  riastrad {
    987  1.1  riastrad 	if (r100_debugfs_rbbm_init(rdev))
    988  1.1  riastrad 		DRM_ERROR("Failed to register debugfs file for RBBM !\n");
    989  1.1  riastrad }
    990  1.1  riastrad 
    991  1.1  riastrad void rs600_set_safe_registers(struct radeon_device *rdev)
    992  1.1  riastrad {
    993  1.1  riastrad 	rdev->config.r300.reg_safe_bm = rs600_reg_safe_bm;
    994  1.1  riastrad 	rdev->config.r300.reg_safe_bm_size = ARRAY_SIZE(rs600_reg_safe_bm);
    995  1.1  riastrad }
    996  1.1  riastrad 
    997  1.1  riastrad static void rs600_mc_program(struct radeon_device *rdev)
    998  1.1  riastrad {
    999  1.1  riastrad 	struct rv515_mc_save save;
   1000  1.1  riastrad 
   1001  1.1  riastrad 	/* Stops all mc clients */
   1002  1.1  riastrad 	rv515_mc_stop(rdev, &save);
   1003  1.1  riastrad 
   1004  1.1  riastrad 	/* Wait for mc idle */
   1005  1.1  riastrad 	if (rs600_mc_wait_for_idle(rdev))
   1006  1.1  riastrad 		dev_warn(rdev->dev, "Wait MC idle timeout before updating MC.\n");
   1007  1.1  riastrad 
   1008  1.1  riastrad 	/* FIXME: What does AGP means for such chipset ? */
   1009  1.1  riastrad 	WREG32_MC(R_000005_MC_AGP_LOCATION, 0x0FFFFFFF);
   1010  1.1  riastrad 	WREG32_MC(R_000006_AGP_BASE, 0);
   1011  1.1  riastrad 	WREG32_MC(R_000007_AGP_BASE_2, 0);
   1012  1.1  riastrad 	/* Program MC */
   1013  1.1  riastrad 	WREG32_MC(R_000004_MC_FB_LOCATION,
   1014  1.1  riastrad 			S_000004_MC_FB_START(rdev->mc.vram_start >> 16) |
   1015  1.1  riastrad 			S_000004_MC_FB_TOP(rdev->mc.vram_end >> 16));
   1016  1.1  riastrad 	WREG32(R_000134_HDP_FB_LOCATION,
   1017  1.1  riastrad 		S_000134_HDP_FB_START(rdev->mc.vram_start >> 16));
   1018  1.1  riastrad 
   1019  1.1  riastrad 	rv515_mc_resume(rdev, &save);
   1020  1.1  riastrad }
   1021  1.1  riastrad 
   1022  1.1  riastrad static int rs600_startup(struct radeon_device *rdev)
   1023  1.1  riastrad {
   1024  1.1  riastrad 	int r;
   1025  1.1  riastrad 
   1026  1.1  riastrad 	rs600_mc_program(rdev);
   1027  1.1  riastrad 	/* Resume clock */
   1028  1.1  riastrad 	rv515_clock_startup(rdev);
   1029  1.1  riastrad 	/* Initialize GPU configuration (# pipes, ...) */
   1030  1.1  riastrad 	rs600_gpu_init(rdev);
   1031  1.1  riastrad 	/* Initialize GART (initialize after TTM so we can allocate
   1032  1.1  riastrad 	 * memory through TTM but finalize after TTM) */
   1033  1.1  riastrad 	r = rs600_gart_enable(rdev);
   1034  1.1  riastrad 	if (r)
   1035  1.1  riastrad 		return r;
   1036  1.1  riastrad 
   1037  1.1  riastrad 	/* allocate wb buffer */
   1038  1.1  riastrad 	r = radeon_wb_init(rdev);
   1039  1.1  riastrad 	if (r)
   1040  1.1  riastrad 		return r;
   1041  1.1  riastrad 
   1042  1.1  riastrad 	r = radeon_fence_driver_start_ring(rdev, RADEON_RING_TYPE_GFX_INDEX);
   1043  1.1  riastrad 	if (r) {
   1044  1.1  riastrad 		dev_err(rdev->dev, "failed initializing CP fences (%d).\n", r);
   1045  1.1  riastrad 		return r;
   1046  1.1  riastrad 	}
   1047  1.1  riastrad 
   1048  1.1  riastrad 	/* Enable IRQ */
   1049  1.1  riastrad 	if (!rdev->irq.installed) {
   1050  1.1  riastrad 		r = radeon_irq_kms_init(rdev);
   1051  1.1  riastrad 		if (r)
   1052  1.1  riastrad 			return r;
   1053  1.1  riastrad 	}
   1054  1.1  riastrad 
   1055  1.1  riastrad 	rs600_irq_set(rdev);
   1056  1.1  riastrad 	rdev->config.r300.hdp_cntl = RREG32(RADEON_HOST_PATH_CNTL);
   1057  1.1  riastrad 	/* 1M ring buffer */
   1058  1.1  riastrad 	r = r100_cp_init(rdev, 1024 * 1024);
   1059  1.1  riastrad 	if (r) {
   1060  1.1  riastrad 		dev_err(rdev->dev, "failed initializing CP (%d).\n", r);
   1061  1.1  riastrad 		return r;
   1062  1.1  riastrad 	}
   1063  1.1  riastrad 
   1064  1.1  riastrad 	r = radeon_ib_pool_init(rdev);
   1065  1.1  riastrad 	if (r) {
   1066  1.1  riastrad 		dev_err(rdev->dev, "IB initialization failed (%d).\n", r);
   1067  1.1  riastrad 		return r;
   1068  1.1  riastrad 	}
   1069  1.1  riastrad 
   1070  1.1  riastrad 	r = radeon_audio_init(rdev);
   1071  1.1  riastrad 	if (r) {
   1072  1.1  riastrad 		dev_err(rdev->dev, "failed initializing audio\n");
   1073  1.1  riastrad 		return r;
   1074  1.1  riastrad 	}
   1075  1.1  riastrad 
   1076  1.1  riastrad 	return 0;
   1077  1.1  riastrad }
   1078  1.1  riastrad 
   1079  1.1  riastrad int rs600_resume(struct radeon_device *rdev)
   1080  1.1  riastrad {
   1081  1.1  riastrad 	int r;
   1082  1.1  riastrad 
   1083  1.1  riastrad 	/* Make sur GART are not working */
   1084  1.1  riastrad 	rs600_gart_disable(rdev);
   1085  1.1  riastrad 	/* Resume clock before doing reset */
   1086  1.1  riastrad 	rv515_clock_startup(rdev);
   1087  1.1  riastrad 	/* Reset gpu before posting otherwise ATOM will enter infinite loop */
   1088  1.1  riastrad 	if (radeon_asic_reset(rdev)) {
   1089  1.1  riastrad 		dev_warn(rdev->dev, "GPU reset failed ! (0xE40=0x%08X, 0x7C0=0x%08X)\n",
   1090  1.1  riastrad 			RREG32(R_000E40_RBBM_STATUS),
   1091  1.1  riastrad 			RREG32(R_0007C0_CP_STAT));
   1092  1.1  riastrad 	}
   1093  1.1  riastrad 	/* post */
   1094  1.1  riastrad 	atom_asic_init(rdev->mode_info.atom_context);
   1095  1.1  riastrad 	/* Resume clock after posting */
   1096  1.1  riastrad 	rv515_clock_startup(rdev);
   1097  1.1  riastrad 	/* Initialize surface registers */
   1098  1.1  riastrad 	radeon_surface_init(rdev);
   1099  1.1  riastrad 
   1100  1.1  riastrad 	rdev->accel_working = true;
   1101  1.1  riastrad 	r = rs600_startup(rdev);
   1102  1.1  riastrad 	if (r) {
   1103  1.1  riastrad 		rdev->accel_working = false;
   1104  1.1  riastrad 	}
   1105  1.1  riastrad 	return r;
   1106  1.1  riastrad }
   1107  1.1  riastrad 
   1108  1.1  riastrad int rs600_suspend(struct radeon_device *rdev)
   1109  1.1  riastrad {
   1110  1.1  riastrad 	radeon_pm_suspend(rdev);
   1111  1.1  riastrad 	radeon_audio_fini(rdev);
   1112  1.1  riastrad 	r100_cp_disable(rdev);
   1113  1.1  riastrad 	radeon_wb_disable(rdev);
   1114  1.1  riastrad 	rs600_irq_disable(rdev);
   1115  1.1  riastrad 	rs600_gart_disable(rdev);
   1116  1.1  riastrad 	return 0;
   1117  1.1  riastrad }
   1118  1.1  riastrad 
   1119  1.1  riastrad void rs600_fini(struct radeon_device *rdev)
   1120  1.1  riastrad {
   1121  1.1  riastrad 	radeon_pm_fini(rdev);
   1122  1.1  riastrad 	radeon_audio_fini(rdev);
   1123  1.1  riastrad 	r100_cp_fini(rdev);
   1124  1.1  riastrad 	radeon_wb_fini(rdev);
   1125  1.1  riastrad 	radeon_ib_pool_fini(rdev);
   1126  1.1  riastrad 	radeon_gem_fini(rdev);
   1127  1.1  riastrad 	rs600_gart_fini(rdev);
   1128  1.1  riastrad 	radeon_irq_kms_fini(rdev);
   1129  1.1  riastrad 	radeon_fence_driver_fini(rdev);
   1130  1.1  riastrad 	radeon_bo_fini(rdev);
   1131  1.1  riastrad 	radeon_atombios_fini(rdev);
   1132  1.1  riastrad 	kfree(rdev->bios);
   1133  1.1  riastrad 	rdev->bios = NULL;
   1134  1.1  riastrad }
   1135  1.1  riastrad 
   1136  1.1  riastrad int rs600_init(struct radeon_device *rdev)
   1137  1.1  riastrad {
   1138  1.1  riastrad 	int r;
   1139  1.1  riastrad 
   1140  1.1  riastrad 	/* Disable VGA */
   1141  1.1  riastrad 	rv515_vga_render_disable(rdev);
   1142  1.1  riastrad 	/* Initialize scratch registers */
   1143  1.1  riastrad 	radeon_scratch_init(rdev);
   1144  1.1  riastrad 	/* Initialize surface registers */
   1145  1.1  riastrad 	radeon_surface_init(rdev);
   1146  1.1  riastrad 	/* restore some register to sane defaults */
   1147  1.1  riastrad 	r100_restore_sanity(rdev);
   1148  1.1  riastrad 	/* BIOS */
   1149  1.1  riastrad 	if (!radeon_get_bios(rdev)) {
   1150  1.1  riastrad 		if (ASIC_IS_AVIVO(rdev))
   1151  1.1  riastrad 			return -EINVAL;
   1152  1.1  riastrad 	}
   1153  1.1  riastrad 	if (rdev->is_atom_bios) {
   1154  1.1  riastrad 		r = radeon_atombios_init(rdev);
   1155  1.1  riastrad 		if (r)
   1156  1.1  riastrad 			return r;
   1157  1.1  riastrad 	} else {
   1158  1.1  riastrad 		dev_err(rdev->dev, "Expecting atombios for RS600 GPU\n");
   1159  1.1  riastrad 		return -EINVAL;
   1160  1.1  riastrad 	}
   1161  1.1  riastrad 	/* Reset gpu before posting otherwise ATOM will enter infinite loop */
   1162  1.1  riastrad 	if (radeon_asic_reset(rdev)) {
   1163  1.1  riastrad 		dev_warn(rdev->dev,
   1164  1.1  riastrad 			"GPU reset failed ! (0xE40=0x%08X, 0x7C0=0x%08X)\n",
   1165  1.1  riastrad 			RREG32(R_000E40_RBBM_STATUS),
   1166  1.1  riastrad 			RREG32(R_0007C0_CP_STAT));
   1167  1.1  riastrad 	}
   1168  1.1  riastrad 	/* check if cards are posted or not */
   1169  1.1  riastrad 	if (radeon_boot_test_post_card(rdev) == false)
   1170  1.1  riastrad 		return -EINVAL;
   1171  1.1  riastrad 
   1172  1.1  riastrad 	/* Initialize clocks */
   1173  1.1  riastrad 	radeon_get_clock_info(rdev->ddev);
   1174  1.1  riastrad 	/* initialize memory controller */
   1175  1.1  riastrad 	rs600_mc_init(rdev);
   1176  1.1  riastrad 	rs600_debugfs(rdev);
   1177  1.1  riastrad 	/* Fence driver */
   1178  1.1  riastrad 	r = radeon_fence_driver_init(rdev);
   1179  1.1  riastrad 	if (r)
   1180  1.1  riastrad 		return r;
   1181  1.1  riastrad 	/* Memory manager */
   1182  1.1  riastrad 	r = radeon_bo_init(rdev);
   1183  1.1  riastrad 	if (r)
   1184  1.1  riastrad 		return r;
   1185  1.1  riastrad 	r = rs600_gart_init(rdev);
   1186  1.1  riastrad 	if (r)
   1187  1.1  riastrad 		return r;
   1188  1.1  riastrad 	rs600_set_safe_registers(rdev);
   1189  1.1  riastrad 
   1190  1.1  riastrad 	/* Initialize power management */
   1191  1.1  riastrad 	radeon_pm_init(rdev);
   1192  1.1  riastrad 
   1193  1.1  riastrad 	rdev->accel_working = true;
   1194  1.1  riastrad 	r = rs600_startup(rdev);
   1195  1.1  riastrad 	if (r) {
   1196  1.1  riastrad 		/* Somethings want wront with the accel init stop accel */
   1197  1.1  riastrad 		dev_err(rdev->dev, "Disabling GPU acceleration\n");
   1198  1.1  riastrad 		r100_cp_fini(rdev);
   1199  1.1  riastrad 		radeon_wb_fini(rdev);
   1200  1.1  riastrad 		radeon_ib_pool_fini(rdev);
   1201  1.1  riastrad 		rs600_gart_fini(rdev);
   1202  1.1  riastrad 		radeon_irq_kms_fini(rdev);
   1203  1.1  riastrad 		rdev->accel_working = false;
   1204  1.1  riastrad 	}
   1205  1.1  riastrad 	return 0;
   1206  1.1  riastrad }
   1207