blob: ebdce08cfefc22c1eadd50df2c514c437ce66c1e [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>
David Howells760285e2012-10-02 18:01:07 +010030#include <drm/drmP.h>
31#include <drm/radeon_drm.h>
Jerome Glisse771fe6b2009-06-05 14:42:42 +020032#include "radeon_reg.h"
33#include "radeon.h"
Daniel Vettere6990372010-03-11 21:19:17 +000034#include "radeon_asic.h"
Jerome Glisse3ce0a232009-09-08 10:10:24 +100035#include "r100d.h"
Jerome Glissed4550902009-10-01 10:12:06 +020036#include "rs100d.h"
37#include "rv200d.h"
38#include "rv250d.h"
Alex Deucher49e02b72010-04-23 17:57:27 -040039#include "atom.h"
Jerome Glisse3ce0a232009-09-08 10:10:24 +100040
Ben Hutchings70967ab2009-08-29 14:53:51 +010041#include <linux/firmware.h>
Paul Gortmakere0cd3602011-08-30 11:04:30 -040042#include <linux/module.h>
Ben Hutchings70967ab2009-08-29 14:53:51 +010043
Dave Airlie551ebd82009-09-01 15:25:57 +100044#include "r100_reg_safe.h"
45#include "rn50_reg_safe.h"
46
Ben Hutchings70967ab2009-08-29 14:53:51 +010047/* Firmware Names */
48#define FIRMWARE_R100 "radeon/R100_cp.bin"
49#define FIRMWARE_R200 "radeon/R200_cp.bin"
50#define FIRMWARE_R300 "radeon/R300_cp.bin"
51#define FIRMWARE_R420 "radeon/R420_cp.bin"
52#define FIRMWARE_RS690 "radeon/RS690_cp.bin"
53#define FIRMWARE_RS600 "radeon/RS600_cp.bin"
54#define FIRMWARE_R520 "radeon/R520_cp.bin"
55
56MODULE_FIRMWARE(FIRMWARE_R100);
57MODULE_FIRMWARE(FIRMWARE_R200);
58MODULE_FIRMWARE(FIRMWARE_R300);
59MODULE_FIRMWARE(FIRMWARE_R420);
60MODULE_FIRMWARE(FIRMWARE_RS690);
61MODULE_FIRMWARE(FIRMWARE_RS600);
62MODULE_FIRMWARE(FIRMWARE_R520);
Jerome Glisse771fe6b2009-06-05 14:42:42 +020063
Dave Airlie551ebd82009-09-01 15:25:57 +100064#include "r100_track.h"
65
Alex Deucher48ef7792012-07-17 14:02:41 -040066/* This files gather functions specifics to:
67 * r100,rv100,rs100,rv200,rs200,r200,rv250,rs300,rv280
68 * and others in some cases.
69 */
70
Alex Deucher2b48b962013-04-09 18:32:01 -040071static bool r100_is_in_vblank(struct radeon_device *rdev, int crtc)
72{
73 if (crtc == 0) {
74 if (RREG32(RADEON_CRTC_STATUS) & RADEON_CRTC_VBLANK_CUR)
75 return true;
76 else
77 return false;
78 } else {
79 if (RREG32(RADEON_CRTC2_STATUS) & RADEON_CRTC2_VBLANK_CUR)
80 return true;
81 else
82 return false;
83 }
84}
85
86static bool r100_is_counter_moving(struct radeon_device *rdev, int crtc)
87{
88 u32 vline1, vline2;
89
90 if (crtc == 0) {
91 vline1 = (RREG32(RADEON_CRTC_VLINE_CRNT_VLINE) >> 16) & RADEON_CRTC_V_TOTAL;
92 vline2 = (RREG32(RADEON_CRTC_VLINE_CRNT_VLINE) >> 16) & RADEON_CRTC_V_TOTAL;
93 } else {
94 vline1 = (RREG32(RADEON_CRTC2_VLINE_CRNT_VLINE) >> 16) & RADEON_CRTC_V_TOTAL;
95 vline2 = (RREG32(RADEON_CRTC2_VLINE_CRNT_VLINE) >> 16) & RADEON_CRTC_V_TOTAL;
96 }
97 if (vline1 != vline2)
98 return true;
99 else
100 return false;
101}
102
Alex Deucher48ef7792012-07-17 14:02:41 -0400103/**
104 * r100_wait_for_vblank - vblank wait asic callback.
105 *
106 * @rdev: radeon_device pointer
107 * @crtc: crtc to wait for vblank on
108 *
109 * Wait for vblank on the requested crtc (r1xx-r4xx).
110 */
Alex Deucher3ae19b72012-02-23 17:53:37 -0500111void r100_wait_for_vblank(struct radeon_device *rdev, int crtc)
112{
Alex Deucher2b48b962013-04-09 18:32:01 -0400113 unsigned i = 0;
Alex Deucher3ae19b72012-02-23 17:53:37 -0500114
Alex Deucher94f768f2012-08-15 16:58:30 -0400115 if (crtc >= rdev->num_crtc)
116 return;
117
118 if (crtc == 0) {
Alex Deucher2b48b962013-04-09 18:32:01 -0400119 if (!(RREG32(RADEON_CRTC_GEN_CNTL) & RADEON_CRTC_EN))
120 return;
Alex Deucher3ae19b72012-02-23 17:53:37 -0500121 } else {
Alex Deucher2b48b962013-04-09 18:32:01 -0400122 if (!(RREG32(RADEON_CRTC2_GEN_CNTL) & RADEON_CRTC2_EN))
123 return;
124 }
125
126 /* depending on when we hit vblank, we may be close to active; if so,
127 * wait for another frame.
128 */
129 while (r100_is_in_vblank(rdev, crtc)) {
130 if (i++ % 100 == 0) {
131 if (!r100_is_counter_moving(rdev, crtc))
132 break;
133 }
134 }
135
136 while (!r100_is_in_vblank(rdev, crtc)) {
137 if (i++ % 100 == 0) {
138 if (!r100_is_counter_moving(rdev, crtc))
139 break;
Alex Deucher3ae19b72012-02-23 17:53:37 -0500140 }
141 }
142}
143
Alex Deucher48ef7792012-07-17 14:02:41 -0400144/**
Alex Deucher48ef7792012-07-17 14:02:41 -0400145 * r100_page_flip - pageflip callback.
146 *
147 * @rdev: radeon_device pointer
148 * @crtc_id: crtc to cleanup pageflip on
149 * @crtc_base: new address of the crtc (GPU MC address)
150 *
151 * Does the actual pageflip (r1xx-r4xx).
152 * During vblank we take the crtc lock and wait for the update_pending
153 * bit to go high, when it does, we release the lock, and allow the
154 * double buffered update to take place.
Alex Deucher48ef7792012-07-17 14:02:41 -0400155 */
Christian König157fa142014-05-27 16:49:20 +0200156void r100_page_flip(struct radeon_device *rdev, int crtc_id, u64 crtc_base)
Alex Deucher6f34be52010-11-21 10:59:01 -0500157{
158 struct radeon_crtc *radeon_crtc = rdev->mode_info.crtcs[crtc_id];
159 u32 tmp = ((u32)crtc_base) | RADEON_CRTC_OFFSET__OFFSET_LOCK;
Alex Deucherf6496472011-11-28 14:49:26 -0500160 int i;
Alex Deucher6f34be52010-11-21 10:59:01 -0500161
162 /* Lock the graphics update lock */
163 /* update the scanout addresses */
164 WREG32(RADEON_CRTC_OFFSET + radeon_crtc->crtc_offset, tmp);
165
Alex Deucheracb32502010-11-23 00:41:00 -0500166 /* Wait for update_pending to go high. */
Alex Deucherf6496472011-11-28 14:49:26 -0500167 for (i = 0; i < rdev->usec_timeout; i++) {
168 if (RREG32(RADEON_CRTC_OFFSET + radeon_crtc->crtc_offset) & RADEON_CRTC_OFFSET__GUI_TRIG_OFFSET)
169 break;
170 udelay(1);
171 }
Alex Deucheracb32502010-11-23 00:41:00 -0500172 DRM_DEBUG("Update pending now high. Unlocking vupdate_lock.\n");
Alex Deucher6f34be52010-11-21 10:59:01 -0500173
174 /* Unlock the lock, so double-buffering can take place inside vblank */
175 tmp &= ~RADEON_CRTC_OFFSET__OFFSET_LOCK;
176 WREG32(RADEON_CRTC_OFFSET + radeon_crtc->crtc_offset, tmp);
177
Christian König157fa142014-05-27 16:49:20 +0200178}
179
180/**
181 * r100_page_flip_pending - check if page flip is still pending
182 *
183 * @rdev: radeon_device pointer
184 * @crtc_id: crtc to check
185 *
186 * Check if the last pagefilp is still pending (r1xx-r4xx).
187 * Returns the current update pending status.
188 */
189bool r100_page_flip_pending(struct radeon_device *rdev, int crtc_id)
190{
191 struct radeon_crtc *radeon_crtc = rdev->mode_info.crtcs[crtc_id];
192
Alex Deucher6f34be52010-11-21 10:59:01 -0500193 /* Return current update_pending status: */
Christian König157fa142014-05-27 16:49:20 +0200194 return !!(RREG32(RADEON_CRTC_OFFSET + radeon_crtc->crtc_offset) &
195 RADEON_CRTC_OFFSET__GUI_TRIG_OFFSET);
Alex Deucher6f34be52010-11-21 10:59:01 -0500196}
197
Alex Deucher48ef7792012-07-17 14:02:41 -0400198/**
199 * r100_pm_get_dynpm_state - look up dynpm power state callback.
200 *
201 * @rdev: radeon_device pointer
202 *
203 * Look up the optimal power state based on the
204 * current state of the GPU (r1xx-r5xx).
205 * Used for dynpm only.
206 */
Alex Deucherce8f5372010-05-07 15:10:16 -0400207void r100_pm_get_dynpm_state(struct radeon_device *rdev)
Alex Deuchera48b9b42010-04-22 14:03:55 -0400208{
209 int i;
Alex Deucherce8f5372010-05-07 15:10:16 -0400210 rdev->pm.dynpm_can_upclock = true;
211 rdev->pm.dynpm_can_downclock = true;
Alex Deuchera48b9b42010-04-22 14:03:55 -0400212
Alex Deucherce8f5372010-05-07 15:10:16 -0400213 switch (rdev->pm.dynpm_planned_action) {
214 case DYNPM_ACTION_MINIMUM:
Alex Deuchera48b9b42010-04-22 14:03:55 -0400215 rdev->pm.requested_power_state_index = 0;
Alex Deucherce8f5372010-05-07 15:10:16 -0400216 rdev->pm.dynpm_can_downclock = false;
Alex Deuchera48b9b42010-04-22 14:03:55 -0400217 break;
Alex Deucherce8f5372010-05-07 15:10:16 -0400218 case DYNPM_ACTION_DOWNCLOCK:
Alex Deuchera48b9b42010-04-22 14:03:55 -0400219 if (rdev->pm.current_power_state_index == 0) {
220 rdev->pm.requested_power_state_index = rdev->pm.current_power_state_index;
Alex Deucherce8f5372010-05-07 15:10:16 -0400221 rdev->pm.dynpm_can_downclock = false;
Alex Deuchera48b9b42010-04-22 14:03:55 -0400222 } else {
223 if (rdev->pm.active_crtc_count > 1) {
224 for (i = 0; i < rdev->pm.num_power_states; i++) {
Alex Deucherd7311172010-05-03 01:13:14 -0400225 if (rdev->pm.power_state[i].flags & RADEON_PM_STATE_SINGLE_DISPLAY_ONLY)
Alex Deuchera48b9b42010-04-22 14:03:55 -0400226 continue;
227 else if (i >= rdev->pm.current_power_state_index) {
228 rdev->pm.requested_power_state_index = rdev->pm.current_power_state_index;
229 break;
230 } else {
231 rdev->pm.requested_power_state_index = i;
232 break;
233 }
234 }
235 } else
236 rdev->pm.requested_power_state_index =
237 rdev->pm.current_power_state_index - 1;
238 }
Alex Deucherd7311172010-05-03 01:13:14 -0400239 /* don't use the power state if crtcs are active and no display flag is set */
240 if ((rdev->pm.active_crtc_count > 0) &&
241 (rdev->pm.power_state[rdev->pm.requested_power_state_index].clock_info[0].flags &
242 RADEON_PM_MODE_NO_DISPLAY)) {
243 rdev->pm.requested_power_state_index++;
244 }
Alex Deuchera48b9b42010-04-22 14:03:55 -0400245 break;
Alex Deucherce8f5372010-05-07 15:10:16 -0400246 case DYNPM_ACTION_UPCLOCK:
Alex Deuchera48b9b42010-04-22 14:03:55 -0400247 if (rdev->pm.current_power_state_index == (rdev->pm.num_power_states - 1)) {
248 rdev->pm.requested_power_state_index = rdev->pm.current_power_state_index;
Alex Deucherce8f5372010-05-07 15:10:16 -0400249 rdev->pm.dynpm_can_upclock = false;
Alex Deuchera48b9b42010-04-22 14:03:55 -0400250 } else {
251 if (rdev->pm.active_crtc_count > 1) {
252 for (i = (rdev->pm.num_power_states - 1); i >= 0; i--) {
Alex Deucherd7311172010-05-03 01:13:14 -0400253 if (rdev->pm.power_state[i].flags & RADEON_PM_STATE_SINGLE_DISPLAY_ONLY)
Alex Deuchera48b9b42010-04-22 14:03:55 -0400254 continue;
255 else if (i <= rdev->pm.current_power_state_index) {
256 rdev->pm.requested_power_state_index = rdev->pm.current_power_state_index;
257 break;
258 } else {
259 rdev->pm.requested_power_state_index = i;
260 break;
261 }
262 }
263 } else
264 rdev->pm.requested_power_state_index =
265 rdev->pm.current_power_state_index + 1;
266 }
267 break;
Alex Deucherce8f5372010-05-07 15:10:16 -0400268 case DYNPM_ACTION_DEFAULT:
Alex Deucher58e21df2010-03-22 13:31:08 -0400269 rdev->pm.requested_power_state_index = rdev->pm.default_power_state_index;
Alex Deucherce8f5372010-05-07 15:10:16 -0400270 rdev->pm.dynpm_can_upclock = false;
Alex Deucher58e21df2010-03-22 13:31:08 -0400271 break;
Alex Deucherce8f5372010-05-07 15:10:16 -0400272 case DYNPM_ACTION_NONE:
Alex Deuchera48b9b42010-04-22 14:03:55 -0400273 default:
274 DRM_ERROR("Requested mode for not defined action\n");
275 return;
276 }
277 /* only one clock mode per power state */
278 rdev->pm.requested_clock_mode_index = 0;
279
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000280 DRM_DEBUG_DRIVER("Requested: e: %d m: %d p: %d\n",
Alex Deucherce8a3eb2010-05-07 16:58:27 -0400281 rdev->pm.power_state[rdev->pm.requested_power_state_index].
282 clock_info[rdev->pm.requested_clock_mode_index].sclk,
283 rdev->pm.power_state[rdev->pm.requested_power_state_index].
284 clock_info[rdev->pm.requested_clock_mode_index].mclk,
285 rdev->pm.power_state[rdev->pm.requested_power_state_index].
286 pcie_lanes);
Alex Deuchera48b9b42010-04-22 14:03:55 -0400287}
288
Alex Deucher48ef7792012-07-17 14:02:41 -0400289/**
290 * r100_pm_init_profile - Initialize power profiles callback.
291 *
292 * @rdev: radeon_device pointer
293 *
294 * Initialize the power states used in profile mode
295 * (r1xx-r3xx).
296 * Used for profile mode only.
297 */
Alex Deucherce8f5372010-05-07 15:10:16 -0400298void r100_pm_init_profile(struct radeon_device *rdev)
Alex Deucherbae6b5622010-04-22 13:38:05 -0400299{
Alex Deucherce8f5372010-05-07 15:10:16 -0400300 /* default */
301 rdev->pm.profiles[PM_PROFILE_DEFAULT_IDX].dpms_off_ps_idx = rdev->pm.default_power_state_index;
302 rdev->pm.profiles[PM_PROFILE_DEFAULT_IDX].dpms_on_ps_idx = rdev->pm.default_power_state_index;
303 rdev->pm.profiles[PM_PROFILE_DEFAULT_IDX].dpms_off_cm_idx = 0;
304 rdev->pm.profiles[PM_PROFILE_DEFAULT_IDX].dpms_on_cm_idx = 0;
305 /* low sh */
306 rdev->pm.profiles[PM_PROFILE_LOW_SH_IDX].dpms_off_ps_idx = 0;
307 rdev->pm.profiles[PM_PROFILE_LOW_SH_IDX].dpms_on_ps_idx = 0;
308 rdev->pm.profiles[PM_PROFILE_LOW_SH_IDX].dpms_off_cm_idx = 0;
309 rdev->pm.profiles[PM_PROFILE_LOW_SH_IDX].dpms_on_cm_idx = 0;
Alex Deucherc9e75b22010-06-02 17:56:01 -0400310 /* mid sh */
311 rdev->pm.profiles[PM_PROFILE_MID_SH_IDX].dpms_off_ps_idx = 0;
312 rdev->pm.profiles[PM_PROFILE_MID_SH_IDX].dpms_on_ps_idx = 0;
313 rdev->pm.profiles[PM_PROFILE_MID_SH_IDX].dpms_off_cm_idx = 0;
314 rdev->pm.profiles[PM_PROFILE_MID_SH_IDX].dpms_on_cm_idx = 0;
Alex Deucherce8f5372010-05-07 15:10:16 -0400315 /* high sh */
316 rdev->pm.profiles[PM_PROFILE_HIGH_SH_IDX].dpms_off_ps_idx = 0;
317 rdev->pm.profiles[PM_PROFILE_HIGH_SH_IDX].dpms_on_ps_idx = rdev->pm.default_power_state_index;
318 rdev->pm.profiles[PM_PROFILE_HIGH_SH_IDX].dpms_off_cm_idx = 0;
319 rdev->pm.profiles[PM_PROFILE_HIGH_SH_IDX].dpms_on_cm_idx = 0;
320 /* low mh */
321 rdev->pm.profiles[PM_PROFILE_LOW_MH_IDX].dpms_off_ps_idx = 0;
322 rdev->pm.profiles[PM_PROFILE_LOW_MH_IDX].dpms_on_ps_idx = rdev->pm.default_power_state_index;
323 rdev->pm.profiles[PM_PROFILE_LOW_MH_IDX].dpms_off_cm_idx = 0;
324 rdev->pm.profiles[PM_PROFILE_LOW_MH_IDX].dpms_on_cm_idx = 0;
Alex Deucherc9e75b22010-06-02 17:56:01 -0400325 /* mid mh */
326 rdev->pm.profiles[PM_PROFILE_MID_MH_IDX].dpms_off_ps_idx = 0;
327 rdev->pm.profiles[PM_PROFILE_MID_MH_IDX].dpms_on_ps_idx = rdev->pm.default_power_state_index;
328 rdev->pm.profiles[PM_PROFILE_MID_MH_IDX].dpms_off_cm_idx = 0;
329 rdev->pm.profiles[PM_PROFILE_MID_MH_IDX].dpms_on_cm_idx = 0;
Alex Deucherce8f5372010-05-07 15:10:16 -0400330 /* high mh */
331 rdev->pm.profiles[PM_PROFILE_HIGH_MH_IDX].dpms_off_ps_idx = 0;
332 rdev->pm.profiles[PM_PROFILE_HIGH_MH_IDX].dpms_on_ps_idx = rdev->pm.default_power_state_index;
333 rdev->pm.profiles[PM_PROFILE_HIGH_MH_IDX].dpms_off_cm_idx = 0;
334 rdev->pm.profiles[PM_PROFILE_HIGH_MH_IDX].dpms_on_cm_idx = 0;
Alex Deucherbae6b5622010-04-22 13:38:05 -0400335}
336
Alex Deucher48ef7792012-07-17 14:02:41 -0400337/**
338 * r100_pm_misc - set additional pm hw parameters callback.
339 *
340 * @rdev: radeon_device pointer
341 *
342 * Set non-clock parameters associated with a power state
343 * (voltage, pcie lanes, etc.) (r1xx-r4xx).
344 */
Alex Deucher49e02b72010-04-23 17:57:27 -0400345void r100_pm_misc(struct radeon_device *rdev)
346{
Alex Deucher49e02b72010-04-23 17:57:27 -0400347 int requested_index = rdev->pm.requested_power_state_index;
348 struct radeon_power_state *ps = &rdev->pm.power_state[requested_index];
349 struct radeon_voltage *voltage = &ps->clock_info[0].voltage;
350 u32 tmp, sclk_cntl, sclk_cntl2, sclk_more_cntl;
351
352 if ((voltage->type == VOLTAGE_GPIO) && (voltage->gpio.valid)) {
353 if (ps->misc & ATOM_PM_MISCINFO_VOLTAGE_DROP_SUPPORT) {
354 tmp = RREG32(voltage->gpio.reg);
355 if (voltage->active_high)
356 tmp |= voltage->gpio.mask;
357 else
358 tmp &= ~(voltage->gpio.mask);
359 WREG32(voltage->gpio.reg, tmp);
360 if (voltage->delay)
361 udelay(voltage->delay);
362 } else {
363 tmp = RREG32(voltage->gpio.reg);
364 if (voltage->active_high)
365 tmp &= ~voltage->gpio.mask;
366 else
367 tmp |= voltage->gpio.mask;
368 WREG32(voltage->gpio.reg, tmp);
369 if (voltage->delay)
370 udelay(voltage->delay);
371 }
372 }
373
374 sclk_cntl = RREG32_PLL(SCLK_CNTL);
375 sclk_cntl2 = RREG32_PLL(SCLK_CNTL2);
376 sclk_cntl2 &= ~REDUCED_SPEED_SCLK_SEL(3);
377 sclk_more_cntl = RREG32_PLL(SCLK_MORE_CNTL);
378 sclk_more_cntl &= ~VOLTAGE_DELAY_SEL(3);
379 if (ps->misc & ATOM_PM_MISCINFO_ASIC_REDUCED_SPEED_SCLK_EN) {
380 sclk_more_cntl |= REDUCED_SPEED_SCLK_EN;
381 if (ps->misc & ATOM_PM_MISCINFO_DYN_CLK_3D_IDLE)
382 sclk_cntl2 |= REDUCED_SPEED_SCLK_MODE;
383 else
384 sclk_cntl2 &= ~REDUCED_SPEED_SCLK_MODE;
385 if (ps->misc & ATOM_PM_MISCINFO_DYNAMIC_CLOCK_DIVIDER_BY_2)
386 sclk_cntl2 |= REDUCED_SPEED_SCLK_SEL(0);
387 else if (ps->misc & ATOM_PM_MISCINFO_DYNAMIC_CLOCK_DIVIDER_BY_4)
388 sclk_cntl2 |= REDUCED_SPEED_SCLK_SEL(2);
389 } else
390 sclk_more_cntl &= ~REDUCED_SPEED_SCLK_EN;
391
392 if (ps->misc & ATOM_PM_MISCINFO_ASIC_DYNAMIC_VOLTAGE_EN) {
393 sclk_more_cntl |= IO_CG_VOLTAGE_DROP;
394 if (voltage->delay) {
395 sclk_more_cntl |= VOLTAGE_DROP_SYNC;
396 switch (voltage->delay) {
397 case 33:
398 sclk_more_cntl |= VOLTAGE_DELAY_SEL(0);
399 break;
400 case 66:
401 sclk_more_cntl |= VOLTAGE_DELAY_SEL(1);
402 break;
403 case 99:
404 sclk_more_cntl |= VOLTAGE_DELAY_SEL(2);
405 break;
406 case 132:
407 sclk_more_cntl |= VOLTAGE_DELAY_SEL(3);
408 break;
409 }
410 } else
411 sclk_more_cntl &= ~VOLTAGE_DROP_SYNC;
412 } else
413 sclk_more_cntl &= ~IO_CG_VOLTAGE_DROP;
414
415 if (ps->misc & ATOM_PM_MISCINFO_DYNAMIC_HDP_BLOCK_EN)
416 sclk_cntl &= ~FORCE_HDP;
417 else
418 sclk_cntl |= FORCE_HDP;
419
420 WREG32_PLL(SCLK_CNTL, sclk_cntl);
421 WREG32_PLL(SCLK_CNTL2, sclk_cntl2);
422 WREG32_PLL(SCLK_MORE_CNTL, sclk_more_cntl);
423
424 /* set pcie lanes */
425 if ((rdev->flags & RADEON_IS_PCIE) &&
426 !(rdev->flags & RADEON_IS_IGP) &&
Alex Deucher798bcf72012-02-23 17:53:48 -0500427 rdev->asic->pm.set_pcie_lanes &&
Alex Deucher49e02b72010-04-23 17:57:27 -0400428 (ps->pcie_lanes !=
429 rdev->pm.power_state[rdev->pm.current_power_state_index].pcie_lanes)) {
430 radeon_set_pcie_lanes(rdev,
431 ps->pcie_lanes);
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000432 DRM_DEBUG_DRIVER("Setting: p: %d\n", ps->pcie_lanes);
Alex Deucher49e02b72010-04-23 17:57:27 -0400433 }
Alex Deucher49e02b72010-04-23 17:57:27 -0400434}
435
Alex Deucher48ef7792012-07-17 14:02:41 -0400436/**
437 * r100_pm_prepare - pre-power state change callback.
438 *
439 * @rdev: radeon_device pointer
440 *
441 * Prepare for a power state change (r1xx-r4xx).
442 */
Alex Deucher49e02b72010-04-23 17:57:27 -0400443void r100_pm_prepare(struct radeon_device *rdev)
444{
445 struct drm_device *ddev = rdev->ddev;
446 struct drm_crtc *crtc;
447 struct radeon_crtc *radeon_crtc;
448 u32 tmp;
449
450 /* disable any active CRTCs */
451 list_for_each_entry(crtc, &ddev->mode_config.crtc_list, head) {
452 radeon_crtc = to_radeon_crtc(crtc);
453 if (radeon_crtc->enabled) {
454 if (radeon_crtc->crtc_id) {
455 tmp = RREG32(RADEON_CRTC2_GEN_CNTL);
456 tmp |= RADEON_CRTC2_DISP_REQ_EN_B;
457 WREG32(RADEON_CRTC2_GEN_CNTL, tmp);
458 } else {
459 tmp = RREG32(RADEON_CRTC_GEN_CNTL);
460 tmp |= RADEON_CRTC_DISP_REQ_EN_B;
461 WREG32(RADEON_CRTC_GEN_CNTL, tmp);
462 }
463 }
464 }
465}
466
Alex Deucher48ef7792012-07-17 14:02:41 -0400467/**
468 * r100_pm_finish - post-power state change callback.
469 *
470 * @rdev: radeon_device pointer
471 *
472 * Clean up after a power state change (r1xx-r4xx).
473 */
Alex Deucher49e02b72010-04-23 17:57:27 -0400474void r100_pm_finish(struct radeon_device *rdev)
475{
476 struct drm_device *ddev = rdev->ddev;
477 struct drm_crtc *crtc;
478 struct radeon_crtc *radeon_crtc;
479 u32 tmp;
480
481 /* enable any active CRTCs */
482 list_for_each_entry(crtc, &ddev->mode_config.crtc_list, head) {
483 radeon_crtc = to_radeon_crtc(crtc);
484 if (radeon_crtc->enabled) {
485 if (radeon_crtc->crtc_id) {
486 tmp = RREG32(RADEON_CRTC2_GEN_CNTL);
487 tmp &= ~RADEON_CRTC2_DISP_REQ_EN_B;
488 WREG32(RADEON_CRTC2_GEN_CNTL, tmp);
489 } else {
490 tmp = RREG32(RADEON_CRTC_GEN_CNTL);
491 tmp &= ~RADEON_CRTC_DISP_REQ_EN_B;
492 WREG32(RADEON_CRTC_GEN_CNTL, tmp);
493 }
494 }
495 }
496}
497
Alex Deucher48ef7792012-07-17 14:02:41 -0400498/**
499 * r100_gui_idle - gui idle callback.
500 *
501 * @rdev: radeon_device pointer
502 *
503 * Check of the GUI (2D/3D engines) are idle (r1xx-r5xx).
504 * Returns true if idle, false if not.
505 */
Alex Deucherdef9ba92010-04-22 12:39:58 -0400506bool r100_gui_idle(struct radeon_device *rdev)
507{
508 if (RREG32(RADEON_RBBM_STATUS) & RADEON_RBBM_ACTIVE)
509 return false;
510 else
511 return true;
512}
513
Alex Deucher05a05c52009-12-04 14:53:41 -0500514/* hpd for digital panel detect/disconnect */
Alex Deucher48ef7792012-07-17 14:02:41 -0400515/**
516 * r100_hpd_sense - hpd sense callback.
517 *
518 * @rdev: radeon_device pointer
519 * @hpd: hpd (hotplug detect) pin
520 *
521 * Checks if a digital monitor is connected (r1xx-r4xx).
522 * Returns true if connected, false if not connected.
523 */
Alex Deucher05a05c52009-12-04 14:53:41 -0500524bool r100_hpd_sense(struct radeon_device *rdev, enum radeon_hpd_id hpd)
525{
526 bool connected = false;
527
528 switch (hpd) {
529 case RADEON_HPD_1:
530 if (RREG32(RADEON_FP_GEN_CNTL) & RADEON_FP_DETECT_SENSE)
531 connected = true;
532 break;
533 case RADEON_HPD_2:
534 if (RREG32(RADEON_FP2_GEN_CNTL) & RADEON_FP2_DETECT_SENSE)
535 connected = true;
536 break;
537 default:
538 break;
539 }
540 return connected;
541}
542
Alex Deucher48ef7792012-07-17 14:02:41 -0400543/**
544 * r100_hpd_set_polarity - hpd set polarity callback.
545 *
546 * @rdev: radeon_device pointer
547 * @hpd: hpd (hotplug detect) pin
548 *
549 * Set the polarity of the hpd pin (r1xx-r4xx).
550 */
Alex Deucher05a05c52009-12-04 14:53:41 -0500551void r100_hpd_set_polarity(struct radeon_device *rdev,
552 enum radeon_hpd_id hpd)
553{
554 u32 tmp;
555 bool connected = r100_hpd_sense(rdev, hpd);
556
557 switch (hpd) {
558 case RADEON_HPD_1:
559 tmp = RREG32(RADEON_FP_GEN_CNTL);
560 if (connected)
561 tmp &= ~RADEON_FP_DETECT_INT_POL;
562 else
563 tmp |= RADEON_FP_DETECT_INT_POL;
564 WREG32(RADEON_FP_GEN_CNTL, tmp);
565 break;
566 case RADEON_HPD_2:
567 tmp = RREG32(RADEON_FP2_GEN_CNTL);
568 if (connected)
569 tmp &= ~RADEON_FP2_DETECT_INT_POL;
570 else
571 tmp |= RADEON_FP2_DETECT_INT_POL;
572 WREG32(RADEON_FP2_GEN_CNTL, tmp);
573 break;
574 default:
575 break;
576 }
577}
578
Alex Deucher48ef7792012-07-17 14:02:41 -0400579/**
580 * r100_hpd_init - hpd setup callback.
581 *
582 * @rdev: radeon_device pointer
583 *
584 * Setup the hpd pins used by the card (r1xx-r4xx).
585 * Set the polarity, and enable the hpd interrupts.
586 */
Alex Deucher05a05c52009-12-04 14:53:41 -0500587void r100_hpd_init(struct radeon_device *rdev)
588{
589 struct drm_device *dev = rdev->ddev;
590 struct drm_connector *connector;
Christian Koenigfb982572012-05-17 01:33:30 +0200591 unsigned enable = 0;
Alex Deucher05a05c52009-12-04 14:53:41 -0500592
593 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
594 struct radeon_connector *radeon_connector = to_radeon_connector(connector);
Christian Koenigfb982572012-05-17 01:33:30 +0200595 enable |= 1 << radeon_connector->hpd.hpd;
Alex Deucher64912e92011-11-03 11:21:39 -0400596 radeon_hpd_set_polarity(rdev, radeon_connector->hpd.hpd);
Alex Deucher05a05c52009-12-04 14:53:41 -0500597 }
Christian Koenigfb982572012-05-17 01:33:30 +0200598 radeon_irq_kms_enable_hpd(rdev, enable);
Alex Deucher05a05c52009-12-04 14:53:41 -0500599}
600
Alex Deucher48ef7792012-07-17 14:02:41 -0400601/**
602 * r100_hpd_fini - hpd tear down callback.
603 *
604 * @rdev: radeon_device pointer
605 *
606 * Tear down the hpd pins used by the card (r1xx-r4xx).
607 * Disable the hpd interrupts.
608 */
Alex Deucher05a05c52009-12-04 14:53:41 -0500609void r100_hpd_fini(struct radeon_device *rdev)
610{
611 struct drm_device *dev = rdev->ddev;
612 struct drm_connector *connector;
Christian Koenigfb982572012-05-17 01:33:30 +0200613 unsigned disable = 0;
Alex Deucher05a05c52009-12-04 14:53:41 -0500614
615 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
616 struct radeon_connector *radeon_connector = to_radeon_connector(connector);
Christian Koenigfb982572012-05-17 01:33:30 +0200617 disable |= 1 << radeon_connector->hpd.hpd;
Alex Deucher05a05c52009-12-04 14:53:41 -0500618 }
Christian Koenigfb982572012-05-17 01:33:30 +0200619 radeon_irq_kms_disable_hpd(rdev, disable);
Alex Deucher05a05c52009-12-04 14:53:41 -0500620}
621
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200622/*
623 * PCI GART
624 */
625void r100_pci_gart_tlb_flush(struct radeon_device *rdev)
626{
627 /* TODO: can we do somethings here ? */
628 /* It seems hw only cache one entry so we should discard this
629 * entry otherwise if first GPU GART read hit this entry it
630 * could end up in wrong address. */
631}
632
Jerome Glisse4aac0472009-09-14 18:29:49 +0200633int r100_pci_gart_init(struct radeon_device *rdev)
634{
635 int r;
636
Jerome Glissec9a1be92011-11-03 11:16:49 -0400637 if (rdev->gart.ptr) {
Joe Perchesfce7d612010-10-30 21:08:30 +0000638 WARN(1, "R100 PCI GART already initialized\n");
Jerome Glisse4aac0472009-09-14 18:29:49 +0200639 return 0;
640 }
641 /* Initialize common gart structure */
642 r = radeon_gart_init(rdev);
643 if (r)
644 return r;
645 rdev->gart.table_size = rdev->gart.num_gpu_pages * 4;
Alex Deucherc5b3b852012-02-23 17:53:46 -0500646 rdev->asic->gart.tlb_flush = &r100_pci_gart_tlb_flush;
647 rdev->asic->gart.set_page = &r100_pci_gart_set_page;
Jerome Glisse4aac0472009-09-14 18:29:49 +0200648 return radeon_gart_table_ram_alloc(rdev);
649}
650
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200651int r100_pci_gart_enable(struct radeon_device *rdev)
652{
653 uint32_t tmp;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200654
Dave Airlie82568562010-02-05 16:00:07 +1000655 radeon_gart_restore(rdev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200656 /* discard memory request outside of configured range */
657 tmp = RREG32(RADEON_AIC_CNTL) | RADEON_DIS_OUT_OF_PCI_GART_ACCESS;
658 WREG32(RADEON_AIC_CNTL, tmp);
659 /* set address range for PCI address translate */
Jerome Glissed594e462010-02-17 21:54:29 +0000660 WREG32(RADEON_AIC_LO_ADDR, rdev->mc.gtt_start);
661 WREG32(RADEON_AIC_HI_ADDR, rdev->mc.gtt_end);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200662 /* set PCI GART page-table base address */
663 WREG32(RADEON_AIC_PT_BASE, rdev->gart.table_addr);
664 tmp = RREG32(RADEON_AIC_CNTL) | RADEON_PCIGART_TRANSLATE_EN;
665 WREG32(RADEON_AIC_CNTL, tmp);
666 r100_pci_gart_tlb_flush(rdev);
Michel Dänzer43caf452012-05-02 10:29:56 +0200667 DRM_INFO("PCI GART of %uM enabled (table at 0x%016llX).\n",
Tormod Voldenfcf4de52011-08-31 21:54:07 +0000668 (unsigned)(rdev->mc.gtt_size >> 20),
669 (unsigned long long)rdev->gart.table_addr);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200670 rdev->gart.ready = true;
671 return 0;
672}
673
674void r100_pci_gart_disable(struct radeon_device *rdev)
675{
676 uint32_t tmp;
677
678 /* discard memory request outside of configured range */
679 tmp = RREG32(RADEON_AIC_CNTL) | RADEON_DIS_OUT_OF_PCI_GART_ACCESS;
680 WREG32(RADEON_AIC_CNTL, tmp & ~RADEON_PCIGART_TRANSLATE_EN);
681 WREG32(RADEON_AIC_LO_ADDR, 0);
682 WREG32(RADEON_AIC_HI_ADDR, 0);
683}
684
Christian König7f90fc92014-06-04 15:29:57 +0200685void r100_pci_gart_set_page(struct radeon_device *rdev, unsigned i,
686 uint64_t addr)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200687{
Jerome Glissec9a1be92011-11-03 11:16:49 -0400688 u32 *gtt = rdev->gart.ptr;
Jerome Glissec9a1be92011-11-03 11:16:49 -0400689 gtt[i] = cpu_to_le32(lower_32_bits(addr));
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200690}
691
Jerome Glisse4aac0472009-09-14 18:29:49 +0200692void r100_pci_gart_fini(struct radeon_device *rdev)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200693{
Jerome Glissef9274562010-03-17 14:44:29 +0000694 radeon_gart_fini(rdev);
Jerome Glisse4aac0472009-09-14 18:29:49 +0200695 r100_pci_gart_disable(rdev);
696 radeon_gart_table_ram_free(rdev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200697}
698
Michel Dänzer7ed220d2009-08-13 11:10:51 +0200699int r100_irq_set(struct radeon_device *rdev)
700{
701 uint32_t tmp = 0;
702
Jerome Glisse003e69f2010-01-07 15:39:14 +0100703 if (!rdev->irq.installed) {
Joe Perchesfce7d612010-10-30 21:08:30 +0000704 WARN(1, "Can't enable IRQ/MSI because no handler is installed\n");
Jerome Glisse003e69f2010-01-07 15:39:14 +0100705 WREG32(R_000040_GEN_INT_CNTL, 0);
706 return -EINVAL;
707 }
Christian Koenig736fc372012-05-17 19:52:00 +0200708 if (atomic_read(&rdev->irq.ring_int[RADEON_RING_TYPE_GFX_INDEX])) {
Michel Dänzer7ed220d2009-08-13 11:10:51 +0200709 tmp |= RADEON_SW_INT_ENABLE;
710 }
Alex Deucher6f34be52010-11-21 10:59:01 -0500711 if (rdev->irq.crtc_vblank_int[0] ||
Christian Koenig736fc372012-05-17 19:52:00 +0200712 atomic_read(&rdev->irq.pflip[0])) {
Michel Dänzer7ed220d2009-08-13 11:10:51 +0200713 tmp |= RADEON_CRTC_VBLANK_MASK;
714 }
Alex Deucher6f34be52010-11-21 10:59:01 -0500715 if (rdev->irq.crtc_vblank_int[1] ||
Christian Koenig736fc372012-05-17 19:52:00 +0200716 atomic_read(&rdev->irq.pflip[1])) {
Michel Dänzer7ed220d2009-08-13 11:10:51 +0200717 tmp |= RADEON_CRTC2_VBLANK_MASK;
718 }
Alex Deucher05a05c52009-12-04 14:53:41 -0500719 if (rdev->irq.hpd[0]) {
720 tmp |= RADEON_FP_DETECT_MASK;
721 }
722 if (rdev->irq.hpd[1]) {
723 tmp |= RADEON_FP2_DETECT_MASK;
724 }
Michel Dänzer7ed220d2009-08-13 11:10:51 +0200725 WREG32(RADEON_GEN_INT_CNTL, tmp);
726 return 0;
727}
728
Jerome Glisse9f022dd2009-09-11 15:35:22 +0200729void r100_irq_disable(struct radeon_device *rdev)
730{
731 u32 tmp;
732
733 WREG32(R_000040_GEN_INT_CNTL, 0);
734 /* Wait and acknowledge irq */
735 mdelay(1);
736 tmp = RREG32(R_000044_GEN_INT_STATUS);
737 WREG32(R_000044_GEN_INT_STATUS, tmp);
738}
739
Andi Kleencbdd4502011-10-13 16:08:46 -0700740static uint32_t r100_irq_ack(struct radeon_device *rdev)
Michel Dänzer7ed220d2009-08-13 11:10:51 +0200741{
742 uint32_t irqs = RREG32(RADEON_GEN_INT_STATUS);
Alex Deucher05a05c52009-12-04 14:53:41 -0500743 uint32_t irq_mask = RADEON_SW_INT_TEST |
744 RADEON_CRTC_VBLANK_STAT | RADEON_CRTC2_VBLANK_STAT |
745 RADEON_FP_DETECT_STAT | RADEON_FP2_DETECT_STAT;
Michel Dänzer7ed220d2009-08-13 11:10:51 +0200746
747 if (irqs) {
748 WREG32(RADEON_GEN_INT_STATUS, irqs);
749 }
750 return irqs & irq_mask;
751}
752
753int r100_irq_process(struct radeon_device *rdev)
754{
Alex Deucher3e5cb982009-10-16 12:21:24 -0400755 uint32_t status, msi_rearm;
Alex Deucherd4877cf2009-12-04 16:56:37 -0500756 bool queue_hotplug = false;
Michel Dänzer7ed220d2009-08-13 11:10:51 +0200757
758 status = r100_irq_ack(rdev);
759 if (!status) {
760 return IRQ_NONE;
761 }
Jerome Glissea513c182009-09-09 22:23:07 +0200762 if (rdev->shutdown) {
763 return IRQ_NONE;
764 }
Michel Dänzer7ed220d2009-08-13 11:10:51 +0200765 while (status) {
766 /* SW interrupt */
767 if (status & RADEON_SW_INT_TEST) {
Alex Deucher74652802011-08-25 13:39:48 -0400768 radeon_fence_process(rdev, RADEON_RING_TYPE_GFX_INDEX);
Michel Dänzer7ed220d2009-08-13 11:10:51 +0200769 }
770 /* Vertical blank interrupts */
771 if (status & RADEON_CRTC_VBLANK_STAT) {
Alex Deucher6f34be52010-11-21 10:59:01 -0500772 if (rdev->irq.crtc_vblank_int[0]) {
773 drm_handle_vblank(rdev->ddev, 0);
774 rdev->pm.vblank_sync = true;
775 wake_up(&rdev->irq.vblank_queue);
776 }
Christian Koenig736fc372012-05-17 19:52:00 +0200777 if (atomic_read(&rdev->irq.pflip[0]))
Christian König1a0e7912014-05-27 16:49:21 +0200778 radeon_crtc_handle_vblank(rdev, 0);
Michel Dänzer7ed220d2009-08-13 11:10:51 +0200779 }
780 if (status & RADEON_CRTC2_VBLANK_STAT) {
Alex Deucher6f34be52010-11-21 10:59:01 -0500781 if (rdev->irq.crtc_vblank_int[1]) {
782 drm_handle_vblank(rdev->ddev, 1);
783 rdev->pm.vblank_sync = true;
784 wake_up(&rdev->irq.vblank_queue);
785 }
Christian Koenig736fc372012-05-17 19:52:00 +0200786 if (atomic_read(&rdev->irq.pflip[1]))
Christian König1a0e7912014-05-27 16:49:21 +0200787 radeon_crtc_handle_vblank(rdev, 1);
Michel Dänzer7ed220d2009-08-13 11:10:51 +0200788 }
Alex Deucher05a05c52009-12-04 14:53:41 -0500789 if (status & RADEON_FP_DETECT_STAT) {
Alex Deucherd4877cf2009-12-04 16:56:37 -0500790 queue_hotplug = true;
791 DRM_DEBUG("HPD1\n");
Alex Deucher05a05c52009-12-04 14:53:41 -0500792 }
793 if (status & RADEON_FP2_DETECT_STAT) {
Alex Deucherd4877cf2009-12-04 16:56:37 -0500794 queue_hotplug = true;
795 DRM_DEBUG("HPD2\n");
Alex Deucher05a05c52009-12-04 14:53:41 -0500796 }
Michel Dänzer7ed220d2009-08-13 11:10:51 +0200797 status = r100_irq_ack(rdev);
798 }
Alex Deucherd4877cf2009-12-04 16:56:37 -0500799 if (queue_hotplug)
Tejun Heo32c87fc2011-01-03 14:49:32 +0100800 schedule_work(&rdev->hotplug_work);
Alex Deucher3e5cb982009-10-16 12:21:24 -0400801 if (rdev->msi_enabled) {
802 switch (rdev->family) {
803 case CHIP_RS400:
804 case CHIP_RS480:
805 msi_rearm = RREG32(RADEON_AIC_CNTL) & ~RS400_MSI_REARM;
806 WREG32(RADEON_AIC_CNTL, msi_rearm);
807 WREG32(RADEON_AIC_CNTL, msi_rearm | RS400_MSI_REARM);
808 break;
809 default:
Alex Deucherb7f5b7d2012-02-13 16:36:34 -0500810 WREG32(RADEON_MSI_REARM_EN, RV370_MSI_REARM_EN);
Alex Deucher3e5cb982009-10-16 12:21:24 -0400811 break;
812 }
813 }
Michel Dänzer7ed220d2009-08-13 11:10:51 +0200814 return IRQ_HANDLED;
815}
816
817u32 r100_get_vblank_counter(struct radeon_device *rdev, int crtc)
818{
819 if (crtc == 0)
820 return RREG32(RADEON_CRTC_CRNT_FRAME);
821 else
822 return RREG32(RADEON_CRTC2_CRNT_FRAME);
823}
824
Pauli Nieminen9e5b2af2010-02-04 19:20:53 +0200825/* Who ever call radeon_fence_emit should call ring_lock and ask
826 * for enough space (today caller are ib schedule and buffer move) */
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200827void r100_fence_ring_emit(struct radeon_device *rdev,
828 struct radeon_fence *fence)
829{
Christian Könige32eb502011-10-23 12:56:27 +0200830 struct radeon_ring *ring = &rdev->ring[fence->ring];
Christian König7b1f2482011-09-23 15:11:23 +0200831
Pauli Nieminen9e5b2af2010-02-04 19:20:53 +0200832 /* We have to make sure that caches are flushed before
833 * CPU might read something from VRAM. */
Christian Könige32eb502011-10-23 12:56:27 +0200834 radeon_ring_write(ring, PACKET0(RADEON_RB3D_DSTCACHE_CTLSTAT, 0));
835 radeon_ring_write(ring, RADEON_RB3D_DC_FLUSH_ALL);
836 radeon_ring_write(ring, PACKET0(RADEON_RB3D_ZCACHE_CTLSTAT, 0));
837 radeon_ring_write(ring, RADEON_RB3D_ZC_FLUSH_ALL);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200838 /* Wait until IDLE & CLEAN */
Christian Könige32eb502011-10-23 12:56:27 +0200839 radeon_ring_write(ring, PACKET0(RADEON_WAIT_UNTIL, 0));
840 radeon_ring_write(ring, RADEON_WAIT_2D_IDLECLEAN | RADEON_WAIT_3D_IDLECLEAN);
841 radeon_ring_write(ring, PACKET0(RADEON_HOST_PATH_CNTL, 0));
842 radeon_ring_write(ring, rdev->config.r100.hdp_cntl |
Jerome Glissecafe6602010-01-07 12:39:21 +0100843 RADEON_HDP_READ_BUFFER_INVALIDATE);
Christian Könige32eb502011-10-23 12:56:27 +0200844 radeon_ring_write(ring, PACKET0(RADEON_HOST_PATH_CNTL, 0));
845 radeon_ring_write(ring, rdev->config.r100.hdp_cntl);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200846 /* Emit fence sequence & fire IRQ */
Christian Könige32eb502011-10-23 12:56:27 +0200847 radeon_ring_write(ring, PACKET0(rdev->fence_drv[fence->ring].scratch_reg, 0));
848 radeon_ring_write(ring, fence->seq);
849 radeon_ring_write(ring, PACKET0(RADEON_GEN_INT_STATUS, 0));
850 radeon_ring_write(ring, RADEON_SW_INT_FIRE);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200851}
852
Christian König1654b812013-11-12 12:58:05 +0100853bool r100_semaphore_ring_emit(struct radeon_device *rdev,
Christian Könige32eb502011-10-23 12:56:27 +0200854 struct radeon_ring *ring,
Christian König15d33322011-09-15 19:02:22 +0200855 struct radeon_semaphore *semaphore,
Christian König7b1f2482011-09-23 15:11:23 +0200856 bool emit_wait)
Christian König15d33322011-09-15 19:02:22 +0200857{
858 /* Unused on older asics, since we don't have semaphores or multiple rings */
859 BUG();
Christian König1654b812013-11-12 12:58:05 +0100860 return false;
Christian König15d33322011-09-15 19:02:22 +0200861}
862
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200863int r100_copy_blit(struct radeon_device *rdev,
864 uint64_t src_offset,
865 uint64_t dst_offset,
Alex Deucher003cefe2011-09-16 12:04:08 -0400866 unsigned num_gpu_pages,
Christian König876dc9f2012-05-08 14:24:01 +0200867 struct radeon_fence **fence)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200868{
Christian Könige32eb502011-10-23 12:56:27 +0200869 struct radeon_ring *ring = &rdev->ring[RADEON_RING_TYPE_GFX_INDEX];
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200870 uint32_t cur_pages;
Alex Deucher003cefe2011-09-16 12:04:08 -0400871 uint32_t stride_bytes = RADEON_GPU_PAGE_SIZE;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200872 uint32_t pitch;
873 uint32_t stride_pixels;
874 unsigned ndw;
875 int num_loops;
876 int r = 0;
877
878 /* radeon limited to 16k stride */
879 stride_bytes &= 0x3fff;
880 /* radeon pitch is /64 */
881 pitch = stride_bytes / 64;
882 stride_pixels = stride_bytes / 4;
Alex Deucher003cefe2011-09-16 12:04:08 -0400883 num_loops = DIV_ROUND_UP(num_gpu_pages, 8191);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200884
885 /* Ask for enough room for blit + flush + fence */
886 ndw = 64 + (10 * num_loops);
Christian Könige32eb502011-10-23 12:56:27 +0200887 r = radeon_ring_lock(rdev, ring, ndw);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200888 if (r) {
889 DRM_ERROR("radeon: moving bo (%d) asking for %u dw.\n", r, ndw);
890 return -EINVAL;
891 }
Alex Deucher003cefe2011-09-16 12:04:08 -0400892 while (num_gpu_pages > 0) {
893 cur_pages = num_gpu_pages;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200894 if (cur_pages > 8191) {
895 cur_pages = 8191;
896 }
Alex Deucher003cefe2011-09-16 12:04:08 -0400897 num_gpu_pages -= cur_pages;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200898
899 /* pages are in Y direction - height
900 page width in X direction - width */
Christian Könige32eb502011-10-23 12:56:27 +0200901 radeon_ring_write(ring, PACKET3(PACKET3_BITBLT_MULTI, 8));
902 radeon_ring_write(ring,
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200903 RADEON_GMC_SRC_PITCH_OFFSET_CNTL |
904 RADEON_GMC_DST_PITCH_OFFSET_CNTL |
905 RADEON_GMC_SRC_CLIPPING |
906 RADEON_GMC_DST_CLIPPING |
907 RADEON_GMC_BRUSH_NONE |
908 (RADEON_COLOR_FORMAT_ARGB8888 << 8) |
909 RADEON_GMC_SRC_DATATYPE_COLOR |
910 RADEON_ROP3_S |
911 RADEON_DP_SRC_SOURCE_MEMORY |
912 RADEON_GMC_CLR_CMP_CNTL_DIS |
913 RADEON_GMC_WR_MSK_DIS);
Christian Könige32eb502011-10-23 12:56:27 +0200914 radeon_ring_write(ring, (pitch << 22) | (src_offset >> 10));
915 radeon_ring_write(ring, (pitch << 22) | (dst_offset >> 10));
916 radeon_ring_write(ring, (0x1fff) | (0x1fff << 16));
917 radeon_ring_write(ring, 0);
918 radeon_ring_write(ring, (0x1fff) | (0x1fff << 16));
919 radeon_ring_write(ring, num_gpu_pages);
920 radeon_ring_write(ring, num_gpu_pages);
921 radeon_ring_write(ring, cur_pages | (stride_pixels << 16));
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200922 }
Christian Könige32eb502011-10-23 12:56:27 +0200923 radeon_ring_write(ring, PACKET0(RADEON_DSTCACHE_CTLSTAT, 0));
924 radeon_ring_write(ring, RADEON_RB2D_DC_FLUSH_ALL);
925 radeon_ring_write(ring, PACKET0(RADEON_WAIT_UNTIL, 0));
926 radeon_ring_write(ring,
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200927 RADEON_WAIT_2D_IDLECLEAN |
928 RADEON_WAIT_HOST_IDLECLEAN |
929 RADEON_WAIT_DMA_GUI_IDLE);
930 if (fence) {
Christian König876dc9f2012-05-08 14:24:01 +0200931 r = radeon_fence_emit(rdev, fence, RADEON_RING_TYPE_GFX_INDEX);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200932 }
Christian Könige32eb502011-10-23 12:56:27 +0200933 radeon_ring_unlock_commit(rdev, ring);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200934 return r;
935}
936
Jerome Glisse45600232009-09-09 22:23:45 +0200937static int r100_cp_wait_for_idle(struct radeon_device *rdev)
938{
939 unsigned i;
940 u32 tmp;
941
942 for (i = 0; i < rdev->usec_timeout; i++) {
943 tmp = RREG32(R_000E40_RBBM_STATUS);
944 if (!G_000E40_CP_CMDSTRM_BUSY(tmp)) {
945 return 0;
946 }
947 udelay(1);
948 }
949 return -1;
950}
951
Alex Deucherf7128122012-02-23 17:53:45 -0500952void r100_ring_start(struct radeon_device *rdev, struct radeon_ring *ring)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200953{
954 int r;
955
Christian Könige32eb502011-10-23 12:56:27 +0200956 r = radeon_ring_lock(rdev, ring, 2);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200957 if (r) {
958 return;
959 }
Christian Könige32eb502011-10-23 12:56:27 +0200960 radeon_ring_write(ring, PACKET0(RADEON_ISYNC_CNTL, 0));
961 radeon_ring_write(ring,
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200962 RADEON_ISYNC_ANY2D_IDLE3D |
963 RADEON_ISYNC_ANY3D_IDLE2D |
964 RADEON_ISYNC_WAIT_IDLEGUI |
965 RADEON_ISYNC_CPSCRATCH_IDLEGUI);
Christian Könige32eb502011-10-23 12:56:27 +0200966 radeon_ring_unlock_commit(rdev, ring);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200967}
968
Ben Hutchings70967ab2009-08-29 14:53:51 +0100969
970/* Load the microcode for the CP */
971static int r100_cp_init_microcode(struct radeon_device *rdev)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200972{
Ben Hutchings70967ab2009-08-29 14:53:51 +0100973 const char *fw_name = NULL;
974 int err;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200975
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000976 DRM_DEBUG_KMS("\n");
Ben Hutchings70967ab2009-08-29 14:53:51 +0100977
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200978 if ((rdev->family == CHIP_R100) || (rdev->family == CHIP_RV100) ||
979 (rdev->family == CHIP_RV200) || (rdev->family == CHIP_RS100) ||
980 (rdev->family == CHIP_RS200)) {
981 DRM_INFO("Loading R100 Microcode\n");
Ben Hutchings70967ab2009-08-29 14:53:51 +0100982 fw_name = FIRMWARE_R100;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200983 } else if ((rdev->family == CHIP_R200) ||
984 (rdev->family == CHIP_RV250) ||
985 (rdev->family == CHIP_RV280) ||
986 (rdev->family == CHIP_RS300)) {
987 DRM_INFO("Loading R200 Microcode\n");
Ben Hutchings70967ab2009-08-29 14:53:51 +0100988 fw_name = FIRMWARE_R200;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200989 } else if ((rdev->family == CHIP_R300) ||
990 (rdev->family == CHIP_R350) ||
991 (rdev->family == CHIP_RV350) ||
992 (rdev->family == CHIP_RV380) ||
993 (rdev->family == CHIP_RS400) ||
994 (rdev->family == CHIP_RS480)) {
995 DRM_INFO("Loading R300 Microcode\n");
Ben Hutchings70967ab2009-08-29 14:53:51 +0100996 fw_name = FIRMWARE_R300;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200997 } else if ((rdev->family == CHIP_R420) ||
998 (rdev->family == CHIP_R423) ||
999 (rdev->family == CHIP_RV410)) {
1000 DRM_INFO("Loading R400 Microcode\n");
Ben Hutchings70967ab2009-08-29 14:53:51 +01001001 fw_name = FIRMWARE_R420;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001002 } else if ((rdev->family == CHIP_RS690) ||
1003 (rdev->family == CHIP_RS740)) {
1004 DRM_INFO("Loading RS690/RS740 Microcode\n");
Ben Hutchings70967ab2009-08-29 14:53:51 +01001005 fw_name = FIRMWARE_RS690;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001006 } else if (rdev->family == CHIP_RS600) {
1007 DRM_INFO("Loading RS600 Microcode\n");
Ben Hutchings70967ab2009-08-29 14:53:51 +01001008 fw_name = FIRMWARE_RS600;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001009 } else if ((rdev->family == CHIP_RV515) ||
1010 (rdev->family == CHIP_R520) ||
1011 (rdev->family == CHIP_RV530) ||
1012 (rdev->family == CHIP_R580) ||
1013 (rdev->family == CHIP_RV560) ||
1014 (rdev->family == CHIP_RV570)) {
1015 DRM_INFO("Loading R500 Microcode\n");
Ben Hutchings70967ab2009-08-29 14:53:51 +01001016 fw_name = FIRMWARE_R520;
1017 }
1018
Jerome Glisse0a168932013-07-11 15:53:01 -04001019 err = request_firmware(&rdev->me_fw, fw_name, rdev->dev);
Ben Hutchings70967ab2009-08-29 14:53:51 +01001020 if (err) {
1021 printk(KERN_ERR "radeon_cp: Failed to load firmware \"%s\"\n",
1022 fw_name);
Jerome Glisse3ce0a232009-09-08 10:10:24 +10001023 } else if (rdev->me_fw->size % 8) {
Ben Hutchings70967ab2009-08-29 14:53:51 +01001024 printk(KERN_ERR
1025 "radeon_cp: Bogus length %zu in firmware \"%s\"\n",
Jerome Glisse3ce0a232009-09-08 10:10:24 +10001026 rdev->me_fw->size, fw_name);
Ben Hutchings70967ab2009-08-29 14:53:51 +01001027 err = -EINVAL;
Jerome Glisse3ce0a232009-09-08 10:10:24 +10001028 release_firmware(rdev->me_fw);
1029 rdev->me_fw = NULL;
Ben Hutchings70967ab2009-08-29 14:53:51 +01001030 }
1031 return err;
1032}
Jerome Glissed4550902009-10-01 10:12:06 +02001033
Alex Deucherea31bf62013-12-09 19:44:30 -05001034u32 r100_gfx_get_rptr(struct radeon_device *rdev,
1035 struct radeon_ring *ring)
1036{
1037 u32 rptr;
1038
1039 if (rdev->wb.enabled)
1040 rptr = le32_to_cpu(rdev->wb.wb[ring->rptr_offs/4]);
1041 else
1042 rptr = RREG32(RADEON_CP_RB_RPTR);
1043
1044 return rptr;
1045}
1046
1047u32 r100_gfx_get_wptr(struct radeon_device *rdev,
1048 struct radeon_ring *ring)
1049{
1050 u32 wptr;
1051
1052 wptr = RREG32(RADEON_CP_RB_WPTR);
1053
1054 return wptr;
1055}
1056
1057void r100_gfx_set_wptr(struct radeon_device *rdev,
1058 struct radeon_ring *ring)
1059{
1060 WREG32(RADEON_CP_RB_WPTR, ring->wptr);
1061 (void)RREG32(RADEON_CP_RB_WPTR);
1062}
1063
Ben Hutchings70967ab2009-08-29 14:53:51 +01001064static void r100_cp_load_microcode(struct radeon_device *rdev)
1065{
1066 const __be32 *fw_data;
1067 int i, size;
1068
1069 if (r100_gui_wait_for_idle(rdev)) {
1070 printk(KERN_WARNING "Failed to wait GUI idle while "
1071 "programming pipes. Bad things might happen.\n");
1072 }
1073
Jerome Glisse3ce0a232009-09-08 10:10:24 +10001074 if (rdev->me_fw) {
1075 size = rdev->me_fw->size / 4;
1076 fw_data = (const __be32 *)&rdev->me_fw->data[0];
Ben Hutchings70967ab2009-08-29 14:53:51 +01001077 WREG32(RADEON_CP_ME_RAM_ADDR, 0);
1078 for (i = 0; i < size; i += 2) {
1079 WREG32(RADEON_CP_ME_RAM_DATAH,
1080 be32_to_cpup(&fw_data[i]));
1081 WREG32(RADEON_CP_ME_RAM_DATAL,
1082 be32_to_cpup(&fw_data[i + 1]));
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001083 }
1084 }
1085}
1086
1087int r100_cp_init(struct radeon_device *rdev, unsigned ring_size)
1088{
Christian Könige32eb502011-10-23 12:56:27 +02001089 struct radeon_ring *ring = &rdev->ring[RADEON_RING_TYPE_GFX_INDEX];
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001090 unsigned rb_bufsz;
1091 unsigned rb_blksz;
1092 unsigned max_fetch;
1093 unsigned pre_write_timer;
1094 unsigned pre_write_limit;
1095 unsigned indirect2_start;
1096 unsigned indirect1_start;
1097 uint32_t tmp;
1098 int r;
1099
1100 if (r100_debugfs_cp_init(rdev)) {
1101 DRM_ERROR("Failed to register debugfs file for CP !\n");
1102 }
Jerome Glisse3ce0a232009-09-08 10:10:24 +10001103 if (!rdev->me_fw) {
Ben Hutchings70967ab2009-08-29 14:53:51 +01001104 r = r100_cp_init_microcode(rdev);
1105 if (r) {
1106 DRM_ERROR("Failed to load firmware!\n");
1107 return r;
1108 }
1109 }
1110
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001111 /* Align ring size */
Daniel Vetterb72a8922013-07-10 14:11:59 +02001112 rb_bufsz = order_base_2(ring_size / 8);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001113 ring_size = (1 << (rb_bufsz + 1)) * 4;
1114 r100_cp_load_microcode(rdev);
Christian Könige32eb502011-10-23 12:56:27 +02001115 r = radeon_ring_init(rdev, ring, ring_size, RADEON_WB_CP_RPTR_OFFSET,
Christian König2e1e6da2013-08-13 11:56:52 +02001116 RADEON_CP_PACKET2);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001117 if (r) {
1118 return r;
1119 }
1120 /* Each time the cp read 1024 bytes (16 dword/quadword) update
1121 * the rptr copy in system ram */
1122 rb_blksz = 9;
1123 /* cp will read 128bytes at a time (4 dwords) */
1124 max_fetch = 1;
Christian Könige32eb502011-10-23 12:56:27 +02001125 ring->align_mask = 16 - 1;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001126 /* Write to CP_RB_WPTR will be delayed for pre_write_timer clocks */
1127 pre_write_timer = 64;
1128 /* Force CP_RB_WPTR write if written more than one time before the
1129 * delay expire
1130 */
1131 pre_write_limit = 0;
1132 /* Setup the cp cache like this (cache size is 96 dwords) :
1133 * RING 0 to 15
1134 * INDIRECT1 16 to 79
1135 * INDIRECT2 80 to 95
1136 * So ring cache size is 16dwords (> (2 * max_fetch = 2 * 4dwords))
1137 * indirect1 cache size is 64dwords (> (2 * max_fetch = 2 * 4dwords))
1138 * indirect2 cache size is 16dwords (> (2 * max_fetch = 2 * 4dwords))
1139 * Idea being that most of the gpu cmd will be through indirect1 buffer
1140 * so it gets the bigger cache.
1141 */
1142 indirect2_start = 80;
1143 indirect1_start = 16;
1144 /* cp setup */
1145 WREG32(0x718, pre_write_timer | (pre_write_limit << 28));
Alex Deucherd6f28932009-11-02 16:01:27 -05001146 tmp = (REG_SET(RADEON_RB_BUFSZ, rb_bufsz) |
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001147 REG_SET(RADEON_RB_BLKSZ, rb_blksz) |
Alex Deucher724c80e2010-08-27 18:25:25 -04001148 REG_SET(RADEON_MAX_FETCH, max_fetch));
Alex Deucherd6f28932009-11-02 16:01:27 -05001149#ifdef __BIG_ENDIAN
1150 tmp |= RADEON_BUF_SWAP_32BIT;
1151#endif
Alex Deucher724c80e2010-08-27 18:25:25 -04001152 WREG32(RADEON_CP_RB_CNTL, tmp | RADEON_RB_NO_UPDATE);
Alex Deucherd6f28932009-11-02 16:01:27 -05001153
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001154 /* Set ring address */
Christian Könige32eb502011-10-23 12:56:27 +02001155 DRM_INFO("radeon: ring at 0x%016lX\n", (unsigned long)ring->gpu_addr);
1156 WREG32(RADEON_CP_RB_BASE, ring->gpu_addr);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001157 /* Force read & write ptr to 0 */
Alex Deucher724c80e2010-08-27 18:25:25 -04001158 WREG32(RADEON_CP_RB_CNTL, tmp | RADEON_RB_RPTR_WR_ENA | RADEON_RB_NO_UPDATE);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001159 WREG32(RADEON_CP_RB_RPTR_WR, 0);
Christian Könige32eb502011-10-23 12:56:27 +02001160 ring->wptr = 0;
1161 WREG32(RADEON_CP_RB_WPTR, ring->wptr);
Alex Deucher724c80e2010-08-27 18:25:25 -04001162
1163 /* set the wb address whether it's enabled or not */
1164 WREG32(R_00070C_CP_RB_RPTR_ADDR,
1165 S_00070C_RB_RPTR_ADDR((rdev->wb.gpu_addr + RADEON_WB_CP_RPTR_OFFSET) >> 2));
1166 WREG32(R_000774_SCRATCH_ADDR, rdev->wb.gpu_addr + RADEON_WB_SCRATCH_OFFSET);
1167
1168 if (rdev->wb.enabled)
1169 WREG32(R_000770_SCRATCH_UMSK, 0xff);
1170 else {
1171 tmp |= RADEON_RB_NO_UPDATE;
1172 WREG32(R_000770_SCRATCH_UMSK, 0);
1173 }
1174
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001175 WREG32(RADEON_CP_RB_CNTL, tmp);
1176 udelay(10);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001177 /* Set cp mode to bus mastering & enable cp*/
1178 WREG32(RADEON_CP_CSQ_MODE,
1179 REG_SET(RADEON_INDIRECT2_START, indirect2_start) |
1180 REG_SET(RADEON_INDIRECT1_START, indirect1_start));
Alex Deucherd75ee3b2011-01-24 23:24:59 -05001181 WREG32(RADEON_CP_RB_WPTR_DELAY, 0);
1182 WREG32(RADEON_CP_CSQ_MODE, 0x00004D4D);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001183 WREG32(RADEON_CP_CSQ_CNTL, RADEON_CSQ_PRIBM_INDBM);
Dave Airlie20998102012-04-03 11:53:05 +01001184
1185 /* at this point everything should be setup correctly to enable master */
1186 pci_set_master(rdev->pdev);
1187
Alex Deucherf7128122012-02-23 17:53:45 -05001188 radeon_ring_start(rdev, RADEON_RING_TYPE_GFX_INDEX, &rdev->ring[RADEON_RING_TYPE_GFX_INDEX]);
1189 r = radeon_ring_test(rdev, RADEON_RING_TYPE_GFX_INDEX, ring);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001190 if (r) {
1191 DRM_ERROR("radeon: cp isn't working (%d).\n", r);
1192 return r;
1193 }
Christian Könige32eb502011-10-23 12:56:27 +02001194 ring->ready = true;
Dave Airlie53595332011-03-14 09:47:24 +10001195 radeon_ttm_set_active_vram_size(rdev, rdev->mc.real_vram_size);
Alex Deucherc7eff972012-07-17 14:02:32 -04001196
Simon Kitching16c58082012-09-20 12:59:16 -04001197 if (!ring->rptr_save_reg /* not resuming from suspend */
1198 && radeon_ring_supports_scratch_reg(rdev, ring)) {
Alex Deucherc7eff972012-07-17 14:02:32 -04001199 r = radeon_scratch_get(rdev, &ring->rptr_save_reg);
1200 if (r) {
1201 DRM_ERROR("failed to get scratch reg for rptr save (%d).\n", r);
1202 ring->rptr_save_reg = 0;
1203 }
1204 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001205 return 0;
1206}
1207
1208void r100_cp_fini(struct radeon_device *rdev)
1209{
Jerome Glisse45600232009-09-09 22:23:45 +02001210 if (r100_cp_wait_for_idle(rdev)) {
1211 DRM_ERROR("Wait for CP idle timeout, shutting down CP.\n");
1212 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001213 /* Disable ring */
Jerome Glissea18d7ea2009-09-09 22:23:27 +02001214 r100_cp_disable(rdev);
Alex Deucherc7eff972012-07-17 14:02:32 -04001215 radeon_scratch_free(rdev, rdev->ring[RADEON_RING_TYPE_GFX_INDEX].rptr_save_reg);
Christian Könige32eb502011-10-23 12:56:27 +02001216 radeon_ring_fini(rdev, &rdev->ring[RADEON_RING_TYPE_GFX_INDEX]);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001217 DRM_INFO("radeon: cp finalized\n");
1218}
1219
1220void r100_cp_disable(struct radeon_device *rdev)
1221{
1222 /* Disable ring */
Dave Airlie53595332011-03-14 09:47:24 +10001223 radeon_ttm_set_active_vram_size(rdev, rdev->mc.visible_vram_size);
Christian Könige32eb502011-10-23 12:56:27 +02001224 rdev->ring[RADEON_RING_TYPE_GFX_INDEX].ready = false;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001225 WREG32(RADEON_CP_CSQ_MODE, 0);
1226 WREG32(RADEON_CP_CSQ_CNTL, 0);
Alex Deucher724c80e2010-08-27 18:25:25 -04001227 WREG32(R_000770_SCRATCH_UMSK, 0);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001228 if (r100_gui_wait_for_idle(rdev)) {
1229 printk(KERN_WARNING "Failed to wait GUI idle while "
1230 "programming pipes. Bad things might happen.\n");
1231 }
1232}
1233
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001234/*
1235 * CS functions
1236 */
Alex Deucher0242f742012-06-28 17:50:34 -04001237int r100_reloc_pitch_offset(struct radeon_cs_parser *p,
1238 struct radeon_cs_packet *pkt,
1239 unsigned idx,
1240 unsigned reg)
1241{
1242 int r;
1243 u32 tile_flags = 0;
1244 u32 tmp;
1245 struct radeon_cs_reloc *reloc;
1246 u32 value;
1247
Ilija Hadzic012e9762013-01-02 18:27:47 -05001248 r = radeon_cs_packet_next_reloc(p, &reloc, 0);
Alex Deucher0242f742012-06-28 17:50:34 -04001249 if (r) {
1250 DRM_ERROR("No reloc for ib[%d]=0x%04X\n",
1251 idx, reg);
Ilija Hadzicc3ad63a2013-01-02 18:27:45 -05001252 radeon_cs_dump_packet(p, pkt);
Alex Deucher0242f742012-06-28 17:50:34 -04001253 return r;
1254 }
1255
1256 value = radeon_get_ib_value(p, idx);
1257 tmp = value & 0x003fffff;
Christian Königdf0af442014-03-03 12:38:08 +01001258 tmp += (((u32)reloc->gpu_offset) >> 10);
Alex Deucher0242f742012-06-28 17:50:34 -04001259
1260 if (!(p->cs_flags & RADEON_CS_KEEP_TILING_FLAGS)) {
Christian Königdf0af442014-03-03 12:38:08 +01001261 if (reloc->tiling_flags & RADEON_TILING_MACRO)
Alex Deucher0242f742012-06-28 17:50:34 -04001262 tile_flags |= RADEON_DST_TILE_MACRO;
Christian Königdf0af442014-03-03 12:38:08 +01001263 if (reloc->tiling_flags & RADEON_TILING_MICRO) {
Alex Deucher0242f742012-06-28 17:50:34 -04001264 if (reg == RADEON_SRC_PITCH_OFFSET) {
1265 DRM_ERROR("Cannot src blit from microtiled surface\n");
Ilija Hadzicc3ad63a2013-01-02 18:27:45 -05001266 radeon_cs_dump_packet(p, pkt);
Alex Deucher0242f742012-06-28 17:50:34 -04001267 return -EINVAL;
1268 }
1269 tile_flags |= RADEON_DST_TILE_MICRO;
1270 }
1271
1272 tmp |= tile_flags;
1273 p->ib.ptr[idx] = (value & 0x3fc00000) | tmp;
1274 } else
1275 p->ib.ptr[idx] = (value & 0xffc00000) | tmp;
1276 return 0;
1277}
1278
1279int r100_packet3_load_vbpntr(struct radeon_cs_parser *p,
1280 struct radeon_cs_packet *pkt,
1281 int idx)
1282{
1283 unsigned c, i;
1284 struct radeon_cs_reloc *reloc;
1285 struct r100_cs_track *track;
1286 int r = 0;
1287 volatile uint32_t *ib;
1288 u32 idx_value;
1289
1290 ib = p->ib.ptr;
1291 track = (struct r100_cs_track *)p->track;
1292 c = radeon_get_ib_value(p, idx++) & 0x1F;
1293 if (c > 16) {
1294 DRM_ERROR("Only 16 vertex buffers are allowed %d\n",
1295 pkt->opcode);
Ilija Hadzicc3ad63a2013-01-02 18:27:45 -05001296 radeon_cs_dump_packet(p, pkt);
Alex Deucher0242f742012-06-28 17:50:34 -04001297 return -EINVAL;
1298 }
1299 track->num_arrays = c;
1300 for (i = 0; i < (c - 1); i+=2, idx+=3) {
Ilija Hadzic012e9762013-01-02 18:27:47 -05001301 r = radeon_cs_packet_next_reloc(p, &reloc, 0);
Alex Deucher0242f742012-06-28 17:50:34 -04001302 if (r) {
1303 DRM_ERROR("No reloc for packet3 %d\n",
1304 pkt->opcode);
Ilija Hadzicc3ad63a2013-01-02 18:27:45 -05001305 radeon_cs_dump_packet(p, pkt);
Alex Deucher0242f742012-06-28 17:50:34 -04001306 return r;
1307 }
1308 idx_value = radeon_get_ib_value(p, idx);
Christian Königdf0af442014-03-03 12:38:08 +01001309 ib[idx+1] = radeon_get_ib_value(p, idx + 1) + ((u32)reloc->gpu_offset);
Alex Deucher0242f742012-06-28 17:50:34 -04001310
1311 track->arrays[i + 0].esize = idx_value >> 8;
1312 track->arrays[i + 0].robj = reloc->robj;
1313 track->arrays[i + 0].esize &= 0x7F;
Ilija Hadzic012e9762013-01-02 18:27:47 -05001314 r = radeon_cs_packet_next_reloc(p, &reloc, 0);
Alex Deucher0242f742012-06-28 17:50:34 -04001315 if (r) {
1316 DRM_ERROR("No reloc for packet3 %d\n",
1317 pkt->opcode);
Ilija Hadzicc3ad63a2013-01-02 18:27:45 -05001318 radeon_cs_dump_packet(p, pkt);
Alex Deucher0242f742012-06-28 17:50:34 -04001319 return r;
1320 }
Christian Königdf0af442014-03-03 12:38:08 +01001321 ib[idx+2] = radeon_get_ib_value(p, idx + 2) + ((u32)reloc->gpu_offset);
Alex Deucher0242f742012-06-28 17:50:34 -04001322 track->arrays[i + 1].robj = reloc->robj;
1323 track->arrays[i + 1].esize = idx_value >> 24;
1324 track->arrays[i + 1].esize &= 0x7F;
1325 }
1326 if (c & 1) {
Ilija Hadzic012e9762013-01-02 18:27:47 -05001327 r = radeon_cs_packet_next_reloc(p, &reloc, 0);
Alex Deucher0242f742012-06-28 17:50:34 -04001328 if (r) {
1329 DRM_ERROR("No reloc for packet3 %d\n",
1330 pkt->opcode);
Ilija Hadzicc3ad63a2013-01-02 18:27:45 -05001331 radeon_cs_dump_packet(p, pkt);
Alex Deucher0242f742012-06-28 17:50:34 -04001332 return r;
1333 }
1334 idx_value = radeon_get_ib_value(p, idx);
Christian Königdf0af442014-03-03 12:38:08 +01001335 ib[idx+1] = radeon_get_ib_value(p, idx + 1) + ((u32)reloc->gpu_offset);
Alex Deucher0242f742012-06-28 17:50:34 -04001336 track->arrays[i + 0].robj = reloc->robj;
1337 track->arrays[i + 0].esize = idx_value >> 8;
1338 track->arrays[i + 0].esize &= 0x7F;
1339 }
1340 return r;
1341}
1342
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001343int r100_cs_parse_packet0(struct radeon_cs_parser *p,
1344 struct radeon_cs_packet *pkt,
Jerome Glisse068a1172009-06-17 13:28:30 +02001345 const unsigned *auth, unsigned n,
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001346 radeon_packet0_check_t check)
1347{
1348 unsigned reg;
1349 unsigned i, j, m;
1350 unsigned idx;
1351 int r;
1352
1353 idx = pkt->idx + 1;
1354 reg = pkt->reg;
Jerome Glisse068a1172009-06-17 13:28:30 +02001355 /* Check that register fall into register range
1356 * determined by the number of entry (n) in the
1357 * safe register bitmap.
1358 */
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001359 if (pkt->one_reg_wr) {
1360 if ((reg >> 7) > n) {
1361 return -EINVAL;
1362 }
1363 } else {
1364 if (((reg + (pkt->count << 2)) >> 7) > n) {
1365 return -EINVAL;
1366 }
1367 }
1368 for (i = 0; i <= pkt->count; i++, idx++) {
1369 j = (reg >> 7);
1370 m = 1 << ((reg >> 2) & 31);
1371 if (auth[j] & m) {
1372 r = check(p, pkt, idx, reg);
1373 if (r) {
1374 return r;
1375 }
1376 }
1377 if (pkt->one_reg_wr) {
1378 if (!(auth[j] & m)) {
1379 break;
1380 }
1381 } else {
1382 reg += 4;
1383 }
1384 }
1385 return 0;
1386}
1387
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001388/**
Dave Airlie531369e2009-06-29 11:21:25 +10001389 * r100_cs_packet_next_vline() - parse userspace VLINE packet
1390 * @parser: parser structure holding parsing context.
1391 *
1392 * Userspace sends a special sequence for VLINE waits.
1393 * PACKET0 - VLINE_START_END + value
1394 * PACKET0 - WAIT_UNTIL +_value
1395 * RELOC (P3) - crtc_id in reloc.
1396 *
1397 * This function parses this and relocates the VLINE START END
1398 * and WAIT UNTIL packets to the correct crtc.
1399 * It also detects a switched off crtc and nulls out the
1400 * wait in that case.
1401 */
1402int r100_cs_packet_parse_vline(struct radeon_cs_parser *p)
1403{
Dave Airlie531369e2009-06-29 11:21:25 +10001404 struct drm_crtc *crtc;
1405 struct radeon_crtc *radeon_crtc;
1406 struct radeon_cs_packet p3reloc, waitreloc;
1407 int crtc_id;
1408 int r;
1409 uint32_t header, h_idx, reg;
Dave Airlie513bcb42009-09-23 16:56:27 +10001410 volatile uint32_t *ib;
Dave Airlie531369e2009-06-29 11:21:25 +10001411
Jerome Glissef2e39222012-05-09 15:35:02 +02001412 ib = p->ib.ptr;
Dave Airlie531369e2009-06-29 11:21:25 +10001413
1414 /* parse the wait until */
Ilija Hadzicc38f34b2013-01-02 18:27:41 -05001415 r = radeon_cs_packet_parse(p, &waitreloc, p->idx);
Dave Airlie531369e2009-06-29 11:21:25 +10001416 if (r)
1417 return r;
1418
1419 /* check its a wait until and only 1 count */
1420 if (waitreloc.reg != RADEON_WAIT_UNTIL ||
1421 waitreloc.count != 0) {
1422 DRM_ERROR("vline wait had illegal wait until segment\n");
Paul Bollea3a88a62011-03-16 22:10:06 +01001423 return -EINVAL;
Dave Airlie531369e2009-06-29 11:21:25 +10001424 }
1425
Dave Airlie513bcb42009-09-23 16:56:27 +10001426 if (radeon_get_ib_value(p, waitreloc.idx + 1) != RADEON_WAIT_CRTC_VLINE) {
Dave Airlie531369e2009-06-29 11:21:25 +10001427 DRM_ERROR("vline wait had illegal wait until\n");
Paul Bollea3a88a62011-03-16 22:10:06 +01001428 return -EINVAL;
Dave Airlie531369e2009-06-29 11:21:25 +10001429 }
1430
1431 /* jump over the NOP */
Ilija Hadzicc38f34b2013-01-02 18:27:41 -05001432 r = radeon_cs_packet_parse(p, &p3reloc, p->idx + waitreloc.count + 2);
Dave Airlie531369e2009-06-29 11:21:25 +10001433 if (r)
1434 return r;
1435
1436 h_idx = p->idx - 2;
Alex Deucher90ebd062009-09-25 16:39:24 -04001437 p->idx += waitreloc.count + 2;
1438 p->idx += p3reloc.count + 2;
Dave Airlie531369e2009-06-29 11:21:25 +10001439
Dave Airlie513bcb42009-09-23 16:56:27 +10001440 header = radeon_get_ib_value(p, h_idx);
1441 crtc_id = radeon_get_ib_value(p, h_idx + 5);
Ilija Hadzic4e872ae2013-01-02 18:27:48 -05001442 reg = R100_CP_PACKET0_GET_REG(header);
Rob Clarkb957f452014-07-17 23:30:05 -04001443 crtc = drm_crtc_find(p->rdev->ddev, crtc_id);
1444 if (!crtc) {
Dave Airlie531369e2009-06-29 11:21:25 +10001445 DRM_ERROR("cannot find crtc %d\n", crtc_id);
Ville Syrjälä10e10d32013-10-17 13:35:04 +03001446 return -ENOENT;
Dave Airlie531369e2009-06-29 11:21:25 +10001447 }
Dave Airlie531369e2009-06-29 11:21:25 +10001448 radeon_crtc = to_radeon_crtc(crtc);
1449 crtc_id = radeon_crtc->crtc_id;
1450
1451 if (!crtc->enabled) {
1452 /* if the CRTC isn't enabled - we need to nop out the wait until */
Dave Airlie513bcb42009-09-23 16:56:27 +10001453 ib[h_idx + 2] = PACKET2(0);
1454 ib[h_idx + 3] = PACKET2(0);
Dave Airlie531369e2009-06-29 11:21:25 +10001455 } else if (crtc_id == 1) {
1456 switch (reg) {
1457 case AVIVO_D1MODE_VLINE_START_END:
Alex Deucher90ebd062009-09-25 16:39:24 -04001458 header &= ~R300_CP_PACKET0_REG_MASK;
Dave Airlie531369e2009-06-29 11:21:25 +10001459 header |= AVIVO_D2MODE_VLINE_START_END >> 2;
1460 break;
1461 case RADEON_CRTC_GUI_TRIG_VLINE:
Alex Deucher90ebd062009-09-25 16:39:24 -04001462 header &= ~R300_CP_PACKET0_REG_MASK;
Dave Airlie531369e2009-06-29 11:21:25 +10001463 header |= RADEON_CRTC2_GUI_TRIG_VLINE >> 2;
1464 break;
1465 default:
1466 DRM_ERROR("unknown crtc reloc\n");
Paul Bollea3a88a62011-03-16 22:10:06 +01001467 return -EINVAL;
Dave Airlie531369e2009-06-29 11:21:25 +10001468 }
Dave Airlie513bcb42009-09-23 16:56:27 +10001469 ib[h_idx] = header;
1470 ib[h_idx + 3] |= RADEON_ENG_DISPLAY_SELECT_CRTC1;
Dave Airlie531369e2009-06-29 11:21:25 +10001471 }
Paul Bollea3a88a62011-03-16 22:10:06 +01001472
1473 return 0;
Dave Airlie531369e2009-06-29 11:21:25 +10001474}
1475
Dave Airlie551ebd82009-09-01 15:25:57 +10001476static int r100_get_vtx_size(uint32_t vtx_fmt)
1477{
1478 int vtx_size;
1479 vtx_size = 2;
1480 /* ordered according to bits in spec */
1481 if (vtx_fmt & RADEON_SE_VTX_FMT_W0)
1482 vtx_size++;
1483 if (vtx_fmt & RADEON_SE_VTX_FMT_FPCOLOR)
1484 vtx_size += 3;
1485 if (vtx_fmt & RADEON_SE_VTX_FMT_FPALPHA)
1486 vtx_size++;
1487 if (vtx_fmt & RADEON_SE_VTX_FMT_PKCOLOR)
1488 vtx_size++;
1489 if (vtx_fmt & RADEON_SE_VTX_FMT_FPSPEC)
1490 vtx_size += 3;
1491 if (vtx_fmt & RADEON_SE_VTX_FMT_FPFOG)
1492 vtx_size++;
1493 if (vtx_fmt & RADEON_SE_VTX_FMT_PKSPEC)
1494 vtx_size++;
1495 if (vtx_fmt & RADEON_SE_VTX_FMT_ST0)
1496 vtx_size += 2;
1497 if (vtx_fmt & RADEON_SE_VTX_FMT_ST1)
1498 vtx_size += 2;
1499 if (vtx_fmt & RADEON_SE_VTX_FMT_Q1)
1500 vtx_size++;
1501 if (vtx_fmt & RADEON_SE_VTX_FMT_ST2)
1502 vtx_size += 2;
1503 if (vtx_fmt & RADEON_SE_VTX_FMT_Q2)
1504 vtx_size++;
1505 if (vtx_fmt & RADEON_SE_VTX_FMT_ST3)
1506 vtx_size += 2;
1507 if (vtx_fmt & RADEON_SE_VTX_FMT_Q3)
1508 vtx_size++;
1509 if (vtx_fmt & RADEON_SE_VTX_FMT_Q0)
1510 vtx_size++;
1511 /* blend weight */
1512 if (vtx_fmt & (0x7 << 15))
1513 vtx_size += (vtx_fmt >> 15) & 0x7;
1514 if (vtx_fmt & RADEON_SE_VTX_FMT_N0)
1515 vtx_size += 3;
1516 if (vtx_fmt & RADEON_SE_VTX_FMT_XY1)
1517 vtx_size += 2;
1518 if (vtx_fmt & RADEON_SE_VTX_FMT_Z1)
1519 vtx_size++;
1520 if (vtx_fmt & RADEON_SE_VTX_FMT_W1)
1521 vtx_size++;
1522 if (vtx_fmt & RADEON_SE_VTX_FMT_N1)
1523 vtx_size++;
1524 if (vtx_fmt & RADEON_SE_VTX_FMT_Z)
1525 vtx_size++;
1526 return vtx_size;
1527}
1528
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001529static int r100_packet0_check(struct radeon_cs_parser *p,
Dave Airlie551ebd82009-09-01 15:25:57 +10001530 struct radeon_cs_packet *pkt,
1531 unsigned idx, unsigned reg)
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001532{
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001533 struct radeon_cs_reloc *reloc;
Dave Airlie551ebd82009-09-01 15:25:57 +10001534 struct r100_cs_track *track;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001535 volatile uint32_t *ib;
1536 uint32_t tmp;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001537 int r;
Dave Airlie551ebd82009-09-01 15:25:57 +10001538 int i, face;
Dave Airliee024e112009-06-24 09:48:08 +10001539 u32 tile_flags = 0;
Dave Airlie513bcb42009-09-23 16:56:27 +10001540 u32 idx_value;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001541
Jerome Glissef2e39222012-05-09 15:35:02 +02001542 ib = p->ib.ptr;
Dave Airlie551ebd82009-09-01 15:25:57 +10001543 track = (struct r100_cs_track *)p->track;
1544
Dave Airlie513bcb42009-09-23 16:56:27 +10001545 idx_value = radeon_get_ib_value(p, idx);
1546
Dave Airlie551ebd82009-09-01 15:25:57 +10001547 switch (reg) {
1548 case RADEON_CRTC_GUI_TRIG_VLINE:
1549 r = r100_cs_packet_parse_vline(p);
1550 if (r) {
1551 DRM_ERROR("No reloc for ib[%d]=0x%04X\n",
1552 idx, reg);
Ilija Hadzicc3ad63a2013-01-02 18:27:45 -05001553 radeon_cs_dump_packet(p, pkt);
Dave Airlie551ebd82009-09-01 15:25:57 +10001554 return r;
1555 }
1556 break;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001557 /* FIXME: only allow PACKET3 blit? easier to check for out of
1558 * range access */
Dave Airlie551ebd82009-09-01 15:25:57 +10001559 case RADEON_DST_PITCH_OFFSET:
1560 case RADEON_SRC_PITCH_OFFSET:
1561 r = r100_reloc_pitch_offset(p, pkt, idx, reg);
1562 if (r)
1563 return r;
1564 break;
1565 case RADEON_RB3D_DEPTHOFFSET:
Ilija Hadzic012e9762013-01-02 18:27:47 -05001566 r = radeon_cs_packet_next_reloc(p, &reloc, 0);
Dave Airlie551ebd82009-09-01 15:25:57 +10001567 if (r) {
1568 DRM_ERROR("No reloc for ib[%d]=0x%04X\n",
1569 idx, reg);
Ilija Hadzicc3ad63a2013-01-02 18:27:45 -05001570 radeon_cs_dump_packet(p, pkt);
Dave Airlie551ebd82009-09-01 15:25:57 +10001571 return r;
1572 }
1573 track->zb.robj = reloc->robj;
Dave Airlie513bcb42009-09-23 16:56:27 +10001574 track->zb.offset = idx_value;
Marek Olšák40b4a752011-02-12 19:21:35 +01001575 track->zb_dirty = true;
Christian Königdf0af442014-03-03 12:38:08 +01001576 ib[idx] = idx_value + ((u32)reloc->gpu_offset);
Dave Airlie551ebd82009-09-01 15:25:57 +10001577 break;
1578 case RADEON_RB3D_COLOROFFSET:
Ilija Hadzic012e9762013-01-02 18:27:47 -05001579 r = radeon_cs_packet_next_reloc(p, &reloc, 0);
Dave Airlie551ebd82009-09-01 15:25:57 +10001580 if (r) {
1581 DRM_ERROR("No reloc for ib[%d]=0x%04X\n",
1582 idx, reg);
Ilija Hadzicc3ad63a2013-01-02 18:27:45 -05001583 radeon_cs_dump_packet(p, pkt);
Dave Airlie551ebd82009-09-01 15:25:57 +10001584 return r;
1585 }
1586 track->cb[0].robj = reloc->robj;
Dave Airlie513bcb42009-09-23 16:56:27 +10001587 track->cb[0].offset = idx_value;
Marek Olšák40b4a752011-02-12 19:21:35 +01001588 track->cb_dirty = true;
Christian Königdf0af442014-03-03 12:38:08 +01001589 ib[idx] = idx_value + ((u32)reloc->gpu_offset);
Dave Airlie551ebd82009-09-01 15:25:57 +10001590 break;
1591 case RADEON_PP_TXOFFSET_0:
1592 case RADEON_PP_TXOFFSET_1:
1593 case RADEON_PP_TXOFFSET_2:
1594 i = (reg - RADEON_PP_TXOFFSET_0) / 24;
Ilija Hadzic012e9762013-01-02 18:27:47 -05001595 r = radeon_cs_packet_next_reloc(p, &reloc, 0);
Dave Airlie551ebd82009-09-01 15:25:57 +10001596 if (r) {
1597 DRM_ERROR("No reloc for ib[%d]=0x%04X\n",
1598 idx, reg);
Ilija Hadzicc3ad63a2013-01-02 18:27:45 -05001599 radeon_cs_dump_packet(p, pkt);
Dave Airlie551ebd82009-09-01 15:25:57 +10001600 return r;
1601 }
Alex Deucherf2746f82012-02-02 10:11:12 -05001602 if (!(p->cs_flags & RADEON_CS_KEEP_TILING_FLAGS)) {
Christian Königdf0af442014-03-03 12:38:08 +01001603 if (reloc->tiling_flags & RADEON_TILING_MACRO)
Alex Deucherf2746f82012-02-02 10:11:12 -05001604 tile_flags |= RADEON_TXO_MACRO_TILE;
Christian Königdf0af442014-03-03 12:38:08 +01001605 if (reloc->tiling_flags & RADEON_TILING_MICRO)
Alex Deucherf2746f82012-02-02 10:11:12 -05001606 tile_flags |= RADEON_TXO_MICRO_TILE_X2;
1607
1608 tmp = idx_value & ~(0x7 << 2);
1609 tmp |= tile_flags;
Christian Königdf0af442014-03-03 12:38:08 +01001610 ib[idx] = tmp + ((u32)reloc->gpu_offset);
Alex Deucherf2746f82012-02-02 10:11:12 -05001611 } else
Christian Königdf0af442014-03-03 12:38:08 +01001612 ib[idx] = idx_value + ((u32)reloc->gpu_offset);
Dave Airlie551ebd82009-09-01 15:25:57 +10001613 track->textures[i].robj = reloc->robj;
Marek Olšák40b4a752011-02-12 19:21:35 +01001614 track->tex_dirty = true;
Dave Airlie551ebd82009-09-01 15:25:57 +10001615 break;
1616 case RADEON_PP_CUBIC_OFFSET_T0_0:
1617 case RADEON_PP_CUBIC_OFFSET_T0_1:
1618 case RADEON_PP_CUBIC_OFFSET_T0_2:
1619 case RADEON_PP_CUBIC_OFFSET_T0_3:
1620 case RADEON_PP_CUBIC_OFFSET_T0_4:
1621 i = (reg - RADEON_PP_CUBIC_OFFSET_T0_0) / 4;
Ilija Hadzic012e9762013-01-02 18:27:47 -05001622 r = radeon_cs_packet_next_reloc(p, &reloc, 0);
Dave Airlie551ebd82009-09-01 15:25:57 +10001623 if (r) {
1624 DRM_ERROR("No reloc for ib[%d]=0x%04X\n",
1625 idx, reg);
Ilija Hadzicc3ad63a2013-01-02 18:27:45 -05001626 radeon_cs_dump_packet(p, pkt);
Dave Airlie551ebd82009-09-01 15:25:57 +10001627 return r;
1628 }
Dave Airlie513bcb42009-09-23 16:56:27 +10001629 track->textures[0].cube_info[i].offset = idx_value;
Christian Königdf0af442014-03-03 12:38:08 +01001630 ib[idx] = idx_value + ((u32)reloc->gpu_offset);
Dave Airlie551ebd82009-09-01 15:25:57 +10001631 track->textures[0].cube_info[i].robj = reloc->robj;
Marek Olšák40b4a752011-02-12 19:21:35 +01001632 track->tex_dirty = true;
Dave Airlie551ebd82009-09-01 15:25:57 +10001633 break;
1634 case RADEON_PP_CUBIC_OFFSET_T1_0:
1635 case RADEON_PP_CUBIC_OFFSET_T1_1:
1636 case RADEON_PP_CUBIC_OFFSET_T1_2:
1637 case RADEON_PP_CUBIC_OFFSET_T1_3:
1638 case RADEON_PP_CUBIC_OFFSET_T1_4:
1639 i = (reg - RADEON_PP_CUBIC_OFFSET_T1_0) / 4;
Ilija Hadzic012e9762013-01-02 18:27:47 -05001640 r = radeon_cs_packet_next_reloc(p, &reloc, 0);
Dave Airlie551ebd82009-09-01 15:25:57 +10001641 if (r) {
1642 DRM_ERROR("No reloc for ib[%d]=0x%04X\n",
1643 idx, reg);
Ilija Hadzicc3ad63a2013-01-02 18:27:45 -05001644 radeon_cs_dump_packet(p, pkt);
Dave Airlie551ebd82009-09-01 15:25:57 +10001645 return r;
1646 }
Dave Airlie513bcb42009-09-23 16:56:27 +10001647 track->textures[1].cube_info[i].offset = idx_value;
Christian Königdf0af442014-03-03 12:38:08 +01001648 ib[idx] = idx_value + ((u32)reloc->gpu_offset);
Dave Airlie551ebd82009-09-01 15:25:57 +10001649 track->textures[1].cube_info[i].robj = reloc->robj;
Marek Olšák40b4a752011-02-12 19:21:35 +01001650 track->tex_dirty = true;
Dave Airlie551ebd82009-09-01 15:25:57 +10001651 break;
1652 case RADEON_PP_CUBIC_OFFSET_T2_0:
1653 case RADEON_PP_CUBIC_OFFSET_T2_1:
1654 case RADEON_PP_CUBIC_OFFSET_T2_2:
1655 case RADEON_PP_CUBIC_OFFSET_T2_3:
1656 case RADEON_PP_CUBIC_OFFSET_T2_4:
1657 i = (reg - RADEON_PP_CUBIC_OFFSET_T2_0) / 4;
Ilija Hadzic012e9762013-01-02 18:27:47 -05001658 r = radeon_cs_packet_next_reloc(p, &reloc, 0);
Dave Airlie551ebd82009-09-01 15:25:57 +10001659 if (r) {
1660 DRM_ERROR("No reloc for ib[%d]=0x%04X\n",
1661 idx, reg);
Ilija Hadzicc3ad63a2013-01-02 18:27:45 -05001662 radeon_cs_dump_packet(p, pkt);
Dave Airlie551ebd82009-09-01 15:25:57 +10001663 return r;
1664 }
Dave Airlie513bcb42009-09-23 16:56:27 +10001665 track->textures[2].cube_info[i].offset = idx_value;
Christian Königdf0af442014-03-03 12:38:08 +01001666 ib[idx] = idx_value + ((u32)reloc->gpu_offset);
Dave Airlie551ebd82009-09-01 15:25:57 +10001667 track->textures[2].cube_info[i].robj = reloc->robj;
Marek Olšák40b4a752011-02-12 19:21:35 +01001668 track->tex_dirty = true;
Dave Airlie551ebd82009-09-01 15:25:57 +10001669 break;
1670 case RADEON_RE_WIDTH_HEIGHT:
Dave Airlie513bcb42009-09-23 16:56:27 +10001671 track->maxy = ((idx_value >> 16) & 0x7FF);
Marek Olšák40b4a752011-02-12 19:21:35 +01001672 track->cb_dirty = true;
1673 track->zb_dirty = true;
Dave Airlie551ebd82009-09-01 15:25:57 +10001674 break;
1675 case RADEON_RB3D_COLORPITCH:
Ilija Hadzic012e9762013-01-02 18:27:47 -05001676 r = radeon_cs_packet_next_reloc(p, &reloc, 0);
Dave Airlie551ebd82009-09-01 15:25:57 +10001677 if (r) {
1678 DRM_ERROR("No reloc for ib[%d]=0x%04X\n",
1679 idx, reg);
Ilija Hadzicc3ad63a2013-01-02 18:27:45 -05001680 radeon_cs_dump_packet(p, pkt);
Dave Airlie551ebd82009-09-01 15:25:57 +10001681 return r;
1682 }
Alex Deucherc9068eb2012-02-02 10:11:11 -05001683 if (!(p->cs_flags & RADEON_CS_KEEP_TILING_FLAGS)) {
Christian Königdf0af442014-03-03 12:38:08 +01001684 if (reloc->tiling_flags & RADEON_TILING_MACRO)
Alex Deucherc9068eb2012-02-02 10:11:11 -05001685 tile_flags |= RADEON_COLOR_TILE_ENABLE;
Christian Königdf0af442014-03-03 12:38:08 +01001686 if (reloc->tiling_flags & RADEON_TILING_MICRO)
Alex Deucherc9068eb2012-02-02 10:11:11 -05001687 tile_flags |= RADEON_COLOR_MICROTILE_ENABLE;
Dave Airliee024e112009-06-24 09:48:08 +10001688
Alex Deucherc9068eb2012-02-02 10:11:11 -05001689 tmp = idx_value & ~(0x7 << 16);
1690 tmp |= tile_flags;
1691 ib[idx] = tmp;
1692 } else
1693 ib[idx] = idx_value;
Dave Airlie551ebd82009-09-01 15:25:57 +10001694
Dave Airlie513bcb42009-09-23 16:56:27 +10001695 track->cb[0].pitch = idx_value & RADEON_COLORPITCH_MASK;
Marek Olšák40b4a752011-02-12 19:21:35 +01001696 track->cb_dirty = true;
Dave Airlie551ebd82009-09-01 15:25:57 +10001697 break;
1698 case RADEON_RB3D_DEPTHPITCH:
Dave Airlie513bcb42009-09-23 16:56:27 +10001699 track->zb.pitch = idx_value & RADEON_DEPTHPITCH_MASK;
Marek Olšák40b4a752011-02-12 19:21:35 +01001700 track->zb_dirty = true;
Dave Airlie551ebd82009-09-01 15:25:57 +10001701 break;
1702 case RADEON_RB3D_CNTL:
Dave Airlie513bcb42009-09-23 16:56:27 +10001703 switch ((idx_value >> RADEON_RB3D_COLOR_FORMAT_SHIFT) & 0x1f) {
Dave Airlie551ebd82009-09-01 15:25:57 +10001704 case 7:
1705 case 8:
1706 case 9:
1707 case 11:
1708 case 12:
1709 track->cb[0].cpp = 1;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001710 break;
Dave Airlie551ebd82009-09-01 15:25:57 +10001711 case 3:
1712 case 4:
1713 case 15:
1714 track->cb[0].cpp = 2;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001715 break;
Dave Airlie551ebd82009-09-01 15:25:57 +10001716 case 6:
1717 track->cb[0].cpp = 4;
Dave Airlie17782d92009-08-21 10:07:54 +10001718 break;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001719 default:
Dave Airlie551ebd82009-09-01 15:25:57 +10001720 DRM_ERROR("Invalid color buffer format (%d) !\n",
Dave Airlie513bcb42009-09-23 16:56:27 +10001721 ((idx_value >> RADEON_RB3D_COLOR_FORMAT_SHIFT) & 0x1f));
Dave Airlie551ebd82009-09-01 15:25:57 +10001722 return -EINVAL;
1723 }
Dave Airlie513bcb42009-09-23 16:56:27 +10001724 track->z_enabled = !!(idx_value & RADEON_Z_ENABLE);
Marek Olšák40b4a752011-02-12 19:21:35 +01001725 track->cb_dirty = true;
1726 track->zb_dirty = true;
Dave Airlie551ebd82009-09-01 15:25:57 +10001727 break;
1728 case RADEON_RB3D_ZSTENCILCNTL:
Dave Airlie513bcb42009-09-23 16:56:27 +10001729 switch (idx_value & 0xf) {
Dave Airlie551ebd82009-09-01 15:25:57 +10001730 case 0:
1731 track->zb.cpp = 2;
1732 break;
1733 case 2:
1734 case 3:
1735 case 4:
1736 case 5:
1737 case 9:
1738 case 11:
1739 track->zb.cpp = 4;
1740 break;
1741 default:
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001742 break;
1743 }
Marek Olšák40b4a752011-02-12 19:21:35 +01001744 track->zb_dirty = true;
Dave Airlie551ebd82009-09-01 15:25:57 +10001745 break;
1746 case RADEON_RB3D_ZPASS_ADDR:
Ilija Hadzic012e9762013-01-02 18:27:47 -05001747 r = radeon_cs_packet_next_reloc(p, &reloc, 0);
Dave Airlie551ebd82009-09-01 15:25:57 +10001748 if (r) {
1749 DRM_ERROR("No reloc for ib[%d]=0x%04X\n",
1750 idx, reg);
Ilija Hadzicc3ad63a2013-01-02 18:27:45 -05001751 radeon_cs_dump_packet(p, pkt);
Dave Airlie551ebd82009-09-01 15:25:57 +10001752 return r;
1753 }
Christian Königdf0af442014-03-03 12:38:08 +01001754 ib[idx] = idx_value + ((u32)reloc->gpu_offset);
Dave Airlie551ebd82009-09-01 15:25:57 +10001755 break;
1756 case RADEON_PP_CNTL:
1757 {
Dave Airlie513bcb42009-09-23 16:56:27 +10001758 uint32_t temp = idx_value >> 4;
Dave Airlie551ebd82009-09-01 15:25:57 +10001759 for (i = 0; i < track->num_texture; i++)
1760 track->textures[i].enabled = !!(temp & (1 << i));
Marek Olšák40b4a752011-02-12 19:21:35 +01001761 track->tex_dirty = true;
Dave Airlie551ebd82009-09-01 15:25:57 +10001762 }
1763 break;
1764 case RADEON_SE_VF_CNTL:
Dave Airlie513bcb42009-09-23 16:56:27 +10001765 track->vap_vf_cntl = idx_value;
Dave Airlie551ebd82009-09-01 15:25:57 +10001766 break;
1767 case RADEON_SE_VTX_FMT:
Dave Airlie513bcb42009-09-23 16:56:27 +10001768 track->vtx_size = r100_get_vtx_size(idx_value);
Dave Airlie551ebd82009-09-01 15:25:57 +10001769 break;
1770 case RADEON_PP_TEX_SIZE_0:
1771 case RADEON_PP_TEX_SIZE_1:
1772 case RADEON_PP_TEX_SIZE_2:
1773 i = (reg - RADEON_PP_TEX_SIZE_0) / 8;
Dave Airlie513bcb42009-09-23 16:56:27 +10001774 track->textures[i].width = (idx_value & RADEON_TEX_USIZE_MASK) + 1;
1775 track->textures[i].height = ((idx_value & RADEON_TEX_VSIZE_MASK) >> RADEON_TEX_VSIZE_SHIFT) + 1;
Marek Olšák40b4a752011-02-12 19:21:35 +01001776 track->tex_dirty = true;
Dave Airlie551ebd82009-09-01 15:25:57 +10001777 break;
1778 case RADEON_PP_TEX_PITCH_0:
1779 case RADEON_PP_TEX_PITCH_1:
1780 case RADEON_PP_TEX_PITCH_2:
1781 i = (reg - RADEON_PP_TEX_PITCH_0) / 8;
Dave Airlie513bcb42009-09-23 16:56:27 +10001782 track->textures[i].pitch = idx_value + 32;
Marek Olšák40b4a752011-02-12 19:21:35 +01001783 track->tex_dirty = true;
Dave Airlie551ebd82009-09-01 15:25:57 +10001784 break;
1785 case RADEON_PP_TXFILTER_0:
1786 case RADEON_PP_TXFILTER_1:
1787 case RADEON_PP_TXFILTER_2:
1788 i = (reg - RADEON_PP_TXFILTER_0) / 24;
Dave Airlie513bcb42009-09-23 16:56:27 +10001789 track->textures[i].num_levels = ((idx_value & RADEON_MAX_MIP_LEVEL_MASK)
Dave Airlie551ebd82009-09-01 15:25:57 +10001790 >> RADEON_MAX_MIP_LEVEL_SHIFT);
Dave Airlie513bcb42009-09-23 16:56:27 +10001791 tmp = (idx_value >> 23) & 0x7;
Dave Airlie551ebd82009-09-01 15:25:57 +10001792 if (tmp == 2 || tmp == 6)
1793 track->textures[i].roundup_w = false;
Dave Airlie513bcb42009-09-23 16:56:27 +10001794 tmp = (idx_value >> 27) & 0x7;
Dave Airlie551ebd82009-09-01 15:25:57 +10001795 if (tmp == 2 || tmp == 6)
1796 track->textures[i].roundup_h = false;
Marek Olšák40b4a752011-02-12 19:21:35 +01001797 track->tex_dirty = true;
Dave Airlie551ebd82009-09-01 15:25:57 +10001798 break;
1799 case RADEON_PP_TXFORMAT_0:
1800 case RADEON_PP_TXFORMAT_1:
1801 case RADEON_PP_TXFORMAT_2:
1802 i = (reg - RADEON_PP_TXFORMAT_0) / 24;
Dave Airlie513bcb42009-09-23 16:56:27 +10001803 if (idx_value & RADEON_TXFORMAT_NON_POWER2) {
Dave Airlie551ebd82009-09-01 15:25:57 +10001804 track->textures[i].use_pitch = 1;
1805 } else {
1806 track->textures[i].use_pitch = 0;
Dave Airlie513bcb42009-09-23 16:56:27 +10001807 track->textures[i].width = 1 << ((idx_value >> RADEON_TXFORMAT_WIDTH_SHIFT) & RADEON_TXFORMAT_WIDTH_MASK);
1808 track->textures[i].height = 1 << ((idx_value >> RADEON_TXFORMAT_HEIGHT_SHIFT) & RADEON_TXFORMAT_HEIGHT_MASK);
Dave Airlie551ebd82009-09-01 15:25:57 +10001809 }
Dave Airlie513bcb42009-09-23 16:56:27 +10001810 if (idx_value & RADEON_TXFORMAT_CUBIC_MAP_ENABLE)
Dave Airlie551ebd82009-09-01 15:25:57 +10001811 track->textures[i].tex_coord_type = 2;
Dave Airlie513bcb42009-09-23 16:56:27 +10001812 switch ((idx_value & RADEON_TXFORMAT_FORMAT_MASK)) {
Dave Airlie551ebd82009-09-01 15:25:57 +10001813 case RADEON_TXFORMAT_I8:
1814 case RADEON_TXFORMAT_RGB332:
1815 case RADEON_TXFORMAT_Y8:
1816 track->textures[i].cpp = 1;
Roland Scheideggerf9da52d2010-06-12 12:12:37 -04001817 track->textures[i].compress_format = R100_TRACK_COMP_NONE;
Dave Airlie551ebd82009-09-01 15:25:57 +10001818 break;
1819 case RADEON_TXFORMAT_AI88:
1820 case RADEON_TXFORMAT_ARGB1555:
1821 case RADEON_TXFORMAT_RGB565:
1822 case RADEON_TXFORMAT_ARGB4444:
1823 case RADEON_TXFORMAT_VYUY422:
1824 case RADEON_TXFORMAT_YVYU422:
Dave Airlie551ebd82009-09-01 15:25:57 +10001825 case RADEON_TXFORMAT_SHADOW16:
1826 case RADEON_TXFORMAT_LDUDV655:
1827 case RADEON_TXFORMAT_DUDV88:
1828 track->textures[i].cpp = 2;
Roland Scheideggerf9da52d2010-06-12 12:12:37 -04001829 track->textures[i].compress_format = R100_TRACK_COMP_NONE;
Dave Airlie551ebd82009-09-01 15:25:57 +10001830 break;
1831 case RADEON_TXFORMAT_ARGB8888:
1832 case RADEON_TXFORMAT_RGBA8888:
Dave Airlie551ebd82009-09-01 15:25:57 +10001833 case RADEON_TXFORMAT_SHADOW32:
1834 case RADEON_TXFORMAT_LDUDUV8888:
1835 track->textures[i].cpp = 4;
Roland Scheideggerf9da52d2010-06-12 12:12:37 -04001836 track->textures[i].compress_format = R100_TRACK_COMP_NONE;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001837 break;
Dave Airlied785d782009-12-07 13:16:06 +10001838 case RADEON_TXFORMAT_DXT1:
1839 track->textures[i].cpp = 1;
1840 track->textures[i].compress_format = R100_TRACK_COMP_DXT1;
1841 break;
1842 case RADEON_TXFORMAT_DXT23:
1843 case RADEON_TXFORMAT_DXT45:
1844 track->textures[i].cpp = 1;
1845 track->textures[i].compress_format = R100_TRACK_COMP_DXT35;
1846 break;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001847 }
Dave Airlie513bcb42009-09-23 16:56:27 +10001848 track->textures[i].cube_info[4].width = 1 << ((idx_value >> 16) & 0xf);
1849 track->textures[i].cube_info[4].height = 1 << ((idx_value >> 20) & 0xf);
Marek Olšák40b4a752011-02-12 19:21:35 +01001850 track->tex_dirty = true;
Dave Airlie551ebd82009-09-01 15:25:57 +10001851 break;
1852 case RADEON_PP_CUBIC_FACES_0:
1853 case RADEON_PP_CUBIC_FACES_1:
1854 case RADEON_PP_CUBIC_FACES_2:
Dave Airlie513bcb42009-09-23 16:56:27 +10001855 tmp = idx_value;
Dave Airlie551ebd82009-09-01 15:25:57 +10001856 i = (reg - RADEON_PP_CUBIC_FACES_0) / 4;
1857 for (face = 0; face < 4; face++) {
1858 track->textures[i].cube_info[face].width = 1 << ((tmp >> (face * 8)) & 0xf);
1859 track->textures[i].cube_info[face].height = 1 << ((tmp >> ((face * 8) + 4)) & 0xf);
1860 }
Marek Olšák40b4a752011-02-12 19:21:35 +01001861 track->tex_dirty = true;
Dave Airlie551ebd82009-09-01 15:25:57 +10001862 break;
1863 default:
1864 printk(KERN_ERR "Forbidden register 0x%04X in cs at %d\n",
1865 reg, idx);
1866 return -EINVAL;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001867 }
1868 return 0;
1869}
1870
Jerome Glisse068a1172009-06-17 13:28:30 +02001871int r100_cs_track_check_pkt3_indx_buffer(struct radeon_cs_parser *p,
1872 struct radeon_cs_packet *pkt,
Jerome Glisse4c788672009-11-20 14:29:23 +01001873 struct radeon_bo *robj)
Jerome Glisse068a1172009-06-17 13:28:30 +02001874{
Jerome Glisse068a1172009-06-17 13:28:30 +02001875 unsigned idx;
Dave Airlie513bcb42009-09-23 16:56:27 +10001876 u32 value;
Jerome Glisse068a1172009-06-17 13:28:30 +02001877 idx = pkt->idx + 1;
Dave Airlie513bcb42009-09-23 16:56:27 +10001878 value = radeon_get_ib_value(p, idx + 2);
Jerome Glisse4c788672009-11-20 14:29:23 +01001879 if ((value + 1) > radeon_bo_size(robj)) {
Jerome Glisse068a1172009-06-17 13:28:30 +02001880 DRM_ERROR("[drm] Buffer too small for PACKET3 INDX_BUFFER "
1881 "(need %u have %lu) !\n",
Dave Airlie513bcb42009-09-23 16:56:27 +10001882 value + 1,
Jerome Glisse4c788672009-11-20 14:29:23 +01001883 radeon_bo_size(robj));
Jerome Glisse068a1172009-06-17 13:28:30 +02001884 return -EINVAL;
1885 }
1886 return 0;
1887}
1888
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001889static int r100_packet3_check(struct radeon_cs_parser *p,
1890 struct radeon_cs_packet *pkt)
1891{
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001892 struct radeon_cs_reloc *reloc;
Dave Airlie551ebd82009-09-01 15:25:57 +10001893 struct r100_cs_track *track;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001894 unsigned idx;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001895 volatile uint32_t *ib;
1896 int r;
1897
Jerome Glissef2e39222012-05-09 15:35:02 +02001898 ib = p->ib.ptr;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001899 idx = pkt->idx + 1;
Dave Airlie551ebd82009-09-01 15:25:57 +10001900 track = (struct r100_cs_track *)p->track;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001901 switch (pkt->opcode) {
1902 case PACKET3_3D_LOAD_VBPNTR:
Dave Airlie513bcb42009-09-23 16:56:27 +10001903 r = r100_packet3_load_vbpntr(p, pkt, idx);
1904 if (r)
1905 return r;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001906 break;
1907 case PACKET3_INDX_BUFFER:
Ilija Hadzic012e9762013-01-02 18:27:47 -05001908 r = radeon_cs_packet_next_reloc(p, &reloc, 0);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001909 if (r) {
1910 DRM_ERROR("No reloc for packet3 %d\n", pkt->opcode);
Ilija Hadzicc3ad63a2013-01-02 18:27:45 -05001911 radeon_cs_dump_packet(p, pkt);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001912 return r;
1913 }
Christian Königdf0af442014-03-03 12:38:08 +01001914 ib[idx+1] = radeon_get_ib_value(p, idx+1) + ((u32)reloc->gpu_offset);
Jerome Glisse068a1172009-06-17 13:28:30 +02001915 r = r100_cs_track_check_pkt3_indx_buffer(p, pkt, reloc->robj);
1916 if (r) {
1917 return r;
1918 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001919 break;
1920 case 0x23:
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001921 /* 3D_RNDR_GEN_INDX_PRIM on r100/r200 */
Ilija Hadzic012e9762013-01-02 18:27:47 -05001922 r = radeon_cs_packet_next_reloc(p, &reloc, 0);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001923 if (r) {
1924 DRM_ERROR("No reloc for packet3 %d\n", pkt->opcode);
Ilija Hadzicc3ad63a2013-01-02 18:27:45 -05001925 radeon_cs_dump_packet(p, pkt);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001926 return r;
1927 }
Christian Königdf0af442014-03-03 12:38:08 +01001928 ib[idx] = radeon_get_ib_value(p, idx) + ((u32)reloc->gpu_offset);
Dave Airlie551ebd82009-09-01 15:25:57 +10001929 track->num_arrays = 1;
Dave Airlie513bcb42009-09-23 16:56:27 +10001930 track->vtx_size = r100_get_vtx_size(radeon_get_ib_value(p, idx + 2));
Dave Airlie551ebd82009-09-01 15:25:57 +10001931
1932 track->arrays[0].robj = reloc->robj;
1933 track->arrays[0].esize = track->vtx_size;
1934
Dave Airlie513bcb42009-09-23 16:56:27 +10001935 track->max_indx = radeon_get_ib_value(p, idx+1);
Dave Airlie551ebd82009-09-01 15:25:57 +10001936
Dave Airlie513bcb42009-09-23 16:56:27 +10001937 track->vap_vf_cntl = radeon_get_ib_value(p, idx+3);
Dave Airlie551ebd82009-09-01 15:25:57 +10001938 track->immd_dwords = pkt->count - 1;
1939 r = r100_cs_track_check(p->rdev, track);
1940 if (r)
1941 return r;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001942 break;
1943 case PACKET3_3D_DRAW_IMMD:
Dave Airlie513bcb42009-09-23 16:56:27 +10001944 if (((radeon_get_ib_value(p, idx + 1) >> 4) & 0x3) != 3) {
Dave Airlie551ebd82009-09-01 15:25:57 +10001945 DRM_ERROR("PRIM_WALK must be 3 for IMMD draw\n");
1946 return -EINVAL;
1947 }
Alex Deuchercf57fc72010-01-18 20:20:07 -05001948 track->vtx_size = r100_get_vtx_size(radeon_get_ib_value(p, idx + 0));
Dave Airlie513bcb42009-09-23 16:56:27 +10001949 track->vap_vf_cntl = radeon_get_ib_value(p, idx + 1);
Dave Airlie551ebd82009-09-01 15:25:57 +10001950 track->immd_dwords = pkt->count - 1;
1951 r = r100_cs_track_check(p->rdev, track);
1952 if (r)
1953 return r;
1954 break;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001955 /* triggers drawing using in-packet vertex data */
1956 case PACKET3_3D_DRAW_IMMD_2:
Dave Airlie513bcb42009-09-23 16:56:27 +10001957 if (((radeon_get_ib_value(p, idx) >> 4) & 0x3) != 3) {
Dave Airlie551ebd82009-09-01 15:25:57 +10001958 DRM_ERROR("PRIM_WALK must be 3 for IMMD draw\n");
1959 return -EINVAL;
1960 }
Dave Airlie513bcb42009-09-23 16:56:27 +10001961 track->vap_vf_cntl = radeon_get_ib_value(p, idx);
Dave Airlie551ebd82009-09-01 15:25:57 +10001962 track->immd_dwords = pkt->count;
1963 r = r100_cs_track_check(p->rdev, track);
1964 if (r)
1965 return r;
1966 break;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001967 /* triggers drawing using in-packet vertex data */
1968 case PACKET3_3D_DRAW_VBUF_2:
Dave Airlie513bcb42009-09-23 16:56:27 +10001969 track->vap_vf_cntl = radeon_get_ib_value(p, idx);
Dave Airlie551ebd82009-09-01 15:25:57 +10001970 r = r100_cs_track_check(p->rdev, track);
1971 if (r)
1972 return r;
1973 break;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001974 /* triggers drawing of vertex buffers setup elsewhere */
1975 case PACKET3_3D_DRAW_INDX_2:
Dave Airlie513bcb42009-09-23 16:56:27 +10001976 track->vap_vf_cntl = radeon_get_ib_value(p, idx);
Dave Airlie551ebd82009-09-01 15:25:57 +10001977 r = r100_cs_track_check(p->rdev, track);
1978 if (r)
1979 return r;
1980 break;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001981 /* triggers drawing using indices to vertex buffer */
1982 case PACKET3_3D_DRAW_VBUF:
Dave Airlie513bcb42009-09-23 16:56:27 +10001983 track->vap_vf_cntl = radeon_get_ib_value(p, idx + 1);
Dave Airlie551ebd82009-09-01 15:25:57 +10001984 r = r100_cs_track_check(p->rdev, track);
1985 if (r)
1986 return r;
1987 break;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001988 /* triggers drawing of vertex buffers setup elsewhere */
1989 case PACKET3_3D_DRAW_INDX:
Dave Airlie513bcb42009-09-23 16:56:27 +10001990 track->vap_vf_cntl = radeon_get_ib_value(p, idx + 1);
Dave Airlie551ebd82009-09-01 15:25:57 +10001991 r = r100_cs_track_check(p->rdev, track);
1992 if (r)
1993 return r;
1994 break;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001995 /* triggers drawing using indices to vertex buffer */
Dave Airlieab9e1f52010-07-13 11:11:11 +10001996 case PACKET3_3D_CLEAR_HIZ:
1997 case PACKET3_3D_CLEAR_ZMASK:
1998 if (p->rdev->hyperz_filp != p->filp)
1999 return -EINVAL;
2000 break;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002001 case PACKET3_NOP:
2002 break;
2003 default:
2004 DRM_ERROR("Packet3 opcode %x not supported\n", pkt->opcode);
2005 return -EINVAL;
2006 }
2007 return 0;
2008}
2009
2010int r100_cs_parse(struct radeon_cs_parser *p)
2011{
2012 struct radeon_cs_packet pkt;
Jerome Glisse9f022dd2009-09-11 15:35:22 +02002013 struct r100_cs_track *track;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002014 int r;
2015
Jerome Glisse9f022dd2009-09-11 15:35:22 +02002016 track = kzalloc(sizeof(*track), GFP_KERNEL);
Dan Carpenterce067912012-05-15 11:56:59 +03002017 if (!track)
2018 return -ENOMEM;
Jerome Glisse9f022dd2009-09-11 15:35:22 +02002019 r100_cs_track_clear(p->rdev, track);
2020 p->track = track;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002021 do {
Ilija Hadzicc38f34b2013-01-02 18:27:41 -05002022 r = radeon_cs_packet_parse(p, &pkt, p->idx);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002023 if (r) {
2024 return r;
2025 }
2026 p->idx += pkt.count + 2;
2027 switch (pkt.type) {
Ilija Hadzic4e872ae2013-01-02 18:27:48 -05002028 case RADEON_PACKET_TYPE0:
Ilija Hadzic66b35432013-01-02 18:27:39 -05002029 if (p->rdev->family >= CHIP_R200)
2030 r = r100_cs_parse_packet0(p, &pkt,
2031 p->rdev->config.r100.reg_safe_bm,
2032 p->rdev->config.r100.reg_safe_bm_size,
2033 &r200_packet0_check);
2034 else
2035 r = r100_cs_parse_packet0(p, &pkt,
2036 p->rdev->config.r100.reg_safe_bm,
2037 p->rdev->config.r100.reg_safe_bm_size,
2038 &r100_packet0_check);
2039 break;
Ilija Hadzic4e872ae2013-01-02 18:27:48 -05002040 case RADEON_PACKET_TYPE2:
Ilija Hadzic66b35432013-01-02 18:27:39 -05002041 break;
Ilija Hadzic4e872ae2013-01-02 18:27:48 -05002042 case RADEON_PACKET_TYPE3:
Ilija Hadzic66b35432013-01-02 18:27:39 -05002043 r = r100_packet3_check(p, &pkt);
2044 break;
2045 default:
2046 DRM_ERROR("Unknown packet type %d !\n",
2047 pkt.type);
2048 return -EINVAL;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002049 }
Ilija Hadzic66b35432013-01-02 18:27:39 -05002050 if (r)
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002051 return r;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002052 } while (p->idx < p->chunks[p->chunk_ib_idx].length_dw);
2053 return 0;
2054}
2055
Alex Deucher0242f742012-06-28 17:50:34 -04002056static void r100_cs_track_texture_print(struct r100_cs_track_texture *t)
2057{
2058 DRM_ERROR("pitch %d\n", t->pitch);
2059 DRM_ERROR("use_pitch %d\n", t->use_pitch);
2060 DRM_ERROR("width %d\n", t->width);
2061 DRM_ERROR("width_11 %d\n", t->width_11);
2062 DRM_ERROR("height %d\n", t->height);
2063 DRM_ERROR("height_11 %d\n", t->height_11);
2064 DRM_ERROR("num levels %d\n", t->num_levels);
2065 DRM_ERROR("depth %d\n", t->txdepth);
2066 DRM_ERROR("bpp %d\n", t->cpp);
2067 DRM_ERROR("coordinate type %d\n", t->tex_coord_type);
2068 DRM_ERROR("width round to power of 2 %d\n", t->roundup_w);
2069 DRM_ERROR("height round to power of 2 %d\n", t->roundup_h);
2070 DRM_ERROR("compress format %d\n", t->compress_format);
2071}
2072
2073static int r100_track_compress_size(int compress_format, int w, int h)
2074{
2075 int block_width, block_height, block_bytes;
2076 int wblocks, hblocks;
2077 int min_wblocks;
2078 int sz;
2079
2080 block_width = 4;
2081 block_height = 4;
2082
2083 switch (compress_format) {
2084 case R100_TRACK_COMP_DXT1:
2085 block_bytes = 8;
2086 min_wblocks = 4;
2087 break;
2088 default:
2089 case R100_TRACK_COMP_DXT35:
2090 block_bytes = 16;
2091 min_wblocks = 2;
2092 break;
2093 }
2094
2095 hblocks = (h + block_height - 1) / block_height;
2096 wblocks = (w + block_width - 1) / block_width;
2097 if (wblocks < min_wblocks)
2098 wblocks = min_wblocks;
2099 sz = wblocks * hblocks * block_bytes;
2100 return sz;
2101}
2102
2103static int r100_cs_track_cube(struct radeon_device *rdev,
2104 struct r100_cs_track *track, unsigned idx)
2105{
2106 unsigned face, w, h;
2107 struct radeon_bo *cube_robj;
2108 unsigned long size;
2109 unsigned compress_format = track->textures[idx].compress_format;
2110
2111 for (face = 0; face < 5; face++) {
2112 cube_robj = track->textures[idx].cube_info[face].robj;
2113 w = track->textures[idx].cube_info[face].width;
2114 h = track->textures[idx].cube_info[face].height;
2115
2116 if (compress_format) {
2117 size = r100_track_compress_size(compress_format, w, h);
2118 } else
2119 size = w * h;
2120 size *= track->textures[idx].cpp;
2121
2122 size += track->textures[idx].cube_info[face].offset;
2123
2124 if (size > radeon_bo_size(cube_robj)) {
2125 DRM_ERROR("Cube texture offset greater than object size %lu %lu\n",
2126 size, radeon_bo_size(cube_robj));
2127 r100_cs_track_texture_print(&track->textures[idx]);
2128 return -1;
2129 }
2130 }
2131 return 0;
2132}
2133
2134static int r100_cs_track_texture_check(struct radeon_device *rdev,
2135 struct r100_cs_track *track)
2136{
2137 struct radeon_bo *robj;
2138 unsigned long size;
2139 unsigned u, i, w, h, d;
2140 int ret;
2141
2142 for (u = 0; u < track->num_texture; u++) {
2143 if (!track->textures[u].enabled)
2144 continue;
2145 if (track->textures[u].lookup_disable)
2146 continue;
2147 robj = track->textures[u].robj;
2148 if (robj == NULL) {
2149 DRM_ERROR("No texture bound to unit %u\n", u);
2150 return -EINVAL;
2151 }
2152 size = 0;
2153 for (i = 0; i <= track->textures[u].num_levels; i++) {
2154 if (track->textures[u].use_pitch) {
2155 if (rdev->family < CHIP_R300)
2156 w = (track->textures[u].pitch / track->textures[u].cpp) / (1 << i);
2157 else
2158 w = track->textures[u].pitch / (1 << i);
2159 } else {
2160 w = track->textures[u].width;
2161 if (rdev->family >= CHIP_RV515)
2162 w |= track->textures[u].width_11;
2163 w = w / (1 << i);
2164 if (track->textures[u].roundup_w)
2165 w = roundup_pow_of_two(w);
2166 }
2167 h = track->textures[u].height;
2168 if (rdev->family >= CHIP_RV515)
2169 h |= track->textures[u].height_11;
2170 h = h / (1 << i);
2171 if (track->textures[u].roundup_h)
2172 h = roundup_pow_of_two(h);
2173 if (track->textures[u].tex_coord_type == 1) {
2174 d = (1 << track->textures[u].txdepth) / (1 << i);
2175 if (!d)
2176 d = 1;
2177 } else {
2178 d = 1;
2179 }
2180 if (track->textures[u].compress_format) {
2181
2182 size += r100_track_compress_size(track->textures[u].compress_format, w, h) * d;
2183 /* compressed textures are block based */
2184 } else
2185 size += w * h * d;
2186 }
2187 size *= track->textures[u].cpp;
2188
2189 switch (track->textures[u].tex_coord_type) {
2190 case 0:
2191 case 1:
2192 break;
2193 case 2:
2194 if (track->separate_cube) {
2195 ret = r100_cs_track_cube(rdev, track, u);
2196 if (ret)
2197 return ret;
2198 } else
2199 size *= 6;
2200 break;
2201 default:
2202 DRM_ERROR("Invalid texture coordinate type %u for unit "
2203 "%u\n", track->textures[u].tex_coord_type, u);
2204 return -EINVAL;
2205 }
2206 if (size > radeon_bo_size(robj)) {
2207 DRM_ERROR("Texture of unit %u needs %lu bytes but is "
2208 "%lu\n", u, size, radeon_bo_size(robj));
2209 r100_cs_track_texture_print(&track->textures[u]);
2210 return -EINVAL;
2211 }
2212 }
2213 return 0;
2214}
2215
2216int r100_cs_track_check(struct radeon_device *rdev, struct r100_cs_track *track)
2217{
2218 unsigned i;
2219 unsigned long size;
2220 unsigned prim_walk;
2221 unsigned nverts;
2222 unsigned num_cb = track->cb_dirty ? track->num_cb : 0;
2223
2224 if (num_cb && !track->zb_cb_clear && !track->color_channel_mask &&
2225 !track->blend_read_enable)
2226 num_cb = 0;
2227
2228 for (i = 0; i < num_cb; i++) {
2229 if (track->cb[i].robj == NULL) {
2230 DRM_ERROR("[drm] No buffer for color buffer %d !\n", i);
2231 return -EINVAL;
2232 }
2233 size = track->cb[i].pitch * track->cb[i].cpp * track->maxy;
2234 size += track->cb[i].offset;
2235 if (size > radeon_bo_size(track->cb[i].robj)) {
2236 DRM_ERROR("[drm] Buffer too small for color buffer %d "
2237 "(need %lu have %lu) !\n", i, size,
2238 radeon_bo_size(track->cb[i].robj));
2239 DRM_ERROR("[drm] color buffer %d (%u %u %u %u)\n",
2240 i, track->cb[i].pitch, track->cb[i].cpp,
2241 track->cb[i].offset, track->maxy);
2242 return -EINVAL;
2243 }
2244 }
2245 track->cb_dirty = false;
2246
2247 if (track->zb_dirty && track->z_enabled) {
2248 if (track->zb.robj == NULL) {
2249 DRM_ERROR("[drm] No buffer for z buffer !\n");
2250 return -EINVAL;
2251 }
2252 size = track->zb.pitch * track->zb.cpp * track->maxy;
2253 size += track->zb.offset;
2254 if (size > radeon_bo_size(track->zb.robj)) {
2255 DRM_ERROR("[drm] Buffer too small for z buffer "
2256 "(need %lu have %lu) !\n", size,
2257 radeon_bo_size(track->zb.robj));
2258 DRM_ERROR("[drm] zbuffer (%u %u %u %u)\n",
2259 track->zb.pitch, track->zb.cpp,
2260 track->zb.offset, track->maxy);
2261 return -EINVAL;
2262 }
2263 }
2264 track->zb_dirty = false;
2265
2266 if (track->aa_dirty && track->aaresolve) {
2267 if (track->aa.robj == NULL) {
2268 DRM_ERROR("[drm] No buffer for AA resolve buffer %d !\n", i);
2269 return -EINVAL;
2270 }
2271 /* I believe the format comes from colorbuffer0. */
2272 size = track->aa.pitch * track->cb[0].cpp * track->maxy;
2273 size += track->aa.offset;
2274 if (size > radeon_bo_size(track->aa.robj)) {
2275 DRM_ERROR("[drm] Buffer too small for AA resolve buffer %d "
2276 "(need %lu have %lu) !\n", i, size,
2277 radeon_bo_size(track->aa.robj));
2278 DRM_ERROR("[drm] AA resolve buffer %d (%u %u %u %u)\n",
2279 i, track->aa.pitch, track->cb[0].cpp,
2280 track->aa.offset, track->maxy);
2281 return -EINVAL;
2282 }
2283 }
2284 track->aa_dirty = false;
2285
2286 prim_walk = (track->vap_vf_cntl >> 4) & 0x3;
2287 if (track->vap_vf_cntl & (1 << 14)) {
2288 nverts = track->vap_alt_nverts;
2289 } else {
2290 nverts = (track->vap_vf_cntl >> 16) & 0xFFFF;
2291 }
2292 switch (prim_walk) {
2293 case 1:
2294 for (i = 0; i < track->num_arrays; i++) {
2295 size = track->arrays[i].esize * track->max_indx * 4;
2296 if (track->arrays[i].robj == NULL) {
2297 DRM_ERROR("(PW %u) Vertex array %u no buffer "
2298 "bound\n", prim_walk, i);
2299 return -EINVAL;
2300 }
2301 if (size > radeon_bo_size(track->arrays[i].robj)) {
2302 dev_err(rdev->dev, "(PW %u) Vertex array %u "
2303 "need %lu dwords have %lu dwords\n",
2304 prim_walk, i, size >> 2,
2305 radeon_bo_size(track->arrays[i].robj)
2306 >> 2);
2307 DRM_ERROR("Max indices %u\n", track->max_indx);
2308 return -EINVAL;
2309 }
2310 }
2311 break;
2312 case 2:
2313 for (i = 0; i < track->num_arrays; i++) {
2314 size = track->arrays[i].esize * (nverts - 1) * 4;
2315 if (track->arrays[i].robj == NULL) {
2316 DRM_ERROR("(PW %u) Vertex array %u no buffer "
2317 "bound\n", prim_walk, i);
2318 return -EINVAL;
2319 }
2320 if (size > radeon_bo_size(track->arrays[i].robj)) {
2321 dev_err(rdev->dev, "(PW %u) Vertex array %u "
2322 "need %lu dwords have %lu dwords\n",
2323 prim_walk, i, size >> 2,
2324 radeon_bo_size(track->arrays[i].robj)
2325 >> 2);
2326 return -EINVAL;
2327 }
2328 }
2329 break;
2330 case 3:
2331 size = track->vtx_size * nverts;
2332 if (size != track->immd_dwords) {
2333 DRM_ERROR("IMMD draw %u dwors but needs %lu dwords\n",
2334 track->immd_dwords, size);
2335 DRM_ERROR("VAP_VF_CNTL.NUM_VERTICES %u, VTX_SIZE %u\n",
2336 nverts, track->vtx_size);
2337 return -EINVAL;
2338 }
2339 break;
2340 default:
2341 DRM_ERROR("[drm] Invalid primitive walk %d for VAP_VF_CNTL\n",
2342 prim_walk);
2343 return -EINVAL;
2344 }
2345
2346 if (track->tex_dirty) {
2347 track->tex_dirty = false;
2348 return r100_cs_track_texture_check(rdev, track);
2349 }
2350 return 0;
2351}
2352
2353void r100_cs_track_clear(struct radeon_device *rdev, struct r100_cs_track *track)
2354{
2355 unsigned i, face;
2356
2357 track->cb_dirty = true;
2358 track->zb_dirty = true;
2359 track->tex_dirty = true;
2360 track->aa_dirty = true;
2361
2362 if (rdev->family < CHIP_R300) {
2363 track->num_cb = 1;
2364 if (rdev->family <= CHIP_RS200)
2365 track->num_texture = 3;
2366 else
2367 track->num_texture = 6;
2368 track->maxy = 2048;
2369 track->separate_cube = 1;
2370 } else {
2371 track->num_cb = 4;
2372 track->num_texture = 16;
2373 track->maxy = 4096;
2374 track->separate_cube = 0;
2375 track->aaresolve = false;
2376 track->aa.robj = NULL;
2377 }
2378
2379 for (i = 0; i < track->num_cb; i++) {
2380 track->cb[i].robj = NULL;
2381 track->cb[i].pitch = 8192;
2382 track->cb[i].cpp = 16;
2383 track->cb[i].offset = 0;
2384 }
2385 track->z_enabled = true;
2386 track->zb.robj = NULL;
2387 track->zb.pitch = 8192;
2388 track->zb.cpp = 4;
2389 track->zb.offset = 0;
2390 track->vtx_size = 0x7F;
2391 track->immd_dwords = 0xFFFFFFFFUL;
2392 track->num_arrays = 11;
2393 track->max_indx = 0x00FFFFFFUL;
2394 for (i = 0; i < track->num_arrays; i++) {
2395 track->arrays[i].robj = NULL;
2396 track->arrays[i].esize = 0x7F;
2397 }
2398 for (i = 0; i < track->num_texture; i++) {
2399 track->textures[i].compress_format = R100_TRACK_COMP_NONE;
2400 track->textures[i].pitch = 16536;
2401 track->textures[i].width = 16536;
2402 track->textures[i].height = 16536;
2403 track->textures[i].width_11 = 1 << 11;
2404 track->textures[i].height_11 = 1 << 11;
2405 track->textures[i].num_levels = 12;
2406 if (rdev->family <= CHIP_RS200) {
2407 track->textures[i].tex_coord_type = 0;
2408 track->textures[i].txdepth = 0;
2409 } else {
2410 track->textures[i].txdepth = 16;
2411 track->textures[i].tex_coord_type = 1;
2412 }
2413 track->textures[i].cpp = 64;
2414 track->textures[i].robj = NULL;
2415 /* CS IB emission code makes sure texture unit are disabled */
2416 track->textures[i].enabled = false;
2417 track->textures[i].lookup_disable = false;
2418 track->textures[i].roundup_w = true;
2419 track->textures[i].roundup_h = true;
2420 if (track->separate_cube)
2421 for (face = 0; face < 5; face++) {
2422 track->textures[i].cube_info[face].robj = NULL;
2423 track->textures[i].cube_info[face].width = 16536;
2424 track->textures[i].cube_info[face].height = 16536;
2425 track->textures[i].cube_info[face].offset = 0;
2426 }
2427 }
2428}
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002429
2430/*
2431 * Global GPU functions
2432 */
Lauri Kasanen1109ca02012-08-31 13:43:50 -04002433static void r100_errata(struct radeon_device *rdev)
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002434{
2435 rdev->pll_errata = 0;
2436
2437 if (rdev->family == CHIP_RV200 || rdev->family == CHIP_RS200) {
2438 rdev->pll_errata |= CHIP_ERRATA_PLL_DUMMYREADS;
2439 }
2440
2441 if (rdev->family == CHIP_RV100 ||
2442 rdev->family == CHIP_RS100 ||
2443 rdev->family == CHIP_RS200) {
2444 rdev->pll_errata |= CHIP_ERRATA_PLL_DELAY;
2445 }
2446}
2447
Lauri Kasanen1109ca02012-08-31 13:43:50 -04002448static int r100_rbbm_fifo_wait_for_entry(struct radeon_device *rdev, unsigned n)
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002449{
2450 unsigned i;
2451 uint32_t tmp;
2452
2453 for (i = 0; i < rdev->usec_timeout; i++) {
2454 tmp = RREG32(RADEON_RBBM_STATUS) & RADEON_RBBM_FIFOCNT_MASK;
2455 if (tmp >= n) {
2456 return 0;
2457 }
2458 DRM_UDELAY(1);
2459 }
2460 return -1;
2461}
2462
2463int r100_gui_wait_for_idle(struct radeon_device *rdev)
2464{
2465 unsigned i;
2466 uint32_t tmp;
2467
2468 if (r100_rbbm_fifo_wait_for_entry(rdev, 64)) {
2469 printk(KERN_WARNING "radeon: wait for empty RBBM fifo failed !"
2470 " Bad things might happen.\n");
2471 }
2472 for (i = 0; i < rdev->usec_timeout; i++) {
2473 tmp = RREG32(RADEON_RBBM_STATUS);
Alex Deucher4612dc92010-02-05 01:58:28 -05002474 if (!(tmp & RADEON_RBBM_ACTIVE)) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002475 return 0;
2476 }
2477 DRM_UDELAY(1);
2478 }
2479 return -1;
2480}
2481
2482int r100_mc_wait_for_idle(struct radeon_device *rdev)
2483{
2484 unsigned i;
2485 uint32_t tmp;
2486
2487 for (i = 0; i < rdev->usec_timeout; i++) {
2488 /* read MC_STATUS */
Alex Deucher4612dc92010-02-05 01:58:28 -05002489 tmp = RREG32(RADEON_MC_STATUS);
2490 if (tmp & RADEON_MC_IDLE) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002491 return 0;
2492 }
2493 DRM_UDELAY(1);
2494 }
2495 return -1;
2496}
2497
Christian Könige32eb502011-10-23 12:56:27 +02002498bool r100_gpu_is_lockup(struct radeon_device *rdev, struct radeon_ring *ring)
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002499{
Jerome Glisse225758d2010-03-09 14:45:10 +00002500 u32 rbbm_status;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002501
Jerome Glisse225758d2010-03-09 14:45:10 +00002502 rbbm_status = RREG32(R_000E40_RBBM_STATUS);
2503 if (!G_000E40_GUI_ACTIVE(rbbm_status)) {
Christian Königff212f22014-02-18 14:52:33 +01002504 radeon_ring_lockup_update(rdev, ring);
Jerome Glisse225758d2010-03-09 14:45:10 +00002505 return false;
2506 }
Christian König069211e2012-05-02 15:11:20 +02002507 return radeon_ring_test_lockup(rdev, ring);
Jerome Glisse225758d2010-03-09 14:45:10 +00002508}
2509
Alex Deucher74da01d2012-06-28 17:50:35 -04002510/* required on r1xx, r2xx, r300, r(v)350, r420/r481, rs400/rs480 */
2511void r100_enable_bm(struct radeon_device *rdev)
2512{
2513 uint32_t tmp;
2514 /* Enable bus mastering */
2515 tmp = RREG32(RADEON_BUS_CNTL) & ~RADEON_BUS_MASTER_DIS;
2516 WREG32(RADEON_BUS_CNTL, tmp);
2517}
2518
Jerome Glisse90aca4d2010-03-09 14:45:12 +00002519void r100_bm_disable(struct radeon_device *rdev)
2520{
2521 u32 tmp;
2522
2523 /* disable bus mastering */
2524 tmp = RREG32(R_000030_BUS_CNTL);
2525 WREG32(R_000030_BUS_CNTL, (tmp & 0xFFFFFFFF) | 0x00000044);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002526 mdelay(1);
Jerome Glisse90aca4d2010-03-09 14:45:12 +00002527 WREG32(R_000030_BUS_CNTL, (tmp & 0xFFFFFFFF) | 0x00000042);
2528 mdelay(1);
2529 WREG32(R_000030_BUS_CNTL, (tmp & 0xFFFFFFFF) | 0x00000040);
2530 tmp = RREG32(RADEON_BUS_CNTL);
2531 mdelay(1);
Michel Dänzer642ce522012-01-12 16:04:11 +01002532 pci_clear_master(rdev->pdev);
Jerome Glisse90aca4d2010-03-09 14:45:12 +00002533 mdelay(1);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002534}
2535
Jerome Glissea2d07b72010-03-09 14:45:11 +00002536int r100_asic_reset(struct radeon_device *rdev)
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002537{
Jerome Glisse90aca4d2010-03-09 14:45:12 +00002538 struct r100_mc_save save;
2539 u32 status, tmp;
Alex Deucher25b2ec5b2011-01-11 13:36:55 -05002540 int ret = 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002541
Jerome Glisse90aca4d2010-03-09 14:45:12 +00002542 status = RREG32(R_000E40_RBBM_STATUS);
2543 if (!G_000E40_GUI_ACTIVE(status)) {
2544 return 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002545 }
Alex Deucher25b2ec5b2011-01-11 13:36:55 -05002546 r100_mc_stop(rdev, &save);
Jerome Glisse90aca4d2010-03-09 14:45:12 +00002547 status = RREG32(R_000E40_RBBM_STATUS);
2548 dev_info(rdev->dev, "(%s:%d) RBBM_STATUS=0x%08X\n", __func__, __LINE__, status);
2549 /* stop CP */
2550 WREG32(RADEON_CP_CSQ_CNTL, 0);
2551 tmp = RREG32(RADEON_CP_RB_CNTL);
2552 WREG32(RADEON_CP_RB_CNTL, tmp | RADEON_RB_RPTR_WR_ENA);
2553 WREG32(RADEON_CP_RB_RPTR_WR, 0);
2554 WREG32(RADEON_CP_RB_WPTR, 0);
2555 WREG32(RADEON_CP_RB_CNTL, tmp);
2556 /* save PCI state */
2557 pci_save_state(rdev->pdev);
2558 /* disable bus mastering */
2559 r100_bm_disable(rdev);
2560 WREG32(R_0000F0_RBBM_SOFT_RESET, S_0000F0_SOFT_RESET_SE(1) |
2561 S_0000F0_SOFT_RESET_RE(1) |
2562 S_0000F0_SOFT_RESET_PP(1) |
2563 S_0000F0_SOFT_RESET_RB(1));
2564 RREG32(R_0000F0_RBBM_SOFT_RESET);
2565 mdelay(500);
2566 WREG32(R_0000F0_RBBM_SOFT_RESET, 0);
2567 mdelay(1);
2568 status = RREG32(R_000E40_RBBM_STATUS);
2569 dev_info(rdev->dev, "(%s:%d) RBBM_STATUS=0x%08X\n", __func__, __LINE__, status);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002570 /* reset CP */
Jerome Glisse90aca4d2010-03-09 14:45:12 +00002571 WREG32(R_0000F0_RBBM_SOFT_RESET, S_0000F0_SOFT_RESET_CP(1));
2572 RREG32(R_0000F0_RBBM_SOFT_RESET);
2573 mdelay(500);
2574 WREG32(R_0000F0_RBBM_SOFT_RESET, 0);
2575 mdelay(1);
2576 status = RREG32(R_000E40_RBBM_STATUS);
2577 dev_info(rdev->dev, "(%s:%d) RBBM_STATUS=0x%08X\n", __func__, __LINE__, status);
2578 /* restore PCI & busmastering */
2579 pci_restore_state(rdev->pdev);
2580 r100_enable_bm(rdev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002581 /* Check if GPU is idle */
Jerome Glisse90aca4d2010-03-09 14:45:12 +00002582 if (G_000E40_SE_BUSY(status) || G_000E40_RE_BUSY(status) ||
2583 G_000E40_TAM_BUSY(status) || G_000E40_PB_BUSY(status)) {
2584 dev_err(rdev->dev, "failed to reset GPU\n");
Alex Deucher25b2ec5b2011-01-11 13:36:55 -05002585 ret = -1;
2586 } else
2587 dev_info(rdev->dev, "GPU reset succeed\n");
Jerome Glisse90aca4d2010-03-09 14:45:12 +00002588 r100_mc_resume(rdev, &save);
Alex Deucher25b2ec5b2011-01-11 13:36:55 -05002589 return ret;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002590}
2591
Alex Deucher92cde002009-12-04 10:55:12 -05002592void r100_set_common_regs(struct radeon_device *rdev)
2593{
Alex Deucher2739d492010-02-05 03:34:16 -05002594 struct drm_device *dev = rdev->ddev;
2595 bool force_dac2 = false;
Dave Airlied6680462010-03-31 13:41:35 +10002596 u32 tmp;
Alex Deucher2739d492010-02-05 03:34:16 -05002597
Alex Deucher92cde002009-12-04 10:55:12 -05002598 /* set these so they don't interfere with anything */
2599 WREG32(RADEON_OV0_SCALE_CNTL, 0);
2600 WREG32(RADEON_SUBPIC_CNTL, 0);
2601 WREG32(RADEON_VIPH_CONTROL, 0);
2602 WREG32(RADEON_I2C_CNTL_1, 0);
2603 WREG32(RADEON_DVI_I2C_CNTL_1, 0);
2604 WREG32(RADEON_CAP0_TRIG_CNTL, 0);
2605 WREG32(RADEON_CAP1_TRIG_CNTL, 0);
Alex Deucher2739d492010-02-05 03:34:16 -05002606
2607 /* always set up dac2 on rn50 and some rv100 as lots
2608 * of servers seem to wire it up to a VGA port but
2609 * don't report it in the bios connector
2610 * table.
2611 */
2612 switch (dev->pdev->device) {
2613 /* RN50 */
2614 case 0x515e:
2615 case 0x5969:
2616 force_dac2 = true;
2617 break;
2618 /* RV100*/
2619 case 0x5159:
2620 case 0x515a:
2621 /* DELL triple head servers */
2622 if ((dev->pdev->subsystem_vendor == 0x1028 /* DELL */) &&
2623 ((dev->pdev->subsystem_device == 0x016c) ||
2624 (dev->pdev->subsystem_device == 0x016d) ||
2625 (dev->pdev->subsystem_device == 0x016e) ||
2626 (dev->pdev->subsystem_device == 0x016f) ||
2627 (dev->pdev->subsystem_device == 0x0170) ||
2628 (dev->pdev->subsystem_device == 0x017d) ||
2629 (dev->pdev->subsystem_device == 0x017e) ||
2630 (dev->pdev->subsystem_device == 0x0183) ||
2631 (dev->pdev->subsystem_device == 0x018a) ||
2632 (dev->pdev->subsystem_device == 0x019a)))
2633 force_dac2 = true;
2634 break;
2635 }
2636
2637 if (force_dac2) {
2638 u32 disp_hw_debug = RREG32(RADEON_DISP_HW_DEBUG);
2639 u32 tv_dac_cntl = RREG32(RADEON_TV_DAC_CNTL);
2640 u32 dac2_cntl = RREG32(RADEON_DAC_CNTL2);
2641
2642 /* For CRT on DAC2, don't turn it on if BIOS didn't
2643 enable it, even it's detected.
2644 */
2645
2646 /* force it to crtc0 */
2647 dac2_cntl &= ~RADEON_DAC2_DAC_CLK_SEL;
2648 dac2_cntl |= RADEON_DAC2_DAC2_CLK_SEL;
2649 disp_hw_debug |= RADEON_CRT2_DISP1_SEL;
2650
2651 /* set up the TV DAC */
2652 tv_dac_cntl &= ~(RADEON_TV_DAC_PEDESTAL |
2653 RADEON_TV_DAC_STD_MASK |
2654 RADEON_TV_DAC_RDACPD |
2655 RADEON_TV_DAC_GDACPD |
2656 RADEON_TV_DAC_BDACPD |
2657 RADEON_TV_DAC_BGADJ_MASK |
2658 RADEON_TV_DAC_DACADJ_MASK);
2659 tv_dac_cntl |= (RADEON_TV_DAC_NBLANK |
2660 RADEON_TV_DAC_NHOLD |
2661 RADEON_TV_DAC_STD_PS2 |
2662 (0x58 << 16));
2663
2664 WREG32(RADEON_TV_DAC_CNTL, tv_dac_cntl);
2665 WREG32(RADEON_DISP_HW_DEBUG, disp_hw_debug);
2666 WREG32(RADEON_DAC_CNTL2, dac2_cntl);
2667 }
Dave Airlied6680462010-03-31 13:41:35 +10002668
2669 /* switch PM block to ACPI mode */
2670 tmp = RREG32_PLL(RADEON_PLL_PWRMGT_CNTL);
2671 tmp &= ~RADEON_PM_MODE_SEL;
2672 WREG32_PLL(RADEON_PLL_PWRMGT_CNTL, tmp);
2673
Alex Deucher92cde002009-12-04 10:55:12 -05002674}
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002675
2676/*
2677 * VRAM info
2678 */
2679static void r100_vram_get_type(struct radeon_device *rdev)
2680{
2681 uint32_t tmp;
2682
2683 rdev->mc.vram_is_ddr = false;
2684 if (rdev->flags & RADEON_IS_IGP)
2685 rdev->mc.vram_is_ddr = true;
2686 else if (RREG32(RADEON_MEM_SDRAM_MODE_REG) & RADEON_MEM_CFG_TYPE_DDR)
2687 rdev->mc.vram_is_ddr = true;
2688 if ((rdev->family == CHIP_RV100) ||
2689 (rdev->family == CHIP_RS100) ||
2690 (rdev->family == CHIP_RS200)) {
2691 tmp = RREG32(RADEON_MEM_CNTL);
2692 if (tmp & RV100_HALF_MODE) {
2693 rdev->mc.vram_width = 32;
2694 } else {
2695 rdev->mc.vram_width = 64;
2696 }
2697 if (rdev->flags & RADEON_SINGLE_CRTC) {
2698 rdev->mc.vram_width /= 4;
2699 rdev->mc.vram_is_ddr = true;
2700 }
2701 } else if (rdev->family <= CHIP_RV280) {
2702 tmp = RREG32(RADEON_MEM_CNTL);
2703 if (tmp & RADEON_MEM_NUM_CHANNELS_MASK) {
2704 rdev->mc.vram_width = 128;
2705 } else {
2706 rdev->mc.vram_width = 64;
2707 }
2708 } else {
2709 /* newer IGPs */
2710 rdev->mc.vram_width = 128;
2711 }
2712}
2713
Dave Airlie2a0f8912009-07-11 04:44:47 +10002714static u32 r100_get_accessible_vram(struct radeon_device *rdev)
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002715{
Dave Airlie2a0f8912009-07-11 04:44:47 +10002716 u32 aper_size;
2717 u8 byte;
2718
2719 aper_size = RREG32(RADEON_CONFIG_APER_SIZE);
2720
2721 /* Set HDP_APER_CNTL only on cards that are known not to be broken,
2722 * that is has the 2nd generation multifunction PCI interface
2723 */
2724 if (rdev->family == CHIP_RV280 ||
2725 rdev->family >= CHIP_RV350) {
2726 WREG32_P(RADEON_HOST_PATH_CNTL, RADEON_HDP_APER_CNTL,
2727 ~RADEON_HDP_APER_CNTL);
2728 DRM_INFO("Generation 2 PCI interface, using max accessible memory\n");
2729 return aper_size * 2;
2730 }
2731
2732 /* Older cards have all sorts of funny issues to deal with. First
2733 * check if it's a multifunction card by reading the PCI config
2734 * header type... Limit those to one aperture size
2735 */
2736 pci_read_config_byte(rdev->pdev, 0xe, &byte);
2737 if (byte & 0x80) {
2738 DRM_INFO("Generation 1 PCI interface in multifunction mode\n");
2739 DRM_INFO("Limiting VRAM to one aperture\n");
2740 return aper_size;
2741 }
2742
2743 /* Single function older card. We read HDP_APER_CNTL to see how the BIOS
2744 * have set it up. We don't write this as it's broken on some ASICs but
2745 * we expect the BIOS to have done the right thing (might be too optimistic...)
2746 */
2747 if (RREG32(RADEON_HOST_PATH_CNTL) & RADEON_HDP_APER_CNTL)
2748 return aper_size * 2;
2749 return aper_size;
2750}
2751
2752void r100_vram_init_sizes(struct radeon_device *rdev)
2753{
2754 u64 config_aper_size;
Dave Airlie2a0f8912009-07-11 04:44:47 +10002755
Jerome Glissed594e462010-02-17 21:54:29 +00002756 /* work out accessible VRAM */
Jordan Crouse01d73a62010-05-27 13:40:24 -06002757 rdev->mc.aper_base = pci_resource_start(rdev->pdev, 0);
2758 rdev->mc.aper_size = pci_resource_len(rdev->pdev, 0);
Jerome Glisse51e5fcd2010-02-19 14:33:54 +00002759 rdev->mc.visible_vram_size = r100_get_accessible_vram(rdev);
2760 /* FIXME we don't use the second aperture yet when we could use it */
2761 if (rdev->mc.visible_vram_size > rdev->mc.aper_size)
2762 rdev->mc.visible_vram_size = rdev->mc.aper_size;
Dave Airlie2a0f8912009-07-11 04:44:47 +10002763 config_aper_size = RREG32(RADEON_CONFIG_APER_SIZE);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002764 if (rdev->flags & RADEON_IS_IGP) {
2765 uint32_t tom;
2766 /* read NB_TOM to get the amount of ram stolen for the GPU */
2767 tom = RREG32(RADEON_NB_TOM);
Dave Airlie7a50f012009-07-21 20:39:30 +10002768 rdev->mc.real_vram_size = (((tom >> 16) - (tom & 0xffff) + 1) << 16);
Dave Airlie7a50f012009-07-21 20:39:30 +10002769 WREG32(RADEON_CONFIG_MEMSIZE, rdev->mc.real_vram_size);
2770 rdev->mc.mc_vram_size = rdev->mc.real_vram_size;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002771 } else {
Dave Airlie7a50f012009-07-21 20:39:30 +10002772 rdev->mc.real_vram_size = RREG32(RADEON_CONFIG_MEMSIZE);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002773 /* Some production boards of m6 will report 0
2774 * if it's 8 MB
2775 */
Dave Airlie7a50f012009-07-21 20:39:30 +10002776 if (rdev->mc.real_vram_size == 0) {
2777 rdev->mc.real_vram_size = 8192 * 1024;
2778 WREG32(RADEON_CONFIG_MEMSIZE, rdev->mc.real_vram_size);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002779 }
Jerome Glissed594e462010-02-17 21:54:29 +00002780 /* Fix for RN50, M6, M7 with 8/16/32(??) MBs of VRAM -
2781 * Novell bug 204882 + along with lots of ubuntu ones
2782 */
Alex Deucherb7d8cce2010-10-25 19:44:00 -04002783 if (rdev->mc.aper_size > config_aper_size)
2784 config_aper_size = rdev->mc.aper_size;
2785
Dave Airlie7a50f012009-07-21 20:39:30 +10002786 if (config_aper_size > rdev->mc.real_vram_size)
2787 rdev->mc.mc_vram_size = config_aper_size;
2788 else
2789 rdev->mc.mc_vram_size = rdev->mc.real_vram_size;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002790 }
Dave Airlie2a0f8912009-07-11 04:44:47 +10002791}
2792
Dave Airlie28d52042009-09-21 14:33:58 +10002793void r100_vga_set_state(struct radeon_device *rdev, bool state)
2794{
2795 uint32_t temp;
2796
2797 temp = RREG32(RADEON_CONFIG_CNTL);
2798 if (state == false) {
Alex Deucherd75ee3b2011-01-24 23:24:59 -05002799 temp &= ~RADEON_CFG_VGA_RAM_EN;
2800 temp |= RADEON_CFG_VGA_IO_DIS;
Dave Airlie28d52042009-09-21 14:33:58 +10002801 } else {
Alex Deucherd75ee3b2011-01-24 23:24:59 -05002802 temp &= ~RADEON_CFG_VGA_IO_DIS;
Dave Airlie28d52042009-09-21 14:33:58 +10002803 }
2804 WREG32(RADEON_CONFIG_CNTL, temp);
2805}
2806
Lauri Kasanen1109ca02012-08-31 13:43:50 -04002807static void r100_mc_init(struct radeon_device *rdev)
Dave Airlie2a0f8912009-07-11 04:44:47 +10002808{
Jerome Glissed594e462010-02-17 21:54:29 +00002809 u64 base;
Dave Airlie2a0f8912009-07-11 04:44:47 +10002810
Jerome Glissed594e462010-02-17 21:54:29 +00002811 r100_vram_get_type(rdev);
Dave Airlie2a0f8912009-07-11 04:44:47 +10002812 r100_vram_init_sizes(rdev);
Jerome Glissed594e462010-02-17 21:54:29 +00002813 base = rdev->mc.aper_base;
2814 if (rdev->flags & RADEON_IS_IGP)
2815 base = (RREG32(RADEON_NB_TOM) & 0xffff) << 16;
2816 radeon_vram_location(rdev, &rdev->mc, base);
Alex Deucher8d369bb2010-07-15 10:51:10 -04002817 rdev->mc.gtt_base_align = 0;
Jerome Glissed594e462010-02-17 21:54:29 +00002818 if (!(rdev->flags & RADEON_IS_AGP))
2819 radeon_gtt_location(rdev, &rdev->mc);
Alex Deucherf47299c2010-03-16 20:54:38 -04002820 radeon_update_bandwidth_info(rdev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002821}
2822
2823
2824/*
2825 * Indirect registers accessor
2826 */
2827void r100_pll_errata_after_index(struct radeon_device *rdev)
2828{
Alex Deucher4ce91982010-06-30 12:13:55 -04002829 if (rdev->pll_errata & CHIP_ERRATA_PLL_DUMMYREADS) {
2830 (void)RREG32(RADEON_CLOCK_CNTL_DATA);
2831 (void)RREG32(RADEON_CRTC_GEN_CNTL);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002832 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002833}
2834
2835static void r100_pll_errata_after_data(struct radeon_device *rdev)
2836{
2837 /* This workarounds is necessary on RV100, RS100 and RS200 chips
2838 * or the chip could hang on a subsequent access
2839 */
2840 if (rdev->pll_errata & CHIP_ERRATA_PLL_DELAY) {
Arnd Bergmann4de833c2012-04-05 12:58:22 -06002841 mdelay(5);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002842 }
2843
2844 /* This function is required to workaround a hardware bug in some (all?)
2845 * revisions of the R300. This workaround should be called after every
2846 * CLOCK_CNTL_INDEX register access. If not, register reads afterward
2847 * may not be correct.
2848 */
2849 if (rdev->pll_errata & CHIP_ERRATA_R300_CG) {
2850 uint32_t save, tmp;
2851
2852 save = RREG32(RADEON_CLOCK_CNTL_INDEX);
2853 tmp = save & ~(0x3f | RADEON_PLL_WR_EN);
2854 WREG32(RADEON_CLOCK_CNTL_INDEX, tmp);
2855 tmp = RREG32(RADEON_CLOCK_CNTL_DATA);
2856 WREG32(RADEON_CLOCK_CNTL_INDEX, save);
2857 }
2858}
2859
2860uint32_t r100_pll_rreg(struct radeon_device *rdev, uint32_t reg)
2861{
Alex Deucher0a5b7b02013-09-03 19:00:09 -04002862 unsigned long flags;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002863 uint32_t data;
2864
Alex Deucher0a5b7b02013-09-03 19:00:09 -04002865 spin_lock_irqsave(&rdev->pll_idx_lock, flags);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002866 WREG8(RADEON_CLOCK_CNTL_INDEX, reg & 0x3f);
2867 r100_pll_errata_after_index(rdev);
2868 data = RREG32(RADEON_CLOCK_CNTL_DATA);
2869 r100_pll_errata_after_data(rdev);
Alex Deucher0a5b7b02013-09-03 19:00:09 -04002870 spin_unlock_irqrestore(&rdev->pll_idx_lock, flags);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002871 return data;
2872}
2873
2874void r100_pll_wreg(struct radeon_device *rdev, uint32_t reg, uint32_t v)
2875{
Alex Deucher0a5b7b02013-09-03 19:00:09 -04002876 unsigned long flags;
2877
2878 spin_lock_irqsave(&rdev->pll_idx_lock, flags);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002879 WREG8(RADEON_CLOCK_CNTL_INDEX, ((reg & 0x3f) | RADEON_PLL_WR_EN));
2880 r100_pll_errata_after_index(rdev);
2881 WREG32(RADEON_CLOCK_CNTL_DATA, v);
2882 r100_pll_errata_after_data(rdev);
Alex Deucher0a5b7b02013-09-03 19:00:09 -04002883 spin_unlock_irqrestore(&rdev->pll_idx_lock, flags);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002884}
2885
Lauri Kasanen1109ca02012-08-31 13:43:50 -04002886static void r100_set_safe_registers(struct radeon_device *rdev)
Jerome Glisse068a1172009-06-17 13:28:30 +02002887{
Dave Airlie551ebd82009-09-01 15:25:57 +10002888 if (ASIC_IS_RN50(rdev)) {
2889 rdev->config.r100.reg_safe_bm = rn50_reg_safe_bm;
2890 rdev->config.r100.reg_safe_bm_size = ARRAY_SIZE(rn50_reg_safe_bm);
2891 } else if (rdev->family < CHIP_R200) {
2892 rdev->config.r100.reg_safe_bm = r100_reg_safe_bm;
2893 rdev->config.r100.reg_safe_bm_size = ARRAY_SIZE(r100_reg_safe_bm);
2894 } else {
Jerome Glissed4550902009-10-01 10:12:06 +02002895 r200_set_safe_registers(rdev);
Dave Airlie551ebd82009-09-01 15:25:57 +10002896 }
Jerome Glisse068a1172009-06-17 13:28:30 +02002897}
2898
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002899/*
2900 * Debugfs info
2901 */
2902#if defined(CONFIG_DEBUG_FS)
2903static int r100_debugfs_rbbm_info(struct seq_file *m, void *data)
2904{
2905 struct drm_info_node *node = (struct drm_info_node *) m->private;
2906 struct drm_device *dev = node->minor->dev;
2907 struct radeon_device *rdev = dev->dev_private;
2908 uint32_t reg, value;
2909 unsigned i;
2910
2911 seq_printf(m, "RBBM_STATUS 0x%08x\n", RREG32(RADEON_RBBM_STATUS));
2912 seq_printf(m, "RBBM_CMDFIFO_STAT 0x%08x\n", RREG32(0xE7C));
2913 seq_printf(m, "CP_STAT 0x%08x\n", RREG32(RADEON_CP_STAT));
2914 for (i = 0; i < 64; i++) {
2915 WREG32(RADEON_RBBM_CMDFIFO_ADDR, i | 0x100);
2916 reg = (RREG32(RADEON_RBBM_CMDFIFO_DATA) - 1) >> 2;
2917 WREG32(RADEON_RBBM_CMDFIFO_ADDR, i);
2918 value = RREG32(RADEON_RBBM_CMDFIFO_DATA);
2919 seq_printf(m, "[0x%03X] 0x%04X=0x%08X\n", i, reg, value);
2920 }
2921 return 0;
2922}
2923
2924static int r100_debugfs_cp_ring_info(struct seq_file *m, void *data)
2925{
2926 struct drm_info_node *node = (struct drm_info_node *) m->private;
2927 struct drm_device *dev = node->minor->dev;
2928 struct radeon_device *rdev = dev->dev_private;
Christian Könige32eb502011-10-23 12:56:27 +02002929 struct radeon_ring *ring = &rdev->ring[RADEON_RING_TYPE_GFX_INDEX];
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002930 uint32_t rdp, wdp;
2931 unsigned count, i, j;
2932
Christian Könige32eb502011-10-23 12:56:27 +02002933 radeon_ring_free_size(rdev, ring);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002934 rdp = RREG32(RADEON_CP_RB_RPTR);
2935 wdp = RREG32(RADEON_CP_RB_WPTR);
Christian Könige32eb502011-10-23 12:56:27 +02002936 count = (rdp + ring->ring_size - wdp) & ring->ptr_mask;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002937 seq_printf(m, "CP_STAT 0x%08x\n", RREG32(RADEON_CP_STAT));
2938 seq_printf(m, "CP_RB_WPTR 0x%08x\n", wdp);
2939 seq_printf(m, "CP_RB_RPTR 0x%08x\n", rdp);
Christian Könige32eb502011-10-23 12:56:27 +02002940 seq_printf(m, "%u free dwords in ring\n", ring->ring_free_dw);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002941 seq_printf(m, "%u dwords in ring\n", count);
Alex Ivanov0eb34482013-09-20 17:36:06 +04002942 if (ring->ready) {
2943 for (j = 0; j <= count; j++) {
2944 i = (rdp + j) & ring->ptr_mask;
2945 seq_printf(m, "r[%04d]=0x%08x\n", i, ring->ring[i]);
2946 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002947 }
2948 return 0;
2949}
2950
2951
2952static int r100_debugfs_cp_csq_fifo(struct seq_file *m, void *data)
2953{
2954 struct drm_info_node *node = (struct drm_info_node *) m->private;
2955 struct drm_device *dev = node->minor->dev;
2956 struct radeon_device *rdev = dev->dev_private;
2957 uint32_t csq_stat, csq2_stat, tmp;
2958 unsigned r_rptr, r_wptr, ib1_rptr, ib1_wptr, ib2_rptr, ib2_wptr;
2959 unsigned i;
2960
2961 seq_printf(m, "CP_STAT 0x%08x\n", RREG32(RADEON_CP_STAT));
2962 seq_printf(m, "CP_CSQ_MODE 0x%08x\n", RREG32(RADEON_CP_CSQ_MODE));
2963 csq_stat = RREG32(RADEON_CP_CSQ_STAT);
2964 csq2_stat = RREG32(RADEON_CP_CSQ2_STAT);
2965 r_rptr = (csq_stat >> 0) & 0x3ff;
2966 r_wptr = (csq_stat >> 10) & 0x3ff;
2967 ib1_rptr = (csq_stat >> 20) & 0x3ff;
2968 ib1_wptr = (csq2_stat >> 0) & 0x3ff;
2969 ib2_rptr = (csq2_stat >> 10) & 0x3ff;
2970 ib2_wptr = (csq2_stat >> 20) & 0x3ff;
2971 seq_printf(m, "CP_CSQ_STAT 0x%08x\n", csq_stat);
2972 seq_printf(m, "CP_CSQ2_STAT 0x%08x\n", csq2_stat);
2973 seq_printf(m, "Ring rptr %u\n", r_rptr);
2974 seq_printf(m, "Ring wptr %u\n", r_wptr);
2975 seq_printf(m, "Indirect1 rptr %u\n", ib1_rptr);
2976 seq_printf(m, "Indirect1 wptr %u\n", ib1_wptr);
2977 seq_printf(m, "Indirect2 rptr %u\n", ib2_rptr);
2978 seq_printf(m, "Indirect2 wptr %u\n", ib2_wptr);
2979 /* FIXME: 0, 128, 640 depends on fifo setup see cp_init_kms
2980 * 128 = indirect1_start * 8 & 640 = indirect2_start * 8 */
2981 seq_printf(m, "Ring fifo:\n");
2982 for (i = 0; i < 256; i++) {
2983 WREG32(RADEON_CP_CSQ_ADDR, i << 2);
2984 tmp = RREG32(RADEON_CP_CSQ_DATA);
2985 seq_printf(m, "rfifo[%04d]=0x%08X\n", i, tmp);
2986 }
2987 seq_printf(m, "Indirect1 fifo:\n");
2988 for (i = 256; i <= 512; i++) {
2989 WREG32(RADEON_CP_CSQ_ADDR, i << 2);
2990 tmp = RREG32(RADEON_CP_CSQ_DATA);
2991 seq_printf(m, "ib1fifo[%04d]=0x%08X\n", i, tmp);
2992 }
2993 seq_printf(m, "Indirect2 fifo:\n");
2994 for (i = 640; i < ib1_wptr; i++) {
2995 WREG32(RADEON_CP_CSQ_ADDR, i << 2);
2996 tmp = RREG32(RADEON_CP_CSQ_DATA);
2997 seq_printf(m, "ib2fifo[%04d]=0x%08X\n", i, tmp);
2998 }
2999 return 0;
3000}
3001
3002static int r100_debugfs_mc_info(struct seq_file *m, void *data)
3003{
3004 struct drm_info_node *node = (struct drm_info_node *) m->private;
3005 struct drm_device *dev = node->minor->dev;
3006 struct radeon_device *rdev = dev->dev_private;
3007 uint32_t tmp;
3008
3009 tmp = RREG32(RADEON_CONFIG_MEMSIZE);
3010 seq_printf(m, "CONFIG_MEMSIZE 0x%08x\n", tmp);
3011 tmp = RREG32(RADEON_MC_FB_LOCATION);
3012 seq_printf(m, "MC_FB_LOCATION 0x%08x\n", tmp);
3013 tmp = RREG32(RADEON_BUS_CNTL);
3014 seq_printf(m, "BUS_CNTL 0x%08x\n", tmp);
3015 tmp = RREG32(RADEON_MC_AGP_LOCATION);
3016 seq_printf(m, "MC_AGP_LOCATION 0x%08x\n", tmp);
3017 tmp = RREG32(RADEON_AGP_BASE);
3018 seq_printf(m, "AGP_BASE 0x%08x\n", tmp);
3019 tmp = RREG32(RADEON_HOST_PATH_CNTL);
3020 seq_printf(m, "HOST_PATH_CNTL 0x%08x\n", tmp);
3021 tmp = RREG32(0x01D0);
3022 seq_printf(m, "AIC_CTRL 0x%08x\n", tmp);
3023 tmp = RREG32(RADEON_AIC_LO_ADDR);
3024 seq_printf(m, "AIC_LO_ADDR 0x%08x\n", tmp);
3025 tmp = RREG32(RADEON_AIC_HI_ADDR);
3026 seq_printf(m, "AIC_HI_ADDR 0x%08x\n", tmp);
3027 tmp = RREG32(0x01E4);
3028 seq_printf(m, "AIC_TLB_ADDR 0x%08x\n", tmp);
3029 return 0;
3030}
3031
3032static struct drm_info_list r100_debugfs_rbbm_list[] = {
3033 {"r100_rbbm_info", r100_debugfs_rbbm_info, 0, NULL},
3034};
3035
3036static struct drm_info_list r100_debugfs_cp_list[] = {
3037 {"r100_cp_ring_info", r100_debugfs_cp_ring_info, 0, NULL},
3038 {"r100_cp_csq_fifo", r100_debugfs_cp_csq_fifo, 0, NULL},
3039};
3040
3041static struct drm_info_list r100_debugfs_mc_info_list[] = {
3042 {"r100_mc_info", r100_debugfs_mc_info, 0, NULL},
3043};
3044#endif
3045
3046int r100_debugfs_rbbm_init(struct radeon_device *rdev)
3047{
3048#if defined(CONFIG_DEBUG_FS)
3049 return radeon_debugfs_add_files(rdev, r100_debugfs_rbbm_list, 1);
3050#else
3051 return 0;
3052#endif
3053}
3054
3055int r100_debugfs_cp_init(struct radeon_device *rdev)
3056{
3057#if defined(CONFIG_DEBUG_FS)
3058 return radeon_debugfs_add_files(rdev, r100_debugfs_cp_list, 2);
3059#else
3060 return 0;
3061#endif
3062}
3063
3064int r100_debugfs_mc_info_init(struct radeon_device *rdev)
3065{
3066#if defined(CONFIG_DEBUG_FS)
3067 return radeon_debugfs_add_files(rdev, r100_debugfs_mc_info_list, 1);
3068#else
3069 return 0;
3070#endif
3071}
Dave Airliee024e112009-06-24 09:48:08 +10003072
3073int r100_set_surface_reg(struct radeon_device *rdev, int reg,
3074 uint32_t tiling_flags, uint32_t pitch,
3075 uint32_t offset, uint32_t obj_size)
3076{
3077 int surf_index = reg * 16;
3078 int flags = 0;
3079
Dave Airliee024e112009-06-24 09:48:08 +10003080 if (rdev->family <= CHIP_RS200) {
3081 if ((tiling_flags & (RADEON_TILING_MACRO|RADEON_TILING_MICRO))
3082 == (RADEON_TILING_MACRO|RADEON_TILING_MICRO))
3083 flags |= RADEON_SURF_TILE_COLOR_BOTH;
3084 if (tiling_flags & RADEON_TILING_MACRO)
3085 flags |= RADEON_SURF_TILE_COLOR_MACRO;
Alex Deucher67d5ced2013-07-05 10:05:49 -04003086 /* setting pitch to 0 disables tiling */
3087 if ((tiling_flags & (RADEON_TILING_MACRO|RADEON_TILING_MICRO))
3088 == 0)
3089 pitch = 0;
Dave Airliee024e112009-06-24 09:48:08 +10003090 } else if (rdev->family <= CHIP_RV280) {
3091 if (tiling_flags & (RADEON_TILING_MACRO))
3092 flags |= R200_SURF_TILE_COLOR_MACRO;
3093 if (tiling_flags & RADEON_TILING_MICRO)
3094 flags |= R200_SURF_TILE_COLOR_MICRO;
3095 } else {
3096 if (tiling_flags & RADEON_TILING_MACRO)
3097 flags |= R300_SURF_TILE_MACRO;
3098 if (tiling_flags & RADEON_TILING_MICRO)
3099 flags |= R300_SURF_TILE_MICRO;
3100 }
3101
Michel Dänzerc88f9f02009-09-15 17:09:30 +02003102 if (tiling_flags & RADEON_TILING_SWAP_16BIT)
3103 flags |= RADEON_SURF_AP0_SWP_16BPP | RADEON_SURF_AP1_SWP_16BPP;
3104 if (tiling_flags & RADEON_TILING_SWAP_32BIT)
3105 flags |= RADEON_SURF_AP0_SWP_32BPP | RADEON_SURF_AP1_SWP_32BPP;
3106
Dave Airlief5c5f042010-06-11 14:40:16 +10003107 /* r100/r200 divide by 16 */
3108 if (rdev->family < CHIP_R300)
3109 flags |= pitch / 16;
3110 else
3111 flags |= pitch / 8;
3112
3113
Dave Airlied9fdaaf2010-08-02 10:42:55 +10003114 DRM_DEBUG_KMS("writing surface %d %d %x %x\n", reg, flags, offset, offset+obj_size-1);
Dave Airliee024e112009-06-24 09:48:08 +10003115 WREG32(RADEON_SURFACE0_INFO + surf_index, flags);
3116 WREG32(RADEON_SURFACE0_LOWER_BOUND + surf_index, offset);
3117 WREG32(RADEON_SURFACE0_UPPER_BOUND + surf_index, offset + obj_size - 1);
3118 return 0;
3119}
3120
3121void r100_clear_surface_reg(struct radeon_device *rdev, int reg)
3122{
3123 int surf_index = reg * 16;
3124 WREG32(RADEON_SURFACE0_INFO + surf_index, 0);
3125}
Jerome Glissec93bb852009-07-13 21:04:08 +02003126
3127void r100_bandwidth_update(struct radeon_device *rdev)
3128{
3129 fixed20_12 trcd_ff, trp_ff, tras_ff, trbs_ff, tcas_ff;
3130 fixed20_12 sclk_ff, mclk_ff, sclk_eff_ff, sclk_delay_ff;
3131 fixed20_12 peak_disp_bw, mem_bw, pix_clk, pix_clk2, temp_ff, crit_point_ff;
3132 uint32_t temp, data, mem_trcd, mem_trp, mem_tras;
3133 fixed20_12 memtcas_ff[8] = {
Ben Skeggs68adac52010-04-28 11:46:42 +10003134 dfixed_init(1),
3135 dfixed_init(2),
3136 dfixed_init(3),
3137 dfixed_init(0),
3138 dfixed_init_half(1),
3139 dfixed_init_half(2),
3140 dfixed_init(0),
Jerome Glissec93bb852009-07-13 21:04:08 +02003141 };
3142 fixed20_12 memtcas_rs480_ff[8] = {
Ben Skeggs68adac52010-04-28 11:46:42 +10003143 dfixed_init(0),
3144 dfixed_init(1),
3145 dfixed_init(2),
3146 dfixed_init(3),
3147 dfixed_init(0),
3148 dfixed_init_half(1),
3149 dfixed_init_half(2),
3150 dfixed_init_half(3),
Jerome Glissec93bb852009-07-13 21:04:08 +02003151 };
3152 fixed20_12 memtcas2_ff[8] = {
Ben Skeggs68adac52010-04-28 11:46:42 +10003153 dfixed_init(0),
3154 dfixed_init(1),
3155 dfixed_init(2),
3156 dfixed_init(3),
3157 dfixed_init(4),
3158 dfixed_init(5),
3159 dfixed_init(6),
3160 dfixed_init(7),
Jerome Glissec93bb852009-07-13 21:04:08 +02003161 };
3162 fixed20_12 memtrbs[8] = {
Ben Skeggs68adac52010-04-28 11:46:42 +10003163 dfixed_init(1),
3164 dfixed_init_half(1),
3165 dfixed_init(2),
3166 dfixed_init_half(2),
3167 dfixed_init(3),
3168 dfixed_init_half(3),
3169 dfixed_init(4),
3170 dfixed_init_half(4)
Jerome Glissec93bb852009-07-13 21:04:08 +02003171 };
3172 fixed20_12 memtrbs_r4xx[8] = {
Ben Skeggs68adac52010-04-28 11:46:42 +10003173 dfixed_init(4),
3174 dfixed_init(5),
3175 dfixed_init(6),
3176 dfixed_init(7),
3177 dfixed_init(8),
3178 dfixed_init(9),
3179 dfixed_init(10),
3180 dfixed_init(11)
Jerome Glissec93bb852009-07-13 21:04:08 +02003181 };
3182 fixed20_12 min_mem_eff;
3183 fixed20_12 mc_latency_sclk, mc_latency_mclk, k1;
3184 fixed20_12 cur_latency_mclk, cur_latency_sclk;
3185 fixed20_12 disp_latency, disp_latency_overhead, disp_drain_rate,
3186 disp_drain_rate2, read_return_rate;
3187 fixed20_12 time_disp1_drop_priority;
3188 int c;
3189 int cur_size = 16; /* in octawords */
3190 int critical_point = 0, critical_point2;
3191/* uint32_t read_return_rate, time_disp1_drop_priority; */
3192 int stop_req, max_stop_req;
3193 struct drm_display_mode *mode1 = NULL;
3194 struct drm_display_mode *mode2 = NULL;
3195 uint32_t pixel_bytes1 = 0;
3196 uint32_t pixel_bytes2 = 0;
3197
Alex Deucherf46c0122010-03-31 00:33:27 -04003198 radeon_update_display_priority(rdev);
3199
Jerome Glissec93bb852009-07-13 21:04:08 +02003200 if (rdev->mode_info.crtcs[0]->base.enabled) {
3201 mode1 = &rdev->mode_info.crtcs[0]->base.mode;
Matt Roperf4510a22014-04-01 15:22:40 -07003202 pixel_bytes1 = rdev->mode_info.crtcs[0]->base.primary->fb->bits_per_pixel / 8;
Jerome Glissec93bb852009-07-13 21:04:08 +02003203 }
Dave Airliedfee5612009-10-02 09:19:09 +10003204 if (!(rdev->flags & RADEON_SINGLE_CRTC)) {
3205 if (rdev->mode_info.crtcs[1]->base.enabled) {
3206 mode2 = &rdev->mode_info.crtcs[1]->base.mode;
Matt Roperf4510a22014-04-01 15:22:40 -07003207 pixel_bytes2 = rdev->mode_info.crtcs[1]->base.primary->fb->bits_per_pixel / 8;
Dave Airliedfee5612009-10-02 09:19:09 +10003208 }
Jerome Glissec93bb852009-07-13 21:04:08 +02003209 }
3210
Ben Skeggs68adac52010-04-28 11:46:42 +10003211 min_mem_eff.full = dfixed_const_8(0);
Jerome Glissec93bb852009-07-13 21:04:08 +02003212 /* get modes */
3213 if ((rdev->disp_priority == 2) && ASIC_IS_R300(rdev)) {
3214 uint32_t mc_init_misc_lat_timer = RREG32(R300_MC_INIT_MISC_LAT_TIMER);
3215 mc_init_misc_lat_timer &= ~(R300_MC_DISP1R_INIT_LAT_MASK << R300_MC_DISP1R_INIT_LAT_SHIFT);
3216 mc_init_misc_lat_timer &= ~(R300_MC_DISP0R_INIT_LAT_MASK << R300_MC_DISP0R_INIT_LAT_SHIFT);
3217 /* check crtc enables */
3218 if (mode2)
3219 mc_init_misc_lat_timer |= (1 << R300_MC_DISP1R_INIT_LAT_SHIFT);
3220 if (mode1)
3221 mc_init_misc_lat_timer |= (1 << R300_MC_DISP0R_INIT_LAT_SHIFT);
3222 WREG32(R300_MC_INIT_MISC_LAT_TIMER, mc_init_misc_lat_timer);
3223 }
3224
3225 /*
3226 * determine is there is enough bw for current mode
3227 */
Alex Deucherf47299c2010-03-16 20:54:38 -04003228 sclk_ff = rdev->pm.sclk;
3229 mclk_ff = rdev->pm.mclk;
Jerome Glissec93bb852009-07-13 21:04:08 +02003230
3231 temp = (rdev->mc.vram_width / 8) * (rdev->mc.vram_is_ddr ? 2 : 1);
Ben Skeggs68adac52010-04-28 11:46:42 +10003232 temp_ff.full = dfixed_const(temp);
3233 mem_bw.full = dfixed_mul(mclk_ff, temp_ff);
Jerome Glissec93bb852009-07-13 21:04:08 +02003234
3235 pix_clk.full = 0;
3236 pix_clk2.full = 0;
3237 peak_disp_bw.full = 0;
3238 if (mode1) {
Ben Skeggs68adac52010-04-28 11:46:42 +10003239 temp_ff.full = dfixed_const(1000);
3240 pix_clk.full = dfixed_const(mode1->clock); /* convert to fixed point */
3241 pix_clk.full = dfixed_div(pix_clk, temp_ff);
3242 temp_ff.full = dfixed_const(pixel_bytes1);
3243 peak_disp_bw.full += dfixed_mul(pix_clk, temp_ff);
Jerome Glissec93bb852009-07-13 21:04:08 +02003244 }
3245 if (mode2) {
Ben Skeggs68adac52010-04-28 11:46:42 +10003246 temp_ff.full = dfixed_const(1000);
3247 pix_clk2.full = dfixed_const(mode2->clock); /* convert to fixed point */
3248 pix_clk2.full = dfixed_div(pix_clk2, temp_ff);
3249 temp_ff.full = dfixed_const(pixel_bytes2);
3250 peak_disp_bw.full += dfixed_mul(pix_clk2, temp_ff);
Jerome Glissec93bb852009-07-13 21:04:08 +02003251 }
3252
Ben Skeggs68adac52010-04-28 11:46:42 +10003253 mem_bw.full = dfixed_mul(mem_bw, min_mem_eff);
Jerome Glissec93bb852009-07-13 21:04:08 +02003254 if (peak_disp_bw.full >= mem_bw.full) {
3255 DRM_ERROR("You may not have enough display bandwidth for current mode\n"
3256 "If you have flickering problem, try to lower resolution, refresh rate, or color depth\n");
3257 }
3258
3259 /* Get values from the EXT_MEM_CNTL register...converting its contents. */
3260 temp = RREG32(RADEON_MEM_TIMING_CNTL);
3261 if ((rdev->family == CHIP_RV100) || (rdev->flags & RADEON_IS_IGP)) { /* RV100, M6, IGPs */
3262 mem_trcd = ((temp >> 2) & 0x3) + 1;
3263 mem_trp = ((temp & 0x3)) + 1;
3264 mem_tras = ((temp & 0x70) >> 4) + 1;
3265 } else if (rdev->family == CHIP_R300 ||
3266 rdev->family == CHIP_R350) { /* r300, r350 */
3267 mem_trcd = (temp & 0x7) + 1;
3268 mem_trp = ((temp >> 8) & 0x7) + 1;
3269 mem_tras = ((temp >> 11) & 0xf) + 4;
3270 } else if (rdev->family == CHIP_RV350 ||
3271 rdev->family <= CHIP_RV380) {
3272 /* rv3x0 */
3273 mem_trcd = (temp & 0x7) + 3;
3274 mem_trp = ((temp >> 8) & 0x7) + 3;
3275 mem_tras = ((temp >> 11) & 0xf) + 6;
3276 } else if (rdev->family == CHIP_R420 ||
3277 rdev->family == CHIP_R423 ||
3278 rdev->family == CHIP_RV410) {
3279 /* r4xx */
3280 mem_trcd = (temp & 0xf) + 3;
3281 if (mem_trcd > 15)
3282 mem_trcd = 15;
3283 mem_trp = ((temp >> 8) & 0xf) + 3;
3284 if (mem_trp > 15)
3285 mem_trp = 15;
3286 mem_tras = ((temp >> 12) & 0x1f) + 6;
3287 if (mem_tras > 31)
3288 mem_tras = 31;
3289 } else { /* RV200, R200 */
3290 mem_trcd = (temp & 0x7) + 1;
3291 mem_trp = ((temp >> 8) & 0x7) + 1;
3292 mem_tras = ((temp >> 12) & 0xf) + 4;
3293 }
3294 /* convert to FF */
Ben Skeggs68adac52010-04-28 11:46:42 +10003295 trcd_ff.full = dfixed_const(mem_trcd);
3296 trp_ff.full = dfixed_const(mem_trp);
3297 tras_ff.full = dfixed_const(mem_tras);
Jerome Glissec93bb852009-07-13 21:04:08 +02003298
3299 /* Get values from the MEM_SDRAM_MODE_REG register...converting its */
3300 temp = RREG32(RADEON_MEM_SDRAM_MODE_REG);
3301 data = (temp & (7 << 20)) >> 20;
3302 if ((rdev->family == CHIP_RV100) || rdev->flags & RADEON_IS_IGP) {
3303 if (rdev->family == CHIP_RS480) /* don't think rs400 */
3304 tcas_ff = memtcas_rs480_ff[data];
3305 else
3306 tcas_ff = memtcas_ff[data];
3307 } else
3308 tcas_ff = memtcas2_ff[data];
3309
3310 if (rdev->family == CHIP_RS400 ||
3311 rdev->family == CHIP_RS480) {
3312 /* extra cas latency stored in bits 23-25 0-4 clocks */
3313 data = (temp >> 23) & 0x7;
3314 if (data < 5)
Ben Skeggs68adac52010-04-28 11:46:42 +10003315 tcas_ff.full += dfixed_const(data);
Jerome Glissec93bb852009-07-13 21:04:08 +02003316 }
3317
3318 if (ASIC_IS_R300(rdev) && !(rdev->flags & RADEON_IS_IGP)) {
3319 /* on the R300, Tcas is included in Trbs.
3320 */
3321 temp = RREG32(RADEON_MEM_CNTL);
3322 data = (R300_MEM_NUM_CHANNELS_MASK & temp);
3323 if (data == 1) {
3324 if (R300_MEM_USE_CD_CH_ONLY & temp) {
3325 temp = RREG32(R300_MC_IND_INDEX);
3326 temp &= ~R300_MC_IND_ADDR_MASK;
3327 temp |= R300_MC_READ_CNTL_CD_mcind;
3328 WREG32(R300_MC_IND_INDEX, temp);
3329 temp = RREG32(R300_MC_IND_DATA);
3330 data = (R300_MEM_RBS_POSITION_C_MASK & temp);
3331 } else {
3332 temp = RREG32(R300_MC_READ_CNTL_AB);
3333 data = (R300_MEM_RBS_POSITION_A_MASK & temp);
3334 }
3335 } else {
3336 temp = RREG32(R300_MC_READ_CNTL_AB);
3337 data = (R300_MEM_RBS_POSITION_A_MASK & temp);
3338 }
3339 if (rdev->family == CHIP_RV410 ||
3340 rdev->family == CHIP_R420 ||
3341 rdev->family == CHIP_R423)
3342 trbs_ff = memtrbs_r4xx[data];
3343 else
3344 trbs_ff = memtrbs[data];
3345 tcas_ff.full += trbs_ff.full;
3346 }
3347
3348 sclk_eff_ff.full = sclk_ff.full;
3349
3350 if (rdev->flags & RADEON_IS_AGP) {
3351 fixed20_12 agpmode_ff;
Ben Skeggs68adac52010-04-28 11:46:42 +10003352 agpmode_ff.full = dfixed_const(radeon_agpmode);
3353 temp_ff.full = dfixed_const_666(16);
3354 sclk_eff_ff.full -= dfixed_mul(agpmode_ff, temp_ff);
Jerome Glissec93bb852009-07-13 21:04:08 +02003355 }
3356 /* TODO PCIE lanes may affect this - agpmode == 16?? */
3357
3358 if (ASIC_IS_R300(rdev)) {
Ben Skeggs68adac52010-04-28 11:46:42 +10003359 sclk_delay_ff.full = dfixed_const(250);
Jerome Glissec93bb852009-07-13 21:04:08 +02003360 } else {
3361 if ((rdev->family == CHIP_RV100) ||
3362 rdev->flags & RADEON_IS_IGP) {
3363 if (rdev->mc.vram_is_ddr)
Ben Skeggs68adac52010-04-28 11:46:42 +10003364 sclk_delay_ff.full = dfixed_const(41);
Jerome Glissec93bb852009-07-13 21:04:08 +02003365 else
Ben Skeggs68adac52010-04-28 11:46:42 +10003366 sclk_delay_ff.full = dfixed_const(33);
Jerome Glissec93bb852009-07-13 21:04:08 +02003367 } else {
3368 if (rdev->mc.vram_width == 128)
Ben Skeggs68adac52010-04-28 11:46:42 +10003369 sclk_delay_ff.full = dfixed_const(57);
Jerome Glissec93bb852009-07-13 21:04:08 +02003370 else
Ben Skeggs68adac52010-04-28 11:46:42 +10003371 sclk_delay_ff.full = dfixed_const(41);
Jerome Glissec93bb852009-07-13 21:04:08 +02003372 }
3373 }
3374
Ben Skeggs68adac52010-04-28 11:46:42 +10003375 mc_latency_sclk.full = dfixed_div(sclk_delay_ff, sclk_eff_ff);
Jerome Glissec93bb852009-07-13 21:04:08 +02003376
3377 if (rdev->mc.vram_is_ddr) {
3378 if (rdev->mc.vram_width == 32) {
Ben Skeggs68adac52010-04-28 11:46:42 +10003379 k1.full = dfixed_const(40);
Jerome Glissec93bb852009-07-13 21:04:08 +02003380 c = 3;
3381 } else {
Ben Skeggs68adac52010-04-28 11:46:42 +10003382 k1.full = dfixed_const(20);
Jerome Glissec93bb852009-07-13 21:04:08 +02003383 c = 1;
3384 }
3385 } else {
Ben Skeggs68adac52010-04-28 11:46:42 +10003386 k1.full = dfixed_const(40);
Jerome Glissec93bb852009-07-13 21:04:08 +02003387 c = 3;
3388 }
3389
Ben Skeggs68adac52010-04-28 11:46:42 +10003390 temp_ff.full = dfixed_const(2);
3391 mc_latency_mclk.full = dfixed_mul(trcd_ff, temp_ff);
3392 temp_ff.full = dfixed_const(c);
3393 mc_latency_mclk.full += dfixed_mul(tcas_ff, temp_ff);
3394 temp_ff.full = dfixed_const(4);
3395 mc_latency_mclk.full += dfixed_mul(tras_ff, temp_ff);
3396 mc_latency_mclk.full += dfixed_mul(trp_ff, temp_ff);
Jerome Glissec93bb852009-07-13 21:04:08 +02003397 mc_latency_mclk.full += k1.full;
3398
Ben Skeggs68adac52010-04-28 11:46:42 +10003399 mc_latency_mclk.full = dfixed_div(mc_latency_mclk, mclk_ff);
3400 mc_latency_mclk.full += dfixed_div(temp_ff, sclk_eff_ff);
Jerome Glissec93bb852009-07-13 21:04:08 +02003401
3402 /*
3403 HW cursor time assuming worst case of full size colour cursor.
3404 */
Ben Skeggs68adac52010-04-28 11:46:42 +10003405 temp_ff.full = dfixed_const((2 * (cur_size - (rdev->mc.vram_is_ddr + 1))));
Jerome Glissec93bb852009-07-13 21:04:08 +02003406 temp_ff.full += trcd_ff.full;
3407 if (temp_ff.full < tras_ff.full)
3408 temp_ff.full = tras_ff.full;
Ben Skeggs68adac52010-04-28 11:46:42 +10003409 cur_latency_mclk.full = dfixed_div(temp_ff, mclk_ff);
Jerome Glissec93bb852009-07-13 21:04:08 +02003410
Ben Skeggs68adac52010-04-28 11:46:42 +10003411 temp_ff.full = dfixed_const(cur_size);
3412 cur_latency_sclk.full = dfixed_div(temp_ff, sclk_eff_ff);
Jerome Glissec93bb852009-07-13 21:04:08 +02003413 /*
3414 Find the total latency for the display data.
3415 */
Ben Skeggs68adac52010-04-28 11:46:42 +10003416 disp_latency_overhead.full = dfixed_const(8);
3417 disp_latency_overhead.full = dfixed_div(disp_latency_overhead, sclk_ff);
Jerome Glissec93bb852009-07-13 21:04:08 +02003418 mc_latency_mclk.full += disp_latency_overhead.full + cur_latency_mclk.full;
3419 mc_latency_sclk.full += disp_latency_overhead.full + cur_latency_sclk.full;
3420
3421 if (mc_latency_mclk.full > mc_latency_sclk.full)
3422 disp_latency.full = mc_latency_mclk.full;
3423 else
3424 disp_latency.full = mc_latency_sclk.full;
3425
3426 /* setup Max GRPH_STOP_REQ default value */
3427 if (ASIC_IS_RV100(rdev))
3428 max_stop_req = 0x5c;
3429 else
3430 max_stop_req = 0x7c;
3431
3432 if (mode1) {
3433 /* CRTC1
3434 Set GRPH_BUFFER_CNTL register using h/w defined optimal values.
3435 GRPH_STOP_REQ <= MIN[ 0x7C, (CRTC_H_DISP + 1) * (bit depth) / 0x10 ]
3436 */
3437 stop_req = mode1->hdisplay * pixel_bytes1 / 16;
3438
3439 if (stop_req > max_stop_req)
3440 stop_req = max_stop_req;
3441
3442 /*
3443 Find the drain rate of the display buffer.
3444 */
Ben Skeggs68adac52010-04-28 11:46:42 +10003445 temp_ff.full = dfixed_const((16/pixel_bytes1));
3446 disp_drain_rate.full = dfixed_div(pix_clk, temp_ff);
Jerome Glissec93bb852009-07-13 21:04:08 +02003447
3448 /*
3449 Find the critical point of the display buffer.
3450 */
Ben Skeggs68adac52010-04-28 11:46:42 +10003451 crit_point_ff.full = dfixed_mul(disp_drain_rate, disp_latency);
3452 crit_point_ff.full += dfixed_const_half(0);
Jerome Glissec93bb852009-07-13 21:04:08 +02003453
Ben Skeggs68adac52010-04-28 11:46:42 +10003454 critical_point = dfixed_trunc(crit_point_ff);
Jerome Glissec93bb852009-07-13 21:04:08 +02003455
3456 if (rdev->disp_priority == 2) {
3457 critical_point = 0;
3458 }
3459
3460 /*
3461 The critical point should never be above max_stop_req-4. Setting
3462 GRPH_CRITICAL_CNTL = 0 will thus force high priority all the time.
3463 */
3464 if (max_stop_req - critical_point < 4)
3465 critical_point = 0;
3466
3467 if (critical_point == 0 && mode2 && rdev->family == CHIP_R300) {
3468 /* some R300 cards have problem with this set to 0, when CRTC2 is enabled.*/
3469 critical_point = 0x10;
3470 }
3471
3472 temp = RREG32(RADEON_GRPH_BUFFER_CNTL);
3473 temp &= ~(RADEON_GRPH_STOP_REQ_MASK);
3474 temp |= (stop_req << RADEON_GRPH_STOP_REQ_SHIFT);
3475 temp &= ~(RADEON_GRPH_START_REQ_MASK);
3476 if ((rdev->family == CHIP_R350) &&
3477 (stop_req > 0x15)) {
3478 stop_req -= 0x10;
3479 }
3480 temp |= (stop_req << RADEON_GRPH_START_REQ_SHIFT);
3481 temp |= RADEON_GRPH_BUFFER_SIZE;
3482 temp &= ~(RADEON_GRPH_CRITICAL_CNTL |
3483 RADEON_GRPH_CRITICAL_AT_SOF |
3484 RADEON_GRPH_STOP_CNTL);
3485 /*
3486 Write the result into the register.
3487 */
3488 WREG32(RADEON_GRPH_BUFFER_CNTL, ((temp & ~RADEON_GRPH_CRITICAL_POINT_MASK) |
3489 (critical_point << RADEON_GRPH_CRITICAL_POINT_SHIFT)));
3490
3491#if 0
3492 if ((rdev->family == CHIP_RS400) ||
3493 (rdev->family == CHIP_RS480)) {
3494 /* attempt to program RS400 disp regs correctly ??? */
3495 temp = RREG32(RS400_DISP1_REG_CNTL);
3496 temp &= ~(RS400_DISP1_START_REQ_LEVEL_MASK |
3497 RS400_DISP1_STOP_REQ_LEVEL_MASK);
3498 WREG32(RS400_DISP1_REQ_CNTL1, (temp |
3499 (critical_point << RS400_DISP1_START_REQ_LEVEL_SHIFT) |
3500 (critical_point << RS400_DISP1_STOP_REQ_LEVEL_SHIFT)));
3501 temp = RREG32(RS400_DMIF_MEM_CNTL1);
3502 temp &= ~(RS400_DISP1_CRITICAL_POINT_START_MASK |
3503 RS400_DISP1_CRITICAL_POINT_STOP_MASK);
3504 WREG32(RS400_DMIF_MEM_CNTL1, (temp |
3505 (critical_point << RS400_DISP1_CRITICAL_POINT_START_SHIFT) |
3506 (critical_point << RS400_DISP1_CRITICAL_POINT_STOP_SHIFT)));
3507 }
3508#endif
3509
Dave Airlied9fdaaf2010-08-02 10:42:55 +10003510 DRM_DEBUG_KMS("GRPH_BUFFER_CNTL from to %x\n",
Jerome Glissec93bb852009-07-13 21:04:08 +02003511 /* (unsigned int)info->SavedReg->grph_buffer_cntl, */
3512 (unsigned int)RREG32(RADEON_GRPH_BUFFER_CNTL));
3513 }
3514
3515 if (mode2) {
3516 u32 grph2_cntl;
3517 stop_req = mode2->hdisplay * pixel_bytes2 / 16;
3518
3519 if (stop_req > max_stop_req)
3520 stop_req = max_stop_req;
3521
3522 /*
3523 Find the drain rate of the display buffer.
3524 */
Ben Skeggs68adac52010-04-28 11:46:42 +10003525 temp_ff.full = dfixed_const((16/pixel_bytes2));
3526 disp_drain_rate2.full = dfixed_div(pix_clk2, temp_ff);
Jerome Glissec93bb852009-07-13 21:04:08 +02003527
3528 grph2_cntl = RREG32(RADEON_GRPH2_BUFFER_CNTL);
3529 grph2_cntl &= ~(RADEON_GRPH_STOP_REQ_MASK);
3530 grph2_cntl |= (stop_req << RADEON_GRPH_STOP_REQ_SHIFT);
3531 grph2_cntl &= ~(RADEON_GRPH_START_REQ_MASK);
3532 if ((rdev->family == CHIP_R350) &&
3533 (stop_req > 0x15)) {
3534 stop_req -= 0x10;
3535 }
3536 grph2_cntl |= (stop_req << RADEON_GRPH_START_REQ_SHIFT);
3537 grph2_cntl |= RADEON_GRPH_BUFFER_SIZE;
3538 grph2_cntl &= ~(RADEON_GRPH_CRITICAL_CNTL |
3539 RADEON_GRPH_CRITICAL_AT_SOF |
3540 RADEON_GRPH_STOP_CNTL);
3541
3542 if ((rdev->family == CHIP_RS100) ||
3543 (rdev->family == CHIP_RS200))
3544 critical_point2 = 0;
3545 else {
3546 temp = (rdev->mc.vram_width * rdev->mc.vram_is_ddr + 1)/128;
Ben Skeggs68adac52010-04-28 11:46:42 +10003547 temp_ff.full = dfixed_const(temp);
3548 temp_ff.full = dfixed_mul(mclk_ff, temp_ff);
Jerome Glissec93bb852009-07-13 21:04:08 +02003549 if (sclk_ff.full < temp_ff.full)
3550 temp_ff.full = sclk_ff.full;
3551
3552 read_return_rate.full = temp_ff.full;
3553
3554 if (mode1) {
3555 temp_ff.full = read_return_rate.full - disp_drain_rate.full;
Ben Skeggs68adac52010-04-28 11:46:42 +10003556 time_disp1_drop_priority.full = dfixed_div(crit_point_ff, temp_ff);
Jerome Glissec93bb852009-07-13 21:04:08 +02003557 } else {
3558 time_disp1_drop_priority.full = 0;
3559 }
3560 crit_point_ff.full = disp_latency.full + time_disp1_drop_priority.full + disp_latency.full;
Ben Skeggs68adac52010-04-28 11:46:42 +10003561 crit_point_ff.full = dfixed_mul(crit_point_ff, disp_drain_rate2);
3562 crit_point_ff.full += dfixed_const_half(0);
Jerome Glissec93bb852009-07-13 21:04:08 +02003563
Ben Skeggs68adac52010-04-28 11:46:42 +10003564 critical_point2 = dfixed_trunc(crit_point_ff);
Jerome Glissec93bb852009-07-13 21:04:08 +02003565
3566 if (rdev->disp_priority == 2) {
3567 critical_point2 = 0;
3568 }
3569
3570 if (max_stop_req - critical_point2 < 4)
3571 critical_point2 = 0;
3572
3573 }
3574
3575 if (critical_point2 == 0 && rdev->family == CHIP_R300) {
3576 /* some R300 cards have problem with this set to 0 */
3577 critical_point2 = 0x10;
3578 }
3579
3580 WREG32(RADEON_GRPH2_BUFFER_CNTL, ((grph2_cntl & ~RADEON_GRPH_CRITICAL_POINT_MASK) |
3581 (critical_point2 << RADEON_GRPH_CRITICAL_POINT_SHIFT)));
3582
3583 if ((rdev->family == CHIP_RS400) ||
3584 (rdev->family == CHIP_RS480)) {
3585#if 0
3586 /* attempt to program RS400 disp2 regs correctly ??? */
3587 temp = RREG32(RS400_DISP2_REQ_CNTL1);
3588 temp &= ~(RS400_DISP2_START_REQ_LEVEL_MASK |
3589 RS400_DISP2_STOP_REQ_LEVEL_MASK);
3590 WREG32(RS400_DISP2_REQ_CNTL1, (temp |
3591 (critical_point2 << RS400_DISP1_START_REQ_LEVEL_SHIFT) |
3592 (critical_point2 << RS400_DISP1_STOP_REQ_LEVEL_SHIFT)));
3593 temp = RREG32(RS400_DISP2_REQ_CNTL2);
3594 temp &= ~(RS400_DISP2_CRITICAL_POINT_START_MASK |
3595 RS400_DISP2_CRITICAL_POINT_STOP_MASK);
3596 WREG32(RS400_DISP2_REQ_CNTL2, (temp |
3597 (critical_point2 << RS400_DISP2_CRITICAL_POINT_START_SHIFT) |
3598 (critical_point2 << RS400_DISP2_CRITICAL_POINT_STOP_SHIFT)));
3599#endif
3600 WREG32(RS400_DISP2_REQ_CNTL1, 0x105DC1CC);
3601 WREG32(RS400_DISP2_REQ_CNTL2, 0x2749D000);
3602 WREG32(RS400_DMIF_MEM_CNTL1, 0x29CA71DC);
3603 WREG32(RS400_DISP1_REQ_CNTL1, 0x28FBC3AC);
3604 }
3605
Dave Airlied9fdaaf2010-08-02 10:42:55 +10003606 DRM_DEBUG_KMS("GRPH2_BUFFER_CNTL from to %x\n",
Jerome Glissec93bb852009-07-13 21:04:08 +02003607 (unsigned int)RREG32(RADEON_GRPH2_BUFFER_CNTL));
3608 }
3609}
Dave Airlie551ebd82009-09-01 15:25:57 +10003610
Christian Könige32eb502011-10-23 12:56:27 +02003611int r100_ring_test(struct radeon_device *rdev, struct radeon_ring *ring)
Jerome Glisse3ce0a232009-09-08 10:10:24 +10003612{
3613 uint32_t scratch;
3614 uint32_t tmp = 0;
3615 unsigned i;
3616 int r;
3617
3618 r = radeon_scratch_get(rdev, &scratch);
3619 if (r) {
3620 DRM_ERROR("radeon: cp failed to get scratch reg (%d).\n", r);
3621 return r;
3622 }
3623 WREG32(scratch, 0xCAFEDEAD);
Christian Könige32eb502011-10-23 12:56:27 +02003624 r = radeon_ring_lock(rdev, ring, 2);
Jerome Glisse3ce0a232009-09-08 10:10:24 +10003625 if (r) {
3626 DRM_ERROR("radeon: cp failed to lock ring (%d).\n", r);
3627 radeon_scratch_free(rdev, scratch);
3628 return r;
3629 }
Christian Könige32eb502011-10-23 12:56:27 +02003630 radeon_ring_write(ring, PACKET0(scratch, 0));
3631 radeon_ring_write(ring, 0xDEADBEEF);
3632 radeon_ring_unlock_commit(rdev, ring);
Jerome Glisse3ce0a232009-09-08 10:10:24 +10003633 for (i = 0; i < rdev->usec_timeout; i++) {
3634 tmp = RREG32(scratch);
3635 if (tmp == 0xDEADBEEF) {
3636 break;
3637 }
3638 DRM_UDELAY(1);
3639 }
3640 if (i < rdev->usec_timeout) {
3641 DRM_INFO("ring test succeeded in %d usecs\n", i);
3642 } else {
Alex Deucher369d7ec2011-01-17 18:08:58 +00003643 DRM_ERROR("radeon: ring test failed (scratch(0x%04X)=0x%08X)\n",
Jerome Glisse3ce0a232009-09-08 10:10:24 +10003644 scratch, tmp);
3645 r = -EINVAL;
3646 }
3647 radeon_scratch_free(rdev, scratch);
3648 return r;
3649}
3650
3651void r100_ring_ib_execute(struct radeon_device *rdev, struct radeon_ib *ib)
3652{
Christian Könige32eb502011-10-23 12:56:27 +02003653 struct radeon_ring *ring = &rdev->ring[RADEON_RING_TYPE_GFX_INDEX];
Christian König7b1f2482011-09-23 15:11:23 +02003654
Alex Deucherc7eff972012-07-17 14:02:32 -04003655 if (ring->rptr_save_reg) {
3656 u32 next_rptr = ring->wptr + 2 + 3;
3657 radeon_ring_write(ring, PACKET0(ring->rptr_save_reg, 0));
3658 radeon_ring_write(ring, next_rptr);
3659 }
3660
Christian Könige32eb502011-10-23 12:56:27 +02003661 radeon_ring_write(ring, PACKET0(RADEON_CP_IB_BASE, 1));
3662 radeon_ring_write(ring, ib->gpu_addr);
3663 radeon_ring_write(ring, ib->length_dw);
Jerome Glisse3ce0a232009-09-08 10:10:24 +10003664}
3665
Alex Deucherf7128122012-02-23 17:53:45 -05003666int r100_ib_test(struct radeon_device *rdev, struct radeon_ring *ring)
Jerome Glisse3ce0a232009-09-08 10:10:24 +10003667{
Jerome Glissef2e39222012-05-09 15:35:02 +02003668 struct radeon_ib ib;
Jerome Glisse3ce0a232009-09-08 10:10:24 +10003669 uint32_t scratch;
3670 uint32_t tmp = 0;
3671 unsigned i;
3672 int r;
3673
3674 r = radeon_scratch_get(rdev, &scratch);
3675 if (r) {
3676 DRM_ERROR("radeon: failed to get scratch reg (%d).\n", r);
3677 return r;
3678 }
3679 WREG32(scratch, 0xCAFEDEAD);
Christian König4bf3dd92012-08-06 18:57:44 +02003680 r = radeon_ib_get(rdev, RADEON_RING_TYPE_GFX_INDEX, &ib, NULL, 256);
Jerome Glisse3ce0a232009-09-08 10:10:24 +10003681 if (r) {
Michel Dänzeraf026c52012-09-20 10:31:10 +02003682 DRM_ERROR("radeon: failed to get ib (%d).\n", r);
3683 goto free_scratch;
Jerome Glisse3ce0a232009-09-08 10:10:24 +10003684 }
Jerome Glissef2e39222012-05-09 15:35:02 +02003685 ib.ptr[0] = PACKET0(scratch, 0);
3686 ib.ptr[1] = 0xDEADBEEF;
3687 ib.ptr[2] = PACKET2(0);
3688 ib.ptr[3] = PACKET2(0);
3689 ib.ptr[4] = PACKET2(0);
3690 ib.ptr[5] = PACKET2(0);
3691 ib.ptr[6] = PACKET2(0);
3692 ib.ptr[7] = PACKET2(0);
3693 ib.length_dw = 8;
Christian König4ef72562012-07-13 13:06:00 +02003694 r = radeon_ib_schedule(rdev, &ib, NULL);
Jerome Glisse3ce0a232009-09-08 10:10:24 +10003695 if (r) {
Michel Dänzeraf026c52012-09-20 10:31:10 +02003696 DRM_ERROR("radeon: failed to schedule ib (%d).\n", r);
3697 goto free_ib;
Jerome Glisse3ce0a232009-09-08 10:10:24 +10003698 }
Jerome Glissef2e39222012-05-09 15:35:02 +02003699 r = radeon_fence_wait(ib.fence, false);
Jerome Glisse3ce0a232009-09-08 10:10:24 +10003700 if (r) {
Michel Dänzeraf026c52012-09-20 10:31:10 +02003701 DRM_ERROR("radeon: fence wait failed (%d).\n", r);
3702 goto free_ib;
Jerome Glisse3ce0a232009-09-08 10:10:24 +10003703 }
3704 for (i = 0; i < rdev->usec_timeout; i++) {
3705 tmp = RREG32(scratch);
3706 if (tmp == 0xDEADBEEF) {
3707 break;
3708 }
3709 DRM_UDELAY(1);
3710 }
3711 if (i < rdev->usec_timeout) {
3712 DRM_INFO("ib test succeeded in %u usecs\n", i);
3713 } else {
Paul Bolle62f288c2011-02-19 22:34:00 +01003714 DRM_ERROR("radeon: ib test failed (scratch(0x%04X)=0x%08X)\n",
Jerome Glisse3ce0a232009-09-08 10:10:24 +10003715 scratch, tmp);
3716 r = -EINVAL;
3717 }
Michel Dänzeraf026c52012-09-20 10:31:10 +02003718free_ib:
Jerome Glisse3ce0a232009-09-08 10:10:24 +10003719 radeon_ib_free(rdev, &ib);
Michel Dänzeraf026c52012-09-20 10:31:10 +02003720free_scratch:
3721 radeon_scratch_free(rdev, scratch);
Jerome Glisse3ce0a232009-09-08 10:10:24 +10003722 return r;
3723}
Jerome Glisse9f022dd2009-09-11 15:35:22 +02003724
Jerome Glisse9f022dd2009-09-11 15:35:22 +02003725void r100_mc_stop(struct radeon_device *rdev, struct r100_mc_save *save)
3726{
3727 /* Shutdown CP we shouldn't need to do that but better be safe than
3728 * sorry
3729 */
Christian Könige32eb502011-10-23 12:56:27 +02003730 rdev->ring[RADEON_RING_TYPE_GFX_INDEX].ready = false;
Jerome Glisse9f022dd2009-09-11 15:35:22 +02003731 WREG32(R_000740_CP_CSQ_CNTL, 0);
3732
3733 /* Save few CRTC registers */
Jerome Glisseca6ffc62009-10-01 10:20:52 +02003734 save->GENMO_WT = RREG8(R_0003C2_GENMO_WT);
Jerome Glisse9f022dd2009-09-11 15:35:22 +02003735 save->CRTC_EXT_CNTL = RREG32(R_000054_CRTC_EXT_CNTL);
3736 save->CRTC_GEN_CNTL = RREG32(R_000050_CRTC_GEN_CNTL);
3737 save->CUR_OFFSET = RREG32(R_000260_CUR_OFFSET);
3738 if (!(rdev->flags & RADEON_SINGLE_CRTC)) {
3739 save->CRTC2_GEN_CNTL = RREG32(R_0003F8_CRTC2_GEN_CNTL);
3740 save->CUR2_OFFSET = RREG32(R_000360_CUR2_OFFSET);
3741 }
3742
3743 /* Disable VGA aperture access */
Jerome Glisseca6ffc62009-10-01 10:20:52 +02003744 WREG8(R_0003C2_GENMO_WT, C_0003C2_VGA_RAM_EN & save->GENMO_WT);
Jerome Glisse9f022dd2009-09-11 15:35:22 +02003745 /* Disable cursor, overlay, crtc */
3746 WREG32(R_000260_CUR_OFFSET, save->CUR_OFFSET | S_000260_CUR_LOCK(1));
3747 WREG32(R_000054_CRTC_EXT_CNTL, save->CRTC_EXT_CNTL |
3748 S_000054_CRTC_DISPLAY_DIS(1));
3749 WREG32(R_000050_CRTC_GEN_CNTL,
3750 (C_000050_CRTC_CUR_EN & save->CRTC_GEN_CNTL) |
3751 S_000050_CRTC_DISP_REQ_EN_B(1));
3752 WREG32(R_000420_OV0_SCALE_CNTL,
3753 C_000420_OV0_OVERLAY_EN & RREG32(R_000420_OV0_SCALE_CNTL));
3754 WREG32(R_000260_CUR_OFFSET, C_000260_CUR_LOCK & save->CUR_OFFSET);
3755 if (!(rdev->flags & RADEON_SINGLE_CRTC)) {
3756 WREG32(R_000360_CUR2_OFFSET, save->CUR2_OFFSET |
3757 S_000360_CUR2_LOCK(1));
3758 WREG32(R_0003F8_CRTC2_GEN_CNTL,
3759 (C_0003F8_CRTC2_CUR_EN & save->CRTC2_GEN_CNTL) |
3760 S_0003F8_CRTC2_DISPLAY_DIS(1) |
3761 S_0003F8_CRTC2_DISP_REQ_EN_B(1));
3762 WREG32(R_000360_CUR2_OFFSET,
3763 C_000360_CUR2_LOCK & save->CUR2_OFFSET);
3764 }
3765}
3766
3767void r100_mc_resume(struct radeon_device *rdev, struct r100_mc_save *save)
3768{
3769 /* Update base address for crtc */
Jerome Glissed594e462010-02-17 21:54:29 +00003770 WREG32(R_00023C_DISPLAY_BASE_ADDR, rdev->mc.vram_start);
Jerome Glisse9f022dd2009-09-11 15:35:22 +02003771 if (!(rdev->flags & RADEON_SINGLE_CRTC)) {
Jerome Glissed594e462010-02-17 21:54:29 +00003772 WREG32(R_00033C_CRTC2_DISPLAY_BASE_ADDR, rdev->mc.vram_start);
Jerome Glisse9f022dd2009-09-11 15:35:22 +02003773 }
3774 /* Restore CRTC registers */
Jerome Glisseca6ffc62009-10-01 10:20:52 +02003775 WREG8(R_0003C2_GENMO_WT, save->GENMO_WT);
Jerome Glisse9f022dd2009-09-11 15:35:22 +02003776 WREG32(R_000054_CRTC_EXT_CNTL, save->CRTC_EXT_CNTL);
3777 WREG32(R_000050_CRTC_GEN_CNTL, save->CRTC_GEN_CNTL);
3778 if (!(rdev->flags & RADEON_SINGLE_CRTC)) {
3779 WREG32(R_0003F8_CRTC2_GEN_CNTL, save->CRTC2_GEN_CNTL);
3780 }
3781}
Jerome Glisseca6ffc62009-10-01 10:20:52 +02003782
3783void r100_vga_render_disable(struct radeon_device *rdev)
3784{
Jerome Glissed4550902009-10-01 10:12:06 +02003785 u32 tmp;
Jerome Glisseca6ffc62009-10-01 10:20:52 +02003786
Jerome Glissed4550902009-10-01 10:12:06 +02003787 tmp = RREG8(R_0003C2_GENMO_WT);
Jerome Glisseca6ffc62009-10-01 10:20:52 +02003788 WREG8(R_0003C2_GENMO_WT, C_0003C2_VGA_RAM_EN & tmp);
3789}
Jerome Glissed4550902009-10-01 10:12:06 +02003790
3791static void r100_debugfs(struct radeon_device *rdev)
3792{
3793 int r;
3794
3795 r = r100_debugfs_mc_info_init(rdev);
3796 if (r)
3797 dev_warn(rdev->dev, "Failed to create r100_mc debugfs file.\n");
3798}
3799
3800static void r100_mc_program(struct radeon_device *rdev)
3801{
3802 struct r100_mc_save save;
3803
3804 /* Stops all mc clients */
3805 r100_mc_stop(rdev, &save);
3806 if (rdev->flags & RADEON_IS_AGP) {
3807 WREG32(R_00014C_MC_AGP_LOCATION,
3808 S_00014C_MC_AGP_START(rdev->mc.gtt_start >> 16) |
3809 S_00014C_MC_AGP_TOP(rdev->mc.gtt_end >> 16));
3810 WREG32(R_000170_AGP_BASE, lower_32_bits(rdev->mc.agp_base));
3811 if (rdev->family > CHIP_RV200)
3812 WREG32(R_00015C_AGP_BASE_2,
3813 upper_32_bits(rdev->mc.agp_base) & 0xff);
3814 } else {
3815 WREG32(R_00014C_MC_AGP_LOCATION, 0x0FFFFFFF);
3816 WREG32(R_000170_AGP_BASE, 0);
3817 if (rdev->family > CHIP_RV200)
3818 WREG32(R_00015C_AGP_BASE_2, 0);
3819 }
3820 /* Wait for mc idle */
3821 if (r100_mc_wait_for_idle(rdev))
3822 dev_warn(rdev->dev, "Wait for MC idle timeout.\n");
3823 /* Program MC, should be a 32bits limited address space */
3824 WREG32(R_000148_MC_FB_LOCATION,
3825 S_000148_MC_FB_START(rdev->mc.vram_start >> 16) |
3826 S_000148_MC_FB_TOP(rdev->mc.vram_end >> 16));
3827 r100_mc_resume(rdev, &save);
3828}
3829
Lauri Kasanen1109ca02012-08-31 13:43:50 -04003830static void r100_clock_startup(struct radeon_device *rdev)
Jerome Glissed4550902009-10-01 10:12:06 +02003831{
3832 u32 tmp;
3833
3834 if (radeon_dynclks != -1 && radeon_dynclks)
3835 radeon_legacy_set_clock_gating(rdev, 1);
3836 /* We need to force on some of the block */
3837 tmp = RREG32_PLL(R_00000D_SCLK_CNTL);
3838 tmp |= S_00000D_FORCE_CP(1) | S_00000D_FORCE_VIP(1);
3839 if ((rdev->family == CHIP_RV250) || (rdev->family == CHIP_RV280))
3840 tmp |= S_00000D_FORCE_DISP1(1) | S_00000D_FORCE_DISP2(1);
3841 WREG32_PLL(R_00000D_SCLK_CNTL, tmp);
3842}
3843
3844static int r100_startup(struct radeon_device *rdev)
3845{
3846 int r;
3847
Alex Deucher92cde002009-12-04 10:55:12 -05003848 /* set common regs */
3849 r100_set_common_regs(rdev);
3850 /* program mc */
Jerome Glissed4550902009-10-01 10:12:06 +02003851 r100_mc_program(rdev);
3852 /* Resume clock */
3853 r100_clock_startup(rdev);
Jerome Glissed4550902009-10-01 10:12:06 +02003854 /* Initialize GART (initialize after TTM so we can allocate
3855 * memory through TTM but finalize after TTM) */
Dave Airlie17e15b02009-11-05 15:36:53 +10003856 r100_enable_bm(rdev);
Jerome Glissed4550902009-10-01 10:12:06 +02003857 if (rdev->flags & RADEON_IS_PCI) {
3858 r = r100_pci_gart_enable(rdev);
3859 if (r)
3860 return r;
3861 }
Alex Deucher724c80e2010-08-27 18:25:25 -04003862
3863 /* allocate wb buffer */
3864 r = radeon_wb_init(rdev);
3865 if (r)
3866 return r;
3867
Jerome Glisse30eb77f2011-11-20 20:45:34 +00003868 r = radeon_fence_driver_start_ring(rdev, RADEON_RING_TYPE_GFX_INDEX);
3869 if (r) {
3870 dev_err(rdev->dev, "failed initializing CP fences (%d).\n", r);
3871 return r;
3872 }
3873
Jerome Glissed4550902009-10-01 10:12:06 +02003874 /* Enable IRQ */
Adis Hamziće49f3952013-06-02 16:47:54 +02003875 if (!rdev->irq.installed) {
3876 r = radeon_irq_kms_init(rdev);
3877 if (r)
3878 return r;
3879 }
3880
Jerome Glissed4550902009-10-01 10:12:06 +02003881 r100_irq_set(rdev);
Jerome Glissecafe6602010-01-07 12:39:21 +01003882 rdev->config.r100.hdp_cntl = RREG32(RADEON_HOST_PATH_CNTL);
Jerome Glissed4550902009-10-01 10:12:06 +02003883 /* 1M ring buffer */
3884 r = r100_cp_init(rdev, 1024 * 1024);
3885 if (r) {
Paul Bolleec4f2ac2011-01-28 23:32:04 +01003886 dev_err(rdev->dev, "failed initializing CP (%d).\n", r);
Jerome Glissed4550902009-10-01 10:12:06 +02003887 return r;
3888 }
Jerome Glisseb15ba512011-11-15 11:48:34 -05003889
Christian König2898c342012-07-05 11:55:34 +02003890 r = radeon_ib_pool_init(rdev);
3891 if (r) {
3892 dev_err(rdev->dev, "IB initialization failed (%d).\n", r);
Jerome Glisseb15ba512011-11-15 11:48:34 -05003893 return r;
Christian König2898c342012-07-05 11:55:34 +02003894 }
Jerome Glisseb15ba512011-11-15 11:48:34 -05003895
Jerome Glissed4550902009-10-01 10:12:06 +02003896 return 0;
3897}
3898
3899int r100_resume(struct radeon_device *rdev)
3900{
Jerome Glisse6b7746e2012-02-20 17:57:20 -05003901 int r;
3902
Jerome Glissed4550902009-10-01 10:12:06 +02003903 /* Make sur GART are not working */
3904 if (rdev->flags & RADEON_IS_PCI)
3905 r100_pci_gart_disable(rdev);
3906 /* Resume clock before doing reset */
3907 r100_clock_startup(rdev);
3908 /* Reset gpu before posting otherwise ATOM will enter infinite loop */
Jerome Glissea2d07b72010-03-09 14:45:11 +00003909 if (radeon_asic_reset(rdev)) {
Jerome Glissed4550902009-10-01 10:12:06 +02003910 dev_warn(rdev->dev, "GPU reset failed ! (0xE40=0x%08X, 0x7C0=0x%08X)\n",
3911 RREG32(R_000E40_RBBM_STATUS),
3912 RREG32(R_0007C0_CP_STAT));
3913 }
3914 /* post */
3915 radeon_combios_asic_init(rdev->ddev);
3916 /* Resume clock after posting */
3917 r100_clock_startup(rdev);
Dave Airlie550e2d92009-12-09 14:15:38 +10003918 /* Initialize surface registers */
3919 radeon_surface_init(rdev);
Jerome Glisseb15ba512011-11-15 11:48:34 -05003920
3921 rdev->accel_working = true;
Jerome Glisse6b7746e2012-02-20 17:57:20 -05003922 r = r100_startup(rdev);
3923 if (r) {
3924 rdev->accel_working = false;
3925 }
3926 return r;
Jerome Glissed4550902009-10-01 10:12:06 +02003927}
3928
3929int r100_suspend(struct radeon_device *rdev)
3930{
Alex Deucher6c7bcce2013-12-18 14:07:14 -05003931 radeon_pm_suspend(rdev);
Jerome Glissed4550902009-10-01 10:12:06 +02003932 r100_cp_disable(rdev);
Alex Deucher724c80e2010-08-27 18:25:25 -04003933 radeon_wb_disable(rdev);
Jerome Glissed4550902009-10-01 10:12:06 +02003934 r100_irq_disable(rdev);
3935 if (rdev->flags & RADEON_IS_PCI)
3936 r100_pci_gart_disable(rdev);
3937 return 0;
3938}
3939
3940void r100_fini(struct radeon_device *rdev)
3941{
Alex Deucher6c7bcce2013-12-18 14:07:14 -05003942 radeon_pm_fini(rdev);
Jerome Glissed4550902009-10-01 10:12:06 +02003943 r100_cp_fini(rdev);
Alex Deucher724c80e2010-08-27 18:25:25 -04003944 radeon_wb_fini(rdev);
Christian König2898c342012-07-05 11:55:34 +02003945 radeon_ib_pool_fini(rdev);
Jerome Glissed4550902009-10-01 10:12:06 +02003946 radeon_gem_fini(rdev);
3947 if (rdev->flags & RADEON_IS_PCI)
3948 r100_pci_gart_fini(rdev);
Jerome Glissed0269ed2010-01-07 16:08:32 +01003949 radeon_agp_fini(rdev);
Jerome Glissed4550902009-10-01 10:12:06 +02003950 radeon_irq_kms_fini(rdev);
3951 radeon_fence_driver_fini(rdev);
Jerome Glisse4c788672009-11-20 14:29:23 +01003952 radeon_bo_fini(rdev);
Jerome Glissed4550902009-10-01 10:12:06 +02003953 radeon_atombios_fini(rdev);
3954 kfree(rdev->bios);
3955 rdev->bios = NULL;
3956}
3957
Dave Airlie4c712e62010-07-15 12:13:50 +10003958/*
3959 * Due to how kexec works, it can leave the hw fully initialised when it
3960 * boots the new kernel. However doing our init sequence with the CP and
3961 * WB stuff setup causes GPU hangs on the RN50 at least. So at startup
3962 * do some quick sanity checks and restore sane values to avoid this
3963 * problem.
3964 */
3965void r100_restore_sanity(struct radeon_device *rdev)
3966{
3967 u32 tmp;
3968
3969 tmp = RREG32(RADEON_CP_CSQ_CNTL);
3970 if (tmp) {
3971 WREG32(RADEON_CP_CSQ_CNTL, 0);
3972 }
3973 tmp = RREG32(RADEON_CP_RB_CNTL);
3974 if (tmp) {
3975 WREG32(RADEON_CP_RB_CNTL, 0);
3976 }
3977 tmp = RREG32(RADEON_SCRATCH_UMSK);
3978 if (tmp) {
3979 WREG32(RADEON_SCRATCH_UMSK, 0);
3980 }
3981}
3982
Jerome Glissed4550902009-10-01 10:12:06 +02003983int r100_init(struct radeon_device *rdev)
3984{
3985 int r;
3986
Jerome Glissed4550902009-10-01 10:12:06 +02003987 /* Register debugfs file specific to this group of asics */
3988 r100_debugfs(rdev);
3989 /* Disable VGA */
3990 r100_vga_render_disable(rdev);
3991 /* Initialize scratch registers */
3992 radeon_scratch_init(rdev);
3993 /* Initialize surface registers */
3994 radeon_surface_init(rdev);
Dave Airlie4c712e62010-07-15 12:13:50 +10003995 /* sanity check some register to avoid hangs like after kexec */
3996 r100_restore_sanity(rdev);
Jerome Glissed4550902009-10-01 10:12:06 +02003997 /* TODO: disable VGA need to use VGA request */
3998 /* BIOS*/
3999 if (!radeon_get_bios(rdev)) {
4000 if (ASIC_IS_AVIVO(rdev))
4001 return -EINVAL;
4002 }
4003 if (rdev->is_atom_bios) {
4004 dev_err(rdev->dev, "Expecting combios for RS400/RS480 GPU\n");
4005 return -EINVAL;
4006 } else {
4007 r = radeon_combios_init(rdev);
4008 if (r)
4009 return r;
4010 }
4011 /* Reset gpu before posting otherwise ATOM will enter infinite loop */
Jerome Glissea2d07b72010-03-09 14:45:11 +00004012 if (radeon_asic_reset(rdev)) {
Jerome Glissed4550902009-10-01 10:12:06 +02004013 dev_warn(rdev->dev,
4014 "GPU reset failed ! (0xE40=0x%08X, 0x7C0=0x%08X)\n",
4015 RREG32(R_000E40_RBBM_STATUS),
4016 RREG32(R_0007C0_CP_STAT));
4017 }
4018 /* check if cards are posted or not */
Dave Airlie72542d72009-12-01 14:06:31 +10004019 if (radeon_boot_test_post_card(rdev) == false)
4020 return -EINVAL;
Jerome Glissed4550902009-10-01 10:12:06 +02004021 /* Set asic errata */
4022 r100_errata(rdev);
4023 /* Initialize clocks */
4024 radeon_get_clock_info(rdev->ddev);
Jerome Glissed594e462010-02-17 21:54:29 +00004025 /* initialize AGP */
4026 if (rdev->flags & RADEON_IS_AGP) {
4027 r = radeon_agp_init(rdev);
4028 if (r) {
4029 radeon_agp_disable(rdev);
4030 }
4031 }
4032 /* initialize VRAM */
4033 r100_mc_init(rdev);
Jerome Glissed4550902009-10-01 10:12:06 +02004034 /* Fence driver */
Jerome Glisse30eb77f2011-11-20 20:45:34 +00004035 r = radeon_fence_driver_init(rdev);
Jerome Glissed4550902009-10-01 10:12:06 +02004036 if (r)
4037 return r;
Jerome Glissed4550902009-10-01 10:12:06 +02004038 /* Memory manager */
Jerome Glisse4c788672009-11-20 14:29:23 +01004039 r = radeon_bo_init(rdev);
Jerome Glissed4550902009-10-01 10:12:06 +02004040 if (r)
4041 return r;
4042 if (rdev->flags & RADEON_IS_PCI) {
4043 r = r100_pci_gart_init(rdev);
4044 if (r)
4045 return r;
4046 }
4047 r100_set_safe_registers(rdev);
Jerome Glisseb15ba512011-11-15 11:48:34 -05004048
Alex Deucher6c7bcce2013-12-18 14:07:14 -05004049 /* Initialize power management */
4050 radeon_pm_init(rdev);
4051
Jerome Glissed4550902009-10-01 10:12:06 +02004052 rdev->accel_working = true;
4053 r = r100_startup(rdev);
4054 if (r) {
4055 /* Somethings want wront with the accel init stop accel */
4056 dev_err(rdev->dev, "Disabling GPU acceleration\n");
Jerome Glissed4550902009-10-01 10:12:06 +02004057 r100_cp_fini(rdev);
Alex Deucher724c80e2010-08-27 18:25:25 -04004058 radeon_wb_fini(rdev);
Christian König2898c342012-07-05 11:55:34 +02004059 radeon_ib_pool_fini(rdev);
Jerome Glisse655efd32010-02-02 11:51:45 +01004060 radeon_irq_kms_fini(rdev);
Jerome Glissed4550902009-10-01 10:12:06 +02004061 if (rdev->flags & RADEON_IS_PCI)
4062 r100_pci_gart_fini(rdev);
Jerome Glissed4550902009-10-01 10:12:06 +02004063 rdev->accel_working = false;
4064 }
4065 return 0;
4066}
Andi Kleen6fcbef72011-10-13 16:08:42 -07004067
Daniel Vetter2ef9bdf2012-12-02 14:02:51 +01004068uint32_t r100_mm_rreg(struct radeon_device *rdev, uint32_t reg,
4069 bool always_indirect)
Andi Kleen6fcbef72011-10-13 16:08:42 -07004070{
Daniel Vetter2ef9bdf2012-12-02 14:02:51 +01004071 if (reg < rdev->rmmio_size && !always_indirect)
Andi Kleen6fcbef72011-10-13 16:08:42 -07004072 return readl(((void __iomem *)rdev->rmmio) + reg);
4073 else {
Daniel Vetter2c385152012-12-02 14:06:15 +01004074 unsigned long flags;
4075 uint32_t ret;
4076
4077 spin_lock_irqsave(&rdev->mmio_idx_lock, flags);
Andi Kleen6fcbef72011-10-13 16:08:42 -07004078 writel(reg, ((void __iomem *)rdev->rmmio) + RADEON_MM_INDEX);
Daniel Vetter2c385152012-12-02 14:06:15 +01004079 ret = readl(((void __iomem *)rdev->rmmio) + RADEON_MM_DATA);
4080 spin_unlock_irqrestore(&rdev->mmio_idx_lock, flags);
4081
4082 return ret;
Andi Kleen6fcbef72011-10-13 16:08:42 -07004083 }
4084}
4085
Daniel Vetter2ef9bdf2012-12-02 14:02:51 +01004086void r100_mm_wreg(struct radeon_device *rdev, uint32_t reg, uint32_t v,
4087 bool always_indirect)
Andi Kleen6fcbef72011-10-13 16:08:42 -07004088{
Daniel Vetter2ef9bdf2012-12-02 14:02:51 +01004089 if (reg < rdev->rmmio_size && !always_indirect)
Andi Kleen6fcbef72011-10-13 16:08:42 -07004090 writel(v, ((void __iomem *)rdev->rmmio) + reg);
4091 else {
Daniel Vetter2c385152012-12-02 14:06:15 +01004092 unsigned long flags;
4093
4094 spin_lock_irqsave(&rdev->mmio_idx_lock, flags);
Andi Kleen6fcbef72011-10-13 16:08:42 -07004095 writel(reg, ((void __iomem *)rdev->rmmio) + RADEON_MM_INDEX);
4096 writel(v, ((void __iomem *)rdev->rmmio) + RADEON_MM_DATA);
Daniel Vetter2c385152012-12-02 14:06:15 +01004097 spin_unlock_irqrestore(&rdev->mmio_idx_lock, flags);
Andi Kleen6fcbef72011-10-13 16:08:42 -07004098 }
4099}
4100
4101u32 r100_io_rreg(struct radeon_device *rdev, u32 reg)
4102{
4103 if (reg < rdev->rio_mem_size)
4104 return ioread32(rdev->rio_mem + reg);
4105 else {
4106 iowrite32(reg, rdev->rio_mem + RADEON_MM_INDEX);
4107 return ioread32(rdev->rio_mem + RADEON_MM_DATA);
4108 }
4109}
4110
4111void r100_io_wreg(struct radeon_device *rdev, u32 reg, u32 v)
4112{
4113 if (reg < rdev->rio_mem_size)
4114 iowrite32(v, rdev->rio_mem + reg);
4115 else {
4116 iowrite32(reg, rdev->rio_mem + RADEON_MM_INDEX);
4117 iowrite32(v, rdev->rio_mem + RADEON_MM_DATA);
4118 }
4119}