]> git.openfabrics.org - ~shefty/rdma-dev.git/blob - drivers/gpu/drm/radeon/r600.c
drm/radeon/kms: fix vram base calculation on rs780/rs880
[~shefty/rdma-dev.git] / drivers / gpu / drm / radeon / r600.c
1 /*
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/slab.h>
29 #include <linux/seq_file.h>
30 #include <linux/firmware.h>
31 #include <linux/platform_device.h>
32 #include "drmP.h"
33 #include "radeon_drm.h"
34 #include "radeon.h"
35 #include "radeon_asic.h"
36 #include "radeon_mode.h"
37 #include "r600d.h"
38 #include "atom.h"
39 #include "avivod.h"
40
41 #define PFP_UCODE_SIZE 576
42 #define PM4_UCODE_SIZE 1792
43 #define RLC_UCODE_SIZE 768
44 #define R700_PFP_UCODE_SIZE 848
45 #define R700_PM4_UCODE_SIZE 1360
46 #define R700_RLC_UCODE_SIZE 1024
47 #define EVERGREEN_PFP_UCODE_SIZE 1120
48 #define EVERGREEN_PM4_UCODE_SIZE 1376
49 #define EVERGREEN_RLC_UCODE_SIZE 768
50
51 /* Firmware Names */
52 MODULE_FIRMWARE("radeon/R600_pfp.bin");
53 MODULE_FIRMWARE("radeon/R600_me.bin");
54 MODULE_FIRMWARE("radeon/RV610_pfp.bin");
55 MODULE_FIRMWARE("radeon/RV610_me.bin");
56 MODULE_FIRMWARE("radeon/RV630_pfp.bin");
57 MODULE_FIRMWARE("radeon/RV630_me.bin");
58 MODULE_FIRMWARE("radeon/RV620_pfp.bin");
59 MODULE_FIRMWARE("radeon/RV620_me.bin");
60 MODULE_FIRMWARE("radeon/RV635_pfp.bin");
61 MODULE_FIRMWARE("radeon/RV635_me.bin");
62 MODULE_FIRMWARE("radeon/RV670_pfp.bin");
63 MODULE_FIRMWARE("radeon/RV670_me.bin");
64 MODULE_FIRMWARE("radeon/RS780_pfp.bin");
65 MODULE_FIRMWARE("radeon/RS780_me.bin");
66 MODULE_FIRMWARE("radeon/RV770_pfp.bin");
67 MODULE_FIRMWARE("radeon/RV770_me.bin");
68 MODULE_FIRMWARE("radeon/RV730_pfp.bin");
69 MODULE_FIRMWARE("radeon/RV730_me.bin");
70 MODULE_FIRMWARE("radeon/RV710_pfp.bin");
71 MODULE_FIRMWARE("radeon/RV710_me.bin");
72 MODULE_FIRMWARE("radeon/R600_rlc.bin");
73 MODULE_FIRMWARE("radeon/R700_rlc.bin");
74 MODULE_FIRMWARE("radeon/CEDAR_pfp.bin");
75 MODULE_FIRMWARE("radeon/CEDAR_me.bin");
76 MODULE_FIRMWARE("radeon/CEDAR_rlc.bin");
77 MODULE_FIRMWARE("radeon/REDWOOD_pfp.bin");
78 MODULE_FIRMWARE("radeon/REDWOOD_me.bin");
79 MODULE_FIRMWARE("radeon/REDWOOD_rlc.bin");
80 MODULE_FIRMWARE("radeon/JUNIPER_pfp.bin");
81 MODULE_FIRMWARE("radeon/JUNIPER_me.bin");
82 MODULE_FIRMWARE("radeon/JUNIPER_rlc.bin");
83 MODULE_FIRMWARE("radeon/CYPRESS_pfp.bin");
84 MODULE_FIRMWARE("radeon/CYPRESS_me.bin");
85 MODULE_FIRMWARE("radeon/CYPRESS_rlc.bin");
86
87 int r600_debugfs_mc_info_init(struct radeon_device *rdev);
88
89 /* r600,rv610,rv630,rv620,rv635,rv670 */
90 int r600_mc_wait_for_idle(struct radeon_device *rdev);
91 void r600_gpu_init(struct radeon_device *rdev);
92 void r600_fini(struct radeon_device *rdev);
93 void r600_irq_disable(struct radeon_device *rdev);
94
95 /* get temperature in millidegrees */
96 u32 rv6xx_get_temp(struct radeon_device *rdev)
97 {
98         u32 temp = (RREG32(CG_THERMAL_STATUS) & ASIC_T_MASK) >>
99                 ASIC_T_SHIFT;
100
101         return temp * 1000;
102 }
103
104 void r600_pm_get_dynpm_state(struct radeon_device *rdev)
105 {
106         int i;
107
108         rdev->pm.dynpm_can_upclock = true;
109         rdev->pm.dynpm_can_downclock = true;
110
111         /* power state array is low to high, default is first */
112         if ((rdev->flags & RADEON_IS_IGP) || (rdev->family == CHIP_R600)) {
113                 int min_power_state_index = 0;
114
115                 if (rdev->pm.num_power_states > 2)
116                         min_power_state_index = 1;
117
118                 switch (rdev->pm.dynpm_planned_action) {
119                 case DYNPM_ACTION_MINIMUM:
120                         rdev->pm.requested_power_state_index = min_power_state_index;
121                         rdev->pm.requested_clock_mode_index = 0;
122                         rdev->pm.dynpm_can_downclock = false;
123                         break;
124                 case DYNPM_ACTION_DOWNCLOCK:
125                         if (rdev->pm.current_power_state_index == min_power_state_index) {
126                                 rdev->pm.requested_power_state_index = rdev->pm.current_power_state_index;
127                                 rdev->pm.dynpm_can_downclock = false;
128                         } else {
129                                 if (rdev->pm.active_crtc_count > 1) {
130                                         for (i = 0; i < rdev->pm.num_power_states; i++) {
131                                                 if (rdev->pm.power_state[i].flags & RADEON_PM_STATE_SINGLE_DISPLAY_ONLY)
132                                                         continue;
133                                                 else if (i >= rdev->pm.current_power_state_index) {
134                                                         rdev->pm.requested_power_state_index =
135                                                                 rdev->pm.current_power_state_index;
136                                                         break;
137                                                 } else {
138                                                         rdev->pm.requested_power_state_index = i;
139                                                         break;
140                                                 }
141                                         }
142                                 } else {
143                                         if (rdev->pm.current_power_state_index == 0)
144                                                 rdev->pm.requested_power_state_index =
145                                                         rdev->pm.num_power_states - 1;
146                                         else
147                                                 rdev->pm.requested_power_state_index =
148                                                         rdev->pm.current_power_state_index - 1;
149                                 }
150                         }
151                         rdev->pm.requested_clock_mode_index = 0;
152                         /* don't use the power state if crtcs are active and no display flag is set */
153                         if ((rdev->pm.active_crtc_count > 0) &&
154                             (rdev->pm.power_state[rdev->pm.requested_power_state_index].
155                              clock_info[rdev->pm.requested_clock_mode_index].flags &
156                              RADEON_PM_MODE_NO_DISPLAY)) {
157                                 rdev->pm.requested_power_state_index++;
158                         }
159                         break;
160                 case DYNPM_ACTION_UPCLOCK:
161                         if (rdev->pm.current_power_state_index == (rdev->pm.num_power_states - 1)) {
162                                 rdev->pm.requested_power_state_index = rdev->pm.current_power_state_index;
163                                 rdev->pm.dynpm_can_upclock = false;
164                         } else {
165                                 if (rdev->pm.active_crtc_count > 1) {
166                                         for (i = (rdev->pm.num_power_states - 1); i >= 0; i--) {
167                                                 if (rdev->pm.power_state[i].flags & RADEON_PM_STATE_SINGLE_DISPLAY_ONLY)
168                                                         continue;
169                                                 else if (i <= rdev->pm.current_power_state_index) {
170                                                         rdev->pm.requested_power_state_index =
171                                                                 rdev->pm.current_power_state_index;
172                                                         break;
173                                                 } else {
174                                                         rdev->pm.requested_power_state_index = i;
175                                                         break;
176                                                 }
177                                         }
178                                 } else
179                                         rdev->pm.requested_power_state_index =
180                                                 rdev->pm.current_power_state_index + 1;
181                         }
182                         rdev->pm.requested_clock_mode_index = 0;
183                         break;
184                 case DYNPM_ACTION_DEFAULT:
185                         rdev->pm.requested_power_state_index = rdev->pm.default_power_state_index;
186                         rdev->pm.requested_clock_mode_index = 0;
187                         rdev->pm.dynpm_can_upclock = false;
188                         break;
189                 case DYNPM_ACTION_NONE:
190                 default:
191                         DRM_ERROR("Requested mode for not defined action\n");
192                         return;
193                 }
194         } else {
195                 /* XXX select a power state based on AC/DC, single/dualhead, etc. */
196                 /* for now just select the first power state and switch between clock modes */
197                 /* power state array is low to high, default is first (0) */
198                 if (rdev->pm.active_crtc_count > 1) {
199                         rdev->pm.requested_power_state_index = -1;
200                         /* start at 1 as we don't want the default mode */
201                         for (i = 1; i < rdev->pm.num_power_states; i++) {
202                                 if (rdev->pm.power_state[i].flags & RADEON_PM_STATE_SINGLE_DISPLAY_ONLY)
203                                         continue;
204                                 else if ((rdev->pm.power_state[i].type == POWER_STATE_TYPE_PERFORMANCE) ||
205                                          (rdev->pm.power_state[i].type == POWER_STATE_TYPE_BATTERY)) {
206                                         rdev->pm.requested_power_state_index = i;
207                                         break;
208                                 }
209                         }
210                         /* if nothing selected, grab the default state. */
211                         if (rdev->pm.requested_power_state_index == -1)
212                                 rdev->pm.requested_power_state_index = 0;
213                 } else
214                         rdev->pm.requested_power_state_index = 1;
215
216                 switch (rdev->pm.dynpm_planned_action) {
217                 case DYNPM_ACTION_MINIMUM:
218                         rdev->pm.requested_clock_mode_index = 0;
219                         rdev->pm.dynpm_can_downclock = false;
220                         break;
221                 case DYNPM_ACTION_DOWNCLOCK:
222                         if (rdev->pm.requested_power_state_index == rdev->pm.current_power_state_index) {
223                                 if (rdev->pm.current_clock_mode_index == 0) {
224                                         rdev->pm.requested_clock_mode_index = 0;
225                                         rdev->pm.dynpm_can_downclock = false;
226                                 } else
227                                         rdev->pm.requested_clock_mode_index =
228                                                 rdev->pm.current_clock_mode_index - 1;
229                         } else {
230                                 rdev->pm.requested_clock_mode_index = 0;
231                                 rdev->pm.dynpm_can_downclock = false;
232                         }
233                         /* don't use the power state if crtcs are active and no display flag is set */
234                         if ((rdev->pm.active_crtc_count > 0) &&
235                             (rdev->pm.power_state[rdev->pm.requested_power_state_index].
236                              clock_info[rdev->pm.requested_clock_mode_index].flags &
237                              RADEON_PM_MODE_NO_DISPLAY)) {
238                                 rdev->pm.requested_clock_mode_index++;
239                         }
240                         break;
241                 case DYNPM_ACTION_UPCLOCK:
242                         if (rdev->pm.requested_power_state_index == rdev->pm.current_power_state_index) {
243                                 if (rdev->pm.current_clock_mode_index ==
244                                     (rdev->pm.power_state[rdev->pm.requested_power_state_index].num_clock_modes - 1)) {
245                                         rdev->pm.requested_clock_mode_index = rdev->pm.current_clock_mode_index;
246                                         rdev->pm.dynpm_can_upclock = false;
247                                 } else
248                                         rdev->pm.requested_clock_mode_index =
249                                                 rdev->pm.current_clock_mode_index + 1;
250                         } else {
251                                 rdev->pm.requested_clock_mode_index =
252                                         rdev->pm.power_state[rdev->pm.requested_power_state_index].num_clock_modes - 1;
253                                 rdev->pm.dynpm_can_upclock = false;
254                         }
255                         break;
256                 case DYNPM_ACTION_DEFAULT:
257                         rdev->pm.requested_power_state_index = rdev->pm.default_power_state_index;
258                         rdev->pm.requested_clock_mode_index = 0;
259                         rdev->pm.dynpm_can_upclock = false;
260                         break;
261                 case DYNPM_ACTION_NONE:
262                 default:
263                         DRM_ERROR("Requested mode for not defined action\n");
264                         return;
265                 }
266         }
267
268         DRM_DEBUG_DRIVER("Requested: e: %d m: %d p: %d\n",
269                   rdev->pm.power_state[rdev->pm.requested_power_state_index].
270                   clock_info[rdev->pm.requested_clock_mode_index].sclk,
271                   rdev->pm.power_state[rdev->pm.requested_power_state_index].
272                   clock_info[rdev->pm.requested_clock_mode_index].mclk,
273                   rdev->pm.power_state[rdev->pm.requested_power_state_index].
274                   pcie_lanes);
275 }
276
277 static int r600_pm_get_type_index(struct radeon_device *rdev,
278                                   enum radeon_pm_state_type ps_type,
279                                   int instance)
280 {
281         int i;
282         int found_instance = -1;
283
284         for (i = 0; i < rdev->pm.num_power_states; i++) {
285                 if (rdev->pm.power_state[i].type == ps_type) {
286                         found_instance++;
287                         if (found_instance == instance)
288                                 return i;
289                 }
290         }
291         /* return default if no match */
292         return rdev->pm.default_power_state_index;
293 }
294
295 void rs780_pm_init_profile(struct radeon_device *rdev)
296 {
297         if (rdev->pm.num_power_states == 2) {
298                 /* default */
299                 rdev->pm.profiles[PM_PROFILE_DEFAULT_IDX].dpms_off_ps_idx = rdev->pm.default_power_state_index;
300                 rdev->pm.profiles[PM_PROFILE_DEFAULT_IDX].dpms_on_ps_idx = rdev->pm.default_power_state_index;
301                 rdev->pm.profiles[PM_PROFILE_DEFAULT_IDX].dpms_off_cm_idx = 0;
302                 rdev->pm.profiles[PM_PROFILE_DEFAULT_IDX].dpms_on_cm_idx = 0;
303                 /* low sh */
304                 rdev->pm.profiles[PM_PROFILE_LOW_SH_IDX].dpms_off_ps_idx = 0;
305                 rdev->pm.profiles[PM_PROFILE_LOW_SH_IDX].dpms_on_ps_idx = 0;
306                 rdev->pm.profiles[PM_PROFILE_LOW_SH_IDX].dpms_off_cm_idx = 0;
307                 rdev->pm.profiles[PM_PROFILE_LOW_SH_IDX].dpms_on_cm_idx = 0;
308                 /* mid sh */
309                 rdev->pm.profiles[PM_PROFILE_MID_SH_IDX].dpms_off_ps_idx = 0;
310                 rdev->pm.profiles[PM_PROFILE_MID_SH_IDX].dpms_on_ps_idx = 0;
311                 rdev->pm.profiles[PM_PROFILE_MID_SH_IDX].dpms_off_cm_idx = 0;
312                 rdev->pm.profiles[PM_PROFILE_MID_SH_IDX].dpms_on_cm_idx = 0;
313                 /* high sh */
314                 rdev->pm.profiles[PM_PROFILE_HIGH_SH_IDX].dpms_off_ps_idx = 0;
315                 rdev->pm.profiles[PM_PROFILE_HIGH_SH_IDX].dpms_on_ps_idx = 1;
316                 rdev->pm.profiles[PM_PROFILE_HIGH_SH_IDX].dpms_off_cm_idx = 0;
317                 rdev->pm.profiles[PM_PROFILE_HIGH_SH_IDX].dpms_on_cm_idx = 0;
318                 /* low mh */
319                 rdev->pm.profiles[PM_PROFILE_LOW_MH_IDX].dpms_off_ps_idx = 0;
320                 rdev->pm.profiles[PM_PROFILE_LOW_MH_IDX].dpms_on_ps_idx = 0;
321                 rdev->pm.profiles[PM_PROFILE_LOW_MH_IDX].dpms_off_cm_idx = 0;
322                 rdev->pm.profiles[PM_PROFILE_LOW_MH_IDX].dpms_on_cm_idx = 0;
323                 /* mid mh */
324                 rdev->pm.profiles[PM_PROFILE_MID_MH_IDX].dpms_off_ps_idx = 0;
325                 rdev->pm.profiles[PM_PROFILE_MID_MH_IDX].dpms_on_ps_idx = 0;
326                 rdev->pm.profiles[PM_PROFILE_MID_MH_IDX].dpms_off_cm_idx = 0;
327                 rdev->pm.profiles[PM_PROFILE_MID_MH_IDX].dpms_on_cm_idx = 0;
328                 /* high mh */
329                 rdev->pm.profiles[PM_PROFILE_HIGH_MH_IDX].dpms_off_ps_idx = 0;
330                 rdev->pm.profiles[PM_PROFILE_HIGH_MH_IDX].dpms_on_ps_idx = 1;
331                 rdev->pm.profiles[PM_PROFILE_HIGH_MH_IDX].dpms_off_cm_idx = 0;
332                 rdev->pm.profiles[PM_PROFILE_HIGH_MH_IDX].dpms_on_cm_idx = 0;
333         } else if (rdev->pm.num_power_states == 3) {
334                 /* default */
335                 rdev->pm.profiles[PM_PROFILE_DEFAULT_IDX].dpms_off_ps_idx = rdev->pm.default_power_state_index;
336                 rdev->pm.profiles[PM_PROFILE_DEFAULT_IDX].dpms_on_ps_idx = rdev->pm.default_power_state_index;
337                 rdev->pm.profiles[PM_PROFILE_DEFAULT_IDX].dpms_off_cm_idx = 0;
338                 rdev->pm.profiles[PM_PROFILE_DEFAULT_IDX].dpms_on_cm_idx = 0;
339                 /* low sh */
340                 rdev->pm.profiles[PM_PROFILE_LOW_SH_IDX].dpms_off_ps_idx = 1;
341                 rdev->pm.profiles[PM_PROFILE_LOW_SH_IDX].dpms_on_ps_idx = 1;
342                 rdev->pm.profiles[PM_PROFILE_LOW_SH_IDX].dpms_off_cm_idx = 0;
343                 rdev->pm.profiles[PM_PROFILE_LOW_SH_IDX].dpms_on_cm_idx = 0;
344                 /* mid sh */
345                 rdev->pm.profiles[PM_PROFILE_MID_SH_IDX].dpms_off_ps_idx = 1;
346                 rdev->pm.profiles[PM_PROFILE_MID_SH_IDX].dpms_on_ps_idx = 1;
347                 rdev->pm.profiles[PM_PROFILE_MID_SH_IDX].dpms_off_cm_idx = 0;
348                 rdev->pm.profiles[PM_PROFILE_MID_SH_IDX].dpms_on_cm_idx = 0;
349                 /* high sh */
350                 rdev->pm.profiles[PM_PROFILE_HIGH_SH_IDX].dpms_off_ps_idx = 1;
351                 rdev->pm.profiles[PM_PROFILE_HIGH_SH_IDX].dpms_on_ps_idx = 2;
352                 rdev->pm.profiles[PM_PROFILE_HIGH_SH_IDX].dpms_off_cm_idx = 0;
353                 rdev->pm.profiles[PM_PROFILE_HIGH_SH_IDX].dpms_on_cm_idx = 0;
354                 /* low mh */
355                 rdev->pm.profiles[PM_PROFILE_LOW_MH_IDX].dpms_off_ps_idx = 1;
356                 rdev->pm.profiles[PM_PROFILE_LOW_MH_IDX].dpms_on_ps_idx = 1;
357                 rdev->pm.profiles[PM_PROFILE_LOW_MH_IDX].dpms_off_cm_idx = 0;
358                 rdev->pm.profiles[PM_PROFILE_LOW_MH_IDX].dpms_on_cm_idx = 0;
359                 /* mid mh */
360                 rdev->pm.profiles[PM_PROFILE_MID_MH_IDX].dpms_off_ps_idx = 1;
361                 rdev->pm.profiles[PM_PROFILE_MID_MH_IDX].dpms_on_ps_idx = 1;
362                 rdev->pm.profiles[PM_PROFILE_MID_MH_IDX].dpms_off_cm_idx = 0;
363                 rdev->pm.profiles[PM_PROFILE_MID_MH_IDX].dpms_on_cm_idx = 0;
364                 /* high mh */
365                 rdev->pm.profiles[PM_PROFILE_HIGH_MH_IDX].dpms_off_ps_idx = 1;
366                 rdev->pm.profiles[PM_PROFILE_HIGH_MH_IDX].dpms_on_ps_idx = 2;
367                 rdev->pm.profiles[PM_PROFILE_HIGH_MH_IDX].dpms_off_cm_idx = 0;
368                 rdev->pm.profiles[PM_PROFILE_HIGH_MH_IDX].dpms_on_cm_idx = 0;
369         } else {
370                 /* default */
371                 rdev->pm.profiles[PM_PROFILE_DEFAULT_IDX].dpms_off_ps_idx = rdev->pm.default_power_state_index;
372                 rdev->pm.profiles[PM_PROFILE_DEFAULT_IDX].dpms_on_ps_idx = rdev->pm.default_power_state_index;
373                 rdev->pm.profiles[PM_PROFILE_DEFAULT_IDX].dpms_off_cm_idx = 0;
374                 rdev->pm.profiles[PM_PROFILE_DEFAULT_IDX].dpms_on_cm_idx = 0;
375                 /* low sh */
376                 rdev->pm.profiles[PM_PROFILE_LOW_SH_IDX].dpms_off_ps_idx = 2;
377                 rdev->pm.profiles[PM_PROFILE_LOW_SH_IDX].dpms_on_ps_idx = 2;
378                 rdev->pm.profiles[PM_PROFILE_LOW_SH_IDX].dpms_off_cm_idx = 0;
379                 rdev->pm.profiles[PM_PROFILE_LOW_SH_IDX].dpms_on_cm_idx = 0;
380                 /* mid sh */
381                 rdev->pm.profiles[PM_PROFILE_MID_SH_IDX].dpms_off_ps_idx = 2;
382                 rdev->pm.profiles[PM_PROFILE_MID_SH_IDX].dpms_on_ps_idx = 2;
383                 rdev->pm.profiles[PM_PROFILE_MID_SH_IDX].dpms_off_cm_idx = 0;
384                 rdev->pm.profiles[PM_PROFILE_MID_SH_IDX].dpms_on_cm_idx = 0;
385                 /* high sh */
386                 rdev->pm.profiles[PM_PROFILE_HIGH_SH_IDX].dpms_off_ps_idx = 2;
387                 rdev->pm.profiles[PM_PROFILE_HIGH_SH_IDX].dpms_on_ps_idx = 3;
388                 rdev->pm.profiles[PM_PROFILE_HIGH_SH_IDX].dpms_off_cm_idx = 0;
389                 rdev->pm.profiles[PM_PROFILE_HIGH_SH_IDX].dpms_on_cm_idx = 0;
390                 /* low mh */
391                 rdev->pm.profiles[PM_PROFILE_LOW_MH_IDX].dpms_off_ps_idx = 2;
392                 rdev->pm.profiles[PM_PROFILE_LOW_MH_IDX].dpms_on_ps_idx = 0;
393                 rdev->pm.profiles[PM_PROFILE_LOW_MH_IDX].dpms_off_cm_idx = 0;
394                 rdev->pm.profiles[PM_PROFILE_LOW_MH_IDX].dpms_on_cm_idx = 0;
395                 /* mid mh */
396                 rdev->pm.profiles[PM_PROFILE_MID_MH_IDX].dpms_off_ps_idx = 2;
397                 rdev->pm.profiles[PM_PROFILE_MID_MH_IDX].dpms_on_ps_idx = 0;
398                 rdev->pm.profiles[PM_PROFILE_MID_MH_IDX].dpms_off_cm_idx = 0;
399                 rdev->pm.profiles[PM_PROFILE_MID_MH_IDX].dpms_on_cm_idx = 0;
400                 /* high mh */
401                 rdev->pm.profiles[PM_PROFILE_HIGH_MH_IDX].dpms_off_ps_idx = 2;
402                 rdev->pm.profiles[PM_PROFILE_HIGH_MH_IDX].dpms_on_ps_idx = 3;
403                 rdev->pm.profiles[PM_PROFILE_HIGH_MH_IDX].dpms_off_cm_idx = 0;
404                 rdev->pm.profiles[PM_PROFILE_HIGH_MH_IDX].dpms_on_cm_idx = 0;
405         }
406 }
407
408 void r600_pm_init_profile(struct radeon_device *rdev)
409 {
410         if (rdev->family == CHIP_R600) {
411                 /* XXX */
412                 /* default */
413                 rdev->pm.profiles[PM_PROFILE_DEFAULT_IDX].dpms_off_ps_idx = rdev->pm.default_power_state_index;
414                 rdev->pm.profiles[PM_PROFILE_DEFAULT_IDX].dpms_on_ps_idx = rdev->pm.default_power_state_index;
415                 rdev->pm.profiles[PM_PROFILE_DEFAULT_IDX].dpms_off_cm_idx = 0;
416                 rdev->pm.profiles[PM_PROFILE_DEFAULT_IDX].dpms_on_cm_idx = 0;
417                 /* low sh */
418                 rdev->pm.profiles[PM_PROFILE_LOW_SH_IDX].dpms_off_ps_idx = rdev->pm.default_power_state_index;
419                 rdev->pm.profiles[PM_PROFILE_LOW_SH_IDX].dpms_on_ps_idx = rdev->pm.default_power_state_index;
420                 rdev->pm.profiles[PM_PROFILE_LOW_SH_IDX].dpms_off_cm_idx = 0;
421                 rdev->pm.profiles[PM_PROFILE_LOW_SH_IDX].dpms_on_cm_idx = 0;
422                 /* mid sh */
423                 rdev->pm.profiles[PM_PROFILE_MID_SH_IDX].dpms_off_ps_idx = rdev->pm.default_power_state_index;
424                 rdev->pm.profiles[PM_PROFILE_MID_SH_IDX].dpms_on_ps_idx = rdev->pm.default_power_state_index;
425                 rdev->pm.profiles[PM_PROFILE_MID_SH_IDX].dpms_off_cm_idx = 0;
426                 rdev->pm.profiles[PM_PROFILE_MID_SH_IDX].dpms_on_cm_idx = 0;
427                 /* high sh */
428                 rdev->pm.profiles[PM_PROFILE_HIGH_SH_IDX].dpms_off_ps_idx = rdev->pm.default_power_state_index;
429                 rdev->pm.profiles[PM_PROFILE_HIGH_SH_IDX].dpms_on_ps_idx = rdev->pm.default_power_state_index;
430                 rdev->pm.profiles[PM_PROFILE_HIGH_SH_IDX].dpms_off_cm_idx = 0;
431                 rdev->pm.profiles[PM_PROFILE_HIGH_SH_IDX].dpms_on_cm_idx = 0;
432                 /* low mh */
433                 rdev->pm.profiles[PM_PROFILE_LOW_MH_IDX].dpms_off_ps_idx = rdev->pm.default_power_state_index;
434                 rdev->pm.profiles[PM_PROFILE_LOW_MH_IDX].dpms_on_ps_idx = rdev->pm.default_power_state_index;
435                 rdev->pm.profiles[PM_PROFILE_LOW_MH_IDX].dpms_off_cm_idx = 0;
436                 rdev->pm.profiles[PM_PROFILE_LOW_MH_IDX].dpms_on_cm_idx = 0;
437                 /* mid mh */
438                 rdev->pm.profiles[PM_PROFILE_MID_MH_IDX].dpms_off_ps_idx = rdev->pm.default_power_state_index;
439                 rdev->pm.profiles[PM_PROFILE_MID_MH_IDX].dpms_on_ps_idx = rdev->pm.default_power_state_index;
440                 rdev->pm.profiles[PM_PROFILE_MID_MH_IDX].dpms_off_cm_idx = 0;
441                 rdev->pm.profiles[PM_PROFILE_MID_MH_IDX].dpms_on_cm_idx = 0;
442                 /* high mh */
443                 rdev->pm.profiles[PM_PROFILE_HIGH_MH_IDX].dpms_off_ps_idx = rdev->pm.default_power_state_index;
444                 rdev->pm.profiles[PM_PROFILE_HIGH_MH_IDX].dpms_on_ps_idx = rdev->pm.default_power_state_index;
445                 rdev->pm.profiles[PM_PROFILE_HIGH_MH_IDX].dpms_off_cm_idx = 0;
446                 rdev->pm.profiles[PM_PROFILE_HIGH_MH_IDX].dpms_on_cm_idx = 0;
447         } else {
448                 if (rdev->pm.num_power_states < 4) {
449                         /* default */
450                         rdev->pm.profiles[PM_PROFILE_DEFAULT_IDX].dpms_off_ps_idx = rdev->pm.default_power_state_index;
451                         rdev->pm.profiles[PM_PROFILE_DEFAULT_IDX].dpms_on_ps_idx = rdev->pm.default_power_state_index;
452                         rdev->pm.profiles[PM_PROFILE_DEFAULT_IDX].dpms_off_cm_idx = 0;
453                         rdev->pm.profiles[PM_PROFILE_DEFAULT_IDX].dpms_on_cm_idx = 2;
454                         /* low sh */
455                         rdev->pm.profiles[PM_PROFILE_LOW_SH_IDX].dpms_off_ps_idx = 1;
456                         rdev->pm.profiles[PM_PROFILE_LOW_SH_IDX].dpms_on_ps_idx = 1;
457                         rdev->pm.profiles[PM_PROFILE_LOW_SH_IDX].dpms_off_cm_idx = 0;
458                         rdev->pm.profiles[PM_PROFILE_LOW_SH_IDX].dpms_on_cm_idx = 0;
459                         /* mid sh */
460                         rdev->pm.profiles[PM_PROFILE_MID_SH_IDX].dpms_off_ps_idx = 1;
461                         rdev->pm.profiles[PM_PROFILE_MID_SH_IDX].dpms_on_ps_idx = 1;
462                         rdev->pm.profiles[PM_PROFILE_MID_SH_IDX].dpms_off_cm_idx = 0;
463                         rdev->pm.profiles[PM_PROFILE_MID_SH_IDX].dpms_on_cm_idx = 1;
464                         /* high sh */
465                         rdev->pm.profiles[PM_PROFILE_HIGH_SH_IDX].dpms_off_ps_idx = 1;
466                         rdev->pm.profiles[PM_PROFILE_HIGH_SH_IDX].dpms_on_ps_idx = 1;
467                         rdev->pm.profiles[PM_PROFILE_HIGH_SH_IDX].dpms_off_cm_idx = 0;
468                         rdev->pm.profiles[PM_PROFILE_HIGH_SH_IDX].dpms_on_cm_idx = 2;
469                         /* low mh */
470                         rdev->pm.profiles[PM_PROFILE_LOW_MH_IDX].dpms_off_ps_idx = 2;
471                         rdev->pm.profiles[PM_PROFILE_LOW_MH_IDX].dpms_on_ps_idx = 2;
472                         rdev->pm.profiles[PM_PROFILE_LOW_MH_IDX].dpms_off_cm_idx = 0;
473                         rdev->pm.profiles[PM_PROFILE_LOW_MH_IDX].dpms_on_cm_idx = 0;
474                         /* low mh */
475                         rdev->pm.profiles[PM_PROFILE_MID_MH_IDX].dpms_off_ps_idx = 2;
476                         rdev->pm.profiles[PM_PROFILE_MID_MH_IDX].dpms_on_ps_idx = 2;
477                         rdev->pm.profiles[PM_PROFILE_MID_MH_IDX].dpms_off_cm_idx = 0;
478                         rdev->pm.profiles[PM_PROFILE_MID_MH_IDX].dpms_on_cm_idx = 1;
479                         /* high mh */
480                         rdev->pm.profiles[PM_PROFILE_HIGH_MH_IDX].dpms_off_ps_idx = 2;
481                         rdev->pm.profiles[PM_PROFILE_HIGH_MH_IDX].dpms_on_ps_idx = 2;
482                         rdev->pm.profiles[PM_PROFILE_HIGH_MH_IDX].dpms_off_cm_idx = 0;
483                         rdev->pm.profiles[PM_PROFILE_HIGH_MH_IDX].dpms_on_cm_idx = 2;
484                 } else {
485                         /* default */
486                         rdev->pm.profiles[PM_PROFILE_DEFAULT_IDX].dpms_off_ps_idx = rdev->pm.default_power_state_index;
487                         rdev->pm.profiles[PM_PROFILE_DEFAULT_IDX].dpms_on_ps_idx = rdev->pm.default_power_state_index;
488                         rdev->pm.profiles[PM_PROFILE_DEFAULT_IDX].dpms_off_cm_idx = 0;
489                         rdev->pm.profiles[PM_PROFILE_DEFAULT_IDX].dpms_on_cm_idx = 2;
490                         /* low sh */
491                         if (rdev->flags & RADEON_IS_MOBILITY) {
492                                 rdev->pm.profiles[PM_PROFILE_LOW_SH_IDX].dpms_off_ps_idx =
493                                         r600_pm_get_type_index(rdev, POWER_STATE_TYPE_BATTERY, 0);
494                                 rdev->pm.profiles[PM_PROFILE_LOW_SH_IDX].dpms_on_ps_idx =
495                                         r600_pm_get_type_index(rdev, POWER_STATE_TYPE_BATTERY, 0);
496                                 rdev->pm.profiles[PM_PROFILE_LOW_SH_IDX].dpms_off_cm_idx = 0;
497                                 rdev->pm.profiles[PM_PROFILE_LOW_SH_IDX].dpms_on_cm_idx = 0;
498                         } else {
499                                 rdev->pm.profiles[PM_PROFILE_LOW_SH_IDX].dpms_off_ps_idx =
500                                         r600_pm_get_type_index(rdev, POWER_STATE_TYPE_PERFORMANCE, 0);
501                                 rdev->pm.profiles[PM_PROFILE_LOW_SH_IDX].dpms_on_ps_idx =
502                                         r600_pm_get_type_index(rdev, POWER_STATE_TYPE_PERFORMANCE, 0);
503                                 rdev->pm.profiles[PM_PROFILE_LOW_SH_IDX].dpms_off_cm_idx = 0;
504                                 rdev->pm.profiles[PM_PROFILE_LOW_SH_IDX].dpms_on_cm_idx = 0;
505                         }
506                         /* mid sh */
507                         if (rdev->flags & RADEON_IS_MOBILITY) {
508                                 rdev->pm.profiles[PM_PROFILE_MID_SH_IDX].dpms_off_ps_idx =
509                                         r600_pm_get_type_index(rdev, POWER_STATE_TYPE_BATTERY, 0);
510                                 rdev->pm.profiles[PM_PROFILE_MID_SH_IDX].dpms_on_ps_idx =
511                                         r600_pm_get_type_index(rdev, POWER_STATE_TYPE_BATTERY, 0);
512                                 rdev->pm.profiles[PM_PROFILE_MID_SH_IDX].dpms_off_cm_idx = 0;
513                                 rdev->pm.profiles[PM_PROFILE_MID_SH_IDX].dpms_on_cm_idx = 1;
514                         } else {
515                                 rdev->pm.profiles[PM_PROFILE_MID_SH_IDX].dpms_off_ps_idx =
516                                         r600_pm_get_type_index(rdev, POWER_STATE_TYPE_PERFORMANCE, 0);
517                                 rdev->pm.profiles[PM_PROFILE_MID_SH_IDX].dpms_on_ps_idx =
518                                         r600_pm_get_type_index(rdev, POWER_STATE_TYPE_PERFORMANCE, 0);
519                                 rdev->pm.profiles[PM_PROFILE_MID_SH_IDX].dpms_off_cm_idx = 0;
520                                 rdev->pm.profiles[PM_PROFILE_MID_SH_IDX].dpms_on_cm_idx = 1;
521                         }
522                         /* high sh */
523                         rdev->pm.profiles[PM_PROFILE_HIGH_SH_IDX].dpms_off_ps_idx =
524                                 r600_pm_get_type_index(rdev, POWER_STATE_TYPE_PERFORMANCE, 0);
525                         rdev->pm.profiles[PM_PROFILE_HIGH_SH_IDX].dpms_on_ps_idx =
526                                 r600_pm_get_type_index(rdev, POWER_STATE_TYPE_PERFORMANCE, 0);
527                         rdev->pm.profiles[PM_PROFILE_HIGH_SH_IDX].dpms_off_cm_idx = 0;
528                         rdev->pm.profiles[PM_PROFILE_HIGH_SH_IDX].dpms_on_cm_idx = 2;
529                         /* low mh */
530                         if (rdev->flags & RADEON_IS_MOBILITY) {
531                                 rdev->pm.profiles[PM_PROFILE_LOW_MH_IDX].dpms_off_ps_idx =
532                                         r600_pm_get_type_index(rdev, POWER_STATE_TYPE_BATTERY, 1);
533                                 rdev->pm.profiles[PM_PROFILE_LOW_MH_IDX].dpms_on_ps_idx =
534                                         r600_pm_get_type_index(rdev, POWER_STATE_TYPE_BATTERY, 1);
535                                 rdev->pm.profiles[PM_PROFILE_LOW_MH_IDX].dpms_off_cm_idx = 0;
536                                 rdev->pm.profiles[PM_PROFILE_LOW_MH_IDX].dpms_on_cm_idx = 0;
537                         } else {
538                                 rdev->pm.profiles[PM_PROFILE_LOW_MH_IDX].dpms_off_ps_idx =
539                                         r600_pm_get_type_index(rdev, POWER_STATE_TYPE_PERFORMANCE, 1);
540                                 rdev->pm.profiles[PM_PROFILE_LOW_MH_IDX].dpms_on_ps_idx =
541                                         r600_pm_get_type_index(rdev, POWER_STATE_TYPE_PERFORMANCE, 1);
542                                 rdev->pm.profiles[PM_PROFILE_LOW_MH_IDX].dpms_off_cm_idx = 0;
543                                 rdev->pm.profiles[PM_PROFILE_LOW_MH_IDX].dpms_on_cm_idx = 0;
544                         }
545                         /* mid mh */
546                         if (rdev->flags & RADEON_IS_MOBILITY) {
547                                 rdev->pm.profiles[PM_PROFILE_MID_MH_IDX].dpms_off_ps_idx =
548                                         r600_pm_get_type_index(rdev, POWER_STATE_TYPE_BATTERY, 1);
549                                 rdev->pm.profiles[PM_PROFILE_MID_MH_IDX].dpms_on_ps_idx =
550                                         r600_pm_get_type_index(rdev, POWER_STATE_TYPE_BATTERY, 1);
551                                 rdev->pm.profiles[PM_PROFILE_MID_MH_IDX].dpms_off_cm_idx = 0;
552                                 rdev->pm.profiles[PM_PROFILE_MID_MH_IDX].dpms_on_cm_idx = 1;
553                         } else {
554                                 rdev->pm.profiles[PM_PROFILE_MID_MH_IDX].dpms_off_ps_idx =
555                                         r600_pm_get_type_index(rdev, POWER_STATE_TYPE_PERFORMANCE, 1);
556                                 rdev->pm.profiles[PM_PROFILE_MID_MH_IDX].dpms_on_ps_idx =
557                                         r600_pm_get_type_index(rdev, POWER_STATE_TYPE_PERFORMANCE, 1);
558                                 rdev->pm.profiles[PM_PROFILE_MID_MH_IDX].dpms_off_cm_idx = 0;
559                                 rdev->pm.profiles[PM_PROFILE_MID_MH_IDX].dpms_on_cm_idx = 1;
560                         }
561                         /* high mh */
562                         rdev->pm.profiles[PM_PROFILE_HIGH_MH_IDX].dpms_off_ps_idx =
563                                 r600_pm_get_type_index(rdev, POWER_STATE_TYPE_PERFORMANCE, 1);
564                         rdev->pm.profiles[PM_PROFILE_HIGH_MH_IDX].dpms_on_ps_idx =
565                                 r600_pm_get_type_index(rdev, POWER_STATE_TYPE_PERFORMANCE, 1);
566                         rdev->pm.profiles[PM_PROFILE_HIGH_MH_IDX].dpms_off_cm_idx = 0;
567                         rdev->pm.profiles[PM_PROFILE_HIGH_MH_IDX].dpms_on_cm_idx = 2;
568                 }
569         }
570 }
571
572 void r600_pm_misc(struct radeon_device *rdev)
573 {
574         int req_ps_idx = rdev->pm.requested_power_state_index;
575         int req_cm_idx = rdev->pm.requested_clock_mode_index;
576         struct radeon_power_state *ps = &rdev->pm.power_state[req_ps_idx];
577         struct radeon_voltage *voltage = &ps->clock_info[req_cm_idx].voltage;
578
579         if ((voltage->type == VOLTAGE_SW) && voltage->voltage) {
580                 if (voltage->voltage != rdev->pm.current_vddc) {
581                         radeon_atom_set_voltage(rdev, voltage->voltage);
582                         rdev->pm.current_vddc = voltage->voltage;
583                         DRM_DEBUG_DRIVER("Setting: v: %d\n", voltage->voltage);
584                 }
585         }
586 }
587
588 bool r600_gui_idle(struct radeon_device *rdev)
589 {
590         if (RREG32(GRBM_STATUS) & GUI_ACTIVE)
591                 return false;
592         else
593                 return true;
594 }
595
596 /* hpd for digital panel detect/disconnect */
597 bool r600_hpd_sense(struct radeon_device *rdev, enum radeon_hpd_id hpd)
598 {
599         bool connected = false;
600
601         if (ASIC_IS_DCE3(rdev)) {
602                 switch (hpd) {
603                 case RADEON_HPD_1:
604                         if (RREG32(DC_HPD1_INT_STATUS) & DC_HPDx_SENSE)
605                                 connected = true;
606                         break;
607                 case RADEON_HPD_2:
608                         if (RREG32(DC_HPD2_INT_STATUS) & DC_HPDx_SENSE)
609                                 connected = true;
610                         break;
611                 case RADEON_HPD_3:
612                         if (RREG32(DC_HPD3_INT_STATUS) & DC_HPDx_SENSE)
613                                 connected = true;
614                         break;
615                 case RADEON_HPD_4:
616                         if (RREG32(DC_HPD4_INT_STATUS) & DC_HPDx_SENSE)
617                                 connected = true;
618                         break;
619                         /* DCE 3.2 */
620                 case RADEON_HPD_5:
621                         if (RREG32(DC_HPD5_INT_STATUS) & DC_HPDx_SENSE)
622                                 connected = true;
623                         break;
624                 case RADEON_HPD_6:
625                         if (RREG32(DC_HPD6_INT_STATUS) & DC_HPDx_SENSE)
626                                 connected = true;
627                         break;
628                 default:
629                         break;
630                 }
631         } else {
632                 switch (hpd) {
633                 case RADEON_HPD_1:
634                         if (RREG32(DC_HOT_PLUG_DETECT1_INT_STATUS) & DC_HOT_PLUG_DETECTx_SENSE)
635                                 connected = true;
636                         break;
637                 case RADEON_HPD_2:
638                         if (RREG32(DC_HOT_PLUG_DETECT2_INT_STATUS) & DC_HOT_PLUG_DETECTx_SENSE)
639                                 connected = true;
640                         break;
641                 case RADEON_HPD_3:
642                         if (RREG32(DC_HOT_PLUG_DETECT3_INT_STATUS) & DC_HOT_PLUG_DETECTx_SENSE)
643                                 connected = true;
644                         break;
645                 default:
646                         break;
647                 }
648         }
649         return connected;
650 }
651
652 void r600_hpd_set_polarity(struct radeon_device *rdev,
653                            enum radeon_hpd_id hpd)
654 {
655         u32 tmp;
656         bool connected = r600_hpd_sense(rdev, hpd);
657
658         if (ASIC_IS_DCE3(rdev)) {
659                 switch (hpd) {
660                 case RADEON_HPD_1:
661                         tmp = RREG32(DC_HPD1_INT_CONTROL);
662                         if (connected)
663                                 tmp &= ~DC_HPDx_INT_POLARITY;
664                         else
665                                 tmp |= DC_HPDx_INT_POLARITY;
666                         WREG32(DC_HPD1_INT_CONTROL, tmp);
667                         break;
668                 case RADEON_HPD_2:
669                         tmp = RREG32(DC_HPD2_INT_CONTROL);
670                         if (connected)
671                                 tmp &= ~DC_HPDx_INT_POLARITY;
672                         else
673                                 tmp |= DC_HPDx_INT_POLARITY;
674                         WREG32(DC_HPD2_INT_CONTROL, tmp);
675                         break;
676                 case RADEON_HPD_3:
677                         tmp = RREG32(DC_HPD3_INT_CONTROL);
678                         if (connected)
679                                 tmp &= ~DC_HPDx_INT_POLARITY;
680                         else
681                                 tmp |= DC_HPDx_INT_POLARITY;
682                         WREG32(DC_HPD3_INT_CONTROL, tmp);
683                         break;
684                 case RADEON_HPD_4:
685                         tmp = RREG32(DC_HPD4_INT_CONTROL);
686                         if (connected)
687                                 tmp &= ~DC_HPDx_INT_POLARITY;
688                         else
689                                 tmp |= DC_HPDx_INT_POLARITY;
690                         WREG32(DC_HPD4_INT_CONTROL, tmp);
691                         break;
692                 case RADEON_HPD_5:
693                         tmp = RREG32(DC_HPD5_INT_CONTROL);
694                         if (connected)
695                                 tmp &= ~DC_HPDx_INT_POLARITY;
696                         else
697                                 tmp |= DC_HPDx_INT_POLARITY;
698                         WREG32(DC_HPD5_INT_CONTROL, tmp);
699                         break;
700                         /* DCE 3.2 */
701                 case RADEON_HPD_6:
702                         tmp = RREG32(DC_HPD6_INT_CONTROL);
703                         if (connected)
704                                 tmp &= ~DC_HPDx_INT_POLARITY;
705                         else
706                                 tmp |= DC_HPDx_INT_POLARITY;
707                         WREG32(DC_HPD6_INT_CONTROL, tmp);
708                         break;
709                 default:
710                         break;
711                 }
712         } else {
713                 switch (hpd) {
714                 case RADEON_HPD_1:
715                         tmp = RREG32(DC_HOT_PLUG_DETECT1_INT_CONTROL);
716                         if (connected)
717                                 tmp &= ~DC_HOT_PLUG_DETECTx_INT_POLARITY;
718                         else
719                                 tmp |= DC_HOT_PLUG_DETECTx_INT_POLARITY;
720                         WREG32(DC_HOT_PLUG_DETECT1_INT_CONTROL, tmp);
721                         break;
722                 case RADEON_HPD_2:
723                         tmp = RREG32(DC_HOT_PLUG_DETECT2_INT_CONTROL);
724                         if (connected)
725                                 tmp &= ~DC_HOT_PLUG_DETECTx_INT_POLARITY;
726                         else
727                                 tmp |= DC_HOT_PLUG_DETECTx_INT_POLARITY;
728                         WREG32(DC_HOT_PLUG_DETECT2_INT_CONTROL, tmp);
729                         break;
730                 case RADEON_HPD_3:
731                         tmp = RREG32(DC_HOT_PLUG_DETECT3_INT_CONTROL);
732                         if (connected)
733                                 tmp &= ~DC_HOT_PLUG_DETECTx_INT_POLARITY;
734                         else
735                                 tmp |= DC_HOT_PLUG_DETECTx_INT_POLARITY;
736                         WREG32(DC_HOT_PLUG_DETECT3_INT_CONTROL, tmp);
737                         break;
738                 default:
739                         break;
740                 }
741         }
742 }
743
744 void r600_hpd_init(struct radeon_device *rdev)
745 {
746         struct drm_device *dev = rdev->ddev;
747         struct drm_connector *connector;
748
749         if (ASIC_IS_DCE3(rdev)) {
750                 u32 tmp = DC_HPDx_CONNECTION_TIMER(0x9c4) | DC_HPDx_RX_INT_TIMER(0xfa);
751                 if (ASIC_IS_DCE32(rdev))
752                         tmp |= DC_HPDx_EN;
753
754                 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
755                         struct radeon_connector *radeon_connector = to_radeon_connector(connector);
756                         switch (radeon_connector->hpd.hpd) {
757                         case RADEON_HPD_1:
758                                 WREG32(DC_HPD1_CONTROL, tmp);
759                                 rdev->irq.hpd[0] = true;
760                                 break;
761                         case RADEON_HPD_2:
762                                 WREG32(DC_HPD2_CONTROL, tmp);
763                                 rdev->irq.hpd[1] = true;
764                                 break;
765                         case RADEON_HPD_3:
766                                 WREG32(DC_HPD3_CONTROL, tmp);
767                                 rdev->irq.hpd[2] = true;
768                                 break;
769                         case RADEON_HPD_4:
770                                 WREG32(DC_HPD4_CONTROL, tmp);
771                                 rdev->irq.hpd[3] = true;
772                                 break;
773                                 /* DCE 3.2 */
774                         case RADEON_HPD_5:
775                                 WREG32(DC_HPD5_CONTROL, tmp);
776                                 rdev->irq.hpd[4] = true;
777                                 break;
778                         case RADEON_HPD_6:
779                                 WREG32(DC_HPD6_CONTROL, tmp);
780                                 rdev->irq.hpd[5] = true;
781                                 break;
782                         default:
783                                 break;
784                         }
785                 }
786         } else {
787                 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
788                         struct radeon_connector *radeon_connector = to_radeon_connector(connector);
789                         switch (radeon_connector->hpd.hpd) {
790                         case RADEON_HPD_1:
791                                 WREG32(DC_HOT_PLUG_DETECT1_CONTROL, DC_HOT_PLUG_DETECTx_EN);
792                                 rdev->irq.hpd[0] = true;
793                                 break;
794                         case RADEON_HPD_2:
795                                 WREG32(DC_HOT_PLUG_DETECT2_CONTROL, DC_HOT_PLUG_DETECTx_EN);
796                                 rdev->irq.hpd[1] = true;
797                                 break;
798                         case RADEON_HPD_3:
799                                 WREG32(DC_HOT_PLUG_DETECT3_CONTROL, DC_HOT_PLUG_DETECTx_EN);
800                                 rdev->irq.hpd[2] = true;
801                                 break;
802                         default:
803                                 break;
804                         }
805                 }
806         }
807         if (rdev->irq.installed)
808                 r600_irq_set(rdev);
809 }
810
811 void r600_hpd_fini(struct radeon_device *rdev)
812 {
813         struct drm_device *dev = rdev->ddev;
814         struct drm_connector *connector;
815
816         if (ASIC_IS_DCE3(rdev)) {
817                 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
818                         struct radeon_connector *radeon_connector = to_radeon_connector(connector);
819                         switch (radeon_connector->hpd.hpd) {
820                         case RADEON_HPD_1:
821                                 WREG32(DC_HPD1_CONTROL, 0);
822                                 rdev->irq.hpd[0] = false;
823                                 break;
824                         case RADEON_HPD_2:
825                                 WREG32(DC_HPD2_CONTROL, 0);
826                                 rdev->irq.hpd[1] = false;
827                                 break;
828                         case RADEON_HPD_3:
829                                 WREG32(DC_HPD3_CONTROL, 0);
830                                 rdev->irq.hpd[2] = false;
831                                 break;
832                         case RADEON_HPD_4:
833                                 WREG32(DC_HPD4_CONTROL, 0);
834                                 rdev->irq.hpd[3] = false;
835                                 break;
836                                 /* DCE 3.2 */
837                         case RADEON_HPD_5:
838                                 WREG32(DC_HPD5_CONTROL, 0);
839                                 rdev->irq.hpd[4] = false;
840                                 break;
841                         case RADEON_HPD_6:
842                                 WREG32(DC_HPD6_CONTROL, 0);
843                                 rdev->irq.hpd[5] = false;
844                                 break;
845                         default:
846                                 break;
847                         }
848                 }
849         } else {
850                 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
851                         struct radeon_connector *radeon_connector = to_radeon_connector(connector);
852                         switch (radeon_connector->hpd.hpd) {
853                         case RADEON_HPD_1:
854                                 WREG32(DC_HOT_PLUG_DETECT1_CONTROL, 0);
855                                 rdev->irq.hpd[0] = false;
856                                 break;
857                         case RADEON_HPD_2:
858                                 WREG32(DC_HOT_PLUG_DETECT2_CONTROL, 0);
859                                 rdev->irq.hpd[1] = false;
860                                 break;
861                         case RADEON_HPD_3:
862                                 WREG32(DC_HOT_PLUG_DETECT3_CONTROL, 0);
863                                 rdev->irq.hpd[2] = false;
864                                 break;
865                         default:
866                                 break;
867                         }
868                 }
869         }
870 }
871
872 /*
873  * R600 PCIE GART
874  */
875 void r600_pcie_gart_tlb_flush(struct radeon_device *rdev)
876 {
877         unsigned i;
878         u32 tmp;
879
880         /* flush hdp cache so updates hit vram */
881         if ((rdev->family >= CHIP_RV770) && (rdev->family <= CHIP_RV740)) {
882                 void __iomem *ptr = (void *)rdev->gart.table.vram.ptr;
883                 u32 tmp;
884
885                 /* r7xx hw bug.  write to HDP_DEBUG1 followed by fb read
886                  * rather than write to HDP_REG_COHERENCY_FLUSH_CNTL
887                  */
888                 WREG32(HDP_DEBUG1, 0);
889                 tmp = readl((void __iomem *)ptr);
890         } else
891                 WREG32(R_005480_HDP_MEM_COHERENCY_FLUSH_CNTL, 0x1);
892
893         WREG32(VM_CONTEXT0_INVALIDATION_LOW_ADDR, rdev->mc.gtt_start >> 12);
894         WREG32(VM_CONTEXT0_INVALIDATION_HIGH_ADDR, (rdev->mc.gtt_end - 1) >> 12);
895         WREG32(VM_CONTEXT0_REQUEST_RESPONSE, REQUEST_TYPE(1));
896         for (i = 0; i < rdev->usec_timeout; i++) {
897                 /* read MC_STATUS */
898                 tmp = RREG32(VM_CONTEXT0_REQUEST_RESPONSE);
899                 tmp = (tmp & RESPONSE_TYPE_MASK) >> RESPONSE_TYPE_SHIFT;
900                 if (tmp == 2) {
901                         printk(KERN_WARNING "[drm] r600 flush TLB failed\n");
902                         return;
903                 }
904                 if (tmp) {
905                         return;
906                 }
907                 udelay(1);
908         }
909 }
910
911 int r600_pcie_gart_init(struct radeon_device *rdev)
912 {
913         int r;
914
915         if (rdev->gart.table.vram.robj) {
916                 WARN(1, "R600 PCIE GART already initialized\n");
917                 return 0;
918         }
919         /* Initialize common gart structure */
920         r = radeon_gart_init(rdev);
921         if (r)
922                 return r;
923         rdev->gart.table_size = rdev->gart.num_gpu_pages * 8;
924         return radeon_gart_table_vram_alloc(rdev);
925 }
926
927 int r600_pcie_gart_enable(struct radeon_device *rdev)
928 {
929         u32 tmp;
930         int r, i;
931
932         if (rdev->gart.table.vram.robj == NULL) {
933                 dev_err(rdev->dev, "No VRAM object for PCIE GART.\n");
934                 return -EINVAL;
935         }
936         r = radeon_gart_table_vram_pin(rdev);
937         if (r)
938                 return r;
939         radeon_gart_restore(rdev);
940
941         /* Setup L2 cache */
942         WREG32(VM_L2_CNTL, ENABLE_L2_CACHE | ENABLE_L2_FRAGMENT_PROCESSING |
943                                 ENABLE_L2_PTE_CACHE_LRU_UPDATE_BY_WRITE |
944                                 EFFECTIVE_L2_QUEUE_SIZE(7));
945         WREG32(VM_L2_CNTL2, 0);
946         WREG32(VM_L2_CNTL3, BANK_SELECT_0(0) | BANK_SELECT_1(1));
947         /* Setup TLB control */
948         tmp = ENABLE_L1_TLB | ENABLE_L1_FRAGMENT_PROCESSING |
949                 SYSTEM_ACCESS_MODE_NOT_IN_SYS |
950                 EFFECTIVE_L1_TLB_SIZE(5) | EFFECTIVE_L1_QUEUE_SIZE(5) |
951                 ENABLE_WAIT_L2_QUERY;
952         WREG32(MC_VM_L1_TLB_MCB_RD_SYS_CNTL, tmp);
953         WREG32(MC_VM_L1_TLB_MCB_WR_SYS_CNTL, tmp);
954         WREG32(MC_VM_L1_TLB_MCB_RD_HDP_CNTL, tmp | ENABLE_L1_STRICT_ORDERING);
955         WREG32(MC_VM_L1_TLB_MCB_WR_HDP_CNTL, tmp);
956         WREG32(MC_VM_L1_TLB_MCD_RD_A_CNTL, tmp);
957         WREG32(MC_VM_L1_TLB_MCD_WR_A_CNTL, tmp);
958         WREG32(MC_VM_L1_TLB_MCD_RD_B_CNTL, tmp);
959         WREG32(MC_VM_L1_TLB_MCD_WR_B_CNTL, tmp);
960         WREG32(MC_VM_L1_TLB_MCB_RD_GFX_CNTL, tmp);
961         WREG32(MC_VM_L1_TLB_MCB_WR_GFX_CNTL, tmp);
962         WREG32(MC_VM_L1_TLB_MCB_RD_PDMA_CNTL, tmp);
963         WREG32(MC_VM_L1_TLB_MCB_WR_PDMA_CNTL, tmp);
964         WREG32(MC_VM_L1_TLB_MCB_RD_SEM_CNTL, tmp | ENABLE_SEMAPHORE_MODE);
965         WREG32(MC_VM_L1_TLB_MCB_WR_SEM_CNTL, tmp | ENABLE_SEMAPHORE_MODE);
966         WREG32(VM_CONTEXT0_PAGE_TABLE_START_ADDR, rdev->mc.gtt_start >> 12);
967         WREG32(VM_CONTEXT0_PAGE_TABLE_END_ADDR, rdev->mc.gtt_end >> 12);
968         WREG32(VM_CONTEXT0_PAGE_TABLE_BASE_ADDR, rdev->gart.table_addr >> 12);
969         WREG32(VM_CONTEXT0_CNTL, ENABLE_CONTEXT | PAGE_TABLE_DEPTH(0) |
970                                 RANGE_PROTECTION_FAULT_ENABLE_DEFAULT);
971         WREG32(VM_CONTEXT0_PROTECTION_FAULT_DEFAULT_ADDR,
972                         (u32)(rdev->dummy_page.addr >> 12));
973         for (i = 1; i < 7; i++)
974                 WREG32(VM_CONTEXT0_CNTL + (i * 4), 0);
975
976         r600_pcie_gart_tlb_flush(rdev);
977         rdev->gart.ready = true;
978         return 0;
979 }
980
981 void r600_pcie_gart_disable(struct radeon_device *rdev)
982 {
983         u32 tmp;
984         int i, r;
985
986         /* Disable all tables */
987         for (i = 0; i < 7; i++)
988                 WREG32(VM_CONTEXT0_CNTL + (i * 4), 0);
989
990         /* Disable L2 cache */
991         WREG32(VM_L2_CNTL, ENABLE_L2_FRAGMENT_PROCESSING |
992                                 EFFECTIVE_L2_QUEUE_SIZE(7));
993         WREG32(VM_L2_CNTL3, BANK_SELECT_0(0) | BANK_SELECT_1(1));
994         /* Setup L1 TLB control */
995         tmp = EFFECTIVE_L1_TLB_SIZE(5) | EFFECTIVE_L1_QUEUE_SIZE(5) |
996                 ENABLE_WAIT_L2_QUERY;
997         WREG32(MC_VM_L1_TLB_MCD_RD_A_CNTL, tmp);
998         WREG32(MC_VM_L1_TLB_MCD_WR_A_CNTL, tmp);
999         WREG32(MC_VM_L1_TLB_MCD_RD_B_CNTL, tmp);
1000         WREG32(MC_VM_L1_TLB_MCD_WR_B_CNTL, tmp);
1001         WREG32(MC_VM_L1_TLB_MCB_RD_GFX_CNTL, tmp);
1002         WREG32(MC_VM_L1_TLB_MCB_WR_GFX_CNTL, tmp);
1003         WREG32(MC_VM_L1_TLB_MCB_RD_PDMA_CNTL, tmp);
1004         WREG32(MC_VM_L1_TLB_MCB_WR_PDMA_CNTL, tmp);
1005         WREG32(MC_VM_L1_TLB_MCB_RD_SEM_CNTL, tmp);
1006         WREG32(MC_VM_L1_TLB_MCB_WR_SEM_CNTL, tmp);
1007         WREG32(MC_VM_L1_TLB_MCB_RD_SYS_CNTL, tmp);
1008         WREG32(MC_VM_L1_TLB_MCB_WR_SYS_CNTL, tmp);
1009         WREG32(MC_VM_L1_TLB_MCB_RD_HDP_CNTL, tmp);
1010         WREG32(MC_VM_L1_TLB_MCB_WR_HDP_CNTL, tmp);
1011         if (rdev->gart.table.vram.robj) {
1012                 r = radeon_bo_reserve(rdev->gart.table.vram.robj, false);
1013                 if (likely(r == 0)) {
1014                         radeon_bo_kunmap(rdev->gart.table.vram.robj);
1015                         radeon_bo_unpin(rdev->gart.table.vram.robj);
1016                         radeon_bo_unreserve(rdev->gart.table.vram.robj);
1017                 }
1018         }
1019 }
1020
1021 void r600_pcie_gart_fini(struct radeon_device *rdev)
1022 {
1023         radeon_gart_fini(rdev);
1024         r600_pcie_gart_disable(rdev);
1025         radeon_gart_table_vram_free(rdev);
1026 }
1027
1028 void r600_agp_enable(struct radeon_device *rdev)
1029 {
1030         u32 tmp;
1031         int i;
1032
1033         /* Setup L2 cache */
1034         WREG32(VM_L2_CNTL, ENABLE_L2_CACHE | ENABLE_L2_FRAGMENT_PROCESSING |
1035                                 ENABLE_L2_PTE_CACHE_LRU_UPDATE_BY_WRITE |
1036                                 EFFECTIVE_L2_QUEUE_SIZE(7));
1037         WREG32(VM_L2_CNTL2, 0);
1038         WREG32(VM_L2_CNTL3, BANK_SELECT_0(0) | BANK_SELECT_1(1));
1039         /* Setup TLB control */
1040         tmp = ENABLE_L1_TLB | ENABLE_L1_FRAGMENT_PROCESSING |
1041                 SYSTEM_ACCESS_MODE_NOT_IN_SYS |
1042                 EFFECTIVE_L1_TLB_SIZE(5) | EFFECTIVE_L1_QUEUE_SIZE(5) |
1043                 ENABLE_WAIT_L2_QUERY;
1044         WREG32(MC_VM_L1_TLB_MCB_RD_SYS_CNTL, tmp);
1045         WREG32(MC_VM_L1_TLB_MCB_WR_SYS_CNTL, tmp);
1046         WREG32(MC_VM_L1_TLB_MCB_RD_HDP_CNTL, tmp | ENABLE_L1_STRICT_ORDERING);
1047         WREG32(MC_VM_L1_TLB_MCB_WR_HDP_CNTL, tmp);
1048         WREG32(MC_VM_L1_TLB_MCD_RD_A_CNTL, tmp);
1049         WREG32(MC_VM_L1_TLB_MCD_WR_A_CNTL, tmp);
1050         WREG32(MC_VM_L1_TLB_MCD_RD_B_CNTL, tmp);
1051         WREG32(MC_VM_L1_TLB_MCD_WR_B_CNTL, tmp);
1052         WREG32(MC_VM_L1_TLB_MCB_RD_GFX_CNTL, tmp);
1053         WREG32(MC_VM_L1_TLB_MCB_WR_GFX_CNTL, tmp);
1054         WREG32(MC_VM_L1_TLB_MCB_RD_PDMA_CNTL, tmp);
1055         WREG32(MC_VM_L1_TLB_MCB_WR_PDMA_CNTL, tmp);
1056         WREG32(MC_VM_L1_TLB_MCB_RD_SEM_CNTL, tmp | ENABLE_SEMAPHORE_MODE);
1057         WREG32(MC_VM_L1_TLB_MCB_WR_SEM_CNTL, tmp | ENABLE_SEMAPHORE_MODE);
1058         for (i = 0; i < 7; i++)
1059                 WREG32(VM_CONTEXT0_CNTL + (i * 4), 0);
1060 }
1061
1062 int r600_mc_wait_for_idle(struct radeon_device *rdev)
1063 {
1064         unsigned i;
1065         u32 tmp;
1066
1067         for (i = 0; i < rdev->usec_timeout; i++) {
1068                 /* read MC_STATUS */
1069                 tmp = RREG32(R_000E50_SRBM_STATUS) & 0x3F00;
1070                 if (!tmp)
1071                         return 0;
1072                 udelay(1);
1073         }
1074         return -1;
1075 }
1076
1077 static void r600_mc_program(struct radeon_device *rdev)
1078 {
1079         struct rv515_mc_save save;
1080         u32 tmp;
1081         int i, j;
1082
1083         /* Initialize HDP */
1084         for (i = 0, j = 0; i < 32; i++, j += 0x18) {
1085                 WREG32((0x2c14 + j), 0x00000000);
1086                 WREG32((0x2c18 + j), 0x00000000);
1087                 WREG32((0x2c1c + j), 0x00000000);
1088                 WREG32((0x2c20 + j), 0x00000000);
1089                 WREG32((0x2c24 + j), 0x00000000);
1090         }
1091         WREG32(HDP_REG_COHERENCY_FLUSH_CNTL, 0);
1092
1093         rv515_mc_stop(rdev, &save);
1094         if (r600_mc_wait_for_idle(rdev)) {
1095                 dev_warn(rdev->dev, "Wait for MC idle timedout !\n");
1096         }
1097         /* Lockout access through VGA aperture (doesn't exist before R600) */
1098         WREG32(VGA_HDP_CONTROL, VGA_MEMORY_DISABLE);
1099         /* Update configuration */
1100         if (rdev->flags & RADEON_IS_AGP) {
1101                 if (rdev->mc.vram_start < rdev->mc.gtt_start) {
1102                         /* VRAM before AGP */
1103                         WREG32(MC_VM_SYSTEM_APERTURE_LOW_ADDR,
1104                                 rdev->mc.vram_start >> 12);
1105                         WREG32(MC_VM_SYSTEM_APERTURE_HIGH_ADDR,
1106                                 rdev->mc.gtt_end >> 12);
1107                 } else {
1108                         /* VRAM after AGP */
1109                         WREG32(MC_VM_SYSTEM_APERTURE_LOW_ADDR,
1110                                 rdev->mc.gtt_start >> 12);
1111                         WREG32(MC_VM_SYSTEM_APERTURE_HIGH_ADDR,
1112                                 rdev->mc.vram_end >> 12);
1113                 }
1114         } else {
1115                 WREG32(MC_VM_SYSTEM_APERTURE_LOW_ADDR, rdev->mc.vram_start >> 12);
1116                 WREG32(MC_VM_SYSTEM_APERTURE_HIGH_ADDR, rdev->mc.vram_end >> 12);
1117         }
1118         WREG32(MC_VM_SYSTEM_APERTURE_DEFAULT_ADDR, 0);
1119         tmp = ((rdev->mc.vram_end >> 24) & 0xFFFF) << 16;
1120         tmp |= ((rdev->mc.vram_start >> 24) & 0xFFFF);
1121         WREG32(MC_VM_FB_LOCATION, tmp);
1122         WREG32(HDP_NONSURFACE_BASE, (rdev->mc.vram_start >> 8));
1123         WREG32(HDP_NONSURFACE_INFO, (2 << 7));
1124         WREG32(HDP_NONSURFACE_SIZE, 0x3FFFFFFF);
1125         if (rdev->flags & RADEON_IS_AGP) {
1126                 WREG32(MC_VM_AGP_TOP, rdev->mc.gtt_end >> 22);
1127                 WREG32(MC_VM_AGP_BOT, rdev->mc.gtt_start >> 22);
1128                 WREG32(MC_VM_AGP_BASE, rdev->mc.agp_base >> 22);
1129         } else {
1130                 WREG32(MC_VM_AGP_BASE, 0);
1131                 WREG32(MC_VM_AGP_TOP, 0x0FFFFFFF);
1132                 WREG32(MC_VM_AGP_BOT, 0x0FFFFFFF);
1133         }
1134         if (r600_mc_wait_for_idle(rdev)) {
1135                 dev_warn(rdev->dev, "Wait for MC idle timedout !\n");
1136         }
1137         rv515_mc_resume(rdev, &save);
1138         /* we need to own VRAM, so turn off the VGA renderer here
1139          * to stop it overwriting our objects */
1140         rv515_vga_render_disable(rdev);
1141 }
1142
1143 /**
1144  * r600_vram_gtt_location - try to find VRAM & GTT location
1145  * @rdev: radeon device structure holding all necessary informations
1146  * @mc: memory controller structure holding memory informations
1147  *
1148  * Function will place try to place VRAM at same place as in CPU (PCI)
1149  * address space as some GPU seems to have issue when we reprogram at
1150  * different address space.
1151  *
1152  * If there is not enough space to fit the unvisible VRAM after the
1153  * aperture then we limit the VRAM size to the aperture.
1154  *
1155  * If we are using AGP then place VRAM adjacent to AGP aperture are we need
1156  * them to be in one from GPU point of view so that we can program GPU to
1157  * catch access outside them (weird GPU policy see ??).
1158  *
1159  * This function will never fails, worst case are limiting VRAM or GTT.
1160  *
1161  * Note: GTT start, end, size should be initialized before calling this
1162  * function on AGP platform.
1163  */
1164 void r600_vram_gtt_location(struct radeon_device *rdev, struct radeon_mc *mc)
1165 {
1166         u64 size_bf, size_af;
1167
1168         if (mc->mc_vram_size > 0xE0000000) {
1169                 /* leave room for at least 512M GTT */
1170                 dev_warn(rdev->dev, "limiting VRAM\n");
1171                 mc->real_vram_size = 0xE0000000;
1172                 mc->mc_vram_size = 0xE0000000;
1173         }
1174         if (rdev->flags & RADEON_IS_AGP) {
1175                 size_bf = mc->gtt_start;
1176                 size_af = 0xFFFFFFFF - mc->gtt_end + 1;
1177                 if (size_bf > size_af) {
1178                         if (mc->mc_vram_size > size_bf) {
1179                                 dev_warn(rdev->dev, "limiting VRAM\n");
1180                                 mc->real_vram_size = size_bf;
1181                                 mc->mc_vram_size = size_bf;
1182                         }
1183                         mc->vram_start = mc->gtt_start - mc->mc_vram_size;
1184                 } else {
1185                         if (mc->mc_vram_size > size_af) {
1186                                 dev_warn(rdev->dev, "limiting VRAM\n");
1187                                 mc->real_vram_size = size_af;
1188                                 mc->mc_vram_size = size_af;
1189                         }
1190                         mc->vram_start = mc->gtt_end;
1191                 }
1192                 mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
1193                 dev_info(rdev->dev, "VRAM: %lluM 0x%08llX - 0x%08llX (%lluM used)\n",
1194                                 mc->mc_vram_size >> 20, mc->vram_start,
1195                                 mc->vram_end, mc->real_vram_size >> 20);
1196         } else {
1197                 u64 base = 0;
1198                 if (rdev->flags & RADEON_IS_IGP) {
1199                         base = RREG32(MC_VM_FB_LOCATION) & 0xFFFF;
1200                         base <<= 24;
1201                 }
1202                 radeon_vram_location(rdev, &rdev->mc, base);
1203                 rdev->mc.gtt_base_align = 0;
1204                 radeon_gtt_location(rdev, mc);
1205         }
1206 }
1207
1208 int r600_mc_init(struct radeon_device *rdev)
1209 {
1210         u32 tmp;
1211         int chansize, numchan;
1212
1213         /* Get VRAM informations */
1214         rdev->mc.vram_is_ddr = true;
1215         tmp = RREG32(RAMCFG);
1216         if (tmp & CHANSIZE_OVERRIDE) {
1217                 chansize = 16;
1218         } else if (tmp & CHANSIZE_MASK) {
1219                 chansize = 64;
1220         } else {
1221                 chansize = 32;
1222         }
1223         tmp = RREG32(CHMAP);
1224         switch ((tmp & NOOFCHAN_MASK) >> NOOFCHAN_SHIFT) {
1225         case 0:
1226         default:
1227                 numchan = 1;
1228                 break;
1229         case 1:
1230                 numchan = 2;
1231                 break;
1232         case 2:
1233                 numchan = 4;
1234                 break;
1235         case 3:
1236                 numchan = 8;
1237                 break;
1238         }
1239         rdev->mc.vram_width = numchan * chansize;
1240         /* Could aper size report 0 ? */
1241         rdev->mc.aper_base = pci_resource_start(rdev->pdev, 0);
1242         rdev->mc.aper_size = pci_resource_len(rdev->pdev, 0);
1243         /* Setup GPU memory space */
1244         rdev->mc.mc_vram_size = RREG32(CONFIG_MEMSIZE);
1245         rdev->mc.real_vram_size = RREG32(CONFIG_MEMSIZE);
1246         rdev->mc.visible_vram_size = rdev->mc.aper_size;
1247         rdev->mc.active_vram_size = rdev->mc.visible_vram_size;
1248         r600_vram_gtt_location(rdev, &rdev->mc);
1249
1250         if (rdev->flags & RADEON_IS_IGP) {
1251                 rs690_pm_info(rdev);
1252                 rdev->mc.igp_sideport_enabled = radeon_atombios_sideport_present(rdev);
1253         }
1254         radeon_update_bandwidth_info(rdev);
1255         return 0;
1256 }
1257
1258 /* We doesn't check that the GPU really needs a reset we simply do the
1259  * reset, it's up to the caller to determine if the GPU needs one. We
1260  * might add an helper function to check that.
1261  */
1262 int r600_gpu_soft_reset(struct radeon_device *rdev)
1263 {
1264         struct rv515_mc_save save;
1265         u32 grbm_busy_mask = S_008010_VC_BUSY(1) | S_008010_VGT_BUSY_NO_DMA(1) |
1266                                 S_008010_VGT_BUSY(1) | S_008010_TA03_BUSY(1) |
1267                                 S_008010_TC_BUSY(1) | S_008010_SX_BUSY(1) |
1268                                 S_008010_SH_BUSY(1) | S_008010_SPI03_BUSY(1) |
1269                                 S_008010_SMX_BUSY(1) | S_008010_SC_BUSY(1) |
1270                                 S_008010_PA_BUSY(1) | S_008010_DB03_BUSY(1) |
1271                                 S_008010_CR_BUSY(1) | S_008010_CB03_BUSY(1) |
1272                                 S_008010_GUI_ACTIVE(1);
1273         u32 grbm2_busy_mask = S_008014_SPI0_BUSY(1) | S_008014_SPI1_BUSY(1) |
1274                         S_008014_SPI2_BUSY(1) | S_008014_SPI3_BUSY(1) |
1275                         S_008014_TA0_BUSY(1) | S_008014_TA1_BUSY(1) |
1276                         S_008014_TA2_BUSY(1) | S_008014_TA3_BUSY(1) |
1277                         S_008014_DB0_BUSY(1) | S_008014_DB1_BUSY(1) |
1278                         S_008014_DB2_BUSY(1) | S_008014_DB3_BUSY(1) |
1279                         S_008014_CB0_BUSY(1) | S_008014_CB1_BUSY(1) |
1280                         S_008014_CB2_BUSY(1) | S_008014_CB3_BUSY(1);
1281         u32 tmp;
1282
1283         dev_info(rdev->dev, "GPU softreset \n");
1284         dev_info(rdev->dev, "  R_008010_GRBM_STATUS=0x%08X\n",
1285                 RREG32(R_008010_GRBM_STATUS));
1286         dev_info(rdev->dev, "  R_008014_GRBM_STATUS2=0x%08X\n",
1287                 RREG32(R_008014_GRBM_STATUS2));
1288         dev_info(rdev->dev, "  R_000E50_SRBM_STATUS=0x%08X\n",
1289                 RREG32(R_000E50_SRBM_STATUS));
1290         rv515_mc_stop(rdev, &save);
1291         if (r600_mc_wait_for_idle(rdev)) {
1292                 dev_warn(rdev->dev, "Wait for MC idle timedout !\n");
1293         }
1294         /* Disable CP parsing/prefetching */
1295         WREG32(R_0086D8_CP_ME_CNTL, S_0086D8_CP_ME_HALT(1));
1296         /* Check if any of the rendering block is busy and reset it */
1297         if ((RREG32(R_008010_GRBM_STATUS) & grbm_busy_mask) ||
1298             (RREG32(R_008014_GRBM_STATUS2) & grbm2_busy_mask)) {
1299                 tmp = S_008020_SOFT_RESET_CR(1) |
1300                         S_008020_SOFT_RESET_DB(1) |
1301                         S_008020_SOFT_RESET_CB(1) |
1302                         S_008020_SOFT_RESET_PA(1) |
1303                         S_008020_SOFT_RESET_SC(1) |
1304                         S_008020_SOFT_RESET_SMX(1) |
1305                         S_008020_SOFT_RESET_SPI(1) |
1306                         S_008020_SOFT_RESET_SX(1) |
1307                         S_008020_SOFT_RESET_SH(1) |
1308                         S_008020_SOFT_RESET_TC(1) |
1309                         S_008020_SOFT_RESET_TA(1) |
1310                         S_008020_SOFT_RESET_VC(1) |
1311                         S_008020_SOFT_RESET_VGT(1);
1312                 dev_info(rdev->dev, "  R_008020_GRBM_SOFT_RESET=0x%08X\n", tmp);
1313                 WREG32(R_008020_GRBM_SOFT_RESET, tmp);
1314                 RREG32(R_008020_GRBM_SOFT_RESET);
1315                 mdelay(15);
1316                 WREG32(R_008020_GRBM_SOFT_RESET, 0);
1317         }
1318         /* Reset CP (we always reset CP) */
1319         tmp = S_008020_SOFT_RESET_CP(1);
1320         dev_info(rdev->dev, "R_008020_GRBM_SOFT_RESET=0x%08X\n", tmp);
1321         WREG32(R_008020_GRBM_SOFT_RESET, tmp);
1322         RREG32(R_008020_GRBM_SOFT_RESET);
1323         mdelay(15);
1324         WREG32(R_008020_GRBM_SOFT_RESET, 0);
1325         /* Wait a little for things to settle down */
1326         mdelay(1);
1327         dev_info(rdev->dev, "  R_008010_GRBM_STATUS=0x%08X\n",
1328                 RREG32(R_008010_GRBM_STATUS));
1329         dev_info(rdev->dev, "  R_008014_GRBM_STATUS2=0x%08X\n",
1330                 RREG32(R_008014_GRBM_STATUS2));
1331         dev_info(rdev->dev, "  R_000E50_SRBM_STATUS=0x%08X\n",
1332                 RREG32(R_000E50_SRBM_STATUS));
1333         rv515_mc_resume(rdev, &save);
1334         return 0;
1335 }
1336
1337 bool r600_gpu_is_lockup(struct radeon_device *rdev)
1338 {
1339         u32 srbm_status;
1340         u32 grbm_status;
1341         u32 grbm_status2;
1342         int r;
1343
1344         srbm_status = RREG32(R_000E50_SRBM_STATUS);
1345         grbm_status = RREG32(R_008010_GRBM_STATUS);
1346         grbm_status2 = RREG32(R_008014_GRBM_STATUS2);
1347         if (!G_008010_GUI_ACTIVE(grbm_status)) {
1348                 r100_gpu_lockup_update(&rdev->config.r300.lockup, &rdev->cp);
1349                 return false;
1350         }
1351         /* force CP activities */
1352         r = radeon_ring_lock(rdev, 2);
1353         if (!r) {
1354                 /* PACKET2 NOP */
1355                 radeon_ring_write(rdev, 0x80000000);
1356                 radeon_ring_write(rdev, 0x80000000);
1357                 radeon_ring_unlock_commit(rdev);
1358         }
1359         rdev->cp.rptr = RREG32(R600_CP_RB_RPTR);
1360         return r100_gpu_cp_is_lockup(rdev, &rdev->config.r300.lockup, &rdev->cp);
1361 }
1362
1363 int r600_asic_reset(struct radeon_device *rdev)
1364 {
1365         return r600_gpu_soft_reset(rdev);
1366 }
1367
1368 static u32 r600_get_tile_pipe_to_backend_map(u32 num_tile_pipes,
1369                                              u32 num_backends,
1370                                              u32 backend_disable_mask)
1371 {
1372         u32 backend_map = 0;
1373         u32 enabled_backends_mask;
1374         u32 enabled_backends_count;
1375         u32 cur_pipe;
1376         u32 swizzle_pipe[R6XX_MAX_PIPES];
1377         u32 cur_backend;
1378         u32 i;
1379
1380         if (num_tile_pipes > R6XX_MAX_PIPES)
1381                 num_tile_pipes = R6XX_MAX_PIPES;
1382         if (num_tile_pipes < 1)
1383                 num_tile_pipes = 1;
1384         if (num_backends > R6XX_MAX_BACKENDS)
1385                 num_backends = R6XX_MAX_BACKENDS;
1386         if (num_backends < 1)
1387                 num_backends = 1;
1388
1389         enabled_backends_mask = 0;
1390         enabled_backends_count = 0;
1391         for (i = 0; i < R6XX_MAX_BACKENDS; ++i) {
1392                 if (((backend_disable_mask >> i) & 1) == 0) {
1393                         enabled_backends_mask |= (1 << i);
1394                         ++enabled_backends_count;
1395                 }
1396                 if (enabled_backends_count == num_backends)
1397                         break;
1398         }
1399
1400         if (enabled_backends_count == 0) {
1401                 enabled_backends_mask = 1;
1402                 enabled_backends_count = 1;
1403         }
1404
1405         if (enabled_backends_count != num_backends)
1406                 num_backends = enabled_backends_count;
1407
1408         memset((uint8_t *)&swizzle_pipe[0], 0, sizeof(u32) * R6XX_MAX_PIPES);
1409         switch (num_tile_pipes) {
1410         case 1:
1411                 swizzle_pipe[0] = 0;
1412                 break;
1413         case 2:
1414                 swizzle_pipe[0] = 0;
1415                 swizzle_pipe[1] = 1;
1416                 break;
1417         case 3:
1418                 swizzle_pipe[0] = 0;
1419                 swizzle_pipe[1] = 1;
1420                 swizzle_pipe[2] = 2;
1421                 break;
1422         case 4:
1423                 swizzle_pipe[0] = 0;
1424                 swizzle_pipe[1] = 1;
1425                 swizzle_pipe[2] = 2;
1426                 swizzle_pipe[3] = 3;
1427                 break;
1428         case 5:
1429                 swizzle_pipe[0] = 0;
1430                 swizzle_pipe[1] = 1;
1431                 swizzle_pipe[2] = 2;
1432                 swizzle_pipe[3] = 3;
1433                 swizzle_pipe[4] = 4;
1434                 break;
1435         case 6:
1436                 swizzle_pipe[0] = 0;
1437                 swizzle_pipe[1] = 2;
1438                 swizzle_pipe[2] = 4;
1439                 swizzle_pipe[3] = 5;
1440                 swizzle_pipe[4] = 1;
1441                 swizzle_pipe[5] = 3;
1442                 break;
1443         case 7:
1444                 swizzle_pipe[0] = 0;
1445                 swizzle_pipe[1] = 2;
1446                 swizzle_pipe[2] = 4;
1447                 swizzle_pipe[3] = 6;
1448                 swizzle_pipe[4] = 1;
1449                 swizzle_pipe[5] = 3;
1450                 swizzle_pipe[6] = 5;
1451                 break;
1452         case 8:
1453                 swizzle_pipe[0] = 0;
1454                 swizzle_pipe[1] = 2;
1455                 swizzle_pipe[2] = 4;
1456                 swizzle_pipe[3] = 6;
1457                 swizzle_pipe[4] = 1;
1458                 swizzle_pipe[5] = 3;
1459                 swizzle_pipe[6] = 5;
1460                 swizzle_pipe[7] = 7;
1461                 break;
1462         }
1463
1464         cur_backend = 0;
1465         for (cur_pipe = 0; cur_pipe < num_tile_pipes; ++cur_pipe) {
1466                 while (((1 << cur_backend) & enabled_backends_mask) == 0)
1467                         cur_backend = (cur_backend + 1) % R6XX_MAX_BACKENDS;
1468
1469                 backend_map |= (u32)(((cur_backend & 3) << (swizzle_pipe[cur_pipe] * 2)));
1470
1471                 cur_backend = (cur_backend + 1) % R6XX_MAX_BACKENDS;
1472         }
1473
1474         return backend_map;
1475 }
1476
1477 int r600_count_pipe_bits(uint32_t val)
1478 {
1479         int i, ret = 0;
1480
1481         for (i = 0; i < 32; i++) {
1482                 ret += val & 1;
1483                 val >>= 1;
1484         }
1485         return ret;
1486 }
1487
1488 void r600_gpu_init(struct radeon_device *rdev)
1489 {
1490         u32 tiling_config;
1491         u32 ramcfg;
1492         u32 backend_map;
1493         u32 cc_rb_backend_disable;
1494         u32 cc_gc_shader_pipe_config;
1495         u32 tmp;
1496         int i, j;
1497         u32 sq_config;
1498         u32 sq_gpr_resource_mgmt_1 = 0;
1499         u32 sq_gpr_resource_mgmt_2 = 0;
1500         u32 sq_thread_resource_mgmt = 0;
1501         u32 sq_stack_resource_mgmt_1 = 0;
1502         u32 sq_stack_resource_mgmt_2 = 0;
1503
1504         /* FIXME: implement */
1505         switch (rdev->family) {
1506         case CHIP_R600:
1507                 rdev->config.r600.max_pipes = 4;
1508                 rdev->config.r600.max_tile_pipes = 8;
1509                 rdev->config.r600.max_simds = 4;
1510                 rdev->config.r600.max_backends = 4;
1511                 rdev->config.r600.max_gprs = 256;
1512                 rdev->config.r600.max_threads = 192;
1513                 rdev->config.r600.max_stack_entries = 256;
1514                 rdev->config.r600.max_hw_contexts = 8;
1515                 rdev->config.r600.max_gs_threads = 16;
1516                 rdev->config.r600.sx_max_export_size = 128;
1517                 rdev->config.r600.sx_max_export_pos_size = 16;
1518                 rdev->config.r600.sx_max_export_smx_size = 128;
1519                 rdev->config.r600.sq_num_cf_insts = 2;
1520                 break;
1521         case CHIP_RV630:
1522         case CHIP_RV635:
1523                 rdev->config.r600.max_pipes = 2;
1524                 rdev->config.r600.max_tile_pipes = 2;
1525                 rdev->config.r600.max_simds = 3;
1526                 rdev->config.r600.max_backends = 1;
1527                 rdev->config.r600.max_gprs = 128;
1528                 rdev->config.r600.max_threads = 192;
1529                 rdev->config.r600.max_stack_entries = 128;
1530                 rdev->config.r600.max_hw_contexts = 8;
1531                 rdev->config.r600.max_gs_threads = 4;
1532                 rdev->config.r600.sx_max_export_size = 128;
1533                 rdev->config.r600.sx_max_export_pos_size = 16;
1534                 rdev->config.r600.sx_max_export_smx_size = 128;
1535                 rdev->config.r600.sq_num_cf_insts = 2;
1536                 break;
1537         case CHIP_RV610:
1538         case CHIP_RV620:
1539         case CHIP_RS780:
1540         case CHIP_RS880:
1541                 rdev->config.r600.max_pipes = 1;
1542                 rdev->config.r600.max_tile_pipes = 1;
1543                 rdev->config.r600.max_simds = 2;
1544                 rdev->config.r600.max_backends = 1;
1545                 rdev->config.r600.max_gprs = 128;
1546                 rdev->config.r600.max_threads = 192;
1547                 rdev->config.r600.max_stack_entries = 128;
1548                 rdev->config.r600.max_hw_contexts = 4;
1549                 rdev->config.r600.max_gs_threads = 4;
1550                 rdev->config.r600.sx_max_export_size = 128;
1551                 rdev->config.r600.sx_max_export_pos_size = 16;
1552                 rdev->config.r600.sx_max_export_smx_size = 128;
1553                 rdev->config.r600.sq_num_cf_insts = 1;
1554                 break;
1555         case CHIP_RV670:
1556                 rdev->config.r600.max_pipes = 4;
1557                 rdev->config.r600.max_tile_pipes = 4;
1558                 rdev->config.r600.max_simds = 4;
1559                 rdev->config.r600.max_backends = 4;
1560                 rdev->config.r600.max_gprs = 192;
1561                 rdev->config.r600.max_threads = 192;
1562                 rdev->config.r600.max_stack_entries = 256;
1563                 rdev->config.r600.max_hw_contexts = 8;
1564                 rdev->config.r600.max_gs_threads = 16;
1565                 rdev->config.r600.sx_max_export_size = 128;
1566                 rdev->config.r600.sx_max_export_pos_size = 16;
1567                 rdev->config.r600.sx_max_export_smx_size = 128;
1568                 rdev->config.r600.sq_num_cf_insts = 2;
1569                 break;
1570         default:
1571                 break;
1572         }
1573
1574         /* Initialize HDP */
1575         for (i = 0, j = 0; i < 32; i++, j += 0x18) {
1576                 WREG32((0x2c14 + j), 0x00000000);
1577                 WREG32((0x2c18 + j), 0x00000000);
1578                 WREG32((0x2c1c + j), 0x00000000);
1579                 WREG32((0x2c20 + j), 0x00000000);
1580                 WREG32((0x2c24 + j), 0x00000000);
1581         }
1582
1583         WREG32(GRBM_CNTL, GRBM_READ_TIMEOUT(0xff));
1584
1585         /* Setup tiling */
1586         tiling_config = 0;
1587         ramcfg = RREG32(RAMCFG);
1588         switch (rdev->config.r600.max_tile_pipes) {
1589         case 1:
1590                 tiling_config |= PIPE_TILING(0);
1591                 break;
1592         case 2:
1593                 tiling_config |= PIPE_TILING(1);
1594                 break;
1595         case 4:
1596                 tiling_config |= PIPE_TILING(2);
1597                 break;
1598         case 8:
1599                 tiling_config |= PIPE_TILING(3);
1600                 break;
1601         default:
1602                 break;
1603         }
1604         rdev->config.r600.tiling_npipes = rdev->config.r600.max_tile_pipes;
1605         rdev->config.r600.tiling_nbanks = 4 << ((ramcfg & NOOFBANK_MASK) >> NOOFBANK_SHIFT);
1606         tiling_config |= BANK_TILING((ramcfg & NOOFBANK_MASK) >> NOOFBANK_SHIFT);
1607         tiling_config |= GROUP_SIZE((ramcfg & BURSTLENGTH_MASK) >> BURSTLENGTH_SHIFT);
1608         if ((ramcfg & BURSTLENGTH_MASK) >> BURSTLENGTH_SHIFT)
1609                 rdev->config.r600.tiling_group_size = 512;
1610         else
1611                 rdev->config.r600.tiling_group_size = 256;
1612         tmp = (ramcfg & NOOFROWS_MASK) >> NOOFROWS_SHIFT;
1613         if (tmp > 3) {
1614                 tiling_config |= ROW_TILING(3);
1615                 tiling_config |= SAMPLE_SPLIT(3);
1616         } else {
1617                 tiling_config |= ROW_TILING(tmp);
1618                 tiling_config |= SAMPLE_SPLIT(tmp);
1619         }
1620         tiling_config |= BANK_SWAPS(1);
1621
1622         cc_rb_backend_disable = RREG32(CC_RB_BACKEND_DISABLE) & 0x00ff0000;
1623         cc_rb_backend_disable |=
1624                 BACKEND_DISABLE((R6XX_MAX_BACKENDS_MASK << rdev->config.r600.max_backends) & R6XX_MAX_BACKENDS_MASK);
1625
1626         cc_gc_shader_pipe_config = RREG32(CC_GC_SHADER_PIPE_CONFIG) & 0xffffff00;
1627         cc_gc_shader_pipe_config |=
1628                 INACTIVE_QD_PIPES((R6XX_MAX_PIPES_MASK << rdev->config.r600.max_pipes) & R6XX_MAX_PIPES_MASK);
1629         cc_gc_shader_pipe_config |=
1630                 INACTIVE_SIMDS((R6XX_MAX_SIMDS_MASK << rdev->config.r600.max_simds) & R6XX_MAX_SIMDS_MASK);
1631
1632         backend_map = r600_get_tile_pipe_to_backend_map(rdev->config.r600.max_tile_pipes,
1633                                                         (R6XX_MAX_BACKENDS -
1634                                                          r600_count_pipe_bits((cc_rb_backend_disable &
1635                                                                                R6XX_MAX_BACKENDS_MASK) >> 16)),
1636                                                         (cc_rb_backend_disable >> 16));
1637         rdev->config.r600.tile_config = tiling_config;
1638         tiling_config |= BACKEND_MAP(backend_map);
1639         WREG32(GB_TILING_CONFIG, tiling_config);
1640         WREG32(DCP_TILING_CONFIG, tiling_config & 0xffff);
1641         WREG32(HDP_TILING_CONFIG, tiling_config & 0xffff);
1642
1643         /* Setup pipes */
1644         WREG32(CC_RB_BACKEND_DISABLE, cc_rb_backend_disable);
1645         WREG32(CC_GC_SHADER_PIPE_CONFIG, cc_gc_shader_pipe_config);
1646         WREG32(GC_USER_SHADER_PIPE_CONFIG, cc_gc_shader_pipe_config);
1647
1648         tmp = R6XX_MAX_PIPES - r600_count_pipe_bits((cc_gc_shader_pipe_config & INACTIVE_QD_PIPES_MASK) >> 8);
1649         WREG32(VGT_OUT_DEALLOC_CNTL, (tmp * 4) & DEALLOC_DIST_MASK);
1650         WREG32(VGT_VERTEX_REUSE_BLOCK_CNTL, ((tmp * 4) - 2) & VTX_REUSE_DEPTH_MASK);
1651
1652         /* Setup some CP states */
1653         WREG32(CP_QUEUE_THRESHOLDS, (ROQ_IB1_START(0x16) | ROQ_IB2_START(0x2b)));
1654         WREG32(CP_MEQ_THRESHOLDS, (MEQ_END(0x40) | ROQ_END(0x40)));
1655
1656         WREG32(TA_CNTL_AUX, (DISABLE_CUBE_ANISO | SYNC_GRADIENT |
1657                              SYNC_WALKER | SYNC_ALIGNER));
1658         /* Setup various GPU states */
1659         if (rdev->family == CHIP_RV670)
1660                 WREG32(ARB_GDEC_RD_CNTL, 0x00000021);
1661
1662         tmp = RREG32(SX_DEBUG_1);
1663         tmp |= SMX_EVENT_RELEASE;
1664         if ((rdev->family > CHIP_R600))
1665                 tmp |= ENABLE_NEW_SMX_ADDRESS;
1666         WREG32(SX_DEBUG_1, tmp);
1667
1668         if (((rdev->family) == CHIP_R600) ||
1669             ((rdev->family) == CHIP_RV630) ||
1670             ((rdev->family) == CHIP_RV610) ||
1671             ((rdev->family) == CHIP_RV620) ||
1672             ((rdev->family) == CHIP_RS780) ||
1673             ((rdev->family) == CHIP_RS880)) {
1674                 WREG32(DB_DEBUG, PREZ_MUST_WAIT_FOR_POSTZ_DONE);
1675         } else {
1676                 WREG32(DB_DEBUG, 0);
1677         }
1678         WREG32(DB_WATERMARKS, (DEPTH_FREE(4) | DEPTH_CACHELINE_FREE(16) |
1679                                DEPTH_FLUSH(16) | DEPTH_PENDING_FREE(4)));
1680
1681         WREG32(PA_SC_MULTI_CHIP_CNTL, 0);
1682         WREG32(VGT_NUM_INSTANCES, 0);
1683
1684         WREG32(SPI_CONFIG_CNTL, GPR_WRITE_PRIORITY(0));
1685         WREG32(SPI_CONFIG_CNTL_1, VTX_DONE_DELAY(0));
1686
1687         tmp = RREG32(SQ_MS_FIFO_SIZES);
1688         if (((rdev->family) == CHIP_RV610) ||
1689             ((rdev->family) == CHIP_RV620) ||
1690             ((rdev->family) == CHIP_RS780) ||
1691             ((rdev->family) == CHIP_RS880)) {
1692                 tmp = (CACHE_FIFO_SIZE(0xa) |
1693                        FETCH_FIFO_HIWATER(0xa) |
1694                        DONE_FIFO_HIWATER(0xe0) |
1695                        ALU_UPDATE_FIFO_HIWATER(0x8));
1696         } else if (((rdev->family) == CHIP_R600) ||
1697                    ((rdev->family) == CHIP_RV630)) {
1698                 tmp &= ~DONE_FIFO_HIWATER(0xff);
1699                 tmp |= DONE_FIFO_HIWATER(0x4);
1700         }
1701         WREG32(SQ_MS_FIFO_SIZES, tmp);
1702
1703         /* SQ_CONFIG, SQ_GPR_RESOURCE_MGMT, SQ_THREAD_RESOURCE_MGMT, SQ_STACK_RESOURCE_MGMT
1704          * should be adjusted as needed by the 2D/3D drivers.  This just sets default values
1705          */
1706         sq_config = RREG32(SQ_CONFIG);
1707         sq_config &= ~(PS_PRIO(3) |
1708                        VS_PRIO(3) |
1709                        GS_PRIO(3) |
1710                        ES_PRIO(3));
1711         sq_config |= (DX9_CONSTS |
1712                       VC_ENABLE |
1713                       PS_PRIO(0) |
1714                       VS_PRIO(1) |
1715                       GS_PRIO(2) |
1716                       ES_PRIO(3));
1717
1718         if ((rdev->family) == CHIP_R600) {
1719                 sq_gpr_resource_mgmt_1 = (NUM_PS_GPRS(124) |
1720                                           NUM_VS_GPRS(124) |
1721                                           NUM_CLAUSE_TEMP_GPRS(4));
1722                 sq_gpr_resource_mgmt_2 = (NUM_GS_GPRS(0) |
1723                                           NUM_ES_GPRS(0));
1724                 sq_thread_resource_mgmt = (NUM_PS_THREADS(136) |
1725                                            NUM_VS_THREADS(48) |
1726                                            NUM_GS_THREADS(4) |
1727                                            NUM_ES_THREADS(4));
1728                 sq_stack_resource_mgmt_1 = (NUM_PS_STACK_ENTRIES(128) |
1729                                             NUM_VS_STACK_ENTRIES(128));
1730                 sq_stack_resource_mgmt_2 = (NUM_GS_STACK_ENTRIES(0) |
1731                                             NUM_ES_STACK_ENTRIES(0));
1732         } else if (((rdev->family) == CHIP_RV610) ||
1733                    ((rdev->family) == CHIP_RV620) ||
1734                    ((rdev->family) == CHIP_RS780) ||
1735                    ((rdev->family) == CHIP_RS880)) {
1736                 /* no vertex cache */
1737                 sq_config &= ~VC_ENABLE;
1738
1739                 sq_gpr_resource_mgmt_1 = (NUM_PS_GPRS(44) |
1740                                           NUM_VS_GPRS(44) |
1741                                           NUM_CLAUSE_TEMP_GPRS(2));
1742                 sq_gpr_resource_mgmt_2 = (NUM_GS_GPRS(17) |
1743                                           NUM_ES_GPRS(17));
1744                 sq_thread_resource_mgmt = (NUM_PS_THREADS(79) |
1745                                            NUM_VS_THREADS(78) |
1746                                            NUM_GS_THREADS(4) |
1747                                            NUM_ES_THREADS(31));
1748                 sq_stack_resource_mgmt_1 = (NUM_PS_STACK_ENTRIES(40) |
1749                                             NUM_VS_STACK_ENTRIES(40));
1750                 sq_stack_resource_mgmt_2 = (NUM_GS_STACK_ENTRIES(32) |
1751                                             NUM_ES_STACK_ENTRIES(16));
1752         } else if (((rdev->family) == CHIP_RV630) ||
1753                    ((rdev->family) == CHIP_RV635)) {
1754                 sq_gpr_resource_mgmt_1 = (NUM_PS_GPRS(44) |
1755                                           NUM_VS_GPRS(44) |
1756                                           NUM_CLAUSE_TEMP_GPRS(2));
1757                 sq_gpr_resource_mgmt_2 = (NUM_GS_GPRS(18) |
1758                                           NUM_ES_GPRS(18));
1759                 sq_thread_resource_mgmt = (NUM_PS_THREADS(79) |
1760                                            NUM_VS_THREADS(78) |
1761                                            NUM_GS_THREADS(4) |
1762                                            NUM_ES_THREADS(31));
1763                 sq_stack_resource_mgmt_1 = (NUM_PS_STACK_ENTRIES(40) |
1764                                             NUM_VS_STACK_ENTRIES(40));
1765                 sq_stack_resource_mgmt_2 = (NUM_GS_STACK_ENTRIES(32) |
1766                                             NUM_ES_STACK_ENTRIES(16));
1767         } else if ((rdev->family) == CHIP_RV670) {
1768                 sq_gpr_resource_mgmt_1 = (NUM_PS_GPRS(44) |
1769                                           NUM_VS_GPRS(44) |
1770                                           NUM_CLAUSE_TEMP_GPRS(2));
1771                 sq_gpr_resource_mgmt_2 = (NUM_GS_GPRS(17) |
1772                                           NUM_ES_GPRS(17));
1773                 sq_thread_resource_mgmt = (NUM_PS_THREADS(79) |
1774                                            NUM_VS_THREADS(78) |
1775                                            NUM_GS_THREADS(4) |
1776                                            NUM_ES_THREADS(31));
1777                 sq_stack_resource_mgmt_1 = (NUM_PS_STACK_ENTRIES(64) |
1778                                             NUM_VS_STACK_ENTRIES(64));
1779                 sq_stack_resource_mgmt_2 = (NUM_GS_STACK_ENTRIES(64) |
1780                                             NUM_ES_STACK_ENTRIES(64));
1781         }
1782
1783         WREG32(SQ_CONFIG, sq_config);
1784         WREG32(SQ_GPR_RESOURCE_MGMT_1,  sq_gpr_resource_mgmt_1);
1785         WREG32(SQ_GPR_RESOURCE_MGMT_2,  sq_gpr_resource_mgmt_2);
1786         WREG32(SQ_THREAD_RESOURCE_MGMT, sq_thread_resource_mgmt);
1787         WREG32(SQ_STACK_RESOURCE_MGMT_1, sq_stack_resource_mgmt_1);
1788         WREG32(SQ_STACK_RESOURCE_MGMT_2, sq_stack_resource_mgmt_2);
1789
1790         if (((rdev->family) == CHIP_RV610) ||
1791             ((rdev->family) == CHIP_RV620) ||
1792             ((rdev->family) == CHIP_RS780) ||
1793             ((rdev->family) == CHIP_RS880)) {
1794                 WREG32(VGT_CACHE_INVALIDATION, CACHE_INVALIDATION(TC_ONLY));
1795         } else {
1796                 WREG32(VGT_CACHE_INVALIDATION, CACHE_INVALIDATION(VC_AND_TC));
1797         }
1798
1799         /* More default values. 2D/3D driver should adjust as needed */
1800         WREG32(PA_SC_AA_SAMPLE_LOCS_2S, (S0_X(0xc) | S0_Y(0x4) |
1801                                          S1_X(0x4) | S1_Y(0xc)));
1802         WREG32(PA_SC_AA_SAMPLE_LOCS_4S, (S0_X(0xe) | S0_Y(0xe) |
1803                                          S1_X(0x2) | S1_Y(0x2) |
1804                                          S2_X(0xa) | S2_Y(0x6) |
1805                                          S3_X(0x6) | S3_Y(0xa)));
1806         WREG32(PA_SC_AA_SAMPLE_LOCS_8S_WD0, (S0_X(0xe) | S0_Y(0xb) |
1807                                              S1_X(0x4) | S1_Y(0xc) |
1808                                              S2_X(0x1) | S2_Y(0x6) |
1809                                              S3_X(0xa) | S3_Y(0xe)));
1810         WREG32(PA_SC_AA_SAMPLE_LOCS_8S_WD1, (S4_X(0x6) | S4_Y(0x1) |
1811                                              S5_X(0x0) | S5_Y(0x0) |
1812                                              S6_X(0xb) | S6_Y(0x4) |
1813                                              S7_X(0x7) | S7_Y(0x8)));
1814
1815         WREG32(VGT_STRMOUT_EN, 0);
1816         tmp = rdev->config.r600.max_pipes * 16;
1817         switch (rdev->family) {
1818         case CHIP_RV610:
1819         case CHIP_RV620:
1820         case CHIP_RS780:
1821         case CHIP_RS880:
1822                 tmp += 32;
1823                 break;
1824         case CHIP_RV670:
1825                 tmp += 128;
1826                 break;
1827         default:
1828                 break;
1829         }
1830         if (tmp > 256) {
1831                 tmp = 256;
1832         }
1833         WREG32(VGT_ES_PER_GS, 128);
1834         WREG32(VGT_GS_PER_ES, tmp);
1835         WREG32(VGT_GS_PER_VS, 2);
1836         WREG32(VGT_GS_VERTEX_REUSE, 16);
1837
1838         /* more default values. 2D/3D driver should adjust as needed */
1839         WREG32(PA_SC_LINE_STIPPLE_STATE, 0);
1840         WREG32(VGT_STRMOUT_EN, 0);
1841         WREG32(SX_MISC, 0);
1842         WREG32(PA_SC_MODE_CNTL, 0);
1843         WREG32(PA_SC_AA_CONFIG, 0);
1844         WREG32(PA_SC_LINE_STIPPLE, 0);
1845         WREG32(SPI_INPUT_Z, 0);
1846         WREG32(SPI_PS_IN_CONTROL_0, NUM_INTERP(2));
1847         WREG32(CB_COLOR7_FRAG, 0);
1848
1849         /* Clear render buffer base addresses */
1850         WREG32(CB_COLOR0_BASE, 0);
1851         WREG32(CB_COLOR1_BASE, 0);
1852         WREG32(CB_COLOR2_BASE, 0);
1853         WREG32(CB_COLOR3_BASE, 0);
1854         WREG32(CB_COLOR4_BASE, 0);
1855         WREG32(CB_COLOR5_BASE, 0);
1856         WREG32(CB_COLOR6_BASE, 0);
1857         WREG32(CB_COLOR7_BASE, 0);
1858         WREG32(CB_COLOR7_FRAG, 0);
1859
1860         switch (rdev->family) {
1861         case CHIP_RV610:
1862         case CHIP_RV620:
1863         case CHIP_RS780:
1864         case CHIP_RS880:
1865                 tmp = TC_L2_SIZE(8);
1866                 break;
1867         case CHIP_RV630:
1868         case CHIP_RV635:
1869                 tmp = TC_L2_SIZE(4);
1870                 break;
1871         case CHIP_R600:
1872                 tmp = TC_L2_SIZE(0) | L2_DISABLE_LATE_HIT;
1873                 break;
1874         default:
1875                 tmp = TC_L2_SIZE(0);
1876                 break;
1877         }
1878         WREG32(TC_CNTL, tmp);
1879
1880         tmp = RREG32(HDP_HOST_PATH_CNTL);
1881         WREG32(HDP_HOST_PATH_CNTL, tmp);
1882
1883         tmp = RREG32(ARB_POP);
1884         tmp |= ENABLE_TC128;
1885         WREG32(ARB_POP, tmp);
1886
1887         WREG32(PA_SC_MULTI_CHIP_CNTL, 0);
1888         WREG32(PA_CL_ENHANCE, (CLIP_VTX_REORDER_ENA |
1889                                NUM_CLIP_SEQ(3)));
1890         WREG32(PA_SC_ENHANCE, FORCE_EOV_MAX_CLK_CNT(4095));
1891 }
1892
1893
1894 /*
1895  * Indirect registers accessor
1896  */
1897 u32 r600_pciep_rreg(struct radeon_device *rdev, u32 reg)
1898 {
1899         u32 r;
1900
1901         WREG32(PCIE_PORT_INDEX, ((reg) & 0xff));
1902         (void)RREG32(PCIE_PORT_INDEX);
1903         r = RREG32(PCIE_PORT_DATA);
1904         return r;
1905 }
1906
1907 void r600_pciep_wreg(struct radeon_device *rdev, u32 reg, u32 v)
1908 {
1909         WREG32(PCIE_PORT_INDEX, ((reg) & 0xff));
1910         (void)RREG32(PCIE_PORT_INDEX);
1911         WREG32(PCIE_PORT_DATA, (v));
1912         (void)RREG32(PCIE_PORT_DATA);
1913 }
1914
1915 /*
1916  * CP & Ring
1917  */
1918 void r600_cp_stop(struct radeon_device *rdev)
1919 {
1920         rdev->mc.active_vram_size = rdev->mc.visible_vram_size;
1921         WREG32(R_0086D8_CP_ME_CNTL, S_0086D8_CP_ME_HALT(1));
1922         WREG32(SCRATCH_UMSK, 0);
1923 }
1924
1925 int r600_init_microcode(struct radeon_device *rdev)
1926 {
1927         struct platform_device *pdev;
1928         const char *chip_name;
1929         const char *rlc_chip_name;
1930         size_t pfp_req_size, me_req_size, rlc_req_size;
1931         char fw_name[30];
1932         int err;
1933
1934         DRM_DEBUG("\n");
1935
1936         pdev = platform_device_register_simple("radeon_cp", 0, NULL, 0);
1937         err = IS_ERR(pdev);
1938         if (err) {
1939                 printk(KERN_ERR "radeon_cp: Failed to register firmware\n");
1940                 return -EINVAL;
1941         }
1942
1943         switch (rdev->family) {
1944         case CHIP_R600:
1945                 chip_name = "R600";
1946                 rlc_chip_name = "R600";
1947                 break;
1948         case CHIP_RV610:
1949                 chip_name = "RV610";
1950                 rlc_chip_name = "R600";
1951                 break;
1952         case CHIP_RV630:
1953                 chip_name = "RV630";
1954                 rlc_chip_name = "R600";
1955                 break;
1956         case CHIP_RV620:
1957                 chip_name = "RV620";
1958                 rlc_chip_name = "R600";
1959                 break;
1960         case CHIP_RV635:
1961                 chip_name = "RV635";
1962                 rlc_chip_name = "R600";
1963                 break;
1964         case CHIP_RV670:
1965                 chip_name = "RV670";
1966                 rlc_chip_name = "R600";
1967                 break;
1968         case CHIP_RS780:
1969         case CHIP_RS880:
1970                 chip_name = "RS780";
1971                 rlc_chip_name = "R600";
1972                 break;
1973         case CHIP_RV770:
1974                 chip_name = "RV770";
1975                 rlc_chip_name = "R700";
1976                 break;
1977         case CHIP_RV730:
1978         case CHIP_RV740:
1979                 chip_name = "RV730";
1980                 rlc_chip_name = "R700";
1981                 break;
1982         case CHIP_RV710:
1983                 chip_name = "RV710";
1984                 rlc_chip_name = "R700";
1985                 break;
1986         case CHIP_CEDAR:
1987                 chip_name = "CEDAR";
1988                 rlc_chip_name = "CEDAR";
1989                 break;
1990         case CHIP_REDWOOD:
1991                 chip_name = "REDWOOD";
1992                 rlc_chip_name = "REDWOOD";
1993                 break;
1994         case CHIP_JUNIPER:
1995                 chip_name = "JUNIPER";
1996                 rlc_chip_name = "JUNIPER";
1997                 break;
1998         case CHIP_CYPRESS:
1999         case CHIP_HEMLOCK:
2000                 chip_name = "CYPRESS";
2001                 rlc_chip_name = "CYPRESS";
2002                 break;
2003         default: BUG();
2004         }
2005
2006         if (rdev->family >= CHIP_CEDAR) {
2007                 pfp_req_size = EVERGREEN_PFP_UCODE_SIZE * 4;
2008                 me_req_size = EVERGREEN_PM4_UCODE_SIZE * 4;
2009                 rlc_req_size = EVERGREEN_RLC_UCODE_SIZE * 4;
2010         } else if (rdev->family >= CHIP_RV770) {
2011                 pfp_req_size = R700_PFP_UCODE_SIZE * 4;
2012                 me_req_size = R700_PM4_UCODE_SIZE * 4;
2013                 rlc_req_size = R700_RLC_UCODE_SIZE * 4;
2014         } else {
2015                 pfp_req_size = PFP_UCODE_SIZE * 4;
2016                 me_req_size = PM4_UCODE_SIZE * 12;
2017                 rlc_req_size = RLC_UCODE_SIZE * 4;
2018         }
2019
2020         DRM_INFO("Loading %s Microcode\n", chip_name);
2021
2022         snprintf(fw_name, sizeof(fw_name), "radeon/%s_pfp.bin", chip_name);
2023         err = request_firmware(&rdev->pfp_fw, fw_name, &pdev->dev);
2024         if (err)
2025                 goto out;
2026         if (rdev->pfp_fw->size != pfp_req_size) {
2027                 printk(KERN_ERR
2028                        "r600_cp: Bogus length %zu in firmware \"%s\"\n",
2029                        rdev->pfp_fw->size, fw_name);
2030                 err = -EINVAL;
2031                 goto out;
2032         }
2033
2034         snprintf(fw_name, sizeof(fw_name), "radeon/%s_me.bin", chip_name);
2035         err = request_firmware(&rdev->me_fw, fw_name, &pdev->dev);
2036         if (err)
2037                 goto out;
2038         if (rdev->me_fw->size != me_req_size) {
2039                 printk(KERN_ERR
2040                        "r600_cp: Bogus length %zu in firmware \"%s\"\n",
2041                        rdev->me_fw->size, fw_name);
2042                 err = -EINVAL;
2043         }
2044
2045         snprintf(fw_name, sizeof(fw_name), "radeon/%s_rlc.bin", rlc_chip_name);
2046         err = request_firmware(&rdev->rlc_fw, fw_name, &pdev->dev);
2047         if (err)
2048                 goto out;
2049         if (rdev->rlc_fw->size != rlc_req_size) {
2050                 printk(KERN_ERR
2051                        "r600_rlc: Bogus length %zu in firmware \"%s\"\n",
2052                        rdev->rlc_fw->size, fw_name);
2053                 err = -EINVAL;
2054         }
2055
2056 out:
2057         platform_device_unregister(pdev);
2058
2059         if (err) {
2060                 if (err != -EINVAL)
2061                         printk(KERN_ERR
2062                                "r600_cp: Failed to load firmware \"%s\"\n",
2063                                fw_name);
2064                 release_firmware(rdev->pfp_fw);
2065                 rdev->pfp_fw = NULL;
2066                 release_firmware(rdev->me_fw);
2067                 rdev->me_fw = NULL;
2068                 release_firmware(rdev->rlc_fw);
2069                 rdev->rlc_fw = NULL;
2070         }
2071         return err;
2072 }
2073
2074 static int r600_cp_load_microcode(struct radeon_device *rdev)
2075 {
2076         const __be32 *fw_data;
2077         int i;
2078
2079         if (!rdev->me_fw || !rdev->pfp_fw)
2080                 return -EINVAL;
2081
2082         r600_cp_stop(rdev);
2083
2084         WREG32(CP_RB_CNTL, RB_NO_UPDATE | RB_BLKSZ(15) | RB_BUFSZ(3));
2085
2086         /* Reset cp */
2087         WREG32(GRBM_SOFT_RESET, SOFT_RESET_CP);
2088         RREG32(GRBM_SOFT_RESET);
2089         mdelay(15);
2090         WREG32(GRBM_SOFT_RESET, 0);
2091
2092         WREG32(CP_ME_RAM_WADDR, 0);
2093
2094         fw_data = (const __be32 *)rdev->me_fw->data;
2095         WREG32(CP_ME_RAM_WADDR, 0);
2096         for (i = 0; i < PM4_UCODE_SIZE * 3; i++)
2097                 WREG32(CP_ME_RAM_DATA,
2098                        be32_to_cpup(fw_data++));
2099
2100         fw_data = (const __be32 *)rdev->pfp_fw->data;
2101         WREG32(CP_PFP_UCODE_ADDR, 0);
2102         for (i = 0; i < PFP_UCODE_SIZE; i++)
2103                 WREG32(CP_PFP_UCODE_DATA,
2104                        be32_to_cpup(fw_data++));
2105
2106         WREG32(CP_PFP_UCODE_ADDR, 0);
2107         WREG32(CP_ME_RAM_WADDR, 0);
2108         WREG32(CP_ME_RAM_RADDR, 0);
2109         return 0;
2110 }
2111
2112 int r600_cp_start(struct radeon_device *rdev)
2113 {
2114         int r;
2115         uint32_t cp_me;
2116
2117         r = radeon_ring_lock(rdev, 7);
2118         if (r) {
2119                 DRM_ERROR("radeon: cp failed to lock ring (%d).\n", r);
2120                 return r;
2121         }
2122         radeon_ring_write(rdev, PACKET3(PACKET3_ME_INITIALIZE, 5));
2123         radeon_ring_write(rdev, 0x1);
2124         if (rdev->family >= CHIP_RV770) {
2125                 radeon_ring_write(rdev, 0x0);
2126                 radeon_ring_write(rdev, rdev->config.rv770.max_hw_contexts - 1);
2127         } else {
2128                 radeon_ring_write(rdev, 0x3);
2129                 radeon_ring_write(rdev, rdev->config.r600.max_hw_contexts - 1);
2130         }
2131         radeon_ring_write(rdev, PACKET3_ME_INITIALIZE_DEVICE_ID(1));
2132         radeon_ring_write(rdev, 0);
2133         radeon_ring_write(rdev, 0);
2134         radeon_ring_unlock_commit(rdev);
2135
2136         cp_me = 0xff;
2137         WREG32(R_0086D8_CP_ME_CNTL, cp_me);
2138         return 0;
2139 }
2140
2141 int r600_cp_resume(struct radeon_device *rdev)
2142 {
2143         u32 tmp;
2144         u32 rb_bufsz;
2145         int r;
2146
2147         /* Reset cp */
2148         WREG32(GRBM_SOFT_RESET, SOFT_RESET_CP);
2149         RREG32(GRBM_SOFT_RESET);
2150         mdelay(15);
2151         WREG32(GRBM_SOFT_RESET, 0);
2152
2153         /* Set ring buffer size */
2154         rb_bufsz = drm_order(rdev->cp.ring_size / 8);
2155         tmp = (drm_order(RADEON_GPU_PAGE_SIZE/8) << 8) | rb_bufsz;
2156 #ifdef __BIG_ENDIAN
2157         tmp |= BUF_SWAP_32BIT;
2158 #endif
2159         WREG32(CP_RB_CNTL, tmp);
2160         WREG32(CP_SEM_WAIT_TIMER, 0x4);
2161
2162         /* Set the write pointer delay */
2163         WREG32(CP_RB_WPTR_DELAY, 0);
2164
2165         /* Initialize the ring buffer's read and write pointers */
2166         WREG32(CP_RB_CNTL, tmp | RB_RPTR_WR_ENA);
2167         WREG32(CP_RB_RPTR_WR, 0);
2168         WREG32(CP_RB_WPTR, 0);
2169
2170         /* set the wb address whether it's enabled or not */
2171         WREG32(CP_RB_RPTR_ADDR, (rdev->wb.gpu_addr + RADEON_WB_CP_RPTR_OFFSET) & 0xFFFFFFFC);
2172         WREG32(CP_RB_RPTR_ADDR_HI, upper_32_bits(rdev->wb.gpu_addr + RADEON_WB_CP_RPTR_OFFSET) & 0xFF);
2173         WREG32(SCRATCH_ADDR, ((rdev->wb.gpu_addr + RADEON_WB_SCRATCH_OFFSET) >> 8) & 0xFFFFFFFF);
2174
2175         if (rdev->wb.enabled)
2176                 WREG32(SCRATCH_UMSK, 0xff);
2177         else {
2178                 tmp |= RB_NO_UPDATE;
2179                 WREG32(SCRATCH_UMSK, 0);
2180         }
2181
2182         mdelay(1);
2183         WREG32(CP_RB_CNTL, tmp);
2184
2185         WREG32(CP_RB_BASE, rdev->cp.gpu_addr >> 8);
2186         WREG32(CP_DEBUG, (1 << 27) | (1 << 28));
2187
2188         rdev->cp.rptr = RREG32(CP_RB_RPTR);
2189         rdev->cp.wptr = RREG32(CP_RB_WPTR);
2190
2191         r600_cp_start(rdev);
2192         rdev->cp.ready = true;
2193         r = radeon_ring_test(rdev);
2194         if (r) {
2195                 rdev->cp.ready = false;
2196                 return r;
2197         }
2198         return 0;
2199 }
2200
2201 void r600_cp_commit(struct radeon_device *rdev)
2202 {
2203         WREG32(CP_RB_WPTR, rdev->cp.wptr);
2204         (void)RREG32(CP_RB_WPTR);
2205 }
2206
2207 void r600_ring_init(struct radeon_device *rdev, unsigned ring_size)
2208 {
2209         u32 rb_bufsz;
2210
2211         /* Align ring size */
2212         rb_bufsz = drm_order(ring_size / 8);
2213         ring_size = (1 << (rb_bufsz + 1)) * 4;
2214         rdev->cp.ring_size = ring_size;
2215         rdev->cp.align_mask = 16 - 1;
2216 }
2217
2218 void r600_cp_fini(struct radeon_device *rdev)
2219 {
2220         r600_cp_stop(rdev);
2221         radeon_ring_fini(rdev);
2222 }
2223
2224
2225 /*
2226  * GPU scratch registers helpers function.
2227  */
2228 void r600_scratch_init(struct radeon_device *rdev)
2229 {
2230         int i;
2231
2232         rdev->scratch.num_reg = 7;
2233         rdev->scratch.reg_base = SCRATCH_REG0;
2234         for (i = 0; i < rdev->scratch.num_reg; i++) {
2235                 rdev->scratch.free[i] = true;
2236                 rdev->scratch.reg[i] = rdev->scratch.reg_base + (i * 4);
2237         }
2238 }
2239
2240 int r600_ring_test(struct radeon_device *rdev)
2241 {
2242         uint32_t scratch;
2243         uint32_t tmp = 0;
2244         unsigned i;
2245         int r;
2246
2247         r = radeon_scratch_get(rdev, &scratch);
2248         if (r) {
2249                 DRM_ERROR("radeon: cp failed to get scratch reg (%d).\n", r);
2250                 return r;
2251         }
2252         WREG32(scratch, 0xCAFEDEAD);
2253         r = radeon_ring_lock(rdev, 3);
2254         if (r) {
2255                 DRM_ERROR("radeon: cp failed to lock ring (%d).\n", r);
2256                 radeon_scratch_free(rdev, scratch);
2257                 return r;
2258         }
2259         radeon_ring_write(rdev, PACKET3(PACKET3_SET_CONFIG_REG, 1));
2260         radeon_ring_write(rdev, ((scratch - PACKET3_SET_CONFIG_REG_OFFSET) >> 2));
2261         radeon_ring_write(rdev, 0xDEADBEEF);
2262         radeon_ring_unlock_commit(rdev);
2263         for (i = 0; i < rdev->usec_timeout; i++) {
2264                 tmp = RREG32(scratch);
2265                 if (tmp == 0xDEADBEEF)
2266                         break;
2267                 DRM_UDELAY(1);
2268         }
2269         if (i < rdev->usec_timeout) {
2270                 DRM_INFO("ring test succeeded in %d usecs\n", i);
2271         } else {
2272                 DRM_ERROR("radeon: ring test failed (scratch(0x%04X)=0x%08X)\n",
2273                           scratch, tmp);
2274                 r = -EINVAL;
2275         }
2276         radeon_scratch_free(rdev, scratch);
2277         return r;
2278 }
2279
2280 void r600_fence_ring_emit(struct radeon_device *rdev,
2281                           struct radeon_fence *fence)
2282 {
2283         if (rdev->wb.use_event) {
2284                 u64 addr = rdev->wb.gpu_addr + R600_WB_EVENT_OFFSET +
2285                         (u64)(rdev->fence_drv.scratch_reg - rdev->scratch.reg_base);
2286                 /* EVENT_WRITE_EOP - flush caches, send int */
2287                 radeon_ring_write(rdev, PACKET3(PACKET3_EVENT_WRITE_EOP, 4));
2288                 radeon_ring_write(rdev, EVENT_TYPE(CACHE_FLUSH_AND_INV_EVENT_TS) | EVENT_INDEX(5));
2289                 radeon_ring_write(rdev, addr & 0xffffffff);
2290                 radeon_ring_write(rdev, (upper_32_bits(addr) & 0xff) | DATA_SEL(1) | INT_SEL(2));
2291                 radeon_ring_write(rdev, fence->seq);
2292                 radeon_ring_write(rdev, 0);
2293         } else {
2294                 radeon_ring_write(rdev, PACKET3(PACKET3_EVENT_WRITE, 0));
2295                 radeon_ring_write(rdev, EVENT_TYPE(CACHE_FLUSH_AND_INV_EVENT) | EVENT_INDEX(0));
2296                 /* wait for 3D idle clean */
2297                 radeon_ring_write(rdev, PACKET3(PACKET3_SET_CONFIG_REG, 1));
2298                 radeon_ring_write(rdev, (WAIT_UNTIL - PACKET3_SET_CONFIG_REG_OFFSET) >> 2);
2299                 radeon_ring_write(rdev, WAIT_3D_IDLE_bit | WAIT_3D_IDLECLEAN_bit);
2300                 /* Emit fence sequence & fire IRQ */
2301                 radeon_ring_write(rdev, PACKET3(PACKET3_SET_CONFIG_REG, 1));
2302                 radeon_ring_write(rdev, ((rdev->fence_drv.scratch_reg - PACKET3_SET_CONFIG_REG_OFFSET) >> 2));
2303                 radeon_ring_write(rdev, fence->seq);
2304                 /* CP_INTERRUPT packet 3 no longer exists, use packet 0 */
2305                 radeon_ring_write(rdev, PACKET0(CP_INT_STATUS, 0));
2306                 radeon_ring_write(rdev, RB_INT_STAT);
2307         }
2308 }
2309
2310 int r600_copy_blit(struct radeon_device *rdev,
2311                    uint64_t src_offset, uint64_t dst_offset,
2312                    unsigned num_pages, struct radeon_fence *fence)
2313 {
2314         int r;
2315
2316         mutex_lock(&rdev->r600_blit.mutex);
2317         rdev->r600_blit.vb_ib = NULL;
2318         r = r600_blit_prepare_copy(rdev, num_pages * RADEON_GPU_PAGE_SIZE);
2319         if (r) {
2320                 if (rdev->r600_blit.vb_ib)
2321                         radeon_ib_free(rdev, &rdev->r600_blit.vb_ib);
2322                 mutex_unlock(&rdev->r600_blit.mutex);
2323                 return r;
2324         }
2325         r600_kms_blit_copy(rdev, src_offset, dst_offset, num_pages * RADEON_GPU_PAGE_SIZE);
2326         r600_blit_done_copy(rdev, fence);
2327         mutex_unlock(&rdev->r600_blit.mutex);
2328         return 0;
2329 }
2330
2331 int r600_set_surface_reg(struct radeon_device *rdev, int reg,
2332                          uint32_t tiling_flags, uint32_t pitch,
2333                          uint32_t offset, uint32_t obj_size)
2334 {
2335         /* FIXME: implement */
2336         return 0;
2337 }
2338
2339 void r600_clear_surface_reg(struct radeon_device *rdev, int reg)
2340 {
2341         /* FIXME: implement */
2342 }
2343
2344
2345 bool r600_card_posted(struct radeon_device *rdev)
2346 {
2347         uint32_t reg;
2348
2349         /* first check CRTCs */
2350         reg = RREG32(D1CRTC_CONTROL) |
2351                 RREG32(D2CRTC_CONTROL);
2352         if (reg & CRTC_EN)
2353                 return true;
2354
2355         /* then check MEM_SIZE, in case the crtcs are off */
2356         if (RREG32(CONFIG_MEMSIZE))
2357                 return true;
2358
2359         return false;
2360 }
2361
2362 int r600_startup(struct radeon_device *rdev)
2363 {
2364         int r;
2365
2366         if (!rdev->me_fw || !rdev->pfp_fw || !rdev->rlc_fw) {
2367                 r = r600_init_microcode(rdev);
2368                 if (r) {
2369                         DRM_ERROR("Failed to load firmware!\n");
2370                         return r;
2371                 }
2372         }
2373
2374         r600_mc_program(rdev);
2375         if (rdev->flags & RADEON_IS_AGP) {
2376                 r600_agp_enable(rdev);
2377         } else {
2378                 r = r600_pcie_gart_enable(rdev);
2379                 if (r)
2380                         return r;
2381         }
2382         r600_gpu_init(rdev);
2383         r = r600_blit_init(rdev);
2384         if (r) {
2385                 r600_blit_fini(rdev);
2386                 rdev->asic->copy = NULL;
2387                 dev_warn(rdev->dev, "failed blitter (%d) falling back to memcpy\n", r);
2388         }
2389
2390         /* allocate wb buffer */
2391         r = radeon_wb_init(rdev);
2392         if (r)
2393                 return r;
2394
2395         /* Enable IRQ */
2396         r = r600_irq_init(rdev);
2397         if (r) {
2398                 DRM_ERROR("radeon: IH init failed (%d).\n", r);
2399                 radeon_irq_kms_fini(rdev);
2400                 return r;
2401         }
2402         r600_irq_set(rdev);
2403
2404         r = radeon_ring_init(rdev, rdev->cp.ring_size);
2405         if (r)
2406                 return r;
2407         r = r600_cp_load_microcode(rdev);
2408         if (r)
2409                 return r;
2410         r = r600_cp_resume(rdev);
2411         if (r)
2412                 return r;
2413
2414         return 0;
2415 }
2416
2417 void r600_vga_set_state(struct radeon_device *rdev, bool state)
2418 {
2419         uint32_t temp;
2420
2421         temp = RREG32(CONFIG_CNTL);
2422         if (state == false) {
2423                 temp &= ~(1<<0);
2424                 temp |= (1<<1);
2425         } else {
2426                 temp &= ~(1<<1);
2427         }
2428         WREG32(CONFIG_CNTL, temp);
2429 }
2430
2431 int r600_resume(struct radeon_device *rdev)
2432 {
2433         int r;
2434
2435         /* Do not reset GPU before posting, on r600 hw unlike on r500 hw,
2436          * posting will perform necessary task to bring back GPU into good
2437          * shape.
2438          */
2439         /* post card */
2440         atom_asic_init(rdev->mode_info.atom_context);
2441
2442         r = r600_startup(rdev);
2443         if (r) {
2444                 DRM_ERROR("r600 startup failed on resume\n");
2445                 return r;
2446         }
2447
2448         r = r600_ib_test(rdev);
2449         if (r) {
2450                 DRM_ERROR("radeon: failled testing IB (%d).\n", r);
2451                 return r;
2452         }
2453
2454         r = r600_audio_init(rdev);
2455         if (r) {
2456                 DRM_ERROR("radeon: audio resume failed\n");
2457                 return r;
2458         }
2459
2460         return r;
2461 }
2462
2463 int r600_suspend(struct radeon_device *rdev)
2464 {
2465         int r;
2466
2467         r600_audio_fini(rdev);
2468         /* FIXME: we should wait for ring to be empty */
2469         r600_cp_stop(rdev);
2470         rdev->cp.ready = false;
2471         r600_irq_suspend(rdev);
2472         radeon_wb_disable(rdev);
2473         r600_pcie_gart_disable(rdev);
2474         /* unpin shaders bo */
2475         if (rdev->r600_blit.shader_obj) {
2476                 r = radeon_bo_reserve(rdev->r600_blit.shader_obj, false);
2477                 if (!r) {
2478                         radeon_bo_unpin(rdev->r600_blit.shader_obj);
2479                         radeon_bo_unreserve(rdev->r600_blit.shader_obj);
2480                 }
2481         }
2482         return 0;
2483 }
2484
2485 /* Plan is to move initialization in that function and use
2486  * helper function so that radeon_device_init pretty much
2487  * do nothing more than calling asic specific function. This
2488  * should also allow to remove a bunch of callback function
2489  * like vram_info.
2490  */
2491 int r600_init(struct radeon_device *rdev)
2492 {
2493         int r;
2494
2495         r = radeon_dummy_page_init(rdev);
2496         if (r)
2497                 return r;
2498         if (r600_debugfs_mc_info_init(rdev)) {
2499                 DRM_ERROR("Failed to register debugfs file for mc !\n");
2500         }
2501         /* This don't do much */
2502         r = radeon_gem_init(rdev);
2503         if (r)
2504                 return r;
2505         /* Read BIOS */
2506         if (!radeon_get_bios(rdev)) {
2507                 if (ASIC_IS_AVIVO(rdev))
2508                         return -EINVAL;
2509         }
2510         /* Must be an ATOMBIOS */
2511         if (!rdev->is_atom_bios) {
2512                 dev_err(rdev->dev, "Expecting atombios for R600 GPU\n");
2513                 return -EINVAL;
2514         }
2515         r = radeon_atombios_init(rdev);
2516         if (r)
2517                 return r;
2518         /* Post card if necessary */
2519         if (!r600_card_posted(rdev)) {
2520                 if (!rdev->bios) {
2521                         dev_err(rdev->dev, "Card not posted and no BIOS - ignoring\n");
2522                         return -EINVAL;
2523                 }
2524                 DRM_INFO("GPU not posted. posting now...\n");
2525                 atom_asic_init(rdev->mode_info.atom_context);
2526         }
2527         /* Initialize scratch registers */
2528         r600_scratch_init(rdev);
2529         /* Initialize surface registers */
2530         radeon_surface_init(rdev);
2531         /* Initialize clocks */
2532         radeon_get_clock_info(rdev->ddev);
2533         /* Fence driver */
2534         r = radeon_fence_driver_init(rdev);
2535         if (r)
2536                 return r;
2537         if (rdev->flags & RADEON_IS_AGP) {
2538                 r = radeon_agp_init(rdev);
2539                 if (r)
2540                         radeon_agp_disable(rdev);
2541         }
2542         r = r600_mc_init(rdev);
2543         if (r)
2544                 return r;
2545         /* Memory manager */
2546         r = radeon_bo_init(rdev);
2547         if (r)
2548                 return r;
2549
2550         r = radeon_irq_kms_init(rdev);
2551         if (r)
2552                 return r;
2553
2554         rdev->cp.ring_obj = NULL;
2555         r600_ring_init(rdev, 1024 * 1024);
2556
2557         rdev->ih.ring_obj = NULL;
2558         r600_ih_ring_init(rdev, 64 * 1024);
2559
2560         r = r600_pcie_gart_init(rdev);
2561         if (r)
2562                 return r;
2563
2564         rdev->accel_working = true;
2565         r = r600_startup(rdev);
2566         if (r) {
2567                 dev_err(rdev->dev, "disabling GPU acceleration\n");
2568                 r600_cp_fini(rdev);
2569                 r600_irq_fini(rdev);
2570                 radeon_wb_fini(rdev);
2571                 radeon_irq_kms_fini(rdev);
2572                 r600_pcie_gart_fini(rdev);
2573                 rdev->accel_working = false;
2574         }
2575         if (rdev->accel_working) {
2576                 r = radeon_ib_pool_init(rdev);
2577                 if (r) {
2578                         dev_err(rdev->dev, "IB initialization failed (%d).\n", r);
2579                         rdev->accel_working = false;
2580                 } else {
2581                         r = r600_ib_test(rdev);
2582                         if (r) {
2583                                 dev_err(rdev->dev, "IB test failed (%d).\n", r);
2584                                 rdev->accel_working = false;
2585                         }
2586                 }
2587         }
2588
2589         r = r600_audio_init(rdev);
2590         if (r)
2591                 return r; /* TODO error handling */
2592         return 0;
2593 }
2594
2595 void r600_fini(struct radeon_device *rdev)
2596 {
2597         r600_audio_fini(rdev);
2598         r600_blit_fini(rdev);
2599         r600_cp_fini(rdev);
2600         r600_irq_fini(rdev);
2601         radeon_wb_fini(rdev);
2602         radeon_irq_kms_fini(rdev);
2603         r600_pcie_gart_fini(rdev);
2604         radeon_agp_fini(rdev);
2605         radeon_gem_fini(rdev);
2606         radeon_fence_driver_fini(rdev);
2607         radeon_bo_fini(rdev);
2608         radeon_atombios_fini(rdev);
2609         kfree(rdev->bios);
2610         rdev->bios = NULL;
2611         radeon_dummy_page_fini(rdev);
2612 }
2613
2614
2615 /*
2616  * CS stuff
2617  */
2618 void r600_ring_ib_execute(struct radeon_device *rdev, struct radeon_ib *ib)
2619 {
2620         /* FIXME: implement */
2621         radeon_ring_write(rdev, PACKET3(PACKET3_INDIRECT_BUFFER, 2));
2622         radeon_ring_write(rdev, ib->gpu_addr & 0xFFFFFFFC);
2623         radeon_ring_write(rdev, upper_32_bits(ib->gpu_addr) & 0xFF);
2624         radeon_ring_write(rdev, ib->length_dw);
2625 }
2626
2627 int r600_ib_test(struct radeon_device *rdev)
2628 {
2629         struct radeon_ib *ib;
2630         uint32_t scratch;
2631         uint32_t tmp = 0;
2632         unsigned i;
2633         int r;
2634
2635         r = radeon_scratch_get(rdev, &scratch);
2636         if (r) {
2637                 DRM_ERROR("radeon: failed to get scratch reg (%d).\n", r);
2638                 return r;
2639         }
2640         WREG32(scratch, 0xCAFEDEAD);
2641         r = radeon_ib_get(rdev, &ib);
2642         if (r) {
2643                 DRM_ERROR("radeon: failed to get ib (%d).\n", r);
2644                 return r;
2645         }
2646         ib->ptr[0] = PACKET3(PACKET3_SET_CONFIG_REG, 1);
2647         ib->ptr[1] = ((scratch - PACKET3_SET_CONFIG_REG_OFFSET) >> 2);
2648         ib->ptr[2] = 0xDEADBEEF;
2649         ib->ptr[3] = PACKET2(0);
2650         ib->ptr[4] = PACKET2(0);
2651         ib->ptr[5] = PACKET2(0);
2652         ib->ptr[6] = PACKET2(0);
2653         ib->ptr[7] = PACKET2(0);
2654         ib->ptr[8] = PACKET2(0);
2655         ib->ptr[9] = PACKET2(0);
2656         ib->ptr[10] = PACKET2(0);
2657         ib->ptr[11] = PACKET2(0);
2658         ib->ptr[12] = PACKET2(0);
2659         ib->ptr[13] = PACKET2(0);
2660         ib->ptr[14] = PACKET2(0);
2661         ib->ptr[15] = PACKET2(0);
2662         ib->length_dw = 16;
2663         r = radeon_ib_schedule(rdev, ib);
2664         if (r) {
2665                 radeon_scratch_free(rdev, scratch);
2666                 radeon_ib_free(rdev, &ib);
2667                 DRM_ERROR("radeon: failed to schedule ib (%d).\n", r);
2668                 return r;
2669         }
2670         r = radeon_fence_wait(ib->fence, false);
2671         if (r) {
2672                 DRM_ERROR("radeon: fence wait failed (%d).\n", r);
2673                 return r;
2674         }
2675         for (i = 0; i < rdev->usec_timeout; i++) {
2676                 tmp = RREG32(scratch);
2677                 if (tmp == 0xDEADBEEF)
2678                         break;
2679                 DRM_UDELAY(1);
2680         }
2681         if (i < rdev->usec_timeout) {
2682                 DRM_INFO("ib test succeeded in %u usecs\n", i);
2683         } else {
2684                 DRM_ERROR("radeon: ib test failed (scratch(0x%04X)=0x%08X)\n",
2685                           scratch, tmp);
2686                 r = -EINVAL;
2687         }
2688         radeon_scratch_free(rdev, scratch);
2689         radeon_ib_free(rdev, &ib);
2690         return r;
2691 }
2692
2693 /*
2694  * Interrupts
2695  *
2696  * Interrupts use a ring buffer on r6xx/r7xx hardware.  It works pretty
2697  * the same as the CP ring buffer, but in reverse.  Rather than the CPU
2698  * writing to the ring and the GPU consuming, the GPU writes to the ring
2699  * and host consumes.  As the host irq handler processes interrupts, it
2700  * increments the rptr.  When the rptr catches up with the wptr, all the
2701  * current interrupts have been processed.
2702  */
2703
2704 void r600_ih_ring_init(struct radeon_device *rdev, unsigned ring_size)
2705 {
2706         u32 rb_bufsz;
2707
2708         /* Align ring size */
2709         rb_bufsz = drm_order(ring_size / 4);
2710         ring_size = (1 << rb_bufsz) * 4;
2711         rdev->ih.ring_size = ring_size;
2712         rdev->ih.ptr_mask = rdev->ih.ring_size - 1;
2713         rdev->ih.rptr = 0;
2714 }
2715
2716 static int r600_ih_ring_alloc(struct radeon_device *rdev)
2717 {
2718         int r;
2719
2720         /* Allocate ring buffer */
2721         if (rdev->ih.ring_obj == NULL) {
2722                 r = radeon_bo_create(rdev, NULL, rdev->ih.ring_size,
2723                                      PAGE_SIZE, true,
2724                                      RADEON_GEM_DOMAIN_GTT,
2725                                      &rdev->ih.ring_obj);
2726                 if (r) {
2727                         DRM_ERROR("radeon: failed to create ih ring buffer (%d).\n", r);
2728                         return r;
2729                 }
2730                 r = radeon_bo_reserve(rdev->ih.ring_obj, false);
2731                 if (unlikely(r != 0))
2732                         return r;
2733                 r = radeon_bo_pin(rdev->ih.ring_obj,
2734                                   RADEON_GEM_DOMAIN_GTT,
2735                                   &rdev->ih.gpu_addr);
2736                 if (r) {
2737                         radeon_bo_unreserve(rdev->ih.ring_obj);
2738                         DRM_ERROR("radeon: failed to pin ih ring buffer (%d).\n", r);
2739                         return r;
2740                 }
2741                 r = radeon_bo_kmap(rdev->ih.ring_obj,
2742                                    (void **)&rdev->ih.ring);
2743                 radeon_bo_unreserve(rdev->ih.ring_obj);
2744                 if (r) {
2745                         DRM_ERROR("radeon: failed to map ih ring buffer (%d).\n", r);
2746                         return r;
2747                 }
2748         }
2749         return 0;
2750 }
2751
2752 static void r600_ih_ring_fini(struct radeon_device *rdev)
2753 {
2754         int r;
2755         if (rdev->ih.ring_obj) {
2756                 r = radeon_bo_reserve(rdev->ih.ring_obj, false);
2757                 if (likely(r == 0)) {
2758                         radeon_bo_kunmap(rdev->ih.ring_obj);
2759                         radeon_bo_unpin(rdev->ih.ring_obj);
2760                         radeon_bo_unreserve(rdev->ih.ring_obj);
2761                 }
2762                 radeon_bo_unref(&rdev->ih.ring_obj);
2763                 rdev->ih.ring = NULL;
2764                 rdev->ih.ring_obj = NULL;
2765         }
2766 }
2767
2768 void r600_rlc_stop(struct radeon_device *rdev)
2769 {
2770
2771         if ((rdev->family >= CHIP_RV770) &&
2772             (rdev->family <= CHIP_RV740)) {
2773                 /* r7xx asics need to soft reset RLC before halting */
2774                 WREG32(SRBM_SOFT_RESET, SOFT_RESET_RLC);
2775                 RREG32(SRBM_SOFT_RESET);
2776                 udelay(15000);
2777                 WREG32(SRBM_SOFT_RESET, 0);
2778                 RREG32(SRBM_SOFT_RESET);
2779         }
2780
2781         WREG32(RLC_CNTL, 0);
2782 }
2783
2784 static void r600_rlc_start(struct radeon_device *rdev)
2785 {
2786         WREG32(RLC_CNTL, RLC_ENABLE);
2787 }
2788
2789 static int r600_rlc_init(struct radeon_device *rdev)
2790 {
2791         u32 i;
2792         const __be32 *fw_data;
2793
2794         if (!rdev->rlc_fw)
2795                 return -EINVAL;
2796
2797         r600_rlc_stop(rdev);
2798
2799         WREG32(RLC_HB_BASE, 0);
2800         WREG32(RLC_HB_CNTL, 0);
2801         WREG32(RLC_HB_RPTR, 0);
2802         WREG32(RLC_HB_WPTR, 0);
2803         WREG32(RLC_HB_WPTR_LSB_ADDR, 0);
2804         WREG32(RLC_HB_WPTR_MSB_ADDR, 0);
2805         WREG32(RLC_MC_CNTL, 0);
2806         WREG32(RLC_UCODE_CNTL, 0);
2807
2808         fw_data = (const __be32 *)rdev->rlc_fw->data;
2809         if (rdev->family >= CHIP_CEDAR) {
2810                 for (i = 0; i < EVERGREEN_RLC_UCODE_SIZE; i++) {
2811                         WREG32(RLC_UCODE_ADDR, i);
2812                         WREG32(RLC_UCODE_DATA, be32_to_cpup(fw_data++));
2813                 }
2814         } else if (rdev->family >= CHIP_RV770) {
2815                 for (i = 0; i < R700_RLC_UCODE_SIZE; i++) {
2816                         WREG32(RLC_UCODE_ADDR, i);
2817                         WREG32(RLC_UCODE_DATA, be32_to_cpup(fw_data++));
2818                 }
2819         } else {
2820                 for (i = 0; i < RLC_UCODE_SIZE; i++) {
2821                         WREG32(RLC_UCODE_ADDR, i);
2822                         WREG32(RLC_UCODE_DATA, be32_to_cpup(fw_data++));
2823                 }
2824         }
2825         WREG32(RLC_UCODE_ADDR, 0);
2826
2827         r600_rlc_start(rdev);
2828
2829         return 0;
2830 }
2831
2832 static void r600_enable_interrupts(struct radeon_device *rdev)
2833 {
2834         u32 ih_cntl = RREG32(IH_CNTL);
2835         u32 ih_rb_cntl = RREG32(IH_RB_CNTL);
2836
2837         ih_cntl |= ENABLE_INTR;
2838         ih_rb_cntl |= IH_RB_ENABLE;
2839         WREG32(IH_CNTL, ih_cntl);
2840         WREG32(IH_RB_CNTL, ih_rb_cntl);
2841         rdev->ih.enabled = true;
2842 }
2843
2844 void r600_disable_interrupts(struct radeon_device *rdev)
2845 {
2846         u32 ih_rb_cntl = RREG32(IH_RB_CNTL);
2847         u32 ih_cntl = RREG32(IH_CNTL);
2848
2849         ih_rb_cntl &= ~IH_RB_ENABLE;
2850         ih_cntl &= ~ENABLE_INTR;
2851         WREG32(IH_RB_CNTL, ih_rb_cntl);
2852         WREG32(IH_CNTL, ih_cntl);
2853         /* set rptr, wptr to 0 */
2854         WREG32(IH_RB_RPTR, 0);
2855         WREG32(IH_RB_WPTR, 0);
2856         rdev->ih.enabled = false;
2857         rdev->ih.wptr = 0;
2858         rdev->ih.rptr = 0;
2859 }
2860
2861 static void r600_disable_interrupt_state(struct radeon_device *rdev)
2862 {
2863         u32 tmp;
2864
2865         WREG32(CP_INT_CNTL, CNTX_BUSY_INT_ENABLE | CNTX_EMPTY_INT_ENABLE);
2866         WREG32(GRBM_INT_CNTL, 0);
2867         WREG32(DxMODE_INT_MASK, 0);
2868         if (ASIC_IS_DCE3(rdev)) {
2869                 WREG32(DCE3_DACA_AUTODETECT_INT_CONTROL, 0);
2870                 WREG32(DCE3_DACB_AUTODETECT_INT_CONTROL, 0);
2871                 tmp = RREG32(DC_HPD1_INT_CONTROL) & DC_HPDx_INT_POLARITY;
2872                 WREG32(DC_HPD1_INT_CONTROL, tmp);
2873                 tmp = RREG32(DC_HPD2_INT_CONTROL) & DC_HPDx_INT_POLARITY;
2874                 WREG32(DC_HPD2_INT_CONTROL, tmp);
2875                 tmp = RREG32(DC_HPD3_INT_CONTROL) & DC_HPDx_INT_POLARITY;
2876                 WREG32(DC_HPD3_INT_CONTROL, tmp);
2877                 tmp = RREG32(DC_HPD4_INT_CONTROL) & DC_HPDx_INT_POLARITY;
2878                 WREG32(DC_HPD4_INT_CONTROL, tmp);
2879                 if (ASIC_IS_DCE32(rdev)) {
2880                         tmp = RREG32(DC_HPD5_INT_CONTROL) & DC_HPDx_INT_POLARITY;
2881                         WREG32(DC_HPD5_INT_CONTROL, tmp);
2882                         tmp = RREG32(DC_HPD6_INT_CONTROL) & DC_HPDx_INT_POLARITY;
2883                         WREG32(DC_HPD6_INT_CONTROL, tmp);
2884                 }
2885         } else {
2886                 WREG32(DACA_AUTODETECT_INT_CONTROL, 0);
2887                 WREG32(DACB_AUTODETECT_INT_CONTROL, 0);
2888                 tmp = RREG32(DC_HOT_PLUG_DETECT1_INT_CONTROL) & DC_HOT_PLUG_DETECTx_INT_POLARITY;
2889                 WREG32(DC_HOT_PLUG_DETECT1_INT_CONTROL, tmp);
2890                 tmp = RREG32(DC_HOT_PLUG_DETECT2_INT_CONTROL) & DC_HOT_PLUG_DETECTx_INT_POLARITY;
2891                 WREG32(DC_HOT_PLUG_DETECT2_INT_CONTROL, tmp);
2892                 tmp = RREG32(DC_HOT_PLUG_DETECT3_INT_CONTROL) & DC_HOT_PLUG_DETECTx_INT_POLARITY;
2893                 WREG32(DC_HOT_PLUG_DETECT3_INT_CONTROL, tmp);
2894         }
2895 }
2896
2897 int r600_irq_init(struct radeon_device *rdev)
2898 {
2899         int ret = 0;
2900         int rb_bufsz;
2901         u32 interrupt_cntl, ih_cntl, ih_rb_cntl;
2902
2903         /* allocate ring */
2904         ret = r600_ih_ring_alloc(rdev);
2905         if (ret)
2906                 return ret;
2907
2908         /* disable irqs */
2909         r600_disable_interrupts(rdev);
2910
2911         /* init rlc */
2912         ret = r600_rlc_init(rdev);
2913         if (ret) {
2914                 r600_ih_ring_fini(rdev);
2915                 return ret;
2916         }
2917
2918         /* setup interrupt control */
2919         /* set dummy read address to ring address */
2920         WREG32(INTERRUPT_CNTL2, rdev->ih.gpu_addr >> 8);
2921         interrupt_cntl = RREG32(INTERRUPT_CNTL);
2922         /* IH_DUMMY_RD_OVERRIDE=0 - dummy read disabled with msi, enabled without msi
2923          * IH_DUMMY_RD_OVERRIDE=1 - dummy read controlled by IH_DUMMY_RD_EN
2924          */
2925         interrupt_cntl &= ~IH_DUMMY_RD_OVERRIDE;
2926         /* IH_REQ_NONSNOOP_EN=1 if ring is in non-cacheable memory, e.g., vram */
2927         interrupt_cntl &= ~IH_REQ_NONSNOOP_EN;
2928         WREG32(INTERRUPT_CNTL, interrupt_cntl);
2929
2930         WREG32(IH_RB_BASE, rdev->ih.gpu_addr >> 8);
2931         rb_bufsz = drm_order(rdev->ih.ring_size / 4);
2932
2933         ih_rb_cntl = (IH_WPTR_OVERFLOW_ENABLE |
2934                       IH_WPTR_OVERFLOW_CLEAR |
2935                       (rb_bufsz << 1));
2936
2937         if (rdev->wb.enabled)
2938                 ih_rb_cntl |= IH_WPTR_WRITEBACK_ENABLE;
2939
2940         /* set the writeback address whether it's enabled or not */
2941         WREG32(IH_RB_WPTR_ADDR_LO, (rdev->wb.gpu_addr + R600_WB_IH_WPTR_OFFSET) & 0xFFFFFFFC);
2942         WREG32(IH_RB_WPTR_ADDR_HI, upper_32_bits(rdev->wb.gpu_addr + R600_WB_IH_WPTR_OFFSET) & 0xFF);
2943
2944         WREG32(IH_RB_CNTL, ih_rb_cntl);
2945
2946         /* set rptr, wptr to 0 */
2947         WREG32(IH_RB_RPTR, 0);
2948         WREG32(IH_RB_WPTR, 0);
2949
2950         /* Default settings for IH_CNTL (disabled at first) */
2951         ih_cntl = MC_WRREQ_CREDIT(0x10) | MC_WR_CLEAN_CNT(0x10);
2952         /* RPTR_REARM only works if msi's are enabled */
2953         if (rdev->msi_enabled)
2954                 ih_cntl |= RPTR_REARM;
2955
2956 #ifdef __BIG_ENDIAN
2957         ih_cntl |= IH_MC_SWAP(IH_MC_SWAP_32BIT);
2958 #endif
2959         WREG32(IH_CNTL, ih_cntl);
2960
2961         /* force the active interrupt state to all disabled */
2962         if (rdev->family >= CHIP_CEDAR)
2963                 evergreen_disable_interrupt_state(rdev);
2964         else
2965                 r600_disable_interrupt_state(rdev);
2966
2967         /* enable irqs */
2968         r600_enable_interrupts(rdev);
2969
2970         return ret;
2971 }
2972
2973 void r600_irq_suspend(struct radeon_device *rdev)
2974 {
2975         r600_irq_disable(rdev);
2976         r600_rlc_stop(rdev);
2977 }
2978
2979 void r600_irq_fini(struct radeon_device *rdev)
2980 {
2981         r600_irq_suspend(rdev);
2982         r600_ih_ring_fini(rdev);
2983 }
2984
2985 int r600_irq_set(struct radeon_device *rdev)
2986 {
2987         u32 cp_int_cntl = CNTX_BUSY_INT_ENABLE | CNTX_EMPTY_INT_ENABLE;
2988         u32 mode_int = 0;
2989         u32 hpd1, hpd2, hpd3, hpd4 = 0, hpd5 = 0, hpd6 = 0;
2990         u32 grbm_int_cntl = 0;
2991         u32 hdmi1, hdmi2;
2992
2993         if (!rdev->irq.installed) {
2994                 WARN(1, "Can't enable IRQ/MSI because no handler is installed\n");
2995                 return -EINVAL;
2996         }
2997         /* don't enable anything if the ih is disabled */
2998         if (!rdev->ih.enabled) {
2999                 r600_disable_interrupts(rdev);
3000                 /* force the active interrupt state to all disabled */
3001                 r600_disable_interrupt_state(rdev);
3002                 return 0;
3003         }
3004
3005         hdmi1 = RREG32(R600_HDMI_BLOCK1 + R600_HDMI_CNTL) & ~R600_HDMI_INT_EN;
3006         if (ASIC_IS_DCE3(rdev)) {
3007                 hdmi2 = RREG32(R600_HDMI_BLOCK3 + R600_HDMI_CNTL) & ~R600_HDMI_INT_EN;
3008                 hpd1 = RREG32(DC_HPD1_INT_CONTROL) & ~DC_HPDx_INT_EN;
3009                 hpd2 = RREG32(DC_HPD2_INT_CONTROL) & ~DC_HPDx_INT_EN;
3010                 hpd3 = RREG32(DC_HPD3_INT_CONTROL) & ~DC_HPDx_INT_EN;
3011                 hpd4 = RREG32(DC_HPD4_INT_CONTROL) & ~DC_HPDx_INT_EN;
3012                 if (ASIC_IS_DCE32(rdev)) {
3013                         hpd5 = RREG32(DC_HPD5_INT_CONTROL) & ~DC_HPDx_INT_EN;
3014                         hpd6 = RREG32(DC_HPD6_INT_CONTROL) & ~DC_HPDx_INT_EN;
3015                 }
3016         } else {
3017                 hdmi2 = RREG32(R600_HDMI_BLOCK2 + R600_HDMI_CNTL) & ~R600_HDMI_INT_EN;
3018                 hpd1 = RREG32(DC_HOT_PLUG_DETECT1_INT_CONTROL) & ~DC_HPDx_INT_EN;
3019                 hpd2 = RREG32(DC_HOT_PLUG_DETECT2_INT_CONTROL) & ~DC_HPDx_INT_EN;
3020                 hpd3 = RREG32(DC_HOT_PLUG_DETECT3_INT_CONTROL) & ~DC_HPDx_INT_EN;
3021         }
3022
3023         if (rdev->irq.sw_int) {
3024                 DRM_DEBUG("r600_irq_set: sw int\n");
3025                 cp_int_cntl |= RB_INT_ENABLE;
3026                 cp_int_cntl |= TIME_STAMP_INT_ENABLE;
3027         }
3028         if (rdev->irq.crtc_vblank_int[0]) {
3029                 DRM_DEBUG("r600_irq_set: vblank 0\n");
3030                 mode_int |= D1MODE_VBLANK_INT_MASK;
3031         }
3032         if (rdev->irq.crtc_vblank_int[1]) {
3033                 DRM_DEBUG("r600_irq_set: vblank 1\n");
3034                 mode_int |= D2MODE_VBLANK_INT_MASK;
3035         }
3036         if (rdev->irq.hpd[0]) {