xref: /openbmc/linux/drivers/gpu/drm/radeon/r100.c (revision 7465280c076d6440e5908c158c83b542dc063a30)
1771fe6b9SJerome Glisse /*
2771fe6b9SJerome Glisse  * Copyright 2008 Advanced Micro Devices, Inc.
3771fe6b9SJerome Glisse  * Copyright 2008 Red Hat Inc.
4771fe6b9SJerome Glisse  * Copyright 2009 Jerome Glisse.
5771fe6b9SJerome Glisse  *
6771fe6b9SJerome Glisse  * Permission is hereby granted, free of charge, to any person obtaining a
7771fe6b9SJerome Glisse  * copy of this software and associated documentation files (the "Software"),
8771fe6b9SJerome Glisse  * to deal in the Software without restriction, including without limitation
9771fe6b9SJerome Glisse  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10771fe6b9SJerome Glisse  * and/or sell copies of the Software, and to permit persons to whom the
11771fe6b9SJerome Glisse  * Software is furnished to do so, subject to the following conditions:
12771fe6b9SJerome Glisse  *
13771fe6b9SJerome Glisse  * The above copyright notice and this permission notice shall be included in
14771fe6b9SJerome Glisse  * all copies or substantial portions of the Software.
15771fe6b9SJerome Glisse  *
16771fe6b9SJerome Glisse  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17771fe6b9SJerome Glisse  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18771fe6b9SJerome Glisse  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19771fe6b9SJerome Glisse  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20771fe6b9SJerome Glisse  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21771fe6b9SJerome Glisse  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22771fe6b9SJerome Glisse  * OTHER DEALINGS IN THE SOFTWARE.
23771fe6b9SJerome Glisse  *
24771fe6b9SJerome Glisse  * Authors: Dave Airlie
25771fe6b9SJerome Glisse  *          Alex Deucher
26771fe6b9SJerome Glisse  *          Jerome Glisse
27771fe6b9SJerome Glisse  */
28771fe6b9SJerome Glisse #include <linux/seq_file.h>
295a0e3ad6STejun Heo #include <linux/slab.h>
30771fe6b9SJerome Glisse #include "drmP.h"
31771fe6b9SJerome Glisse #include "drm.h"
32771fe6b9SJerome Glisse #include "radeon_drm.h"
33771fe6b9SJerome Glisse #include "radeon_reg.h"
34771fe6b9SJerome Glisse #include "radeon.h"
35e6990375SDaniel Vetter #include "radeon_asic.h"
363ce0a23dSJerome Glisse #include "r100d.h"
37d4550907SJerome Glisse #include "rs100d.h"
38d4550907SJerome Glisse #include "rv200d.h"
39d4550907SJerome Glisse #include "rv250d.h"
4049e02b73SAlex Deucher #include "atom.h"
413ce0a23dSJerome Glisse 
4270967ab9SBen Hutchings #include <linux/firmware.h>
4370967ab9SBen Hutchings #include <linux/platform_device.h>
44e0cd3608SPaul Gortmaker #include <linux/module.h>
4570967ab9SBen Hutchings 
46551ebd83SDave Airlie #include "r100_reg_safe.h"
47551ebd83SDave Airlie #include "rn50_reg_safe.h"
48551ebd83SDave Airlie 
4970967ab9SBen Hutchings /* Firmware Names */
5070967ab9SBen Hutchings #define FIRMWARE_R100		"radeon/R100_cp.bin"
5170967ab9SBen Hutchings #define FIRMWARE_R200		"radeon/R200_cp.bin"
5270967ab9SBen Hutchings #define FIRMWARE_R300		"radeon/R300_cp.bin"
5370967ab9SBen Hutchings #define FIRMWARE_R420		"radeon/R420_cp.bin"
5470967ab9SBen Hutchings #define FIRMWARE_RS690		"radeon/RS690_cp.bin"
5570967ab9SBen Hutchings #define FIRMWARE_RS600		"radeon/RS600_cp.bin"
5670967ab9SBen Hutchings #define FIRMWARE_R520		"radeon/R520_cp.bin"
5770967ab9SBen Hutchings 
5870967ab9SBen Hutchings MODULE_FIRMWARE(FIRMWARE_R100);
5970967ab9SBen Hutchings MODULE_FIRMWARE(FIRMWARE_R200);
6070967ab9SBen Hutchings MODULE_FIRMWARE(FIRMWARE_R300);
6170967ab9SBen Hutchings MODULE_FIRMWARE(FIRMWARE_R420);
6270967ab9SBen Hutchings MODULE_FIRMWARE(FIRMWARE_RS690);
6370967ab9SBen Hutchings MODULE_FIRMWARE(FIRMWARE_RS600);
6470967ab9SBen Hutchings MODULE_FIRMWARE(FIRMWARE_R520);
65771fe6b9SJerome Glisse 
66551ebd83SDave Airlie #include "r100_track.h"
67551ebd83SDave Airlie 
68771fe6b9SJerome Glisse /* This files gather functions specifics to:
69771fe6b9SJerome Glisse  * r100,rv100,rs100,rv200,rs200,r200,rv250,rs300,rv280
70771fe6b9SJerome Glisse  */
71771fe6b9SJerome Glisse 
72cbdd4501SAndi Kleen int r100_reloc_pitch_offset(struct radeon_cs_parser *p,
73cbdd4501SAndi Kleen 			    struct radeon_cs_packet *pkt,
74cbdd4501SAndi Kleen 			    unsigned idx,
75cbdd4501SAndi Kleen 			    unsigned reg)
76cbdd4501SAndi Kleen {
77cbdd4501SAndi Kleen 	int r;
78cbdd4501SAndi Kleen 	u32 tile_flags = 0;
79cbdd4501SAndi Kleen 	u32 tmp;
80cbdd4501SAndi Kleen 	struct radeon_cs_reloc *reloc;
81cbdd4501SAndi Kleen 	u32 value;
82cbdd4501SAndi Kleen 
83cbdd4501SAndi Kleen 	r = r100_cs_packet_next_reloc(p, &reloc);
84cbdd4501SAndi Kleen 	if (r) {
85cbdd4501SAndi Kleen 		DRM_ERROR("No reloc for ib[%d]=0x%04X\n",
86cbdd4501SAndi Kleen 			  idx, reg);
87cbdd4501SAndi Kleen 		r100_cs_dump_packet(p, pkt);
88cbdd4501SAndi Kleen 		return r;
89cbdd4501SAndi Kleen 	}
90cbdd4501SAndi Kleen 	value = radeon_get_ib_value(p, idx);
91cbdd4501SAndi Kleen 	tmp = value & 0x003fffff;
92cbdd4501SAndi Kleen 	tmp += (((u32)reloc->lobj.gpu_offset) >> 10);
93cbdd4501SAndi Kleen 
94cbdd4501SAndi Kleen 	if (reloc->lobj.tiling_flags & RADEON_TILING_MACRO)
95cbdd4501SAndi Kleen 		tile_flags |= RADEON_DST_TILE_MACRO;
96cbdd4501SAndi Kleen 	if (reloc->lobj.tiling_flags & RADEON_TILING_MICRO) {
97cbdd4501SAndi Kleen 		if (reg == RADEON_SRC_PITCH_OFFSET) {
98cbdd4501SAndi Kleen 			DRM_ERROR("Cannot src blit from microtiled surface\n");
99cbdd4501SAndi Kleen 			r100_cs_dump_packet(p, pkt);
100cbdd4501SAndi Kleen 			return -EINVAL;
101cbdd4501SAndi Kleen 		}
102cbdd4501SAndi Kleen 		tile_flags |= RADEON_DST_TILE_MICRO;
103cbdd4501SAndi Kleen 	}
104cbdd4501SAndi Kleen 
105cbdd4501SAndi Kleen 	tmp |= tile_flags;
106cbdd4501SAndi Kleen 	p->ib->ptr[idx] = (value & 0x3fc00000) | tmp;
107cbdd4501SAndi Kleen 	return 0;
108cbdd4501SAndi Kleen }
109cbdd4501SAndi Kleen 
110cbdd4501SAndi Kleen int r100_packet3_load_vbpntr(struct radeon_cs_parser *p,
111cbdd4501SAndi Kleen 			     struct radeon_cs_packet *pkt,
112cbdd4501SAndi Kleen 			     int idx)
113cbdd4501SAndi Kleen {
114cbdd4501SAndi Kleen 	unsigned c, i;
115cbdd4501SAndi Kleen 	struct radeon_cs_reloc *reloc;
116cbdd4501SAndi Kleen 	struct r100_cs_track *track;
117cbdd4501SAndi Kleen 	int r = 0;
118cbdd4501SAndi Kleen 	volatile uint32_t *ib;
119cbdd4501SAndi Kleen 	u32 idx_value;
120cbdd4501SAndi Kleen 
121cbdd4501SAndi Kleen 	ib = p->ib->ptr;
122cbdd4501SAndi Kleen 	track = (struct r100_cs_track *)p->track;
123cbdd4501SAndi Kleen 	c = radeon_get_ib_value(p, idx++) & 0x1F;
124cbdd4501SAndi Kleen 	if (c > 16) {
125cbdd4501SAndi Kleen 	    DRM_ERROR("Only 16 vertex buffers are allowed %d\n",
126cbdd4501SAndi Kleen 		      pkt->opcode);
127cbdd4501SAndi Kleen 	    r100_cs_dump_packet(p, pkt);
128cbdd4501SAndi Kleen 	    return -EINVAL;
129cbdd4501SAndi Kleen 	}
130cbdd4501SAndi Kleen 	track->num_arrays = c;
131cbdd4501SAndi Kleen 	for (i = 0; i < (c - 1); i+=2, idx+=3) {
132cbdd4501SAndi Kleen 		r = r100_cs_packet_next_reloc(p, &reloc);
133cbdd4501SAndi Kleen 		if (r) {
134cbdd4501SAndi Kleen 			DRM_ERROR("No reloc for packet3 %d\n",
135cbdd4501SAndi Kleen 				  pkt->opcode);
136cbdd4501SAndi Kleen 			r100_cs_dump_packet(p, pkt);
137cbdd4501SAndi Kleen 			return r;
138cbdd4501SAndi Kleen 		}
139cbdd4501SAndi Kleen 		idx_value = radeon_get_ib_value(p, idx);
140cbdd4501SAndi Kleen 		ib[idx+1] = radeon_get_ib_value(p, idx + 1) + ((u32)reloc->lobj.gpu_offset);
141cbdd4501SAndi Kleen 
142cbdd4501SAndi Kleen 		track->arrays[i + 0].esize = idx_value >> 8;
143cbdd4501SAndi Kleen 		track->arrays[i + 0].robj = reloc->robj;
144cbdd4501SAndi Kleen 		track->arrays[i + 0].esize &= 0x7F;
145cbdd4501SAndi Kleen 		r = r100_cs_packet_next_reloc(p, &reloc);
146cbdd4501SAndi Kleen 		if (r) {
147cbdd4501SAndi Kleen 			DRM_ERROR("No reloc for packet3 %d\n",
148cbdd4501SAndi Kleen 				  pkt->opcode);
149cbdd4501SAndi Kleen 			r100_cs_dump_packet(p, pkt);
150cbdd4501SAndi Kleen 			return r;
151cbdd4501SAndi Kleen 		}
152cbdd4501SAndi Kleen 		ib[idx+2] = radeon_get_ib_value(p, idx + 2) + ((u32)reloc->lobj.gpu_offset);
153cbdd4501SAndi Kleen 		track->arrays[i + 1].robj = reloc->robj;
154cbdd4501SAndi Kleen 		track->arrays[i + 1].esize = idx_value >> 24;
155cbdd4501SAndi Kleen 		track->arrays[i + 1].esize &= 0x7F;
156cbdd4501SAndi Kleen 	}
157cbdd4501SAndi Kleen 	if (c & 1) {
158cbdd4501SAndi Kleen 		r = r100_cs_packet_next_reloc(p, &reloc);
159cbdd4501SAndi Kleen 		if (r) {
160cbdd4501SAndi Kleen 			DRM_ERROR("No reloc for packet3 %d\n",
161cbdd4501SAndi Kleen 					  pkt->opcode);
162cbdd4501SAndi Kleen 			r100_cs_dump_packet(p, pkt);
163cbdd4501SAndi Kleen 			return r;
164cbdd4501SAndi Kleen 		}
165cbdd4501SAndi Kleen 		idx_value = radeon_get_ib_value(p, idx);
166cbdd4501SAndi Kleen 		ib[idx+1] = radeon_get_ib_value(p, idx + 1) + ((u32)reloc->lobj.gpu_offset);
167cbdd4501SAndi Kleen 		track->arrays[i + 0].robj = reloc->robj;
168cbdd4501SAndi Kleen 		track->arrays[i + 0].esize = idx_value >> 8;
169cbdd4501SAndi Kleen 		track->arrays[i + 0].esize &= 0x7F;
170cbdd4501SAndi Kleen 	}
171cbdd4501SAndi Kleen 	return r;
172cbdd4501SAndi Kleen }
173cbdd4501SAndi Kleen 
1746f34be50SAlex Deucher void r100_pre_page_flip(struct radeon_device *rdev, int crtc)
1756f34be50SAlex Deucher {
1766f34be50SAlex Deucher 	/* enable the pflip int */
1776f34be50SAlex Deucher 	radeon_irq_kms_pflip_irq_get(rdev, crtc);
1786f34be50SAlex Deucher }
1796f34be50SAlex Deucher 
1806f34be50SAlex Deucher void r100_post_page_flip(struct radeon_device *rdev, int crtc)
1816f34be50SAlex Deucher {
1826f34be50SAlex Deucher 	/* disable the pflip int */
1836f34be50SAlex Deucher 	radeon_irq_kms_pflip_irq_put(rdev, crtc);
1846f34be50SAlex Deucher }
1856f34be50SAlex Deucher 
1866f34be50SAlex Deucher u32 r100_page_flip(struct radeon_device *rdev, int crtc_id, u64 crtc_base)
1876f34be50SAlex Deucher {
1886f34be50SAlex Deucher 	struct radeon_crtc *radeon_crtc = rdev->mode_info.crtcs[crtc_id];
1896f34be50SAlex Deucher 	u32 tmp = ((u32)crtc_base) | RADEON_CRTC_OFFSET__OFFSET_LOCK;
190f6496479SAlex Deucher 	int i;
1916f34be50SAlex Deucher 
1926f34be50SAlex Deucher 	/* Lock the graphics update lock */
1936f34be50SAlex Deucher 	/* update the scanout addresses */
1946f34be50SAlex Deucher 	WREG32(RADEON_CRTC_OFFSET + radeon_crtc->crtc_offset, tmp);
1956f34be50SAlex Deucher 
196acb32506SAlex Deucher 	/* Wait for update_pending to go high. */
197f6496479SAlex Deucher 	for (i = 0; i < rdev->usec_timeout; i++) {
198f6496479SAlex Deucher 		if (RREG32(RADEON_CRTC_OFFSET + radeon_crtc->crtc_offset) & RADEON_CRTC_OFFSET__GUI_TRIG_OFFSET)
199f6496479SAlex Deucher 			break;
200f6496479SAlex Deucher 		udelay(1);
201f6496479SAlex Deucher 	}
202acb32506SAlex Deucher 	DRM_DEBUG("Update pending now high. Unlocking vupdate_lock.\n");
2036f34be50SAlex Deucher 
2046f34be50SAlex Deucher 	/* Unlock the lock, so double-buffering can take place inside vblank */
2056f34be50SAlex Deucher 	tmp &= ~RADEON_CRTC_OFFSET__OFFSET_LOCK;
2066f34be50SAlex Deucher 	WREG32(RADEON_CRTC_OFFSET + radeon_crtc->crtc_offset, tmp);
2076f34be50SAlex Deucher 
2086f34be50SAlex Deucher 	/* Return current update_pending status: */
2096f34be50SAlex Deucher 	return RREG32(RADEON_CRTC_OFFSET + radeon_crtc->crtc_offset) & RADEON_CRTC_OFFSET__GUI_TRIG_OFFSET;
2106f34be50SAlex Deucher }
2116f34be50SAlex Deucher 
212ce8f5370SAlex Deucher void r100_pm_get_dynpm_state(struct radeon_device *rdev)
213a48b9b4eSAlex Deucher {
214a48b9b4eSAlex Deucher 	int i;
215ce8f5370SAlex Deucher 	rdev->pm.dynpm_can_upclock = true;
216ce8f5370SAlex Deucher 	rdev->pm.dynpm_can_downclock = true;
217a48b9b4eSAlex Deucher 
218ce8f5370SAlex Deucher 	switch (rdev->pm.dynpm_planned_action) {
219ce8f5370SAlex Deucher 	case DYNPM_ACTION_MINIMUM:
220a48b9b4eSAlex Deucher 		rdev->pm.requested_power_state_index = 0;
221ce8f5370SAlex Deucher 		rdev->pm.dynpm_can_downclock = false;
222a48b9b4eSAlex Deucher 		break;
223ce8f5370SAlex Deucher 	case DYNPM_ACTION_DOWNCLOCK:
224a48b9b4eSAlex Deucher 		if (rdev->pm.current_power_state_index == 0) {
225a48b9b4eSAlex Deucher 			rdev->pm.requested_power_state_index = rdev->pm.current_power_state_index;
226ce8f5370SAlex Deucher 			rdev->pm.dynpm_can_downclock = false;
227a48b9b4eSAlex Deucher 		} else {
228a48b9b4eSAlex Deucher 			if (rdev->pm.active_crtc_count > 1) {
229a48b9b4eSAlex Deucher 				for (i = 0; i < rdev->pm.num_power_states; i++) {
230d7311171SAlex Deucher 					if (rdev->pm.power_state[i].flags & RADEON_PM_STATE_SINGLE_DISPLAY_ONLY)
231a48b9b4eSAlex Deucher 						continue;
232a48b9b4eSAlex Deucher 					else if (i >= rdev->pm.current_power_state_index) {
233a48b9b4eSAlex Deucher 						rdev->pm.requested_power_state_index = rdev->pm.current_power_state_index;
234a48b9b4eSAlex Deucher 						break;
235a48b9b4eSAlex Deucher 					} else {
236a48b9b4eSAlex Deucher 						rdev->pm.requested_power_state_index = i;
237a48b9b4eSAlex Deucher 						break;
238a48b9b4eSAlex Deucher 					}
239a48b9b4eSAlex Deucher 				}
240a48b9b4eSAlex Deucher 			} else
241a48b9b4eSAlex Deucher 				rdev->pm.requested_power_state_index =
242a48b9b4eSAlex Deucher 					rdev->pm.current_power_state_index - 1;
243a48b9b4eSAlex Deucher 		}
244d7311171SAlex Deucher 		/* don't use the power state if crtcs are active and no display flag is set */
245d7311171SAlex Deucher 		if ((rdev->pm.active_crtc_count > 0) &&
246d7311171SAlex Deucher 		    (rdev->pm.power_state[rdev->pm.requested_power_state_index].clock_info[0].flags &
247d7311171SAlex Deucher 		     RADEON_PM_MODE_NO_DISPLAY)) {
248d7311171SAlex Deucher 			rdev->pm.requested_power_state_index++;
249d7311171SAlex Deucher 		}
250a48b9b4eSAlex Deucher 		break;
251ce8f5370SAlex Deucher 	case DYNPM_ACTION_UPCLOCK:
252a48b9b4eSAlex Deucher 		if (rdev->pm.current_power_state_index == (rdev->pm.num_power_states - 1)) {
253a48b9b4eSAlex Deucher 			rdev->pm.requested_power_state_index = rdev->pm.current_power_state_index;
254ce8f5370SAlex Deucher 			rdev->pm.dynpm_can_upclock = false;
255a48b9b4eSAlex Deucher 		} else {
256a48b9b4eSAlex Deucher 			if (rdev->pm.active_crtc_count > 1) {
257a48b9b4eSAlex Deucher 				for (i = (rdev->pm.num_power_states - 1); i >= 0; i--) {
258d7311171SAlex Deucher 					if (rdev->pm.power_state[i].flags & RADEON_PM_STATE_SINGLE_DISPLAY_ONLY)
259a48b9b4eSAlex Deucher 						continue;
260a48b9b4eSAlex Deucher 					else if (i <= rdev->pm.current_power_state_index) {
261a48b9b4eSAlex Deucher 						rdev->pm.requested_power_state_index = rdev->pm.current_power_state_index;
262a48b9b4eSAlex Deucher 						break;
263a48b9b4eSAlex Deucher 					} else {
264a48b9b4eSAlex Deucher 						rdev->pm.requested_power_state_index = i;
265a48b9b4eSAlex Deucher 						break;
266a48b9b4eSAlex Deucher 					}
267a48b9b4eSAlex Deucher 				}
268a48b9b4eSAlex Deucher 			} else
269a48b9b4eSAlex Deucher 				rdev->pm.requested_power_state_index =
270a48b9b4eSAlex Deucher 					rdev->pm.current_power_state_index + 1;
271a48b9b4eSAlex Deucher 		}
272a48b9b4eSAlex Deucher 		break;
273ce8f5370SAlex Deucher 	case DYNPM_ACTION_DEFAULT:
27458e21dffSAlex Deucher 		rdev->pm.requested_power_state_index = rdev->pm.default_power_state_index;
275ce8f5370SAlex Deucher 		rdev->pm.dynpm_can_upclock = false;
27658e21dffSAlex Deucher 		break;
277ce8f5370SAlex Deucher 	case DYNPM_ACTION_NONE:
278a48b9b4eSAlex Deucher 	default:
279a48b9b4eSAlex Deucher 		DRM_ERROR("Requested mode for not defined action\n");
280a48b9b4eSAlex Deucher 		return;
281a48b9b4eSAlex Deucher 	}
282a48b9b4eSAlex Deucher 	/* only one clock mode per power state */
283a48b9b4eSAlex Deucher 	rdev->pm.requested_clock_mode_index = 0;
284a48b9b4eSAlex Deucher 
285d9fdaafbSDave Airlie 	DRM_DEBUG_DRIVER("Requested: e: %d m: %d p: %d\n",
286a48b9b4eSAlex Deucher 		  rdev->pm.power_state[rdev->pm.requested_power_state_index].
287a48b9b4eSAlex Deucher 		  clock_info[rdev->pm.requested_clock_mode_index].sclk,
288a48b9b4eSAlex Deucher 		  rdev->pm.power_state[rdev->pm.requested_power_state_index].
289a48b9b4eSAlex Deucher 		  clock_info[rdev->pm.requested_clock_mode_index].mclk,
290a48b9b4eSAlex Deucher 		  rdev->pm.power_state[rdev->pm.requested_power_state_index].
29179daedc9SAlex Deucher 		  pcie_lanes);
292a48b9b4eSAlex Deucher }
293a48b9b4eSAlex Deucher 
294ce8f5370SAlex Deucher void r100_pm_init_profile(struct radeon_device *rdev)
295bae6b562SAlex Deucher {
296ce8f5370SAlex Deucher 	/* default */
297ce8f5370SAlex Deucher 	rdev->pm.profiles[PM_PROFILE_DEFAULT_IDX].dpms_off_ps_idx = rdev->pm.default_power_state_index;
298ce8f5370SAlex Deucher 	rdev->pm.profiles[PM_PROFILE_DEFAULT_IDX].dpms_on_ps_idx = rdev->pm.default_power_state_index;
299ce8f5370SAlex Deucher 	rdev->pm.profiles[PM_PROFILE_DEFAULT_IDX].dpms_off_cm_idx = 0;
300ce8f5370SAlex Deucher 	rdev->pm.profiles[PM_PROFILE_DEFAULT_IDX].dpms_on_cm_idx = 0;
301ce8f5370SAlex Deucher 	/* low sh */
302ce8f5370SAlex Deucher 	rdev->pm.profiles[PM_PROFILE_LOW_SH_IDX].dpms_off_ps_idx = 0;
303ce8f5370SAlex Deucher 	rdev->pm.profiles[PM_PROFILE_LOW_SH_IDX].dpms_on_ps_idx = 0;
304ce8f5370SAlex Deucher 	rdev->pm.profiles[PM_PROFILE_LOW_SH_IDX].dpms_off_cm_idx = 0;
305ce8f5370SAlex Deucher 	rdev->pm.profiles[PM_PROFILE_LOW_SH_IDX].dpms_on_cm_idx = 0;
306c9e75b21SAlex Deucher 	/* mid sh */
307c9e75b21SAlex Deucher 	rdev->pm.profiles[PM_PROFILE_MID_SH_IDX].dpms_off_ps_idx = 0;
308c9e75b21SAlex Deucher 	rdev->pm.profiles[PM_PROFILE_MID_SH_IDX].dpms_on_ps_idx = 0;
309c9e75b21SAlex Deucher 	rdev->pm.profiles[PM_PROFILE_MID_SH_IDX].dpms_off_cm_idx = 0;
310c9e75b21SAlex Deucher 	rdev->pm.profiles[PM_PROFILE_MID_SH_IDX].dpms_on_cm_idx = 0;
311ce8f5370SAlex Deucher 	/* high sh */
312ce8f5370SAlex Deucher 	rdev->pm.profiles[PM_PROFILE_HIGH_SH_IDX].dpms_off_ps_idx = 0;
313ce8f5370SAlex Deucher 	rdev->pm.profiles[PM_PROFILE_HIGH_SH_IDX].dpms_on_ps_idx = rdev->pm.default_power_state_index;
314ce8f5370SAlex Deucher 	rdev->pm.profiles[PM_PROFILE_HIGH_SH_IDX].dpms_off_cm_idx = 0;
315ce8f5370SAlex Deucher 	rdev->pm.profiles[PM_PROFILE_HIGH_SH_IDX].dpms_on_cm_idx = 0;
316ce8f5370SAlex Deucher 	/* low mh */
317ce8f5370SAlex Deucher 	rdev->pm.profiles[PM_PROFILE_LOW_MH_IDX].dpms_off_ps_idx = 0;
318ce8f5370SAlex Deucher 	rdev->pm.profiles[PM_PROFILE_LOW_MH_IDX].dpms_on_ps_idx = rdev->pm.default_power_state_index;
319ce8f5370SAlex Deucher 	rdev->pm.profiles[PM_PROFILE_LOW_MH_IDX].dpms_off_cm_idx = 0;
320ce8f5370SAlex Deucher 	rdev->pm.profiles[PM_PROFILE_LOW_MH_IDX].dpms_on_cm_idx = 0;
321c9e75b21SAlex Deucher 	/* mid mh */
322c9e75b21SAlex Deucher 	rdev->pm.profiles[PM_PROFILE_MID_MH_IDX].dpms_off_ps_idx = 0;
323c9e75b21SAlex Deucher 	rdev->pm.profiles[PM_PROFILE_MID_MH_IDX].dpms_on_ps_idx = rdev->pm.default_power_state_index;
324c9e75b21SAlex Deucher 	rdev->pm.profiles[PM_PROFILE_MID_MH_IDX].dpms_off_cm_idx = 0;
325c9e75b21SAlex Deucher 	rdev->pm.profiles[PM_PROFILE_MID_MH_IDX].dpms_on_cm_idx = 0;
326ce8f5370SAlex Deucher 	/* high mh */
327ce8f5370SAlex Deucher 	rdev->pm.profiles[PM_PROFILE_HIGH_MH_IDX].dpms_off_ps_idx = 0;
328ce8f5370SAlex Deucher 	rdev->pm.profiles[PM_PROFILE_HIGH_MH_IDX].dpms_on_ps_idx = rdev->pm.default_power_state_index;
329ce8f5370SAlex Deucher 	rdev->pm.profiles[PM_PROFILE_HIGH_MH_IDX].dpms_off_cm_idx = 0;
330ce8f5370SAlex Deucher 	rdev->pm.profiles[PM_PROFILE_HIGH_MH_IDX].dpms_on_cm_idx = 0;
331bae6b562SAlex Deucher }
332bae6b562SAlex Deucher 
33349e02b73SAlex Deucher void r100_pm_misc(struct radeon_device *rdev)
33449e02b73SAlex Deucher {
33549e02b73SAlex Deucher 	int requested_index = rdev->pm.requested_power_state_index;
33649e02b73SAlex Deucher 	struct radeon_power_state *ps = &rdev->pm.power_state[requested_index];
33749e02b73SAlex Deucher 	struct radeon_voltage *voltage = &ps->clock_info[0].voltage;
33849e02b73SAlex Deucher 	u32 tmp, sclk_cntl, sclk_cntl2, sclk_more_cntl;
33949e02b73SAlex Deucher 
34049e02b73SAlex Deucher 	if ((voltage->type == VOLTAGE_GPIO) && (voltage->gpio.valid)) {
34149e02b73SAlex Deucher 		if (ps->misc & ATOM_PM_MISCINFO_VOLTAGE_DROP_SUPPORT) {
34249e02b73SAlex Deucher 			tmp = RREG32(voltage->gpio.reg);
34349e02b73SAlex Deucher 			if (voltage->active_high)
34449e02b73SAlex Deucher 				tmp |= voltage->gpio.mask;
34549e02b73SAlex Deucher 			else
34649e02b73SAlex Deucher 				tmp &= ~(voltage->gpio.mask);
34749e02b73SAlex Deucher 			WREG32(voltage->gpio.reg, tmp);
34849e02b73SAlex Deucher 			if (voltage->delay)
34949e02b73SAlex Deucher 				udelay(voltage->delay);
35049e02b73SAlex Deucher 		} else {
35149e02b73SAlex Deucher 			tmp = RREG32(voltage->gpio.reg);
35249e02b73SAlex Deucher 			if (voltage->active_high)
35349e02b73SAlex Deucher 				tmp &= ~voltage->gpio.mask;
35449e02b73SAlex Deucher 			else
35549e02b73SAlex Deucher 				tmp |= voltage->gpio.mask;
35649e02b73SAlex Deucher 			WREG32(voltage->gpio.reg, tmp);
35749e02b73SAlex Deucher 			if (voltage->delay)
35849e02b73SAlex Deucher 				udelay(voltage->delay);
35949e02b73SAlex Deucher 		}
36049e02b73SAlex Deucher 	}
36149e02b73SAlex Deucher 
36249e02b73SAlex Deucher 	sclk_cntl = RREG32_PLL(SCLK_CNTL);
36349e02b73SAlex Deucher 	sclk_cntl2 = RREG32_PLL(SCLK_CNTL2);
36449e02b73SAlex Deucher 	sclk_cntl2 &= ~REDUCED_SPEED_SCLK_SEL(3);
36549e02b73SAlex Deucher 	sclk_more_cntl = RREG32_PLL(SCLK_MORE_CNTL);
36649e02b73SAlex Deucher 	sclk_more_cntl &= ~VOLTAGE_DELAY_SEL(3);
36749e02b73SAlex Deucher 	if (ps->misc & ATOM_PM_MISCINFO_ASIC_REDUCED_SPEED_SCLK_EN) {
36849e02b73SAlex Deucher 		sclk_more_cntl |= REDUCED_SPEED_SCLK_EN;
36949e02b73SAlex Deucher 		if (ps->misc & ATOM_PM_MISCINFO_DYN_CLK_3D_IDLE)
37049e02b73SAlex Deucher 			sclk_cntl2 |= REDUCED_SPEED_SCLK_MODE;
37149e02b73SAlex Deucher 		else
37249e02b73SAlex Deucher 			sclk_cntl2 &= ~REDUCED_SPEED_SCLK_MODE;
37349e02b73SAlex Deucher 		if (ps->misc & ATOM_PM_MISCINFO_DYNAMIC_CLOCK_DIVIDER_BY_2)
37449e02b73SAlex Deucher 			sclk_cntl2 |= REDUCED_SPEED_SCLK_SEL(0);
37549e02b73SAlex Deucher 		else if (ps->misc & ATOM_PM_MISCINFO_DYNAMIC_CLOCK_DIVIDER_BY_4)
37649e02b73SAlex Deucher 			sclk_cntl2 |= REDUCED_SPEED_SCLK_SEL(2);
37749e02b73SAlex Deucher 	} else
37849e02b73SAlex Deucher 		sclk_more_cntl &= ~REDUCED_SPEED_SCLK_EN;
37949e02b73SAlex Deucher 
38049e02b73SAlex Deucher 	if (ps->misc & ATOM_PM_MISCINFO_ASIC_DYNAMIC_VOLTAGE_EN) {
38149e02b73SAlex Deucher 		sclk_more_cntl |= IO_CG_VOLTAGE_DROP;
38249e02b73SAlex Deucher 		if (voltage->delay) {
38349e02b73SAlex Deucher 			sclk_more_cntl |= VOLTAGE_DROP_SYNC;
38449e02b73SAlex Deucher 			switch (voltage->delay) {
38549e02b73SAlex Deucher 			case 33:
38649e02b73SAlex Deucher 				sclk_more_cntl |= VOLTAGE_DELAY_SEL(0);
38749e02b73SAlex Deucher 				break;
38849e02b73SAlex Deucher 			case 66:
38949e02b73SAlex Deucher 				sclk_more_cntl |= VOLTAGE_DELAY_SEL(1);
39049e02b73SAlex Deucher 				break;
39149e02b73SAlex Deucher 			case 99:
39249e02b73SAlex Deucher 				sclk_more_cntl |= VOLTAGE_DELAY_SEL(2);
39349e02b73SAlex Deucher 				break;
39449e02b73SAlex Deucher 			case 132:
39549e02b73SAlex Deucher 				sclk_more_cntl |= VOLTAGE_DELAY_SEL(3);
39649e02b73SAlex Deucher 				break;
39749e02b73SAlex Deucher 			}
39849e02b73SAlex Deucher 		} else
39949e02b73SAlex Deucher 			sclk_more_cntl &= ~VOLTAGE_DROP_SYNC;
40049e02b73SAlex Deucher 	} else
40149e02b73SAlex Deucher 		sclk_more_cntl &= ~IO_CG_VOLTAGE_DROP;
40249e02b73SAlex Deucher 
40349e02b73SAlex Deucher 	if (ps->misc & ATOM_PM_MISCINFO_DYNAMIC_HDP_BLOCK_EN)
40449e02b73SAlex Deucher 		sclk_cntl &= ~FORCE_HDP;
40549e02b73SAlex Deucher 	else
40649e02b73SAlex Deucher 		sclk_cntl |= FORCE_HDP;
40749e02b73SAlex Deucher 
40849e02b73SAlex Deucher 	WREG32_PLL(SCLK_CNTL, sclk_cntl);
40949e02b73SAlex Deucher 	WREG32_PLL(SCLK_CNTL2, sclk_cntl2);
41049e02b73SAlex Deucher 	WREG32_PLL(SCLK_MORE_CNTL, sclk_more_cntl);
41149e02b73SAlex Deucher 
41249e02b73SAlex Deucher 	/* set pcie lanes */
41349e02b73SAlex Deucher 	if ((rdev->flags & RADEON_IS_PCIE) &&
41449e02b73SAlex Deucher 	    !(rdev->flags & RADEON_IS_IGP) &&
41549e02b73SAlex Deucher 	    rdev->asic->set_pcie_lanes &&
41649e02b73SAlex Deucher 	    (ps->pcie_lanes !=
41749e02b73SAlex Deucher 	     rdev->pm.power_state[rdev->pm.current_power_state_index].pcie_lanes)) {
41849e02b73SAlex Deucher 		radeon_set_pcie_lanes(rdev,
41949e02b73SAlex Deucher 				      ps->pcie_lanes);
420d9fdaafbSDave Airlie 		DRM_DEBUG_DRIVER("Setting: p: %d\n", ps->pcie_lanes);
42149e02b73SAlex Deucher 	}
42249e02b73SAlex Deucher }
42349e02b73SAlex Deucher 
42449e02b73SAlex Deucher void r100_pm_prepare(struct radeon_device *rdev)
42549e02b73SAlex Deucher {
42649e02b73SAlex Deucher 	struct drm_device *ddev = rdev->ddev;
42749e02b73SAlex Deucher 	struct drm_crtc *crtc;
42849e02b73SAlex Deucher 	struct radeon_crtc *radeon_crtc;
42949e02b73SAlex Deucher 	u32 tmp;
43049e02b73SAlex Deucher 
43149e02b73SAlex Deucher 	/* disable any active CRTCs */
43249e02b73SAlex Deucher 	list_for_each_entry(crtc, &ddev->mode_config.crtc_list, head) {
43349e02b73SAlex Deucher 		radeon_crtc = to_radeon_crtc(crtc);
43449e02b73SAlex Deucher 		if (radeon_crtc->enabled) {
43549e02b73SAlex Deucher 			if (radeon_crtc->crtc_id) {
43649e02b73SAlex Deucher 				tmp = RREG32(RADEON_CRTC2_GEN_CNTL);
43749e02b73SAlex Deucher 				tmp |= RADEON_CRTC2_DISP_REQ_EN_B;
43849e02b73SAlex Deucher 				WREG32(RADEON_CRTC2_GEN_CNTL, tmp);
43949e02b73SAlex Deucher 			} else {
44049e02b73SAlex Deucher 				tmp = RREG32(RADEON_CRTC_GEN_CNTL);
44149e02b73SAlex Deucher 				tmp |= RADEON_CRTC_DISP_REQ_EN_B;
44249e02b73SAlex Deucher 				WREG32(RADEON_CRTC_GEN_CNTL, tmp);
44349e02b73SAlex Deucher 			}
44449e02b73SAlex Deucher 		}
44549e02b73SAlex Deucher 	}
44649e02b73SAlex Deucher }
44749e02b73SAlex Deucher 
44849e02b73SAlex Deucher void r100_pm_finish(struct radeon_device *rdev)
44949e02b73SAlex Deucher {
45049e02b73SAlex Deucher 	struct drm_device *ddev = rdev->ddev;
45149e02b73SAlex Deucher 	struct drm_crtc *crtc;
45249e02b73SAlex Deucher 	struct radeon_crtc *radeon_crtc;
45349e02b73SAlex Deucher 	u32 tmp;
45449e02b73SAlex Deucher 
45549e02b73SAlex Deucher 	/* enable any active CRTCs */
45649e02b73SAlex Deucher 	list_for_each_entry(crtc, &ddev->mode_config.crtc_list, head) {
45749e02b73SAlex Deucher 		radeon_crtc = to_radeon_crtc(crtc);
45849e02b73SAlex Deucher 		if (radeon_crtc->enabled) {
45949e02b73SAlex Deucher 			if (radeon_crtc->crtc_id) {
46049e02b73SAlex Deucher 				tmp = RREG32(RADEON_CRTC2_GEN_CNTL);
46149e02b73SAlex Deucher 				tmp &= ~RADEON_CRTC2_DISP_REQ_EN_B;
46249e02b73SAlex Deucher 				WREG32(RADEON_CRTC2_GEN_CNTL, tmp);
46349e02b73SAlex Deucher 			} else {
46449e02b73SAlex Deucher 				tmp = RREG32(RADEON_CRTC_GEN_CNTL);
46549e02b73SAlex Deucher 				tmp &= ~RADEON_CRTC_DISP_REQ_EN_B;
46649e02b73SAlex Deucher 				WREG32(RADEON_CRTC_GEN_CNTL, tmp);
46749e02b73SAlex Deucher 			}
46849e02b73SAlex Deucher 		}
46949e02b73SAlex Deucher 	}
47049e02b73SAlex Deucher }
47149e02b73SAlex Deucher 
472def9ba9cSAlex Deucher bool r100_gui_idle(struct radeon_device *rdev)
473def9ba9cSAlex Deucher {
474def9ba9cSAlex Deucher 	if (RREG32(RADEON_RBBM_STATUS) & RADEON_RBBM_ACTIVE)
475def9ba9cSAlex Deucher 		return false;
476def9ba9cSAlex Deucher 	else
477def9ba9cSAlex Deucher 		return true;
478def9ba9cSAlex Deucher }
479def9ba9cSAlex Deucher 
48005a05c50SAlex Deucher /* hpd for digital panel detect/disconnect */
48105a05c50SAlex Deucher bool r100_hpd_sense(struct radeon_device *rdev, enum radeon_hpd_id hpd)
48205a05c50SAlex Deucher {
48305a05c50SAlex Deucher 	bool connected = false;
48405a05c50SAlex Deucher 
48505a05c50SAlex Deucher 	switch (hpd) {
48605a05c50SAlex Deucher 	case RADEON_HPD_1:
48705a05c50SAlex Deucher 		if (RREG32(RADEON_FP_GEN_CNTL) & RADEON_FP_DETECT_SENSE)
48805a05c50SAlex Deucher 			connected = true;
48905a05c50SAlex Deucher 		break;
49005a05c50SAlex Deucher 	case RADEON_HPD_2:
49105a05c50SAlex Deucher 		if (RREG32(RADEON_FP2_GEN_CNTL) & RADEON_FP2_DETECT_SENSE)
49205a05c50SAlex Deucher 			connected = true;
49305a05c50SAlex Deucher 		break;
49405a05c50SAlex Deucher 	default:
49505a05c50SAlex Deucher 		break;
49605a05c50SAlex Deucher 	}
49705a05c50SAlex Deucher 	return connected;
49805a05c50SAlex Deucher }
49905a05c50SAlex Deucher 
50005a05c50SAlex Deucher void r100_hpd_set_polarity(struct radeon_device *rdev,
50105a05c50SAlex Deucher 			   enum radeon_hpd_id hpd)
50205a05c50SAlex Deucher {
50305a05c50SAlex Deucher 	u32 tmp;
50405a05c50SAlex Deucher 	bool connected = r100_hpd_sense(rdev, hpd);
50505a05c50SAlex Deucher 
50605a05c50SAlex Deucher 	switch (hpd) {
50705a05c50SAlex Deucher 	case RADEON_HPD_1:
50805a05c50SAlex Deucher 		tmp = RREG32(RADEON_FP_GEN_CNTL);
50905a05c50SAlex Deucher 		if (connected)
51005a05c50SAlex Deucher 			tmp &= ~RADEON_FP_DETECT_INT_POL;
51105a05c50SAlex Deucher 		else
51205a05c50SAlex Deucher 			tmp |= RADEON_FP_DETECT_INT_POL;
51305a05c50SAlex Deucher 		WREG32(RADEON_FP_GEN_CNTL, tmp);
51405a05c50SAlex Deucher 		break;
51505a05c50SAlex Deucher 	case RADEON_HPD_2:
51605a05c50SAlex Deucher 		tmp = RREG32(RADEON_FP2_GEN_CNTL);
51705a05c50SAlex Deucher 		if (connected)
51805a05c50SAlex Deucher 			tmp &= ~RADEON_FP2_DETECT_INT_POL;
51905a05c50SAlex Deucher 		else
52005a05c50SAlex Deucher 			tmp |= RADEON_FP2_DETECT_INT_POL;
52105a05c50SAlex Deucher 		WREG32(RADEON_FP2_GEN_CNTL, tmp);
52205a05c50SAlex Deucher 		break;
52305a05c50SAlex Deucher 	default:
52405a05c50SAlex Deucher 		break;
52505a05c50SAlex Deucher 	}
52605a05c50SAlex Deucher }
52705a05c50SAlex Deucher 
52805a05c50SAlex Deucher void r100_hpd_init(struct radeon_device *rdev)
52905a05c50SAlex Deucher {
53005a05c50SAlex Deucher 	struct drm_device *dev = rdev->ddev;
53105a05c50SAlex Deucher 	struct drm_connector *connector;
53205a05c50SAlex Deucher 
53305a05c50SAlex Deucher 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
53405a05c50SAlex Deucher 		struct radeon_connector *radeon_connector = to_radeon_connector(connector);
53505a05c50SAlex Deucher 		switch (radeon_connector->hpd.hpd) {
53605a05c50SAlex Deucher 		case RADEON_HPD_1:
53705a05c50SAlex Deucher 			rdev->irq.hpd[0] = true;
53805a05c50SAlex Deucher 			break;
53905a05c50SAlex Deucher 		case RADEON_HPD_2:
54005a05c50SAlex Deucher 			rdev->irq.hpd[1] = true;
54105a05c50SAlex Deucher 			break;
54205a05c50SAlex Deucher 		default:
54305a05c50SAlex Deucher 			break;
54405a05c50SAlex Deucher 		}
54564912e99SAlex Deucher 		radeon_hpd_set_polarity(rdev, radeon_connector->hpd.hpd);
54605a05c50SAlex Deucher 	}
547003e69f9SJerome Glisse 	if (rdev->irq.installed)
54805a05c50SAlex Deucher 		r100_irq_set(rdev);
54905a05c50SAlex Deucher }
55005a05c50SAlex Deucher 
55105a05c50SAlex Deucher void r100_hpd_fini(struct radeon_device *rdev)
55205a05c50SAlex Deucher {
55305a05c50SAlex Deucher 	struct drm_device *dev = rdev->ddev;
55405a05c50SAlex Deucher 	struct drm_connector *connector;
55505a05c50SAlex Deucher 
55605a05c50SAlex Deucher 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
55705a05c50SAlex Deucher 		struct radeon_connector *radeon_connector = to_radeon_connector(connector);
55805a05c50SAlex Deucher 		switch (radeon_connector->hpd.hpd) {
55905a05c50SAlex Deucher 		case RADEON_HPD_1:
56005a05c50SAlex Deucher 			rdev->irq.hpd[0] = false;
56105a05c50SAlex Deucher 			break;
56205a05c50SAlex Deucher 		case RADEON_HPD_2:
56305a05c50SAlex Deucher 			rdev->irq.hpd[1] = false;
56405a05c50SAlex Deucher 			break;
56505a05c50SAlex Deucher 		default:
56605a05c50SAlex Deucher 			break;
56705a05c50SAlex Deucher 		}
56805a05c50SAlex Deucher 	}
56905a05c50SAlex Deucher }
57005a05c50SAlex Deucher 
571771fe6b9SJerome Glisse /*
572771fe6b9SJerome Glisse  * PCI GART
573771fe6b9SJerome Glisse  */
574771fe6b9SJerome Glisse void r100_pci_gart_tlb_flush(struct radeon_device *rdev)
575771fe6b9SJerome Glisse {
576771fe6b9SJerome Glisse 	/* TODO: can we do somethings here ? */
577771fe6b9SJerome Glisse 	/* It seems hw only cache one entry so we should discard this
578771fe6b9SJerome Glisse 	 * entry otherwise if first GPU GART read hit this entry it
579771fe6b9SJerome Glisse 	 * could end up in wrong address. */
580771fe6b9SJerome Glisse }
581771fe6b9SJerome Glisse 
5824aac0473SJerome Glisse int r100_pci_gart_init(struct radeon_device *rdev)
5834aac0473SJerome Glisse {
5844aac0473SJerome Glisse 	int r;
5854aac0473SJerome Glisse 
586c9a1be96SJerome Glisse 	if (rdev->gart.ptr) {
587fce7d61bSJoe Perches 		WARN(1, "R100 PCI GART already initialized\n");
5884aac0473SJerome Glisse 		return 0;
5894aac0473SJerome Glisse 	}
5904aac0473SJerome Glisse 	/* Initialize common gart structure */
5914aac0473SJerome Glisse 	r = radeon_gart_init(rdev);
5924aac0473SJerome Glisse 	if (r)
5934aac0473SJerome Glisse 		return r;
5944aac0473SJerome Glisse 	rdev->gart.table_size = rdev->gart.num_gpu_pages * 4;
5954aac0473SJerome Glisse 	rdev->asic->gart_tlb_flush = &r100_pci_gart_tlb_flush;
5964aac0473SJerome Glisse 	rdev->asic->gart_set_page = &r100_pci_gart_set_page;
5974aac0473SJerome Glisse 	return radeon_gart_table_ram_alloc(rdev);
5984aac0473SJerome Glisse }
5994aac0473SJerome Glisse 
60017e15b0cSDave Airlie /* required on r1xx, r2xx, r300, r(v)350, r420/r481, rs400/rs480 */
60117e15b0cSDave Airlie void r100_enable_bm(struct radeon_device *rdev)
60217e15b0cSDave Airlie {
60317e15b0cSDave Airlie 	uint32_t tmp;
60417e15b0cSDave Airlie 	/* Enable bus mastering */
60517e15b0cSDave Airlie 	tmp = RREG32(RADEON_BUS_CNTL) & ~RADEON_BUS_MASTER_DIS;
60617e15b0cSDave Airlie 	WREG32(RADEON_BUS_CNTL, tmp);
60717e15b0cSDave Airlie }
60817e15b0cSDave Airlie 
609771fe6b9SJerome Glisse int r100_pci_gart_enable(struct radeon_device *rdev)
610771fe6b9SJerome Glisse {
611771fe6b9SJerome Glisse 	uint32_t tmp;
612771fe6b9SJerome Glisse 
61382568565SDave Airlie 	radeon_gart_restore(rdev);
614771fe6b9SJerome Glisse 	/* discard memory request outside of configured range */
615771fe6b9SJerome Glisse 	tmp = RREG32(RADEON_AIC_CNTL) | RADEON_DIS_OUT_OF_PCI_GART_ACCESS;
616771fe6b9SJerome Glisse 	WREG32(RADEON_AIC_CNTL, tmp);
617771fe6b9SJerome Glisse 	/* set address range for PCI address translate */
618d594e46aSJerome Glisse 	WREG32(RADEON_AIC_LO_ADDR, rdev->mc.gtt_start);
619d594e46aSJerome Glisse 	WREG32(RADEON_AIC_HI_ADDR, rdev->mc.gtt_end);
620771fe6b9SJerome Glisse 	/* set PCI GART page-table base address */
621771fe6b9SJerome Glisse 	WREG32(RADEON_AIC_PT_BASE, rdev->gart.table_addr);
622771fe6b9SJerome Glisse 	tmp = RREG32(RADEON_AIC_CNTL) | RADEON_PCIGART_TRANSLATE_EN;
623771fe6b9SJerome Glisse 	WREG32(RADEON_AIC_CNTL, tmp);
624771fe6b9SJerome Glisse 	r100_pci_gart_tlb_flush(rdev);
625fcf4de5aSTormod Volden 	DRM_INFO("PCIE GART of %uM enabled (table at 0x%016llX).\n",
626fcf4de5aSTormod Volden 		 (unsigned)(rdev->mc.gtt_size >> 20),
627fcf4de5aSTormod Volden 		 (unsigned long long)rdev->gart.table_addr);
628771fe6b9SJerome Glisse 	rdev->gart.ready = true;
629771fe6b9SJerome Glisse 	return 0;
630771fe6b9SJerome Glisse }
631771fe6b9SJerome Glisse 
632771fe6b9SJerome Glisse void r100_pci_gart_disable(struct radeon_device *rdev)
633771fe6b9SJerome Glisse {
634771fe6b9SJerome Glisse 	uint32_t tmp;
635771fe6b9SJerome Glisse 
636771fe6b9SJerome Glisse 	/* discard memory request outside of configured range */
637771fe6b9SJerome Glisse 	tmp = RREG32(RADEON_AIC_CNTL) | RADEON_DIS_OUT_OF_PCI_GART_ACCESS;
638771fe6b9SJerome Glisse 	WREG32(RADEON_AIC_CNTL, tmp & ~RADEON_PCIGART_TRANSLATE_EN);
639771fe6b9SJerome Glisse 	WREG32(RADEON_AIC_LO_ADDR, 0);
640771fe6b9SJerome Glisse 	WREG32(RADEON_AIC_HI_ADDR, 0);
641771fe6b9SJerome Glisse }
642771fe6b9SJerome Glisse 
643771fe6b9SJerome Glisse int r100_pci_gart_set_page(struct radeon_device *rdev, int i, uint64_t addr)
644771fe6b9SJerome Glisse {
645c9a1be96SJerome Glisse 	u32 *gtt = rdev->gart.ptr;
646c9a1be96SJerome Glisse 
647771fe6b9SJerome Glisse 	if (i < 0 || i > rdev->gart.num_gpu_pages) {
648771fe6b9SJerome Glisse 		return -EINVAL;
649771fe6b9SJerome Glisse 	}
650c9a1be96SJerome Glisse 	gtt[i] = cpu_to_le32(lower_32_bits(addr));
651771fe6b9SJerome Glisse 	return 0;
652771fe6b9SJerome Glisse }
653771fe6b9SJerome Glisse 
6544aac0473SJerome Glisse void r100_pci_gart_fini(struct radeon_device *rdev)
655771fe6b9SJerome Glisse {
656f9274562SJerome Glisse 	radeon_gart_fini(rdev);
657771fe6b9SJerome Glisse 	r100_pci_gart_disable(rdev);
6584aac0473SJerome Glisse 	radeon_gart_table_ram_free(rdev);
659771fe6b9SJerome Glisse }
660771fe6b9SJerome Glisse 
6617ed220d7SMichel Dänzer int r100_irq_set(struct radeon_device *rdev)
6627ed220d7SMichel Dänzer {
6637ed220d7SMichel Dänzer 	uint32_t tmp = 0;
6647ed220d7SMichel Dänzer 
665003e69f9SJerome Glisse 	if (!rdev->irq.installed) {
666fce7d61bSJoe Perches 		WARN(1, "Can't enable IRQ/MSI because no handler is installed\n");
667003e69f9SJerome Glisse 		WREG32(R_000040_GEN_INT_CNTL, 0);
668003e69f9SJerome Glisse 		return -EINVAL;
669003e69f9SJerome Glisse 	}
6707ed220d7SMichel Dänzer 	if (rdev->irq.sw_int) {
6717ed220d7SMichel Dänzer 		tmp |= RADEON_SW_INT_ENABLE;
6727ed220d7SMichel Dänzer 	}
6732031f77cSAlex Deucher 	if (rdev->irq.gui_idle) {
6742031f77cSAlex Deucher 		tmp |= RADEON_GUI_IDLE_MASK;
6752031f77cSAlex Deucher 	}
6766f34be50SAlex Deucher 	if (rdev->irq.crtc_vblank_int[0] ||
6776f34be50SAlex Deucher 	    rdev->irq.pflip[0]) {
6787ed220d7SMichel Dänzer 		tmp |= RADEON_CRTC_VBLANK_MASK;
6797ed220d7SMichel Dänzer 	}
6806f34be50SAlex Deucher 	if (rdev->irq.crtc_vblank_int[1] ||
6816f34be50SAlex Deucher 	    rdev->irq.pflip[1]) {
6827ed220d7SMichel Dänzer 		tmp |= RADEON_CRTC2_VBLANK_MASK;
6837ed220d7SMichel Dänzer 	}
68405a05c50SAlex Deucher 	if (rdev->irq.hpd[0]) {
68505a05c50SAlex Deucher 		tmp |= RADEON_FP_DETECT_MASK;
68605a05c50SAlex Deucher 	}
68705a05c50SAlex Deucher 	if (rdev->irq.hpd[1]) {
68805a05c50SAlex Deucher 		tmp |= RADEON_FP2_DETECT_MASK;
68905a05c50SAlex Deucher 	}
6907ed220d7SMichel Dänzer 	WREG32(RADEON_GEN_INT_CNTL, tmp);
6917ed220d7SMichel Dänzer 	return 0;
6927ed220d7SMichel Dänzer }
6937ed220d7SMichel Dänzer 
6949f022ddfSJerome Glisse void r100_irq_disable(struct radeon_device *rdev)
6959f022ddfSJerome Glisse {
6969f022ddfSJerome Glisse 	u32 tmp;
6979f022ddfSJerome Glisse 
6989f022ddfSJerome Glisse 	WREG32(R_000040_GEN_INT_CNTL, 0);
6999f022ddfSJerome Glisse 	/* Wait and acknowledge irq */
7009f022ddfSJerome Glisse 	mdelay(1);
7019f022ddfSJerome Glisse 	tmp = RREG32(R_000044_GEN_INT_STATUS);
7029f022ddfSJerome Glisse 	WREG32(R_000044_GEN_INT_STATUS, tmp);
7039f022ddfSJerome Glisse }
7049f022ddfSJerome Glisse 
705cbdd4501SAndi Kleen static uint32_t r100_irq_ack(struct radeon_device *rdev)
7067ed220d7SMichel Dänzer {
7077ed220d7SMichel Dänzer 	uint32_t irqs = RREG32(RADEON_GEN_INT_STATUS);
70805a05c50SAlex Deucher 	uint32_t irq_mask = RADEON_SW_INT_TEST |
70905a05c50SAlex Deucher 		RADEON_CRTC_VBLANK_STAT | RADEON_CRTC2_VBLANK_STAT |
71005a05c50SAlex Deucher 		RADEON_FP_DETECT_STAT | RADEON_FP2_DETECT_STAT;
7117ed220d7SMichel Dänzer 
7122031f77cSAlex Deucher 	/* the interrupt works, but the status bit is permanently asserted */
7132031f77cSAlex Deucher 	if (rdev->irq.gui_idle && radeon_gui_idle(rdev)) {
7142031f77cSAlex Deucher 		if (!rdev->irq.gui_idle_acked)
7152031f77cSAlex Deucher 			irq_mask |= RADEON_GUI_IDLE_STAT;
7162031f77cSAlex Deucher 	}
7172031f77cSAlex Deucher 
7187ed220d7SMichel Dänzer 	if (irqs) {
7197ed220d7SMichel Dänzer 		WREG32(RADEON_GEN_INT_STATUS, irqs);
7207ed220d7SMichel Dänzer 	}
7217ed220d7SMichel Dänzer 	return irqs & irq_mask;
7227ed220d7SMichel Dänzer }
7237ed220d7SMichel Dänzer 
7247ed220d7SMichel Dänzer int r100_irq_process(struct radeon_device *rdev)
7257ed220d7SMichel Dänzer {
7263e5cb98dSAlex Deucher 	uint32_t status, msi_rearm;
727d4877cf2SAlex Deucher 	bool queue_hotplug = false;
7287ed220d7SMichel Dänzer 
7292031f77cSAlex Deucher 	/* reset gui idle ack.  the status bit is broken */
7302031f77cSAlex Deucher 	rdev->irq.gui_idle_acked = false;
7312031f77cSAlex Deucher 
7327ed220d7SMichel Dänzer 	status = r100_irq_ack(rdev);
7337ed220d7SMichel Dänzer 	if (!status) {
7347ed220d7SMichel Dänzer 		return IRQ_NONE;
7357ed220d7SMichel Dänzer 	}
736a513c184SJerome Glisse 	if (rdev->shutdown) {
737a513c184SJerome Glisse 		return IRQ_NONE;
738a513c184SJerome Glisse 	}
7397ed220d7SMichel Dänzer 	while (status) {
7407ed220d7SMichel Dänzer 		/* SW interrupt */
7417ed220d7SMichel Dänzer 		if (status & RADEON_SW_INT_TEST) {
742*7465280cSAlex Deucher 			radeon_fence_process(rdev, RADEON_RING_TYPE_GFX_INDEX);
7437ed220d7SMichel Dänzer 		}
7442031f77cSAlex Deucher 		/* gui idle interrupt */
7452031f77cSAlex Deucher 		if (status & RADEON_GUI_IDLE_STAT) {
7462031f77cSAlex Deucher 			rdev->irq.gui_idle_acked = true;
7472031f77cSAlex Deucher 			rdev->pm.gui_idle = true;
7482031f77cSAlex Deucher 			wake_up(&rdev->irq.idle_queue);
7492031f77cSAlex Deucher 		}
7507ed220d7SMichel Dänzer 		/* Vertical blank interrupts */
7517ed220d7SMichel Dänzer 		if (status & RADEON_CRTC_VBLANK_STAT) {
7526f34be50SAlex Deucher 			if (rdev->irq.crtc_vblank_int[0]) {
7537ed220d7SMichel Dänzer 				drm_handle_vblank(rdev->ddev, 0);
754839461d3SRafał Miłecki 				rdev->pm.vblank_sync = true;
75573a6d3fcSRafał Miłecki 				wake_up(&rdev->irq.vblank_queue);
7567ed220d7SMichel Dänzer 			}
7573e4ea742SMario Kleiner 			if (rdev->irq.pflip[0])
7583e4ea742SMario Kleiner 				radeon_crtc_handle_flip(rdev, 0);
7596f34be50SAlex Deucher 		}
7607ed220d7SMichel Dänzer 		if (status & RADEON_CRTC2_VBLANK_STAT) {
7616f34be50SAlex Deucher 			if (rdev->irq.crtc_vblank_int[1]) {
7627ed220d7SMichel Dänzer 				drm_handle_vblank(rdev->ddev, 1);
763839461d3SRafał Miłecki 				rdev->pm.vblank_sync = true;
76473a6d3fcSRafał Miłecki 				wake_up(&rdev->irq.vblank_queue);
7657ed220d7SMichel Dänzer 			}
7663e4ea742SMario Kleiner 			if (rdev->irq.pflip[1])
7673e4ea742SMario Kleiner 				radeon_crtc_handle_flip(rdev, 1);
7686f34be50SAlex Deucher 		}
76905a05c50SAlex Deucher 		if (status & RADEON_FP_DETECT_STAT) {
770d4877cf2SAlex Deucher 			queue_hotplug = true;
771d4877cf2SAlex Deucher 			DRM_DEBUG("HPD1\n");
77205a05c50SAlex Deucher 		}
77305a05c50SAlex Deucher 		if (status & RADEON_FP2_DETECT_STAT) {
774d4877cf2SAlex Deucher 			queue_hotplug = true;
775d4877cf2SAlex Deucher 			DRM_DEBUG("HPD2\n");
77605a05c50SAlex Deucher 		}
7777ed220d7SMichel Dänzer 		status = r100_irq_ack(rdev);
7787ed220d7SMichel Dänzer 	}
7792031f77cSAlex Deucher 	/* reset gui idle ack.  the status bit is broken */
7802031f77cSAlex Deucher 	rdev->irq.gui_idle_acked = false;
781d4877cf2SAlex Deucher 	if (queue_hotplug)
78232c87fcaSTejun Heo 		schedule_work(&rdev->hotplug_work);
7833e5cb98dSAlex Deucher 	if (rdev->msi_enabled) {
7843e5cb98dSAlex Deucher 		switch (rdev->family) {
7853e5cb98dSAlex Deucher 		case CHIP_RS400:
7863e5cb98dSAlex Deucher 		case CHIP_RS480:
7873e5cb98dSAlex Deucher 			msi_rearm = RREG32(RADEON_AIC_CNTL) & ~RS400_MSI_REARM;
7883e5cb98dSAlex Deucher 			WREG32(RADEON_AIC_CNTL, msi_rearm);
7893e5cb98dSAlex Deucher 			WREG32(RADEON_AIC_CNTL, msi_rearm | RS400_MSI_REARM);
7903e5cb98dSAlex Deucher 			break;
7913e5cb98dSAlex Deucher 		default:
7923e5cb98dSAlex Deucher 			msi_rearm = RREG32(RADEON_MSI_REARM_EN) & ~RV370_MSI_REARM_EN;
7933e5cb98dSAlex Deucher 			WREG32(RADEON_MSI_REARM_EN, msi_rearm);
7943e5cb98dSAlex Deucher 			WREG32(RADEON_MSI_REARM_EN, msi_rearm | RV370_MSI_REARM_EN);
7953e5cb98dSAlex Deucher 			break;
7963e5cb98dSAlex Deucher 		}
7973e5cb98dSAlex Deucher 	}
7987ed220d7SMichel Dänzer 	return IRQ_HANDLED;
7997ed220d7SMichel Dänzer }
8007ed220d7SMichel Dänzer 
8017ed220d7SMichel Dänzer u32 r100_get_vblank_counter(struct radeon_device *rdev, int crtc)
8027ed220d7SMichel Dänzer {
8037ed220d7SMichel Dänzer 	if (crtc == 0)
8047ed220d7SMichel Dänzer 		return RREG32(RADEON_CRTC_CRNT_FRAME);
8057ed220d7SMichel Dänzer 	else
8067ed220d7SMichel Dänzer 		return RREG32(RADEON_CRTC2_CRNT_FRAME);
8077ed220d7SMichel Dänzer }
8087ed220d7SMichel Dänzer 
8099e5b2af7SPauli Nieminen /* Who ever call radeon_fence_emit should call ring_lock and ask
8109e5b2af7SPauli Nieminen  * for enough space (today caller are ib schedule and buffer move) */
811771fe6b9SJerome Glisse void r100_fence_ring_emit(struct radeon_device *rdev,
812771fe6b9SJerome Glisse 			  struct radeon_fence *fence)
813771fe6b9SJerome Glisse {
8149e5b2af7SPauli Nieminen 	/* We have to make sure that caches are flushed before
8159e5b2af7SPauli Nieminen 	 * CPU might read something from VRAM. */
8169e5b2af7SPauli Nieminen 	radeon_ring_write(rdev, PACKET0(RADEON_RB3D_DSTCACHE_CTLSTAT, 0));
8179e5b2af7SPauli Nieminen 	radeon_ring_write(rdev, RADEON_RB3D_DC_FLUSH_ALL);
8189e5b2af7SPauli Nieminen 	radeon_ring_write(rdev, PACKET0(RADEON_RB3D_ZCACHE_CTLSTAT, 0));
8199e5b2af7SPauli Nieminen 	radeon_ring_write(rdev, RADEON_RB3D_ZC_FLUSH_ALL);
820771fe6b9SJerome Glisse 	/* Wait until IDLE & CLEAN */
8214612dc97SAlex Deucher 	radeon_ring_write(rdev, PACKET0(RADEON_WAIT_UNTIL, 0));
8224612dc97SAlex Deucher 	radeon_ring_write(rdev, RADEON_WAIT_2D_IDLECLEAN | RADEON_WAIT_3D_IDLECLEAN);
823cafe6609SJerome Glisse 	radeon_ring_write(rdev, PACKET0(RADEON_HOST_PATH_CNTL, 0));
824cafe6609SJerome Glisse 	radeon_ring_write(rdev, rdev->config.r100.hdp_cntl |
825cafe6609SJerome Glisse 				RADEON_HDP_READ_BUFFER_INVALIDATE);
826cafe6609SJerome Glisse 	radeon_ring_write(rdev, PACKET0(RADEON_HOST_PATH_CNTL, 0));
827cafe6609SJerome Glisse 	radeon_ring_write(rdev, rdev->config.r100.hdp_cntl);
828771fe6b9SJerome Glisse 	/* Emit fence sequence & fire IRQ */
829*7465280cSAlex Deucher 	radeon_ring_write(rdev, PACKET0(rdev->fence_drv[fence->ring].scratch_reg, 0));
830771fe6b9SJerome Glisse 	radeon_ring_write(rdev, fence->seq);
831771fe6b9SJerome Glisse 	radeon_ring_write(rdev, PACKET0(RADEON_GEN_INT_STATUS, 0));
832771fe6b9SJerome Glisse 	radeon_ring_write(rdev, RADEON_SW_INT_FIRE);
833771fe6b9SJerome Glisse }
834771fe6b9SJerome Glisse 
835771fe6b9SJerome Glisse int r100_copy_blit(struct radeon_device *rdev,
836771fe6b9SJerome Glisse 		   uint64_t src_offset,
837771fe6b9SJerome Glisse 		   uint64_t dst_offset,
838003cefe0SAlex Deucher 		   unsigned num_gpu_pages,
839771fe6b9SJerome Glisse 		   struct radeon_fence *fence)
840771fe6b9SJerome Glisse {
841771fe6b9SJerome Glisse 	uint32_t cur_pages;
842003cefe0SAlex Deucher 	uint32_t stride_bytes = RADEON_GPU_PAGE_SIZE;
843771fe6b9SJerome Glisse 	uint32_t pitch;
844771fe6b9SJerome Glisse 	uint32_t stride_pixels;
845771fe6b9SJerome Glisse 	unsigned ndw;
846771fe6b9SJerome Glisse 	int num_loops;
847771fe6b9SJerome Glisse 	int r = 0;
848771fe6b9SJerome Glisse 
849771fe6b9SJerome Glisse 	/* radeon limited to 16k stride */
850771fe6b9SJerome Glisse 	stride_bytes &= 0x3fff;
851771fe6b9SJerome Glisse 	/* radeon pitch is /64 */
852771fe6b9SJerome Glisse 	pitch = stride_bytes / 64;
853771fe6b9SJerome Glisse 	stride_pixels = stride_bytes / 4;
854003cefe0SAlex Deucher 	num_loops = DIV_ROUND_UP(num_gpu_pages, 8191);
855771fe6b9SJerome Glisse 
856771fe6b9SJerome Glisse 	/* Ask for enough room for blit + flush + fence */
857771fe6b9SJerome Glisse 	ndw = 64 + (10 * num_loops);
858771fe6b9SJerome Glisse 	r = radeon_ring_lock(rdev, ndw);
859771fe6b9SJerome Glisse 	if (r) {
860771fe6b9SJerome Glisse 		DRM_ERROR("radeon: moving bo (%d) asking for %u dw.\n", r, ndw);
861771fe6b9SJerome Glisse 		return -EINVAL;
862771fe6b9SJerome Glisse 	}
863003cefe0SAlex Deucher 	while (num_gpu_pages > 0) {
864003cefe0SAlex Deucher 		cur_pages = num_gpu_pages;
865771fe6b9SJerome Glisse 		if (cur_pages > 8191) {
866771fe6b9SJerome Glisse 			cur_pages = 8191;
867771fe6b9SJerome Glisse 		}
868003cefe0SAlex Deucher 		num_gpu_pages -= cur_pages;
869771fe6b9SJerome Glisse 
870771fe6b9SJerome Glisse 		/* pages are in Y direction - height
871771fe6b9SJerome Glisse 		   page width in X direction - width */
872771fe6b9SJerome Glisse 		radeon_ring_write(rdev, PACKET3(PACKET3_BITBLT_MULTI, 8));
873771fe6b9SJerome Glisse 		radeon_ring_write(rdev,
874771fe6b9SJerome Glisse 				  RADEON_GMC_SRC_PITCH_OFFSET_CNTL |
875771fe6b9SJerome Glisse 				  RADEON_GMC_DST_PITCH_OFFSET_CNTL |
876771fe6b9SJerome Glisse 				  RADEON_GMC_SRC_CLIPPING |
877771fe6b9SJerome Glisse 				  RADEON_GMC_DST_CLIPPING |
878771fe6b9SJerome Glisse 				  RADEON_GMC_BRUSH_NONE |
879771fe6b9SJerome Glisse 				  (RADEON_COLOR_FORMAT_ARGB8888 << 8) |
880771fe6b9SJerome Glisse 				  RADEON_GMC_SRC_DATATYPE_COLOR |
881771fe6b9SJerome Glisse 				  RADEON_ROP3_S |
882771fe6b9SJerome Glisse 				  RADEON_DP_SRC_SOURCE_MEMORY |
883771fe6b9SJerome Glisse 				  RADEON_GMC_CLR_CMP_CNTL_DIS |
884771fe6b9SJerome Glisse 				  RADEON_GMC_WR_MSK_DIS);
885771fe6b9SJerome Glisse 		radeon_ring_write(rdev, (pitch << 22) | (src_offset >> 10));
886771fe6b9SJerome Glisse 		radeon_ring_write(rdev, (pitch << 22) | (dst_offset >> 10));
887771fe6b9SJerome Glisse 		radeon_ring_write(rdev, (0x1fff) | (0x1fff << 16));
888771fe6b9SJerome Glisse 		radeon_ring_write(rdev, 0);
889771fe6b9SJerome Glisse 		radeon_ring_write(rdev, (0x1fff) | (0x1fff << 16));
890d9ad77ebSDave Airlie 		radeon_ring_write(rdev, num_gpu_pages);
891d9ad77ebSDave Airlie 		radeon_ring_write(rdev, num_gpu_pages);
892771fe6b9SJerome Glisse 		radeon_ring_write(rdev, cur_pages | (stride_pixels << 16));
893771fe6b9SJerome Glisse 	}
894771fe6b9SJerome Glisse 	radeon_ring_write(rdev, PACKET0(RADEON_DSTCACHE_CTLSTAT, 0));
895771fe6b9SJerome Glisse 	radeon_ring_write(rdev, RADEON_RB2D_DC_FLUSH_ALL);
896771fe6b9SJerome Glisse 	radeon_ring_write(rdev, PACKET0(RADEON_WAIT_UNTIL, 0));
897771fe6b9SJerome Glisse 	radeon_ring_write(rdev,
898771fe6b9SJerome Glisse 			  RADEON_WAIT_2D_IDLECLEAN |
899771fe6b9SJerome Glisse 			  RADEON_WAIT_HOST_IDLECLEAN |
900771fe6b9SJerome Glisse 			  RADEON_WAIT_DMA_GUI_IDLE);
901771fe6b9SJerome Glisse 	if (fence) {
902771fe6b9SJerome Glisse 		r = radeon_fence_emit(rdev, fence);
903771fe6b9SJerome Glisse 	}
904771fe6b9SJerome Glisse 	radeon_ring_unlock_commit(rdev);
905771fe6b9SJerome Glisse 	return r;
906771fe6b9SJerome Glisse }
907771fe6b9SJerome Glisse 
90845600232SJerome Glisse static int r100_cp_wait_for_idle(struct radeon_device *rdev)
90945600232SJerome Glisse {
91045600232SJerome Glisse 	unsigned i;
91145600232SJerome Glisse 	u32 tmp;
91245600232SJerome Glisse 
91345600232SJerome Glisse 	for (i = 0; i < rdev->usec_timeout; i++) {
91445600232SJerome Glisse 		tmp = RREG32(R_000E40_RBBM_STATUS);
91545600232SJerome Glisse 		if (!G_000E40_CP_CMDSTRM_BUSY(tmp)) {
91645600232SJerome Glisse 			return 0;
91745600232SJerome Glisse 		}
91845600232SJerome Glisse 		udelay(1);
91945600232SJerome Glisse 	}
92045600232SJerome Glisse 	return -1;
92145600232SJerome Glisse }
92245600232SJerome Glisse 
923771fe6b9SJerome Glisse void r100_ring_start(struct radeon_device *rdev)
924771fe6b9SJerome Glisse {
925771fe6b9SJerome Glisse 	int r;
926771fe6b9SJerome Glisse 
927771fe6b9SJerome Glisse 	r = radeon_ring_lock(rdev, 2);
928771fe6b9SJerome Glisse 	if (r) {
929771fe6b9SJerome Glisse 		return;
930771fe6b9SJerome Glisse 	}
931771fe6b9SJerome Glisse 	radeon_ring_write(rdev, PACKET0(RADEON_ISYNC_CNTL, 0));
932771fe6b9SJerome Glisse 	radeon_ring_write(rdev,
933771fe6b9SJerome Glisse 			  RADEON_ISYNC_ANY2D_IDLE3D |
934771fe6b9SJerome Glisse 			  RADEON_ISYNC_ANY3D_IDLE2D |
935771fe6b9SJerome Glisse 			  RADEON_ISYNC_WAIT_IDLEGUI |
936771fe6b9SJerome Glisse 			  RADEON_ISYNC_CPSCRATCH_IDLEGUI);
937771fe6b9SJerome Glisse 	radeon_ring_unlock_commit(rdev);
938771fe6b9SJerome Glisse }
939771fe6b9SJerome Glisse 
94070967ab9SBen Hutchings 
94170967ab9SBen Hutchings /* Load the microcode for the CP */
94270967ab9SBen Hutchings static int r100_cp_init_microcode(struct radeon_device *rdev)
943771fe6b9SJerome Glisse {
94470967ab9SBen Hutchings 	struct platform_device *pdev;
94570967ab9SBen Hutchings 	const char *fw_name = NULL;
94670967ab9SBen Hutchings 	int err;
947771fe6b9SJerome Glisse 
948d9fdaafbSDave Airlie 	DRM_DEBUG_KMS("\n");
94970967ab9SBen Hutchings 
95070967ab9SBen Hutchings 	pdev = platform_device_register_simple("radeon_cp", 0, NULL, 0);
95170967ab9SBen Hutchings 	err = IS_ERR(pdev);
95270967ab9SBen Hutchings 	if (err) {
95370967ab9SBen Hutchings 		printk(KERN_ERR "radeon_cp: Failed to register firmware\n");
95470967ab9SBen Hutchings 		return -EINVAL;
955771fe6b9SJerome Glisse 	}
956771fe6b9SJerome Glisse 	if ((rdev->family == CHIP_R100) || (rdev->family == CHIP_RV100) ||
957771fe6b9SJerome Glisse 	    (rdev->family == CHIP_RV200) || (rdev->family == CHIP_RS100) ||
958771fe6b9SJerome Glisse 	    (rdev->family == CHIP_RS200)) {
959771fe6b9SJerome Glisse 		DRM_INFO("Loading R100 Microcode\n");
96070967ab9SBen Hutchings 		fw_name = FIRMWARE_R100;
961771fe6b9SJerome Glisse 	} else if ((rdev->family == CHIP_R200) ||
962771fe6b9SJerome Glisse 		   (rdev->family == CHIP_RV250) ||
963771fe6b9SJerome Glisse 		   (rdev->family == CHIP_RV280) ||
964771fe6b9SJerome Glisse 		   (rdev->family == CHIP_RS300)) {
965771fe6b9SJerome Glisse 		DRM_INFO("Loading R200 Microcode\n");
96670967ab9SBen Hutchings 		fw_name = FIRMWARE_R200;
967771fe6b9SJerome Glisse 	} else if ((rdev->family == CHIP_R300) ||
968771fe6b9SJerome Glisse 		   (rdev->family == CHIP_R350) ||
969771fe6b9SJerome Glisse 		   (rdev->family == CHIP_RV350) ||
970771fe6b9SJerome Glisse 		   (rdev->family == CHIP_RV380) ||
971771fe6b9SJerome Glisse 		   (rdev->family == CHIP_RS400) ||
972771fe6b9SJerome Glisse 		   (rdev->family == CHIP_RS480)) {
973771fe6b9SJerome Glisse 		DRM_INFO("Loading R300 Microcode\n");
97470967ab9SBen Hutchings 		fw_name = FIRMWARE_R300;
975771fe6b9SJerome Glisse 	} else if ((rdev->family == CHIP_R420) ||
976771fe6b9SJerome Glisse 		   (rdev->family == CHIP_R423) ||
977771fe6b9SJerome Glisse 		   (rdev->family == CHIP_RV410)) {
978771fe6b9SJerome Glisse 		DRM_INFO("Loading R400 Microcode\n");
97970967ab9SBen Hutchings 		fw_name = FIRMWARE_R420;
980771fe6b9SJerome Glisse 	} else if ((rdev->family == CHIP_RS690) ||
981771fe6b9SJerome Glisse 		   (rdev->family == CHIP_RS740)) {
982771fe6b9SJerome Glisse 		DRM_INFO("Loading RS690/RS740 Microcode\n");
98370967ab9SBen Hutchings 		fw_name = FIRMWARE_RS690;
984771fe6b9SJerome Glisse 	} else if (rdev->family == CHIP_RS600) {
985771fe6b9SJerome Glisse 		DRM_INFO("Loading RS600 Microcode\n");
98670967ab9SBen Hutchings 		fw_name = FIRMWARE_RS600;
987771fe6b9SJerome Glisse 	} else if ((rdev->family == CHIP_RV515) ||
988771fe6b9SJerome Glisse 		   (rdev->family == CHIP_R520) ||
989771fe6b9SJerome Glisse 		   (rdev->family == CHIP_RV530) ||
990771fe6b9SJerome Glisse 		   (rdev->family == CHIP_R580) ||
991771fe6b9SJerome Glisse 		   (rdev->family == CHIP_RV560) ||
992771fe6b9SJerome Glisse 		   (rdev->family == CHIP_RV570)) {
993771fe6b9SJerome Glisse 		DRM_INFO("Loading R500 Microcode\n");
99470967ab9SBen Hutchings 		fw_name = FIRMWARE_R520;
99570967ab9SBen Hutchings 	}
99670967ab9SBen Hutchings 
9973ce0a23dSJerome Glisse 	err = request_firmware(&rdev->me_fw, fw_name, &pdev->dev);
99870967ab9SBen Hutchings 	platform_device_unregister(pdev);
99970967ab9SBen Hutchings 	if (err) {
100070967ab9SBen Hutchings 		printk(KERN_ERR "radeon_cp: Failed to load firmware \"%s\"\n",
100170967ab9SBen Hutchings 		       fw_name);
10023ce0a23dSJerome Glisse 	} else if (rdev->me_fw->size % 8) {
100370967ab9SBen Hutchings 		printk(KERN_ERR
100470967ab9SBen Hutchings 		       "radeon_cp: Bogus length %zu in firmware \"%s\"\n",
10053ce0a23dSJerome Glisse 		       rdev->me_fw->size, fw_name);
100670967ab9SBen Hutchings 		err = -EINVAL;
10073ce0a23dSJerome Glisse 		release_firmware(rdev->me_fw);
10083ce0a23dSJerome Glisse 		rdev->me_fw = NULL;
100970967ab9SBen Hutchings 	}
101070967ab9SBen Hutchings 	return err;
101170967ab9SBen Hutchings }
1012d4550907SJerome Glisse 
101370967ab9SBen Hutchings static void r100_cp_load_microcode(struct radeon_device *rdev)
101470967ab9SBen Hutchings {
101570967ab9SBen Hutchings 	const __be32 *fw_data;
101670967ab9SBen Hutchings 	int i, size;
101770967ab9SBen Hutchings 
101870967ab9SBen Hutchings 	if (r100_gui_wait_for_idle(rdev)) {
101970967ab9SBen Hutchings 		printk(KERN_WARNING "Failed to wait GUI idle while "
102070967ab9SBen Hutchings 		       "programming pipes. Bad things might happen.\n");
102170967ab9SBen Hutchings 	}
102270967ab9SBen Hutchings 
10233ce0a23dSJerome Glisse 	if (rdev->me_fw) {
10243ce0a23dSJerome Glisse 		size = rdev->me_fw->size / 4;
10253ce0a23dSJerome Glisse 		fw_data = (const __be32 *)&rdev->me_fw->data[0];
102670967ab9SBen Hutchings 		WREG32(RADEON_CP_ME_RAM_ADDR, 0);
102770967ab9SBen Hutchings 		for (i = 0; i < size; i += 2) {
102870967ab9SBen Hutchings 			WREG32(RADEON_CP_ME_RAM_DATAH,
102970967ab9SBen Hutchings 			       be32_to_cpup(&fw_data[i]));
103070967ab9SBen Hutchings 			WREG32(RADEON_CP_ME_RAM_DATAL,
103170967ab9SBen Hutchings 			       be32_to_cpup(&fw_data[i + 1]));
1032771fe6b9SJerome Glisse 		}
1033771fe6b9SJerome Glisse 	}
1034771fe6b9SJerome Glisse }
1035771fe6b9SJerome Glisse 
1036771fe6b9SJerome Glisse int r100_cp_init(struct radeon_device *rdev, unsigned ring_size)
1037771fe6b9SJerome Glisse {
1038771fe6b9SJerome Glisse 	unsigned rb_bufsz;
1039771fe6b9SJerome Glisse 	unsigned rb_blksz;
1040771fe6b9SJerome Glisse 	unsigned max_fetch;
1041771fe6b9SJerome Glisse 	unsigned pre_write_timer;
1042771fe6b9SJerome Glisse 	unsigned pre_write_limit;
1043771fe6b9SJerome Glisse 	unsigned indirect2_start;
1044771fe6b9SJerome Glisse 	unsigned indirect1_start;
1045771fe6b9SJerome Glisse 	uint32_t tmp;
1046771fe6b9SJerome Glisse 	int r;
1047771fe6b9SJerome Glisse 
1048771fe6b9SJerome Glisse 	if (r100_debugfs_cp_init(rdev)) {
1049771fe6b9SJerome Glisse 		DRM_ERROR("Failed to register debugfs file for CP !\n");
1050771fe6b9SJerome Glisse 	}
10513ce0a23dSJerome Glisse 	if (!rdev->me_fw) {
105270967ab9SBen Hutchings 		r = r100_cp_init_microcode(rdev);
105370967ab9SBen Hutchings 		if (r) {
105470967ab9SBen Hutchings 			DRM_ERROR("Failed to load firmware!\n");
105570967ab9SBen Hutchings 			return r;
105670967ab9SBen Hutchings 		}
105770967ab9SBen Hutchings 	}
105870967ab9SBen Hutchings 
1059771fe6b9SJerome Glisse 	/* Align ring size */
1060771fe6b9SJerome Glisse 	rb_bufsz = drm_order(ring_size / 8);
1061771fe6b9SJerome Glisse 	ring_size = (1 << (rb_bufsz + 1)) * 4;
1062771fe6b9SJerome Glisse 	r100_cp_load_microcode(rdev);
1063771fe6b9SJerome Glisse 	r = radeon_ring_init(rdev, ring_size);
1064771fe6b9SJerome Glisse 	if (r) {
1065771fe6b9SJerome Glisse 		return r;
1066771fe6b9SJerome Glisse 	}
1067771fe6b9SJerome Glisse 	/* Each time the cp read 1024 bytes (16 dword/quadword) update
1068771fe6b9SJerome Glisse 	 * the rptr copy in system ram */
1069771fe6b9SJerome Glisse 	rb_blksz = 9;
1070771fe6b9SJerome Glisse 	/* cp will read 128bytes at a time (4 dwords) */
1071771fe6b9SJerome Glisse 	max_fetch = 1;
1072771fe6b9SJerome Glisse 	rdev->cp.align_mask = 16 - 1;
1073771fe6b9SJerome Glisse 	/* Write to CP_RB_WPTR will be delayed for pre_write_timer clocks */
1074771fe6b9SJerome Glisse 	pre_write_timer = 64;
1075771fe6b9SJerome Glisse 	/* Force CP_RB_WPTR write if written more than one time before the
1076771fe6b9SJerome Glisse 	 * delay expire
1077771fe6b9SJerome Glisse 	 */
1078771fe6b9SJerome Glisse 	pre_write_limit = 0;
1079771fe6b9SJerome Glisse 	/* Setup the cp cache like this (cache size is 96 dwords) :
1080771fe6b9SJerome Glisse 	 *	RING		0  to 15
1081771fe6b9SJerome Glisse 	 *	INDIRECT1	16 to 79
1082771fe6b9SJerome Glisse 	 *	INDIRECT2	80 to 95
1083771fe6b9SJerome Glisse 	 * So ring cache size is 16dwords (> (2 * max_fetch = 2 * 4dwords))
1084771fe6b9SJerome Glisse 	 *    indirect1 cache size is 64dwords (> (2 * max_fetch = 2 * 4dwords))
1085771fe6b9SJerome Glisse 	 *    indirect2 cache size is 16dwords (> (2 * max_fetch = 2 * 4dwords))
1086771fe6b9SJerome Glisse 	 * Idea being that most of the gpu cmd will be through indirect1 buffer
1087771fe6b9SJerome Glisse 	 * so it gets the bigger cache.
1088771fe6b9SJerome Glisse 	 */
1089771fe6b9SJerome Glisse 	indirect2_start = 80;
1090771fe6b9SJerome Glisse 	indirect1_start = 16;
1091771fe6b9SJerome Glisse 	/* cp setup */
1092771fe6b9SJerome Glisse 	WREG32(0x718, pre_write_timer | (pre_write_limit << 28));
1093d6f28938SAlex Deucher 	tmp = (REG_SET(RADEON_RB_BUFSZ, rb_bufsz) |
1094771fe6b9SJerome Glisse 	       REG_SET(RADEON_RB_BLKSZ, rb_blksz) |
1095724c80e1SAlex Deucher 	       REG_SET(RADEON_MAX_FETCH, max_fetch));
1096d6f28938SAlex Deucher #ifdef __BIG_ENDIAN
1097d6f28938SAlex Deucher 	tmp |= RADEON_BUF_SWAP_32BIT;
1098d6f28938SAlex Deucher #endif
1099724c80e1SAlex Deucher 	WREG32(RADEON_CP_RB_CNTL, tmp | RADEON_RB_NO_UPDATE);
1100d6f28938SAlex Deucher 
1101771fe6b9SJerome Glisse 	/* Set ring address */
1102771fe6b9SJerome Glisse 	DRM_INFO("radeon: ring at 0x%016lX\n", (unsigned long)rdev->cp.gpu_addr);
1103771fe6b9SJerome Glisse 	WREG32(RADEON_CP_RB_BASE, rdev->cp.gpu_addr);
1104771fe6b9SJerome Glisse 	/* Force read & write ptr to 0 */
1105724c80e1SAlex Deucher 	WREG32(RADEON_CP_RB_CNTL, tmp | RADEON_RB_RPTR_WR_ENA | RADEON_RB_NO_UPDATE);
1106771fe6b9SJerome Glisse 	WREG32(RADEON_CP_RB_RPTR_WR, 0);
110787463ff8SMichel Dänzer 	rdev->cp.wptr = 0;
110887463ff8SMichel Dänzer 	WREG32(RADEON_CP_RB_WPTR, rdev->cp.wptr);
1109724c80e1SAlex Deucher 
1110724c80e1SAlex Deucher 	/* set the wb address whether it's enabled or not */
1111724c80e1SAlex Deucher 	WREG32(R_00070C_CP_RB_RPTR_ADDR,
1112724c80e1SAlex Deucher 		S_00070C_RB_RPTR_ADDR((rdev->wb.gpu_addr + RADEON_WB_CP_RPTR_OFFSET) >> 2));
1113724c80e1SAlex Deucher 	WREG32(R_000774_SCRATCH_ADDR, rdev->wb.gpu_addr + RADEON_WB_SCRATCH_OFFSET);
1114724c80e1SAlex Deucher 
1115724c80e1SAlex Deucher 	if (rdev->wb.enabled)
1116724c80e1SAlex Deucher 		WREG32(R_000770_SCRATCH_UMSK, 0xff);
1117724c80e1SAlex Deucher 	else {
1118724c80e1SAlex Deucher 		tmp |= RADEON_RB_NO_UPDATE;
1119724c80e1SAlex Deucher 		WREG32(R_000770_SCRATCH_UMSK, 0);
1120724c80e1SAlex Deucher 	}
1121724c80e1SAlex Deucher 
1122771fe6b9SJerome Glisse 	WREG32(RADEON_CP_RB_CNTL, tmp);
1123771fe6b9SJerome Glisse 	udelay(10);
1124771fe6b9SJerome Glisse 	rdev->cp.rptr = RREG32(RADEON_CP_RB_RPTR);
1125771fe6b9SJerome Glisse 	/* Set cp mode to bus mastering & enable cp*/
1126771fe6b9SJerome Glisse 	WREG32(RADEON_CP_CSQ_MODE,
1127771fe6b9SJerome Glisse 	       REG_SET(RADEON_INDIRECT2_START, indirect2_start) |
1128771fe6b9SJerome Glisse 	       REG_SET(RADEON_INDIRECT1_START, indirect1_start));
1129d75ee3beSAlex Deucher 	WREG32(RADEON_CP_RB_WPTR_DELAY, 0);
1130d75ee3beSAlex Deucher 	WREG32(RADEON_CP_CSQ_MODE, 0x00004D4D);
1131771fe6b9SJerome Glisse 	WREG32(RADEON_CP_CSQ_CNTL, RADEON_CSQ_PRIBM_INDBM);
1132771fe6b9SJerome Glisse 	radeon_ring_start(rdev);
1133771fe6b9SJerome Glisse 	r = radeon_ring_test(rdev);
1134771fe6b9SJerome Glisse 	if (r) {
1135771fe6b9SJerome Glisse 		DRM_ERROR("radeon: cp isn't working (%d).\n", r);
1136771fe6b9SJerome Glisse 		return r;
1137771fe6b9SJerome Glisse 	}
1138771fe6b9SJerome Glisse 	rdev->cp.ready = true;
113953595338SDave Airlie 	radeon_ttm_set_active_vram_size(rdev, rdev->mc.real_vram_size);
1140771fe6b9SJerome Glisse 	return 0;
1141771fe6b9SJerome Glisse }
1142771fe6b9SJerome Glisse 
1143771fe6b9SJerome Glisse void r100_cp_fini(struct radeon_device *rdev)
1144771fe6b9SJerome Glisse {
114545600232SJerome Glisse 	if (r100_cp_wait_for_idle(rdev)) {
114645600232SJerome Glisse 		DRM_ERROR("Wait for CP idle timeout, shutting down CP.\n");
114745600232SJerome Glisse 	}
1148771fe6b9SJerome Glisse 	/* Disable ring */
1149a18d7ea1SJerome Glisse 	r100_cp_disable(rdev);
1150771fe6b9SJerome Glisse 	radeon_ring_fini(rdev);
1151771fe6b9SJerome Glisse 	DRM_INFO("radeon: cp finalized\n");
1152771fe6b9SJerome Glisse }
1153771fe6b9SJerome Glisse 
1154771fe6b9SJerome Glisse void r100_cp_disable(struct radeon_device *rdev)
1155771fe6b9SJerome Glisse {
1156771fe6b9SJerome Glisse 	/* Disable ring */
115753595338SDave Airlie 	radeon_ttm_set_active_vram_size(rdev, rdev->mc.visible_vram_size);
1158771fe6b9SJerome Glisse 	rdev->cp.ready = false;
1159771fe6b9SJerome Glisse 	WREG32(RADEON_CP_CSQ_MODE, 0);
1160771fe6b9SJerome Glisse 	WREG32(RADEON_CP_CSQ_CNTL, 0);
1161724c80e1SAlex Deucher 	WREG32(R_000770_SCRATCH_UMSK, 0);
1162771fe6b9SJerome Glisse 	if (r100_gui_wait_for_idle(rdev)) {
1163771fe6b9SJerome Glisse 		printk(KERN_WARNING "Failed to wait GUI idle while "
1164771fe6b9SJerome Glisse 		       "programming pipes. Bad things might happen.\n");
1165771fe6b9SJerome Glisse 	}
1166771fe6b9SJerome Glisse }
1167771fe6b9SJerome Glisse 
11683ce0a23dSJerome Glisse void r100_cp_commit(struct radeon_device *rdev)
11693ce0a23dSJerome Glisse {
11703ce0a23dSJerome Glisse 	WREG32(RADEON_CP_RB_WPTR, rdev->cp.wptr);
11713ce0a23dSJerome Glisse 	(void)RREG32(RADEON_CP_RB_WPTR);
11723ce0a23dSJerome Glisse }
11733ce0a23dSJerome Glisse 
1174771fe6b9SJerome Glisse 
1175771fe6b9SJerome Glisse /*
1176771fe6b9SJerome Glisse  * CS functions
1177771fe6b9SJerome Glisse  */
1178771fe6b9SJerome Glisse int r100_cs_parse_packet0(struct radeon_cs_parser *p,
1179771fe6b9SJerome Glisse 			  struct radeon_cs_packet *pkt,
1180068a117cSJerome Glisse 			  const unsigned *auth, unsigned n,
1181771fe6b9SJerome Glisse 			  radeon_packet0_check_t check)
1182771fe6b9SJerome Glisse {
1183771fe6b9SJerome Glisse 	unsigned reg;
1184771fe6b9SJerome Glisse 	unsigned i, j, m;
1185771fe6b9SJerome Glisse 	unsigned idx;
1186771fe6b9SJerome Glisse 	int r;
1187771fe6b9SJerome Glisse 
1188771fe6b9SJerome Glisse 	idx = pkt->idx + 1;
1189771fe6b9SJerome Glisse 	reg = pkt->reg;
1190068a117cSJerome Glisse 	/* Check that register fall into register range
1191068a117cSJerome Glisse 	 * determined by the number of entry (n) in the
1192068a117cSJerome Glisse 	 * safe register bitmap.
1193068a117cSJerome Glisse 	 */
1194771fe6b9SJerome Glisse 	if (pkt->one_reg_wr) {
1195771fe6b9SJerome Glisse 		if ((reg >> 7) > n) {
1196771fe6b9SJerome Glisse 			return -EINVAL;
1197771fe6b9SJerome Glisse 		}
1198771fe6b9SJerome Glisse 	} else {
1199771fe6b9SJerome Glisse 		if (((reg + (pkt->count << 2)) >> 7) > n) {
1200771fe6b9SJerome Glisse 			return -EINVAL;
1201771fe6b9SJerome Glisse 		}
1202771fe6b9SJerome Glisse 	}
1203771fe6b9SJerome Glisse 	for (i = 0; i <= pkt->count; i++, idx++) {
1204771fe6b9SJerome Glisse 		j = (reg >> 7);
1205771fe6b9SJerome Glisse 		m = 1 << ((reg >> 2) & 31);
1206771fe6b9SJerome Glisse 		if (auth[j] & m) {
1207771fe6b9SJerome Glisse 			r = check(p, pkt, idx, reg);
1208771fe6b9SJerome Glisse 			if (r) {
1209771fe6b9SJerome Glisse 				return r;
1210771fe6b9SJerome Glisse 			}
1211771fe6b9SJerome Glisse 		}
1212771fe6b9SJerome Glisse 		if (pkt->one_reg_wr) {
1213771fe6b9SJerome Glisse 			if (!(auth[j] & m)) {
1214771fe6b9SJerome Glisse 				break;
1215771fe6b9SJerome Glisse 			}
1216771fe6b9SJerome Glisse 		} else {
1217771fe6b9SJerome Glisse 			reg += 4;
1218771fe6b9SJerome Glisse 		}
1219771fe6b9SJerome Glisse 	}
1220771fe6b9SJerome Glisse 	return 0;
1221771fe6b9SJerome Glisse }
1222771fe6b9SJerome Glisse 
1223771fe6b9SJerome Glisse void r100_cs_dump_packet(struct radeon_cs_parser *p,
1224771fe6b9SJerome Glisse 			 struct radeon_cs_packet *pkt)
1225771fe6b9SJerome Glisse {
1226771fe6b9SJerome Glisse 	volatile uint32_t *ib;
1227771fe6b9SJerome Glisse 	unsigned i;
1228771fe6b9SJerome Glisse 	unsigned idx;
1229771fe6b9SJerome Glisse 
1230771fe6b9SJerome Glisse 	ib = p->ib->ptr;
1231771fe6b9SJerome Glisse 	idx = pkt->idx;
1232771fe6b9SJerome Glisse 	for (i = 0; i <= (pkt->count + 1); i++, idx++) {
1233771fe6b9SJerome Glisse 		DRM_INFO("ib[%d]=0x%08X\n", idx, ib[idx]);
1234771fe6b9SJerome Glisse 	}
1235771fe6b9SJerome Glisse }
1236771fe6b9SJerome Glisse 
1237771fe6b9SJerome Glisse /**
1238771fe6b9SJerome Glisse  * r100_cs_packet_parse() - parse cp packet and point ib index to next packet
1239771fe6b9SJerome Glisse  * @parser:	parser structure holding parsing context.
1240771fe6b9SJerome Glisse  * @pkt:	where to store packet informations
1241771fe6b9SJerome Glisse  *
1242771fe6b9SJerome Glisse  * Assume that chunk_ib_index is properly set. Will return -EINVAL
1243771fe6b9SJerome Glisse  * if packet is bigger than remaining ib size. or if packets is unknown.
1244771fe6b9SJerome Glisse  **/
1245771fe6b9SJerome Glisse int r100_cs_packet_parse(struct radeon_cs_parser *p,
1246771fe6b9SJerome Glisse 			 struct radeon_cs_packet *pkt,
1247771fe6b9SJerome Glisse 			 unsigned idx)
1248771fe6b9SJerome Glisse {
1249771fe6b9SJerome Glisse 	struct radeon_cs_chunk *ib_chunk = &p->chunks[p->chunk_ib_idx];
1250fa99239cSRoel Kluin 	uint32_t header;
1251771fe6b9SJerome Glisse 
1252771fe6b9SJerome Glisse 	if (idx >= ib_chunk->length_dw) {
1253771fe6b9SJerome Glisse 		DRM_ERROR("Can not parse packet at %d after CS end %d !\n",
1254771fe6b9SJerome Glisse 			  idx, ib_chunk->length_dw);
1255771fe6b9SJerome Glisse 		return -EINVAL;
1256771fe6b9SJerome Glisse 	}
1257513bcb46SDave Airlie 	header = radeon_get_ib_value(p, idx);
1258771fe6b9SJerome Glisse 	pkt->idx = idx;
1259771fe6b9SJerome Glisse 	pkt->type = CP_PACKET_GET_TYPE(header);
1260771fe6b9SJerome Glisse 	pkt->count = CP_PACKET_GET_COUNT(header);
1261771fe6b9SJerome Glisse 	switch (pkt->type) {
1262771fe6b9SJerome Glisse 	case PACKET_TYPE0:
1263771fe6b9SJerome Glisse 		pkt->reg = CP_PACKET0_GET_REG(header);
1264771fe6b9SJerome Glisse 		pkt->one_reg_wr = CP_PACKET0_GET_ONE_REG_WR(header);
1265771fe6b9SJerome Glisse 		break;
1266771fe6b9SJerome Glisse 	case PACKET_TYPE3:
1267771fe6b9SJerome Glisse 		pkt->opcode = CP_PACKET3_GET_OPCODE(header);
1268771fe6b9SJerome Glisse 		break;
1269771fe6b9SJerome Glisse 	case PACKET_TYPE2:
1270771fe6b9SJerome Glisse 		pkt->count = -1;
1271771fe6b9SJerome Glisse 		break;
1272771fe6b9SJerome Glisse 	default:
1273771fe6b9SJerome Glisse 		DRM_ERROR("Unknown packet type %d at %d !\n", pkt->type, idx);
1274771fe6b9SJerome Glisse 		return -EINVAL;
1275771fe6b9SJerome Glisse 	}
1276771fe6b9SJerome Glisse 	if ((pkt->count + 1 + pkt->idx) >= ib_chunk->length_dw) {
1277771fe6b9SJerome Glisse 		DRM_ERROR("Packet (%d:%d:%d) end after CS buffer (%d) !\n",
1278771fe6b9SJerome Glisse 			  pkt->idx, pkt->type, pkt->count, ib_chunk->length_dw);
1279771fe6b9SJerome Glisse 		return -EINVAL;
1280771fe6b9SJerome Glisse 	}
1281771fe6b9SJerome Glisse 	return 0;
1282771fe6b9SJerome Glisse }
1283771fe6b9SJerome Glisse 
1284771fe6b9SJerome Glisse /**
1285531369e6SDave Airlie  * r100_cs_packet_next_vline() - parse userspace VLINE packet
1286531369e6SDave Airlie  * @parser:		parser structure holding parsing context.
1287531369e6SDave Airlie  *
1288531369e6SDave Airlie  * Userspace sends a special sequence for VLINE waits.
1289531369e6SDave Airlie  * PACKET0 - VLINE_START_END + value
1290531369e6SDave Airlie  * PACKET0 - WAIT_UNTIL +_value
1291531369e6SDave Airlie  * RELOC (P3) - crtc_id in reloc.
1292531369e6SDave Airlie  *
1293531369e6SDave Airlie  * This function parses this and relocates the VLINE START END
1294531369e6SDave Airlie  * and WAIT UNTIL packets to the correct crtc.
1295531369e6SDave Airlie  * It also detects a switched off crtc and nulls out the
1296531369e6SDave Airlie  * wait in that case.
1297531369e6SDave Airlie  */
1298531369e6SDave Airlie int r100_cs_packet_parse_vline(struct radeon_cs_parser *p)
1299531369e6SDave Airlie {
1300531369e6SDave Airlie 	struct drm_mode_object *obj;
1301531369e6SDave Airlie 	struct drm_crtc *crtc;
1302531369e6SDave Airlie 	struct radeon_crtc *radeon_crtc;
1303531369e6SDave Airlie 	struct radeon_cs_packet p3reloc, waitreloc;
1304531369e6SDave Airlie 	int crtc_id;
1305531369e6SDave Airlie 	int r;
1306531369e6SDave Airlie 	uint32_t header, h_idx, reg;
1307513bcb46SDave Airlie 	volatile uint32_t *ib;
1308531369e6SDave Airlie 
1309513bcb46SDave Airlie 	ib = p->ib->ptr;
1310531369e6SDave Airlie 
1311531369e6SDave Airlie 	/* parse the wait until */
1312531369e6SDave Airlie 	r = r100_cs_packet_parse(p, &waitreloc, p->idx);
1313531369e6SDave Airlie 	if (r)
1314531369e6SDave Airlie 		return r;
1315531369e6SDave Airlie 
1316531369e6SDave Airlie 	/* check its a wait until and only 1 count */
1317531369e6SDave Airlie 	if (waitreloc.reg != RADEON_WAIT_UNTIL ||
1318531369e6SDave Airlie 	    waitreloc.count != 0) {
1319531369e6SDave Airlie 		DRM_ERROR("vline wait had illegal wait until segment\n");
1320a3a88a66SPaul Bolle 		return -EINVAL;
1321531369e6SDave Airlie 	}
1322531369e6SDave Airlie 
1323513bcb46SDave Airlie 	if (radeon_get_ib_value(p, waitreloc.idx + 1) != RADEON_WAIT_CRTC_VLINE) {
1324531369e6SDave Airlie 		DRM_ERROR("vline wait had illegal wait until\n");
1325a3a88a66SPaul Bolle 		return -EINVAL;
1326531369e6SDave Airlie 	}
1327531369e6SDave Airlie 
1328531369e6SDave Airlie 	/* jump over the NOP */
132990ebd065SAlex Deucher 	r = r100_cs_packet_parse(p, &p3reloc, p->idx + waitreloc.count + 2);
1330531369e6SDave Airlie 	if (r)
1331531369e6SDave Airlie 		return r;
1332531369e6SDave Airlie 
1333531369e6SDave Airlie 	h_idx = p->idx - 2;
133490ebd065SAlex Deucher 	p->idx += waitreloc.count + 2;
133590ebd065SAlex Deucher 	p->idx += p3reloc.count + 2;
1336531369e6SDave Airlie 
1337513bcb46SDave Airlie 	header = radeon_get_ib_value(p, h_idx);
1338513bcb46SDave Airlie 	crtc_id = radeon_get_ib_value(p, h_idx + 5);
1339d4ac6a05SDave Airlie 	reg = CP_PACKET0_GET_REG(header);
1340531369e6SDave Airlie 	obj = drm_mode_object_find(p->rdev->ddev, crtc_id, DRM_MODE_OBJECT_CRTC);
1341531369e6SDave Airlie 	if (!obj) {
1342531369e6SDave Airlie 		DRM_ERROR("cannot find crtc %d\n", crtc_id);
1343a3a88a66SPaul Bolle 		return -EINVAL;
1344531369e6SDave Airlie 	}
1345531369e6SDave Airlie 	crtc = obj_to_crtc(obj);
1346531369e6SDave Airlie 	radeon_crtc = to_radeon_crtc(crtc);
1347531369e6SDave Airlie 	crtc_id = radeon_crtc->crtc_id;
1348531369e6SDave Airlie 
1349531369e6SDave Airlie 	if (!crtc->enabled) {
1350531369e6SDave Airlie 		/* if the CRTC isn't enabled - we need to nop out the wait until */
1351513bcb46SDave Airlie 		ib[h_idx + 2] = PACKET2(0);
1352513bcb46SDave Airlie 		ib[h_idx + 3] = PACKET2(0);
1353531369e6SDave Airlie 	} else if (crtc_id == 1) {
1354531369e6SDave Airlie 		switch (reg) {
1355531369e6SDave Airlie 		case AVIVO_D1MODE_VLINE_START_END:
135690ebd065SAlex Deucher 			header &= ~R300_CP_PACKET0_REG_MASK;
1357531369e6SDave Airlie 			header |= AVIVO_D2MODE_VLINE_START_END >> 2;
1358531369e6SDave Airlie 			break;
1359531369e6SDave Airlie 		case RADEON_CRTC_GUI_TRIG_VLINE:
136090ebd065SAlex Deucher 			header &= ~R300_CP_PACKET0_REG_MASK;
1361531369e6SDave Airlie 			header |= RADEON_CRTC2_GUI_TRIG_VLINE >> 2;
1362531369e6SDave Airlie 			break;
1363531369e6SDave Airlie 		default:
1364531369e6SDave Airlie 			DRM_ERROR("unknown crtc reloc\n");
1365a3a88a66SPaul Bolle 			return -EINVAL;
1366531369e6SDave Airlie 		}
1367513bcb46SDave Airlie 		ib[h_idx] = header;
1368513bcb46SDave Airlie 		ib[h_idx + 3] |= RADEON_ENG_DISPLAY_SELECT_CRTC1;
1369531369e6SDave Airlie 	}
1370a3a88a66SPaul Bolle 
1371a3a88a66SPaul Bolle 	return 0;
1372531369e6SDave Airlie }
1373531369e6SDave Airlie 
1374531369e6SDave Airlie /**
1375771fe6b9SJerome Glisse  * r100_cs_packet_next_reloc() - parse next packet which should be reloc packet3
1376771fe6b9SJerome Glisse  * @parser:		parser structure holding parsing context.
1377771fe6b9SJerome Glisse  * @data:		pointer to relocation data
1378771fe6b9SJerome Glisse  * @offset_start:	starting offset
1379771fe6b9SJerome Glisse  * @offset_mask:	offset mask (to align start offset on)
1380771fe6b9SJerome Glisse  * @reloc:		reloc informations
1381771fe6b9SJerome Glisse  *
1382771fe6b9SJerome Glisse  * Check next packet is relocation packet3, do bo validation and compute
1383771fe6b9SJerome Glisse  * GPU offset using the provided start.
1384771fe6b9SJerome Glisse  **/
1385771fe6b9SJerome Glisse int r100_cs_packet_next_reloc(struct radeon_cs_parser *p,
1386771fe6b9SJerome Glisse 			      struct radeon_cs_reloc **cs_reloc)
1387771fe6b9SJerome Glisse {
1388771fe6b9SJerome Glisse 	struct radeon_cs_chunk *relocs_chunk;
1389771fe6b9SJerome Glisse 	struct radeon_cs_packet p3reloc;
1390771fe6b9SJerome Glisse 	unsigned idx;
1391771fe6b9SJerome Glisse 	int r;
1392771fe6b9SJerome Glisse 
1393771fe6b9SJerome Glisse 	if (p->chunk_relocs_idx == -1) {
1394771fe6b9SJerome Glisse 		DRM_ERROR("No relocation chunk !\n");
1395771fe6b9SJerome Glisse 		return -EINVAL;
1396771fe6b9SJerome Glisse 	}
1397771fe6b9SJerome Glisse 	*cs_reloc = NULL;
1398771fe6b9SJerome Glisse 	relocs_chunk = &p->chunks[p->chunk_relocs_idx];
1399771fe6b9SJerome Glisse 	r = r100_cs_packet_parse(p, &p3reloc, p->idx);
1400771fe6b9SJerome Glisse 	if (r) {
1401771fe6b9SJerome Glisse 		return r;
1402771fe6b9SJerome Glisse 	}
1403771fe6b9SJerome Glisse 	p->idx += p3reloc.count + 2;
1404771fe6b9SJerome Glisse 	if (p3reloc.type != PACKET_TYPE3 || p3reloc.opcode != PACKET3_NOP) {
1405771fe6b9SJerome Glisse 		DRM_ERROR("No packet3 for relocation for packet at %d.\n",
1406771fe6b9SJerome Glisse 			  p3reloc.idx);
1407771fe6b9SJerome Glisse 		r100_cs_dump_packet(p, &p3reloc);
1408771fe6b9SJerome Glisse 		return -EINVAL;
1409771fe6b9SJerome Glisse 	}
1410513bcb46SDave Airlie 	idx = radeon_get_ib_value(p, p3reloc.idx + 1);
1411771fe6b9SJerome Glisse 	if (idx >= relocs_chunk->length_dw) {
1412771fe6b9SJerome Glisse 		DRM_ERROR("Relocs at %d after relocations chunk end %d !\n",
1413771fe6b9SJerome Glisse 			  idx, relocs_chunk->length_dw);
1414771fe6b9SJerome Glisse 		r100_cs_dump_packet(p, &p3reloc);
1415771fe6b9SJerome Glisse 		return -EINVAL;
1416771fe6b9SJerome Glisse 	}
1417771fe6b9SJerome Glisse 	/* FIXME: we assume reloc size is 4 dwords */
1418771fe6b9SJerome Glisse 	*cs_reloc = p->relocs_ptr[(idx / 4)];
1419771fe6b9SJerome Glisse 	return 0;
1420771fe6b9SJerome Glisse }
1421771fe6b9SJerome Glisse 
1422551ebd83SDave Airlie static int r100_get_vtx_size(uint32_t vtx_fmt)
1423551ebd83SDave Airlie {
1424551ebd83SDave Airlie 	int vtx_size;
1425551ebd83SDave Airlie 	vtx_size = 2;
1426551ebd83SDave Airlie 	/* ordered according to bits in spec */
1427551ebd83SDave Airlie 	if (vtx_fmt & RADEON_SE_VTX_FMT_W0)
1428551ebd83SDave Airlie 		vtx_size++;
1429551ebd83SDave Airlie 	if (vtx_fmt & RADEON_SE_VTX_FMT_FPCOLOR)
1430551ebd83SDave Airlie 		vtx_size += 3;
1431551ebd83SDave Airlie 	if (vtx_fmt & RADEON_SE_VTX_FMT_FPALPHA)
1432551ebd83SDave Airlie 		vtx_size++;
1433551ebd83SDave Airlie 	if (vtx_fmt & RADEON_SE_VTX_FMT_PKCOLOR)
1434551ebd83SDave Airlie 		vtx_size++;
1435551ebd83SDave Airlie 	if (vtx_fmt & RADEON_SE_VTX_FMT_FPSPEC)
1436551ebd83SDave Airlie 		vtx_size += 3;
1437551ebd83SDave Airlie 	if (vtx_fmt & RADEON_SE_VTX_FMT_FPFOG)
1438551ebd83SDave Airlie 		vtx_size++;
1439551ebd83SDave Airlie 	if (vtx_fmt & RADEON_SE_VTX_FMT_PKSPEC)
1440551ebd83SDave Airlie 		vtx_size++;
1441551ebd83SDave Airlie 	if (vtx_fmt & RADEON_SE_VTX_FMT_ST0)
1442551ebd83SDave Airlie 		vtx_size += 2;
1443551ebd83SDave Airlie 	if (vtx_fmt & RADEON_SE_VTX_FMT_ST1)
1444551ebd83SDave Airlie 		vtx_size += 2;
1445551ebd83SDave Airlie 	if (vtx_fmt & RADEON_SE_VTX_FMT_Q1)
1446551ebd83SDave Airlie 		vtx_size++;
1447551ebd83SDave Airlie 	if (vtx_fmt & RADEON_SE_VTX_FMT_ST2)
1448551ebd83SDave Airlie 		vtx_size += 2;
1449551ebd83SDave Airlie 	if (vtx_fmt & RADEON_SE_VTX_FMT_Q2)
1450551ebd83SDave Airlie 		vtx_size++;
1451551ebd83SDave Airlie 	if (vtx_fmt & RADEON_SE_VTX_FMT_ST3)
1452551ebd83SDave Airlie 		vtx_size += 2;
1453551ebd83SDave Airlie 	if (vtx_fmt & RADEON_SE_VTX_FMT_Q3)
1454551ebd83SDave Airlie 		vtx_size++;
1455551ebd83SDave Airlie 	if (vtx_fmt & RADEON_SE_VTX_FMT_Q0)
1456551ebd83SDave Airlie 		vtx_size++;
1457551ebd83SDave Airlie 	/* blend weight */
1458551ebd83SDave Airlie 	if (vtx_fmt & (0x7 << 15))
1459551ebd83SDave Airlie 		vtx_size += (vtx_fmt >> 15) & 0x7;
1460551ebd83SDave Airlie 	if (vtx_fmt & RADEON_SE_VTX_FMT_N0)
1461551ebd83SDave Airlie 		vtx_size += 3;
1462551ebd83SDave Airlie 	if (vtx_fmt & RADEON_SE_VTX_FMT_XY1)
1463551ebd83SDave Airlie 		vtx_size += 2;
1464551ebd83SDave Airlie 	if (vtx_fmt & RADEON_SE_VTX_FMT_Z1)
1465551ebd83SDave Airlie 		vtx_size++;
1466551ebd83SDave Airlie 	if (vtx_fmt & RADEON_SE_VTX_FMT_W1)
1467551ebd83SDave Airlie 		vtx_size++;
1468551ebd83SDave Airlie 	if (vtx_fmt & RADEON_SE_VTX_FMT_N1)
1469551ebd83SDave Airlie 		vtx_size++;
1470551ebd83SDave Airlie 	if (vtx_fmt & RADEON_SE_VTX_FMT_Z)
1471551ebd83SDave Airlie 		vtx_size++;
1472551ebd83SDave Airlie 	return vtx_size;
1473551ebd83SDave Airlie }
1474551ebd83SDave Airlie 
1475771fe6b9SJerome Glisse static int r100_packet0_check(struct radeon_cs_parser *p,
1476551ebd83SDave Airlie 			      struct radeon_cs_packet *pkt,
1477551ebd83SDave Airlie 			      unsigned idx, unsigned reg)
1478771fe6b9SJerome Glisse {
1479771fe6b9SJerome Glisse 	struct radeon_cs_reloc *reloc;
1480551ebd83SDave Airlie 	struct r100_cs_track *track;
1481771fe6b9SJerome Glisse 	volatile uint32_t *ib;
1482771fe6b9SJerome Glisse 	uint32_t tmp;
1483771fe6b9SJerome Glisse 	int r;
1484551ebd83SDave Airlie 	int i, face;
1485e024e110SDave Airlie 	u32 tile_flags = 0;
1486513bcb46SDave Airlie 	u32 idx_value;
1487771fe6b9SJerome Glisse 
1488771fe6b9SJerome Glisse 	ib = p->ib->ptr;
1489551ebd83SDave Airlie 	track = (struct r100_cs_track *)p->track;
1490551ebd83SDave Airlie 
1491513bcb46SDave Airlie 	idx_value = radeon_get_ib_value(p, idx);
1492513bcb46SDave Airlie 
1493771fe6b9SJerome Glisse 	switch (reg) {
1494531369e6SDave Airlie 	case RADEON_CRTC_GUI_TRIG_VLINE:
1495531369e6SDave Airlie 		r = r100_cs_packet_parse_vline(p);
1496531369e6SDave Airlie 		if (r) {
1497531369e6SDave Airlie 			DRM_ERROR("No reloc for ib[%d]=0x%04X\n",
1498531369e6SDave Airlie 				  idx, reg);
1499531369e6SDave Airlie 			r100_cs_dump_packet(p, pkt);
1500531369e6SDave Airlie 			return r;
1501531369e6SDave Airlie 		}
1502531369e6SDave Airlie 		break;
1503771fe6b9SJerome Glisse 		/* FIXME: only allow PACKET3 blit? easier to check for out of
1504771fe6b9SJerome Glisse 		 * range access */
1505771fe6b9SJerome Glisse 	case RADEON_DST_PITCH_OFFSET:
1506771fe6b9SJerome Glisse 	case RADEON_SRC_PITCH_OFFSET:
1507551ebd83SDave Airlie 		r = r100_reloc_pitch_offset(p, pkt, idx, reg);
1508551ebd83SDave Airlie 		if (r)
1509551ebd83SDave Airlie 			return r;
1510551ebd83SDave Airlie 		break;
1511551ebd83SDave Airlie 	case RADEON_RB3D_DEPTHOFFSET:
1512771fe6b9SJerome Glisse 		r = r100_cs_packet_next_reloc(p, &reloc);
1513771fe6b9SJerome Glisse 		if (r) {
1514771fe6b9SJerome Glisse 			DRM_ERROR("No reloc for ib[%d]=0x%04X\n",
1515771fe6b9SJerome Glisse 				  idx, reg);
1516771fe6b9SJerome Glisse 			r100_cs_dump_packet(p, pkt);
1517771fe6b9SJerome Glisse 			return r;
1518771fe6b9SJerome Glisse 		}
1519551ebd83SDave Airlie 		track->zb.robj = reloc->robj;
1520513bcb46SDave Airlie 		track->zb.offset = idx_value;
152140b4a759SMarek Olšák 		track->zb_dirty = true;
1522513bcb46SDave Airlie 		ib[idx] = idx_value + ((u32)reloc->lobj.gpu_offset);
1523771fe6b9SJerome Glisse 		break;
1524771fe6b9SJerome Glisse 	case RADEON_RB3D_COLOROFFSET:
1525551ebd83SDave Airlie 		r = r100_cs_packet_next_reloc(p, &reloc);
1526551ebd83SDave Airlie 		if (r) {
1527551ebd83SDave Airlie 			DRM_ERROR("No reloc for ib[%d]=0x%04X\n",
1528551ebd83SDave Airlie 				  idx, reg);
1529551ebd83SDave Airlie 			r100_cs_dump_packet(p, pkt);
1530551ebd83SDave Airlie 			return r;
1531551ebd83SDave Airlie 		}
1532551ebd83SDave Airlie 		track->cb[0].robj = reloc->robj;
1533513bcb46SDave Airlie 		track->cb[0].offset = idx_value;
153440b4a759SMarek Olšák 		track->cb_dirty = true;
1535513bcb46SDave Airlie 		ib[idx] = idx_value + ((u32)reloc->lobj.gpu_offset);
1536551ebd83SDave Airlie 		break;
1537771fe6b9SJerome Glisse 	case RADEON_PP_TXOFFSET_0:
1538771fe6b9SJerome Glisse 	case RADEON_PP_TXOFFSET_1:
1539771fe6b9SJerome Glisse 	case RADEON_PP_TXOFFSET_2:
1540551ebd83SDave Airlie 		i = (reg - RADEON_PP_TXOFFSET_0) / 24;
1541771fe6b9SJerome Glisse 		r = r100_cs_packet_next_reloc(p, &reloc);
1542771fe6b9SJerome Glisse 		if (r) {
1543771fe6b9SJerome Glisse 			DRM_ERROR("No reloc for ib[%d]=0x%04X\n",
1544771fe6b9SJerome Glisse 				  idx, reg);
1545771fe6b9SJerome Glisse 			r100_cs_dump_packet(p, pkt);
1546771fe6b9SJerome Glisse 			return r;
1547771fe6b9SJerome Glisse 		}
1548513bcb46SDave Airlie 		ib[idx] = idx_value + ((u32)reloc->lobj.gpu_offset);
1549551ebd83SDave Airlie 		track->textures[i].robj = reloc->robj;
155040b4a759SMarek Olšák 		track->tex_dirty = true;
1551771fe6b9SJerome Glisse 		break;
1552551ebd83SDave Airlie 	case RADEON_PP_CUBIC_OFFSET_T0_0:
1553551ebd83SDave Airlie 	case RADEON_PP_CUBIC_OFFSET_T0_1:
1554551ebd83SDave Airlie 	case RADEON_PP_CUBIC_OFFSET_T0_2:
1555551ebd83SDave Airlie 	case RADEON_PP_CUBIC_OFFSET_T0_3:
1556551ebd83SDave Airlie 	case RADEON_PP_CUBIC_OFFSET_T0_4:
1557551ebd83SDave Airlie 		i = (reg - RADEON_PP_CUBIC_OFFSET_T0_0) / 4;
1558551ebd83SDave Airlie 		r = r100_cs_packet_next_reloc(p, &reloc);
1559551ebd83SDave Airlie 		if (r) {
1560551ebd83SDave Airlie 			DRM_ERROR("No reloc for ib[%d]=0x%04X\n",
1561551ebd83SDave Airlie 				  idx, reg);
1562551ebd83SDave Airlie 			r100_cs_dump_packet(p, pkt);
1563551ebd83SDave Airlie 			return r;
1564551ebd83SDave Airlie 		}
1565513bcb46SDave Airlie 		track->textures[0].cube_info[i].offset = idx_value;
1566513bcb46SDave Airlie 		ib[idx] = idx_value + ((u32)reloc->lobj.gpu_offset);
1567551ebd83SDave Airlie 		track->textures[0].cube_info[i].robj = reloc->robj;
156840b4a759SMarek Olšák 		track->tex_dirty = true;
1569551ebd83SDave Airlie 		break;
1570551ebd83SDave Airlie 	case RADEON_PP_CUBIC_OFFSET_T1_0:
1571551ebd83SDave Airlie 	case RADEON_PP_CUBIC_OFFSET_T1_1:
1572551ebd83SDave Airlie 	case RADEON_PP_CUBIC_OFFSET_T1_2:
1573551ebd83SDave Airlie 	case RADEON_PP_CUBIC_OFFSET_T1_3:
1574551ebd83SDave Airlie 	case RADEON_PP_CUBIC_OFFSET_T1_4:
1575551ebd83SDave Airlie 		i = (reg - RADEON_PP_CUBIC_OFFSET_T1_0) / 4;
1576551ebd83SDave Airlie 		r = r100_cs_packet_next_reloc(p, &reloc);
1577551ebd83SDave Airlie 		if (r) {
1578551ebd83SDave Airlie 			DRM_ERROR("No reloc for ib[%d]=0x%04X\n",
1579551ebd83SDave Airlie 				  idx, reg);
1580551ebd83SDave Airlie 			r100_cs_dump_packet(p, pkt);
1581551ebd83SDave Airlie 			return r;
1582551ebd83SDave Airlie 		}
1583513bcb46SDave Airlie 		track->textures[1].cube_info[i].offset = idx_value;
1584513bcb46SDave Airlie 		ib[idx] = idx_value + ((u32)reloc->lobj.gpu_offset);
1585551ebd83SDave Airlie 		track->textures[1].cube_info[i].robj = reloc->robj;
158640b4a759SMarek Olšák 		track->tex_dirty = true;
1587551ebd83SDave Airlie 		break;
1588551ebd83SDave Airlie 	case RADEON_PP_CUBIC_OFFSET_T2_0:
1589551ebd83SDave Airlie 	case RADEON_PP_CUBIC_OFFSET_T2_1:
1590551ebd83SDave Airlie 	case RADEON_PP_CUBIC_OFFSET_T2_2:
1591551ebd83SDave Airlie 	case RADEON_PP_CUBIC_OFFSET_T2_3:
1592551ebd83SDave Airlie 	case RADEON_PP_CUBIC_OFFSET_T2_4:
1593551ebd83SDave Airlie 		i = (reg - RADEON_PP_CUBIC_OFFSET_T2_0) / 4;
1594551ebd83SDave Airlie 		r = r100_cs_packet_next_reloc(p, &reloc);
1595551ebd83SDave Airlie 		if (r) {
1596551ebd83SDave Airlie 			DRM_ERROR("No reloc for ib[%d]=0x%04X\n",
1597551ebd83SDave Airlie 				  idx, reg);
1598551ebd83SDave Airlie 			r100_cs_dump_packet(p, pkt);
1599551ebd83SDave Airlie 			return r;
1600551ebd83SDave Airlie 		}
1601513bcb46SDave Airlie 		track->textures[2].cube_info[i].offset = idx_value;
1602513bcb46SDave Airlie 		ib[idx] = idx_value + ((u32)reloc->lobj.gpu_offset);
1603551ebd83SDave Airlie 		track->textures[2].cube_info[i].robj = reloc->robj;
160440b4a759SMarek Olšák 		track->tex_dirty = true;
1605551ebd83SDave Airlie 		break;
1606551ebd83SDave Airlie 	case RADEON_RE_WIDTH_HEIGHT:
1607513bcb46SDave Airlie 		track->maxy = ((idx_value >> 16) & 0x7FF);
160840b4a759SMarek Olšák 		track->cb_dirty = true;
160940b4a759SMarek Olšák 		track->zb_dirty = true;
1610551ebd83SDave Airlie 		break;
1611e024e110SDave Airlie 	case RADEON_RB3D_COLORPITCH:
1612e024e110SDave Airlie 		r = r100_cs_packet_next_reloc(p, &reloc);
1613e024e110SDave Airlie 		if (r) {
1614e024e110SDave Airlie 			DRM_ERROR("No reloc for ib[%d]=0x%04X\n",
1615e024e110SDave Airlie 				  idx, reg);
1616e024e110SDave Airlie 			r100_cs_dump_packet(p, pkt);
1617e024e110SDave Airlie 			return r;
1618e024e110SDave Airlie 		}
1619e024e110SDave Airlie 
1620e024e110SDave Airlie 		if (reloc->lobj.tiling_flags & RADEON_TILING_MACRO)
1621e024e110SDave Airlie 			tile_flags |= RADEON_COLOR_TILE_ENABLE;
1622e024e110SDave Airlie 		if (reloc->lobj.tiling_flags & RADEON_TILING_MICRO)
1623e024e110SDave Airlie 			tile_flags |= RADEON_COLOR_MICROTILE_ENABLE;
1624e024e110SDave Airlie 
1625513bcb46SDave Airlie 		tmp = idx_value & ~(0x7 << 16);
1626e024e110SDave Airlie 		tmp |= tile_flags;
1627e024e110SDave Airlie 		ib[idx] = tmp;
1628551ebd83SDave Airlie 
1629513bcb46SDave Airlie 		track->cb[0].pitch = idx_value & RADEON_COLORPITCH_MASK;
163040b4a759SMarek Olšák 		track->cb_dirty = true;
1631551ebd83SDave Airlie 		break;
1632551ebd83SDave Airlie 	case RADEON_RB3D_DEPTHPITCH:
1633513bcb46SDave Airlie 		track->zb.pitch = idx_value & RADEON_DEPTHPITCH_MASK;
163440b4a759SMarek Olšák 		track->zb_dirty = true;
1635551ebd83SDave Airlie 		break;
1636551ebd83SDave Airlie 	case RADEON_RB3D_CNTL:
1637513bcb46SDave Airlie 		switch ((idx_value >> RADEON_RB3D_COLOR_FORMAT_SHIFT) & 0x1f) {
1638551ebd83SDave Airlie 		case 7:
1639551ebd83SDave Airlie 		case 8:
1640551ebd83SDave Airlie 		case 9:
1641551ebd83SDave Airlie 		case 11:
1642551ebd83SDave Airlie 		case 12:
1643551ebd83SDave Airlie 			track->cb[0].cpp = 1;
1644551ebd83SDave Airlie 			break;
1645551ebd83SDave Airlie 		case 3:
1646551ebd83SDave Airlie 		case 4:
1647551ebd83SDave Airlie 		case 15:
1648551ebd83SDave Airlie 			track->cb[0].cpp = 2;
1649551ebd83SDave Airlie 			break;
1650551ebd83SDave Airlie 		case 6:
1651551ebd83SDave Airlie 			track->cb[0].cpp = 4;
1652551ebd83SDave Airlie 			break;
1653551ebd83SDave Airlie 		default:
1654551ebd83SDave Airlie 			DRM_ERROR("Invalid color buffer format (%d) !\n",
1655513bcb46SDave Airlie 				  ((idx_value >> RADEON_RB3D_COLOR_FORMAT_SHIFT) & 0x1f));
1656551ebd83SDave Airlie 			return -EINVAL;
1657551ebd83SDave Airlie 		}
1658513bcb46SDave Airlie 		track->z_enabled = !!(idx_value & RADEON_Z_ENABLE);
165940b4a759SMarek Olšák 		track->cb_dirty = true;
166040b4a759SMarek Olšák 		track->zb_dirty = true;
1661551ebd83SDave Airlie 		break;
1662551ebd83SDave Airlie 	case RADEON_RB3D_ZSTENCILCNTL:
1663513bcb46SDave Airlie 		switch (idx_value & 0xf) {
1664551ebd83SDave Airlie 		case 0:
1665551ebd83SDave Airlie 			track->zb.cpp = 2;
1666551ebd83SDave Airlie 			break;
1667551ebd83SDave Airlie 		case 2:
1668551ebd83SDave Airlie 		case 3:
1669551ebd83SDave Airlie 		case 4:
1670551ebd83SDave Airlie 		case 5:
1671551ebd83SDave Airlie 		case 9:
1672551ebd83SDave Airlie 		case 11:
1673551ebd83SDave Airlie 			track->zb.cpp = 4;
1674551ebd83SDave Airlie 			break;
1675551ebd83SDave Airlie 		default:
1676551ebd83SDave Airlie 			break;
1677551ebd83SDave Airlie 		}
167840b4a759SMarek Olšák 		track->zb_dirty = true;
1679e024e110SDave Airlie 		break;
168017782d99SDave Airlie 	case RADEON_RB3D_ZPASS_ADDR:
168117782d99SDave Airlie 		r = r100_cs_packet_next_reloc(p, &reloc);
168217782d99SDave Airlie 		if (r) {
168317782d99SDave Airlie 			DRM_ERROR("No reloc for ib[%d]=0x%04X\n",
168417782d99SDave Airlie 				  idx, reg);
168517782d99SDave Airlie 			r100_cs_dump_packet(p, pkt);
168617782d99SDave Airlie 			return r;
168717782d99SDave Airlie 		}
1688513bcb46SDave Airlie 		ib[idx] = idx_value + ((u32)reloc->lobj.gpu_offset);
168917782d99SDave Airlie 		break;
1690551ebd83SDave Airlie 	case RADEON_PP_CNTL:
1691551ebd83SDave Airlie 		{
1692513bcb46SDave Airlie 			uint32_t temp = idx_value >> 4;
1693551ebd83SDave Airlie 			for (i = 0; i < track->num_texture; i++)
1694551ebd83SDave Airlie 				track->textures[i].enabled = !!(temp & (1 << i));
169540b4a759SMarek Olšák 			track->tex_dirty = true;
1696551ebd83SDave Airlie 		}
1697551ebd83SDave Airlie 		break;
1698551ebd83SDave Airlie 	case RADEON_SE_VF_CNTL:
1699513bcb46SDave Airlie 		track->vap_vf_cntl = idx_value;
1700551ebd83SDave Airlie 		break;
1701551ebd83SDave Airlie 	case RADEON_SE_VTX_FMT:
1702513bcb46SDave Airlie 		track->vtx_size = r100_get_vtx_size(idx_value);
1703551ebd83SDave Airlie 		break;
1704551ebd83SDave Airlie 	case RADEON_PP_TEX_SIZE_0:
1705551ebd83SDave Airlie 	case RADEON_PP_TEX_SIZE_1:
1706551ebd83SDave Airlie 	case RADEON_PP_TEX_SIZE_2:
1707551ebd83SDave Airlie 		i = (reg - RADEON_PP_TEX_SIZE_0) / 8;
1708513bcb46SDave Airlie 		track->textures[i].width = (idx_value & RADEON_TEX_USIZE_MASK) + 1;
1709513bcb46SDave Airlie 		track->textures[i].height = ((idx_value & RADEON_TEX_VSIZE_MASK) >> RADEON_TEX_VSIZE_SHIFT) + 1;
171040b4a759SMarek Olšák 		track->tex_dirty = true;
1711551ebd83SDave Airlie 		break;
1712551ebd83SDave Airlie 	case RADEON_PP_TEX_PITCH_0:
1713551ebd83SDave Airlie 	case RADEON_PP_TEX_PITCH_1:
1714551ebd83SDave Airlie 	case RADEON_PP_TEX_PITCH_2:
1715551ebd83SDave Airlie 		i = (reg - RADEON_PP_TEX_PITCH_0) / 8;
1716513bcb46SDave Airlie 		track->textures[i].pitch = idx_value + 32;
171740b4a759SMarek Olšák 		track->tex_dirty = true;
1718551ebd83SDave Airlie 		break;
1719551ebd83SDave Airlie 	case RADEON_PP_TXFILTER_0:
1720551ebd83SDave Airlie 	case RADEON_PP_TXFILTER_1:
1721551ebd83SDave Airlie 	case RADEON_PP_TXFILTER_2:
1722551ebd83SDave Airlie 		i = (reg - RADEON_PP_TXFILTER_0) / 24;
1723513bcb46SDave Airlie 		track->textures[i].num_levels = ((idx_value & RADEON_MAX_MIP_LEVEL_MASK)
1724551ebd83SDave Airlie 						 >> RADEON_MAX_MIP_LEVEL_SHIFT);
1725513bcb46SDave Airlie 		tmp = (idx_value >> 23) & 0x7;
1726551ebd83SDave Airlie 		if (tmp == 2 || tmp == 6)
1727551ebd83SDave Airlie 			track->textures[i].roundup_w = false;
1728513bcb46SDave Airlie 		tmp = (idx_value >> 27) & 0x7;
1729551ebd83SDave Airlie 		if (tmp == 2 || tmp == 6)
1730551ebd83SDave Airlie 			track->textures[i].roundup_h = false;
173140b4a759SMarek Olšák 		track->tex_dirty = true;
1732551ebd83SDave Airlie 		break;
1733551ebd83SDave Airlie 	case RADEON_PP_TXFORMAT_0:
1734551ebd83SDave Airlie 	case RADEON_PP_TXFORMAT_1:
1735551ebd83SDave Airlie 	case RADEON_PP_TXFORMAT_2:
1736551ebd83SDave Airlie 		i = (reg - RADEON_PP_TXFORMAT_0) / 24;
1737513bcb46SDave Airlie 		if (idx_value & RADEON_TXFORMAT_NON_POWER2) {
1738551ebd83SDave Airlie 			track->textures[i].use_pitch = 1;
1739551ebd83SDave Airlie 		} else {
1740551ebd83SDave Airlie 			track->textures[i].use_pitch = 0;
1741513bcb46SDave Airlie 			track->textures[i].width = 1 << ((idx_value >> RADEON_TXFORMAT_WIDTH_SHIFT) & RADEON_TXFORMAT_WIDTH_MASK);
1742513bcb46SDave Airlie 			track->textures[i].height = 1 << ((idx_value >> RADEON_TXFORMAT_HEIGHT_SHIFT) & RADEON_TXFORMAT_HEIGHT_MASK);
1743551ebd83SDave Airlie 		}
1744513bcb46SDave Airlie 		if (idx_value & RADEON_TXFORMAT_CUBIC_MAP_ENABLE)
1745551ebd83SDave Airlie 			track->textures[i].tex_coord_type = 2;
1746513bcb46SDave Airlie 		switch ((idx_value & RADEON_TXFORMAT_FORMAT_MASK)) {
1747551ebd83SDave Airlie 		case RADEON_TXFORMAT_I8:
1748551ebd83SDave Airlie 		case RADEON_TXFORMAT_RGB332:
1749551ebd83SDave Airlie 		case RADEON_TXFORMAT_Y8:
1750551ebd83SDave Airlie 			track->textures[i].cpp = 1;
1751f9da52d5SRoland Scheidegger 			track->textures[i].compress_format = R100_TRACK_COMP_NONE;
1752551ebd83SDave Airlie 			break;
1753551ebd83SDave Airlie 		case RADEON_TXFORMAT_AI88:
1754551ebd83SDave Airlie 		case RADEON_TXFORMAT_ARGB1555:
1755551ebd83SDave Airlie 		case RADEON_TXFORMAT_RGB565:
1756551ebd83SDave Airlie 		case RADEON_TXFORMAT_ARGB4444:
1757551ebd83SDave Airlie 		case RADEON_TXFORMAT_VYUY422:
1758551ebd83SDave Airlie 		case RADEON_TXFORMAT_YVYU422:
1759551ebd83SDave Airlie 		case RADEON_TXFORMAT_SHADOW16:
1760551ebd83SDave Airlie 		case RADEON_TXFORMAT_LDUDV655:
1761551ebd83SDave Airlie 		case RADEON_TXFORMAT_DUDV88:
1762551ebd83SDave Airlie 			track->textures[i].cpp = 2;
1763f9da52d5SRoland Scheidegger 			track->textures[i].compress_format = R100_TRACK_COMP_NONE;
1764551ebd83SDave Airlie 			break;
1765551ebd83SDave Airlie 		case RADEON_TXFORMAT_ARGB8888:
1766551ebd83SDave Airlie 		case RADEON_TXFORMAT_RGBA8888:
1767551ebd83SDave Airlie 		case RADEON_TXFORMAT_SHADOW32:
1768551ebd83SDave Airlie 		case RADEON_TXFORMAT_LDUDUV8888:
1769551ebd83SDave Airlie 			track->textures[i].cpp = 4;
1770f9da52d5SRoland Scheidegger 			track->textures[i].compress_format = R100_TRACK_COMP_NONE;
1771551ebd83SDave Airlie 			break;
1772d785d78bSDave Airlie 		case RADEON_TXFORMAT_DXT1:
1773d785d78bSDave Airlie 			track->textures[i].cpp = 1;
1774d785d78bSDave Airlie 			track->textures[i].compress_format = R100_TRACK_COMP_DXT1;
1775d785d78bSDave Airlie 			break;
1776d785d78bSDave Airlie 		case RADEON_TXFORMAT_DXT23:
1777d785d78bSDave Airlie 		case RADEON_TXFORMAT_DXT45:
1778d785d78bSDave Airlie 			track->textures[i].cpp = 1;
1779d785d78bSDave Airlie 			track->textures[i].compress_format = R100_TRACK_COMP_DXT35;
1780d785d78bSDave Airlie 			break;
1781551ebd83SDave Airlie 		}
1782513bcb46SDave Airlie 		track->textures[i].cube_info[4].width = 1 << ((idx_value >> 16) & 0xf);
1783513bcb46SDave Airlie 		track->textures[i].cube_info[4].height = 1 << ((idx_value >> 20) & 0xf);
178440b4a759SMarek Olšák 		track->tex_dirty = true;
1785551ebd83SDave Airlie 		break;
1786551ebd83SDave Airlie 	case RADEON_PP_CUBIC_FACES_0:
1787551ebd83SDave Airlie 	case RADEON_PP_CUBIC_FACES_1:
1788551ebd83SDave Airlie 	case RADEON_PP_CUBIC_FACES_2:
1789513bcb46SDave Airlie 		tmp = idx_value;
1790551ebd83SDave Airlie 		i = (reg - RADEON_PP_CUBIC_FACES_0) / 4;
1791551ebd83SDave Airlie 		for (face = 0; face < 4; face++) {
1792551ebd83SDave Airlie 			track->textures[i].cube_info[face].width = 1 << ((tmp >> (face * 8)) & 0xf);
1793551ebd83SDave Airlie 			track->textures[i].cube_info[face].height = 1 << ((tmp >> ((face * 8) + 4)) & 0xf);
1794551ebd83SDave Airlie 		}
179540b4a759SMarek Olšák 		track->tex_dirty = true;
1796551ebd83SDave Airlie 		break;
1797771fe6b9SJerome Glisse 	default:
1798551ebd83SDave Airlie 		printk(KERN_ERR "Forbidden register 0x%04X in cs at %d\n",
1799551ebd83SDave Airlie 		       reg, idx);
1800551ebd83SDave Airlie 		return -EINVAL;
1801771fe6b9SJerome Glisse 	}
1802771fe6b9SJerome Glisse 	return 0;
1803771fe6b9SJerome Glisse }
1804771fe6b9SJerome Glisse 
1805068a117cSJerome Glisse int r100_cs_track_check_pkt3_indx_buffer(struct radeon_cs_parser *p,
1806068a117cSJerome Glisse 					 struct radeon_cs_packet *pkt,
18074c788679SJerome Glisse 					 struct radeon_bo *robj)
1808068a117cSJerome Glisse {
1809068a117cSJerome Glisse 	unsigned idx;
1810513bcb46SDave Airlie 	u32 value;
1811068a117cSJerome Glisse 	idx = pkt->idx + 1;
1812513bcb46SDave Airlie 	value = radeon_get_ib_value(p, idx + 2);
18134c788679SJerome Glisse 	if ((value + 1) > radeon_bo_size(robj)) {
1814068a117cSJerome Glisse 		DRM_ERROR("[drm] Buffer too small for PACKET3 INDX_BUFFER "
1815068a117cSJerome Glisse 			  "(need %u have %lu) !\n",
1816513bcb46SDave Airlie 			  value + 1,
18174c788679SJerome Glisse 			  radeon_bo_size(robj));
1818068a117cSJerome Glisse 		return -EINVAL;
1819068a117cSJerome Glisse 	}
1820068a117cSJerome Glisse 	return 0;
1821068a117cSJerome Glisse }
1822068a117cSJerome Glisse 
1823771fe6b9SJerome Glisse static int r100_packet3_check(struct radeon_cs_parser *p,
1824771fe6b9SJerome Glisse 			      struct radeon_cs_packet *pkt)
1825771fe6b9SJerome Glisse {
1826771fe6b9SJerome Glisse 	struct radeon_cs_reloc *reloc;
1827551ebd83SDave Airlie 	struct r100_cs_track *track;
1828771fe6b9SJerome Glisse 	unsigned idx;
1829771fe6b9SJerome Glisse 	volatile uint32_t *ib;
1830771fe6b9SJerome Glisse 	int r;
1831771fe6b9SJerome Glisse 
1832771fe6b9SJerome Glisse 	ib = p->ib->ptr;
1833771fe6b9SJerome Glisse 	idx = pkt->idx + 1;
1834551ebd83SDave Airlie 	track = (struct r100_cs_track *)p->track;
1835771fe6b9SJerome Glisse 	switch (pkt->opcode) {
1836771fe6b9SJerome Glisse 	case PACKET3_3D_LOAD_VBPNTR:
1837513bcb46SDave Airlie 		r = r100_packet3_load_vbpntr(p, pkt, idx);
1838513bcb46SDave Airlie 		if (r)
1839771fe6b9SJerome Glisse 			return r;
1840771fe6b9SJerome Glisse 		break;
1841771fe6b9SJerome Glisse 	case PACKET3_INDX_BUFFER:
1842771fe6b9SJerome Glisse 		r = r100_cs_packet_next_reloc(p, &reloc);
1843771fe6b9SJerome Glisse 		if (r) {
1844771fe6b9SJerome Glisse 			DRM_ERROR("No reloc for packet3 %d\n", pkt->opcode);
1845771fe6b9SJerome Glisse 			r100_cs_dump_packet(p, pkt);
1846771fe6b9SJerome Glisse 			return r;
1847771fe6b9SJerome Glisse 		}
1848513bcb46SDave Airlie 		ib[idx+1] = radeon_get_ib_value(p, idx+1) + ((u32)reloc->lobj.gpu_offset);
1849068a117cSJerome Glisse 		r = r100_cs_track_check_pkt3_indx_buffer(p, pkt, reloc->robj);
1850068a117cSJerome Glisse 		if (r) {
1851068a117cSJerome Glisse 			return r;
1852068a117cSJerome Glisse 		}
1853771fe6b9SJerome Glisse 		break;
1854771fe6b9SJerome Glisse 	case 0x23:
1855771fe6b9SJerome Glisse 		/* 3D_RNDR_GEN_INDX_PRIM on r100/r200 */
1856771fe6b9SJerome Glisse 		r = r100_cs_packet_next_reloc(p, &reloc);
1857771fe6b9SJerome Glisse 		if (r) {
1858771fe6b9SJerome Glisse 			DRM_ERROR("No reloc for packet3 %d\n", pkt->opcode);
1859771fe6b9SJerome Glisse 			r100_cs_dump_packet(p, pkt);
1860771fe6b9SJerome Glisse 			return r;
1861771fe6b9SJerome Glisse 		}
1862513bcb46SDave Airlie 		ib[idx] = radeon_get_ib_value(p, idx) + ((u32)reloc->lobj.gpu_offset);
1863551ebd83SDave Airlie 		track->num_arrays = 1;
1864513bcb46SDave Airlie 		track->vtx_size = r100_get_vtx_size(radeon_get_ib_value(p, idx + 2));
1865551ebd83SDave Airlie 
1866551ebd83SDave Airlie 		track->arrays[0].robj = reloc->robj;
1867551ebd83SDave Airlie 		track->arrays[0].esize = track->vtx_size;
1868551ebd83SDave Airlie 
1869513bcb46SDave Airlie 		track->max_indx = radeon_get_ib_value(p, idx+1);
1870551ebd83SDave Airlie 
1871513bcb46SDave Airlie 		track->vap_vf_cntl = radeon_get_ib_value(p, idx+3);
1872551ebd83SDave Airlie 		track->immd_dwords = pkt->count - 1;
1873551ebd83SDave Airlie 		r = r100_cs_track_check(p->rdev, track);
1874551ebd83SDave Airlie 		if (r)
1875551ebd83SDave Airlie 			return r;
1876771fe6b9SJerome Glisse 		break;
1877771fe6b9SJerome Glisse 	case PACKET3_3D_DRAW_IMMD:
1878513bcb46SDave Airlie 		if (((radeon_get_ib_value(p, idx + 1) >> 4) & 0x3) != 3) {
1879551ebd83SDave Airlie 			DRM_ERROR("PRIM_WALK must be 3 for IMMD draw\n");
1880551ebd83SDave Airlie 			return -EINVAL;
1881551ebd83SDave Airlie 		}
1882cf57fc7aSAlex Deucher 		track->vtx_size = r100_get_vtx_size(radeon_get_ib_value(p, idx + 0));
1883513bcb46SDave Airlie 		track->vap_vf_cntl = radeon_get_ib_value(p, idx + 1);
1884551ebd83SDave Airlie 		track->immd_dwords = pkt->count - 1;
1885551ebd83SDave Airlie 		r = r100_cs_track_check(p->rdev, track);
1886551ebd83SDave Airlie 		if (r)
1887551ebd83SDave Airlie 			return r;
1888551ebd83SDave Airlie 		break;
1889771fe6b9SJerome Glisse 		/* triggers drawing using in-packet vertex data */
1890771fe6b9SJerome Glisse 	case PACKET3_3D_DRAW_IMMD_2:
1891513bcb46SDave Airlie 		if (((radeon_get_ib_value(p, idx) >> 4) & 0x3) != 3) {
1892551ebd83SDave Airlie 			DRM_ERROR("PRIM_WALK must be 3 for IMMD draw\n");
1893551ebd83SDave Airlie 			return -EINVAL;
1894551ebd83SDave Airlie 		}
1895513bcb46SDave Airlie 		track->vap_vf_cntl = radeon_get_ib_value(p, idx);
1896551ebd83SDave Airlie 		track->immd_dwords = pkt->count;
1897551ebd83SDave Airlie 		r = r100_cs_track_check(p->rdev, track);
1898551ebd83SDave Airlie 		if (r)
1899551ebd83SDave Airlie 			return r;
1900551ebd83SDave Airlie 		break;
1901771fe6b9SJerome Glisse 		/* triggers drawing using in-packet vertex data */
1902771fe6b9SJerome Glisse 	case PACKET3_3D_DRAW_VBUF_2:
1903513bcb46SDave Airlie 		track->vap_vf_cntl = radeon_get_ib_value(p, idx);
1904551ebd83SDave Airlie 		r = r100_cs_track_check(p->rdev, track);
1905551ebd83SDave Airlie 		if (r)
1906551ebd83SDave Airlie 			return r;
1907551ebd83SDave Airlie 		break;
1908771fe6b9SJerome Glisse 		/* triggers drawing of vertex buffers setup elsewhere */
1909771fe6b9SJerome Glisse 	case PACKET3_3D_DRAW_INDX_2:
1910513bcb46SDave Airlie 		track->vap_vf_cntl = radeon_get_ib_value(p, idx);
1911551ebd83SDave Airlie 		r = r100_cs_track_check(p->rdev, track);
1912551ebd83SDave Airlie 		if (r)
1913551ebd83SDave Airlie 			return r;
1914551ebd83SDave Airlie 		break;
1915771fe6b9SJerome Glisse 		/* triggers drawing using indices to vertex buffer */
1916771fe6b9SJerome Glisse 	case PACKET3_3D_DRAW_VBUF:
1917513bcb46SDave Airlie 		track->vap_vf_cntl = radeon_get_ib_value(p, idx + 1);
1918551ebd83SDave Airlie 		r = r100_cs_track_check(p->rdev, track);
1919551ebd83SDave Airlie 		if (r)
1920551ebd83SDave Airlie 			return r;
1921551ebd83SDave Airlie 		break;
1922771fe6b9SJerome Glisse 		/* triggers drawing of vertex buffers setup elsewhere */
1923771fe6b9SJerome Glisse 	case PACKET3_3D_DRAW_INDX:
1924513bcb46SDave Airlie 		track->vap_vf_cntl = radeon_get_ib_value(p, idx + 1);
1925551ebd83SDave Airlie 		r = r100_cs_track_check(p->rdev, track);
1926551ebd83SDave Airlie 		if (r)
1927551ebd83SDave Airlie 			return r;
1928551ebd83SDave Airlie 		break;
1929771fe6b9SJerome Glisse 		/* triggers drawing using indices to vertex buffer */
1930ab9e1f59SDave Airlie 	case PACKET3_3D_CLEAR_HIZ:
1931ab9e1f59SDave Airlie 	case PACKET3_3D_CLEAR_ZMASK:
1932ab9e1f59SDave Airlie 		if (p->rdev->hyperz_filp != p->filp)
1933ab9e1f59SDave Airlie 			return -EINVAL;
1934ab9e1f59SDave Airlie 		break;
1935771fe6b9SJerome Glisse 	case PACKET3_NOP:
1936771fe6b9SJerome Glisse 		break;
1937771fe6b9SJerome Glisse 	default:
1938771fe6b9SJerome Glisse 		DRM_ERROR("Packet3 opcode %x not supported\n", pkt->opcode);
1939771fe6b9SJerome Glisse 		return -EINVAL;
1940771fe6b9SJerome Glisse 	}
1941771fe6b9SJerome Glisse 	return 0;
1942771fe6b9SJerome Glisse }
1943771fe6b9SJerome Glisse 
1944771fe6b9SJerome Glisse int r100_cs_parse(struct radeon_cs_parser *p)
1945771fe6b9SJerome Glisse {
1946771fe6b9SJerome Glisse 	struct radeon_cs_packet pkt;
19479f022ddfSJerome Glisse 	struct r100_cs_track *track;
1948771fe6b9SJerome Glisse 	int r;
1949771fe6b9SJerome Glisse 
19509f022ddfSJerome Glisse 	track = kzalloc(sizeof(*track), GFP_KERNEL);
19519f022ddfSJerome Glisse 	r100_cs_track_clear(p->rdev, track);
19529f022ddfSJerome Glisse 	p->track = track;
1953771fe6b9SJerome Glisse 	do {
1954771fe6b9SJerome Glisse 		r = r100_cs_packet_parse(p, &pkt, p->idx);
1955771fe6b9SJerome Glisse 		if (r) {
1956771fe6b9SJerome Glisse 			return r;
1957771fe6b9SJerome Glisse 		}
1958771fe6b9SJerome Glisse 		p->idx += pkt.count + 2;
1959771fe6b9SJerome Glisse 		switch (pkt.type) {
1960771fe6b9SJerome Glisse 			case PACKET_TYPE0:
1961551ebd83SDave Airlie 				if (p->rdev->family >= CHIP_R200)
1962551ebd83SDave Airlie 					r = r100_cs_parse_packet0(p, &pkt,
1963551ebd83SDave Airlie 								  p->rdev->config.r100.reg_safe_bm,
1964551ebd83SDave Airlie 								  p->rdev->config.r100.reg_safe_bm_size,
1965551ebd83SDave Airlie 								  &r200_packet0_check);
1966551ebd83SDave Airlie 				else
1967551ebd83SDave Airlie 					r = r100_cs_parse_packet0(p, &pkt,
1968551ebd83SDave Airlie 								  p->rdev->config.r100.reg_safe_bm,
1969551ebd83SDave Airlie 								  p->rdev->config.r100.reg_safe_bm_size,
1970551ebd83SDave Airlie 								  &r100_packet0_check);
1971771fe6b9SJerome Glisse 				break;
1972771fe6b9SJerome Glisse 			case PACKET_TYPE2:
1973771fe6b9SJerome Glisse 				break;
1974771fe6b9SJerome Glisse 			case PACKET_TYPE3:
1975771fe6b9SJerome Glisse 				r = r100_packet3_check(p, &pkt);
1976771fe6b9SJerome Glisse 				break;
1977771fe6b9SJerome Glisse 			default:
1978771fe6b9SJerome Glisse 				DRM_ERROR("Unknown packet type %d !\n",
1979771fe6b9SJerome Glisse 					  pkt.type);
1980771fe6b9SJerome Glisse 				return -EINVAL;
1981771fe6b9SJerome Glisse 		}
1982771fe6b9SJerome Glisse 		if (r) {
1983771fe6b9SJerome Glisse 			return r;
1984771fe6b9SJerome Glisse 		}
1985771fe6b9SJerome Glisse 	} while (p->idx < p->chunks[p->chunk_ib_idx].length_dw);
1986771fe6b9SJerome Glisse 	return 0;
1987771fe6b9SJerome Glisse }
1988771fe6b9SJerome Glisse 
1989771fe6b9SJerome Glisse 
1990771fe6b9SJerome Glisse /*
1991771fe6b9SJerome Glisse  * Global GPU functions
1992771fe6b9SJerome Glisse  */
1993771fe6b9SJerome Glisse void r100_errata(struct radeon_device *rdev)
1994771fe6b9SJerome Glisse {
1995771fe6b9SJerome Glisse 	rdev->pll_errata = 0;
1996771fe6b9SJerome Glisse 
1997771fe6b9SJerome Glisse 	if (rdev->family == CHIP_RV200 || rdev->family == CHIP_RS200) {
1998771fe6b9SJerome Glisse 		rdev->pll_errata |= CHIP_ERRATA_PLL_DUMMYREADS;
1999771fe6b9SJerome Glisse 	}
2000771fe6b9SJerome Glisse 
2001771fe6b9SJerome Glisse 	if (rdev->family == CHIP_RV100 ||
2002771fe6b9SJerome Glisse 	    rdev->family == CHIP_RS100 ||
2003771fe6b9SJerome Glisse 	    rdev->family == CHIP_RS200) {
2004771fe6b9SJerome Glisse 		rdev->pll_errata |= CHIP_ERRATA_PLL_DELAY;
2005771fe6b9SJerome Glisse 	}
2006771fe6b9SJerome Glisse }
2007771fe6b9SJerome Glisse 
2008771fe6b9SJerome Glisse /* Wait for vertical sync on primary CRTC */
2009771fe6b9SJerome Glisse void r100_gpu_wait_for_vsync(struct radeon_device *rdev)
2010771fe6b9SJerome Glisse {
2011771fe6b9SJerome Glisse 	uint32_t crtc_gen_cntl, tmp;
2012771fe6b9SJerome Glisse 	int i;
2013771fe6b9SJerome Glisse 
2014771fe6b9SJerome Glisse 	crtc_gen_cntl = RREG32(RADEON_CRTC_GEN_CNTL);
2015771fe6b9SJerome Glisse 	if ((crtc_gen_cntl & RADEON_CRTC_DISP_REQ_EN_B) ||
2016771fe6b9SJerome Glisse 	    !(crtc_gen_cntl & RADEON_CRTC_EN)) {
2017771fe6b9SJerome Glisse 		return;
2018771fe6b9SJerome Glisse 	}
2019771fe6b9SJerome Glisse 	/* Clear the CRTC_VBLANK_SAVE bit */
2020771fe6b9SJerome Glisse 	WREG32(RADEON_CRTC_STATUS, RADEON_CRTC_VBLANK_SAVE_CLEAR);
2021771fe6b9SJerome Glisse 	for (i = 0; i < rdev->usec_timeout; i++) {
2022771fe6b9SJerome Glisse 		tmp = RREG32(RADEON_CRTC_STATUS);
2023771fe6b9SJerome Glisse 		if (tmp & RADEON_CRTC_VBLANK_SAVE) {
2024771fe6b9SJerome Glisse 			return;
2025771fe6b9SJerome Glisse 		}
2026771fe6b9SJerome Glisse 		DRM_UDELAY(1);
2027771fe6b9SJerome Glisse 	}
2028771fe6b9SJerome Glisse }
2029771fe6b9SJerome Glisse 
2030771fe6b9SJerome Glisse /* Wait for vertical sync on secondary CRTC */
2031771fe6b9SJerome Glisse void r100_gpu_wait_for_vsync2(struct radeon_device *rdev)
2032771fe6b9SJerome Glisse {
2033771fe6b9SJerome Glisse 	uint32_t crtc2_gen_cntl, tmp;
2034771fe6b9SJerome Glisse 	int i;
2035771fe6b9SJerome Glisse 
2036771fe6b9SJerome Glisse 	crtc2_gen_cntl = RREG32(RADEON_CRTC2_GEN_CNTL);
2037771fe6b9SJerome Glisse 	if ((crtc2_gen_cntl & RADEON_CRTC2_DISP_REQ_EN_B) ||
2038771fe6b9SJerome Glisse 	    !(crtc2_gen_cntl & RADEON_CRTC2_EN))
2039771fe6b9SJerome Glisse 		return;
2040771fe6b9SJerome Glisse 
2041771fe6b9SJerome Glisse 	/* Clear the CRTC_VBLANK_SAVE bit */
2042771fe6b9SJerome Glisse 	WREG32(RADEON_CRTC2_STATUS, RADEON_CRTC2_VBLANK_SAVE_CLEAR);
2043771fe6b9SJerome Glisse 	for (i = 0; i < rdev->usec_timeout; i++) {
2044771fe6b9SJerome Glisse 		tmp = RREG32(RADEON_CRTC2_STATUS);
2045771fe6b9SJerome Glisse 		if (tmp & RADEON_CRTC2_VBLANK_SAVE) {
2046771fe6b9SJerome Glisse 			return;
2047771fe6b9SJerome Glisse 		}
2048771fe6b9SJerome Glisse 		DRM_UDELAY(1);
2049771fe6b9SJerome Glisse 	}
2050771fe6b9SJerome Glisse }
2051771fe6b9SJerome Glisse 
2052771fe6b9SJerome Glisse int r100_rbbm_fifo_wait_for_entry(struct radeon_device *rdev, unsigned n)
2053771fe6b9SJerome Glisse {
2054771fe6b9SJerome Glisse 	unsigned i;
2055771fe6b9SJerome Glisse 	uint32_t tmp;
2056771fe6b9SJerome Glisse 
2057771fe6b9SJerome Glisse 	for (i = 0; i < rdev->usec_timeout; i++) {
2058771fe6b9SJerome Glisse 		tmp = RREG32(RADEON_RBBM_STATUS) & RADEON_RBBM_FIFOCNT_MASK;
2059771fe6b9SJerome Glisse 		if (tmp >= n) {
2060771fe6b9SJerome Glisse 			return 0;
2061771fe6b9SJerome Glisse 		}
2062771fe6b9SJerome Glisse 		DRM_UDELAY(1);
2063771fe6b9SJerome Glisse 	}
2064771fe6b9SJerome Glisse 	return -1;
2065771fe6b9SJerome Glisse }
2066771fe6b9SJerome Glisse 
2067771fe6b9SJerome Glisse int r100_gui_wait_for_idle(struct radeon_device *rdev)
2068771fe6b9SJerome Glisse {
2069771fe6b9SJerome Glisse 	unsigned i;
2070771fe6b9SJerome Glisse 	uint32_t tmp;
2071771fe6b9SJerome Glisse 
2072771fe6b9SJerome Glisse 	if (r100_rbbm_fifo_wait_for_entry(rdev, 64)) {
2073771fe6b9SJerome Glisse 		printk(KERN_WARNING "radeon: wait for empty RBBM fifo failed !"
2074771fe6b9SJerome Glisse 		       " Bad things might happen.\n");
2075771fe6b9SJerome Glisse 	}
2076771fe6b9SJerome Glisse 	for (i = 0; i < rdev->usec_timeout; i++) {
2077771fe6b9SJerome Glisse 		tmp = RREG32(RADEON_RBBM_STATUS);
20784612dc97SAlex Deucher 		if (!(tmp & RADEON_RBBM_ACTIVE)) {
2079771fe6b9SJerome Glisse 			return 0;
2080771fe6b9SJerome Glisse 		}
2081771fe6b9SJerome Glisse 		DRM_UDELAY(1);
2082771fe6b9SJerome Glisse 	}
2083771fe6b9SJerome Glisse 	return -1;
2084771fe6b9SJerome Glisse }
2085771fe6b9SJerome Glisse 
2086771fe6b9SJerome Glisse int r100_mc_wait_for_idle(struct radeon_device *rdev)
2087771fe6b9SJerome Glisse {
2088771fe6b9SJerome Glisse 	unsigned i;
2089771fe6b9SJerome Glisse 	uint32_t tmp;
2090771fe6b9SJerome Glisse 
2091771fe6b9SJerome Glisse 	for (i = 0; i < rdev->usec_timeout; i++) {
2092771fe6b9SJerome Glisse 		/* read MC_STATUS */
20934612dc97SAlex Deucher 		tmp = RREG32(RADEON_MC_STATUS);
20944612dc97SAlex Deucher 		if (tmp & RADEON_MC_IDLE) {
2095771fe6b9SJerome Glisse 			return 0;
2096771fe6b9SJerome Glisse 		}
2097771fe6b9SJerome Glisse 		DRM_UDELAY(1);
2098771fe6b9SJerome Glisse 	}
2099771fe6b9SJerome Glisse 	return -1;
2100771fe6b9SJerome Glisse }
2101771fe6b9SJerome Glisse 
2102225758d8SJerome Glisse void r100_gpu_lockup_update(struct r100_gpu_lockup *lockup, struct radeon_cp *cp)
2103771fe6b9SJerome Glisse {
2104225758d8SJerome Glisse 	lockup->last_cp_rptr = cp->rptr;
2105225758d8SJerome Glisse 	lockup->last_jiffies = jiffies;
2106771fe6b9SJerome Glisse }
2107771fe6b9SJerome Glisse 
2108225758d8SJerome Glisse /**
2109225758d8SJerome Glisse  * r100_gpu_cp_is_lockup() - check if CP is lockup by recording information
2110225758d8SJerome Glisse  * @rdev:	radeon device structure
2111225758d8SJerome Glisse  * @lockup:	r100_gpu_lockup structure holding CP lockup tracking informations
2112225758d8SJerome Glisse  * @cp:		radeon_cp structure holding CP information
2113225758d8SJerome Glisse  *
2114225758d8SJerome Glisse  * We don't need to initialize the lockup tracking information as we will either
2115225758d8SJerome Glisse  * have CP rptr to a different value of jiffies wrap around which will force
2116225758d8SJerome Glisse  * initialization of the lockup tracking informations.
2117225758d8SJerome Glisse  *
2118225758d8SJerome Glisse  * A possible false positivie is if we get call after while and last_cp_rptr ==
2119225758d8SJerome Glisse  * the current CP rptr, even if it's unlikely it might happen. To avoid this
2120225758d8SJerome Glisse  * if the elapsed time since last call is bigger than 2 second than we return
2121225758d8SJerome Glisse  * false and update the tracking information. Due to this the caller must call
2122225758d8SJerome Glisse  * r100_gpu_cp_is_lockup several time in less than 2sec for lockup to be reported
2123225758d8SJerome Glisse  * the fencing code should be cautious about that.
2124225758d8SJerome Glisse  *
2125225758d8SJerome Glisse  * Caller should write to the ring to force CP to do something so we don't get
2126225758d8SJerome Glisse  * false positive when CP is just gived nothing to do.
2127225758d8SJerome Glisse  *
2128225758d8SJerome Glisse  **/
2129225758d8SJerome Glisse bool r100_gpu_cp_is_lockup(struct radeon_device *rdev, struct r100_gpu_lockup *lockup, struct radeon_cp *cp)
2130771fe6b9SJerome Glisse {
2131225758d8SJerome Glisse 	unsigned long cjiffies, elapsed;
2132771fe6b9SJerome Glisse 
2133225758d8SJerome Glisse 	cjiffies = jiffies;
2134225758d8SJerome Glisse 	if (!time_after(cjiffies, lockup->last_jiffies)) {
2135225758d8SJerome Glisse 		/* likely a wrap around */
2136225758d8SJerome Glisse 		lockup->last_cp_rptr = cp->rptr;
2137225758d8SJerome Glisse 		lockup->last_jiffies = jiffies;
2138225758d8SJerome Glisse 		return false;
2139225758d8SJerome Glisse 	}
2140225758d8SJerome Glisse 	if (cp->rptr != lockup->last_cp_rptr) {
2141225758d8SJerome Glisse 		/* CP is still working no lockup */
2142225758d8SJerome Glisse 		lockup->last_cp_rptr = cp->rptr;
2143225758d8SJerome Glisse 		lockup->last_jiffies = jiffies;
2144225758d8SJerome Glisse 		return false;
2145225758d8SJerome Glisse 	}
2146225758d8SJerome Glisse 	elapsed = jiffies_to_msecs(cjiffies - lockup->last_jiffies);
2147ec00efb7SMarek Olšák 	if (elapsed >= 10000) {
2148225758d8SJerome Glisse 		dev_err(rdev->dev, "GPU lockup CP stall for more than %lumsec\n", elapsed);
2149225758d8SJerome Glisse 		return true;
2150225758d8SJerome Glisse 	}
2151225758d8SJerome Glisse 	/* give a chance to the GPU ... */
2152225758d8SJerome Glisse 	return false;
2153771fe6b9SJerome Glisse }
2154771fe6b9SJerome Glisse 
2155225758d8SJerome Glisse bool r100_gpu_is_lockup(struct radeon_device *rdev)
2156771fe6b9SJerome Glisse {
2157225758d8SJerome Glisse 	u32 rbbm_status;
2158225758d8SJerome Glisse 	int r;
2159771fe6b9SJerome Glisse 
2160225758d8SJerome Glisse 	rbbm_status = RREG32(R_000E40_RBBM_STATUS);
2161225758d8SJerome Glisse 	if (!G_000E40_GUI_ACTIVE(rbbm_status)) {
2162225758d8SJerome Glisse 		r100_gpu_lockup_update(&rdev->config.r100.lockup, &rdev->cp);
2163225758d8SJerome Glisse 		return false;
2164225758d8SJerome Glisse 	}
2165225758d8SJerome Glisse 	/* force CP activities */
2166225758d8SJerome Glisse 	r = radeon_ring_lock(rdev, 2);
2167225758d8SJerome Glisse 	if (!r) {
2168225758d8SJerome Glisse 		/* PACKET2 NOP */
2169225758d8SJerome Glisse 		radeon_ring_write(rdev, 0x80000000);
2170225758d8SJerome Glisse 		radeon_ring_write(rdev, 0x80000000);
2171225758d8SJerome Glisse 		radeon_ring_unlock_commit(rdev);
2172225758d8SJerome Glisse 	}
2173225758d8SJerome Glisse 	rdev->cp.rptr = RREG32(RADEON_CP_RB_RPTR);
2174225758d8SJerome Glisse 	return r100_gpu_cp_is_lockup(rdev, &rdev->config.r100.lockup, &rdev->cp);
2175225758d8SJerome Glisse }
2176225758d8SJerome Glisse 
217790aca4d2SJerome Glisse void r100_bm_disable(struct radeon_device *rdev)
217890aca4d2SJerome Glisse {
217990aca4d2SJerome Glisse 	u32 tmp;
218090aca4d2SJerome Glisse 
218190aca4d2SJerome Glisse 	/* disable bus mastering */
218290aca4d2SJerome Glisse 	tmp = RREG32(R_000030_BUS_CNTL);
218390aca4d2SJerome Glisse 	WREG32(R_000030_BUS_CNTL, (tmp & 0xFFFFFFFF) | 0x00000044);
2184771fe6b9SJerome Glisse 	mdelay(1);
218590aca4d2SJerome Glisse 	WREG32(R_000030_BUS_CNTL, (tmp & 0xFFFFFFFF) | 0x00000042);
218690aca4d2SJerome Glisse 	mdelay(1);
218790aca4d2SJerome Glisse 	WREG32(R_000030_BUS_CNTL, (tmp & 0xFFFFFFFF) | 0x00000040);
218890aca4d2SJerome Glisse 	tmp = RREG32(RADEON_BUS_CNTL);
218990aca4d2SJerome Glisse 	mdelay(1);
219090aca4d2SJerome Glisse 	pci_read_config_word(rdev->pdev, 0x4, (u16*)&tmp);
219190aca4d2SJerome Glisse 	pci_write_config_word(rdev->pdev, 0x4, tmp & 0xFFFB);
219290aca4d2SJerome Glisse 	mdelay(1);
219390aca4d2SJerome Glisse }
219490aca4d2SJerome Glisse 
2195a2d07b74SJerome Glisse int r100_asic_reset(struct radeon_device *rdev)
2196771fe6b9SJerome Glisse {
219790aca4d2SJerome Glisse 	struct r100_mc_save save;
219890aca4d2SJerome Glisse 	u32 status, tmp;
219925b2ec5bSAlex Deucher 	int ret = 0;
2200771fe6b9SJerome Glisse 
220190aca4d2SJerome Glisse 	status = RREG32(R_000E40_RBBM_STATUS);
220290aca4d2SJerome Glisse 	if (!G_000E40_GUI_ACTIVE(status)) {
2203771fe6b9SJerome Glisse 		return 0;
2204771fe6b9SJerome Glisse 	}
220525b2ec5bSAlex Deucher 	r100_mc_stop(rdev, &save);
220690aca4d2SJerome Glisse 	status = RREG32(R_000E40_RBBM_STATUS);
220790aca4d2SJerome Glisse 	dev_info(rdev->dev, "(%s:%d) RBBM_STATUS=0x%08X\n", __func__, __LINE__, status);
220890aca4d2SJerome Glisse 	/* stop CP */
220990aca4d2SJerome Glisse 	WREG32(RADEON_CP_CSQ_CNTL, 0);
221090aca4d2SJerome Glisse 	tmp = RREG32(RADEON_CP_RB_CNTL);
221190aca4d2SJerome Glisse 	WREG32(RADEON_CP_RB_CNTL, tmp | RADEON_RB_RPTR_WR_ENA);
221290aca4d2SJerome Glisse 	WREG32(RADEON_CP_RB_RPTR_WR, 0);
221390aca4d2SJerome Glisse 	WREG32(RADEON_CP_RB_WPTR, 0);
221490aca4d2SJerome Glisse 	WREG32(RADEON_CP_RB_CNTL, tmp);
221590aca4d2SJerome Glisse 	/* save PCI state */
221690aca4d2SJerome Glisse 	pci_save_state(rdev->pdev);
221790aca4d2SJerome Glisse 	/* disable bus mastering */
221890aca4d2SJerome Glisse 	r100_bm_disable(rdev);
221990aca4d2SJerome Glisse 	WREG32(R_0000F0_RBBM_SOFT_RESET, S_0000F0_SOFT_RESET_SE(1) |
222090aca4d2SJerome Glisse 					S_0000F0_SOFT_RESET_RE(1) |
222190aca4d2SJerome Glisse 					S_0000F0_SOFT_RESET_PP(1) |
222290aca4d2SJerome Glisse 					S_0000F0_SOFT_RESET_RB(1));
222390aca4d2SJerome Glisse 	RREG32(R_0000F0_RBBM_SOFT_RESET);
222490aca4d2SJerome Glisse 	mdelay(500);
222590aca4d2SJerome Glisse 	WREG32(R_0000F0_RBBM_SOFT_RESET, 0);
222690aca4d2SJerome Glisse 	mdelay(1);
222790aca4d2SJerome Glisse 	status = RREG32(R_000E40_RBBM_STATUS);
222890aca4d2SJerome Glisse 	dev_info(rdev->dev, "(%s:%d) RBBM_STATUS=0x%08X\n", __func__, __LINE__, status);
2229771fe6b9SJerome Glisse 	/* reset CP */
223090aca4d2SJerome Glisse 	WREG32(R_0000F0_RBBM_SOFT_RESET, S_0000F0_SOFT_RESET_CP(1));
223190aca4d2SJerome Glisse 	RREG32(R_0000F0_RBBM_SOFT_RESET);
223290aca4d2SJerome Glisse 	mdelay(500);
223390aca4d2SJerome Glisse 	WREG32(R_0000F0_RBBM_SOFT_RESET, 0);
223490aca4d2SJerome Glisse 	mdelay(1);
223590aca4d2SJerome Glisse 	status = RREG32(R_000E40_RBBM_STATUS);
223690aca4d2SJerome Glisse 	dev_info(rdev->dev, "(%s:%d) RBBM_STATUS=0x%08X\n", __func__, __LINE__, status);
223790aca4d2SJerome Glisse 	/* restore PCI & busmastering */
223890aca4d2SJerome Glisse 	pci_restore_state(rdev->pdev);
223990aca4d2SJerome Glisse 	r100_enable_bm(rdev);
2240771fe6b9SJerome Glisse 	/* Check if GPU is idle */
224190aca4d2SJerome Glisse 	if (G_000E40_SE_BUSY(status) || G_000E40_RE_BUSY(status) ||
224290aca4d2SJerome Glisse 		G_000E40_TAM_BUSY(status) || G_000E40_PB_BUSY(status)) {
224390aca4d2SJerome Glisse 		dev_err(rdev->dev, "failed to reset GPU\n");
224490aca4d2SJerome Glisse 		rdev->gpu_lockup = true;
224525b2ec5bSAlex Deucher 		ret = -1;
224625b2ec5bSAlex Deucher 	} else
224790aca4d2SJerome Glisse 		dev_info(rdev->dev, "GPU reset succeed\n");
224825b2ec5bSAlex Deucher 	r100_mc_resume(rdev, &save);
224925b2ec5bSAlex Deucher 	return ret;
2250771fe6b9SJerome Glisse }
2251771fe6b9SJerome Glisse 
225292cde00cSAlex Deucher void r100_set_common_regs(struct radeon_device *rdev)
225392cde00cSAlex Deucher {
22542739d49cSAlex Deucher 	struct drm_device *dev = rdev->ddev;
22552739d49cSAlex Deucher 	bool force_dac2 = false;
2256d668046cSDave Airlie 	u32 tmp;
22572739d49cSAlex Deucher 
225892cde00cSAlex Deucher 	/* set these so they don't interfere with anything */
225992cde00cSAlex Deucher 	WREG32(RADEON_OV0_SCALE_CNTL, 0);
226092cde00cSAlex Deucher 	WREG32(RADEON_SUBPIC_CNTL, 0);
226192cde00cSAlex Deucher 	WREG32(RADEON_VIPH_CONTROL, 0);
226292cde00cSAlex Deucher 	WREG32(RADEON_I2C_CNTL_1, 0);
226392cde00cSAlex Deucher 	WREG32(RADEON_DVI_I2C_CNTL_1, 0);
226492cde00cSAlex Deucher 	WREG32(RADEON_CAP0_TRIG_CNTL, 0);
226592cde00cSAlex Deucher 	WREG32(RADEON_CAP1_TRIG_CNTL, 0);
22662739d49cSAlex Deucher 
22672739d49cSAlex Deucher 	/* always set up dac2 on rn50 and some rv100 as lots
22682739d49cSAlex Deucher 	 * of servers seem to wire it up to a VGA port but
22692739d49cSAlex Deucher 	 * don't report it in the bios connector
22702739d49cSAlex Deucher 	 * table.
22712739d49cSAlex Deucher 	 */
22722739d49cSAlex Deucher 	switch (dev->pdev->device) {
22732739d49cSAlex Deucher 		/* RN50 */
22742739d49cSAlex Deucher 	case 0x515e:
22752739d49cSAlex Deucher 	case 0x5969:
22762739d49cSAlex Deucher 		force_dac2 = true;
22772739d49cSAlex Deucher 		break;
22782739d49cSAlex Deucher 		/* RV100*/
22792739d49cSAlex Deucher 	case 0x5159:
22802739d49cSAlex Deucher 	case 0x515a:
22812739d49cSAlex Deucher 		/* DELL triple head servers */
22822739d49cSAlex Deucher 		if ((dev->pdev->subsystem_vendor == 0x1028 /* DELL */) &&
22832739d49cSAlex Deucher 		    ((dev->pdev->subsystem_device == 0x016c) ||
22842739d49cSAlex Deucher 		     (dev->pdev->subsystem_device == 0x016d) ||
22852739d49cSAlex Deucher 		     (dev->pdev->subsystem_device == 0x016e) ||
22862739d49cSAlex Deucher 		     (dev->pdev->subsystem_device == 0x016f) ||
22872739d49cSAlex Deucher 		     (dev->pdev->subsystem_device == 0x0170) ||
22882739d49cSAlex Deucher 		     (dev->pdev->subsystem_device == 0x017d) ||
22892739d49cSAlex Deucher 		     (dev->pdev->subsystem_device == 0x017e) ||
22902739d49cSAlex Deucher 		     (dev->pdev->subsystem_device == 0x0183) ||
22912739d49cSAlex Deucher 		     (dev->pdev->subsystem_device == 0x018a) ||
22922739d49cSAlex Deucher 		     (dev->pdev->subsystem_device == 0x019a)))
22932739d49cSAlex Deucher 			force_dac2 = true;
22942739d49cSAlex Deucher 		break;
22952739d49cSAlex Deucher 	}
22962739d49cSAlex Deucher 
22972739d49cSAlex Deucher 	if (force_dac2) {
22982739d49cSAlex Deucher 		u32 disp_hw_debug = RREG32(RADEON_DISP_HW_DEBUG);
22992739d49cSAlex Deucher 		u32 tv_dac_cntl = RREG32(RADEON_TV_DAC_CNTL);
23002739d49cSAlex Deucher 		u32 dac2_cntl = RREG32(RADEON_DAC_CNTL2);
23012739d49cSAlex Deucher 
23022739d49cSAlex Deucher 		/* For CRT on DAC2, don't turn it on if BIOS didn't
23032739d49cSAlex Deucher 		   enable it, even it's detected.
23042739d49cSAlex Deucher 		*/
23052739d49cSAlex Deucher 
23062739d49cSAlex Deucher 		/* force it to crtc0 */
23072739d49cSAlex Deucher 		dac2_cntl &= ~RADEON_DAC2_DAC_CLK_SEL;
23082739d49cSAlex Deucher 		dac2_cntl |= RADEON_DAC2_DAC2_CLK_SEL;
23092739d49cSAlex Deucher 		disp_hw_debug |= RADEON_CRT2_DISP1_SEL;
23102739d49cSAlex Deucher 
23112739d49cSAlex Deucher 		/* set up the TV DAC */
23122739d49cSAlex Deucher 		tv_dac_cntl &= ~(RADEON_TV_DAC_PEDESTAL |
23132739d49cSAlex Deucher 				 RADEON_TV_DAC_STD_MASK |
23142739d49cSAlex Deucher 				 RADEON_TV_DAC_RDACPD |
23152739d49cSAlex Deucher 				 RADEON_TV_DAC_GDACPD |
23162739d49cSAlex Deucher 				 RADEON_TV_DAC_BDACPD |
23172739d49cSAlex Deucher 				 RADEON_TV_DAC_BGADJ_MASK |
23182739d49cSAlex Deucher 				 RADEON_TV_DAC_DACADJ_MASK);
23192739d49cSAlex Deucher 		tv_dac_cntl |= (RADEON_TV_DAC_NBLANK |
23202739d49cSAlex Deucher 				RADEON_TV_DAC_NHOLD |
23212739d49cSAlex Deucher 				RADEON_TV_DAC_STD_PS2 |
23222739d49cSAlex Deucher 				(0x58 << 16));
23232739d49cSAlex Deucher 
23242739d49cSAlex Deucher 		WREG32(RADEON_TV_DAC_CNTL, tv_dac_cntl);
23252739d49cSAlex Deucher 		WREG32(RADEON_DISP_HW_DEBUG, disp_hw_debug);
23262739d49cSAlex Deucher 		WREG32(RADEON_DAC_CNTL2, dac2_cntl);
23272739d49cSAlex Deucher 	}
2328d668046cSDave Airlie 
2329d668046cSDave Airlie 	/* switch PM block to ACPI mode */
2330d668046cSDave Airlie 	tmp = RREG32_PLL(RADEON_PLL_PWRMGT_CNTL);
2331d668046cSDave Airlie 	tmp &= ~RADEON_PM_MODE_SEL;
2332d668046cSDave Airlie 	WREG32_PLL(RADEON_PLL_PWRMGT_CNTL, tmp);
2333d668046cSDave Airlie 
233492cde00cSAlex Deucher }
2335771fe6b9SJerome Glisse 
2336771fe6b9SJerome Glisse /*
2337771fe6b9SJerome Glisse  * VRAM info
2338771fe6b9SJerome Glisse  */
2339771fe6b9SJerome Glisse static void r100_vram_get_type(struct radeon_device *rdev)
2340771fe6b9SJerome Glisse {
2341771fe6b9SJerome Glisse 	uint32_t tmp;
2342771fe6b9SJerome Glisse 
2343771fe6b9SJerome Glisse 	rdev->mc.vram_is_ddr = false;
2344771fe6b9SJerome Glisse 	if (rdev->flags & RADEON_IS_IGP)
2345771fe6b9SJerome Glisse 		rdev->mc.vram_is_ddr = true;
2346771fe6b9SJerome Glisse 	else if (RREG32(RADEON_MEM_SDRAM_MODE_REG) & RADEON_MEM_CFG_TYPE_DDR)
2347771fe6b9SJerome Glisse 		rdev->mc.vram_is_ddr = true;
2348771fe6b9SJerome Glisse 	if ((rdev->family == CHIP_RV100) ||
2349771fe6b9SJerome Glisse 	    (rdev->family == CHIP_RS100) ||
2350771fe6b9SJerome Glisse 	    (rdev->family == CHIP_RS200)) {
2351771fe6b9SJerome Glisse 		tmp = RREG32(RADEON_MEM_CNTL);
2352771fe6b9SJerome Glisse 		if (tmp & RV100_HALF_MODE) {
2353771fe6b9SJerome Glisse 			rdev->mc.vram_width = 32;
2354771fe6b9SJerome Glisse 		} else {
2355771fe6b9SJerome Glisse 			rdev->mc.vram_width = 64;
2356771fe6b9SJerome Glisse 		}
2357771fe6b9SJerome Glisse 		if (rdev->flags & RADEON_SINGLE_CRTC) {
2358771fe6b9SJerome Glisse 			rdev->mc.vram_width /= 4;
2359771fe6b9SJerome Glisse 			rdev->mc.vram_is_ddr = true;
2360771fe6b9SJerome Glisse 		}
2361771fe6b9SJerome Glisse 	} else if (rdev->family <= CHIP_RV280) {
2362771fe6b9SJerome Glisse 		tmp = RREG32(RADEON_MEM_CNTL);
2363771fe6b9SJerome Glisse 		if (tmp & RADEON_MEM_NUM_CHANNELS_MASK) {
2364771fe6b9SJerome Glisse 			rdev->mc.vram_width = 128;
2365771fe6b9SJerome Glisse 		} else {
2366771fe6b9SJerome Glisse 			rdev->mc.vram_width = 64;
2367771fe6b9SJerome Glisse 		}
2368771fe6b9SJerome Glisse 	} else {
2369771fe6b9SJerome Glisse 		/* newer IGPs */
2370771fe6b9SJerome Glisse 		rdev->mc.vram_width = 128;
2371771fe6b9SJerome Glisse 	}
2372771fe6b9SJerome Glisse }
2373771fe6b9SJerome Glisse 
23742a0f8918SDave Airlie static u32 r100_get_accessible_vram(struct radeon_device *rdev)
2375771fe6b9SJerome Glisse {
23762a0f8918SDave Airlie 	u32 aper_size;
23772a0f8918SDave Airlie 	u8 byte;
23782a0f8918SDave Airlie 
23792a0f8918SDave Airlie 	aper_size = RREG32(RADEON_CONFIG_APER_SIZE);
23802a0f8918SDave Airlie 
23812a0f8918SDave Airlie 	/* Set HDP_APER_CNTL only on cards that are known not to be broken,
23822a0f8918SDave Airlie 	 * that is has the 2nd generation multifunction PCI interface
23832a0f8918SDave Airlie 	 */
23842a0f8918SDave Airlie 	if (rdev->family == CHIP_RV280 ||
23852a0f8918SDave Airlie 	    rdev->family >= CHIP_RV350) {
23862a0f8918SDave Airlie 		WREG32_P(RADEON_HOST_PATH_CNTL, RADEON_HDP_APER_CNTL,
23872a0f8918SDave Airlie 		       ~RADEON_HDP_APER_CNTL);
23882a0f8918SDave Airlie 		DRM_INFO("Generation 2 PCI interface, using max accessible memory\n");
23892a0f8918SDave Airlie 		return aper_size * 2;
23902a0f8918SDave Airlie 	}
23912a0f8918SDave Airlie 
23922a0f8918SDave Airlie 	/* Older cards have all sorts of funny issues to deal with. First
23932a0f8918SDave Airlie 	 * check if it's a multifunction card by reading the PCI config
23942a0f8918SDave Airlie 	 * header type... Limit those to one aperture size
23952a0f8918SDave Airlie 	 */
23962a0f8918SDave Airlie 	pci_read_config_byte(rdev->pdev, 0xe, &byte);
23972a0f8918SDave Airlie 	if (byte & 0x80) {
23982a0f8918SDave Airlie 		DRM_INFO("Generation 1 PCI interface in multifunction mode\n");
23992a0f8918SDave Airlie 		DRM_INFO("Limiting VRAM to one aperture\n");
24002a0f8918SDave Airlie 		return aper_size;
24012a0f8918SDave Airlie 	}
24022a0f8918SDave Airlie 
24032a0f8918SDave Airlie 	/* Single function older card. We read HDP_APER_CNTL to see how the BIOS
24042a0f8918SDave Airlie 	 * have set it up. We don't write this as it's broken on some ASICs but
24052a0f8918SDave Airlie 	 * we expect the BIOS to have done the right thing (might be too optimistic...)
24062a0f8918SDave Airlie 	 */
24072a0f8918SDave Airlie 	if (RREG32(RADEON_HOST_PATH_CNTL) & RADEON_HDP_APER_CNTL)
24082a0f8918SDave Airlie 		return aper_size * 2;
24092a0f8918SDave Airlie 	return aper_size;
24102a0f8918SDave Airlie }
24112a0f8918SDave Airlie 
24122a0f8918SDave Airlie void r100_vram_init_sizes(struct radeon_device *rdev)
24132a0f8918SDave Airlie {
24142a0f8918SDave Airlie 	u64 config_aper_size;
24152a0f8918SDave Airlie 
2416d594e46aSJerome Glisse 	/* work out accessible VRAM */
241701d73a69SJordan Crouse 	rdev->mc.aper_base = pci_resource_start(rdev->pdev, 0);
241801d73a69SJordan Crouse 	rdev->mc.aper_size = pci_resource_len(rdev->pdev, 0);
241951e5fcd3SJerome Glisse 	rdev->mc.visible_vram_size = r100_get_accessible_vram(rdev);
242051e5fcd3SJerome Glisse 	/* FIXME we don't use the second aperture yet when we could use it */
242151e5fcd3SJerome Glisse 	if (rdev->mc.visible_vram_size > rdev->mc.aper_size)
242251e5fcd3SJerome Glisse 		rdev->mc.visible_vram_size = rdev->mc.aper_size;
24232a0f8918SDave Airlie 	config_aper_size = RREG32(RADEON_CONFIG_APER_SIZE);
2424771fe6b9SJerome Glisse 	if (rdev->flags & RADEON_IS_IGP) {
2425771fe6b9SJerome Glisse 		uint32_t tom;
2426771fe6b9SJerome Glisse 		/* read NB_TOM to get the amount of ram stolen for the GPU */
2427771fe6b9SJerome Glisse 		tom = RREG32(RADEON_NB_TOM);
24287a50f01aSDave Airlie 		rdev->mc.real_vram_size = (((tom >> 16) - (tom & 0xffff) + 1) << 16);
24297a50f01aSDave Airlie 		WREG32(RADEON_CONFIG_MEMSIZE, rdev->mc.real_vram_size);
24307a50f01aSDave Airlie 		rdev->mc.mc_vram_size = rdev->mc.real_vram_size;
2431771fe6b9SJerome Glisse 	} else {
24327a50f01aSDave Airlie 		rdev->mc.real_vram_size = RREG32(RADEON_CONFIG_MEMSIZE);
2433771fe6b9SJerome Glisse 		/* Some production boards of m6 will report 0
2434771fe6b9SJerome Glisse 		 * if it's 8 MB
2435771fe6b9SJerome Glisse 		 */
24367a50f01aSDave Airlie 		if (rdev->mc.real_vram_size == 0) {
24377a50f01aSDave Airlie 			rdev->mc.real_vram_size = 8192 * 1024;
24387a50f01aSDave Airlie 			WREG32(RADEON_CONFIG_MEMSIZE, rdev->mc.real_vram_size);
2439771fe6b9SJerome Glisse 		}
24402a0f8918SDave Airlie 		/* Fix for RN50, M6, M7 with 8/16/32(??) MBs of VRAM -
2441d594e46aSJerome Glisse 		 * Novell bug 204882 + along with lots of ubuntu ones
2442d594e46aSJerome Glisse 		 */
2443b7d8cce5SAlex Deucher 		if (rdev->mc.aper_size > config_aper_size)
2444b7d8cce5SAlex Deucher 			config_aper_size = rdev->mc.aper_size;
2445b7d8cce5SAlex Deucher 
24467a50f01aSDave Airlie 		if (config_aper_size > rdev->mc.real_vram_size)
24477a50f01aSDave Airlie 			rdev->mc.mc_vram_size = config_aper_size;
24487a50f01aSDave Airlie 		else
24497a50f01aSDave Airlie 			rdev->mc.mc_vram_size = rdev->mc.real_vram_size;
2450771fe6b9SJerome Glisse 	}
2451d594e46aSJerome Glisse }
24522a0f8918SDave Airlie 
245328d52043SDave Airlie void r100_vga_set_state(struct radeon_device *rdev, bool state)
245428d52043SDave Airlie {
245528d52043SDave Airlie 	uint32_t temp;
245628d52043SDave Airlie 
245728d52043SDave Airlie 	temp = RREG32(RADEON_CONFIG_CNTL);
245828d52043SDave Airlie 	if (state == false) {
2459d75ee3beSAlex Deucher 		temp &= ~RADEON_CFG_VGA_RAM_EN;
2460d75ee3beSAlex Deucher 		temp |= RADEON_CFG_VGA_IO_DIS;
246128d52043SDave Airlie 	} else {
2462d75ee3beSAlex Deucher 		temp &= ~RADEON_CFG_VGA_IO_DIS;
246328d52043SDave Airlie 	}
246428d52043SDave Airlie 	WREG32(RADEON_CONFIG_CNTL, temp);
246528d52043SDave Airlie }
246628d52043SDave Airlie 
2467d594e46aSJerome Glisse void r100_mc_init(struct radeon_device *rdev)
24682a0f8918SDave Airlie {
2469d594e46aSJerome Glisse 	u64 base;
24702a0f8918SDave Airlie 
2471d594e46aSJerome Glisse 	r100_vram_get_type(rdev);
24722a0f8918SDave Airlie 	r100_vram_init_sizes(rdev);
2473d594e46aSJerome Glisse 	base = rdev->mc.aper_base;
2474d594e46aSJerome Glisse 	if (rdev->flags & RADEON_IS_IGP)
2475d594e46aSJerome Glisse 		base = (RREG32(RADEON_NB_TOM) & 0xffff) << 16;
2476d594e46aSJerome Glisse 	radeon_vram_location(rdev, &rdev->mc, base);
24778d369bb1SAlex Deucher 	rdev->mc.gtt_base_align = 0;
2478d594e46aSJerome Glisse 	if (!(rdev->flags & RADEON_IS_AGP))
2479d594e46aSJerome Glisse 		radeon_gtt_location(rdev, &rdev->mc);
2480f47299c5SAlex Deucher 	radeon_update_bandwidth_info(rdev);
2481771fe6b9SJerome Glisse }
2482771fe6b9SJerome Glisse 
2483771fe6b9SJerome Glisse 
2484771fe6b9SJerome Glisse /*
2485771fe6b9SJerome Glisse  * Indirect registers accessor
2486771fe6b9SJerome Glisse  */
2487771fe6b9SJerome Glisse void r100_pll_errata_after_index(struct radeon_device *rdev)
2488771fe6b9SJerome Glisse {
24894ce9198eSAlex Deucher 	if (rdev->pll_errata & CHIP_ERRATA_PLL_DUMMYREADS) {
2490771fe6b9SJerome Glisse 		(void)RREG32(RADEON_CLOCK_CNTL_DATA);
2491771fe6b9SJerome Glisse 		(void)RREG32(RADEON_CRTC_GEN_CNTL);
2492771fe6b9SJerome Glisse 	}
24934ce9198eSAlex Deucher }
2494771fe6b9SJerome Glisse 
2495771fe6b9SJerome Glisse static void r100_pll_errata_after_data(struct radeon_device *rdev)
2496771fe6b9SJerome Glisse {
2497771fe6b9SJerome Glisse 	/* This workarounds is necessary on RV100, RS100 and RS200 chips
2498771fe6b9SJerome Glisse 	 * or the chip could hang on a subsequent access
2499771fe6b9SJerome Glisse 	 */
2500771fe6b9SJerome Glisse 	if (rdev->pll_errata & CHIP_ERRATA_PLL_DELAY) {
2501771fe6b9SJerome Glisse 		udelay(5000);
2502771fe6b9SJerome Glisse 	}
2503771fe6b9SJerome Glisse 
2504771fe6b9SJerome Glisse 	/* This function is required to workaround a hardware bug in some (all?)
2505771fe6b9SJerome Glisse 	 * revisions of the R300.  This workaround should be called after every
2506771fe6b9SJerome Glisse 	 * CLOCK_CNTL_INDEX register access.  If not, register reads afterward
2507771fe6b9SJerome Glisse 	 * may not be correct.
2508771fe6b9SJerome Glisse 	 */
2509771fe6b9SJerome Glisse 	if (rdev->pll_errata & CHIP_ERRATA_R300_CG) {
2510771fe6b9SJerome Glisse 		uint32_t save, tmp;
2511771fe6b9SJerome Glisse 
2512771fe6b9SJerome Glisse 		save = RREG32(RADEON_CLOCK_CNTL_INDEX);
2513771fe6b9SJerome Glisse 		tmp = save & ~(0x3f | RADEON_PLL_WR_EN);
2514771fe6b9SJerome Glisse 		WREG32(RADEON_CLOCK_CNTL_INDEX, tmp);
2515771fe6b9SJerome Glisse 		tmp = RREG32(RADEON_CLOCK_CNTL_DATA);
2516771fe6b9SJerome Glisse 		WREG32(RADEON_CLOCK_CNTL_INDEX, save);
2517771fe6b9SJerome Glisse 	}
2518771fe6b9SJerome Glisse }
2519771fe6b9SJerome Glisse 
2520771fe6b9SJerome Glisse uint32_t r100_pll_rreg(struct radeon_device *rdev, uint32_t reg)
2521771fe6b9SJerome Glisse {
2522771fe6b9SJerome Glisse 	uint32_t data;
2523771fe6b9SJerome Glisse 
2524771fe6b9SJerome Glisse 	WREG8(RADEON_CLOCK_CNTL_INDEX, reg & 0x3f);
2525771fe6b9SJerome Glisse 	r100_pll_errata_after_index(rdev);
2526771fe6b9SJerome Glisse 	data = RREG32(RADEON_CLOCK_CNTL_DATA);
2527771fe6b9SJerome Glisse 	r100_pll_errata_after_data(rdev);
2528771fe6b9SJerome Glisse 	return data;
2529771fe6b9SJerome Glisse }
2530771fe6b9SJerome Glisse 
2531771fe6b9SJerome Glisse void r100_pll_wreg(struct radeon_device *rdev, uint32_t reg, uint32_t v)
2532771fe6b9SJerome Glisse {
2533771fe6b9SJerome Glisse 	WREG8(RADEON_CLOCK_CNTL_INDEX, ((reg & 0x3f) | RADEON_PLL_WR_EN));
2534771fe6b9SJerome Glisse 	r100_pll_errata_after_index(rdev);
2535771fe6b9SJerome Glisse 	WREG32(RADEON_CLOCK_CNTL_DATA, v);
2536771fe6b9SJerome Glisse 	r100_pll_errata_after_data(rdev);
2537771fe6b9SJerome Glisse }
2538771fe6b9SJerome Glisse 
2539d4550907SJerome Glisse void r100_set_safe_registers(struct radeon_device *rdev)
2540068a117cSJerome Glisse {
2541551ebd83SDave Airlie 	if (ASIC_IS_RN50(rdev)) {
2542551ebd83SDave Airlie 		rdev->config.r100.reg_safe_bm = rn50_reg_safe_bm;
2543551ebd83SDave Airlie 		rdev->config.r100.reg_safe_bm_size = ARRAY_SIZE(rn50_reg_safe_bm);
2544551ebd83SDave Airlie 	} else if (rdev->family < CHIP_R200) {
2545551ebd83SDave Airlie 		rdev->config.r100.reg_safe_bm = r100_reg_safe_bm;
2546551ebd83SDave Airlie 		rdev->config.r100.reg_safe_bm_size = ARRAY_SIZE(r100_reg_safe_bm);
2547551ebd83SDave Airlie 	} else {
2548d4550907SJerome Glisse 		r200_set_safe_registers(rdev);
2549551ebd83SDave Airlie 	}
2550068a117cSJerome Glisse }
2551068a117cSJerome Glisse 
2552771fe6b9SJerome Glisse /*
2553771fe6b9SJerome Glisse  * Debugfs info
2554771fe6b9SJerome Glisse  */
2555771fe6b9SJerome Glisse #if defined(CONFIG_DEBUG_FS)
2556771fe6b9SJerome Glisse static int r100_debugfs_rbbm_info(struct seq_file *m, void *data)
2557771fe6b9SJerome Glisse {
2558771fe6b9SJerome Glisse 	struct drm_info_node *node = (struct drm_info_node *) m->private;
2559771fe6b9SJerome Glisse 	struct drm_device *dev = node->minor->dev;
2560771fe6b9SJerome Glisse 	struct radeon_device *rdev = dev->dev_private;
2561771fe6b9SJerome Glisse 	uint32_t reg, value;
2562771fe6b9SJerome Glisse 	unsigned i;
2563771fe6b9SJerome Glisse 
2564771fe6b9SJerome Glisse 	seq_printf(m, "RBBM_STATUS 0x%08x\n", RREG32(RADEON_RBBM_STATUS));
2565771fe6b9SJerome Glisse 	seq_printf(m, "RBBM_CMDFIFO_STAT 0x%08x\n", RREG32(0xE7C));
2566771fe6b9SJerome Glisse 	seq_printf(m, "CP_STAT 0x%08x\n", RREG32(RADEON_CP_STAT));
2567771fe6b9SJerome Glisse 	for (i = 0; i < 64; i++) {
2568771fe6b9SJerome Glisse 		WREG32(RADEON_RBBM_CMDFIFO_ADDR, i | 0x100);
2569771fe6b9SJerome Glisse 		reg = (RREG32(RADEON_RBBM_CMDFIFO_DATA) - 1) >> 2;
2570771fe6b9SJerome Glisse 		WREG32(RADEON_RBBM_CMDFIFO_ADDR, i);
2571771fe6b9SJerome Glisse 		value = RREG32(RADEON_RBBM_CMDFIFO_DATA);
2572771fe6b9SJerome Glisse 		seq_printf(m, "[0x%03X] 0x%04X=0x%08X\n", i, reg, value);
2573771fe6b9SJerome Glisse 	}
2574771fe6b9SJerome Glisse 	return 0;
2575771fe6b9SJerome Glisse }
2576771fe6b9SJerome Glisse 
2577771fe6b9SJerome Glisse static int r100_debugfs_cp_ring_info(struct seq_file *m, void *data)
2578771fe6b9SJerome Glisse {
2579771fe6b9SJerome Glisse 	struct drm_info_node *node = (struct drm_info_node *) m->private;
2580771fe6b9SJerome Glisse 	struct drm_device *dev = node->minor->dev;
2581771fe6b9SJerome Glisse 	struct radeon_device *rdev = dev->dev_private;
2582771fe6b9SJerome Glisse 	uint32_t rdp, wdp;
2583771fe6b9SJerome Glisse 	unsigned count, i, j;
2584771fe6b9SJerome Glisse 
2585771fe6b9SJerome Glisse 	radeon_ring_free_size(rdev);
2586771fe6b9SJerome Glisse 	rdp = RREG32(RADEON_CP_RB_RPTR);
2587771fe6b9SJerome Glisse 	wdp = RREG32(RADEON_CP_RB_WPTR);
2588771fe6b9SJerome Glisse 	count = (rdp + rdev->cp.ring_size - wdp) & rdev->cp.ptr_mask;
2589771fe6b9SJerome Glisse 	seq_printf(m, "CP_STAT 0x%08x\n", RREG32(RADEON_CP_STAT));
2590771fe6b9SJerome Glisse 	seq_printf(m, "CP_RB_WPTR 0x%08x\n", wdp);
2591771fe6b9SJerome Glisse 	seq_printf(m, "CP_RB_RPTR 0x%08x\n", rdp);
2592771fe6b9SJerome Glisse 	seq_printf(m, "%u free dwords in ring\n", rdev->cp.ring_free_dw);
2593771fe6b9SJerome Glisse 	seq_printf(m, "%u dwords in ring\n", count);
2594771fe6b9SJerome Glisse 	for (j = 0; j <= count; j++) {
2595771fe6b9SJerome Glisse 		i = (rdp + j) & rdev->cp.ptr_mask;
2596771fe6b9SJerome Glisse 		seq_printf(m, "r[%04d]=0x%08x\n", i, rdev->cp.ring[i]);
2597771fe6b9SJerome Glisse 	}
2598771fe6b9SJerome Glisse 	return 0;
2599771fe6b9SJerome Glisse }
2600771fe6b9SJerome Glisse 
2601771fe6b9SJerome Glisse 
2602771fe6b9SJerome Glisse static int r100_debugfs_cp_csq_fifo(struct seq_file *m, void *data)
2603771fe6b9SJerome Glisse {
2604771fe6b9SJerome Glisse 	struct drm_info_node *node = (struct drm_info_node *) m->private;
2605771fe6b9SJerome Glisse 	struct drm_device *dev = node->minor->dev;
2606771fe6b9SJerome Glisse 	struct radeon_device *rdev = dev->dev_private;
2607771fe6b9SJerome Glisse 	uint32_t csq_stat, csq2_stat, tmp;
2608771fe6b9SJerome Glisse 	unsigned r_rptr, r_wptr, ib1_rptr, ib1_wptr, ib2_rptr, ib2_wptr;
2609771fe6b9SJerome Glisse 	unsigned i;
2610771fe6b9SJerome Glisse 
2611771fe6b9SJerome Glisse 	seq_printf(m, "CP_STAT 0x%08x\n", RREG32(RADEON_CP_STAT));
2612771fe6b9SJerome Glisse 	seq_printf(m, "CP_CSQ_MODE 0x%08x\n", RREG32(RADEON_CP_CSQ_MODE));
2613771fe6b9SJerome Glisse 	csq_stat = RREG32(RADEON_CP_CSQ_STAT);
2614771fe6b9SJerome Glisse 	csq2_stat = RREG32(RADEON_CP_CSQ2_STAT);
2615771fe6b9SJerome Glisse 	r_rptr = (csq_stat >> 0) & 0x3ff;
2616771fe6b9SJerome Glisse 	r_wptr = (csq_stat >> 10) & 0x3ff;
2617771fe6b9SJerome Glisse 	ib1_rptr = (csq_stat >> 20) & 0x3ff;
2618771fe6b9SJerome Glisse 	ib1_wptr = (csq2_stat >> 0) & 0x3ff;
2619771fe6b9SJerome Glisse 	ib2_rptr = (csq2_stat >> 10) & 0x3ff;
2620771fe6b9SJerome Glisse 	ib2_wptr = (csq2_stat >> 20) & 0x3ff;
2621771fe6b9SJerome Glisse 	seq_printf(m, "CP_CSQ_STAT 0x%08x\n", csq_stat);
2622771fe6b9SJerome Glisse 	seq_printf(m, "CP_CSQ2_STAT 0x%08x\n", csq2_stat);
2623771fe6b9SJerome Glisse 	seq_printf(m, "Ring rptr %u\n", r_rptr);
2624771fe6b9SJerome Glisse 	seq_printf(m, "Ring wptr %u\n", r_wptr);
2625771fe6b9SJerome Glisse 	seq_printf(m, "Indirect1 rptr %u\n", ib1_rptr);
2626771fe6b9SJerome Glisse 	seq_printf(m, "Indirect1 wptr %u\n", ib1_wptr);
2627771fe6b9SJerome Glisse 	seq_printf(m, "Indirect2 rptr %u\n", ib2_rptr);
2628771fe6b9SJerome Glisse 	seq_printf(m, "Indirect2 wptr %u\n", ib2_wptr);
2629771fe6b9SJerome Glisse 	/* FIXME: 0, 128, 640 depends on fifo setup see cp_init_kms
2630771fe6b9SJerome Glisse 	 * 128 = indirect1_start * 8 & 640 = indirect2_start * 8 */
2631771fe6b9SJerome Glisse 	seq_printf(m, "Ring fifo:\n");
2632771fe6b9SJerome Glisse 	for (i = 0; i < 256; i++) {
2633771fe6b9SJerome Glisse 		WREG32(RADEON_CP_CSQ_ADDR, i << 2);
2634771fe6b9SJerome Glisse 		tmp = RREG32(RADEON_CP_CSQ_DATA);
2635771fe6b9SJerome Glisse 		seq_printf(m, "rfifo[%04d]=0x%08X\n", i, tmp);
2636771fe6b9SJerome Glisse 	}
2637771fe6b9SJerome Glisse 	seq_printf(m, "Indirect1 fifo:\n");
2638771fe6b9SJerome Glisse 	for (i = 256; i <= 512; i++) {
2639771fe6b9SJerome Glisse 		WREG32(RADEON_CP_CSQ_ADDR, i << 2);
2640771fe6b9SJerome Glisse 		tmp = RREG32(RADEON_CP_CSQ_DATA);
2641771fe6b9SJerome Glisse 		seq_printf(m, "ib1fifo[%04d]=0x%08X\n", i, tmp);
2642771fe6b9SJerome Glisse 	}
2643771fe6b9SJerome Glisse 	seq_printf(m, "Indirect2 fifo:\n");
2644771fe6b9SJerome Glisse 	for (i = 640; i < ib1_wptr; i++) {
2645771fe6b9SJerome Glisse 		WREG32(RADEON_CP_CSQ_ADDR, i << 2);
2646771fe6b9SJerome Glisse 		tmp = RREG32(RADEON_CP_CSQ_DATA);
2647771fe6b9SJerome Glisse 		seq_printf(m, "ib2fifo[%04d]=0x%08X\n", i, tmp);
2648771fe6b9SJerome Glisse 	}
2649771fe6b9SJerome Glisse 	return 0;
2650771fe6b9SJerome Glisse }
2651771fe6b9SJerome Glisse 
2652771fe6b9SJerome Glisse static int r100_debugfs_mc_info(struct seq_file *m, void *data)
2653771fe6b9SJerome Glisse {
2654771fe6b9SJerome Glisse 	struct drm_info_node *node = (struct drm_info_node *) m->private;
2655771fe6b9SJerome Glisse 	struct drm_device *dev = node->minor->dev;
2656771fe6b9SJerome Glisse 	struct radeon_device *rdev = dev->dev_private;
2657771fe6b9SJerome Glisse 	uint32_t tmp;
2658771fe6b9SJerome Glisse 
2659771fe6b9SJerome Glisse 	tmp = RREG32(RADEON_CONFIG_MEMSIZE);
2660771fe6b9SJerome Glisse 	seq_printf(m, "CONFIG_MEMSIZE 0x%08x\n", tmp);
2661771fe6b9SJerome Glisse 	tmp = RREG32(RADEON_MC_FB_LOCATION);
2662771fe6b9SJerome Glisse 	seq_printf(m, "MC_FB_LOCATION 0x%08x\n", tmp);
2663771fe6b9SJerome Glisse 	tmp = RREG32(RADEON_BUS_CNTL);
2664771fe6b9SJerome Glisse 	seq_printf(m, "BUS_CNTL 0x%08x\n", tmp);
2665771fe6b9SJerome Glisse 	tmp = RREG32(RADEON_MC_AGP_LOCATION);
2666771fe6b9SJerome Glisse 	seq_printf(m, "MC_AGP_LOCATION 0x%08x\n", tmp);
2667771fe6b9SJerome Glisse 	tmp = RREG32(RADEON_AGP_BASE);
2668771fe6b9SJerome Glisse 	seq_printf(m, "AGP_BASE 0x%08x\n", tmp);
2669771fe6b9SJerome Glisse 	tmp = RREG32(RADEON_HOST_PATH_CNTL);
2670771fe6b9SJerome Glisse 	seq_printf(m, "HOST_PATH_CNTL 0x%08x\n", tmp);
2671771fe6b9SJerome Glisse 	tmp = RREG32(0x01D0);
2672771fe6b9SJerome Glisse 	seq_printf(m, "AIC_CTRL 0x%08x\n", tmp);
2673771fe6b9SJerome Glisse 	tmp = RREG32(RADEON_AIC_LO_ADDR);
2674771fe6b9SJerome Glisse 	seq_printf(m, "AIC_LO_ADDR 0x%08x\n", tmp);
2675771fe6b9SJerome Glisse 	tmp = RREG32(RADEON_AIC_HI_ADDR);
2676771fe6b9SJerome Glisse 	seq_printf(m, "AIC_HI_ADDR 0x%08x\n", tmp);
2677771fe6b9SJerome Glisse 	tmp = RREG32(0x01E4);
2678771fe6b9SJerome Glisse 	seq_printf(m, "AIC_TLB_ADDR 0x%08x\n", tmp);
2679771fe6b9SJerome Glisse 	return 0;
2680771fe6b9SJerome Glisse }
2681771fe6b9SJerome Glisse 
2682771fe6b9SJerome Glisse static struct drm_info_list r100_debugfs_rbbm_list[] = {
2683771fe6b9SJerome Glisse 	{"r100_rbbm_info", r100_debugfs_rbbm_info, 0, NULL},
2684771fe6b9SJerome Glisse };
2685771fe6b9SJerome Glisse 
2686771fe6b9SJerome Glisse static struct drm_info_list r100_debugfs_cp_list[] = {
2687771fe6b9SJerome Glisse 	{"r100_cp_ring_info", r100_debugfs_cp_ring_info, 0, NULL},
2688771fe6b9SJerome Glisse 	{"r100_cp_csq_fifo", r100_debugfs_cp_csq_fifo, 0, NULL},
2689771fe6b9SJerome Glisse };
2690771fe6b9SJerome Glisse 
2691771fe6b9SJerome Glisse static struct drm_info_list r100_debugfs_mc_info_list[] = {
2692771fe6b9SJerome Glisse 	{"r100_mc_info", r100_debugfs_mc_info, 0, NULL},
2693771fe6b9SJerome Glisse };
2694771fe6b9SJerome Glisse #endif
2695771fe6b9SJerome Glisse 
2696771fe6b9SJerome Glisse int r100_debugfs_rbbm_init(struct radeon_device *rdev)
2697771fe6b9SJerome Glisse {
2698771fe6b9SJerome Glisse #if defined(CONFIG_DEBUG_FS)
2699771fe6b9SJerome Glisse 	return radeon_debugfs_add_files(rdev, r100_debugfs_rbbm_list, 1);
2700771fe6b9SJerome Glisse #else
2701771fe6b9SJerome Glisse 	return 0;
2702771fe6b9SJerome Glisse #endif
2703771fe6b9SJerome Glisse }
2704771fe6b9SJerome Glisse 
2705771fe6b9SJerome Glisse int r100_debugfs_cp_init(struct radeon_device *rdev)
2706771fe6b9SJerome Glisse {
2707771fe6b9SJerome Glisse #if defined(CONFIG_DEBUG_FS)
2708771fe6b9SJerome Glisse 	return radeon_debugfs_add_files(rdev, r100_debugfs_cp_list, 2);
2709771fe6b9SJerome Glisse #else
2710771fe6b9SJerome Glisse 	return 0;
2711771fe6b9SJerome Glisse #endif
2712771fe6b9SJerome Glisse }
2713771fe6b9SJerome Glisse 
2714771fe6b9SJerome Glisse int r100_debugfs_mc_info_init(struct radeon_device *rdev)
2715771fe6b9SJerome Glisse {
2716771fe6b9SJerome Glisse #if defined(CONFIG_DEBUG_FS)
2717771fe6b9SJerome Glisse 	return radeon_debugfs_add_files(rdev, r100_debugfs_mc_info_list, 1);
2718771fe6b9SJerome Glisse #else
2719771fe6b9SJerome Glisse 	return 0;
2720771fe6b9SJerome Glisse #endif
2721771fe6b9SJerome Glisse }
2722e024e110SDave Airlie 
2723e024e110SDave Airlie int r100_set_surface_reg(struct radeon_device *rdev, int reg,
2724e024e110SDave Airlie 			 uint32_t tiling_flags, uint32_t pitch,
2725e024e110SDave Airlie 			 uint32_t offset, uint32_t obj_size)
2726e024e110SDave Airlie {
2727e024e110SDave Airlie 	int surf_index = reg * 16;
2728e024e110SDave Airlie 	int flags = 0;
2729e024e110SDave Airlie 
2730e024e110SDave Airlie 	if (rdev->family <= CHIP_RS200) {
2731e024e110SDave Airlie 		if ((tiling_flags & (RADEON_TILING_MACRO|RADEON_TILING_MICRO))
2732e024e110SDave Airlie 				 == (RADEON_TILING_MACRO|RADEON_TILING_MICRO))
2733e024e110SDave Airlie 			flags |= RADEON_SURF_TILE_COLOR_BOTH;
2734e024e110SDave Airlie 		if (tiling_flags & RADEON_TILING_MACRO)
2735e024e110SDave Airlie 			flags |= RADEON_SURF_TILE_COLOR_MACRO;
2736e024e110SDave Airlie 	} else if (rdev->family <= CHIP_RV280) {
2737e024e110SDave Airlie 		if (tiling_flags & (RADEON_TILING_MACRO))
2738e024e110SDave Airlie 			flags |= R200_SURF_TILE_COLOR_MACRO;
2739e024e110SDave Airlie 		if (tiling_flags & RADEON_TILING_MICRO)
2740e024e110SDave Airlie 			flags |= R200_SURF_TILE_COLOR_MICRO;
2741e024e110SDave Airlie 	} else {
2742e024e110SDave Airlie 		if (tiling_flags & RADEON_TILING_MACRO)
2743e024e110SDave Airlie 			flags |= R300_SURF_TILE_MACRO;
2744e024e110SDave Airlie 		if (tiling_flags & RADEON_TILING_MICRO)
2745e024e110SDave Airlie 			flags |= R300_SURF_TILE_MICRO;
2746e024e110SDave Airlie 	}
2747e024e110SDave Airlie 
2748c88f9f0cSMichel Dänzer 	if (tiling_flags & RADEON_TILING_SWAP_16BIT)
2749c88f9f0cSMichel Dänzer 		flags |= RADEON_SURF_AP0_SWP_16BPP | RADEON_SURF_AP1_SWP_16BPP;
2750c88f9f0cSMichel Dänzer 	if (tiling_flags & RADEON_TILING_SWAP_32BIT)
2751c88f9f0cSMichel Dänzer 		flags |= RADEON_SURF_AP0_SWP_32BPP | RADEON_SURF_AP1_SWP_32BPP;
2752c88f9f0cSMichel Dänzer 
2753f5c5f040SDave Airlie 	/* when we aren't tiling the pitch seems to needs to be furtherdivided down. - tested on power5 + rn50 server */
2754f5c5f040SDave Airlie 	if (tiling_flags & (RADEON_TILING_SWAP_16BIT | RADEON_TILING_SWAP_32BIT)) {
2755f5c5f040SDave Airlie 		if (!(tiling_flags & (RADEON_TILING_MACRO | RADEON_TILING_MICRO)))
2756f5c5f040SDave Airlie 			if (ASIC_IS_RN50(rdev))
2757f5c5f040SDave Airlie 				pitch /= 16;
2758f5c5f040SDave Airlie 	}
2759f5c5f040SDave Airlie 
2760f5c5f040SDave Airlie 	/* r100/r200 divide by 16 */
2761f5c5f040SDave Airlie 	if (rdev->family < CHIP_R300)
2762f5c5f040SDave Airlie 		flags |= pitch / 16;
2763f5c5f040SDave Airlie 	else
2764f5c5f040SDave Airlie 		flags |= pitch / 8;
2765f5c5f040SDave Airlie 
2766f5c5f040SDave Airlie 
2767d9fdaafbSDave Airlie 	DRM_DEBUG_KMS("writing surface %d %d %x %x\n", reg, flags, offset, offset+obj_size-1);
2768e024e110SDave Airlie 	WREG32(RADEON_SURFACE0_INFO + surf_index, flags);
2769e024e110SDave Airlie 	WREG32(RADEON_SURFACE0_LOWER_BOUND + surf_index, offset);
2770e024e110SDave Airlie 	WREG32(RADEON_SURFACE0_UPPER_BOUND + surf_index, offset + obj_size - 1);
2771e024e110SDave Airlie 	return 0;
2772e024e110SDave Airlie }
2773e024e110SDave Airlie 
2774e024e110SDave Airlie void r100_clear_surface_reg(struct radeon_device *rdev, int reg)
2775e024e110SDave Airlie {
2776e024e110SDave Airlie 	int surf_index = reg * 16;
2777e024e110SDave Airlie 	WREG32(RADEON_SURFACE0_INFO + surf_index, 0);
2778e024e110SDave Airlie }
2779c93bb85bSJerome Glisse 
2780c93bb85bSJerome Glisse void r100_bandwidth_update(struct radeon_device *rdev)
2781c93bb85bSJerome Glisse {
2782c93bb85bSJerome Glisse 	fixed20_12 trcd_ff, trp_ff, tras_ff, trbs_ff, tcas_ff;
2783c93bb85bSJerome Glisse 	fixed20_12 sclk_ff, mclk_ff, sclk_eff_ff, sclk_delay_ff;
2784c93bb85bSJerome Glisse 	fixed20_12 peak_disp_bw, mem_bw, pix_clk, pix_clk2, temp_ff, crit_point_ff;
2785c93bb85bSJerome Glisse 	uint32_t temp, data, mem_trcd, mem_trp, mem_tras;
2786c93bb85bSJerome Glisse 	fixed20_12 memtcas_ff[8] = {
278768adac5eSBen Skeggs 		dfixed_init(1),
278868adac5eSBen Skeggs 		dfixed_init(2),
278968adac5eSBen Skeggs 		dfixed_init(3),
279068adac5eSBen Skeggs 		dfixed_init(0),
279168adac5eSBen Skeggs 		dfixed_init_half(1),
279268adac5eSBen Skeggs 		dfixed_init_half(2),
279368adac5eSBen Skeggs 		dfixed_init(0),
2794c93bb85bSJerome Glisse 	};
2795c93bb85bSJerome Glisse 	fixed20_12 memtcas_rs480_ff[8] = {
279668adac5eSBen Skeggs 		dfixed_init(0),
279768adac5eSBen Skeggs 		dfixed_init(1),
279868adac5eSBen Skeggs 		dfixed_init(2),
279968adac5eSBen Skeggs 		dfixed_init(3),
280068adac5eSBen Skeggs 		dfixed_init(0),
280168adac5eSBen Skeggs 		dfixed_init_half(1),
280268adac5eSBen Skeggs 		dfixed_init_half(2),
280368adac5eSBen Skeggs 		dfixed_init_half(3),
2804c93bb85bSJerome Glisse 	};
2805c93bb85bSJerome Glisse 	fixed20_12 memtcas2_ff[8] = {
280668adac5eSBen Skeggs 		dfixed_init(0),
280768adac5eSBen Skeggs 		dfixed_init(1),
280868adac5eSBen Skeggs 		dfixed_init(2),
280968adac5eSBen Skeggs 		dfixed_init(3),
281068adac5eSBen Skeggs 		dfixed_init(4),
281168adac5eSBen Skeggs 		dfixed_init(5),
281268adac5eSBen Skeggs 		dfixed_init(6),
281368adac5eSBen Skeggs 		dfixed_init(7),
2814c93bb85bSJerome Glisse 	};
2815c93bb85bSJerome Glisse 	fixed20_12 memtrbs[8] = {
281668adac5eSBen Skeggs 		dfixed_init(1),
281768adac5eSBen Skeggs 		dfixed_init_half(1),
281868adac5eSBen Skeggs 		dfixed_init(2),
281968adac5eSBen Skeggs 		dfixed_init_half(2),
282068adac5eSBen Skeggs 		dfixed_init(3),
282168adac5eSBen Skeggs 		dfixed_init_half(3),
282268adac5eSBen Skeggs 		dfixed_init(4),
282368adac5eSBen Skeggs 		dfixed_init_half(4)
2824c93bb85bSJerome Glisse 	};
2825c93bb85bSJerome Glisse 	fixed20_12 memtrbs_r4xx[8] = {
282668adac5eSBen Skeggs 		dfixed_init(4),
282768adac5eSBen Skeggs 		dfixed_init(5),
282868adac5eSBen Skeggs 		dfixed_init(6),
282968adac5eSBen Skeggs 		dfixed_init(7),
283068adac5eSBen Skeggs 		dfixed_init(8),
283168adac5eSBen Skeggs 		dfixed_init(9),
283268adac5eSBen Skeggs 		dfixed_init(10),
283368adac5eSBen Skeggs 		dfixed_init(11)
2834c93bb85bSJerome Glisse 	};
2835c93bb85bSJerome Glisse 	fixed20_12 min_mem_eff;
2836c93bb85bSJerome Glisse 	fixed20_12 mc_latency_sclk, mc_latency_mclk, k1;
2837c93bb85bSJerome Glisse 	fixed20_12 cur_latency_mclk, cur_latency_sclk;
2838c93bb85bSJerome Glisse 	fixed20_12 disp_latency, disp_latency_overhead, disp_drain_rate,
2839c93bb85bSJerome Glisse 		disp_drain_rate2, read_return_rate;
2840c93bb85bSJerome Glisse 	fixed20_12 time_disp1_drop_priority;
2841c93bb85bSJerome Glisse 	int c;
2842c93bb85bSJerome Glisse 	int cur_size = 16;       /* in octawords */
2843c93bb85bSJerome Glisse 	int critical_point = 0, critical_point2;
2844c93bb85bSJerome Glisse /* 	uint32_t read_return_rate, time_disp1_drop_priority; */
2845c93bb85bSJerome Glisse 	int stop_req, max_stop_req;
2846c93bb85bSJerome Glisse 	struct drm_display_mode *mode1 = NULL;
2847c93bb85bSJerome Glisse 	struct drm_display_mode *mode2 = NULL;
2848c93bb85bSJerome Glisse 	uint32_t pixel_bytes1 = 0;
2849c93bb85bSJerome Glisse 	uint32_t pixel_bytes2 = 0;
2850c93bb85bSJerome Glisse 
2851f46c0120SAlex Deucher 	radeon_update_display_priority(rdev);
2852f46c0120SAlex Deucher 
2853c93bb85bSJerome Glisse 	if (rdev->mode_info.crtcs[0]->base.enabled) {
2854c93bb85bSJerome Glisse 		mode1 = &rdev->mode_info.crtcs[0]->base.mode;
2855c93bb85bSJerome Glisse 		pixel_bytes1 = rdev->mode_info.crtcs[0]->base.fb->bits_per_pixel / 8;
2856c93bb85bSJerome Glisse 	}
2857dfee5614SDave Airlie 	if (!(rdev->flags & RADEON_SINGLE_CRTC)) {
2858c93bb85bSJerome Glisse 		if (rdev->mode_info.crtcs[1]->base.enabled) {
2859c93bb85bSJerome Glisse 			mode2 = &rdev->mode_info.crtcs[1]->base.mode;
2860c93bb85bSJerome Glisse 			pixel_bytes2 = rdev->mode_info.crtcs[1]->base.fb->bits_per_pixel / 8;
2861c93bb85bSJerome Glisse 		}
2862dfee5614SDave Airlie 	}
2863c93bb85bSJerome Glisse 
286468adac5eSBen Skeggs 	min_mem_eff.full = dfixed_const_8(0);
2865c93bb85bSJerome Glisse 	/* get modes */
2866c93bb85bSJerome Glisse 	if ((rdev->disp_priority == 2) && ASIC_IS_R300(rdev)) {
2867c93bb85bSJerome Glisse 		uint32_t mc_init_misc_lat_timer = RREG32(R300_MC_INIT_MISC_LAT_TIMER);
2868c93bb85bSJerome Glisse 		mc_init_misc_lat_timer &= ~(R300_MC_DISP1R_INIT_LAT_MASK << R300_MC_DISP1R_INIT_LAT_SHIFT);
2869c93bb85bSJerome Glisse 		mc_init_misc_lat_timer &= ~(R300_MC_DISP0R_INIT_LAT_MASK << R300_MC_DISP0R_INIT_LAT_SHIFT);
2870c93bb85bSJerome Glisse 		/* check crtc enables */
2871c93bb85bSJerome Glisse 		if (mode2)
2872c93bb85bSJerome Glisse 			mc_init_misc_lat_timer |= (1 << R300_MC_DISP1R_INIT_LAT_SHIFT);
2873c93bb85bSJerome Glisse 		if (mode1)
2874c93bb85bSJerome Glisse 			mc_init_misc_lat_timer |= (1 << R300_MC_DISP0R_INIT_LAT_SHIFT);
2875c93bb85bSJerome Glisse 		WREG32(R300_MC_INIT_MISC_LAT_TIMER, mc_init_misc_lat_timer);
2876c93bb85bSJerome Glisse 	}
2877c93bb85bSJerome Glisse 
2878c93bb85bSJerome Glisse 	/*
2879c93bb85bSJerome Glisse 	 * determine is there is enough bw for current mode
2880c93bb85bSJerome Glisse 	 */
2881f47299c5SAlex Deucher 	sclk_ff = rdev->pm.sclk;
2882f47299c5SAlex Deucher 	mclk_ff = rdev->pm.mclk;
2883c93bb85bSJerome Glisse 
2884c93bb85bSJerome Glisse 	temp = (rdev->mc.vram_width / 8) * (rdev->mc.vram_is_ddr ? 2 : 1);
288568adac5eSBen Skeggs 	temp_ff.full = dfixed_const(temp);
288668adac5eSBen Skeggs 	mem_bw.full = dfixed_mul(mclk_ff, temp_ff);
2887c93bb85bSJerome Glisse 
2888c93bb85bSJerome Glisse 	pix_clk.full = 0;
2889c93bb85bSJerome Glisse 	pix_clk2.full = 0;
2890c93bb85bSJerome Glisse 	peak_disp_bw.full = 0;
2891c93bb85bSJerome Glisse 	if (mode1) {
289268adac5eSBen Skeggs 		temp_ff.full = dfixed_const(1000);
289368adac5eSBen Skeggs 		pix_clk.full = dfixed_const(mode1->clock); /* convert to fixed point */
289468adac5eSBen Skeggs 		pix_clk.full = dfixed_div(pix_clk, temp_ff);
289568adac5eSBen Skeggs 		temp_ff.full = dfixed_const(pixel_bytes1);
289668adac5eSBen Skeggs 		peak_disp_bw.full += dfixed_mul(pix_clk, temp_ff);
2897c93bb85bSJerome Glisse 	}
2898c93bb85bSJerome Glisse 	if (mode2) {
289968adac5eSBen Skeggs 		temp_ff.full = dfixed_const(1000);
290068adac5eSBen Skeggs 		pix_clk2.full = dfixed_const(mode2->clock); /* convert to fixed point */
290168adac5eSBen Skeggs 		pix_clk2.full = dfixed_div(pix_clk2, temp_ff);
290268adac5eSBen Skeggs 		temp_ff.full = dfixed_const(pixel_bytes2);
290368adac5eSBen Skeggs 		peak_disp_bw.full += dfixed_mul(pix_clk2, temp_ff);
2904c93bb85bSJerome Glisse 	}
2905c93bb85bSJerome Glisse 
290668adac5eSBen Skeggs 	mem_bw.full = dfixed_mul(mem_bw, min_mem_eff);
2907c93bb85bSJerome Glisse 	if (peak_disp_bw.full >= mem_bw.full) {
2908c93bb85bSJerome Glisse 		DRM_ERROR("You may not have enough display bandwidth for current mode\n"
2909c93bb85bSJerome Glisse 			  "If you have flickering problem, try to lower resolution, refresh rate, or color depth\n");
2910c93bb85bSJerome Glisse 	}
2911c93bb85bSJerome Glisse 
2912c93bb85bSJerome Glisse 	/*  Get values from the EXT_MEM_CNTL register...converting its contents. */
2913c93bb85bSJerome Glisse 	temp = RREG32(RADEON_MEM_TIMING_CNTL);
2914c93bb85bSJerome Glisse 	if ((rdev->family == CHIP_RV100) || (rdev->flags & RADEON_IS_IGP)) { /* RV100, M6, IGPs */
2915c93bb85bSJerome Glisse 		mem_trcd = ((temp >> 2) & 0x3) + 1;
2916c93bb85bSJerome Glisse 		mem_trp  = ((temp & 0x3)) + 1;
2917c93bb85bSJerome Glisse 		mem_tras = ((temp & 0x70) >> 4) + 1;
2918c93bb85bSJerome Glisse 	} else if (rdev->family == CHIP_R300 ||
2919c93bb85bSJerome Glisse 		   rdev->family == CHIP_R350) { /* r300, r350 */
2920c93bb85bSJerome Glisse 		mem_trcd = (temp & 0x7) + 1;
2921c93bb85bSJerome Glisse 		mem_trp = ((temp >> 8) & 0x7) + 1;
2922c93bb85bSJerome Glisse 		mem_tras = ((temp >> 11) & 0xf) + 4;
2923c93bb85bSJerome Glisse 	} else if (rdev->family == CHIP_RV350 ||
2924c93bb85bSJerome Glisse 		   rdev->family <= CHIP_RV380) {
2925c93bb85bSJerome Glisse 		/* rv3x0 */
2926c93bb85bSJerome Glisse 		mem_trcd = (temp & 0x7) + 3;
2927c93bb85bSJerome Glisse 		mem_trp = ((temp >> 8) & 0x7) + 3;
2928c93bb85bSJerome Glisse 		mem_tras = ((temp >> 11) & 0xf) + 6;
2929c93bb85bSJerome Glisse 	} else if (rdev->family == CHIP_R420 ||
2930c93bb85bSJerome Glisse 		   rdev->family == CHIP_R423 ||
2931c93bb85bSJerome Glisse 		   rdev->family == CHIP_RV410) {
2932c93bb85bSJerome Glisse 		/* r4xx */
2933c93bb85bSJerome Glisse 		mem_trcd = (temp & 0xf) + 3;
2934c93bb85bSJerome Glisse 		if (mem_trcd > 15)
2935c93bb85bSJerome Glisse 			mem_trcd = 15;
2936c93bb85bSJerome Glisse 		mem_trp = ((temp >> 8) & 0xf) + 3;
2937c93bb85bSJerome Glisse 		if (mem_trp > 15)
2938c93bb85bSJerome Glisse 			mem_trp = 15;
2939c93bb85bSJerome Glisse 		mem_tras = ((temp >> 12) & 0x1f) + 6;
2940c93bb85bSJerome Glisse 		if (mem_tras > 31)
2941c93bb85bSJerome Glisse 			mem_tras = 31;
2942c93bb85bSJerome Glisse 	} else { /* RV200, R200 */
2943c93bb85bSJerome Glisse 		mem_trcd = (temp & 0x7) + 1;
2944c93bb85bSJerome Glisse 		mem_trp = ((temp >> 8) & 0x7) + 1;
2945c93bb85bSJerome Glisse 		mem_tras = ((temp >> 12) & 0xf) + 4;
2946c93bb85bSJerome Glisse 	}
2947c93bb85bSJerome Glisse 	/* convert to FF */
294868adac5eSBen Skeggs 	trcd_ff.full = dfixed_const(mem_trcd);
294968adac5eSBen Skeggs 	trp_ff.full = dfixed_const(mem_trp);
295068adac5eSBen Skeggs 	tras_ff.full = dfixed_const(mem_tras);
2951c93bb85bSJerome Glisse 
2952c93bb85bSJerome Glisse 	/* Get values from the MEM_SDRAM_MODE_REG register...converting its */
2953c93bb85bSJerome Glisse 	temp = RREG32(RADEON_MEM_SDRAM_MODE_REG);
2954c93bb85bSJerome Glisse 	data = (temp & (7 << 20)) >> 20;
2955c93bb85bSJerome Glisse 	if ((rdev->family == CHIP_RV100) || rdev->flags & RADEON_IS_IGP) {
2956c93bb85bSJerome Glisse 		if (rdev->family == CHIP_RS480) /* don't think rs400 */
2957c93bb85bSJerome Glisse 			tcas_ff = memtcas_rs480_ff[data];
2958c93bb85bSJerome Glisse 		else
2959c93bb85bSJerome Glisse 			tcas_ff = memtcas_ff[data];
2960c93bb85bSJerome Glisse 	} else
2961c93bb85bSJerome Glisse 		tcas_ff = memtcas2_ff[data];
2962c93bb85bSJerome Glisse 
2963c93bb85bSJerome Glisse 	if (rdev->family == CHIP_RS400 ||
2964c93bb85bSJerome Glisse 	    rdev->family == CHIP_RS480) {
2965c93bb85bSJerome Glisse 		/* extra cas latency stored in bits 23-25 0-4 clocks */
2966c93bb85bSJerome Glisse 		data = (temp >> 23) & 0x7;
2967c93bb85bSJerome Glisse 		if (data < 5)
296868adac5eSBen Skeggs 			tcas_ff.full += dfixed_const(data);
2969c93bb85bSJerome Glisse 	}
2970c93bb85bSJerome Glisse 
2971c93bb85bSJerome Glisse 	if (ASIC_IS_R300(rdev) && !(rdev->flags & RADEON_IS_IGP)) {
2972c93bb85bSJerome Glisse 		/* on the R300, Tcas is included in Trbs.
2973c93bb85bSJerome Glisse 		 */
2974c93bb85bSJerome Glisse 		temp = RREG32(RADEON_MEM_CNTL);
2975c93bb85bSJerome Glisse 		data = (R300_MEM_NUM_CHANNELS_MASK & temp);
2976c93bb85bSJerome Glisse 		if (data == 1) {
2977c93bb85bSJerome Glisse 			if (R300_MEM_USE_CD_CH_ONLY & temp) {
2978c93bb85bSJerome Glisse 				temp = RREG32(R300_MC_IND_INDEX);
2979c93bb85bSJerome Glisse 				temp &= ~R300_MC_IND_ADDR_MASK;
2980c93bb85bSJerome Glisse 				temp |= R300_MC_READ_CNTL_CD_mcind;
2981c93bb85bSJerome Glisse 				WREG32(R300_MC_IND_INDEX, temp);
2982c93bb85bSJerome Glisse 				temp = RREG32(R300_MC_IND_DATA);
2983c93bb85bSJerome Glisse 				data = (R300_MEM_RBS_POSITION_C_MASK & temp);
2984c93bb85bSJerome Glisse 			} else {
2985c93bb85bSJerome Glisse 				temp = RREG32(R300_MC_READ_CNTL_AB);
2986c93bb85bSJerome Glisse 				data = (R300_MEM_RBS_POSITION_A_MASK & temp);
2987c93bb85bSJerome Glisse 			}
2988c93bb85bSJerome Glisse 		} else {
2989c93bb85bSJerome Glisse 			temp = RREG32(R300_MC_READ_CNTL_AB);
2990c93bb85bSJerome Glisse 			data = (R300_MEM_RBS_POSITION_A_MASK & temp);
2991c93bb85bSJerome Glisse 		}
2992c93bb85bSJerome Glisse 		if (rdev->family == CHIP_RV410 ||
2993c93bb85bSJerome Glisse 		    rdev->family == CHIP_R420 ||
2994c93bb85bSJerome Glisse 		    rdev->family == CHIP_R423)
2995c93bb85bSJerome Glisse 			trbs_ff = memtrbs_r4xx[data];
2996c93bb85bSJerome Glisse 		else
2997c93bb85bSJerome Glisse 			trbs_ff = memtrbs[data];
2998c93bb85bSJerome Glisse 		tcas_ff.full += trbs_ff.full;
2999c93bb85bSJerome Glisse 	}
3000c93bb85bSJerome Glisse 
3001c93bb85bSJerome Glisse 	sclk_eff_ff.full = sclk_ff.full;
3002c93bb85bSJerome Glisse 
3003c93bb85bSJerome Glisse 	if (rdev->flags & RADEON_IS_AGP) {
3004c93bb85bSJerome Glisse 		fixed20_12 agpmode_ff;
300568adac5eSBen Skeggs 		agpmode_ff.full = dfixed_const(radeon_agpmode);
300668adac5eSBen Skeggs 		temp_ff.full = dfixed_const_666(16);
300768adac5eSBen Skeggs 		sclk_eff_ff.full -= dfixed_mul(agpmode_ff, temp_ff);
3008c93bb85bSJerome Glisse 	}
3009c93bb85bSJerome Glisse 	/* TODO PCIE lanes may affect this - agpmode == 16?? */
3010c93bb85bSJerome Glisse 
3011c93bb85bSJerome Glisse 	if (ASIC_IS_R300(rdev)) {
301268adac5eSBen Skeggs 		sclk_delay_ff.full = dfixed_const(250);
3013c93bb85bSJerome Glisse 	} else {
3014c93bb85bSJerome Glisse 		if ((rdev->family == CHIP_RV100) ||
3015c93bb85bSJerome Glisse 		    rdev->flags & RADEON_IS_IGP) {
3016c93bb85bSJerome Glisse 			if (rdev->mc.vram_is_ddr)
301768adac5eSBen Skeggs 				sclk_delay_ff.full = dfixed_const(41);
3018c93bb85bSJerome Glisse 			else
301968adac5eSBen Skeggs 				sclk_delay_ff.full = dfixed_const(33);
3020c93bb85bSJerome Glisse 		} else {
3021c93bb85bSJerome Glisse 			if (rdev->mc.vram_width == 128)
302268adac5eSBen Skeggs 				sclk_delay_ff.full = dfixed_const(57);
3023c93bb85bSJerome Glisse 			else
302468adac5eSBen Skeggs 				sclk_delay_ff.full = dfixed_const(41);
3025c93bb85bSJerome Glisse 		}
3026c93bb85bSJerome Glisse 	}
3027c93bb85bSJerome Glisse 
302868adac5eSBen Skeggs 	mc_latency_sclk.full = dfixed_div(sclk_delay_ff, sclk_eff_ff);
3029c93bb85bSJerome Glisse 
3030c93bb85bSJerome Glisse 	if (rdev->mc.vram_is_ddr) {
3031c93bb85bSJerome Glisse 		if (rdev->mc.vram_width == 32) {
303268adac5eSBen Skeggs 			k1.full = dfixed_const(40);
3033c93bb85bSJerome Glisse 			c  = 3;
3034c93bb85bSJerome Glisse 		} else {
303568adac5eSBen Skeggs 			k1.full = dfixed_const(20);
3036c93bb85bSJerome Glisse 			c  = 1;
3037c93bb85bSJerome Glisse 		}
3038c93bb85bSJerome Glisse 	} else {
303968adac5eSBen Skeggs 		k1.full = dfixed_const(40);
3040c93bb85bSJerome Glisse 		c  = 3;
3041c93bb85bSJerome Glisse 	}
3042c93bb85bSJerome Glisse 
304368adac5eSBen Skeggs 	temp_ff.full = dfixed_const(2);
304468adac5eSBen Skeggs 	mc_latency_mclk.full = dfixed_mul(trcd_ff, temp_ff);
304568adac5eSBen Skeggs 	temp_ff.full = dfixed_const(c);
304668adac5eSBen Skeggs 	mc_latency_mclk.full += dfixed_mul(tcas_ff, temp_ff);
304768adac5eSBen Skeggs 	temp_ff.full = dfixed_const(4);
304868adac5eSBen Skeggs 	mc_latency_mclk.full += dfixed_mul(tras_ff, temp_ff);
304968adac5eSBen Skeggs 	mc_latency_mclk.full += dfixed_mul(trp_ff, temp_ff);
3050c93bb85bSJerome Glisse 	mc_latency_mclk.full += k1.full;
3051c93bb85bSJerome Glisse 
305268adac5eSBen Skeggs 	mc_latency_mclk.full = dfixed_div(mc_latency_mclk, mclk_ff);
305368adac5eSBen Skeggs 	mc_latency_mclk.full += dfixed_div(temp_ff, sclk_eff_ff);
3054c93bb85bSJerome Glisse 
3055c93bb85bSJerome Glisse 	/*
3056c93bb85bSJerome Glisse 	  HW cursor time assuming worst case of full size colour cursor.
3057c93bb85bSJerome Glisse 	*/
305868adac5eSBen Skeggs 	temp_ff.full = dfixed_const((2 * (cur_size - (rdev->mc.vram_is_ddr + 1))));
3059c93bb85bSJerome Glisse 	temp_ff.full += trcd_ff.full;
3060c93bb85bSJerome Glisse 	if (temp_ff.full < tras_ff.full)
3061c93bb85bSJerome Glisse 		temp_ff.full = tras_ff.full;
306268adac5eSBen Skeggs 	cur_latency_mclk.full = dfixed_div(temp_ff, mclk_ff);
3063c93bb85bSJerome Glisse 
306468adac5eSBen Skeggs 	temp_ff.full = dfixed_const(cur_size);
306568adac5eSBen Skeggs 	cur_latency_sclk.full = dfixed_div(temp_ff, sclk_eff_ff);
3066c93bb85bSJerome Glisse 	/*
3067c93bb85bSJerome Glisse 	  Find the total latency for the display data.
3068c93bb85bSJerome Glisse 	*/
306968adac5eSBen Skeggs 	disp_latency_overhead.full = dfixed_const(8);
307068adac5eSBen Skeggs 	disp_latency_overhead.full = dfixed_div(disp_latency_overhead, sclk_ff);
3071c93bb85bSJerome Glisse 	mc_latency_mclk.full += disp_latency_overhead.full + cur_latency_mclk.full;
3072c93bb85bSJerome Glisse 	mc_latency_sclk.full += disp_latency_overhead.full + cur_latency_sclk.full;
3073c93bb85bSJerome Glisse 
3074c93bb85bSJerome Glisse 	if (mc_latency_mclk.full > mc_latency_sclk.full)
3075c93bb85bSJerome Glisse 		disp_latency.full = mc_latency_mclk.full;
3076c93bb85bSJerome Glisse 	else
3077c93bb85bSJerome Glisse 		disp_latency.full = mc_latency_sclk.full;
3078c93bb85bSJerome Glisse 
3079c93bb85bSJerome Glisse 	/* setup Max GRPH_STOP_REQ default value */
3080c93bb85bSJerome Glisse 	if (ASIC_IS_RV100(rdev))
3081c93bb85bSJerome Glisse 		max_stop_req = 0x5c;
3082c93bb85bSJerome Glisse 	else
3083c93bb85bSJerome Glisse 		max_stop_req = 0x7c;
3084c93bb85bSJerome Glisse 
3085c93bb85bSJerome Glisse 	if (mode1) {
3086c93bb85bSJerome Glisse 		/*  CRTC1
3087c93bb85bSJerome Glisse 		    Set GRPH_BUFFER_CNTL register using h/w defined optimal values.
3088c93bb85bSJerome Glisse 		    GRPH_STOP_REQ <= MIN[ 0x7C, (CRTC_H_DISP + 1) * (bit depth) / 0x10 ]
3089c93bb85bSJerome Glisse 		*/
3090c93bb85bSJerome Glisse 		stop_req = mode1->hdisplay * pixel_bytes1 / 16;
3091c93bb85bSJerome Glisse 
3092c93bb85bSJerome Glisse 		if (stop_req > max_stop_req)
3093c93bb85bSJerome Glisse 			stop_req = max_stop_req;
3094c93bb85bSJerome Glisse 
3095c93bb85bSJerome Glisse 		/*
3096c93bb85bSJerome Glisse 		  Find the drain rate of the display buffer.
3097c93bb85bSJerome Glisse 		*/
309868adac5eSBen Skeggs 		temp_ff.full = dfixed_const((16/pixel_bytes1));
309968adac5eSBen Skeggs 		disp_drain_rate.full = dfixed_div(pix_clk, temp_ff);
3100c93bb85bSJerome Glisse 
3101c93bb85bSJerome Glisse 		/*
3102c93bb85bSJerome Glisse 		  Find the critical point of the display buffer.
3103c93bb85bSJerome Glisse 		*/
310468adac5eSBen Skeggs 		crit_point_ff.full = dfixed_mul(disp_drain_rate, disp_latency);
310568adac5eSBen Skeggs 		crit_point_ff.full += dfixed_const_half(0);
3106c93bb85bSJerome Glisse 
310768adac5eSBen Skeggs 		critical_point = dfixed_trunc(crit_point_ff);
3108c93bb85bSJerome Glisse 
3109c93bb85bSJerome Glisse 		if (rdev->disp_priority == 2) {
3110c93bb85bSJerome Glisse 			critical_point = 0;
3111c93bb85bSJerome Glisse 		}
3112c93bb85bSJerome Glisse 
3113c93bb85bSJerome Glisse 		/*
3114c93bb85bSJerome Glisse 		  The critical point should never be above max_stop_req-4.  Setting
3115c93bb85bSJerome Glisse 		  GRPH_CRITICAL_CNTL = 0 will thus force high priority all the time.
3116c93bb85bSJerome Glisse 		*/
3117c93bb85bSJerome Glisse 		if (max_stop_req - critical_point < 4)
3118c93bb85bSJerome Glisse 			critical_point = 0;
3119c93bb85bSJerome Glisse 
3120c93bb85bSJerome Glisse 		if (critical_point == 0 && mode2 && rdev->family == CHIP_R300) {
3121c93bb85bSJerome Glisse 			/* some R300 cards have problem with this set to 0, when CRTC2 is enabled.*/
3122c93bb85bSJerome Glisse 			critical_point = 0x10;
3123c93bb85bSJerome Glisse 		}
3124c93bb85bSJerome Glisse 
3125c93bb85bSJerome Glisse 		temp = RREG32(RADEON_GRPH_BUFFER_CNTL);
3126c93bb85bSJerome Glisse 		temp &= ~(RADEON_GRPH_STOP_REQ_MASK);
3127c93bb85bSJerome Glisse 		temp |= (stop_req << RADEON_GRPH_STOP_REQ_SHIFT);
3128c93bb85bSJerome Glisse 		temp &= ~(RADEON_GRPH_START_REQ_MASK);
3129c93bb85bSJerome Glisse 		if ((rdev->family == CHIP_R350) &&
3130c93bb85bSJerome Glisse 		    (stop_req > 0x15)) {
3131c93bb85bSJerome Glisse 			stop_req -= 0x10;
3132c93bb85bSJerome Glisse 		}
3133c93bb85bSJerome Glisse 		temp |= (stop_req << RADEON_GRPH_START_REQ_SHIFT);
3134c93bb85bSJerome Glisse 		temp |= RADEON_GRPH_BUFFER_SIZE;
3135c93bb85bSJerome Glisse 		temp &= ~(RADEON_GRPH_CRITICAL_CNTL   |
3136c93bb85bSJerome Glisse 			  RADEON_GRPH_CRITICAL_AT_SOF |
3137c93bb85bSJerome Glisse 			  RADEON_GRPH_STOP_CNTL);
3138c93bb85bSJerome Glisse 		/*
3139c93bb85bSJerome Glisse 		  Write the result into the register.
3140c93bb85bSJerome Glisse 		*/
3141c93bb85bSJerome Glisse 		WREG32(RADEON_GRPH_BUFFER_CNTL, ((temp & ~RADEON_GRPH_CRITICAL_POINT_MASK) |
3142c93bb85bSJerome Glisse 						       (critical_point << RADEON_GRPH_CRITICAL_POINT_SHIFT)));
3143c93bb85bSJerome Glisse 
3144c93bb85bSJerome Glisse #if 0
3145c93bb85bSJerome Glisse 		if ((rdev->family == CHIP_RS400) ||
3146c93bb85bSJerome Glisse 		    (rdev->family == CHIP_RS480)) {
3147c93bb85bSJerome Glisse 			/* attempt to program RS400 disp regs correctly ??? */
3148c93bb85bSJerome Glisse 			temp = RREG32(RS400_DISP1_REG_CNTL);
3149c93bb85bSJerome Glisse 			temp &= ~(RS400_DISP1_START_REQ_LEVEL_MASK |
3150c93bb85bSJerome Glisse 				  RS400_DISP1_STOP_REQ_LEVEL_MASK);
3151c93bb85bSJerome Glisse 			WREG32(RS400_DISP1_REQ_CNTL1, (temp |
3152c93bb85bSJerome Glisse 						       (critical_point << RS400_DISP1_START_REQ_LEVEL_SHIFT) |
3153c93bb85bSJerome Glisse 						       (critical_point << RS400_DISP1_STOP_REQ_LEVEL_SHIFT)));
3154c93bb85bSJerome Glisse 			temp = RREG32(RS400_DMIF_MEM_CNTL1);
3155c93bb85bSJerome Glisse 			temp &= ~(RS400_DISP1_CRITICAL_POINT_START_MASK |
3156c93bb85bSJerome Glisse 				  RS400_DISP1_CRITICAL_POINT_STOP_MASK);
3157c93bb85bSJerome Glisse 			WREG32(RS400_DMIF_MEM_CNTL1, (temp |
3158c93bb85bSJerome Glisse 						      (critical_point << RS400_DISP1_CRITICAL_POINT_START_SHIFT) |
3159c93bb85bSJerome Glisse 						      (critical_point << RS400_DISP1_CRITICAL_POINT_STOP_SHIFT)));
3160c93bb85bSJerome Glisse 		}
3161c93bb85bSJerome Glisse #endif
3162c93bb85bSJerome Glisse 
3163d9fdaafbSDave Airlie 		DRM_DEBUG_KMS("GRPH_BUFFER_CNTL from to %x\n",
3164c93bb85bSJerome Glisse 			  /* 	  (unsigned int)info->SavedReg->grph_buffer_cntl, */
3165c93bb85bSJerome Glisse 			  (unsigned int)RREG32(RADEON_GRPH_BUFFER_CNTL));
3166c93bb85bSJerome Glisse 	}
3167c93bb85bSJerome Glisse 
3168c93bb85bSJerome Glisse 	if (mode2) {
3169c93bb85bSJerome Glisse 		u32 grph2_cntl;
3170c93bb85bSJerome Glisse 		stop_req = mode2->hdisplay * pixel_bytes2 / 16;
3171c93bb85bSJerome Glisse 
3172c93bb85bSJerome Glisse 		if (stop_req > max_stop_req)
3173c93bb85bSJerome Glisse 			stop_req = max_stop_req;
3174c93bb85bSJerome Glisse 
3175c93bb85bSJerome Glisse 		/*
3176c93bb85bSJerome Glisse 		  Find the drain rate of the display buffer.
3177c93bb85bSJerome Glisse 		*/
317868adac5eSBen Skeggs 		temp_ff.full = dfixed_const((16/pixel_bytes2));
317968adac5eSBen Skeggs 		disp_drain_rate2.full = dfixed_div(pix_clk2, temp_ff);
3180c93bb85bSJerome Glisse 
3181c93bb85bSJerome Glisse 		grph2_cntl = RREG32(RADEON_GRPH2_BUFFER_CNTL);
3182c93bb85bSJerome Glisse 		grph2_cntl &= ~(RADEON_GRPH_STOP_REQ_MASK);
3183c93bb85bSJerome Glisse 		grph2_cntl |= (stop_req << RADEON_GRPH_STOP_REQ_SHIFT);
3184c93bb85bSJerome Glisse 		grph2_cntl &= ~(RADEON_GRPH_START_REQ_MASK);
3185c93bb85bSJerome Glisse 		if ((rdev->family == CHIP_R350) &&
3186c93bb85bSJerome Glisse 		    (stop_req > 0x15)) {
3187c93bb85bSJerome Glisse 			stop_req -= 0x10;
3188c93bb85bSJerome Glisse 		}
3189c93bb85bSJerome Glisse 		grph2_cntl |= (stop_req << RADEON_GRPH_START_REQ_SHIFT);
3190c93bb85bSJerome Glisse 		grph2_cntl |= RADEON_GRPH_BUFFER_SIZE;
3191c93bb85bSJerome Glisse 		grph2_cntl &= ~(RADEON_GRPH_CRITICAL_CNTL   |
3192c93bb85bSJerome Glisse 			  RADEON_GRPH_CRITICAL_AT_SOF |
3193c93bb85bSJerome Glisse 			  RADEON_GRPH_STOP_CNTL);
3194c93bb85bSJerome Glisse 
3195c93bb85bSJerome Glisse 		if ((rdev->family == CHIP_RS100) ||
3196c93bb85bSJerome Glisse 		    (rdev->family == CHIP_RS200))
3197c93bb85bSJerome Glisse 			critical_point2 = 0;
3198c93bb85bSJerome Glisse 		else {
3199c93bb85bSJerome Glisse 			temp = (rdev->mc.vram_width * rdev->mc.vram_is_ddr + 1)/128;
320068adac5eSBen Skeggs 			temp_ff.full = dfixed_const(temp);
320168adac5eSBen Skeggs 			temp_ff.full = dfixed_mul(mclk_ff, temp_ff);
3202c93bb85bSJerome Glisse 			if (sclk_ff.full < temp_ff.full)
3203c93bb85bSJerome Glisse 				temp_ff.full = sclk_ff.full;
3204c93bb85bSJerome Glisse 
3205c93bb85bSJerome Glisse 			read_return_rate.full = temp_ff.full;
3206c93bb85bSJerome Glisse 
3207c93bb85bSJerome Glisse 			if (mode1) {
3208c93bb85bSJerome Glisse 				temp_ff.full = read_return_rate.full - disp_drain_rate.full;
320968adac5eSBen Skeggs 				time_disp1_drop_priority.full = dfixed_div(crit_point_ff, temp_ff);
3210c93bb85bSJerome Glisse 			} else {
3211c93bb85bSJerome Glisse 				time_disp1_drop_priority.full = 0;
3212c93bb85bSJerome Glisse 			}
3213c93bb85bSJerome Glisse 			crit_point_ff.full = disp_latency.full + time_disp1_drop_priority.full + disp_latency.full;
321468adac5eSBen Skeggs 			crit_point_ff.full = dfixed_mul(crit_point_ff, disp_drain_rate2);
321568adac5eSBen Skeggs 			crit_point_ff.full += dfixed_const_half(0);
3216c93bb85bSJerome Glisse 
321768adac5eSBen Skeggs 			critical_point2 = dfixed_trunc(crit_point_ff);
3218c93bb85bSJerome Glisse 
3219c93bb85bSJerome Glisse 			if (rdev->disp_priority == 2) {
3220c93bb85bSJerome Glisse 				critical_point2 = 0;
3221c93bb85bSJerome Glisse 			}
3222c93bb85bSJerome Glisse 
3223c93bb85bSJerome Glisse 			if (max_stop_req - critical_point2 < 4)
3224c93bb85bSJerome Glisse 				critical_point2 = 0;
3225c93bb85bSJerome Glisse 
3226c93bb85bSJerome Glisse 		}
3227c93bb85bSJerome Glisse 
3228c93bb85bSJerome Glisse 		if (critical_point2 == 0 && rdev->family == CHIP_R300) {
3229c93bb85bSJerome Glisse 			/* some R300 cards have problem with this set to 0 */
3230c93bb85bSJerome Glisse 			critical_point2 = 0x10;
3231c93bb85bSJerome Glisse 		}
3232c93bb85bSJerome Glisse 
3233c93bb85bSJerome Glisse 		WREG32(RADEON_GRPH2_BUFFER_CNTL, ((grph2_cntl & ~RADEON_GRPH_CRITICAL_POINT_MASK) |
3234c93bb85bSJerome Glisse 						  (critical_point2 << RADEON_GRPH_CRITICAL_POINT_SHIFT)));
3235c93bb85bSJerome Glisse 
3236c93bb85bSJerome Glisse 		if ((rdev->family == CHIP_RS400) ||
3237c93bb85bSJerome Glisse 		    (rdev->family == CHIP_RS480)) {
3238c93bb85bSJerome Glisse #if 0
3239c93bb85bSJerome Glisse 			/* attempt to program RS400 disp2 regs correctly ??? */
3240c93bb85bSJerome Glisse 			temp = RREG32(RS400_DISP2_REQ_CNTL1);
3241c93bb85bSJerome Glisse 			temp &= ~(RS400_DISP2_START_REQ_LEVEL_MASK |
3242c93bb85bSJerome Glisse 				  RS400_DISP2_STOP_REQ_LEVEL_MASK);
3243c93bb85bSJerome Glisse 			WREG32(RS400_DISP2_REQ_CNTL1, (temp |
3244c93bb85bSJerome Glisse 						       (critical_point2 << RS400_DISP1_START_REQ_LEVEL_SHIFT) |
3245c93bb85bSJerome Glisse 						       (critical_point2 << RS400_DISP1_STOP_REQ_LEVEL_SHIFT)));
3246c93bb85bSJerome Glisse 			temp = RREG32(RS400_DISP2_REQ_CNTL2);
3247c93bb85bSJerome Glisse 			temp &= ~(RS400_DISP2_CRITICAL_POINT_START_MASK |
3248c93bb85bSJerome Glisse 				  RS400_DISP2_CRITICAL_POINT_STOP_MASK);
3249c93bb85bSJerome Glisse 			WREG32(RS400_DISP2_REQ_CNTL2, (temp |
3250c93bb85bSJerome Glisse 						       (critical_point2 << RS400_DISP2_CRITICAL_POINT_START_SHIFT) |
3251c93bb85bSJerome Glisse 						       (critical_point2 << RS400_DISP2_CRITICAL_POINT_STOP_SHIFT)));
3252c93bb85bSJerome Glisse #endif
3253c93bb85bSJerome Glisse 			WREG32(RS400_DISP2_REQ_CNTL1, 0x105DC1CC);
3254c93bb85bSJerome Glisse 			WREG32(RS400_DISP2_REQ_CNTL2, 0x2749D000);
3255c93bb85bSJerome Glisse 			WREG32(RS400_DMIF_MEM_CNTL1,  0x29CA71DC);
3256c93bb85bSJerome Glisse 			WREG32(RS400_DISP1_REQ_CNTL1, 0x28FBC3AC);
3257c93bb85bSJerome Glisse 		}
3258c93bb85bSJerome Glisse 
3259d9fdaafbSDave Airlie 		DRM_DEBUG_KMS("GRPH2_BUFFER_CNTL from to %x\n",
3260c93bb85bSJerome Glisse 			  (unsigned int)RREG32(RADEON_GRPH2_BUFFER_CNTL));
3261c93bb85bSJerome Glisse 	}
3262c93bb85bSJerome Glisse }
3263551ebd83SDave Airlie 
3264cbdd4501SAndi Kleen static void r100_cs_track_texture_print(struct r100_cs_track_texture *t)
3265551ebd83SDave Airlie {
3266551ebd83SDave Airlie 	DRM_ERROR("pitch                      %d\n", t->pitch);
3267ceb776bcSMathias Fröhlich 	DRM_ERROR("use_pitch                  %d\n", t->use_pitch);
3268551ebd83SDave Airlie 	DRM_ERROR("width                      %d\n", t->width);
3269ceb776bcSMathias Fröhlich 	DRM_ERROR("width_11                   %d\n", t->width_11);
3270551ebd83SDave Airlie 	DRM_ERROR("height                     %d\n", t->height);
3271ceb776bcSMathias Fröhlich 	DRM_ERROR("height_11                  %d\n", t->height_11);
3272551ebd83SDave Airlie 	DRM_ERROR("num levels                 %d\n", t->num_levels);
3273551ebd83SDave Airlie 	DRM_ERROR("depth                      %d\n", t->txdepth);
3274551ebd83SDave Airlie 	DRM_ERROR("bpp                        %d\n", t->cpp);
3275551ebd83SDave Airlie 	DRM_ERROR("coordinate type            %d\n", t->tex_coord_type);
3276551ebd83SDave Airlie 	DRM_ERROR("width round to power of 2  %d\n", t->roundup_w);
3277551ebd83SDave Airlie 	DRM_ERROR("height round to power of 2 %d\n", t->roundup_h);
3278d785d78bSDave Airlie 	DRM_ERROR("compress format            %d\n", t->compress_format);
3279551ebd83SDave Airlie }
3280551ebd83SDave Airlie 
3281d785d78bSDave Airlie static int r100_track_compress_size(int compress_format, int w, int h)
3282d785d78bSDave Airlie {
3283d785d78bSDave Airlie 	int block_width, block_height, block_bytes;
3284d785d78bSDave Airlie 	int wblocks, hblocks;
3285d785d78bSDave Airlie 	int min_wblocks;
3286d785d78bSDave Airlie 	int sz;
3287d785d78bSDave Airlie 
3288d785d78bSDave Airlie 	block_width = 4;
3289d785d78bSDave Airlie 	block_height = 4;
3290d785d78bSDave Airlie 
3291d785d78bSDave Airlie 	switch (compress_format) {
3292d785d78bSDave Airlie 	case R100_TRACK_COMP_DXT1:
3293d785d78bSDave Airlie 		block_bytes = 8;
3294d785d78bSDave Airlie 		min_wblocks = 4;
3295d785d78bSDave Airlie 		break;
3296d785d78bSDave Airlie 	default:
3297d785d78bSDave Airlie 	case R100_TRACK_COMP_DXT35:
3298d785d78bSDave Airlie 		block_bytes = 16;
3299d785d78bSDave Airlie 		min_wblocks = 2;
3300d785d78bSDave Airlie 		break;
3301d785d78bSDave Airlie 	}
3302d785d78bSDave Airlie 
3303d785d78bSDave Airlie 	hblocks = (h + block_height - 1) / block_height;
3304d785d78bSDave Airlie 	wblocks = (w + block_width - 1) / block_width;
3305d785d78bSDave Airlie 	if (wblocks < min_wblocks)
3306d785d78bSDave Airlie 		wblocks = min_wblocks;
3307d785d78bSDave Airlie 	sz = wblocks * hblocks * block_bytes;
3308d785d78bSDave Airlie 	return sz;
3309d785d78bSDave Airlie }
3310d785d78bSDave Airlie 
331137cf6b03SRoland Scheidegger static int r100_cs_track_cube(struct radeon_device *rdev,
331237cf6b03SRoland Scheidegger 			      struct r100_cs_track *track, unsigned idx)
331337cf6b03SRoland Scheidegger {
331437cf6b03SRoland Scheidegger 	unsigned face, w, h;
331537cf6b03SRoland Scheidegger 	struct radeon_bo *cube_robj;
331637cf6b03SRoland Scheidegger 	unsigned long size;
331737cf6b03SRoland Scheidegger 	unsigned compress_format = track->textures[idx].compress_format;
331837cf6b03SRoland Scheidegger 
331937cf6b03SRoland Scheidegger 	for (face = 0; face < 5; face++) {
332037cf6b03SRoland Scheidegger 		cube_robj = track->textures[idx].cube_info[face].robj;
332137cf6b03SRoland Scheidegger 		w = track->textures[idx].cube_info[face].width;
332237cf6b03SRoland Scheidegger 		h = track->textures[idx].cube_info[face].height;
332337cf6b03SRoland Scheidegger 
332437cf6b03SRoland Scheidegger 		if (compress_format) {
332537cf6b03SRoland Scheidegger 			size = r100_track_compress_size(compress_format, w, h);
332637cf6b03SRoland Scheidegger 		} else
332737cf6b03SRoland Scheidegger 			size = w * h;
332837cf6b03SRoland Scheidegger 		size *= track->textures[idx].cpp;
332937cf6b03SRoland Scheidegger 
333037cf6b03SRoland Scheidegger 		size += track->textures[idx].cube_info[face].offset;
333137cf6b03SRoland Scheidegger 
333237cf6b03SRoland Scheidegger 		if (size > radeon_bo_size(cube_robj)) {
333337cf6b03SRoland Scheidegger 			DRM_ERROR("Cube texture offset greater than object size %lu %lu\n",
333437cf6b03SRoland Scheidegger 				  size, radeon_bo_size(cube_robj));
333537cf6b03SRoland Scheidegger 			r100_cs_track_texture_print(&track->textures[idx]);
333637cf6b03SRoland Scheidegger 			return -1;
333737cf6b03SRoland Scheidegger 		}
333837cf6b03SRoland Scheidegger 	}
333937cf6b03SRoland Scheidegger 	return 0;
334037cf6b03SRoland Scheidegger }
334137cf6b03SRoland Scheidegger 
3342551ebd83SDave Airlie static int r100_cs_track_texture_check(struct radeon_device *rdev,
3343551ebd83SDave Airlie 				       struct r100_cs_track *track)
3344551ebd83SDave Airlie {
33454c788679SJerome Glisse 	struct radeon_bo *robj;
3346551ebd83SDave Airlie 	unsigned long size;
3347b73c5f8bSMarek Olšák 	unsigned u, i, w, h, d;
3348551ebd83SDave Airlie 	int ret;
3349551ebd83SDave Airlie 
3350551ebd83SDave Airlie 	for (u = 0; u < track->num_texture; u++) {
3351551ebd83SDave Airlie 		if (!track->textures[u].enabled)
3352551ebd83SDave Airlie 			continue;
335343b93fbfSAlex Deucher 		if (track->textures[u].lookup_disable)
335443b93fbfSAlex Deucher 			continue;
3355551ebd83SDave Airlie 		robj = track->textures[u].robj;
3356551ebd83SDave Airlie 		if (robj == NULL) {
3357551ebd83SDave Airlie 			DRM_ERROR("No texture bound to unit %u\n", u);
3358551ebd83SDave Airlie 			return -EINVAL;
3359551ebd83SDave Airlie 		}
3360551ebd83SDave Airlie 		size = 0;
3361551ebd83SDave Airlie 		for (i = 0; i <= track->textures[u].num_levels; i++) {
3362551ebd83SDave Airlie 			if (track->textures[u].use_pitch) {
3363551ebd83SDave Airlie 				if (rdev->family < CHIP_R300)
3364551ebd83SDave Airlie 					w = (track->textures[u].pitch / track->textures[u].cpp) / (1 << i);
3365551ebd83SDave Airlie 				else
3366551ebd83SDave Airlie 					w = track->textures[u].pitch / (1 << i);
3367551ebd83SDave Airlie 			} else {
3368ceb776bcSMathias Fröhlich 				w = track->textures[u].width;
3369551ebd83SDave Airlie 				if (rdev->family >= CHIP_RV515)
3370551ebd83SDave Airlie 					w |= track->textures[u].width_11;
3371ceb776bcSMathias Fröhlich 				w = w / (1 << i);
3372551ebd83SDave Airlie 				if (track->textures[u].roundup_w)
3373551ebd83SDave Airlie 					w = roundup_pow_of_two(w);
3374551ebd83SDave Airlie 			}
3375ceb776bcSMathias Fröhlich 			h = track->textures[u].height;
3376551ebd83SDave Airlie 			if (rdev->family >= CHIP_RV515)
3377551ebd83SDave Airlie 				h |= track->textures[u].height_11;
3378ceb776bcSMathias Fröhlich 			h = h / (1 << i);
3379551ebd83SDave Airlie 			if (track->textures[u].roundup_h)
3380551ebd83SDave Airlie 				h = roundup_pow_of_two(h);
3381b73c5f8bSMarek Olšák 			if (track->textures[u].tex_coord_type == 1) {
3382b73c5f8bSMarek Olšák 				d = (1 << track->textures[u].txdepth) / (1 << i);
3383b73c5f8bSMarek Olšák 				if (!d)
3384b73c5f8bSMarek Olšák 					d = 1;
3385b73c5f8bSMarek Olšák 			} else {
3386b73c5f8bSMarek Olšák 				d = 1;
3387b73c5f8bSMarek Olšák 			}
3388d785d78bSDave Airlie 			if (track->textures[u].compress_format) {
3389d785d78bSDave Airlie 
3390b73c5f8bSMarek Olšák 				size += r100_track_compress_size(track->textures[u].compress_format, w, h) * d;
3391d785d78bSDave Airlie 				/* compressed textures are block based */
3392d785d78bSDave Airlie 			} else
3393b73c5f8bSMarek Olšák 				size += w * h * d;
3394551ebd83SDave Airlie 		}
3395551ebd83SDave Airlie 		size *= track->textures[u].cpp;
3396d785d78bSDave Airlie 
3397551ebd83SDave Airlie 		switch (track->textures[u].tex_coord_type) {
3398551ebd83SDave Airlie 		case 0:
3399551ebd83SDave Airlie 		case 1:
3400551ebd83SDave Airlie 			break;
3401551ebd83SDave Airlie 		case 2:
3402551ebd83SDave Airlie 			if (track->separate_cube) {
3403551ebd83SDave Airlie 				ret = r100_cs_track_cube(rdev, track, u);
3404551ebd83SDave Airlie 				if (ret)
3405551ebd83SDave Airlie 					return ret;
3406551ebd83SDave Airlie 			} else
3407551ebd83SDave Airlie 				size *= 6;
3408551ebd83SDave Airlie 			break;
3409551ebd83SDave Airlie 		default:
3410551ebd83SDave Airlie 			DRM_ERROR("Invalid texture coordinate type %u for unit "
3411551ebd83SDave Airlie 				  "%u\n", track->textures[u].tex_coord_type, u);
3412551ebd83SDave Airlie 			return -EINVAL;
3413551ebd83SDave Airlie 		}
34144c788679SJerome Glisse 		if (size > radeon_bo_size(robj)) {
3415551ebd83SDave Airlie 			DRM_ERROR("Texture of unit %u needs %lu bytes but is "
34164c788679SJerome Glisse 				  "%lu\n", u, size, radeon_bo_size(robj));
3417551ebd83SDave Airlie 			r100_cs_track_texture_print(&track->textures[u]);
3418551ebd83SDave Airlie 			return -EINVAL;
3419551ebd83SDave Airlie 		}
3420551ebd83SDave Airlie 	}
3421551ebd83SDave Airlie 	return 0;
3422551ebd83SDave Airlie }
3423551ebd83SDave Airlie 
3424551ebd83SDave Airlie int r100_cs_track_check(struct radeon_device *rdev, struct r100_cs_track *track)
3425551ebd83SDave Airlie {
3426551ebd83SDave Airlie 	unsigned i;
3427551ebd83SDave Airlie 	unsigned long size;
3428551ebd83SDave Airlie 	unsigned prim_walk;
3429551ebd83SDave Airlie 	unsigned nverts;
343040b4a759SMarek Olšák 	unsigned num_cb = track->cb_dirty ? track->num_cb : 0;
3431551ebd83SDave Airlie 
343240b4a759SMarek Olšák 	if (num_cb && !track->zb_cb_clear && !track->color_channel_mask &&
3433a41ceb1cSMarek Olšák 	    !track->blend_read_enable)
3434a41ceb1cSMarek Olšák 		num_cb = 0;
3435a41ceb1cSMarek Olšák 
3436a41ceb1cSMarek Olšák 	for (i = 0; i < num_cb; i++) {
3437551ebd83SDave Airlie 		if (track->cb[i].robj == NULL) {
3438551ebd83SDave Airlie 			DRM_ERROR("[drm] No buffer for color buffer %d !\n", i);
3439551ebd83SDave Airlie 			return -EINVAL;
3440551ebd83SDave Airlie 		}
3441551ebd83SDave Airlie 		size = track->cb[i].pitch * track->cb[i].cpp * track->maxy;
3442551ebd83SDave Airlie 		size += track->cb[i].offset;
34434c788679SJerome Glisse 		if (size > radeon_bo_size(track->cb[i].robj)) {
3444551ebd83SDave Airlie 			DRM_ERROR("[drm] Buffer too small for color buffer %d "
3445551ebd83SDave Airlie 				  "(need %lu have %lu) !\n", i, size,
34464c788679SJerome Glisse 				  radeon_bo_size(track->cb[i].robj));
3447551ebd83SDave Airlie 			DRM_ERROR("[drm] color buffer %d (%u %u %u %u)\n",
3448551ebd83SDave Airlie 				  i, track->cb[i].pitch, track->cb[i].cpp,
3449551ebd83SDave Airlie 				  track->cb[i].offset, track->maxy);
3450551ebd83SDave Airlie 			return -EINVAL;
3451551ebd83SDave Airlie 		}
3452551ebd83SDave Airlie 	}
345340b4a759SMarek Olšák 	track->cb_dirty = false;
345440b4a759SMarek Olšák 
345540b4a759SMarek Olšák 	if (track->zb_dirty && track->z_enabled) {
3456551ebd83SDave Airlie 		if (track->zb.robj == NULL) {
3457551ebd83SDave Airlie 			DRM_ERROR("[drm] No buffer for z buffer !\n");
3458551ebd83SDave Airlie 			return -EINVAL;
3459551ebd83SDave Airlie 		}
3460551ebd83SDave Airlie 		size = track->zb.pitch * track->zb.cpp * track->maxy;
3461551ebd83SDave Airlie 		size += track->zb.offset;
34624c788679SJerome Glisse 		if (size > radeon_bo_size(track->zb.robj)) {
3463551ebd83SDave Airlie 			DRM_ERROR("[drm] Buffer too small for z buffer "
3464551ebd83SDave Airlie 				  "(need %lu have %lu) !\n", size,
34654c788679SJerome Glisse 				  radeon_bo_size(track->zb.robj));
3466551ebd83SDave Airlie 			DRM_ERROR("[drm] zbuffer (%u %u %u %u)\n",
3467551ebd83SDave Airlie 				  track->zb.pitch, track->zb.cpp,
3468551ebd83SDave Airlie 				  track->zb.offset, track->maxy);
3469551ebd83SDave Airlie 			return -EINVAL;
3470551ebd83SDave Airlie 		}
3471551ebd83SDave Airlie 	}
347240b4a759SMarek Olšák 	track->zb_dirty = false;
347340b4a759SMarek Olšák 
3474fff1ce4dSMarek Olšák 	if (track->aa_dirty && track->aaresolve) {
3475fff1ce4dSMarek Olšák 		if (track->aa.robj == NULL) {
3476fff1ce4dSMarek Olšák 			DRM_ERROR("[drm] No buffer for AA resolve buffer %d !\n", i);
3477fff1ce4dSMarek Olšák 			return -EINVAL;
3478fff1ce4dSMarek Olšák 		}
3479fff1ce4dSMarek Olšák 		/* I believe the format comes from colorbuffer0. */
3480fff1ce4dSMarek Olšák 		size = track->aa.pitch * track->cb[0].cpp * track->maxy;
3481fff1ce4dSMarek Olšák 		size += track->aa.offset;
3482fff1ce4dSMarek Olšák 		if (size > radeon_bo_size(track->aa.robj)) {
3483fff1ce4dSMarek Olšák 			DRM_ERROR("[drm] Buffer too small for AA resolve buffer %d "
3484fff1ce4dSMarek Olšák 				  "(need %lu have %lu) !\n", i, size,
3485fff1ce4dSMarek Olšák 				  radeon_bo_size(track->aa.robj));
3486fff1ce4dSMarek Olšák 			DRM_ERROR("[drm] AA resolve buffer %d (%u %u %u %u)\n",
3487fff1ce4dSMarek Olšák 				  i, track->aa.pitch, track->cb[0].cpp,
3488fff1ce4dSMarek Olšák 				  track->aa.offset, track->maxy);
3489fff1ce4dSMarek Olšák 			return -EINVAL;
3490fff1ce4dSMarek Olšák 		}
3491fff1ce4dSMarek Olšák 	}
3492fff1ce4dSMarek Olšák 	track->aa_dirty = false;
3493fff1ce4dSMarek Olšák 
3494551ebd83SDave Airlie 	prim_walk = (track->vap_vf_cntl >> 4) & 0x3;
3495cae94b0aSMarek Olšák 	if (track->vap_vf_cntl & (1 << 14)) {
3496cae94b0aSMarek Olšák 		nverts = track->vap_alt_nverts;
3497cae94b0aSMarek Olšák 	} else {
3498551ebd83SDave Airlie 		nverts = (track->vap_vf_cntl >> 16) & 0xFFFF;
3499cae94b0aSMarek Olšák 	}
3500551ebd83SDave Airlie 	switch (prim_walk) {
3501551ebd83SDave Airlie 	case 1:
3502551ebd83SDave Airlie 		for (i = 0; i < track->num_arrays; i++) {
3503551ebd83SDave Airlie 			size = track->arrays[i].esize * track->max_indx * 4;
3504551ebd83SDave Airlie 			if (track->arrays[i].robj == NULL) {
3505551ebd83SDave Airlie 				DRM_ERROR("(PW %u) Vertex array %u no buffer "
3506551ebd83SDave Airlie 					  "bound\n", prim_walk, i);
3507551ebd83SDave Airlie 				return -EINVAL;
3508551ebd83SDave Airlie 			}
35094c788679SJerome Glisse 			if (size > radeon_bo_size(track->arrays[i].robj)) {
35104c788679SJerome Glisse 				dev_err(rdev->dev, "(PW %u) Vertex array %u "
35114c788679SJerome Glisse 					"need %lu dwords have %lu dwords\n",
35124c788679SJerome Glisse 					prim_walk, i, size >> 2,
35134c788679SJerome Glisse 					radeon_bo_size(track->arrays[i].robj)
35144c788679SJerome Glisse 					>> 2);
3515551ebd83SDave Airlie 				DRM_ERROR("Max indices %u\n", track->max_indx);
3516551ebd83SDave Airlie 				return -EINVAL;
3517551ebd83SDave Airlie 			}
3518551ebd83SDave Airlie 		}
3519551ebd83SDave Airlie 		break;
3520551ebd83SDave Airlie 	case 2:
3521551ebd83SDave Airlie 		for (i = 0; i < track->num_arrays; i++) {
3522551ebd83SDave Airlie 			size = track->arrays[i].esize * (nverts - 1) * 4;
3523551ebd83SDave Airlie 			if (track->arrays[i].robj == NULL) {
3524551ebd83SDave Airlie 				DRM_ERROR("(PW %u) Vertex array %u no buffer "
3525551ebd83SDave Airlie 					  "bound\n", prim_walk, i);
3526551ebd83SDave Airlie 				return -EINVAL;
3527551ebd83SDave Airlie 			}
35284c788679SJerome Glisse 			if (size > radeon_bo_size(track->arrays[i].robj)) {
35294c788679SJerome Glisse 				dev_err(rdev->dev, "(PW %u) Vertex array %u "
35304c788679SJerome Glisse 					"need %lu dwords have %lu dwords\n",
35314c788679SJerome Glisse 					prim_walk, i, size >> 2,
35324c788679SJerome Glisse 					radeon_bo_size(track->arrays[i].robj)
35334c788679SJerome Glisse 					>> 2);
3534551ebd83SDave Airlie 				return -EINVAL;
3535551ebd83SDave Airlie 			}
3536551ebd83SDave Airlie 		}
3537551ebd83SDave Airlie 		break;
3538551ebd83SDave Airlie 	case 3:
3539551ebd83SDave Airlie 		size = track->vtx_size * nverts;
3540551ebd83SDave Airlie 		if (size != track->immd_dwords) {
3541551ebd83SDave Airlie 			DRM_ERROR("IMMD draw %u dwors but needs %lu dwords\n",
3542551ebd83SDave Airlie 				  track->immd_dwords, size);
3543551ebd83SDave Airlie 			DRM_ERROR("VAP_VF_CNTL.NUM_VERTICES %u, VTX_SIZE %u\n",
3544551ebd83SDave Airlie 				  nverts, track->vtx_size);
3545551ebd83SDave Airlie 			return -EINVAL;
3546551ebd83SDave Airlie 		}
3547551ebd83SDave Airlie 		break;
3548551ebd83SDave Airlie 	default:
3549551ebd83SDave Airlie 		DRM_ERROR("[drm] Invalid primitive walk %d for VAP_VF_CNTL\n",
3550551ebd83SDave Airlie 			  prim_walk);
3551551ebd83SDave Airlie 		return -EINVAL;
3552551ebd83SDave Airlie 	}
355340b4a759SMarek Olšák 
355440b4a759SMarek Olšák 	if (track->tex_dirty) {
355540b4a759SMarek Olšák 		track->tex_dirty = false;
3556551ebd83SDave Airlie 		return r100_cs_track_texture_check(rdev, track);
3557551ebd83SDave Airlie 	}
355840b4a759SMarek Olšák 	return 0;
355940b4a759SMarek Olšák }
3560551ebd83SDave Airlie 
3561551ebd83SDave Airlie void r100_cs_track_clear(struct radeon_device *rdev, struct r100_cs_track *track)
3562551ebd83SDave Airlie {
3563551ebd83SDave Airlie 	unsigned i, face;
3564551ebd83SDave Airlie 
356540b4a759SMarek Olšák 	track->cb_dirty = true;
356640b4a759SMarek Olšák 	track->zb_dirty = true;
356740b4a759SMarek Olšák 	track->tex_dirty = true;
3568fff1ce4dSMarek Olšák 	track->aa_dirty = true;
356940b4a759SMarek Olšák 
3570551ebd83SDave Airlie 	if (rdev->family < CHIP_R300) {
3571551ebd83SDave Airlie 		track->num_cb = 1;
3572551ebd83SDave Airlie 		if (rdev->family <= CHIP_RS200)
3573551ebd83SDave Airlie 			track->num_texture = 3;
3574551ebd83SDave Airlie 		else
3575551ebd83SDave Airlie 			track->num_texture = 6;
3576551ebd83SDave Airlie 		track->maxy = 2048;
3577551ebd83SDave Airlie 		track->separate_cube = 1;
3578551ebd83SDave Airlie 	} else {
3579551ebd83SDave Airlie 		track->num_cb = 4;
3580551ebd83SDave Airlie 		track->num_texture = 16;
3581551ebd83SDave Airlie 		track->maxy = 4096;
3582551ebd83SDave Airlie 		track->separate_cube = 0;
358345e4039cSDave Airlie 		track->aaresolve = false;
3584fff1ce4dSMarek Olšák 		track->aa.robj = NULL;
3585551ebd83SDave Airlie 	}
3586551ebd83SDave Airlie 
3587551ebd83SDave Airlie 	for (i = 0; i < track->num_cb; i++) {
3588551ebd83SDave Airlie 		track->cb[i].robj = NULL;
3589551ebd83SDave Airlie 		track->cb[i].pitch = 8192;
3590551ebd83SDave Airlie 		track->cb[i].cpp = 16;
3591551ebd83SDave Airlie 		track->cb[i].offset = 0;
3592551ebd83SDave Airlie 	}
3593551ebd83SDave Airlie 	track->z_enabled = true;
3594551ebd83SDave Airlie 	track->zb.robj = NULL;
3595551ebd83SDave Airlie 	track->zb.pitch = 8192;
3596551ebd83SDave Airlie 	track->zb.cpp = 4;
3597551ebd83SDave Airlie 	track->zb.offset = 0;
3598551ebd83SDave Airlie 	track->vtx_size = 0x7F;
3599551ebd83SDave Airlie 	track->immd_dwords = 0xFFFFFFFFUL;
3600551ebd83SDave Airlie 	track->num_arrays = 11;
3601551ebd83SDave Airlie 	track->max_indx = 0x00FFFFFFUL;
3602551ebd83SDave Airlie 	for (i = 0; i < track->num_arrays; i++) {
3603551ebd83SDave Airlie 		track->arrays[i].robj = NULL;
3604551ebd83SDave Airlie 		track->arrays[i].esize = 0x7F;
3605551ebd83SDave Airlie 	}
3606551ebd83SDave Airlie 	for (i = 0; i < track->num_texture; i++) {
3607d785d78bSDave Airlie 		track->textures[i].compress_format = R100_TRACK_COMP_NONE;
3608551ebd83SDave Airlie 		track->textures[i].pitch = 16536;
3609551ebd83SDave Airlie 		track->textures[i].width = 16536;
3610551ebd83SDave Airlie 		track->textures[i].height = 16536;
3611551ebd83SDave Airlie 		track->textures[i].width_11 = 1 << 11;
3612551ebd83SDave Airlie 		track->textures[i].height_11 = 1 << 11;
3613551ebd83SDave Airlie 		track->textures[i].num_levels = 12;
3614551ebd83SDave Airlie 		if (rdev->family <= CHIP_RS200) {
3615551ebd83SDave Airlie 			track->textures[i].tex_coord_type = 0;
3616551ebd83SDave Airlie 			track->textures[i].txdepth = 0;
3617551ebd83SDave Airlie 		} else {
3618551ebd83SDave Airlie 			track->textures[i].txdepth = 16;
3619551ebd83SDave Airlie 			track->textures[i].tex_coord_type = 1;
3620551ebd83SDave Airlie 		}
3621551ebd83SDave Airlie 		track->textures[i].cpp = 64;
3622551ebd83SDave Airlie 		track->textures[i].robj = NULL;
3623551ebd83SDave Airlie 		/* CS IB emission code makes sure texture unit are disabled */
3624551ebd83SDave Airlie 		track->textures[i].enabled = false;
362543b93fbfSAlex Deucher 		track->textures[i].lookup_disable = false;
3626551ebd83SDave Airlie 		track->textures[i].roundup_w = true;
3627551ebd83SDave Airlie 		track->textures[i].roundup_h = true;
3628551ebd83SDave Airlie 		if (track->separate_cube)
3629551ebd83SDave Airlie 			for (face = 0; face < 5; face++) {
3630551ebd83SDave Airlie 				track->textures[i].cube_info[face].robj = NULL;
3631551ebd83SDave Airlie 				track->textures[i].cube_info[face].width = 16536;
3632551ebd83SDave Airlie 				track->textures[i].cube_info[face].height = 16536;
3633551ebd83SDave Airlie 				track->textures[i].cube_info[face].offset = 0;
3634551ebd83SDave Airlie 			}
3635551ebd83SDave Airlie 	}
3636551ebd83SDave Airlie }
36373ce0a23dSJerome Glisse 
36383ce0a23dSJerome Glisse int r100_ring_test(struct radeon_device *rdev)
36393ce0a23dSJerome Glisse {
36403ce0a23dSJerome Glisse 	uint32_t scratch;
36413ce0a23dSJerome Glisse 	uint32_t tmp = 0;
36423ce0a23dSJerome Glisse 	unsigned i;
36433ce0a23dSJerome Glisse 	int r;
36443ce0a23dSJerome Glisse 
36453ce0a23dSJerome Glisse 	r = radeon_scratch_get(rdev, &scratch);
36463ce0a23dSJerome Glisse 	if (r) {
36473ce0a23dSJerome Glisse 		DRM_ERROR("radeon: cp failed to get scratch reg (%d).\n", r);
36483ce0a23dSJerome Glisse 		return r;
36493ce0a23dSJerome Glisse 	}
36503ce0a23dSJerome Glisse 	WREG32(scratch, 0xCAFEDEAD);
36513ce0a23dSJerome Glisse 	r = radeon_ring_lock(rdev, 2);
36523ce0a23dSJerome Glisse 	if (r) {
36533ce0a23dSJerome Glisse 		DRM_ERROR("radeon: cp failed to lock ring (%d).\n", r);
36543ce0a23dSJerome Glisse 		radeon_scratch_free(rdev, scratch);
36553ce0a23dSJerome Glisse 		return r;
36563ce0a23dSJerome Glisse 	}
36573ce0a23dSJerome Glisse 	radeon_ring_write(rdev, PACKET0(scratch, 0));
36583ce0a23dSJerome Glisse 	radeon_ring_write(rdev, 0xDEADBEEF);
36593ce0a23dSJerome Glisse 	radeon_ring_unlock_commit(rdev);
36603ce0a23dSJerome Glisse 	for (i = 0; i < rdev->usec_timeout; i++) {
36613ce0a23dSJerome Glisse 		tmp = RREG32(scratch);
36623ce0a23dSJerome Glisse 		if (tmp == 0xDEADBEEF) {
36633ce0a23dSJerome Glisse 			break;
36643ce0a23dSJerome Glisse 		}
36653ce0a23dSJerome Glisse 		DRM_UDELAY(1);
36663ce0a23dSJerome Glisse 	}
36673ce0a23dSJerome Glisse 	if (i < rdev->usec_timeout) {
36683ce0a23dSJerome Glisse 		DRM_INFO("ring test succeeded in %d usecs\n", i);
36693ce0a23dSJerome Glisse 	} else {
3670369d7ec1SAlex Deucher 		DRM_ERROR("radeon: ring test failed (scratch(0x%04X)=0x%08X)\n",
36713ce0a23dSJerome Glisse 			  scratch, tmp);
36723ce0a23dSJerome Glisse 		r = -EINVAL;
36733ce0a23dSJerome Glisse 	}
36743ce0a23dSJerome Glisse 	radeon_scratch_free(rdev, scratch);
36753ce0a23dSJerome Glisse 	return r;
36763ce0a23dSJerome Glisse }
36773ce0a23dSJerome Glisse 
36783ce0a23dSJerome Glisse void r100_ring_ib_execute(struct radeon_device *rdev, struct radeon_ib *ib)
36793ce0a23dSJerome Glisse {
36803ce0a23dSJerome Glisse 	radeon_ring_write(rdev, PACKET0(RADEON_CP_IB_BASE, 1));
36813ce0a23dSJerome Glisse 	radeon_ring_write(rdev, ib->gpu_addr);
36823ce0a23dSJerome Glisse 	radeon_ring_write(rdev, ib->length_dw);
36833ce0a23dSJerome Glisse }
36843ce0a23dSJerome Glisse 
36853ce0a23dSJerome Glisse int r100_ib_test(struct radeon_device *rdev)
36863ce0a23dSJerome Glisse {
36873ce0a23dSJerome Glisse 	struct radeon_ib *ib;
36883ce0a23dSJerome Glisse 	uint32_t scratch;
36893ce0a23dSJerome Glisse 	uint32_t tmp = 0;
36903ce0a23dSJerome Glisse 	unsigned i;
36913ce0a23dSJerome Glisse 	int r;
36923ce0a23dSJerome Glisse 
36933ce0a23dSJerome Glisse 	r = radeon_scratch_get(rdev, &scratch);
36943ce0a23dSJerome Glisse 	if (r) {
36953ce0a23dSJerome Glisse 		DRM_ERROR("radeon: failed to get scratch reg (%d).\n", r);
36963ce0a23dSJerome Glisse 		return r;
36973ce0a23dSJerome Glisse 	}
36983ce0a23dSJerome Glisse 	WREG32(scratch, 0xCAFEDEAD);
36993ce0a23dSJerome Glisse 	r = radeon_ib_get(rdev, &ib);
37003ce0a23dSJerome Glisse 	if (r) {
37013ce0a23dSJerome Glisse 		return r;
37023ce0a23dSJerome Glisse 	}
37033ce0a23dSJerome Glisse 	ib->ptr[0] = PACKET0(scratch, 0);
37043ce0a23dSJerome Glisse 	ib->ptr[1] = 0xDEADBEEF;
37053ce0a23dSJerome Glisse 	ib->ptr[2] = PACKET2(0);
37063ce0a23dSJerome Glisse 	ib->ptr[3] = PACKET2(0);
37073ce0a23dSJerome Glisse 	ib->ptr[4] = PACKET2(0);
37083ce0a23dSJerome Glisse 	ib->ptr[5] = PACKET2(0);
37093ce0a23dSJerome Glisse 	ib->ptr[6] = PACKET2(0);
37103ce0a23dSJerome Glisse 	ib->ptr[7] = PACKET2(0);
37113ce0a23dSJerome Glisse 	ib->length_dw = 8;
37123ce0a23dSJerome Glisse 	r = radeon_ib_schedule(rdev, ib);
37133ce0a23dSJerome Glisse 	if (r) {
37143ce0a23dSJerome Glisse 		radeon_scratch_free(rdev, scratch);
37153ce0a23dSJerome Glisse 		radeon_ib_free(rdev, &ib);
37163ce0a23dSJerome Glisse 		return r;
37173ce0a23dSJerome Glisse 	}
37183ce0a23dSJerome Glisse 	r = radeon_fence_wait(ib->fence, false);
37193ce0a23dSJerome Glisse 	if (r) {
37203ce0a23dSJerome Glisse 		return r;
37213ce0a23dSJerome Glisse 	}
37223ce0a23dSJerome Glisse 	for (i = 0; i < rdev->usec_timeout; i++) {
37233ce0a23dSJerome Glisse 		tmp = RREG32(scratch);
37243ce0a23dSJerome Glisse 		if (tmp == 0xDEADBEEF) {
37253ce0a23dSJerome Glisse 			break;
37263ce0a23dSJerome Glisse 		}
37273ce0a23dSJerome Glisse 		DRM_UDELAY(1);
37283ce0a23dSJerome Glisse 	}
37293ce0a23dSJerome Glisse 	if (i < rdev->usec_timeout) {
37303ce0a23dSJerome Glisse 		DRM_INFO("ib test succeeded in %u usecs\n", i);
37313ce0a23dSJerome Glisse 	} else {
373262f288cfSPaul Bolle 		DRM_ERROR("radeon: ib test failed (scratch(0x%04X)=0x%08X)\n",
37333ce0a23dSJerome Glisse 			  scratch, tmp);
37343ce0a23dSJerome Glisse 		r = -EINVAL;
37353ce0a23dSJerome Glisse 	}
37363ce0a23dSJerome Glisse 	radeon_scratch_free(rdev, scratch);
37373ce0a23dSJerome Glisse 	radeon_ib_free(rdev, &ib);
37383ce0a23dSJerome Glisse 	return r;
37393ce0a23dSJerome Glisse }
37409f022ddfSJerome Glisse 
37419f022ddfSJerome Glisse void r100_ib_fini(struct radeon_device *rdev)
37429f022ddfSJerome Glisse {
37439f022ddfSJerome Glisse 	radeon_ib_pool_fini(rdev);
37449f022ddfSJerome Glisse }
37459f022ddfSJerome Glisse 
37469f022ddfSJerome Glisse int r100_ib_init(struct radeon_device *rdev)
37479f022ddfSJerome Glisse {
37489f022ddfSJerome Glisse 	int r;
37499f022ddfSJerome Glisse 
37509f022ddfSJerome Glisse 	r = radeon_ib_pool_init(rdev);
37519f022ddfSJerome Glisse 	if (r) {
3752ec4f2ac4SPaul Bolle 		dev_err(rdev->dev, "failed initializing IB pool (%d).\n", r);
37539f022ddfSJerome Glisse 		r100_ib_fini(rdev);
37549f022ddfSJerome Glisse 		return r;
37559f022ddfSJerome Glisse 	}
37569f022ddfSJerome Glisse 	r = r100_ib_test(rdev);
37579f022ddfSJerome Glisse 	if (r) {
3758ec4f2ac4SPaul Bolle 		dev_err(rdev->dev, "failed testing IB (%d).\n", r);
37599f022ddfSJerome Glisse 		r100_ib_fini(rdev);
37609f022ddfSJerome Glisse 		return r;
37619f022ddfSJerome Glisse 	}
37629f022ddfSJerome Glisse 	return 0;
37639f022ddfSJerome Glisse }
37649f022ddfSJerome Glisse 
37659f022ddfSJerome Glisse void r100_mc_stop(struct radeon_device *rdev, struct r100_mc_save *save)
37669f022ddfSJerome Glisse {
37679f022ddfSJerome Glisse 	/* Shutdown CP we shouldn't need to do that but better be safe than
37689f022ddfSJerome Glisse 	 * sorry
37699f022ddfSJerome Glisse 	 */
37709f022ddfSJerome Glisse 	rdev->cp.ready = false;
37719f022ddfSJerome Glisse 	WREG32(R_000740_CP_CSQ_CNTL, 0);
37729f022ddfSJerome Glisse 
37739f022ddfSJerome Glisse 	/* Save few CRTC registers */
3774ca6ffc64SJerome Glisse 	save->GENMO_WT = RREG8(R_0003C2_GENMO_WT);
37759f022ddfSJerome Glisse 	save->CRTC_EXT_CNTL = RREG32(R_000054_CRTC_EXT_CNTL);
37769f022ddfSJerome Glisse 	save->CRTC_GEN_CNTL = RREG32(R_000050_CRTC_GEN_CNTL);
37779f022ddfSJerome Glisse 	save->CUR_OFFSET = RREG32(R_000260_CUR_OFFSET);
37789f022ddfSJerome Glisse 	if (!(rdev->flags & RADEON_SINGLE_CRTC)) {
37799f022ddfSJerome Glisse 		save->CRTC2_GEN_CNTL = RREG32(R_0003F8_CRTC2_GEN_CNTL);
37809f022ddfSJerome Glisse 		save->CUR2_OFFSET = RREG32(R_000360_CUR2_OFFSET);
37819f022ddfSJerome Glisse 	}
37829f022ddfSJerome Glisse 
37839f022ddfSJerome Glisse 	/* Disable VGA aperture access */
3784ca6ffc64SJerome Glisse 	WREG8(R_0003C2_GENMO_WT, C_0003C2_VGA_RAM_EN & save->GENMO_WT);
37859f022ddfSJerome Glisse 	/* Disable cursor, overlay, crtc */
37869f022ddfSJerome Glisse 	WREG32(R_000260_CUR_OFFSET, save->CUR_OFFSET | S_000260_CUR_LOCK(1));
37879f022ddfSJerome Glisse 	WREG32(R_000054_CRTC_EXT_CNTL, save->CRTC_EXT_CNTL |
37889f022ddfSJerome Glisse 					S_000054_CRTC_DISPLAY_DIS(1));
37899f022ddfSJerome Glisse 	WREG32(R_000050_CRTC_GEN_CNTL,
37909f022ddfSJerome Glisse 			(C_000050_CRTC_CUR_EN & save->CRTC_GEN_CNTL) |
37919f022ddfSJerome Glisse 			S_000050_CRTC_DISP_REQ_EN_B(1));
37929f022ddfSJerome Glisse 	WREG32(R_000420_OV0_SCALE_CNTL,
37939f022ddfSJerome Glisse 		C_000420_OV0_OVERLAY_EN & RREG32(R_000420_OV0_SCALE_CNTL));
37949f022ddfSJerome Glisse 	WREG32(R_000260_CUR_OFFSET, C_000260_CUR_LOCK & save->CUR_OFFSET);
37959f022ddfSJerome Glisse 	if (!(rdev->flags & RADEON_SINGLE_CRTC)) {
37969f022ddfSJerome Glisse 		WREG32(R_000360_CUR2_OFFSET, save->CUR2_OFFSET |
37979f022ddfSJerome Glisse 						S_000360_CUR2_LOCK(1));
37989f022ddfSJerome Glisse 		WREG32(R_0003F8_CRTC2_GEN_CNTL,
37999f022ddfSJerome Glisse 			(C_0003F8_CRTC2_CUR_EN & save->CRTC2_GEN_CNTL) |
38009f022ddfSJerome Glisse 			S_0003F8_CRTC2_DISPLAY_DIS(1) |
38019f022ddfSJerome Glisse 			S_0003F8_CRTC2_DISP_REQ_EN_B(1));
38029f022ddfSJerome Glisse 		WREG32(R_000360_CUR2_OFFSET,
38039f022ddfSJerome Glisse 			C_000360_CUR2_LOCK & save->CUR2_OFFSET);
38049f022ddfSJerome Glisse 	}
38059f022ddfSJerome Glisse }
38069f022ddfSJerome Glisse 
38079f022ddfSJerome Glisse void r100_mc_resume(struct radeon_device *rdev, struct r100_mc_save *save)
38089f022ddfSJerome Glisse {
38099f022ddfSJerome Glisse 	/* Update base address for crtc */
3810d594e46aSJerome Glisse 	WREG32(R_00023C_DISPLAY_BASE_ADDR, rdev->mc.vram_start);
38119f022ddfSJerome Glisse 	if (!(rdev->flags & RADEON_SINGLE_CRTC)) {
3812d594e46aSJerome Glisse 		WREG32(R_00033C_CRTC2_DISPLAY_BASE_ADDR, rdev->mc.vram_start);
38139f022ddfSJerome Glisse 	}
38149f022ddfSJerome Glisse 	/* Restore CRTC registers */
3815ca6ffc64SJerome Glisse 	WREG8(R_0003C2_GENMO_WT, save->GENMO_WT);
38169f022ddfSJerome Glisse 	WREG32(R_000054_CRTC_EXT_CNTL, save->CRTC_EXT_CNTL);
38179f022ddfSJerome Glisse 	WREG32(R_000050_CRTC_GEN_CNTL, save->CRTC_GEN_CNTL);
38189f022ddfSJerome Glisse 	if (!(rdev->flags & RADEON_SINGLE_CRTC)) {
38199f022ddfSJerome Glisse 		WREG32(R_0003F8_CRTC2_GEN_CNTL, save->CRTC2_GEN_CNTL);
38209f022ddfSJerome Glisse 	}
38219f022ddfSJerome Glisse }
3822ca6ffc64SJerome Glisse 
3823ca6ffc64SJerome Glisse void r100_vga_render_disable(struct radeon_device *rdev)
3824ca6ffc64SJerome Glisse {
3825ca6ffc64SJerome Glisse 	u32 tmp;
3826ca6ffc64SJerome Glisse 
3827ca6ffc64SJerome Glisse 	tmp = RREG8(R_0003C2_GENMO_WT);
3828ca6ffc64SJerome Glisse 	WREG8(R_0003C2_GENMO_WT, C_0003C2_VGA_RAM_EN & tmp);
3829ca6ffc64SJerome Glisse }
3830d4550907SJerome Glisse 
3831d4550907SJerome Glisse static void r100_debugfs(struct radeon_device *rdev)
3832d4550907SJerome Glisse {
3833d4550907SJerome Glisse 	int r;
3834d4550907SJerome Glisse 
3835d4550907SJerome Glisse 	r = r100_debugfs_mc_info_init(rdev);
3836d4550907SJerome Glisse 	if (r)
3837d4550907SJerome Glisse 		dev_warn(rdev->dev, "Failed to create r100_mc debugfs file.\n");
3838d4550907SJerome Glisse }
3839d4550907SJerome Glisse 
3840d4550907SJerome Glisse static void r100_mc_program(struct radeon_device *rdev)
3841d4550907SJerome Glisse {
3842d4550907SJerome Glisse 	struct r100_mc_save save;
3843d4550907SJerome Glisse 
3844d4550907SJerome Glisse 	/* Stops all mc clients */
3845d4550907SJerome Glisse 	r100_mc_stop(rdev, &save);
3846d4550907SJerome Glisse 	if (rdev->flags & RADEON_IS_AGP) {
3847d4550907SJerome Glisse 		WREG32(R_00014C_MC_AGP_LOCATION,
3848d4550907SJerome Glisse 			S_00014C_MC_AGP_START(rdev->mc.gtt_start >> 16) |
3849d4550907SJerome Glisse 			S_00014C_MC_AGP_TOP(rdev->mc.gtt_end >> 16));
3850d4550907SJerome Glisse 		WREG32(R_000170_AGP_BASE, lower_32_bits(rdev->mc.agp_base));
3851d4550907SJerome Glisse 		if (rdev->family > CHIP_RV200)
3852d4550907SJerome Glisse 			WREG32(R_00015C_AGP_BASE_2,
3853d4550907SJerome Glisse 				upper_32_bits(rdev->mc.agp_base) & 0xff);
3854d4550907SJerome Glisse 	} else {
3855d4550907SJerome Glisse 		WREG32(R_00014C_MC_AGP_LOCATION, 0x0FFFFFFF);
3856d4550907SJerome Glisse 		WREG32(R_000170_AGP_BASE, 0);
3857d4550907SJerome Glisse 		if (rdev->family > CHIP_RV200)
3858d4550907SJerome Glisse 			WREG32(R_00015C_AGP_BASE_2, 0);
3859d4550907SJerome Glisse 	}
3860d4550907SJerome Glisse 	/* Wait for mc idle */
3861d4550907SJerome Glisse 	if (r100_mc_wait_for_idle(rdev))
3862d4550907SJerome Glisse 		dev_warn(rdev->dev, "Wait for MC idle timeout.\n");
3863d4550907SJerome Glisse 	/* Program MC, should be a 32bits limited address space */
3864d4550907SJerome Glisse 	WREG32(R_000148_MC_FB_LOCATION,
3865d4550907SJerome Glisse 		S_000148_MC_FB_START(rdev->mc.vram_start >> 16) |
3866d4550907SJerome Glisse 		S_000148_MC_FB_TOP(rdev->mc.vram_end >> 16));
3867d4550907SJerome Glisse 	r100_mc_resume(rdev, &save);
3868d4550907SJerome Glisse }
3869d4550907SJerome Glisse 
3870d4550907SJerome Glisse void r100_clock_startup(struct radeon_device *rdev)
3871d4550907SJerome Glisse {
3872d4550907SJerome Glisse 	u32 tmp;
3873d4550907SJerome Glisse 
3874d4550907SJerome Glisse 	if (radeon_dynclks != -1 && radeon_dynclks)
3875d4550907SJerome Glisse 		radeon_legacy_set_clock_gating(rdev, 1);
3876d4550907SJerome Glisse 	/* We need to force on some of the block */
3877d4550907SJerome Glisse 	tmp = RREG32_PLL(R_00000D_SCLK_CNTL);
3878d4550907SJerome Glisse 	tmp |= S_00000D_FORCE_CP(1) | S_00000D_FORCE_VIP(1);
3879d4550907SJerome Glisse 	if ((rdev->family == CHIP_RV250) || (rdev->family == CHIP_RV280))
3880d4550907SJerome Glisse 		tmp |= S_00000D_FORCE_DISP1(1) | S_00000D_FORCE_DISP2(1);
3881d4550907SJerome Glisse 	WREG32_PLL(R_00000D_SCLK_CNTL, tmp);
3882d4550907SJerome Glisse }
3883d4550907SJerome Glisse 
3884d4550907SJerome Glisse static int r100_startup(struct radeon_device *rdev)
3885d4550907SJerome Glisse {
3886d4550907SJerome Glisse 	int r;
3887d4550907SJerome Glisse 
388892cde00cSAlex Deucher 	/* set common regs */
388992cde00cSAlex Deucher 	r100_set_common_regs(rdev);
389092cde00cSAlex Deucher 	/* program mc */
3891d4550907SJerome Glisse 	r100_mc_program(rdev);
3892d4550907SJerome Glisse 	/* Resume clock */
3893d4550907SJerome Glisse 	r100_clock_startup(rdev);
3894d4550907SJerome Glisse 	/* Initialize GART (initialize after TTM so we can allocate
3895d4550907SJerome Glisse 	 * memory through TTM but finalize after TTM) */
389617e15b0cSDave Airlie 	r100_enable_bm(rdev);
3897d4550907SJerome Glisse 	if (rdev->flags & RADEON_IS_PCI) {
3898d4550907SJerome Glisse 		r = r100_pci_gart_enable(rdev);
3899d4550907SJerome Glisse 		if (r)
3900d4550907SJerome Glisse 			return r;
3901d4550907SJerome Glisse 	}
3902724c80e1SAlex Deucher 
3903724c80e1SAlex Deucher 	/* allocate wb buffer */
3904724c80e1SAlex Deucher 	r = radeon_wb_init(rdev);
3905724c80e1SAlex Deucher 	if (r)
3906724c80e1SAlex Deucher 		return r;
3907724c80e1SAlex Deucher 
3908d4550907SJerome Glisse 	/* Enable IRQ */
3909d4550907SJerome Glisse 	r100_irq_set(rdev);
3910cafe6609SJerome Glisse 	rdev->config.r100.hdp_cntl = RREG32(RADEON_HOST_PATH_CNTL);
3911d4550907SJerome Glisse 	/* 1M ring buffer */
3912d4550907SJerome Glisse 	r = r100_cp_init(rdev, 1024 * 1024);
3913d4550907SJerome Glisse 	if (r) {
3914ec4f2ac4SPaul Bolle 		dev_err(rdev->dev, "failed initializing CP (%d).\n", r);
3915d4550907SJerome Glisse 		return r;
3916d4550907SJerome Glisse 	}
3917d4550907SJerome Glisse 	r = r100_ib_init(rdev);
3918d4550907SJerome Glisse 	if (r) {
3919ec4f2ac4SPaul Bolle 		dev_err(rdev->dev, "failed initializing IB (%d).\n", r);
3920d4550907SJerome Glisse 		return r;
3921d4550907SJerome Glisse 	}
3922d4550907SJerome Glisse 	return 0;
3923d4550907SJerome Glisse }
3924d4550907SJerome Glisse 
3925d4550907SJerome Glisse int r100_resume(struct radeon_device *rdev)
3926d4550907SJerome Glisse {
3927d4550907SJerome Glisse 	/* Make sur GART are not working */
3928d4550907SJerome Glisse 	if (rdev->flags & RADEON_IS_PCI)
3929d4550907SJerome Glisse 		r100_pci_gart_disable(rdev);
3930d4550907SJerome Glisse 	/* Resume clock before doing reset */
3931d4550907SJerome Glisse 	r100_clock_startup(rdev);
3932d4550907SJerome Glisse 	/* Reset gpu before posting otherwise ATOM will enter infinite loop */
3933a2d07b74SJerome Glisse 	if (radeon_asic_reset(rdev)) {
3934d4550907SJerome Glisse 		dev_warn(rdev->dev, "GPU reset failed ! (0xE40=0x%08X, 0x7C0=0x%08X)\n",
3935d4550907SJerome Glisse 			RREG32(R_000E40_RBBM_STATUS),
3936d4550907SJerome Glisse 			RREG32(R_0007C0_CP_STAT));
3937d4550907SJerome Glisse 	}
3938d4550907SJerome Glisse 	/* post */
3939d4550907SJerome Glisse 	radeon_combios_asic_init(rdev->ddev);
3940d4550907SJerome Glisse 	/* Resume clock after posting */
3941d4550907SJerome Glisse 	r100_clock_startup(rdev);
3942550e2d92SDave Airlie 	/* Initialize surface registers */
3943550e2d92SDave Airlie 	radeon_surface_init(rdev);
3944d4550907SJerome Glisse 	return r100_startup(rdev);
3945d4550907SJerome Glisse }
3946d4550907SJerome Glisse 
3947d4550907SJerome Glisse int r100_suspend(struct radeon_device *rdev)
3948d4550907SJerome Glisse {
3949d4550907SJerome Glisse 	r100_cp_disable(rdev);
3950724c80e1SAlex Deucher 	radeon_wb_disable(rdev);
3951d4550907SJerome Glisse 	r100_irq_disable(rdev);
3952d4550907SJerome Glisse 	if (rdev->flags & RADEON_IS_PCI)
3953d4550907SJerome Glisse 		r100_pci_gart_disable(rdev);
3954d4550907SJerome Glisse 	return 0;
3955d4550907SJerome Glisse }
3956d4550907SJerome Glisse 
3957d4550907SJerome Glisse void r100_fini(struct radeon_device *rdev)
3958d4550907SJerome Glisse {
3959d4550907SJerome Glisse 	r100_cp_fini(rdev);
3960724c80e1SAlex Deucher 	radeon_wb_fini(rdev);
3961d4550907SJerome Glisse 	r100_ib_fini(rdev);
3962d4550907SJerome Glisse 	radeon_gem_fini(rdev);
3963d4550907SJerome Glisse 	if (rdev->flags & RADEON_IS_PCI)
3964d4550907SJerome Glisse 		r100_pci_gart_fini(rdev);
3965d0269ed8SJerome Glisse 	radeon_agp_fini(rdev);
3966d4550907SJerome Glisse 	radeon_irq_kms_fini(rdev);
3967d4550907SJerome Glisse 	radeon_fence_driver_fini(rdev);
39684c788679SJerome Glisse 	radeon_bo_fini(rdev);
3969d4550907SJerome Glisse 	radeon_atombios_fini(rdev);
3970d4550907SJerome Glisse 	kfree(rdev->bios);
3971d4550907SJerome Glisse 	rdev->bios = NULL;
3972d4550907SJerome Glisse }
3973d4550907SJerome Glisse 
39744c712e6cSDave Airlie /*
39754c712e6cSDave Airlie  * Due to how kexec works, it can leave the hw fully initialised when it
39764c712e6cSDave Airlie  * boots the new kernel. However doing our init sequence with the CP and
39774c712e6cSDave Airlie  * WB stuff setup causes GPU hangs on the RN50 at least. So at startup
39784c712e6cSDave Airlie  * do some quick sanity checks and restore sane values to avoid this
39794c712e6cSDave Airlie  * problem.
39804c712e6cSDave Airlie  */
39814c712e6cSDave Airlie void r100_restore_sanity(struct radeon_device *rdev)
39824c712e6cSDave Airlie {
39834c712e6cSDave Airlie 	u32 tmp;
39844c712e6cSDave Airlie 
39854c712e6cSDave Airlie 	tmp = RREG32(RADEON_CP_CSQ_CNTL);
39864c712e6cSDave Airlie 	if (tmp) {
39874c712e6cSDave Airlie 		WREG32(RADEON_CP_CSQ_CNTL, 0);
39884c712e6cSDave Airlie 	}
39894c712e6cSDave Airlie 	tmp = RREG32(RADEON_CP_RB_CNTL);
39904c712e6cSDave Airlie 	if (tmp) {
39914c712e6cSDave Airlie 		WREG32(RADEON_CP_RB_CNTL, 0);
39924c712e6cSDave Airlie 	}
39934c712e6cSDave Airlie 	tmp = RREG32(RADEON_SCRATCH_UMSK);
39944c712e6cSDave Airlie 	if (tmp) {
39954c712e6cSDave Airlie 		WREG32(RADEON_SCRATCH_UMSK, 0);
39964c712e6cSDave Airlie 	}
39974c712e6cSDave Airlie }
39984c712e6cSDave Airlie 
3999d4550907SJerome Glisse int r100_init(struct radeon_device *rdev)
4000d4550907SJerome Glisse {
4001d4550907SJerome Glisse 	int r;
4002d4550907SJerome Glisse 
4003d4550907SJerome Glisse 	/* Register debugfs file specific to this group of asics */
4004d4550907SJerome Glisse 	r100_debugfs(rdev);
4005d4550907SJerome Glisse 	/* Disable VGA */
4006d4550907SJerome Glisse 	r100_vga_render_disable(rdev);
4007d4550907SJerome Glisse 	/* Initialize scratch registers */
4008d4550907SJerome Glisse 	radeon_scratch_init(rdev);
4009d4550907SJerome Glisse 	/* Initialize surface registers */
4010d4550907SJerome Glisse 	radeon_surface_init(rdev);
40114c712e6cSDave Airlie 	/* sanity check some register to avoid hangs like after kexec */
40124c712e6cSDave Airlie 	r100_restore_sanity(rdev);
4013d4550907SJerome Glisse 	/* TODO: disable VGA need to use VGA request */
4014d4550907SJerome Glisse 	/* BIOS*/
4015d4550907SJerome Glisse 	if (!radeon_get_bios(rdev)) {
4016d4550907SJerome Glisse 		if (ASIC_IS_AVIVO(rdev))
4017d4550907SJerome Glisse 			return -EINVAL;
4018d4550907SJerome Glisse 	}
4019d4550907SJerome Glisse 	if (rdev->is_atom_bios) {
4020d4550907SJerome Glisse 		dev_err(rdev->dev, "Expecting combios for RS400/RS480 GPU\n");
4021d4550907SJerome Glisse 		return -EINVAL;
4022d4550907SJerome Glisse 	} else {
4023d4550907SJerome Glisse 		r = radeon_combios_init(rdev);
4024d4550907SJerome Glisse 		if (r)
4025d4550907SJerome Glisse 			return r;
4026d4550907SJerome Glisse 	}
4027d4550907SJerome Glisse 	/* Reset gpu before posting otherwise ATOM will enter infinite loop */
4028a2d07b74SJerome Glisse 	if (radeon_asic_reset(rdev)) {
4029d4550907SJerome Glisse 		dev_warn(rdev->dev,
4030d4550907SJerome Glisse 			"GPU reset failed ! (0xE40=0x%08X, 0x7C0=0x%08X)\n",
4031d4550907SJerome Glisse 			RREG32(R_000E40_RBBM_STATUS),
4032d4550907SJerome Glisse 			RREG32(R_0007C0_CP_STAT));
4033d4550907SJerome Glisse 	}
4034d4550907SJerome Glisse 	/* check if cards are posted or not */
403572542d77SDave Airlie 	if (radeon_boot_test_post_card(rdev) == false)
403672542d77SDave Airlie 		return -EINVAL;
4037d4550907SJerome Glisse 	/* Set asic errata */
4038d4550907SJerome Glisse 	r100_errata(rdev);
4039d4550907SJerome Glisse 	/* Initialize clocks */
4040d4550907SJerome Glisse 	radeon_get_clock_info(rdev->ddev);
4041d594e46aSJerome Glisse 	/* initialize AGP */
4042d594e46aSJerome Glisse 	if (rdev->flags & RADEON_IS_AGP) {
4043d594e46aSJerome Glisse 		r = radeon_agp_init(rdev);
4044d594e46aSJerome Glisse 		if (r) {
4045d594e46aSJerome Glisse 			radeon_agp_disable(rdev);
4046d594e46aSJerome Glisse 		}
4047d594e46aSJerome Glisse 	}
4048d594e46aSJerome Glisse 	/* initialize VRAM */
4049d594e46aSJerome Glisse 	r100_mc_init(rdev);
4050d4550907SJerome Glisse 	/* Fence driver */
4051*7465280cSAlex Deucher 	r = radeon_fence_driver_init(rdev, 1);
4052d4550907SJerome Glisse 	if (r)
4053d4550907SJerome Glisse 		return r;
4054d4550907SJerome Glisse 	r = radeon_irq_kms_init(rdev);
4055d4550907SJerome Glisse 	if (r)
4056d4550907SJerome Glisse 		return r;
4057d4550907SJerome Glisse 	/* Memory manager */
40584c788679SJerome Glisse 	r = radeon_bo_init(rdev);
4059d4550907SJerome Glisse 	if (r)
4060d4550907SJerome Glisse 		return r;
4061d4550907SJerome Glisse 	if (rdev->flags & RADEON_IS_PCI) {
4062d4550907SJerome Glisse 		r = r100_pci_gart_init(rdev);
4063d4550907SJerome Glisse 		if (r)
4064d4550907SJerome Glisse 			return r;
4065d4550907SJerome Glisse 	}
4066d4550907SJerome Glisse 	r100_set_safe_registers(rdev);
4067d4550907SJerome Glisse 	rdev->accel_working = true;
4068d4550907SJerome Glisse 	r = r100_startup(rdev);
4069d4550907SJerome Glisse 	if (r) {
4070d4550907SJerome Glisse 		/* Somethings want wront with the accel init stop accel */
4071d4550907SJerome Glisse 		dev_err(rdev->dev, "Disabling GPU acceleration\n");
4072d4550907SJerome Glisse 		r100_cp_fini(rdev);
4073724c80e1SAlex Deucher 		radeon_wb_fini(rdev);
4074d4550907SJerome Glisse 		r100_ib_fini(rdev);
4075655efd3dSJerome Glisse 		radeon_irq_kms_fini(rdev);
4076d4550907SJerome Glisse 		if (rdev->flags & RADEON_IS_PCI)
4077d4550907SJerome Glisse 			r100_pci_gart_fini(rdev);
4078d4550907SJerome Glisse 		rdev->accel_working = false;
4079d4550907SJerome Glisse 	}
4080d4550907SJerome Glisse 	return 0;
4081d4550907SJerome Glisse }
40826fcbef7aSAndi Kleen 
40836fcbef7aSAndi Kleen uint32_t r100_mm_rreg(struct radeon_device *rdev, uint32_t reg)
40846fcbef7aSAndi Kleen {
40856fcbef7aSAndi Kleen 	if (reg < rdev->rmmio_size)
40866fcbef7aSAndi Kleen 		return readl(((void __iomem *)rdev->rmmio) + reg);
40876fcbef7aSAndi Kleen 	else {
40886fcbef7aSAndi Kleen 		writel(reg, ((void __iomem *)rdev->rmmio) + RADEON_MM_INDEX);
40896fcbef7aSAndi Kleen 		return readl(((void __iomem *)rdev->rmmio) + RADEON_MM_DATA);
40906fcbef7aSAndi Kleen 	}
40916fcbef7aSAndi Kleen }
40926fcbef7aSAndi Kleen 
40936fcbef7aSAndi Kleen void r100_mm_wreg(struct radeon_device *rdev, uint32_t reg, uint32_t v)
40946fcbef7aSAndi Kleen {
40956fcbef7aSAndi Kleen 	if (reg < rdev->rmmio_size)
40966fcbef7aSAndi Kleen 		writel(v, ((void __iomem *)rdev->rmmio) + reg);
40976fcbef7aSAndi Kleen 	else {
40986fcbef7aSAndi Kleen 		writel(reg, ((void __iomem *)rdev->rmmio) + RADEON_MM_INDEX);
40996fcbef7aSAndi Kleen 		writel(v, ((void __iomem *)rdev->rmmio) + RADEON_MM_DATA);
41006fcbef7aSAndi Kleen 	}
41016fcbef7aSAndi Kleen }
41026fcbef7aSAndi Kleen 
41036fcbef7aSAndi Kleen u32 r100_io_rreg(struct radeon_device *rdev, u32 reg)
41046fcbef7aSAndi Kleen {
41056fcbef7aSAndi Kleen 	if (reg < rdev->rio_mem_size)
41066fcbef7aSAndi Kleen 		return ioread32(rdev->rio_mem + reg);
41076fcbef7aSAndi Kleen 	else {
41086fcbef7aSAndi Kleen 		iowrite32(reg, rdev->rio_mem + RADEON_MM_INDEX);
41096fcbef7aSAndi Kleen 		return ioread32(rdev->rio_mem + RADEON_MM_DATA);
41106fcbef7aSAndi Kleen 	}
41116fcbef7aSAndi Kleen }
41126fcbef7aSAndi Kleen 
41136fcbef7aSAndi Kleen void r100_io_wreg(struct radeon_device *rdev, u32 reg, u32 v)
41146fcbef7aSAndi Kleen {
41156fcbef7aSAndi Kleen 	if (reg < rdev->rio_mem_size)
41166fcbef7aSAndi Kleen 		iowrite32(v, rdev->rio_mem + reg);
41176fcbef7aSAndi Kleen 	else {
41186fcbef7aSAndi Kleen 		iowrite32(reg, rdev->rio_mem + RADEON_MM_INDEX);
41196fcbef7aSAndi Kleen 		iowrite32(v, rdev->rio_mem + RADEON_MM_DATA);
41206fcbef7aSAndi Kleen 	}
41216fcbef7aSAndi Kleen }
4122