Home | History | Annotate | Line # | Download | only in radeon
      1  1.3  riastrad /*	$NetBSD: radeon_r300.c,v 1.3 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.3  riastrad 
     31  1.1  riastrad #include <sys/cdefs.h>
     32  1.3  riastrad __KERNEL_RCSID(0, "$NetBSD: radeon_r300.c,v 1.3 2021/12/18 23:45:43 riastradh Exp $");
     33  1.1  riastrad 
     34  1.3  riastrad #include <linux/pci.h>
     35  1.1  riastrad #include <linux/seq_file.h>
     36  1.1  riastrad #include <linux/slab.h>
     37  1.3  riastrad 
     38  1.1  riastrad #include <drm/drm.h>
     39  1.1  riastrad #include <drm/drm_crtc_helper.h>
     40  1.3  riastrad #include <drm/drm_debugfs.h>
     41  1.3  riastrad #include <drm/drm_device.h>
     42  1.3  riastrad #include <drm/drm_file.h>
     43  1.1  riastrad #include <drm/radeon_drm.h>
     44  1.3  riastrad 
     45  1.1  riastrad #include "r100_track.h"
     46  1.3  riastrad #include "r300_reg_safe.h"
     47  1.1  riastrad #include "r300d.h"
     48  1.3  riastrad #include "radeon.h"
     49  1.3  riastrad #include "radeon_asic.h"
     50  1.3  riastrad #include "radeon_reg.h"
     51  1.1  riastrad #include "rv350d.h"
     52  1.1  riastrad 
     53  1.1  riastrad /* This files gather functions specifics to: r300,r350,rv350,rv370,rv380
     54  1.1  riastrad  *
     55  1.1  riastrad  * GPU Errata:
     56  1.1  riastrad  * - HOST_PATH_CNTL: r300 family seems to dislike write to HOST_PATH_CNTL
     57  1.1  riastrad  *   using MMIO to flush host path read cache, this lead to HARDLOCKUP.
     58  1.1  riastrad  *   However, scheduling such write to the ring seems harmless, i suspect
     59  1.1  riastrad  *   the CP read collide with the flush somehow, or maybe the MC, hard to
     60  1.1  riastrad  *   tell. (Jerome Glisse)
     61  1.1  riastrad  */
     62  1.1  riastrad 
     63  1.1  riastrad /*
     64  1.1  riastrad  * Indirect registers accessor
     65  1.1  riastrad  */
     66  1.1  riastrad uint32_t rv370_pcie_rreg(struct radeon_device *rdev, uint32_t reg)
     67  1.1  riastrad {
     68  1.1  riastrad 	unsigned long flags;
     69  1.1  riastrad 	uint32_t r;
     70  1.1  riastrad 
     71  1.1  riastrad 	spin_lock_irqsave(&rdev->pcie_idx_lock, flags);
     72  1.1  riastrad 	WREG32(RADEON_PCIE_INDEX, ((reg) & rdev->pcie_reg_mask));
     73  1.1  riastrad 	r = RREG32(RADEON_PCIE_DATA);
     74  1.1  riastrad 	spin_unlock_irqrestore(&rdev->pcie_idx_lock, flags);
     75  1.1  riastrad 	return r;
     76  1.1  riastrad }
     77  1.1  riastrad 
     78  1.1  riastrad void rv370_pcie_wreg(struct radeon_device *rdev, uint32_t reg, uint32_t v)
     79  1.1  riastrad {
     80  1.1  riastrad 	unsigned long flags;
     81  1.1  riastrad 
     82  1.1  riastrad 	spin_lock_irqsave(&rdev->pcie_idx_lock, flags);
     83  1.1  riastrad 	WREG32(RADEON_PCIE_INDEX, ((reg) & rdev->pcie_reg_mask));
     84  1.1  riastrad 	WREG32(RADEON_PCIE_DATA, (v));
     85  1.1  riastrad 	spin_unlock_irqrestore(&rdev->pcie_idx_lock, flags);
     86  1.1  riastrad }
     87  1.1  riastrad 
     88  1.1  riastrad /*
     89  1.1  riastrad  * rv370,rv380 PCIE GART
     90  1.1  riastrad  */
     91  1.1  riastrad static int rv370_debugfs_pcie_gart_info_init(struct radeon_device *rdev);
     92  1.1  riastrad 
     93  1.1  riastrad void rv370_pcie_gart_tlb_flush(struct radeon_device *rdev)
     94  1.1  riastrad {
     95  1.1  riastrad 	uint32_t tmp;
     96  1.1  riastrad 	int i;
     97  1.1  riastrad 
     98  1.1  riastrad 	/* Workaround HW bug do flush 2 times */
     99  1.1  riastrad 	for (i = 0; i < 2; i++) {
    100  1.1  riastrad 		tmp = RREG32_PCIE(RADEON_PCIE_TX_GART_CNTL);
    101  1.1  riastrad 		WREG32_PCIE(RADEON_PCIE_TX_GART_CNTL, tmp | RADEON_PCIE_TX_GART_INVALIDATE_TLB);
    102  1.1  riastrad 		(void)RREG32_PCIE(RADEON_PCIE_TX_GART_CNTL);
    103  1.1  riastrad 		WREG32_PCIE(RADEON_PCIE_TX_GART_CNTL, tmp);
    104  1.1  riastrad 	}
    105  1.1  riastrad 	mb();
    106  1.1  riastrad }
    107  1.1  riastrad 
    108  1.1  riastrad #define R300_PTE_UNSNOOPED (1 << 0)
    109  1.1  riastrad #define R300_PTE_WRITEABLE (1 << 2)
    110  1.1  riastrad #define R300_PTE_READABLE  (1 << 3)
    111  1.1  riastrad 
    112  1.1  riastrad uint64_t rv370_pcie_gart_get_page_entry(uint64_t addr, uint32_t flags)
    113  1.1  riastrad {
    114  1.1  riastrad 	addr = (lower_32_bits(addr) >> 8) |
    115  1.1  riastrad 		((upper_32_bits(addr) & 0xff) << 24);
    116  1.1  riastrad 	if (flags & RADEON_GART_PAGE_READ)
    117  1.1  riastrad 		addr |= R300_PTE_READABLE;
    118  1.1  riastrad 	if (flags & RADEON_GART_PAGE_WRITE)
    119  1.1  riastrad 		addr |= R300_PTE_WRITEABLE;
    120  1.1  riastrad 	if (!(flags & RADEON_GART_PAGE_SNOOP))
    121  1.1  riastrad 		addr |= R300_PTE_UNSNOOPED;
    122  1.1  riastrad 	return addr;
    123  1.1  riastrad }
    124  1.1  riastrad 
    125  1.1  riastrad #ifdef __NetBSD__
    126  1.1  riastrad /*
    127  1.1  riastrad  * XXX Can't use bus_space here because this is all mapped through the
    128  1.1  riastrad  * radeon_bo abstraction.  Can't assume we're x86 because this is
    129  1.1  riastrad  * AMD/ATI Radeon, not Intel.
    130  1.1  riastrad  */
    131  1.1  riastrad 
    132  1.1  riastrad #  define	__iomem		volatile
    133  1.1  riastrad #  define	writel		fake_writel
    134  1.1  riastrad 
    135  1.1  riastrad static inline void
    136  1.1  riastrad fake_writel(uint32_t v, void __iomem *ptr)
    137  1.1  riastrad {
    138  1.1  riastrad 
    139  1.1  riastrad 	membar_producer();
    140  1.1  riastrad 	*(uint32_t __iomem *)ptr = v;
    141  1.1  riastrad }
    142  1.1  riastrad #endif
    143  1.1  riastrad 
    144  1.1  riastrad void rv370_pcie_gart_set_page(struct radeon_device *rdev, unsigned i,
    145  1.1  riastrad 			      uint64_t entry)
    146  1.1  riastrad {
    147  1.1  riastrad 	void __iomem *ptr = rdev->gart.ptr;
    148  1.1  riastrad 
    149  1.1  riastrad 	/* on x86 we want this to be CPU endian, on powerpc
    150  1.1  riastrad 	 * on powerpc without HW swappers, it'll get swapped on way
    151  1.1  riastrad 	 * into VRAM - so no need for cpu_to_le32 on VRAM tables */
    152  1.1  riastrad 	writel(entry, (uint8_t __iomem *)ptr + (i * 4));
    153  1.1  riastrad }
    154  1.1  riastrad 
    155  1.1  riastrad #ifdef __NetBSD__
    156  1.1  riastrad #  undef	__iomem
    157  1.1  riastrad #  undef	writel
    158  1.1  riastrad #endif
    159  1.1  riastrad 
    160  1.1  riastrad int rv370_pcie_gart_init(struct radeon_device *rdev)
    161  1.1  riastrad {
    162  1.1  riastrad 	int r;
    163  1.1  riastrad 
    164  1.1  riastrad 	if (rdev->gart.robj) {
    165  1.1  riastrad 		WARN(1, "RV370 PCIE GART already initialized\n");
    166  1.1  riastrad 		return 0;
    167  1.1  riastrad 	}
    168  1.1  riastrad 	/* Initialize common gart structure */
    169  1.1  riastrad 	r = radeon_gart_init(rdev);
    170  1.1  riastrad 	if (r)
    171  1.1  riastrad 		return r;
    172  1.1  riastrad 	r = rv370_debugfs_pcie_gart_info_init(rdev);
    173  1.1  riastrad 	if (r)
    174  1.1  riastrad 		DRM_ERROR("Failed to register debugfs file for PCIE gart !\n");
    175  1.1  riastrad 	rdev->gart.table_size = rdev->gart.num_gpu_pages * 4;
    176  1.1  riastrad 	rdev->asic->gart.tlb_flush = &rv370_pcie_gart_tlb_flush;
    177  1.1  riastrad 	rdev->asic->gart.get_page_entry = &rv370_pcie_gart_get_page_entry;
    178  1.1  riastrad 	rdev->asic->gart.set_page = &rv370_pcie_gart_set_page;
    179  1.1  riastrad 	return radeon_gart_table_vram_alloc(rdev);
    180  1.1  riastrad }
    181  1.1  riastrad 
    182  1.1  riastrad int rv370_pcie_gart_enable(struct radeon_device *rdev)
    183  1.1  riastrad {
    184  1.1  riastrad 	uint32_t table_addr;
    185  1.1  riastrad 	uint32_t tmp;
    186  1.1  riastrad 	int r;
    187  1.1  riastrad 
    188  1.1  riastrad 	if (rdev->gart.robj == NULL) {
    189  1.1  riastrad 		dev_err(rdev->dev, "No VRAM object for PCIE GART.\n");
    190  1.1  riastrad 		return -EINVAL;
    191  1.1  riastrad 	}
    192  1.1  riastrad 	r = radeon_gart_table_vram_pin(rdev);
    193  1.1  riastrad 	if (r)
    194  1.1  riastrad 		return r;
    195  1.1  riastrad 	/* discard memory request outside of configured range */
    196  1.1  riastrad 	tmp = RADEON_PCIE_TX_GART_UNMAPPED_ACCESS_DISCARD;
    197  1.1  riastrad 	WREG32_PCIE(RADEON_PCIE_TX_GART_CNTL, tmp);
    198  1.1  riastrad 	WREG32_PCIE(RADEON_PCIE_TX_GART_START_LO, rdev->mc.gtt_start);
    199  1.1  riastrad 	tmp = rdev->mc.gtt_end & ~RADEON_GPU_PAGE_MASK;
    200  1.1  riastrad 	WREG32_PCIE(RADEON_PCIE_TX_GART_END_LO, tmp);
    201  1.1  riastrad 	WREG32_PCIE(RADEON_PCIE_TX_GART_START_HI, 0);
    202  1.1  riastrad 	WREG32_PCIE(RADEON_PCIE_TX_GART_END_HI, 0);
    203  1.1  riastrad 	table_addr = rdev->gart.table_addr;
    204  1.1  riastrad 	WREG32_PCIE(RADEON_PCIE_TX_GART_BASE, table_addr);
    205  1.1  riastrad 	/* FIXME: setup default page */
    206  1.1  riastrad 	WREG32_PCIE(RADEON_PCIE_TX_DISCARD_RD_ADDR_LO, rdev->mc.vram_start);
    207  1.1  riastrad 	WREG32_PCIE(RADEON_PCIE_TX_DISCARD_RD_ADDR_HI, 0);
    208  1.1  riastrad 	/* Clear error */
    209  1.1  riastrad 	WREG32_PCIE(RADEON_PCIE_TX_GART_ERROR, 0);
    210  1.1  riastrad 	tmp = RREG32_PCIE(RADEON_PCIE_TX_GART_CNTL);
    211  1.1  riastrad 	tmp |= RADEON_PCIE_TX_GART_EN;
    212  1.1  riastrad 	tmp |= RADEON_PCIE_TX_GART_UNMAPPED_ACCESS_DISCARD;
    213  1.1  riastrad 	WREG32_PCIE(RADEON_PCIE_TX_GART_CNTL, tmp);
    214  1.1  riastrad 	rv370_pcie_gart_tlb_flush(rdev);
    215  1.1  riastrad 	DRM_INFO("PCIE GART of %uM enabled (table at 0x%016llX).\n",
    216  1.1  riastrad 		 (unsigned)(rdev->mc.gtt_size >> 20),
    217  1.1  riastrad 		 (unsigned long long)table_addr);
    218  1.1  riastrad 	rdev->gart.ready = true;
    219  1.1  riastrad 	return 0;
    220  1.1  riastrad }
    221  1.1  riastrad 
    222  1.1  riastrad void rv370_pcie_gart_disable(struct radeon_device *rdev)
    223  1.1  riastrad {
    224  1.1  riastrad 	u32 tmp;
    225  1.1  riastrad 
    226  1.1  riastrad 	WREG32_PCIE(RADEON_PCIE_TX_GART_START_LO, 0);
    227  1.1  riastrad 	WREG32_PCIE(RADEON_PCIE_TX_GART_END_LO, 0);
    228  1.1  riastrad 	WREG32_PCIE(RADEON_PCIE_TX_GART_START_HI, 0);
    229  1.1  riastrad 	WREG32_PCIE(RADEON_PCIE_TX_GART_END_HI, 0);
    230  1.1  riastrad 	tmp = RREG32_PCIE(RADEON_PCIE_TX_GART_CNTL);
    231  1.1  riastrad 	tmp |= RADEON_PCIE_TX_GART_UNMAPPED_ACCESS_DISCARD;
    232  1.1  riastrad 	WREG32_PCIE(RADEON_PCIE_TX_GART_CNTL, tmp & ~RADEON_PCIE_TX_GART_EN);
    233  1.1  riastrad 	radeon_gart_table_vram_unpin(rdev);
    234  1.1  riastrad }
    235  1.1  riastrad 
    236  1.1  riastrad void rv370_pcie_gart_fini(struct radeon_device *rdev)
    237  1.1  riastrad {
    238  1.1  riastrad 	radeon_gart_fini(rdev);
    239  1.1  riastrad 	rv370_pcie_gart_disable(rdev);
    240  1.1  riastrad 	radeon_gart_table_vram_free(rdev);
    241  1.1  riastrad }
    242  1.1  riastrad 
    243  1.1  riastrad void r300_fence_ring_emit(struct radeon_device *rdev,
    244  1.1  riastrad 			  struct radeon_fence *fence)
    245  1.1  riastrad {
    246  1.1  riastrad 	struct radeon_ring *ring = &rdev->ring[fence->ring];
    247  1.1  riastrad 
    248  1.1  riastrad 	/* Who ever call radeon_fence_emit should call ring_lock and ask
    249  1.1  riastrad 	 * for enough space (today caller are ib schedule and buffer move) */
    250  1.1  riastrad 	/* Write SC register so SC & US assert idle */
    251  1.1  riastrad 	radeon_ring_write(ring, PACKET0(R300_RE_SCISSORS_TL, 0));
    252  1.1  riastrad 	radeon_ring_write(ring, 0);
    253  1.1  riastrad 	radeon_ring_write(ring, PACKET0(R300_RE_SCISSORS_BR, 0));
    254  1.1  riastrad 	radeon_ring_write(ring, 0);
    255  1.1  riastrad 	/* Flush 3D cache */
    256  1.1  riastrad 	radeon_ring_write(ring, PACKET0(R300_RB3D_DSTCACHE_CTLSTAT, 0));
    257  1.1  riastrad 	radeon_ring_write(ring, R300_RB3D_DC_FLUSH);
    258  1.1  riastrad 	radeon_ring_write(ring, PACKET0(R300_RB3D_ZCACHE_CTLSTAT, 0));
    259  1.1  riastrad 	radeon_ring_write(ring, R300_ZC_FLUSH);
    260  1.1  riastrad 	/* Wait until IDLE & CLEAN */
    261  1.1  riastrad 	radeon_ring_write(ring, PACKET0(RADEON_WAIT_UNTIL, 0));
    262  1.1  riastrad 	radeon_ring_write(ring, (RADEON_WAIT_3D_IDLECLEAN |
    263  1.1  riastrad 				 RADEON_WAIT_2D_IDLECLEAN |
    264  1.1  riastrad 				 RADEON_WAIT_DMA_GUI_IDLE));
    265  1.1  riastrad 	radeon_ring_write(ring, PACKET0(RADEON_HOST_PATH_CNTL, 0));
    266  1.1  riastrad 	radeon_ring_write(ring, rdev->config.r300.hdp_cntl |
    267  1.1  riastrad 				RADEON_HDP_READ_BUFFER_INVALIDATE);
    268  1.1  riastrad 	radeon_ring_write(ring, PACKET0(RADEON_HOST_PATH_CNTL, 0));
    269  1.1  riastrad 	radeon_ring_write(ring, rdev->config.r300.hdp_cntl);
    270  1.1  riastrad 	/* Emit fence sequence & fire IRQ */
    271  1.1  riastrad 	radeon_ring_write(ring, PACKET0(rdev->fence_drv[fence->ring].scratch_reg, 0));
    272  1.1  riastrad 	radeon_ring_write(ring, fence->seq);
    273  1.1  riastrad 	radeon_ring_write(ring, PACKET0(RADEON_GEN_INT_STATUS, 0));
    274  1.1  riastrad 	radeon_ring_write(ring, RADEON_SW_INT_FIRE);
    275  1.1  riastrad }
    276  1.1  riastrad 
    277  1.1  riastrad void r300_ring_start(struct radeon_device *rdev, struct radeon_ring *ring)
    278  1.1  riastrad {
    279  1.1  riastrad 	unsigned gb_tile_config;
    280  1.1  riastrad 	int r;
    281  1.1  riastrad 
    282  1.1  riastrad 	/* Sub pixel 1/12 so we can have 4K rendering according to doc */
    283  1.1  riastrad 	gb_tile_config = (R300_ENABLE_TILING | R300_TILE_SIZE_16);
    284  1.1  riastrad 	switch(rdev->num_gb_pipes) {
    285  1.1  riastrad 	case 2:
    286  1.1  riastrad 		gb_tile_config |= R300_PIPE_COUNT_R300;
    287  1.1  riastrad 		break;
    288  1.1  riastrad 	case 3:
    289  1.1  riastrad 		gb_tile_config |= R300_PIPE_COUNT_R420_3P;
    290  1.1  riastrad 		break;
    291  1.1  riastrad 	case 4:
    292  1.1  riastrad 		gb_tile_config |= R300_PIPE_COUNT_R420;
    293  1.1  riastrad 		break;
    294  1.1  riastrad 	case 1:
    295  1.1  riastrad 	default:
    296  1.1  riastrad 		gb_tile_config |= R300_PIPE_COUNT_RV350;
    297  1.1  riastrad 		break;
    298  1.1  riastrad 	}
    299  1.1  riastrad 
    300  1.1  riastrad 	r = radeon_ring_lock(rdev, ring, 64);
    301  1.1  riastrad 	if (r) {
    302  1.1  riastrad 		return;
    303  1.1  riastrad 	}
    304  1.1  riastrad 	radeon_ring_write(ring, PACKET0(RADEON_ISYNC_CNTL, 0));
    305  1.1  riastrad 	radeon_ring_write(ring,
    306  1.1  riastrad 			  RADEON_ISYNC_ANY2D_IDLE3D |
    307  1.1  riastrad 			  RADEON_ISYNC_ANY3D_IDLE2D |
    308  1.1  riastrad 			  RADEON_ISYNC_WAIT_IDLEGUI |
    309  1.1  riastrad 			  RADEON_ISYNC_CPSCRATCH_IDLEGUI);
    310  1.1  riastrad 	radeon_ring_write(ring, PACKET0(R300_GB_TILE_CONFIG, 0));
    311  1.1  riastrad 	radeon_ring_write(ring, gb_tile_config);
    312  1.1  riastrad 	radeon_ring_write(ring, PACKET0(RADEON_WAIT_UNTIL, 0));
    313  1.1  riastrad 	radeon_ring_write(ring,
    314  1.1  riastrad 			  RADEON_WAIT_2D_IDLECLEAN |
    315  1.1  riastrad 			  RADEON_WAIT_3D_IDLECLEAN);
    316  1.1  riastrad 	radeon_ring_write(ring, PACKET0(R300_DST_PIPE_CONFIG, 0));
    317  1.1  riastrad 	radeon_ring_write(ring, R300_PIPE_AUTO_CONFIG);
    318  1.1  riastrad 	radeon_ring_write(ring, PACKET0(R300_GB_SELECT, 0));
    319  1.1  riastrad 	radeon_ring_write(ring, 0);
    320  1.1  riastrad 	radeon_ring_write(ring, PACKET0(R300_GB_ENABLE, 0));
    321  1.1  riastrad 	radeon_ring_write(ring, 0);
    322  1.1  riastrad 	radeon_ring_write(ring, PACKET0(R300_RB3D_DSTCACHE_CTLSTAT, 0));
    323  1.1  riastrad 	radeon_ring_write(ring, R300_RB3D_DC_FLUSH | R300_RB3D_DC_FREE);
    324  1.1  riastrad 	radeon_ring_write(ring, PACKET0(R300_RB3D_ZCACHE_CTLSTAT, 0));
    325  1.1  riastrad 	radeon_ring_write(ring, R300_ZC_FLUSH | R300_ZC_FREE);
    326  1.1  riastrad 	radeon_ring_write(ring, PACKET0(RADEON_WAIT_UNTIL, 0));
    327  1.1  riastrad 	radeon_ring_write(ring,
    328  1.1  riastrad 			  RADEON_WAIT_2D_IDLECLEAN |
    329  1.1  riastrad 			  RADEON_WAIT_3D_IDLECLEAN);
    330  1.1  riastrad 	radeon_ring_write(ring, PACKET0(R300_GB_AA_CONFIG, 0));
    331  1.1  riastrad 	radeon_ring_write(ring, 0);
    332  1.1  riastrad 	radeon_ring_write(ring, PACKET0(R300_RB3D_DSTCACHE_CTLSTAT, 0));
    333  1.1  riastrad 	radeon_ring_write(ring, R300_RB3D_DC_FLUSH | R300_RB3D_DC_FREE);
    334  1.1  riastrad 	radeon_ring_write(ring, PACKET0(R300_RB3D_ZCACHE_CTLSTAT, 0));
    335  1.1  riastrad 	radeon_ring_write(ring, R300_ZC_FLUSH | R300_ZC_FREE);
    336  1.1  riastrad 	radeon_ring_write(ring, PACKET0(R300_GB_MSPOS0, 0));
    337  1.1  riastrad 	radeon_ring_write(ring,
    338  1.1  riastrad 			  ((6 << R300_MS_X0_SHIFT) |
    339  1.1  riastrad 			   (6 << R300_MS_Y0_SHIFT) |
    340  1.1  riastrad 			   (6 << R300_MS_X1_SHIFT) |
    341  1.1  riastrad 			   (6 << R300_MS_Y1_SHIFT) |
    342  1.1  riastrad 			   (6 << R300_MS_X2_SHIFT) |
    343  1.1  riastrad 			   (6 << R300_MS_Y2_SHIFT) |
    344  1.1  riastrad 			   (6 << R300_MSBD0_Y_SHIFT) |
    345  1.1  riastrad 			   (6 << R300_MSBD0_X_SHIFT)));
    346  1.1  riastrad 	radeon_ring_write(ring, PACKET0(R300_GB_MSPOS1, 0));
    347  1.1  riastrad 	radeon_ring_write(ring,
    348  1.1  riastrad 			  ((6 << R300_MS_X3_SHIFT) |
    349  1.1  riastrad 			   (6 << R300_MS_Y3_SHIFT) |
    350  1.1  riastrad 			   (6 << R300_MS_X4_SHIFT) |
    351  1.1  riastrad 			   (6 << R300_MS_Y4_SHIFT) |
    352  1.1  riastrad 			   (6 << R300_MS_X5_SHIFT) |
    353  1.1  riastrad 			   (6 << R300_MS_Y5_SHIFT) |
    354  1.1  riastrad 			   (6 << R300_MSBD1_SHIFT)));
    355  1.1  riastrad 	radeon_ring_write(ring, PACKET0(R300_GA_ENHANCE, 0));
    356  1.1  riastrad 	radeon_ring_write(ring, R300_GA_DEADLOCK_CNTL | R300_GA_FASTSYNC_CNTL);
    357  1.1  riastrad 	radeon_ring_write(ring, PACKET0(R300_GA_POLY_MODE, 0));
    358  1.1  riastrad 	radeon_ring_write(ring,
    359  1.1  riastrad 			  R300_FRONT_PTYPE_TRIANGE | R300_BACK_PTYPE_TRIANGE);
    360  1.1  riastrad 	radeon_ring_write(ring, PACKET0(R300_GA_ROUND_MODE, 0));
    361  1.1  riastrad 	radeon_ring_write(ring,
    362  1.1  riastrad 			  R300_GEOMETRY_ROUND_NEAREST |
    363  1.1  riastrad 			  R300_COLOR_ROUND_NEAREST);
    364  1.1  riastrad 	radeon_ring_unlock_commit(rdev, ring, false);
    365  1.1  riastrad }
    366  1.1  riastrad 
    367  1.1  riastrad static void r300_errata(struct radeon_device *rdev)
    368  1.1  riastrad {
    369  1.1  riastrad 	rdev->pll_errata = 0;
    370  1.1  riastrad 
    371  1.1  riastrad 	if (rdev->family == CHIP_R300 &&
    372  1.1  riastrad 	    (RREG32(RADEON_CONFIG_CNTL) & RADEON_CFG_ATI_REV_ID_MASK) == RADEON_CFG_ATI_REV_A11) {
    373  1.1  riastrad 		rdev->pll_errata |= CHIP_ERRATA_R300_CG;
    374  1.1  riastrad 	}
    375  1.1  riastrad }
    376  1.1  riastrad 
    377  1.1  riastrad int r300_mc_wait_for_idle(struct radeon_device *rdev)
    378  1.1  riastrad {
    379  1.1  riastrad 	unsigned i;
    380  1.1  riastrad 	uint32_t tmp;
    381  1.1  riastrad 
    382  1.1  riastrad 	for (i = 0; i < rdev->usec_timeout; i++) {
    383  1.1  riastrad 		/* read MC_STATUS */
    384  1.1  riastrad 		tmp = RREG32(RADEON_MC_STATUS);
    385  1.1  riastrad 		if (tmp & R300_MC_IDLE) {
    386  1.1  riastrad 			return 0;
    387  1.1  riastrad 		}
    388  1.3  riastrad 		udelay(1);
    389  1.1  riastrad 	}
    390  1.1  riastrad 	return -1;
    391  1.1  riastrad }
    392  1.1  riastrad 
    393  1.1  riastrad static void r300_gpu_init(struct radeon_device *rdev)
    394  1.1  riastrad {
    395  1.1  riastrad 	uint32_t gb_tile_config, tmp;
    396  1.1  riastrad 
    397  1.1  riastrad 	if ((rdev->family == CHIP_R300 && rdev->pdev->device != 0x4144) ||
    398  1.1  riastrad 	    (rdev->family == CHIP_R350 && rdev->pdev->device != 0x4148)) {
    399  1.1  riastrad 		/* r300,r350 */
    400  1.1  riastrad 		rdev->num_gb_pipes = 2;
    401  1.1  riastrad 	} else {
    402  1.1  riastrad 		/* rv350,rv370,rv380,r300 AD, r350 AH */
    403  1.1  riastrad 		rdev->num_gb_pipes = 1;
    404  1.1  riastrad 	}
    405  1.1  riastrad 	rdev->num_z_pipes = 1;
    406  1.1  riastrad 	gb_tile_config = (R300_ENABLE_TILING | R300_TILE_SIZE_16);
    407  1.1  riastrad 	switch (rdev->num_gb_pipes) {
    408  1.1  riastrad 	case 2:
    409  1.1  riastrad 		gb_tile_config |= R300_PIPE_COUNT_R300;
    410  1.1  riastrad 		break;
    411  1.1  riastrad 	case 3:
    412  1.1  riastrad 		gb_tile_config |= R300_PIPE_COUNT_R420_3P;
    413  1.1  riastrad 		break;
    414  1.1  riastrad 	case 4:
    415  1.1  riastrad 		gb_tile_config |= R300_PIPE_COUNT_R420;
    416  1.1  riastrad 		break;
    417  1.1  riastrad 	default:
    418  1.1  riastrad 	case 1:
    419  1.1  riastrad 		gb_tile_config |= R300_PIPE_COUNT_RV350;
    420  1.1  riastrad 		break;
    421  1.1  riastrad 	}
    422  1.1  riastrad 	WREG32(R300_GB_TILE_CONFIG, gb_tile_config);
    423  1.1  riastrad 
    424  1.1  riastrad 	if (r100_gui_wait_for_idle(rdev)) {
    425  1.3  riastrad 		pr_warn("Failed to wait GUI idle while programming pipes. Bad things might happen.\n");
    426  1.1  riastrad 	}
    427  1.1  riastrad 
    428  1.1  riastrad 	tmp = RREG32(R300_DST_PIPE_CONFIG);
    429  1.1  riastrad 	WREG32(R300_DST_PIPE_CONFIG, tmp | R300_PIPE_AUTO_CONFIG);
    430  1.1  riastrad 
    431  1.1  riastrad 	WREG32(R300_RB2D_DSTCACHE_MODE,
    432  1.1  riastrad 	       R300_DC_AUTOFLUSH_ENABLE |
    433  1.1  riastrad 	       R300_DC_DC_DISABLE_IGNORE_PE);
    434  1.1  riastrad 
    435  1.1  riastrad 	if (r100_gui_wait_for_idle(rdev)) {
    436  1.3  riastrad 		pr_warn("Failed to wait GUI idle while programming pipes. Bad things might happen.\n");
    437  1.1  riastrad 	}
    438  1.1  riastrad 	if (r300_mc_wait_for_idle(rdev)) {
    439  1.3  riastrad 		pr_warn("Failed to wait MC idle while programming pipes. Bad things might happen.\n");
    440  1.1  riastrad 	}
    441  1.3  riastrad 	DRM_INFO("radeon: %d quad pipes, %d Z pipes initialized\n",
    442  1.1  riastrad 		 rdev->num_gb_pipes, rdev->num_z_pipes);
    443  1.1  riastrad }
    444  1.1  riastrad 
    445  1.3  riastrad int r300_asic_reset(struct radeon_device *rdev, bool hard)
    446  1.1  riastrad {
    447  1.1  riastrad 	struct r100_mc_save save;
    448  1.1  riastrad 	u32 status, tmp;
    449  1.1  riastrad 	int ret = 0;
    450  1.1  riastrad 
    451  1.1  riastrad 	status = RREG32(R_000E40_RBBM_STATUS);
    452  1.1  riastrad 	if (!G_000E40_GUI_ACTIVE(status)) {
    453  1.1  riastrad 		return 0;
    454  1.1  riastrad 	}
    455  1.1  riastrad 	r100_mc_stop(rdev, &save);
    456  1.1  riastrad 	status = RREG32(R_000E40_RBBM_STATUS);
    457  1.1  riastrad 	dev_info(rdev->dev, "(%s:%d) RBBM_STATUS=0x%08X\n", __func__, __LINE__, status);
    458  1.1  riastrad 	/* stop CP */
    459  1.1  riastrad 	WREG32(RADEON_CP_CSQ_CNTL, 0);
    460  1.1  riastrad 	tmp = RREG32(RADEON_CP_RB_CNTL);
    461  1.1  riastrad 	WREG32(RADEON_CP_RB_CNTL, tmp | RADEON_RB_RPTR_WR_ENA);
    462  1.1  riastrad 	WREG32(RADEON_CP_RB_RPTR_WR, 0);
    463  1.1  riastrad 	WREG32(RADEON_CP_RB_WPTR, 0);
    464  1.1  riastrad 	WREG32(RADEON_CP_RB_CNTL, tmp);
    465  1.1  riastrad 	/* save PCI state */
    466  1.1  riastrad 	pci_save_state(rdev->pdev);
    467  1.1  riastrad 	/* disable bus mastering */
    468  1.1  riastrad 	r100_bm_disable(rdev);
    469  1.1  riastrad 	WREG32(R_0000F0_RBBM_SOFT_RESET, S_0000F0_SOFT_RESET_VAP(1) |
    470  1.1  riastrad 					S_0000F0_SOFT_RESET_GA(1));
    471  1.1  riastrad 	RREG32(R_0000F0_RBBM_SOFT_RESET);
    472  1.1  riastrad 	mdelay(500);
    473  1.1  riastrad 	WREG32(R_0000F0_RBBM_SOFT_RESET, 0);
    474  1.1  riastrad 	mdelay(1);
    475  1.1  riastrad 	status = RREG32(R_000E40_RBBM_STATUS);
    476  1.1  riastrad 	dev_info(rdev->dev, "(%s:%d) RBBM_STATUS=0x%08X\n", __func__, __LINE__, status);
    477  1.1  riastrad 	/* resetting the CP seems to be problematic sometimes it end up
    478  1.1  riastrad 	 * hard locking the computer, but it's necessary for successful
    479  1.1  riastrad 	 * reset more test & playing is needed on R3XX/R4XX to find a
    480  1.1  riastrad 	 * reliable (if any solution)
    481  1.1  riastrad 	 */
    482  1.1  riastrad 	WREG32(R_0000F0_RBBM_SOFT_RESET, S_0000F0_SOFT_RESET_CP(1));
    483  1.1  riastrad 	RREG32(R_0000F0_RBBM_SOFT_RESET);
    484  1.1  riastrad 	mdelay(500);
    485  1.1  riastrad 	WREG32(R_0000F0_RBBM_SOFT_RESET, 0);
    486  1.1  riastrad 	mdelay(1);
    487  1.1  riastrad 	status = RREG32(R_000E40_RBBM_STATUS);
    488  1.1  riastrad 	dev_info(rdev->dev, "(%s:%d) RBBM_STATUS=0x%08X\n", __func__, __LINE__, status);
    489  1.1  riastrad 	/* restore PCI & busmastering */
    490  1.1  riastrad 	pci_restore_state(rdev->pdev);
    491  1.1  riastrad 	r100_enable_bm(rdev);
    492  1.1  riastrad 	/* Check if GPU is idle */
    493  1.1  riastrad 	if (G_000E40_GA_BUSY(status) || G_000E40_VAP_BUSY(status)) {
    494  1.1  riastrad 		dev_err(rdev->dev, "failed to reset GPU\n");
    495  1.1  riastrad 		ret = -1;
    496  1.1  riastrad 	} else
    497  1.1  riastrad 		dev_info(rdev->dev, "GPU reset succeed\n");
    498  1.1  riastrad 	r100_mc_resume(rdev, &save);
    499  1.1  riastrad 	return ret;
    500  1.1  riastrad }
    501  1.1  riastrad 
    502  1.1  riastrad /*
    503  1.1  riastrad  * r300,r350,rv350,rv380 VRAM info
    504  1.1  riastrad  */
    505  1.1  riastrad void r300_mc_init(struct radeon_device *rdev)
    506  1.1  riastrad {
    507  1.1  riastrad 	u64 base;
    508  1.1  riastrad 	u32 tmp;
    509  1.1  riastrad 
    510  1.1  riastrad 	/* DDR for all card after R300 & IGP */
    511  1.1  riastrad 	rdev->mc.vram_is_ddr = true;
    512  1.1  riastrad 	tmp = RREG32(RADEON_MEM_CNTL);
    513  1.1  riastrad 	tmp &= R300_MEM_NUM_CHANNELS_MASK;
    514  1.1  riastrad 	switch (tmp) {
    515  1.1  riastrad 	case 0: rdev->mc.vram_width = 64; break;
    516  1.1  riastrad 	case 1: rdev->mc.vram_width = 128; break;
    517  1.1  riastrad 	case 2: rdev->mc.vram_width = 256; break;
    518  1.1  riastrad 	default:  rdev->mc.vram_width = 128; break;
    519  1.1  riastrad 	}
    520  1.1  riastrad 	r100_vram_init_sizes(rdev);
    521  1.1  riastrad 	base = rdev->mc.aper_base;
    522  1.1  riastrad 	if (rdev->flags & RADEON_IS_IGP)
    523  1.1  riastrad 		base = (RREG32(RADEON_NB_TOM) & 0xffff) << 16;
    524  1.1  riastrad 	radeon_vram_location(rdev, &rdev->mc, base);
    525  1.1  riastrad 	rdev->mc.gtt_base_align = 0;
    526  1.1  riastrad 	if (!(rdev->flags & RADEON_IS_AGP))
    527  1.1  riastrad 		radeon_gtt_location(rdev, &rdev->mc);
    528  1.1  riastrad 	radeon_update_bandwidth_info(rdev);
    529  1.1  riastrad }
    530  1.1  riastrad 
    531  1.1  riastrad void rv370_set_pcie_lanes(struct radeon_device *rdev, int lanes)
    532  1.1  riastrad {
    533  1.1  riastrad 	uint32_t link_width_cntl, mask;
    534  1.1  riastrad 
    535  1.1  riastrad 	if (rdev->flags & RADEON_IS_IGP)
    536  1.1  riastrad 		return;
    537  1.1  riastrad 
    538  1.1  riastrad 	if (!(rdev->flags & RADEON_IS_PCIE))
    539  1.1  riastrad 		return;
    540  1.1  riastrad 
    541  1.1  riastrad 	/* FIXME wait for idle */
    542  1.1  riastrad 
    543  1.1  riastrad 	switch (lanes) {
    544  1.1  riastrad 	case 0:
    545  1.1  riastrad 		mask = RADEON_PCIE_LC_LINK_WIDTH_X0;
    546  1.1  riastrad 		break;
    547  1.1  riastrad 	case 1:
    548  1.1  riastrad 		mask = RADEON_PCIE_LC_LINK_WIDTH_X1;
    549  1.1  riastrad 		break;
    550  1.1  riastrad 	case 2:
    551  1.1  riastrad 		mask = RADEON_PCIE_LC_LINK_WIDTH_X2;
    552  1.1  riastrad 		break;
    553  1.1  riastrad 	case 4:
    554  1.1  riastrad 		mask = RADEON_PCIE_LC_LINK_WIDTH_X4;
    555  1.1  riastrad 		break;
    556  1.1  riastrad 	case 8:
    557  1.1  riastrad 		mask = RADEON_PCIE_LC_LINK_WIDTH_X8;
    558  1.1  riastrad 		break;
    559  1.1  riastrad 	case 12:
    560  1.1  riastrad 		mask = RADEON_PCIE_LC_LINK_WIDTH_X12;
    561  1.1  riastrad 		break;
    562  1.1  riastrad 	case 16:
    563  1.1  riastrad 	default:
    564  1.1  riastrad 		mask = RADEON_PCIE_LC_LINK_WIDTH_X16;
    565  1.1  riastrad 		break;
    566  1.1  riastrad 	}
    567  1.1  riastrad 
    568  1.1  riastrad 	link_width_cntl = RREG32_PCIE(RADEON_PCIE_LC_LINK_WIDTH_CNTL);
    569  1.1  riastrad 
    570  1.1  riastrad 	if ((link_width_cntl & RADEON_PCIE_LC_LINK_WIDTH_RD_MASK) ==
    571  1.1  riastrad 	    (mask << RADEON_PCIE_LC_LINK_WIDTH_RD_SHIFT))
    572  1.1  riastrad 		return;
    573  1.1  riastrad 
    574  1.1  riastrad 	link_width_cntl &= ~(RADEON_PCIE_LC_LINK_WIDTH_MASK |
    575  1.1  riastrad 			     RADEON_PCIE_LC_RECONFIG_NOW |
    576  1.1  riastrad 			     RADEON_PCIE_LC_RECONFIG_LATER |
    577  1.1  riastrad 			     RADEON_PCIE_LC_SHORT_RECONFIG_EN);
    578  1.1  riastrad 	link_width_cntl |= mask;
    579  1.1  riastrad 	WREG32_PCIE(RADEON_PCIE_LC_LINK_WIDTH_CNTL, link_width_cntl);
    580  1.1  riastrad 	WREG32_PCIE(RADEON_PCIE_LC_LINK_WIDTH_CNTL, (link_width_cntl |
    581  1.1  riastrad 						     RADEON_PCIE_LC_RECONFIG_NOW));
    582  1.1  riastrad 
    583  1.1  riastrad 	/* wait for lane set to complete */
    584  1.1  riastrad 	link_width_cntl = RREG32_PCIE(RADEON_PCIE_LC_LINK_WIDTH_CNTL);
    585  1.1  riastrad 	while (link_width_cntl == 0xffffffff)
    586  1.1  riastrad 		link_width_cntl = RREG32_PCIE(RADEON_PCIE_LC_LINK_WIDTH_CNTL);
    587  1.1  riastrad 
    588  1.1  riastrad }
    589  1.1  riastrad 
    590  1.1  riastrad int rv370_get_pcie_lanes(struct radeon_device *rdev)
    591  1.1  riastrad {
    592  1.1  riastrad 	u32 link_width_cntl;
    593  1.1  riastrad 
    594  1.1  riastrad 	if (rdev->flags & RADEON_IS_IGP)
    595  1.1  riastrad 		return 0;
    596  1.1  riastrad 
    597  1.1  riastrad 	if (!(rdev->flags & RADEON_IS_PCIE))
    598  1.1  riastrad 		return 0;
    599  1.1  riastrad 
    600  1.1  riastrad 	/* FIXME wait for idle */
    601  1.1  riastrad 
    602  1.1  riastrad 	link_width_cntl = RREG32_PCIE(RADEON_PCIE_LC_LINK_WIDTH_CNTL);
    603  1.1  riastrad 
    604  1.1  riastrad 	switch ((link_width_cntl & RADEON_PCIE_LC_LINK_WIDTH_RD_MASK) >> RADEON_PCIE_LC_LINK_WIDTH_RD_SHIFT) {
    605  1.1  riastrad 	case RADEON_PCIE_LC_LINK_WIDTH_X0:
    606  1.1  riastrad 		return 0;
    607  1.1  riastrad 	case RADEON_PCIE_LC_LINK_WIDTH_X1:
    608  1.1  riastrad 		return 1;
    609  1.1  riastrad 	case RADEON_PCIE_LC_LINK_WIDTH_X2:
    610  1.1  riastrad 		return 2;
    611  1.1  riastrad 	case RADEON_PCIE_LC_LINK_WIDTH_X4:
    612  1.1  riastrad 		return 4;
    613  1.1  riastrad 	case RADEON_PCIE_LC_LINK_WIDTH_X8:
    614  1.1  riastrad 		return 8;
    615  1.1  riastrad 	case RADEON_PCIE_LC_LINK_WIDTH_X16:
    616  1.1  riastrad 	default:
    617  1.1  riastrad 		return 16;
    618  1.1  riastrad 	}
    619  1.1  riastrad }
    620  1.1  riastrad 
    621  1.1  riastrad #if defined(CONFIG_DEBUG_FS)
    622  1.1  riastrad static int rv370_debugfs_pcie_gart_info(struct seq_file *m, void *data)
    623  1.1  riastrad {
    624  1.1  riastrad 	struct drm_info_node *node = (struct drm_info_node *) m->private;
    625  1.1  riastrad 	struct drm_device *dev = node->minor->dev;
    626  1.1  riastrad 	struct radeon_device *rdev = dev->dev_private;
    627  1.1  riastrad 	uint32_t tmp;
    628  1.1  riastrad 
    629  1.1  riastrad 	tmp = RREG32_PCIE(RADEON_PCIE_TX_GART_CNTL);
    630  1.1  riastrad 	seq_printf(m, "PCIE_TX_GART_CNTL 0x%08x\n", tmp);
    631  1.1  riastrad 	tmp = RREG32_PCIE(RADEON_PCIE_TX_GART_BASE);
    632  1.1  riastrad 	seq_printf(m, "PCIE_TX_GART_BASE 0x%08x\n", tmp);
    633  1.1  riastrad 	tmp = RREG32_PCIE(RADEON_PCIE_TX_GART_START_LO);
    634  1.1  riastrad 	seq_printf(m, "PCIE_TX_GART_START_LO 0x%08x\n", tmp);
    635  1.1  riastrad 	tmp = RREG32_PCIE(RADEON_PCIE_TX_GART_START_HI);
    636  1.1  riastrad 	seq_printf(m, "PCIE_TX_GART_START_HI 0x%08x\n", tmp);
    637  1.1  riastrad 	tmp = RREG32_PCIE(RADEON_PCIE_TX_GART_END_LO);
    638  1.1  riastrad 	seq_printf(m, "PCIE_TX_GART_END_LO 0x%08x\n", tmp);
    639  1.1  riastrad 	tmp = RREG32_PCIE(RADEON_PCIE_TX_GART_END_HI);
    640  1.1  riastrad 	seq_printf(m, "PCIE_TX_GART_END_HI 0x%08x\n", tmp);
    641  1.1  riastrad 	tmp = RREG32_PCIE(RADEON_PCIE_TX_GART_ERROR);
    642  1.1  riastrad 	seq_printf(m, "PCIE_TX_GART_ERROR 0x%08x\n", tmp);
    643  1.1  riastrad 	return 0;
    644  1.1  riastrad }
    645  1.1  riastrad 
    646  1.1  riastrad static struct drm_info_list rv370_pcie_gart_info_list[] = {
    647  1.1  riastrad 	{"rv370_pcie_gart_info", rv370_debugfs_pcie_gart_info, 0, NULL},
    648  1.1  riastrad };
    649  1.1  riastrad #endif
    650  1.1  riastrad 
    651  1.1  riastrad static int rv370_debugfs_pcie_gart_info_init(struct radeon_device *rdev)
    652  1.1  riastrad {
    653  1.1  riastrad #if defined(CONFIG_DEBUG_FS)
    654  1.1  riastrad 	return radeon_debugfs_add_files(rdev, rv370_pcie_gart_info_list, 1);
    655  1.1  riastrad #else
    656  1.1  riastrad 	return 0;
    657  1.1  riastrad #endif
    658  1.1  riastrad }
    659  1.1  riastrad 
    660  1.1  riastrad static int r300_packet0_check(struct radeon_cs_parser *p,
    661  1.1  riastrad 		struct radeon_cs_packet *pkt,
    662  1.1  riastrad 		unsigned idx, unsigned reg)
    663  1.1  riastrad {
    664  1.1  riastrad 	struct radeon_bo_list *reloc;
    665  1.1  riastrad 	struct r100_cs_track *track;
    666  1.1  riastrad 	volatile uint32_t *ib;
    667  1.1  riastrad 	uint32_t tmp, tile_flags = 0;
    668  1.1  riastrad 	unsigned i;
    669  1.1  riastrad 	int r;
    670  1.1  riastrad 	u32 idx_value;
    671  1.1  riastrad 
    672  1.1  riastrad 	ib = p->ib.ptr;
    673  1.1  riastrad 	track = (struct r100_cs_track *)p->track;
    674  1.1  riastrad 	idx_value = radeon_get_ib_value(p, idx);
    675  1.1  riastrad 
    676  1.1  riastrad 	switch(reg) {
    677  1.1  riastrad 	case AVIVO_D1MODE_VLINE_START_END:
    678  1.1  riastrad 	case RADEON_CRTC_GUI_TRIG_VLINE:
    679  1.1  riastrad 		r = r100_cs_packet_parse_vline(p);
    680  1.1  riastrad 		if (r) {
    681  1.1  riastrad 			DRM_ERROR("No reloc for ib[%d]=0x%04X\n",
    682  1.1  riastrad 					idx, reg);
    683  1.1  riastrad 			radeon_cs_dump_packet(p, pkt);
    684  1.1  riastrad 			return r;
    685  1.1  riastrad 		}
    686  1.1  riastrad 		break;
    687  1.1  riastrad 	case RADEON_DST_PITCH_OFFSET:
    688  1.1  riastrad 	case RADEON_SRC_PITCH_OFFSET:
    689  1.1  riastrad 		r = r100_reloc_pitch_offset(p, pkt, idx, reg);
    690  1.1  riastrad 		if (r)
    691  1.1  riastrad 			return r;
    692  1.1  riastrad 		break;
    693  1.1  riastrad 	case R300_RB3D_COLOROFFSET0:
    694  1.1  riastrad 	case R300_RB3D_COLOROFFSET1:
    695  1.1  riastrad 	case R300_RB3D_COLOROFFSET2:
    696  1.1  riastrad 	case R300_RB3D_COLOROFFSET3:
    697  1.1  riastrad 		i = (reg - R300_RB3D_COLOROFFSET0) >> 2;
    698  1.1  riastrad 		r = radeon_cs_packet_next_reloc(p, &reloc, 0);
    699  1.1  riastrad 		if (r) {
    700  1.1  riastrad 			DRM_ERROR("No reloc for ib[%d]=0x%04X\n",
    701  1.1  riastrad 					idx, reg);
    702  1.1  riastrad 			radeon_cs_dump_packet(p, pkt);
    703  1.1  riastrad 			return r;
    704  1.1  riastrad 		}
    705  1.1  riastrad 		track->cb[i].robj = reloc->robj;
    706  1.1  riastrad 		track->cb[i].offset = idx_value;
    707  1.1  riastrad 		track->cb_dirty = true;
    708  1.1  riastrad 		ib[idx] = idx_value + ((u32)reloc->gpu_offset);
    709  1.1  riastrad 		break;
    710  1.1  riastrad 	case R300_ZB_DEPTHOFFSET:
    711  1.1  riastrad 		r = radeon_cs_packet_next_reloc(p, &reloc, 0);
    712  1.1  riastrad 		if (r) {
    713  1.1  riastrad 			DRM_ERROR("No reloc for ib[%d]=0x%04X\n",
    714  1.1  riastrad 					idx, reg);
    715  1.1  riastrad 			radeon_cs_dump_packet(p, pkt);
    716  1.1  riastrad 			return r;
    717  1.1  riastrad 		}
    718  1.1  riastrad 		track->zb.robj = reloc->robj;
    719  1.1  riastrad 		track->zb.offset = idx_value;
    720  1.1  riastrad 		track->zb_dirty = true;
    721  1.1  riastrad 		ib[idx] = idx_value + ((u32)reloc->gpu_offset);
    722  1.1  riastrad 		break;
    723  1.1  riastrad 	case R300_TX_OFFSET_0:
    724  1.1  riastrad 	case R300_TX_OFFSET_0+4:
    725  1.1  riastrad 	case R300_TX_OFFSET_0+8:
    726  1.1  riastrad 	case R300_TX_OFFSET_0+12:
    727  1.1  riastrad 	case R300_TX_OFFSET_0+16:
    728  1.1  riastrad 	case R300_TX_OFFSET_0+20:
    729  1.1  riastrad 	case R300_TX_OFFSET_0+24:
    730  1.1  riastrad 	case R300_TX_OFFSET_0+28:
    731  1.1  riastrad 	case R300_TX_OFFSET_0+32:
    732  1.1  riastrad 	case R300_TX_OFFSET_0+36:
    733  1.1  riastrad 	case R300_TX_OFFSET_0+40:
    734  1.1  riastrad 	case R300_TX_OFFSET_0+44:
    735  1.1  riastrad 	case R300_TX_OFFSET_0+48:
    736  1.1  riastrad 	case R300_TX_OFFSET_0+52:
    737  1.1  riastrad 	case R300_TX_OFFSET_0+56:
    738  1.1  riastrad 	case R300_TX_OFFSET_0+60:
    739  1.1  riastrad 		i = (reg - R300_TX_OFFSET_0) >> 2;
    740  1.1  riastrad 		r = radeon_cs_packet_next_reloc(p, &reloc, 0);
    741  1.1  riastrad 		if (r) {
    742  1.1  riastrad 			DRM_ERROR("No reloc for ib[%d]=0x%04X\n",
    743  1.1  riastrad 					idx, reg);
    744  1.1  riastrad 			radeon_cs_dump_packet(p, pkt);
    745  1.1  riastrad 			return r;
    746  1.1  riastrad 		}
    747  1.1  riastrad 
    748  1.1  riastrad 		if (p->cs_flags & RADEON_CS_KEEP_TILING_FLAGS) {
    749  1.1  riastrad 			ib[idx] = (idx_value & 31) | /* keep the 1st 5 bits */
    750  1.1  riastrad 				  ((idx_value & ~31) + (u32)reloc->gpu_offset);
    751  1.1  riastrad 		} else {
    752  1.1  riastrad 			if (reloc->tiling_flags & RADEON_TILING_MACRO)
    753  1.1  riastrad 				tile_flags |= R300_TXO_MACRO_TILE;
    754  1.1  riastrad 			if (reloc->tiling_flags & RADEON_TILING_MICRO)
    755  1.1  riastrad 				tile_flags |= R300_TXO_MICRO_TILE;
    756  1.1  riastrad 			else if (reloc->tiling_flags & RADEON_TILING_MICRO_SQUARE)
    757  1.1  riastrad 				tile_flags |= R300_TXO_MICRO_TILE_SQUARE;
    758  1.1  riastrad 
    759  1.1  riastrad 			tmp = idx_value + ((u32)reloc->gpu_offset);
    760  1.1  riastrad 			tmp |= tile_flags;
    761  1.1  riastrad 			ib[idx] = tmp;
    762  1.1  riastrad 		}
    763  1.1  riastrad 		track->textures[i].robj = reloc->robj;
    764  1.1  riastrad 		track->tex_dirty = true;
    765  1.1  riastrad 		break;
    766  1.1  riastrad 	/* Tracked registers */
    767  1.1  riastrad 	case 0x2084:
    768  1.1  riastrad 		/* VAP_VF_CNTL */
    769  1.1  riastrad 		track->vap_vf_cntl = idx_value;
    770  1.1  riastrad 		break;
    771  1.1  riastrad 	case 0x20B4:
    772  1.1  riastrad 		/* VAP_VTX_SIZE */
    773  1.1  riastrad 		track->vtx_size = idx_value & 0x7F;
    774  1.1  riastrad 		break;
    775  1.1  riastrad 	case 0x2134:
    776  1.1  riastrad 		/* VAP_VF_MAX_VTX_INDX */
    777  1.1  riastrad 		track->max_indx = idx_value & 0x00FFFFFFUL;
    778  1.1  riastrad 		break;
    779  1.1  riastrad 	case 0x2088:
    780  1.1  riastrad 		/* VAP_ALT_NUM_VERTICES - only valid on r500 */
    781  1.1  riastrad 		if (p->rdev->family < CHIP_RV515)
    782  1.1  riastrad 			goto fail;
    783  1.1  riastrad 		track->vap_alt_nverts = idx_value & 0xFFFFFF;
    784  1.1  riastrad 		break;
    785  1.1  riastrad 	case 0x43E4:
    786  1.1  riastrad 		/* SC_SCISSOR1 */
    787  1.1  riastrad 		track->maxy = ((idx_value >> 13) & 0x1FFF) + 1;
    788  1.1  riastrad 		if (p->rdev->family < CHIP_RV515) {
    789  1.1  riastrad 			track->maxy -= 1440;
    790  1.1  riastrad 		}
    791  1.1  riastrad 		track->cb_dirty = true;
    792  1.1  riastrad 		track->zb_dirty = true;
    793  1.1  riastrad 		break;
    794  1.1  riastrad 	case 0x4E00:
    795  1.1  riastrad 		/* RB3D_CCTL */
    796  1.1  riastrad 		if ((idx_value & (1 << 10)) && /* CMASK_ENABLE */
    797  1.1  riastrad 		    p->rdev->cmask_filp != p->filp) {
    798  1.1  riastrad 			DRM_ERROR("Invalid RB3D_CCTL: Cannot enable CMASK.\n");
    799  1.1  riastrad 			return -EINVAL;
    800  1.1  riastrad 		}
    801  1.1  riastrad 		track->num_cb = ((idx_value >> 5) & 0x3) + 1;
    802  1.1  riastrad 		track->cb_dirty = true;
    803  1.1  riastrad 		break;
    804  1.1  riastrad 	case 0x4E38:
    805  1.1  riastrad 	case 0x4E3C:
    806  1.1  riastrad 	case 0x4E40:
    807  1.1  riastrad 	case 0x4E44:
    808  1.1  riastrad 		/* RB3D_COLORPITCH0 */
    809  1.1  riastrad 		/* RB3D_COLORPITCH1 */
    810  1.1  riastrad 		/* RB3D_COLORPITCH2 */
    811  1.1  riastrad 		/* RB3D_COLORPITCH3 */
    812  1.1  riastrad 		if (!(p->cs_flags & RADEON_CS_KEEP_TILING_FLAGS)) {
    813  1.1  riastrad 			r = radeon_cs_packet_next_reloc(p, &reloc, 0);
    814  1.1  riastrad 			if (r) {
    815  1.1  riastrad 				DRM_ERROR("No reloc for ib[%d]=0x%04X\n",
    816  1.1  riastrad 					  idx, reg);
    817  1.1  riastrad 				radeon_cs_dump_packet(p, pkt);
    818  1.1  riastrad 				return r;
    819  1.1  riastrad 			}
    820  1.1  riastrad 
    821  1.1  riastrad 			if (reloc->tiling_flags & RADEON_TILING_MACRO)
    822  1.1  riastrad 				tile_flags |= R300_COLOR_TILE_ENABLE;
    823  1.1  riastrad 			if (reloc->tiling_flags & RADEON_TILING_MICRO)
    824  1.1  riastrad 				tile_flags |= R300_COLOR_MICROTILE_ENABLE;
    825  1.1  riastrad 			else if (reloc->tiling_flags & RADEON_TILING_MICRO_SQUARE)
    826  1.1  riastrad 				tile_flags |= R300_COLOR_MICROTILE_SQUARE_ENABLE;
    827  1.1  riastrad 
    828  1.1  riastrad 			tmp = idx_value & ~(0x7 << 16);
    829  1.1  riastrad 			tmp |= tile_flags;
    830  1.1  riastrad 			ib[idx] = tmp;
    831  1.1  riastrad 		}
    832  1.1  riastrad 		i = (reg - 0x4E38) >> 2;
    833  1.1  riastrad 		track->cb[i].pitch = idx_value & 0x3FFE;
    834  1.1  riastrad 		switch (((idx_value >> 21) & 0xF)) {
    835  1.1  riastrad 		case 9:
    836  1.1  riastrad 		case 11:
    837  1.1  riastrad 		case 12:
    838  1.1  riastrad 			track->cb[i].cpp = 1;
    839  1.1  riastrad 			break;
    840  1.1  riastrad 		case 3:
    841  1.1  riastrad 		case 4:
    842  1.1  riastrad 		case 13:
    843  1.1  riastrad 		case 15:
    844  1.1  riastrad 			track->cb[i].cpp = 2;
    845  1.1  riastrad 			break;
    846  1.1  riastrad 		case 5:
    847  1.1  riastrad 			if (p->rdev->family < CHIP_RV515) {
    848  1.1  riastrad 				DRM_ERROR("Invalid color buffer format (%d)!\n",
    849  1.1  riastrad 					  ((idx_value >> 21) & 0xF));
    850  1.1  riastrad 				return -EINVAL;
    851  1.1  riastrad 			}
    852  1.3  riastrad 			/* Fall through. */
    853  1.1  riastrad 		case 6:
    854  1.1  riastrad 			track->cb[i].cpp = 4;
    855  1.1  riastrad 			break;
    856  1.1  riastrad 		case 10:
    857  1.1  riastrad 			track->cb[i].cpp = 8;
    858  1.1  riastrad 			break;
    859  1.1  riastrad 		case 7:
    860  1.1  riastrad 			track->cb[i].cpp = 16;
    861  1.1  riastrad 			break;
    862  1.1  riastrad 		default:
    863  1.1  riastrad 			DRM_ERROR("Invalid color buffer format (%d) !\n",
    864  1.1  riastrad 				  ((idx_value >> 21) & 0xF));
    865  1.1  riastrad 			return -EINVAL;
    866  1.1  riastrad 		}
    867  1.1  riastrad 		track->cb_dirty = true;
    868  1.1  riastrad 		break;
    869  1.1  riastrad 	case 0x4F00:
    870  1.1  riastrad 		/* ZB_CNTL */
    871  1.1  riastrad 		if (idx_value & 2) {
    872  1.1  riastrad 			track->z_enabled = true;
    873  1.1  riastrad 		} else {
    874  1.1  riastrad 			track->z_enabled = false;
    875  1.1  riastrad 		}
    876  1.1  riastrad 		track->zb_dirty = true;
    877  1.1  riastrad 		break;
    878  1.1  riastrad 	case 0x4F10:
    879  1.1  riastrad 		/* ZB_FORMAT */
    880  1.1  riastrad 		switch ((idx_value & 0xF)) {
    881  1.1  riastrad 		case 0:
    882  1.1  riastrad 		case 1:
    883  1.1  riastrad 			track->zb.cpp = 2;
    884  1.1  riastrad 			break;
    885  1.1  riastrad 		case 2:
    886  1.1  riastrad 			track->zb.cpp = 4;
    887  1.1  riastrad 			break;
    888  1.1  riastrad 		default:
    889  1.1  riastrad 			DRM_ERROR("Invalid z buffer format (%d) !\n",
    890  1.1  riastrad 				  (idx_value & 0xF));
    891  1.1  riastrad 			return -EINVAL;
    892  1.1  riastrad 		}
    893  1.1  riastrad 		track->zb_dirty = true;
    894  1.1  riastrad 		break;
    895  1.1  riastrad 	case 0x4F24:
    896  1.1  riastrad 		/* ZB_DEPTHPITCH */
    897  1.1  riastrad 		if (!(p->cs_flags & RADEON_CS_KEEP_TILING_FLAGS)) {
    898  1.1  riastrad 			r = radeon_cs_packet_next_reloc(p, &reloc, 0);
    899  1.1  riastrad 			if (r) {
    900  1.1  riastrad 				DRM_ERROR("No reloc for ib[%d]=0x%04X\n",
    901  1.1  riastrad 					  idx, reg);
    902  1.1  riastrad 				radeon_cs_dump_packet(p, pkt);
    903  1.1  riastrad 				return r;
    904  1.1  riastrad 			}
    905  1.1  riastrad 
    906  1.1  riastrad 			if (reloc->tiling_flags & RADEON_TILING_MACRO)
    907  1.1  riastrad 				tile_flags |= R300_DEPTHMACROTILE_ENABLE;
    908  1.1  riastrad 			if (reloc->tiling_flags & RADEON_TILING_MICRO)
    909  1.1  riastrad 				tile_flags |= R300_DEPTHMICROTILE_TILED;
    910  1.1  riastrad 			else if (reloc->tiling_flags & RADEON_TILING_MICRO_SQUARE)
    911  1.1  riastrad 				tile_flags |= R300_DEPTHMICROTILE_TILED_SQUARE;
    912  1.1  riastrad 
    913  1.1  riastrad 			tmp = idx_value & ~(0x7 << 16);
    914  1.1  riastrad 			tmp |= tile_flags;
    915  1.1  riastrad 			ib[idx] = tmp;
    916  1.1  riastrad 		}
    917  1.1  riastrad 		track->zb.pitch = idx_value & 0x3FFC;
    918  1.1  riastrad 		track->zb_dirty = true;
    919  1.1  riastrad 		break;
    920  1.1  riastrad 	case 0x4104:
    921  1.1  riastrad 		/* TX_ENABLE */
    922  1.1  riastrad 		for (i = 0; i < 16; i++) {
    923  1.1  riastrad 			bool enabled;
    924  1.1  riastrad 
    925  1.1  riastrad 			enabled = !!(idx_value & (1 << i));
    926  1.1  riastrad 			track->textures[i].enabled = enabled;
    927  1.1  riastrad 		}
    928  1.1  riastrad 		track->tex_dirty = true;
    929  1.1  riastrad 		break;
    930  1.1  riastrad 	case 0x44C0:
    931  1.1  riastrad 	case 0x44C4:
    932  1.1  riastrad 	case 0x44C8:
    933  1.1  riastrad 	case 0x44CC:
    934  1.1  riastrad 	case 0x44D0:
    935  1.1  riastrad 	case 0x44D4:
    936  1.1  riastrad 	case 0x44D8:
    937  1.1  riastrad 	case 0x44DC:
    938  1.1  riastrad 	case 0x44E0:
    939  1.1  riastrad 	case 0x44E4:
    940  1.1  riastrad 	case 0x44E8:
    941  1.1  riastrad 	case 0x44EC:
    942  1.1  riastrad 	case 0x44F0:
    943  1.1  riastrad 	case 0x44F4:
    944  1.1  riastrad 	case 0x44F8:
    945  1.1  riastrad 	case 0x44FC:
    946  1.1  riastrad 		/* TX_FORMAT1_[0-15] */
    947  1.1  riastrad 		i = (reg - 0x44C0) >> 2;
    948  1.1  riastrad 		tmp = (idx_value >> 25) & 0x3;
    949  1.1  riastrad 		track->textures[i].tex_coord_type = tmp;
    950  1.1  riastrad 		switch ((idx_value & 0x1F)) {
    951  1.1  riastrad 		case R300_TX_FORMAT_X8:
    952  1.1  riastrad 		case R300_TX_FORMAT_Y4X4:
    953  1.1  riastrad 		case R300_TX_FORMAT_Z3Y3X2:
    954  1.1  riastrad 			track->textures[i].cpp = 1;
    955  1.1  riastrad 			track->textures[i].compress_format = R100_TRACK_COMP_NONE;
    956  1.1  riastrad 			break;
    957  1.1  riastrad 		case R300_TX_FORMAT_X16:
    958  1.1  riastrad 		case R300_TX_FORMAT_FL_I16:
    959  1.1  riastrad 		case R300_TX_FORMAT_Y8X8:
    960  1.1  riastrad 		case R300_TX_FORMAT_Z5Y6X5:
    961  1.1  riastrad 		case R300_TX_FORMAT_Z6Y5X5:
    962  1.1  riastrad 		case R300_TX_FORMAT_W4Z4Y4X4:
    963  1.1  riastrad 		case R300_TX_FORMAT_W1Z5Y5X5:
    964  1.1  riastrad 		case R300_TX_FORMAT_D3DMFT_CxV8U8:
    965  1.1  riastrad 		case R300_TX_FORMAT_B8G8_B8G8:
    966  1.1  riastrad 		case R300_TX_FORMAT_G8R8_G8B8:
    967  1.1  riastrad 			track->textures[i].cpp = 2;
    968  1.1  riastrad 			track->textures[i].compress_format = R100_TRACK_COMP_NONE;
    969  1.1  riastrad 			break;
    970  1.1  riastrad 		case R300_TX_FORMAT_Y16X16:
    971  1.1  riastrad 		case R300_TX_FORMAT_FL_I16A16:
    972  1.1  riastrad 		case R300_TX_FORMAT_Z11Y11X10:
    973  1.1  riastrad 		case R300_TX_FORMAT_Z10Y11X11:
    974  1.1  riastrad 		case R300_TX_FORMAT_W8Z8Y8X8:
    975  1.1  riastrad 		case R300_TX_FORMAT_W2Z10Y10X10:
    976  1.1  riastrad 		case 0x17:
    977  1.1  riastrad 		case R300_TX_FORMAT_FL_I32:
    978  1.1  riastrad 		case 0x1e:
    979  1.1  riastrad 			track->textures[i].cpp = 4;
    980  1.1  riastrad 			track->textures[i].compress_format = R100_TRACK_COMP_NONE;
    981  1.1  riastrad 			break;
    982  1.1  riastrad 		case R300_TX_FORMAT_W16Z16Y16X16:
    983  1.1  riastrad 		case R300_TX_FORMAT_FL_R16G16B16A16:
    984  1.1  riastrad 		case R300_TX_FORMAT_FL_I32A32:
    985  1.1  riastrad 			track->textures[i].cpp = 8;
    986  1.1  riastrad 			track->textures[i].compress_format = R100_TRACK_COMP_NONE;
    987  1.1  riastrad 			break;
    988  1.1  riastrad 		case R300_TX_FORMAT_FL_R32G32B32A32:
    989  1.1  riastrad 			track->textures[i].cpp = 16;
    990  1.1  riastrad 			track->textures[i].compress_format = R100_TRACK_COMP_NONE;
    991  1.1  riastrad 			break;
    992  1.1  riastrad 		case R300_TX_FORMAT_DXT1:
    993  1.1  riastrad 			track->textures[i].cpp = 1;
    994  1.1  riastrad 			track->textures[i].compress_format = R100_TRACK_COMP_DXT1;
    995  1.1  riastrad 			break;
    996  1.1  riastrad 		case R300_TX_FORMAT_ATI2N:
    997  1.1  riastrad 			if (p->rdev->family < CHIP_R420) {
    998  1.1  riastrad 				DRM_ERROR("Invalid texture format %u\n",
    999  1.1  riastrad 					  (idx_value & 0x1F));
   1000  1.1  riastrad 				return -EINVAL;
   1001  1.1  riastrad 			}
   1002  1.1  riastrad 			/* The same rules apply as for DXT3/5. */
   1003  1.3  riastrad 			/* Fall through. */
   1004  1.1  riastrad 		case R300_TX_FORMAT_DXT3:
   1005  1.1  riastrad 		case R300_TX_FORMAT_DXT5:
   1006  1.1  riastrad 			track->textures[i].cpp = 1;
   1007  1.1  riastrad 			track->textures[i].compress_format = R100_TRACK_COMP_DXT35;
   1008  1.1  riastrad 			break;
   1009  1.1  riastrad 		default:
   1010  1.1  riastrad 			DRM_ERROR("Invalid texture format %u\n",
   1011  1.1  riastrad 				  (idx_value & 0x1F));
   1012  1.1  riastrad 			return -EINVAL;
   1013  1.1  riastrad 		}
   1014  1.1  riastrad 		track->tex_dirty = true;
   1015  1.1  riastrad 		break;
   1016  1.1  riastrad 	case 0x4400:
   1017  1.1  riastrad 	case 0x4404:
   1018  1.1  riastrad 	case 0x4408:
   1019  1.1  riastrad 	case 0x440C:
   1020  1.1  riastrad 	case 0x4410:
   1021  1.1  riastrad 	case 0x4414:
   1022  1.1  riastrad 	case 0x4418:
   1023  1.1  riastrad 	case 0x441C:
   1024  1.1  riastrad 	case 0x4420:
   1025  1.1  riastrad 	case 0x4424:
   1026  1.1  riastrad 	case 0x4428:
   1027  1.1  riastrad 	case 0x442C:
   1028  1.1  riastrad 	case 0x4430:
   1029  1.1  riastrad 	case 0x4434:
   1030  1.1  riastrad 	case 0x4438:
   1031  1.1  riastrad 	case 0x443C:
   1032  1.1  riastrad 		/* TX_FILTER0_[0-15] */
   1033  1.1  riastrad 		i = (reg - 0x4400) >> 2;
   1034  1.1  riastrad 		tmp = idx_value & 0x7;
   1035  1.1  riastrad 		if (tmp == 2 || tmp == 4 || tmp == 6) {
   1036  1.1  riastrad 			track->textures[i].roundup_w = false;
   1037  1.1  riastrad 		}
   1038  1.1  riastrad 		tmp = (idx_value >> 3) & 0x7;
   1039  1.1  riastrad 		if (tmp == 2 || tmp == 4 || tmp == 6) {
   1040  1.1  riastrad 			track->textures[i].roundup_h = false;
   1041  1.1  riastrad 		}
   1042  1.1  riastrad 		track->tex_dirty = true;
   1043  1.1  riastrad 		break;
   1044  1.1  riastrad 	case 0x4500:
   1045  1.1  riastrad 	case 0x4504:
   1046  1.1  riastrad 	case 0x4508:
   1047  1.1  riastrad 	case 0x450C:
   1048  1.1  riastrad 	case 0x4510:
   1049  1.1  riastrad 	case 0x4514:
   1050  1.1  riastrad 	case 0x4518:
   1051  1.1  riastrad 	case 0x451C:
   1052  1.1  riastrad 	case 0x4520:
   1053  1.1  riastrad 	case 0x4524:
   1054  1.1  riastrad 	case 0x4528:
   1055  1.1  riastrad 	case 0x452C:
   1056  1.1  riastrad 	case 0x4530:
   1057  1.1  riastrad 	case 0x4534:
   1058  1.1  riastrad 	case 0x4538:
   1059  1.1  riastrad 	case 0x453C:
   1060  1.1  riastrad 		/* TX_FORMAT2_[0-15] */
   1061  1.1  riastrad 		i = (reg - 0x4500) >> 2;
   1062  1.1  riastrad 		tmp = idx_value & 0x3FFF;
   1063  1.1  riastrad 		track->textures[i].pitch = tmp + 1;
   1064  1.1  riastrad 		if (p->rdev->family >= CHIP_RV515) {
   1065  1.1  riastrad 			tmp = ((idx_value >> 15) & 1) << 11;
   1066  1.1  riastrad 			track->textures[i].width_11 = tmp;
   1067  1.1  riastrad 			tmp = ((idx_value >> 16) & 1) << 11;
   1068  1.1  riastrad 			track->textures[i].height_11 = tmp;
   1069  1.1  riastrad 
   1070  1.1  riastrad 			/* ATI1N */
   1071  1.1  riastrad 			if (idx_value & (1 << 14)) {
   1072  1.1  riastrad 				/* The same rules apply as for DXT1. */
   1073  1.1  riastrad 				track->textures[i].compress_format =
   1074  1.1  riastrad 					R100_TRACK_COMP_DXT1;
   1075  1.1  riastrad 			}
   1076  1.1  riastrad 		} else if (idx_value & (1 << 14)) {
   1077  1.1  riastrad 			DRM_ERROR("Forbidden bit TXFORMAT_MSB\n");
   1078  1.1  riastrad 			return -EINVAL;
   1079  1.1  riastrad 		}
   1080  1.1  riastrad 		track->tex_dirty = true;
   1081  1.1  riastrad 		break;
   1082  1.1  riastrad 	case 0x4480:
   1083  1.1  riastrad 	case 0x4484:
   1084  1.1  riastrad 	case 0x4488:
   1085  1.1  riastrad 	case 0x448C:
   1086  1.1  riastrad 	case 0x4490:
   1087  1.1  riastrad 	case 0x4494:
   1088  1.1  riastrad 	case 0x4498:
   1089  1.1  riastrad 	case 0x449C:
   1090  1.1  riastrad 	case 0x44A0:
   1091  1.1  riastrad 	case 0x44A4:
   1092  1.1  riastrad 	case 0x44A8:
   1093  1.1  riastrad 	case 0x44AC:
   1094  1.1  riastrad 	case 0x44B0:
   1095  1.1  riastrad 	case 0x44B4:
   1096  1.1  riastrad 	case 0x44B8:
   1097  1.1  riastrad 	case 0x44BC:
   1098  1.1  riastrad 		/* TX_FORMAT0_[0-15] */
   1099  1.1  riastrad 		i = (reg - 0x4480) >> 2;
   1100  1.1  riastrad 		tmp = idx_value & 0x7FF;
   1101  1.1  riastrad 		track->textures[i].width = tmp + 1;
   1102  1.1  riastrad 		tmp = (idx_value >> 11) & 0x7FF;
   1103  1.1  riastrad 		track->textures[i].height = tmp + 1;
   1104  1.1  riastrad 		tmp = (idx_value >> 26) & 0xF;
   1105  1.1  riastrad 		track->textures[i].num_levels = tmp;
   1106  1.1  riastrad 		tmp = idx_value & (1 << 31);
   1107  1.1  riastrad 		track->textures[i].use_pitch = !!tmp;
   1108  1.1  riastrad 		tmp = (idx_value >> 22) & 0xF;
   1109  1.1  riastrad 		track->textures[i].txdepth = tmp;
   1110  1.1  riastrad 		track->tex_dirty = true;
   1111  1.1  riastrad 		break;
   1112  1.1  riastrad 	case R300_ZB_ZPASS_ADDR:
   1113  1.1  riastrad 		r = radeon_cs_packet_next_reloc(p, &reloc, 0);
   1114  1.1  riastrad 		if (r) {
   1115  1.1  riastrad 			DRM_ERROR("No reloc for ib[%d]=0x%04X\n",
   1116  1.1  riastrad 					idx, reg);
   1117  1.1  riastrad 			radeon_cs_dump_packet(p, pkt);
   1118  1.1  riastrad 			return r;
   1119  1.1  riastrad 		}
   1120  1.1  riastrad 		ib[idx] = idx_value + ((u32)reloc->gpu_offset);
   1121  1.1  riastrad 		break;
   1122  1.1  riastrad 	case 0x4e0c:
   1123  1.1  riastrad 		/* RB3D_COLOR_CHANNEL_MASK */
   1124  1.1  riastrad 		track->color_channel_mask = idx_value;
   1125  1.1  riastrad 		track->cb_dirty = true;
   1126  1.1  riastrad 		break;
   1127  1.1  riastrad 	case 0x43a4:
   1128  1.1  riastrad 		/* SC_HYPERZ_EN */
   1129  1.1  riastrad 		/* r300c emits this register - we need to disable hyperz for it
   1130  1.1  riastrad 		 * without complaining */
   1131  1.1  riastrad 		if (p->rdev->hyperz_filp != p->filp) {
   1132  1.1  riastrad 			if (idx_value & 0x1)
   1133  1.1  riastrad 				ib[idx] = idx_value & ~1;
   1134  1.1  riastrad 		}
   1135  1.1  riastrad 		break;
   1136  1.1  riastrad 	case 0x4f1c:
   1137  1.1  riastrad 		/* ZB_BW_CNTL */
   1138  1.1  riastrad 		track->zb_cb_clear = !!(idx_value & (1 << 5));
   1139  1.1  riastrad 		track->cb_dirty = true;
   1140  1.1  riastrad 		track->zb_dirty = true;
   1141  1.1  riastrad 		if (p->rdev->hyperz_filp != p->filp) {
   1142  1.1  riastrad 			if (idx_value & (R300_HIZ_ENABLE |
   1143  1.1  riastrad 					 R300_RD_COMP_ENABLE |
   1144  1.1  riastrad 					 R300_WR_COMP_ENABLE |
   1145  1.1  riastrad 					 R300_FAST_FILL_ENABLE))
   1146  1.1  riastrad 				goto fail;
   1147  1.1  riastrad 		}
   1148  1.1  riastrad 		break;
   1149  1.1  riastrad 	case 0x4e04:
   1150  1.1  riastrad 		/* RB3D_BLENDCNTL */
   1151  1.1  riastrad 		track->blend_read_enable = !!(idx_value & (1 << 2));
   1152  1.1  riastrad 		track->cb_dirty = true;
   1153  1.1  riastrad 		break;
   1154  1.1  riastrad 	case R300_RB3D_AARESOLVE_OFFSET:
   1155  1.1  riastrad 		r = radeon_cs_packet_next_reloc(p, &reloc, 0);
   1156  1.1  riastrad 		if (r) {
   1157  1.1  riastrad 			DRM_ERROR("No reloc for ib[%d]=0x%04X\n",
   1158  1.1  riastrad 				  idx, reg);
   1159  1.1  riastrad 			radeon_cs_dump_packet(p, pkt);
   1160  1.1  riastrad 			return r;
   1161  1.1  riastrad 		}
   1162  1.1  riastrad 		track->aa.robj = reloc->robj;
   1163  1.1  riastrad 		track->aa.offset = idx_value;
   1164  1.1  riastrad 		track->aa_dirty = true;
   1165  1.1  riastrad 		ib[idx] = idx_value + ((u32)reloc->gpu_offset);
   1166  1.1  riastrad 		break;
   1167  1.1  riastrad 	case R300_RB3D_AARESOLVE_PITCH:
   1168  1.1  riastrad 		track->aa.pitch = idx_value & 0x3FFE;
   1169  1.1  riastrad 		track->aa_dirty = true;
   1170  1.1  riastrad 		break;
   1171  1.1  riastrad 	case R300_RB3D_AARESOLVE_CTL:
   1172  1.1  riastrad 		track->aaresolve = idx_value & 0x1;
   1173  1.1  riastrad 		track->aa_dirty = true;
   1174  1.1  riastrad 		break;
   1175  1.1  riastrad 	case 0x4f30: /* ZB_MASK_OFFSET */
   1176  1.1  riastrad 	case 0x4f34: /* ZB_ZMASK_PITCH */
   1177  1.1  riastrad 	case 0x4f44: /* ZB_HIZ_OFFSET */
   1178  1.1  riastrad 	case 0x4f54: /* ZB_HIZ_PITCH */
   1179  1.1  riastrad 		if (idx_value && (p->rdev->hyperz_filp != p->filp))
   1180  1.1  riastrad 			goto fail;
   1181  1.1  riastrad 		break;
   1182  1.1  riastrad 	case 0x4028:
   1183  1.1  riastrad 		if (idx_value && (p->rdev->hyperz_filp != p->filp))
   1184  1.1  riastrad 			goto fail;
   1185  1.1  riastrad 		/* GB_Z_PEQ_CONFIG */
   1186  1.1  riastrad 		if (p->rdev->family >= CHIP_RV350)
   1187  1.1  riastrad 			break;
   1188  1.1  riastrad 		goto fail;
   1189  1.1  riastrad 		break;
   1190  1.1  riastrad 	case 0x4be8:
   1191  1.1  riastrad 		/* valid register only on RV530 */
   1192  1.1  riastrad 		if (p->rdev->family == CHIP_RV530)
   1193  1.1  riastrad 			break;
   1194  1.1  riastrad 		/* fallthrough do not move */
   1195  1.1  riastrad 	default:
   1196  1.1  riastrad 		goto fail;
   1197  1.1  riastrad 	}
   1198  1.1  riastrad 	return 0;
   1199  1.1  riastrad fail:
   1200  1.3  riastrad 	pr_err("Forbidden register 0x%04X in cs at %d (val=%08x)\n",
   1201  1.1  riastrad 	       reg, idx, idx_value);
   1202  1.1  riastrad 	return -EINVAL;
   1203  1.1  riastrad }
   1204  1.1  riastrad 
   1205  1.1  riastrad static int r300_packet3_check(struct radeon_cs_parser *p,
   1206  1.1  riastrad 			      struct radeon_cs_packet *pkt)
   1207  1.1  riastrad {
   1208  1.1  riastrad 	struct radeon_bo_list *reloc;
   1209  1.1  riastrad 	struct r100_cs_track *track;
   1210  1.1  riastrad 	volatile uint32_t *ib;
   1211  1.1  riastrad 	unsigned idx;
   1212  1.1  riastrad 	int r;
   1213  1.1  riastrad 
   1214  1.1  riastrad 	ib = p->ib.ptr;
   1215  1.1  riastrad 	idx = pkt->idx + 1;
   1216  1.1  riastrad 	track = (struct r100_cs_track *)p->track;
   1217  1.1  riastrad 	switch(pkt->opcode) {
   1218  1.1  riastrad 	case PACKET3_3D_LOAD_VBPNTR:
   1219  1.1  riastrad 		r = r100_packet3_load_vbpntr(p, pkt, idx);
   1220  1.1  riastrad 		if (r)
   1221  1.1  riastrad 			return r;
   1222  1.1  riastrad 		break;
   1223  1.1  riastrad 	case PACKET3_INDX_BUFFER:
   1224  1.1  riastrad 		r = radeon_cs_packet_next_reloc(p, &reloc, 0);
   1225  1.1  riastrad 		if (r) {
   1226  1.1  riastrad 			DRM_ERROR("No reloc for packet3 %d\n", pkt->opcode);
   1227  1.1  riastrad 			radeon_cs_dump_packet(p, pkt);
   1228  1.1  riastrad 			return r;
   1229  1.1  riastrad 		}
   1230  1.1  riastrad 		ib[idx+1] = radeon_get_ib_value(p, idx + 1) + ((u32)reloc->gpu_offset);
   1231  1.1  riastrad 		r = r100_cs_track_check_pkt3_indx_buffer(p, pkt, reloc->robj);
   1232  1.1  riastrad 		if (r) {
   1233  1.1  riastrad 			return r;
   1234  1.1  riastrad 		}
   1235  1.1  riastrad 		break;
   1236  1.1  riastrad 	/* Draw packet */
   1237  1.1  riastrad 	case PACKET3_3D_DRAW_IMMD:
   1238  1.1  riastrad 		/* Number of dwords is vtx_size * (num_vertices - 1)
   1239  1.1  riastrad 		 * PRIM_WALK must be equal to 3 vertex data in embedded
   1240  1.1  riastrad 		 * in cmd stream */
   1241  1.1  riastrad 		if (((radeon_get_ib_value(p, idx + 1) >> 4) & 0x3) != 3) {
   1242  1.1  riastrad 			DRM_ERROR("PRIM_WALK must be 3 for IMMD draw\n");
   1243  1.1  riastrad 			return -EINVAL;
   1244  1.1  riastrad 		}
   1245  1.1  riastrad 		track->vap_vf_cntl = radeon_get_ib_value(p, idx + 1);
   1246  1.1  riastrad 		track->immd_dwords = pkt->count - 1;
   1247  1.1  riastrad 		r = r100_cs_track_check(p->rdev, track);
   1248  1.1  riastrad 		if (r) {
   1249  1.1  riastrad 			return r;
   1250  1.1  riastrad 		}
   1251  1.1  riastrad 		break;
   1252  1.1  riastrad 	case PACKET3_3D_DRAW_IMMD_2:
   1253  1.1  riastrad 		/* Number of dwords is vtx_size * (num_vertices - 1)
   1254  1.1  riastrad 		 * PRIM_WALK must be equal to 3 vertex data in embedded
   1255  1.1  riastrad 		 * in cmd stream */
   1256  1.1  riastrad 		if (((radeon_get_ib_value(p, idx) >> 4) & 0x3) != 3) {
   1257  1.1  riastrad 			DRM_ERROR("PRIM_WALK must be 3 for IMMD draw\n");
   1258  1.1  riastrad 			return -EINVAL;
   1259  1.1  riastrad 		}
   1260  1.1  riastrad 		track->vap_vf_cntl = radeon_get_ib_value(p, idx);
   1261  1.1  riastrad 		track->immd_dwords = pkt->count;
   1262  1.1  riastrad 		r = r100_cs_track_check(p->rdev, track);
   1263  1.1  riastrad 		if (r) {
   1264  1.1  riastrad 			return r;
   1265  1.1  riastrad 		}
   1266  1.1  riastrad 		break;
   1267  1.1  riastrad 	case PACKET3_3D_DRAW_VBUF:
   1268  1.1  riastrad 		track->vap_vf_cntl = radeon_get_ib_value(p, idx + 1);
   1269  1.1  riastrad 		r = r100_cs_track_check(p->rdev, track);
   1270  1.1  riastrad 		if (r) {
   1271  1.1  riastrad 			return r;
   1272  1.1  riastrad 		}
   1273  1.1  riastrad 		break;
   1274  1.1  riastrad 	case PACKET3_3D_DRAW_VBUF_2:
   1275  1.1  riastrad 		track->vap_vf_cntl = radeon_get_ib_value(p, idx);
   1276  1.1  riastrad 		r = r100_cs_track_check(p->rdev, track);
   1277  1.1  riastrad 		if (r) {
   1278  1.1  riastrad 			return r;
   1279  1.1  riastrad 		}
   1280  1.1  riastrad 		break;
   1281  1.1  riastrad 	case PACKET3_3D_DRAW_INDX:
   1282  1.1  riastrad 		track->vap_vf_cntl = radeon_get_ib_value(p, idx + 1);
   1283  1.1  riastrad 		r = r100_cs_track_check(p->rdev, track);
   1284  1.1  riastrad 		if (r) {
   1285  1.1  riastrad 			return r;
   1286  1.1  riastrad 		}
   1287  1.1  riastrad 		break;
   1288  1.1  riastrad 	case PACKET3_3D_DRAW_INDX_2:
   1289  1.1  riastrad 		track->vap_vf_cntl = radeon_get_ib_value(p, idx);
   1290  1.1  riastrad 		r = r100_cs_track_check(p->rdev, track);
   1291  1.1  riastrad 		if (r) {
   1292  1.1  riastrad 			return r;
   1293  1.1  riastrad 		}
   1294  1.1  riastrad 		break;
   1295  1.1  riastrad 	case PACKET3_3D_CLEAR_HIZ:
   1296  1.1  riastrad 	case PACKET3_3D_CLEAR_ZMASK:
   1297  1.1  riastrad 		if (p->rdev->hyperz_filp != p->filp)
   1298  1.1  riastrad 			return -EINVAL;
   1299  1.1  riastrad 		break;
   1300  1.1  riastrad 	case PACKET3_3D_CLEAR_CMASK:
   1301  1.1  riastrad 		if (p->rdev->cmask_filp != p->filp)
   1302  1.1  riastrad 			return -EINVAL;
   1303  1.1  riastrad 		break;
   1304  1.1  riastrad 	case PACKET3_NOP:
   1305  1.1  riastrad 		break;
   1306  1.1  riastrad 	default:
   1307  1.1  riastrad 		DRM_ERROR("Packet3 opcode %x not supported\n", pkt->opcode);
   1308  1.1  riastrad 		return -EINVAL;
   1309  1.1  riastrad 	}
   1310  1.1  riastrad 	return 0;
   1311  1.1  riastrad }
   1312  1.1  riastrad 
   1313  1.1  riastrad int r300_cs_parse(struct radeon_cs_parser *p)
   1314  1.1  riastrad {
   1315  1.1  riastrad 	struct radeon_cs_packet pkt;
   1316  1.1  riastrad 	struct r100_cs_track *track;
   1317  1.1  riastrad 	int r;
   1318  1.1  riastrad 
   1319  1.1  riastrad 	track = kzalloc(sizeof(*track), GFP_KERNEL);
   1320  1.1  riastrad 	if (track == NULL)
   1321  1.1  riastrad 		return -ENOMEM;
   1322  1.1  riastrad 	r100_cs_track_clear(p->rdev, track);
   1323  1.1  riastrad 	p->track = track;
   1324  1.1  riastrad 	do {
   1325  1.1  riastrad 		r = radeon_cs_packet_parse(p, &pkt, p->idx);
   1326  1.1  riastrad 		if (r) {
   1327  1.1  riastrad 			return r;
   1328  1.1  riastrad 		}
   1329  1.1  riastrad 		p->idx += pkt.count + 2;
   1330  1.1  riastrad 		switch (pkt.type) {
   1331  1.1  riastrad 		case RADEON_PACKET_TYPE0:
   1332  1.1  riastrad 			r = r100_cs_parse_packet0(p, &pkt,
   1333  1.1  riastrad 						  p->rdev->config.r300.reg_safe_bm,
   1334  1.1  riastrad 						  p->rdev->config.r300.reg_safe_bm_size,
   1335  1.1  riastrad 						  &r300_packet0_check);
   1336  1.1  riastrad 			break;
   1337  1.1  riastrad 		case RADEON_PACKET_TYPE2:
   1338  1.1  riastrad 			break;
   1339  1.1  riastrad 		case RADEON_PACKET_TYPE3:
   1340  1.1  riastrad 			r = r300_packet3_check(p, &pkt);
   1341  1.1  riastrad 			break;
   1342  1.1  riastrad 		default:
   1343  1.1  riastrad 			DRM_ERROR("Unknown packet type %d !\n", pkt.type);
   1344  1.1  riastrad 			return -EINVAL;
   1345  1.1  riastrad 		}
   1346  1.1  riastrad 		if (r) {
   1347  1.1  riastrad 			return r;
   1348  1.1  riastrad 		}
   1349  1.1  riastrad 	} while (p->idx < p->chunk_ib->length_dw);
   1350  1.1  riastrad 	return 0;
   1351  1.1  riastrad }
   1352  1.1  riastrad 
   1353  1.1  riastrad void r300_set_reg_safe(struct radeon_device *rdev)
   1354  1.1  riastrad {
   1355  1.1  riastrad 	rdev->config.r300.reg_safe_bm = r300_reg_safe_bm;
   1356  1.1  riastrad 	rdev->config.r300.reg_safe_bm_size = ARRAY_SIZE(r300_reg_safe_bm);
   1357  1.1  riastrad }
   1358  1.1  riastrad 
   1359  1.1  riastrad void r300_mc_program(struct radeon_device *rdev)
   1360  1.1  riastrad {
   1361  1.1  riastrad 	struct r100_mc_save save;
   1362  1.1  riastrad 	int r;
   1363  1.1  riastrad 
   1364  1.1  riastrad 	r = r100_debugfs_mc_info_init(rdev);
   1365  1.1  riastrad 	if (r) {
   1366  1.1  riastrad 		dev_err(rdev->dev, "Failed to create r100_mc debugfs file.\n");
   1367  1.1  riastrad 	}
   1368  1.1  riastrad 
   1369  1.1  riastrad 	/* Stops all mc clients */
   1370  1.1  riastrad 	r100_mc_stop(rdev, &save);
   1371  1.1  riastrad 	if (rdev->flags & RADEON_IS_AGP) {
   1372  1.1  riastrad 		WREG32(R_00014C_MC_AGP_LOCATION,
   1373  1.1  riastrad 			S_00014C_MC_AGP_START(rdev->mc.gtt_start >> 16) |
   1374  1.1  riastrad 			S_00014C_MC_AGP_TOP(rdev->mc.gtt_end >> 16));
   1375  1.1  riastrad 		WREG32(R_000170_AGP_BASE, lower_32_bits(rdev->mc.agp_base));
   1376  1.1  riastrad 		WREG32(R_00015C_AGP_BASE_2,
   1377  1.1  riastrad 			upper_32_bits(rdev->mc.agp_base) & 0xff);
   1378  1.1  riastrad 	} else {
   1379  1.1  riastrad 		WREG32(R_00014C_MC_AGP_LOCATION, 0x0FFFFFFF);
   1380  1.1  riastrad 		WREG32(R_000170_AGP_BASE, 0);
   1381  1.1  riastrad 		WREG32(R_00015C_AGP_BASE_2, 0);
   1382  1.1  riastrad 	}
   1383  1.1  riastrad 	/* Wait for mc idle */
   1384  1.1  riastrad 	if (r300_mc_wait_for_idle(rdev))
   1385  1.1  riastrad 		DRM_INFO("Failed to wait MC idle before programming MC.\n");
   1386  1.1  riastrad 	/* Program MC, should be a 32bits limited address space */
   1387  1.1  riastrad 	WREG32(R_000148_MC_FB_LOCATION,
   1388  1.1  riastrad 		S_000148_MC_FB_START(rdev->mc.vram_start >> 16) |
   1389  1.1  riastrad 		S_000148_MC_FB_TOP(rdev->mc.vram_end >> 16));
   1390  1.1  riastrad 	r100_mc_resume(rdev, &save);
   1391  1.1  riastrad }
   1392  1.1  riastrad 
   1393  1.1  riastrad void r300_clock_startup(struct radeon_device *rdev)
   1394  1.1  riastrad {
   1395  1.1  riastrad 	u32 tmp;
   1396  1.1  riastrad 
   1397  1.1  riastrad 	if (radeon_dynclks != -1 && radeon_dynclks)
   1398  1.1  riastrad 		radeon_legacy_set_clock_gating(rdev, 1);
   1399  1.1  riastrad 	/* We need to force on some of the block */
   1400  1.1  riastrad 	tmp = RREG32_PLL(R_00000D_SCLK_CNTL);
   1401  1.1  riastrad 	tmp |= S_00000D_FORCE_CP(1) | S_00000D_FORCE_VIP(1);
   1402  1.1  riastrad 	if ((rdev->family == CHIP_RV350) || (rdev->family == CHIP_RV380))
   1403  1.1  riastrad 		tmp |= S_00000D_FORCE_VAP(1);
   1404  1.1  riastrad 	WREG32_PLL(R_00000D_SCLK_CNTL, tmp);
   1405  1.1  riastrad }
   1406  1.1  riastrad 
   1407  1.1  riastrad static int r300_startup(struct radeon_device *rdev)
   1408  1.1  riastrad {
   1409  1.1  riastrad 	int r;
   1410  1.1  riastrad 
   1411  1.1  riastrad 	/* set common regs */
   1412  1.1  riastrad 	r100_set_common_regs(rdev);
   1413  1.1  riastrad 	/* program mc */
   1414  1.1  riastrad 	r300_mc_program(rdev);
   1415  1.1  riastrad 	/* Resume clock */
   1416  1.1  riastrad 	r300_clock_startup(rdev);
   1417  1.1  riastrad 	/* Initialize GPU configuration (# pipes, ...) */
   1418  1.1  riastrad 	r300_gpu_init(rdev);
   1419  1.1  riastrad 	/* Initialize GART (initialize after TTM so we can allocate
   1420  1.1  riastrad 	 * memory through TTM but finalize after TTM) */
   1421  1.1  riastrad 	if (rdev->flags & RADEON_IS_PCIE) {
   1422  1.1  riastrad 		r = rv370_pcie_gart_enable(rdev);
   1423  1.1  riastrad 		if (r)
   1424  1.1  riastrad 			return r;
   1425  1.1  riastrad 	}
   1426  1.1  riastrad 
   1427  1.1  riastrad 	if (rdev->family == CHIP_R300 ||
   1428  1.1  riastrad 	    rdev->family == CHIP_R350 ||
   1429  1.1  riastrad 	    rdev->family == CHIP_RV350)
   1430  1.1  riastrad 		r100_enable_bm(rdev);
   1431  1.1  riastrad 
   1432  1.1  riastrad 	if (rdev->flags & RADEON_IS_PCI) {
   1433  1.1  riastrad 		r = r100_pci_gart_enable(rdev);
   1434  1.1  riastrad 		if (r)
   1435  1.1  riastrad 			return r;
   1436  1.1  riastrad 	}
   1437  1.1  riastrad 
   1438  1.1  riastrad 	/* allocate wb buffer */
   1439  1.1  riastrad 	r = radeon_wb_init(rdev);
   1440  1.1  riastrad 	if (r)
   1441  1.1  riastrad 		return r;
   1442  1.1  riastrad 
   1443  1.1  riastrad 	r = radeon_fence_driver_start_ring(rdev, RADEON_RING_TYPE_GFX_INDEX);
   1444  1.1  riastrad 	if (r) {
   1445  1.1  riastrad 		dev_err(rdev->dev, "failed initializing CP fences (%d).\n", r);
   1446  1.1  riastrad 		return r;
   1447  1.1  riastrad 	}
   1448  1.1  riastrad 
   1449  1.1  riastrad 	/* Enable IRQ */
   1450  1.1  riastrad 	if (!rdev->irq.installed) {
   1451  1.1  riastrad 		r = radeon_irq_kms_init(rdev);
   1452  1.1  riastrad 		if (r)
   1453  1.1  riastrad 			return r;
   1454  1.1  riastrad 	}
   1455  1.1  riastrad 
   1456  1.1  riastrad 	r100_irq_set(rdev);
   1457  1.1  riastrad 	rdev->config.r300.hdp_cntl = RREG32(RADEON_HOST_PATH_CNTL);
   1458  1.1  riastrad 	/* 1M ring buffer */
   1459  1.1  riastrad 	r = r100_cp_init(rdev, 1024 * 1024);
   1460  1.1  riastrad 	if (r) {
   1461  1.1  riastrad 		dev_err(rdev->dev, "failed initializing CP (%d).\n", r);
   1462  1.1  riastrad 		return r;
   1463  1.1  riastrad 	}
   1464  1.1  riastrad 
   1465  1.1  riastrad 	r = radeon_ib_pool_init(rdev);
   1466  1.1  riastrad 	if (r) {
   1467  1.1  riastrad 		dev_err(rdev->dev, "IB initialization failed (%d).\n", r);
   1468  1.1  riastrad 		return r;
   1469  1.1  riastrad 	}
   1470  1.1  riastrad 
   1471  1.1  riastrad 	return 0;
   1472  1.1  riastrad }
   1473  1.1  riastrad 
   1474  1.1  riastrad int r300_resume(struct radeon_device *rdev)
   1475  1.1  riastrad {
   1476  1.1  riastrad 	int r;
   1477  1.1  riastrad 
   1478  1.1  riastrad 	/* Make sur GART are not working */
   1479  1.1  riastrad 	if (rdev->flags & RADEON_IS_PCIE)
   1480  1.1  riastrad 		rv370_pcie_gart_disable(rdev);
   1481  1.1  riastrad 	if (rdev->flags & RADEON_IS_PCI)
   1482  1.1  riastrad 		r100_pci_gart_disable(rdev);
   1483  1.1  riastrad 	/* Resume clock before doing reset */
   1484  1.1  riastrad 	r300_clock_startup(rdev);
   1485  1.1  riastrad 	/* Reset gpu before posting otherwise ATOM will enter infinite loop */
   1486  1.1  riastrad 	if (radeon_asic_reset(rdev)) {
   1487  1.1  riastrad 		dev_warn(rdev->dev, "GPU reset failed ! (0xE40=0x%08X, 0x7C0=0x%08X)\n",
   1488  1.1  riastrad 			RREG32(R_000E40_RBBM_STATUS),
   1489  1.1  riastrad 			RREG32(R_0007C0_CP_STAT));
   1490  1.1  riastrad 	}
   1491  1.1  riastrad 	/* post */
   1492  1.1  riastrad 	radeon_combios_asic_init(rdev->ddev);
   1493  1.1  riastrad 	/* Resume clock after posting */
   1494  1.1  riastrad 	r300_clock_startup(rdev);
   1495  1.1  riastrad 	/* Initialize surface registers */
   1496  1.1  riastrad 	radeon_surface_init(rdev);
   1497  1.1  riastrad 
   1498  1.1  riastrad 	rdev->accel_working = true;
   1499  1.1  riastrad 	r = r300_startup(rdev);
   1500  1.1  riastrad 	if (r) {
   1501  1.1  riastrad 		rdev->accel_working = false;
   1502  1.1  riastrad 	}
   1503  1.1  riastrad 	return r;
   1504  1.1  riastrad }
   1505  1.1  riastrad 
   1506  1.1  riastrad int r300_suspend(struct radeon_device *rdev)
   1507  1.1  riastrad {
   1508  1.1  riastrad 	radeon_pm_suspend(rdev);
   1509  1.1  riastrad 	r100_cp_disable(rdev);
   1510  1.1  riastrad 	radeon_wb_disable(rdev);
   1511  1.1  riastrad 	r100_irq_disable(rdev);
   1512  1.1  riastrad 	if (rdev->flags & RADEON_IS_PCIE)
   1513  1.1  riastrad 		rv370_pcie_gart_disable(rdev);
   1514  1.1  riastrad 	if (rdev->flags & RADEON_IS_PCI)
   1515  1.1  riastrad 		r100_pci_gart_disable(rdev);
   1516  1.1  riastrad 	return 0;
   1517  1.1  riastrad }
   1518  1.1  riastrad 
   1519  1.1  riastrad void r300_fini(struct radeon_device *rdev)
   1520  1.1  riastrad {
   1521  1.1  riastrad 	radeon_pm_fini(rdev);
   1522  1.1  riastrad 	r100_cp_fini(rdev);
   1523  1.1  riastrad 	radeon_wb_fini(rdev);
   1524  1.1  riastrad 	radeon_ib_pool_fini(rdev);
   1525  1.1  riastrad 	radeon_gem_fini(rdev);
   1526  1.1  riastrad 	if (rdev->flags & RADEON_IS_PCIE)
   1527  1.1  riastrad 		rv370_pcie_gart_fini(rdev);
   1528  1.1  riastrad 	if (rdev->flags & RADEON_IS_PCI)
   1529  1.1  riastrad 		r100_pci_gart_fini(rdev);
   1530  1.1  riastrad 	radeon_agp_fini(rdev);
   1531  1.1  riastrad 	radeon_irq_kms_fini(rdev);
   1532  1.1  riastrad 	radeon_fence_driver_fini(rdev);
   1533  1.1  riastrad 	radeon_bo_fini(rdev);
   1534  1.1  riastrad 	radeon_atombios_fini(rdev);
   1535  1.1  riastrad 	kfree(rdev->bios);
   1536  1.1  riastrad 	rdev->bios = NULL;
   1537  1.1  riastrad }
   1538  1.1  riastrad 
   1539  1.1  riastrad int r300_init(struct radeon_device *rdev)
   1540  1.1  riastrad {
   1541  1.1  riastrad 	int r;
   1542  1.1  riastrad 
   1543  1.1  riastrad 	/* Disable VGA */
   1544  1.1  riastrad 	r100_vga_render_disable(rdev);
   1545  1.1  riastrad 	/* Initialize scratch registers */
   1546  1.1  riastrad 	radeon_scratch_init(rdev);
   1547  1.1  riastrad 	/* Initialize surface registers */
   1548  1.1  riastrad 	radeon_surface_init(rdev);
   1549  1.1  riastrad 	/* TODO: disable VGA need to use VGA request */
   1550  1.1  riastrad 	/* restore some register to sane defaults */
   1551  1.1  riastrad 	r100_restore_sanity(rdev);
   1552  1.1  riastrad 	/* BIOS*/
   1553  1.1  riastrad 	if (!radeon_get_bios(rdev)) {
   1554  1.1  riastrad 		if (ASIC_IS_AVIVO(rdev))
   1555  1.1  riastrad 			return -EINVAL;
   1556  1.1  riastrad 	}
   1557  1.1  riastrad 	if (rdev->is_atom_bios) {
   1558  1.1  riastrad 		dev_err(rdev->dev, "Expecting combios for RS400/RS480 GPU\n");
   1559  1.1  riastrad 		return -EINVAL;
   1560  1.1  riastrad 	} else {
   1561  1.1  riastrad 		r = radeon_combios_init(rdev);
   1562  1.1  riastrad 		if (r)
   1563  1.1  riastrad 			return r;
   1564  1.1  riastrad 	}
   1565  1.1  riastrad 	/* Reset gpu before posting otherwise ATOM will enter infinite loop */
   1566  1.1  riastrad 	if (radeon_asic_reset(rdev)) {
   1567  1.1  riastrad 		dev_warn(rdev->dev,
   1568  1.1  riastrad 			"GPU reset failed ! (0xE40=0x%08X, 0x7C0=0x%08X)\n",
   1569  1.1  riastrad 			RREG32(R_000E40_RBBM_STATUS),
   1570  1.1  riastrad 			RREG32(R_0007C0_CP_STAT));
   1571  1.1  riastrad 	}
   1572  1.1  riastrad 	/* check if cards are posted or not */
   1573  1.1  riastrad 	if (radeon_boot_test_post_card(rdev) == false)
   1574  1.1  riastrad 		return -EINVAL;
   1575  1.1  riastrad 	/* Set asic errata */
   1576  1.1  riastrad 	r300_errata(rdev);
   1577  1.1  riastrad 	/* Initialize clocks */
   1578  1.1  riastrad 	radeon_get_clock_info(rdev->ddev);
   1579  1.1  riastrad 	/* initialize AGP */
   1580  1.1  riastrad 	if (rdev->flags & RADEON_IS_AGP) {
   1581  1.1  riastrad 		r = radeon_agp_init(rdev);
   1582  1.1  riastrad 		if (r) {
   1583  1.1  riastrad 			radeon_agp_disable(rdev);
   1584  1.1  riastrad 		}
   1585  1.1  riastrad 	}
   1586  1.1  riastrad 	/* initialize memory controller */
   1587  1.1  riastrad 	r300_mc_init(rdev);
   1588  1.1  riastrad 	/* Fence driver */
   1589  1.1  riastrad 	r = radeon_fence_driver_init(rdev);
   1590  1.1  riastrad 	if (r)
   1591  1.1  riastrad 		return r;
   1592  1.1  riastrad 	/* Memory manager */
   1593  1.1  riastrad 	r = radeon_bo_init(rdev);
   1594  1.1  riastrad 	if (r)
   1595  1.1  riastrad 		return r;
   1596  1.1  riastrad 	if (rdev->flags & RADEON_IS_PCIE) {
   1597  1.1  riastrad 		r = rv370_pcie_gart_init(rdev);
   1598  1.1  riastrad 		if (r)
   1599  1.1  riastrad 			return r;
   1600  1.1  riastrad 	}
   1601  1.1  riastrad 	if (rdev->flags & RADEON_IS_PCI) {
   1602  1.1  riastrad 		r = r100_pci_gart_init(rdev);
   1603  1.1  riastrad 		if (r)
   1604  1.1  riastrad 			return r;
   1605  1.1  riastrad 	}
   1606  1.1  riastrad 	r300_set_reg_safe(rdev);
   1607  1.1  riastrad 
   1608  1.1  riastrad 	/* Initialize power management */
   1609  1.1  riastrad 	radeon_pm_init(rdev);
   1610  1.1  riastrad 
   1611  1.1  riastrad 	rdev->accel_working = true;
   1612  1.1  riastrad 	r = r300_startup(rdev);
   1613  1.1  riastrad 	if (r) {
   1614  1.1  riastrad 		/* Something went wrong with the accel init, so stop accel */
   1615  1.1  riastrad 		dev_err(rdev->dev, "Disabling GPU acceleration\n");
   1616  1.1  riastrad 		r100_cp_fini(rdev);
   1617  1.1  riastrad 		radeon_wb_fini(rdev);
   1618  1.1  riastrad 		radeon_ib_pool_fini(rdev);
   1619  1.1  riastrad 		radeon_irq_kms_fini(rdev);
   1620  1.1  riastrad 		if (rdev->flags & RADEON_IS_PCIE)
   1621  1.1  riastrad 			rv370_pcie_gart_fini(rdev);
   1622  1.1  riastrad 		if (rdev->flags & RADEON_IS_PCI)
   1623  1.1  riastrad 			r100_pci_gart_fini(rdev);
   1624  1.1  riastrad 		radeon_agp_fini(rdev);
   1625  1.1  riastrad 		rdev->accel_working = false;
   1626  1.1  riastrad 	}
   1627  1.1  riastrad 	return 0;
   1628  1.1  riastrad }
   1629