blob: 5d14732dc28492b9ecc56baf39285e97aa9e25dd [file] [log] [blame]
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001/*
2 * Copyright 2008 Advanced Micro Devices, Inc.
3 * Copyright 2008 Red Hat Inc.
4 * Copyright 2009 Jerome Glisse.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors: Dave Airlie
25 * Alex Deucher
26 * Jerome Glisse
27 */
28#include <linux/seq_file.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Jerome Glisse771fe6b2009-06-05 14:42:42 +020030#include "drmP.h"
31#include "drm.h"
32#include "radeon_drm.h"
Jerome Glisse771fe6b2009-06-05 14:42:42 +020033#include "radeon_reg.h"
34#include "radeon.h"
Daniel Vettere6990372010-03-11 21:19:17 +000035#include "radeon_asic.h"
Jerome Glisse3ce0a232009-09-08 10:10:24 +100036#include "r100d.h"
Jerome Glissed4550902009-10-01 10:12:06 +020037#include "rs100d.h"
38#include "rv200d.h"
39#include "rv250d.h"
Alex Deucher49e02b72010-04-23 17:57:27 -040040#include "atom.h"
Jerome Glisse3ce0a232009-09-08 10:10:24 +100041
Ben Hutchings70967ab2009-08-29 14:53:51 +010042#include <linux/firmware.h>
43#include <linux/platform_device.h>
44
Dave Airlie551ebd82009-09-01 15:25:57 +100045#include "r100_reg_safe.h"
46#include "rn50_reg_safe.h"
47
Ben Hutchings70967ab2009-08-29 14:53:51 +010048/* Firmware Names */
49#define FIRMWARE_R100 "radeon/R100_cp.bin"
50#define FIRMWARE_R200 "radeon/R200_cp.bin"
51#define FIRMWARE_R300 "radeon/R300_cp.bin"
52#define FIRMWARE_R420 "radeon/R420_cp.bin"
53#define FIRMWARE_RS690 "radeon/RS690_cp.bin"
54#define FIRMWARE_RS600 "radeon/RS600_cp.bin"
55#define FIRMWARE_R520 "radeon/R520_cp.bin"
56
57MODULE_FIRMWARE(FIRMWARE_R100);
58MODULE_FIRMWARE(FIRMWARE_R200);
59MODULE_FIRMWARE(FIRMWARE_R300);
60MODULE_FIRMWARE(FIRMWARE_R420);
61MODULE_FIRMWARE(FIRMWARE_RS690);
62MODULE_FIRMWARE(FIRMWARE_RS600);
63MODULE_FIRMWARE(FIRMWARE_R520);
Jerome Glisse771fe6b2009-06-05 14:42:42 +020064
Dave Airlie551ebd82009-09-01 15:25:57 +100065#include "r100_track.h"
66
Jerome Glisse771fe6b2009-06-05 14:42:42 +020067/* This files gather functions specifics to:
68 * r100,rv100,rs100,rv200,rs200,r200,rv250,rs300,rv280
Jerome Glisse771fe6b2009-06-05 14:42:42 +020069 */
Jerome Glisse771fe6b2009-06-05 14:42:42 +020070
Alex Deucherce8f5372010-05-07 15:10:16 -040071void r100_pm_get_dynpm_state(struct radeon_device *rdev)
Alex Deuchera48b9b42010-04-22 14:03:55 -040072{
73 int i;
Alex Deucherce8f5372010-05-07 15:10:16 -040074 rdev->pm.dynpm_can_upclock = true;
75 rdev->pm.dynpm_can_downclock = true;
Alex Deuchera48b9b42010-04-22 14:03:55 -040076
Alex Deucherce8f5372010-05-07 15:10:16 -040077 switch (rdev->pm.dynpm_planned_action) {
78 case DYNPM_ACTION_MINIMUM:
Alex Deuchera48b9b42010-04-22 14:03:55 -040079 rdev->pm.requested_power_state_index = 0;
Alex Deucherce8f5372010-05-07 15:10:16 -040080 rdev->pm.dynpm_can_downclock = false;
Alex Deuchera48b9b42010-04-22 14:03:55 -040081 break;
Alex Deucherce8f5372010-05-07 15:10:16 -040082 case DYNPM_ACTION_DOWNCLOCK:
Alex Deuchera48b9b42010-04-22 14:03:55 -040083 if (rdev->pm.current_power_state_index == 0) {
84 rdev->pm.requested_power_state_index = rdev->pm.current_power_state_index;
Alex Deucherce8f5372010-05-07 15:10:16 -040085 rdev->pm.dynpm_can_downclock = false;
Alex Deuchera48b9b42010-04-22 14:03:55 -040086 } else {
87 if (rdev->pm.active_crtc_count > 1) {
88 for (i = 0; i < rdev->pm.num_power_states; i++) {
Alex Deucherd7311172010-05-03 01:13:14 -040089 if (rdev->pm.power_state[i].flags & RADEON_PM_STATE_SINGLE_DISPLAY_ONLY)
Alex Deuchera48b9b42010-04-22 14:03:55 -040090 continue;
91 else if (i >= rdev->pm.current_power_state_index) {
92 rdev->pm.requested_power_state_index = rdev->pm.current_power_state_index;
93 break;
94 } else {
95 rdev->pm.requested_power_state_index = i;
96 break;
97 }
98 }
99 } else
100 rdev->pm.requested_power_state_index =
101 rdev->pm.current_power_state_index - 1;
102 }
Alex Deucherd7311172010-05-03 01:13:14 -0400103 /* don't use the power state if crtcs are active and no display flag is set */
104 if ((rdev->pm.active_crtc_count > 0) &&
105 (rdev->pm.power_state[rdev->pm.requested_power_state_index].clock_info[0].flags &
106 RADEON_PM_MODE_NO_DISPLAY)) {
107 rdev->pm.requested_power_state_index++;
108 }
Alex Deuchera48b9b42010-04-22 14:03:55 -0400109 break;
Alex Deucherce8f5372010-05-07 15:10:16 -0400110 case DYNPM_ACTION_UPCLOCK:
Alex Deuchera48b9b42010-04-22 14:03:55 -0400111 if (rdev->pm.current_power_state_index == (rdev->pm.num_power_states - 1)) {
112 rdev->pm.requested_power_state_index = rdev->pm.current_power_state_index;
Alex Deucherce8f5372010-05-07 15:10:16 -0400113 rdev->pm.dynpm_can_upclock = false;
Alex Deuchera48b9b42010-04-22 14:03:55 -0400114 } else {
115 if (rdev->pm.active_crtc_count > 1) {
116 for (i = (rdev->pm.num_power_states - 1); i >= 0; i--) {
Alex Deucherd7311172010-05-03 01:13:14 -0400117 if (rdev->pm.power_state[i].flags & RADEON_PM_STATE_SINGLE_DISPLAY_ONLY)
Alex Deuchera48b9b42010-04-22 14:03:55 -0400118 continue;
119 else if (i <= rdev->pm.current_power_state_index) {
120 rdev->pm.requested_power_state_index = rdev->pm.current_power_state_index;
121 break;
122 } else {
123 rdev->pm.requested_power_state_index = i;
124 break;
125 }
126 }
127 } else
128 rdev->pm.requested_power_state_index =
129 rdev->pm.current_power_state_index + 1;
130 }
131 break;
Alex Deucherce8f5372010-05-07 15:10:16 -0400132 case DYNPM_ACTION_DEFAULT:
Alex Deucher58e21df2010-03-22 13:31:08 -0400133 rdev->pm.requested_power_state_index = rdev->pm.default_power_state_index;
Alex Deucherce8f5372010-05-07 15:10:16 -0400134 rdev->pm.dynpm_can_upclock = false;
Alex Deucher58e21df2010-03-22 13:31:08 -0400135 break;
Alex Deucherce8f5372010-05-07 15:10:16 -0400136 case DYNPM_ACTION_NONE:
Alex Deuchera48b9b42010-04-22 14:03:55 -0400137 default:
138 DRM_ERROR("Requested mode for not defined action\n");
139 return;
140 }
141 /* only one clock mode per power state */
142 rdev->pm.requested_clock_mode_index = 0;
143
Alex Deucherce8a3eb2010-05-07 16:58:27 -0400144 DRM_DEBUG("Requested: e: %d m: %d p: %d\n",
145 rdev->pm.power_state[rdev->pm.requested_power_state_index].
146 clock_info[rdev->pm.requested_clock_mode_index].sclk,
147 rdev->pm.power_state[rdev->pm.requested_power_state_index].
148 clock_info[rdev->pm.requested_clock_mode_index].mclk,
149 rdev->pm.power_state[rdev->pm.requested_power_state_index].
150 pcie_lanes);
Alex Deuchera48b9b42010-04-22 14:03:55 -0400151}
152
Alex Deucherce8f5372010-05-07 15:10:16 -0400153void r100_pm_init_profile(struct radeon_device *rdev)
Alex Deucherbae6b5622010-04-22 13:38:05 -0400154{
Alex Deucherce8f5372010-05-07 15:10:16 -0400155 /* default */
156 rdev->pm.profiles[PM_PROFILE_DEFAULT_IDX].dpms_off_ps_idx = rdev->pm.default_power_state_index;
157 rdev->pm.profiles[PM_PROFILE_DEFAULT_IDX].dpms_on_ps_idx = rdev->pm.default_power_state_index;
158 rdev->pm.profiles[PM_PROFILE_DEFAULT_IDX].dpms_off_cm_idx = 0;
159 rdev->pm.profiles[PM_PROFILE_DEFAULT_IDX].dpms_on_cm_idx = 0;
160 /* low sh */
161 rdev->pm.profiles[PM_PROFILE_LOW_SH_IDX].dpms_off_ps_idx = 0;
162 rdev->pm.profiles[PM_PROFILE_LOW_SH_IDX].dpms_on_ps_idx = 0;
163 rdev->pm.profiles[PM_PROFILE_LOW_SH_IDX].dpms_off_cm_idx = 0;
164 rdev->pm.profiles[PM_PROFILE_LOW_SH_IDX].dpms_on_cm_idx = 0;
Alex Deucherc9e75b22010-06-02 17:56:01 -0400165 /* mid sh */
166 rdev->pm.profiles[PM_PROFILE_MID_SH_IDX].dpms_off_ps_idx = 0;
167 rdev->pm.profiles[PM_PROFILE_MID_SH_IDX].dpms_on_ps_idx = 0;
168 rdev->pm.profiles[PM_PROFILE_MID_SH_IDX].dpms_off_cm_idx = 0;
169 rdev->pm.profiles[PM_PROFILE_MID_SH_IDX].dpms_on_cm_idx = 0;
Alex Deucherce8f5372010-05-07 15:10:16 -0400170 /* high sh */
171 rdev->pm.profiles[PM_PROFILE_HIGH_SH_IDX].dpms_off_ps_idx = 0;
172 rdev->pm.profiles[PM_PROFILE_HIGH_SH_IDX].dpms_on_ps_idx = rdev->pm.default_power_state_index;
173 rdev->pm.profiles[PM_PROFILE_HIGH_SH_IDX].dpms_off_cm_idx = 0;
174 rdev->pm.profiles[PM_PROFILE_HIGH_SH_IDX].dpms_on_cm_idx = 0;
175 /* low mh */
176 rdev->pm.profiles[PM_PROFILE_LOW_MH_IDX].dpms_off_ps_idx = 0;
177 rdev->pm.profiles[PM_PROFILE_LOW_MH_IDX].dpms_on_ps_idx = rdev->pm.default_power_state_index;
178 rdev->pm.profiles[PM_PROFILE_LOW_MH_IDX].dpms_off_cm_idx = 0;
179 rdev->pm.profiles[PM_PROFILE_LOW_MH_IDX].dpms_on_cm_idx = 0;
Alex Deucherc9e75b22010-06-02 17:56:01 -0400180 /* mid mh */
181 rdev->pm.profiles[PM_PROFILE_MID_MH_IDX].dpms_off_ps_idx = 0;
182 rdev->pm.profiles[PM_PROFILE_MID_MH_IDX].dpms_on_ps_idx = rdev->pm.default_power_state_index;
183 rdev->pm.profiles[PM_PROFILE_MID_MH_IDX].dpms_off_cm_idx = 0;
184 rdev->pm.profiles[PM_PROFILE_MID_MH_IDX].dpms_on_cm_idx = 0;
Alex Deucherce8f5372010-05-07 15:10:16 -0400185 /* high mh */
186 rdev->pm.profiles[PM_PROFILE_HIGH_MH_IDX].dpms_off_ps_idx = 0;
187 rdev->pm.profiles[PM_PROFILE_HIGH_MH_IDX].dpms_on_ps_idx = rdev->pm.default_power_state_index;
188 rdev->pm.profiles[PM_PROFILE_HIGH_MH_IDX].dpms_off_cm_idx = 0;
189 rdev->pm.profiles[PM_PROFILE_HIGH_MH_IDX].dpms_on_cm_idx = 0;
Alex Deucherbae6b5622010-04-22 13:38:05 -0400190}
191
Alex Deucher49e02b72010-04-23 17:57:27 -0400192void r100_pm_misc(struct radeon_device *rdev)
193{
Alex Deucher49e02b72010-04-23 17:57:27 -0400194 int requested_index = rdev->pm.requested_power_state_index;
195 struct radeon_power_state *ps = &rdev->pm.power_state[requested_index];
196 struct radeon_voltage *voltage = &ps->clock_info[0].voltage;
197 u32 tmp, sclk_cntl, sclk_cntl2, sclk_more_cntl;
198
199 if ((voltage->type == VOLTAGE_GPIO) && (voltage->gpio.valid)) {
200 if (ps->misc & ATOM_PM_MISCINFO_VOLTAGE_DROP_SUPPORT) {
201 tmp = RREG32(voltage->gpio.reg);
202 if (voltage->active_high)
203 tmp |= voltage->gpio.mask;
204 else
205 tmp &= ~(voltage->gpio.mask);
206 WREG32(voltage->gpio.reg, tmp);
207 if (voltage->delay)
208 udelay(voltage->delay);
209 } else {
210 tmp = RREG32(voltage->gpio.reg);
211 if (voltage->active_high)
212 tmp &= ~voltage->gpio.mask;
213 else
214 tmp |= voltage->gpio.mask;
215 WREG32(voltage->gpio.reg, tmp);
216 if (voltage->delay)
217 udelay(voltage->delay);
218 }
219 }
220
221 sclk_cntl = RREG32_PLL(SCLK_CNTL);
222 sclk_cntl2 = RREG32_PLL(SCLK_CNTL2);
223 sclk_cntl2 &= ~REDUCED_SPEED_SCLK_SEL(3);
224 sclk_more_cntl = RREG32_PLL(SCLK_MORE_CNTL);
225 sclk_more_cntl &= ~VOLTAGE_DELAY_SEL(3);
226 if (ps->misc & ATOM_PM_MISCINFO_ASIC_REDUCED_SPEED_SCLK_EN) {
227 sclk_more_cntl |= REDUCED_SPEED_SCLK_EN;
228 if (ps->misc & ATOM_PM_MISCINFO_DYN_CLK_3D_IDLE)
229 sclk_cntl2 |= REDUCED_SPEED_SCLK_MODE;
230 else
231 sclk_cntl2 &= ~REDUCED_SPEED_SCLK_MODE;
232 if (ps->misc & ATOM_PM_MISCINFO_DYNAMIC_CLOCK_DIVIDER_BY_2)
233 sclk_cntl2 |= REDUCED_SPEED_SCLK_SEL(0);
234 else if (ps->misc & ATOM_PM_MISCINFO_DYNAMIC_CLOCK_DIVIDER_BY_4)
235 sclk_cntl2 |= REDUCED_SPEED_SCLK_SEL(2);
236 } else
237 sclk_more_cntl &= ~REDUCED_SPEED_SCLK_EN;
238
239 if (ps->misc & ATOM_PM_MISCINFO_ASIC_DYNAMIC_VOLTAGE_EN) {
240 sclk_more_cntl |= IO_CG_VOLTAGE_DROP;
241 if (voltage->delay) {
242 sclk_more_cntl |= VOLTAGE_DROP_SYNC;
243 switch (voltage->delay) {
244 case 33:
245 sclk_more_cntl |= VOLTAGE_DELAY_SEL(0);
246 break;
247 case 66:
248 sclk_more_cntl |= VOLTAGE_DELAY_SEL(1);
249 break;
250 case 99:
251 sclk_more_cntl |= VOLTAGE_DELAY_SEL(2);
252 break;
253 case 132:
254 sclk_more_cntl |= VOLTAGE_DELAY_SEL(3);
255 break;
256 }
257 } else
258 sclk_more_cntl &= ~VOLTAGE_DROP_SYNC;
259 } else
260 sclk_more_cntl &= ~IO_CG_VOLTAGE_DROP;
261
262 if (ps->misc & ATOM_PM_MISCINFO_DYNAMIC_HDP_BLOCK_EN)
263 sclk_cntl &= ~FORCE_HDP;
264 else
265 sclk_cntl |= FORCE_HDP;
266
267 WREG32_PLL(SCLK_CNTL, sclk_cntl);
268 WREG32_PLL(SCLK_CNTL2, sclk_cntl2);
269 WREG32_PLL(SCLK_MORE_CNTL, sclk_more_cntl);
270
271 /* set pcie lanes */
272 if ((rdev->flags & RADEON_IS_PCIE) &&
273 !(rdev->flags & RADEON_IS_IGP) &&
274 rdev->asic->set_pcie_lanes &&
275 (ps->pcie_lanes !=
276 rdev->pm.power_state[rdev->pm.current_power_state_index].pcie_lanes)) {
277 radeon_set_pcie_lanes(rdev,
278 ps->pcie_lanes);
Alex Deucherce8a3eb2010-05-07 16:58:27 -0400279 DRM_DEBUG("Setting: p: %d\n", ps->pcie_lanes);
Alex Deucher49e02b72010-04-23 17:57:27 -0400280 }
Alex Deucher49e02b72010-04-23 17:57:27 -0400281}
282
283void r100_pm_prepare(struct radeon_device *rdev)
284{
285 struct drm_device *ddev = rdev->ddev;
286 struct drm_crtc *crtc;
287 struct radeon_crtc *radeon_crtc;
288 u32 tmp;
289
290 /* disable any active CRTCs */
291 list_for_each_entry(crtc, &ddev->mode_config.crtc_list, head) {
292 radeon_crtc = to_radeon_crtc(crtc);
293 if (radeon_crtc->enabled) {
294 if (radeon_crtc->crtc_id) {
295 tmp = RREG32(RADEON_CRTC2_GEN_CNTL);
296 tmp |= RADEON_CRTC2_DISP_REQ_EN_B;
297 WREG32(RADEON_CRTC2_GEN_CNTL, tmp);
298 } else {
299 tmp = RREG32(RADEON_CRTC_GEN_CNTL);
300 tmp |= RADEON_CRTC_DISP_REQ_EN_B;
301 WREG32(RADEON_CRTC_GEN_CNTL, tmp);
302 }
303 }
304 }
305}
306
307void r100_pm_finish(struct radeon_device *rdev)
308{
309 struct drm_device *ddev = rdev->ddev;
310 struct drm_crtc *crtc;
311 struct radeon_crtc *radeon_crtc;
312 u32 tmp;
313
314 /* enable any active CRTCs */
315 list_for_each_entry(crtc, &ddev->mode_config.crtc_list, head) {
316 radeon_crtc = to_radeon_crtc(crtc);
317 if (radeon_crtc->enabled) {
318 if (radeon_crtc->crtc_id) {
319 tmp = RREG32(RADEON_CRTC2_GEN_CNTL);
320 tmp &= ~RADEON_CRTC2_DISP_REQ_EN_B;
321 WREG32(RADEON_CRTC2_GEN_CNTL, tmp);
322 } else {
323 tmp = RREG32(RADEON_CRTC_GEN_CNTL);
324 tmp &= ~RADEON_CRTC_DISP_REQ_EN_B;
325 WREG32(RADEON_CRTC_GEN_CNTL, tmp);
326 }
327 }
328 }
329}
330
Alex Deucherdef9ba92010-04-22 12:39:58 -0400331bool r100_gui_idle(struct radeon_device *rdev)
332{
333 if (RREG32(RADEON_RBBM_STATUS) & RADEON_RBBM_ACTIVE)
334 return false;
335 else
336 return true;
337}
338
Alex Deucher05a05c52009-12-04 14:53:41 -0500339/* hpd for digital panel detect/disconnect */
340bool r100_hpd_sense(struct radeon_device *rdev, enum radeon_hpd_id hpd)
341{
342 bool connected = false;
343
344 switch (hpd) {
345 case RADEON_HPD_1:
346 if (RREG32(RADEON_FP_GEN_CNTL) & RADEON_FP_DETECT_SENSE)
347 connected = true;
348 break;
349 case RADEON_HPD_2:
350 if (RREG32(RADEON_FP2_GEN_CNTL) & RADEON_FP2_DETECT_SENSE)
351 connected = true;
352 break;
353 default:
354 break;
355 }
356 return connected;
357}
358
359void r100_hpd_set_polarity(struct radeon_device *rdev,
360 enum radeon_hpd_id hpd)
361{
362 u32 tmp;
363 bool connected = r100_hpd_sense(rdev, hpd);
364
365 switch (hpd) {
366 case RADEON_HPD_1:
367 tmp = RREG32(RADEON_FP_GEN_CNTL);
368 if (connected)
369 tmp &= ~RADEON_FP_DETECT_INT_POL;
370 else
371 tmp |= RADEON_FP_DETECT_INT_POL;
372 WREG32(RADEON_FP_GEN_CNTL, tmp);
373 break;
374 case RADEON_HPD_2:
375 tmp = RREG32(RADEON_FP2_GEN_CNTL);
376 if (connected)
377 tmp &= ~RADEON_FP2_DETECT_INT_POL;
378 else
379 tmp |= RADEON_FP2_DETECT_INT_POL;
380 WREG32(RADEON_FP2_GEN_CNTL, tmp);
381 break;
382 default:
383 break;
384 }
385}
386
387void r100_hpd_init(struct radeon_device *rdev)
388{
389 struct drm_device *dev = rdev->ddev;
390 struct drm_connector *connector;
391
392 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
393 struct radeon_connector *radeon_connector = to_radeon_connector(connector);
394 switch (radeon_connector->hpd.hpd) {
395 case RADEON_HPD_1:
396 rdev->irq.hpd[0] = true;
397 break;
398 case RADEON_HPD_2:
399 rdev->irq.hpd[1] = true;
400 break;
401 default:
402 break;
403 }
404 }
Jerome Glisse003e69f2010-01-07 15:39:14 +0100405 if (rdev->irq.installed)
406 r100_irq_set(rdev);
Alex Deucher05a05c52009-12-04 14:53:41 -0500407}
408
409void r100_hpd_fini(struct radeon_device *rdev)
410{
411 struct drm_device *dev = rdev->ddev;
412 struct drm_connector *connector;
413
414 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
415 struct radeon_connector *radeon_connector = to_radeon_connector(connector);
416 switch (radeon_connector->hpd.hpd) {
417 case RADEON_HPD_1:
418 rdev->irq.hpd[0] = false;
419 break;
420 case RADEON_HPD_2:
421 rdev->irq.hpd[1] = false;
422 break;
423 default:
424 break;
425 }
426 }
427}
428
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200429/*
430 * PCI GART
431 */
432void r100_pci_gart_tlb_flush(struct radeon_device *rdev)
433{
434 /* TODO: can we do somethings here ? */
435 /* It seems hw only cache one entry so we should discard this
436 * entry otherwise if first GPU GART read hit this entry it
437 * could end up in wrong address. */
438}
439
Jerome Glisse4aac0472009-09-14 18:29:49 +0200440int r100_pci_gart_init(struct radeon_device *rdev)
441{
442 int r;
443
444 if (rdev->gart.table.ram.ptr) {
445 WARN(1, "R100 PCI GART already initialized.\n");
446 return 0;
447 }
448 /* Initialize common gart structure */
449 r = radeon_gart_init(rdev);
450 if (r)
451 return r;
452 rdev->gart.table_size = rdev->gart.num_gpu_pages * 4;
453 rdev->asic->gart_tlb_flush = &r100_pci_gart_tlb_flush;
454 rdev->asic->gart_set_page = &r100_pci_gart_set_page;
455 return radeon_gart_table_ram_alloc(rdev);
456}
457
Dave Airlie17e15b02009-11-05 15:36:53 +1000458/* required on r1xx, r2xx, r300, r(v)350, r420/r481, rs400/rs480 */
459void r100_enable_bm(struct radeon_device *rdev)
460{
461 uint32_t tmp;
462 /* Enable bus mastering */
463 tmp = RREG32(RADEON_BUS_CNTL) & ~RADEON_BUS_MASTER_DIS;
464 WREG32(RADEON_BUS_CNTL, tmp);
465}
466
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200467int r100_pci_gart_enable(struct radeon_device *rdev)
468{
469 uint32_t tmp;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200470
Dave Airlie82568562010-02-05 16:00:07 +1000471 radeon_gart_restore(rdev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200472 /* discard memory request outside of configured range */
473 tmp = RREG32(RADEON_AIC_CNTL) | RADEON_DIS_OUT_OF_PCI_GART_ACCESS;
474 WREG32(RADEON_AIC_CNTL, tmp);
475 /* set address range for PCI address translate */
Jerome Glissed594e462010-02-17 21:54:29 +0000476 WREG32(RADEON_AIC_LO_ADDR, rdev->mc.gtt_start);
477 WREG32(RADEON_AIC_HI_ADDR, rdev->mc.gtt_end);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200478 /* set PCI GART page-table base address */
479 WREG32(RADEON_AIC_PT_BASE, rdev->gart.table_addr);
480 tmp = RREG32(RADEON_AIC_CNTL) | RADEON_PCIGART_TRANSLATE_EN;
481 WREG32(RADEON_AIC_CNTL, tmp);
482 r100_pci_gart_tlb_flush(rdev);
483 rdev->gart.ready = true;
484 return 0;
485}
486
487void r100_pci_gart_disable(struct radeon_device *rdev)
488{
489 uint32_t tmp;
490
491 /* discard memory request outside of configured range */
492 tmp = RREG32(RADEON_AIC_CNTL) | RADEON_DIS_OUT_OF_PCI_GART_ACCESS;
493 WREG32(RADEON_AIC_CNTL, tmp & ~RADEON_PCIGART_TRANSLATE_EN);
494 WREG32(RADEON_AIC_LO_ADDR, 0);
495 WREG32(RADEON_AIC_HI_ADDR, 0);
496}
497
498int r100_pci_gart_set_page(struct radeon_device *rdev, int i, uint64_t addr)
499{
500 if (i < 0 || i > rdev->gart.num_gpu_pages) {
501 return -EINVAL;
502 }
Dave Airlieed10f952009-06-29 18:29:11 +1000503 rdev->gart.table.ram.ptr[i] = cpu_to_le32(lower_32_bits(addr));
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200504 return 0;
505}
506
Jerome Glisse4aac0472009-09-14 18:29:49 +0200507void r100_pci_gart_fini(struct radeon_device *rdev)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200508{
Jerome Glissef9274562010-03-17 14:44:29 +0000509 radeon_gart_fini(rdev);
Jerome Glisse4aac0472009-09-14 18:29:49 +0200510 r100_pci_gart_disable(rdev);
511 radeon_gart_table_ram_free(rdev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200512}
513
Michel Dänzer7ed220d2009-08-13 11:10:51 +0200514int r100_irq_set(struct radeon_device *rdev)
515{
516 uint32_t tmp = 0;
517
Jerome Glisse003e69f2010-01-07 15:39:14 +0100518 if (!rdev->irq.installed) {
519 WARN(1, "Can't enable IRQ/MSI because no handler is installed.\n");
520 WREG32(R_000040_GEN_INT_CNTL, 0);
521 return -EINVAL;
522 }
Michel Dänzer7ed220d2009-08-13 11:10:51 +0200523 if (rdev->irq.sw_int) {
524 tmp |= RADEON_SW_INT_ENABLE;
525 }
Alex Deucher2031f772010-04-22 12:52:11 -0400526 if (rdev->irq.gui_idle) {
527 tmp |= RADEON_GUI_IDLE_MASK;
528 }
Michel Dänzer7ed220d2009-08-13 11:10:51 +0200529 if (rdev->irq.crtc_vblank_int[0]) {
530 tmp |= RADEON_CRTC_VBLANK_MASK;
531 }
532 if (rdev->irq.crtc_vblank_int[1]) {
533 tmp |= RADEON_CRTC2_VBLANK_MASK;
534 }
Alex Deucher05a05c52009-12-04 14:53:41 -0500535 if (rdev->irq.hpd[0]) {
536 tmp |= RADEON_FP_DETECT_MASK;
537 }
538 if (rdev->irq.hpd[1]) {
539 tmp |= RADEON_FP2_DETECT_MASK;
540 }
Michel Dänzer7ed220d2009-08-13 11:10:51 +0200541 WREG32(RADEON_GEN_INT_CNTL, tmp);
542 return 0;
543}
544
Jerome Glisse9f022dd2009-09-11 15:35:22 +0200545void r100_irq_disable(struct radeon_device *rdev)
546{
547 u32 tmp;
548
549 WREG32(R_000040_GEN_INT_CNTL, 0);
550 /* Wait and acknowledge irq */
551 mdelay(1);
552 tmp = RREG32(R_000044_GEN_INT_STATUS);
553 WREG32(R_000044_GEN_INT_STATUS, tmp);
554}
555
Michel Dänzer7ed220d2009-08-13 11:10:51 +0200556static inline uint32_t r100_irq_ack(struct radeon_device *rdev)
557{
558 uint32_t irqs = RREG32(RADEON_GEN_INT_STATUS);
Alex Deucher05a05c52009-12-04 14:53:41 -0500559 uint32_t irq_mask = RADEON_SW_INT_TEST |
560 RADEON_CRTC_VBLANK_STAT | RADEON_CRTC2_VBLANK_STAT |
561 RADEON_FP_DETECT_STAT | RADEON_FP2_DETECT_STAT;
Michel Dänzer7ed220d2009-08-13 11:10:51 +0200562
Alex Deucher2031f772010-04-22 12:52:11 -0400563 /* the interrupt works, but the status bit is permanently asserted */
564 if (rdev->irq.gui_idle && radeon_gui_idle(rdev)) {
565 if (!rdev->irq.gui_idle_acked)
566 irq_mask |= RADEON_GUI_IDLE_STAT;
567 }
568
Michel Dänzer7ed220d2009-08-13 11:10:51 +0200569 if (irqs) {
570 WREG32(RADEON_GEN_INT_STATUS, irqs);
571 }
572 return irqs & irq_mask;
573}
574
575int r100_irq_process(struct radeon_device *rdev)
576{
Alex Deucher3e5cb982009-10-16 12:21:24 -0400577 uint32_t status, msi_rearm;
Alex Deucherd4877cf2009-12-04 16:56:37 -0500578 bool queue_hotplug = false;
Michel Dänzer7ed220d2009-08-13 11:10:51 +0200579
Alex Deucher2031f772010-04-22 12:52:11 -0400580 /* reset gui idle ack. the status bit is broken */
581 rdev->irq.gui_idle_acked = false;
582
Michel Dänzer7ed220d2009-08-13 11:10:51 +0200583 status = r100_irq_ack(rdev);
584 if (!status) {
585 return IRQ_NONE;
586 }
Jerome Glissea513c182009-09-09 22:23:07 +0200587 if (rdev->shutdown) {
588 return IRQ_NONE;
589 }
Michel Dänzer7ed220d2009-08-13 11:10:51 +0200590 while (status) {
591 /* SW interrupt */
592 if (status & RADEON_SW_INT_TEST) {
593 radeon_fence_process(rdev);
594 }
Alex Deucher2031f772010-04-22 12:52:11 -0400595 /* gui idle interrupt */
596 if (status & RADEON_GUI_IDLE_STAT) {
597 rdev->irq.gui_idle_acked = true;
598 rdev->pm.gui_idle = true;
599 wake_up(&rdev->irq.idle_queue);
600 }
Michel Dänzer7ed220d2009-08-13 11:10:51 +0200601 /* Vertical blank interrupts */
602 if (status & RADEON_CRTC_VBLANK_STAT) {
603 drm_handle_vblank(rdev->ddev, 0);
Rafał Miłecki839461d2010-03-02 22:06:51 +0100604 rdev->pm.vblank_sync = true;
Rafał Miłecki73a6d3f2010-01-08 00:22:47 +0100605 wake_up(&rdev->irq.vblank_queue);
Michel Dänzer7ed220d2009-08-13 11:10:51 +0200606 }
607 if (status & RADEON_CRTC2_VBLANK_STAT) {
608 drm_handle_vblank(rdev->ddev, 1);
Rafał Miłecki839461d2010-03-02 22:06:51 +0100609 rdev->pm.vblank_sync = true;
Rafał Miłecki73a6d3f2010-01-08 00:22:47 +0100610 wake_up(&rdev->irq.vblank_queue);
Michel Dänzer7ed220d2009-08-13 11:10:51 +0200611 }
Alex Deucher05a05c52009-12-04 14:53:41 -0500612 if (status & RADEON_FP_DETECT_STAT) {
Alex Deucherd4877cf2009-12-04 16:56:37 -0500613 queue_hotplug = true;
614 DRM_DEBUG("HPD1\n");
Alex Deucher05a05c52009-12-04 14:53:41 -0500615 }
616 if (status & RADEON_FP2_DETECT_STAT) {
Alex Deucherd4877cf2009-12-04 16:56:37 -0500617 queue_hotplug = true;
618 DRM_DEBUG("HPD2\n");
Alex Deucher05a05c52009-12-04 14:53:41 -0500619 }
Michel Dänzer7ed220d2009-08-13 11:10:51 +0200620 status = r100_irq_ack(rdev);
621 }
Alex Deucher2031f772010-04-22 12:52:11 -0400622 /* reset gui idle ack. the status bit is broken */
623 rdev->irq.gui_idle_acked = false;
Alex Deucherd4877cf2009-12-04 16:56:37 -0500624 if (queue_hotplug)
625 queue_work(rdev->wq, &rdev->hotplug_work);
Alex Deucher3e5cb982009-10-16 12:21:24 -0400626 if (rdev->msi_enabled) {
627 switch (rdev->family) {
628 case CHIP_RS400:
629 case CHIP_RS480:
630 msi_rearm = RREG32(RADEON_AIC_CNTL) & ~RS400_MSI_REARM;
631 WREG32(RADEON_AIC_CNTL, msi_rearm);
632 WREG32(RADEON_AIC_CNTL, msi_rearm | RS400_MSI_REARM);
633 break;
634 default:
635 msi_rearm = RREG32(RADEON_MSI_REARM_EN) & ~RV370_MSI_REARM_EN;
636 WREG32(RADEON_MSI_REARM_EN, msi_rearm);
637 WREG32(RADEON_MSI_REARM_EN, msi_rearm | RV370_MSI_REARM_EN);
638 break;
639 }
640 }
Michel Dänzer7ed220d2009-08-13 11:10:51 +0200641 return IRQ_HANDLED;
642}
643
644u32 r100_get_vblank_counter(struct radeon_device *rdev, int crtc)
645{
646 if (crtc == 0)
647 return RREG32(RADEON_CRTC_CRNT_FRAME);
648 else
649 return RREG32(RADEON_CRTC2_CRNT_FRAME);
650}
651
Pauli Nieminen9e5b2af2010-02-04 19:20:53 +0200652/* Who ever call radeon_fence_emit should call ring_lock and ask
653 * for enough space (today caller are ib schedule and buffer move) */
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200654void r100_fence_ring_emit(struct radeon_device *rdev,
655 struct radeon_fence *fence)
656{
Pauli Nieminen9e5b2af2010-02-04 19:20:53 +0200657 /* We have to make sure that caches are flushed before
658 * CPU might read something from VRAM. */
659 radeon_ring_write(rdev, PACKET0(RADEON_RB3D_DSTCACHE_CTLSTAT, 0));
660 radeon_ring_write(rdev, RADEON_RB3D_DC_FLUSH_ALL);
661 radeon_ring_write(rdev, PACKET0(RADEON_RB3D_ZCACHE_CTLSTAT, 0));
662 radeon_ring_write(rdev, RADEON_RB3D_ZC_FLUSH_ALL);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200663 /* Wait until IDLE & CLEAN */
Alex Deucher4612dc92010-02-05 01:58:28 -0500664 radeon_ring_write(rdev, PACKET0(RADEON_WAIT_UNTIL, 0));
665 radeon_ring_write(rdev, RADEON_WAIT_2D_IDLECLEAN | RADEON_WAIT_3D_IDLECLEAN);
Jerome Glissecafe6602010-01-07 12:39:21 +0100666 radeon_ring_write(rdev, PACKET0(RADEON_HOST_PATH_CNTL, 0));
667 radeon_ring_write(rdev, rdev->config.r100.hdp_cntl |
668 RADEON_HDP_READ_BUFFER_INVALIDATE);
669 radeon_ring_write(rdev, PACKET0(RADEON_HOST_PATH_CNTL, 0));
670 radeon_ring_write(rdev, rdev->config.r100.hdp_cntl);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200671 /* Emit fence sequence & fire IRQ */
672 radeon_ring_write(rdev, PACKET0(rdev->fence_drv.scratch_reg, 0));
673 radeon_ring_write(rdev, fence->seq);
674 radeon_ring_write(rdev, PACKET0(RADEON_GEN_INT_STATUS, 0));
675 radeon_ring_write(rdev, RADEON_SW_INT_FIRE);
676}
677
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200678int r100_wb_init(struct radeon_device *rdev)
679{
680 int r;
681
682 if (rdev->wb.wb_obj == NULL) {
Jerome Glisse4c788672009-11-20 14:29:23 +0100683 r = radeon_bo_create(rdev, NULL, RADEON_GPU_PAGE_SIZE, true,
684 RADEON_GEM_DOMAIN_GTT,
685 &rdev->wb.wb_obj);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200686 if (r) {
Jerome Glisse4c788672009-11-20 14:29:23 +0100687 dev_err(rdev->dev, "(%d) create WB buffer failed\n", r);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200688 return r;
689 }
Jerome Glisse4c788672009-11-20 14:29:23 +0100690 r = radeon_bo_reserve(rdev->wb.wb_obj, false);
691 if (unlikely(r != 0))
692 return r;
693 r = radeon_bo_pin(rdev->wb.wb_obj, RADEON_GEM_DOMAIN_GTT,
694 &rdev->wb.gpu_addr);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200695 if (r) {
Jerome Glisse4c788672009-11-20 14:29:23 +0100696 dev_err(rdev->dev, "(%d) pin WB buffer failed\n", r);
697 radeon_bo_unreserve(rdev->wb.wb_obj);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200698 return r;
699 }
Jerome Glisse4c788672009-11-20 14:29:23 +0100700 r = radeon_bo_kmap(rdev->wb.wb_obj, (void **)&rdev->wb.wb);
701 radeon_bo_unreserve(rdev->wb.wb_obj);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200702 if (r) {
Jerome Glisse4c788672009-11-20 14:29:23 +0100703 dev_err(rdev->dev, "(%d) map WB buffer failed\n", r);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200704 return r;
705 }
706 }
Jerome Glisse9f022dd2009-09-11 15:35:22 +0200707 WREG32(R_000774_SCRATCH_ADDR, rdev->wb.gpu_addr);
708 WREG32(R_00070C_CP_RB_RPTR_ADDR,
709 S_00070C_RB_RPTR_ADDR((rdev->wb.gpu_addr + 1024) >> 2));
710 WREG32(R_000770_SCRATCH_UMSK, 0xff);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200711 return 0;
712}
713
Jerome Glisse9f022dd2009-09-11 15:35:22 +0200714void r100_wb_disable(struct radeon_device *rdev)
715{
716 WREG32(R_000770_SCRATCH_UMSK, 0);
717}
718
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200719void r100_wb_fini(struct radeon_device *rdev)
720{
Jerome Glisse4c788672009-11-20 14:29:23 +0100721 int r;
722
Jerome Glisse9f022dd2009-09-11 15:35:22 +0200723 r100_wb_disable(rdev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200724 if (rdev->wb.wb_obj) {
Jerome Glisse4c788672009-11-20 14:29:23 +0100725 r = radeon_bo_reserve(rdev->wb.wb_obj, false);
726 if (unlikely(r != 0)) {
727 dev_err(rdev->dev, "(%d) can't finish WB\n", r);
728 return;
729 }
730 radeon_bo_kunmap(rdev->wb.wb_obj);
731 radeon_bo_unpin(rdev->wb.wb_obj);
732 radeon_bo_unreserve(rdev->wb.wb_obj);
733 radeon_bo_unref(&rdev->wb.wb_obj);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200734 rdev->wb.wb = NULL;
735 rdev->wb.wb_obj = NULL;
736 }
737}
738
739int r100_copy_blit(struct radeon_device *rdev,
740 uint64_t src_offset,
741 uint64_t dst_offset,
742 unsigned num_pages,
743 struct radeon_fence *fence)
744{
745 uint32_t cur_pages;
746 uint32_t stride_bytes = PAGE_SIZE;
747 uint32_t pitch;
748 uint32_t stride_pixels;
749 unsigned ndw;
750 int num_loops;
751 int r = 0;
752
753 /* radeon limited to 16k stride */
754 stride_bytes &= 0x3fff;
755 /* radeon pitch is /64 */
756 pitch = stride_bytes / 64;
757 stride_pixels = stride_bytes / 4;
758 num_loops = DIV_ROUND_UP(num_pages, 8191);
759
760 /* Ask for enough room for blit + flush + fence */
761 ndw = 64 + (10 * num_loops);
762 r = radeon_ring_lock(rdev, ndw);
763 if (r) {
764 DRM_ERROR("radeon: moving bo (%d) asking for %u dw.\n", r, ndw);
765 return -EINVAL;
766 }
767 while (num_pages > 0) {
768 cur_pages = num_pages;
769 if (cur_pages > 8191) {
770 cur_pages = 8191;
771 }
772 num_pages -= cur_pages;
773
774 /* pages are in Y direction - height
775 page width in X direction - width */
776 radeon_ring_write(rdev, PACKET3(PACKET3_BITBLT_MULTI, 8));
777 radeon_ring_write(rdev,
778 RADEON_GMC_SRC_PITCH_OFFSET_CNTL |
779 RADEON_GMC_DST_PITCH_OFFSET_CNTL |
780 RADEON_GMC_SRC_CLIPPING |
781 RADEON_GMC_DST_CLIPPING |
782 RADEON_GMC_BRUSH_NONE |
783 (RADEON_COLOR_FORMAT_ARGB8888 << 8) |
784 RADEON_GMC_SRC_DATATYPE_COLOR |
785 RADEON_ROP3_S |
786 RADEON_DP_SRC_SOURCE_MEMORY |
787 RADEON_GMC_CLR_CMP_CNTL_DIS |
788 RADEON_GMC_WR_MSK_DIS);
789 radeon_ring_write(rdev, (pitch << 22) | (src_offset >> 10));
790 radeon_ring_write(rdev, (pitch << 22) | (dst_offset >> 10));
791 radeon_ring_write(rdev, (0x1fff) | (0x1fff << 16));
792 radeon_ring_write(rdev, 0);
793 radeon_ring_write(rdev, (0x1fff) | (0x1fff << 16));
794 radeon_ring_write(rdev, num_pages);
795 radeon_ring_write(rdev, num_pages);
796 radeon_ring_write(rdev, cur_pages | (stride_pixels << 16));
797 }
798 radeon_ring_write(rdev, PACKET0(RADEON_DSTCACHE_CTLSTAT, 0));
799 radeon_ring_write(rdev, RADEON_RB2D_DC_FLUSH_ALL);
800 radeon_ring_write(rdev, PACKET0(RADEON_WAIT_UNTIL, 0));
801 radeon_ring_write(rdev,
802 RADEON_WAIT_2D_IDLECLEAN |
803 RADEON_WAIT_HOST_IDLECLEAN |
804 RADEON_WAIT_DMA_GUI_IDLE);
805 if (fence) {
806 r = radeon_fence_emit(rdev, fence);
807 }
808 radeon_ring_unlock_commit(rdev);
809 return r;
810}
811
Jerome Glisse45600232009-09-09 22:23:45 +0200812static int r100_cp_wait_for_idle(struct radeon_device *rdev)
813{
814 unsigned i;
815 u32 tmp;
816
817 for (i = 0; i < rdev->usec_timeout; i++) {
818 tmp = RREG32(R_000E40_RBBM_STATUS);
819 if (!G_000E40_CP_CMDSTRM_BUSY(tmp)) {
820 return 0;
821 }
822 udelay(1);
823 }
824 return -1;
825}
826
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200827void r100_ring_start(struct radeon_device *rdev)
828{
829 int r;
830
831 r = radeon_ring_lock(rdev, 2);
832 if (r) {
833 return;
834 }
835 radeon_ring_write(rdev, PACKET0(RADEON_ISYNC_CNTL, 0));
836 radeon_ring_write(rdev,
837 RADEON_ISYNC_ANY2D_IDLE3D |
838 RADEON_ISYNC_ANY3D_IDLE2D |
839 RADEON_ISYNC_WAIT_IDLEGUI |
840 RADEON_ISYNC_CPSCRATCH_IDLEGUI);
841 radeon_ring_unlock_commit(rdev);
842}
843
Ben Hutchings70967ab2009-08-29 14:53:51 +0100844
845/* Load the microcode for the CP */
846static int r100_cp_init_microcode(struct radeon_device *rdev)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200847{
Ben Hutchings70967ab2009-08-29 14:53:51 +0100848 struct platform_device *pdev;
849 const char *fw_name = NULL;
850 int err;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200851
Ben Hutchings70967ab2009-08-29 14:53:51 +0100852 DRM_DEBUG("\n");
853
854 pdev = platform_device_register_simple("radeon_cp", 0, NULL, 0);
855 err = IS_ERR(pdev);
856 if (err) {
857 printk(KERN_ERR "radeon_cp: Failed to register firmware\n");
858 return -EINVAL;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200859 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200860 if ((rdev->family == CHIP_R100) || (rdev->family == CHIP_RV100) ||
861 (rdev->family == CHIP_RV200) || (rdev->family == CHIP_RS100) ||
862 (rdev->family == CHIP_RS200)) {
863 DRM_INFO("Loading R100 Microcode\n");
Ben Hutchings70967ab2009-08-29 14:53:51 +0100864 fw_name = FIRMWARE_R100;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200865 } else if ((rdev->family == CHIP_R200) ||
866 (rdev->family == CHIP_RV250) ||
867 (rdev->family == CHIP_RV280) ||
868 (rdev->family == CHIP_RS300)) {
869 DRM_INFO("Loading R200 Microcode\n");
Ben Hutchings70967ab2009-08-29 14:53:51 +0100870 fw_name = FIRMWARE_R200;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200871 } else if ((rdev->family == CHIP_R300) ||
872 (rdev->family == CHIP_R350) ||
873 (rdev->family == CHIP_RV350) ||
874 (rdev->family == CHIP_RV380) ||
875 (rdev->family == CHIP_RS400) ||
876 (rdev->family == CHIP_RS480)) {
877 DRM_INFO("Loading R300 Microcode\n");
Ben Hutchings70967ab2009-08-29 14:53:51 +0100878 fw_name = FIRMWARE_R300;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200879 } else if ((rdev->family == CHIP_R420) ||
880 (rdev->family == CHIP_R423) ||
881 (rdev->family == CHIP_RV410)) {
882 DRM_INFO("Loading R400 Microcode\n");
Ben Hutchings70967ab2009-08-29 14:53:51 +0100883 fw_name = FIRMWARE_R420;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200884 } else if ((rdev->family == CHIP_RS690) ||
885 (rdev->family == CHIP_RS740)) {
886 DRM_INFO("Loading RS690/RS740 Microcode\n");
Ben Hutchings70967ab2009-08-29 14:53:51 +0100887 fw_name = FIRMWARE_RS690;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200888 } else if (rdev->family == CHIP_RS600) {
889 DRM_INFO("Loading RS600 Microcode\n");
Ben Hutchings70967ab2009-08-29 14:53:51 +0100890 fw_name = FIRMWARE_RS600;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200891 } else if ((rdev->family == CHIP_RV515) ||
892 (rdev->family == CHIP_R520) ||
893 (rdev->family == CHIP_RV530) ||
894 (rdev->family == CHIP_R580) ||
895 (rdev->family == CHIP_RV560) ||
896 (rdev->family == CHIP_RV570)) {
897 DRM_INFO("Loading R500 Microcode\n");
Ben Hutchings70967ab2009-08-29 14:53:51 +0100898 fw_name = FIRMWARE_R520;
899 }
900
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000901 err = request_firmware(&rdev->me_fw, fw_name, &pdev->dev);
Ben Hutchings70967ab2009-08-29 14:53:51 +0100902 platform_device_unregister(pdev);
903 if (err) {
904 printk(KERN_ERR "radeon_cp: Failed to load firmware \"%s\"\n",
905 fw_name);
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000906 } else if (rdev->me_fw->size % 8) {
Ben Hutchings70967ab2009-08-29 14:53:51 +0100907 printk(KERN_ERR
908 "radeon_cp: Bogus length %zu in firmware \"%s\"\n",
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000909 rdev->me_fw->size, fw_name);
Ben Hutchings70967ab2009-08-29 14:53:51 +0100910 err = -EINVAL;
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000911 release_firmware(rdev->me_fw);
912 rdev->me_fw = NULL;
Ben Hutchings70967ab2009-08-29 14:53:51 +0100913 }
914 return err;
915}
Jerome Glissed4550902009-10-01 10:12:06 +0200916
Ben Hutchings70967ab2009-08-29 14:53:51 +0100917static void r100_cp_load_microcode(struct radeon_device *rdev)
918{
919 const __be32 *fw_data;
920 int i, size;
921
922 if (r100_gui_wait_for_idle(rdev)) {
923 printk(KERN_WARNING "Failed to wait GUI idle while "
924 "programming pipes. Bad things might happen.\n");
925 }
926
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000927 if (rdev->me_fw) {
928 size = rdev->me_fw->size / 4;
929 fw_data = (const __be32 *)&rdev->me_fw->data[0];
Ben Hutchings70967ab2009-08-29 14:53:51 +0100930 WREG32(RADEON_CP_ME_RAM_ADDR, 0);
931 for (i = 0; i < size; i += 2) {
932 WREG32(RADEON_CP_ME_RAM_DATAH,
933 be32_to_cpup(&fw_data[i]));
934 WREG32(RADEON_CP_ME_RAM_DATAL,
935 be32_to_cpup(&fw_data[i + 1]));
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200936 }
937 }
938}
939
940int r100_cp_init(struct radeon_device *rdev, unsigned ring_size)
941{
942 unsigned rb_bufsz;
943 unsigned rb_blksz;
944 unsigned max_fetch;
945 unsigned pre_write_timer;
946 unsigned pre_write_limit;
947 unsigned indirect2_start;
948 unsigned indirect1_start;
949 uint32_t tmp;
950 int r;
951
952 if (r100_debugfs_cp_init(rdev)) {
953 DRM_ERROR("Failed to register debugfs file for CP !\n");
954 }
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000955 if (!rdev->me_fw) {
Ben Hutchings70967ab2009-08-29 14:53:51 +0100956 r = r100_cp_init_microcode(rdev);
957 if (r) {
958 DRM_ERROR("Failed to load firmware!\n");
959 return r;
960 }
961 }
962
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200963 /* Align ring size */
964 rb_bufsz = drm_order(ring_size / 8);
965 ring_size = (1 << (rb_bufsz + 1)) * 4;
966 r100_cp_load_microcode(rdev);
967 r = radeon_ring_init(rdev, ring_size);
968 if (r) {
969 return r;
970 }
971 /* Each time the cp read 1024 bytes (16 dword/quadword) update
972 * the rptr copy in system ram */
973 rb_blksz = 9;
974 /* cp will read 128bytes at a time (4 dwords) */
975 max_fetch = 1;
976 rdev->cp.align_mask = 16 - 1;
977 /* Write to CP_RB_WPTR will be delayed for pre_write_timer clocks */
978 pre_write_timer = 64;
979 /* Force CP_RB_WPTR write if written more than one time before the
980 * delay expire
981 */
982 pre_write_limit = 0;
983 /* Setup the cp cache like this (cache size is 96 dwords) :
984 * RING 0 to 15
985 * INDIRECT1 16 to 79
986 * INDIRECT2 80 to 95
987 * So ring cache size is 16dwords (> (2 * max_fetch = 2 * 4dwords))
988 * indirect1 cache size is 64dwords (> (2 * max_fetch = 2 * 4dwords))
989 * indirect2 cache size is 16dwords (> (2 * max_fetch = 2 * 4dwords))
990 * Idea being that most of the gpu cmd will be through indirect1 buffer
991 * so it gets the bigger cache.
992 */
993 indirect2_start = 80;
994 indirect1_start = 16;
995 /* cp setup */
996 WREG32(0x718, pre_write_timer | (pre_write_limit << 28));
Alex Deucherd6f28932009-11-02 16:01:27 -0500997 tmp = (REG_SET(RADEON_RB_BUFSZ, rb_bufsz) |
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200998 REG_SET(RADEON_RB_BLKSZ, rb_blksz) |
999 REG_SET(RADEON_MAX_FETCH, max_fetch) |
1000 RADEON_RB_NO_UPDATE);
Alex Deucherd6f28932009-11-02 16:01:27 -05001001#ifdef __BIG_ENDIAN
1002 tmp |= RADEON_BUF_SWAP_32BIT;
1003#endif
1004 WREG32(RADEON_CP_RB_CNTL, tmp);
1005
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001006 /* Set ring address */
1007 DRM_INFO("radeon: ring at 0x%016lX\n", (unsigned long)rdev->cp.gpu_addr);
1008 WREG32(RADEON_CP_RB_BASE, rdev->cp.gpu_addr);
1009 /* Force read & write ptr to 0 */
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001010 WREG32(RADEON_CP_RB_CNTL, tmp | RADEON_RB_RPTR_WR_ENA);
1011 WREG32(RADEON_CP_RB_RPTR_WR, 0);
1012 WREG32(RADEON_CP_RB_WPTR, 0);
1013 WREG32(RADEON_CP_RB_CNTL, tmp);
1014 udelay(10);
1015 rdev->cp.rptr = RREG32(RADEON_CP_RB_RPTR);
1016 rdev->cp.wptr = RREG32(RADEON_CP_RB_WPTR);
Dave Airlie9e5786b2010-03-31 13:38:56 +10001017 /* protect against crazy HW on resume */
1018 rdev->cp.wptr &= rdev->cp.ptr_mask;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001019 /* Set cp mode to bus mastering & enable cp*/
1020 WREG32(RADEON_CP_CSQ_MODE,
1021 REG_SET(RADEON_INDIRECT2_START, indirect2_start) |
1022 REG_SET(RADEON_INDIRECT1_START, indirect1_start));
1023 WREG32(0x718, 0);
1024 WREG32(0x744, 0x00004D4D);
1025 WREG32(RADEON_CP_CSQ_CNTL, RADEON_CSQ_PRIBM_INDBM);
1026 radeon_ring_start(rdev);
1027 r = radeon_ring_test(rdev);
1028 if (r) {
1029 DRM_ERROR("radeon: cp isn't working (%d).\n", r);
1030 return r;
1031 }
1032 rdev->cp.ready = true;
1033 return 0;
1034}
1035
1036void r100_cp_fini(struct radeon_device *rdev)
1037{
Jerome Glisse45600232009-09-09 22:23:45 +02001038 if (r100_cp_wait_for_idle(rdev)) {
1039 DRM_ERROR("Wait for CP idle timeout, shutting down CP.\n");
1040 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001041 /* Disable ring */
Jerome Glissea18d7ea2009-09-09 22:23:27 +02001042 r100_cp_disable(rdev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001043 radeon_ring_fini(rdev);
1044 DRM_INFO("radeon: cp finalized\n");
1045}
1046
1047void r100_cp_disable(struct radeon_device *rdev)
1048{
1049 /* Disable ring */
1050 rdev->cp.ready = false;
1051 WREG32(RADEON_CP_CSQ_MODE, 0);
1052 WREG32(RADEON_CP_CSQ_CNTL, 0);
1053 if (r100_gui_wait_for_idle(rdev)) {
1054 printk(KERN_WARNING "Failed to wait GUI idle while "
1055 "programming pipes. Bad things might happen.\n");
1056 }
1057}
1058
Jerome Glisse3ce0a232009-09-08 10:10:24 +10001059void r100_cp_commit(struct radeon_device *rdev)
1060{
1061 WREG32(RADEON_CP_RB_WPTR, rdev->cp.wptr);
1062 (void)RREG32(RADEON_CP_RB_WPTR);
1063}
1064
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001065
1066/*
1067 * CS functions
1068 */
1069int r100_cs_parse_packet0(struct radeon_cs_parser *p,
1070 struct radeon_cs_packet *pkt,
Jerome Glisse068a1172009-06-17 13:28:30 +02001071 const unsigned *auth, unsigned n,
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001072 radeon_packet0_check_t check)
1073{
1074 unsigned reg;
1075 unsigned i, j, m;
1076 unsigned idx;
1077 int r;
1078
1079 idx = pkt->idx + 1;
1080 reg = pkt->reg;
Jerome Glisse068a1172009-06-17 13:28:30 +02001081 /* Check that register fall into register range
1082 * determined by the number of entry (n) in the
1083 * safe register bitmap.
1084 */
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001085 if (pkt->one_reg_wr) {
1086 if ((reg >> 7) > n) {
1087 return -EINVAL;
1088 }
1089 } else {
1090 if (((reg + (pkt->count << 2)) >> 7) > n) {
1091 return -EINVAL;
1092 }
1093 }
1094 for (i = 0; i <= pkt->count; i++, idx++) {
1095 j = (reg >> 7);
1096 m = 1 << ((reg >> 2) & 31);
1097 if (auth[j] & m) {
1098 r = check(p, pkt, idx, reg);
1099 if (r) {
1100 return r;
1101 }
1102 }
1103 if (pkt->one_reg_wr) {
1104 if (!(auth[j] & m)) {
1105 break;
1106 }
1107 } else {
1108 reg += 4;
1109 }
1110 }
1111 return 0;
1112}
1113
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001114void r100_cs_dump_packet(struct radeon_cs_parser *p,
1115 struct radeon_cs_packet *pkt)
1116{
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001117 volatile uint32_t *ib;
1118 unsigned i;
1119 unsigned idx;
1120
1121 ib = p->ib->ptr;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001122 idx = pkt->idx;
1123 for (i = 0; i <= (pkt->count + 1); i++, idx++) {
1124 DRM_INFO("ib[%d]=0x%08X\n", idx, ib[idx]);
1125 }
1126}
1127
1128/**
1129 * r100_cs_packet_parse() - parse cp packet and point ib index to next packet
1130 * @parser: parser structure holding parsing context.
1131 * @pkt: where to store packet informations
1132 *
1133 * Assume that chunk_ib_index is properly set. Will return -EINVAL
1134 * if packet is bigger than remaining ib size. or if packets is unknown.
1135 **/
1136int r100_cs_packet_parse(struct radeon_cs_parser *p,
1137 struct radeon_cs_packet *pkt,
1138 unsigned idx)
1139{
1140 struct radeon_cs_chunk *ib_chunk = &p->chunks[p->chunk_ib_idx];
Roel Kluinfa992392009-08-03 14:20:32 +02001141 uint32_t header;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001142
1143 if (idx >= ib_chunk->length_dw) {
1144 DRM_ERROR("Can not parse packet at %d after CS end %d !\n",
1145 idx, ib_chunk->length_dw);
1146 return -EINVAL;
1147 }
Dave Airlie513bcb42009-09-23 16:56:27 +10001148 header = radeon_get_ib_value(p, idx);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001149 pkt->idx = idx;
1150 pkt->type = CP_PACKET_GET_TYPE(header);
1151 pkt->count = CP_PACKET_GET_COUNT(header);
1152 switch (pkt->type) {
1153 case PACKET_TYPE0:
1154 pkt->reg = CP_PACKET0_GET_REG(header);
1155 pkt->one_reg_wr = CP_PACKET0_GET_ONE_REG_WR(header);
1156 break;
1157 case PACKET_TYPE3:
1158 pkt->opcode = CP_PACKET3_GET_OPCODE(header);
1159 break;
1160 case PACKET_TYPE2:
1161 pkt->count = -1;
1162 break;
1163 default:
1164 DRM_ERROR("Unknown packet type %d at %d !\n", pkt->type, idx);
1165 return -EINVAL;
1166 }
1167 if ((pkt->count + 1 + pkt->idx) >= ib_chunk->length_dw) {
1168 DRM_ERROR("Packet (%d:%d:%d) end after CS buffer (%d) !\n",
1169 pkt->idx, pkt->type, pkt->count, ib_chunk->length_dw);
1170 return -EINVAL;
1171 }
1172 return 0;
1173}
1174
1175/**
Dave Airlie531369e2009-06-29 11:21:25 +10001176 * r100_cs_packet_next_vline() - parse userspace VLINE packet
1177 * @parser: parser structure holding parsing context.
1178 *
1179 * Userspace sends a special sequence for VLINE waits.
1180 * PACKET0 - VLINE_START_END + value
1181 * PACKET0 - WAIT_UNTIL +_value
1182 * RELOC (P3) - crtc_id in reloc.
1183 *
1184 * This function parses this and relocates the VLINE START END
1185 * and WAIT UNTIL packets to the correct crtc.
1186 * It also detects a switched off crtc and nulls out the
1187 * wait in that case.
1188 */
1189int r100_cs_packet_parse_vline(struct radeon_cs_parser *p)
1190{
Dave Airlie531369e2009-06-29 11:21:25 +10001191 struct drm_mode_object *obj;
1192 struct drm_crtc *crtc;
1193 struct radeon_crtc *radeon_crtc;
1194 struct radeon_cs_packet p3reloc, waitreloc;
1195 int crtc_id;
1196 int r;
1197 uint32_t header, h_idx, reg;
Dave Airlie513bcb42009-09-23 16:56:27 +10001198 volatile uint32_t *ib;
Dave Airlie531369e2009-06-29 11:21:25 +10001199
Dave Airlie513bcb42009-09-23 16:56:27 +10001200 ib = p->ib->ptr;
Dave Airlie531369e2009-06-29 11:21:25 +10001201
1202 /* parse the wait until */
1203 r = r100_cs_packet_parse(p, &waitreloc, p->idx);
1204 if (r)
1205 return r;
1206
1207 /* check its a wait until and only 1 count */
1208 if (waitreloc.reg != RADEON_WAIT_UNTIL ||
1209 waitreloc.count != 0) {
1210 DRM_ERROR("vline wait had illegal wait until segment\n");
1211 r = -EINVAL;
1212 return r;
1213 }
1214
Dave Airlie513bcb42009-09-23 16:56:27 +10001215 if (radeon_get_ib_value(p, waitreloc.idx + 1) != RADEON_WAIT_CRTC_VLINE) {
Dave Airlie531369e2009-06-29 11:21:25 +10001216 DRM_ERROR("vline wait had illegal wait until\n");
1217 r = -EINVAL;
1218 return r;
1219 }
1220
1221 /* jump over the NOP */
Alex Deucher90ebd062009-09-25 16:39:24 -04001222 r = r100_cs_packet_parse(p, &p3reloc, p->idx + waitreloc.count + 2);
Dave Airlie531369e2009-06-29 11:21:25 +10001223 if (r)
1224 return r;
1225
1226 h_idx = p->idx - 2;
Alex Deucher90ebd062009-09-25 16:39:24 -04001227 p->idx += waitreloc.count + 2;
1228 p->idx += p3reloc.count + 2;
Dave Airlie531369e2009-06-29 11:21:25 +10001229
Dave Airlie513bcb42009-09-23 16:56:27 +10001230 header = radeon_get_ib_value(p, h_idx);
1231 crtc_id = radeon_get_ib_value(p, h_idx + 5);
Dave Airlied4ac6a02009-10-08 11:32:49 +10001232 reg = CP_PACKET0_GET_REG(header);
Dave Airlie531369e2009-06-29 11:21:25 +10001233 mutex_lock(&p->rdev->ddev->mode_config.mutex);
1234 obj = drm_mode_object_find(p->rdev->ddev, crtc_id, DRM_MODE_OBJECT_CRTC);
1235 if (!obj) {
1236 DRM_ERROR("cannot find crtc %d\n", crtc_id);
1237 r = -EINVAL;
1238 goto out;
1239 }
1240 crtc = obj_to_crtc(obj);
1241 radeon_crtc = to_radeon_crtc(crtc);
1242 crtc_id = radeon_crtc->crtc_id;
1243
1244 if (!crtc->enabled) {
1245 /* if the CRTC isn't enabled - we need to nop out the wait until */
Dave Airlie513bcb42009-09-23 16:56:27 +10001246 ib[h_idx + 2] = PACKET2(0);
1247 ib[h_idx + 3] = PACKET2(0);
Dave Airlie531369e2009-06-29 11:21:25 +10001248 } else if (crtc_id == 1) {
1249 switch (reg) {
1250 case AVIVO_D1MODE_VLINE_START_END:
Alex Deucher90ebd062009-09-25 16:39:24 -04001251 header &= ~R300_CP_PACKET0_REG_MASK;
Dave Airlie531369e2009-06-29 11:21:25 +10001252 header |= AVIVO_D2MODE_VLINE_START_END >> 2;
1253 break;
1254 case RADEON_CRTC_GUI_TRIG_VLINE:
Alex Deucher90ebd062009-09-25 16:39:24 -04001255 header &= ~R300_CP_PACKET0_REG_MASK;
Dave Airlie531369e2009-06-29 11:21:25 +10001256 header |= RADEON_CRTC2_GUI_TRIG_VLINE >> 2;
1257 break;
1258 default:
1259 DRM_ERROR("unknown crtc reloc\n");
1260 r = -EINVAL;
1261 goto out;
1262 }
Dave Airlie513bcb42009-09-23 16:56:27 +10001263 ib[h_idx] = header;
1264 ib[h_idx + 3] |= RADEON_ENG_DISPLAY_SELECT_CRTC1;
Dave Airlie531369e2009-06-29 11:21:25 +10001265 }
1266out:
1267 mutex_unlock(&p->rdev->ddev->mode_config.mutex);
1268 return r;
1269}
1270
1271/**
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001272 * r100_cs_packet_next_reloc() - parse next packet which should be reloc packet3
1273 * @parser: parser structure holding parsing context.
1274 * @data: pointer to relocation data
1275 * @offset_start: starting offset
1276 * @offset_mask: offset mask (to align start offset on)
1277 * @reloc: reloc informations
1278 *
1279 * Check next packet is relocation packet3, do bo validation and compute
1280 * GPU offset using the provided start.
1281 **/
1282int r100_cs_packet_next_reloc(struct radeon_cs_parser *p,
1283 struct radeon_cs_reloc **cs_reloc)
1284{
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001285 struct radeon_cs_chunk *relocs_chunk;
1286 struct radeon_cs_packet p3reloc;
1287 unsigned idx;
1288 int r;
1289
1290 if (p->chunk_relocs_idx == -1) {
1291 DRM_ERROR("No relocation chunk !\n");
1292 return -EINVAL;
1293 }
1294 *cs_reloc = NULL;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001295 relocs_chunk = &p->chunks[p->chunk_relocs_idx];
1296 r = r100_cs_packet_parse(p, &p3reloc, p->idx);
1297 if (r) {
1298 return r;
1299 }
1300 p->idx += p3reloc.count + 2;
1301 if (p3reloc.type != PACKET_TYPE3 || p3reloc.opcode != PACKET3_NOP) {
1302 DRM_ERROR("No packet3 for relocation for packet at %d.\n",
1303 p3reloc.idx);
1304 r100_cs_dump_packet(p, &p3reloc);
1305 return -EINVAL;
1306 }
Dave Airlie513bcb42009-09-23 16:56:27 +10001307 idx = radeon_get_ib_value(p, p3reloc.idx + 1);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001308 if (idx >= relocs_chunk->length_dw) {
1309 DRM_ERROR("Relocs at %d after relocations chunk end %d !\n",
1310 idx, relocs_chunk->length_dw);
1311 r100_cs_dump_packet(p, &p3reloc);
1312 return -EINVAL;
1313 }
1314 /* FIXME: we assume reloc size is 4 dwords */
1315 *cs_reloc = p->relocs_ptr[(idx / 4)];
1316 return 0;
1317}
1318
Dave Airlie551ebd82009-09-01 15:25:57 +10001319static int r100_get_vtx_size(uint32_t vtx_fmt)
1320{
1321 int vtx_size;
1322 vtx_size = 2;
1323 /* ordered according to bits in spec */
1324 if (vtx_fmt & RADEON_SE_VTX_FMT_W0)
1325 vtx_size++;
1326 if (vtx_fmt & RADEON_SE_VTX_FMT_FPCOLOR)
1327 vtx_size += 3;
1328 if (vtx_fmt & RADEON_SE_VTX_FMT_FPALPHA)
1329 vtx_size++;
1330 if (vtx_fmt & RADEON_SE_VTX_FMT_PKCOLOR)
1331 vtx_size++;
1332 if (vtx_fmt & RADEON_SE_VTX_FMT_FPSPEC)
1333 vtx_size += 3;
1334 if (vtx_fmt & RADEON_SE_VTX_FMT_FPFOG)
1335 vtx_size++;
1336 if (vtx_fmt & RADEON_SE_VTX_FMT_PKSPEC)
1337 vtx_size++;
1338 if (vtx_fmt & RADEON_SE_VTX_FMT_ST0)
1339 vtx_size += 2;
1340 if (vtx_fmt & RADEON_SE_VTX_FMT_ST1)
1341 vtx_size += 2;
1342 if (vtx_fmt & RADEON_SE_VTX_FMT_Q1)
1343 vtx_size++;
1344 if (vtx_fmt & RADEON_SE_VTX_FMT_ST2)
1345 vtx_size += 2;
1346 if (vtx_fmt & RADEON_SE_VTX_FMT_Q2)
1347 vtx_size++;
1348 if (vtx_fmt & RADEON_SE_VTX_FMT_ST3)
1349 vtx_size += 2;
1350 if (vtx_fmt & RADEON_SE_VTX_FMT_Q3)
1351 vtx_size++;
1352 if (vtx_fmt & RADEON_SE_VTX_FMT_Q0)
1353 vtx_size++;
1354 /* blend weight */
1355 if (vtx_fmt & (0x7 << 15))
1356 vtx_size += (vtx_fmt >> 15) & 0x7;
1357 if (vtx_fmt & RADEON_SE_VTX_FMT_N0)
1358 vtx_size += 3;
1359 if (vtx_fmt & RADEON_SE_VTX_FMT_XY1)
1360 vtx_size += 2;
1361 if (vtx_fmt & RADEON_SE_VTX_FMT_Z1)
1362 vtx_size++;
1363 if (vtx_fmt & RADEON_SE_VTX_FMT_W1)
1364 vtx_size++;
1365 if (vtx_fmt & RADEON_SE_VTX_FMT_N1)
1366 vtx_size++;
1367 if (vtx_fmt & RADEON_SE_VTX_FMT_Z)
1368 vtx_size++;
1369 return vtx_size;
1370}
1371
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001372static int r100_packet0_check(struct radeon_cs_parser *p,
Dave Airlie551ebd82009-09-01 15:25:57 +10001373 struct radeon_cs_packet *pkt,
1374 unsigned idx, unsigned reg)
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001375{
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001376 struct radeon_cs_reloc *reloc;
Dave Airlie551ebd82009-09-01 15:25:57 +10001377 struct r100_cs_track *track;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001378 volatile uint32_t *ib;
1379 uint32_t tmp;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001380 int r;
Dave Airlie551ebd82009-09-01 15:25:57 +10001381 int i, face;
Dave Airliee024e112009-06-24 09:48:08 +10001382 u32 tile_flags = 0;
Dave Airlie513bcb42009-09-23 16:56:27 +10001383 u32 idx_value;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001384
1385 ib = p->ib->ptr;
Dave Airlie551ebd82009-09-01 15:25:57 +10001386 track = (struct r100_cs_track *)p->track;
1387
Dave Airlie513bcb42009-09-23 16:56:27 +10001388 idx_value = radeon_get_ib_value(p, idx);
1389
Dave Airlie551ebd82009-09-01 15:25:57 +10001390 switch (reg) {
1391 case RADEON_CRTC_GUI_TRIG_VLINE:
1392 r = r100_cs_packet_parse_vline(p);
1393 if (r) {
1394 DRM_ERROR("No reloc for ib[%d]=0x%04X\n",
1395 idx, reg);
1396 r100_cs_dump_packet(p, pkt);
1397 return r;
1398 }
1399 break;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001400 /* FIXME: only allow PACKET3 blit? easier to check for out of
1401 * range access */
Dave Airlie551ebd82009-09-01 15:25:57 +10001402 case RADEON_DST_PITCH_OFFSET:
1403 case RADEON_SRC_PITCH_OFFSET:
1404 r = r100_reloc_pitch_offset(p, pkt, idx, reg);
1405 if (r)
1406 return r;
1407 break;
1408 case RADEON_RB3D_DEPTHOFFSET:
1409 r = r100_cs_packet_next_reloc(p, &reloc);
1410 if (r) {
1411 DRM_ERROR("No reloc for ib[%d]=0x%04X\n",
1412 idx, reg);
1413 r100_cs_dump_packet(p, pkt);
1414 return r;
1415 }
1416 track->zb.robj = reloc->robj;
Dave Airlie513bcb42009-09-23 16:56:27 +10001417 track->zb.offset = idx_value;
1418 ib[idx] = idx_value + ((u32)reloc->lobj.gpu_offset);
Dave Airlie551ebd82009-09-01 15:25:57 +10001419 break;
1420 case RADEON_RB3D_COLOROFFSET:
1421 r = r100_cs_packet_next_reloc(p, &reloc);
1422 if (r) {
1423 DRM_ERROR("No reloc for ib[%d]=0x%04X\n",
1424 idx, reg);
1425 r100_cs_dump_packet(p, pkt);
1426 return r;
1427 }
1428 track->cb[0].robj = reloc->robj;
Dave Airlie513bcb42009-09-23 16:56:27 +10001429 track->cb[0].offset = idx_value;
1430 ib[idx] = idx_value + ((u32)reloc->lobj.gpu_offset);
Dave Airlie551ebd82009-09-01 15:25:57 +10001431 break;
1432 case RADEON_PP_TXOFFSET_0:
1433 case RADEON_PP_TXOFFSET_1:
1434 case RADEON_PP_TXOFFSET_2:
1435 i = (reg - RADEON_PP_TXOFFSET_0) / 24;
1436 r = r100_cs_packet_next_reloc(p, &reloc);
1437 if (r) {
1438 DRM_ERROR("No reloc for ib[%d]=0x%04X\n",
1439 idx, reg);
1440 r100_cs_dump_packet(p, pkt);
1441 return r;
1442 }
Dave Airlie513bcb42009-09-23 16:56:27 +10001443 ib[idx] = idx_value + ((u32)reloc->lobj.gpu_offset);
Dave Airlie551ebd82009-09-01 15:25:57 +10001444 track->textures[i].robj = reloc->robj;
1445 break;
1446 case RADEON_PP_CUBIC_OFFSET_T0_0:
1447 case RADEON_PP_CUBIC_OFFSET_T0_1:
1448 case RADEON_PP_CUBIC_OFFSET_T0_2:
1449 case RADEON_PP_CUBIC_OFFSET_T0_3:
1450 case RADEON_PP_CUBIC_OFFSET_T0_4:
1451 i = (reg - RADEON_PP_CUBIC_OFFSET_T0_0) / 4;
1452 r = r100_cs_packet_next_reloc(p, &reloc);
1453 if (r) {
1454 DRM_ERROR("No reloc for ib[%d]=0x%04X\n",
1455 idx, reg);
1456 r100_cs_dump_packet(p, pkt);
1457 return r;
1458 }
Dave Airlie513bcb42009-09-23 16:56:27 +10001459 track->textures[0].cube_info[i].offset = idx_value;
1460 ib[idx] = idx_value + ((u32)reloc->lobj.gpu_offset);
Dave Airlie551ebd82009-09-01 15:25:57 +10001461 track->textures[0].cube_info[i].robj = reloc->robj;
1462 break;
1463 case RADEON_PP_CUBIC_OFFSET_T1_0:
1464 case RADEON_PP_CUBIC_OFFSET_T1_1:
1465 case RADEON_PP_CUBIC_OFFSET_T1_2:
1466 case RADEON_PP_CUBIC_OFFSET_T1_3:
1467 case RADEON_PP_CUBIC_OFFSET_T1_4:
1468 i = (reg - RADEON_PP_CUBIC_OFFSET_T1_0) / 4;
1469 r = r100_cs_packet_next_reloc(p, &reloc);
1470 if (r) {
1471 DRM_ERROR("No reloc for ib[%d]=0x%04X\n",
1472 idx, reg);
1473 r100_cs_dump_packet(p, pkt);
1474 return r;
1475 }
Dave Airlie513bcb42009-09-23 16:56:27 +10001476 track->textures[1].cube_info[i].offset = idx_value;
1477 ib[idx] = idx_value + ((u32)reloc->lobj.gpu_offset);
Dave Airlie551ebd82009-09-01 15:25:57 +10001478 track->textures[1].cube_info[i].robj = reloc->robj;
1479 break;
1480 case RADEON_PP_CUBIC_OFFSET_T2_0:
1481 case RADEON_PP_CUBIC_OFFSET_T2_1:
1482 case RADEON_PP_CUBIC_OFFSET_T2_2:
1483 case RADEON_PP_CUBIC_OFFSET_T2_3:
1484 case RADEON_PP_CUBIC_OFFSET_T2_4:
1485 i = (reg - RADEON_PP_CUBIC_OFFSET_T2_0) / 4;
1486 r = r100_cs_packet_next_reloc(p, &reloc);
1487 if (r) {
1488 DRM_ERROR("No reloc for ib[%d]=0x%04X\n",
1489 idx, reg);
1490 r100_cs_dump_packet(p, pkt);
1491 return r;
1492 }
Dave Airlie513bcb42009-09-23 16:56:27 +10001493 track->textures[2].cube_info[i].offset = idx_value;
1494 ib[idx] = idx_value + ((u32)reloc->lobj.gpu_offset);
Dave Airlie551ebd82009-09-01 15:25:57 +10001495 track->textures[2].cube_info[i].robj = reloc->robj;
1496 break;
1497 case RADEON_RE_WIDTH_HEIGHT:
Dave Airlie513bcb42009-09-23 16:56:27 +10001498 track->maxy = ((idx_value >> 16) & 0x7FF);
Dave Airlie551ebd82009-09-01 15:25:57 +10001499 break;
1500 case RADEON_RB3D_COLORPITCH:
1501 r = r100_cs_packet_next_reloc(p, &reloc);
1502 if (r) {
1503 DRM_ERROR("No reloc for ib[%d]=0x%04X\n",
1504 idx, reg);
1505 r100_cs_dump_packet(p, pkt);
1506 return r;
1507 }
Dave Airliee024e112009-06-24 09:48:08 +10001508
Dave Airlie551ebd82009-09-01 15:25:57 +10001509 if (reloc->lobj.tiling_flags & RADEON_TILING_MACRO)
1510 tile_flags |= RADEON_COLOR_TILE_ENABLE;
1511 if (reloc->lobj.tiling_flags & RADEON_TILING_MICRO)
1512 tile_flags |= RADEON_COLOR_MICROTILE_ENABLE;
Dave Airliee024e112009-06-24 09:48:08 +10001513
Dave Airlie513bcb42009-09-23 16:56:27 +10001514 tmp = idx_value & ~(0x7 << 16);
Dave Airlie551ebd82009-09-01 15:25:57 +10001515 tmp |= tile_flags;
1516 ib[idx] = tmp;
1517
Dave Airlie513bcb42009-09-23 16:56:27 +10001518 track->cb[0].pitch = idx_value & RADEON_COLORPITCH_MASK;
Dave Airlie551ebd82009-09-01 15:25:57 +10001519 break;
1520 case RADEON_RB3D_DEPTHPITCH:
Dave Airlie513bcb42009-09-23 16:56:27 +10001521 track->zb.pitch = idx_value & RADEON_DEPTHPITCH_MASK;
Dave Airlie551ebd82009-09-01 15:25:57 +10001522 break;
1523 case RADEON_RB3D_CNTL:
Dave Airlie513bcb42009-09-23 16:56:27 +10001524 switch ((idx_value >> RADEON_RB3D_COLOR_FORMAT_SHIFT) & 0x1f) {
Dave Airlie551ebd82009-09-01 15:25:57 +10001525 case 7:
1526 case 8:
1527 case 9:
1528 case 11:
1529 case 12:
1530 track->cb[0].cpp = 1;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001531 break;
Dave Airlie551ebd82009-09-01 15:25:57 +10001532 case 3:
1533 case 4:
1534 case 15:
1535 track->cb[0].cpp = 2;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001536 break;
Dave Airlie551ebd82009-09-01 15:25:57 +10001537 case 6:
1538 track->cb[0].cpp = 4;
Dave Airlie17782d92009-08-21 10:07:54 +10001539 break;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001540 default:
Dave Airlie551ebd82009-09-01 15:25:57 +10001541 DRM_ERROR("Invalid color buffer format (%d) !\n",
Dave Airlie513bcb42009-09-23 16:56:27 +10001542 ((idx_value >> RADEON_RB3D_COLOR_FORMAT_SHIFT) & 0x1f));
Dave Airlie551ebd82009-09-01 15:25:57 +10001543 return -EINVAL;
1544 }
Dave Airlie513bcb42009-09-23 16:56:27 +10001545 track->z_enabled = !!(idx_value & RADEON_Z_ENABLE);
Dave Airlie551ebd82009-09-01 15:25:57 +10001546 break;
1547 case RADEON_RB3D_ZSTENCILCNTL:
Dave Airlie513bcb42009-09-23 16:56:27 +10001548 switch (idx_value & 0xf) {
Dave Airlie551ebd82009-09-01 15:25:57 +10001549 case 0:
1550 track->zb.cpp = 2;
1551 break;
1552 case 2:
1553 case 3:
1554 case 4:
1555 case 5:
1556 case 9:
1557 case 11:
1558 track->zb.cpp = 4;
1559 break;
1560 default:
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001561 break;
1562 }
Dave Airlie551ebd82009-09-01 15:25:57 +10001563 break;
1564 case RADEON_RB3D_ZPASS_ADDR:
1565 r = r100_cs_packet_next_reloc(p, &reloc);
1566 if (r) {
1567 DRM_ERROR("No reloc for ib[%d]=0x%04X\n",
1568 idx, reg);
1569 r100_cs_dump_packet(p, pkt);
1570 return r;
1571 }
Dave Airlie513bcb42009-09-23 16:56:27 +10001572 ib[idx] = idx_value + ((u32)reloc->lobj.gpu_offset);
Dave Airlie551ebd82009-09-01 15:25:57 +10001573 break;
1574 case RADEON_PP_CNTL:
1575 {
Dave Airlie513bcb42009-09-23 16:56:27 +10001576 uint32_t temp = idx_value >> 4;
Dave Airlie551ebd82009-09-01 15:25:57 +10001577 for (i = 0; i < track->num_texture; i++)
1578 track->textures[i].enabled = !!(temp & (1 << i));
1579 }
1580 break;
1581 case RADEON_SE_VF_CNTL:
Dave Airlie513bcb42009-09-23 16:56:27 +10001582 track->vap_vf_cntl = idx_value;
Dave Airlie551ebd82009-09-01 15:25:57 +10001583 break;
1584 case RADEON_SE_VTX_FMT:
Dave Airlie513bcb42009-09-23 16:56:27 +10001585 track->vtx_size = r100_get_vtx_size(idx_value);
Dave Airlie551ebd82009-09-01 15:25:57 +10001586 break;
1587 case RADEON_PP_TEX_SIZE_0:
1588 case RADEON_PP_TEX_SIZE_1:
1589 case RADEON_PP_TEX_SIZE_2:
1590 i = (reg - RADEON_PP_TEX_SIZE_0) / 8;
Dave Airlie513bcb42009-09-23 16:56:27 +10001591 track->textures[i].width = (idx_value & RADEON_TEX_USIZE_MASK) + 1;
1592 track->textures[i].height = ((idx_value & RADEON_TEX_VSIZE_MASK) >> RADEON_TEX_VSIZE_SHIFT) + 1;
Dave Airlie551ebd82009-09-01 15:25:57 +10001593 break;
1594 case RADEON_PP_TEX_PITCH_0:
1595 case RADEON_PP_TEX_PITCH_1:
1596 case RADEON_PP_TEX_PITCH_2:
1597 i = (reg - RADEON_PP_TEX_PITCH_0) / 8;
Dave Airlie513bcb42009-09-23 16:56:27 +10001598 track->textures[i].pitch = idx_value + 32;
Dave Airlie551ebd82009-09-01 15:25:57 +10001599 break;
1600 case RADEON_PP_TXFILTER_0:
1601 case RADEON_PP_TXFILTER_1:
1602 case RADEON_PP_TXFILTER_2:
1603 i = (reg - RADEON_PP_TXFILTER_0) / 24;
Dave Airlie513bcb42009-09-23 16:56:27 +10001604 track->textures[i].num_levels = ((idx_value & RADEON_MAX_MIP_LEVEL_MASK)
Dave Airlie551ebd82009-09-01 15:25:57 +10001605 >> RADEON_MAX_MIP_LEVEL_SHIFT);
Dave Airlie513bcb42009-09-23 16:56:27 +10001606 tmp = (idx_value >> 23) & 0x7;
Dave Airlie551ebd82009-09-01 15:25:57 +10001607 if (tmp == 2 || tmp == 6)
1608 track->textures[i].roundup_w = false;
Dave Airlie513bcb42009-09-23 16:56:27 +10001609 tmp = (idx_value >> 27) & 0x7;
Dave Airlie551ebd82009-09-01 15:25:57 +10001610 if (tmp == 2 || tmp == 6)
1611 track->textures[i].roundup_h = false;
1612 break;
1613 case RADEON_PP_TXFORMAT_0:
1614 case RADEON_PP_TXFORMAT_1:
1615 case RADEON_PP_TXFORMAT_2:
1616 i = (reg - RADEON_PP_TXFORMAT_0) / 24;
Dave Airlie513bcb42009-09-23 16:56:27 +10001617 if (idx_value & RADEON_TXFORMAT_NON_POWER2) {
Dave Airlie551ebd82009-09-01 15:25:57 +10001618 track->textures[i].use_pitch = 1;
1619 } else {
1620 track->textures[i].use_pitch = 0;
Dave Airlie513bcb42009-09-23 16:56:27 +10001621 track->textures[i].width = 1 << ((idx_value >> RADEON_TXFORMAT_WIDTH_SHIFT) & RADEON_TXFORMAT_WIDTH_MASK);
1622 track->textures[i].height = 1 << ((idx_value >> RADEON_TXFORMAT_HEIGHT_SHIFT) & RADEON_TXFORMAT_HEIGHT_MASK);
Dave Airlie551ebd82009-09-01 15:25:57 +10001623 }
Dave Airlie513bcb42009-09-23 16:56:27 +10001624 if (idx_value & RADEON_TXFORMAT_CUBIC_MAP_ENABLE)
Dave Airlie551ebd82009-09-01 15:25:57 +10001625 track->textures[i].tex_coord_type = 2;
Dave Airlie513bcb42009-09-23 16:56:27 +10001626 switch ((idx_value & RADEON_TXFORMAT_FORMAT_MASK)) {
Dave Airlie551ebd82009-09-01 15:25:57 +10001627 case RADEON_TXFORMAT_I8:
1628 case RADEON_TXFORMAT_RGB332:
1629 case RADEON_TXFORMAT_Y8:
1630 track->textures[i].cpp = 1;
Roland Scheideggerf9da52d2010-06-12 12:12:37 -04001631 track->textures[i].compress_format = R100_TRACK_COMP_NONE;
Dave Airlie551ebd82009-09-01 15:25:57 +10001632 break;
1633 case RADEON_TXFORMAT_AI88:
1634 case RADEON_TXFORMAT_ARGB1555:
1635 case RADEON_TXFORMAT_RGB565:
1636 case RADEON_TXFORMAT_ARGB4444:
1637 case RADEON_TXFORMAT_VYUY422:
1638 case RADEON_TXFORMAT_YVYU422:
Dave Airlie551ebd82009-09-01 15:25:57 +10001639 case RADEON_TXFORMAT_SHADOW16:
1640 case RADEON_TXFORMAT_LDUDV655:
1641 case RADEON_TXFORMAT_DUDV88:
1642 track->textures[i].cpp = 2;
Roland Scheideggerf9da52d2010-06-12 12:12:37 -04001643 track->textures[i].compress_format = R100_TRACK_COMP_NONE;
Dave Airlie551ebd82009-09-01 15:25:57 +10001644 break;
1645 case RADEON_TXFORMAT_ARGB8888:
1646 case RADEON_TXFORMAT_RGBA8888:
Dave Airlie551ebd82009-09-01 15:25:57 +10001647 case RADEON_TXFORMAT_SHADOW32:
1648 case RADEON_TXFORMAT_LDUDUV8888:
1649 track->textures[i].cpp = 4;
Roland Scheideggerf9da52d2010-06-12 12:12:37 -04001650 track->textures[i].compress_format = R100_TRACK_COMP_NONE;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001651 break;
Dave Airlied785d782009-12-07 13:16:06 +10001652 case RADEON_TXFORMAT_DXT1:
1653 track->textures[i].cpp = 1;
1654 track->textures[i].compress_format = R100_TRACK_COMP_DXT1;
1655 break;
1656 case RADEON_TXFORMAT_DXT23:
1657 case RADEON_TXFORMAT_DXT45:
1658 track->textures[i].cpp = 1;
1659 track->textures[i].compress_format = R100_TRACK_COMP_DXT35;
1660 break;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001661 }
Dave Airlie513bcb42009-09-23 16:56:27 +10001662 track->textures[i].cube_info[4].width = 1 << ((idx_value >> 16) & 0xf);
1663 track->textures[i].cube_info[4].height = 1 << ((idx_value >> 20) & 0xf);
Dave Airlie551ebd82009-09-01 15:25:57 +10001664 break;
1665 case RADEON_PP_CUBIC_FACES_0:
1666 case RADEON_PP_CUBIC_FACES_1:
1667 case RADEON_PP_CUBIC_FACES_2:
Dave Airlie513bcb42009-09-23 16:56:27 +10001668 tmp = idx_value;
Dave Airlie551ebd82009-09-01 15:25:57 +10001669 i = (reg - RADEON_PP_CUBIC_FACES_0) / 4;
1670 for (face = 0; face < 4; face++) {
1671 track->textures[i].cube_info[face].width = 1 << ((tmp >> (face * 8)) & 0xf);
1672 track->textures[i].cube_info[face].height = 1 << ((tmp >> ((face * 8) + 4)) & 0xf);
1673 }
1674 break;
1675 default:
1676 printk(KERN_ERR "Forbidden register 0x%04X in cs at %d\n",
1677 reg, idx);
1678 return -EINVAL;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001679 }
1680 return 0;
1681}
1682
Jerome Glisse068a1172009-06-17 13:28:30 +02001683int r100_cs_track_check_pkt3_indx_buffer(struct radeon_cs_parser *p,
1684 struct radeon_cs_packet *pkt,
Jerome Glisse4c788672009-11-20 14:29:23 +01001685 struct radeon_bo *robj)
Jerome Glisse068a1172009-06-17 13:28:30 +02001686{
Jerome Glisse068a1172009-06-17 13:28:30 +02001687 unsigned idx;
Dave Airlie513bcb42009-09-23 16:56:27 +10001688 u32 value;
Jerome Glisse068a1172009-06-17 13:28:30 +02001689 idx = pkt->idx + 1;
Dave Airlie513bcb42009-09-23 16:56:27 +10001690 value = radeon_get_ib_value(p, idx + 2);
Jerome Glisse4c788672009-11-20 14:29:23 +01001691 if ((value + 1) > radeon_bo_size(robj)) {
Jerome Glisse068a1172009-06-17 13:28:30 +02001692 DRM_ERROR("[drm] Buffer too small for PACKET3 INDX_BUFFER "
1693 "(need %u have %lu) !\n",
Dave Airlie513bcb42009-09-23 16:56:27 +10001694 value + 1,
Jerome Glisse4c788672009-11-20 14:29:23 +01001695 radeon_bo_size(robj));
Jerome Glisse068a1172009-06-17 13:28:30 +02001696 return -EINVAL;
1697 }
1698 return 0;
1699}
1700
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001701static int r100_packet3_check(struct radeon_cs_parser *p,
1702 struct radeon_cs_packet *pkt)
1703{
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001704 struct radeon_cs_reloc *reloc;
Dave Airlie551ebd82009-09-01 15:25:57 +10001705 struct r100_cs_track *track;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001706 unsigned idx;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001707 volatile uint32_t *ib;
1708 int r;
1709
1710 ib = p->ib->ptr;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001711 idx = pkt->idx + 1;
Dave Airlie551ebd82009-09-01 15:25:57 +10001712 track = (struct r100_cs_track *)p->track;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001713 switch (pkt->opcode) {
1714 case PACKET3_3D_LOAD_VBPNTR:
Dave Airlie513bcb42009-09-23 16:56:27 +10001715 r = r100_packet3_load_vbpntr(p, pkt, idx);
1716 if (r)
1717 return r;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001718 break;
1719 case PACKET3_INDX_BUFFER:
1720 r = r100_cs_packet_next_reloc(p, &reloc);
1721 if (r) {
1722 DRM_ERROR("No reloc for packet3 %d\n", pkt->opcode);
1723 r100_cs_dump_packet(p, pkt);
1724 return r;
1725 }
Dave Airlie513bcb42009-09-23 16:56:27 +10001726 ib[idx+1] = radeon_get_ib_value(p, idx+1) + ((u32)reloc->lobj.gpu_offset);
Jerome Glisse068a1172009-06-17 13:28:30 +02001727 r = r100_cs_track_check_pkt3_indx_buffer(p, pkt, reloc->robj);
1728 if (r) {
1729 return r;
1730 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001731 break;
1732 case 0x23:
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001733 /* 3D_RNDR_GEN_INDX_PRIM on r100/r200 */
1734 r = r100_cs_packet_next_reloc(p, &reloc);
1735 if (r) {
1736 DRM_ERROR("No reloc for packet3 %d\n", pkt->opcode);
1737 r100_cs_dump_packet(p, pkt);
1738 return r;
1739 }
Dave Airlie513bcb42009-09-23 16:56:27 +10001740 ib[idx] = radeon_get_ib_value(p, idx) + ((u32)reloc->lobj.gpu_offset);
Dave Airlie551ebd82009-09-01 15:25:57 +10001741 track->num_arrays = 1;
Dave Airlie513bcb42009-09-23 16:56:27 +10001742 track->vtx_size = r100_get_vtx_size(radeon_get_ib_value(p, idx + 2));
Dave Airlie551ebd82009-09-01 15:25:57 +10001743
1744 track->arrays[0].robj = reloc->robj;
1745 track->arrays[0].esize = track->vtx_size;
1746
Dave Airlie513bcb42009-09-23 16:56:27 +10001747 track->max_indx = radeon_get_ib_value(p, idx+1);
Dave Airlie551ebd82009-09-01 15:25:57 +10001748
Dave Airlie513bcb42009-09-23 16:56:27 +10001749 track->vap_vf_cntl = radeon_get_ib_value(p, idx+3);
Dave Airlie551ebd82009-09-01 15:25:57 +10001750 track->immd_dwords = pkt->count - 1;
1751 r = r100_cs_track_check(p->rdev, track);
1752 if (r)
1753 return r;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001754 break;
1755 case PACKET3_3D_DRAW_IMMD:
Dave Airlie513bcb42009-09-23 16:56:27 +10001756 if (((radeon_get_ib_value(p, idx + 1) >> 4) & 0x3) != 3) {
Dave Airlie551ebd82009-09-01 15:25:57 +10001757 DRM_ERROR("PRIM_WALK must be 3 for IMMD draw\n");
1758 return -EINVAL;
1759 }
Alex Deuchercf57fc72010-01-18 20:20:07 -05001760 track->vtx_size = r100_get_vtx_size(radeon_get_ib_value(p, idx + 0));
Dave Airlie513bcb42009-09-23 16:56:27 +10001761 track->vap_vf_cntl = radeon_get_ib_value(p, idx + 1);
Dave Airlie551ebd82009-09-01 15:25:57 +10001762 track->immd_dwords = pkt->count - 1;
1763 r = r100_cs_track_check(p->rdev, track);
1764 if (r)
1765 return r;
1766 break;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001767 /* triggers drawing using in-packet vertex data */
1768 case PACKET3_3D_DRAW_IMMD_2:
Dave Airlie513bcb42009-09-23 16:56:27 +10001769 if (((radeon_get_ib_value(p, idx) >> 4) & 0x3) != 3) {
Dave Airlie551ebd82009-09-01 15:25:57 +10001770 DRM_ERROR("PRIM_WALK must be 3 for IMMD draw\n");
1771 return -EINVAL;
1772 }
Dave Airlie513bcb42009-09-23 16:56:27 +10001773 track->vap_vf_cntl = radeon_get_ib_value(p, idx);
Dave Airlie551ebd82009-09-01 15:25:57 +10001774 track->immd_dwords = pkt->count;
1775 r = r100_cs_track_check(p->rdev, track);
1776 if (r)
1777 return r;
1778 break;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001779 /* triggers drawing using in-packet vertex data */
1780 case PACKET3_3D_DRAW_VBUF_2:
Dave Airlie513bcb42009-09-23 16:56:27 +10001781 track->vap_vf_cntl = radeon_get_ib_value(p, idx);
Dave Airlie551ebd82009-09-01 15:25:57 +10001782 r = r100_cs_track_check(p->rdev, track);
1783 if (r)
1784 return r;
1785 break;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001786 /* triggers drawing of vertex buffers setup elsewhere */
1787 case PACKET3_3D_DRAW_INDX_2:
Dave Airlie513bcb42009-09-23 16:56:27 +10001788 track->vap_vf_cntl = radeon_get_ib_value(p, idx);
Dave Airlie551ebd82009-09-01 15:25:57 +10001789 r = r100_cs_track_check(p->rdev, track);
1790 if (r)
1791 return r;
1792 break;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001793 /* triggers drawing using indices to vertex buffer */
1794 case PACKET3_3D_DRAW_VBUF:
Dave Airlie513bcb42009-09-23 16:56:27 +10001795 track->vap_vf_cntl = radeon_get_ib_value(p, idx + 1);
Dave Airlie551ebd82009-09-01 15:25:57 +10001796 r = r100_cs_track_check(p->rdev, track);
1797 if (r)
1798 return r;
1799 break;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001800 /* triggers drawing of vertex buffers setup elsewhere */
1801 case PACKET3_3D_DRAW_INDX:
Dave Airlie513bcb42009-09-23 16:56:27 +10001802 track->vap_vf_cntl = radeon_get_ib_value(p, idx + 1);
Dave Airlie551ebd82009-09-01 15:25:57 +10001803 r = r100_cs_track_check(p->rdev, track);
1804 if (r)
1805 return r;
1806 break;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001807 /* triggers drawing using indices to vertex buffer */
1808 case PACKET3_NOP:
1809 break;
1810 default:
1811 DRM_ERROR("Packet3 opcode %x not supported\n", pkt->opcode);
1812 return -EINVAL;
1813 }
1814 return 0;
1815}
1816
1817int r100_cs_parse(struct radeon_cs_parser *p)
1818{
1819 struct radeon_cs_packet pkt;
Jerome Glisse9f022dd2009-09-11 15:35:22 +02001820 struct r100_cs_track *track;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001821 int r;
1822
Jerome Glisse9f022dd2009-09-11 15:35:22 +02001823 track = kzalloc(sizeof(*track), GFP_KERNEL);
1824 r100_cs_track_clear(p->rdev, track);
1825 p->track = track;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001826 do {
1827 r = r100_cs_packet_parse(p, &pkt, p->idx);
1828 if (r) {
1829 return r;
1830 }
1831 p->idx += pkt.count + 2;
1832 switch (pkt.type) {
Jerome Glisse068a1172009-06-17 13:28:30 +02001833 case PACKET_TYPE0:
Dave Airlie551ebd82009-09-01 15:25:57 +10001834 if (p->rdev->family >= CHIP_R200)
1835 r = r100_cs_parse_packet0(p, &pkt,
1836 p->rdev->config.r100.reg_safe_bm,
1837 p->rdev->config.r100.reg_safe_bm_size,
1838 &r200_packet0_check);
1839 else
1840 r = r100_cs_parse_packet0(p, &pkt,
1841 p->rdev->config.r100.reg_safe_bm,
1842 p->rdev->config.r100.reg_safe_bm_size,
1843 &r100_packet0_check);
Jerome Glisse068a1172009-06-17 13:28:30 +02001844 break;
1845 case PACKET_TYPE2:
1846 break;
1847 case PACKET_TYPE3:
1848 r = r100_packet3_check(p, &pkt);
1849 break;
1850 default:
1851 DRM_ERROR("Unknown packet type %d !\n",
1852 pkt.type);
1853 return -EINVAL;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001854 }
1855 if (r) {
1856 return r;
1857 }
1858 } while (p->idx < p->chunks[p->chunk_ib_idx].length_dw);
1859 return 0;
1860}
1861
1862
1863/*
1864 * Global GPU functions
1865 */
1866void r100_errata(struct radeon_device *rdev)
1867{
1868 rdev->pll_errata = 0;
1869
1870 if (rdev->family == CHIP_RV200 || rdev->family == CHIP_RS200) {
1871 rdev->pll_errata |= CHIP_ERRATA_PLL_DUMMYREADS;
1872 }
1873
1874 if (rdev->family == CHIP_RV100 ||
1875 rdev->family == CHIP_RS100 ||
1876 rdev->family == CHIP_RS200) {
1877 rdev->pll_errata |= CHIP_ERRATA_PLL_DELAY;
1878 }
1879}
1880
1881/* Wait for vertical sync on primary CRTC */
1882void r100_gpu_wait_for_vsync(struct radeon_device *rdev)
1883{
1884 uint32_t crtc_gen_cntl, tmp;
1885 int i;
1886
1887 crtc_gen_cntl = RREG32(RADEON_CRTC_GEN_CNTL);
1888 if ((crtc_gen_cntl & RADEON_CRTC_DISP_REQ_EN_B) ||
1889 !(crtc_gen_cntl & RADEON_CRTC_EN)) {
1890 return;
1891 }
1892 /* Clear the CRTC_VBLANK_SAVE bit */
1893 WREG32(RADEON_CRTC_STATUS, RADEON_CRTC_VBLANK_SAVE_CLEAR);
1894 for (i = 0; i < rdev->usec_timeout; i++) {
1895 tmp = RREG32(RADEON_CRTC_STATUS);
1896 if (tmp & RADEON_CRTC_VBLANK_SAVE) {
1897 return;
1898 }
1899 DRM_UDELAY(1);
1900 }
1901}
1902
1903/* Wait for vertical sync on secondary CRTC */
1904void r100_gpu_wait_for_vsync2(struct radeon_device *rdev)
1905{
1906 uint32_t crtc2_gen_cntl, tmp;
1907 int i;
1908
1909 crtc2_gen_cntl = RREG32(RADEON_CRTC2_GEN_CNTL);
1910 if ((crtc2_gen_cntl & RADEON_CRTC2_DISP_REQ_EN_B) ||
1911 !(crtc2_gen_cntl & RADEON_CRTC2_EN))
1912 return;
1913
1914 /* Clear the CRTC_VBLANK_SAVE bit */
1915 WREG32(RADEON_CRTC2_STATUS, RADEON_CRTC2_VBLANK_SAVE_CLEAR);
1916 for (i = 0; i < rdev->usec_timeout; i++) {
1917 tmp = RREG32(RADEON_CRTC2_STATUS);
1918 if (tmp & RADEON_CRTC2_VBLANK_SAVE) {
1919 return;
1920 }
1921 DRM_UDELAY(1);
1922 }
1923}
1924
1925int r100_rbbm_fifo_wait_for_entry(struct radeon_device *rdev, unsigned n)
1926{
1927 unsigned i;
1928 uint32_t tmp;
1929
1930 for (i = 0; i < rdev->usec_timeout; i++) {
1931 tmp = RREG32(RADEON_RBBM_STATUS) & RADEON_RBBM_FIFOCNT_MASK;
1932 if (tmp >= n) {
1933 return 0;
1934 }
1935 DRM_UDELAY(1);
1936 }
1937 return -1;
1938}
1939
1940int r100_gui_wait_for_idle(struct radeon_device *rdev)
1941{
1942 unsigned i;
1943 uint32_t tmp;
1944
1945 if (r100_rbbm_fifo_wait_for_entry(rdev, 64)) {
1946 printk(KERN_WARNING "radeon: wait for empty RBBM fifo failed !"
1947 " Bad things might happen.\n");
1948 }
1949 for (i = 0; i < rdev->usec_timeout; i++) {
1950 tmp = RREG32(RADEON_RBBM_STATUS);
Alex Deucher4612dc92010-02-05 01:58:28 -05001951 if (!(tmp & RADEON_RBBM_ACTIVE)) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001952 return 0;
1953 }
1954 DRM_UDELAY(1);
1955 }
1956 return -1;
1957}
1958
1959int r100_mc_wait_for_idle(struct radeon_device *rdev)
1960{
1961 unsigned i;
1962 uint32_t tmp;
1963
1964 for (i = 0; i < rdev->usec_timeout; i++) {
1965 /* read MC_STATUS */
Alex Deucher4612dc92010-02-05 01:58:28 -05001966 tmp = RREG32(RADEON_MC_STATUS);
1967 if (tmp & RADEON_MC_IDLE) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001968 return 0;
1969 }
1970 DRM_UDELAY(1);
1971 }
1972 return -1;
1973}
1974
Jerome Glisse225758d2010-03-09 14:45:10 +00001975void r100_gpu_lockup_update(struct r100_gpu_lockup *lockup, struct radeon_cp *cp)
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001976{
Jerome Glisse225758d2010-03-09 14:45:10 +00001977 lockup->last_cp_rptr = cp->rptr;
1978 lockup->last_jiffies = jiffies;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001979}
1980
Jerome Glisse225758d2010-03-09 14:45:10 +00001981/**
1982 * r100_gpu_cp_is_lockup() - check if CP is lockup by recording information
1983 * @rdev: radeon device structure
1984 * @lockup: r100_gpu_lockup structure holding CP lockup tracking informations
1985 * @cp: radeon_cp structure holding CP information
1986 *
1987 * We don't need to initialize the lockup tracking information as we will either
1988 * have CP rptr to a different value of jiffies wrap around which will force
1989 * initialization of the lockup tracking informations.
1990 *
1991 * A possible false positivie is if we get call after while and last_cp_rptr ==
1992 * the current CP rptr, even if it's unlikely it might happen. To avoid this
1993 * if the elapsed time since last call is bigger than 2 second than we return
1994 * false and update the tracking information. Due to this the caller must call
1995 * r100_gpu_cp_is_lockup several time in less than 2sec for lockup to be reported
1996 * the fencing code should be cautious about that.
1997 *
1998 * Caller should write to the ring to force CP to do something so we don't get
1999 * false positive when CP is just gived nothing to do.
2000 *
2001 **/
2002bool r100_gpu_cp_is_lockup(struct radeon_device *rdev, struct r100_gpu_lockup *lockup, struct radeon_cp *cp)
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002003{
Jerome Glisse225758d2010-03-09 14:45:10 +00002004 unsigned long cjiffies, elapsed;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002005
Jerome Glisse225758d2010-03-09 14:45:10 +00002006 cjiffies = jiffies;
2007 if (!time_after(cjiffies, lockup->last_jiffies)) {
2008 /* likely a wrap around */
2009 lockup->last_cp_rptr = cp->rptr;
2010 lockup->last_jiffies = jiffies;
2011 return false;
2012 }
2013 if (cp->rptr != lockup->last_cp_rptr) {
2014 /* CP is still working no lockup */
2015 lockup->last_cp_rptr = cp->rptr;
2016 lockup->last_jiffies = jiffies;
2017 return false;
2018 }
2019 elapsed = jiffies_to_msecs(cjiffies - lockup->last_jiffies);
2020 if (elapsed >= 3000) {
2021 /* very likely the improbable case where current
2022 * rptr is equal to last recorded, a while ago, rptr
2023 * this is more likely a false positive update tracking
2024 * information which should force us to be recall at
2025 * latter point
2026 */
2027 lockup->last_cp_rptr = cp->rptr;
2028 lockup->last_jiffies = jiffies;
2029 return false;
2030 }
2031 if (elapsed >= 1000) {
2032 dev_err(rdev->dev, "GPU lockup CP stall for more than %lumsec\n", elapsed);
2033 return true;
2034 }
2035 /* give a chance to the GPU ... */
2036 return false;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002037}
2038
Jerome Glisse225758d2010-03-09 14:45:10 +00002039bool r100_gpu_is_lockup(struct radeon_device *rdev)
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002040{
Jerome Glisse225758d2010-03-09 14:45:10 +00002041 u32 rbbm_status;
2042 int r;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002043
Jerome Glisse225758d2010-03-09 14:45:10 +00002044 rbbm_status = RREG32(R_000E40_RBBM_STATUS);
2045 if (!G_000E40_GUI_ACTIVE(rbbm_status)) {
2046 r100_gpu_lockup_update(&rdev->config.r100.lockup, &rdev->cp);
2047 return false;
2048 }
2049 /* force CP activities */
2050 r = radeon_ring_lock(rdev, 2);
2051 if (!r) {
2052 /* PACKET2 NOP */
2053 radeon_ring_write(rdev, 0x80000000);
2054 radeon_ring_write(rdev, 0x80000000);
2055 radeon_ring_unlock_commit(rdev);
2056 }
2057 rdev->cp.rptr = RREG32(RADEON_CP_RB_RPTR);
2058 return r100_gpu_cp_is_lockup(rdev, &rdev->config.r100.lockup, &rdev->cp);
2059}
2060
Jerome Glisse90aca4d2010-03-09 14:45:12 +00002061void r100_bm_disable(struct radeon_device *rdev)
2062{
2063 u32 tmp;
2064
2065 /* disable bus mastering */
2066 tmp = RREG32(R_000030_BUS_CNTL);
2067 WREG32(R_000030_BUS_CNTL, (tmp & 0xFFFFFFFF) | 0x00000044);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002068 mdelay(1);
Jerome Glisse90aca4d2010-03-09 14:45:12 +00002069 WREG32(R_000030_BUS_CNTL, (tmp & 0xFFFFFFFF) | 0x00000042);
2070 mdelay(1);
2071 WREG32(R_000030_BUS_CNTL, (tmp & 0xFFFFFFFF) | 0x00000040);
2072 tmp = RREG32(RADEON_BUS_CNTL);
2073 mdelay(1);
2074 pci_read_config_word(rdev->pdev, 0x4, (u16*)&tmp);
2075 pci_write_config_word(rdev->pdev, 0x4, tmp & 0xFFFB);
2076 mdelay(1);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002077}
2078
Jerome Glissea2d07b72010-03-09 14:45:11 +00002079int r100_asic_reset(struct radeon_device *rdev)
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002080{
Jerome Glisse90aca4d2010-03-09 14:45:12 +00002081 struct r100_mc_save save;
2082 u32 status, tmp;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002083
Jerome Glisse90aca4d2010-03-09 14:45:12 +00002084 r100_mc_stop(rdev, &save);
2085 status = RREG32(R_000E40_RBBM_STATUS);
2086 if (!G_000E40_GUI_ACTIVE(status)) {
2087 return 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002088 }
Jerome Glisse90aca4d2010-03-09 14:45:12 +00002089 status = RREG32(R_000E40_RBBM_STATUS);
2090 dev_info(rdev->dev, "(%s:%d) RBBM_STATUS=0x%08X\n", __func__, __LINE__, status);
2091 /* stop CP */
2092 WREG32(RADEON_CP_CSQ_CNTL, 0);
2093 tmp = RREG32(RADEON_CP_RB_CNTL);
2094 WREG32(RADEON_CP_RB_CNTL, tmp | RADEON_RB_RPTR_WR_ENA);
2095 WREG32(RADEON_CP_RB_RPTR_WR, 0);
2096 WREG32(RADEON_CP_RB_WPTR, 0);
2097 WREG32(RADEON_CP_RB_CNTL, tmp);
2098 /* save PCI state */
2099 pci_save_state(rdev->pdev);
2100 /* disable bus mastering */
2101 r100_bm_disable(rdev);
2102 WREG32(R_0000F0_RBBM_SOFT_RESET, S_0000F0_SOFT_RESET_SE(1) |
2103 S_0000F0_SOFT_RESET_RE(1) |
2104 S_0000F0_SOFT_RESET_PP(1) |
2105 S_0000F0_SOFT_RESET_RB(1));
2106 RREG32(R_0000F0_RBBM_SOFT_RESET);
2107 mdelay(500);
2108 WREG32(R_0000F0_RBBM_SOFT_RESET, 0);
2109 mdelay(1);
2110 status = RREG32(R_000E40_RBBM_STATUS);
2111 dev_info(rdev->dev, "(%s:%d) RBBM_STATUS=0x%08X\n", __func__, __LINE__, status);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002112 /* reset CP */
Jerome Glisse90aca4d2010-03-09 14:45:12 +00002113 WREG32(R_0000F0_RBBM_SOFT_RESET, S_0000F0_SOFT_RESET_CP(1));
2114 RREG32(R_0000F0_RBBM_SOFT_RESET);
2115 mdelay(500);
2116 WREG32(R_0000F0_RBBM_SOFT_RESET, 0);
2117 mdelay(1);
2118 status = RREG32(R_000E40_RBBM_STATUS);
2119 dev_info(rdev->dev, "(%s:%d) RBBM_STATUS=0x%08X\n", __func__, __LINE__, status);
2120 /* restore PCI & busmastering */
2121 pci_restore_state(rdev->pdev);
2122 r100_enable_bm(rdev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002123 /* Check if GPU is idle */
Jerome Glisse90aca4d2010-03-09 14:45:12 +00002124 if (G_000E40_SE_BUSY(status) || G_000E40_RE_BUSY(status) ||
2125 G_000E40_TAM_BUSY(status) || G_000E40_PB_BUSY(status)) {
2126 dev_err(rdev->dev, "failed to reset GPU\n");
2127 rdev->gpu_lockup = true;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002128 return -1;
2129 }
Jerome Glisse90aca4d2010-03-09 14:45:12 +00002130 r100_mc_resume(rdev, &save);
2131 dev_info(rdev->dev, "GPU reset succeed\n");
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002132 return 0;
2133}
2134
Alex Deucher92cde002009-12-04 10:55:12 -05002135void r100_set_common_regs(struct radeon_device *rdev)
2136{
Alex Deucher2739d492010-02-05 03:34:16 -05002137 struct drm_device *dev = rdev->ddev;
2138 bool force_dac2 = false;
Dave Airlied6680462010-03-31 13:41:35 +10002139 u32 tmp;
Alex Deucher2739d492010-02-05 03:34:16 -05002140
Alex Deucher92cde002009-12-04 10:55:12 -05002141 /* set these so they don't interfere with anything */
2142 WREG32(RADEON_OV0_SCALE_CNTL, 0);
2143 WREG32(RADEON_SUBPIC_CNTL, 0);
2144 WREG32(RADEON_VIPH_CONTROL, 0);
2145 WREG32(RADEON_I2C_CNTL_1, 0);
2146 WREG32(RADEON_DVI_I2C_CNTL_1, 0);
2147 WREG32(RADEON_CAP0_TRIG_CNTL, 0);
2148 WREG32(RADEON_CAP1_TRIG_CNTL, 0);
Alex Deucher2739d492010-02-05 03:34:16 -05002149
2150 /* always set up dac2 on rn50 and some rv100 as lots
2151 * of servers seem to wire it up to a VGA port but
2152 * don't report it in the bios connector
2153 * table.
2154 */
2155 switch (dev->pdev->device) {
2156 /* RN50 */
2157 case 0x515e:
2158 case 0x5969:
2159 force_dac2 = true;
2160 break;
2161 /* RV100*/
2162 case 0x5159:
2163 case 0x515a:
2164 /* DELL triple head servers */
2165 if ((dev->pdev->subsystem_vendor == 0x1028 /* DELL */) &&
2166 ((dev->pdev->subsystem_device == 0x016c) ||
2167 (dev->pdev->subsystem_device == 0x016d) ||
2168 (dev->pdev->subsystem_device == 0x016e) ||
2169 (dev->pdev->subsystem_device == 0x016f) ||
2170 (dev->pdev->subsystem_device == 0x0170) ||
2171 (dev->pdev->subsystem_device == 0x017d) ||
2172 (dev->pdev->subsystem_device == 0x017e) ||
2173 (dev->pdev->subsystem_device == 0x0183) ||
2174 (dev->pdev->subsystem_device == 0x018a) ||
2175 (dev->pdev->subsystem_device == 0x019a)))
2176 force_dac2 = true;
2177 break;
2178 }
2179
2180 if (force_dac2) {
2181 u32 disp_hw_debug = RREG32(RADEON_DISP_HW_DEBUG);
2182 u32 tv_dac_cntl = RREG32(RADEON_TV_DAC_CNTL);
2183 u32 dac2_cntl = RREG32(RADEON_DAC_CNTL2);
2184
2185 /* For CRT on DAC2, don't turn it on if BIOS didn't
2186 enable it, even it's detected.
2187 */
2188
2189 /* force it to crtc0 */
2190 dac2_cntl &= ~RADEON_DAC2_DAC_CLK_SEL;
2191 dac2_cntl |= RADEON_DAC2_DAC2_CLK_SEL;
2192 disp_hw_debug |= RADEON_CRT2_DISP1_SEL;
2193
2194 /* set up the TV DAC */
2195 tv_dac_cntl &= ~(RADEON_TV_DAC_PEDESTAL |
2196 RADEON_TV_DAC_STD_MASK |
2197 RADEON_TV_DAC_RDACPD |
2198 RADEON_TV_DAC_GDACPD |
2199 RADEON_TV_DAC_BDACPD |
2200 RADEON_TV_DAC_BGADJ_MASK |
2201 RADEON_TV_DAC_DACADJ_MASK);
2202 tv_dac_cntl |= (RADEON_TV_DAC_NBLANK |
2203 RADEON_TV_DAC_NHOLD |
2204 RADEON_TV_DAC_STD_PS2 |
2205 (0x58 << 16));
2206
2207 WREG32(RADEON_TV_DAC_CNTL, tv_dac_cntl);
2208 WREG32(RADEON_DISP_HW_DEBUG, disp_hw_debug);
2209 WREG32(RADEON_DAC_CNTL2, dac2_cntl);
2210 }
Dave Airlied6680462010-03-31 13:41:35 +10002211
2212 /* switch PM block to ACPI mode */
2213 tmp = RREG32_PLL(RADEON_PLL_PWRMGT_CNTL);
2214 tmp &= ~RADEON_PM_MODE_SEL;
2215 WREG32_PLL(RADEON_PLL_PWRMGT_CNTL, tmp);
2216
Alex Deucher92cde002009-12-04 10:55:12 -05002217}
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002218
2219/*
2220 * VRAM info
2221 */
2222static void r100_vram_get_type(struct radeon_device *rdev)
2223{
2224 uint32_t tmp;
2225
2226 rdev->mc.vram_is_ddr = false;
2227 if (rdev->flags & RADEON_IS_IGP)
2228 rdev->mc.vram_is_ddr = true;
2229 else if (RREG32(RADEON_MEM_SDRAM_MODE_REG) & RADEON_MEM_CFG_TYPE_DDR)
2230 rdev->mc.vram_is_ddr = true;
2231 if ((rdev->family == CHIP_RV100) ||
2232 (rdev->family == CHIP_RS100) ||
2233 (rdev->family == CHIP_RS200)) {
2234 tmp = RREG32(RADEON_MEM_CNTL);
2235 if (tmp & RV100_HALF_MODE) {
2236 rdev->mc.vram_width = 32;
2237 } else {
2238 rdev->mc.vram_width = 64;
2239 }
2240 if (rdev->flags & RADEON_SINGLE_CRTC) {
2241 rdev->mc.vram_width /= 4;
2242 rdev->mc.vram_is_ddr = true;
2243 }
2244 } else if (rdev->family <= CHIP_RV280) {
2245 tmp = RREG32(RADEON_MEM_CNTL);
2246 if (tmp & RADEON_MEM_NUM_CHANNELS_MASK) {
2247 rdev->mc.vram_width = 128;
2248 } else {
2249 rdev->mc.vram_width = 64;
2250 }
2251 } else {
2252 /* newer IGPs */
2253 rdev->mc.vram_width = 128;
2254 }
2255}
2256
Dave Airlie2a0f8912009-07-11 04:44:47 +10002257static u32 r100_get_accessible_vram(struct radeon_device *rdev)
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002258{
Dave Airlie2a0f8912009-07-11 04:44:47 +10002259 u32 aper_size;
2260 u8 byte;
2261
2262 aper_size = RREG32(RADEON_CONFIG_APER_SIZE);
2263
2264 /* Set HDP_APER_CNTL only on cards that are known not to be broken,
2265 * that is has the 2nd generation multifunction PCI interface
2266 */
2267 if (rdev->family == CHIP_RV280 ||
2268 rdev->family >= CHIP_RV350) {
2269 WREG32_P(RADEON_HOST_PATH_CNTL, RADEON_HDP_APER_CNTL,
2270 ~RADEON_HDP_APER_CNTL);
2271 DRM_INFO("Generation 2 PCI interface, using max accessible memory\n");
2272 return aper_size * 2;
2273 }
2274
2275 /* Older cards have all sorts of funny issues to deal with. First
2276 * check if it's a multifunction card by reading the PCI config
2277 * header type... Limit those to one aperture size
2278 */
2279 pci_read_config_byte(rdev->pdev, 0xe, &byte);
2280 if (byte & 0x80) {
2281 DRM_INFO("Generation 1 PCI interface in multifunction mode\n");
2282 DRM_INFO("Limiting VRAM to one aperture\n");
2283 return aper_size;
2284 }
2285
2286 /* Single function older card. We read HDP_APER_CNTL to see how the BIOS
2287 * have set it up. We don't write this as it's broken on some ASICs but
2288 * we expect the BIOS to have done the right thing (might be too optimistic...)
2289 */
2290 if (RREG32(RADEON_HOST_PATH_CNTL) & RADEON_HDP_APER_CNTL)
2291 return aper_size * 2;
2292 return aper_size;
2293}
2294
2295void r100_vram_init_sizes(struct radeon_device *rdev)
2296{
2297 u64 config_aper_size;
Dave Airlie2a0f8912009-07-11 04:44:47 +10002298
Jerome Glissed594e462010-02-17 21:54:29 +00002299 /* work out accessible VRAM */
Jordan Crouse01d73a62010-05-27 13:40:24 -06002300 rdev->mc.aper_base = pci_resource_start(rdev->pdev, 0);
2301 rdev->mc.aper_size = pci_resource_len(rdev->pdev, 0);
Jerome Glisse51e5fcd2010-02-19 14:33:54 +00002302 rdev->mc.visible_vram_size = r100_get_accessible_vram(rdev);
2303 /* FIXME we don't use the second aperture yet when we could use it */
2304 if (rdev->mc.visible_vram_size > rdev->mc.aper_size)
2305 rdev->mc.visible_vram_size = rdev->mc.aper_size;
Dave Airlie2a0f8912009-07-11 04:44:47 +10002306 config_aper_size = RREG32(RADEON_CONFIG_APER_SIZE);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002307 if (rdev->flags & RADEON_IS_IGP) {
2308 uint32_t tom;
2309 /* read NB_TOM to get the amount of ram stolen for the GPU */
2310 tom = RREG32(RADEON_NB_TOM);
Dave Airlie7a50f012009-07-21 20:39:30 +10002311 rdev->mc.real_vram_size = (((tom >> 16) - (tom & 0xffff) + 1) << 16);
Dave Airlie7a50f012009-07-21 20:39:30 +10002312 WREG32(RADEON_CONFIG_MEMSIZE, rdev->mc.real_vram_size);
2313 rdev->mc.mc_vram_size = rdev->mc.real_vram_size;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002314 } else {
Dave Airlie7a50f012009-07-21 20:39:30 +10002315 rdev->mc.real_vram_size = RREG32(RADEON_CONFIG_MEMSIZE);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002316 /* Some production boards of m6 will report 0
2317 * if it's 8 MB
2318 */
Dave Airlie7a50f012009-07-21 20:39:30 +10002319 if (rdev->mc.real_vram_size == 0) {
2320 rdev->mc.real_vram_size = 8192 * 1024;
2321 WREG32(RADEON_CONFIG_MEMSIZE, rdev->mc.real_vram_size);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002322 }
Jerome Glissed594e462010-02-17 21:54:29 +00002323 /* Fix for RN50, M6, M7 with 8/16/32(??) MBs of VRAM -
2324 * Novell bug 204882 + along with lots of ubuntu ones
2325 */
Dave Airlie7a50f012009-07-21 20:39:30 +10002326 if (config_aper_size > rdev->mc.real_vram_size)
2327 rdev->mc.mc_vram_size = config_aper_size;
2328 else
2329 rdev->mc.mc_vram_size = rdev->mc.real_vram_size;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002330 }
Dave Airlie2a0f8912009-07-11 04:44:47 +10002331}
2332
Dave Airlie28d52042009-09-21 14:33:58 +10002333void r100_vga_set_state(struct radeon_device *rdev, bool state)
2334{
2335 uint32_t temp;
2336
2337 temp = RREG32(RADEON_CONFIG_CNTL);
2338 if (state == false) {
2339 temp &= ~(1<<8);
2340 temp |= (1<<9);
2341 } else {
2342 temp &= ~(1<<9);
2343 }
2344 WREG32(RADEON_CONFIG_CNTL, temp);
2345}
2346
Jerome Glissed594e462010-02-17 21:54:29 +00002347void r100_mc_init(struct radeon_device *rdev)
Dave Airlie2a0f8912009-07-11 04:44:47 +10002348{
Jerome Glissed594e462010-02-17 21:54:29 +00002349 u64 base;
Dave Airlie2a0f8912009-07-11 04:44:47 +10002350
Jerome Glissed594e462010-02-17 21:54:29 +00002351 r100_vram_get_type(rdev);
Dave Airlie2a0f8912009-07-11 04:44:47 +10002352 r100_vram_init_sizes(rdev);
Jerome Glissed594e462010-02-17 21:54:29 +00002353 base = rdev->mc.aper_base;
2354 if (rdev->flags & RADEON_IS_IGP)
2355 base = (RREG32(RADEON_NB_TOM) & 0xffff) << 16;
2356 radeon_vram_location(rdev, &rdev->mc, base);
2357 if (!(rdev->flags & RADEON_IS_AGP))
2358 radeon_gtt_location(rdev, &rdev->mc);
Alex Deucherf47299c2010-03-16 20:54:38 -04002359 radeon_update_bandwidth_info(rdev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002360}
2361
2362
2363/*
2364 * Indirect registers accessor
2365 */
2366void r100_pll_errata_after_index(struct radeon_device *rdev)
2367{
Alex Deucher4ce91982010-06-30 12:13:55 -04002368 if (rdev->pll_errata & CHIP_ERRATA_PLL_DUMMYREADS) {
2369 (void)RREG32(RADEON_CLOCK_CNTL_DATA);
2370 (void)RREG32(RADEON_CRTC_GEN_CNTL);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002371 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002372}
2373
2374static void r100_pll_errata_after_data(struct radeon_device *rdev)
2375{
2376 /* This workarounds is necessary on RV100, RS100 and RS200 chips
2377 * or the chip could hang on a subsequent access
2378 */
2379 if (rdev->pll_errata & CHIP_ERRATA_PLL_DELAY) {
2380 udelay(5000);
2381 }
2382
2383 /* This function is required to workaround a hardware bug in some (all?)
2384 * revisions of the R300. This workaround should be called after every
2385 * CLOCK_CNTL_INDEX register access. If not, register reads afterward
2386 * may not be correct.
2387 */
2388 if (rdev->pll_errata & CHIP_ERRATA_R300_CG) {
2389 uint32_t save, tmp;
2390
2391 save = RREG32(RADEON_CLOCK_CNTL_INDEX);
2392 tmp = save & ~(0x3f | RADEON_PLL_WR_EN);
2393 WREG32(RADEON_CLOCK_CNTL_INDEX, tmp);
2394 tmp = RREG32(RADEON_CLOCK_CNTL_DATA);
2395 WREG32(RADEON_CLOCK_CNTL_INDEX, save);
2396 }
2397}
2398
2399uint32_t r100_pll_rreg(struct radeon_device *rdev, uint32_t reg)
2400{
2401 uint32_t data;
2402
2403 WREG8(RADEON_CLOCK_CNTL_INDEX, reg & 0x3f);
2404 r100_pll_errata_after_index(rdev);
2405 data = RREG32(RADEON_CLOCK_CNTL_DATA);
2406 r100_pll_errata_after_data(rdev);
2407 return data;
2408}
2409
2410void r100_pll_wreg(struct radeon_device *rdev, uint32_t reg, uint32_t v)
2411{
2412 WREG8(RADEON_CLOCK_CNTL_INDEX, ((reg & 0x3f) | RADEON_PLL_WR_EN));
2413 r100_pll_errata_after_index(rdev);
2414 WREG32(RADEON_CLOCK_CNTL_DATA, v);
2415 r100_pll_errata_after_data(rdev);
2416}
2417
Jerome Glissed4550902009-10-01 10:12:06 +02002418void r100_set_safe_registers(struct radeon_device *rdev)
Jerome Glisse068a1172009-06-17 13:28:30 +02002419{
Dave Airlie551ebd82009-09-01 15:25:57 +10002420 if (ASIC_IS_RN50(rdev)) {
2421 rdev->config.r100.reg_safe_bm = rn50_reg_safe_bm;
2422 rdev->config.r100.reg_safe_bm_size = ARRAY_SIZE(rn50_reg_safe_bm);
2423 } else if (rdev->family < CHIP_R200) {
2424 rdev->config.r100.reg_safe_bm = r100_reg_safe_bm;
2425 rdev->config.r100.reg_safe_bm_size = ARRAY_SIZE(r100_reg_safe_bm);
2426 } else {
Jerome Glissed4550902009-10-01 10:12:06 +02002427 r200_set_safe_registers(rdev);
Dave Airlie551ebd82009-09-01 15:25:57 +10002428 }
Jerome Glisse068a1172009-06-17 13:28:30 +02002429}
2430
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002431/*
2432 * Debugfs info
2433 */
2434#if defined(CONFIG_DEBUG_FS)
2435static int r100_debugfs_rbbm_info(struct seq_file *m, void *data)
2436{
2437 struct drm_info_node *node = (struct drm_info_node *) m->private;
2438 struct drm_device *dev = node->minor->dev;
2439 struct radeon_device *rdev = dev->dev_private;
2440 uint32_t reg, value;
2441 unsigned i;
2442
2443 seq_printf(m, "RBBM_STATUS 0x%08x\n", RREG32(RADEON_RBBM_STATUS));
2444 seq_printf(m, "RBBM_CMDFIFO_STAT 0x%08x\n", RREG32(0xE7C));
2445 seq_printf(m, "CP_STAT 0x%08x\n", RREG32(RADEON_CP_STAT));
2446 for (i = 0; i < 64; i++) {
2447 WREG32(RADEON_RBBM_CMDFIFO_ADDR, i | 0x100);
2448 reg = (RREG32(RADEON_RBBM_CMDFIFO_DATA) - 1) >> 2;
2449 WREG32(RADEON_RBBM_CMDFIFO_ADDR, i);
2450 value = RREG32(RADEON_RBBM_CMDFIFO_DATA);
2451 seq_printf(m, "[0x%03X] 0x%04X=0x%08X\n", i, reg, value);
2452 }
2453 return 0;
2454}
2455
2456static int r100_debugfs_cp_ring_info(struct seq_file *m, void *data)
2457{
2458 struct drm_info_node *node = (struct drm_info_node *) m->private;
2459 struct drm_device *dev = node->minor->dev;
2460 struct radeon_device *rdev = dev->dev_private;
2461 uint32_t rdp, wdp;
2462 unsigned count, i, j;
2463
2464 radeon_ring_free_size(rdev);
2465 rdp = RREG32(RADEON_CP_RB_RPTR);
2466 wdp = RREG32(RADEON_CP_RB_WPTR);
2467 count = (rdp + rdev->cp.ring_size - wdp) & rdev->cp.ptr_mask;
2468 seq_printf(m, "CP_STAT 0x%08x\n", RREG32(RADEON_CP_STAT));
2469 seq_printf(m, "CP_RB_WPTR 0x%08x\n", wdp);
2470 seq_printf(m, "CP_RB_RPTR 0x%08x\n", rdp);
2471 seq_printf(m, "%u free dwords in ring\n", rdev->cp.ring_free_dw);
2472 seq_printf(m, "%u dwords in ring\n", count);
2473 for (j = 0; j <= count; j++) {
2474 i = (rdp + j) & rdev->cp.ptr_mask;
2475 seq_printf(m, "r[%04d]=0x%08x\n", i, rdev->cp.ring[i]);
2476 }
2477 return 0;
2478}
2479
2480
2481static int r100_debugfs_cp_csq_fifo(struct seq_file *m, void *data)
2482{
2483 struct drm_info_node *node = (struct drm_info_node *) m->private;
2484 struct drm_device *dev = node->minor->dev;
2485 struct radeon_device *rdev = dev->dev_private;
2486 uint32_t csq_stat, csq2_stat, tmp;
2487 unsigned r_rptr, r_wptr, ib1_rptr, ib1_wptr, ib2_rptr, ib2_wptr;
2488 unsigned i;
2489
2490 seq_printf(m, "CP_STAT 0x%08x\n", RREG32(RADEON_CP_STAT));
2491 seq_printf(m, "CP_CSQ_MODE 0x%08x\n", RREG32(RADEON_CP_CSQ_MODE));
2492 csq_stat = RREG32(RADEON_CP_CSQ_STAT);
2493 csq2_stat = RREG32(RADEON_CP_CSQ2_STAT);
2494 r_rptr = (csq_stat >> 0) & 0x3ff;
2495 r_wptr = (csq_stat >> 10) & 0x3ff;
2496 ib1_rptr = (csq_stat >> 20) & 0x3ff;
2497 ib1_wptr = (csq2_stat >> 0) & 0x3ff;
2498 ib2_rptr = (csq2_stat >> 10) & 0x3ff;
2499 ib2_wptr = (csq2_stat >> 20) & 0x3ff;
2500 seq_printf(m, "CP_CSQ_STAT 0x%08x\n", csq_stat);
2501 seq_printf(m, "CP_CSQ2_STAT 0x%08x\n", csq2_stat);
2502 seq_printf(m, "Ring rptr %u\n", r_rptr);
2503 seq_printf(m, "Ring wptr %u\n", r_wptr);
2504 seq_printf(m, "Indirect1 rptr %u\n", ib1_rptr);
2505 seq_printf(m, "Indirect1 wptr %u\n", ib1_wptr);
2506 seq_printf(m, "Indirect2 rptr %u\n", ib2_rptr);
2507 seq_printf(m, "Indirect2 wptr %u\n", ib2_wptr);
2508 /* FIXME: 0, 128, 640 depends on fifo setup see cp_init_kms
2509 * 128 = indirect1_start * 8 & 640 = indirect2_start * 8 */
2510 seq_printf(m, "Ring fifo:\n");
2511 for (i = 0; i < 256; i++) {
2512 WREG32(RADEON_CP_CSQ_ADDR, i << 2);
2513 tmp = RREG32(RADEON_CP_CSQ_DATA);
2514 seq_printf(m, "rfifo[%04d]=0x%08X\n", i, tmp);
2515 }
2516 seq_printf(m, "Indirect1 fifo:\n");
2517 for (i = 256; i <= 512; i++) {
2518 WREG32(RADEON_CP_CSQ_ADDR, i << 2);
2519 tmp = RREG32(RADEON_CP_CSQ_DATA);
2520 seq_printf(m, "ib1fifo[%04d]=0x%08X\n", i, tmp);
2521 }
2522 seq_printf(m, "Indirect2 fifo:\n");
2523 for (i = 640; i < ib1_wptr; i++) {
2524 WREG32(RADEON_CP_CSQ_ADDR, i << 2);
2525 tmp = RREG32(RADEON_CP_CSQ_DATA);
2526 seq_printf(m, "ib2fifo[%04d]=0x%08X\n", i, tmp);
2527 }
2528 return 0;
2529}
2530
2531static int r100_debugfs_mc_info(struct seq_file *m, void *data)
2532{
2533 struct drm_info_node *node = (struct drm_info_node *) m->private;
2534 struct drm_device *dev = node->minor->dev;
2535 struct radeon_device *rdev = dev->dev_private;
2536 uint32_t tmp;
2537
2538 tmp = RREG32(RADEON_CONFIG_MEMSIZE);
2539 seq_printf(m, "CONFIG_MEMSIZE 0x%08x\n", tmp);
2540 tmp = RREG32(RADEON_MC_FB_LOCATION);
2541 seq_printf(m, "MC_FB_LOCATION 0x%08x\n", tmp);
2542 tmp = RREG32(RADEON_BUS_CNTL);
2543 seq_printf(m, "BUS_CNTL 0x%08x\n", tmp);
2544 tmp = RREG32(RADEON_MC_AGP_LOCATION);
2545 seq_printf(m, "MC_AGP_LOCATION 0x%08x\n", tmp);
2546 tmp = RREG32(RADEON_AGP_BASE);
2547 seq_printf(m, "AGP_BASE 0x%08x\n", tmp);
2548 tmp = RREG32(RADEON_HOST_PATH_CNTL);
2549 seq_printf(m, "HOST_PATH_CNTL 0x%08x\n", tmp);
2550 tmp = RREG32(0x01D0);
2551 seq_printf(m, "AIC_CTRL 0x%08x\n", tmp);
2552 tmp = RREG32(RADEON_AIC_LO_ADDR);
2553 seq_printf(m, "AIC_LO_ADDR 0x%08x\n", tmp);
2554 tmp = RREG32(RADEON_AIC_HI_ADDR);
2555 seq_printf(m, "AIC_HI_ADDR 0x%08x\n", tmp);
2556 tmp = RREG32(0x01E4);
2557 seq_printf(m, "AIC_TLB_ADDR 0x%08x\n", tmp);
2558 return 0;
2559}
2560
2561static struct drm_info_list r100_debugfs_rbbm_list[] = {
2562 {"r100_rbbm_info", r100_debugfs_rbbm_info, 0, NULL},
2563};
2564
2565static struct drm_info_list r100_debugfs_cp_list[] = {
2566 {"r100_cp_ring_info", r100_debugfs_cp_ring_info, 0, NULL},
2567 {"r100_cp_csq_fifo", r100_debugfs_cp_csq_fifo, 0, NULL},
2568};
2569
2570static struct drm_info_list r100_debugfs_mc_info_list[] = {
2571 {"r100_mc_info", r100_debugfs_mc_info, 0, NULL},
2572};
2573#endif
2574
2575int r100_debugfs_rbbm_init(struct radeon_device *rdev)
2576{
2577#if defined(CONFIG_DEBUG_FS)
2578 return radeon_debugfs_add_files(rdev, r100_debugfs_rbbm_list, 1);
2579#else
2580 return 0;
2581#endif
2582}
2583
2584int r100_debugfs_cp_init(struct radeon_device *rdev)
2585{
2586#if defined(CONFIG_DEBUG_FS)
2587 return radeon_debugfs_add_files(rdev, r100_debugfs_cp_list, 2);
2588#else
2589 return 0;
2590#endif
2591}
2592
2593int r100_debugfs_mc_info_init(struct radeon_device *rdev)
2594{
2595#if defined(CONFIG_DEBUG_FS)
2596 return radeon_debugfs_add_files(rdev, r100_debugfs_mc_info_list, 1);
2597#else
2598 return 0;
2599#endif
2600}
Dave Airliee024e112009-06-24 09:48:08 +10002601
2602int r100_set_surface_reg(struct radeon_device *rdev, int reg,
2603 uint32_t tiling_flags, uint32_t pitch,
2604 uint32_t offset, uint32_t obj_size)
2605{
2606 int surf_index = reg * 16;
2607 int flags = 0;
2608
Dave Airliee024e112009-06-24 09:48:08 +10002609 if (rdev->family <= CHIP_RS200) {
2610 if ((tiling_flags & (RADEON_TILING_MACRO|RADEON_TILING_MICRO))
2611 == (RADEON_TILING_MACRO|RADEON_TILING_MICRO))
2612 flags |= RADEON_SURF_TILE_COLOR_BOTH;
2613 if (tiling_flags & RADEON_TILING_MACRO)
2614 flags |= RADEON_SURF_TILE_COLOR_MACRO;
2615 } else if (rdev->family <= CHIP_RV280) {
2616 if (tiling_flags & (RADEON_TILING_MACRO))
2617 flags |= R200_SURF_TILE_COLOR_MACRO;
2618 if (tiling_flags & RADEON_TILING_MICRO)
2619 flags |= R200_SURF_TILE_COLOR_MICRO;
2620 } else {
2621 if (tiling_flags & RADEON_TILING_MACRO)
2622 flags |= R300_SURF_TILE_MACRO;
2623 if (tiling_flags & RADEON_TILING_MICRO)
2624 flags |= R300_SURF_TILE_MICRO;
2625 }
2626
Michel Dänzerc88f9f02009-09-15 17:09:30 +02002627 if (tiling_flags & RADEON_TILING_SWAP_16BIT)
2628 flags |= RADEON_SURF_AP0_SWP_16BPP | RADEON_SURF_AP1_SWP_16BPP;
2629 if (tiling_flags & RADEON_TILING_SWAP_32BIT)
2630 flags |= RADEON_SURF_AP0_SWP_32BPP | RADEON_SURF_AP1_SWP_32BPP;
2631
Dave Airlief5c5f042010-06-11 14:40:16 +10002632 /* when we aren't tiling the pitch seems to needs to be furtherdivided down. - tested on power5 + rn50 server */
2633 if (tiling_flags & (RADEON_TILING_SWAP_16BIT | RADEON_TILING_SWAP_32BIT)) {
2634 if (!(tiling_flags & (RADEON_TILING_MACRO | RADEON_TILING_MICRO)))
2635 if (ASIC_IS_RN50(rdev))
2636 pitch /= 16;
2637 }
2638
2639 /* r100/r200 divide by 16 */
2640 if (rdev->family < CHIP_R300)
2641 flags |= pitch / 16;
2642 else
2643 flags |= pitch / 8;
2644
2645
Dave Airliee024e112009-06-24 09:48:08 +10002646 DRM_DEBUG("writing surface %d %d %x %x\n", reg, flags, offset, offset+obj_size-1);
2647 WREG32(RADEON_SURFACE0_INFO + surf_index, flags);
2648 WREG32(RADEON_SURFACE0_LOWER_BOUND + surf_index, offset);
2649 WREG32(RADEON_SURFACE0_UPPER_BOUND + surf_index, offset + obj_size - 1);
2650 return 0;
2651}
2652
2653void r100_clear_surface_reg(struct radeon_device *rdev, int reg)
2654{
2655 int surf_index = reg * 16;
2656 WREG32(RADEON_SURFACE0_INFO + surf_index, 0);
2657}
Jerome Glissec93bb852009-07-13 21:04:08 +02002658
2659void r100_bandwidth_update(struct radeon_device *rdev)
2660{
2661 fixed20_12 trcd_ff, trp_ff, tras_ff, trbs_ff, tcas_ff;
2662 fixed20_12 sclk_ff, mclk_ff, sclk_eff_ff, sclk_delay_ff;
2663 fixed20_12 peak_disp_bw, mem_bw, pix_clk, pix_clk2, temp_ff, crit_point_ff;
2664 uint32_t temp, data, mem_trcd, mem_trp, mem_tras;
2665 fixed20_12 memtcas_ff[8] = {
Ben Skeggs68adac52010-04-28 11:46:42 +10002666 dfixed_init(1),
2667 dfixed_init(2),
2668 dfixed_init(3),
2669 dfixed_init(0),
2670 dfixed_init_half(1),
2671 dfixed_init_half(2),
2672 dfixed_init(0),
Jerome Glissec93bb852009-07-13 21:04:08 +02002673 };
2674 fixed20_12 memtcas_rs480_ff[8] = {
Ben Skeggs68adac52010-04-28 11:46:42 +10002675 dfixed_init(0),
2676 dfixed_init(1),
2677 dfixed_init(2),
2678 dfixed_init(3),
2679 dfixed_init(0),
2680 dfixed_init_half(1),
2681 dfixed_init_half(2),
2682 dfixed_init_half(3),
Jerome Glissec93bb852009-07-13 21:04:08 +02002683 };
2684 fixed20_12 memtcas2_ff[8] = {
Ben Skeggs68adac52010-04-28 11:46:42 +10002685 dfixed_init(0),
2686 dfixed_init(1),
2687 dfixed_init(2),
2688 dfixed_init(3),
2689 dfixed_init(4),
2690 dfixed_init(5),
2691 dfixed_init(6),
2692 dfixed_init(7),
Jerome Glissec93bb852009-07-13 21:04:08 +02002693 };
2694 fixed20_12 memtrbs[8] = {
Ben Skeggs68adac52010-04-28 11:46:42 +10002695 dfixed_init(1),
2696 dfixed_init_half(1),
2697 dfixed_init(2),
2698 dfixed_init_half(2),
2699 dfixed_init(3),
2700 dfixed_init_half(3),
2701 dfixed_init(4),
2702 dfixed_init_half(4)
Jerome Glissec93bb852009-07-13 21:04:08 +02002703 };
2704 fixed20_12 memtrbs_r4xx[8] = {
Ben Skeggs68adac52010-04-28 11:46:42 +10002705 dfixed_init(4),
2706 dfixed_init(5),
2707 dfixed_init(6),
2708 dfixed_init(7),
2709 dfixed_init(8),
2710 dfixed_init(9),
2711 dfixed_init(10),
2712 dfixed_init(11)
Jerome Glissec93bb852009-07-13 21:04:08 +02002713 };
2714 fixed20_12 min_mem_eff;
2715 fixed20_12 mc_latency_sclk, mc_latency_mclk, k1;
2716 fixed20_12 cur_latency_mclk, cur_latency_sclk;
2717 fixed20_12 disp_latency, disp_latency_overhead, disp_drain_rate,
2718 disp_drain_rate2, read_return_rate;
2719 fixed20_12 time_disp1_drop_priority;
2720 int c;
2721 int cur_size = 16; /* in octawords */
2722 int critical_point = 0, critical_point2;
2723/* uint32_t read_return_rate, time_disp1_drop_priority; */
2724 int stop_req, max_stop_req;
2725 struct drm_display_mode *mode1 = NULL;
2726 struct drm_display_mode *mode2 = NULL;
2727 uint32_t pixel_bytes1 = 0;
2728 uint32_t pixel_bytes2 = 0;
2729
Alex Deucherf46c0122010-03-31 00:33:27 -04002730 radeon_update_display_priority(rdev);
2731
Jerome Glissec93bb852009-07-13 21:04:08 +02002732 if (rdev->mode_info.crtcs[0]->base.enabled) {
2733 mode1 = &rdev->mode_info.crtcs[0]->base.mode;
2734 pixel_bytes1 = rdev->mode_info.crtcs[0]->base.fb->bits_per_pixel / 8;
2735 }
Dave Airliedfee5612009-10-02 09:19:09 +10002736 if (!(rdev->flags & RADEON_SINGLE_CRTC)) {
2737 if (rdev->mode_info.crtcs[1]->base.enabled) {
2738 mode2 = &rdev->mode_info.crtcs[1]->base.mode;
2739 pixel_bytes2 = rdev->mode_info.crtcs[1]->base.fb->bits_per_pixel / 8;
2740 }
Jerome Glissec93bb852009-07-13 21:04:08 +02002741 }
2742
Ben Skeggs68adac52010-04-28 11:46:42 +10002743 min_mem_eff.full = dfixed_const_8(0);
Jerome Glissec93bb852009-07-13 21:04:08 +02002744 /* get modes */
2745 if ((rdev->disp_priority == 2) && ASIC_IS_R300(rdev)) {
2746 uint32_t mc_init_misc_lat_timer = RREG32(R300_MC_INIT_MISC_LAT_TIMER);
2747 mc_init_misc_lat_timer &= ~(R300_MC_DISP1R_INIT_LAT_MASK << R300_MC_DISP1R_INIT_LAT_SHIFT);
2748 mc_init_misc_lat_timer &= ~(R300_MC_DISP0R_INIT_LAT_MASK << R300_MC_DISP0R_INIT_LAT_SHIFT);
2749 /* check crtc enables */
2750 if (mode2)
2751 mc_init_misc_lat_timer |= (1 << R300_MC_DISP1R_INIT_LAT_SHIFT);
2752 if (mode1)
2753 mc_init_misc_lat_timer |= (1 << R300_MC_DISP0R_INIT_LAT_SHIFT);
2754 WREG32(R300_MC_INIT_MISC_LAT_TIMER, mc_init_misc_lat_timer);
2755 }
2756
2757 /*
2758 * determine is there is enough bw for current mode
2759 */
Alex Deucherf47299c2010-03-16 20:54:38 -04002760 sclk_ff = rdev->pm.sclk;
2761 mclk_ff = rdev->pm.mclk;
Jerome Glissec93bb852009-07-13 21:04:08 +02002762
2763 temp = (rdev->mc.vram_width / 8) * (rdev->mc.vram_is_ddr ? 2 : 1);
Ben Skeggs68adac52010-04-28 11:46:42 +10002764 temp_ff.full = dfixed_const(temp);
2765 mem_bw.full = dfixed_mul(mclk_ff, temp_ff);
Jerome Glissec93bb852009-07-13 21:04:08 +02002766
2767 pix_clk.full = 0;
2768 pix_clk2.full = 0;
2769 peak_disp_bw.full = 0;
2770 if (mode1) {
Ben Skeggs68adac52010-04-28 11:46:42 +10002771 temp_ff.full = dfixed_const(1000);
2772 pix_clk.full = dfixed_const(mode1->clock); /* convert to fixed point */
2773 pix_clk.full = dfixed_div(pix_clk, temp_ff);
2774 temp_ff.full = dfixed_const(pixel_bytes1);
2775 peak_disp_bw.full += dfixed_mul(pix_clk, temp_ff);
Jerome Glissec93bb852009-07-13 21:04:08 +02002776 }
2777 if (mode2) {
Ben Skeggs68adac52010-04-28 11:46:42 +10002778 temp_ff.full = dfixed_const(1000);
2779 pix_clk2.full = dfixed_const(mode2->clock); /* convert to fixed point */
2780 pix_clk2.full = dfixed_div(pix_clk2, temp_ff);
2781 temp_ff.full = dfixed_const(pixel_bytes2);
2782 peak_disp_bw.full += dfixed_mul(pix_clk2, temp_ff);
Jerome Glissec93bb852009-07-13 21:04:08 +02002783 }
2784
Ben Skeggs68adac52010-04-28 11:46:42 +10002785 mem_bw.full = dfixed_mul(mem_bw, min_mem_eff);
Jerome Glissec93bb852009-07-13 21:04:08 +02002786 if (peak_disp_bw.full >= mem_bw.full) {
2787 DRM_ERROR("You may not have enough display bandwidth for current mode\n"
2788 "If you have flickering problem, try to lower resolution, refresh rate, or color depth\n");
2789 }
2790
2791 /* Get values from the EXT_MEM_CNTL register...converting its contents. */
2792 temp = RREG32(RADEON_MEM_TIMING_CNTL);
2793 if ((rdev->family == CHIP_RV100) || (rdev->flags & RADEON_IS_IGP)) { /* RV100, M6, IGPs */
2794 mem_trcd = ((temp >> 2) & 0x3) + 1;
2795 mem_trp = ((temp & 0x3)) + 1;
2796 mem_tras = ((temp & 0x70) >> 4) + 1;
2797 } else if (rdev->family == CHIP_R300 ||
2798 rdev->family == CHIP_R350) { /* r300, r350 */
2799 mem_trcd = (temp & 0x7) + 1;
2800 mem_trp = ((temp >> 8) & 0x7) + 1;
2801 mem_tras = ((temp >> 11) & 0xf) + 4;
2802 } else if (rdev->family == CHIP_RV350 ||
2803 rdev->family <= CHIP_RV380) {
2804 /* rv3x0 */
2805 mem_trcd = (temp & 0x7) + 3;
2806 mem_trp = ((temp >> 8) & 0x7) + 3;
2807 mem_tras = ((temp >> 11) & 0xf) + 6;
2808 } else if (rdev->family == CHIP_R420 ||
2809 rdev->family == CHIP_R423 ||
2810 rdev->family == CHIP_RV410) {
2811 /* r4xx */
2812 mem_trcd = (temp & 0xf) + 3;
2813 if (mem_trcd > 15)
2814 mem_trcd = 15;
2815 mem_trp = ((temp >> 8) & 0xf) + 3;
2816 if (mem_trp > 15)
2817 mem_trp = 15;
2818 mem_tras = ((temp >> 12) & 0x1f) + 6;
2819 if (mem_tras > 31)
2820 mem_tras = 31;
2821 } else { /* RV200, R200 */
2822 mem_trcd = (temp & 0x7) + 1;
2823 mem_trp = ((temp >> 8) & 0x7) + 1;
2824 mem_tras = ((temp >> 12) & 0xf) + 4;
2825 }
2826 /* convert to FF */
Ben Skeggs68adac52010-04-28 11:46:42 +10002827 trcd_ff.full = dfixed_const(mem_trcd);
2828 trp_ff.full = dfixed_const(mem_trp);
2829 tras_ff.full = dfixed_const(mem_tras);
Jerome Glissec93bb852009-07-13 21:04:08 +02002830
2831 /* Get values from the MEM_SDRAM_MODE_REG register...converting its */
2832 temp = RREG32(RADEON_MEM_SDRAM_MODE_REG);
2833 data = (temp & (7 << 20)) >> 20;
2834 if ((rdev->family == CHIP_RV100) || rdev->flags & RADEON_IS_IGP) {
2835 if (rdev->family == CHIP_RS480) /* don't think rs400 */
2836 tcas_ff = memtcas_rs480_ff[data];
2837 else
2838 tcas_ff = memtcas_ff[data];
2839 } else
2840 tcas_ff = memtcas2_ff[data];
2841
2842 if (rdev->family == CHIP_RS400 ||
2843 rdev->family == CHIP_RS480) {
2844 /* extra cas latency stored in bits 23-25 0-4 clocks */
2845 data = (temp >> 23) & 0x7;
2846 if (data < 5)
Ben Skeggs68adac52010-04-28 11:46:42 +10002847 tcas_ff.full += dfixed_const(data);
Jerome Glissec93bb852009-07-13 21:04:08 +02002848 }
2849
2850 if (ASIC_IS_R300(rdev) && !(rdev->flags & RADEON_IS_IGP)) {
2851 /* on the R300, Tcas is included in Trbs.
2852 */
2853 temp = RREG32(RADEON_MEM_CNTL);
2854 data = (R300_MEM_NUM_CHANNELS_MASK & temp);
2855 if (data == 1) {
2856 if (R300_MEM_USE_CD_CH_ONLY & temp) {
2857 temp = RREG32(R300_MC_IND_INDEX);
2858 temp &= ~R300_MC_IND_ADDR_MASK;
2859 temp |= R300_MC_READ_CNTL_CD_mcind;
2860 WREG32(R300_MC_IND_INDEX, temp);
2861 temp = RREG32(R300_MC_IND_DATA);
2862 data = (R300_MEM_RBS_POSITION_C_MASK & temp);
2863 } else {
2864 temp = RREG32(R300_MC_READ_CNTL_AB);
2865 data = (R300_MEM_RBS_POSITION_A_MASK & temp);
2866 }
2867 } else {
2868 temp = RREG32(R300_MC_READ_CNTL_AB);
2869 data = (R300_MEM_RBS_POSITION_A_MASK & temp);
2870 }
2871 if (rdev->family == CHIP_RV410 ||
2872 rdev->family == CHIP_R420 ||
2873 rdev->family == CHIP_R423)
2874 trbs_ff = memtrbs_r4xx[data];
2875 else
2876 trbs_ff = memtrbs[data];
2877 tcas_ff.full += trbs_ff.full;
2878 }
2879
2880 sclk_eff_ff.full = sclk_ff.full;
2881
2882 if (rdev->flags & RADEON_IS_AGP) {
2883 fixed20_12 agpmode_ff;
Ben Skeggs68adac52010-04-28 11:46:42 +10002884 agpmode_ff.full = dfixed_const(radeon_agpmode);
2885 temp_ff.full = dfixed_const_666(16);
2886 sclk_eff_ff.full -= dfixed_mul(agpmode_ff, temp_ff);
Jerome Glissec93bb852009-07-13 21:04:08 +02002887 }
2888 /* TODO PCIE lanes may affect this - agpmode == 16?? */
2889
2890 if (ASIC_IS_R300(rdev)) {
Ben Skeggs68adac52010-04-28 11:46:42 +10002891 sclk_delay_ff.full = dfixed_const(250);
Jerome Glissec93bb852009-07-13 21:04:08 +02002892 } else {
2893 if ((rdev->family == CHIP_RV100) ||
2894 rdev->flags & RADEON_IS_IGP) {
2895 if (rdev->mc.vram_is_ddr)
Ben Skeggs68adac52010-04-28 11:46:42 +10002896 sclk_delay_ff.full = dfixed_const(41);
Jerome Glissec93bb852009-07-13 21:04:08 +02002897 else
Ben Skeggs68adac52010-04-28 11:46:42 +10002898 sclk_delay_ff.full = dfixed_const(33);
Jerome Glissec93bb852009-07-13 21:04:08 +02002899 } else {
2900 if (rdev->mc.vram_width == 128)
Ben Skeggs68adac52010-04-28 11:46:42 +10002901 sclk_delay_ff.full = dfixed_const(57);
Jerome Glissec93bb852009-07-13 21:04:08 +02002902 else
Ben Skeggs68adac52010-04-28 11:46:42 +10002903 sclk_delay_ff.full = dfixed_const(41);
Jerome Glissec93bb852009-07-13 21:04:08 +02002904 }
2905 }
2906
Ben Skeggs68adac52010-04-28 11:46:42 +10002907 mc_latency_sclk.full = dfixed_div(sclk_delay_ff, sclk_eff_ff);
Jerome Glissec93bb852009-07-13 21:04:08 +02002908
2909 if (rdev->mc.vram_is_ddr) {
2910 if (rdev->mc.vram_width == 32) {
Ben Skeggs68adac52010-04-28 11:46:42 +10002911 k1.full = dfixed_const(40);
Jerome Glissec93bb852009-07-13 21:04:08 +02002912 c = 3;
2913 } else {
Ben Skeggs68adac52010-04-28 11:46:42 +10002914 k1.full = dfixed_const(20);
Jerome Glissec93bb852009-07-13 21:04:08 +02002915 c = 1;
2916 }
2917 } else {
Ben Skeggs68adac52010-04-28 11:46:42 +10002918 k1.full = dfixed_const(40);
Jerome Glissec93bb852009-07-13 21:04:08 +02002919 c = 3;
2920 }
2921
Ben Skeggs68adac52010-04-28 11:46:42 +10002922 temp_ff.full = dfixed_const(2);
2923 mc_latency_mclk.full = dfixed_mul(trcd_ff, temp_ff);
2924 temp_ff.full = dfixed_const(c);
2925 mc_latency_mclk.full += dfixed_mul(tcas_ff, temp_ff);
2926 temp_ff.full = dfixed_const(4);
2927 mc_latency_mclk.full += dfixed_mul(tras_ff, temp_ff);
2928 mc_latency_mclk.full += dfixed_mul(trp_ff, temp_ff);
Jerome Glissec93bb852009-07-13 21:04:08 +02002929 mc_latency_mclk.full += k1.full;
2930
Ben Skeggs68adac52010-04-28 11:46:42 +10002931 mc_latency_mclk.full = dfixed_div(mc_latency_mclk, mclk_ff);
2932 mc_latency_mclk.full += dfixed_div(temp_ff, sclk_eff_ff);
Jerome Glissec93bb852009-07-13 21:04:08 +02002933
2934 /*
2935 HW cursor time assuming worst case of full size colour cursor.
2936 */
Ben Skeggs68adac52010-04-28 11:46:42 +10002937 temp_ff.full = dfixed_const((2 * (cur_size - (rdev->mc.vram_is_ddr + 1))));
Jerome Glissec93bb852009-07-13 21:04:08 +02002938 temp_ff.full += trcd_ff.full;
2939 if (temp_ff.full < tras_ff.full)
2940 temp_ff.full = tras_ff.full;
Ben Skeggs68adac52010-04-28 11:46:42 +10002941 cur_latency_mclk.full = dfixed_div(temp_ff, mclk_ff);
Jerome Glissec93bb852009-07-13 21:04:08 +02002942
Ben Skeggs68adac52010-04-28 11:46:42 +10002943 temp_ff.full = dfixed_const(cur_size);
2944 cur_latency_sclk.full = dfixed_div(temp_ff, sclk_eff_ff);
Jerome Glissec93bb852009-07-13 21:04:08 +02002945 /*
2946 Find the total latency for the display data.
2947 */
Ben Skeggs68adac52010-04-28 11:46:42 +10002948 disp_latency_overhead.full = dfixed_const(8);
2949 disp_latency_overhead.full = dfixed_div(disp_latency_overhead, sclk_ff);
Jerome Glissec93bb852009-07-13 21:04:08 +02002950 mc_latency_mclk.full += disp_latency_overhead.full + cur_latency_mclk.full;
2951 mc_latency_sclk.full += disp_latency_overhead.full + cur_latency_sclk.full;
2952
2953 if (mc_latency_mclk.full > mc_latency_sclk.full)
2954 disp_latency.full = mc_latency_mclk.full;
2955 else
2956 disp_latency.full = mc_latency_sclk.full;
2957
2958 /* setup Max GRPH_STOP_REQ default value */
2959 if (ASIC_IS_RV100(rdev))
2960 max_stop_req = 0x5c;
2961 else
2962 max_stop_req = 0x7c;
2963
2964 if (mode1) {
2965 /* CRTC1
2966 Set GRPH_BUFFER_CNTL register using h/w defined optimal values.
2967 GRPH_STOP_REQ <= MIN[ 0x7C, (CRTC_H_DISP + 1) * (bit depth) / 0x10 ]
2968 */
2969 stop_req = mode1->hdisplay * pixel_bytes1 / 16;
2970
2971 if (stop_req > max_stop_req)
2972 stop_req = max_stop_req;
2973
2974 /*
2975 Find the drain rate of the display buffer.
2976 */
Ben Skeggs68adac52010-04-28 11:46:42 +10002977 temp_ff.full = dfixed_const((16/pixel_bytes1));
2978 disp_drain_rate.full = dfixed_div(pix_clk, temp_ff);
Jerome Glissec93bb852009-07-13 21:04:08 +02002979
2980 /*
2981 Find the critical point of the display buffer.
2982 */
Ben Skeggs68adac52010-04-28 11:46:42 +10002983 crit_point_ff.full = dfixed_mul(disp_drain_rate, disp_latency);
2984 crit_point_ff.full += dfixed_const_half(0);
Jerome Glissec93bb852009-07-13 21:04:08 +02002985
Ben Skeggs68adac52010-04-28 11:46:42 +10002986 critical_point = dfixed_trunc(crit_point_ff);
Jerome Glissec93bb852009-07-13 21:04:08 +02002987
2988 if (rdev->disp_priority == 2) {
2989 critical_point = 0;
2990 }
2991
2992 /*
2993 The critical point should never be above max_stop_req-4. Setting
2994 GRPH_CRITICAL_CNTL = 0 will thus force high priority all the time.
2995 */
2996 if (max_stop_req - critical_point < 4)
2997 critical_point = 0;
2998
2999 if (critical_point == 0 && mode2 && rdev->family == CHIP_R300) {
3000 /* some R300 cards have problem with this set to 0, when CRTC2 is enabled.*/
3001 critical_point = 0x10;
3002 }
3003
3004 temp = RREG32(RADEON_GRPH_BUFFER_CNTL);
3005 temp &= ~(RADEON_GRPH_STOP_REQ_MASK);
3006 temp |= (stop_req << RADEON_GRPH_STOP_REQ_SHIFT);
3007 temp &= ~(RADEON_GRPH_START_REQ_MASK);
3008 if ((rdev->family == CHIP_R350) &&
3009 (stop_req > 0x15)) {
3010 stop_req -= 0x10;
3011 }
3012 temp |= (stop_req << RADEON_GRPH_START_REQ_SHIFT);
3013 temp |= RADEON_GRPH_BUFFER_SIZE;
3014 temp &= ~(RADEON_GRPH_CRITICAL_CNTL |
3015 RADEON_GRPH_CRITICAL_AT_SOF |
3016 RADEON_GRPH_STOP_CNTL);
3017 /*
3018 Write the result into the register.
3019 */
3020 WREG32(RADEON_GRPH_BUFFER_CNTL, ((temp & ~RADEON_GRPH_CRITICAL_POINT_MASK) |
3021 (critical_point << RADEON_GRPH_CRITICAL_POINT_SHIFT)));
3022
3023#if 0
3024 if ((rdev->family == CHIP_RS400) ||
3025 (rdev->family == CHIP_RS480)) {
3026 /* attempt to program RS400 disp regs correctly ??? */
3027 temp = RREG32(RS400_DISP1_REG_CNTL);
3028 temp &= ~(RS400_DISP1_START_REQ_LEVEL_MASK |
3029 RS400_DISP1_STOP_REQ_LEVEL_MASK);
3030 WREG32(RS400_DISP1_REQ_CNTL1, (temp |
3031 (critical_point << RS400_DISP1_START_REQ_LEVEL_SHIFT) |
3032 (critical_point << RS400_DISP1_STOP_REQ_LEVEL_SHIFT)));
3033 temp = RREG32(RS400_DMIF_MEM_CNTL1);
3034 temp &= ~(RS400_DISP1_CRITICAL_POINT_START_MASK |
3035 RS400_DISP1_CRITICAL_POINT_STOP_MASK);
3036 WREG32(RS400_DMIF_MEM_CNTL1, (temp |
3037 (critical_point << RS400_DISP1_CRITICAL_POINT_START_SHIFT) |
3038 (critical_point << RS400_DISP1_CRITICAL_POINT_STOP_SHIFT)));
3039 }
3040#endif
3041
3042 DRM_DEBUG("GRPH_BUFFER_CNTL from to %x\n",
3043 /* (unsigned int)info->SavedReg->grph_buffer_cntl, */
3044 (unsigned int)RREG32(RADEON_GRPH_BUFFER_CNTL));
3045 }
3046
3047 if (mode2) {
3048 u32 grph2_cntl;
3049 stop_req = mode2->hdisplay * pixel_bytes2 / 16;
3050
3051 if (stop_req > max_stop_req)
3052 stop_req = max_stop_req;
3053
3054 /*
3055 Find the drain rate of the display buffer.
3056 */
Ben Skeggs68adac52010-04-28 11:46:42 +10003057 temp_ff.full = dfixed_const((16/pixel_bytes2));
3058 disp_drain_rate2.full = dfixed_div(pix_clk2, temp_ff);
Jerome Glissec93bb852009-07-13 21:04:08 +02003059
3060 grph2_cntl = RREG32(RADEON_GRPH2_BUFFER_CNTL);
3061 grph2_cntl &= ~(RADEON_GRPH_STOP_REQ_MASK);
3062 grph2_cntl |= (stop_req << RADEON_GRPH_STOP_REQ_SHIFT);
3063 grph2_cntl &= ~(RADEON_GRPH_START_REQ_MASK);
3064 if ((rdev->family == CHIP_R350) &&
3065 (stop_req > 0x15)) {
3066 stop_req -= 0x10;
3067 }
3068 grph2_cntl |= (stop_req << RADEON_GRPH_START_REQ_SHIFT);
3069 grph2_cntl |= RADEON_GRPH_BUFFER_SIZE;
3070 grph2_cntl &= ~(RADEON_GRPH_CRITICAL_CNTL |
3071 RADEON_GRPH_CRITICAL_AT_SOF |
3072 RADEON_GRPH_STOP_CNTL);
3073
3074 if ((rdev->family == CHIP_RS100) ||
3075 (rdev->family == CHIP_RS200))
3076 critical_point2 = 0;
3077 else {
3078 temp = (rdev->mc.vram_width * rdev->mc.vram_is_ddr + 1)/128;
Ben Skeggs68adac52010-04-28 11:46:42 +10003079 temp_ff.full = dfixed_const(temp);
3080 temp_ff.full = dfixed_mul(mclk_ff, temp_ff);
Jerome Glissec93bb852009-07-13 21:04:08 +02003081 if (sclk_ff.full < temp_ff.full)
3082 temp_ff.full = sclk_ff.full;
3083
3084 read_return_rate.full = temp_ff.full;
3085
3086 if (mode1) {
3087 temp_ff.full = read_return_rate.full - disp_drain_rate.full;
Ben Skeggs68adac52010-04-28 11:46:42 +10003088 time_disp1_drop_priority.full = dfixed_div(crit_point_ff, temp_ff);
Jerome Glissec93bb852009-07-13 21:04:08 +02003089 } else {
3090 time_disp1_drop_priority.full = 0;
3091 }
3092 crit_point_ff.full = disp_latency.full + time_disp1_drop_priority.full + disp_latency.full;
Ben Skeggs68adac52010-04-28 11:46:42 +10003093 crit_point_ff.full = dfixed_mul(crit_point_ff, disp_drain_rate2);
3094 crit_point_ff.full += dfixed_const_half(0);
Jerome Glissec93bb852009-07-13 21:04:08 +02003095
Ben Skeggs68adac52010-04-28 11:46:42 +10003096 critical_point2 = dfixed_trunc(crit_point_ff);
Jerome Glissec93bb852009-07-13 21:04:08 +02003097
3098 if (rdev->disp_priority == 2) {
3099 critical_point2 = 0;
3100 }
3101
3102 if (max_stop_req - critical_point2 < 4)
3103 critical_point2 = 0;
3104
3105 }
3106
3107 if (critical_point2 == 0 && rdev->family == CHIP_R300) {
3108 /* some R300 cards have problem with this set to 0 */
3109 critical_point2 = 0x10;
3110 }
3111
3112 WREG32(RADEON_GRPH2_BUFFER_CNTL, ((grph2_cntl & ~RADEON_GRPH_CRITICAL_POINT_MASK) |
3113 (critical_point2 << RADEON_GRPH_CRITICAL_POINT_SHIFT)));
3114
3115 if ((rdev->family == CHIP_RS400) ||
3116 (rdev->family == CHIP_RS480)) {
3117#if 0
3118 /* attempt to program RS400 disp2 regs correctly ??? */
3119 temp = RREG32(RS400_DISP2_REQ_CNTL1);
3120 temp &= ~(RS400_DISP2_START_REQ_LEVEL_MASK |
3121 RS400_DISP2_STOP_REQ_LEVEL_MASK);
3122 WREG32(RS400_DISP2_REQ_CNTL1, (temp |
3123 (critical_point2 << RS400_DISP1_START_REQ_LEVEL_SHIFT) |
3124 (critical_point2 << RS400_DISP1_STOP_REQ_LEVEL_SHIFT)));
3125 temp = RREG32(RS400_DISP2_REQ_CNTL2);
3126 temp &= ~(RS400_DISP2_CRITICAL_POINT_START_MASK |
3127 RS400_DISP2_CRITICAL_POINT_STOP_MASK);
3128 WREG32(RS400_DISP2_REQ_CNTL2, (temp |
3129 (critical_point2 << RS400_DISP2_CRITICAL_POINT_START_SHIFT) |
3130 (critical_point2 << RS400_DISP2_CRITICAL_POINT_STOP_SHIFT)));
3131#endif
3132 WREG32(RS400_DISP2_REQ_CNTL1, 0x105DC1CC);
3133 WREG32(RS400_DISP2_REQ_CNTL2, 0x2749D000);
3134 WREG32(RS400_DMIF_MEM_CNTL1, 0x29CA71DC);
3135 WREG32(RS400_DISP1_REQ_CNTL1, 0x28FBC3AC);
3136 }
3137
3138 DRM_DEBUG("GRPH2_BUFFER_CNTL from to %x\n",
3139 (unsigned int)RREG32(RADEON_GRPH2_BUFFER_CNTL));
3140 }
3141}
Dave Airlie551ebd82009-09-01 15:25:57 +10003142
3143static inline void r100_cs_track_texture_print(struct r100_cs_track_texture *t)
3144{
3145 DRM_ERROR("pitch %d\n", t->pitch);
Mathias Fröhlichceb776b2009-10-19 12:50:41 -04003146 DRM_ERROR("use_pitch %d\n", t->use_pitch);
Dave Airlie551ebd82009-09-01 15:25:57 +10003147 DRM_ERROR("width %d\n", t->width);
Mathias Fröhlichceb776b2009-10-19 12:50:41 -04003148 DRM_ERROR("width_11 %d\n", t->width_11);
Dave Airlie551ebd82009-09-01 15:25:57 +10003149 DRM_ERROR("height %d\n", t->height);
Mathias Fröhlichceb776b2009-10-19 12:50:41 -04003150 DRM_ERROR("height_11 %d\n", t->height_11);
Dave Airlie551ebd82009-09-01 15:25:57 +10003151 DRM_ERROR("num levels %d\n", t->num_levels);
3152 DRM_ERROR("depth %d\n", t->txdepth);
3153 DRM_ERROR("bpp %d\n", t->cpp);
3154 DRM_ERROR("coordinate type %d\n", t->tex_coord_type);
3155 DRM_ERROR("width round to power of 2 %d\n", t->roundup_w);
3156 DRM_ERROR("height round to power of 2 %d\n", t->roundup_h);
Dave Airlied785d782009-12-07 13:16:06 +10003157 DRM_ERROR("compress format %d\n", t->compress_format);
Dave Airlie551ebd82009-09-01 15:25:57 +10003158}
3159
Dave Airlied785d782009-12-07 13:16:06 +10003160static int r100_track_compress_size(int compress_format, int w, int h)
3161{
3162 int block_width, block_height, block_bytes;
3163 int wblocks, hblocks;
3164 int min_wblocks;
3165 int sz;
3166
3167 block_width = 4;
3168 block_height = 4;
3169
3170 switch (compress_format) {
3171 case R100_TRACK_COMP_DXT1:
3172 block_bytes = 8;
3173 min_wblocks = 4;
3174 break;
3175 default:
3176 case R100_TRACK_COMP_DXT35:
3177 block_bytes = 16;
3178 min_wblocks = 2;
3179 break;
3180 }
3181
3182 hblocks = (h + block_height - 1) / block_height;
3183 wblocks = (w + block_width - 1) / block_width;
3184 if (wblocks < min_wblocks)
3185 wblocks = min_wblocks;
3186 sz = wblocks * hblocks * block_bytes;
3187 return sz;
3188}
3189
Roland Scheidegger37cf6b02010-06-12 13:31:11 -04003190static int r100_cs_track_cube(struct radeon_device *rdev,
3191 struct r100_cs_track *track, unsigned idx)
3192{
3193 unsigned face, w, h;
3194 struct radeon_bo *cube_robj;
3195 unsigned long size;
3196 unsigned compress_format = track->textures[idx].compress_format;
3197
3198 for (face = 0; face < 5; face++) {
3199 cube_robj = track->textures[idx].cube_info[face].robj;
3200 w = track->textures[idx].cube_info[face].width;
3201 h = track->textures[idx].cube_info[face].height;
3202
3203 if (compress_format) {
3204 size = r100_track_compress_size(compress_format, w, h);
3205 } else
3206 size = w * h;
3207 size *= track->textures[idx].cpp;
3208
3209 size += track->textures[idx].cube_info[face].offset;
3210
3211 if (size > radeon_bo_size(cube_robj)) {
3212 DRM_ERROR("Cube texture offset greater than object size %lu %lu\n",
3213 size, radeon_bo_size(cube_robj));
3214 r100_cs_track_texture_print(&track->textures[idx]);
3215 return -1;
3216 }
3217 }
3218 return 0;
3219}
3220
Dave Airlie551ebd82009-09-01 15:25:57 +10003221static int r100_cs_track_texture_check(struct radeon_device *rdev,
3222 struct r100_cs_track *track)
3223{
Jerome Glisse4c788672009-11-20 14:29:23 +01003224 struct radeon_bo *robj;
Dave Airlie551ebd82009-09-01 15:25:57 +10003225 unsigned long size;
Marek Olšákb73c5f82010-04-11 03:18:52 +02003226 unsigned u, i, w, h, d;
Dave Airlie551ebd82009-09-01 15:25:57 +10003227 int ret;
3228
3229 for (u = 0; u < track->num_texture; u++) {
3230 if (!track->textures[u].enabled)
3231 continue;
3232 robj = track->textures[u].robj;
3233 if (robj == NULL) {
3234 DRM_ERROR("No texture bound to unit %u\n", u);
3235 return -EINVAL;
3236 }
3237 size = 0;
3238 for (i = 0; i <= track->textures[u].num_levels; i++) {
3239 if (track->textures[u].use_pitch) {
3240 if (rdev->family < CHIP_R300)
3241 w = (track->textures[u].pitch / track->textures[u].cpp) / (1 << i);
3242 else
3243 w = track->textures[u].pitch / (1 << i);
3244 } else {
Mathias Fröhlichceb776b2009-10-19 12:50:41 -04003245 w = track->textures[u].width;
Dave Airlie551ebd82009-09-01 15:25:57 +10003246 if (rdev->family >= CHIP_RV515)
3247 w |= track->textures[u].width_11;
Mathias Fröhlichceb776b2009-10-19 12:50:41 -04003248 w = w / (1 << i);
Dave Airlie551ebd82009-09-01 15:25:57 +10003249 if (track->textures[u].roundup_w)
3250 w = roundup_pow_of_two(w);
3251 }
Mathias Fröhlichceb776b2009-10-19 12:50:41 -04003252 h = track->textures[u].height;
Dave Airlie551ebd82009-09-01 15:25:57 +10003253 if (rdev->family >= CHIP_RV515)
3254 h |= track->textures[u].height_11;
Mathias Fröhlichceb776b2009-10-19 12:50:41 -04003255 h = h / (1 << i);
Dave Airlie551ebd82009-09-01 15:25:57 +10003256 if (track->textures[u].roundup_h)
3257 h = roundup_pow_of_two(h);
Marek Olšákb73c5f82010-04-11 03:18:52 +02003258 if (track->textures[u].tex_coord_type == 1) {
3259 d = (1 << track->textures[u].txdepth) / (1 << i);
3260 if (!d)
3261 d = 1;
3262 } else {
3263 d = 1;
3264 }
Dave Airlied785d782009-12-07 13:16:06 +10003265 if (track->textures[u].compress_format) {
3266
Marek Olšákb73c5f82010-04-11 03:18:52 +02003267 size += r100_track_compress_size(track->textures[u].compress_format, w, h) * d;
Dave Airlied785d782009-12-07 13:16:06 +10003268 /* compressed textures are block based */
3269 } else
Marek Olšákb73c5f82010-04-11 03:18:52 +02003270 size += w * h * d;
Dave Airlie551ebd82009-09-01 15:25:57 +10003271 }
3272 size *= track->textures[u].cpp;
Dave Airlied785d782009-12-07 13:16:06 +10003273
Dave Airlie551ebd82009-09-01 15:25:57 +10003274 switch (track->textures[u].tex_coord_type) {
3275 case 0:
Dave Airlie551ebd82009-09-01 15:25:57 +10003276 case 1:
Dave Airlie551ebd82009-09-01 15:25:57 +10003277 break;
3278 case 2:
3279 if (track->separate_cube) {
3280 ret = r100_cs_track_cube(rdev, track, u);
3281 if (ret)
3282 return ret;
3283 } else
3284 size *= 6;
3285 break;
3286 default:
3287 DRM_ERROR("Invalid texture coordinate type %u for unit "
3288 "%u\n", track->textures[u].tex_coord_type, u);
3289 return -EINVAL;
3290 }
Jerome Glisse4c788672009-11-20 14:29:23 +01003291 if (size > radeon_bo_size(robj)) {
Dave Airlie551ebd82009-09-01 15:25:57 +10003292 DRM_ERROR("Texture of unit %u needs %lu bytes but is "
Jerome Glisse4c788672009-11-20 14:29:23 +01003293 "%lu\n", u, size, radeon_bo_size(robj));
Dave Airlie551ebd82009-09-01 15:25:57 +10003294 r100_cs_track_texture_print(&track->textures[u]);
3295 return -EINVAL;
3296 }
3297 }
3298 return 0;
3299}
3300
3301int r100_cs_track_check(struct radeon_device *rdev, struct r100_cs_track *track)
3302{
3303 unsigned i;
3304 unsigned long size;
3305 unsigned prim_walk;
3306 unsigned nverts;
3307
3308 for (i = 0; i < track->num_cb; i++) {
3309 if (track->cb[i].robj == NULL) {
Marek Olšák797fd5b2010-04-13 02:33:36 +02003310 if (!(track->zb_cb_clear || track->color_channel_mask ||
Marek Olšák46c64d42009-12-17 06:02:28 +01003311 track->blend_read_enable)) {
3312 continue;
3313 }
Dave Airlie551ebd82009-09-01 15:25:57 +10003314 DRM_ERROR("[drm] No buffer for color buffer %d !\n", i);
3315 return -EINVAL;
3316 }
3317 size = track->cb[i].pitch * track->cb[i].cpp * track->maxy;
3318 size += track->cb[i].offset;
Jerome Glisse4c788672009-11-20 14:29:23 +01003319 if (size > radeon_bo_size(track->cb[i].robj)) {
Dave Airlie551ebd82009-09-01 15:25:57 +10003320 DRM_ERROR("[drm] Buffer too small for color buffer %d "
3321 "(need %lu have %lu) !\n", i, size,
Jerome Glisse4c788672009-11-20 14:29:23 +01003322 radeon_bo_size(track->cb[i].robj));
Dave Airlie551ebd82009-09-01 15:25:57 +10003323 DRM_ERROR("[drm] color buffer %d (%u %u %u %u)\n",
3324 i, track->cb[i].pitch, track->cb[i].cpp,
3325 track->cb[i].offset, track->maxy);
3326 return -EINVAL;
3327 }
3328 }
3329 if (track->z_enabled) {
3330 if (track->zb.robj == NULL) {
3331 DRM_ERROR("[drm] No buffer for z buffer !\n");
3332 return -EINVAL;
3333 }
3334 size = track->zb.pitch * track->zb.cpp * track->maxy;
3335 size += track->zb.offset;
Jerome Glisse4c788672009-11-20 14:29:23 +01003336 if (size > radeon_bo_size(track->zb.robj)) {
Dave Airlie551ebd82009-09-01 15:25:57 +10003337 DRM_ERROR("[drm] Buffer too small for z buffer "
3338 "(need %lu have %lu) !\n", size,
Jerome Glisse4c788672009-11-20 14:29:23 +01003339 radeon_bo_size(track->zb.robj));
Dave Airlie551ebd82009-09-01 15:25:57 +10003340 DRM_ERROR("[drm] zbuffer (%u %u %u %u)\n",
3341 track->zb.pitch, track->zb.cpp,
3342 track->zb.offset, track->maxy);
3343 return -EINVAL;
3344 }
3345 }
3346 prim_walk = (track->vap_vf_cntl >> 4) & 0x3;
Marek Olšákcae94b02010-02-21 21:24:15 +01003347 if (track->vap_vf_cntl & (1 << 14)) {
3348 nverts = track->vap_alt_nverts;
3349 } else {
3350 nverts = (track->vap_vf_cntl >> 16) & 0xFFFF;
3351 }
Dave Airlie551ebd82009-09-01 15:25:57 +10003352 switch (prim_walk) {
3353 case 1:
3354 for (i = 0; i < track->num_arrays; i++) {
3355 size = track->arrays[i].esize * track->max_indx * 4;
3356 if (track->arrays[i].robj == NULL) {
3357 DRM_ERROR("(PW %u) Vertex array %u no buffer "
3358 "bound\n", prim_walk, i);
3359 return -EINVAL;
3360 }
Jerome Glisse4c788672009-11-20 14:29:23 +01003361 if (size > radeon_bo_size(track->arrays[i].robj)) {
3362 dev_err(rdev->dev, "(PW %u) Vertex array %u "
3363 "need %lu dwords have %lu dwords\n",
3364 prim_walk, i, size >> 2,
3365 radeon_bo_size(track->arrays[i].robj)
3366 >> 2);
Dave Airlie551ebd82009-09-01 15:25:57 +10003367 DRM_ERROR("Max indices %u\n", track->max_indx);
3368 return -EINVAL;
3369 }
3370 }
3371 break;
3372 case 2:
3373 for (i = 0; i < track->num_arrays; i++) {
3374 size = track->arrays[i].esize * (nverts - 1) * 4;
3375 if (track->arrays[i].robj == NULL) {
3376 DRM_ERROR("(PW %u) Vertex array %u no buffer "
3377 "bound\n", prim_walk, i);
3378 return -EINVAL;
3379 }
Jerome Glisse4c788672009-11-20 14:29:23 +01003380 if (size > radeon_bo_size(track->arrays[i].robj)) {
3381 dev_err(rdev->dev, "(PW %u) Vertex array %u "
3382 "need %lu dwords have %lu dwords\n",
3383 prim_walk, i, size >> 2,
3384 radeon_bo_size(track->arrays[i].robj)
3385 >> 2);
Dave Airlie551ebd82009-09-01 15:25:57 +10003386 return -EINVAL;
3387 }
3388 }
3389 break;
3390 case 3:
3391 size = track->vtx_size * nverts;
3392 if (size != track->immd_dwords) {
3393 DRM_ERROR("IMMD draw %u dwors but needs %lu dwords\n",
3394 track->immd_dwords, size);
3395 DRM_ERROR("VAP_VF_CNTL.NUM_VERTICES %u, VTX_SIZE %u\n",
3396 nverts, track->vtx_size);
3397 return -EINVAL;
3398 }
3399 break;
3400 default:
3401 DRM_ERROR("[drm] Invalid primitive walk %d for VAP_VF_CNTL\n",
3402 prim_walk);
3403 return -EINVAL;
3404 }
3405 return r100_cs_track_texture_check(rdev, track);
3406}
3407
3408void r100_cs_track_clear(struct radeon_device *rdev, struct r100_cs_track *track)
3409{
3410 unsigned i, face;
3411
3412 if (rdev->family < CHIP_R300) {
3413 track->num_cb = 1;
3414 if (rdev->family <= CHIP_RS200)
3415 track->num_texture = 3;
3416 else
3417 track->num_texture = 6;
3418 track->maxy = 2048;
3419 track->separate_cube = 1;
3420 } else {
3421 track->num_cb = 4;
3422 track->num_texture = 16;
3423 track->maxy = 4096;
3424 track->separate_cube = 0;
3425 }
3426
3427 for (i = 0; i < track->num_cb; i++) {
3428 track->cb[i].robj = NULL;
3429 track->cb[i].pitch = 8192;
3430 track->cb[i].cpp = 16;
3431 track->cb[i].offset = 0;
3432 }
3433 track->z_enabled = true;
3434 track->zb.robj = NULL;
3435 track->zb.pitch = 8192;
3436 track->zb.cpp = 4;
3437 track->zb.offset = 0;
3438 track->vtx_size = 0x7F;
3439 track->immd_dwords = 0xFFFFFFFFUL;
3440 track->num_arrays = 11;
3441 track->max_indx = 0x00FFFFFFUL;
3442 for (i = 0; i < track->num_arrays; i++) {
3443 track->arrays[i].robj = NULL;
3444 track->arrays[i].esize = 0x7F;
3445 }
3446 for (i = 0; i < track->num_texture; i++) {
Dave Airlied785d782009-12-07 13:16:06 +10003447 track->textures[i].compress_format = R100_TRACK_COMP_NONE;
Dave Airlie551ebd82009-09-01 15:25:57 +10003448 track->textures[i].pitch = 16536;
3449 track->textures[i].width = 16536;
3450 track->textures[i].height = 16536;
3451 track->textures[i].width_11 = 1 << 11;
3452 track->textures[i].height_11 = 1 << 11;
3453 track->textures[i].num_levels = 12;
3454 if (rdev->family <= CHIP_RS200) {
3455 track->textures[i].tex_coord_type = 0;
3456 track->textures[i].txdepth = 0;
3457 } else {
3458 track->textures[i].txdepth = 16;
3459 track->textures[i].tex_coord_type = 1;
3460 }
3461 track->textures[i].cpp = 64;
3462 track->textures[i].robj = NULL;
3463 /* CS IB emission code makes sure texture unit are disabled */
3464 track->textures[i].enabled = false;
3465 track->textures[i].roundup_w = true;
3466 track->textures[i].roundup_h = true;
3467 if (track->separate_cube)
3468 for (face = 0; face < 5; face++) {
3469 track->textures[i].cube_info[face].robj = NULL;
3470 track->textures[i].cube_info[face].width = 16536;
3471 track->textures[i].cube_info[face].height = 16536;
3472 track->textures[i].cube_info[face].offset = 0;
3473 }
3474 }
3475}
Jerome Glisse3ce0a232009-09-08 10:10:24 +10003476
3477int r100_ring_test(struct radeon_device *rdev)
3478{
3479 uint32_t scratch;
3480 uint32_t tmp = 0;
3481 unsigned i;
3482 int r;
3483
3484 r = radeon_scratch_get(rdev, &scratch);
3485 if (r) {
3486 DRM_ERROR("radeon: cp failed to get scratch reg (%d).\n", r);
3487 return r;
3488 }
3489 WREG32(scratch, 0xCAFEDEAD);
3490 r = radeon_ring_lock(rdev, 2);
3491 if (r) {
3492 DRM_ERROR("radeon: cp failed to lock ring (%d).\n", r);
3493 radeon_scratch_free(rdev, scratch);
3494 return r;
3495 }
3496 radeon_ring_write(rdev, PACKET0(scratch, 0));
3497 radeon_ring_write(rdev, 0xDEADBEEF);
3498 radeon_ring_unlock_commit(rdev);
3499 for (i = 0; i < rdev->usec_timeout; i++) {
3500 tmp = RREG32(scratch);
3501 if (tmp == 0xDEADBEEF) {
3502 break;
3503 }
3504 DRM_UDELAY(1);
3505 }
3506 if (i < rdev->usec_timeout) {
3507 DRM_INFO("ring test succeeded in %d usecs\n", i);
3508 } else {
3509 DRM_ERROR("radeon: ring test failed (sracth(0x%04X)=0x%08X)\n",
3510 scratch, tmp);
3511 r = -EINVAL;
3512 }
3513 radeon_scratch_free(rdev, scratch);
3514 return r;
3515}
3516
3517void r100_ring_ib_execute(struct radeon_device *rdev, struct radeon_ib *ib)
3518{
3519 radeon_ring_write(rdev, PACKET0(RADEON_CP_IB_BASE, 1));
3520 radeon_ring_write(rdev, ib->gpu_addr);
3521 radeon_ring_write(rdev, ib->length_dw);
3522}
3523
3524int r100_ib_test(struct radeon_device *rdev)
3525{
3526 struct radeon_ib *ib;
3527 uint32_t scratch;
3528 uint32_t tmp = 0;
3529 unsigned i;
3530 int r;
3531
3532 r = radeon_scratch_get(rdev, &scratch);
3533 if (r) {
3534 DRM_ERROR("radeon: failed to get scratch reg (%d).\n", r);
3535 return r;
3536 }
3537 WREG32(scratch, 0xCAFEDEAD);
3538 r = radeon_ib_get(rdev, &ib);
3539 if (r) {
3540 return r;
3541 }
3542 ib->ptr[0] = PACKET0(scratch, 0);
3543 ib->ptr[1] = 0xDEADBEEF;
3544 ib->ptr[2] = PACKET2(0);
3545 ib->ptr[3] = PACKET2(0);
3546 ib->ptr[4] = PACKET2(0);
3547 ib->ptr[5] = PACKET2(0);
3548 ib->ptr[6] = PACKET2(0);
3549 ib->ptr[7] = PACKET2(0);
3550 ib->length_dw = 8;
3551 r = radeon_ib_schedule(rdev, ib);
3552 if (r) {
3553 radeon_scratch_free(rdev, scratch);
3554 radeon_ib_free(rdev, &ib);
3555 return r;
3556 }
3557 r = radeon_fence_wait(ib->fence, false);
3558 if (r) {
3559 return r;
3560 }
3561 for (i = 0; i < rdev->usec_timeout; i++) {
3562 tmp = RREG32(scratch);
3563 if (tmp == 0xDEADBEEF) {
3564 break;
3565 }
3566 DRM_UDELAY(1);
3567 }
3568 if (i < rdev->usec_timeout) {
3569 DRM_INFO("ib test succeeded in %u usecs\n", i);
3570 } else {
3571 DRM_ERROR("radeon: ib test failed (sracth(0x%04X)=0x%08X)\n",
3572 scratch, tmp);
3573 r = -EINVAL;
3574 }
3575 radeon_scratch_free(rdev, scratch);
3576 radeon_ib_free(rdev, &ib);
3577 return r;
3578}
Jerome Glisse9f022dd2009-09-11 15:35:22 +02003579
3580void r100_ib_fini(struct radeon_device *rdev)
3581{
3582 radeon_ib_pool_fini(rdev);
3583}
3584
3585int r100_ib_init(struct radeon_device *rdev)
3586{
3587 int r;
3588
3589 r = radeon_ib_pool_init(rdev);
3590 if (r) {
3591 dev_err(rdev->dev, "failled initializing IB pool (%d).\n", r);
3592 r100_ib_fini(rdev);
3593 return r;
3594 }
3595 r = r100_ib_test(rdev);
3596 if (r) {
3597 dev_err(rdev->dev, "failled testing IB (%d).\n", r);
3598 r100_ib_fini(rdev);
3599 return r;
3600 }
3601 return 0;
3602}
3603
3604void r100_mc_stop(struct radeon_device *rdev, struct r100_mc_save *save)
3605{
3606 /* Shutdown CP we shouldn't need to do that but better be safe than
3607 * sorry
3608 */
3609 rdev->cp.ready = false;
3610 WREG32(R_000740_CP_CSQ_CNTL, 0);
3611
3612 /* Save few CRTC registers */
Jerome Glisseca6ffc62009-10-01 10:20:52 +02003613 save->GENMO_WT = RREG8(R_0003C2_GENMO_WT);
Jerome Glisse9f022dd2009-09-11 15:35:22 +02003614 save->CRTC_EXT_CNTL = RREG32(R_000054_CRTC_EXT_CNTL);
3615 save->CRTC_GEN_CNTL = RREG32(R_000050_CRTC_GEN_CNTL);
3616 save->CUR_OFFSET = RREG32(R_000260_CUR_OFFSET);
3617 if (!(rdev->flags & RADEON_SINGLE_CRTC)) {
3618 save->CRTC2_GEN_CNTL = RREG32(R_0003F8_CRTC2_GEN_CNTL);
3619 save->CUR2_OFFSET = RREG32(R_000360_CUR2_OFFSET);
3620 }
3621
3622 /* Disable VGA aperture access */
Jerome Glisseca6ffc62009-10-01 10:20:52 +02003623 WREG8(R_0003C2_GENMO_WT, C_0003C2_VGA_RAM_EN & save->GENMO_WT);
Jerome Glisse9f022dd2009-09-11 15:35:22 +02003624 /* Disable cursor, overlay, crtc */
3625 WREG32(R_000260_CUR_OFFSET, save->CUR_OFFSET | S_000260_CUR_LOCK(1));
3626 WREG32(R_000054_CRTC_EXT_CNTL, save->CRTC_EXT_CNTL |
3627 S_000054_CRTC_DISPLAY_DIS(1));
3628 WREG32(R_000050_CRTC_GEN_CNTL,
3629 (C_000050_CRTC_CUR_EN & save->CRTC_GEN_CNTL) |
3630 S_000050_CRTC_DISP_REQ_EN_B(1));
3631 WREG32(R_000420_OV0_SCALE_CNTL,
3632 C_000420_OV0_OVERLAY_EN & RREG32(R_000420_OV0_SCALE_CNTL));
3633 WREG32(R_000260_CUR_OFFSET, C_000260_CUR_LOCK & save->CUR_OFFSET);
3634 if (!(rdev->flags & RADEON_SINGLE_CRTC)) {
3635 WREG32(R_000360_CUR2_OFFSET, save->CUR2_OFFSET |
3636 S_000360_CUR2_LOCK(1));
3637 WREG32(R_0003F8_CRTC2_GEN_CNTL,
3638 (C_0003F8_CRTC2_CUR_EN & save->CRTC2_GEN_CNTL) |
3639 S_0003F8_CRTC2_DISPLAY_DIS(1) |
3640 S_0003F8_CRTC2_DISP_REQ_EN_B(1));
3641 WREG32(R_000360_CUR2_OFFSET,
3642 C_000360_CUR2_LOCK & save->CUR2_OFFSET);
3643 }
3644}
3645
3646void r100_mc_resume(struct radeon_device *rdev, struct r100_mc_save *save)
3647{
3648 /* Update base address for crtc */
Jerome Glissed594e462010-02-17 21:54:29 +00003649 WREG32(R_00023C_DISPLAY_BASE_ADDR, rdev->mc.vram_start);
Jerome Glisse9f022dd2009-09-11 15:35:22 +02003650 if (!(rdev->flags & RADEON_SINGLE_CRTC)) {
Jerome Glissed594e462010-02-17 21:54:29 +00003651 WREG32(R_00033C_CRTC2_DISPLAY_BASE_ADDR, rdev->mc.vram_start);
Jerome Glisse9f022dd2009-09-11 15:35:22 +02003652 }
3653 /* Restore CRTC registers */
Jerome Glisseca6ffc62009-10-01 10:20:52 +02003654 WREG8(R_0003C2_GENMO_WT, save->GENMO_WT);
Jerome Glisse9f022dd2009-09-11 15:35:22 +02003655 WREG32(R_000054_CRTC_EXT_CNTL, save->CRTC_EXT_CNTL);
3656 WREG32(R_000050_CRTC_GEN_CNTL, save->CRTC_GEN_CNTL);
3657 if (!(rdev->flags & RADEON_SINGLE_CRTC)) {
3658 WREG32(R_0003F8_CRTC2_GEN_CNTL, save->CRTC2_GEN_CNTL);
3659 }
3660}
Jerome Glisseca6ffc62009-10-01 10:20:52 +02003661
3662void r100_vga_render_disable(struct radeon_device *rdev)
3663{
Jerome Glissed4550902009-10-01 10:12:06 +02003664 u32 tmp;
Jerome Glisseca6ffc62009-10-01 10:20:52 +02003665
Jerome Glissed4550902009-10-01 10:12:06 +02003666 tmp = RREG8(R_0003C2_GENMO_WT);
Jerome Glisseca6ffc62009-10-01 10:20:52 +02003667 WREG8(R_0003C2_GENMO_WT, C_0003C2_VGA_RAM_EN & tmp);
3668}
Jerome Glissed4550902009-10-01 10:12:06 +02003669
3670static void r100_debugfs(struct radeon_device *rdev)
3671{
3672 int r;
3673
3674 r = r100_debugfs_mc_info_init(rdev);
3675 if (r)
3676 dev_warn(rdev->dev, "Failed to create r100_mc debugfs file.\n");
3677}
3678
3679static void r100_mc_program(struct radeon_device *rdev)
3680{
3681 struct r100_mc_save save;
3682
3683 /* Stops all mc clients */
3684 r100_mc_stop(rdev, &save);
3685 if (rdev->flags & RADEON_IS_AGP) {
3686 WREG32(R_00014C_MC_AGP_LOCATION,
3687 S_00014C_MC_AGP_START(rdev->mc.gtt_start >> 16) |
3688 S_00014C_MC_AGP_TOP(rdev->mc.gtt_end >> 16));
3689 WREG32(R_000170_AGP_BASE, lower_32_bits(rdev->mc.agp_base));
3690 if (rdev->family > CHIP_RV200)
3691 WREG32(R_00015C_AGP_BASE_2,
3692 upper_32_bits(rdev->mc.agp_base) & 0xff);
3693 } else {
3694 WREG32(R_00014C_MC_AGP_LOCATION, 0x0FFFFFFF);
3695 WREG32(R_000170_AGP_BASE, 0);
3696 if (rdev->family > CHIP_RV200)
3697 WREG32(R_00015C_AGP_BASE_2, 0);
3698 }
3699 /* Wait for mc idle */
3700 if (r100_mc_wait_for_idle(rdev))
3701 dev_warn(rdev->dev, "Wait for MC idle timeout.\n");
3702 /* Program MC, should be a 32bits limited address space */
3703 WREG32(R_000148_MC_FB_LOCATION,
3704 S_000148_MC_FB_START(rdev->mc.vram_start >> 16) |
3705 S_000148_MC_FB_TOP(rdev->mc.vram_end >> 16));
3706 r100_mc_resume(rdev, &save);
3707}
3708
3709void r100_clock_startup(struct radeon_device *rdev)
3710{
3711 u32 tmp;
3712
3713 if (radeon_dynclks != -1 && radeon_dynclks)
3714 radeon_legacy_set_clock_gating(rdev, 1);
3715 /* We need to force on some of the block */
3716 tmp = RREG32_PLL(R_00000D_SCLK_CNTL);
3717 tmp |= S_00000D_FORCE_CP(1) | S_00000D_FORCE_VIP(1);
3718 if ((rdev->family == CHIP_RV250) || (rdev->family == CHIP_RV280))
3719 tmp |= S_00000D_FORCE_DISP1(1) | S_00000D_FORCE_DISP2(1);
3720 WREG32_PLL(R_00000D_SCLK_CNTL, tmp);
3721}
3722
3723static int r100_startup(struct radeon_device *rdev)
3724{
3725 int r;
3726
Alex Deucher92cde002009-12-04 10:55:12 -05003727 /* set common regs */
3728 r100_set_common_regs(rdev);
3729 /* program mc */
Jerome Glissed4550902009-10-01 10:12:06 +02003730 r100_mc_program(rdev);
3731 /* Resume clock */
3732 r100_clock_startup(rdev);
3733 /* Initialize GPU configuration (# pipes, ...) */
Jerome Glisse90aca4d2010-03-09 14:45:12 +00003734// r100_gpu_init(rdev);
Jerome Glissed4550902009-10-01 10:12:06 +02003735 /* Initialize GART (initialize after TTM so we can allocate
3736 * memory through TTM but finalize after TTM) */
Dave Airlie17e15b02009-11-05 15:36:53 +10003737 r100_enable_bm(rdev);
Jerome Glissed4550902009-10-01 10:12:06 +02003738 if (rdev->flags & RADEON_IS_PCI) {
3739 r = r100_pci_gart_enable(rdev);
3740 if (r)
3741 return r;
3742 }
3743 /* Enable IRQ */
Jerome Glissed4550902009-10-01 10:12:06 +02003744 r100_irq_set(rdev);
Jerome Glissecafe6602010-01-07 12:39:21 +01003745 rdev->config.r100.hdp_cntl = RREG32(RADEON_HOST_PATH_CNTL);
Jerome Glissed4550902009-10-01 10:12:06 +02003746 /* 1M ring buffer */
3747 r = r100_cp_init(rdev, 1024 * 1024);
3748 if (r) {
3749 dev_err(rdev->dev, "failled initializing CP (%d).\n", r);
3750 return r;
3751 }
3752 r = r100_wb_init(rdev);
3753 if (r)
3754 dev_err(rdev->dev, "failled initializing WB (%d).\n", r);
3755 r = r100_ib_init(rdev);
3756 if (r) {
3757 dev_err(rdev->dev, "failled initializing IB (%d).\n", r);
3758 return r;
3759 }
3760 return 0;
3761}
3762
3763int r100_resume(struct radeon_device *rdev)
3764{
3765 /* Make sur GART are not working */
3766 if (rdev->flags & RADEON_IS_PCI)
3767 r100_pci_gart_disable(rdev);
3768 /* Resume clock before doing reset */
3769 r100_clock_startup(rdev);
3770 /* Reset gpu before posting otherwise ATOM will enter infinite loop */
Jerome Glissea2d07b72010-03-09 14:45:11 +00003771 if (radeon_asic_reset(rdev)) {
Jerome Glissed4550902009-10-01 10:12:06 +02003772 dev_warn(rdev->dev, "GPU reset failed ! (0xE40=0x%08X, 0x7C0=0x%08X)\n",
3773 RREG32(R_000E40_RBBM_STATUS),
3774 RREG32(R_0007C0_CP_STAT));
3775 }
3776 /* post */
3777 radeon_combios_asic_init(rdev->ddev);
3778 /* Resume clock after posting */
3779 r100_clock_startup(rdev);
Dave Airlie550e2d92009-12-09 14:15:38 +10003780 /* Initialize surface registers */
3781 radeon_surface_init(rdev);
Jerome Glissed4550902009-10-01 10:12:06 +02003782 return r100_startup(rdev);
3783}
3784
3785int r100_suspend(struct radeon_device *rdev)
3786{
3787 r100_cp_disable(rdev);
3788 r100_wb_disable(rdev);
3789 r100_irq_disable(rdev);
3790 if (rdev->flags & RADEON_IS_PCI)
3791 r100_pci_gart_disable(rdev);
3792 return 0;
3793}
3794
3795void r100_fini(struct radeon_device *rdev)
3796{
Jerome Glissed4550902009-10-01 10:12:06 +02003797 r100_cp_fini(rdev);
3798 r100_wb_fini(rdev);
3799 r100_ib_fini(rdev);
3800 radeon_gem_fini(rdev);
3801 if (rdev->flags & RADEON_IS_PCI)
3802 r100_pci_gart_fini(rdev);
Jerome Glissed0269ed2010-01-07 16:08:32 +01003803 radeon_agp_fini(rdev);
Jerome Glissed4550902009-10-01 10:12:06 +02003804 radeon_irq_kms_fini(rdev);
3805 radeon_fence_driver_fini(rdev);
Jerome Glisse4c788672009-11-20 14:29:23 +01003806 radeon_bo_fini(rdev);
Jerome Glissed4550902009-10-01 10:12:06 +02003807 radeon_atombios_fini(rdev);
3808 kfree(rdev->bios);
3809 rdev->bios = NULL;
3810}
3811
Dave Airlie4c712e62010-07-15 12:13:50 +10003812/*
3813 * Due to how kexec works, it can leave the hw fully initialised when it
3814 * boots the new kernel. However doing our init sequence with the CP and
3815 * WB stuff setup causes GPU hangs on the RN50 at least. So at startup
3816 * do some quick sanity checks and restore sane values to avoid this
3817 * problem.
3818 */
3819void r100_restore_sanity(struct radeon_device *rdev)
3820{
3821 u32 tmp;
3822
3823 tmp = RREG32(RADEON_CP_CSQ_CNTL);
3824 if (tmp) {
3825 WREG32(RADEON_CP_CSQ_CNTL, 0);
3826 }
3827 tmp = RREG32(RADEON_CP_RB_CNTL);
3828 if (tmp) {
3829 WREG32(RADEON_CP_RB_CNTL, 0);
3830 }
3831 tmp = RREG32(RADEON_SCRATCH_UMSK);
3832 if (tmp) {
3833 WREG32(RADEON_SCRATCH_UMSK, 0);
3834 }
3835}
3836
Jerome Glissed4550902009-10-01 10:12:06 +02003837int r100_init(struct radeon_device *rdev)
3838{
3839 int r;
3840
Jerome Glissed4550902009-10-01 10:12:06 +02003841 /* Register debugfs file specific to this group of asics */
3842 r100_debugfs(rdev);
3843 /* Disable VGA */
3844 r100_vga_render_disable(rdev);
3845 /* Initialize scratch registers */
3846 radeon_scratch_init(rdev);
3847 /* Initialize surface registers */
3848 radeon_surface_init(rdev);
Dave Airlie4c712e62010-07-15 12:13:50 +10003849 /* sanity check some register to avoid hangs like after kexec */
3850 r100_restore_sanity(rdev);
Jerome Glissed4550902009-10-01 10:12:06 +02003851 /* TODO: disable VGA need to use VGA request */
3852 /* BIOS*/
3853 if (!radeon_get_bios(rdev)) {
3854 if (ASIC_IS_AVIVO(rdev))
3855 return -EINVAL;
3856 }
3857 if (rdev->is_atom_bios) {
3858 dev_err(rdev->dev, "Expecting combios for RS400/RS480 GPU\n");
3859 return -EINVAL;
3860 } else {
3861 r = radeon_combios_init(rdev);
3862 if (r)
3863 return r;
3864 }
3865 /* Reset gpu before posting otherwise ATOM will enter infinite loop */
Jerome Glissea2d07b72010-03-09 14:45:11 +00003866 if (radeon_asic_reset(rdev)) {
Jerome Glissed4550902009-10-01 10:12:06 +02003867 dev_warn(rdev->dev,
3868 "GPU reset failed ! (0xE40=0x%08X, 0x7C0=0x%08X)\n",
3869 RREG32(R_000E40_RBBM_STATUS),
3870 RREG32(R_0007C0_CP_STAT));
3871 }
3872 /* check if cards are posted or not */
Dave Airlie72542d72009-12-01 14:06:31 +10003873 if (radeon_boot_test_post_card(rdev) == false)
3874 return -EINVAL;
Jerome Glissed4550902009-10-01 10:12:06 +02003875 /* Set asic errata */
3876 r100_errata(rdev);
3877 /* Initialize clocks */
3878 radeon_get_clock_info(rdev->ddev);
Jerome Glissed594e462010-02-17 21:54:29 +00003879 /* initialize AGP */
3880 if (rdev->flags & RADEON_IS_AGP) {
3881 r = radeon_agp_init(rdev);
3882 if (r) {
3883 radeon_agp_disable(rdev);
3884 }
3885 }
3886 /* initialize VRAM */
3887 r100_mc_init(rdev);
Jerome Glissed4550902009-10-01 10:12:06 +02003888 /* Fence driver */
3889 r = radeon_fence_driver_init(rdev);
3890 if (r)
3891 return r;
3892 r = radeon_irq_kms_init(rdev);
3893 if (r)
3894 return r;
3895 /* Memory manager */
Jerome Glisse4c788672009-11-20 14:29:23 +01003896 r = radeon_bo_init(rdev);
Jerome Glissed4550902009-10-01 10:12:06 +02003897 if (r)
3898 return r;
3899 if (rdev->flags & RADEON_IS_PCI) {
3900 r = r100_pci_gart_init(rdev);
3901 if (r)
3902 return r;
3903 }
3904 r100_set_safe_registers(rdev);
3905 rdev->accel_working = true;
3906 r = r100_startup(rdev);
3907 if (r) {
3908 /* Somethings want wront with the accel init stop accel */
3909 dev_err(rdev->dev, "Disabling GPU acceleration\n");
Jerome Glissed4550902009-10-01 10:12:06 +02003910 r100_cp_fini(rdev);
3911 r100_wb_fini(rdev);
3912 r100_ib_fini(rdev);
Jerome Glisse655efd32010-02-02 11:51:45 +01003913 radeon_irq_kms_fini(rdev);
Jerome Glissed4550902009-10-01 10:12:06 +02003914 if (rdev->flags & RADEON_IS_PCI)
3915 r100_pci_gart_fini(rdev);
Jerome Glissed4550902009-10-01 10:12:06 +02003916 rdev->accel_working = false;
3917 }
3918 return 0;
3919}