r300.c 41.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/*
 * Copyright 2008 Advanced Micro Devices, Inc.
 * Copyright 2008 Red Hat Inc.
 * Copyright 2009 Jerome Glisse.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 * Authors: Dave Airlie
 *          Alex Deucher
 *          Jerome Glisse
 */
#include <linux/seq_file.h>
29
#include <linux/slab.h>
30 31 32
#include <drm/drmP.h>
#include <drm/drm.h>
#include <drm/drm_crtc_helper.h>
33 34
#include "radeon_reg.h"
#include "radeon.h"
35
#include "radeon_asic.h"
36
#include <drm/radeon_drm.h>
37
#include "r100_track.h"
38
#include "r300d.h"
39
#include "rv350d.h"
40 41
#include "r300_reg_safe.h"

42 43 44 45 46 47 48 49 50
/* This files gather functions specifics to: r300,r350,rv350,rv370,rv380
 *
 * GPU Errata:
 * - HOST_PATH_CNTL: r300 family seems to dislike write to HOST_PATH_CNTL
 *   using MMIO to flush host path read cache, this lead to HARDLOCKUP.
 *   However, scheduling such write to the ring seems harmless, i suspect
 *   the CP read collide with the flush somehow, or maybe the MC, hard to
 *   tell. (Jerome Glisse)
 */
51 52 53 54

/*
 * rv370,rv380 PCIE GART
 */
55 56
static int rv370_debugfs_pcie_gart_info_init(struct radeon_device *rdev);

57 58 59 60 61 62 63 64 65 66 67 68
void rv370_pcie_gart_tlb_flush(struct radeon_device *rdev)
{
	uint32_t tmp;
	int i;

	/* Workaround HW bug do flush 2 times */
	for (i = 0; i < 2; i++) {
		tmp = RREG32_PCIE(RADEON_PCIE_TX_GART_CNTL);
		WREG32_PCIE(RADEON_PCIE_TX_GART_CNTL, tmp | RADEON_PCIE_TX_GART_INVALIDATE_TLB);
		(void)RREG32_PCIE(RADEON_PCIE_TX_GART_CNTL);
		WREG32_PCIE(RADEON_PCIE_TX_GART_CNTL, tmp);
	}
69
	mb();
70 71
}

72
#define R300_PTE_UNSNOOPED (1 << 0)
73 74 75
#define R300_PTE_WRITEABLE (1 << 2)
#define R300_PTE_READABLE  (1 << 3)

76
void rv370_pcie_gart_set_page(struct radeon_device *rdev, unsigned i,
77
			      uint64_t addr, uint32_t flags)
78
{
79
	void __iomem *ptr = rdev->gart.ptr;
80 81

	addr = (lower_32_bits(addr) >> 8) |
82 83 84 85 86 87 88
		((upper_32_bits(addr) & 0xff) << 24);
	if (flags & RADEON_GART_PAGE_READ)
		addr |= R300_PTE_READABLE;
	if (flags & RADEON_GART_PAGE_WRITE)
		addr |= R300_PTE_WRITEABLE;
	if (!(flags & RADEON_GART_PAGE_SNOOP))
		addr |= R300_PTE_UNSNOOPED;
89 90 91 92 93 94 95
	/* on x86 we want this to be CPU endian, on powerpc
	 * on powerpc without HW swappers, it'll get swapped on way
	 * into VRAM - so no need for cpu_to_le32 on VRAM tables */
	writel(addr, ((void __iomem *)ptr) + (i * 4));
}

int rv370_pcie_gart_init(struct radeon_device *rdev)
96 97 98
{
	int r;

99
	if (rdev->gart.robj) {
J
Joe Perches 已提交
100
		WARN(1, "RV370 PCIE GART already initialized\n");
101 102
		return 0;
	}
103 104
	/* Initialize common gart structure */
	r = radeon_gart_init(rdev);
105
	if (r)
106 107
		return r;
	r = rv370_debugfs_pcie_gart_info_init(rdev);
108
	if (r)
109 110
		DRM_ERROR("Failed to register debugfs file for PCIE gart !\n");
	rdev->gart.table_size = rdev->gart.num_gpu_pages * 4;
111 112
	rdev->asic->gart.tlb_flush = &rv370_pcie_gart_tlb_flush;
	rdev->asic->gart.set_page = &rv370_pcie_gart_set_page;
113 114 115 116 117 118 119 120 121
	return radeon_gart_table_vram_alloc(rdev);
}

int rv370_pcie_gart_enable(struct radeon_device *rdev)
{
	uint32_t table_addr;
	uint32_t tmp;
	int r;

122
	if (rdev->gart.robj == NULL) {
123 124
		dev_err(rdev->dev, "No VRAM object for PCIE GART.\n");
		return -EINVAL;
125
	}
126 127 128
	r = radeon_gart_table_vram_pin(rdev);
	if (r)
		return r;
129 130 131
	/* discard memory request outside of configured range */
	tmp = RADEON_PCIE_TX_GART_UNMAPPED_ACCESS_DISCARD;
	WREG32_PCIE(RADEON_PCIE_TX_GART_CNTL, tmp);
132 133
	WREG32_PCIE(RADEON_PCIE_TX_GART_START_LO, rdev->mc.gtt_start);
	tmp = rdev->mc.gtt_end & ~RADEON_GPU_PAGE_MASK;
134 135 136 137 138 139
	WREG32_PCIE(RADEON_PCIE_TX_GART_END_LO, tmp);
	WREG32_PCIE(RADEON_PCIE_TX_GART_START_HI, 0);
	WREG32_PCIE(RADEON_PCIE_TX_GART_END_HI, 0);
	table_addr = rdev->gart.table_addr;
	WREG32_PCIE(RADEON_PCIE_TX_GART_BASE, table_addr);
	/* FIXME: setup default page */
140
	WREG32_PCIE(RADEON_PCIE_TX_DISCARD_RD_ADDR_LO, rdev->mc.vram_start);
141 142
	WREG32_PCIE(RADEON_PCIE_TX_DISCARD_RD_ADDR_HI, 0);
	/* Clear error */
143
	WREG32_PCIE(RADEON_PCIE_TX_GART_ERROR, 0);
144 145 146 147 148
	tmp = RREG32_PCIE(RADEON_PCIE_TX_GART_CNTL);
	tmp |= RADEON_PCIE_TX_GART_EN;
	tmp |= RADEON_PCIE_TX_GART_UNMAPPED_ACCESS_DISCARD;
	WREG32_PCIE(RADEON_PCIE_TX_GART_CNTL, tmp);
	rv370_pcie_gart_tlb_flush(rdev);
149 150 151
	DRM_INFO("PCIE GART of %uM enabled (table at 0x%016llX).\n",
		 (unsigned)(rdev->mc.gtt_size >> 20),
		 (unsigned long long)table_addr);
152 153 154 155 156 157
	rdev->gart.ready = true;
	return 0;
}

void rv370_pcie_gart_disable(struct radeon_device *rdev)
{
158
	u32 tmp;
159

160 161 162 163
	WREG32_PCIE(RADEON_PCIE_TX_GART_START_LO, 0);
	WREG32_PCIE(RADEON_PCIE_TX_GART_END_LO, 0);
	WREG32_PCIE(RADEON_PCIE_TX_GART_START_HI, 0);
	WREG32_PCIE(RADEON_PCIE_TX_GART_END_HI, 0);
164 165 166
	tmp = RREG32_PCIE(RADEON_PCIE_TX_GART_CNTL);
	tmp |= RADEON_PCIE_TX_GART_UNMAPPED_ACCESS_DISCARD;
	WREG32_PCIE(RADEON_PCIE_TX_GART_CNTL, tmp & ~RADEON_PCIE_TX_GART_EN);
167
	radeon_gart_table_vram_unpin(rdev);
168 169
}

170
void rv370_pcie_gart_fini(struct radeon_device *rdev)
171
{
172
	radeon_gart_fini(rdev);
173 174
	rv370_pcie_gart_disable(rdev);
	radeon_gart_table_vram_free(rdev);
175 176 177 178 179
}

void r300_fence_ring_emit(struct radeon_device *rdev,
			  struct radeon_fence *fence)
{
180
	struct radeon_ring *ring = &rdev->ring[fence->ring];
181

182 183 184
	/* Who ever call radeon_fence_emit should call ring_lock and ask
	 * for enough space (today caller are ib schedule and buffer move) */
	/* Write SC register so SC & US assert idle */
185 186 187 188
	radeon_ring_write(ring, PACKET0(R300_RE_SCISSORS_TL, 0));
	radeon_ring_write(ring, 0);
	radeon_ring_write(ring, PACKET0(R300_RE_SCISSORS_BR, 0));
	radeon_ring_write(ring, 0);
189
	/* Flush 3D cache */
190 191 192 193
	radeon_ring_write(ring, PACKET0(R300_RB3D_DSTCACHE_CTLSTAT, 0));
	radeon_ring_write(ring, R300_RB3D_DC_FLUSH);
	radeon_ring_write(ring, PACKET0(R300_RB3D_ZCACHE_CTLSTAT, 0));
	radeon_ring_write(ring, R300_ZC_FLUSH);
194
	/* Wait until IDLE & CLEAN */
195 196
	radeon_ring_write(ring, PACKET0(RADEON_WAIT_UNTIL, 0));
	radeon_ring_write(ring, (RADEON_WAIT_3D_IDLECLEAN |
197 198
				 RADEON_WAIT_2D_IDLECLEAN |
				 RADEON_WAIT_DMA_GUI_IDLE));
199 200
	radeon_ring_write(ring, PACKET0(RADEON_HOST_PATH_CNTL, 0));
	radeon_ring_write(ring, rdev->config.r300.hdp_cntl |
201
				RADEON_HDP_READ_BUFFER_INVALIDATE);
202 203
	radeon_ring_write(ring, PACKET0(RADEON_HOST_PATH_CNTL, 0));
	radeon_ring_write(ring, rdev->config.r300.hdp_cntl);
204
	/* Emit fence sequence & fire IRQ */
205 206 207 208
	radeon_ring_write(ring, PACKET0(rdev->fence_drv[fence->ring].scratch_reg, 0));
	radeon_ring_write(ring, fence->seq);
	radeon_ring_write(ring, PACKET0(RADEON_GEN_INT_STATUS, 0));
	radeon_ring_write(ring, RADEON_SW_INT_FIRE);
209 210
}

211
void r300_ring_start(struct radeon_device *rdev, struct radeon_ring *ring)
212 213 214 215 216 217
{
	unsigned gb_tile_config;
	int r;

	/* Sub pixel 1/12 so we can have 4K rendering according to doc */
	gb_tile_config = (R300_ENABLE_TILING | R300_TILE_SIZE_16);
218
	switch(rdev->num_gb_pipes) {
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
	case 2:
		gb_tile_config |= R300_PIPE_COUNT_R300;
		break;
	case 3:
		gb_tile_config |= R300_PIPE_COUNT_R420_3P;
		break;
	case 4:
		gb_tile_config |= R300_PIPE_COUNT_R420;
		break;
	case 1:
	default:
		gb_tile_config |= R300_PIPE_COUNT_RV350;
		break;
	}

234
	r = radeon_ring_lock(rdev, ring, 64);
235 236 237
	if (r) {
		return;
	}
238 239
	radeon_ring_write(ring, PACKET0(RADEON_ISYNC_CNTL, 0));
	radeon_ring_write(ring,
240 241 242 243
			  RADEON_ISYNC_ANY2D_IDLE3D |
			  RADEON_ISYNC_ANY3D_IDLE2D |
			  RADEON_ISYNC_WAIT_IDLEGUI |
			  RADEON_ISYNC_CPSCRATCH_IDLEGUI);
244 245 246 247
	radeon_ring_write(ring, PACKET0(R300_GB_TILE_CONFIG, 0));
	radeon_ring_write(ring, gb_tile_config);
	radeon_ring_write(ring, PACKET0(RADEON_WAIT_UNTIL, 0));
	radeon_ring_write(ring,
248 249
			  RADEON_WAIT_2D_IDLECLEAN |
			  RADEON_WAIT_3D_IDLECLEAN);
250 251 252 253 254 255 256 257 258 259 260 261
	radeon_ring_write(ring, PACKET0(R300_DST_PIPE_CONFIG, 0));
	radeon_ring_write(ring, R300_PIPE_AUTO_CONFIG);
	radeon_ring_write(ring, PACKET0(R300_GB_SELECT, 0));
	radeon_ring_write(ring, 0);
	radeon_ring_write(ring, PACKET0(R300_GB_ENABLE, 0));
	radeon_ring_write(ring, 0);
	radeon_ring_write(ring, PACKET0(R300_RB3D_DSTCACHE_CTLSTAT, 0));
	radeon_ring_write(ring, R300_RB3D_DC_FLUSH | R300_RB3D_DC_FREE);
	radeon_ring_write(ring, PACKET0(R300_RB3D_ZCACHE_CTLSTAT, 0));
	radeon_ring_write(ring, R300_ZC_FLUSH | R300_ZC_FREE);
	radeon_ring_write(ring, PACKET0(RADEON_WAIT_UNTIL, 0));
	radeon_ring_write(ring,
262 263
			  RADEON_WAIT_2D_IDLECLEAN |
			  RADEON_WAIT_3D_IDLECLEAN);
264 265 266 267 268 269 270 271
	radeon_ring_write(ring, PACKET0(R300_GB_AA_CONFIG, 0));
	radeon_ring_write(ring, 0);
	radeon_ring_write(ring, PACKET0(R300_RB3D_DSTCACHE_CTLSTAT, 0));
	radeon_ring_write(ring, R300_RB3D_DC_FLUSH | R300_RB3D_DC_FREE);
	radeon_ring_write(ring, PACKET0(R300_RB3D_ZCACHE_CTLSTAT, 0));
	radeon_ring_write(ring, R300_ZC_FLUSH | R300_ZC_FREE);
	radeon_ring_write(ring, PACKET0(R300_GB_MSPOS0, 0));
	radeon_ring_write(ring,
272 273 274 275 276 277 278 279
			  ((6 << R300_MS_X0_SHIFT) |
			   (6 << R300_MS_Y0_SHIFT) |
			   (6 << R300_MS_X1_SHIFT) |
			   (6 << R300_MS_Y1_SHIFT) |
			   (6 << R300_MS_X2_SHIFT) |
			   (6 << R300_MS_Y2_SHIFT) |
			   (6 << R300_MSBD0_Y_SHIFT) |
			   (6 << R300_MSBD0_X_SHIFT)));
280 281
	radeon_ring_write(ring, PACKET0(R300_GB_MSPOS1, 0));
	radeon_ring_write(ring,
282 283 284 285 286 287 288
			  ((6 << R300_MS_X3_SHIFT) |
			   (6 << R300_MS_Y3_SHIFT) |
			   (6 << R300_MS_X4_SHIFT) |
			   (6 << R300_MS_Y4_SHIFT) |
			   (6 << R300_MS_X5_SHIFT) |
			   (6 << R300_MS_Y5_SHIFT) |
			   (6 << R300_MSBD1_SHIFT)));
289 290 291 292
	radeon_ring_write(ring, PACKET0(R300_GA_ENHANCE, 0));
	radeon_ring_write(ring, R300_GA_DEADLOCK_CNTL | R300_GA_FASTSYNC_CNTL);
	radeon_ring_write(ring, PACKET0(R300_GA_POLY_MODE, 0));
	radeon_ring_write(ring,
293
			  R300_FRONT_PTYPE_TRIANGE | R300_BACK_PTYPE_TRIANGE);
294 295
	radeon_ring_write(ring, PACKET0(R300_GA_ROUND_MODE, 0));
	radeon_ring_write(ring,
296 297
			  R300_GEOMETRY_ROUND_NEAREST |
			  R300_COLOR_ROUND_NEAREST);
298
	radeon_ring_unlock_commit(rdev, ring, false);
299 300
}

301
static void r300_errata(struct radeon_device *rdev)
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
{
	rdev->pll_errata = 0;

	if (rdev->family == CHIP_R300 &&
	    (RREG32(RADEON_CONFIG_CNTL) & RADEON_CFG_ATI_REV_ID_MASK) == RADEON_CFG_ATI_REV_A11) {
		rdev->pll_errata |= CHIP_ERRATA_R300_CG;
	}
}

int r300_mc_wait_for_idle(struct radeon_device *rdev)
{
	unsigned i;
	uint32_t tmp;

	for (i = 0; i < rdev->usec_timeout; i++) {
		/* read MC_STATUS */
318 319
		tmp = RREG32(RADEON_MC_STATUS);
		if (tmp & R300_MC_IDLE) {
320 321 322 323 324 325 326
			return 0;
		}
		DRM_UDELAY(1);
	}
	return -1;
}

327
static void r300_gpu_init(struct radeon_device *rdev)
328 329 330
{
	uint32_t gb_tile_config, tmp;

331
	if ((rdev->family == CHIP_R300 && rdev->pdev->device != 0x4144) ||
332
	    (rdev->family == CHIP_R350 && rdev->pdev->device != 0x4148)) {
333 334 335
		/* r300,r350 */
		rdev->num_gb_pipes = 2;
	} else {
336
		/* rv350,rv370,rv380,r300 AD, r350 AH */
337 338
		rdev->num_gb_pipes = 1;
	}
339
	rdev->num_z_pipes = 1;
340 341 342 343 344 345 346 347 348 349 350 351
	gb_tile_config = (R300_ENABLE_TILING | R300_TILE_SIZE_16);
	switch (rdev->num_gb_pipes) {
	case 2:
		gb_tile_config |= R300_PIPE_COUNT_R300;
		break;
	case 3:
		gb_tile_config |= R300_PIPE_COUNT_R420_3P;
		break;
	case 4:
		gb_tile_config |= R300_PIPE_COUNT_R420;
		break;
	default:
352
	case 1:
353 354 355 356 357 358 359 360 361 362
		gb_tile_config |= R300_PIPE_COUNT_RV350;
		break;
	}
	WREG32(R300_GB_TILE_CONFIG, gb_tile_config);

	if (r100_gui_wait_for_idle(rdev)) {
		printk(KERN_WARNING "Failed to wait GUI idle while "
		       "programming pipes. Bad things might happen.\n");
	}

363 364
	tmp = RREG32(R300_DST_PIPE_CONFIG);
	WREG32(R300_DST_PIPE_CONFIG, tmp | R300_PIPE_AUTO_CONFIG);
365 366 367 368 369 370 371 372 373 374 375 376 377

	WREG32(R300_RB2D_DSTCACHE_MODE,
	       R300_DC_AUTOFLUSH_ENABLE |
	       R300_DC_DC_DISABLE_IGNORE_PE);

	if (r100_gui_wait_for_idle(rdev)) {
		printk(KERN_WARNING "Failed to wait GUI idle while "
		       "programming pipes. Bad things might happen.\n");
	}
	if (r300_mc_wait_for_idle(rdev)) {
		printk(KERN_WARNING "Failed to wait MC idle while "
		       "programming pipes. Bad things might happen.\n");
	}
378 379
	DRM_INFO("radeon: %d quad pipes, %d Z pipes initialized.\n",
		 rdev->num_gb_pipes, rdev->num_z_pipes);
380 381
}

382
int r300_asic_reset(struct radeon_device *rdev)
383
{
384 385
	struct r100_mc_save save;
	u32 status, tmp;
386
	int ret = 0;
387

388 389 390
	status = RREG32(R_000E40_RBBM_STATUS);
	if (!G_000E40_GUI_ACTIVE(status)) {
		return 0;
391
	}
392
	r100_mc_stop(rdev, &save);
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
	status = RREG32(R_000E40_RBBM_STATUS);
	dev_info(rdev->dev, "(%s:%d) RBBM_STATUS=0x%08X\n", __func__, __LINE__, status);
	/* stop CP */
	WREG32(RADEON_CP_CSQ_CNTL, 0);
	tmp = RREG32(RADEON_CP_RB_CNTL);
	WREG32(RADEON_CP_RB_CNTL, tmp | RADEON_RB_RPTR_WR_ENA);
	WREG32(RADEON_CP_RB_RPTR_WR, 0);
	WREG32(RADEON_CP_RB_WPTR, 0);
	WREG32(RADEON_CP_RB_CNTL, tmp);
	/* save PCI state */
	pci_save_state(rdev->pdev);
	/* disable bus mastering */
	r100_bm_disable(rdev);
	WREG32(R_0000F0_RBBM_SOFT_RESET, S_0000F0_SOFT_RESET_VAP(1) |
					S_0000F0_SOFT_RESET_GA(1));
	RREG32(R_0000F0_RBBM_SOFT_RESET);
	mdelay(500);
	WREG32(R_0000F0_RBBM_SOFT_RESET, 0);
	mdelay(1);
	status = RREG32(R_000E40_RBBM_STATUS);
	dev_info(rdev->dev, "(%s:%d) RBBM_STATUS=0x%08X\n", __func__, __LINE__, status);
	/* resetting the CP seems to be problematic sometimes it end up
L
Lucas De Marchi 已提交
415
	 * hard locking the computer, but it's necessary for successful
416 417 418 419 420 421 422 423 424 425 426 427 428
	 * reset more test & playing is needed on R3XX/R4XX to find a
	 * reliable (if any solution)
	 */
	WREG32(R_0000F0_RBBM_SOFT_RESET, S_0000F0_SOFT_RESET_CP(1));
	RREG32(R_0000F0_RBBM_SOFT_RESET);
	mdelay(500);
	WREG32(R_0000F0_RBBM_SOFT_RESET, 0);
	mdelay(1);
	status = RREG32(R_000E40_RBBM_STATUS);
	dev_info(rdev->dev, "(%s:%d) RBBM_STATUS=0x%08X\n", __func__, __LINE__, status);
	/* restore PCI & busmastering */
	pci_restore_state(rdev->pdev);
	r100_enable_bm(rdev);
429
	/* Check if GPU is idle */
430 431
	if (G_000E40_GA_BUSY(status) || G_000E40_VAP_BUSY(status)) {
		dev_err(rdev->dev, "failed to reset GPU\n");
432 433 434
		ret = -1;
	} else
		dev_info(rdev->dev, "GPU reset succeed\n");
435
	r100_mc_resume(rdev, &save);
436
	return ret;
437 438 439 440 441
}

/*
 * r300,r350,rv350,rv380 VRAM info
 */
442
void r300_mc_init(struct radeon_device *rdev)
443
{
444 445
	u64 base;
	u32 tmp;
446 447 448 449

	/* DDR for all card after R300 & IGP */
	rdev->mc.vram_is_ddr = true;
	tmp = RREG32(RADEON_MEM_CNTL);
450 451 452 453 454 455
	tmp &= R300_MEM_NUM_CHANNELS_MASK;
	switch (tmp) {
	case 0: rdev->mc.vram_width = 64; break;
	case 1: rdev->mc.vram_width = 128; break;
	case 2: rdev->mc.vram_width = 256; break;
	default:  rdev->mc.vram_width = 128; break;
456
	}
457
	r100_vram_init_sizes(rdev);
458 459 460 461
	base = rdev->mc.aper_base;
	if (rdev->flags & RADEON_IS_IGP)
		base = (RREG32(RADEON_NB_TOM) & 0xffff) << 16;
	radeon_vram_location(rdev, &rdev->mc, base);
462
	rdev->mc.gtt_base_align = 0;
463 464
	if (!(rdev->flags & RADEON_IS_AGP))
		radeon_gtt_location(rdev, &rdev->mc);
465
	radeon_update_bandwidth_info(rdev);
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
}

void rv370_set_pcie_lanes(struct radeon_device *rdev, int lanes)
{
	uint32_t link_width_cntl, mask;

	if (rdev->flags & RADEON_IS_IGP)
		return;

	if (!(rdev->flags & RADEON_IS_PCIE))
		return;

	/* FIXME wait for idle */

	switch (lanes) {
	case 0:
		mask = RADEON_PCIE_LC_LINK_WIDTH_X0;
		break;
	case 1:
		mask = RADEON_PCIE_LC_LINK_WIDTH_X1;
		break;
	case 2:
		mask = RADEON_PCIE_LC_LINK_WIDTH_X2;
		break;
	case 4:
		mask = RADEON_PCIE_LC_LINK_WIDTH_X4;
		break;
	case 8:
		mask = RADEON_PCIE_LC_LINK_WIDTH_X8;
		break;
	case 12:
		mask = RADEON_PCIE_LC_LINK_WIDTH_X12;
		break;
	case 16:
	default:
		mask = RADEON_PCIE_LC_LINK_WIDTH_X16;
		break;
	}

	link_width_cntl = RREG32_PCIE(RADEON_PCIE_LC_LINK_WIDTH_CNTL);

	if ((link_width_cntl & RADEON_PCIE_LC_LINK_WIDTH_RD_MASK) ==
	    (mask << RADEON_PCIE_LC_LINK_WIDTH_RD_SHIFT))
		return;

	link_width_cntl &= ~(RADEON_PCIE_LC_LINK_WIDTH_MASK |
			     RADEON_PCIE_LC_RECONFIG_NOW |
			     RADEON_PCIE_LC_RECONFIG_LATER |
			     RADEON_PCIE_LC_SHORT_RECONFIG_EN);
	link_width_cntl |= mask;
	WREG32_PCIE(RADEON_PCIE_LC_LINK_WIDTH_CNTL, link_width_cntl);
	WREG32_PCIE(RADEON_PCIE_LC_LINK_WIDTH_CNTL, (link_width_cntl |
						     RADEON_PCIE_LC_RECONFIG_NOW));

	/* wait for lane set to complete */
	link_width_cntl = RREG32_PCIE(RADEON_PCIE_LC_LINK_WIDTH_CNTL);
	while (link_width_cntl == 0xffffffff)
		link_width_cntl = RREG32_PCIE(RADEON_PCIE_LC_LINK_WIDTH_CNTL);

}

527 528 529 530 531 532 533 534 535 536 537 538
int rv370_get_pcie_lanes(struct radeon_device *rdev)
{
	u32 link_width_cntl;

	if (rdev->flags & RADEON_IS_IGP)
		return 0;

	if (!(rdev->flags & RADEON_IS_PCIE))
		return 0;

	/* FIXME wait for idle */

539
	link_width_cntl = RREG32_PCIE(RADEON_PCIE_LC_LINK_WIDTH_CNTL);
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557

	switch ((link_width_cntl & RADEON_PCIE_LC_LINK_WIDTH_RD_MASK) >> RADEON_PCIE_LC_LINK_WIDTH_RD_SHIFT) {
	case RADEON_PCIE_LC_LINK_WIDTH_X0:
		return 0;
	case RADEON_PCIE_LC_LINK_WIDTH_X1:
		return 1;
	case RADEON_PCIE_LC_LINK_WIDTH_X2:
		return 2;
	case RADEON_PCIE_LC_LINK_WIDTH_X4:
		return 4;
	case RADEON_PCIE_LC_LINK_WIDTH_X8:
		return 8;
	case RADEON_PCIE_LC_LINK_WIDTH_X16:
	default:
		return 16;
	}
}

558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
#if defined(CONFIG_DEBUG_FS)
static int rv370_debugfs_pcie_gart_info(struct seq_file *m, void *data)
{
	struct drm_info_node *node = (struct drm_info_node *) m->private;
	struct drm_device *dev = node->minor->dev;
	struct radeon_device *rdev = dev->dev_private;
	uint32_t tmp;

	tmp = RREG32_PCIE(RADEON_PCIE_TX_GART_CNTL);
	seq_printf(m, "PCIE_TX_GART_CNTL 0x%08x\n", tmp);
	tmp = RREG32_PCIE(RADEON_PCIE_TX_GART_BASE);
	seq_printf(m, "PCIE_TX_GART_BASE 0x%08x\n", tmp);
	tmp = RREG32_PCIE(RADEON_PCIE_TX_GART_START_LO);
	seq_printf(m, "PCIE_TX_GART_START_LO 0x%08x\n", tmp);
	tmp = RREG32_PCIE(RADEON_PCIE_TX_GART_START_HI);
	seq_printf(m, "PCIE_TX_GART_START_HI 0x%08x\n", tmp);
	tmp = RREG32_PCIE(RADEON_PCIE_TX_GART_END_LO);
	seq_printf(m, "PCIE_TX_GART_END_LO 0x%08x\n", tmp);
	tmp = RREG32_PCIE(RADEON_PCIE_TX_GART_END_HI);
	seq_printf(m, "PCIE_TX_GART_END_HI 0x%08x\n", tmp);
	tmp = RREG32_PCIE(RADEON_PCIE_TX_GART_ERROR);
	seq_printf(m, "PCIE_TX_GART_ERROR 0x%08x\n", tmp);
	return 0;
}

static struct drm_info_list rv370_pcie_gart_info_list[] = {
	{"rv370_pcie_gart_info", rv370_debugfs_pcie_gart_info, 0, NULL},
};
#endif

588
static int rv370_debugfs_pcie_gart_info_init(struct radeon_device *rdev)
589 590 591 592 593 594 595 596 597 598 599 600
{
#if defined(CONFIG_DEBUG_FS)
	return radeon_debugfs_add_files(rdev, rv370_pcie_gart_info_list, 1);
#else
	return 0;
#endif
}

static int r300_packet0_check(struct radeon_cs_parser *p,
		struct radeon_cs_packet *pkt,
		unsigned idx, unsigned reg)
{
601
	struct radeon_bo_list *reloc;
602
	struct r100_cs_track *track;
603
	volatile uint32_t *ib;
604
	uint32_t tmp, tile_flags = 0;
605 606
	unsigned i;
	int r;
607
	u32 idx_value;
608

609
	ib = p->ib.ptr;
610
	track = (struct r100_cs_track *)p->track;
611 612
	idx_value = radeon_get_ib_value(p, idx);

613
	switch(reg) {
614 615 616 617 618 619
	case AVIVO_D1MODE_VLINE_START_END:
	case RADEON_CRTC_GUI_TRIG_VLINE:
		r = r100_cs_packet_parse_vline(p);
		if (r) {
			DRM_ERROR("No reloc for ib[%d]=0x%04X\n",
					idx, reg);
620
			radeon_cs_dump_packet(p, pkt);
621 622 623
			return r;
		}
		break;
624 625
	case RADEON_DST_PITCH_OFFSET:
	case RADEON_SRC_PITCH_OFFSET:
626 627
		r = r100_reloc_pitch_offset(p, pkt, idx, reg);
		if (r)
628 629 630 631 632 633 634
			return r;
		break;
	case R300_RB3D_COLOROFFSET0:
	case R300_RB3D_COLOROFFSET1:
	case R300_RB3D_COLOROFFSET2:
	case R300_RB3D_COLOROFFSET3:
		i = (reg - R300_RB3D_COLOROFFSET0) >> 2;
635
		r = radeon_cs_packet_next_reloc(p, &reloc, 0);
636 637 638
		if (r) {
			DRM_ERROR("No reloc for ib[%d]=0x%04X\n",
					idx, reg);
639
			radeon_cs_dump_packet(p, pkt);
640 641 642
			return r;
		}
		track->cb[i].robj = reloc->robj;
643
		track->cb[i].offset = idx_value;
644
		track->cb_dirty = true;
645
		ib[idx] = idx_value + ((u32)reloc->gpu_offset);
646 647
		break;
	case R300_ZB_DEPTHOFFSET:
648
		r = radeon_cs_packet_next_reloc(p, &reloc, 0);
649 650 651
		if (r) {
			DRM_ERROR("No reloc for ib[%d]=0x%04X\n",
					idx, reg);
652
			radeon_cs_dump_packet(p, pkt);
653 654 655
			return r;
		}
		track->zb.robj = reloc->robj;
656
		track->zb.offset = idx_value;
657
		track->zb_dirty = true;
658
		ib[idx] = idx_value + ((u32)reloc->gpu_offset);
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675
		break;
	case R300_TX_OFFSET_0:
	case R300_TX_OFFSET_0+4:
	case R300_TX_OFFSET_0+8:
	case R300_TX_OFFSET_0+12:
	case R300_TX_OFFSET_0+16:
	case R300_TX_OFFSET_0+20:
	case R300_TX_OFFSET_0+24:
	case R300_TX_OFFSET_0+28:
	case R300_TX_OFFSET_0+32:
	case R300_TX_OFFSET_0+36:
	case R300_TX_OFFSET_0+40:
	case R300_TX_OFFSET_0+44:
	case R300_TX_OFFSET_0+48:
	case R300_TX_OFFSET_0+52:
	case R300_TX_OFFSET_0+56:
	case R300_TX_OFFSET_0+60:
676
		i = (reg - R300_TX_OFFSET_0) >> 2;
677
		r = radeon_cs_packet_next_reloc(p, &reloc, 0);
678 679 680
		if (r) {
			DRM_ERROR("No reloc for ib[%d]=0x%04X\n",
					idx, reg);
681
			radeon_cs_dump_packet(p, pkt);
682 683
			return r;
		}
684

685
		if (p->cs_flags & RADEON_CS_KEEP_TILING_FLAGS) {
686
			ib[idx] = (idx_value & 31) | /* keep the 1st 5 bits */
687
				  ((idx_value & ~31) + (u32)reloc->gpu_offset);
688
		} else {
689
			if (reloc->tiling_flags & RADEON_TILING_MACRO)
690
				tile_flags |= R300_TXO_MACRO_TILE;
691
			if (reloc->tiling_flags & RADEON_TILING_MICRO)
692
				tile_flags |= R300_TXO_MICRO_TILE;
693
			else if (reloc->tiling_flags & RADEON_TILING_MICRO_SQUARE)
694 695
				tile_flags |= R300_TXO_MICRO_TILE_SQUARE;

696
			tmp = idx_value + ((u32)reloc->gpu_offset);
697 698 699
			tmp |= tile_flags;
			ib[idx] = tmp;
		}
700
		track->textures[i].robj = reloc->robj;
701
		track->tex_dirty = true;
702 703
		break;
	/* Tracked registers */
704 705
	case 0x2084:
		/* VAP_VF_CNTL */
706
		track->vap_vf_cntl = idx_value;
707 708 709
		break;
	case 0x20B4:
		/* VAP_VTX_SIZE */
710
		track->vtx_size = idx_value & 0x7F;
711 712 713
		break;
	case 0x2134:
		/* VAP_VF_MAX_VTX_INDX */
714
		track->max_indx = idx_value & 0x00FFFFFFUL;
715
		break;
716 717 718 719 720 721
	case 0x2088:
		/* VAP_ALT_NUM_VERTICES - only valid on r500 */
		if (p->rdev->family < CHIP_RV515)
			goto fail;
		track->vap_alt_nverts = idx_value & 0xFFFFFF;
		break;
722 723
	case 0x43E4:
		/* SC_SCISSOR1 */
724
		track->maxy = ((idx_value >> 13) & 0x1FFF) + 1;
725 726 727
		if (p->rdev->family < CHIP_RV515) {
			track->maxy -= 1440;
		}
728 729
		track->cb_dirty = true;
		track->zb_dirty = true;
730 731 732
		break;
	case 0x4E00:
		/* RB3D_CCTL */
733 734 735 736 737
		if ((idx_value & (1 << 10)) && /* CMASK_ENABLE */
		    p->rdev->cmask_filp != p->filp) {
			DRM_ERROR("Invalid RB3D_CCTL: Cannot enable CMASK.\n");
			return -EINVAL;
		}
738
		track->num_cb = ((idx_value >> 5) & 0x3) + 1;
739
		track->cb_dirty = true;
740 741 742 743 744 745 746 747 748
		break;
	case 0x4E38:
	case 0x4E3C:
	case 0x4E40:
	case 0x4E44:
		/* RB3D_COLORPITCH0 */
		/* RB3D_COLORPITCH1 */
		/* RB3D_COLORPITCH2 */
		/* RB3D_COLORPITCH3 */
749
		if (!(p->cs_flags & RADEON_CS_KEEP_TILING_FLAGS)) {
750
			r = radeon_cs_packet_next_reloc(p, &reloc, 0);
751 752 753
			if (r) {
				DRM_ERROR("No reloc for ib[%d]=0x%04X\n",
					  idx, reg);
754
				radeon_cs_dump_packet(p, pkt);
755 756
				return r;
			}
757

758
			if (reloc->tiling_flags & RADEON_TILING_MACRO)
759
				tile_flags |= R300_COLOR_TILE_ENABLE;
760
			if (reloc->tiling_flags & RADEON_TILING_MICRO)
761
				tile_flags |= R300_COLOR_MICROTILE_ENABLE;
762
			else if (reloc->tiling_flags & RADEON_TILING_MICRO_SQUARE)
763
				tile_flags |= R300_COLOR_MICROTILE_SQUARE_ENABLE;
764

765 766 767 768
			tmp = idx_value & ~(0x7 << 16);
			tmp |= tile_flags;
			ib[idx] = tmp;
		}
769
		i = (reg - 0x4E38) >> 2;
770 771
		track->cb[i].pitch = idx_value & 0x3FFE;
		switch (((idx_value >> 21) & 0xF)) {
772 773 774 775 776 777 778 779 780 781 782
		case 9:
		case 11:
		case 12:
			track->cb[i].cpp = 1;
			break;
		case 3:
		case 4:
		case 13:
		case 15:
			track->cb[i].cpp = 2;
			break;
783 784 785 786 787 788 789
		case 5:
			if (p->rdev->family < CHIP_RV515) {
				DRM_ERROR("Invalid color buffer format (%d)!\n",
					  ((idx_value >> 21) & 0xF));
				return -EINVAL;
			}
			/* Pass through. */
790 791 792 793 794 795 796 797 798 799 800
		case 6:
			track->cb[i].cpp = 4;
			break;
		case 10:
			track->cb[i].cpp = 8;
			break;
		case 7:
			track->cb[i].cpp = 16;
			break;
		default:
			DRM_ERROR("Invalid color buffer format (%d) !\n",
801
				  ((idx_value >> 21) & 0xF));
802 803
			return -EINVAL;
		}
804
		track->cb_dirty = true;
805 806 807
		break;
	case 0x4F00:
		/* ZB_CNTL */
808
		if (idx_value & 2) {
809 810 811 812
			track->z_enabled = true;
		} else {
			track->z_enabled = false;
		}
813
		track->zb_dirty = true;
814 815 816
		break;
	case 0x4F10:
		/* ZB_FORMAT */
817
		switch ((idx_value & 0xF)) {
818 819 820 821 822 823 824 825 826
		case 0:
		case 1:
			track->zb.cpp = 2;
			break;
		case 2:
			track->zb.cpp = 4;
			break;
		default:
			DRM_ERROR("Invalid z buffer format (%d) !\n",
827
				  (idx_value & 0xF));
828 829
			return -EINVAL;
		}
830
		track->zb_dirty = true;
831 832 833
		break;
	case 0x4F24:
		/* ZB_DEPTHPITCH */
834
		if (!(p->cs_flags & RADEON_CS_KEEP_TILING_FLAGS)) {
835
			r = radeon_cs_packet_next_reloc(p, &reloc, 0);
836 837 838
			if (r) {
				DRM_ERROR("No reloc for ib[%d]=0x%04X\n",
					  idx, reg);
839
				radeon_cs_dump_packet(p, pkt);
840 841
				return r;
			}
842

843
			if (reloc->tiling_flags & RADEON_TILING_MACRO)
844
				tile_flags |= R300_DEPTHMACROTILE_ENABLE;
845
			if (reloc->tiling_flags & RADEON_TILING_MICRO)
846
				tile_flags |= R300_DEPTHMICROTILE_TILED;
847
			else if (reloc->tiling_flags & RADEON_TILING_MICRO_SQUARE)
848
				tile_flags |= R300_DEPTHMICROTILE_TILED_SQUARE;
849

850 851 852 853
			tmp = idx_value & ~(0x7 << 16);
			tmp |= tile_flags;
			ib[idx] = tmp;
		}
854
		track->zb.pitch = idx_value & 0x3FFC;
855
		track->zb_dirty = true;
856
		break;
857
	case 0x4104:
858
		/* TX_ENABLE */
859 860 861
		for (i = 0; i < 16; i++) {
			bool enabled;

862
			enabled = !!(idx_value & (1 << i));
863 864
			track->textures[i].enabled = enabled;
		}
865
		track->tex_dirty = true;
866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884
		break;
	case 0x44C0:
	case 0x44C4:
	case 0x44C8:
	case 0x44CC:
	case 0x44D0:
	case 0x44D4:
	case 0x44D8:
	case 0x44DC:
	case 0x44E0:
	case 0x44E4:
	case 0x44E8:
	case 0x44EC:
	case 0x44F0:
	case 0x44F4:
	case 0x44F8:
	case 0x44FC:
		/* TX_FORMAT1_[0-15] */
		i = (reg - 0x44C0) >> 2;
885
		tmp = (idx_value >> 25) & 0x3;
886
		track->textures[i].tex_coord_type = tmp;
887
		switch ((idx_value & 0x1F)) {
888 889 890
		case R300_TX_FORMAT_X8:
		case R300_TX_FORMAT_Y4X4:
		case R300_TX_FORMAT_Z3Y3X2:
891
			track->textures[i].cpp = 1;
892
			track->textures[i].compress_format = R100_TRACK_COMP_NONE;
893
			break;
894
		case R300_TX_FORMAT_X16:
895
		case R300_TX_FORMAT_FL_I16:
896 897 898 899 900 901 902 903
		case R300_TX_FORMAT_Y8X8:
		case R300_TX_FORMAT_Z5Y6X5:
		case R300_TX_FORMAT_Z6Y5X5:
		case R300_TX_FORMAT_W4Z4Y4X4:
		case R300_TX_FORMAT_W1Z5Y5X5:
		case R300_TX_FORMAT_D3DMFT_CxV8U8:
		case R300_TX_FORMAT_B8G8_B8G8:
		case R300_TX_FORMAT_G8R8_G8B8:
904
			track->textures[i].cpp = 2;
905
			track->textures[i].compress_format = R100_TRACK_COMP_NONE;
906
			break;
907
		case R300_TX_FORMAT_Y16X16:
908
		case R300_TX_FORMAT_FL_I16A16:
909 910 911 912 913 914 915
		case R300_TX_FORMAT_Z11Y11X10:
		case R300_TX_FORMAT_Z10Y11X11:
		case R300_TX_FORMAT_W8Z8Y8X8:
		case R300_TX_FORMAT_W2Z10Y10X10:
		case 0x17:
		case R300_TX_FORMAT_FL_I32:
		case 0x1e:
916
			track->textures[i].cpp = 4;
917
			track->textures[i].compress_format = R100_TRACK_COMP_NONE;
918
			break;
919 920 921
		case R300_TX_FORMAT_W16Z16Y16X16:
		case R300_TX_FORMAT_FL_R16G16B16A16:
		case R300_TX_FORMAT_FL_I32A32:
922
			track->textures[i].cpp = 8;
923
			track->textures[i].compress_format = R100_TRACK_COMP_NONE;
924
			break;
925
		case R300_TX_FORMAT_FL_R32G32B32A32:
926
			track->textures[i].cpp = 16;
927
			track->textures[i].compress_format = R100_TRACK_COMP_NONE;
928
			break;
929 930 931 932
		case R300_TX_FORMAT_DXT1:
			track->textures[i].cpp = 1;
			track->textures[i].compress_format = R100_TRACK_COMP_DXT1;
			break;
933 934 935 936 937 938 939 940
		case R300_TX_FORMAT_ATI2N:
			if (p->rdev->family < CHIP_R420) {
				DRM_ERROR("Invalid texture format %u\n",
					  (idx_value & 0x1F));
				return -EINVAL;
			}
			/* The same rules apply as for DXT3/5. */
			/* Pass through. */
941 942 943 944 945
		case R300_TX_FORMAT_DXT3:
		case R300_TX_FORMAT_DXT5:
			track->textures[i].cpp = 1;
			track->textures[i].compress_format = R100_TRACK_COMP_DXT35;
			break;
946 947
		default:
			DRM_ERROR("Invalid texture format %u\n",
948
				  (idx_value & 0x1F));
949 950
			return -EINVAL;
		}
951
		track->tex_dirty = true;
952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970
		break;
	case 0x4400:
	case 0x4404:
	case 0x4408:
	case 0x440C:
	case 0x4410:
	case 0x4414:
	case 0x4418:
	case 0x441C:
	case 0x4420:
	case 0x4424:
	case 0x4428:
	case 0x442C:
	case 0x4430:
	case 0x4434:
	case 0x4438:
	case 0x443C:
		/* TX_FILTER0_[0-15] */
		i = (reg - 0x4400) >> 2;
971
		tmp = idx_value & 0x7;
972 973 974
		if (tmp == 2 || tmp == 4 || tmp == 6) {
			track->textures[i].roundup_w = false;
		}
975
		tmp = (idx_value >> 3) & 0x7;
976 977 978
		if (tmp == 2 || tmp == 4 || tmp == 6) {
			track->textures[i].roundup_h = false;
		}
979
		track->tex_dirty = true;
980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998
		break;
	case 0x4500:
	case 0x4504:
	case 0x4508:
	case 0x450C:
	case 0x4510:
	case 0x4514:
	case 0x4518:
	case 0x451C:
	case 0x4520:
	case 0x4524:
	case 0x4528:
	case 0x452C:
	case 0x4530:
	case 0x4534:
	case 0x4538:
	case 0x453C:
		/* TX_FORMAT2_[0-15] */
		i = (reg - 0x4500) >> 2;
999
		tmp = idx_value & 0x3FFF;
1000 1001
		track->textures[i].pitch = tmp + 1;
		if (p->rdev->family >= CHIP_RV515) {
1002
			tmp = ((idx_value >> 15) & 1) << 11;
1003
			track->textures[i].width_11 = tmp;
1004
			tmp = ((idx_value >> 16) & 1) << 11;
1005
			track->textures[i].height_11 = tmp;
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015

			/* ATI1N */
			if (idx_value & (1 << 14)) {
				/* The same rules apply as for DXT1. */
				track->textures[i].compress_format =
					R100_TRACK_COMP_DXT1;
			}
		} else if (idx_value & (1 << 14)) {
			DRM_ERROR("Forbidden bit TXFORMAT_MSB\n");
			return -EINVAL;
1016
		}
1017
		track->tex_dirty = true;
1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
		break;
	case 0x4480:
	case 0x4484:
	case 0x4488:
	case 0x448C:
	case 0x4490:
	case 0x4494:
	case 0x4498:
	case 0x449C:
	case 0x44A0:
	case 0x44A4:
	case 0x44A8:
	case 0x44AC:
	case 0x44B0:
	case 0x44B4:
	case 0x44B8:
	case 0x44BC:
		/* TX_FORMAT0_[0-15] */
		i = (reg - 0x4480) >> 2;
1037
		tmp = idx_value & 0x7FF;
1038
		track->textures[i].width = tmp + 1;
1039
		tmp = (idx_value >> 11) & 0x7FF;
1040
		track->textures[i].height = tmp + 1;
1041
		tmp = (idx_value >> 26) & 0xF;
1042
		track->textures[i].num_levels = tmp;
1043
		tmp = idx_value & (1 << 31);
1044
		track->textures[i].use_pitch = !!tmp;
1045
		tmp = (idx_value >> 22) & 0xF;
1046
		track->textures[i].txdepth = tmp;
1047
		track->tex_dirty = true;
1048
		break;
1049
	case R300_ZB_ZPASS_ADDR:
1050
		r = radeon_cs_packet_next_reloc(p, &reloc, 0);
1051 1052 1053
		if (r) {
			DRM_ERROR("No reloc for ib[%d]=0x%04X\n",
					idx, reg);
1054
			radeon_cs_dump_packet(p, pkt);
1055 1056
			return r;
		}
1057
		ib[idx] = idx_value + ((u32)reloc->gpu_offset);
1058
		break;
1059 1060 1061
	case 0x4e0c:
		/* RB3D_COLOR_CHANNEL_MASK */
		track->color_channel_mask = idx_value;
1062
		track->cb_dirty = true;
1063
		break;
1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
	case 0x43a4:
		/* SC_HYPERZ_EN */
		/* r300c emits this register - we need to disable hyperz for it
		 * without complaining */
		if (p->rdev->hyperz_filp != p->filp) {
			if (idx_value & 0x1)
				ib[idx] = idx_value & ~1;
		}
		break;
	case 0x4f1c:
1074
		/* ZB_BW_CNTL */
1075
		track->zb_cb_clear = !!(idx_value & (1 << 5));
1076 1077
		track->cb_dirty = true;
		track->zb_dirty = true;
1078 1079 1080 1081 1082 1083 1084
		if (p->rdev->hyperz_filp != p->filp) {
			if (idx_value & (R300_HIZ_ENABLE |
					 R300_RD_COMP_ENABLE |
					 R300_WR_COMP_ENABLE |
					 R300_FAST_FILL_ENABLE))
				goto fail;
		}
1085 1086 1087 1088
		break;
	case 0x4e04:
		/* RB3D_BLENDCNTL */
		track->blend_read_enable = !!(idx_value & (1 << 2));
1089
		track->cb_dirty = true;
1090
		break;
1091
	case R300_RB3D_AARESOLVE_OFFSET:
1092
		r = radeon_cs_packet_next_reloc(p, &reloc, 0);
1093 1094 1095
		if (r) {
			DRM_ERROR("No reloc for ib[%d]=0x%04X\n",
				  idx, reg);
1096
			radeon_cs_dump_packet(p, pkt);
1097 1098 1099 1100 1101
			return r;
		}
		track->aa.robj = reloc->robj;
		track->aa.offset = idx_value;
		track->aa_dirty = true;
1102
		ib[idx] = idx_value + ((u32)reloc->gpu_offset);
1103 1104 1105 1106 1107 1108 1109 1110 1111
		break;
	case R300_RB3D_AARESOLVE_PITCH:
		track->aa.pitch = idx_value & 0x3FFE;
		track->aa_dirty = true;
		break;
	case R300_RB3D_AARESOLVE_CTL:
		track->aaresolve = idx_value & 0x1;
		track->aa_dirty = true;
		break;
1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126
	case 0x4f30: /* ZB_MASK_OFFSET */
	case 0x4f34: /* ZB_ZMASK_PITCH */
	case 0x4f44: /* ZB_HIZ_OFFSET */
	case 0x4f54: /* ZB_HIZ_PITCH */
		if (idx_value && (p->rdev->hyperz_filp != p->filp))
			goto fail;
		break;
	case 0x4028:
		if (idx_value && (p->rdev->hyperz_filp != p->filp))
			goto fail;
		/* GB_Z_PEQ_CONFIG */
		if (p->rdev->family >= CHIP_RV350)
			break;
		goto fail;
		break;
1127 1128 1129 1130 1131
	case 0x4be8:
		/* valid register only on RV530 */
		if (p->rdev->family == CHIP_RV530)
			break;
		/* fallthrough do not move */
1132
	default:
1133
		goto fail;
1134 1135
	}
	return 0;
1136
fail:
1137 1138
	printk(KERN_ERR "Forbidden register 0x%04X in cs at %d (val=%08x)\n",
	       reg, idx, idx_value);
1139
	return -EINVAL;
1140 1141 1142 1143 1144
}

static int r300_packet3_check(struct radeon_cs_parser *p,
			      struct radeon_cs_packet *pkt)
{
1145
	struct radeon_bo_list *reloc;
1146
	struct r100_cs_track *track;
1147 1148 1149 1150
	volatile uint32_t *ib;
	unsigned idx;
	int r;

1151
	ib = p->ib.ptr;
1152
	idx = pkt->idx + 1;
1153
	track = (struct r100_cs_track *)p->track;
1154
	switch(pkt->opcode) {
1155
	case PACKET3_3D_LOAD_VBPNTR:
1156 1157 1158
		r = r100_packet3_load_vbpntr(p, pkt, idx);
		if (r)
			return r;
1159 1160
		break;
	case PACKET3_INDX_BUFFER:
1161
		r = radeon_cs_packet_next_reloc(p, &reloc, 0);
1162 1163
		if (r) {
			DRM_ERROR("No reloc for packet3 %d\n", pkt->opcode);
1164
			radeon_cs_dump_packet(p, pkt);
1165 1166
			return r;
		}
1167
		ib[idx+1] = radeon_get_ib_value(p, idx + 1) + ((u32)reloc->gpu_offset);
1168 1169 1170 1171
		r = r100_cs_track_check_pkt3_indx_buffer(p, pkt, reloc->robj);
		if (r) {
			return r;
		}
1172 1173 1174
		break;
	/* Draw packet */
	case PACKET3_3D_DRAW_IMMD:
1175 1176 1177
		/* Number of dwords is vtx_size * (num_vertices - 1)
		 * PRIM_WALK must be equal to 3 vertex data in embedded
		 * in cmd stream */
1178
		if (((radeon_get_ib_value(p, idx + 1) >> 4) & 0x3) != 3) {
1179 1180 1181
			DRM_ERROR("PRIM_WALK must be 3 for IMMD draw\n");
			return -EINVAL;
		}
1182
		track->vap_vf_cntl = radeon_get_ib_value(p, idx + 1);
1183
		track->immd_dwords = pkt->count - 1;
1184
		r = r100_cs_track_check(p->rdev, track);
1185 1186 1187 1188
		if (r) {
			return r;
		}
		break;
1189
	case PACKET3_3D_DRAW_IMMD_2:
1190 1191 1192
		/* Number of dwords is vtx_size * (num_vertices - 1)
		 * PRIM_WALK must be equal to 3 vertex data in embedded
		 * in cmd stream */
1193
		if (((radeon_get_ib_value(p, idx) >> 4) & 0x3) != 3) {
1194 1195 1196
			DRM_ERROR("PRIM_WALK must be 3 for IMMD draw\n");
			return -EINVAL;
		}
1197
		track->vap_vf_cntl = radeon_get_ib_value(p, idx);
1198
		track->immd_dwords = pkt->count;
1199
		r = r100_cs_track_check(p->rdev, track);
1200 1201 1202 1203 1204
		if (r) {
			return r;
		}
		break;
	case PACKET3_3D_DRAW_VBUF:
1205
		track->vap_vf_cntl = radeon_get_ib_value(p, idx + 1);
1206
		r = r100_cs_track_check(p->rdev, track);
1207 1208 1209 1210 1211
		if (r) {
			return r;
		}
		break;
	case PACKET3_3D_DRAW_VBUF_2:
1212
		track->vap_vf_cntl = radeon_get_ib_value(p, idx);
1213
		r = r100_cs_track_check(p->rdev, track);
1214 1215 1216 1217 1218
		if (r) {
			return r;
		}
		break;
	case PACKET3_3D_DRAW_INDX:
1219
		track->vap_vf_cntl = radeon_get_ib_value(p, idx + 1);
1220
		r = r100_cs_track_check(p->rdev, track);
1221 1222 1223 1224
		if (r) {
			return r;
		}
		break;
1225
	case PACKET3_3D_DRAW_INDX_2:
1226
		track->vap_vf_cntl = radeon_get_ib_value(p, idx);
1227
		r = r100_cs_track_check(p->rdev, track);
1228 1229 1230 1231
		if (r) {
			return r;
		}
		break;
1232 1233 1234 1235 1236
	case PACKET3_3D_CLEAR_HIZ:
	case PACKET3_3D_CLEAR_ZMASK:
		if (p->rdev->hyperz_filp != p->filp)
			return -EINVAL;
		break;
1237 1238 1239 1240
	case PACKET3_3D_CLEAR_CMASK:
		if (p->rdev->cmask_filp != p->filp)
			return -EINVAL;
		break;
1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252
	case PACKET3_NOP:
		break;
	default:
		DRM_ERROR("Packet3 opcode %x not supported\n", pkt->opcode);
		return -EINVAL;
	}
	return 0;
}

int r300_cs_parse(struct radeon_cs_parser *p)
{
	struct radeon_cs_packet pkt;
1253
	struct r100_cs_track *track;
1254 1255
	int r;

1256
	track = kzalloc(sizeof(*track), GFP_KERNEL);
1257 1258
	if (track == NULL)
		return -ENOMEM;
1259 1260
	r100_cs_track_clear(p->rdev, track);
	p->track = track;
1261
	do {
1262
		r = radeon_cs_packet_parse(p, &pkt, p->idx);
1263 1264 1265 1266 1267
		if (r) {
			return r;
		}
		p->idx += pkt.count + 2;
		switch (pkt.type) {
1268
		case RADEON_PACKET_TYPE0:
1269
			r = r100_cs_parse_packet0(p, &pkt,
1270 1271
						  p->rdev->config.r300.reg_safe_bm,
						  p->rdev->config.r300.reg_safe_bm_size,
1272 1273
						  &r300_packet0_check);
			break;
1274
		case RADEON_PACKET_TYPE2:
1275
			break;
1276
		case RADEON_PACKET_TYPE3:
1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288
			r = r300_packet3_check(p, &pkt);
			break;
		default:
			DRM_ERROR("Unknown packet type %d !\n", pkt.type);
			return -EINVAL;
		}
		if (r) {
			return r;
		}
	} while (p->idx < p->chunks[p->chunk_ib_idx].length_dw);
	return 0;
}
1289

1290
void r300_set_reg_safe(struct radeon_device *rdev)
1291 1292 1293
{
	rdev->config.r300.reg_safe_bm = r300_reg_safe_bm;
	rdev->config.r300.reg_safe_bm_size = ARRAY_SIZE(r300_reg_safe_bm);
1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328
}

void r300_mc_program(struct radeon_device *rdev)
{
	struct r100_mc_save save;
	int r;

	r = r100_debugfs_mc_info_init(rdev);
	if (r) {
		dev_err(rdev->dev, "Failed to create r100_mc debugfs file.\n");
	}

	/* Stops all mc clients */
	r100_mc_stop(rdev, &save);
	if (rdev->flags & RADEON_IS_AGP) {
		WREG32(R_00014C_MC_AGP_LOCATION,
			S_00014C_MC_AGP_START(rdev->mc.gtt_start >> 16) |
			S_00014C_MC_AGP_TOP(rdev->mc.gtt_end >> 16));
		WREG32(R_000170_AGP_BASE, lower_32_bits(rdev->mc.agp_base));
		WREG32(R_00015C_AGP_BASE_2,
			upper_32_bits(rdev->mc.agp_base) & 0xff);
	} else {
		WREG32(R_00014C_MC_AGP_LOCATION, 0x0FFFFFFF);
		WREG32(R_000170_AGP_BASE, 0);
		WREG32(R_00015C_AGP_BASE_2, 0);
	}
	/* Wait for mc idle */
	if (r300_mc_wait_for_idle(rdev))
		DRM_INFO("Failed to wait MC idle before programming MC.\n");
	/* Program MC, should be a 32bits limited address space */
	WREG32(R_000148_MC_FB_LOCATION,
		S_000148_MC_FB_START(rdev->mc.vram_start >> 16) |
		S_000148_MC_FB_TOP(rdev->mc.vram_end >> 16));
	r100_mc_resume(rdev, &save);
}
1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342

void r300_clock_startup(struct radeon_device *rdev)
{
	u32 tmp;

	if (radeon_dynclks != -1 && radeon_dynclks)
		radeon_legacy_set_clock_gating(rdev, 1);
	/* We need to force on some of the block */
	tmp = RREG32_PLL(R_00000D_SCLK_CNTL);
	tmp |= S_00000D_FORCE_CP(1) | S_00000D_FORCE_VIP(1);
	if ((rdev->family == CHIP_RV350) || (rdev->family == CHIP_RV380))
		tmp |= S_00000D_FORCE_VAP(1);
	WREG32_PLL(R_00000D_SCLK_CNTL, tmp);
}
1343 1344 1345 1346 1347

static int r300_startup(struct radeon_device *rdev)
{
	int r;

1348 1349 1350
	/* set common regs */
	r100_set_common_regs(rdev);
	/* program mc */
1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362
	r300_mc_program(rdev);
	/* Resume clock */
	r300_clock_startup(rdev);
	/* Initialize GPU configuration (# pipes, ...) */
	r300_gpu_init(rdev);
	/* Initialize GART (initialize after TTM so we can allocate
	 * memory through TTM but finalize after TTM) */
	if (rdev->flags & RADEON_IS_PCIE) {
		r = rv370_pcie_gart_enable(rdev);
		if (r)
			return r;
	}
1363 1364 1365 1366 1367 1368

	if (rdev->family == CHIP_R300 ||
	    rdev->family == CHIP_R350 ||
	    rdev->family == CHIP_RV350)
		r100_enable_bm(rdev);

1369 1370 1371 1372 1373
	if (rdev->flags & RADEON_IS_PCI) {
		r = r100_pci_gart_enable(rdev);
		if (r)
			return r;
	}
1374 1375 1376 1377 1378 1379

	/* allocate wb buffer */
	r = radeon_wb_init(rdev);
	if (r)
		return r;

1380 1381 1382 1383 1384 1385
	r = radeon_fence_driver_start_ring(rdev, RADEON_RING_TYPE_GFX_INDEX);
	if (r) {
		dev_err(rdev->dev, "failed initializing CP fences (%d).\n", r);
		return r;
	}

1386
	/* Enable IRQ */
1387 1388 1389 1390 1391 1392
	if (!rdev->irq.installed) {
		r = radeon_irq_kms_init(rdev);
		if (r)
			return r;
	}

1393
	r100_irq_set(rdev);
1394
	rdev->config.r300.hdp_cntl = RREG32(RADEON_HOST_PATH_CNTL);
1395 1396 1397
	/* 1M ring buffer */
	r = r100_cp_init(rdev, 1024 * 1024);
	if (r) {
P
Paul Bolle 已提交
1398
		dev_err(rdev->dev, "failed initializing CP (%d).\n", r);
1399 1400
		return r;
	}
1401

1402 1403 1404
	r = radeon_ib_pool_init(rdev);
	if (r) {
		dev_err(rdev->dev, "IB initialization failed (%d).\n", r);
1405
		return r;
1406
	}
1407

1408 1409 1410 1411 1412
	return 0;
}

int r300_resume(struct radeon_device *rdev)
{
1413 1414
	int r;

1415 1416 1417 1418 1419 1420 1421 1422
	/* Make sur GART are not working */
	if (rdev->flags & RADEON_IS_PCIE)
		rv370_pcie_gart_disable(rdev);
	if (rdev->flags & RADEON_IS_PCI)
		r100_pci_gart_disable(rdev);
	/* Resume clock before doing reset */
	r300_clock_startup(rdev);
	/* Reset gpu before posting otherwise ATOM will enter infinite loop */
1423
	if (radeon_asic_reset(rdev)) {
1424 1425 1426 1427 1428 1429 1430 1431
		dev_warn(rdev->dev, "GPU reset failed ! (0xE40=0x%08X, 0x7C0=0x%08X)\n",
			RREG32(R_000E40_RBBM_STATUS),
			RREG32(R_0007C0_CP_STAT));
	}
	/* post */
	radeon_combios_asic_init(rdev->ddev);
	/* Resume clock after posting */
	r300_clock_startup(rdev);
1432 1433
	/* Initialize surface registers */
	radeon_surface_init(rdev);
1434 1435

	rdev->accel_working = true;
1436 1437 1438 1439 1440
	r = r300_startup(rdev);
	if (r) {
		rdev->accel_working = false;
	}
	return r;
1441 1442 1443 1444
}

int r300_suspend(struct radeon_device *rdev)
{
1445
	radeon_pm_suspend(rdev);
1446
	r100_cp_disable(rdev);
1447
	radeon_wb_disable(rdev);
1448 1449 1450 1451 1452 1453 1454 1455 1456 1457
	r100_irq_disable(rdev);
	if (rdev->flags & RADEON_IS_PCIE)
		rv370_pcie_gart_disable(rdev);
	if (rdev->flags & RADEON_IS_PCI)
		r100_pci_gart_disable(rdev);
	return 0;
}

void r300_fini(struct radeon_device *rdev)
{
1458
	radeon_pm_fini(rdev);
1459
	r100_cp_fini(rdev);
1460
	radeon_wb_fini(rdev);
1461
	radeon_ib_pool_fini(rdev);
1462 1463 1464 1465 1466
	radeon_gem_fini(rdev);
	if (rdev->flags & RADEON_IS_PCIE)
		rv370_pcie_gart_fini(rdev);
	if (rdev->flags & RADEON_IS_PCI)
		r100_pci_gart_fini(rdev);
1467
	radeon_agp_fini(rdev);
1468 1469
	radeon_irq_kms_fini(rdev);
	radeon_fence_driver_fini(rdev);
1470
	radeon_bo_fini(rdev);
1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486
	radeon_atombios_fini(rdev);
	kfree(rdev->bios);
	rdev->bios = NULL;
}

int r300_init(struct radeon_device *rdev)
{
	int r;

	/* Disable VGA */
	r100_vga_render_disable(rdev);
	/* Initialize scratch registers */
	radeon_scratch_init(rdev);
	/* Initialize surface registers */
	radeon_surface_init(rdev);
	/* TODO: disable VGA need to use VGA request */
1487 1488
	/* restore some register to sane defaults */
	r100_restore_sanity(rdev);
1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502
	/* BIOS*/
	if (!radeon_get_bios(rdev)) {
		if (ASIC_IS_AVIVO(rdev))
			return -EINVAL;
	}
	if (rdev->is_atom_bios) {
		dev_err(rdev->dev, "Expecting combios for RS400/RS480 GPU\n");
		return -EINVAL;
	} else {
		r = radeon_combios_init(rdev);
		if (r)
			return r;
	}
	/* Reset gpu before posting otherwise ATOM will enter infinite loop */
1503
	if (radeon_asic_reset(rdev)) {
1504 1505 1506 1507 1508 1509
		dev_warn(rdev->dev,
			"GPU reset failed ! (0xE40=0x%08X, 0x7C0=0x%08X)\n",
			RREG32(R_000E40_RBBM_STATUS),
			RREG32(R_0007C0_CP_STAT));
	}
	/* check if cards are posted or not */
1510 1511
	if (radeon_boot_test_post_card(rdev) == false)
		return -EINVAL;
1512 1513 1514 1515
	/* Set asic errata */
	r300_errata(rdev);
	/* Initialize clocks */
	radeon_get_clock_info(rdev->ddev);
1516 1517 1518 1519 1520 1521 1522 1523 1524
	/* initialize AGP */
	if (rdev->flags & RADEON_IS_AGP) {
		r = radeon_agp_init(rdev);
		if (r) {
			radeon_agp_disable(rdev);
		}
	}
	/* initialize memory controller */
	r300_mc_init(rdev);
1525
	/* Fence driver */
1526
	r = radeon_fence_driver_init(rdev);
1527 1528 1529
	if (r)
		return r;
	/* Memory manager */
1530
	r = radeon_bo_init(rdev);
1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543
	if (r)
		return r;
	if (rdev->flags & RADEON_IS_PCIE) {
		r = rv370_pcie_gart_init(rdev);
		if (r)
			return r;
	}
	if (rdev->flags & RADEON_IS_PCI) {
		r = r100_pci_gart_init(rdev);
		if (r)
			return r;
	}
	r300_set_reg_safe(rdev);
1544

1545 1546 1547
	/* Initialize power management */
	radeon_pm_init(rdev);

1548 1549 1550
	rdev->accel_working = true;
	r = r300_startup(rdev);
	if (r) {
1551
		/* Something went wrong with the accel init, so stop accel */
1552 1553
		dev_err(rdev->dev, "Disabling GPU acceleration\n");
		r100_cp_fini(rdev);
1554
		radeon_wb_fini(rdev);
1555
		radeon_ib_pool_fini(rdev);
1556
		radeon_irq_kms_fini(rdev);
1557 1558 1559 1560
		if (rdev->flags & RADEON_IS_PCIE)
			rv370_pcie_gart_fini(rdev);
		if (rdev->flags & RADEON_IS_PCI)
			r100_pci_gart_fini(rdev);
1561
		radeon_agp_fini(rdev);
1562 1563 1564 1565
		rdev->accel_working = false;
	}
	return 0;
}